1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI Camera Access Layer (CAL) - Driver
5 * Copyright (c) 2015-2020 Texas Instruments Inc.
8 * Benoit Parrot <bparrot@ti.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
23 #include <media/media-device.h>
24 #include <media/v4l2-async.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-device.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
33 MODULE_DESCRIPTION("TI CAL driver");
34 MODULE_AUTHOR("Benoit Parrot, <bparrot@ti.com>");
35 MODULE_LICENSE("GPL v2");
36 MODULE_VERSION("0.1.0");
38 int cal_video_nr = -1;
39 module_param_named(video_nr, cal_video_nr, uint, 0644);
40 MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
42 unsigned int cal_debug;
43 module_param_named(debug, cal_debug, uint, 0644);
44 MODULE_PARM_DESC(debug, "activates debug info");
46 #ifdef CONFIG_VIDEO_TI_CAL_MC
47 #define CAL_MC_API_DEFAULT 1
49 #define CAL_MC_API_DEFAULT 0
52 bool cal_mc_api = CAL_MC_API_DEFAULT;
53 module_param_named(mc_api, cal_mc_api, bool, 0444);
54 MODULE_PARM_DESC(mc_api, "activates the MC API");
56 /* ------------------------------------------------------------------
58 * ------------------------------------------------------------------
61 const struct cal_format_info cal_formats[] = {
63 .fourcc = V4L2_PIX_FMT_YUYV,
64 .code = MEDIA_BUS_FMT_YUYV8_2X8,
67 .fourcc = V4L2_PIX_FMT_UYVY,
68 .code = MEDIA_BUS_FMT_UYVY8_2X8,
71 .fourcc = V4L2_PIX_FMT_YVYU,
72 .code = MEDIA_BUS_FMT_YVYU8_2X8,
75 .fourcc = V4L2_PIX_FMT_VYUY,
76 .code = MEDIA_BUS_FMT_VYUY8_2X8,
79 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
80 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
83 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
84 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
87 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
88 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
91 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
92 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
95 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
96 .code = MEDIA_BUS_FMT_RGB888_2X12_LE,
99 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
100 .code = MEDIA_BUS_FMT_RGB888_2X12_BE,
103 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
104 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
111 .fourcc = V4L2_PIX_FMT_SGBRG8,
112 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
115 .fourcc = V4L2_PIX_FMT_SGRBG8,
116 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
119 .fourcc = V4L2_PIX_FMT_SRGGB8,
120 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
123 .fourcc = V4L2_PIX_FMT_SBGGR10,
124 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
127 .fourcc = V4L2_PIX_FMT_SGBRG10,
128 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
131 .fourcc = V4L2_PIX_FMT_SGRBG10,
132 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
135 .fourcc = V4L2_PIX_FMT_SRGGB10,
136 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
139 .fourcc = V4L2_PIX_FMT_SBGGR12,
140 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
143 .fourcc = V4L2_PIX_FMT_SGBRG12,
144 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
147 .fourcc = V4L2_PIX_FMT_SGRBG12,
148 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
151 .fourcc = V4L2_PIX_FMT_SRGGB12,
152 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
157 const unsigned int cal_num_formats = ARRAY_SIZE(cal_formats);
159 const struct cal_format_info *cal_format_by_fourcc(u32 fourcc)
163 for (i = 0; i < ARRAY_SIZE(cal_formats); ++i) {
164 if (cal_formats[i].fourcc == fourcc)
165 return &cal_formats[i];
171 const struct cal_format_info *cal_format_by_code(u32 code)
175 for (i = 0; i < ARRAY_SIZE(cal_formats); ++i) {
176 if (cal_formats[i].code == code)
177 return &cal_formats[i];
183 /* ------------------------------------------------------------------
185 * ------------------------------------------------------------------
188 static const struct cal_camerarx_data dra72x_cal_camerarx[] = {
191 [F_CTRLCLKEN] = { 10, 10 },
192 [F_CAMMODE] = { 11, 12 },
193 [F_LANEENABLE] = { 13, 16 },
194 [F_CSI_MODE] = { 17, 17 },
200 [F_CTRLCLKEN] = { 0, 0 },
201 [F_CAMMODE] = { 1, 2 },
202 [F_LANEENABLE] = { 3, 4 },
203 [F_CSI_MODE] = { 5, 5 },
209 static const struct cal_data dra72x_cal_data = {
210 .camerarx = dra72x_cal_camerarx,
211 .num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
214 static const struct cal_data dra72x_es1_cal_data = {
215 .camerarx = dra72x_cal_camerarx,
216 .num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
217 .flags = DRA72_CAL_PRE_ES2_LDO_DISABLE,
220 static const struct cal_camerarx_data dra76x_cal_csi_phy[] = {
223 [F_CTRLCLKEN] = { 8, 8 },
224 [F_CAMMODE] = { 9, 10 },
225 [F_CSI_MODE] = { 11, 11 },
226 [F_LANEENABLE] = { 27, 31 },
232 [F_CTRLCLKEN] = { 0, 0 },
233 [F_CAMMODE] = { 1, 2 },
234 [F_CSI_MODE] = { 3, 3 },
235 [F_LANEENABLE] = { 24, 26 },
241 static const struct cal_data dra76x_cal_data = {
242 .camerarx = dra76x_cal_csi_phy,
243 .num_csi2_phy = ARRAY_SIZE(dra76x_cal_csi_phy),
246 static const struct cal_camerarx_data am654_cal_csi_phy[] = {
249 [F_CTRLCLKEN] = { 15, 15 },
250 [F_CAMMODE] = { 24, 25 },
251 [F_LANEENABLE] = { 0, 4 },
257 static const struct cal_data am654_cal_data = {
258 .camerarx = am654_cal_csi_phy,
259 .num_csi2_phy = ARRAY_SIZE(am654_cal_csi_phy),
262 /* ------------------------------------------------------------------
263 * I/O Register Accessors
264 * ------------------------------------------------------------------
267 void cal_quickdump_regs(struct cal_dev *cal)
271 cal_info(cal, "CAL Registers @ 0x%pa:\n", &cal->res->start);
272 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
273 (__force const void *)cal->base,
274 resource_size(cal->res), false);
276 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
277 struct cal_camerarx *phy = cal->phy[i];
279 cal_info(cal, "CSI2 Core %u Registers @ %pa:\n", i,
281 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
282 (__force const void *)phy->base,
283 resource_size(phy->res),
288 /* ------------------------------------------------------------------
290 * ------------------------------------------------------------------
293 #define CAL_MAX_PIX_PROC 4
295 static int cal_reserve_pix_proc(struct cal_dev *cal)
299 spin_lock(&cal->v4l2_dev.lock);
301 ret = find_first_zero_bit(&cal->reserved_pix_proc_mask, CAL_MAX_PIX_PROC);
303 if (ret == CAL_MAX_PIX_PROC) {
304 spin_unlock(&cal->v4l2_dev.lock);
308 cal->reserved_pix_proc_mask |= BIT(ret);
310 spin_unlock(&cal->v4l2_dev.lock);
315 static void cal_release_pix_proc(struct cal_dev *cal, unsigned int pix_proc_num)
317 spin_lock(&cal->v4l2_dev.lock);
319 cal->reserved_pix_proc_mask &= ~BIT(pix_proc_num);
321 spin_unlock(&cal->v4l2_dev.lock);
324 static void cal_ctx_csi2_config(struct cal_ctx *ctx)
328 val = cal_read(ctx->cal, CAL_CSI2_CTX(ctx->phy->instance, ctx->csi2_ctx));
329 cal_set_field(&val, ctx->cport, CAL_CSI2_CTX_CPORT_MASK);
331 * DT type: MIPI CSI-2 Specs
332 * 0x1: All - DT filter is disabled
333 * 0x24: RGB888 1 pixel = 3 bytes
334 * 0x2B: RAW10 4 pixels = 5 bytes
335 * 0x2A: RAW8 1 pixel = 1 byte
336 * 0x1E: YUV422 2 pixels = 4 bytes
338 cal_set_field(&val, ctx->datatype, CAL_CSI2_CTX_DT_MASK);
339 cal_set_field(&val, ctx->vc, CAL_CSI2_CTX_VC_MASK);
340 cal_set_field(&val, ctx->v_fmt.fmt.pix.height, CAL_CSI2_CTX_LINES_MASK);
341 cal_set_field(&val, CAL_CSI2_CTX_ATT_PIX, CAL_CSI2_CTX_ATT_MASK);
342 cal_set_field(&val, CAL_CSI2_CTX_PACK_MODE_LINE,
343 CAL_CSI2_CTX_PACK_MODE_MASK);
344 cal_write(ctx->cal, CAL_CSI2_CTX(ctx->phy->instance, ctx->csi2_ctx), val);
345 ctx_dbg(3, ctx, "CAL_CSI2_CTX(%u, %u) = 0x%08x\n",
346 ctx->phy->instance, ctx->csi2_ctx,
347 cal_read(ctx->cal, CAL_CSI2_CTX(ctx->phy->instance, ctx->csi2_ctx)));
350 static void cal_ctx_pix_proc_config(struct cal_ctx *ctx)
352 u32 val, extract, pack;
354 switch (ctx->fmtinfo->bpp) {
356 extract = CAL_PIX_PROC_EXTRACT_B8;
357 pack = CAL_PIX_PROC_PACK_B8;
360 extract = CAL_PIX_PROC_EXTRACT_B10_MIPI;
361 pack = CAL_PIX_PROC_PACK_B16;
364 extract = CAL_PIX_PROC_EXTRACT_B12_MIPI;
365 pack = CAL_PIX_PROC_PACK_B16;
368 extract = CAL_PIX_PROC_EXTRACT_B16_LE;
369 pack = CAL_PIX_PROC_PACK_B16;
373 * If you see this warning then it means that you added
374 * some new entry in the cal_formats[] array with a different
375 * bit per pixel values then the one supported below.
376 * Either add support for the new bpp value below or adjust
377 * the new entry to use one of the value below.
379 * Instead of failing here just use 8 bpp as a default.
381 dev_warn_once(ctx->cal->dev,
382 "%s:%d:%s: bpp:%d unsupported! Overwritten with 8.\n",
383 __FILE__, __LINE__, __func__, ctx->fmtinfo->bpp);
384 extract = CAL_PIX_PROC_EXTRACT_B8;
385 pack = CAL_PIX_PROC_PACK_B8;
389 val = cal_read(ctx->cal, CAL_PIX_PROC(ctx->pix_proc));
390 cal_set_field(&val, extract, CAL_PIX_PROC_EXTRACT_MASK);
391 cal_set_field(&val, CAL_PIX_PROC_DPCMD_BYPASS, CAL_PIX_PROC_DPCMD_MASK);
392 cal_set_field(&val, CAL_PIX_PROC_DPCME_BYPASS, CAL_PIX_PROC_DPCME_MASK);
393 cal_set_field(&val, pack, CAL_PIX_PROC_PACK_MASK);
394 cal_set_field(&val, ctx->cport, CAL_PIX_PROC_CPORT_MASK);
395 cal_set_field(&val, 1, CAL_PIX_PROC_EN_MASK);
396 cal_write(ctx->cal, CAL_PIX_PROC(ctx->pix_proc), val);
397 ctx_dbg(3, ctx, "CAL_PIX_PROC(%u) = 0x%08x\n", ctx->pix_proc,
398 cal_read(ctx->cal, CAL_PIX_PROC(ctx->pix_proc)));
401 static void cal_ctx_wr_dma_config(struct cal_ctx *ctx)
403 unsigned int stride = ctx->v_fmt.fmt.pix.bytesperline;
406 val = cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx));
407 cal_set_field(&val, ctx->cport, CAL_WR_DMA_CTRL_CPORT_MASK);
408 cal_set_field(&val, ctx->v_fmt.fmt.pix.height,
409 CAL_WR_DMA_CTRL_YSIZE_MASK);
410 cal_set_field(&val, CAL_WR_DMA_CTRL_DTAG_PIX_DAT,
411 CAL_WR_DMA_CTRL_DTAG_MASK);
412 cal_set_field(&val, CAL_WR_DMA_CTRL_PATTERN_LINEAR,
413 CAL_WR_DMA_CTRL_PATTERN_MASK);
414 cal_set_field(&val, 1, CAL_WR_DMA_CTRL_STALL_RD_MASK);
415 cal_write(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx), val);
416 ctx_dbg(3, ctx, "CAL_WR_DMA_CTRL(%d) = 0x%08x\n", ctx->dma_ctx,
417 cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx)));
419 cal_write_field(ctx->cal, CAL_WR_DMA_OFST(ctx->dma_ctx),
420 stride / 16, CAL_WR_DMA_OFST_MASK);
421 ctx_dbg(3, ctx, "CAL_WR_DMA_OFST(%d) = 0x%08x\n", ctx->dma_ctx,
422 cal_read(ctx->cal, CAL_WR_DMA_OFST(ctx->dma_ctx)));
424 val = cal_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->dma_ctx));
425 /* 64 bit word means no skipping */
426 cal_set_field(&val, 0, CAL_WR_DMA_XSIZE_XSKIP_MASK);
428 * The XSIZE field is expressed in 64-bit units and prevents overflows
429 * in case of synchronization issues by limiting the number of bytes
432 cal_set_field(&val, stride / 8, CAL_WR_DMA_XSIZE_MASK);
433 cal_write(ctx->cal, CAL_WR_DMA_XSIZE(ctx->dma_ctx), val);
434 ctx_dbg(3, ctx, "CAL_WR_DMA_XSIZE(%d) = 0x%08x\n", ctx->dma_ctx,
435 cal_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->dma_ctx)));
438 void cal_ctx_set_dma_addr(struct cal_ctx *ctx, dma_addr_t addr)
440 cal_write(ctx->cal, CAL_WR_DMA_ADDR(ctx->dma_ctx), addr);
443 static void cal_ctx_wr_dma_enable(struct cal_ctx *ctx)
445 u32 val = cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx));
447 cal_set_field(&val, CAL_WR_DMA_CTRL_MODE_CONST,
448 CAL_WR_DMA_CTRL_MODE_MASK);
449 cal_write(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx), val);
452 static void cal_ctx_wr_dma_disable(struct cal_ctx *ctx)
454 u32 val = cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx));
456 cal_set_field(&val, CAL_WR_DMA_CTRL_MODE_DIS,
457 CAL_WR_DMA_CTRL_MODE_MASK);
458 cal_write(ctx->cal, CAL_WR_DMA_CTRL(ctx->dma_ctx), val);
461 static bool cal_ctx_wr_dma_stopped(struct cal_ctx *ctx)
465 spin_lock_irq(&ctx->dma.lock);
466 stopped = ctx->dma.state == CAL_DMA_STOPPED;
467 spin_unlock_irq(&ctx->dma.lock);
473 cal_get_remote_frame_desc_entry(struct cal_camerarx *phy,
474 struct v4l2_mbus_frame_desc_entry *entry)
476 struct v4l2_mbus_frame_desc fd;
479 ret = cal_camerarx_get_remote_frame_desc(phy, &fd);
481 if (ret != -ENOIOCTLCMD)
482 dev_err(phy->cal->dev,
483 "Failed to get remote frame desc: %d\n", ret);
487 if (fd.num_entries == 0) {
488 dev_err(phy->cal->dev,
489 "No streams found in the remote frame descriptor\n");
494 if (fd.num_entries > 1)
495 dev_dbg(phy->cal->dev,
496 "Multiple streams not supported in remote frame descriptor, using the first one\n");
498 *entry = fd.entry[0];
503 int cal_ctx_prepare(struct cal_ctx *ctx)
505 struct v4l2_mbus_frame_desc_entry entry;
508 ret = cal_get_remote_frame_desc_entry(ctx->phy, &entry);
510 if (ret == -ENOIOCTLCMD) {
512 ctx->datatype = CAL_CSI2_CTX_DT_ANY;
514 ctx_dbg(2, ctx, "Framedesc: len %u, vc %u, dt %#x\n",
515 entry.length, entry.bus.csi2.vc, entry.bus.csi2.dt);
517 ctx->vc = entry.bus.csi2.vc;
518 ctx->datatype = entry.bus.csi2.dt;
523 ctx->use_pix_proc = !ctx->fmtinfo->meta;
525 if (ctx->use_pix_proc) {
526 ret = cal_reserve_pix_proc(ctx->cal);
528 ctx_err(ctx, "Failed to reserve pix proc: %d\n", ret);
538 void cal_ctx_unprepare(struct cal_ctx *ctx)
540 if (ctx->use_pix_proc)
541 cal_release_pix_proc(ctx->cal, ctx->pix_proc);
544 void cal_ctx_start(struct cal_ctx *ctx)
546 struct cal_camerarx *phy = ctx->phy;
549 * Reset the frame number & sequence number, but only if the
550 * virtual channel is not already in use.
553 spin_lock(&phy->vc_lock);
555 if (phy->vc_enable_count[ctx->vc]++ == 0) {
556 phy->vc_frame_number[ctx->vc] = 0;
557 phy->vc_sequence[ctx->vc] = 0;
560 spin_unlock(&phy->vc_lock);
562 ctx->dma.state = CAL_DMA_RUNNING;
564 /* Configure the CSI-2, pixel processing and write DMA contexts. */
565 cal_ctx_csi2_config(ctx);
566 if (ctx->use_pix_proc)
567 cal_ctx_pix_proc_config(ctx);
568 cal_ctx_wr_dma_config(ctx);
570 /* Enable IRQ_WDMA_END and IRQ_WDMA_START. */
571 cal_write(ctx->cal, CAL_HL_IRQENABLE_SET(1),
572 CAL_HL_IRQ_WDMA_END_MASK(ctx->dma_ctx));
573 cal_write(ctx->cal, CAL_HL_IRQENABLE_SET(2),
574 CAL_HL_IRQ_WDMA_START_MASK(ctx->dma_ctx));
576 cal_ctx_wr_dma_enable(ctx);
579 void cal_ctx_stop(struct cal_ctx *ctx)
581 struct cal_camerarx *phy = ctx->phy;
584 WARN_ON(phy->vc_enable_count[ctx->vc] == 0);
586 spin_lock(&phy->vc_lock);
587 phy->vc_enable_count[ctx->vc]--;
588 spin_unlock(&phy->vc_lock);
591 * Request DMA stop and wait until it completes. If completion times
592 * out, forcefully disable the DMA.
594 spin_lock_irq(&ctx->dma.lock);
595 ctx->dma.state = CAL_DMA_STOP_REQUESTED;
596 spin_unlock_irq(&ctx->dma.lock);
598 timeout = wait_event_timeout(ctx->dma.wait, cal_ctx_wr_dma_stopped(ctx),
599 msecs_to_jiffies(500));
601 ctx_err(ctx, "failed to disable dma cleanly\n");
602 cal_ctx_wr_dma_disable(ctx);
605 /* Disable IRQ_WDMA_END and IRQ_WDMA_START. */
606 cal_write(ctx->cal, CAL_HL_IRQENABLE_CLR(1),
607 CAL_HL_IRQ_WDMA_END_MASK(ctx->dma_ctx));
608 cal_write(ctx->cal, CAL_HL_IRQENABLE_CLR(2),
609 CAL_HL_IRQ_WDMA_START_MASK(ctx->dma_ctx));
611 ctx->dma.state = CAL_DMA_STOPPED;
613 /* Disable CSI2 context */
614 cal_write(ctx->cal, CAL_CSI2_CTX(ctx->phy->instance, ctx->csi2_ctx), 0);
616 /* Disable pix proc */
617 if (ctx->use_pix_proc)
618 cal_write(ctx->cal, CAL_PIX_PROC(ctx->pix_proc), 0);
621 /* ------------------------------------------------------------------
623 * ------------------------------------------------------------------
627 * Track a sequence number for each virtual channel, which is shared by
628 * all contexts using the same virtual channel. This is done using the
629 * CSI-2 frame number as a base.
631 static void cal_update_seq_number(struct cal_ctx *ctx)
633 struct cal_dev *cal = ctx->cal;
634 struct cal_camerarx *phy = ctx->phy;
635 u16 prev_frame_num, frame_num;
639 cal_read(cal, CAL_CSI2_STATUS(phy->instance, ctx->csi2_ctx)) &
642 if (phy->vc_frame_number[vc] != frame_num) {
643 prev_frame_num = phy->vc_frame_number[vc];
645 if (prev_frame_num >= frame_num)
646 phy->vc_sequence[vc] += 1;
648 phy->vc_sequence[vc] += frame_num - prev_frame_num;
650 phy->vc_frame_number[vc] = frame_num;
654 static inline void cal_irq_wdma_start(struct cal_ctx *ctx)
656 spin_lock(&ctx->dma.lock);
658 if (ctx->dma.state == CAL_DMA_STOP_REQUESTED) {
660 * If a stop is requested, disable the write DMA context
661 * immediately. The CAL_WR_DMA_CTRL_j.MODE field is shadowed,
662 * the current frame will complete and the DMA will then stop.
664 cal_ctx_wr_dma_disable(ctx);
665 ctx->dma.state = CAL_DMA_STOP_PENDING;
666 } else if (!list_empty(&ctx->dma.queue) && !ctx->dma.pending) {
668 * Otherwise, if a new buffer is available, queue it to the
671 struct cal_buffer *buf;
674 buf = list_first_entry(&ctx->dma.queue, struct cal_buffer,
676 addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
677 cal_ctx_set_dma_addr(ctx, addr);
679 ctx->dma.pending = buf;
680 list_del(&buf->list);
683 spin_unlock(&ctx->dma.lock);
685 cal_update_seq_number(ctx);
688 static inline void cal_irq_wdma_end(struct cal_ctx *ctx)
690 struct cal_buffer *buf = NULL;
692 spin_lock(&ctx->dma.lock);
694 /* If the DMA context was stopping, it is now stopped. */
695 if (ctx->dma.state == CAL_DMA_STOP_PENDING) {
696 ctx->dma.state = CAL_DMA_STOPPED;
697 wake_up(&ctx->dma.wait);
700 /* If a new buffer was queued, complete the current buffer. */
701 if (ctx->dma.pending) {
702 buf = ctx->dma.active;
703 ctx->dma.active = ctx->dma.pending;
704 ctx->dma.pending = NULL;
707 spin_unlock(&ctx->dma.lock);
710 buf->vb.vb2_buf.timestamp = ktime_get_ns();
711 buf->vb.field = ctx->v_fmt.fmt.pix.field;
712 buf->vb.sequence = ctx->phy->vc_sequence[ctx->vc];
714 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
718 static irqreturn_t cal_irq(int irq_cal, void *data)
720 struct cal_dev *cal = data;
723 status = cal_read(cal, CAL_HL_IRQSTATUS(0));
727 cal_write(cal, CAL_HL_IRQSTATUS(0), status);
729 if (status & CAL_HL_IRQ_OCPO_ERR_MASK)
730 dev_err_ratelimited(cal->dev, "OCPO ERROR\n");
732 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
733 if (status & CAL_HL_IRQ_CIO_MASK(i)) {
734 u32 cio_stat = cal_read(cal,
735 CAL_CSI2_COMPLEXIO_IRQSTATUS(i));
737 dev_err_ratelimited(cal->dev,
738 "CIO%u error: %#08x\n", i, cio_stat);
740 cal_write(cal, CAL_CSI2_COMPLEXIO_IRQSTATUS(i),
744 if (status & CAL_HL_IRQ_VC_MASK(i)) {
745 u32 vc_stat = cal_read(cal, CAL_CSI2_VC_IRQSTATUS(i));
747 dev_err_ratelimited(cal->dev,
748 "CIO%u VC error: %#08x\n",
751 cal_write(cal, CAL_CSI2_VC_IRQSTATUS(i), vc_stat);
756 /* Check which DMA just finished */
757 status = cal_read(cal, CAL_HL_IRQSTATUS(1));
761 /* Clear Interrupt status */
762 cal_write(cal, CAL_HL_IRQSTATUS(1), status);
764 for (i = 0; i < cal->num_contexts; ++i) {
765 if (status & CAL_HL_IRQ_WDMA_END_MASK(i))
766 cal_irq_wdma_end(cal->ctx[i]);
770 /* Check which DMA just started */
771 status = cal_read(cal, CAL_HL_IRQSTATUS(2));
775 /* Clear Interrupt status */
776 cal_write(cal, CAL_HL_IRQSTATUS(2), status);
778 for (i = 0; i < cal->num_contexts; ++i) {
779 if (status & CAL_HL_IRQ_WDMA_START_MASK(i))
780 cal_irq_wdma_start(cal->ctx[i]);
787 /* ------------------------------------------------------------------
788 * Asynchronous V4L2 subdev binding
789 * ------------------------------------------------------------------
792 struct cal_v4l2_async_subdev {
793 struct v4l2_async_subdev asd; /* Must be first */
794 struct cal_camerarx *phy;
797 static inline struct cal_v4l2_async_subdev *
798 to_cal_asd(struct v4l2_async_subdev *asd)
800 return container_of(asd, struct cal_v4l2_async_subdev, asd);
803 static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
804 struct v4l2_subdev *subdev,
805 struct v4l2_async_subdev *asd)
807 struct cal_camerarx *phy = to_cal_asd(asd)->phy;
812 phy_info(phy, "Rejecting subdev %s (Already set!!)",
817 phy->source = subdev;
818 phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
820 pad = media_entity_get_fwnode_pad(&subdev->entity,
821 of_fwnode_handle(phy->source_ep_node),
822 MEDIA_PAD_FL_SOURCE);
824 phy_err(phy, "Source %s has no connected source pad\n",
829 ret = media_create_pad_link(&subdev->entity, pad,
830 &phy->subdev.entity, CAL_CAMERARX_PAD_SINK,
831 MEDIA_LNK_FL_IMMUTABLE |
832 MEDIA_LNK_FL_ENABLED);
834 phy_err(phy, "Failed to create media link for source %s\n",
842 static int cal_async_notifier_complete(struct v4l2_async_notifier *notifier)
844 struct cal_dev *cal = container_of(notifier, struct cal_dev, notifier);
848 for (i = 0; i < cal->num_contexts; ++i) {
849 ret = cal_ctx_v4l2_register(cal->ctx[i]);
857 ret = v4l2_device_register_subdev_nodes(&cal->v4l2_dev);
865 if (!cal->ctx[i - 1])
868 cal_ctx_v4l2_unregister(cal->ctx[i - 1]);
874 static const struct v4l2_async_notifier_operations cal_async_notifier_ops = {
875 .bound = cal_async_notifier_bound,
876 .complete = cal_async_notifier_complete,
879 static int cal_async_notifier_register(struct cal_dev *cal)
884 v4l2_async_nf_init(&cal->notifier);
885 cal->notifier.ops = &cal_async_notifier_ops;
887 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
888 struct cal_camerarx *phy = cal->phy[i];
889 struct cal_v4l2_async_subdev *casd;
890 struct fwnode_handle *fwnode;
892 if (!phy->source_node)
895 fwnode = of_fwnode_handle(phy->source_node);
896 casd = v4l2_async_nf_add_fwnode(&cal->notifier,
898 struct cal_v4l2_async_subdev);
900 phy_err(phy, "Failed to add subdev to notifier\n");
908 ret = v4l2_async_nf_register(&cal->v4l2_dev, &cal->notifier);
910 cal_err(cal, "Error registering async notifier\n");
917 v4l2_async_nf_cleanup(&cal->notifier);
921 static void cal_async_notifier_unregister(struct cal_dev *cal)
923 v4l2_async_nf_unregister(&cal->notifier);
924 v4l2_async_nf_cleanup(&cal->notifier);
927 /* ------------------------------------------------------------------
928 * Media and V4L2 device handling
929 * ------------------------------------------------------------------
933 * Register user-facing devices. To be called at the end of the probe function
934 * when all resources are initialized and ready.
936 static int cal_media_register(struct cal_dev *cal)
940 ret = media_device_register(&cal->mdev);
942 cal_err(cal, "Failed to register media device\n");
947 * Register the async notifier. This may trigger registration of the
948 * V4L2 video devices if all subdevs are ready.
950 ret = cal_async_notifier_register(cal);
952 media_device_unregister(&cal->mdev);
960 * Unregister the user-facing devices, but don't free memory yet. To be called
961 * at the beginning of the remove function, to disallow access from userspace.
963 static void cal_media_unregister(struct cal_dev *cal)
967 /* Unregister all the V4L2 video devices. */
968 for (i = 0; i < cal->num_contexts; i++)
969 cal_ctx_v4l2_unregister(cal->ctx[i]);
971 cal_async_notifier_unregister(cal);
972 media_device_unregister(&cal->mdev);
976 * Initialize the in-kernel objects. To be called at the beginning of the probe
977 * function, before the V4L2 device is used by the driver.
979 static int cal_media_init(struct cal_dev *cal)
981 struct media_device *mdev = &cal->mdev;
984 mdev->dev = cal->dev;
985 mdev->hw_revision = cal->revision;
986 strscpy(mdev->model, "CAL", sizeof(mdev->model));
987 media_device_init(mdev);
990 * Initialize the V4L2 device (despite the function name, this performs
991 * initialization, not registration).
993 cal->v4l2_dev.mdev = mdev;
994 ret = v4l2_device_register(cal->dev, &cal->v4l2_dev);
996 cal_err(cal, "Failed to register V4L2 device\n");
1000 vb2_dma_contig_set_max_seg_size(cal->dev, DMA_BIT_MASK(32));
1006 * Cleanup the in-kernel objects, freeing memory. To be called at the very end
1007 * of the remove sequence, when nothing (including userspace) can access the
1010 static void cal_media_cleanup(struct cal_dev *cal)
1012 v4l2_device_unregister(&cal->v4l2_dev);
1013 media_device_cleanup(&cal->mdev);
1015 vb2_dma_contig_clear_max_seg_size(cal->dev);
1018 /* ------------------------------------------------------------------
1019 * Initialization and module stuff
1020 * ------------------------------------------------------------------
1023 static struct cal_ctx *cal_ctx_create(struct cal_dev *cal, int inst)
1025 struct cal_ctx *ctx;
1028 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1033 ctx->phy = cal->phy[inst];
1034 ctx->dma_ctx = inst;
1035 ctx->csi2_ctx = inst;
1038 ret = cal_ctx_v4l2_init(ctx);
1045 static void cal_ctx_destroy(struct cal_ctx *ctx)
1047 cal_ctx_v4l2_cleanup(ctx);
1052 static const struct of_device_id cal_of_match[] = {
1054 .compatible = "ti,dra72-cal",
1055 .data = (void *)&dra72x_cal_data,
1058 .compatible = "ti,dra72-pre-es2-cal",
1059 .data = (void *)&dra72x_es1_cal_data,
1062 .compatible = "ti,dra76-cal",
1063 .data = (void *)&dra76x_cal_data,
1066 .compatible = "ti,am654-cal",
1067 .data = (void *)&am654_cal_data,
1071 MODULE_DEVICE_TABLE(of, cal_of_match);
1073 /* Get hardware revision and info. */
1075 #define CAL_HL_HWINFO_VALUE 0xa3c90469
1077 static void cal_get_hwinfo(struct cal_dev *cal)
1081 cal->revision = cal_read(cal, CAL_HL_REVISION);
1082 switch (FIELD_GET(CAL_HL_REVISION_SCHEME_MASK, cal->revision)) {
1083 case CAL_HL_REVISION_SCHEME_H08:
1084 cal_dbg(3, cal, "CAL HW revision %lu.%lu.%lu (0x%08x)\n",
1085 FIELD_GET(CAL_HL_REVISION_MAJOR_MASK, cal->revision),
1086 FIELD_GET(CAL_HL_REVISION_MINOR_MASK, cal->revision),
1087 FIELD_GET(CAL_HL_REVISION_RTL_MASK, cal->revision),
1091 case CAL_HL_REVISION_SCHEME_LEGACY:
1093 cal_info(cal, "Unexpected CAL HW revision 0x%08x\n",
1098 hwinfo = cal_read(cal, CAL_HL_HWINFO);
1099 if (hwinfo != CAL_HL_HWINFO_VALUE)
1100 cal_info(cal, "CAL_HL_HWINFO = 0x%08x, expected 0x%08x\n",
1101 hwinfo, CAL_HL_HWINFO_VALUE);
1104 static int cal_init_camerarx_regmap(struct cal_dev *cal)
1106 struct platform_device *pdev = to_platform_device(cal->dev);
1107 struct device_node *np = cal->dev->of_node;
1108 struct regmap_config config = { };
1109 struct regmap *syscon;
1110 struct resource *res;
1111 unsigned int offset;
1114 syscon = syscon_regmap_lookup_by_phandle_args(np, "ti,camerrx-control",
1116 if (!IS_ERR(syscon)) {
1117 cal->syscon_camerrx = syscon;
1118 cal->syscon_camerrx_offset = offset;
1122 dev_warn(cal->dev, "failed to get ti,camerrx-control: %ld\n",
1126 * Backward DTS compatibility. If syscon entry is not present then
1127 * check if the camerrx_control resource is present.
1129 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1131 base = devm_ioremap_resource(cal->dev, res);
1133 cal_err(cal, "failed to ioremap camerrx_control\n");
1134 return PTR_ERR(base);
1137 cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
1138 res->name, &res->start, &res->end);
1140 config.reg_bits = 32;
1141 config.reg_stride = 4;
1142 config.val_bits = 32;
1143 config.max_register = resource_size(res) - 4;
1145 syscon = regmap_init_mmio(NULL, base, &config);
1146 if (IS_ERR(syscon)) {
1147 pr_err("regmap init failed\n");
1148 return PTR_ERR(syscon);
1152 * In this case the base already point to the direct CM register so no
1153 * need for an offset.
1155 cal->syscon_camerrx = syscon;
1156 cal->syscon_camerrx_offset = 0;
1161 static int cal_probe(struct platform_device *pdev)
1163 struct cal_dev *cal;
1164 bool connected = false;
1169 cal = devm_kzalloc(&pdev->dev, sizeof(*cal), GFP_KERNEL);
1173 cal->data = of_device_get_match_data(&pdev->dev);
1175 dev_err(&pdev->dev, "Could not get feature data based on compatible version\n");
1179 cal->dev = &pdev->dev;
1180 platform_set_drvdata(pdev, cal);
1182 /* Acquire resources: clocks, CAMERARX regmap, I/O memory and IRQ. */
1183 cal->fclk = devm_clk_get(&pdev->dev, "fck");
1184 if (IS_ERR(cal->fclk)) {
1185 dev_err(&pdev->dev, "cannot get CAL fclk\n");
1186 return PTR_ERR(cal->fclk);
1189 ret = cal_init_camerarx_regmap(cal);
1193 cal->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1195 cal->base = devm_ioremap_resource(&pdev->dev, cal->res);
1196 if (IS_ERR(cal->base))
1197 return PTR_ERR(cal->base);
1199 cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
1200 cal->res->name, &cal->res->start, &cal->res->end);
1202 irq = platform_get_irq(pdev, 0);
1203 cal_dbg(1, cal, "got irq# %d\n", irq);
1204 ret = devm_request_irq(&pdev->dev, irq, cal_irq, 0, CAL_MODULE_NAME,
1209 /* Read the revision and hardware info to verify hardware access. */
1210 pm_runtime_enable(&pdev->dev);
1211 ret = pm_runtime_resume_and_get(&pdev->dev);
1213 goto error_pm_runtime;
1215 cal_get_hwinfo(cal);
1216 pm_runtime_put_sync(&pdev->dev);
1218 /* Initialize the media device. */
1219 ret = cal_media_init(cal);
1221 goto error_pm_runtime;
1223 /* Create CAMERARX PHYs. */
1224 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
1225 cal->phy[i] = cal_camerarx_create(cal, i);
1226 if (IS_ERR(cal->phy[i])) {
1227 ret = PTR_ERR(cal->phy[i]);
1229 goto error_camerarx;
1232 if (cal->phy[i]->source_node)
1237 cal_err(cal, "Neither port is configured, no point in staying up\n");
1239 goto error_camerarx;
1242 /* Create contexts. */
1243 for (i = 0; i < cal->data->num_csi2_phy; ++i) {
1244 if (!cal->phy[i]->source_node)
1247 cal->ctx[cal->num_contexts] = cal_ctx_create(cal, i);
1248 if (!cal->ctx[cal->num_contexts]) {
1249 cal_err(cal, "Failed to create context %u\n", cal->num_contexts);
1254 cal->num_contexts++;
1257 /* Register the media device. */
1258 ret = cal_media_register(cal);
1265 for (i = 0; i < cal->num_contexts; i++)
1266 cal_ctx_destroy(cal->ctx[i]);
1269 for (i = 0; i < cal->data->num_csi2_phy; i++)
1270 cal_camerarx_destroy(cal->phy[i]);
1272 cal_media_cleanup(cal);
1275 pm_runtime_disable(&pdev->dev);
1280 static int cal_remove(struct platform_device *pdev)
1282 struct cal_dev *cal = platform_get_drvdata(pdev);
1286 cal_dbg(1, cal, "Removing %s\n", CAL_MODULE_NAME);
1288 ret = pm_runtime_resume_and_get(&pdev->dev);
1290 cal_media_unregister(cal);
1292 for (i = 0; i < cal->data->num_csi2_phy; i++)
1293 cal_camerarx_disable(cal->phy[i]);
1295 for (i = 0; i < cal->num_contexts; i++)
1296 cal_ctx_destroy(cal->ctx[i]);
1298 for (i = 0; i < cal->data->num_csi2_phy; i++)
1299 cal_camerarx_destroy(cal->phy[i]);
1301 cal_media_cleanup(cal);
1304 pm_runtime_put_sync(&pdev->dev);
1305 pm_runtime_disable(&pdev->dev);
1310 static int cal_runtime_resume(struct device *dev)
1312 struct cal_dev *cal = dev_get_drvdata(dev);
1316 if (cal->data->flags & DRA72_CAL_PRE_ES2_LDO_DISABLE) {
1318 * Apply errata on both port everytime we (re-)enable
1321 for (i = 0; i < cal->data->num_csi2_phy; i++)
1322 cal_camerarx_i913_errata(cal->phy[i]);
1326 * Enable global interrupts that are not related to a particular
1327 * CAMERARAX or context.
1329 cal_write(cal, CAL_HL_IRQENABLE_SET(0), CAL_HL_IRQ_OCPO_ERR_MASK);
1331 val = cal_read(cal, CAL_CTRL);
1332 cal_set_field(&val, CAL_CTRL_BURSTSIZE_BURST128,
1333 CAL_CTRL_BURSTSIZE_MASK);
1334 cal_set_field(&val, 0xf, CAL_CTRL_TAGCNT_MASK);
1335 cal_set_field(&val, CAL_CTRL_POSTED_WRITES_NONPOSTED,
1336 CAL_CTRL_POSTED_WRITES_MASK);
1337 cal_set_field(&val, 0xff, CAL_CTRL_MFLAGL_MASK);
1338 cal_set_field(&val, 0xff, CAL_CTRL_MFLAGH_MASK);
1339 cal_write(cal, CAL_CTRL, val);
1340 cal_dbg(3, cal, "CAL_CTRL = 0x%08x\n", cal_read(cal, CAL_CTRL));
1345 static const struct dev_pm_ops cal_pm_ops = {
1346 .runtime_resume = cal_runtime_resume,
1349 static struct platform_driver cal_pdrv = {
1351 .remove = cal_remove,
1353 .name = CAL_MODULE_NAME,
1355 .of_match_table = cal_of_match,
1359 module_platform_driver(cal_pdrv);