blob: 02f846d1908bd782f07181e4310ce8fe4109e075 [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>
32#include <linux/mutex.h>
33#include <linux/videodev2.h>
Magnus Damma42b6dd2008-10-31 20:21:44 +090034#include <linux/clk.h>
Magnus Damm0d3244d2008-07-16 22:59:28 -030035
36#include <media/v4l2-common.h>
37#include <media/v4l2-dev.h>
38#include <media/soc_camera.h>
39#include <media/sh_mobile_ceu.h>
40#include <media/videobuf-dma-contig.h>
41
42/* register offsets for sh7722 / sh7723 */
43
Magnus Dammdd542032008-10-16 19:50:22 -030044#define CAPSR 0x00 /* Capture start register */
45#define CAPCR 0x04 /* Capture control register */
46#define CAMCR 0x08 /* Capture interface control register */
47#define CMCYR 0x0c /* Capture interface cycle register */
48#define CAMOR 0x10 /* Capture interface offset register */
49#define CAPWR 0x14 /* Capture interface width register */
50#define CAIFR 0x18 /* Capture interface input format register */
51#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
52#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
53#define CRCNTR 0x28 /* CEU register control register */
54#define CRCMPR 0x2c /* CEU register forcible control register */
55#define CFLCR 0x30 /* Capture filter control register */
56#define CFSZR 0x34 /* Capture filter size clip register */
57#define CDWDR 0x38 /* Capture destination width register */
58#define CDAYR 0x3c /* Capture data address Y register */
59#define CDACR 0x40 /* Capture data address C register */
60#define CDBYR 0x44 /* Capture data bottom-field address Y register */
61#define CDBCR 0x48 /* Capture data bottom-field address C register */
62#define CBDSR 0x4c /* Capture bundle destination size register */
63#define CFWCR 0x5c /* Firewall operation control register */
64#define CLFCR 0x60 /* Capture low-pass filter control register */
65#define CDOCR 0x64 /* Capture data output control register */
66#define CDDCR 0x68 /* Capture data complexity level register */
67#define CDDAR 0x6c /* Capture data complexity level address register */
68#define CEIER 0x70 /* Capture event interrupt enable register */
69#define CETCR 0x74 /* Capture event flag clear register */
70#define CSTSR 0x7c /* Capture status register */
71#define CSRTR 0x80 /* Capture software reset register */
72#define CDSSR 0x84 /* Capture data size register */
73#define CDAYR2 0x90 /* Capture data address Y register 2 */
74#define CDACR2 0x94 /* Capture data address C register 2 */
75#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
76#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
Magnus Damm0d3244d2008-07-16 22:59:28 -030077
78static DEFINE_MUTEX(camera_lock);
79
80/* per video frame buffer */
81struct sh_mobile_ceu_buffer {
82 struct videobuf_buffer vb; /* v4l buffer must be first */
83 const struct soc_camera_data_format *fmt;
84};
85
86struct sh_mobile_ceu_dev {
87 struct device *dev;
88 struct soc_camera_host ici;
89 struct soc_camera_device *icd;
90
91 unsigned int irq;
92 void __iomem *base;
Magnus Damma42b6dd2008-10-31 20:21:44 +090093 struct clk *clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -030094 unsigned long video_limit;
95
Guennadi Liakhovetskic60f2b52008-07-17 17:30:47 -030096 /* lock used to protect videobuf */
Magnus Damm0d3244d2008-07-16 22:59:28 -030097 spinlock_t lock;
98 struct list_head capture;
99 struct videobuf_buffer *active;
100
101 struct sh_mobile_ceu_info *pdata;
102};
103
104static void ceu_write(struct sh_mobile_ceu_dev *priv,
105 unsigned long reg_offs, unsigned long data)
106{
107 iowrite32(data, priv->base + reg_offs);
108}
109
110static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
111 unsigned long reg_offs)
112{
113 return ioread32(priv->base + reg_offs);
114}
115
116/*
117 * Videobuf operations
118 */
119static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
120 unsigned int *count,
121 unsigned int *size)
122{
123 struct soc_camera_device *icd = vq->priv_data;
124 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
125 struct sh_mobile_ceu_dev *pcdev = ici->priv;
126 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
127
128 *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
129
130 if (0 == *count)
131 *count = 2;
132
133 if (pcdev->video_limit) {
134 while (*size * *count > pcdev->video_limit)
135 (*count)--;
136 }
137
138 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
139
140 return 0;
141}
142
143static void free_buffer(struct videobuf_queue *vq,
144 struct sh_mobile_ceu_buffer *buf)
145{
146 struct soc_camera_device *icd = vq->priv_data;
147
Hans Verkuil86751b02008-07-18 01:35:14 -0300148 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300149 &buf->vb, buf->vb.baddr, buf->vb.bsize);
150
151 if (in_interrupt())
152 BUG();
153
154 videobuf_dma_contig_free(vq, &buf->vb);
155 dev_dbg(&icd->dev, "%s freed\n", __func__);
156 buf->vb.state = VIDEOBUF_NEEDS_INIT;
157}
158
159static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
160{
161 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
162 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
163 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
164
165 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
166
167 ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
168
169 if (pcdev->active) {
Magnus Damm51354cc2008-10-16 19:51:20 -0300170 pcdev->active->state = VIDEOBUF_ACTIVE;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300171 ceu_write(pcdev, CDAYR, videobuf_to_dma_contig(pcdev->active));
172 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
173 }
174}
175
176static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
177 struct videobuf_buffer *vb,
178 enum v4l2_field field)
179{
180 struct soc_camera_device *icd = vq->priv_data;
181 struct sh_mobile_ceu_buffer *buf;
182 int ret;
183
184 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
185
Hans Verkuil86751b02008-07-18 01:35:14 -0300186 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300187 vb, vb->baddr, vb->bsize);
188
189 /* Added list head initialization on alloc */
190 WARN_ON(!list_empty(&vb->queue));
191
192#ifdef DEBUG
193 /* This can be useful if you want to see if we actually fill
194 * the buffer with something */
195 memset((void *)vb->baddr, 0xaa, vb->bsize);
196#endif
197
198 BUG_ON(NULL == icd->current_fmt);
199
200 if (buf->fmt != icd->current_fmt ||
201 vb->width != icd->width ||
202 vb->height != icd->height ||
203 vb->field != field) {
204 buf->fmt = icd->current_fmt;
205 vb->width = icd->width;
206 vb->height = icd->height;
207 vb->field = field;
208 vb->state = VIDEOBUF_NEEDS_INIT;
209 }
210
211 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
212 if (0 != vb->baddr && vb->bsize < vb->size) {
213 ret = -EINVAL;
214 goto out;
215 }
216
217 if (vb->state == VIDEOBUF_NEEDS_INIT) {
218 ret = videobuf_iolock(vq, vb, NULL);
219 if (ret)
220 goto fail;
221 vb->state = VIDEOBUF_PREPARED;
222 }
223
224 return 0;
225fail:
226 free_buffer(vq, buf);
227out:
228 return ret;
229}
230
231static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
232 struct videobuf_buffer *vb)
233{
234 struct soc_camera_device *icd = vq->priv_data;
235 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
236 struct sh_mobile_ceu_dev *pcdev = ici->priv;
237 unsigned long flags;
238
Hans Verkuil86751b02008-07-18 01:35:14 -0300239 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300240 vb, vb->baddr, vb->bsize);
241
Magnus Damm51354cc2008-10-16 19:51:20 -0300242 vb->state = VIDEOBUF_QUEUED;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300243 spin_lock_irqsave(&pcdev->lock, flags);
244 list_add_tail(&vb->queue, &pcdev->capture);
245
246 if (!pcdev->active) {
247 pcdev->active = vb;
248 sh_mobile_ceu_capture(pcdev);
249 }
250
251 spin_unlock_irqrestore(&pcdev->lock, flags);
252}
253
254static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
255 struct videobuf_buffer *vb)
256{
257 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
258}
259
260static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
261 .buf_setup = sh_mobile_ceu_videobuf_setup,
262 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
263 .buf_queue = sh_mobile_ceu_videobuf_queue,
264 .buf_release = sh_mobile_ceu_videobuf_release,
265};
266
267static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
268{
269 struct sh_mobile_ceu_dev *pcdev = data;
270 struct videobuf_buffer *vb;
271 unsigned long flags;
272
273 spin_lock_irqsave(&pcdev->lock, flags);
274
275 vb = pcdev->active;
276 list_del_init(&vb->queue);
277
278 if (!list_empty(&pcdev->capture))
279 pcdev->active = list_entry(pcdev->capture.next,
280 struct videobuf_buffer, queue);
281 else
282 pcdev->active = NULL;
283
284 sh_mobile_ceu_capture(pcdev);
285
286 vb->state = VIDEOBUF_DONE;
287 do_gettimeofday(&vb->ts);
288 vb->field_count++;
289 wake_up(&vb->done);
290 spin_unlock_irqrestore(&pcdev->lock, flags);
291
292 return IRQ_HANDLED;
293}
294
295static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
296{
297 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
298 struct sh_mobile_ceu_dev *pcdev = ici->priv;
299 int ret = -EBUSY;
300
301 mutex_lock(&camera_lock);
302
303 if (pcdev->icd)
304 goto err;
305
306 dev_info(&icd->dev,
307 "SuperH Mobile CEU driver attached to camera %d\n",
308 icd->devnum);
309
Magnus Damm0d3244d2008-07-16 22:59:28 -0300310 ret = icd->ops->init(icd);
311 if (ret)
312 goto err;
313
Magnus Damma42b6dd2008-10-31 20:21:44 +0900314 clk_enable(pcdev->clk);
315
Magnus Damm0d3244d2008-07-16 22:59:28 -0300316 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
317 while (ceu_read(pcdev, CSTSR) & 1)
318 msleep(1);
319
320 pcdev->icd = icd;
321err:
322 mutex_unlock(&camera_lock);
323
324 return ret;
325}
326
327static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
328{
329 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
330 struct sh_mobile_ceu_dev *pcdev = ici->priv;
Magnus Damm51354cc2008-10-16 19:51:20 -0300331 unsigned long flags;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300332
333 BUG_ON(icd != pcdev->icd);
334
335 /* disable capture, disable interrupts */
336 ceu_write(pcdev, CEIER, 0);
337 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
Magnus Damm51354cc2008-10-16 19:51:20 -0300338
339 /* make sure active buffer is canceled */
340 spin_lock_irqsave(&pcdev->lock, flags);
341 if (pcdev->active) {
342 list_del(&pcdev->active->queue);
343 pcdev->active->state = VIDEOBUF_ERROR;
344 wake_up_all(&pcdev->active->done);
345 pcdev->active = NULL;
346 }
347 spin_unlock_irqrestore(&pcdev->lock, flags);
348
Magnus Damma42b6dd2008-10-31 20:21:44 +0900349 clk_disable(pcdev->clk);
350
Magnus Damm0d3244d2008-07-16 22:59:28 -0300351 icd->ops->release(icd);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300352
353 dev_info(&icd->dev,
354 "SuperH Mobile CEU driver detached from camera %d\n",
355 icd->devnum);
356
357 pcdev->icd = NULL;
358}
359
360static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
361 __u32 pixfmt)
362{
363 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
364 struct sh_mobile_ceu_dev *pcdev = ici->priv;
365 int ret, buswidth, width, cfszr_width, cdwdr_width;
366 unsigned long camera_flags, common_flags, value;
367
368 camera_flags = icd->ops->query_bus_param(icd);
369 common_flags = soc_camera_bus_param_compatible(camera_flags,
370 pcdev->pdata->flags);
371 if (!common_flags)
372 return -EINVAL;
373
374 ret = icd->ops->set_bus_param(icd, common_flags);
375 if (ret < 0)
376 return ret;
377
378 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
379 case SOCAM_DATAWIDTH_8:
380 buswidth = 8;
381 break;
382 case SOCAM_DATAWIDTH_16:
383 buswidth = 16;
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 ceu_write(pcdev, CRCNTR, 0);
390 ceu_write(pcdev, CRCMPR, 0);
391
392 value = 0x00000010;
393 value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
394 value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
395 value |= (buswidth == 16) ? (1 << 12) : 0;
396 ceu_write(pcdev, CAMCR, value);
397
398 ceu_write(pcdev, CAPCR, 0x00300000);
399 ceu_write(pcdev, CAIFR, 0);
400
401 mdelay(1);
402
403 width = icd->width * (icd->current_fmt->depth / 8);
404 width = (buswidth == 16) ? width / 2 : width;
405 cfszr_width = (buswidth == 8) ? width / 2 : width;
406 cdwdr_width = (buswidth == 16) ? width * 2 : width;
407
408 ceu_write(pcdev, CAMOR, 0);
409 ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
410 ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */
411 ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
412 ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */
Magnus Dammdd542032008-10-16 19:50:22 -0300413
414 /* A few words about byte order (observed in Big Endian mode)
415 *
416 * In data fetch mode bytes are received in chunks of 8 bytes.
417 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
418 *
419 * The data is however by default written to memory in reverse order:
420 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
421 *
422 * The lowest three bits of CDOCR allows us to do swapping,
Magnus Damm2c0a0722008-10-16 19:50:56 -0300423 * using 7 we swap the data bytes to match the incoming order:
424 * D0, D1, D2, D3, D4, D5, D6, D7
Magnus Dammdd542032008-10-16 19:50:22 -0300425 */
Magnus Damm2c0a0722008-10-16 19:50:56 -0300426 ceu_write(pcdev, CDOCR, 0x00000017);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300427
428 ceu_write(pcdev, CDWDR, cdwdr_width);
429 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
430
431 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
432 /* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
433
434 return 0;
435}
436
437static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
438 __u32 pixfmt)
439{
440 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
441 struct sh_mobile_ceu_dev *pcdev = ici->priv;
442 unsigned long camera_flags, common_flags;
443
444 camera_flags = icd->ops->query_bus_param(icd);
445 common_flags = soc_camera_bus_param_compatible(camera_flags,
446 pcdev->pdata->flags);
447 if (!common_flags)
448 return -EINVAL;
449
450 return 0;
451}
452
453static int sh_mobile_ceu_set_fmt_cap(struct soc_camera_device *icd,
454 __u32 pixfmt, struct v4l2_rect *rect)
455{
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300456 const struct soc_camera_data_format *cam_fmt;
457 int ret;
458
459 /*
460 * TODO: find a suitable supported by the SoC output format, check
461 * whether the sensor supports one of acceptable input formats.
462 */
463 if (pixfmt) {
464 cam_fmt = soc_camera_format_by_fourcc(icd, pixfmt);
465 if (!cam_fmt)
466 return -EINVAL;
467 }
468
469 ret = icd->ops->set_fmt_cap(icd, pixfmt, rect);
470 if (pixfmt && !ret)
471 icd->current_fmt = cam_fmt;
472
473 return ret;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300474}
475
476static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device *icd,
477 struct v4l2_format *f)
478{
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300479 const struct soc_camera_data_format *cam_fmt;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -0300480 int ret = sh_mobile_ceu_try_bus_param(icd, f->fmt.pix.pixelformat);
481
482 if (ret < 0)
483 return ret;
484
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300485 /*
486 * TODO: find a suitable supported by the SoC output format, check
487 * whether the sensor supports one of acceptable input formats.
488 */
489 cam_fmt = soc_camera_format_by_fourcc(icd, f->fmt.pix.pixelformat);
490 if (!cam_fmt)
491 return -EINVAL;
492
Magnus Damm0d3244d2008-07-16 22:59:28 -0300493 /* FIXME: calculate using depth and bus width */
494
495 if (f->fmt.pix.height < 4)
496 f->fmt.pix.height = 4;
497 if (f->fmt.pix.height > 1920)
498 f->fmt.pix.height = 1920;
499 if (f->fmt.pix.width < 2)
500 f->fmt.pix.width = 2;
501 if (f->fmt.pix.width > 2560)
502 f->fmt.pix.width = 2560;
503 f->fmt.pix.width &= ~0x01;
504 f->fmt.pix.height &= ~0x03;
505
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300506 f->fmt.pix.bytesperline = f->fmt.pix.width *
507 DIV_ROUND_UP(cam_fmt->depth, 8);
508 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
509
Magnus Damm0d3244d2008-07-16 22:59:28 -0300510 /* limit to sensor capabilities */
511 return icd->ops->try_fmt_cap(icd, f);
512}
513
514static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
515 struct v4l2_requestbuffers *p)
516{
517 int i;
518
519 /* This is for locking debugging only. I removed spinlocks and now I
520 * check whether .prepare is ever called on a linked buffer, or whether
521 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
522 * it hadn't triggered */
523 for (i = 0; i < p->count; i++) {
524 struct sh_mobile_ceu_buffer *buf;
525
526 buf = container_of(icf->vb_vidq.bufs[i],
527 struct sh_mobile_ceu_buffer, vb);
528 INIT_LIST_HEAD(&buf->vb.queue);
529 }
530
531 return 0;
532}
533
534static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
535{
536 struct soc_camera_file *icf = file->private_data;
537 struct sh_mobile_ceu_buffer *buf;
538
539 buf = list_entry(icf->vb_vidq.stream.next,
540 struct sh_mobile_ceu_buffer, vb.stream);
541
542 poll_wait(file, &buf->vb.done, pt);
543
544 if (buf->vb.state == VIDEOBUF_DONE ||
545 buf->vb.state == VIDEOBUF_ERROR)
546 return POLLIN|POLLRDNORM;
547
548 return 0;
549}
550
551static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
552 struct v4l2_capability *cap)
553{
554 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
555 cap->version = KERNEL_VERSION(0, 0, 5);
556 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
557 return 0;
558}
559
560static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
561 struct soc_camera_device *icd)
562{
563 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
564 struct sh_mobile_ceu_dev *pcdev = ici->priv;
565
566 videobuf_queue_dma_contig_init(q,
567 &sh_mobile_ceu_videobuf_ops,
568 &ici->dev, &pcdev->lock,
569 V4L2_BUF_TYPE_VIDEO_CAPTURE,
570 V4L2_FIELD_NONE,
571 sizeof(struct sh_mobile_ceu_buffer),
572 icd);
573}
574
575static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
576 .owner = THIS_MODULE,
577 .add = sh_mobile_ceu_add_device,
578 .remove = sh_mobile_ceu_remove_device,
579 .set_fmt_cap = sh_mobile_ceu_set_fmt_cap,
580 .try_fmt_cap = sh_mobile_ceu_try_fmt_cap,
581 .reqbufs = sh_mobile_ceu_reqbufs,
582 .poll = sh_mobile_ceu_poll,
583 .querycap = sh_mobile_ceu_querycap,
Magnus Damm0d3244d2008-07-16 22:59:28 -0300584 .set_bus_param = sh_mobile_ceu_set_bus_param,
585 .init_videobuf = sh_mobile_ceu_init_videobuf,
586};
587
588static int sh_mobile_ceu_probe(struct platform_device *pdev)
589{
590 struct sh_mobile_ceu_dev *pcdev;
591 struct resource *res;
592 void __iomem *base;
Magnus Damma42b6dd2008-10-31 20:21:44 +0900593 char clk_name[8];
Magnus Damm0d3244d2008-07-16 22:59:28 -0300594 unsigned int irq;
595 int err = 0;
596
597 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
598 irq = platform_get_irq(pdev, 0);
599 if (!res || !irq) {
600 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
601 err = -ENODEV;
602 goto exit;
603 }
604
605 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
606 if (!pcdev) {
607 dev_err(&pdev->dev, "Could not allocate pcdev\n");
608 err = -ENOMEM;
609 goto exit;
610 }
611
612 platform_set_drvdata(pdev, pcdev);
613 INIT_LIST_HEAD(&pcdev->capture);
614 spin_lock_init(&pcdev->lock);
615
616 pcdev->pdata = pdev->dev.platform_data;
617 if (!pcdev->pdata) {
618 err = -EINVAL;
619 dev_err(&pdev->dev, "CEU platform data not set.\n");
620 goto exit_kfree;
621 }
622
623 base = ioremap_nocache(res->start, res->end - res->start + 1);
624 if (!base) {
625 err = -ENXIO;
626 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
627 goto exit_kfree;
628 }
629
630 pcdev->irq = irq;
631 pcdev->base = base;
632 pcdev->video_limit = 0; /* only enabled if second resource exists */
633 pcdev->dev = &pdev->dev;
634
635 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
636 if (res) {
637 err = dma_declare_coherent_memory(&pdev->dev, res->start,
638 res->start,
639 (res->end - res->start) + 1,
640 DMA_MEMORY_MAP |
641 DMA_MEMORY_EXCLUSIVE);
642 if (!err) {
643 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
644 err = -ENXIO;
645 goto exit_iounmap;
646 }
647
648 pcdev->video_limit = (res->end - res->start) + 1;
649 }
650
651 /* request irq */
652 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
Kay Sieversaf128a12008-10-30 00:51:46 -0300653 dev_name(&pdev->dev), pcdev);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300654 if (err) {
655 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
656 goto exit_release_mem;
657 }
658
Magnus Damma42b6dd2008-10-31 20:21:44 +0900659 snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
660 pcdev->clk = clk_get(&pdev->dev, clk_name);
661 if (IS_ERR(pcdev->clk)) {
662 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
663 err = PTR_ERR(pcdev->clk);
664 goto exit_free_irq;
665 }
666
Magnus Damm0d3244d2008-07-16 22:59:28 -0300667 pcdev->ici.priv = pcdev;
668 pcdev->ici.dev.parent = &pdev->dev;
669 pcdev->ici.nr = pdev->id;
Kay Sieversaf128a12008-10-30 00:51:46 -0300670 pcdev->ici.drv_name = dev_name(&pdev->dev);
671 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300672
673 err = soc_camera_host_register(&pcdev->ici);
674 if (err)
Magnus Damma42b6dd2008-10-31 20:21:44 +0900675 goto exit_free_clk;
Magnus Damm0d3244d2008-07-16 22:59:28 -0300676
677 return 0;
678
Magnus Damma42b6dd2008-10-31 20:21:44 +0900679exit_free_clk:
680 clk_put(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300681exit_free_irq:
682 free_irq(pcdev->irq, pcdev);
683exit_release_mem:
684 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
685 dma_release_declared_memory(&pdev->dev);
686exit_iounmap:
687 iounmap(base);
688exit_kfree:
689 kfree(pcdev);
690exit:
691 return err;
692}
693
694static int sh_mobile_ceu_remove(struct platform_device *pdev)
695{
696 struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
697
698 soc_camera_host_unregister(&pcdev->ici);
Magnus Damma42b6dd2008-10-31 20:21:44 +0900699 clk_put(pcdev->clk);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300700 free_irq(pcdev->irq, pcdev);
701 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
702 dma_release_declared_memory(&pdev->dev);
703 iounmap(pcdev->base);
704 kfree(pcdev);
705 return 0;
706}
707
708static struct platform_driver sh_mobile_ceu_driver = {
709 .driver = {
710 .name = "sh_mobile_ceu",
711 },
712 .probe = sh_mobile_ceu_probe,
713 .remove = sh_mobile_ceu_remove,
714};
715
716static int __init sh_mobile_ceu_init(void)
717{
718 return platform_driver_register(&sh_mobile_ceu_driver);
719}
720
721static void __exit sh_mobile_ceu_exit(void)
722{
Paul Mundt01c1e4c2008-08-01 19:48:51 -0300723 platform_driver_unregister(&sh_mobile_ceu_driver);
Magnus Damm0d3244d2008-07-16 22:59:28 -0300724}
725
726module_init(sh_mobile_ceu_init);
727module_exit(sh_mobile_ceu_exit);
728
729MODULE_DESCRIPTION("SuperH Mobile CEU driver");
730MODULE_AUTHOR("Magnus Damm");
731MODULE_LICENSE("GPL");