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