GNU Linux-libre 4.19.286-gnu1
[releases.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  * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 #include <linux/delay.h>
13 #include <linux/gcd.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-mc.h>
24 #include <media/v4l2-subdev.h>
25 #include <media/videobuf2-dma-contig.h>
26 #include <video/imx-ipu-v3.h>
27 #include <media/imx.h>
28 #include "imx-media.h"
29
30 /*
31  * Min/Max supported width and heights.
32  *
33  * We allow planar output, so we have to align width by 16 pixels
34  * to meet IDMAC alignment requirements.
35  *
36  * TODO: move this into pad format negotiation, if capture device
37  * has not requested planar formats, we should allow 8 pixel
38  * alignment.
39  */
40 #define MIN_W       176
41 #define MIN_H       144
42 #define MAX_W      4096
43 #define MAX_H      4096
44 #define W_ALIGN    4 /* multiple of 16 pixels */
45 #define H_ALIGN    1 /* multiple of 2 lines */
46 #define S_ALIGN    1 /* multiple of 2 */
47
48 /*
49  * struct csi_skip_desc - CSI frame skipping descriptor
50  * @keep - number of frames kept per max_ratio frames
51  * @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield
52  * @skip_smfc - skip pattern written to the SKIP_SMFC bitfield
53  */
54 struct csi_skip_desc {
55         u8 keep;
56         u8 max_ratio;
57         u8 skip_smfc;
58 };
59
60 struct csi_priv {
61         struct device *dev;
62         struct ipu_soc *ipu;
63         struct imx_media_dev *md;
64         struct v4l2_subdev sd;
65         struct media_pad pad[CSI_NUM_PADS];
66         /* the video device at IDMAC output pad */
67         struct imx_media_video_dev *vdev;
68         struct imx_media_fim *fim;
69         int csi_id;
70         int smfc_id;
71
72         /* lock to protect all members below */
73         struct mutex lock;
74
75         int active_output_pad;
76
77         struct ipuv3_channel *idmac_ch;
78         struct ipu_smfc *smfc;
79         struct ipu_csi *csi;
80
81         struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
82         const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
83         struct v4l2_fract frame_interval[CSI_NUM_PADS];
84         struct v4l2_rect crop;
85         struct v4l2_rect compose;
86         const struct csi_skip_desc *skip;
87
88         /* active vb2 buffers to send to video dev sink */
89         struct imx_media_buffer *active_vb2_buf[2];
90         struct imx_media_dma_buf underrun_buf;
91
92         int ipu_buf_num;  /* ipu double buffer index: 0-1 */
93
94         /* the sink for the captured frames */
95         struct media_entity *sink;
96         enum ipu_csi_dest dest;
97         /* the source subdev */
98         struct v4l2_subdev *src_sd;
99
100         /* the mipi virtual channel number at link validate */
101         int vc_num;
102
103         /* the upstream endpoint CSI is receiving from */
104         struct v4l2_fwnode_endpoint upstream_ep;
105
106         spinlock_t irqlock; /* protect eof_irq handler */
107         struct timer_list eof_timeout_timer;
108         int eof_irq;
109         int nfb4eof_irq;
110
111         struct v4l2_ctrl_handler ctrl_hdlr;
112
113         int stream_count; /* streaming counter */
114         u32 frame_sequence; /* frame sequence counter */
115         bool last_eof;   /* waiting for last EOF at stream off */
116         bool nfb4eof;    /* NFB4EOF encountered during streaming */
117         struct completion last_eof_comp;
118 };
119
120 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
121 {
122         return container_of(sdev, struct csi_priv, sd);
123 }
124
125 static inline bool is_parallel_bus(struct v4l2_fwnode_endpoint *ep)
126 {
127         return ep->bus_type != V4L2_MBUS_CSI2;
128 }
129
130 static inline bool is_parallel_16bit_bus(struct v4l2_fwnode_endpoint *ep)
131 {
132         return is_parallel_bus(ep) && ep->bus.parallel.bus_width >= 16;
133 }
134
135 /*
136  * Check for conditions that require the IPU to handle the
137  * data internally as generic data, aka passthrough mode:
138  * - raw bayer media bus formats, or
139  * - the CSI is receiving from a 16-bit parallel bus, or
140  * - the CSI is receiving from an 8-bit parallel bus and the incoming
141  *   media bus format is other than UYVY8_2X8/YUYV8_2X8.
142  */
143 static inline bool requires_passthrough(struct v4l2_fwnode_endpoint *ep,
144                                         struct v4l2_mbus_framefmt *infmt,
145                                         const struct imx_media_pixfmt *incc)
146 {
147         return incc->bayer || is_parallel_16bit_bus(ep) ||
148                 (is_parallel_bus(ep) &&
149                  infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 &&
150                  infmt->code != MEDIA_BUS_FMT_YUYV8_2X8);
151 }
152
153 /*
154  * Parses the fwnode endpoint from the source pad of the entity
155  * connected to this CSI. This will either be the entity directly
156  * upstream from the CSI-2 receiver, directly upstream from the
157  * video mux, or directly upstream from the CSI itself. The endpoint
158  * is needed to determine the bus type and bus config coming into
159  * the CSI.
160  */
161 static int csi_get_upstream_endpoint(struct csi_priv *priv,
162                                      struct v4l2_fwnode_endpoint *ep)
163 {
164         struct device_node *endpoint, *port;
165         struct media_entity *src;
166         struct v4l2_subdev *sd;
167         struct media_pad *pad;
168
169         if (!IS_ENABLED(CONFIG_OF))
170                 return -ENXIO;
171
172         if (!priv->src_sd)
173                 return -EPIPE;
174
175         sd = priv->src_sd;
176         src = &sd->entity;
177
178         if (src->function == MEDIA_ENT_F_VID_MUX) {
179                 /*
180                  * CSI is connected directly to video mux, skip up to
181                  * CSI-2 receiver if it is in the path, otherwise stay
182                  * with video mux.
183                  */
184                 sd = imx_media_find_upstream_subdev(priv->md, src,
185                                                     IMX_MEDIA_GRP_ID_CSI2);
186                 if (!IS_ERR(sd))
187                         src = &sd->entity;
188         }
189
190         /*
191          * If the source is neither the video mux nor the CSI-2 receiver,
192          * get the source pad directly upstream from CSI itself.
193          */
194         if (src->function != MEDIA_ENT_F_VID_MUX &&
195             sd->grp_id != IMX_MEDIA_GRP_ID_CSI2)
196                 src = &priv->sd.entity;
197
198         /* get source pad of entity directly upstream from src */
199         pad = imx_media_find_upstream_pad(priv->md, src, 0);
200         if (IS_ERR(pad))
201                 return PTR_ERR(pad);
202
203         sd = media_entity_to_v4l2_subdev(pad->entity);
204
205         /*
206          * NOTE: this assumes an OF-graph port id is the same as a
207          * media pad index.
208          */
209         port = of_graph_get_port_by_id(sd->dev->of_node, pad->index);
210         if (!port)
211                 return -ENODEV;
212
213         endpoint = of_get_next_child(port, NULL);
214         of_node_put(port);
215         if (!endpoint)
216                 return -ENODEV;
217
218         v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), ep);
219         of_node_put(endpoint);
220
221         return 0;
222 }
223
224 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
225 {
226         if (priv->idmac_ch)
227                 ipu_idmac_put(priv->idmac_ch);
228         priv->idmac_ch = NULL;
229
230         if (priv->smfc)
231                 ipu_smfc_put(priv->smfc);
232         priv->smfc = NULL;
233 }
234
235 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
236 {
237         int ch_num, ret;
238         struct ipu_smfc *smfc;
239         struct ipuv3_channel *idmac_ch;
240
241         ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
242
243         smfc = ipu_smfc_get(priv->ipu, ch_num);
244         if (IS_ERR(smfc)) {
245                 v4l2_err(&priv->sd, "failed to get SMFC\n");
246                 ret = PTR_ERR(smfc);
247                 goto out;
248         }
249         priv->smfc = smfc;
250
251         idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
252         if (IS_ERR(idmac_ch)) {
253                 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
254                          ch_num);
255                 ret = PTR_ERR(idmac_ch);
256                 goto out;
257         }
258         priv->idmac_ch = idmac_ch;
259
260         return 0;
261 out:
262         csi_idmac_put_ipu_resources(priv);
263         return ret;
264 }
265
266 static void csi_vb2_buf_done(struct csi_priv *priv)
267 {
268         struct imx_media_video_dev *vdev = priv->vdev;
269         struct imx_media_buffer *done, *next;
270         struct vb2_buffer *vb;
271         dma_addr_t phys;
272
273         done = priv->active_vb2_buf[priv->ipu_buf_num];
274         if (done) {
275                 done->vbuf.field = vdev->fmt.fmt.pix.field;
276                 done->vbuf.sequence = priv->frame_sequence;
277                 vb = &done->vbuf.vb2_buf;
278                 vb->timestamp = ktime_get_ns();
279                 vb2_buffer_done(vb, priv->nfb4eof ?
280                                 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
281         }
282
283         priv->frame_sequence++;
284         priv->nfb4eof = false;
285
286         /* get next queued buffer */
287         next = imx_media_capture_device_next_buf(vdev);
288         if (next) {
289                 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
290                 priv->active_vb2_buf[priv->ipu_buf_num] = next;
291         } else {
292                 phys = priv->underrun_buf.phys;
293                 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
294         }
295
296         if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
297                 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
298
299         ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
300 }
301
302 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
303 {
304         struct csi_priv *priv = dev_id;
305
306         spin_lock(&priv->irqlock);
307
308         if (priv->last_eof) {
309                 complete(&priv->last_eof_comp);
310                 priv->last_eof = false;
311                 goto unlock;
312         }
313
314         if (priv->fim)
315                 /* call frame interval monitor */
316                 imx_media_fim_eof_monitor(priv->fim, ktime_get());
317
318         csi_vb2_buf_done(priv);
319
320         /* select new IPU buf */
321         ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
322         /* toggle IPU double-buffer index */
323         priv->ipu_buf_num ^= 1;
324
325         /* bump the EOF timeout timer */
326         mod_timer(&priv->eof_timeout_timer,
327                   jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
328
329 unlock:
330         spin_unlock(&priv->irqlock);
331         return IRQ_HANDLED;
332 }
333
334 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
335 {
336         struct csi_priv *priv = dev_id;
337
338         spin_lock(&priv->irqlock);
339
340         /*
341          * this is not an unrecoverable error, just mark
342          * the next captured frame with vb2 error flag.
343          */
344         priv->nfb4eof = true;
345
346         v4l2_err(&priv->sd, "NFB4EOF\n");
347
348         spin_unlock(&priv->irqlock);
349
350         return IRQ_HANDLED;
351 }
352
353 /*
354  * EOF timeout timer function. This is an unrecoverable condition
355  * without a stream restart.
356  */
357 static void csi_idmac_eof_timeout(struct timer_list *t)
358 {
359         struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer);
360         struct imx_media_video_dev *vdev = priv->vdev;
361
362         v4l2_err(&priv->sd, "EOF timeout\n");
363
364         /* signal a fatal error to capture device */
365         imx_media_capture_device_error(vdev);
366 }
367
368 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
369 {
370         struct imx_media_video_dev *vdev = priv->vdev;
371         struct imx_media_buffer *buf;
372         int i;
373
374         for (i = 0; i < 2; i++) {
375                 buf = imx_media_capture_device_next_buf(vdev);
376                 if (buf) {
377                         priv->active_vb2_buf[i] = buf;
378                         phys[i] = vb2_dma_contig_plane_dma_addr(
379                                 &buf->vbuf.vb2_buf, 0);
380                 } else {
381                         priv->active_vb2_buf[i] = NULL;
382                         phys[i] = priv->underrun_buf.phys;
383                 }
384         }
385 }
386
387 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
388                                       enum vb2_buffer_state return_status)
389 {
390         struct imx_media_buffer *buf;
391         int i;
392
393         /* return any remaining active frames with return_status */
394         for (i = 0; i < 2; i++) {
395                 buf = priv->active_vb2_buf[i];
396                 if (buf) {
397                         struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
398
399                         vb->timestamp = ktime_get_ns();
400                         vb2_buffer_done(vb, return_status);
401                 }
402         }
403 }
404
405 /* init the SMFC IDMAC channel */
406 static int csi_idmac_setup_channel(struct csi_priv *priv)
407 {
408         struct imx_media_video_dev *vdev = priv->vdev;
409         const struct imx_media_pixfmt *incc;
410         struct v4l2_mbus_framefmt *infmt;
411         struct ipu_image image;
412         u32 passthrough_bits;
413         u32 passthrough_cycles;
414         dma_addr_t phys[2];
415         bool passthrough;
416         u32 burst_size;
417         int ret;
418
419         infmt = &priv->format_mbus[CSI_SINK_PAD];
420         incc = priv->cc[CSI_SINK_PAD];
421
422         ipu_cpmem_zero(priv->idmac_ch);
423
424         memset(&image, 0, sizeof(image));
425         image.pix = vdev->fmt.fmt.pix;
426         image.rect.width = image.pix.width;
427         image.rect.height = image.pix.height;
428
429         csi_idmac_setup_vb2_buf(priv, phys);
430
431         image.phys0 = phys[0];
432         image.phys1 = phys[1];
433
434         passthrough = requires_passthrough(&priv->upstream_ep, infmt, incc);
435         passthrough_cycles = 1;
436
437         switch (image.pix.pixelformat) {
438         case V4L2_PIX_FMT_SBGGR8:
439         case V4L2_PIX_FMT_SGBRG8:
440         case V4L2_PIX_FMT_SGRBG8:
441         case V4L2_PIX_FMT_SRGGB8:
442         case V4L2_PIX_FMT_GREY:
443                 burst_size = 16;
444                 passthrough_bits = 8;
445                 break;
446         case V4L2_PIX_FMT_SBGGR16:
447         case V4L2_PIX_FMT_SGBRG16:
448         case V4L2_PIX_FMT_SGRBG16:
449         case V4L2_PIX_FMT_SRGGB16:
450         case V4L2_PIX_FMT_Y16:
451                 burst_size = 8;
452                 passthrough_bits = 16;
453                 break;
454         case V4L2_PIX_FMT_YUV420:
455         case V4L2_PIX_FMT_NV12:
456                 burst_size = (image.pix.width & 0x3f) ?
457                              ((image.pix.width & 0x1f) ?
458                               ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
459                 passthrough_bits = 16;
460                 /* Skip writing U and V components to odd rows */
461                 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
462                 break;
463         case V4L2_PIX_FMT_YUYV:
464         case V4L2_PIX_FMT_UYVY:
465                 burst_size = (image.pix.width & 0x1f) ?
466                              ((image.pix.width & 0xf) ? 8 : 16) : 32;
467                 passthrough_bits = 16;
468                 break;
469         case V4L2_PIX_FMT_RGB565:
470                 if (passthrough) {
471                         burst_size = 16;
472                         passthrough_bits = 8;
473                         passthrough_cycles = incc->cycles;
474                         break;
475                 }
476                 /* fallthrough - non-passthrough RGB565 (CSI-2 bus) */
477         default:
478                 burst_size = (image.pix.width & 0xf) ? 8 : 16;
479                 passthrough_bits = 16;
480                 break;
481         }
482
483         if (passthrough) {
484                 ipu_cpmem_set_resolution(priv->idmac_ch,
485                                          image.rect.width * passthrough_cycles,
486                                          image.rect.height);
487                 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
488                 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
489                 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
490                 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
491                                                  passthrough_bits);
492         } else {
493                 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
494                 if (ret)
495                         goto unsetup_vb2;
496         }
497
498         ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
499
500         /*
501          * Set the channel for the direct CSI-->memory via SMFC
502          * use-case to very high priority, by enabling the watermark
503          * signal in the SMFC, enabling WM in the channel, and setting
504          * the channel priority to high.
505          *
506          * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
507          * value.
508          *
509          * The WM's are set very low by intention here to ensure that
510          * the SMFC FIFOs do not overflow.
511          */
512         ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
513         ipu_cpmem_set_high_priority(priv->idmac_ch);
514         ipu_idmac_enable_watermark(priv->idmac_ch, true);
515         ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
516
517         burst_size = passthrough ?
518                 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
519
520         ipu_smfc_set_burstsize(priv->smfc, burst_size);
521
522         if (image.pix.field == V4L2_FIELD_NONE &&
523             V4L2_FIELD_HAS_BOTH(infmt->field))
524                 ipu_cpmem_interlaced_scan(priv->idmac_ch,
525                                           image.pix.bytesperline);
526
527         ipu_idmac_set_double_buffer(priv->idmac_ch, true);
528
529         return 0;
530
531 unsetup_vb2:
532         csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
533         return ret;
534 }
535
536 static void csi_idmac_unsetup(struct csi_priv *priv,
537                               enum vb2_buffer_state state)
538 {
539         ipu_idmac_disable_channel(priv->idmac_ch);
540         ipu_smfc_disable(priv->smfc);
541
542         csi_idmac_unsetup_vb2_buf(priv, state);
543 }
544
545 static int csi_idmac_setup(struct csi_priv *priv)
546 {
547         int ret;
548
549         ret = csi_idmac_setup_channel(priv);
550         if (ret)
551                 return ret;
552
553         ipu_cpmem_dump(priv->idmac_ch);
554         ipu_dump(priv->ipu);
555
556         ipu_smfc_enable(priv->smfc);
557
558         /* set buffers ready */
559         ipu_idmac_select_buffer(priv->idmac_ch, 0);
560         ipu_idmac_select_buffer(priv->idmac_ch, 1);
561
562         /* enable the channels */
563         ipu_idmac_enable_channel(priv->idmac_ch);
564
565         return 0;
566 }
567
568 static int csi_idmac_start(struct csi_priv *priv)
569 {
570         struct imx_media_video_dev *vdev = priv->vdev;
571         struct v4l2_pix_format *outfmt;
572         int ret;
573
574         ret = csi_idmac_get_ipu_resources(priv);
575         if (ret)
576                 return ret;
577
578         ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
579
580         outfmt = &vdev->fmt.fmt.pix;
581
582         ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
583                                       outfmt->sizeimage);
584         if (ret)
585                 goto out_put_ipu;
586
587         priv->ipu_buf_num = 0;
588
589         /* init EOF completion waitq */
590         init_completion(&priv->last_eof_comp);
591         priv->frame_sequence = 0;
592         priv->last_eof = false;
593         priv->nfb4eof = false;
594
595         ret = csi_idmac_setup(priv);
596         if (ret) {
597                 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
598                 goto out_free_dma_buf;
599         }
600
601         priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
602                                                  priv->idmac_ch,
603                                                  IPU_IRQ_NFB4EOF);
604         ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
605                                csi_idmac_nfb4eof_interrupt, 0,
606                                "imx-smfc-nfb4eof", priv);
607         if (ret) {
608                 v4l2_err(&priv->sd,
609                          "Error registering NFB4EOF irq: %d\n", ret);
610                 goto out_unsetup;
611         }
612
613         priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
614                                               IPU_IRQ_EOF);
615
616         ret = devm_request_irq(priv->dev, priv->eof_irq,
617                                csi_idmac_eof_interrupt, 0,
618                                "imx-smfc-eof", priv);
619         if (ret) {
620                 v4l2_err(&priv->sd,
621                          "Error registering eof irq: %d\n", ret);
622                 goto out_free_nfb4eof_irq;
623         }
624
625         /* start the EOF timeout timer */
626         mod_timer(&priv->eof_timeout_timer,
627                   jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
628
629         return 0;
630
631 out_free_nfb4eof_irq:
632         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
633 out_unsetup:
634         csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
635 out_free_dma_buf:
636         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
637 out_put_ipu:
638         csi_idmac_put_ipu_resources(priv);
639         return ret;
640 }
641
642 static void csi_idmac_wait_last_eof(struct csi_priv *priv)
643 {
644         unsigned long flags;
645         int ret;
646
647         /* mark next EOF interrupt as the last before stream off */
648         spin_lock_irqsave(&priv->irqlock, flags);
649         priv->last_eof = true;
650         spin_unlock_irqrestore(&priv->irqlock, flags);
651
652         /*
653          * and then wait for interrupt handler to mark completion.
654          */
655         ret = wait_for_completion_timeout(
656                 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
657         if (ret == 0)
658                 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
659 }
660
661 static void csi_idmac_stop(struct csi_priv *priv)
662 {
663         devm_free_irq(priv->dev, priv->eof_irq, priv);
664         devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
665
666         csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
667
668         imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
669
670         /* cancel the EOF timeout timer */
671         del_timer_sync(&priv->eof_timeout_timer);
672
673         csi_idmac_put_ipu_resources(priv);
674 }
675
676 /* Update the CSI whole sensor and active windows */
677 static int csi_setup(struct csi_priv *priv)
678 {
679         struct v4l2_mbus_framefmt *infmt, *outfmt;
680         const struct imx_media_pixfmt *incc;
681         struct v4l2_mbus_config mbus_cfg;
682         struct v4l2_mbus_framefmt if_fmt;
683         struct v4l2_rect crop;
684
685         infmt = &priv->format_mbus[CSI_SINK_PAD];
686         incc = priv->cc[CSI_SINK_PAD];
687         outfmt = &priv->format_mbus[priv->active_output_pad];
688
689         /* compose mbus_config from the upstream endpoint */
690         mbus_cfg.type = priv->upstream_ep.bus_type;
691         mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ?
692                 priv->upstream_ep.bus.parallel.flags :
693                 priv->upstream_ep.bus.mipi_csi2.flags;
694
695         /*
696          * we need to pass input frame to CSI interface, but
697          * with translated field type from output format
698          */
699         if_fmt = *infmt;
700         if_fmt.field = outfmt->field;
701         crop = priv->crop;
702
703         /*
704          * if cycles is set, we need to handle this over multiple cycles as
705          * generic/bayer data
706          */
707         if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) {
708                 if_fmt.width *= incc->cycles;
709                 crop.width *= incc->cycles;
710         }
711
712         ipu_csi_set_window(priv->csi, &crop);
713
714         ipu_csi_set_downsize(priv->csi,
715                              priv->crop.width == 2 * priv->compose.width,
716                              priv->crop.height == 2 * priv->compose.height);
717
718         ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt);
719
720         ipu_csi_set_dest(priv->csi, priv->dest);
721
722         if (priv->dest == IPU_CSI_DEST_IDMAC)
723                 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
724                                       priv->skip->max_ratio - 1, 0);
725
726         ipu_csi_dump(priv->csi);
727
728         return 0;
729 }
730
731 static int csi_start(struct csi_priv *priv)
732 {
733         struct v4l2_fract *input_fi, *output_fi;
734         int ret;
735
736         input_fi = &priv->frame_interval[CSI_SINK_PAD];
737         output_fi = &priv->frame_interval[priv->active_output_pad];
738
739         /* start upstream */
740         ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
741         ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
742         if (ret)
743                 return ret;
744
745         /* Skip first few frames from a BT.656 source */
746         if (priv->upstream_ep.bus_type == V4L2_MBUS_BT656) {
747                 u32 delay_usec, bad_frames = 20;
748
749                 delay_usec = DIV_ROUND_UP_ULL((u64)USEC_PER_SEC *
750                         input_fi->numerator * bad_frames,
751                         input_fi->denominator);
752
753                 usleep_range(delay_usec, delay_usec + 1000);
754         }
755
756         if (priv->dest == IPU_CSI_DEST_IDMAC) {
757                 ret = csi_idmac_start(priv);
758                 if (ret)
759                         goto stop_upstream;
760         }
761
762         ret = csi_setup(priv);
763         if (ret)
764                 goto idmac_stop;
765
766         /* start the frame interval monitor */
767         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
768                 ret = imx_media_fim_set_stream(priv->fim, output_fi, true);
769                 if (ret)
770                         goto idmac_stop;
771         }
772
773         ret = ipu_csi_enable(priv->csi);
774         if (ret) {
775                 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
776                 goto fim_off;
777         }
778
779         return 0;
780
781 fim_off:
782         if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
783                 imx_media_fim_set_stream(priv->fim, NULL, false);
784 idmac_stop:
785         if (priv->dest == IPU_CSI_DEST_IDMAC)
786                 csi_idmac_stop(priv);
787 stop_upstream:
788         v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
789         return ret;
790 }
791
792 static void csi_stop(struct csi_priv *priv)
793 {
794         if (priv->dest == IPU_CSI_DEST_IDMAC)
795                 csi_idmac_wait_last_eof(priv);
796
797         /*
798          * Disable the CSI asap, after syncing with the last EOF.
799          * Doing so after the IDMA channel is disabled has shown to
800          * create hard system-wide hangs.
801          */
802         ipu_csi_disable(priv->csi);
803
804         /* stop upstream */
805         v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
806
807         if (priv->dest == IPU_CSI_DEST_IDMAC) {
808                 csi_idmac_stop(priv);
809
810                 /* stop the frame interval monitor */
811                 if (priv->fim)
812                         imx_media_fim_set_stream(priv->fim, NULL, false);
813         }
814 }
815
816 static const struct csi_skip_desc csi_skip[12] = {
817         { 1, 1, 0x00 }, /* Keep all frames */
818         { 5, 6, 0x10 }, /* Skip every sixth frame */
819         { 4, 5, 0x08 }, /* Skip every fifth frame */
820         { 3, 4, 0x04 }, /* Skip every fourth frame */
821         { 2, 3, 0x02 }, /* Skip every third frame */
822         { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */
823         { 1, 2, 0x01 }, /* Skip every second frame */
824         { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */
825         { 1, 3, 0x03 }, /* Keep one in three frames */
826         { 1, 4, 0x07 }, /* Keep one in four frames */
827         { 1, 5, 0x0f }, /* Keep one in five frames */
828         { 1, 6, 0x1f }, /* Keep one in six frames */
829 };
830
831 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
832                                     struct v4l2_fract *interval)
833 {
834         unsigned int div;
835
836         interval->numerator *= skip->max_ratio;
837         interval->denominator *= skip->keep;
838
839         /* Reduce fraction to lowest terms */
840         div = gcd(interval->numerator, interval->denominator);
841         if (div > 1) {
842                 interval->numerator /= div;
843                 interval->denominator /= div;
844         }
845 }
846
847 /*
848  * Find the skip pattern to produce the output frame interval closest to the
849  * requested one, for the given input frame interval. Updates the output frame
850  * interval to the exact value.
851  */
852 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
853                                                       struct v4l2_fract *out)
854 {
855         const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
856         u32 min_err = UINT_MAX;
857         u64 want_us;
858         int i;
859
860         /* Default to 1:1 ratio */
861         if (out->numerator == 0 || out->denominator == 0 ||
862             in->numerator == 0 || in->denominator == 0) {
863                 *out = *in;
864                 return best_skip;
865         }
866
867         want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
868
869         /* Find the reduction closest to the requested time per frame */
870         for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
871                 u64 tmp, err;
872
873                 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
874                               skip->max_ratio, in->denominator * skip->keep);
875
876                 err = abs((s64)tmp - want_us);
877                 if (err < min_err) {
878                         min_err = err;
879                         best_skip = skip;
880                 }
881         }
882
883         *out = *in;
884         csi_apply_skip_interval(best_skip, out);
885
886         return best_skip;
887 }
888
889 /*
890  * V4L2 subdev operations.
891  */
892
893 static int csi_g_frame_interval(struct v4l2_subdev *sd,
894                                 struct v4l2_subdev_frame_interval *fi)
895 {
896         struct csi_priv *priv = v4l2_get_subdevdata(sd);
897
898         if (fi->pad >= CSI_NUM_PADS)
899                 return -EINVAL;
900
901         mutex_lock(&priv->lock);
902
903         fi->interval = priv->frame_interval[fi->pad];
904
905         mutex_unlock(&priv->lock);
906
907         return 0;
908 }
909
910 static int csi_s_frame_interval(struct v4l2_subdev *sd,
911                                 struct v4l2_subdev_frame_interval *fi)
912 {
913         struct csi_priv *priv = v4l2_get_subdevdata(sd);
914         struct v4l2_fract *input_fi;
915         int ret = 0;
916
917         mutex_lock(&priv->lock);
918
919         input_fi = &priv->frame_interval[CSI_SINK_PAD];
920
921         switch (fi->pad) {
922         case CSI_SINK_PAD:
923                 /* No limits on input frame interval */
924                 /* Reset output intervals and frame skipping ratio to 1:1 */
925                 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
926                 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
927                 priv->skip = &csi_skip[0];
928                 break;
929         case CSI_SRC_PAD_IDMAC:
930                 /*
931                  * frame interval at IDMAC output pad depends on input
932                  * interval, modified by frame skipping.
933                  */
934                 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
935                 break;
936         case CSI_SRC_PAD_DIRECT:
937                 /*
938                  * frame interval at DIRECT output pad is same as input
939                  * interval.
940                  */
941                 fi->interval = *input_fi;
942                 break;
943         default:
944                 ret = -EINVAL;
945                 goto out;
946         }
947
948         priv->frame_interval[fi->pad] = fi->interval;
949 out:
950         mutex_unlock(&priv->lock);
951         return ret;
952 }
953
954 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
955 {
956         struct csi_priv *priv = v4l2_get_subdevdata(sd);
957         int ret = 0;
958
959         mutex_lock(&priv->lock);
960
961         if (!priv->src_sd || !priv->sink) {
962                 ret = -EPIPE;
963                 goto out;
964         }
965
966         /*
967          * enable/disable streaming only if stream_count is
968          * going from 0 to 1 / 1 to 0.
969          */
970         if (priv->stream_count != !enable)
971                 goto update_count;
972
973         if (enable) {
974                 dev_dbg(priv->dev, "stream ON\n");
975                 ret = csi_start(priv);
976                 if (ret)
977                         goto out;
978         } else {
979                 dev_dbg(priv->dev, "stream OFF\n");
980                 csi_stop(priv);
981         }
982
983 update_count:
984         priv->stream_count += enable ? 1 : -1;
985         if (priv->stream_count < 0)
986                 priv->stream_count = 0;
987 out:
988         mutex_unlock(&priv->lock);
989         return ret;
990 }
991
992 static int csi_link_setup(struct media_entity *entity,
993                           const struct media_pad *local,
994                           const struct media_pad *remote, u32 flags)
995 {
996         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
997         struct csi_priv *priv = v4l2_get_subdevdata(sd);
998         struct v4l2_subdev *remote_sd;
999         int ret = 0;
1000
1001         dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
1002                 local->entity->name);
1003
1004         mutex_lock(&priv->lock);
1005
1006         if (local->flags & MEDIA_PAD_FL_SINK) {
1007                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1008                         ret = -EINVAL;
1009                         goto out;
1010                 }
1011
1012                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1013
1014                 if (flags & MEDIA_LNK_FL_ENABLED) {
1015                         if (priv->src_sd) {
1016                                 ret = -EBUSY;
1017                                 goto out;
1018                         }
1019                         priv->src_sd = remote_sd;
1020                 } else {
1021                         priv->src_sd = NULL;
1022                 }
1023
1024                 goto out;
1025         }
1026
1027         /* this is a source pad */
1028
1029         if (flags & MEDIA_LNK_FL_ENABLED) {
1030                 if (priv->sink) {
1031                         ret = -EBUSY;
1032                         goto out;
1033                 }
1034         } else {
1035                 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1036                 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1037                 priv->sink = NULL;
1038                 goto out;
1039         }
1040
1041         /* record which output pad is now active */
1042         priv->active_output_pad = local->index;
1043
1044         /* set CSI destination */
1045         if (local->index == CSI_SRC_PAD_IDMAC) {
1046                 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1047                         ret = -EINVAL;
1048                         goto out;
1049                 }
1050
1051                 if (priv->fim) {
1052                         ret = imx_media_fim_add_controls(priv->fim);
1053                         if (ret)
1054                                 goto out;
1055                 }
1056
1057                 priv->dest = IPU_CSI_DEST_IDMAC;
1058         } else {
1059                 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1060                         ret = -EINVAL;
1061                         goto out;
1062                 }
1063
1064                 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1065                 switch (remote_sd->grp_id) {
1066                 case IMX_MEDIA_GRP_ID_VDIC:
1067                         priv->dest = IPU_CSI_DEST_VDIC;
1068                         break;
1069                 case IMX_MEDIA_GRP_ID_IC_PRP:
1070                         priv->dest = IPU_CSI_DEST_IC;
1071                         break;
1072                 default:
1073                         ret = -EINVAL;
1074                         goto out;
1075                 }
1076         }
1077
1078         priv->sink = remote->entity;
1079 out:
1080         mutex_unlock(&priv->lock);
1081         return ret;
1082 }
1083
1084 static int csi_link_validate(struct v4l2_subdev *sd,
1085                              struct media_link *link,
1086                              struct v4l2_subdev_format *source_fmt,
1087                              struct v4l2_subdev_format *sink_fmt)
1088 {
1089         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1090         struct v4l2_fwnode_endpoint upstream_ep;
1091         bool is_csi2;
1092         int ret;
1093
1094         ret = v4l2_subdev_link_validate_default(sd, link,
1095                                                 source_fmt, sink_fmt);
1096         if (ret)
1097                 return ret;
1098
1099         ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1100         if (ret) {
1101                 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1102                 return ret;
1103         }
1104
1105         mutex_lock(&priv->lock);
1106
1107         priv->upstream_ep = upstream_ep;
1108         is_csi2 = !is_parallel_bus(&upstream_ep);
1109         if (is_csi2) {
1110                 int vc_num = 0;
1111                 /*
1112                  * NOTE! It seems the virtual channels from the mipi csi-2
1113                  * receiver are used only for routing by the video mux's,
1114                  * or for hard-wired routing to the CSI's. Once the stream
1115                  * enters the CSI's however, they are treated internally
1116                  * in the IPU as virtual channel 0.
1117                  */
1118 #if 0
1119                 mutex_unlock(&priv->lock);
1120                 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
1121                                                           &priv->sd.entity);
1122                 if (vc_num < 0)
1123                         return vc_num;
1124                 mutex_lock(&priv->lock);
1125 #endif
1126                 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
1127                                           &priv->format_mbus[CSI_SINK_PAD]);
1128         }
1129
1130         /* select either parallel or MIPI-CSI2 as input to CSI */
1131         ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1132
1133         mutex_unlock(&priv->lock);
1134         return ret;
1135 }
1136
1137 static struct v4l2_mbus_framefmt *
1138 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1139               unsigned int pad, enum v4l2_subdev_format_whence which)
1140 {
1141         if (which == V4L2_SUBDEV_FORMAT_TRY)
1142                 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
1143         else
1144                 return &priv->format_mbus[pad];
1145 }
1146
1147 static struct v4l2_rect *
1148 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1149                enum v4l2_subdev_format_whence which)
1150 {
1151         if (which == V4L2_SUBDEV_FORMAT_TRY)
1152                 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
1153         else
1154                 return &priv->crop;
1155 }
1156
1157 static struct v4l2_rect *
1158 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1159                   enum v4l2_subdev_format_whence which)
1160 {
1161         if (which == V4L2_SUBDEV_FORMAT_TRY)
1162                 return v4l2_subdev_get_try_compose(&priv->sd, cfg,
1163                                                    CSI_SINK_PAD);
1164         else
1165                 return &priv->compose;
1166 }
1167
1168 static void csi_try_crop(struct csi_priv *priv,
1169                          struct v4l2_rect *crop,
1170                          struct v4l2_subdev_pad_config *cfg,
1171                          struct v4l2_mbus_framefmt *infmt,
1172                          struct v4l2_fwnode_endpoint *upstream_ep)
1173 {
1174         crop->width = min_t(__u32, infmt->width, crop->width);
1175         if (crop->left + crop->width > infmt->width)
1176                 crop->left = infmt->width - crop->width;
1177         /* adjust crop left/width to h/w alignment restrictions */
1178         crop->left &= ~0x3;
1179         crop->width &= ~0x7;
1180
1181         /*
1182          * FIXME: not sure why yet, but on interlaced bt.656,
1183          * changing the vertical cropping causes loss of vertical
1184          * sync, so fix it to NTSC/PAL active lines. NTSC contains
1185          * 2 extra lines of active video that need to be cropped.
1186          */
1187         if (upstream_ep->bus_type == V4L2_MBUS_BT656 &&
1188             (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1189              infmt->field == V4L2_FIELD_ALTERNATE)) {
1190                 crop->height = infmt->height;
1191                 crop->top = (infmt->height == 480) ? 2 : 0;
1192         } else {
1193                 crop->height = min_t(__u32, infmt->height, crop->height);
1194                 if (crop->top + crop->height > infmt->height)
1195                         crop->top = infmt->height - crop->height;
1196         }
1197 }
1198
1199 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1200                               struct v4l2_subdev_pad_config *cfg,
1201                               struct v4l2_subdev_mbus_code_enum *code)
1202 {
1203         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1204         struct v4l2_fwnode_endpoint upstream_ep;
1205         const struct imx_media_pixfmt *incc;
1206         struct v4l2_mbus_framefmt *infmt;
1207         int ret = 0;
1208
1209         mutex_lock(&priv->lock);
1210
1211         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
1212         incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
1213
1214         switch (code->pad) {
1215         case CSI_SINK_PAD:
1216                 ret = imx_media_enum_mbus_format(&code->code, code->index,
1217                                                  CS_SEL_ANY, true);
1218                 break;
1219         case CSI_SRC_PAD_DIRECT:
1220         case CSI_SRC_PAD_IDMAC:
1221                 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1222                 if (ret) {
1223                         v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1224                         goto out;
1225                 }
1226
1227                 if (requires_passthrough(&upstream_ep, infmt, incc)) {
1228                         if (code->index != 0) {
1229                                 ret = -EINVAL;
1230                                 goto out;
1231                         }
1232                         code->code = infmt->code;
1233                 } else {
1234                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1235                                 CS_SEL_YUV : CS_SEL_RGB;
1236                         ret = imx_media_enum_ipu_format(&code->code,
1237                                                         code->index,
1238                                                         cs_sel);
1239                 }
1240                 break;
1241         default:
1242                 ret = -EINVAL;
1243         }
1244
1245 out:
1246         mutex_unlock(&priv->lock);
1247         return ret;
1248 }
1249
1250 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1251                                struct v4l2_subdev_pad_config *cfg,
1252                                struct v4l2_subdev_frame_size_enum *fse)
1253 {
1254         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1255         struct v4l2_rect *crop;
1256         int ret = 0;
1257
1258         if (fse->pad >= CSI_NUM_PADS ||
1259             fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1260                 return -EINVAL;
1261
1262         mutex_lock(&priv->lock);
1263
1264         if (fse->pad == CSI_SINK_PAD) {
1265                 fse->min_width = MIN_W;
1266                 fse->max_width = MAX_W;
1267                 fse->min_height = MIN_H;
1268                 fse->max_height = MAX_H;
1269         } else {
1270                 crop = __csi_get_crop(priv, cfg, fse->which);
1271
1272                 fse->min_width = fse->index & 1 ?
1273                         crop->width / 2 : crop->width;
1274                 fse->max_width = fse->min_width;
1275                 fse->min_height = fse->index & 2 ?
1276                         crop->height / 2 : crop->height;
1277                 fse->max_height = fse->min_height;
1278         }
1279
1280         mutex_unlock(&priv->lock);
1281         return ret;
1282 }
1283
1284 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1285                                    struct v4l2_subdev_pad_config *cfg,
1286                                    struct v4l2_subdev_frame_interval_enum *fie)
1287 {
1288         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1289         struct v4l2_fract *input_fi;
1290         struct v4l2_rect *crop;
1291         int ret = 0;
1292
1293         if (fie->pad >= CSI_NUM_PADS ||
1294             fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1295                            1 : ARRAY_SIZE(csi_skip)))
1296                 return -EINVAL;
1297
1298         mutex_lock(&priv->lock);
1299
1300         input_fi = &priv->frame_interval[CSI_SINK_PAD];
1301         crop = __csi_get_crop(priv, cfg, fie->which);
1302
1303         if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1304             (fie->height != crop->height && fie->height != crop->height / 2)) {
1305                 ret = -EINVAL;
1306                 goto out;
1307         }
1308
1309         fie->interval = *input_fi;
1310
1311         if (fie->pad == CSI_SRC_PAD_IDMAC)
1312                 csi_apply_skip_interval(&csi_skip[fie->index],
1313                                         &fie->interval);
1314
1315 out:
1316         mutex_unlock(&priv->lock);
1317         return ret;
1318 }
1319
1320 static int csi_get_fmt(struct v4l2_subdev *sd,
1321                        struct v4l2_subdev_pad_config *cfg,
1322                        struct v4l2_subdev_format *sdformat)
1323 {
1324         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1325         struct v4l2_mbus_framefmt *fmt;
1326         int ret = 0;
1327
1328         if (sdformat->pad >= CSI_NUM_PADS)
1329                 return -EINVAL;
1330
1331         mutex_lock(&priv->lock);
1332
1333         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1334         if (!fmt) {
1335                 ret = -EINVAL;
1336                 goto out;
1337         }
1338
1339         sdformat->format = *fmt;
1340 out:
1341         mutex_unlock(&priv->lock);
1342         return ret;
1343 }
1344
1345 static void csi_try_fmt(struct csi_priv *priv,
1346                         struct v4l2_fwnode_endpoint *upstream_ep,
1347                         struct v4l2_subdev_pad_config *cfg,
1348                         struct v4l2_subdev_format *sdformat,
1349                         struct v4l2_rect *crop,
1350                         struct v4l2_rect *compose,
1351                         const struct imx_media_pixfmt **cc)
1352 {
1353         const struct imx_media_pixfmt *incc;
1354         struct v4l2_mbus_framefmt *infmt;
1355         u32 code;
1356
1357         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
1358
1359         switch (sdformat->pad) {
1360         case CSI_SRC_PAD_DIRECT:
1361         case CSI_SRC_PAD_IDMAC:
1362                 incc = imx_media_find_mbus_format(infmt->code,
1363                                                   CS_SEL_ANY, true);
1364
1365                 sdformat->format.width = compose->width;
1366                 sdformat->format.height = compose->height;
1367
1368                 if (requires_passthrough(upstream_ep, infmt, incc)) {
1369                         sdformat->format.code = infmt->code;
1370                         *cc = incc;
1371                 } else {
1372                         u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1373                                 CS_SEL_YUV : CS_SEL_RGB;
1374
1375                         *cc = imx_media_find_ipu_format(sdformat->format.code,
1376                                                         cs_sel);
1377                         if (!*cc) {
1378                                 imx_media_enum_ipu_format(&code, 0, cs_sel);
1379                                 *cc = imx_media_find_ipu_format(code, cs_sel);
1380                                 sdformat->format.code = (*cc)->codes[0];
1381                         }
1382                 }
1383
1384                 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1385                     sdformat->format.field != V4L2_FIELD_NONE)
1386                         sdformat->format.field = infmt->field;
1387
1388                 /*
1389                  * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1390                  * depending on input height (assume NTSC top-bottom
1391                  * order if 480 lines, otherwise PAL bottom-top order).
1392                  */
1393                 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1394                         sdformat->format.field =  (infmt->height == 480) ?
1395                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1396                 }
1397
1398                 /* propagate colorimetry from sink */
1399                 sdformat->format.colorspace = infmt->colorspace;
1400                 sdformat->format.xfer_func = infmt->xfer_func;
1401                 sdformat->format.quantization = infmt->quantization;
1402                 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1403                 break;
1404         case CSI_SINK_PAD:
1405                 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1406                                       W_ALIGN, &sdformat->format.height,
1407                                       MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1408
1409                 /* Reset crop and compose rectangles */
1410                 crop->left = 0;
1411                 crop->top = 0;
1412                 crop->width = sdformat->format.width;
1413                 crop->height = sdformat->format.height;
1414                 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep);
1415                 compose->left = 0;
1416                 compose->top = 0;
1417                 compose->width = crop->width;
1418                 compose->height = crop->height;
1419
1420                 *cc = imx_media_find_mbus_format(sdformat->format.code,
1421                                                  CS_SEL_ANY, true);
1422                 if (!*cc) {
1423                         imx_media_enum_mbus_format(&code, 0,
1424                                                    CS_SEL_ANY, false);
1425                         *cc = imx_media_find_mbus_format(code,
1426                                                         CS_SEL_ANY, false);
1427                         sdformat->format.code = (*cc)->codes[0];
1428                 }
1429
1430                 imx_media_fill_default_mbus_fields(
1431                         &sdformat->format, infmt,
1432                         priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1433                 break;
1434         }
1435 }
1436
1437 static int csi_set_fmt(struct v4l2_subdev *sd,
1438                        struct v4l2_subdev_pad_config *cfg,
1439                        struct v4l2_subdev_format *sdformat)
1440 {
1441         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1442         struct imx_media_video_dev *vdev = priv->vdev;
1443         struct v4l2_fwnode_endpoint upstream_ep;
1444         const struct imx_media_pixfmt *cc;
1445         struct v4l2_pix_format vdev_fmt;
1446         struct v4l2_mbus_framefmt *fmt;
1447         struct v4l2_rect *crop, *compose;
1448         int ret;
1449
1450         if (sdformat->pad >= CSI_NUM_PADS)
1451                 return -EINVAL;
1452
1453         ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1454         if (ret) {
1455                 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1456                 return ret;
1457         }
1458
1459         mutex_lock(&priv->lock);
1460
1461         if (priv->stream_count > 0) {
1462                 ret = -EBUSY;
1463                 goto out;
1464         }
1465
1466         crop = __csi_get_crop(priv, cfg, sdformat->which);
1467         compose = __csi_get_compose(priv, cfg, sdformat->which);
1468
1469         csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc);
1470
1471         fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1472         *fmt = sdformat->format;
1473
1474         if (sdformat->pad == CSI_SINK_PAD) {
1475                 int pad;
1476
1477                 /* propagate format to source pads */
1478                 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1479                         const struct imx_media_pixfmt *outcc;
1480                         struct v4l2_mbus_framefmt *outfmt;
1481                         struct v4l2_subdev_format format;
1482
1483                         format.pad = pad;
1484                         format.which = sdformat->which;
1485                         format.format = sdformat->format;
1486                         csi_try_fmt(priv, &upstream_ep, cfg, &format,
1487                                     NULL, compose, &outcc);
1488
1489                         outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1490                         *outfmt = format.format;
1491
1492                         if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1493                                 priv->cc[pad] = outcc;
1494                 }
1495         }
1496
1497         if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1498                 goto out;
1499
1500         priv->cc[sdformat->pad] = cc;
1501
1502         /* propagate IDMAC output pad format to capture device */
1503         imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1504                                       &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1505                                       priv->cc[CSI_SRC_PAD_IDMAC]);
1506         mutex_unlock(&priv->lock);
1507         imx_media_capture_device_set_format(vdev, &vdev_fmt);
1508
1509         return 0;
1510 out:
1511         mutex_unlock(&priv->lock);
1512         return ret;
1513 }
1514
1515 static int csi_get_selection(struct v4l2_subdev *sd,
1516                              struct v4l2_subdev_pad_config *cfg,
1517                              struct v4l2_subdev_selection *sel)
1518 {
1519         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1520         struct v4l2_mbus_framefmt *infmt;
1521         struct v4l2_rect *crop, *compose;
1522         int ret = 0;
1523
1524         if (sel->pad != CSI_SINK_PAD)
1525                 return -EINVAL;
1526
1527         mutex_lock(&priv->lock);
1528
1529         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1530         crop = __csi_get_crop(priv, cfg, sel->which);
1531         compose = __csi_get_compose(priv, cfg, sel->which);
1532
1533         switch (sel->target) {
1534         case V4L2_SEL_TGT_CROP_BOUNDS:
1535                 sel->r.left = 0;
1536                 sel->r.top = 0;
1537                 sel->r.width = infmt->width;
1538                 sel->r.height = infmt->height;
1539                 break;
1540         case V4L2_SEL_TGT_CROP:
1541                 sel->r = *crop;
1542                 break;
1543         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1544                 sel->r.left = 0;
1545                 sel->r.top = 0;
1546                 sel->r.width = crop->width;
1547                 sel->r.height = crop->height;
1548                 break;
1549         case V4L2_SEL_TGT_COMPOSE:
1550                 sel->r = *compose;
1551                 break;
1552         default:
1553                 ret = -EINVAL;
1554         }
1555
1556         mutex_unlock(&priv->lock);
1557         return ret;
1558 }
1559
1560 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1561 {
1562         if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1563                      (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1564             *compose != crop && *compose != crop / 2)
1565                 return -ERANGE;
1566
1567         if (*compose <= crop / 2 ||
1568             (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1569             (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1570                 *compose = crop / 2;
1571         else
1572                 *compose = crop;
1573
1574         return 0;
1575 }
1576
1577 static int csi_set_selection(struct v4l2_subdev *sd,
1578                              struct v4l2_subdev_pad_config *cfg,
1579                              struct v4l2_subdev_selection *sel)
1580 {
1581         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1582         struct v4l2_fwnode_endpoint upstream_ep;
1583         struct v4l2_mbus_framefmt *infmt;
1584         struct v4l2_rect *crop, *compose;
1585         int pad, ret;
1586
1587         if (sel->pad != CSI_SINK_PAD)
1588                 return -EINVAL;
1589
1590         ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1591         if (ret) {
1592                 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1593                 return ret;
1594         }
1595
1596         mutex_lock(&priv->lock);
1597
1598         if (priv->stream_count > 0) {
1599                 ret = -EBUSY;
1600                 goto out;
1601         }
1602
1603         infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1604         crop = __csi_get_crop(priv, cfg, sel->which);
1605         compose = __csi_get_compose(priv, cfg, sel->which);
1606
1607         switch (sel->target) {
1608         case V4L2_SEL_TGT_CROP:
1609                 /*
1610                  * Modifying the crop rectangle always changes the format on
1611                  * the source pads. If the KEEP_CONFIG flag is set, just return
1612                  * the current crop rectangle.
1613                  */
1614                 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1615                         sel->r = priv->crop;
1616                         if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1617                                 *crop = sel->r;
1618                         goto out;
1619                 }
1620
1621                 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep);
1622
1623                 *crop = sel->r;
1624
1625                 /* Reset scaling to 1:1 */
1626                 compose->width = crop->width;
1627                 compose->height = crop->height;
1628                 break;
1629         case V4L2_SEL_TGT_COMPOSE:
1630                 /*
1631                  * Modifying the compose rectangle always changes the format on
1632                  * the source pads. If the KEEP_CONFIG flag is set, just return
1633                  * the current compose rectangle.
1634                  */
1635                 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1636                         sel->r = priv->compose;
1637                         if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1638                                 *compose = sel->r;
1639                         goto out;
1640                 }
1641
1642                 sel->r.left = 0;
1643                 sel->r.top = 0;
1644                 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1645                 if (ret)
1646                         goto out;
1647                 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1648                 if (ret)
1649                         goto out;
1650
1651                 *compose = sel->r;
1652                 break;
1653         default:
1654                 ret = -EINVAL;
1655                 goto out;
1656         }
1657
1658         /* Reset source pads to sink compose rectangle */
1659         for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1660                 struct v4l2_mbus_framefmt *outfmt;
1661
1662                 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1663                 outfmt->width = compose->width;
1664                 outfmt->height = compose->height;
1665         }
1666
1667 out:
1668         mutex_unlock(&priv->lock);
1669         return ret;
1670 }
1671
1672 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1673                                struct v4l2_event_subscription *sub)
1674 {
1675         if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1676                 return -EINVAL;
1677         if (sub->id != 0)
1678                 return -EINVAL;
1679
1680         return v4l2_event_subscribe(fh, sub, 0, NULL);
1681 }
1682
1683 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1684                                  struct v4l2_event_subscription *sub)
1685 {
1686         return v4l2_event_unsubscribe(fh, sub);
1687 }
1688
1689 /*
1690  * retrieve our pads parsed from the OF graph by the media device
1691  */
1692 static int csi_registered(struct v4l2_subdev *sd)
1693 {
1694         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1695         struct ipu_csi *csi;
1696         int i, ret;
1697         u32 code;
1698
1699         /* get media device */
1700         priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1701
1702         /* get handle to IPU CSI */
1703         csi = ipu_csi_get(priv->ipu, priv->csi_id);
1704         if (IS_ERR(csi)) {
1705                 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1706                 return PTR_ERR(csi);
1707         }
1708         priv->csi = csi;
1709
1710         for (i = 0; i < CSI_NUM_PADS; i++) {
1711                 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1712                         MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1713
1714                 code = 0;
1715                 if (i != CSI_SINK_PAD)
1716                         imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1717
1718                 /* set a default mbus format  */
1719                 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1720                                               640, 480, code, V4L2_FIELD_NONE,
1721                                               &priv->cc[i]);
1722                 if (ret)
1723                         goto put_csi;
1724
1725                 /* init default frame interval */
1726                 priv->frame_interval[i].numerator = 1;
1727                 priv->frame_interval[i].denominator = 30;
1728         }
1729
1730         /* disable frame skipping */
1731         priv->skip = &csi_skip[0];
1732
1733         /* init default crop and compose rectangle sizes */
1734         priv->crop.width = 640;
1735         priv->crop.height = 480;
1736         priv->compose.width = 640;
1737         priv->compose.height = 480;
1738
1739         priv->fim = imx_media_fim_init(&priv->sd);
1740         if (IS_ERR(priv->fim)) {
1741                 ret = PTR_ERR(priv->fim);
1742                 goto put_csi;
1743         }
1744
1745         ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1746         if (ret)
1747                 goto free_fim;
1748
1749         ret = imx_media_capture_device_register(priv->vdev);
1750         if (ret)
1751                 goto free_fim;
1752
1753         ret = imx_media_add_video_device(priv->md, priv->vdev);
1754         if (ret)
1755                 goto unreg;
1756
1757         return 0;
1758 unreg:
1759         imx_media_capture_device_unregister(priv->vdev);
1760 free_fim:
1761         if (priv->fim)
1762                 imx_media_fim_free(priv->fim);
1763 put_csi:
1764         ipu_csi_put(priv->csi);
1765         return ret;
1766 }
1767
1768 static void csi_unregistered(struct v4l2_subdev *sd)
1769 {
1770         struct csi_priv *priv = v4l2_get_subdevdata(sd);
1771
1772         imx_media_capture_device_unregister(priv->vdev);
1773
1774         if (priv->fim)
1775                 imx_media_fim_free(priv->fim);
1776
1777         if (priv->csi)
1778                 ipu_csi_put(priv->csi);
1779 }
1780
1781 static const struct media_entity_operations csi_entity_ops = {
1782         .link_setup = csi_link_setup,
1783         .link_validate = v4l2_subdev_link_validate,
1784 };
1785
1786 static const struct v4l2_subdev_core_ops csi_core_ops = {
1787         .subscribe_event = csi_subscribe_event,
1788         .unsubscribe_event = csi_unsubscribe_event,
1789 };
1790
1791 static const struct v4l2_subdev_video_ops csi_video_ops = {
1792         .g_frame_interval = csi_g_frame_interval,
1793         .s_frame_interval = csi_s_frame_interval,
1794         .s_stream = csi_s_stream,
1795 };
1796
1797 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1798         .init_cfg = imx_media_init_cfg,
1799         .enum_mbus_code = csi_enum_mbus_code,
1800         .enum_frame_size = csi_enum_frame_size,
1801         .enum_frame_interval = csi_enum_frame_interval,
1802         .get_fmt = csi_get_fmt,
1803         .set_fmt = csi_set_fmt,
1804         .get_selection = csi_get_selection,
1805         .set_selection = csi_set_selection,
1806         .link_validate = csi_link_validate,
1807 };
1808
1809 static const struct v4l2_subdev_ops csi_subdev_ops = {
1810         .core = &csi_core_ops,
1811         .video = &csi_video_ops,
1812         .pad = &csi_pad_ops,
1813 };
1814
1815 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1816         .registered = csi_registered,
1817         .unregistered = csi_unregistered,
1818 };
1819
1820 static int imx_csi_probe(struct platform_device *pdev)
1821 {
1822         struct ipu_client_platformdata *pdata;
1823         struct pinctrl *pinctrl;
1824         struct csi_priv *priv;
1825         int ret;
1826
1827         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1828         if (!priv)
1829                 return -ENOMEM;
1830
1831         platform_set_drvdata(pdev, &priv->sd);
1832         priv->dev = &pdev->dev;
1833
1834         ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1835         if (ret)
1836                 return ret;
1837
1838         /* get parent IPU */
1839         priv->ipu = dev_get_drvdata(priv->dev->parent);
1840
1841         /* get our CSI id */
1842         pdata = priv->dev->platform_data;
1843         priv->csi_id = pdata->csi;
1844         priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1845
1846         timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1847         spin_lock_init(&priv->irqlock);
1848
1849         v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1850         v4l2_set_subdevdata(&priv->sd, priv);
1851         priv->sd.internal_ops = &csi_internal_ops;
1852         priv->sd.entity.ops = &csi_entity_ops;
1853         priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1854         priv->sd.dev = &pdev->dev;
1855         priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1856         priv->sd.owner = THIS_MODULE;
1857         priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1858         priv->sd.grp_id = priv->csi_id ?
1859                 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1860         imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1861                                     priv->sd.grp_id, ipu_get_num(priv->ipu));
1862
1863         priv->vdev = imx_media_capture_device_init(&priv->sd,
1864                                                    CSI_SRC_PAD_IDMAC);
1865         if (IS_ERR(priv->vdev))
1866                 return PTR_ERR(priv->vdev);
1867
1868         mutex_init(&priv->lock);
1869
1870         v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1871         priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1872
1873         /*
1874          * The IPUv3 driver did not assign an of_node to this
1875          * device. As a result, pinctrl does not automatically
1876          * configure our pin groups, so we need to do that manually
1877          * here, after setting this device's of_node.
1878          */
1879         priv->dev->of_node = pdata->of_node;
1880         pinctrl = devm_pinctrl_get_select_default(priv->dev);
1881         if (IS_ERR(pinctrl)) {
1882                 ret = PTR_ERR(pinctrl);
1883                 dev_dbg(priv->dev,
1884                         "devm_pinctrl_get_select_default() failed: %d\n", ret);
1885                 if (ret != -ENODEV)
1886                         goto free;
1887         }
1888
1889         ret = v4l2_async_register_subdev(&priv->sd);
1890         if (ret)
1891                 goto free;
1892
1893         return 0;
1894 free:
1895         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1896         mutex_destroy(&priv->lock);
1897         imx_media_capture_device_remove(priv->vdev);
1898         return ret;
1899 }
1900
1901 static int imx_csi_remove(struct platform_device *pdev)
1902 {
1903         struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1904         struct csi_priv *priv = sd_to_dev(sd);
1905
1906         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1907         mutex_destroy(&priv->lock);
1908         imx_media_capture_device_remove(priv->vdev);
1909         v4l2_async_unregister_subdev(sd);
1910         media_entity_cleanup(&sd->entity);
1911
1912         return 0;
1913 }
1914
1915 static const struct platform_device_id imx_csi_ids[] = {
1916         { .name = "imx-ipuv3-csi" },
1917         { },
1918 };
1919 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1920
1921 static struct platform_driver imx_csi_driver = {
1922         .probe = imx_csi_probe,
1923         .remove = imx_csi_remove,
1924         .id_table = imx_csi_ids,
1925         .driver = {
1926                 .name = "imx-ipuv3-csi",
1927         },
1928 };
1929 module_platform_driver(imx_csi_driver);
1930
1931 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1932 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1933 MODULE_LICENSE("GPL");
1934 MODULE_ALIAS("platform:imx-ipuv3-csi");