1 // SPDX-License-Identifier: GPL-2.0+
3 * V4L2 Driver for SuperH Mobile CEU interface
5 * Copyright (C) 2008 Magnus Damm
7 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
9 * Copyright (C) 2006, Sascha Hauer, Pengutronix
10 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/errno.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
25 #include <linux/moduleparam.h>
27 #include <linux/time.h>
28 #include <linux/slab.h>
29 #include <linux/device.h>
30 #include <linux/platform_device.h>
31 #include <linux/videodev2.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/sched.h>
35 #include <media/v4l2-async.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-dev.h>
38 #include <media/soc_camera.h>
39 #include <media/drv-intf/sh_mobile_ceu.h>
40 #include <media/videobuf2-dma-contig.h>
41 #include <media/v4l2-mediabus.h>
42 #include <media/drv-intf/soc_mediabus.h>
44 #include "soc_scale_crop.h"
46 /* register offsets for sh7722 / sh7723 */
48 #define CAPSR 0x00 /* Capture start register */
49 #define CAPCR 0x04 /* Capture control register */
50 #define CAMCR 0x08 /* Capture interface control register */
51 #define CMCYR 0x0c /* Capture interface cycle register */
52 #define CAMOR 0x10 /* Capture interface offset register */
53 #define CAPWR 0x14 /* Capture interface width register */
54 #define CAIFR 0x18 /* Capture interface input format register */
55 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
56 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
57 #define CRCNTR 0x28 /* CEU register control register */
58 #define CRCMPR 0x2c /* CEU register forcible control register */
59 #define CFLCR 0x30 /* Capture filter control register */
60 #define CFSZR 0x34 /* Capture filter size clip register */
61 #define CDWDR 0x38 /* Capture destination width register */
62 #define CDAYR 0x3c /* Capture data address Y register */
63 #define CDACR 0x40 /* Capture data address C register */
64 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
65 #define CDBCR 0x48 /* Capture data bottom-field address C register */
66 #define CBDSR 0x4c /* Capture bundle destination size register */
67 #define CFWCR 0x5c /* Firewall operation control register */
68 #define CLFCR 0x60 /* Capture low-pass filter control register */
69 #define CDOCR 0x64 /* Capture data output control register */
70 #define CDDCR 0x68 /* Capture data complexity level register */
71 #define CDDAR 0x6c /* Capture data complexity level address register */
72 #define CEIER 0x70 /* Capture event interrupt enable register */
73 #define CETCR 0x74 /* Capture event flag clear register */
74 #define CSTSR 0x7c /* Capture status register */
75 #define CSRTR 0x80 /* Capture software reset register */
76 #define CDSSR 0x84 /* Capture data size register */
77 #define CDAYR2 0x90 /* Capture data address Y register 2 */
78 #define CDACR2 0x94 /* Capture data address C register 2 */
79 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
80 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
84 #define dev_geo dev_info
86 #define dev_geo dev_dbg
89 /* per video frame buffer */
90 struct sh_mobile_ceu_buffer
{
91 struct vb2_v4l2_buffer vb
; /* v4l buffer must be first */
92 struct list_head queue
;
95 struct sh_mobile_ceu_dev
{
96 struct soc_camera_host ici
;
103 spinlock_t lock
; /* Protects video buffer lists */
104 struct list_head capture
;
105 struct vb2_v4l2_buffer
*active
;
107 struct sh_mobile_ceu_info
*pdata
;
108 struct completion complete
;
112 /* static max sizes either from platform data or default */
116 enum v4l2_field field
;
120 unsigned int image_mode
:1;
121 unsigned int is_16bit
:1;
122 unsigned int frozen
:1;
125 struct sh_mobile_ceu_cam
{
126 /* CEU offsets within the camera output, before the CEU scaler */
127 unsigned int ceu_left
;
128 unsigned int ceu_top
;
129 /* Client output, as seen by the CEU */
133 * User window from S_SELECTION / G_SELECTION, produced by client cropping and
134 * scaling, CEU scaling and CEU cropping, mapped back onto the client
137 struct v4l2_rect subrect
;
138 /* Camera cropping rectangle */
139 struct v4l2_rect rect
;
140 const struct soc_mbus_pixelfmt
*extra_fmt
;
144 static struct sh_mobile_ceu_buffer
*to_ceu_vb(struct vb2_v4l2_buffer
*vbuf
)
146 return container_of(vbuf
, struct sh_mobile_ceu_buffer
, vb
);
149 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
150 unsigned long reg_offs
, u32 data
)
152 iowrite32(data
, priv
->base
+ reg_offs
);
155 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
157 return ioread32(priv
->base
+ reg_offs
);
160 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev
*pcdev
)
164 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
166 /* wait CSTSR.CPTON bit */
167 for (i
= 0; i
< 1000; i
++) {
168 if (!(ceu_read(pcdev
, CSTSR
) & 1)) {
175 /* wait CAPSR.CPKIL bit */
176 for (i
= 0; i
< 1000; i
++) {
177 if (!(ceu_read(pcdev
, CAPSR
) & (1 << 16))) {
185 dev_warn(pcdev
->ici
.v4l2_dev
.dev
, "soft reset time out\n");
193 * Videobuf operations
197 * .queue_setup() is called to check, whether the driver can accept the
198 * requested number of buffers and to fill in plane sizes
199 * for the current frame format if required
201 static int sh_mobile_ceu_videobuf_setup(struct vb2_queue
*vq
,
202 unsigned int *count
, unsigned int *num_planes
,
203 unsigned int sizes
[], struct device
*alloc_devs
[])
205 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
206 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
207 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
209 if (!vq
->num_buffers
)
215 /* Called from VIDIOC_REQBUFS or in compatibility mode */
217 sizes
[0] = icd
->sizeimage
;
218 else if (sizes
[0] < icd
->sizeimage
)
221 /* If *num_planes != 0, we have already verified *count. */
222 if (pcdev
->video_limit
) {
223 size_t size
= PAGE_ALIGN(sizes
[0]) * *count
;
225 if (size
+ pcdev
->buf_total
> pcdev
->video_limit
)
226 *count
= (pcdev
->video_limit
- pcdev
->buf_total
) /
227 PAGE_ALIGN(sizes
[0]);
232 dev_dbg(icd
->parent
, "count=%d, size=%u\n", *count
, sizes
[0]);
237 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
238 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
239 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
240 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
241 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
242 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
246 * return value doesn't reflex the success/failure to queue the new buffer,
247 * but rather the status of the previous buffer.
249 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
251 struct soc_camera_device
*icd
= pcdev
->ici
.icd
;
252 dma_addr_t phys_addr_top
, phys_addr_bottom
;
253 unsigned long top1
, top2
;
254 unsigned long bottom1
, bottom2
;
260 * The hardware is _very_ picky about this sequence. Especially
261 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
262 * several not-so-well documented interrupt sources in CETCR.
264 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_MASK
);
265 status
= ceu_read(pcdev
, CETCR
);
266 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
);
283 complete(&pcdev
->complete
);
290 if (V4L2_FIELD_INTERLACED_BT
== pcdev
->field
) {
303 vb2_dma_contig_plane_dma_addr(&pcdev
->active
->vb2_buf
, 0);
305 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
306 case V4L2_PIX_FMT_NV12
:
307 case V4L2_PIX_FMT_NV21
:
308 case V4L2_PIX_FMT_NV16
:
309 case V4L2_PIX_FMT_NV61
:
316 ceu_write(pcdev
, top1
, phys_addr_top
);
317 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
318 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
319 ceu_write(pcdev
, bottom1
, phys_addr_bottom
);
323 phys_addr_top
+= icd
->bytesperline
* icd
->user_height
;
324 ceu_write(pcdev
, top2
, phys_addr_top
);
325 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
326 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
327 ceu_write(pcdev
, bottom2
, phys_addr_bottom
);
331 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
336 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer
*vb
)
338 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
339 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
341 /* Added list head initialization on alloc */
342 WARN(!list_empty(&buf
->queue
), "Buffer %p on queue!\n", vb
);
347 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer
*vb
)
349 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
350 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
351 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
352 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
353 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
356 size
= icd
->sizeimage
;
358 if (vb2_plane_size(vb
, 0) < size
) {
359 dev_err(icd
->parent
, "Buffer #%d too small (%lu < %lu)\n",
360 vb
->index
, vb2_plane_size(vb
, 0), size
);
364 vb2_set_plane_payload(vb
, 0, size
);
366 dev_dbg(icd
->parent
, "%s (vb=0x%p) 0x%p %lu\n", __func__
,
367 vb
, vb2_plane_vaddr(vb
, 0), vb2_get_plane_payload(vb
, 0));
371 * This can be useful if you want to see if we actually fill
372 * the buffer with something
374 if (vb2_plane_vaddr(vb
, 0))
375 memset(vb2_plane_vaddr(vb
, 0), 0xaa, vb2_get_plane_payload(vb
, 0));
378 spin_lock_irq(&pcdev
->lock
);
379 list_add_tail(&buf
->queue
, &pcdev
->capture
);
381 if (!pcdev
->active
) {
383 * Because there were no active buffer at this moment,
384 * we are not interested in the return value of
385 * sh_mobile_ceu_capture here.
387 pcdev
->active
= vbuf
;
388 sh_mobile_ceu_capture(pcdev
);
390 spin_unlock_irq(&pcdev
->lock
);
395 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
398 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer
*vb
)
400 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
401 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
402 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
403 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
404 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
406 spin_lock_irq(&pcdev
->lock
);
408 if (pcdev
->active
== vbuf
) {
409 /* disable capture (release DMA buffer), reset */
410 ceu_write(pcdev
, CAPSR
, 1 << 16);
411 pcdev
->active
= NULL
;
415 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
416 * buffer failed, and .buf_init() hasn't been called
419 list_del_init(&buf
->queue
);
421 pcdev
->buf_total
-= PAGE_ALIGN(vb2_plane_size(vb
, 0));
422 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
425 spin_unlock_irq(&pcdev
->lock
);
428 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer
*vb
)
430 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
431 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
432 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
433 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
435 pcdev
->buf_total
+= PAGE_ALIGN(vb2_plane_size(vb
, 0));
436 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
439 /* This is for locking debugging only */
440 INIT_LIST_HEAD(&to_ceu_vb(vbuf
)->queue
);
444 static void sh_mobile_ceu_stop_streaming(struct vb2_queue
*q
)
446 struct soc_camera_device
*icd
= soc_camera_from_vb2q(q
);
447 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
448 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
449 struct list_head
*buf_head
, *tmp
;
450 struct vb2_v4l2_buffer
*vbuf
;
452 spin_lock_irq(&pcdev
->lock
);
454 pcdev
->active
= NULL
;
456 list_for_each_safe(buf_head
, tmp
, &pcdev
->capture
) {
457 vbuf
= &list_entry(buf_head
, struct sh_mobile_ceu_buffer
,
459 vb2_buffer_done(&vbuf
->vb2_buf
, VB2_BUF_STATE_DONE
);
460 list_del_init(buf_head
);
463 spin_unlock_irq(&pcdev
->lock
);
465 sh_mobile_ceu_soft_reset(pcdev
);
468 static const struct vb2_ops sh_mobile_ceu_videobuf_ops
= {
469 .queue_setup
= sh_mobile_ceu_videobuf_setup
,
470 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
471 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
472 .buf_cleanup
= sh_mobile_ceu_videobuf_release
,
473 .buf_init
= sh_mobile_ceu_videobuf_init
,
474 .wait_prepare
= vb2_ops_wait_prepare
,
475 .wait_finish
= vb2_ops_wait_finish
,
476 .stop_streaming
= sh_mobile_ceu_stop_streaming
,
479 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
481 struct sh_mobile_ceu_dev
*pcdev
= data
;
482 struct vb2_v4l2_buffer
*vbuf
;
485 spin_lock(&pcdev
->lock
);
487 vbuf
= pcdev
->active
;
489 /* Stale interrupt from a released buffer */
492 list_del_init(&to_ceu_vb(vbuf
)->queue
);
494 if (!list_empty(&pcdev
->capture
))
495 pcdev
->active
= &list_entry(pcdev
->capture
.next
,
496 struct sh_mobile_ceu_buffer
, queue
)->vb
;
498 pcdev
->active
= NULL
;
500 ret
= sh_mobile_ceu_capture(pcdev
);
501 vbuf
->vb2_buf
.timestamp
= ktime_get_ns();
503 vbuf
->field
= pcdev
->field
;
504 vbuf
->sequence
= pcdev
->sequence
++;
506 vb2_buffer_done(&vbuf
->vb2_buf
,
507 ret
< 0 ? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
510 spin_unlock(&pcdev
->lock
);
515 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
517 dev_info(icd
->parent
,
518 "SuperH Mobile CEU driver attached to camera %d\n",
524 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
526 dev_info(icd
->parent
,
527 "SuperH Mobile CEU driver detached from camera %d\n",
531 /* Called with .host_lock held */
532 static int sh_mobile_ceu_clock_start(struct soc_camera_host
*ici
)
534 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
536 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
538 pcdev
->buf_total
= 0;
540 sh_mobile_ceu_soft_reset(pcdev
);
545 /* Called with .host_lock held */
546 static void sh_mobile_ceu_clock_stop(struct soc_camera_host
*ici
)
548 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
550 /* disable capture, disable interrupts */
551 ceu_write(pcdev
, CEIER
, 0);
552 sh_mobile_ceu_soft_reset(pcdev
);
554 /* make sure active buffer is canceled */
555 spin_lock_irq(&pcdev
->lock
);
557 list_del_init(&to_ceu_vb(pcdev
->active
)->queue
);
558 vb2_buffer_done(&pcdev
->active
->vb2_buf
, VB2_BUF_STATE_ERROR
);
559 pcdev
->active
= NULL
;
561 spin_unlock_irq(&pcdev
->lock
);
563 pm_runtime_put(ici
->v4l2_dev
.dev
);
567 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
568 * in SH7722 Hardware Manual
570 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
572 unsigned int mant_pre
= scale
>> 12;
575 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
576 mant_pre
* 4096 / scale
+ 1;
579 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
586 scale
= (src
* 4096 / *dst
) & ~7;
588 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
591 *dst
= size_dst(src
, scale
);
596 /* rect is guaranteed to not exceed the scaled camera rectangle */
597 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
)
599 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
600 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
601 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
602 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
603 unsigned int left_offset
, top_offset
;
606 dev_geo(icd
->parent
, "Crop %ux%u@%u:%u\n",
607 icd
->user_width
, icd
->user_height
, cam
->ceu_left
, cam
->ceu_top
);
609 left_offset
= cam
->ceu_left
;
610 top_offset
= cam
->ceu_top
;
612 WARN_ON(icd
->user_width
& 3 || icd
->user_height
& 3);
614 width
= icd
->user_width
;
616 if (pcdev
->image_mode
) {
617 in_width
= cam
->width
;
618 if (!pcdev
->is_16bit
) {
623 unsigned int w_factor
;
625 switch (icd
->current_fmt
->host_fmt
->packing
) {
626 case SOC_MBUS_PACKING_2X8_PADHI
:
633 in_width
= cam
->width
* w_factor
;
634 left_offset
*= w_factor
;
637 cdwdr_width
= icd
->bytesperline
;
639 height
= icd
->user_height
;
640 in_height
= cam
->height
;
641 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
642 height
= (height
/ 2) & ~3;
648 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
649 camor
= left_offset
| (top_offset
<< 16);
652 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
653 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
656 ceu_write(pcdev
, CAMOR
, camor
);
657 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
658 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
659 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
660 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
663 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
665 u32 capsr
= ceu_read(pcdev
, CAPSR
);
666 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
670 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
672 unsigned long timeout
= jiffies
+ 10 * HZ
;
675 * Wait until the end of the current frame. It can take a long time,
676 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
678 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
681 if (time_after(jiffies
, timeout
)) {
682 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
683 "Timeout waiting for frame end! Interface problem?\n");
687 /* Wait until reset clears, this shall not hang... */
688 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
691 /* Anything to restore? */
692 if (capsr
& ~(1 << 16))
693 ceu_write(pcdev
, CAPSR
, capsr
);
696 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
697 V4L2_MBUS_PCLK_SAMPLE_RISING | \
698 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
699 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
700 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
701 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
702 V4L2_MBUS_DATA_ACTIVE_HIGH)
704 /* Capture is not running, no interrupts, no locking needed */
705 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
)
707 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
708 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
709 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
710 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
711 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
712 unsigned long value
, common_flags
= CEU_BUS_FLAGS
;
713 u32 capsr
= capture_save_reset(pcdev
);
714 unsigned int yuv_lineskip
;
718 * If the client doesn't implement g_mbus_config, we just use our
721 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
723 common_flags
= soc_mbus_config_compatible(&cfg
,
727 } else if (ret
!= -ENOIOCTLCMD
) {
731 /* Make choises, based on platform preferences */
732 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
733 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
734 if (pcdev
->flags
& SH_CEU_FLAG_HSYNC_LOW
)
735 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
737 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
740 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
741 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
742 if (pcdev
->flags
& SH_CEU_FLAG_VSYNC_LOW
)
743 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
745 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
748 cfg
.flags
= common_flags
;
749 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
750 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
753 if (icd
->current_fmt
->host_fmt
->bits_per_sample
> 8)
758 ceu_write(pcdev
, CRCNTR
, 0);
759 ceu_write(pcdev
, CRCMPR
, 0);
761 value
= 0x00000010; /* data fetch by default */
764 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
765 case V4L2_PIX_FMT_NV12
:
766 case V4L2_PIX_FMT_NV21
:
767 /* convert 4:2:2 -> 4:2:0 */
768 yuv_lineskip
= 0; /* skip for NV12/21, no skip for NV16/61 */
770 case V4L2_PIX_FMT_NV16
:
771 case V4L2_PIX_FMT_NV61
:
773 case MEDIA_BUS_FMT_UYVY8_2X8
:
774 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
776 case MEDIA_BUS_FMT_VYUY8_2X8
:
777 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
779 case MEDIA_BUS_FMT_YUYV8_2X8
:
780 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
782 case MEDIA_BUS_FMT_YVYU8_2X8
:
783 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
790 if (icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
791 icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
792 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
794 value
|= common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
795 value
|= common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
799 else if (pcdev
->flags
& SH_CEU_FLAG_LOWER_8BIT
)
802 ceu_write(pcdev
, CAMCR
, value
);
804 ceu_write(pcdev
, CAPCR
, 0x00300000);
806 switch (pcdev
->field
) {
807 case V4L2_FIELD_INTERLACED_TB
:
810 case V4L2_FIELD_INTERLACED_BT
:
817 ceu_write(pcdev
, CAIFR
, value
);
819 sh_mobile_ceu_set_rect(icd
);
822 dev_geo(icd
->parent
, "CFLCR 0x%x\n", pcdev
->cflcr
);
823 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
826 * A few words about byte order (observed in Big Endian mode)
828 * In data fetch mode bytes are received in chunks of 8 bytes.
829 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
831 * The data is however by default written to memory in reverse order:
832 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
834 * The lowest three bits of CDOCR allows us to do swapping,
835 * using 7 we swap the data bytes to match the incoming order:
836 * D0, D1, D2, D3, D4, D5, D6, D7
838 value
= 0x00000007 | yuv_lineskip
;
840 ceu_write(pcdev
, CDOCR
, value
);
841 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
843 capture_restore(pcdev
, capsr
);
845 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
849 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
850 unsigned char buswidth
)
852 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
853 unsigned long common_flags
= CEU_BUS_FLAGS
;
854 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
857 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
859 common_flags
= soc_mbus_config_compatible(&cfg
,
861 else if (ret
!= -ENOIOCTLCMD
)
864 if (!common_flags
|| buswidth
> 16)
870 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats
[] = {
872 .fourcc
= V4L2_PIX_FMT_NV12
,
874 .bits_per_sample
= 8,
875 .packing
= SOC_MBUS_PACKING_1_5X8
,
876 .order
= SOC_MBUS_ORDER_LE
,
877 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
879 .fourcc
= V4L2_PIX_FMT_NV21
,
881 .bits_per_sample
= 8,
882 .packing
= SOC_MBUS_PACKING_1_5X8
,
883 .order
= SOC_MBUS_ORDER_LE
,
884 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
886 .fourcc
= V4L2_PIX_FMT_NV16
,
888 .bits_per_sample
= 8,
889 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
890 .order
= SOC_MBUS_ORDER_LE
,
891 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
893 .fourcc
= V4L2_PIX_FMT_NV61
,
895 .bits_per_sample
= 8,
896 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
897 .order
= SOC_MBUS_ORDER_LE
,
898 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
902 /* This will be corrected as we get more formats */
903 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
905 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
906 (fmt
->bits_per_sample
== 8 &&
907 fmt
->packing
== SOC_MBUS_PACKING_1_5X8
) ||
908 (fmt
->bits_per_sample
== 8 &&
909 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
910 (fmt
->bits_per_sample
> 8 &&
911 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
914 static struct soc_camera_device
*ctrl_to_icd(struct v4l2_ctrl
*ctrl
)
916 return container_of(ctrl
->handler
, struct soc_camera_device
,
920 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl
*ctrl
)
922 struct soc_camera_device
*icd
= ctrl_to_icd(ctrl
);
923 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
924 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
927 case V4L2_CID_SHARPNESS
:
928 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
929 case V4L2_PIX_FMT_NV12
:
930 case V4L2_PIX_FMT_NV21
:
931 case V4L2_PIX_FMT_NV16
:
932 case V4L2_PIX_FMT_NV61
:
933 ceu_write(pcdev
, CLFCR
, !ctrl
->val
);
942 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops
= {
943 .s_ctrl
= sh_mobile_ceu_s_ctrl
,
946 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, unsigned int idx
,
947 struct soc_camera_format_xlate
*xlate
)
949 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
950 struct device
*dev
= icd
->parent
;
951 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
952 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
955 struct sh_mobile_ceu_cam
*cam
;
956 struct v4l2_subdev_mbus_code_enum code
= {
957 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
960 const struct soc_mbus_pixelfmt
*fmt
;
962 ret
= v4l2_subdev_call(sd
, pad
, enum_mbus_code
, NULL
, &code
);
964 /* No more formats */
967 fmt
= soc_mbus_get_fmtdesc(code
.code
);
969 dev_warn(dev
, "unsupported format code #%u: %d\n", idx
, code
.code
);
973 ret
= sh_mobile_ceu_try_bus_param(icd
, fmt
->bits_per_sample
);
977 if (!icd
->host_priv
) {
978 struct v4l2_subdev_format fmt
= {
979 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
981 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
982 struct v4l2_rect rect
;
985 /* Add our control */
986 v4l2_ctrl_new_std(&icd
->ctrl_handler
, &sh_mobile_ceu_ctrl_ops
,
987 V4L2_CID_SHARPNESS
, 0, 1, 1, 1);
988 if (icd
->ctrl_handler
.error
)
989 return icd
->ctrl_handler
.error
;
991 /* FIXME: subwindow is lost between close / open */
993 /* Cache current client geometry */
994 ret
= soc_camera_client_g_rect(sd
, &rect
);
999 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1004 * All currently existing CEU implementations support 2560x1920
1005 * or larger frames. If the sensor is proposing too big a frame,
1006 * don't bother with possibly supportred by the CEU larger
1007 * sizes, just try VGA multiples. If needed, this can be
1008 * adjusted in the future.
1010 while ((mf
->width
> pcdev
->max_width
||
1011 mf
->height
> pcdev
->max_height
) && shift
< 4) {
1012 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1013 mf
->width
= 2560 >> shift
;
1014 mf
->height
= 1920 >> shift
;
1015 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1016 soc_camera_grp_id(icd
), pad
,
1017 set_fmt
, NULL
, &fmt
);
1024 dev_err(dev
, "Failed to configure the client below %ux%x\n",
1025 mf
->width
, mf
->height
);
1029 dev_geo(dev
, "camera fmt %ux%u\n", mf
->width
, mf
->height
);
1031 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1035 /* We are called with current camera crop, initialise subrect with it */
1037 cam
->subrect
= rect
;
1039 cam
->width
= mf
->width
;
1040 cam
->height
= mf
->height
;
1042 icd
->host_priv
= cam
;
1044 cam
= icd
->host_priv
;
1047 /* Beginning of a pass */
1049 cam
->extra_fmt
= NULL
;
1051 switch (code
.code
) {
1052 case MEDIA_BUS_FMT_UYVY8_2X8
:
1053 case MEDIA_BUS_FMT_VYUY8_2X8
:
1054 case MEDIA_BUS_FMT_YUYV8_2X8
:
1055 case MEDIA_BUS_FMT_YVYU8_2X8
:
1060 * Our case is simple so far: for any of the above four camera
1061 * formats we add all our four synthesized NV* formats, so,
1062 * just marking the device with a single flag suffices. If
1063 * the format generation rules are more complex, you would have
1064 * to actually hang your already added / counted formats onto
1065 * the host_priv pointer and check whether the format you're
1066 * going to add now is already there.
1068 cam
->extra_fmt
= sh_mobile_ceu_formats
;
1070 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
1072 for (k
= 0; xlate
&& k
< n
; k
++) {
1073 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
1074 xlate
->code
= code
.code
;
1076 dev_dbg(dev
, "Providing format %s using code %d\n",
1077 sh_mobile_ceu_formats
[k
].name
, code
.code
);
1081 if (!sh_mobile_ceu_packing_supported(fmt
))
1085 /* Generic pass-through */
1088 xlate
->host_fmt
= fmt
;
1089 xlate
->code
= code
.code
;
1091 dev_dbg(dev
, "Providing format %s in pass-through mode\n",
1098 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
1100 kfree(icd
->host_priv
);
1101 icd
->host_priv
= NULL
;
1104 #define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale)
1105 #define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out)
1108 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1109 * framerate by always requesting the maximum image from the client. See
1110 * Documentation/media/v4l-drivers/sh_mobile_ceu_camera.rst for a description of
1111 * scaling and cropping algorithms and for the meaning of referenced here steps.
1113 static int sh_mobile_ceu_set_selection(struct soc_camera_device
*icd
,
1114 struct v4l2_selection
*sel
)
1116 struct v4l2_rect
*rect
= &sel
->r
;
1117 struct device
*dev
= icd
->parent
;
1118 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1119 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1120 struct v4l2_selection cam_sel
;
1121 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1122 struct v4l2_rect
*cam_rect
= &cam_sel
.r
;
1123 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1124 struct v4l2_subdev_format fmt
= {
1125 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1127 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
1128 unsigned int scale_cam_h
, scale_cam_v
, scale_ceu_h
, scale_ceu_v
,
1129 out_width
, out_height
;
1130 int interm_width
, interm_height
;
1134 dev_geo(dev
, "S_SELECTION(%ux%u@%u:%u)\n", rect
->width
, rect
->height
,
1135 rect
->left
, rect
->top
);
1137 /* During camera cropping its output window can change too, stop CEU */
1138 capsr
= capture_save_reset(pcdev
);
1139 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1142 * 1. - 2. Apply iterative camera S_SELECTION for new input window, read back
1143 * actual camera rectangle.
1145 ret
= soc_camera_client_s_selection(sd
, sel
, &cam_sel
,
1146 &cam
->rect
, &cam
->subrect
);
1150 dev_geo(dev
, "1-2: camera cropped to %ux%u@%u:%u\n",
1151 cam_rect
->width
, cam_rect
->height
,
1152 cam_rect
->left
, cam_rect
->top
);
1154 /* On success cam_crop contains current camera crop */
1156 /* 3. Retrieve camera output window */
1157 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1161 if (mf
->width
> pcdev
->max_width
|| mf
->height
> pcdev
->max_height
)
1164 /* 4. Calculate camera scales */
1165 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1166 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1168 /* Calculate intermediate window */
1169 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1170 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1172 if (interm_width
< icd
->user_width
) {
1175 new_scale_h
= calc_generic_scale(rect
->width
, icd
->user_width
);
1177 mf
->width
= scale_down(cam_rect
->width
, new_scale_h
);
1180 if (interm_height
< icd
->user_height
) {
1183 new_scale_v
= calc_generic_scale(rect
->height
, icd
->user_height
);
1185 mf
->height
= scale_down(cam_rect
->height
, new_scale_v
);
1188 if (interm_width
< icd
->user_width
|| interm_height
< icd
->user_height
) {
1189 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1190 soc_camera_grp_id(icd
), pad
,
1191 set_fmt
, NULL
, &fmt
);
1195 dev_geo(dev
, "New camera output %ux%u\n", mf
->width
, mf
->height
);
1196 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1197 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1198 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1199 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1202 /* Cache camera output window */
1203 cam
->width
= mf
->width
;
1204 cam
->height
= mf
->height
;
1206 if (pcdev
->image_mode
) {
1207 out_width
= min(interm_width
, icd
->user_width
);
1208 out_height
= min(interm_height
, icd
->user_height
);
1210 out_width
= interm_width
;
1211 out_height
= interm_height
;
1215 * 5. Calculate CEU scales from camera scales from results of (5) and
1218 scale_ceu_h
= calc_scale(interm_width
, &out_width
);
1219 scale_ceu_v
= calc_scale(interm_height
, &out_height
);
1221 dev_geo(dev
, "5: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1223 /* Apply CEU scales. */
1224 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1225 if (cflcr
!= pcdev
->cflcr
) {
1226 pcdev
->cflcr
= cflcr
;
1227 ceu_write(pcdev
, CFLCR
, cflcr
);
1230 icd
->user_width
= out_width
& ~3;
1231 icd
->user_height
= out_height
& ~3;
1232 /* Offsets are applied at the CEU scaling filter input */
1233 cam
->ceu_left
= scale_down(rect
->left
- cam_rect
->left
, scale_cam_h
) & ~1;
1234 cam
->ceu_top
= scale_down(rect
->top
- cam_rect
->top
, scale_cam_v
) & ~1;
1236 /* 6. Use CEU cropping to crop to the new window. */
1237 sh_mobile_ceu_set_rect(icd
);
1239 cam
->subrect
= *rect
;
1241 dev_geo(dev
, "6: CEU cropped to %ux%u@%u:%u\n",
1242 icd
->user_width
, icd
->user_height
,
1243 cam
->ceu_left
, cam
->ceu_top
);
1245 /* Restore capture. The CE bit can be cleared by the hardware */
1248 capture_restore(pcdev
, capsr
);
1250 /* Even if only camera cropping succeeded */
1254 static int sh_mobile_ceu_get_selection(struct soc_camera_device
*icd
,
1255 struct v4l2_selection
*sel
)
1257 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1259 sel
->r
= cam
->subrect
;
1264 /* Similar to set_crop multistage iterative algorithm */
1265 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1266 struct v4l2_format
*f
)
1268 struct device
*dev
= icd
->parent
;
1269 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1270 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1271 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1272 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1273 struct v4l2_mbus_framefmt mf
;
1274 __u32 pixfmt
= pix
->pixelformat
;
1275 const struct soc_camera_format_xlate
*xlate
;
1276 unsigned int ceu_sub_width
= pcdev
->max_width
,
1277 ceu_sub_height
= pcdev
->max_height
;
1278 u16 scale_v
, scale_h
;
1281 enum v4l2_field field
;
1283 switch (pix
->field
) {
1285 pix
->field
= V4L2_FIELD_NONE
;
1287 case V4L2_FIELD_INTERLACED_TB
:
1288 case V4L2_FIELD_INTERLACED_BT
:
1289 case V4L2_FIELD_NONE
:
1292 case V4L2_FIELD_INTERLACED
:
1293 field
= V4L2_FIELD_INTERLACED_TB
;
1297 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1299 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1303 /* 1.-4. Calculate desired client output geometry */
1304 soc_camera_calc_client_output(icd
, &cam
->rect
, &cam
->subrect
, pix
, &mf
, 12);
1305 mf
.field
= pix
->field
;
1306 mf
.colorspace
= pix
->colorspace
;
1307 mf
.code
= xlate
->code
;
1310 case V4L2_PIX_FMT_NV12
:
1311 case V4L2_PIX_FMT_NV21
:
1312 case V4L2_PIX_FMT_NV16
:
1313 case V4L2_PIX_FMT_NV61
:
1320 dev_geo(dev
, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt
, mf
.field
, mf
.code
,
1321 pix
->width
, pix
->height
);
1323 dev_geo(dev
, "4: request camera output %ux%u\n", mf
.width
, mf
.height
);
1326 ret
= soc_camera_client_scale(icd
, &cam
->rect
, &cam
->subrect
,
1327 &mf
, &ceu_sub_width
, &ceu_sub_height
,
1328 image_mode
&& V4L2_FIELD_NONE
== field
, 12);
1330 dev_geo(dev
, "5-9: client scale return %d\n", ret
);
1332 /* Done with the camera. Now see if we can improve the result */
1334 dev_geo(dev
, "fmt %ux%u, requested %ux%u\n",
1335 mf
.width
, mf
.height
, pix
->width
, pix
->height
);
1339 if (mf
.code
!= xlate
->code
)
1342 /* 9. Prepare CEU crop */
1343 cam
->width
= mf
.width
;
1344 cam
->height
= mf
.height
;
1346 /* 10. Use CEU scaling to scale to the requested user window. */
1348 /* We cannot scale up */
1349 if (pix
->width
> ceu_sub_width
)
1350 ceu_sub_width
= pix
->width
;
1352 if (pix
->height
> ceu_sub_height
)
1353 ceu_sub_height
= pix
->height
;
1355 pix
->colorspace
= mf
.colorspace
;
1358 /* Scale pix->{width x height} down to width x height */
1359 scale_h
= calc_scale(ceu_sub_width
, &pix
->width
);
1360 scale_v
= calc_scale(ceu_sub_height
, &pix
->height
);
1362 pix
->width
= ceu_sub_width
;
1363 pix
->height
= ceu_sub_height
;
1368 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1371 * We have calculated CFLCR, the actual configuration will be performed
1372 * in sh_mobile_ceu_set_bus_param()
1375 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1376 ceu_sub_width
, scale_h
, pix
->width
,
1377 ceu_sub_height
, scale_v
, pix
->height
);
1379 cam
->code
= xlate
->code
;
1380 icd
->current_fmt
= xlate
;
1382 pcdev
->field
= field
;
1383 pcdev
->image_mode
= image_mode
;
1385 /* CFSZR requirement */
1392 #define CEU_CHDW_MAX 8188U /* Maximum line stride */
1394 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1395 struct v4l2_format
*f
)
1397 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1398 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1399 const struct soc_camera_format_xlate
*xlate
;
1400 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1401 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1402 struct v4l2_subdev_pad_config pad_cfg
;
1403 struct v4l2_subdev_format format
= {
1404 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1406 struct v4l2_mbus_framefmt
*mf
= &format
.format
;
1407 __u32 pixfmt
= pix
->pixelformat
;
1411 dev_geo(icd
->parent
, "TRY_FMT(pix=0x%x, %ux%u)\n",
1412 pixfmt
, pix
->width
, pix
->height
);
1414 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1416 xlate
= icd
->current_fmt
;
1417 dev_dbg(icd
->parent
, "Format %x not found, keeping %x\n",
1418 pixfmt
, xlate
->host_fmt
->fourcc
);
1419 pixfmt
= xlate
->host_fmt
->fourcc
;
1420 pix
->pixelformat
= pixfmt
;
1421 pix
->colorspace
= icd
->colorspace
;
1424 /* FIXME: calculate using depth and bus width */
1426 /* CFSZR requires height and width to be 4-pixel aligned */
1427 v4l_bound_align_image(&pix
->width
, 2, pcdev
->max_width
, 2,
1428 &pix
->height
, 4, pcdev
->max_height
, 2, 0);
1431 height
= pix
->height
;
1433 /* limit to sensor capabilities */
1434 mf
->width
= pix
->width
;
1435 mf
->height
= pix
->height
;
1436 mf
->field
= pix
->field
;
1437 mf
->code
= xlate
->code
;
1438 mf
->colorspace
= pix
->colorspace
;
1440 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
, soc_camera_grp_id(icd
),
1441 pad
, set_fmt
, &pad_cfg
, &format
);
1445 pix
->width
= mf
->width
;
1446 pix
->height
= mf
->height
;
1447 pix
->field
= mf
->field
;
1448 pix
->colorspace
= mf
->colorspace
;
1451 case V4L2_PIX_FMT_NV12
:
1452 case V4L2_PIX_FMT_NV21
:
1453 case V4L2_PIX_FMT_NV16
:
1454 case V4L2_PIX_FMT_NV61
:
1455 /* FIXME: check against rect_max after converting soc-camera */
1456 /* We can scale precisely, need a bigger image from camera */
1457 if (pix
->width
< width
|| pix
->height
< height
) {
1459 * We presume, the sensor behaves sanely, i.e., if
1460 * requested a bigger rectangle, it will not return a
1463 mf
->width
= pcdev
->max_width
;
1464 mf
->height
= pcdev
->max_height
;
1465 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1466 soc_camera_grp_id(icd
), pad
,
1467 set_fmt
, &pad_cfg
, &format
);
1469 /* Shouldn't actually happen... */
1470 dev_err(icd
->parent
,
1471 "FIXME: client try_fmt() = %d\n", ret
);
1475 /* We will scale exactly */
1476 if (mf
->width
> width
)
1478 if (mf
->height
> height
)
1479 pix
->height
= height
;
1481 pix
->bytesperline
= max(pix
->bytesperline
, pix
->width
);
1482 pix
->bytesperline
= min(pix
->bytesperline
, CEU_CHDW_MAX
);
1483 pix
->bytesperline
&= ~3;
1487 /* Configurable stride isn't supported in pass-through mode. */
1488 pix
->bytesperline
= 0;
1495 dev_geo(icd
->parent
, "%s(): return %d, fmt 0x%x, %ux%u\n",
1496 __func__
, ret
, pix
->pixelformat
, pix
->width
, pix
->height
);
1501 static int sh_mobile_ceu_set_liveselection(struct soc_camera_device
*icd
,
1502 struct v4l2_selection
*sel
)
1504 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1505 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1506 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1507 u32 out_width
= icd
->user_width
, out_height
= icd
->user_height
;
1512 /* Wait for frame */
1513 ret
= wait_for_completion_interruptible(&pcdev
->complete
);
1514 /* Stop the client */
1515 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 0);
1517 dev_warn(icd
->parent
,
1518 "Client failed to stop the stream: %d\n", ret
);
1520 /* Do the crop, if it fails, there's nothing more we can do */
1521 sh_mobile_ceu_set_selection(icd
, sel
);
1523 dev_geo(icd
->parent
, "Output after crop: %ux%u\n", icd
->user_width
, icd
->user_height
);
1525 if (icd
->user_width
!= out_width
|| icd
->user_height
!= out_height
) {
1526 struct v4l2_format f
= {
1527 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1530 .height
= out_height
,
1531 .pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
,
1532 .field
= pcdev
->field
,
1533 .colorspace
= icd
->colorspace
,
1536 ret
= sh_mobile_ceu_set_fmt(icd
, &f
);
1537 if (!ret
&& (out_width
!= f
.fmt
.pix
.width
||
1538 out_height
!= f
.fmt
.pix
.height
))
1541 icd
->user_width
= out_width
& ~3;
1542 icd
->user_height
= out_height
& ~3;
1543 ret
= sh_mobile_ceu_set_bus_param(icd
);
1547 /* Thaw the queue */
1549 spin_lock_irq(&pcdev
->lock
);
1550 sh_mobile_ceu_capture(pcdev
);
1551 spin_unlock_irq(&pcdev
->lock
);
1552 /* Start the client */
1553 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 1);
1557 static __poll_t
sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1559 struct soc_camera_device
*icd
= file
->private_data
;
1561 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
1564 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1565 struct v4l2_capability
*cap
)
1567 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1568 strlcpy(cap
->driver
, "sh_mobile_ceu", sizeof(cap
->driver
));
1569 strlcpy(cap
->bus_info
, "platform:sh_mobile_ceu", sizeof(cap
->bus_info
));
1570 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1571 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1576 static int sh_mobile_ceu_init_videobuf(struct vb2_queue
*q
,
1577 struct soc_camera_device
*icd
)
1579 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1581 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1582 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1584 q
->ops
= &sh_mobile_ceu_videobuf_ops
;
1585 q
->mem_ops
= &vb2_dma_contig_memops
;
1586 q
->buf_struct_size
= sizeof(struct sh_mobile_ceu_buffer
);
1587 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1588 q
->lock
= &ici
->host_lock
;
1589 q
->dev
= ici
->v4l2_dev
.dev
;
1591 return vb2_queue_init(q
);
1594 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1595 .owner
= THIS_MODULE
,
1596 .add
= sh_mobile_ceu_add_device
,
1597 .remove
= sh_mobile_ceu_remove_device
,
1598 .clock_start
= sh_mobile_ceu_clock_start
,
1599 .clock_stop
= sh_mobile_ceu_clock_stop
,
1600 .get_formats
= sh_mobile_ceu_get_formats
,
1601 .put_formats
= sh_mobile_ceu_put_formats
,
1602 .get_selection
= sh_mobile_ceu_get_selection
,
1603 .set_selection
= sh_mobile_ceu_set_selection
,
1604 .set_liveselection
= sh_mobile_ceu_set_liveselection
,
1605 .set_fmt
= sh_mobile_ceu_set_fmt
,
1606 .try_fmt
= sh_mobile_ceu_try_fmt
,
1607 .poll
= sh_mobile_ceu_poll
,
1608 .querycap
= sh_mobile_ceu_querycap
,
1609 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1610 .init_videobuf2
= sh_mobile_ceu_init_videobuf
,
1614 struct notifier_block notifier
;
1615 struct completion completion
;
1619 static int bus_notify(struct notifier_block
*nb
,
1620 unsigned long action
, void *data
)
1622 struct device
*dev
= data
;
1623 struct bus_wait
*wait
= container_of(nb
, struct bus_wait
, notifier
);
1625 if (wait
->dev
!= dev
)
1629 case BUS_NOTIFY_UNBOUND_DRIVER
:
1630 /* Protect from module unloading */
1631 wait_for_completion(&wait
->completion
);
1637 static int sh_mobile_ceu_probe(struct platform_device
*pdev
)
1639 struct sh_mobile_ceu_dev
*pcdev
;
1640 struct resource
*res
;
1644 struct bus_wait wait
= {
1645 .completion
= COMPLETION_INITIALIZER_ONSTACK(wait
.completion
),
1646 .notifier
.notifier_call
= bus_notify
,
1649 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1650 irq
= platform_get_irq(pdev
, 0);
1651 if (!res
|| (int)irq
<= 0) {
1652 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1656 pcdev
= devm_kzalloc(&pdev
->dev
, sizeof(*pcdev
), GFP_KERNEL
);
1658 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1662 INIT_LIST_HEAD(&pcdev
->capture
);
1663 spin_lock_init(&pcdev
->lock
);
1664 init_completion(&pcdev
->complete
);
1666 pcdev
->pdata
= pdev
->dev
.platform_data
;
1667 if (!pcdev
->pdata
&& !pdev
->dev
.of_node
) {
1668 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1672 /* TODO: implement per-device bus flags */
1674 pcdev
->max_width
= pcdev
->pdata
->max_width
;
1675 pcdev
->max_height
= pcdev
->pdata
->max_height
;
1676 pcdev
->flags
= pcdev
->pdata
->flags
;
1678 pcdev
->field
= V4L2_FIELD_NONE
;
1680 if (!pcdev
->max_width
) {
1682 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-width", &v
);
1684 pcdev
->max_width
= v
;
1686 if (!pcdev
->max_width
)
1687 pcdev
->max_width
= 2560;
1689 if (!pcdev
->max_height
) {
1691 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-height", &v
);
1693 pcdev
->max_height
= v
;
1695 if (!pcdev
->max_height
)
1696 pcdev
->max_height
= 1920;
1699 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1701 return PTR_ERR(base
);
1705 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1707 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1709 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
1712 DMA_MEMORY_EXCLUSIVE
);
1714 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
1718 pcdev
->video_limit
= resource_size(res
);
1722 err
= devm_request_irq(&pdev
->dev
, pcdev
->irq
, sh_mobile_ceu_irq
,
1723 0, dev_name(&pdev
->dev
), pcdev
);
1725 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
1726 goto exit_release_mem
;
1729 pm_suspend_ignore_children(&pdev
->dev
, true);
1730 pm_runtime_enable(&pdev
->dev
);
1731 pm_runtime_resume(&pdev
->dev
);
1733 pcdev
->ici
.priv
= pcdev
;
1734 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
1735 pcdev
->ici
.nr
= pdev
->id
;
1736 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
1737 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
1738 pcdev
->ici
.capabilities
= SOCAM_HOST_CAP_STRIDE
;
1740 if (pcdev
->pdata
&& pcdev
->pdata
->asd_sizes
) {
1741 pcdev
->ici
.asd
= pcdev
->pdata
->asd
;
1742 pcdev
->ici
.asd_sizes
= pcdev
->pdata
->asd_sizes
;
1745 err
= soc_camera_host_register(&pcdev
->ici
);
1752 pm_runtime_disable(&pdev
->dev
);
1754 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1755 dma_release_declared_memory(&pdev
->dev
);
1759 static int sh_mobile_ceu_remove(struct platform_device
*pdev
)
1761 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1763 soc_camera_host_unregister(soc_host
);
1764 pm_runtime_disable(&pdev
->dev
);
1765 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1766 dma_release_declared_memory(&pdev
->dev
);
1771 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
1773 /* Runtime PM callback shared between ->runtime_suspend()
1774 * and ->runtime_resume(). Simply returns success.
1776 * This driver re-initializes all registers after
1777 * pm_runtime_get_sync() anyway so there is no need
1778 * to save and restore registers here.
1783 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
1784 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
1785 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
1788 static const struct of_device_id sh_mobile_ceu_of_match
[] = {
1789 { .compatible
= "renesas,sh-mobile-ceu" },
1792 MODULE_DEVICE_TABLE(of
, sh_mobile_ceu_of_match
);
1794 static struct platform_driver sh_mobile_ceu_driver
= {
1796 .name
= "sh_mobile_ceu",
1797 .pm
= &sh_mobile_ceu_dev_pm_ops
,
1798 .of_match_table
= sh_mobile_ceu_of_match
,
1800 .probe
= sh_mobile_ceu_probe
,
1801 .remove
= sh_mobile_ceu_remove
,
1804 module_platform_driver(sh_mobile_ceu_driver
);
1806 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1807 MODULE_AUTHOR("Magnus Damm");
1808 MODULE_LICENSE("GPL");
1809 MODULE_VERSION("0.1.0");
1810 MODULE_ALIAS("platform:sh_mobile_ceu");