Commit | Line | Data |
---|---|---|
0d3244d6 MD |
1 | /* |
2 | * V4L2 Driver for SuperH Mobile CEU interface | |
3 | * | |
4 | * Copyright (C) 2008 Magnus Damm | |
5 | * | |
6 | * Based on V4L2 Driver for PXA camera host - "pxa_camera.c", | |
7 | * | |
8 | * Copyright (C) 2006, Sascha Hauer, Pengutronix | |
9 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | */ | |
16 | ||
17 | #include <linux/init.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/io.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/dma-mapping.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/fs.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/moduleparam.h> | |
28 | #include <linux/time.h> | |
29 | #include <linux/version.h> | |
30 | #include <linux/device.h> | |
31 | #include <linux/platform_device.h> | |
0d3244d6 | 32 | #include <linux/videodev2.h> |
6d1386c6 | 33 | #include <linux/pm_runtime.h> |
f39c1ab3 | 34 | #include <linux/sched.h> |
0d3244d6 MD |
35 | |
36 | #include <media/v4l2-common.h> | |
37 | #include <media/v4l2-dev.h> | |
38 | #include <media/soc_camera.h> | |
39 | #include <media/sh_mobile_ceu.h> | |
40 | #include <media/videobuf-dma-contig.h> | |
760697be GL |
41 | #include <media/v4l2-mediabus.h> |
42 | #include <media/soc_mediabus.h> | |
0d3244d6 MD |
43 | |
44 | /* register offsets for sh7722 / sh7723 */ | |
45 | ||
dd54203b MD |
46 | #define CAPSR 0x00 /* Capture start register */ |
47 | #define CAPCR 0x04 /* Capture control register */ | |
48 | #define CAMCR 0x08 /* Capture interface control register */ | |
49 | #define CMCYR 0x0c /* Capture interface cycle register */ | |
50 | #define CAMOR 0x10 /* Capture interface offset register */ | |
51 | #define CAPWR 0x14 /* Capture interface width register */ | |
52 | #define CAIFR 0x18 /* Capture interface input format register */ | |
53 | #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */ | |
54 | #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */ | |
55 | #define CRCNTR 0x28 /* CEU register control register */ | |
56 | #define CRCMPR 0x2c /* CEU register forcible control register */ | |
57 | #define CFLCR 0x30 /* Capture filter control register */ | |
58 | #define CFSZR 0x34 /* Capture filter size clip register */ | |
59 | #define CDWDR 0x38 /* Capture destination width register */ | |
60 | #define CDAYR 0x3c /* Capture data address Y register */ | |
61 | #define CDACR 0x40 /* Capture data address C register */ | |
62 | #define CDBYR 0x44 /* Capture data bottom-field address Y register */ | |
63 | #define CDBCR 0x48 /* Capture data bottom-field address C register */ | |
64 | #define CBDSR 0x4c /* Capture bundle destination size register */ | |
65 | #define CFWCR 0x5c /* Firewall operation control register */ | |
66 | #define CLFCR 0x60 /* Capture low-pass filter control register */ | |
67 | #define CDOCR 0x64 /* Capture data output control register */ | |
68 | #define CDDCR 0x68 /* Capture data complexity level register */ | |
69 | #define CDDAR 0x6c /* Capture data complexity level address register */ | |
70 | #define CEIER 0x70 /* Capture event interrupt enable register */ | |
71 | #define CETCR 0x74 /* Capture event flag clear register */ | |
72 | #define CSTSR 0x7c /* Capture status register */ | |
73 | #define CSRTR 0x80 /* Capture software reset register */ | |
74 | #define CDSSR 0x84 /* Capture data size register */ | |
75 | #define CDAYR2 0x90 /* Capture data address Y register 2 */ | |
76 | #define CDACR2 0x94 /* Capture data address C register 2 */ | |
77 | #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */ | |
78 | #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */ | |
0d3244d6 | 79 | |
6a6c8786 GL |
80 | #undef DEBUG_GEOMETRY |
81 | #ifdef DEBUG_GEOMETRY | |
82 | #define dev_geo dev_info | |
83 | #else | |
84 | #define dev_geo dev_dbg | |
85 | #endif | |
86 | ||
0d3244d6 MD |
87 | /* per video frame buffer */ |
88 | struct sh_mobile_ceu_buffer { | |
89 | struct videobuf_buffer vb; /* v4l buffer must be first */ | |
760697be | 90 | enum v4l2_mbus_pixelcode code; |
0d3244d6 MD |
91 | }; |
92 | ||
93 | struct sh_mobile_ceu_dev { | |
0d3244d6 MD |
94 | struct soc_camera_host ici; |
95 | struct soc_camera_device *icd; | |
96 | ||
97 | unsigned int irq; | |
98 | void __iomem *base; | |
99 | unsigned long video_limit; | |
100 | ||
c60f2b5c | 101 | /* lock used to protect videobuf */ |
0d3244d6 MD |
102 | spinlock_t lock; |
103 | struct list_head capture; | |
104 | struct videobuf_buffer *active; | |
105 | ||
106 | struct sh_mobile_ceu_info *pdata; | |
8be65dc5 | 107 | |
961801bb GL |
108 | u32 cflcr; |
109 | ||
1edcc10d KM |
110 | enum v4l2_field field; |
111 | ||
904078f1 GL |
112 | unsigned int image_mode:1; |
113 | unsigned int is_16bit:1; | |
114 | }; | |
115 | ||
116 | struct sh_mobile_ceu_cam { | |
6a6c8786 GL |
117 | struct v4l2_rect ceu_rect; |
118 | unsigned int cam_width; | |
119 | unsigned int cam_height; | |
760697be GL |
120 | const struct soc_mbus_pixelfmt *extra_fmt; |
121 | enum v4l2_mbus_pixelcode code; | |
0d3244d6 MD |
122 | }; |
123 | ||
c354b400 KM |
124 | static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev) |
125 | { | |
126 | unsigned long flags; | |
127 | ||
128 | flags = SOCAM_MASTER | | |
129 | SOCAM_PCLK_SAMPLE_RISING | | |
130 | SOCAM_HSYNC_ACTIVE_HIGH | | |
131 | SOCAM_HSYNC_ACTIVE_LOW | | |
132 | SOCAM_VSYNC_ACTIVE_HIGH | | |
133 | SOCAM_VSYNC_ACTIVE_LOW | | |
134 | SOCAM_DATA_ACTIVE_HIGH; | |
135 | ||
136 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS) | |
137 | flags |= SOCAM_DATAWIDTH_8; | |
138 | ||
139 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS) | |
140 | flags |= SOCAM_DATAWIDTH_16; | |
141 | ||
142 | if (flags & SOCAM_DATAWIDTH_MASK) | |
143 | return flags; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
0d3244d6 | 148 | static void ceu_write(struct sh_mobile_ceu_dev *priv, |
7a1a8164 | 149 | unsigned long reg_offs, u32 data) |
0d3244d6 MD |
150 | { |
151 | iowrite32(data, priv->base + reg_offs); | |
152 | } | |
153 | ||
7a1a8164 | 154 | static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs) |
0d3244d6 MD |
155 | { |
156 | return ioread32(priv->base + reg_offs); | |
157 | } | |
158 | ||
b1de7aeb KM |
159 | static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev) |
160 | { | |
161 | int i, success = 0; | |
162 | struct soc_camera_device *icd = pcdev->icd; | |
163 | ||
164 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ | |
165 | ||
166 | /* wait CSTSR.CPTON bit */ | |
167 | for (i = 0; i < 1000; i++) { | |
168 | if (!(ceu_read(pcdev, CSTSR) & 1)) { | |
169 | success++; | |
170 | break; | |
171 | } | |
172 | udelay(1); | |
173 | } | |
174 | ||
175 | /* wait CAPSR.CPKIL bit */ | |
176 | for (i = 0; i < 1000; i++) { | |
177 | if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) { | |
178 | success++; | |
179 | break; | |
180 | } | |
181 | udelay(1); | |
182 | } | |
183 | ||
184 | ||
185 | if (2 != success) { | |
186 | dev_warn(&icd->dev, "soft reset time out\n"); | |
187 | return -EIO; | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
0d3244d6 MD |
193 | /* |
194 | * Videobuf operations | |
195 | */ | |
196 | static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq, | |
197 | unsigned int *count, | |
198 | unsigned int *size) | |
199 | { | |
200 | struct soc_camera_device *icd = vq->priv_data; | |
201 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
202 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
760697be GL |
203 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
204 | icd->current_fmt->host_fmt); | |
0d3244d6 | 205 | |
760697be GL |
206 | if (bytes_per_line < 0) |
207 | return bytes_per_line; | |
208 | ||
209 | *size = PAGE_ALIGN(bytes_per_line * icd->user_height); | |
0d3244d6 MD |
210 | |
211 | if (0 == *count) | |
212 | *count = 2; | |
213 | ||
214 | if (pcdev->video_limit) { | |
215 | while (*size * *count > pcdev->video_limit) | |
216 | (*count)--; | |
217 | } | |
218 | ||
0166b743 | 219 | dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size); |
0d3244d6 MD |
220 | |
221 | return 0; | |
222 | } | |
223 | ||
224 | static void free_buffer(struct videobuf_queue *vq, | |
225 | struct sh_mobile_ceu_buffer *buf) | |
226 | { | |
227 | struct soc_camera_device *icd = vq->priv_data; | |
6a6c8786 | 228 | struct device *dev = icd->dev.parent; |
0d3244d6 | 229 | |
6a6c8786 | 230 | dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
231 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
232 | ||
233 | if (in_interrupt()) | |
234 | BUG(); | |
235 | ||
97215cbd | 236 | videobuf_waiton(&buf->vb, 0, 0); |
0d3244d6 | 237 | videobuf_dma_contig_free(vq, &buf->vb); |
6a6c8786 | 238 | dev_dbg(dev, "%s freed\n", __func__); |
0d3244d6 MD |
239 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
240 | } | |
241 | ||
7a1a8164 MD |
242 | #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ |
243 | #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ | |
244 | #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ | |
5cf93f1d | 245 | #define CEU_CEIER_VBP (1 << 20) /* vbp error */ |
7a1a8164 | 246 | #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ |
5cf93f1d | 247 | #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP) |
7a1a8164 MD |
248 | |
249 | ||
5cf93f1d KM |
250 | /* |
251 | * return value doesn't reflex the success/failure to queue the new buffer, | |
252 | * but rather the status of the previous buffer. | |
253 | */ | |
254 | static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) | |
0d3244d6 | 255 | { |
8be65dc5 | 256 | struct soc_camera_device *icd = pcdev->icd; |
ccab8a29 | 257 | dma_addr_t phys_addr_top, phys_addr_bottom; |
1edcc10d KM |
258 | unsigned long top1, top2; |
259 | unsigned long bottom1, bottom2; | |
5cf93f1d KM |
260 | u32 status; |
261 | int ret = 0; | |
7a1a8164 | 262 | |
5d28d525 GL |
263 | /* |
264 | * The hardware is _very_ picky about this sequence. Especially | |
7a1a8164 MD |
265 | * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge |
266 | * several not-so-well documented interrupt sources in CETCR. | |
267 | */ | |
5cf93f1d KM |
268 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK); |
269 | status = ceu_read(pcdev, CETCR); | |
270 | ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC); | |
271 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK); | |
7a1a8164 MD |
272 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); |
273 | ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); | |
0d3244d6 | 274 | |
5cf93f1d KM |
275 | /* |
276 | * When a VBP interrupt occurs, a capture end interrupt does not occur | |
277 | * and the image of that frame is not captured correctly. So, soft reset | |
278 | * is needed here. | |
279 | */ | |
280 | if (status & CEU_CEIER_VBP) { | |
281 | sh_mobile_ceu_soft_reset(pcdev); | |
282 | ret = -EIO; | |
283 | } | |
284 | ||
8be65dc5 | 285 | if (!pcdev->active) |
5cf93f1d | 286 | return ret; |
8be65dc5 | 287 | |
1edcc10d KM |
288 | if (V4L2_FIELD_INTERLACED_BT == pcdev->field) { |
289 | top1 = CDBYR; | |
290 | top2 = CDBCR; | |
291 | bottom1 = CDAYR; | |
292 | bottom2 = CDACR; | |
293 | } else { | |
294 | top1 = CDAYR; | |
295 | top2 = CDACR; | |
296 | bottom1 = CDBYR; | |
297 | bottom2 = CDBCR; | |
298 | } | |
299 | ||
ccab8a29 | 300 | phys_addr_top = videobuf_to_dma_contig(pcdev->active); |
1edcc10d KM |
301 | ceu_write(pcdev, top1, phys_addr_top); |
302 | if (V4L2_FIELD_NONE != pcdev->field) { | |
6a6c8786 | 303 | phys_addr_bottom = phys_addr_top + icd->user_width; |
1edcc10d | 304 | ceu_write(pcdev, bottom1, phys_addr_bottom); |
ccab8a29 | 305 | } |
8be65dc5 | 306 | |
760697be | 307 | switch (icd->current_fmt->host_fmt->fourcc) { |
8be65dc5 MD |
308 | case V4L2_PIX_FMT_NV12: |
309 | case V4L2_PIX_FMT_NV21: | |
9e4a56d2 MD |
310 | case V4L2_PIX_FMT_NV16: |
311 | case V4L2_PIX_FMT_NV61: | |
6a6c8786 GL |
312 | phys_addr_top += icd->user_width * |
313 | icd->user_height; | |
1edcc10d KM |
314 | ceu_write(pcdev, top2, phys_addr_top); |
315 | if (V4L2_FIELD_NONE != pcdev->field) { | |
316 | phys_addr_bottom = phys_addr_top + icd->user_width; | |
317 | ceu_write(pcdev, bottom2, phys_addr_bottom); | |
ccab8a29 | 318 | } |
0d3244d6 | 319 | } |
8be65dc5 MD |
320 | |
321 | pcdev->active->state = VIDEOBUF_ACTIVE; | |
322 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ | |
5cf93f1d KM |
323 | |
324 | return ret; | |
0d3244d6 MD |
325 | } |
326 | ||
327 | static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq, | |
328 | struct videobuf_buffer *vb, | |
329 | enum v4l2_field field) | |
330 | { | |
331 | struct soc_camera_device *icd = vq->priv_data; | |
332 | struct sh_mobile_ceu_buffer *buf; | |
760697be GL |
333 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
334 | icd->current_fmt->host_fmt); | |
0d3244d6 MD |
335 | int ret; |
336 | ||
760697be GL |
337 | if (bytes_per_line < 0) |
338 | return bytes_per_line; | |
339 | ||
0d3244d6 MD |
340 | buf = container_of(vb, struct sh_mobile_ceu_buffer, vb); |
341 | ||
0166b743 | 342 | dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
343 | vb, vb->baddr, vb->bsize); |
344 | ||
345 | /* Added list head initialization on alloc */ | |
346 | WARN_ON(!list_empty(&vb->queue)); | |
347 | ||
348 | #ifdef DEBUG | |
5d28d525 GL |
349 | /* |
350 | * This can be useful if you want to see if we actually fill | |
351 | * the buffer with something | |
352 | */ | |
0d3244d6 MD |
353 | memset((void *)vb->baddr, 0xaa, vb->bsize); |
354 | #endif | |
355 | ||
356 | BUG_ON(NULL == icd->current_fmt); | |
357 | ||
760697be | 358 | if (buf->code != icd->current_fmt->code || |
6a6c8786 GL |
359 | vb->width != icd->user_width || |
360 | vb->height != icd->user_height || | |
0d3244d6 | 361 | vb->field != field) { |
760697be | 362 | buf->code = icd->current_fmt->code; |
6a6c8786 GL |
363 | vb->width = icd->user_width; |
364 | vb->height = icd->user_height; | |
0d3244d6 MD |
365 | vb->field = field; |
366 | vb->state = VIDEOBUF_NEEDS_INIT; | |
367 | } | |
368 | ||
760697be | 369 | vb->size = vb->height * bytes_per_line; |
0d3244d6 MD |
370 | if (0 != vb->baddr && vb->bsize < vb->size) { |
371 | ret = -EINVAL; | |
372 | goto out; | |
373 | } | |
374 | ||
375 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
376 | ret = videobuf_iolock(vq, vb, NULL); | |
377 | if (ret) | |
378 | goto fail; | |
379 | vb->state = VIDEOBUF_PREPARED; | |
380 | } | |
381 | ||
382 | return 0; | |
383 | fail: | |
384 | free_buffer(vq, buf); | |
385 | out: | |
386 | return ret; | |
387 | } | |
388 | ||
2dd54a54 | 389 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
0d3244d6 MD |
390 | static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq, |
391 | struct videobuf_buffer *vb) | |
392 | { | |
393 | struct soc_camera_device *icd = vq->priv_data; | |
394 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
395 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
0d3244d6 | 396 | |
0166b743 | 397 | dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
398 | vb, vb->baddr, vb->bsize); |
399 | ||
51354cc3 | 400 | vb->state = VIDEOBUF_QUEUED; |
0d3244d6 MD |
401 | list_add_tail(&vb->queue, &pcdev->capture); |
402 | ||
403 | if (!pcdev->active) { | |
5cf93f1d KM |
404 | /* |
405 | * Because there were no active buffer at this moment, | |
406 | * we are not interested in the return value of | |
407 | * sh_mobile_ceu_capture here. | |
408 | */ | |
0d3244d6 MD |
409 | pcdev->active = vb; |
410 | sh_mobile_ceu_capture(pcdev); | |
411 | } | |
0d3244d6 MD |
412 | } |
413 | ||
414 | static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq, | |
415 | struct videobuf_buffer *vb) | |
416 | { | |
cca0e549 GL |
417 | struct soc_camera_device *icd = vq->priv_data; |
418 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
419 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
420 | unsigned long flags; | |
421 | ||
422 | spin_lock_irqsave(&pcdev->lock, flags); | |
423 | ||
424 | if (pcdev->active == vb) { | |
425 | /* disable capture (release DMA buffer), reset */ | |
426 | ceu_write(pcdev, CAPSR, 1 << 16); | |
427 | pcdev->active = NULL; | |
428 | } | |
429 | ||
430 | if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) && | |
431 | !list_empty(&vb->queue)) { | |
432 | vb->state = VIDEOBUF_ERROR; | |
433 | list_del_init(&vb->queue); | |
434 | } | |
435 | ||
436 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
437 | ||
0d3244d6 MD |
438 | free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb)); |
439 | } | |
440 | ||
441 | static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = { | |
442 | .buf_setup = sh_mobile_ceu_videobuf_setup, | |
443 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, | |
444 | .buf_queue = sh_mobile_ceu_videobuf_queue, | |
445 | .buf_release = sh_mobile_ceu_videobuf_release, | |
446 | }; | |
447 | ||
448 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) | |
449 | { | |
450 | struct sh_mobile_ceu_dev *pcdev = data; | |
451 | struct videobuf_buffer *vb; | |
452 | unsigned long flags; | |
453 | ||
454 | spin_lock_irqsave(&pcdev->lock, flags); | |
455 | ||
456 | vb = pcdev->active; | |
cca0e549 GL |
457 | if (!vb) |
458 | /* Stale interrupt from a released buffer */ | |
459 | goto out; | |
460 | ||
0d3244d6 MD |
461 | list_del_init(&vb->queue); |
462 | ||
463 | if (!list_empty(&pcdev->capture)) | |
464 | pcdev->active = list_entry(pcdev->capture.next, | |
465 | struct videobuf_buffer, queue); | |
466 | else | |
467 | pcdev->active = NULL; | |
468 | ||
5cf93f1d KM |
469 | vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ? |
470 | VIDEOBUF_ERROR : VIDEOBUF_DONE; | |
0d3244d6 MD |
471 | do_gettimeofday(&vb->ts); |
472 | vb->field_count++; | |
473 | wake_up(&vb->done); | |
cca0e549 GL |
474 | |
475 | out: | |
0d3244d6 MD |
476 | spin_unlock_irqrestore(&pcdev->lock, flags); |
477 | ||
478 | return IRQ_HANDLED; | |
479 | } | |
480 | ||
1c3bb743 | 481 | /* Called with .video_lock held */ |
0d3244d6 MD |
482 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) |
483 | { | |
484 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
485 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
6ed7c038 | 486 | int ret; |
0d3244d6 | 487 | |
0d3244d6 | 488 | if (pcdev->icd) |
979ea1dd | 489 | return -EBUSY; |
0d3244d6 | 490 | |
0166b743 | 491 | dev_info(icd->dev.parent, |
0d3244d6 MD |
492 | "SuperH Mobile CEU driver attached to camera %d\n", |
493 | icd->devnum); | |
494 | ||
a79aebfc | 495 | pm_runtime_get_sync(ici->v4l2_dev.dev); |
40e2e092 | 496 | |
6ed7c038 GL |
497 | ret = sh_mobile_ceu_soft_reset(pcdev); |
498 | if (!ret) | |
499 | pcdev->icd = icd; | |
979ea1dd | 500 | |
6ed7c038 | 501 | return ret; |
0d3244d6 MD |
502 | } |
503 | ||
1c3bb743 | 504 | /* Called with .video_lock held */ |
0d3244d6 MD |
505 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) |
506 | { | |
507 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
508 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
51354cc3 | 509 | unsigned long flags; |
0d3244d6 MD |
510 | |
511 | BUG_ON(icd != pcdev->icd); | |
512 | ||
513 | /* disable capture, disable interrupts */ | |
514 | ceu_write(pcdev, CEIER, 0); | |
b1de7aeb | 515 | sh_mobile_ceu_soft_reset(pcdev); |
51354cc3 MD |
516 | |
517 | /* make sure active buffer is canceled */ | |
518 | spin_lock_irqsave(&pcdev->lock, flags); | |
519 | if (pcdev->active) { | |
520 | list_del(&pcdev->active->queue); | |
521 | pcdev->active->state = VIDEOBUF_ERROR; | |
522 | wake_up_all(&pcdev->active->done); | |
523 | pcdev->active = NULL; | |
524 | } | |
525 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
526 | ||
a79aebfc | 527 | pm_runtime_put_sync(ici->v4l2_dev.dev); |
40e2e092 | 528 | |
0166b743 | 529 | dev_info(icd->dev.parent, |
0d3244d6 MD |
530 | "SuperH Mobile CEU driver detached from camera %d\n", |
531 | icd->devnum); | |
532 | ||
533 | pcdev->icd = NULL; | |
534 | } | |
535 | ||
961801bb GL |
536 | /* |
537 | * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)" | |
538 | * in SH7722 Hardware Manual | |
539 | */ | |
540 | static unsigned int size_dst(unsigned int src, unsigned int scale) | |
541 | { | |
542 | unsigned int mant_pre = scale >> 12; | |
543 | if (!src || !scale) | |
544 | return src; | |
545 | return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) * | |
546 | mant_pre * 4096 / scale + 1; | |
547 | } | |
548 | ||
961801bb GL |
549 | static u16 calc_scale(unsigned int src, unsigned int *dst) |
550 | { | |
551 | u16 scale; | |
552 | ||
553 | if (src == *dst) | |
554 | return 0; | |
555 | ||
556 | scale = (src * 4096 / *dst) & ~7; | |
557 | ||
558 | while (scale > 4096 && size_dst(src, scale) < *dst) | |
559 | scale -= 8; | |
560 | ||
561 | *dst = size_dst(src, scale); | |
562 | ||
563 | return scale; | |
564 | } | |
565 | ||
566 | /* rect is guaranteed to not exceed the scaled camera rectangle */ | |
904078f1 | 567 | static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd, |
6a6c8786 GL |
568 | unsigned int out_width, |
569 | unsigned int out_height) | |
904078f1 GL |
570 | { |
571 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
572 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
6a6c8786 | 573 | struct v4l2_rect *rect = &cam->ceu_rect; |
904078f1 | 574 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
6a6c8786 GL |
575 | unsigned int height, width, cdwdr_width, in_width, in_height; |
576 | unsigned int left_offset, top_offset; | |
904078f1 GL |
577 | u32 camor; |
578 | ||
6a6c8786 GL |
579 | dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n", |
580 | rect->width, rect->height, rect->left, rect->top); | |
904078f1 | 581 | |
6a6c8786 GL |
582 | left_offset = rect->left; |
583 | top_offset = rect->top; | |
904078f1 GL |
584 | |
585 | if (pcdev->image_mode) { | |
6a6c8786 | 586 | in_width = rect->width; |
961801bb | 587 | if (!pcdev->is_16bit) { |
961801bb GL |
588 | in_width *= 2; |
589 | left_offset *= 2; | |
590 | } | |
760697be GL |
591 | width = out_width; |
592 | cdwdr_width = out_width; | |
904078f1 | 593 | } else { |
760697be GL |
594 | int bytes_per_line = soc_mbus_bytes_per_line(out_width, |
595 | icd->current_fmt->host_fmt); | |
596 | unsigned int w_factor; | |
6a6c8786 | 597 | |
760697be | 598 | width = out_width; |
6a6c8786 | 599 | |
760697be GL |
600 | switch (icd->current_fmt->host_fmt->packing) { |
601 | case SOC_MBUS_PACKING_2X8_PADHI: | |
602 | w_factor = 2; | |
603 | break; | |
604 | default: | |
605 | w_factor = 1; | |
606 | } | |
961801bb | 607 | |
760697be GL |
608 | in_width = rect->width * w_factor; |
609 | left_offset = left_offset * w_factor; | |
961801bb | 610 | |
760697be GL |
611 | if (bytes_per_line < 0) |
612 | cdwdr_width = out_width; | |
613 | else | |
614 | cdwdr_width = bytes_per_line; | |
904078f1 GL |
615 | } |
616 | ||
6a6c8786 GL |
617 | height = out_height; |
618 | in_height = rect->height; | |
1edcc10d | 619 | if (V4L2_FIELD_NONE != pcdev->field) { |
904078f1 | 620 | height /= 2; |
961801bb GL |
621 | in_height /= 2; |
622 | top_offset /= 2; | |
904078f1 GL |
623 | cdwdr_width *= 2; |
624 | } | |
625 | ||
6a6c8786 | 626 | /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */ |
904078f1 | 627 | camor = left_offset | (top_offset << 16); |
6a6c8786 GL |
628 | |
629 | dev_geo(icd->dev.parent, | |
630 | "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor, | |
631 | (in_height << 16) | in_width, (height << 16) | width, | |
632 | cdwdr_width); | |
633 | ||
904078f1 | 634 | ceu_write(pcdev, CAMOR, camor); |
961801bb | 635 | ceu_write(pcdev, CAPWR, (in_height << 16) | in_width); |
6a6c8786 | 636 | ceu_write(pcdev, CFSZR, (height << 16) | width); |
904078f1 GL |
637 | ceu_write(pcdev, CDWDR, cdwdr_width); |
638 | } | |
639 | ||
640 | static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev) | |
641 | { | |
642 | u32 capsr = ceu_read(pcdev, CAPSR); | |
643 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */ | |
644 | return capsr; | |
645 | } | |
646 | ||
647 | static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr) | |
648 | { | |
649 | unsigned long timeout = jiffies + 10 * HZ; | |
650 | ||
651 | /* | |
652 | * Wait until the end of the current frame. It can take a long time, | |
653 | * but if it has been aborted by a CAPSR reset, it shoule exit sooner. | |
654 | */ | |
655 | while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout)) | |
656 | msleep(1); | |
657 | ||
658 | if (time_after(jiffies, timeout)) { | |
659 | dev_err(pcdev->ici.v4l2_dev.dev, | |
660 | "Timeout waiting for frame end! Interface problem?\n"); | |
661 | return; | |
662 | } | |
663 | ||
664 | /* Wait until reset clears, this shall not hang... */ | |
665 | while (ceu_read(pcdev, CAPSR) & (1 << 16)) | |
666 | udelay(10); | |
667 | ||
668 | /* Anything to restore? */ | |
669 | if (capsr & ~(1 << 16)) | |
670 | ceu_write(pcdev, CAPSR, capsr); | |
671 | } | |
672 | ||
0d3244d6 MD |
673 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, |
674 | __u32 pixfmt) | |
675 | { | |
676 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
677 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
904078f1 | 678 | int ret; |
0d3244d6 | 679 | unsigned long camera_flags, common_flags, value; |
904078f1 GL |
680 | int yuv_lineskip; |
681 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
682 | u32 capsr = capture_save_reset(pcdev); | |
0d3244d6 MD |
683 | |
684 | camera_flags = icd->ops->query_bus_param(icd); | |
685 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
c354b400 | 686 | make_bus_param(pcdev)); |
0d3244d6 MD |
687 | if (!common_flags) |
688 | return -EINVAL; | |
689 | ||
690 | ret = icd->ops->set_bus_param(icd, common_flags); | |
691 | if (ret < 0) | |
692 | return ret; | |
693 | ||
694 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { | |
695 | case SOCAM_DATAWIDTH_8: | |
904078f1 | 696 | pcdev->is_16bit = 0; |
0d3244d6 MD |
697 | break; |
698 | case SOCAM_DATAWIDTH_16: | |
904078f1 | 699 | pcdev->is_16bit = 1; |
0d3244d6 MD |
700 | break; |
701 | default: | |
702 | return -EINVAL; | |
703 | } | |
704 | ||
705 | ceu_write(pcdev, CRCNTR, 0); | |
706 | ceu_write(pcdev, CRCMPR, 0); | |
707 | ||
8be65dc5 | 708 | value = 0x00000010; /* data fetch by default */ |
961801bb | 709 | yuv_lineskip = 0; |
8be65dc5 | 710 | |
760697be | 711 | switch (icd->current_fmt->host_fmt->fourcc) { |
8be65dc5 MD |
712 | case V4L2_PIX_FMT_NV12: |
713 | case V4L2_PIX_FMT_NV21: | |
714 | yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */ | |
9e4a56d2 MD |
715 | /* fall-through */ |
716 | case V4L2_PIX_FMT_NV16: | |
717 | case V4L2_PIX_FMT_NV61: | |
760697be GL |
718 | switch (cam->code) { |
719 | case V4L2_MBUS_FMT_YUYV8_2X8_BE: | |
8be65dc5 MD |
720 | value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ |
721 | break; | |
760697be | 722 | case V4L2_MBUS_FMT_YVYU8_2X8_BE: |
8be65dc5 MD |
723 | value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ |
724 | break; | |
760697be | 725 | case V4L2_MBUS_FMT_YUYV8_2X8_LE: |
8be65dc5 MD |
726 | value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ |
727 | break; | |
760697be | 728 | case V4L2_MBUS_FMT_YVYU8_2X8_LE: |
8be65dc5 MD |
729 | value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ |
730 | break; | |
731 | default: | |
732 | BUG(); | |
733 | } | |
734 | } | |
735 | ||
760697be GL |
736 | if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 || |
737 | icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61) | |
9e4a56d2 | 738 | value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ |
8be65dc5 | 739 | |
7a1a8164 MD |
740 | value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; |
741 | value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; | |
904078f1 | 742 | value |= pcdev->is_16bit ? 1 << 12 : 0; |
0d3244d6 MD |
743 | ceu_write(pcdev, CAMCR, value); |
744 | ||
745 | ceu_write(pcdev, CAPCR, 0x00300000); | |
1edcc10d KM |
746 | |
747 | switch (pcdev->field) { | |
748 | case V4L2_FIELD_INTERLACED_TB: | |
749 | value = 0x101; | |
750 | break; | |
751 | case V4L2_FIELD_INTERLACED_BT: | |
752 | value = 0x102; | |
753 | break; | |
754 | default: | |
755 | value = 0; | |
756 | break; | |
757 | } | |
758 | ceu_write(pcdev, CAIFR, value); | |
0d3244d6 | 759 | |
6a6c8786 | 760 | sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height); |
0d3244d6 MD |
761 | mdelay(1); |
762 | ||
961801bb | 763 | ceu_write(pcdev, CFLCR, pcdev->cflcr); |
dd54203b | 764 | |
5d28d525 GL |
765 | /* |
766 | * A few words about byte order (observed in Big Endian mode) | |
dd54203b MD |
767 | * |
768 | * In data fetch mode bytes are received in chunks of 8 bytes. | |
769 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) | |
770 | * | |
771 | * The data is however by default written to memory in reverse order: | |
772 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) | |
773 | * | |
774 | * The lowest three bits of CDOCR allows us to do swapping, | |
2c0a072e MD |
775 | * using 7 we swap the data bytes to match the incoming order: |
776 | * D0, D1, D2, D3, D4, D5, D6, D7 | |
dd54203b | 777 | */ |
8be65dc5 MD |
778 | value = 0x00000017; |
779 | if (yuv_lineskip) | |
780 | value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */ | |
781 | ||
782 | ceu_write(pcdev, CDOCR, value); | |
0d3244d6 MD |
783 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ |
784 | ||
6a6c8786 | 785 | dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n", |
904078f1 GL |
786 | pixfmt & 0xff, (pixfmt >> 8) & 0xff, |
787 | (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff, | |
6a6c8786 | 788 | icd->user_width, icd->user_height); |
904078f1 GL |
789 | |
790 | capture_restore(pcdev, capsr); | |
791 | ||
0d3244d6 | 792 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ |
0d3244d6 MD |
793 | return 0; |
794 | } | |
795 | ||
760697be GL |
796 | static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd, |
797 | unsigned char buswidth) | |
0d3244d6 MD |
798 | { |
799 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
800 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
801 | unsigned long camera_flags, common_flags; | |
802 | ||
803 | camera_flags = icd->ops->query_bus_param(icd); | |
804 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
c354b400 | 805 | make_bus_param(pcdev)); |
760697be GL |
806 | if (!common_flags || buswidth > 16 || |
807 | (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16))) | |
0d3244d6 MD |
808 | return -EINVAL; |
809 | ||
810 | return 0; | |
811 | } | |
812 | ||
760697be | 813 | static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = { |
9e4a56d2 | 814 | { |
760697be GL |
815 | .fourcc = V4L2_PIX_FMT_NV12, |
816 | .name = "NV12", | |
817 | .bits_per_sample = 12, | |
818 | .packing = SOC_MBUS_PACKING_NONE, | |
819 | .order = SOC_MBUS_ORDER_LE, | |
820 | }, { | |
821 | .fourcc = V4L2_PIX_FMT_NV21, | |
822 | .name = "NV21", | |
823 | .bits_per_sample = 12, | |
824 | .packing = SOC_MBUS_PACKING_NONE, | |
825 | .order = SOC_MBUS_ORDER_LE, | |
826 | }, { | |
827 | .fourcc = V4L2_PIX_FMT_NV16, | |
828 | .name = "NV16", | |
829 | .bits_per_sample = 16, | |
830 | .packing = SOC_MBUS_PACKING_NONE, | |
831 | .order = SOC_MBUS_ORDER_LE, | |
832 | }, { | |
833 | .fourcc = V4L2_PIX_FMT_NV61, | |
834 | .name = "NV61", | |
835 | .bits_per_sample = 16, | |
836 | .packing = SOC_MBUS_PACKING_NONE, | |
837 | .order = SOC_MBUS_ORDER_LE, | |
9e4a56d2 | 838 | }, |
8be65dc5 MD |
839 | }; |
840 | ||
760697be GL |
841 | /* This will be corrected as we get more formats */ |
842 | static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt) | |
843 | { | |
844 | return fmt->packing == SOC_MBUS_PACKING_NONE || | |
845 | (fmt->bits_per_sample == 8 && | |
846 | fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) || | |
847 | (fmt->bits_per_sample > 8 && | |
848 | fmt->packing == SOC_MBUS_PACKING_EXTEND16); | |
849 | } | |
850 | ||
9414de39 MD |
851 | static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, |
852 | struct soc_camera_format_xlate *xlate) | |
853 | { | |
760697be | 854 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
6a6c8786 | 855 | struct device *dev = icd->dev.parent; |
8be65dc5 | 856 | int ret, k, n; |
9414de39 | 857 | int formats = 0; |
904078f1 | 858 | struct sh_mobile_ceu_cam *cam; |
760697be GL |
859 | enum v4l2_mbus_pixelcode code; |
860 | const struct soc_mbus_pixelfmt *fmt; | |
9414de39 | 861 | |
760697be GL |
862 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); |
863 | if (ret < 0) | |
864 | /* No more formats */ | |
865 | return 0; | |
866 | ||
867 | fmt = soc_mbus_get_fmtdesc(code); | |
868 | if (!fmt) { | |
869 | dev_err(icd->dev.parent, | |
870 | "Invalid format code #%d: %d\n", idx, code); | |
871 | return -EINVAL; | |
872 | } | |
873 | ||
874 | ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample); | |
9414de39 MD |
875 | if (ret < 0) |
876 | return 0; | |
877 | ||
904078f1 GL |
878 | if (!icd->host_priv) { |
879 | cam = kzalloc(sizeof(*cam), GFP_KERNEL); | |
880 | if (!cam) | |
881 | return -ENOMEM; | |
882 | ||
883 | icd->host_priv = cam; | |
884 | } else { | |
885 | cam = icd->host_priv; | |
886 | } | |
887 | ||
c8329acc GL |
888 | /* Beginning of a pass */ |
889 | if (!idx) | |
904078f1 | 890 | cam->extra_fmt = NULL; |
c8329acc | 891 | |
760697be GL |
892 | switch (code) { |
893 | case V4L2_MBUS_FMT_YUYV8_2X8_BE: | |
894 | case V4L2_MBUS_FMT_YVYU8_2X8_BE: | |
895 | case V4L2_MBUS_FMT_YUYV8_2X8_LE: | |
896 | case V4L2_MBUS_FMT_YVYU8_2X8_LE: | |
904078f1 | 897 | if (cam->extra_fmt) |
760697be | 898 | break; |
c8329acc GL |
899 | |
900 | /* | |
901 | * Our case is simple so far: for any of the above four camera | |
902 | * formats we add all our four synthesized NV* formats, so, | |
903 | * just marking the device with a single flag suffices. If | |
904 | * the format generation rules are more complex, you would have | |
905 | * to actually hang your already added / counted formats onto | |
906 | * the host_priv pointer and check whether the format you're | |
907 | * going to add now is already there. | |
908 | */ | |
760697be | 909 | cam->extra_fmt = sh_mobile_ceu_formats; |
c8329acc | 910 | |
8be65dc5 MD |
911 | n = ARRAY_SIZE(sh_mobile_ceu_formats); |
912 | formats += n; | |
913 | for (k = 0; xlate && k < n; k++) { | |
760697be GL |
914 | xlate->host_fmt = &sh_mobile_ceu_formats[k]; |
915 | xlate->code = code; | |
8be65dc5 | 916 | xlate++; |
760697be GL |
917 | dev_dbg(dev, "Providing format %s using code %d\n", |
918 | sh_mobile_ceu_formats[k].name, code); | |
8be65dc5 | 919 | } |
760697be | 920 | break; |
9414de39 | 921 | default: |
760697be GL |
922 | if (!sh_mobile_ceu_packing_supported(fmt)) |
923 | return 0; | |
924 | } | |
925 | ||
926 | /* Generic pass-through */ | |
927 | formats++; | |
928 | if (xlate) { | |
929 | xlate->host_fmt = fmt; | |
930 | xlate->code = code; | |
931 | xlate++; | |
932 | dev_dbg(dev, "Providing format %s in pass-through mode\n", | |
933 | xlate->host_fmt->name); | |
9414de39 MD |
934 | } |
935 | ||
936 | return formats; | |
937 | } | |
938 | ||
904078f1 GL |
939 | static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd) |
940 | { | |
941 | kfree(icd->host_priv); | |
942 | icd->host_priv = NULL; | |
943 | } | |
944 | ||
961801bb | 945 | /* Check if any dimension of r1 is smaller than respective one of r2 */ |
904078f1 GL |
946 | static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2) |
947 | { | |
948 | return r1->width < r2->width || r1->height < r2->height; | |
949 | } | |
950 | ||
961801bb GL |
951 | /* Check if r1 fails to cover r2 */ |
952 | static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2) | |
953 | { | |
954 | return r1->left > r2->left || r1->top > r2->top || | |
955 | r1->left + r1->width < r2->left + r2->width || | |
956 | r1->top + r1->height < r2->top + r2->height; | |
957 | } | |
958 | ||
6a6c8786 GL |
959 | static unsigned int scale_down(unsigned int size, unsigned int scale) |
960 | { | |
961 | return (size * 4096 + scale / 2) / scale; | |
962 | } | |
963 | ||
964 | static unsigned int scale_up(unsigned int size, unsigned int scale) | |
965 | { | |
966 | return (size * scale + 2048) / 4096; | |
967 | } | |
968 | ||
969 | static unsigned int calc_generic_scale(unsigned int input, unsigned int output) | |
970 | { | |
971 | return (input * 4096 + output / 2) / output; | |
972 | } | |
973 | ||
974 | static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect) | |
975 | { | |
976 | struct v4l2_crop crop; | |
977 | struct v4l2_cropcap cap; | |
978 | int ret; | |
979 | ||
980 | crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
981 | ||
982 | ret = v4l2_subdev_call(sd, video, g_crop, &crop); | |
983 | if (!ret) { | |
984 | *rect = crop.c; | |
985 | return ret; | |
986 | } | |
987 | ||
988 | /* Camera driver doesn't support .g_crop(), assume default rectangle */ | |
989 | cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
990 | ||
991 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); | |
992 | if (ret < 0) | |
993 | return ret; | |
994 | ||
995 | *rect = cap.defrect; | |
996 | ||
997 | return ret; | |
998 | } | |
999 | ||
961801bb | 1000 | /* |
6a6c8786 | 1001 | * The common for both scaling and cropping iterative approach is: |
961801bb GL |
1002 | * 1. try if the client can produce exactly what requested by the user |
1003 | * 2. if (1) failed, try to double the client image until we get one big enough | |
1004 | * 3. if (2) failed, try to request the maximum image | |
1005 | */ | |
6a6c8786 GL |
1006 | static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, |
1007 | struct v4l2_crop *cam_crop) | |
09e231b3 | 1008 | { |
6a6c8786 GL |
1009 | struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c; |
1010 | struct device *dev = sd->v4l2_dev->dev; | |
1011 | struct v4l2_cropcap cap; | |
904078f1 | 1012 | int ret; |
6a6c8786 | 1013 | unsigned int width, height; |
904078f1 | 1014 | |
6a6c8786 GL |
1015 | v4l2_subdev_call(sd, video, s_crop, crop); |
1016 | ret = client_g_rect(sd, cam_rect); | |
1017 | if (ret < 0) | |
1018 | return ret; | |
961801bb | 1019 | |
6a6c8786 GL |
1020 | /* |
1021 | * Now cam_crop contains the current camera input rectangle, and it must | |
1022 | * be within camera cropcap bounds | |
1023 | */ | |
1024 | if (!memcmp(rect, cam_rect, sizeof(*rect))) { | |
1025 | /* Even if camera S_CROP failed, but camera rectangle matches */ | |
1026 | dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n", | |
1027 | rect->width, rect->height, rect->left, rect->top); | |
1028 | return 0; | |
904078f1 GL |
1029 | } |
1030 | ||
6a6c8786 GL |
1031 | /* Try to fix cropping, that camera hasn't managed to set */ |
1032 | dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n", | |
1033 | cam_rect->width, cam_rect->height, | |
08590b96 | 1034 | cam_rect->left, cam_rect->top, |
6a6c8786 GL |
1035 | rect->width, rect->height, rect->left, rect->top); |
1036 | ||
1037 | /* We need sensor maximum rectangle */ | |
1038 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); | |
1039 | if (ret < 0) | |
1040 | return ret; | |
1041 | ||
1042 | soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2, | |
1043 | cap.bounds.width); | |
1044 | soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4, | |
1045 | cap.bounds.height); | |
904078f1 GL |
1046 | |
1047 | /* | |
1048 | * Popular special case - some cameras can only handle fixed sizes like | |
1049 | * QVGA, VGA,... Take care to avoid infinite loop. | |
1050 | */ | |
6a6c8786 GL |
1051 | width = max(cam_rect->width, 2); |
1052 | height = max(cam_rect->height, 2); | |
1053 | ||
1054 | while (!ret && (is_smaller(cam_rect, rect) || | |
1055 | is_inside(cam_rect, rect)) && | |
1056 | (cap.bounds.width > width || cap.bounds.height > height)) { | |
961801bb GL |
1057 | |
1058 | width *= 2; | |
1059 | height *= 2; | |
6a6c8786 | 1060 | |
08590b96 GL |
1061 | cam_rect->width = width; |
1062 | cam_rect->height = height; | |
904078f1 | 1063 | |
6a6c8786 GL |
1064 | /* |
1065 | * We do not know what capabilities the camera has to set up | |
1066 | * left and top borders. We could try to be smarter in iterating | |
1067 | * them, e.g., if camera current left is to the right of the | |
1068 | * target left, set it to the middle point between the current | |
1069 | * left and minimum left. But that would add too much | |
1070 | * complexity: we would have to iterate each border separately. | |
1071 | */ | |
1072 | if (cam_rect->left > rect->left) | |
1073 | cam_rect->left = cap.bounds.left; | |
961801bb | 1074 | |
6a6c8786 GL |
1075 | if (cam_rect->left + cam_rect->width < rect->left + rect->width) |
1076 | cam_rect->width = rect->left + rect->width - | |
08590b96 | 1077 | cam_rect->left; |
961801bb | 1078 | |
6a6c8786 GL |
1079 | if (cam_rect->top > rect->top) |
1080 | cam_rect->top = cap.bounds.top; | |
961801bb | 1081 | |
6a6c8786 GL |
1082 | if (cam_rect->top + cam_rect->height < rect->top + rect->height) |
1083 | cam_rect->height = rect->top + rect->height - | |
08590b96 | 1084 | cam_rect->top; |
961801bb | 1085 | |
6a6c8786 GL |
1086 | v4l2_subdev_call(sd, video, s_crop, cam_crop); |
1087 | ret = client_g_rect(sd, cam_rect); | |
1088 | dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret, | |
1089 | cam_rect->width, cam_rect->height, | |
08590b96 | 1090 | cam_rect->left, cam_rect->top); |
904078f1 GL |
1091 | } |
1092 | ||
6a6c8786 GL |
1093 | /* S_CROP must not modify the rectangle */ |
1094 | if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) { | |
904078f1 GL |
1095 | /* |
1096 | * The camera failed to configure a suitable cropping, | |
1097 | * we cannot use the current rectangle, set to max | |
1098 | */ | |
6a6c8786 GL |
1099 | *cam_rect = cap.bounds; |
1100 | v4l2_subdev_call(sd, video, s_crop, cam_crop); | |
1101 | ret = client_g_rect(sd, cam_rect); | |
1102 | dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret, | |
1103 | cam_rect->width, cam_rect->height, | |
08590b96 | 1104 | cam_rect->left, cam_rect->top); |
904078f1 GL |
1105 | } |
1106 | ||
6a6c8786 GL |
1107 | return ret; |
1108 | } | |
1109 | ||
1110 | static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect, | |
1111 | unsigned int *scale_h, unsigned int *scale_v) | |
1112 | { | |
760697be | 1113 | struct v4l2_mbus_framefmt mf; |
6a6c8786 GL |
1114 | int ret; |
1115 | ||
760697be | 1116 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
6a6c8786 GL |
1117 | if (ret < 0) |
1118 | return ret; | |
1119 | ||
760697be GL |
1120 | *scale_h = calc_generic_scale(rect->width, mf.width); |
1121 | *scale_v = calc_generic_scale(rect->height, mf.height); | |
6a6c8786 GL |
1122 | |
1123 | return 0; | |
1124 | } | |
1125 | ||
1126 | static int get_camera_subwin(struct soc_camera_device *icd, | |
1127 | struct v4l2_rect *cam_subrect, | |
1128 | unsigned int cam_hscale, unsigned int cam_vscale) | |
1129 | { | |
1130 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
1131 | struct v4l2_rect *ceu_rect = &cam->ceu_rect; | |
1132 | ||
1133 | if (!ceu_rect->width) { | |
1134 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1135 | struct device *dev = icd->dev.parent; | |
760697be | 1136 | struct v4l2_mbus_framefmt mf; |
6a6c8786 GL |
1137 | int ret; |
1138 | /* First time */ | |
1139 | ||
760697be | 1140 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
6a6c8786 GL |
1141 | if (ret < 0) |
1142 | return ret; | |
1143 | ||
760697be | 1144 | dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height); |
6a6c8786 | 1145 | |
760697be | 1146 | if (mf.width > 2560) { |
6a6c8786 | 1147 | ceu_rect->width = 2560; |
760697be | 1148 | ceu_rect->left = (mf.width - 2560) / 2; |
6a6c8786 | 1149 | } else { |
760697be | 1150 | ceu_rect->width = mf.width; |
6a6c8786 GL |
1151 | ceu_rect->left = 0; |
1152 | } | |
1153 | ||
760697be | 1154 | if (mf.height > 1920) { |
6a6c8786 | 1155 | ceu_rect->height = 1920; |
760697be | 1156 | ceu_rect->top = (mf.height - 1920) / 2; |
6a6c8786 | 1157 | } else { |
760697be | 1158 | ceu_rect->height = mf.height; |
6a6c8786 GL |
1159 | ceu_rect->top = 0; |
1160 | } | |
1161 | ||
1162 | dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n", | |
1163 | ceu_rect->width, ceu_rect->height, | |
1164 | ceu_rect->left, ceu_rect->top); | |
1165 | } | |
1166 | ||
1167 | cam_subrect->width = scale_up(ceu_rect->width, cam_hscale); | |
1168 | cam_subrect->left = scale_up(ceu_rect->left, cam_hscale); | |
1169 | cam_subrect->height = scale_up(ceu_rect->height, cam_vscale); | |
1170 | cam_subrect->top = scale_up(ceu_rect->top, cam_vscale); | |
1171 | ||
1172 | return 0; | |
1173 | } | |
1174 | ||
760697be GL |
1175 | static int client_s_fmt(struct soc_camera_device *icd, |
1176 | struct v4l2_mbus_framefmt *mf, bool ceu_can_scale) | |
6a6c8786 GL |
1177 | { |
1178 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1179 | struct device *dev = icd->dev.parent; | |
760697be | 1180 | unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h; |
6a6c8786 GL |
1181 | unsigned int max_width, max_height; |
1182 | struct v4l2_cropcap cap; | |
1183 | int ret; | |
1184 | ||
1185 | cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1186 | ||
1187 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); | |
1188 | if (ret < 0) | |
1189 | return ret; | |
1190 | ||
1191 | max_width = min(cap.bounds.width, 2560); | |
1192 | max_height = min(cap.bounds.height, 1920); | |
1193 | ||
760697be | 1194 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf); |
6a6c8786 GL |
1195 | if (ret < 0) |
1196 | return ret; | |
1197 | ||
760697be | 1198 | dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height); |
6a6c8786 | 1199 | |
760697be | 1200 | if ((width == mf->width && height == mf->height) || !ceu_can_scale) |
6a6c8786 GL |
1201 | return 0; |
1202 | ||
1203 | /* Camera set a format, but geometry is not precise, try to improve */ | |
760697be GL |
1204 | tmp_w = mf->width; |
1205 | tmp_h = mf->height; | |
6a6c8786 GL |
1206 | |
1207 | /* width <= max_width && height <= max_height - guaranteed by try_fmt */ | |
1208 | while ((width > tmp_w || height > tmp_h) && | |
1209 | tmp_w < max_width && tmp_h < max_height) { | |
1210 | tmp_w = min(2 * tmp_w, max_width); | |
1211 | tmp_h = min(2 * tmp_h, max_height); | |
760697be GL |
1212 | mf->width = tmp_w; |
1213 | mf->height = tmp_h; | |
1214 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf); | |
6a6c8786 | 1215 | dev_geo(dev, "Camera scaled to %ux%u\n", |
760697be | 1216 | mf->width, mf->height); |
6a6c8786 GL |
1217 | if (ret < 0) { |
1218 | /* This shouldn't happen */ | |
1219 | dev_err(dev, "Client failed to set format: %d\n", ret); | |
1220 | return ret; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | return 0; | |
1225 | } | |
1226 | ||
1227 | /** | |
1228 | * @rect - camera cropped rectangle | |
1229 | * @sub_rect - CEU cropped rectangle, mapped back to camera input area | |
1230 | * @ceu_rect - on output calculated CEU crop rectangle | |
1231 | */ | |
1232 | static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect, | |
1233 | struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect, | |
760697be | 1234 | struct v4l2_mbus_framefmt *mf, bool ceu_can_scale) |
6a6c8786 GL |
1235 | { |
1236 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1237 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
1238 | struct device *dev = icd->dev.parent; | |
760697be | 1239 | struct v4l2_mbus_framefmt mf_tmp = *mf; |
6a6c8786 GL |
1240 | unsigned int scale_h, scale_v; |
1241 | int ret; | |
1242 | ||
1243 | /* 5. Apply iterative camera S_FMT for camera user window. */ | |
760697be | 1244 | ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale); |
6a6c8786 GL |
1245 | if (ret < 0) |
1246 | return ret; | |
1247 | ||
1248 | dev_geo(dev, "5: camera scaled to %ux%u\n", | |
760697be | 1249 | mf_tmp.width, mf_tmp.height); |
6a6c8786 GL |
1250 | |
1251 | /* 6. Retrieve camera output window (g_fmt) */ | |
1252 | ||
760697be | 1253 | /* unneeded - it is already in "mf_tmp" */ |
6a6c8786 GL |
1254 | |
1255 | /* 7. Calculate new camera scales. */ | |
1256 | ret = get_camera_scales(sd, rect, &scale_h, &scale_v); | |
1257 | if (ret < 0) | |
1258 | return ret; | |
1259 | ||
1260 | dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v); | |
1261 | ||
760697be GL |
1262 | cam->cam_width = mf_tmp.width; |
1263 | cam->cam_height = mf_tmp.height; | |
1264 | mf->width = mf_tmp.width; | |
1265 | mf->height = mf_tmp.height; | |
1266 | mf->colorspace = mf_tmp.colorspace; | |
904078f1 GL |
1267 | |
1268 | /* | |
6a6c8786 GL |
1269 | * 8. Calculate new CEU crop - apply camera scales to previously |
1270 | * calculated "effective" crop. | |
904078f1 | 1271 | */ |
6a6c8786 GL |
1272 | ceu_rect->left = scale_down(sub_rect->left, scale_h); |
1273 | ceu_rect->width = scale_down(sub_rect->width, scale_h); | |
1274 | ceu_rect->top = scale_down(sub_rect->top, scale_v); | |
1275 | ceu_rect->height = scale_down(sub_rect->height, scale_v); | |
1276 | ||
1277 | dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n", | |
1278 | ceu_rect->width, ceu_rect->height, | |
1279 | ceu_rect->left, ceu_rect->top); | |
1280 | ||
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | /* Get combined scales */ | |
1285 | static int get_scales(struct soc_camera_device *icd, | |
1286 | unsigned int *scale_h, unsigned int *scale_v) | |
1287 | { | |
1288 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
1289 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1290 | struct v4l2_crop cam_crop; | |
1291 | unsigned int width_in, height_in; | |
1292 | int ret; | |
904078f1 | 1293 | |
6a6c8786 | 1294 | cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
904078f1 | 1295 | |
6a6c8786 GL |
1296 | ret = client_g_rect(sd, &cam_crop.c); |
1297 | if (ret < 0) | |
1298 | return ret; | |
904078f1 | 1299 | |
6a6c8786 GL |
1300 | ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v); |
1301 | if (ret < 0) | |
1302 | return ret; | |
961801bb | 1303 | |
6a6c8786 GL |
1304 | width_in = scale_up(cam->ceu_rect.width, *scale_h); |
1305 | height_in = scale_up(cam->ceu_rect.height, *scale_v); | |
961801bb | 1306 | |
8cf1fec8 GL |
1307 | *scale_h = calc_generic_scale(width_in, icd->user_width); |
1308 | *scale_v = calc_generic_scale(height_in, icd->user_height); | |
6a6c8786 GL |
1309 | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * CEU can scale and crop, but we don't want to waste bandwidth and kill the | |
1315 | * framerate by always requesting the maximum image from the client. See | |
1316 | * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of | |
1317 | * scaling and cropping algorithms and for the meaning of referenced here steps. | |
1318 | */ | |
1319 | static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, | |
1320 | struct v4l2_crop *a) | |
1321 | { | |
1322 | struct v4l2_rect *rect = &a->c; | |
1323 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1324 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
1325 | struct v4l2_crop cam_crop; | |
1326 | struct sh_mobile_ceu_cam *cam = icd->host_priv; | |
1327 | struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect; | |
1328 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1329 | struct device *dev = icd->dev.parent; | |
760697be | 1330 | struct v4l2_mbus_framefmt mf; |
6a6c8786 GL |
1331 | unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v, |
1332 | out_width, out_height; | |
1333 | u32 capsr, cflcr; | |
1334 | int ret; | |
1335 | ||
1336 | /* 1. Calculate current combined scales. */ | |
1337 | ret = get_scales(icd, &scale_comb_h, &scale_comb_v); | |
1338 | if (ret < 0) | |
1339 | return ret; | |
1340 | ||
1341 | dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v); | |
1342 | ||
1343 | /* 2. Apply iterative camera S_CROP for new input window. */ | |
1344 | ret = client_s_crop(sd, a, &cam_crop); | |
1345 | if (ret < 0) | |
1346 | return ret; | |
1347 | ||
1348 | dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n", | |
1349 | cam_rect->width, cam_rect->height, | |
1350 | cam_rect->left, cam_rect->top); | |
1351 | ||
1352 | /* On success cam_crop contains current camera crop */ | |
1353 | ||
1354 | /* | |
1355 | * 3. If old combined scales applied to new crop produce an impossible | |
1356 | * user window, adjust scales to produce nearest possible window. | |
1357 | */ | |
1358 | out_width = scale_down(rect->width, scale_comb_h); | |
1359 | out_height = scale_down(rect->height, scale_comb_v); | |
1360 | ||
1361 | if (out_width > 2560) | |
1362 | out_width = 2560; | |
1363 | else if (out_width < 2) | |
1364 | out_width = 2; | |
1365 | ||
1366 | if (out_height > 1920) | |
1367 | out_height = 1920; | |
1368 | else if (out_height < 4) | |
1369 | out_height = 4; | |
1370 | ||
1371 | dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height); | |
1372 | ||
1373 | /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */ | |
1374 | ||
1375 | /* | |
1376 | * 5. Using actual input window and calculated combined scales calculate | |
1377 | * camera target output window. | |
1378 | */ | |
760697be GL |
1379 | mf.width = scale_down(cam_rect->width, scale_comb_h); |
1380 | mf.height = scale_down(cam_rect->height, scale_comb_v); | |
6a6c8786 | 1381 | |
760697be | 1382 | dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height); |
6a6c8786 GL |
1383 | |
1384 | /* 6. - 9. */ | |
760697be | 1385 | mf.code = cam->code; |
1edcc10d | 1386 | mf.field = pcdev->field; |
6a6c8786 GL |
1387 | |
1388 | capsr = capture_save_reset(pcdev); | |
1389 | dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr); | |
1390 | ||
1391 | /* Make relative to camera rectangle */ | |
760697be GL |
1392 | rect->left -= cam_rect->left; |
1393 | rect->top -= cam_rect->top; | |
6a6c8786 | 1394 | |
760697be | 1395 | ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf, |
1edcc10d KM |
1396 | pcdev->image_mode && |
1397 | V4L2_FIELD_NONE == pcdev->field); | |
6a6c8786 GL |
1398 | |
1399 | dev_geo(dev, "6-9: %d\n", ret); | |
1400 | ||
1401 | /* 10. Use CEU cropping to crop to the new window. */ | |
1402 | sh_mobile_ceu_set_rect(icd, out_width, out_height); | |
1403 | ||
1404 | dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n", | |
1405 | ceu_rect->width, ceu_rect->height, | |
1406 | ceu_rect->left, ceu_rect->top); | |
1407 | ||
1408 | /* | |
1409 | * 11. Calculate CEU scales from camera scales from results of (10) and | |
1410 | * user window from (3) | |
1411 | */ | |
1412 | scale_ceu_h = calc_scale(ceu_rect->width, &out_width); | |
1413 | scale_ceu_v = calc_scale(ceu_rect->height, &out_height); | |
1414 | ||
1415 | dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v); | |
1416 | ||
1417 | /* 12. Apply CEU scales. */ | |
1418 | cflcr = scale_ceu_h | (scale_ceu_v << 16); | |
1419 | if (cflcr != pcdev->cflcr) { | |
1420 | pcdev->cflcr = cflcr; | |
1421 | ceu_write(pcdev, CFLCR, cflcr); | |
1422 | } | |
1423 | ||
1424 | /* Restore capture */ | |
904078f1 GL |
1425 | if (pcdev->active) |
1426 | capsr |= 1; | |
904078f1 GL |
1427 | capture_restore(pcdev, capsr); |
1428 | ||
6a6c8786 GL |
1429 | icd->user_width = out_width; |
1430 | icd->user_height = out_height; | |
1431 | ||
904078f1 GL |
1432 | /* Even if only camera cropping succeeded */ |
1433 | return ret; | |
09e231b3 GL |
1434 | } |
1435 | ||
961801bb | 1436 | /* Similar to set_crop multistage iterative algorithm */ |
d8fac217 | 1437 | static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, |
09e231b3 | 1438 | struct v4l2_format *f) |
0d3244d6 | 1439 | { |
9414de39 | 1440 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
961801bb | 1441 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
904078f1 GL |
1442 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
1443 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
760697be | 1444 | struct v4l2_mbus_framefmt mf; |
c9c1f1c0 | 1445 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
6a6c8786 | 1446 | struct device *dev = icd->dev.parent; |
904078f1 | 1447 | __u32 pixfmt = pix->pixelformat; |
9414de39 | 1448 | const struct soc_camera_format_xlate *xlate; |
6a6c8786 GL |
1449 | struct v4l2_crop cam_crop; |
1450 | struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect; | |
1451 | unsigned int scale_cam_h, scale_cam_v; | |
1452 | u16 scale_v, scale_h; | |
1453 | int ret; | |
1edcc10d KM |
1454 | bool image_mode; |
1455 | enum v4l2_field field; | |
961801bb GL |
1456 | |
1457 | switch (pix->field) { | |
961801bb GL |
1458 | default: |
1459 | pix->field = V4L2_FIELD_NONE; | |
1460 | /* fall-through */ | |
1edcc10d KM |
1461 | case V4L2_FIELD_INTERLACED_TB: |
1462 | case V4L2_FIELD_INTERLACED_BT: | |
961801bb | 1463 | case V4L2_FIELD_NONE: |
1edcc10d KM |
1464 | field = pix->field; |
1465 | break; | |
1466 | case V4L2_FIELD_INTERLACED: | |
1467 | field = V4L2_FIELD_INTERLACED_TB; | |
961801bb GL |
1468 | break; |
1469 | } | |
25c4d74e | 1470 | |
9414de39 MD |
1471 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
1472 | if (!xlate) { | |
6a6c8786 | 1473 | dev_warn(dev, "Format %x not found\n", pixfmt); |
9414de39 | 1474 | return -EINVAL; |
25c4d74e GL |
1475 | } |
1476 | ||
6a6c8786 GL |
1477 | /* 1. Calculate current camera scales. */ |
1478 | cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1479 | ||
1480 | ret = client_g_rect(sd, cam_rect); | |
1481 | if (ret < 0) | |
1482 | return ret; | |
1483 | ||
1484 | ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v); | |
1485 | if (ret < 0) | |
1486 | return ret; | |
1487 | ||
1488 | dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v); | |
1489 | ||
1490 | /* | |
1491 | * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop | |
1492 | * scaled back at current camera scales onto input window. | |
1493 | */ | |
1494 | ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v); | |
961801bb GL |
1495 | if (ret < 0) |
1496 | return ret; | |
1497 | ||
6a6c8786 GL |
1498 | dev_geo(dev, "2: subwin %ux%u@%u:%u\n", |
1499 | cam_subrect.width, cam_subrect.height, | |
1500 | cam_subrect.left, cam_subrect.top); | |
1501 | ||
1502 | /* | |
1503 | * 3. Calculate new combined scales from "effective" input window to | |
1504 | * requested user window. | |
1505 | */ | |
1506 | scale_h = calc_generic_scale(cam_subrect.width, pix->width); | |
1507 | scale_v = calc_generic_scale(cam_subrect.height, pix->height); | |
1508 | ||
1509 | dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v); | |
1510 | ||
1511 | /* | |
1512 | * 4. Calculate camera output window by applying combined scales to real | |
1513 | * input window. | |
1514 | */ | |
760697be GL |
1515 | mf.width = scale_down(cam_rect->width, scale_h); |
1516 | mf.height = scale_down(cam_rect->height, scale_v); | |
1517 | mf.field = pix->field; | |
1518 | mf.colorspace = pix->colorspace; | |
1519 | mf.code = xlate->code; | |
6a6c8786 | 1520 | |
961801bb GL |
1521 | switch (pixfmt) { |
1522 | case V4L2_PIX_FMT_NV12: | |
1523 | case V4L2_PIX_FMT_NV21: | |
1524 | case V4L2_PIX_FMT_NV16: | |
1525 | case V4L2_PIX_FMT_NV61: | |
6a6c8786 | 1526 | image_mode = true; |
961801bb GL |
1527 | break; |
1528 | default: | |
6a6c8786 | 1529 | image_mode = false; |
9414de39 | 1530 | } |
25c4d74e | 1531 | |
760697be | 1532 | dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height); |
961801bb | 1533 | |
6a6c8786 | 1534 | /* 5. - 9. */ |
760697be | 1535 | ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf, |
1edcc10d | 1536 | image_mode && V4L2_FIELD_NONE == field); |
6a6c8786 GL |
1537 | |
1538 | dev_geo(dev, "5-9: client scale %d\n", ret); | |
1539 | ||
1540 | /* Done with the camera. Now see if we can improve the result */ | |
1541 | ||
1542 | dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n", | |
760697be | 1543 | ret, mf.width, mf.height, pix->width, pix->height); |
6a6c8786 GL |
1544 | if (ret < 0) |
1545 | return ret; | |
1546 | ||
760697be GL |
1547 | if (mf.code != xlate->code) |
1548 | return -EINVAL; | |
1549 | ||
6a6c8786 | 1550 | /* 10. Use CEU scaling to scale to the requested user window. */ |
961801bb GL |
1551 | |
1552 | /* We cannot scale up */ | |
760697be GL |
1553 | if (pix->width > mf.width) |
1554 | pix->width = mf.width; | |
6a6c8786 GL |
1555 | if (pix->width > ceu_rect.width) |
1556 | pix->width = ceu_rect.width; | |
961801bb | 1557 | |
760697be GL |
1558 | if (pix->height > mf.height) |
1559 | pix->height = mf.height; | |
6a6c8786 GL |
1560 | if (pix->height > ceu_rect.height) |
1561 | pix->height = ceu_rect.height; | |
961801bb | 1562 | |
760697be GL |
1563 | pix->colorspace = mf.colorspace; |
1564 | ||
1565 | if (image_mode) { | |
1566 | /* Scale pix->{width x height} down to width x height */ | |
1567 | scale_h = calc_scale(ceu_rect.width, &pix->width); | |
1568 | scale_v = calc_scale(ceu_rect.height, &pix->height); | |
1569 | ||
1570 | pcdev->cflcr = scale_h | (scale_v << 16); | |
1571 | } else { | |
1572 | pix->width = ceu_rect.width; | |
1573 | pix->height = ceu_rect.height; | |
1574 | scale_h = scale_v = 0; | |
1575 | pcdev->cflcr = 0; | |
1576 | } | |
961801bb | 1577 | |
6a6c8786 GL |
1578 | dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n", |
1579 | ceu_rect.width, scale_h, pix->width, | |
1580 | ceu_rect.height, scale_v, pix->height); | |
961801bb | 1581 | |
760697be GL |
1582 | cam->code = xlate->code; |
1583 | cam->ceu_rect = ceu_rect; | |
1584 | icd->current_fmt = xlate; | |
961801bb | 1585 | |
1edcc10d | 1586 | pcdev->field = field; |
6a6c8786 | 1587 | pcdev->image_mode = image_mode; |
961801bb GL |
1588 | |
1589 | return 0; | |
0d3244d6 MD |
1590 | } |
1591 | ||
d8fac217 GL |
1592 | static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, |
1593 | struct v4l2_format *f) | |
0d3244d6 | 1594 | { |
9414de39 | 1595 | const struct soc_camera_format_xlate *xlate; |
904078f1 | 1596 | struct v4l2_pix_format *pix = &f->fmt.pix; |
c9c1f1c0 | 1597 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
760697be | 1598 | struct v4l2_mbus_framefmt mf; |
904078f1 | 1599 | __u32 pixfmt = pix->pixelformat; |
961801bb | 1600 | int width, height; |
ccab8a29 | 1601 | int ret; |
a2c8c68c | 1602 | |
9414de39 MD |
1603 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
1604 | if (!xlate) { | |
0166b743 | 1605 | dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt); |
25c4d74e | 1606 | return -EINVAL; |
9414de39 | 1607 | } |
25c4d74e | 1608 | |
0d3244d6 MD |
1609 | /* FIXME: calculate using depth and bus width */ |
1610 | ||
904078f1 GL |
1611 | v4l_bound_align_image(&pix->width, 2, 2560, 1, |
1612 | &pix->height, 4, 1920, 2, 0); | |
0d3244d6 | 1613 | |
961801bb GL |
1614 | width = pix->width; |
1615 | height = pix->height; | |
1616 | ||
760697be GL |
1617 | pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt); |
1618 | if (pix->bytesperline < 0) | |
1619 | return pix->bytesperline; | |
1620 | pix->sizeimage = height * pix->bytesperline; | |
979ea1dd | 1621 | |
0d3244d6 | 1622 | /* limit to sensor capabilities */ |
760697be GL |
1623 | mf.width = pix->width; |
1624 | mf.height = pix->height; | |
1625 | mf.field = pix->field; | |
1626 | mf.code = xlate->code; | |
1627 | mf.colorspace = pix->colorspace; | |
1628 | ||
1629 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); | |
ccab8a29 KM |
1630 | if (ret < 0) |
1631 | return ret; | |
1632 | ||
760697be GL |
1633 | pix->width = mf.width; |
1634 | pix->height = mf.height; | |
1635 | pix->field = mf.field; | |
1636 | pix->colorspace = mf.colorspace; | |
1637 | ||
961801bb GL |
1638 | switch (pixfmt) { |
1639 | case V4L2_PIX_FMT_NV12: | |
1640 | case V4L2_PIX_FMT_NV21: | |
1641 | case V4L2_PIX_FMT_NV16: | |
1642 | case V4L2_PIX_FMT_NV61: | |
1643 | /* FIXME: check against rect_max after converting soc-camera */ | |
1644 | /* We can scale precisely, need a bigger image from camera */ | |
1645 | if (pix->width < width || pix->height < height) { | |
760697be GL |
1646 | /* |
1647 | * We presume, the sensor behaves sanely, i.e., if | |
1648 | * requested a bigger rectangle, it will not return a | |
1649 | * smaller one. | |
1650 | */ | |
1651 | mf.width = 2560; | |
1652 | mf.height = 1920; | |
1653 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); | |
961801bb GL |
1654 | if (ret < 0) { |
1655 | /* Shouldn't actually happen... */ | |
0166b743 | 1656 | dev_err(icd->dev.parent, |
760697be GL |
1657 | "FIXME: client try_fmt() = %d\n", ret); |
1658 | return ret; | |
961801bb GL |
1659 | } |
1660 | } | |
760697be GL |
1661 | /* We will scale exactly */ |
1662 | if (mf.width > width) | |
961801bb | 1663 | pix->width = width; |
760697be | 1664 | if (mf.height > height) |
961801bb | 1665 | pix->height = height; |
ccab8a29 KM |
1666 | } |
1667 | ||
1668 | return ret; | |
0d3244d6 MD |
1669 | } |
1670 | ||
1671 | static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf, | |
1672 | struct v4l2_requestbuffers *p) | |
1673 | { | |
1674 | int i; | |
1675 | ||
5d28d525 GL |
1676 | /* |
1677 | * This is for locking debugging only. I removed spinlocks and now I | |
0d3244d6 MD |
1678 | * check whether .prepare is ever called on a linked buffer, or whether |
1679 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now | |
5d28d525 GL |
1680 | * it hadn't triggered |
1681 | */ | |
0d3244d6 MD |
1682 | for (i = 0; i < p->count; i++) { |
1683 | struct sh_mobile_ceu_buffer *buf; | |
1684 | ||
1685 | buf = container_of(icf->vb_vidq.bufs[i], | |
1686 | struct sh_mobile_ceu_buffer, vb); | |
1687 | INIT_LIST_HEAD(&buf->vb.queue); | |
1688 | } | |
1689 | ||
1690 | return 0; | |
1691 | } | |
1692 | ||
1693 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) | |
1694 | { | |
1695 | struct soc_camera_file *icf = file->private_data; | |
1696 | struct sh_mobile_ceu_buffer *buf; | |
1697 | ||
1698 | buf = list_entry(icf->vb_vidq.stream.next, | |
1699 | struct sh_mobile_ceu_buffer, vb.stream); | |
1700 | ||
1701 | poll_wait(file, &buf->vb.done, pt); | |
1702 | ||
1703 | if (buf->vb.state == VIDEOBUF_DONE || | |
1704 | buf->vb.state == VIDEOBUF_ERROR) | |
1705 | return POLLIN|POLLRDNORM; | |
1706 | ||
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, | |
1711 | struct v4l2_capability *cap) | |
1712 | { | |
1713 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); | |
1714 | cap->version = KERNEL_VERSION(0, 0, 5); | |
1715 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q, | |
1720 | struct soc_camera_device *icd) | |
1721 | { | |
1722 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1723 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
1724 | ||
1725 | videobuf_queue_dma_contig_init(q, | |
1726 | &sh_mobile_ceu_videobuf_ops, | |
0166b743 | 1727 | icd->dev.parent, &pcdev->lock, |
0d3244d6 | 1728 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
1edcc10d | 1729 | pcdev->field, |
0d3244d6 MD |
1730 | sizeof(struct sh_mobile_ceu_buffer), |
1731 | icd); | |
1732 | } | |
1733 | ||
4a6110bc GL |
1734 | static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd, |
1735 | struct v4l2_control *ctrl) | |
1736 | { | |
1737 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1738 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
1739 | u32 val; | |
1740 | ||
1741 | switch (ctrl->id) { | |
1742 | case V4L2_CID_SHARPNESS: | |
1743 | val = ceu_read(pcdev, CLFCR); | |
1744 | ctrl->value = val ^ 1; | |
1745 | return 0; | |
1746 | } | |
1747 | return -ENOIOCTLCMD; | |
1748 | } | |
1749 | ||
1750 | static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd, | |
1751 | struct v4l2_control *ctrl) | |
1752 | { | |
1753 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1754 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
1755 | ||
1756 | switch (ctrl->id) { | |
1757 | case V4L2_CID_SHARPNESS: | |
760697be | 1758 | switch (icd->current_fmt->host_fmt->fourcc) { |
4a6110bc GL |
1759 | case V4L2_PIX_FMT_NV12: |
1760 | case V4L2_PIX_FMT_NV21: | |
1761 | case V4L2_PIX_FMT_NV16: | |
1762 | case V4L2_PIX_FMT_NV61: | |
1763 | ceu_write(pcdev, CLFCR, !ctrl->value); | |
1764 | return 0; | |
1765 | } | |
1766 | return -EINVAL; | |
1767 | } | |
1768 | return -ENOIOCTLCMD; | |
1769 | } | |
1770 | ||
1771 | static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = { | |
1772 | { | |
1773 | .id = V4L2_CID_SHARPNESS, | |
1774 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1775 | .name = "Low-pass filter", | |
1776 | .minimum = 0, | |
1777 | .maximum = 1, | |
1778 | .step = 1, | |
1779 | .default_value = 0, | |
1780 | }, | |
1781 | }; | |
1782 | ||
0d3244d6 MD |
1783 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { |
1784 | .owner = THIS_MODULE, | |
1785 | .add = sh_mobile_ceu_add_device, | |
1786 | .remove = sh_mobile_ceu_remove_device, | |
9414de39 | 1787 | .get_formats = sh_mobile_ceu_get_formats, |
904078f1 | 1788 | .put_formats = sh_mobile_ceu_put_formats, |
09e231b3 | 1789 | .set_crop = sh_mobile_ceu_set_crop, |
d8fac217 GL |
1790 | .set_fmt = sh_mobile_ceu_set_fmt, |
1791 | .try_fmt = sh_mobile_ceu_try_fmt, | |
4a6110bc GL |
1792 | .set_ctrl = sh_mobile_ceu_set_ctrl, |
1793 | .get_ctrl = sh_mobile_ceu_get_ctrl, | |
0d3244d6 MD |
1794 | .reqbufs = sh_mobile_ceu_reqbufs, |
1795 | .poll = sh_mobile_ceu_poll, | |
1796 | .querycap = sh_mobile_ceu_querycap, | |
0d3244d6 MD |
1797 | .set_bus_param = sh_mobile_ceu_set_bus_param, |
1798 | .init_videobuf = sh_mobile_ceu_init_videobuf, | |
4a6110bc GL |
1799 | .controls = sh_mobile_ceu_controls, |
1800 | .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls), | |
0d3244d6 MD |
1801 | }; |
1802 | ||
979ea1dd | 1803 | static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev) |
0d3244d6 MD |
1804 | { |
1805 | struct sh_mobile_ceu_dev *pcdev; | |
1806 | struct resource *res; | |
1807 | void __iomem *base; | |
1808 | unsigned int irq; | |
1809 | int err = 0; | |
1810 | ||
1811 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1812 | irq = platform_get_irq(pdev, 0); | |
1813 | if (!res || !irq) { | |
1814 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); | |
1815 | err = -ENODEV; | |
1816 | goto exit; | |
1817 | } | |
1818 | ||
1819 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); | |
1820 | if (!pcdev) { | |
1821 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); | |
1822 | err = -ENOMEM; | |
1823 | goto exit; | |
1824 | } | |
1825 | ||
0d3244d6 MD |
1826 | INIT_LIST_HEAD(&pcdev->capture); |
1827 | spin_lock_init(&pcdev->lock); | |
1828 | ||
1829 | pcdev->pdata = pdev->dev.platform_data; | |
1830 | if (!pcdev->pdata) { | |
1831 | err = -EINVAL; | |
1832 | dev_err(&pdev->dev, "CEU platform data not set.\n"); | |
1833 | goto exit_kfree; | |
1834 | } | |
1835 | ||
eb6c8558 | 1836 | base = ioremap_nocache(res->start, resource_size(res)); |
0d3244d6 MD |
1837 | if (!base) { |
1838 | err = -ENXIO; | |
1839 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); | |
1840 | goto exit_kfree; | |
1841 | } | |
1842 | ||
1843 | pcdev->irq = irq; | |
1844 | pcdev->base = base; | |
1845 | pcdev->video_limit = 0; /* only enabled if second resource exists */ | |
0d3244d6 MD |
1846 | |
1847 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
1848 | if (res) { | |
1849 | err = dma_declare_coherent_memory(&pdev->dev, res->start, | |
1850 | res->start, | |
eb6c8558 | 1851 | resource_size(res), |
0d3244d6 MD |
1852 | DMA_MEMORY_MAP | |
1853 | DMA_MEMORY_EXCLUSIVE); | |
1854 | if (!err) { | |
1855 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); | |
1856 | err = -ENXIO; | |
1857 | goto exit_iounmap; | |
1858 | } | |
1859 | ||
eb6c8558 | 1860 | pcdev->video_limit = resource_size(res); |
0d3244d6 MD |
1861 | } |
1862 | ||
1863 | /* request irq */ | |
1864 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, | |
af128a10 | 1865 | dev_name(&pdev->dev), pcdev); |
0d3244d6 MD |
1866 | if (err) { |
1867 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); | |
1868 | goto exit_release_mem; | |
1869 | } | |
1870 | ||
6d1386c6 MD |
1871 | pm_suspend_ignore_children(&pdev->dev, true); |
1872 | pm_runtime_enable(&pdev->dev); | |
1873 | pm_runtime_resume(&pdev->dev); | |
a42b6dd6 | 1874 | |
0d3244d6 | 1875 | pcdev->ici.priv = pcdev; |
979ea1dd | 1876 | pcdev->ici.v4l2_dev.dev = &pdev->dev; |
0d3244d6 | 1877 | pcdev->ici.nr = pdev->id; |
af128a10 KS |
1878 | pcdev->ici.drv_name = dev_name(&pdev->dev); |
1879 | pcdev->ici.ops = &sh_mobile_ceu_host_ops; | |
0d3244d6 MD |
1880 | |
1881 | err = soc_camera_host_register(&pcdev->ici); | |
1882 | if (err) | |
055e05a0 | 1883 | goto exit_free_clk; |
0d3244d6 MD |
1884 | |
1885 | return 0; | |
1886 | ||
055e05a0 KM |
1887 | exit_free_clk: |
1888 | pm_runtime_disable(&pdev->dev); | |
0d3244d6 MD |
1889 | free_irq(pcdev->irq, pcdev); |
1890 | exit_release_mem: | |
1891 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
1892 | dma_release_declared_memory(&pdev->dev); | |
1893 | exit_iounmap: | |
1894 | iounmap(base); | |
1895 | exit_kfree: | |
1896 | kfree(pcdev); | |
1897 | exit: | |
1898 | return err; | |
1899 | } | |
1900 | ||
979ea1dd | 1901 | static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev) |
0d3244d6 | 1902 | { |
eff505fa GL |
1903 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
1904 | struct sh_mobile_ceu_dev *pcdev = container_of(soc_host, | |
1905 | struct sh_mobile_ceu_dev, ici); | |
0d3244d6 | 1906 | |
eff505fa | 1907 | soc_camera_host_unregister(soc_host); |
055e05a0 | 1908 | pm_runtime_disable(&pdev->dev); |
0d3244d6 MD |
1909 | free_irq(pcdev->irq, pcdev); |
1910 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
1911 | dma_release_declared_memory(&pdev->dev); | |
1912 | iounmap(pcdev->base); | |
1913 | kfree(pcdev); | |
1914 | return 0; | |
1915 | } | |
1916 | ||
6d1386c6 MD |
1917 | static int sh_mobile_ceu_runtime_nop(struct device *dev) |
1918 | { | |
1919 | /* Runtime PM callback shared between ->runtime_suspend() | |
1920 | * and ->runtime_resume(). Simply returns success. | |
1921 | * | |
1922 | * This driver re-initializes all registers after | |
1923 | * pm_runtime_get_sync() anyway so there is no need | |
1924 | * to save and restore registers here. | |
1925 | */ | |
1926 | return 0; | |
1927 | } | |
1928 | ||
47145210 | 1929 | static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = { |
6d1386c6 MD |
1930 | .runtime_suspend = sh_mobile_ceu_runtime_nop, |
1931 | .runtime_resume = sh_mobile_ceu_runtime_nop, | |
1932 | }; | |
1933 | ||
0d3244d6 MD |
1934 | static struct platform_driver sh_mobile_ceu_driver = { |
1935 | .driver = { | |
1936 | .name = "sh_mobile_ceu", | |
6d1386c6 | 1937 | .pm = &sh_mobile_ceu_dev_pm_ops, |
0d3244d6 MD |
1938 | }, |
1939 | .probe = sh_mobile_ceu_probe, | |
50e55fd1 | 1940 | .remove = __devexit_p(sh_mobile_ceu_remove), |
0d3244d6 MD |
1941 | }; |
1942 | ||
1943 | static int __init sh_mobile_ceu_init(void) | |
1944 | { | |
1945 | return platform_driver_register(&sh_mobile_ceu_driver); | |
1946 | } | |
1947 | ||
1948 | static void __exit sh_mobile_ceu_exit(void) | |
1949 | { | |
01c1e4ca | 1950 | platform_driver_unregister(&sh_mobile_ceu_driver); |
0d3244d6 MD |
1951 | } |
1952 | ||
1953 | module_init(sh_mobile_ceu_init); | |
1954 | module_exit(sh_mobile_ceu_exit); | |
1955 | ||
1956 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); | |
1957 | MODULE_AUTHOR("Magnus Damm"); | |
1958 | MODULE_LICENSE("GPL"); | |
40e2e092 | 1959 | MODULE_ALIAS("platform:sh_mobile_ceu"); |