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
;
454 struct vb2_v4l2_buffer
*vbuf
;
456 spin_lock_irq(&pcdev
->lock
);
458 pcdev
->active
= NULL
;
460 list_for_each_safe(buf_head
, tmp
, &pcdev
->capture
) {
461 vbuf
= &list_entry(buf_head
, struct sh_mobile_ceu_buffer
,
463 vb2_buffer_done(&vbuf
->vb2_buf
, VB2_BUF_STATE_DONE
);
464 list_del_init(buf_head
);
467 spin_unlock_irq(&pcdev
->lock
);
469 sh_mobile_ceu_soft_reset(pcdev
);
472 static const struct vb2_ops sh_mobile_ceu_videobuf_ops
= {
473 .queue_setup
= sh_mobile_ceu_videobuf_setup
,
474 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
475 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
476 .buf_cleanup
= sh_mobile_ceu_videobuf_release
,
477 .buf_init
= sh_mobile_ceu_videobuf_init
,
478 .wait_prepare
= vb2_ops_wait_prepare
,
479 .wait_finish
= vb2_ops_wait_finish
,
480 .stop_streaming
= sh_mobile_ceu_stop_streaming
,
483 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
485 struct sh_mobile_ceu_dev
*pcdev
= data
;
486 struct vb2_v4l2_buffer
*vbuf
;
489 spin_lock(&pcdev
->lock
);
491 vbuf
= pcdev
->active
;
493 /* Stale interrupt from a released buffer */
496 list_del_init(&to_ceu_vb(vbuf
)->queue
);
498 if (!list_empty(&pcdev
->capture
))
499 pcdev
->active
= &list_entry(pcdev
->capture
.next
,
500 struct sh_mobile_ceu_buffer
, queue
)->vb
;
502 pcdev
->active
= NULL
;
504 ret
= sh_mobile_ceu_capture(pcdev
);
505 vbuf
->vb2_buf
.timestamp
= ktime_get_ns();
507 vbuf
->field
= pcdev
->field
;
508 vbuf
->sequence
= pcdev
->sequence
++;
510 vb2_buffer_done(&vbuf
->vb2_buf
,
511 ret
< 0 ? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
514 spin_unlock(&pcdev
->lock
);
519 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
521 dev_info(icd
->parent
,
522 "SuperH Mobile CEU driver attached to camera %d\n",
528 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
530 dev_info(icd
->parent
,
531 "SuperH Mobile CEU driver detached from camera %d\n",
535 /* Called with .host_lock held */
536 static int sh_mobile_ceu_clock_start(struct soc_camera_host
*ici
)
538 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
540 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
542 pcdev
->buf_total
= 0;
544 sh_mobile_ceu_soft_reset(pcdev
);
549 /* Called with .host_lock held */
550 static void sh_mobile_ceu_clock_stop(struct soc_camera_host
*ici
)
552 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
554 /* disable capture, disable interrupts */
555 ceu_write(pcdev
, CEIER
, 0);
556 sh_mobile_ceu_soft_reset(pcdev
);
558 /* make sure active buffer is canceled */
559 spin_lock_irq(&pcdev
->lock
);
561 list_del_init(&to_ceu_vb(pcdev
->active
)->queue
);
562 vb2_buffer_done(&pcdev
->active
->vb2_buf
, VB2_BUF_STATE_ERROR
);
563 pcdev
->active
= NULL
;
565 spin_unlock_irq(&pcdev
->lock
);
567 pm_runtime_put(ici
->v4l2_dev
.dev
);
571 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
572 * in SH7722 Hardware Manual
574 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
576 unsigned int mant_pre
= scale
>> 12;
579 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
580 mant_pre
* 4096 / scale
+ 1;
583 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
590 scale
= (src
* 4096 / *dst
) & ~7;
592 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
595 *dst
= size_dst(src
, scale
);
600 /* rect is guaranteed to not exceed the scaled camera rectangle */
601 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
)
603 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
604 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
605 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
606 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
607 unsigned int left_offset
, top_offset
;
610 dev_geo(icd
->parent
, "Crop %ux%u@%u:%u\n",
611 icd
->user_width
, icd
->user_height
, cam
->ceu_left
, cam
->ceu_top
);
613 left_offset
= cam
->ceu_left
;
614 top_offset
= cam
->ceu_top
;
616 WARN_ON(icd
->user_width
& 3 || icd
->user_height
& 3);
618 width
= icd
->user_width
;
620 if (pcdev
->image_mode
) {
621 in_width
= cam
->width
;
622 if (!pcdev
->is_16bit
) {
627 unsigned int w_factor
;
629 switch (icd
->current_fmt
->host_fmt
->packing
) {
630 case SOC_MBUS_PACKING_2X8_PADHI
:
637 in_width
= cam
->width
* w_factor
;
638 left_offset
*= w_factor
;
641 cdwdr_width
= icd
->bytesperline
;
643 height
= icd
->user_height
;
644 in_height
= cam
->height
;
645 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
646 height
= (height
/ 2) & ~3;
652 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
653 camor
= left_offset
| (top_offset
<< 16);
656 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
657 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
660 ceu_write(pcdev
, CAMOR
, camor
);
661 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
662 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
663 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
664 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
667 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
669 u32 capsr
= ceu_read(pcdev
, CAPSR
);
670 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
674 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
676 unsigned long timeout
= jiffies
+ 10 * HZ
;
679 * Wait until the end of the current frame. It can take a long time,
680 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
682 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
685 if (time_after(jiffies
, timeout
)) {
686 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
687 "Timeout waiting for frame end! Interface problem?\n");
691 /* Wait until reset clears, this shall not hang... */
692 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
695 /* Anything to restore? */
696 if (capsr
& ~(1 << 16))
697 ceu_write(pcdev
, CAPSR
, capsr
);
700 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
701 V4L2_MBUS_PCLK_SAMPLE_RISING | \
702 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
703 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
704 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
705 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
706 V4L2_MBUS_DATA_ACTIVE_HIGH)
708 /* Capture is not running, no interrupts, no locking needed */
709 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
)
711 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
712 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
713 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
714 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
715 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
716 unsigned long value
, common_flags
= CEU_BUS_FLAGS
;
717 u32 capsr
= capture_save_reset(pcdev
);
718 unsigned int yuv_lineskip
;
722 * If the client doesn't implement g_mbus_config, we just use our
725 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
727 common_flags
= soc_mbus_config_compatible(&cfg
,
731 } else if (ret
!= -ENOIOCTLCMD
) {
735 /* Make choises, based on platform preferences */
736 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
737 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
738 if (pcdev
->flags
& SH_CEU_FLAG_HSYNC_LOW
)
739 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
741 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
744 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
745 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
746 if (pcdev
->flags
& SH_CEU_FLAG_VSYNC_LOW
)
747 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
749 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
752 cfg
.flags
= common_flags
;
753 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
754 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
757 if (icd
->current_fmt
->host_fmt
->bits_per_sample
> 8)
762 ceu_write(pcdev
, CRCNTR
, 0);
763 ceu_write(pcdev
, CRCMPR
, 0);
765 value
= 0x00000010; /* data fetch by default */
768 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
769 case V4L2_PIX_FMT_NV12
:
770 case V4L2_PIX_FMT_NV21
:
771 /* convert 4:2:2 -> 4:2:0 */
772 yuv_lineskip
= 0; /* skip for NV12/21, no skip for NV16/61 */
774 case V4L2_PIX_FMT_NV16
:
775 case V4L2_PIX_FMT_NV61
:
777 case MEDIA_BUS_FMT_UYVY8_2X8
:
778 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
780 case MEDIA_BUS_FMT_VYUY8_2X8
:
781 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
783 case MEDIA_BUS_FMT_YUYV8_2X8
:
784 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
786 case MEDIA_BUS_FMT_YVYU8_2X8
:
787 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
794 if (icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
795 icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
796 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
798 value
|= common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
799 value
|= common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
803 else if (pcdev
->flags
& SH_CEU_FLAG_LOWER_8BIT
)
806 ceu_write(pcdev
, CAMCR
, value
);
808 ceu_write(pcdev
, CAPCR
, 0x00300000);
810 switch (pcdev
->field
) {
811 case V4L2_FIELD_INTERLACED_TB
:
814 case V4L2_FIELD_INTERLACED_BT
:
821 ceu_write(pcdev
, CAIFR
, value
);
823 sh_mobile_ceu_set_rect(icd
);
826 dev_geo(icd
->parent
, "CFLCR 0x%x\n", pcdev
->cflcr
);
827 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
830 * A few words about byte order (observed in Big Endian mode)
832 * In data fetch mode bytes are received in chunks of 8 bytes.
833 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
835 * The data is however by default written to memory in reverse order:
836 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
838 * The lowest three bits of CDOCR allows us to do swapping,
839 * using 7 we swap the data bytes to match the incoming order:
840 * D0, D1, D2, D3, D4, D5, D6, D7
842 value
= 0x00000007 | yuv_lineskip
;
844 ceu_write(pcdev
, CDOCR
, value
);
845 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
847 capture_restore(pcdev
, capsr
);
849 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
853 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
854 unsigned char buswidth
)
856 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
857 unsigned long common_flags
= CEU_BUS_FLAGS
;
858 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
861 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
863 common_flags
= soc_mbus_config_compatible(&cfg
,
865 else if (ret
!= -ENOIOCTLCMD
)
868 if (!common_flags
|| buswidth
> 16)
874 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats
[] = {
876 .fourcc
= V4L2_PIX_FMT_NV12
,
878 .bits_per_sample
= 8,
879 .packing
= SOC_MBUS_PACKING_1_5X8
,
880 .order
= SOC_MBUS_ORDER_LE
,
881 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
883 .fourcc
= V4L2_PIX_FMT_NV21
,
885 .bits_per_sample
= 8,
886 .packing
= SOC_MBUS_PACKING_1_5X8
,
887 .order
= SOC_MBUS_ORDER_LE
,
888 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
890 .fourcc
= V4L2_PIX_FMT_NV16
,
892 .bits_per_sample
= 8,
893 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
894 .order
= SOC_MBUS_ORDER_LE
,
895 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
897 .fourcc
= V4L2_PIX_FMT_NV61
,
899 .bits_per_sample
= 8,
900 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
901 .order
= SOC_MBUS_ORDER_LE
,
902 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
906 /* This will be corrected as we get more formats */
907 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
909 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
910 (fmt
->bits_per_sample
== 8 &&
911 fmt
->packing
== SOC_MBUS_PACKING_1_5X8
) ||
912 (fmt
->bits_per_sample
== 8 &&
913 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
914 (fmt
->bits_per_sample
> 8 &&
915 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
918 static struct soc_camera_device
*ctrl_to_icd(struct v4l2_ctrl
*ctrl
)
920 return container_of(ctrl
->handler
, struct soc_camera_device
,
924 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl
*ctrl
)
926 struct soc_camera_device
*icd
= ctrl_to_icd(ctrl
);
927 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
928 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
931 case V4L2_CID_SHARPNESS
:
932 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
933 case V4L2_PIX_FMT_NV12
:
934 case V4L2_PIX_FMT_NV21
:
935 case V4L2_PIX_FMT_NV16
:
936 case V4L2_PIX_FMT_NV61
:
937 ceu_write(pcdev
, CLFCR
, !ctrl
->val
);
946 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops
= {
947 .s_ctrl
= sh_mobile_ceu_s_ctrl
,
950 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, unsigned int idx
,
951 struct soc_camera_format_xlate
*xlate
)
953 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
954 struct device
*dev
= icd
->parent
;
955 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
956 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
959 struct sh_mobile_ceu_cam
*cam
;
960 struct v4l2_subdev_mbus_code_enum code
= {
961 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
964 const struct soc_mbus_pixelfmt
*fmt
;
966 ret
= v4l2_subdev_call(sd
, pad
, enum_mbus_code
, NULL
, &code
);
968 /* No more formats */
971 fmt
= soc_mbus_get_fmtdesc(code
.code
);
973 dev_warn(dev
, "unsupported format code #%u: %d\n", idx
, code
.code
);
977 ret
= sh_mobile_ceu_try_bus_param(icd
, fmt
->bits_per_sample
);
981 if (!icd
->host_priv
) {
982 struct v4l2_subdev_format fmt
= {
983 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
985 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
986 struct v4l2_rect rect
;
989 /* Add our control */
990 v4l2_ctrl_new_std(&icd
->ctrl_handler
, &sh_mobile_ceu_ctrl_ops
,
991 V4L2_CID_SHARPNESS
, 0, 1, 1, 1);
992 if (icd
->ctrl_handler
.error
)
993 return icd
->ctrl_handler
.error
;
995 /* FIXME: subwindow is lost between close / open */
997 /* Cache current client geometry */
998 ret
= soc_camera_client_g_rect(sd
, &rect
);
1003 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1008 * All currently existing CEU implementations support 2560x1920
1009 * or larger frames. If the sensor is proposing too big a frame,
1010 * don't bother with possibly supportred by the CEU larger
1011 * sizes, just try VGA multiples. If needed, this can be
1012 * adjusted in the future.
1014 while ((mf
->width
> pcdev
->max_width
||
1015 mf
->height
> pcdev
->max_height
) && shift
< 4) {
1016 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1017 mf
->width
= 2560 >> shift
;
1018 mf
->height
= 1920 >> shift
;
1019 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1020 soc_camera_grp_id(icd
), pad
,
1021 set_fmt
, NULL
, &fmt
);
1028 dev_err(dev
, "Failed to configure the client below %ux%x\n",
1029 mf
->width
, mf
->height
);
1033 dev_geo(dev
, "camera fmt %ux%u\n", mf
->width
, mf
->height
);
1035 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1039 /* We are called with current camera crop, initialise subrect with it */
1041 cam
->subrect
= rect
;
1043 cam
->width
= mf
->width
;
1044 cam
->height
= mf
->height
;
1046 icd
->host_priv
= cam
;
1048 cam
= icd
->host_priv
;
1051 /* Beginning of a pass */
1053 cam
->extra_fmt
= NULL
;
1055 switch (code
.code
) {
1056 case MEDIA_BUS_FMT_UYVY8_2X8
:
1057 case MEDIA_BUS_FMT_VYUY8_2X8
:
1058 case MEDIA_BUS_FMT_YUYV8_2X8
:
1059 case MEDIA_BUS_FMT_YVYU8_2X8
:
1064 * Our case is simple so far: for any of the above four camera
1065 * formats we add all our four synthesized NV* formats, so,
1066 * just marking the device with a single flag suffices. If
1067 * the format generation rules are more complex, you would have
1068 * to actually hang your already added / counted formats onto
1069 * the host_priv pointer and check whether the format you're
1070 * going to add now is already there.
1072 cam
->extra_fmt
= sh_mobile_ceu_formats
;
1074 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
1076 for (k
= 0; xlate
&& k
< n
; k
++) {
1077 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
1078 xlate
->code
= code
.code
;
1080 dev_dbg(dev
, "Providing format %s using code %d\n",
1081 sh_mobile_ceu_formats
[k
].name
, code
.code
);
1085 if (!sh_mobile_ceu_packing_supported(fmt
))
1089 /* Generic pass-through */
1092 xlate
->host_fmt
= fmt
;
1093 xlate
->code
= code
.code
;
1095 dev_dbg(dev
, "Providing format %s in pass-through mode\n",
1102 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
1104 kfree(icd
->host_priv
);
1105 icd
->host_priv
= NULL
;
1108 #define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale)
1109 #define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out)
1112 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1113 * framerate by always requesting the maximum image from the client. See
1114 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1115 * scaling and cropping algorithms and for the meaning of referenced here steps.
1117 static int sh_mobile_ceu_set_selection(struct soc_camera_device
*icd
,
1118 struct v4l2_selection
*sel
)
1120 struct v4l2_rect
*rect
= &sel
->r
;
1121 struct device
*dev
= icd
->parent
;
1122 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1123 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1124 struct v4l2_selection cam_sel
;
1125 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1126 struct v4l2_rect
*cam_rect
= &cam_sel
.r
;
1127 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1128 struct v4l2_subdev_format fmt
= {
1129 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1131 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
1132 unsigned int scale_cam_h
, scale_cam_v
, scale_ceu_h
, scale_ceu_v
,
1133 out_width
, out_height
;
1134 int interm_width
, interm_height
;
1138 dev_geo(dev
, "S_SELECTION(%ux%u@%u:%u)\n", rect
->width
, rect
->height
,
1139 rect
->left
, rect
->top
);
1141 /* During camera cropping its output window can change too, stop CEU */
1142 capsr
= capture_save_reset(pcdev
);
1143 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1146 * 1. - 2. Apply iterative camera S_SELECTION for new input window, read back
1147 * actual camera rectangle.
1149 ret
= soc_camera_client_s_selection(sd
, sel
, &cam_sel
,
1150 &cam
->rect
, &cam
->subrect
);
1154 dev_geo(dev
, "1-2: camera cropped to %ux%u@%u:%u\n",
1155 cam_rect
->width
, cam_rect
->height
,
1156 cam_rect
->left
, cam_rect
->top
);
1158 /* On success cam_crop contains current camera crop */
1160 /* 3. Retrieve camera output window */
1161 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
);
1165 if (mf
->width
> pcdev
->max_width
|| mf
->height
> pcdev
->max_height
)
1168 /* 4. Calculate camera scales */
1169 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1170 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1172 /* Calculate intermediate window */
1173 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1174 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1176 if (interm_width
< icd
->user_width
) {
1179 new_scale_h
= calc_generic_scale(rect
->width
, icd
->user_width
);
1181 mf
->width
= scale_down(cam_rect
->width
, new_scale_h
);
1184 if (interm_height
< icd
->user_height
) {
1187 new_scale_v
= calc_generic_scale(rect
->height
, icd
->user_height
);
1189 mf
->height
= scale_down(cam_rect
->height
, new_scale_v
);
1192 if (interm_width
< icd
->user_width
|| interm_height
< icd
->user_height
) {
1193 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1194 soc_camera_grp_id(icd
), pad
,
1195 set_fmt
, NULL
, &fmt
);
1199 dev_geo(dev
, "New camera output %ux%u\n", mf
->width
, mf
->height
);
1200 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
->width
);
1201 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
->height
);
1202 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1203 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1206 /* Cache camera output window */
1207 cam
->width
= mf
->width
;
1208 cam
->height
= mf
->height
;
1210 if (pcdev
->image_mode
) {
1211 out_width
= min(interm_width
, icd
->user_width
);
1212 out_height
= min(interm_height
, icd
->user_height
);
1214 out_width
= interm_width
;
1215 out_height
= interm_height
;
1219 * 5. Calculate CEU scales from camera scales from results of (5) and
1222 scale_ceu_h
= calc_scale(interm_width
, &out_width
);
1223 scale_ceu_v
= calc_scale(interm_height
, &out_height
);
1225 dev_geo(dev
, "5: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1227 /* Apply CEU scales. */
1228 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1229 if (cflcr
!= pcdev
->cflcr
) {
1230 pcdev
->cflcr
= cflcr
;
1231 ceu_write(pcdev
, CFLCR
, cflcr
);
1234 icd
->user_width
= out_width
& ~3;
1235 icd
->user_height
= out_height
& ~3;
1236 /* Offsets are applied at the CEU scaling filter input */
1237 cam
->ceu_left
= scale_down(rect
->left
- cam_rect
->left
, scale_cam_h
) & ~1;
1238 cam
->ceu_top
= scale_down(rect
->top
- cam_rect
->top
, scale_cam_v
) & ~1;
1240 /* 6. Use CEU cropping to crop to the new window. */
1241 sh_mobile_ceu_set_rect(icd
);
1243 cam
->subrect
= *rect
;
1245 dev_geo(dev
, "6: CEU cropped to %ux%u@%u:%u\n",
1246 icd
->user_width
, icd
->user_height
,
1247 cam
->ceu_left
, cam
->ceu_top
);
1249 /* Restore capture. The CE bit can be cleared by the hardware */
1252 capture_restore(pcdev
, capsr
);
1254 /* Even if only camera cropping succeeded */
1258 static int sh_mobile_ceu_get_selection(struct soc_camera_device
*icd
,
1259 struct v4l2_selection
*sel
)
1261 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1263 sel
->r
= cam
->subrect
;
1268 /* Similar to set_crop multistage iterative algorithm */
1269 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1270 struct v4l2_format
*f
)
1272 struct device
*dev
= icd
->parent
;
1273 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1274 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1275 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1276 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1277 struct v4l2_mbus_framefmt mf
;
1278 __u32 pixfmt
= pix
->pixelformat
;
1279 const struct soc_camera_format_xlate
*xlate
;
1280 unsigned int ceu_sub_width
= pcdev
->max_width
,
1281 ceu_sub_height
= pcdev
->max_height
;
1282 u16 scale_v
, scale_h
;
1285 enum v4l2_field field
;
1287 switch (pix
->field
) {
1289 pix
->field
= V4L2_FIELD_NONE
;
1291 case V4L2_FIELD_INTERLACED_TB
:
1292 case V4L2_FIELD_INTERLACED_BT
:
1293 case V4L2_FIELD_NONE
:
1296 case V4L2_FIELD_INTERLACED
:
1297 field
= V4L2_FIELD_INTERLACED_TB
;
1301 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1303 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1307 /* 1.-4. Calculate desired client output geometry */
1308 soc_camera_calc_client_output(icd
, &cam
->rect
, &cam
->subrect
, pix
, &mf
, 12);
1309 mf
.field
= pix
->field
;
1310 mf
.colorspace
= pix
->colorspace
;
1311 mf
.code
= xlate
->code
;
1314 case V4L2_PIX_FMT_NV12
:
1315 case V4L2_PIX_FMT_NV21
:
1316 case V4L2_PIX_FMT_NV16
:
1317 case V4L2_PIX_FMT_NV61
:
1324 dev_geo(dev
, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt
, mf
.field
, mf
.code
,
1325 pix
->width
, pix
->height
);
1327 dev_geo(dev
, "4: request camera output %ux%u\n", mf
.width
, mf
.height
);
1330 ret
= soc_camera_client_scale(icd
, &cam
->rect
, &cam
->subrect
,
1331 &mf
, &ceu_sub_width
, &ceu_sub_height
,
1332 image_mode
&& V4L2_FIELD_NONE
== field
, 12);
1334 dev_geo(dev
, "5-9: client scale return %d\n", ret
);
1336 /* Done with the camera. Now see if we can improve the result */
1338 dev_geo(dev
, "fmt %ux%u, requested %ux%u\n",
1339 mf
.width
, mf
.height
, pix
->width
, pix
->height
);
1343 if (mf
.code
!= xlate
->code
)
1346 /* 9. Prepare CEU crop */
1347 cam
->width
= mf
.width
;
1348 cam
->height
= mf
.height
;
1350 /* 10. Use CEU scaling to scale to the requested user window. */
1352 /* We cannot scale up */
1353 if (pix
->width
> ceu_sub_width
)
1354 ceu_sub_width
= pix
->width
;
1356 if (pix
->height
> ceu_sub_height
)
1357 ceu_sub_height
= pix
->height
;
1359 pix
->colorspace
= mf
.colorspace
;
1362 /* Scale pix->{width x height} down to width x height */
1363 scale_h
= calc_scale(ceu_sub_width
, &pix
->width
);
1364 scale_v
= calc_scale(ceu_sub_height
, &pix
->height
);
1366 pix
->width
= ceu_sub_width
;
1367 pix
->height
= ceu_sub_height
;
1372 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1375 * We have calculated CFLCR, the actual configuration will be performed
1376 * in sh_mobile_ceu_set_bus_param()
1379 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1380 ceu_sub_width
, scale_h
, pix
->width
,
1381 ceu_sub_height
, scale_v
, pix
->height
);
1383 cam
->code
= xlate
->code
;
1384 icd
->current_fmt
= xlate
;
1386 pcdev
->field
= field
;
1387 pcdev
->image_mode
= image_mode
;
1389 /* CFSZR requirement */
1396 #define CEU_CHDW_MAX 8188U /* Maximum line stride */
1398 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1399 struct v4l2_format
*f
)
1401 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1402 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1403 const struct soc_camera_format_xlate
*xlate
;
1404 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1405 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1406 struct v4l2_subdev_pad_config pad_cfg
;
1407 struct v4l2_subdev_format format
= {
1408 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1410 struct v4l2_mbus_framefmt
*mf
= &format
.format
;
1411 __u32 pixfmt
= pix
->pixelformat
;
1415 dev_geo(icd
->parent
, "TRY_FMT(pix=0x%x, %ux%u)\n",
1416 pixfmt
, pix
->width
, pix
->height
);
1418 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1420 xlate
= icd
->current_fmt
;
1421 dev_dbg(icd
->parent
, "Format %x not found, keeping %x\n",
1422 pixfmt
, xlate
->host_fmt
->fourcc
);
1423 pixfmt
= xlate
->host_fmt
->fourcc
;
1424 pix
->pixelformat
= pixfmt
;
1425 pix
->colorspace
= icd
->colorspace
;
1428 /* FIXME: calculate using depth and bus width */
1430 /* CFSZR requires height and width to be 4-pixel aligned */
1431 v4l_bound_align_image(&pix
->width
, 2, pcdev
->max_width
, 2,
1432 &pix
->height
, 4, pcdev
->max_height
, 2, 0);
1435 height
= pix
->height
;
1437 /* limit to sensor capabilities */
1438 mf
->width
= pix
->width
;
1439 mf
->height
= pix
->height
;
1440 mf
->field
= pix
->field
;
1441 mf
->code
= xlate
->code
;
1442 mf
->colorspace
= pix
->colorspace
;
1444 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
, soc_camera_grp_id(icd
),
1445 pad
, set_fmt
, &pad_cfg
, &format
);
1449 pix
->width
= mf
->width
;
1450 pix
->height
= mf
->height
;
1451 pix
->field
= mf
->field
;
1452 pix
->colorspace
= mf
->colorspace
;
1455 case V4L2_PIX_FMT_NV12
:
1456 case V4L2_PIX_FMT_NV21
:
1457 case V4L2_PIX_FMT_NV16
:
1458 case V4L2_PIX_FMT_NV61
:
1459 /* FIXME: check against rect_max after converting soc-camera */
1460 /* We can scale precisely, need a bigger image from camera */
1461 if (pix
->width
< width
|| pix
->height
< height
) {
1463 * We presume, the sensor behaves sanely, i.e., if
1464 * requested a bigger rectangle, it will not return a
1467 mf
->width
= pcdev
->max_width
;
1468 mf
->height
= pcdev
->max_height
;
1469 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1470 soc_camera_grp_id(icd
), pad
,
1471 set_fmt
, &pad_cfg
, &format
);
1473 /* Shouldn't actually happen... */
1474 dev_err(icd
->parent
,
1475 "FIXME: client try_fmt() = %d\n", ret
);
1479 /* We will scale exactly */
1480 if (mf
->width
> width
)
1482 if (mf
->height
> height
)
1483 pix
->height
= height
;
1485 pix
->bytesperline
= max(pix
->bytesperline
, pix
->width
);
1486 pix
->bytesperline
= min(pix
->bytesperline
, CEU_CHDW_MAX
);
1487 pix
->bytesperline
&= ~3;
1491 /* Configurable stride isn't supported in pass-through mode. */
1492 pix
->bytesperline
= 0;
1499 dev_geo(icd
->parent
, "%s(): return %d, fmt 0x%x, %ux%u\n",
1500 __func__
, ret
, pix
->pixelformat
, pix
->width
, pix
->height
);
1505 static int sh_mobile_ceu_set_liveselection(struct soc_camera_device
*icd
,
1506 struct v4l2_selection
*sel
)
1508 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1509 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1510 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1511 u32 out_width
= icd
->user_width
, out_height
= icd
->user_height
;
1516 /* Wait for frame */
1517 ret
= wait_for_completion_interruptible(&pcdev
->complete
);
1518 /* Stop the client */
1519 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 0);
1521 dev_warn(icd
->parent
,
1522 "Client failed to stop the stream: %d\n", ret
);
1524 /* Do the crop, if it fails, there's nothing more we can do */
1525 sh_mobile_ceu_set_selection(icd
, sel
);
1527 dev_geo(icd
->parent
, "Output after crop: %ux%u\n", icd
->user_width
, icd
->user_height
);
1529 if (icd
->user_width
!= out_width
|| icd
->user_height
!= out_height
) {
1530 struct v4l2_format f
= {
1531 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1534 .height
= out_height
,
1535 .pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
,
1536 .field
= pcdev
->field
,
1537 .colorspace
= icd
->colorspace
,
1540 ret
= sh_mobile_ceu_set_fmt(icd
, &f
);
1541 if (!ret
&& (out_width
!= f
.fmt
.pix
.width
||
1542 out_height
!= f
.fmt
.pix
.height
))
1545 icd
->user_width
= out_width
& ~3;
1546 icd
->user_height
= out_height
& ~3;
1547 ret
= sh_mobile_ceu_set_bus_param(icd
);
1551 /* Thaw the queue */
1553 spin_lock_irq(&pcdev
->lock
);
1554 sh_mobile_ceu_capture(pcdev
);
1555 spin_unlock_irq(&pcdev
->lock
);
1556 /* Start the client */
1557 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 1);
1561 static __poll_t
sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
1563 struct soc_camera_device
*icd
= file
->private_data
;
1565 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
1568 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
1569 struct v4l2_capability
*cap
)
1571 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
1572 strlcpy(cap
->driver
, "sh_mobile_ceu", sizeof(cap
->driver
));
1573 strlcpy(cap
->bus_info
, "platform:sh_mobile_ceu", sizeof(cap
->bus_info
));
1574 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1575 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1580 static int sh_mobile_ceu_init_videobuf(struct vb2_queue
*q
,
1581 struct soc_camera_device
*icd
)
1583 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1585 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1586 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1588 q
->ops
= &sh_mobile_ceu_videobuf_ops
;
1589 q
->mem_ops
= &vb2_dma_contig_memops
;
1590 q
->buf_struct_size
= sizeof(struct sh_mobile_ceu_buffer
);
1591 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1592 q
->lock
= &ici
->host_lock
;
1593 q
->dev
= ici
->v4l2_dev
.dev
;
1595 return vb2_queue_init(q
);
1598 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
1599 .owner
= THIS_MODULE
,
1600 .add
= sh_mobile_ceu_add_device
,
1601 .remove
= sh_mobile_ceu_remove_device
,
1602 .clock_start
= sh_mobile_ceu_clock_start
,
1603 .clock_stop
= sh_mobile_ceu_clock_stop
,
1604 .get_formats
= sh_mobile_ceu_get_formats
,
1605 .put_formats
= sh_mobile_ceu_put_formats
,
1606 .get_selection
= sh_mobile_ceu_get_selection
,
1607 .set_selection
= sh_mobile_ceu_set_selection
,
1608 .set_liveselection
= sh_mobile_ceu_set_liveselection
,
1609 .set_fmt
= sh_mobile_ceu_set_fmt
,
1610 .try_fmt
= sh_mobile_ceu_try_fmt
,
1611 .poll
= sh_mobile_ceu_poll
,
1612 .querycap
= sh_mobile_ceu_querycap
,
1613 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
1614 .init_videobuf2
= sh_mobile_ceu_init_videobuf
,
1618 struct notifier_block notifier
;
1619 struct completion completion
;
1623 static int bus_notify(struct notifier_block
*nb
,
1624 unsigned long action
, void *data
)
1626 struct device
*dev
= data
;
1627 struct bus_wait
*wait
= container_of(nb
, struct bus_wait
, notifier
);
1629 if (wait
->dev
!= dev
)
1633 case BUS_NOTIFY_UNBOUND_DRIVER
:
1634 /* Protect from module unloading */
1635 wait_for_completion(&wait
->completion
);
1641 static int sh_mobile_ceu_probe(struct platform_device
*pdev
)
1643 struct sh_mobile_ceu_dev
*pcdev
;
1644 struct resource
*res
;
1648 struct bus_wait wait
= {
1649 .completion
= COMPLETION_INITIALIZER_ONSTACK(wait
.completion
),
1650 .notifier
.notifier_call
= bus_notify
,
1653 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1654 irq
= platform_get_irq(pdev
, 0);
1655 if (!res
|| (int)irq
<= 0) {
1656 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
1660 pcdev
= devm_kzalloc(&pdev
->dev
, sizeof(*pcdev
), GFP_KERNEL
);
1662 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1666 INIT_LIST_HEAD(&pcdev
->capture
);
1667 spin_lock_init(&pcdev
->lock
);
1668 init_completion(&pcdev
->complete
);
1670 pcdev
->pdata
= pdev
->dev
.platform_data
;
1671 if (!pcdev
->pdata
&& !pdev
->dev
.of_node
) {
1672 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
1676 /* TODO: implement per-device bus flags */
1678 pcdev
->max_width
= pcdev
->pdata
->max_width
;
1679 pcdev
->max_height
= pcdev
->pdata
->max_height
;
1680 pcdev
->flags
= pcdev
->pdata
->flags
;
1682 pcdev
->field
= V4L2_FIELD_NONE
;
1684 if (!pcdev
->max_width
) {
1686 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-width", &v
);
1688 pcdev
->max_width
= v
;
1690 if (!pcdev
->max_width
)
1691 pcdev
->max_width
= 2560;
1693 if (!pcdev
->max_height
) {
1695 err
= of_property_read_u32(pdev
->dev
.of_node
, "renesas,max-height", &v
);
1697 pcdev
->max_height
= v
;
1699 if (!pcdev
->max_height
)
1700 pcdev
->max_height
= 1920;
1703 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1705 return PTR_ERR(base
);
1709 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
1711 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1713 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
1716 DMA_MEMORY_EXCLUSIVE
);
1718 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
1722 pcdev
->video_limit
= resource_size(res
);
1726 err
= devm_request_irq(&pdev
->dev
, pcdev
->irq
, sh_mobile_ceu_irq
,
1727 0, dev_name(&pdev
->dev
), pcdev
);
1729 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
1730 goto exit_release_mem
;
1733 pm_suspend_ignore_children(&pdev
->dev
, true);
1734 pm_runtime_enable(&pdev
->dev
);
1735 pm_runtime_resume(&pdev
->dev
);
1737 pcdev
->ici
.priv
= pcdev
;
1738 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
1739 pcdev
->ici
.nr
= pdev
->id
;
1740 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
1741 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
1742 pcdev
->ici
.capabilities
= SOCAM_HOST_CAP_STRIDE
;
1744 if (pcdev
->pdata
&& pcdev
->pdata
->asd_sizes
) {
1745 pcdev
->ici
.asd
= pcdev
->pdata
->asd
;
1746 pcdev
->ici
.asd_sizes
= pcdev
->pdata
->asd_sizes
;
1749 err
= soc_camera_host_register(&pcdev
->ici
);
1756 pm_runtime_disable(&pdev
->dev
);
1758 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1759 dma_release_declared_memory(&pdev
->dev
);
1763 static int sh_mobile_ceu_remove(struct platform_device
*pdev
)
1765 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1767 soc_camera_host_unregister(soc_host
);
1768 pm_runtime_disable(&pdev
->dev
);
1769 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
1770 dma_release_declared_memory(&pdev
->dev
);
1775 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
1777 /* Runtime PM callback shared between ->runtime_suspend()
1778 * and ->runtime_resume(). Simply returns success.
1780 * This driver re-initializes all registers after
1781 * pm_runtime_get_sync() anyway so there is no need
1782 * to save and restore registers here.
1787 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
1788 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
1789 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
1792 static const struct of_device_id sh_mobile_ceu_of_match
[] = {
1793 { .compatible
= "renesas,sh-mobile-ceu" },
1796 MODULE_DEVICE_TABLE(of
, sh_mobile_ceu_of_match
);
1798 static struct platform_driver sh_mobile_ceu_driver
= {
1800 .name
= "sh_mobile_ceu",
1801 .pm
= &sh_mobile_ceu_dev_pm_ops
,
1802 .of_match_table
= sh_mobile_ceu_of_match
,
1804 .probe
= sh_mobile_ceu_probe
,
1805 .remove
= sh_mobile_ceu_remove
,
1808 module_platform_driver(sh_mobile_ceu_driver
);
1810 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1811 MODULE_AUTHOR("Magnus Damm");
1812 MODULE_LICENSE("GPL");
1813 MODULE_VERSION("0.1.0");
1814 MODULE_ALIAS("platform:sh_mobile_ceu");