OMAP3: PM: decouple PER and CORE context save and restore
[linux-ginger.git] / drivers / media / video / sh_mobile_ceu_camera.c
blob65ac474c517acf9a3df55f4b4f20be35ca91dd4b
1 /*
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>
19 #include <linux/io.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/errno.h>
23 #include <linux/fs.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/moduleparam.h>
28 #include <linux/time.h>
29 #include <linux/version.h>
30 #include <linux/device.h>
31 #include <linux/platform_device.h>
32 #include <linux/videodev2.h>
33 #include <linux/pm_runtime.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-dev.h>
37 #include <media/soc_camera.h>
38 #include <media/sh_mobile_ceu.h>
39 #include <media/videobuf-dma-contig.h>
41 /* register offsets for sh7722 / sh7723 */
43 #define CAPSR 0x00 /* Capture start register */
44 #define CAPCR 0x04 /* Capture control register */
45 #define CAMCR 0x08 /* Capture interface control register */
46 #define CMCYR 0x0c /* Capture interface cycle register */
47 #define CAMOR 0x10 /* Capture interface offset register */
48 #define CAPWR 0x14 /* Capture interface width register */
49 #define CAIFR 0x18 /* Capture interface input format register */
50 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
51 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
52 #define CRCNTR 0x28 /* CEU register control register */
53 #define CRCMPR 0x2c /* CEU register forcible control register */
54 #define CFLCR 0x30 /* Capture filter control register */
55 #define CFSZR 0x34 /* Capture filter size clip register */
56 #define CDWDR 0x38 /* Capture destination width register */
57 #define CDAYR 0x3c /* Capture data address Y register */
58 #define CDACR 0x40 /* Capture data address C register */
59 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
60 #define CDBCR 0x48 /* Capture data bottom-field address C register */
61 #define CBDSR 0x4c /* Capture bundle destination size register */
62 #define CFWCR 0x5c /* Firewall operation control register */
63 #define CLFCR 0x60 /* Capture low-pass filter control register */
64 #define CDOCR 0x64 /* Capture data output control register */
65 #define CDDCR 0x68 /* Capture data complexity level register */
66 #define CDDAR 0x6c /* Capture data complexity level address register */
67 #define CEIER 0x70 /* Capture event interrupt enable register */
68 #define CETCR 0x74 /* Capture event flag clear register */
69 #define CSTSR 0x7c /* Capture status register */
70 #define CSRTR 0x80 /* Capture software reset register */
71 #define CDSSR 0x84 /* Capture data size register */
72 #define CDAYR2 0x90 /* Capture data address Y register 2 */
73 #define CDACR2 0x94 /* Capture data address C register 2 */
74 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
75 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
77 #undef DEBUG_GEOMETRY
78 #ifdef DEBUG_GEOMETRY
79 #define dev_geo dev_info
80 #else
81 #define dev_geo dev_dbg
82 #endif
84 /* per video frame buffer */
85 struct sh_mobile_ceu_buffer {
86 struct videobuf_buffer vb; /* v4l buffer must be first */
87 const struct soc_camera_data_format *fmt;
90 struct sh_mobile_ceu_dev {
91 struct soc_camera_host ici;
92 struct soc_camera_device *icd;
94 unsigned int irq;
95 void __iomem *base;
96 unsigned long video_limit;
98 /* lock used to protect videobuf */
99 spinlock_t lock;
100 struct list_head capture;
101 struct videobuf_buffer *active;
103 struct sh_mobile_ceu_info *pdata;
105 u32 cflcr;
107 unsigned int is_interlaced:1;
108 unsigned int image_mode:1;
109 unsigned int is_16bit:1;
112 struct sh_mobile_ceu_cam {
113 struct v4l2_rect ceu_rect;
114 unsigned int cam_width;
115 unsigned int cam_height;
116 const struct soc_camera_data_format *extra_fmt;
117 const struct soc_camera_data_format *camera_fmt;
120 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
122 unsigned long flags;
124 flags = SOCAM_MASTER |
125 SOCAM_PCLK_SAMPLE_RISING |
126 SOCAM_HSYNC_ACTIVE_HIGH |
127 SOCAM_HSYNC_ACTIVE_LOW |
128 SOCAM_VSYNC_ACTIVE_HIGH |
129 SOCAM_VSYNC_ACTIVE_LOW |
130 SOCAM_DATA_ACTIVE_HIGH;
132 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
133 flags |= SOCAM_DATAWIDTH_8;
135 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
136 flags |= SOCAM_DATAWIDTH_16;
138 if (flags & SOCAM_DATAWIDTH_MASK)
139 return flags;
141 return 0;
144 static void ceu_write(struct sh_mobile_ceu_dev *priv,
145 unsigned long reg_offs, u32 data)
147 iowrite32(data, priv->base + reg_offs);
150 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
152 return ioread32(priv->base + reg_offs);
156 * Videobuf operations
158 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
159 unsigned int *count,
160 unsigned int *size)
162 struct soc_camera_device *icd = vq->priv_data;
163 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
164 struct sh_mobile_ceu_dev *pcdev = ici->priv;
165 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
167 *size = PAGE_ALIGN(icd->user_width * icd->user_height *
168 bytes_per_pixel);
170 if (0 == *count)
171 *count = 2;
173 if (pcdev->video_limit) {
174 while (*size * *count > pcdev->video_limit)
175 (*count)--;
178 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
180 return 0;
183 static void free_buffer(struct videobuf_queue *vq,
184 struct sh_mobile_ceu_buffer *buf)
186 struct soc_camera_device *icd = vq->priv_data;
187 struct device *dev = icd->dev.parent;
189 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
190 &buf->vb, buf->vb.baddr, buf->vb.bsize);
192 if (in_interrupt())
193 BUG();
195 videobuf_waiton(&buf->vb, 0, 0);
196 videobuf_dma_contig_free(vq, &buf->vb);
197 dev_dbg(dev, "%s freed\n", __func__);
198 buf->vb.state = VIDEOBUF_NEEDS_INIT;
201 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
202 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
203 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
204 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
207 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
209 struct soc_camera_device *icd = pcdev->icd;
210 dma_addr_t phys_addr_top, phys_addr_bottom;
212 /* The hardware is _very_ picky about this sequence. Especially
213 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
214 * several not-so-well documented interrupt sources in CETCR.
216 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
217 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
218 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
219 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
220 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
222 if (!pcdev->active)
223 return;
225 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
226 ceu_write(pcdev, CDAYR, phys_addr_top);
227 if (pcdev->is_interlaced) {
228 phys_addr_bottom = phys_addr_top + icd->user_width;
229 ceu_write(pcdev, CDBYR, phys_addr_bottom);
232 switch (icd->current_fmt->fourcc) {
233 case V4L2_PIX_FMT_NV12:
234 case V4L2_PIX_FMT_NV21:
235 case V4L2_PIX_FMT_NV16:
236 case V4L2_PIX_FMT_NV61:
237 phys_addr_top += icd->user_width *
238 icd->user_height;
239 ceu_write(pcdev, CDACR, phys_addr_top);
240 if (pcdev->is_interlaced) {
241 phys_addr_bottom = phys_addr_top +
242 icd->user_width;
243 ceu_write(pcdev, CDBCR, phys_addr_bottom);
247 pcdev->active->state = VIDEOBUF_ACTIVE;
248 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
251 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
252 struct videobuf_buffer *vb,
253 enum v4l2_field field)
255 struct soc_camera_device *icd = vq->priv_data;
256 struct sh_mobile_ceu_buffer *buf;
257 int ret;
259 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
261 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
262 vb, vb->baddr, vb->bsize);
264 /* Added list head initialization on alloc */
265 WARN_ON(!list_empty(&vb->queue));
267 #ifdef DEBUG
268 /* This can be useful if you want to see if we actually fill
269 * the buffer with something */
270 memset((void *)vb->baddr, 0xaa, vb->bsize);
271 #endif
273 BUG_ON(NULL == icd->current_fmt);
275 if (buf->fmt != icd->current_fmt ||
276 vb->width != icd->user_width ||
277 vb->height != icd->user_height ||
278 vb->field != field) {
279 buf->fmt = icd->current_fmt;
280 vb->width = icd->user_width;
281 vb->height = icd->user_height;
282 vb->field = field;
283 vb->state = VIDEOBUF_NEEDS_INIT;
286 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
287 if (0 != vb->baddr && vb->bsize < vb->size) {
288 ret = -EINVAL;
289 goto out;
292 if (vb->state == VIDEOBUF_NEEDS_INIT) {
293 ret = videobuf_iolock(vq, vb, NULL);
294 if (ret)
295 goto fail;
296 vb->state = VIDEOBUF_PREPARED;
299 return 0;
300 fail:
301 free_buffer(vq, buf);
302 out:
303 return ret;
306 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
307 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
308 struct videobuf_buffer *vb)
310 struct soc_camera_device *icd = vq->priv_data;
311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
314 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
315 vb, vb->baddr, vb->bsize);
317 vb->state = VIDEOBUF_QUEUED;
318 list_add_tail(&vb->queue, &pcdev->capture);
320 if (!pcdev->active) {
321 pcdev->active = vb;
322 sh_mobile_ceu_capture(pcdev);
326 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
327 struct videobuf_buffer *vb)
329 struct soc_camera_device *icd = vq->priv_data;
330 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
331 struct sh_mobile_ceu_dev *pcdev = ici->priv;
332 unsigned long flags;
334 spin_lock_irqsave(&pcdev->lock, flags);
336 if (pcdev->active == vb) {
337 /* disable capture (release DMA buffer), reset */
338 ceu_write(pcdev, CAPSR, 1 << 16);
339 pcdev->active = NULL;
342 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
343 !list_empty(&vb->queue)) {
344 vb->state = VIDEOBUF_ERROR;
345 list_del_init(&vb->queue);
348 spin_unlock_irqrestore(&pcdev->lock, flags);
350 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
353 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
354 .buf_setup = sh_mobile_ceu_videobuf_setup,
355 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
356 .buf_queue = sh_mobile_ceu_videobuf_queue,
357 .buf_release = sh_mobile_ceu_videobuf_release,
360 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
362 struct sh_mobile_ceu_dev *pcdev = data;
363 struct videobuf_buffer *vb;
364 unsigned long flags;
366 spin_lock_irqsave(&pcdev->lock, flags);
368 vb = pcdev->active;
369 if (!vb)
370 /* Stale interrupt from a released buffer */
371 goto out;
373 list_del_init(&vb->queue);
375 if (!list_empty(&pcdev->capture))
376 pcdev->active = list_entry(pcdev->capture.next,
377 struct videobuf_buffer, queue);
378 else
379 pcdev->active = NULL;
381 sh_mobile_ceu_capture(pcdev);
383 vb->state = VIDEOBUF_DONE;
384 do_gettimeofday(&vb->ts);
385 vb->field_count++;
386 wake_up(&vb->done);
388 out:
389 spin_unlock_irqrestore(&pcdev->lock, flags);
391 return IRQ_HANDLED;
394 /* Called with .video_lock held */
395 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
397 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
398 struct sh_mobile_ceu_dev *pcdev = ici->priv;
400 if (pcdev->icd)
401 return -EBUSY;
403 dev_info(icd->dev.parent,
404 "SuperH Mobile CEU driver attached to camera %d\n",
405 icd->devnum);
407 pm_runtime_get_sync(ici->v4l2_dev.dev);
409 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
410 while (ceu_read(pcdev, CSTSR) & 1)
411 msleep(1);
413 pcdev->icd = icd;
415 return 0;
418 /* Called with .video_lock held */
419 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
421 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
422 struct sh_mobile_ceu_dev *pcdev = ici->priv;
423 unsigned long flags;
425 BUG_ON(icd != pcdev->icd);
427 /* disable capture, disable interrupts */
428 ceu_write(pcdev, CEIER, 0);
429 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
431 /* make sure active buffer is canceled */
432 spin_lock_irqsave(&pcdev->lock, flags);
433 if (pcdev->active) {
434 list_del(&pcdev->active->queue);
435 pcdev->active->state = VIDEOBUF_ERROR;
436 wake_up_all(&pcdev->active->done);
437 pcdev->active = NULL;
439 spin_unlock_irqrestore(&pcdev->lock, flags);
441 pm_runtime_put_sync(ici->v4l2_dev.dev);
443 dev_info(icd->dev.parent,
444 "SuperH Mobile CEU driver detached from camera %d\n",
445 icd->devnum);
447 pcdev->icd = NULL;
451 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
452 * in SH7722 Hardware Manual
454 static unsigned int size_dst(unsigned int src, unsigned int scale)
456 unsigned int mant_pre = scale >> 12;
457 if (!src || !scale)
458 return src;
459 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
460 mant_pre * 4096 / scale + 1;
463 static u16 calc_scale(unsigned int src, unsigned int *dst)
465 u16 scale;
467 if (src == *dst)
468 return 0;
470 scale = (src * 4096 / *dst) & ~7;
472 while (scale > 4096 && size_dst(src, scale) < *dst)
473 scale -= 8;
475 *dst = size_dst(src, scale);
477 return scale;
480 /* rect is guaranteed to not exceed the scaled camera rectangle */
481 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
482 unsigned int out_width,
483 unsigned int out_height)
485 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
486 struct sh_mobile_ceu_cam *cam = icd->host_priv;
487 struct v4l2_rect *rect = &cam->ceu_rect;
488 struct sh_mobile_ceu_dev *pcdev = ici->priv;
489 unsigned int height, width, cdwdr_width, in_width, in_height;
490 unsigned int left_offset, top_offset;
491 u32 camor;
493 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
494 rect->width, rect->height, rect->left, rect->top);
496 left_offset = rect->left;
497 top_offset = rect->top;
499 if (pcdev->image_mode) {
500 in_width = rect->width;
501 if (!pcdev->is_16bit) {
502 in_width *= 2;
503 left_offset *= 2;
505 width = cdwdr_width = out_width;
506 } else {
507 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
509 width = out_width * w_factor / 2;
511 if (!pcdev->is_16bit)
512 w_factor *= 2;
514 in_width = rect->width * w_factor / 2;
515 left_offset = left_offset * w_factor / 2;
517 cdwdr_width = width * 2;
520 height = out_height;
521 in_height = rect->height;
522 if (pcdev->is_interlaced) {
523 height /= 2;
524 in_height /= 2;
525 top_offset /= 2;
526 cdwdr_width *= 2;
529 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
530 camor = left_offset | (top_offset << 16);
532 dev_geo(icd->dev.parent,
533 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
534 (in_height << 16) | in_width, (height << 16) | width,
535 cdwdr_width);
537 ceu_write(pcdev, CAMOR, camor);
538 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
539 ceu_write(pcdev, CFSZR, (height << 16) | width);
540 ceu_write(pcdev, CDWDR, cdwdr_width);
543 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
545 u32 capsr = ceu_read(pcdev, CAPSR);
546 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
547 return capsr;
550 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
552 unsigned long timeout = jiffies + 10 * HZ;
555 * Wait until the end of the current frame. It can take a long time,
556 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
558 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
559 msleep(1);
561 if (time_after(jiffies, timeout)) {
562 dev_err(pcdev->ici.v4l2_dev.dev,
563 "Timeout waiting for frame end! Interface problem?\n");
564 return;
567 /* Wait until reset clears, this shall not hang... */
568 while (ceu_read(pcdev, CAPSR) & (1 << 16))
569 udelay(10);
571 /* Anything to restore? */
572 if (capsr & ~(1 << 16))
573 ceu_write(pcdev, CAPSR, capsr);
576 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
577 __u32 pixfmt)
579 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
580 struct sh_mobile_ceu_dev *pcdev = ici->priv;
581 int ret;
582 unsigned long camera_flags, common_flags, value;
583 int yuv_lineskip;
584 struct sh_mobile_ceu_cam *cam = icd->host_priv;
585 u32 capsr = capture_save_reset(pcdev);
587 camera_flags = icd->ops->query_bus_param(icd);
588 common_flags = soc_camera_bus_param_compatible(camera_flags,
589 make_bus_param(pcdev));
590 if (!common_flags)
591 return -EINVAL;
593 ret = icd->ops->set_bus_param(icd, common_flags);
594 if (ret < 0)
595 return ret;
597 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
598 case SOCAM_DATAWIDTH_8:
599 pcdev->is_16bit = 0;
600 break;
601 case SOCAM_DATAWIDTH_16:
602 pcdev->is_16bit = 1;
603 break;
604 default:
605 return -EINVAL;
608 ceu_write(pcdev, CRCNTR, 0);
609 ceu_write(pcdev, CRCMPR, 0);
611 value = 0x00000010; /* data fetch by default */
612 yuv_lineskip = 0;
614 switch (icd->current_fmt->fourcc) {
615 case V4L2_PIX_FMT_NV12:
616 case V4L2_PIX_FMT_NV21:
617 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
618 /* fall-through */
619 case V4L2_PIX_FMT_NV16:
620 case V4L2_PIX_FMT_NV61:
621 switch (cam->camera_fmt->fourcc) {
622 case V4L2_PIX_FMT_UYVY:
623 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
624 break;
625 case V4L2_PIX_FMT_VYUY:
626 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
627 break;
628 case V4L2_PIX_FMT_YUYV:
629 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
630 break;
631 case V4L2_PIX_FMT_YVYU:
632 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
633 break;
634 default:
635 BUG();
639 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
640 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
641 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
643 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
644 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
645 value |= pcdev->is_16bit ? 1 << 12 : 0;
646 ceu_write(pcdev, CAMCR, value);
648 ceu_write(pcdev, CAPCR, 0x00300000);
649 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
651 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
652 mdelay(1);
654 ceu_write(pcdev, CFLCR, pcdev->cflcr);
656 /* A few words about byte order (observed in Big Endian mode)
658 * In data fetch mode bytes are received in chunks of 8 bytes.
659 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
661 * The data is however by default written to memory in reverse order:
662 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
664 * The lowest three bits of CDOCR allows us to do swapping,
665 * using 7 we swap the data bytes to match the incoming order:
666 * D0, D1, D2, D3, D4, D5, D6, D7
668 value = 0x00000017;
669 if (yuv_lineskip)
670 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
672 ceu_write(pcdev, CDOCR, value);
673 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
675 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
676 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
677 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
678 icd->user_width, icd->user_height);
680 capture_restore(pcdev, capsr);
682 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
683 return 0;
686 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
688 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
689 struct sh_mobile_ceu_dev *pcdev = ici->priv;
690 unsigned long camera_flags, common_flags;
692 camera_flags = icd->ops->query_bus_param(icd);
693 common_flags = soc_camera_bus_param_compatible(camera_flags,
694 make_bus_param(pcdev));
695 if (!common_flags)
696 return -EINVAL;
698 return 0;
701 static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
703 .name = "NV12",
704 .depth = 12,
705 .fourcc = V4L2_PIX_FMT_NV12,
706 .colorspace = V4L2_COLORSPACE_JPEG,
709 .name = "NV21",
710 .depth = 12,
711 .fourcc = V4L2_PIX_FMT_NV21,
712 .colorspace = V4L2_COLORSPACE_JPEG,
715 .name = "NV16",
716 .depth = 16,
717 .fourcc = V4L2_PIX_FMT_NV16,
718 .colorspace = V4L2_COLORSPACE_JPEG,
721 .name = "NV61",
722 .depth = 16,
723 .fourcc = V4L2_PIX_FMT_NV61,
724 .colorspace = V4L2_COLORSPACE_JPEG,
728 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
729 struct soc_camera_format_xlate *xlate)
731 struct device *dev = icd->dev.parent;
732 int ret, k, n;
733 int formats = 0;
734 struct sh_mobile_ceu_cam *cam;
736 ret = sh_mobile_ceu_try_bus_param(icd);
737 if (ret < 0)
738 return 0;
740 if (!icd->host_priv) {
741 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
742 if (!cam)
743 return -ENOMEM;
745 icd->host_priv = cam;
746 } else {
747 cam = icd->host_priv;
750 /* Beginning of a pass */
751 if (!idx)
752 cam->extra_fmt = NULL;
754 switch (icd->formats[idx].fourcc) {
755 case V4L2_PIX_FMT_UYVY:
756 case V4L2_PIX_FMT_VYUY:
757 case V4L2_PIX_FMT_YUYV:
758 case V4L2_PIX_FMT_YVYU:
759 if (cam->extra_fmt)
760 goto add_single_format;
763 * Our case is simple so far: for any of the above four camera
764 * formats we add all our four synthesized NV* formats, so,
765 * just marking the device with a single flag suffices. If
766 * the format generation rules are more complex, you would have
767 * to actually hang your already added / counted formats onto
768 * the host_priv pointer and check whether the format you're
769 * going to add now is already there.
771 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
773 n = ARRAY_SIZE(sh_mobile_ceu_formats);
774 formats += n;
775 for (k = 0; xlate && k < n; k++) {
776 xlate->host_fmt = &sh_mobile_ceu_formats[k];
777 xlate->cam_fmt = icd->formats + idx;
778 xlate->buswidth = icd->formats[idx].depth;
779 xlate++;
780 dev_dbg(dev, "Providing format %s using %s\n",
781 sh_mobile_ceu_formats[k].name,
782 icd->formats[idx].name);
784 default:
785 add_single_format:
786 /* Generic pass-through */
787 formats++;
788 if (xlate) {
789 xlate->host_fmt = icd->formats + idx;
790 xlate->cam_fmt = icd->formats + idx;
791 xlate->buswidth = icd->formats[idx].depth;
792 xlate++;
793 dev_dbg(dev,
794 "Providing format %s in pass-through mode\n",
795 icd->formats[idx].name);
799 return formats;
802 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
804 kfree(icd->host_priv);
805 icd->host_priv = NULL;
808 /* Check if any dimension of r1 is smaller than respective one of r2 */
809 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
811 return r1->width < r2->width || r1->height < r2->height;
814 /* Check if r1 fails to cover r2 */
815 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
817 return r1->left > r2->left || r1->top > r2->top ||
818 r1->left + r1->width < r2->left + r2->width ||
819 r1->top + r1->height < r2->top + r2->height;
822 static unsigned int scale_down(unsigned int size, unsigned int scale)
824 return (size * 4096 + scale / 2) / scale;
827 static unsigned int scale_up(unsigned int size, unsigned int scale)
829 return (size * scale + 2048) / 4096;
832 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
834 return (input * 4096 + output / 2) / output;
837 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
839 struct v4l2_crop crop;
840 struct v4l2_cropcap cap;
841 int ret;
843 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
845 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
846 if (!ret) {
847 *rect = crop.c;
848 return ret;
851 /* Camera driver doesn't support .g_crop(), assume default rectangle */
852 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
854 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
855 if (ret < 0)
856 return ret;
858 *rect = cap.defrect;
860 return ret;
864 * The common for both scaling and cropping iterative approach is:
865 * 1. try if the client can produce exactly what requested by the user
866 * 2. if (1) failed, try to double the client image until we get one big enough
867 * 3. if (2) failed, try to request the maximum image
869 static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
870 struct v4l2_crop *cam_crop)
872 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
873 struct device *dev = sd->v4l2_dev->dev;
874 struct v4l2_cropcap cap;
875 int ret;
876 unsigned int width, height;
878 v4l2_subdev_call(sd, video, s_crop, crop);
879 ret = client_g_rect(sd, cam_rect);
880 if (ret < 0)
881 return ret;
884 * Now cam_crop contains the current camera input rectangle, and it must
885 * be within camera cropcap bounds
887 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
888 /* Even if camera S_CROP failed, but camera rectangle matches */
889 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
890 rect->width, rect->height, rect->left, rect->top);
891 return 0;
894 /* Try to fix cropping, that camera hasn't managed to set */
895 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
896 cam_rect->width, cam_rect->height,
897 cam_rect->left, cam_rect->top,
898 rect->width, rect->height, rect->left, rect->top);
900 /* We need sensor maximum rectangle */
901 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
902 if (ret < 0)
903 return ret;
905 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
906 cap.bounds.width);
907 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
908 cap.bounds.height);
911 * Popular special case - some cameras can only handle fixed sizes like
912 * QVGA, VGA,... Take care to avoid infinite loop.
914 width = max(cam_rect->width, 2);
915 height = max(cam_rect->height, 2);
917 while (!ret && (is_smaller(cam_rect, rect) ||
918 is_inside(cam_rect, rect)) &&
919 (cap.bounds.width > width || cap.bounds.height > height)) {
921 width *= 2;
922 height *= 2;
924 cam_rect->width = width;
925 cam_rect->height = height;
928 * We do not know what capabilities the camera has to set up
929 * left and top borders. We could try to be smarter in iterating
930 * them, e.g., if camera current left is to the right of the
931 * target left, set it to the middle point between the current
932 * left and minimum left. But that would add too much
933 * complexity: we would have to iterate each border separately.
935 if (cam_rect->left > rect->left)
936 cam_rect->left = cap.bounds.left;
938 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
939 cam_rect->width = rect->left + rect->width -
940 cam_rect->left;
942 if (cam_rect->top > rect->top)
943 cam_rect->top = cap.bounds.top;
945 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
946 cam_rect->height = rect->top + rect->height -
947 cam_rect->top;
949 v4l2_subdev_call(sd, video, s_crop, cam_crop);
950 ret = client_g_rect(sd, cam_rect);
951 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
952 cam_rect->width, cam_rect->height,
953 cam_rect->left, cam_rect->top);
956 /* S_CROP must not modify the rectangle */
957 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
959 * The camera failed to configure a suitable cropping,
960 * we cannot use the current rectangle, set to max
962 *cam_rect = cap.bounds;
963 v4l2_subdev_call(sd, video, s_crop, cam_crop);
964 ret = client_g_rect(sd, cam_rect);
965 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
966 cam_rect->width, cam_rect->height,
967 cam_rect->left, cam_rect->top);
970 return ret;
973 static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
974 unsigned int *scale_h, unsigned int *scale_v)
976 struct v4l2_format f;
977 int ret;
979 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
981 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
982 if (ret < 0)
983 return ret;
985 *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
986 *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
988 return 0;
991 static int get_camera_subwin(struct soc_camera_device *icd,
992 struct v4l2_rect *cam_subrect,
993 unsigned int cam_hscale, unsigned int cam_vscale)
995 struct sh_mobile_ceu_cam *cam = icd->host_priv;
996 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
998 if (!ceu_rect->width) {
999 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1000 struct device *dev = icd->dev.parent;
1001 struct v4l2_format f;
1002 struct v4l2_pix_format *pix = &f.fmt.pix;
1003 int ret;
1004 /* First time */
1006 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1008 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1009 if (ret < 0)
1010 return ret;
1012 dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
1014 if (pix->width > 2560) {
1015 ceu_rect->width = 2560;
1016 ceu_rect->left = (pix->width - 2560) / 2;
1017 } else {
1018 ceu_rect->width = pix->width;
1019 ceu_rect->left = 0;
1022 if (pix->height > 1920) {
1023 ceu_rect->height = 1920;
1024 ceu_rect->top = (pix->height - 1920) / 2;
1025 } else {
1026 ceu_rect->height = pix->height;
1027 ceu_rect->top = 0;
1030 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1031 ceu_rect->width, ceu_rect->height,
1032 ceu_rect->left, ceu_rect->top);
1035 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1036 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1037 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1038 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1040 return 0;
1043 static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
1044 bool ceu_can_scale)
1046 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1047 struct device *dev = icd->dev.parent;
1048 struct v4l2_pix_format *pix = &f->fmt.pix;
1049 unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
1050 unsigned int max_width, max_height;
1051 struct v4l2_cropcap cap;
1052 int ret;
1054 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1056 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1057 if (ret < 0)
1058 return ret;
1060 max_width = min(cap.bounds.width, 2560);
1061 max_height = min(cap.bounds.height, 1920);
1063 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1064 if (ret < 0)
1065 return ret;
1067 dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
1069 if ((width == pix->width && height == pix->height) || !ceu_can_scale)
1070 return 0;
1072 /* Camera set a format, but geometry is not precise, try to improve */
1073 tmp_w = pix->width;
1074 tmp_h = pix->height;
1076 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1077 while ((width > tmp_w || height > tmp_h) &&
1078 tmp_w < max_width && tmp_h < max_height) {
1079 tmp_w = min(2 * tmp_w, max_width);
1080 tmp_h = min(2 * tmp_h, max_height);
1081 pix->width = tmp_w;
1082 pix->height = tmp_h;
1083 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1084 dev_geo(dev, "Camera scaled to %ux%u\n",
1085 pix->width, pix->height);
1086 if (ret < 0) {
1087 /* This shouldn't happen */
1088 dev_err(dev, "Client failed to set format: %d\n", ret);
1089 return ret;
1093 return 0;
1097 * @rect - camera cropped rectangle
1098 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1099 * @ceu_rect - on output calculated CEU crop rectangle
1101 static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1102 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
1103 struct v4l2_format *f, bool ceu_can_scale)
1105 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1106 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1107 struct device *dev = icd->dev.parent;
1108 struct v4l2_format f_tmp = *f;
1109 struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
1110 unsigned int scale_h, scale_v;
1111 int ret;
1113 /* 5. Apply iterative camera S_FMT for camera user window. */
1114 ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
1115 if (ret < 0)
1116 return ret;
1118 dev_geo(dev, "5: camera scaled to %ux%u\n",
1119 pix_tmp->width, pix_tmp->height);
1121 /* 6. Retrieve camera output window (g_fmt) */
1123 /* unneeded - it is already in "f_tmp" */
1125 /* 7. Calculate new camera scales. */
1126 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1127 if (ret < 0)
1128 return ret;
1130 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1132 cam->cam_width = pix_tmp->width;
1133 cam->cam_height = pix_tmp->height;
1134 f->fmt.pix.width = pix_tmp->width;
1135 f->fmt.pix.height = pix_tmp->height;
1138 * 8. Calculate new CEU crop - apply camera scales to previously
1139 * calculated "effective" crop.
1141 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1142 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1143 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1144 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1146 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1147 ceu_rect->width, ceu_rect->height,
1148 ceu_rect->left, ceu_rect->top);
1150 return 0;
1153 /* Get combined scales */
1154 static int get_scales(struct soc_camera_device *icd,
1155 unsigned int *scale_h, unsigned int *scale_v)
1157 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1158 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1159 struct v4l2_crop cam_crop;
1160 unsigned int width_in, height_in;
1161 int ret;
1163 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1165 ret = client_g_rect(sd, &cam_crop.c);
1166 if (ret < 0)
1167 return ret;
1169 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1170 if (ret < 0)
1171 return ret;
1173 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1174 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1176 *scale_h = calc_generic_scale(cam->ceu_rect.width, icd->user_width);
1177 *scale_v = calc_generic_scale(cam->ceu_rect.height, icd->user_height);
1179 return 0;
1183 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1184 * framerate by always requesting the maximum image from the client. See
1185 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1186 * scaling and cropping algorithms and for the meaning of referenced here steps.
1188 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1189 struct v4l2_crop *a)
1191 struct v4l2_rect *rect = &a->c;
1192 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1193 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1194 struct v4l2_crop cam_crop;
1195 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1196 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
1197 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1198 struct device *dev = icd->dev.parent;
1199 struct v4l2_format f;
1200 struct v4l2_pix_format *pix = &f.fmt.pix;
1201 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1202 out_width, out_height;
1203 u32 capsr, cflcr;
1204 int ret;
1206 /* 1. Calculate current combined scales. */
1207 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1208 if (ret < 0)
1209 return ret;
1211 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1213 /* 2. Apply iterative camera S_CROP for new input window. */
1214 ret = client_s_crop(sd, a, &cam_crop);
1215 if (ret < 0)
1216 return ret;
1218 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1219 cam_rect->width, cam_rect->height,
1220 cam_rect->left, cam_rect->top);
1222 /* On success cam_crop contains current camera crop */
1225 * 3. If old combined scales applied to new crop produce an impossible
1226 * user window, adjust scales to produce nearest possible window.
1228 out_width = scale_down(rect->width, scale_comb_h);
1229 out_height = scale_down(rect->height, scale_comb_v);
1231 if (out_width > 2560)
1232 out_width = 2560;
1233 else if (out_width < 2)
1234 out_width = 2;
1236 if (out_height > 1920)
1237 out_height = 1920;
1238 else if (out_height < 4)
1239 out_height = 4;
1241 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1243 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1246 * 5. Using actual input window and calculated combined scales calculate
1247 * camera target output window.
1249 pix->width = scale_down(cam_rect->width, scale_comb_h);
1250 pix->height = scale_down(cam_rect->height, scale_comb_v);
1252 dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
1254 /* 6. - 9. */
1255 pix->pixelformat = cam->camera_fmt->fourcc;
1256 pix->colorspace = cam->camera_fmt->colorspace;
1258 capsr = capture_save_reset(pcdev);
1259 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1261 /* Make relative to camera rectangle */
1262 rect->left -= cam_rect->left;
1263 rect->top -= cam_rect->top;
1265 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1267 ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
1268 pcdev->image_mode && !pcdev->is_interlaced);
1270 dev_geo(dev, "6-9: %d\n", ret);
1272 /* 10. Use CEU cropping to crop to the new window. */
1273 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1275 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1276 ceu_rect->width, ceu_rect->height,
1277 ceu_rect->left, ceu_rect->top);
1280 * 11. Calculate CEU scales from camera scales from results of (10) and
1281 * user window from (3)
1283 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1284 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
1286 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1288 /* 12. Apply CEU scales. */
1289 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1290 if (cflcr != pcdev->cflcr) {
1291 pcdev->cflcr = cflcr;
1292 ceu_write(pcdev, CFLCR, cflcr);
1295 /* Restore capture */
1296 if (pcdev->active)
1297 capsr |= 1;
1298 capture_restore(pcdev, capsr);
1300 icd->user_width = out_width;
1301 icd->user_height = out_height;
1303 /* Even if only camera cropping succeeded */
1304 return ret;
1307 /* Similar to set_crop multistage iterative algorithm */
1308 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1309 struct v4l2_format *f)
1311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1313 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1314 struct v4l2_pix_format *pix = &f->fmt.pix;
1315 struct v4l2_format cam_f = *f;
1316 struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
1317 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1318 struct device *dev = icd->dev.parent;
1319 __u32 pixfmt = pix->pixelformat;
1320 const struct soc_camera_format_xlate *xlate;
1321 struct v4l2_crop cam_crop;
1322 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1323 unsigned int scale_cam_h, scale_cam_v;
1324 u16 scale_v, scale_h;
1325 int ret;
1326 bool is_interlaced, image_mode;
1328 switch (pix->field) {
1329 case V4L2_FIELD_INTERLACED:
1330 is_interlaced = true;
1331 break;
1332 case V4L2_FIELD_ANY:
1333 default:
1334 pix->field = V4L2_FIELD_NONE;
1335 /* fall-through */
1336 case V4L2_FIELD_NONE:
1337 is_interlaced = false;
1338 break;
1341 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1342 if (!xlate) {
1343 dev_warn(dev, "Format %x not found\n", pixfmt);
1344 return -EINVAL;
1347 /* 1. Calculate current camera scales. */
1348 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1350 ret = client_g_rect(sd, cam_rect);
1351 if (ret < 0)
1352 return ret;
1354 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1355 if (ret < 0)
1356 return ret;
1358 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1361 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1362 * scaled back at current camera scales onto input window.
1364 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1365 if (ret < 0)
1366 return ret;
1368 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1369 cam_subrect.width, cam_subrect.height,
1370 cam_subrect.left, cam_subrect.top);
1373 * 3. Calculate new combined scales from "effective" input window to
1374 * requested user window.
1376 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1377 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1379 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1382 * 4. Calculate camera output window by applying combined scales to real
1383 * input window.
1385 cam_pix->width = scale_down(cam_rect->width, scale_h);
1386 cam_pix->height = scale_down(cam_rect->height, scale_v);
1387 cam_pix->pixelformat = xlate->cam_fmt->fourcc;
1389 switch (pixfmt) {
1390 case V4L2_PIX_FMT_NV12:
1391 case V4L2_PIX_FMT_NV21:
1392 case V4L2_PIX_FMT_NV16:
1393 case V4L2_PIX_FMT_NV61:
1394 image_mode = true;
1395 break;
1396 default:
1397 image_mode = false;
1400 dev_geo(dev, "4: camera output %ux%u\n",
1401 cam_pix->width, cam_pix->height);
1403 /* 5. - 9. */
1404 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
1405 image_mode && !is_interlaced);
1407 dev_geo(dev, "5-9: client scale %d\n", ret);
1409 /* Done with the camera. Now see if we can improve the result */
1411 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1412 ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
1413 if (ret < 0)
1414 return ret;
1416 /* 10. Use CEU scaling to scale to the requested user window. */
1418 /* We cannot scale up */
1419 if (pix->width > cam_pix->width)
1420 pix->width = cam_pix->width;
1421 if (pix->width > ceu_rect.width)
1422 pix->width = ceu_rect.width;
1424 if (pix->height > cam_pix->height)
1425 pix->height = cam_pix->height;
1426 if (pix->height > ceu_rect.height)
1427 pix->height = ceu_rect.height;
1429 /* Let's rock: scale pix->{width x height} down to width x height */
1430 scale_h = calc_scale(ceu_rect.width, &pix->width);
1431 scale_v = calc_scale(ceu_rect.height, &pix->height);
1433 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1434 ceu_rect.width, scale_h, pix->width,
1435 ceu_rect.height, scale_v, pix->height);
1437 pcdev->cflcr = scale_h | (scale_v << 16);
1439 icd->buswidth = xlate->buswidth;
1440 icd->current_fmt = xlate->host_fmt;
1441 cam->camera_fmt = xlate->cam_fmt;
1442 cam->ceu_rect = ceu_rect;
1444 pcdev->is_interlaced = is_interlaced;
1445 pcdev->image_mode = image_mode;
1447 return 0;
1450 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1451 struct v4l2_format *f)
1453 const struct soc_camera_format_xlate *xlate;
1454 struct v4l2_pix_format *pix = &f->fmt.pix;
1455 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1456 __u32 pixfmt = pix->pixelformat;
1457 int width, height;
1458 int ret;
1460 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1461 if (!xlate) {
1462 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
1463 return -EINVAL;
1466 /* FIXME: calculate using depth and bus width */
1468 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1469 &pix->height, 4, 1920, 2, 0);
1471 width = pix->width;
1472 height = pix->height;
1474 pix->bytesperline = pix->width *
1475 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1476 pix->sizeimage = pix->height * pix->bytesperline;
1478 pix->pixelformat = xlate->cam_fmt->fourcc;
1480 /* limit to sensor capabilities */
1481 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1482 pix->pixelformat = pixfmt;
1483 if (ret < 0)
1484 return ret;
1486 switch (pixfmt) {
1487 case V4L2_PIX_FMT_NV12:
1488 case V4L2_PIX_FMT_NV21:
1489 case V4L2_PIX_FMT_NV16:
1490 case V4L2_PIX_FMT_NV61:
1491 /* FIXME: check against rect_max after converting soc-camera */
1492 /* We can scale precisely, need a bigger image from camera */
1493 if (pix->width < width || pix->height < height) {
1494 int tmp_w = pix->width, tmp_h = pix->height;
1495 pix->width = 2560;
1496 pix->height = 1920;
1497 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1498 if (ret < 0) {
1499 /* Shouldn't actually happen... */
1500 dev_err(icd->dev.parent,
1501 "FIXME: try_fmt() returned %d\n", ret);
1502 pix->width = tmp_w;
1503 pix->height = tmp_h;
1506 if (pix->width > width)
1507 pix->width = width;
1508 if (pix->height > height)
1509 pix->height = height;
1512 return ret;
1515 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1516 struct v4l2_requestbuffers *p)
1518 int i;
1520 /* This is for locking debugging only. I removed spinlocks and now I
1521 * check whether .prepare is ever called on a linked buffer, or whether
1522 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1523 * it hadn't triggered */
1524 for (i = 0; i < p->count; i++) {
1525 struct sh_mobile_ceu_buffer *buf;
1527 buf = container_of(icf->vb_vidq.bufs[i],
1528 struct sh_mobile_ceu_buffer, vb);
1529 INIT_LIST_HEAD(&buf->vb.queue);
1532 return 0;
1535 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1537 struct soc_camera_file *icf = file->private_data;
1538 struct sh_mobile_ceu_buffer *buf;
1540 buf = list_entry(icf->vb_vidq.stream.next,
1541 struct sh_mobile_ceu_buffer, vb.stream);
1543 poll_wait(file, &buf->vb.done, pt);
1545 if (buf->vb.state == VIDEOBUF_DONE ||
1546 buf->vb.state == VIDEOBUF_ERROR)
1547 return POLLIN|POLLRDNORM;
1549 return 0;
1552 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1553 struct v4l2_capability *cap)
1555 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1556 cap->version = KERNEL_VERSION(0, 0, 5);
1557 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1558 return 0;
1561 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1562 struct soc_camera_device *icd)
1564 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1565 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1567 videobuf_queue_dma_contig_init(q,
1568 &sh_mobile_ceu_videobuf_ops,
1569 icd->dev.parent, &pcdev->lock,
1570 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1571 pcdev->is_interlaced ?
1572 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
1573 sizeof(struct sh_mobile_ceu_buffer),
1574 icd);
1577 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1578 struct v4l2_control *ctrl)
1580 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1581 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1582 u32 val;
1584 switch (ctrl->id) {
1585 case V4L2_CID_SHARPNESS:
1586 val = ceu_read(pcdev, CLFCR);
1587 ctrl->value = val ^ 1;
1588 return 0;
1590 return -ENOIOCTLCMD;
1593 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1594 struct v4l2_control *ctrl)
1596 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1597 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1599 switch (ctrl->id) {
1600 case V4L2_CID_SHARPNESS:
1601 switch (icd->current_fmt->fourcc) {
1602 case V4L2_PIX_FMT_NV12:
1603 case V4L2_PIX_FMT_NV21:
1604 case V4L2_PIX_FMT_NV16:
1605 case V4L2_PIX_FMT_NV61:
1606 ceu_write(pcdev, CLFCR, !ctrl->value);
1607 return 0;
1609 return -EINVAL;
1611 return -ENOIOCTLCMD;
1614 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1616 .id = V4L2_CID_SHARPNESS,
1617 .type = V4L2_CTRL_TYPE_BOOLEAN,
1618 .name = "Low-pass filter",
1619 .minimum = 0,
1620 .maximum = 1,
1621 .step = 1,
1622 .default_value = 0,
1626 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1627 .owner = THIS_MODULE,
1628 .add = sh_mobile_ceu_add_device,
1629 .remove = sh_mobile_ceu_remove_device,
1630 .get_formats = sh_mobile_ceu_get_formats,
1631 .put_formats = sh_mobile_ceu_put_formats,
1632 .set_crop = sh_mobile_ceu_set_crop,
1633 .set_fmt = sh_mobile_ceu_set_fmt,
1634 .try_fmt = sh_mobile_ceu_try_fmt,
1635 .set_ctrl = sh_mobile_ceu_set_ctrl,
1636 .get_ctrl = sh_mobile_ceu_get_ctrl,
1637 .reqbufs = sh_mobile_ceu_reqbufs,
1638 .poll = sh_mobile_ceu_poll,
1639 .querycap = sh_mobile_ceu_querycap,
1640 .set_bus_param = sh_mobile_ceu_set_bus_param,
1641 .init_videobuf = sh_mobile_ceu_init_videobuf,
1642 .controls = sh_mobile_ceu_controls,
1643 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
1646 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
1648 struct sh_mobile_ceu_dev *pcdev;
1649 struct resource *res;
1650 void __iomem *base;
1651 unsigned int irq;
1652 int err = 0;
1654 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1655 irq = platform_get_irq(pdev, 0);
1656 if (!res || !irq) {
1657 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1658 err = -ENODEV;
1659 goto exit;
1662 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1663 if (!pcdev) {
1664 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1665 err = -ENOMEM;
1666 goto exit;
1669 INIT_LIST_HEAD(&pcdev->capture);
1670 spin_lock_init(&pcdev->lock);
1672 pcdev->pdata = pdev->dev.platform_data;
1673 if (!pcdev->pdata) {
1674 err = -EINVAL;
1675 dev_err(&pdev->dev, "CEU platform data not set.\n");
1676 goto exit_kfree;
1679 base = ioremap_nocache(res->start, resource_size(res));
1680 if (!base) {
1681 err = -ENXIO;
1682 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1683 goto exit_kfree;
1686 pcdev->irq = irq;
1687 pcdev->base = base;
1688 pcdev->video_limit = 0; /* only enabled if second resource exists */
1690 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1691 if (res) {
1692 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1693 res->start,
1694 resource_size(res),
1695 DMA_MEMORY_MAP |
1696 DMA_MEMORY_EXCLUSIVE);
1697 if (!err) {
1698 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1699 err = -ENXIO;
1700 goto exit_iounmap;
1703 pcdev->video_limit = resource_size(res);
1706 /* request irq */
1707 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
1708 dev_name(&pdev->dev), pcdev);
1709 if (err) {
1710 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1711 goto exit_release_mem;
1714 pm_suspend_ignore_children(&pdev->dev, true);
1715 pm_runtime_enable(&pdev->dev);
1716 pm_runtime_resume(&pdev->dev);
1718 pcdev->ici.priv = pcdev;
1719 pcdev->ici.v4l2_dev.dev = &pdev->dev;
1720 pcdev->ici.nr = pdev->id;
1721 pcdev->ici.drv_name = dev_name(&pdev->dev);
1722 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1724 err = soc_camera_host_register(&pcdev->ici);
1725 if (err)
1726 goto exit_free_irq;
1728 return 0;
1730 exit_free_irq:
1731 free_irq(pcdev->irq, pcdev);
1732 exit_release_mem:
1733 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1734 dma_release_declared_memory(&pdev->dev);
1735 exit_iounmap:
1736 iounmap(base);
1737 exit_kfree:
1738 kfree(pcdev);
1739 exit:
1740 return err;
1743 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
1745 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1746 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1747 struct sh_mobile_ceu_dev, ici);
1749 soc_camera_host_unregister(soc_host);
1750 free_irq(pcdev->irq, pcdev);
1751 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1752 dma_release_declared_memory(&pdev->dev);
1753 iounmap(pcdev->base);
1754 kfree(pcdev);
1755 return 0;
1758 static int sh_mobile_ceu_runtime_nop(struct device *dev)
1760 /* Runtime PM callback shared between ->runtime_suspend()
1761 * and ->runtime_resume(). Simply returns success.
1763 * This driver re-initializes all registers after
1764 * pm_runtime_get_sync() anyway so there is no need
1765 * to save and restore registers here.
1767 return 0;
1770 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1771 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1772 .runtime_resume = sh_mobile_ceu_runtime_nop,
1775 static struct platform_driver sh_mobile_ceu_driver = {
1776 .driver = {
1777 .name = "sh_mobile_ceu",
1778 .pm = &sh_mobile_ceu_dev_pm_ops,
1780 .probe = sh_mobile_ceu_probe,
1781 .remove = __exit_p(sh_mobile_ceu_remove),
1784 static int __init sh_mobile_ceu_init(void)
1786 return platform_driver_register(&sh_mobile_ceu_driver);
1789 static void __exit sh_mobile_ceu_exit(void)
1791 platform_driver_unregister(&sh_mobile_ceu_driver);
1794 module_init(sh_mobile_ceu_init);
1795 module_exit(sh_mobile_ceu_exit);
1797 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1798 MODULE_AUTHOR("Magnus Damm");
1799 MODULE_LICENSE("GPL");
1800 MODULE_ALIAS("platform:sh_mobile_ceu");