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;
217 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
218 fmt->fmt.pix.pixelformat);
221 bytes_per_line = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
223 height = fmt->fmt.pix.height;
225 /* Called from VIDIOC_REQBUFS or in compatibility mode */
226 bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
227 icd->current_fmt->host_fmt);
228 height = icd->user_height;
230 if (bytes_per_line < 0)
231 return bytes_per_line;
233 sizes[0] = bytes_per_line * height;
235 alloc_ctxs[0] = pcdev->alloc_ctx;
237 if (!vq->num_buffers)
243 /* If *num_planes != 0, we have already verified *count. */
244 if (pcdev->video_limit && !*num_planes) {
245 size_t size = PAGE_ALIGN(sizes[0]) * *count;
247 if (size + pcdev->buf_total > pcdev->video_limit)
248 *count = (pcdev->video_limit - pcdev->buf_total) /
249 PAGE_ALIGN(sizes[0]);
254 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
259 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
260 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
261 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
262 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
263 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
264 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
268 * return value doesn't reflex the success/failure to queue the new buffer,
269 * but rather the status of the previous buffer.
271 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
273 struct soc_camera_device *icd = pcdev->icd;
274 dma_addr_t phys_addr_top, phys_addr_bottom;
275 unsigned long top1, top2;
276 unsigned long bottom1, bottom2;
282 * The hardware is _very_ picky about this sequence. Especially
283 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
284 * several not-so-well documented interrupt sources in CETCR.
286 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
287 status = ceu_read(pcdev, CETCR);
288 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
290 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
291 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
292 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
295 * When a VBP interrupt occurs, a capture end interrupt does not occur
296 * and the image of that frame is not captured correctly. So, soft reset
299 if (status & CEU_CEIER_VBP) {
300 sh_mobile_ceu_soft_reset(pcdev);
305 complete(&pcdev->complete);
312 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
324 phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0);
326 switch (icd->current_fmt->host_fmt->fourcc) {
327 case V4L2_PIX_FMT_NV12:
328 case V4L2_PIX_FMT_NV21:
329 case V4L2_PIX_FMT_NV16:
330 case V4L2_PIX_FMT_NV61:
337 ceu_write(pcdev, top1, phys_addr_top);
338 if (V4L2_FIELD_NONE != pcdev->field) {
340 phys_addr_bottom = phys_addr_top + icd->user_width;
342 phys_addr_bottom = phys_addr_top +
343 soc_mbus_bytes_per_line(icd->user_width,
344 icd->current_fmt->host_fmt);
345 ceu_write(pcdev, bottom1, phys_addr_bottom);
349 phys_addr_top += icd->user_width *
351 ceu_write(pcdev, top2, phys_addr_top);
352 if (V4L2_FIELD_NONE != pcdev->field) {
353 phys_addr_bottom = phys_addr_top + icd->user_width;
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);
380 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
381 icd->current_fmt->host_fmt);
383 if (bytes_per_line < 0)
386 size = icd->user_height * bytes_per_line;
388 if (vb2_plane_size(vb, 0) < size) {
389 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
390 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
394 vb2_set_plane_payload(vb, 0, size);
396 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
397 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
401 * This can be useful if you want to see if we actually fill
402 * the buffer with something
404 if (vb2_plane_vaddr(vb, 0))
405 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
408 spin_lock_irq(&pcdev->lock);
409 list_add_tail(&buf->queue, &pcdev->capture);
411 if (!pcdev->active) {
413 * Because there were no active buffer at this moment,
414 * we are not interested in the return value of
415 * sh_mobile_ceu_capture here.
418 sh_mobile_ceu_capture(pcdev);
420 spin_unlock_irq(&pcdev->lock);
425 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
428 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
430 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
431 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
432 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
433 struct sh_mobile_ceu_dev *pcdev = ici->priv;
435 spin_lock_irq(&pcdev->lock);
437 if (pcdev->active == vb) {
438 /* disable capture (release DMA buffer), reset */
439 ceu_write(pcdev, CAPSR, 1 << 16);
440 pcdev->active = NULL;
444 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
445 * buffer failed, and .buf_init() hasn't been called
448 list_del_init(&buf->queue);
450 pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
451 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
454 spin_unlock_irq(&pcdev->lock);
457 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
459 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
460 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
461 struct sh_mobile_ceu_dev *pcdev = ici->priv;
463 pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
464 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
467 /* This is for locking debugging only */
468 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
472 static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
474 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
475 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
476 struct sh_mobile_ceu_dev *pcdev = ici->priv;
477 struct list_head *buf_head, *tmp;
479 spin_lock_irq(&pcdev->lock);
481 pcdev->active = NULL;
483 list_for_each_safe(buf_head, tmp, &pcdev->capture)
484 list_del_init(buf_head);
486 spin_unlock_irq(&pcdev->lock);
488 return sh_mobile_ceu_soft_reset(pcdev);
491 static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
492 .queue_setup = sh_mobile_ceu_videobuf_setup,
493 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
494 .buf_queue = sh_mobile_ceu_videobuf_queue,
495 .buf_cleanup = sh_mobile_ceu_videobuf_release,
496 .buf_init = sh_mobile_ceu_videobuf_init,
497 .wait_prepare = soc_camera_unlock,
498 .wait_finish = soc_camera_lock,
499 .stop_streaming = sh_mobile_ceu_stop_streaming,
502 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
504 struct sh_mobile_ceu_dev *pcdev = data;
505 struct vb2_buffer *vb;
508 spin_lock(&pcdev->lock);
512 /* Stale interrupt from a released buffer */
515 list_del_init(&to_ceu_vb(vb)->queue);
517 if (!list_empty(&pcdev->capture))
518 pcdev->active = &list_entry(pcdev->capture.next,
519 struct sh_mobile_ceu_buffer, queue)->vb;
521 pcdev->active = NULL;
523 ret = sh_mobile_ceu_capture(pcdev);
524 do_gettimeofday(&vb->v4l2_buf.timestamp);
526 vb->v4l2_buf.field = pcdev->field;
527 vb->v4l2_buf.sequence = pcdev->sequence++;
529 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
532 spin_unlock(&pcdev->lock);
537 static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev)
539 struct v4l2_subdev *sd;
541 if (!pcdev->csi2_pdev)
544 v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev)
545 if (&pcdev->csi2_pdev->dev == v4l2_get_subdevdata(sd))
551 /* Called with .video_lock held */
552 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
554 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
555 struct sh_mobile_ceu_dev *pcdev = ici->priv;
556 struct v4l2_subdev *csi2_sd;
562 dev_info(icd->parent,
563 "SuperH Mobile CEU driver attached to camera %d\n",
566 pm_runtime_get_sync(ici->v4l2_dev.dev);
568 pcdev->buf_total = 0;
570 ret = sh_mobile_ceu_soft_reset(pcdev);
572 csi2_sd = find_csi2(pcdev);
574 csi2_sd->grp_id = soc_camera_grp_id(icd);
575 v4l2_set_subdev_hostdata(csi2_sd, icd);
578 ret = v4l2_subdev_call(csi2_sd, core, s_power, 1);
579 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) {
580 pm_runtime_put_sync(ici->v4l2_dev.dev);
585 * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
586 * has not found this soc-camera device among its clients
588 if (ret == -ENODEV && csi2_sd)
595 /* Called with .video_lock held */
596 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
598 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
599 struct sh_mobile_ceu_dev *pcdev = ici->priv;
600 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
602 BUG_ON(icd != pcdev->icd);
604 v4l2_subdev_call(csi2_sd, core, s_power, 0);
607 /* disable capture, disable interrupts */
608 ceu_write(pcdev, CEIER, 0);
609 sh_mobile_ceu_soft_reset(pcdev);
611 /* make sure active buffer is canceled */
612 spin_lock_irq(&pcdev->lock);
614 list_del_init(&to_ceu_vb(pcdev->active)->queue);
615 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
616 pcdev->active = NULL;
618 spin_unlock_irq(&pcdev->lock);
620 pm_runtime_put_sync(ici->v4l2_dev.dev);
622 dev_info(icd->parent,
623 "SuperH Mobile CEU driver detached from camera %d\n",
630 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
631 * in SH7722 Hardware Manual
633 static unsigned int size_dst(unsigned int src, unsigned int scale)
635 unsigned int mant_pre = scale >> 12;
638 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
639 mant_pre * 4096 / scale + 1;
642 static u16 calc_scale(unsigned int src, unsigned int *dst)
649 scale = (src * 4096 / *dst) & ~7;
651 while (scale > 4096 && size_dst(src, scale) < *dst)
654 *dst = size_dst(src, scale);
659 /* rect is guaranteed to not exceed the scaled camera rectangle */
660 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
662 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
663 struct sh_mobile_ceu_cam *cam = icd->host_priv;
664 struct sh_mobile_ceu_dev *pcdev = ici->priv;
665 unsigned int height, width, cdwdr_width, in_width, in_height;
666 unsigned int left_offset, top_offset;
669 dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
670 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
672 left_offset = cam->ceu_left;
673 top_offset = cam->ceu_top;
675 WARN_ON(icd->user_width & 3 || icd->user_height & 3);
677 width = icd->user_width;
679 if (pcdev->image_mode) {
680 in_width = cam->width;
681 if (!pcdev->is_16bit) {
687 int bytes_per_line = soc_mbus_bytes_per_line(width,
688 icd->current_fmt->host_fmt);
689 unsigned int w_factor;
691 switch (icd->current_fmt->host_fmt->packing) {
692 case SOC_MBUS_PACKING_2X8_PADHI:
699 in_width = cam->width * w_factor;
700 left_offset *= w_factor;
702 if (bytes_per_line < 0)
705 cdwdr_width = bytes_per_line;
708 height = icd->user_height;
709 in_height = cam->height;
710 if (V4L2_FIELD_NONE != pcdev->field) {
711 height = (height / 2) & ~3;
717 /* CSI2 special configuration */
718 if (pcdev->pdata->csi2) {
719 in_width = ((in_width - 2) * 2);
723 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
724 camor = left_offset | (top_offset << 16);
727 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
728 (in_height << 16) | in_width, (height << 16) | width,
731 ceu_write(pcdev, CAMOR, camor);
732 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
733 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
734 ceu_write(pcdev, CFSZR, (height << 16) | width);
735 ceu_write(pcdev, CDWDR, cdwdr_width);
738 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
740 u32 capsr = ceu_read(pcdev, CAPSR);
741 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
745 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
747 unsigned long timeout = jiffies + 10 * HZ;
750 * Wait until the end of the current frame. It can take a long time,
751 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
753 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
756 if (time_after(jiffies, timeout)) {
757 dev_err(pcdev->ici.v4l2_dev.dev,
758 "Timeout waiting for frame end! Interface problem?\n");
762 /* Wait until reset clears, this shall not hang... */
763 while (ceu_read(pcdev, CAPSR) & (1 << 16))
766 /* Anything to restore? */
767 if (capsr & ~(1 << 16))
768 ceu_write(pcdev, CAPSR, capsr);
771 /* Find the bus subdevice driver, e.g., CSI2 */
772 static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev,
773 struct soc_camera_device *icd)
775 if (pcdev->csi2_pdev) {
776 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
777 if (csi2_sd && csi2_sd->grp_id == soc_camera_grp_id(icd))
781 return soc_camera_to_subdev(icd);
784 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
785 V4L2_MBUS_PCLK_SAMPLE_RISING | \
786 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
787 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
788 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
789 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
790 V4L2_MBUS_DATA_ACTIVE_HIGH)
792 /* Capture is not running, no interrupts, no locking needed */
793 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
795 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
796 struct sh_mobile_ceu_dev *pcdev = ici->priv;
797 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
798 struct sh_mobile_ceu_cam *cam = icd->host_priv;
799 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
800 unsigned long value, common_flags = CEU_BUS_FLAGS;
801 u32 capsr = capture_save_reset(pcdev);
802 unsigned int yuv_lineskip;
806 * If the client doesn't implement g_mbus_config, we just use our
809 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
811 common_flags = soc_mbus_config_compatible(&cfg,
815 } else if (ret != -ENOIOCTLCMD) {
819 /* Make choises, based on platform preferences */
820 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
821 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
822 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
823 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
825 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
828 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
829 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
830 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
831 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
833 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
836 cfg.flags = common_flags;
837 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
838 if (ret < 0 && ret != -ENOIOCTLCMD)
841 if (icd->current_fmt->host_fmt->bits_per_sample > 8)
846 ceu_write(pcdev, CRCNTR, 0);
847 ceu_write(pcdev, CRCMPR, 0);
849 value = 0x00000010; /* data fetch by default */
852 switch (icd->current_fmt->host_fmt->fourcc) {
853 case V4L2_PIX_FMT_NV12:
854 case V4L2_PIX_FMT_NV21:
855 /* convert 4:2:2 -> 4:2:0 */
856 yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */
858 case V4L2_PIX_FMT_NV16:
859 case V4L2_PIX_FMT_NV61:
861 case V4L2_MBUS_FMT_UYVY8_2X8:
862 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
864 case V4L2_MBUS_FMT_VYUY8_2X8:
865 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
867 case V4L2_MBUS_FMT_YUYV8_2X8:
868 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
870 case V4L2_MBUS_FMT_YVYU8_2X8:
871 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
878 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
879 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
880 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
882 value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
883 value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
884 value |= pcdev->is_16bit ? 1 << 12 : 0;
887 if (pcdev->pdata->csi2)
890 ceu_write(pcdev, CAMCR, value);
892 ceu_write(pcdev, CAPCR, 0x00300000);
894 switch (pcdev->field) {
895 case V4L2_FIELD_INTERLACED_TB:
898 case V4L2_FIELD_INTERLACED_BT:
905 ceu_write(pcdev, CAIFR, value);
907 sh_mobile_ceu_set_rect(icd);
910 dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
911 ceu_write(pcdev, CFLCR, pcdev->cflcr);
914 * A few words about byte order (observed in Big Endian mode)
916 * In data fetch mode bytes are received in chunks of 8 bytes.
917 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
919 * The data is however by default written to memory in reverse order:
920 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
922 * The lowest three bits of CDOCR allows us to do swapping,
923 * using 7 we swap the data bytes to match the incoming order:
924 * D0, D1, D2, D3, D4, D5, D6, D7
926 value = 0x00000007 | yuv_lineskip;
928 ceu_write(pcdev, CDOCR, value);
929 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
931 capture_restore(pcdev, capsr);
933 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
937 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
938 unsigned char buswidth)
940 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
941 struct sh_mobile_ceu_dev *pcdev = ici->priv;
942 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
943 unsigned long common_flags = CEU_BUS_FLAGS;
944 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
947 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
949 common_flags = soc_mbus_config_compatible(&cfg,
951 else if (ret != -ENOIOCTLCMD)
954 if (!common_flags || buswidth > 16)
960 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
962 .fourcc = V4L2_PIX_FMT_NV12,
964 .bits_per_sample = 8,
965 .packing = SOC_MBUS_PACKING_1_5X8,
966 .order = SOC_MBUS_ORDER_LE,
968 .fourcc = V4L2_PIX_FMT_NV21,
970 .bits_per_sample = 8,
971 .packing = SOC_MBUS_PACKING_1_5X8,
972 .order = SOC_MBUS_ORDER_LE,
974 .fourcc = V4L2_PIX_FMT_NV16,
976 .bits_per_sample = 8,
977 .packing = SOC_MBUS_PACKING_2X8_PADHI,
978 .order = SOC_MBUS_ORDER_LE,
980 .fourcc = V4L2_PIX_FMT_NV61,
982 .bits_per_sample = 8,
983 .packing = SOC_MBUS_PACKING_2X8_PADHI,
984 .order = SOC_MBUS_ORDER_LE,
988 /* This will be corrected as we get more formats */
989 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
991 return fmt->packing == SOC_MBUS_PACKING_NONE ||
992 (fmt->bits_per_sample == 8 &&
993 fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
994 (fmt->bits_per_sample == 8 &&
995 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
996 (fmt->bits_per_sample > 8 &&
997 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
1000 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
1002 static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
1004 return container_of(ctrl->handler, struct soc_camera_device,
1008 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
1010 struct soc_camera_device *icd = ctrl_to_icd(ctrl);
1011 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1012 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1015 case V4L2_CID_SHARPNESS:
1016 switch (icd->current_fmt->host_fmt->fourcc) {
1017 case V4L2_PIX_FMT_NV12:
1018 case V4L2_PIX_FMT_NV21:
1019 case V4L2_PIX_FMT_NV16:
1020 case V4L2_PIX_FMT_NV61:
1021 ceu_write(pcdev, CLFCR, !ctrl->val);
1030 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
1031 .s_ctrl = sh_mobile_ceu_s_ctrl,
1034 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
1035 struct soc_camera_format_xlate *xlate)
1037 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1038 struct device *dev = icd->parent;
1039 struct soc_camera_host *ici = to_soc_camera_host(dev);
1040 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1043 struct sh_mobile_ceu_cam *cam;
1044 enum v4l2_mbus_pixelcode code;
1045 const struct soc_mbus_pixelfmt *fmt;
1047 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
1049 /* No more formats */
1052 fmt = soc_mbus_get_fmtdesc(code);
1054 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
1058 if (!pcdev->pdata->csi2) {
1059 /* Are there any restrictions in the CSI-2 case? */
1060 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
1065 if (!icd->host_priv) {
1066 struct v4l2_mbus_framefmt mf;
1067 struct v4l2_rect rect;
1070 /* Add our control */
1071 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
1072 V4L2_CID_SHARPNESS, 0, 1, 1, 0);
1073 if (icd->ctrl_handler.error)
1074 return icd->ctrl_handler.error;
1076 /* FIXME: subwindow is lost between close / open */
1078 /* Cache current client geometry */
1079 ret = client_g_rect(sd, &rect);
1084 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1089 * All currently existing CEU implementations support 2560x1920
1090 * or larger frames. If the sensor is proposing too big a frame,
1091 * don't bother with possibly supportred by the CEU larger
1092 * sizes, just try VGA multiples. If needed, this can be
1093 * adjusted in the future.
1095 while ((mf.width > pcdev->max_width ||
1096 mf.height > pcdev->max_height) && shift < 4) {
1097 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1098 mf.width = 2560 >> shift;
1099 mf.height = 1920 >> shift;
1100 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1101 soc_camera_grp_id(icd), video,
1109 dev_err(dev, "Failed to configure the client below %ux%x\n",
1110 mf.width, mf.height);
1114 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
1116 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1120 /* We are called with current camera crop, initialise subrect with it */
1122 cam->subrect = rect;
1124 cam->width = mf.width;
1125 cam->height = mf.height;
1127 icd->host_priv = cam;
1129 cam = icd->host_priv;
1132 /* Beginning of a pass */
1134 cam->extra_fmt = NULL;
1137 case V4L2_MBUS_FMT_UYVY8_2X8:
1138 case V4L2_MBUS_FMT_VYUY8_2X8:
1139 case V4L2_MBUS_FMT_YUYV8_2X8:
1140 case V4L2_MBUS_FMT_YVYU8_2X8:
1145 * Our case is simple so far: for any of the above four camera
1146 * formats we add all our four synthesized NV* formats, so,
1147 * just marking the device with a single flag suffices. If
1148 * the format generation rules are more complex, you would have
1149 * to actually hang your already added / counted formats onto
1150 * the host_priv pointer and check whether the format you're
1151 * going to add now is already there.
1153 cam->extra_fmt = sh_mobile_ceu_formats;
1155 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1157 for (k = 0; xlate && k < n; k++) {
1158 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1161 dev_dbg(dev, "Providing format %s using code %d\n",
1162 sh_mobile_ceu_formats[k].name, code);
1166 if (!sh_mobile_ceu_packing_supported(fmt))
1170 /* Generic pass-through */
1173 xlate->host_fmt = fmt;
1176 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1183 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1185 kfree(icd->host_priv);
1186 icd->host_priv = NULL;
1189 /* Check if any dimension of r1 is smaller than respective one of r2 */
1190 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1192 return r1->width < r2->width || r1->height < r2->height;
1195 /* Check if r1 fails to cover r2 */
1196 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1198 return r1->left > r2->left || r1->top > r2->top ||
1199 r1->left + r1->width < r2->left + r2->width ||
1200 r1->top + r1->height < r2->top + r2->height;
1203 static unsigned int scale_down(unsigned int size, unsigned int scale)
1205 return (size * 4096 + scale / 2) / scale;
1208 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1210 return (input * 4096 + output / 2) / output;
1213 /* Get and store current client crop */
1214 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1216 struct v4l2_crop crop;
1217 struct v4l2_cropcap cap;
1220 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1222 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1228 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1229 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1231 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1233 *rect = cap.defrect;
1238 /* Client crop has changed, update our sub-rectangle to remain within the area */
1239 static void update_subrect(struct sh_mobile_ceu_cam *cam)
1241 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1243 if (rect->width < subrect->width)
1244 subrect->width = rect->width;
1246 if (rect->height < subrect->height)
1247 subrect->height = rect->height;
1249 if (rect->left > subrect->left)
1250 subrect->left = rect->left;
1251 else if (rect->left + rect->width >
1252 subrect->left + subrect->width)
1253 subrect->left = rect->left + rect->width -
1256 if (rect->top > subrect->top)
1257 subrect->top = rect->top;
1258 else if (rect->top + rect->height >
1259 subrect->top + subrect->height)
1260 subrect->top = rect->top + rect->height -
1265 * The common for both scaling and cropping iterative approach is:
1266 * 1. try if the client can produce exactly what requested by the user
1267 * 2. if (1) failed, try to double the client image until we get one big enough
1268 * 3. if (2) failed, try to request the maximum image
1270 static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
1271 struct v4l2_crop *cam_crop)
1273 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1274 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1275 struct device *dev = sd->v4l2_dev->dev;
1276 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1277 struct v4l2_cropcap cap;
1279 unsigned int width, height;
1281 v4l2_subdev_call(sd, video, s_crop, crop);
1282 ret = client_g_rect(sd, cam_rect);
1287 * Now cam_crop contains the current camera input rectangle, and it must
1288 * be within camera cropcap bounds
1290 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1291 /* Even if camera S_CROP failed, but camera rectangle matches */
1292 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
1293 rect->width, rect->height, rect->left, rect->top);
1294 cam->rect = *cam_rect;
1298 /* Try to fix cropping, that camera hasn't managed to set */
1299 dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
1300 cam_rect->width, cam_rect->height,
1301 cam_rect->left, cam_rect->top,
1302 rect->width, rect->height, rect->left, rect->top);
1304 /* We need sensor maximum rectangle */
1305 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1309 /* Put user requested rectangle within sensor bounds */
1310 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1312 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1316 * Popular special case - some cameras can only handle fixed sizes like
1317 * QVGA, VGA,... Take care to avoid infinite loop.
1319 width = max(cam_rect->width, 2);
1320 height = max(cam_rect->height, 2);
1323 * Loop as long as sensor is not covering the requested rectangle and
1324 * is still within its bounds
1326 while (!ret && (is_smaller(cam_rect, rect) ||
1327 is_inside(cam_rect, rect)) &&
1328 (cap.bounds.width > width || cap.bounds.height > height)) {
1333 cam_rect->width = width;
1334 cam_rect->height = height;
1337 * We do not know what capabilities the camera has to set up
1338 * left and top borders. We could try to be smarter in iterating
1339 * them, e.g., if camera current left is to the right of the
1340 * target left, set it to the middle point between the current
1341 * left and minimum left. But that would add too much
1342 * complexity: we would have to iterate each border separately.
1343 * Instead we just drop to the left and top bounds.
1345 if (cam_rect->left > rect->left)
1346 cam_rect->left = cap.bounds.left;
1348 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1349 cam_rect->width = rect->left + rect->width -
1352 if (cam_rect->top > rect->top)
1353 cam_rect->top = cap.bounds.top;
1355 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1356 cam_rect->height = rect->top + rect->height -
1359 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1360 ret = client_g_rect(sd, cam_rect);
1361 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
1362 cam_rect->width, cam_rect->height,
1363 cam_rect->left, cam_rect->top);
1366 /* S_CROP must not modify the rectangle */
1367 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1369 * The camera failed to configure a suitable cropping,
1370 * we cannot use the current rectangle, set to max
1372 *cam_rect = cap.bounds;
1373 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1374 ret = client_g_rect(sd, cam_rect);
1375 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
1376 cam_rect->width, cam_rect->height,
1377 cam_rect->left, cam_rect->top);
1381 cam->rect = *cam_rect;
1382 update_subrect(cam);
1388 /* Iterative s_mbus_fmt, also updates cached client crop on success */
1389 static int client_s_fmt(struct soc_camera_device *icd,
1390 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
1392 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1393 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1394 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1395 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1396 struct device *dev = icd->parent;
1397 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
1398 unsigned int max_width, max_height;
1399 struct v4l2_cropcap cap;
1403 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1404 soc_camera_grp_id(icd), video,
1409 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1411 if (width == mf->width && height == mf->height) {
1412 /* Perfect! The client has done it all. */
1421 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1423 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1427 max_width = min(cap.bounds.width, pcdev->max_width);
1428 max_height = min(cap.bounds.height, pcdev->max_height);
1430 /* Camera set a format, but geometry is not precise, try to improve */
1434 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1435 while ((width > tmp_w || height > tmp_h) &&
1436 tmp_w < max_width && tmp_h < max_height) {
1437 tmp_w = min(2 * tmp_w, max_width);
1438 tmp_h = min(2 * tmp_h, max_height);
1441 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1442 soc_camera_grp_id(icd), video,
1444 dev_geo(dev, "Camera scaled to %ux%u\n",
1445 mf->width, mf->height);
1447 /* This shouldn't happen */
1448 dev_err(dev, "Client failed to set format: %d\n", ret);
1455 ret = client_g_rect(sd, &cam->rect);
1460 cam->subrect = cam->rect;
1462 update_subrect(cam);
1468 * @width - on output: user width, mapped back to input
1469 * @height - on output: user height, mapped back to input
1470 * @mf - in- / output camera output window
1472 static int client_scale(struct soc_camera_device *icd,
1473 struct v4l2_mbus_framefmt *mf,
1474 unsigned int *width, unsigned int *height,
1477 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1478 struct device *dev = icd->parent;
1479 struct v4l2_mbus_framefmt mf_tmp = *mf;
1480 unsigned int scale_h, scale_v;
1484 * 5. Apply iterative camera S_FMT for camera user window (also updates
1485 * client crop cache and the imaginary sub-rectangle).
1487 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
1491 dev_geo(dev, "5: camera scaled to %ux%u\n",
1492 mf_tmp.width, mf_tmp.height);
1494 /* 6. Retrieve camera output window (g_fmt) */
1496 /* unneeded - it is already in "mf_tmp" */
1498 /* 7. Calculate new client scales. */
1499 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1500 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
1502 mf->width = mf_tmp.width;
1503 mf->height = mf_tmp.height;
1504 mf->colorspace = mf_tmp.colorspace;
1507 * 8. Calculate new CEU crop - apply camera scales to previously
1508 * updated "effective" crop.
1510 *width = scale_down(cam->subrect.width, scale_h);
1511 *height = scale_down(cam->subrect.height, scale_v);
1513 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
1519 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1520 * framerate by always requesting the maximum image from the client. See
1521 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1522 * scaling and cropping algorithms and for the meaning of referenced here steps.
1524 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1525 struct v4l2_crop *a)
1527 struct v4l2_rect *rect = &a->c;
1528 struct device *dev = icd->parent;
1529 struct soc_camera_host *ici = to_soc_camera_host(dev);
1530 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1531 struct v4l2_crop cam_crop;
1532 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1533 struct v4l2_rect *cam_rect = &cam_crop.c;
1534 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1535 struct v4l2_mbus_framefmt mf;
1536 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1537 out_width, out_height;
1538 int interm_width, interm_height;
1542 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1543 rect->left, rect->top);
1545 /* During camera cropping its output window can change too, stop CEU */
1546 capsr = capture_save_reset(pcdev);
1547 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1550 * 1. - 2. Apply iterative camera S_CROP for new input window, read back
1551 * actual camera rectangle.
1553 ret = client_s_crop(icd, a, &cam_crop);
1557 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
1558 cam_rect->width, cam_rect->height,
1559 cam_rect->left, cam_rect->top);
1561 /* On success cam_crop contains current camera crop */
1563 /* 3. Retrieve camera output window */
1564 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1568 if (mf.width > pcdev->max_width || mf.height > pcdev->max_height)
1571 /* 4. Calculate camera scales */
1572 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1573 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1575 /* Calculate intermediate window */
1576 interm_width = scale_down(rect->width, scale_cam_h);
1577 interm_height = scale_down(rect->height, scale_cam_v);
1579 if (interm_width < icd->user_width) {
1582 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1584 mf.width = scale_down(cam_rect->width, new_scale_h);
1587 if (interm_height < icd->user_height) {
1590 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1592 mf.height = scale_down(cam_rect->height, new_scale_v);
1595 if (interm_width < icd->user_width || interm_height < icd->user_height) {
1596 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1597 soc_camera_grp_id(icd), video,
1602 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1603 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1604 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1605 interm_width = scale_down(rect->width, scale_cam_h);
1606 interm_height = scale_down(rect->height, scale_cam_v);
1609 /* Cache camera output window */
1610 cam->width = mf.width;
1611 cam->height = mf.height;
1613 if (pcdev->image_mode) {
1614 out_width = min(interm_width, icd->user_width);
1615 out_height = min(interm_height, icd->user_height);
1617 out_width = interm_width;
1618 out_height = interm_height;
1622 * 5. Calculate CEU scales from camera scales from results of (5) and
1625 scale_ceu_h = calc_scale(interm_width, &out_width);
1626 scale_ceu_v = calc_scale(interm_height, &out_height);
1628 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1630 /* Apply CEU scales. */
1631 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1632 if (cflcr != pcdev->cflcr) {
1633 pcdev->cflcr = cflcr;
1634 ceu_write(pcdev, CFLCR, cflcr);
1637 icd->user_width = out_width & ~3;
1638 icd->user_height = out_height & ~3;
1639 /* Offsets are applied at the CEU scaling filter input */
1640 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1641 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
1643 /* 6. Use CEU cropping to crop to the new window. */
1644 sh_mobile_ceu_set_rect(icd);
1646 cam->subrect = *rect;
1648 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1649 icd->user_width, icd->user_height,
1650 cam->ceu_left, cam->ceu_top);
1652 /* Restore capture. The CE bit can be cleared by the hardware */
1655 capture_restore(pcdev, capsr);
1657 /* Even if only camera cropping succeeded */
1661 static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1662 struct v4l2_crop *a)
1664 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1666 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1667 a->c = cam->subrect;
1673 * Calculate real client output window by applying new scales to the current
1674 * client crop. New scales are calculated from the requested output format and
1675 * CEU crop, mapped backed onto the client input (subrect).
1677 static void calculate_client_output(struct soc_camera_device *icd,
1678 const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
1680 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1681 struct device *dev = icd->parent;
1682 struct v4l2_rect *cam_subrect = &cam->subrect;
1683 unsigned int scale_v, scale_h;
1685 if (cam_subrect->width == cam->rect.width &&
1686 cam_subrect->height == cam->rect.height) {
1687 /* No sub-cropping */
1688 mf->width = pix->width;
1689 mf->height = pix->height;
1693 /* 1.-2. Current camera scales and subwin - cached. */
1695 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1696 cam_subrect->width, cam_subrect->height,
1697 cam_subrect->left, cam_subrect->top);
1700 * 3. Calculate new combined scales from input sub-window to requested
1705 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1706 * (128x96) or larger than VGA
1708 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1709 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1711 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1714 * 4. Calculate desired client output window by applying combined scales
1715 * to client (real) input window.
1717 mf->width = scale_down(cam->rect.width, scale_h);
1718 mf->height = scale_down(cam->rect.height, scale_v);
1721 /* Similar to set_crop multistage iterative algorithm */
1722 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1723 struct v4l2_format *f)
1725 struct device *dev = icd->parent;
1726 struct soc_camera_host *ici = to_soc_camera_host(dev);
1727 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1728 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1729 struct v4l2_pix_format *pix = &f->fmt.pix;
1730 struct v4l2_mbus_framefmt mf;
1731 __u32 pixfmt = pix->pixelformat;
1732 const struct soc_camera_format_xlate *xlate;
1733 /* Keep Compiler Happy */
1734 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
1735 u16 scale_v, scale_h;
1738 enum v4l2_field field;
1740 switch (pix->field) {
1742 pix->field = V4L2_FIELD_NONE;
1744 case V4L2_FIELD_INTERLACED_TB:
1745 case V4L2_FIELD_INTERLACED_BT:
1746 case V4L2_FIELD_NONE:
1749 case V4L2_FIELD_INTERLACED:
1750 field = V4L2_FIELD_INTERLACED_TB;
1754 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1756 dev_warn(dev, "Format %x not found\n", pixfmt);
1760 /* 1.-4. Calculate desired client output geometry */
1761 calculate_client_output(icd, pix, &mf);
1762 mf.field = pix->field;
1763 mf.colorspace = pix->colorspace;
1764 mf.code = xlate->code;
1767 case V4L2_PIX_FMT_NV12:
1768 case V4L2_PIX_FMT_NV21:
1769 case V4L2_PIX_FMT_NV16:
1770 case V4L2_PIX_FMT_NV61:
1777 dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
1778 pix->width, pix->height);
1780 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
1783 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
1784 image_mode && V4L2_FIELD_NONE == field);
1786 dev_geo(dev, "5-9: client scale return %d\n", ret);
1788 /* Done with the camera. Now see if we can improve the result */
1790 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1791 mf.width, mf.height, pix->width, pix->height);
1795 if (mf.code != xlate->code)
1798 /* 9. Prepare CEU crop */
1799 cam->width = mf.width;
1800 cam->height = mf.height;
1802 /* 10. Use CEU scaling to scale to the requested user window. */
1804 /* We cannot scale up */
1805 if (pix->width > ceu_sub_width)
1806 ceu_sub_width = pix->width;
1808 if (pix->height > ceu_sub_height)
1809 ceu_sub_height = pix->height;
1811 pix->colorspace = mf.colorspace;
1814 /* Scale pix->{width x height} down to width x height */
1815 scale_h = calc_scale(ceu_sub_width, &pix->width);
1816 scale_v = calc_scale(ceu_sub_height, &pix->height);
1818 pix->width = ceu_sub_width;
1819 pix->height = ceu_sub_height;
1824 pcdev->cflcr = scale_h | (scale_v << 16);
1827 * We have calculated CFLCR, the actual configuration will be performed
1828 * in sh_mobile_ceu_set_bus_param()
1831 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1832 ceu_sub_width, scale_h, pix->width,
1833 ceu_sub_height, scale_v, pix->height);
1835 cam->code = xlate->code;
1836 icd->current_fmt = xlate;
1838 pcdev->field = field;
1839 pcdev->image_mode = image_mode;
1841 /* CFSZR requirement */
1848 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1849 struct v4l2_format *f)
1851 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1852 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1853 const struct soc_camera_format_xlate *xlate;
1854 struct v4l2_pix_format *pix = &f->fmt.pix;
1855 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1856 struct v4l2_mbus_framefmt mf;
1857 __u32 pixfmt = pix->pixelformat;
1861 dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1862 pixfmt, pix->width, pix->height);
1864 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1866 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
1870 /* FIXME: calculate using depth and bus width */
1872 /* CFSZR requires height and width to be 4-pixel aligned */
1873 v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
1874 &pix->height, 4, pcdev->max_height, 2, 0);
1877 height = pix->height;
1879 /* limit to sensor capabilities */
1880 mf.width = pix->width;
1881 mf.height = pix->height;
1882 mf.field = pix->field;
1883 mf.code = xlate->code;
1884 mf.colorspace = pix->colorspace;
1886 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1887 video, try_mbus_fmt, &mf);
1891 pix->width = mf.width;
1892 pix->height = mf.height;
1893 pix->field = mf.field;
1894 pix->colorspace = mf.colorspace;
1897 case V4L2_PIX_FMT_NV12:
1898 case V4L2_PIX_FMT_NV21:
1899 case V4L2_PIX_FMT_NV16:
1900 case V4L2_PIX_FMT_NV61:
1901 /* FIXME: check against rect_max after converting soc-camera */
1902 /* We can scale precisely, need a bigger image from camera */
1903 if (pix->width < width || pix->height < height) {
1905 * We presume, the sensor behaves sanely, i.e., if
1906 * requested a bigger rectangle, it will not return a
1909 mf.width = pcdev->max_width;
1910 mf.height = pcdev->max_height;
1911 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1912 soc_camera_grp_id(icd), video,
1915 /* Shouldn't actually happen... */
1916 dev_err(icd->parent,
1917 "FIXME: client try_fmt() = %d\n", ret);
1921 /* We will scale exactly */
1922 if (mf.width > width)
1924 if (mf.height > height)
1925 pix->height = height;
1931 dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1932 __func__, ret, pix->pixelformat, pix->width, pix->height);
1937 static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1938 struct v4l2_crop *a)
1940 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1941 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1942 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1943 u32 out_width = icd->user_width, out_height = icd->user_height;
1948 /* Wait for frame */
1949 ret = wait_for_completion_interruptible(&pcdev->complete);
1950 /* Stop the client */
1951 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1953 dev_warn(icd->parent,
1954 "Client failed to stop the stream: %d\n", ret);
1956 /* Do the crop, if it fails, there's nothing more we can do */
1957 sh_mobile_ceu_set_crop(icd, a);
1959 dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1961 if (icd->user_width != out_width || icd->user_height != out_height) {
1962 struct v4l2_format f = {
1963 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1966 .height = out_height,
1967 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1968 .field = pcdev->field,
1969 .colorspace = icd->colorspace,
1972 ret = sh_mobile_ceu_set_fmt(icd, &f);
1973 if (!ret && (out_width != f.fmt.pix.width ||
1974 out_height != f.fmt.pix.height))
1977 icd->user_width = out_width & ~3;
1978 icd->user_height = out_height & ~3;
1979 ret = sh_mobile_ceu_set_bus_param(icd);
1983 /* Thaw the queue */
1985 spin_lock_irq(&pcdev->lock);
1986 sh_mobile_ceu_capture(pcdev);
1987 spin_unlock_irq(&pcdev->lock);
1988 /* Start the client */
1989 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1993 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1995 struct soc_camera_device *icd = file->private_data;
1997 return vb2_poll(&icd->vb2_vidq, file, pt);
2000 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
2001 struct v4l2_capability *cap)
2003 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
2004 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2008 static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
2009 struct soc_camera_device *icd)
2011 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2012 q->io_modes = VB2_MMAP | VB2_USERPTR;
2014 q->ops = &sh_mobile_ceu_videobuf_ops;
2015 q->mem_ops = &vb2_dma_contig_memops;
2016 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
2018 return vb2_queue_init(q);
2021 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
2022 .owner = THIS_MODULE,
2023 .add = sh_mobile_ceu_add_device,
2024 .remove = sh_mobile_ceu_remove_device,
2025 .get_formats = sh_mobile_ceu_get_formats,
2026 .put_formats = sh_mobile_ceu_put_formats,
2027 .get_crop = sh_mobile_ceu_get_crop,
2028 .set_crop = sh_mobile_ceu_set_crop,
2029 .set_livecrop = sh_mobile_ceu_set_livecrop,
2030 .set_fmt = sh_mobile_ceu_set_fmt,
2031 .try_fmt = sh_mobile_ceu_try_fmt,
2032 .poll = sh_mobile_ceu_poll,
2033 .querycap = sh_mobile_ceu_querycap,
2034 .set_bus_param = sh_mobile_ceu_set_bus_param,
2035 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
2039 struct notifier_block notifier;
2040 struct completion completion;
2044 static int bus_notify(struct notifier_block *nb,
2045 unsigned long action, void *data)
2047 struct device *dev = data;
2048 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
2050 if (wait->dev != dev)
2054 case BUS_NOTIFY_UNBOUND_DRIVER:
2055 /* Protect from module unloading */
2056 wait_for_completion(&wait->completion);
2062 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
2064 struct sh_mobile_ceu_dev *pcdev;
2065 struct resource *res;
2069 struct bus_wait wait = {
2070 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
2071 .notifier.notifier_call = bus_notify,
2073 struct sh_mobile_ceu_companion *csi2;
2075 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2076 irq = platform_get_irq(pdev, 0);
2077 if (!res || (int)irq <= 0) {
2078 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
2083 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
2085 dev_err(&pdev->dev, "Could not allocate pcdev\n");
2090 INIT_LIST_HEAD(&pcdev->capture);
2091 spin_lock_init(&pcdev->lock);
2092 init_completion(&pcdev->complete);
2094 pcdev->pdata = pdev->dev.platform_data;
2095 if (!pcdev->pdata) {
2097 dev_err(&pdev->dev, "CEU platform data not set.\n");
2101 pcdev->max_width = pcdev->pdata->max_width ? : 2560;
2102 pcdev->max_height = pcdev->pdata->max_height ? : 1920;
2104 base = ioremap_nocache(res->start, resource_size(res));
2107 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
2113 pcdev->video_limit = 0; /* only enabled if second resource exists */
2115 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2117 err = dma_declare_coherent_memory(&pdev->dev, res->start,
2121 DMA_MEMORY_EXCLUSIVE);
2123 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2128 pcdev->video_limit = resource_size(res);
2132 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
2133 dev_name(&pdev->dev), pcdev);
2135 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2136 goto exit_release_mem;
2139 pm_suspend_ignore_children(&pdev->dev, true);
2140 pm_runtime_enable(&pdev->dev);
2141 pm_runtime_resume(&pdev->dev);
2143 pcdev->ici.priv = pcdev;
2144 pcdev->ici.v4l2_dev.dev = &pdev->dev;
2145 pcdev->ici.nr = pdev->id;
2146 pcdev->ici.drv_name = dev_name(&pdev->dev);
2147 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
2149 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2150 if (IS_ERR(pcdev->alloc_ctx)) {
2151 err = PTR_ERR(pcdev->alloc_ctx);
2155 err = soc_camera_host_register(&pcdev->ici);
2159 /* CSI2 interfacing */
2160 csi2 = pcdev->pdata->csi2;
2162 struct platform_device *csi2_pdev =
2163 platform_device_alloc("sh-mobile-csi2", csi2->id);
2164 struct sh_csi2_pdata *csi2_pdata = csi2->platform_data;
2168 goto exit_host_unregister;
2171 pcdev->csi2_pdev = csi2_pdev;
2173 err = platform_device_add_data(csi2_pdev, csi2_pdata, sizeof(*csi2_pdata));
2177 csi2_pdata = csi2_pdev->dev.platform_data;
2178 csi2_pdata->v4l2_dev = &pcdev->ici.v4l2_dev;
2180 csi2_pdev->resource = csi2->resource;
2181 csi2_pdev->num_resources = csi2->num_resources;
2183 err = platform_device_add(csi2_pdev);
2187 wait.dev = &csi2_pdev->dev;
2189 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2191 goto exit_pdev_unregister;
2194 * From this point the driver module will not unload, until
2195 * we complete the completion.
2198 if (!csi2_pdev->dev.driver) {
2199 complete(&wait.completion);
2200 /* Either too late, or probing failed */
2201 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2203 goto exit_pdev_unregister;
2207 * The module is still loaded, in the worst case it is hanging
2208 * in device release on our completion. So, _now_ dereferencing
2209 * the "owner" is safe!
2212 err = try_module_get(csi2_pdev->dev.driver->owner);
2214 /* Let notifier complete, if it has been locked */
2215 complete(&wait.completion);
2216 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2219 goto exit_pdev_unregister;
2225 exit_pdev_unregister:
2226 platform_device_del(pcdev->csi2_pdev);
2228 pcdev->csi2_pdev->resource = NULL;
2229 platform_device_put(pcdev->csi2_pdev);
2230 exit_host_unregister:
2231 soc_camera_host_unregister(&pcdev->ici);
2233 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2235 pm_runtime_disable(&pdev->dev);
2236 free_irq(pcdev->irq, pcdev);
2238 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2239 dma_release_declared_memory(&pdev->dev);
2248 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
2250 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2251 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2252 struct sh_mobile_ceu_dev, ici);
2253 struct platform_device *csi2_pdev = pcdev->csi2_pdev;
2255 soc_camera_host_unregister(soc_host);
2256 pm_runtime_disable(&pdev->dev);
2257 free_irq(pcdev->irq, pcdev);
2258 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2259 dma_release_declared_memory(&pdev->dev);
2260 iounmap(pcdev->base);
2261 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2262 if (csi2_pdev && csi2_pdev->dev.driver) {
2263 struct module *csi2_drv = csi2_pdev->dev.driver->owner;
2264 platform_device_del(csi2_pdev);
2265 csi2_pdev->resource = NULL;
2266 platform_device_put(csi2_pdev);
2267 module_put(csi2_drv);
2274 static int sh_mobile_ceu_runtime_nop(struct device *dev)
2276 /* Runtime PM callback shared between ->runtime_suspend()
2277 * and ->runtime_resume(). Simply returns success.
2279 * This driver re-initializes all registers after
2280 * pm_runtime_get_sync() anyway so there is no need
2281 * to save and restore registers here.
2286 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
2287 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2288 .runtime_resume = sh_mobile_ceu_runtime_nop,
2291 static struct platform_driver sh_mobile_ceu_driver = {
2293 .name = "sh_mobile_ceu",
2294 .pm = &sh_mobile_ceu_dev_pm_ops,
2296 .probe = sh_mobile_ceu_probe,
2297 .remove = __devexit_p(sh_mobile_ceu_remove),
2300 static int __init sh_mobile_ceu_init(void)
2302 /* Whatever return code */
2303 request_module("sh_mobile_csi2");
2304 return platform_driver_register(&sh_mobile_ceu_driver);
2307 static void __exit sh_mobile_ceu_exit(void)
2309 platform_driver_unregister(&sh_mobile_ceu_driver);
2312 module_init(sh_mobile_ceu_init);
2313 module_exit(sh_mobile_ceu_exit);
2315 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2316 MODULE_AUTHOR("Magnus Damm");
2317 MODULE_LICENSE("GPL");
2318 MODULE_VERSION("0.0.6");
2319 MODULE_ALIAS("platform:sh_mobile_ceu");