V4L/DVB (12520): sh-mobile-ceu-camera: do not wait for interrupt when releasing buffers
[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/pm_runtime.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 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
91         /* lock used to protect videobuf */
92         spinlock_t lock;
93         struct list_head capture;
94         struct videobuf_buffer *active;
95         int is_interlaced;
96
97         struct sh_mobile_ceu_info *pdata;
98
99         const struct soc_camera_data_format *camera_fmt;
100 };
101
102 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
103 {
104         unsigned long flags;
105
106         flags = SOCAM_MASTER |
107                 SOCAM_PCLK_SAMPLE_RISING |
108                 SOCAM_HSYNC_ACTIVE_HIGH |
109                 SOCAM_HSYNC_ACTIVE_LOW |
110                 SOCAM_VSYNC_ACTIVE_HIGH |
111                 SOCAM_VSYNC_ACTIVE_LOW |
112                 SOCAM_DATA_ACTIVE_HIGH;
113
114         if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
115                 flags |= SOCAM_DATAWIDTH_8;
116
117         if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
118                 flags |= SOCAM_DATAWIDTH_16;
119
120         if (flags & SOCAM_DATAWIDTH_MASK)
121                 return flags;
122
123         return 0;
124 }
125
126 static void ceu_write(struct sh_mobile_ceu_dev *priv,
127                       unsigned long reg_offs, u32 data)
128 {
129         iowrite32(data, priv->base + reg_offs);
130 }
131
132 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
133 {
134         return ioread32(priv->base + reg_offs);
135 }
136
137 /*
138  *  Videobuf operations
139  */
140 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
141                                         unsigned int *count,
142                                         unsigned int *size)
143 {
144         struct soc_camera_device *icd = vq->priv_data;
145         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
146         struct sh_mobile_ceu_dev *pcdev = ici->priv;
147         int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
148
149         *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height *
150                            bytes_per_pixel);
151
152         if (0 == *count)
153                 *count = 2;
154
155         if (pcdev->video_limit) {
156                 while (*size * *count > pcdev->video_limit)
157                         (*count)--;
158         }
159
160         dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
161
162         return 0;
163 }
164
165 static void free_buffer(struct videobuf_queue *vq,
166                         struct sh_mobile_ceu_buffer *buf)
167 {
168         struct soc_camera_device *icd = vq->priv_data;
169
170         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
171                 &buf->vb, buf->vb.baddr, buf->vb.bsize);
172
173         if (in_interrupt())
174                 BUG();
175
176         videobuf_waiton(&buf->vb, 0, 0);
177         videobuf_dma_contig_free(vq, &buf->vb);
178         dev_dbg(&icd->dev, "%s freed\n", __func__);
179         buf->vb.state = VIDEOBUF_NEEDS_INIT;
180 }
181
182 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
183 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
184 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
185 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
186
187
188 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
189 {
190         struct soc_camera_device *icd = pcdev->icd;
191         dma_addr_t phys_addr_top, phys_addr_bottom;
192
193         /* The hardware is _very_ picky about this sequence. Especially
194          * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
195          * several not-so-well documented interrupt sources in CETCR.
196          */
197         ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
198         ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
199         ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
200         ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
201         ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
202
203         if (!pcdev->active)
204                 return;
205
206         phys_addr_top = videobuf_to_dma_contig(pcdev->active);
207         ceu_write(pcdev, CDAYR, phys_addr_top);
208         if (pcdev->is_interlaced) {
209                 phys_addr_bottom = phys_addr_top + icd->rect_current.width;
210                 ceu_write(pcdev, CDBYR, phys_addr_bottom);
211         }
212
213         switch (icd->current_fmt->fourcc) {
214         case V4L2_PIX_FMT_NV12:
215         case V4L2_PIX_FMT_NV21:
216         case V4L2_PIX_FMT_NV16:
217         case V4L2_PIX_FMT_NV61:
218                 phys_addr_top += icd->rect_current.width *
219                         icd->rect_current.height;
220                 ceu_write(pcdev, CDACR, phys_addr_top);
221                 if (pcdev->is_interlaced) {
222                         phys_addr_bottom = phys_addr_top +
223                                 icd->rect_current.width;
224                         ceu_write(pcdev, CDBCR, phys_addr_bottom);
225                 }
226         }
227
228         pcdev->active->state = VIDEOBUF_ACTIVE;
229         ceu_write(pcdev, CAPSR, 0x1); /* start capture */
230 }
231
232 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
233                                           struct videobuf_buffer *vb,
234                                           enum v4l2_field field)
235 {
236         struct soc_camera_device *icd = vq->priv_data;
237         struct sh_mobile_ceu_buffer *buf;
238         int ret;
239
240         buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
241
242         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
243                 vb, vb->baddr, vb->bsize);
244
245         /* Added list head initialization on alloc */
246         WARN_ON(!list_empty(&vb->queue));
247
248 #ifdef DEBUG
249         /* This can be useful if you want to see if we actually fill
250          * the buffer with something */
251         memset((void *)vb->baddr, 0xaa, vb->bsize);
252 #endif
253
254         BUG_ON(NULL == icd->current_fmt);
255
256         if (buf->fmt    != icd->current_fmt ||
257             vb->width   != icd->rect_current.width ||
258             vb->height  != icd->rect_current.height ||
259             vb->field   != field) {
260                 buf->fmt        = icd->current_fmt;
261                 vb->width       = icd->rect_current.width;
262                 vb->height      = icd->rect_current.height;
263                 vb->field       = field;
264                 vb->state       = VIDEOBUF_NEEDS_INIT;
265         }
266
267         vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
268         if (0 != vb->baddr && vb->bsize < vb->size) {
269                 ret = -EINVAL;
270                 goto out;
271         }
272
273         if (vb->state == VIDEOBUF_NEEDS_INIT) {
274                 ret = videobuf_iolock(vq, vb, NULL);
275                 if (ret)
276                         goto fail;
277                 vb->state = VIDEOBUF_PREPARED;
278         }
279
280         return 0;
281 fail:
282         free_buffer(vq, buf);
283 out:
284         return ret;
285 }
286
287 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
288 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
289                                          struct videobuf_buffer *vb)
290 {
291         struct soc_camera_device *icd = vq->priv_data;
292         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
293         struct sh_mobile_ceu_dev *pcdev = ici->priv;
294
295         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
296                 vb, vb->baddr, vb->bsize);
297
298         vb->state = VIDEOBUF_QUEUED;
299         list_add_tail(&vb->queue, &pcdev->capture);
300
301         if (!pcdev->active) {
302                 pcdev->active = vb;
303                 sh_mobile_ceu_capture(pcdev);
304         }
305 }
306
307 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
308                                            struct videobuf_buffer *vb)
309 {
310         struct soc_camera_device *icd = vq->priv_data;
311         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
312         struct sh_mobile_ceu_dev *pcdev = ici->priv;
313         unsigned long flags;
314
315         spin_lock_irqsave(&pcdev->lock, flags);
316
317         if (pcdev->active == vb) {
318                 /* disable capture (release DMA buffer), reset */
319                 ceu_write(pcdev, CAPSR, 1 << 16);
320                 pcdev->active = NULL;
321         }
322
323         if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
324             !list_empty(&vb->queue)) {
325                 vb->state = VIDEOBUF_ERROR;
326                 list_del_init(&vb->queue);
327         }
328
329         spin_unlock_irqrestore(&pcdev->lock, flags);
330
331         free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
332 }
333
334 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
335         .buf_setup      = sh_mobile_ceu_videobuf_setup,
336         .buf_prepare    = sh_mobile_ceu_videobuf_prepare,
337         .buf_queue      = sh_mobile_ceu_videobuf_queue,
338         .buf_release    = sh_mobile_ceu_videobuf_release,
339 };
340
341 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
342 {
343         struct sh_mobile_ceu_dev *pcdev = data;
344         struct videobuf_buffer *vb;
345         unsigned long flags;
346
347         spin_lock_irqsave(&pcdev->lock, flags);
348
349         vb = pcdev->active;
350         if (!vb)
351                 /* Stale interrupt from a released buffer */
352                 goto out;
353
354         list_del_init(&vb->queue);
355
356         if (!list_empty(&pcdev->capture))
357                 pcdev->active = list_entry(pcdev->capture.next,
358                                            struct videobuf_buffer, queue);
359         else
360                 pcdev->active = NULL;
361
362         sh_mobile_ceu_capture(pcdev);
363
364         vb->state = VIDEOBUF_DONE;
365         do_gettimeofday(&vb->ts);
366         vb->field_count++;
367         wake_up(&vb->done);
368
369 out:
370         spin_unlock_irqrestore(&pcdev->lock, flags);
371
372         return IRQ_HANDLED;
373 }
374
375 /* Called with .video_lock held */
376 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
377 {
378         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
379         struct sh_mobile_ceu_dev *pcdev = ici->priv;
380
381         if (pcdev->icd)
382                 return -EBUSY;
383
384         dev_info(&icd->dev,
385                  "SuperH Mobile CEU driver attached to camera %d\n",
386                  icd->devnum);
387
388         clk_enable(pcdev->clk);
389
390         ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
391         while (ceu_read(pcdev, CSTSR) & 1)
392                 msleep(1);
393
394         pcdev->icd = icd;
395
396         return 0;
397 }
398
399 /* Called with .video_lock held */
400 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
401 {
402         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
403         struct sh_mobile_ceu_dev *pcdev = ici->priv;
404         unsigned long flags;
405
406         BUG_ON(icd != pcdev->icd);
407
408         /* disable capture, disable interrupts */
409         ceu_write(pcdev, CEIER, 0);
410         ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
411
412         /* make sure active buffer is canceled */
413         spin_lock_irqsave(&pcdev->lock, flags);
414         if (pcdev->active) {
415                 list_del(&pcdev->active->queue);
416                 pcdev->active->state = VIDEOBUF_ERROR;
417                 wake_up_all(&pcdev->active->done);
418                 pcdev->active = NULL;
419         }
420         spin_unlock_irqrestore(&pcdev->lock, flags);
421
422         clk_disable(pcdev->clk);
423
424         dev_info(&icd->dev,
425                  "SuperH Mobile CEU driver detached from camera %d\n",
426                  icd->devnum);
427
428         pcdev->icd = NULL;
429 }
430
431 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
432                                        __u32 pixfmt)
433 {
434         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
435         struct sh_mobile_ceu_dev *pcdev = ici->priv;
436         int ret, buswidth, width, height, cfszr_width, cdwdr_width;
437         unsigned long camera_flags, common_flags, value;
438         int yuv_mode, yuv_lineskip;
439
440         camera_flags = icd->ops->query_bus_param(icd);
441         common_flags = soc_camera_bus_param_compatible(camera_flags,
442                                                        make_bus_param(pcdev));
443         if (!common_flags)
444                 return -EINVAL;
445
446         ret = icd->ops->set_bus_param(icd, common_flags);
447         if (ret < 0)
448                 return ret;
449
450         switch (common_flags & SOCAM_DATAWIDTH_MASK) {
451         case SOCAM_DATAWIDTH_8:
452                 buswidth = 8;
453                 break;
454         case SOCAM_DATAWIDTH_16:
455                 buswidth = 16;
456                 break;
457         default:
458                 return -EINVAL;
459         }
460
461         ceu_write(pcdev, CRCNTR, 0);
462         ceu_write(pcdev, CRCMPR, 0);
463
464         value = 0x00000010; /* data fetch by default */
465         yuv_mode = yuv_lineskip = 0;
466
467         switch (icd->current_fmt->fourcc) {
468         case V4L2_PIX_FMT_NV12:
469         case V4L2_PIX_FMT_NV21:
470                 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
471                 /* fall-through */
472         case V4L2_PIX_FMT_NV16:
473         case V4L2_PIX_FMT_NV61:
474                 yuv_mode = 1;
475                 switch (pcdev->camera_fmt->fourcc) {
476                 case V4L2_PIX_FMT_UYVY:
477                         value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
478                         break;
479                 case V4L2_PIX_FMT_VYUY:
480                         value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
481                         break;
482                 case V4L2_PIX_FMT_YUYV:
483                         value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
484                         break;
485                 case V4L2_PIX_FMT_YVYU:
486                         value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
487                         break;
488                 default:
489                         BUG();
490                 }
491         }
492
493         if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
494             icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
495                 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
496
497         value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
498         value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
499         value |= buswidth == 16 ? 1 << 12 : 0;
500         ceu_write(pcdev, CAMCR, value);
501
502         ceu_write(pcdev, CAPCR, 0x00300000);
503         ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
504
505         mdelay(1);
506
507         if (yuv_mode) {
508                 width = icd->rect_current.width * 2;
509                 width = buswidth == 16 ? width / 2 : width;
510                 cfszr_width = cdwdr_width = icd->rect_current.width;
511         } else {
512                 width = icd->rect_current.width *
513                         ((icd->current_fmt->depth + 7) >> 3);
514                 width = buswidth == 16 ? width / 2 : width;
515                 cfszr_width = buswidth == 8 ? width / 2 : width;
516                 cdwdr_width = buswidth == 16 ? width * 2 : width;
517         }
518
519         height = icd->rect_current.height;
520         if (pcdev->is_interlaced) {
521                 height /= 2;
522                 cdwdr_width *= 2;
523         }
524
525         ceu_write(pcdev, CAMOR, 0);
526         ceu_write(pcdev, CAPWR, (height << 16) | width);
527         ceu_write(pcdev, CFLCR, 0); /* no scaling */
528         ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
529
530         /* A few words about byte order (observed in Big Endian mode)
531          *
532          * In data fetch mode bytes are received in chunks of 8 bytes.
533          * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
534          *
535          * The data is however by default written to memory in reverse order:
536          * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
537          *
538          * The lowest three bits of CDOCR allows us to do swapping,
539          * using 7 we swap the data bytes to match the incoming order:
540          * D0, D1, D2, D3, D4, D5, D6, D7
541          */
542         value = 0x00000017;
543         if (yuv_lineskip)
544                 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
545
546         ceu_write(pcdev, CDOCR, value);
547
548         ceu_write(pcdev, CDWDR, cdwdr_width);
549         ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
550
551         /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
552         return 0;
553 }
554
555 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
556 {
557         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
558         struct sh_mobile_ceu_dev *pcdev = ici->priv;
559         unsigned long camera_flags, common_flags;
560
561         camera_flags = icd->ops->query_bus_param(icd);
562         common_flags = soc_camera_bus_param_compatible(camera_flags,
563                                                        make_bus_param(pcdev));
564         if (!common_flags)
565                 return -EINVAL;
566
567         return 0;
568 }
569
570 static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
571         {
572                 .name           = "NV12",
573                 .depth          = 12,
574                 .fourcc         = V4L2_PIX_FMT_NV12,
575                 .colorspace     = V4L2_COLORSPACE_JPEG,
576         },
577         {
578                 .name           = "NV21",
579                 .depth          = 12,
580                 .fourcc         = V4L2_PIX_FMT_NV21,
581                 .colorspace     = V4L2_COLORSPACE_JPEG,
582         },
583         {
584                 .name           = "NV16",
585                 .depth          = 16,
586                 .fourcc         = V4L2_PIX_FMT_NV16,
587                 .colorspace     = V4L2_COLORSPACE_JPEG,
588         },
589         {
590                 .name           = "NV61",
591                 .depth          = 16,
592                 .fourcc         = V4L2_PIX_FMT_NV61,
593                 .colorspace     = V4L2_COLORSPACE_JPEG,
594         },
595 };
596
597 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
598                                      struct soc_camera_format_xlate *xlate)
599 {
600         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
601         int ret, k, n;
602         int formats = 0;
603
604         ret = sh_mobile_ceu_try_bus_param(icd);
605         if (ret < 0)
606                 return 0;
607
608         /* Beginning of a pass */
609         if (!idx)
610                 icd->host_priv = NULL;
611
612         switch (icd->formats[idx].fourcc) {
613         case V4L2_PIX_FMT_UYVY:
614         case V4L2_PIX_FMT_VYUY:
615         case V4L2_PIX_FMT_YUYV:
616         case V4L2_PIX_FMT_YVYU:
617                 if (icd->host_priv)
618                         goto add_single_format;
619
620                 /*
621                  * Our case is simple so far: for any of the above four camera
622                  * formats we add all our four synthesized NV* formats, so,
623                  * just marking the device with a single flag suffices. If
624                  * the format generation rules are more complex, you would have
625                  * to actually hang your already added / counted formats onto
626                  * the host_priv pointer and check whether the format you're
627                  * going to add now is already there.
628                  */
629                 icd->host_priv = (void *)sh_mobile_ceu_formats;
630
631                 n = ARRAY_SIZE(sh_mobile_ceu_formats);
632                 formats += n;
633                 for (k = 0; xlate && k < n; k++) {
634                         xlate->host_fmt = &sh_mobile_ceu_formats[k];
635                         xlate->cam_fmt = icd->formats + idx;
636                         xlate->buswidth = icd->formats[idx].depth;
637                         xlate++;
638                         dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n",
639                                 sh_mobile_ceu_formats[k].name,
640                                 icd->formats[idx].name);
641                 }
642         default:
643 add_single_format:
644                 /* Generic pass-through */
645                 formats++;
646                 if (xlate) {
647                         xlate->host_fmt = icd->formats + idx;
648                         xlate->cam_fmt = icd->formats + idx;
649                         xlate->buswidth = icd->formats[idx].depth;
650                         xlate++;
651                         dev_dbg(ici->v4l2_dev.dev,
652                                 "Providing format %s in pass-through mode\n",
653                                 icd->formats[idx].name);
654                 }
655         }
656
657         return formats;
658 }
659
660 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
661                                   struct v4l2_rect *rect)
662 {
663         return icd->ops->set_crop(icd, rect);
664 }
665
666 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
667                                  struct v4l2_format *f)
668 {
669         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
670         struct sh_mobile_ceu_dev *pcdev = ici->priv;
671         __u32 pixfmt = f->fmt.pix.pixelformat;
672         const struct soc_camera_format_xlate *xlate;
673         int ret;
674
675         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
676         if (!xlate) {
677                 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
678                 return -EINVAL;
679         }
680
681         f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
682         ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f);
683         f->fmt.pix.pixelformat = pixfmt;
684         if (!ret) {
685                 icd->buswidth = xlate->buswidth;
686                 icd->current_fmt = xlate->host_fmt;
687                 pcdev->camera_fmt = xlate->cam_fmt;
688         }
689
690         return ret;
691 }
692
693 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
694                                  struct v4l2_format *f)
695 {
696         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
697         struct sh_mobile_ceu_dev *pcdev = ici->priv;
698         const struct soc_camera_format_xlate *xlate;
699         __u32 pixfmt = f->fmt.pix.pixelformat;
700         int ret;
701
702         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
703         if (!xlate) {
704                 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
705                 return -EINVAL;
706         }
707
708         /* FIXME: calculate using depth and bus width */
709
710         v4l_bound_align_image(&f->fmt.pix.width, 2, 2560, 1,
711                               &f->fmt.pix.height, 4, 1920, 2, 0);
712
713         f->fmt.pix.bytesperline = f->fmt.pix.width *
714                 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
715         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
716
717         f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
718
719         /* limit to sensor capabilities */
720         ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f);
721         f->fmt.pix.pixelformat = pixfmt;
722         if (ret < 0)
723                 return ret;
724
725         switch (f->fmt.pix.field) {
726         case V4L2_FIELD_INTERLACED:
727                 pcdev->is_interlaced = 1;
728                 break;
729         case V4L2_FIELD_ANY:
730                 f->fmt.pix.field = V4L2_FIELD_NONE;
731                 /* fall-through */
732         case V4L2_FIELD_NONE:
733                 pcdev->is_interlaced = 0;
734                 break;
735         default:
736                 ret = -EINVAL;
737                 break;
738         }
739
740         return ret;
741 }
742
743 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
744                                  struct v4l2_requestbuffers *p)
745 {
746         int i;
747
748         /* This is for locking debugging only. I removed spinlocks and now I
749          * check whether .prepare is ever called on a linked buffer, or whether
750          * a dma IRQ can occur for an in-work or unlinked buffer. Until now
751          * it hadn't triggered */
752         for (i = 0; i < p->count; i++) {
753                 struct sh_mobile_ceu_buffer *buf;
754
755                 buf = container_of(icf->vb_vidq.bufs[i],
756                                    struct sh_mobile_ceu_buffer, vb);
757                 INIT_LIST_HEAD(&buf->vb.queue);
758         }
759
760         return 0;
761 }
762
763 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
764 {
765         struct soc_camera_file *icf = file->private_data;
766         struct sh_mobile_ceu_buffer *buf;
767
768         buf = list_entry(icf->vb_vidq.stream.next,
769                          struct sh_mobile_ceu_buffer, vb.stream);
770
771         poll_wait(file, &buf->vb.done, pt);
772
773         if (buf->vb.state == VIDEOBUF_DONE ||
774             buf->vb.state == VIDEOBUF_ERROR)
775                 return POLLIN|POLLRDNORM;
776
777         return 0;
778 }
779
780 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
781                                   struct v4l2_capability *cap)
782 {
783         strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
784         cap->version = KERNEL_VERSION(0, 0, 5);
785         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
786         return 0;
787 }
788
789 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
790                                         struct soc_camera_device *icd)
791 {
792         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
793         struct sh_mobile_ceu_dev *pcdev = ici->priv;
794
795         videobuf_queue_dma_contig_init(q,
796                                        &sh_mobile_ceu_videobuf_ops,
797                                        ici->v4l2_dev.dev, &pcdev->lock,
798                                        V4L2_BUF_TYPE_VIDEO_CAPTURE,
799                                        pcdev->is_interlaced ?
800                                        V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
801                                        sizeof(struct sh_mobile_ceu_buffer),
802                                        icd);
803 }
804
805 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
806                                   struct v4l2_control *ctrl)
807 {
808         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
809         struct sh_mobile_ceu_dev *pcdev = ici->priv;
810         u32 val;
811
812         switch (ctrl->id) {
813         case V4L2_CID_SHARPNESS:
814                 val = ceu_read(pcdev, CLFCR);
815                 ctrl->value = val ^ 1;
816                 return 0;
817         }
818         return -ENOIOCTLCMD;
819 }
820
821 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
822                                   struct v4l2_control *ctrl)
823 {
824         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
825         struct sh_mobile_ceu_dev *pcdev = ici->priv;
826
827         switch (ctrl->id) {
828         case V4L2_CID_SHARPNESS:
829                 switch (icd->current_fmt->fourcc) {
830                 case V4L2_PIX_FMT_NV12:
831                 case V4L2_PIX_FMT_NV21:
832                 case V4L2_PIX_FMT_NV16:
833                 case V4L2_PIX_FMT_NV61:
834                         ceu_write(pcdev, CLFCR, !ctrl->value);
835                         return 0;
836                 }
837                 return -EINVAL;
838         }
839         return -ENOIOCTLCMD;
840 }
841
842 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
843         {
844                 .id             = V4L2_CID_SHARPNESS,
845                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
846                 .name           = "Low-pass filter",
847                 .minimum        = 0,
848                 .maximum        = 1,
849                 .step           = 1,
850                 .default_value  = 0,
851         },
852 };
853
854 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
855         .owner          = THIS_MODULE,
856         .add            = sh_mobile_ceu_add_device,
857         .remove         = sh_mobile_ceu_remove_device,
858         .get_formats    = sh_mobile_ceu_get_formats,
859         .set_crop       = sh_mobile_ceu_set_crop,
860         .set_fmt        = sh_mobile_ceu_set_fmt,
861         .try_fmt        = sh_mobile_ceu_try_fmt,
862         .set_ctrl       = sh_mobile_ceu_set_ctrl,
863         .get_ctrl       = sh_mobile_ceu_get_ctrl,
864         .reqbufs        = sh_mobile_ceu_reqbufs,
865         .poll           = sh_mobile_ceu_poll,
866         .querycap       = sh_mobile_ceu_querycap,
867         .set_bus_param  = sh_mobile_ceu_set_bus_param,
868         .init_videobuf  = sh_mobile_ceu_init_videobuf,
869         .controls       = sh_mobile_ceu_controls,
870         .num_controls   = ARRAY_SIZE(sh_mobile_ceu_controls),
871 };
872
873 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
874 {
875         struct sh_mobile_ceu_dev *pcdev;
876         struct resource *res;
877         void __iomem *base;
878         unsigned int irq;
879         int err = 0;
880
881         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
882         irq = platform_get_irq(pdev, 0);
883         if (!res || !irq) {
884                 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
885                 err = -ENODEV;
886                 goto exit;
887         }
888
889         pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
890         if (!pcdev) {
891                 dev_err(&pdev->dev, "Could not allocate pcdev\n");
892                 err = -ENOMEM;
893                 goto exit;
894         }
895
896         INIT_LIST_HEAD(&pcdev->capture);
897         spin_lock_init(&pcdev->lock);
898
899         pcdev->pdata = pdev->dev.platform_data;
900         if (!pcdev->pdata) {
901                 err = -EINVAL;
902                 dev_err(&pdev->dev, "CEU platform data not set.\n");
903                 goto exit_kfree;
904         }
905
906         base = ioremap_nocache(res->start, resource_size(res));
907         if (!base) {
908                 err = -ENXIO;
909                 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
910                 goto exit_kfree;
911         }
912
913         pcdev->irq = irq;
914         pcdev->base = base;
915         pcdev->video_limit = 0; /* only enabled if second resource exists */
916
917         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
918         if (res) {
919                 err = dma_declare_coherent_memory(&pdev->dev, res->start,
920                                                   res->start,
921                                                   resource_size(res),
922                                                   DMA_MEMORY_MAP |
923                                                   DMA_MEMORY_EXCLUSIVE);
924                 if (!err) {
925                         dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
926                         err = -ENXIO;
927                         goto exit_iounmap;
928                 }
929
930                 pcdev->video_limit = resource_size(res);
931         }
932
933         /* request irq */
934         err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
935                           dev_name(&pdev->dev), pcdev);
936         if (err) {
937                 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
938                 goto exit_release_mem;
939         }
940
941         pm_suspend_ignore_children(&pdev->dev, true);
942         pm_runtime_enable(&pdev->dev);
943         pm_runtime_resume(&pdev->dev);
944
945         pcdev->ici.priv = pcdev;
946         pcdev->ici.v4l2_dev.dev = &pdev->dev;
947         pcdev->ici.nr = pdev->id;
948         pcdev->ici.drv_name = dev_name(&pdev->dev);
949         pcdev->ici.ops = &sh_mobile_ceu_host_ops;
950
951         err = soc_camera_host_register(&pcdev->ici);
952         if (err)
953                 goto exit_free_irq;
954
955         return 0;
956
957 exit_free_irq:
958         free_irq(pcdev->irq, pcdev);
959 exit_release_mem:
960         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
961                 dma_release_declared_memory(&pdev->dev);
962 exit_iounmap:
963         iounmap(base);
964 exit_kfree:
965         kfree(pcdev);
966 exit:
967         return err;
968 }
969
970 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
971 {
972         struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
973         struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
974                                         struct sh_mobile_ceu_dev, ici);
975
976         soc_camera_host_unregister(soc_host);
977         free_irq(pcdev->irq, pcdev);
978         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
979                 dma_release_declared_memory(&pdev->dev);
980         iounmap(pcdev->base);
981         kfree(pcdev);
982         return 0;
983 }
984
985 static int sh_mobile_ceu_runtime_nop(struct device *dev)
986 {
987         /* Runtime PM callback shared between ->runtime_suspend()
988          * and ->runtime_resume(). Simply returns success.
989          *
990          * This driver re-initializes all registers after
991          * pm_runtime_get_sync() anyway so there is no need
992          * to save and restore registers here.
993          */
994         return 0;
995 }
996
997 static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
998         .runtime_suspend = sh_mobile_ceu_runtime_nop,
999         .runtime_resume = sh_mobile_ceu_runtime_nop,
1000 };
1001
1002 static struct platform_driver sh_mobile_ceu_driver = {
1003         .driver         = {
1004                 .name   = "sh_mobile_ceu",
1005                 .pm     = &sh_mobile_ceu_dev_pm_ops,
1006         },
1007         .probe          = sh_mobile_ceu_probe,
1008         .remove         = __exit_p(sh_mobile_ceu_remove),
1009 };
1010
1011 static int __init sh_mobile_ceu_init(void)
1012 {
1013         return platform_driver_register(&sh_mobile_ceu_driver);
1014 }
1015
1016 static void __exit sh_mobile_ceu_exit(void)
1017 {
1018         platform_driver_unregister(&sh_mobile_ceu_driver);
1019 }
1020
1021 module_init(sh_mobile_ceu_init);
1022 module_exit(sh_mobile_ceu_exit);
1023
1024 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1025 MODULE_AUTHOR("Magnus Damm");
1026 MODULE_LICENSE("GPL");
1027 MODULE_ALIAS("platform:sh_mobile_ceu");