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