blob: fb88c63188f3c02ab7b8930938a0168fbc79a332 [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>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030034#include <linux/sched.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030035
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>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030041#include <media/v4l2-mediabus.h>
42#include <media/soc_mediabus.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030043
44/* register offsets for sh7722 / sh7723 */
45
Magnus Dammdd542032008-10-16 19:50:22 -030046#define CAPSR 0x00 /* Capture start register */
47#define CAPCR 0x04 /* Capture control register */
48#define CAMCR 0x08 /* Capture interface control register */
49#define CMCYR 0x0c /* Capture interface cycle register */
50#define CAMOR 0x10 /* Capture interface offset register */
51#define CAPWR 0x14 /* Capture interface width register */
52#define CAIFR 0x18 /* Capture interface input format register */
53#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
54#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
55#define CRCNTR 0x28 /* CEU register control register */
56#define CRCMPR 0x2c /* CEU register forcible control register */
57#define CFLCR 0x30 /* Capture filter control register */
58#define CFSZR 0x34 /* Capture filter size clip register */
59#define CDWDR 0x38 /* Capture destination width register */
60#define CDAYR 0x3c /* Capture data address Y register */
61#define CDACR 0x40 /* Capture data address C register */
62#define CDBYR 0x44 /* Capture data bottom-field address Y register */
63#define CDBCR 0x48 /* Capture data bottom-field address C register */
64#define CBDSR 0x4c /* Capture bundle destination size register */
65#define CFWCR 0x5c /* Firewall operation control register */
66#define CLFCR 0x60 /* Capture low-pass filter control register */
67#define CDOCR 0x64 /* Capture data output control register */
68#define CDDCR 0x68 /* Capture data complexity level register */
69#define CDDAR 0x6c /* Capture data complexity level address register */
70#define CEIER 0x70 /* Capture event interrupt enable register */
71#define CETCR 0x74 /* Capture event flag clear register */
72#define CSTSR 0x7c /* Capture status register */
73#define CSRTR 0x80 /* Capture software reset register */
74#define CDSSR 0x84 /* Capture data size register */
75#define CDAYR2 0x90 /* Capture data address Y register 2 */
76#define CDACR2 0x94 /* Capture data address C register 2 */
77#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
78#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030079
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030080#undef DEBUG_GEOMETRY
81#ifdef DEBUG_GEOMETRY
82#define dev_geo dev_info
83#else
84#define dev_geo dev_dbg
85#endif
86
Magnus Damm0d3244d2008-07-16 22:59:28 -030087/* per video frame buffer */
88struct sh_mobile_ceu_buffer {
89 struct videobuf_buffer vb; /* v4l buffer must be first */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030090 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -030091};
92
93struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030094 struct soc_camera_host ici;
95 struct soc_camera_device *icd;
96
97 unsigned int irq;
98 void __iomem *base;
99 unsigned long video_limit;
100
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -0300101 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300102 spinlock_t lock;
103 struct list_head capture;
104 struct videobuf_buffer *active;
105
106 struct sh_mobile_ceu_info *pdata;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300107
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300108 u32 cflcr;
109
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300110 enum v4l2_field field;
111
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300112 unsigned int image_mode:1;
113 unsigned int is_16bit:1;
114};
115
116struct sh_mobile_ceu_cam {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300117 struct v4l2_rect ceu_rect;
118 unsigned int cam_width;
119 unsigned int cam_height;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300120 const struct soc_mbus_pixelfmt *extra_fmt;
121 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300122};
123
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300124static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
125{
126 unsigned long flags;
127
128 flags = SOCAM_MASTER |
129 SOCAM_PCLK_SAMPLE_RISING |
130 SOCAM_HSYNC_ACTIVE_HIGH |
131 SOCAM_HSYNC_ACTIVE_LOW |
132 SOCAM_VSYNC_ACTIVE_HIGH |
133 SOCAM_VSYNC_ACTIVE_LOW |
134 SOCAM_DATA_ACTIVE_HIGH;
135
136 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
137 flags |= SOCAM_DATAWIDTH_8;
138
139 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
140 flags |= SOCAM_DATAWIDTH_16;
141
142 if (flags & SOCAM_DATAWIDTH_MASK)
143 return flags;
144
145 return 0;
146}
147
Magnus Damm0d3244d2008-07-16 22:59:28 -0300148static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300149 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300150{
151 iowrite32(data, priv->base + reg_offs);
152}
153
Magnus Damm7a1a8162008-12-18 12:50:30 -0300154static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300155{
156 return ioread32(priv->base + reg_offs);
157}
158
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300159static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
160{
161 int i, success = 0;
162 struct soc_camera_device *icd = pcdev->icd;
163
164 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
165
166 /* wait CSTSR.CPTON bit */
167 for (i = 0; i < 1000; i++) {
168 if (!(ceu_read(pcdev, CSTSR) & 1)) {
169 success++;
170 break;
171 }
172 udelay(1);
173 }
174
175 /* wait CAPSR.CPKIL bit */
176 for (i = 0; i < 1000; i++) {
177 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
178 success++;
179 break;
180 }
181 udelay(1);
182 }
183
184
185 if (2 != success) {
186 dev_warn(&icd->dev, "soft reset time out\n");
187 return -EIO;
188 }
189
190 return 0;
191}
192
Magnus Damm0d3244d2008-07-16 22:59:28 -0300193/*
194 * Videobuf operations
195 */
196static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
197 unsigned int *count,
198 unsigned int *size)
199{
200 struct soc_camera_device *icd = vq->priv_data;
201 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
202 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300203 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
204 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300205
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300206 if (bytes_per_line < 0)
207 return bytes_per_line;
208
Magnus Damme6641c82009-12-11 11:53:56 -0300209 *size = bytes_per_line * icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300210
211 if (0 == *count)
212 *count = 2;
213
214 if (pcdev->video_limit) {
Magnus Damme6641c82009-12-11 11:53:56 -0300215 while (PAGE_ALIGN(*size) * *count > pcdev->video_limit)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300216 (*count)--;
217 }
218
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300219 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300220
221 return 0;
222}
223
224static void free_buffer(struct videobuf_queue *vq,
225 struct sh_mobile_ceu_buffer *buf)
226{
227 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300228 struct device *dev = icd->dev.parent;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300229
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300230 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300231 &buf->vb, buf->vb.baddr, buf->vb.bsize);
232
233 if (in_interrupt())
234 BUG();
235
Magnus Damm97215cb2009-03-13 06:08:20 -0300236 videobuf_waiton(&buf->vb, 0, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300237 videobuf_dma_contig_free(vq, &buf->vb);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300238 dev_dbg(dev, "%s freed\n", __func__);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300239 buf->vb.state = VIDEOBUF_NEEDS_INIT;
240}
241
Magnus Damm7a1a8162008-12-18 12:50:30 -0300242#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
243#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
244#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300245#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300246#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300247#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300248
249
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300250/*
251 * return value doesn't reflex the success/failure to queue the new buffer,
252 * but rather the status of the previous buffer.
253 */
254static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300255{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300256 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300257 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300258 unsigned long top1, top2;
259 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300260 u32 status;
261 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300262
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300263 /*
264 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300265 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
266 * several not-so-well documented interrupt sources in CETCR.
267 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300268 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
269 status = ceu_read(pcdev, CETCR);
270 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
271 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300272 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
273 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300274
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300275 /*
276 * When a VBP interrupt occurs, a capture end interrupt does not occur
277 * and the image of that frame is not captured correctly. So, soft reset
278 * is needed here.
279 */
280 if (status & CEU_CEIER_VBP) {
281 sh_mobile_ceu_soft_reset(pcdev);
282 ret = -EIO;
283 }
284
Magnus Damm8be65dc2008-12-18 12:38:06 -0300285 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300286 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300287
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300288 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
289 top1 = CDBYR;
290 top2 = CDBCR;
291 bottom1 = CDAYR;
292 bottom2 = CDACR;
293 } else {
294 top1 = CDAYR;
295 top2 = CDACR;
296 bottom1 = CDBYR;
297 bottom2 = CDBCR;
298 }
299
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300300 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300301 ceu_write(pcdev, top1, phys_addr_top);
302 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300303 phys_addr_bottom = phys_addr_top + icd->user_width;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300304 ceu_write(pcdev, bottom1, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300305 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300306
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300307 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300308 case V4L2_PIX_FMT_NV12:
309 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300310 case V4L2_PIX_FMT_NV16:
311 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300312 phys_addr_top += icd->user_width *
313 icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300314 ceu_write(pcdev, top2, phys_addr_top);
315 if (V4L2_FIELD_NONE != pcdev->field) {
316 phys_addr_bottom = phys_addr_top + icd->user_width;
317 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300318 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300319 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300320
321 pcdev->active->state = VIDEOBUF_ACTIVE;
322 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300323
324 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300325}
326
327static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
328 struct videobuf_buffer *vb,
329 enum v4l2_field field)
330{
331 struct soc_camera_device *icd = vq->priv_data;
332 struct sh_mobile_ceu_buffer *buf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300333 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
334 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300335 int ret;
336
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300337 if (bytes_per_line < 0)
338 return bytes_per_line;
339
Magnus Damm0d3244d2008-07-16 22:59:28 -0300340 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
341
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300342 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300343 vb, vb->baddr, vb->bsize);
344
345 /* Added list head initialization on alloc */
346 WARN_ON(!list_empty(&vb->queue));
347
348#ifdef DEBUG
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300349 /*
350 * This can be useful if you want to see if we actually fill
351 * the buffer with something
352 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300353 memset((void *)vb->baddr, 0xaa, vb->bsize);
354#endif
355
356 BUG_ON(NULL == icd->current_fmt);
357
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300358 if (buf->code != icd->current_fmt->code ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300359 vb->width != icd->user_width ||
360 vb->height != icd->user_height ||
Magnus Damm0d3244d2008-07-16 22:59:28 -0300361 vb->field != field) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300362 buf->code = icd->current_fmt->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300363 vb->width = icd->user_width;
364 vb->height = icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300365 vb->field = field;
366 vb->state = VIDEOBUF_NEEDS_INIT;
367 }
368
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300369 vb->size = vb->height * bytes_per_line;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300370 if (0 != vb->baddr && vb->bsize < vb->size) {
371 ret = -EINVAL;
372 goto out;
373 }
374
375 if (vb->state == VIDEOBUF_NEEDS_INIT) {
376 ret = videobuf_iolock(vq, vb, NULL);
377 if (ret)
378 goto fail;
379 vb->state = VIDEOBUF_PREPARED;
380 }
381
382 return 0;
383fail:
384 free_buffer(vq, buf);
385out:
386 return ret;
387}
388
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300389/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300390static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
391 struct videobuf_buffer *vb)
392{
393 struct soc_camera_device *icd = vq->priv_data;
394 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
395 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300396
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300397 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300398 vb, vb->baddr, vb->bsize);
399
Magnus Damm51354cc2008-10-16 19:51:20 -0300400 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300401 list_add_tail(&vb->queue, &pcdev->capture);
402
403 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300404 /*
405 * Because there were no active buffer at this moment,
406 * we are not interested in the return value of
407 * sh_mobile_ceu_capture here.
408 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300409 pcdev->active = vb;
410 sh_mobile_ceu_capture(pcdev);
411 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300412}
413
414static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
415 struct videobuf_buffer *vb)
416{
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300417 struct soc_camera_device *icd = vq->priv_data;
418 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
419 struct sh_mobile_ceu_dev *pcdev = ici->priv;
420 unsigned long flags;
421
422 spin_lock_irqsave(&pcdev->lock, flags);
423
424 if (pcdev->active == vb) {
425 /* disable capture (release DMA buffer), reset */
426 ceu_write(pcdev, CAPSR, 1 << 16);
427 pcdev->active = NULL;
428 }
429
430 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
431 !list_empty(&vb->queue)) {
432 vb->state = VIDEOBUF_ERROR;
433 list_del_init(&vb->queue);
434 }
435
436 spin_unlock_irqrestore(&pcdev->lock, flags);
437
Magnus Damm0d3244d2008-07-16 22:59:28 -0300438 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
439}
440
441static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
442 .buf_setup = sh_mobile_ceu_videobuf_setup,
443 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
444 .buf_queue = sh_mobile_ceu_videobuf_queue,
445 .buf_release = sh_mobile_ceu_videobuf_release,
446};
447
448static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
449{
450 struct sh_mobile_ceu_dev *pcdev = data;
451 struct videobuf_buffer *vb;
452 unsigned long flags;
453
454 spin_lock_irqsave(&pcdev->lock, flags);
455
456 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300457 if (!vb)
458 /* Stale interrupt from a released buffer */
459 goto out;
460
Magnus Damm0d3244d2008-07-16 22:59:28 -0300461 list_del_init(&vb->queue);
462
463 if (!list_empty(&pcdev->capture))
464 pcdev->active = list_entry(pcdev->capture.next,
465 struct videobuf_buffer, queue);
466 else
467 pcdev->active = NULL;
468
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300469 vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
470 VIDEOBUF_ERROR : VIDEOBUF_DONE;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300471 do_gettimeofday(&vb->ts);
472 vb->field_count++;
473 wake_up(&vb->done);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300474
475out:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300476 spin_unlock_irqrestore(&pcdev->lock, flags);
477
478 return IRQ_HANDLED;
479}
480
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300481/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300482static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
483{
484 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
485 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300486 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300487
Magnus Damm0d3244d2008-07-16 22:59:28 -0300488 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300489 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300490
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300491 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300492 "SuperH Mobile CEU driver attached to camera %d\n",
493 icd->devnum);
494
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900495 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300496
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300497 ret = sh_mobile_ceu_soft_reset(pcdev);
498 if (!ret)
499 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300500
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300501 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300502}
503
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300504/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300505static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
506{
507 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
508 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300509 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300510
511 BUG_ON(icd != pcdev->icd);
512
513 /* disable capture, disable interrupts */
514 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300515 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300516
517 /* make sure active buffer is canceled */
518 spin_lock_irqsave(&pcdev->lock, flags);
519 if (pcdev->active) {
520 list_del(&pcdev->active->queue);
521 pcdev->active->state = VIDEOBUF_ERROR;
522 wake_up_all(&pcdev->active->done);
523 pcdev->active = NULL;
524 }
525 spin_unlock_irqrestore(&pcdev->lock, flags);
526
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900527 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300528
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300529 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300530 "SuperH Mobile CEU driver detached from camera %d\n",
531 icd->devnum);
532
533 pcdev->icd = NULL;
534}
535
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300536/*
537 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
538 * in SH7722 Hardware Manual
539 */
540static unsigned int size_dst(unsigned int src, unsigned int scale)
541{
542 unsigned int mant_pre = scale >> 12;
543 if (!src || !scale)
544 return src;
545 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
546 mant_pre * 4096 / scale + 1;
547}
548
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300549static u16 calc_scale(unsigned int src, unsigned int *dst)
550{
551 u16 scale;
552
553 if (src == *dst)
554 return 0;
555
556 scale = (src * 4096 / *dst) & ~7;
557
558 while (scale > 4096 && size_dst(src, scale) < *dst)
559 scale -= 8;
560
561 *dst = size_dst(src, scale);
562
563 return scale;
564}
565
566/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300567static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300568 unsigned int out_width,
569 unsigned int out_height)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300570{
571 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
572 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300573 struct v4l2_rect *rect = &cam->ceu_rect;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300574 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300575 unsigned int height, width, cdwdr_width, in_width, in_height;
576 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300577 u32 camor;
578
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300579 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
580 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300581
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300582 left_offset = rect->left;
583 top_offset = rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300584
585 if (pcdev->image_mode) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300586 in_width = rect->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300587 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300588 in_width *= 2;
589 left_offset *= 2;
590 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300591 width = out_width;
592 cdwdr_width = out_width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300593 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300594 int bytes_per_line = soc_mbus_bytes_per_line(out_width,
595 icd->current_fmt->host_fmt);
596 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300597
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300598 width = out_width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300599
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300600 switch (icd->current_fmt->host_fmt->packing) {
601 case SOC_MBUS_PACKING_2X8_PADHI:
602 w_factor = 2;
603 break;
604 default:
605 w_factor = 1;
606 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300607
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300608 in_width = rect->width * w_factor;
609 left_offset = left_offset * w_factor;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300610
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300611 if (bytes_per_line < 0)
612 cdwdr_width = out_width;
613 else
614 cdwdr_width = bytes_per_line;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300615 }
616
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300617 height = out_height;
618 in_height = rect->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300619 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300620 height /= 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300621 in_height /= 2;
622 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300623 cdwdr_width *= 2;
624 }
625
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300626 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300627 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300628
629 dev_geo(icd->dev.parent,
630 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
631 (in_height << 16) | in_width, (height << 16) | width,
632 cdwdr_width);
633
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300634 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300635 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300636 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300637 ceu_write(pcdev, CDWDR, cdwdr_width);
638}
639
640static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
641{
642 u32 capsr = ceu_read(pcdev, CAPSR);
643 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
644 return capsr;
645}
646
647static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
648{
649 unsigned long timeout = jiffies + 10 * HZ;
650
651 /*
652 * Wait until the end of the current frame. It can take a long time,
653 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
654 */
655 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
656 msleep(1);
657
658 if (time_after(jiffies, timeout)) {
659 dev_err(pcdev->ici.v4l2_dev.dev,
660 "Timeout waiting for frame end! Interface problem?\n");
661 return;
662 }
663
664 /* Wait until reset clears, this shall not hang... */
665 while (ceu_read(pcdev, CAPSR) & (1 << 16))
666 udelay(10);
667
668 /* Anything to restore? */
669 if (capsr & ~(1 << 16))
670 ceu_write(pcdev, CAPSR, capsr);
671}
672
Magnus Damm0d3244d2008-07-16 22:59:28 -0300673static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
674 __u32 pixfmt)
675{
676 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
677 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300678 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300679 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300680 int yuv_lineskip;
681 struct sh_mobile_ceu_cam *cam = icd->host_priv;
682 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300683
684 camera_flags = icd->ops->query_bus_param(icd);
685 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300686 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300687 if (!common_flags)
688 return -EINVAL;
689
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300690 /* Make choises, based on platform preferences */
691 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
692 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
693 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
694 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
695 else
696 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
697 }
698
699 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
700 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
701 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
702 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
703 else
704 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
705 }
706
Magnus Damm0d3244d2008-07-16 22:59:28 -0300707 ret = icd->ops->set_bus_param(icd, common_flags);
708 if (ret < 0)
709 return ret;
710
711 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
712 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300713 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300714 break;
715 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300716 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300717 break;
718 default:
719 return -EINVAL;
720 }
721
722 ceu_write(pcdev, CRCNTR, 0);
723 ceu_write(pcdev, CRCMPR, 0);
724
Magnus Damm8be65dc2008-12-18 12:38:06 -0300725 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300726 yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300727
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300728 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300729 case V4L2_PIX_FMT_NV12:
730 case V4L2_PIX_FMT_NV21:
731 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300732 /* fall-through */
733 case V4L2_PIX_FMT_NV16:
734 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300735 switch (cam->code) {
736 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300737 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
738 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300739 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300740 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
741 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300742 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300743 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
744 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300745 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300746 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
747 break;
748 default:
749 BUG();
750 }
751 }
752
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300753 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
754 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300755 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300756
Magnus Damm7a1a8162008-12-18 12:50:30 -0300757 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
758 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300759 value |= pcdev->is_16bit ? 1 << 12 : 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300760 ceu_write(pcdev, CAMCR, value);
761
762 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300763
764 switch (pcdev->field) {
765 case V4L2_FIELD_INTERLACED_TB:
766 value = 0x101;
767 break;
768 case V4L2_FIELD_INTERLACED_BT:
769 value = 0x102;
770 break;
771 default:
772 value = 0;
773 break;
774 }
775 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300776
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300777 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300778 mdelay(1);
779
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300780 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300781
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300782 /*
783 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300784 *
785 * In data fetch mode bytes are received in chunks of 8 bytes.
786 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
787 *
788 * The data is however by default written to memory in reverse order:
789 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
790 *
791 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300792 * using 7 we swap the data bytes to match the incoming order:
793 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300794 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300795 value = 0x00000017;
796 if (yuv_lineskip)
797 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
798
799 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300800 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
801
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300802 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300803 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
804 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300805 icd->user_width, icd->user_height);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300806
807 capture_restore(pcdev, capsr);
808
Magnus Damm0d3244d2008-07-16 22:59:28 -0300809 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300810 return 0;
811}
812
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300813static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
814 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300815{
816 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
817 struct sh_mobile_ceu_dev *pcdev = ici->priv;
818 unsigned long camera_flags, common_flags;
819
820 camera_flags = icd->ops->query_bus_param(icd);
821 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300822 make_bus_param(pcdev));
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300823 if (!common_flags || buswidth > 16 ||
824 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
Magnus Damm0d3244d2008-07-16 22:59:28 -0300825 return -EINVAL;
826
827 return 0;
828}
829
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300830static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300831 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300832 .fourcc = V4L2_PIX_FMT_NV12,
833 .name = "NV12",
834 .bits_per_sample = 12,
835 .packing = SOC_MBUS_PACKING_NONE,
836 .order = SOC_MBUS_ORDER_LE,
837 }, {
838 .fourcc = V4L2_PIX_FMT_NV21,
839 .name = "NV21",
840 .bits_per_sample = 12,
841 .packing = SOC_MBUS_PACKING_NONE,
842 .order = SOC_MBUS_ORDER_LE,
843 }, {
844 .fourcc = V4L2_PIX_FMT_NV16,
845 .name = "NV16",
846 .bits_per_sample = 16,
847 .packing = SOC_MBUS_PACKING_NONE,
848 .order = SOC_MBUS_ORDER_LE,
849 }, {
850 .fourcc = V4L2_PIX_FMT_NV61,
851 .name = "NV61",
852 .bits_per_sample = 16,
853 .packing = SOC_MBUS_PACKING_NONE,
854 .order = SOC_MBUS_ORDER_LE,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300855 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300856};
857
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300858/* This will be corrected as we get more formats */
859static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
860{
861 return fmt->packing == SOC_MBUS_PACKING_NONE ||
862 (fmt->bits_per_sample == 8 &&
863 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
864 (fmt->bits_per_sample > 8 &&
865 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
866}
867
Magnus Damm9414de32008-12-18 11:49:06 -0300868static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
869 struct soc_camera_format_xlate *xlate)
870{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300871 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300872 struct device *dev = icd->dev.parent;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300873 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300874 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300875 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300876 enum v4l2_mbus_pixelcode code;
877 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -0300878
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300879 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
880 if (ret < 0)
881 /* No more formats */
882 return 0;
883
884 fmt = soc_mbus_get_fmtdesc(code);
885 if (!fmt) {
886 dev_err(icd->dev.parent,
887 "Invalid format code #%d: %d\n", idx, code);
888 return -EINVAL;
889 }
890
891 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
Magnus Damm9414de32008-12-18 11:49:06 -0300892 if (ret < 0)
893 return 0;
894
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300895 if (!icd->host_priv) {
896 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
897 if (!cam)
898 return -ENOMEM;
899
900 icd->host_priv = cam;
901 } else {
902 cam = icd->host_priv;
903 }
904
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300905 /* Beginning of a pass */
906 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300907 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300908
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300909 switch (code) {
910 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
911 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
912 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
913 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300914 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300915 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300916
917 /*
918 * Our case is simple so far: for any of the above four camera
919 * formats we add all our four synthesized NV* formats, so,
920 * just marking the device with a single flag suffices. If
921 * the format generation rules are more complex, you would have
922 * to actually hang your already added / counted formats onto
923 * the host_priv pointer and check whether the format you're
924 * going to add now is already there.
925 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300926 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300927
Magnus Damm8be65dc2008-12-18 12:38:06 -0300928 n = ARRAY_SIZE(sh_mobile_ceu_formats);
929 formats += n;
930 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300931 xlate->host_fmt = &sh_mobile_ceu_formats[k];
932 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300933 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300934 dev_dbg(dev, "Providing format %s using code %d\n",
935 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -0300936 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300937 break;
Magnus Damm9414de32008-12-18 11:49:06 -0300938 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300939 if (!sh_mobile_ceu_packing_supported(fmt))
940 return 0;
941 }
942
943 /* Generic pass-through */
944 formats++;
945 if (xlate) {
946 xlate->host_fmt = fmt;
947 xlate->code = code;
948 xlate++;
949 dev_dbg(dev, "Providing format %s in pass-through mode\n",
950 xlate->host_fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -0300951 }
952
953 return formats;
954}
955
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300956static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
957{
958 kfree(icd->host_priv);
959 icd->host_priv = NULL;
960}
961
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300962/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300963static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
964{
965 return r1->width < r2->width || r1->height < r2->height;
966}
967
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300968/* Check if r1 fails to cover r2 */
969static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
970{
971 return r1->left > r2->left || r1->top > r2->top ||
972 r1->left + r1->width < r2->left + r2->width ||
973 r1->top + r1->height < r2->top + r2->height;
974}
975
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300976static unsigned int scale_down(unsigned int size, unsigned int scale)
977{
978 return (size * 4096 + scale / 2) / scale;
979}
980
981static unsigned int scale_up(unsigned int size, unsigned int scale)
982{
983 return (size * scale + 2048) / 4096;
984}
985
986static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
987{
988 return (input * 4096 + output / 2) / output;
989}
990
991static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
992{
993 struct v4l2_crop crop;
994 struct v4l2_cropcap cap;
995 int ret;
996
997 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
998
999 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1000 if (!ret) {
1001 *rect = crop.c;
1002 return ret;
1003 }
1004
1005 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1006 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1007
1008 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1009 if (ret < 0)
1010 return ret;
1011
1012 *rect = cap.defrect;
1013
1014 return ret;
1015}
1016
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001017/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001018 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001019 * 1. try if the client can produce exactly what requested by the user
1020 * 2. if (1) failed, try to double the client image until we get one big enough
1021 * 3. if (2) failed, try to request the maximum image
1022 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001023static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
1024 struct v4l2_crop *cam_crop)
1025{
1026 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1027 struct device *dev = sd->v4l2_dev->dev;
1028 struct v4l2_cropcap cap;
1029 int ret;
1030 unsigned int width, height;
1031
1032 v4l2_subdev_call(sd, video, s_crop, crop);
1033 ret = client_g_rect(sd, cam_rect);
1034 if (ret < 0)
1035 return ret;
1036
1037 /*
1038 * Now cam_crop contains the current camera input rectangle, and it must
1039 * be within camera cropcap bounds
1040 */
1041 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1042 /* Even if camera S_CROP failed, but camera rectangle matches */
1043 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
1044 rect->width, rect->height, rect->left, rect->top);
1045 return 0;
1046 }
1047
1048 /* Try to fix cropping, that camera hasn't managed to set */
1049 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
1050 cam_rect->width, cam_rect->height,
1051 cam_rect->left, cam_rect->top,
1052 rect->width, rect->height, rect->left, rect->top);
1053
1054 /* We need sensor maximum rectangle */
1055 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1056 if (ret < 0)
1057 return ret;
1058
1059 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1060 cap.bounds.width);
1061 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1062 cap.bounds.height);
1063
1064 /*
1065 * Popular special case - some cameras can only handle fixed sizes like
1066 * QVGA, VGA,... Take care to avoid infinite loop.
1067 */
1068 width = max(cam_rect->width, 2);
1069 height = max(cam_rect->height, 2);
1070
1071 while (!ret && (is_smaller(cam_rect, rect) ||
1072 is_inside(cam_rect, rect)) &&
1073 (cap.bounds.width > width || cap.bounds.height > height)) {
1074
1075 width *= 2;
1076 height *= 2;
1077
1078 cam_rect->width = width;
1079 cam_rect->height = height;
1080
1081 /*
1082 * We do not know what capabilities the camera has to set up
1083 * left and top borders. We could try to be smarter in iterating
1084 * them, e.g., if camera current left is to the right of the
1085 * target left, set it to the middle point between the current
1086 * left and minimum left. But that would add too much
1087 * complexity: we would have to iterate each border separately.
1088 */
1089 if (cam_rect->left > rect->left)
1090 cam_rect->left = cap.bounds.left;
1091
1092 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1093 cam_rect->width = rect->left + rect->width -
1094 cam_rect->left;
1095
1096 if (cam_rect->top > rect->top)
1097 cam_rect->top = cap.bounds.top;
1098
1099 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1100 cam_rect->height = rect->top + rect->height -
1101 cam_rect->top;
1102
1103 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1104 ret = client_g_rect(sd, cam_rect);
1105 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
1106 cam_rect->width, cam_rect->height,
1107 cam_rect->left, cam_rect->top);
1108 }
1109
1110 /* S_CROP must not modify the rectangle */
1111 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1112 /*
1113 * The camera failed to configure a suitable cropping,
1114 * we cannot use the current rectangle, set to max
1115 */
1116 *cam_rect = cap.bounds;
1117 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1118 ret = client_g_rect(sd, cam_rect);
1119 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
1120 cam_rect->width, cam_rect->height,
1121 cam_rect->left, cam_rect->top);
1122 }
1123
1124 return ret;
1125}
1126
1127static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
1128 unsigned int *scale_h, unsigned int *scale_v)
1129{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001130 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001131 int ret;
1132
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001133 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001134 if (ret < 0)
1135 return ret;
1136
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001137 *scale_h = calc_generic_scale(rect->width, mf.width);
1138 *scale_v = calc_generic_scale(rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001139
1140 return 0;
1141}
1142
1143static int get_camera_subwin(struct soc_camera_device *icd,
1144 struct v4l2_rect *cam_subrect,
1145 unsigned int cam_hscale, unsigned int cam_vscale)
1146{
1147 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1148 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
1149
1150 if (!ceu_rect->width) {
1151 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1152 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001153 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001154 int ret;
1155 /* First time */
1156
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001157 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001158 if (ret < 0)
1159 return ret;
1160
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001161 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001162
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001163 if (mf.width > 2560) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001164 ceu_rect->width = 2560;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001165 ceu_rect->left = (mf.width - 2560) / 2;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001166 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001167 ceu_rect->width = mf.width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001168 ceu_rect->left = 0;
1169 }
1170
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001171 if (mf.height > 1920) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001172 ceu_rect->height = 1920;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001173 ceu_rect->top = (mf.height - 1920) / 2;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001174 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001175 ceu_rect->height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001176 ceu_rect->top = 0;
1177 }
1178
1179 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1180 ceu_rect->width, ceu_rect->height,
1181 ceu_rect->left, ceu_rect->top);
1182 }
1183
1184 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1185 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1186 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1187 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1188
1189 return 0;
1190}
1191
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001192static int client_s_fmt(struct soc_camera_device *icd,
1193 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001194{
1195 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1196 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001197 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001198 unsigned int max_width, max_height;
1199 struct v4l2_cropcap cap;
1200 int ret;
1201
1202 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1203
1204 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1205 if (ret < 0)
1206 return ret;
1207
1208 max_width = min(cap.bounds.width, 2560);
1209 max_height = min(cap.bounds.height, 1920);
1210
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001211 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001212 if (ret < 0)
1213 return ret;
1214
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001215 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001216
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001217 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001218 return 0;
1219
1220 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001221 tmp_w = mf->width;
1222 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001223
1224 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1225 while ((width > tmp_w || height > tmp_h) &&
1226 tmp_w < max_width && tmp_h < max_height) {
1227 tmp_w = min(2 * tmp_w, max_width);
1228 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001229 mf->width = tmp_w;
1230 mf->height = tmp_h;
1231 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001232 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001233 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001234 if (ret < 0) {
1235 /* This shouldn't happen */
1236 dev_err(dev, "Client failed to set format: %d\n", ret);
1237 return ret;
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244/**
1245 * @rect - camera cropped rectangle
1246 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1247 * @ceu_rect - on output calculated CEU crop rectangle
1248 */
1249static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1250 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001251 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001252{
1253 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1254 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1255 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001256 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001257 unsigned int scale_h, scale_v;
1258 int ret;
1259
1260 /* 5. Apply iterative camera S_FMT for camera user window. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001261 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001262 if (ret < 0)
1263 return ret;
1264
1265 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001266 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001267
1268 /* 6. Retrieve camera output window (g_fmt) */
1269
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001270 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001271
1272 /* 7. Calculate new camera scales. */
1273 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1274 if (ret < 0)
1275 return ret;
1276
1277 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1278
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001279 cam->cam_width = mf_tmp.width;
1280 cam->cam_height = mf_tmp.height;
1281 mf->width = mf_tmp.width;
1282 mf->height = mf_tmp.height;
1283 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001284
1285 /*
1286 * 8. Calculate new CEU crop - apply camera scales to previously
1287 * calculated "effective" crop.
1288 */
1289 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1290 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1291 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1292 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1293
1294 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1295 ceu_rect->width, ceu_rect->height,
1296 ceu_rect->left, ceu_rect->top);
1297
1298 return 0;
1299}
1300
1301/* Get combined scales */
1302static int get_scales(struct soc_camera_device *icd,
1303 unsigned int *scale_h, unsigned int *scale_v)
1304{
1305 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1306 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1307 struct v4l2_crop cam_crop;
1308 unsigned int width_in, height_in;
1309 int ret;
1310
1311 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1312
1313 ret = client_g_rect(sd, &cam_crop.c);
1314 if (ret < 0)
1315 return ret;
1316
1317 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1318 if (ret < 0)
1319 return ret;
1320
1321 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1322 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1323
Guennadi Liakhovetski8cf1fec2009-10-05 12:51:47 -03001324 *scale_h = calc_generic_scale(width_in, icd->user_width);
1325 *scale_v = calc_generic_scale(height_in, icd->user_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001326
1327 return 0;
1328}
1329
1330/*
1331 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1332 * framerate by always requesting the maximum image from the client. See
1333 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1334 * scaling and cropping algorithms and for the meaning of referenced here steps.
1335 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001336static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001337 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001338{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001339 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001340 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1341 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001342 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001343 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001344 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001345 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001346 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001347 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001348 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1349 out_width, out_height;
1350 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001351 int ret;
1352
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001353 /* 1. Calculate current combined scales. */
1354 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1355 if (ret < 0)
1356 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001357
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001358 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1359
1360 /* 2. Apply iterative camera S_CROP for new input window. */
1361 ret = client_s_crop(sd, a, &cam_crop);
1362 if (ret < 0)
1363 return ret;
1364
1365 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1366 cam_rect->width, cam_rect->height,
1367 cam_rect->left, cam_rect->top);
1368
1369 /* On success cam_crop contains current camera crop */
1370
1371 /*
1372 * 3. If old combined scales applied to new crop produce an impossible
1373 * user window, adjust scales to produce nearest possible window.
1374 */
1375 out_width = scale_down(rect->width, scale_comb_h);
1376 out_height = scale_down(rect->height, scale_comb_v);
1377
1378 if (out_width > 2560)
1379 out_width = 2560;
1380 else if (out_width < 2)
1381 out_width = 2;
1382
1383 if (out_height > 1920)
1384 out_height = 1920;
1385 else if (out_height < 4)
1386 out_height = 4;
1387
1388 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1389
1390 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1391
1392 /*
1393 * 5. Using actual input window and calculated combined scales calculate
1394 * camera target output window.
1395 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001396 mf.width = scale_down(cam_rect->width, scale_comb_h);
1397 mf.height = scale_down(cam_rect->height, scale_comb_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001398
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001399 dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001400
1401 /* 6. - 9. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001402 mf.code = cam->code;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001403 mf.field = pcdev->field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001404
1405 capsr = capture_save_reset(pcdev);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001406 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001407
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001408 /* Make relative to camera rectangle */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001409 rect->left -= cam_rect->left;
1410 rect->top -= cam_rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001411
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001412 ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001413 pcdev->image_mode &&
1414 V4L2_FIELD_NONE == pcdev->field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001415
1416 dev_geo(dev, "6-9: %d\n", ret);
1417
1418 /* 10. Use CEU cropping to crop to the new window. */
1419 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1420
1421 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1422 ceu_rect->width, ceu_rect->height,
1423 ceu_rect->left, ceu_rect->top);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001424
1425 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001426 * 11. Calculate CEU scales from camera scales from results of (10) and
1427 * user window from (3)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001428 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001429 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1430 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001431
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001432 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001433
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001434 /* 12. Apply CEU scales. */
1435 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1436 if (cflcr != pcdev->cflcr) {
1437 pcdev->cflcr = cflcr;
1438 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001439 }
1440
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001441 /* Restore capture */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001442 if (pcdev->active)
1443 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001444 capture_restore(pcdev, capsr);
1445
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001446 icd->user_width = out_width;
1447 icd->user_height = out_height;
1448
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001449 /* Even if only camera cropping succeeded */
1450 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001451}
1452
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001453/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001454static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001455 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001456{
Magnus Damm9414de32008-12-18 11:49:06 -03001457 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001458 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001459 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1460 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001461 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001462 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001463 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001464 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001465 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001466 struct v4l2_crop cam_crop;
1467 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1468 unsigned int scale_cam_h, scale_cam_v;
1469 u16 scale_v, scale_h;
1470 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001471 bool image_mode;
1472 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001473
1474 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001475 default:
1476 pix->field = V4L2_FIELD_NONE;
1477 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001478 case V4L2_FIELD_INTERLACED_TB:
1479 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001480 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001481 field = pix->field;
1482 break;
1483 case V4L2_FIELD_INTERLACED:
1484 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001485 break;
1486 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001487
Magnus Damm9414de32008-12-18 11:49:06 -03001488 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1489 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001490 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001491 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001492 }
1493
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001494 /* 1. Calculate current camera scales. */
1495 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1496
1497 ret = client_g_rect(sd, cam_rect);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001498 if (ret < 0)
1499 return ret;
1500
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001501 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1502 if (ret < 0)
1503 return ret;
1504
1505 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1506
1507 /*
1508 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1509 * scaled back at current camera scales onto input window.
1510 */
1511 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1512 if (ret < 0)
1513 return ret;
1514
1515 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1516 cam_subrect.width, cam_subrect.height,
1517 cam_subrect.left, cam_subrect.top);
1518
1519 /*
1520 * 3. Calculate new combined scales from "effective" input window to
1521 * requested user window.
1522 */
1523 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1524 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1525
1526 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1527
1528 /*
1529 * 4. Calculate camera output window by applying combined scales to real
1530 * input window.
1531 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001532 mf.width = scale_down(cam_rect->width, scale_h);
1533 mf.height = scale_down(cam_rect->height, scale_v);
1534 mf.field = pix->field;
1535 mf.colorspace = pix->colorspace;
1536 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001537
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001538 switch (pixfmt) {
1539 case V4L2_PIX_FMT_NV12:
1540 case V4L2_PIX_FMT_NV21:
1541 case V4L2_PIX_FMT_NV16:
1542 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001543 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001544 break;
1545 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001546 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001547 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001548
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001549 dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001550
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001551 /* 5. - 9. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001552 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001553 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001554
1555 dev_geo(dev, "5-9: client scale %d\n", ret);
1556
1557 /* Done with the camera. Now see if we can improve the result */
1558
1559 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001560 ret, mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001561 if (ret < 0)
1562 return ret;
1563
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001564 if (mf.code != xlate->code)
1565 return -EINVAL;
1566
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001567 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001568
1569 /* We cannot scale up */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001570 if (pix->width > mf.width)
1571 pix->width = mf.width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001572 if (pix->width > ceu_rect.width)
1573 pix->width = ceu_rect.width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001574
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001575 if (pix->height > mf.height)
1576 pix->height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001577 if (pix->height > ceu_rect.height)
1578 pix->height = ceu_rect.height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001579
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001580 pix->colorspace = mf.colorspace;
1581
1582 if (image_mode) {
1583 /* Scale pix->{width x height} down to width x height */
1584 scale_h = calc_scale(ceu_rect.width, &pix->width);
1585 scale_v = calc_scale(ceu_rect.height, &pix->height);
1586
1587 pcdev->cflcr = scale_h | (scale_v << 16);
1588 } else {
1589 pix->width = ceu_rect.width;
1590 pix->height = ceu_rect.height;
1591 scale_h = scale_v = 0;
1592 pcdev->cflcr = 0;
1593 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001594
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001595 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1596 ceu_rect.width, scale_h, pix->width,
1597 ceu_rect.height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001598
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001599 cam->code = xlate->code;
1600 cam->ceu_rect = ceu_rect;
1601 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001602
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001603 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001604 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001605
1606 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001607}
1608
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001609static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1610 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001611{
Magnus Damm9414de32008-12-18 11:49:06 -03001612 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001613 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001614 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001615 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001616 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001617 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001618 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001619
Magnus Damm9414de32008-12-18 11:49:06 -03001620 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1621 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001622 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001623 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001624 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001625
Magnus Damm0d3244d2008-07-16 22:59:28 -03001626 /* FIXME: calculate using depth and bus width */
1627
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001628 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1629 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001630
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001631 width = pix->width;
1632 height = pix->height;
1633
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001634 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
1635 if (pix->bytesperline < 0)
1636 return pix->bytesperline;
1637 pix->sizeimage = height * pix->bytesperline;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001638
Magnus Damm0d3244d2008-07-16 22:59:28 -03001639 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001640 mf.width = pix->width;
1641 mf.height = pix->height;
1642 mf.field = pix->field;
1643 mf.code = xlate->code;
1644 mf.colorspace = pix->colorspace;
1645
1646 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001647 if (ret < 0)
1648 return ret;
1649
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001650 pix->width = mf.width;
1651 pix->height = mf.height;
1652 pix->field = mf.field;
1653 pix->colorspace = mf.colorspace;
1654
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001655 switch (pixfmt) {
1656 case V4L2_PIX_FMT_NV12:
1657 case V4L2_PIX_FMT_NV21:
1658 case V4L2_PIX_FMT_NV16:
1659 case V4L2_PIX_FMT_NV61:
1660 /* FIXME: check against rect_max after converting soc-camera */
1661 /* We can scale precisely, need a bigger image from camera */
1662 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001663 /*
1664 * We presume, the sensor behaves sanely, i.e., if
1665 * requested a bigger rectangle, it will not return a
1666 * smaller one.
1667 */
1668 mf.width = 2560;
1669 mf.height = 1920;
1670 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001671 if (ret < 0) {
1672 /* Shouldn't actually happen... */
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001673 dev_err(icd->dev.parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001674 "FIXME: client try_fmt() = %d\n", ret);
1675 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001676 }
1677 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001678 /* We will scale exactly */
1679 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001680 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001681 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001682 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001683 }
1684
1685 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001686}
1687
1688static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1689 struct v4l2_requestbuffers *p)
1690{
1691 int i;
1692
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001693 /*
1694 * This is for locking debugging only. I removed spinlocks and now I
Magnus Damm0d3244d2008-07-16 22:59:28 -03001695 * check whether .prepare is ever called on a linked buffer, or whether
1696 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001697 * it hadn't triggered
1698 */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001699 for (i = 0; i < p->count; i++) {
1700 struct sh_mobile_ceu_buffer *buf;
1701
1702 buf = container_of(icf->vb_vidq.bufs[i],
1703 struct sh_mobile_ceu_buffer, vb);
1704 INIT_LIST_HEAD(&buf->vb.queue);
1705 }
1706
1707 return 0;
1708}
1709
1710static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1711{
1712 struct soc_camera_file *icf = file->private_data;
1713 struct sh_mobile_ceu_buffer *buf;
1714
1715 buf = list_entry(icf->vb_vidq.stream.next,
1716 struct sh_mobile_ceu_buffer, vb.stream);
1717
1718 poll_wait(file, &buf->vb.done, pt);
1719
1720 if (buf->vb.state == VIDEOBUF_DONE ||
1721 buf->vb.state == VIDEOBUF_ERROR)
1722 return POLLIN|POLLRDNORM;
1723
1724 return 0;
1725}
1726
1727static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1728 struct v4l2_capability *cap)
1729{
1730 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1731 cap->version = KERNEL_VERSION(0, 0, 5);
1732 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1733 return 0;
1734}
1735
1736static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1737 struct soc_camera_device *icd)
1738{
1739 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1740 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1741
1742 videobuf_queue_dma_contig_init(q,
1743 &sh_mobile_ceu_videobuf_ops,
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001744 icd->dev.parent, &pcdev->lock,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001745 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001746 pcdev->field,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001747 sizeof(struct sh_mobile_ceu_buffer),
1748 icd);
1749}
1750
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001751static int sh_mobile_ceu_get_parm(struct soc_camera_device *icd,
1752 struct v4l2_streamparm *parm)
1753{
1754 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1755
1756 return v4l2_subdev_call(sd, video, g_parm, parm);
1757}
1758
1759static int sh_mobile_ceu_set_parm(struct soc_camera_device *icd,
1760 struct v4l2_streamparm *parm)
1761{
1762 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1763
1764 return v4l2_subdev_call(sd, video, s_parm, parm);
1765}
1766
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001767static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1768 struct v4l2_control *ctrl)
1769{
1770 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1771 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1772 u32 val;
1773
1774 switch (ctrl->id) {
1775 case V4L2_CID_SHARPNESS:
1776 val = ceu_read(pcdev, CLFCR);
1777 ctrl->value = val ^ 1;
1778 return 0;
1779 }
1780 return -ENOIOCTLCMD;
1781}
1782
1783static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1784 struct v4l2_control *ctrl)
1785{
1786 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1787 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1788
1789 switch (ctrl->id) {
1790 case V4L2_CID_SHARPNESS:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001791 switch (icd->current_fmt->host_fmt->fourcc) {
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001792 case V4L2_PIX_FMT_NV12:
1793 case V4L2_PIX_FMT_NV21:
1794 case V4L2_PIX_FMT_NV16:
1795 case V4L2_PIX_FMT_NV61:
1796 ceu_write(pcdev, CLFCR, !ctrl->value);
1797 return 0;
1798 }
1799 return -EINVAL;
1800 }
1801 return -ENOIOCTLCMD;
1802}
1803
1804static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1805 {
1806 .id = V4L2_CID_SHARPNESS,
1807 .type = V4L2_CTRL_TYPE_BOOLEAN,
1808 .name = "Low-pass filter",
1809 .minimum = 0,
1810 .maximum = 1,
1811 .step = 1,
1812 .default_value = 0,
1813 },
1814};
1815
Magnus Damm0d3244d2008-07-16 22:59:28 -03001816static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1817 .owner = THIS_MODULE,
1818 .add = sh_mobile_ceu_add_device,
1819 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03001820 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001821 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001822 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001823 .set_fmt = sh_mobile_ceu_set_fmt,
1824 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001825 .set_ctrl = sh_mobile_ceu_set_ctrl,
1826 .get_ctrl = sh_mobile_ceu_get_ctrl,
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001827 .set_parm = sh_mobile_ceu_set_parm,
1828 .get_parm = sh_mobile_ceu_get_parm,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001829 .reqbufs = sh_mobile_ceu_reqbufs,
1830 .poll = sh_mobile_ceu_poll,
1831 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001832 .set_bus_param = sh_mobile_ceu_set_bus_param,
1833 .init_videobuf = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001834 .controls = sh_mobile_ceu_controls,
1835 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001836};
1837
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001838static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001839{
1840 struct sh_mobile_ceu_dev *pcdev;
1841 struct resource *res;
1842 void __iomem *base;
1843 unsigned int irq;
1844 int err = 0;
1845
1846 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1847 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03001848 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03001849 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1850 err = -ENODEV;
1851 goto exit;
1852 }
1853
1854 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1855 if (!pcdev) {
1856 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1857 err = -ENOMEM;
1858 goto exit;
1859 }
1860
Magnus Damm0d3244d2008-07-16 22:59:28 -03001861 INIT_LIST_HEAD(&pcdev->capture);
1862 spin_lock_init(&pcdev->lock);
1863
1864 pcdev->pdata = pdev->dev.platform_data;
1865 if (!pcdev->pdata) {
1866 err = -EINVAL;
1867 dev_err(&pdev->dev, "CEU platform data not set.\n");
1868 goto exit_kfree;
1869 }
1870
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001871 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001872 if (!base) {
1873 err = -ENXIO;
1874 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1875 goto exit_kfree;
1876 }
1877
1878 pcdev->irq = irq;
1879 pcdev->base = base;
1880 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001881
1882 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1883 if (res) {
1884 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1885 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001886 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001887 DMA_MEMORY_MAP |
1888 DMA_MEMORY_EXCLUSIVE);
1889 if (!err) {
1890 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1891 err = -ENXIO;
1892 goto exit_iounmap;
1893 }
1894
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001895 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001896 }
1897
1898 /* request irq */
1899 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03001900 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001901 if (err) {
1902 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1903 goto exit_release_mem;
1904 }
1905
Magnus Damm6d1386c2009-08-14 10:49:17 +00001906 pm_suspend_ignore_children(&pdev->dev, true);
1907 pm_runtime_enable(&pdev->dev);
1908 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09001909
Magnus Damm0d3244d2008-07-16 22:59:28 -03001910 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001911 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001912 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03001913 pcdev->ici.drv_name = dev_name(&pdev->dev);
1914 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001915
1916 err = soc_camera_host_register(&pcdev->ici);
1917 if (err)
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001918 goto exit_free_clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001919
1920 return 0;
1921
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001922exit_free_clk:
1923 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001924 free_irq(pcdev->irq, pcdev);
1925exit_release_mem:
1926 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1927 dma_release_declared_memory(&pdev->dev);
1928exit_iounmap:
1929 iounmap(base);
1930exit_kfree:
1931 kfree(pcdev);
1932exit:
1933 return err;
1934}
1935
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001936static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001937{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001938 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1939 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1940 struct sh_mobile_ceu_dev, ici);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001941
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001942 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001943 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001944 free_irq(pcdev->irq, pcdev);
1945 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1946 dma_release_declared_memory(&pdev->dev);
1947 iounmap(pcdev->base);
1948 kfree(pcdev);
1949 return 0;
1950}
1951
Magnus Damm6d1386c2009-08-14 10:49:17 +00001952static int sh_mobile_ceu_runtime_nop(struct device *dev)
1953{
1954 /* Runtime PM callback shared between ->runtime_suspend()
1955 * and ->runtime_resume(). Simply returns success.
1956 *
1957 * This driver re-initializes all registers after
1958 * pm_runtime_get_sync() anyway so there is no need
1959 * to save and restore registers here.
1960 */
1961 return 0;
1962}
1963
Alexey Dobriyan47145212009-12-14 18:00:08 -08001964static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00001965 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1966 .runtime_resume = sh_mobile_ceu_runtime_nop,
1967};
1968
Magnus Damm0d3244d2008-07-16 22:59:28 -03001969static struct platform_driver sh_mobile_ceu_driver = {
1970 .driver = {
1971 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00001972 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001973 },
1974 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03001975 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001976};
1977
1978static int __init sh_mobile_ceu_init(void)
1979{
1980 return platform_driver_register(&sh_mobile_ceu_driver);
1981}
1982
1983static void __exit sh_mobile_ceu_exit(void)
1984{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001985 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001986}
1987
1988module_init(sh_mobile_ceu_init);
1989module_exit(sh_mobile_ceu_exit);
1990
1991MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1992MODULE_AUTHOR("Magnus Damm");
1993MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001994MODULE_ALIAS("platform:sh_mobile_ceu");