Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us