Merge remote-tracking branch 'mkp-scsi/4.8/scsi-fixes' into fixes
[cascardo/linux.git] / drivers / media / platform / soc_camera / 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/completion.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/moduleparam.h>
30 #include <linux/of.h>
31 #include <linux/time.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 #include <linux/platform_device.h>
35 #include <linux/videodev2.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/sched.h>
38
39 #include <media/v4l2-async.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-dev.h>
42 #include <media/soc_camera.h>
43 #include <media/drv-intf/sh_mobile_ceu.h>
44 #include <media/drv-intf/sh_mobile_csi2.h>
45 #include <media/videobuf2-dma-contig.h>
46 #include <media/v4l2-mediabus.h>
47 #include <media/drv-intf/soc_mediabus.h>
48
49 #include "soc_scale_crop.h"
50
51 /* register offsets for sh7722 / sh7723 */
52
53 #define CAPSR  0x00 /* Capture start register */
54 #define CAPCR  0x04 /* Capture control register */
55 #define CAMCR  0x08 /* Capture interface control register */
56 #define CMCYR  0x0c /* Capture interface cycle  register */
57 #define CAMOR  0x10 /* Capture interface offset register */
58 #define CAPWR  0x14 /* Capture interface width register */
59 #define CAIFR  0x18 /* Capture interface input format register */
60 #define CSTCR  0x20 /* Camera strobe control register (<= sh7722) */
61 #define CSECR  0x24 /* Camera strobe emission count register (<= sh7722) */
62 #define CRCNTR 0x28 /* CEU register control register */
63 #define CRCMPR 0x2c /* CEU register forcible control register */
64 #define CFLCR  0x30 /* Capture filter control register */
65 #define CFSZR  0x34 /* Capture filter size clip register */
66 #define CDWDR  0x38 /* Capture destination width register */
67 #define CDAYR  0x3c /* Capture data address Y register */
68 #define CDACR  0x40 /* Capture data address C register */
69 #define CDBYR  0x44 /* Capture data bottom-field address Y register */
70 #define CDBCR  0x48 /* Capture data bottom-field address C register */
71 #define CBDSR  0x4c /* Capture bundle destination size register */
72 #define CFWCR  0x5c /* Firewall operation control register */
73 #define CLFCR  0x60 /* Capture low-pass filter control register */
74 #define CDOCR  0x64 /* Capture data output control register */
75 #define CDDCR  0x68 /* Capture data complexity level register */
76 #define CDDAR  0x6c /* Capture data complexity level address register */
77 #define CEIER  0x70 /* Capture event interrupt enable register */
78 #define CETCR  0x74 /* Capture event flag clear register */
79 #define CSTSR  0x7c /* Capture status register */
80 #define CSRTR  0x80 /* Capture software reset register */
81 #define CDSSR  0x84 /* Capture data size register */
82 #define CDAYR2 0x90 /* Capture data address Y register 2 */
83 #define CDACR2 0x94 /* Capture data address C register 2 */
84 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
85 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
86
87 #undef DEBUG_GEOMETRY
88 #ifdef DEBUG_GEOMETRY
89 #define dev_geo dev_info
90 #else
91 #define dev_geo dev_dbg
92 #endif
93
94 /* per video frame buffer */
95 struct sh_mobile_ceu_buffer {
96         struct vb2_v4l2_buffer vb; /* v4l buffer must be first */
97         struct list_head queue;
98 };
99
100 struct sh_mobile_ceu_dev {
101         struct soc_camera_host ici;
102         /* Asynchronous CSI2 linking */
103         struct v4l2_async_subdev *csi2_asd;
104         struct v4l2_subdev *csi2_sd;
105         /* Synchronous probing compatibility */
106         struct platform_device *csi2_pdev;
107
108         unsigned int irq;
109         void __iomem *base;
110         size_t video_limit;
111         size_t buf_total;
112
113         spinlock_t lock;                /* Protects video buffer lists */
114         struct list_head capture;
115         struct vb2_v4l2_buffer *active;
116
117         struct sh_mobile_ceu_info *pdata;
118         struct completion complete;
119
120         u32 cflcr;
121
122         /* static max sizes either from platform data or default */
123         int max_width;
124         int max_height;
125
126         enum v4l2_field field;
127         int sequence;
128         unsigned long flags;
129
130         unsigned int image_mode:1;
131         unsigned int is_16bit:1;
132         unsigned int frozen:1;
133 };
134
135 struct sh_mobile_ceu_cam {
136         /* CEU offsets within the camera output, before the CEU scaler */
137         unsigned int ceu_left;
138         unsigned int ceu_top;
139         /* Client output, as seen by the CEU */
140         unsigned int width;
141         unsigned int height;
142         /*
143          * User window from S_CROP / G_CROP, produced by client cropping and
144          * scaling, CEU scaling and CEU cropping, mapped back onto the client
145          * input window
146          */
147         struct v4l2_rect subrect;
148         /* Camera cropping rectangle */
149         struct v4l2_rect rect;
150         const struct soc_mbus_pixelfmt *extra_fmt;
151         u32 code;
152 };
153
154 static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_v4l2_buffer *vbuf)
155 {
156         return container_of(vbuf, struct sh_mobile_ceu_buffer, vb);
157 }
158
159 static void ceu_write(struct sh_mobile_ceu_dev *priv,
160                       unsigned long reg_offs, u32 data)
161 {
162         iowrite32(data, priv->base + reg_offs);
163 }
164
165 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
166 {
167         return ioread32(priv->base + reg_offs);
168 }
169
170 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
171 {
172         int i, success = 0;
173
174         ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
175
176         /* wait CSTSR.CPTON bit */
177         for (i = 0; i < 1000; i++) {
178                 if (!(ceu_read(pcdev, CSTSR) & 1)) {
179                         success++;
180                         break;
181                 }
182                 udelay(1);
183         }
184
185         /* wait CAPSR.CPKIL bit */
186         for (i = 0; i < 1000; i++) {
187                 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
188                         success++;
189                         break;
190                 }
191                 udelay(1);
192         }
193
194         if (2 != success) {
195                 dev_warn(pcdev->ici.v4l2_dev.dev, "soft reset time out\n");
196                 return -EIO;
197         }
198
199         return 0;
200 }
201
202 /*
203  *  Videobuf operations
204  */
205
206 /*
207  * .queue_setup() is called to check, whether the driver can accept the
208  *                requested number of buffers and to fill in plane sizes
209  *                for the current frame format if required
210  */
211 static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
212                         unsigned int *count, unsigned int *num_planes,
213                         unsigned int sizes[], struct device *alloc_devs[])
214 {
215         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
216         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
217         struct sh_mobile_ceu_dev *pcdev = ici->priv;
218
219         if (!vq->num_buffers)
220                 pcdev->sequence = 0;
221
222         if (!*count)
223                 *count = 2;
224
225         /* Called from VIDIOC_REQBUFS or in compatibility mode */
226         if (!*num_planes)
227                 sizes[0] = icd->sizeimage;
228         else if (sizes[0] < icd->sizeimage)
229                 return -EINVAL;
230
231         /* If *num_planes != 0, we have already verified *count. */
232         if (pcdev->video_limit) {
233                 size_t size = PAGE_ALIGN(sizes[0]) * *count;
234
235                 if (size + pcdev->buf_total > pcdev->video_limit)
236                         *count = (pcdev->video_limit - pcdev->buf_total) /
237                                 PAGE_ALIGN(sizes[0]);
238         }
239
240         *num_planes = 1;
241
242         dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
243
244         return 0;
245 }
246
247 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
248 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
249 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
250 #define CEU_CEIER_VBP   (1 << 20) /* vbp error */
251 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
252 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
253
254
255 /*
256  * return value doesn't reflex the success/failure to queue the new buffer,
257  * but rather the status of the previous buffer.
258  */
259 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
260 {
261         struct soc_camera_device *icd = pcdev->ici.icd;
262         dma_addr_t phys_addr_top, phys_addr_bottom;
263         unsigned long top1, top2;
264         unsigned long bottom1, bottom2;
265         u32 status;
266         bool planar;
267         int ret = 0;
268
269         /*
270          * The hardware is _very_ picky about this sequence. Especially
271          * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
272          * several not-so-well documented interrupt sources in CETCR.
273          */
274         ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
275         status = ceu_read(pcdev, CETCR);
276         ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
277         if (!pcdev->frozen)
278                 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
279         ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
280         ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
281
282         /*
283          * When a VBP interrupt occurs, a capture end interrupt does not occur
284          * and the image of that frame is not captured correctly. So, soft reset
285          * is needed here.
286          */
287         if (status & CEU_CEIER_VBP) {
288                 sh_mobile_ceu_soft_reset(pcdev);
289                 ret = -EIO;
290         }
291
292         if (pcdev->frozen) {
293                 complete(&pcdev->complete);
294                 return ret;
295         }
296
297         if (!pcdev->active)
298                 return ret;
299
300         if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
301                 top1    = CDBYR;
302                 top2    = CDBCR;
303                 bottom1 = CDAYR;
304                 bottom2 = CDACR;
305         } else {
306                 top1    = CDAYR;
307                 top2    = CDACR;
308                 bottom1 = CDBYR;
309                 bottom2 = CDBCR;
310         }
311
312         phys_addr_top =
313                 vb2_dma_contig_plane_dma_addr(&pcdev->active->vb2_buf, 0);
314
315         switch (icd->current_fmt->host_fmt->fourcc) {
316         case V4L2_PIX_FMT_NV12:
317         case V4L2_PIX_FMT_NV21:
318         case V4L2_PIX_FMT_NV16:
319         case V4L2_PIX_FMT_NV61:
320                 planar = true;
321                 break;
322         default:
323                 planar = false;
324         }
325
326         ceu_write(pcdev, top1, phys_addr_top);
327         if (V4L2_FIELD_NONE != pcdev->field) {
328                 phys_addr_bottom = phys_addr_top + icd->bytesperline;
329                 ceu_write(pcdev, bottom1, phys_addr_bottom);
330         }
331
332         if (planar) {
333                 phys_addr_top += icd->bytesperline * icd->user_height;
334                 ceu_write(pcdev, top2, phys_addr_top);
335                 if (V4L2_FIELD_NONE != pcdev->field) {
336                         phys_addr_bottom = phys_addr_top + icd->bytesperline;
337                         ceu_write(pcdev, bottom2, phys_addr_bottom);
338                 }
339         }
340
341         ceu_write(pcdev, CAPSR, 0x1); /* start capture */
342
343         return ret;
344 }
345
346 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
347 {
348         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
349         struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
350
351         /* Added list head initialization on alloc */
352         WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
353
354         return 0;
355 }
356
357 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
358 {
359         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
360         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
361         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
362         struct sh_mobile_ceu_dev *pcdev = ici->priv;
363         struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
364         unsigned long size;
365
366         size = icd->sizeimage;
367
368         if (vb2_plane_size(vb, 0) < size) {
369                 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
370                         vb->index, vb2_plane_size(vb, 0), size);
371                 goto error;
372         }
373
374         vb2_set_plane_payload(vb, 0, size);
375
376         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
377                 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
378
379 #ifdef DEBUG
380         /*
381          * This can be useful if you want to see if we actually fill
382          * the buffer with something
383          */
384         if (vb2_plane_vaddr(vb, 0))
385                 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
386 #endif
387
388         spin_lock_irq(&pcdev->lock);
389         list_add_tail(&buf->queue, &pcdev->capture);
390
391         if (!pcdev->active) {
392                 /*
393                  * Because there were no active buffer at this moment,
394                  * we are not interested in the return value of
395                  * sh_mobile_ceu_capture here.
396                  */
397                 pcdev->active = vbuf;
398                 sh_mobile_ceu_capture(pcdev);
399         }
400         spin_unlock_irq(&pcdev->lock);
401
402         return;
403
404 error:
405         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
406 }
407
408 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
409 {
410         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
411         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
412         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
413         struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
414         struct sh_mobile_ceu_dev *pcdev = ici->priv;
415
416         spin_lock_irq(&pcdev->lock);
417
418         if (pcdev->active == vbuf) {
419                 /* disable capture (release DMA buffer), reset */
420                 ceu_write(pcdev, CAPSR, 1 << 16);
421                 pcdev->active = NULL;
422         }
423
424         /*
425          * Doesn't hurt also if the list is empty, but it hurts, if queuing the
426          * buffer failed, and .buf_init() hasn't been called
427          */
428         if (buf->queue.next)
429                 list_del_init(&buf->queue);
430
431         pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
432         dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
433                 pcdev->buf_total);
434
435         spin_unlock_irq(&pcdev->lock);
436 }
437
438 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
439 {
440         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
441         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
442         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
443         struct sh_mobile_ceu_dev *pcdev = ici->priv;
444
445         pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
446         dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
447                 pcdev->buf_total);
448
449         /* This is for locking debugging only */
450         INIT_LIST_HEAD(&to_ceu_vb(vbuf)->queue);
451         return 0;
452 }
453
454 static void sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
455 {
456         struct soc_camera_device *icd = soc_camera_from_vb2q(q);
457         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
458         struct sh_mobile_ceu_dev *pcdev = ici->priv;
459         struct list_head *buf_head, *tmp;
460
461         spin_lock_irq(&pcdev->lock);
462
463         pcdev->active = NULL;
464
465         list_for_each_safe(buf_head, tmp, &pcdev->capture)
466                 list_del_init(buf_head);
467
468         spin_unlock_irq(&pcdev->lock);
469
470         sh_mobile_ceu_soft_reset(pcdev);
471 }
472
473 static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
474         .queue_setup    = sh_mobile_ceu_videobuf_setup,
475         .buf_prepare    = sh_mobile_ceu_videobuf_prepare,
476         .buf_queue      = sh_mobile_ceu_videobuf_queue,
477         .buf_cleanup    = sh_mobile_ceu_videobuf_release,
478         .buf_init       = sh_mobile_ceu_videobuf_init,
479         .wait_prepare   = vb2_ops_wait_prepare,
480         .wait_finish    = vb2_ops_wait_finish,
481         .stop_streaming = sh_mobile_ceu_stop_streaming,
482 };
483
484 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
485 {
486         struct sh_mobile_ceu_dev *pcdev = data;
487         struct vb2_v4l2_buffer *vbuf;
488         int ret;
489
490         spin_lock(&pcdev->lock);
491
492         vbuf = pcdev->active;
493         if (!vbuf)
494                 /* Stale interrupt from a released buffer */
495                 goto out;
496
497         list_del_init(&to_ceu_vb(vbuf)->queue);
498
499         if (!list_empty(&pcdev->capture))
500                 pcdev->active = &list_entry(pcdev->capture.next,
501                                             struct sh_mobile_ceu_buffer, queue)->vb;
502         else
503                 pcdev->active = NULL;
504
505         ret = sh_mobile_ceu_capture(pcdev);
506         vbuf->vb2_buf.timestamp = ktime_get_ns();
507         if (!ret) {
508                 vbuf->field = pcdev->field;
509                 vbuf->sequence = pcdev->sequence++;
510         }
511         vb2_buffer_done(&vbuf->vb2_buf,
512                         ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
513
514 out:
515         spin_unlock(&pcdev->lock);
516
517         return IRQ_HANDLED;
518 }
519
520 static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev)
521 {
522         struct v4l2_subdev *sd;
523
524         if (pcdev->csi2_sd)
525                 return pcdev->csi2_sd;
526
527         if (pcdev->csi2_asd) {
528                 char name[] = "sh-mobile-csi2";
529                 v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev)
530                         if (!strncmp(name, sd->name, sizeof(name) - 1)) {
531                                 pcdev->csi2_sd = sd;
532                                 return sd;
533                         }
534         }
535
536         return NULL;
537 }
538
539 static struct v4l2_subdev *csi2_subdev(struct sh_mobile_ceu_dev *pcdev,
540                                        struct soc_camera_device *icd)
541 {
542         struct v4l2_subdev *sd = pcdev->csi2_sd;
543
544         return sd && sd->grp_id == soc_camera_grp_id(icd) ? sd : NULL;
545 }
546
547 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
548 {
549         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
550         struct sh_mobile_ceu_dev *pcdev = ici->priv;
551         struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
552         int ret;
553
554         if (csi2_sd) {
555                 csi2_sd->grp_id = soc_camera_grp_id(icd);
556                 v4l2_set_subdev_hostdata(csi2_sd, icd);
557         }
558
559         ret = v4l2_subdev_call(csi2_sd, core, s_power, 1);
560         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
561                 return ret;
562
563         /*
564          * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
565          * has not found this soc-camera device among its clients
566          */
567         if (csi2_sd && ret == -ENODEV)
568                 csi2_sd->grp_id = 0;
569
570         dev_info(icd->parent,
571                  "SuperH Mobile CEU%s driver attached to camera %d\n",
572                  csi2_sd && csi2_sd->grp_id ? "/CSI-2" : "", icd->devnum);
573
574         return 0;
575 }
576
577 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
578 {
579         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
580         struct sh_mobile_ceu_dev *pcdev = ici->priv;
581         struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
582
583         dev_info(icd->parent,
584                  "SuperH Mobile CEU driver detached from camera %d\n",
585                  icd->devnum);
586
587         v4l2_subdev_call(csi2_sd, core, s_power, 0);
588 }
589
590 /* Called with .host_lock held */
591 static int sh_mobile_ceu_clock_start(struct soc_camera_host *ici)
592 {
593         struct sh_mobile_ceu_dev *pcdev = ici->priv;
594
595         pm_runtime_get_sync(ici->v4l2_dev.dev);
596
597         pcdev->buf_total = 0;
598
599         sh_mobile_ceu_soft_reset(pcdev);
600
601         return 0;
602 }
603
604 /* Called with .host_lock held */
605 static void sh_mobile_ceu_clock_stop(struct soc_camera_host *ici)
606 {
607         struct sh_mobile_ceu_dev *pcdev = ici->priv;
608
609         /* disable capture, disable interrupts */
610         ceu_write(pcdev, CEIER, 0);
611         sh_mobile_ceu_soft_reset(pcdev);
612
613         /* make sure active buffer is canceled */
614         spin_lock_irq(&pcdev->lock);
615         if (pcdev->active) {
616                 list_del_init(&to_ceu_vb(pcdev->active)->queue);
617                 vb2_buffer_done(&pcdev->active->vb2_buf, VB2_BUF_STATE_ERROR);
618                 pcdev->active = NULL;
619         }
620         spin_unlock_irq(&pcdev->lock);
621
622         pm_runtime_put(ici->v4l2_dev.dev);
623 }
624
625 /*
626  * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
627  * in SH7722 Hardware Manual
628  */
629 static unsigned int size_dst(unsigned int src, unsigned int scale)
630 {
631         unsigned int mant_pre = scale >> 12;
632         if (!src || !scale)
633                 return src;
634         return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
635                 mant_pre * 4096 / scale + 1;
636 }
637
638 static u16 calc_scale(unsigned int src, unsigned int *dst)
639 {
640         u16 scale;
641
642         if (src == *dst)
643                 return 0;
644
645         scale = (src * 4096 / *dst) & ~7;
646
647         while (scale > 4096 && size_dst(src, scale) < *dst)
648                 scale -= 8;
649
650         *dst = size_dst(src, scale);
651
652         return scale;
653 }
654
655 /* rect is guaranteed to not exceed the scaled camera rectangle */
656 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
657 {
658         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
659         struct sh_mobile_ceu_cam *cam = icd->host_priv;
660         struct sh_mobile_ceu_dev *pcdev = ici->priv;
661         unsigned int height, width, cdwdr_width, in_width, in_height;
662         unsigned int left_offset, top_offset;
663         u32 camor;
664
665         dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
666                 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
667
668         left_offset     = cam->ceu_left;
669         top_offset      = cam->ceu_top;
670
671         WARN_ON(icd->user_width & 3 || icd->user_height & 3);
672
673         width = icd->user_width;
674
675         if (pcdev->image_mode) {
676                 in_width = cam->width;
677                 if (!pcdev->is_16bit) {
678                         in_width *= 2;
679                         left_offset *= 2;
680                 }
681         } else {
682                 unsigned int w_factor;
683
684                 switch (icd->current_fmt->host_fmt->packing) {
685                 case SOC_MBUS_PACKING_2X8_PADHI:
686                         w_factor = 2;
687                         break;
688                 default:
689                         w_factor = 1;
690                 }
691
692                 in_width = cam->width * w_factor;
693                 left_offset *= w_factor;
694         }
695
696         cdwdr_width = icd->bytesperline;
697
698         height = icd->user_height;
699         in_height = cam->height;
700         if (V4L2_FIELD_NONE != pcdev->field) {
701                 height = (height / 2) & ~3;
702                 in_height /= 2;
703                 top_offset /= 2;
704                 cdwdr_width *= 2;
705         }
706
707         /* CSI2 special configuration */
708         if (csi2_subdev(pcdev, icd)) {
709                 in_width = ((in_width - 2) * 2);
710                 left_offset *= 2;
711         }
712
713         /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
714         camor = left_offset | (top_offset << 16);
715
716         dev_geo(icd->parent,
717                 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
718                 (in_height << 16) | in_width, (height << 16) | width,
719                 cdwdr_width);
720
721         ceu_write(pcdev, CAMOR, camor);
722         ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
723         /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
724         ceu_write(pcdev, CFSZR, (height << 16) | width);
725         ceu_write(pcdev, CDWDR, cdwdr_width);
726 }
727
728 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
729 {
730         u32 capsr = ceu_read(pcdev, CAPSR);
731         ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
732         return capsr;
733 }
734
735 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
736 {
737         unsigned long timeout = jiffies + 10 * HZ;
738
739         /*
740          * Wait until the end of the current frame. It can take a long time,
741          * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
742          */
743         while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
744                 msleep(1);
745
746         if (time_after(jiffies, timeout)) {
747                 dev_err(pcdev->ici.v4l2_dev.dev,
748                         "Timeout waiting for frame end! Interface problem?\n");
749                 return;
750         }
751
752         /* Wait until reset clears, this shall not hang... */
753         while (ceu_read(pcdev, CAPSR) & (1 << 16))
754                 udelay(10);
755
756         /* Anything to restore? */
757         if (capsr & ~(1 << 16))
758                 ceu_write(pcdev, CAPSR, capsr);
759 }
760
761 /* Find the bus subdevice driver, e.g., CSI2 */
762 static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev,
763                                            struct soc_camera_device *icd)
764 {
765         return csi2_subdev(pcdev, icd) ? : soc_camera_to_subdev(icd);
766 }
767
768 #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER |       \
769                 V4L2_MBUS_PCLK_SAMPLE_RISING |  \
770                 V4L2_MBUS_HSYNC_ACTIVE_HIGH |   \
771                 V4L2_MBUS_HSYNC_ACTIVE_LOW |    \
772                 V4L2_MBUS_VSYNC_ACTIVE_HIGH |   \
773                 V4L2_MBUS_VSYNC_ACTIVE_LOW |    \
774                 V4L2_MBUS_DATA_ACTIVE_HIGH)
775
776 /* Capture is not running, no interrupts, no locking needed */
777 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
778 {
779         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
780         struct sh_mobile_ceu_dev *pcdev = ici->priv;
781         struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
782         struct sh_mobile_ceu_cam *cam = icd->host_priv;
783         struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
784         unsigned long value, common_flags = CEU_BUS_FLAGS;
785         u32 capsr = capture_save_reset(pcdev);
786         unsigned int yuv_lineskip;
787         int ret;
788
789         /*
790          * If the client doesn't implement g_mbus_config, we just use our
791          * platform data
792          */
793         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
794         if (!ret) {
795                 common_flags = soc_mbus_config_compatible(&cfg,
796                                                           common_flags);
797                 if (!common_flags)
798                         return -EINVAL;
799         } else if (ret != -ENOIOCTLCMD) {
800                 return ret;
801         }
802
803         /* Make choises, based on platform preferences */
804         if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
805             (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
806                 if (pcdev->flags & SH_CEU_FLAG_HSYNC_LOW)
807                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
808                 else
809                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
810         }
811
812         if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
813             (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
814                 if (pcdev->flags & SH_CEU_FLAG_VSYNC_LOW)
815                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
816                 else
817                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
818         }
819
820         cfg.flags = common_flags;
821         ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
822         if (ret < 0 && ret != -ENOIOCTLCMD)
823                 return ret;
824
825         if (icd->current_fmt->host_fmt->bits_per_sample > 8)
826                 pcdev->is_16bit = 1;
827         else
828                 pcdev->is_16bit = 0;
829
830         ceu_write(pcdev, CRCNTR, 0);
831         ceu_write(pcdev, CRCMPR, 0);
832
833         value = 0x00000010; /* data fetch by default */
834         yuv_lineskip = 0x10;
835
836         switch (icd->current_fmt->host_fmt->fourcc) {
837         case V4L2_PIX_FMT_NV12:
838         case V4L2_PIX_FMT_NV21:
839                 /* convert 4:2:2 -> 4:2:0 */
840                 yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */
841                 /* fall-through */
842         case V4L2_PIX_FMT_NV16:
843         case V4L2_PIX_FMT_NV61:
844                 switch (cam->code) {
845                 case MEDIA_BUS_FMT_UYVY8_2X8:
846                         value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
847                         break;
848                 case MEDIA_BUS_FMT_VYUY8_2X8:
849                         value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
850                         break;
851                 case MEDIA_BUS_FMT_YUYV8_2X8:
852                         value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
853                         break;
854                 case MEDIA_BUS_FMT_YVYU8_2X8:
855                         value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
856                         break;
857                 default:
858                         BUG();
859                 }
860         }
861
862         if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
863             icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
864                 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
865
866         value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
867         value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
868
869         if (csi2_subdev(pcdev, icd)) /* CSI2 mode */
870                 value |= 3 << 12;
871         else if (pcdev->is_16bit)
872                 value |= 1 << 12;
873         else if (pcdev->flags & SH_CEU_FLAG_LOWER_8BIT)
874                 value |= 2 << 12;
875
876         ceu_write(pcdev, CAMCR, value);
877
878         ceu_write(pcdev, CAPCR, 0x00300000);
879
880         switch (pcdev->field) {
881         case V4L2_FIELD_INTERLACED_TB:
882                 value = 0x101;
883                 break;
884         case V4L2_FIELD_INTERLACED_BT:
885                 value = 0x102;
886                 break;
887         default:
888                 value = 0;
889                 break;
890         }
891         ceu_write(pcdev, CAIFR, value);
892
893         sh_mobile_ceu_set_rect(icd);
894         mdelay(1);
895
896         dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
897         ceu_write(pcdev, CFLCR, pcdev->cflcr);
898
899         /*
900          * A few words about byte order (observed in Big Endian mode)
901          *
902          * In data fetch mode bytes are received in chunks of 8 bytes.
903          * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
904          *
905          * The data is however by default written to memory in reverse order:
906          * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
907          *
908          * The lowest three bits of CDOCR allows us to do swapping,
909          * using 7 we swap the data bytes to match the incoming order:
910          * D0, D1, D2, D3, D4, D5, D6, D7
911          */
912         value = 0x00000007 | yuv_lineskip;
913
914         ceu_write(pcdev, CDOCR, value);
915         ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
916
917         capture_restore(pcdev, capsr);
918
919         /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
920         return 0;
921 }
922
923 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
924                                        unsigned char buswidth)
925 {
926         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
927         struct sh_mobile_ceu_dev *pcdev = ici->priv;
928         struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
929         unsigned long common_flags = CEU_BUS_FLAGS;
930         struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
931         int ret;
932
933         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
934         if (!ret)
935                 common_flags = soc_mbus_config_compatible(&cfg,
936                                                           common_flags);
937         else if (ret != -ENOIOCTLCMD)
938                 return ret;
939
940         if (!common_flags || buswidth > 16)
941                 return -EINVAL;
942
943         return 0;
944 }
945
946 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
947         {
948                 .fourcc                 = V4L2_PIX_FMT_NV12,
949                 .name                   = "NV12",
950                 .bits_per_sample        = 8,
951                 .packing                = SOC_MBUS_PACKING_1_5X8,
952                 .order                  = SOC_MBUS_ORDER_LE,
953                 .layout                 = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
954         }, {
955                 .fourcc                 = V4L2_PIX_FMT_NV21,
956                 .name                   = "NV21",
957                 .bits_per_sample        = 8,
958                 .packing                = SOC_MBUS_PACKING_1_5X8,
959                 .order                  = SOC_MBUS_ORDER_LE,
960                 .layout                 = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
961         }, {
962                 .fourcc                 = V4L2_PIX_FMT_NV16,
963                 .name                   = "NV16",
964                 .bits_per_sample        = 8,
965                 .packing                = SOC_MBUS_PACKING_2X8_PADHI,
966                 .order                  = SOC_MBUS_ORDER_LE,
967                 .layout                 = SOC_MBUS_LAYOUT_PLANAR_Y_C,
968         }, {
969                 .fourcc                 = V4L2_PIX_FMT_NV61,
970                 .name                   = "NV61",
971                 .bits_per_sample        = 8,
972                 .packing                = SOC_MBUS_PACKING_2X8_PADHI,
973                 .order                  = SOC_MBUS_ORDER_LE,
974                 .layout                 = SOC_MBUS_LAYOUT_PLANAR_Y_C,
975         },
976 };
977
978 /* This will be corrected as we get more formats */
979 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
980 {
981         return  fmt->packing == SOC_MBUS_PACKING_NONE ||
982                 (fmt->bits_per_sample == 8 &&
983                  fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
984                 (fmt->bits_per_sample == 8 &&
985                  fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
986                 (fmt->bits_per_sample > 8 &&
987                  fmt->packing == SOC_MBUS_PACKING_EXTEND16);
988 }
989
990 static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
991 {
992         return container_of(ctrl->handler, struct soc_camera_device,
993                                                         ctrl_handler);
994 }
995
996 static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
997 {
998         struct soc_camera_device *icd = ctrl_to_icd(ctrl);
999         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1000         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1001
1002         switch (ctrl->id) {
1003         case V4L2_CID_SHARPNESS:
1004                 switch (icd->current_fmt->host_fmt->fourcc) {
1005                 case V4L2_PIX_FMT_NV12:
1006                 case V4L2_PIX_FMT_NV21:
1007                 case V4L2_PIX_FMT_NV16:
1008                 case V4L2_PIX_FMT_NV61:
1009                         ceu_write(pcdev, CLFCR, !ctrl->val);
1010                         return 0;
1011                 }
1012                 break;
1013         }
1014
1015         return -EINVAL;
1016 }
1017
1018 static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
1019         .s_ctrl = sh_mobile_ceu_s_ctrl,
1020 };
1021
1022 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
1023                                      struct soc_camera_format_xlate *xlate)
1024 {
1025         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1026         struct device *dev = icd->parent;
1027         struct soc_camera_host *ici = to_soc_camera_host(dev);
1028         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1029         int ret, k, n;
1030         int formats = 0;
1031         struct sh_mobile_ceu_cam *cam;
1032         struct v4l2_subdev_mbus_code_enum code = {
1033                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1034                 .index = idx,
1035         };
1036         const struct soc_mbus_pixelfmt *fmt;
1037
1038         ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
1039         if (ret < 0)
1040                 /* No more formats */
1041                 return 0;
1042
1043         fmt = soc_mbus_get_fmtdesc(code.code);
1044         if (!fmt) {
1045                 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code.code);
1046                 return 0;
1047         }
1048
1049         if (!csi2_subdev(pcdev, icd)) {
1050                 /* Are there any restrictions in the CSI-2 case? */
1051                 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
1052                 if (ret < 0)
1053                         return 0;
1054         }
1055
1056         if (!icd->host_priv) {
1057                 struct v4l2_subdev_format fmt = {
1058                         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1059                 };
1060                 struct v4l2_mbus_framefmt *mf = &fmt.format;
1061                 struct v4l2_rect rect;
1062                 int shift = 0;
1063
1064                 /* Add our control */
1065                 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
1066                                   V4L2_CID_SHARPNESS, 0, 1, 1, 1);
1067                 if (icd->ctrl_handler.error)
1068                         return icd->ctrl_handler.error;
1069
1070                 /* FIXME: subwindow is lost between close / open */
1071
1072                 /* Cache current client geometry */
1073                 ret = soc_camera_client_g_rect(sd, &rect);
1074                 if (ret < 0)
1075                         return ret;
1076
1077                 /* First time */
1078                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1079                 if (ret < 0)
1080                         return ret;
1081
1082                 /*
1083                  * All currently existing CEU implementations support 2560x1920
1084                  * or larger frames. If the sensor is proposing too big a frame,
1085                  * don't bother with possibly supportred by the CEU larger
1086                  * sizes, just try VGA multiples. If needed, this can be
1087                  * adjusted in the future.
1088                  */
1089                 while ((mf->width > pcdev->max_width ||
1090                         mf->height > pcdev->max_height) && shift < 4) {
1091                         /* Try 2560x1920, 1280x960, 640x480, 320x240 */
1092                         mf->width       = 2560 >> shift;
1093                         mf->height      = 1920 >> shift;
1094                         ret = v4l2_device_call_until_err(sd->v4l2_dev,
1095                                         soc_camera_grp_id(icd), pad,
1096                                         set_fmt, NULL, &fmt);
1097                         if (ret < 0)
1098                                 return ret;
1099                         shift++;
1100                 }
1101
1102                 if (shift == 4) {
1103                         dev_err(dev, "Failed to configure the client below %ux%x\n",
1104                                 mf->width, mf->height);
1105                         return -EIO;
1106                 }
1107
1108                 dev_geo(dev, "camera fmt %ux%u\n", mf->width, mf->height);
1109
1110                 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1111                 if (!cam)
1112                         return -ENOMEM;
1113
1114                 /* We are called with current camera crop, initialise subrect with it */
1115                 cam->rect       = rect;
1116                 cam->subrect    = rect;
1117
1118                 cam->width      = mf->width;
1119                 cam->height     = mf->height;
1120
1121                 icd->host_priv = cam;
1122         } else {
1123                 cam = icd->host_priv;
1124         }
1125
1126         /* Beginning of a pass */
1127         if (!idx)
1128                 cam->extra_fmt = NULL;
1129
1130         switch (code.code) {
1131         case MEDIA_BUS_FMT_UYVY8_2X8:
1132         case MEDIA_BUS_FMT_VYUY8_2X8:
1133         case MEDIA_BUS_FMT_YUYV8_2X8:
1134         case MEDIA_BUS_FMT_YVYU8_2X8:
1135                 if (cam->extra_fmt)
1136                         break;
1137
1138                 /*
1139                  * Our case is simple so far: for any of the above four camera
1140                  * formats we add all our four synthesized NV* formats, so,
1141                  * just marking the device with a single flag suffices. If
1142                  * the format generation rules are more complex, you would have
1143                  * to actually hang your already added / counted formats onto
1144                  * the host_priv pointer and check whether the format you're
1145                  * going to add now is already there.
1146                  */
1147                 cam->extra_fmt = sh_mobile_ceu_formats;
1148
1149                 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1150                 formats += n;
1151                 for (k = 0; xlate && k < n; k++) {
1152                         xlate->host_fmt = &sh_mobile_ceu_formats[k];
1153                         xlate->code     = code.code;
1154                         xlate++;
1155                         dev_dbg(dev, "Providing format %s using code %d\n",
1156                                 sh_mobile_ceu_formats[k].name, code.code);
1157                 }
1158                 break;
1159         default:
1160                 if (!sh_mobile_ceu_packing_supported(fmt))
1161                         return 0;
1162         }
1163
1164         /* Generic pass-through */
1165         formats++;
1166         if (xlate) {
1167                 xlate->host_fmt = fmt;
1168                 xlate->code     = code.code;
1169                 xlate++;
1170                 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1171                         fmt->name);
1172         }
1173
1174         return formats;
1175 }
1176
1177 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1178 {
1179         kfree(icd->host_priv);
1180         icd->host_priv = NULL;
1181 }
1182
1183 #define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale)
1184 #define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out)
1185
1186 /*
1187  * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1188  * framerate by always requesting the maximum image from the client. See
1189  * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1190  * scaling and cropping algorithms and for the meaning of referenced here steps.
1191  */
1192 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1193                                   const struct v4l2_crop *a)
1194 {
1195         struct v4l2_crop a_writable = *a;
1196         const struct v4l2_rect *rect = &a_writable.c;
1197         struct device *dev = icd->parent;
1198         struct soc_camera_host *ici = to_soc_camera_host(dev);
1199         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1200         struct v4l2_crop cam_crop;
1201         struct sh_mobile_ceu_cam *cam = icd->host_priv;
1202         struct v4l2_rect *cam_rect = &cam_crop.c;
1203         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1204         struct v4l2_subdev_format fmt = {
1205                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1206         };
1207         struct v4l2_mbus_framefmt *mf = &fmt.format;
1208         unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1209                 out_width, out_height;
1210         int interm_width, interm_height;
1211         u32 capsr, cflcr;
1212         int ret;
1213
1214         dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1215                 rect->left, rect->top);
1216
1217         /* During camera cropping its output window can change too, stop CEU */
1218         capsr = capture_save_reset(pcdev);
1219         dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1220
1221         /*
1222          * 1. - 2. Apply iterative camera S_CROP for new input window, read back
1223          * actual camera rectangle.
1224          */
1225         ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
1226                                        &cam->rect, &cam->subrect);
1227         if (ret < 0)
1228                 return ret;
1229
1230         dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
1231                 cam_rect->width, cam_rect->height,
1232                 cam_rect->left, cam_rect->top);
1233
1234         /* On success cam_crop contains current camera crop */
1235
1236         /* 3. Retrieve camera output window */
1237         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1238         if (ret < 0)
1239                 return ret;
1240
1241         if (mf->width > pcdev->max_width || mf->height > pcdev->max_height)
1242                 return -EINVAL;
1243
1244         /* 4. Calculate camera scales */
1245         scale_cam_h     = calc_generic_scale(cam_rect->width, mf->width);
1246         scale_cam_v     = calc_generic_scale(cam_rect->height, mf->height);
1247
1248         /* Calculate intermediate window */
1249         interm_width    = scale_down(rect->width, scale_cam_h);
1250         interm_height   = scale_down(rect->height, scale_cam_v);
1251
1252         if (interm_width < icd->user_width) {
1253                 u32 new_scale_h;
1254
1255                 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1256
1257                 mf->width = scale_down(cam_rect->width, new_scale_h);
1258         }
1259
1260         if (interm_height < icd->user_height) {
1261                 u32 new_scale_v;
1262
1263                 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1264
1265                 mf->height = scale_down(cam_rect->height, new_scale_v);
1266         }
1267
1268         if (interm_width < icd->user_width || interm_height < icd->user_height) {
1269                 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1270                                         soc_camera_grp_id(icd), pad,
1271                                         set_fmt, NULL, &fmt);
1272                 if (ret < 0)
1273                         return ret;
1274
1275                 dev_geo(dev, "New camera output %ux%u\n", mf->width, mf->height);
1276                 scale_cam_h     = calc_generic_scale(cam_rect->width, mf->width);
1277                 scale_cam_v     = calc_generic_scale(cam_rect->height, mf->height);
1278                 interm_width    = scale_down(rect->width, scale_cam_h);
1279                 interm_height   = scale_down(rect->height, scale_cam_v);
1280         }
1281
1282         /* Cache camera output window */
1283         cam->width      = mf->width;
1284         cam->height     = mf->height;
1285
1286         if (pcdev->image_mode) {
1287                 out_width       = min(interm_width, icd->user_width);
1288                 out_height      = min(interm_height, icd->user_height);
1289         } else {
1290                 out_width       = interm_width;
1291                 out_height      = interm_height;
1292         }
1293
1294         /*
1295          * 5. Calculate CEU scales from camera scales from results of (5) and
1296          *    the user window
1297          */
1298         scale_ceu_h     = calc_scale(interm_width, &out_width);
1299         scale_ceu_v     = calc_scale(interm_height, &out_height);
1300
1301         dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1302
1303         /* Apply CEU scales. */
1304         cflcr = scale_ceu_h | (scale_ceu_v << 16);
1305         if (cflcr != pcdev->cflcr) {
1306                 pcdev->cflcr = cflcr;
1307                 ceu_write(pcdev, CFLCR, cflcr);
1308         }
1309
1310         icd->user_width  = out_width & ~3;
1311         icd->user_height = out_height & ~3;
1312         /* Offsets are applied at the CEU scaling filter input */
1313         cam->ceu_left    = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1314         cam->ceu_top     = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
1315
1316         /* 6. Use CEU cropping to crop to the new window. */
1317         sh_mobile_ceu_set_rect(icd);
1318
1319         cam->subrect = *rect;
1320
1321         dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1322                 icd->user_width, icd->user_height,
1323                 cam->ceu_left, cam->ceu_top);
1324
1325         /* Restore capture. The CE bit can be cleared by the hardware */
1326         if (pcdev->active)
1327                 capsr |= 1;
1328         capture_restore(pcdev, capsr);
1329
1330         /* Even if only camera cropping succeeded */
1331         return ret;
1332 }
1333
1334 static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1335                                   struct v4l2_crop *a)
1336 {
1337         struct sh_mobile_ceu_cam *cam = icd->host_priv;
1338
1339         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1340         a->c = cam->subrect;
1341
1342         return 0;
1343 }
1344
1345 /* Similar to set_crop multistage iterative algorithm */
1346 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1347                                  struct v4l2_format *f)
1348 {
1349         struct device *dev = icd->parent;
1350         struct soc_camera_host *ici = to_soc_camera_host(dev);
1351         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1352         struct sh_mobile_ceu_cam *cam = icd->host_priv;
1353         struct v4l2_pix_format *pix = &f->fmt.pix;
1354         struct v4l2_mbus_framefmt mf;
1355         __u32 pixfmt = pix->pixelformat;
1356         const struct soc_camera_format_xlate *xlate;
1357         unsigned int ceu_sub_width = pcdev->max_width,
1358                 ceu_sub_height = pcdev->max_height;
1359         u16 scale_v, scale_h;
1360         int ret;
1361         bool image_mode;
1362         enum v4l2_field field;
1363
1364         switch (pix->field) {
1365         default:
1366                 pix->field = V4L2_FIELD_NONE;
1367                 /* fall-through */
1368         case V4L2_FIELD_INTERLACED_TB:
1369         case V4L2_FIELD_INTERLACED_BT:
1370         case V4L2_FIELD_NONE:
1371                 field = pix->field;
1372                 break;
1373         case V4L2_FIELD_INTERLACED:
1374                 field = V4L2_FIELD_INTERLACED_TB;
1375                 break;
1376         }
1377
1378         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1379         if (!xlate) {
1380                 dev_warn(dev, "Format %x not found\n", pixfmt);
1381                 return -EINVAL;
1382         }
1383
1384         /* 1.-4. Calculate desired client output geometry */
1385         soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf, 12);
1386         mf.field        = pix->field;
1387         mf.colorspace   = pix->colorspace;
1388         mf.code         = xlate->code;
1389
1390         switch (pixfmt) {
1391         case V4L2_PIX_FMT_NV12:
1392         case V4L2_PIX_FMT_NV21:
1393         case V4L2_PIX_FMT_NV16:
1394         case V4L2_PIX_FMT_NV61:
1395                 image_mode = true;
1396                 break;
1397         default:
1398                 image_mode = false;
1399         }
1400
1401         dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
1402                 pix->width, pix->height);
1403
1404         dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
1405
1406         /* 5. - 9. */
1407         ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1408                                 &mf, &ceu_sub_width, &ceu_sub_height,
1409                                 image_mode && V4L2_FIELD_NONE == field, 12);
1410
1411         dev_geo(dev, "5-9: client scale return %d\n", ret);
1412
1413         /* Done with the camera. Now see if we can improve the result */
1414
1415         dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1416                 mf.width, mf.height, pix->width, pix->height);
1417         if (ret < 0)
1418                 return ret;
1419
1420         if (mf.code != xlate->code)
1421                 return -EINVAL;
1422
1423         /* 9. Prepare CEU crop */
1424         cam->width = mf.width;
1425         cam->height = mf.height;
1426
1427         /* 10. Use CEU scaling to scale to the requested user window. */
1428
1429         /* We cannot scale up */
1430         if (pix->width > ceu_sub_width)
1431                 ceu_sub_width = pix->width;
1432
1433         if (pix->height > ceu_sub_height)
1434                 ceu_sub_height = pix->height;
1435
1436         pix->colorspace = mf.colorspace;
1437
1438         if (image_mode) {
1439                 /* Scale pix->{width x height} down to width x height */
1440                 scale_h         = calc_scale(ceu_sub_width, &pix->width);
1441                 scale_v         = calc_scale(ceu_sub_height, &pix->height);
1442         } else {
1443                 pix->width      = ceu_sub_width;
1444                 pix->height     = ceu_sub_height;
1445                 scale_h         = 0;
1446                 scale_v         = 0;
1447         }
1448
1449         pcdev->cflcr = scale_h | (scale_v << 16);
1450
1451         /*
1452          * We have calculated CFLCR, the actual configuration will be performed
1453          * in sh_mobile_ceu_set_bus_param()
1454          */
1455
1456         dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1457                 ceu_sub_width, scale_h, pix->width,
1458                 ceu_sub_height, scale_v, pix->height);
1459
1460         cam->code               = xlate->code;
1461         icd->current_fmt        = xlate;
1462
1463         pcdev->field = field;
1464         pcdev->image_mode = image_mode;
1465
1466         /* CFSZR requirement */
1467         pix->width      &= ~3;
1468         pix->height     &= ~3;
1469
1470         return 0;
1471 }
1472
1473 #define CEU_CHDW_MAX    8188U   /* Maximum line stride */
1474
1475 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1476                                  struct v4l2_format *f)
1477 {
1478         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1479         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1480         const struct soc_camera_format_xlate *xlate;
1481         struct v4l2_pix_format *pix = &f->fmt.pix;
1482         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1483         struct v4l2_subdev_pad_config pad_cfg;
1484         struct v4l2_subdev_format format = {
1485                 .which = V4L2_SUBDEV_FORMAT_TRY,
1486         };
1487         struct v4l2_mbus_framefmt *mf = &format.format;
1488         __u32 pixfmt = pix->pixelformat;
1489         int width, height;
1490         int ret;
1491
1492         dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1493                  pixfmt, pix->width, pix->height);
1494
1495         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1496         if (!xlate) {
1497                 xlate = icd->current_fmt;
1498                 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1499                         pixfmt, xlate->host_fmt->fourcc);
1500                 pixfmt = xlate->host_fmt->fourcc;
1501                 pix->pixelformat = pixfmt;
1502                 pix->colorspace = icd->colorspace;
1503         }
1504
1505         /* FIXME: calculate using depth and bus width */
1506
1507         /* CFSZR requires height and width to be 4-pixel aligned */
1508         v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
1509                               &pix->height, 4, pcdev->max_height, 2, 0);
1510
1511         width = pix->width;
1512         height = pix->height;
1513
1514         /* limit to sensor capabilities */
1515         mf->width       = pix->width;
1516         mf->height      = pix->height;
1517         mf->field       = pix->field;
1518         mf->code        = xlate->code;
1519         mf->colorspace  = pix->colorspace;
1520
1521         ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1522                                          pad, set_fmt, &pad_cfg, &format);
1523         if (ret < 0)
1524                 return ret;
1525
1526         pix->width      = mf->width;
1527         pix->height     = mf->height;
1528         pix->field      = mf->field;
1529         pix->colorspace = mf->colorspace;
1530
1531         switch (pixfmt) {
1532         case V4L2_PIX_FMT_NV12:
1533         case V4L2_PIX_FMT_NV21:
1534         case V4L2_PIX_FMT_NV16:
1535         case V4L2_PIX_FMT_NV61:
1536                 /* FIXME: check against rect_max after converting soc-camera */
1537                 /* We can scale precisely, need a bigger image from camera */
1538                 if (pix->width < width || pix->height < height) {
1539                         /*
1540                          * We presume, the sensor behaves sanely, i.e., if
1541                          * requested a bigger rectangle, it will not return a
1542                          * smaller one.
1543                          */
1544                         mf->width = pcdev->max_width;
1545                         mf->height = pcdev->max_height;
1546                         ret = v4l2_device_call_until_err(sd->v4l2_dev,
1547                                         soc_camera_grp_id(icd), pad,
1548                                         set_fmt, &pad_cfg, &format);
1549                         if (ret < 0) {
1550                                 /* Shouldn't actually happen... */
1551                                 dev_err(icd->parent,
1552                                         "FIXME: client try_fmt() = %d\n", ret);
1553                                 return ret;
1554                         }
1555                 }
1556                 /* We will scale exactly */
1557                 if (mf->width > width)
1558                         pix->width = width;
1559                 if (mf->height > height)
1560                         pix->height = height;
1561
1562                 pix->bytesperline = max(pix->bytesperline, pix->width);
1563                 pix->bytesperline = min(pix->bytesperline, CEU_CHDW_MAX);
1564                 pix->bytesperline &= ~3;
1565                 break;
1566
1567         default:
1568                 /* Configurable stride isn't supported in pass-through mode. */
1569                 pix->bytesperline  = 0;
1570         }
1571
1572         pix->width      &= ~3;
1573         pix->height     &= ~3;
1574         pix->sizeimage  = 0;
1575
1576         dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1577                 __func__, ret, pix->pixelformat, pix->width, pix->height);
1578
1579         return ret;
1580 }
1581
1582 static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1583                                       const struct v4l2_crop *a)
1584 {
1585         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1586         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1587         struct sh_mobile_ceu_dev *pcdev = ici->priv;
1588         u32 out_width = icd->user_width, out_height = icd->user_height;
1589         int ret;
1590
1591         /* Freeze queue */
1592         pcdev->frozen = 1;
1593         /* Wait for frame */
1594         ret = wait_for_completion_interruptible(&pcdev->complete);
1595         /* Stop the client */
1596         ret = v4l2_subdev_call(sd, video, s_stream, 0);
1597         if (ret < 0)
1598                 dev_warn(icd->parent,
1599                          "Client failed to stop the stream: %d\n", ret);
1600         else
1601                 /* Do the crop, if it fails, there's nothing more we can do */
1602                 sh_mobile_ceu_set_crop(icd, a);
1603
1604         dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1605
1606         if (icd->user_width != out_width || icd->user_height != out_height) {
1607                 struct v4l2_format f = {
1608                         .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1609                         .fmt.pix        = {
1610                                 .width          = out_width,
1611                                 .height         = out_height,
1612                                 .pixelformat    = icd->current_fmt->host_fmt->fourcc,
1613                                 .field          = pcdev->field,
1614                                 .colorspace     = icd->colorspace,
1615                         },
1616                 };
1617                 ret = sh_mobile_ceu_set_fmt(icd, &f);
1618                 if (!ret && (out_width != f.fmt.pix.width ||
1619                              out_height != f.fmt.pix.height))
1620                         ret = -EINVAL;
1621                 if (!ret) {
1622                         icd->user_width         = out_width & ~3;
1623                         icd->user_height        = out_height & ~3;
1624                         ret = sh_mobile_ceu_set_bus_param(icd);
1625                 }
1626         }
1627
1628         /* Thaw the queue */
1629         pcdev->frozen = 0;
1630         spin_lock_irq(&pcdev->lock);
1631         sh_mobile_ceu_capture(pcdev);
1632         spin_unlock_irq(&pcdev->lock);
1633         /* Start the client */
1634         ret = v4l2_subdev_call(sd, video, s_stream, 1);
1635         return ret;
1636 }
1637
1638 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1639 {
1640         struct soc_camera_device *icd = file->private_data;
1641
1642         return vb2_poll(&icd->vb2_vidq, file, pt);
1643 }
1644
1645 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1646                                   struct v4l2_capability *cap)
1647 {
1648         strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1649         strlcpy(cap->driver, "sh_mobile_ceu", sizeof(cap->driver));
1650         strlcpy(cap->bus_info, "platform:sh_mobile_ceu", sizeof(cap->bus_info));
1651         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1652         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1653
1654         return 0;
1655 }
1656
1657 static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1658                                        struct soc_camera_device *icd)
1659 {
1660         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1661
1662         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1663         q->io_modes = VB2_MMAP | VB2_USERPTR;
1664         q->drv_priv = icd;
1665         q->ops = &sh_mobile_ceu_videobuf_ops;
1666         q->mem_ops = &vb2_dma_contig_memops;
1667         q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
1668         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1669         q->lock = &ici->host_lock;
1670         q->dev = ici->v4l2_dev.dev;
1671
1672         return vb2_queue_init(q);
1673 }
1674
1675 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1676         .owner          = THIS_MODULE,
1677         .add            = sh_mobile_ceu_add_device,
1678         .remove         = sh_mobile_ceu_remove_device,
1679         .clock_start    = sh_mobile_ceu_clock_start,
1680         .clock_stop     = sh_mobile_ceu_clock_stop,
1681         .get_formats    = sh_mobile_ceu_get_formats,
1682         .put_formats    = sh_mobile_ceu_put_formats,
1683         .get_crop       = sh_mobile_ceu_get_crop,
1684         .set_crop       = sh_mobile_ceu_set_crop,
1685         .set_livecrop   = sh_mobile_ceu_set_livecrop,
1686         .set_fmt        = sh_mobile_ceu_set_fmt,
1687         .try_fmt        = sh_mobile_ceu_try_fmt,
1688         .poll           = sh_mobile_ceu_poll,
1689         .querycap       = sh_mobile_ceu_querycap,
1690         .set_bus_param  = sh_mobile_ceu_set_bus_param,
1691         .init_videobuf2 = sh_mobile_ceu_init_videobuf,
1692 };
1693
1694 struct bus_wait {
1695         struct notifier_block   notifier;
1696         struct completion       completion;
1697         struct device           *dev;
1698 };
1699
1700 static int bus_notify(struct notifier_block *nb,
1701                       unsigned long action, void *data)
1702 {
1703         struct device *dev = data;
1704         struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1705
1706         if (wait->dev != dev)
1707                 return NOTIFY_DONE;
1708
1709         switch (action) {
1710         case BUS_NOTIFY_UNBOUND_DRIVER:
1711                 /* Protect from module unloading */
1712                 wait_for_completion(&wait->completion);
1713                 return NOTIFY_OK;
1714         }
1715         return NOTIFY_DONE;
1716 }
1717
1718 static int sh_mobile_ceu_probe(struct platform_device *pdev)
1719 {
1720         struct sh_mobile_ceu_dev *pcdev;
1721         struct resource *res;
1722         void __iomem *base;
1723         unsigned int irq;
1724         int err, i;
1725         struct bus_wait wait = {
1726                 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1727                 .notifier.notifier_call = bus_notify,
1728         };
1729         struct sh_mobile_ceu_companion *csi2;
1730
1731         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1732         irq = platform_get_irq(pdev, 0);
1733         if (!res || (int)irq <= 0) {
1734                 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1735                 return -ENODEV;
1736         }
1737
1738         pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
1739         if (!pcdev) {
1740                 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1741                 return -ENOMEM;
1742         }
1743
1744         INIT_LIST_HEAD(&pcdev->capture);
1745         spin_lock_init(&pcdev->lock);
1746         init_completion(&pcdev->complete);
1747
1748         pcdev->pdata = pdev->dev.platform_data;
1749         if (!pcdev->pdata && !pdev->dev.of_node) {
1750                 dev_err(&pdev->dev, "CEU platform data not set.\n");
1751                 return -EINVAL;
1752         }
1753
1754         /* TODO: implement per-device bus flags */
1755         if (pcdev->pdata) {
1756                 pcdev->max_width = pcdev->pdata->max_width;
1757                 pcdev->max_height = pcdev->pdata->max_height;
1758                 pcdev->flags = pcdev->pdata->flags;
1759         }
1760         pcdev->field = V4L2_FIELD_NONE;
1761
1762         if (!pcdev->max_width) {
1763                 unsigned int v;
1764                 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-width", &v);
1765                 if (!err)
1766                         pcdev->max_width = v;
1767
1768                 if (!pcdev->max_width)
1769                         pcdev->max_width = 2560;
1770         }
1771         if (!pcdev->max_height) {
1772                 unsigned int v;
1773                 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-height", &v);
1774                 if (!err)
1775                         pcdev->max_height = v;
1776
1777                 if (!pcdev->max_height)
1778                         pcdev->max_height = 1920;
1779         }
1780
1781         base = devm_ioremap_resource(&pdev->dev, res);
1782         if (IS_ERR(base))
1783                 return PTR_ERR(base);
1784
1785         pcdev->irq = irq;
1786         pcdev->base = base;
1787         pcdev->video_limit = 0; /* only enabled if second resource exists */
1788
1789         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1790         if (res) {
1791                 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1792                                                   res->start,
1793                                                   resource_size(res),
1794                                                   DMA_MEMORY_MAP |
1795                                                   DMA_MEMORY_EXCLUSIVE);
1796                 if (!err) {
1797                         dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1798                         return -ENXIO;
1799                 }
1800
1801                 pcdev->video_limit = resource_size(res);
1802         }
1803
1804         /* request irq */
1805         err = devm_request_irq(&pdev->dev, pcdev->irq, sh_mobile_ceu_irq,
1806                                0, dev_name(&pdev->dev), pcdev);
1807         if (err) {
1808                 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1809                 goto exit_release_mem;
1810         }
1811
1812         pm_suspend_ignore_children(&pdev->dev, true);
1813         pm_runtime_enable(&pdev->dev);
1814         pm_runtime_resume(&pdev->dev);
1815
1816         pcdev->ici.priv = pcdev;
1817         pcdev->ici.v4l2_dev.dev = &pdev->dev;
1818         pcdev->ici.nr = pdev->id;
1819         pcdev->ici.drv_name = dev_name(&pdev->dev);
1820         pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1821         pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE;
1822
1823         if (pcdev->pdata && pcdev->pdata->asd_sizes) {
1824                 struct v4l2_async_subdev **asd;
1825                 char name[] = "sh-mobile-csi2";
1826                 int j;
1827
1828                 /*
1829                  * CSI2 interfacing: several groups can use CSI2, pick up the
1830                  * first one
1831                  */
1832                 asd = pcdev->pdata->asd;
1833                 for (j = 0; pcdev->pdata->asd_sizes[j]; j++) {
1834                         for (i = 0; i < pcdev->pdata->asd_sizes[j]; i++, asd++) {
1835                                 dev_dbg(&pdev->dev, "%s(): subdev #%d, type %u\n",
1836                                         __func__, i, (*asd)->match_type);
1837                                 if ((*asd)->match_type == V4L2_ASYNC_MATCH_DEVNAME &&
1838                                     !strncmp(name, (*asd)->match.device_name.name,
1839                                              sizeof(name) - 1)) {
1840                                         pcdev->csi2_asd = *asd;
1841                                         break;
1842                                 }
1843                         }
1844                         if (pcdev->csi2_asd)
1845                                 break;
1846                 }
1847
1848                 pcdev->ici.asd = pcdev->pdata->asd;
1849                 pcdev->ici.asd_sizes = pcdev->pdata->asd_sizes;
1850         }
1851
1852         /* Legacy CSI2 interfacing */
1853         csi2 = pcdev->pdata ? pcdev->pdata->csi2 : NULL;
1854         if (csi2) {
1855                 /*
1856                  * TODO: remove this once all users are converted to
1857                  * asynchronous CSI2 probing. If it has to be kept, csi2
1858                  * platform device resources have to be added, using
1859                  * platform_device_add_resources()
1860                  */
1861                 struct platform_device *csi2_pdev =
1862                         platform_device_alloc("sh-mobile-csi2", csi2->id);
1863                 struct sh_csi2_pdata *csi2_pdata = csi2->platform_data;
1864
1865                 if (!csi2_pdev) {
1866                         err = -ENOMEM;
1867                         goto exit_free_clk;
1868                 }
1869
1870                 pcdev->csi2_pdev                = csi2_pdev;
1871
1872                 err = platform_device_add_data(csi2_pdev, csi2_pdata,
1873                                                sizeof(*csi2_pdata));
1874                 if (err < 0)
1875                         goto exit_pdev_put;
1876
1877                 csi2_pdev->resource             = csi2->resource;
1878                 csi2_pdev->num_resources        = csi2->num_resources;
1879
1880                 err = platform_device_add(csi2_pdev);
1881                 if (err < 0)
1882                         goto exit_pdev_put;
1883
1884                 wait.dev = &csi2_pdev->dev;
1885
1886                 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
1887                 if (err < 0)
1888                         goto exit_pdev_unregister;
1889
1890                 /*
1891                  * From this point the driver module will not unload, until
1892                  * we complete the completion.
1893                  */
1894
1895                 if (!csi2_pdev->dev.driver) {
1896                         complete(&wait.completion);
1897                         /* Either too late, or probing failed */
1898                         bus_unregister_notifier(&platform_bus_type, &wait.notifier);
1899                         err = -ENXIO;
1900                         goto exit_pdev_unregister;
1901                 }
1902
1903                 /*
1904                  * The module is still loaded, in the worst case it is hanging
1905                  * in device release on our completion. So, _now_ dereferencing
1906                  * the "owner" is safe!
1907                  */
1908
1909                 err = try_module_get(csi2_pdev->dev.driver->owner);
1910
1911                 /* Let notifier complete, if it has been locked */
1912                 complete(&wait.completion);
1913                 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
1914                 if (!err) {
1915                         err = -ENODEV;
1916                         goto exit_pdev_unregister;
1917                 }
1918
1919                 pcdev->csi2_sd = platform_get_drvdata(csi2_pdev);
1920         }
1921
1922         err = soc_camera_host_register(&pcdev->ici);
1923         if (err)
1924                 goto exit_csi2_unregister;
1925
1926         if (csi2) {
1927                 err = v4l2_device_register_subdev(&pcdev->ici.v4l2_dev,
1928                                                   pcdev->csi2_sd);
1929                 dev_dbg(&pdev->dev, "%s(): ret(register_subdev) = %d\n",
1930                         __func__, err);
1931                 if (err < 0)
1932                         goto exit_host_unregister;
1933                 /* v4l2_device_register_subdev() took a reference too */
1934                 module_put(pcdev->csi2_sd->owner);
1935         }
1936
1937         return 0;
1938
1939 exit_host_unregister:
1940         soc_camera_host_unregister(&pcdev->ici);
1941 exit_csi2_unregister:
1942         if (csi2) {
1943                 module_put(pcdev->csi2_pdev->dev.driver->owner);
1944 exit_pdev_unregister:
1945                 platform_device_del(pcdev->csi2_pdev);
1946 exit_pdev_put:
1947                 pcdev->csi2_pdev->resource = NULL;
1948                 platform_device_put(pcdev->csi2_pdev);
1949         }
1950 exit_free_clk:
1951         pm_runtime_disable(&pdev->dev);
1952 exit_release_mem:
1953         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1954                 dma_release_declared_memory(&pdev->dev);
1955         return err;
1956 }
1957
1958 static int sh_mobile_ceu_remove(struct platform_device *pdev)
1959 {
1960         struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1961         struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1962                                         struct sh_mobile_ceu_dev, ici);
1963         struct platform_device *csi2_pdev = pcdev->csi2_pdev;
1964
1965         soc_camera_host_unregister(soc_host);
1966         pm_runtime_disable(&pdev->dev);
1967         if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1968                 dma_release_declared_memory(&pdev->dev);
1969         if (csi2_pdev && csi2_pdev->dev.driver) {
1970                 struct module *csi2_drv = csi2_pdev->dev.driver->owner;
1971                 platform_device_del(csi2_pdev);
1972                 csi2_pdev->resource = NULL;
1973                 platform_device_put(csi2_pdev);
1974                 module_put(csi2_drv);
1975         }
1976
1977         return 0;
1978 }
1979
1980 static int sh_mobile_ceu_runtime_nop(struct device *dev)
1981 {
1982         /* Runtime PM callback shared between ->runtime_suspend()
1983          * and ->runtime_resume(). Simply returns success.
1984          *
1985          * This driver re-initializes all registers after
1986          * pm_runtime_get_sync() anyway so there is no need
1987          * to save and restore registers here.
1988          */
1989         return 0;
1990 }
1991
1992 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1993         .runtime_suspend = sh_mobile_ceu_runtime_nop,
1994         .runtime_resume = sh_mobile_ceu_runtime_nop,
1995 };
1996
1997 static const struct of_device_id sh_mobile_ceu_of_match[] = {
1998         { .compatible = "renesas,sh-mobile-ceu" },
1999         { }
2000 };
2001 MODULE_DEVICE_TABLE(of, sh_mobile_ceu_of_match);
2002
2003 static struct platform_driver sh_mobile_ceu_driver = {
2004         .driver         = {
2005                 .name   = "sh_mobile_ceu",
2006                 .pm     = &sh_mobile_ceu_dev_pm_ops,
2007                 .of_match_table = sh_mobile_ceu_of_match,
2008         },
2009         .probe          = sh_mobile_ceu_probe,
2010         .remove         = sh_mobile_ceu_remove,
2011 };
2012
2013 static int __init sh_mobile_ceu_init(void)
2014 {
2015         /* Whatever return code */
2016         request_module("sh_mobile_csi2");
2017         return platform_driver_register(&sh_mobile_ceu_driver);
2018 }
2019
2020 static void __exit sh_mobile_ceu_exit(void)
2021 {
2022         platform_driver_unregister(&sh_mobile_ceu_driver);
2023 }
2024
2025 module_init(sh_mobile_ceu_init);
2026 module_exit(sh_mobile_ceu_exit);
2027
2028 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2029 MODULE_AUTHOR("Magnus Damm");
2030 MODULE_LICENSE("GPL");
2031 MODULE_VERSION("0.1.0");
2032 MODULE_ALIAS("platform:sh_mobile_ceu");