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