162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Rockchip ISP1 Driver - Common definitions
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
862306a36Sopenharmony_ci * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef _RKISP1_COMMON_H
1262306a36Sopenharmony_ci#define _RKISP1_COMMON_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/clk.h>
1562306a36Sopenharmony_ci#include <linux/interrupt.h>
1662306a36Sopenharmony_ci#include <linux/mutex.h>
1762306a36Sopenharmony_ci#include <linux/rkisp1-config.h>
1862306a36Sopenharmony_ci#include <media/media-device.h>
1962306a36Sopenharmony_ci#include <media/media-entity.h>
2062306a36Sopenharmony_ci#include <media/v4l2-ctrls.h>
2162306a36Sopenharmony_ci#include <media/v4l2-device.h>
2262306a36Sopenharmony_ci#include <media/videobuf2-v4l2.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "rkisp1-regs.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistruct dentry;
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * flags on the 'direction' field in struct rkisp1_mbus_info' that indicate
3062306a36Sopenharmony_ci * on which pad the media bus format is supported
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#define RKISP1_ISP_SD_SRC			BIT(0)
3362306a36Sopenharmony_ci#define RKISP1_ISP_SD_SINK			BIT(1)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* min and max values for the widths and heights of the entities */
3662306a36Sopenharmony_ci#define RKISP1_ISP_MAX_WIDTH			4032
3762306a36Sopenharmony_ci#define RKISP1_ISP_MAX_HEIGHT			3024
3862306a36Sopenharmony_ci#define RKISP1_ISP_MIN_WIDTH			32
3962306a36Sopenharmony_ci#define RKISP1_ISP_MIN_HEIGHT			32
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define RKISP1_RSZ_MP_SRC_MAX_WIDTH		4416
4262306a36Sopenharmony_ci#define RKISP1_RSZ_MP_SRC_MAX_HEIGHT		3312
4362306a36Sopenharmony_ci#define RKISP1_RSZ_SP_SRC_MAX_WIDTH		1920
4462306a36Sopenharmony_ci#define RKISP1_RSZ_SP_SRC_MAX_HEIGHT		1920
4562306a36Sopenharmony_ci#define RKISP1_RSZ_SRC_MIN_WIDTH		32
4662306a36Sopenharmony_ci#define RKISP1_RSZ_SRC_MIN_HEIGHT		16
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* the default width and height of all the entities */
4962306a36Sopenharmony_ci#define RKISP1_DEFAULT_WIDTH			800
5062306a36Sopenharmony_ci#define RKISP1_DEFAULT_HEIGHT			600
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define RKISP1_DRIVER_NAME			"rkisp1"
5362306a36Sopenharmony_ci#define RKISP1_BUS_INFO				"platform:" RKISP1_DRIVER_NAME
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* maximum number of clocks */
5662306a36Sopenharmony_ci#define RKISP1_MAX_BUS_CLK			8
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/* a bitmask of the ready stats */
5962306a36Sopenharmony_ci#define RKISP1_STATS_MEAS_MASK			(RKISP1_CIF_ISP_AWB_DONE |	\
6062306a36Sopenharmony_ci						 RKISP1_CIF_ISP_AFM_FIN |	\
6162306a36Sopenharmony_ci						 RKISP1_CIF_ISP_EXP_END |	\
6262306a36Sopenharmony_ci						 RKISP1_CIF_ISP_HIST_MEASURE_RDY)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* IRQ lines */
6562306a36Sopenharmony_cienum rkisp1_irq_line {
6662306a36Sopenharmony_ci	RKISP1_IRQ_ISP = 0,
6762306a36Sopenharmony_ci	RKISP1_IRQ_MI,
6862306a36Sopenharmony_ci	RKISP1_IRQ_MIPI,
6962306a36Sopenharmony_ci	RKISP1_NUM_IRQS,
7062306a36Sopenharmony_ci};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* enum for the resizer pads */
7362306a36Sopenharmony_cienum rkisp1_rsz_pad {
7462306a36Sopenharmony_ci	RKISP1_RSZ_PAD_SINK,
7562306a36Sopenharmony_ci	RKISP1_RSZ_PAD_SRC,
7662306a36Sopenharmony_ci	RKISP1_RSZ_PAD_MAX
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/* enum for the csi receiver pads */
8062306a36Sopenharmony_cienum rkisp1_csi_pad {
8162306a36Sopenharmony_ci	RKISP1_CSI_PAD_SINK,
8262306a36Sopenharmony_ci	RKISP1_CSI_PAD_SRC,
8362306a36Sopenharmony_ci	RKISP1_CSI_PAD_NUM
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/* enum for the capture id */
8762306a36Sopenharmony_cienum rkisp1_stream_id {
8862306a36Sopenharmony_ci	RKISP1_MAINPATH,
8962306a36Sopenharmony_ci	RKISP1_SELFPATH,
9062306a36Sopenharmony_ci};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/* bayer patterns */
9362306a36Sopenharmony_cienum rkisp1_fmt_raw_pat_type {
9462306a36Sopenharmony_ci	RKISP1_RAW_RGGB = 0,
9562306a36Sopenharmony_ci	RKISP1_RAW_GRBG,
9662306a36Sopenharmony_ci	RKISP1_RAW_GBRG,
9762306a36Sopenharmony_ci	RKISP1_RAW_BGGR,
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* enum for the isp pads */
10162306a36Sopenharmony_cienum rkisp1_isp_pad {
10262306a36Sopenharmony_ci	RKISP1_ISP_PAD_SINK_VIDEO,
10362306a36Sopenharmony_ci	RKISP1_ISP_PAD_SINK_PARAMS,
10462306a36Sopenharmony_ci	RKISP1_ISP_PAD_SOURCE_VIDEO,
10562306a36Sopenharmony_ci	RKISP1_ISP_PAD_SOURCE_STATS,
10662306a36Sopenharmony_ci	RKISP1_ISP_PAD_MAX
10762306a36Sopenharmony_ci};
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/*
11062306a36Sopenharmony_ci * enum rkisp1_feature - ISP features
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci * @RKISP1_FEATURE_MIPI_CSI2: The ISP has an internal MIPI CSI-2 receiver
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * The ISP features are stored in a bitmask in &rkisp1_info.features and allow
11562306a36Sopenharmony_ci * the driver to implement support for features present in some ISP versions
11662306a36Sopenharmony_ci * only.
11762306a36Sopenharmony_ci */
11862306a36Sopenharmony_cienum rkisp1_feature {
11962306a36Sopenharmony_ci	RKISP1_FEATURE_MIPI_CSI2 = BIT(0),
12062306a36Sopenharmony_ci};
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci/*
12362306a36Sopenharmony_ci * struct rkisp1_info - Model-specific ISP Information
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * @clks: array of ISP clock names
12662306a36Sopenharmony_ci * @clk_size: number of entries in the @clks array
12762306a36Sopenharmony_ci * @isrs: array of ISP interrupt descriptors
12862306a36Sopenharmony_ci * @isr_size: number of entries in the @isrs array
12962306a36Sopenharmony_ci * @isp_ver: ISP version
13062306a36Sopenharmony_ci * @features: bitmask of rkisp1_feature features implemented by the ISP
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * This structure contains information about the ISP specific to a particular
13362306a36Sopenharmony_ci * ISP model, version, or integration in a particular SoC.
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_cistruct rkisp1_info {
13662306a36Sopenharmony_ci	const char * const *clks;
13762306a36Sopenharmony_ci	unsigned int clk_size;
13862306a36Sopenharmony_ci	const struct rkisp1_isr_data *isrs;
13962306a36Sopenharmony_ci	unsigned int isr_size;
14062306a36Sopenharmony_ci	enum rkisp1_cif_isp_version isp_ver;
14162306a36Sopenharmony_ci	unsigned int features;
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier
14662306a36Sopenharmony_ci *				of the v4l2-async API
14762306a36Sopenharmony_ci *
14862306a36Sopenharmony_ci * @asd:		async_subdev variable for the sensor
14962306a36Sopenharmony_ci * @index:		index of the sensor (counting sensor found in DT)
15062306a36Sopenharmony_ci * @source_ep:		fwnode for the sensor source endpoint
15162306a36Sopenharmony_ci * @lanes:		number of lanes
15262306a36Sopenharmony_ci * @mbus_type:		type of bus (currently only CSI2 is supported)
15362306a36Sopenharmony_ci * @mbus_flags:		media bus (V4L2_MBUS_*) flags
15462306a36Sopenharmony_ci * @sd:			a pointer to v4l2_subdev struct of the sensor
15562306a36Sopenharmony_ci * @pixel_rate_ctrl:	pixel rate of the sensor, used to initialize the phy
15662306a36Sopenharmony_ci * @port:		port number (0: MIPI, 1: Parallel)
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_cistruct rkisp1_sensor_async {
15962306a36Sopenharmony_ci	struct v4l2_async_connection asd;
16062306a36Sopenharmony_ci	unsigned int index;
16162306a36Sopenharmony_ci	struct fwnode_handle *source_ep;
16262306a36Sopenharmony_ci	unsigned int lanes;
16362306a36Sopenharmony_ci	enum v4l2_mbus_type mbus_type;
16462306a36Sopenharmony_ci	unsigned int mbus_flags;
16562306a36Sopenharmony_ci	struct v4l2_subdev *sd;
16662306a36Sopenharmony_ci	struct v4l2_ctrl *pixel_rate_ctrl;
16762306a36Sopenharmony_ci	unsigned int port;
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci/*
17162306a36Sopenharmony_ci * struct rkisp1_csi - CSI receiver subdev
17262306a36Sopenharmony_ci *
17362306a36Sopenharmony_ci * @rkisp1: pointer to the rkisp1 device
17462306a36Sopenharmony_ci * @dphy: a pointer to the phy
17562306a36Sopenharmony_ci * @is_dphy_errctrl_disabled: if dphy errctrl is disabled (avoid endless interrupt)
17662306a36Sopenharmony_ci * @sd: v4l2_subdev variable
17762306a36Sopenharmony_ci * @pads: media pads
17862306a36Sopenharmony_ci * @pad_cfg: configurations for the pads
17962306a36Sopenharmony_ci * @sink_fmt: input format
18062306a36Sopenharmony_ci * @lock: protects pad_cfg and sink_fmt
18162306a36Sopenharmony_ci * @source: source in-use, set when starting streaming
18262306a36Sopenharmony_ci */
18362306a36Sopenharmony_cistruct rkisp1_csi {
18462306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
18562306a36Sopenharmony_ci	struct phy *dphy;
18662306a36Sopenharmony_ci	bool is_dphy_errctrl_disabled;
18762306a36Sopenharmony_ci	struct v4l2_subdev sd;
18862306a36Sopenharmony_ci	struct media_pad pads[RKISP1_CSI_PAD_NUM];
18962306a36Sopenharmony_ci	struct v4l2_subdev_pad_config pad_cfg[RKISP1_CSI_PAD_NUM];
19062306a36Sopenharmony_ci	const struct rkisp1_mbus_info *sink_fmt;
19162306a36Sopenharmony_ci	struct mutex lock;
19262306a36Sopenharmony_ci	struct v4l2_subdev *source;
19362306a36Sopenharmony_ci};
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci/*
19662306a36Sopenharmony_ci * struct rkisp1_isp - ISP subdev entity
19762306a36Sopenharmony_ci *
19862306a36Sopenharmony_ci * @sd:				v4l2_subdev variable
19962306a36Sopenharmony_ci * @rkisp1:			pointer to rkisp1_device
20062306a36Sopenharmony_ci * @pads:			media pads
20162306a36Sopenharmony_ci * @pad_cfg:			pads configurations
20262306a36Sopenharmony_ci * @sink_fmt:			input format
20362306a36Sopenharmony_ci * @src_fmt:			output format
20462306a36Sopenharmony_ci * @ops_lock:			ops serialization
20562306a36Sopenharmony_ci * @frame_sequence:		used to synchronize frame_id between video devices.
20662306a36Sopenharmony_ci */
20762306a36Sopenharmony_cistruct rkisp1_isp {
20862306a36Sopenharmony_ci	struct v4l2_subdev sd;
20962306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
21062306a36Sopenharmony_ci	struct media_pad pads[RKISP1_ISP_PAD_MAX];
21162306a36Sopenharmony_ci	struct v4l2_subdev_pad_config pad_cfg[RKISP1_ISP_PAD_MAX];
21262306a36Sopenharmony_ci	const struct rkisp1_mbus_info *sink_fmt;
21362306a36Sopenharmony_ci	const struct rkisp1_mbus_info *src_fmt;
21462306a36Sopenharmony_ci	struct mutex ops_lock; /* serialize the subdevice ops */
21562306a36Sopenharmony_ci	__u32 frame_sequence;
21662306a36Sopenharmony_ci};
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/*
21962306a36Sopenharmony_ci * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath
22062306a36Sopenharmony_ci *
22162306a36Sopenharmony_ci * @buf_queue:	queue of buffers
22262306a36Sopenharmony_ci * @vlock:	lock of the video node
22362306a36Sopenharmony_ci * @vdev:	video node
22462306a36Sopenharmony_ci * @pad:	media pad
22562306a36Sopenharmony_ci */
22662306a36Sopenharmony_cistruct rkisp1_vdev_node {
22762306a36Sopenharmony_ci	struct vb2_queue buf_queue;
22862306a36Sopenharmony_ci	struct mutex vlock; /* ioctl serialization mutex */
22962306a36Sopenharmony_ci	struct video_device vdev;
23062306a36Sopenharmony_ci	struct media_pad pad;
23162306a36Sopenharmony_ci};
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci/*
23462306a36Sopenharmony_ci * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices:
23562306a36Sopenharmony_ci *			  params, stats, mainpath, selfpath
23662306a36Sopenharmony_ci *
23762306a36Sopenharmony_ci * @vb:		vb2 buffer
23862306a36Sopenharmony_ci * @queue:	entry of the buffer in the queue
23962306a36Sopenharmony_ci * @buff_addr:	dma addresses of each plane, used only by the capture devices: selfpath, mainpath
24062306a36Sopenharmony_ci */
24162306a36Sopenharmony_cistruct rkisp1_buffer {
24262306a36Sopenharmony_ci	struct vb2_v4l2_buffer vb;
24362306a36Sopenharmony_ci	struct list_head queue;
24462306a36Sopenharmony_ci	u32 buff_addr[VIDEO_MAX_PLANES];
24562306a36Sopenharmony_ci};
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci/*
24862306a36Sopenharmony_ci * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case
24962306a36Sopenharmony_ci *				there are no vb2 buffers available.
25062306a36Sopenharmony_ci *
25162306a36Sopenharmony_ci * @vaddr:	return value of call to dma_alloc_attrs.
25262306a36Sopenharmony_ci * @dma_addr:	dma address of the buffer.
25362306a36Sopenharmony_ci * @size:	size of the buffer.
25462306a36Sopenharmony_ci */
25562306a36Sopenharmony_cistruct rkisp1_dummy_buffer {
25662306a36Sopenharmony_ci	void *vaddr;
25762306a36Sopenharmony_ci	dma_addr_t dma_addr;
25862306a36Sopenharmony_ci	u32 size;
25962306a36Sopenharmony_ci};
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistruct rkisp1_device;
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci/*
26462306a36Sopenharmony_ci * struct rkisp1_capture - ISP capture video device
26562306a36Sopenharmony_ci *
26662306a36Sopenharmony_ci * @vnode:	  video node
26762306a36Sopenharmony_ci * @rkisp1:	  pointer to rkisp1_device
26862306a36Sopenharmony_ci * @id:		  id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH
26962306a36Sopenharmony_ci * @ops:	  list of callbacks to configure the capture device.
27062306a36Sopenharmony_ci * @config:	  a pointer to the list of registers to configure the capture format.
27162306a36Sopenharmony_ci * @is_streaming: device is streaming
27262306a36Sopenharmony_ci * @is_stopping:  stop_streaming callback was called and the device is in the process of
27362306a36Sopenharmony_ci *		  stopping the streaming.
27462306a36Sopenharmony_ci * @done:	  when stop_streaming callback is called, the device waits for the next irq
27562306a36Sopenharmony_ci *		  handler to stop the streaming by waiting on the 'done' wait queue.
27662306a36Sopenharmony_ci *		  If the irq handler is not called, the stream is stopped by the callback
27762306a36Sopenharmony_ci *		  after timeout.
27862306a36Sopenharmony_ci * @sp_y_stride:  the selfpath allows to configure a y stride that is longer than the image width.
27962306a36Sopenharmony_ci * @buf.lock:	  lock to protect buf.queue
28062306a36Sopenharmony_ci * @buf.queue:	  queued buffer list
28162306a36Sopenharmony_ci * @buf.dummy:	  dummy space to store dropped data
28262306a36Sopenharmony_ci *
28362306a36Sopenharmony_ci * rkisp1 uses shadow registers, so it needs two buffers at a time
28462306a36Sopenharmony_ci * @buf.curr:	  the buffer used for current frame
28562306a36Sopenharmony_ci * @buf.next:	  the buffer used for next frame
28662306a36Sopenharmony_ci * @pix.cfg:	  pixel configuration
28762306a36Sopenharmony_ci * @pix.info:	  a pointer to the v4l2_format_info of the pixel format
28862306a36Sopenharmony_ci * @pix.fmt:	  buffer format
28962306a36Sopenharmony_ci */
29062306a36Sopenharmony_cistruct rkisp1_capture {
29162306a36Sopenharmony_ci	struct rkisp1_vdev_node vnode;
29262306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
29362306a36Sopenharmony_ci	enum rkisp1_stream_id id;
29462306a36Sopenharmony_ci	const struct rkisp1_capture_ops *ops;
29562306a36Sopenharmony_ci	const struct rkisp1_capture_config *config;
29662306a36Sopenharmony_ci	bool is_streaming;
29762306a36Sopenharmony_ci	bool is_stopping;
29862306a36Sopenharmony_ci	wait_queue_head_t done;
29962306a36Sopenharmony_ci	unsigned int sp_y_stride;
30062306a36Sopenharmony_ci	struct {
30162306a36Sopenharmony_ci		/* protects queue, curr and next */
30262306a36Sopenharmony_ci		spinlock_t lock;
30362306a36Sopenharmony_ci		struct list_head queue;
30462306a36Sopenharmony_ci		struct rkisp1_dummy_buffer dummy;
30562306a36Sopenharmony_ci		struct rkisp1_buffer *curr;
30662306a36Sopenharmony_ci		struct rkisp1_buffer *next;
30762306a36Sopenharmony_ci	} buf;
30862306a36Sopenharmony_ci	struct {
30962306a36Sopenharmony_ci		const struct rkisp1_capture_fmt_cfg *cfg;
31062306a36Sopenharmony_ci		const struct v4l2_format_info *info;
31162306a36Sopenharmony_ci		struct v4l2_pix_format_mplane fmt;
31262306a36Sopenharmony_ci	} pix;
31362306a36Sopenharmony_ci};
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_cistruct rkisp1_stats;
31662306a36Sopenharmony_cistruct rkisp1_stats_ops {
31762306a36Sopenharmony_ci	void (*get_awb_meas)(struct rkisp1_stats *stats,
31862306a36Sopenharmony_ci			     struct rkisp1_stat_buffer *pbuf);
31962306a36Sopenharmony_ci	void (*get_aec_meas)(struct rkisp1_stats *stats,
32062306a36Sopenharmony_ci			     struct rkisp1_stat_buffer *pbuf);
32162306a36Sopenharmony_ci	void (*get_hst_meas)(struct rkisp1_stats *stats,
32262306a36Sopenharmony_ci			     struct rkisp1_stat_buffer *pbuf);
32362306a36Sopenharmony_ci};
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci/*
32662306a36Sopenharmony_ci * struct rkisp1_stats - ISP Statistics device
32762306a36Sopenharmony_ci *
32862306a36Sopenharmony_ci * @vnode:	  video node
32962306a36Sopenharmony_ci * @rkisp1:	  pointer to the rkisp1 device
33062306a36Sopenharmony_ci * @lock:	  locks the buffer list 'stat'
33162306a36Sopenharmony_ci * @stat:	  queue of rkisp1_buffer
33262306a36Sopenharmony_ci * @vdev_fmt:	  v4l2_format of the metadata format
33362306a36Sopenharmony_ci */
33462306a36Sopenharmony_cistruct rkisp1_stats {
33562306a36Sopenharmony_ci	struct rkisp1_vdev_node vnode;
33662306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
33762306a36Sopenharmony_ci	const struct rkisp1_stats_ops *ops;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	spinlock_t lock; /* locks the buffers list 'stats' */
34062306a36Sopenharmony_ci	struct list_head stat;
34162306a36Sopenharmony_ci	struct v4l2_format vdev_fmt;
34262306a36Sopenharmony_ci};
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_cistruct rkisp1_params;
34562306a36Sopenharmony_cistruct rkisp1_params_ops {
34662306a36Sopenharmony_ci	void (*lsc_matrix_config)(struct rkisp1_params *params,
34762306a36Sopenharmony_ci				  const struct rkisp1_cif_isp_lsc_config *pconfig);
34862306a36Sopenharmony_ci	void (*goc_config)(struct rkisp1_params *params,
34962306a36Sopenharmony_ci			   const struct rkisp1_cif_isp_goc_config *arg);
35062306a36Sopenharmony_ci	void (*awb_meas_config)(struct rkisp1_params *params,
35162306a36Sopenharmony_ci				const struct rkisp1_cif_isp_awb_meas_config *arg);
35262306a36Sopenharmony_ci	void (*awb_meas_enable)(struct rkisp1_params *params,
35362306a36Sopenharmony_ci				const struct rkisp1_cif_isp_awb_meas_config *arg,
35462306a36Sopenharmony_ci				bool en);
35562306a36Sopenharmony_ci	void (*awb_gain_config)(struct rkisp1_params *params,
35662306a36Sopenharmony_ci				const struct rkisp1_cif_isp_awb_gain_config *arg);
35762306a36Sopenharmony_ci	void (*aec_config)(struct rkisp1_params *params,
35862306a36Sopenharmony_ci			   const struct rkisp1_cif_isp_aec_config *arg);
35962306a36Sopenharmony_ci	void (*hst_config)(struct rkisp1_params *params,
36062306a36Sopenharmony_ci			   const struct rkisp1_cif_isp_hst_config *arg);
36162306a36Sopenharmony_ci	void (*hst_enable)(struct rkisp1_params *params,
36262306a36Sopenharmony_ci			   const struct rkisp1_cif_isp_hst_config *arg, bool en);
36362306a36Sopenharmony_ci	void (*afm_config)(struct rkisp1_params *params,
36462306a36Sopenharmony_ci			   const struct rkisp1_cif_isp_afc_config *arg);
36562306a36Sopenharmony_ci};
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/*
36862306a36Sopenharmony_ci * struct rkisp1_params - ISP input parameters device
36962306a36Sopenharmony_ci *
37062306a36Sopenharmony_ci * @vnode:		video node
37162306a36Sopenharmony_ci * @rkisp1:		pointer to the rkisp1 device
37262306a36Sopenharmony_ci * @ops:		pointer to the variant-specific operations
37362306a36Sopenharmony_ci * @config_lock:	locks the buffer list 'params'
37462306a36Sopenharmony_ci * @params:		queue of rkisp1_buffer
37562306a36Sopenharmony_ci * @vdev_fmt:		v4l2_format of the metadata format
37662306a36Sopenharmony_ci * @quantization:	the quantization configured on the isp's src pad
37762306a36Sopenharmony_ci * @raw_type:		the bayer pattern on the isp video sink pad
37862306a36Sopenharmony_ci */
37962306a36Sopenharmony_cistruct rkisp1_params {
38062306a36Sopenharmony_ci	struct rkisp1_vdev_node vnode;
38162306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
38262306a36Sopenharmony_ci	const struct rkisp1_params_ops *ops;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	spinlock_t config_lock; /* locks the buffers list 'params' */
38562306a36Sopenharmony_ci	struct list_head params;
38662306a36Sopenharmony_ci	struct v4l2_format vdev_fmt;
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	enum v4l2_quantization quantization;
38962306a36Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_encoding;
39062306a36Sopenharmony_ci	enum rkisp1_fmt_raw_pat_type raw_type;
39162306a36Sopenharmony_ci};
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci/*
39462306a36Sopenharmony_ci * struct rkisp1_resizer - Resizer subdev
39562306a36Sopenharmony_ci *
39662306a36Sopenharmony_ci * @sd:	       v4l2_subdev variable
39762306a36Sopenharmony_ci * @regs_base: base register address offset
39862306a36Sopenharmony_ci * @id:	       id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
39962306a36Sopenharmony_ci * @rkisp1:    pointer to the rkisp1 device
40062306a36Sopenharmony_ci * @pads:      media pads
40162306a36Sopenharmony_ci * @pad_cfg:   configurations for the pads
40262306a36Sopenharmony_ci * @config:    the set of registers to configure the resizer
40362306a36Sopenharmony_ci * @pixel_enc: pixel encoding of the resizer
40462306a36Sopenharmony_ci * @ops_lock:  a lock for the subdev ops
40562306a36Sopenharmony_ci */
40662306a36Sopenharmony_cistruct rkisp1_resizer {
40762306a36Sopenharmony_ci	struct v4l2_subdev sd;
40862306a36Sopenharmony_ci	u32 regs_base;
40962306a36Sopenharmony_ci	enum rkisp1_stream_id id;
41062306a36Sopenharmony_ci	struct rkisp1_device *rkisp1;
41162306a36Sopenharmony_ci	struct media_pad pads[RKISP1_RSZ_PAD_MAX];
41262306a36Sopenharmony_ci	struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX];
41362306a36Sopenharmony_ci	const struct rkisp1_rsz_config *config;
41462306a36Sopenharmony_ci	enum v4l2_pixel_encoding pixel_enc;
41562306a36Sopenharmony_ci	struct mutex ops_lock; /* serialize the subdevice ops */
41662306a36Sopenharmony_ci};
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci/*
41962306a36Sopenharmony_ci * struct rkisp1_debug - Values to be exposed on debugfs.
42062306a36Sopenharmony_ci *			 The parameters are counters of the number of times the
42162306a36Sopenharmony_ci *			 event occurred since the driver was loaded.
42262306a36Sopenharmony_ci *
42362306a36Sopenharmony_ci * @data_loss:			  loss of data occurred within a line, processing failure
42462306a36Sopenharmony_ci * @outform_size_error:		  size error is generated in outmux submodule
42562306a36Sopenharmony_ci * @img_stabilization_size_error: size error is generated in image stabilization submodule
42662306a36Sopenharmony_ci * @inform_size_err:		  size error is generated in inform submodule
42762306a36Sopenharmony_ci * @mipi_error:			  mipi error occurred
42862306a36Sopenharmony_ci * @stats_error:		  writing to the 'Interrupt clear register' did not clear
42962306a36Sopenharmony_ci *				  it in the register 'Masked interrupt status'
43062306a36Sopenharmony_ci * @stop_timeout:		  upon stream stop, the capture waits 1 second for the isr to stop
43162306a36Sopenharmony_ci *				  the stream. This param is incremented in case of timeout.
43262306a36Sopenharmony_ci * @frame_drop:			  a frame was ready but the buffer queue was empty so the frame
43362306a36Sopenharmony_ci *				  was not sent to userspace
43462306a36Sopenharmony_ci */
43562306a36Sopenharmony_cistruct rkisp1_debug {
43662306a36Sopenharmony_ci	struct dentry *debugfs_dir;
43762306a36Sopenharmony_ci	unsigned long data_loss;
43862306a36Sopenharmony_ci	unsigned long outform_size_error;
43962306a36Sopenharmony_ci	unsigned long img_stabilization_size_error;
44062306a36Sopenharmony_ci	unsigned long inform_size_error;
44162306a36Sopenharmony_ci	unsigned long irq_delay;
44262306a36Sopenharmony_ci	unsigned long mipi_error;
44362306a36Sopenharmony_ci	unsigned long stats_error;
44462306a36Sopenharmony_ci	unsigned long stop_timeout[2];
44562306a36Sopenharmony_ci	unsigned long frame_drop[2];
44662306a36Sopenharmony_ci};
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci/*
44962306a36Sopenharmony_ci * struct rkisp1_device - ISP platform device
45062306a36Sopenharmony_ci *
45162306a36Sopenharmony_ci * @base_addr:	   base register address
45262306a36Sopenharmony_ci * @dev:	   a pointer to the struct device
45362306a36Sopenharmony_ci * @clk_size:	   number of clocks
45462306a36Sopenharmony_ci * @clks:	   array of clocks
45562306a36Sopenharmony_ci * @v4l2_dev:	   v4l2_device variable
45662306a36Sopenharmony_ci * @media_dev:	   media_device variable
45762306a36Sopenharmony_ci * @notifier:	   a notifier to register on the v4l2-async API to be notified on the sensor
45862306a36Sopenharmony_ci * @source:        source subdev in-use, set when starting streaming
45962306a36Sopenharmony_ci * @csi:	   internal CSI-2 receiver
46062306a36Sopenharmony_ci * @isp:	   ISP sub-device
46162306a36Sopenharmony_ci * @resizer_devs:  resizer sub-devices
46262306a36Sopenharmony_ci * @capture_devs:  capture devices
46362306a36Sopenharmony_ci * @stats:	   ISP statistics metadata capture device
46462306a36Sopenharmony_ci * @params:	   ISP parameters metadata output device
46562306a36Sopenharmony_ci * @pipe:	   media pipeline
46662306a36Sopenharmony_ci * @stream_lock:   serializes {start/stop}_streaming callbacks between the capture devices.
46762306a36Sopenharmony_ci * @debug:	   debug params to be exposed on debugfs
46862306a36Sopenharmony_ci * @info:	   version-specific ISP information
46962306a36Sopenharmony_ci * @irqs:          IRQ line numbers
47062306a36Sopenharmony_ci * @irqs_enabled:  the hardware is enabled and can cause interrupts
47162306a36Sopenharmony_ci */
47262306a36Sopenharmony_cistruct rkisp1_device {
47362306a36Sopenharmony_ci	void __iomem *base_addr;
47462306a36Sopenharmony_ci	struct device *dev;
47562306a36Sopenharmony_ci	unsigned int clk_size;
47662306a36Sopenharmony_ci	struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
47762306a36Sopenharmony_ci	struct v4l2_device v4l2_dev;
47862306a36Sopenharmony_ci	struct media_device media_dev;
47962306a36Sopenharmony_ci	struct v4l2_async_notifier notifier;
48062306a36Sopenharmony_ci	struct v4l2_subdev *source;
48162306a36Sopenharmony_ci	struct rkisp1_csi csi;
48262306a36Sopenharmony_ci	struct rkisp1_isp isp;
48362306a36Sopenharmony_ci	struct rkisp1_resizer resizer_devs[2];
48462306a36Sopenharmony_ci	struct rkisp1_capture capture_devs[2];
48562306a36Sopenharmony_ci	struct rkisp1_stats stats;
48662306a36Sopenharmony_ci	struct rkisp1_params params;
48762306a36Sopenharmony_ci	struct media_pipeline pipe;
48862306a36Sopenharmony_ci	struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
48962306a36Sopenharmony_ci	struct rkisp1_debug debug;
49062306a36Sopenharmony_ci	const struct rkisp1_info *info;
49162306a36Sopenharmony_ci	int irqs[RKISP1_NUM_IRQS];
49262306a36Sopenharmony_ci	bool irqs_enabled;
49362306a36Sopenharmony_ci};
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci/*
49662306a36Sopenharmony_ci * struct rkisp1_mbus_info - ISP media bus info, Translates media bus code to hardware
49762306a36Sopenharmony_ci *			     format values
49862306a36Sopenharmony_ci *
49962306a36Sopenharmony_ci * @mbus_code: media bus code
50062306a36Sopenharmony_ci * @pixel_enc: pixel encoding
50162306a36Sopenharmony_ci * @mipi_dt:   mipi data type
50262306a36Sopenharmony_ci * @yuv_seq:   the order of the Y, Cb, Cr values
50362306a36Sopenharmony_ci * @bus_width: bus width
50462306a36Sopenharmony_ci * @bayer_pat: bayer pattern
50562306a36Sopenharmony_ci * @direction: a bitmask of the flags indicating on which pad the format is supported on
50662306a36Sopenharmony_ci */
50762306a36Sopenharmony_cistruct rkisp1_mbus_info {
50862306a36Sopenharmony_ci	u32 mbus_code;
50962306a36Sopenharmony_ci	enum v4l2_pixel_encoding pixel_enc;
51062306a36Sopenharmony_ci	u32 mipi_dt;
51162306a36Sopenharmony_ci	u32 yuv_seq;
51262306a36Sopenharmony_ci	u8 bus_width;
51362306a36Sopenharmony_ci	enum rkisp1_fmt_raw_pat_type bayer_pat;
51462306a36Sopenharmony_ci	unsigned int direction;
51562306a36Sopenharmony_ci};
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_cistatic inline void
51862306a36Sopenharmony_cirkisp1_write(struct rkisp1_device *rkisp1, unsigned int addr, u32 val)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	writel(val, rkisp1->base_addr + addr);
52162306a36Sopenharmony_ci}
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_cistatic inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
52462306a36Sopenharmony_ci{
52562306a36Sopenharmony_ci	return readl(rkisp1->base_addr + addr);
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci/*
52962306a36Sopenharmony_ci * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
53062306a36Sopenharmony_ci *				of the capture entity. This is used to enumerate the supported
53162306a36Sopenharmony_ci *				mbus codes on the source pad of the resizer.
53262306a36Sopenharmony_ci *
53362306a36Sopenharmony_ci * @cap:  the capture entity
53462306a36Sopenharmony_ci * @code: the mbus code, the function reads the code->index and fills the code->code
53562306a36Sopenharmony_ci */
53662306a36Sopenharmony_ciint rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
53762306a36Sopenharmony_ci			       struct v4l2_subdev_mbus_code_enum *code);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci/*
54062306a36Sopenharmony_ci * rkisp1_mbus_info_get_by_index - Retrieve the ith supported mbus info
54162306a36Sopenharmony_ci *
54262306a36Sopenharmony_ci * @index: index of the mbus info to fetch
54362306a36Sopenharmony_ci */
54462306a36Sopenharmony_ciconst struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_index(unsigned int index);
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci/*
54762306a36Sopenharmony_ci * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
54862306a36Sopenharmony_ci *
54962306a36Sopenharmony_ci * @crop:   rectangle to adjust.
55062306a36Sopenharmony_ci * @bounds: rectangle used as bounds.
55162306a36Sopenharmony_ci */
55262306a36Sopenharmony_civoid rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
55362306a36Sopenharmony_ci				const struct v4l2_rect *bounds);
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci/*
55662306a36Sopenharmony_ci * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
55762306a36Sopenharmony_ci *
55862306a36Sopenharmony_ci * @crop:   rectangle to adjust.
55962306a36Sopenharmony_ci * @bounds: media bus format used as bounds.
56062306a36Sopenharmony_ci */
56162306a36Sopenharmony_civoid rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
56262306a36Sopenharmony_ci			   const struct v4l2_mbus_framefmt *bounds);
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci/*
56562306a36Sopenharmony_ci * rkisp1_mbus_info_get_by_code - get the isp info of the media bus code
56662306a36Sopenharmony_ci *
56762306a36Sopenharmony_ci * @mbus_code: the media bus code
56862306a36Sopenharmony_ci */
56962306a36Sopenharmony_ciconst struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_code(u32 mbus_code);
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci/*
57262306a36Sopenharmony_ci * rkisp1_params_pre_configure - Configure the params before stream start
57362306a36Sopenharmony_ci *
57462306a36Sopenharmony_ci * @params:	  pointer to rkisp1_params
57562306a36Sopenharmony_ci * @bayer_pat:	  the bayer pattern on the isp video sink pad
57662306a36Sopenharmony_ci * @quantization: the quantization configured on the isp's src pad
57762306a36Sopenharmony_ci * @ycbcr_encoding: the ycbcr_encoding configured on the isp's src pad
57862306a36Sopenharmony_ci *
57962306a36Sopenharmony_ci * This function is called by the ISP entity just before the ISP gets started.
58062306a36Sopenharmony_ci * It applies the initial ISP parameters from the first params buffer, but
58162306a36Sopenharmony_ci * skips LSC as it needs to be configured after the ISP is started.
58262306a36Sopenharmony_ci */
58362306a36Sopenharmony_civoid rkisp1_params_pre_configure(struct rkisp1_params *params,
58462306a36Sopenharmony_ci				 enum rkisp1_fmt_raw_pat_type bayer_pat,
58562306a36Sopenharmony_ci				 enum v4l2_quantization quantization,
58662306a36Sopenharmony_ci				 enum v4l2_ycbcr_encoding ycbcr_encoding);
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci/*
58962306a36Sopenharmony_ci * rkisp1_params_post_configure - Configure the params after stream start
59062306a36Sopenharmony_ci *
59162306a36Sopenharmony_ci * @params:	  pointer to rkisp1_params
59262306a36Sopenharmony_ci *
59362306a36Sopenharmony_ci * This function is called by the ISP entity just after the ISP gets started.
59462306a36Sopenharmony_ci * It applies the initial ISP LSC parameters from the first params buffer.
59562306a36Sopenharmony_ci */
59662306a36Sopenharmony_civoid rkisp1_params_post_configure(struct rkisp1_params *params);
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci/* rkisp1_params_disable - disable all parameters.
59962306a36Sopenharmony_ci *			   This function is called by the isp entity upon stream start
60062306a36Sopenharmony_ci *			   when capturing bayer format.
60162306a36Sopenharmony_ci *
60262306a36Sopenharmony_ci * @params: pointer to rkisp1_params.
60362306a36Sopenharmony_ci */
60462306a36Sopenharmony_civoid rkisp1_params_disable(struct rkisp1_params *params);
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci/* irq handlers */
60762306a36Sopenharmony_ciirqreturn_t rkisp1_isp_isr(int irq, void *ctx);
60862306a36Sopenharmony_ciirqreturn_t rkisp1_csi_isr(int irq, void *ctx);
60962306a36Sopenharmony_ciirqreturn_t rkisp1_capture_isr(int irq, void *ctx);
61062306a36Sopenharmony_civoid rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
61162306a36Sopenharmony_civoid rkisp1_params_isr(struct rkisp1_device *rkisp1);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci/* register/unregisters functions of the entities */
61462306a36Sopenharmony_ciint rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
61562306a36Sopenharmony_civoid rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ciint rkisp1_isp_register(struct rkisp1_device *rkisp1);
61862306a36Sopenharmony_civoid rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ciint rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
62162306a36Sopenharmony_civoid rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ciint rkisp1_stats_register(struct rkisp1_device *rkisp1);
62462306a36Sopenharmony_civoid rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ciint rkisp1_params_register(struct rkisp1_device *rkisp1);
62762306a36Sopenharmony_civoid rkisp1_params_unregister(struct rkisp1_device *rkisp1);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_DEBUG_FS)
63062306a36Sopenharmony_civoid rkisp1_debug_init(struct rkisp1_device *rkisp1);
63162306a36Sopenharmony_civoid rkisp1_debug_cleanup(struct rkisp1_device *rkisp1);
63262306a36Sopenharmony_ci#else
63362306a36Sopenharmony_cistatic inline void rkisp1_debug_init(struct rkisp1_device *rkisp1)
63462306a36Sopenharmony_ci{
63562306a36Sopenharmony_ci}
63662306a36Sopenharmony_cistatic inline void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1)
63762306a36Sopenharmony_ci{
63862306a36Sopenharmony_ci}
63962306a36Sopenharmony_ci#endif
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci#endif /* _RKISP1_COMMON_H */
642