1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2016 NextThing Co
4 * Copyright (C) 2016-2019 Bootlin
5 *
6 * Author: Maxime Ripard <maxime.ripard@bootlin.com>
7 */
8
9#ifndef _SUN4I_CSI_H_
10#define _SUN4I_CSI_H_
11
12#include <media/media-device.h>
13#include <media/v4l2-async.h>
14#include <media/v4l2-dev.h>
15#include <media/v4l2-device.h>
16#include <media/v4l2-fwnode.h>
17#include <media/videobuf2-core.h>
18
19#define CSI_EN_REG			0x00
20
21#define CSI_CFG_REG			0x04
22#define CSI_CFG_INPUT_FMT(fmt)			((fmt) << 20)
23#define CSI_CFG_OUTPUT_FMT(fmt)			((fmt) << 16)
24#define CSI_CFG_YUV_DATA_SEQ(seq)		((seq) << 8)
25#define CSI_CFG_VREF_POL(pol)			((pol) << 2)
26#define CSI_CFG_HREF_POL(pol)			((pol) << 1)
27#define CSI_CFG_PCLK_POL(pol)			((pol) << 0)
28
29#define CSI_CPT_CTRL_REG		0x08
30#define CSI_CPT_CTRL_VIDEO_START		BIT(1)
31#define CSI_CPT_CTRL_IMAGE_START		BIT(0)
32
33#define CSI_BUF_ADDR_REG(fifo, buf)	(0x10 + (0x8 * (fifo)) + (0x4 * (buf)))
34
35#define CSI_BUF_CTRL_REG		0x28
36#define CSI_BUF_CTRL_DBN			BIT(2)
37#define CSI_BUF_CTRL_DBS			BIT(1)
38#define CSI_BUF_CTRL_DBE			BIT(0)
39
40#define CSI_INT_EN_REG			0x30
41#define CSI_INT_FRM_DONE			BIT(1)
42#define CSI_INT_CPT_DONE			BIT(0)
43
44#define CSI_INT_STA_REG			0x34
45
46#define CSI_WIN_CTRL_W_REG		0x40
47#define CSI_WIN_CTRL_W_ACTIVE(w)		((w) << 16)
48
49#define CSI_WIN_CTRL_H_REG		0x44
50#define CSI_WIN_CTRL_H_ACTIVE(h)		((h) << 16)
51
52#define CSI_BUF_LEN_REG			0x48
53
54#define CSI_MAX_BUFFER		2
55#define CSI_MAX_HEIGHT		8192U
56#define CSI_MAX_WIDTH		8192U
57
58enum csi_input {
59	CSI_INPUT_RAW	= 0,
60	CSI_INPUT_BT656	= 2,
61	CSI_INPUT_YUV	= 3,
62};
63
64enum csi_output_raw {
65	CSI_OUTPUT_RAW_PASSTHROUGH = 0,
66};
67
68enum csi_output_yuv {
69	CSI_OUTPUT_YUV_422_PLANAR	= 0,
70	CSI_OUTPUT_YUV_420_PLANAR	= 1,
71	CSI_OUTPUT_YUV_422_UV		= 4,
72	CSI_OUTPUT_YUV_420_UV		= 5,
73	CSI_OUTPUT_YUV_422_MACRO	= 8,
74	CSI_OUTPUT_YUV_420_MACRO	= 9,
75};
76
77enum csi_yuv_data_seq {
78	CSI_YUV_DATA_SEQ_YUYV	= 0,
79	CSI_YUV_DATA_SEQ_YVYU	= 1,
80	CSI_YUV_DATA_SEQ_UYVY	= 2,
81	CSI_YUV_DATA_SEQ_VYUY	= 3,
82};
83
84enum csi_subdev_pads {
85	CSI_SUBDEV_SINK,
86	CSI_SUBDEV_SOURCE,
87
88	CSI_SUBDEV_PADS,
89};
90
91extern const struct v4l2_subdev_ops sun4i_csi_subdev_ops;
92extern const struct v4l2_subdev_internal_ops sun4i_csi_subdev_internal_ops;
93
94struct sun4i_csi_format {
95	u32			mbus;
96	u32			fourcc;
97	enum csi_input		input;
98	u32			output;
99	unsigned int		num_planes;
100	u8			bpp[3];
101	unsigned int		hsub;
102	unsigned int		vsub;
103};
104
105const struct sun4i_csi_format *sun4i_csi_find_format(const u32 *fourcc,
106						     const u32 *mbus);
107
108struct sun4i_csi {
109	/* Device resources */
110	struct device			*dev;
111
112	const struct sun4i_csi_traits	*traits;
113
114	void __iomem			*regs;
115	struct clk			*bus_clk;
116	struct clk			*isp_clk;
117	struct clk			*ram_clk;
118	struct reset_control		*rst;
119
120	struct vb2_v4l2_buffer		*current_buf[CSI_MAX_BUFFER];
121
122	struct {
123		size_t			size;
124		void			*vaddr;
125		dma_addr_t		paddr;
126	} scratch;
127
128	struct v4l2_mbus_config_parallel	bus;
129
130	/* Main Device */
131	struct v4l2_device		v4l;
132	struct media_device		mdev;
133	struct video_device		vdev;
134	struct media_pad		vdev_pad;
135	struct v4l2_pix_format_mplane	fmt;
136
137	/* Local subdev */
138	struct v4l2_subdev		subdev;
139	struct media_pad		subdev_pads[CSI_SUBDEV_PADS];
140	struct v4l2_mbus_framefmt	subdev_fmt;
141
142	/* V4L2 Async variables */
143	struct v4l2_async_notifier	notifier;
144	struct v4l2_subdev		*src_subdev;
145	int				src_pad;
146
147	/* V4L2 variables */
148	struct mutex			lock;
149
150	/* Videobuf2 */
151	struct vb2_queue		queue;
152	struct list_head		buf_list;
153	spinlock_t			qlock;
154	unsigned int			sequence;
155};
156
157int sun4i_csi_dma_register(struct sun4i_csi *csi, int irq);
158void sun4i_csi_dma_unregister(struct sun4i_csi *csi);
159
160int sun4i_csi_v4l2_register(struct sun4i_csi *csi);
161
162#endif /* _SUN4I_CSI_H_ */
163