2 * V4L2 Driver for SuperH Mobile CEU interface
4 * Copyright (C) 2008 Magnus Damm
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.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>
32 #include <linux/videodev2.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/sched.h>
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>
42 /* register offsets for sh7722 / sh7723 */
44 #define CAPSR 0x00 /* Capture start register */
45 #define CAPCR 0x04 /* Capture control register */
46 #define CAMCR 0x08 /* Capture interface control register */
47 #define CMCYR 0x0c /* Capture interface cycle register */
48 #define CAMOR 0x10 /* Capture interface offset register */
49 #define CAPWR 0x14 /* Capture interface width register */
50 #define CAIFR 0x18 /* Capture interface input format register */
51 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
52 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
53 #define CRCNTR 0x28 /* CEU register control register */
54 #define CRCMPR 0x2c /* CEU register forcible control register */
55 #define CFLCR 0x30 /* Capture filter control register */
56 #define CFSZR 0x34 /* Capture filter size clip register */
57 #define CDWDR 0x38 /* Capture destination width register */
58 #define CDAYR 0x3c /* Capture data address Y register */
59 #define CDACR 0x40 /* Capture data address C register */
60 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
61 #define CDBCR 0x48 /* Capture data bottom-field address C register */
62 #define CBDSR 0x4c /* Capture bundle destination size register */
63 #define CFWCR 0x5c /* Firewall operation control register */
64 #define CLFCR 0x60 /* Capture low-pass filter control register */
65 #define CDOCR 0x64 /* Capture data output control register */
66 #define CDDCR 0x68 /* Capture data complexity level register */
67 #define CDDAR 0x6c /* Capture data complexity level address register */
68 #define CEIER 0x70 /* Capture event interrupt enable register */
69 #define CETCR 0x74 /* Capture event flag clear register */
70 #define CSTSR 0x7c /* Capture status register */
71 #define CSRTR 0x80 /* Capture software reset register */
72 #define CDSSR 0x84 /* Capture data size register */
73 #define CDAYR2 0x90 /* Capture data address Y register 2 */
74 #define CDACR2 0x94 /* Capture data address C register 2 */
75 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
76 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
80 #define dev_geo dev_info
82 #define dev_geo dev_dbg
85 /* per video frame buffer */
86 struct sh_mobile_ceu_buffer
{
87 struct videobuf_buffer vb
; /* v4l buffer must be first */
88 const struct soc_camera_data_format
*fmt
;
91 struct sh_mobile_ceu_dev
{
92 struct soc_camera_host ici
;
93 struct soc_camera_device
*icd
;
97 unsigned long video_limit
;
99 /* lock used to protect videobuf */
101 struct list_head capture
;
102 struct videobuf_buffer
*active
;
104 struct sh_mobile_ceu_info
*pdata
;
108 unsigned int is_interlaced
:1;
109 unsigned int image_mode
:1;
110 unsigned int is_16bit
:1;
113 struct sh_mobile_ceu_cam
{
114 struct v4l2_rect ceu_rect
;
115 unsigned int cam_width
;
116 unsigned int cam_height
;
117 const struct soc_camera_data_format
*extra_fmt
;
118 const struct soc_camera_data_format
*camera_fmt
;
121 static unsigned long make_bus_param(struct sh_mobile_ceu_dev
*pcdev
)
125 flags
= SOCAM_MASTER
|
126 SOCAM_PCLK_SAMPLE_RISING
|
127 SOCAM_HSYNC_ACTIVE_HIGH
|
128 SOCAM_HSYNC_ACTIVE_LOW
|
129 SOCAM_VSYNC_ACTIVE_HIGH
|
130 SOCAM_VSYNC_ACTIVE_LOW
|
131 SOCAM_DATA_ACTIVE_HIGH
;
133 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_8BIT_BUS
)
134 flags
|= SOCAM_DATAWIDTH_8
;
136 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_16BIT_BUS
)
137 flags
|= SOCAM_DATAWIDTH_16
;
139 if (flags
& SOCAM_DATAWIDTH_MASK
)
145 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
146 unsigned long reg_offs
, u32 data
)
148 iowrite32(data
, priv
->base
+ reg_offs
);
151 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
153 return ioread32(priv
->base
+ reg_offs
);
157 * Videobuf operations
159 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue
*vq
,
163 struct soc_camera_device
*icd
= vq
->priv_data
;
164 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
165 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
166 int bytes_per_pixel
= (icd
->current_fmt
->depth
+ 7) >> 3;
168 *size
= PAGE_ALIGN(icd
->user_width
* icd
->user_height
*
174 if (pcdev
->video_limit
) {
175 while (*size
* *count
> pcdev
->video_limit
)
179 dev_dbg(icd
->dev
.parent
, "count=%d, size=%d\n", *count
, *size
);
184 static void free_buffer(struct videobuf_queue
*vq
,
185 struct sh_mobile_ceu_buffer
*buf
)
187 struct soc_camera_device
*icd
= vq
->priv_data
;
188 struct device
*dev
= icd
->dev
.parent
;
190 dev_dbg(dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
191 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
196 videobuf_waiton(&buf
->vb
, 0, 0);
197 videobuf_dma_contig_free(vq
, &buf
->vb
);
198 dev_dbg(dev
, "%s freed\n", __func__
);
199 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
202 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
203 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
204 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
205 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
208 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
210 struct soc_camera_device
*icd
= pcdev
->icd
;
211 dma_addr_t phys_addr_top
, phys_addr_bottom
;
213 /* The hardware is _very_ picky about this sequence. Especially
214 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
215 * several not-so-well documented interrupt sources in CETCR.
217 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_CPEIE
);
218 ceu_write(pcdev
, CETCR
, ~ceu_read(pcdev
, CETCR
) & CEU_CETCR_MAGIC
);
219 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_CPEIE
);
220 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
221 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
226 phys_addr_top
= videobuf_to_dma_contig(pcdev
->active
);
227 ceu_write(pcdev
, CDAYR
, phys_addr_top
);
228 if (pcdev
->is_interlaced
) {
229 phys_addr_bottom
= phys_addr_top
+ icd
->user_width
;
230 ceu_write(pcdev
, CDBYR
, phys_addr_bottom
);
233 switch (icd
->current_fmt
->fourcc
) {
234 case V4L2_PIX_FMT_NV12
:
235 case V4L2_PIX_FMT_NV21
:
236 case V4L2_PIX_FMT_NV16
:
237 case V4L2_PIX_FMT_NV61
:
238 phys_addr_top
+= icd
->user_width
*
240 ceu_write(pcdev
, CDACR
, phys_addr_top
);
241 if (pcdev
->is_interlaced
) {
242 phys_addr_bottom
= phys_addr_top
+
244 ceu_write(pcdev
, CDBCR
, phys_addr_bottom
);
248 pcdev
->active
->state
= VIDEOBUF_ACTIVE
;
249 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
252 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue
*vq
,
253 struct videobuf_buffer
*vb
,
254 enum v4l2_field field
)
256 struct soc_camera_device
*icd
= vq
->priv_data
;
257 struct sh_mobile_ceu_buffer
*buf
;
260 buf
= container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
262 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
263 vb
, vb
->baddr
, vb
->bsize
);
265 /* Added list head initialization on alloc */
266 WARN_ON(!list_empty(&vb
->queue
));
269 /* This can be useful if you want to see if we actually fill
270 * the buffer with something */
271 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
274 BUG_ON(NULL
== icd
->current_fmt
);
276 if (buf
->fmt
!= icd
->current_fmt
||
277 vb
->width
!= icd
->user_width
||
278 vb
->height
!= icd
->user_height
||
279 vb
->field
!= field
) {
280 buf
->fmt
= icd
->current_fmt
;
281 vb
->width
= icd
->user_width
;
282 vb
->height
= icd
->user_height
;
284 vb
->state
= VIDEOBUF_NEEDS_INIT
;
287 vb
->size
= vb
->width
* vb
->height
* ((buf
->fmt
->depth
+ 7) >> 3);
288 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
293 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
294 ret
= videobuf_iolock(vq
, vb
, NULL
);
297 vb
->state
= VIDEOBUF_PREPARED
;
302 free_buffer(vq
, buf
);
307 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
308 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue
*vq
,
309 struct videobuf_buffer
*vb
)
311 struct soc_camera_device
*icd
= vq
->priv_data
;
312 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
313 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
315 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
316 vb
, vb
->baddr
, vb
->bsize
);
318 vb
->state
= VIDEOBUF_QUEUED
;
319 list_add_tail(&vb
->queue
, &pcdev
->capture
);
321 if (!pcdev
->active
) {
323 sh_mobile_ceu_capture(pcdev
);
327 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue
*vq
,
328 struct videobuf_buffer
*vb
)
330 struct soc_camera_device
*icd
= vq
->priv_data
;
331 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
332 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
335 spin_lock_irqsave(&pcdev
->lock
, flags
);
337 if (pcdev
->active
== vb
) {
338 /* disable capture (release DMA buffer), reset */
339 ceu_write(pcdev
, CAPSR
, 1 << 16);
340 pcdev
->active
= NULL
;
343 if ((vb
->state
== VIDEOBUF_ACTIVE
|| vb
->state
== VIDEOBUF_QUEUED
) &&
344 !list_empty(&vb
->queue
)) {
345 vb
->state
= VIDEOBUF_ERROR
;
346 list_del_init(&vb
->queue
);
349 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
351 free_buffer(vq
, container_of(vb
, struct sh_mobile_ceu_buffer
, vb
));
354 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops
= {
355 .buf_setup
= sh_mobile_ceu_videobuf_setup
,
356 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
357 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
358 .buf_release
= sh_mobile_ceu_videobuf_release
,
361 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
363 struct sh_mobile_ceu_dev
*pcdev
= data
;
364 struct videobuf_buffer
*vb
;
367 spin_lock_irqsave(&pcdev
->lock
, flags
);
371 /* Stale interrupt from a released buffer */
374 list_del_init(&vb
->queue
);
376 if (!list_empty(&pcdev
->capture
))
377 pcdev
->active
= list_entry(pcdev
->capture
.next
,
378 struct videobuf_buffer
, queue
);
380 pcdev
->active
= NULL
;
382 sh_mobile_ceu_capture(pcdev
);
384 vb
->state
= VIDEOBUF_DONE
;
385 do_gettimeofday(&vb
->ts
);
390 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
395 /* Called with .video_lock held */
396 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
398 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
399 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
404 dev_info(icd
->dev
.parent
,
405 "SuperH Mobile CEU driver attached to camera %d\n",
408 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
410 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
411 while (ceu_read(pcdev
, CSTSR
) & 1)
419 /* Called with .video_lock held */
420 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
422 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
423 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
426 BUG_ON(icd
!= pcdev
->icd
);
428 /* disable capture, disable interrupts */
429 ceu_write(pcdev
, CEIER
, 0);
430 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
432 /* make sure active buffer is canceled */
433 spin_lock_irqsave(&pcdev
->lock
, flags
);
435 list_del(&pcdev
->active
->queue
);
436 pcdev
->active
->state
= VIDEOBUF_ERROR
;
437 wake_up_all(&pcdev
->active
->done
);
438 pcdev
->active
= NULL
;
440 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
442 pm_runtime_put_sync(ici
->v4l2_dev
.dev
);
444 dev_info(icd
->dev
.parent
,
445 "SuperH Mobile CEU driver detached from camera %d\n",
452 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
453 * in SH7722 Hardware Manual
455 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
457 unsigned int mant_pre
= scale
>> 12;
460 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
461 mant_pre
* 4096 / scale
+ 1;
464 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
471 scale
= (src
* 4096 / *dst
) & ~7;
473 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
476 *dst
= size_dst(src
, scale
);
481 /* rect is guaranteed to not exceed the scaled camera rectangle */
482 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
,
483 unsigned int out_width
,
484 unsigned int out_height
)
486 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
487 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
488 struct v4l2_rect
*rect
= &cam
->ceu_rect
;
489 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
490 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
491 unsigned int left_offset
, top_offset
;
494 dev_dbg(icd
->dev
.parent
, "Crop %ux%u@%u:%u\n",
495 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
497 left_offset
= rect
->left
;
498 top_offset
= rect
->top
;
500 if (pcdev
->image_mode
) {
501 in_width
= rect
->width
;
502 if (!pcdev
->is_16bit
) {
506 width
= cdwdr_width
= out_width
;
508 unsigned int w_factor
= (icd
->current_fmt
->depth
+ 7) >> 3;
510 width
= out_width
* w_factor
/ 2;
512 if (!pcdev
->is_16bit
)
515 in_width
= rect
->width
* w_factor
/ 2;
516 left_offset
= left_offset
* w_factor
/ 2;
518 cdwdr_width
= width
* 2;
522 in_height
= rect
->height
;
523 if (pcdev
->is_interlaced
) {
530 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
531 camor
= left_offset
| (top_offset
<< 16);
533 dev_geo(icd
->dev
.parent
,
534 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
535 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
538 ceu_write(pcdev
, CAMOR
, camor
);
539 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
540 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
541 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
544 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
546 u32 capsr
= ceu_read(pcdev
, CAPSR
);
547 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
551 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
553 unsigned long timeout
= jiffies
+ 10 * HZ
;
556 * Wait until the end of the current frame. It can take a long time,
557 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
559 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
562 if (time_after(jiffies
, timeout
)) {
563 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
564 "Timeout waiting for frame end! Interface problem?\n");
568 /* Wait until reset clears, this shall not hang... */
569 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
572 /* Anything to restore? */
573 if (capsr
& ~(1 << 16))
574 ceu_write(pcdev
, CAPSR
, capsr
);
577 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
,
580 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
581 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
583 unsigned long camera_flags
, common_flags
, value
;
585 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
586 u32 capsr
= capture_save_reset(pcdev
);
588 camera_flags
= icd
->ops
->query_bus_param(icd
);
589 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
590 make_bus_param(pcdev
));
594 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
598 switch (common_flags
& SOCAM_DATAWIDTH_MASK
) {
599 case SOCAM_DATAWIDTH_8
:
602 case SOCAM_DATAWIDTH_16
:
609 ceu_write(pcdev
, CRCNTR
, 0);
610 ceu_write(pcdev
, CRCMPR
, 0);
612 value
= 0x00000010; /* data fetch by default */
615 switch (icd
->current_fmt
->fourcc
) {
616 case V4L2_PIX_FMT_NV12
:
617 case V4L2_PIX_FMT_NV21
:
618 yuv_lineskip
= 1; /* skip for NV12/21, no skip for NV16/61 */
620 case V4L2_PIX_FMT_NV16
:
621 case V4L2_PIX_FMT_NV61
:
622 switch (cam
->camera_fmt
->fourcc
) {
623 case V4L2_PIX_FMT_UYVY
:
624 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
626 case V4L2_PIX_FMT_VYUY
:
627 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
629 case V4L2_PIX_FMT_YUYV
:
630 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
632 case V4L2_PIX_FMT_YVYU
:
633 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
640 if (icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
641 icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
642 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
644 value
|= common_flags
& SOCAM_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
645 value
|= common_flags
& SOCAM_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
646 value
|= pcdev
->is_16bit
? 1 << 12 : 0;
647 ceu_write(pcdev
, CAMCR
, value
);
649 ceu_write(pcdev
, CAPCR
, 0x00300000);
650 ceu_write(pcdev
, CAIFR
, pcdev
->is_interlaced
? 0x101 : 0);
652 sh_mobile_ceu_set_rect(icd
, icd
->user_width
, icd
->user_height
);
655 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
657 /* A few words about byte order (observed in Big Endian mode)
659 * In data fetch mode bytes are received in chunks of 8 bytes.
660 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
662 * The data is however by default written to memory in reverse order:
663 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
665 * The lowest three bits of CDOCR allows us to do swapping,
666 * using 7 we swap the data bytes to match the incoming order:
667 * D0, D1, D2, D3, D4, D5, D6, D7
671 value
&= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
673 ceu_write(pcdev
, CDOCR
, value
);
674 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
676 dev_dbg(icd
->dev
.parent
, "S_FMT successful for %c%c%c%c %ux%u\n",
677 pixfmt
& 0xff, (pixfmt
>> 8) & 0xff,
678 (pixfmt
>> 16) & 0xff, (pixfmt
>> 24) & 0xff,
679 icd
->user_width
, icd
->user_height
);
681 capture_restore(pcdev
, capsr
);
683 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
687 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
)
689 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
690 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
691 unsigned long camera_flags
, common_flags
;
693 camera_flags
= icd
->ops
->query_bus_param(icd
);
694 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
695 make_bus_param(pcdev
));
702 static const struct soc_camera_data_format sh_mobile_ceu_formats
[] = {
706 .fourcc
= V4L2_PIX_FMT_NV12
,
707 .colorspace
= V4L2_COLORSPACE_JPEG
,
712 .fourcc
= V4L2_PIX_FMT_NV21
,
713 .colorspace
= V4L2_COLORSPACE_JPEG
,
718 .fourcc
= V4L2_PIX_FMT_NV16
,
719 .colorspace
= V4L2_COLORSPACE_JPEG
,
724 .fourcc
= V4L2_PIX_FMT_NV61
,
725 .colorspace
= V4L2_COLORSPACE_JPEG
,
729 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, int idx
,
730 struct soc_camera_format_xlate
*xlate
)
732 struct device
*dev
= icd
->dev
.parent
;
735 struct sh_mobile_ceu_cam
*cam
;
737 ret
= sh_mobile_ceu_try_bus_param(icd
);
741 if (!icd
->host_priv
) {
742 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
746 icd
->host_priv
= cam
;
748 cam
= icd
->host_priv
;
751 /* Beginning of a pass */
753 cam
->extra_fmt
= NULL
;
755 switch (icd
->formats
[idx
].fourcc
) {
756 case V4L2_PIX_FMT_UYVY
:
757 case V4L2_PIX_FMT_VYUY
:
758 case V4L2_PIX_FMT_YUYV
:
759 case V4L2_PIX_FMT_YVYU
:
761 goto add_single_format
;
764 * Our case is simple so far: for any of the above four camera
765 * formats we add all our four synthesized NV* formats, so,
766 * just marking the device with a single flag suffices. If
767 * the format generation rules are more complex, you would have
768 * to actually hang your already added / counted formats onto
769 * the host_priv pointer and check whether the format you're
770 * going to add now is already there.
772 cam
->extra_fmt
= (void *)sh_mobile_ceu_formats
;
774 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
776 for (k
= 0; xlate
&& k
< n
; k
++) {
777 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
778 xlate
->cam_fmt
= icd
->formats
+ idx
;
779 xlate
->buswidth
= icd
->formats
[idx
].depth
;
781 dev_dbg(dev
, "Providing format %s using %s\n",
782 sh_mobile_ceu_formats
[k
].name
,
783 icd
->formats
[idx
].name
);
787 /* Generic pass-through */
790 xlate
->host_fmt
= icd
->formats
+ idx
;
791 xlate
->cam_fmt
= icd
->formats
+ idx
;
792 xlate
->buswidth
= icd
->formats
[idx
].depth
;
795 "Providing format %s in pass-through mode\n",
796 icd
->formats
[idx
].name
);
803 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
805 kfree(icd
->host_priv
);
806 icd
->host_priv
= NULL
;
809 /* Check if any dimension of r1 is smaller than respective one of r2 */
810 static bool is_smaller(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
812 return r1
->width
< r2
->width
|| r1
->height
< r2
->height
;
815 /* Check if r1 fails to cover r2 */
816 static bool is_inside(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
818 return r1
->left
> r2
->left
|| r1
->top
> r2
->top
||
819 r1
->left
+ r1
->width
< r2
->left
+ r2
->width
||
820 r1
->top
+ r1
->height
< r2
->top
+ r2
->height
;
823 static unsigned int scale_down(unsigned int size
, unsigned int scale
)
825 return (size
* 4096 + scale
/ 2) / scale
;
828 static unsigned int scale_up(unsigned int size
, unsigned int scale
)
830 return (size
* scale
+ 2048) / 4096;
833 static unsigned int calc_generic_scale(unsigned int input
, unsigned int output
)
835 return (input
* 4096 + output
/ 2) / output
;
838 static int client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
)
840 struct v4l2_crop crop
;
841 struct v4l2_cropcap cap
;
844 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
846 ret
= v4l2_subdev_call(sd
, video
, g_crop
, &crop
);
852 /* Camera driver doesn't support .g_crop(), assume default rectangle */
853 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
855 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
865 * The common for both scaling and cropping iterative approach is:
866 * 1. try if the client can produce exactly what requested by the user
867 * 2. if (1) failed, try to double the client image until we get one big enough
868 * 3. if (2) failed, try to request the maximum image
870 static int client_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*crop
,
871 struct v4l2_crop
*cam_crop
)
873 struct v4l2_rect
*rect
= &crop
->c
, *cam_rect
= &cam_crop
->c
;
874 struct device
*dev
= sd
->v4l2_dev
->dev
;
875 struct v4l2_cropcap cap
;
877 unsigned int width
, height
;
879 v4l2_subdev_call(sd
, video
, s_crop
, crop
);
880 ret
= client_g_rect(sd
, cam_rect
);
885 * Now cam_crop contains the current camera input rectangle, and it must
886 * be within camera cropcap bounds
888 if (!memcmp(rect
, cam_rect
, sizeof(*rect
))) {
889 /* Even if camera S_CROP failed, but camera rectangle matches */
890 dev_dbg(dev
, "Camera S_CROP successful for %ux%u@%u:%u\n",
891 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
895 /* Try to fix cropping, that camera hasn't managed to set */
896 dev_geo(dev
, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
897 cam_rect
->width
, cam_rect
->height
,
898 cam_rect
->left
, cam_rect
->top
,
899 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
901 /* We need sensor maximum rectangle */
902 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
906 soc_camera_limit_side(&rect
->left
, &rect
->width
, cap
.bounds
.left
, 2,
908 soc_camera_limit_side(&rect
->top
, &rect
->height
, cap
.bounds
.top
, 4,
912 * Popular special case - some cameras can only handle fixed sizes like
913 * QVGA, VGA,... Take care to avoid infinite loop.
915 width
= max(cam_rect
->width
, 2);
916 height
= max(cam_rect
->height
, 2);
918 while (!ret
&& (is_smaller(cam_rect
, rect
) ||
919 is_inside(cam_rect
, rect
)) &&
920 (cap
.bounds
.width
> width
|| cap
.bounds
.height
> height
)) {
925 cam_rect
->width
= width
;
926 cam_rect
->height
= height
;
929 * We do not know what capabilities the camera has to set up
930 * left and top borders. We could try to be smarter in iterating
931 * them, e.g., if camera current left is to the right of the
932 * target left, set it to the middle point between the current
933 * left and minimum left. But that would add too much
934 * complexity: we would have to iterate each border separately.
936 if (cam_rect
->left
> rect
->left
)
937 cam_rect
->left
= cap
.bounds
.left
;
939 if (cam_rect
->left
+ cam_rect
->width
< rect
->left
+ rect
->width
)
940 cam_rect
->width
= rect
->left
+ rect
->width
-
943 if (cam_rect
->top
> rect
->top
)
944 cam_rect
->top
= cap
.bounds
.top
;
946 if (cam_rect
->top
+ cam_rect
->height
< rect
->top
+ rect
->height
)
947 cam_rect
->height
= rect
->top
+ rect
->height
-
950 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
951 ret
= client_g_rect(sd
, cam_rect
);
952 dev_geo(dev
, "Camera S_CROP %d for %ux%u@%u:%u\n", ret
,
953 cam_rect
->width
, cam_rect
->height
,
954 cam_rect
->left
, cam_rect
->top
);
957 /* S_CROP must not modify the rectangle */
958 if (is_smaller(cam_rect
, rect
) || is_inside(cam_rect
, rect
)) {
960 * The camera failed to configure a suitable cropping,
961 * we cannot use the current rectangle, set to max
963 *cam_rect
= cap
.bounds
;
964 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
965 ret
= client_g_rect(sd
, cam_rect
);
966 dev_geo(dev
, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret
,
967 cam_rect
->width
, cam_rect
->height
,
968 cam_rect
->left
, cam_rect
->top
);
974 static int get_camera_scales(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
,
975 unsigned int *scale_h
, unsigned int *scale_v
)
977 struct v4l2_format f
;
980 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
982 ret
= v4l2_subdev_call(sd
, video
, g_fmt
, &f
);
986 *scale_h
= calc_generic_scale(rect
->width
, f
.fmt
.pix
.width
);
987 *scale_v
= calc_generic_scale(rect
->height
, f
.fmt
.pix
.height
);
992 static int get_camera_subwin(struct soc_camera_device
*icd
,
993 struct v4l2_rect
*cam_subrect
,
994 unsigned int cam_hscale
, unsigned int cam_vscale
)
996 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
997 struct v4l2_rect
*ceu_rect
= &cam
->ceu_rect
;
999 if (!ceu_rect
->width
) {
1000 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1001 struct device
*dev
= icd
->dev
.parent
;
1002 struct v4l2_format f
;
1003 struct v4l2_pix_format
*pix
= &f
.fmt
.pix
;
1007 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1009 ret
= v4l2_subdev_call(sd
, video
, g_fmt
, &f
);
1013 dev_geo(dev
, "camera fmt %ux%u\n", pix
->width
, pix
->height
);
1015 if (pix
->width
> 2560) {
1016 ceu_rect
->width
= 2560;
1017 ceu_rect
->left
= (pix
->width
- 2560) / 2;
1019 ceu_rect
->width
= pix
->width
;
1023 if (pix
->height
> 1920) {
1024 ceu_rect
->height
= 1920;
1025 ceu_rect
->top
= (pix
->height
- 1920) / 2;
1027 ceu_rect
->height
= pix
->height
;
1031 dev_geo(dev
, "initialised CEU rect %ux%u@%u:%u\n",
1032 ceu_rect
->width
, ceu_rect
->height
,
1033 ceu_rect
->left
, ceu_rect
->top
);
1036 cam_subrect
->width
= scale_up(ceu_rect
->width
, cam_hscale
);
1037 cam_subrect
->left
= scale_up(ceu_rect
->left
, cam_hscale
);
1038 cam_subrect
->height
= scale_up(ceu_rect
->height
, cam_vscale
);
1039 cam_subrect
->top
= scale_up(ceu_rect
->top
, cam_vscale
);
1044 static int client_s_fmt(struct soc_camera_device
*icd
, struct v4l2_format
*f
,
1047 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1048 struct device
*dev
= icd
->dev
.parent
;
1049 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1050 unsigned int width
= pix
->width
, height
= pix
->height
, tmp_w
, tmp_h
;
1051 unsigned int max_width
, max_height
;
1052 struct v4l2_cropcap cap
;
1055 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1057 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1061 max_width
= min(cap
.bounds
.width
, 2560);
1062 max_height
= min(cap
.bounds
.height
, 1920);
1064 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, f
);
1068 dev_geo(dev
, "camera scaled to %ux%u\n", pix
->width
, pix
->height
);
1070 if ((width
== pix
->width
&& height
== pix
->height
) || !ceu_can_scale
)
1073 /* Camera set a format, but geometry is not precise, try to improve */
1075 tmp_h
= pix
->height
;
1077 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1078 while ((width
> tmp_w
|| height
> tmp_h
) &&
1079 tmp_w
< max_width
&& tmp_h
< max_height
) {
1080 tmp_w
= min(2 * tmp_w
, max_width
);
1081 tmp_h
= min(2 * tmp_h
, max_height
);
1083 pix
->height
= tmp_h
;
1084 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, f
);
1085 dev_geo(dev
, "Camera scaled to %ux%u\n",
1086 pix
->width
, pix
->height
);
1088 /* This shouldn't happen */
1089 dev_err(dev
, "Client failed to set format: %d\n", ret
);
1098 * @rect - camera cropped rectangle
1099 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1100 * @ceu_rect - on output calculated CEU crop rectangle
1102 static int client_scale(struct soc_camera_device
*icd
, struct v4l2_rect
*rect
,
1103 struct v4l2_rect
*sub_rect
, struct v4l2_rect
*ceu_rect
,
1104 struct v4l2_format
*f
, bool ceu_can_scale
)
1106 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1107 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1108 struct device
*dev
= icd
->dev
.parent
;
1109 struct v4l2_format f_tmp
= *f
;
1110 struct v4l2_pix_format
*pix_tmp
= &f_tmp
.fmt
.pix
;
1111 unsigned int scale_h
, scale_v
;
1114 /* 5. Apply iterative camera S_FMT for camera user window. */
1115 ret
= client_s_fmt(icd
, &f_tmp
, ceu_can_scale
);
1119 dev_geo(dev
, "5: camera scaled to %ux%u\n",
1120 pix_tmp
->width
, pix_tmp
->height
);
1122 /* 6. Retrieve camera output window (g_fmt) */
1124 /* unneeded - it is already in "f_tmp" */
1126 /* 7. Calculate new camera scales. */
1127 ret
= get_camera_scales(sd
, rect
, &scale_h
, &scale_v
);
1131 dev_geo(dev
, "7: camera scales %u:%u\n", scale_h
, scale_v
);
1133 cam
->cam_width
= pix_tmp
->width
;
1134 cam
->cam_height
= pix_tmp
->height
;
1135 f
->fmt
.pix
.width
= pix_tmp
->width
;
1136 f
->fmt
.pix
.height
= pix_tmp
->height
;
1139 * 8. Calculate new CEU crop - apply camera scales to previously
1140 * calculated "effective" crop.
1142 ceu_rect
->left
= scale_down(sub_rect
->left
, scale_h
);
1143 ceu_rect
->width
= scale_down(sub_rect
->width
, scale_h
);
1144 ceu_rect
->top
= scale_down(sub_rect
->top
, scale_v
);
1145 ceu_rect
->height
= scale_down(sub_rect
->height
, scale_v
);
1147 dev_geo(dev
, "8: new CEU rect %ux%u@%u:%u\n",
1148 ceu_rect
->width
, ceu_rect
->height
,
1149 ceu_rect
->left
, ceu_rect
->top
);
1154 /* Get combined scales */
1155 static int get_scales(struct soc_camera_device
*icd
,
1156 unsigned int *scale_h
, unsigned int *scale_v
)
1158 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1159 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1160 struct v4l2_crop cam_crop
;
1161 unsigned int width_in
, height_in
;
1164 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1166 ret
= client_g_rect(sd
, &cam_crop
.c
);
1170 ret
= get_camera_scales(sd
, &cam_crop
.c
, scale_h
, scale_v
);
1174 width_in
= scale_up(cam
->ceu_rect
.width
, *scale_h
);
1175 height_in
= scale_up(cam
->ceu_rect
.height
, *scale_v
);
1177 *scale_h
= calc_generic_scale(cam
->ceu_rect
.width
, icd
->user_width
);
1178 *scale_v
= calc_generic_scale(cam
->ceu_rect
.height
, icd
->user_height
);
1184 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1185 * framerate by always requesting the maximum image from the client. See
1186 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1187 * scaling and cropping algorithms and for the meaning of referenced here steps.
1189 static int sh_mobile_ceu_set_crop(struct soc_camera_device
*icd
,
1190 struct v4l2_crop
*a
)
1192 struct v4l2_rect
*rect
= &a
->c
;
1193 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1194 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1195 struct v4l2_crop cam_crop
;
1196 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1197 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, *ceu_rect
= &cam
->ceu_rect
;
1198 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1199 struct device
*dev
= icd
->dev
.parent
;
1200 struct v4l2_format f
;
1201 struct v4l2_pix_format
*pix
= &f
.fmt
.pix
;
1202 unsigned int scale_comb_h
, scale_comb_v
, scale_ceu_h
, scale_ceu_v
,
1203 out_width
, out_height
;
1207 /* 1. Calculate current combined scales. */
1208 ret
= get_scales(icd
, &scale_comb_h
, &scale_comb_v
);
1212 dev_geo(dev
, "1: combined scales %u:%u\n", scale_comb_h
, scale_comb_v
);
1214 /* 2. Apply iterative camera S_CROP for new input window. */
1215 ret
= client_s_crop(sd
, a
, &cam_crop
);
1219 dev_geo(dev
, "2: camera cropped to %ux%u@%u:%u\n",
1220 cam_rect
->width
, cam_rect
->height
,
1221 cam_rect
->left
, cam_rect
->top
);
1223 /* On success cam_crop contains current camera crop */
1226 * 3. If old combined scales applied to new crop produce an impossible
1227 * user window, adjust scales to produce nearest possible window.
1229 out_width
= scale_down(rect
->width
, scale_comb_h
);
1230 out_height
= scale_down(rect
->height
, scale_comb_v
);
1232 if (out_width
> 2560)
1234 else if (out_width
< 2)
1237 if (out_height
> 1920)
1239 else if (out_height
< 4)
1242 dev_geo(dev
, "3: Adjusted output %ux%u\n", out_width
, out_height
);
1244 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1247 * 5. Using actual input window and calculated combined scales calculate
1248 * camera target output window.
1250 pix
->width
= scale_down(cam_rect
->width
, scale_comb_h
);
1251 pix
->height
= scale_down(cam_rect
->height
, scale_comb_v
);
1253 dev_geo(dev
, "5: camera target %ux%u\n", pix
->width
, pix
->height
);
1256 pix
->pixelformat
= cam
->camera_fmt
->fourcc
;
1257 pix
->colorspace
= cam
->camera_fmt
->colorspace
;
1259 capsr
= capture_save_reset(pcdev
);
1260 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1262 /* Make relative to camera rectangle */
1263 rect
->left
-= cam_rect
->left
;
1264 rect
->top
-= cam_rect
->top
;
1266 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1268 ret
= client_scale(icd
, cam_rect
, rect
, ceu_rect
, &f
,
1269 pcdev
->image_mode
&& !pcdev
->is_interlaced
);
1271 dev_geo(dev
, "6-9: %d\n", ret
);
1273 /* 10. Use CEU cropping to crop to the new window. */
1274 sh_mobile_ceu_set_rect(icd
, out_width
, out_height
);
1276 dev_geo(dev
, "10: CEU cropped to %ux%u@%u:%u\n",
1277 ceu_rect
->width
, ceu_rect
->height
,
1278 ceu_rect
->left
, ceu_rect
->top
);
1281 * 11. Calculate CEU scales from camera scales from results of (10) and
1282 * user window from (3)
1284 scale_ceu_h
= calc_scale(ceu_rect
->width
, &out_width
);
1285 scale_ceu_v
= calc_scale(ceu_rect
->height
, &out_height
);
1287 dev_geo(dev
, "11: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1289 /* 12. Apply CEU scales. */
1290 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1291 if (cflcr
!= pcdev
->cflcr
) {
1292 pcdev
->cflcr
= cflcr
;
1293 ceu_write(pcdev
, CFLCR
, cflcr
);
1296 /* Restore capture */
1299 capture_restore(pcdev
, capsr
);
1301 icd
->user_width
= out_width
;
1302 icd
->user_height
= out_height
;
1304 /* Even if only camera cropping succeeded */
1308 /* Similar to set_crop multistage iterative algorithm */
1309 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1310 struct v4l2_format
*f
)
1312 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1313 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1314 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1315 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1316 struct v4l2_format cam_f
= *f
;
1317 struct v4l2_pix_format
*cam_pix
= &cam_f
.fmt
.pix
;
1318 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1319 struct device
*dev
= icd
->dev
.parent
;
1320 __u32 pixfmt
= pix
->pixelformat
;
1321 const struct soc_camera_format_xlate
*xlate
;
1322 struct v4l2_crop cam_crop
;
1323 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, cam_subrect
, ceu_rect
;
1324 unsigned int scale_cam_h
, scale_cam_v
;
1325 u16 scale_v
, scale_h
;
1327 bool is_interlaced
, image_mode
;
1329 switch (pix
->field
) {
1330 case V4L2_FIELD_INTERLACED
:
1331 is_interlaced
= true;
1333 case V4L2_FIELD_ANY
:
1335 pix
->field
= V4L2_FIELD_NONE
;
1337 case V4L2_FIELD_NONE
:
1338 is_interlaced
= false;
1342 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1344 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1348 /* 1. Calculate current camera scales. */
1349 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1351 ret
= client_g_rect(sd
, cam_rect
);
1355 ret
= get_camera_scales(sd
, cam_rect
, &scale_cam_h
, &scale_cam_v
);
1359 dev_geo(dev
, "1: camera scales %u:%u\n", scale_cam_h
, scale_cam_v
);
1362 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1363 * scaled back at current camera scales onto input window.
1365 ret
= get_camera_subwin(icd
, &cam_subrect
, scale_cam_h
, scale_cam_v
);
1369 dev_geo(dev
, "2: subwin %ux%u@%u:%u\n",
1370 cam_subrect
.width
, cam_subrect
.height
,
1371 cam_subrect
.left
, cam_subrect
.top
);
1374 * 3. Calculate new combined scales from "effective" input window to
1375 * requested user window.
1377 scale_h
= calc_generic_scale(cam_subrect
.width
, pix
->width
);
1378 scale_v
= calc_generic_scale(cam_subrect
.height
, pix
->height
);
1380 dev_geo(dev
, "3: scales %u:%u\n", scale_h
, scale_v
);
1383 * 4. Calculate camera output window by applying combined scales to real
1386 cam_pix
->width
= scale_down(cam_rect
->width
, scale_h
);
1387 cam_pix
->height
= scale_down(cam_rect
->height
, scale_v
);
1388 cam_pix
->pixelformat
= xlate
->cam_fmt
->fourcc
;
1391 case V4L2_PIX_FMT_NV12
:
1392 case V4L2_PIX_FMT_NV21
:
1393 case V4L2_PIX_FMT_NV16
:
1394 case V4L2_PIX_FMT_NV61
:
1401 dev_geo(dev
, "4: camera output %ux%u\n",
1402 cam_pix
->width
, cam_pix
->height
);
1405 ret
= client_scale(icd
, cam_rect
, &cam_subrect
, &ceu_rect
, &cam_f
,
1406 image_mode
&& !is_interlaced
);
1408 dev_geo(dev
, "5-9: client scale %d\n", ret
);
1410 /* Done with the camera. Now see if we can improve the result */
1412 dev_dbg(dev
, "Camera %d fmt %ux%u, requested %ux%u\n",
1413 ret
, cam_pix
->width
, cam_pix
->height
, pix
->width
, pix
->height
);
1417 /* 10. Use CEU scaling to scale to the requested user window. */
1419 /* We cannot scale up */
1420 if (pix
->width
> cam_pix
->width
)
1421 pix
->width
= cam_pix
->width
;
1422 if (pix
->width
> ceu_rect
.width
)
1423 pix
->width
= ceu_rect
.width
;
1425 if (pix
->height
> cam_pix
->height
)
1426 pix
->height
= cam_pix
->height
;
1427 if (pix
->height
> ceu_rect
.height
)
1428 pix
->height
= ceu_rect
.height
;
1430 /* Let's rock: scale pix->{width x height} down to width x height */
1431 scale_h
= calc_scale(ceu_rect
.width
, &pix
->width
);
1432 scale_v
= calc_scale(ceu_rect
.height
, &pix
->height
);
1434 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1435 ceu_rect
.width
, scale_h
, pix
->width
,
1436 ceu_rect
.height
, scale_v
, pix
->height
);
1438 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1440 icd
->buswidth
= xlate
->buswidth
;
1441 icd
->current_fmt
= xlate
->host_fmt
;
1442 cam
->camera_fmt
= xlate
->cam_fmt
;
1443 cam
->ceu_rect
= ceu_rect
;
1445 pcdev
->is_interlaced
= is_interlaced
;
1446 pcdev
->image_mode
= image_mode
;
1451 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1452 struct v4l2_format
*f
)
1454 const struct soc_camera_format_xlate
*xlate
;
1455 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1456 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1457 __u32 pixfmt
= pix
->pixelformat
;
1461 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1463 dev_warn(icd
->dev
.parent
, "Format %x not found\n", pixfmt
);
1467 /* FIXME: calculate using depth and bus width */
1469 v4l_bound_align_image(&pix
->width
, 2, 2560, 1,
1470 &pix
->height
, 4, 1920, 2, 0);
1473 height
= pix
->height
;
1475 pix
->bytesperline
= pix
->width
*
1476 DIV_ROUND_UP(xlate
->host_fmt
->depth
, 8);
1477 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
1479 pix
->pixelformat
= xlate
->cam_fmt
->fourcc
;
1481 /* limit to sensor capabilities */
1482 ret
= v4l2_subdev_call(sd
, video
, try_fmt
, f
);
1483 pix
->pixelformat
= pixfmt
;
1488 case V4L2_PIX_FMT_NV12
:
1489 case V4L2_PIX_FMT_NV21
:
1490 case V4L2_PIX_FMT_NV16
:
1491 case V4L2_PIX_FMT_NV61
:
1492 /* FIXME: check against rect_max after converting soc-camera */
1493 /* We can scale precisely, need a bigger image from camera */
1494 if (pix
->width
< width
|| pix
->height
< height
) {
1495 int tmp_w
= pix
->width
, tmp_h
= pix
->height
;
1498 ret
= v4l2_subdev_call(sd
, video
, try_fmt
, f
);
1500 /* Shouldn't actually happen... */
1501 dev_err(icd
->dev
.parent
,
1502 "FIXME: try_fmt() returned %d\n", ret
);
1504 pix
->height
= tmp_h
;
1507 if (pix
->width
> width
)
1509 if (pix
->height
> height
)
1510 pix
->height
= height
;
1516 static int sh_mobile_ceu_reqbufs(struct soc_camera_file
*icf
,
1517 struct v4l2_requestbuffers
*p
)
1521 /* This is for locking debugging only. I removed spinlocks and now I
1522 * check whether .prepare is ever called on a linked buffer, or whether
1523 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1524 * it hadn't triggered */
1525 for (i
= 0; i
< p
->count
; i
++) {
1526 struct sh_mobile_ceu_buffer
*buf
;
1528 buf
= container_of(icf
->vb_vidq
.bufs
[i
],
1529 struct sh_mobile_ceu_buffer
, vb
);
1530 INIT_LIST_HEAD(&buf
->vb
.queue
);
1536 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1538 struct soc_camera_file
*icf
= file
->private_data
;
1539 struct sh_mobile_ceu_buffer
*buf
;
1541 buf
= list_entry(icf
->vb_vidq
.stream
.next
,
1542 struct sh_mobile_ceu_buffer
, vb
.stream
);
1544 poll_wait(file
, &buf
->vb
.done
, pt
);
1546 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1547 buf
->vb
.state
== VIDEOBUF_ERROR
)
1548 return POLLIN
|POLLRDNORM
;
1553 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1554 struct v4l2_capability
*cap
)
1556 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1557 cap
->version
= KERNEL_VERSION(0, 0, 5);
1558 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1562 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue
*q
,
1563 struct soc_camera_device
*icd
)
1565 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1566 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1568 videobuf_queue_dma_contig_init(q
,
1569 &sh_mobile_ceu_videobuf_ops
,
1570 icd
->dev
.parent
, &pcdev
->lock
,
1571 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1572 pcdev
->is_interlaced
?
1573 V4L2_FIELD_INTERLACED
: V4L2_FIELD_NONE
,
1574 sizeof(struct sh_mobile_ceu_buffer
),
1578 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device
*icd
,
1579 struct v4l2_control
*ctrl
)
1581 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1582 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1586 case V4L2_CID_SHARPNESS
:
1587 val
= ceu_read(pcdev
, CLFCR
);
1588 ctrl
->value
= val
^ 1;
1591 return -ENOIOCTLCMD
;
1594 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device
*icd
,
1595 struct v4l2_control
*ctrl
)
1597 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1598 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1601 case V4L2_CID_SHARPNESS
:
1602 switch (icd
->current_fmt
->fourcc
) {
1603 case V4L2_PIX_FMT_NV12
:
1604 case V4L2_PIX_FMT_NV21
:
1605 case V4L2_PIX_FMT_NV16
:
1606 case V4L2_PIX_FMT_NV61
:
1607 ceu_write(pcdev
, CLFCR
, !ctrl
->value
);
1612 return -ENOIOCTLCMD
;
1615 static const struct v4l2_queryctrl sh_mobile_ceu_controls
[] = {
1617 .id
= V4L2_CID_SHARPNESS
,
1618 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1619 .name
= "Low-pass filter",
1627 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1628 .owner
= THIS_MODULE
,
1629 .add
= sh_mobile_ceu_add_device
,
1630 .remove
= sh_mobile_ceu_remove_device
,
1631 .get_formats
= sh_mobile_ceu_get_formats
,
1632 .put_formats
= sh_mobile_ceu_put_formats
,
1633 .set_crop
= sh_mobile_ceu_set_crop
,
1634 .set_fmt
= sh_mobile_ceu_set_fmt
,
1635 .try_fmt
= sh_mobile_ceu_try_fmt
,
1636 .set_ctrl
= sh_mobile_ceu_set_ctrl
,
1637 .get_ctrl
= sh_mobile_ceu_get_ctrl
,
1638 .reqbufs
= sh_mobile_ceu_reqbufs
,
1639 .poll
= sh_mobile_ceu_poll
,
1640 .querycap
= sh_mobile_ceu_querycap
,
1641 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1642 .init_videobuf
= sh_mobile_ceu_init_videobuf
,
1643 .controls
= sh_mobile_ceu_controls
,
1644 .num_controls
= ARRAY_SIZE(sh_mobile_ceu_controls
),
1647 static int __devinit
sh_mobile_ceu_probe(struct platform_device
*pdev
)
1649 struct sh_mobile_ceu_dev
*pcdev
;
1650 struct resource
*res
;
1655 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1656 irq
= platform_get_irq(pdev
, 0);
1658 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1663 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
1665 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1670 INIT_LIST_HEAD(&pcdev
->capture
);
1671 spin_lock_init(&pcdev
->lock
);
1673 pcdev
->pdata
= pdev
->dev
.platform_data
;
1674 if (!pcdev
->pdata
) {
1676 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1680 base
= ioremap_nocache(res
->start
, resource_size(res
));
1683 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
1689 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1691 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1693 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
1697 DMA_MEMORY_EXCLUSIVE
);
1699 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
1704 pcdev
->video_limit
= resource_size(res
);
1708 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
1709 dev_name(&pdev
->dev
), pcdev
);
1711 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
1712 goto exit_release_mem
;
1715 pm_suspend_ignore_children(&pdev
->dev
, true);
1716 pm_runtime_enable(&pdev
->dev
);
1717 pm_runtime_resume(&pdev
->dev
);
1719 pcdev
->ici
.priv
= pcdev
;
1720 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
1721 pcdev
->ici
.nr
= pdev
->id
;
1722 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
1723 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
1725 err
= soc_camera_host_register(&pcdev
->ici
);
1732 free_irq(pcdev
->irq
, pcdev
);
1734 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1735 dma_release_declared_memory(&pdev
->dev
);
1744 static int __devexit
sh_mobile_ceu_remove(struct platform_device
*pdev
)
1746 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1747 struct sh_mobile_ceu_dev
*pcdev
= container_of(soc_host
,
1748 struct sh_mobile_ceu_dev
, ici
);
1750 soc_camera_host_unregister(soc_host
);
1751 free_irq(pcdev
->irq
, pcdev
);
1752 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1753 dma_release_declared_memory(&pdev
->dev
);
1754 iounmap(pcdev
->base
);
1759 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
1761 /* Runtime PM callback shared between ->runtime_suspend()
1762 * and ->runtime_resume(). Simply returns success.
1764 * This driver re-initializes all registers after
1765 * pm_runtime_get_sync() anyway so there is no need
1766 * to save and restore registers here.
1771 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
1772 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
1773 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
1776 static struct platform_driver sh_mobile_ceu_driver
= {
1778 .name
= "sh_mobile_ceu",
1779 .pm
= &sh_mobile_ceu_dev_pm_ops
,
1781 .probe
= sh_mobile_ceu_probe
,
1782 .remove
= __exit_p(sh_mobile_ceu_remove
),
1785 static int __init
sh_mobile_ceu_init(void)
1787 return platform_driver_register(&sh_mobile_ceu_driver
);
1790 static void __exit
sh_mobile_ceu_exit(void)
1792 platform_driver_unregister(&sh_mobile_ceu_driver
);
1795 module_init(sh_mobile_ceu_init
);
1796 module_exit(sh_mobile_ceu_exit
);
1798 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1799 MODULE_AUTHOR("Magnus Damm");
1800 MODULE_LICENSE("GPL");
1801 MODULE_ALIAS("platform:sh_mobile_ceu");