mac80211: fix WARN_ON in the new work code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / sh_mobile_ceu_camera.c
blob9c8b7c7b89ee6de7a6c5199636feb2d49194947a
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>
34 #include <linux/sched.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-dev.h>
38 #include <media/soc_camera.h>
39 #include <media/sh_mobile_ceu.h>
40 #include <media/videobuf-dma-contig.h>
42 /* register offsets for sh7722 / sh7723 */
44 #define CAPSR 0x00 /* Capture start register */
45 #define CAPCR 0x04 /* Capture control register */
46 #define CAMCR 0x08 /* Capture interface control register */
47 #define CMCYR 0x0c /* Capture interface cycle register */
48 #define CAMOR 0x10 /* Capture interface offset register */
49 #define CAPWR 0x14 /* Capture interface width register */
50 #define CAIFR 0x18 /* Capture interface input format register */
51 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
52 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
53 #define CRCNTR 0x28 /* CEU register control register */
54 #define CRCMPR 0x2c /* CEU register forcible control register */
55 #define CFLCR 0x30 /* Capture filter control register */
56 #define CFSZR 0x34 /* Capture filter size clip register */
57 #define CDWDR 0x38 /* Capture destination width register */
58 #define CDAYR 0x3c /* Capture data address Y register */
59 #define CDACR 0x40 /* Capture data address C register */
60 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
61 #define CDBCR 0x48 /* Capture data bottom-field address C register */
62 #define CBDSR 0x4c /* Capture bundle destination size register */
63 #define CFWCR 0x5c /* Firewall operation control register */
64 #define CLFCR 0x60 /* Capture low-pass filter control register */
65 #define CDOCR 0x64 /* Capture data output control register */
66 #define CDDCR 0x68 /* Capture data complexity level register */
67 #define CDDAR 0x6c /* Capture data complexity level address register */
68 #define CEIER 0x70 /* Capture event interrupt enable register */
69 #define CETCR 0x74 /* Capture event flag clear register */
70 #define CSTSR 0x7c /* Capture status register */
71 #define CSRTR 0x80 /* Capture software reset register */
72 #define CDSSR 0x84 /* Capture data size register */
73 #define CDAYR2 0x90 /* Capture data address Y register 2 */
74 #define CDACR2 0x94 /* Capture data address C register 2 */
75 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
76 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
78 #undef DEBUG_GEOMETRY
79 #ifdef DEBUG_GEOMETRY
80 #define dev_geo dev_info
81 #else
82 #define dev_geo dev_dbg
83 #endif
85 /* per video frame buffer */
86 struct sh_mobile_ceu_buffer {
87 struct videobuf_buffer vb; /* v4l buffer must be first */
88 const struct soc_camera_data_format *fmt;
91 struct sh_mobile_ceu_dev {
92 struct soc_camera_host ici;
93 struct soc_camera_device *icd;
95 unsigned int irq;
96 void __iomem *base;
97 unsigned long video_limit;
99 /* lock used to protect videobuf */
100 spinlock_t lock;
101 struct list_head capture;
102 struct videobuf_buffer *active;
104 struct sh_mobile_ceu_info *pdata;
106 u32 cflcr;
108 unsigned int is_interlaced:1;
109 unsigned int image_mode:1;
110 unsigned int is_16bit:1;
113 struct sh_mobile_ceu_cam {
114 struct v4l2_rect ceu_rect;
115 unsigned int cam_width;
116 unsigned int cam_height;
117 const struct soc_camera_data_format *extra_fmt;
118 const struct soc_camera_data_format *camera_fmt;
121 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
123 unsigned long flags;
125 flags = SOCAM_MASTER |
126 SOCAM_PCLK_SAMPLE_RISING |
127 SOCAM_HSYNC_ACTIVE_HIGH |
128 SOCAM_HSYNC_ACTIVE_LOW |
129 SOCAM_VSYNC_ACTIVE_HIGH |
130 SOCAM_VSYNC_ACTIVE_LOW |
131 SOCAM_DATA_ACTIVE_HIGH;
133 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
134 flags |= SOCAM_DATAWIDTH_8;
136 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
137 flags |= SOCAM_DATAWIDTH_16;
139 if (flags & SOCAM_DATAWIDTH_MASK)
140 return flags;
142 return 0;
145 static void ceu_write(struct sh_mobile_ceu_dev *priv,
146 unsigned long reg_offs, u32 data)
148 iowrite32(data, priv->base + reg_offs);
151 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
153 return ioread32(priv->base + reg_offs);
157 * Videobuf operations
159 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
160 unsigned int *count,
161 unsigned int *size)
163 struct soc_camera_device *icd = vq->priv_data;
164 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
165 struct sh_mobile_ceu_dev *pcdev = ici->priv;
166 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
168 *size = PAGE_ALIGN(icd->user_width * icd->user_height *
169 bytes_per_pixel);
171 if (0 == *count)
172 *count = 2;
174 if (pcdev->video_limit) {
175 while (*size * *count > pcdev->video_limit)
176 (*count)--;
179 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
181 return 0;
184 static void free_buffer(struct videobuf_queue *vq,
185 struct sh_mobile_ceu_buffer *buf)
187 struct soc_camera_device *icd = vq->priv_data;
188 struct device *dev = icd->dev.parent;
190 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
191 &buf->vb, buf->vb.baddr, buf->vb.bsize);
193 if (in_interrupt())
194 BUG();
196 videobuf_waiton(&buf->vb, 0, 0);
197 videobuf_dma_contig_free(vq, &buf->vb);
198 dev_dbg(dev, "%s freed\n", __func__);
199 buf->vb.state = VIDEOBUF_NEEDS_INIT;
202 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
203 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
204 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
205 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
208 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
210 struct soc_camera_device *icd = pcdev->icd;
211 dma_addr_t phys_addr_top, phys_addr_bottom;
213 /* The hardware is _very_ picky about this sequence. Especially
214 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
215 * several not-so-well documented interrupt sources in CETCR.
217 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
218 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
219 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
220 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
221 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
223 if (!pcdev->active)
224 return;
226 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
227 ceu_write(pcdev, CDAYR, phys_addr_top);
228 if (pcdev->is_interlaced) {
229 phys_addr_bottom = phys_addr_top + icd->user_width;
230 ceu_write(pcdev, CDBYR, phys_addr_bottom);
233 switch (icd->current_fmt->fourcc) {
234 case V4L2_PIX_FMT_NV12:
235 case V4L2_PIX_FMT_NV21:
236 case V4L2_PIX_FMT_NV16:
237 case V4L2_PIX_FMT_NV61:
238 phys_addr_top += icd->user_width *
239 icd->user_height;
240 ceu_write(pcdev, CDACR, phys_addr_top);
241 if (pcdev->is_interlaced) {
242 phys_addr_bottom = phys_addr_top +
243 icd->user_width;
244 ceu_write(pcdev, CDBCR, phys_addr_bottom);
248 pcdev->active->state = VIDEOBUF_ACTIVE;
249 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
252 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
253 struct videobuf_buffer *vb,
254 enum v4l2_field field)
256 struct soc_camera_device *icd = vq->priv_data;
257 struct sh_mobile_ceu_buffer *buf;
258 int ret;
260 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
262 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
263 vb, vb->baddr, vb->bsize);
265 /* Added list head initialization on alloc */
266 WARN_ON(!list_empty(&vb->queue));
268 #ifdef DEBUG
269 /* This can be useful if you want to see if we actually fill
270 * the buffer with something */
271 memset((void *)vb->baddr, 0xaa, vb->bsize);
272 #endif
274 BUG_ON(NULL == icd->current_fmt);
276 if (buf->fmt != icd->current_fmt ||
277 vb->width != icd->user_width ||
278 vb->height != icd->user_height ||
279 vb->field != field) {
280 buf->fmt = icd->current_fmt;
281 vb->width = icd->user_width;
282 vb->height = icd->user_height;
283 vb->field = field;
284 vb->state = VIDEOBUF_NEEDS_INIT;
287 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
288 if (0 != vb->baddr && vb->bsize < vb->size) {
289 ret = -EINVAL;
290 goto out;
293 if (vb->state == VIDEOBUF_NEEDS_INIT) {
294 ret = videobuf_iolock(vq, vb, NULL);
295 if (ret)
296 goto fail;
297 vb->state = VIDEOBUF_PREPARED;
300 return 0;
301 fail:
302 free_buffer(vq, buf);
303 out:
304 return ret;
307 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
308 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
309 struct videobuf_buffer *vb)
311 struct soc_camera_device *icd = vq->priv_data;
312 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
313 struct sh_mobile_ceu_dev *pcdev = ici->priv;
315 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
316 vb, vb->baddr, vb->bsize);
318 vb->state = VIDEOBUF_QUEUED;
319 list_add_tail(&vb->queue, &pcdev->capture);
321 if (!pcdev->active) {
322 pcdev->active = vb;
323 sh_mobile_ceu_capture(pcdev);
327 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
328 struct videobuf_buffer *vb)
330 struct soc_camera_device *icd = vq->priv_data;
331 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
332 struct sh_mobile_ceu_dev *pcdev = ici->priv;
333 unsigned long flags;
335 spin_lock_irqsave(&pcdev->lock, flags);
337 if (pcdev->active == vb) {
338 /* disable capture (release DMA buffer), reset */
339 ceu_write(pcdev, CAPSR, 1 << 16);
340 pcdev->active = NULL;
343 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
344 !list_empty(&vb->queue)) {
345 vb->state = VIDEOBUF_ERROR;
346 list_del_init(&vb->queue);
349 spin_unlock_irqrestore(&pcdev->lock, flags);
351 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
354 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
355 .buf_setup = sh_mobile_ceu_videobuf_setup,
356 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
357 .buf_queue = sh_mobile_ceu_videobuf_queue,
358 .buf_release = sh_mobile_ceu_videobuf_release,
361 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
363 struct sh_mobile_ceu_dev *pcdev = data;
364 struct videobuf_buffer *vb;
365 unsigned long flags;
367 spin_lock_irqsave(&pcdev->lock, flags);
369 vb = pcdev->active;
370 if (!vb)
371 /* Stale interrupt from a released buffer */
372 goto out;
374 list_del_init(&vb->queue);
376 if (!list_empty(&pcdev->capture))
377 pcdev->active = list_entry(pcdev->capture.next,
378 struct videobuf_buffer, queue);
379 else
380 pcdev->active = NULL;
382 sh_mobile_ceu_capture(pcdev);
384 vb->state = VIDEOBUF_DONE;
385 do_gettimeofday(&vb->ts);
386 vb->field_count++;
387 wake_up(&vb->done);
389 out:
390 spin_unlock_irqrestore(&pcdev->lock, flags);
392 return IRQ_HANDLED;
395 /* Called with .video_lock held */
396 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
398 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
399 struct sh_mobile_ceu_dev *pcdev = ici->priv;
401 if (pcdev->icd)
402 return -EBUSY;
404 dev_info(icd->dev.parent,
405 "SuperH Mobile CEU driver attached to camera %d\n",
406 icd->devnum);
408 pm_runtime_get_sync(ici->v4l2_dev.dev);
410 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
411 while (ceu_read(pcdev, CSTSR) & 1)
412 msleep(1);
414 pcdev->icd = icd;
416 return 0;
419 /* Called with .video_lock held */
420 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
422 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
423 struct sh_mobile_ceu_dev *pcdev = ici->priv;
424 unsigned long flags;
426 BUG_ON(icd != pcdev->icd);
428 /* disable capture, disable interrupts */
429 ceu_write(pcdev, CEIER, 0);
430 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
432 /* make sure active buffer is canceled */
433 spin_lock_irqsave(&pcdev->lock, flags);
434 if (pcdev->active) {
435 list_del(&pcdev->active->queue);
436 pcdev->active->state = VIDEOBUF_ERROR;
437 wake_up_all(&pcdev->active->done);
438 pcdev->active = NULL;
440 spin_unlock_irqrestore(&pcdev->lock, flags);
442 pm_runtime_put_sync(ici->v4l2_dev.dev);
444 dev_info(icd->dev.parent,
445 "SuperH Mobile CEU driver detached from camera %d\n",
446 icd->devnum);
448 pcdev->icd = NULL;
452 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
453 * in SH7722 Hardware Manual
455 static unsigned int size_dst(unsigned int src, unsigned int scale)
457 unsigned int mant_pre = scale >> 12;
458 if (!src || !scale)
459 return src;
460 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
461 mant_pre * 4096 / scale + 1;
464 static u16 calc_scale(unsigned int src, unsigned int *dst)
466 u16 scale;
468 if (src == *dst)
469 return 0;
471 scale = (src * 4096 / *dst) & ~7;
473 while (scale > 4096 && size_dst(src, scale) < *dst)
474 scale -= 8;
476 *dst = size_dst(src, scale);
478 return scale;
481 /* rect is guaranteed to not exceed the scaled camera rectangle */
482 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
483 unsigned int out_width,
484 unsigned int out_height)
486 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
487 struct sh_mobile_ceu_cam *cam = icd->host_priv;
488 struct v4l2_rect *rect = &cam->ceu_rect;
489 struct sh_mobile_ceu_dev *pcdev = ici->priv;
490 unsigned int height, width, cdwdr_width, in_width, in_height;
491 unsigned int left_offset, top_offset;
492 u32 camor;
494 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
495 rect->width, rect->height, rect->left, rect->top);
497 left_offset = rect->left;
498 top_offset = rect->top;
500 if (pcdev->image_mode) {
501 in_width = rect->width;
502 if (!pcdev->is_16bit) {
503 in_width *= 2;
504 left_offset *= 2;
506 width = cdwdr_width = out_width;
507 } else {
508 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
510 width = out_width * w_factor / 2;
512 if (!pcdev->is_16bit)
513 w_factor *= 2;
515 in_width = rect->width * w_factor / 2;
516 left_offset = left_offset * w_factor / 2;
518 cdwdr_width = width * 2;
521 height = out_height;
522 in_height = rect->height;
523 if (pcdev->is_interlaced) {
524 height /= 2;
525 in_height /= 2;
526 top_offset /= 2;
527 cdwdr_width *= 2;
530 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
531 camor = left_offset | (top_offset << 16);
533 dev_geo(icd->dev.parent,
534 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
535 (in_height << 16) | in_width, (height << 16) | width,
536 cdwdr_width);
538 ceu_write(pcdev, CAMOR, camor);
539 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
540 ceu_write(pcdev, CFSZR, (height << 16) | width);
541 ceu_write(pcdev, CDWDR, cdwdr_width);
544 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
546 u32 capsr = ceu_read(pcdev, CAPSR);
547 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
548 return capsr;
551 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
553 unsigned long timeout = jiffies + 10 * HZ;
556 * Wait until the end of the current frame. It can take a long time,
557 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
559 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
560 msleep(1);
562 if (time_after(jiffies, timeout)) {
563 dev_err(pcdev->ici.v4l2_dev.dev,
564 "Timeout waiting for frame end! Interface problem?\n");
565 return;
568 /* Wait until reset clears, this shall not hang... */
569 while (ceu_read(pcdev, CAPSR) & (1 << 16))
570 udelay(10);
572 /* Anything to restore? */
573 if (capsr & ~(1 << 16))
574 ceu_write(pcdev, CAPSR, capsr);
577 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
578 __u32 pixfmt)
580 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
581 struct sh_mobile_ceu_dev *pcdev = ici->priv;
582 int ret;
583 unsigned long camera_flags, common_flags, value;
584 int yuv_lineskip;
585 struct sh_mobile_ceu_cam *cam = icd->host_priv;
586 u32 capsr = capture_save_reset(pcdev);
588 camera_flags = icd->ops->query_bus_param(icd);
589 common_flags = soc_camera_bus_param_compatible(camera_flags,
590 make_bus_param(pcdev));
591 if (!common_flags)
592 return -EINVAL;
594 ret = icd->ops->set_bus_param(icd, common_flags);
595 if (ret < 0)
596 return ret;
598 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
599 case SOCAM_DATAWIDTH_8:
600 pcdev->is_16bit = 0;
601 break;
602 case SOCAM_DATAWIDTH_16:
603 pcdev->is_16bit = 1;
604 break;
605 default:
606 return -EINVAL;
609 ceu_write(pcdev, CRCNTR, 0);
610 ceu_write(pcdev, CRCMPR, 0);
612 value = 0x00000010; /* data fetch by default */
613 yuv_lineskip = 0;
615 switch (icd->current_fmt->fourcc) {
616 case V4L2_PIX_FMT_NV12:
617 case V4L2_PIX_FMT_NV21:
618 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
619 /* fall-through */
620 case V4L2_PIX_FMT_NV16:
621 case V4L2_PIX_FMT_NV61:
622 switch (cam->camera_fmt->fourcc) {
623 case V4L2_PIX_FMT_UYVY:
624 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
625 break;
626 case V4L2_PIX_FMT_VYUY:
627 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
628 break;
629 case V4L2_PIX_FMT_YUYV:
630 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
631 break;
632 case V4L2_PIX_FMT_YVYU:
633 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
634 break;
635 default:
636 BUG();
640 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
641 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
642 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
644 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
645 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
646 value |= pcdev->is_16bit ? 1 << 12 : 0;
647 ceu_write(pcdev, CAMCR, value);
649 ceu_write(pcdev, CAPCR, 0x00300000);
650 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
652 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
653 mdelay(1);
655 ceu_write(pcdev, CFLCR, pcdev->cflcr);
657 /* A few words about byte order (observed in Big Endian mode)
659 * In data fetch mode bytes are received in chunks of 8 bytes.
660 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
662 * The data is however by default written to memory in reverse order:
663 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
665 * The lowest three bits of CDOCR allows us to do swapping,
666 * using 7 we swap the data bytes to match the incoming order:
667 * D0, D1, D2, D3, D4, D5, D6, D7
669 value = 0x00000017;
670 if (yuv_lineskip)
671 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
673 ceu_write(pcdev, CDOCR, value);
674 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
676 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
677 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
678 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
679 icd->user_width, icd->user_height);
681 capture_restore(pcdev, capsr);
683 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
684 return 0;
687 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
689 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
690 struct sh_mobile_ceu_dev *pcdev = ici->priv;
691 unsigned long camera_flags, common_flags;
693 camera_flags = icd->ops->query_bus_param(icd);
694 common_flags = soc_camera_bus_param_compatible(camera_flags,
695 make_bus_param(pcdev));
696 if (!common_flags)
697 return -EINVAL;
699 return 0;
702 static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
704 .name = "NV12",
705 .depth = 12,
706 .fourcc = V4L2_PIX_FMT_NV12,
707 .colorspace = V4L2_COLORSPACE_JPEG,
710 .name = "NV21",
711 .depth = 12,
712 .fourcc = V4L2_PIX_FMT_NV21,
713 .colorspace = V4L2_COLORSPACE_JPEG,
716 .name = "NV16",
717 .depth = 16,
718 .fourcc = V4L2_PIX_FMT_NV16,
719 .colorspace = V4L2_COLORSPACE_JPEG,
722 .name = "NV61",
723 .depth = 16,
724 .fourcc = V4L2_PIX_FMT_NV61,
725 .colorspace = V4L2_COLORSPACE_JPEG,
729 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
730 struct soc_camera_format_xlate *xlate)
732 struct device *dev = icd->dev.parent;
733 int ret, k, n;
734 int formats = 0;
735 struct sh_mobile_ceu_cam *cam;
737 ret = sh_mobile_ceu_try_bus_param(icd);
738 if (ret < 0)
739 return 0;
741 if (!icd->host_priv) {
742 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
743 if (!cam)
744 return -ENOMEM;
746 icd->host_priv = cam;
747 } else {
748 cam = icd->host_priv;
751 /* Beginning of a pass */
752 if (!idx)
753 cam->extra_fmt = NULL;
755 switch (icd->formats[idx].fourcc) {
756 case V4L2_PIX_FMT_UYVY:
757 case V4L2_PIX_FMT_VYUY:
758 case V4L2_PIX_FMT_YUYV:
759 case V4L2_PIX_FMT_YVYU:
760 if (cam->extra_fmt)
761 goto add_single_format;
764 * Our case is simple so far: for any of the above four camera
765 * formats we add all our four synthesized NV* formats, so,
766 * just marking the device with a single flag suffices. If
767 * the format generation rules are more complex, you would have
768 * to actually hang your already added / counted formats onto
769 * the host_priv pointer and check whether the format you're
770 * going to add now is already there.
772 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
774 n = ARRAY_SIZE(sh_mobile_ceu_formats);
775 formats += n;
776 for (k = 0; xlate && k < n; k++) {
777 xlate->host_fmt = &sh_mobile_ceu_formats[k];
778 xlate->cam_fmt = icd->formats + idx;
779 xlate->buswidth = icd->formats[idx].depth;
780 xlate++;
781 dev_dbg(dev, "Providing format %s using %s\n",
782 sh_mobile_ceu_formats[k].name,
783 icd->formats[idx].name);
785 default:
786 add_single_format:
787 /* Generic pass-through */
788 formats++;
789 if (xlate) {
790 xlate->host_fmt = icd->formats + idx;
791 xlate->cam_fmt = icd->formats + idx;
792 xlate->buswidth = icd->formats[idx].depth;
793 xlate++;
794 dev_dbg(dev,
795 "Providing format %s in pass-through mode\n",
796 icd->formats[idx].name);
800 return formats;
803 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
805 kfree(icd->host_priv);
806 icd->host_priv = NULL;
809 /* Check if any dimension of r1 is smaller than respective one of r2 */
810 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
812 return r1->width < r2->width || r1->height < r2->height;
815 /* Check if r1 fails to cover r2 */
816 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
818 return r1->left > r2->left || r1->top > r2->top ||
819 r1->left + r1->width < r2->left + r2->width ||
820 r1->top + r1->height < r2->top + r2->height;
823 static unsigned int scale_down(unsigned int size, unsigned int scale)
825 return (size * 4096 + scale / 2) / scale;
828 static unsigned int scale_up(unsigned int size, unsigned int scale)
830 return (size * scale + 2048) / 4096;
833 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
835 return (input * 4096 + output / 2) / output;
838 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
840 struct v4l2_crop crop;
841 struct v4l2_cropcap cap;
842 int ret;
844 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
846 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
847 if (!ret) {
848 *rect = crop.c;
849 return ret;
852 /* Camera driver doesn't support .g_crop(), assume default rectangle */
853 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
855 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
856 if (ret < 0)
857 return ret;
859 *rect = cap.defrect;
861 return ret;
865 * The common for both scaling and cropping iterative approach is:
866 * 1. try if the client can produce exactly what requested by the user
867 * 2. if (1) failed, try to double the client image until we get one big enough
868 * 3. if (2) failed, try to request the maximum image
870 static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
871 struct v4l2_crop *cam_crop)
873 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
874 struct device *dev = sd->v4l2_dev->dev;
875 struct v4l2_cropcap cap;
876 int ret;
877 unsigned int width, height;
879 v4l2_subdev_call(sd, video, s_crop, crop);
880 ret = client_g_rect(sd, cam_rect);
881 if (ret < 0)
882 return ret;
885 * Now cam_crop contains the current camera input rectangle, and it must
886 * be within camera cropcap bounds
888 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
889 /* Even if camera S_CROP failed, but camera rectangle matches */
890 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
891 rect->width, rect->height, rect->left, rect->top);
892 return 0;
895 /* Try to fix cropping, that camera hasn't managed to set */
896 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
897 cam_rect->width, cam_rect->height,
898 cam_rect->left, cam_rect->top,
899 rect->width, rect->height, rect->left, rect->top);
901 /* We need sensor maximum rectangle */
902 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
903 if (ret < 0)
904 return ret;
906 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
907 cap.bounds.width);
908 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
909 cap.bounds.height);
912 * Popular special case - some cameras can only handle fixed sizes like
913 * QVGA, VGA,... Take care to avoid infinite loop.
915 width = max(cam_rect->width, 2);
916 height = max(cam_rect->height, 2);
918 while (!ret && (is_smaller(cam_rect, rect) ||
919 is_inside(cam_rect, rect)) &&
920 (cap.bounds.width > width || cap.bounds.height > height)) {
922 width *= 2;
923 height *= 2;
925 cam_rect->width = width;
926 cam_rect->height = height;
929 * We do not know what capabilities the camera has to set up
930 * left and top borders. We could try to be smarter in iterating
931 * them, e.g., if camera current left is to the right of the
932 * target left, set it to the middle point between the current
933 * left and minimum left. But that would add too much
934 * complexity: we would have to iterate each border separately.
936 if (cam_rect->left > rect->left)
937 cam_rect->left = cap.bounds.left;
939 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
940 cam_rect->width = rect->left + rect->width -
941 cam_rect->left;
943 if (cam_rect->top > rect->top)
944 cam_rect->top = cap.bounds.top;
946 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
947 cam_rect->height = rect->top + rect->height -
948 cam_rect->top;
950 v4l2_subdev_call(sd, video, s_crop, cam_crop);
951 ret = client_g_rect(sd, cam_rect);
952 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
953 cam_rect->width, cam_rect->height,
954 cam_rect->left, cam_rect->top);
957 /* S_CROP must not modify the rectangle */
958 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
960 * The camera failed to configure a suitable cropping,
961 * we cannot use the current rectangle, set to max
963 *cam_rect = cap.bounds;
964 v4l2_subdev_call(sd, video, s_crop, cam_crop);
965 ret = client_g_rect(sd, cam_rect);
966 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
967 cam_rect->width, cam_rect->height,
968 cam_rect->left, cam_rect->top);
971 return ret;
974 static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
975 unsigned int *scale_h, unsigned int *scale_v)
977 struct v4l2_format f;
978 int ret;
980 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
982 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
983 if (ret < 0)
984 return ret;
986 *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
987 *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
989 return 0;
992 static int get_camera_subwin(struct soc_camera_device *icd,
993 struct v4l2_rect *cam_subrect,
994 unsigned int cam_hscale, unsigned int cam_vscale)
996 struct sh_mobile_ceu_cam *cam = icd->host_priv;
997 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
999 if (!ceu_rect->width) {
1000 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1001 struct device *dev = icd->dev.parent;
1002 struct v4l2_format f;
1003 struct v4l2_pix_format *pix = &f.fmt.pix;
1004 int ret;
1005 /* First time */
1007 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1010 if (ret < 0)
1011 return ret;
1013 dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
1015 if (pix->width > 2560) {
1016 ceu_rect->width = 2560;
1017 ceu_rect->left = (pix->width - 2560) / 2;
1018 } else {
1019 ceu_rect->width = pix->width;
1020 ceu_rect->left = 0;
1023 if (pix->height > 1920) {
1024 ceu_rect->height = 1920;
1025 ceu_rect->top = (pix->height - 1920) / 2;
1026 } else {
1027 ceu_rect->height = pix->height;
1028 ceu_rect->top = 0;
1031 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1032 ceu_rect->width, ceu_rect->height,
1033 ceu_rect->left, ceu_rect->top);
1036 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1037 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1038 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1039 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1041 return 0;
1044 static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
1045 bool ceu_can_scale)
1047 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1048 struct device *dev = icd->dev.parent;
1049 struct v4l2_pix_format *pix = &f->fmt.pix;
1050 unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
1051 unsigned int max_width, max_height;
1052 struct v4l2_cropcap cap;
1053 int ret;
1055 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1057 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1058 if (ret < 0)
1059 return ret;
1061 max_width = min(cap.bounds.width, 2560);
1062 max_height = min(cap.bounds.height, 1920);
1064 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1065 if (ret < 0)
1066 return ret;
1068 dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
1070 if ((width == pix->width && height == pix->height) || !ceu_can_scale)
1071 return 0;
1073 /* Camera set a format, but geometry is not precise, try to improve */
1074 tmp_w = pix->width;
1075 tmp_h = pix->height;
1077 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1078 while ((width > tmp_w || height > tmp_h) &&
1079 tmp_w < max_width && tmp_h < max_height) {
1080 tmp_w = min(2 * tmp_w, max_width);
1081 tmp_h = min(2 * tmp_h, max_height);
1082 pix->width = tmp_w;
1083 pix->height = tmp_h;
1084 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1085 dev_geo(dev, "Camera scaled to %ux%u\n",
1086 pix->width, pix->height);
1087 if (ret < 0) {
1088 /* This shouldn't happen */
1089 dev_err(dev, "Client failed to set format: %d\n", ret);
1090 return ret;
1094 return 0;
1098 * @rect - camera cropped rectangle
1099 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1100 * @ceu_rect - on output calculated CEU crop rectangle
1102 static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1103 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
1104 struct v4l2_format *f, bool ceu_can_scale)
1106 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1107 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1108 struct device *dev = icd->dev.parent;
1109 struct v4l2_format f_tmp = *f;
1110 struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
1111 unsigned int scale_h, scale_v;
1112 int ret;
1114 /* 5. Apply iterative camera S_FMT for camera user window. */
1115 ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
1116 if (ret < 0)
1117 return ret;
1119 dev_geo(dev, "5: camera scaled to %ux%u\n",
1120 pix_tmp->width, pix_tmp->height);
1122 /* 6. Retrieve camera output window (g_fmt) */
1124 /* unneeded - it is already in "f_tmp" */
1126 /* 7. Calculate new camera scales. */
1127 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1128 if (ret < 0)
1129 return ret;
1131 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1133 cam->cam_width = pix_tmp->width;
1134 cam->cam_height = pix_tmp->height;
1135 f->fmt.pix.width = pix_tmp->width;
1136 f->fmt.pix.height = pix_tmp->height;
1139 * 8. Calculate new CEU crop - apply camera scales to previously
1140 * calculated "effective" crop.
1142 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1143 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1144 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1145 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1147 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1148 ceu_rect->width, ceu_rect->height,
1149 ceu_rect->left, ceu_rect->top);
1151 return 0;
1154 /* Get combined scales */
1155 static int get_scales(struct soc_camera_device *icd,
1156 unsigned int *scale_h, unsigned int *scale_v)
1158 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1159 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1160 struct v4l2_crop cam_crop;
1161 unsigned int width_in, height_in;
1162 int ret;
1164 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1166 ret = client_g_rect(sd, &cam_crop.c);
1167 if (ret < 0)
1168 return ret;
1170 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1171 if (ret < 0)
1172 return ret;
1174 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1175 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1177 *scale_h = calc_generic_scale(width_in, icd->user_width);
1178 *scale_v = calc_generic_scale(height_in, icd->user_height);
1180 return 0;
1184 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1185 * framerate by always requesting the maximum image from the client. See
1186 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1187 * scaling and cropping algorithms and for the meaning of referenced here steps.
1189 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1190 struct v4l2_crop *a)
1192 struct v4l2_rect *rect = &a->c;
1193 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1194 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1195 struct v4l2_crop cam_crop;
1196 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1197 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
1198 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1199 struct device *dev = icd->dev.parent;
1200 struct v4l2_format f;
1201 struct v4l2_pix_format *pix = &f.fmt.pix;
1202 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1203 out_width, out_height;
1204 u32 capsr, cflcr;
1205 int ret;
1207 /* 1. Calculate current combined scales. */
1208 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1209 if (ret < 0)
1210 return ret;
1212 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1214 /* 2. Apply iterative camera S_CROP for new input window. */
1215 ret = client_s_crop(sd, a, &cam_crop);
1216 if (ret < 0)
1217 return ret;
1219 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1220 cam_rect->width, cam_rect->height,
1221 cam_rect->left, cam_rect->top);
1223 /* On success cam_crop contains current camera crop */
1226 * 3. If old combined scales applied to new crop produce an impossible
1227 * user window, adjust scales to produce nearest possible window.
1229 out_width = scale_down(rect->width, scale_comb_h);
1230 out_height = scale_down(rect->height, scale_comb_v);
1232 if (out_width > 2560)
1233 out_width = 2560;
1234 else if (out_width < 2)
1235 out_width = 2;
1237 if (out_height > 1920)
1238 out_height = 1920;
1239 else if (out_height < 4)
1240 out_height = 4;
1242 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1244 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1247 * 5. Using actual input window and calculated combined scales calculate
1248 * camera target output window.
1250 pix->width = scale_down(cam_rect->width, scale_comb_h);
1251 pix->height = scale_down(cam_rect->height, scale_comb_v);
1253 dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
1255 /* 6. - 9. */
1256 pix->pixelformat = cam->camera_fmt->fourcc;
1257 pix->colorspace = cam->camera_fmt->colorspace;
1259 capsr = capture_save_reset(pcdev);
1260 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1262 /* Make relative to camera rectangle */
1263 rect->left -= cam_rect->left;
1264 rect->top -= cam_rect->top;
1266 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1268 ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
1269 pcdev->image_mode && !pcdev->is_interlaced);
1271 dev_geo(dev, "6-9: %d\n", ret);
1273 /* 10. Use CEU cropping to crop to the new window. */
1274 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1276 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1277 ceu_rect->width, ceu_rect->height,
1278 ceu_rect->left, ceu_rect->top);
1281 * 11. Calculate CEU scales from camera scales from results of (10) and
1282 * user window from (3)
1284 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1285 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
1287 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1289 /* 12. Apply CEU scales. */
1290 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1291 if (cflcr != pcdev->cflcr) {
1292 pcdev->cflcr = cflcr;
1293 ceu_write(pcdev, CFLCR, cflcr);
1296 /* Restore capture */
1297 if (pcdev->active)
1298 capsr |= 1;
1299 capture_restore(pcdev, capsr);
1301 icd->user_width = out_width;
1302 icd->user_height = out_height;
1304 /* Even if only camera cropping succeeded */
1305 return ret;
1308 /* Similar to set_crop multistage iterative algorithm */
1309 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1310 struct v4l2_format *f)
1312 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1313 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1314 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1315 struct v4l2_pix_format *pix = &f->fmt.pix;
1316 struct v4l2_format cam_f = *f;
1317 struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
1318 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1319 struct device *dev = icd->dev.parent;
1320 __u32 pixfmt = pix->pixelformat;
1321 const struct soc_camera_format_xlate *xlate;
1322 struct v4l2_crop cam_crop;
1323 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1324 unsigned int scale_cam_h, scale_cam_v;
1325 u16 scale_v, scale_h;
1326 int ret;
1327 bool is_interlaced, image_mode;
1329 switch (pix->field) {
1330 case V4L2_FIELD_INTERLACED:
1331 is_interlaced = true;
1332 break;
1333 case V4L2_FIELD_ANY:
1334 default:
1335 pix->field = V4L2_FIELD_NONE;
1336 /* fall-through */
1337 case V4L2_FIELD_NONE:
1338 is_interlaced = false;
1339 break;
1342 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1343 if (!xlate) {
1344 dev_warn(dev, "Format %x not found\n", pixfmt);
1345 return -EINVAL;
1348 /* 1. Calculate current camera scales. */
1349 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1351 ret = client_g_rect(sd, cam_rect);
1352 if (ret < 0)
1353 return ret;
1355 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1356 if (ret < 0)
1357 return ret;
1359 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1362 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1363 * scaled back at current camera scales onto input window.
1365 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1366 if (ret < 0)
1367 return ret;
1369 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1370 cam_subrect.width, cam_subrect.height,
1371 cam_subrect.left, cam_subrect.top);
1374 * 3. Calculate new combined scales from "effective" input window to
1375 * requested user window.
1377 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1378 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1380 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1383 * 4. Calculate camera output window by applying combined scales to real
1384 * input window.
1386 cam_pix->width = scale_down(cam_rect->width, scale_h);
1387 cam_pix->height = scale_down(cam_rect->height, scale_v);
1388 cam_pix->pixelformat = xlate->cam_fmt->fourcc;
1390 switch (pixfmt) {
1391 case V4L2_PIX_FMT_NV12:
1392 case V4L2_PIX_FMT_NV21:
1393 case V4L2_PIX_FMT_NV16:
1394 case V4L2_PIX_FMT_NV61:
1395 image_mode = true;
1396 break;
1397 default:
1398 image_mode = false;
1401 dev_geo(dev, "4: camera output %ux%u\n",
1402 cam_pix->width, cam_pix->height);
1404 /* 5. - 9. */
1405 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
1406 image_mode && !is_interlaced);
1408 dev_geo(dev, "5-9: client scale %d\n", ret);
1410 /* Done with the camera. Now see if we can improve the result */
1412 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1413 ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
1414 if (ret < 0)
1415 return ret;
1417 /* 10. Use CEU scaling to scale to the requested user window. */
1419 /* We cannot scale up */
1420 if (pix->width > cam_pix->width)
1421 pix->width = cam_pix->width;
1422 if (pix->width > ceu_rect.width)
1423 pix->width = ceu_rect.width;
1425 if (pix->height > cam_pix->height)
1426 pix->height = cam_pix->height;
1427 if (pix->height > ceu_rect.height)
1428 pix->height = ceu_rect.height;
1430 /* Let's rock: scale pix->{width x height} down to width x height */
1431 scale_h = calc_scale(ceu_rect.width, &pix->width);
1432 scale_v = calc_scale(ceu_rect.height, &pix->height);
1434 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1435 ceu_rect.width, scale_h, pix->width,
1436 ceu_rect.height, scale_v, pix->height);
1438 pcdev->cflcr = scale_h | (scale_v << 16);
1440 icd->buswidth = xlate->buswidth;
1441 icd->current_fmt = xlate->host_fmt;
1442 cam->camera_fmt = xlate->cam_fmt;
1443 cam->ceu_rect = ceu_rect;
1445 pcdev->is_interlaced = is_interlaced;
1446 pcdev->image_mode = image_mode;
1448 return 0;
1451 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1452 struct v4l2_format *f)
1454 const struct soc_camera_format_xlate *xlate;
1455 struct v4l2_pix_format *pix = &f->fmt.pix;
1456 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1457 __u32 pixfmt = pix->pixelformat;
1458 int width, height;
1459 int ret;
1461 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1462 if (!xlate) {
1463 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
1464 return -EINVAL;
1467 /* FIXME: calculate using depth and bus width */
1469 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1470 &pix->height, 4, 1920, 2, 0);
1472 width = pix->width;
1473 height = pix->height;
1475 pix->bytesperline = pix->width *
1476 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1477 pix->sizeimage = pix->height * pix->bytesperline;
1479 pix->pixelformat = xlate->cam_fmt->fourcc;
1481 /* limit to sensor capabilities */
1482 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1483 pix->pixelformat = pixfmt;
1484 if (ret < 0)
1485 return ret;
1487 switch (pixfmt) {
1488 case V4L2_PIX_FMT_NV12:
1489 case V4L2_PIX_FMT_NV21:
1490 case V4L2_PIX_FMT_NV16:
1491 case V4L2_PIX_FMT_NV61:
1492 /* FIXME: check against rect_max after converting soc-camera */
1493 /* We can scale precisely, need a bigger image from camera */
1494 if (pix->width < width || pix->height < height) {
1495 int tmp_w = pix->width, tmp_h = pix->height;
1496 pix->width = 2560;
1497 pix->height = 1920;
1498 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1499 if (ret < 0) {
1500 /* Shouldn't actually happen... */
1501 dev_err(icd->dev.parent,
1502 "FIXME: try_fmt() returned %d\n", ret);
1503 pix->width = tmp_w;
1504 pix->height = tmp_h;
1507 if (pix->width > width)
1508 pix->width = width;
1509 if (pix->height > height)
1510 pix->height = height;
1513 return ret;
1516 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1517 struct v4l2_requestbuffers *p)
1519 int i;
1521 /* This is for locking debugging only. I removed spinlocks and now I
1522 * check whether .prepare is ever called on a linked buffer, or whether
1523 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1524 * it hadn't triggered */
1525 for (i = 0; i < p->count; i++) {
1526 struct sh_mobile_ceu_buffer *buf;
1528 buf = container_of(icf->vb_vidq.bufs[i],
1529 struct sh_mobile_ceu_buffer, vb);
1530 INIT_LIST_HEAD(&buf->vb.queue);
1533 return 0;
1536 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1538 struct soc_camera_file *icf = file->private_data;
1539 struct sh_mobile_ceu_buffer *buf;
1541 buf = list_entry(icf->vb_vidq.stream.next,
1542 struct sh_mobile_ceu_buffer, vb.stream);
1544 poll_wait(file, &buf->vb.done, pt);
1546 if (buf->vb.state == VIDEOBUF_DONE ||
1547 buf->vb.state == VIDEOBUF_ERROR)
1548 return POLLIN|POLLRDNORM;
1550 return 0;
1553 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1554 struct v4l2_capability *cap)
1556 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1557 cap->version = KERNEL_VERSION(0, 0, 5);
1558 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1559 return 0;
1562 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1563 struct soc_camera_device *icd)
1565 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1566 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1568 videobuf_queue_dma_contig_init(q,
1569 &sh_mobile_ceu_videobuf_ops,
1570 icd->dev.parent, &pcdev->lock,
1571 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1572 pcdev->is_interlaced ?
1573 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
1574 sizeof(struct sh_mobile_ceu_buffer),
1575 icd);
1578 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1579 struct v4l2_control *ctrl)
1581 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1582 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1583 u32 val;
1585 switch (ctrl->id) {
1586 case V4L2_CID_SHARPNESS:
1587 val = ceu_read(pcdev, CLFCR);
1588 ctrl->value = val ^ 1;
1589 return 0;
1591 return -ENOIOCTLCMD;
1594 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1595 struct v4l2_control *ctrl)
1597 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1598 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1600 switch (ctrl->id) {
1601 case V4L2_CID_SHARPNESS:
1602 switch (icd->current_fmt->fourcc) {
1603 case V4L2_PIX_FMT_NV12:
1604 case V4L2_PIX_FMT_NV21:
1605 case V4L2_PIX_FMT_NV16:
1606 case V4L2_PIX_FMT_NV61:
1607 ceu_write(pcdev, CLFCR, !ctrl->value);
1608 return 0;
1610 return -EINVAL;
1612 return -ENOIOCTLCMD;
1615 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1617 .id = V4L2_CID_SHARPNESS,
1618 .type = V4L2_CTRL_TYPE_BOOLEAN,
1619 .name = "Low-pass filter",
1620 .minimum = 0,
1621 .maximum = 1,
1622 .step = 1,
1623 .default_value = 0,
1627 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1628 .owner = THIS_MODULE,
1629 .add = sh_mobile_ceu_add_device,
1630 .remove = sh_mobile_ceu_remove_device,
1631 .get_formats = sh_mobile_ceu_get_formats,
1632 .put_formats = sh_mobile_ceu_put_formats,
1633 .set_crop = sh_mobile_ceu_set_crop,
1634 .set_fmt = sh_mobile_ceu_set_fmt,
1635 .try_fmt = sh_mobile_ceu_try_fmt,
1636 .set_ctrl = sh_mobile_ceu_set_ctrl,
1637 .get_ctrl = sh_mobile_ceu_get_ctrl,
1638 .reqbufs = sh_mobile_ceu_reqbufs,
1639 .poll = sh_mobile_ceu_poll,
1640 .querycap = sh_mobile_ceu_querycap,
1641 .set_bus_param = sh_mobile_ceu_set_bus_param,
1642 .init_videobuf = sh_mobile_ceu_init_videobuf,
1643 .controls = sh_mobile_ceu_controls,
1644 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
1647 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
1649 struct sh_mobile_ceu_dev *pcdev;
1650 struct resource *res;
1651 void __iomem *base;
1652 unsigned int irq;
1653 int err = 0;
1655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1656 irq = platform_get_irq(pdev, 0);
1657 if (!res || !irq) {
1658 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1659 err = -ENODEV;
1660 goto exit;
1663 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1664 if (!pcdev) {
1665 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1666 err = -ENOMEM;
1667 goto exit;
1670 INIT_LIST_HEAD(&pcdev->capture);
1671 spin_lock_init(&pcdev->lock);
1673 pcdev->pdata = pdev->dev.platform_data;
1674 if (!pcdev->pdata) {
1675 err = -EINVAL;
1676 dev_err(&pdev->dev, "CEU platform data not set.\n");
1677 goto exit_kfree;
1680 base = ioremap_nocache(res->start, resource_size(res));
1681 if (!base) {
1682 err = -ENXIO;
1683 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1684 goto exit_kfree;
1687 pcdev->irq = irq;
1688 pcdev->base = base;
1689 pcdev->video_limit = 0; /* only enabled if second resource exists */
1691 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1692 if (res) {
1693 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1694 res->start,
1695 resource_size(res),
1696 DMA_MEMORY_MAP |
1697 DMA_MEMORY_EXCLUSIVE);
1698 if (!err) {
1699 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1700 err = -ENXIO;
1701 goto exit_iounmap;
1704 pcdev->video_limit = resource_size(res);
1707 /* request irq */
1708 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
1709 dev_name(&pdev->dev), pcdev);
1710 if (err) {
1711 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1712 goto exit_release_mem;
1715 pm_suspend_ignore_children(&pdev->dev, true);
1716 pm_runtime_enable(&pdev->dev);
1717 pm_runtime_resume(&pdev->dev);
1719 pcdev->ici.priv = pcdev;
1720 pcdev->ici.v4l2_dev.dev = &pdev->dev;
1721 pcdev->ici.nr = pdev->id;
1722 pcdev->ici.drv_name = dev_name(&pdev->dev);
1723 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1725 err = soc_camera_host_register(&pcdev->ici);
1726 if (err)
1727 goto exit_free_clk;
1729 return 0;
1731 exit_free_clk:
1732 pm_runtime_disable(&pdev->dev);
1733 free_irq(pcdev->irq, pcdev);
1734 exit_release_mem:
1735 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1736 dma_release_declared_memory(&pdev->dev);
1737 exit_iounmap:
1738 iounmap(base);
1739 exit_kfree:
1740 kfree(pcdev);
1741 exit:
1742 return err;
1745 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
1747 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1748 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1749 struct sh_mobile_ceu_dev, ici);
1751 soc_camera_host_unregister(soc_host);
1752 pm_runtime_disable(&pdev->dev);
1753 free_irq(pcdev->irq, pcdev);
1754 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1755 dma_release_declared_memory(&pdev->dev);
1756 iounmap(pcdev->base);
1757 kfree(pcdev);
1758 return 0;
1761 static int sh_mobile_ceu_runtime_nop(struct device *dev)
1763 /* Runtime PM callback shared between ->runtime_suspend()
1764 * and ->runtime_resume(). Simply returns success.
1766 * This driver re-initializes all registers after
1767 * pm_runtime_get_sync() anyway so there is no need
1768 * to save and restore registers here.
1770 return 0;
1773 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1774 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1775 .runtime_resume = sh_mobile_ceu_runtime_nop,
1778 static struct platform_driver sh_mobile_ceu_driver = {
1779 .driver = {
1780 .name = "sh_mobile_ceu",
1781 .pm = &sh_mobile_ceu_dev_pm_ops,
1783 .probe = sh_mobile_ceu_probe,
1784 .remove = __exit_p(sh_mobile_ceu_remove),
1787 static int __init sh_mobile_ceu_init(void)
1789 return platform_driver_register(&sh_mobile_ceu_driver);
1792 static void __exit sh_mobile_ceu_exit(void)
1794 platform_driver_unregister(&sh_mobile_ceu_driver);
1797 module_init(sh_mobile_ceu_init);
1798 module_exit(sh_mobile_ceu_exit);
1800 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1801 MODULE_AUTHOR("Magnus Damm");
1802 MODULE_LICENSE("GPL");
1803 MODULE_ALIAS("platform:sh_mobile_ceu");