2 * V4L2 Driver for SuperH Mobile CEU interface
4 * Copyright (C) 2008 Magnus Damm
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
27 #include <linux/moduleparam.h>
28 #include <linux/time.h>
29 #include <linux/version.h>
30 #include <linux/device.h>
31 #include <linux/platform_device.h>
32 #include <linux/videodev2.h>
33 #include <linux/pm_runtime.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-dev.h>
37 #include <media/soc_camera.h>
38 #include <media/sh_mobile_ceu.h>
39 #include <media/videobuf-dma-contig.h>
41 /* register offsets for sh7722 / sh7723 */
43 #define CAPSR 0x00 /* Capture start register */
44 #define CAPCR 0x04 /* Capture control register */
45 #define CAMCR 0x08 /* Capture interface control register */
46 #define CMCYR 0x0c /* Capture interface cycle register */
47 #define CAMOR 0x10 /* Capture interface offset register */
48 #define CAPWR 0x14 /* Capture interface width register */
49 #define CAIFR 0x18 /* Capture interface input format register */
50 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
51 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
52 #define CRCNTR 0x28 /* CEU register control register */
53 #define CRCMPR 0x2c /* CEU register forcible control register */
54 #define CFLCR 0x30 /* Capture filter control register */
55 #define CFSZR 0x34 /* Capture filter size clip register */
56 #define CDWDR 0x38 /* Capture destination width register */
57 #define CDAYR 0x3c /* Capture data address Y register */
58 #define CDACR 0x40 /* Capture data address C register */
59 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
60 #define CDBCR 0x48 /* Capture data bottom-field address C register */
61 #define CBDSR 0x4c /* Capture bundle destination size register */
62 #define CFWCR 0x5c /* Firewall operation control register */
63 #define CLFCR 0x60 /* Capture low-pass filter control register */
64 #define CDOCR 0x64 /* Capture data output control register */
65 #define CDDCR 0x68 /* Capture data complexity level register */
66 #define CDDAR 0x6c /* Capture data complexity level address register */
67 #define CEIER 0x70 /* Capture event interrupt enable register */
68 #define CETCR 0x74 /* Capture event flag clear register */
69 #define CSTSR 0x7c /* Capture status register */
70 #define CSRTR 0x80 /* Capture software reset register */
71 #define CDSSR 0x84 /* Capture data size register */
72 #define CDAYR2 0x90 /* Capture data address Y register 2 */
73 #define CDACR2 0x94 /* Capture data address C register 2 */
74 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
75 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
77 /* per video frame buffer */
78 struct sh_mobile_ceu_buffer
{
79 struct videobuf_buffer vb
; /* v4l buffer must be first */
80 const struct soc_camera_data_format
*fmt
;
83 struct sh_mobile_ceu_dev
{
84 struct soc_camera_host ici
;
85 struct soc_camera_device
*icd
;
89 unsigned long video_limit
;
91 /* lock used to protect videobuf */
93 struct list_head capture
;
94 struct videobuf_buffer
*active
;
97 struct sh_mobile_ceu_info
*pdata
;
99 const struct soc_camera_data_format
*camera_fmt
;
102 static unsigned long make_bus_param(struct sh_mobile_ceu_dev
*pcdev
)
106 flags
= SOCAM_MASTER
|
107 SOCAM_PCLK_SAMPLE_RISING
|
108 SOCAM_HSYNC_ACTIVE_HIGH
|
109 SOCAM_HSYNC_ACTIVE_LOW
|
110 SOCAM_VSYNC_ACTIVE_HIGH
|
111 SOCAM_VSYNC_ACTIVE_LOW
|
112 SOCAM_DATA_ACTIVE_HIGH
;
114 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_8BIT_BUS
)
115 flags
|= SOCAM_DATAWIDTH_8
;
117 if (pcdev
->pdata
->flags
& SH_CEU_FLAG_USE_16BIT_BUS
)
118 flags
|= SOCAM_DATAWIDTH_16
;
120 if (flags
& SOCAM_DATAWIDTH_MASK
)
126 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
127 unsigned long reg_offs
, u32 data
)
129 iowrite32(data
, priv
->base
+ reg_offs
);
132 static u32
ceu_read(struct sh_mobile_ceu_dev
*priv
, unsigned long reg_offs
)
134 return ioread32(priv
->base
+ reg_offs
);
138 * Videobuf operations
140 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue
*vq
,
144 struct soc_camera_device
*icd
= vq
->priv_data
;
145 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
146 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
147 int bytes_per_pixel
= (icd
->current_fmt
->depth
+ 7) >> 3;
149 *size
= PAGE_ALIGN(icd
->rect_current
.width
* icd
->rect_current
.height
*
155 if (pcdev
->video_limit
) {
156 while (*size
* *count
> pcdev
->video_limit
)
160 dev_dbg(&icd
->dev
, "count=%d, size=%d\n", *count
, *size
);
165 static void free_buffer(struct videobuf_queue
*vq
,
166 struct sh_mobile_ceu_buffer
*buf
)
168 struct soc_camera_device
*icd
= vq
->priv_data
;
170 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
171 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
176 videobuf_waiton(&buf
->vb
, 0, 0);
177 videobuf_dma_contig_free(vq
, &buf
->vb
);
178 dev_dbg(&icd
->dev
, "%s freed\n", __func__
);
179 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
182 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
183 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
184 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
185 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
188 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
190 struct soc_camera_device
*icd
= pcdev
->icd
;
191 dma_addr_t phys_addr_top
, phys_addr_bottom
;
193 /* The hardware is _very_ picky about this sequence. Especially
194 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
195 * several not-so-well documented interrupt sources in CETCR.
197 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~CEU_CEIER_CPEIE
);
198 ceu_write(pcdev
, CETCR
, ~ceu_read(pcdev
, CETCR
) & CEU_CETCR_MAGIC
);
199 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | CEU_CEIER_CPEIE
);
200 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~CEU_CAPCR_CTNCP
);
201 ceu_write(pcdev
, CETCR
, CEU_CETCR_MAGIC
^ CEU_CETCR_IGRW
);
206 phys_addr_top
= videobuf_to_dma_contig(pcdev
->active
);
207 ceu_write(pcdev
, CDAYR
, phys_addr_top
);
208 if (pcdev
->is_interlaced
) {
209 phys_addr_bottom
= phys_addr_top
+ icd
->rect_current
.width
;
210 ceu_write(pcdev
, CDBYR
, phys_addr_bottom
);
213 switch (icd
->current_fmt
->fourcc
) {
214 case V4L2_PIX_FMT_NV12
:
215 case V4L2_PIX_FMT_NV21
:
216 case V4L2_PIX_FMT_NV16
:
217 case V4L2_PIX_FMT_NV61
:
218 phys_addr_top
+= icd
->rect_current
.width
*
219 icd
->rect_current
.height
;
220 ceu_write(pcdev
, CDACR
, phys_addr_top
);
221 if (pcdev
->is_interlaced
) {
222 phys_addr_bottom
= phys_addr_top
+
223 icd
->rect_current
.width
;
224 ceu_write(pcdev
, CDBCR
, phys_addr_bottom
);
228 pcdev
->active
->state
= VIDEOBUF_ACTIVE
;
229 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
232 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue
*vq
,
233 struct videobuf_buffer
*vb
,
234 enum v4l2_field field
)
236 struct soc_camera_device
*icd
= vq
->priv_data
;
237 struct sh_mobile_ceu_buffer
*buf
;
240 buf
= container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
242 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
243 vb
, vb
->baddr
, vb
->bsize
);
245 /* Added list head initialization on alloc */
246 WARN_ON(!list_empty(&vb
->queue
));
249 /* This can be useful if you want to see if we actually fill
250 * the buffer with something */
251 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
254 BUG_ON(NULL
== icd
->current_fmt
);
256 if (buf
->fmt
!= icd
->current_fmt
||
257 vb
->width
!= icd
->rect_current
.width
||
258 vb
->height
!= icd
->rect_current
.height
||
259 vb
->field
!= field
) {
260 buf
->fmt
= icd
->current_fmt
;
261 vb
->width
= icd
->rect_current
.width
;
262 vb
->height
= icd
->rect_current
.height
;
264 vb
->state
= VIDEOBUF_NEEDS_INIT
;
267 vb
->size
= vb
->width
* vb
->height
* ((buf
->fmt
->depth
+ 7) >> 3);
268 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
273 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
274 ret
= videobuf_iolock(vq
, vb
, NULL
);
277 vb
->state
= VIDEOBUF_PREPARED
;
282 free_buffer(vq
, buf
);
287 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
288 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue
*vq
,
289 struct videobuf_buffer
*vb
)
291 struct soc_camera_device
*icd
= vq
->priv_data
;
292 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
293 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
295 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
296 vb
, vb
->baddr
, vb
->bsize
);
298 vb
->state
= VIDEOBUF_QUEUED
;
299 list_add_tail(&vb
->queue
, &pcdev
->capture
);
301 if (!pcdev
->active
) {
303 sh_mobile_ceu_capture(pcdev
);
307 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue
*vq
,
308 struct videobuf_buffer
*vb
)
310 free_buffer(vq
, container_of(vb
, struct sh_mobile_ceu_buffer
, vb
));
313 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops
= {
314 .buf_setup
= sh_mobile_ceu_videobuf_setup
,
315 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
316 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
317 .buf_release
= sh_mobile_ceu_videobuf_release
,
320 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
322 struct sh_mobile_ceu_dev
*pcdev
= data
;
323 struct videobuf_buffer
*vb
;
326 spin_lock_irqsave(&pcdev
->lock
, flags
);
329 list_del_init(&vb
->queue
);
331 if (!list_empty(&pcdev
->capture
))
332 pcdev
->active
= list_entry(pcdev
->capture
.next
,
333 struct videobuf_buffer
, queue
);
335 pcdev
->active
= NULL
;
337 sh_mobile_ceu_capture(pcdev
);
339 vb
->state
= VIDEOBUF_DONE
;
340 do_gettimeofday(&vb
->ts
);
343 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
348 /* Called with .video_lock held */
349 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
351 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
352 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
358 "SuperH Mobile CEU driver attached to camera %d\n",
361 clk_enable(pcdev
->clk
);
363 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
364 while (ceu_read(pcdev
, CSTSR
) & 1)
372 /* Called with .video_lock held */
373 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
375 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
376 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
379 BUG_ON(icd
!= pcdev
->icd
);
381 /* disable capture, disable interrupts */
382 ceu_write(pcdev
, CEIER
, 0);
383 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
385 /* make sure active buffer is canceled */
386 spin_lock_irqsave(&pcdev
->lock
, flags
);
388 list_del(&pcdev
->active
->queue
);
389 pcdev
->active
->state
= VIDEOBUF_ERROR
;
390 wake_up_all(&pcdev
->active
->done
);
391 pcdev
->active
= NULL
;
393 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
395 clk_disable(pcdev
->clk
);
398 "SuperH Mobile CEU driver detached from camera %d\n",
404 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
,
407 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
408 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
409 int ret
, buswidth
, width
, height
, cfszr_width
, cdwdr_width
;
410 unsigned long camera_flags
, common_flags
, value
;
411 int yuv_mode
, yuv_lineskip
;
413 camera_flags
= icd
->ops
->query_bus_param(icd
);
414 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
415 make_bus_param(pcdev
));
419 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
423 switch (common_flags
& SOCAM_DATAWIDTH_MASK
) {
424 case SOCAM_DATAWIDTH_8
:
427 case SOCAM_DATAWIDTH_16
:
434 ceu_write(pcdev
, CRCNTR
, 0);
435 ceu_write(pcdev
, CRCMPR
, 0);
437 value
= 0x00000010; /* data fetch by default */
438 yuv_mode
= yuv_lineskip
= 0;
440 switch (icd
->current_fmt
->fourcc
) {
441 case V4L2_PIX_FMT_NV12
:
442 case V4L2_PIX_FMT_NV21
:
443 yuv_lineskip
= 1; /* skip for NV12/21, no skip for NV16/61 */
445 case V4L2_PIX_FMT_NV16
:
446 case V4L2_PIX_FMT_NV61
:
448 switch (pcdev
->camera_fmt
->fourcc
) {
449 case V4L2_PIX_FMT_UYVY
:
450 value
= 0x00000000; /* Cb0, Y0, Cr0, Y1 */
452 case V4L2_PIX_FMT_VYUY
:
453 value
= 0x00000100; /* Cr0, Y0, Cb0, Y1 */
455 case V4L2_PIX_FMT_YUYV
:
456 value
= 0x00000200; /* Y0, Cb0, Y1, Cr0 */
458 case V4L2_PIX_FMT_YVYU
:
459 value
= 0x00000300; /* Y0, Cr0, Y1, Cb0 */
466 if (icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV21
||
467 icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_NV61
)
468 value
^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
470 value
|= common_flags
& SOCAM_VSYNC_ACTIVE_LOW
? 1 << 1 : 0;
471 value
|= common_flags
& SOCAM_HSYNC_ACTIVE_LOW
? 1 << 0 : 0;
472 value
|= buswidth
== 16 ? 1 << 12 : 0;
473 ceu_write(pcdev
, CAMCR
, value
);
475 ceu_write(pcdev
, CAPCR
, 0x00300000);
476 ceu_write(pcdev
, CAIFR
, pcdev
->is_interlaced
? 0x101 : 0);
481 width
= icd
->rect_current
.width
* 2;
482 width
= buswidth
== 16 ? width
/ 2 : width
;
483 cfszr_width
= cdwdr_width
= icd
->rect_current
.width
;
485 width
= icd
->rect_current
.width
*
486 ((icd
->current_fmt
->depth
+ 7) >> 3);
487 width
= buswidth
== 16 ? width
/ 2 : width
;
488 cfszr_width
= buswidth
== 8 ? width
/ 2 : width
;
489 cdwdr_width
= buswidth
== 16 ? width
* 2 : width
;
492 height
= icd
->rect_current
.height
;
493 if (pcdev
->is_interlaced
) {
498 ceu_write(pcdev
, CAMOR
, 0);
499 ceu_write(pcdev
, CAPWR
, (height
<< 16) | width
);
500 ceu_write(pcdev
, CFLCR
, 0); /* no scaling */
501 ceu_write(pcdev
, CFSZR
, (height
<< 16) | cfszr_width
);
503 /* A few words about byte order (observed in Big Endian mode)
505 * In data fetch mode bytes are received in chunks of 8 bytes.
506 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
508 * The data is however by default written to memory in reverse order:
509 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
511 * The lowest three bits of CDOCR allows us to do swapping,
512 * using 7 we swap the data bytes to match the incoming order:
513 * D0, D1, D2, D3, D4, D5, D6, D7
517 value
&= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
519 ceu_write(pcdev
, CDOCR
, value
);
521 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
522 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
524 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
528 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
)
530 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
531 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
532 unsigned long camera_flags
, common_flags
;
534 camera_flags
= icd
->ops
->query_bus_param(icd
);
535 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
536 make_bus_param(pcdev
));
543 static const struct soc_camera_data_format sh_mobile_ceu_formats
[] = {
547 .fourcc
= V4L2_PIX_FMT_NV12
,
548 .colorspace
= V4L2_COLORSPACE_JPEG
,
553 .fourcc
= V4L2_PIX_FMT_NV21
,
554 .colorspace
= V4L2_COLORSPACE_JPEG
,
559 .fourcc
= V4L2_PIX_FMT_NV16
,
560 .colorspace
= V4L2_COLORSPACE_JPEG
,
565 .fourcc
= V4L2_PIX_FMT_NV61
,
566 .colorspace
= V4L2_COLORSPACE_JPEG
,
570 static int sh_mobile_ceu_get_formats(struct soc_camera_device
*icd
, int idx
,
571 struct soc_camera_format_xlate
*xlate
)
573 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
577 ret
= sh_mobile_ceu_try_bus_param(icd
);
581 /* Beginning of a pass */
583 icd
->host_priv
= NULL
;
585 switch (icd
->formats
[idx
].fourcc
) {
586 case V4L2_PIX_FMT_UYVY
:
587 case V4L2_PIX_FMT_VYUY
:
588 case V4L2_PIX_FMT_YUYV
:
589 case V4L2_PIX_FMT_YVYU
:
591 goto add_single_format
;
594 * Our case is simple so far: for any of the above four camera
595 * formats we add all our four synthesized NV* formats, so,
596 * just marking the device with a single flag suffices. If
597 * the format generation rules are more complex, you would have
598 * to actually hang your already added / counted formats onto
599 * the host_priv pointer and check whether the format you're
600 * going to add now is already there.
602 icd
->host_priv
= (void *)sh_mobile_ceu_formats
;
604 n
= ARRAY_SIZE(sh_mobile_ceu_formats
);
606 for (k
= 0; xlate
&& k
< n
; k
++) {
607 xlate
->host_fmt
= &sh_mobile_ceu_formats
[k
];
608 xlate
->cam_fmt
= icd
->formats
+ idx
;
609 xlate
->buswidth
= icd
->formats
[idx
].depth
;
611 dev_dbg(ici
->v4l2_dev
.dev
, "Providing format %s using %s\n",
612 sh_mobile_ceu_formats
[k
].name
,
613 icd
->formats
[idx
].name
);
617 /* Generic pass-through */
620 xlate
->host_fmt
= icd
->formats
+ idx
;
621 xlate
->cam_fmt
= icd
->formats
+ idx
;
622 xlate
->buswidth
= icd
->formats
[idx
].depth
;
624 dev_dbg(ici
->v4l2_dev
.dev
,
625 "Providing format %s in pass-through mode\n",
626 icd
->formats
[idx
].name
);
633 static int sh_mobile_ceu_set_crop(struct soc_camera_device
*icd
,
634 struct v4l2_rect
*rect
)
636 return icd
->ops
->set_crop(icd
, rect
);
639 static int sh_mobile_ceu_set_fmt(struct soc_camera_device
*icd
,
640 struct v4l2_format
*f
)
642 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
643 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
644 __u32 pixfmt
= f
->fmt
.pix
.pixelformat
;
645 const struct soc_camera_format_xlate
*xlate
;
648 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
650 dev_warn(ici
->v4l2_dev
.dev
, "Format %x not found\n", pixfmt
);
654 f
->fmt
.pix
.pixelformat
= xlate
->cam_fmt
->fourcc
;
655 ret
= v4l2_device_call_until_err(&ici
->v4l2_dev
, (__u32
)icd
, video
, s_fmt
, f
);
656 f
->fmt
.pix
.pixelformat
= pixfmt
;
658 icd
->buswidth
= xlate
->buswidth
;
659 icd
->current_fmt
= xlate
->host_fmt
;
660 pcdev
->camera_fmt
= xlate
->cam_fmt
;
666 static int sh_mobile_ceu_try_fmt(struct soc_camera_device
*icd
,
667 struct v4l2_format
*f
)
669 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
670 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
671 const struct soc_camera_format_xlate
*xlate
;
672 __u32 pixfmt
= f
->fmt
.pix
.pixelformat
;
675 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
677 dev_warn(ici
->v4l2_dev
.dev
, "Format %x not found\n", pixfmt
);
681 /* FIXME: calculate using depth and bus width */
683 v4l_bound_align_image(&f
->fmt
.pix
.width
, 2, 2560, 1,
684 &f
->fmt
.pix
.height
, 4, 1920, 2, 0);
686 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
*
687 DIV_ROUND_UP(xlate
->host_fmt
->depth
, 8);
688 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
690 f
->fmt
.pix
.pixelformat
= xlate
->cam_fmt
->fourcc
;
692 /* limit to sensor capabilities */
693 ret
= v4l2_device_call_until_err(&ici
->v4l2_dev
, (__u32
)icd
, video
, try_fmt
, f
);
694 f
->fmt
.pix
.pixelformat
= pixfmt
;
698 switch (f
->fmt
.pix
.field
) {
699 case V4L2_FIELD_INTERLACED
:
700 pcdev
->is_interlaced
= 1;
703 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
705 case V4L2_FIELD_NONE
:
706 pcdev
->is_interlaced
= 0;
716 static int sh_mobile_ceu_reqbufs(struct soc_camera_file
*icf
,
717 struct v4l2_requestbuffers
*p
)
721 /* This is for locking debugging only. I removed spinlocks and now I
722 * check whether .prepare is ever called on a linked buffer, or whether
723 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
724 * it hadn't triggered */
725 for (i
= 0; i
< p
->count
; i
++) {
726 struct sh_mobile_ceu_buffer
*buf
;
728 buf
= container_of(icf
->vb_vidq
.bufs
[i
],
729 struct sh_mobile_ceu_buffer
, vb
);
730 INIT_LIST_HEAD(&buf
->vb
.queue
);
736 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
738 struct soc_camera_file
*icf
= file
->private_data
;
739 struct sh_mobile_ceu_buffer
*buf
;
741 buf
= list_entry(icf
->vb_vidq
.stream
.next
,
742 struct sh_mobile_ceu_buffer
, vb
.stream
);
744 poll_wait(file
, &buf
->vb
.done
, pt
);
746 if (buf
->vb
.state
== VIDEOBUF_DONE
||
747 buf
->vb
.state
== VIDEOBUF_ERROR
)
748 return POLLIN
|POLLRDNORM
;
753 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
754 struct v4l2_capability
*cap
)
756 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
757 cap
->version
= KERNEL_VERSION(0, 0, 5);
758 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
762 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue
*q
,
763 struct soc_camera_device
*icd
)
765 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
766 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
768 videobuf_queue_dma_contig_init(q
,
769 &sh_mobile_ceu_videobuf_ops
,
770 ici
->v4l2_dev
.dev
, &pcdev
->lock
,
771 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
772 pcdev
->is_interlaced
?
773 V4L2_FIELD_INTERLACED
: V4L2_FIELD_NONE
,
774 sizeof(struct sh_mobile_ceu_buffer
),
778 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device
*icd
,
779 struct v4l2_control
*ctrl
)
781 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
782 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
786 case V4L2_CID_SHARPNESS
:
787 val
= ceu_read(pcdev
, CLFCR
);
788 ctrl
->value
= val
^ 1;
794 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device
*icd
,
795 struct v4l2_control
*ctrl
)
797 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
798 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
801 case V4L2_CID_SHARPNESS
:
802 switch (icd
->current_fmt
->fourcc
) {
803 case V4L2_PIX_FMT_NV12
:
804 case V4L2_PIX_FMT_NV21
:
805 case V4L2_PIX_FMT_NV16
:
806 case V4L2_PIX_FMT_NV61
:
807 ceu_write(pcdev
, CLFCR
, !ctrl
->value
);
815 static const struct v4l2_queryctrl sh_mobile_ceu_controls
[] = {
817 .id
= V4L2_CID_SHARPNESS
,
818 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
819 .name
= "Low-pass filter",
827 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
828 .owner
= THIS_MODULE
,
829 .add
= sh_mobile_ceu_add_device
,
830 .remove
= sh_mobile_ceu_remove_device
,
831 .get_formats
= sh_mobile_ceu_get_formats
,
832 .set_crop
= sh_mobile_ceu_set_crop
,
833 .set_fmt
= sh_mobile_ceu_set_fmt
,
834 .try_fmt
= sh_mobile_ceu_try_fmt
,
835 .set_ctrl
= sh_mobile_ceu_set_ctrl
,
836 .get_ctrl
= sh_mobile_ceu_get_ctrl
,
837 .reqbufs
= sh_mobile_ceu_reqbufs
,
838 .poll
= sh_mobile_ceu_poll
,
839 .querycap
= sh_mobile_ceu_querycap
,
840 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
841 .init_videobuf
= sh_mobile_ceu_init_videobuf
,
842 .controls
= sh_mobile_ceu_controls
,
843 .num_controls
= ARRAY_SIZE(sh_mobile_ceu_controls
),
846 static int __devinit
sh_mobile_ceu_probe(struct platform_device
*pdev
)
848 struct sh_mobile_ceu_dev
*pcdev
;
849 struct resource
*res
;
854 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
855 irq
= platform_get_irq(pdev
, 0);
857 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
862 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
864 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
869 INIT_LIST_HEAD(&pcdev
->capture
);
870 spin_lock_init(&pcdev
->lock
);
872 pcdev
->pdata
= pdev
->dev
.platform_data
;
875 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
879 base
= ioremap_nocache(res
->start
, resource_size(res
));
882 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
888 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
890 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
892 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
896 DMA_MEMORY_EXCLUSIVE
);
898 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
903 pcdev
->video_limit
= resource_size(res
);
907 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
908 dev_name(&pdev
->dev
), pcdev
);
910 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
911 goto exit_release_mem
;
914 pm_suspend_ignore_children(&pdev
->dev
, true);
915 pm_runtime_enable(&pdev
->dev
);
916 pm_runtime_resume(&pdev
->dev
);
918 pcdev
->ici
.priv
= pcdev
;
919 pcdev
->ici
.v4l2_dev
.dev
= &pdev
->dev
;
920 pcdev
->ici
.nr
= pdev
->id
;
921 pcdev
->ici
.drv_name
= dev_name(&pdev
->dev
);
922 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
;
924 err
= soc_camera_host_register(&pcdev
->ici
);
931 free_irq(pcdev
->irq
, pcdev
);
933 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
934 dma_release_declared_memory(&pdev
->dev
);
943 static int __devexit
sh_mobile_ceu_remove(struct platform_device
*pdev
)
945 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
946 struct sh_mobile_ceu_dev
*pcdev
= container_of(soc_host
,
947 struct sh_mobile_ceu_dev
, ici
);
949 soc_camera_host_unregister(soc_host
);
950 free_irq(pcdev
->irq
, pcdev
);
951 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
952 dma_release_declared_memory(&pdev
->dev
);
953 iounmap(pcdev
->base
);
958 static int sh_mobile_ceu_runtime_nop(struct device
*dev
)
960 /* Runtime PM callback shared between ->runtime_suspend()
961 * and ->runtime_resume(). Simply returns success.
963 * This driver re-initializes all registers after
964 * pm_runtime_get_sync() anyway so there is no need
965 * to save and restore registers here.
970 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops
= {
971 .runtime_suspend
= sh_mobile_ceu_runtime_nop
,
972 .runtime_resume
= sh_mobile_ceu_runtime_nop
,
975 static struct platform_driver sh_mobile_ceu_driver
= {
977 .name
= "sh_mobile_ceu",
978 .pm
= &sh_mobile_ceu_dev_pm_ops
,
980 .probe
= sh_mobile_ceu_probe
,
981 .remove
= __exit_p(sh_mobile_ceu_remove
),
984 static int __init
sh_mobile_ceu_init(void)
986 return platform_driver_register(&sh_mobile_ceu_driver
);
989 static void __exit
sh_mobile_ceu_exit(void)
991 platform_driver_unregister(&sh_mobile_ceu_driver
);
994 module_init(sh_mobile_ceu_init
);
995 module_exit(sh_mobile_ceu_exit
);
997 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
998 MODULE_AUTHOR("Magnus Damm");
999 MODULE_LICENSE("GPL");
1000 MODULE_ALIAS("platform:sh_mobile_ceu");