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 struct soc_camera_device *icd = vq->priv_data;
311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
315 spin_lock_irqsave(&pcdev->lock, flags);
317 if (pcdev->active == vb) {
318 /* disable capture (release DMA buffer), reset */
319 ceu_write(pcdev, CAPSR, 1 << 16);
320 pcdev->active = NULL;
323 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
324 !list_empty(&vb->queue)) {
325 vb->state = VIDEOBUF_ERROR;
326 list_del_init(&vb->queue);
329 spin_unlock_irqrestore(&pcdev->lock, flags);
331 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
334 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
335 .buf_setup = sh_mobile_ceu_videobuf_setup,
336 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
337 .buf_queue = sh_mobile_ceu_videobuf_queue,
338 .buf_release = sh_mobile_ceu_videobuf_release,
341 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
343 struct sh_mobile_ceu_dev *pcdev = data;
344 struct videobuf_buffer *vb;
347 spin_lock_irqsave(&pcdev->lock, flags);
351 /* Stale interrupt from a released buffer */
354 list_del_init(&vb->queue);
356 if (!list_empty(&pcdev->capture))
357 pcdev->active = list_entry(pcdev->capture.next,
358 struct videobuf_buffer, queue);
360 pcdev->active = NULL;
362 sh_mobile_ceu_capture(pcdev);
364 vb->state = VIDEOBUF_DONE;
365 do_gettimeofday(&vb->ts);
370 spin_unlock_irqrestore(&pcdev->lock, flags);
375 /* Called with .video_lock held */
376 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
378 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
379 struct sh_mobile_ceu_dev *pcdev = ici->priv;
385 "SuperH Mobile CEU driver attached to camera %d\n",
388 clk_enable(pcdev->clk);
390 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
391 while (ceu_read(pcdev, CSTSR) & 1)
399 /* Called with .video_lock held */
400 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
402 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
403 struct sh_mobile_ceu_dev *pcdev = ici->priv;
406 BUG_ON(icd != pcdev->icd);
408 /* disable capture, disable interrupts */
409 ceu_write(pcdev, CEIER, 0);
410 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
412 /* make sure active buffer is canceled */
413 spin_lock_irqsave(&pcdev->lock, flags);
415 list_del(&pcdev->active->queue);
416 pcdev->active->state = VIDEOBUF_ERROR;
417 wake_up_all(&pcdev->active->done);
418 pcdev->active = NULL;
420 spin_unlock_irqrestore(&pcdev->lock, flags);
422 clk_disable(pcdev->clk);
425 "SuperH Mobile CEU driver detached from camera %d\n",
431 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
434 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
435 struct sh_mobile_ceu_dev *pcdev = ici->priv;
436 int ret, buswidth, width, height, cfszr_width, cdwdr_width;
437 unsigned long camera_flags, common_flags, value;
438 int yuv_mode, yuv_lineskip;
440 camera_flags = icd->ops->query_bus_param(icd);
441 common_flags = soc_camera_bus_param_compatible(camera_flags,
442 make_bus_param(pcdev));
446 ret = icd->ops->set_bus_param(icd, common_flags);
450 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
451 case SOCAM_DATAWIDTH_8:
454 case SOCAM_DATAWIDTH_16:
461 ceu_write(pcdev, CRCNTR, 0);
462 ceu_write(pcdev, CRCMPR, 0);
464 value = 0x00000010; /* data fetch by default */
465 yuv_mode = yuv_lineskip = 0;
467 switch (icd->current_fmt->fourcc) {
468 case V4L2_PIX_FMT_NV12:
469 case V4L2_PIX_FMT_NV21:
470 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
472 case V4L2_PIX_FMT_NV16:
473 case V4L2_PIX_FMT_NV61:
475 switch (pcdev->camera_fmt->fourcc) {
476 case V4L2_PIX_FMT_UYVY:
477 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
479 case V4L2_PIX_FMT_VYUY:
480 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
482 case V4L2_PIX_FMT_YUYV:
483 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
485 case V4L2_PIX_FMT_YVYU:
486 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
493 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
494 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
495 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
497 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
498 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
499 value |= buswidth == 16 ? 1 << 12 : 0;
500 ceu_write(pcdev, CAMCR, value);
502 ceu_write(pcdev, CAPCR, 0x00300000);
503 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
508 width = icd->rect_current.width * 2;
509 width = buswidth == 16 ? width / 2 : width;
510 cfszr_width = cdwdr_width = icd->rect_current.width;
512 width = icd->rect_current.width *
513 ((icd->current_fmt->depth + 7) >> 3);
514 width = buswidth == 16 ? width / 2 : width;
515 cfszr_width = buswidth == 8 ? width / 2 : width;
516 cdwdr_width = buswidth == 16 ? width * 2 : width;
519 height = icd->rect_current.height;
520 if (pcdev->is_interlaced) {
525 ceu_write(pcdev, CAMOR, 0);
526 ceu_write(pcdev, CAPWR, (height << 16) | width);
527 ceu_write(pcdev, CFLCR, 0); /* no scaling */
528 ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
530 /* A few words about byte order (observed in Big Endian mode)
532 * In data fetch mode bytes are received in chunks of 8 bytes.
533 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
535 * The data is however by default written to memory in reverse order:
536 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
538 * The lowest three bits of CDOCR allows us to do swapping,
539 * using 7 we swap the data bytes to match the incoming order:
540 * D0, D1, D2, D3, D4, D5, D6, D7
544 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
546 ceu_write(pcdev, CDOCR, value);
548 ceu_write(pcdev, CDWDR, cdwdr_width);
549 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
551 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
555 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
557 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
558 struct sh_mobile_ceu_dev *pcdev = ici->priv;
559 unsigned long camera_flags, common_flags;
561 camera_flags = icd->ops->query_bus_param(icd);
562 common_flags = soc_camera_bus_param_compatible(camera_flags,
563 make_bus_param(pcdev));
570 static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
574 .fourcc = V4L2_PIX_FMT_NV12,
575 .colorspace = V4L2_COLORSPACE_JPEG,
580 .fourcc = V4L2_PIX_FMT_NV21,
581 .colorspace = V4L2_COLORSPACE_JPEG,
586 .fourcc = V4L2_PIX_FMT_NV16,
587 .colorspace = V4L2_COLORSPACE_JPEG,
592 .fourcc = V4L2_PIX_FMT_NV61,
593 .colorspace = V4L2_COLORSPACE_JPEG,
597 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
598 struct soc_camera_format_xlate *xlate)
600 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
604 ret = sh_mobile_ceu_try_bus_param(icd);
608 /* Beginning of a pass */
610 icd->host_priv = NULL;
612 switch (icd->formats[idx].fourcc) {
613 case V4L2_PIX_FMT_UYVY:
614 case V4L2_PIX_FMT_VYUY:
615 case V4L2_PIX_FMT_YUYV:
616 case V4L2_PIX_FMT_YVYU:
618 goto add_single_format;
621 * Our case is simple so far: for any of the above four camera
622 * formats we add all our four synthesized NV* formats, so,
623 * just marking the device with a single flag suffices. If
624 * the format generation rules are more complex, you would have
625 * to actually hang your already added / counted formats onto
626 * the host_priv pointer and check whether the format you're
627 * going to add now is already there.
629 icd->host_priv = (void *)sh_mobile_ceu_formats;
631 n = ARRAY_SIZE(sh_mobile_ceu_formats);
633 for (k = 0; xlate && k < n; k++) {
634 xlate->host_fmt = &sh_mobile_ceu_formats[k];
635 xlate->cam_fmt = icd->formats + idx;
636 xlate->buswidth = icd->formats[idx].depth;
638 dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n",
639 sh_mobile_ceu_formats[k].name,
640 icd->formats[idx].name);
644 /* Generic pass-through */
647 xlate->host_fmt = icd->formats + idx;
648 xlate->cam_fmt = icd->formats + idx;
649 xlate->buswidth = icd->formats[idx].depth;
651 dev_dbg(ici->v4l2_dev.dev,
652 "Providing format %s in pass-through mode\n",
653 icd->formats[idx].name);
660 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
661 struct v4l2_rect *rect)
663 return icd->ops->set_crop(icd, rect);
666 static int sh_mobile_ceu_set_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 __u32 pixfmt = f->fmt.pix.pixelformat;
672 const struct soc_camera_format_xlate *xlate;
675 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
677 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
681 f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
682 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f);
683 f->fmt.pix.pixelformat = pixfmt;
685 icd->buswidth = xlate->buswidth;
686 icd->current_fmt = xlate->host_fmt;
687 pcdev->camera_fmt = xlate->cam_fmt;
693 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
694 struct v4l2_format *f)
696 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
697 struct sh_mobile_ceu_dev *pcdev = ici->priv;
698 const struct soc_camera_format_xlate *xlate;
699 __u32 pixfmt = f->fmt.pix.pixelformat;
702 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
704 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
708 /* FIXME: calculate using depth and bus width */
710 v4l_bound_align_image(&f->fmt.pix.width, 2, 2560, 1,
711 &f->fmt.pix.height, 4, 1920, 2, 0);
713 f->fmt.pix.bytesperline = f->fmt.pix.width *
714 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
715 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
717 f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
719 /* limit to sensor capabilities */
720 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f);
721 f->fmt.pix.pixelformat = pixfmt;
725 switch (f->fmt.pix.field) {
726 case V4L2_FIELD_INTERLACED:
727 pcdev->is_interlaced = 1;
730 f->fmt.pix.field = V4L2_FIELD_NONE;
732 case V4L2_FIELD_NONE:
733 pcdev->is_interlaced = 0;
743 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
744 struct v4l2_requestbuffers *p)
748 /* This is for locking debugging only. I removed spinlocks and now I
749 * check whether .prepare is ever called on a linked buffer, or whether
750 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
751 * it hadn't triggered */
752 for (i = 0; i < p->count; i++) {
753 struct sh_mobile_ceu_buffer *buf;
755 buf = container_of(icf->vb_vidq.bufs[i],
756 struct sh_mobile_ceu_buffer, vb);
757 INIT_LIST_HEAD(&buf->vb.queue);
763 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
765 struct soc_camera_file *icf = file->private_data;
766 struct sh_mobile_ceu_buffer *buf;
768 buf = list_entry(icf->vb_vidq.stream.next,
769 struct sh_mobile_ceu_buffer, vb.stream);
771 poll_wait(file, &buf->vb.done, pt);
773 if (buf->vb.state == VIDEOBUF_DONE ||
774 buf->vb.state == VIDEOBUF_ERROR)
775 return POLLIN|POLLRDNORM;
780 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
781 struct v4l2_capability *cap)
783 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
784 cap->version = KERNEL_VERSION(0, 0, 5);
785 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
789 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
790 struct soc_camera_device *icd)
792 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
793 struct sh_mobile_ceu_dev *pcdev = ici->priv;
795 videobuf_queue_dma_contig_init(q,
796 &sh_mobile_ceu_videobuf_ops,
797 ici->v4l2_dev.dev, &pcdev->lock,
798 V4L2_BUF_TYPE_VIDEO_CAPTURE,
799 pcdev->is_interlaced ?
800 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
801 sizeof(struct sh_mobile_ceu_buffer),
805 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
806 struct v4l2_control *ctrl)
808 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
809 struct sh_mobile_ceu_dev *pcdev = ici->priv;
813 case V4L2_CID_SHARPNESS:
814 val = ceu_read(pcdev, CLFCR);
815 ctrl->value = val ^ 1;
821 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
822 struct v4l2_control *ctrl)
824 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
825 struct sh_mobile_ceu_dev *pcdev = ici->priv;
828 case V4L2_CID_SHARPNESS:
829 switch (icd->current_fmt->fourcc) {
830 case V4L2_PIX_FMT_NV12:
831 case V4L2_PIX_FMT_NV21:
832 case V4L2_PIX_FMT_NV16:
833 case V4L2_PIX_FMT_NV61:
834 ceu_write(pcdev, CLFCR, !ctrl->value);
842 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
844 .id = V4L2_CID_SHARPNESS,
845 .type = V4L2_CTRL_TYPE_BOOLEAN,
846 .name = "Low-pass filter",
854 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
855 .owner = THIS_MODULE,
856 .add = sh_mobile_ceu_add_device,
857 .remove = sh_mobile_ceu_remove_device,
858 .get_formats = sh_mobile_ceu_get_formats,
859 .set_crop = sh_mobile_ceu_set_crop,
860 .set_fmt = sh_mobile_ceu_set_fmt,
861 .try_fmt = sh_mobile_ceu_try_fmt,
862 .set_ctrl = sh_mobile_ceu_set_ctrl,
863 .get_ctrl = sh_mobile_ceu_get_ctrl,
864 .reqbufs = sh_mobile_ceu_reqbufs,
865 .poll = sh_mobile_ceu_poll,
866 .querycap = sh_mobile_ceu_querycap,
867 .set_bus_param = sh_mobile_ceu_set_bus_param,
868 .init_videobuf = sh_mobile_ceu_init_videobuf,
869 .controls = sh_mobile_ceu_controls,
870 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
873 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
875 struct sh_mobile_ceu_dev *pcdev;
876 struct resource *res;
881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
882 irq = platform_get_irq(pdev, 0);
884 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
889 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
891 dev_err(&pdev->dev, "Could not allocate pcdev\n");
896 INIT_LIST_HEAD(&pcdev->capture);
897 spin_lock_init(&pcdev->lock);
899 pcdev->pdata = pdev->dev.platform_data;
902 dev_err(&pdev->dev, "CEU platform data not set.\n");
906 base = ioremap_nocache(res->start, resource_size(res));
909 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
915 pcdev->video_limit = 0; /* only enabled if second resource exists */
917 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
919 err = dma_declare_coherent_memory(&pdev->dev, res->start,
923 DMA_MEMORY_EXCLUSIVE);
925 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
930 pcdev->video_limit = resource_size(res);
934 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
935 dev_name(&pdev->dev), pcdev);
937 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
938 goto exit_release_mem;
941 pm_suspend_ignore_children(&pdev->dev, true);
942 pm_runtime_enable(&pdev->dev);
943 pm_runtime_resume(&pdev->dev);
945 pcdev->ici.priv = pcdev;
946 pcdev->ici.v4l2_dev.dev = &pdev->dev;
947 pcdev->ici.nr = pdev->id;
948 pcdev->ici.drv_name = dev_name(&pdev->dev);
949 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
951 err = soc_camera_host_register(&pcdev->ici);
958 free_irq(pcdev->irq, pcdev);
960 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
961 dma_release_declared_memory(&pdev->dev);
970 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
972 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
973 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
974 struct sh_mobile_ceu_dev, ici);
976 soc_camera_host_unregister(soc_host);
977 free_irq(pcdev->irq, pcdev);
978 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
979 dma_release_declared_memory(&pdev->dev);
980 iounmap(pcdev->base);
985 static int sh_mobile_ceu_runtime_nop(struct device *dev)
987 /* Runtime PM callback shared between ->runtime_suspend()
988 * and ->runtime_resume(). Simply returns success.
990 * This driver re-initializes all registers after
991 * pm_runtime_get_sync() anyway so there is no need
992 * to save and restore registers here.
997 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
998 .runtime_suspend = sh_mobile_ceu_runtime_nop,
999 .runtime_resume = sh_mobile_ceu_runtime_nop,
1002 static struct platform_driver sh_mobile_ceu_driver = {
1004 .name = "sh_mobile_ceu",
1005 .pm = &sh_mobile_ceu_dev_pm_ops,
1007 .probe = sh_mobile_ceu_probe,
1008 .remove = __exit_p(sh_mobile_ceu_remove),
1011 static int __init sh_mobile_ceu_init(void)
1013 return platform_driver_register(&sh_mobile_ceu_driver);
1016 static void __exit sh_mobile_ceu_exit(void)
1018 platform_driver_unregister(&sh_mobile_ceu_driver);
1021 module_init(sh_mobile_ceu_init);
1022 module_exit(sh_mobile_ceu_exit);
1024 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1025 MODULE_AUTHOR("Magnus Damm");
1026 MODULE_LICENSE("GPL");
1027 MODULE_ALIAS("platform:sh_mobile_ceu");