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/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
28 #include <linux/moduleparam.h>
29 #include <linux/time.h>
30 #include <linux/slab.h>
31 #include <linux/device.h>
32 #include <linux/platform_device.h>
33 #include <linux/videodev2.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/sched.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-dev.h>
39 #include <media/soc_camera.h>
40 #include <media/sh_mobile_ceu.h>
41 #include <media/sh_mobile_csi2.h>
42 #include <media/videobuf2-dma-contig.h>
43 #include <media/v4l2-mediabus.h>
44 #include <media/soc_mediabus.h>
46 /* register offsets for sh7722 / sh7723 */
48 #define CAPSR 0x00 /* Capture start register */
49 #define CAPCR 0x04 /* Capture control register */
50 #define CAMCR 0x08 /* Capture interface control register */
51 #define CMCYR 0x0c /* Capture interface cycle register */
52 #define CAMOR 0x10 /* Capture interface offset register */
53 #define CAPWR 0x14 /* Capture interface width register */
54 #define CAIFR 0x18 /* Capture interface input format register */
55 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
56 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
57 #define CRCNTR 0x28 /* CEU register control register */
58 #define CRCMPR 0x2c /* CEU register forcible control register */
59 #define CFLCR 0x30 /* Capture filter control register */
60 #define CFSZR 0x34 /* Capture filter size clip register */
61 #define CDWDR 0x38 /* Capture destination width register */
62 #define CDAYR 0x3c /* Capture data address Y register */
63 #define CDACR 0x40 /* Capture data address C register */
64 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
65 #define CDBCR 0x48 /* Capture data bottom-field address C register */
66 #define CBDSR 0x4c /* Capture bundle destination size register */
67 #define CFWCR 0x5c /* Firewall operation control register */
68 #define CLFCR 0x60 /* Capture low-pass filter control register */
69 #define CDOCR 0x64 /* Capture data output control register */
70 #define CDDCR 0x68 /* Capture data complexity level register */
71 #define CDDAR 0x6c /* Capture data complexity level address register */
72 #define CEIER 0x70 /* Capture event interrupt enable register */
73 #define CETCR 0x74 /* Capture event flag clear register */
74 #define CSTSR 0x7c /* Capture status register */
75 #define CSRTR 0x80 /* Capture software reset register */
76 #define CDSSR 0x84 /* Capture data size register */
77 #define CDAYR2 0x90 /* Capture data address Y register 2 */
78 #define CDACR2 0x94 /* Capture data address C register 2 */
79 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
80 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
84 #define dev_geo dev_info
86 #define dev_geo dev_dbg
89 /* per video frame buffer */
90 struct sh_mobile_ceu_buffer
{
91 struct vb2_buffer vb
; /* v4l buffer must be first */
92 struct list_head queue
;
95 struct sh_mobile_ceu_dev
{
96 struct soc_camera_host ici
;
97 struct soc_camera_device
*icd
;
98 struct platform_device
*csi2_pdev
;
105 spinlock_t lock
; /* Protects video buffer lists */
106 struct list_head capture
;
107 struct vb2_buffer
*active
;
108 struct vb2_alloc_ctx
*alloc_ctx
;
110 struct sh_mobile_ceu_info
*pdata
;
111 struct completion complete
;
115 /* static max sizes either from platform data or default */
119 enum v4l2_field field
;
122 unsigned int image_mode
:1;
123 unsigned int is_16bit
:1;
124 unsigned int frozen
:1;
127 struct sh_mobile_ceu_cam
{
128 /* CEU offsets within the camera output, before the CEU scaler */
129 unsigned int ceu_left
;
130 unsigned int ceu_top
;
131 /* Client output, as seen by the CEU */
135 * User window from S_CROP / G_CROP, produced by client cropping and
136 * scaling, CEU scaling and CEU cropping, mapped back onto the client
139 struct v4l2_rect subrect
;
140 /* Camera cropping rectangle */
141 struct v4l2_rect rect
;
142 const struct soc_mbus_pixelfmt
*extra_fmt
;
143 enum v4l2_mbus_pixelcode code
;
146 static struct sh_mobile_ceu_buffer
*to_ceu_vb(struct vb2_buffer
*vb
)
148 return container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
151 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
152 unsigned long reg_offs
, u32 data
)
154 iowrite32(data
, priv
->base
+ reg_offs
);
157 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
159 return ioread32(priv
->base
+ reg_offs
);
162 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev
*pcdev
)
165 struct soc_camera_device
*icd
= pcdev
->icd
;
167 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
169 /* wait CSTSR.CPTON bit */
170 for (i
= 0; i
< 1000; i
++) {
171 if (!(ceu_read(pcdev
, CSTSR
) & 1)) {
178 /* wait CAPSR.CPKIL bit */
179 for (i
= 0; i
< 1000; i
++) {
180 if (!(ceu_read(pcdev
, CAPSR
) & (1 << 16))) {
189 dev_warn(icd
->pdev
, "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 const struct v4l2_format
*fmt
,
207 unsigned int *count
, unsigned int *num_planes
,
208 unsigned int sizes
[], void *alloc_ctxs
[])
210 struct soc_camera_device
*icd
= container_of(vq
, struct soc_camera_device
, vb2_vidq
);
211 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
212 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
215 const struct soc_camera_format_xlate
*xlate
= soc_camera_xlate_by_fourcc(icd
,
216 fmt
->fmt
.pix
.pixelformat
);
217 unsigned int bytes_per_line
;
223 ret
= soc_mbus_bytes_per_line(fmt
->fmt
.pix
.width
,
228 bytes_per_line
= max_t(u32
, fmt
->fmt
.pix
.bytesperline
, ret
);
230 ret
= soc_mbus_image_size(xlate
->host_fmt
, bytes_per_line
,
231 fmt
->fmt
.pix
.height
);
235 sizes
[0] = max_t(u32
, fmt
->fmt
.pix
.sizeimage
, ret
);
237 /* Called from VIDIOC_REQBUFS or in compatibility mode */
238 sizes
[0] = icd
->sizeimage
;
241 alloc_ctxs
[0] = pcdev
->alloc_ctx
;
243 if (!vq
->num_buffers
)
249 /* If *num_planes != 0, we have already verified *count. */
250 if (pcdev
->video_limit
&& !*num_planes
) {
251 size_t size
= PAGE_ALIGN(sizes
[0]) * *count
;
253 if (size
+ pcdev
->buf_total
> pcdev
->video_limit
)
254 *count
= (pcdev
->video_limit
- pcdev
->buf_total
) /
255 PAGE_ALIGN(sizes
[0]);
260 dev_dbg(icd
->parent
, "count=%d, size=%u\n", *count
, sizes
[0]);
265 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
266 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
267 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
268 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
269 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
270 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
274 * return value doesn't reflex the success/failure to queue the new buffer,
275 * but rather the status of the previous buffer.
277 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
279 struct soc_camera_device
*icd
= pcdev
->icd
;
280 dma_addr_t phys_addr_top
, phys_addr_bottom
;
281 unsigned long top1
, top2
;
282 unsigned long bottom1
, bottom2
;
288 * The hardware is _very_ picky about this sequence. Especially
289 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
290 * several not-so-well documented interrupt sources in CETCR.
292 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_MASK
);
293 status
= ceu_read(pcdev
, CETCR
);
294 ceu_write(pcdev
, CETCR
, ~status
& CEU_CETCR_MAGIC
);
296 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_MASK
);
297 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
298 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
301 * When a VBP interrupt occurs, a capture end interrupt does not occur
302 * and the image of that frame is not captured correctly. So, soft reset
305 if (status
& CEU_CEIER_VBP
) {
306 sh_mobile_ceu_soft_reset(pcdev
);
311 complete(&pcdev
->complete
);
318 if (V4L2_FIELD_INTERLACED_BT
== pcdev
->field
) {
330 phys_addr_top
= vb2_dma_contig_plane_dma_addr(pcdev
->active
, 0);
332 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
333 case V4L2_PIX_FMT_NV12
:
334 case V4L2_PIX_FMT_NV21
:
335 case V4L2_PIX_FMT_NV16
:
336 case V4L2_PIX_FMT_NV61
:
343 ceu_write(pcdev
, top1
, phys_addr_top
);
344 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
345 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
346 ceu_write(pcdev
, bottom1
, phys_addr_bottom
);
350 phys_addr_top
+= icd
->bytesperline
* icd
->user_height
;
351 ceu_write(pcdev
, top2
, phys_addr_top
);
352 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
353 phys_addr_bottom
= phys_addr_top
+ icd
->bytesperline
;
354 ceu_write(pcdev
, bottom2
, phys_addr_bottom
);
358 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
363 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer
*vb
)
365 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vb
);
367 /* Added list head initialization on alloc */
368 WARN(!list_empty(&buf
->queue
), "Buffer %p on queue!\n", vb
);
373 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer
*vb
)
375 struct soc_camera_device
*icd
= container_of(vb
->vb2_queue
, struct soc_camera_device
, vb2_vidq
);
376 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
377 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
378 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vb
);
381 size
= icd
->sizeimage
;
383 if (vb2_plane_size(vb
, 0) < size
) {
384 dev_err(icd
->parent
, "Buffer #%d too small (%lu < %lu)\n",
385 vb
->v4l2_buf
.index
, vb2_plane_size(vb
, 0), size
);
389 vb2_set_plane_payload(vb
, 0, size
);
391 dev_dbg(icd
->parent
, "%s (vb=0x%p) 0x%p %lu\n", __func__
,
392 vb
, vb2_plane_vaddr(vb
, 0), vb2_get_plane_payload(vb
, 0));
396 * This can be useful if you want to see if we actually fill
397 * the buffer with something
399 if (vb2_plane_vaddr(vb
, 0))
400 memset(vb2_plane_vaddr(vb
, 0), 0xaa, vb2_get_plane_payload(vb
, 0));
403 spin_lock_irq(&pcdev
->lock
);
404 list_add_tail(&buf
->queue
, &pcdev
->capture
);
406 if (!pcdev
->active
) {
408 * Because there were no active buffer at this moment,
409 * we are not interested in the return value of
410 * sh_mobile_ceu_capture here.
413 sh_mobile_ceu_capture(pcdev
);
415 spin_unlock_irq(&pcdev
->lock
);
420 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
423 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer
*vb
)
425 struct soc_camera_device
*icd
= container_of(vb
->vb2_queue
, struct soc_camera_device
, vb2_vidq
);
426 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
427 struct sh_mobile_ceu_buffer
*buf
= to_ceu_vb(vb
);
428 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
430 spin_lock_irq(&pcdev
->lock
);
432 if (pcdev
->active
== vb
) {
433 /* disable capture (release DMA buffer), reset */
434 ceu_write(pcdev
, CAPSR
, 1 << 16);
435 pcdev
->active
= NULL
;
439 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
440 * buffer failed, and .buf_init() hasn't been called
443 list_del_init(&buf
->queue
);
445 pcdev
->buf_total
-= PAGE_ALIGN(vb2_plane_size(vb
, 0));
446 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
449 spin_unlock_irq(&pcdev
->lock
);
452 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer
*vb
)
454 struct soc_camera_device
*icd
= container_of(vb
->vb2_queue
, struct soc_camera_device
, vb2_vidq
);
455 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
456 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
458 pcdev
->buf_total
+= PAGE_ALIGN(vb2_plane_size(vb
, 0));
459 dev_dbg(icd
->parent
, "%s() %zu bytes buffers\n", __func__
,
462 /* This is for locking debugging only */
463 INIT_LIST_HEAD(&to_ceu_vb(vb
)->queue
);
467 static int sh_mobile_ceu_stop_streaming(struct vb2_queue
*q
)
469 struct soc_camera_device
*icd
= container_of(q
, struct soc_camera_device
, vb2_vidq
);
470 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
471 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
472 struct list_head
*buf_head
, *tmp
;
474 spin_lock_irq(&pcdev
->lock
);
476 pcdev
->active
= NULL
;
478 list_for_each_safe(buf_head
, tmp
, &pcdev
->capture
)
479 list_del_init(buf_head
);
481 spin_unlock_irq(&pcdev
->lock
);
483 return sh_mobile_ceu_soft_reset(pcdev
);
486 static struct vb2_ops sh_mobile_ceu_videobuf_ops
= {
487 .queue_setup
= sh_mobile_ceu_videobuf_setup
,
488 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
489 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
490 .buf_cleanup
= sh_mobile_ceu_videobuf_release
,
491 .buf_init
= sh_mobile_ceu_videobuf_init
,
492 .wait_prepare
= soc_camera_unlock
,
493 .wait_finish
= soc_camera_lock
,
494 .stop_streaming
= sh_mobile_ceu_stop_streaming
,
497 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
499 struct sh_mobile_ceu_dev
*pcdev
= data
;
500 struct vb2_buffer
*vb
;
503 spin_lock(&pcdev
->lock
);
507 /* Stale interrupt from a released buffer */
510 list_del_init(&to_ceu_vb(vb
)->queue
);
512 if (!list_empty(&pcdev
->capture
))
513 pcdev
->active
= &list_entry(pcdev
->capture
.next
,
514 struct sh_mobile_ceu_buffer
, queue
)->vb
;
516 pcdev
->active
= NULL
;
518 ret
= sh_mobile_ceu_capture(pcdev
);
519 do_gettimeofday(&vb
->v4l2_buf
.timestamp
);
521 vb
->v4l2_buf
.field
= pcdev
->field
;
522 vb
->v4l2_buf
.sequence
= pcdev
->sequence
++;
524 vb2_buffer_done(vb
, ret
< 0 ? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
527 spin_unlock(&pcdev
->lock
);
532 static struct v4l2_subdev
*find_csi2(struct sh_mobile_ceu_dev
*pcdev
)
534 struct v4l2_subdev
*sd
;
536 if (!pcdev
->csi2_pdev
)
539 v4l2_device_for_each_subdev(sd
, &pcdev
->ici
.v4l2_dev
)
540 if (&pcdev
->csi2_pdev
->dev
== v4l2_get_subdevdata(sd
))
546 /* Called with .video_lock held */
547 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
549 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
550 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
551 struct v4l2_subdev
*csi2_sd
;
557 dev_info(icd
->parent
,
558 "SuperH Mobile CEU driver attached to camera %d\n",
561 pm_runtime_get_sync(ici
->v4l2_dev
.dev
);
563 pcdev
->buf_total
= 0;
565 ret
= sh_mobile_ceu_soft_reset(pcdev
);
567 csi2_sd
= find_csi2(pcdev
);
569 csi2_sd
->grp_id
= soc_camera_grp_id(icd
);
570 v4l2_set_subdev_hostdata(csi2_sd
, icd
);
573 ret
= v4l2_subdev_call(csi2_sd
, core
, s_power
, 1);
574 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
) {
575 pm_runtime_put_sync(ici
->v4l2_dev
.dev
);
580 * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
581 * has not found this soc-camera device among its clients
583 if (ret
== -ENODEV
&& csi2_sd
)
590 /* Called with .video_lock held */
591 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
593 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
594 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
595 struct v4l2_subdev
*csi2_sd
= find_csi2(pcdev
);
597 BUG_ON(icd
!= pcdev
->icd
);
599 v4l2_subdev_call(csi2_sd
, core
, s_power
, 0);
602 /* disable capture, disable interrupts */
603 ceu_write(pcdev
, CEIER
, 0);
604 sh_mobile_ceu_soft_reset(pcdev
);
606 /* make sure active buffer is canceled */
607 spin_lock_irq(&pcdev
->lock
);
609 list_del_init(&to_ceu_vb(pcdev
->active
)->queue
);
610 vb2_buffer_done(pcdev
->active
, VB2_BUF_STATE_ERROR
);
611 pcdev
->active
= NULL
;
613 spin_unlock_irq(&pcdev
->lock
);
615 pm_runtime_put_sync(ici
->v4l2_dev
.dev
);
617 dev_info(icd
->parent
,
618 "SuperH Mobile CEU driver detached from camera %d\n",
625 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
626 * in SH7722 Hardware Manual
628 static unsigned int size_dst(unsigned int src
, unsigned int scale
)
630 unsigned int mant_pre
= scale
>> 12;
633 return ((mant_pre
+ 2 * (src
- 1)) / (2 * mant_pre
) - 1) *
634 mant_pre
* 4096 / scale
+ 1;
637 static u16
calc_scale(unsigned int src
, unsigned int *dst
)
644 scale
= (src
* 4096 / *dst
) & ~7;
646 while (scale
> 4096 && size_dst(src
, scale
) < *dst
)
649 *dst
= size_dst(src
, scale
);
654 /* rect is guaranteed to not exceed the scaled camera rectangle */
655 static void sh_mobile_ceu_set_rect(struct soc_camera_device
*icd
)
657 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
658 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
659 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
660 unsigned int height
, width
, cdwdr_width
, in_width
, in_height
;
661 unsigned int left_offset
, top_offset
;
664 dev_geo(icd
->parent
, "Crop %ux%u@%u:%u\n",
665 icd
->user_width
, icd
->user_height
, cam
->ceu_left
, cam
->ceu_top
);
667 left_offset
= cam
->ceu_left
;
668 top_offset
= cam
->ceu_top
;
670 WARN_ON(icd
->user_width
& 3 || icd
->user_height
& 3);
672 width
= icd
->user_width
;
674 if (pcdev
->image_mode
) {
675 in_width
= cam
->width
;
676 if (!pcdev
->is_16bit
) {
681 unsigned int w_factor
;
683 switch (icd
->current_fmt
->host_fmt
->packing
) {
684 case SOC_MBUS_PACKING_2X8_PADHI
:
691 in_width
= cam
->width
* w_factor
;
692 left_offset
*= w_factor
;
695 cdwdr_width
= icd
->bytesperline
;
697 height
= icd
->user_height
;
698 in_height
= cam
->height
;
699 if (V4L2_FIELD_NONE
!= pcdev
->field
) {
700 height
= (height
/ 2) & ~3;
706 /* CSI2 special configuration */
707 if (pcdev
->pdata
->csi2
) {
708 in_width
= ((in_width
- 2) * 2);
712 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
713 camor
= left_offset
| (top_offset
<< 16);
716 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor
,
717 (in_height
<< 16) | in_width
, (height
<< 16) | width
,
720 ceu_write(pcdev
, CAMOR
, camor
);
721 ceu_write(pcdev
, CAPWR
, (in_height
<< 16) | in_width
);
722 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
723 ceu_write(pcdev
, CFSZR
, (height
<< 16) | width
);
724 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
727 static u32
capture_save_reset(struct sh_mobile_ceu_dev
*pcdev
)
729 u32 capsr
= ceu_read(pcdev
, CAPSR
);
730 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset, stop capture */
734 static void capture_restore(struct sh_mobile_ceu_dev
*pcdev
, u32 capsr
)
736 unsigned long timeout
= jiffies
+ 10 * HZ
;
739 * Wait until the end of the current frame. It can take a long time,
740 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
742 while ((ceu_read(pcdev
, CSTSR
) & 1) && time_before(jiffies
, timeout
))
745 if (time_after(jiffies
, timeout
)) {
746 dev_err(pcdev
->ici
.v4l2_dev
.dev
,
747 "Timeout waiting for frame end! Interface problem?\n");
751 /* Wait until reset clears, this shall not hang... */
752 while (ceu_read(pcdev
, CAPSR
) & (1 << 16))
755 /* Anything to restore? */
756 if (capsr
& ~(1 << 16))
757 ceu_write(pcdev
, CAPSR
, capsr
);
760 /* Find the bus subdevice driver, e.g., CSI2 */
761 static struct v4l2_subdev
*find_bus_subdev(struct sh_mobile_ceu_dev
*pcdev
,
762 struct soc_camera_device
*icd
)
764 if (pcdev
->csi2_pdev
) {
765 struct v4l2_subdev
*csi2_sd
= find_csi2(pcdev
);
766 if (csi2_sd
&& csi2_sd
->grp_id
== soc_camera_grp_id(icd
))
770 return soc_camera_to_subdev(icd
);
773 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
774 V4L2_MBUS_PCLK_SAMPLE_RISING | \
775 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
776 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
777 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
778 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
779 V4L2_MBUS_DATA_ACTIVE_HIGH)
781 /* Capture is not running, no interrupts, no locking needed */
782 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
)
784 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
785 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
786 struct v4l2_subdev
*sd
= find_bus_subdev(pcdev
, icd
);
787 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
788 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
789 unsigned long value
, common_flags
= CEU_BUS_FLAGS
;
790 u32 capsr
= capture_save_reset(pcdev
);
791 unsigned int yuv_lineskip
;
795 * If the client doesn't implement g_mbus_config, we just use our
798 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
800 common_flags
= soc_mbus_config_compatible(&cfg
,
804 } else if (ret
!= -ENOIOCTLCMD
) {
808 /* Make choises, based on platform preferences */
809 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
810 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
811 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_HSYNC_LOW
)
812 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
814 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
817 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
818 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
819 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_VSYNC_LOW
)
820 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
822 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
825 cfg
.flags
= common_flags
;
826 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
827 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
830 if (icd
->current_fmt
->host_fmt
->bits_per_sample
> 8)
835 ceu_write(pcdev
, CRCNTR
, 0);
836 ceu_write(pcdev
, CRCMPR
, 0);
838 value
= 0x00000010; /* data fetch by default */
841 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
842 case V4L2_PIX_FMT_NV12
:
843 case V4L2_PIX_FMT_NV21
:
844 /* convert 4:2:2 -> 4:2:0 */
845 yuv_lineskip
= 0; /* skip for NV12/21, no skip for NV16/61 */
847 case V4L2_PIX_FMT_NV16
:
848 case V4L2_PIX_FMT_NV61
:
850 case V4L2_MBUS_FMT_UYVY8_2X8
:
851 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
853 case V4L2_MBUS_FMT_VYUY8_2X8
:
854 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
856 case V4L2_MBUS_FMT_YUYV8_2X8
:
857 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
859 case V4L2_MBUS_FMT_YVYU8_2X8
:
860 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
867 if (icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
868 icd
->current_fmt
->host_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
869 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
871 value
|= common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
872 value
|= common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
874 if (pcdev
->pdata
->csi2
) /* CSI2 mode */
876 else if (pcdev
->is_16bit
)
878 else if (pcdev
->pdata
->flags
& SH_CEU_FLAG_LOWER_8BIT
)
881 ceu_write(pcdev
, CAMCR
, value
);
883 ceu_write(pcdev
, CAPCR
, 0x00300000);
885 switch (pcdev
->field
) {
886 case V4L2_FIELD_INTERLACED_TB
:
889 case V4L2_FIELD_INTERLACED_BT
:
896 ceu_write(pcdev
, CAIFR
, value
);
898 sh_mobile_ceu_set_rect(icd
);
901 dev_geo(icd
->parent
, "CFLCR 0x%x\n", pcdev
->cflcr
);
902 ceu_write(pcdev
, CFLCR
, pcdev
->cflcr
);
905 * A few words about byte order (observed in Big Endian mode)
907 * In data fetch mode bytes are received in chunks of 8 bytes.
908 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
910 * The data is however by default written to memory in reverse order:
911 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
913 * The lowest three bits of CDOCR allows us to do swapping,
914 * using 7 we swap the data bytes to match the incoming order:
915 * D0, D1, D2, D3, D4, D5, D6, D7
917 value
= 0x00000007 | yuv_lineskip
;
919 ceu_write(pcdev
, CDOCR
, value
);
920 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
922 capture_restore(pcdev
, capsr
);
924 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
928 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
929 unsigned char buswidth
)
931 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
932 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
933 struct v4l2_subdev
*sd
= find_bus_subdev(pcdev
, icd
);
934 unsigned long common_flags
= CEU_BUS_FLAGS
;
935 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
938 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
940 common_flags
= soc_mbus_config_compatible(&cfg
,
942 else if (ret
!= -ENOIOCTLCMD
)
945 if (!common_flags
|| buswidth
> 16)
951 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats
[] = {
953 .fourcc
= V4L2_PIX_FMT_NV12
,
955 .bits_per_sample
= 8,
956 .packing
= SOC_MBUS_PACKING_1_5X8
,
957 .order
= SOC_MBUS_ORDER_LE
,
958 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
960 .fourcc
= V4L2_PIX_FMT_NV21
,
962 .bits_per_sample
= 8,
963 .packing
= SOC_MBUS_PACKING_1_5X8
,
964 .order
= SOC_MBUS_ORDER_LE
,
965 .layout
= SOC_MBUS_LAYOUT_PLANAR_2Y_C
,
967 .fourcc
= V4L2_PIX_FMT_NV16
,
969 .bits_per_sample
= 8,
970 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
971 .order
= SOC_MBUS_ORDER_LE
,
972 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
974 .fourcc
= V4L2_PIX_FMT_NV61
,
976 .bits_per_sample
= 8,
977 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
978 .order
= SOC_MBUS_ORDER_LE
,
979 .layout
= SOC_MBUS_LAYOUT_PLANAR_Y_C
,
983 /* This will be corrected as we get more formats */
984 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
986 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
987 (fmt
->bits_per_sample
== 8 &&
988 fmt
->packing
== SOC_MBUS_PACKING_1_5X8
) ||
989 (fmt
->bits_per_sample
== 8 &&
990 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
991 (fmt
->bits_per_sample
> 8 &&
992 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
995 static int client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
);
997 static struct soc_camera_device
*ctrl_to_icd(struct v4l2_ctrl
*ctrl
)
999 return container_of(ctrl
->handler
, struct soc_camera_device
,
1003 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl
*ctrl
)
1005 struct soc_camera_device
*icd
= ctrl_to_icd(ctrl
);
1006 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1007 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1010 case V4L2_CID_SHARPNESS
:
1011 switch (icd
->current_fmt
->host_fmt
->fourcc
) {
1012 case V4L2_PIX_FMT_NV12
:
1013 case V4L2_PIX_FMT_NV21
:
1014 case V4L2_PIX_FMT_NV16
:
1015 case V4L2_PIX_FMT_NV61
:
1016 ceu_write(pcdev
, CLFCR
, !ctrl
->val
);
1025 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops
= {
1026 .s_ctrl
= sh_mobile_ceu_s_ctrl
,
1029 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, unsigned int idx
,
1030 struct soc_camera_format_xlate
*xlate
)
1032 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1033 struct device
*dev
= icd
->parent
;
1034 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1035 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1038 struct sh_mobile_ceu_cam
*cam
;
1039 enum v4l2_mbus_pixelcode code
;
1040 const struct soc_mbus_pixelfmt
*fmt
;
1042 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, idx
, &code
);
1044 /* No more formats */
1047 fmt
= soc_mbus_get_fmtdesc(code
);
1049 dev_warn(dev
, "unsupported format code #%u: %d\n", idx
, code
);
1053 if (!pcdev
->pdata
->csi2
) {
1054 /* Are there any restrictions in the CSI-2 case? */
1055 ret
= sh_mobile_ceu_try_bus_param(icd
, fmt
->bits_per_sample
);
1060 if (!icd
->host_priv
) {
1061 struct v4l2_mbus_framefmt mf
;
1062 struct v4l2_rect rect
;
1065 /* Add our control */
1066 v4l2_ctrl_new_std(&icd
->ctrl_handler
, &sh_mobile_ceu_ctrl_ops
,
1067 V4L2_CID_SHARPNESS
, 0, 1, 1, 0);
1068 if (icd
->ctrl_handler
.error
)
1069 return icd
->ctrl_handler
.error
;
1071 /* FIXME: subwindow is lost between close / open */
1073 /* Cache current client geometry */
1074 ret
= client_g_rect(sd
, &rect
);
1079 ret
= v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
);
1084 * All currently existing CEU implementations support 2560x1920
1085 * or larger frames. If the sensor is proposing too big a frame,
1086 * don't bother with possibly supportred by the CEU larger
1087 * sizes, just try VGA multiples. If needed, this can be
1088 * adjusted in the future.
1090 while ((mf
.width
> pcdev
->max_width
||
1091 mf
.height
> pcdev
->max_height
) && shift
< 4) {
1092 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1093 mf
.width
= 2560 >> shift
;
1094 mf
.height
= 1920 >> shift
;
1095 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1096 soc_camera_grp_id(icd
), video
,
1104 dev_err(dev
, "Failed to configure the client below %ux%x\n",
1105 mf
.width
, mf
.height
);
1109 dev_geo(dev
, "camera fmt %ux%u\n", mf
.width
, mf
.height
);
1111 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1115 /* We are called with current camera crop, initialise subrect with it */
1117 cam
->subrect
= rect
;
1119 cam
->width
= mf
.width
;
1120 cam
->height
= mf
.height
;
1122 icd
->host_priv
= cam
;
1124 cam
= icd
->host_priv
;
1127 /* Beginning of a pass */
1129 cam
->extra_fmt
= NULL
;
1132 case V4L2_MBUS_FMT_UYVY8_2X8
:
1133 case V4L2_MBUS_FMT_VYUY8_2X8
:
1134 case V4L2_MBUS_FMT_YUYV8_2X8
:
1135 case V4L2_MBUS_FMT_YVYU8_2X8
:
1140 * Our case is simple so far: for any of the above four camera
1141 * formats we add all our four synthesized NV* formats, so,
1142 * just marking the device with a single flag suffices. If
1143 * the format generation rules are more complex, you would have
1144 * to actually hang your already added / counted formats onto
1145 * the host_priv pointer and check whether the format you're
1146 * going to add now is already there.
1148 cam
->extra_fmt
= sh_mobile_ceu_formats
;
1150 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
1152 for (k
= 0; xlate
&& k
< n
; k
++) {
1153 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
1156 dev_dbg(dev
, "Providing format %s using code %d\n",
1157 sh_mobile_ceu_formats
[k
].name
, code
);
1161 if (!sh_mobile_ceu_packing_supported(fmt
))
1165 /* Generic pass-through */
1168 xlate
->host_fmt
= fmt
;
1171 dev_dbg(dev
, "Providing format %s in pass-through mode\n",
1178 static void sh_mobile_ceu_put_formats(struct soc_camera_device
*icd
)
1180 kfree(icd
->host_priv
);
1181 icd
->host_priv
= NULL
;
1184 /* Check if any dimension of r1 is smaller than respective one of r2 */
1185 static bool is_smaller(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
1187 return r1
->width
< r2
->width
|| r1
->height
< r2
->height
;
1190 /* Check if r1 fails to cover r2 */
1191 static bool is_inside(struct v4l2_rect
*r1
, struct v4l2_rect
*r2
)
1193 return r1
->left
> r2
->left
|| r1
->top
> r2
->top
||
1194 r1
->left
+ r1
->width
< r2
->left
+ r2
->width
||
1195 r1
->top
+ r1
->height
< r2
->top
+ r2
->height
;
1198 static unsigned int scale_down(unsigned int size
, unsigned int scale
)
1200 return (size
* 4096 + scale
/ 2) / scale
;
1203 static unsigned int calc_generic_scale(unsigned int input
, unsigned int output
)
1205 return (input
* 4096 + output
/ 2) / output
;
1208 /* Get and store current client crop */
1209 static int client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
)
1211 struct v4l2_crop crop
;
1212 struct v4l2_cropcap cap
;
1215 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1217 ret
= v4l2_subdev_call(sd
, video
, g_crop
, &crop
);
1223 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1224 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1226 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1228 *rect
= cap
.defrect
;
1233 /* Client crop has changed, update our sub-rectangle to remain within the area */
1234 static void update_subrect(struct sh_mobile_ceu_cam
*cam
)
1236 struct v4l2_rect
*rect
= &cam
->rect
, *subrect
= &cam
->subrect
;
1238 if (rect
->width
< subrect
->width
)
1239 subrect
->width
= rect
->width
;
1241 if (rect
->height
< subrect
->height
)
1242 subrect
->height
= rect
->height
;
1244 if (rect
->left
> subrect
->left
)
1245 subrect
->left
= rect
->left
;
1246 else if (rect
->left
+ rect
->width
>
1247 subrect
->left
+ subrect
->width
)
1248 subrect
->left
= rect
->left
+ rect
->width
-
1251 if (rect
->top
> subrect
->top
)
1252 subrect
->top
= rect
->top
;
1253 else if (rect
->top
+ rect
->height
>
1254 subrect
->top
+ subrect
->height
)
1255 subrect
->top
= rect
->top
+ rect
->height
-
1260 * The common for both scaling and cropping iterative approach is:
1261 * 1. try if the client can produce exactly what requested by the user
1262 * 2. if (1) failed, try to double the client image until we get one big enough
1263 * 3. if (2) failed, try to request the maximum image
1265 static int client_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*crop
,
1266 struct v4l2_crop
*cam_crop
)
1268 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1269 struct v4l2_rect
*rect
= &crop
->c
, *cam_rect
= &cam_crop
->c
;
1270 struct device
*dev
= sd
->v4l2_dev
->dev
;
1271 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1272 struct v4l2_cropcap cap
;
1274 unsigned int width
, height
;
1276 v4l2_subdev_call(sd
, video
, s_crop
, crop
);
1277 ret
= client_g_rect(sd
, cam_rect
);
1282 * Now cam_crop contains the current camera input rectangle, and it must
1283 * be within camera cropcap bounds
1285 if (!memcmp(rect
, cam_rect
, sizeof(*rect
))) {
1286 /* Even if camera S_CROP failed, but camera rectangle matches */
1287 dev_dbg(dev
, "Camera S_CROP successful for %dx%d@%d:%d\n",
1288 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1289 cam
->rect
= *cam_rect
;
1293 /* Try to fix cropping, that camera hasn't managed to set */
1294 dev_geo(dev
, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
1295 cam_rect
->width
, cam_rect
->height
,
1296 cam_rect
->left
, cam_rect
->top
,
1297 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1299 /* We need sensor maximum rectangle */
1300 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1304 /* Put user requested rectangle within sensor bounds */
1305 soc_camera_limit_side(&rect
->left
, &rect
->width
, cap
.bounds
.left
, 2,
1307 soc_camera_limit_side(&rect
->top
, &rect
->height
, cap
.bounds
.top
, 4,
1311 * Popular special case - some cameras can only handle fixed sizes like
1312 * QVGA, VGA,... Take care to avoid infinite loop.
1314 width
= max(cam_rect
->width
, 2);
1315 height
= max(cam_rect
->height
, 2);
1318 * Loop as long as sensor is not covering the requested rectangle and
1319 * is still within its bounds
1321 while (!ret
&& (is_smaller(cam_rect
, rect
) ||
1322 is_inside(cam_rect
, rect
)) &&
1323 (cap
.bounds
.width
> width
|| cap
.bounds
.height
> height
)) {
1328 cam_rect
->width
= width
;
1329 cam_rect
->height
= height
;
1332 * We do not know what capabilities the camera has to set up
1333 * left and top borders. We could try to be smarter in iterating
1334 * them, e.g., if camera current left is to the right of the
1335 * target left, set it to the middle point between the current
1336 * left and minimum left. But that would add too much
1337 * complexity: we would have to iterate each border separately.
1338 * Instead we just drop to the left and top bounds.
1340 if (cam_rect
->left
> rect
->left
)
1341 cam_rect
->left
= cap
.bounds
.left
;
1343 if (cam_rect
->left
+ cam_rect
->width
< rect
->left
+ rect
->width
)
1344 cam_rect
->width
= rect
->left
+ rect
->width
-
1347 if (cam_rect
->top
> rect
->top
)
1348 cam_rect
->top
= cap
.bounds
.top
;
1350 if (cam_rect
->top
+ cam_rect
->height
< rect
->top
+ rect
->height
)
1351 cam_rect
->height
= rect
->top
+ rect
->height
-
1354 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1355 ret
= client_g_rect(sd
, cam_rect
);
1356 dev_geo(dev
, "Camera S_CROP %d for %dx%d@%d:%d\n", ret
,
1357 cam_rect
->width
, cam_rect
->height
,
1358 cam_rect
->left
, cam_rect
->top
);
1361 /* S_CROP must not modify the rectangle */
1362 if (is_smaller(cam_rect
, rect
) || is_inside(cam_rect
, rect
)) {
1364 * The camera failed to configure a suitable cropping,
1365 * we cannot use the current rectangle, set to max
1367 *cam_rect
= cap
.bounds
;
1368 v4l2_subdev_call(sd
, video
, s_crop
, cam_crop
);
1369 ret
= client_g_rect(sd
, cam_rect
);
1370 dev_geo(dev
, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret
,
1371 cam_rect
->width
, cam_rect
->height
,
1372 cam_rect
->left
, cam_rect
->top
);
1376 cam
->rect
= *cam_rect
;
1377 update_subrect(cam
);
1383 /* Iterative s_mbus_fmt, also updates cached client crop on success */
1384 static int client_s_fmt(struct soc_camera_device
*icd
,
1385 struct v4l2_mbus_framefmt
*mf
, bool ceu_can_scale
)
1387 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1388 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1389 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1390 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1391 struct device
*dev
= icd
->parent
;
1392 unsigned int width
= mf
->width
, height
= mf
->height
, tmp_w
, tmp_h
;
1393 unsigned int max_width
, max_height
;
1394 struct v4l2_cropcap cap
;
1398 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1399 soc_camera_grp_id(icd
), video
,
1404 dev_geo(dev
, "camera scaled to %ux%u\n", mf
->width
, mf
->height
);
1406 if (width
== mf
->width
&& height
== mf
->height
) {
1407 /* Perfect! The client has done it all. */
1416 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1418 ret
= v4l2_subdev_call(sd
, video
, cropcap
, &cap
);
1422 max_width
= min(cap
.bounds
.width
, pcdev
->max_width
);
1423 max_height
= min(cap
.bounds
.height
, pcdev
->max_height
);
1425 /* Camera set a format, but geometry is not precise, try to improve */
1429 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1430 while ((width
> tmp_w
|| height
> tmp_h
) &&
1431 tmp_w
< max_width
&& tmp_h
< max_height
) {
1432 tmp_w
= min(2 * tmp_w
, max_width
);
1433 tmp_h
= min(2 * tmp_h
, max_height
);
1436 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1437 soc_camera_grp_id(icd
), video
,
1439 dev_geo(dev
, "Camera scaled to %ux%u\n",
1440 mf
->width
, mf
->height
);
1442 /* This shouldn't happen */
1443 dev_err(dev
, "Client failed to set format: %d\n", ret
);
1450 ret
= client_g_rect(sd
, &cam
->rect
);
1455 cam
->subrect
= cam
->rect
;
1457 update_subrect(cam
);
1463 * @width - on output: user width, mapped back to input
1464 * @height - on output: user height, mapped back to input
1465 * @mf - in- / output camera output window
1467 static int client_scale(struct soc_camera_device
*icd
,
1468 struct v4l2_mbus_framefmt
*mf
,
1469 unsigned int *width
, unsigned int *height
,
1472 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1473 struct device
*dev
= icd
->parent
;
1474 struct v4l2_mbus_framefmt mf_tmp
= *mf
;
1475 unsigned int scale_h
, scale_v
;
1479 * 5. Apply iterative camera S_FMT for camera user window (also updates
1480 * client crop cache and the imaginary sub-rectangle).
1482 ret
= client_s_fmt(icd
, &mf_tmp
, ceu_can_scale
);
1486 dev_geo(dev
, "5: camera scaled to %ux%u\n",
1487 mf_tmp
.width
, mf_tmp
.height
);
1489 /* 6. Retrieve camera output window (g_fmt) */
1491 /* unneeded - it is already in "mf_tmp" */
1493 /* 7. Calculate new client scales. */
1494 scale_h
= calc_generic_scale(cam
->rect
.width
, mf_tmp
.width
);
1495 scale_v
= calc_generic_scale(cam
->rect
.height
, mf_tmp
.height
);
1497 mf
->width
= mf_tmp
.width
;
1498 mf
->height
= mf_tmp
.height
;
1499 mf
->colorspace
= mf_tmp
.colorspace
;
1502 * 8. Calculate new CEU crop - apply camera scales to previously
1503 * updated "effective" crop.
1505 *width
= scale_down(cam
->subrect
.width
, scale_h
);
1506 *height
= scale_down(cam
->subrect
.height
, scale_v
);
1508 dev_geo(dev
, "8: new client sub-window %ux%u\n", *width
, *height
);
1514 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1515 * framerate by always requesting the maximum image from the client. See
1516 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1517 * scaling and cropping algorithms and for the meaning of referenced here steps.
1519 static int sh_mobile_ceu_set_crop(struct soc_camera_device
*icd
,
1520 struct v4l2_crop
*a
)
1522 struct v4l2_rect
*rect
= &a
->c
;
1523 struct device
*dev
= icd
->parent
;
1524 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1525 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1526 struct v4l2_crop cam_crop
;
1527 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1528 struct v4l2_rect
*cam_rect
= &cam_crop
.c
;
1529 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1530 struct v4l2_mbus_framefmt mf
;
1531 unsigned int scale_cam_h
, scale_cam_v
, scale_ceu_h
, scale_ceu_v
,
1532 out_width
, out_height
;
1533 int interm_width
, interm_height
;
1537 dev_geo(dev
, "S_CROP(%ux%u@%u:%u)\n", rect
->width
, rect
->height
,
1538 rect
->left
, rect
->top
);
1540 /* During camera cropping its output window can change too, stop CEU */
1541 capsr
= capture_save_reset(pcdev
);
1542 dev_dbg(dev
, "CAPSR 0x%x, CFLCR 0x%x\n", capsr
, pcdev
->cflcr
);
1545 * 1. - 2. Apply iterative camera S_CROP for new input window, read back
1546 * actual camera rectangle.
1548 ret
= client_s_crop(icd
, a
, &cam_crop
);
1552 dev_geo(dev
, "1-2: camera cropped to %ux%u@%u:%u\n",
1553 cam_rect
->width
, cam_rect
->height
,
1554 cam_rect
->left
, cam_rect
->top
);
1556 /* On success cam_crop contains current camera crop */
1558 /* 3. Retrieve camera output window */
1559 ret
= v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
);
1563 if (mf
.width
> pcdev
->max_width
|| mf
.height
> pcdev
->max_height
)
1566 /* 4. Calculate camera scales */
1567 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
.width
);
1568 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
.height
);
1570 /* Calculate intermediate window */
1571 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1572 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1574 if (interm_width
< icd
->user_width
) {
1577 new_scale_h
= calc_generic_scale(rect
->width
, icd
->user_width
);
1579 mf
.width
= scale_down(cam_rect
->width
, new_scale_h
);
1582 if (interm_height
< icd
->user_height
) {
1585 new_scale_v
= calc_generic_scale(rect
->height
, icd
->user_height
);
1587 mf
.height
= scale_down(cam_rect
->height
, new_scale_v
);
1590 if (interm_width
< icd
->user_width
|| interm_height
< icd
->user_height
) {
1591 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1592 soc_camera_grp_id(icd
), video
,
1597 dev_geo(dev
, "New camera output %ux%u\n", mf
.width
, mf
.height
);
1598 scale_cam_h
= calc_generic_scale(cam_rect
->width
, mf
.width
);
1599 scale_cam_v
= calc_generic_scale(cam_rect
->height
, mf
.height
);
1600 interm_width
= scale_down(rect
->width
, scale_cam_h
);
1601 interm_height
= scale_down(rect
->height
, scale_cam_v
);
1604 /* Cache camera output window */
1605 cam
->width
= mf
.width
;
1606 cam
->height
= mf
.height
;
1608 if (pcdev
->image_mode
) {
1609 out_width
= min(interm_width
, icd
->user_width
);
1610 out_height
= min(interm_height
, icd
->user_height
);
1612 out_width
= interm_width
;
1613 out_height
= interm_height
;
1617 * 5. Calculate CEU scales from camera scales from results of (5) and
1620 scale_ceu_h
= calc_scale(interm_width
, &out_width
);
1621 scale_ceu_v
= calc_scale(interm_height
, &out_height
);
1623 dev_geo(dev
, "5: CEU scales %u:%u\n", scale_ceu_h
, scale_ceu_v
);
1625 /* Apply CEU scales. */
1626 cflcr
= scale_ceu_h
| (scale_ceu_v
<< 16);
1627 if (cflcr
!= pcdev
->cflcr
) {
1628 pcdev
->cflcr
= cflcr
;
1629 ceu_write(pcdev
, CFLCR
, cflcr
);
1632 icd
->user_width
= out_width
& ~3;
1633 icd
->user_height
= out_height
& ~3;
1634 /* Offsets are applied at the CEU scaling filter input */
1635 cam
->ceu_left
= scale_down(rect
->left
- cam_rect
->left
, scale_cam_h
) & ~1;
1636 cam
->ceu_top
= scale_down(rect
->top
- cam_rect
->top
, scale_cam_v
) & ~1;
1638 /* 6. Use CEU cropping to crop to the new window. */
1639 sh_mobile_ceu_set_rect(icd
);
1641 cam
->subrect
= *rect
;
1643 dev_geo(dev
, "6: CEU cropped to %ux%u@%u:%u\n",
1644 icd
->user_width
, icd
->user_height
,
1645 cam
->ceu_left
, cam
->ceu_top
);
1647 /* Restore capture. The CE bit can be cleared by the hardware */
1650 capture_restore(pcdev
, capsr
);
1652 /* Even if only camera cropping succeeded */
1656 static int sh_mobile_ceu_get_crop(struct soc_camera_device
*icd
,
1657 struct v4l2_crop
*a
)
1659 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1661 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1662 a
->c
= cam
->subrect
;
1668 * Calculate real client output window by applying new scales to the current
1669 * client crop. New scales are calculated from the requested output format and
1670 * CEU crop, mapped backed onto the client input (subrect).
1672 static void calculate_client_output(struct soc_camera_device
*icd
,
1673 const struct v4l2_pix_format
*pix
, struct v4l2_mbus_framefmt
*mf
)
1675 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1676 struct device
*dev
= icd
->parent
;
1677 struct v4l2_rect
*cam_subrect
= &cam
->subrect
;
1678 unsigned int scale_v
, scale_h
;
1680 if (cam_subrect
->width
== cam
->rect
.width
&&
1681 cam_subrect
->height
== cam
->rect
.height
) {
1682 /* No sub-cropping */
1683 mf
->width
= pix
->width
;
1684 mf
->height
= pix
->height
;
1688 /* 1.-2. Current camera scales and subwin - cached. */
1690 dev_geo(dev
, "2: subwin %ux%u@%u:%u\n",
1691 cam_subrect
->width
, cam_subrect
->height
,
1692 cam_subrect
->left
, cam_subrect
->top
);
1695 * 3. Calculate new combined scales from input sub-window to requested
1700 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1701 * (128x96) or larger than VGA
1703 scale_h
= calc_generic_scale(cam_subrect
->width
, pix
->width
);
1704 scale_v
= calc_generic_scale(cam_subrect
->height
, pix
->height
);
1706 dev_geo(dev
, "3: scales %u:%u\n", scale_h
, scale_v
);
1709 * 4. Calculate desired client output window by applying combined scales
1710 * to client (real) input window.
1712 mf
->width
= scale_down(cam
->rect
.width
, scale_h
);
1713 mf
->height
= scale_down(cam
->rect
.height
, scale_v
);
1716 /* Similar to set_crop multistage iterative algorithm */
1717 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
1718 struct v4l2_format
*f
)
1720 struct device
*dev
= icd
->parent
;
1721 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1722 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1723 struct sh_mobile_ceu_cam
*cam
= icd
->host_priv
;
1724 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1725 struct v4l2_mbus_framefmt mf
;
1726 __u32 pixfmt
= pix
->pixelformat
;
1727 const struct soc_camera_format_xlate
*xlate
;
1728 /* Keep Compiler Happy */
1729 unsigned int ceu_sub_width
= 0, ceu_sub_height
= 0;
1730 u16 scale_v
, scale_h
;
1733 enum v4l2_field field
;
1735 switch (pix
->field
) {
1737 pix
->field
= V4L2_FIELD_NONE
;
1739 case V4L2_FIELD_INTERLACED_TB
:
1740 case V4L2_FIELD_INTERLACED_BT
:
1741 case V4L2_FIELD_NONE
:
1744 case V4L2_FIELD_INTERLACED
:
1745 field
= V4L2_FIELD_INTERLACED_TB
;
1749 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1751 dev_warn(dev
, "Format %x not found\n", pixfmt
);
1755 /* 1.-4. Calculate desired client output geometry */
1756 calculate_client_output(icd
, pix
, &mf
);
1757 mf
.field
= pix
->field
;
1758 mf
.colorspace
= pix
->colorspace
;
1759 mf
.code
= xlate
->code
;
1762 case V4L2_PIX_FMT_NV12
:
1763 case V4L2_PIX_FMT_NV21
:
1764 case V4L2_PIX_FMT_NV16
:
1765 case V4L2_PIX_FMT_NV61
:
1772 dev_geo(dev
, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt
, mf
.field
, mf
.code
,
1773 pix
->width
, pix
->height
);
1775 dev_geo(dev
, "4: request camera output %ux%u\n", mf
.width
, mf
.height
);
1778 ret
= client_scale(icd
, &mf
, &ceu_sub_width
, &ceu_sub_height
,
1779 image_mode
&& V4L2_FIELD_NONE
== field
);
1781 dev_geo(dev
, "5-9: client scale return %d\n", ret
);
1783 /* Done with the camera. Now see if we can improve the result */
1785 dev_geo(dev
, "fmt %ux%u, requested %ux%u\n",
1786 mf
.width
, mf
.height
, pix
->width
, pix
->height
);
1790 if (mf
.code
!= xlate
->code
)
1793 /* 9. Prepare CEU crop */
1794 cam
->width
= mf
.width
;
1795 cam
->height
= mf
.height
;
1797 /* 10. Use CEU scaling to scale to the requested user window. */
1799 /* We cannot scale up */
1800 if (pix
->width
> ceu_sub_width
)
1801 ceu_sub_width
= pix
->width
;
1803 if (pix
->height
> ceu_sub_height
)
1804 ceu_sub_height
= pix
->height
;
1806 pix
->colorspace
= mf
.colorspace
;
1809 /* Scale pix->{width x height} down to width x height */
1810 scale_h
= calc_scale(ceu_sub_width
, &pix
->width
);
1811 scale_v
= calc_scale(ceu_sub_height
, &pix
->height
);
1813 pix
->width
= ceu_sub_width
;
1814 pix
->height
= ceu_sub_height
;
1819 pcdev
->cflcr
= scale_h
| (scale_v
<< 16);
1822 * We have calculated CFLCR, the actual configuration will be performed
1823 * in sh_mobile_ceu_set_bus_param()
1826 dev_geo(dev
, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1827 ceu_sub_width
, scale_h
, pix
->width
,
1828 ceu_sub_height
, scale_v
, pix
->height
);
1830 cam
->code
= xlate
->code
;
1831 icd
->current_fmt
= xlate
;
1833 pcdev
->field
= field
;
1834 pcdev
->image_mode
= image_mode
;
1836 /* CFSZR requirement */
1843 #define CEU_CHDW_MAX 8188U /* Maximum line stride */
1845 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
1846 struct v4l2_format
*f
)
1848 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1849 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1850 const struct soc_camera_format_xlate
*xlate
;
1851 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1852 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1853 struct v4l2_mbus_framefmt mf
;
1854 __u32 pixfmt
= pix
->pixelformat
;
1858 dev_geo(icd
->parent
, "TRY_FMT(pix=0x%x, %ux%u)\n",
1859 pixfmt
, pix
->width
, pix
->height
);
1861 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1863 xlate
= icd
->current_fmt
;
1864 dev_dbg(icd
->parent
, "Format %x not found, keeping %x\n",
1865 pixfmt
, xlate
->host_fmt
->fourcc
);
1866 pixfmt
= xlate
->host_fmt
->fourcc
;
1867 pix
->pixelformat
= pixfmt
;
1868 pix
->colorspace
= icd
->colorspace
;
1871 /* FIXME: calculate using depth and bus width */
1873 /* CFSZR requires height and width to be 4-pixel aligned */
1874 v4l_bound_align_image(&pix
->width
, 2, pcdev
->max_width
, 2,
1875 &pix
->height
, 4, pcdev
->max_height
, 2, 0);
1878 height
= pix
->height
;
1880 /* limit to sensor capabilities */
1881 mf
.width
= pix
->width
;
1882 mf
.height
= pix
->height
;
1883 mf
.field
= pix
->field
;
1884 mf
.code
= xlate
->code
;
1885 mf
.colorspace
= pix
->colorspace
;
1887 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
, soc_camera_grp_id(icd
),
1888 video
, try_mbus_fmt
, &mf
);
1892 pix
->width
= mf
.width
;
1893 pix
->height
= mf
.height
;
1894 pix
->field
= mf
.field
;
1895 pix
->colorspace
= mf
.colorspace
;
1898 case V4L2_PIX_FMT_NV12
:
1899 case V4L2_PIX_FMT_NV21
:
1900 case V4L2_PIX_FMT_NV16
:
1901 case V4L2_PIX_FMT_NV61
:
1902 /* FIXME: check against rect_max after converting soc-camera */
1903 /* We can scale precisely, need a bigger image from camera */
1904 if (pix
->width
< width
|| pix
->height
< height
) {
1906 * We presume, the sensor behaves sanely, i.e., if
1907 * requested a bigger rectangle, it will not return a
1910 mf
.width
= pcdev
->max_width
;
1911 mf
.height
= pcdev
->max_height
;
1912 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
1913 soc_camera_grp_id(icd
), video
,
1916 /* Shouldn't actually happen... */
1917 dev_err(icd
->parent
,
1918 "FIXME: client try_fmt() = %d\n", ret
);
1922 /* We will scale exactly */
1923 if (mf
.width
> width
)
1925 if (mf
.height
> height
)
1926 pix
->height
= height
;
1928 pix
->bytesperline
= max(pix
->bytesperline
, pix
->width
);
1929 pix
->bytesperline
= min(pix
->bytesperline
, CEU_CHDW_MAX
);
1930 pix
->bytesperline
&= ~3;
1934 /* Configurable stride isn't supported in pass-through mode. */
1935 pix
->bytesperline
= 0;
1942 dev_geo(icd
->parent
, "%s(): return %d, fmt 0x%x, %ux%u\n",
1943 __func__
, ret
, pix
->pixelformat
, pix
->width
, pix
->height
);
1948 static int sh_mobile_ceu_set_livecrop(struct soc_camera_device
*icd
,
1949 struct v4l2_crop
*a
)
1951 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1952 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1953 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
1954 u32 out_width
= icd
->user_width
, out_height
= icd
->user_height
;
1959 /* Wait for frame */
1960 ret
= wait_for_completion_interruptible(&pcdev
->complete
);
1961 /* Stop the client */
1962 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 0);
1964 dev_warn(icd
->parent
,
1965 "Client failed to stop the stream: %d\n", ret
);
1967 /* Do the crop, if it fails, there's nothing more we can do */
1968 sh_mobile_ceu_set_crop(icd
, a
);
1970 dev_geo(icd
->parent
, "Output after crop: %ux%u\n", icd
->user_width
, icd
->user_height
);
1972 if (icd
->user_width
!= out_width
|| icd
->user_height
!= out_height
) {
1973 struct v4l2_format f
= {
1974 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1977 .height
= out_height
,
1978 .pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
,
1979 .field
= pcdev
->field
,
1980 .colorspace
= icd
->colorspace
,
1983 ret
= sh_mobile_ceu_set_fmt(icd
, &f
);
1984 if (!ret
&& (out_width
!= f
.fmt
.pix
.width
||
1985 out_height
!= f
.fmt
.pix
.height
))
1988 icd
->user_width
= out_width
& ~3;
1989 icd
->user_height
= out_height
& ~3;
1990 ret
= sh_mobile_ceu_set_bus_param(icd
);
1994 /* Thaw the queue */
1996 spin_lock_irq(&pcdev
->lock
);
1997 sh_mobile_ceu_capture(pcdev
);
1998 spin_unlock_irq(&pcdev
->lock
);
1999 /* Start the client */
2000 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 1);
2004 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
2006 struct soc_camera_device
*icd
= file
->private_data
;
2008 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
2011 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
2012 struct v4l2_capability
*cap
)
2014 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
2015 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
2019 static int sh_mobile_ceu_init_videobuf(struct vb2_queue
*q
,
2020 struct soc_camera_device
*icd
)
2022 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2023 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
2025 q
->ops
= &sh_mobile_ceu_videobuf_ops
;
2026 q
->mem_ops
= &vb2_dma_contig_memops
;
2027 q
->buf_struct_size
= sizeof(struct sh_mobile_ceu_buffer
);
2029 return vb2_queue_init(q
);
2032 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
2033 .owner
= THIS_MODULE
,
2034 .add
= sh_mobile_ceu_add_device
,
2035 .remove
= sh_mobile_ceu_remove_device
,
2036 .get_formats
= sh_mobile_ceu_get_formats
,
2037 .put_formats
= sh_mobile_ceu_put_formats
,
2038 .get_crop
= sh_mobile_ceu_get_crop
,
2039 .set_crop
= sh_mobile_ceu_set_crop
,
2040 .set_livecrop
= sh_mobile_ceu_set_livecrop
,
2041 .set_fmt
= sh_mobile_ceu_set_fmt
,
2042 .try_fmt
= sh_mobile_ceu_try_fmt
,
2043 .poll
= sh_mobile_ceu_poll
,
2044 .querycap
= sh_mobile_ceu_querycap
,
2045 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
2046 .init_videobuf2
= sh_mobile_ceu_init_videobuf
,
2050 struct notifier_block notifier
;
2051 struct completion completion
;
2055 static int bus_notify(struct notifier_block
*nb
,
2056 unsigned long action
, void *data
)
2058 struct device
*dev
= data
;
2059 struct bus_wait
*wait
= container_of(nb
, struct bus_wait
, notifier
);
2061 if (wait
->dev
!= dev
)
2065 case BUS_NOTIFY_UNBOUND_DRIVER
:
2066 /* Protect from module unloading */
2067 wait_for_completion(&wait
->completion
);
2073 static int __devinit
sh_mobile_ceu_probe(struct platform_device
*pdev
)
2075 struct sh_mobile_ceu_dev
*pcdev
;
2076 struct resource
*res
;
2080 struct bus_wait wait
= {
2081 .completion
= COMPLETION_INITIALIZER_ONSTACK(wait
.completion
),
2082 .notifier
.notifier_call
= bus_notify
,
2084 struct sh_mobile_ceu_companion
*csi2
;
2086 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2087 irq
= platform_get_irq(pdev
, 0);
2088 if (!res
|| (int)irq
<= 0) {
2089 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
2094 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
2096 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
2101 INIT_LIST_HEAD(&pcdev
->capture
);
2102 spin_lock_init(&pcdev
->lock
);
2103 init_completion(&pcdev
->complete
);
2105 pcdev
->pdata
= pdev
->dev
.platform_data
;
2106 if (!pcdev
->pdata
) {
2108 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
2112 pcdev
->max_width
= pcdev
->pdata
->max_width
? : 2560;
2113 pcdev
->max_height
= pcdev
->pdata
->max_height
? : 1920;
2115 base
= ioremap_nocache(res
->start
, resource_size(res
));
2118 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
2124 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
2126 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2128 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
2132 DMA_MEMORY_EXCLUSIVE
);
2134 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
2139 pcdev
->video_limit
= resource_size(res
);
2143 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
2144 dev_name(&pdev
->dev
), pcdev
);
2146 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
2147 goto exit_release_mem
;
2150 pm_suspend_ignore_children(&pdev
->dev
, true);
2151 pm_runtime_enable(&pdev
->dev
);
2152 pm_runtime_resume(&pdev
->dev
);
2154 pcdev
->ici
.priv
= pcdev
;
2155 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
2156 pcdev
->ici
.nr
= pdev
->id
;
2157 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
2158 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
2159 pcdev
->ici
.capabilities
= SOCAM_HOST_CAP_STRIDE
;
2161 pcdev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
2162 if (IS_ERR(pcdev
->alloc_ctx
)) {
2163 err
= PTR_ERR(pcdev
->alloc_ctx
);
2167 err
= soc_camera_host_register(&pcdev
->ici
);
2171 /* CSI2 interfacing */
2172 csi2
= pcdev
->pdata
->csi2
;
2174 struct platform_device
*csi2_pdev
=
2175 platform_device_alloc("sh-mobile-csi2", csi2
->id
);
2176 struct sh_csi2_pdata
*csi2_pdata
= csi2
->platform_data
;
2180 goto exit_host_unregister
;
2183 pcdev
->csi2_pdev
= csi2_pdev
;
2185 err
= platform_device_add_data(csi2_pdev
, csi2_pdata
, sizeof(*csi2_pdata
));
2189 csi2_pdata
= csi2_pdev
->dev
.platform_data
;
2190 csi2_pdata
->v4l2_dev
= &pcdev
->ici
.v4l2_dev
;
2192 csi2_pdev
->resource
= csi2
->resource
;
2193 csi2_pdev
->num_resources
= csi2
->num_resources
;
2195 err
= platform_device_add(csi2_pdev
);
2199 wait
.dev
= &csi2_pdev
->dev
;
2201 err
= bus_register_notifier(&platform_bus_type
, &wait
.notifier
);
2203 goto exit_pdev_unregister
;
2206 * From this point the driver module will not unload, until
2207 * we complete the completion.
2210 if (!csi2_pdev
->dev
.driver
) {
2211 complete(&wait
.completion
);
2212 /* Either too late, or probing failed */
2213 bus_unregister_notifier(&platform_bus_type
, &wait
.notifier
);
2215 goto exit_pdev_unregister
;
2219 * The module is still loaded, in the worst case it is hanging
2220 * in device release on our completion. So, _now_ dereferencing
2221 * the "owner" is safe!
2224 err
= try_module_get(csi2_pdev
->dev
.driver
->owner
);
2226 /* Let notifier complete, if it has been locked */
2227 complete(&wait
.completion
);
2228 bus_unregister_notifier(&platform_bus_type
, &wait
.notifier
);
2231 goto exit_pdev_unregister
;
2237 exit_pdev_unregister
:
2238 platform_device_del(pcdev
->csi2_pdev
);
2240 pcdev
->csi2_pdev
->resource
= NULL
;
2241 platform_device_put(pcdev
->csi2_pdev
);
2242 exit_host_unregister
:
2243 soc_camera_host_unregister(&pcdev
->ici
);
2245 vb2_dma_contig_cleanup_ctx(pcdev
->alloc_ctx
);
2247 pm_runtime_disable(&pdev
->dev
);
2248 free_irq(pcdev
->irq
, pcdev
);
2250 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
2251 dma_release_declared_memory(&pdev
->dev
);
2260 static int __devexit
sh_mobile_ceu_remove(struct platform_device
*pdev
)
2262 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
2263 struct sh_mobile_ceu_dev
*pcdev
= container_of(soc_host
,
2264 struct sh_mobile_ceu_dev
, ici
);
2265 struct platform_device
*csi2_pdev
= pcdev
->csi2_pdev
;
2267 soc_camera_host_unregister(soc_host
);
2268 pm_runtime_disable(&pdev
->dev
);
2269 free_irq(pcdev
->irq
, pcdev
);
2270 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
2271 dma_release_declared_memory(&pdev
->dev
);
2272 iounmap(pcdev
->base
);
2273 vb2_dma_contig_cleanup_ctx(pcdev
->alloc_ctx
);
2274 if (csi2_pdev
&& csi2_pdev
->dev
.driver
) {
2275 struct module
*csi2_drv
= csi2_pdev
->dev
.driver
->owner
;
2276 platform_device_del(csi2_pdev
);
2277 csi2_pdev
->resource
= NULL
;
2278 platform_device_put(csi2_pdev
);
2279 module_put(csi2_drv
);
2286 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
2288 /* Runtime PM callback shared between ->runtime_suspend()
2289 * and ->runtime_resume(). Simply returns success.
2291 * This driver re-initializes all registers after
2292 * pm_runtime_get_sync() anyway so there is no need
2293 * to save and restore registers here.
2298 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
2299 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
2300 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
2303 static struct platform_driver sh_mobile_ceu_driver
= {
2305 .name
= "sh_mobile_ceu",
2306 .pm
= &sh_mobile_ceu_dev_pm_ops
,
2308 .probe
= sh_mobile_ceu_probe
,
2309 .remove
= __devexit_p(sh_mobile_ceu_remove
),
2312 static int __init
sh_mobile_ceu_init(void)
2314 /* Whatever return code */
2315 request_module("sh_mobile_csi2");
2316 return platform_driver_register(&sh_mobile_ceu_driver
);
2319 static void __exit
sh_mobile_ceu_exit(void)
2321 platform_driver_unregister(&sh_mobile_ceu_driver
);
2324 module_init(sh_mobile_ceu_init
);
2325 module_exit(sh_mobile_ceu_exit
);
2327 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2328 MODULE_AUTHOR("Magnus Damm");
2329 MODULE_LICENSE("GPL");
2330 MODULE_VERSION("0.0.6");
2331 MODULE_ALIAS("platform:sh_mobile_ceu");