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
);
156 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev
*pcdev
)
159 struct soc_camera_device
*icd
= pcdev
->icd
;
161 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
163 /* wait CSTSR.CPTON bit */
164 for (i
= 0; i
< 1000; i
++) {
165 if (!(ceu_read(pcdev
, CSTSR
) & 1)) {
172 /* wait CAPSR.CPKIL bit */
173 for (i
= 0; i
< 1000; i
++) {
174 if (!(ceu_read(pcdev
, CAPSR
) & (1 << 16))) {
183 dev_warn(&icd
->dev
, "soft reset time out\n");
191 * Videobuf operations
193 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue
*vq
,
197 struct soc_camera_device
*icd
= vq
->priv_data
;
198 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
199 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
200 int bytes_per_pixel
= (icd
->current_fmt
->depth
+ 7) >> 3;
202 *size
= PAGE_ALIGN(icd
->user_width
* icd
->user_height
*
208 if (pcdev
->video_limit
) {
209 while (*size
* *count
> pcdev
->video_limit
)
213 dev_dbg(icd
->dev
.parent
, "count=%d, size=%d\n", *count
, *size
);
218 static void free_buffer(struct videobuf_queue
*vq
,
219 struct sh_mobile_ceu_buffer
*buf
)
221 struct soc_camera_device
*icd
= vq
->priv_data
;
222 struct device
*dev
= icd
->dev
.parent
;
224 dev_dbg(dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
225 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
230 videobuf_waiton(&buf
->vb
, 0, 0);
231 videobuf_dma_contig_free(vq
, &buf
->vb
);
232 dev_dbg(dev
, "%s freed\n", __func__
);
233 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
236 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
237 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
238 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
239 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
240 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
241 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
245 * return value doesn't reflex the success/failure to queue the new buffer,
246 * but rather the status of the previous buffer.
248 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
250 struct soc_camera_device
*icd
= pcdev
->icd
;
251 dma_addr_t phys_addr_top
, phys_addr_bottom
;
255 /* The hardware is _very_ picky about this sequence. Especially
256 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
257 * several not-so-well documented interrupt sources in CETCR.
259 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_MASK
);
260 status
= ceu_read(pcdev
, CETCR
);
261 ceu_write(pcdev
, CETCR
, ~status
& CEU_CETCR_MAGIC
);
262 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_MASK
);
263 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
264 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
267 * When a VBP interrupt occurs, a capture end interrupt does not occur
268 * and the image of that frame is not captured correctly. So, soft reset
271 if (status
& CEU_CEIER_VBP
) {
272 sh_mobile_ceu_soft_reset(pcdev
);
279 phys_addr_top
= videobuf_to_dma_contig(pcdev
->active
);
280 ceu_write(pcdev
, CDAYR
, phys_addr_top
);
281 if (pcdev
->is_interlaced
) {
282 phys_addr_bottom
= phys_addr_top
+ icd
->user_width
;
283 ceu_write(pcdev
, CDBYR
, phys_addr_bottom
);
286 switch (icd
->current_fmt
->fourcc
) {
287 case V4L2_PIX_FMT_NV12
:
288 case V4L2_PIX_FMT_NV21
:
289 case V4L2_PIX_FMT_NV16
:
290 case V4L2_PIX_FMT_NV61
:
291 phys_addr_top
+= icd
->user_width
*
293 ceu_write(pcdev
, CDACR
, phys_addr_top
);
294 if (pcdev
->is_interlaced
) {
295 phys_addr_bottom
= phys_addr_top
+
297 ceu_write(pcdev
, CDBCR
, phys_addr_bottom
);
301 pcdev
->active
->state
= VIDEOBUF_ACTIVE
;
302 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
307 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue
*vq
,
308 struct videobuf_buffer
*vb
,
309 enum v4l2_field field
)
311 struct soc_camera_device
*icd
= vq
->priv_data
;
312 struct sh_mobile_ceu_buffer
*buf
;
315 buf
= container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
317 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
318 vb
, vb
->baddr
, vb
->bsize
);
320 /* Added list head initialization on alloc */
321 WARN_ON(!list_empty(&vb
->queue
));
324 /* This can be useful if you want to see if we actually fill
325 * the buffer with something */
326 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
329 BUG_ON(NULL
== icd
->current_fmt
);
331 if (buf
->fmt
!= icd
->current_fmt
||
332 vb
->width
!= icd
->user_width
||
333 vb
->height
!= icd
->user_height
||
334 vb
->field
!= field
) {
335 buf
->fmt
= icd
->current_fmt
;
336 vb
->width
= icd
->user_width
;
337 vb
->height
= icd
->user_height
;
339 vb
->state
= VIDEOBUF_NEEDS_INIT
;
342 vb
->size
= vb
->width
* vb
->height
* ((buf
->fmt
->depth
+ 7) >> 3);
343 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
348 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
349 ret
= videobuf_iolock(vq
, vb
, NULL
);
352 vb
->state
= VIDEOBUF_PREPARED
;
357 free_buffer(vq
, buf
);
362 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
363 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue
*vq
,
364 struct videobuf_buffer
*vb
)
366 struct soc_camera_device
*icd
= vq
->priv_data
;
367 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
368 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
370 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
371 vb
, vb
->baddr
, vb
->bsize
);
373 vb
->state
= VIDEOBUF_QUEUED
;
374 list_add_tail(&vb
->queue
, &pcdev
->capture
);
376 if (!pcdev
->active
) {
378 * Because there were no active buffer at this moment,
379 * we are not interested in the return value of
380 * sh_mobile_ceu_capture here.
383 sh_mobile_ceu_capture(pcdev
);
387 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue
*vq
,
388 struct videobuf_buffer
*vb
)
390 struct soc_camera_device
*icd
= vq
->priv_data
;
391 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
392 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
395 spin_lock_irqsave(&pcdev
->lock
, flags
);
397 if (pcdev
->active
== vb
) {
398 /* disable capture (release DMA buffer), reset */
399 ceu_write(pcdev
, CAPSR
, 1 << 16);
400 pcdev
->active
= NULL
;
403 if ((vb
->state
== VIDEOBUF_ACTIVE
|| vb
->state
== VIDEOBUF_QUEUED
) &&
404 !list_empty(&vb
->queue
)) {
405 vb
->state
= VIDEOBUF_ERROR
;
406 list_del_init(&vb
->queue
);
409 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
411 free_buffer(vq
, container_of(vb
, struct sh_mobile_ceu_buffer
, vb
));
414 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops
= {
415 .buf_setup
= sh_mobile_ceu_videobuf_setup
,
416 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
417 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
418 .buf_release
= sh_mobile_ceu_videobuf_release
,
421 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
423 struct sh_mobile_ceu_dev
*pcdev
= data
;
424 struct videobuf_buffer
*vb
;
427 spin_lock_irqsave(&pcdev
->lock
, flags
);
431 /* Stale interrupt from a released buffer */
434 list_del_init(&vb
->queue
);
436 if (!list_empty(&pcdev
->capture
))
437 pcdev
->active
= list_entry(pcdev
->capture
.next
,
438 struct videobuf_buffer
, queue
);
440 pcdev
->active
= NULL
;
442 vb
->state
= (sh_mobile_ceu_capture(pcdev
) < 0) ?
443 VIDEOBUF_ERROR
: VIDEOBUF_DONE
;
444 do_gettimeofday(&vb
->ts
);
449 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
454 /* Called with .video_lock held */
455 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
457 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
458 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
463 dev_info(icd
->dev
.parent
,
464 "SuperH Mobile CEU driver attached to camera %d\n",
467 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
471 return sh_mobile_ceu_soft_reset(pcdev
);
474 /* Called with .video_lock held */
475 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
477 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
478 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
481 BUG_ON(icd
!= pcdev
->icd
);
483 /* disable capture, disable interrupts */
484 ceu_write(pcdev
, CEIER
, 0);
485 sh_mobile_ceu_soft_reset(pcdev
);
487 /* make sure active buffer is canceled */
488 spin_lock_irqsave(&pcdev
->lock
, flags
);
490 list_del(&pcdev
->active
->queue
);
491 pcdev
->active
->state
= VIDEOBUF_ERROR
;
492 wake_up_all(&pcdev
->active
->done
);
493 pcdev
->active
= NULL
;
495 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
497 pm_runtime_put_sync(ici
->v4l2_dev
.dev
);
499 dev_info(icd
->dev
.parent
,
500 "SuperH Mobile CEU driver detached from camera %d\n",
507 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
508 * in SH7722 Hardware Manual
510 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
512 unsigned int mant_pre
= scale
>> 12;
515 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
516 mant_pre
* 4096 / scale
+ 1;
519 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
526 scale
= (src
* 4096 / *dst
) & ~7;
528 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
531 *dst
= size_dst(src
, scale
);
536 /* rect is guaranteed to not exceed the scaled camera rectangle */
537 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
,
538 unsigned int out_width
,
539 unsigned int out_height
)
541 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
542 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
543 struct v4l2_rect
*rect
= &cam
->ceu_rect
;
544 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
545 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
546 unsigned int left_offset
, top_offset
;
549 dev_dbg(icd
->dev
.parent
, "Crop %ux%u@%u:%u\n",
550 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
552 left_offset
= rect
->left
;
553 top_offset
= rect
->top
;
555 if (pcdev
->image_mode
) {
556 in_width
= rect
->width
;
557 if (!pcdev
->is_16bit
) {
561 width
= cdwdr_width
= out_width
;
563 unsigned int w_factor
= (icd
->current_fmt
->depth
+ 7) >> 3;
565 width
= out_width
* w_factor
/ 2;
567 if (!pcdev
->is_16bit
)
570 in_width
= rect
->width
* w_factor
/ 2;
571 left_offset
= left_offset
* w_factor
/ 2;
573 cdwdr_width
= width
* 2;
577 in_height
= rect
->height
;
578 if (pcdev
->is_interlaced
) {
585 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
586 camor
= left_offset
| (top_offset
<< 16);
588 dev_geo(icd
->dev
.parent
,
589 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
590 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
593 ceu_write(pcdev
, CAMOR
, camor
);
594 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
595 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
596 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
599 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
601 u32 capsr
= ceu_read(pcdev
, CAPSR
);
602 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
606 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
608 unsigned long timeout
= jiffies
+ 10 * HZ
;
611 * Wait until the end of the current frame. It can take a long time,
612 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
614 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
617 if (time_after(jiffies
, timeout
)) {
618 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
619 "Timeout waiting for frame end! Interface problem?\n");
623 /* Wait until reset clears, this shall not hang... */
624 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
627 /* Anything to restore? */
628 if (capsr
& ~(1 << 16))
629 ceu_write(pcdev
, CAPSR
, capsr
);
632 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
,
635 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
636 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
638 unsigned long camera_flags
, common_flags
, value
;
640 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
641 u32 capsr
= capture_save_reset(pcdev
);
643 camera_flags
= icd
->ops
->query_bus_param(icd
);
644 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
645 make_bus_param(pcdev
));
649 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
653 switch (common_flags
& SOCAM_DATAWIDTH_MASK
) {
654 case SOCAM_DATAWIDTH_8
:
657 case SOCAM_DATAWIDTH_16
:
664 ceu_write(pcdev
, CRCNTR
, 0);
665 ceu_write(pcdev
, CRCMPR
, 0);
667 value
= 0x00000010; /* data fetch by default */
670 switch (icd
->current_fmt
->fourcc
) {
671 case V4L2_PIX_FMT_NV12
:
672 case V4L2_PIX_FMT_NV21
:
673 yuv_lineskip
= 1; /* skip for NV12/21, no skip for NV16/61 */
675 case V4L2_PIX_FMT_NV16
:
676 case V4L2_PIX_FMT_NV61
:
677 switch (cam
->camera_fmt
->fourcc
) {
678 case V4L2_PIX_FMT_UYVY
:
679 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
681 case V4L2_PIX_FMT_VYUY
:
682 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
684 case V4L2_PIX_FMT_YUYV
:
685 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
687 case V4L2_PIX_FMT_YVYU
:
688 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
695 if (icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
696 icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
697 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
699 value
|= common_flags
& SOCAM_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
700 value
|= common_flags
& SOCAM_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
701 value
|= pcdev
->is_16bit
? 1 << 12 : 0;
702 ceu_write(pcdev
, CAMCR
, value
);
704 ceu_write(pcdev
, CAPCR
, 0x00300000);
705 ceu_write(pcdev
, CAIFR
, pcdev
->is_interlaced
? 0x101 : 0);
707 sh_mobile_ceu_set_rect(icd
, icd
->user_width
, icd
->user_height
);
710 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
712 /* A few words about byte order (observed in Big Endian mode)
714 * In data fetch mode bytes are received in chunks of 8 bytes.
715 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
717 * The data is however by default written to memory in reverse order:
718 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
720 * The lowest three bits of CDOCR allows us to do swapping,
721 * using 7 we swap the data bytes to match the incoming order:
722 * D0, D1, D2, D3, D4, D5, D6, D7
726 value
&= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
728 ceu_write(pcdev
, CDOCR
, value
);
729 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
731 dev_dbg(icd
->dev
.parent
, "S_FMT successful for %c%c%c%c %ux%u\n",
732 pixfmt
& 0xff, (pixfmt
>> 8) & 0xff,
733 (pixfmt
>> 16) & 0xff, (pixfmt
>> 24) & 0xff,
734 icd
->user_width
, icd
->user_height
);
736 capture_restore(pcdev
, capsr
);
738 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
742 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
)
744 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
745 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
746 unsigned long camera_flags
, common_flags
;
748 camera_flags
= icd
->ops
->query_bus_param(icd
);
749 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
750 make_bus_param(pcdev
));
757 static const struct soc_camera_data_format sh_mobile_ceu_formats
[] = {
761 .fourcc
= V4L2_PIX_FMT_NV12
,
762 .colorspace
= V4L2_COLORSPACE_JPEG
,
767 .fourcc
= V4L2_PIX_FMT_NV21
,
768 .colorspace
= V4L2_COLORSPACE_JPEG
,
773 .fourcc
= V4L2_PIX_FMT_NV16
,
774 .colorspace
= V4L2_COLORSPACE_JPEG
,
779 .fourcc
= V4L2_PIX_FMT_NV61
,
780 .colorspace
= V4L2_COLORSPACE_JPEG
,
784 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, int idx
,
785 struct soc_camera_format_xlate
*xlate
)
787 struct device
*dev
= icd
->dev
.parent
;
790 struct sh_mobile_ceu_cam
*cam
;
792 ret
= sh_mobile_ceu_try_bus_param(icd
);
796 if (!icd
->host_priv
) {
797 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
801 icd
->host_priv
= cam
;
803 cam
= icd
->host_priv
;
806 /* Beginning of a pass */
808 cam
->extra_fmt
= NULL
;
810 switch (icd
->formats
[idx
].fourcc
) {
811 case V4L2_PIX_FMT_UYVY
:
812 case V4L2_PIX_FMT_VYUY
:
813 case V4L2_PIX_FMT_YUYV
:
814 case V4L2_PIX_FMT_YVYU
:
816 goto add_single_format
;
819 * Our case is simple so far: for any of the above four camera
820 * formats we add all our four synthesized NV* formats, so,
821 * just marking the device with a single flag suffices. If
822 * the format generation rules are more complex, you would have
823 * to actually hang your already added / counted formats onto
824 * the host_priv pointer and check whether the format you're
825 * going to add now is already there.
827 cam
->extra_fmt
= (void *)sh_mobile_ceu_formats
;
829 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
831 for (k
= 0; xlate
&& k
< n
; k
++) {
832 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
833 xlate
->cam_fmt
= icd
->formats
+ idx
;
834 xlate
->buswidth
= icd
->formats
[idx
].depth
;
836 dev_dbg(dev
, "Providing format %s using %s\n",
837 sh_mobile_ceu_formats
[k
].name
,
838 icd
->formats
[idx
].name
);
842 /* Generic pass-through */
845 xlate
->host_fmt
= icd
->formats
+ idx
;
846 xlate
->cam_fmt
= icd
->formats
+ idx
;
847 xlate
->buswidth
= icd
->formats
[idx
].depth
;
850 "Providing format %s in pass-through mode\n",
851 icd
->formats
[idx
].name
);
858 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
860 kfree(icd
->host_priv
);
861 icd
->host_priv
= NULL
;
864 /* Check if any dimension of r1 is smaller than respective one of r2 */
865 static bool is_smaller(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
867 return r1
->width
< r2
->width
|| r1
->height
< r2
->height
;
870 /* Check if r1 fails to cover r2 */
871 static bool is_inside(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
873 return r1
->left
> r2
->left
|| r1
->top
> r2
->top
||
874 r1
->left
+ r1
->width
< r2
->left
+ r2
->width
||
875 r1
->top
+ r1
->height
< r2
->top
+ r2
->height
;
878 static unsigned int scale_down(unsigned int size
, unsigned int scale
)
880 return (size
* 4096 + scale
/ 2) / scale
;
883 static unsigned int scale_up(unsigned int size
, unsigned int scale
)
885 return (size
* scale
+ 2048) / 4096;
888 static unsigned int calc_generic_scale(unsigned int input
, unsigned int output
)
890 return (input
* 4096 + output
/ 2) / output
;
893 static int client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
)
895 struct v4l2_crop crop
;
896 struct v4l2_cropcap cap
;
899 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
901 ret
= v4l2_subdev_call(sd
, video
, g_crop
, &crop
);
907 /* Camera driver doesn't support .g_crop(), assume default rectangle */
908 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
910 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
920 * The common for both scaling and cropping iterative approach is:
921 * 1. try if the client can produce exactly what requested by the user
922 * 2. if (1) failed, try to double the client image until we get one big enough
923 * 3. if (2) failed, try to request the maximum image
925 static int client_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*crop
,
926 struct v4l2_crop
*cam_crop
)
928 struct v4l2_rect
*rect
= &crop
->c
, *cam_rect
= &cam_crop
->c
;
929 struct device
*dev
= sd
->v4l2_dev
->dev
;
930 struct v4l2_cropcap cap
;
932 unsigned int width
, height
;
934 v4l2_subdev_call(sd
, video
, s_crop
, crop
);
935 ret
= client_g_rect(sd
, cam_rect
);
940 * Now cam_crop contains the current camera input rectangle, and it must
941 * be within camera cropcap bounds
943 if (!memcmp(rect
, cam_rect
, sizeof(*rect
))) {
944 /* Even if camera S_CROP failed, but camera rectangle matches */
945 dev_dbg(dev
, "Camera S_CROP successful for %ux%u@%u:%u\n",
946 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
950 /* Try to fix cropping, that camera hasn't managed to set */
951 dev_geo(dev
, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
952 cam_rect
->width
, cam_rect
->height
,
953 cam_rect
->left
, cam_rect
->top
,
954 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
956 /* We need sensor maximum rectangle */
957 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
961 soc_camera_limit_side(&rect
->left
, &rect
->width
, cap
.bounds
.left
, 2,
963 soc_camera_limit_side(&rect
->top
, &rect
->height
, cap
.bounds
.top
, 4,
967 * Popular special case - some cameras can only handle fixed sizes like
968 * QVGA, VGA,... Take care to avoid infinite loop.
970 width
= max(cam_rect
->width
, 2);
971 height
= max(cam_rect
->height
, 2);
973 while (!ret
&& (is_smaller(cam_rect
, rect
) ||
974 is_inside(cam_rect
, rect
)) &&
975 (cap
.bounds
.width
> width
|| cap
.bounds
.height
> height
)) {
980 cam_rect
->width
= width
;
981 cam_rect
->height
= height
;
984 * We do not know what capabilities the camera has to set up
985 * left and top borders. We could try to be smarter in iterating
986 * them, e.g., if camera current left is to the right of the
987 * target left, set it to the middle point between the current
988 * left and minimum left. But that would add too much
989 * complexity: we would have to iterate each border separately.
991 if (cam_rect
->left
> rect
->left
)
992 cam_rect
->left
= cap
.bounds
.left
;
994 if (cam_rect
->left
+ cam_rect
->width
< rect
->left
+ rect
->width
)
995 cam_rect
->width
= rect
->left
+ rect
->width
-
998 if (cam_rect
->top
> rect
->top
)
999 cam_rect
->top
= cap
.bounds
.top
;
1001 if (cam_rect
->top
+ cam_rect
->height
< rect
->top
+ rect
->height
)
1002 cam_rect
->height
= rect
->top
+ rect
->height
-
1005 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1006 ret
= client_g_rect(sd
, cam_rect
);
1007 dev_geo(dev
, "Camera S_CROP %d for %ux%u@%u:%u\n", ret
,
1008 cam_rect
->width
, cam_rect
->height
,
1009 cam_rect
->left
, cam_rect
->top
);
1012 /* S_CROP must not modify the rectangle */
1013 if (is_smaller(cam_rect
, rect
) || is_inside(cam_rect
, rect
)) {
1015 * The camera failed to configure a suitable cropping,
1016 * we cannot use the current rectangle, set to max
1018 *cam_rect
= cap
.bounds
;
1019 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1020 ret
= client_g_rect(sd
, cam_rect
);
1021 dev_geo(dev
, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret
,
1022 cam_rect
->width
, cam_rect
->height
,
1023 cam_rect
->left
, cam_rect
->top
);
1029 static int get_camera_scales(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
,
1030 unsigned int *scale_h
, unsigned int *scale_v
)
1032 struct v4l2_format f
;
1035 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1037 ret
= v4l2_subdev_call(sd
, video
, g_fmt
, &f
);
1041 *scale_h
= calc_generic_scale(rect
->width
, f
.fmt
.pix
.width
);
1042 *scale_v
= calc_generic_scale(rect
->height
, f
.fmt
.pix
.height
);
1047 static int get_camera_subwin(struct soc_camera_device
*icd
,
1048 struct v4l2_rect
*cam_subrect
,
1049 unsigned int cam_hscale
, unsigned int cam_vscale
)
1051 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1052 struct v4l2_rect
*ceu_rect
= &cam
->ceu_rect
;
1054 if (!ceu_rect
->width
) {
1055 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1056 struct device
*dev
= icd
->dev
.parent
;
1057 struct v4l2_format f
;
1058 struct v4l2_pix_format
*pix
= &f
.fmt
.pix
;
1062 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1064 ret
= v4l2_subdev_call(sd
, video
, g_fmt
, &f
);
1068 dev_geo(dev
, "camera fmt %ux%u\n", pix
->width
, pix
->height
);
1070 if (pix
->width
> 2560) {
1071 ceu_rect
->width
= 2560;
1072 ceu_rect
->left
= (pix
->width
- 2560) / 2;
1074 ceu_rect
->width
= pix
->width
;
1078 if (pix
->height
> 1920) {
1079 ceu_rect
->height
= 1920;
1080 ceu_rect
->top
= (pix
->height
- 1920) / 2;
1082 ceu_rect
->height
= pix
->height
;
1086 dev_geo(dev
, "initialised CEU rect %ux%u@%u:%u\n",
1087 ceu_rect
->width
, ceu_rect
->height
,
1088 ceu_rect
->left
, ceu_rect
->top
);
1091 cam_subrect
->width
= scale_up(ceu_rect
->width
, cam_hscale
);
1092 cam_subrect
->left
= scale_up(ceu_rect
->left
, cam_hscale
);
1093 cam_subrect
->height
= scale_up(ceu_rect
->height
, cam_vscale
);
1094 cam_subrect
->top
= scale_up(ceu_rect
->top
, cam_vscale
);
1099 static int client_s_fmt(struct soc_camera_device
*icd
, struct v4l2_format
*f
,
1102 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1103 struct device
*dev
= icd
->dev
.parent
;
1104 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1105 unsigned int width
= pix
->width
, height
= pix
->height
, tmp_w
, tmp_h
;
1106 unsigned int max_width
, max_height
;
1107 struct v4l2_cropcap cap
;
1110 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1112 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1116 max_width
= min(cap
.bounds
.width
, 2560);
1117 max_height
= min(cap
.bounds
.height
, 1920);
1119 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, f
);
1123 dev_geo(dev
, "camera scaled to %ux%u\n", pix
->width
, pix
->height
);
1125 if ((width
== pix
->width
&& height
== pix
->height
) || !ceu_can_scale
)
1128 /* Camera set a format, but geometry is not precise, try to improve */
1130 tmp_h
= pix
->height
;
1132 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1133 while ((width
> tmp_w
|| height
> tmp_h
) &&
1134 tmp_w
< max_width
&& tmp_h
< max_height
) {
1135 tmp_w
= min(2 * tmp_w
, max_width
);
1136 tmp_h
= min(2 * tmp_h
, max_height
);
1138 pix
->height
= tmp_h
;
1139 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, f
);
1140 dev_geo(dev
, "Camera scaled to %ux%u\n",
1141 pix
->width
, pix
->height
);
1143 /* This shouldn't happen */
1144 dev_err(dev
, "Client failed to set format: %d\n", ret
);
1153 * @rect - camera cropped rectangle
1154 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1155 * @ceu_rect - on output calculated CEU crop rectangle
1157 static int client_scale(struct soc_camera_device
*icd
, struct v4l2_rect
*rect
,
1158 struct v4l2_rect
*sub_rect
, struct v4l2_rect
*ceu_rect
,
1159 struct v4l2_format
*f
, bool ceu_can_scale
)
1161 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1162 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1163 struct device
*dev
= icd
->dev
.parent
;
1164 struct v4l2_format f_tmp
= *f
;
1165 struct v4l2_pix_format
*pix_tmp
= &f_tmp
.fmt
.pix
;
1166 unsigned int scale_h
, scale_v
;
1169 /* 5. Apply iterative camera S_FMT for camera user window. */
1170 ret
= client_s_fmt(icd
, &f_tmp
, ceu_can_scale
);
1174 dev_geo(dev
, "5: camera scaled to %ux%u\n",
1175 pix_tmp
->width
, pix_tmp
->height
);
1177 /* 6. Retrieve camera output window (g_fmt) */
1179 /* unneeded - it is already in "f_tmp" */
1181 /* 7. Calculate new camera scales. */
1182 ret
= get_camera_scales(sd
, rect
, &scale_h
, &scale_v
);
1186 dev_geo(dev
, "7: camera scales %u:%u\n", scale_h
, scale_v
);
1188 cam
->cam_width
= pix_tmp
->width
;
1189 cam
->cam_height
= pix_tmp
->height
;
1190 f
->fmt
.pix
.width
= pix_tmp
->width
;
1191 f
->fmt
.pix
.height
= pix_tmp
->height
;
1194 * 8. Calculate new CEU crop - apply camera scales to previously
1195 * calculated "effective" crop.
1197 ceu_rect
->left
= scale_down(sub_rect
->left
, scale_h
);
1198 ceu_rect
->width
= scale_down(sub_rect
->width
, scale_h
);
1199 ceu_rect
->top
= scale_down(sub_rect
->top
, scale_v
);
1200 ceu_rect
->height
= scale_down(sub_rect
->height
, scale_v
);
1202 dev_geo(dev
, "8: new CEU rect %ux%u@%u:%u\n",
1203 ceu_rect
->width
, ceu_rect
->height
,
1204 ceu_rect
->left
, ceu_rect
->top
);
1209 /* Get combined scales */
1210 static int get_scales(struct soc_camera_device
*icd
,
1211 unsigned int *scale_h
, unsigned int *scale_v
)
1213 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1214 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1215 struct v4l2_crop cam_crop
;
1216 unsigned int width_in
, height_in
;
1219 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1221 ret
= client_g_rect(sd
, &cam_crop
.c
);
1225 ret
= get_camera_scales(sd
, &cam_crop
.c
, scale_h
, scale_v
);
1229 width_in
= scale_up(cam
->ceu_rect
.width
, *scale_h
);
1230 height_in
= scale_up(cam
->ceu_rect
.height
, *scale_v
);
1232 *scale_h
= calc_generic_scale(width_in
, icd
->user_width
);
1233 *scale_v
= calc_generic_scale(height_in
, icd
->user_height
);
1239 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1240 * framerate by always requesting the maximum image from the client. See
1241 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1242 * scaling and cropping algorithms and for the meaning of referenced here steps.
1244 static int sh_mobile_ceu_set_crop(struct soc_camera_device
*icd
,
1245 struct v4l2_crop
*a
)
1247 struct v4l2_rect
*rect
= &a
->c
;
1248 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1249 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1250 struct v4l2_crop cam_crop
;
1251 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1252 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, *ceu_rect
= &cam
->ceu_rect
;
1253 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1254 struct device
*dev
= icd
->dev
.parent
;
1255 struct v4l2_format f
;
1256 struct v4l2_pix_format
*pix
= &f
.fmt
.pix
;
1257 unsigned int scale_comb_h
, scale_comb_v
, scale_ceu_h
, scale_ceu_v
,
1258 out_width
, out_height
;
1262 /* 1. Calculate current combined scales. */
1263 ret
= get_scales(icd
, &scale_comb_h
, &scale_comb_v
);
1267 dev_geo(dev
, "1: combined scales %u:%u\n", scale_comb_h
, scale_comb_v
);
1269 /* 2. Apply iterative camera S_CROP for new input window. */
1270 ret
= client_s_crop(sd
, a
, &cam_crop
);
1274 dev_geo(dev
, "2: camera cropped to %ux%u@%u:%u\n",
1275 cam_rect
->width
, cam_rect
->height
,
1276 cam_rect
->left
, cam_rect
->top
);
1278 /* On success cam_crop contains current camera crop */
1281 * 3. If old combined scales applied to new crop produce an impossible
1282 * user window, adjust scales to produce nearest possible window.
1284 out_width
= scale_down(rect
->width
, scale_comb_h
);
1285 out_height
= scale_down(rect
->height
, scale_comb_v
);
1287 if (out_width
> 2560)
1289 else if (out_width
< 2)
1292 if (out_height
> 1920)
1294 else if (out_height
< 4)
1297 dev_geo(dev
, "3: Adjusted output %ux%u\n", out_width
, out_height
);
1299 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1302 * 5. Using actual input window and calculated combined scales calculate
1303 * camera target output window.
1305 pix
->width
= scale_down(cam_rect
->width
, scale_comb_h
);
1306 pix
->height
= scale_down(cam_rect
->height
, scale_comb_v
);
1308 dev_geo(dev
, "5: camera target %ux%u\n", pix
->width
, pix
->height
);
1311 pix
->pixelformat
= cam
->camera_fmt
->fourcc
;
1312 pix
->colorspace
= cam
->camera_fmt
->colorspace
;
1314 capsr
= capture_save_reset(pcdev
);
1315 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1317 /* Make relative to camera rectangle */
1318 rect
->left
-= cam_rect
->left
;
1319 rect
->top
-= cam_rect
->top
;
1321 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1323 ret
= client_scale(icd
, cam_rect
, rect
, ceu_rect
, &f
,
1324 pcdev
->image_mode
&& !pcdev
->is_interlaced
);
1326 dev_geo(dev
, "6-9: %d\n", ret
);
1328 /* 10. Use CEU cropping to crop to the new window. */
1329 sh_mobile_ceu_set_rect(icd
, out_width
, out_height
);
1331 dev_geo(dev
, "10: CEU cropped to %ux%u@%u:%u\n",
1332 ceu_rect
->width
, ceu_rect
->height
,
1333 ceu_rect
->left
, ceu_rect
->top
);
1336 * 11. Calculate CEU scales from camera scales from results of (10) and
1337 * user window from (3)
1339 scale_ceu_h
= calc_scale(ceu_rect
->width
, &out_width
);
1340 scale_ceu_v
= calc_scale(ceu_rect
->height
, &out_height
);
1342 dev_geo(dev
, "11: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1344 /* 12. Apply CEU scales. */
1345 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1346 if (cflcr
!= pcdev
->cflcr
) {
1347 pcdev
->cflcr
= cflcr
;
1348 ceu_write(pcdev
, CFLCR
, cflcr
);
1351 /* Restore capture */
1354 capture_restore(pcdev
, capsr
);
1356 icd
->user_width
= out_width
;
1357 icd
->user_height
= out_height
;
1359 /* Even if only camera cropping succeeded */
1363 /* Similar to set_crop multistage iterative algorithm */
1364 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1365 struct v4l2_format
*f
)
1367 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1368 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1369 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1370 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1371 struct v4l2_format cam_f
= *f
;
1372 struct v4l2_pix_format
*cam_pix
= &cam_f
.fmt
.pix
;
1373 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1374 struct device
*dev
= icd
->dev
.parent
;
1375 __u32 pixfmt
= pix
->pixelformat
;
1376 const struct soc_camera_format_xlate
*xlate
;
1377 struct v4l2_crop cam_crop
;
1378 struct v4l2_rect
*cam_rect
= &cam_crop
.c
, cam_subrect
, ceu_rect
;
1379 unsigned int scale_cam_h
, scale_cam_v
;
1380 u16 scale_v
, scale_h
;
1382 bool is_interlaced
, image_mode
;
1384 switch (pix
->field
) {
1385 case V4L2_FIELD_INTERLACED
:
1386 is_interlaced
= true;
1388 case V4L2_FIELD_ANY
:
1390 pix
->field
= V4L2_FIELD_NONE
;
1392 case V4L2_FIELD_NONE
:
1393 is_interlaced
= false;
1397 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1399 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1403 /* 1. Calculate current camera scales. */
1404 cam_crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1406 ret
= client_g_rect(sd
, cam_rect
);
1410 ret
= get_camera_scales(sd
, cam_rect
, &scale_cam_h
, &scale_cam_v
);
1414 dev_geo(dev
, "1: camera scales %u:%u\n", scale_cam_h
, scale_cam_v
);
1417 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1418 * scaled back at current camera scales onto input window.
1420 ret
= get_camera_subwin(icd
, &cam_subrect
, scale_cam_h
, scale_cam_v
);
1424 dev_geo(dev
, "2: subwin %ux%u@%u:%u\n",
1425 cam_subrect
.width
, cam_subrect
.height
,
1426 cam_subrect
.left
, cam_subrect
.top
);
1429 * 3. Calculate new combined scales from "effective" input window to
1430 * requested user window.
1432 scale_h
= calc_generic_scale(cam_subrect
.width
, pix
->width
);
1433 scale_v
= calc_generic_scale(cam_subrect
.height
, pix
->height
);
1435 dev_geo(dev
, "3: scales %u:%u\n", scale_h
, scale_v
);
1438 * 4. Calculate camera output window by applying combined scales to real
1441 cam_pix
->width
= scale_down(cam_rect
->width
, scale_h
);
1442 cam_pix
->height
= scale_down(cam_rect
->height
, scale_v
);
1443 cam_pix
->pixelformat
= xlate
->cam_fmt
->fourcc
;
1446 case V4L2_PIX_FMT_NV12
:
1447 case V4L2_PIX_FMT_NV21
:
1448 case V4L2_PIX_FMT_NV16
:
1449 case V4L2_PIX_FMT_NV61
:
1456 dev_geo(dev
, "4: camera output %ux%u\n",
1457 cam_pix
->width
, cam_pix
->height
);
1460 ret
= client_scale(icd
, cam_rect
, &cam_subrect
, &ceu_rect
, &cam_f
,
1461 image_mode
&& !is_interlaced
);
1463 dev_geo(dev
, "5-9: client scale %d\n", ret
);
1465 /* Done with the camera. Now see if we can improve the result */
1467 dev_dbg(dev
, "Camera %d fmt %ux%u, requested %ux%u\n",
1468 ret
, cam_pix
->width
, cam_pix
->height
, pix
->width
, pix
->height
);
1472 /* 10. Use CEU scaling to scale to the requested user window. */
1474 /* We cannot scale up */
1475 if (pix
->width
> cam_pix
->width
)
1476 pix
->width
= cam_pix
->width
;
1477 if (pix
->width
> ceu_rect
.width
)
1478 pix
->width
= ceu_rect
.width
;
1480 if (pix
->height
> cam_pix
->height
)
1481 pix
->height
= cam_pix
->height
;
1482 if (pix
->height
> ceu_rect
.height
)
1483 pix
->height
= ceu_rect
.height
;
1485 /* Let's rock: scale pix->{width x height} down to width x height */
1486 scale_h
= calc_scale(ceu_rect
.width
, &pix
->width
);
1487 scale_v
= calc_scale(ceu_rect
.height
, &pix
->height
);
1489 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1490 ceu_rect
.width
, scale_h
, pix
->width
,
1491 ceu_rect
.height
, scale_v
, pix
->height
);
1493 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1495 icd
->buswidth
= xlate
->buswidth
;
1496 icd
->current_fmt
= xlate
->host_fmt
;
1497 cam
->camera_fmt
= xlate
->cam_fmt
;
1498 cam
->ceu_rect
= ceu_rect
;
1500 pcdev
->is_interlaced
= is_interlaced
;
1501 pcdev
->image_mode
= image_mode
;
1506 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1507 struct v4l2_format
*f
)
1509 const struct soc_camera_format_xlate
*xlate
;
1510 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1511 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1512 __u32 pixfmt
= pix
->pixelformat
;
1516 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1518 dev_warn(icd
->dev
.parent
, "Format %x not found\n", pixfmt
);
1522 /* FIXME: calculate using depth and bus width */
1524 v4l_bound_align_image(&pix
->width
, 2, 2560, 1,
1525 &pix
->height
, 4, 1920, 2, 0);
1528 height
= pix
->height
;
1530 pix
->bytesperline
= pix
->width
*
1531 DIV_ROUND_UP(xlate
->host_fmt
->depth
, 8);
1532 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
1534 pix
->pixelformat
= xlate
->cam_fmt
->fourcc
;
1536 /* limit to sensor capabilities */
1537 ret
= v4l2_subdev_call(sd
, video
, try_fmt
, f
);
1538 pix
->pixelformat
= pixfmt
;
1543 case V4L2_PIX_FMT_NV12
:
1544 case V4L2_PIX_FMT_NV21
:
1545 case V4L2_PIX_FMT_NV16
:
1546 case V4L2_PIX_FMT_NV61
:
1547 /* FIXME: check against rect_max after converting soc-camera */
1548 /* We can scale precisely, need a bigger image from camera */
1549 if (pix
->width
< width
|| pix
->height
< height
) {
1550 int tmp_w
= pix
->width
, tmp_h
= pix
->height
;
1553 ret
= v4l2_subdev_call(sd
, video
, try_fmt
, f
);
1555 /* Shouldn't actually happen... */
1556 dev_err(icd
->dev
.parent
,
1557 "FIXME: try_fmt() returned %d\n", ret
);
1559 pix
->height
= tmp_h
;
1562 if (pix
->width
> width
)
1564 if (pix
->height
> height
)
1565 pix
->height
= height
;
1571 static int sh_mobile_ceu_reqbufs(struct soc_camera_file
*icf
,
1572 struct v4l2_requestbuffers
*p
)
1576 /* This is for locking debugging only. I removed spinlocks and now I
1577 * check whether .prepare is ever called on a linked buffer, or whether
1578 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1579 * it hadn't triggered */
1580 for (i
= 0; i
< p
->count
; i
++) {
1581 struct sh_mobile_ceu_buffer
*buf
;
1583 buf
= container_of(icf
->vb_vidq
.bufs
[i
],
1584 struct sh_mobile_ceu_buffer
, vb
);
1585 INIT_LIST_HEAD(&buf
->vb
.queue
);
1591 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1593 struct soc_camera_file
*icf
= file
->private_data
;
1594 struct sh_mobile_ceu_buffer
*buf
;
1596 buf
= list_entry(icf
->vb_vidq
.stream
.next
,
1597 struct sh_mobile_ceu_buffer
, vb
.stream
);
1599 poll_wait(file
, &buf
->vb
.done
, pt
);
1601 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1602 buf
->vb
.state
== VIDEOBUF_ERROR
)
1603 return POLLIN
|POLLRDNORM
;
1608 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1609 struct v4l2_capability
*cap
)
1611 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1612 cap
->version
= KERNEL_VERSION(0, 0, 5);
1613 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1617 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue
*q
,
1618 struct soc_camera_device
*icd
)
1620 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1621 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1623 videobuf_queue_dma_contig_init(q
,
1624 &sh_mobile_ceu_videobuf_ops
,
1625 icd
->dev
.parent
, &pcdev
->lock
,
1626 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1627 pcdev
->is_interlaced
?
1628 V4L2_FIELD_INTERLACED
: V4L2_FIELD_NONE
,
1629 sizeof(struct sh_mobile_ceu_buffer
),
1633 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device
*icd
,
1634 struct v4l2_control
*ctrl
)
1636 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1637 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1641 case V4L2_CID_SHARPNESS
:
1642 val
= ceu_read(pcdev
, CLFCR
);
1643 ctrl
->value
= val
^ 1;
1646 return -ENOIOCTLCMD
;
1649 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device
*icd
,
1650 struct v4l2_control
*ctrl
)
1652 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1653 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1656 case V4L2_CID_SHARPNESS
:
1657 switch (icd
->current_fmt
->fourcc
) {
1658 case V4L2_PIX_FMT_NV12
:
1659 case V4L2_PIX_FMT_NV21
:
1660 case V4L2_PIX_FMT_NV16
:
1661 case V4L2_PIX_FMT_NV61
:
1662 ceu_write(pcdev
, CLFCR
, !ctrl
->value
);
1667 return -ENOIOCTLCMD
;
1670 static const struct v4l2_queryctrl sh_mobile_ceu_controls
[] = {
1672 .id
= V4L2_CID_SHARPNESS
,
1673 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1674 .name
= "Low-pass filter",
1682 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1683 .owner
= THIS_MODULE
,
1684 .add
= sh_mobile_ceu_add_device
,
1685 .remove
= sh_mobile_ceu_remove_device
,
1686 .get_formats
= sh_mobile_ceu_get_formats
,
1687 .put_formats
= sh_mobile_ceu_put_formats
,
1688 .set_crop
= sh_mobile_ceu_set_crop
,
1689 .set_fmt
= sh_mobile_ceu_set_fmt
,
1690 .try_fmt
= sh_mobile_ceu_try_fmt
,
1691 .set_ctrl
= sh_mobile_ceu_set_ctrl
,
1692 .get_ctrl
= sh_mobile_ceu_get_ctrl
,
1693 .reqbufs
= sh_mobile_ceu_reqbufs
,
1694 .poll
= sh_mobile_ceu_poll
,
1695 .querycap
= sh_mobile_ceu_querycap
,
1696 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1697 .init_videobuf
= sh_mobile_ceu_init_videobuf
,
1698 .controls
= sh_mobile_ceu_controls
,
1699 .num_controls
= ARRAY_SIZE(sh_mobile_ceu_controls
),
1702 static int __devinit
sh_mobile_ceu_probe(struct platform_device
*pdev
)
1704 struct sh_mobile_ceu_dev
*pcdev
;
1705 struct resource
*res
;
1710 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1711 irq
= platform_get_irq(pdev
, 0);
1713 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1718 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
1720 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1725 INIT_LIST_HEAD(&pcdev
->capture
);
1726 spin_lock_init(&pcdev
->lock
);
1728 pcdev
->pdata
= pdev
->dev
.platform_data
;
1729 if (!pcdev
->pdata
) {
1731 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1735 base
= ioremap_nocache(res
->start
, resource_size(res
));
1738 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
1744 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1746 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1748 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
1752 DMA_MEMORY_EXCLUSIVE
);
1754 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
1759 pcdev
->video_limit
= resource_size(res
);
1763 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
1764 dev_name(&pdev
->dev
), pcdev
);
1766 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
1767 goto exit_release_mem
;
1770 pm_suspend_ignore_children(&pdev
->dev
, true);
1771 pm_runtime_enable(&pdev
->dev
);
1772 pm_runtime_resume(&pdev
->dev
);
1774 pcdev
->ici
.priv
= pcdev
;
1775 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
1776 pcdev
->ici
.nr
= pdev
->id
;
1777 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
1778 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
1780 err
= soc_camera_host_register(&pcdev
->ici
);
1787 pm_runtime_disable(&pdev
->dev
);
1788 free_irq(pcdev
->irq
, pcdev
);
1790 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1791 dma_release_declared_memory(&pdev
->dev
);
1800 static int __devexit
sh_mobile_ceu_remove(struct platform_device
*pdev
)
1802 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1803 struct sh_mobile_ceu_dev
*pcdev
= container_of(soc_host
,
1804 struct sh_mobile_ceu_dev
, ici
);
1806 soc_camera_host_unregister(soc_host
);
1807 pm_runtime_disable(&pdev
->dev
);
1808 free_irq(pcdev
->irq
, pcdev
);
1809 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1810 dma_release_declared_memory(&pdev
->dev
);
1811 iounmap(pcdev
->base
);
1816 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
1818 /* Runtime PM callback shared between ->runtime_suspend()
1819 * and ->runtime_resume(). Simply returns success.
1821 * This driver re-initializes all registers after
1822 * pm_runtime_get_sync() anyway so there is no need
1823 * to save and restore registers here.
1828 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
1829 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
1830 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
1833 static struct platform_driver sh_mobile_ceu_driver
= {
1835 .name
= "sh_mobile_ceu",
1836 .pm
= &sh_mobile_ceu_dev_pm_ops
,
1838 .probe
= sh_mobile_ceu_probe
,
1839 .remove
= __exit_p(sh_mobile_ceu_remove
),
1842 static int __init
sh_mobile_ceu_init(void)
1844 return platform_driver_register(&sh_mobile_ceu_driver
);
1847 static void __exit
sh_mobile_ceu_exit(void)
1849 platform_driver_unregister(&sh_mobile_ceu_driver
);
1852 module_init(sh_mobile_ceu_init
);
1853 module_exit(sh_mobile_ceu_exit
);
1855 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1856 MODULE_AUTHOR("Magnus Damm");
1857 MODULE_LICENSE("GPL");
1858 MODULE_ALIAS("platform:sh_mobile_ceu");