blob: 424dfacd263a461866066a0e47ae65710649f315 [file] [log] [blame]
Magnus Damm0d3244d2008-07-16 22:59:28 -03001/*
2 * V4L2 Driver for SuperH Mobile CEU interface
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
7 *
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/io.h>
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -030020#include <linux/completion.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030021#include <linux/delay.h>
22#include <linux/dma-mapping.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/interrupt.h>
26#include <linux/kernel.h>
27#include <linux/mm.h>
28#include <linux/moduleparam.h>
29#include <linux/time.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030031#include <linux/device.h>
32#include <linux/platform_device.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030033#include <linux/videodev2.h>
Magnus Damm6d1386c2009-08-14 10:49:17 +000034#include <linux/pm_runtime.h>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030035#include <linux/sched.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030036
37#include <media/v4l2-common.h>
38#include <media/v4l2-dev.h>
39#include <media/soc_camera.h>
40#include <media/sh_mobile_ceu.h>
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -030041#include <media/sh_mobile_csi2.h>
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -030042#include <media/videobuf2-dma-contig.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030043#include <media/v4l2-mediabus.h>
44#include <media/soc_mediabus.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030045
46/* register offsets for sh7722 / sh7723 */
47
Magnus Dammdd542032008-10-16 19:50:22 -030048#define CAPSR 0x00 /* Capture start register */
49#define CAPCR 0x04 /* Capture control register */
50#define CAMCR 0x08 /* Capture interface control register */
51#define CMCYR 0x0c /* Capture interface cycle register */
52#define CAMOR 0x10 /* Capture interface offset register */
53#define CAPWR 0x14 /* Capture interface width register */
54#define CAIFR 0x18 /* Capture interface input format register */
55#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
56#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
57#define CRCNTR 0x28 /* CEU register control register */
58#define CRCMPR 0x2c /* CEU register forcible control register */
59#define CFLCR 0x30 /* Capture filter control register */
60#define CFSZR 0x34 /* Capture filter size clip register */
61#define CDWDR 0x38 /* Capture destination width register */
62#define CDAYR 0x3c /* Capture data address Y register */
63#define CDACR 0x40 /* Capture data address C register */
64#define CDBYR 0x44 /* Capture data bottom-field address Y register */
65#define CDBCR 0x48 /* Capture data bottom-field address C register */
66#define CBDSR 0x4c /* Capture bundle destination size register */
67#define CFWCR 0x5c /* Firewall operation control register */
68#define CLFCR 0x60 /* Capture low-pass filter control register */
69#define CDOCR 0x64 /* Capture data output control register */
70#define CDDCR 0x68 /* Capture data complexity level register */
71#define CDDAR 0x6c /* Capture data complexity level address register */
72#define CEIER 0x70 /* Capture event interrupt enable register */
73#define CETCR 0x74 /* Capture event flag clear register */
74#define CSTSR 0x7c /* Capture status register */
75#define CSRTR 0x80 /* Capture software reset register */
76#define CDSSR 0x84 /* Capture data size register */
77#define CDAYR2 0x90 /* Capture data address Y register 2 */
78#define CDACR2 0x94 /* Capture data address C register 2 */
79#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
80#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030081
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030082#undef DEBUG_GEOMETRY
83#ifdef DEBUG_GEOMETRY
84#define dev_geo dev_info
85#else
86#define dev_geo dev_dbg
87#endif
88
Magnus Damm0d3244d2008-07-16 22:59:28 -030089/* per video frame buffer */
90struct sh_mobile_ceu_buffer {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -030091 struct vb2_buffer vb; /* v4l buffer must be first */
92 struct list_head queue;
Magnus Damm0d3244d2008-07-16 22:59:28 -030093};
94
95struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030096 struct soc_camera_host ici;
97 struct soc_camera_device *icd;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -030098 struct platform_device *csi2_pdev;
Magnus Damm0d3244d2008-07-16 22:59:28 -030099
100 unsigned int irq;
101 void __iomem *base;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300102 size_t video_limit;
103 size_t buf_total;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300104
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300105 spinlock_t lock; /* Protects video buffer lists */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300106 struct list_head capture;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300107 struct vb2_buffer *active;
108 struct vb2_alloc_ctx *alloc_ctx;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300109
110 struct sh_mobile_ceu_info *pdata;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300111 struct completion complete;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300112
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300113 u32 cflcr;
114
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -0300115 /* static max sizes either from platform data or default */
116 int max_width;
117 int max_height;
118
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300119 enum v4l2_field field;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300120 int sequence;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300121
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300122 unsigned int image_mode:1;
123 unsigned int is_16bit:1;
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300124 unsigned int frozen:1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300125};
126
127struct sh_mobile_ceu_cam {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300128 /* CEU offsets within the camera output, before the CEU scaler */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300129 unsigned int ceu_left;
130 unsigned int ceu_top;
131 /* Client output, as seen by the CEU */
132 unsigned int width;
133 unsigned int height;
134 /*
135 * User window from S_CROP / G_CROP, produced by client cropping and
136 * scaling, CEU scaling and CEU cropping, mapped back onto the client
137 * input window
138 */
139 struct v4l2_rect subrect;
140 /* Camera cropping rectangle */
141 struct v4l2_rect rect;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300142 const struct soc_mbus_pixelfmt *extra_fmt;
143 enum v4l2_mbus_pixelcode code;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300144};
145
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300146static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
147{
148 return container_of(vb, struct sh_mobile_ceu_buffer, vb);
149}
150
Magnus Damm0d3244d2008-07-16 22:59:28 -0300151static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300152 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300153{
154 iowrite32(data, priv->base + reg_offs);
155}
156
Magnus Damm7a1a8162008-12-18 12:50:30 -0300157static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300158{
159 return ioread32(priv->base + reg_offs);
160}
161
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300162static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
163{
164 int i, success = 0;
165 struct soc_camera_device *icd = pcdev->icd;
166
167 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
168
169 /* wait CSTSR.CPTON bit */
170 for (i = 0; i < 1000; i++) {
171 if (!(ceu_read(pcdev, CSTSR) & 1)) {
172 success++;
173 break;
174 }
175 udelay(1);
176 }
177
178 /* wait CAPSR.CPKIL bit */
179 for (i = 0; i < 1000; i++) {
180 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
181 success++;
182 break;
183 }
184 udelay(1);
185 }
186
187
188 if (2 != success) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300189 dev_warn(icd->pdev, "soft reset time out\n");
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300190 return -EIO;
191 }
192
193 return 0;
194}
195
Magnus Damm0d3244d2008-07-16 22:59:28 -0300196/*
197 * Videobuf operations
198 */
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300199
200/*
201 * .queue_setup() is called to check, whether the driver can accept the
202 * requested number of buffers and to fill in plane sizes
203 * for the current frame format if required
204 */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300205static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300206 const struct v4l2_format *fmt,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300207 unsigned int *count, unsigned int *num_planes,
Marek Szyprowski035aa142011-08-24 06:43:36 -0300208 unsigned int sizes[], void *alloc_ctxs[])
Magnus Damm0d3244d2008-07-16 22:59:28 -0300209{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300210 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300211 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300212 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300213 int bytes_per_line;
214 unsigned int height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300215
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300216 if (fmt) {
217 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
218 fmt->fmt.pix.pixelformat);
219 if (!xlate)
220 return -EINVAL;
221 bytes_per_line = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
222 xlate->host_fmt);
223 height = fmt->fmt.pix.height;
224 } else {
225 /* Called from VIDIOC_REQBUFS or in compatibility mode */
226 bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
227 icd->current_fmt->host_fmt);
228 height = icd->user_height;
229 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300230 if (bytes_per_line < 0)
231 return bytes_per_line;
232
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300233 sizes[0] = bytes_per_line * height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300234
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300235 alloc_ctxs[0] = pcdev->alloc_ctx;
236
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300237 if (!vq->num_buffers)
238 pcdev->sequence = 0;
239
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300240 if (!*count)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300241 *count = 2;
242
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300243 /* If *num_planes != 0, we have already verified *count. */
244 if (pcdev->video_limit && !*num_planes) {
245 size_t size = PAGE_ALIGN(sizes[0]) * *count;
246
247 if (size + pcdev->buf_total > pcdev->video_limit)
248 *count = (pcdev->video_limit - pcdev->buf_total) /
249 PAGE_ALIGN(sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300250 }
251
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300252 *num_planes = 1;
253
Marek Szyprowski035aa142011-08-24 06:43:36 -0300254 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300255
256 return 0;
257}
258
Magnus Damm7a1a8162008-12-18 12:50:30 -0300259#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
260#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
261#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300262#define CEU_CEIER_VBP (1 << 20) /* vbp error */
Magnus Damm7a1a8162008-12-18 12:50:30 -0300263#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300264#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
Magnus Damm7a1a8162008-12-18 12:50:30 -0300265
266
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300267/*
268 * return value doesn't reflex the success/failure to queue the new buffer,
269 * but rather the status of the previous buffer.
270 */
271static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300272{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300273 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300274 dma_addr_t phys_addr_top, phys_addr_bottom;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300275 unsigned long top1, top2;
276 unsigned long bottom1, bottom2;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300277 u32 status;
Guennadi Liakhovetski15f517e2011-07-26 11:03:37 -0300278 bool planar;
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300279 int ret = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300280
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300281 /*
282 * The hardware is _very_ picky about this sequence. Especially
Magnus Damm7a1a8162008-12-18 12:50:30 -0300283 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
284 * several not-so-well documented interrupt sources in CETCR.
285 */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300286 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
287 status = ceu_read(pcdev, CETCR);
288 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300289 if (!pcdev->frozen)
290 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
Magnus Damm7a1a8162008-12-18 12:50:30 -0300291 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
292 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300293
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300294 /*
295 * When a VBP interrupt occurs, a capture end interrupt does not occur
296 * and the image of that frame is not captured correctly. So, soft reset
297 * is needed here.
298 */
299 if (status & CEU_CEIER_VBP) {
300 sh_mobile_ceu_soft_reset(pcdev);
301 ret = -EIO;
302 }
303
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300304 if (pcdev->frozen) {
305 complete(&pcdev->complete);
306 return ret;
307 }
308
Magnus Damm8be65dc2008-12-18 12:38:06 -0300309 if (!pcdev->active)
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300310 return ret;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300311
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300312 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
313 top1 = CDBYR;
314 top2 = CDBCR;
315 bottom1 = CDAYR;
316 bottom2 = CDACR;
317 } else {
318 top1 = CDAYR;
319 top2 = CDACR;
320 bottom1 = CDBYR;
321 bottom2 = CDBCR;
322 }
323
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300324 phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300325
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300326 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300327 case V4L2_PIX_FMT_NV12:
328 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300329 case V4L2_PIX_FMT_NV16:
330 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski15f517e2011-07-26 11:03:37 -0300331 planar = true;
332 break;
333 default:
334 planar = false;
335 }
336
337 ceu_write(pcdev, top1, phys_addr_top);
338 if (V4L2_FIELD_NONE != pcdev->field) {
339 if (planar)
340 phys_addr_bottom = phys_addr_top + icd->user_width;
341 else
342 phys_addr_bottom = phys_addr_top +
343 soc_mbus_bytes_per_line(icd->user_width,
344 icd->current_fmt->host_fmt);
345 ceu_write(pcdev, bottom1, phys_addr_bottom);
346 }
347
348 if (planar) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300349 phys_addr_top += icd->user_width *
350 icd->user_height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300351 ceu_write(pcdev, top2, phys_addr_top);
352 if (V4L2_FIELD_NONE != pcdev->field) {
353 phys_addr_bottom = phys_addr_top + icd->user_width;
354 ceu_write(pcdev, bottom2, phys_addr_bottom);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300355 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300356 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300357
Magnus Damm8be65dc2008-12-18 12:38:06 -0300358 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300359
360 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300361}
362
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300363static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300364{
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300365 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300366
367 /* Added list head initialization on alloc */
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300368 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300369
Magnus Damm0d3244d2008-07-16 22:59:28 -0300370 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300371}
372
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300373static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300374{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300375 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300376 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300377 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300378 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300379 unsigned long size;
380 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
381 icd->current_fmt->host_fmt);
382
383 if (bytes_per_line < 0)
384 goto error;
385
386 size = icd->user_height * bytes_per_line;
387
388 if (vb2_plane_size(vb, 0) < size) {
389 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
390 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
391 goto error;
392 }
393
394 vb2_set_plane_payload(vb, 0, size);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300395
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300396 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300397 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300398
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300399#ifdef DEBUG
400 /*
401 * This can be useful if you want to see if we actually fill
402 * the buffer with something
403 */
404 if (vb2_plane_vaddr(vb, 0))
405 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
406#endif
407
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300408 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300409 list_add_tail(&buf->queue, &pcdev->capture);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300410
411 if (!pcdev->active) {
Kuninori Morimoto5cf93f12009-10-05 12:48:59 -0300412 /*
413 * Because there were no active buffer at this moment,
414 * we are not interested in the return value of
415 * sh_mobile_ceu_capture here.
416 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300417 pcdev->active = vb;
418 sh_mobile_ceu_capture(pcdev);
419 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300420 spin_unlock_irq(&pcdev->lock);
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300421
422 return;
423
424error:
425 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300426}
427
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300428static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300429{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300430 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300431 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300432 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300433 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300434
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300435 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300436
437 if (pcdev->active == vb) {
438 /* disable capture (release DMA buffer), reset */
439 ceu_write(pcdev, CAPSR, 1 << 16);
440 pcdev->active = NULL;
441 }
442
Guennadi Liakhovetskicb74cf52011-07-04 12:15:14 -0300443 /*
444 * Doesn't hurt also if the list is empty, but it hurts, if queuing the
445 * buffer failed, and .buf_init() hasn't been called
446 */
447 if (buf->queue.next)
448 list_del_init(&buf->queue);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300449
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300450 pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
451 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
452 pcdev->buf_total);
453
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300454 spin_unlock_irq(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300455}
456
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300457static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
458{
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300459 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
460 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
461 struct sh_mobile_ceu_dev *pcdev = ici->priv;
462
463 pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
464 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
465 pcdev->buf_total);
466
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300467 /* This is for locking debugging only */
468 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
469 return 0;
470}
471
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300472static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
473{
474 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300475 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300476 struct sh_mobile_ceu_dev *pcdev = ici->priv;
477 struct list_head *buf_head, *tmp;
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300478
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300479 spin_lock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300480
481 pcdev->active = NULL;
482
483 list_for_each_safe(buf_head, tmp, &pcdev->capture)
484 list_del_init(buf_head);
485
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300486 spin_unlock_irq(&pcdev->lock);
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300487
488 return sh_mobile_ceu_soft_reset(pcdev);
489}
490
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300491static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
492 .queue_setup = sh_mobile_ceu_videobuf_setup,
493 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
494 .buf_queue = sh_mobile_ceu_videobuf_queue,
495 .buf_cleanup = sh_mobile_ceu_videobuf_release,
496 .buf_init = sh_mobile_ceu_videobuf_init,
497 .wait_prepare = soc_camera_unlock,
498 .wait_finish = soc_camera_lock,
Guennadi Liakhovetski066f7862011-03-24 12:51:07 -0300499 .stop_streaming = sh_mobile_ceu_stop_streaming,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300500};
501
502static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
503{
504 struct sh_mobile_ceu_dev *pcdev = data;
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300505 struct vb2_buffer *vb;
506 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300507
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300508 spin_lock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300509
510 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300511 if (!vb)
512 /* Stale interrupt from a released buffer */
513 goto out;
514
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300515 list_del_init(&to_ceu_vb(vb)->queue);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300516
517 if (!list_empty(&pcdev->capture))
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300518 pcdev->active = &list_entry(pcdev->capture.next,
519 struct sh_mobile_ceu_buffer, queue)->vb;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300520 else
521 pcdev->active = NULL;
522
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300523 ret = sh_mobile_ceu_capture(pcdev);
524 do_gettimeofday(&vb->v4l2_buf.timestamp);
525 if (!ret) {
526 vb->v4l2_buf.field = pcdev->field;
527 vb->v4l2_buf.sequence = pcdev->sequence++;
528 }
529 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300530
531out:
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300532 spin_unlock(&pcdev->lock);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300533
534 return IRQ_HANDLED;
535}
536
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300537static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev)
538{
539 struct v4l2_subdev *sd;
540
541 if (!pcdev->csi2_pdev)
542 return NULL;
543
544 v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev)
545 if (&pcdev->csi2_pdev->dev == v4l2_get_subdevdata(sd))
546 return sd;
547
548 return NULL;
549}
550
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300551/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300552static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
553{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300554 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300555 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300556 struct v4l2_subdev *csi2_sd;
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300557 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300558
Magnus Damm0d3244d2008-07-16 22:59:28 -0300559 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300560 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300561
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300562 dev_info(icd->parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300563 "SuperH Mobile CEU driver attached to camera %d\n",
564 icd->devnum);
565
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900566 pm_runtime_get_sync(ici->v4l2_dev.dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300567
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -0300568 pcdev->buf_total = 0;
569
Guennadi Liakhovetski6ed7c032009-12-11 11:15:06 -0300570 ret = sh_mobile_ceu_soft_reset(pcdev);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300571
572 csi2_sd = find_csi2(pcdev);
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -0300573 if (csi2_sd) {
574 csi2_sd->grp_id = soc_camera_grp_id(icd);
575 v4l2_set_subdev_hostdata(csi2_sd, icd);
576 }
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300577
578 ret = v4l2_subdev_call(csi2_sd, core, s_power, 1);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300579 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300580 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300581 return ret;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300582 }
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300583
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300584 /*
585 * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
586 * has not found this soc-camera device among its clients
587 */
588 if (ret == -ENODEV && csi2_sd)
589 csi2_sd->grp_id = 0;
590 pcdev->icd = icd;
591
592 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300593}
594
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300595/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300596static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
597{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300598 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300599 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300600 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300601
602 BUG_ON(icd != pcdev->icd);
603
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300604 v4l2_subdev_call(csi2_sd, core, s_power, 0);
Guennadi Liakhovetski489759c2011-09-07 11:59:47 -0300605 if (csi2_sd)
606 csi2_sd->grp_id = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300607 /* disable capture, disable interrupts */
608 ceu_write(pcdev, CEIER, 0);
Kuninori Morimotob1de7ae2009-10-05 12:48:20 -0300609 sh_mobile_ceu_soft_reset(pcdev);
Magnus Damm51354cc2008-10-16 19:51:20 -0300610
611 /* make sure active buffer is canceled */
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300612 spin_lock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300613 if (pcdev->active) {
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300614 list_del_init(&to_ceu_vb(pcdev->active)->queue);
615 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
Magnus Damm51354cc2008-10-16 19:51:20 -0300616 pcdev->active = NULL;
617 }
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -0300618 spin_unlock_irq(&pcdev->lock);
Magnus Damm51354cc2008-10-16 19:51:20 -0300619
Guennadi Liakhovetskia79aebf2009-09-25 13:36:51 +0900620 pm_runtime_put_sync(ici->v4l2_dev.dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300621
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300622 dev_info(icd->parent,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300623 "SuperH Mobile CEU driver detached from camera %d\n",
624 icd->devnum);
625
626 pcdev->icd = NULL;
627}
628
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300629/*
630 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
631 * in SH7722 Hardware Manual
632 */
633static unsigned int size_dst(unsigned int src, unsigned int scale)
634{
635 unsigned int mant_pre = scale >> 12;
636 if (!src || !scale)
637 return src;
638 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
639 mant_pre * 4096 / scale + 1;
640}
641
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300642static u16 calc_scale(unsigned int src, unsigned int *dst)
643{
644 u16 scale;
645
646 if (src == *dst)
647 return 0;
648
649 scale = (src * 4096 / *dst) & ~7;
650
651 while (scale > 4096 && size_dst(src, scale) < *dst)
652 scale -= 8;
653
654 *dst = size_dst(src, scale);
655
656 return scale;
657}
658
659/* rect is guaranteed to not exceed the scaled camera rectangle */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300660static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300661{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300662 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300663 struct sh_mobile_ceu_cam *cam = icd->host_priv;
664 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300665 unsigned int height, width, cdwdr_width, in_width, in_height;
666 unsigned int left_offset, top_offset;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300667 u32 camor;
668
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300669 dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300670 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300671
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300672 left_offset = cam->ceu_left;
673 top_offset = cam->ceu_top;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300674
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300675 WARN_ON(icd->user_width & 3 || icd->user_height & 3);
676
677 width = icd->user_width;
678
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300679 if (pcdev->image_mode) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300680 in_width = cam->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300681 if (!pcdev->is_16bit) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300682 in_width *= 2;
683 left_offset *= 2;
684 }
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300685 cdwdr_width = width;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300686 } else {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300687 int bytes_per_line = soc_mbus_bytes_per_line(width,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300688 icd->current_fmt->host_fmt);
689 unsigned int w_factor;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300690
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300691 switch (icd->current_fmt->host_fmt->packing) {
692 case SOC_MBUS_PACKING_2X8_PADHI:
693 w_factor = 2;
694 break;
695 default:
696 w_factor = 1;
697 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300698
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300699 in_width = cam->width * w_factor;
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300700 left_offset *= w_factor;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300701
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300702 if (bytes_per_line < 0)
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300703 cdwdr_width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300704 else
705 cdwdr_width = bytes_per_line;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300706 }
707
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300708 height = icd->user_height;
709 in_height = cam->height;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300710 if (V4L2_FIELD_NONE != pcdev->field) {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300711 height = (height / 2) & ~3;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300712 in_height /= 2;
713 top_offset /= 2;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300714 cdwdr_width *= 2;
715 }
716
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300717 /* CSI2 special configuration */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300718 if (pcdev->pdata->csi2) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300719 in_width = ((in_width - 2) * 2);
720 left_offset *= 2;
721 }
722
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300723 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300724 camor = left_offset | (top_offset << 16);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300725
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300726 dev_geo(icd->parent,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300727 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
728 (in_height << 16) | in_width, (height << 16) | width,
729 cdwdr_width);
730
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300731 ceu_write(pcdev, CAMOR, camor);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300732 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -0300733 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300734 ceu_write(pcdev, CFSZR, (height << 16) | width);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300735 ceu_write(pcdev, CDWDR, cdwdr_width);
736}
737
738static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
739{
740 u32 capsr = ceu_read(pcdev, CAPSR);
741 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
742 return capsr;
743}
744
745static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
746{
747 unsigned long timeout = jiffies + 10 * HZ;
748
749 /*
750 * Wait until the end of the current frame. It can take a long time,
751 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
752 */
753 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
754 msleep(1);
755
756 if (time_after(jiffies, timeout)) {
757 dev_err(pcdev->ici.v4l2_dev.dev,
758 "Timeout waiting for frame end! Interface problem?\n");
759 return;
760 }
761
762 /* Wait until reset clears, this shall not hang... */
763 while (ceu_read(pcdev, CAPSR) & (1 << 16))
764 udelay(10);
765
766 /* Anything to restore? */
767 if (capsr & ~(1 << 16))
768 ceu_write(pcdev, CAPSR, capsr);
769}
770
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300771/* Find the bus subdevice driver, e.g., CSI2 */
772static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev,
773 struct soc_camera_device *icd)
774{
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300775 if (pcdev->csi2_pdev) {
776 struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -0300777 if (csi2_sd && csi2_sd->grp_id == soc_camera_grp_id(icd))
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300778 return csi2_sd;
779 }
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300780
Guennadi Liakhovetski6ac939c2011-08-22 12:35:09 -0300781 return soc_camera_to_subdev(icd);
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300782}
783
784#define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
785 V4L2_MBUS_PCLK_SAMPLE_RISING | \
786 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
787 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
788 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
789 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
790 V4L2_MBUS_DATA_ACTIVE_HIGH)
791
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -0300792/* Capture is not running, no interrupts, no locking needed */
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -0300793static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300794{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300795 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300796 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300797 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300798 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300799 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
800 unsigned long value, common_flags = CEU_BUS_FLAGS;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300801 u32 capsr = capture_save_reset(pcdev);
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300802 unsigned int yuv_lineskip;
803 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300804
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300805 /*
806 * If the client doesn't implement g_mbus_config, we just use our
807 * platform data
808 */
809 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
810 if (!ret) {
811 common_flags = soc_mbus_config_compatible(&cfg,
812 common_flags);
813 if (!common_flags)
814 return -EINVAL;
815 } else if (ret != -ENOIOCTLCMD) {
816 return ret;
817 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300818
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300819 /* Make choises, based on platform preferences */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300820 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
821 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300822 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300823 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300824 else
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300825 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300826 }
827
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300828 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
829 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300830 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300831 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300832 else
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300833 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
Kuninori Morimoto85dc1cf2009-12-11 11:53:54 -0300834 }
835
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300836 cfg.flags = common_flags;
837 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
838 if (ret < 0 && ret != -ENOIOCTLCMD)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300839 return ret;
840
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300841 if (icd->current_fmt->host_fmt->bits_per_sample > 8)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300842 pcdev->is_16bit = 1;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300843 else
844 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300845
846 ceu_write(pcdev, CRCNTR, 0);
847 ceu_write(pcdev, CRCMPR, 0);
848
Magnus Damm8be65dc2008-12-18 12:38:06 -0300849 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300850 yuv_lineskip = 0x10;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300851
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300852 switch (icd->current_fmt->host_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300853 case V4L2_PIX_FMT_NV12:
854 case V4L2_PIX_FMT_NV21:
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300855 /* convert 4:2:2 -> 4:2:0 */
856 yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300857 /* fall-through */
858 case V4L2_PIX_FMT_NV16:
859 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300860 switch (cam->code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300861 case V4L2_MBUS_FMT_UYVY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300862 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
863 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300864 case V4L2_MBUS_FMT_VYUY8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300865 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
866 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300867 case V4L2_MBUS_FMT_YUYV8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300868 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
869 break;
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300870 case V4L2_MBUS_FMT_YVYU8_2X8:
Magnus Damm8be65dc2008-12-18 12:38:06 -0300871 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
872 break;
873 default:
874 BUG();
875 }
876 }
877
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300878 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
879 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300880 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300881
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300882 value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
883 value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300884 value |= pcdev->is_16bit ? 1 << 12 : 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300885
886 /* CSI2 mode */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -0300887 if (pcdev->pdata->csi2)
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -0300888 value |= 3 << 12;
889
Magnus Damm0d3244d2008-07-16 22:59:28 -0300890 ceu_write(pcdev, CAMCR, value);
891
892 ceu_write(pcdev, CAPCR, 0x00300000);
Kuninori Morimoto1edcc102009-12-11 11:53:53 -0300893
894 switch (pcdev->field) {
895 case V4L2_FIELD_INTERLACED_TB:
896 value = 0x101;
897 break;
898 case V4L2_FIELD_INTERLACED_BT:
899 value = 0x102;
900 break;
901 default:
902 value = 0;
903 break;
904 }
905 ceu_write(pcdev, CAIFR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300906
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -0300907 sh_mobile_ceu_set_rect(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300908 mdelay(1);
909
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300910 dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -0300911 ceu_write(pcdev, CFLCR, pcdev->cflcr);
Magnus Dammdd542032008-10-16 19:50:22 -0300912
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300913 /*
914 * A few words about byte order (observed in Big Endian mode)
Magnus Dammdd542032008-10-16 19:50:22 -0300915 *
916 * In data fetch mode bytes are received in chunks of 8 bytes.
917 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
918 *
919 * The data is however by default written to memory in reverse order:
920 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
921 *
922 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300923 * using 7 we swap the data bytes to match the incoming order:
924 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300925 */
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300926 value = 0x00000007 | yuv_lineskip;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300927
928 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300929 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
930
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300931 capture_restore(pcdev, capsr);
932
Magnus Damm0d3244d2008-07-16 22:59:28 -0300933 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300934 return 0;
935}
936
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300937static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
938 unsigned char buswidth)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300939{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300940 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300941 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300942 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
943 unsigned long common_flags = CEU_BUS_FLAGS;
944 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
945 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300946
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300947 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
948 if (!ret)
949 common_flags = soc_mbus_config_compatible(&cfg,
950 common_flags);
951 else if (ret != -ENOIOCTLCMD)
952 return ret;
953
954 if (!common_flags || buswidth > 16)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300955 return -EINVAL;
956
957 return 0;
958}
959
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300960static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300961 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300962 .fourcc = V4L2_PIX_FMT_NV12,
963 .name = "NV12",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300964 .bits_per_sample = 8,
965 .packing = SOC_MBUS_PACKING_1_5X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300966 .order = SOC_MBUS_ORDER_LE,
967 }, {
968 .fourcc = V4L2_PIX_FMT_NV21,
969 .name = "NV21",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300970 .bits_per_sample = 8,
971 .packing = SOC_MBUS_PACKING_1_5X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300972 .order = SOC_MBUS_ORDER_LE,
973 }, {
974 .fourcc = V4L2_PIX_FMT_NV16,
975 .name = "NV16",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300976 .bits_per_sample = 8,
977 .packing = SOC_MBUS_PACKING_2X8_PADHI,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300978 .order = SOC_MBUS_ORDER_LE,
979 }, {
980 .fourcc = V4L2_PIX_FMT_NV61,
981 .name = "NV61",
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300982 .bits_per_sample = 8,
983 .packing = SOC_MBUS_PACKING_2X8_PADHI,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300984 .order = SOC_MBUS_ORDER_LE,
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300985 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300986};
987
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300988/* This will be corrected as we get more formats */
989static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
990{
991 return fmt->packing == SOC_MBUS_PACKING_NONE ||
992 (fmt->bits_per_sample == 8 &&
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -0300993 fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
994 (fmt->bits_per_sample == 8 &&
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300995 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
996 (fmt->bits_per_sample > 8 &&
997 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
998}
999
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001000static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
1001
Hans Verkuild34bfcd22011-09-05 17:07:47 -03001002static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
1003{
1004 return container_of(ctrl->handler, struct soc_camera_device,
1005 ctrl_handler);
1006}
1007
1008static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
1009{
1010 struct soc_camera_device *icd = ctrl_to_icd(ctrl);
1011 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1012 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1013
1014 switch (ctrl->id) {
1015 case V4L2_CID_SHARPNESS:
1016 switch (icd->current_fmt->host_fmt->fourcc) {
1017 case V4L2_PIX_FMT_NV12:
1018 case V4L2_PIX_FMT_NV21:
1019 case V4L2_PIX_FMT_NV16:
1020 case V4L2_PIX_FMT_NV61:
1021 ceu_write(pcdev, CLFCR, !ctrl->val);
1022 return 0;
1023 }
1024 break;
1025 }
1026
1027 return -EINVAL;
1028}
1029
1030static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
1031 .s_ctrl = sh_mobile_ceu_s_ctrl,
1032};
1033
Hans Verkuil3805f202010-05-08 17:55:00 -03001034static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
Magnus Damm9414de32008-12-18 11:49:06 -03001035 struct soc_camera_format_xlate *xlate)
1036{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001037 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001038 struct device *dev = icd->parent;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001039 struct soc_camera_host *ici = to_soc_camera_host(dev);
1040 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001041 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -03001042 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001043 struct sh_mobile_ceu_cam *cam;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001044 enum v4l2_mbus_pixelcode code;
1045 const struct soc_mbus_pixelfmt *fmt;
Magnus Damm9414de32008-12-18 11:49:06 -03001046
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001047 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
1048 if (ret < 0)
1049 /* No more formats */
1050 return 0;
1051
1052 fmt = soc_mbus_get_fmtdesc(code);
1053 if (!fmt) {
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -03001054 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
1055 return 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001056 }
1057
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03001058 if (!pcdev->pdata->csi2) {
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -03001059 /* Are there any restrictions in the CSI-2 case? */
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001060 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
1061 if (ret < 0)
1062 return 0;
1063 }
Magnus Damm9414de32008-12-18 11:49:06 -03001064
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001065 if (!icd->host_priv) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001066 struct v4l2_mbus_framefmt mf;
1067 struct v4l2_rect rect;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001068 int shift = 0;
1069
Hans Verkuild34bfcd22011-09-05 17:07:47 -03001070 /* Add our control */
1071 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
1072 V4L2_CID_SHARPNESS, 0, 1, 1, 0);
1073 if (icd->ctrl_handler.error)
1074 return icd->ctrl_handler.error;
1075
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001076 /* FIXME: subwindow is lost between close / open */
1077
1078 /* Cache current client geometry */
1079 ret = client_g_rect(sd, &rect);
1080 if (ret < 0)
1081 return ret;
1082
1083 /* First time */
1084 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1085 if (ret < 0)
1086 return ret;
1087
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001088 /*
1089 * All currently existing CEU implementations support 2560x1920
1090 * or larger frames. If the sensor is proposing too big a frame,
1091 * don't bother with possibly supportred by the CEU larger
1092 * sizes, just try VGA multiples. If needed, this can be
1093 * adjusted in the future.
1094 */
1095 while ((mf.width > pcdev->max_width ||
1096 mf.height > pcdev->max_height) && shift < 4) {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001097 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1098 mf.width = 2560 >> shift;
1099 mf.height = 1920 >> shift;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001100 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1101 soc_camera_grp_id(icd), video,
1102 s_mbus_fmt, &mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001103 if (ret < 0)
1104 return ret;
1105 shift++;
1106 }
1107
1108 if (shift == 4) {
1109 dev_err(dev, "Failed to configure the client below %ux%x\n",
1110 mf.width, mf.height);
1111 return -EIO;
1112 }
1113
1114 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
1115
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001116 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1117 if (!cam)
1118 return -ENOMEM;
1119
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001120 /* We are called with current camera crop, initialise subrect with it */
1121 cam->rect = rect;
1122 cam->subrect = rect;
1123
1124 cam->width = mf.width;
1125 cam->height = mf.height;
1126
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001127 icd->host_priv = cam;
1128 } else {
1129 cam = icd->host_priv;
1130 }
1131
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001132 /* Beginning of a pass */
1133 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001134 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001135
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001136 switch (code) {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -03001137 case V4L2_MBUS_FMT_UYVY8_2X8:
1138 case V4L2_MBUS_FMT_VYUY8_2X8:
1139 case V4L2_MBUS_FMT_YUYV8_2X8:
1140 case V4L2_MBUS_FMT_YVYU8_2X8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001141 if (cam->extra_fmt)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001142 break;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001143
1144 /*
1145 * Our case is simple so far: for any of the above four camera
1146 * formats we add all our four synthesized NV* formats, so,
1147 * just marking the device with a single flag suffices. If
1148 * the format generation rules are more complex, you would have
1149 * to actually hang your already added / counted formats onto
1150 * the host_priv pointer and check whether the format you're
1151 * going to add now is already there.
1152 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001153 cam->extra_fmt = sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -03001154
Magnus Damm8be65dc2008-12-18 12:38:06 -03001155 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1156 formats += n;
1157 for (k = 0; xlate && k < n; k++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001158 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1159 xlate->code = code;
Magnus Damm8be65dc2008-12-18 12:38:06 -03001160 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001161 dev_dbg(dev, "Providing format %s using code %d\n",
1162 sh_mobile_ceu_formats[k].name, code);
Magnus Damm8be65dc2008-12-18 12:38:06 -03001163 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001164 break;
Magnus Damm9414de32008-12-18 11:49:06 -03001165 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001166 if (!sh_mobile_ceu_packing_supported(fmt))
1167 return 0;
1168 }
1169
1170 /* Generic pass-through */
1171 formats++;
1172 if (xlate) {
1173 xlate->host_fmt = fmt;
1174 xlate->code = code;
1175 xlate++;
1176 dev_dbg(dev, "Providing format %s in pass-through mode\n",
Guennadi Liakhovetskie53a4802010-06-18 04:20:56 -03001177 fmt->name);
Magnus Damm9414de32008-12-18 11:49:06 -03001178 }
1179
1180 return formats;
1181}
1182
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001183static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1184{
1185 kfree(icd->host_priv);
1186 icd->host_priv = NULL;
1187}
1188
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001189/* Check if any dimension of r1 is smaller than respective one of r2 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001190static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1191{
1192 return r1->width < r2->width || r1->height < r2->height;
1193}
1194
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001195/* Check if r1 fails to cover r2 */
1196static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1197{
1198 return r1->left > r2->left || r1->top > r2->top ||
1199 r1->left + r1->width < r2->left + r2->width ||
1200 r1->top + r1->height < r2->top + r2->height;
1201}
1202
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001203static unsigned int scale_down(unsigned int size, unsigned int scale)
1204{
1205 return (size * 4096 + scale / 2) / scale;
1206}
1207
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001208static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1209{
1210 return (input * 4096 + output / 2) / output;
1211}
1212
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001213/* Get and store current client crop */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001214static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1215{
1216 struct v4l2_crop crop;
1217 struct v4l2_cropcap cap;
1218 int ret;
1219
1220 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1221
1222 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1223 if (!ret) {
1224 *rect = crop.c;
1225 return ret;
1226 }
1227
1228 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1229 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1230
1231 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001232 if (!ret)
1233 *rect = cap.defrect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001234
1235 return ret;
1236}
1237
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001238/* Client crop has changed, update our sub-rectangle to remain within the area */
1239static void update_subrect(struct sh_mobile_ceu_cam *cam)
1240{
1241 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1242
1243 if (rect->width < subrect->width)
1244 subrect->width = rect->width;
1245
1246 if (rect->height < subrect->height)
1247 subrect->height = rect->height;
1248
1249 if (rect->left > subrect->left)
1250 subrect->left = rect->left;
1251 else if (rect->left + rect->width >
1252 subrect->left + subrect->width)
1253 subrect->left = rect->left + rect->width -
1254 subrect->width;
1255
1256 if (rect->top > subrect->top)
1257 subrect->top = rect->top;
1258 else if (rect->top + rect->height >
1259 subrect->top + subrect->height)
1260 subrect->top = rect->top + rect->height -
1261 subrect->height;
1262}
1263
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001264/*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001265 * The common for both scaling and cropping iterative approach is:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001266 * 1. try if the client can produce exactly what requested by the user
1267 * 2. if (1) failed, try to double the client image until we get one big enough
1268 * 3. if (2) failed, try to request the maximum image
1269 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001270static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001271 struct v4l2_crop *cam_crop)
1272{
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001273 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001274 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1275 struct device *dev = sd->v4l2_dev->dev;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001276 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001277 struct v4l2_cropcap cap;
1278 int ret;
1279 unsigned int width, height;
1280
1281 v4l2_subdev_call(sd, video, s_crop, crop);
1282 ret = client_g_rect(sd, cam_rect);
1283 if (ret < 0)
1284 return ret;
1285
1286 /*
1287 * Now cam_crop contains the current camera input rectangle, and it must
1288 * be within camera cropcap bounds
1289 */
1290 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1291 /* Even if camera S_CROP failed, but camera rectangle matches */
Márton Némethe26b3142010-02-24 17:13:29 -03001292 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001293 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001294 cam->rect = *cam_rect;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001295 return 0;
1296 }
1297
1298 /* Try to fix cropping, that camera hasn't managed to set */
Márton Némethe26b3142010-02-24 17:13:29 -03001299 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 -03001300 cam_rect->width, cam_rect->height,
1301 cam_rect->left, cam_rect->top,
1302 rect->width, rect->height, rect->left, rect->top);
1303
1304 /* We need sensor maximum rectangle */
1305 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1306 if (ret < 0)
1307 return ret;
1308
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001309 /* Put user requested rectangle within sensor bounds */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001310 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1311 cap.bounds.width);
1312 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1313 cap.bounds.height);
1314
1315 /*
1316 * Popular special case - some cameras can only handle fixed sizes like
1317 * QVGA, VGA,... Take care to avoid infinite loop.
1318 */
1319 width = max(cam_rect->width, 2);
1320 height = max(cam_rect->height, 2);
1321
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001322 /*
1323 * Loop as long as sensor is not covering the requested rectangle and
1324 * is still within its bounds
1325 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001326 while (!ret && (is_smaller(cam_rect, rect) ||
1327 is_inside(cam_rect, rect)) &&
1328 (cap.bounds.width > width || cap.bounds.height > height)) {
1329
1330 width *= 2;
1331 height *= 2;
1332
1333 cam_rect->width = width;
1334 cam_rect->height = height;
1335
1336 /*
1337 * We do not know what capabilities the camera has to set up
1338 * left and top borders. We could try to be smarter in iterating
1339 * them, e.g., if camera current left is to the right of the
1340 * target left, set it to the middle point between the current
1341 * left and minimum left. But that would add too much
1342 * complexity: we would have to iterate each border separately.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001343 * Instead we just drop to the left and top bounds.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001344 */
1345 if (cam_rect->left > rect->left)
1346 cam_rect->left = cap.bounds.left;
1347
1348 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1349 cam_rect->width = rect->left + rect->width -
1350 cam_rect->left;
1351
1352 if (cam_rect->top > rect->top)
1353 cam_rect->top = cap.bounds.top;
1354
1355 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1356 cam_rect->height = rect->top + rect->height -
1357 cam_rect->top;
1358
1359 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1360 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001361 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001362 cam_rect->width, cam_rect->height,
1363 cam_rect->left, cam_rect->top);
1364 }
1365
1366 /* S_CROP must not modify the rectangle */
1367 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1368 /*
1369 * The camera failed to configure a suitable cropping,
1370 * we cannot use the current rectangle, set to max
1371 */
1372 *cam_rect = cap.bounds;
1373 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1374 ret = client_g_rect(sd, cam_rect);
Márton Némethe26b3142010-02-24 17:13:29 -03001375 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001376 cam_rect->width, cam_rect->height,
1377 cam_rect->left, cam_rect->top);
1378 }
1379
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001380 if (!ret) {
1381 cam->rect = *cam_rect;
1382 update_subrect(cam);
1383 }
1384
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001385 return ret;
1386}
1387
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001388/* Iterative s_mbus_fmt, also updates cached client crop on success */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001389static int client_s_fmt(struct soc_camera_device *icd,
1390 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001391{
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001392 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1393 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001394 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001395 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001396 struct device *dev = icd->parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001397 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001398 unsigned int max_width, max_height;
1399 struct v4l2_cropcap cap;
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001400 bool ceu_1to1;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001401 int ret;
1402
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001403 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1404 soc_camera_grp_id(icd), video,
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001405 s_mbus_fmt, mf);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001406 if (ret < 0)
1407 return ret;
1408
1409 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1410
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001411 if (width == mf->width && height == mf->height) {
1412 /* Perfect! The client has done it all. */
1413 ceu_1to1 = true;
1414 goto update_cache;
1415 }
1416
1417 ceu_1to1 = false;
1418 if (!ceu_can_scale)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001419 goto update_cache;
1420
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001421 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1422
1423 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1424 if (ret < 0)
1425 return ret;
1426
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001427 max_width = min(cap.bounds.width, pcdev->max_width);
1428 max_height = min(cap.bounds.height, pcdev->max_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001429
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001430 /* Camera set a format, but geometry is not precise, try to improve */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001431 tmp_w = mf->width;
1432 tmp_h = mf->height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001433
1434 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1435 while ((width > tmp_w || height > tmp_h) &&
1436 tmp_w < max_width && tmp_h < max_height) {
1437 tmp_w = min(2 * tmp_w, max_width);
1438 tmp_h = min(2 * tmp_h, max_height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001439 mf->width = tmp_w;
1440 mf->height = tmp_h;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001441 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1442 soc_camera_grp_id(icd), video,
1443 s_mbus_fmt, mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001444 dev_geo(dev, "Camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001445 mf->width, mf->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001446 if (ret < 0) {
1447 /* This shouldn't happen */
1448 dev_err(dev, "Client failed to set format: %d\n", ret);
1449 return ret;
1450 }
1451 }
1452
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001453update_cache:
1454 /* Update cache */
1455 ret = client_g_rect(sd, &cam->rect);
1456 if (ret < 0)
1457 return ret;
1458
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001459 if (ceu_1to1)
1460 cam->subrect = cam->rect;
1461 else
1462 update_subrect(cam);
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001463
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001464 return 0;
1465}
1466
1467/**
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001468 * @width - on output: user width, mapped back to input
1469 * @height - on output: user height, mapped back to input
1470 * @mf - in- / output camera output window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001471 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001472static int client_scale(struct soc_camera_device *icd,
1473 struct v4l2_mbus_framefmt *mf,
1474 unsigned int *width, unsigned int *height,
1475 bool ceu_can_scale)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001476{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001477 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001478 struct device *dev = icd->parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001479 struct v4l2_mbus_framefmt mf_tmp = *mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001480 unsigned int scale_h, scale_v;
1481 int ret;
1482
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001483 /*
1484 * 5. Apply iterative camera S_FMT for camera user window (also updates
1485 * client crop cache and the imaginary sub-rectangle).
1486 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001487 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001488 if (ret < 0)
1489 return ret;
1490
1491 dev_geo(dev, "5: camera scaled to %ux%u\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001492 mf_tmp.width, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001493
1494 /* 6. Retrieve camera output window (g_fmt) */
1495
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001496 /* unneeded - it is already in "mf_tmp" */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001497
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001498 /* 7. Calculate new client scales. */
1499 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1500 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001501
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001502 mf->width = mf_tmp.width;
1503 mf->height = mf_tmp.height;
1504 mf->colorspace = mf_tmp.colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001505
1506 /*
1507 * 8. Calculate new CEU crop - apply camera scales to previously
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001508 * updated "effective" crop.
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001509 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001510 *width = scale_down(cam->subrect.width, scale_h);
1511 *height = scale_down(cam->subrect.height, scale_v);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001512
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001513 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001514
1515 return 0;
1516}
1517
1518/*
1519 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1520 * framerate by always requesting the maximum image from the client. See
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001521 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001522 * scaling and cropping algorithms and for the meaning of referenced here steps.
1523 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001524static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001525 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001526{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001527 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001528 struct device *dev = icd->parent;
1529 struct soc_camera_host *ici = to_soc_camera_host(dev);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001530 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001531 struct v4l2_crop cam_crop;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001532 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001533 struct v4l2_rect *cam_rect = &cam_crop.c;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001534 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001535 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001536 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001537 out_width, out_height;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001538 int interm_width, interm_height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001539 u32 capsr, cflcr;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001540 int ret;
1541
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001542 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1543 rect->left, rect->top);
1544
1545 /* During camera cropping its output window can change too, stop CEU */
1546 capsr = capture_save_reset(pcdev);
1547 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1548
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001549 /*
1550 * 1. - 2. Apply iterative camera S_CROP for new input window, read back
1551 * actual camera rectangle.
1552 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001553 ret = client_s_crop(icd, a, &cam_crop);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001554 if (ret < 0)
1555 return ret;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001556
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001557 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001558 cam_rect->width, cam_rect->height,
1559 cam_rect->left, cam_rect->top);
1560
1561 /* On success cam_crop contains current camera crop */
1562
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001563 /* 3. Retrieve camera output window */
1564 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1565 if (ret < 0)
1566 return ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001567
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001568 if (mf.width > pcdev->max_width || mf.height > pcdev->max_height)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001569 return -EINVAL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001570
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001571 /* 4. Calculate camera scales */
1572 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1573 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001574
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001575 /* Calculate intermediate window */
1576 interm_width = scale_down(rect->width, scale_cam_h);
1577 interm_height = scale_down(rect->height, scale_cam_v);
1578
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001579 if (interm_width < icd->user_width) {
1580 u32 new_scale_h;
1581
1582 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1583
1584 mf.width = scale_down(cam_rect->width, new_scale_h);
1585 }
1586
1587 if (interm_height < icd->user_height) {
1588 u32 new_scale_v;
1589
1590 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1591
1592 mf.height = scale_down(cam_rect->height, new_scale_v);
1593 }
1594
1595 if (interm_width < icd->user_width || interm_height < icd->user_height) {
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001596 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1597 soc_camera_grp_id(icd), video,
1598 s_mbus_fmt, &mf);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001599 if (ret < 0)
1600 return ret;
1601
1602 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1603 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1604 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1605 interm_width = scale_down(rect->width, scale_cam_h);
1606 interm_height = scale_down(rect->height, scale_cam_v);
1607 }
1608
1609 /* Cache camera output window */
1610 cam->width = mf.width;
1611 cam->height = mf.height;
1612
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001613 if (pcdev->image_mode) {
1614 out_width = min(interm_width, icd->user_width);
1615 out_height = min(interm_height, icd->user_height);
1616 } else {
1617 out_width = interm_width;
1618 out_height = interm_height;
1619 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001620
1621 /*
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001622 * 5. Calculate CEU scales from camera scales from results of (5) and
1623 * the user window
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001624 */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001625 scale_ceu_h = calc_scale(interm_width, &out_width);
1626 scale_ceu_v = calc_scale(interm_height, &out_height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001627
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001628 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001629
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001630 /* Apply CEU scales. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001631 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1632 if (cflcr != pcdev->cflcr) {
1633 pcdev->cflcr = cflcr;
1634 ceu_write(pcdev, CFLCR, cflcr);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001635 }
1636
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001637 icd->user_width = out_width & ~3;
1638 icd->user_height = out_height & ~3;
1639 /* Offsets are applied at the CEU scaling filter input */
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001640 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1641 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001642
1643 /* 6. Use CEU cropping to crop to the new window. */
1644 sh_mobile_ceu_set_rect(icd);
1645
1646 cam->subrect = *rect;
1647
1648 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1649 icd->user_width, icd->user_height,
1650 cam->ceu_left, cam->ceu_top);
1651
Guennadi Liakhovetski787d0f92011-02-16 18:42:24 -03001652 /* Restore capture. The CE bit can be cleared by the hardware */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001653 if (pcdev->active)
1654 capsr |= 1;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001655 capture_restore(pcdev, capsr);
1656
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001657 /* Even if only camera cropping succeeded */
1658 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001659}
1660
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001661static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1662 struct v4l2_crop *a)
1663{
1664 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1665
1666 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1667 a->c = cam->subrect;
1668
1669 return 0;
1670}
1671
1672/*
1673 * Calculate real client output window by applying new scales to the current
1674 * client crop. New scales are calculated from the requested output format and
1675 * CEU crop, mapped backed onto the client input (subrect).
1676 */
1677static void calculate_client_output(struct soc_camera_device *icd,
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001678 const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001679{
1680 struct sh_mobile_ceu_cam *cam = icd->host_priv;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001681 struct device *dev = icd->parent;
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001682 struct v4l2_rect *cam_subrect = &cam->subrect;
1683 unsigned int scale_v, scale_h;
1684
1685 if (cam_subrect->width == cam->rect.width &&
1686 cam_subrect->height == cam->rect.height) {
1687 /* No sub-cropping */
1688 mf->width = pix->width;
1689 mf->height = pix->height;
1690 return;
1691 }
1692
1693 /* 1.-2. Current camera scales and subwin - cached. */
1694
1695 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1696 cam_subrect->width, cam_subrect->height,
1697 cam_subrect->left, cam_subrect->top);
1698
1699 /*
1700 * 3. Calculate new combined scales from input sub-window to requested
1701 * user window.
1702 */
1703
1704 /*
1705 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1706 * (128x96) or larger than VGA
1707 */
1708 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1709 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1710
1711 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1712
1713 /*
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001714 * 4. Calculate desired client output window by applying combined scales
1715 * to client (real) input window.
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001716 */
1717 mf->width = scale_down(cam->rect.width, scale_h);
1718 mf->height = scale_down(cam->rect.height, scale_v);
1719}
1720
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001721/* Similar to set_crop multistage iterative algorithm */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001722static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001723 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001724{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001725 struct device *dev = icd->parent;
1726 struct soc_camera_host *ici = to_soc_camera_host(dev);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001727 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001728 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1729 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001730 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001731 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -03001732 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001733 /* Keep Compiler Happy */
1734 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001735 u16 scale_v, scale_h;
1736 int ret;
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001737 bool image_mode;
1738 enum v4l2_field field;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001739
1740 switch (pix->field) {
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001741 default:
1742 pix->field = V4L2_FIELD_NONE;
1743 /* fall-through */
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001744 case V4L2_FIELD_INTERLACED_TB:
1745 case V4L2_FIELD_INTERLACED_BT:
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001746 case V4L2_FIELD_NONE:
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001747 field = pix->field;
1748 break;
1749 case V4L2_FIELD_INTERLACED:
1750 field = V4L2_FIELD_INTERLACED_TB;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001751 break;
1752 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001753
Magnus Damm9414de32008-12-18 11:49:06 -03001754 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1755 if (!xlate) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001756 dev_warn(dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -03001757 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001758 }
1759
Guennadi Liakhovetski36988742011-07-22 09:44:40 -03001760 /* 1.-4. Calculate desired client output geometry */
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001761 calculate_client_output(icd, pix, &mf);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001762 mf.field = pix->field;
1763 mf.colorspace = pix->colorspace;
1764 mf.code = xlate->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001765
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001766 switch (pixfmt) {
1767 case V4L2_PIX_FMT_NV12:
1768 case V4L2_PIX_FMT_NV21:
1769 case V4L2_PIX_FMT_NV16:
1770 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001771 image_mode = true;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001772 break;
1773 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001774 image_mode = false;
Magnus Damm9414de32008-12-18 11:49:06 -03001775 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001776
Guennadi Liakhovetskib5518a42011-11-03 10:11:11 -03001777 dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
Guennadi Liakhovetskie1db7042011-07-27 18:17:56 -03001778 pix->width, pix->height);
1779
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001780 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001781
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001782 /* 5. - 9. */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001783 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001784 image_mode && V4L2_FIELD_NONE == field);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001785
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001786 dev_geo(dev, "5-9: client scale return %d\n", ret);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001787
1788 /* Done with the camera. Now see if we can improve the result */
1789
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001790 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1791 mf.width, mf.height, pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001792 if (ret < 0)
1793 return ret;
1794
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001795 if (mf.code != xlate->code)
1796 return -EINVAL;
1797
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001798 /* 9. Prepare CEU crop */
1799 cam->width = mf.width;
1800 cam->height = mf.height;
1801
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001802 /* 10. Use CEU scaling to scale to the requested user window. */
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001803
1804 /* We cannot scale up */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001805 if (pix->width > ceu_sub_width)
1806 ceu_sub_width = pix->width;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001807
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001808 if (pix->height > ceu_sub_height)
1809 ceu_sub_height = pix->height;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001810
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001811 pix->colorspace = mf.colorspace;
1812
1813 if (image_mode) {
1814 /* Scale pix->{width x height} down to width x height */
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001815 scale_h = calc_scale(ceu_sub_width, &pix->width);
1816 scale_v = calc_scale(ceu_sub_height, &pix->height);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001817 } else {
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001818 pix->width = ceu_sub_width;
1819 pix->height = ceu_sub_height;
1820 scale_h = 0;
1821 scale_v = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001822 }
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001823
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001824 pcdev->cflcr = scale_h | (scale_v << 16);
1825
1826 /*
1827 * We have calculated CFLCR, the actual configuration will be performed
1828 * in sh_mobile_ceu_set_bus_param()
1829 */
1830
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001831 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03001832 ceu_sub_width, scale_h, pix->width,
1833 ceu_sub_height, scale_v, pix->height);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001834
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001835 cam->code = xlate->code;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001836 icd->current_fmt = xlate;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001837
Kuninori Morimoto1edcc102009-12-11 11:53:53 -03001838 pcdev->field = field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001839 pcdev->image_mode = image_mode;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001840
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001841 /* CFSZR requirement */
1842 pix->width &= ~3;
1843 pix->height &= ~3;
1844
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001845 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001846}
1847
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001848static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1849 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001850{
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001851 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1852 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm9414de32008-12-18 11:49:06 -03001853 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001854 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001855 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001856 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001857 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001858 int width, height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001859 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001860
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001861 dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001862 pixfmt, pix->width, pix->height);
1863
Magnus Damm9414de32008-12-18 11:49:06 -03001864 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1865 if (!xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001866 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001867 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -03001868 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001869
Magnus Damm0d3244d2008-07-16 22:59:28 -03001870 /* FIXME: calculate using depth and bus width */
1871
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001872 /* CFSZR requires height and width to be 4-pixel aligned */
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001873 v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
1874 &pix->height, 4, pcdev->max_height, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001875
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001876 width = pix->width;
1877 height = pix->height;
1878
Magnus Damm0d3244d2008-07-16 22:59:28 -03001879 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001880 mf.width = pix->width;
1881 mf.height = pix->height;
1882 mf.field = pix->field;
1883 mf.code = xlate->code;
1884 mf.colorspace = pix->colorspace;
1885
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001886 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1887 video, try_mbus_fmt, &mf);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001888 if (ret < 0)
1889 return ret;
1890
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001891 pix->width = mf.width;
1892 pix->height = mf.height;
1893 pix->field = mf.field;
1894 pix->colorspace = mf.colorspace;
1895
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001896 switch (pixfmt) {
1897 case V4L2_PIX_FMT_NV12:
1898 case V4L2_PIX_FMT_NV21:
1899 case V4L2_PIX_FMT_NV16:
1900 case V4L2_PIX_FMT_NV61:
1901 /* FIXME: check against rect_max after converting soc-camera */
1902 /* We can scale precisely, need a bigger image from camera */
1903 if (pix->width < width || pix->height < height) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001904 /*
1905 * We presume, the sensor behaves sanely, i.e., if
1906 * requested a bigger rectangle, it will not return a
1907 * smaller one.
1908 */
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03001909 mf.width = pcdev->max_width;
1910 mf.height = pcdev->max_height;
Guennadi Liakhovetski4c0b0362011-12-05 16:01:13 -03001911 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1912 soc_camera_grp_id(icd), video,
1913 try_mbus_fmt, &mf);
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001914 if (ret < 0) {
1915 /* Shouldn't actually happen... */
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001916 dev_err(icd->parent,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001917 "FIXME: client try_fmt() = %d\n", ret);
1918 return ret;
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001919 }
1920 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001921 /* We will scale exactly */
1922 if (mf.width > width)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001923 pix->width = width;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001924 if (mf.height > height)
Guennadi Liakhovetski961801b2009-08-25 11:46:54 -03001925 pix->height = height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001926 }
1927
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001928 pix->width &= ~3;
1929 pix->height &= ~3;
1930
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001931 dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03001932 __func__, ret, pix->pixelformat, pix->width, pix->height);
1933
Kuninori Morimotoccab8a22008-12-18 12:51:21 -03001934 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001935}
1936
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001937static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1938 struct v4l2_crop *a)
1939{
1940 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001941 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001942 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1943 u32 out_width = icd->user_width, out_height = icd->user_height;
1944 int ret;
1945
1946 /* Freeze queue */
1947 pcdev->frozen = 1;
1948 /* Wait for frame */
1949 ret = wait_for_completion_interruptible(&pcdev->complete);
1950 /* Stop the client */
1951 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1952 if (ret < 0)
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001953 dev_warn(icd->parent,
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001954 "Client failed to stop the stream: %d\n", ret);
1955 else
1956 /* Do the crop, if it fails, there's nothing more we can do */
1957 sh_mobile_ceu_set_crop(icd, a);
1958
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001959 dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001960
1961 if (icd->user_width != out_width || icd->user_height != out_height) {
1962 struct v4l2_format f = {
1963 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1964 .fmt.pix = {
1965 .width = out_width,
1966 .height = out_height,
1967 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1968 .field = pcdev->field,
1969 .colorspace = icd->colorspace,
1970 },
1971 };
1972 ret = sh_mobile_ceu_set_fmt(icd, &f);
1973 if (!ret && (out_width != f.fmt.pix.width ||
1974 out_height != f.fmt.pix.height))
1975 ret = -EINVAL;
1976 if (!ret) {
Guennadi Liakhovetskieec5ce02011-07-22 07:43:15 -03001977 icd->user_width = out_width & ~3;
1978 icd->user_height = out_height & ~3;
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -03001979 ret = sh_mobile_ceu_set_bus_param(icd);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03001980 }
1981 }
1982
1983 /* Thaw the queue */
1984 pcdev->frozen = 0;
1985 spin_lock_irq(&pcdev->lock);
1986 sh_mobile_ceu_capture(pcdev);
1987 spin_unlock_irq(&pcdev->lock);
1988 /* Start the client */
1989 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1990 return ret;
1991}
1992
Magnus Damm0d3244d2008-07-16 22:59:28 -03001993static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1994{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -03001995 struct soc_camera_device *icd = file->private_data;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001996
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03001997 return vb2_poll(&icd->vb2_vidq, file, pt);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001998}
1999
2000static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
2001 struct v4l2_capability *cap)
2002{
2003 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
Magnus Damm0d3244d2008-07-16 22:59:28 -03002004 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2005 return 0;
2006}
2007
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002008static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
2009 struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002010{
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002011 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2012 q->io_modes = VB2_MMAP | VB2_USERPTR;
2013 q->drv_priv = icd;
2014 q->ops = &sh_mobile_ceu_videobuf_ops;
2015 q->mem_ops = &vb2_dma_contig_memops;
2016 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002017
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002018 return vb2_queue_init(q);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002019}
2020
2021static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
2022 .owner = THIS_MODULE,
2023 .add = sh_mobile_ceu_add_device,
2024 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03002025 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03002026 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetskie6226812010-03-23 11:23:31 -03002027 .get_crop = sh_mobile_ceu_get_crop,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03002028 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03002029 .set_livecrop = sh_mobile_ceu_set_livecrop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03002030 .set_fmt = sh_mobile_ceu_set_fmt,
2031 .try_fmt = sh_mobile_ceu_try_fmt,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002032 .poll = sh_mobile_ceu_poll,
2033 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002034 .set_bus_param = sh_mobile_ceu_set_bus_param,
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002035 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002036};
2037
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002038struct bus_wait {
2039 struct notifier_block notifier;
2040 struct completion completion;
2041 struct device *dev;
2042};
2043
2044static int bus_notify(struct notifier_block *nb,
2045 unsigned long action, void *data)
2046{
2047 struct device *dev = data;
2048 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
2049
2050 if (wait->dev != dev)
2051 return NOTIFY_DONE;
2052
2053 switch (action) {
2054 case BUS_NOTIFY_UNBOUND_DRIVER:
2055 /* Protect from module unloading */
2056 wait_for_completion(&wait->completion);
2057 return NOTIFY_OK;
2058 }
2059 return NOTIFY_DONE;
2060}
2061
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002062static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002063{
2064 struct sh_mobile_ceu_dev *pcdev;
2065 struct resource *res;
2066 void __iomem *base;
2067 unsigned int irq;
2068 int err = 0;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002069 struct bus_wait wait = {
2070 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
2071 .notifier.notifier_call = bus_notify,
2072 };
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002073 struct sh_mobile_ceu_companion *csi2;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002074
2075 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2076 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb89fc2e2010-01-09 20:45:13 -03002077 if (!res || (int)irq <= 0) {
Magnus Damm0d3244d2008-07-16 22:59:28 -03002078 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
2079 err = -ENODEV;
2080 goto exit;
2081 }
2082
2083 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
2084 if (!pcdev) {
2085 dev_err(&pdev->dev, "Could not allocate pcdev\n");
2086 err = -ENOMEM;
2087 goto exit;
2088 }
2089
Magnus Damm0d3244d2008-07-16 22:59:28 -03002090 INIT_LIST_HEAD(&pcdev->capture);
2091 spin_lock_init(&pcdev->lock);
Guennadi Liakhovetski3dac3222011-03-28 13:39:16 -03002092 init_completion(&pcdev->complete);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002093
2094 pcdev->pdata = pdev->dev.platform_data;
2095 if (!pcdev->pdata) {
2096 err = -EINVAL;
2097 dev_err(&pdev->dev, "CEU platform data not set.\n");
2098 goto exit_kfree;
2099 }
2100
Guennadi Liakhovetski48e971c2012-03-14 08:37:03 -03002101 pcdev->max_width = pcdev->pdata->max_width ? : 2560;
2102 pcdev->max_height = pcdev->pdata->max_height ? : 1920;
2103
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002104 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03002105 if (!base) {
2106 err = -ENXIO;
2107 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
2108 goto exit_kfree;
2109 }
2110
2111 pcdev->irq = irq;
2112 pcdev->base = base;
2113 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03002114
2115 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2116 if (res) {
2117 err = dma_declare_coherent_memory(&pdev->dev, res->start,
2118 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002119 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002120 DMA_MEMORY_MAP |
2121 DMA_MEMORY_EXCLUSIVE);
2122 if (!err) {
2123 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2124 err = -ENXIO;
2125 goto exit_iounmap;
2126 }
2127
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03002128 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002129 }
2130
2131 /* request irq */
2132 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03002133 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002134 if (err) {
2135 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2136 goto exit_release_mem;
2137 }
2138
Magnus Damm6d1386c2009-08-14 10:49:17 +00002139 pm_suspend_ignore_children(&pdev->dev, true);
2140 pm_runtime_enable(&pdev->dev);
2141 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09002142
Magnus Damm0d3244d2008-07-16 22:59:28 -03002143 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002144 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002145 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03002146 pcdev->ici.drv_name = dev_name(&pdev->dev);
2147 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002148
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002149 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2150 if (IS_ERR(pcdev->alloc_ctx)) {
2151 err = PTR_ERR(pcdev->alloc_ctx);
2152 goto exit_free_clk;
2153 }
2154
2155 err = soc_camera_host_register(&pcdev->ici);
2156 if (err)
2157 goto exit_free_ctx;
2158
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002159 /* CSI2 interfacing */
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002160 csi2 = pcdev->pdata->csi2;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002161 if (csi2) {
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002162 struct platform_device *csi2_pdev =
2163 platform_device_alloc("sh-mobile-csi2", csi2->id);
2164 struct sh_csi2_pdata *csi2_pdata = csi2->platform_data;
2165
2166 if (!csi2_pdev) {
2167 err = -ENOMEM;
2168 goto exit_host_unregister;
2169 }
2170
2171 pcdev->csi2_pdev = csi2_pdev;
2172
2173 err = platform_device_add_data(csi2_pdev, csi2_pdata, sizeof(*csi2_pdata));
2174 if (err < 0)
2175 goto exit_pdev_put;
2176
2177 csi2_pdata = csi2_pdev->dev.platform_data;
2178 csi2_pdata->v4l2_dev = &pcdev->ici.v4l2_dev;
2179
2180 csi2_pdev->resource = csi2->resource;
2181 csi2_pdev->num_resources = csi2->num_resources;
2182
2183 err = platform_device_add(csi2_pdev);
2184 if (err < 0)
2185 goto exit_pdev_put;
2186
2187 wait.dev = &csi2_pdev->dev;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002188
2189 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2190 if (err < 0)
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002191 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002192
2193 /*
2194 * From this point the driver module will not unload, until
2195 * we complete the completion.
2196 */
2197
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002198 if (!csi2_pdev->dev.driver) {
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002199 complete(&wait.completion);
2200 /* Either too late, or probing failed */
2201 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2202 err = -ENXIO;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002203 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002204 }
2205
2206 /*
2207 * The module is still loaded, in the worst case it is hanging
2208 * in device release on our completion. So, _now_ dereferencing
2209 * the "owner" is safe!
2210 */
2211
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002212 err = try_module_get(csi2_pdev->dev.driver->owner);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002213
2214 /* Let notifier complete, if it has been locked */
2215 complete(&wait.completion);
2216 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2217 if (!err) {
2218 err = -ENODEV;
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002219 goto exit_pdev_unregister;
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002220 }
2221 }
2222
Magnus Damm0d3244d2008-07-16 22:59:28 -03002223 return 0;
2224
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002225exit_pdev_unregister:
2226 platform_device_del(pcdev->csi2_pdev);
2227exit_pdev_put:
2228 pcdev->csi2_pdev->resource = NULL;
2229 platform_device_put(pcdev->csi2_pdev);
2230exit_host_unregister:
2231 soc_camera_host_unregister(&pcdev->ici);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002232exit_free_ctx:
2233 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002234exit_free_clk:
2235 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002236 free_irq(pcdev->irq, pcdev);
2237exit_release_mem:
2238 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2239 dma_release_declared_memory(&pdev->dev);
2240exit_iounmap:
2241 iounmap(base);
2242exit_kfree:
2243 kfree(pcdev);
2244exit:
2245 return err;
2246}
2247
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03002248static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03002249{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002250 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2251 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2252 struct sh_mobile_ceu_dev, ici);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002253 struct platform_device *csi2_pdev = pcdev->csi2_pdev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03002254
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03002255 soc_camera_host_unregister(soc_host);
Kuninori Morimoto055e05a2009-11-09 16:13:24 -03002256 pm_runtime_disable(&pdev->dev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002257 free_irq(pcdev->irq, pcdev);
2258 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2259 dma_release_declared_memory(&pdev->dev);
2260 iounmap(pcdev->base);
Guennadi Liakhovetskia6168982011-02-18 05:30:15 -03002261 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
Guennadi Liakhovetski6b526fe2011-07-01 11:19:58 -03002262 if (csi2_pdev && csi2_pdev->dev.driver) {
2263 struct module *csi2_drv = csi2_pdev->dev.driver->owner;
2264 platform_device_del(csi2_pdev);
2265 csi2_pdev->resource = NULL;
2266 platform_device_put(csi2_pdev);
2267 module_put(csi2_drv);
2268 }
Magnus Damm0d3244d2008-07-16 22:59:28 -03002269 kfree(pcdev);
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002270
Magnus Damm0d3244d2008-07-16 22:59:28 -03002271 return 0;
2272}
2273
Magnus Damm6d1386c2009-08-14 10:49:17 +00002274static int sh_mobile_ceu_runtime_nop(struct device *dev)
2275{
2276 /* Runtime PM callback shared between ->runtime_suspend()
2277 * and ->runtime_resume(). Simply returns success.
2278 *
2279 * This driver re-initializes all registers after
2280 * pm_runtime_get_sync() anyway so there is no need
2281 * to save and restore registers here.
2282 */
2283 return 0;
2284}
2285
Alexey Dobriyan47145212009-12-14 18:00:08 -08002286static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
Magnus Damm6d1386c2009-08-14 10:49:17 +00002287 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2288 .runtime_resume = sh_mobile_ceu_runtime_nop,
2289};
2290
Magnus Damm0d3244d2008-07-16 22:59:28 -03002291static struct platform_driver sh_mobile_ceu_driver = {
2292 .driver = {
2293 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00002294 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03002295 },
2296 .probe = sh_mobile_ceu_probe,
Uwe Kleine-König50e55fd2009-12-10 17:00:13 -03002297 .remove = __devexit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03002298};
2299
2300static int __init sh_mobile_ceu_init(void)
2301{
Guennadi Liakhovetskib3b50202010-07-26 12:13:34 -03002302 /* Whatever return code */
2303 request_module("sh_mobile_csi2");
Magnus Damm0d3244d2008-07-16 22:59:28 -03002304 return platform_driver_register(&sh_mobile_ceu_driver);
2305}
2306
2307static void __exit sh_mobile_ceu_exit(void)
2308{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03002309 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03002310}
2311
2312module_init(sh_mobile_ceu_init);
2313module_exit(sh_mobile_ceu_exit);
2314
2315MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2316MODULE_AUTHOR("Magnus Damm");
2317MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002318MODULE_VERSION("0.0.6");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03002319MODULE_ALIAS("platform:sh_mobile_ceu");