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