V4L/DVB (10084): sh_mobile_ceu: add NV12 and NV21 support
[safe/jmp/linux-2.6] / drivers / media / video / sh_mobile_ceu_camera.c
1 /*
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/videodev2.h>
33 #include <linux/clk.h>
34
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
43 #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 */
76
77 /* per video frame buffer */
78 struct sh_mobile_ceu_buffer {
79         struct videobuf_buffer vb; /* v4l buffer must be first */
80         const struct soc_camera_data_format *fmt;
81 };
82
83 struct 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;
90         struct clk *clk;
91         unsigned long video_limit;
92
93         /* lock used to protect videobuf */
94         spinlock_t lock;
95         struct list_head capture;
96         struct videobuf_buffer *active;
97
98         struct sh_mobile_ceu_info *pdata;
99
100         const struct soc_camera_data_format *camera_fmt;
101 };
102
103 static void ceu_write(struct sh_mobile_ceu_dev *priv,
104                       unsigned long reg_offs, unsigned long data)
105 {
106         iowrite32(data, priv->base + reg_offs);
107 }
108
109 static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
110                               unsigned long reg_offs)
111 {
112         return ioread32(priv->base + reg_offs);
113 }
114
115 /*
116  *  Videobuf operations
117  */
118 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
119                                         unsigned int *count,
120                                         unsigned int *size)
121 {
122         struct soc_camera_device *icd = vq->priv_data;
123         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
124         struct sh_mobile_ceu_dev *pcdev = ici->priv;
125         int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
126
127         *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
128
129         if (0 == *count)
130                 *count = 2;
131
132         if (pcdev->video_limit) {
133                 while (*size * *count > pcdev->video_limit)
134                         (*count)--;
135         }
136
137         dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
138
139         return 0;
140 }
141
142 static void free_buffer(struct videobuf_queue *vq,
143                         struct sh_mobile_ceu_buffer *buf)
144 {
145         struct soc_camera_device *icd = vq->priv_data;
146
147         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
148                 &buf->vb, buf->vb.baddr, buf->vb.bsize);
149
150         if (in_interrupt())
151                 BUG();
152
153         videobuf_dma_contig_free(vq, &buf->vb);
154         dev_dbg(&icd->dev, "%s freed\n", __func__);
155         buf->vb.state = VIDEOBUF_NEEDS_INIT;
156 }
157
158 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
159 {
160         struct soc_camera_device *icd = pcdev->icd;
161         unsigned long phys_addr;
162
163         ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
164         ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
165         ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
166
167         ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
168
169         ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
170
171         if (!pcdev->active)
172                 return;
173
174         phys_addr = videobuf_to_dma_contig(pcdev->active);
175         ceu_write(pcdev, CDAYR, phys_addr);
176
177         switch (icd->current_fmt->fourcc) {
178         case V4L2_PIX_FMT_NV12:
179         case V4L2_PIX_FMT_NV21:
180                 phys_addr += (icd->width * icd->height);
181                 ceu_write(pcdev, CDACR, phys_addr);
182         }
183
184         pcdev->active->state = VIDEOBUF_ACTIVE;
185         ceu_write(pcdev, CAPSR, 0x1); /* start capture */
186 }
187
188 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
189                                           struct videobuf_buffer *vb,
190                                           enum v4l2_field field)
191 {
192         struct soc_camera_device *icd = vq->priv_data;
193         struct sh_mobile_ceu_buffer *buf;
194         int ret;
195
196         buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
197
198         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
199                 vb, vb->baddr, vb->bsize);
200
201         /* Added list head initialization on alloc */
202         WARN_ON(!list_empty(&vb->queue));
203
204 #ifdef DEBUG
205         /* This can be useful if you want to see if we actually fill
206          * the buffer with something */
207         memset((void *)vb->baddr, 0xaa, vb->bsize);
208 #endif
209
210         BUG_ON(NULL == icd->current_fmt);
211
212         if (buf->fmt    != icd->current_fmt ||
213             vb->width   != icd->width ||
214             vb->height  != icd->height ||
215             vb->field   != field) {
216                 buf->fmt        = icd->current_fmt;
217                 vb->width       = icd->width;
218                 vb->height      = icd->height;
219                 vb->field       = field;
220                 vb->state       = VIDEOBUF_NEEDS_INIT;
221         }
222
223         vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
224         if (0 != vb->baddr && vb->bsize < vb->size) {
225                 ret = -EINVAL;
226                 goto out;
227         }
228
229         if (vb->state == VIDEOBUF_NEEDS_INIT) {
230                 ret = videobuf_iolock(vq, vb, NULL);
231                 if (ret)
232                         goto fail;
233                 vb->state = VIDEOBUF_PREPARED;
234         }
235
236         return 0;
237 fail:
238         free_buffer(vq, buf);
239 out:
240         return ret;
241 }
242
243 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
244                                          struct videobuf_buffer *vb)
245 {
246         struct soc_camera_device *icd = vq->priv_data;
247         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
248         struct sh_mobile_ceu_dev *pcdev = ici->priv;
249         unsigned long flags;
250
251         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
252                 vb, vb->baddr, vb->bsize);
253
254         vb->state = VIDEOBUF_QUEUED;
255         spin_lock_irqsave(&pcdev->lock, flags);
256         list_add_tail(&vb->queue, &pcdev->capture);
257
258         if (!pcdev->active) {
259                 pcdev->active = vb;
260                 sh_mobile_ceu_capture(pcdev);
261         }
262
263         spin_unlock_irqrestore(&pcdev->lock, flags);
264 }
265
266 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
267                                            struct videobuf_buffer *vb)
268 {
269         free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
270 }
271
272 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
273         .buf_setup      = sh_mobile_ceu_videobuf_setup,
274         .buf_prepare    = sh_mobile_ceu_videobuf_prepare,
275         .buf_queue      = sh_mobile_ceu_videobuf_queue,
276         .buf_release    = sh_mobile_ceu_videobuf_release,
277 };
278
279 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
280 {
281         struct sh_mobile_ceu_dev *pcdev = data;
282         struct videobuf_buffer *vb;
283         unsigned long flags;
284
285         spin_lock_irqsave(&pcdev->lock, flags);
286
287         vb = pcdev->active;
288         list_del_init(&vb->queue);
289
290         if (!list_empty(&pcdev->capture))
291                 pcdev->active = list_entry(pcdev->capture.next,
292                                            struct videobuf_buffer, queue);
293         else
294                 pcdev->active = NULL;
295
296         sh_mobile_ceu_capture(pcdev);
297
298         vb->state = VIDEOBUF_DONE;
299         do_gettimeofday(&vb->ts);
300         vb->field_count++;
301         wake_up(&vb->done);
302         spin_unlock_irqrestore(&pcdev->lock, flags);
303
304         return IRQ_HANDLED;
305 }
306
307 /* Called with .video_lock held */
308 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
309 {
310         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
311         struct sh_mobile_ceu_dev *pcdev = ici->priv;
312         int ret = -EBUSY;
313
314         if (pcdev->icd)
315                 goto err;
316
317         dev_info(&icd->dev,
318                  "SuperH Mobile CEU driver attached to camera %d\n",
319                  icd->devnum);
320
321         ret = icd->ops->init(icd);
322         if (ret)
323                 goto err;
324
325         clk_enable(pcdev->clk);
326
327         ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
328         while (ceu_read(pcdev, CSTSR) & 1)
329                 msleep(1);
330
331         pcdev->icd = icd;
332 err:
333         return ret;
334 }
335
336 /* Called with .video_lock held */
337 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
338 {
339         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
340         struct sh_mobile_ceu_dev *pcdev = ici->priv;
341         unsigned long flags;
342
343         BUG_ON(icd != pcdev->icd);
344
345         /* disable capture, disable interrupts */
346         ceu_write(pcdev, CEIER, 0);
347         ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
348
349         /* make sure active buffer is canceled */
350         spin_lock_irqsave(&pcdev->lock, flags);
351         if (pcdev->active) {
352                 list_del(&pcdev->active->queue);
353                 pcdev->active->state = VIDEOBUF_ERROR;
354                 wake_up_all(&pcdev->active->done);
355                 pcdev->active = NULL;
356         }
357         spin_unlock_irqrestore(&pcdev->lock, flags);
358
359         clk_disable(pcdev->clk);
360
361         icd->ops->release(icd);
362
363         dev_info(&icd->dev,
364                  "SuperH Mobile CEU driver detached from camera %d\n",
365                  icd->devnum);
366
367         pcdev->icd = NULL;
368 }
369
370 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
371                                        __u32 pixfmt)
372 {
373         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
374         struct sh_mobile_ceu_dev *pcdev = ici->priv;
375         int ret, buswidth, width, cfszr_width, cdwdr_width;
376         unsigned long camera_flags, common_flags, value;
377         int yuv_mode, yuv_lineskip;
378
379         camera_flags = icd->ops->query_bus_param(icd);
380         common_flags = soc_camera_bus_param_compatible(camera_flags,
381                                                        pcdev->pdata->flags);
382         if (!common_flags)
383                 return -EINVAL;
384
385         ret = icd->ops->set_bus_param(icd, common_flags);
386         if (ret < 0)
387                 return ret;
388
389         switch (common_flags & SOCAM_DATAWIDTH_MASK) {
390         case SOCAM_DATAWIDTH_8:
391                 buswidth = 8;
392                 break;
393         case SOCAM_DATAWIDTH_16:
394                 buswidth = 16;
395                 break;
396         default:
397                 return -EINVAL;
398         }
399
400         ceu_write(pcdev, CRCNTR, 0);
401         ceu_write(pcdev, CRCMPR, 0);
402
403         value = 0x00000010; /* data fetch by default */
404         yuv_mode = yuv_lineskip = 0;
405
406         switch (icd->current_fmt->fourcc) {
407         case V4L2_PIX_FMT_NV12:
408         case V4L2_PIX_FMT_NV21:
409                 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
410                 yuv_mode = 1;
411                 switch (pcdev->camera_fmt->fourcc) {
412                 case V4L2_PIX_FMT_UYVY:
413                         value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
414                         break;
415                 case V4L2_PIX_FMT_VYUY:
416                         value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
417                         break;
418                 case V4L2_PIX_FMT_YUYV:
419                         value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
420                         break;
421                 case V4L2_PIX_FMT_YVYU:
422                         value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
423                         break;
424                 default:
425                         BUG();
426                 }
427         }
428
429         if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21)
430                 value ^= 0x00000100; /* swap U, V to change from NV12->NV21 */
431
432         value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
433         value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
434         value |= (buswidth == 16) ? (1 << 12) : 0;
435         ceu_write(pcdev, CAMCR, value);
436
437         ceu_write(pcdev, CAPCR, 0x00300000);
438         ceu_write(pcdev, CAIFR, 0);
439
440         mdelay(1);
441
442         if (yuv_mode) {
443                 width = icd->width * 2;
444                 width = (buswidth == 16) ? width / 2 : width;
445                 cfszr_width = cdwdr_width = icd->width;
446         } else {
447                 width = icd->width * ((icd->current_fmt->depth + 7) >> 3);
448                 width = (buswidth == 16) ? width / 2 : width;
449                 cfszr_width = (buswidth == 8) ? width / 2 : width;
450                 cdwdr_width = (buswidth == 16) ? width * 2 : width;
451         }
452
453         ceu_write(pcdev, CAMOR, 0);
454         ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
455         ceu_write(pcdev, CFLCR, 0); /* no scaling */
456         ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
457         ceu_write(pcdev, CLFCR, 0); /* no lowpass filter */
458
459         /* A few words about byte order (observed in Big Endian mode)
460          *
461          * In data fetch mode bytes are received in chunks of 8 bytes.
462          * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
463          *
464          * The data is however by default written to memory in reverse order:
465          * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
466          *
467          * The lowest three bits of CDOCR allows us to do swapping,
468          * using 7 we swap the data bytes to match the incoming order:
469          * D0, D1, D2, D3, D4, D5, D6, D7
470          */
471         value = 0x00000017;
472         if (yuv_lineskip)
473                 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
474
475         ceu_write(pcdev, CDOCR, value);
476
477         ceu_write(pcdev, CDWDR, cdwdr_width);
478         ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
479
480         /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
481         return 0;
482 }
483
484 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
485 {
486         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
487         struct sh_mobile_ceu_dev *pcdev = ici->priv;
488         unsigned long camera_flags, common_flags;
489
490         camera_flags = icd->ops->query_bus_param(icd);
491         common_flags = soc_camera_bus_param_compatible(camera_flags,
492                                                        pcdev->pdata->flags);
493         if (!common_flags)
494                 return -EINVAL;
495
496         return 0;
497 }
498
499 static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
500         {
501                 .name           = "NV12",
502                 .depth          = 12,
503                 .fourcc         = V4L2_PIX_FMT_NV12,
504                 .colorspace     = V4L2_COLORSPACE_JPEG,
505         },
506         {
507                 .name           = "NV21",
508                 .depth          = 12,
509                 .fourcc         = V4L2_PIX_FMT_NV21,
510                 .colorspace     = V4L2_COLORSPACE_JPEG,
511         },
512 };
513
514 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
515                                      struct soc_camera_format_xlate *xlate)
516 {
517         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
518         int ret, k, n;
519         int formats = 0;
520
521         ret = sh_mobile_ceu_try_bus_param(icd);
522         if (ret < 0)
523                 return 0;
524
525         switch (icd->formats[idx].fourcc) {
526         case V4L2_PIX_FMT_UYVY:
527         case V4L2_PIX_FMT_VYUY:
528         case V4L2_PIX_FMT_YUYV:
529         case V4L2_PIX_FMT_YVYU:
530                 n = ARRAY_SIZE(sh_mobile_ceu_formats);
531                 formats += n;
532                 for (k = 0; xlate && k < n; k++) {
533                         xlate->host_fmt = &sh_mobile_ceu_formats[k];
534                         xlate->cam_fmt = icd->formats + idx;
535                         xlate->buswidth = icd->formats[idx].depth;
536                         xlate++;
537                         dev_dbg(&ici->dev, "Providing format %s using %s\n",
538                                 sh_mobile_ceu_formats[k].name,
539                                 icd->formats[idx].name);
540                 }
541         default:
542                 /* Generic pass-through */
543                 formats++;
544                 if (xlate) {
545                         xlate->host_fmt = icd->formats + idx;
546                         xlate->cam_fmt = icd->formats + idx;
547                         xlate->buswidth = icd->formats[idx].depth;
548                         xlate++;
549                         dev_dbg(&ici->dev,
550                                 "Providing format %s in pass-through mode\n",
551                                 icd->formats[idx].name);
552                 }
553         }
554
555         return formats;
556 }
557
558 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
559                                  __u32 pixfmt, struct v4l2_rect *rect)
560 {
561         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
562         struct sh_mobile_ceu_dev *pcdev = ici->priv;
563         const struct soc_camera_format_xlate *xlate;
564         int ret;
565
566         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
567         if (!xlate) {
568                 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
569                 return -EINVAL;
570         }
571
572         switch (pixfmt) {
573         case 0:                         /* Only geometry change */
574                 ret = icd->ops->set_fmt(icd, pixfmt, rect);
575                 break;
576         default:
577                 ret = icd->ops->set_fmt(icd, xlate->cam_fmt->fourcc, rect);
578         }
579
580         if (pixfmt && !ret) {
581                 icd->buswidth = xlate->buswidth;
582                 icd->current_fmt = xlate->host_fmt;
583                 pcdev->camera_fmt = xlate->cam_fmt;
584         }
585
586         return ret;
587 }
588
589 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
590                                  struct v4l2_format *f)
591 {
592         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
593         const struct soc_camera_format_xlate *xlate;
594         __u32 pixfmt = f->fmt.pix.pixelformat;
595
596         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
597         if (!xlate) {
598                 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
599                 return -EINVAL;
600         }
601
602         /* FIXME: calculate using depth and bus width */
603
604         if (f->fmt.pix.height < 4)
605                 f->fmt.pix.height = 4;
606         if (f->fmt.pix.height > 1920)
607                 f->fmt.pix.height = 1920;
608         if (f->fmt.pix.width < 2)
609                 f->fmt.pix.width = 2;
610         if (f->fmt.pix.width > 2560)
611                 f->fmt.pix.width = 2560;
612         f->fmt.pix.width &= ~0x01;
613         f->fmt.pix.height &= ~0x03;
614
615         f->fmt.pix.bytesperline = f->fmt.pix.width *
616                 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
617         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
618
619         /* limit to sensor capabilities */
620         return icd->ops->try_fmt(icd, f);
621 }
622
623 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
624                                  struct v4l2_requestbuffers *p)
625 {
626         int i;
627
628         /* This is for locking debugging only. I removed spinlocks and now I
629          * check whether .prepare is ever called on a linked buffer, or whether
630          * a dma IRQ can occur for an in-work or unlinked buffer. Until now
631          * it hadn't triggered */
632         for (i = 0; i < p->count; i++) {
633                 struct sh_mobile_ceu_buffer *buf;
634
635                 buf = container_of(icf->vb_vidq.bufs[i],
636                                    struct sh_mobile_ceu_buffer, vb);
637                 INIT_LIST_HEAD(&buf->vb.queue);
638         }
639
640         return 0;
641 }
642
643 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
644 {
645         struct soc_camera_file *icf = file->private_data;
646         struct sh_mobile_ceu_buffer *buf;
647
648         buf = list_entry(icf->vb_vidq.stream.next,
649                          struct sh_mobile_ceu_buffer, vb.stream);
650
651         poll_wait(file, &buf->vb.done, pt);
652
653         if (buf->vb.state == VIDEOBUF_DONE ||
654             buf->vb.state == VIDEOBUF_ERROR)
655                 return POLLIN|POLLRDNORM;
656
657         return 0;
658 }
659
660 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
661                                   struct v4l2_capability *cap)
662 {
663         strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
664         cap->version = KERNEL_VERSION(0, 0, 5);
665         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
666         return 0;
667 }
668
669 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
670                                         struct soc_camera_device *icd)
671 {
672         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
673         struct sh_mobile_ceu_dev *pcdev = ici->priv;
674
675         videobuf_queue_dma_contig_init(q,
676                                        &sh_mobile_ceu_videobuf_ops,
677                                        &ici->dev, &pcdev->lock,
678                                        V4L2_BUF_TYPE_VIDEO_CAPTURE,
679                                        V4L2_FIELD_NONE,
680                                        sizeof(struct sh_mobile_ceu_buffer),
681                                        icd);
682 }
683
684 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
685         .owner          = THIS_MODULE,
686         .add            = sh_mobile_ceu_add_device,
687         .remove         = sh_mobile_ceu_remove_device,
688         .get_formats    = sh_mobile_ceu_get_formats,
689         .set_fmt        = sh_mobile_ceu_set_fmt,
690         .try_fmt        = sh_mobile_ceu_try_fmt,
691         .reqbufs        = sh_mobile_ceu_reqbufs,
692         .poll           = sh_mobile_ceu_poll,
693         .querycap       = sh_mobile_ceu_querycap,
694         .set_bus_param  = sh_mobile_ceu_set_bus_param,
695         .init_videobuf  = sh_mobile_ceu_init_videobuf,
696 };
697
698 static int sh_mobile_ceu_probe(struct platform_device *pdev)
699 {
700         struct sh_mobile_ceu_dev *pcdev;
701         struct resource *res;
702         void __iomem *base;
703         char clk_name[8];
704         unsigned int irq;
705         int err = 0;
706
707         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
708         irq = platform_get_irq(pdev, 0);
709         if (!res || !irq) {
710                 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
711                 err = -ENODEV;
712                 goto exit;
713         }
714
715         pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
716         if (!pcdev) {
717                 dev_err(&pdev->dev, "Could not allocate pcdev\n");
718                 err = -ENOMEM;
719                 goto exit;
720         }
721
722         platform_set_drvdata(pdev, pcdev);
723         INIT_LIST_HEAD(&pcdev->capture);
724         spin_lock_init(&pcdev->lock);
725
726         pcdev->pdata = pdev->dev.platform_data;
727         if (!pcdev->pdata) {
728                 err = -EINVAL;
729                 dev_err(&pdev->dev, "CEU platform data not set.\n");
730                 goto exit_kfree;
731         }
732
733         base = ioremap_nocache(res->start, res->end - res->start + 1);
734         if (!base) {
735                 err = -ENXIO;
736                 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
737                 goto exit_kfree;
738         }
739
740         pcdev->irq = irq;
741         pcdev->base = base;
742         pcdev->video_limit = 0; /* only enabled if second resource exists */
743         pcdev->dev = &pdev->dev;
744
745         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
746         if (res) {
747                 err = dma_declare_coherent_memory(&pdev->dev, res->start,
748                                                   res->start,
749                                                   (res->end - res->start) + 1,
750                                                   DMA_MEMORY_MAP |
751                                                   DMA_MEMORY_EXCLUSIVE);
752                 if (!err) {
753                         dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
754                         err = -ENXIO;
755                         goto exit_iounmap;
756                 }
757
758                 pcdev->video_limit = (res->end - res->start) + 1;
759         }
760
761         /* request irq */
762         err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
763                           dev_name(&pdev->dev), pcdev);
764         if (err) {
765                 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
766                 goto exit_release_mem;
767         }
768
769         snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
770         pcdev->clk = clk_get(&pdev->dev, clk_name);
771         if (IS_ERR(pcdev->clk)) {
772                 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
773                 err = PTR_ERR(pcdev->clk);
774                 goto exit_free_irq;
775         }
776
777         pcdev->ici.priv = pcdev;
778         pcdev->ici.dev.parent = &pdev->dev;
779         pcdev->ici.nr = pdev->id;
780         pcdev->ici.drv_name = dev_name(&pdev->dev);
781         pcdev->ici.ops = &sh_mobile_ceu_host_ops;
782
783         err = soc_camera_host_register(&pcdev->ici);
784         if (err)
785                 goto exit_free_clk;
786
787         return 0;
788
789 exit_free_clk:
790         clk_put(pcdev->clk);
791 exit_free_irq:
792         free_irq(pcdev->irq, pcdev);
793 exit_release_mem:
794         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
795                 dma_release_declared_memory(&pdev->dev);
796 exit_iounmap:
797         iounmap(base);
798 exit_kfree:
799         kfree(pcdev);
800 exit:
801         return err;
802 }
803
804 static int sh_mobile_ceu_remove(struct platform_device *pdev)
805 {
806         struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
807
808         soc_camera_host_unregister(&pcdev->ici);
809         clk_put(pcdev->clk);
810         free_irq(pcdev->irq, pcdev);
811         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
812                 dma_release_declared_memory(&pdev->dev);
813         iounmap(pcdev->base);
814         kfree(pcdev);
815         return 0;
816 }
817
818 static struct platform_driver sh_mobile_ceu_driver = {
819         .driver         = {
820                 .name   = "sh_mobile_ceu",
821         },
822         .probe          = sh_mobile_ceu_probe,
823         .remove         = sh_mobile_ceu_remove,
824 };
825
826 static int __init sh_mobile_ceu_init(void)
827 {
828         return platform_driver_register(&sh_mobile_ceu_driver);
829 }
830
831 static void __exit sh_mobile_ceu_exit(void)
832 {
833         platform_driver_unregister(&sh_mobile_ceu_driver);
834 }
835
836 module_init(sh_mobile_ceu_init);
837 module_exit(sh_mobile_ceu_exit);
838
839 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
840 MODULE_AUTHOR("Magnus Damm");
841 MODULE_LICENSE("GPL");