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