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/version.h>
31 #include <linux/slab.h>
32 #include <linux/device.h>
33 #include <linux/platform_device.h>
34 #include <linux/videodev2.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/sched.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-dev.h>
40 #include <media/soc_camera.h>
41 #include <media/sh_mobile_ceu.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;
93 enum v4l2_mbus_pixelcode code;
96 struct sh_mobile_ceu_dev {
97 struct soc_camera_host ici;
98 struct soc_camera_device *icd;
102 unsigned long video_limit;
104 spinlock_t lock; /* Protects video buffer lists */
105 struct list_head capture;
106 struct vb2_buffer *active;
107 struct vb2_alloc_ctx *alloc_ctx;
109 struct sh_mobile_ceu_info *pdata;
110 struct completion complete;
114 enum v4l2_field field;
117 unsigned int image_mode:1;
118 unsigned int is_16bit:1;
119 unsigned int frozen:1;
122 struct sh_mobile_ceu_cam {
123 /* CEU offsets within scaled by the CEU camera output */
124 unsigned int ceu_left;
125 unsigned int ceu_top;
126 /* Client output, as seen by the CEU */
130 * User window from S_CROP / G_CROP, produced by client cropping and
131 * scaling, CEU scaling and CEU cropping, mapped back onto the client
134 struct v4l2_rect subrect;
135 /* Camera cropping rectangle */
136 struct v4l2_rect rect;
137 const struct soc_mbus_pixelfmt *extra_fmt;
138 enum v4l2_mbus_pixelcode code;
141 static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
143 return container_of(vb, struct sh_mobile_ceu_buffer, vb);
146 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
150 flags = SOCAM_MASTER |
151 SOCAM_PCLK_SAMPLE_RISING |
152 SOCAM_HSYNC_ACTIVE_HIGH |
153 SOCAM_HSYNC_ACTIVE_LOW |
154 SOCAM_VSYNC_ACTIVE_HIGH |
155 SOCAM_VSYNC_ACTIVE_LOW |
156 SOCAM_DATA_ACTIVE_HIGH;
158 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
159 flags |= SOCAM_DATAWIDTH_8;
161 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
162 flags |= SOCAM_DATAWIDTH_16;
164 if (flags & SOCAM_DATAWIDTH_MASK)
170 static void ceu_write(struct sh_mobile_ceu_dev *priv,
171 unsigned long reg_offs, u32 data)
173 iowrite32(data, priv->base + reg_offs);
176 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
178 return ioread32(priv->base + reg_offs);
181 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
184 struct soc_camera_device *icd = pcdev->icd;
186 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
188 /* wait CSTSR.CPTON bit */
189 for (i = 0; i < 1000; i++) {
190 if (!(ceu_read(pcdev, CSTSR) & 1)) {
197 /* wait CAPSR.CPKIL bit */
198 for (i = 0; i < 1000; i++) {
199 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
208 dev_warn(&icd->dev, "soft reset time out\n");
216 * Videobuf operations
218 static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
219 unsigned int *count, unsigned int *num_planes,
220 unsigned long sizes[], void *alloc_ctxs[])
222 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
223 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
224 struct sh_mobile_ceu_dev *pcdev = ici->priv;
225 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
226 icd->current_fmt->host_fmt);
228 if (bytes_per_line < 0)
229 return bytes_per_line;
234 sizes[0] = bytes_per_line * icd->user_height;
235 alloc_ctxs[0] = pcdev->alloc_ctx;
240 if (pcdev->video_limit) {
241 if (PAGE_ALIGN(sizes[0]) * *count > pcdev->video_limit)
242 *count = pcdev->video_limit / PAGE_ALIGN(sizes[0]);
245 dev_dbg(icd->dev.parent, "count=%d, size=%lu\n", *count, sizes[0]);
250 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
251 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
252 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
253 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
254 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
255 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
259 * return value doesn't reflex the success/failure to queue the new buffer,
260 * but rather the status of the previous buffer.
262 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
264 struct soc_camera_device *icd = pcdev->icd;
265 dma_addr_t phys_addr_top, phys_addr_bottom;
266 unsigned long top1, top2;
267 unsigned long bottom1, bottom2;
272 * The hardware is _very_ picky about this sequence. Especially
273 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
274 * several not-so-well documented interrupt sources in CETCR.
276 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
277 status = ceu_read(pcdev, CETCR);
278 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
280 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
281 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
282 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
285 * When a VBP interrupt occurs, a capture end interrupt does not occur
286 * and the image of that frame is not captured correctly. So, soft reset
289 if (status & CEU_CEIER_VBP) {
290 sh_mobile_ceu_soft_reset(pcdev);
295 complete(&pcdev->complete);
302 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
314 phys_addr_top = vb2_dma_contig_plane_paddr(pcdev->active, 0);
316 ceu_write(pcdev, top1, phys_addr_top);
317 if (V4L2_FIELD_NONE != pcdev->field) {
318 phys_addr_bottom = phys_addr_top + icd->user_width;
319 ceu_write(pcdev, bottom1, phys_addr_bottom);
322 switch (icd->current_fmt->host_fmt->fourcc) {
323 case V4L2_PIX_FMT_NV12:
324 case V4L2_PIX_FMT_NV21:
325 case V4L2_PIX_FMT_NV16:
326 case V4L2_PIX_FMT_NV61:
327 phys_addr_top += icd->user_width *
329 ceu_write(pcdev, top2, phys_addr_top);
330 if (V4L2_FIELD_NONE != pcdev->field) {
331 phys_addr_bottom = phys_addr_top + icd->user_width;
332 ceu_write(pcdev, bottom2, phys_addr_bottom);
336 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
341 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
343 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
344 struct sh_mobile_ceu_buffer *buf;
345 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
346 icd->current_fmt->host_fmt);
349 if (bytes_per_line < 0)
350 return bytes_per_line;
354 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
355 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
357 /* Added list head initialization on alloc */
358 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
362 * This can be useful if you want to see if we actually fill
363 * the buffer with something
365 if (vb2_plane_vaddr(vb, 0))
366 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
369 BUG_ON(NULL == icd->current_fmt);
371 size = icd->user_height * bytes_per_line;
373 if (vb2_plane_size(vb, 0) < size) {
374 dev_err(icd->dev.parent, "Buffer too small (%lu < %lu)\n",
375 vb2_plane_size(vb, 0), size);
379 vb2_set_plane_payload(vb, 0, size);
384 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
386 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
387 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
388 struct sh_mobile_ceu_dev *pcdev = ici->priv;
389 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
391 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
392 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
394 spin_lock_irq(&pcdev->lock);
395 list_add_tail(&buf->queue, &pcdev->capture);
397 if (!pcdev->active) {
399 * Because there were no active buffer at this moment,
400 * we are not interested in the return value of
401 * sh_mobile_ceu_capture here.
404 sh_mobile_ceu_capture(pcdev);
406 spin_unlock_irq(&pcdev->lock);
409 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
411 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
413 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
414 struct sh_mobile_ceu_dev *pcdev = ici->priv;
416 spin_lock_irq(&pcdev->lock);
418 if (pcdev->active == vb) {
419 /* disable capture (release DMA buffer), reset */
420 ceu_write(pcdev, CAPSR, 1 << 16);
421 pcdev->active = NULL;
424 /* Doesn't hurt also if the list is empty */
425 list_del_init(&buf->queue);
427 spin_unlock_irq(&pcdev->lock);
430 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
432 /* This is for locking debugging only */
433 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
437 static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
439 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
440 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
441 struct sh_mobile_ceu_dev *pcdev = ici->priv;
442 struct list_head *buf_head, *tmp;
444 spin_lock_irq(&pcdev->lock);
446 pcdev->active = NULL;
448 list_for_each_safe(buf_head, tmp, &pcdev->capture)
449 list_del_init(buf_head);
451 spin_unlock_irq(&pcdev->lock);
453 return sh_mobile_ceu_soft_reset(pcdev);
456 static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
457 .queue_setup = sh_mobile_ceu_videobuf_setup,
458 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
459 .buf_queue = sh_mobile_ceu_videobuf_queue,
460 .buf_cleanup = sh_mobile_ceu_videobuf_release,
461 .buf_init = sh_mobile_ceu_videobuf_init,
462 .wait_prepare = soc_camera_unlock,
463 .wait_finish = soc_camera_lock,
464 .stop_streaming = sh_mobile_ceu_stop_streaming,
467 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
469 struct sh_mobile_ceu_dev *pcdev = data;
470 struct vb2_buffer *vb;
473 spin_lock(&pcdev->lock);
477 /* Stale interrupt from a released buffer */
480 list_del_init(&to_ceu_vb(vb)->queue);
482 if (!list_empty(&pcdev->capture))
483 pcdev->active = &list_entry(pcdev->capture.next,
484 struct sh_mobile_ceu_buffer, queue)->vb;
486 pcdev->active = NULL;
488 ret = sh_mobile_ceu_capture(pcdev);
489 do_gettimeofday(&vb->v4l2_buf.timestamp);
491 vb->v4l2_buf.field = pcdev->field;
492 vb->v4l2_buf.sequence = pcdev->sequence++;
494 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
497 spin_unlock(&pcdev->lock);
502 /* Called with .video_lock held */
503 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
505 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
506 struct sh_mobile_ceu_dev *pcdev = ici->priv;
512 dev_info(icd->dev.parent,
513 "SuperH Mobile CEU driver attached to camera %d\n",
516 pm_runtime_get_sync(ici->v4l2_dev.dev);
518 ret = sh_mobile_ceu_soft_reset(pcdev);
525 /* Called with .video_lock held */
526 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
528 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
529 struct sh_mobile_ceu_dev *pcdev = ici->priv;
531 BUG_ON(icd != pcdev->icd);
533 /* disable capture, disable interrupts */
534 ceu_write(pcdev, CEIER, 0);
535 sh_mobile_ceu_soft_reset(pcdev);
537 /* make sure active buffer is canceled */
538 spin_lock_irq(&pcdev->lock);
540 list_del_init(&to_ceu_vb(pcdev->active)->queue);
541 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
542 pcdev->active = NULL;
544 spin_unlock_irq(&pcdev->lock);
546 pm_runtime_put_sync(ici->v4l2_dev.dev);
548 dev_info(icd->dev.parent,
549 "SuperH Mobile CEU driver detached from camera %d\n",
556 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
557 * in SH7722 Hardware Manual
559 static unsigned int size_dst(unsigned int src, unsigned int scale)
561 unsigned int mant_pre = scale >> 12;
564 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
565 mant_pre * 4096 / scale + 1;
568 static u16 calc_scale(unsigned int src, unsigned int *dst)
575 scale = (src * 4096 / *dst) & ~7;
577 while (scale > 4096 && size_dst(src, scale) < *dst)
580 *dst = size_dst(src, scale);
585 /* rect is guaranteed to not exceed the scaled camera rectangle */
586 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
588 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
589 struct sh_mobile_ceu_cam *cam = icd->host_priv;
590 struct sh_mobile_ceu_dev *pcdev = ici->priv;
591 unsigned int height, width, cdwdr_width, in_width, in_height;
592 unsigned int left_offset, top_offset;
595 dev_geo(icd->dev.parent, "Crop %ux%u@%u:%u\n",
596 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
598 left_offset = cam->ceu_left;
599 top_offset = cam->ceu_top;
601 /* CEU cropping (CFSZR) is applied _after_ the scaling filter (CFLCR) */
602 if (pcdev->image_mode) {
603 in_width = cam->width;
604 if (!pcdev->is_16bit) {
608 width = icd->user_width;
609 cdwdr_width = icd->user_width;
611 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
612 icd->current_fmt->host_fmt);
613 unsigned int w_factor;
615 width = icd->user_width;
617 switch (icd->current_fmt->host_fmt->packing) {
618 case SOC_MBUS_PACKING_2X8_PADHI:
625 in_width = cam->width * w_factor;
626 left_offset = left_offset * w_factor;
628 if (bytes_per_line < 0)
629 cdwdr_width = icd->user_width;
631 cdwdr_width = bytes_per_line;
634 height = icd->user_height;
635 in_height = cam->height;
636 if (V4L2_FIELD_NONE != pcdev->field) {
643 /* CSI2 special configuration */
644 if (pcdev->pdata->csi2_dev) {
645 in_width = ((in_width - 2) * 2);
649 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
650 camor = left_offset | (top_offset << 16);
652 dev_geo(icd->dev.parent,
653 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
654 (in_height << 16) | in_width, (height << 16) | width,
657 ceu_write(pcdev, CAMOR, camor);
658 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
659 ceu_write(pcdev, CFSZR, (height << 16) | width);
660 ceu_write(pcdev, CDWDR, cdwdr_width);
663 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
665 u32 capsr = ceu_read(pcdev, CAPSR);
666 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
670 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
672 unsigned long timeout = jiffies + 10 * HZ;
675 * Wait until the end of the current frame. It can take a long time,
676 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
678 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
681 if (time_after(jiffies, timeout)) {
682 dev_err(pcdev->ici.v4l2_dev.dev,
683 "Timeout waiting for frame end! Interface problem?\n");
687 /* Wait until reset clears, this shall not hang... */
688 while (ceu_read(pcdev, CAPSR) & (1 << 16))
691 /* Anything to restore? */
692 if (capsr & ~(1 << 16))
693 ceu_write(pcdev, CAPSR, capsr);
696 /* Capture is not running, no interrupts, no locking needed */
697 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct sh_mobile_ceu_dev *pcdev = ici->priv;
703 unsigned long camera_flags, common_flags, value;
705 struct sh_mobile_ceu_cam *cam = icd->host_priv;
706 u32 capsr = capture_save_reset(pcdev);
708 camera_flags = icd->ops->query_bus_param(icd);
709 common_flags = soc_camera_bus_param_compatible(camera_flags,
710 make_bus_param(pcdev));
714 /* Make choises, based on platform preferences */
715 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
716 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
717 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
718 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
720 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
723 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
724 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
725 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
726 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
728 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
731 ret = icd->ops->set_bus_param(icd, common_flags);
735 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
736 case SOCAM_DATAWIDTH_8:
739 case SOCAM_DATAWIDTH_16:
746 ceu_write(pcdev, CRCNTR, 0);
747 ceu_write(pcdev, CRCMPR, 0);
749 value = 0x00000010; /* data fetch by default */
752 switch (icd->current_fmt->host_fmt->fourcc) {
753 case V4L2_PIX_FMT_NV12:
754 case V4L2_PIX_FMT_NV21:
755 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
757 case V4L2_PIX_FMT_NV16:
758 case V4L2_PIX_FMT_NV61:
760 case V4L2_MBUS_FMT_UYVY8_2X8:
761 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
763 case V4L2_MBUS_FMT_VYUY8_2X8:
764 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
766 case V4L2_MBUS_FMT_YUYV8_2X8:
767 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
769 case V4L2_MBUS_FMT_YVYU8_2X8:
770 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
777 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
778 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
779 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
781 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
782 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
783 value |= pcdev->is_16bit ? 1 << 12 : 0;
786 if (pcdev->pdata->csi2_dev)
789 ceu_write(pcdev, CAMCR, value);
791 ceu_write(pcdev, CAPCR, 0x00300000);
793 switch (pcdev->field) {
794 case V4L2_FIELD_INTERLACED_TB:
797 case V4L2_FIELD_INTERLACED_BT:
804 ceu_write(pcdev, CAIFR, value);
806 sh_mobile_ceu_set_rect(icd);
809 dev_geo(icd->dev.parent, "CFLCR 0x%x\n", pcdev->cflcr);
810 ceu_write(pcdev, CFLCR, pcdev->cflcr);
813 * A few words about byte order (observed in Big Endian mode)
815 * In data fetch mode bytes are received in chunks of 8 bytes.
816 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
818 * The data is however by default written to memory in reverse order:
819 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
821 * The lowest three bits of CDOCR allows us to do swapping,
822 * using 7 we swap the data bytes to match the incoming order:
823 * D0, D1, D2, D3, D4, D5, D6, D7
827 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
829 ceu_write(pcdev, CDOCR, value);
830 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
832 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
833 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
834 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
835 icd->user_width, icd->user_height);
837 capture_restore(pcdev, capsr);
839 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
843 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
844 unsigned char buswidth)
846 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
847 struct sh_mobile_ceu_dev *pcdev = ici->priv;
848 unsigned long camera_flags, common_flags;
850 camera_flags = icd->ops->query_bus_param(icd);
851 common_flags = soc_camera_bus_param_compatible(camera_flags,
852 make_bus_param(pcdev));
853 if (!common_flags || buswidth > 16 ||
854 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
860 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
862 .fourcc = V4L2_PIX_FMT_NV12,
864 .bits_per_sample = 12,
865 .packing = SOC_MBUS_PACKING_NONE,
866 .order = SOC_MBUS_ORDER_LE,
868 .fourcc = V4L2_PIX_FMT_NV21,
870 .bits_per_sample = 12,
871 .packing = SOC_MBUS_PACKING_NONE,
872 .order = SOC_MBUS_ORDER_LE,
874 .fourcc = V4L2_PIX_FMT_NV16,
876 .bits_per_sample = 16,
877 .packing = SOC_MBUS_PACKING_NONE,
878 .order = SOC_MBUS_ORDER_LE,
880 .fourcc = V4L2_PIX_FMT_NV61,
882 .bits_per_sample = 16,
883 .packing = SOC_MBUS_PACKING_NONE,
884 .order = SOC_MBUS_ORDER_LE,
888 /* This will be corrected as we get more formats */
889 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
891 return fmt->packing == SOC_MBUS_PACKING_NONE ||
892 (fmt->bits_per_sample == 8 &&
893 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
894 (fmt->bits_per_sample > 8 &&
895 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
898 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
900 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
901 struct soc_camera_format_xlate *xlate)
903 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904 struct device *dev = icd->dev.parent;
905 struct soc_camera_host *ici = to_soc_camera_host(dev);
906 struct sh_mobile_ceu_dev *pcdev = ici->priv;
909 struct sh_mobile_ceu_cam *cam;
910 enum v4l2_mbus_pixelcode code;
911 const struct soc_mbus_pixelfmt *fmt;
913 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
915 /* No more formats */
918 fmt = soc_mbus_get_fmtdesc(code);
920 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
924 if (!pcdev->pdata->csi2_dev) {
925 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
930 if (!icd->host_priv) {
931 struct v4l2_mbus_framefmt mf;
932 struct v4l2_rect rect;
935 /* FIXME: subwindow is lost between close / open */
937 /* Cache current client geometry */
938 ret = client_g_rect(sd, &rect);
943 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
947 while ((mf.width > 2560 || mf.height > 1920) && shift < 4) {
948 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
949 mf.width = 2560 >> shift;
950 mf.height = 1920 >> shift;
951 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
959 dev_err(dev, "Failed to configure the client below %ux%x\n",
960 mf.width, mf.height);
964 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
966 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
970 /* We are called with current camera crop, initialise subrect with it */
974 cam->width = mf.width;
975 cam->height = mf.height;
977 cam->width = mf.width;
978 cam->height = mf.height;
980 icd->host_priv = cam;
982 cam = icd->host_priv;
985 /* Beginning of a pass */
987 cam->extra_fmt = NULL;
990 case V4L2_MBUS_FMT_UYVY8_2X8:
991 case V4L2_MBUS_FMT_VYUY8_2X8:
992 case V4L2_MBUS_FMT_YUYV8_2X8:
993 case V4L2_MBUS_FMT_YVYU8_2X8:
998 * Our case is simple so far: for any of the above four camera
999 * formats we add all our four synthesized NV* formats, so,
1000 * just marking the device with a single flag suffices. If
1001 * the format generation rules are more complex, you would have
1002 * to actually hang your already added / counted formats onto
1003 * the host_priv pointer and check whether the format you're
1004 * going to add now is already there.
1006 cam->extra_fmt = sh_mobile_ceu_formats;
1008 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1010 for (k = 0; xlate && k < n; k++) {
1011 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1014 dev_dbg(dev, "Providing format %s using code %d\n",
1015 sh_mobile_ceu_formats[k].name, code);
1019 if (!sh_mobile_ceu_packing_supported(fmt))
1023 /* Generic pass-through */
1026 xlate->host_fmt = fmt;
1029 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1036 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1038 kfree(icd->host_priv);
1039 icd->host_priv = NULL;
1042 /* Check if any dimension of r1 is smaller than respective one of r2 */
1043 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1045 return r1->width < r2->width || r1->height < r2->height;
1048 /* Check if r1 fails to cover r2 */
1049 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1051 return r1->left > r2->left || r1->top > r2->top ||
1052 r1->left + r1->width < r2->left + r2->width ||
1053 r1->top + r1->height < r2->top + r2->height;
1056 static unsigned int scale_down(unsigned int size, unsigned int scale)
1058 return (size * 4096 + scale / 2) / scale;
1061 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1063 return (input * 4096 + output / 2) / output;
1066 /* Get and store current client crop */
1067 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1069 struct v4l2_crop crop;
1070 struct v4l2_cropcap cap;
1073 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1075 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1081 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1082 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1084 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1086 *rect = cap.defrect;
1091 /* Client crop has changed, update our sub-rectangle to remain within the area */
1092 static void update_subrect(struct sh_mobile_ceu_cam *cam)
1094 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1096 if (rect->width < subrect->width)
1097 subrect->width = rect->width;
1099 if (rect->height < subrect->height)
1100 subrect->height = rect->height;
1102 if (rect->left > subrect->left)
1103 subrect->left = rect->left;
1104 else if (rect->left + rect->width >
1105 subrect->left + subrect->width)
1106 subrect->left = rect->left + rect->width -
1109 if (rect->top > subrect->top)
1110 subrect->top = rect->top;
1111 else if (rect->top + rect->height >
1112 subrect->top + subrect->height)
1113 subrect->top = rect->top + rect->height -
1118 * The common for both scaling and cropping iterative approach is:
1119 * 1. try if the client can produce exactly what requested by the user
1120 * 2. if (1) failed, try to double the client image until we get one big enough
1121 * 3. if (2) failed, try to request the maximum image
1123 static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
1124 struct v4l2_crop *cam_crop)
1126 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1127 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1128 struct device *dev = sd->v4l2_dev->dev;
1129 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1130 struct v4l2_cropcap cap;
1132 unsigned int width, height;
1134 v4l2_subdev_call(sd, video, s_crop, crop);
1135 ret = client_g_rect(sd, cam_rect);
1140 * Now cam_crop contains the current camera input rectangle, and it must
1141 * be within camera cropcap bounds
1143 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1144 /* Even if camera S_CROP failed, but camera rectangle matches */
1145 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
1146 rect->width, rect->height, rect->left, rect->top);
1147 cam->rect = *cam_rect;
1151 /* Try to fix cropping, that camera hasn't managed to set */
1152 dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
1153 cam_rect->width, cam_rect->height,
1154 cam_rect->left, cam_rect->top,
1155 rect->width, rect->height, rect->left, rect->top);
1157 /* We need sensor maximum rectangle */
1158 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1162 /* Put user requested rectangle within sensor bounds */
1163 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1165 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1169 * Popular special case - some cameras can only handle fixed sizes like
1170 * QVGA, VGA,... Take care to avoid infinite loop.
1172 width = max(cam_rect->width, 2);
1173 height = max(cam_rect->height, 2);
1176 * Loop as long as sensor is not covering the requested rectangle and
1177 * is still within its bounds
1179 while (!ret && (is_smaller(cam_rect, rect) ||
1180 is_inside(cam_rect, rect)) &&
1181 (cap.bounds.width > width || cap.bounds.height > height)) {
1186 cam_rect->width = width;
1187 cam_rect->height = height;
1190 * We do not know what capabilities the camera has to set up
1191 * left and top borders. We could try to be smarter in iterating
1192 * them, e.g., if camera current left is to the right of the
1193 * target left, set it to the middle point between the current
1194 * left and minimum left. But that would add too much
1195 * complexity: we would have to iterate each border separately.
1196 * Instead we just drop to the left and top bounds.
1198 if (cam_rect->left > rect->left)
1199 cam_rect->left = cap.bounds.left;
1201 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1202 cam_rect->width = rect->left + rect->width -
1205 if (cam_rect->top > rect->top)
1206 cam_rect->top = cap.bounds.top;
1208 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1209 cam_rect->height = rect->top + rect->height -
1212 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1213 ret = client_g_rect(sd, cam_rect);
1214 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
1215 cam_rect->width, cam_rect->height,
1216 cam_rect->left, cam_rect->top);
1219 /* S_CROP must not modify the rectangle */
1220 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1222 * The camera failed to configure a suitable cropping,
1223 * we cannot use the current rectangle, set to max
1225 *cam_rect = cap.bounds;
1226 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1227 ret = client_g_rect(sd, cam_rect);
1228 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
1229 cam_rect->width, cam_rect->height,
1230 cam_rect->left, cam_rect->top);
1234 cam->rect = *cam_rect;
1235 update_subrect(cam);
1241 /* Iterative s_mbus_fmt, also updates cached client crop on success */
1242 static int client_s_fmt(struct soc_camera_device *icd,
1243 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
1245 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1246 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1247 struct device *dev = icd->dev.parent;
1248 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
1249 unsigned int max_width, max_height;
1250 struct v4l2_cropcap cap;
1253 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1258 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1260 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
1263 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1265 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1269 max_width = min(cap.bounds.width, 2560);
1270 max_height = min(cap.bounds.height, 1920);
1272 /* Camera set a format, but geometry is not precise, try to improve */
1276 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1277 while ((width > tmp_w || height > tmp_h) &&
1278 tmp_w < max_width && tmp_h < max_height) {
1279 tmp_w = min(2 * tmp_w, max_width);
1280 tmp_h = min(2 * tmp_h, max_height);
1283 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1285 dev_geo(dev, "Camera scaled to %ux%u\n",
1286 mf->width, mf->height);
1288 /* This shouldn't happen */
1289 dev_err(dev, "Client failed to set format: %d\n", ret);
1296 ret = client_g_rect(sd, &cam->rect);
1300 update_subrect(cam);
1306 * @width - on output: user width, mapped back to input
1307 * @height - on output: user height, mapped back to input
1308 * @mf - in- / output camera output window
1310 static int client_scale(struct soc_camera_device *icd,
1311 struct v4l2_mbus_framefmt *mf,
1312 unsigned int *width, unsigned int *height,
1315 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1316 struct device *dev = icd->dev.parent;
1317 struct v4l2_mbus_framefmt mf_tmp = *mf;
1318 unsigned int scale_h, scale_v;
1322 * 5. Apply iterative camera S_FMT for camera user window (also updates
1323 * client crop cache and the imaginary sub-rectangle).
1325 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
1329 dev_geo(dev, "5: camera scaled to %ux%u\n",
1330 mf_tmp.width, mf_tmp.height);
1332 /* 6. Retrieve camera output window (g_fmt) */
1334 /* unneeded - it is already in "mf_tmp" */
1336 /* 7. Calculate new client scales. */
1337 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1338 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
1340 mf->width = mf_tmp.width;
1341 mf->height = mf_tmp.height;
1342 mf->colorspace = mf_tmp.colorspace;
1345 * 8. Calculate new CEU crop - apply camera scales to previously
1346 * updated "effective" crop.
1348 *width = scale_down(cam->subrect.width, scale_h);
1349 *height = scale_down(cam->subrect.height, scale_v);
1351 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
1357 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1358 * framerate by always requesting the maximum image from the client. See
1359 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1360 * scaling and cropping algorithms and for the meaning of referenced here steps.
1362 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1363 struct v4l2_crop *a)
1365 struct v4l2_rect *rect = &a->c;
1366 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1367 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1368 struct v4l2_crop cam_crop;
1369 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1370 struct v4l2_rect *cam_rect = &cam_crop.c;
1371 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1372 struct device *dev = icd->dev.parent;
1373 struct v4l2_mbus_framefmt mf;
1374 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1375 out_width, out_height;
1376 int interm_width, interm_height;
1380 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1381 rect->left, rect->top);
1383 /* During camera cropping its output window can change too, stop CEU */
1384 capsr = capture_save_reset(pcdev);
1385 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1387 /* 1. - 2. Apply iterative camera S_CROP for new input window. */
1388 ret = client_s_crop(icd, a, &cam_crop);
1392 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
1393 cam_rect->width, cam_rect->height,
1394 cam_rect->left, cam_rect->top);
1396 /* On success cam_crop contains current camera crop */
1398 /* 3. Retrieve camera output window */
1399 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1403 if (mf.width > 2560 || mf.height > 1920)
1406 /* 4. Calculate camera scales */
1407 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1408 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1410 /* Calculate intermediate window */
1411 interm_width = scale_down(rect->width, scale_cam_h);
1412 interm_height = scale_down(rect->height, scale_cam_v);
1414 if (interm_width < icd->user_width) {
1417 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1419 mf.width = scale_down(cam_rect->width, new_scale_h);
1422 if (interm_height < icd->user_height) {
1425 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1427 mf.height = scale_down(cam_rect->height, new_scale_v);
1430 if (interm_width < icd->user_width || interm_height < icd->user_height) {
1431 ret = v4l2_device_call_until_err(sd->v4l2_dev, (int)icd, video,
1436 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1437 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1438 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1439 interm_width = scale_down(rect->width, scale_cam_h);
1440 interm_height = scale_down(rect->height, scale_cam_v);
1443 /* Cache camera output window */
1444 cam->width = mf.width;
1445 cam->height = mf.height;
1447 if (pcdev->image_mode) {
1448 out_width = min(interm_width, icd->user_width);
1449 out_height = min(interm_height, icd->user_height);
1451 out_width = interm_width;
1452 out_height = interm_height;
1456 * 5. Calculate CEU scales from camera scales from results of (5) and
1459 scale_ceu_h = calc_scale(interm_width, &out_width);
1460 scale_ceu_v = calc_scale(interm_height, &out_height);
1462 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1464 /* Apply CEU scales. */
1465 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1466 if (cflcr != pcdev->cflcr) {
1467 pcdev->cflcr = cflcr;
1468 ceu_write(pcdev, CFLCR, cflcr);
1471 icd->user_width = out_width;
1472 icd->user_height = out_height;
1473 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1474 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
1476 /* 6. Use CEU cropping to crop to the new window. */
1477 sh_mobile_ceu_set_rect(icd);
1479 cam->subrect = *rect;
1481 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1482 icd->user_width, icd->user_height,
1483 cam->ceu_left, cam->ceu_top);
1485 /* Restore capture. The CE bit can be cleared by the hardware */
1488 capture_restore(pcdev, capsr);
1490 /* Even if only camera cropping succeeded */
1494 static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1495 struct v4l2_crop *a)
1497 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1499 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1500 a->c = cam->subrect;
1506 * Calculate real client output window by applying new scales to the current
1507 * client crop. New scales are calculated from the requested output format and
1508 * CEU crop, mapped backed onto the client input (subrect).
1510 static void calculate_client_output(struct soc_camera_device *icd,
1511 struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
1513 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1514 struct device *dev = icd->dev.parent;
1515 struct v4l2_rect *cam_subrect = &cam->subrect;
1516 unsigned int scale_v, scale_h;
1518 if (cam_subrect->width == cam->rect.width &&
1519 cam_subrect->height == cam->rect.height) {
1520 /* No sub-cropping */
1521 mf->width = pix->width;
1522 mf->height = pix->height;
1526 /* 1.-2. Current camera scales and subwin - cached. */
1528 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1529 cam_subrect->width, cam_subrect->height,
1530 cam_subrect->left, cam_subrect->top);
1533 * 3. Calculate new combined scales from input sub-window to requested
1538 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1539 * (128x96) or larger than VGA
1541 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1542 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1544 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1547 * 4. Calculate client output window by applying combined scales to real
1550 mf->width = scale_down(cam->rect.width, scale_h);
1551 mf->height = scale_down(cam->rect.height, scale_v);
1554 /* Similar to set_crop multistage iterative algorithm */
1555 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1556 struct v4l2_format *f)
1558 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1559 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1560 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1561 struct v4l2_pix_format *pix = &f->fmt.pix;
1562 struct v4l2_mbus_framefmt mf;
1563 struct device *dev = icd->dev.parent;
1564 __u32 pixfmt = pix->pixelformat;
1565 const struct soc_camera_format_xlate *xlate;
1566 /* Keep Compiler Happy */
1567 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
1568 u16 scale_v, scale_h;
1571 enum v4l2_field field;
1573 dev_geo(dev, "S_FMT(pix=0x%x, %ux%u)\n", pixfmt, pix->width, pix->height);
1575 switch (pix->field) {
1577 pix->field = V4L2_FIELD_NONE;
1579 case V4L2_FIELD_INTERLACED_TB:
1580 case V4L2_FIELD_INTERLACED_BT:
1581 case V4L2_FIELD_NONE:
1584 case V4L2_FIELD_INTERLACED:
1585 field = V4L2_FIELD_INTERLACED_TB;
1589 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1591 dev_warn(dev, "Format %x not found\n", pixfmt);
1595 /* 1.-4. Calculate client output geometry */
1596 calculate_client_output(icd, &f->fmt.pix, &mf);
1597 mf.field = pix->field;
1598 mf.colorspace = pix->colorspace;
1599 mf.code = xlate->code;
1602 case V4L2_PIX_FMT_NV12:
1603 case V4L2_PIX_FMT_NV21:
1604 case V4L2_PIX_FMT_NV16:
1605 case V4L2_PIX_FMT_NV61:
1612 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
1615 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
1616 image_mode && V4L2_FIELD_NONE == field);
1618 dev_geo(dev, "5-9: client scale return %d\n", ret);
1620 /* Done with the camera. Now see if we can improve the result */
1622 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1623 mf.width, mf.height, pix->width, pix->height);
1627 if (mf.code != xlate->code)
1630 /* 9. Prepare CEU crop */
1631 cam->width = mf.width;
1632 cam->height = mf.height;
1634 /* 10. Use CEU scaling to scale to the requested user window. */
1636 /* We cannot scale up */
1637 if (pix->width > ceu_sub_width)
1638 ceu_sub_width = pix->width;
1640 if (pix->height > ceu_sub_height)
1641 ceu_sub_height = pix->height;
1643 pix->colorspace = mf.colorspace;
1646 /* Scale pix->{width x height} down to width x height */
1647 scale_h = calc_scale(ceu_sub_width, &pix->width);
1648 scale_v = calc_scale(ceu_sub_height, &pix->height);
1650 pix->width = ceu_sub_width;
1651 pix->height = ceu_sub_height;
1656 pcdev->cflcr = scale_h | (scale_v << 16);
1659 * We have calculated CFLCR, the actual configuration will be performed
1660 * in sh_mobile_ceu_set_bus_param()
1663 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1664 ceu_sub_width, scale_h, pix->width,
1665 ceu_sub_height, scale_v, pix->height);
1667 cam->code = xlate->code;
1668 icd->current_fmt = xlate;
1670 pcdev->field = field;
1671 pcdev->image_mode = image_mode;
1676 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1677 struct v4l2_format *f)
1679 const struct soc_camera_format_xlate *xlate;
1680 struct v4l2_pix_format *pix = &f->fmt.pix;
1681 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1682 struct v4l2_mbus_framefmt mf;
1683 __u32 pixfmt = pix->pixelformat;
1687 dev_geo(icd->dev.parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1688 pixfmt, pix->width, pix->height);
1690 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1692 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
1696 /* FIXME: calculate using depth and bus width */
1698 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1699 &pix->height, 4, 1920, 2, 0);
1702 height = pix->height;
1704 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
1705 if ((int)pix->bytesperline < 0)
1706 return pix->bytesperline;
1707 pix->sizeimage = height * pix->bytesperline;
1709 /* limit to sensor capabilities */
1710 mf.width = pix->width;
1711 mf.height = pix->height;
1712 mf.field = pix->field;
1713 mf.code = xlate->code;
1714 mf.colorspace = pix->colorspace;
1716 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video, try_mbus_fmt, &mf);
1720 pix->width = mf.width;
1721 pix->height = mf.height;
1722 pix->field = mf.field;
1723 pix->colorspace = mf.colorspace;
1726 case V4L2_PIX_FMT_NV12:
1727 case V4L2_PIX_FMT_NV21:
1728 case V4L2_PIX_FMT_NV16:
1729 case V4L2_PIX_FMT_NV61:
1730 /* FIXME: check against rect_max after converting soc-camera */
1731 /* We can scale precisely, need a bigger image from camera */
1732 if (pix->width < width || pix->height < height) {
1734 * We presume, the sensor behaves sanely, i.e., if
1735 * requested a bigger rectangle, it will not return a
1740 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1743 /* Shouldn't actually happen... */
1744 dev_err(icd->dev.parent,
1745 "FIXME: client try_fmt() = %d\n", ret);
1749 /* We will scale exactly */
1750 if (mf.width > width)
1752 if (mf.height > height)
1753 pix->height = height;
1756 dev_geo(icd->dev.parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1757 __func__, ret, pix->pixelformat, pix->width, pix->height);
1762 static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1763 struct v4l2_crop *a)
1765 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1766 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1767 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1768 u32 out_width = icd->user_width, out_height = icd->user_height;
1773 /* Wait for frame */
1774 ret = wait_for_completion_interruptible(&pcdev->complete);
1775 /* Stop the client */
1776 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1778 dev_warn(icd->dev.parent,
1779 "Client failed to stop the stream: %d\n", ret);
1781 /* Do the crop, if it fails, there's nothing more we can do */
1782 sh_mobile_ceu_set_crop(icd, a);
1784 dev_geo(icd->dev.parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1786 if (icd->user_width != out_width || icd->user_height != out_height) {
1787 struct v4l2_format f = {
1788 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1791 .height = out_height,
1792 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1793 .field = pcdev->field,
1794 .colorspace = icd->colorspace,
1797 ret = sh_mobile_ceu_set_fmt(icd, &f);
1798 if (!ret && (out_width != f.fmt.pix.width ||
1799 out_height != f.fmt.pix.height))
1802 icd->user_width = out_width;
1803 icd->user_height = out_height;
1804 ret = sh_mobile_ceu_set_bus_param(icd,
1805 icd->current_fmt->host_fmt->fourcc);
1809 /* Thaw the queue */
1811 spin_lock_irq(&pcdev->lock);
1812 sh_mobile_ceu_capture(pcdev);
1813 spin_unlock_irq(&pcdev->lock);
1814 /* Start the client */
1815 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1819 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1821 struct soc_camera_device *icd = file->private_data;
1823 return vb2_poll(&icd->vb2_vidq, file, pt);
1826 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1827 struct v4l2_capability *cap)
1829 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1830 cap->version = KERNEL_VERSION(0, 0, 5);
1831 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1835 static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1836 struct soc_camera_device *icd)
1838 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1839 q->io_modes = VB2_MMAP | VB2_USERPTR;
1841 q->ops = &sh_mobile_ceu_videobuf_ops;
1842 q->mem_ops = &vb2_dma_contig_memops;
1843 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
1845 return vb2_queue_init(q);
1848 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1849 struct v4l2_control *ctrl)
1851 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1852 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1856 case V4L2_CID_SHARPNESS:
1857 val = ceu_read(pcdev, CLFCR);
1858 ctrl->value = val ^ 1;
1861 return -ENOIOCTLCMD;
1864 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1865 struct v4l2_control *ctrl)
1867 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1868 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1871 case V4L2_CID_SHARPNESS:
1872 switch (icd->current_fmt->host_fmt->fourcc) {
1873 case V4L2_PIX_FMT_NV12:
1874 case V4L2_PIX_FMT_NV21:
1875 case V4L2_PIX_FMT_NV16:
1876 case V4L2_PIX_FMT_NV61:
1877 ceu_write(pcdev, CLFCR, !ctrl->value);
1882 return -ENOIOCTLCMD;
1885 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1887 .id = V4L2_CID_SHARPNESS,
1888 .type = V4L2_CTRL_TYPE_BOOLEAN,
1889 .name = "Low-pass filter",
1897 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1898 .owner = THIS_MODULE,
1899 .add = sh_mobile_ceu_add_device,
1900 .remove = sh_mobile_ceu_remove_device,
1901 .get_formats = sh_mobile_ceu_get_formats,
1902 .put_formats = sh_mobile_ceu_put_formats,
1903 .get_crop = sh_mobile_ceu_get_crop,
1904 .set_crop = sh_mobile_ceu_set_crop,
1905 .set_livecrop = sh_mobile_ceu_set_livecrop,
1906 .set_fmt = sh_mobile_ceu_set_fmt,
1907 .try_fmt = sh_mobile_ceu_try_fmt,
1908 .set_ctrl = sh_mobile_ceu_set_ctrl,
1909 .get_ctrl = sh_mobile_ceu_get_ctrl,
1910 .poll = sh_mobile_ceu_poll,
1911 .querycap = sh_mobile_ceu_querycap,
1912 .set_bus_param = sh_mobile_ceu_set_bus_param,
1913 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
1914 .controls = sh_mobile_ceu_controls,
1915 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
1919 struct notifier_block notifier;
1920 struct completion completion;
1924 static int bus_notify(struct notifier_block *nb,
1925 unsigned long action, void *data)
1927 struct device *dev = data;
1928 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1930 if (wait->dev != dev)
1934 case BUS_NOTIFY_UNBOUND_DRIVER:
1935 /* Protect from module unloading */
1936 wait_for_completion(&wait->completion);
1942 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
1944 struct sh_mobile_ceu_dev *pcdev;
1945 struct resource *res;
1949 struct bus_wait wait = {
1950 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1951 .notifier.notifier_call = bus_notify,
1953 struct device *csi2;
1955 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1956 irq = platform_get_irq(pdev, 0);
1957 if (!res || (int)irq <= 0) {
1958 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1963 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1965 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1970 INIT_LIST_HEAD(&pcdev->capture);
1971 spin_lock_init(&pcdev->lock);
1972 init_completion(&pcdev->complete);
1974 pcdev->pdata = pdev->dev.platform_data;
1975 if (!pcdev->pdata) {
1977 dev_err(&pdev->dev, "CEU platform data not set.\n");
1981 base = ioremap_nocache(res->start, resource_size(res));
1984 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1990 pcdev->video_limit = 0; /* only enabled if second resource exists */
1992 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1994 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1998 DMA_MEMORY_EXCLUSIVE);
2000 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2005 pcdev->video_limit = resource_size(res);
2009 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
2010 dev_name(&pdev->dev), pcdev);
2012 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2013 goto exit_release_mem;
2016 pm_suspend_ignore_children(&pdev->dev, true);
2017 pm_runtime_enable(&pdev->dev);
2018 pm_runtime_resume(&pdev->dev);
2020 pcdev->ici.priv = pcdev;
2021 pcdev->ici.v4l2_dev.dev = &pdev->dev;
2022 pcdev->ici.nr = pdev->id;
2023 pcdev->ici.drv_name = dev_name(&pdev->dev);
2024 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
2026 /* CSI2 interfacing */
2027 csi2 = pcdev->pdata->csi2_dev;
2031 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2036 * From this point the driver module will not unload, until
2037 * we complete the completion.
2040 if (!csi2->driver) {
2041 complete(&wait.completion);
2042 /* Either too late, or probing failed */
2043 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2049 * The module is still loaded, in the worst case it is hanging
2050 * in device release on our completion. So, _now_ dereferencing
2051 * the "owner" is safe!
2054 err = try_module_get(csi2->driver->owner);
2056 /* Let notifier complete, if it has been locked */
2057 complete(&wait.completion);
2058 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2065 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2066 if (IS_ERR(pcdev->alloc_ctx)) {
2067 err = PTR_ERR(pcdev->alloc_ctx);
2068 goto exit_module_put;
2071 err = soc_camera_host_register(&pcdev->ici);
2078 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2080 if (csi2 && csi2->driver)
2081 module_put(csi2->driver->owner);
2083 pm_runtime_disable(&pdev->dev);
2084 free_irq(pcdev->irq, pcdev);
2086 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2087 dma_release_declared_memory(&pdev->dev);
2096 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
2098 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2099 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2100 struct sh_mobile_ceu_dev, ici);
2101 struct device *csi2 = pcdev->pdata->csi2_dev;
2103 soc_camera_host_unregister(soc_host);
2104 pm_runtime_disable(&pdev->dev);
2105 free_irq(pcdev->irq, pcdev);
2106 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2107 dma_release_declared_memory(&pdev->dev);
2108 iounmap(pcdev->base);
2109 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2110 if (csi2 && csi2->driver)
2111 module_put(csi2->driver->owner);
2117 static int sh_mobile_ceu_runtime_nop(struct device *dev)
2119 /* Runtime PM callback shared between ->runtime_suspend()
2120 * and ->runtime_resume(). Simply returns success.
2122 * This driver re-initializes all registers after
2123 * pm_runtime_get_sync() anyway so there is no need
2124 * to save and restore registers here.
2129 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
2130 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2131 .runtime_resume = sh_mobile_ceu_runtime_nop,
2134 static struct platform_driver sh_mobile_ceu_driver = {
2136 .name = "sh_mobile_ceu",
2137 .pm = &sh_mobile_ceu_dev_pm_ops,
2139 .probe = sh_mobile_ceu_probe,
2140 .remove = __devexit_p(sh_mobile_ceu_remove),
2143 static int __init sh_mobile_ceu_init(void)
2145 /* Whatever return code */
2146 request_module("sh_mobile_csi2");
2147 return platform_driver_register(&sh_mobile_ceu_driver);
2150 static void __exit sh_mobile_ceu_exit(void)
2152 platform_driver_unregister(&sh_mobile_ceu_driver);
2155 module_init(sh_mobile_ceu_init);
2156 module_exit(sh_mobile_ceu_exit);
2158 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2159 MODULE_AUTHOR("Magnus Damm");
2160 MODULE_LICENSE("GPL");
2161 MODULE_ALIAS("platform:sh_mobile_ceu");