18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Microchip Image Sensor Controller (ISC) driver header file
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016-2019 Microchip Technology, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Songjun Wu
88c2ecf20Sopenharmony_ci * Author: Eugen Hristev <eugen.hristev@microchip.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#ifndef _ATMEL_ISC_H_
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define ISC_MAX_SUPPORT_WIDTH   2592
148c2ecf20Sopenharmony_ci#define ISC_MAX_SUPPORT_HEIGHT  1944
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define ISC_CLK_MAX_DIV		255
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cienum isc_clk_id {
198c2ecf20Sopenharmony_ci	ISC_ISPCK = 0,
208c2ecf20Sopenharmony_ci	ISC_MCK = 1,
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct isc_clk {
248c2ecf20Sopenharmony_ci	struct clk_hw   hw;
258c2ecf20Sopenharmony_ci	struct clk      *clk;
268c2ecf20Sopenharmony_ci	struct regmap   *regmap;
278c2ecf20Sopenharmony_ci	spinlock_t	lock;	/* serialize access to clock registers */
288c2ecf20Sopenharmony_ci	u8		id;
298c2ecf20Sopenharmony_ci	u8		parent_id;
308c2ecf20Sopenharmony_ci	u32		div;
318c2ecf20Sopenharmony_ci	struct device	*dev;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define to_isc_clk(v) container_of(v, struct isc_clk, hw)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct isc_buffer {
378c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer  vb;
388c2ecf20Sopenharmony_ci	struct list_head	list;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct isc_subdev_entity {
428c2ecf20Sopenharmony_ci	struct v4l2_subdev		*sd;
438c2ecf20Sopenharmony_ci	struct v4l2_async_subdev	*asd;
448c2ecf20Sopenharmony_ci	struct v4l2_async_notifier      notifier;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	u32 pfe_cfg0;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	struct list_head list;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * struct isc_format - ISC media bus format information
538c2ecf20Sopenharmony_ci			This structure represents the interface between the ISC
548c2ecf20Sopenharmony_ci			and the sensor. It's the input format received by
558c2ecf20Sopenharmony_ci			the ISC.
568c2ecf20Sopenharmony_ci * @fourcc:		Fourcc code for this format
578c2ecf20Sopenharmony_ci * @mbus_code:		V4L2 media bus format code.
588c2ecf20Sopenharmony_ci * @cfa_baycfg:		If this format is RAW BAYER, indicate the type of bayer.
598c2ecf20Sopenharmony_ci			this is either BGBG, RGRG, etc.
608c2ecf20Sopenharmony_ci * @pfe_cfg0_bps:	Number of hardware data lines connected to the ISC
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct isc_format {
648c2ecf20Sopenharmony_ci	u32	fourcc;
658c2ecf20Sopenharmony_ci	u32	mbus_code;
668c2ecf20Sopenharmony_ci	u32	cfa_baycfg;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	bool	sd_support;
698c2ecf20Sopenharmony_ci	u32	pfe_cfg0_bps;
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* Pipeline bitmap */
738c2ecf20Sopenharmony_ci#define WB_ENABLE	BIT(0)
748c2ecf20Sopenharmony_ci#define CFA_ENABLE	BIT(1)
758c2ecf20Sopenharmony_ci#define CC_ENABLE	BIT(2)
768c2ecf20Sopenharmony_ci#define GAM_ENABLE	BIT(3)
778c2ecf20Sopenharmony_ci#define GAM_BENABLE	BIT(4)
788c2ecf20Sopenharmony_ci#define GAM_GENABLE	BIT(5)
798c2ecf20Sopenharmony_ci#define GAM_RENABLE	BIT(6)
808c2ecf20Sopenharmony_ci#define CSC_ENABLE	BIT(7)
818c2ecf20Sopenharmony_ci#define CBC_ENABLE	BIT(8)
828c2ecf20Sopenharmony_ci#define SUB422_ENABLE	BIT(9)
838c2ecf20Sopenharmony_ci#define SUB420_ENABLE	BIT(10)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define GAM_ENABLES	(GAM_RENABLE | GAM_GENABLE | GAM_BENABLE | GAM_ENABLE)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * struct fmt_config - ISC format configuration and internal pipeline
898c2ecf20Sopenharmony_ci			This structure represents the internal configuration
908c2ecf20Sopenharmony_ci			of the ISC.
918c2ecf20Sopenharmony_ci			It also holds the format that ISC will present to v4l2.
928c2ecf20Sopenharmony_ci * @sd_format:		Pointer to an isc_format struct that holds the sensor
938c2ecf20Sopenharmony_ci			configuration.
948c2ecf20Sopenharmony_ci * @fourcc:		Fourcc code for this format.
958c2ecf20Sopenharmony_ci * @bpp:		Bytes per pixel in the current format.
968c2ecf20Sopenharmony_ci * @rlp_cfg_mode:	Configuration of the RLP (rounding, limiting packaging)
978c2ecf20Sopenharmony_ci * @dcfg_imode:		Configuration of the input of the DMA module
988c2ecf20Sopenharmony_ci * @dctrl_dview:	Configuration of the output of the DMA module
998c2ecf20Sopenharmony_ci * @bits_pipeline:	Configuration of the pipeline, which modules are enabled
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistruct fmt_config {
1028c2ecf20Sopenharmony_ci	struct isc_format	*sd_format;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	u32			fourcc;
1058c2ecf20Sopenharmony_ci	u8			bpp;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	u32			rlp_cfg_mode;
1088c2ecf20Sopenharmony_ci	u32			dcfg_imode;
1098c2ecf20Sopenharmony_ci	u32			dctrl_dview;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	u32			bits_pipeline;
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define HIST_ENTRIES		512
1158c2ecf20Sopenharmony_ci#define HIST_BAYER		(ISC_HIS_CFG_MODE_B + 1)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cienum{
1188c2ecf20Sopenharmony_ci	HIST_INIT = 0,
1198c2ecf20Sopenharmony_ci	HIST_ENABLED,
1208c2ecf20Sopenharmony_ci	HIST_DISABLED,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistruct isc_ctrls {
1248c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler handler;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	u32 brightness;
1278c2ecf20Sopenharmony_ci	u32 contrast;
1288c2ecf20Sopenharmony_ci	u8 gamma_index;
1298c2ecf20Sopenharmony_ci#define ISC_WB_NONE	0
1308c2ecf20Sopenharmony_ci#define ISC_WB_AUTO	1
1318c2ecf20Sopenharmony_ci#define ISC_WB_ONETIME	2
1328c2ecf20Sopenharmony_ci	u8 awb;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/* one for each component : GR, R, GB, B */
1358c2ecf20Sopenharmony_ci	u32 gain[HIST_BAYER];
1368c2ecf20Sopenharmony_ci	s32 offset[HIST_BAYER];
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	u32 hist_entry[HIST_ENTRIES];
1398c2ecf20Sopenharmony_ci	u32 hist_count[HIST_BAYER];
1408c2ecf20Sopenharmony_ci	u8 hist_id;
1418c2ecf20Sopenharmony_ci	u8 hist_stat;
1428c2ecf20Sopenharmony_ci#define HIST_MIN_INDEX		0
1438c2ecf20Sopenharmony_ci#define HIST_MAX_INDEX		1
1448c2ecf20Sopenharmony_ci	u32 hist_minmax[HIST_BAYER][2];
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define ISC_PIPE_LINE_NODE_NUM	11
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/*
1508c2ecf20Sopenharmony_ci * struct isc_device - ISC device driver data/config struct
1518c2ecf20Sopenharmony_ci * @regmap:		Register map
1528c2ecf20Sopenharmony_ci * @hclock:		Hclock clock input (refer datasheet)
1538c2ecf20Sopenharmony_ci * @ispck:		iscpck clock (refer datasheet)
1548c2ecf20Sopenharmony_ci * @isc_clks:		ISC clocks
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * @dev:		Registered device driver
1578c2ecf20Sopenharmony_ci * @v4l2_dev:		v4l2 registered device
1588c2ecf20Sopenharmony_ci * @video_dev:		registered video device
1598c2ecf20Sopenharmony_ci *
1608c2ecf20Sopenharmony_ci * @vb2_vidq:		video buffer 2 video queue
1618c2ecf20Sopenharmony_ci * @dma_queue_lock:	lock to serialize the dma buffer queue
1628c2ecf20Sopenharmony_ci * @dma_queue:		the queue for dma buffers
1638c2ecf20Sopenharmony_ci * @cur_frm:		current isc frame/buffer
1648c2ecf20Sopenharmony_ci * @sequence:		current frame number
1658c2ecf20Sopenharmony_ci * @stop:		true if isc is not streaming, false if streaming
1668c2ecf20Sopenharmony_ci * @comp:		completion reference that signals frame completion
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * @fmt:		current v42l format
1698c2ecf20Sopenharmony_ci * @user_formats:	list of formats that are supported and agreed with sd
1708c2ecf20Sopenharmony_ci * @num_user_formats:	how many formats are in user_formats
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * @config:		current ISC format configuration
1738c2ecf20Sopenharmony_ci * @try_config:		the current ISC try format , not yet activated
1748c2ecf20Sopenharmony_ci *
1758c2ecf20Sopenharmony_ci * @ctrls:		holds information about ISC controls
1768c2ecf20Sopenharmony_ci * @do_wb_ctrl:		control regarding the DO_WHITE_BALANCE button
1778c2ecf20Sopenharmony_ci * @awb_work:		workqueue reference for autowhitebalance histogram
1788c2ecf20Sopenharmony_ci *			analysis
1798c2ecf20Sopenharmony_ci *
1808c2ecf20Sopenharmony_ci * @lock:		lock for serializing userspace file operations
1818c2ecf20Sopenharmony_ci *			with ISC operations
1828c2ecf20Sopenharmony_ci * @awb_lock:		lock for serializing awb work queue operations
1838c2ecf20Sopenharmony_ci *			with DMA/buffer operations
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * @pipeline:		configuration of the ISC pipeline
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * @current_subdev:	current subdevice: the sensor
1888c2ecf20Sopenharmony_ci * @subdev_entities:	list of subdevice entitites
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_cistruct isc_device {
1918c2ecf20Sopenharmony_ci	struct regmap		*regmap;
1928c2ecf20Sopenharmony_ci	struct clk		*hclock;
1938c2ecf20Sopenharmony_ci	struct clk		*ispck;
1948c2ecf20Sopenharmony_ci	struct isc_clk		isc_clks[2];
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	struct device		*dev;
1978c2ecf20Sopenharmony_ci	struct v4l2_device	v4l2_dev;
1988c2ecf20Sopenharmony_ci	struct video_device	video_dev;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	struct vb2_queue	vb2_vidq;
2018c2ecf20Sopenharmony_ci	spinlock_t		dma_queue_lock; /* serialize access to dma queue */
2028c2ecf20Sopenharmony_ci	struct list_head	dma_queue;
2038c2ecf20Sopenharmony_ci	struct isc_buffer	*cur_frm;
2048c2ecf20Sopenharmony_ci	unsigned int		sequence;
2058c2ecf20Sopenharmony_ci	bool			stop;
2068c2ecf20Sopenharmony_ci	struct completion	comp;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	struct v4l2_format	fmt;
2098c2ecf20Sopenharmony_ci	struct isc_format	**user_formats;
2108c2ecf20Sopenharmony_ci	unsigned int		num_user_formats;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	struct fmt_config	config;
2138c2ecf20Sopenharmony_ci	struct fmt_config	try_config;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	struct isc_ctrls	ctrls;
2168c2ecf20Sopenharmony_ci	struct work_struct	awb_work;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	struct mutex		lock; /* serialize access to file operations */
2198c2ecf20Sopenharmony_ci	spinlock_t		awb_lock; /* serialize access to DMA buffers from awb work queue */
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	struct regmap_field	*pipeline[ISC_PIPE_LINE_NODE_NUM];
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	struct isc_subdev_entity	*current_subdev;
2248c2ecf20Sopenharmony_ci	struct list_head		subdev_entities;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	struct {
2278c2ecf20Sopenharmony_ci#define ISC_CTRL_DO_WB 1
2288c2ecf20Sopenharmony_ci#define ISC_CTRL_R_GAIN 2
2298c2ecf20Sopenharmony_ci#define ISC_CTRL_B_GAIN 3
2308c2ecf20Sopenharmony_ci#define ISC_CTRL_GR_GAIN 4
2318c2ecf20Sopenharmony_ci#define ISC_CTRL_GB_GAIN 5
2328c2ecf20Sopenharmony_ci#define ISC_CTRL_R_OFF 6
2338c2ecf20Sopenharmony_ci#define ISC_CTRL_B_OFF 7
2348c2ecf20Sopenharmony_ci#define ISC_CTRL_GR_OFF 8
2358c2ecf20Sopenharmony_ci#define ISC_CTRL_GB_OFF 9
2368c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*awb_ctrl;
2378c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*do_wb_ctrl;
2388c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*r_gain_ctrl;
2398c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*b_gain_ctrl;
2408c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*gr_gain_ctrl;
2418c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*gb_gain_ctrl;
2428c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*r_off_ctrl;
2438c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*b_off_ctrl;
2448c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*gr_off_ctrl;
2458c2ecf20Sopenharmony_ci		struct v4l2_ctrl	*gb_off_ctrl;
2468c2ecf20Sopenharmony_ci	};
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci#define GAMMA_MAX	2
2508c2ecf20Sopenharmony_ci#define GAMMA_ENTRIES	64
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci#define ATMEL_ISC_NAME "atmel-isc"
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciextern struct isc_format formats_list[];
2558c2ecf20Sopenharmony_ciextern const struct isc_format controller_formats[];
2568c2ecf20Sopenharmony_ciextern const u32 isc_gamma_table[GAMMA_MAX + 1][GAMMA_ENTRIES];
2578c2ecf20Sopenharmony_ciextern const struct regmap_config isc_regmap_config;
2588c2ecf20Sopenharmony_ciextern const struct v4l2_async_notifier_operations isc_async_ops;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciirqreturn_t isc_interrupt(int irq, void *dev_id);
2618c2ecf20Sopenharmony_ciint isc_pipeline_init(struct isc_device *isc);
2628c2ecf20Sopenharmony_ciint isc_clk_init(struct isc_device *isc);
2638c2ecf20Sopenharmony_civoid isc_subdev_cleanup(struct isc_device *isc);
2648c2ecf20Sopenharmony_civoid isc_clk_cleanup(struct isc_device *isc);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci#endif
267