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

source code of linux/drivers/staging/media/imx/imx-media-csi.c