blob: 47ffa441c86925c418805f3a3d902fa640425422 [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 Damma42b6dd2008-10-31 20:21:44 +090033#include <linux/clk.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 {
84 struct device *dev;
85 struct soc_camera_host ici;
86 struct soc_camera_device *icd;
87
88 unsigned int irq;
89 void __iomem *base;
Magnus Damma42b6dd2008-10-31 20:21:44 +090090 struct clk *clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -030091 unsigned long video_limit;
92
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -030093 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -030094 spinlock_t lock;
95 struct list_head capture;
96 struct videobuf_buffer *active;
97
98 struct sh_mobile_ceu_info *pdata;
99};
100
101static void ceu_write(struct sh_mobile_ceu_dev *priv,
102 unsigned long reg_offs, unsigned long data)
103{
104 iowrite32(data, priv->base + reg_offs);
105}
106
107static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
108 unsigned long reg_offs)
109{
110 return ioread32(priv->base + reg_offs);
111}
112
113/*
114 * Videobuf operations
115 */
116static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
117 unsigned int *count,
118 unsigned int *size)
119{
120 struct soc_camera_device *icd = vq->priv_data;
121 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
122 struct sh_mobile_ceu_dev *pcdev = ici->priv;
123 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
124
125 *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
126
127 if (0 == *count)
128 *count = 2;
129
130 if (pcdev->video_limit) {
131 while (*size * *count > pcdev->video_limit)
132 (*count)--;
133 }
134
135 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
136
137 return 0;
138}
139
140static void free_buffer(struct videobuf_queue *vq,
141 struct sh_mobile_ceu_buffer *buf)
142{
143 struct soc_camera_device *icd = vq->priv_data;
144
Hans Verkuil86751b02008-07-18 01:35:14 -0300145 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300146 &buf->vb, buf->vb.baddr, buf->vb.bsize);
147
148 if (in_interrupt())
149 BUG();
150
151 videobuf_dma_contig_free(vq, &buf->vb);
152 dev_dbg(&icd->dev, "%s freed\n", __func__);
153 buf->vb.state = VIDEOBUF_NEEDS_INIT;
154}
155
156static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
157{
158 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
159 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
160 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
161
162 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
163
164 ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
165
166 if (pcdev->active) {
Magnus Damm51354cc2008-10-16 19:51:20 -0300167 pcdev->active->state = VIDEOBUF_ACTIVE;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300168 ceu_write(pcdev, CDAYR, videobuf_to_dma_contig(pcdev->active));
169 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
170 }
171}
172
173static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
174 struct videobuf_buffer *vb,
175 enum v4l2_field field)
176{
177 struct soc_camera_device *icd = vq->priv_data;
178 struct sh_mobile_ceu_buffer *buf;
179 int ret;
180
181 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
182
Hans Verkuil86751b02008-07-18 01:35:14 -0300183 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300184 vb, vb->baddr, vb->bsize);
185
186 /* Added list head initialization on alloc */
187 WARN_ON(!list_empty(&vb->queue));
188
189#ifdef DEBUG
190 /* This can be useful if you want to see if we actually fill
191 * the buffer with something */
192 memset((void *)vb->baddr, 0xaa, vb->bsize);
193#endif
194
195 BUG_ON(NULL == icd->current_fmt);
196
197 if (buf->fmt != icd->current_fmt ||
198 vb->width != icd->width ||
199 vb->height != icd->height ||
200 vb->field != field) {
201 buf->fmt = icd->current_fmt;
202 vb->width = icd->width;
203 vb->height = icd->height;
204 vb->field = field;
205 vb->state = VIDEOBUF_NEEDS_INIT;
206 }
207
208 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
209 if (0 != vb->baddr && vb->bsize < vb->size) {
210 ret = -EINVAL;
211 goto out;
212 }
213
214 if (vb->state == VIDEOBUF_NEEDS_INIT) {
215 ret = videobuf_iolock(vq, vb, NULL);
216 if (ret)
217 goto fail;
218 vb->state = VIDEOBUF_PREPARED;
219 }
220
221 return 0;
222fail:
223 free_buffer(vq, buf);
224out:
225 return ret;
226}
227
228static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
229 struct videobuf_buffer *vb)
230{
231 struct soc_camera_device *icd = vq->priv_data;
232 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
233 struct sh_mobile_ceu_dev *pcdev = ici->priv;
234 unsigned long flags;
235
Hans Verkuil86751b02008-07-18 01:35:14 -0300236 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300237 vb, vb->baddr, vb->bsize);
238
Magnus Damm51354cc2008-10-16 19:51:20 -0300239 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300240 spin_lock_irqsave(&pcdev->lock, flags);
241 list_add_tail(&vb->queue, &pcdev->capture);
242
243 if (!pcdev->active) {
244 pcdev->active = vb;
245 sh_mobile_ceu_capture(pcdev);
246 }
247
248 spin_unlock_irqrestore(&pcdev->lock, flags);
249}
250
251static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
252 struct videobuf_buffer *vb)
253{
254 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
255}
256
257static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
258 .buf_setup = sh_mobile_ceu_videobuf_setup,
259 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
260 .buf_queue = sh_mobile_ceu_videobuf_queue,
261 .buf_release = sh_mobile_ceu_videobuf_release,
262};
263
264static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
265{
266 struct sh_mobile_ceu_dev *pcdev = data;
267 struct videobuf_buffer *vb;
268 unsigned long flags;
269
270 spin_lock_irqsave(&pcdev->lock, flags);
271
272 vb = pcdev->active;
273 list_del_init(&vb->queue);
274
275 if (!list_empty(&pcdev->capture))
276 pcdev->active = list_entry(pcdev->capture.next,
277 struct videobuf_buffer, queue);
278 else
279 pcdev->active = NULL;
280
281 sh_mobile_ceu_capture(pcdev);
282
283 vb->state = VIDEOBUF_DONE;
284 do_gettimeofday(&vb->ts);
285 vb->field_count++;
286 wake_up(&vb->done);
287 spin_unlock_irqrestore(&pcdev->lock, flags);
288
289 return IRQ_HANDLED;
290}
291
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300292/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300293static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
294{
295 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
296 struct sh_mobile_ceu_dev *pcdev = ici->priv;
297 int ret = -EBUSY;
298
Magnus Damm0d3244d2008-07-16 22:59:28 -0300299 if (pcdev->icd)
300 goto err;
301
302 dev_info(&icd->dev,
303 "SuperH Mobile CEU driver attached to camera %d\n",
304 icd->devnum);
305
Magnus Damm0d3244d2008-07-16 22:59:28 -0300306 ret = icd->ops->init(icd);
307 if (ret)
308 goto err;
309
Magnus Damma42b6dd2008-10-31 20:21:44 +0900310 clk_enable(pcdev->clk);
311
Magnus Damm0d3244d2008-07-16 22:59:28 -0300312 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
313 while (ceu_read(pcdev, CSTSR) & 1)
314 msleep(1);
315
316 pcdev->icd = icd;
317err:
Magnus Damm0d3244d2008-07-16 22:59:28 -0300318 return ret;
319}
320
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300321/* Called with .video_lock held */
Magnus Damm0d3244d2008-07-16 22:59:28 -0300322static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
323{
324 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
325 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300326 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300327
328 BUG_ON(icd != pcdev->icd);
329
330 /* disable capture, disable interrupts */
331 ceu_write(pcdev, CEIER, 0);
332 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
Magnus Damm51354cc2008-10-16 19:51:20 -0300333
334 /* make sure active buffer is canceled */
335 spin_lock_irqsave(&pcdev->lock, flags);
336 if (pcdev->active) {
337 list_del(&pcdev->active->queue);
338 pcdev->active->state = VIDEOBUF_ERROR;
339 wake_up_all(&pcdev->active->done);
340 pcdev->active = NULL;
341 }
342 spin_unlock_irqrestore(&pcdev->lock, flags);
343
Magnus Damma42b6dd2008-10-31 20:21:44 +0900344 clk_disable(pcdev->clk);
345
Magnus Damm0d3244d2008-07-16 22:59:28 -0300346 icd->ops->release(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300347
348 dev_info(&icd->dev,
349 "SuperH Mobile CEU driver detached from camera %d\n",
350 icd->devnum);
351
352 pcdev->icd = NULL;
353}
354
355static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
356 __u32 pixfmt)
357{
358 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
359 struct sh_mobile_ceu_dev *pcdev = ici->priv;
360 int ret, buswidth, width, cfszr_width, cdwdr_width;
361 unsigned long camera_flags, common_flags, value;
362
363 camera_flags = icd->ops->query_bus_param(icd);
364 common_flags = soc_camera_bus_param_compatible(camera_flags,
365 pcdev->pdata->flags);
366 if (!common_flags)
367 return -EINVAL;
368
369 ret = icd->ops->set_bus_param(icd, common_flags);
370 if (ret < 0)
371 return ret;
372
373 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
374 case SOCAM_DATAWIDTH_8:
375 buswidth = 8;
376 break;
377 case SOCAM_DATAWIDTH_16:
378 buswidth = 16;
379 break;
380 default:
381 return -EINVAL;
382 }
383
384 ceu_write(pcdev, CRCNTR, 0);
385 ceu_write(pcdev, CRCMPR, 0);
386
387 value = 0x00000010;
388 value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
389 value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
390 value |= (buswidth == 16) ? (1 << 12) : 0;
391 ceu_write(pcdev, CAMCR, value);
392
393 ceu_write(pcdev, CAPCR, 0x00300000);
394 ceu_write(pcdev, CAIFR, 0);
395
396 mdelay(1);
397
398 width = icd->width * (icd->current_fmt->depth / 8);
399 width = (buswidth == 16) ? width / 2 : width;
400 cfszr_width = (buswidth == 8) ? width / 2 : width;
401 cdwdr_width = (buswidth == 16) ? width * 2 : width;
402
403 ceu_write(pcdev, CAMOR, 0);
404 ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
405 ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */
406 ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
407 ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */
Magnus Dammdd542032008-10-16 19:50:22 -0300408
409 /* A few words about byte order (observed in Big Endian mode)
410 *
411 * In data fetch mode bytes are received in chunks of 8 bytes.
412 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
413 *
414 * The data is however by default written to memory in reverse order:
415 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
416 *
417 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300418 * using 7 we swap the data bytes to match the incoming order:
419 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300420 */
Magnus Damm2c0a0722008-10-16 19:50:56 -0300421 ceu_write(pcdev, CDOCR, 0x00000017);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300422
423 ceu_write(pcdev, CDWDR, cdwdr_width);
424 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
425
426 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
427 /* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
428
429 return 0;
430}
431
Magnus Damm9414de32008-12-18 11:49:06 -0300432static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300433{
434 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
435 struct sh_mobile_ceu_dev *pcdev = ici->priv;
436 unsigned long camera_flags, common_flags;
437
438 camera_flags = icd->ops->query_bus_param(icd);
439 common_flags = soc_camera_bus_param_compatible(camera_flags,
440 pcdev->pdata->flags);
441 if (!common_flags)
442 return -EINVAL;
443
444 return 0;
445}
446
Magnus Damm9414de32008-12-18 11:49:06 -0300447static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
448 struct soc_camera_format_xlate *xlate)
449{
450 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
451 int ret;
452 int formats = 0;
453
454 ret = sh_mobile_ceu_try_bus_param(icd);
455 if (ret < 0)
456 return 0;
457
458 switch (icd->formats[idx].fourcc) {
459 default:
460 /* Generic pass-through */
461 formats++;
462 if (xlate) {
463 xlate->host_fmt = icd->formats + idx;
464 xlate->cam_fmt = icd->formats + idx;
465 xlate->buswidth = icd->formats[idx].depth;
466 xlate++;
467 dev_dbg(&ici->dev,
468 "Providing format %s in pass-through mode\n",
469 icd->formats[idx].name);
470 }
471 }
472
473 return formats;
474}
475
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300476static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
477 __u32 pixfmt, struct v4l2_rect *rect)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300478{
Magnus Damm9414de32008-12-18 11:49:06 -0300479 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
480 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300481 int ret;
482
Magnus Damm9414de32008-12-18 11:49:06 -0300483 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
484 if (!xlate) {
485 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
486 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300487 }
488
Magnus Damm9414de32008-12-18 11:49:06 -0300489 switch (pixfmt) {
490 case 0: /* Only geometry change */
491 ret = icd->ops->set_fmt(icd, pixfmt, rect);
492 break;
493 default:
494 ret = icd->ops->set_fmt(icd, xlate->cam_fmt->fourcc, rect);
495 }
496
497 if (pixfmt && !ret) {
498 icd->buswidth = xlate->buswidth;
499 icd->current_fmt = xlate->host_fmt;
500 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300501
502 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300503}
504
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300505static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
506 struct v4l2_format *f)
Magnus Damm0d3244d2008-07-16 22:59:28 -0300507{
Magnus Damm9414de32008-12-18 11:49:06 -0300508 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
509 const struct soc_camera_format_xlate *xlate;
510 __u32 pixfmt = f->fmt.pix.pixelformat;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -0300511
Magnus Damm9414de32008-12-18 11:49:06 -0300512 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
513 if (!xlate) {
514 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300515 return -EINVAL;
Magnus Damm9414de32008-12-18 11:49:06 -0300516 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300517
Magnus Damm0d3244d2008-07-16 22:59:28 -0300518 /* FIXME: calculate using depth and bus width */
519
520 if (f->fmt.pix.height < 4)
521 f->fmt.pix.height = 4;
522 if (f->fmt.pix.height > 1920)
523 f->fmt.pix.height = 1920;
524 if (f->fmt.pix.width < 2)
525 f->fmt.pix.width = 2;
526 if (f->fmt.pix.width > 2560)
527 f->fmt.pix.width = 2560;
528 f->fmt.pix.width &= ~0x01;
529 f->fmt.pix.height &= ~0x03;
530
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300531 f->fmt.pix.bytesperline = f->fmt.pix.width *
Magnus Damm9414de32008-12-18 11:49:06 -0300532 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300533 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
534
Magnus Damm0d3244d2008-07-16 22:59:28 -0300535 /* limit to sensor capabilities */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300536 return icd->ops->try_fmt(icd, f);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300537}
538
539static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
540 struct v4l2_requestbuffers *p)
541{
542 int i;
543
544 /* This is for locking debugging only. I removed spinlocks and now I
545 * check whether .prepare is ever called on a linked buffer, or whether
546 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
547 * it hadn't triggered */
548 for (i = 0; i < p->count; i++) {
549 struct sh_mobile_ceu_buffer *buf;
550
551 buf = container_of(icf->vb_vidq.bufs[i],
552 struct sh_mobile_ceu_buffer, vb);
553 INIT_LIST_HEAD(&buf->vb.queue);
554 }
555
556 return 0;
557}
558
559static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
560{
561 struct soc_camera_file *icf = file->private_data;
562 struct sh_mobile_ceu_buffer *buf;
563
564 buf = list_entry(icf->vb_vidq.stream.next,
565 struct sh_mobile_ceu_buffer, vb.stream);
566
567 poll_wait(file, &buf->vb.done, pt);
568
569 if (buf->vb.state == VIDEOBUF_DONE ||
570 buf->vb.state == VIDEOBUF_ERROR)
571 return POLLIN|POLLRDNORM;
572
573 return 0;
574}
575
576static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
577 struct v4l2_capability *cap)
578{
579 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
580 cap->version = KERNEL_VERSION(0, 0, 5);
581 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
582 return 0;
583}
584
585static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
586 struct soc_camera_device *icd)
587{
588 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
589 struct sh_mobile_ceu_dev *pcdev = ici->priv;
590
591 videobuf_queue_dma_contig_init(q,
592 &sh_mobile_ceu_videobuf_ops,
593 &ici->dev, &pcdev->lock,
594 V4L2_BUF_TYPE_VIDEO_CAPTURE,
595 V4L2_FIELD_NONE,
596 sizeof(struct sh_mobile_ceu_buffer),
597 icd);
598}
599
600static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
601 .owner = THIS_MODULE,
602 .add = sh_mobile_ceu_add_device,
603 .remove = sh_mobile_ceu_remove_device,
Magnus Damm9414de32008-12-18 11:49:06 -0300604 .get_formats = sh_mobile_ceu_get_formats,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300605 .set_fmt = sh_mobile_ceu_set_fmt,
606 .try_fmt = sh_mobile_ceu_try_fmt,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300607 .reqbufs = sh_mobile_ceu_reqbufs,
608 .poll = sh_mobile_ceu_poll,
609 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300610 .set_bus_param = sh_mobile_ceu_set_bus_param,
611 .init_videobuf = sh_mobile_ceu_init_videobuf,
612};
613
614static int sh_mobile_ceu_probe(struct platform_device *pdev)
615{
616 struct sh_mobile_ceu_dev *pcdev;
617 struct resource *res;
618 void __iomem *base;
Magnus Damma42b6dd2008-10-31 20:21:44 +0900619 char clk_name[8];
Magnus Damm0d3244d2008-07-16 22:59:28 -0300620 unsigned int irq;
621 int err = 0;
622
623 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
624 irq = platform_get_irq(pdev, 0);
625 if (!res || !irq) {
626 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
627 err = -ENODEV;
628 goto exit;
629 }
630
631 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
632 if (!pcdev) {
633 dev_err(&pdev->dev, "Could not allocate pcdev\n");
634 err = -ENOMEM;
635 goto exit;
636 }
637
638 platform_set_drvdata(pdev, pcdev);
639 INIT_LIST_HEAD(&pcdev->capture);
640 spin_lock_init(&pcdev->lock);
641
642 pcdev->pdata = pdev->dev.platform_data;
643 if (!pcdev->pdata) {
644 err = -EINVAL;
645 dev_err(&pdev->dev, "CEU platform data not set.\n");
646 goto exit_kfree;
647 }
648
649 base = ioremap_nocache(res->start, res->end - res->start + 1);
650 if (!base) {
651 err = -ENXIO;
652 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
653 goto exit_kfree;
654 }
655
656 pcdev->irq = irq;
657 pcdev->base = base;
658 pcdev->video_limit = 0; /* only enabled if second resource exists */
659 pcdev->dev = &pdev->dev;
660
661 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
662 if (res) {
663 err = dma_declare_coherent_memory(&pdev->dev, res->start,
664 res->start,
665 (res->end - res->start) + 1,
666 DMA_MEMORY_MAP |
667 DMA_MEMORY_EXCLUSIVE);
668 if (!err) {
669 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
670 err = -ENXIO;
671 goto exit_iounmap;
672 }
673
674 pcdev->video_limit = (res->end - res->start) + 1;
675 }
676
677 /* request irq */
678 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -0300679 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300680 if (err) {
681 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
682 goto exit_release_mem;
683 }
684
Magnus Damma42b6dd2008-10-31 20:21:44 +0900685 snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
686 pcdev->clk = clk_get(&pdev->dev, clk_name);
687 if (IS_ERR(pcdev->clk)) {
688 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
689 err = PTR_ERR(pcdev->clk);
690 goto exit_free_irq;
691 }
692
Magnus Damm0d3244d2008-07-16 22:59:28 -0300693 pcdev->ici.priv = pcdev;
694 pcdev->ici.dev.parent = &pdev->dev;
695 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -0300696 pcdev->ici.drv_name = dev_name(&pdev->dev);
697 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300698
699 err = soc_camera_host_register(&pcdev->ici);
700 if (err)
Magnus Damma42b6dd2008-10-31 20:21:44 +0900701 goto exit_free_clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300702
703 return 0;
704
Magnus Damma42b6dd2008-10-31 20:21:44 +0900705exit_free_clk:
706 clk_put(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300707exit_free_irq:
708 free_irq(pcdev->irq, pcdev);
709exit_release_mem:
710 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
711 dma_release_declared_memory(&pdev->dev);
712exit_iounmap:
713 iounmap(base);
714exit_kfree:
715 kfree(pcdev);
716exit:
717 return err;
718}
719
720static int sh_mobile_ceu_remove(struct platform_device *pdev)
721{
722 struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
723
724 soc_camera_host_unregister(&pcdev->ici);
Magnus Damma42b6dd2008-10-31 20:21:44 +0900725 clk_put(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300726 free_irq(pcdev->irq, pcdev);
727 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
728 dma_release_declared_memory(&pdev->dev);
729 iounmap(pcdev->base);
730 kfree(pcdev);
731 return 0;
732}
733
734static struct platform_driver sh_mobile_ceu_driver = {
735 .driver = {
736 .name = "sh_mobile_ceu",
737 },
738 .probe = sh_mobile_ceu_probe,
739 .remove = sh_mobile_ceu_remove,
740};
741
742static int __init sh_mobile_ceu_init(void)
743{
744 return platform_driver_register(&sh_mobile_ceu_driver);
745}
746
747static void __exit sh_mobile_ceu_exit(void)
748{
Paul Mundt01c1e4c2008-08-01 19:48:51 -0300749 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300750}
751
752module_init(sh_mobile_ceu_init);
753module_exit(sh_mobile_ceu_exit);
754
755MODULE_DESCRIPTION("SuperH Mobile CEU driver");
756MODULE_AUTHOR("Magnus Damm");
757MODULE_LICENSE("GPL");