18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * TI Camera Access Layer (CAL) - Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015-2020 Texas Instruments Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors:
88c2ecf20Sopenharmony_ci *	Benoit Parrot <bparrot@ti.com>
98c2ecf20Sopenharmony_ci *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/of_device.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
198c2ecf20Sopenharmony_ci#include <linux/regmap.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <media/media-device.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-async.h>
258c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
278c2ecf20Sopenharmony_ci#include <media/videobuf2-core.h>
288c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "cal.h"
318c2ecf20Sopenharmony_ci#include "cal_regs.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI CAL driver");
348c2ecf20Sopenharmony_ciMODULE_AUTHOR("Benoit Parrot, <bparrot@ti.com>");
358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
368c2ecf20Sopenharmony_ciMODULE_VERSION("0.1.0");
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint cal_video_nr = -1;
398c2ecf20Sopenharmony_cimodule_param_named(video_nr, cal_video_nr, uint, 0644);
408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciunsigned int cal_debug;
438c2ecf20Sopenharmony_cimodule_param_named(debug, cal_debug, uint, 0644);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "activates debug info");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
478c2ecf20Sopenharmony_ci *	Platform Data
488c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct cal_camerarx_data dra72x_cal_camerarx[] = {
528c2ecf20Sopenharmony_ci	{
538c2ecf20Sopenharmony_ci		.fields = {
548c2ecf20Sopenharmony_ci			[F_CTRLCLKEN] = { 10, 10 },
558c2ecf20Sopenharmony_ci			[F_CAMMODE] = { 11, 12 },
568c2ecf20Sopenharmony_ci			[F_LANEENABLE] = { 13, 16 },
578c2ecf20Sopenharmony_ci			[F_CSI_MODE] = { 17, 17 },
588c2ecf20Sopenharmony_ci		},
598c2ecf20Sopenharmony_ci		.num_lanes = 4,
608c2ecf20Sopenharmony_ci	},
618c2ecf20Sopenharmony_ci	{
628c2ecf20Sopenharmony_ci		.fields = {
638c2ecf20Sopenharmony_ci			[F_CTRLCLKEN] = { 0, 0 },
648c2ecf20Sopenharmony_ci			[F_CAMMODE] = { 1, 2 },
658c2ecf20Sopenharmony_ci			[F_LANEENABLE] = { 3, 4 },
668c2ecf20Sopenharmony_ci			[F_CSI_MODE] = { 5, 5 },
678c2ecf20Sopenharmony_ci		},
688c2ecf20Sopenharmony_ci		.num_lanes = 2,
698c2ecf20Sopenharmony_ci	},
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic const struct cal_data dra72x_cal_data = {
738c2ecf20Sopenharmony_ci	.camerarx = dra72x_cal_camerarx,
748c2ecf20Sopenharmony_ci	.num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic const struct cal_data dra72x_es1_cal_data = {
788c2ecf20Sopenharmony_ci	.camerarx = dra72x_cal_camerarx,
798c2ecf20Sopenharmony_ci	.num_csi2_phy = ARRAY_SIZE(dra72x_cal_camerarx),
808c2ecf20Sopenharmony_ci	.flags = DRA72_CAL_PRE_ES2_LDO_DISABLE,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct cal_camerarx_data dra76x_cal_csi_phy[] = {
848c2ecf20Sopenharmony_ci	{
858c2ecf20Sopenharmony_ci		.fields = {
868c2ecf20Sopenharmony_ci			[F_CTRLCLKEN] = { 8, 8 },
878c2ecf20Sopenharmony_ci			[F_CAMMODE] = { 9, 10 },
888c2ecf20Sopenharmony_ci			[F_CSI_MODE] = { 11, 11 },
898c2ecf20Sopenharmony_ci			[F_LANEENABLE] = { 27, 31 },
908c2ecf20Sopenharmony_ci		},
918c2ecf20Sopenharmony_ci		.num_lanes = 5,
928c2ecf20Sopenharmony_ci	},
938c2ecf20Sopenharmony_ci	{
948c2ecf20Sopenharmony_ci		.fields = {
958c2ecf20Sopenharmony_ci			[F_CTRLCLKEN] = { 0, 0 },
968c2ecf20Sopenharmony_ci			[F_CAMMODE] = { 1, 2 },
978c2ecf20Sopenharmony_ci			[F_CSI_MODE] = { 3, 3 },
988c2ecf20Sopenharmony_ci			[F_LANEENABLE] = { 24, 26 },
998c2ecf20Sopenharmony_ci		},
1008c2ecf20Sopenharmony_ci		.num_lanes = 3,
1018c2ecf20Sopenharmony_ci	},
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic const struct cal_data dra76x_cal_data = {
1058c2ecf20Sopenharmony_ci	.camerarx = dra76x_cal_csi_phy,
1068c2ecf20Sopenharmony_ci	.num_csi2_phy = ARRAY_SIZE(dra76x_cal_csi_phy),
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic const struct cal_camerarx_data am654_cal_csi_phy[] = {
1108c2ecf20Sopenharmony_ci	{
1118c2ecf20Sopenharmony_ci		.fields = {
1128c2ecf20Sopenharmony_ci			[F_CTRLCLKEN] = { 15, 15 },
1138c2ecf20Sopenharmony_ci			[F_CAMMODE] = { 24, 25 },
1148c2ecf20Sopenharmony_ci			[F_LANEENABLE] = { 0, 4 },
1158c2ecf20Sopenharmony_ci		},
1168c2ecf20Sopenharmony_ci		.num_lanes = 5,
1178c2ecf20Sopenharmony_ci	},
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct cal_data am654_cal_data = {
1218c2ecf20Sopenharmony_ci	.camerarx = am654_cal_csi_phy,
1228c2ecf20Sopenharmony_ci	.num_csi2_phy = ARRAY_SIZE(am654_cal_csi_phy),
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
1268c2ecf20Sopenharmony_ci *	I/O Register Accessors
1278c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_civoid cal_quickdump_regs(struct cal_dev *cal)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	unsigned int i;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	cal_info(cal, "CAL Registers @ 0x%pa:\n", &cal->res->start);
1358c2ecf20Sopenharmony_ci	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
1368c2ecf20Sopenharmony_ci		       (__force const void *)cal->base,
1378c2ecf20Sopenharmony_ci		       resource_size(cal->res), false);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->phy); ++i) {
1408c2ecf20Sopenharmony_ci		struct cal_camerarx *phy = cal->phy[i];
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci		if (!phy)
1438c2ecf20Sopenharmony_ci			continue;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		cal_info(cal, "CSI2 Core %u Registers @ %pa:\n", i,
1468c2ecf20Sopenharmony_ci			 &phy->res->start);
1478c2ecf20Sopenharmony_ci		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
1488c2ecf20Sopenharmony_ci			       (__force const void *)phy->base,
1498c2ecf20Sopenharmony_ci			       resource_size(phy->res),
1508c2ecf20Sopenharmony_ci			       false);
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
1558c2ecf20Sopenharmony_ci *	Context Management
1568c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_civoid cal_ctx_csi2_config(struct cal_ctx *ctx)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	u32 val;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	val = cal_read(ctx->cal, CAL_CSI2_CTX0(ctx->index));
1648c2ecf20Sopenharmony_ci	cal_set_field(&val, ctx->cport, CAL_CSI2_CTX_CPORT_MASK);
1658c2ecf20Sopenharmony_ci	/*
1668c2ecf20Sopenharmony_ci	 * DT type: MIPI CSI-2 Specs
1678c2ecf20Sopenharmony_ci	 *   0x1: All - DT filter is disabled
1688c2ecf20Sopenharmony_ci	 *  0x24: RGB888 1 pixel  = 3 bytes
1698c2ecf20Sopenharmony_ci	 *  0x2B: RAW10  4 pixels = 5 bytes
1708c2ecf20Sopenharmony_ci	 *  0x2A: RAW8   1 pixel  = 1 byte
1718c2ecf20Sopenharmony_ci	 *  0x1E: YUV422 2 pixels = 4 bytes
1728c2ecf20Sopenharmony_ci	 */
1738c2ecf20Sopenharmony_ci	cal_set_field(&val, 0x1, CAL_CSI2_CTX_DT_MASK);
1748c2ecf20Sopenharmony_ci	cal_set_field(&val, 0, CAL_CSI2_CTX_VC_MASK);
1758c2ecf20Sopenharmony_ci	cal_set_field(&val, ctx->v_fmt.fmt.pix.height, CAL_CSI2_CTX_LINES_MASK);
1768c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_CSI2_CTX_ATT_PIX, CAL_CSI2_CTX_ATT_MASK);
1778c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_CSI2_CTX_PACK_MODE_LINE,
1788c2ecf20Sopenharmony_ci		      CAL_CSI2_CTX_PACK_MODE_MASK);
1798c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_CSI2_CTX0(ctx->index), val);
1808c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_CSI2_CTX0(%d) = 0x%08x\n", ctx->index,
1818c2ecf20Sopenharmony_ci		cal_read(ctx->cal, CAL_CSI2_CTX0(ctx->index)));
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_civoid cal_ctx_pix_proc_config(struct cal_ctx *ctx)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	u32 val, extract, pack;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	switch (ctx->fmt->bpp) {
1898c2ecf20Sopenharmony_ci	case 8:
1908c2ecf20Sopenharmony_ci		extract = CAL_PIX_PROC_EXTRACT_B8;
1918c2ecf20Sopenharmony_ci		pack = CAL_PIX_PROC_PACK_B8;
1928c2ecf20Sopenharmony_ci		break;
1938c2ecf20Sopenharmony_ci	case 10:
1948c2ecf20Sopenharmony_ci		extract = CAL_PIX_PROC_EXTRACT_B10_MIPI;
1958c2ecf20Sopenharmony_ci		pack = CAL_PIX_PROC_PACK_B16;
1968c2ecf20Sopenharmony_ci		break;
1978c2ecf20Sopenharmony_ci	case 12:
1988c2ecf20Sopenharmony_ci		extract = CAL_PIX_PROC_EXTRACT_B12_MIPI;
1998c2ecf20Sopenharmony_ci		pack = CAL_PIX_PROC_PACK_B16;
2008c2ecf20Sopenharmony_ci		break;
2018c2ecf20Sopenharmony_ci	case 16:
2028c2ecf20Sopenharmony_ci		extract = CAL_PIX_PROC_EXTRACT_B16_LE;
2038c2ecf20Sopenharmony_ci		pack = CAL_PIX_PROC_PACK_B16;
2048c2ecf20Sopenharmony_ci		break;
2058c2ecf20Sopenharmony_ci	default:
2068c2ecf20Sopenharmony_ci		/*
2078c2ecf20Sopenharmony_ci		 * If you see this warning then it means that you added
2088c2ecf20Sopenharmony_ci		 * some new entry in the cal_formats[] array with a different
2098c2ecf20Sopenharmony_ci		 * bit per pixel values then the one supported below.
2108c2ecf20Sopenharmony_ci		 * Either add support for the new bpp value below or adjust
2118c2ecf20Sopenharmony_ci		 * the new entry to use one of the value below.
2128c2ecf20Sopenharmony_ci		 *
2138c2ecf20Sopenharmony_ci		 * Instead of failing here just use 8 bpp as a default.
2148c2ecf20Sopenharmony_ci		 */
2158c2ecf20Sopenharmony_ci		dev_warn_once(ctx->cal->dev,
2168c2ecf20Sopenharmony_ci			      "%s:%d:%s: bpp:%d unsupported! Overwritten with 8.\n",
2178c2ecf20Sopenharmony_ci			      __FILE__, __LINE__, __func__, ctx->fmt->bpp);
2188c2ecf20Sopenharmony_ci		extract = CAL_PIX_PROC_EXTRACT_B8;
2198c2ecf20Sopenharmony_ci		pack = CAL_PIX_PROC_PACK_B8;
2208c2ecf20Sopenharmony_ci		break;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	val = cal_read(ctx->cal, CAL_PIX_PROC(ctx->index));
2248c2ecf20Sopenharmony_ci	cal_set_field(&val, extract, CAL_PIX_PROC_EXTRACT_MASK);
2258c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_PIX_PROC_DPCMD_BYPASS, CAL_PIX_PROC_DPCMD_MASK);
2268c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_PIX_PROC_DPCME_BYPASS, CAL_PIX_PROC_DPCME_MASK);
2278c2ecf20Sopenharmony_ci	cal_set_field(&val, pack, CAL_PIX_PROC_PACK_MASK);
2288c2ecf20Sopenharmony_ci	cal_set_field(&val, ctx->cport, CAL_PIX_PROC_CPORT_MASK);
2298c2ecf20Sopenharmony_ci	cal_set_field(&val, 1, CAL_PIX_PROC_EN_MASK);
2308c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_PIX_PROC(ctx->index), val);
2318c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_PIX_PROC(%d) = 0x%08x\n", ctx->index,
2328c2ecf20Sopenharmony_ci		cal_read(ctx->cal, CAL_PIX_PROC(ctx->index)));
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_civoid cal_ctx_wr_dma_config(struct cal_ctx *ctx, unsigned int width,
2368c2ecf20Sopenharmony_ci			    unsigned int height)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	u32 val;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	val = cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->index));
2418c2ecf20Sopenharmony_ci	cal_set_field(&val, ctx->cport, CAL_WR_DMA_CTRL_CPORT_MASK);
2428c2ecf20Sopenharmony_ci	cal_set_field(&val, height, CAL_WR_DMA_CTRL_YSIZE_MASK);
2438c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_WR_DMA_CTRL_DTAG_PIX_DAT,
2448c2ecf20Sopenharmony_ci		      CAL_WR_DMA_CTRL_DTAG_MASK);
2458c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_WR_DMA_CTRL_MODE_CONST,
2468c2ecf20Sopenharmony_ci		      CAL_WR_DMA_CTRL_MODE_MASK);
2478c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_WR_DMA_CTRL_PATTERN_LINEAR,
2488c2ecf20Sopenharmony_ci		      CAL_WR_DMA_CTRL_PATTERN_MASK);
2498c2ecf20Sopenharmony_ci	cal_set_field(&val, 1, CAL_WR_DMA_CTRL_STALL_RD_MASK);
2508c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_WR_DMA_CTRL(ctx->index), val);
2518c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_WR_DMA_CTRL(%d) = 0x%08x\n", ctx->index,
2528c2ecf20Sopenharmony_ci		cal_read(ctx->cal, CAL_WR_DMA_CTRL(ctx->index)));
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/*
2558c2ecf20Sopenharmony_ci	 * width/16 not sure but giving it a whirl.
2568c2ecf20Sopenharmony_ci	 * zero does not work right
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	cal_write_field(ctx->cal,
2598c2ecf20Sopenharmony_ci			CAL_WR_DMA_OFST(ctx->index),
2608c2ecf20Sopenharmony_ci			(width / 16),
2618c2ecf20Sopenharmony_ci			CAL_WR_DMA_OFST_MASK);
2628c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_WR_DMA_OFST(%d) = 0x%08x\n", ctx->index,
2638c2ecf20Sopenharmony_ci		cal_read(ctx->cal, CAL_WR_DMA_OFST(ctx->index)));
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	val = cal_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index));
2668c2ecf20Sopenharmony_ci	/* 64 bit word means no skipping */
2678c2ecf20Sopenharmony_ci	cal_set_field(&val, 0, CAL_WR_DMA_XSIZE_XSKIP_MASK);
2688c2ecf20Sopenharmony_ci	/*
2698c2ecf20Sopenharmony_ci	 * (width*8)/64 this should be size of an entire line
2708c2ecf20Sopenharmony_ci	 * in 64bit word but 0 means all data until the end
2718c2ecf20Sopenharmony_ci	 * is detected automagically
2728c2ecf20Sopenharmony_ci	 */
2738c2ecf20Sopenharmony_ci	cal_set_field(&val, (width / 8), CAL_WR_DMA_XSIZE_MASK);
2748c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index), val);
2758c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_WR_DMA_XSIZE(%d) = 0x%08x\n", ctx->index,
2768c2ecf20Sopenharmony_ci		cal_read(ctx->cal, CAL_WR_DMA_XSIZE(ctx->index)));
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	val = cal_read(ctx->cal, CAL_CTRL);
2798c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_CTRL_BURSTSIZE_BURST128,
2808c2ecf20Sopenharmony_ci		      CAL_CTRL_BURSTSIZE_MASK);
2818c2ecf20Sopenharmony_ci	cal_set_field(&val, 0xF, CAL_CTRL_TAGCNT_MASK);
2828c2ecf20Sopenharmony_ci	cal_set_field(&val, CAL_CTRL_POSTED_WRITES_NONPOSTED,
2838c2ecf20Sopenharmony_ci		      CAL_CTRL_POSTED_WRITES_MASK);
2848c2ecf20Sopenharmony_ci	cal_set_field(&val, 0xFF, CAL_CTRL_MFLAGL_MASK);
2858c2ecf20Sopenharmony_ci	cal_set_field(&val, 0xFF, CAL_CTRL_MFLAGH_MASK);
2868c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_CTRL, val);
2878c2ecf20Sopenharmony_ci	ctx_dbg(3, ctx, "CAL_CTRL = 0x%08x\n", cal_read(ctx->cal, CAL_CTRL));
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_civoid cal_ctx_wr_dma_addr(struct cal_ctx *ctx, unsigned int dmaaddr)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	cal_write(ctx->cal, CAL_WR_DMA_ADDR(ctx->index), dmaaddr);
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
2968c2ecf20Sopenharmony_ci *	IRQ Handling
2978c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic inline void cal_schedule_next_buffer(struct cal_ctx *ctx)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct cal_dmaqueue *dma_q = &ctx->vidq;
3038c2ecf20Sopenharmony_ci	struct cal_buffer *buf;
3048c2ecf20Sopenharmony_ci	unsigned long addr;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	buf = list_entry(dma_q->active.next, struct cal_buffer, list);
3078c2ecf20Sopenharmony_ci	ctx->next_frm = buf;
3088c2ecf20Sopenharmony_ci	list_del(&buf->list);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
3118c2ecf20Sopenharmony_ci	cal_ctx_wr_dma_addr(ctx, addr);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic inline void cal_process_buffer_complete(struct cal_ctx *ctx)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	ctx->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
3178c2ecf20Sopenharmony_ci	ctx->cur_frm->vb.field = ctx->m_fmt.field;
3188c2ecf20Sopenharmony_ci	ctx->cur_frm->vb.sequence = ctx->sequence++;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
3218c2ecf20Sopenharmony_ci	ctx->cur_frm = ctx->next_frm;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic irqreturn_t cal_irq(int irq_cal, void *data)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct cal_dev *cal = data;
3278c2ecf20Sopenharmony_ci	struct cal_ctx *ctx;
3288c2ecf20Sopenharmony_ci	struct cal_dmaqueue *dma_q;
3298c2ecf20Sopenharmony_ci	u32 status;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	status = cal_read(cal, CAL_HL_IRQSTATUS(0));
3328c2ecf20Sopenharmony_ci	if (status) {
3338c2ecf20Sopenharmony_ci		unsigned int i;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		cal_write(cal, CAL_HL_IRQSTATUS(0), status);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		if (status & CAL_HL_IRQ_OCPO_ERR_MASK)
3388c2ecf20Sopenharmony_ci			dev_err_ratelimited(cal->dev, "OCPO ERROR\n");
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		for (i = 0; i < CAL_NUM_CSI2_PORTS; ++i) {
3418c2ecf20Sopenharmony_ci			if (status & CAL_HL_IRQ_CIO_MASK(i)) {
3428c2ecf20Sopenharmony_ci				u32 cio_stat = cal_read(cal,
3438c2ecf20Sopenharmony_ci							CAL_CSI2_COMPLEXIO_IRQSTATUS(i));
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci				dev_err_ratelimited(cal->dev,
3468c2ecf20Sopenharmony_ci						    "CIO%u error: %#08x\n", i, cio_stat);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci				cal_write(cal, CAL_CSI2_COMPLEXIO_IRQSTATUS(i),
3498c2ecf20Sopenharmony_ci					  cio_stat);
3508c2ecf20Sopenharmony_ci			}
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* Check which DMA just finished */
3558c2ecf20Sopenharmony_ci	status = cal_read(cal, CAL_HL_IRQSTATUS(1));
3568c2ecf20Sopenharmony_ci	if (status) {
3578c2ecf20Sopenharmony_ci		unsigned int i;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci		/* Clear Interrupt status */
3608c2ecf20Sopenharmony_ci		cal_write(cal, CAL_HL_IRQSTATUS(1), status);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(cal->ctx); ++i) {
3638c2ecf20Sopenharmony_ci			if (status & CAL_HL_IRQ_MASK(i)) {
3648c2ecf20Sopenharmony_ci				ctx = cal->ctx[i];
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci				spin_lock(&ctx->slock);
3678c2ecf20Sopenharmony_ci				ctx->dma_act = false;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci				if (ctx->cur_frm != ctx->next_frm)
3708c2ecf20Sopenharmony_ci					cal_process_buffer_complete(ctx);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci				spin_unlock(&ctx->slock);
3738c2ecf20Sopenharmony_ci			}
3748c2ecf20Sopenharmony_ci		}
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* Check which DMA just started */
3788c2ecf20Sopenharmony_ci	status = cal_read(cal, CAL_HL_IRQSTATUS(2));
3798c2ecf20Sopenharmony_ci	if (status) {
3808c2ecf20Sopenharmony_ci		unsigned int i;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		/* Clear Interrupt status */
3838c2ecf20Sopenharmony_ci		cal_write(cal, CAL_HL_IRQSTATUS(2), status);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(cal->ctx); ++i) {
3868c2ecf20Sopenharmony_ci			if (status & CAL_HL_IRQ_MASK(i)) {
3878c2ecf20Sopenharmony_ci				ctx = cal->ctx[i];
3888c2ecf20Sopenharmony_ci				dma_q = &ctx->vidq;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci				spin_lock(&ctx->slock);
3918c2ecf20Sopenharmony_ci				ctx->dma_act = true;
3928c2ecf20Sopenharmony_ci				if (!list_empty(&dma_q->active) &&
3938c2ecf20Sopenharmony_ci				    ctx->cur_frm == ctx->next_frm)
3948c2ecf20Sopenharmony_ci					cal_schedule_next_buffer(ctx);
3958c2ecf20Sopenharmony_ci				spin_unlock(&ctx->slock);
3968c2ecf20Sopenharmony_ci			}
3978c2ecf20Sopenharmony_ci		}
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
4048c2ecf20Sopenharmony_ci *	Asynchronous V4L2 subdev binding
4058c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistruct cal_v4l2_async_subdev {
4098c2ecf20Sopenharmony_ci	struct v4l2_async_subdev asd; /* Must be first */
4108c2ecf20Sopenharmony_ci	struct cal_camerarx *phy;
4118c2ecf20Sopenharmony_ci};
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic inline struct cal_v4l2_async_subdev *
4148c2ecf20Sopenharmony_cito_cal_asd(struct v4l2_async_subdev *asd)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	return container_of(asd, struct cal_v4l2_async_subdev, asd);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
4208c2ecf20Sopenharmony_ci				    struct v4l2_subdev *subdev,
4218c2ecf20Sopenharmony_ci				    struct v4l2_async_subdev *asd)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	struct cal_camerarx *phy = to_cal_asd(asd)->phy;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (phy->sensor) {
4268c2ecf20Sopenharmony_ci		phy_info(phy, "Rejecting subdev %s (Already set!!)",
4278c2ecf20Sopenharmony_ci			 subdev->name);
4288c2ecf20Sopenharmony_ci		return 0;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	phy->sensor = subdev;
4328c2ecf20Sopenharmony_ci	phy_dbg(1, phy, "Using sensor %s for capture\n", subdev->name);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return 0;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic int cal_async_notifier_complete(struct v4l2_async_notifier *notifier)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	struct cal_dev *cal = container_of(notifier, struct cal_dev, notifier);
4408c2ecf20Sopenharmony_ci	unsigned int i;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->ctx); ++i) {
4438c2ecf20Sopenharmony_ci		if (cal->ctx[i])
4448c2ecf20Sopenharmony_ci			cal_ctx_v4l2_register(cal->ctx[i]);
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	return 0;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic const struct v4l2_async_notifier_operations cal_async_notifier_ops = {
4518c2ecf20Sopenharmony_ci	.bound = cal_async_notifier_bound,
4528c2ecf20Sopenharmony_ci	.complete = cal_async_notifier_complete,
4538c2ecf20Sopenharmony_ci};
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic int cal_async_notifier_register(struct cal_dev *cal)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	unsigned int i;
4588c2ecf20Sopenharmony_ci	int ret;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	v4l2_async_notifier_init(&cal->notifier);
4618c2ecf20Sopenharmony_ci	cal->notifier.ops = &cal_async_notifier_ops;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->phy); ++i) {
4648c2ecf20Sopenharmony_ci		struct cal_camerarx *phy = cal->phy[i];
4658c2ecf20Sopenharmony_ci		struct cal_v4l2_async_subdev *casd;
4668c2ecf20Sopenharmony_ci		struct v4l2_async_subdev *asd;
4678c2ecf20Sopenharmony_ci		struct fwnode_handle *fwnode;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci		if (!phy || !phy->sensor_node)
4708c2ecf20Sopenharmony_ci			continue;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci		fwnode = of_fwnode_handle(phy->sensor_node);
4738c2ecf20Sopenharmony_ci		asd = v4l2_async_notifier_add_fwnode_subdev(&cal->notifier,
4748c2ecf20Sopenharmony_ci							    fwnode,
4758c2ecf20Sopenharmony_ci							    sizeof(*casd));
4768c2ecf20Sopenharmony_ci		if (IS_ERR(asd)) {
4778c2ecf20Sopenharmony_ci			phy_err(phy, "Failed to add subdev to notifier\n");
4788c2ecf20Sopenharmony_ci			ret = PTR_ERR(asd);
4798c2ecf20Sopenharmony_ci			goto error;
4808c2ecf20Sopenharmony_ci		}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci		casd = to_cal_asd(asd);
4838c2ecf20Sopenharmony_ci		casd->phy = phy;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	ret = v4l2_async_notifier_register(&cal->v4l2_dev, &cal->notifier);
4878c2ecf20Sopenharmony_ci	if (ret) {
4888c2ecf20Sopenharmony_ci		cal_err(cal, "Error registering async notifier\n");
4898c2ecf20Sopenharmony_ci		goto error;
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cierror:
4958c2ecf20Sopenharmony_ci	v4l2_async_notifier_cleanup(&cal->notifier);
4968c2ecf20Sopenharmony_ci	return ret;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_cistatic void cal_async_notifier_unregister(struct cal_dev *cal)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	v4l2_async_notifier_unregister(&cal->notifier);
5028c2ecf20Sopenharmony_ci	v4l2_async_notifier_cleanup(&cal->notifier);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
5068c2ecf20Sopenharmony_ci *	Media and V4L2 device handling
5078c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
5088c2ecf20Sopenharmony_ci */
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/*
5118c2ecf20Sopenharmony_ci * Register user-facing devices. To be called at the end of the probe function
5128c2ecf20Sopenharmony_ci * when all resources are initialized and ready.
5138c2ecf20Sopenharmony_ci */
5148c2ecf20Sopenharmony_cistatic int cal_media_register(struct cal_dev *cal)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	int ret;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	ret = media_device_register(&cal->mdev);
5198c2ecf20Sopenharmony_ci	if (ret) {
5208c2ecf20Sopenharmony_ci		cal_err(cal, "Failed to register media device\n");
5218c2ecf20Sopenharmony_ci		return ret;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/*
5258c2ecf20Sopenharmony_ci	 * Register the async notifier. This may trigger registration of the
5268c2ecf20Sopenharmony_ci	 * V4L2 video devices if all subdevs are ready.
5278c2ecf20Sopenharmony_ci	 */
5288c2ecf20Sopenharmony_ci	ret = cal_async_notifier_register(cal);
5298c2ecf20Sopenharmony_ci	if (ret) {
5308c2ecf20Sopenharmony_ci		media_device_unregister(&cal->mdev);
5318c2ecf20Sopenharmony_ci		return ret;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	return 0;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci/*
5388c2ecf20Sopenharmony_ci * Unregister the user-facing devices, but don't free memory yet. To be called
5398c2ecf20Sopenharmony_ci * at the beginning of the remove function, to disallow access from userspace.
5408c2ecf20Sopenharmony_ci */
5418c2ecf20Sopenharmony_cistatic void cal_media_unregister(struct cal_dev *cal)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	unsigned int i;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	/* Unregister all the V4L2 video devices. */
5468c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->ctx); i++) {
5478c2ecf20Sopenharmony_ci		if (cal->ctx[i])
5488c2ecf20Sopenharmony_ci			cal_ctx_v4l2_unregister(cal->ctx[i]);
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	cal_async_notifier_unregister(cal);
5528c2ecf20Sopenharmony_ci	media_device_unregister(&cal->mdev);
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci/*
5568c2ecf20Sopenharmony_ci * Initialize the in-kernel objects. To be called at the beginning of the probe
5578c2ecf20Sopenharmony_ci * function, before the V4L2 device is used by the driver.
5588c2ecf20Sopenharmony_ci */
5598c2ecf20Sopenharmony_cistatic int cal_media_init(struct cal_dev *cal)
5608c2ecf20Sopenharmony_ci{
5618c2ecf20Sopenharmony_ci	struct media_device *mdev = &cal->mdev;
5628c2ecf20Sopenharmony_ci	int ret;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	mdev->dev = cal->dev;
5658c2ecf20Sopenharmony_ci	mdev->hw_revision = cal->revision;
5668c2ecf20Sopenharmony_ci	strscpy(mdev->model, "CAL", sizeof(mdev->model));
5678c2ecf20Sopenharmony_ci	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
5688c2ecf20Sopenharmony_ci		 dev_name(mdev->dev));
5698c2ecf20Sopenharmony_ci	media_device_init(mdev);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	/*
5728c2ecf20Sopenharmony_ci	 * Initialize the V4L2 device (despite the function name, this performs
5738c2ecf20Sopenharmony_ci	 * initialization, not registration).
5748c2ecf20Sopenharmony_ci	 */
5758c2ecf20Sopenharmony_ci	cal->v4l2_dev.mdev = mdev;
5768c2ecf20Sopenharmony_ci	ret = v4l2_device_register(cal->dev, &cal->v4l2_dev);
5778c2ecf20Sopenharmony_ci	if (ret) {
5788c2ecf20Sopenharmony_ci		cal_err(cal, "Failed to register V4L2 device\n");
5798c2ecf20Sopenharmony_ci		return ret;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	vb2_dma_contig_set_max_seg_size(cal->dev, DMA_BIT_MASK(32));
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	return 0;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci/*
5888c2ecf20Sopenharmony_ci * Cleanup the in-kernel objects, freeing memory. To be called at the very end
5898c2ecf20Sopenharmony_ci * of the remove sequence, when nothing (including userspace) can access the
5908c2ecf20Sopenharmony_ci * objects anymore.
5918c2ecf20Sopenharmony_ci */
5928c2ecf20Sopenharmony_cistatic void cal_media_cleanup(struct cal_dev *cal)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	unsigned int i;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->ctx); i++) {
5978c2ecf20Sopenharmony_ci		if (cal->ctx[i])
5988c2ecf20Sopenharmony_ci			cal_ctx_v4l2_cleanup(cal->ctx[i]);
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	v4l2_device_unregister(&cal->v4l2_dev);
6028c2ecf20Sopenharmony_ci	media_device_cleanup(&cal->mdev);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	vb2_dma_contig_clear_max_seg_size(cal->dev);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------
6088c2ecf20Sopenharmony_ci *	Initialization and module stuff
6098c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
6108c2ecf20Sopenharmony_ci */
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic struct cal_ctx *cal_ctx_create(struct cal_dev *cal, int inst)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	struct cal_ctx *ctx;
6158c2ecf20Sopenharmony_ci	int ret;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(cal->dev, sizeof(*ctx), GFP_KERNEL);
6188c2ecf20Sopenharmony_ci	if (!ctx)
6198c2ecf20Sopenharmony_ci		return NULL;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	ctx->cal = cal;
6228c2ecf20Sopenharmony_ci	ctx->phy = cal->phy[inst];
6238c2ecf20Sopenharmony_ci	ctx->index = inst;
6248c2ecf20Sopenharmony_ci	ctx->cport = inst;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ret = cal_ctx_v4l2_init(ctx);
6278c2ecf20Sopenharmony_ci	if (ret)
6288c2ecf20Sopenharmony_ci		return NULL;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return ctx;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic const struct of_device_id cal_of_match[] = {
6348c2ecf20Sopenharmony_ci	{
6358c2ecf20Sopenharmony_ci		.compatible = "ti,dra72-cal",
6368c2ecf20Sopenharmony_ci		.data = (void *)&dra72x_cal_data,
6378c2ecf20Sopenharmony_ci	},
6388c2ecf20Sopenharmony_ci	{
6398c2ecf20Sopenharmony_ci		.compatible = "ti,dra72-pre-es2-cal",
6408c2ecf20Sopenharmony_ci		.data = (void *)&dra72x_es1_cal_data,
6418c2ecf20Sopenharmony_ci	},
6428c2ecf20Sopenharmony_ci	{
6438c2ecf20Sopenharmony_ci		.compatible = "ti,dra76-cal",
6448c2ecf20Sopenharmony_ci		.data = (void *)&dra76x_cal_data,
6458c2ecf20Sopenharmony_ci	},
6468c2ecf20Sopenharmony_ci	{
6478c2ecf20Sopenharmony_ci		.compatible = "ti,am654-cal",
6488c2ecf20Sopenharmony_ci		.data = (void *)&am654_cal_data,
6498c2ecf20Sopenharmony_ci	},
6508c2ecf20Sopenharmony_ci	{},
6518c2ecf20Sopenharmony_ci};
6528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cal_of_match);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci/* Get hardware revision and info. */
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci#define CAL_HL_HWINFO_VALUE		0xa3c90469
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic void cal_get_hwinfo(struct cal_dev *cal)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	u32 hwinfo;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	cal->revision = cal_read(cal, CAL_HL_REVISION);
6638c2ecf20Sopenharmony_ci	switch (FIELD_GET(CAL_HL_REVISION_SCHEME_MASK, cal->revision)) {
6648c2ecf20Sopenharmony_ci	case CAL_HL_REVISION_SCHEME_H08:
6658c2ecf20Sopenharmony_ci		cal_dbg(3, cal, "CAL HW revision %lu.%lu.%lu (0x%08x)\n",
6668c2ecf20Sopenharmony_ci			FIELD_GET(CAL_HL_REVISION_MAJOR_MASK, cal->revision),
6678c2ecf20Sopenharmony_ci			FIELD_GET(CAL_HL_REVISION_MINOR_MASK, cal->revision),
6688c2ecf20Sopenharmony_ci			FIELD_GET(CAL_HL_REVISION_RTL_MASK, cal->revision),
6698c2ecf20Sopenharmony_ci			cal->revision);
6708c2ecf20Sopenharmony_ci		break;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	case CAL_HL_REVISION_SCHEME_LEGACY:
6738c2ecf20Sopenharmony_ci	default:
6748c2ecf20Sopenharmony_ci		cal_info(cal, "Unexpected CAL HW revision 0x%08x\n",
6758c2ecf20Sopenharmony_ci			 cal->revision);
6768c2ecf20Sopenharmony_ci		break;
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	hwinfo = cal_read(cal, CAL_HL_HWINFO);
6808c2ecf20Sopenharmony_ci	if (hwinfo != CAL_HL_HWINFO_VALUE)
6818c2ecf20Sopenharmony_ci		cal_info(cal, "CAL_HL_HWINFO = 0x%08x, expected 0x%08x\n",
6828c2ecf20Sopenharmony_ci			 hwinfo, CAL_HL_HWINFO_VALUE);
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic int cal_init_camerarx_regmap(struct cal_dev *cal)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(cal->dev);
6888c2ecf20Sopenharmony_ci	struct device_node *np = cal->dev->of_node;
6898c2ecf20Sopenharmony_ci	struct regmap_config config = { };
6908c2ecf20Sopenharmony_ci	struct regmap *syscon;
6918c2ecf20Sopenharmony_ci	struct resource *res;
6928c2ecf20Sopenharmony_ci	unsigned int offset;
6938c2ecf20Sopenharmony_ci	void __iomem *base;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	syscon = syscon_regmap_lookup_by_phandle_args(np, "ti,camerrx-control",
6968c2ecf20Sopenharmony_ci						      1, &offset);
6978c2ecf20Sopenharmony_ci	if (!IS_ERR(syscon)) {
6988c2ecf20Sopenharmony_ci		cal->syscon_camerrx = syscon;
6998c2ecf20Sopenharmony_ci		cal->syscon_camerrx_offset = offset;
7008c2ecf20Sopenharmony_ci		return 0;
7018c2ecf20Sopenharmony_ci	}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	dev_warn(cal->dev, "failed to get ti,camerrx-control: %ld\n",
7048c2ecf20Sopenharmony_ci		 PTR_ERR(syscon));
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	/*
7078c2ecf20Sopenharmony_ci	 * Backward DTS compatibility. If syscon entry is not present then
7088c2ecf20Sopenharmony_ci	 * check if the camerrx_control resource is present.
7098c2ecf20Sopenharmony_ci	 */
7108c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
7118c2ecf20Sopenharmony_ci					   "camerrx_control");
7128c2ecf20Sopenharmony_ci	base = devm_ioremap_resource(cal->dev, res);
7138c2ecf20Sopenharmony_ci	if (IS_ERR(base)) {
7148c2ecf20Sopenharmony_ci		cal_err(cal, "failed to ioremap camerrx_control\n");
7158c2ecf20Sopenharmony_ci		return PTR_ERR(base);
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
7198c2ecf20Sopenharmony_ci		res->name, &res->start, &res->end);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	config.reg_bits = 32;
7228c2ecf20Sopenharmony_ci	config.reg_stride = 4;
7238c2ecf20Sopenharmony_ci	config.val_bits = 32;
7248c2ecf20Sopenharmony_ci	config.max_register = resource_size(res) - 4;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	syscon = regmap_init_mmio(NULL, base, &config);
7278c2ecf20Sopenharmony_ci	if (IS_ERR(syscon)) {
7288c2ecf20Sopenharmony_ci		pr_err("regmap init failed\n");
7298c2ecf20Sopenharmony_ci		return PTR_ERR(syscon);
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	/*
7338c2ecf20Sopenharmony_ci	 * In this case the base already point to the direct CM register so no
7348c2ecf20Sopenharmony_ci	 * need for an offset.
7358c2ecf20Sopenharmony_ci	 */
7368c2ecf20Sopenharmony_ci	cal->syscon_camerrx = syscon;
7378c2ecf20Sopenharmony_ci	cal->syscon_camerrx_offset = 0;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return 0;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic int cal_probe(struct platform_device *pdev)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct cal_dev *cal;
7458c2ecf20Sopenharmony_ci	struct cal_ctx *ctx;
7468c2ecf20Sopenharmony_ci	bool connected = false;
7478c2ecf20Sopenharmony_ci	unsigned int i;
7488c2ecf20Sopenharmony_ci	int ret;
7498c2ecf20Sopenharmony_ci	int irq;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	cal = devm_kzalloc(&pdev->dev, sizeof(*cal), GFP_KERNEL);
7528c2ecf20Sopenharmony_ci	if (!cal)
7538c2ecf20Sopenharmony_ci		return -ENOMEM;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	cal->data = of_device_get_match_data(&pdev->dev);
7568c2ecf20Sopenharmony_ci	if (!cal->data) {
7578c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not get feature data based on compatible version\n");
7588c2ecf20Sopenharmony_ci		return -ENODEV;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	cal->dev = &pdev->dev;
7628c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, cal);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* Acquire resources: clocks, CAMERARX regmap, I/O memory and IRQ. */
7658c2ecf20Sopenharmony_ci	cal->fclk = devm_clk_get(&pdev->dev, "fck");
7668c2ecf20Sopenharmony_ci	if (IS_ERR(cal->fclk)) {
7678c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot get CAL fclk\n");
7688c2ecf20Sopenharmony_ci		return PTR_ERR(cal->fclk);
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	ret = cal_init_camerarx_regmap(cal);
7728c2ecf20Sopenharmony_ci	if (ret < 0)
7738c2ecf20Sopenharmony_ci		return ret;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	cal->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
7768c2ecf20Sopenharmony_ci						"cal_top");
7778c2ecf20Sopenharmony_ci	cal->base = devm_ioremap_resource(&pdev->dev, cal->res);
7788c2ecf20Sopenharmony_ci	if (IS_ERR(cal->base))
7798c2ecf20Sopenharmony_ci		return PTR_ERR(cal->base);
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	cal_dbg(1, cal, "ioresource %s at %pa - %pa\n",
7828c2ecf20Sopenharmony_ci		cal->res->name, &cal->res->start, &cal->res->end);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
7858c2ecf20Sopenharmony_ci	cal_dbg(1, cal, "got irq# %d\n", irq);
7868c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, irq, cal_irq, 0, CAL_MODULE_NAME,
7878c2ecf20Sopenharmony_ci			       cal);
7888c2ecf20Sopenharmony_ci	if (ret)
7898c2ecf20Sopenharmony_ci		return ret;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/* Read the revision and hardware info to verify hardware access. */
7928c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
7938c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(&pdev->dev);
7948c2ecf20Sopenharmony_ci	if (ret)
7958c2ecf20Sopenharmony_ci		goto error_pm_runtime;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	cal_get_hwinfo(cal);
7988c2ecf20Sopenharmony_ci	pm_runtime_put_sync(&pdev->dev);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	/* Create CAMERARX PHYs. */
8018c2ecf20Sopenharmony_ci	for (i = 0; i < cal->data->num_csi2_phy; ++i) {
8028c2ecf20Sopenharmony_ci		cal->phy[i] = cal_camerarx_create(cal, i);
8038c2ecf20Sopenharmony_ci		if (IS_ERR(cal->phy[i])) {
8048c2ecf20Sopenharmony_ci			ret = PTR_ERR(cal->phy[i]);
8058c2ecf20Sopenharmony_ci			cal->phy[i] = NULL;
8068c2ecf20Sopenharmony_ci			goto error_camerarx;
8078c2ecf20Sopenharmony_ci		}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci		if (cal->phy[i]->sensor_node)
8108c2ecf20Sopenharmony_ci			connected = true;
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	if (!connected) {
8148c2ecf20Sopenharmony_ci		cal_err(cal, "Neither port is configured, no point in staying up\n");
8158c2ecf20Sopenharmony_ci		ret = -ENODEV;
8168c2ecf20Sopenharmony_ci		goto error_camerarx;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	/* Initialize the media device. */
8208c2ecf20Sopenharmony_ci	ret = cal_media_init(cal);
8218c2ecf20Sopenharmony_ci	if (ret < 0)
8228c2ecf20Sopenharmony_ci		goto error_camerarx;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	/* Create contexts. */
8258c2ecf20Sopenharmony_ci	for (i = 0; i < cal->data->num_csi2_phy; ++i) {
8268c2ecf20Sopenharmony_ci		if (!cal->phy[i]->sensor_node)
8278c2ecf20Sopenharmony_ci			continue;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci		cal->ctx[i] = cal_ctx_create(cal, i);
8308c2ecf20Sopenharmony_ci		if (!cal->ctx[i]) {
8318c2ecf20Sopenharmony_ci			cal_err(cal, "Failed to create context %u\n", i);
8328c2ecf20Sopenharmony_ci			ret = -ENODEV;
8338c2ecf20Sopenharmony_ci			goto error_context;
8348c2ecf20Sopenharmony_ci		}
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	/* Register the media device. */
8388c2ecf20Sopenharmony_ci	ret = cal_media_register(cal);
8398c2ecf20Sopenharmony_ci	if (ret)
8408c2ecf20Sopenharmony_ci		goto error_context;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	return 0;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_cierror_context:
8458c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->ctx); i++) {
8468c2ecf20Sopenharmony_ci		ctx = cal->ctx[i];
8478c2ecf20Sopenharmony_ci		if (ctx)
8488c2ecf20Sopenharmony_ci			cal_ctx_v4l2_cleanup(ctx);
8498c2ecf20Sopenharmony_ci	}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	cal_media_cleanup(cal);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cierror_camerarx:
8548c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->phy); i++)
8558c2ecf20Sopenharmony_ci		cal_camerarx_destroy(cal->phy[i]);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_cierror_pm_runtime:
8588c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	return ret;
8618c2ecf20Sopenharmony_ci}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_cistatic int cal_remove(struct platform_device *pdev)
8648c2ecf20Sopenharmony_ci{
8658c2ecf20Sopenharmony_ci	struct cal_dev *cal = platform_get_drvdata(pdev);
8668c2ecf20Sopenharmony_ci	unsigned int i;
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	cal_dbg(1, cal, "Removing %s\n", CAL_MODULE_NAME);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	pm_runtime_get_sync(&pdev->dev);
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	cal_media_unregister(cal);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->phy); i++) {
8758c2ecf20Sopenharmony_ci		if (cal->phy[i])
8768c2ecf20Sopenharmony_ci			cal_camerarx_disable(cal->phy[i]);
8778c2ecf20Sopenharmony_ci	}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	cal_media_cleanup(cal);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cal->phy); i++)
8828c2ecf20Sopenharmony_ci		cal_camerarx_destroy(cal->phy[i]);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	pm_runtime_put_sync(&pdev->dev);
8858c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	return 0;
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_cistatic int cal_runtime_resume(struct device *dev)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	struct cal_dev *cal = dev_get_drvdata(dev);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	if (cal->data->flags & DRA72_CAL_PRE_ES2_LDO_DISABLE) {
8958c2ecf20Sopenharmony_ci		/*
8968c2ecf20Sopenharmony_ci		 * Apply errata on both port everytime we (re-)enable
8978c2ecf20Sopenharmony_ci		 * the clock
8988c2ecf20Sopenharmony_ci		 */
8998c2ecf20Sopenharmony_ci		cal_camerarx_i913_errata(cal->phy[0]);
9008c2ecf20Sopenharmony_ci		cal_camerarx_i913_errata(cal->phy[1]);
9018c2ecf20Sopenharmony_ci	}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	return 0;
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic const struct dev_pm_ops cal_pm_ops = {
9078c2ecf20Sopenharmony_ci	.runtime_resume = cal_runtime_resume,
9088c2ecf20Sopenharmony_ci};
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cistatic struct platform_driver cal_pdrv = {
9118c2ecf20Sopenharmony_ci	.probe		= cal_probe,
9128c2ecf20Sopenharmony_ci	.remove		= cal_remove,
9138c2ecf20Sopenharmony_ci	.driver		= {
9148c2ecf20Sopenharmony_ci		.name	= CAL_MODULE_NAME,
9158c2ecf20Sopenharmony_ci		.pm	= &cal_pm_ops,
9168c2ecf20Sopenharmony_ci		.of_match_table = cal_of_match,
9178c2ecf20Sopenharmony_ci	},
9188c2ecf20Sopenharmony_ci};
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_cimodule_platform_driver(cal_pdrv);
921