blob: 3ae5c9c58cba18b4cca6f3645279f2dc9ed88531 [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>
30#include <linux/version.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030032#include <linux/device.h>
33#include <linux/platform_device.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030034#include <linux/videodev2.h>
Magnus Damm6d1386c2009-08-14 10:49:17 +000035#include <linux/pm_runtime.h>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030036#include <linux/sched.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030037
38#include <media/v4l2-common.h>
39#include <media/v4l2-dev.h>
40#include <media/soc_camera.h>
41#include <media/sh_mobile_ceu.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;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030093 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -030094};
95
96struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030097 struct soc_camera_host ici;
98 struct soc_camera_device *icd;
99
100 unsigned int irq;
101 void __iomem *base;
102 unsigned long video_limit;
103
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300104 spinlock_t lock; /* Protects video buffer lists */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300105 struct list_head capture;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300106 struct vb2_buffer *active;
107 struct vb2_alloc_ctx *alloc_ctx;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300108
109 struct sh_mobile_ceu_info *pdata;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300110 struct completion complete;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300111
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300112 u32 cflcr;
113
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300114 enum v4l2_field field;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300115 int sequence;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300116
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300117 unsigned int image_mode:1;
118 unsigned int is_16bit:1;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300119 unsigned int frozen:1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300120};
121
122struct sh_mobile_ceu_cam {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300123 /* CEU offsets within scaled by the CEU camera output */
124 unsigned int ceu_left;
125 unsigned int ceu_top;
126 /* Client output, as seen by the CEU */
127 unsigned int width;
128 unsigned int height;
129 /*
130 * User window from S_CROP / G_CROP, produced by client cropping and
131 * scaling, CEU scaling and CEU cropping, mapped back onto the client
132 * input window
133 */
134 struct v4l2_rect subrect;
135 /* Camera cropping rectangle */
136 struct v4l2_rect rect;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300137 const struct soc_mbus_pixelfmt *extra_fmt;
138 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300139};
140
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300141static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
142{
143 return container_of(vb, struct sh_mobile_ceu_buffer, vb);
144}
145
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300146static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
147{
148 unsigned long flags;
149
150 flags = SOCAM_MASTER |
151 SOCAM_PCLK_SAMPLE_RISING |
152 SOCAM_HSYNC_ACTIVE_HIGH |
153 SOCAM_HSYNC_ACTIVE_LOW |
154 SOCAM_VSYNC_ACTIVE_HIGH |
155 SOCAM_VSYNC_ACTIVE_LOW |
156 SOCAM_DATA_ACTIVE_HIGH;
157
158 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
159 flags |= SOCAM_DATAWIDTH_8;
160
161 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
162 flags |= SOCAM_DATAWIDTH_16;
163
164 if (flags & SOCAM_DATAWIDTH_MASK)
165 return flags;
166
167 return 0;
168}
169
Magnus Damm0d3244d2008-07-16 22:59:28 -0300170static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300171 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300172{
173 iowrite32(data, priv->base + reg_offs);
174}
175
Magnus Damm7a1a8162008-12-18 12:50:30 -0300176static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300177{
178 return ioread32(priv->base + reg_offs);
179}
180
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300181static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
182{
183 int i, success = 0;
184 struct soc_camera_device *icd = pcdev->icd;
185
186 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
187
188 /* wait CSTSR.CPTON bit */
189 for (i = 0; i < 1000; i++) {
190 if (!(ceu_read(pcdev, CSTSR) & 1)) {
191 success++;
192 break;
193 }
194 udelay(1);
195 }
196
197 /* wait CAPSR.CPKIL bit */
198 for (i = 0; i < 1000; i++) {
199 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
200 success++;
201 break;
202 }
203 udelay(1);
204 }
205
206
207 if (2 != success) {
208 dev_warn(&icd->dev, "soft reset time out\n");
209 return -EIO;
210 }
211
212 return 0;
213}
214
Magnus Damm0d3244d2008-07-16 22:59:28 -0300215/*
216 * Videobuf operations
217 */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300218static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
219 unsigned int *count, unsigned int *num_planes,
220 unsigned long sizes[], void *alloc_ctxs[])
Magnus Damm0d3244d2008-07-16 22:59:28 -0300221{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300222 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300223 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
224 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300225 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
226 icd->current_fmt->host_fmt);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300227
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300228 if (bytes_per_line < 0)
229 return bytes_per_line;
230
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300231 *num_planes = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300232
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300233 pcdev->sequence = 0;
234 sizes[0] = bytes_per_line * icd->user_height;
235 alloc_ctxs[0] = pcdev->alloc_ctx;
236
237 if (!*count)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300238 *count = 2;
239
240 if (pcdev->video_limit) {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300241 if (PAGE_ALIGN(sizes[0]) * *count > pcdev->video_limit)
242 *count = pcdev->video_limit / PAGE_ALIGN(sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300243 }
244
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300245 dev_dbg(icd->dev.parent, "count=%d, size=%lu\n", *count, sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300246
247 return 0;
248}
249
Magnus Damm7a1a8162008-12-18 12:50:30 -0300250#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
251#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
252#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300253#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300254#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300255#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300256
257
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300258/*
259 * return value doesn't reflex the success/failure to queue the new buffer,
260 * but rather the status of the previous buffer.
261 */
262static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300263{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300264 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300265 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300266 unsigned long top1, top2;
267 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300268 u32 status;
269 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300270
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300271 /*
272 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300273 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
274 * several not-so-well documented interrupt sources in CETCR.
275 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300276 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
277 status = ceu_read(pcdev, CETCR);
278 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300279 if (!pcdev->frozen)
280 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300281 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
282 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300283
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300284 /*
285 * When a VBP interrupt occurs, a capture end interrupt does not occur
286 * and the image of that frame is not captured correctly. So, soft reset
287 * is needed here.
288 */
289 if (status & CEU_CEIER_VBP) {
290 sh_mobile_ceu_soft_reset(pcdev);
291 ret = -EIO;
292 }
293
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300294 if (pcdev->frozen) {
295 complete(&pcdev->complete);
296 return ret;
297 }
298
Magnus Damm8be65dc2008-12-18 12:38:06 -0300299 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300300 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300301
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300302 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
303 top1 = CDBYR;
304 top2 = CDBCR;
305 bottom1 = CDAYR;
306 bottom2 = CDACR;
307 } else {
308 top1 = CDAYR;
309 top2 = CDACR;
310 bottom1 = CDBYR;
311 bottom2 = CDBCR;
312 }
313
Pawel Osciakd6db5bf2011-03-14 10:38:23 -0300314 phys_addr_top = vb2_dma_contig_plane_paddr(pcdev->active, 0);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300315
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300316 ceu_write(pcdev, top1, phys_addr_top);
317 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300318 phys_addr_bottom = phys_addr_top + icd->user_width;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300319 ceu_write(pcdev, bottom1, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300320 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300321
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300322 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300323 case V4L2_PIX_FMT_NV12:
324 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300325 case V4L2_PIX_FMT_NV16:
326 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300327 phys_addr_top += icd->user_width *
328 icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300329 ceu_write(pcdev, top2, phys_addr_top);
330 if (V4L2_FIELD_NONE != pcdev->field) {
331 phys_addr_bottom = phys_addr_top + icd->user_width;
332 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300333 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300334 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300335
Magnus Damm8be65dc2008-12-18 12:38:06 -0300336 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300337
338 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300339}
340
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300341static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300342{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300343 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300344 struct sh_mobile_ceu_buffer *buf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300345 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
346 icd->current_fmt->host_fmt);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300347 unsigned long size;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300348
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300349 if (bytes_per_line < 0)
350 return bytes_per_line;
351
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300352 buf = to_ceu_vb(vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300353
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300354 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
355 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300356
357 /* Added list head initialization on alloc */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300358 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300359
360#ifdef DEBUG
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300361 /*
362 * This can be useful if you want to see if we actually fill
363 * the buffer with something
364 */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300365 if (vb2_plane_vaddr(vb, 0))
366 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300367#endif
368
369 BUG_ON(NULL == icd->current_fmt);
370
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300371 size = icd->user_height * bytes_per_line;
372
373 if (vb2_plane_size(vb, 0) < size) {
374 dev_err(icd->dev.parent, "Buffer too small (%lu < %lu)\n",
375 vb2_plane_size(vb, 0), size);
376 return -ENOBUFS;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300377 }
378
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300379 vb2_set_plane_payload(vb, 0, size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300380
381 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300382}
383
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300384static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300385{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300386 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300387 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
388 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300389 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300390
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300391 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
392 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300393
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300394 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300395 list_add_tail(&buf->queue, &pcdev->capture);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300396
397 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300398 /*
399 * Because there were no active buffer at this moment,
400 * we are not interested in the return value of
401 * sh_mobile_ceu_capture here.
402 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300403 pcdev->active = vb;
404 sh_mobile_ceu_capture(pcdev);
405 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300406 spin_unlock_irq(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300407}
408
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300409static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300410{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300411 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300413 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300414 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300415
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300416 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300417
418 if (pcdev->active == vb) {
419 /* disable capture (release DMA buffer), reset */
420 ceu_write(pcdev, CAPSR, 1 << 16);
421 pcdev->active = NULL;
422 }
423
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300424 /* Doesn't hurt also if the list is empty */
425 list_del_init(&buf->queue);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300426
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300427 spin_unlock_irq(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300428}
429
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300430static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
431{
432 /* This is for locking debugging only */
433 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
434 return 0;
435}
436
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300437static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
438{
439 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
440 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
441 struct sh_mobile_ceu_dev *pcdev = ici->priv;
442 struct list_head *buf_head, *tmp;
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300443
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300444 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300445
446 pcdev->active = NULL;
447
448 list_for_each_safe(buf_head, tmp, &pcdev->capture)
449 list_del_init(buf_head);
450
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300451 spin_unlock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300452
453 return sh_mobile_ceu_soft_reset(pcdev);
454}
455
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300456static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
457 .queue_setup = sh_mobile_ceu_videobuf_setup,
458 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
459 .buf_queue = sh_mobile_ceu_videobuf_queue,
460 .buf_cleanup = sh_mobile_ceu_videobuf_release,
461 .buf_init = sh_mobile_ceu_videobuf_init,
462 .wait_prepare = soc_camera_unlock,
463 .wait_finish = soc_camera_lock,
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300464 .stop_streaming = sh_mobile_ceu_stop_streaming,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300465};
466
467static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
468{
469 struct sh_mobile_ceu_dev *pcdev = data;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300470 struct vb2_buffer *vb;
471 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300472
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300473 spin_lock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300474
475 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300476 if (!vb)
477 /* Stale interrupt from a released buffer */
478 goto out;
479
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300480 list_del_init(&to_ceu_vb(vb)->queue);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300481
482 if (!list_empty(&pcdev->capture))
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300483 pcdev->active = &list_entry(pcdev->capture.next,
484 struct sh_mobile_ceu_buffer, queue)->vb;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300485 else
486 pcdev->active = NULL;
487
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300488 ret = sh_mobile_ceu_capture(pcdev);
489 do_gettimeofday(&vb->v4l2_buf.timestamp);
490 if (!ret) {
491 vb->v4l2_buf.field = pcdev->field;
492 vb->v4l2_buf.sequence = pcdev->sequence++;
493 }
494 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300495
496out:
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300497 spin_unlock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300498
499 return IRQ_HANDLED;
500}
501
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300502/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300503static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
504{
505 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
506 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300507 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300508
Magnus Damm0d3244d2008-07-16 22:59:28 -0300509 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300510 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300511
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300512 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300513 "SuperH Mobile CEU driver attached to camera %d\n",
514 icd->devnum);
515
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900516 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300517
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300518 ret = sh_mobile_ceu_soft_reset(pcdev);
519 if (!ret)
520 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300521
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300522 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300523}
524
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300525/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300526static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
527{
528 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
529 struct sh_mobile_ceu_dev *pcdev = ici->priv;
530
531 BUG_ON(icd != pcdev->icd);
532
533 /* disable capture, disable interrupts */
534 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300535 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300536
537 /* make sure active buffer is canceled */
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300538 spin_lock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300539 if (pcdev->active) {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300540 list_del_init(&to_ceu_vb(pcdev->active)->queue);
541 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
Magnus Damm51354cc2008-10-16 19:51:20 -0300542 pcdev->active = NULL;
543 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300544 spin_unlock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300545
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900546 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300547
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300548 dev_info(icd->dev.parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300549 "SuperH Mobile CEU driver detached from camera %d\n",
550 icd->devnum);
551
552 pcdev->icd = NULL;
553}
554
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300555/*
556 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
557 * in SH7722 Hardware Manual
558 */
559static unsigned int size_dst(unsigned int src, unsigned int scale)
560{
561 unsigned int mant_pre = scale >> 12;
562 if (!src || !scale)
563 return src;
564 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
565 mant_pre * 4096 / scale + 1;
566}
567
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300568static u16 calc_scale(unsigned int src, unsigned int *dst)
569{
570 u16 scale;
571
572 if (src == *dst)
573 return 0;
574
575 scale = (src * 4096 / *dst) & ~7;
576
577 while (scale > 4096 && size_dst(src, scale) < *dst)
578 scale -= 8;
579
580 *dst = size_dst(src, scale);
581
582 return scale;
583}
584
585/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300586static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300587{
588 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
589 struct sh_mobile_ceu_cam *cam = icd->host_priv;
590 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300591 unsigned int height, width, cdwdr_width, in_width, in_height;
592 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300593 u32 camor;
594
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300595 dev_geo(icd->dev.parent, "Crop %ux%u@%u:%u\n",
596 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300597
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300598 left_offset = cam->ceu_left;
599 top_offset = cam->ceu_top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300600
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300601 /* CEU cropping (CFSZR) is applied _after_ the scaling filter (CFLCR) */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300602 if (pcdev->image_mode) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300603 in_width = cam->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300604 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300605 in_width *= 2;
606 left_offset *= 2;
607 }
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300608 width = icd->user_width;
609 cdwdr_width = icd->user_width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300610 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300611 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300612 icd->current_fmt->host_fmt);
613 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300614
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300615 width = icd->user_width;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300616
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300617 switch (icd->current_fmt->host_fmt->packing) {
618 case SOC_MBUS_PACKING_2X8_PADHI:
619 w_factor = 2;
620 break;
621 default:
622 w_factor = 1;
623 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300624
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300625 in_width = cam->width * w_factor;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300626 left_offset = left_offset * w_factor;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300627
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300628 if (bytes_per_line < 0)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300629 cdwdr_width = icd->user_width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300630 else
631 cdwdr_width = bytes_per_line;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300632 }
633
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300634 height = icd->user_height;
635 in_height = cam->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300636 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300637 height /= 2;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300638 in_height /= 2;
639 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300640 cdwdr_width *= 2;
641 }
642
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300643 /* CSI2 special configuration */
644 if (pcdev->pdata->csi2_dev) {
645 in_width = ((in_width - 2) * 2);
646 left_offset *= 2;
647 }
648
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300649 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300650 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300651
652 dev_geo(icd->dev.parent,
653 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
654 (in_height << 16) | in_width, (height << 16) | width,
655 cdwdr_width);
656
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300657 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300658 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300659 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300660 ceu_write(pcdev, CDWDR, cdwdr_width);
661}
662
663static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
664{
665 u32 capsr = ceu_read(pcdev, CAPSR);
666 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
667 return capsr;
668}
669
670static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
671{
672 unsigned long timeout = jiffies + 10 * HZ;
673
674 /*
675 * Wait until the end of the current frame. It can take a long time,
676 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
677 */
678 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
679 msleep(1);
680
681 if (time_after(jiffies, timeout)) {
682 dev_err(pcdev->ici.v4l2_dev.dev,
683 "Timeout waiting for frame end! Interface problem?\n");
684 return;
685 }
686
687 /* Wait until reset clears, this shall not hang... */
688 while (ceu_read(pcdev, CAPSR) & (1 << 16))
689 udelay(10);
690
691 /* Anything to restore? */
692 if (capsr & ~(1 << 16))
693 ceu_write(pcdev, CAPSR, capsr);
694}
695
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300696/* Capture is not running, no interrupts, no locking needed */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300697static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
698 __u32 pixfmt)
699{
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300702 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300703 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300704 int yuv_lineskip;
705 struct sh_mobile_ceu_cam *cam = icd->host_priv;
706 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300707
708 camera_flags = icd->ops->query_bus_param(icd);
709 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300710 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300711 if (!common_flags)
712 return -EINVAL;
713
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300714 /* Make choises, based on platform preferences */
715 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
716 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
717 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
718 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
719 else
720 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
721 }
722
723 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
724 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
725 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
726 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
727 else
728 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
729 }
730
Magnus Damm0d3244d2008-07-16 22:59:28 -0300731 ret = icd->ops->set_bus_param(icd, common_flags);
732 if (ret < 0)
733 return ret;
734
735 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
736 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300737 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300738 break;
739 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300740 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300741 break;
742 default:
743 return -EINVAL;
744 }
745
746 ceu_write(pcdev, CRCNTR, 0);
747 ceu_write(pcdev, CRCMPR, 0);
748
Magnus Damm8be65dc2008-12-18 12:38:06 -0300749 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300750 yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300751
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300752 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300753 case V4L2_PIX_FMT_NV12:
754 case V4L2_PIX_FMT_NV21:
755 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300756 /* fall-through */
757 case V4L2_PIX_FMT_NV16:
758 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300759 switch (cam->code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300760 case V4L2_MBUS_FMT_UYVY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300761 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
762 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300763 case V4L2_MBUS_FMT_VYUY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300764 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
765 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300766 case V4L2_MBUS_FMT_YUYV8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300767 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
768 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300769 case V4L2_MBUS_FMT_YVYU8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300770 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
771 break;
772 default:
773 BUG();
774 }
775 }
776
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300777 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
778 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300779 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300780
Magnus Damm7a1a8162008-12-18 12:50:30 -0300781 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
782 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300783 value |= pcdev->is_16bit ? 1 << 12 : 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300784
785 /* CSI2 mode */
786 if (pcdev->pdata->csi2_dev)
787 value |= 3 << 12;
788
Magnus Damm0d3244d2008-07-16 22:59:28 -0300789 ceu_write(pcdev, CAMCR, value);
790
791 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300792
793 switch (pcdev->field) {
794 case V4L2_FIELD_INTERLACED_TB:
795 value = 0x101;
796 break;
797 case V4L2_FIELD_INTERLACED_BT:
798 value = 0x102;
799 break;
800 default:
801 value = 0;
802 break;
803 }
804 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300805
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300806 sh_mobile_ceu_set_rect(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300807 mdelay(1);
808
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300809 dev_geo(icd->dev.parent, "CFLCR 0x%x\n", pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300810 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300811
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300812 /*
813 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300814 *
815 * In data fetch mode bytes are received in chunks of 8 bytes.
816 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
817 *
818 * The data is however by default written to memory in reverse order:
819 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
820 *
821 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300822 * using 7 we swap the data bytes to match the incoming order:
823 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300824 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300825 value = 0x00000017;
826 if (yuv_lineskip)
827 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
828
829 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300830 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
831
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300832 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300833 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
834 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300835 icd->user_width, icd->user_height);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300836
837 capture_restore(pcdev, capsr);
838
Magnus Damm0d3244d2008-07-16 22:59:28 -0300839 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300840 return 0;
841}
842
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300843static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
844 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300845{
846 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
847 struct sh_mobile_ceu_dev *pcdev = ici->priv;
848 unsigned long camera_flags, common_flags;
849
850 camera_flags = icd->ops->query_bus_param(icd);
851 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300852 make_bus_param(pcdev));
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300853 if (!common_flags || buswidth > 16 ||
854 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
Magnus Damm0d3244d2008-07-16 22:59:28 -0300855 return -EINVAL;
856
857 return 0;
858}
859
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300860static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300861 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300862 .fourcc = V4L2_PIX_FMT_NV12,
863 .name = "NV12",
864 .bits_per_sample = 12,
865 .packing = SOC_MBUS_PACKING_NONE,
866 .order = SOC_MBUS_ORDER_LE,
867 }, {
868 .fourcc = V4L2_PIX_FMT_NV21,
869 .name = "NV21",
870 .bits_per_sample = 12,
871 .packing = SOC_MBUS_PACKING_NONE,
872 .order = SOC_MBUS_ORDER_LE,
873 }, {
874 .fourcc = V4L2_PIX_FMT_NV16,
875 .name = "NV16",
876 .bits_per_sample = 16,
877 .packing = SOC_MBUS_PACKING_NONE,
878 .order = SOC_MBUS_ORDER_LE,
879 }, {
880 .fourcc = V4L2_PIX_FMT_NV61,
881 .name = "NV61",
882 .bits_per_sample = 16,
883 .packing = SOC_MBUS_PACKING_NONE,
884 .order = SOC_MBUS_ORDER_LE,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300885 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300886};
887
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300888/* This will be corrected as we get more formats */
889static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
890{
891 return fmt->packing == SOC_MBUS_PACKING_NONE ||
892 (fmt->bits_per_sample == 8 &&
893 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
894 (fmt->bits_per_sample > 8 &&
895 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
896}
897
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300898static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
899
Hans Verkuil3805f202010-05-08 17:55:00 -0300900static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
Magnus Damm9414de32008-12-18 11:49:06 -0300901 struct soc_camera_format_xlate *xlate)
902{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300903 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300904 struct device *dev = icd->dev.parent;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300905 struct soc_camera_host *ici = to_soc_camera_host(dev);
906 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300907 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300908 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300909 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300910 enum v4l2_mbus_pixelcode code;
911 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -0300912
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300913 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
914 if (ret < 0)
915 /* No more formats */
916 return 0;
917
918 fmt = soc_mbus_get_fmtdesc(code);
919 if (!fmt) {
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300920 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
921 return 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300922 }
923
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300924 if (!pcdev->pdata->csi2_dev) {
925 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
926 if (ret < 0)
927 return 0;
928 }
Magnus Damm9414de32008-12-18 11:49:06 -0300929
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300930 if (!icd->host_priv) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300931 struct v4l2_mbus_framefmt mf;
932 struct v4l2_rect rect;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300933 int shift = 0;
934
935 /* FIXME: subwindow is lost between close / open */
936
937 /* Cache current client geometry */
938 ret = client_g_rect(sd, &rect);
939 if (ret < 0)
940 return ret;
941
942 /* First time */
943 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
944 if (ret < 0)
945 return ret;
946
947 while ((mf.width > 2560 || mf.height > 1920) && shift < 4) {
948 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
949 mf.width = 2560 >> shift;
950 mf.height = 1920 >> shift;
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -0300951 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300952 s_mbus_fmt, &mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300953 if (ret < 0)
954 return ret;
955 shift++;
956 }
957
958 if (shift == 4) {
959 dev_err(dev, "Failed to configure the client below %ux%x\n",
960 mf.width, mf.height);
961 return -EIO;
962 }
963
964 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
965
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300966 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
967 if (!cam)
968 return -ENOMEM;
969
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300970 /* We are called with current camera crop, initialise subrect with it */
971 cam->rect = rect;
972 cam->subrect = rect;
973
974 cam->width = mf.width;
975 cam->height = mf.height;
976
977 cam->width = mf.width;
978 cam->height = mf.height;
979
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300980 icd->host_priv = cam;
981 } else {
982 cam = icd->host_priv;
983 }
984
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300985 /* Beginning of a pass */
986 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300987 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300988
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300989 switch (code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300990 case V4L2_MBUS_FMT_UYVY8_2X8:
991 case V4L2_MBUS_FMT_VYUY8_2X8:
992 case V4L2_MBUS_FMT_YUYV8_2X8:
993 case V4L2_MBUS_FMT_YVYU8_2X8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300994 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300995 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300996
997 /*
998 * Our case is simple so far: for any of the above four camera
999 * formats we add all our four synthesized NV* formats, so,
1000 * just marking the device with a single flag suffices. If
1001 * the format generation rules are more complex, you would have
1002 * to actually hang your already added / counted formats onto
1003 * the host_priv pointer and check whether the format you're
1004 * going to add now is already there.
1005 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001006 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001007
Magnus Damm8be65dc2008-12-18 12:38:06 -03001008 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1009 formats += n;
1010 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001011 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1012 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001013 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001014 dev_dbg(dev, "Providing format %s using code %d\n",
1015 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -03001016 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001017 break;
Magnus Damm9414de32008-12-18 11:49:06 -03001018 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001019 if (!sh_mobile_ceu_packing_supported(fmt))
1020 return 0;
1021 }
1022
1023 /* Generic pass-through */
1024 formats++;
1025 if (xlate) {
1026 xlate->host_fmt = fmt;
1027 xlate->code = code;
1028 xlate++;
1029 dev_dbg(dev, "Providing format %s in pass-through mode\n",
Guennadi Liakhovetskie53a4802010-06-18 04:20:56 -03001030 fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -03001031 }
1032
1033 return formats;
1034}
1035
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001036static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1037{
1038 kfree(icd->host_priv);
1039 icd->host_priv = NULL;
1040}
1041
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001042/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001043static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1044{
1045 return r1->width < r2->width || r1->height < r2->height;
1046}
1047
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001048/* Check if r1 fails to cover r2 */
1049static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1050{
1051 return r1->left > r2->left || r1->top > r2->top ||
1052 r1->left + r1->width < r2->left + r2->width ||
1053 r1->top + r1->height < r2->top + r2->height;
1054}
1055
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001056static unsigned int scale_down(unsigned int size, unsigned int scale)
1057{
1058 return (size * 4096 + scale / 2) / scale;
1059}
1060
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001061static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1062{
1063 return (input * 4096 + output / 2) / output;
1064}
1065
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001066/* Get and store current client crop */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001067static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1068{
1069 struct v4l2_crop crop;
1070 struct v4l2_cropcap cap;
1071 int ret;
1072
1073 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1074
1075 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1076 if (!ret) {
1077 *rect = crop.c;
1078 return ret;
1079 }
1080
1081 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1082 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1083
1084 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001085 if (!ret)
1086 *rect = cap.defrect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001087
1088 return ret;
1089}
1090
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001091/* Client crop has changed, update our sub-rectangle to remain within the area */
1092static void update_subrect(struct sh_mobile_ceu_cam *cam)
1093{
1094 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1095
1096 if (rect->width < subrect->width)
1097 subrect->width = rect->width;
1098
1099 if (rect->height < subrect->height)
1100 subrect->height = rect->height;
1101
1102 if (rect->left > subrect->left)
1103 subrect->left = rect->left;
1104 else if (rect->left + rect->width >
1105 subrect->left + subrect->width)
1106 subrect->left = rect->left + rect->width -
1107 subrect->width;
1108
1109 if (rect->top > subrect->top)
1110 subrect->top = rect->top;
1111 else if (rect->top + rect->height >
1112 subrect->top + subrect->height)
1113 subrect->top = rect->top + rect->height -
1114 subrect->height;
1115}
1116
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001117/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001118 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001119 * 1. try if the client can produce exactly what requested by the user
1120 * 2. if (1) failed, try to double the client image until we get one big enough
1121 * 3. if (2) failed, try to request the maximum image
1122 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001123static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001124 struct v4l2_crop *cam_crop)
1125{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001126 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001127 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1128 struct device *dev = sd->v4l2_dev->dev;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001129 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001130 struct v4l2_cropcap cap;
1131 int ret;
1132 unsigned int width, height;
1133
1134 v4l2_subdev_call(sd, video, s_crop, crop);
1135 ret = client_g_rect(sd, cam_rect);
1136 if (ret < 0)
1137 return ret;
1138
1139 /*
1140 * Now cam_crop contains the current camera input rectangle, and it must
1141 * be within camera cropcap bounds
1142 */
1143 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1144 /* Even if camera S_CROP failed, but camera rectangle matches */
Márton Némethe26b3142010-02-24 17:13:29 -03001145 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001146 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001147 cam->rect = *cam_rect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001148 return 0;
1149 }
1150
1151 /* Try to fix cropping, that camera hasn't managed to set */
Márton Némethe26b3142010-02-24 17:13:29 -03001152 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 -03001153 cam_rect->width, cam_rect->height,
1154 cam_rect->left, cam_rect->top,
1155 rect->width, rect->height, rect->left, rect->top);
1156
1157 /* We need sensor maximum rectangle */
1158 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1159 if (ret < 0)
1160 return ret;
1161
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001162 /* Put user requested rectangle within sensor bounds */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001163 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1164 cap.bounds.width);
1165 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1166 cap.bounds.height);
1167
1168 /*
1169 * Popular special case - some cameras can only handle fixed sizes like
1170 * QVGA, VGA,... Take care to avoid infinite loop.
1171 */
1172 width = max(cam_rect->width, 2);
1173 height = max(cam_rect->height, 2);
1174
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001175 /*
1176 * Loop as long as sensor is not covering the requested rectangle and
1177 * is still within its bounds
1178 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001179 while (!ret && (is_smaller(cam_rect, rect) ||
1180 is_inside(cam_rect, rect)) &&
1181 (cap.bounds.width > width || cap.bounds.height > height)) {
1182
1183 width *= 2;
1184 height *= 2;
1185
1186 cam_rect->width = width;
1187 cam_rect->height = height;
1188
1189 /*
1190 * We do not know what capabilities the camera has to set up
1191 * left and top borders. We could try to be smarter in iterating
1192 * them, e.g., if camera current left is to the right of the
1193 * target left, set it to the middle point between the current
1194 * left and minimum left. But that would add too much
1195 * complexity: we would have to iterate each border separately.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001196 * Instead we just drop to the left and top bounds.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001197 */
1198 if (cam_rect->left > rect->left)
1199 cam_rect->left = cap.bounds.left;
1200
1201 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1202 cam_rect->width = rect->left + rect->width -
1203 cam_rect->left;
1204
1205 if (cam_rect->top > rect->top)
1206 cam_rect->top = cap.bounds.top;
1207
1208 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1209 cam_rect->height = rect->top + rect->height -
1210 cam_rect->top;
1211
1212 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1213 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001214 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001215 cam_rect->width, cam_rect->height,
1216 cam_rect->left, cam_rect->top);
1217 }
1218
1219 /* S_CROP must not modify the rectangle */
1220 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1221 /*
1222 * The camera failed to configure a suitable cropping,
1223 * we cannot use the current rectangle, set to max
1224 */
1225 *cam_rect = cap.bounds;
1226 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1227 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001228 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001229 cam_rect->width, cam_rect->height,
1230 cam_rect->left, cam_rect->top);
1231 }
1232
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001233 if (!ret) {
1234 cam->rect = *cam_rect;
1235 update_subrect(cam);
1236 }
1237
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001238 return ret;
1239}
1240
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001241/* Iterative s_mbus_fmt, also updates cached client crop on success */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001242static int client_s_fmt(struct soc_camera_device *icd,
1243 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001244{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001245 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001246 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1247 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001248 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001249 unsigned int max_width, max_height;
1250 struct v4l2_cropcap cap;
1251 int ret;
1252
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -03001253 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001254 s_mbus_fmt, mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001255 if (ret < 0)
1256 return ret;
1257
1258 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1259
1260 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
1261 goto update_cache;
1262
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001263 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1264
1265 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1266 if (ret < 0)
1267 return ret;
1268
1269 max_width = min(cap.bounds.width, 2560);
1270 max_height = min(cap.bounds.height, 1920);
1271
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001272 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001273 tmp_w = mf->width;
1274 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001275
1276 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1277 while ((width > tmp_w || height > tmp_h) &&
1278 tmp_w < max_width && tmp_h < max_height) {
1279 tmp_w = min(2 * tmp_w, max_width);
1280 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001281 mf->width = tmp_w;
1282 mf->height = tmp_h;
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -03001283 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001284 s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001285 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001286 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001287 if (ret < 0) {
1288 /* This shouldn't happen */
1289 dev_err(dev, "Client failed to set format: %d\n", ret);
1290 return ret;
1291 }
1292 }
1293
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001294update_cache:
1295 /* Update cache */
1296 ret = client_g_rect(sd, &cam->rect);
1297 if (ret < 0)
1298 return ret;
1299
1300 update_subrect(cam);
1301
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001302 return 0;
1303}
1304
1305/**
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001306 * @width - on output: user width, mapped back to input
1307 * @height - on output: user height, mapped back to input
1308 * @mf - in- / output camera output window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001309 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001310static int client_scale(struct soc_camera_device *icd,
1311 struct v4l2_mbus_framefmt *mf,
1312 unsigned int *width, unsigned int *height,
1313 bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001314{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001315 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1316 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001317 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001318 unsigned int scale_h, scale_v;
1319 int ret;
1320
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001321 /*
1322 * 5. Apply iterative camera S_FMT for camera user window (also updates
1323 * client crop cache and the imaginary sub-rectangle).
1324 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001325 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001326 if (ret < 0)
1327 return ret;
1328
1329 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001330 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001331
1332 /* 6. Retrieve camera output window (g_fmt) */
1333
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001334 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001335
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001336 /* 7. Calculate new client scales. */
1337 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1338 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001339
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001340 mf->width = mf_tmp.width;
1341 mf->height = mf_tmp.height;
1342 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001343
1344 /*
1345 * 8. Calculate new CEU crop - apply camera scales to previously
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001346 * updated "effective" crop.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001347 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001348 *width = scale_down(cam->subrect.width, scale_h);
1349 *height = scale_down(cam->subrect.height, scale_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001350
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001351 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001352
1353 return 0;
1354}
1355
1356/*
1357 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1358 * framerate by always requesting the maximum image from the client. See
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001359 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001360 * scaling and cropping algorithms and for the meaning of referenced here steps.
1361 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001362static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001363 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001364{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001365 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001366 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1367 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001368 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001369 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001370 struct v4l2_rect *cam_rect = &cam_crop.c;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001371 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001372 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001373 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001374 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001375 out_width, out_height;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001376 int interm_width, interm_height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001377 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001378 int ret;
1379
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001380 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1381 rect->left, rect->top);
1382
1383 /* During camera cropping its output window can change too, stop CEU */
1384 capsr = capture_save_reset(pcdev);
1385 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1386
1387 /* 1. - 2. Apply iterative camera S_CROP for new input window. */
1388 ret = client_s_crop(icd, a, &cam_crop);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001389 if (ret < 0)
1390 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001391
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001392 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001393 cam_rect->width, cam_rect->height,
1394 cam_rect->left, cam_rect->top);
1395
1396 /* On success cam_crop contains current camera crop */
1397
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001398 /* 3. Retrieve camera output window */
1399 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1400 if (ret < 0)
1401 return ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001402
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001403 if (mf.width > 2560 || mf.height > 1920)
1404 return -EINVAL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001405
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001406 /* 4. Calculate camera scales */
1407 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1408 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001409
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001410 /* Calculate intermediate window */
1411 interm_width = scale_down(rect->width, scale_cam_h);
1412 interm_height = scale_down(rect->height, scale_cam_v);
1413
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001414 if (interm_width < icd->user_width) {
1415 u32 new_scale_h;
1416
1417 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1418
1419 mf.width = scale_down(cam_rect->width, new_scale_h);
1420 }
1421
1422 if (interm_height < icd->user_height) {
1423 u32 new_scale_v;
1424
1425 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1426
1427 mf.height = scale_down(cam_rect->height, new_scale_v);
1428 }
1429
1430 if (interm_width < icd->user_width || interm_height < icd->user_height) {
1431 ret = v4l2_device_call_until_err(sd->v4l2_dev, (int)icd, video,
1432 s_mbus_fmt, &mf);
1433 if (ret < 0)
1434 return ret;
1435
1436 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1437 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1438 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1439 interm_width = scale_down(rect->width, scale_cam_h);
1440 interm_height = scale_down(rect->height, scale_cam_v);
1441 }
1442
1443 /* Cache camera output window */
1444 cam->width = mf.width;
1445 cam->height = mf.height;
1446
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001447 if (pcdev->image_mode) {
1448 out_width = min(interm_width, icd->user_width);
1449 out_height = min(interm_height, icd->user_height);
1450 } else {
1451 out_width = interm_width;
1452 out_height = interm_height;
1453 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001454
1455 /*
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001456 * 5. Calculate CEU scales from camera scales from results of (5) and
1457 * the user window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001458 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001459 scale_ceu_h = calc_scale(interm_width, &out_width);
1460 scale_ceu_v = calc_scale(interm_height, &out_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001461
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001462 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001463
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001464 /* Apply CEU scales. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001465 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1466 if (cflcr != pcdev->cflcr) {
1467 pcdev->cflcr = cflcr;
1468 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001469 }
1470
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001471 icd->user_width = out_width;
1472 icd->user_height = out_height;
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001473 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1474 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001475
1476 /* 6. Use CEU cropping to crop to the new window. */
1477 sh_mobile_ceu_set_rect(icd);
1478
1479 cam->subrect = *rect;
1480
1481 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1482 icd->user_width, icd->user_height,
1483 cam->ceu_left, cam->ceu_top);
1484
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001485 /* Restore capture. The CE bit can be cleared by the hardware */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001486 if (pcdev->active)
1487 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001488 capture_restore(pcdev, capsr);
1489
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001490 /* Even if only camera cropping succeeded */
1491 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001492}
1493
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001494static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1495 struct v4l2_crop *a)
1496{
1497 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1498
1499 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1500 a->c = cam->subrect;
1501
1502 return 0;
1503}
1504
1505/*
1506 * Calculate real client output window by applying new scales to the current
1507 * client crop. New scales are calculated from the requested output format and
1508 * CEU crop, mapped backed onto the client input (subrect).
1509 */
1510static void calculate_client_output(struct soc_camera_device *icd,
1511 struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
1512{
1513 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1514 struct device *dev = icd->dev.parent;
1515 struct v4l2_rect *cam_subrect = &cam->subrect;
1516 unsigned int scale_v, scale_h;
1517
1518 if (cam_subrect->width == cam->rect.width &&
1519 cam_subrect->height == cam->rect.height) {
1520 /* No sub-cropping */
1521 mf->width = pix->width;
1522 mf->height = pix->height;
1523 return;
1524 }
1525
1526 /* 1.-2. Current camera scales and subwin - cached. */
1527
1528 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1529 cam_subrect->width, cam_subrect->height,
1530 cam_subrect->left, cam_subrect->top);
1531
1532 /*
1533 * 3. Calculate new combined scales from input sub-window to requested
1534 * user window.
1535 */
1536
1537 /*
1538 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1539 * (128x96) or larger than VGA
1540 */
1541 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1542 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1543
1544 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1545
1546 /*
1547 * 4. Calculate client output window by applying combined scales to real
1548 * input window.
1549 */
1550 mf->width = scale_down(cam->rect.width, scale_h);
1551 mf->height = scale_down(cam->rect.height, scale_v);
1552}
1553
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001554/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001555static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001556 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001557{
Magnus Damm9414de32008-12-18 11:49:06 -03001558 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001559 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001560 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1561 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001562 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001563 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001564 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001565 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001566 /* Keep Compiler Happy */
1567 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001568 u16 scale_v, scale_h;
1569 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001570 bool image_mode;
1571 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001572
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001573 dev_geo(dev, "S_FMT(pix=0x%x, %ux%u)\n", pixfmt, pix->width, pix->height);
1574
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001575 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001576 default:
1577 pix->field = V4L2_FIELD_NONE;
1578 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001579 case V4L2_FIELD_INTERLACED_TB:
1580 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001581 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001582 field = pix->field;
1583 break;
1584 case V4L2_FIELD_INTERLACED:
1585 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001586 break;
1587 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001588
Magnus Damm9414de32008-12-18 11:49:06 -03001589 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1590 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001591 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001592 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001593 }
1594
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001595 /* 1.-4. Calculate client output geometry */
1596 calculate_client_output(icd, &f->fmt.pix, &mf);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001597 mf.field = pix->field;
1598 mf.colorspace = pix->colorspace;
1599 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001600
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001601 switch (pixfmt) {
1602 case V4L2_PIX_FMT_NV12:
1603 case V4L2_PIX_FMT_NV21:
1604 case V4L2_PIX_FMT_NV16:
1605 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001606 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001607 break;
1608 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001609 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001610 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001611
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001612 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001613
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001614 /* 5. - 9. */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001615 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001616 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001617
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001618 dev_geo(dev, "5-9: client scale return %d\n", ret);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001619
1620 /* Done with the camera. Now see if we can improve the result */
1621
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001622 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1623 mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001624 if (ret < 0)
1625 return ret;
1626
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001627 if (mf.code != xlate->code)
1628 return -EINVAL;
1629
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001630 /* 9. Prepare CEU crop */
1631 cam->width = mf.width;
1632 cam->height = mf.height;
1633
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001634 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001635
1636 /* We cannot scale up */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001637 if (pix->width > ceu_sub_width)
1638 ceu_sub_width = pix->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001639
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001640 if (pix->height > ceu_sub_height)
1641 ceu_sub_height = pix->height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001642
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001643 pix->colorspace = mf.colorspace;
1644
1645 if (image_mode) {
1646 /* Scale pix->{width x height} down to width x height */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001647 scale_h = calc_scale(ceu_sub_width, &pix->width);
1648 scale_v = calc_scale(ceu_sub_height, &pix->height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001649 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001650 pix->width = ceu_sub_width;
1651 pix->height = ceu_sub_height;
1652 scale_h = 0;
1653 scale_v = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001654 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001655
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001656 pcdev->cflcr = scale_h | (scale_v << 16);
1657
1658 /*
1659 * We have calculated CFLCR, the actual configuration will be performed
1660 * in sh_mobile_ceu_set_bus_param()
1661 */
1662
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001663 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001664 ceu_sub_width, scale_h, pix->width,
1665 ceu_sub_height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001666
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001667 cam->code = xlate->code;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001668 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001669
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001670 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001671 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001672
1673 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001674}
1675
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001676static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1677 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001678{
Magnus Damm9414de32008-12-18 11:49:06 -03001679 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001680 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001681 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001682 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001683 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001684 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001685 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001686
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001687 dev_geo(icd->dev.parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1688 pixfmt, pix->width, pix->height);
1689
Magnus Damm9414de32008-12-18 11:49:06 -03001690 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1691 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001692 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001693 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001694 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001695
Magnus Damm0d3244d2008-07-16 22:59:28 -03001696 /* FIXME: calculate using depth and bus width */
1697
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001698 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1699 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001700
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001701 width = pix->width;
1702 height = pix->height;
1703
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001704 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
Dan Carpenter981cbef2010-04-07 06:41:14 -03001705 if ((int)pix->bytesperline < 0)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001706 return pix->bytesperline;
1707 pix->sizeimage = height * pix->bytesperline;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001708
Magnus Damm0d3244d2008-07-16 22:59:28 -03001709 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001710 mf.width = pix->width;
1711 mf.height = pix->height;
1712 mf.field = pix->field;
1713 mf.code = xlate->code;
1714 mf.colorspace = pix->colorspace;
1715
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -03001716 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001717 if (ret < 0)
1718 return ret;
1719
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001720 pix->width = mf.width;
1721 pix->height = mf.height;
1722 pix->field = mf.field;
1723 pix->colorspace = mf.colorspace;
1724
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001725 switch (pixfmt) {
1726 case V4L2_PIX_FMT_NV12:
1727 case V4L2_PIX_FMT_NV21:
1728 case V4L2_PIX_FMT_NV16:
1729 case V4L2_PIX_FMT_NV61:
1730 /* FIXME: check against rect_max after converting soc-camera */
1731 /* We can scale precisely, need a bigger image from camera */
1732 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001733 /*
1734 * We presume, the sensor behaves sanely, i.e., if
1735 * requested a bigger rectangle, it will not return a
1736 * smaller one.
1737 */
1738 mf.width = 2560;
1739 mf.height = 1920;
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -03001740 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001741 try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001742 if (ret < 0) {
1743 /* Shouldn't actually happen... */
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001744 dev_err(icd->dev.parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001745 "FIXME: client try_fmt() = %d\n", ret);
1746 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001747 }
1748 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001749 /* We will scale exactly */
1750 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001751 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001752 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001753 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001754 }
1755
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001756 dev_geo(icd->dev.parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1757 __func__, ret, pix->pixelformat, pix->width, pix->height);
1758
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001759 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001760}
1761
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001762static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1763 struct v4l2_crop *a)
1764{
1765 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1766 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1767 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1768 u32 out_width = icd->user_width, out_height = icd->user_height;
1769 int ret;
1770
1771 /* Freeze queue */
1772 pcdev->frozen = 1;
1773 /* Wait for frame */
1774 ret = wait_for_completion_interruptible(&pcdev->complete);
1775 /* Stop the client */
1776 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1777 if (ret < 0)
1778 dev_warn(icd->dev.parent,
1779 "Client failed to stop the stream: %d\n", ret);
1780 else
1781 /* Do the crop, if it fails, there's nothing more we can do */
1782 sh_mobile_ceu_set_crop(icd, a);
1783
1784 dev_geo(icd->dev.parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1785
1786 if (icd->user_width != out_width || icd->user_height != out_height) {
1787 struct v4l2_format f = {
1788 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1789 .fmt.pix = {
1790 .width = out_width,
1791 .height = out_height,
1792 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1793 .field = pcdev->field,
1794 .colorspace = icd->colorspace,
1795 },
1796 };
1797 ret = sh_mobile_ceu_set_fmt(icd, &f);
1798 if (!ret && (out_width != f.fmt.pix.width ||
1799 out_height != f.fmt.pix.height))
1800 ret = -EINVAL;
1801 if (!ret) {
1802 icd->user_width = out_width;
1803 icd->user_height = out_height;
1804 ret = sh_mobile_ceu_set_bus_param(icd,
1805 icd->current_fmt->host_fmt->fourcc);
1806 }
1807 }
1808
1809 /* Thaw the queue */
1810 pcdev->frozen = 0;
1811 spin_lock_irq(&pcdev->lock);
1812 sh_mobile_ceu_capture(pcdev);
1813 spin_unlock_irq(&pcdev->lock);
1814 /* Start the client */
1815 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1816 return ret;
1817}
1818
Magnus Damm0d3244d2008-07-16 22:59:28 -03001819static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1820{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -03001821 struct soc_camera_device *icd = file->private_data;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001822
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001823 return vb2_poll(&icd->vb2_vidq, file, pt);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001824}
1825
1826static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1827 struct v4l2_capability *cap)
1828{
1829 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1830 cap->version = KERNEL_VERSION(0, 0, 5);
1831 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1832 return 0;
1833}
1834
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001835static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1836 struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001837{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001838 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1839 q->io_modes = VB2_MMAP | VB2_USERPTR;
1840 q->drv_priv = icd;
1841 q->ops = &sh_mobile_ceu_videobuf_ops;
1842 q->mem_ops = &vb2_dma_contig_memops;
1843 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001844
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001845 return vb2_queue_init(q);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001846}
1847
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001848static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1849 struct v4l2_control *ctrl)
1850{
1851 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1852 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1853 u32 val;
1854
1855 switch (ctrl->id) {
1856 case V4L2_CID_SHARPNESS:
1857 val = ceu_read(pcdev, CLFCR);
1858 ctrl->value = val ^ 1;
1859 return 0;
1860 }
1861 return -ENOIOCTLCMD;
1862}
1863
1864static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1865 struct v4l2_control *ctrl)
1866{
1867 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1868 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1869
1870 switch (ctrl->id) {
1871 case V4L2_CID_SHARPNESS:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001872 switch (icd->current_fmt->host_fmt->fourcc) {
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001873 case V4L2_PIX_FMT_NV12:
1874 case V4L2_PIX_FMT_NV21:
1875 case V4L2_PIX_FMT_NV16:
1876 case V4L2_PIX_FMT_NV61:
1877 ceu_write(pcdev, CLFCR, !ctrl->value);
1878 return 0;
1879 }
1880 return -EINVAL;
1881 }
1882 return -ENOIOCTLCMD;
1883}
1884
1885static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1886 {
1887 .id = V4L2_CID_SHARPNESS,
1888 .type = V4L2_CTRL_TYPE_BOOLEAN,
1889 .name = "Low-pass filter",
1890 .minimum = 0,
1891 .maximum = 1,
1892 .step = 1,
1893 .default_value = 0,
1894 },
1895};
1896
Magnus Damm0d3244d2008-07-16 22:59:28 -03001897static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1898 .owner = THIS_MODULE,
1899 .add = sh_mobile_ceu_add_device,
1900 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03001901 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001902 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001903 .get_crop = sh_mobile_ceu_get_crop,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001904 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001905 .set_livecrop = sh_mobile_ceu_set_livecrop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001906 .set_fmt = sh_mobile_ceu_set_fmt,
1907 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001908 .set_ctrl = sh_mobile_ceu_set_ctrl,
1909 .get_ctrl = sh_mobile_ceu_get_ctrl,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001910 .poll = sh_mobile_ceu_poll,
1911 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001912 .set_bus_param = sh_mobile_ceu_set_bus_param,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001913 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001914 .controls = sh_mobile_ceu_controls,
1915 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001916};
1917
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001918struct bus_wait {
1919 struct notifier_block notifier;
1920 struct completion completion;
1921 struct device *dev;
1922};
1923
1924static int bus_notify(struct notifier_block *nb,
1925 unsigned long action, void *data)
1926{
1927 struct device *dev = data;
1928 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1929
1930 if (wait->dev != dev)
1931 return NOTIFY_DONE;
1932
1933 switch (action) {
1934 case BUS_NOTIFY_UNBOUND_DRIVER:
1935 /* Protect from module unloading */
1936 wait_for_completion(&wait->completion);
1937 return NOTIFY_OK;
1938 }
1939 return NOTIFY_DONE;
1940}
1941
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001942static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001943{
1944 struct sh_mobile_ceu_dev *pcdev;
1945 struct resource *res;
1946 void __iomem *base;
1947 unsigned int irq;
1948 int err = 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001949 struct bus_wait wait = {
1950 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1951 .notifier.notifier_call = bus_notify,
1952 };
1953 struct device *csi2;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001954
1955 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1956 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03001957 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03001958 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1959 err = -ENODEV;
1960 goto exit;
1961 }
1962
1963 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1964 if (!pcdev) {
1965 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1966 err = -ENOMEM;
1967 goto exit;
1968 }
1969
Magnus Damm0d3244d2008-07-16 22:59:28 -03001970 INIT_LIST_HEAD(&pcdev->capture);
1971 spin_lock_init(&pcdev->lock);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001972 init_completion(&pcdev->complete);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001973
1974 pcdev->pdata = pdev->dev.platform_data;
1975 if (!pcdev->pdata) {
1976 err = -EINVAL;
1977 dev_err(&pdev->dev, "CEU platform data not set.\n");
1978 goto exit_kfree;
1979 }
1980
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001981 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001982 if (!base) {
1983 err = -ENXIO;
1984 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1985 goto exit_kfree;
1986 }
1987
1988 pcdev->irq = irq;
1989 pcdev->base = base;
1990 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001991
1992 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1993 if (res) {
1994 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1995 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001996 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001997 DMA_MEMORY_MAP |
1998 DMA_MEMORY_EXCLUSIVE);
1999 if (!err) {
2000 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2001 err = -ENXIO;
2002 goto exit_iounmap;
2003 }
2004
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002005 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002006 }
2007
2008 /* request irq */
2009 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03002010 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002011 if (err) {
2012 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2013 goto exit_release_mem;
2014 }
2015
Magnus Damm6d1386c2009-08-14 10:49:17 +00002016 pm_suspend_ignore_children(&pdev->dev, true);
2017 pm_runtime_enable(&pdev->dev);
2018 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09002019
Magnus Damm0d3244d2008-07-16 22:59:28 -03002020 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002021 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002022 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03002023 pcdev->ici.drv_name = dev_name(&pdev->dev);
2024 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002025
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002026 /* CSI2 interfacing */
2027 csi2 = pcdev->pdata->csi2_dev;
2028 if (csi2) {
2029 wait.dev = csi2;
2030
2031 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2032 if (err < 0)
2033 goto exit_free_clk;
2034
2035 /*
2036 * From this point the driver module will not unload, until
2037 * we complete the completion.
2038 */
2039
Guennadi Liakhovetskicd05e792010-10-20 10:14:57 -03002040 if (!csi2->driver) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002041 complete(&wait.completion);
2042 /* Either too late, or probing failed */
2043 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2044 err = -ENXIO;
2045 goto exit_free_clk;
2046 }
2047
2048 /*
2049 * The module is still loaded, in the worst case it is hanging
2050 * in device release on our completion. So, _now_ dereferencing
2051 * the "owner" is safe!
2052 */
2053
2054 err = try_module_get(csi2->driver->owner);
2055
2056 /* Let notifier complete, if it has been locked */
2057 complete(&wait.completion);
2058 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2059 if (!err) {
2060 err = -ENODEV;
2061 goto exit_free_clk;
2062 }
2063 }
2064
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002065 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2066 if (IS_ERR(pcdev->alloc_ctx)) {
2067 err = PTR_ERR(pcdev->alloc_ctx);
2068 goto exit_module_put;
2069 }
2070
Magnus Damm0d3244d2008-07-16 22:59:28 -03002071 err = soc_camera_host_register(&pcdev->ici);
2072 if (err)
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002073 goto exit_free_ctx;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002074
2075 return 0;
2076
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002077exit_free_ctx:
2078 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002079exit_module_put:
2080 if (csi2 && csi2->driver)
2081 module_put(csi2->driver->owner);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002082exit_free_clk:
2083 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002084 free_irq(pcdev->irq, pcdev);
2085exit_release_mem:
2086 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2087 dma_release_declared_memory(&pdev->dev);
2088exit_iounmap:
2089 iounmap(base);
2090exit_kfree:
2091 kfree(pcdev);
2092exit:
2093 return err;
2094}
2095
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002096static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002097{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002098 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2099 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2100 struct sh_mobile_ceu_dev, ici);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002101 struct device *csi2 = pcdev->pdata->csi2_dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002102
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002103 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002104 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002105 free_irq(pcdev->irq, pcdev);
2106 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2107 dma_release_declared_memory(&pdev->dev);
2108 iounmap(pcdev->base);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002109 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002110 if (csi2 && csi2->driver)
2111 module_put(csi2->driver->owner);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002112 kfree(pcdev);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002113
Magnus Damm0d3244d2008-07-16 22:59:28 -03002114 return 0;
2115}
2116
Magnus Damm6d1386c2009-08-14 10:49:17 +00002117static int sh_mobile_ceu_runtime_nop(struct device *dev)
2118{
2119 /* Runtime PM callback shared between ->runtime_suspend()
2120 * and ->runtime_resume(). Simply returns success.
2121 *
2122 * This driver re-initializes all registers after
2123 * pm_runtime_get_sync() anyway so there is no need
2124 * to save and restore registers here.
2125 */
2126 return 0;
2127}
2128
Alexey Dobriyan47145212009-12-14 18:00:08 -08002129static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00002130 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2131 .runtime_resume = sh_mobile_ceu_runtime_nop,
2132};
2133
Magnus Damm0d3244d2008-07-16 22:59:28 -03002134static struct platform_driver sh_mobile_ceu_driver = {
2135 .driver = {
2136 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00002137 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002138 },
2139 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03002140 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002141};
2142
2143static int __init sh_mobile_ceu_init(void)
2144{
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002145 /* Whatever return code */
2146 request_module("sh_mobile_csi2");
Magnus Damm0d3244d2008-07-16 22:59:28 -03002147 return platform_driver_register(&sh_mobile_ceu_driver);
2148}
2149
2150static void __exit sh_mobile_ceu_exit(void)
2151{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03002152 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002153}
2154
2155module_init(sh_mobile_ceu_init);
2156module_exit(sh_mobile_ceu_exit);
2157
2158MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2159MODULE_AUTHOR("Magnus Damm");
2160MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03002161MODULE_ALIAS("platform:sh_mobile_ceu");