blob: 2b24bd0de3ad47257752ea69a1e3619d229de108 [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 Liakhovetskie6226812010-03-23 11:23:31 -0300118 /* CEU offsets within scaled by the CEU camera output */
119 unsigned int ceu_left;
120 unsigned int ceu_top;
121 /* Client output, as seen by the CEU */
122 unsigned int width;
123 unsigned int height;
124 /*
125 * User window from S_CROP / G_CROP, produced by client cropping and
126 * scaling, CEU scaling and CEU cropping, mapped back onto the client
127 * input window
128 */
129 struct v4l2_rect subrect;
130 /* Camera cropping rectangle */
131 struct v4l2_rect rect;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300132 const struct soc_mbus_pixelfmt *extra_fmt;
133 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300134};
135
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300136static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
137{
138 unsigned long flags;
139
140 flags = SOCAM_MASTER |
141 SOCAM_PCLK_SAMPLE_RISING |
142 SOCAM_HSYNC_ACTIVE_HIGH |
143 SOCAM_HSYNC_ACTIVE_LOW |
144 SOCAM_VSYNC_ACTIVE_HIGH |
145 SOCAM_VSYNC_ACTIVE_LOW |
146 SOCAM_DATA_ACTIVE_HIGH;
147
148 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
149 flags |= SOCAM_DATAWIDTH_8;
150
151 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
152 flags |= SOCAM_DATAWIDTH_16;
153
154 if (flags & SOCAM_DATAWIDTH_MASK)
155 return flags;
156
157 return 0;
158}
159
Magnus Damm0d3244d2008-07-16 22:59:28 -0300160static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300161 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300162{
163 iowrite32(data, priv->base + reg_offs);
164}
165
Magnus Damm7a1a8162008-12-18 12:50:30 -0300166static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300167{
168 return ioread32(priv->base + reg_offs);
169}
170
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300171static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
172{
173 int i, success = 0;
174 struct soc_camera_device *icd = pcdev->icd;
175
176 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
177
178 /* wait CSTSR.CPTON bit */
179 for (i = 0; i < 1000; i++) {
180 if (!(ceu_read(pcdev, CSTSR) & 1)) {
181 success++;
182 break;
183 }
184 udelay(1);
185 }
186
187 /* wait CAPSR.CPKIL bit */
188 for (i = 0; i < 1000; i++) {
189 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
190 success++;
191 break;
192 }
193 udelay(1);
194 }
195
196
197 if (2 != success) {
198 dev_warn(&icd->dev, "soft reset time out\n");
199 return -EIO;
200 }
201
202 return 0;
203}
204
Magnus Damm0d3244d2008-07-16 22:59:28 -0300205/*
206 * Videobuf operations
207 */
208static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
209 unsigned int *count,
210 unsigned int *size)
211{
212 struct soc_camera_device *icd = vq->priv_data;
213 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
214 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300215 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
216 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300217
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300218 if (bytes_per_line < 0)
219 return bytes_per_line;
220
Magnus Damme6641c82009-12-11 11:53:56 -0300221 *size = bytes_per_line * icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300222
223 if (0 == *count)
224 *count = 2;
225
226 if (pcdev->video_limit) {
Andreas Bombedab7e312010-03-21 16:02:45 -0300227 if (PAGE_ALIGN(*size) * *count > pcdev->video_limit)
228 *count = pcdev->video_limit / PAGE_ALIGN(*size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300229 }
230
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300231 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300232
233 return 0;
234}
235
236static void free_buffer(struct videobuf_queue *vq,
237 struct sh_mobile_ceu_buffer *buf)
238{
239 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300240 struct device *dev = icd->dev.parent;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300241
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300242 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300243 &buf->vb, buf->vb.baddr, buf->vb.bsize);
244
245 if (in_interrupt())
246 BUG();
247
Magnus Damm97215cb2009-03-13 06:08:20 -0300248 videobuf_waiton(&buf->vb, 0, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300249 videobuf_dma_contig_free(vq, &buf->vb);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300250 dev_dbg(dev, "%s freed\n", __func__);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300251 buf->vb.state = VIDEOBUF_NEEDS_INIT;
252}
253
Magnus Damm7a1a8162008-12-18 12:50:30 -0300254#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
255#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
256#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300257#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300258#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300259#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300260
261
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300262/*
263 * return value doesn't reflex the success/failure to queue the new buffer,
264 * but rather the status of the previous buffer.
265 */
266static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300267{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300268 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300269 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300270 unsigned long top1, top2;
271 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300272 u32 status;
273 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300274
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300275 /*
276 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300277 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
278 * several not-so-well documented interrupt sources in CETCR.
279 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300280 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
281 status = ceu_read(pcdev, CETCR);
282 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
283 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300284 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
285 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300286
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300287 /*
288 * When a VBP interrupt occurs, a capture end interrupt does not occur
289 * and the image of that frame is not captured correctly. So, soft reset
290 * is needed here.
291 */
292 if (status & CEU_CEIER_VBP) {
293 sh_mobile_ceu_soft_reset(pcdev);
294 ret = -EIO;
295 }
296
Magnus Damm8be65dc2008-12-18 12:38:06 -0300297 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300298 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300299
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300300 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
301 top1 = CDBYR;
302 top2 = CDBCR;
303 bottom1 = CDAYR;
304 bottom2 = CDACR;
305 } else {
306 top1 = CDAYR;
307 top2 = CDACR;
308 bottom1 = CDBYR;
309 bottom2 = CDBCR;
310 }
311
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300312 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300313 ceu_write(pcdev, top1, phys_addr_top);
314 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300315 phys_addr_bottom = phys_addr_top + icd->user_width;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300316 ceu_write(pcdev, bottom1, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300317 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300318
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300319 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300320 case V4L2_PIX_FMT_NV12:
321 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300322 case V4L2_PIX_FMT_NV16:
323 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300324 phys_addr_top += icd->user_width *
325 icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300326 ceu_write(pcdev, top2, phys_addr_top);
327 if (V4L2_FIELD_NONE != pcdev->field) {
328 phys_addr_bottom = phys_addr_top + icd->user_width;
329 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300330 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300331 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300332
333 pcdev->active->state = VIDEOBUF_ACTIVE;
334 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300335
336 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300337}
338
339static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
340 struct videobuf_buffer *vb,
341 enum v4l2_field field)
342{
343 struct soc_camera_device *icd = vq->priv_data;
344 struct sh_mobile_ceu_buffer *buf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300345 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
346 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300347 int ret;
348
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300349 if (bytes_per_line < 0)
350 return bytes_per_line;
351
Magnus Damm0d3244d2008-07-16 22:59:28 -0300352 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
353
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300354 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300355 vb, vb->baddr, vb->bsize);
356
357 /* Added list head initialization on alloc */
358 WARN_ON(!list_empty(&vb->queue));
359
360#ifdef DEBUG
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300361 /*
362 * This can be useful if you want to see if we actually fill
363 * the buffer with something
364 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300365 memset((void *)vb->baddr, 0xaa, vb->bsize);
366#endif
367
368 BUG_ON(NULL == icd->current_fmt);
369
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300370 if (buf->code != icd->current_fmt->code ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300371 vb->width != icd->user_width ||
372 vb->height != icd->user_height ||
Magnus Damm0d3244d2008-07-16 22:59:28 -0300373 vb->field != field) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300374 buf->code = icd->current_fmt->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300375 vb->width = icd->user_width;
376 vb->height = icd->user_height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300377 vb->field = field;
378 vb->state = VIDEOBUF_NEEDS_INIT;
379 }
380
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300381 vb->size = vb->height * bytes_per_line;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300382 if (0 != vb->baddr && vb->bsize < vb->size) {
383 ret = -EINVAL;
384 goto out;
385 }
386
387 if (vb->state == VIDEOBUF_NEEDS_INIT) {
388 ret = videobuf_iolock(vq, vb, NULL);
389 if (ret)
390 goto fail;
391 vb->state = VIDEOBUF_PREPARED;
392 }
393
394 return 0;
395fail:
396 free_buffer(vq, buf);
397out:
398 return ret;
399}
400
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300401/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300402static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
403 struct videobuf_buffer *vb)
404{
405 struct soc_camera_device *icd = vq->priv_data;
406 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
407 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300408
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300409 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300410 vb, vb->baddr, vb->bsize);
411
Magnus Damm51354cc2008-10-16 19:51:20 -0300412 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300413 list_add_tail(&vb->queue, &pcdev->capture);
414
415 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300416 /*
417 * Because there were no active buffer at this moment,
418 * we are not interested in the return value of
419 * sh_mobile_ceu_capture here.
420 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300421 pcdev->active = vb;
422 sh_mobile_ceu_capture(pcdev);
423 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300424}
425
426static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
427 struct videobuf_buffer *vb)
428{
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300429 struct soc_camera_device *icd = vq->priv_data;
430 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
431 struct sh_mobile_ceu_dev *pcdev = ici->priv;
432 unsigned long flags;
433
434 spin_lock_irqsave(&pcdev->lock, flags);
435
436 if (pcdev->active == vb) {
437 /* disable capture (release DMA buffer), reset */
438 ceu_write(pcdev, CAPSR, 1 << 16);
439 pcdev->active = NULL;
440 }
441
442 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
443 !list_empty(&vb->queue)) {
444 vb->state = VIDEOBUF_ERROR;
445 list_del_init(&vb->queue);
446 }
447
448 spin_unlock_irqrestore(&pcdev->lock, flags);
449
Magnus Damm0d3244d2008-07-16 22:59:28 -0300450 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
451}
452
453static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
454 .buf_setup = sh_mobile_ceu_videobuf_setup,
455 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
456 .buf_queue = sh_mobile_ceu_videobuf_queue,
457 .buf_release = sh_mobile_ceu_videobuf_release,
458};
459
460static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
461{
462 struct sh_mobile_ceu_dev *pcdev = data;
463 struct videobuf_buffer *vb;
464 unsigned long flags;
465
466 spin_lock_irqsave(&pcdev->lock, flags);
467
468 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300469 if (!vb)
470 /* Stale interrupt from a released buffer */
471 goto out;
472
Magnus Damm0d3244d2008-07-16 22:59:28 -0300473 list_del_init(&vb->queue);
474
475 if (!list_empty(&pcdev->capture))
476 pcdev->active = list_entry(pcdev->capture.next,
477 struct videobuf_buffer, queue);
478 else
479 pcdev->active = NULL;
480
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300481 vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
482 VIDEOBUF_ERROR : VIDEOBUF_DONE;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300483 do_gettimeofday(&vb->ts);
484 vb->field_count++;
485 wake_up(&vb->done);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300486
487out:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300488 spin_unlock_irqrestore(&pcdev->lock, flags);
489
490 return IRQ_HANDLED;
491}
492
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300493/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300494static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
495{
496 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
497 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300498 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300499
Magnus Damm0d3244d2008-07-16 22:59:28 -0300500 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300501 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300502
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300503 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300504 "SuperH Mobile CEU driver attached to camera %d\n",
505 icd->devnum);
506
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900507 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300508
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300509 ret = sh_mobile_ceu_soft_reset(pcdev);
510 if (!ret)
511 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300512
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300513 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300514}
515
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300516/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300517static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
518{
519 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
520 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300521 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300522
523 BUG_ON(icd != pcdev->icd);
524
525 /* disable capture, disable interrupts */
526 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300527 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300528
529 /* make sure active buffer is canceled */
530 spin_lock_irqsave(&pcdev->lock, flags);
531 if (pcdev->active) {
532 list_del(&pcdev->active->queue);
533 pcdev->active->state = VIDEOBUF_ERROR;
534 wake_up_all(&pcdev->active->done);
535 pcdev->active = NULL;
536 }
537 spin_unlock_irqrestore(&pcdev->lock, flags);
538
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900539 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300540
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300541 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300542 "SuperH Mobile CEU driver detached from camera %d\n",
543 icd->devnum);
544
545 pcdev->icd = NULL;
546}
547
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300548/*
549 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
550 * in SH7722 Hardware Manual
551 */
552static unsigned int size_dst(unsigned int src, unsigned int scale)
553{
554 unsigned int mant_pre = scale >> 12;
555 if (!src || !scale)
556 return src;
557 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
558 mant_pre * 4096 / scale + 1;
559}
560
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300561static u16 calc_scale(unsigned int src, unsigned int *dst)
562{
563 u16 scale;
564
565 if (src == *dst)
566 return 0;
567
568 scale = (src * 4096 / *dst) & ~7;
569
570 while (scale > 4096 && size_dst(src, scale) < *dst)
571 scale -= 8;
572
573 *dst = size_dst(src, scale);
574
575 return scale;
576}
577
578/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300579static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300580{
581 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
582 struct sh_mobile_ceu_cam *cam = icd->host_priv;
583 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300584 unsigned int height, width, cdwdr_width, in_width, in_height;
585 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300586 u32 camor;
587
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300588 dev_geo(icd->dev.parent, "Crop %ux%u@%u:%u\n",
589 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300590
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300591 left_offset = cam->ceu_left;
592 top_offset = cam->ceu_top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300593
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300594 /* CEU cropping (CFSZR) is applied _after_ the scaling filter (CFLCR) */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300595 if (pcdev->image_mode) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300596 in_width = cam->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300597 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300598 in_width *= 2;
599 left_offset *= 2;
600 }
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300601 width = icd->user_width;
602 cdwdr_width = icd->user_width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300603 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300604 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300605 icd->current_fmt->host_fmt);
606 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300607
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300608 width = icd->user_width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300609
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300610 switch (icd->current_fmt->host_fmt->packing) {
611 case SOC_MBUS_PACKING_2X8_PADHI:
612 w_factor = 2;
613 break;
614 default:
615 w_factor = 1;
616 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300617
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300618 in_width = cam->width * w_factor;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300619 left_offset = left_offset * w_factor;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300620
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300621 if (bytes_per_line < 0)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300622 cdwdr_width = icd->user_width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300623 else
624 cdwdr_width = bytes_per_line;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300625 }
626
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300627 height = icd->user_height;
628 in_height = cam->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300629 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300630 height /= 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300631 in_height /= 2;
632 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300633 cdwdr_width *= 2;
634 }
635
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300636 /* CSI2 special configuration */
637 if (pcdev->pdata->csi2_dev) {
638 in_width = ((in_width - 2) * 2);
639 left_offset *= 2;
640 }
641
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300642 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300643 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300644
645 dev_geo(icd->dev.parent,
646 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
647 (in_height << 16) | in_width, (height << 16) | width,
648 cdwdr_width);
649
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300650 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300651 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300652 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300653 ceu_write(pcdev, CDWDR, cdwdr_width);
654}
655
656static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
657{
658 u32 capsr = ceu_read(pcdev, CAPSR);
659 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
660 return capsr;
661}
662
663static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
664{
665 unsigned long timeout = jiffies + 10 * HZ;
666
667 /*
668 * Wait until the end of the current frame. It can take a long time,
669 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
670 */
671 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
672 msleep(1);
673
674 if (time_after(jiffies, timeout)) {
675 dev_err(pcdev->ici.v4l2_dev.dev,
676 "Timeout waiting for frame end! Interface problem?\n");
677 return;
678 }
679
680 /* Wait until reset clears, this shall not hang... */
681 while (ceu_read(pcdev, CAPSR) & (1 << 16))
682 udelay(10);
683
684 /* Anything to restore? */
685 if (capsr & ~(1 << 16))
686 ceu_write(pcdev, CAPSR, capsr);
687}
688
Magnus Damm0d3244d2008-07-16 22:59:28 -0300689static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
690 __u32 pixfmt)
691{
692 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
693 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300694 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300695 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300696 int yuv_lineskip;
697 struct sh_mobile_ceu_cam *cam = icd->host_priv;
698 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300699
700 camera_flags = icd->ops->query_bus_param(icd);
701 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300702 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300703 if (!common_flags)
704 return -EINVAL;
705
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300706 /* Make choises, based on platform preferences */
707 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
708 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
709 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
710 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
711 else
712 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
713 }
714
715 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
716 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
717 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
718 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
719 else
720 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
721 }
722
Magnus Damm0d3244d2008-07-16 22:59:28 -0300723 ret = icd->ops->set_bus_param(icd, common_flags);
724 if (ret < 0)
725 return ret;
726
727 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
728 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300729 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300730 break;
731 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300732 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300733 break;
734 default:
735 return -EINVAL;
736 }
737
738 ceu_write(pcdev, CRCNTR, 0);
739 ceu_write(pcdev, CRCMPR, 0);
740
Magnus Damm8be65dc2008-12-18 12:38:06 -0300741 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300742 yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300743
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300744 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300745 case V4L2_PIX_FMT_NV12:
746 case V4L2_PIX_FMT_NV21:
747 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300748 /* fall-through */
749 case V4L2_PIX_FMT_NV16:
750 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300751 switch (cam->code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300752 case V4L2_MBUS_FMT_UYVY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300753 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
754 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300755 case V4L2_MBUS_FMT_VYUY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300756 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
757 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300758 case V4L2_MBUS_FMT_YUYV8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300759 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
760 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300761 case V4L2_MBUS_FMT_YVYU8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300762 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
763 break;
764 default:
765 BUG();
766 }
767 }
768
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300769 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
770 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300771 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300772
Magnus Damm7a1a8162008-12-18 12:50:30 -0300773 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
774 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300775 value |= pcdev->is_16bit ? 1 << 12 : 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300776
777 /* CSI2 mode */
778 if (pcdev->pdata->csi2_dev)
779 value |= 3 << 12;
780
Magnus Damm0d3244d2008-07-16 22:59:28 -0300781 ceu_write(pcdev, CAMCR, value);
782
783 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300784
785 switch (pcdev->field) {
786 case V4L2_FIELD_INTERLACED_TB:
787 value = 0x101;
788 break;
789 case V4L2_FIELD_INTERLACED_BT:
790 value = 0x102;
791 break;
792 default:
793 value = 0;
794 break;
795 }
796 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300797
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300798 sh_mobile_ceu_set_rect(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300799 mdelay(1);
800
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300801 dev_geo(icd->dev.parent, "CFLCR 0x%x\n", pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300802 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300803
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300804 /*
805 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300806 *
807 * In data fetch mode bytes are received in chunks of 8 bytes.
808 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
809 *
810 * The data is however by default written to memory in reverse order:
811 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
812 *
813 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300814 * using 7 we swap the data bytes to match the incoming order:
815 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300816 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300817 value = 0x00000017;
818 if (yuv_lineskip)
819 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
820
821 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300822 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
823
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300824 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300825 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
826 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300827 icd->user_width, icd->user_height);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300828
829 capture_restore(pcdev, capsr);
830
Magnus Damm0d3244d2008-07-16 22:59:28 -0300831 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300832 return 0;
833}
834
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300835static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
836 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300837{
838 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
839 struct sh_mobile_ceu_dev *pcdev = ici->priv;
840 unsigned long camera_flags, common_flags;
841
842 camera_flags = icd->ops->query_bus_param(icd);
843 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300844 make_bus_param(pcdev));
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300845 if (!common_flags || buswidth > 16 ||
846 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
Magnus Damm0d3244d2008-07-16 22:59:28 -0300847 return -EINVAL;
848
849 return 0;
850}
851
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300852static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300853 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300854 .fourcc = V4L2_PIX_FMT_NV12,
855 .name = "NV12",
856 .bits_per_sample = 12,
857 .packing = SOC_MBUS_PACKING_NONE,
858 .order = SOC_MBUS_ORDER_LE,
859 }, {
860 .fourcc = V4L2_PIX_FMT_NV21,
861 .name = "NV21",
862 .bits_per_sample = 12,
863 .packing = SOC_MBUS_PACKING_NONE,
864 .order = SOC_MBUS_ORDER_LE,
865 }, {
866 .fourcc = V4L2_PIX_FMT_NV16,
867 .name = "NV16",
868 .bits_per_sample = 16,
869 .packing = SOC_MBUS_PACKING_NONE,
870 .order = SOC_MBUS_ORDER_LE,
871 }, {
872 .fourcc = V4L2_PIX_FMT_NV61,
873 .name = "NV61",
874 .bits_per_sample = 16,
875 .packing = SOC_MBUS_PACKING_NONE,
876 .order = SOC_MBUS_ORDER_LE,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300877 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300878};
879
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300880/* This will be corrected as we get more formats */
881static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
882{
883 return fmt->packing == SOC_MBUS_PACKING_NONE ||
884 (fmt->bits_per_sample == 8 &&
885 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
886 (fmt->bits_per_sample > 8 &&
887 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
888}
889
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300890static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
891
Hans Verkuil3805f202010-05-08 17:55:00 -0300892static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
Magnus Damm9414de32008-12-18 11:49:06 -0300893 struct soc_camera_format_xlate *xlate)
894{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300895 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300896 struct device *dev = icd->dev.parent;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300897 struct soc_camera_host *ici = to_soc_camera_host(dev);
898 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300899 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300900 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300901 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300902 enum v4l2_mbus_pixelcode code;
903 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -0300904
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300905 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
906 if (ret < 0)
907 /* No more formats */
908 return 0;
909
910 fmt = soc_mbus_get_fmtdesc(code);
911 if (!fmt) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300912 dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300913 return -EINVAL;
914 }
915
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300916 if (!pcdev->pdata->csi2_dev) {
917 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
918 if (ret < 0)
919 return 0;
920 }
Magnus Damm9414de32008-12-18 11:49:06 -0300921
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300922 if (!icd->host_priv) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300923 struct v4l2_mbus_framefmt mf;
924 struct v4l2_rect rect;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300925 int shift = 0;
926
927 /* FIXME: subwindow is lost between close / open */
928
929 /* Cache current client geometry */
930 ret = client_g_rect(sd, &rect);
931 if (ret < 0)
932 return ret;
933
934 /* First time */
935 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
936 if (ret < 0)
937 return ret;
938
939 while ((mf.width > 2560 || mf.height > 1920) && shift < 4) {
940 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
941 mf.width = 2560 >> shift;
942 mf.height = 1920 >> shift;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300943 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, video,
944 s_mbus_fmt, &mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300945 if (ret < 0)
946 return ret;
947 shift++;
948 }
949
950 if (shift == 4) {
951 dev_err(dev, "Failed to configure the client below %ux%x\n",
952 mf.width, mf.height);
953 return -EIO;
954 }
955
956 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
957
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300958 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
959 if (!cam)
960 return -ENOMEM;
961
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300962 /* We are called with current camera crop, initialise subrect with it */
963 cam->rect = rect;
964 cam->subrect = rect;
965
966 cam->width = mf.width;
967 cam->height = mf.height;
968
969 cam->width = mf.width;
970 cam->height = mf.height;
971
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300972 icd->host_priv = cam;
973 } else {
974 cam = icd->host_priv;
975 }
976
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300977 /* Beginning of a pass */
978 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300979 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300980
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300981 switch (code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300982 case V4L2_MBUS_FMT_UYVY8_2X8:
983 case V4L2_MBUS_FMT_VYUY8_2X8:
984 case V4L2_MBUS_FMT_YUYV8_2X8:
985 case V4L2_MBUS_FMT_YVYU8_2X8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300986 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300987 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300988
989 /*
990 * Our case is simple so far: for any of the above four camera
991 * formats we add all our four synthesized NV* formats, so,
992 * just marking the device with a single flag suffices. If
993 * the format generation rules are more complex, you would have
994 * to actually hang your already added / counted formats onto
995 * the host_priv pointer and check whether the format you're
996 * going to add now is already there.
997 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300998 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300999
Magnus Damm8be65dc2008-12-18 12:38:06 -03001000 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1001 formats += n;
1002 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001003 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1004 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001005 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001006 dev_dbg(dev, "Providing format %s using code %d\n",
1007 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -03001008 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001009 break;
Magnus Damm9414de32008-12-18 11:49:06 -03001010 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001011 if (!sh_mobile_ceu_packing_supported(fmt))
1012 return 0;
1013 }
1014
1015 /* Generic pass-through */
1016 formats++;
1017 if (xlate) {
1018 xlate->host_fmt = fmt;
1019 xlate->code = code;
1020 xlate++;
1021 dev_dbg(dev, "Providing format %s in pass-through mode\n",
Guennadi Liakhovetskie53a4802010-06-18 04:20:56 -03001022 fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -03001023 }
1024
1025 return formats;
1026}
1027
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001028static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1029{
1030 kfree(icd->host_priv);
1031 icd->host_priv = NULL;
1032}
1033
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001034/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001035static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1036{
1037 return r1->width < r2->width || r1->height < r2->height;
1038}
1039
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001040/* Check if r1 fails to cover r2 */
1041static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1042{
1043 return r1->left > r2->left || r1->top > r2->top ||
1044 r1->left + r1->width < r2->left + r2->width ||
1045 r1->top + r1->height < r2->top + r2->height;
1046}
1047
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001048static unsigned int scale_down(unsigned int size, unsigned int scale)
1049{
1050 return (size * 4096 + scale / 2) / scale;
1051}
1052
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001053static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1054{
1055 return (input * 4096 + output / 2) / output;
1056}
1057
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001058/* Get and store current client crop */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001059static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1060{
1061 struct v4l2_crop crop;
1062 struct v4l2_cropcap cap;
1063 int ret;
1064
1065 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1066
1067 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1068 if (!ret) {
1069 *rect = crop.c;
1070 return ret;
1071 }
1072
1073 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1074 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1075
1076 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001077 if (!ret)
1078 *rect = cap.defrect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001079
1080 return ret;
1081}
1082
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001083/* Client crop has changed, update our sub-rectangle to remain within the area */
1084static void update_subrect(struct sh_mobile_ceu_cam *cam)
1085{
1086 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1087
1088 if (rect->width < subrect->width)
1089 subrect->width = rect->width;
1090
1091 if (rect->height < subrect->height)
1092 subrect->height = rect->height;
1093
1094 if (rect->left > subrect->left)
1095 subrect->left = rect->left;
1096 else if (rect->left + rect->width >
1097 subrect->left + subrect->width)
1098 subrect->left = rect->left + rect->width -
1099 subrect->width;
1100
1101 if (rect->top > subrect->top)
1102 subrect->top = rect->top;
1103 else if (rect->top + rect->height >
1104 subrect->top + subrect->height)
1105 subrect->top = rect->top + rect->height -
1106 subrect->height;
1107}
1108
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001109/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001110 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001111 * 1. try if the client can produce exactly what requested by the user
1112 * 2. if (1) failed, try to double the client image until we get one big enough
1113 * 3. if (2) failed, try to request the maximum image
1114 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001115static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001116 struct v4l2_crop *cam_crop)
1117{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001118 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001119 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1120 struct device *dev = sd->v4l2_dev->dev;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001121 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001122 struct v4l2_cropcap cap;
1123 int ret;
1124 unsigned int width, height;
1125
1126 v4l2_subdev_call(sd, video, s_crop, crop);
1127 ret = client_g_rect(sd, cam_rect);
1128 if (ret < 0)
1129 return ret;
1130
1131 /*
1132 * Now cam_crop contains the current camera input rectangle, and it must
1133 * be within camera cropcap bounds
1134 */
1135 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1136 /* Even if camera S_CROP failed, but camera rectangle matches */
Márton Némethe26b3142010-02-24 17:13:29 -03001137 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001138 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001139 cam->rect = *cam_rect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001140 return 0;
1141 }
1142
1143 /* Try to fix cropping, that camera hasn't managed to set */
Márton Némethe26b3142010-02-24 17:13:29 -03001144 dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001145 cam_rect->width, cam_rect->height,
1146 cam_rect->left, cam_rect->top,
1147 rect->width, rect->height, rect->left, rect->top);
1148
1149 /* We need sensor maximum rectangle */
1150 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1151 if (ret < 0)
1152 return ret;
1153
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001154 /* Put user requested rectangle within sensor bounds */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001155 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1156 cap.bounds.width);
1157 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1158 cap.bounds.height);
1159
1160 /*
1161 * Popular special case - some cameras can only handle fixed sizes like
1162 * QVGA, VGA,... Take care to avoid infinite loop.
1163 */
1164 width = max(cam_rect->width, 2);
1165 height = max(cam_rect->height, 2);
1166
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001167 /*
1168 * Loop as long as sensor is not covering the requested rectangle and
1169 * is still within its bounds
1170 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001171 while (!ret && (is_smaller(cam_rect, rect) ||
1172 is_inside(cam_rect, rect)) &&
1173 (cap.bounds.width > width || cap.bounds.height > height)) {
1174
1175 width *= 2;
1176 height *= 2;
1177
1178 cam_rect->width = width;
1179 cam_rect->height = height;
1180
1181 /*
1182 * We do not know what capabilities the camera has to set up
1183 * left and top borders. We could try to be smarter in iterating
1184 * them, e.g., if camera current left is to the right of the
1185 * target left, set it to the middle point between the current
1186 * left and minimum left. But that would add too much
1187 * complexity: we would have to iterate each border separately.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001188 * Instead we just drop to the left and top bounds.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001189 */
1190 if (cam_rect->left > rect->left)
1191 cam_rect->left = cap.bounds.left;
1192
1193 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1194 cam_rect->width = rect->left + rect->width -
1195 cam_rect->left;
1196
1197 if (cam_rect->top > rect->top)
1198 cam_rect->top = cap.bounds.top;
1199
1200 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1201 cam_rect->height = rect->top + rect->height -
1202 cam_rect->top;
1203
1204 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1205 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001206 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001207 cam_rect->width, cam_rect->height,
1208 cam_rect->left, cam_rect->top);
1209 }
1210
1211 /* S_CROP must not modify the rectangle */
1212 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1213 /*
1214 * The camera failed to configure a suitable cropping,
1215 * we cannot use the current rectangle, set to max
1216 */
1217 *cam_rect = cap.bounds;
1218 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1219 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001220 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001221 cam_rect->width, cam_rect->height,
1222 cam_rect->left, cam_rect->top);
1223 }
1224
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001225 if (!ret) {
1226 cam->rect = *cam_rect;
1227 update_subrect(cam);
1228 }
1229
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001230 return ret;
1231}
1232
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001233/* Iterative s_mbus_fmt, also updates cached client crop on success */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001234static int client_s_fmt(struct soc_camera_device *icd,
1235 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001236{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001237 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001238 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1239 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001240 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001241 unsigned int max_width, max_height;
1242 struct v4l2_cropcap cap;
1243 int ret;
1244
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001245 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, video,
1246 s_mbus_fmt, mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001247 if (ret < 0)
1248 return ret;
1249
1250 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1251
1252 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
1253 goto update_cache;
1254
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001255 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1256
1257 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1258 if (ret < 0)
1259 return ret;
1260
1261 max_width = min(cap.bounds.width, 2560);
1262 max_height = min(cap.bounds.height, 1920);
1263
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001264 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001265 tmp_w = mf->width;
1266 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001267
1268 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1269 while ((width > tmp_w || height > tmp_h) &&
1270 tmp_w < max_width && tmp_h < max_height) {
1271 tmp_w = min(2 * tmp_w, max_width);
1272 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001273 mf->width = tmp_w;
1274 mf->height = tmp_h;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001275 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, video,
1276 s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001277 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001278 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001279 if (ret < 0) {
1280 /* This shouldn't happen */
1281 dev_err(dev, "Client failed to set format: %d\n", ret);
1282 return ret;
1283 }
1284 }
1285
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001286update_cache:
1287 /* Update cache */
1288 ret = client_g_rect(sd, &cam->rect);
1289 if (ret < 0)
1290 return ret;
1291
1292 update_subrect(cam);
1293
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001294 return 0;
1295}
1296
1297/**
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001298 * @width - on output: user width, mapped back to input
1299 * @height - on output: user height, mapped back to input
1300 * @mf - in- / output camera output window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001301 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001302static int client_scale(struct soc_camera_device *icd,
1303 struct v4l2_mbus_framefmt *mf,
1304 unsigned int *width, unsigned int *height,
1305 bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001306{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001307 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1308 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001309 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001310 unsigned int scale_h, scale_v;
1311 int ret;
1312
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001313 /*
1314 * 5. Apply iterative camera S_FMT for camera user window (also updates
1315 * client crop cache and the imaginary sub-rectangle).
1316 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001317 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001318 if (ret < 0)
1319 return ret;
1320
1321 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001322 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001323
1324 /* 6. Retrieve camera output window (g_fmt) */
1325
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001326 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001327
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001328 /* 7. Calculate new client scales. */
1329 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1330 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001331
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001332 mf->width = mf_tmp.width;
1333 mf->height = mf_tmp.height;
1334 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001335
1336 /*
1337 * 8. Calculate new CEU crop - apply camera scales to previously
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001338 * updated "effective" crop.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001339 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001340 *width = scale_down(cam->subrect.width, scale_h);
1341 *height = scale_down(cam->subrect.height, scale_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001342
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001343 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001344
1345 return 0;
1346}
1347
1348/*
1349 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1350 * framerate by always requesting the maximum image from the client. See
1351 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1352 * scaling and cropping algorithms and for the meaning of referenced here steps.
1353 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001354static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001355 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001356{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001357 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001358 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1359 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001360 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001361 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001362 struct v4l2_rect *cam_rect = &cam_crop.c;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001363 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001364 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001365 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001366 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1367 out_width, out_height, scale_h, scale_v;
1368 int interm_width, interm_height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001369 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001370 int ret;
1371
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001372 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1373 rect->left, rect->top);
1374
1375 /* During camera cropping its output window can change too, stop CEU */
1376 capsr = capture_save_reset(pcdev);
1377 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1378
1379 /* 1. - 2. Apply iterative camera S_CROP for new input window. */
1380 ret = client_s_crop(icd, a, &cam_crop);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001381 if (ret < 0)
1382 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001383
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001384 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001385 cam_rect->width, cam_rect->height,
1386 cam_rect->left, cam_rect->top);
1387
1388 /* On success cam_crop contains current camera crop */
1389
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001390 /* 3. Retrieve camera output window */
1391 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1392 if (ret < 0)
1393 return ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001394
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001395 if (mf.width > 2560 || mf.height > 1920)
1396 return -EINVAL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001397
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001398 /* Cache camera output window */
1399 cam->width = mf.width;
1400 cam->height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001401
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001402 /* 4. Calculate camera scales */
1403 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1404 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001405
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001406 /* Calculate intermediate window */
1407 interm_width = scale_down(rect->width, scale_cam_h);
1408 interm_height = scale_down(rect->height, scale_cam_v);
1409
1410 if (pcdev->image_mode) {
1411 out_width = min(interm_width, icd->user_width);
1412 out_height = min(interm_height, icd->user_height);
1413 } else {
1414 out_width = interm_width;
1415 out_height = interm_height;
1416 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001417
1418 /*
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001419 * 5. Calculate CEU scales from camera scales from results of (5) and
1420 * the user window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001421 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001422 scale_ceu_h = calc_scale(interm_width, &out_width);
1423 scale_ceu_v = calc_scale(interm_height, &out_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001424
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001425 /* Calculate camera scales */
1426 scale_h = calc_generic_scale(cam_rect->width, out_width);
1427 scale_v = calc_generic_scale(cam_rect->height, out_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001428
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001429 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001430
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001431 /* Apply CEU scales. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001432 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1433 if (cflcr != pcdev->cflcr) {
1434 pcdev->cflcr = cflcr;
1435 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001436 }
1437
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001438 icd->user_width = out_width;
1439 icd->user_height = out_height;
1440 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_h) & ~1;
1441 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_v) & ~1;
1442
1443 /* 6. Use CEU cropping to crop to the new window. */
1444 sh_mobile_ceu_set_rect(icd);
1445
1446 cam->subrect = *rect;
1447
1448 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1449 icd->user_width, icd->user_height,
1450 cam->ceu_left, cam->ceu_top);
1451
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001452 /* Restore capture */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001453 if (pcdev->active)
1454 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001455 capture_restore(pcdev, capsr);
1456
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001457 /* Even if only camera cropping succeeded */
1458 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001459}
1460
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001461static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1462 struct v4l2_crop *a)
1463{
1464 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1465
1466 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1467 a->c = cam->subrect;
1468
1469 return 0;
1470}
1471
1472/*
1473 * Calculate real client output window by applying new scales to the current
1474 * client crop. New scales are calculated from the requested output format and
1475 * CEU crop, mapped backed onto the client input (subrect).
1476 */
1477static void calculate_client_output(struct soc_camera_device *icd,
1478 struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
1479{
1480 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1481 struct device *dev = icd->dev.parent;
1482 struct v4l2_rect *cam_subrect = &cam->subrect;
1483 unsigned int scale_v, scale_h;
1484
1485 if (cam_subrect->width == cam->rect.width &&
1486 cam_subrect->height == cam->rect.height) {
1487 /* No sub-cropping */
1488 mf->width = pix->width;
1489 mf->height = pix->height;
1490 return;
1491 }
1492
1493 /* 1.-2. Current camera scales and subwin - cached. */
1494
1495 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1496 cam_subrect->width, cam_subrect->height,
1497 cam_subrect->left, cam_subrect->top);
1498
1499 /*
1500 * 3. Calculate new combined scales from input sub-window to requested
1501 * user window.
1502 */
1503
1504 /*
1505 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1506 * (128x96) or larger than VGA
1507 */
1508 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1509 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1510
1511 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1512
1513 /*
1514 * 4. Calculate client output window by applying combined scales to real
1515 * input window.
1516 */
1517 mf->width = scale_down(cam->rect.width, scale_h);
1518 mf->height = scale_down(cam->rect.height, scale_v);
1519}
1520
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001521/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001522static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001523 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001524{
Magnus Damm9414de32008-12-18 11:49:06 -03001525 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001526 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001527 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1528 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001529 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001530 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001531 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001532 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001533 /* Keep Compiler Happy */
1534 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001535 u16 scale_v, scale_h;
1536 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001537 bool image_mode;
1538 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001539
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001540 dev_geo(dev, "S_FMT(pix=0x%x, %ux%u)\n", pixfmt, pix->width, pix->height);
1541
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001542 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001543 default:
1544 pix->field = V4L2_FIELD_NONE;
1545 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001546 case V4L2_FIELD_INTERLACED_TB:
1547 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001548 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001549 field = pix->field;
1550 break;
1551 case V4L2_FIELD_INTERLACED:
1552 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001553 break;
1554 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001555
Magnus Damm9414de32008-12-18 11:49:06 -03001556 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1557 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001558 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001559 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001560 }
1561
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001562 /* 1.-4. Calculate client output geometry */
1563 calculate_client_output(icd, &f->fmt.pix, &mf);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001564 mf.field = pix->field;
1565 mf.colorspace = pix->colorspace;
1566 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001567
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001568 switch (pixfmt) {
1569 case V4L2_PIX_FMT_NV12:
1570 case V4L2_PIX_FMT_NV21:
1571 case V4L2_PIX_FMT_NV16:
1572 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001573 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001574 break;
1575 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001576 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001577 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001578
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001579 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001580
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001581 /* 5. - 9. */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001582 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001583 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001584
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001585 dev_geo(dev, "5-9: client scale return %d\n", ret);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001586
1587 /* Done with the camera. Now see if we can improve the result */
1588
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001589 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1590 mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001591 if (ret < 0)
1592 return ret;
1593
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001594 if (mf.code != xlate->code)
1595 return -EINVAL;
1596
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001597 /* 9. Prepare CEU crop */
1598 cam->width = mf.width;
1599 cam->height = mf.height;
1600
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001601 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001602
1603 /* We cannot scale up */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001604 if (pix->width > ceu_sub_width)
1605 ceu_sub_width = pix->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001606
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001607 if (pix->height > ceu_sub_height)
1608 ceu_sub_height = pix->height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001609
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001610 pix->colorspace = mf.colorspace;
1611
1612 if (image_mode) {
1613 /* Scale pix->{width x height} down to width x height */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001614 scale_h = calc_scale(ceu_sub_width, &pix->width);
1615 scale_v = calc_scale(ceu_sub_height, &pix->height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001616 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001617 pix->width = ceu_sub_width;
1618 pix->height = ceu_sub_height;
1619 scale_h = 0;
1620 scale_v = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001621 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001622
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001623 pcdev->cflcr = scale_h | (scale_v << 16);
1624
1625 /*
1626 * We have calculated CFLCR, the actual configuration will be performed
1627 * in sh_mobile_ceu_set_bus_param()
1628 */
1629
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001630 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001631 ceu_sub_width, scale_h, pix->width,
1632 ceu_sub_height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001633
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001634 cam->code = xlate->code;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001635 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001636
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001637 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001638 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001639
1640 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001641}
1642
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001643static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1644 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001645{
Magnus Damm9414de32008-12-18 11:49:06 -03001646 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001647 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001648 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001649 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001650 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001651 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001652 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001653
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001654 dev_geo(icd->dev.parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1655 pixfmt, pix->width, pix->height);
1656
Magnus Damm9414de32008-12-18 11:49:06 -03001657 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1658 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001659 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001660 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001661 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001662
Magnus Damm0d3244d2008-07-16 22:59:28 -03001663 /* FIXME: calculate using depth and bus width */
1664
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001665 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1666 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001667
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001668 width = pix->width;
1669 height = pix->height;
1670
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001671 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
Dan Carpenter981cbef2010-04-07 06:41:14 -03001672 if ((int)pix->bytesperline < 0)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001673 return pix->bytesperline;
1674 pix->sizeimage = height * pix->bytesperline;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001675
Magnus Damm0d3244d2008-07-16 22:59:28 -03001676 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001677 mf.width = pix->width;
1678 mf.height = pix->height;
1679 mf.field = pix->field;
1680 mf.code = xlate->code;
1681 mf.colorspace = pix->colorspace;
1682
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001683 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001684 if (ret < 0)
1685 return ret;
1686
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001687 pix->width = mf.width;
1688 pix->height = mf.height;
1689 pix->field = mf.field;
1690 pix->colorspace = mf.colorspace;
1691
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001692 switch (pixfmt) {
1693 case V4L2_PIX_FMT_NV12:
1694 case V4L2_PIX_FMT_NV21:
1695 case V4L2_PIX_FMT_NV16:
1696 case V4L2_PIX_FMT_NV61:
1697 /* FIXME: check against rect_max after converting soc-camera */
1698 /* We can scale precisely, need a bigger image from camera */
1699 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001700 /*
1701 * We presume, the sensor behaves sanely, i.e., if
1702 * requested a bigger rectangle, it will not return a
1703 * smaller one.
1704 */
1705 mf.width = 2560;
1706 mf.height = 1920;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001707 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, video,
1708 try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001709 if (ret < 0) {
1710 /* Shouldn't actually happen... */
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001711 dev_err(icd->dev.parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001712 "FIXME: client try_fmt() = %d\n", ret);
1713 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001714 }
1715 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001716 /* We will scale exactly */
1717 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001718 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001719 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001720 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001721 }
1722
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001723 dev_geo(icd->dev.parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1724 __func__, ret, pix->pixelformat, pix->width, pix->height);
1725
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001726 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001727}
1728
1729static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1730 struct v4l2_requestbuffers *p)
1731{
1732 int i;
1733
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001734 /*
1735 * This is for locking debugging only. I removed spinlocks and now I
Magnus Damm0d3244d2008-07-16 22:59:28 -03001736 * check whether .prepare is ever called on a linked buffer, or whether
1737 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001738 * it hadn't triggered
1739 */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001740 for (i = 0; i < p->count; i++) {
1741 struct sh_mobile_ceu_buffer *buf;
1742
1743 buf = container_of(icf->vb_vidq.bufs[i],
1744 struct sh_mobile_ceu_buffer, vb);
1745 INIT_LIST_HEAD(&buf->vb.queue);
1746 }
1747
1748 return 0;
1749}
1750
1751static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1752{
1753 struct soc_camera_file *icf = file->private_data;
1754 struct sh_mobile_ceu_buffer *buf;
1755
1756 buf = list_entry(icf->vb_vidq.stream.next,
1757 struct sh_mobile_ceu_buffer, vb.stream);
1758
1759 poll_wait(file, &buf->vb.done, pt);
1760
1761 if (buf->vb.state == VIDEOBUF_DONE ||
1762 buf->vb.state == VIDEOBUF_ERROR)
1763 return POLLIN|POLLRDNORM;
1764
1765 return 0;
1766}
1767
1768static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1769 struct v4l2_capability *cap)
1770{
1771 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1772 cap->version = KERNEL_VERSION(0, 0, 5);
1773 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1774 return 0;
1775}
1776
1777static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1778 struct soc_camera_device *icd)
1779{
1780 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1781 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1782
1783 videobuf_queue_dma_contig_init(q,
1784 &sh_mobile_ceu_videobuf_ops,
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001785 icd->dev.parent, &pcdev->lock,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001786 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001787 pcdev->field,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001788 sizeof(struct sh_mobile_ceu_buffer),
1789 icd);
1790}
1791
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001792static int sh_mobile_ceu_get_parm(struct soc_camera_device *icd,
1793 struct v4l2_streamparm *parm)
1794{
1795 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1796
1797 return v4l2_subdev_call(sd, video, g_parm, parm);
1798}
1799
1800static int sh_mobile_ceu_set_parm(struct soc_camera_device *icd,
1801 struct v4l2_streamparm *parm)
1802{
1803 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1804
1805 return v4l2_subdev_call(sd, video, s_parm, parm);
1806}
1807
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001808static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1809 struct v4l2_control *ctrl)
1810{
1811 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1812 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1813 u32 val;
1814
1815 switch (ctrl->id) {
1816 case V4L2_CID_SHARPNESS:
1817 val = ceu_read(pcdev, CLFCR);
1818 ctrl->value = val ^ 1;
1819 return 0;
1820 }
1821 return -ENOIOCTLCMD;
1822}
1823
1824static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1825 struct v4l2_control *ctrl)
1826{
1827 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1828 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1829
1830 switch (ctrl->id) {
1831 case V4L2_CID_SHARPNESS:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001832 switch (icd->current_fmt->host_fmt->fourcc) {
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001833 case V4L2_PIX_FMT_NV12:
1834 case V4L2_PIX_FMT_NV21:
1835 case V4L2_PIX_FMT_NV16:
1836 case V4L2_PIX_FMT_NV61:
1837 ceu_write(pcdev, CLFCR, !ctrl->value);
1838 return 0;
1839 }
1840 return -EINVAL;
1841 }
1842 return -ENOIOCTLCMD;
1843}
1844
1845static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1846 {
1847 .id = V4L2_CID_SHARPNESS,
1848 .type = V4L2_CTRL_TYPE_BOOLEAN,
1849 .name = "Low-pass filter",
1850 .minimum = 0,
1851 .maximum = 1,
1852 .step = 1,
1853 .default_value = 0,
1854 },
1855};
1856
Magnus Damm0d3244d2008-07-16 22:59:28 -03001857static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1858 .owner = THIS_MODULE,
1859 .add = sh_mobile_ceu_add_device,
1860 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03001861 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001862 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001863 .get_crop = sh_mobile_ceu_get_crop,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001864 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001865 .set_fmt = sh_mobile_ceu_set_fmt,
1866 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001867 .set_ctrl = sh_mobile_ceu_set_ctrl,
1868 .get_ctrl = sh_mobile_ceu_get_ctrl,
Guennadi Liakhovetskid07602a2010-02-09 18:00:38 +01001869 .set_parm = sh_mobile_ceu_set_parm,
1870 .get_parm = sh_mobile_ceu_get_parm,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001871 .reqbufs = sh_mobile_ceu_reqbufs,
1872 .poll = sh_mobile_ceu_poll,
1873 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001874 .set_bus_param = sh_mobile_ceu_set_bus_param,
1875 .init_videobuf = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001876 .controls = sh_mobile_ceu_controls,
1877 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001878};
1879
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001880struct bus_wait {
1881 struct notifier_block notifier;
1882 struct completion completion;
1883 struct device *dev;
1884};
1885
1886static int bus_notify(struct notifier_block *nb,
1887 unsigned long action, void *data)
1888{
1889 struct device *dev = data;
1890 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1891
1892 if (wait->dev != dev)
1893 return NOTIFY_DONE;
1894
1895 switch (action) {
1896 case BUS_NOTIFY_UNBOUND_DRIVER:
1897 /* Protect from module unloading */
1898 wait_for_completion(&wait->completion);
1899 return NOTIFY_OK;
1900 }
1901 return NOTIFY_DONE;
1902}
1903
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001904static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001905{
1906 struct sh_mobile_ceu_dev *pcdev;
1907 struct resource *res;
1908 void __iomem *base;
1909 unsigned int irq;
1910 int err = 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001911 struct bus_wait wait = {
1912 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1913 .notifier.notifier_call = bus_notify,
1914 };
1915 struct device *csi2;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001916
1917 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1918 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03001919 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03001920 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1921 err = -ENODEV;
1922 goto exit;
1923 }
1924
1925 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1926 if (!pcdev) {
1927 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1928 err = -ENOMEM;
1929 goto exit;
1930 }
1931
Magnus Damm0d3244d2008-07-16 22:59:28 -03001932 INIT_LIST_HEAD(&pcdev->capture);
1933 spin_lock_init(&pcdev->lock);
1934
1935 pcdev->pdata = pdev->dev.platform_data;
1936 if (!pcdev->pdata) {
1937 err = -EINVAL;
1938 dev_err(&pdev->dev, "CEU platform data not set.\n");
1939 goto exit_kfree;
1940 }
1941
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001942 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001943 if (!base) {
1944 err = -ENXIO;
1945 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1946 goto exit_kfree;
1947 }
1948
1949 pcdev->irq = irq;
1950 pcdev->base = base;
1951 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001952
1953 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1954 if (res) {
1955 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1956 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001957 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001958 DMA_MEMORY_MAP |
1959 DMA_MEMORY_EXCLUSIVE);
1960 if (!err) {
1961 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1962 err = -ENXIO;
1963 goto exit_iounmap;
1964 }
1965
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001966 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001967 }
1968
1969 /* request irq */
1970 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03001971 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001972 if (err) {
1973 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1974 goto exit_release_mem;
1975 }
1976
Magnus Damm6d1386c2009-08-14 10:49:17 +00001977 pm_suspend_ignore_children(&pdev->dev, true);
1978 pm_runtime_enable(&pdev->dev);
1979 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09001980
Magnus Damm0d3244d2008-07-16 22:59:28 -03001981 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001982 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001983 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03001984 pcdev->ici.drv_name = dev_name(&pdev->dev);
1985 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001986
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001987 /* CSI2 interfacing */
1988 csi2 = pcdev->pdata->csi2_dev;
1989 if (csi2) {
1990 wait.dev = csi2;
1991
1992 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
1993 if (err < 0)
1994 goto exit_free_clk;
1995
1996 /*
1997 * From this point the driver module will not unload, until
1998 * we complete the completion.
1999 */
2000
2001 if (!csi2->driver || !csi2->driver->owner) {
2002 complete(&wait.completion);
2003 /* Either too late, or probing failed */
2004 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2005 err = -ENXIO;
2006 goto exit_free_clk;
2007 }
2008
2009 /*
2010 * The module is still loaded, in the worst case it is hanging
2011 * in device release on our completion. So, _now_ dereferencing
2012 * the "owner" is safe!
2013 */
2014
2015 err = try_module_get(csi2->driver->owner);
2016
2017 /* Let notifier complete, if it has been locked */
2018 complete(&wait.completion);
2019 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2020 if (!err) {
2021 err = -ENODEV;
2022 goto exit_free_clk;
2023 }
2024 }
2025
Magnus Damm0d3244d2008-07-16 22:59:28 -03002026 err = soc_camera_host_register(&pcdev->ici);
2027 if (err)
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002028 goto exit_module_put;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002029
2030 return 0;
2031
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002032exit_module_put:
2033 if (csi2 && csi2->driver)
2034 module_put(csi2->driver->owner);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002035exit_free_clk:
2036 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002037 free_irq(pcdev->irq, pcdev);
2038exit_release_mem:
2039 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2040 dma_release_declared_memory(&pdev->dev);
2041exit_iounmap:
2042 iounmap(base);
2043exit_kfree:
2044 kfree(pcdev);
2045exit:
2046 return err;
2047}
2048
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002049static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002050{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002051 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2052 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2053 struct sh_mobile_ceu_dev, ici);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002054 struct device *csi2 = pcdev->pdata->csi2_dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002055
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002056 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002057 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002058 free_irq(pcdev->irq, pcdev);
2059 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2060 dma_release_declared_memory(&pdev->dev);
2061 iounmap(pcdev->base);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002062 if (csi2 && csi2->driver)
2063 module_put(csi2->driver->owner);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002064 kfree(pcdev);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002065
Magnus Damm0d3244d2008-07-16 22:59:28 -03002066 return 0;
2067}
2068
Magnus Damm6d1386c2009-08-14 10:49:17 +00002069static int sh_mobile_ceu_runtime_nop(struct device *dev)
2070{
2071 /* Runtime PM callback shared between ->runtime_suspend()
2072 * and ->runtime_resume(). Simply returns success.
2073 *
2074 * This driver re-initializes all registers after
2075 * pm_runtime_get_sync() anyway so there is no need
2076 * to save and restore registers here.
2077 */
2078 return 0;
2079}
2080
Alexey Dobriyan47145212009-12-14 18:00:08 -08002081static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00002082 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2083 .runtime_resume = sh_mobile_ceu_runtime_nop,
2084};
2085
Magnus Damm0d3244d2008-07-16 22:59:28 -03002086static struct platform_driver sh_mobile_ceu_driver = {
2087 .driver = {
2088 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00002089 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002090 },
2091 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03002092 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002093};
2094
2095static int __init sh_mobile_ceu_init(void)
2096{
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002097 /* Whatever return code */
2098 request_module("sh_mobile_csi2");
Magnus Damm0d3244d2008-07-16 22:59:28 -03002099 return platform_driver_register(&sh_mobile_ceu_driver);
2100}
2101
2102static void __exit sh_mobile_ceu_exit(void)
2103{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03002104 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002105}
2106
2107module_init(sh_mobile_ceu_init);
2108module_exit(sh_mobile_ceu_exit);
2109
2110MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2111MODULE_AUTHOR("Magnus Damm");
2112MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03002113MODULE_ALIAS("platform:sh_mobile_ceu");