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>
41 #include <media/v4l2-mediabus.h>
42 #include <media/soc_mediabus.h>
44 /* register offsets for sh7722 / sh7723 */
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 */
82 #define dev_geo dev_info
84 #define dev_geo dev_dbg
87 /* per video frame buffer */
88 struct sh_mobile_ceu_buffer
{
89 struct videobuf_buffer vb
; /* v4l buffer must be first */
90 enum v4l2_mbus_pixelcode code
;
93 struct sh_mobile_ceu_dev
{
94 struct soc_camera_host ici
;
95 struct soc_camera_device
*icd
;
99 unsigned long video_limit
;
101 /* lock used to protect videobuf */
103 struct list_head capture
;
104 struct videobuf_buffer
*active
;
106 struct sh_mobile_ceu_info
*pdata
;
110 unsigned int is_interlaced
:1;
111 unsigned int image_mode
:1;
112 unsigned int is_16bit
:1;
115 struct sh_mobile_ceu_cam
{
116 struct v4l2_rect ceu_rect
;
117 unsigned int cam_width
;
118 unsigned int cam_height
;
119 const struct soc_mbus_pixelfmt
*extra_fmt
;
120 enum v4l2_mbus_pixelcode code
;
123 static unsigned long make_bus_param(struct sh_mobile_ceu_dev
*pcdev
)
127 flags
= SOCAM_MASTER
|
128 SOCAM_PCLK_SAMPLE_RISING
|
129 SOCAM_HSYNC_ACTIVE_HIGH
|
130 SOCAM_HSYNC_ACTIVE_LOW
|
131 SOCAM_VSYNC_ACTIVE_HIGH
|
132 SOCAM_VSYNC_ACTIVE_LOW
|
133 SOCAM_DATA_ACTIVE_HIGH
;
135 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_8BIT_BUS
)
136 flags
|= SOCAM_DATAWIDTH_8
;
138 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_16BIT_BUS
)
139 flags
|= SOCAM_DATAWIDTH_16
;
141 if (flags
& SOCAM_DATAWIDTH_MASK
)
147 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
148 unsigned long reg_offs
, u32 data
)
150 iowrite32(data
, priv
->base
+ reg_offs
);
153 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
155 return ioread32(priv
->base
+ reg_offs
);
158 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev
*pcdev
)
161 struct soc_camera_device
*icd
= pcdev
->icd
;
163 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
165 /* wait CSTSR.CPTON bit */
166 for (i
= 0; i
< 1000; i
++) {
167 if (!(ceu_read(pcdev
, CSTSR
) & 1)) {
174 /* wait CAPSR.CPKIL bit */
175 for (i
= 0; i
< 1000; i
++) {
176 if (!(ceu_read(pcdev
, CAPSR
) & (1 << 16))) {
185 dev_warn(&icd
->dev
, "soft reset time out\n");
193 * Videobuf operations
195 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue
*vq
,
199 struct soc_camera_device
*icd
= vq
->priv_data
;
200 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
201 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
202 int bytes_per_line
= soc_mbus_bytes_per_line(icd
->user_width
,
203 icd
->current_fmt
->host_fmt
);
205 if (bytes_per_line
< 0)
206 return bytes_per_line
;
208 *size
= PAGE_ALIGN(bytes_per_line
* icd
->user_height
);
213 if (pcdev
->video_limit
) {
214 while (*size
* *count
> pcdev
->video_limit
)
218 dev_dbg(icd
->dev
.parent
, "count=%d, size=%d\n", *count
, *size
);
223 static void free_buffer(struct videobuf_queue
*vq
,
224 struct sh_mobile_ceu_buffer
*buf
)
226 struct soc_camera_device
*icd
= vq
->priv_data
;
227 struct device
*dev
= icd
->dev
.parent
;
229 dev_dbg(dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
230 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
235 videobuf_waiton(&buf
->vb
, 0, 0);
236 videobuf_dma_contig_free(vq
, &buf
->vb
);
237 dev_dbg(dev
, "%s freed\n", __func__
);
238 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
241 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
242 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
243 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
244 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
245 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
246 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
250 * return value doesn't reflex the success/failure to queue the new buffer,
251 * but rather the status of the previous buffer.
253 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
255 struct soc_camera_device
*icd
= pcdev
->icd
;
256 dma_addr_t phys_addr_top
, phys_addr_bottom
;
261 * The hardware is _very_ picky about this sequence. Especially
262 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
263 * several not-so-well documented interrupt sources in CETCR.
265 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_MASK
);
266 status
= ceu_read(pcdev
, CETCR
);
267 ceu_write(pcdev
, CETCR
, ~status
& CEU_CETCR_MAGIC
);
268 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_MASK
);
269 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
270 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
273 * When a VBP interrupt occurs, a capture end interrupt does not occur
274 * and the image of that frame is not captured correctly. So, soft reset
277 if (status
& CEU_CEIER_VBP
) {
278 sh_mobile_ceu_soft_reset(pcdev
);
285 phys_addr_top
= videobuf_to_dma_contig(pcdev
->active
);
286 ceu_write(pcdev
, CDAYR
, phys_addr_top
);
287 if (pcdev
->is_interlaced
) {
288 phys_addr_bottom
= phys_addr_top
+ icd
->user_width
;
289 ceu_write(pcdev
, CDBYR
, phys_addr_bottom
);
292 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
293 case V4L2_PIX_FMT_NV12
:
294 case V4L2_PIX_FMT_NV21
:
295 case V4L2_PIX_FMT_NV16
:
296 case V4L2_PIX_FMT_NV61
:
297 phys_addr_top
+= icd
->user_width
*
299 ceu_write(pcdev
, CDACR
, phys_addr_top
);
300 if (pcdev
->is_interlaced
) {
301 phys_addr_bottom
= phys_addr_top
+
303 ceu_write(pcdev
, CDBCR
, phys_addr_bottom
);
307 pcdev
->active
->state
= VIDEOBUF_ACTIVE
;
308 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
313 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue
*vq
,
314 struct videobuf_buffer
*vb
,
315 enum v4l2_field field
)
317 struct soc_camera_device
*icd
= vq
->priv_data
;
318 struct sh_mobile_ceu_buffer
*buf
;
319 int bytes_per_line
= soc_mbus_bytes_per_line(icd
->user_width
,
320 icd
->current_fmt
->host_fmt
);
323 if (bytes_per_line
< 0)
324 return bytes_per_line
;
326 buf
= container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
328 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
329 vb
, vb
->baddr
, vb
->bsize
);
331 /* Added list head initialization on alloc */
332 WARN_ON(!list_empty(&vb
->queue
));
336 * This can be useful if you want to see if we actually fill
337 * the buffer with something
339 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
342 BUG_ON(NULL
== icd
->current_fmt
);
344 if (buf
->code
!= icd
->current_fmt
->code
||
345 vb
->width
!= icd
->user_width
||
346 vb
->height
!= icd
->user_height
||
347 vb
->field
!= field
) {
348 buf
->code
= icd
->current_fmt
->code
;
349 vb
->width
= icd
->user_width
;
350 vb
->height
= icd
->user_height
;
352 vb
->state
= VIDEOBUF_NEEDS_INIT
;
355 vb
->size
= vb
->height
* bytes_per_line
;
356 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
361 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
362 ret
= videobuf_iolock(vq
, vb
, NULL
);
365 vb
->state
= VIDEOBUF_PREPARED
;
370 free_buffer(vq
, buf
);
375 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
376 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue
*vq
,
377 struct videobuf_buffer
*vb
)
379 struct soc_camera_device
*icd
= vq
->priv_data
;
380 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
381 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
383 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
384 vb
, vb
->baddr
, vb
->bsize
);
386 vb
->state
= VIDEOBUF_QUEUED
;
387 list_add_tail(&vb
->queue
, &pcdev
->capture
);
389 if (!pcdev
->active
) {
391 * Because there were no active buffer at this moment,
392 * we are not interested in the return value of
393 * sh_mobile_ceu_capture here.
396 sh_mobile_ceu_capture(pcdev
);
400 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue
*vq
,
401 struct videobuf_buffer
*vb
)
403 struct soc_camera_device
*icd
= vq
->priv_data
;
404 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
405 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
408 spin_lock_irqsave(&pcdev
->lock
, flags
);
410 if (pcdev
->active
== vb
) {
411 /* disable capture (release DMA buffer), reset */
412 ceu_write(pcdev
, CAPSR
, 1 << 16);
413 pcdev
->active
= NULL
;
416 if ((vb
->state
== VIDEOBUF_ACTIVE
|| vb
->state
== VIDEOBUF_QUEUED
) &&
417 !list_empty(&vb
->queue
)) {
418 vb
->state
= VIDEOBUF_ERROR
;
419 list_del_init(&vb
->queue
);
422 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
424 free_buffer(vq
, container_of(vb
, struct sh_mobile_ceu_buffer
, vb
));
427 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops
= {
428 .buf_setup
= sh_mobile_ceu_videobuf_setup
,
429 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
430 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
431 .buf_release
= sh_mobile_ceu_videobuf_release
,
434 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
436 struct sh_mobile_ceu_dev
*pcdev
= data
;
437 struct videobuf_buffer
*vb
;
440 spin_lock_irqsave(&pcdev
->lock
, flags
);
444 /* Stale interrupt from a released buffer */
447 list_del_init(&vb
->queue
);
449 if (!list_empty(&pcdev
->capture
))
450 pcdev
->active
= list_entry(pcdev
->capture
.next
,
451 struct videobuf_buffer
, queue
);
453 pcdev
->active
= NULL
;
455 vb
->state
= (sh_mobile_ceu_capture(pcdev
) < 0) ?
456 VIDEOBUF_ERROR
: VIDEOBUF_DONE
;
457 do_gettimeofday(&vb
->ts
);
462 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
467 /* Called with .video_lock held */
468 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
470 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
471 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
477 dev_info(icd
->dev
.parent
,
478 "SuperH Mobile CEU driver attached to camera %d\n",
481 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
483 ret
= sh_mobile_ceu_soft_reset(pcdev
);
490 /* Called with .video_lock held */
491 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
493 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
494 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
497 BUG_ON(icd
!= pcdev
->icd
);
499 /* disable capture, disable interrupts */
500 ceu_write(pcdev
, CEIER
, 0);
501 sh_mobile_ceu_soft_reset(pcdev
);
503 /* make sure active buffer is canceled */
504 spin_lock_irqsave(&pcdev
->lock
, flags
);
506 list_del(&pcdev
->active
->queue
);
507 pcdev
->active
->state
= VIDEOBUF_ERROR
;
508 wake_up_all(&pcdev
->active
->done
);
509 pcdev
->active
= NULL
;
511 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
513 pm_runtime_put_sync(ici
->v4l2_dev
.dev
);
515 dev_info(icd
->dev
.parent
,
516 "SuperH Mobile CEU driver detached from camera %d\n",
523 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
524 * in SH7722 Hardware Manual
526 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
528 unsigned int mant_pre
= scale
>> 12;
531 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
532 mant_pre
* 4096 / scale
+ 1;
535 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
542 scale
= (src
* 4096 / *dst
) & ~7;
544 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
547 *dst
= size_dst(src
, scale
);
552 /* rect is guaranteed to not exceed the scaled camera rectangle */
553 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
,
554 unsigned int out_width
,
555 unsigned int out_height
)
557 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
558 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
559 struct v4l2_rect
*rect
= &cam
->ceu_rect
;
560 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
561 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
562 unsigned int left_offset
, top_offset
;
565 dev_dbg(icd
->dev
.parent
, "Crop %ux%u@%u:%u\n",
566 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
568 left_offset
= rect
->left
;
569 top_offset
= rect
->top
;
571 if (pcdev
->image_mode
) {
572 in_width
= rect
->width
;
573 if (!pcdev
->is_16bit
) {
578 cdwdr_width
= out_width
;
580 int bytes_per_line
= soc_mbus_bytes_per_line(out_width
,
581 icd
->current_fmt
->host_fmt
);
582 unsigned int w_factor
;
586 switch (icd
->current_fmt
->host_fmt
->packing
) {
587 case SOC_MBUS_PACKING_2X8_PADHI
:
594 in_width
= rect
->width
* w_factor
;
595 left_offset
= left_offset
* w_factor
;
597 if (bytes_per_line
< 0)
598 cdwdr_width
= out_width
;
600 cdwdr_width
= bytes_per_line
;
604 in_height
= rect
->height
;
605 if (pcdev
->is_interlaced
) {
612 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
613 camor
= left_offset
| (top_offset
<< 16);
615 dev_geo(icd
->dev
.parent
,
616 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
617 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
620 ceu_write(pcdev
, CAMOR
, camor
);
621 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
622 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
623 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
626 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
628 u32 capsr
= ceu_read(pcdev
, CAPSR
);
629 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
633 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
635 unsigned long timeout
= jiffies
+ 10 * HZ
;
638 * Wait until the end of the current frame. It can take a long time,
639 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
641 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
644 if (time_after(jiffies
, timeout
)) {
645 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
646 "Timeout waiting for frame end! Interface problem?\n");
650 /* Wait until reset clears, this shall not hang... */
651 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
654 /* Anything to restore? */
655 if (capsr
& ~(1 << 16))
656 ceu_write(pcdev
, CAPSR
, capsr
);
659 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
,
662 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
663 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
665 unsigned long camera_flags
, common_flags
, value
;
667 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
668 u32 capsr
= capture_save_reset(pcdev
);
670 camera_flags
= icd
->ops
->query_bus_param(icd
);
671 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
672 make_bus_param(pcdev
));
676 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
680 switch (common_flags
& SOCAM_DATAWIDTH_MASK
) {
681 case SOCAM_DATAWIDTH_8
:
684 case SOCAM_DATAWIDTH_16
:
691 ceu_write(pcdev
, CRCNTR
, 0);
692 ceu_write(pcdev
, CRCMPR
, 0);
694 value
= 0x00000010; /* data fetch by default */
697 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
698 case V4L2_PIX_FMT_NV12
:
699 case V4L2_PIX_FMT_NV21
:
700 yuv_lineskip
= 1; /* skip for NV12/21, no skip for NV16/61 */
702 case V4L2_PIX_FMT_NV16
:
703 case V4L2_PIX_FMT_NV61
:
705 case V4L2_MBUS_FMT_YUYV8_2X8_BE
:
706 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
708 case V4L2_MBUS_FMT_YVYU8_2X8_BE
:
709 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
711 case V4L2_MBUS_FMT_YUYV8_2X8_LE
:
712 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
714 case V4L2_MBUS_FMT_YVYU8_2X8_LE
:
715 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
722 if (icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
723 icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
724 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
726 value
|= common_flags
& SOCAM_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
727 value
|= common_flags
& SOCAM_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
728 value
|= pcdev
->is_16bit
? 1 << 12 : 0;
729 ceu_write(pcdev
, CAMCR
, value
);
731 ceu_write(pcdev
, CAPCR
, 0x00300000);
732 ceu_write(pcdev
, CAIFR
, pcdev
->is_interlaced
? 0x101 : 0);
734 sh_mobile_ceu_set_rect(icd
, icd
->user_width
, icd
->user_height
);
737 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
740 * A few words about byte order (observed in Big Endian mode)
742 * In data fetch mode bytes are received in chunks of 8 bytes.
743 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
745 * The data is however by default written to memory in reverse order:
746 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
748 * The lowest three bits of CDOCR allows us to do swapping,
749 * using 7 we swap the data bytes to match the incoming order:
750 * D0, D1, D2, D3, D4, D5, D6, D7
754 value
&= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
756 ceu_write(pcdev
, CDOCR
, value
);
757 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
759 dev_dbg(icd
->dev
.parent
, "S_FMT successful for %c%c%c%c %ux%u\n",
760 pixfmt
& 0xff, (pixfmt
>> 8) & 0xff,
761 (pixfmt
>> 16) & 0xff, (pixfmt
>> 24) & 0xff,
762 icd
->user_width
, icd
->user_height
);
764 capture_restore(pcdev
, capsr
);
766 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
770 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
771 unsigned char buswidth
)
773 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
774 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
775 unsigned long camera_flags
, common_flags
;
777 camera_flags
= icd
->ops
->query_bus_param(icd
);
778 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
779 make_bus_param(pcdev
));
780 if (!common_flags
|| buswidth
> 16 ||
781 (buswidth
> 8 && !(common_flags
& SOCAM_DATAWIDTH_16
)))
787 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats
[] = {
789 .fourcc
= V4L2_PIX_FMT_NV12
,
791 .bits_per_sample
= 12,
792 .packing
= SOC_MBUS_PACKING_NONE
,
793 .order
= SOC_MBUS_ORDER_LE
,
795 .fourcc
= V4L2_PIX_FMT_NV21
,
797 .bits_per_sample
= 12,
798 .packing
= SOC_MBUS_PACKING_NONE
,
799 .order
= SOC_MBUS_ORDER_LE
,
801 .fourcc
= V4L2_PIX_FMT_NV16
,
803 .bits_per_sample
= 16,
804 .packing
= SOC_MBUS_PACKING_NONE
,
805 .order
= SOC_MBUS_ORDER_LE
,
807 .fourcc
= V4L2_PIX_FMT_NV61
,
809 .bits_per_sample
= 16,
810 .packing
= SOC_MBUS_PACKING_NONE
,
811 .order
= SOC_MBUS_ORDER_LE
,
815 /* This will be corrected as we get more formats */
816 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
818 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
819 (fmt
->bits_per_sample
== 8 &&
820 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
821 (fmt
->bits_per_sample
> 8 &&
822 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
825 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, int idx
,
826 struct soc_camera_format_xlate
*xlate
)
828 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
829 struct device
*dev
= icd
->dev
.parent
;
832 struct sh_mobile_ceu_cam
*cam
;
833 enum v4l2_mbus_pixelcode code
;
834 const struct soc_mbus_pixelfmt
*fmt
;
836 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, idx
, &code
);
838 /* No more formats */
841 fmt
= soc_mbus_get_fmtdesc(code
);
843 dev_err(icd
->dev
.parent
,
844 "Invalid format code #%d: %d\n", idx
, code
);
848 ret
= sh_mobile_ceu_try_bus_param(icd
, fmt
->bits_per_sample
);
852 if (!icd
->host_priv
) {
853 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
857 icd
->host_priv
= cam
;
859 cam
= icd
->host_priv
;
862 /* Beginning of a pass */
864 cam
->extra_fmt
= NULL
;
867 case V4L2_MBUS_FMT_YUYV8_2X8_BE
:
868 case V4L2_MBUS_FMT_YVYU8_2X8_BE
:
869 case V4L2_MBUS_FMT_YUYV8_2X8_LE
:
870 case V4L2_MBUS_FMT_YVYU8_2X8_LE
:
875 * Our case is simple so far: for any of the above four camera
876 * formats we add all our four synthesized NV* formats, so,
877 * just marking the device with a single flag suffices. If
878 * the format generation rules are more complex, you would have
879 * to actually hang your already added / counted formats onto
880 * the host_priv pointer and check whether the format you're
881 * going to add now is already there.
883 cam
->extra_fmt
= sh_mobile_ceu_formats
;
885 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
887 for (k
= 0; xlate
&& k
< n
; k
++) {
888 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
891 dev_dbg(dev
, "Providing format %s using code %d\n",
892 sh_mobile_ceu_formats
[k
].name
, code
);
896 if (!sh_mobile_ceu_packing_supported(fmt
))
900 /* Generic pass-through */
903 xlate
->host_fmt
= fmt
;
906 dev_dbg(dev
, "Providing format %s in pass-through mode\n",
907 xlate
->host_fmt
->name
);
913 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
915 kfree(icd
->host_priv
);
916 icd
->host_priv
= NULL
;
919 /* Check if any dimension of r1 is smaller than respective one of r2 */
920 static bool is_smaller(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
922 return r1
->width
< r2
->width
|| r1
->height
< r2
->height
;
925 /* Check if r1 fails to cover r2 */
926 static bool is_inside(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
928 return r1
->left
> r2
->left
|| r1
->top
> r2
->top
||
929 r1
->left
+ r1
->width
< r2
->left
+ r2
->width
||
930 r1
->top
+ r1
->height
< r2
->top
+ r2
->height
;
933 static unsigned int scale_down(unsigned int size
, unsigned int scale
)
935 return (size
* 4096 + scale
/ 2) / scale
;
938 static unsigned int scale_up(unsigned int size
, unsigned int scale
)
940 return (size
* scale
+ 2048) / 4096;
943 static unsigned int calc_generic_scale(unsigned int input
, unsigned int output
)
945 return (input
* 4096 + output
/ 2) / output
;
948 static int client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
)
950 struct v4l2_crop crop
;
951 struct v4l2_cropcap cap
;
954 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
956 ret
= v4l2_subdev_call(sd
, video
, g_crop
, &crop
);
962 /* Camera driver doesn't support .g_crop(), assume default rectangle */
963 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
965 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
975 * The common for both scaling and cropping iterative approach is:
976 * 1. try if the client can produce exactly what requested by the user
977 * 2. if (1) failed, try to double the client image until we get one big enough
978 * 3. if (2) failed, try to request the maximum image
980 static int client_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*crop
,
981 struct v4l2_crop
*cam_crop
)
983 struct v4l2_rect
*rect
= &crop
->c
, *cam_rect
= &cam_crop
->c
;
984 struct device
*dev
= sd
->v4l2_dev
->dev
;
985 struct v4l2_cropcap cap
;
987 unsigned int width
, height
;
989 v4l2_subdev_call(sd
, video
, s_crop
, crop
);
990 ret
= client_g_rect(sd
, cam_rect
);
995 * Now cam_crop contains the current camera input rectangle, and it must
996 * be within camera cropcap bounds
998 if (!memcmp(rect
, cam_rect
, sizeof(*rect
))) {
999 /* Even if camera S_CROP failed, but camera rectangle matches */
1000 dev_dbg(dev
, "Camera S_CROP successful for %ux%u@%u:%u\n",
1001 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1005 /* Try to fix cropping, that camera hasn't managed to set */
1006 dev_geo(dev
, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
1007 cam_rect
->width
, cam_rect
->height
,
1008 cam_rect
->left
, cam_rect
->top
,
1009 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1011 /* We need sensor maximum rectangle */
1012 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1016 soc_camera_limit_side(&rect
->left
, &rect
->width
, cap
.bounds
.left
, 2,
1018 soc_camera_limit_side(&rect
->top
, &rect
->height
, cap
.bounds
.top
, 4,
1022 * Popular special case - some cameras can only handle fixed sizes like
1023 * QVGA, VGA,... Take care to avoid infinite loop.
1025 width
= max(cam_rect
->width
, 2);
1026 height
= max(cam_rect
->height
, 2);
1028 while (!ret
&& (is_smaller(cam_rect
, rect
) ||
1029 is_inside(cam_rect
, rect
)) &&
1030 (cap
.bounds
.width
> width
|| cap
.bounds
.height
> height
)) {
1035 cam_rect
->width
= width
;
1036 cam_rect
->height
= height
;
1039 * We do not know what capabilities the camera has to set up
1040 * left and top borders. We could try to be smarter in iterating
1041 * them, e.g., if camera current left is to the right of the
1042 * target left, set it to the middle point between the current
1043 * left and minimum left. But that would add too much
1044 * complexity: we would have to iterate each border separately.
1046 if (cam_rect
->left
> rect
->left
)
1047 cam_rect
->left
= cap
.bounds
.left
;
1049 if (cam_rect
->left
+ cam_rect
->width
< rect
->left
+ rect
->width
)
1050 cam_rect
->width
= rect
->left
+ rect
->width
-
1053 if (cam_rect
->top
> rect
->top
)
1054 cam_rect
->top
= cap
.bounds
.top
;
1056 if (cam_rect
->top
+ cam_rect
->height
< rect
->top
+ rect
->height
)
1057 cam_rect
->height
= rect
->top
+ rect
->height
-
1060 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1061 ret
= client_g_rect(sd
, cam_rect
);
1062 dev_geo(dev
, "Camera S_CROP %d for %ux%u@%u:%u\n", ret
,
1063 cam_rect
->width
, cam_rect
->height
,
1064 cam_rect
->left
, cam_rect
->top
);
1067 /* S_CROP must not modify the rectangle */
1068 if (is_smaller(cam_rect
, rect
) || is_inside(cam_rect
, rect
)) {
1070 * The camera failed to configure a suitable cropping,
1071 * we cannot use the current rectangle, set to max
1073 *cam_rect
= cap
.bounds
;
1074 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1075 ret
= client_g_rect(sd
, cam_rect
);
1076 dev_geo(dev
, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret
,
1077 cam_rect
->width
, cam_rect
->height
,
1078 cam_rect
->left
, cam_rect
->top
);
1084 static int get_camera_scales(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
,
1085 unsigned int *scale_h
, unsigned int *scale_v
)
1087 struct v4l2_mbus_framefmt mf
;
1090 ret
= v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
);
1094 *scale_h
= calc_generic_scale(rect
->width
, mf
.width
);
1095 *scale_v
= calc_generic_scale(rect
->height
, mf
.height
);
1100 static int get_camera_subwin(struct soc_camera_device
*icd
,
1101 struct v4l2_rect
*cam_subrect
,
1102 unsigned int cam_hscale
, unsigned int cam_vscale
)
1104 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1105 struct v4l2_rect
*ceu_rect
= &cam
->ceu_rect
;
1107 if (!ceu_rect
->width
) {
1108 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1109 struct device
*dev
= icd
->dev
.parent
;
1110 struct v4l2_mbus_framefmt mf
;
1114 ret
= v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
);
1118 dev_geo(dev
, "camera fmt %ux%u\n", mf
.width
, mf
.height
);
1120 if (mf
.width
> 2560) {
1121 ceu_rect
->width
= 2560;
1122 ceu_rect
->left
= (mf
.width
- 2560) / 2;
1124 ceu_rect
->width
= mf
.width
;
1128 if (mf
.height
> 1920) {
1129 ceu_rect
->height
= 1920;
1130 ceu_rect
->top
= (mf
.height
- 1920) / 2;
1132 ceu_rect
->height
= mf
.height
;
1136 dev_geo(dev
, "initialised CEU rect %ux%u@%u:%u\n",
1137 ceu_rect
->width
, ceu_rect
->height
,
1138 ceu_rect
->left
, ceu_rect
->top
);
1141 cam_subrect
->width
= scale_up(ceu_rect
->width
, cam_hscale
);
1142 cam_subrect
->left
= scale_up(ceu_rect
->left
, cam_hscale
);
1143 cam_subrect
->height
= scale_up(ceu_rect
->height
, cam_vscale
);
1144 cam_subrect
->top
= scale_up(ceu_rect
->top
, cam_vscale
);
1149 static int client_s_fmt(struct soc_camera_device
*icd
,
1150 struct v4l2_mbus_framefmt
*mf
, bool ceu_can_scale
)
1152 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1153 struct device
*dev
= icd
->dev
.parent
;
1154 unsigned int width
= mf
->width
, height
= mf
->height
, tmp_w
, tmp_h
;
1155 unsigned int max_width
, max_height
;
1156 struct v4l2_cropcap cap
;
1159 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1161 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1165 max_width
= min(cap
.bounds
.width
, 2560);
1166 max_height
= min(cap
.bounds
.height
, 1920);
1168 ret
= v4l2_subdev_call(sd
, video
, s_mbus_fmt
, mf
);
1172 dev_geo(dev
, "camera scaled to %ux%u\n", mf
->width
, mf
->height
);
1174 if ((width
== mf
->width
&& height
== mf
->height
) || !ceu_can_scale
)
1177 /* Camera set a format, but geometry is not precise, try to improve */
1181 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1182 while ((width
> tmp_w
|| height
> tmp_h
) &&
1183 tmp_w
< max_width
&& tmp_h
< max_height
) {
1184 tmp_w
= min(2 * tmp_w
, max_width
);
1185 tmp_h
= min(2 * tmp_h
, max_height
);
1188 ret
= v4l2_subdev_call(sd
, video
, s_mbus_fmt
, mf
);
1189 dev_geo(dev
, "Camera scaled to %ux%u\n",
1190 mf
->width
, mf
->height
);
1192 /* This shouldn't happen */
1193 dev_err(dev
, "Client failed to set format: %d\n", ret
);
1202 * @rect - camera cropped rectangle
1203 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1204 * @ceu_rect - on output calculated CEU crop rectangle
1206 static int client_scale(struct soc_camera_device
*icd
, struct v4l2_rect
*rect
,
1207 struct v4l2_rect
*sub_rect
, struct v4l2_rect
*ceu_rect
,
1208 struct v4l2_mbus_framefmt
*mf
, bool ceu_can_scale
)
1210 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1211 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1212 struct device
*dev
= icd
->dev
.parent
;
1213 struct v4l2_mbus_framefmt mf_tmp
= *mf
;
1214 unsigned int scale_h
, scale_v
;
1217 /* 5. Apply iterative camera S_FMT for camera user window. */
1218 ret
= client_s_fmt(icd
, &mf_tmp
, ceu_can_scale
);
1222 dev_geo(dev
, "5: camera scaled to %ux%u\n",
1223 mf_tmp
.width
, mf_tmp
.height
);
1225 /* 6. Retrieve camera output window (g_fmt) */
1227 /* unneeded - it is already in "mf_tmp" */
1229 /* 7. Calculate new camera scales. */
1230 ret
= get_camera_scales(sd
, rect
, &scale_h
, &scale_v
);
1234 dev_geo(dev
, "7: camera scales %u:%u\n", scale_h
, scale_v
);
1236 cam
->cam_width
= mf_tmp
.width
;
1237 cam
->cam_height
= mf_tmp
.height
;
1238 mf
->width
= mf_tmp
.width
;
1239 mf
->height
= mf_tmp
.height
;
1240 mf
->colorspace
= mf_tmp
.colorspace
;
1243 * 8. Calculate new CEU crop - apply camera scales to previously
1244 * calculated "effective" crop.
1246 ceu_rect
->left
= scale_down(sub_rect
->left
, scale_h
);
1247 ceu_rect
->width
= scale_down(sub_rect
->width
, scale_h
);
1248 ceu_rect
->top
= scale_down(sub_rect
->top
, scale_v
);
1249 ceu_rect
->height
= scale_down(sub_rect
->height
, scale_v
);
1251 dev_geo(dev
, "8: new CEU rect %ux%u@%u:%u\n",
1252 ceu_rect
->width
, ceu_rect
->height
,
1253 ceu_rect
->left
, ceu_rect
->top
);
1258 /* Get combined scales */
1259 static int get_scales(struct soc_camera_device
*icd
,
1260 unsigned int *scale_h
, unsigned int *scale_v
)
1262 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1263 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1264 struct v4l2_crop cam_crop
;
1265 unsigned int width_in
, height_in
;
1268 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1270 ret
= client_g_rect(sd
, &cam_crop
.c
);
1274 ret
= get_camera_scales(sd
, &cam_crop
.c
, scale_h
, scale_v
);
1278 width_in
= scale_up(cam
->ceu_rect
.width
, *scale_h
);
1279 height_in
= scale_up(cam
->ceu_rect
.height
, *scale_v
);
1281 *scale_h
= calc_generic_scale(width_in
, icd
->user_width
);
1282 *scale_v
= calc_generic_scale(height_in
, icd
->user_height
);
1288 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1289 * framerate by always requesting the maximum image from the client. See
1290 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1291 * scaling and cropping algorithms and for the meaning of referenced here steps.
1293 static int sh_mobile_ceu_set_crop(struct soc_camera_device
*icd
,
1294 struct v4l2_crop
*a
)
1296 struct v4l2_rect
*rect
= &a
->c
;
1297 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1298 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1299 struct v4l2_crop cam_crop
;
1300 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1301 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, *ceu_rect
= &cam
->ceu_rect
;
1302 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1303 struct device
*dev
= icd
->dev
.parent
;
1304 struct v4l2_mbus_framefmt mf
;
1305 unsigned int scale_comb_h
, scale_comb_v
, scale_ceu_h
, scale_ceu_v
,
1306 out_width
, out_height
;
1310 /* 1. Calculate current combined scales. */
1311 ret
= get_scales(icd
, &scale_comb_h
, &scale_comb_v
);
1315 dev_geo(dev
, "1: combined scales %u:%u\n", scale_comb_h
, scale_comb_v
);
1317 /* 2. Apply iterative camera S_CROP for new input window. */
1318 ret
= client_s_crop(sd
, a
, &cam_crop
);
1322 dev_geo(dev
, "2: camera cropped to %ux%u@%u:%u\n",
1323 cam_rect
->width
, cam_rect
->height
,
1324 cam_rect
->left
, cam_rect
->top
);
1326 /* On success cam_crop contains current camera crop */
1329 * 3. If old combined scales applied to new crop produce an impossible
1330 * user window, adjust scales to produce nearest possible window.
1332 out_width
= scale_down(rect
->width
, scale_comb_h
);
1333 out_height
= scale_down(rect
->height
, scale_comb_v
);
1335 if (out_width
> 2560)
1337 else if (out_width
< 2)
1340 if (out_height
> 1920)
1342 else if (out_height
< 4)
1345 dev_geo(dev
, "3: Adjusted output %ux%u\n", out_width
, out_height
);
1347 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1350 * 5. Using actual input window and calculated combined scales calculate
1351 * camera target output window.
1353 mf
.width
= scale_down(cam_rect
->width
, scale_comb_h
);
1354 mf
.height
= scale_down(cam_rect
->height
, scale_comb_v
);
1356 dev_geo(dev
, "5: camera target %ux%u\n", mf
.width
, mf
.height
);
1359 mf
.code
= cam
->code
;
1360 mf
.field
= pcdev
->is_interlaced
? V4L2_FIELD_INTERLACED
:
1363 capsr
= capture_save_reset(pcdev
);
1364 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1366 /* Make relative to camera rectangle */
1367 rect
->left
-= cam_rect
->left
;
1368 rect
->top
-= cam_rect
->top
;
1370 ret
= client_scale(icd
, cam_rect
, rect
, ceu_rect
, &mf
,
1371 pcdev
->image_mode
&& !pcdev
->is_interlaced
);
1373 dev_geo(dev
, "6-9: %d\n", ret
);
1375 /* 10. Use CEU cropping to crop to the new window. */
1376 sh_mobile_ceu_set_rect(icd
, out_width
, out_height
);
1378 dev_geo(dev
, "10: CEU cropped to %ux%u@%u:%u\n",
1379 ceu_rect
->width
, ceu_rect
->height
,
1380 ceu_rect
->left
, ceu_rect
->top
);
1383 * 11. Calculate CEU scales from camera scales from results of (10) and
1384 * user window from (3)
1386 scale_ceu_h
= calc_scale(ceu_rect
->width
, &out_width
);
1387 scale_ceu_v
= calc_scale(ceu_rect
->height
, &out_height
);
1389 dev_geo(dev
, "11: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1391 /* 12. Apply CEU scales. */
1392 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1393 if (cflcr
!= pcdev
->cflcr
) {
1394 pcdev
->cflcr
= cflcr
;
1395 ceu_write(pcdev
, CFLCR
, cflcr
);
1398 /* Restore capture */
1401 capture_restore(pcdev
, capsr
);
1403 icd
->user_width
= out_width
;
1404 icd
->user_height
= out_height
;
1406 /* Even if only camera cropping succeeded */
1410 /* Similar to set_crop multistage iterative algorithm */
1411 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1412 struct v4l2_format
*f
)
1414 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1415 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1416 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1417 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1418 struct v4l2_mbus_framefmt mf
;
1419 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1420 struct device
*dev
= icd
->dev
.parent
;
1421 __u32 pixfmt
= pix
->pixelformat
;
1422 const struct soc_camera_format_xlate
*xlate
;
1423 struct v4l2_crop cam_crop
;
1424 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, cam_subrect
, ceu_rect
;
1425 unsigned int scale_cam_h
, scale_cam_v
;
1426 u16 scale_v
, scale_h
;
1428 bool is_interlaced
, image_mode
;
1430 switch (pix
->field
) {
1431 case V4L2_FIELD_INTERLACED
:
1432 is_interlaced
= true;
1434 case V4L2_FIELD_ANY
:
1436 pix
->field
= V4L2_FIELD_NONE
;
1438 case V4L2_FIELD_NONE
:
1439 is_interlaced
= false;
1443 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1445 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1449 /* 1. Calculate current camera scales. */
1450 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1452 ret
= client_g_rect(sd
, cam_rect
);
1456 ret
= get_camera_scales(sd
, cam_rect
, &scale_cam_h
, &scale_cam_v
);
1460 dev_geo(dev
, "1: camera scales %u:%u\n", scale_cam_h
, scale_cam_v
);
1463 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1464 * scaled back at current camera scales onto input window.
1466 ret
= get_camera_subwin(icd
, &cam_subrect
, scale_cam_h
, scale_cam_v
);
1470 dev_geo(dev
, "2: subwin %ux%u@%u:%u\n",
1471 cam_subrect
.width
, cam_subrect
.height
,
1472 cam_subrect
.left
, cam_subrect
.top
);
1475 * 3. Calculate new combined scales from "effective" input window to
1476 * requested user window.
1478 scale_h
= calc_generic_scale(cam_subrect
.width
, pix
->width
);
1479 scale_v
= calc_generic_scale(cam_subrect
.height
, pix
->height
);
1481 dev_geo(dev
, "3: scales %u:%u\n", scale_h
, scale_v
);
1484 * 4. Calculate camera output window by applying combined scales to real
1487 mf
.width
= scale_down(cam_rect
->width
, scale_h
);
1488 mf
.height
= scale_down(cam_rect
->height
, scale_v
);
1489 mf
.field
= pix
->field
;
1490 mf
.colorspace
= pix
->colorspace
;
1491 mf
.code
= xlate
->code
;
1494 case V4L2_PIX_FMT_NV12
:
1495 case V4L2_PIX_FMT_NV21
:
1496 case V4L2_PIX_FMT_NV16
:
1497 case V4L2_PIX_FMT_NV61
:
1504 dev_geo(dev
, "4: camera output %ux%u\n", mf
.width
, mf
.height
);
1507 ret
= client_scale(icd
, cam_rect
, &cam_subrect
, &ceu_rect
, &mf
,
1508 image_mode
&& !is_interlaced
);
1510 dev_geo(dev
, "5-9: client scale %d\n", ret
);
1512 /* Done with the camera. Now see if we can improve the result */
1514 dev_dbg(dev
, "Camera %d fmt %ux%u, requested %ux%u\n",
1515 ret
, mf
.width
, mf
.height
, pix
->width
, pix
->height
);
1519 if (mf
.code
!= xlate
->code
)
1522 /* 10. Use CEU scaling to scale to the requested user window. */
1524 /* We cannot scale up */
1525 if (pix
->width
> mf
.width
)
1526 pix
->width
= mf
.width
;
1527 if (pix
->width
> ceu_rect
.width
)
1528 pix
->width
= ceu_rect
.width
;
1530 if (pix
->height
> mf
.height
)
1531 pix
->height
= mf
.height
;
1532 if (pix
->height
> ceu_rect
.height
)
1533 pix
->height
= ceu_rect
.height
;
1535 pix
->colorspace
= mf
.colorspace
;
1538 /* Scale pix->{width x height} down to width x height */
1539 scale_h
= calc_scale(ceu_rect
.width
, &pix
->width
);
1540 scale_v
= calc_scale(ceu_rect
.height
, &pix
->height
);
1542 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1544 pix
->width
= ceu_rect
.width
;
1545 pix
->height
= ceu_rect
.height
;
1546 scale_h
= scale_v
= 0;
1550 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1551 ceu_rect
.width
, scale_h
, pix
->width
,
1552 ceu_rect
.height
, scale_v
, pix
->height
);
1554 cam
->code
= xlate
->code
;
1555 cam
->ceu_rect
= ceu_rect
;
1556 icd
->current_fmt
= xlate
;
1558 pcdev
->is_interlaced
= is_interlaced
;
1559 pcdev
->image_mode
= image_mode
;
1564 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1565 struct v4l2_format
*f
)
1567 const struct soc_camera_format_xlate
*xlate
;
1568 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1569 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1570 struct v4l2_mbus_framefmt mf
;
1571 __u32 pixfmt
= pix
->pixelformat
;
1575 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1577 dev_warn(icd
->dev
.parent
, "Format %x not found\n", pixfmt
);
1581 /* FIXME: calculate using depth and bus width */
1583 v4l_bound_align_image(&pix
->width
, 2, 2560, 1,
1584 &pix
->height
, 4, 1920, 2, 0);
1587 height
= pix
->height
;
1589 pix
->bytesperline
= soc_mbus_bytes_per_line(width
, xlate
->host_fmt
);
1590 if (pix
->bytesperline
< 0)
1591 return pix
->bytesperline
;
1592 pix
->sizeimage
= height
* pix
->bytesperline
;
1594 /* limit to sensor capabilities */
1595 mf
.width
= pix
->width
;
1596 mf
.height
= pix
->height
;
1597 mf
.field
= pix
->field
;
1598 mf
.code
= xlate
->code
;
1599 mf
.colorspace
= pix
->colorspace
;
1601 ret
= v4l2_subdev_call(sd
, video
, try_mbus_fmt
, &mf
);
1605 pix
->width
= mf
.width
;
1606 pix
->height
= mf
.height
;
1607 pix
->field
= mf
.field
;
1608 pix
->colorspace
= mf
.colorspace
;
1611 case V4L2_PIX_FMT_NV12
:
1612 case V4L2_PIX_FMT_NV21
:
1613 case V4L2_PIX_FMT_NV16
:
1614 case V4L2_PIX_FMT_NV61
:
1615 /* FIXME: check against rect_max after converting soc-camera */
1616 /* We can scale precisely, need a bigger image from camera */
1617 if (pix
->width
< width
|| pix
->height
< height
) {
1619 * We presume, the sensor behaves sanely, i.e., if
1620 * requested a bigger rectangle, it will not return a
1625 ret
= v4l2_subdev_call(sd
, video
, try_mbus_fmt
, &mf
);
1627 /* Shouldn't actually happen... */
1628 dev_err(icd
->dev
.parent
,
1629 "FIXME: client try_fmt() = %d\n", ret
);
1633 /* We will scale exactly */
1634 if (mf
.width
> width
)
1636 if (mf
.height
> height
)
1637 pix
->height
= height
;
1643 static int sh_mobile_ceu_reqbufs(struct soc_camera_file
*icf
,
1644 struct v4l2_requestbuffers
*p
)
1649 * This is for locking debugging only. I removed spinlocks and now I
1650 * check whether .prepare is ever called on a linked buffer, or whether
1651 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1652 * it hadn't triggered
1654 for (i
= 0; i
< p
->count
; i
++) {
1655 struct sh_mobile_ceu_buffer
*buf
;
1657 buf
= container_of(icf
->vb_vidq
.bufs
[i
],
1658 struct sh_mobile_ceu_buffer
, vb
);
1659 INIT_LIST_HEAD(&buf
->vb
.queue
);
1665 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1667 struct soc_camera_file
*icf
= file
->private_data
;
1668 struct sh_mobile_ceu_buffer
*buf
;
1670 buf
= list_entry(icf
->vb_vidq
.stream
.next
,
1671 struct sh_mobile_ceu_buffer
, vb
.stream
);
1673 poll_wait(file
, &buf
->vb
.done
, pt
);
1675 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1676 buf
->vb
.state
== VIDEOBUF_ERROR
)
1677 return POLLIN
|POLLRDNORM
;
1682 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1683 struct v4l2_capability
*cap
)
1685 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1686 cap
->version
= KERNEL_VERSION(0, 0, 5);
1687 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1691 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue
*q
,
1692 struct soc_camera_device
*icd
)
1694 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1695 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1697 videobuf_queue_dma_contig_init(q
,
1698 &sh_mobile_ceu_videobuf_ops
,
1699 icd
->dev
.parent
, &pcdev
->lock
,
1700 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1701 pcdev
->is_interlaced
?
1702 V4L2_FIELD_INTERLACED
: V4L2_FIELD_NONE
,
1703 sizeof(struct sh_mobile_ceu_buffer
),
1707 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device
*icd
,
1708 struct v4l2_control
*ctrl
)
1710 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1711 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1715 case V4L2_CID_SHARPNESS
:
1716 val
= ceu_read(pcdev
, CLFCR
);
1717 ctrl
->value
= val
^ 1;
1720 return -ENOIOCTLCMD
;
1723 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device
*icd
,
1724 struct v4l2_control
*ctrl
)
1726 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1727 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1730 case V4L2_CID_SHARPNESS
:
1731 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
1732 case V4L2_PIX_FMT_NV12
:
1733 case V4L2_PIX_FMT_NV21
:
1734 case V4L2_PIX_FMT_NV16
:
1735 case V4L2_PIX_FMT_NV61
:
1736 ceu_write(pcdev
, CLFCR
, !ctrl
->value
);
1741 return -ENOIOCTLCMD
;
1744 static const struct v4l2_queryctrl sh_mobile_ceu_controls
[] = {
1746 .id
= V4L2_CID_SHARPNESS
,
1747 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1748 .name
= "Low-pass filter",
1756 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1757 .owner
= THIS_MODULE
,
1758 .add
= sh_mobile_ceu_add_device
,
1759 .remove
= sh_mobile_ceu_remove_device
,
1760 .get_formats
= sh_mobile_ceu_get_formats
,
1761 .put_formats
= sh_mobile_ceu_put_formats
,
1762 .set_crop
= sh_mobile_ceu_set_crop
,
1763 .set_fmt
= sh_mobile_ceu_set_fmt
,
1764 .try_fmt
= sh_mobile_ceu_try_fmt
,
1765 .set_ctrl
= sh_mobile_ceu_set_ctrl
,
1766 .get_ctrl
= sh_mobile_ceu_get_ctrl
,
1767 .reqbufs
= sh_mobile_ceu_reqbufs
,
1768 .poll
= sh_mobile_ceu_poll
,
1769 .querycap
= sh_mobile_ceu_querycap
,
1770 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1771 .init_videobuf
= sh_mobile_ceu_init_videobuf
,
1772 .controls
= sh_mobile_ceu_controls
,
1773 .num_controls
= ARRAY_SIZE(sh_mobile_ceu_controls
),
1776 static int __devinit
sh_mobile_ceu_probe(struct platform_device
*pdev
)
1778 struct sh_mobile_ceu_dev
*pcdev
;
1779 struct resource
*res
;
1784 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1785 irq
= platform_get_irq(pdev
, 0);
1787 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1792 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
1794 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1799 INIT_LIST_HEAD(&pcdev
->capture
);
1800 spin_lock_init(&pcdev
->lock
);
1802 pcdev
->pdata
= pdev
->dev
.platform_data
;
1803 if (!pcdev
->pdata
) {
1805 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1809 base
= ioremap_nocache(res
->start
, resource_size(res
));
1812 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
1818 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1820 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1822 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
1826 DMA_MEMORY_EXCLUSIVE
);
1828 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
1833 pcdev
->video_limit
= resource_size(res
);
1837 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
1838 dev_name(&pdev
->dev
), pcdev
);
1840 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
1841 goto exit_release_mem
;
1844 pm_suspend_ignore_children(&pdev
->dev
, true);
1845 pm_runtime_enable(&pdev
->dev
);
1846 pm_runtime_resume(&pdev
->dev
);
1848 pcdev
->ici
.priv
= pcdev
;
1849 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
1850 pcdev
->ici
.nr
= pdev
->id
;
1851 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
1852 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
1854 err
= soc_camera_host_register(&pcdev
->ici
);
1861 pm_runtime_disable(&pdev
->dev
);
1862 free_irq(pcdev
->irq
, pcdev
);
1864 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1865 dma_release_declared_memory(&pdev
->dev
);
1874 static int __devexit
sh_mobile_ceu_remove(struct platform_device
*pdev
)
1876 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1877 struct sh_mobile_ceu_dev
*pcdev
= container_of(soc_host
,
1878 struct sh_mobile_ceu_dev
, ici
);
1880 soc_camera_host_unregister(soc_host
);
1881 pm_runtime_disable(&pdev
->dev
);
1882 free_irq(pcdev
->irq
, pcdev
);
1883 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1884 dma_release_declared_memory(&pdev
->dev
);
1885 iounmap(pcdev
->base
);
1890 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
1892 /* Runtime PM callback shared between ->runtime_suspend()
1893 * and ->runtime_resume(). Simply returns success.
1895 * This driver re-initializes all registers after
1896 * pm_runtime_get_sync() anyway so there is no need
1897 * to save and restore registers here.
1902 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
1903 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
1904 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
1907 static struct platform_driver sh_mobile_ceu_driver
= {
1909 .name
= "sh_mobile_ceu",
1910 .pm
= &sh_mobile_ceu_dev_pm_ops
,
1912 .probe
= sh_mobile_ceu_probe
,
1913 .remove
= __devexit_p(sh_mobile_ceu_remove
),
1916 static int __init
sh_mobile_ceu_init(void)
1918 return platform_driver_register(&sh_mobile_ceu_driver
);
1921 static void __exit
sh_mobile_ceu_exit(void)
1923 platform_driver_unregister(&sh_mobile_ceu_driver
);
1926 module_init(sh_mobile_ceu_init
);
1927 module_exit(sh_mobile_ceu_exit
);
1929 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1930 MODULE_AUTHOR("Magnus Damm");
1931 MODULE_LICENSE("GPL");
1932 MODULE_ALIAS("platform:sh_mobile_ceu");