File Index
Files and directories
Sign in
FILE

imx-media-csi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/staging/media/imx/imx-media-csi.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC
   4 *
   5 * Copyright (c) 2014-2017 Mentor Graphics Inc.
   6 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
   7 */
   8#include <linux/delay.h>
   9#include <linux/gcd.h>
  10#include <linux/interrupt.h>
  11#include <linux/module.h>
  12#include <linux/of_graph.h>
  13#include <linux/pinctrl/consumer.h>
  14#include <linux/platform_device.h>
  15#include <media/v4l2-ctrls.h>
  16#include <media/v4l2-device.h>
  17#include <media/v4l2-event.h>
  18#include <media/v4l2-fwnode.h>
  19#include <media/v4l2-mc.h>
  20#include <media/v4l2-subdev.h>
  21#include <media/videobuf2-dma-contig.h>
  22#include <video/imx-ipu-v3.h>
  23#include <media/imx.h>
  24#include "imx-media.h"
  25
  26/*
  27 * Min/Max supported width and heights.
  28 *
  29 * We allow planar output, so we have to align width by 16 pixels
  30 * to meet IDMAC alignment requirements.
  31 *
  32 * TODO: move this into pad format negotiation, if capture device
  33 * has not requested planar formats, we should allow 8 pixel
  34 * alignment.
  35 */
  36#define MIN_W       32
  37#define MIN_H       32
  38#define MAX_W      4096
  39#define MAX_H      4096
  40#define W_ALIGN    1 /* multiple of 2 pixels */
  41#define H_ALIGN    1 /* multiple of 2 lines */
  42#define S_ALIGN    1 /* multiple of 2 */
  43
  44/*
  45 * struct csi_skip_desc - CSI frame skipping descriptor
  46 * @keep - number of frames kept per max_ratio frames
  47 * @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield
  48 * @skip_smfc - skip pattern written to the SKIP_SMFC bitfield
  49 */
  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(&sd->entity,
 187					       IMX_MEDIA_GRP_ID_CSI2,
 188					       true);
 189		if (IS_ERR(sd))
 190			sd = priv->src_sd;
 191		break;
 192	case IMX_MEDIA_GRP_ID_CSI2:
 193		break;
 194	default:
 195		/*
 196		 * the source is neither the CSI mux nor the CSI-2 receiver,
 197		 * get the source pad directly upstream from CSI itself.
 198		 */
 199		sd = &priv->sd;
 200		break;
 201	}
 202
 203	/* get source pad of entity directly upstream from sd */
 204	remote_pad = media_entity_remote_pad_unique(&sd->entity,
 205						    MEDIA_PAD_FL_SOURCE);
 206	if (IS_ERR(remote_pad))
 207		return PTR_ERR(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(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(priv->ipu, ch_num);
 241	if (IS_ERR(smfc)) {
 242		v4l2_err(&priv->sd, "failed to get SMFC\n");
 243		ret = PTR_ERR(smfc);
 244		goto out;
 245	}
 246	priv->smfc = smfc;
 247
 248	idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
 249	if (IS_ERR(idmac_ch)) {
 250		v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
 251			 ch_num);
 252		ret = PTR_ERR(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, 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(&next->vbuf.vb2_buf, 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(priv->idmac_ch, priv->ipu_buf_num))
 294		ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
 295
 296	if (priv->interweave_swap)
 297		phys += vdev->fmt.bytesperline;
 298
 299	ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, 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(&priv->irqlock);
 307
 308	if (priv->last_eof) {
 309		complete(&priv->last_eof_comp);
 310		priv->last_eof = false;
 311		goto unlock;
 312	}
 313
 314	if (priv->fim)
 315		/* call frame interval monitor */
 316		imx_media_fim_eof_monitor(priv->fim, ktime_get());
 317
 318	csi_vb2_buf_done(priv);
 319
 320	/* select new IPU buf */
 321	ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
 322	/* toggle IPU double-buffer index */
 323	priv->ipu_buf_num ^= 1;
 324
 325	/* bump the EOF timeout timer */
 326	mod_timer(&priv->eof_timeout_timer,
 327		  jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
 328
 329unlock:
 330	spin_unlock(&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(&priv->irqlock);
 339
 340	/*
 341	 * this is not an unrecoverable error, just mark
 342	 * the next captured frame with vb2 error flag.
 343	 */
 344	priv->nfb4eof = true;
 345
 346	v4l2_err(&priv->sd, "NFB4EOF\n");
 347
 348	spin_unlock(&priv->irqlock);
 349
 350	return IRQ_HANDLED;
 351}
 352
 353/*
 354 * EOF timeout timer function. This is an unrecoverable condition
 355 * without a stream restart.
 356 */
 357static void csi_idmac_eof_timeout(struct timer_list *t)
 358{
 359	struct csi_priv *priv = timer_container_of(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				&buf->vbuf.vb2_buf, 0);
 380		} else {
 381			priv->active_vb2_buf[i] = NULL;
 382			phys[i] = priv->underrun_buf.phys;
 383		}
 384	}
 385}
 386
 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, 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(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(&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(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(priv->idmac_ch,
 508					 image.rect.width * passthrough_cycles,
 509					 image.rect.height);
 510		ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
 511		ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
 512		ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
 513		ipu_cpmem_set_format_passthrough(priv->idmac_ch,
 514						 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(priv->idmac_ch, &image);
 522		if (ret)
 523			goto unsetup_vb2;
 524	}
 525
 526	ipu_cpmem_set_burstsize(priv->idmac_ch, 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(priv->smfc, 0x02, 0x01);
 541	ipu_cpmem_set_high_priority(priv->idmac_ch);
 542	ipu_idmac_enable_watermark(priv->idmac_ch, true);
 543	ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
 544
 545	burst_size = passthrough ?
 546		(burst_size >> 3) - 1 : (burst_size >> 2) - 1;
 547
 548	ipu_smfc_set_burstsize(priv->smfc, burst_size);
 549
 550	if (interweave)
 551		ipu_cpmem_interlaced_scan(priv->idmac_ch,
 552					  priv->interweave_swap ?
 553					  -image.pix.bytesperline :
 554					  image.pix.bytesperline,
 555					  image.pix.pixelformat);
 556
 557	ipu_idmac_set_double_buffer(priv->idmac_ch, true);
 558
 559	return 0;
 560
 561unsetup_vb2:
 562	csi_idmac_unsetup_vb2_buf(priv, 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(priv->idmac_ch);
 570	ipu_smfc_disable(priv->smfc);
 571
 572	csi_idmac_unsetup_vb2_buf(priv, 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(priv->idmac_ch);
 584	ipu_dump(priv->ipu);
 585
 586	ipu_smfc_enable(priv->smfc);
 587
 588	/* set buffers ready */
 589	ipu_idmac_select_buffer(priv->idmac_ch, 0);
 590	ipu_idmac_select_buffer(priv->idmac_ch, 1);
 591
 592	/* enable the channels */
 593	ipu_idmac_enable_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(priv->smfc, priv->csi_id, priv->vc_num);
 608
 609	ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf,
 610				      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(&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(priv->ipu,
 629						  priv->idmac_ch,
 630						  IPU_IRQ_NFB4EOF);
 631	ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
 632			       csi_idmac_nfb4eof_interrupt, 0,
 633			       "imx-smfc-nfb4eof", 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(priv->ipu, priv->idmac_ch,
 641					      IPU_IRQ_EOF);
 642
 643	ret = devm_request_irq(priv->dev, priv->eof_irq,
 644			       csi_idmac_eof_interrupt, 0,
 645			       "imx-smfc-eof", 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(&priv->eof_timeout_timer,
 654		  jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
 655
 656	return 0;
 657
 658out_free_nfb4eof_irq:
 659	devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
 660out_unsetup:
 661	csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
 662out_free_dma_buf:
 663	imx_media_free_dma_buf(priv->dev, &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(&priv->irqlock, flags);
 678
 679	/*
 680	 * and then wait for interrupt handler to mark completion.
 681	 */
 682	ret = wait_for_completion_timeout(
 683		&priv->last_eof_comp, 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(priv->dev, priv->eof_irq, priv);
 691	devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
 692
 693	csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
 694
 695	imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
 696
 697	/* cancel the EOF timeout timer */
 698	timer_delete_sync(&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(&priv->mbus_cfg) && incc->cycles) {
 723		if_fmt.width *= incc->cycles;
 724		crop.width *= incc->cycles;
 725	}
 726
 727	ipu_csi_set_window(priv->csi, &crop);
 728
 729	ipu_csi_set_downsize(priv->csi,
 730			     priv->crop.width == 2 * priv->compose.width,
 731			     priv->crop.height == 2 * priv->compose.height);
 732
 733	ipu_csi_init_interface(priv->csi, &priv->mbus_cfg, &if_fmt, outfmt);
 734
 735	ipu_csi_set_dest(priv->csi, priv->dest);
 736
 737	if (priv->dest == IPU_CSI_DEST_IDMAC)
 738		ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
 739				      priv->skip->max_ratio - 1, 0);
 740
 741	ipu_csi_dump(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(delay_usec, 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(priv->fim, output_fi, true);
 784
 785	ret = ipu_csi_enable(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(priv->fim, NULL, 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(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(priv->fim, NULL, 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(interval->numerator, 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((u64)USEC_PER_SEC * out->numerator, 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((u64)USEC_PER_SEC * in->numerator *
 886			      skip->max_ratio, 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(best_skip, 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(&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(input_fi, &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(&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(&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(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(&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(remote->entity)) {
1080			ret = -EINVAL;
1081			goto out;
1082		}
1083
1084		if (priv->fim) {
1085			ret = imx_media_fim_add_controls(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(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(&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);
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);
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(priv->csi, 0,
1152					  &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(priv->ipu, priv->csi_id, is_csi2);
1157
1158	mutex_unlock(&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, CSI_SINK_PAD, code->which);
1245	incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY);
1246
1247	switch (code->pad) {
1248	case CSI_SINK_PAD:
1249		ret = imx_media_enum_mbus_formats(&code->code, code->index,
1250						  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);
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, 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,
1273							 code->index,
1274							 fmt_sel);
1275		}
1276		break;
1277	default:
1278		ret = -EINVAL;
1279	}
1280
1281out:
1282	mutex_unlock(&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, 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(&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, 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(&csi_skip[fie->index],
1349					&fie->interval);
1350
1351out:
1352	mutex_unlock(&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, sdformat->pad, sdformat->which);
1370	if (!fmt) {
1371		ret = -EINVAL;
1372		goto out;
1373	}
1374
1375	sdformat->format = *fmt;
1376out:
1377	mutex_unlock(&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, CSI_SINK_PAD, 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, CSI_SINK_PAD, 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(infmt->code, 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(sdformat->format.code,
1461							fmt_sel);
1462			if (!*cc) {
1463				imx_media_enum_ipu_formats(&code, 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(&sdformat->format.width, MIN_W, MAX_W,
1480				      W_ALIGN, &sdformat->format.height,
1481				      MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1482
1483		*cc = imx_media_find_mbus_format(sdformat->format.code,
1484						 PIXFMT_SEL_ANY);
1485		if (!*cc) {
1486			imx_media_enum_mbus_formats(&code, 0,
1487						    PIXFMT_SEL_YUV_RGB);
1488			*cc = imx_media_find_mbus_format(code,
1489							 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, &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(&sdformat->format,
1512			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);
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, sdformat->which);
1544	compose = __csi_get_compose(priv, sd_state, sdformat->which);
1545
1546	csi_try_fmt(priv, &mbus_cfg, sd_state, sdformat, crop, compose, &cc);
1547
1548	fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, 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, sd_state, &format, NULL,
1564				    compose, &outcc);
1565
1566			outfmt = __csi_get_fmt(priv, sd_state, pad,
1567					       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(&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, CSI_SINK_PAD, sel->which);
1598	crop = __csi_get_crop(priv, sd_state, sel->which);
1599	compose = __csi_get_compose(priv, sd_state, 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(&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);
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, CSI_SINK_PAD, sel->which);
1675	crop = __csi_get_crop(priv, sd_state, sel->which);
1676	compose = __csi_get_compose(priv, sd_state, 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, &sel->r, sd_state, infmt, &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(&sel->r.width, crop->width, sel->flags);
1716		if (ret)
1717			goto out;
1718		ret = csi_set_scale(&sel->r.height, crop->height, 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, sel->which);
1734		outfmt->width = compose->width;
1735		outfmt->height = compose->height;
1736	}
1737
1738out:
1739	mutex_unlock(&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, 0, NULL);
1752}
1753
1754static int csi_registered(struct v4l2_subdev *sd)
1755{
1756	struct csi_priv *priv = v4l2_get_subdevdata(sd);
1757	struct ipu_csi *csi;
1758	int i, ret;
1759	u32 code;
1760
1761	/* get handle to IPU CSI */
1762	csi = ipu_csi_get(priv->ipu, priv->csi_id);
1763	if (IS_ERR(csi)) {
1764		v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1765		return PTR_ERR(csi);
1766	}
1767	priv->csi = csi;
1768
1769	for (i = 0; i < CSI_NUM_PADS; i++) {
1770		code = 0;
1771		if (i != CSI_SINK_PAD)
1772			imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV);
1773
1774		/* set a default mbus format  */
1775		ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1776					      IMX_MEDIA_DEF_PIX_WIDTH,
1777					      IMX_MEDIA_DEF_PIX_HEIGHT, code,
1778					      V4L2_FIELD_NONE, &priv->cc[i]);
1779		if (ret)
1780			goto put_csi;
1781
1782		/* init default frame interval */
1783		priv->frame_interval[i].numerator = 1;
1784		priv->frame_interval[i].denominator = 30;
1785	}
1786
1787	/* disable frame skipping */
1788	priv->skip = &csi_skip[0];
1789
1790	/* init default crop and compose rectangle sizes */
1791	priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH;
1792	priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1793	priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH;
1794	priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1795
1796	priv->fim = imx_media_fim_init(&priv->sd);
1797	if (IS_ERR(priv->fim)) {
1798		ret = PTR_ERR(priv->fim);
1799		goto put_csi;
1800	}
1801
1802	priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd,
1803						   CSI_SRC_PAD_IDMAC, true);
1804	if (IS_ERR(priv->vdev)) {
1805		ret = PTR_ERR(priv->vdev);
1806		goto free_fim;
1807	}
1808
1809	ret = imx_media_capture_device_register(priv->vdev, 0);
1810	if (ret)
1811		goto remove_vdev;
1812
1813	return 0;
1814
1815remove_vdev:
1816	imx_media_capture_device_remove(priv->vdev);
1817free_fim:
1818	if (priv->fim)
1819		imx_media_fim_free(priv->fim);
1820put_csi:
1821	ipu_csi_put(priv->csi);
1822	return ret;
1823}
1824
1825static void csi_unregistered(struct v4l2_subdev *sd)
1826{
1827	struct csi_priv *priv = v4l2_get_subdevdata(sd);
1828
1829	imx_media_capture_device_unregister(priv->vdev);
1830	imx_media_capture_device_remove(priv->vdev);
1831
1832	if (priv->fim)
1833		imx_media_fim_free(priv->fim);
1834
1835	if (priv->csi)
1836		ipu_csi_put(priv->csi);
1837}
1838
1839/*
1840 * The CSI has only one fwnode endpoint, at the sink pad. Verify the
1841 * endpoint belongs to us, and return CSI_SINK_PAD.
1842 */
1843static int csi_get_fwnode_pad(struct media_entity *entity,
1844			      struct fwnode_endpoint *endpoint)
1845{
1846	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1847	struct csi_priv *priv = v4l2_get_subdevdata(sd);
1848	struct fwnode_handle *csi_port = dev_fwnode(priv->dev);
1849	struct fwnode_handle *csi_ep;
1850	int ret;
1851
1852	csi_ep = fwnode_get_next_child_node(csi_port, NULL);
1853
1854	ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO;
1855
1856	fwnode_handle_put(csi_ep);
1857
1858	return ret;
1859}
1860
1861static const struct media_entity_operations csi_entity_ops = {
1862	.link_setup = csi_link_setup,
1863	.link_validate = v4l2_subdev_link_validate,
1864	.get_fwnode_pad = csi_get_fwnode_pad,
1865};
1866
1867static const struct v4l2_subdev_core_ops csi_core_ops = {
1868	.subscribe_event = csi_subscribe_event,
1869	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
1870};
1871
1872static const struct v4l2_subdev_video_ops csi_video_ops = {
1873	.s_stream = csi_s_stream,
1874};
1875
1876static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1877	.enum_mbus_code = csi_enum_mbus_code,
1878	.enum_frame_size = csi_enum_frame_size,
1879	.enum_frame_interval = csi_enum_frame_interval,
1880	.get_fmt = csi_get_fmt,
1881	.set_fmt = csi_set_fmt,
1882	.get_selection = csi_get_selection,
1883	.set_selection = csi_set_selection,
1884	.get_frame_interval = csi_get_frame_interval,
1885	.set_frame_interval = csi_set_frame_interval,
1886	.link_validate = csi_link_validate,
1887};
1888
1889static const struct v4l2_subdev_ops csi_subdev_ops = {
1890	.core = &csi_core_ops,
1891	.video = &csi_video_ops,
1892	.pad = &csi_pad_ops,
1893};
1894
1895static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1896	.init_state = imx_media_init_state,
1897	.registered = csi_registered,
1898	.unregistered = csi_unregistered,
1899};
1900
1901static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier,
1902				struct v4l2_subdev *sd,
1903				struct v4l2_async_connection *asd)
1904{
1905	struct csi_priv *priv = notifier_to_dev(notifier);
1906	struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD];
1907
1908	/*
1909	 * If the subdev is a video mux, it must be one of the CSI
1910	 * muxes. Mark it as such via its group id.
1911	 */
1912	if (sd->entity.function == MEDIA_ENT_F_VID_MUX)
1913		sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX;
1914
1915	return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
1916}
1917
1918static const struct v4l2_async_notifier_operations csi_notify_ops = {
1919	.bound = imx_csi_notify_bound,
1920};
1921
1922static int imx_csi_async_register(struct csi_priv *priv)
1923{
1924	struct v4l2_async_connection *asd = NULL;
1925	struct fwnode_handle *ep;
1926	unsigned int port;
1927	int ret;
1928
1929	v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
1930
1931	/* get this CSI's port id */
1932	ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port);
1933	if (ret < 0)
1934		return ret;
1935
1936	ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent),
1937					     port, 0,
1938					     FWNODE_GRAPH_ENDPOINT_NEXT);
1939	if (ep) {
1940		asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep,
1941						      struct v4l2_async_connection);
1942
1943		fwnode_handle_put(ep);
1944
1945		if (IS_ERR(asd)) {
1946			ret = PTR_ERR(asd);
1947			/* OK if asd already exists */
1948			if (ret != -EEXIST)
1949				return ret;
1950		}
1951	}
1952
1953	priv->notifier.ops = &csi_notify_ops;
1954
1955	ret = v4l2_async_nf_register(&priv->notifier);
1956	if (ret)
1957		return ret;
1958
1959	return v4l2_async_register_subdev(&priv->sd);
1960}
1961
1962static int imx_csi_probe(struct platform_device *pdev)
1963{
1964	struct ipu_client_platformdata *pdata;
1965	struct pinctrl *pinctrl;
1966	struct csi_priv *priv;
1967	int i, ret;
1968
1969	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1970	if (!priv)
1971		return -ENOMEM;
1972
1973	platform_set_drvdata(pdev, &priv->sd);
1974	priv->dev = &pdev->dev;
1975
1976	ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1977	if (ret)
1978		return ret;
1979
1980	/* get parent IPU */
1981	priv->ipu = dev_get_drvdata(priv->dev->parent);
1982
1983	/* get our CSI id */
1984	pdata = priv->dev->platform_data;
1985	priv->csi_id = pdata->csi;
1986	priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1987
1988	priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1989
1990	timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1991	spin_lock_init(&priv->irqlock);
1992
1993	v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1994	v4l2_set_subdevdata(&priv->sd, priv);
1995	priv->sd.internal_ops = &csi_internal_ops;
1996	priv->sd.entity.ops = &csi_entity_ops;
1997	priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1998	priv->sd.dev = &pdev->dev;
1999	priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
2000	priv->sd.owner = THIS_MODULE;
2001	priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2002	priv->sd.grp_id = priv->csi_id ?
2003		IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0;
2004	imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
2005				    priv->sd.grp_id, ipu_get_num(priv->ipu));
2006
2007	for (i = 0; i < CSI_NUM_PADS; i++)
2008		priv->pad[i].flags = (i == CSI_SINK_PAD) ?
2009			MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2010
2011	ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS,
2012				     priv->pad);
2013	if (ret)
2014		return ret;
2015
2016	mutex_init(&priv->lock);
2017
2018	v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
2019	priv->sd.ctrl_handler = &priv->ctrl_hdlr;
2020
2021	/*
2022	 * The IPUv3 driver did not assign an of_node to this
2023	 * device. As a result, pinctrl does not automatically
2024	 * configure our pin groups, so we need to do that manually
2025	 * here, after setting this device's of_node.
2026	 */
2027	priv->dev->of_node = pdata->of_node;
2028	pinctrl = devm_pinctrl_get_select_default(priv->dev);
2029	if (IS_ERR(pinctrl)) {
2030		ret = PTR_ERR(pinctrl);
2031		dev_dbg(priv->dev,
2032			"devm_pinctrl_get_select_default() failed: %d\n", ret);
2033		if (ret != -ENODEV)
2034			goto free;
2035	}
2036
2037	ret = imx_csi_async_register(priv);
2038	if (ret)
2039		goto cleanup;
2040
2041	return 0;
2042
2043cleanup:
2044	v4l2_async_nf_unregister(&priv->notifier);
2045	v4l2_async_nf_cleanup(&priv->notifier);
2046free:
2047	v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2048	mutex_destroy(&priv->lock);
2049	return ret;
2050}
2051
2052static void imx_csi_remove(struct platform_device *pdev)
2053{
2054	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
2055	struct csi_priv *priv = sd_to_dev(sd);
2056
2057	v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2058	mutex_destroy(&priv->lock);
2059	v4l2_async_nf_unregister(&priv->notifier);
2060	v4l2_async_nf_cleanup(&priv->notifier);
2061	v4l2_async_unregister_subdev(sd);
2062	media_entity_cleanup(&sd->entity);
2063}
2064
2065static const struct platform_device_id imx_csi_ids[] = {
2066	{ .name = "imx-ipuv3-csi" },
2067	{ },
2068};
2069MODULE_DEVICE_TABLE(platform, imx_csi_ids);
2070
2071static struct platform_driver imx_csi_driver = {
2072	.probe = imx_csi_probe,
2073	.remove = imx_csi_remove,
2074	.id_table = imx_csi_ids,
2075	.driver = {
2076		.name = "imx-ipuv3-csi",
2077	},
2078};
2079module_platform_driver(imx_csi_driver);
2080
2081MODULE_DESCRIPTION("i.MX CSI subdev driver");
2082MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
2083MODULE_LICENSE("GPL");
~/ Read-only file preview.