1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI CAL camera interface driver
5 * Copyright (c) 2015 Texas Instruments Inc.
6 * Benoit Parrot, <bparrot@ti.com>
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
14 #include <linux/ioctl.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-fh.h>
31 #include <media/v4l2-fwnode.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/videobuf2-core.h>
34 #include <media/videobuf2-dma-contig.h>
38 #define CAL_MODULE_NAME "cal"
40 MODULE_DESCRIPTION("TI CAL driver");
41 MODULE_AUTHOR("Benoit Parrot, <bparrot@ti.com>");
42 MODULE_LICENSE("GPL v2");
43 MODULE_VERSION("0.1.0");
45 static unsigned video_nr = -1;
46 module_param(video_nr, uint, 0644);
47 MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
49 static unsigned debug;
50 module_param(debug, uint, 0644);
51 MODULE_PARM_DESC(debug, "activates debug info");
53 #define cal_dbg(level, cal, fmt, arg...) \
55 if (debug >= (level)) \
56 dev_printk(KERN_DEBUG, &(cal)->pdev->dev, \
59 #define cal_info(cal, fmt, arg...) \
60 dev_info(&(cal)->pdev->dev, fmt, ##arg)
61 #define cal_err(cal, fmt, arg...) \
62 dev_err(&(cal)->pdev->dev, fmt, ##arg)
64 #define ctx_dbg(level, ctx, fmt, arg...) \
65 cal_dbg(level, (ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
66 #define ctx_info(ctx, fmt, arg...) \
67 cal_info((ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
68 #define ctx_err(ctx, fmt, arg...) \
69 cal_err((ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
71 #define phy_dbg(level, phy, fmt, arg...) \
72 cal_dbg(level, (phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
73 #define phy_info(phy, fmt, arg...) \
74 cal_info((phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
75 #define phy_err(phy, fmt, arg...) \
76 cal_err((phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
78 #define CAL_NUM_CONTEXT 2
80 #define MAX_WIDTH_BYTES (8192 * 8)
81 #define MAX_HEIGHT_LINES 16383
83 /* ------------------------------------------------------------------
85 * ------------------------------------------------------------------
95 static const struct cal_fmt cal_formats[] = {
97 .fourcc = V4L2_PIX_FMT_YUYV,
98 .code = MEDIA_BUS_FMT_YUYV8_2X8,
101 .fourcc = V4L2_PIX_FMT_UYVY,
102 .code = MEDIA_BUS_FMT_UYVY8_2X8,
105 .fourcc = V4L2_PIX_FMT_YVYU,
106 .code = MEDIA_BUS_FMT_YVYU8_2X8,
109 .fourcc = V4L2_PIX_FMT_VYUY,
110 .code = MEDIA_BUS_FMT_VYUY8_2X8,
113 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
114 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
117 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
118 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
121 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
122 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
125 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
126 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
129 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
130 .code = MEDIA_BUS_FMT_RGB888_2X12_LE,
133 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
134 .code = MEDIA_BUS_FMT_RGB888_2X12_BE,
137 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
138 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
141 .fourcc = V4L2_PIX_FMT_SBGGR8,
142 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
145 .fourcc = V4L2_PIX_FMT_SGBRG8,
146 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
149 .fourcc = V4L2_PIX_FMT_SGRBG8,
150 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
153 .fourcc = V4L2_PIX_FMT_SRGGB8,
154 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
157 .fourcc = V4L2_PIX_FMT_SBGGR10,
158 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
161 .fourcc = V4L2_PIX_FMT_SGBRG10,
162 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
165 .fourcc = V4L2_PIX_FMT_SGRBG10,
166 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
169 .fourcc = V4L2_PIX_FMT_SRGGB10,
170 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
173 .fourcc = V4L2_PIX_FMT_SBGGR12,
174 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
177 .fourcc = V4L2_PIX_FMT_SGBRG12,
178 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
181 .fourcc = V4L2_PIX_FMT_SGRBG12,
182 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
185 .fourcc = V4L2_PIX_FMT_SRGGB12,
186 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
191 /* Print Four-character-code (FOURCC) */
192 static char *fourcc_to_str(u32 fmt)
196 code[0] = (unsigned char)(fmt & 0xff);
197 code[1] = (unsigned char)((fmt >> 8) & 0xff);
198 code[2] = (unsigned char)((fmt >> 16) & 0xff);
199 code[3] = (unsigned char)((fmt >> 24) & 0xff);
205 /* ------------------------------------------------------------------
207 * ------------------------------------------------------------------
210 /* buffer for one video frame */
212 /* common v4l buffer stuff -- must be first */
213 struct vb2_v4l2_buffer vb;
214 struct list_head list;
217 struct cal_dmaqueue {
218 struct list_head active;
221 /* CTRL_CORE_CAMERRX_CONTROL register field id */
222 enum cal_camerarx_field {
231 struct cal_camerarx_data {
235 } fields[F_MAX_FIELDS];
236 unsigned int num_lanes;
240 const struct cal_camerarx_data *camerarx;
241 unsigned int num_csi2_phy;
246 * The Camera Adaptation Layer (CAL) module is paired with one or more complex
247 * I/O PHYs (CAMERARX). It contains multiple instances of CSI-2, processing and
250 * The cal_dev structure represents the whole subsystem, including the CAL and
251 * the CAMERARX instances. Instances of struct cal_dev are named cal through the
254 * The cal_camerarx structure represents one CAMERARX instance. Instances of
255 * cal_camerarx are named phy through the driver.
257 * The cal_ctx structure represents the combination of one CSI-2 context, one
258 * processing context and one DMA context. Instance of struct cal_ctx are named
259 * ctx through the driver.
262 struct cal_camerarx {
264 struct resource *res;
265 struct platform_device *pdev;
266 struct regmap_field *fields[F_MAX_FIELDS];
269 unsigned int instance;
271 struct v4l2_fwnode_endpoint endpoint;
272 struct v4l2_subdev *sensor;
273 unsigned int external_rate;
280 struct resource *res;
281 struct platform_device *pdev;
283 const struct cal_data *data;
285 /* Control Module handle */
286 struct regmap *syscon_camerrx;
287 u32 syscon_camerrx_offset;
289 /* Camera Core Module handle */
290 struct cal_camerarx *phy[CAL_NUM_CSI2_PORTS];
292 struct cal_ctx *ctx[CAL_NUM_CONTEXT];
296 * There is one cal_ctx structure for each camera core context.
299 struct v4l2_device v4l2_dev;
300 struct v4l2_ctrl_handler ctrl_handler;
301 struct video_device vdev;
302 struct v4l2_async_notifier notifier;
305 struct cal_camerarx *phy;
307 /* v4l2_ioctl mutex */
309 /* v4l2 buffers lock */
312 struct cal_dmaqueue vidq;
315 const struct cal_fmt *fmt;
316 /* Used to store current pixel format */
317 struct v4l2_format v_fmt;
318 /* Used to store current mbus frame format */
319 struct v4l2_mbus_framefmt m_fmt;
321 /* Current subdev enumerated format */
322 const struct cal_fmt *active_fmt[ARRAY_SIZE(cal_formats)];
323 unsigned int num_active_fmt;
325 unsigned int sequence;
326 struct vb2_queue vb_vidq;
329 unsigned int virtual_channel;
331 /* Pointer pointing to current v4l2_buffer */
332 struct cal_buffer *cur_frm;
333 /* Pointer pointing to next v4l2_buffer */
334 struct cal_buffer *next_frm;
339 static inline struct cal_ctx *notifier_to_ctx(struct v4l2_async_notifier *n)
341 return container_of(n, struct cal_ctx, notifier);
344 /* ------------------------------------------------------------------
346 * ------------------------------------------------------------------
349 static const struct cal_camerarx_data dra72x_cal_camerarx[] = {
352 [F_CTRLCLKEN] = { 10, 10 },
353 [F_CAMMODE] = { 11, 12 },
354 [F_LANEENABLE] = { 13, 16 },
355 [F_CSI_MODE] = { 17, 17 },
361 [F_CTRLCLKEN] = { 0, 0 },
362 [F_CAMMODE] = { 1, 2 },
363 [F_LANEENABLE] = { 3, 4 },
364 [F_CSI_MODE] = { 5, 5 },
370 static const struct cal_data dra72x_cal_data = {
371 .camerarx = dra72x_cal_camerarx,
372 .num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
375 static const struct cal_data dra72x_es1_cal_data = {
376 .camerarx = dra72x_cal_camerarx,
377 .num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
378 .flags = DRA72_CAL_PRE_ES2_LDO_DISABLE,
381 static const struct cal_camerarx_data dra76x_cal_csi_phy[] = {
384 [F_CTRLCLKEN] = { 8, 8 },
385 [F_CAMMODE] = { 9, 10 },
386 [F_CSI_MODE] = { 11, 11 },
387 [F_LANEENABLE] = { 27, 31 },
393 [F_CTRLCLKEN] = { 0, 0 },
394 [F_CAMMODE] = { 1, 2 },
395 [F_CSI_MODE] = { 3, 3 },
396 [F_LANEENABLE] = { 24, 26 },
402 static const struct cal_data dra76x_cal_data = {
403 .camerarx = dra76x_cal_csi_phy,
404 .num_csi2_phy = ARRAY_SIZE(dra76x_cal_csi_phy),
407 static const struct cal_camerarx_data am654_cal_csi_phy[] = {
410 [F_CTRLCLKEN] = { 15, 15 },
411 [F_CAMMODE] = { 24, 25 },
412 [F_LANEENABLE] = { 0, 4 },
418 static const struct cal_data am654_cal_data = {
419 .camerarx = am654_cal_csi_phy,
420 .num_csi2_phy = ARRAY_SIZE(am654_cal_csi_phy),
423 /* ------------------------------------------------------------------
424 * I/O Register Accessors
425 * ------------------------------------------------------------------
428 #define reg_read(dev, offset) ioread32(dev->base + offset)
429 #define reg_write(dev, offset, val) iowrite32(val, dev->base + offset)
431 static inline u32 reg_read_field(struct cal_dev *cal, u32 offset, u32 mask)
433 return FIELD_GET(mask, reg_read(cal, offset));
436 static inline void reg_write_field(struct cal_dev *cal, u32 offset, u32 value,
439 u32 val = reg_read(cal, offset);
442 val |= FIELD_PREP(mask, value);
443 reg_write(cal, offset, val);
446 static inline void set_field(u32 *valp, u32 field, u32 mask)
451 val |= (field << __ffs(mask)) & mask;
455 static void cal_quickdump_regs(struct cal_dev *cal)
457 cal_info(cal, "CAL Registers @ 0x%pa:\n", &cal->res->start);
458 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
459 (__force const void *)cal->base,
460 resource_size(cal->res), false);
463 cal_info(cal, "CSI2 Core 0 Registers @ %pa:\n",
464 &cal->ctx[0]->phy->res->start);
465 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
466 (__force const void *)cal->ctx[0]->phy->base,
467 resource_size(cal->ctx[0]->phy->res),
472 cal_info(cal, "CSI2 Core 1 Registers @ %pa:\n",
473 &cal->ctx[1]->phy->res->start);
474 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
475 (__force const void *)cal->ctx[1]->phy->base,
476 resource_size(cal->ctx[1]->phy->res),
481 /* ------------------------------------------------------------------
482 * CAMERARX Management
483 * ------------------------------------------------------------------
486 static u32 cal_camerarx_max_lanes(struct cal_camerarx *phy)
488 return phy->cal->data->camerarx[phy->instance].num_lanes;
491 static void cal_camerarx_enable(struct cal_camerarx *phy)
495 regmap_field_write(phy->fields[F_CAMMODE], 0);
496 /* Always enable all lanes at the phy control level */
497 max_lanes = (1 << cal_camerarx_max_lanes(phy)) - 1;
498 regmap_field_write(phy->fields[F_LANEENABLE], max_lanes);
499 /* F_CSI_MODE is not present on every architecture */
500 if (phy->fields[F_CSI_MODE])
501 regmap_field_write(phy->fields[F_CSI_MODE], 1);
502 regmap_field_write(phy->fields[F_CTRLCLKEN], 1);
505 static void cal_camerarx_disable(struct cal_camerarx *phy)
507 regmap_field_write(phy->fields[F_CTRLCLKEN], 0);
511 * Errata i913: CSI2 LDO Needs to be disabled when module is powered on
513 * Enabling CSI2 LDO shorts it to core supply. It is crucial the 2 CSI2
514 * LDOs on the device are disabled if CSI-2 module is powered on
515 * (0x4845 B304 | 0x4845 B384 [28:27] = 0x1) or in ULPS (0x4845 B304
516 * | 0x4845 B384 [28:27] = 0x2) mode. Common concerns include: high
517 * current draw on the module supply in active mode.
519 * Errata does not apply when CSI-2 module is powered off
520 * (0x4845 B304 | 0x4845 B384 [28:27] = 0x0).
523 * Set the following register bits to disable the LDO,
524 * which is essentially CSI2 REG10 bit 6:
526 * Core 0: 0x4845 B828 = 0x0000 0040
527 * Core 1: 0x4845 B928 = 0x0000 0040
529 static void cal_camerarx_i913_errata(struct cal_camerarx *phy)
531 u32 reg10 = reg_read(phy, CAL_CSI2_PHY_REG10);
533 set_field(®10, 1, CAL_CSI2_PHY_REG10_I933_LDO_DISABLE_MASK);
535 phy_dbg(1, phy, "CSI2_%d_REG10 = 0x%08x\n", phy->instance, reg10);
536 reg_write(phy, CAL_CSI2_PHY_REG10, reg10);
540 * Enable the expected IRQ sources
542 static void cal_camerarx_enable_irqs(struct cal_camerarx *phy)
546 const u32 cio_err_mask =
547 CAL_CSI2_COMPLEXIO_IRQ_LANE_ERRORS_MASK |
548 CAL_CSI2_COMPLEXIO_IRQ_FIFO_OVR_MASK |
549 CAL_CSI2_COMPLEXIO_IRQ_SHORT_PACKET_MASK |
550 CAL_CSI2_COMPLEXIO_IRQ_ECC_NO_CORRECTION_MASK;
552 /* Enable CIO error irqs */
553 reg_write(phy->cal, CAL_HL_IRQENABLE_SET(0),
554 CAL_HL_IRQ_CIO_MASK(phy->instance));
555 reg_write(phy->cal, CAL_CSI2_COMPLEXIO_IRQENABLE(phy->instance),
558 /* Always enable OCPO error */
559 reg_write(phy->cal, CAL_HL_IRQENABLE_SET(0), CAL_HL_IRQ_OCPO_ERR_MASK);
561 /* Enable IRQ_WDMA_END 0/1 */
563 set_field(&val, 1, CAL_HL_IRQ_MASK(phy->instance));
564 reg_write(phy->cal, CAL_HL_IRQENABLE_SET(1), val);
565 /* Enable IRQ_WDMA_START 0/1 */
567 set_field(&val, 1, CAL_HL_IRQ_MASK(phy->instance));
568 reg_write(phy->cal, CAL_HL_IRQENABLE_SET(2), val);
569 /* Todo: Add VC_IRQ and CSI2_COMPLEXIO_IRQ handling */
570 reg_write(phy->cal, CAL_CSI2_VC_IRQENABLE(0), 0xFF000000);
573 static void cal_camerarx_disable_irqs(struct cal_camerarx *phy)
577 /* Disable CIO error irqs */
578 reg_write(phy->cal, CAL_HL_IRQENABLE_CLR(0),
579 CAL_HL_IRQ_CIO_MASK(phy->instance));
580 reg_write(phy->cal, CAL_CSI2_COMPLEXIO_IRQENABLE(phy->instance),
583 /* Disable IRQ_WDMA_END 0/1 */
585 set_field(&val, 1, CAL_HL_IRQ_MASK(phy->instance));
586 reg_write(phy->cal, CAL_HL_IRQENABLE_CLR(1), val);
587 /* Disable IRQ_WDMA_START 0/1 */
589 set_field(&val, 1, CAL_HL_IRQ_MASK(phy->instance));
590 reg_write(phy->cal, CAL_HL_IRQENABLE_CLR(2), val);
591 /* Todo: Add VC_IRQ and CSI2_COMPLEXIO_IRQ handling */
592 reg_write(phy->cal, CAL_CSI2_VC_IRQENABLE(0), 0);
595 static void cal_camerarx_power(struct cal_camerarx *phy, bool enable)
600 target_state = enable ? CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_STATE_ON :
601 CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_STATE_OFF;
603 reg_write_field(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance),
604 target_state, CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_MASK);
606 for (i = 0; i < 10; i++) {
609 current_state = reg_read_field(phy->cal,
610 CAL_CSI2_COMPLEXIO_CFG(phy->instance),
611 CAL_CSI2_COMPLEXIO_CFG_PWR_STATUS_MASK);
613 if (current_state == target_state)
616 usleep_range(1000, 1100);
620 phy_err(phy, "Failed to power %s complexio\n",
621 enable ? "up" : "down");
625 * TCLK values are OK at their reset values
629 #define TCLK_SETTLE 14
631 static void cal_camerarx_config(struct cal_camerarx *phy,
632 const struct cal_fmt *fmt)
634 unsigned int reg0, reg1;
635 unsigned int ths_term, ths_settle;
636 unsigned int csi2_ddrclk_khz;
637 struct v4l2_fwnode_bus_mipi_csi2 *mipi_csi2 =
638 &phy->endpoint.bus.mipi_csi2;
639 u32 num_lanes = mipi_csi2->num_data_lanes;
641 /* DPHY timing configuration */
642 /* CSI-2 is DDR and we only count used lanes. */
643 csi2_ddrclk_khz = phy->external_rate / 1000
644 / (2 * num_lanes) * fmt->bpp;
645 phy_dbg(1, phy, "csi2_ddrclk_khz: %d\n", csi2_ddrclk_khz);
647 /* THS_TERM: Programmed value = floor(20 ns/DDRClk period) */
648 ths_term = 20 * csi2_ddrclk_khz / 1000000;
649 phy_dbg(1, phy, "ths_term: %d (0x%02x)\n", ths_term, ths_term);
651 /* THS_SETTLE: Programmed value = floor(105 ns/DDRClk period) + 4 */
652 ths_settle = (105 * csi2_ddrclk_khz / 1000000) + 4;
653 phy_dbg(1, phy, "ths_settle: %d (0x%02x)\n", ths_settle, ths_settle);
655 reg0 = reg_read(phy, CAL_CSI2_PHY_REG0);
656 set_field(®0, CAL_CSI2_PHY_REG0_HSCLOCKCONFIG_DISABLE,
657 CAL_CSI2_PHY_REG0_HSCLOCKCONFIG_MASK);
658 set_field(®0, ths_term, CAL_CSI2_PHY_REG0_THS_TERM_MASK);
659 set_field(®0, ths_settle, CAL_CSI2_PHY_REG0_THS_SETTLE_MASK);
661 phy_dbg(1, phy, "CSI2_%d_REG0 = 0x%08x\n", phy->instance, reg0);
662 reg_write(phy, CAL_CSI2_PHY_REG0, reg0);
664 reg1 = reg_read(phy, CAL_CSI2_PHY_REG1);
665 set_field(®1, TCLK_TERM, CAL_CSI2_PHY_REG1_TCLK_TERM_MASK);
666 set_field(®1, 0xb8, CAL_CSI2_PHY_REG1_DPHY_HS_SYNC_PATTERN_MASK);
667 set_field(®1, TCLK_MISS, CAL_CSI2_PHY_REG1_CTRLCLK_DIV_FACTOR_MASK);
668 set_field(®1, TCLK_SETTLE, CAL_CSI2_PHY_REG1_TCLK_SETTLE_MASK);
670 phy_dbg(1, phy, "CSI2_%d_REG1 = 0x%08x\n", phy->instance, reg1);
671 reg_write(phy, CAL_CSI2_PHY_REG1, reg1);
674 static void cal_camerarx_init(struct cal_camerarx *phy,
675 const struct cal_fmt *fmt)
681 * 1. Configure D-PHY mode and enable required lanes
682 * 2. Reset complex IO - Wait for completion of reset
683 * Note if the external sensor is not sending byte clock,
684 * the reset will timeout
685 * 3 Program Stop States
686 * A. Program THS_TERM, THS_SETTLE, etc... Timings parameters
687 * in terms of DDR clock periods
688 * B. Enable stop state transition timeouts
689 * 4.Force FORCERXMODE
690 * D. Enable pull down using pad control
692 * F. Wait for power up completion
693 * G. Wait for all enabled lane to reach stop state
694 * H. Disable pull down using pad control
697 /* 1. Configure D-PHY mode and enable required lanes */
698 cal_camerarx_enable(phy);
700 /* 2. Reset complex IO - Do not wait for reset completion */
701 reg_write_field(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance),
702 CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_OPERATIONAL,
703 CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_MASK);
704 phy_dbg(3, phy, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x De-assert Complex IO Reset\n",
706 reg_read(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance)));
708 /* Dummy read to allow SCP reset to complete */
709 reg_read(phy, CAL_CSI2_PHY_REG0);
711 /* 3.A. Program Phy Timing Parameters */
712 cal_camerarx_config(phy, fmt);
714 /* 3.B. Program Stop States */
716 * The stop-state-counter is based on fclk cycles, and we always use
717 * the x16 and x4 settings, so stop-state-timeout =
718 * fclk-cycle * 16 * 4 * counter.
720 * Stop-state-timeout must be more than 100us as per CSI2 spec, so we
721 * calculate a timeout that's 100us (rounding up).
723 sscounter = DIV_ROUND_UP(clk_get_rate(phy->cal->fclk), 10000 * 16 * 4);
725 val = reg_read(phy->cal, CAL_CSI2_TIMING(phy->instance));
726 set_field(&val, 1, CAL_CSI2_TIMING_STOP_STATE_X16_IO1_MASK);
727 set_field(&val, 1, CAL_CSI2_TIMING_STOP_STATE_X4_IO1_MASK);
728 set_field(&val, sscounter, CAL_CSI2_TIMING_STOP_STATE_COUNTER_IO1_MASK);
729 reg_write(phy->cal, CAL_CSI2_TIMING(phy->instance), val);
730 phy_dbg(3, phy, "CAL_CSI2_TIMING(%d) = 0x%08x Stop States\n",
732 reg_read(phy->cal, CAL_CSI2_TIMING(phy->instance)));
734 /* 4. Force FORCERXMODE */
735 reg_write_field(phy->cal, CAL_CSI2_TIMING(phy->instance),
736 1, CAL_CSI2_TIMING_FORCE_RX_MODE_IO1_MASK);
737 phy_dbg(3, phy, "CAL_CSI2_TIMING(%d) = 0x%08x Force RXMODE\n",
739 reg_read(phy->cal, CAL_CSI2_TIMING(phy->instance)));
741 /* E. Power up the PHY using the complex IO */
742 cal_camerarx_power(phy, true);
745 static void cal_camerarx_wait_reset(struct cal_camerarx *phy)
747 unsigned long timeout;
749 timeout = jiffies + msecs_to_jiffies(750);
750 while (time_before(jiffies, timeout)) {
751 if (reg_read_field(phy->cal,
752 CAL_CSI2_COMPLEXIO_CFG(phy->instance),
753 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_MASK) ==
754 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_RESETCOMPLETED)
756 usleep_range(500, 5000);
759 if (reg_read_field(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance),
760 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_MASK) !=
761 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_RESETCOMPLETED)
762 phy_err(phy, "Timeout waiting for Complex IO reset done\n");
765 static void cal_camerarx_wait_stop_state(struct cal_camerarx *phy)
767 unsigned long timeout;
769 timeout = jiffies + msecs_to_jiffies(750);
770 while (time_before(jiffies, timeout)) {
771 if (reg_read_field(phy->cal,
772 CAL_CSI2_TIMING(phy->instance),
773 CAL_CSI2_TIMING_FORCE_RX_MODE_IO1_MASK) == 0)
775 usleep_range(500, 5000);
778 if (reg_read_field(phy->cal, CAL_CSI2_TIMING(phy->instance),
779 CAL_CSI2_TIMING_FORCE_RX_MODE_IO1_MASK) != 0)
780 phy_err(phy, "Timeout waiting for stop state\n");
783 static void cal_camerarx_wait_ready(struct cal_camerarx *phy)
786 * 2. Wait for completion of reset
787 * Note if the external sensor is not sending byte clock,
788 * the reset will timeout
789 * 4.Force FORCERXMODE
790 * G. Wait for all enabled lane to reach stop state
791 * H. Disable pull down using pad control
794 /* 2. Wait for reset completion */
795 cal_camerarx_wait_reset(phy);
797 /* 4. G. Wait for all enabled lane to reach stop state */
798 cal_camerarx_wait_stop_state(phy);
800 phy_dbg(1, phy, "CSI2_%d_REG1 = 0x%08x (Bit(31,28) should be set!)\n",
801 phy->instance, reg_read(phy, CAL_CSI2_PHY_REG1));
804 static void cal_camerarx_deinit(struct cal_camerarx *phy)
808 cal_camerarx_power(phy, false);
810 /* Assert Comple IO Reset */
811 reg_write_field(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance),
812 CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL,
813 CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_MASK);
815 /* Wait for power down completion */
816 for (i = 0; i < 10; i++) {
817 if (reg_read_field(phy->cal,
818 CAL_CSI2_COMPLEXIO_CFG(phy->instance),
819 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_MASK) ==
820 CAL_CSI2_COMPLEXIO_CFG_RESET_DONE_RESETONGOING)
822 usleep_range(1000, 1100);
824 phy_dbg(3, phy, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x Complex IO in Reset (%d) %s\n",
826 reg_read(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance)), i,
827 (i >= 10) ? "(timeout)" : "");
829 /* Disable the phy */
830 cal_camerarx_disable(phy);
833 static void cal_camerarx_lane_config(struct cal_camerarx *phy)
835 u32 val = reg_read(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance));
836 u32 lane_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POSITION_MASK;
837 u32 polarity_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POL_MASK;
838 struct v4l2_fwnode_bus_mipi_csi2 *mipi_csi2 =
839 &phy->endpoint.bus.mipi_csi2;
842 set_field(&val, mipi_csi2->clock_lane + 1, lane_mask);
843 set_field(&val, mipi_csi2->lane_polarities[0], polarity_mask);
844 for (lane = 0; lane < mipi_csi2->num_data_lanes; lane++) {
846 * Every lane are one nibble apart starting with the
847 * clock followed by the data lanes so shift masks by 4.
851 set_field(&val, mipi_csi2->data_lanes[lane] + 1, lane_mask);
852 set_field(&val, mipi_csi2->lane_polarities[lane + 1],
856 reg_write(phy->cal, CAL_CSI2_COMPLEXIO_CFG(phy->instance), val);
857 phy_dbg(3, phy, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x\n",
861 static void cal_camerarx_ppi_enable(struct cal_camerarx *phy)
863 reg_write(phy->cal, CAL_CSI2_PPI_CTRL(phy->instance), BIT(3));
864 reg_write_field(phy->cal, CAL_CSI2_PPI_CTRL(phy->instance),
865 1, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
868 static void cal_camerarx_ppi_disable(struct cal_camerarx *phy)
870 reg_write_field(phy->cal, CAL_CSI2_PPI_CTRL(phy->instance),
871 0, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
874 static int cal_camerarx_get_external_info(struct cal_camerarx *phy)
876 struct v4l2_ctrl *ctrl;
881 ctrl = v4l2_ctrl_find(phy->sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
883 phy_err(phy, "no pixel rate control in subdev: %s\n",
888 phy->external_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
889 phy_dbg(3, phy, "sensor Pixel Rate: %u\n", phy->external_rate);
894 static int cal_camerarx_regmap_init(struct cal_dev *cal,
895 struct cal_camerarx *phy)
897 const struct cal_camerarx_data *phy_data;
903 phy_data = &cal->data->camerarx[phy->instance];
905 for (i = 0; i < F_MAX_FIELDS; i++) {
906 struct reg_field field = {
907 .reg = cal->syscon_camerrx_offset,
908 .lsb = phy_data->fields[i].lsb,
909 .msb = phy_data->fields[i].msb,
913 * Here we update the reg offset with the
916 phy->fields[i] = devm_regmap_field_alloc(&cal->pdev->dev,
919 if (IS_ERR(phy->fields[i])) {
920 cal_err(cal, "Unable to allocate regmap fields\n");
921 return PTR_ERR(phy->fields[i]);
928 static struct cal_camerarx *cal_camerarx_create(struct cal_dev *cal,
929 unsigned int instance)
931 struct platform_device *pdev = cal->pdev;
932 struct cal_camerarx *phy;
935 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
937 return ERR_PTR(-ENOMEM);
940 phy->instance = instance;
941 phy->external_rate = 192000000;
943 phy->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
947 phy->base = devm_ioremap_resource(&pdev->dev, phy->res);
948 if (IS_ERR(phy->base)) {
949 cal_err(cal, "failed to ioremap\n");
950 return ERR_CAST(phy->base);
953 cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
954 phy->res->name, &phy->res->start, &phy->res->end);
956 ret = cal_camerarx_regmap_init(cal, phy);
963 static int cal_camerarx_init_regmap(struct cal_dev *cal)
965 struct device_node *np = cal->pdev->dev.of_node;
966 struct regmap_config config = { };
967 struct regmap *syscon;
968 struct resource *res;
972 syscon = syscon_regmap_lookup_by_phandle_args(np, "ti,camerrx-control",
974 if (!IS_ERR(syscon)) {
975 cal->syscon_camerrx = syscon;
976 cal->syscon_camerrx_offset = offset;
980 dev_warn(&cal->pdev->dev, "failed to get ti,camerrx-control: %ld\n",
984 * Backward DTS compatibility. If syscon entry is not present then
985 * check if the camerrx_control resource is present.
987 res = platform_get_resource_byname(cal->pdev, IORESOURCE_MEM,
989 base = devm_ioremap_resource(&cal->pdev->dev, res);
991 cal_err(cal, "failed to ioremap camerrx_control\n");
992 return PTR_ERR(base);
995 cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
996 res->name, &res->start, &res->end);
998 config.reg_bits = 32;
999 config.reg_stride = 4;
1000 config.val_bits = 32;
1001 config.max_register = resource_size(res) - 4;
1003 syscon = regmap_init_mmio(NULL, base, &config);
1004 if (IS_ERR(syscon)) {
1005 pr_err("regmap init failed\n");
1006 return PTR_ERR(syscon);
1010 * In this case the base already point to the direct CM register so no
1011 * need for an offset.
1013 cal->syscon_camerrx = syscon;
1014 cal->syscon_camerrx_offset = 0;
1019 /* ------------------------------------------------------------------
1020 * Context Management
1021 * ------------------------------------------------------------------
1024 static void cal_ctx_csi2_config(struct cal_ctx *ctx)
1028 val = reg_read(ctx->cal, CAL_CSI2_CTX0(ctx->index));
1029 set_field(&val, ctx->cport, CAL_CSI2_CTX_CPORT_MASK);
1031 * DT type: MIPI CSI-2 Specs
1032 * 0x1: All - DT filter is disabled
1033 * 0x24: RGB888 1 pixel = 3 bytes
1034 * 0x2B: RAW10 4 pixels = 5 bytes
1035 * 0x2A: RAW8 1 pixel = 1 byte
1036 * 0x1E: YUV422 2 pixels = 4 bytes
1038 set_field(&val, 0x1, CAL_CSI2_CTX_DT_MASK);
1039 /* Virtual Channel from the CSI2 sensor usually 0! */
1040 set_field(&val, ctx->virtual_channel, CAL_CSI2_CTX_VC_MASK);
1041 set_field(&val, ctx->v_fmt.fmt.pix.height, CAL_CSI2_CTX_LINES_MASK);
1042 set_field(&val, CAL_CSI2_CTX_ATT_PIX, CAL_CSI2_CTX_ATT_MASK);
1043 set_field(&val, CAL_CSI2_CTX_PACK_MODE_LINE,
1044 CAL_CSI2_CTX_PACK_MODE_MASK);
1045 reg_write(ctx->cal, CAL_CSI2_CTX0(ctx->index), val);
1046 ctx_dbg(3, ctx, "CAL_CSI2_CTX0(%d) = 0x%08x\n", ctx->index,
1047 reg_read(ctx->cal, CAL_CSI2_CTX0(ctx->index)));
1050 static void cal_ctx_pix_proc_config(struct cal_ctx *ctx)
1052 u32 val, extract, pack;
1054 switch (ctx->fmt->bpp) {
1056 extract = CAL_PIX_PROC_EXTRACT_B8;
1057 pack = CAL_PIX_PROC_PACK_B8;
1060 extract = CAL_PIX_PROC_EXTRACT_B10_MIPI;
1061 pack = CAL_PIX_PROC_PACK_B16;
1064 extract = CAL_PIX_PROC_EXTRACT_B12_MIPI;
1065 pack = CAL_PIX_PROC_PACK_B16;
1068 extract = CAL_PIX_PROC_EXTRACT_B16_LE;
1069 pack = CAL_PIX_PROC_PACK_B16;
1073 * If you see this warning then it means that you added
1074 * some new entry in the cal_formats[] array with a different
1075 * bit per pixel values then the one supported below.
1076 * Either add support for the new bpp value below or adjust
1077 * the new entry to use one of the value below.
1079 * Instead of failing here just use 8 bpp as a default.
1081 dev_warn_once(&ctx->cal->pdev->dev,
1082 "%s:%d:%s: bpp:%d unsupported! Overwritten with 8.\n",
1083 __FILE__, __LINE__, __func__, ctx->fmt->bpp);
1084 extract = CAL_PIX_PROC_EXTRACT_B8;
1085 pack = CAL_PIX_PROC_PACK_B8;
1089 val = reg_read(ctx->cal, CAL_PIX_PROC(ctx->index));
1090 set_field(&val, extract, CAL_PIX_PROC_EXTRACT_MASK);
1091 set_field(&val, CAL_PIX_PROC_DPCMD_BYPASS, CAL_PIX_PROC_DPCMD_MASK);
1092 set_field(&val, CAL_PIX_PROC_DPCME_BYPASS, CAL_PIX_PROC_DPCME_MASK);
1093 set_field(&val, pack, CAL_PIX_PROC_PACK_MASK);
1094 set_field(&val, ctx->cport, CAL_PIX_PROC_CPORT_MASK);
1095 set_field(&val, 1, CAL_PIX_PROC_EN_MASK);
1096 reg_write(ctx->cal, CAL_PIX_PROC(ctx->index), val);
1097 ctx_dbg(3, ctx, "CAL_PIX_PROC(%d) = 0x%08x\n", ctx->index,
1098 reg_read(ctx->cal, CAL_PIX_PROC(ctx->index)));
1101 static void cal_ctx_wr_dma_config(struct cal_ctx *ctx,
1102 unsigned int width, unsigned int height)
1106 val = reg_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->index));
1107 set_field(&val, ctx->cport, CAL_WR_DMA_CTRL_CPORT_MASK);
1108 set_field(&val, height, CAL_WR_DMA_CTRL_YSIZE_MASK);
1109 set_field(&val, CAL_WR_DMA_CTRL_DTAG_PIX_DAT,
1110 CAL_WR_DMA_CTRL_DTAG_MASK);
1111 set_field(&val, CAL_WR_DMA_CTRL_MODE_CONST,
1112 CAL_WR_DMA_CTRL_MODE_MASK);
1113 set_field(&val, CAL_WR_DMA_CTRL_PATTERN_LINEAR,
1114 CAL_WR_DMA_CTRL_PATTERN_MASK);
1115 set_field(&val, 1, CAL_WR_DMA_CTRL_STALL_RD_MASK);
1116 reg_write(ctx->cal, CAL_WR_DMA_CTRL(ctx->index), val);
1117 ctx_dbg(3, ctx, "CAL_WR_DMA_CTRL(%d) = 0x%08x\n", ctx->index,
1118 reg_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->index)));
1121 * width/16 not sure but giving it a whirl.
1122 * zero does not work right
1124 reg_write_field(ctx->cal,
1125 CAL_WR_DMA_OFST(ctx->index),
1127 CAL_WR_DMA_OFST_MASK);
1128 ctx_dbg(3, ctx, "CAL_WR_DMA_OFST(%d) = 0x%08x\n", ctx->index,
1129 reg_read(ctx->cal, CAL_WR_DMA_OFST(ctx->index)));
1131 val = reg_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index));
1132 /* 64 bit word means no skipping */
1133 set_field(&val, 0, CAL_WR_DMA_XSIZE_XSKIP_MASK);
1135 * (width*8)/64 this should be size of an entire line
1136 * in 64bit word but 0 means all data until the end
1137 * is detected automagically
1139 set_field(&val, (width / 8), CAL_WR_DMA_XSIZE_MASK);
1140 reg_write(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index), val);
1141 ctx_dbg(3, ctx, "CAL_WR_DMA_XSIZE(%d) = 0x%08x\n", ctx->index,
1142 reg_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index)));
1144 val = reg_read(ctx->cal, CAL_CTRL);
1145 set_field(&val, CAL_CTRL_BURSTSIZE_BURST128, CAL_CTRL_BURSTSIZE_MASK);
1146 set_field(&val, 0xF, CAL_CTRL_TAGCNT_MASK);
1147 set_field(&val, CAL_CTRL_POSTED_WRITES_NONPOSTED,
1148 CAL_CTRL_POSTED_WRITES_MASK);
1149 set_field(&val, 0xFF, CAL_CTRL_MFLAGL_MASK);
1150 set_field(&val, 0xFF, CAL_CTRL_MFLAGH_MASK);
1151 reg_write(ctx->cal, CAL_CTRL, val);
1152 ctx_dbg(3, ctx, "CAL_CTRL = 0x%08x\n", reg_read(ctx->cal, CAL_CTRL));
1155 static void cal_ctx_wr_dma_addr(struct cal_ctx *ctx, unsigned int dmaaddr)
1157 reg_write(ctx->cal, CAL_WR_DMA_ADDR(ctx->index), dmaaddr);
1160 /* ------------------------------------------------------------------
1162 * ------------------------------------------------------------------
1165 static inline void cal_schedule_next_buffer(struct cal_ctx *ctx)
1167 struct cal_dmaqueue *dma_q = &ctx->vidq;
1168 struct cal_buffer *buf;
1171 buf = list_entry(dma_q->active.next, struct cal_buffer, list);
1172 ctx->next_frm = buf;
1173 list_del(&buf->list);
1175 addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
1176 cal_ctx_wr_dma_addr(ctx, addr);
1179 static inline void cal_process_buffer_complete(struct cal_ctx *ctx)
1181 ctx->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1182 ctx->cur_frm->vb.field = ctx->m_fmt.field;
1183 ctx->cur_frm->vb.sequence = ctx->sequence++;
1185 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1186 ctx->cur_frm = ctx->next_frm;
1189 #define isvcirqset(irq, vc, ff) (irq & \
1190 (CAL_CSI2_VC_IRQENABLE_ ##ff ##_IRQ_##vc ##_MASK))
1192 #define isportirqset(irq, port) (irq & CAL_HL_IRQ_MASK(port))
1194 static irqreturn_t cal_irq(int irq_cal, void *data)
1196 struct cal_dev *cal = data;
1197 struct cal_ctx *ctx;
1198 struct cal_dmaqueue *dma_q;
1201 status = reg_read(cal, CAL_HL_IRQSTATUS(0));
1205 reg_write(cal, CAL_HL_IRQSTATUS(0), status);
1207 if (status & CAL_HL_IRQ_OCPO_ERR_MASK)
1208 dev_err_ratelimited(&cal->pdev->dev, "OCPO ERROR\n");
1210 for (i = 0; i < 2; ++i) {
1211 if (status & CAL_HL_IRQ_CIO_MASK(i)) {
1212 u32 cio_stat = reg_read(cal,
1213 CAL_CSI2_COMPLEXIO_IRQSTATUS(i));
1215 dev_err_ratelimited(&cal->pdev->dev,
1216 "CIO%u error: %#08x\n", i, cio_stat);
1218 reg_write(cal, CAL_CSI2_COMPLEXIO_IRQSTATUS(i),
1224 /* Check which DMA just finished */
1225 status = reg_read(cal, CAL_HL_IRQSTATUS(1));
1229 /* Clear Interrupt status */
1230 reg_write(cal, CAL_HL_IRQSTATUS(1), status);
1232 for (i = 0; i < 2; ++i) {
1233 if (isportirqset(status, i)) {
1236 spin_lock(&ctx->slock);
1237 ctx->dma_act = false;
1239 if (ctx->cur_frm != ctx->next_frm)
1240 cal_process_buffer_complete(ctx);
1242 spin_unlock(&ctx->slock);
1247 /* Check which DMA just started */
1248 status = reg_read(cal, CAL_HL_IRQSTATUS(2));
1252 /* Clear Interrupt status */
1253 reg_write(cal, CAL_HL_IRQSTATUS(2), status);
1255 for (i = 0; i < 2; ++i) {
1256 if (isportirqset(status, i)) {
1260 spin_lock(&ctx->slock);
1261 ctx->dma_act = true;
1262 if (!list_empty(&dma_q->active) &&
1263 ctx->cur_frm == ctx->next_frm)
1264 cal_schedule_next_buffer(ctx);
1265 spin_unlock(&ctx->slock);
1273 /* ------------------------------------------------------------------
1275 * ------------------------------------------------------------------
1278 static const struct cal_fmt *find_format_by_pix(struct cal_ctx *ctx,
1281 const struct cal_fmt *fmt;
1284 for (k = 0; k < ctx->num_active_fmt; k++) {
1285 fmt = ctx->active_fmt[k];
1286 if (fmt->fourcc == pixelformat)
1293 static const struct cal_fmt *find_format_by_code(struct cal_ctx *ctx,
1296 const struct cal_fmt *fmt;
1299 for (k = 0; k < ctx->num_active_fmt; k++) {
1300 fmt = ctx->active_fmt[k];
1301 if (fmt->code == code)
1308 static int cal_querycap(struct file *file, void *priv,
1309 struct v4l2_capability *cap)
1311 struct cal_ctx *ctx = video_drvdata(file);
1313 strscpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
1314 strscpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));
1316 snprintf(cap->bus_info, sizeof(cap->bus_info),
1317 "platform:%s", dev_name(&ctx->cal->pdev->dev));
1321 static int cal_enum_fmt_vid_cap(struct file *file, void *priv,
1322 struct v4l2_fmtdesc *f)
1324 struct cal_ctx *ctx = video_drvdata(file);
1325 const struct cal_fmt *fmt;
1327 if (f->index >= ctx->num_active_fmt)
1330 fmt = ctx->active_fmt[f->index];
1332 f->pixelformat = fmt->fourcc;
1333 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1337 static int __subdev_get_format(struct cal_ctx *ctx,
1338 struct v4l2_mbus_framefmt *fmt)
1340 struct v4l2_subdev_format sd_fmt;
1341 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1344 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1347 ret = v4l2_subdev_call(ctx->phy->sensor, pad, get_fmt, NULL, &sd_fmt);
1353 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
1354 fmt->width, fmt->height, fmt->code);
1359 static int __subdev_set_format(struct cal_ctx *ctx,
1360 struct v4l2_mbus_framefmt *fmt)
1362 struct v4l2_subdev_format sd_fmt;
1363 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1366 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1370 ret = v4l2_subdev_call(ctx->phy->sensor, pad, set_fmt, NULL, &sd_fmt);
1374 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
1375 fmt->width, fmt->height, fmt->code);
1380 static int cal_calc_format_size(struct cal_ctx *ctx,
1381 const struct cal_fmt *fmt,
1382 struct v4l2_format *f)
1387 ctx_dbg(3, ctx, "No cal_fmt provided!\n");
1392 * Maximum width is bound by the DMA max width in bytes.
1393 * We need to recalculate the actual maxi width depending on the
1394 * number of bytes per pixels required.
1396 max_width = MAX_WIDTH_BYTES / (ALIGN(fmt->bpp, 8) >> 3);
1397 v4l_bound_align_image(&f->fmt.pix.width, 48, max_width, 2,
1398 &f->fmt.pix.height, 32, MAX_HEIGHT_LINES, 0, 0);
1400 bpl = (f->fmt.pix.width * ALIGN(fmt->bpp, 8)) >> 3;
1401 f->fmt.pix.bytesperline = ALIGN(bpl, 16);
1403 f->fmt.pix.sizeimage = f->fmt.pix.height *
1404 f->fmt.pix.bytesperline;
1406 ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
1407 __func__, fourcc_to_str(f->fmt.pix.pixelformat),
1408 f->fmt.pix.width, f->fmt.pix.height,
1409 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1414 static int cal_g_fmt_vid_cap(struct file *file, void *priv,
1415 struct v4l2_format *f)
1417 struct cal_ctx *ctx = video_drvdata(file);
1424 static int cal_try_fmt_vid_cap(struct file *file, void *priv,
1425 struct v4l2_format *f)
1427 struct cal_ctx *ctx = video_drvdata(file);
1428 const struct cal_fmt *fmt;
1429 struct v4l2_subdev_frame_size_enum fse;
1432 fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
1434 ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
1435 f->fmt.pix.pixelformat);
1437 /* Just get the first one enumerated */
1438 fmt = ctx->active_fmt[0];
1439 f->fmt.pix.pixelformat = fmt->fourcc;
1442 f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;
1444 /* check for/find a valid width/height */
1448 fse.code = fmt->code;
1449 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1450 for (fse.index = 0; ; fse.index++) {
1451 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size,
1456 if ((f->fmt.pix.width == fse.max_width) &&
1457 (f->fmt.pix.height == fse.max_height)) {
1460 } else if ((f->fmt.pix.width >= fse.min_width) &&
1461 (f->fmt.pix.width <= fse.max_width) &&
1462 (f->fmt.pix.height >= fse.min_height) &&
1463 (f->fmt.pix.height <= fse.max_height)) {
1470 /* use existing values as default */
1471 f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
1472 f->fmt.pix.height = ctx->v_fmt.fmt.pix.height;
1476 * Use current colorspace for now, it will get
1477 * updated properly during s_fmt
1479 f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
1480 return cal_calc_format_size(ctx, fmt, f);
1483 static int cal_s_fmt_vid_cap(struct file *file, void *priv,
1484 struct v4l2_format *f)
1486 struct cal_ctx *ctx = video_drvdata(file);
1487 struct vb2_queue *q = &ctx->vb_vidq;
1488 const struct cal_fmt *fmt;
1489 struct v4l2_mbus_framefmt mbus_fmt;
1492 if (vb2_is_busy(q)) {
1493 ctx_dbg(3, ctx, "%s device busy\n", __func__);
1497 ret = cal_try_fmt_vid_cap(file, priv, f);
1501 fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
1503 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1505 ret = __subdev_set_format(ctx, &mbus_fmt);
1509 /* Just double check nothing has gone wrong */
1510 if (mbus_fmt.code != fmt->code) {
1512 "%s subdev changed format on us, this should not happen\n",
1517 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
1518 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1519 ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1520 cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
1522 ctx->m_fmt = mbus_fmt;
1528 static int cal_enum_framesizes(struct file *file, void *fh,
1529 struct v4l2_frmsizeenum *fsize)
1531 struct cal_ctx *ctx = video_drvdata(file);
1532 const struct cal_fmt *fmt;
1533 struct v4l2_subdev_frame_size_enum fse;
1536 /* check for valid format */
1537 fmt = find_format_by_pix(ctx, fsize->pixel_format);
1539 ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
1540 fsize->pixel_format);
1544 fse.index = fsize->index;
1546 fse.code = fmt->code;
1547 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1549 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size, NULL,
1554 ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1555 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1556 fse.min_height, fse.max_height);
1558 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1559 fsize->discrete.width = fse.max_width;
1560 fsize->discrete.height = fse.max_height;
1565 static int cal_enum_input(struct file *file, void *priv,
1566 struct v4l2_input *inp)
1571 inp->type = V4L2_INPUT_TYPE_CAMERA;
1572 sprintf(inp->name, "Camera %u", inp->index);
1576 static int cal_g_input(struct file *file, void *priv, unsigned int *i)
1582 static int cal_s_input(struct file *file, void *priv, unsigned int i)
1584 return i > 0 ? -EINVAL : 0;
1587 /* timeperframe is arbitrary and continuous */
1588 static int cal_enum_frameintervals(struct file *file, void *priv,
1589 struct v4l2_frmivalenum *fival)
1591 struct cal_ctx *ctx = video_drvdata(file);
1592 const struct cal_fmt *fmt;
1593 struct v4l2_subdev_frame_interval_enum fie = {
1594 .index = fival->index,
1595 .width = fival->width,
1596 .height = fival->height,
1597 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1601 fmt = find_format_by_pix(ctx, fival->pixel_format);
1605 fie.code = fmt->code;
1606 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_interval,
1610 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1611 fival->discrete = fie.interval;
1616 static const struct v4l2_file_operations cal_fops = {
1617 .owner = THIS_MODULE,
1618 .open = v4l2_fh_open,
1619 .release = vb2_fop_release,
1620 .read = vb2_fop_read,
1621 .poll = vb2_fop_poll,
1622 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1623 .mmap = vb2_fop_mmap,
1626 static const struct v4l2_ioctl_ops cal_ioctl_ops = {
1627 .vidioc_querycap = cal_querycap,
1628 .vidioc_enum_fmt_vid_cap = cal_enum_fmt_vid_cap,
1629 .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
1630 .vidioc_try_fmt_vid_cap = cal_try_fmt_vid_cap,
1631 .vidioc_s_fmt_vid_cap = cal_s_fmt_vid_cap,
1632 .vidioc_enum_framesizes = cal_enum_framesizes,
1633 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1634 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1635 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1636 .vidioc_querybuf = vb2_ioctl_querybuf,
1637 .vidioc_qbuf = vb2_ioctl_qbuf,
1638 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1639 .vidioc_expbuf = vb2_ioctl_expbuf,
1640 .vidioc_enum_input = cal_enum_input,
1641 .vidioc_g_input = cal_g_input,
1642 .vidioc_s_input = cal_s_input,
1643 .vidioc_enum_frameintervals = cal_enum_frameintervals,
1644 .vidioc_streamon = vb2_ioctl_streamon,
1645 .vidioc_streamoff = vb2_ioctl_streamoff,
1646 .vidioc_log_status = v4l2_ctrl_log_status,
1647 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1648 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1651 /* ------------------------------------------------------------------
1652 * videobuf2 Operations
1653 * ------------------------------------------------------------------
1656 static int cal_queue_setup(struct vb2_queue *vq,
1657 unsigned int *nbuffers, unsigned int *nplanes,
1658 unsigned int sizes[], struct device *alloc_devs[])
1660 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
1661 unsigned size = ctx->v_fmt.fmt.pix.sizeimage;
1663 if (vq->num_buffers + *nbuffers < 3)
1664 *nbuffers = 3 - vq->num_buffers;
1667 if (sizes[0] < size)
1675 ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);
1680 static int cal_buffer_prepare(struct vb2_buffer *vb)
1682 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1683 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
1687 if (WARN_ON(!ctx->fmt))
1690 size = ctx->v_fmt.fmt.pix.sizeimage;
1691 if (vb2_plane_size(vb, 0) < size) {
1693 "data will not fit into plane (%lu < %lu)\n",
1694 vb2_plane_size(vb, 0), size);
1698 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1702 static void cal_buffer_queue(struct vb2_buffer *vb)
1704 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1705 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
1707 struct cal_dmaqueue *vidq = &ctx->vidq;
1708 unsigned long flags;
1710 /* recheck locking */
1711 spin_lock_irqsave(&ctx->slock, flags);
1712 list_add_tail(&buf->list, &vidq->active);
1713 spin_unlock_irqrestore(&ctx->slock, flags);
1716 static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
1718 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
1719 struct cal_dmaqueue *dma_q = &ctx->vidq;
1720 struct cal_buffer *buf, *tmp;
1722 unsigned long flags;
1725 spin_lock_irqsave(&ctx->slock, flags);
1726 if (list_empty(&dma_q->active)) {
1727 spin_unlock_irqrestore(&ctx->slock, flags);
1728 ctx_dbg(3, ctx, "buffer queue is empty\n");
1732 buf = list_entry(dma_q->active.next, struct cal_buffer, list);
1734 ctx->next_frm = buf;
1735 list_del(&buf->list);
1736 spin_unlock_irqrestore(&ctx->slock, flags);
1738 addr = vb2_dma_contig_plane_dma_addr(&ctx->cur_frm->vb.vb2_buf, 0);
1741 ret = cal_camerarx_get_external_info(ctx->phy);
1745 ret = v4l2_subdev_call(ctx->phy->sensor, core, s_power, 1);
1746 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1747 ctx_err(ctx, "power on failed in subdev\n");
1751 pm_runtime_get_sync(&ctx->cal->pdev->dev);
1753 cal_ctx_csi2_config(ctx);
1754 cal_ctx_pix_proc_config(ctx);
1755 cal_ctx_wr_dma_config(ctx, ctx->v_fmt.fmt.pix.bytesperline,
1756 ctx->v_fmt.fmt.pix.height);
1757 cal_camerarx_lane_config(ctx->phy);
1759 cal_camerarx_enable_irqs(ctx->phy);
1760 cal_camerarx_init(ctx->phy, ctx->fmt);
1762 ret = v4l2_subdev_call(ctx->phy->sensor, video, s_stream, 1);
1764 v4l2_subdev_call(ctx->phy->sensor, core, s_power, 0);
1765 ctx_err(ctx, "stream on failed in subdev\n");
1766 pm_runtime_put_sync(&ctx->cal->pdev->dev);
1770 cal_camerarx_wait_ready(ctx->phy);
1771 cal_ctx_wr_dma_addr(ctx, addr);
1772 cal_camerarx_ppi_enable(ctx->phy);
1775 cal_quickdump_regs(ctx->cal);
1780 spin_lock_irqsave(&ctx->slock, flags);
1781 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1782 ctx->cur_frm = NULL;
1783 ctx->next_frm = NULL;
1784 list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1785 list_del(&buf->list);
1786 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1788 spin_unlock_irqrestore(&ctx->slock, flags);
1792 static void cal_stop_streaming(struct vb2_queue *vq)
1794 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
1795 struct cal_dmaqueue *dma_q = &ctx->vidq;
1796 struct cal_buffer *buf, *tmp;
1797 unsigned long timeout;
1798 unsigned long flags;
1802 cal_camerarx_ppi_disable(ctx->phy);
1804 /* wait for stream and dma to finish */
1806 timeout = jiffies + msecs_to_jiffies(500);
1807 while (dma_act && time_before(jiffies, timeout)) {
1810 spin_lock_irqsave(&ctx->slock, flags);
1811 dma_act = ctx->dma_act;
1812 spin_unlock_irqrestore(&ctx->slock, flags);
1816 ctx_err(ctx, "failed to disable dma cleanly\n");
1818 cal_camerarx_disable_irqs(ctx->phy);
1819 cal_camerarx_deinit(ctx->phy);
1821 if (v4l2_subdev_call(ctx->phy->sensor, video, s_stream, 0))
1822 ctx_err(ctx, "stream off failed in subdev\n");
1824 ret = v4l2_subdev_call(ctx->phy->sensor, core, s_power, 0);
1825 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
1826 ctx_err(ctx, "power off failed in subdev\n");
1828 /* Release all active buffers */
1829 spin_lock_irqsave(&ctx->slock, flags);
1830 list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1831 list_del(&buf->list);
1832 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1835 if (ctx->cur_frm == ctx->next_frm) {
1836 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1838 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1839 vb2_buffer_done(&ctx->next_frm->vb.vb2_buf,
1840 VB2_BUF_STATE_ERROR);
1842 ctx->cur_frm = NULL;
1843 ctx->next_frm = NULL;
1844 spin_unlock_irqrestore(&ctx->slock, flags);
1846 pm_runtime_put_sync(&ctx->cal->pdev->dev);
1849 static const struct vb2_ops cal_video_qops = {
1850 .queue_setup = cal_queue_setup,
1851 .buf_prepare = cal_buffer_prepare,
1852 .buf_queue = cal_buffer_queue,
1853 .start_streaming = cal_start_streaming,
1854 .stop_streaming = cal_stop_streaming,
1855 .wait_prepare = vb2_ops_wait_prepare,
1856 .wait_finish = vb2_ops_wait_finish,
1859 /* ------------------------------------------------------------------
1860 * Initialization and module stuff
1861 * ------------------------------------------------------------------
1864 static const struct video_device cal_videodev = {
1865 .name = CAL_MODULE_NAME,
1867 .ioctl_ops = &cal_ioctl_ops,
1869 .release = video_device_release_empty,
1870 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1874 static int cal_complete_ctx(struct cal_ctx *ctx)
1876 struct video_device *vfd;
1877 struct vb2_queue *q;
1880 /* initialize locks */
1881 spin_lock_init(&ctx->slock);
1882 mutex_init(&ctx->mutex);
1884 /* initialize queue */
1886 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1887 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1889 q->buf_struct_size = sizeof(struct cal_buffer);
1890 q->ops = &cal_video_qops;
1891 q->mem_ops = &vb2_dma_contig_memops;
1892 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1893 q->lock = &ctx->mutex;
1894 q->min_buffers_needed = 3;
1895 q->dev = &ctx->cal->pdev->dev;
1897 ret = vb2_queue_init(q);
1901 /* init video dma queues */
1902 INIT_LIST_HEAD(&ctx->vidq.active);
1905 *vfd = cal_videodev;
1906 vfd->v4l2_dev = &ctx->v4l2_dev;
1910 * Provide a mutex to v4l2 core. It will be used to protect
1911 * all fops and v4l2 ioctls.
1913 vfd->lock = &ctx->mutex;
1914 video_set_drvdata(vfd, ctx);
1916 ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr);
1920 ctx_info(ctx, "V4L2 device registered as %s\n",
1921 video_device_node_name(vfd));
1926 static int cal_async_bound(struct v4l2_async_notifier *notifier,
1927 struct v4l2_subdev *subdev,
1928 struct v4l2_async_subdev *asd)
1930 struct cal_ctx *ctx = notifier_to_ctx(notifier);
1931 struct v4l2_subdev_mbus_code_enum mbus_code;
1932 unsigned int i, j, k;
1935 if (ctx->phy->sensor) {
1936 ctx_info(ctx, "Rejecting subdev %s (Already set!!)",
1941 ctx->phy->sensor = subdev;
1942 ctx_dbg(1, ctx, "Using sensor %s for capture\n", subdev->name);
1944 /* Enumerate sub device formats and enable all matching local formats */
1945 ctx->num_active_fmt = 0;
1946 for (j = 0, i = 0; ret != -EINVAL; ++j) {
1948 memset(&mbus_code, 0, sizeof(mbus_code));
1949 mbus_code.index = j;
1950 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1951 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1957 "subdev %s: code: %04x idx: %u\n",
1958 subdev->name, mbus_code.code, j);
1960 for (k = 0; k < ARRAY_SIZE(cal_formats); k++) {
1961 const struct cal_fmt *fmt = &cal_formats[k];
1963 if (mbus_code.code == fmt->code) {
1964 ctx->active_fmt[i] = fmt;
1966 "matched fourcc: %s: code: %04x idx: %u\n",
1967 fourcc_to_str(fmt->fourcc),
1969 ctx->num_active_fmt = ++i;
1975 ctx_err(ctx, "No suitable format reported by subdev %s\n",
1980 cal_complete_ctx(ctx);
1985 static int cal_async_complete(struct v4l2_async_notifier *notifier)
1987 struct cal_ctx *ctx = notifier_to_ctx(notifier);
1988 const struct cal_fmt *fmt;
1989 struct v4l2_mbus_framefmt mbus_fmt;
1992 ret = __subdev_get_format(ctx, &mbus_fmt);
1996 fmt = find_format_by_code(ctx, mbus_fmt.code);
1998 ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
2003 /* Save current subdev format */
2004 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
2005 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2006 ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
2007 cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
2009 ctx->m_fmt = mbus_fmt;
2014 static const struct v4l2_async_notifier_operations cal_async_ops = {
2015 .bound = cal_async_bound,
2016 .complete = cal_async_complete,
2019 static struct device_node *
2020 of_get_next_port(const struct device_node *parent,
2021 struct device_node *prev)
2023 struct device_node *port;
2029 struct device_node *ports;
2031 * It's the first call, we have to find a port subnode
2032 * within this node or within an optional 'ports' node.
2034 ports = of_get_child_by_name(parent, "ports");
2038 port = of_get_child_by_name(parent, "port");
2040 /* release the 'ports' node */
2043 struct device_node *ports;
2045 ports = of_get_parent(prev);
2050 port = of_get_next_child(ports, prev);
2056 } while (!of_node_name_eq(port, "port"));
2063 static struct device_node *
2064 of_get_next_endpoint(const struct device_node *parent,
2065 struct device_node *prev)
2067 struct device_node *ep;
2073 ep = of_get_next_child(parent, prev);
2077 } while (!of_node_name_eq(ep, "endpoint"));
2082 static int of_cal_create_instance(struct cal_ctx *ctx, int inst)
2084 struct platform_device *pdev = ctx->cal->pdev;
2085 struct device_node *ep_node, *port, *sensor_node, *parent;
2086 struct v4l2_fwnode_endpoint *endpoint;
2087 struct v4l2_async_subdev *asd;
2089 int ret, index, lane;
2090 bool found_port = false;
2092 parent = pdev->dev.of_node;
2094 endpoint = &ctx->phy->endpoint;
2101 ctx_dbg(3, ctx, "Scanning Port node for csi2 port: %d\n", inst);
2102 for (index = 0; index < CAL_NUM_CSI2_PORTS; index++) {
2103 port = of_get_next_port(parent, port);
2105 ctx_dbg(1, ctx, "No port node found for csi2 port:%d\n",
2110 /* Match the slice number with <REG> */
2111 of_property_read_u32(port, "reg", ®val);
2112 ctx_dbg(3, ctx, "port:%d inst:%d <reg>:%d\n",
2113 index, inst, regval);
2114 if ((regval == inst) && (index == inst)) {
2121 ctx_dbg(1, ctx, "No port node matches csi2 port:%d\n",
2126 ctx_dbg(3, ctx, "Scanning sub-device for csi2 port: %d\n",
2129 ep_node = of_get_next_endpoint(port, ep_node);
2131 ctx_dbg(3, ctx, "can't get next endpoint\n");
2135 sensor_node = of_graph_get_remote_port_parent(ep_node);
2137 ctx_dbg(3, ctx, "can't get remote parent\n");
2141 v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), endpoint);
2143 if (endpoint->bus_type != V4L2_MBUS_CSI2_DPHY) {
2144 ctx_err(ctx, "Port:%d sub-device %pOFn is not a CSI2 device\n",
2149 /* Store Virtual Channel number */
2150 ctx->virtual_channel = endpoint->base.id;
2152 ctx_dbg(3, ctx, "Port:%d v4l2-endpoint: CSI2\n", inst);
2153 ctx_dbg(3, ctx, "Virtual Channel=%d\n", ctx->virtual_channel);
2154 ctx_dbg(3, ctx, "flags=0x%08x\n", endpoint->bus.mipi_csi2.flags);
2155 ctx_dbg(3, ctx, "clock_lane=%d\n", endpoint->bus.mipi_csi2.clock_lane);
2156 ctx_dbg(3, ctx, "num_data_lanes=%d\n",
2157 endpoint->bus.mipi_csi2.num_data_lanes);
2158 ctx_dbg(3, ctx, "data_lanes= <\n");
2159 for (lane = 0; lane < endpoint->bus.mipi_csi2.num_data_lanes; lane++)
2160 ctx_dbg(3, ctx, "\t%d\n",
2161 endpoint->bus.mipi_csi2.data_lanes[lane]);
2162 ctx_dbg(3, ctx, "\t>\n");
2164 ctx_dbg(1, ctx, "Port: %d found sub-device %pOFn\n",
2167 v4l2_async_notifier_init(&ctx->notifier);
2169 asd = kzalloc(sizeof(*asd), GFP_KERNEL);
2173 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
2174 asd->match.fwnode = of_fwnode_handle(sensor_node);
2176 ret = v4l2_async_notifier_add_subdev(&ctx->notifier, asd);
2178 ctx_err(ctx, "Error adding asd\n");
2183 ctx->notifier.ops = &cal_async_ops;
2184 ret = v4l2_async_notifier_register(&ctx->v4l2_dev,
2187 ctx_err(ctx, "Error registering async notifier\n");
2188 v4l2_async_notifier_cleanup(&ctx->notifier);
2193 * On success we need to keep reference on sensor_node, or
2194 * if notifier_cleanup was called above, sensor_node was
2200 of_node_put(sensor_node);
2201 of_node_put(ep_node);
2207 static struct cal_ctx *cal_ctx_create(struct cal_dev *cal, int inst)
2209 struct cal_ctx *ctx;
2210 struct v4l2_ctrl_handler *hdl;
2213 ctx = devm_kzalloc(&cal->pdev->dev, sizeof(*ctx), GFP_KERNEL);
2217 /* save the cal_dev * for future ref */
2220 snprintf(ctx->v4l2_dev.name, sizeof(ctx->v4l2_dev.name),
2221 "%s-%03d", CAL_MODULE_NAME, inst);
2222 ret = v4l2_device_register(&cal->pdev->dev, &ctx->v4l2_dev);
2226 hdl = &ctx->ctrl_handler;
2227 ret = v4l2_ctrl_handler_init(hdl, 11);
2229 ctx_err(ctx, "Failed to init ctrl handler\n");
2232 ctx->v4l2_dev.ctrl_handler = hdl;
2234 /* Make sure Camera Core H/W register area is available */
2235 ctx->phy = cal->phy[inst];
2237 /* Store the instance id */
2241 ret = of_cal_create_instance(ctx, inst);
2249 v4l2_ctrl_handler_free(hdl);
2251 v4l2_device_unregister(&ctx->v4l2_dev);
2256 static const struct of_device_id cal_of_match[] = {
2258 .compatible = "ti,dra72-cal",
2259 .data = (void *)&dra72x_cal_data,
2262 .compatible = "ti,dra72-pre-es2-cal",
2263 .data = (void *)&dra72x_es1_cal_data,
2266 .compatible = "ti,dra76-cal",
2267 .data = (void *)&dra76x_cal_data,
2270 .compatible = "ti,am654-cal",
2271 .data = (void *)&am654_cal_data,
2275 MODULE_DEVICE_TABLE(of, cal_of_match);
2278 * Get Revision and HW info
2280 static void cal_get_hwinfo(struct cal_dev *cal)
2285 revision = reg_read(cal, CAL_HL_REVISION);
2286 cal_dbg(3, cal, "CAL_HL_REVISION = 0x%08x (expecting 0x40000200)\n",
2289 hwinfo = reg_read(cal, CAL_HL_HWINFO);
2290 cal_dbg(3, cal, "CAL_HL_HWINFO = 0x%08x (expecting 0xA3C90469)\n",
2294 static int cal_probe(struct platform_device *pdev)
2296 struct cal_dev *cal;
2297 struct cal_ctx *ctx;
2302 cal = devm_kzalloc(&pdev->dev, sizeof(*cal), GFP_KERNEL);
2306 cal->data = of_device_get_match_data(&pdev->dev);
2308 dev_err(&pdev->dev, "Could not get feature data based on compatible version\n");
2313 platform_set_drvdata(pdev, cal);
2315 /* Acquire resources: clocks, CAMERARX regmap, I/O memory and IRQ. */
2316 cal->fclk = devm_clk_get(&pdev->dev, "fck");
2317 if (IS_ERR(cal->fclk)) {
2318 dev_err(&pdev->dev, "cannot get CAL fclk\n");
2319 return PTR_ERR(cal->fclk);
2322 ret = cal_camerarx_init_regmap(cal);
2326 cal->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2328 cal->base = devm_ioremap_resource(&pdev->dev, cal->res);
2329 if (IS_ERR(cal->base))
2330 return PTR_ERR(cal->base);
2332 cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
2333 cal->res->name, &cal->res->start, &cal->res->end);
2335 irq = platform_get_irq(pdev, 0);
2336 cal_dbg(1, cal, "got irq# %d\n", irq);
2337 ret = devm_request_irq(&pdev->dev, irq, cal_irq, 0, CAL_MODULE_NAME,
2342 /* Create CAMERARX PHYs. */
2343 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
2344 cal->phy[i] = cal_camerarx_create(cal, i);
2345 if (IS_ERR(cal->phy[i]))
2346 return PTR_ERR(cal->phy[i]);
2349 /* Create contexts. */
2350 for (i = 0; i < cal->data->num_csi2_phy; ++i)
2351 cal->ctx[i] = cal_ctx_create(cal, i);
2353 if (!cal->ctx[0] && !cal->ctx[1]) {
2354 cal_err(cal, "Neither port is configured, no point in staying up\n");
2358 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
2360 /* Read the revision and hardware info to verify hardware access. */
2361 pm_runtime_enable(&pdev->dev);
2362 ret = pm_runtime_get_sync(&pdev->dev);
2364 goto runtime_disable;
2366 cal_get_hwinfo(cal);
2367 pm_runtime_put_sync(&pdev->dev);
2372 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
2374 pm_runtime_disable(&pdev->dev);
2375 for (i = 0; i < CAL_NUM_CONTEXT; i++) {
2378 v4l2_async_notifier_unregister(&ctx->notifier);
2379 v4l2_async_notifier_cleanup(&ctx->notifier);
2380 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2381 v4l2_device_unregister(&ctx->v4l2_dev);
2388 static int cal_remove(struct platform_device *pdev)
2390 struct cal_dev *cal = platform_get_drvdata(pdev);
2391 struct cal_ctx *ctx;
2394 cal_dbg(1, cal, "Removing %s\n", CAL_MODULE_NAME);
2396 pm_runtime_get_sync(&pdev->dev);
2398 for (i = 0; i < CAL_NUM_CONTEXT; i++) {
2401 ctx_dbg(1, ctx, "unregistering %s\n",
2402 video_device_node_name(&ctx->vdev));
2403 cal_camerarx_disable(ctx->phy);
2404 v4l2_async_notifier_unregister(&ctx->notifier);
2405 v4l2_async_notifier_cleanup(&ctx->notifier);
2406 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2407 v4l2_device_unregister(&ctx->v4l2_dev);
2408 video_unregister_device(&ctx->vdev);
2412 pm_runtime_put_sync(&pdev->dev);
2413 pm_runtime_disable(&pdev->dev);
2415 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
2420 static int cal_runtime_resume(struct device *dev)
2422 struct cal_dev *cal = dev_get_drvdata(dev);
2424 if (cal->data->flags & DRA72_CAL_PRE_ES2_LDO_DISABLE) {
2426 * Apply errata on both port everytime we (re-)enable
2429 cal_camerarx_i913_errata(cal->phy[0]);
2430 cal_camerarx_i913_errata(cal->phy[1]);
2436 static const struct dev_pm_ops cal_pm_ops = {
2437 .runtime_resume = cal_runtime_resume,
2440 static struct platform_driver cal_pdrv = {
2442 .remove = cal_remove,
2444 .name = CAL_MODULE_NAME,
2446 .of_match_table = cal_of_match,
2450 module_platform_driver(cal_pdrv);