0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/delay.h>
0009 #include <linux/gcd.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/module.h>
0012 #include <linux/of_graph.h>
0013 #include <linux/pinctrl/consumer.h>
0014 #include <linux/platform_device.h>
0015 #include <media/v4l2-ctrls.h>
0016 #include <media/v4l2-device.h>
0017 #include <media/v4l2-event.h>
0018 #include <media/v4l2-fwnode.h>
0019 #include <media/v4l2-mc.h>
0020 #include <media/v4l2-subdev.h>
0021 #include <media/videobuf2-dma-contig.h>
0022 #include <video/imx-ipu-v3.h>
0023 #include <media/imx.h>
0024 #include "imx-media.h"
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #define MIN_W 32
0037 #define MIN_H 32
0038 #define MAX_W 4096
0039 #define MAX_H 4096
0040 #define W_ALIGN 1
0041 #define H_ALIGN 1
0042 #define S_ALIGN 1
0043
0044
0045
0046
0047
0048
0049
0050 struct csi_skip_desc {
0051 u8 keep;
0052 u8 max_ratio;
0053 u8 skip_smfc;
0054 };
0055
0056 struct csi_priv {
0057 struct device *dev;
0058 struct ipu_soc *ipu;
0059 struct v4l2_subdev sd;
0060 struct media_pad pad[CSI_NUM_PADS];
0061 struct v4l2_async_notifier notifier;
0062
0063
0064 struct imx_media_video_dev *vdev;
0065 struct imx_media_fim *fim;
0066 int csi_id;
0067 int smfc_id;
0068
0069
0070 struct mutex lock;
0071
0072 int active_output_pad;
0073
0074 struct ipuv3_channel *idmac_ch;
0075 struct ipu_smfc *smfc;
0076 struct ipu_csi *csi;
0077
0078 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
0079 const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
0080 struct v4l2_fract frame_interval[CSI_NUM_PADS];
0081 struct v4l2_rect crop;
0082 struct v4l2_rect compose;
0083 const struct csi_skip_desc *skip;
0084
0085
0086 struct imx_media_buffer *active_vb2_buf[2];
0087 struct imx_media_dma_buf underrun_buf;
0088
0089 int ipu_buf_num;
0090
0091
0092 struct media_entity *sink;
0093 enum ipu_csi_dest dest;
0094
0095 struct v4l2_subdev *src_sd;
0096
0097
0098 int vc_num;
0099
0100
0101 struct v4l2_fwnode_endpoint upstream_ep;
0102
0103 spinlock_t irqlock;
0104 struct timer_list eof_timeout_timer;
0105 int eof_irq;
0106 int nfb4eof_irq;
0107
0108 struct v4l2_ctrl_handler ctrl_hdlr;
0109
0110 int stream_count;
0111 u32 frame_sequence;
0112 bool last_eof;
0113 bool nfb4eof;
0114 bool interweave_swap;
0115 struct completion last_eof_comp;
0116 };
0117
0118 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
0119 {
0120 return container_of(sdev, struct csi_priv, sd);
0121 }
0122
0123 static inline struct csi_priv *notifier_to_dev(struct v4l2_async_notifier *n)
0124 {
0125 return container_of(n, struct csi_priv, notifier);
0126 }
0127
0128 static inline bool is_parallel_bus(struct v4l2_fwnode_endpoint *ep)
0129 {
0130 return ep->bus_type != V4L2_MBUS_CSI2_DPHY;
0131 }
0132
0133 static inline bool is_parallel_16bit_bus(struct v4l2_fwnode_endpoint *ep)
0134 {
0135 return is_parallel_bus(ep) && ep->bus.parallel.bus_width >= 16;
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 static inline bool requires_passthrough(struct v4l2_fwnode_endpoint *ep,
0149 struct v4l2_mbus_framefmt *infmt,
0150 const struct imx_media_pixfmt *incc)
0151 {
0152 if (ep->bus_type == V4L2_MBUS_BT656)
0153 return false;
0154
0155 return incc->bayer || is_parallel_16bit_bus(ep) ||
0156 (is_parallel_bus(ep) &&
0157 infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 &&
0158 infmt->code != MEDIA_BUS_FMT_YUYV8_2X8);
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 static int csi_get_upstream_endpoint(struct csi_priv *priv,
0170 struct v4l2_fwnode_endpoint *ep)
0171 {
0172 struct fwnode_handle *endpoint;
0173 struct v4l2_subdev *sd;
0174 struct media_pad *pad;
0175
0176 if (!IS_ENABLED(CONFIG_OF))
0177 return -ENXIO;
0178
0179 if (!priv->src_sd)
0180 return -EPIPE;
0181
0182 sd = priv->src_sd;
0183
0184 switch (sd->grp_id) {
0185 case IMX_MEDIA_GRP_ID_CSI_MUX:
0186
0187
0188
0189
0190
0191 sd = imx_media_pipeline_subdev(&sd->entity,
0192 IMX_MEDIA_GRP_ID_CSI2,
0193 true);
0194 if (IS_ERR(sd))
0195 sd = priv->src_sd;
0196 break;
0197 case IMX_MEDIA_GRP_ID_CSI2:
0198 break;
0199 default:
0200
0201
0202
0203
0204 sd = &priv->sd;
0205 break;
0206 }
0207
0208
0209 pad = imx_media_pipeline_pad(&sd->entity, 0, 0, true);
0210 if (!pad)
0211 return -ENODEV;
0212
0213 endpoint = imx_media_get_pad_fwnode(pad);
0214 if (IS_ERR(endpoint))
0215 return PTR_ERR(endpoint);
0216
0217 v4l2_fwnode_endpoint_parse(endpoint, ep);
0218
0219 fwnode_handle_put(endpoint);
0220
0221 return 0;
0222 }
0223
0224 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
0225 {
0226 if (priv->idmac_ch)
0227 ipu_idmac_put(priv->idmac_ch);
0228 priv->idmac_ch = NULL;
0229
0230 if (priv->smfc)
0231 ipu_smfc_put(priv->smfc);
0232 priv->smfc = NULL;
0233 }
0234
0235 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
0236 {
0237 int ch_num, ret;
0238 struct ipu_smfc *smfc;
0239 struct ipuv3_channel *idmac_ch;
0240
0241 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
0242
0243 smfc = ipu_smfc_get(priv->ipu, ch_num);
0244 if (IS_ERR(smfc)) {
0245 v4l2_err(&priv->sd, "failed to get SMFC\n");
0246 ret = PTR_ERR(smfc);
0247 goto out;
0248 }
0249 priv->smfc = smfc;
0250
0251 idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
0252 if (IS_ERR(idmac_ch)) {
0253 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
0254 ch_num);
0255 ret = PTR_ERR(idmac_ch);
0256 goto out;
0257 }
0258 priv->idmac_ch = idmac_ch;
0259
0260 return 0;
0261 out:
0262 csi_idmac_put_ipu_resources(priv);
0263 return ret;
0264 }
0265
0266 static void csi_vb2_buf_done(struct csi_priv *priv)
0267 {
0268 struct imx_media_video_dev *vdev = priv->vdev;
0269 struct imx_media_buffer *done, *next;
0270 struct vb2_buffer *vb;
0271 dma_addr_t phys;
0272
0273 done = priv->active_vb2_buf[priv->ipu_buf_num];
0274 if (done) {
0275 done->vbuf.field = vdev->fmt.field;
0276 done->vbuf.sequence = priv->frame_sequence;
0277 vb = &done->vbuf.vb2_buf;
0278 vb->timestamp = ktime_get_ns();
0279 vb2_buffer_done(vb, priv->nfb4eof ?
0280 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
0281 }
0282
0283 priv->frame_sequence++;
0284 priv->nfb4eof = false;
0285
0286
0287 next = imx_media_capture_device_next_buf(vdev);
0288 if (next) {
0289 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
0290 priv->active_vb2_buf[priv->ipu_buf_num] = next;
0291 } else {
0292 phys = priv->underrun_buf.phys;
0293 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
0294 }
0295
0296 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
0297 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
0298
0299 if (priv->interweave_swap)
0300 phys += vdev->fmt.bytesperline;
0301
0302 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
0303 }
0304
0305 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
0306 {
0307 struct csi_priv *priv = dev_id;
0308
0309 spin_lock(&priv->irqlock);
0310
0311 if (priv->last_eof) {
0312 complete(&priv->last_eof_comp);
0313 priv->last_eof = false;
0314 goto unlock;
0315 }
0316
0317 if (priv->fim)
0318
0319 imx_media_fim_eof_monitor(priv->fim, ktime_get());
0320
0321 csi_vb2_buf_done(priv);
0322
0323
0324 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
0325
0326 priv->ipu_buf_num ^= 1;
0327
0328
0329 mod_timer(&priv->eof_timeout_timer,
0330 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
0331
0332 unlock:
0333 spin_unlock(&priv->irqlock);
0334 return IRQ_HANDLED;
0335 }
0336
0337 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
0338 {
0339 struct csi_priv *priv = dev_id;
0340
0341 spin_lock(&priv->irqlock);
0342
0343
0344
0345
0346
0347 priv->nfb4eof = true;
0348
0349 v4l2_err(&priv->sd, "NFB4EOF\n");
0350
0351 spin_unlock(&priv->irqlock);
0352
0353 return IRQ_HANDLED;
0354 }
0355
0356
0357
0358
0359
0360 static void csi_idmac_eof_timeout(struct timer_list *t)
0361 {
0362 struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer);
0363 struct imx_media_video_dev *vdev = priv->vdev;
0364
0365 v4l2_err(&priv->sd, "EOF timeout\n");
0366
0367
0368 imx_media_capture_device_error(vdev);
0369 }
0370
0371 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
0372 {
0373 struct imx_media_video_dev *vdev = priv->vdev;
0374 struct imx_media_buffer *buf;
0375 int i;
0376
0377 for (i = 0; i < 2; i++) {
0378 buf = imx_media_capture_device_next_buf(vdev);
0379 if (buf) {
0380 priv->active_vb2_buf[i] = buf;
0381 phys[i] = vb2_dma_contig_plane_dma_addr(
0382 &buf->vbuf.vb2_buf, 0);
0383 } else {
0384 priv->active_vb2_buf[i] = NULL;
0385 phys[i] = priv->underrun_buf.phys;
0386 }
0387 }
0388 }
0389
0390 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
0391 enum vb2_buffer_state return_status)
0392 {
0393 struct imx_media_buffer *buf;
0394 int i;
0395
0396
0397 for (i = 0; i < 2; i++) {
0398 buf = priv->active_vb2_buf[i];
0399 if (buf) {
0400 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
0401
0402 vb->timestamp = ktime_get_ns();
0403 vb2_buffer_done(vb, return_status);
0404 }
0405 }
0406 }
0407
0408
0409 static int csi_idmac_setup_channel(struct csi_priv *priv)
0410 {
0411 struct imx_media_video_dev *vdev = priv->vdev;
0412 const struct imx_media_pixfmt *incc;
0413 struct v4l2_mbus_framefmt *infmt;
0414 struct v4l2_mbus_framefmt *outfmt;
0415 bool passthrough, interweave;
0416 struct ipu_image image;
0417 u32 passthrough_bits;
0418 u32 passthrough_cycles;
0419 dma_addr_t phys[2];
0420 u32 burst_size;
0421 int ret;
0422
0423 infmt = &priv->format_mbus[CSI_SINK_PAD];
0424 incc = priv->cc[CSI_SINK_PAD];
0425 outfmt = &priv->format_mbus[CSI_SRC_PAD_IDMAC];
0426
0427 ipu_cpmem_zero(priv->idmac_ch);
0428
0429 memset(&image, 0, sizeof(image));
0430 image.pix = vdev->fmt;
0431 image.rect = vdev->compose;
0432
0433 csi_idmac_setup_vb2_buf(priv, phys);
0434
0435 image.phys0 = phys[0];
0436 image.phys1 = phys[1];
0437
0438 passthrough = requires_passthrough(&priv->upstream_ep, infmt, incc);
0439 passthrough_cycles = 1;
0440
0441
0442
0443
0444
0445
0446 interweave = V4L2_FIELD_IS_INTERLACED(image.pix.field) &&
0447 V4L2_FIELD_IS_SEQUENTIAL(outfmt->field);
0448 priv->interweave_swap = interweave &&
0449 image.pix.field == V4L2_FIELD_INTERLACED_BT;
0450
0451 switch (image.pix.pixelformat) {
0452 case V4L2_PIX_FMT_SBGGR8:
0453 case V4L2_PIX_FMT_SGBRG8:
0454 case V4L2_PIX_FMT_SGRBG8:
0455 case V4L2_PIX_FMT_SRGGB8:
0456 case V4L2_PIX_FMT_GREY:
0457 burst_size = 16;
0458 passthrough_bits = 8;
0459 break;
0460 case V4L2_PIX_FMT_SBGGR16:
0461 case V4L2_PIX_FMT_SGBRG16:
0462 case V4L2_PIX_FMT_SGRBG16:
0463 case V4L2_PIX_FMT_SRGGB16:
0464 case V4L2_PIX_FMT_Y10:
0465 case V4L2_PIX_FMT_Y12:
0466 burst_size = 8;
0467 passthrough_bits = 16;
0468 break;
0469 case V4L2_PIX_FMT_YUV420:
0470 case V4L2_PIX_FMT_YVU420:
0471 case V4L2_PIX_FMT_NV12:
0472 burst_size = (image.pix.width & 0x3f) ?
0473 ((image.pix.width & 0x1f) ?
0474 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
0475 passthrough_bits = 16;
0476
0477
0478
0479
0480 if (!interweave)
0481 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
0482 break;
0483 case V4L2_PIX_FMT_YUYV:
0484 case V4L2_PIX_FMT_UYVY:
0485 burst_size = (image.pix.width & 0x1f) ?
0486 ((image.pix.width & 0xf) ? 8 : 16) : 32;
0487 passthrough_bits = 16;
0488 break;
0489 case V4L2_PIX_FMT_RGB565:
0490 if (passthrough) {
0491 burst_size = 16;
0492 passthrough_bits = 8;
0493 passthrough_cycles = incc->cycles;
0494 break;
0495 }
0496 fallthrough;
0497 default:
0498 burst_size = (image.pix.width & 0xf) ? 8 : 16;
0499 passthrough_bits = 16;
0500 break;
0501 }
0502
0503 if (passthrough) {
0504 if (priv->interweave_swap) {
0505
0506 image.phys0 += image.pix.bytesperline;
0507 image.phys1 += image.pix.bytesperline;
0508 }
0509
0510 ipu_cpmem_set_resolution(priv->idmac_ch,
0511 image.rect.width * passthrough_cycles,
0512 image.rect.height);
0513 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
0514 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
0515 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
0516 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
0517 passthrough_bits);
0518 } else {
0519 if (priv->interweave_swap) {
0520
0521 image.rect.top = 1;
0522 }
0523
0524 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
0525 if (ret)
0526 goto unsetup_vb2;
0527 }
0528
0529 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
0544 ipu_cpmem_set_high_priority(priv->idmac_ch);
0545 ipu_idmac_enable_watermark(priv->idmac_ch, true);
0546 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
0547
0548 burst_size = passthrough ?
0549 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
0550
0551 ipu_smfc_set_burstsize(priv->smfc, burst_size);
0552
0553 if (interweave)
0554 ipu_cpmem_interlaced_scan(priv->idmac_ch,
0555 priv->interweave_swap ?
0556 -image.pix.bytesperline :
0557 image.pix.bytesperline,
0558 image.pix.pixelformat);
0559
0560 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
0561
0562 return 0;
0563
0564 unsetup_vb2:
0565 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
0566 return ret;
0567 }
0568
0569 static void csi_idmac_unsetup(struct csi_priv *priv,
0570 enum vb2_buffer_state state)
0571 {
0572 ipu_idmac_disable_channel(priv->idmac_ch);
0573 ipu_smfc_disable(priv->smfc);
0574
0575 csi_idmac_unsetup_vb2_buf(priv, state);
0576 }
0577
0578 static int csi_idmac_setup(struct csi_priv *priv)
0579 {
0580 int ret;
0581
0582 ret = csi_idmac_setup_channel(priv);
0583 if (ret)
0584 return ret;
0585
0586 ipu_cpmem_dump(priv->idmac_ch);
0587 ipu_dump(priv->ipu);
0588
0589 ipu_smfc_enable(priv->smfc);
0590
0591
0592 ipu_idmac_select_buffer(priv->idmac_ch, 0);
0593 ipu_idmac_select_buffer(priv->idmac_ch, 1);
0594
0595
0596 ipu_idmac_enable_channel(priv->idmac_ch);
0597
0598 return 0;
0599 }
0600
0601 static int csi_idmac_start(struct csi_priv *priv)
0602 {
0603 struct imx_media_video_dev *vdev = priv->vdev;
0604 int ret;
0605
0606 ret = csi_idmac_get_ipu_resources(priv);
0607 if (ret)
0608 return ret;
0609
0610 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
0611
0612 ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf,
0613 vdev->fmt.sizeimage);
0614 if (ret)
0615 goto out_put_ipu;
0616
0617 priv->ipu_buf_num = 0;
0618
0619
0620 init_completion(&priv->last_eof_comp);
0621 priv->frame_sequence = 0;
0622 priv->last_eof = false;
0623 priv->nfb4eof = false;
0624
0625 ret = csi_idmac_setup(priv);
0626 if (ret) {
0627 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
0628 goto out_free_dma_buf;
0629 }
0630
0631 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
0632 priv->idmac_ch,
0633 IPU_IRQ_NFB4EOF);
0634 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
0635 csi_idmac_nfb4eof_interrupt, 0,
0636 "imx-smfc-nfb4eof", priv);
0637 if (ret) {
0638 v4l2_err(&priv->sd,
0639 "Error registering NFB4EOF irq: %d\n", ret);
0640 goto out_unsetup;
0641 }
0642
0643 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
0644 IPU_IRQ_EOF);
0645
0646 ret = devm_request_irq(priv->dev, priv->eof_irq,
0647 csi_idmac_eof_interrupt, 0,
0648 "imx-smfc-eof", priv);
0649 if (ret) {
0650 v4l2_err(&priv->sd,
0651 "Error registering eof irq: %d\n", ret);
0652 goto out_free_nfb4eof_irq;
0653 }
0654
0655
0656 mod_timer(&priv->eof_timeout_timer,
0657 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
0658
0659 return 0;
0660
0661 out_free_nfb4eof_irq:
0662 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
0663 out_unsetup:
0664 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
0665 out_free_dma_buf:
0666 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
0667 out_put_ipu:
0668 csi_idmac_put_ipu_resources(priv);
0669 return ret;
0670 }
0671
0672 static void csi_idmac_wait_last_eof(struct csi_priv *priv)
0673 {
0674 unsigned long flags;
0675 int ret;
0676
0677
0678 spin_lock_irqsave(&priv->irqlock, flags);
0679 priv->last_eof = true;
0680 spin_unlock_irqrestore(&priv->irqlock, flags);
0681
0682
0683
0684
0685 ret = wait_for_completion_timeout(
0686 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
0687 if (ret == 0)
0688 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
0689 }
0690
0691 static void csi_idmac_stop(struct csi_priv *priv)
0692 {
0693 devm_free_irq(priv->dev, priv->eof_irq, priv);
0694 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
0695
0696 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
0697
0698 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
0699
0700
0701 del_timer_sync(&priv->eof_timeout_timer);
0702
0703 csi_idmac_put_ipu_resources(priv);
0704 }
0705
0706
0707 static int csi_setup(struct csi_priv *priv)
0708 {
0709 struct v4l2_mbus_framefmt *infmt, *outfmt;
0710 const struct imx_media_pixfmt *incc;
0711 struct v4l2_mbus_config mbus_cfg;
0712 struct v4l2_mbus_framefmt if_fmt;
0713 struct v4l2_rect crop;
0714
0715 infmt = &priv->format_mbus[CSI_SINK_PAD];
0716 incc = priv->cc[CSI_SINK_PAD];
0717 outfmt = &priv->format_mbus[priv->active_output_pad];
0718
0719
0720 mbus_cfg.type = priv->upstream_ep.bus_type;
0721 if (is_parallel_bus(&priv->upstream_ep))
0722 mbus_cfg.bus.parallel = priv->upstream_ep.bus.parallel;
0723 else
0724 mbus_cfg.bus.mipi_csi2 = priv->upstream_ep.bus.mipi_csi2;
0725
0726 if_fmt = *infmt;
0727 crop = priv->crop;
0728
0729
0730
0731
0732
0733 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) {
0734 if_fmt.width *= incc->cycles;
0735 crop.width *= incc->cycles;
0736 }
0737
0738 ipu_csi_set_window(priv->csi, &crop);
0739
0740 ipu_csi_set_downsize(priv->csi,
0741 priv->crop.width == 2 * priv->compose.width,
0742 priv->crop.height == 2 * priv->compose.height);
0743
0744 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt, outfmt);
0745
0746 ipu_csi_set_dest(priv->csi, priv->dest);
0747
0748 if (priv->dest == IPU_CSI_DEST_IDMAC)
0749 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
0750 priv->skip->max_ratio - 1, 0);
0751
0752 ipu_csi_dump(priv->csi);
0753
0754 return 0;
0755 }
0756
0757 static int csi_start(struct csi_priv *priv)
0758 {
0759 struct v4l2_fract *input_fi, *output_fi;
0760 int ret;
0761
0762 input_fi = &priv->frame_interval[CSI_SINK_PAD];
0763 output_fi = &priv->frame_interval[priv->active_output_pad];
0764
0765
0766 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
0767 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
0768 if (ret)
0769 return ret;
0770
0771
0772 if (priv->upstream_ep.bus_type == V4L2_MBUS_BT656) {
0773 u32 delay_usec, bad_frames = 20;
0774
0775 delay_usec = DIV_ROUND_UP_ULL((u64)USEC_PER_SEC *
0776 input_fi->numerator * bad_frames,
0777 input_fi->denominator);
0778
0779 usleep_range(delay_usec, delay_usec + 1000);
0780 }
0781
0782 if (priv->dest == IPU_CSI_DEST_IDMAC) {
0783 ret = csi_idmac_start(priv);
0784 if (ret)
0785 goto stop_upstream;
0786 }
0787
0788 ret = csi_setup(priv);
0789 if (ret)
0790 goto idmac_stop;
0791
0792
0793 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
0794 ret = imx_media_fim_set_stream(priv->fim, output_fi, true);
0795 if (ret)
0796 goto idmac_stop;
0797 }
0798
0799 ret = ipu_csi_enable(priv->csi);
0800 if (ret) {
0801 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
0802 goto fim_off;
0803 }
0804
0805 return 0;
0806
0807 fim_off:
0808 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
0809 imx_media_fim_set_stream(priv->fim, NULL, false);
0810 idmac_stop:
0811 if (priv->dest == IPU_CSI_DEST_IDMAC)
0812 csi_idmac_stop(priv);
0813 stop_upstream:
0814 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
0815 return ret;
0816 }
0817
0818 static void csi_stop(struct csi_priv *priv)
0819 {
0820 if (priv->dest == IPU_CSI_DEST_IDMAC)
0821 csi_idmac_wait_last_eof(priv);
0822
0823
0824
0825
0826
0827
0828 ipu_csi_disable(priv->csi);
0829
0830
0831 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
0832
0833 if (priv->dest == IPU_CSI_DEST_IDMAC) {
0834 csi_idmac_stop(priv);
0835
0836
0837 if (priv->fim)
0838 imx_media_fim_set_stream(priv->fim, NULL, false);
0839 }
0840 }
0841
0842 static const struct csi_skip_desc csi_skip[12] = {
0843 { 1, 1, 0x00 },
0844 { 5, 6, 0x10 },
0845 { 4, 5, 0x08 },
0846 { 3, 4, 0x04 },
0847 { 2, 3, 0x02 },
0848 { 3, 5, 0x0a },
0849 { 1, 2, 0x01 },
0850 { 2, 5, 0x0b },
0851 { 1, 3, 0x03 },
0852 { 1, 4, 0x07 },
0853 { 1, 5, 0x0f },
0854 { 1, 6, 0x1f },
0855 };
0856
0857 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
0858 struct v4l2_fract *interval)
0859 {
0860 unsigned int div;
0861
0862 interval->numerator *= skip->max_ratio;
0863 interval->denominator *= skip->keep;
0864
0865
0866 div = gcd(interval->numerator, interval->denominator);
0867 if (div > 1) {
0868 interval->numerator /= div;
0869 interval->denominator /= div;
0870 }
0871 }
0872
0873
0874
0875
0876
0877
0878 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
0879 struct v4l2_fract *out)
0880 {
0881 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
0882 u32 min_err = UINT_MAX;
0883 u64 want_us;
0884 int i;
0885
0886
0887 if (out->numerator == 0 || out->denominator == 0 ||
0888 in->numerator == 0 || in->denominator == 0) {
0889 *out = *in;
0890 return best_skip;
0891 }
0892
0893 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
0894
0895
0896 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
0897 u64 tmp, err;
0898
0899 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
0900 skip->max_ratio, in->denominator * skip->keep);
0901
0902 err = abs((s64)tmp - want_us);
0903 if (err < min_err) {
0904 min_err = err;
0905 best_skip = skip;
0906 }
0907 }
0908
0909 *out = *in;
0910 csi_apply_skip_interval(best_skip, out);
0911
0912 return best_skip;
0913 }
0914
0915
0916
0917
0918
0919 static int csi_g_frame_interval(struct v4l2_subdev *sd,
0920 struct v4l2_subdev_frame_interval *fi)
0921 {
0922 struct csi_priv *priv = v4l2_get_subdevdata(sd);
0923
0924 if (fi->pad >= CSI_NUM_PADS)
0925 return -EINVAL;
0926
0927 mutex_lock(&priv->lock);
0928
0929 fi->interval = priv->frame_interval[fi->pad];
0930
0931 mutex_unlock(&priv->lock);
0932
0933 return 0;
0934 }
0935
0936 static int csi_s_frame_interval(struct v4l2_subdev *sd,
0937 struct v4l2_subdev_frame_interval *fi)
0938 {
0939 struct csi_priv *priv = v4l2_get_subdevdata(sd);
0940 struct v4l2_fract *input_fi;
0941 int ret = 0;
0942
0943 mutex_lock(&priv->lock);
0944
0945 input_fi = &priv->frame_interval[CSI_SINK_PAD];
0946
0947 switch (fi->pad) {
0948 case CSI_SINK_PAD:
0949
0950 if (fi->interval.numerator == 0 ||
0951 fi->interval.denominator == 0)
0952 fi->interval = *input_fi;
0953
0954 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
0955 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
0956 priv->skip = &csi_skip[0];
0957 break;
0958 case CSI_SRC_PAD_IDMAC:
0959
0960
0961
0962
0963 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
0964 break;
0965 case CSI_SRC_PAD_DIRECT:
0966
0967
0968
0969
0970 fi->interval = *input_fi;
0971 break;
0972 default:
0973 ret = -EINVAL;
0974 goto out;
0975 }
0976
0977 priv->frame_interval[fi->pad] = fi->interval;
0978 out:
0979 mutex_unlock(&priv->lock);
0980 return ret;
0981 }
0982
0983 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
0984 {
0985 struct csi_priv *priv = v4l2_get_subdevdata(sd);
0986 int ret = 0;
0987
0988 mutex_lock(&priv->lock);
0989
0990 if (!priv->src_sd || !priv->sink) {
0991 ret = -EPIPE;
0992 goto out;
0993 }
0994
0995
0996
0997
0998
0999 if (priv->stream_count != !enable)
1000 goto update_count;
1001
1002 if (enable) {
1003 dev_dbg(priv->dev, "stream ON\n");
1004 ret = csi_start(priv);
1005 if (ret)
1006 goto out;
1007 } else {
1008 dev_dbg(priv->dev, "stream OFF\n");
1009 csi_stop(priv);
1010 }
1011
1012 update_count:
1013 priv->stream_count += enable ? 1 : -1;
1014 if (priv->stream_count < 0)
1015 priv->stream_count = 0;
1016 out:
1017 mutex_unlock(&priv->lock);
1018 return ret;
1019 }
1020
1021 static int csi_link_setup(struct media_entity *entity,
1022 const struct media_pad *local,
1023 const struct media_pad *remote, u32 flags)
1024 {
1025 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1026 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1027 struct v4l2_subdev *remote_sd;
1028 int ret = 0;
1029
1030 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
1031 local->entity->name);
1032
1033 mutex_lock(&priv->lock);
1034
1035 if (local->flags & MEDIA_PAD_FL_SINK) {
1036 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1037 ret = -EINVAL;
1038 goto out;
1039 }
1040
1041 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1042
1043 if (flags & MEDIA_LNK_FL_ENABLED) {
1044 if (priv->src_sd) {
1045 ret = -EBUSY;
1046 goto out;
1047 }
1048 priv->src_sd = remote_sd;
1049 } else {
1050 priv->src_sd = NULL;
1051 }
1052
1053 goto out;
1054 }
1055
1056
1057
1058 if (flags & MEDIA_LNK_FL_ENABLED) {
1059 if (priv->sink) {
1060 ret = -EBUSY;
1061 goto out;
1062 }
1063 } else {
1064 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1065 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1066 priv->sink = NULL;
1067
1068 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1069 goto out;
1070 }
1071
1072
1073 priv->active_output_pad = local->index;
1074
1075
1076 if (local->index == CSI_SRC_PAD_IDMAC) {
1077 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1078 ret = -EINVAL;
1079 goto out;
1080 }
1081
1082 if (priv->fim) {
1083 ret = imx_media_fim_add_controls(priv->fim);
1084 if (ret)
1085 goto out;
1086 }
1087
1088 priv->dest = IPU_CSI_DEST_IDMAC;
1089 } else {
1090 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094
1095 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1096 switch (remote_sd->grp_id) {
1097 case IMX_MEDIA_GRP_ID_IPU_VDIC:
1098 priv->dest = IPU_CSI_DEST_VDIC;
1099 break;
1100 case IMX_MEDIA_GRP_ID_IPU_IC_PRP:
1101 priv->dest = IPU_CSI_DEST_IC;
1102 break;
1103 default:
1104 ret = -EINVAL;
1105 goto out;
1106 }
1107 }
1108
1109 priv->sink = remote->entity;
1110 out:
1111 mutex_unlock(&priv->lock);
1112 return ret;
1113 }
1114
1115 static int csi_link_validate(struct v4l2_subdev *sd,
1116 struct media_link *link,
1117 struct v4l2_subdev_format *source_fmt,
1118 struct v4l2_subdev_format *sink_fmt)
1119 {
1120 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1121 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1122 bool is_csi2;
1123 int ret;
1124
1125 ret = v4l2_subdev_link_validate_default(sd, link,
1126 source_fmt, sink_fmt);
1127 if (ret)
1128 return ret;
1129
1130 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1131 if (ret) {
1132 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1133 return ret;
1134 }
1135
1136 mutex_lock(&priv->lock);
1137
1138 priv->upstream_ep = upstream_ep;
1139 is_csi2 = !is_parallel_bus(&upstream_ep);
1140 if (is_csi2) {
1141
1142
1143
1144
1145
1146
1147
1148 ipu_csi_set_mipi_datatype(priv->csi, 0,
1149 &priv->format_mbus[CSI_SINK_PAD]);
1150 }
1151
1152
1153 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1154
1155 mutex_unlock(&priv->lock);
1156 return ret;
1157 }
1158
1159 static struct v4l2_mbus_framefmt *
1160 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1161 unsigned int pad, enum v4l2_subdev_format_whence which)
1162 {
1163 if (which == V4L2_SUBDEV_FORMAT_TRY)
1164 return v4l2_subdev_get_try_format(&priv->sd, sd_state, pad);
1165 else
1166 return &priv->format_mbus[pad];
1167 }
1168
1169 static struct v4l2_rect *
1170 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1171 enum v4l2_subdev_format_whence which)
1172 {
1173 if (which == V4L2_SUBDEV_FORMAT_TRY)
1174 return v4l2_subdev_get_try_crop(&priv->sd, sd_state,
1175 CSI_SINK_PAD);
1176 else
1177 return &priv->crop;
1178 }
1179
1180 static struct v4l2_rect *
1181 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1182 enum v4l2_subdev_format_whence which)
1183 {
1184 if (which == V4L2_SUBDEV_FORMAT_TRY)
1185 return v4l2_subdev_get_try_compose(&priv->sd, sd_state,
1186 CSI_SINK_PAD);
1187 else
1188 return &priv->compose;
1189 }
1190
1191 static void csi_try_crop(struct csi_priv *priv,
1192 struct v4l2_rect *crop,
1193 struct v4l2_subdev_state *sd_state,
1194 struct v4l2_mbus_framefmt *infmt,
1195 struct v4l2_fwnode_endpoint *upstream_ep)
1196 {
1197 u32 in_height;
1198
1199 crop->width = min_t(__u32, infmt->width, crop->width);
1200 if (crop->left + crop->width > infmt->width)
1201 crop->left = infmt->width - crop->width;
1202
1203 crop->left &= ~0x3;
1204 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT)
1205 crop->width &= ~0x7;
1206 else
1207 crop->width &= ~0x1;
1208
1209 in_height = infmt->height;
1210 if (infmt->field == V4L2_FIELD_ALTERNATE)
1211 in_height *= 2;
1212
1213
1214
1215
1216
1217
1218
1219 if (upstream_ep->bus_type == V4L2_MBUS_BT656 &&
1220 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1221 infmt->field == V4L2_FIELD_ALTERNATE)) {
1222 crop->height = in_height;
1223 crop->top = (in_height == 480) ? 2 : 0;
1224 } else {
1225 crop->height = min_t(__u32, in_height, crop->height);
1226 if (crop->top + crop->height > in_height)
1227 crop->top = in_height - crop->height;
1228 }
1229 }
1230
1231 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1232 struct v4l2_subdev_state *sd_state,
1233 struct v4l2_subdev_mbus_code_enum *code)
1234 {
1235 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1236 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1237 const struct imx_media_pixfmt *incc;
1238 struct v4l2_mbus_framefmt *infmt;
1239 int ret = 0;
1240
1241 mutex_lock(&priv->lock);
1242
1243 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, code->which);
1244 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY);
1245
1246 switch (code->pad) {
1247 case CSI_SINK_PAD:
1248 ret = imx_media_enum_mbus_formats(&code->code, code->index,
1249 PIXFMT_SEL_ANY);
1250 break;
1251 case CSI_SRC_PAD_DIRECT:
1252 case CSI_SRC_PAD_IDMAC:
1253 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1254 if (ret) {
1255 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1256 goto out;
1257 }
1258
1259 if (requires_passthrough(&upstream_ep, infmt, incc)) {
1260 if (code->index != 0) {
1261 ret = -EINVAL;
1262 goto out;
1263 }
1264 code->code = infmt->code;
1265 } else {
1266 enum imx_pixfmt_sel fmt_sel =
1267 (incc->cs == IPUV3_COLORSPACE_YUV) ?
1268 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
1269
1270 ret = imx_media_enum_ipu_formats(&code->code,
1271 code->index,
1272 fmt_sel);
1273 }
1274 break;
1275 default:
1276 ret = -EINVAL;
1277 }
1278
1279 out:
1280 mutex_unlock(&priv->lock);
1281 return ret;
1282 }
1283
1284 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1285 struct v4l2_subdev_state *sd_state,
1286 struct v4l2_subdev_frame_size_enum *fse)
1287 {
1288 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1289 struct v4l2_rect *crop;
1290 int ret = 0;
1291
1292 if (fse->pad >= CSI_NUM_PADS ||
1293 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1294 return -EINVAL;
1295
1296 mutex_lock(&priv->lock);
1297
1298 if (fse->pad == CSI_SINK_PAD) {
1299 fse->min_width = MIN_W;
1300 fse->max_width = MAX_W;
1301 fse->min_height = MIN_H;
1302 fse->max_height = MAX_H;
1303 } else {
1304 crop = __csi_get_crop(priv, sd_state, fse->which);
1305
1306 fse->min_width = fse->index & 1 ?
1307 crop->width / 2 : crop->width;
1308 fse->max_width = fse->min_width;
1309 fse->min_height = fse->index & 2 ?
1310 crop->height / 2 : crop->height;
1311 fse->max_height = fse->min_height;
1312 }
1313
1314 mutex_unlock(&priv->lock);
1315 return ret;
1316 }
1317
1318 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1319 struct v4l2_subdev_state *sd_state,
1320 struct v4l2_subdev_frame_interval_enum *fie)
1321 {
1322 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1323 struct v4l2_fract *input_fi;
1324 struct v4l2_rect *crop;
1325 int ret = 0;
1326
1327 if (fie->pad >= CSI_NUM_PADS ||
1328 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1329 1 : ARRAY_SIZE(csi_skip)))
1330 return -EINVAL;
1331
1332 mutex_lock(&priv->lock);
1333
1334 input_fi = &priv->frame_interval[CSI_SINK_PAD];
1335 crop = __csi_get_crop(priv, sd_state, fie->which);
1336
1337 if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1338 (fie->height != crop->height && fie->height != crop->height / 2)) {
1339 ret = -EINVAL;
1340 goto out;
1341 }
1342
1343 fie->interval = *input_fi;
1344
1345 if (fie->pad == CSI_SRC_PAD_IDMAC)
1346 csi_apply_skip_interval(&csi_skip[fie->index],
1347 &fie->interval);
1348
1349 out:
1350 mutex_unlock(&priv->lock);
1351 return ret;
1352 }
1353
1354 static int csi_get_fmt(struct v4l2_subdev *sd,
1355 struct v4l2_subdev_state *sd_state,
1356 struct v4l2_subdev_format *sdformat)
1357 {
1358 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1359 struct v4l2_mbus_framefmt *fmt;
1360 int ret = 0;
1361
1362 if (sdformat->pad >= CSI_NUM_PADS)
1363 return -EINVAL;
1364
1365 mutex_lock(&priv->lock);
1366
1367 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which);
1368 if (!fmt) {
1369 ret = -EINVAL;
1370 goto out;
1371 }
1372
1373 sdformat->format = *fmt;
1374 out:
1375 mutex_unlock(&priv->lock);
1376 return ret;
1377 }
1378
1379 static void csi_try_field(struct csi_priv *priv,
1380 struct v4l2_subdev_state *sd_state,
1381 struct v4l2_subdev_format *sdformat)
1382 {
1383 struct v4l2_mbus_framefmt *infmt =
1384 __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which);
1385
1386
1387
1388
1389
1390 if (sdformat->pad == CSI_SINK_PAD) {
1391 if (sdformat->format.field == V4L2_FIELD_ANY)
1392 sdformat->format.field = V4L2_FIELD_NONE;
1393 return;
1394 }
1395
1396 switch (infmt->field) {
1397 case V4L2_FIELD_SEQ_TB:
1398 case V4L2_FIELD_SEQ_BT:
1399
1400
1401
1402
1403
1404 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1405 sdformat->format.field = infmt->field;
1406 break;
1407 case V4L2_FIELD_ALTERNATE:
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1418 sdformat->format.field = (infmt->height == 480 / 2) ?
1419 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB;
1420 break;
1421 default:
1422
1423 sdformat->format.field = infmt->field;
1424 break;
1425 }
1426 }
1427
1428 static void csi_try_fmt(struct csi_priv *priv,
1429 struct v4l2_fwnode_endpoint *upstream_ep,
1430 struct v4l2_subdev_state *sd_state,
1431 struct v4l2_subdev_format *sdformat,
1432 struct v4l2_rect *crop,
1433 struct v4l2_rect *compose,
1434 const struct imx_media_pixfmt **cc)
1435 {
1436 const struct imx_media_pixfmt *incc;
1437 struct v4l2_mbus_framefmt *infmt;
1438 u32 code;
1439
1440 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which);
1441
1442 switch (sdformat->pad) {
1443 case CSI_SRC_PAD_DIRECT:
1444 case CSI_SRC_PAD_IDMAC:
1445 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY);
1446
1447 sdformat->format.width = compose->width;
1448 sdformat->format.height = compose->height;
1449
1450 if (requires_passthrough(upstream_ep, infmt, incc)) {
1451 sdformat->format.code = infmt->code;
1452 *cc = incc;
1453 } else {
1454 enum imx_pixfmt_sel fmt_sel =
1455 (incc->cs == IPUV3_COLORSPACE_YUV) ?
1456 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
1457
1458 *cc = imx_media_find_ipu_format(sdformat->format.code,
1459 fmt_sel);
1460 if (!*cc) {
1461 imx_media_enum_ipu_formats(&code, 0, fmt_sel);
1462 *cc = imx_media_find_ipu_format(code, fmt_sel);
1463 sdformat->format.code = (*cc)->codes[0];
1464 }
1465 }
1466
1467 csi_try_field(priv, sd_state, sdformat);
1468
1469
1470 sdformat->format.colorspace = infmt->colorspace;
1471 sdformat->format.xfer_func = infmt->xfer_func;
1472 sdformat->format.quantization = infmt->quantization;
1473 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1474
1475 break;
1476 case CSI_SINK_PAD:
1477 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1478 W_ALIGN, &sdformat->format.height,
1479 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1480
1481 *cc = imx_media_find_mbus_format(sdformat->format.code,
1482 PIXFMT_SEL_ANY);
1483 if (!*cc) {
1484 imx_media_enum_mbus_formats(&code, 0,
1485 PIXFMT_SEL_YUV_RGB);
1486 *cc = imx_media_find_mbus_format(code,
1487 PIXFMT_SEL_YUV_RGB);
1488 sdformat->format.code = (*cc)->codes[0];
1489 }
1490
1491 csi_try_field(priv, sd_state, sdformat);
1492
1493
1494 crop->left = 0;
1495 crop->top = 0;
1496 crop->width = sdformat->format.width;
1497 crop->height = sdformat->format.height;
1498 if (sdformat->format.field == V4L2_FIELD_ALTERNATE)
1499 crop->height *= 2;
1500 csi_try_crop(priv, crop, sd_state, &sdformat->format,
1501 upstream_ep);
1502 compose->left = 0;
1503 compose->top = 0;
1504 compose->width = crop->width;
1505 compose->height = crop->height;
1506
1507 break;
1508 }
1509
1510 imx_media_try_colorimetry(&sdformat->format,
1511 priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1512 }
1513
1514 static int csi_set_fmt(struct v4l2_subdev *sd,
1515 struct v4l2_subdev_state *sd_state,
1516 struct v4l2_subdev_format *sdformat)
1517 {
1518 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1519 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1520 const struct imx_media_pixfmt *cc;
1521 struct v4l2_mbus_framefmt *fmt;
1522 struct v4l2_rect *crop, *compose;
1523 int ret;
1524
1525 if (sdformat->pad >= CSI_NUM_PADS)
1526 return -EINVAL;
1527
1528 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1529 if (ret) {
1530 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1531 return ret;
1532 }
1533
1534 mutex_lock(&priv->lock);
1535
1536 if (priv->stream_count > 0) {
1537 ret = -EBUSY;
1538 goto out;
1539 }
1540
1541 crop = __csi_get_crop(priv, sd_state, sdformat->which);
1542 compose = __csi_get_compose(priv, sd_state, sdformat->which);
1543
1544 csi_try_fmt(priv, &upstream_ep, sd_state, sdformat, crop, compose,
1545 &cc);
1546
1547 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which);
1548 *fmt = sdformat->format;
1549
1550 if (sdformat->pad == CSI_SINK_PAD) {
1551 int pad;
1552
1553
1554 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1555 const struct imx_media_pixfmt *outcc;
1556 struct v4l2_mbus_framefmt *outfmt;
1557 struct v4l2_subdev_format format;
1558
1559 format.pad = pad;
1560 format.which = sdformat->which;
1561 format.format = sdformat->format;
1562 csi_try_fmt(priv, &upstream_ep, sd_state, &format,
1563 NULL, compose, &outcc);
1564
1565 outfmt = __csi_get_fmt(priv, sd_state, pad,
1566 sdformat->which);
1567 *outfmt = format.format;
1568
1569 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1570 priv->cc[pad] = outcc;
1571 }
1572 }
1573
1574 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1575 priv->cc[sdformat->pad] = cc;
1576
1577 out:
1578 mutex_unlock(&priv->lock);
1579 return ret;
1580 }
1581
1582 static int csi_get_selection(struct v4l2_subdev *sd,
1583 struct v4l2_subdev_state *sd_state,
1584 struct v4l2_subdev_selection *sel)
1585 {
1586 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1587 struct v4l2_mbus_framefmt *infmt;
1588 struct v4l2_rect *crop, *compose;
1589 int ret = 0;
1590
1591 if (sel->pad != CSI_SINK_PAD)
1592 return -EINVAL;
1593
1594 mutex_lock(&priv->lock);
1595
1596 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which);
1597 crop = __csi_get_crop(priv, sd_state, sel->which);
1598 compose = __csi_get_compose(priv, sd_state, sel->which);
1599
1600 switch (sel->target) {
1601 case V4L2_SEL_TGT_CROP_BOUNDS:
1602 sel->r.left = 0;
1603 sel->r.top = 0;
1604 sel->r.width = infmt->width;
1605 sel->r.height = infmt->height;
1606 if (infmt->field == V4L2_FIELD_ALTERNATE)
1607 sel->r.height *= 2;
1608 break;
1609 case V4L2_SEL_TGT_CROP:
1610 sel->r = *crop;
1611 break;
1612 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1613 sel->r.left = 0;
1614 sel->r.top = 0;
1615 sel->r.width = crop->width;
1616 sel->r.height = crop->height;
1617 break;
1618 case V4L2_SEL_TGT_COMPOSE:
1619 sel->r = *compose;
1620 break;
1621 default:
1622 ret = -EINVAL;
1623 }
1624
1625 mutex_unlock(&priv->lock);
1626 return ret;
1627 }
1628
1629 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1630 {
1631 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1632 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1633 *compose != crop && *compose != crop / 2)
1634 return -ERANGE;
1635
1636 if (*compose <= crop / 2 ||
1637 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1638 (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1639 *compose = crop / 2;
1640 else
1641 *compose = crop;
1642
1643 return 0;
1644 }
1645
1646 static int csi_set_selection(struct v4l2_subdev *sd,
1647 struct v4l2_subdev_state *sd_state,
1648 struct v4l2_subdev_selection *sel)
1649 {
1650 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1651 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1652 struct v4l2_mbus_framefmt *infmt;
1653 struct v4l2_rect *crop, *compose;
1654 int pad, ret;
1655
1656 if (sel->pad != CSI_SINK_PAD)
1657 return -EINVAL;
1658
1659 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1660 if (ret) {
1661 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1662 return ret;
1663 }
1664
1665 mutex_lock(&priv->lock);
1666
1667 if (priv->stream_count > 0) {
1668 ret = -EBUSY;
1669 goto out;
1670 }
1671
1672 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which);
1673 crop = __csi_get_crop(priv, sd_state, sel->which);
1674 compose = __csi_get_compose(priv, sd_state, sel->which);
1675
1676 switch (sel->target) {
1677 case V4L2_SEL_TGT_CROP:
1678
1679
1680
1681
1682
1683 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1684 sel->r = priv->crop;
1685 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1686 *crop = sel->r;
1687 goto out;
1688 }
1689
1690 csi_try_crop(priv, &sel->r, sd_state, infmt, &upstream_ep);
1691
1692 *crop = sel->r;
1693
1694
1695 compose->width = crop->width;
1696 compose->height = crop->height;
1697 break;
1698 case V4L2_SEL_TGT_COMPOSE:
1699
1700
1701
1702
1703
1704 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1705 sel->r = priv->compose;
1706 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1707 *compose = sel->r;
1708 goto out;
1709 }
1710
1711 sel->r.left = 0;
1712 sel->r.top = 0;
1713 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1714 if (ret)
1715 goto out;
1716 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1717 if (ret)
1718 goto out;
1719
1720 *compose = sel->r;
1721 break;
1722 default:
1723 ret = -EINVAL;
1724 goto out;
1725 }
1726
1727
1728 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1729 struct v4l2_mbus_framefmt *outfmt;
1730
1731 outfmt = __csi_get_fmt(priv, sd_state, pad, sel->which);
1732 outfmt->width = compose->width;
1733 outfmt->height = compose->height;
1734 }
1735
1736 out:
1737 mutex_unlock(&priv->lock);
1738 return ret;
1739 }
1740
1741 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1742 struct v4l2_event_subscription *sub)
1743 {
1744 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1745 return -EINVAL;
1746 if (sub->id != 0)
1747 return -EINVAL;
1748
1749 return v4l2_event_subscribe(fh, sub, 0, NULL);
1750 }
1751
1752 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1753 struct v4l2_event_subscription *sub)
1754 {
1755 return v4l2_event_unsubscribe(fh, sub);
1756 }
1757
1758 static int csi_registered(struct v4l2_subdev *sd)
1759 {
1760 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1761 struct ipu_csi *csi;
1762 int i, ret;
1763 u32 code;
1764
1765
1766 csi = ipu_csi_get(priv->ipu, priv->csi_id);
1767 if (IS_ERR(csi)) {
1768 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1769 return PTR_ERR(csi);
1770 }
1771 priv->csi = csi;
1772
1773 for (i = 0; i < CSI_NUM_PADS; i++) {
1774 code = 0;
1775 if (i != CSI_SINK_PAD)
1776 imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV);
1777
1778
1779 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1780 IMX_MEDIA_DEF_PIX_WIDTH,
1781 IMX_MEDIA_DEF_PIX_HEIGHT, code,
1782 V4L2_FIELD_NONE, &priv->cc[i]);
1783 if (ret)
1784 goto put_csi;
1785
1786
1787 priv->frame_interval[i].numerator = 1;
1788 priv->frame_interval[i].denominator = 30;
1789 }
1790
1791
1792 priv->skip = &csi_skip[0];
1793
1794
1795 priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH;
1796 priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1797 priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH;
1798 priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1799
1800 priv->fim = imx_media_fim_init(&priv->sd);
1801 if (IS_ERR(priv->fim)) {
1802 ret = PTR_ERR(priv->fim);
1803 goto put_csi;
1804 }
1805
1806 priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd,
1807 CSI_SRC_PAD_IDMAC, true);
1808 if (IS_ERR(priv->vdev)) {
1809 ret = PTR_ERR(priv->vdev);
1810 goto free_fim;
1811 }
1812
1813 ret = imx_media_capture_device_register(priv->vdev, 0);
1814 if (ret)
1815 goto remove_vdev;
1816
1817 return 0;
1818
1819 remove_vdev:
1820 imx_media_capture_device_remove(priv->vdev);
1821 free_fim:
1822 if (priv->fim)
1823 imx_media_fim_free(priv->fim);
1824 put_csi:
1825 ipu_csi_put(priv->csi);
1826 return ret;
1827 }
1828
1829 static void csi_unregistered(struct v4l2_subdev *sd)
1830 {
1831 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1832
1833 imx_media_capture_device_unregister(priv->vdev);
1834 imx_media_capture_device_remove(priv->vdev);
1835
1836 if (priv->fim)
1837 imx_media_fim_free(priv->fim);
1838
1839 if (priv->csi)
1840 ipu_csi_put(priv->csi);
1841 }
1842
1843
1844
1845
1846
1847 static int csi_get_fwnode_pad(struct media_entity *entity,
1848 struct fwnode_endpoint *endpoint)
1849 {
1850 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1851 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1852 struct fwnode_handle *csi_port = dev_fwnode(priv->dev);
1853 struct fwnode_handle *csi_ep;
1854 int ret;
1855
1856 csi_ep = fwnode_get_next_child_node(csi_port, NULL);
1857
1858 ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO;
1859
1860 fwnode_handle_put(csi_ep);
1861
1862 return ret;
1863 }
1864
1865 static const struct media_entity_operations csi_entity_ops = {
1866 .link_setup = csi_link_setup,
1867 .link_validate = v4l2_subdev_link_validate,
1868 .get_fwnode_pad = csi_get_fwnode_pad,
1869 };
1870
1871 static const struct v4l2_subdev_core_ops csi_core_ops = {
1872 .subscribe_event = csi_subscribe_event,
1873 .unsubscribe_event = csi_unsubscribe_event,
1874 };
1875
1876 static const struct v4l2_subdev_video_ops csi_video_ops = {
1877 .g_frame_interval = csi_g_frame_interval,
1878 .s_frame_interval = csi_s_frame_interval,
1879 .s_stream = csi_s_stream,
1880 };
1881
1882 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1883 .init_cfg = imx_media_init_cfg,
1884 .enum_mbus_code = csi_enum_mbus_code,
1885 .enum_frame_size = csi_enum_frame_size,
1886 .enum_frame_interval = csi_enum_frame_interval,
1887 .get_fmt = csi_get_fmt,
1888 .set_fmt = csi_set_fmt,
1889 .get_selection = csi_get_selection,
1890 .set_selection = csi_set_selection,
1891 .link_validate = csi_link_validate,
1892 };
1893
1894 static const struct v4l2_subdev_ops csi_subdev_ops = {
1895 .core = &csi_core_ops,
1896 .video = &csi_video_ops,
1897 .pad = &csi_pad_ops,
1898 };
1899
1900 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1901 .registered = csi_registered,
1902 .unregistered = csi_unregistered,
1903 };
1904
1905 static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier,
1906 struct v4l2_subdev *sd,
1907 struct v4l2_async_subdev *asd)
1908 {
1909 struct csi_priv *priv = notifier_to_dev(notifier);
1910 struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD];
1911
1912
1913
1914
1915
1916 if (sd->entity.function == MEDIA_ENT_F_VID_MUX)
1917 sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX;
1918
1919 return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
1920 }
1921
1922 static const struct v4l2_async_notifier_operations csi_notify_ops = {
1923 .bound = imx_csi_notify_bound,
1924 };
1925
1926 static int imx_csi_async_register(struct csi_priv *priv)
1927 {
1928 struct v4l2_async_subdev *asd = NULL;
1929 struct fwnode_handle *ep;
1930 unsigned int port;
1931 int ret;
1932
1933 v4l2_async_nf_init(&priv->notifier);
1934
1935
1936 ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port);
1937 if (ret < 0)
1938 return ret;
1939
1940 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent),
1941 port, 0,
1942 FWNODE_GRAPH_ENDPOINT_NEXT);
1943 if (ep) {
1944 asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep,
1945 struct v4l2_async_subdev);
1946
1947 fwnode_handle_put(ep);
1948
1949 if (IS_ERR(asd)) {
1950 ret = PTR_ERR(asd);
1951
1952 if (ret != -EEXIST)
1953 return ret;
1954 }
1955 }
1956
1957 priv->notifier.ops = &csi_notify_ops;
1958
1959 ret = v4l2_async_subdev_nf_register(&priv->sd, &priv->notifier);
1960 if (ret)
1961 return ret;
1962
1963 return v4l2_async_register_subdev(&priv->sd);
1964 }
1965
1966 static int imx_csi_probe(struct platform_device *pdev)
1967 {
1968 struct ipu_client_platformdata *pdata;
1969 struct pinctrl *pinctrl;
1970 struct csi_priv *priv;
1971 int i, ret;
1972
1973 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1974 if (!priv)
1975 return -ENOMEM;
1976
1977 platform_set_drvdata(pdev, &priv->sd);
1978 priv->dev = &pdev->dev;
1979
1980 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1981 if (ret)
1982 return ret;
1983
1984
1985 priv->ipu = dev_get_drvdata(priv->dev->parent);
1986
1987
1988 pdata = priv->dev->platform_data;
1989 priv->csi_id = pdata->csi;
1990 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1991
1992 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1993
1994 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1995 spin_lock_init(&priv->irqlock);
1996
1997 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1998 v4l2_set_subdevdata(&priv->sd, priv);
1999 priv->sd.internal_ops = &csi_internal_ops;
2000 priv->sd.entity.ops = &csi_entity_ops;
2001 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
2002 priv->sd.dev = &pdev->dev;
2003 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
2004 priv->sd.owner = THIS_MODULE;
2005 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2006 priv->sd.grp_id = priv->csi_id ?
2007 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0;
2008 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
2009 priv->sd.grp_id, ipu_get_num(priv->ipu));
2010
2011 for (i = 0; i < CSI_NUM_PADS; i++)
2012 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
2013 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2014
2015 ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS,
2016 priv->pad);
2017 if (ret)
2018 return ret;
2019
2020 mutex_init(&priv->lock);
2021
2022 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
2023 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
2024
2025
2026
2027
2028
2029
2030
2031 priv->dev->of_node = pdata->of_node;
2032 pinctrl = devm_pinctrl_get_select_default(priv->dev);
2033 if (IS_ERR(pinctrl)) {
2034 ret = PTR_ERR(pinctrl);
2035 dev_dbg(priv->dev,
2036 "devm_pinctrl_get_select_default() failed: %d\n", ret);
2037 if (ret != -ENODEV)
2038 goto free;
2039 }
2040
2041 ret = imx_csi_async_register(priv);
2042 if (ret)
2043 goto cleanup;
2044
2045 return 0;
2046
2047 cleanup:
2048 v4l2_async_nf_unregister(&priv->notifier);
2049 v4l2_async_nf_cleanup(&priv->notifier);
2050 free:
2051 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2052 mutex_destroy(&priv->lock);
2053 return ret;
2054 }
2055
2056 static int imx_csi_remove(struct platform_device *pdev)
2057 {
2058 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
2059 struct csi_priv *priv = sd_to_dev(sd);
2060
2061 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2062 mutex_destroy(&priv->lock);
2063 v4l2_async_nf_unregister(&priv->notifier);
2064 v4l2_async_nf_cleanup(&priv->notifier);
2065 v4l2_async_unregister_subdev(sd);
2066 media_entity_cleanup(&sd->entity);
2067
2068 return 0;
2069 }
2070
2071 static const struct platform_device_id imx_csi_ids[] = {
2072 { .name = "imx-ipuv3-csi" },
2073 { },
2074 };
2075 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
2076
2077 static struct platform_driver imx_csi_driver = {
2078 .probe = imx_csi_probe,
2079 .remove = imx_csi_remove,
2080 .id_table = imx_csi_ids,
2081 .driver = {
2082 .name = "imx-ipuv3-csi",
2083 },
2084 };
2085 module_platform_driver(imx_csi_driver);
2086
2087 MODULE_DESCRIPTION("i.MX CSI subdev driver");
2088 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
2089 MODULE_LICENSE("GPL");