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/completion.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include <linux/moduleparam.h>
31 #include <linux/time.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 #include <linux/platform_device.h>
35 #include <linux/videodev2.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/sched.h>
39 #include <media/v4l2-async.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-dev.h>
42 #include <media/soc_camera.h>
43 #include <media/drv-intf/sh_mobile_ceu.h>
44 #include <media/videobuf2-dma-contig.h>
45 #include <media/v4l2-mediabus.h>
46 #include <media/drv-intf/soc_mediabus.h>
48 #include "soc_scale_crop.h"
50 /* register offsets for sh7722 / sh7723 */
52 #define CAPSR 0x00 /* Capture start register */
53 #define CAPCR 0x04 /* Capture control register */
54 #define CAMCR 0x08 /* Capture interface control register */
55 #define CMCYR 0x0c /* Capture interface cycle register */
56 #define CAMOR 0x10 /* Capture interface offset register */
57 #define CAPWR 0x14 /* Capture interface width register */
58 #define CAIFR 0x18 /* Capture interface input format register */
59 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
60 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
61 #define CRCNTR 0x28 /* CEU register control register */
62 #define CRCMPR 0x2c /* CEU register forcible control register */
63 #define CFLCR 0x30 /* Capture filter control register */
64 #define CFSZR 0x34 /* Capture filter size clip register */
65 #define CDWDR 0x38 /* Capture destination width register */
66 #define CDAYR 0x3c /* Capture data address Y register */
67 #define CDACR 0x40 /* Capture data address C register */
68 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
69 #define CDBCR 0x48 /* Capture data bottom-field address C register */
70 #define CBDSR 0x4c /* Capture bundle destination size register */
71 #define CFWCR 0x5c /* Firewall operation control register */
72 #define CLFCR 0x60 /* Capture low-pass filter control register */
73 #define CDOCR 0x64 /* Capture data output control register */
74 #define CDDCR 0x68 /* Capture data complexity level register */
75 #define CDDAR 0x6c /* Capture data complexity level address register */
76 #define CEIER 0x70 /* Capture event interrupt enable register */
77 #define CETCR 0x74 /* Capture event flag clear register */
78 #define CSTSR 0x7c /* Capture status register */
79 #define CSRTR 0x80 /* Capture software reset register */
80 #define CDSSR 0x84 /* Capture data size register */
81 #define CDAYR2 0x90 /* Capture data address Y register 2 */
82 #define CDACR2 0x94 /* Capture data address C register 2 */
83 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
84 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
88 #define dev_geo dev_info
90 #define dev_geo dev_dbg
93 /* per video frame buffer */
94 struct sh_mobile_ceu_buffer
{
95 struct vb2_v4l2_buffer vb
; /* v4l buffer must be first */
96 struct list_head queue
;
99 struct sh_mobile_ceu_dev
{
100 struct soc_camera_host ici
;
107 spinlock_t lock
; /* Protects video buffer lists */
108 struct list_head capture
;
109 struct vb2_v4l2_buffer
*active
;
111 struct sh_mobile_ceu_info
*pdata
;
112 struct completion complete
;
116 /* static max sizes either from platform data or default */
120 enum v4l2_field field
;
124 unsigned int image_mode
:1;
125 unsigned int is_16bit
:1;
126 unsigned int frozen
:1;
129 struct sh_mobile_ceu_cam
{
130 /* CEU offsets within the camera output, before the CEU scaler */
131 unsigned int ceu_left
;
132 unsigned int ceu_top
;
133 /* Client output, as seen by the CEU */
137 * User window from S_SELECTION / G_SELECTION, produced by client cropping and
138 * scaling, CEU scaling and CEU cropping, mapped back onto the client
141 struct v4l2_rect subrect
;
142 /* Camera cropping rectangle */
143 struct v4l2_rect rect
;
144 const struct soc_mbus_pixelfmt
*extra_fmt
;
148 static struct sh_mobile_ceu_buffer
*to_ceu_vb(struct vb2_v4l2_buffer
*vbuf
)
150 return container_of(vbuf
, struct sh_mobile_ceu_buffer
, vb
);
153 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
154 unsigned long reg_offs
, u32 data
)
156 iowrite32(data
, priv
->base
+ reg_offs
);
159 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
161 return ioread32(priv
->base
+ reg_offs
);
164 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev
*pcdev
)
168 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
170 /* wait CSTSR.CPTON bit */
171 for (i
= 0; i
< 1000; i
++) {
172 if (!(ceu_read(pcdev
, CSTSR
) & 1)) {
179 /* wait CAPSR.CPKIL bit */
180 for (i
= 0; i
< 1000; i
++) {
181 if (!(ceu_read(pcdev
, CAPSR
) & (1 << 16))) {
189 dev_warn(pcdev
->ici
.v4l2_dev
.dev
, "soft reset time out\n");
197 * Videobuf operations
201 * .queue_setup() is called to check, whether the driver can accept the
202 * requested number of buffers and to fill in plane sizes
203 * for the current frame format if required
205 static int sh_mobile_ceu_videobuf_setup(struct vb2_queue
*vq
,
206 unsigned int *count
, unsigned int *num_planes
,
207 unsigned int sizes
[], struct device
*alloc_devs
[])
209 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
210 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
211 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
213 if (!vq
->num_buffers
)
219 /* Called from VIDIOC_REQBUFS or in compatibility mode */
221 sizes
[0] = icd
->sizeimage
;
222 else if (sizes
[0] < icd
->sizeimage
)
225 /* If *num_planes != 0, we have already verified *count. */
226 if (pcdev
->video_limit
) {
227 size_t size
= PAGE_ALIGN(sizes
[0]) * *count
;
229 if (size
+ pcdev
->buf_total
> pcdev
->video_limit
)
230 *count
= (pcdev
->video_limit
- pcdev
->buf_total
) /
231 PAGE_ALIGN(sizes
[0]);
236 dev_dbg(icd
->parent
, "count=%d, size=%u\n", *count
, sizes
[0]);
241 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
242 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
243 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
244 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
245 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
246 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
250 * return value doesn't reflex the success/failure to queue the new buffer,
251 * but rather the status of the previous buffer.
253 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
255 struct soc_camera_device
*icd
= pcdev
->ici
.icd
;
256 dma_addr_t phys_addr_top
, phys_addr_bottom
;
257 unsigned long top1
, top2
;
258 unsigned long bottom1
, bottom2
;
264 * The hardware is _very_ picky about this sequence. Especially
265 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
266 * several not-so-well documented interrupt sources in CETCR.
268 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_MASK
);
269 status
= ceu_read(pcdev
, CETCR
);
270 ceu_write(pcdev
, CETCR
, ~status
& CEU_CETCR_MAGIC
);
272 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_MASK
);
273 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
274 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
277 * When a VBP interrupt occurs, a capture end interrupt does not occur
278 * and the image of that frame is not captured correctly. So, soft reset
281 if (status
& CEU_CEIER_VBP
) {
282 sh_mobile_ceu_soft_reset(pcdev
);
287 complete(&pcdev
->complete
);
294 if (V4L2_FIELD_INTERLACED_BT
== pcdev
->field
) {
307 vb2_dma_contig_plane_dma_addr(&pcdev
->active
->vb2_buf
, 0);
309 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
310 case V4L2_PIX_FMT_NV12
:
311 case V4L2_PIX_FMT_NV21
:
312 case V4L2_PIX_FMT_NV16
:
313 case V4L2_PIX_FMT_NV61
:
320 ceu_write(pcdev
, top1
, phys_addr_top
);
321 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
322 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
323 ceu_write(pcdev
, bottom1
, phys_addr_bottom
);
327 phys_addr_top
+= icd
->bytesperline
* icd
->user_height
;
328 ceu_write(pcdev
, top2
, phys_addr_top
);
329 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
330 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
331 ceu_write(pcdev
, bottom2
, phys_addr_bottom
);
335 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
340 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer
*vb
)
342 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
343 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
345 /* Added list head initialization on alloc */
346 WARN(!list_empty(&buf
->queue
), "Buffer %p on queue!\n", vb
);
351 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer
*vb
)
353 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
354 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
355 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
356 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
357 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
360 size
= icd
->sizeimage
;
362 if (vb2_plane_size(vb
, 0) < size
) {
363 dev_err(icd
->parent
, "Buffer #%d too small (%lu < %lu)\n",
364 vb
->index
, vb2_plane_size(vb
, 0), size
);
368 vb2_set_plane_payload(vb
, 0, size
);
370 dev_dbg(icd
->parent
, "%s (vb=0x%p) 0x%p %lu\n", __func__
,
371 vb
, vb2_plane_vaddr(vb
, 0), vb2_get_plane_payload(vb
, 0));
375 * This can be useful if you want to see if we actually fill
376 * the buffer with something
378 if (vb2_plane_vaddr(vb
, 0))
379 memset(vb2_plane_vaddr(vb
, 0), 0xaa, vb2_get_plane_payload(vb
, 0));
382 spin_lock_irq(&pcdev
->lock
);
383 list_add_tail(&buf
->queue
, &pcdev
->capture
);
385 if (!pcdev
->active
) {
387 * Because there were no active buffer at this moment,
388 * we are not interested in the return value of
389 * sh_mobile_ceu_capture here.
391 pcdev
->active
= vbuf
;
392 sh_mobile_ceu_capture(pcdev
);
394 spin_unlock_irq(&pcdev
->lock
);
399 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
402 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer
*vb
)
404 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
405 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
406 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
407 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vbuf
);
408 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
410 spin_lock_irq(&pcdev
->lock
);
412 if (pcdev
->active
== vbuf
) {
413 /* disable capture (release DMA buffer), reset */
414 ceu_write(pcdev
, CAPSR
, 1 << 16);
415 pcdev
->active
= NULL
;
419 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
420 * buffer failed, and .buf_init() hasn't been called
423 list_del_init(&buf
->queue
);
425 pcdev
->buf_total
-= PAGE_ALIGN(vb2_plane_size(vb
, 0));
426 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
429 spin_unlock_irq(&pcdev
->lock
);
432 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer
*vb
)
434 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
435 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
436 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
437 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
439 pcdev
->buf_total
+= PAGE_ALIGN(vb2_plane_size(vb
, 0));
440 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
443 /* This is for locking debugging only */
444 INIT_LIST_HEAD(&to_ceu_vb(vbuf
)->queue
);
448 static void sh_mobile_ceu_stop_streaming(struct vb2_queue
*q
)
450 struct soc_camera_device
*icd
= soc_camera_from_vb2q(q
);
451 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
452 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
453 struct list_head
*buf_head
, *tmp
;
455 spin_lock_irq(&pcdev
->lock
);
457 pcdev
->active
= NULL
;
459 list_for_each_safe(buf_head
, tmp
, &pcdev
->capture
)
460 list_del_init(buf_head
);
462 spin_unlock_irq(&pcdev
->lock
);
464 sh_mobile_ceu_soft_reset(pcdev
);
467 static const struct vb2_ops sh_mobile_ceu_videobuf_ops
= {
468 .queue_setup
= sh_mobile_ceu_videobuf_setup
,
469 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
470 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
471 .buf_cleanup
= sh_mobile_ceu_videobuf_release
,
472 .buf_init
= sh_mobile_ceu_videobuf_init
,
473 .wait_prepare
= vb2_ops_wait_prepare
,
474 .wait_finish
= vb2_ops_wait_finish
,
475 .stop_streaming
= sh_mobile_ceu_stop_streaming
,
478 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
480 struct sh_mobile_ceu_dev
*pcdev
= data
;
481 struct vb2_v4l2_buffer
*vbuf
;
484 spin_lock(&pcdev
->lock
);
486 vbuf
= pcdev
->active
;
488 /* Stale interrupt from a released buffer */
491 list_del_init(&to_ceu_vb(vbuf
)->queue
);
493 if (!list_empty(&pcdev
->capture
))
494 pcdev
->active
= &list_entry(pcdev
->capture
.next
,
495 struct sh_mobile_ceu_buffer
, queue
)->vb
;
497 pcdev
->active
= NULL
;
499 ret
= sh_mobile_ceu_capture(pcdev
);
500 vbuf
->vb2_buf
.timestamp
= ktime_get_ns();
502 vbuf
->field
= pcdev
->field
;
503 vbuf
->sequence
= pcdev
->sequence
++;
505 vb2_buffer_done(&vbuf
->vb2_buf
,
506 ret
< 0 ? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
509 spin_unlock(&pcdev
->lock
);
514 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
516 dev_info(icd
->parent
,
517 "SuperH Mobile CEU driver attached to camera %d\n",
523 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
525 dev_info(icd
->parent
,
526 "SuperH Mobile CEU driver detached from camera %d\n",
530 /* Called with .host_lock held */
531 static int sh_mobile_ceu_clock_start(struct soc_camera_host
*ici
)
533 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
535 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
537 pcdev
->buf_total
= 0;
539 sh_mobile_ceu_soft_reset(pcdev
);
544 /* Called with .host_lock held */
545 static void sh_mobile_ceu_clock_stop(struct soc_camera_host
*ici
)
547 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
549 /* disable capture, disable interrupts */
550 ceu_write(pcdev
, CEIER
, 0);
551 sh_mobile_ceu_soft_reset(pcdev
);
553 /* make sure active buffer is canceled */
554 spin_lock_irq(&pcdev
->lock
);
556 list_del_init(&to_ceu_vb(pcdev
->active
)->queue
);
557 vb2_buffer_done(&pcdev
->active
->vb2_buf
, VB2_BUF_STATE_ERROR
);
558 pcdev
->active
= NULL
;
560 spin_unlock_irq(&pcdev
->lock
);
562 pm_runtime_put(ici
->v4l2_dev
.dev
);
566 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
567 * in SH7722 Hardware Manual
569 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
571 unsigned int mant_pre
= scale
>> 12;
574 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
575 mant_pre
* 4096 / scale
+ 1;
578 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
585 scale
= (src
* 4096 / *dst
) & ~7;
587 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
590 *dst
= size_dst(src
, scale
);
595 /* rect is guaranteed to not exceed the scaled camera rectangle */
596 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
)
598 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
599 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
600 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
601 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
602 unsigned int left_offset
, top_offset
;
605 dev_geo(icd
->parent
, "Crop %ux%u@%u:%u\n",
606 icd
->user_width
, icd
->user_height
, cam
->ceu_left
, cam
->ceu_top
);
608 left_offset
= cam
->ceu_left
;
609 top_offset
= cam
->ceu_top
;
611 WARN_ON(icd
->user_width
& 3 || icd
->user_height
& 3);
613 width
= icd
->user_width
;
615 if (pcdev
->image_mode
) {
616 in_width
= cam
->width
;
617 if (!pcdev
->is_16bit
) {
622 unsigned int w_factor
;
624 switch (icd
->current_fmt
->host_fmt
->packing
) {
625 case SOC_MBUS_PACKING_2X8_PADHI
:
632 in_width
= cam
->width
* w_factor
;
633 left_offset
*= w_factor
;
636 cdwdr_width
= icd
->bytesperline
;
638 height
= icd
->user_height
;
639 in_height
= cam
->height
;
640 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
641 height
= (height
/ 2) & ~3;
647 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
648 camor
= left_offset
| (top_offset
<< 16);
651 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
652 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
655 ceu_write(pcdev
, CAMOR
, camor
);
656 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
657 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
658 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
659 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
662 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
664 u32 capsr
= ceu_read(pcdev
, CAPSR
);
665 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
669 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
671 unsigned long timeout
= jiffies
+ 10 * HZ
;
674 * Wait until the end of the current frame. It can take a long time,
675 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
677 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
680 if (time_after(jiffies
, timeout
)) {
681 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
682 "Timeout waiting for frame end! Interface problem?\n");
686 /* Wait until reset clears, this shall not hang... */
687 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
690 /* Anything to restore? */
691 if (capsr
& ~(1 << 16))
692 ceu_write(pcdev
, CAPSR
, capsr
);
695 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
696 V4L2_MBUS_PCLK_SAMPLE_RISING | \
697 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
698 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
699 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
700 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
701 V4L2_MBUS_DATA_ACTIVE_HIGH)
703 /* Capture is not running, no interrupts, no locking needed */
704 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
)
706 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
707 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
708 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
709 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
710 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
711 unsigned long value
, common_flags
= CEU_BUS_FLAGS
;
712 u32 capsr
= capture_save_reset(pcdev
);
713 unsigned int yuv_lineskip
;
717 * If the client doesn't implement g_mbus_config, we just use our
720 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
722 common_flags
= soc_mbus_config_compatible(&cfg
,
726 } else if (ret
!= -ENOIOCTLCMD
) {
730 /* Make choises, based on platform preferences */
731 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
732 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
733 if (pcdev
->flags
& SH_CEU_FLAG_HSYNC_LOW
)
734 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
736 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
739 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
740 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
741 if (pcdev
->flags
& SH_CEU_FLAG_VSYNC_LOW
)
742 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
744 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
747 cfg
.flags
= common_flags
;
748 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
749 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
752 if (icd
->current_fmt
->host_fmt
->bits_per_sample
> 8)
757 ceu_write(pcdev
, CRCNTR
, 0);
758 ceu_write(pcdev
, CRCMPR
, 0);
760 value
= 0x00000010; /* data fetch by default */
763 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
764 case V4L2_PIX_FMT_NV12
:
765 case V4L2_PIX_FMT_NV21
:
766 /* convert 4:2:2 -> 4:2:0 */
767 yuv_lineskip
= 0; /* skip for NV12/21, no skip for NV16/61 */
769 case V4L2_PIX_FMT_NV16
:
770 case V4L2_PIX_FMT_NV61
:
772 case MEDIA_BUS_FMT_UYVY8_2X8
:
773 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
775 case MEDIA_BUS_FMT_VYUY8_2X8
:
776 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
778 case MEDIA_BUS_FMT_YUYV8_2X8
:
779 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
781 case MEDIA_BUS_FMT_YVYU8_2X8
:
782 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
789 if (icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
790 icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
791 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
793 value
|= common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
794 value
|= common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
798 else if (pcdev
->flags
& SH_CEU_FLAG_LOWER_8BIT
)
801 ceu_write(pcdev
, CAMCR
, value
);
803 ceu_write(pcdev
, CAPCR
, 0x00300000);
805 switch (pcdev
->field
) {
806 case V4L2_FIELD_INTERLACED_TB
:
809 case V4L2_FIELD_INTERLACED_BT
:
816 ceu_write(pcdev
, CAIFR
, value
);
818 sh_mobile_ceu_set_rect(icd
);
821 dev_geo(icd
->parent
, "CFLCR 0x%x\n", pcdev
->cflcr
);
822 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
825 * A few words about byte order (observed in Big Endian mode)
827 * In data fetch mode bytes are received in chunks of 8 bytes.
828 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
830 * The data is however by default written to memory in reverse order:
831 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
833 * The lowest three bits of CDOCR allows us to do swapping,
834 * using 7 we swap the data bytes to match the incoming order:
835 * D0, D1, D2, D3, D4, D5, D6, D7
837 value
= 0x00000007 | yuv_lineskip
;
839 ceu_write(pcdev
, CDOCR
, value
);
840 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
842 capture_restore(pcdev
, capsr
);
844 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
848 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
849 unsigned char buswidth
)
851 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
852 unsigned long common_flags
= CEU_BUS_FLAGS
;
853 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
856 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
858 common_flags
= soc_mbus_config_compatible(&cfg
,
860 else if (ret
!= -ENOIOCTLCMD
)
863 if (!common_flags
|| buswidth
> 16)
869 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats
[] = {
871 .fourcc
= V4L2_PIX_FMT_NV12
,
873 .bits_per_sample
= 8,
874 .packing
= SOC_MBUS_PACKING_1_5X8
,
875 .order
= SOC_MBUS_ORDER_LE
,
876 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
878 .fourcc
= V4L2_PIX_FMT_NV21
,
880 .bits_per_sample
= 8,
881 .packing
= SOC_MBUS_PACKING_1_5X8
,
882 .order
= SOC_MBUS_ORDER_LE
,
883 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
885 .fourcc
= V4L2_PIX_FMT_NV16
,
887 .bits_per_sample
= 8,
888 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
889 .order
= SOC_MBUS_ORDER_LE
,
890 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
892 .fourcc
= V4L2_PIX_FMT_NV61
,
894 .bits_per_sample
= 8,
895 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
896 .order
= SOC_MBUS_ORDER_LE
,
897 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
901 /* This will be corrected as we get more formats */
902 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
904 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
905 (fmt
->bits_per_sample
== 8 &&
906 fmt
->packing
== SOC_MBUS_PACKING_1_5X8
) ||
907 (fmt
->bits_per_sample
== 8 &&
908 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
909 (fmt
->bits_per_sample
> 8 &&
910 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
913 static struct soc_camera_device
*ctrl_to_icd(struct v4l2_ctrl
*ctrl
)
915 return container_of(ctrl
->handler
, struct soc_camera_device
,
919 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl
*ctrl
)
921 struct soc_camera_device
*icd
= ctrl_to_icd(ctrl
);
922 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
923 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
926 case V4L2_CID_SHARPNESS
:
927 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
928 case V4L2_PIX_FMT_NV12
:
929 case V4L2_PIX_FMT_NV21
:
930 case V4L2_PIX_FMT_NV16
:
931 case V4L2_PIX_FMT_NV61
:
932 ceu_write(pcdev
, CLFCR
, !ctrl
->val
);
941 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops
= {
942 .s_ctrl
= sh_mobile_ceu_s_ctrl
,
945 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, unsigned int idx
,
946 struct soc_camera_format_xlate
*xlate
)
948 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
949 struct device
*dev
= icd
->parent
;
950 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
951 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
954 struct sh_mobile_ceu_cam
*cam
;
955 struct v4l2_subdev_mbus_code_enum code
= {
956 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
959 const struct soc_mbus_pixelfmt
*fmt
;
961 ret
= v4l2_subdev_call(sd
, pad
, enum_mbus_code
, NULL
, &code
);
963 /* No more formats */
966 fmt
= soc_mbus_get_fmtdesc(code
.code
);
968 dev_warn(dev
, "unsupported format code #%u: %d\n", idx
, code
.code
);
972 ret
= sh_mobile_ceu_try_bus_param(icd
, fmt
->bits_per_sample
);
976 if (!icd
->host_priv
) {
977 struct v4l2_subdev_format fmt
= {
978 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
980 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
981 struct v4l2_rect rect
;
984 /* Add our control */
985 v4l2_ctrl_new_std(&icd
->ctrl_handler
, &sh_mobile_ceu_ctrl_ops
,
986 V4L2_CID_SHARPNESS
, 0, 1, 1, 1);
987 if (icd
->ctrl_handler
.error
)
988 return icd
->ctrl_handler
.error
;
990 /* FIXME: subwindow is lost between close / open */
992 /* Cache current client geometry */
993 ret
= soc_camera_client_g_rect(sd
, &rect
);
998 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1003 * All currently existing CEU implementations support 2560x1920
1004 * or larger frames. If the sensor is proposing too big a frame,
1005 * don't bother with possibly supportred by the CEU larger
1006 * sizes, just try VGA multiples. If needed, this can be
1007 * adjusted in the future.
1009 while ((mf
->width
> pcdev
->max_width
||
1010 mf
->height
> pcdev
->max_height
) && shift
< 4) {
1011 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1012 mf
->width
= 2560 >> shift
;
1013 mf
->height
= 1920 >> shift
;
1014 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1015 soc_camera_grp_id(icd
), pad
,
1016 set_fmt
, NULL
, &fmt
);
1023 dev_err(dev
, "Failed to configure the client below %ux%x\n",
1024 mf
->width
, mf
->height
);
1028 dev_geo(dev
, "camera fmt %ux%u\n", mf
->width
, mf
->height
);
1030 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1034 /* We are called with current camera crop, initialise subrect with it */
1036 cam
->subrect
= rect
;
1038 cam
->width
= mf
->width
;
1039 cam
->height
= mf
->height
;
1041 icd
->host_priv
= cam
;
1043 cam
= icd
->host_priv
;
1046 /* Beginning of a pass */
1048 cam
->extra_fmt
= NULL
;
1050 switch (code
.code
) {
1051 case MEDIA_BUS_FMT_UYVY8_2X8
:
1052 case MEDIA_BUS_FMT_VYUY8_2X8
:
1053 case MEDIA_BUS_FMT_YUYV8_2X8
:
1054 case MEDIA_BUS_FMT_YVYU8_2X8
:
1059 * Our case is simple so far: for any of the above four camera
1060 * formats we add all our four synthesized NV* formats, so,
1061 * just marking the device with a single flag suffices. If
1062 * the format generation rules are more complex, you would have
1063 * to actually hang your already added / counted formats onto
1064 * the host_priv pointer and check whether the format you're
1065 * going to add now is already there.
1067 cam
->extra_fmt
= sh_mobile_ceu_formats
;
1069 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
1071 for (k
= 0; xlate
&& k
< n
; k
++) {
1072 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
1073 xlate
->code
= code
.code
;
1075 dev_dbg(dev
, "Providing format %s using code %d\n",
1076 sh_mobile_ceu_formats
[k
].name
, code
.code
);
1080 if (!sh_mobile_ceu_packing_supported(fmt
))
1084 /* Generic pass-through */
1087 xlate
->host_fmt
= fmt
;
1088 xlate
->code
= code
.code
;
1090 dev_dbg(dev
, "Providing format %s in pass-through mode\n",
1097 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
1099 kfree(icd
->host_priv
);
1100 icd
->host_priv
= NULL
;
1103 #define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale)
1104 #define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out)
1107 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1108 * framerate by always requesting the maximum image from the client. See
1109 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1110 * scaling and cropping algorithms and for the meaning of referenced here steps.
1112 static int sh_mobile_ceu_set_selection(struct soc_camera_device
*icd
,
1113 struct v4l2_selection
*sel
)
1115 struct v4l2_rect
*rect
= &sel
->r
;
1116 struct device
*dev
= icd
->parent
;
1117 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1118 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1119 struct v4l2_selection cam_sel
;
1120 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1121 struct v4l2_rect
*cam_rect
= &cam_sel
.r
;
1122 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1123 struct v4l2_subdev_format fmt
= {
1124 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1126 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
1127 unsigned int scale_cam_h
, scale_cam_v
, scale_ceu_h
, scale_ceu_v
,
1128 out_width
, out_height
;
1129 int interm_width
, interm_height
;
1133 dev_geo(dev
, "S_SELECTION(%ux%u@%u:%u)\n", rect
->width
, rect
->height
,
1134 rect
->left
, rect
->top
);
1136 /* During camera cropping its output window can change too, stop CEU */
1137 capsr
= capture_save_reset(pcdev
);
1138 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1141 * 1. - 2. Apply iterative camera S_SELECTION for new input window, read back
1142 * actual camera rectangle.
1144 ret
= soc_camera_client_s_selection(sd
, sel
, &cam_sel
,
1145 &cam
->rect
, &cam
->subrect
);
1149 dev_geo(dev
, "1-2: camera cropped to %ux%u@%u:%u\n",
1150 cam_rect
->width
, cam_rect
->height
,
1151 cam_rect
->left
, cam_rect
->top
);
1153 /* On success cam_crop contains current camera crop */
1155 /* 3. Retrieve camera output window */
1156 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1160 if (mf
->width
> pcdev
->max_width
|| mf
->height
> pcdev
->max_height
)
1163 /* 4. Calculate camera scales */
1164 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1165 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1167 /* Calculate intermediate window */
1168 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1169 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1171 if (interm_width
< icd
->user_width
) {
1174 new_scale_h
= calc_generic_scale(rect
->width
, icd
->user_width
);
1176 mf
->width
= scale_down(cam_rect
->width
, new_scale_h
);
1179 if (interm_height
< icd
->user_height
) {
1182 new_scale_v
= calc_generic_scale(rect
->height
, icd
->user_height
);
1184 mf
->height
= scale_down(cam_rect
->height
, new_scale_v
);
1187 if (interm_width
< icd
->user_width
|| interm_height
< icd
->user_height
) {
1188 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1189 soc_camera_grp_id(icd
), pad
,
1190 set_fmt
, NULL
, &fmt
);
1194 dev_geo(dev
, "New camera output %ux%u\n", mf
->width
, mf
->height
);
1195 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1196 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1197 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1198 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1201 /* Cache camera output window */
1202 cam
->width
= mf
->width
;
1203 cam
->height
= mf
->height
;
1205 if (pcdev
->image_mode
) {
1206 out_width
= min(interm_width
, icd
->user_width
);
1207 out_height
= min(interm_height
, icd
->user_height
);
1209 out_width
= interm_width
;
1210 out_height
= interm_height
;
1214 * 5. Calculate CEU scales from camera scales from results of (5) and
1217 scale_ceu_h
= calc_scale(interm_width
, &out_width
);
1218 scale_ceu_v
= calc_scale(interm_height
, &out_height
);
1220 dev_geo(dev
, "5: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1222 /* Apply CEU scales. */
1223 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1224 if (cflcr
!= pcdev
->cflcr
) {
1225 pcdev
->cflcr
= cflcr
;
1226 ceu_write(pcdev
, CFLCR
, cflcr
);
1229 icd
->user_width
= out_width
& ~3;
1230 icd
->user_height
= out_height
& ~3;
1231 /* Offsets are applied at the CEU scaling filter input */
1232 cam
->ceu_left
= scale_down(rect
->left
- cam_rect
->left
, scale_cam_h
) & ~1;
1233 cam
->ceu_top
= scale_down(rect
->top
- cam_rect
->top
, scale_cam_v
) & ~1;
1235 /* 6. Use CEU cropping to crop to the new window. */
1236 sh_mobile_ceu_set_rect(icd
);
1238 cam
->subrect
= *rect
;
1240 dev_geo(dev
, "6: CEU cropped to %ux%u@%u:%u\n",
1241 icd
->user_width
, icd
->user_height
,
1242 cam
->ceu_left
, cam
->ceu_top
);
1244 /* Restore capture. The CE bit can be cleared by the hardware */
1247 capture_restore(pcdev
, capsr
);
1249 /* Even if only camera cropping succeeded */
1253 static int sh_mobile_ceu_get_selection(struct soc_camera_device
*icd
,
1254 struct v4l2_selection
*sel
)
1256 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1258 sel
->r
= cam
->subrect
;
1263 /* Similar to set_crop multistage iterative algorithm */
1264 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1265 struct v4l2_format
*f
)
1267 struct device
*dev
= icd
->parent
;
1268 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1269 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1270 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1271 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1272 struct v4l2_mbus_framefmt mf
;
1273 __u32 pixfmt
= pix
->pixelformat
;
1274 const struct soc_camera_format_xlate
*xlate
;
1275 unsigned int ceu_sub_width
= pcdev
->max_width
,
1276 ceu_sub_height
= pcdev
->max_height
;
1277 u16 scale_v
, scale_h
;
1280 enum v4l2_field field
;
1282 switch (pix
->field
) {
1284 pix
->field
= V4L2_FIELD_NONE
;
1286 case V4L2_FIELD_INTERLACED_TB
:
1287 case V4L2_FIELD_INTERLACED_BT
:
1288 case V4L2_FIELD_NONE
:
1291 case V4L2_FIELD_INTERLACED
:
1292 field
= V4L2_FIELD_INTERLACED_TB
;
1296 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1298 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1302 /* 1.-4. Calculate desired client output geometry */
1303 soc_camera_calc_client_output(icd
, &cam
->rect
, &cam
->subrect
, pix
, &mf
, 12);
1304 mf
.field
= pix
->field
;
1305 mf
.colorspace
= pix
->colorspace
;
1306 mf
.code
= xlate
->code
;
1309 case V4L2_PIX_FMT_NV12
:
1310 case V4L2_PIX_FMT_NV21
:
1311 case V4L2_PIX_FMT_NV16
:
1312 case V4L2_PIX_FMT_NV61
:
1319 dev_geo(dev
, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt
, mf
.field
, mf
.code
,
1320 pix
->width
, pix
->height
);
1322 dev_geo(dev
, "4: request camera output %ux%u\n", mf
.width
, mf
.height
);
1325 ret
= soc_camera_client_scale(icd
, &cam
->rect
, &cam
->subrect
,
1326 &mf
, &ceu_sub_width
, &ceu_sub_height
,
1327 image_mode
&& V4L2_FIELD_NONE
== field
, 12);
1329 dev_geo(dev
, "5-9: client scale return %d\n", ret
);
1331 /* Done with the camera. Now see if we can improve the result */
1333 dev_geo(dev
, "fmt %ux%u, requested %ux%u\n",
1334 mf
.width
, mf
.height
, pix
->width
, pix
->height
);
1338 if (mf
.code
!= xlate
->code
)
1341 /* 9. Prepare CEU crop */
1342 cam
->width
= mf
.width
;
1343 cam
->height
= mf
.height
;
1345 /* 10. Use CEU scaling to scale to the requested user window. */
1347 /* We cannot scale up */
1348 if (pix
->width
> ceu_sub_width
)
1349 ceu_sub_width
= pix
->width
;
1351 if (pix
->height
> ceu_sub_height
)
1352 ceu_sub_height
= pix
->height
;
1354 pix
->colorspace
= mf
.colorspace
;
1357 /* Scale pix->{width x height} down to width x height */
1358 scale_h
= calc_scale(ceu_sub_width
, &pix
->width
);
1359 scale_v
= calc_scale(ceu_sub_height
, &pix
->height
);
1361 pix
->width
= ceu_sub_width
;
1362 pix
->height
= ceu_sub_height
;
1367 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1370 * We have calculated CFLCR, the actual configuration will be performed
1371 * in sh_mobile_ceu_set_bus_param()
1374 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1375 ceu_sub_width
, scale_h
, pix
->width
,
1376 ceu_sub_height
, scale_v
, pix
->height
);
1378 cam
->code
= xlate
->code
;
1379 icd
->current_fmt
= xlate
;
1381 pcdev
->field
= field
;
1382 pcdev
->image_mode
= image_mode
;
1384 /* CFSZR requirement */
1391 #define CEU_CHDW_MAX 8188U /* Maximum line stride */
1393 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1394 struct v4l2_format
*f
)
1396 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1397 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1398 const struct soc_camera_format_xlate
*xlate
;
1399 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1400 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1401 struct v4l2_subdev_pad_config pad_cfg
;
1402 struct v4l2_subdev_format format
= {
1403 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1405 struct v4l2_mbus_framefmt
*mf
= &format
.format
;
1406 __u32 pixfmt
= pix
->pixelformat
;
1410 dev_geo(icd
->parent
, "TRY_FMT(pix=0x%x, %ux%u)\n",
1411 pixfmt
, pix
->width
, pix
->height
);
1413 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1415 xlate
= icd
->current_fmt
;
1416 dev_dbg(icd
->parent
, "Format %x not found, keeping %x\n",
1417 pixfmt
, xlate
->host_fmt
->fourcc
);
1418 pixfmt
= xlate
->host_fmt
->fourcc
;
1419 pix
->pixelformat
= pixfmt
;
1420 pix
->colorspace
= icd
->colorspace
;
1423 /* FIXME: calculate using depth and bus width */
1425 /* CFSZR requires height and width to be 4-pixel aligned */
1426 v4l_bound_align_image(&pix
->width
, 2, pcdev
->max_width
, 2,
1427 &pix
->height
, 4, pcdev
->max_height
, 2, 0);
1430 height
= pix
->height
;
1432 /* limit to sensor capabilities */
1433 mf
->width
= pix
->width
;
1434 mf
->height
= pix
->height
;
1435 mf
->field
= pix
->field
;
1436 mf
->code
= xlate
->code
;
1437 mf
->colorspace
= pix
->colorspace
;
1439 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
, soc_camera_grp_id(icd
),
1440 pad
, set_fmt
, &pad_cfg
, &format
);
1444 pix
->width
= mf
->width
;
1445 pix
->height
= mf
->height
;
1446 pix
->field
= mf
->field
;
1447 pix
->colorspace
= mf
->colorspace
;
1450 case V4L2_PIX_FMT_NV12
:
1451 case V4L2_PIX_FMT_NV21
:
1452 case V4L2_PIX_FMT_NV16
:
1453 case V4L2_PIX_FMT_NV61
:
1454 /* FIXME: check against rect_max after converting soc-camera */
1455 /* We can scale precisely, need a bigger image from camera */
1456 if (pix
->width
< width
|| pix
->height
< height
) {
1458 * We presume, the sensor behaves sanely, i.e., if
1459 * requested a bigger rectangle, it will not return a
1462 mf
->width
= pcdev
->max_width
;
1463 mf
->height
= pcdev
->max_height
;
1464 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1465 soc_camera_grp_id(icd
), pad
,
1466 set_fmt
, &pad_cfg
, &format
);
1468 /* Shouldn't actually happen... */
1469 dev_err(icd
->parent
,
1470 "FIXME: client try_fmt() = %d\n", ret
);
1474 /* We will scale exactly */
1475 if (mf
->width
> width
)
1477 if (mf
->height
> height
)
1478 pix
->height
= height
;
1480 pix
->bytesperline
= max(pix
->bytesperline
, pix
->width
);
1481 pix
->bytesperline
= min(pix
->bytesperline
, CEU_CHDW_MAX
);
1482 pix
->bytesperline
&= ~3;
1486 /* Configurable stride isn't supported in pass-through mode. */
1487 pix
->bytesperline
= 0;
1494 dev_geo(icd
->parent
, "%s(): return %d, fmt 0x%x, %ux%u\n",
1495 __func__
, ret
, pix
->pixelformat
, pix
->width
, pix
->height
);
1500 static int sh_mobile_ceu_set_liveselection(struct soc_camera_device
*icd
,
1501 struct v4l2_selection
*sel
)
1503 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1504 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1505 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1506 u32 out_width
= icd
->user_width
, out_height
= icd
->user_height
;
1511 /* Wait for frame */
1512 ret
= wait_for_completion_interruptible(&pcdev
->complete
);
1513 /* Stop the client */
1514 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 0);
1516 dev_warn(icd
->parent
,
1517 "Client failed to stop the stream: %d\n", ret
);
1519 /* Do the crop, if it fails, there's nothing more we can do */
1520 sh_mobile_ceu_set_selection(icd
, sel
);
1522 dev_geo(icd
->parent
, "Output after crop: %ux%u\n", icd
->user_width
, icd
->user_height
);
1524 if (icd
->user_width
!= out_width
|| icd
->user_height
!= out_height
) {
1525 struct v4l2_format f
= {
1526 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1529 .height
= out_height
,
1530 .pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
,
1531 .field
= pcdev
->field
,
1532 .colorspace
= icd
->colorspace
,
1535 ret
= sh_mobile_ceu_set_fmt(icd
, &f
);
1536 if (!ret
&& (out_width
!= f
.fmt
.pix
.width
||
1537 out_height
!= f
.fmt
.pix
.height
))
1540 icd
->user_width
= out_width
& ~3;
1541 icd
->user_height
= out_height
& ~3;
1542 ret
= sh_mobile_ceu_set_bus_param(icd
);
1546 /* Thaw the queue */
1548 spin_lock_irq(&pcdev
->lock
);
1549 sh_mobile_ceu_capture(pcdev
);
1550 spin_unlock_irq(&pcdev
->lock
);
1551 /* Start the client */
1552 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 1);
1556 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1558 struct soc_camera_device
*icd
= file
->private_data
;
1560 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
1563 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1564 struct v4l2_capability
*cap
)
1566 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1567 strlcpy(cap
->driver
, "sh_mobile_ceu", sizeof(cap
->driver
));
1568 strlcpy(cap
->bus_info
, "platform:sh_mobile_ceu", sizeof(cap
->bus_info
));
1569 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1570 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1575 static int sh_mobile_ceu_init_videobuf(struct vb2_queue
*q
,
1576 struct soc_camera_device
*icd
)
1578 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1580 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1581 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1583 q
->ops
= &sh_mobile_ceu_videobuf_ops
;
1584 q
->mem_ops
= &vb2_dma_contig_memops
;
1585 q
->buf_struct_size
= sizeof(struct sh_mobile_ceu_buffer
);
1586 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1587 q
->lock
= &ici
->host_lock
;
1588 q
->dev
= ici
->v4l2_dev
.dev
;
1590 return vb2_queue_init(q
);
1593 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1594 .owner
= THIS_MODULE
,
1595 .add
= sh_mobile_ceu_add_device
,
1596 .remove
= sh_mobile_ceu_remove_device
,
1597 .clock_start
= sh_mobile_ceu_clock_start
,
1598 .clock_stop
= sh_mobile_ceu_clock_stop
,
1599 .get_formats
= sh_mobile_ceu_get_formats
,
1600 .put_formats
= sh_mobile_ceu_put_formats
,
1601 .get_selection
= sh_mobile_ceu_get_selection
,
1602 .set_selection
= sh_mobile_ceu_set_selection
,
1603 .set_liveselection
= sh_mobile_ceu_set_liveselection
,
1604 .set_fmt
= sh_mobile_ceu_set_fmt
,
1605 .try_fmt
= sh_mobile_ceu_try_fmt
,
1606 .poll
= sh_mobile_ceu_poll
,
1607 .querycap
= sh_mobile_ceu_querycap
,
1608 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1609 .init_videobuf2
= sh_mobile_ceu_init_videobuf
,
1613 struct notifier_block notifier
;
1614 struct completion completion
;
1618 static int bus_notify(struct notifier_block
*nb
,
1619 unsigned long action
, void *data
)
1621 struct device
*dev
= data
;
1622 struct bus_wait
*wait
= container_of(nb
, struct bus_wait
, notifier
);
1624 if (wait
->dev
!= dev
)
1628 case BUS_NOTIFY_UNBOUND_DRIVER
:
1629 /* Protect from module unloading */
1630 wait_for_completion(&wait
->completion
);
1636 static int sh_mobile_ceu_probe(struct platform_device
*pdev
)
1638 struct sh_mobile_ceu_dev
*pcdev
;
1639 struct resource
*res
;
1643 struct bus_wait wait
= {
1644 .completion
= COMPLETION_INITIALIZER_ONSTACK(wait
.completion
),
1645 .notifier
.notifier_call
= bus_notify
,
1648 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1649 irq
= platform_get_irq(pdev
, 0);
1650 if (!res
|| (int)irq
<= 0) {
1651 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1655 pcdev
= devm_kzalloc(&pdev
->dev
, sizeof(*pcdev
), GFP_KERNEL
);
1657 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1661 INIT_LIST_HEAD(&pcdev
->capture
);
1662 spin_lock_init(&pcdev
->lock
);
1663 init_completion(&pcdev
->complete
);
1665 pcdev
->pdata
= pdev
->dev
.platform_data
;
1666 if (!pcdev
->pdata
&& !pdev
->dev
.of_node
) {
1667 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1671 /* TODO: implement per-device bus flags */
1673 pcdev
->max_width
= pcdev
->pdata
->max_width
;
1674 pcdev
->max_height
= pcdev
->pdata
->max_height
;
1675 pcdev
->flags
= pcdev
->pdata
->flags
;
1677 pcdev
->field
= V4L2_FIELD_NONE
;
1679 if (!pcdev
->max_width
) {
1681 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-width", &v
);
1683 pcdev
->max_width
= v
;
1685 if (!pcdev
->max_width
)
1686 pcdev
->max_width
= 2560;
1688 if (!pcdev
->max_height
) {
1690 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-height", &v
);
1692 pcdev
->max_height
= v
;
1694 if (!pcdev
->max_height
)
1695 pcdev
->max_height
= 1920;
1698 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1700 return PTR_ERR(base
);
1704 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1706 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1708 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 static int __init
sh_mobile_ceu_init(void)
1806 return platform_driver_register(&sh_mobile_ceu_driver
);
1809 static void __exit
sh_mobile_ceu_exit(void)
1811 platform_driver_unregister(&sh_mobile_ceu_driver
);
1814 module_init(sh_mobile_ceu_init
);
1815 module_exit(sh_mobile_ceu_exit
);
1817 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1818 MODULE_AUTHOR("Magnus Damm");
1819 MODULE_LICENSE("GPL");
1820 MODULE_VERSION("0.1.0");
1821 MODULE_ALIAS("platform:sh_mobile_ceu");