blob: 6e16b3979326fd2e39b6bfc910df9fa66ef23e1e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030031#include <linux/device.h>
32#include <linux/platform_device.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030033#include <linux/videodev2.h>
Magnus Damm6d1386c2009-08-14 10:49:17 +000034#include <linux/pm_runtime.h>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030035#include <linux/sched.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030036
37#include <media/v4l2-common.h>
38#include <media/v4l2-dev.h>
39#include <media/soc_camera.h>
40#include <media/sh_mobile_ceu.h>
41#include <media/videobuf-dma-contig.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030042#include <media/v4l2-mediabus.h>
43#include <media/soc_mediabus.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030044
45/* register offsets for sh7722 / sh7723 */
46
Magnus Dammdd542032008-10-16 19:50:22 -030047#define CAPSR 0x00 /* Capture start register */
48#define CAPCR 0x04 /* Capture control register */
49#define CAMCR 0x08 /* Capture interface control register */
50#define CMCYR 0x0c /* Capture interface cycle register */
51#define CAMOR 0x10 /* Capture interface offset register */
52#define CAPWR 0x14 /* Capture interface width register */
53#define CAIFR 0x18 /* Capture interface input format register */
54#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
55#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
56#define CRCNTR 0x28 /* CEU register control register */
57#define CRCMPR 0x2c /* CEU register forcible control register */
58#define CFLCR 0x30 /* Capture filter control register */
59#define CFSZR 0x34 /* Capture filter size clip register */
60#define CDWDR 0x38 /* Capture destination width register */
61#define CDAYR 0x3c /* Capture data address Y register */
62#define CDACR 0x40 /* Capture data address C register */
63#define CDBYR 0x44 /* Capture data bottom-field address Y register */
64#define CDBCR 0x48 /* Capture data bottom-field address C register */
65#define CBDSR 0x4c /* Capture bundle destination size register */
66#define CFWCR 0x5c /* Firewall operation control register */
67#define CLFCR 0x60 /* Capture low-pass filter control register */
68#define CDOCR 0x64 /* Capture data output control register */
69#define CDDCR 0x68 /* Capture data complexity level register */
70#define CDDAR 0x6c /* Capture data complexity level address register */
71#define CEIER 0x70 /* Capture event interrupt enable register */
72#define CETCR 0x74 /* Capture event flag clear register */
73#define CSTSR 0x7c /* Capture status register */
74#define CSRTR 0x80 /* Capture software reset register */
75#define CDSSR 0x84 /* Capture data size register */
76#define CDAYR2 0x90 /* Capture data address Y register 2 */
77#define CDACR2 0x94 /* Capture data address C register 2 */
78#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
79#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030080
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030081#undef DEBUG_GEOMETRY
82#ifdef DEBUG_GEOMETRY
83#define dev_geo dev_info
84#else
85#define dev_geo dev_dbg
86#endif
87
Magnus Damm0d3244d2008-07-16 22:59:28 -030088/* per video frame buffer */
89struct sh_mobile_ceu_buffer {
90 struct videobuf_buffer vb; /* v4l buffer must be first */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030091 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -030092};
93
94struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030095 struct soc_camera_host ici;
96 struct soc_camera_device *icd;
97
98 unsigned int irq;
99 void __iomem *base;
100 unsigned long video_limit;
101
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -0300102 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300103 spinlock_t lock;
104 struct list_head capture;
105 struct videobuf_buffer *active;
106
107 struct sh_mobile_ceu_info *pdata;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300108
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300109 u32 cflcr;
110
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300111 enum v4l2_field field;
112
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300113 unsigned int image_mode:1;
114 unsigned int is_16bit:1;
115};
116
117struct sh_mobile_ceu_cam {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300118 struct v4l2_rect ceu_rect;
119 unsigned int cam_width;
120 unsigned int cam_height;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300121 const struct soc_mbus_pixelfmt *extra_fmt;
122 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300123};
124
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300125static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
126{
127 unsigned long flags;
128
129 flags = SOCAM_MASTER |
130 SOCAM_PCLK_SAMPLE_RISING |
131 SOCAM_HSYNC_ACTIVE_HIGH |
132 SOCAM_HSYNC_ACTIVE_LOW |
133 SOCAM_VSYNC_ACTIVE_HIGH |
134 SOCAM_VSYNC_ACTIVE_LOW |
135 SOCAM_DATA_ACTIVE_HIGH;
136
137 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
138 flags |= SOCAM_DATAWIDTH_8;
139
140 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
141 flags |= SOCAM_DATAWIDTH_16;
142
143 if (flags & SOCAM_DATAWIDTH_MASK)
144 return flags;
145
146 return 0;
147}
148
Magnus Damm0d3244d2008-07-16 22:59:28 -0300149static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300150 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300151{
152 iowrite32(data, priv->base + reg_offs);
153}
154
Magnus Damm7a1a8162008-12-18 12:50:30 -0300155static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300156{
157 return ioread32(priv->base + reg_offs);
158}
159
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300160static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
161{
162 int i, success = 0;
163 struct soc_camera_device *icd = pcdev->icd;
164
165 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
166
167 /* wait CSTSR.CPTON bit */
168 for (i = 0; i < 1000; i++) {
169 if (!(ceu_read(pcdev, CSTSR) & 1)) {
170 success++;
171 break;
172 }
173 udelay(1);
174 }
175
176 /* wait CAPSR.CPKIL bit */
177 for (i = 0; i < 1000; i++) {
178 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
179 success++;
180 break;
181 }
182 udelay(1);
183 }
184
185
186 if (2 != success) {
187 dev_warn(&icd->dev, "soft reset time out\n");
188 return -EIO;
189 }
190
191 return 0;
192}
193
Magnus Damm0d3244d2008-07-16 22:59:28 -0300194/*
195 * Videobuf operations
196 */
197static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
198 unsigned int *count,
199 unsigned int *size)
200{
201 struct soc_camera_device *icd = vq->priv_data;
202 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
203 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300204 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
205 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300206
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300207 if (bytes_per_line < 0)
208 return bytes_per_line;
209
Magnus Damme6641c82009-12-11 11:53:56 -0300210 *size = bytes_per_line * icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300211
212 if (0 == *count)
213 *count = 2;
214
215 if (pcdev->video_limit) {
Magnus Damme6641c82009-12-11 11:53:56 -0300216 while (PAGE_ALIGN(*size) * *count > pcdev->video_limit)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300217 (*count)--;
218 }
219
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300220 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300221
222 return 0;
223}
224
225static void free_buffer(struct videobuf_queue *vq,
226 struct sh_mobile_ceu_buffer *buf)
227{
228 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300229 struct device *dev = icd->dev.parent;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300230
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300231 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300232 &buf->vb, buf->vb.baddr, buf->vb.bsize);
233
234 if (in_interrupt())
235 BUG();
236
Magnus Damm97215cb2009-03-13 06:08:20 -0300237 videobuf_waiton(&buf->vb, 0, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300238 videobuf_dma_contig_free(vq, &buf->vb);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300239 dev_dbg(dev, "%s freed\n", __func__);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300240 buf->vb.state = VIDEOBUF_NEEDS_INIT;
241}
242
Magnus Damm7a1a8162008-12-18 12:50:30 -0300243#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
244#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
245#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300246#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300247#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300248#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300249
250
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300251/*
252 * return value doesn't reflex the success/failure to queue the new buffer,
253 * but rather the status of the previous buffer.
254 */
255static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300256{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300257 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300258 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300259 unsigned long top1, top2;
260 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300261 u32 status;
262 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300263
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300264 /*
265 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300266 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
267 * several not-so-well documented interrupt sources in CETCR.
268 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300269 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
270 status = ceu_read(pcdev, CETCR);
271 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
272 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300273 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
274 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300275
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300276 /*
277 * When a VBP interrupt occurs, a capture end interrupt does not occur
278 * and the image of that frame is not captured correctly. So, soft reset
279 * is needed here.
280 */
281 if (status & CEU_CEIER_VBP) {
282 sh_mobile_ceu_soft_reset(pcdev);
283 ret = -EIO;
284 }
285
Magnus Damm8be65dc2008-12-18 12:38:06 -0300286 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300287 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300288
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300289 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
290 top1 = CDBYR;
291 top2 = CDBCR;
292 bottom1 = CDAYR;
293 bottom2 = CDACR;
294 } else {
295 top1 = CDAYR;
296 top2 = CDACR;
297 bottom1 = CDBYR;
298 bottom2 = CDBCR;
299 }
300
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300301 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300302 ceu_write(pcdev, top1, phys_addr_top);
303 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300304 phys_addr_bottom = phys_addr_top + icd->user_width;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300305 ceu_write(pcdev, bottom1, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300306 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300307
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300308 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300309 case V4L2_PIX_FMT_NV12:
310 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300311 case V4L2_PIX_FMT_NV16:
312 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300313 phys_addr_top += icd->user_width *
314 icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300315 ceu_write(pcdev, top2, phys_addr_top);
316 if (V4L2_FIELD_NONE != pcdev->field) {
317 phys_addr_bottom = phys_addr_top + icd->user_width;
318 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300319 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300320 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300321
322 pcdev->active->state = VIDEOBUF_ACTIVE;
323 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300324
325 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300326}
327
328static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
329 struct videobuf_buffer *vb,
330 enum v4l2_field field)
331{
332 struct soc_camera_device *icd = vq->priv_data;
333 struct sh_mobile_ceu_buffer *buf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300334 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
335 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300336 int ret;
337
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300338 if (bytes_per_line < 0)
339 return bytes_per_line;
340
Magnus Damm0d3244d2008-07-16 22:59:28 -0300341 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
342
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300343 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300344 vb, vb->baddr, vb->bsize);
345
346 /* Added list head initialization on alloc */
347 WARN_ON(!list_empty(&vb->queue));
348
349#ifdef DEBUG
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300350 /*
351 * This can be useful if you want to see if we actually fill
352 * the buffer with something
353 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300354 memset((void *)vb->baddr, 0xaa, vb->bsize);
355#endif
356
357 BUG_ON(NULL == icd->current_fmt);
358
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300359 if (buf->code != icd->current_fmt->code ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300360 vb->width != icd->user_width ||
361 vb->height != icd->user_height ||
Magnus Damm0d3244d2008-07-16 22:59:28 -0300362 vb->field != field) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300363 buf->code = icd->current_fmt->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300364 vb->width = icd->user_width;
365 vb->height = icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300366 vb->field = field;
367 vb->state = VIDEOBUF_NEEDS_INIT;
368 }
369
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300370 vb->size = vb->height * bytes_per_line;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300371 if (0 != vb->baddr && vb->bsize < vb->size) {
372 ret = -EINVAL;
373 goto out;
374 }
375
376 if (vb->state == VIDEOBUF_NEEDS_INIT) {
377 ret = videobuf_iolock(vq, vb, NULL);
378 if (ret)
379 goto fail;
380 vb->state = VIDEOBUF_PREPARED;
381 }
382
383 return 0;
384fail:
385 free_buffer(vq, buf);
386out:
387 return ret;
388}
389
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300390/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300391static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
392 struct videobuf_buffer *vb)
393{
394 struct soc_camera_device *icd = vq->priv_data;
395 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
396 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300397
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300398 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300399 vb, vb->baddr, vb->bsize);
400
Magnus Damm51354cc2008-10-16 19:51:20 -0300401 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300402 list_add_tail(&vb->queue, &pcdev->capture);
403
404 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300405 /*
406 * Because there were no active buffer at this moment,
407 * we are not interested in the return value of
408 * sh_mobile_ceu_capture here.
409 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300410 pcdev->active = vb;
411 sh_mobile_ceu_capture(pcdev);
412 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300413}
414
415static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
416 struct videobuf_buffer *vb)
417{
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300418 struct soc_camera_device *icd = vq->priv_data;
419 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
420 struct sh_mobile_ceu_dev *pcdev = ici->priv;
421 unsigned long flags;
422
423 spin_lock_irqsave(&pcdev->lock, flags);
424
425 if (pcdev->active == vb) {
426 /* disable capture (release DMA buffer), reset */
427 ceu_write(pcdev, CAPSR, 1 << 16);
428 pcdev->active = NULL;
429 }
430
431 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
432 !list_empty(&vb->queue)) {
433 vb->state = VIDEOBUF_ERROR;
434 list_del_init(&vb->queue);
435 }
436
437 spin_unlock_irqrestore(&pcdev->lock, flags);
438
Magnus Damm0d3244d2008-07-16 22:59:28 -0300439 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
440}
441
442static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
443 .buf_setup = sh_mobile_ceu_videobuf_setup,
444 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
445 .buf_queue = sh_mobile_ceu_videobuf_queue,
446 .buf_release = sh_mobile_ceu_videobuf_release,
447};
448
449static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
450{
451 struct sh_mobile_ceu_dev *pcdev = data;
452 struct videobuf_buffer *vb;
453 unsigned long flags;
454
455 spin_lock_irqsave(&pcdev->lock, flags);
456
457 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300458 if (!vb)
459 /* Stale interrupt from a released buffer */
460 goto out;
461
Magnus Damm0d3244d2008-07-16 22:59:28 -0300462 list_del_init(&vb->queue);
463
464 if (!list_empty(&pcdev->capture))
465 pcdev->active = list_entry(pcdev->capture.next,
466 struct videobuf_buffer, queue);
467 else
468 pcdev->active = NULL;
469
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300470 vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
471 VIDEOBUF_ERROR : VIDEOBUF_DONE;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300472 do_gettimeofday(&vb->ts);
473 vb->field_count++;
474 wake_up(&vb->done);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300475
476out:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300477 spin_unlock_irqrestore(&pcdev->lock, flags);
478
479 return IRQ_HANDLED;
480}
481
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300482/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300483static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
484{
485 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
486 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300487 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300488
Magnus Damm0d3244d2008-07-16 22:59:28 -0300489 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300490 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300491
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300492 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300493 "SuperH Mobile CEU driver attached to camera %d\n",
494 icd->devnum);
495
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900496 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300497
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300498 ret = sh_mobile_ceu_soft_reset(pcdev);
499 if (!ret)
500 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300501
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300502 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300503}
504
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300505/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300506static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
507{
508 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
509 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300510 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300511
512 BUG_ON(icd != pcdev->icd);
513
514 /* disable capture, disable interrupts */
515 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300516 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300517
518 /* make sure active buffer is canceled */
519 spin_lock_irqsave(&pcdev->lock, flags);
520 if (pcdev->active) {
521 list_del(&pcdev->active->queue);
522 pcdev->active->state = VIDEOBUF_ERROR;
523 wake_up_all(&pcdev->active->done);
524 pcdev->active = NULL;
525 }
526 spin_unlock_irqrestore(&pcdev->lock, flags);
527
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900528 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300529
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300530 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300531 "SuperH Mobile CEU driver detached from camera %d\n",
532 icd->devnum);
533
534 pcdev->icd = NULL;
535}
536
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300537/*
538 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
539 * in SH7722 Hardware Manual
540 */
541static unsigned int size_dst(unsigned int src, unsigned int scale)
542{
543 unsigned int mant_pre = scale >> 12;
544 if (!src || !scale)
545 return src;
546 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
547 mant_pre * 4096 / scale + 1;
548}
549
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300550static u16 calc_scale(unsigned int src, unsigned int *dst)
551{
552 u16 scale;
553
554 if (src == *dst)
555 return 0;
556
557 scale = (src * 4096 / *dst) & ~7;
558
559 while (scale > 4096 && size_dst(src, scale) < *dst)
560 scale -= 8;
561
562 *dst = size_dst(src, scale);
563
564 return scale;
565}
566
567/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300568static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300569 unsigned int out_width,
570 unsigned int out_height)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300571{
572 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
573 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300574 struct v4l2_rect *rect = &cam->ceu_rect;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300575 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300576 unsigned int height, width, cdwdr_width, in_width, in_height;
577 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300578 u32 camor;
579
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300580 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
581 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300582
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300583 left_offset = rect->left;
584 top_offset = rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300585
586 if (pcdev->image_mode) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300587 in_width = rect->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300588 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300589 in_width *= 2;
590 left_offset *= 2;
591 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300592 width = out_width;
593 cdwdr_width = out_width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300594 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300595 int bytes_per_line = soc_mbus_bytes_per_line(out_width,
596 icd->current_fmt->host_fmt);
597 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300598
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300599 width = out_width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300600
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300601 switch (icd->current_fmt->host_fmt->packing) {
602 case SOC_MBUS_PACKING_2X8_PADHI:
603 w_factor = 2;
604 break;
605 default:
606 w_factor = 1;
607 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300608
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300609 in_width = rect->width * w_factor;
610 left_offset = left_offset * w_factor;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300611
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300612 if (bytes_per_line < 0)
613 cdwdr_width = out_width;
614 else
615 cdwdr_width = bytes_per_line;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300616 }
617
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300618 height = out_height;
619 in_height = rect->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300620 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300621 height /= 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300622 in_height /= 2;
623 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300624 cdwdr_width *= 2;
625 }
626
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300627 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300628 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300629
630 dev_geo(icd->dev.parent,
631 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
632 (in_height << 16) | in_width, (height << 16) | width,
633 cdwdr_width);
634
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300635 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300636 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300637 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300638 ceu_write(pcdev, CDWDR, cdwdr_width);
639}
640
641static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
642{
643 u32 capsr = ceu_read(pcdev, CAPSR);
644 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
645 return capsr;
646}
647
648static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
649{
650 unsigned long timeout = jiffies + 10 * HZ;
651
652 /*
653 * Wait until the end of the current frame. It can take a long time,
654 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
655 */
656 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
657 msleep(1);
658
659 if (time_after(jiffies, timeout)) {
660 dev_err(pcdev->ici.v4l2_dev.dev,
661 "Timeout waiting for frame end! Interface problem?\n");
662 return;
663 }
664
665 /* Wait until reset clears, this shall not hang... */
666 while (ceu_read(pcdev, CAPSR) & (1 << 16))
667 udelay(10);
668
669 /* Anything to restore? */
670 if (capsr & ~(1 << 16))
671 ceu_write(pcdev, CAPSR, capsr);
672}
673
Magnus Damm0d3244d2008-07-16 22:59:28 -0300674static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
675 __u32 pixfmt)
676{
677 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
678 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300679 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300680 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300681 int yuv_lineskip;
682 struct sh_mobile_ceu_cam *cam = icd->host_priv;
683 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300684
685 camera_flags = icd->ops->query_bus_param(icd);
686 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300687 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300688 if (!common_flags)
689 return -EINVAL;
690
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300691 /* Make choises, based on platform preferences */
692 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
693 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
694 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
695 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
696 else
697 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
698 }
699
700 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
701 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
702 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
703 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
704 else
705 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
706 }
707
Magnus Damm0d3244d2008-07-16 22:59:28 -0300708 ret = icd->ops->set_bus_param(icd, common_flags);
709 if (ret < 0)
710 return ret;
711
712 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
713 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300714 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300715 break;
716 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300717 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300718 break;
719 default:
720 return -EINVAL;
721 }
722
723 ceu_write(pcdev, CRCNTR, 0);
724 ceu_write(pcdev, CRCMPR, 0);
725
Magnus Damm8be65dc2008-12-18 12:38:06 -0300726 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300727 yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300728
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300729 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300730 case V4L2_PIX_FMT_NV12:
731 case V4L2_PIX_FMT_NV21:
732 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300733 /* fall-through */
734 case V4L2_PIX_FMT_NV16:
735 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300736 switch (cam->code) {
737 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300738 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
739 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300740 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300741 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
742 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300743 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300744 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
745 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300746 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300747 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
748 break;
749 default:
750 BUG();
751 }
752 }
753
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300754 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
755 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300756 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300757
Magnus Damm7a1a8162008-12-18 12:50:30 -0300758 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
759 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300760 value |= pcdev->is_16bit ? 1 << 12 : 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300761 ceu_write(pcdev, CAMCR, value);
762
763 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300764
765 switch (pcdev->field) {
766 case V4L2_FIELD_INTERLACED_TB:
767 value = 0x101;
768 break;
769 case V4L2_FIELD_INTERLACED_BT:
770 value = 0x102;
771 break;
772 default:
773 value = 0;
774 break;
775 }
776 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300777
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300778 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300779 mdelay(1);
780
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300781 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300782
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300783 /*
784 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300785 *
786 * In data fetch mode bytes are received in chunks of 8 bytes.
787 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
788 *
789 * The data is however by default written to memory in reverse order:
790 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
791 *
792 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300793 * using 7 we swap the data bytes to match the incoming order:
794 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300795 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300796 value = 0x00000017;
797 if (yuv_lineskip)
798 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
799
800 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300801 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
802
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300803 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300804 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
805 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300806 icd->user_width, icd->user_height);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300807
808 capture_restore(pcdev, capsr);
809
Magnus Damm0d3244d2008-07-16 22:59:28 -0300810 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300811 return 0;
812}
813
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300814static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
815 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300816{
817 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
818 struct sh_mobile_ceu_dev *pcdev = ici->priv;
819 unsigned long camera_flags, common_flags;
820
821 camera_flags = icd->ops->query_bus_param(icd);
822 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300823 make_bus_param(pcdev));
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300824 if (!common_flags || buswidth > 16 ||
825 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
Magnus Damm0d3244d2008-07-16 22:59:28 -0300826 return -EINVAL;
827
828 return 0;
829}
830
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300831static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300832 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300833 .fourcc = V4L2_PIX_FMT_NV12,
834 .name = "NV12",
835 .bits_per_sample = 12,
836 .packing = SOC_MBUS_PACKING_NONE,
837 .order = SOC_MBUS_ORDER_LE,
838 }, {
839 .fourcc = V4L2_PIX_FMT_NV21,
840 .name = "NV21",
841 .bits_per_sample = 12,
842 .packing = SOC_MBUS_PACKING_NONE,
843 .order = SOC_MBUS_ORDER_LE,
844 }, {
845 .fourcc = V4L2_PIX_FMT_NV16,
846 .name = "NV16",
847 .bits_per_sample = 16,
848 .packing = SOC_MBUS_PACKING_NONE,
849 .order = SOC_MBUS_ORDER_LE,
850 }, {
851 .fourcc = V4L2_PIX_FMT_NV61,
852 .name = "NV61",
853 .bits_per_sample = 16,
854 .packing = SOC_MBUS_PACKING_NONE,
855 .order = SOC_MBUS_ORDER_LE,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300856 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300857};
858
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300859/* This will be corrected as we get more formats */
860static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
861{
862 return fmt->packing == SOC_MBUS_PACKING_NONE ||
863 (fmt->bits_per_sample == 8 &&
864 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
865 (fmt->bits_per_sample > 8 &&
866 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
867}
868
Magnus Damm9414de32008-12-18 11:49:06 -0300869static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
870 struct soc_camera_format_xlate *xlate)
871{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300872 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300873 struct device *dev = icd->dev.parent;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300874 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300875 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300876 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300877 enum v4l2_mbus_pixelcode code;
878 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -0300879
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300880 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
881 if (ret < 0)
882 /* No more formats */
883 return 0;
884
885 fmt = soc_mbus_get_fmtdesc(code);
886 if (!fmt) {
887 dev_err(icd->dev.parent,
888 "Invalid format code #%d: %d\n", idx, code);
889 return -EINVAL;
890 }
891
892 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
Magnus Damm9414de32008-12-18 11:49:06 -0300893 if (ret < 0)
894 return 0;
895
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300896 if (!icd->host_priv) {
897 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
898 if (!cam)
899 return -ENOMEM;
900
901 icd->host_priv = cam;
902 } else {
903 cam = icd->host_priv;
904 }
905
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300906 /* Beginning of a pass */
907 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300908 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300909
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300910 switch (code) {
911 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
912 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
913 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
914 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300915 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300916 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300917
918 /*
919 * Our case is simple so far: for any of the above four camera
920 * formats we add all our four synthesized NV* formats, so,
921 * just marking the device with a single flag suffices. If
922 * the format generation rules are more complex, you would have
923 * to actually hang your already added / counted formats onto
924 * the host_priv pointer and check whether the format you're
925 * going to add now is already there.
926 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300927 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300928
Magnus Damm8be65dc2008-12-18 12:38:06 -0300929 n = ARRAY_SIZE(sh_mobile_ceu_formats);
930 formats += n;
931 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300932 xlate->host_fmt = &sh_mobile_ceu_formats[k];
933 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300934 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300935 dev_dbg(dev, "Providing format %s using code %d\n",
936 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -0300937 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300938 break;
Magnus Damm9414de32008-12-18 11:49:06 -0300939 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300940 if (!sh_mobile_ceu_packing_supported(fmt))
941 return 0;
942 }
943
944 /* Generic pass-through */
945 formats++;
946 if (xlate) {
947 xlate->host_fmt = fmt;
948 xlate->code = code;
949 xlate++;
950 dev_dbg(dev, "Providing format %s in pass-through mode\n",
951 xlate->host_fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -0300952 }
953
954 return formats;
955}
956
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300957static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
958{
959 kfree(icd->host_priv);
960 icd->host_priv = NULL;
961}
962
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300963/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300964static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
965{
966 return r1->width < r2->width || r1->height < r2->height;
967}
968
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300969/* Check if r1 fails to cover r2 */
970static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
971{
972 return r1->left > r2->left || r1->top > r2->top ||
973 r1->left + r1->width < r2->left + r2->width ||
974 r1->top + r1->height < r2->top + r2->height;
975}
976
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300977static unsigned int scale_down(unsigned int size, unsigned int scale)
978{
979 return (size * 4096 + scale / 2) / scale;
980}
981
982static unsigned int scale_up(unsigned int size, unsigned int scale)
983{
984 return (size * scale + 2048) / 4096;
985}
986
987static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
988{
989 return (input * 4096 + output / 2) / output;
990}
991
992static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
993{
994 struct v4l2_crop crop;
995 struct v4l2_cropcap cap;
996 int ret;
997
998 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
999
1000 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1001 if (!ret) {
1002 *rect = crop.c;
1003 return ret;
1004 }
1005
1006 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1007 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1008
1009 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1010 if (ret < 0)
1011 return ret;
1012
1013 *rect = cap.defrect;
1014
1015 return ret;
1016}
1017
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001018/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001019 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001020 * 1. try if the client can produce exactly what requested by the user
1021 * 2. if (1) failed, try to double the client image until we get one big enough
1022 * 3. if (2) failed, try to request the maximum image
1023 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001024static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
1025 struct v4l2_crop *cam_crop)
1026{
1027 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1028 struct device *dev = sd->v4l2_dev->dev;
1029 struct v4l2_cropcap cap;
1030 int ret;
1031 unsigned int width, height;
1032
1033 v4l2_subdev_call(sd, video, s_crop, crop);
1034 ret = client_g_rect(sd, cam_rect);
1035 if (ret < 0)
1036 return ret;
1037
1038 /*
1039 * Now cam_crop contains the current camera input rectangle, and it must
1040 * be within camera cropcap bounds
1041 */
1042 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1043 /* Even if camera S_CROP failed, but camera rectangle matches */
1044 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
1045 rect->width, rect->height, rect->left, rect->top);
1046 return 0;
1047 }
1048
1049 /* Try to fix cropping, that camera hasn't managed to set */
1050 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
1051 cam_rect->width, cam_rect->height,
1052 cam_rect->left, cam_rect->top,
1053 rect->width, rect->height, rect->left, rect->top);
1054
1055 /* We need sensor maximum rectangle */
1056 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1057 if (ret < 0)
1058 return ret;
1059
1060 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1061 cap.bounds.width);
1062 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1063 cap.bounds.height);
1064
1065 /*
1066 * Popular special case - some cameras can only handle fixed sizes like
1067 * QVGA, VGA,... Take care to avoid infinite loop.
1068 */
1069 width = max(cam_rect->width, 2);
1070 height = max(cam_rect->height, 2);
1071
1072 while (!ret && (is_smaller(cam_rect, rect) ||
1073 is_inside(cam_rect, rect)) &&
1074 (cap.bounds.width > width || cap.bounds.height > height)) {
1075
1076 width *= 2;
1077 height *= 2;
1078
1079 cam_rect->width = width;
1080 cam_rect->height = height;
1081
1082 /*
1083 * We do not know what capabilities the camera has to set up
1084 * left and top borders. We could try to be smarter in iterating
1085 * them, e.g., if camera current left is to the right of the
1086 * target left, set it to the middle point between the current
1087 * left and minimum left. But that would add too much
1088 * complexity: we would have to iterate each border separately.
1089 */
1090 if (cam_rect->left > rect->left)
1091 cam_rect->left = cap.bounds.left;
1092
1093 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1094 cam_rect->width = rect->left + rect->width -
1095 cam_rect->left;
1096
1097 if (cam_rect->top > rect->top)
1098 cam_rect->top = cap.bounds.top;
1099
1100 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1101 cam_rect->height = rect->top + rect->height -
1102 cam_rect->top;
1103
1104 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1105 ret = client_g_rect(sd, cam_rect);
1106 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
1107 cam_rect->width, cam_rect->height,
1108 cam_rect->left, cam_rect->top);
1109 }
1110
1111 /* S_CROP must not modify the rectangle */
1112 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1113 /*
1114 * The camera failed to configure a suitable cropping,
1115 * we cannot use the current rectangle, set to max
1116 */
1117 *cam_rect = cap.bounds;
1118 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1119 ret = client_g_rect(sd, cam_rect);
1120 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
1121 cam_rect->width, cam_rect->height,
1122 cam_rect->left, cam_rect->top);
1123 }
1124
1125 return ret;
1126}
1127
1128static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
1129 unsigned int *scale_h, unsigned int *scale_v)
1130{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001131 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001132 int ret;
1133
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001134 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001135 if (ret < 0)
1136 return ret;
1137
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001138 *scale_h = calc_generic_scale(rect->width, mf.width);
1139 *scale_v = calc_generic_scale(rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001140
1141 return 0;
1142}
1143
1144static int get_camera_subwin(struct soc_camera_device *icd,
1145 struct v4l2_rect *cam_subrect,
1146 unsigned int cam_hscale, unsigned int cam_vscale)
1147{
1148 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1149 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
1150
1151 if (!ceu_rect->width) {
1152 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1153 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001154 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001155 int ret;
1156 /* First time */
1157
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001158 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001159 if (ret < 0)
1160 return ret;
1161
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001162 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001163
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001164 if (mf.width > 2560) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001165 ceu_rect->width = 2560;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001166 ceu_rect->left = (mf.width - 2560) / 2;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001167 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001168 ceu_rect->width = mf.width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001169 ceu_rect->left = 0;
1170 }
1171
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001172 if (mf.height > 1920) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001173 ceu_rect->height = 1920;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001174 ceu_rect->top = (mf.height - 1920) / 2;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001175 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001176 ceu_rect->height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001177 ceu_rect->top = 0;
1178 }
1179
1180 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1181 ceu_rect->width, ceu_rect->height,
1182 ceu_rect->left, ceu_rect->top);
1183 }
1184
1185 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1186 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1187 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1188 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1189
1190 return 0;
1191}
1192
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001193static int client_s_fmt(struct soc_camera_device *icd,
1194 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001195{
1196 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1197 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001198 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001199 unsigned int max_width, max_height;
1200 struct v4l2_cropcap cap;
1201 int ret;
1202
1203 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1204
1205 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1206 if (ret < 0)
1207 return ret;
1208
1209 max_width = min(cap.bounds.width, 2560);
1210 max_height = min(cap.bounds.height, 1920);
1211
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001212 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001213 if (ret < 0)
1214 return ret;
1215
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001216 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001217
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001218 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001219 return 0;
1220
1221 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001222 tmp_w = mf->width;
1223 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001224
1225 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1226 while ((width > tmp_w || height > tmp_h) &&
1227 tmp_w < max_width && tmp_h < max_height) {
1228 tmp_w = min(2 * tmp_w, max_width);
1229 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001230 mf->width = tmp_w;
1231 mf->height = tmp_h;
1232 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001233 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001234 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001235 if (ret < 0) {
1236 /* This shouldn't happen */
1237 dev_err(dev, "Client failed to set format: %d\n", ret);
1238 return ret;
1239 }
1240 }
1241
1242 return 0;
1243}
1244
1245/**
1246 * @rect - camera cropped rectangle
1247 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1248 * @ceu_rect - on output calculated CEU crop rectangle
1249 */
1250static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1251 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001252 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001253{
1254 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1255 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1256 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001257 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001258 unsigned int scale_h, scale_v;
1259 int ret;
1260
1261 /* 5. Apply iterative camera S_FMT for camera user window. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001262 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001263 if (ret < 0)
1264 return ret;
1265
1266 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001267 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001268
1269 /* 6. Retrieve camera output window (g_fmt) */
1270
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001271 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001272
1273 /* 7. Calculate new camera scales. */
1274 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1275 if (ret < 0)
1276 return ret;
1277
1278 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1279
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001280 cam->cam_width = mf_tmp.width;
1281 cam->cam_height = mf_tmp.height;
1282 mf->width = mf_tmp.width;
1283 mf->height = mf_tmp.height;
1284 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001285
1286 /*
1287 * 8. Calculate new CEU crop - apply camera scales to previously
1288 * calculated "effective" crop.
1289 */
1290 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1291 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1292 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1293 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1294
1295 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1296 ceu_rect->width, ceu_rect->height,
1297 ceu_rect->left, ceu_rect->top);
1298
1299 return 0;
1300}
1301
1302/* Get combined scales */
1303static int get_scales(struct soc_camera_device *icd,
1304 unsigned int *scale_h, unsigned int *scale_v)
1305{
1306 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1307 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1308 struct v4l2_crop cam_crop;
1309 unsigned int width_in, height_in;
1310 int ret;
1311
1312 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1313
1314 ret = client_g_rect(sd, &cam_crop.c);
1315 if (ret < 0)
1316 return ret;
1317
1318 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1319 if (ret < 0)
1320 return ret;
1321
1322 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1323 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1324
Guennadi Liakhovetski8cf1fec2009-10-05 12:51:47 -03001325 *scale_h = calc_generic_scale(width_in, icd->user_width);
1326 *scale_v = calc_generic_scale(height_in, icd->user_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001327
1328 return 0;
1329}
1330
1331/*
1332 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1333 * framerate by always requesting the maximum image from the client. See
1334 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1335 * scaling and cropping algorithms and for the meaning of referenced here steps.
1336 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001337static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001338 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001339{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001340 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001341 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1342 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001343 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001344 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001345 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001346 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001347 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001348 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001349 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1350 out_width, out_height;
1351 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001352 int ret;
1353
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001354 /* 1. Calculate current combined scales. */
1355 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1356 if (ret < 0)
1357 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001358
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001359 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1360
1361 /* 2. Apply iterative camera S_CROP for new input window. */
1362 ret = client_s_crop(sd, a, &cam_crop);
1363 if (ret < 0)
1364 return ret;
1365
1366 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1367 cam_rect->width, cam_rect->height,
1368 cam_rect->left, cam_rect->top);
1369
1370 /* On success cam_crop contains current camera crop */
1371
1372 /*
1373 * 3. If old combined scales applied to new crop produce an impossible
1374 * user window, adjust scales to produce nearest possible window.
1375 */
1376 out_width = scale_down(rect->width, scale_comb_h);
1377 out_height = scale_down(rect->height, scale_comb_v);
1378
1379 if (out_width > 2560)
1380 out_width = 2560;
1381 else if (out_width < 2)
1382 out_width = 2;
1383
1384 if (out_height > 1920)
1385 out_height = 1920;
1386 else if (out_height < 4)
1387 out_height = 4;
1388
1389 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1390
1391 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1392
1393 /*
1394 * 5. Using actual input window and calculated combined scales calculate
1395 * camera target output window.
1396 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001397 mf.width = scale_down(cam_rect->width, scale_comb_h);
1398 mf.height = scale_down(cam_rect->height, scale_comb_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001399
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001400 dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001401
1402 /* 6. - 9. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001403 mf.code = cam->code;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001404 mf.field = pcdev->field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001405
1406 capsr = capture_save_reset(pcdev);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001407 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001408
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001409 /* Make relative to camera rectangle */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001410 rect->left -= cam_rect->left;
1411 rect->top -= cam_rect->top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001412
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001413 ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001414 pcdev->image_mode &&
1415 V4L2_FIELD_NONE == pcdev->field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001416
1417 dev_geo(dev, "6-9: %d\n", ret);
1418
1419 /* 10. Use CEU cropping to crop to the new window. */
1420 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1421
1422 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1423 ceu_rect->width, ceu_rect->height,
1424 ceu_rect->left, ceu_rect->top);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001425
1426 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001427 * 11. Calculate CEU scales from camera scales from results of (10) and
1428 * user window from (3)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001429 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001430 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1431 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001432
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001433 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001434
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001435 /* 12. Apply CEU scales. */
1436 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1437 if (cflcr != pcdev->cflcr) {
1438 pcdev->cflcr = cflcr;
1439 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001440 }
1441
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001442 /* Restore capture */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001443 if (pcdev->active)
1444 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001445 capture_restore(pcdev, capsr);
1446
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001447 icd->user_width = out_width;
1448 icd->user_height = out_height;
1449
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001450 /* Even if only camera cropping succeeded */
1451 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001452}
1453
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001454/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001455static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001456 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001457{
Magnus Damm9414de32008-12-18 11:49:06 -03001458 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001459 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001460 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1461 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001462 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001463 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001464 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001465 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001466 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001467 struct v4l2_crop cam_crop;
1468 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1469 unsigned int scale_cam_h, scale_cam_v;
1470 u16 scale_v, scale_h;
1471 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001472 bool image_mode;
1473 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001474
1475 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001476 default:
1477 pix->field = V4L2_FIELD_NONE;
1478 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001479 case V4L2_FIELD_INTERLACED_TB:
1480 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001481 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001482 field = pix->field;
1483 break;
1484 case V4L2_FIELD_INTERLACED:
1485 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001486 break;
1487 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001488
Magnus Damm9414de32008-12-18 11:49:06 -03001489 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1490 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001491 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001492 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001493 }
1494
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001495 /* 1. Calculate current camera scales. */
1496 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1497
1498 ret = client_g_rect(sd, cam_rect);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001499 if (ret < 0)
1500 return ret;
1501
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001502 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1503 if (ret < 0)
1504 return ret;
1505
1506 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1507
1508 /*
1509 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1510 * scaled back at current camera scales onto input window.
1511 */
1512 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1513 if (ret < 0)
1514 return ret;
1515
1516 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1517 cam_subrect.width, cam_subrect.height,
1518 cam_subrect.left, cam_subrect.top);
1519
1520 /*
1521 * 3. Calculate new combined scales from "effective" input window to
1522 * requested user window.
1523 */
1524 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1525 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1526
1527 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1528
1529 /*
1530 * 4. Calculate camera output window by applying combined scales to real
1531 * input window.
1532 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001533 mf.width = scale_down(cam_rect->width, scale_h);
1534 mf.height = scale_down(cam_rect->height, scale_v);
1535 mf.field = pix->field;
1536 mf.colorspace = pix->colorspace;
1537 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001538
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001539 switch (pixfmt) {
1540 case V4L2_PIX_FMT_NV12:
1541 case V4L2_PIX_FMT_NV21:
1542 case V4L2_PIX_FMT_NV16:
1543 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001544 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001545 break;
1546 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001547 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001548 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001549
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001550 dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001551
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001552 /* 5. - 9. */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001553 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001554 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001555
1556 dev_geo(dev, "5-9: client scale %d\n", ret);
1557
1558 /* Done with the camera. Now see if we can improve the result */
1559
1560 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001561 ret, mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001562 if (ret < 0)
1563 return ret;
1564
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001565 if (mf.code != xlate->code)
1566 return -EINVAL;
1567
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001568 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001569
1570 /* We cannot scale up */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001571 if (pix->width > mf.width)
1572 pix->width = mf.width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001573 if (pix->width > ceu_rect.width)
1574 pix->width = ceu_rect.width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001575
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001576 if (pix->height > mf.height)
1577 pix->height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001578 if (pix->height > ceu_rect.height)
1579 pix->height = ceu_rect.height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001580
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001581 pix->colorspace = mf.colorspace;
1582
1583 if (image_mode) {
1584 /* Scale pix->{width x height} down to width x height */
1585 scale_h = calc_scale(ceu_rect.width, &pix->width);
1586 scale_v = calc_scale(ceu_rect.height, &pix->height);
1587
1588 pcdev->cflcr = scale_h | (scale_v << 16);
1589 } else {
1590 pix->width = ceu_rect.width;
1591 pix->height = ceu_rect.height;
1592 scale_h = scale_v = 0;
1593 pcdev->cflcr = 0;
1594 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001595
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001596 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1597 ceu_rect.width, scale_h, pix->width,
1598 ceu_rect.height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001599
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001600 cam->code = xlate->code;
1601 cam->ceu_rect = ceu_rect;
1602 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001603
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001604 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001605 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001606
1607 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001608}
1609
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001610static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1611 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001612{
Magnus Damm9414de32008-12-18 11:49:06 -03001613 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001614 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001615 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001616 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001617 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001618 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001619 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001620
Magnus Damm9414de32008-12-18 11:49:06 -03001621 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1622 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001623 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001624 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001625 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001626
Magnus Damm0d3244d2008-07-16 22:59:28 -03001627 /* FIXME: calculate using depth and bus width */
1628
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001629 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1630 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001631
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001632 width = pix->width;
1633 height = pix->height;
1634
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001635 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
1636 if (pix->bytesperline < 0)
1637 return pix->bytesperline;
1638 pix->sizeimage = height * pix->bytesperline;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001639
Magnus Damm0d3244d2008-07-16 22:59:28 -03001640 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001641 mf.width = pix->width;
1642 mf.height = pix->height;
1643 mf.field = pix->field;
1644 mf.code = xlate->code;
1645 mf.colorspace = pix->colorspace;
1646
1647 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001648 if (ret < 0)
1649 return ret;
1650
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001651 pix->width = mf.width;
1652 pix->height = mf.height;
1653 pix->field = mf.field;
1654 pix->colorspace = mf.colorspace;
1655
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001656 switch (pixfmt) {
1657 case V4L2_PIX_FMT_NV12:
1658 case V4L2_PIX_FMT_NV21:
1659 case V4L2_PIX_FMT_NV16:
1660 case V4L2_PIX_FMT_NV61:
1661 /* FIXME: check against rect_max after converting soc-camera */
1662 /* We can scale precisely, need a bigger image from camera */
1663 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001664 /*
1665 * We presume, the sensor behaves sanely, i.e., if
1666 * requested a bigger rectangle, it will not return a
1667 * smaller one.
1668 */
1669 mf.width = 2560;
1670 mf.height = 1920;
1671 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001672 if (ret < 0) {
1673 /* Shouldn't actually happen... */
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001674 dev_err(icd->dev.parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001675 "FIXME: client try_fmt() = %d\n", ret);
1676 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001677 }
1678 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001679 /* We will scale exactly */
1680 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001681 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001682 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001683 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001684 }
1685
1686 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001687}
1688
1689static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1690 struct v4l2_requestbuffers *p)
1691{
1692 int i;
1693
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001694 /*
1695 * This is for locking debugging only. I removed spinlocks and now I
Magnus Damm0d3244d2008-07-16 22:59:28 -03001696 * check whether .prepare is ever called on a linked buffer, or whether
1697 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001698 * it hadn't triggered
1699 */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001700 for (i = 0; i < p->count; i++) {
1701 struct sh_mobile_ceu_buffer *buf;
1702
1703 buf = container_of(icf->vb_vidq.bufs[i],
1704 struct sh_mobile_ceu_buffer, vb);
1705 INIT_LIST_HEAD(&buf->vb.queue);
1706 }
1707
1708 return 0;
1709}
1710
1711static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1712{
1713 struct soc_camera_file *icf = file->private_data;
1714 struct sh_mobile_ceu_buffer *buf;
1715
1716 buf = list_entry(icf->vb_vidq.stream.next,
1717 struct sh_mobile_ceu_buffer, vb.stream);
1718
1719 poll_wait(file, &buf->vb.done, pt);
1720
1721 if (buf->vb.state == VIDEOBUF_DONE ||
1722 buf->vb.state == VIDEOBUF_ERROR)
1723 return POLLIN|POLLRDNORM;
1724
1725 return 0;
1726}
1727
1728static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1729 struct v4l2_capability *cap)
1730{
1731 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1732 cap->version = KERNEL_VERSION(0, 0, 5);
1733 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1734 return 0;
1735}
1736
1737static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1738 struct soc_camera_device *icd)
1739{
1740 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1741 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1742
1743 videobuf_queue_dma_contig_init(q,
1744 &sh_mobile_ceu_videobuf_ops,
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001745 icd->dev.parent, &pcdev->lock,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001746 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001747 pcdev->field,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001748 sizeof(struct sh_mobile_ceu_buffer),
1749 icd);
1750}
1751
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001752static int sh_mobile_ceu_get_parm(struct soc_camera_device *icd,
1753 struct v4l2_streamparm *parm)
1754{
1755 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1756
1757 return v4l2_subdev_call(sd, video, g_parm, parm);
1758}
1759
1760static int sh_mobile_ceu_set_parm(struct soc_camera_device *icd,
1761 struct v4l2_streamparm *parm)
1762{
1763 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1764
1765 return v4l2_subdev_call(sd, video, s_parm, parm);
1766}
1767
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001768static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1769 struct v4l2_control *ctrl)
1770{
1771 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1772 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1773 u32 val;
1774
1775 switch (ctrl->id) {
1776 case V4L2_CID_SHARPNESS:
1777 val = ceu_read(pcdev, CLFCR);
1778 ctrl->value = val ^ 1;
1779 return 0;
1780 }
1781 return -ENOIOCTLCMD;
1782}
1783
1784static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1785 struct v4l2_control *ctrl)
1786{
1787 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1788 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1789
1790 switch (ctrl->id) {
1791 case V4L2_CID_SHARPNESS:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001792 switch (icd->current_fmt->host_fmt->fourcc) {
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001793 case V4L2_PIX_FMT_NV12:
1794 case V4L2_PIX_FMT_NV21:
1795 case V4L2_PIX_FMT_NV16:
1796 case V4L2_PIX_FMT_NV61:
1797 ceu_write(pcdev, CLFCR, !ctrl->value);
1798 return 0;
1799 }
1800 return -EINVAL;
1801 }
1802 return -ENOIOCTLCMD;
1803}
1804
1805static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1806 {
1807 .id = V4L2_CID_SHARPNESS,
1808 .type = V4L2_CTRL_TYPE_BOOLEAN,
1809 .name = "Low-pass filter",
1810 .minimum = 0,
1811 .maximum = 1,
1812 .step = 1,
1813 .default_value = 0,
1814 },
1815};
1816
Magnus Damm0d3244d2008-07-16 22:59:28 -03001817static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1818 .owner = THIS_MODULE,
1819 .add = sh_mobile_ceu_add_device,
1820 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03001821 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001822 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001823 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001824 .set_fmt = sh_mobile_ceu_set_fmt,
1825 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001826 .set_ctrl = sh_mobile_ceu_set_ctrl,
1827 .get_ctrl = sh_mobile_ceu_get_ctrl,
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001828 .set_parm = sh_mobile_ceu_set_parm,
1829 .get_parm = sh_mobile_ceu_get_parm,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001830 .reqbufs = sh_mobile_ceu_reqbufs,
1831 .poll = sh_mobile_ceu_poll,
1832 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001833 .set_bus_param = sh_mobile_ceu_set_bus_param,
1834 .init_videobuf = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001835 .controls = sh_mobile_ceu_controls,
1836 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001837};
1838
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001839static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001840{
1841 struct sh_mobile_ceu_dev *pcdev;
1842 struct resource *res;
1843 void __iomem *base;
1844 unsigned int irq;
1845 int err = 0;
1846
1847 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1848 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03001849 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03001850 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1851 err = -ENODEV;
1852 goto exit;
1853 }
1854
1855 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1856 if (!pcdev) {
1857 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1858 err = -ENOMEM;
1859 goto exit;
1860 }
1861
Magnus Damm0d3244d2008-07-16 22:59:28 -03001862 INIT_LIST_HEAD(&pcdev->capture);
1863 spin_lock_init(&pcdev->lock);
1864
1865 pcdev->pdata = pdev->dev.platform_data;
1866 if (!pcdev->pdata) {
1867 err = -EINVAL;
1868 dev_err(&pdev->dev, "CEU platform data not set.\n");
1869 goto exit_kfree;
1870 }
1871
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001872 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001873 if (!base) {
1874 err = -ENXIO;
1875 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1876 goto exit_kfree;
1877 }
1878
1879 pcdev->irq = irq;
1880 pcdev->base = base;
1881 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001882
1883 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1884 if (res) {
1885 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1886 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001887 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001888 DMA_MEMORY_MAP |
1889 DMA_MEMORY_EXCLUSIVE);
1890 if (!err) {
1891 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1892 err = -ENXIO;
1893 goto exit_iounmap;
1894 }
1895
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001896 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001897 }
1898
1899 /* request irq */
1900 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03001901 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001902 if (err) {
1903 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1904 goto exit_release_mem;
1905 }
1906
Magnus Damm6d1386c2009-08-14 10:49:17 +00001907 pm_suspend_ignore_children(&pdev->dev, true);
1908 pm_runtime_enable(&pdev->dev);
1909 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09001910
Magnus Damm0d3244d2008-07-16 22:59:28 -03001911 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001912 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001913 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03001914 pcdev->ici.drv_name = dev_name(&pdev->dev);
1915 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001916
1917 err = soc_camera_host_register(&pcdev->ici);
1918 if (err)
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001919 goto exit_free_clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001920
1921 return 0;
1922
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001923exit_free_clk:
1924 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001925 free_irq(pcdev->irq, pcdev);
1926exit_release_mem:
1927 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1928 dma_release_declared_memory(&pdev->dev);
1929exit_iounmap:
1930 iounmap(base);
1931exit_kfree:
1932 kfree(pcdev);
1933exit:
1934 return err;
1935}
1936
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001937static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001938{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001939 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1940 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1941 struct sh_mobile_ceu_dev, ici);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001942
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001943 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03001944 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001945 free_irq(pcdev->irq, pcdev);
1946 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1947 dma_release_declared_memory(&pdev->dev);
1948 iounmap(pcdev->base);
1949 kfree(pcdev);
1950 return 0;
1951}
1952
Magnus Damm6d1386c2009-08-14 10:49:17 +00001953static int sh_mobile_ceu_runtime_nop(struct device *dev)
1954{
1955 /* Runtime PM callback shared between ->runtime_suspend()
1956 * and ->runtime_resume(). Simply returns success.
1957 *
1958 * This driver re-initializes all registers after
1959 * pm_runtime_get_sync() anyway so there is no need
1960 * to save and restore registers here.
1961 */
1962 return 0;
1963}
1964
Alexey Dobriyan47145212009-12-14 18:00:08 -08001965static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00001966 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1967 .runtime_resume = sh_mobile_ceu_runtime_nop,
1968};
1969
Magnus Damm0d3244d2008-07-16 22:59:28 -03001970static struct platform_driver sh_mobile_ceu_driver = {
1971 .driver = {
1972 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00001973 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001974 },
1975 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03001976 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001977};
1978
1979static int __init sh_mobile_ceu_init(void)
1980{
1981 return platform_driver_register(&sh_mobile_ceu_driver);
1982}
1983
1984static void __exit sh_mobile_ceu_exit(void)
1985{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001986 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001987}
1988
1989module_init(sh_mobile_ceu_init);
1990module_exit(sh_mobile_ceu_exit);
1991
1992MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1993MODULE_AUTHOR("Magnus Damm");
1994MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001995MODULE_ALIAS("platform:sh_mobile_ceu");