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