blob: 5ab7c5aefd62d28ebf973d588e32964014ea729c [file] [log] [blame]
Magnus Damm0d3244d2008-07-16 22:59:28 -03001/*
2 * V4L2 Driver for SuperH Mobile CEU interface
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
7 *
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 *
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.
15 */
16
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>
Magnus Damm0d3244d2008-07-16 22:59:28 -030032#include <linux/videodev2.h>
Magnus Damm6d1386c2009-08-14 10:49:17 +000033#include <linux/pm_runtime.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030034
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>
40
41/* register offsets for sh7722 / sh7723 */
42
Magnus Dammdd542032008-10-16 19:50:22 -030043#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 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030076
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030077#undef DEBUG_GEOMETRY
78#ifdef DEBUG_GEOMETRY
79#define dev_geo dev_info
80#else
81#define dev_geo dev_dbg
82#endif
83
Magnus Damm0d3244d2008-07-16 22:59:28 -030084/* per video frame buffer */
85struct sh_mobile_ceu_buffer {
86 struct videobuf_buffer vb; /* v4l buffer must be first */
87 const struct soc_camera_data_format *fmt;
88};
89
90struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030091 struct soc_camera_host ici;
92 struct soc_camera_device *icd;
93
94 unsigned int irq;
95 void __iomem *base;
96 unsigned long video_limit;
97
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -030098 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -030099 spinlock_t lock;
100 struct list_head capture;
101 struct videobuf_buffer *active;
102
103 struct sh_mobile_ceu_info *pdata;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300104
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300105 u32 cflcr;
106
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300107 unsigned int is_interlaced:1;
108 unsigned int image_mode:1;
109 unsigned int is_16bit:1;
110};
111
112struct sh_mobile_ceu_cam {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300113 struct v4l2_rect ceu_rect;
114 unsigned int cam_width;
115 unsigned int cam_height;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300116 const struct soc_camera_data_format *extra_fmt;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300117 const struct soc_camera_data_format *camera_fmt;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300118};
119
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300120static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
121{
122 unsigned long flags;
123
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;
131
132 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
133 flags |= SOCAM_DATAWIDTH_8;
134
135 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
136 flags |= SOCAM_DATAWIDTH_16;
137
138 if (flags & SOCAM_DATAWIDTH_MASK)
139 return flags;
140
141 return 0;
142}
143
Magnus Damm0d3244d2008-07-16 22:59:28 -0300144static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300145 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300146{
147 iowrite32(data, priv->base + reg_offs);
148}
149
Magnus Damm7a1a8162008-12-18 12:50:30 -0300150static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300151{
152 return ioread32(priv->base + reg_offs);
153}
154
155/*
156 * Videobuf operations
157 */
158static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
159 unsigned int *count,
160 unsigned int *size)
161{
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;
166
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300167 *size = PAGE_ALIGN(icd->user_width * icd->user_height *
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300168 bytes_per_pixel);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300169
170 if (0 == *count)
171 *count = 2;
172
173 if (pcdev->video_limit) {
174 while (*size * *count > pcdev->video_limit)
175 (*count)--;
176 }
177
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300178 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300179
180 return 0;
181}
182
183static void free_buffer(struct videobuf_queue *vq,
184 struct sh_mobile_ceu_buffer *buf)
185{
186 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300187 struct device *dev = icd->dev.parent;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300188
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300189 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300190 &buf->vb, buf->vb.baddr, buf->vb.bsize);
191
192 if (in_interrupt())
193 BUG();
194
Magnus Damm97215cb2009-03-13 06:08:20 -0300195 videobuf_waiton(&buf->vb, 0, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300196 videobuf_dma_contig_free(vq, &buf->vb);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300197 dev_dbg(dev, "%s freed\n", __func__);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300198 buf->vb.state = VIDEOBUF_NEEDS_INIT;
199}
200
Magnus Damm7a1a8162008-12-18 12:50:30 -0300201#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) */
205
206
Magnus Damm0d3244d2008-07-16 22:59:28 -0300207static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
208{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300209 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300210 dma_addr_t phys_addr_top, phys_addr_bottom;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300211
Magnus Damm7a1a8162008-12-18 12:50:30 -0300212 /* 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.
215 */
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);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300221
Magnus Damm8be65dc2008-12-18 12:38:06 -0300222 if (!pcdev->active)
223 return;
224
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300225 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
226 ceu_write(pcdev, CDAYR, phys_addr_top);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300227 if (pcdev->is_interlaced) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300228 phys_addr_bottom = phys_addr_top + icd->user_width;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300229 ceu_write(pcdev, CDBYR, phys_addr_bottom);
230 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300231
232 switch (icd->current_fmt->fourcc) {
233 case V4L2_PIX_FMT_NV12:
234 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300235 case V4L2_PIX_FMT_NV16:
236 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300237 phys_addr_top += icd->user_width *
238 icd->user_height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300239 ceu_write(pcdev, CDACR, phys_addr_top);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300240 if (pcdev->is_interlaced) {
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300241 phys_addr_bottom = phys_addr_top +
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300242 icd->user_width;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300243 ceu_write(pcdev, CDBCR, phys_addr_bottom);
244 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300245 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300246
247 pcdev->active->state = VIDEOBUF_ACTIVE;
248 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300249}
250
251static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
252 struct videobuf_buffer *vb,
253 enum v4l2_field field)
254{
255 struct soc_camera_device *icd = vq->priv_data;
256 struct sh_mobile_ceu_buffer *buf;
257 int ret;
258
259 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
260
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300261 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300262 vb, vb->baddr, vb->bsize);
263
264 /* Added list head initialization on alloc */
265 WARN_ON(!list_empty(&vb->queue));
266
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
272
273 BUG_ON(NULL == icd->current_fmt);
274
275 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300276 vb->width != icd->user_width ||
277 vb->height != icd->user_height ||
Magnus Damm0d3244d2008-07-16 22:59:28 -0300278 vb->field != field) {
279 buf->fmt = icd->current_fmt;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300280 vb->width = icd->user_width;
281 vb->height = icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300282 vb->field = field;
283 vb->state = VIDEOBUF_NEEDS_INIT;
284 }
285
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;
290 }
291
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;
297 }
298
299 return 0;
300fail:
301 free_buffer(vq, buf);
302out:
303 return ret;
304}
305
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300306/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300307static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
308 struct videobuf_buffer *vb)
309{
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;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300313
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300314 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300315 vb, vb->baddr, vb->bsize);
316
Magnus Damm51354cc2008-10-16 19:51:20 -0300317 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300318 list_add_tail(&vb->queue, &pcdev->capture);
319
320 if (!pcdev->active) {
321 pcdev->active = vb;
322 sh_mobile_ceu_capture(pcdev);
323 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300324}
325
326static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
327 struct videobuf_buffer *vb)
328{
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300329 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;
333
334 spin_lock_irqsave(&pcdev->lock, flags);
335
336 if (pcdev->active == vb) {
337 /* disable capture (release DMA buffer), reset */
338 ceu_write(pcdev, CAPSR, 1 << 16);
339 pcdev->active = NULL;
340 }
341
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);
346 }
347
348 spin_unlock_irqrestore(&pcdev->lock, flags);
349
Magnus Damm0d3244d2008-07-16 22:59:28 -0300350 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
351}
352
353static 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,
358};
359
360static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
361{
362 struct sh_mobile_ceu_dev *pcdev = data;
363 struct videobuf_buffer *vb;
364 unsigned long flags;
365
366 spin_lock_irqsave(&pcdev->lock, flags);
367
368 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300369 if (!vb)
370 /* Stale interrupt from a released buffer */
371 goto out;
372
Magnus Damm0d3244d2008-07-16 22:59:28 -0300373 list_del_init(&vb->queue);
374
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;
380
381 sh_mobile_ceu_capture(pcdev);
382
383 vb->state = VIDEOBUF_DONE;
384 do_gettimeofday(&vb->ts);
385 vb->field_count++;
386 wake_up(&vb->done);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300387
388out:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300389 spin_unlock_irqrestore(&pcdev->lock, flags);
390
391 return IRQ_HANDLED;
392}
393
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300394/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300395static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
396{
397 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
398 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300399
Magnus Damm0d3244d2008-07-16 22:59:28 -0300400 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300401 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300402
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300403 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300404 "SuperH Mobile CEU driver attached to camera %d\n",
405 icd->devnum);
406
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300407 clk_enable(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300408
409 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
410 while (ceu_read(pcdev, CSTSR) & 1)
411 msleep(1);
412
413 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300414
415 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300416}
417
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300418/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300419static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
420{
421 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
422 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300423 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300424
425 BUG_ON(icd != pcdev->icd);
426
427 /* disable capture, disable interrupts */
428 ceu_write(pcdev, CEIER, 0);
429 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
Magnus Damm51354cc2008-10-16 19:51:20 -0300430
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;
438 }
439 spin_unlock_irqrestore(&pcdev->lock, flags);
440
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300441 clk_disable(pcdev->clk);
442
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300443 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300444 "SuperH Mobile CEU driver detached from camera %d\n",
445 icd->devnum);
446
447 pcdev->icd = NULL;
448}
449
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300450/*
451 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
452 * in SH7722 Hardware Manual
453 */
454static unsigned int size_dst(unsigned int src, unsigned int scale)
455{
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;
461}
462
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300463static u16 calc_scale(unsigned int src, unsigned int *dst)
464{
465 u16 scale;
466
467 if (src == *dst)
468 return 0;
469
470 scale = (src * 4096 / *dst) & ~7;
471
472 while (scale > 4096 && size_dst(src, scale) < *dst)
473 scale -= 8;
474
475 *dst = size_dst(src, scale);
476
477 return scale;
478}
479
480/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300481static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300482 unsigned int out_width,
483 unsigned int out_height)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300484{
485 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
486 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300487 struct v4l2_rect *rect = &cam->ceu_rect;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300488 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300489 unsigned int height, width, cdwdr_width, in_width, in_height;
490 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300491 u32 camor;
492
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300493 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
494 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300495
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300496 left_offset = rect->left;
497 top_offset = rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300498
499 if (pcdev->image_mode) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300500 in_width = rect->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300501 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300502 in_width *= 2;
503 left_offset *= 2;
504 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300505 width = cdwdr_width = out_width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300506 } else {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300507 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300508
509 width = out_width * w_factor / 2;
510
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300511 if (!pcdev->is_16bit)
512 w_factor *= 2;
513
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300514 in_width = rect->width * w_factor / 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300515 left_offset = left_offset * w_factor / 2;
516
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300517 cdwdr_width = width * 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300518 }
519
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300520 height = out_height;
521 in_height = rect->height;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300522 if (pcdev->is_interlaced) {
523 height /= 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300524 in_height /= 2;
525 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300526 cdwdr_width *= 2;
527 }
528
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300529 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300530 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300531
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);
536
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300537 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300538 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300539 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300540 ceu_write(pcdev, CDWDR, cdwdr_width);
541}
542
543static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
544{
545 u32 capsr = ceu_read(pcdev, CAPSR);
546 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
547 return capsr;
548}
549
550static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
551{
552 unsigned long timeout = jiffies + 10 * HZ;
553
554 /*
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.
557 */
558 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
559 msleep(1);
560
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;
565 }
566
567 /* Wait until reset clears, this shall not hang... */
568 while (ceu_read(pcdev, CAPSR) & (1 << 16))
569 udelay(10);
570
571 /* Anything to restore? */
572 if (capsr & ~(1 << 16))
573 ceu_write(pcdev, CAPSR, capsr);
574}
575
Magnus Damm0d3244d2008-07-16 22:59:28 -0300576static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
577 __u32 pixfmt)
578{
579 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
580 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300581 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300582 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300583 int yuv_lineskip;
584 struct sh_mobile_ceu_cam *cam = icd->host_priv;
585 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300586
587 camera_flags = icd->ops->query_bus_param(icd);
588 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300589 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300590 if (!common_flags)
591 return -EINVAL;
592
593 ret = icd->ops->set_bus_param(icd, common_flags);
594 if (ret < 0)
595 return ret;
596
597 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
598 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300599 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300600 break;
601 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300602 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300603 break;
604 default:
605 return -EINVAL;
606 }
607
608 ceu_write(pcdev, CRCNTR, 0);
609 ceu_write(pcdev, CRCMPR, 0);
610
Magnus Damm8be65dc2008-12-18 12:38:06 -0300611 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300612 yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300613
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 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300618 /* fall-through */
619 case V4L2_PIX_FMT_NV16:
620 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300621 switch (cam->camera_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300622 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();
636 }
637 }
638
Magnus Damm7a1a8162008-12-18 12:50:30 -0300639 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
640 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300641 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300642
Magnus Damm7a1a8162008-12-18 12:50:30 -0300643 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
644 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300645 value |= pcdev->is_16bit ? 1 << 12 : 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300646 ceu_write(pcdev, CAMCR, value);
647
648 ceu_write(pcdev, CAPCR, 0x00300000);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300649 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300650
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300651 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300652 mdelay(1);
653
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300654 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300655
656 /* A few words about byte order (observed in Big Endian mode)
657 *
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)
660 *
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)
663 *
664 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300665 * using 7 we swap the data bytes to match the incoming order:
666 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300667 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300668 value = 0x00000017;
669 if (yuv_lineskip)
670 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
671
672 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300673 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
674
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300675 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300676 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
677 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300678 icd->user_width, icd->user_height);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300679
680 capture_restore(pcdev, capsr);
681
Magnus Damm0d3244d2008-07-16 22:59:28 -0300682 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300683 return 0;
684}
685
Magnus Damm9414de32008-12-18 11:49:06 -0300686static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300687{
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;
691
692 camera_flags = icd->ops->query_bus_param(icd);
693 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300694 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300695 if (!common_flags)
696 return -EINVAL;
697
698 return 0;
699}
700
Magnus Damm8be65dc2008-12-18 12:38:06 -0300701static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
702 {
703 .name = "NV12",
704 .depth = 12,
705 .fourcc = V4L2_PIX_FMT_NV12,
706 .colorspace = V4L2_COLORSPACE_JPEG,
707 },
708 {
709 .name = "NV21",
710 .depth = 12,
711 .fourcc = V4L2_PIX_FMT_NV21,
712 .colorspace = V4L2_COLORSPACE_JPEG,
713 },
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300714 {
715 .name = "NV16",
716 .depth = 16,
717 .fourcc = V4L2_PIX_FMT_NV16,
718 .colorspace = V4L2_COLORSPACE_JPEG,
719 },
720 {
721 .name = "NV61",
722 .depth = 16,
723 .fourcc = V4L2_PIX_FMT_NV61,
724 .colorspace = V4L2_COLORSPACE_JPEG,
725 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300726};
727
Magnus Damm9414de32008-12-18 11:49:06 -0300728static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
729 struct soc_camera_format_xlate *xlate)
730{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300731 struct device *dev = icd->dev.parent;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300732 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300733 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300734 struct sh_mobile_ceu_cam *cam;
Magnus Damm9414de32008-12-18 11:49:06 -0300735
736 ret = sh_mobile_ceu_try_bus_param(icd);
737 if (ret < 0)
738 return 0;
739
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300740 if (!icd->host_priv) {
741 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
742 if (!cam)
743 return -ENOMEM;
744
745 icd->host_priv = cam;
746 } else {
747 cam = icd->host_priv;
748 }
749
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300750 /* Beginning of a pass */
751 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300752 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300753
Magnus Damm9414de32008-12-18 11:49:06 -0300754 switch (icd->formats[idx].fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300755 case V4L2_PIX_FMT_UYVY:
756 case V4L2_PIX_FMT_VYUY:
757 case V4L2_PIX_FMT_YUYV:
758 case V4L2_PIX_FMT_YVYU:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300759 if (cam->extra_fmt)
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300760 goto add_single_format;
761
762 /*
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.
770 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300771 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300772
Magnus Damm8be65dc2008-12-18 12:38:06 -0300773 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++;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300780 dev_dbg(dev, "Providing format %s using %s\n",
Magnus Damm8be65dc2008-12-18 12:38:06 -0300781 sh_mobile_ceu_formats[k].name,
782 icd->formats[idx].name);
783 }
Magnus Damm9414de32008-12-18 11:49:06 -0300784 default:
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300785add_single_format:
Magnus Damm9414de32008-12-18 11:49:06 -0300786 /* 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++;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300793 dev_dbg(dev,
Magnus Damm9414de32008-12-18 11:49:06 -0300794 "Providing format %s in pass-through mode\n",
795 icd->formats[idx].name);
796 }
797 }
798
799 return formats;
800}
801
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300802static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
803{
804 kfree(icd->host_priv);
805 icd->host_priv = NULL;
806}
807
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300808/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300809static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
810{
811 return r1->width < r2->width || r1->height < r2->height;
812}
813
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300814/* Check if r1 fails to cover r2 */
815static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
816{
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;
820}
821
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300822static unsigned int scale_down(unsigned int size, unsigned int scale)
823{
824 return (size * 4096 + scale / 2) / scale;
825}
826
827static unsigned int scale_up(unsigned int size, unsigned int scale)
828{
829 return (size * scale + 2048) / 4096;
830}
831
832static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
833{
834 return (input * 4096 + output / 2) / output;
835}
836
837static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
838{
839 struct v4l2_crop crop;
840 struct v4l2_cropcap cap;
841 int ret;
842
843 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844
845 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
846 if (!ret) {
847 *rect = crop.c;
848 return ret;
849 }
850
851 /* Camera driver doesn't support .g_crop(), assume default rectangle */
852 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
853
854 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
855 if (ret < 0)
856 return ret;
857
858 *rect = cap.defrect;
859
860 return ret;
861}
862
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300863/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300864 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300865 * 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
868 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300869static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
870 struct v4l2_crop *cam_crop)
871{
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;
877
878 v4l2_subdev_call(sd, video, s_crop, crop);
879 ret = client_g_rect(sd, cam_rect);
880 if (ret < 0)
881 return ret;
882
883 /*
884 * Now cam_crop contains the current camera input rectangle, and it must
885 * be within camera cropcap bounds
886 */
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;
892 }
893
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);
899
900 /* We need sensor maximum rectangle */
901 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
902 if (ret < 0)
903 return ret;
904
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);
909
910 /*
911 * Popular special case - some cameras can only handle fixed sizes like
912 * QVGA, VGA,... Take care to avoid infinite loop.
913 */
914 width = max(cam_rect->width, 2);
915 height = max(cam_rect->height, 2);
916
917 while (!ret && (is_smaller(cam_rect, rect) ||
918 is_inside(cam_rect, rect)) &&
919 (cap.bounds.width > width || cap.bounds.height > height)) {
920
921 width *= 2;
922 height *= 2;
923
924 cam_rect->width = width;
925 cam_rect->height = height;
926
927 /*
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.
934 */
935 if (cam_rect->left > rect->left)
936 cam_rect->left = cap.bounds.left;
937
938 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
939 cam_rect->width = rect->left + rect->width -
940 cam_rect->left;
941
942 if (cam_rect->top > rect->top)
943 cam_rect->top = cap.bounds.top;
944
945 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
946 cam_rect->height = rect->top + rect->height -
947 cam_rect->top;
948
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);
954 }
955
956 /* S_CROP must not modify the rectangle */
957 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
958 /*
959 * The camera failed to configure a suitable cropping,
960 * we cannot use the current rectangle, set to max
961 */
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);
968 }
969
970 return ret;
971}
972
973static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
974 unsigned int *scale_h, unsigned int *scale_v)
975{
976 struct v4l2_format f;
977 int ret;
978
979 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
980
981 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
982 if (ret < 0)
983 return ret;
984
985 *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
986 *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
987
988 return 0;
989}
990
991static int get_camera_subwin(struct soc_camera_device *icd,
992 struct v4l2_rect *cam_subrect,
993 unsigned int cam_hscale, unsigned int cam_vscale)
994{
995 struct sh_mobile_ceu_cam *cam = icd->host_priv;
996 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
997
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 */
1005
1006 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1007
1008 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1009 if (ret < 0)
1010 return ret;
1011
1012 dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
1013
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;
1020 }
1021
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;
1028 }
1029
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);
1033 }
1034
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);
1039
1040 return 0;
1041}
1042
1043static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
1044 bool ceu_can_scale)
1045{
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;
1053
1054 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1055
1056 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1057 if (ret < 0)
1058 return ret;
1059
1060 max_width = min(cap.bounds.width, 2560);
1061 max_height = min(cap.bounds.height, 1920);
1062
1063 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1064 if (ret < 0)
1065 return ret;
1066
1067 dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
1068
1069 if ((width == pix->width && height == pix->height) || !ceu_can_scale)
1070 return 0;
1071
1072 /* Camera set a format, but geometry is not precise, try to improve */
1073 tmp_w = pix->width;
1074 tmp_h = pix->height;
1075
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;
1090 }
1091 }
1092
1093 return 0;
1094}
1095
1096/**
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
1100 */
1101static 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)
1104{
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;
1112
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;
1117
1118 dev_geo(dev, "5: camera scaled to %ux%u\n",
1119 pix_tmp->width, pix_tmp->height);
1120
1121 /* 6. Retrieve camera output window (g_fmt) */
1122
1123 /* unneeded - it is already in "f_tmp" */
1124
1125 /* 7. Calculate new camera scales. */
1126 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1127 if (ret < 0)
1128 return ret;
1129
1130 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1131
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;
1136
1137 /*
1138 * 8. Calculate new CEU crop - apply camera scales to previously
1139 * calculated "effective" crop.
1140 */
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);
1145
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);
1149
1150 return 0;
1151}
1152
1153/* Get combined scales */
1154static int get_scales(struct soc_camera_device *icd,
1155 unsigned int *scale_h, unsigned int *scale_v)
1156{
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;
1162
1163 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1164
1165 ret = client_g_rect(sd, &cam_crop.c);
1166 if (ret < 0)
1167 return ret;
1168
1169 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1170 if (ret < 0)
1171 return ret;
1172
1173 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1174 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1175
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);
1178
1179 return 0;
1180}
1181
1182/*
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.
1187 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001188static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001189 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001190{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001191 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001192 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1193 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001194 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001195 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001196 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001197 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001198 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;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001204 int ret;
1205
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001206 /* 1. Calculate current combined scales. */
1207 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1208 if (ret < 0)
1209 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001210
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001211 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1212
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;
1217
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);
1221
1222 /* On success cam_crop contains current camera crop */
1223
1224 /*
1225 * 3. If old combined scales applied to new crop produce an impossible
1226 * user window, adjust scales to produce nearest possible window.
1227 */
1228 out_width = scale_down(rect->width, scale_comb_h);
1229 out_height = scale_down(rect->height, scale_comb_v);
1230
1231 if (out_width > 2560)
1232 out_width = 2560;
1233 else if (out_width < 2)
1234 out_width = 2;
1235
1236 if (out_height > 1920)
1237 out_height = 1920;
1238 else if (out_height < 4)
1239 out_height = 4;
1240
1241 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1242
1243 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1244
1245 /*
1246 * 5. Using actual input window and calculated combined scales calculate
1247 * camera target output window.
1248 */
1249 pix->width = scale_down(cam_rect->width, scale_comb_h);
1250 pix->height = scale_down(cam_rect->height, scale_comb_v);
1251
1252 dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
1253
1254 /* 6. - 9. */
1255 pix->pixelformat = cam->camera_fmt->fourcc;
1256 pix->colorspace = cam->camera_fmt->colorspace;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001257
1258 capsr = capture_save_reset(pcdev);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001259 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001260
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001261 /* Make relative to camera rectangle */
1262 rect->left -= cam_rect->left;
1263 rect->top -= cam_rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001264
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001265 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1266
1267 ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
1268 pcdev->image_mode && !pcdev->is_interlaced);
1269
1270 dev_geo(dev, "6-9: %d\n", ret);
1271
1272 /* 10. Use CEU cropping to crop to the new window. */
1273 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1274
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);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001278
1279 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001280 * 11. Calculate CEU scales from camera scales from results of (10) and
1281 * user window from (3)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001282 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001283 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1284 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001285
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001286 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001287
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001288 /* 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);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001293 }
1294
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001295 /* Restore capture */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001296 if (pcdev->active)
1297 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001298 capture_restore(pcdev, capsr);
1299
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001300 icd->user_width = out_width;
1301 icd->user_height = out_height;
1302
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001303 /* Even if only camera cropping succeeded */
1304 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001305}
1306
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001307/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001308static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001309 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001310{
Magnus Damm9414de32008-12-18 11:49:06 -03001311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001313 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1314 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001315 struct v4l2_format cam_f = *f;
1316 struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001317 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001318 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001319 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001320 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001321 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;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001327
1328 switch (pix->field) {
1329 case V4L2_FIELD_INTERLACED:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001330 is_interlaced = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001331 break;
1332 case V4L2_FIELD_ANY:
1333 default:
1334 pix->field = V4L2_FIELD_NONE;
1335 /* fall-through */
1336 case V4L2_FIELD_NONE:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001337 is_interlaced = false;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001338 break;
1339 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001340
Magnus Damm9414de32008-12-18 11:49:06 -03001341 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1342 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001343 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001344 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001345 }
1346
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001347 /* 1. Calculate current camera scales. */
1348 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1349
1350 ret = client_g_rect(sd, cam_rect);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001351 if (ret < 0)
1352 return ret;
1353
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001354 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1355 if (ret < 0)
1356 return ret;
1357
1358 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1359
1360 /*
1361 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1362 * scaled back at current camera scales onto input window.
1363 */
1364 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1365 if (ret < 0)
1366 return ret;
1367
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);
1371
1372 /*
1373 * 3. Calculate new combined scales from "effective" input window to
1374 * requested user window.
1375 */
1376 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1377 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1378
1379 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1380
1381 /*
1382 * 4. Calculate camera output window by applying combined scales to real
1383 * input window.
1384 */
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;
1388
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001389 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:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001394 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001395 break;
1396 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001397 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001398 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001399
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001400 dev_geo(dev, "4: camera output %ux%u\n",
1401 cam_pix->width, cam_pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001402
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001403 /* 5. - 9. */
1404 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
1405 image_mode && !is_interlaced);
1406
1407 dev_geo(dev, "5-9: client scale %d\n", ret);
1408
1409 /* Done with the camera. Now see if we can improve the result */
1410
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;
1415
1416 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001417
1418 /* We cannot scale up */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001419 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;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001423
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001424 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;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001428
1429 /* Let's rock: scale pix->{width x height} down to width x height */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001430 scale_h = calc_scale(ceu_rect.width, &pix->width);
1431 scale_v = calc_scale(ceu_rect.height, &pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001432
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001433 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);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001436
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001437 pcdev->cflcr = scale_h | (scale_v << 16);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001438
1439 icd->buswidth = xlate->buswidth;
1440 icd->current_fmt = xlate->host_fmt;
1441 cam->camera_fmt = xlate->cam_fmt;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001442 cam->ceu_rect = ceu_rect;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001443
1444 pcdev->is_interlaced = is_interlaced;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001445 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001446
1447 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001448}
1449
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001450static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1451 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001452{
Magnus Damm9414de32008-12-18 11:49:06 -03001453 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001454 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001455 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001456 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001457 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001458 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001459
Magnus Damm9414de32008-12-18 11:49:06 -03001460 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1461 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001462 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001463 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001464 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001465
Magnus Damm0d3244d2008-07-16 22:59:28 -03001466 /* FIXME: calculate using depth and bus width */
1467
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001468 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1469 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001470
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001471 width = pix->width;
1472 height = pix->height;
1473
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001474 pix->bytesperline = pix->width *
Magnus Damm9414de32008-12-18 11:49:06 -03001475 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001476 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001477
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001478 pix->pixelformat = xlate->cam_fmt->fourcc;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001479
Magnus Damm0d3244d2008-07-16 22:59:28 -03001480 /* limit to sensor capabilities */
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001481 ret = v4l2_subdev_call(sd, video, try_fmt, f);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001482 pix->pixelformat = pixfmt;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001483 if (ret < 0)
1484 return ret;
1485
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001486 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;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001497 ret = v4l2_subdev_call(sd, video, try_fmt, f);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001498 if (ret < 0) {
1499 /* Shouldn't actually happen... */
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001500 dev_err(icd->dev.parent,
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001501 "FIXME: try_fmt() returned %d\n", ret);
1502 pix->width = tmp_w;
1503 pix->height = tmp_h;
1504 }
1505 }
1506 if (pix->width > width)
1507 pix->width = width;
1508 if (pix->height > height)
1509 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001510 }
1511
1512 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001513}
1514
1515static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1516 struct v4l2_requestbuffers *p)
1517{
1518 int i;
1519
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;
1526
1527 buf = container_of(icf->vb_vidq.bufs[i],
1528 struct sh_mobile_ceu_buffer, vb);
1529 INIT_LIST_HEAD(&buf->vb.queue);
1530 }
1531
1532 return 0;
1533}
1534
1535static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1536{
1537 struct soc_camera_file *icf = file->private_data;
1538 struct sh_mobile_ceu_buffer *buf;
1539
1540 buf = list_entry(icf->vb_vidq.stream.next,
1541 struct sh_mobile_ceu_buffer, vb.stream);
1542
1543 poll_wait(file, &buf->vb.done, pt);
1544
1545 if (buf->vb.state == VIDEOBUF_DONE ||
1546 buf->vb.state == VIDEOBUF_ERROR)
1547 return POLLIN|POLLRDNORM;
1548
1549 return 0;
1550}
1551
1552static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1553 struct v4l2_capability *cap)
1554{
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;
1559}
1560
1561static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1562 struct soc_camera_device *icd)
1563{
1564 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1565 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1566
1567 videobuf_queue_dma_contig_init(q,
1568 &sh_mobile_ceu_videobuf_ops,
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001569 icd->dev.parent, &pcdev->lock,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001570 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -03001571 pcdev->is_interlaced ?
1572 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001573 sizeof(struct sh_mobile_ceu_buffer),
1574 icd);
1575}
1576
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001577static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1578 struct v4l2_control *ctrl)
1579{
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;
1583
1584 switch (ctrl->id) {
1585 case V4L2_CID_SHARPNESS:
1586 val = ceu_read(pcdev, CLFCR);
1587 ctrl->value = val ^ 1;
1588 return 0;
1589 }
1590 return -ENOIOCTLCMD;
1591}
1592
1593static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1594 struct v4l2_control *ctrl)
1595{
1596 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1597 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1598
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;
1608 }
1609 return -EINVAL;
1610 }
1611 return -ENOIOCTLCMD;
1612}
1613
1614static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1615 {
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,
1623 },
1624};
1625
Magnus Damm0d3244d2008-07-16 22:59:28 -03001626static 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,
Magnus Damm9414de32008-12-18 11:49:06 -03001630 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001631 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001632 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001633 .set_fmt = sh_mobile_ceu_set_fmt,
1634 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001635 .set_ctrl = sh_mobile_ceu_set_ctrl,
1636 .get_ctrl = sh_mobile_ceu_get_ctrl,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001637 .reqbufs = sh_mobile_ceu_reqbufs,
1638 .poll = sh_mobile_ceu_poll,
1639 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001640 .set_bus_param = sh_mobile_ceu_set_bus_param,
1641 .init_videobuf = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001642 .controls = sh_mobile_ceu_controls,
1643 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001644};
1645
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001646static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001647{
1648 struct sh_mobile_ceu_dev *pcdev;
1649 struct resource *res;
1650 void __iomem *base;
1651 unsigned int irq;
1652 int err = 0;
1653
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;
1660 }
1661
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;
1667 }
1668
Magnus Damm0d3244d2008-07-16 22:59:28 -03001669 INIT_LIST_HEAD(&pcdev->capture);
1670 spin_lock_init(&pcdev->lock);
1671
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;
1677 }
1678
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001679 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001680 if (!base) {
1681 err = -ENXIO;
1682 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1683 goto exit_kfree;
1684 }
1685
1686 pcdev->irq = irq;
1687 pcdev->base = base;
1688 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001689
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,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001694 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001695 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;
1701 }
1702
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001703 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001704 }
1705
1706 /* request irq */
1707 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03001708 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001709 if (err) {
1710 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1711 goto exit_release_mem;
1712 }
1713
Magnus Damm6d1386c2009-08-14 10:49:17 +00001714 pm_suspend_ignore_children(&pdev->dev, true);
1715 pm_runtime_enable(&pdev->dev);
1716 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09001717
Magnus Damm0d3244d2008-07-16 22:59:28 -03001718 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001719 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001720 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03001721 pcdev->ici.drv_name = dev_name(&pdev->dev);
1722 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001723
1724 err = soc_camera_host_register(&pcdev->ici);
1725 if (err)
Magnus Damm6d1386c2009-08-14 10:49:17 +00001726 goto exit_free_irq;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001727
1728 return 0;
1729
1730exit_free_irq:
1731 free_irq(pcdev->irq, pcdev);
1732exit_release_mem:
1733 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1734 dma_release_declared_memory(&pdev->dev);
1735exit_iounmap:
1736 iounmap(base);
1737exit_kfree:
1738 kfree(pcdev);
1739exit:
1740 return err;
1741}
1742
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001743static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001744{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001745 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);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001748
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001749 soc_camera_host_unregister(soc_host);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001750 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;
1756}
1757
Magnus Damm6d1386c2009-08-14 10:49:17 +00001758static int sh_mobile_ceu_runtime_nop(struct device *dev)
1759{
1760 /* Runtime PM callback shared between ->runtime_suspend()
1761 * and ->runtime_resume(). Simply returns success.
1762 *
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.
1766 */
1767 return 0;
1768}
1769
1770static 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,
1773};
1774
Magnus Damm0d3244d2008-07-16 22:59:28 -03001775static struct platform_driver sh_mobile_ceu_driver = {
1776 .driver = {
1777 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00001778 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001779 },
1780 .probe = sh_mobile_ceu_probe,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001781 .remove = __exit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001782};
1783
1784static int __init sh_mobile_ceu_init(void)
1785{
1786 return platform_driver_register(&sh_mobile_ceu_driver);
1787}
1788
1789static void __exit sh_mobile_ceu_exit(void)
1790{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001791 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001792}
1793
1794module_init(sh_mobile_ceu_init);
1795module_exit(sh_mobile_ceu_exit);
1796
1797MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1798MODULE_AUTHOR("Magnus Damm");
1799MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001800MODULE_ALIAS("platform:sh_mobile_ceu");