b589fa244abcf230d0d41b2ce68aac1c5a46736b
[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         default:
337                 burst_size = (image.pix.width & 0xf) ? 8 : 16;
338                 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
339                                sensor_ep->bus.parallel.bus_width >= 16);
340                 passthrough_bits = 16;
341                 break;
342         }
343
344         if (passthrough) {
345                 ipu_cpmem_set_resolution(priv->idmac_ch, image.rect.width,
346                                          image.rect.height);
347                 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
348                 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
349                 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
350                 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
351                                                  passthrough_bits);
352         } else {
353                 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
354                 if (ret)
355                         goto unsetup_vb2;
356         }
357
358         ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
359
360         /*
361          * Set the channel for the direct CSI-->memory via SMFC
362          * use-case to very high priority, by enabling the watermark
363          * signal in the SMFC, enabling WM in the channel, and setting
364          * the channel priority to high.
365          *
366          * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
367          * value.
368          *
369          * The WM's are set very low by intention here to ensure that
370          * the SMFC FIFOs do not overflow.
371          */
372         ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
373         ipu_cpmem_set_high_priority(priv->idmac_ch);
374         ipu_idmac_enable_watermark(priv->idmac_ch, true);
375         ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
376
377         burst_size = passthrough ?
378                 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
379
380         ipu_smfc_set_burstsize(priv->smfc, burst_size);
381
382         if (image.pix.field == V4L2_FIELD_NONE &&
383             V4L2_FIELD_HAS_BOTH(infmt->field))
384                 ipu_cpmem_interlaced_scan(priv->idmac_ch,
385                                           image.pix.bytesperline);
386
387         ipu_idmac_set_double_buffer(priv->idmac_ch, true);
388
389         return 0;
390
391 unsetup_vb2:
392         csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
393         return ret;
394 }
395
396 static void csi_idmac_unsetup(struct csi_priv *priv,
397                               enum vb2_buffer_state state)
398 {
399         ipu_idmac_disable_channel(priv->idmac_ch);
400         ipu_smfc_disable(priv->smfc);
401
402         csi_idmac_unsetup_vb2_buf(priv, state);
403 }
404
405 static int csi_idmac_setup(struct csi_priv *priv)
406 {
407         int ret;
408
409         ret = csi_idmac_setup_channel(priv);
410         if (ret)
411                 return ret;
412
413         ipu_cpmem_dump(priv->idmac_ch);
414         ipu_dump(priv->ipu);
415
416         ipu_smfc_enable(priv->smfc);
417
418         /* set buffers ready */
419         ipu_idmac_select_buffer(priv->idmac_ch, 0);
420         ipu_idmac_select_buffer(priv->idmac_ch, 1);
421
422         /* enable the channels */
423         ipu_idmac_enable_channel(priv->idmac_ch);
424
425         return 0;
426 }
427
428 static int csi_idmac_start(struct csi_priv *priv)
429 {
430         struct imx_media_video_dev *vdev = priv->vdev;
431         struct v4l2_pix_format *outfmt;
432         int ret;
433
434         ret = csi_idmac_get_ipu_resources(priv);
435         if (ret)
436                 return ret;
437
438         ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
439
440         outfmt = &vdev->fmt.fmt.pix;
441
442         ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
443                                       outfmt->sizeimage);
444         if (ret)
445                 goto out_put_ipu;
446
447         priv->ipu_buf_num = 0;
448
449         /* init EOF completion waitq */
450         init_completion(&priv->last_eof_comp);
451         priv->last_eof = false;
452         priv->nfb4eof = false;
453
454         ret = csi_idmac_setup(priv);
455         if (ret) {
456                 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
457                 goto out_free_dma_buf;
458         }
459
460         priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
461                                                  priv->idmac_ch,
462                                                  IPU_IRQ_NFB4EOF);
463         ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
464                                csi_idmac_nfb4eof_interrupt, 0,
465                                "imx-smfc-nfb4eof", priv);
466         if (ret) {
467                 v4l2_err(&priv->sd,
468                          "Error registering NFB4EOF irq: %d\n", ret);
469                 goto out_unsetup;
470         }
471
472         priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
473                                               IPU_IRQ_EOF);
474
475         ret = devm_request_irq(priv->dev, priv->eof_irq,
476                                csi_idmac_eof_interrupt, 0,
477                                "imx-smfc-eof", priv);
478         if (ret) {
479                 v4l2_err(&priv->sd,
480                          "Error registering eof irq: %d\n", ret);
481                 goto out_free_nfb4eof_irq;
482         }
483
484         /* start the EOF timeout timer */
485         mod_timer(&priv->eof_timeout_timer,
486                   jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
487
488         return 0;
489
490 out_free_nfb4eof_irq:
491         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
492 out_unsetup:
493         csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
494 out_free_dma_buf:
495         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
496 out_put_ipu:
497         csi_idmac_put_ipu_resources(priv);
498         return ret;
499 }
500
501 static void csi_idmac_stop(struct csi_priv *priv)
502 {
503         unsigned long flags;
504         int ret;
505
506         /* mark next EOF interrupt as the last before stream off */
507         spin_lock_irqsave(&priv->irqlock, flags);
508         priv->last_eof = true;
509         spin_unlock_irqrestore(&priv->irqlock, flags);
510
511         /*
512          * and then wait for interrupt handler to mark completion.
513          */
514         ret = wait_for_completion_timeout(
515                 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
516         if (ret == 0)
517                 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
518
519         devm_free_irq(priv->dev, priv->eof_irq, priv);
520         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
521
522         csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
523
524         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
525
526         /* cancel the EOF timeout timer */
527         del_timer_sync(&priv->eof_timeout_timer);
528
529         csi_idmac_put_ipu_resources(priv);
530 }
531
532 /* Update the CSI whole sensor and active windows */
533 static int csi_setup(struct csi_priv *priv)
534 {
535         struct v4l2_mbus_framefmt *infmt, *outfmt;
536         struct v4l2_mbus_config sensor_mbus_cfg;
537         struct v4l2_fwnode_endpoint *sensor_ep;
538         struct v4l2_mbus_framefmt if_fmt;
539
540         infmt = &priv->format_mbus[CSI_SINK_PAD];
541         outfmt = &priv->format_mbus[priv->active_output_pad];
542         sensor_ep = &priv->sensor->sensor_ep;
543
544         /* compose mbus_config from sensor endpoint */
545         sensor_mbus_cfg.type = sensor_ep->bus_type;
546         sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ?
547                 sensor_ep->bus.mipi_csi2.flags :
548                 sensor_ep->bus.parallel.flags;
549
550         /*
551          * we need to pass input sensor frame to CSI interface, but
552          * with translated field type from output format
553          */
554         if_fmt = *infmt;
555         if_fmt.field = outfmt->field;
556
557         ipu_csi_set_window(priv->csi, &priv->crop);
558
559         ipu_csi_set_downsize(priv->csi,
560                              priv->crop.width == 2 * outfmt->width,
561                              priv->crop.height == 2 * outfmt->height);
562
563         ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt);
564
565         ipu_csi_set_dest(priv->csi, priv->dest);
566
567         ipu_csi_dump(priv->csi);
568
569         return 0;
570 }
571
572 static int csi_start(struct csi_priv *priv)
573 {
574         u32 bad_frames = 0;
575         int ret;
576
577         if (!priv->sensor) {
578                 v4l2_err(&priv->sd, "no sensor attached\n");
579                 return -EINVAL;
580         }
581
582         ret = v4l2_subdev_call(priv->sensor->sd, sensor,
583                                g_skip_frames, &bad_frames);
584         if (!ret && bad_frames) {
585                 struct v4l2_fract *fi = &priv->frame_interval;
586                 u32 delay_usec;
587
588                 /*
589                  * This sensor has bad frames when it is turned on,
590                  * add a delay to avoid them before enabling the CSI
591                  * hardware. Especially for sensors with a bt.656 interface,
592                  * any shifts in the SAV/EAV sync codes will cause the CSI
593                  * to lose vert/horiz sync.
594                  */
595                 delay_usec = DIV_ROUND_UP_ULL(
596                         (u64)USEC_PER_SEC * fi->numerator * bad_frames,
597                         fi->denominator);
598                 usleep_range(delay_usec, delay_usec + 1000);
599         }
600
601         if (priv->dest == IPU_CSI_DEST_IDMAC) {
602                 ret = csi_idmac_start(priv);
603                 if (ret)
604                         return ret;
605         }
606
607         ret = csi_setup(priv);
608         if (ret)
609                 goto idmac_stop;
610
611         /* start the frame interval monitor */
612         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
613                 ret = imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
614                                                true);
615                 if (ret)
616                         goto idmac_stop;
617         }
618
619         ret = ipu_csi_enable(priv->csi);
620         if (ret) {
621                 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
622                 goto fim_off;
623         }
624
625         return 0;
626
627 fim_off:
628         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
629                 imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
630                                          false);
631 idmac_stop:
632         if (priv->dest == IPU_CSI_DEST_IDMAC)
633                 csi_idmac_stop(priv);
634         return ret;
635 }
636
637 static void csi_stop(struct csi_priv *priv)
638 {
639         if (priv->dest == IPU_CSI_DEST_IDMAC) {
640                 csi_idmac_stop(priv);
641
642                 /* stop the frame interval monitor */
643                 if (priv->fim)
644                         imx_media_fim_set_stream(priv->fim,
645                                                  &priv->frame_interval,
646                                                  false);
647         }
648
649         ipu_csi_disable(priv->csi);
650 }
651
652 /*
653  * V4L2 subdev operations.
654  */
655
656 static int csi_g_frame_interval(struct v4l2_subdev *sd,
657                                 struct v4l2_subdev_frame_interval *fi)
658 {
659         struct csi_priv *priv = v4l2_get_subdevdata(sd);
660
661         mutex_lock(&priv->lock);
662         fi->interval = priv->frame_interval;
663         mutex_unlock(&priv->lock);
664
665         return 0;
666 }
667
668 static int csi_s_frame_interval(struct v4l2_subdev *sd,
669                                 struct v4l2_subdev_frame_interval *fi)
670 {
671         struct csi_priv *priv = v4l2_get_subdevdata(sd);
672
673         mutex_lock(&priv->lock);
674
675         /* Output pads mirror active input pad, no limits on input pads */
676         if (fi->pad == CSI_SRC_PAD_IDMAC || fi->pad == CSI_SRC_PAD_DIRECT)
677                 fi->interval = priv->frame_interval;
678
679         priv->frame_interval = fi->interval;
680
681         mutex_unlock(&priv->lock);
682
683         return 0;
684 }
685
686 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
687 {
688         struct csi_priv *priv = v4l2_get_subdevdata(sd);
689         int ret = 0;
690
691         mutex_lock(&priv->lock);
692
693         if (!priv->src_sd || !priv->sink) {
694                 ret = -EPIPE;
695                 goto out;
696         }
697
698         /*
699          * enable/disable streaming only if stream_count is
700          * going from 0 to 1 / 1 to 0.
701          */
702         if (priv->stream_count != !enable)
703                 goto update_count;
704
705         if (enable) {
706                 /* upstream must be started first, before starting CSI */
707                 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
708                 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
709                 if (ret)
710                         goto out;
711
712                 dev_dbg(priv->dev, "stream ON\n");
713                 ret = csi_start(priv);
714                 if (ret) {
715                         v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
716                         goto out;
717                 }
718         } else {
719                 dev_dbg(priv->dev, "stream OFF\n");
720                 /* CSI must be stopped first, then stop upstream */
721                 csi_stop(priv);
722                 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
723         }
724
725 update_count:
726         priv->stream_count += enable ? 1 : -1;
727         WARN_ON(priv->stream_count < 0);
728 out:
729         mutex_unlock(&priv->lock);
730         return ret;
731 }
732
733 static int csi_link_setup(struct media_entity *entity,
734                           const struct media_pad *local,
735                           const struct media_pad *remote, u32 flags)
736 {
737         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
738         struct csi_priv *priv = v4l2_get_subdevdata(sd);
739         struct v4l2_subdev *remote_sd;
740         int ret = 0;
741
742         dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
743                 local->entity->name);
744
745         mutex_lock(&priv->lock);
746
747         if (local->flags & MEDIA_PAD_FL_SINK) {
748                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
749                         ret = -EINVAL;
750                         goto out;
751                 }
752
753                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
754
755                 if (flags & MEDIA_LNK_FL_ENABLED) {
756                         if (priv->src_sd) {
757                                 ret = -EBUSY;
758                                 goto out;
759                         }
760                         priv->src_sd = remote_sd;
761                 } else {
762                         priv->src_sd = NULL;
763                 }
764
765                 goto out;
766         }
767
768         /* this is a source pad */
769
770         if (flags & MEDIA_LNK_FL_ENABLED) {
771                 if (priv->sink) {
772                         ret = -EBUSY;
773                         goto out;
774                 }
775         } else {
776                 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
777                 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
778                 priv->sink = NULL;
779                 goto out;
780         }
781
782         /* record which output pad is now active */
783         priv->active_output_pad = local->index;
784
785         /* set CSI destination */
786         if (local->index == CSI_SRC_PAD_IDMAC) {
787                 if (!is_media_entity_v4l2_video_device(remote->entity)) {
788                         ret = -EINVAL;
789                         goto out;
790                 }
791
792                 if (priv->fim) {
793                         ret = imx_media_fim_add_controls(priv->fim);
794                         if (ret)
795                                 goto out;
796                 }
797
798                 priv->dest = IPU_CSI_DEST_IDMAC;
799         } else {
800                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
801                         ret = -EINVAL;
802                         goto out;
803                 }
804
805                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
806                 switch (remote_sd->grp_id) {
807                 case IMX_MEDIA_GRP_ID_VDIC:
808                         priv->dest = IPU_CSI_DEST_VDIC;
809                         break;
810                 case IMX_MEDIA_GRP_ID_IC_PRP:
811                         priv->dest = IPU_CSI_DEST_IC;
812                         break;
813                 default:
814                         ret = -EINVAL;
815                         goto out;
816                 }
817         }
818
819         priv->sink = remote->entity;
820 out:
821         mutex_unlock(&priv->lock);
822         return ret;
823 }
824
825 static int csi_link_validate(struct v4l2_subdev *sd,
826                              struct media_link *link,
827                              struct v4l2_subdev_format *source_fmt,
828                              struct v4l2_subdev_format *sink_fmt)
829 {
830         struct csi_priv *priv = v4l2_get_subdevdata(sd);
831         struct v4l2_fwnode_endpoint *sensor_ep;
832         const struct imx_media_pixfmt *incc;
833         struct imx_media_subdev *sensor;
834         bool is_csi2;
835         int ret;
836
837         ret = v4l2_subdev_link_validate_default(sd, link,
838                                                 source_fmt, sink_fmt);
839         if (ret)
840                 return ret;
841
842         sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity);
843         if (IS_ERR(sensor)) {
844                 v4l2_err(&priv->sd, "no sensor attached\n");
845                 return PTR_ERR(priv->sensor);
846         }
847
848         mutex_lock(&priv->lock);
849
850         priv->sensor = sensor;
851         sensor_ep = &priv->sensor->sensor_ep;
852         is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2);
853         incc = priv->cc[CSI_SINK_PAD];
854
855         if (priv->dest != IPU_CSI_DEST_IDMAC &&
856             (incc->bayer || (!is_csi2 &&
857                              sensor_ep->bus.parallel.bus_width >= 16))) {
858                 v4l2_err(&priv->sd,
859                          "bayer/16-bit parallel buses must go to IDMAC pad\n");
860                 ret = -EINVAL;
861                 goto out;
862         }
863
864         if (is_csi2) {
865                 int vc_num = 0;
866                 /*
867                  * NOTE! It seems the virtual channels from the mipi csi-2
868                  * receiver are used only for routing by the video mux's,
869                  * or for hard-wired routing to the CSI's. Once the stream
870                  * enters the CSI's however, they are treated internally
871                  * in the IPU as virtual channel 0.
872                  */
873 #if 0
874                 mutex_unlock(&priv->lock);
875                 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
876                                                           &priv->sd.entity);
877                 if (vc_num < 0)
878                         return vc_num;
879                 mutex_lock(&priv->lock);
880 #endif
881                 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
882                                           &priv->format_mbus[CSI_SINK_PAD]);
883         }
884
885         /* select either parallel or MIPI-CSI2 as input to CSI */
886         ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
887 out:
888         mutex_unlock(&priv->lock);
889         return ret;
890 }
891
892 static struct v4l2_mbus_framefmt *
893 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
894               unsigned int pad, enum v4l2_subdev_format_whence which)
895 {
896         if (which == V4L2_SUBDEV_FORMAT_TRY)
897                 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
898         else
899                 return &priv->format_mbus[pad];
900 }
901
902 static struct v4l2_rect *
903 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
904                enum v4l2_subdev_format_whence which)
905 {
906         if (which == V4L2_SUBDEV_FORMAT_TRY)
907                 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
908         else
909                 return &priv->crop;
910 }
911
912 static void csi_try_crop(struct csi_priv *priv,
913                          struct v4l2_rect *crop,
914                          struct v4l2_subdev_pad_config *cfg,
915                          struct v4l2_mbus_framefmt *infmt,
916                          struct imx_media_subdev *sensor)
917 {
918         struct v4l2_fwnode_endpoint *sensor_ep;
919
920         sensor_ep = &sensor->sensor_ep;
921
922         crop->width = min_t(__u32, infmt->width, crop->width);
923         if (crop->left + crop->width > infmt->width)
924                 crop->left = infmt->width - crop->width;
925         /* adjust crop left/width to h/w alignment restrictions */
926         crop->left &= ~0x3;
927         crop->width &= ~0x7;
928
929         /*
930          * FIXME: not sure why yet, but on interlaced bt.656,
931          * changing the vertical cropping causes loss of vertical
932          * sync, so fix it to NTSC/PAL active lines. NTSC contains
933          * 2 extra lines of active video that need to be cropped.
934          */
935         if (sensor_ep->bus_type == V4L2_MBUS_BT656 &&
936             (V4L2_FIELD_HAS_BOTH(infmt->field) ||
937              infmt->field == V4L2_FIELD_ALTERNATE)) {
938                 crop->height = infmt->height;
939                 crop->top = (infmt->height == 480) ? 2 : 0;
940         } else {
941                 crop->height = min_t(__u32, infmt->height, crop->height);
942                 if (crop->top + crop->height > infmt->height)
943                         crop->top = infmt->height - crop->height;
944         }
945 }
946
947 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
948                               struct v4l2_subdev_pad_config *cfg,
949                               struct v4l2_subdev_mbus_code_enum *code)
950 {
951         struct csi_priv *priv = v4l2_get_subdevdata(sd);
952         const struct imx_media_pixfmt *incc;
953         struct v4l2_mbus_framefmt *infmt;
954         int ret = 0;
955
956         mutex_lock(&priv->lock);
957
958         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
959         incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
960
961         switch (code->pad) {
962         case CSI_SINK_PAD:
963                 ret = imx_media_enum_mbus_format(&code->code, code->index,
964                                                  CS_SEL_ANY, true);
965                 break;
966         case CSI_SRC_PAD_DIRECT:
967         case CSI_SRC_PAD_IDMAC:
968                 if (incc->bayer) {
969                         if (code->index != 0) {
970                                 ret = -EINVAL;
971                                 goto out;
972                         }
973                         code->code = infmt->code;
974                 } else {
975                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
976                                 CS_SEL_YUV : CS_SEL_RGB;
977                         ret = imx_media_enum_ipu_format(&code->code,
978                                                         code->index,
979                                                         cs_sel);
980                 }
981                 break;
982         default:
983                 ret = -EINVAL;
984         }
985
986 out:
987         mutex_unlock(&priv->lock);
988         return ret;
989 }
990
991 static int csi_get_fmt(struct v4l2_subdev *sd,
992                        struct v4l2_subdev_pad_config *cfg,
993                        struct v4l2_subdev_format *sdformat)
994 {
995         struct csi_priv *priv = v4l2_get_subdevdata(sd);
996         struct v4l2_mbus_framefmt *fmt;
997         int ret = 0;
998
999         if (sdformat->pad >= CSI_NUM_PADS)
1000                 return -EINVAL;
1001
1002         mutex_lock(&priv->lock);
1003
1004         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1005         if (!fmt) {
1006                 ret = -EINVAL;
1007                 goto out;
1008         }
1009
1010         sdformat->format = *fmt;
1011 out:
1012         mutex_unlock(&priv->lock);
1013         return ret;
1014 }
1015
1016 static void csi_try_fmt(struct csi_priv *priv,
1017                         struct imx_media_subdev *sensor,
1018                         struct v4l2_subdev_pad_config *cfg,
1019                         struct v4l2_subdev_format *sdformat,
1020                         struct v4l2_rect *crop,
1021                         const struct imx_media_pixfmt **cc)
1022 {
1023         const struct imx_media_pixfmt *incc;
1024         struct v4l2_mbus_framefmt *infmt;
1025         u32 code;
1026
1027         switch (sdformat->pad) {
1028         case CSI_SRC_PAD_DIRECT:
1029         case CSI_SRC_PAD_IDMAC:
1030                 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD,
1031                                       sdformat->which);
1032                 incc = imx_media_find_mbus_format(infmt->code,
1033                                                   CS_SEL_ANY, true);
1034
1035                 if (sdformat->format.width < crop->width * 3 / 4)
1036                         sdformat->format.width = crop->width / 2;
1037                 else
1038                         sdformat->format.width = crop->width;
1039
1040                 if (sdformat->format.height < crop->height * 3 / 4)
1041                         sdformat->format.height = crop->height / 2;
1042                 else
1043                         sdformat->format.height = crop->height;
1044
1045                 if (incc->bayer) {
1046                         sdformat->format.code = infmt->code;
1047                         *cc = incc;
1048                 } else {
1049                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1050                                 CS_SEL_YUV : CS_SEL_RGB;
1051
1052                         *cc = imx_media_find_ipu_format(sdformat->format.code,
1053                                                         cs_sel);
1054                         if (!*cc) {
1055                                 imx_media_enum_ipu_format(&code, 0, cs_sel);
1056                                 *cc = imx_media_find_ipu_format(code, cs_sel);
1057                                 sdformat->format.code = (*cc)->codes[0];
1058                         }
1059                 }
1060
1061                 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1062                     sdformat->format.field != V4L2_FIELD_NONE)
1063                         sdformat->format.field = infmt->field;
1064
1065                 /*
1066                  * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1067                  * depending on input height (assume NTSC top-bottom
1068                  * order if 480 lines, otherwise PAL bottom-top order).
1069                  */
1070                 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1071                         sdformat->format.field =  (infmt->height == 480) ?
1072                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1073                 }
1074                 break;
1075         case CSI_SINK_PAD:
1076                 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1077                                       W_ALIGN, &sdformat->format.height,
1078                                       MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1079                 crop->left = 0;
1080                 crop->top = 0;
1081                 crop->width = sdformat->format.width;
1082                 crop->height = sdformat->format.height;
1083                 csi_try_crop(priv, crop, cfg, &sdformat->format, sensor);
1084
1085                 *cc = imx_media_find_mbus_format(sdformat->format.code,
1086                                                  CS_SEL_ANY, true);
1087                 if (!*cc) {
1088                         imx_media_enum_mbus_format(&code, 0,
1089                                                    CS_SEL_ANY, false);
1090                         *cc = imx_media_find_mbus_format(code,
1091                                                         CS_SEL_ANY, false);
1092                         sdformat->format.code = (*cc)->codes[0];
1093                 }
1094                 break;
1095         }
1096 }
1097
1098 static int csi_set_fmt(struct v4l2_subdev *sd,
1099                        struct v4l2_subdev_pad_config *cfg,
1100                        struct v4l2_subdev_format *sdformat)
1101 {
1102         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1103         struct imx_media_video_dev *vdev = priv->vdev;
1104         const struct imx_media_pixfmt *cc;
1105         struct imx_media_subdev *sensor;
1106         struct v4l2_pix_format vdev_fmt;
1107         struct v4l2_mbus_framefmt *fmt;
1108         struct v4l2_rect *crop;
1109         int ret = 0;
1110
1111         if (sdformat->pad >= CSI_NUM_PADS)
1112                 return -EINVAL;
1113
1114         sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1115         if (IS_ERR(sensor)) {
1116                 v4l2_err(&priv->sd, "no sensor attached\n");
1117                 return PTR_ERR(sensor);
1118         }
1119
1120         mutex_lock(&priv->lock);
1121
1122         if (priv->stream_count > 0) {
1123                 ret = -EBUSY;
1124                 goto out;
1125         }
1126
1127         crop = __csi_get_crop(priv, cfg, sdformat->which);
1128
1129         csi_try_fmt(priv, sensor, cfg, sdformat, crop, &cc);
1130
1131         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1132         *fmt = sdformat->format;
1133
1134         if (sdformat->pad == CSI_SINK_PAD) {
1135                 int pad;
1136
1137                 /* propagate format to source pads */
1138                 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1139                         const struct imx_media_pixfmt *outcc;
1140                         struct v4l2_mbus_framefmt *outfmt;
1141                         struct v4l2_subdev_format format;
1142
1143                         format.pad = pad;
1144                         format.which = sdformat->which;
1145                         format.format = sdformat->format;
1146                         csi_try_fmt(priv, sensor, cfg, &format, crop, &outcc);
1147
1148                         outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1149                         *outfmt = format.format;
1150
1151                         if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1152                                 priv->cc[pad] = outcc;
1153                 }
1154         }
1155
1156         if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1157                 goto out;
1158
1159         priv->cc[sdformat->pad] = cc;
1160
1161         /* propagate IDMAC output pad format to capture device */
1162         imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1163                                       &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1164                                       priv->cc[CSI_SRC_PAD_IDMAC]);
1165         mutex_unlock(&priv->lock);
1166         imx_media_capture_device_set_format(vdev, &vdev_fmt);
1167
1168         return 0;
1169 out:
1170         mutex_unlock(&priv->lock);
1171         return ret;
1172 }
1173
1174 static int csi_get_selection(struct v4l2_subdev *sd,
1175                              struct v4l2_subdev_pad_config *cfg,
1176                              struct v4l2_subdev_selection *sel)
1177 {
1178         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1179         struct v4l2_mbus_framefmt *infmt;
1180         struct v4l2_rect *crop;
1181         int ret = 0;
1182
1183         if (sel->pad >= CSI_NUM_PADS || sel->pad == CSI_SINK_PAD)
1184                 return -EINVAL;
1185
1186         mutex_lock(&priv->lock);
1187
1188         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1189         crop = __csi_get_crop(priv, cfg, sel->which);
1190
1191         switch (sel->target) {
1192         case V4L2_SEL_TGT_CROP_BOUNDS:
1193                 sel->r.left = 0;
1194                 sel->r.top = 0;
1195                 sel->r.width = infmt->width;
1196                 sel->r.height = infmt->height;
1197                 break;
1198         case V4L2_SEL_TGT_CROP:
1199                 sel->r = *crop;
1200                 break;
1201         default:
1202                 ret = -EINVAL;
1203         }
1204
1205         mutex_unlock(&priv->lock);
1206         return ret;
1207 }
1208
1209 static int csi_set_selection(struct v4l2_subdev *sd,
1210                              struct v4l2_subdev_pad_config *cfg,
1211                              struct v4l2_subdev_selection *sel)
1212 {
1213         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1214         struct v4l2_mbus_framefmt *infmt;
1215         struct imx_media_subdev *sensor;
1216         struct v4l2_rect *crop;
1217         int pad, ret = 0;
1218
1219         if (sel->pad >= CSI_NUM_PADS ||
1220             sel->pad == CSI_SINK_PAD ||
1221             sel->target != V4L2_SEL_TGT_CROP)
1222                 return -EINVAL;
1223
1224         sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1225         if (IS_ERR(sensor)) {
1226                 v4l2_err(&priv->sd, "no sensor attached\n");
1227                 return PTR_ERR(sensor);
1228         }
1229
1230         mutex_lock(&priv->lock);
1231
1232         if (priv->stream_count > 0) {
1233                 ret = -EBUSY;
1234                 goto out;
1235         }
1236
1237         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1238         crop = __csi_get_crop(priv, cfg, sel->which);
1239
1240         /*
1241          * Modifying the crop rectangle always changes the format on the source
1242          * pad. If the KEEP_CONFIG flag is set, just return the current crop
1243          * rectangle.
1244          */
1245         if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1246                 sel->r = priv->crop;
1247                 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1248                         *crop = sel->r;
1249                 goto out;
1250         }
1251
1252         csi_try_crop(priv, &sel->r, cfg, infmt, sensor);
1253
1254         *crop = sel->r;
1255
1256         /* Update the source pad formats */
1257         for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1258                 struct v4l2_mbus_framefmt *outfmt;
1259
1260                 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1261                 outfmt->width = crop->width;
1262                 outfmt->height = crop->height;
1263         }
1264
1265 out:
1266         mutex_unlock(&priv->lock);
1267         return ret;
1268 }
1269
1270 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1271                                struct v4l2_event_subscription *sub)
1272 {
1273         if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1274                 return -EINVAL;
1275         if (sub->id != 0)
1276                 return -EINVAL;
1277
1278         return v4l2_event_subscribe(fh, sub, 0, NULL);
1279 }
1280
1281 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1282                                  struct v4l2_event_subscription *sub)
1283 {
1284         return v4l2_event_unsubscribe(fh, sub);
1285 }
1286
1287 /*
1288  * retrieve our pads parsed from the OF graph by the media device
1289  */
1290 static int csi_registered(struct v4l2_subdev *sd)
1291 {
1292         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1293         int i, ret;
1294         u32 code;
1295
1296         /* get media device */
1297         priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1298
1299         /* get handle to IPU CSI */
1300         priv->csi = ipu_csi_get(priv->ipu, priv->csi_id);
1301         if (IS_ERR(priv->csi)) {
1302                 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1303                 return PTR_ERR(priv->csi);
1304         }
1305
1306         for (i = 0; i < CSI_NUM_PADS; i++) {
1307                 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1308                         MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1309
1310                 code = 0;
1311                 if (i != CSI_SINK_PAD)
1312                         imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1313
1314                 /* set a default mbus format  */
1315                 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1316                                               640, 480, code, V4L2_FIELD_NONE,
1317                                               &priv->cc[i]);
1318                 if (ret)
1319                         goto put_csi;
1320         }
1321
1322         /* init default frame interval */
1323         priv->frame_interval.numerator = 1;
1324         priv->frame_interval.denominator = 30;
1325
1326         priv->fim = imx_media_fim_init(&priv->sd);
1327         if (IS_ERR(priv->fim)) {
1328                 ret = PTR_ERR(priv->fim);
1329                 goto put_csi;
1330         }
1331
1332         ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1333         if (ret)
1334                 goto free_fim;
1335
1336         ret = imx_media_capture_device_register(priv->vdev);
1337         if (ret)
1338                 goto free_fim;
1339
1340         ret = imx_media_add_video_device(priv->md, priv->vdev);
1341         if (ret)
1342                 goto unreg;
1343
1344         return 0;
1345 unreg:
1346         imx_media_capture_device_unregister(priv->vdev);
1347 free_fim:
1348         if (priv->fim)
1349                 imx_media_fim_free(priv->fim);
1350 put_csi:
1351         ipu_csi_put(priv->csi);
1352         return ret;
1353 }
1354
1355 static void csi_unregistered(struct v4l2_subdev *sd)
1356 {
1357         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1358
1359         imx_media_capture_device_unregister(priv->vdev);
1360
1361         if (priv->fim)
1362                 imx_media_fim_free(priv->fim);
1363
1364         if (!IS_ERR_OR_NULL(priv->csi))
1365                 ipu_csi_put(priv->csi);
1366 }
1367
1368 static const struct media_entity_operations csi_entity_ops = {
1369         .link_setup = csi_link_setup,
1370         .link_validate = v4l2_subdev_link_validate,
1371 };
1372
1373 static const struct v4l2_subdev_core_ops csi_core_ops = {
1374         .subscribe_event = csi_subscribe_event,
1375         .unsubscribe_event = csi_unsubscribe_event,
1376 };
1377
1378 static const struct v4l2_subdev_video_ops csi_video_ops = {
1379         .g_frame_interval = csi_g_frame_interval,
1380         .s_frame_interval = csi_s_frame_interval,
1381         .s_stream = csi_s_stream,
1382 };
1383
1384 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1385         .enum_mbus_code = csi_enum_mbus_code,
1386         .get_fmt = csi_get_fmt,
1387         .set_fmt = csi_set_fmt,
1388         .get_selection = csi_get_selection,
1389         .set_selection = csi_set_selection,
1390         .link_validate = csi_link_validate,
1391 };
1392
1393 static const struct v4l2_subdev_ops csi_subdev_ops = {
1394         .core = &csi_core_ops,
1395         .video = &csi_video_ops,
1396         .pad = &csi_pad_ops,
1397 };
1398
1399 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1400         .registered = csi_registered,
1401         .unregistered = csi_unregistered,
1402 };
1403
1404 static int imx_csi_probe(struct platform_device *pdev)
1405 {
1406         struct ipu_client_platformdata *pdata;
1407         struct pinctrl *pinctrl;
1408         struct csi_priv *priv;
1409         int ret;
1410
1411         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1412         if (!priv)
1413                 return -ENOMEM;
1414
1415         platform_set_drvdata(pdev, &priv->sd);
1416         priv->dev = &pdev->dev;
1417
1418         ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1419         if (ret)
1420                 return ret;
1421
1422         /* get parent IPU */
1423         priv->ipu = dev_get_drvdata(priv->dev->parent);
1424
1425         /* get our CSI id */
1426         pdata = priv->dev->platform_data;
1427         priv->csi_id = pdata->csi;
1428         priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1429
1430         init_timer(&priv->eof_timeout_timer);
1431         priv->eof_timeout_timer.data = (unsigned long)priv;
1432         priv->eof_timeout_timer.function = csi_idmac_eof_timeout;
1433         spin_lock_init(&priv->irqlock);
1434
1435         v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1436         v4l2_set_subdevdata(&priv->sd, priv);
1437         priv->sd.internal_ops = &csi_internal_ops;
1438         priv->sd.entity.ops = &csi_entity_ops;
1439         priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1440         priv->sd.dev = &pdev->dev;
1441         priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1442         priv->sd.owner = THIS_MODULE;
1443         priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1444         priv->sd.grp_id = priv->csi_id ?
1445                 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1446         imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1447                                     priv->sd.grp_id, ipu_get_num(priv->ipu));
1448
1449         priv->vdev = imx_media_capture_device_init(&priv->sd,
1450                                                    CSI_SRC_PAD_IDMAC);
1451         if (IS_ERR(priv->vdev))
1452                 return PTR_ERR(priv->vdev);
1453
1454         mutex_init(&priv->lock);
1455
1456         v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1457         priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1458
1459         /*
1460          * The IPUv3 driver did not assign an of_node to this
1461          * device. As a result, pinctrl does not automatically
1462          * configure our pin groups, so we need to do that manually
1463          * here, after setting this device's of_node.
1464          */
1465         priv->dev->of_node = pdata->of_node;
1466         pinctrl = devm_pinctrl_get_select_default(priv->dev);
1467
1468         ret = v4l2_async_register_subdev(&priv->sd);
1469         if (ret)
1470                 goto free;
1471
1472         return 0;
1473 free:
1474         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1475         mutex_destroy(&priv->lock);
1476         imx_media_capture_device_remove(priv->vdev);
1477         return ret;
1478 }
1479
1480 static int imx_csi_remove(struct platform_device *pdev)
1481 {
1482         struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1483         struct csi_priv *priv = sd_to_dev(sd);
1484
1485         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1486         mutex_destroy(&priv->lock);
1487         imx_media_capture_device_remove(priv->vdev);
1488         v4l2_async_unregister_subdev(sd);
1489         media_entity_cleanup(&sd->entity);
1490
1491         return 0;
1492 }
1493
1494 static const struct platform_device_id imx_csi_ids[] = {
1495         { .name = "imx-ipuv3-csi" },
1496         { },
1497 };
1498 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1499
1500 static struct platform_driver imx_csi_driver = {
1501         .probe = imx_csi_probe,
1502         .remove = imx_csi_remove,
1503         .id_table = imx_csi_ids,
1504         .driver = {
1505                 .name = "imx-ipuv3-csi",
1506         },
1507 };
1508 module_platform_driver(imx_csi_driver);
1509
1510 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1511 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1512 MODULE_LICENSE("GPL");
1513 MODULE_ALIAS("platform:imx-ipuv3-csi");