blob: 2ffeb21dfe33e9319e836db2fb3a612562b788d7 [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>
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -030020#include <linux/completion.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030021#include <linux/delay.h>
22#include <linux/dma-mapping.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/interrupt.h>
26#include <linux/kernel.h>
27#include <linux/mm.h>
28#include <linux/moduleparam.h>
29#include <linux/time.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>
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -030041#include <media/sh_mobile_csi2.h>
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -030042#include <media/videobuf2-dma-contig.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030043#include <media/v4l2-mediabus.h>
44#include <media/soc_mediabus.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030045
46/* register offsets for sh7722 / sh7723 */
47
Magnus Dammdd542032008-10-16 19:50:22 -030048#define CAPSR 0x00 /* Capture start register */
49#define CAPCR 0x04 /* Capture control register */
50#define CAMCR 0x08 /* Capture interface control register */
51#define CMCYR 0x0c /* Capture interface cycle register */
52#define CAMOR 0x10 /* Capture interface offset register */
53#define CAPWR 0x14 /* Capture interface width register */
54#define CAIFR 0x18 /* Capture interface input format register */
55#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
56#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
57#define CRCNTR 0x28 /* CEU register control register */
58#define CRCMPR 0x2c /* CEU register forcible control register */
59#define CFLCR 0x30 /* Capture filter control register */
60#define CFSZR 0x34 /* Capture filter size clip register */
61#define CDWDR 0x38 /* Capture destination width register */
62#define CDAYR 0x3c /* Capture data address Y register */
63#define CDACR 0x40 /* Capture data address C register */
64#define CDBYR 0x44 /* Capture data bottom-field address Y register */
65#define CDBCR 0x48 /* Capture data bottom-field address C register */
66#define CBDSR 0x4c /* Capture bundle destination size register */
67#define CFWCR 0x5c /* Firewall operation control register */
68#define CLFCR 0x60 /* Capture low-pass filter control register */
69#define CDOCR 0x64 /* Capture data output control register */
70#define CDDCR 0x68 /* Capture data complexity level register */
71#define CDDAR 0x6c /* Capture data complexity level address register */
72#define CEIER 0x70 /* Capture event interrupt enable register */
73#define CETCR 0x74 /* Capture event flag clear register */
74#define CSTSR 0x7c /* Capture status register */
75#define CSRTR 0x80 /* Capture software reset register */
76#define CDSSR 0x84 /* Capture data size register */
77#define CDAYR2 0x90 /* Capture data address Y register 2 */
78#define CDACR2 0x94 /* Capture data address C register 2 */
79#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
80#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030081
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030082#undef DEBUG_GEOMETRY
83#ifdef DEBUG_GEOMETRY
84#define dev_geo dev_info
85#else
86#define dev_geo dev_dbg
87#endif
88
Magnus Damm0d3244d2008-07-16 22:59:28 -030089/* per video frame buffer */
90struct sh_mobile_ceu_buffer {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -030091 struct vb2_buffer vb; /* v4l buffer must be first */
92 struct list_head queue;
Magnus Damm0d3244d2008-07-16 22:59:28 -030093};
94
95struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030096 struct soc_camera_host ici;
97 struct soc_camera_device *icd;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -030098 struct platform_device *csi2_pdev;
Magnus Damm0d3244d2008-07-16 22:59:28 -030099
100 unsigned int irq;
101 void __iomem *base;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300102 size_t video_limit;
103 size_t buf_total;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300104
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300105 spinlock_t lock; /* Protects video buffer lists */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300106 struct list_head capture;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300107 struct vb2_buffer *active;
108 struct vb2_alloc_ctx *alloc_ctx;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300109
110 struct sh_mobile_ceu_info *pdata;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300111 struct completion complete;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300112
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300113 u32 cflcr;
114
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -0300115 /* static max sizes either from platform data or default */
116 int max_width;
117 int max_height;
118
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300119 enum v4l2_field field;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300120 int sequence;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300121
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300122 unsigned int image_mode:1;
123 unsigned int is_16bit:1;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300124 unsigned int frozen:1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300125};
126
127struct sh_mobile_ceu_cam {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300128 /* CEU offsets within the camera output, before the CEU scaler */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300129 unsigned int ceu_left;
130 unsigned int ceu_top;
131 /* Client output, as seen by the CEU */
132 unsigned int width;
133 unsigned int height;
134 /*
135 * User window from S_CROP / G_CROP, produced by client cropping and
136 * scaling, CEU scaling and CEU cropping, mapped back onto the client
137 * input window
138 */
139 struct v4l2_rect subrect;
140 /* Camera cropping rectangle */
141 struct v4l2_rect rect;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300142 const struct soc_mbus_pixelfmt *extra_fmt;
143 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300144};
145
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300146static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
147{
148 return container_of(vb, struct sh_mobile_ceu_buffer, vb);
149}
150
Magnus Damm0d3244d2008-07-16 22:59:28 -0300151static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300152 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300153{
154 iowrite32(data, priv->base + reg_offs);
155}
156
Magnus Damm7a1a8162008-12-18 12:50:30 -0300157static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300158{
159 return ioread32(priv->base + reg_offs);
160}
161
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300162static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
163{
164 int i, success = 0;
165 struct soc_camera_device *icd = pcdev->icd;
166
167 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
168
169 /* wait CSTSR.CPTON bit */
170 for (i = 0; i < 1000; i++) {
171 if (!(ceu_read(pcdev, CSTSR) & 1)) {
172 success++;
173 break;
174 }
175 udelay(1);
176 }
177
178 /* wait CAPSR.CPKIL bit */
179 for (i = 0; i < 1000; i++) {
180 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
181 success++;
182 break;
183 }
184 udelay(1);
185 }
186
187
188 if (2 != success) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300189 dev_warn(icd->pdev, "soft reset time out\n");
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300190 return -EIO;
191 }
192
193 return 0;
194}
195
Magnus Damm0d3244d2008-07-16 22:59:28 -0300196/*
197 * Videobuf operations
198 */
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300199
200/*
201 * .queue_setup() is called to check, whether the driver can accept the
202 * requested number of buffers and to fill in plane sizes
203 * for the current frame format if required
204 */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300205static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300206 const struct v4l2_format *fmt,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300207 unsigned int *count, unsigned int *num_planes,
Marek Szyprowski035aa142011-08-24 06:43:36 -0300208 unsigned int sizes[], void *alloc_ctxs[])
Magnus Damm0d3244d2008-07-16 22:59:28 -0300209{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300210 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300211 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300212 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300213
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300214 if (fmt) {
215 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
216 fmt->fmt.pix.pixelformat);
Laurent Pinchartbed8d802012-03-21 08:03:26 -0300217 unsigned int bytes_per_line;
218 int ret;
Laurent Pinchart2b61d46e2012-03-21 08:03:21 -0300219
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300220 if (!xlate)
221 return -EINVAL;
Laurent Pinchart2b61d46e2012-03-21 08:03:21 -0300222
Laurent Pinchartbed8d802012-03-21 08:03:26 -0300223 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
224 xlate->host_fmt);
225 if (ret < 0)
226 return ret;
Laurent Pinchart2b61d46e2012-03-21 08:03:21 -0300227
Laurent Pinchartbed8d802012-03-21 08:03:26 -0300228 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
229
230 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
231 fmt->fmt.pix.height);
232 if (ret < 0)
233 return ret;
234
235 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300236 } else {
237 /* Called from VIDIOC_REQBUFS or in compatibility mode */
Laurent Pinchart2b61d46e2012-03-21 08:03:21 -0300238 sizes[0] = icd->sizeimage;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300239 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300240
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300241 alloc_ctxs[0] = pcdev->alloc_ctx;
242
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300243 if (!vq->num_buffers)
244 pcdev->sequence = 0;
245
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300246 if (!*count)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300247 *count = 2;
248
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300249 /* If *num_planes != 0, we have already verified *count. */
250 if (pcdev->video_limit && !*num_planes) {
251 size_t size = PAGE_ALIGN(sizes[0]) * *count;
252
253 if (size + pcdev->buf_total > pcdev->video_limit)
254 *count = (pcdev->video_limit - pcdev->buf_total) /
255 PAGE_ALIGN(sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300256 }
257
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300258 *num_planes = 1;
259
Marek Szyprowski035aa142011-08-24 06:43:36 -0300260 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300261
262 return 0;
263}
264
Magnus Damm7a1a8162008-12-18 12:50:30 -0300265#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
266#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
267#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300268#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300269#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300270#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300271
272
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300273/*
274 * return value doesn't reflex the success/failure to queue the new buffer,
275 * but rather the status of the previous buffer.
276 */
277static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300278{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300279 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300280 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300281 unsigned long top1, top2;
282 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300283 u32 status;
Guennadi Liakhovetski15f517e2011-07-26 11:03:37 -0300284 bool planar;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300285 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300286
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300287 /*
288 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300289 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
290 * several not-so-well documented interrupt sources in CETCR.
291 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300292 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
293 status = ceu_read(pcdev, CETCR);
294 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300295 if (!pcdev->frozen)
296 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300297 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
298 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300299
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300300 /*
301 * When a VBP interrupt occurs, a capture end interrupt does not occur
302 * and the image of that frame is not captured correctly. So, soft reset
303 * is needed here.
304 */
305 if (status & CEU_CEIER_VBP) {
306 sh_mobile_ceu_soft_reset(pcdev);
307 ret = -EIO;
308 }
309
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300310 if (pcdev->frozen) {
311 complete(&pcdev->complete);
312 return ret;
313 }
314
Magnus Damm8be65dc2008-12-18 12:38:06 -0300315 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300316 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300317
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300318 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
319 top1 = CDBYR;
320 top2 = CDBCR;
321 bottom1 = CDAYR;
322 bottom2 = CDACR;
323 } else {
324 top1 = CDAYR;
325 top2 = CDACR;
326 bottom1 = CDBYR;
327 bottom2 = CDBCR;
328 }
329
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300330 phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300331
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300332 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300333 case V4L2_PIX_FMT_NV12:
334 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300335 case V4L2_PIX_FMT_NV16:
336 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski15f517e2011-07-26 11:03:37 -0300337 planar = true;
338 break;
339 default:
340 planar = false;
341 }
342
343 ceu_write(pcdev, top1, phys_addr_top);
344 if (V4L2_FIELD_NONE != pcdev->field) {
Laurent Pinchartfc13baf2012-03-21 08:16:05 -0300345 phys_addr_bottom = phys_addr_top + icd->bytesperline;
Guennadi Liakhovetski15f517e2011-07-26 11:03:37 -0300346 ceu_write(pcdev, bottom1, phys_addr_bottom);
347 }
348
349 if (planar) {
Laurent Pinchartfc13baf2012-03-21 08:16:05 -0300350 phys_addr_top += icd->bytesperline * icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300351 ceu_write(pcdev, top2, phys_addr_top);
352 if (V4L2_FIELD_NONE != pcdev->field) {
Laurent Pinchartfc13baf2012-03-21 08:16:05 -0300353 phys_addr_bottom = phys_addr_top + icd->bytesperline;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300354 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300355 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300356 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300357
Magnus Damm8be65dc2008-12-18 12:38:06 -0300358 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300359
360 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300361}
362
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300363static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300364{
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300365 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300366
367 /* Added list head initialization on alloc */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300368 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300369
Magnus Damm0d3244d2008-07-16 22:59:28 -0300370 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300371}
372
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300373static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300374{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300375 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300376 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300377 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300378 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300379 unsigned long size;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300380
Laurent Pinchart2b61d46e2012-03-21 08:03:21 -0300381 size = icd->sizeimage;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300382
383 if (vb2_plane_size(vb, 0) < size) {
384 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
385 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
386 goto error;
387 }
388
389 vb2_set_plane_payload(vb, 0, size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300390
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300391 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300392 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300393
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300394#ifdef DEBUG
395 /*
396 * This can be useful if you want to see if we actually fill
397 * the buffer with something
398 */
399 if (vb2_plane_vaddr(vb, 0))
400 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
401#endif
402
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300403 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300404 list_add_tail(&buf->queue, &pcdev->capture);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300405
406 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300407 /*
408 * Because there were no active buffer at this moment,
409 * we are not interested in the return value of
410 * sh_mobile_ceu_capture here.
411 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300412 pcdev->active = vb;
413 sh_mobile_ceu_capture(pcdev);
414 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300415 spin_unlock_irq(&pcdev->lock);
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300416
417 return;
418
419error:
420 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300421}
422
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300423static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300424{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300425 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300426 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300427 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300428 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300429
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300430 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300431
432 if (pcdev->active == vb) {
433 /* disable capture (release DMA buffer), reset */
434 ceu_write(pcdev, CAPSR, 1 << 16);
435 pcdev->active = NULL;
436 }
437
Guennadi Liakhovetskicb74cf52011-07-04 12:15:14 -0300438 /*
439 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
440 * buffer failed, and .buf_init() hasn't been called
441 */
442 if (buf->queue.next)
443 list_del_init(&buf->queue);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300444
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300445 pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
446 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
447 pcdev->buf_total);
448
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300449 spin_unlock_irq(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300450}
451
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300452static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
453{
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300454 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
455 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
456 struct sh_mobile_ceu_dev *pcdev = ici->priv;
457
458 pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
459 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
460 pcdev->buf_total);
461
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300462 /* This is for locking debugging only */
463 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
464 return 0;
465}
466
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300467static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
468{
469 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300470 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300471 struct sh_mobile_ceu_dev *pcdev = ici->priv;
472 struct list_head *buf_head, *tmp;
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300473
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300474 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300475
476 pcdev->active = NULL;
477
478 list_for_each_safe(buf_head, tmp, &pcdev->capture)
479 list_del_init(buf_head);
480
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300481 spin_unlock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300482
483 return sh_mobile_ceu_soft_reset(pcdev);
484}
485
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300486static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
487 .queue_setup = sh_mobile_ceu_videobuf_setup,
488 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
489 .buf_queue = sh_mobile_ceu_videobuf_queue,
490 .buf_cleanup = sh_mobile_ceu_videobuf_release,
491 .buf_init = sh_mobile_ceu_videobuf_init,
492 .wait_prepare = soc_camera_unlock,
493 .wait_finish = soc_camera_lock,
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300494 .stop_streaming = sh_mobile_ceu_stop_streaming,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300495};
496
497static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
498{
499 struct sh_mobile_ceu_dev *pcdev = data;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300500 struct vb2_buffer *vb;
501 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300502
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300503 spin_lock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300504
505 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300506 if (!vb)
507 /* Stale interrupt from a released buffer */
508 goto out;
509
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300510 list_del_init(&to_ceu_vb(vb)->queue);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300511
512 if (!list_empty(&pcdev->capture))
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300513 pcdev->active = &list_entry(pcdev->capture.next,
514 struct sh_mobile_ceu_buffer, queue)->vb;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300515 else
516 pcdev->active = NULL;
517
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300518 ret = sh_mobile_ceu_capture(pcdev);
519 do_gettimeofday(&vb->v4l2_buf.timestamp);
520 if (!ret) {
521 vb->v4l2_buf.field = pcdev->field;
522 vb->v4l2_buf.sequence = pcdev->sequence++;
523 }
524 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300525
526out:
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300527 spin_unlock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300528
529 return IRQ_HANDLED;
530}
531
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300532static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev)
533{
534 struct v4l2_subdev *sd;
535
536 if (!pcdev->csi2_pdev)
537 return NULL;
538
539 v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev)
540 if (&pcdev->csi2_pdev->dev == v4l2_get_subdevdata(sd))
541 return sd;
542
543 return NULL;
544}
545
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300546/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300547static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
548{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300549 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300550 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300551 struct v4l2_subdev *csi2_sd;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300552 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300553
Magnus Damm0d3244d2008-07-16 22:59:28 -0300554 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300555 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300556
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300557 dev_info(icd->parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300558 "SuperH Mobile CEU driver attached to camera %d\n",
559 icd->devnum);
560
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900561 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300562
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300563 pcdev->buf_total = 0;
564
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300565 ret = sh_mobile_ceu_soft_reset(pcdev);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300566
567 csi2_sd = find_csi2(pcdev);
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -0300568 if (csi2_sd) {
569 csi2_sd->grp_id = soc_camera_grp_id(icd);
570 v4l2_set_subdev_hostdata(csi2_sd, icd);
571 }
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300572
573 ret = v4l2_subdev_call(csi2_sd, core, s_power, 1);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300574 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300575 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300576 return ret;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300577 }
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300578
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300579 /*
580 * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
581 * has not found this soc-camera device among its clients
582 */
583 if (ret == -ENODEV && csi2_sd)
584 csi2_sd->grp_id = 0;
585 pcdev->icd = icd;
586
587 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300588}
589
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300590/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300591static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
592{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300593 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300594 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300595 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300596
597 BUG_ON(icd != pcdev->icd);
598
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300599 v4l2_subdev_call(csi2_sd, core, s_power, 0);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300600 if (csi2_sd)
601 csi2_sd->grp_id = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300602 /* disable capture, disable interrupts */
603 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300604 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300605
606 /* make sure active buffer is canceled */
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300607 spin_lock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300608 if (pcdev->active) {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300609 list_del_init(&to_ceu_vb(pcdev->active)->queue);
610 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
Magnus Damm51354cc2008-10-16 19:51:20 -0300611 pcdev->active = NULL;
612 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300613 spin_unlock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300614
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900615 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300616
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300617 dev_info(icd->parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300618 "SuperH Mobile CEU driver detached from camera %d\n",
619 icd->devnum);
620
621 pcdev->icd = NULL;
622}
623
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300624/*
625 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
626 * in SH7722 Hardware Manual
627 */
628static unsigned int size_dst(unsigned int src, unsigned int scale)
629{
630 unsigned int mant_pre = scale >> 12;
631 if (!src || !scale)
632 return src;
633 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
634 mant_pre * 4096 / scale + 1;
635}
636
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300637static u16 calc_scale(unsigned int src, unsigned int *dst)
638{
639 u16 scale;
640
641 if (src == *dst)
642 return 0;
643
644 scale = (src * 4096 / *dst) & ~7;
645
646 while (scale > 4096 && size_dst(src, scale) < *dst)
647 scale -= 8;
648
649 *dst = size_dst(src, scale);
650
651 return scale;
652}
653
654/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300655static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300656{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300657 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300658 struct sh_mobile_ceu_cam *cam = icd->host_priv;
659 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300660 unsigned int height, width, cdwdr_width, in_width, in_height;
661 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300662 u32 camor;
663
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300664 dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300665 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300666
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300667 left_offset = cam->ceu_left;
668 top_offset = cam->ceu_top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300669
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300670 WARN_ON(icd->user_width & 3 || icd->user_height & 3);
671
672 width = icd->user_width;
673
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300674 if (pcdev->image_mode) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300675 in_width = cam->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300676 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300677 in_width *= 2;
678 left_offset *= 2;
679 }
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300680 } else {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300681 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300682
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300683 switch (icd->current_fmt->host_fmt->packing) {
684 case SOC_MBUS_PACKING_2X8_PADHI:
685 w_factor = 2;
686 break;
687 default:
688 w_factor = 1;
689 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300690
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300691 in_width = cam->width * w_factor;
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300692 left_offset *= w_factor;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300693 }
694
Laurent Pinchartfc13baf2012-03-21 08:16:05 -0300695 cdwdr_width = icd->bytesperline;
696
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300697 height = icd->user_height;
698 in_height = cam->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300699 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300700 height = (height / 2) & ~3;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300701 in_height /= 2;
702 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300703 cdwdr_width *= 2;
704 }
705
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300706 /* CSI2 special configuration */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300707 if (pcdev->pdata->csi2) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300708 in_width = ((in_width - 2) * 2);
709 left_offset *= 2;
710 }
711
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300712 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300713 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300714
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300715 dev_geo(icd->parent,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300716 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
717 (in_height << 16) | in_width, (height << 16) | width,
718 cdwdr_width);
719
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300720 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300721 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300722 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300723 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300724 ceu_write(pcdev, CDWDR, cdwdr_width);
725}
726
727static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
728{
729 u32 capsr = ceu_read(pcdev, CAPSR);
730 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
731 return capsr;
732}
733
734static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
735{
736 unsigned long timeout = jiffies + 10 * HZ;
737
738 /*
739 * Wait until the end of the current frame. It can take a long time,
740 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
741 */
742 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
743 msleep(1);
744
745 if (time_after(jiffies, timeout)) {
746 dev_err(pcdev->ici.v4l2_dev.dev,
747 "Timeout waiting for frame end! Interface problem?\n");
748 return;
749 }
750
751 /* Wait until reset clears, this shall not hang... */
752 while (ceu_read(pcdev, CAPSR) & (1 << 16))
753 udelay(10);
754
755 /* Anything to restore? */
756 if (capsr & ~(1 << 16))
757 ceu_write(pcdev, CAPSR, capsr);
758}
759
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300760/* Find the bus subdevice driver, e.g., CSI2 */
761static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev,
762 struct soc_camera_device *icd)
763{
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300764 if (pcdev->csi2_pdev) {
765 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -0300766 if (csi2_sd && csi2_sd->grp_id == soc_camera_grp_id(icd))
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300767 return csi2_sd;
768 }
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300769
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300770 return soc_camera_to_subdev(icd);
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300771}
772
773#define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
774 V4L2_MBUS_PCLK_SAMPLE_RISING | \
775 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
776 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
777 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
778 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
779 V4L2_MBUS_DATA_ACTIVE_HIGH)
780
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300781/* Capture is not running, no interrupts, no locking needed */
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -0300782static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300783{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300784 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300785 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300786 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300787 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300788 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
789 unsigned long value, common_flags = CEU_BUS_FLAGS;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300790 u32 capsr = capture_save_reset(pcdev);
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300791 unsigned int yuv_lineskip;
792 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300793
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300794 /*
795 * If the client doesn't implement g_mbus_config, we just use our
796 * platform data
797 */
798 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
799 if (!ret) {
800 common_flags = soc_mbus_config_compatible(&cfg,
801 common_flags);
802 if (!common_flags)
803 return -EINVAL;
804 } else if (ret != -ENOIOCTLCMD) {
805 return ret;
806 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300807
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300808 /* Make choises, based on platform preferences */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300809 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
810 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300811 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300812 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300813 else
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300814 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300815 }
816
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300817 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
818 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300819 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300820 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300821 else
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300822 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300823 }
824
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300825 cfg.flags = common_flags;
826 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
827 if (ret < 0 && ret != -ENOIOCTLCMD)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300828 return ret;
829
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300830 if (icd->current_fmt->host_fmt->bits_per_sample > 8)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300831 pcdev->is_16bit = 1;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300832 else
833 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300834
835 ceu_write(pcdev, CRCNTR, 0);
836 ceu_write(pcdev, CRCMPR, 0);
837
Magnus Damm8be65dc2008-12-18 12:38:06 -0300838 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300839 yuv_lineskip = 0x10;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300840
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300841 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300842 case V4L2_PIX_FMT_NV12:
843 case V4L2_PIX_FMT_NV21:
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300844 /* convert 4:2:2 -> 4:2:0 */
845 yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300846 /* fall-through */
847 case V4L2_PIX_FMT_NV16:
848 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300849 switch (cam->code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300850 case V4L2_MBUS_FMT_UYVY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300851 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
852 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300853 case V4L2_MBUS_FMT_VYUY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300854 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
855 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300856 case V4L2_MBUS_FMT_YUYV8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300857 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
858 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300859 case V4L2_MBUS_FMT_YVYU8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300860 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
861 break;
862 default:
863 BUG();
864 }
865 }
866
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300867 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
868 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300869 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300870
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300871 value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
872 value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300873 value |= pcdev->is_16bit ? 1 << 12 : 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300874
875 /* CSI2 mode */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300876 if (pcdev->pdata->csi2)
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300877 value |= 3 << 12;
878
Magnus Damm0d3244d2008-07-16 22:59:28 -0300879 ceu_write(pcdev, CAMCR, value);
880
881 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300882
883 switch (pcdev->field) {
884 case V4L2_FIELD_INTERLACED_TB:
885 value = 0x101;
886 break;
887 case V4L2_FIELD_INTERLACED_BT:
888 value = 0x102;
889 break;
890 default:
891 value = 0;
892 break;
893 }
894 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300895
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300896 sh_mobile_ceu_set_rect(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300897 mdelay(1);
898
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300899 dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300900 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300901
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300902 /*
903 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300904 *
905 * In data fetch mode bytes are received in chunks of 8 bytes.
906 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
907 *
908 * The data is however by default written to memory in reverse order:
909 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
910 *
911 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300912 * using 7 we swap the data bytes to match the incoming order:
913 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300914 */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300915 value = 0x00000007 | yuv_lineskip;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300916
917 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300918 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
919
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300920 capture_restore(pcdev, capsr);
921
Magnus Damm0d3244d2008-07-16 22:59:28 -0300922 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300923 return 0;
924}
925
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300926static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
927 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300928{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300929 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300930 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300931 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
932 unsigned long common_flags = CEU_BUS_FLAGS;
933 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
934 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300935
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300936 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
937 if (!ret)
938 common_flags = soc_mbus_config_compatible(&cfg,
939 common_flags);
940 else if (ret != -ENOIOCTLCMD)
941 return ret;
942
943 if (!common_flags || buswidth > 16)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300944 return -EINVAL;
945
946 return 0;
947}
948
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300949static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300950 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300951 .fourcc = V4L2_PIX_FMT_NV12,
952 .name = "NV12",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300953 .bits_per_sample = 8,
954 .packing = SOC_MBUS_PACKING_1_5X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300955 .order = SOC_MBUS_ORDER_LE,
Laurent Pinchartad3b81f2012-03-21 08:03:23 -0300956 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300957 }, {
958 .fourcc = V4L2_PIX_FMT_NV21,
959 .name = "NV21",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300960 .bits_per_sample = 8,
961 .packing = SOC_MBUS_PACKING_1_5X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300962 .order = SOC_MBUS_ORDER_LE,
Laurent Pinchartad3b81f2012-03-21 08:03:23 -0300963 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300964 }, {
965 .fourcc = V4L2_PIX_FMT_NV16,
966 .name = "NV16",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300967 .bits_per_sample = 8,
968 .packing = SOC_MBUS_PACKING_2X8_PADHI,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300969 .order = SOC_MBUS_ORDER_LE,
Laurent Pinchartad3b81f2012-03-21 08:03:23 -0300970 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300971 }, {
972 .fourcc = V4L2_PIX_FMT_NV61,
973 .name = "NV61",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300974 .bits_per_sample = 8,
975 .packing = SOC_MBUS_PACKING_2X8_PADHI,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300976 .order = SOC_MBUS_ORDER_LE,
Laurent Pinchartad3b81f2012-03-21 08:03:23 -0300977 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300978 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300979};
980
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300981/* This will be corrected as we get more formats */
982static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
983{
984 return fmt->packing == SOC_MBUS_PACKING_NONE ||
985 (fmt->bits_per_sample == 8 &&
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300986 fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
987 (fmt->bits_per_sample == 8 &&
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300988 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
989 (fmt->bits_per_sample > 8 &&
990 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
991}
992
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300993static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
994
Hans Verkuild34bfcd22011-09-05 17:07:47 -0300995static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
996{
997 return container_of(ctrl->handler, struct soc_camera_device,
998 ctrl_handler);
999}
1000
1001static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
1002{
1003 struct soc_camera_device *icd = ctrl_to_icd(ctrl);
1004 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1005 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1006
1007 switch (ctrl->id) {
1008 case V4L2_CID_SHARPNESS:
1009 switch (icd->current_fmt->host_fmt->fourcc) {
1010 case V4L2_PIX_FMT_NV12:
1011 case V4L2_PIX_FMT_NV21:
1012 case V4L2_PIX_FMT_NV16:
1013 case V4L2_PIX_FMT_NV61:
1014 ceu_write(pcdev, CLFCR, !ctrl->val);
1015 return 0;
1016 }
1017 break;
1018 }
1019
1020 return -EINVAL;
1021}
1022
1023static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
1024 .s_ctrl = sh_mobile_ceu_s_ctrl,
1025};
1026
Hans Verkuil3805f202010-05-08 17:55:00 -03001027static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
Magnus Damm9414de32008-12-18 11:49:06 -03001028 struct soc_camera_format_xlate *xlate)
1029{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001030 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001031 struct device *dev = icd->parent;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001032 struct soc_camera_host *ici = to_soc_camera_host(dev);
1033 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001034 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -03001035 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001036 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001037 enum v4l2_mbus_pixelcode code;
1038 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -03001039
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001040 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
1041 if (ret < 0)
1042 /* No more formats */
1043 return 0;
1044
1045 fmt = soc_mbus_get_fmtdesc(code);
1046 if (!fmt) {
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -03001047 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
1048 return 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001049 }
1050
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03001051 if (!pcdev->pdata->csi2) {
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -03001052 /* Are there any restrictions in the CSI-2 case? */
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001053 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
1054 if (ret < 0)
1055 return 0;
1056 }
Magnus Damm9414de32008-12-18 11:49:06 -03001057
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001058 if (!icd->host_priv) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001059 struct v4l2_mbus_framefmt mf;
1060 struct v4l2_rect rect;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001061 int shift = 0;
1062
Hans Verkuild34bfcd22011-09-05 17:07:47 -03001063 /* Add our control */
1064 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
1065 V4L2_CID_SHARPNESS, 0, 1, 1, 0);
1066 if (icd->ctrl_handler.error)
1067 return icd->ctrl_handler.error;
1068
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001069 /* FIXME: subwindow is lost between close / open */
1070
1071 /* Cache current client geometry */
1072 ret = client_g_rect(sd, &rect);
1073 if (ret < 0)
1074 return ret;
1075
1076 /* First time */
1077 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1078 if (ret < 0)
1079 return ret;
1080
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001081 /*
1082 * All currently existing CEU implementations support 2560x1920
1083 * or larger frames. If the sensor is proposing too big a frame,
1084 * don't bother with possibly supportred by the CEU larger
1085 * sizes, just try VGA multiples. If needed, this can be
1086 * adjusted in the future.
1087 */
1088 while ((mf.width > pcdev->max_width ||
1089 mf.height > pcdev->max_height) && shift < 4) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001090 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1091 mf.width = 2560 >> shift;
1092 mf.height = 1920 >> shift;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001093 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1094 soc_camera_grp_id(icd), video,
1095 s_mbus_fmt, &mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001096 if (ret < 0)
1097 return ret;
1098 shift++;
1099 }
1100
1101 if (shift == 4) {
1102 dev_err(dev, "Failed to configure the client below %ux%x\n",
1103 mf.width, mf.height);
1104 return -EIO;
1105 }
1106
1107 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
1108
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001109 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1110 if (!cam)
1111 return -ENOMEM;
1112
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001113 /* We are called with current camera crop, initialise subrect with it */
1114 cam->rect = rect;
1115 cam->subrect = rect;
1116
1117 cam->width = mf.width;
1118 cam->height = mf.height;
1119
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001120 icd->host_priv = cam;
1121 } else {
1122 cam = icd->host_priv;
1123 }
1124
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001125 /* Beginning of a pass */
1126 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001127 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001128
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001129 switch (code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -03001130 case V4L2_MBUS_FMT_UYVY8_2X8:
1131 case V4L2_MBUS_FMT_VYUY8_2X8:
1132 case V4L2_MBUS_FMT_YUYV8_2X8:
1133 case V4L2_MBUS_FMT_YVYU8_2X8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001134 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001135 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001136
1137 /*
1138 * Our case is simple so far: for any of the above four camera
1139 * formats we add all our four synthesized NV* formats, so,
1140 * just marking the device with a single flag suffices. If
1141 * the format generation rules are more complex, you would have
1142 * to actually hang your already added / counted formats onto
1143 * the host_priv pointer and check whether the format you're
1144 * going to add now is already there.
1145 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001146 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001147
Magnus Damm8be65dc2008-12-18 12:38:06 -03001148 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1149 formats += n;
1150 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001151 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1152 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001153 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001154 dev_dbg(dev, "Providing format %s using code %d\n",
1155 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -03001156 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001157 break;
Magnus Damm9414de32008-12-18 11:49:06 -03001158 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001159 if (!sh_mobile_ceu_packing_supported(fmt))
1160 return 0;
1161 }
1162
1163 /* Generic pass-through */
1164 formats++;
1165 if (xlate) {
1166 xlate->host_fmt = fmt;
1167 xlate->code = code;
1168 xlate++;
1169 dev_dbg(dev, "Providing format %s in pass-through mode\n",
Guennadi Liakhovetskie53a4802010-06-18 04:20:56 -03001170 fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -03001171 }
1172
1173 return formats;
1174}
1175
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001176static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1177{
1178 kfree(icd->host_priv);
1179 icd->host_priv = NULL;
1180}
1181
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001182/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001183static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1184{
1185 return r1->width < r2->width || r1->height < r2->height;
1186}
1187
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001188/* Check if r1 fails to cover r2 */
1189static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1190{
1191 return r1->left > r2->left || r1->top > r2->top ||
1192 r1->left + r1->width < r2->left + r2->width ||
1193 r1->top + r1->height < r2->top + r2->height;
1194}
1195
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001196static unsigned int scale_down(unsigned int size, unsigned int scale)
1197{
1198 return (size * 4096 + scale / 2) / scale;
1199}
1200
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001201static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1202{
1203 return (input * 4096 + output / 2) / output;
1204}
1205
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001206/* Get and store current client crop */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001207static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1208{
1209 struct v4l2_crop crop;
1210 struct v4l2_cropcap cap;
1211 int ret;
1212
1213 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1214
1215 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1216 if (!ret) {
1217 *rect = crop.c;
1218 return ret;
1219 }
1220
1221 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1222 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1223
1224 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001225 if (!ret)
1226 *rect = cap.defrect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001227
1228 return ret;
1229}
1230
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001231/* Client crop has changed, update our sub-rectangle to remain within the area */
1232static void update_subrect(struct sh_mobile_ceu_cam *cam)
1233{
1234 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1235
1236 if (rect->width < subrect->width)
1237 subrect->width = rect->width;
1238
1239 if (rect->height < subrect->height)
1240 subrect->height = rect->height;
1241
1242 if (rect->left > subrect->left)
1243 subrect->left = rect->left;
1244 else if (rect->left + rect->width >
1245 subrect->left + subrect->width)
1246 subrect->left = rect->left + rect->width -
1247 subrect->width;
1248
1249 if (rect->top > subrect->top)
1250 subrect->top = rect->top;
1251 else if (rect->top + rect->height >
1252 subrect->top + subrect->height)
1253 subrect->top = rect->top + rect->height -
1254 subrect->height;
1255}
1256
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001257/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001258 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001259 * 1. try if the client can produce exactly what requested by the user
1260 * 2. if (1) failed, try to double the client image until we get one big enough
1261 * 3. if (2) failed, try to request the maximum image
1262 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001263static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001264 struct v4l2_crop *cam_crop)
1265{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001266 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001267 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1268 struct device *dev = sd->v4l2_dev->dev;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001269 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001270 struct v4l2_cropcap cap;
1271 int ret;
1272 unsigned int width, height;
1273
1274 v4l2_subdev_call(sd, video, s_crop, crop);
1275 ret = client_g_rect(sd, cam_rect);
1276 if (ret < 0)
1277 return ret;
1278
1279 /*
1280 * Now cam_crop contains the current camera input rectangle, and it must
1281 * be within camera cropcap bounds
1282 */
1283 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1284 /* Even if camera S_CROP failed, but camera rectangle matches */
Márton Némethe26b3142010-02-24 17:13:29 -03001285 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001286 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001287 cam->rect = *cam_rect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001288 return 0;
1289 }
1290
1291 /* Try to fix cropping, that camera hasn't managed to set */
Márton Némethe26b3142010-02-24 17:13:29 -03001292 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 -03001293 cam_rect->width, cam_rect->height,
1294 cam_rect->left, cam_rect->top,
1295 rect->width, rect->height, rect->left, rect->top);
1296
1297 /* We need sensor maximum rectangle */
1298 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1299 if (ret < 0)
1300 return ret;
1301
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001302 /* Put user requested rectangle within sensor bounds */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001303 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1304 cap.bounds.width);
1305 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1306 cap.bounds.height);
1307
1308 /*
1309 * Popular special case - some cameras can only handle fixed sizes like
1310 * QVGA, VGA,... Take care to avoid infinite loop.
1311 */
1312 width = max(cam_rect->width, 2);
1313 height = max(cam_rect->height, 2);
1314
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001315 /*
1316 * Loop as long as sensor is not covering the requested rectangle and
1317 * is still within its bounds
1318 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001319 while (!ret && (is_smaller(cam_rect, rect) ||
1320 is_inside(cam_rect, rect)) &&
1321 (cap.bounds.width > width || cap.bounds.height > height)) {
1322
1323 width *= 2;
1324 height *= 2;
1325
1326 cam_rect->width = width;
1327 cam_rect->height = height;
1328
1329 /*
1330 * We do not know what capabilities the camera has to set up
1331 * left and top borders. We could try to be smarter in iterating
1332 * them, e.g., if camera current left is to the right of the
1333 * target left, set it to the middle point between the current
1334 * left and minimum left. But that would add too much
1335 * complexity: we would have to iterate each border separately.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001336 * Instead we just drop to the left and top bounds.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001337 */
1338 if (cam_rect->left > rect->left)
1339 cam_rect->left = cap.bounds.left;
1340
1341 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1342 cam_rect->width = rect->left + rect->width -
1343 cam_rect->left;
1344
1345 if (cam_rect->top > rect->top)
1346 cam_rect->top = cap.bounds.top;
1347
1348 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1349 cam_rect->height = rect->top + rect->height -
1350 cam_rect->top;
1351
1352 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1353 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001354 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001355 cam_rect->width, cam_rect->height,
1356 cam_rect->left, cam_rect->top);
1357 }
1358
1359 /* S_CROP must not modify the rectangle */
1360 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1361 /*
1362 * The camera failed to configure a suitable cropping,
1363 * we cannot use the current rectangle, set to max
1364 */
1365 *cam_rect = cap.bounds;
1366 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1367 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001368 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001369 cam_rect->width, cam_rect->height,
1370 cam_rect->left, cam_rect->top);
1371 }
1372
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001373 if (!ret) {
1374 cam->rect = *cam_rect;
1375 update_subrect(cam);
1376 }
1377
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001378 return ret;
1379}
1380
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001381/* Iterative s_mbus_fmt, also updates cached client crop on success */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001382static int client_s_fmt(struct soc_camera_device *icd,
1383 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001384{
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001385 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1386 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001387 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001388 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001389 struct device *dev = icd->parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001390 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001391 unsigned int max_width, max_height;
1392 struct v4l2_cropcap cap;
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001393 bool ceu_1to1;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001394 int ret;
1395
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001396 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1397 soc_camera_grp_id(icd), video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001398 s_mbus_fmt, mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001399 if (ret < 0)
1400 return ret;
1401
1402 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1403
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001404 if (width == mf->width && height == mf->height) {
1405 /* Perfect! The client has done it all. */
1406 ceu_1to1 = true;
1407 goto update_cache;
1408 }
1409
1410 ceu_1to1 = false;
1411 if (!ceu_can_scale)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001412 goto update_cache;
1413
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001414 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1415
1416 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1417 if (ret < 0)
1418 return ret;
1419
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001420 max_width = min(cap.bounds.width, pcdev->max_width);
1421 max_height = min(cap.bounds.height, pcdev->max_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001422
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001423 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001424 tmp_w = mf->width;
1425 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001426
1427 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1428 while ((width > tmp_w || height > tmp_h) &&
1429 tmp_w < max_width && tmp_h < max_height) {
1430 tmp_w = min(2 * tmp_w, max_width);
1431 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001432 mf->width = tmp_w;
1433 mf->height = tmp_h;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001434 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1435 soc_camera_grp_id(icd), video,
1436 s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001437 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001438 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001439 if (ret < 0) {
1440 /* This shouldn't happen */
1441 dev_err(dev, "Client failed to set format: %d\n", ret);
1442 return ret;
1443 }
1444 }
1445
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001446update_cache:
1447 /* Update cache */
1448 ret = client_g_rect(sd, &cam->rect);
1449 if (ret < 0)
1450 return ret;
1451
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001452 if (ceu_1to1)
1453 cam->subrect = cam->rect;
1454 else
1455 update_subrect(cam);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001456
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001457 return 0;
1458}
1459
1460/**
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001461 * @width - on output: user width, mapped back to input
1462 * @height - on output: user height, mapped back to input
1463 * @mf - in- / output camera output window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001464 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001465static int client_scale(struct soc_camera_device *icd,
1466 struct v4l2_mbus_framefmt *mf,
1467 unsigned int *width, unsigned int *height,
1468 bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001469{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001470 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001471 struct device *dev = icd->parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001472 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001473 unsigned int scale_h, scale_v;
1474 int ret;
1475
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001476 /*
1477 * 5. Apply iterative camera S_FMT for camera user window (also updates
1478 * client crop cache and the imaginary sub-rectangle).
1479 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001480 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001481 if (ret < 0)
1482 return ret;
1483
1484 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001485 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001486
1487 /* 6. Retrieve camera output window (g_fmt) */
1488
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001489 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001490
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001491 /* 7. Calculate new client scales. */
1492 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1493 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001494
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001495 mf->width = mf_tmp.width;
1496 mf->height = mf_tmp.height;
1497 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001498
1499 /*
1500 * 8. Calculate new CEU crop - apply camera scales to previously
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001501 * updated "effective" crop.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001502 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001503 *width = scale_down(cam->subrect.width, scale_h);
1504 *height = scale_down(cam->subrect.height, scale_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001505
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001506 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001507
1508 return 0;
1509}
1510
1511/*
1512 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1513 * framerate by always requesting the maximum image from the client. See
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001514 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001515 * scaling and cropping algorithms and for the meaning of referenced here steps.
1516 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001517static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001518 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001519{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001520 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001521 struct device *dev = icd->parent;
1522 struct soc_camera_host *ici = to_soc_camera_host(dev);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001523 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001524 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001525 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001526 struct v4l2_rect *cam_rect = &cam_crop.c;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001527 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001528 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001529 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001530 out_width, out_height;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001531 int interm_width, interm_height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001532 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001533 int ret;
1534
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001535 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1536 rect->left, rect->top);
1537
1538 /* During camera cropping its output window can change too, stop CEU */
1539 capsr = capture_save_reset(pcdev);
1540 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1541
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001542 /*
1543 * 1. - 2. Apply iterative camera S_CROP for new input window, read back
1544 * actual camera rectangle.
1545 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001546 ret = client_s_crop(icd, a, &cam_crop);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001547 if (ret < 0)
1548 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001549
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001550 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001551 cam_rect->width, cam_rect->height,
1552 cam_rect->left, cam_rect->top);
1553
1554 /* On success cam_crop contains current camera crop */
1555
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001556 /* 3. Retrieve camera output window */
1557 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1558 if (ret < 0)
1559 return ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001560
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001561 if (mf.width > pcdev->max_width || mf.height > pcdev->max_height)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001562 return -EINVAL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001563
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001564 /* 4. Calculate camera scales */
1565 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1566 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001567
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001568 /* Calculate intermediate window */
1569 interm_width = scale_down(rect->width, scale_cam_h);
1570 interm_height = scale_down(rect->height, scale_cam_v);
1571
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001572 if (interm_width < icd->user_width) {
1573 u32 new_scale_h;
1574
1575 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1576
1577 mf.width = scale_down(cam_rect->width, new_scale_h);
1578 }
1579
1580 if (interm_height < icd->user_height) {
1581 u32 new_scale_v;
1582
1583 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1584
1585 mf.height = scale_down(cam_rect->height, new_scale_v);
1586 }
1587
1588 if (interm_width < icd->user_width || interm_height < icd->user_height) {
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001589 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1590 soc_camera_grp_id(icd), video,
1591 s_mbus_fmt, &mf);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001592 if (ret < 0)
1593 return ret;
1594
1595 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1596 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1597 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1598 interm_width = scale_down(rect->width, scale_cam_h);
1599 interm_height = scale_down(rect->height, scale_cam_v);
1600 }
1601
1602 /* Cache camera output window */
1603 cam->width = mf.width;
1604 cam->height = mf.height;
1605
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001606 if (pcdev->image_mode) {
1607 out_width = min(interm_width, icd->user_width);
1608 out_height = min(interm_height, icd->user_height);
1609 } else {
1610 out_width = interm_width;
1611 out_height = interm_height;
1612 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001613
1614 /*
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001615 * 5. Calculate CEU scales from camera scales from results of (5) and
1616 * the user window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001617 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001618 scale_ceu_h = calc_scale(interm_width, &out_width);
1619 scale_ceu_v = calc_scale(interm_height, &out_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001620
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001621 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001622
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001623 /* Apply CEU scales. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001624 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1625 if (cflcr != pcdev->cflcr) {
1626 pcdev->cflcr = cflcr;
1627 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001628 }
1629
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001630 icd->user_width = out_width & ~3;
1631 icd->user_height = out_height & ~3;
1632 /* Offsets are applied at the CEU scaling filter input */
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001633 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1634 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001635
1636 /* 6. Use CEU cropping to crop to the new window. */
1637 sh_mobile_ceu_set_rect(icd);
1638
1639 cam->subrect = *rect;
1640
1641 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1642 icd->user_width, icd->user_height,
1643 cam->ceu_left, cam->ceu_top);
1644
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001645 /* Restore capture. The CE bit can be cleared by the hardware */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001646 if (pcdev->active)
1647 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001648 capture_restore(pcdev, capsr);
1649
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001650 /* Even if only camera cropping succeeded */
1651 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001652}
1653
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001654static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1655 struct v4l2_crop *a)
1656{
1657 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1658
1659 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1660 a->c = cam->subrect;
1661
1662 return 0;
1663}
1664
1665/*
1666 * Calculate real client output window by applying new scales to the current
1667 * client crop. New scales are calculated from the requested output format and
1668 * CEU crop, mapped backed onto the client input (subrect).
1669 */
1670static void calculate_client_output(struct soc_camera_device *icd,
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001671 const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001672{
1673 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001674 struct device *dev = icd->parent;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001675 struct v4l2_rect *cam_subrect = &cam->subrect;
1676 unsigned int scale_v, scale_h;
1677
1678 if (cam_subrect->width == cam->rect.width &&
1679 cam_subrect->height == cam->rect.height) {
1680 /* No sub-cropping */
1681 mf->width = pix->width;
1682 mf->height = pix->height;
1683 return;
1684 }
1685
1686 /* 1.-2. Current camera scales and subwin - cached. */
1687
1688 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1689 cam_subrect->width, cam_subrect->height,
1690 cam_subrect->left, cam_subrect->top);
1691
1692 /*
1693 * 3. Calculate new combined scales from input sub-window to requested
1694 * user window.
1695 */
1696
1697 /*
1698 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1699 * (128x96) or larger than VGA
1700 */
1701 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1702 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1703
1704 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1705
1706 /*
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001707 * 4. Calculate desired client output window by applying combined scales
1708 * to client (real) input window.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001709 */
1710 mf->width = scale_down(cam->rect.width, scale_h);
1711 mf->height = scale_down(cam->rect.height, scale_v);
1712}
1713
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001714/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001715static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001716 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001717{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001718 struct device *dev = icd->parent;
1719 struct soc_camera_host *ici = to_soc_camera_host(dev);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001720 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001721 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1722 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001723 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001724 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001725 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001726 /* Keep Compiler Happy */
1727 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001728 u16 scale_v, scale_h;
1729 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001730 bool image_mode;
1731 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001732
1733 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001734 default:
1735 pix->field = V4L2_FIELD_NONE;
1736 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001737 case V4L2_FIELD_INTERLACED_TB:
1738 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001739 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001740 field = pix->field;
1741 break;
1742 case V4L2_FIELD_INTERLACED:
1743 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001744 break;
1745 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001746
Magnus Damm9414de32008-12-18 11:49:06 -03001747 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1748 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001749 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001750 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001751 }
1752
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001753 /* 1.-4. Calculate desired client output geometry */
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001754 calculate_client_output(icd, pix, &mf);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001755 mf.field = pix->field;
1756 mf.colorspace = pix->colorspace;
1757 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001758
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001759 switch (pixfmt) {
1760 case V4L2_PIX_FMT_NV12:
1761 case V4L2_PIX_FMT_NV21:
1762 case V4L2_PIX_FMT_NV16:
1763 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001764 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001765 break;
1766 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001767 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001768 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001769
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -03001770 dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -03001771 pix->width, pix->height);
1772
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001773 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001774
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001775 /* 5. - 9. */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001776 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001777 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001778
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001779 dev_geo(dev, "5-9: client scale return %d\n", ret);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001780
1781 /* Done with the camera. Now see if we can improve the result */
1782
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001783 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1784 mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001785 if (ret < 0)
1786 return ret;
1787
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001788 if (mf.code != xlate->code)
1789 return -EINVAL;
1790
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001791 /* 9. Prepare CEU crop */
1792 cam->width = mf.width;
1793 cam->height = mf.height;
1794
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001795 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001796
1797 /* We cannot scale up */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001798 if (pix->width > ceu_sub_width)
1799 ceu_sub_width = pix->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001800
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001801 if (pix->height > ceu_sub_height)
1802 ceu_sub_height = pix->height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001803
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001804 pix->colorspace = mf.colorspace;
1805
1806 if (image_mode) {
1807 /* Scale pix->{width x height} down to width x height */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001808 scale_h = calc_scale(ceu_sub_width, &pix->width);
1809 scale_v = calc_scale(ceu_sub_height, &pix->height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001810 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001811 pix->width = ceu_sub_width;
1812 pix->height = ceu_sub_height;
1813 scale_h = 0;
1814 scale_v = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001815 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001816
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001817 pcdev->cflcr = scale_h | (scale_v << 16);
1818
1819 /*
1820 * We have calculated CFLCR, the actual configuration will be performed
1821 * in sh_mobile_ceu_set_bus_param()
1822 */
1823
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001824 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001825 ceu_sub_width, scale_h, pix->width,
1826 ceu_sub_height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001827
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001828 cam->code = xlate->code;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001829 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001830
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001831 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001832 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001833
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001834 /* CFSZR requirement */
1835 pix->width &= ~3;
1836 pix->height &= ~3;
1837
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001838 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001839}
1840
Laurent Pinchartfc13baf2012-03-21 08:16:05 -03001841#define CEU_CHDW_MAX 8188U /* Maximum line stride */
1842
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001843static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1844 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001845{
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001846 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1847 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm9414de32008-12-18 11:49:06 -03001848 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001849 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001850 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001851 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001852 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001853 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001854 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001855
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001856 dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001857 pixfmt, pix->width, pix->height);
1858
Magnus Damm9414de32008-12-18 11:49:06 -03001859 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1860 if (!xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001861 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001862 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001863 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001864
Magnus Damm0d3244d2008-07-16 22:59:28 -03001865 /* FIXME: calculate using depth and bus width */
1866
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001867 /* CFSZR requires height and width to be 4-pixel aligned */
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001868 v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
1869 &pix->height, 4, pcdev->max_height, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001870
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001871 width = pix->width;
1872 height = pix->height;
1873
Magnus Damm0d3244d2008-07-16 22:59:28 -03001874 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001875 mf.width = pix->width;
1876 mf.height = pix->height;
1877 mf.field = pix->field;
1878 mf.code = xlate->code;
1879 mf.colorspace = pix->colorspace;
1880
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001881 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1882 video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001883 if (ret < 0)
1884 return ret;
1885
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001886 pix->width = mf.width;
1887 pix->height = mf.height;
1888 pix->field = mf.field;
1889 pix->colorspace = mf.colorspace;
1890
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001891 switch (pixfmt) {
1892 case V4L2_PIX_FMT_NV12:
1893 case V4L2_PIX_FMT_NV21:
1894 case V4L2_PIX_FMT_NV16:
1895 case V4L2_PIX_FMT_NV61:
1896 /* FIXME: check against rect_max after converting soc-camera */
1897 /* We can scale precisely, need a bigger image from camera */
1898 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001899 /*
1900 * We presume, the sensor behaves sanely, i.e., if
1901 * requested a bigger rectangle, it will not return a
1902 * smaller one.
1903 */
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001904 mf.width = pcdev->max_width;
1905 mf.height = pcdev->max_height;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001906 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1907 soc_camera_grp_id(icd), video,
1908 try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001909 if (ret < 0) {
1910 /* Shouldn't actually happen... */
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001911 dev_err(icd->parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001912 "FIXME: client try_fmt() = %d\n", ret);
1913 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001914 }
1915 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001916 /* We will scale exactly */
1917 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001918 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001919 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001920 pix->height = height;
Laurent Pinchartfc13baf2012-03-21 08:16:05 -03001921
1922 pix->bytesperline = max(pix->bytesperline, pix->width);
1923 pix->bytesperline = min(pix->bytesperline, CEU_CHDW_MAX);
1924 pix->bytesperline &= ~3;
1925 break;
1926
1927 default:
1928 /* Configurable stride isn't supported in pass-through mode. */
1929 pix->bytesperline = 0;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001930 }
1931
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001932 pix->width &= ~3;
1933 pix->height &= ~3;
Laurent Pinchartfc13baf2012-03-21 08:16:05 -03001934 pix->sizeimage = 0;
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001935
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001936 dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001937 __func__, ret, pix->pixelformat, pix->width, pix->height);
1938
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001939 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001940}
1941
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001942static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1943 struct v4l2_crop *a)
1944{
1945 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001946 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001947 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1948 u32 out_width = icd->user_width, out_height = icd->user_height;
1949 int ret;
1950
1951 /* Freeze queue */
1952 pcdev->frozen = 1;
1953 /* Wait for frame */
1954 ret = wait_for_completion_interruptible(&pcdev->complete);
1955 /* Stop the client */
1956 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1957 if (ret < 0)
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001958 dev_warn(icd->parent,
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001959 "Client failed to stop the stream: %d\n", ret);
1960 else
1961 /* Do the crop, if it fails, there's nothing more we can do */
1962 sh_mobile_ceu_set_crop(icd, a);
1963
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001964 dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001965
1966 if (icd->user_width != out_width || icd->user_height != out_height) {
1967 struct v4l2_format f = {
1968 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1969 .fmt.pix = {
1970 .width = out_width,
1971 .height = out_height,
1972 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1973 .field = pcdev->field,
1974 .colorspace = icd->colorspace,
1975 },
1976 };
1977 ret = sh_mobile_ceu_set_fmt(icd, &f);
1978 if (!ret && (out_width != f.fmt.pix.width ||
1979 out_height != f.fmt.pix.height))
1980 ret = -EINVAL;
1981 if (!ret) {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001982 icd->user_width = out_width & ~3;
1983 icd->user_height = out_height & ~3;
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -03001984 ret = sh_mobile_ceu_set_bus_param(icd);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001985 }
1986 }
1987
1988 /* Thaw the queue */
1989 pcdev->frozen = 0;
1990 spin_lock_irq(&pcdev->lock);
1991 sh_mobile_ceu_capture(pcdev);
1992 spin_unlock_irq(&pcdev->lock);
1993 /* Start the client */
1994 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1995 return ret;
1996}
1997
Magnus Damm0d3244d2008-07-16 22:59:28 -03001998static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1999{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -03002000 struct soc_camera_device *icd = file->private_data;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002001
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002002 return vb2_poll(&icd->vb2_vidq, file, pt);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002003}
2004
2005static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
2006 struct v4l2_capability *cap)
2007{
2008 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
Magnus Damm0d3244d2008-07-16 22:59:28 -03002009 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2010 return 0;
2011}
2012
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002013static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
2014 struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002015{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002016 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2017 q->io_modes = VB2_MMAP | VB2_USERPTR;
2018 q->drv_priv = icd;
2019 q->ops = &sh_mobile_ceu_videobuf_ops;
2020 q->mem_ops = &vb2_dma_contig_memops;
2021 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002022
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002023 return vb2_queue_init(q);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002024}
2025
2026static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
2027 .owner = THIS_MODULE,
2028 .add = sh_mobile_ceu_add_device,
2029 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03002030 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03002031 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03002032 .get_crop = sh_mobile_ceu_get_crop,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03002033 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03002034 .set_livecrop = sh_mobile_ceu_set_livecrop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03002035 .set_fmt = sh_mobile_ceu_set_fmt,
2036 .try_fmt = sh_mobile_ceu_try_fmt,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002037 .poll = sh_mobile_ceu_poll,
2038 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002039 .set_bus_param = sh_mobile_ceu_set_bus_param,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002040 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002041};
2042
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002043struct bus_wait {
2044 struct notifier_block notifier;
2045 struct completion completion;
2046 struct device *dev;
2047};
2048
2049static int bus_notify(struct notifier_block *nb,
2050 unsigned long action, void *data)
2051{
2052 struct device *dev = data;
2053 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
2054
2055 if (wait->dev != dev)
2056 return NOTIFY_DONE;
2057
2058 switch (action) {
2059 case BUS_NOTIFY_UNBOUND_DRIVER:
2060 /* Protect from module unloading */
2061 wait_for_completion(&wait->completion);
2062 return NOTIFY_OK;
2063 }
2064 return NOTIFY_DONE;
2065}
2066
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002067static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002068{
2069 struct sh_mobile_ceu_dev *pcdev;
2070 struct resource *res;
2071 void __iomem *base;
2072 unsigned int irq;
2073 int err = 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002074 struct bus_wait wait = {
2075 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
2076 .notifier.notifier_call = bus_notify,
2077 };
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002078 struct sh_mobile_ceu_companion *csi2;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002079
2080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2081 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03002082 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03002083 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
2084 err = -ENODEV;
2085 goto exit;
2086 }
2087
2088 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
2089 if (!pcdev) {
2090 dev_err(&pdev->dev, "Could not allocate pcdev\n");
2091 err = -ENOMEM;
2092 goto exit;
2093 }
2094
Magnus Damm0d3244d2008-07-16 22:59:28 -03002095 INIT_LIST_HEAD(&pcdev->capture);
2096 spin_lock_init(&pcdev->lock);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03002097 init_completion(&pcdev->complete);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002098
2099 pcdev->pdata = pdev->dev.platform_data;
2100 if (!pcdev->pdata) {
2101 err = -EINVAL;
2102 dev_err(&pdev->dev, "CEU platform data not set.\n");
2103 goto exit_kfree;
2104 }
2105
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03002106 pcdev->max_width = pcdev->pdata->max_width ? : 2560;
2107 pcdev->max_height = pcdev->pdata->max_height ? : 1920;
2108
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002109 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03002110 if (!base) {
2111 err = -ENXIO;
2112 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
2113 goto exit_kfree;
2114 }
2115
2116 pcdev->irq = irq;
2117 pcdev->base = base;
2118 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03002119
2120 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2121 if (res) {
2122 err = dma_declare_coherent_memory(&pdev->dev, res->start,
2123 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002124 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002125 DMA_MEMORY_MAP |
2126 DMA_MEMORY_EXCLUSIVE);
2127 if (!err) {
2128 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2129 err = -ENXIO;
2130 goto exit_iounmap;
2131 }
2132
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002133 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002134 }
2135
2136 /* request irq */
2137 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03002138 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002139 if (err) {
2140 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2141 goto exit_release_mem;
2142 }
2143
Magnus Damm6d1386c2009-08-14 10:49:17 +00002144 pm_suspend_ignore_children(&pdev->dev, true);
2145 pm_runtime_enable(&pdev->dev);
2146 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09002147
Magnus Damm0d3244d2008-07-16 22:59:28 -03002148 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002149 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002150 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03002151 pcdev->ici.drv_name = dev_name(&pdev->dev);
2152 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Laurent Pinchartfc13baf2012-03-21 08:16:05 -03002153 pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002154
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002155 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2156 if (IS_ERR(pcdev->alloc_ctx)) {
2157 err = PTR_ERR(pcdev->alloc_ctx);
2158 goto exit_free_clk;
2159 }
2160
2161 err = soc_camera_host_register(&pcdev->ici);
2162 if (err)
2163 goto exit_free_ctx;
2164
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002165 /* CSI2 interfacing */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002166 csi2 = pcdev->pdata->csi2;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002167 if (csi2) {
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002168 struct platform_device *csi2_pdev =
2169 platform_device_alloc("sh-mobile-csi2", csi2->id);
2170 struct sh_csi2_pdata *csi2_pdata = csi2->platform_data;
2171
2172 if (!csi2_pdev) {
2173 err = -ENOMEM;
2174 goto exit_host_unregister;
2175 }
2176
2177 pcdev->csi2_pdev = csi2_pdev;
2178
2179 err = platform_device_add_data(csi2_pdev, csi2_pdata, sizeof(*csi2_pdata));
2180 if (err < 0)
2181 goto exit_pdev_put;
2182
2183 csi2_pdata = csi2_pdev->dev.platform_data;
2184 csi2_pdata->v4l2_dev = &pcdev->ici.v4l2_dev;
2185
2186 csi2_pdev->resource = csi2->resource;
2187 csi2_pdev->num_resources = csi2->num_resources;
2188
2189 err = platform_device_add(csi2_pdev);
2190 if (err < 0)
2191 goto exit_pdev_put;
2192
2193 wait.dev = &csi2_pdev->dev;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002194
2195 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2196 if (err < 0)
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002197 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002198
2199 /*
2200 * From this point the driver module will not unload, until
2201 * we complete the completion.
2202 */
2203
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002204 if (!csi2_pdev->dev.driver) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002205 complete(&wait.completion);
2206 /* Either too late, or probing failed */
2207 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2208 err = -ENXIO;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002209 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002210 }
2211
2212 /*
2213 * The module is still loaded, in the worst case it is hanging
2214 * in device release on our completion. So, _now_ dereferencing
2215 * the "owner" is safe!
2216 */
2217
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002218 err = try_module_get(csi2_pdev->dev.driver->owner);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002219
2220 /* Let notifier complete, if it has been locked */
2221 complete(&wait.completion);
2222 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2223 if (!err) {
2224 err = -ENODEV;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002225 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002226 }
2227 }
2228
Magnus Damm0d3244d2008-07-16 22:59:28 -03002229 return 0;
2230
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002231exit_pdev_unregister:
2232 platform_device_del(pcdev->csi2_pdev);
2233exit_pdev_put:
2234 pcdev->csi2_pdev->resource = NULL;
2235 platform_device_put(pcdev->csi2_pdev);
2236exit_host_unregister:
2237 soc_camera_host_unregister(&pcdev->ici);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002238exit_free_ctx:
2239 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002240exit_free_clk:
2241 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002242 free_irq(pcdev->irq, pcdev);
2243exit_release_mem:
2244 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2245 dma_release_declared_memory(&pdev->dev);
2246exit_iounmap:
2247 iounmap(base);
2248exit_kfree:
2249 kfree(pcdev);
2250exit:
2251 return err;
2252}
2253
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002254static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002255{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002256 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2257 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2258 struct sh_mobile_ceu_dev, ici);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002259 struct platform_device *csi2_pdev = pcdev->csi2_pdev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002260
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002261 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002262 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002263 free_irq(pcdev->irq, pcdev);
2264 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2265 dma_release_declared_memory(&pdev->dev);
2266 iounmap(pcdev->base);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002267 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002268 if (csi2_pdev && csi2_pdev->dev.driver) {
2269 struct module *csi2_drv = csi2_pdev->dev.driver->owner;
2270 platform_device_del(csi2_pdev);
2271 csi2_pdev->resource = NULL;
2272 platform_device_put(csi2_pdev);
2273 module_put(csi2_drv);
2274 }
Magnus Damm0d3244d2008-07-16 22:59:28 -03002275 kfree(pcdev);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002276
Magnus Damm0d3244d2008-07-16 22:59:28 -03002277 return 0;
2278}
2279
Magnus Damm6d1386c2009-08-14 10:49:17 +00002280static int sh_mobile_ceu_runtime_nop(struct device *dev)
2281{
2282 /* Runtime PM callback shared between ->runtime_suspend()
2283 * and ->runtime_resume(). Simply returns success.
2284 *
2285 * This driver re-initializes all registers after
2286 * pm_runtime_get_sync() anyway so there is no need
2287 * to save and restore registers here.
2288 */
2289 return 0;
2290}
2291
Alexey Dobriyan47145212009-12-14 18:00:08 -08002292static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00002293 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2294 .runtime_resume = sh_mobile_ceu_runtime_nop,
2295};
2296
Magnus Damm0d3244d2008-07-16 22:59:28 -03002297static struct platform_driver sh_mobile_ceu_driver = {
2298 .driver = {
2299 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00002300 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002301 },
2302 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03002303 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002304};
2305
2306static int __init sh_mobile_ceu_init(void)
2307{
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002308 /* Whatever return code */
2309 request_module("sh_mobile_csi2");
Magnus Damm0d3244d2008-07-16 22:59:28 -03002310 return platform_driver_register(&sh_mobile_ceu_driver);
2311}
2312
2313static void __exit sh_mobile_ceu_exit(void)
2314{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03002315 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002316}
2317
2318module_init(sh_mobile_ceu_init);
2319module_exit(sh_mobile_ceu_exit);
2320
2321MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2322MODULE_AUTHOR("Magnus Damm");
2323MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002324MODULE_VERSION("0.0.6");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03002325MODULE_ALIAS("platform:sh_mobile_ceu");