[media] media: imx: csi: increase burst size for YUV formats
[platform/kernel/linux-rpi.git] / drivers / staging / media / imx / imx-media-csi.c
1 /*
2  * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC
3  *
4  * Copyright (c) 2014-2017 Mentor Graphics Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/platform_device.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-mc.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <video/imx-ipu-v3.h>
24 #include <media/imx.h>
25 #include "imx-media.h"
26
27 /*
28  * Min/Max supported width and heights.
29  *
30  * We allow planar output, so we have to align width by 16 pixels
31  * to meet IDMAC alignment requirements.
32  *
33  * TODO: move this into pad format negotiation, if capture device
34  * has not requested planar formats, we should allow 8 pixel
35  * alignment.
36  */
37 #define MIN_W       176
38 #define MIN_H       144
39 #define MAX_W      4096
40 #define MAX_H      4096
41 #define W_ALIGN    4 /* multiple of 16 pixels */
42 #define H_ALIGN    1 /* multiple of 2 lines */
43 #define S_ALIGN    1 /* multiple of 2 */
44
45 struct csi_priv {
46         struct device *dev;
47         struct ipu_soc *ipu;
48         struct imx_media_dev *md;
49         struct v4l2_subdev sd;
50         struct media_pad pad[CSI_NUM_PADS];
51         /* the video device at IDMAC output pad */
52         struct imx_media_video_dev *vdev;
53         struct imx_media_fim *fim;
54         int csi_id;
55         int smfc_id;
56
57         /* lock to protect all members below */
58         struct mutex lock;
59
60         int active_output_pad;
61
62         struct ipuv3_channel *idmac_ch;
63         struct ipu_smfc *smfc;
64         struct ipu_csi *csi;
65
66         struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
67         const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
68         struct v4l2_fract frame_interval;
69         struct v4l2_rect crop;
70
71         /* active vb2 buffers to send to video dev sink */
72         struct imx_media_buffer *active_vb2_buf[2];
73         struct imx_media_dma_buf underrun_buf;
74
75         int ipu_buf_num;  /* ipu double buffer index: 0-1 */
76
77         /* the sink for the captured frames */
78         struct media_entity *sink;
79         enum ipu_csi_dest dest;
80         /* the source subdev */
81         struct v4l2_subdev *src_sd;
82
83         /* the mipi virtual channel number at link validate */
84         int vc_num;
85
86         /* the attached sensor at stream on */
87         struct imx_media_subdev *sensor;
88
89         spinlock_t irqlock; /* protect eof_irq handler */
90         struct timer_list eof_timeout_timer;
91         int eof_irq;
92         int nfb4eof_irq;
93
94         struct v4l2_ctrl_handler ctrl_hdlr;
95
96         int stream_count; /* streaming counter */
97         bool last_eof;   /* waiting for last EOF at stream off */
98         bool nfb4eof;    /* NFB4EOF encountered during streaming */
99         struct completion last_eof_comp;
100 };
101
102 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
103 {
104         return container_of(sdev, struct csi_priv, sd);
105 }
106
107 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
108 {
109         if (!IS_ERR_OR_NULL(priv->idmac_ch))
110                 ipu_idmac_put(priv->idmac_ch);
111         priv->idmac_ch = NULL;
112
113         if (!IS_ERR_OR_NULL(priv->smfc))
114                 ipu_smfc_put(priv->smfc);
115         priv->smfc = NULL;
116 }
117
118 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
119 {
120         int ch_num, ret;
121
122         ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
123
124         priv->smfc = ipu_smfc_get(priv->ipu, ch_num);
125         if (IS_ERR(priv->smfc)) {
126                 v4l2_err(&priv->sd, "failed to get SMFC\n");
127                 ret = PTR_ERR(priv->smfc);
128                 goto out;
129         }
130
131         priv->idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
132         if (IS_ERR(priv->idmac_ch)) {
133                 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
134                          ch_num);
135                 ret = PTR_ERR(priv->idmac_ch);
136                 goto out;
137         }
138
139         return 0;
140 out:
141         csi_idmac_put_ipu_resources(priv);
142         return ret;
143 }
144
145 static void csi_vb2_buf_done(struct csi_priv *priv)
146 {
147         struct imx_media_video_dev *vdev = priv->vdev;
148         struct imx_media_buffer *done, *next;
149         struct vb2_buffer *vb;
150         dma_addr_t phys;
151
152         done = priv->active_vb2_buf[priv->ipu_buf_num];
153         if (done) {
154                 vb = &done->vbuf.vb2_buf;
155                 vb->timestamp = ktime_get_ns();
156                 vb2_buffer_done(vb, priv->nfb4eof ?
157                                 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
158         }
159
160         priv->nfb4eof = false;
161
162         /* get next queued buffer */
163         next = imx_media_capture_device_next_buf(vdev);
164         if (next) {
165                 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
166                 priv->active_vb2_buf[priv->ipu_buf_num] = next;
167         } else {
168                 phys = priv->underrun_buf.phys;
169                 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
170         }
171
172         if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
173                 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
174
175         ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
176 }
177
178 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
179 {
180         struct csi_priv *priv = dev_id;
181
182         spin_lock(&priv->irqlock);
183
184         if (priv->last_eof) {
185                 complete(&priv->last_eof_comp);
186                 priv->last_eof = false;
187                 goto unlock;
188         }
189
190         if (priv->fim) {
191                 struct timespec cur_ts;
192
193                 ktime_get_ts(&cur_ts);
194                 /* call frame interval monitor */
195                 imx_media_fim_eof_monitor(priv->fim, &cur_ts);
196         }
197
198         csi_vb2_buf_done(priv);
199
200         /* select new IPU buf */
201         ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
202         /* toggle IPU double-buffer index */
203         priv->ipu_buf_num ^= 1;
204
205         /* bump the EOF timeout timer */
206         mod_timer(&priv->eof_timeout_timer,
207                   jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
208
209 unlock:
210         spin_unlock(&priv->irqlock);
211         return IRQ_HANDLED;
212 }
213
214 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
215 {
216         struct csi_priv *priv = dev_id;
217
218         spin_lock(&priv->irqlock);
219
220         /*
221          * this is not an unrecoverable error, just mark
222          * the next captured frame with vb2 error flag.
223          */
224         priv->nfb4eof = true;
225
226         v4l2_err(&priv->sd, "NFB4EOF\n");
227
228         spin_unlock(&priv->irqlock);
229
230         return IRQ_HANDLED;
231 }
232
233 /*
234  * EOF timeout timer function. This is an unrecoverable condition
235  * without a stream restart.
236  */
237 static void csi_idmac_eof_timeout(unsigned long data)
238 {
239         struct csi_priv *priv = (struct csi_priv *)data;
240         struct imx_media_video_dev *vdev = priv->vdev;
241
242         v4l2_err(&priv->sd, "EOF timeout\n");
243
244         /* signal a fatal error to capture device */
245         imx_media_capture_device_error(vdev);
246 }
247
248 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
249 {
250         struct imx_media_video_dev *vdev = priv->vdev;
251         struct imx_media_buffer *buf;
252         int i;
253
254         for (i = 0; i < 2; i++) {
255                 buf = imx_media_capture_device_next_buf(vdev);
256                 if (buf) {
257                         priv->active_vb2_buf[i] = buf;
258                         phys[i] = vb2_dma_contig_plane_dma_addr(
259                                 &buf->vbuf.vb2_buf, 0);
260                 } else {
261                         priv->active_vb2_buf[i] = NULL;
262                         phys[i] = priv->underrun_buf.phys;
263                 }
264         }
265 }
266
267 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
268                                       enum vb2_buffer_state return_status)
269 {
270         struct imx_media_buffer *buf;
271         int i;
272
273         /* return any remaining active frames with return_status */
274         for (i = 0; i < 2; i++) {
275                 buf = priv->active_vb2_buf[i];
276                 if (buf) {
277                         struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
278
279                         vb->timestamp = ktime_get_ns();
280                         vb2_buffer_done(vb, return_status);
281                 }
282         }
283 }
284
285 /* init the SMFC IDMAC channel */
286 static int csi_idmac_setup_channel(struct csi_priv *priv)
287 {
288         struct imx_media_video_dev *vdev = priv->vdev;
289         struct v4l2_fwnode_endpoint *sensor_ep;
290         struct v4l2_mbus_framefmt *infmt;
291         struct ipu_image image;
292         u32 passthrough_bits;
293         dma_addr_t phys[2];
294         bool passthrough;
295         u32 burst_size;
296         int ret;
297
298         infmt = &priv->format_mbus[CSI_SINK_PAD];
299         sensor_ep = &priv->sensor->sensor_ep;
300
301         ipu_cpmem_zero(priv->idmac_ch);
302
303         memset(&image, 0, sizeof(image));
304         image.pix = vdev->fmt.fmt.pix;
305         image.rect.width = image.pix.width;
306         image.rect.height = image.pix.height;
307
308         csi_idmac_setup_vb2_buf(priv, phys);
309
310         image.phys0 = phys[0];
311         image.phys1 = phys[1];
312
313         /*
314          * Check for conditions that require the IPU to handle the
315          * data internally as generic data, aka passthrough mode:
316          * - raw bayer formats
317          * - the sensor bus is 16-bit parallel
318          */
319         switch (image.pix.pixelformat) {
320         case V4L2_PIX_FMT_SBGGR8:
321         case V4L2_PIX_FMT_SGBRG8:
322         case V4L2_PIX_FMT_SGRBG8:
323         case V4L2_PIX_FMT_SRGGB8:
324                 burst_size = 8;
325                 passthrough = true;
326                 passthrough_bits = 8;
327                 break;
328         case V4L2_PIX_FMT_SBGGR16:
329         case V4L2_PIX_FMT_SGBRG16:
330         case V4L2_PIX_FMT_SGRBG16:
331         case V4L2_PIX_FMT_SRGGB16:
332                 burst_size = 4;
333                 passthrough = true;
334                 passthrough_bits = 16;
335                 break;
336         case V4L2_PIX_FMT_YUV420:
337         case V4L2_PIX_FMT_NV12:
338                 burst_size = (image.pix.width & 0x3f) ?
339                              ((image.pix.width & 0x1f) ?
340                               ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
341                 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
342                                sensor_ep->bus.parallel.bus_width >= 16);
343                 passthrough_bits = 16;
344                 break;
345         case V4L2_PIX_FMT_YUYV:
346         case V4L2_PIX_FMT_UYVY:
347                 burst_size = (image.pix.width & 0x1f) ?
348                              ((image.pix.width & 0xf) ? 8 : 16) : 32;
349                 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
350                                sensor_ep->bus.parallel.bus_width >= 16);
351                 passthrough_bits = 16;
352                 break;
353         default:
354                 burst_size = (image.pix.width & 0xf) ? 8 : 16;
355                 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
356                                sensor_ep->bus.parallel.bus_width >= 16);
357                 passthrough_bits = 16;
358                 break;
359         }
360
361         if (passthrough) {
362                 ipu_cpmem_set_resolution(priv->idmac_ch, image.rect.width,
363                                          image.rect.height);
364                 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
365                 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
366                 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
367                 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
368                                                  passthrough_bits);
369         } else {
370                 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
371                 if (ret)
372                         goto unsetup_vb2;
373         }
374
375         ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
376
377         /*
378          * Set the channel for the direct CSI-->memory via SMFC
379          * use-case to very high priority, by enabling the watermark
380          * signal in the SMFC, enabling WM in the channel, and setting
381          * the channel priority to high.
382          *
383          * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
384          * value.
385          *
386          * The WM's are set very low by intention here to ensure that
387          * the SMFC FIFOs do not overflow.
388          */
389         ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
390         ipu_cpmem_set_high_priority(priv->idmac_ch);
391         ipu_idmac_enable_watermark(priv->idmac_ch, true);
392         ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
393
394         burst_size = passthrough ?
395                 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
396
397         ipu_smfc_set_burstsize(priv->smfc, burst_size);
398
399         if (image.pix.field == V4L2_FIELD_NONE &&
400             V4L2_FIELD_HAS_BOTH(infmt->field))
401                 ipu_cpmem_interlaced_scan(priv->idmac_ch,
402                                           image.pix.bytesperline);
403
404         ipu_idmac_set_double_buffer(priv->idmac_ch, true);
405
406         return 0;
407
408 unsetup_vb2:
409         csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
410         return ret;
411 }
412
413 static void csi_idmac_unsetup(struct csi_priv *priv,
414                               enum vb2_buffer_state state)
415 {
416         ipu_idmac_disable_channel(priv->idmac_ch);
417         ipu_smfc_disable(priv->smfc);
418
419         csi_idmac_unsetup_vb2_buf(priv, state);
420 }
421
422 static int csi_idmac_setup(struct csi_priv *priv)
423 {
424         int ret;
425
426         ret = csi_idmac_setup_channel(priv);
427         if (ret)
428                 return ret;
429
430         ipu_cpmem_dump(priv->idmac_ch);
431         ipu_dump(priv->ipu);
432
433         ipu_smfc_enable(priv->smfc);
434
435         /* set buffers ready */
436         ipu_idmac_select_buffer(priv->idmac_ch, 0);
437         ipu_idmac_select_buffer(priv->idmac_ch, 1);
438
439         /* enable the channels */
440         ipu_idmac_enable_channel(priv->idmac_ch);
441
442         return 0;
443 }
444
445 static int csi_idmac_start(struct csi_priv *priv)
446 {
447         struct imx_media_video_dev *vdev = priv->vdev;
448         struct v4l2_pix_format *outfmt;
449         int ret;
450
451         ret = csi_idmac_get_ipu_resources(priv);
452         if (ret)
453                 return ret;
454
455         ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
456
457         outfmt = &vdev->fmt.fmt.pix;
458
459         ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
460                                       outfmt->sizeimage);
461         if (ret)
462                 goto out_put_ipu;
463
464         priv->ipu_buf_num = 0;
465
466         /* init EOF completion waitq */
467         init_completion(&priv->last_eof_comp);
468         priv->last_eof = false;
469         priv->nfb4eof = false;
470
471         ret = csi_idmac_setup(priv);
472         if (ret) {
473                 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
474                 goto out_free_dma_buf;
475         }
476
477         priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
478                                                  priv->idmac_ch,
479                                                  IPU_IRQ_NFB4EOF);
480         ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
481                                csi_idmac_nfb4eof_interrupt, 0,
482                                "imx-smfc-nfb4eof", priv);
483         if (ret) {
484                 v4l2_err(&priv->sd,
485                          "Error registering NFB4EOF irq: %d\n", ret);
486                 goto out_unsetup;
487         }
488
489         priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
490                                               IPU_IRQ_EOF);
491
492         ret = devm_request_irq(priv->dev, priv->eof_irq,
493                                csi_idmac_eof_interrupt, 0,
494                                "imx-smfc-eof", priv);
495         if (ret) {
496                 v4l2_err(&priv->sd,
497                          "Error registering eof irq: %d\n", ret);
498                 goto out_free_nfb4eof_irq;
499         }
500
501         /* start the EOF timeout timer */
502         mod_timer(&priv->eof_timeout_timer,
503                   jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
504
505         return 0;
506
507 out_free_nfb4eof_irq:
508         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
509 out_unsetup:
510         csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
511 out_free_dma_buf:
512         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
513 out_put_ipu:
514         csi_idmac_put_ipu_resources(priv);
515         return ret;
516 }
517
518 static void csi_idmac_stop(struct csi_priv *priv)
519 {
520         unsigned long flags;
521         int ret;
522
523         /* mark next EOF interrupt as the last before stream off */
524         spin_lock_irqsave(&priv->irqlock, flags);
525         priv->last_eof = true;
526         spin_unlock_irqrestore(&priv->irqlock, flags);
527
528         /*
529          * and then wait for interrupt handler to mark completion.
530          */
531         ret = wait_for_completion_timeout(
532                 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
533         if (ret == 0)
534                 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
535
536         devm_free_irq(priv->dev, priv->eof_irq, priv);
537         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
538
539         csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
540
541         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
542
543         /* cancel the EOF timeout timer */
544         del_timer_sync(&priv->eof_timeout_timer);
545
546         csi_idmac_put_ipu_resources(priv);
547 }
548
549 /* Update the CSI whole sensor and active windows */
550 static int csi_setup(struct csi_priv *priv)
551 {
552         struct v4l2_mbus_framefmt *infmt, *outfmt;
553         struct v4l2_mbus_config sensor_mbus_cfg;
554         struct v4l2_fwnode_endpoint *sensor_ep;
555         struct v4l2_mbus_framefmt if_fmt;
556
557         infmt = &priv->format_mbus[CSI_SINK_PAD];
558         outfmt = &priv->format_mbus[priv->active_output_pad];
559         sensor_ep = &priv->sensor->sensor_ep;
560
561         /* compose mbus_config from sensor endpoint */
562         sensor_mbus_cfg.type = sensor_ep->bus_type;
563         sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ?
564                 sensor_ep->bus.mipi_csi2.flags :
565                 sensor_ep->bus.parallel.flags;
566
567         /*
568          * we need to pass input sensor frame to CSI interface, but
569          * with translated field type from output format
570          */
571         if_fmt = *infmt;
572         if_fmt.field = outfmt->field;
573
574         ipu_csi_set_window(priv->csi, &priv->crop);
575
576         ipu_csi_set_downsize(priv->csi,
577                              priv->crop.width == 2 * outfmt->width,
578                              priv->crop.height == 2 * outfmt->height);
579
580         ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt);
581
582         ipu_csi_set_dest(priv->csi, priv->dest);
583
584         ipu_csi_dump(priv->csi);
585
586         return 0;
587 }
588
589 static int csi_start(struct csi_priv *priv)
590 {
591         u32 bad_frames = 0;
592         int ret;
593
594         if (!priv->sensor) {
595                 v4l2_err(&priv->sd, "no sensor attached\n");
596                 return -EINVAL;
597         }
598
599         ret = v4l2_subdev_call(priv->sensor->sd, sensor,
600                                g_skip_frames, &bad_frames);
601         if (!ret && bad_frames) {
602                 struct v4l2_fract *fi = &priv->frame_interval;
603                 u32 delay_usec;
604
605                 /*
606                  * This sensor has bad frames when it is turned on,
607                  * add a delay to avoid them before enabling the CSI
608                  * hardware. Especially for sensors with a bt.656 interface,
609                  * any shifts in the SAV/EAV sync codes will cause the CSI
610                  * to lose vert/horiz sync.
611                  */
612                 delay_usec = DIV_ROUND_UP_ULL(
613                         (u64)USEC_PER_SEC * fi->numerator * bad_frames,
614                         fi->denominator);
615                 usleep_range(delay_usec, delay_usec + 1000);
616         }
617
618         if (priv->dest == IPU_CSI_DEST_IDMAC) {
619                 ret = csi_idmac_start(priv);
620                 if (ret)
621                         return ret;
622         }
623
624         ret = csi_setup(priv);
625         if (ret)
626                 goto idmac_stop;
627
628         /* start the frame interval monitor */
629         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
630                 ret = imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
631                                                true);
632                 if (ret)
633                         goto idmac_stop;
634         }
635
636         ret = ipu_csi_enable(priv->csi);
637         if (ret) {
638                 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
639                 goto fim_off;
640         }
641
642         return 0;
643
644 fim_off:
645         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
646                 imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
647                                          false);
648 idmac_stop:
649         if (priv->dest == IPU_CSI_DEST_IDMAC)
650                 csi_idmac_stop(priv);
651         return ret;
652 }
653
654 static void csi_stop(struct csi_priv *priv)
655 {
656         if (priv->dest == IPU_CSI_DEST_IDMAC) {
657                 csi_idmac_stop(priv);
658
659                 /* stop the frame interval monitor */
660                 if (priv->fim)
661                         imx_media_fim_set_stream(priv->fim,
662                                                  &priv->frame_interval,
663                                                  false);
664         }
665
666         ipu_csi_disable(priv->csi);
667 }
668
669 /*
670  * V4L2 subdev operations.
671  */
672
673 static int csi_g_frame_interval(struct v4l2_subdev *sd,
674                                 struct v4l2_subdev_frame_interval *fi)
675 {
676         struct csi_priv *priv = v4l2_get_subdevdata(sd);
677
678         mutex_lock(&priv->lock);
679         fi->interval = priv->frame_interval;
680         mutex_unlock(&priv->lock);
681
682         return 0;
683 }
684
685 static int csi_s_frame_interval(struct v4l2_subdev *sd,
686                                 struct v4l2_subdev_frame_interval *fi)
687 {
688         struct csi_priv *priv = v4l2_get_subdevdata(sd);
689
690         mutex_lock(&priv->lock);
691
692         /* Output pads mirror active input pad, no limits on input pads */
693         if (fi->pad == CSI_SRC_PAD_IDMAC || fi->pad == CSI_SRC_PAD_DIRECT)
694                 fi->interval = priv->frame_interval;
695
696         priv->frame_interval = fi->interval;
697
698         mutex_unlock(&priv->lock);
699
700         return 0;
701 }
702
703 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
704 {
705         struct csi_priv *priv = v4l2_get_subdevdata(sd);
706         int ret = 0;
707
708         mutex_lock(&priv->lock);
709
710         if (!priv->src_sd || !priv->sink) {
711                 ret = -EPIPE;
712                 goto out;
713         }
714
715         /*
716          * enable/disable streaming only if stream_count is
717          * going from 0 to 1 / 1 to 0.
718          */
719         if (priv->stream_count != !enable)
720                 goto update_count;
721
722         if (enable) {
723                 /* upstream must be started first, before starting CSI */
724                 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
725                 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
726                 if (ret)
727                         goto out;
728
729                 dev_dbg(priv->dev, "stream ON\n");
730                 ret = csi_start(priv);
731                 if (ret) {
732                         v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
733                         goto out;
734                 }
735         } else {
736                 dev_dbg(priv->dev, "stream OFF\n");
737                 /* CSI must be stopped first, then stop upstream */
738                 csi_stop(priv);
739                 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
740         }
741
742 update_count:
743         priv->stream_count += enable ? 1 : -1;
744         WARN_ON(priv->stream_count < 0);
745 out:
746         mutex_unlock(&priv->lock);
747         return ret;
748 }
749
750 static int csi_link_setup(struct media_entity *entity,
751                           const struct media_pad *local,
752                           const struct media_pad *remote, u32 flags)
753 {
754         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
755         struct csi_priv *priv = v4l2_get_subdevdata(sd);
756         struct v4l2_subdev *remote_sd;
757         int ret = 0;
758
759         dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
760                 local->entity->name);
761
762         mutex_lock(&priv->lock);
763
764         if (local->flags & MEDIA_PAD_FL_SINK) {
765                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
766                         ret = -EINVAL;
767                         goto out;
768                 }
769
770                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
771
772                 if (flags & MEDIA_LNK_FL_ENABLED) {
773                         if (priv->src_sd) {
774                                 ret = -EBUSY;
775                                 goto out;
776                         }
777                         priv->src_sd = remote_sd;
778                 } else {
779                         priv->src_sd = NULL;
780                 }
781
782                 goto out;
783         }
784
785         /* this is a source pad */
786
787         if (flags & MEDIA_LNK_FL_ENABLED) {
788                 if (priv->sink) {
789                         ret = -EBUSY;
790                         goto out;
791                 }
792         } else {
793                 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
794                 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
795                 priv->sink = NULL;
796                 goto out;
797         }
798
799         /* record which output pad is now active */
800         priv->active_output_pad = local->index;
801
802         /* set CSI destination */
803         if (local->index == CSI_SRC_PAD_IDMAC) {
804                 if (!is_media_entity_v4l2_video_device(remote->entity)) {
805                         ret = -EINVAL;
806                         goto out;
807                 }
808
809                 if (priv->fim) {
810                         ret = imx_media_fim_add_controls(priv->fim);
811                         if (ret)
812                                 goto out;
813                 }
814
815                 priv->dest = IPU_CSI_DEST_IDMAC;
816         } else {
817                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
818                         ret = -EINVAL;
819                         goto out;
820                 }
821
822                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
823                 switch (remote_sd->grp_id) {
824                 case IMX_MEDIA_GRP_ID_VDIC:
825                         priv->dest = IPU_CSI_DEST_VDIC;
826                         break;
827                 case IMX_MEDIA_GRP_ID_IC_PRP:
828                         priv->dest = IPU_CSI_DEST_IC;
829                         break;
830                 default:
831                         ret = -EINVAL;
832                         goto out;
833                 }
834         }
835
836         priv->sink = remote->entity;
837 out:
838         mutex_unlock(&priv->lock);
839         return ret;
840 }
841
842 static int csi_link_validate(struct v4l2_subdev *sd,
843                              struct media_link *link,
844                              struct v4l2_subdev_format *source_fmt,
845                              struct v4l2_subdev_format *sink_fmt)
846 {
847         struct csi_priv *priv = v4l2_get_subdevdata(sd);
848         struct v4l2_fwnode_endpoint *sensor_ep;
849         const struct imx_media_pixfmt *incc;
850         struct imx_media_subdev *sensor;
851         bool is_csi2;
852         int ret;
853
854         ret = v4l2_subdev_link_validate_default(sd, link,
855                                                 source_fmt, sink_fmt);
856         if (ret)
857                 return ret;
858
859         sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity);
860         if (IS_ERR(sensor)) {
861                 v4l2_err(&priv->sd, "no sensor attached\n");
862                 return PTR_ERR(priv->sensor);
863         }
864
865         mutex_lock(&priv->lock);
866
867         priv->sensor = sensor;
868         sensor_ep = &priv->sensor->sensor_ep;
869         is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2);
870         incc = priv->cc[CSI_SINK_PAD];
871
872         if (priv->dest != IPU_CSI_DEST_IDMAC &&
873             (incc->bayer || (!is_csi2 &&
874                              sensor_ep->bus.parallel.bus_width >= 16))) {
875                 v4l2_err(&priv->sd,
876                          "bayer/16-bit parallel buses must go to IDMAC pad\n");
877                 ret = -EINVAL;
878                 goto out;
879         }
880
881         if (is_csi2) {
882                 int vc_num = 0;
883                 /*
884                  * NOTE! It seems the virtual channels from the mipi csi-2
885                  * receiver are used only for routing by the video mux's,
886                  * or for hard-wired routing to the CSI's. Once the stream
887                  * enters the CSI's however, they are treated internally
888                  * in the IPU as virtual channel 0.
889                  */
890 #if 0
891                 mutex_unlock(&priv->lock);
892                 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
893                                                           &priv->sd.entity);
894                 if (vc_num < 0)
895                         return vc_num;
896                 mutex_lock(&priv->lock);
897 #endif
898                 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
899                                           &priv->format_mbus[CSI_SINK_PAD]);
900         }
901
902         /* select either parallel or MIPI-CSI2 as input to CSI */
903         ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
904 out:
905         mutex_unlock(&priv->lock);
906         return ret;
907 }
908
909 static struct v4l2_mbus_framefmt *
910 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
911               unsigned int pad, enum v4l2_subdev_format_whence which)
912 {
913         if (which == V4L2_SUBDEV_FORMAT_TRY)
914                 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
915         else
916                 return &priv->format_mbus[pad];
917 }
918
919 static struct v4l2_rect *
920 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
921                enum v4l2_subdev_format_whence which)
922 {
923         if (which == V4L2_SUBDEV_FORMAT_TRY)
924                 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
925         else
926                 return &priv->crop;
927 }
928
929 static void csi_try_crop(struct csi_priv *priv,
930                          struct v4l2_rect *crop,
931                          struct v4l2_subdev_pad_config *cfg,
932                          struct v4l2_mbus_framefmt *infmt,
933                          struct imx_media_subdev *sensor)
934 {
935         struct v4l2_fwnode_endpoint *sensor_ep;
936
937         sensor_ep = &sensor->sensor_ep;
938
939         crop->width = min_t(__u32, infmt->width, crop->width);
940         if (crop->left + crop->width > infmt->width)
941                 crop->left = infmt->width - crop->width;
942         /* adjust crop left/width to h/w alignment restrictions */
943         crop->left &= ~0x3;
944         crop->width &= ~0x7;
945
946         /*
947          * FIXME: not sure why yet, but on interlaced bt.656,
948          * changing the vertical cropping causes loss of vertical
949          * sync, so fix it to NTSC/PAL active lines. NTSC contains
950          * 2 extra lines of active video that need to be cropped.
951          */
952         if (sensor_ep->bus_type == V4L2_MBUS_BT656 &&
953             (V4L2_FIELD_HAS_BOTH(infmt->field) ||
954              infmt->field == V4L2_FIELD_ALTERNATE)) {
955                 crop->height = infmt->height;
956                 crop->top = (infmt->height == 480) ? 2 : 0;
957         } else {
958                 crop->height = min_t(__u32, infmt->height, crop->height);
959                 if (crop->top + crop->height > infmt->height)
960                         crop->top = infmt->height - crop->height;
961         }
962 }
963
964 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
965                               struct v4l2_subdev_pad_config *cfg,
966                               struct v4l2_subdev_mbus_code_enum *code)
967 {
968         struct csi_priv *priv = v4l2_get_subdevdata(sd);
969         const struct imx_media_pixfmt *incc;
970         struct v4l2_mbus_framefmt *infmt;
971         int ret = 0;
972
973         mutex_lock(&priv->lock);
974
975         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
976         incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
977
978         switch (code->pad) {
979         case CSI_SINK_PAD:
980                 ret = imx_media_enum_mbus_format(&code->code, code->index,
981                                                  CS_SEL_ANY, true);
982                 break;
983         case CSI_SRC_PAD_DIRECT:
984         case CSI_SRC_PAD_IDMAC:
985                 if (incc->bayer) {
986                         if (code->index != 0) {
987                                 ret = -EINVAL;
988                                 goto out;
989                         }
990                         code->code = infmt->code;
991                 } else {
992                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
993                                 CS_SEL_YUV : CS_SEL_RGB;
994                         ret = imx_media_enum_ipu_format(&code->code,
995                                                         code->index,
996                                                         cs_sel);
997                 }
998                 break;
999         default:
1000                 ret = -EINVAL;
1001         }
1002
1003 out:
1004         mutex_unlock(&priv->lock);
1005         return ret;
1006 }
1007
1008 static int csi_get_fmt(struct v4l2_subdev *sd,
1009                        struct v4l2_subdev_pad_config *cfg,
1010                        struct v4l2_subdev_format *sdformat)
1011 {
1012         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1013         struct v4l2_mbus_framefmt *fmt;
1014         int ret = 0;
1015
1016         if (sdformat->pad >= CSI_NUM_PADS)
1017                 return -EINVAL;
1018
1019         mutex_lock(&priv->lock);
1020
1021         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1022         if (!fmt) {
1023                 ret = -EINVAL;
1024                 goto out;
1025         }
1026
1027         sdformat->format = *fmt;
1028 out:
1029         mutex_unlock(&priv->lock);
1030         return ret;
1031 }
1032
1033 static void csi_try_fmt(struct csi_priv *priv,
1034                         struct imx_media_subdev *sensor,
1035                         struct v4l2_subdev_pad_config *cfg,
1036                         struct v4l2_subdev_format *sdformat,
1037                         struct v4l2_rect *crop,
1038                         const struct imx_media_pixfmt **cc)
1039 {
1040         const struct imx_media_pixfmt *incc;
1041         struct v4l2_mbus_framefmt *infmt;
1042         u32 code;
1043
1044         switch (sdformat->pad) {
1045         case CSI_SRC_PAD_DIRECT:
1046         case CSI_SRC_PAD_IDMAC:
1047                 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD,
1048                                       sdformat->which);
1049                 incc = imx_media_find_mbus_format(infmt->code,
1050                                                   CS_SEL_ANY, true);
1051
1052                 if (sdformat->format.width < crop->width * 3 / 4)
1053                         sdformat->format.width = crop->width / 2;
1054                 else
1055                         sdformat->format.width = crop->width;
1056
1057                 if (sdformat->format.height < crop->height * 3 / 4)
1058                         sdformat->format.height = crop->height / 2;
1059                 else
1060                         sdformat->format.height = crop->height;
1061
1062                 if (incc->bayer) {
1063                         sdformat->format.code = infmt->code;
1064                         *cc = incc;
1065                 } else {
1066                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1067                                 CS_SEL_YUV : CS_SEL_RGB;
1068
1069                         *cc = imx_media_find_ipu_format(sdformat->format.code,
1070                                                         cs_sel);
1071                         if (!*cc) {
1072                                 imx_media_enum_ipu_format(&code, 0, cs_sel);
1073                                 *cc = imx_media_find_ipu_format(code, cs_sel);
1074                                 sdformat->format.code = (*cc)->codes[0];
1075                         }
1076                 }
1077
1078                 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1079                     sdformat->format.field != V4L2_FIELD_NONE)
1080                         sdformat->format.field = infmt->field;
1081
1082                 /*
1083                  * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1084                  * depending on input height (assume NTSC top-bottom
1085                  * order if 480 lines, otherwise PAL bottom-top order).
1086                  */
1087                 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1088                         sdformat->format.field =  (infmt->height == 480) ?
1089                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1090                 }
1091                 break;
1092         case CSI_SINK_PAD:
1093                 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1094                                       W_ALIGN, &sdformat->format.height,
1095                                       MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1096                 crop->left = 0;
1097                 crop->top = 0;
1098                 crop->width = sdformat->format.width;
1099                 crop->height = sdformat->format.height;
1100                 csi_try_crop(priv, crop, cfg, &sdformat->format, sensor);
1101
1102                 *cc = imx_media_find_mbus_format(sdformat->format.code,
1103                                                  CS_SEL_ANY, true);
1104                 if (!*cc) {
1105                         imx_media_enum_mbus_format(&code, 0,
1106                                                    CS_SEL_ANY, false);
1107                         *cc = imx_media_find_mbus_format(code,
1108                                                         CS_SEL_ANY, false);
1109                         sdformat->format.code = (*cc)->codes[0];
1110                 }
1111                 break;
1112         }
1113 }
1114
1115 static int csi_set_fmt(struct v4l2_subdev *sd,
1116                        struct v4l2_subdev_pad_config *cfg,
1117                        struct v4l2_subdev_format *sdformat)
1118 {
1119         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1120         struct imx_media_video_dev *vdev = priv->vdev;
1121         const struct imx_media_pixfmt *cc;
1122         struct imx_media_subdev *sensor;
1123         struct v4l2_pix_format vdev_fmt;
1124         struct v4l2_mbus_framefmt *fmt;
1125         struct v4l2_rect *crop;
1126         int ret = 0;
1127
1128         if (sdformat->pad >= CSI_NUM_PADS)
1129                 return -EINVAL;
1130
1131         sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1132         if (IS_ERR(sensor)) {
1133                 v4l2_err(&priv->sd, "no sensor attached\n");
1134                 return PTR_ERR(sensor);
1135         }
1136
1137         mutex_lock(&priv->lock);
1138
1139         if (priv->stream_count > 0) {
1140                 ret = -EBUSY;
1141                 goto out;
1142         }
1143
1144         crop = __csi_get_crop(priv, cfg, sdformat->which);
1145
1146         csi_try_fmt(priv, sensor, cfg, sdformat, crop, &cc);
1147
1148         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1149         *fmt = sdformat->format;
1150
1151         if (sdformat->pad == CSI_SINK_PAD) {
1152                 int pad;
1153
1154                 /* propagate format to source pads */
1155                 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1156                         const struct imx_media_pixfmt *outcc;
1157                         struct v4l2_mbus_framefmt *outfmt;
1158                         struct v4l2_subdev_format format;
1159
1160                         format.pad = pad;
1161                         format.which = sdformat->which;
1162                         format.format = sdformat->format;
1163                         csi_try_fmt(priv, sensor, cfg, &format, crop, &outcc);
1164
1165                         outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1166                         *outfmt = format.format;
1167
1168                         if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1169                                 priv->cc[pad] = outcc;
1170                 }
1171         }
1172
1173         if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1174                 goto out;
1175
1176         priv->cc[sdformat->pad] = cc;
1177
1178         /* propagate IDMAC output pad format to capture device */
1179         imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1180                                       &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1181                                       priv->cc[CSI_SRC_PAD_IDMAC]);
1182         mutex_unlock(&priv->lock);
1183         imx_media_capture_device_set_format(vdev, &vdev_fmt);
1184
1185         return 0;
1186 out:
1187         mutex_unlock(&priv->lock);
1188         return ret;
1189 }
1190
1191 static int csi_get_selection(struct v4l2_subdev *sd,
1192                              struct v4l2_subdev_pad_config *cfg,
1193                              struct v4l2_subdev_selection *sel)
1194 {
1195         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1196         struct v4l2_mbus_framefmt *infmt;
1197         struct v4l2_rect *crop;
1198         int ret = 0;
1199
1200         if (sel->pad >= CSI_NUM_PADS || sel->pad == CSI_SINK_PAD)
1201                 return -EINVAL;
1202
1203         mutex_lock(&priv->lock);
1204
1205         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1206         crop = __csi_get_crop(priv, cfg, sel->which);
1207
1208         switch (sel->target) {
1209         case V4L2_SEL_TGT_CROP_BOUNDS:
1210                 sel->r.left = 0;
1211                 sel->r.top = 0;
1212                 sel->r.width = infmt->width;
1213                 sel->r.height = infmt->height;
1214                 break;
1215         case V4L2_SEL_TGT_CROP:
1216                 sel->r = *crop;
1217                 break;
1218         default:
1219                 ret = -EINVAL;
1220         }
1221
1222         mutex_unlock(&priv->lock);
1223         return ret;
1224 }
1225
1226 static int csi_set_selection(struct v4l2_subdev *sd,
1227                              struct v4l2_subdev_pad_config *cfg,
1228                              struct v4l2_subdev_selection *sel)
1229 {
1230         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1231         struct v4l2_mbus_framefmt *infmt;
1232         struct imx_media_subdev *sensor;
1233         struct v4l2_rect *crop;
1234         int pad, ret = 0;
1235
1236         if (sel->pad >= CSI_NUM_PADS ||
1237             sel->pad == CSI_SINK_PAD ||
1238             sel->target != V4L2_SEL_TGT_CROP)
1239                 return -EINVAL;
1240
1241         sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1242         if (IS_ERR(sensor)) {
1243                 v4l2_err(&priv->sd, "no sensor attached\n");
1244                 return PTR_ERR(sensor);
1245         }
1246
1247         mutex_lock(&priv->lock);
1248
1249         if (priv->stream_count > 0) {
1250                 ret = -EBUSY;
1251                 goto out;
1252         }
1253
1254         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1255         crop = __csi_get_crop(priv, cfg, sel->which);
1256
1257         /*
1258          * Modifying the crop rectangle always changes the format on the source
1259          * pad. If the KEEP_CONFIG flag is set, just return the current crop
1260          * rectangle.
1261          */
1262         if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1263                 sel->r = priv->crop;
1264                 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1265                         *crop = sel->r;
1266                 goto out;
1267         }
1268
1269         csi_try_crop(priv, &sel->r, cfg, infmt, sensor);
1270
1271         *crop = sel->r;
1272
1273         /* Update the source pad formats */
1274         for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1275                 struct v4l2_mbus_framefmt *outfmt;
1276
1277                 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1278                 outfmt->width = crop->width;
1279                 outfmt->height = crop->height;
1280         }
1281
1282 out:
1283         mutex_unlock(&priv->lock);
1284         return ret;
1285 }
1286
1287 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1288                                struct v4l2_event_subscription *sub)
1289 {
1290         if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1291                 return -EINVAL;
1292         if (sub->id != 0)
1293                 return -EINVAL;
1294
1295         return v4l2_event_subscribe(fh, sub, 0, NULL);
1296 }
1297
1298 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1299                                  struct v4l2_event_subscription *sub)
1300 {
1301         return v4l2_event_unsubscribe(fh, sub);
1302 }
1303
1304 /*
1305  * retrieve our pads parsed from the OF graph by the media device
1306  */
1307 static int csi_registered(struct v4l2_subdev *sd)
1308 {
1309         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1310         int i, ret;
1311         u32 code;
1312
1313         /* get media device */
1314         priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1315
1316         /* get handle to IPU CSI */
1317         priv->csi = ipu_csi_get(priv->ipu, priv->csi_id);
1318         if (IS_ERR(priv->csi)) {
1319                 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1320                 return PTR_ERR(priv->csi);
1321         }
1322
1323         for (i = 0; i < CSI_NUM_PADS; i++) {
1324                 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1325                         MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1326
1327                 code = 0;
1328                 if (i != CSI_SINK_PAD)
1329                         imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1330
1331                 /* set a default mbus format  */
1332                 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1333                                               640, 480, code, V4L2_FIELD_NONE,
1334                                               &priv->cc[i]);
1335                 if (ret)
1336                         goto put_csi;
1337         }
1338
1339         /* init default frame interval */
1340         priv->frame_interval.numerator = 1;
1341         priv->frame_interval.denominator = 30;
1342
1343         priv->fim = imx_media_fim_init(&priv->sd);
1344         if (IS_ERR(priv->fim)) {
1345                 ret = PTR_ERR(priv->fim);
1346                 goto put_csi;
1347         }
1348
1349         ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1350         if (ret)
1351                 goto free_fim;
1352
1353         ret = imx_media_capture_device_register(priv->vdev);
1354         if (ret)
1355                 goto free_fim;
1356
1357         ret = imx_media_add_video_device(priv->md, priv->vdev);
1358         if (ret)
1359                 goto unreg;
1360
1361         return 0;
1362 unreg:
1363         imx_media_capture_device_unregister(priv->vdev);
1364 free_fim:
1365         if (priv->fim)
1366                 imx_media_fim_free(priv->fim);
1367 put_csi:
1368         ipu_csi_put(priv->csi);
1369         return ret;
1370 }
1371
1372 static void csi_unregistered(struct v4l2_subdev *sd)
1373 {
1374         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1375
1376         imx_media_capture_device_unregister(priv->vdev);
1377
1378         if (priv->fim)
1379                 imx_media_fim_free(priv->fim);
1380
1381         if (!IS_ERR_OR_NULL(priv->csi))
1382                 ipu_csi_put(priv->csi);
1383 }
1384
1385 static const struct media_entity_operations csi_entity_ops = {
1386         .link_setup = csi_link_setup,
1387         .link_validate = v4l2_subdev_link_validate,
1388 };
1389
1390 static const struct v4l2_subdev_core_ops csi_core_ops = {
1391         .subscribe_event = csi_subscribe_event,
1392         .unsubscribe_event = csi_unsubscribe_event,
1393 };
1394
1395 static const struct v4l2_subdev_video_ops csi_video_ops = {
1396         .g_frame_interval = csi_g_frame_interval,
1397         .s_frame_interval = csi_s_frame_interval,
1398         .s_stream = csi_s_stream,
1399 };
1400
1401 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1402         .enum_mbus_code = csi_enum_mbus_code,
1403         .get_fmt = csi_get_fmt,
1404         .set_fmt = csi_set_fmt,
1405         .get_selection = csi_get_selection,
1406         .set_selection = csi_set_selection,
1407         .link_validate = csi_link_validate,
1408 };
1409
1410 static const struct v4l2_subdev_ops csi_subdev_ops = {
1411         .core = &csi_core_ops,
1412         .video = &csi_video_ops,
1413         .pad = &csi_pad_ops,
1414 };
1415
1416 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1417         .registered = csi_registered,
1418         .unregistered = csi_unregistered,
1419 };
1420
1421 static int imx_csi_probe(struct platform_device *pdev)
1422 {
1423         struct ipu_client_platformdata *pdata;
1424         struct pinctrl *pinctrl;
1425         struct csi_priv *priv;
1426         int ret;
1427
1428         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1429         if (!priv)
1430                 return -ENOMEM;
1431
1432         platform_set_drvdata(pdev, &priv->sd);
1433         priv->dev = &pdev->dev;
1434
1435         ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1436         if (ret)
1437                 return ret;
1438
1439         /* get parent IPU */
1440         priv->ipu = dev_get_drvdata(priv->dev->parent);
1441
1442         /* get our CSI id */
1443         pdata = priv->dev->platform_data;
1444         priv->csi_id = pdata->csi;
1445         priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1446
1447         init_timer(&priv->eof_timeout_timer);
1448         priv->eof_timeout_timer.data = (unsigned long)priv;
1449         priv->eof_timeout_timer.function = csi_idmac_eof_timeout;
1450         spin_lock_init(&priv->irqlock);
1451
1452         v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1453         v4l2_set_subdevdata(&priv->sd, priv);
1454         priv->sd.internal_ops = &csi_internal_ops;
1455         priv->sd.entity.ops = &csi_entity_ops;
1456         priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1457         priv->sd.dev = &pdev->dev;
1458         priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1459         priv->sd.owner = THIS_MODULE;
1460         priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1461         priv->sd.grp_id = priv->csi_id ?
1462                 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1463         imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1464                                     priv->sd.grp_id, ipu_get_num(priv->ipu));
1465
1466         priv->vdev = imx_media_capture_device_init(&priv->sd,
1467                                                    CSI_SRC_PAD_IDMAC);
1468         if (IS_ERR(priv->vdev))
1469                 return PTR_ERR(priv->vdev);
1470
1471         mutex_init(&priv->lock);
1472
1473         v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1474         priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1475
1476         /*
1477          * The IPUv3 driver did not assign an of_node to this
1478          * device. As a result, pinctrl does not automatically
1479          * configure our pin groups, so we need to do that manually
1480          * here, after setting this device's of_node.
1481          */
1482         priv->dev->of_node = pdata->of_node;
1483         pinctrl = devm_pinctrl_get_select_default(priv->dev);
1484
1485         ret = v4l2_async_register_subdev(&priv->sd);
1486         if (ret)
1487                 goto free;
1488
1489         return 0;
1490 free:
1491         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1492         mutex_destroy(&priv->lock);
1493         imx_media_capture_device_remove(priv->vdev);
1494         return ret;
1495 }
1496
1497 static int imx_csi_remove(struct platform_device *pdev)
1498 {
1499         struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1500         struct csi_priv *priv = sd_to_dev(sd);
1501
1502         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1503         mutex_destroy(&priv->lock);
1504         imx_media_capture_device_remove(priv->vdev);
1505         v4l2_async_unregister_subdev(sd);
1506         media_entity_cleanup(&sd->entity);
1507
1508         return 0;
1509 }
1510
1511 static const struct platform_device_id imx_csi_ids[] = {
1512         { .name = "imx-ipuv3-csi" },
1513         { },
1514 };
1515 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1516
1517 static struct platform_driver imx_csi_driver = {
1518         .probe = imx_csi_probe,
1519         .remove = imx_csi_remove,
1520         .id_table = imx_csi_ids,
1521         .driver = {
1522                 .name = "imx-ipuv3-csi",
1523         },
1524 };
1525 module_platform_driver(imx_csi_driver);
1526
1527 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1528 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1529 MODULE_LICENSE("GPL");
1530 MODULE_ALIAS("platform:imx-ipuv3-csi");