blob: 3fee7c01fd57633a135c4222f971a7fe140d404b [file] [log] [blame]
Magnus Damm0d3244d2008-07-16 22:59:28 -03001/*
2 * V4L2 Driver for SuperH Mobile CEU interface
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
7 *
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <linux/delay.h>
21#include <linux/dma-mapping.h>
22#include <linux/errno.h>
23#include <linux/fs.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/moduleparam.h>
28#include <linux/time.h>
29#include <linux/version.h>
30#include <linux/device.h>
31#include <linux/platform_device.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030032#include <linux/videodev2.h>
Magnus Damm6d1386c2009-08-14 10:49:17 +000033#include <linux/pm_runtime.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030034
35#include <media/v4l2-common.h>
36#include <media/v4l2-dev.h>
37#include <media/soc_camera.h>
38#include <media/sh_mobile_ceu.h>
39#include <media/videobuf-dma-contig.h>
40
41/* register offsets for sh7722 / sh7723 */
42
Magnus Dammdd542032008-10-16 19:50:22 -030043#define CAPSR 0x00 /* Capture start register */
44#define CAPCR 0x04 /* Capture control register */
45#define CAMCR 0x08 /* Capture interface control register */
46#define CMCYR 0x0c /* Capture interface cycle register */
47#define CAMOR 0x10 /* Capture interface offset register */
48#define CAPWR 0x14 /* Capture interface width register */
49#define CAIFR 0x18 /* Capture interface input format register */
50#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
51#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
52#define CRCNTR 0x28 /* CEU register control register */
53#define CRCMPR 0x2c /* CEU register forcible control register */
54#define CFLCR 0x30 /* Capture filter control register */
55#define CFSZR 0x34 /* Capture filter size clip register */
56#define CDWDR 0x38 /* Capture destination width register */
57#define CDAYR 0x3c /* Capture data address Y register */
58#define CDACR 0x40 /* Capture data address C register */
59#define CDBYR 0x44 /* Capture data bottom-field address Y register */
60#define CDBCR 0x48 /* Capture data bottom-field address C register */
61#define CBDSR 0x4c /* Capture bundle destination size register */
62#define CFWCR 0x5c /* Firewall operation control register */
63#define CLFCR 0x60 /* Capture low-pass filter control register */
64#define CDOCR 0x64 /* Capture data output control register */
65#define CDDCR 0x68 /* Capture data complexity level register */
66#define CDDAR 0x6c /* Capture data complexity level address register */
67#define CEIER 0x70 /* Capture event interrupt enable register */
68#define CETCR 0x74 /* Capture event flag clear register */
69#define CSTSR 0x7c /* Capture status register */
70#define CSRTR 0x80 /* Capture software reset register */
71#define CDSSR 0x84 /* Capture data size register */
72#define CDAYR2 0x90 /* Capture data address Y register 2 */
73#define CDACR2 0x94 /* Capture data address C register 2 */
74#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
75#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030076
Magnus Damm0d3244d2008-07-16 22:59:28 -030077/* per video frame buffer */
78struct sh_mobile_ceu_buffer {
79 struct videobuf_buffer vb; /* v4l buffer must be first */
80 const struct soc_camera_data_format *fmt;
81};
82
83struct sh_mobile_ceu_dev {
Magnus Damm0d3244d2008-07-16 22:59:28 -030084 struct soc_camera_host ici;
85 struct soc_camera_device *icd;
86
87 unsigned int irq;
88 void __iomem *base;
89 unsigned long video_limit;
90
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -030091 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -030092 spinlock_t lock;
93 struct list_head capture;
94 struct videobuf_buffer *active;
95
96 struct sh_mobile_ceu_info *pdata;
Magnus Damm8be65dc2008-12-18 12:38:06 -030097
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -030098 unsigned int is_interlaced:1;
99 unsigned int image_mode:1;
100 unsigned int is_16bit:1;
101};
102
103struct sh_mobile_ceu_cam {
104 struct v4l2_rect camera_rect;
105 const struct soc_camera_data_format *extra_fmt;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300106 const struct soc_camera_data_format *camera_fmt;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300107};
108
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300109static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
110{
111 unsigned long flags;
112
113 flags = SOCAM_MASTER |
114 SOCAM_PCLK_SAMPLE_RISING |
115 SOCAM_HSYNC_ACTIVE_HIGH |
116 SOCAM_HSYNC_ACTIVE_LOW |
117 SOCAM_VSYNC_ACTIVE_HIGH |
118 SOCAM_VSYNC_ACTIVE_LOW |
119 SOCAM_DATA_ACTIVE_HIGH;
120
121 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
122 flags |= SOCAM_DATAWIDTH_8;
123
124 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
125 flags |= SOCAM_DATAWIDTH_16;
126
127 if (flags & SOCAM_DATAWIDTH_MASK)
128 return flags;
129
130 return 0;
131}
132
Magnus Damm0d3244d2008-07-16 22:59:28 -0300133static void ceu_write(struct sh_mobile_ceu_dev *priv,
Magnus Damm7a1a8162008-12-18 12:50:30 -0300134 unsigned long reg_offs, u32 data)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300135{
136 iowrite32(data, priv->base + reg_offs);
137}
138
Magnus Damm7a1a8162008-12-18 12:50:30 -0300139static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300140{
141 return ioread32(priv->base + reg_offs);
142}
143
144/*
145 * Videobuf operations
146 */
147static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
148 unsigned int *count,
149 unsigned int *size)
150{
151 struct soc_camera_device *icd = vq->priv_data;
152 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
153 struct sh_mobile_ceu_dev *pcdev = ici->priv;
154 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
155
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300156 *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height *
157 bytes_per_pixel);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300158
159 if (0 == *count)
160 *count = 2;
161
162 if (pcdev->video_limit) {
163 while (*size * *count > pcdev->video_limit)
164 (*count)--;
165 }
166
167 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
168
169 return 0;
170}
171
172static void free_buffer(struct videobuf_queue *vq,
173 struct sh_mobile_ceu_buffer *buf)
174{
175 struct soc_camera_device *icd = vq->priv_data;
176
Hans Verkuil86751b02008-07-18 01:35:14 -0300177 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300178 &buf->vb, buf->vb.baddr, buf->vb.bsize);
179
180 if (in_interrupt())
181 BUG();
182
Magnus Damm97215cb2009-03-13 06:08:20 -0300183 videobuf_waiton(&buf->vb, 0, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300184 videobuf_dma_contig_free(vq, &buf->vb);
185 dev_dbg(&icd->dev, "%s freed\n", __func__);
186 buf->vb.state = VIDEOBUF_NEEDS_INIT;
187}
188
Magnus Damm7a1a8162008-12-18 12:50:30 -0300189#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
190#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
191#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
192#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
193
194
Magnus Damm0d3244d2008-07-16 22:59:28 -0300195static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
196{
Magnus Damm8be65dc2008-12-18 12:38:06 -0300197 struct soc_camera_device *icd = pcdev->icd;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300198 dma_addr_t phys_addr_top, phys_addr_bottom;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300199
Magnus Damm7a1a8162008-12-18 12:50:30 -0300200 /* The hardware is _very_ picky about this sequence. Especially
201 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
202 * several not-so-well documented interrupt sources in CETCR.
203 */
204 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
205 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
206 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
207 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
208 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300209
Magnus Damm8be65dc2008-12-18 12:38:06 -0300210 if (!pcdev->active)
211 return;
212
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300213 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
214 ceu_write(pcdev, CDAYR, phys_addr_top);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300215 if (pcdev->is_interlaced) {
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300216 phys_addr_bottom = phys_addr_top + icd->rect_current.width;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300217 ceu_write(pcdev, CDBYR, phys_addr_bottom);
218 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300219
220 switch (icd->current_fmt->fourcc) {
221 case V4L2_PIX_FMT_NV12:
222 case V4L2_PIX_FMT_NV21:
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300223 case V4L2_PIX_FMT_NV16:
224 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300225 phys_addr_top += icd->rect_current.width *
226 icd->rect_current.height;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300227 ceu_write(pcdev, CDACR, phys_addr_top);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300228 if (pcdev->is_interlaced) {
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300229 phys_addr_bottom = phys_addr_top +
230 icd->rect_current.width;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300231 ceu_write(pcdev, CDBCR, phys_addr_bottom);
232 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300233 }
Magnus Damm8be65dc2008-12-18 12:38:06 -0300234
235 pcdev->active->state = VIDEOBUF_ACTIVE;
236 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300237}
238
239static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
240 struct videobuf_buffer *vb,
241 enum v4l2_field field)
242{
243 struct soc_camera_device *icd = vq->priv_data;
244 struct sh_mobile_ceu_buffer *buf;
245 int ret;
246
247 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
248
Hans Verkuil86751b02008-07-18 01:35:14 -0300249 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300250 vb, vb->baddr, vb->bsize);
251
252 /* Added list head initialization on alloc */
253 WARN_ON(!list_empty(&vb->queue));
254
255#ifdef DEBUG
256 /* This can be useful if you want to see if we actually fill
257 * the buffer with something */
258 memset((void *)vb->baddr, 0xaa, vb->bsize);
259#endif
260
261 BUG_ON(NULL == icd->current_fmt);
262
263 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300264 vb->width != icd->rect_current.width ||
265 vb->height != icd->rect_current.height ||
Magnus Damm0d3244d2008-07-16 22:59:28 -0300266 vb->field != field) {
267 buf->fmt = icd->current_fmt;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300268 vb->width = icd->rect_current.width;
269 vb->height = icd->rect_current.height;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300270 vb->field = field;
271 vb->state = VIDEOBUF_NEEDS_INIT;
272 }
273
274 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
275 if (0 != vb->baddr && vb->bsize < vb->size) {
276 ret = -EINVAL;
277 goto out;
278 }
279
280 if (vb->state == VIDEOBUF_NEEDS_INIT) {
281 ret = videobuf_iolock(vq, vb, NULL);
282 if (ret)
283 goto fail;
284 vb->state = VIDEOBUF_PREPARED;
285 }
286
287 return 0;
288fail:
289 free_buffer(vq, buf);
290out:
291 return ret;
292}
293
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300294/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300295static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
296 struct videobuf_buffer *vb)
297{
298 struct soc_camera_device *icd = vq->priv_data;
299 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
300 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300301
Hans Verkuil86751b02008-07-18 01:35:14 -0300302 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300303 vb, vb->baddr, vb->bsize);
304
Magnus Damm51354cc2008-10-16 19:51:20 -0300305 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300306 list_add_tail(&vb->queue, &pcdev->capture);
307
308 if (!pcdev->active) {
309 pcdev->active = vb;
310 sh_mobile_ceu_capture(pcdev);
311 }
Magnus Damm0d3244d2008-07-16 22:59:28 -0300312}
313
314static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
315 struct videobuf_buffer *vb)
316{
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300317 struct soc_camera_device *icd = vq->priv_data;
318 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
319 struct sh_mobile_ceu_dev *pcdev = ici->priv;
320 unsigned long flags;
321
322 spin_lock_irqsave(&pcdev->lock, flags);
323
324 if (pcdev->active == vb) {
325 /* disable capture (release DMA buffer), reset */
326 ceu_write(pcdev, CAPSR, 1 << 16);
327 pcdev->active = NULL;
328 }
329
330 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
331 !list_empty(&vb->queue)) {
332 vb->state = VIDEOBUF_ERROR;
333 list_del_init(&vb->queue);
334 }
335
336 spin_unlock_irqrestore(&pcdev->lock, flags);
337
Magnus Damm0d3244d2008-07-16 22:59:28 -0300338 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
339}
340
341static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
342 .buf_setup = sh_mobile_ceu_videobuf_setup,
343 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
344 .buf_queue = sh_mobile_ceu_videobuf_queue,
345 .buf_release = sh_mobile_ceu_videobuf_release,
346};
347
348static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
349{
350 struct sh_mobile_ceu_dev *pcdev = data;
351 struct videobuf_buffer *vb;
352 unsigned long flags;
353
354 spin_lock_irqsave(&pcdev->lock, flags);
355
356 vb = pcdev->active;
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300357 if (!vb)
358 /* Stale interrupt from a released buffer */
359 goto out;
360
Magnus Damm0d3244d2008-07-16 22:59:28 -0300361 list_del_init(&vb->queue);
362
363 if (!list_empty(&pcdev->capture))
364 pcdev->active = list_entry(pcdev->capture.next,
365 struct videobuf_buffer, queue);
366 else
367 pcdev->active = NULL;
368
369 sh_mobile_ceu_capture(pcdev);
370
371 vb->state = VIDEOBUF_DONE;
372 do_gettimeofday(&vb->ts);
373 vb->field_count++;
374 wake_up(&vb->done);
Guennadi Liakhovetskicca0e5492009-08-25 11:46:51 -0300375
376out:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300377 spin_unlock_irqrestore(&pcdev->lock, flags);
378
379 return IRQ_HANDLED;
380}
381
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300382/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300383static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
384{
385 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
386 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300387
Magnus Damm0d3244d2008-07-16 22:59:28 -0300388 if (pcdev->icd)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300389 return -EBUSY;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300390
391 dev_info(&icd->dev,
392 "SuperH Mobile CEU driver attached to camera %d\n",
393 icd->devnum);
394
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300395 clk_enable(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300396
397 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
398 while (ceu_read(pcdev, CSTSR) & 1)
399 msleep(1);
400
401 pcdev->icd = icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300402
403 return 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300404}
405
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300406/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300407static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
408{
409 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
410 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300411 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300412
413 BUG_ON(icd != pcdev->icd);
414
415 /* disable capture, disable interrupts */
416 ceu_write(pcdev, CEIER, 0);
417 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
Magnus Damm51354cc2008-10-16 19:51:20 -0300418
419 /* make sure active buffer is canceled */
420 spin_lock_irqsave(&pcdev->lock, flags);
421 if (pcdev->active) {
422 list_del(&pcdev->active->queue);
423 pcdev->active->state = VIDEOBUF_ERROR;
424 wake_up_all(&pcdev->active->done);
425 pcdev->active = NULL;
426 }
427 spin_unlock_irqrestore(&pcdev->lock, flags);
428
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300429 clk_disable(pcdev->clk);
430
Magnus Damm0d3244d2008-07-16 22:59:28 -0300431 dev_info(&icd->dev,
432 "SuperH Mobile CEU driver detached from camera %d\n",
433 icd->devnum);
434
435 pcdev->icd = NULL;
436}
437
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300438static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
439 struct v4l2_rect *rect)
440{
441 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
442 struct sh_mobile_ceu_cam *cam = icd->host_priv;
443 struct sh_mobile_ceu_dev *pcdev = ici->priv;
444 int width, height, cfszr_width, cdwdr_width;
445 unsigned int left_offset, top_offset;
446 u32 camor;
447
448 if (rect->left > cam->camera_rect.left) {
449 left_offset = rect->left - cam->camera_rect.left;
450 } else {
451 left_offset = 0;
452 rect->left = cam->camera_rect.left;
453 }
454
455 if (rect->top > cam->camera_rect.top) {
456 top_offset = rect->top - cam->camera_rect.top;
457 } else {
458 top_offset = 0;
459 rect->top = cam->camera_rect.top;
460 }
461
462 dev_dbg(&icd->dev, "Offsets %u:%u\n", left_offset, top_offset);
463
464 if (pcdev->image_mode) {
465 width = rect->width;
466 if (!pcdev->is_16bit)
467 width *= 2;
468 cfszr_width = cdwdr_width = rect->width;
469 } else {
470 width = rect->width *
471 ((icd->current_fmt->depth + 7) >> 3);
472 width = pcdev->is_16bit ? width / 2 : width;
473 cfszr_width = pcdev->is_16bit ? width : width / 2;
474 cdwdr_width = pcdev->is_16bit ? width * 2 : width;
475 }
476
477 height = rect->height;
478 if (pcdev->is_interlaced) {
479 height /= 2;
480 cdwdr_width *= 2;
481 }
482
483 camor = left_offset | (top_offset << 16);
484 ceu_write(pcdev, CAMOR, camor);
485 ceu_write(pcdev, CAPWR, (height << 16) | width);
486 ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
487 ceu_write(pcdev, CDWDR, cdwdr_width);
488}
489
490static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
491{
492 u32 capsr = ceu_read(pcdev, CAPSR);
493 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
494 return capsr;
495}
496
497static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
498{
499 unsigned long timeout = jiffies + 10 * HZ;
500
501 /*
502 * Wait until the end of the current frame. It can take a long time,
503 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
504 */
505 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
506 msleep(1);
507
508 if (time_after(jiffies, timeout)) {
509 dev_err(pcdev->ici.v4l2_dev.dev,
510 "Timeout waiting for frame end! Interface problem?\n");
511 return;
512 }
513
514 /* Wait until reset clears, this shall not hang... */
515 while (ceu_read(pcdev, CAPSR) & (1 << 16))
516 udelay(10);
517
518 /* Anything to restore? */
519 if (capsr & ~(1 << 16))
520 ceu_write(pcdev, CAPSR, capsr);
521}
522
Magnus Damm0d3244d2008-07-16 22:59:28 -0300523static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
524 __u32 pixfmt)
525{
526 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
527 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300528 int ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300529 unsigned long camera_flags, common_flags, value;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300530 int yuv_lineskip;
531 struct sh_mobile_ceu_cam *cam = icd->host_priv;
532 u32 capsr = capture_save_reset(pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300533
534 camera_flags = icd->ops->query_bus_param(icd);
535 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300536 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300537 if (!common_flags)
538 return -EINVAL;
539
540 ret = icd->ops->set_bus_param(icd, common_flags);
541 if (ret < 0)
542 return ret;
543
544 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
545 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300546 pcdev->is_16bit = 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300547 break;
548 case SOCAM_DATAWIDTH_16:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300549 pcdev->is_16bit = 1;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300550 break;
551 default:
552 return -EINVAL;
553 }
554
555 ceu_write(pcdev, CRCNTR, 0);
556 ceu_write(pcdev, CRCMPR, 0);
557
Magnus Damm8be65dc2008-12-18 12:38:06 -0300558 value = 0x00000010; /* data fetch by default */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300559 pcdev->image_mode = yuv_lineskip = 0;
Magnus Damm8be65dc2008-12-18 12:38:06 -0300560
561 switch (icd->current_fmt->fourcc) {
562 case V4L2_PIX_FMT_NV12:
563 case V4L2_PIX_FMT_NV21:
564 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300565 /* fall-through */
566 case V4L2_PIX_FMT_NV16:
567 case V4L2_PIX_FMT_NV61:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300568 pcdev->image_mode = 1;
569 switch (cam->camera_fmt->fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300570 case V4L2_PIX_FMT_UYVY:
571 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
572 break;
573 case V4L2_PIX_FMT_VYUY:
574 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
575 break;
576 case V4L2_PIX_FMT_YUYV:
577 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
578 break;
579 case V4L2_PIX_FMT_YVYU:
580 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
581 break;
582 default:
583 BUG();
584 }
585 }
586
Magnus Damm7a1a8162008-12-18 12:50:30 -0300587 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
588 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300589 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300590
Magnus Damm7a1a8162008-12-18 12:50:30 -0300591 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
592 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300593 value |= pcdev->is_16bit ? 1 << 12 : 0;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300594 ceu_write(pcdev, CAMCR, value);
595
596 ceu_write(pcdev, CAPCR, 0x00300000);
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300597 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300598
599 mdelay(1);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300600 sh_mobile_ceu_set_rect(icd, &icd->rect_current);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300601
Magnus Damm8be65dc2008-12-18 12:38:06 -0300602 ceu_write(pcdev, CFLCR, 0); /* no scaling */
Magnus Dammdd542032008-10-16 19:50:22 -0300603
604 /* A few words about byte order (observed in Big Endian mode)
605 *
606 * In data fetch mode bytes are received in chunks of 8 bytes.
607 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
608 *
609 * The data is however by default written to memory in reverse order:
610 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
611 *
612 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300613 * using 7 we swap the data bytes to match the incoming order:
614 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300615 */
Magnus Damm8be65dc2008-12-18 12:38:06 -0300616 value = 0x00000017;
617 if (yuv_lineskip)
618 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
619
620 ceu_write(pcdev, CDOCR, value);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300621 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
622
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300623 dev_dbg(&icd->dev, "S_FMT successful for %c%c%c%c %ux%u@%u.%u\n",
624 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
625 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
626 icd->rect_current.width, icd->rect_current.height,
627 icd->rect_current.left, icd->rect_current.top);
628
629 capture_restore(pcdev, capsr);
630
Magnus Damm0d3244d2008-07-16 22:59:28 -0300631 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300632 return 0;
633}
634
Magnus Damm9414de32008-12-18 11:49:06 -0300635static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300636{
637 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
638 struct sh_mobile_ceu_dev *pcdev = ici->priv;
639 unsigned long camera_flags, common_flags;
640
641 camera_flags = icd->ops->query_bus_param(icd);
642 common_flags = soc_camera_bus_param_compatible(camera_flags,
Kuninori Morimotoc354b402009-02-23 12:12:58 -0300643 make_bus_param(pcdev));
Magnus Damm0d3244d2008-07-16 22:59:28 -0300644 if (!common_flags)
645 return -EINVAL;
646
647 return 0;
648}
649
Magnus Damm8be65dc2008-12-18 12:38:06 -0300650static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
651 {
652 .name = "NV12",
653 .depth = 12,
654 .fourcc = V4L2_PIX_FMT_NV12,
655 .colorspace = V4L2_COLORSPACE_JPEG,
656 },
657 {
658 .name = "NV21",
659 .depth = 12,
660 .fourcc = V4L2_PIX_FMT_NV21,
661 .colorspace = V4L2_COLORSPACE_JPEG,
662 },
Magnus Damm9e4a56d2008-12-18 12:45:33 -0300663 {
664 .name = "NV16",
665 .depth = 16,
666 .fourcc = V4L2_PIX_FMT_NV16,
667 .colorspace = V4L2_COLORSPACE_JPEG,
668 },
669 {
670 .name = "NV61",
671 .depth = 16,
672 .fourcc = V4L2_PIX_FMT_NV61,
673 .colorspace = V4L2_COLORSPACE_JPEG,
674 },
Magnus Damm8be65dc2008-12-18 12:38:06 -0300675};
676
Magnus Damm9414de32008-12-18 11:49:06 -0300677static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
678 struct soc_camera_format_xlate *xlate)
679{
680 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Magnus Damm8be65dc2008-12-18 12:38:06 -0300681 int ret, k, n;
Magnus Damm9414de32008-12-18 11:49:06 -0300682 int formats = 0;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300683 struct sh_mobile_ceu_cam *cam;
Magnus Damm9414de32008-12-18 11:49:06 -0300684
685 ret = sh_mobile_ceu_try_bus_param(icd);
686 if (ret < 0)
687 return 0;
688
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300689 if (!icd->host_priv) {
690 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
691 if (!cam)
692 return -ENOMEM;
693
694 icd->host_priv = cam;
695 } else {
696 cam = icd->host_priv;
697 }
698
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300699 /* Beginning of a pass */
700 if (!idx)
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300701 cam->extra_fmt = NULL;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300702
Magnus Damm9414de32008-12-18 11:49:06 -0300703 switch (icd->formats[idx].fourcc) {
Magnus Damm8be65dc2008-12-18 12:38:06 -0300704 case V4L2_PIX_FMT_UYVY:
705 case V4L2_PIX_FMT_VYUY:
706 case V4L2_PIX_FMT_YUYV:
707 case V4L2_PIX_FMT_YVYU:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300708 if (cam->extra_fmt)
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300709 goto add_single_format;
710
711 /*
712 * Our case is simple so far: for any of the above four camera
713 * formats we add all our four synthesized NV* formats, so,
714 * just marking the device with a single flag suffices. If
715 * the format generation rules are more complex, you would have
716 * to actually hang your already added / counted formats onto
717 * the host_priv pointer and check whether the format you're
718 * going to add now is already there.
719 */
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300720 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300721
Magnus Damm8be65dc2008-12-18 12:38:06 -0300722 n = ARRAY_SIZE(sh_mobile_ceu_formats);
723 formats += n;
724 for (k = 0; xlate && k < n; k++) {
725 xlate->host_fmt = &sh_mobile_ceu_formats[k];
726 xlate->cam_fmt = icd->formats + idx;
727 xlate->buswidth = icd->formats[idx].depth;
728 xlate++;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300729 dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n",
Magnus Damm8be65dc2008-12-18 12:38:06 -0300730 sh_mobile_ceu_formats[k].name,
731 icd->formats[idx].name);
732 }
Magnus Damm9414de32008-12-18 11:49:06 -0300733 default:
Guennadi Liakhovetskic8329ac2009-02-23 12:12:58 -0300734add_single_format:
Magnus Damm9414de32008-12-18 11:49:06 -0300735 /* Generic pass-through */
736 formats++;
737 if (xlate) {
738 xlate->host_fmt = icd->formats + idx;
739 xlate->cam_fmt = icd->formats + idx;
740 xlate->buswidth = icd->formats[idx].depth;
741 xlate++;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300742 dev_dbg(ici->v4l2_dev.dev,
Magnus Damm9414de32008-12-18 11:49:06 -0300743 "Providing format %s in pass-through mode\n",
744 icd->formats[idx].name);
745 }
746 }
747
748 return formats;
749}
750
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300751static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
752{
753 kfree(icd->host_priv);
754 icd->host_priv = NULL;
755}
756
757static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
758{
759 return r1->width < r2->width || r1->height < r2->height;
760}
761
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300762static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
763 struct v4l2_rect *rect)
764{
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300765 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
766 struct sh_mobile_ceu_dev *pcdev = ici->priv;
767 struct v4l2_rect cam_rect = *rect;
768 struct sh_mobile_ceu_cam *cam = icd->host_priv;
769 unsigned short width, height;
770 u32 capsr;
771 int ret;
772
773 capsr = capture_save_reset(pcdev);
774 dev_dbg(&icd->dev, "CAPSR %x\n", capsr);
775
776 ret = icd->ops->set_crop(icd, &cam_rect);
777 if (!ret && !memcmp(rect, &cam_rect, sizeof(*rect))) {
778 dev_dbg(&icd->dev, "Camera S_CROP successful for %ux%u@%u.%u\n",
779 cam_rect.width, cam_rect.height,
780 cam_rect.left, cam_rect.top);
781 goto ceu_set_rect;
782 }
783
784 /* Try to fix cropping, that camera hasn't managed to do */
785 dev_dbg(&icd->dev, "Fix camera S_CROP %d for %ux%u@%u.%u\n",
786 ret, cam_rect.width, cam_rect.height,
787 cam_rect.left, cam_rect.top);
788
789 /*
790 * Popular special case - some cameras can only handle fixed sizes like
791 * QVGA, VGA,... Take care to avoid infinite loop.
792 */
793 width = max(cam_rect.width, 1) * 2;
794 height = max(cam_rect.height, 1) * 2;
795 while (!ret && is_smaller(&cam_rect, rect) &&
796 (icd->rect_max.width >= width &&
797 icd->rect_max.height >= height)) {
798 cam_rect.width = width;
799 cam_rect.height = height;
800
801 if (cam_rect.width + cam_rect.left >
802 icd->rect_max.width + icd->rect_max.left)
803 cam_rect.left = icd->rect_max.width +
804 icd->rect_max.left - cam_rect.width;
805
806 if (cam_rect.height + cam_rect.top >
807 icd->rect_max.height + icd->rect_max.top)
808 cam_rect.top = icd->rect_max.height +
809 icd->rect_max.top - cam_rect.height;
810
811 ret = icd->ops->set_crop(icd, &cam_rect);
812 dev_dbg(&icd->dev, "Camera S_CROP %d for %ux%u@%u.%u\n",
813 ret, cam_rect.width, cam_rect.height,
814 cam_rect.left, cam_rect.top);
815 width *= 2;
816 height *= 2;
817 }
818
819 /*
820 * If the camera failed to configure cropping, it should not modify the
821 * rectangle
822 */
823 if ((ret < 0 && is_smaller(&icd->rect_current, rect)) ||
824 is_smaller(&cam_rect, rect)) {
825 /*
826 * The camera failed to configure a suitable cropping,
827 * we cannot use the current rectangle, set to max
828 */
829 cam_rect = icd->rect_max;
830 ret = icd->ops->set_crop(icd, &cam_rect);
831 dev_dbg(&icd->dev, "Camera S_CROP %d for max %ux%u@%u.%u\n",
832 ret, cam_rect.width, cam_rect.height,
833 cam_rect.left, cam_rect.top);
834 if (ret < 0)
835 /* All failed, hopefully resume current capture */
836 goto resume_capture;
837 }
838
839 /* We now have a rectangle, larger than requested, let's crop */
840
841 /*
842 * We have to preserve camera rectangle between close() / open(),
843 * because soc-camera core calls .set_fmt() on each first open() with
844 * last before last close() _user_ rectangle, which can be different
845 * from camera rectangle.
846 */
847 dev_dbg(&icd->dev, "SH S_CROP from %ux%u@%u.%u to %ux%u@%u.%u\n",
848 cam_rect.width, cam_rect.height, cam_rect.left, cam_rect.top,
849 rect->width, rect->height, rect->left, rect->top);
850
851 ret = 0;
852
853ceu_set_rect:
854 cam->camera_rect = cam_rect;
855
856 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
857 if (pcdev->active)
858 capsr |= 1;
859 sh_mobile_ceu_set_rect(icd, rect);
860 capture_restore(pcdev, capsr);
861
862resume_capture:
863
864 /* Even if only camera cropping succeeded */
865 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300866}
867
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300868static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300869 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300870{
Magnus Damm9414de32008-12-18 11:49:06 -0300871 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300872 struct sh_mobile_ceu_cam *cam = icd->host_priv;
873 struct v4l2_pix_format *pix = &f->fmt.pix;
874 __u32 pixfmt = pix->pixelformat;
Magnus Damm9414de32008-12-18 11:49:06 -0300875 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300876 int ret;
877
Magnus Damm9414de32008-12-18 11:49:06 -0300878 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
879 if (!xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300880 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
Magnus Damm9414de32008-12-18 11:49:06 -0300881 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300882 }
883
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300884 pix->pixelformat = xlate->cam_fmt->fourcc;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300885 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300886 pix->pixelformat = pixfmt;
Guennadi Liakhovetski0ad675e2009-02-23 12:11:25 -0300887 if (!ret) {
Magnus Damm9414de32008-12-18 11:49:06 -0300888 icd->buswidth = xlate->buswidth;
889 icd->current_fmt = xlate->host_fmt;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300890 cam->camera_fmt = xlate->cam_fmt;
891 cam->camera_rect.width = pix->width;
892 cam->camera_rect.height = pix->height;
893 cam->camera_rect.left = icd->rect_current.left;
894 cam->camera_rect.top = icd->rect_current.top;
Magnus Damm9414de32008-12-18 11:49:06 -0300895 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300896
897 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300898}
899
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300900static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
901 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300902{
Magnus Damm9414de32008-12-18 11:49:06 -0300903 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300904 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm9414de32008-12-18 11:49:06 -0300905 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300906 struct v4l2_pix_format *pix = &f->fmt.pix;
907 __u32 pixfmt = pix->pixelformat;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300908 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -0300909
Magnus Damm9414de32008-12-18 11:49:06 -0300910 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
911 if (!xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300912 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300913 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -0300914 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300915
Magnus Damm0d3244d2008-07-16 22:59:28 -0300916 /* FIXME: calculate using depth and bus width */
917
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300918 v4l_bound_align_image(&pix->width, 2, 2560, 1,
919 &pix->height, 4, 1920, 2, 0);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300920
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300921 pix->bytesperline = pix->width *
Magnus Damm9414de32008-12-18 11:49:06 -0300922 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300923 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300924
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300925 pix->pixelformat = xlate->cam_fmt->fourcc;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300926
Magnus Damm0d3244d2008-07-16 22:59:28 -0300927 /* limit to sensor capabilities */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300928 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f);
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300929 pix->pixelformat = pixfmt;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300930 if (ret < 0)
931 return ret;
932
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300933 switch (pix->field) {
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300934 case V4L2_FIELD_INTERLACED:
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300935 pcdev->is_interlaced = 1;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300936 break;
937 case V4L2_FIELD_ANY:
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -0300938 pix->field = V4L2_FIELD_NONE;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300939 /* fall-through */
940 case V4L2_FIELD_NONE:
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -0300941 pcdev->is_interlaced = 0;
Kuninori Morimotoccab8a22008-12-18 12:51:21 -0300942 break;
943 default:
944 ret = -EINVAL;
945 break;
946 }
947
948 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300949}
950
951static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
952 struct v4l2_requestbuffers *p)
953{
954 int i;
955
956 /* This is for locking debugging only. I removed spinlocks and now I
957 * check whether .prepare is ever called on a linked buffer, or whether
958 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
959 * it hadn't triggered */
960 for (i = 0; i < p->count; i++) {
961 struct sh_mobile_ceu_buffer *buf;
962
963 buf = container_of(icf->vb_vidq.bufs[i],
964 struct sh_mobile_ceu_buffer, vb);
965 INIT_LIST_HEAD(&buf->vb.queue);
966 }
967
968 return 0;
969}
970
971static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
972{
973 struct soc_camera_file *icf = file->private_data;
974 struct sh_mobile_ceu_buffer *buf;
975
976 buf = list_entry(icf->vb_vidq.stream.next,
977 struct sh_mobile_ceu_buffer, vb.stream);
978
979 poll_wait(file, &buf->vb.done, pt);
980
981 if (buf->vb.state == VIDEOBUF_DONE ||
982 buf->vb.state == VIDEOBUF_ERROR)
983 return POLLIN|POLLRDNORM;
984
985 return 0;
986}
987
988static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
989 struct v4l2_capability *cap)
990{
991 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
992 cap->version = KERNEL_VERSION(0, 0, 5);
993 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
994 return 0;
995}
996
997static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
998 struct soc_camera_device *icd)
999{
1000 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1001 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1002
1003 videobuf_queue_dma_contig_init(q,
1004 &sh_mobile_ceu_videobuf_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001005 ici->v4l2_dev.dev, &pcdev->lock,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001006 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Guennadi Liakhovetskie8029672009-03-13 06:08:20 -03001007 pcdev->is_interlaced ?
1008 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001009 sizeof(struct sh_mobile_ceu_buffer),
1010 icd);
1011}
1012
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001013static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1014 struct v4l2_control *ctrl)
1015{
1016 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1017 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1018 u32 val;
1019
1020 switch (ctrl->id) {
1021 case V4L2_CID_SHARPNESS:
1022 val = ceu_read(pcdev, CLFCR);
1023 ctrl->value = val ^ 1;
1024 return 0;
1025 }
1026 return -ENOIOCTLCMD;
1027}
1028
1029static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1030 struct v4l2_control *ctrl)
1031{
1032 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1033 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1034
1035 switch (ctrl->id) {
1036 case V4L2_CID_SHARPNESS:
1037 switch (icd->current_fmt->fourcc) {
1038 case V4L2_PIX_FMT_NV12:
1039 case V4L2_PIX_FMT_NV21:
1040 case V4L2_PIX_FMT_NV16:
1041 case V4L2_PIX_FMT_NV61:
1042 ceu_write(pcdev, CLFCR, !ctrl->value);
1043 return 0;
1044 }
1045 return -EINVAL;
1046 }
1047 return -ENOIOCTLCMD;
1048}
1049
1050static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1051 {
1052 .id = V4L2_CID_SHARPNESS,
1053 .type = V4L2_CTRL_TYPE_BOOLEAN,
1054 .name = "Low-pass filter",
1055 .minimum = 0,
1056 .maximum = 1,
1057 .step = 1,
1058 .default_value = 0,
1059 },
1060};
1061
Magnus Damm0d3244d2008-07-16 22:59:28 -03001062static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1063 .owner = THIS_MODULE,
1064 .add = sh_mobile_ceu_add_device,
1065 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -03001066 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetski904078f2009-08-25 11:46:52 -03001067 .put_formats = sh_mobile_ceu_put_formats,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001068 .set_crop = sh_mobile_ceu_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001069 .set_fmt = sh_mobile_ceu_set_fmt,
1070 .try_fmt = sh_mobile_ceu_try_fmt,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001071 .set_ctrl = sh_mobile_ceu_set_ctrl,
1072 .get_ctrl = sh_mobile_ceu_get_ctrl,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001073 .reqbufs = sh_mobile_ceu_reqbufs,
1074 .poll = sh_mobile_ceu_poll,
1075 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001076 .set_bus_param = sh_mobile_ceu_set_bus_param,
1077 .init_videobuf = sh_mobile_ceu_init_videobuf,
Guennadi Liakhovetski4a6110b2009-08-25 11:44:15 -03001078 .controls = sh_mobile_ceu_controls,
1079 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001080};
1081
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001082static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001083{
1084 struct sh_mobile_ceu_dev *pcdev;
1085 struct resource *res;
1086 void __iomem *base;
1087 unsigned int irq;
1088 int err = 0;
1089
1090 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1091 irq = platform_get_irq(pdev, 0);
1092 if (!res || !irq) {
1093 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1094 err = -ENODEV;
1095 goto exit;
1096 }
1097
1098 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1099 if (!pcdev) {
1100 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1101 err = -ENOMEM;
1102 goto exit;
1103 }
1104
Magnus Damm0d3244d2008-07-16 22:59:28 -03001105 INIT_LIST_HEAD(&pcdev->capture);
1106 spin_lock_init(&pcdev->lock);
1107
1108 pcdev->pdata = pdev->dev.platform_data;
1109 if (!pcdev->pdata) {
1110 err = -EINVAL;
1111 dev_err(&pdev->dev, "CEU platform data not set.\n");
1112 goto exit_kfree;
1113 }
1114
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001115 base = ioremap_nocache(res->start, resource_size(res));
Magnus Damm0d3244d2008-07-16 22:59:28 -03001116 if (!base) {
1117 err = -ENXIO;
1118 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1119 goto exit_kfree;
1120 }
1121
1122 pcdev->irq = irq;
1123 pcdev->base = base;
1124 pcdev->video_limit = 0; /* only enabled if second resource exists */
Magnus Damm0d3244d2008-07-16 22:59:28 -03001125
1126 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1127 if (res) {
1128 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1129 res->start,
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001130 resource_size(res),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001131 DMA_MEMORY_MAP |
1132 DMA_MEMORY_EXCLUSIVE);
1133 if (!err) {
1134 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1135 err = -ENXIO;
1136 goto exit_iounmap;
1137 }
1138
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001139 pcdev->video_limit = resource_size(res);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001140 }
1141
1142 /* request irq */
1143 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -03001144 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001145 if (err) {
1146 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1147 goto exit_release_mem;
1148 }
1149
Magnus Damm6d1386c2009-08-14 10:49:17 +00001150 pm_suspend_ignore_children(&pdev->dev, true);
1151 pm_runtime_enable(&pdev->dev);
1152 pm_runtime_resume(&pdev->dev);
Magnus Damma42b6dd2008-10-31 20:21:44 +09001153
Magnus Damm0d3244d2008-07-16 22:59:28 -03001154 pcdev->ici.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001155 pcdev->ici.v4l2_dev.dev = &pdev->dev;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001156 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -03001157 pcdev->ici.drv_name = dev_name(&pdev->dev);
1158 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001159
1160 err = soc_camera_host_register(&pcdev->ici);
1161 if (err)
Magnus Damm6d1386c2009-08-14 10:49:17 +00001162 goto exit_free_irq;
Magnus Damm0d3244d2008-07-16 22:59:28 -03001163
1164 return 0;
1165
1166exit_free_irq:
1167 free_irq(pcdev->irq, pcdev);
1168exit_release_mem:
1169 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1170 dma_release_declared_memory(&pdev->dev);
1171exit_iounmap:
1172 iounmap(base);
1173exit_kfree:
1174 kfree(pcdev);
1175exit:
1176 return err;
1177}
1178
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001179static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
Magnus Damm0d3244d2008-07-16 22:59:28 -03001180{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001181 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1182 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1183 struct sh_mobile_ceu_dev, ici);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001184
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001185 soc_camera_host_unregister(soc_host);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001186 free_irq(pcdev->irq, pcdev);
1187 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1188 dma_release_declared_memory(&pdev->dev);
1189 iounmap(pcdev->base);
1190 kfree(pcdev);
1191 return 0;
1192}
1193
Magnus Damm6d1386c2009-08-14 10:49:17 +00001194static int sh_mobile_ceu_runtime_nop(struct device *dev)
1195{
1196 /* Runtime PM callback shared between ->runtime_suspend()
1197 * and ->runtime_resume(). Simply returns success.
1198 *
1199 * This driver re-initializes all registers after
1200 * pm_runtime_get_sync() anyway so there is no need
1201 * to save and restore registers here.
1202 */
1203 return 0;
1204}
1205
1206static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1207 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1208 .runtime_resume = sh_mobile_ceu_runtime_nop,
1209};
1210
Magnus Damm0d3244d2008-07-16 22:59:28 -03001211static struct platform_driver sh_mobile_ceu_driver = {
1212 .driver = {
1213 .name = "sh_mobile_ceu",
Magnus Damm6d1386c2009-08-14 10:49:17 +00001214 .pm = &sh_mobile_ceu_dev_pm_ops,
Magnus Damm0d3244d2008-07-16 22:59:28 -03001215 },
1216 .probe = sh_mobile_ceu_probe,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001217 .remove = __exit_p(sh_mobile_ceu_remove),
Magnus Damm0d3244d2008-07-16 22:59:28 -03001218};
1219
1220static int __init sh_mobile_ceu_init(void)
1221{
1222 return platform_driver_register(&sh_mobile_ceu_driver);
1223}
1224
1225static void __exit sh_mobile_ceu_exit(void)
1226{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001227 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -03001228}
1229
1230module_init(sh_mobile_ceu_init);
1231module_exit(sh_mobile_ceu_exit);
1232
1233MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1234MODULE_AUTHOR("Magnus Damm");
1235MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001236MODULE_ALIAS("platform:sh_mobile_ceu");