1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * drivers/media/i2c/smiapp/ccs.h
4  *
5  * Generic driver for MIPI CCS/SMIA/SMIA++ compliant camera sensors
6  *
7  * Copyright (C) 2020 Intel Corporation
8  * Copyright (C) 2010--2012 Nokia Corporation
9  * Contact: Sakari Ailus <sakari.ailus@linux.intel.com>
10  */
11 
12 #ifndef __CCS_H__
13 #define __CCS_H__
14 
15 #include <linux/mutex.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-subdev.h>
18 
19 #include "ccs-data.h"
20 #include "ccs-limits.h"
21 #include "ccs-quirk.h"
22 #include "ccs-regs.h"
23 #include "ccs-reg-access.h"
24 #include "../ccs-pll.h"
25 #include "smiapp-reg-defs.h"
26 
27 /*
28  * Standard SMIA++ constants
29  */
30 #define SMIA_VERSION_1			10
31 #define SMIAPP_VERSION_0_8		8 /* Draft 0.8 */
32 #define SMIAPP_VERSION_0_9		9 /* Draft 0.9 */
33 #define SMIAPP_VERSION_1		10
34 
35 #define SMIAPP_PROFILE_0		0
36 #define SMIAPP_PROFILE_1		1
37 #define SMIAPP_PROFILE_2		2
38 
39 #define SMIAPP_NVM_PAGE_SIZE		64	/* bytes */
40 
41 #define SMIAPP_RESET_DELAY_CLOCKS	2400
42 #define SMIAPP_RESET_DELAY(clk)				\
43 	(1000 +	(SMIAPP_RESET_DELAY_CLOCKS * 1000	\
44 		 + (clk) / 1000 - 1) / ((clk) / 1000))
45 
46 #define CCS_COLOUR_COMPONENTS		4
47 
48 #define SMIAPP_NAME			"smiapp"
49 #define CCS_NAME			"ccs"
50 
51 #define CCS_DFL_I2C_ADDR	(0x20 >> 1) /* Default I2C Address */
52 #define CCS_ALT_I2C_ADDR	(0x6e >> 1) /* Alternate I2C Address */
53 
54 #define CCS_LIM(sensor, limit) \
55 	ccs_get_limit(sensor, CCS_L_##limit, 0)
56 
57 #define CCS_LIM_AT(sensor, limit, offset)	\
58 	ccs_get_limit(sensor, CCS_L_##limit, CCS_L_##limit##_OFFSET(offset))
59 
60 /*
61  * Sometimes due to board layout considerations the camera module can be
62  * mounted rotated. The typical rotation used is 180 degrees which can be
63  * corrected by giving a default H-FLIP and V-FLIP in the sensor readout.
64  * FIXME: rotation also changes the bayer pattern.
65  */
66 enum ccs_module_board_orient {
67 	CCS_MODULE_BOARD_ORIENT_0 = 0,
68 	CCS_MODULE_BOARD_ORIENT_180,
69 };
70 
71 struct ccs_flash_strobe_parms {
72 	u8 mode;
73 	u32 strobe_width_high_us;
74 	u16 strobe_delay;
75 	u16 stobe_start_point;
76 	u8 trigger;
77 };
78 
79 struct ccs_hwconfig {
80 	/*
81 	 * Change the cci address if i2c_addr_alt is set.
82 	 * Both default and alternate cci addr need to be present
83 	 */
84 	unsigned short i2c_addr_dfl;	/* Default i2c addr */
85 	unsigned short i2c_addr_alt;	/* Alternate i2c addr */
86 
87 	u32 ext_clk;			/* sensor external clk */
88 
89 	unsigned int lanes;		/* Number of CSI-2 lanes */
90 	u32 csi_signalling_mode;	/* CCS_CSI_SIGNALLING_MODE_* */
91 	u64 *op_sys_clock;
92 
93 	enum ccs_module_board_orient module_board_orient;
94 
95 	struct ccs_flash_strobe_parms *strobe_setup;
96 };
97 
98 struct ccs_quirk;
99 
100 #define CCS_MODULE_IDENT_FLAG_REV_LE		(1 << 0)
101 
102 struct ccs_module_ident {
103 	u16 mipi_manufacturer_id;
104 	u16 model_id;
105 	u8 smia_manufacturer_id;
106 	u8 revision_number_major;
107 
108 	u8 flags;
109 
110 	char *name;
111 	const struct ccs_quirk *quirk;
112 };
113 
114 struct ccs_module_info {
115 	u32 smia_manufacturer_id;
116 	u32 mipi_manufacturer_id;
117 	u32 model_id;
118 	u32 revision_number;
119 
120 	u32 module_year;
121 	u32 module_month;
122 	u32 module_day;
123 
124 	u32 sensor_smia_manufacturer_id;
125 	u32 sensor_mipi_manufacturer_id;
126 	u32 sensor_model_id;
127 	u32 sensor_revision_number;
128 	u32 sensor_firmware_version;
129 
130 	u32 smia_version;
131 	u32 smiapp_version;
132 	u32 ccs_version;
133 
134 	char *name;
135 	const struct ccs_quirk *quirk;
136 };
137 
138 #define CCS_IDENT_FQ(manufacturer, model, rev, fl, _name, _quirk)	\
139 	{ .smia_manufacturer_id = manufacturer,				\
140 	  .model_id = model,						\
141 	  .revision_number_major = rev,					\
142 	  .flags = fl,							\
143 	  .name = _name,						\
144 	  .quirk = _quirk, }
145 
146 #define CCS_IDENT_LQ(manufacturer, model, rev, _name, _quirk)	\
147 	{ .smia_manufacturer_id = manufacturer,				\
148 	  .model_id = model,						\
149 	  .revision_number_major = rev,					\
150 	  .flags = CCS_MODULE_IDENT_FLAG_REV_LE,			\
151 	  .name = _name,						\
152 	  .quirk = _quirk, }
153 
154 #define CCS_IDENT_L(manufacturer, model, rev, _name)			\
155 	{ .smia_manufacturer_id = manufacturer,				\
156 	  .model_id = model,						\
157 	  .revision_number_major = rev,					\
158 	  .flags = CCS_MODULE_IDENT_FLAG_REV_LE,			\
159 	  .name = _name, }
160 
161 #define CCS_IDENT_Q(manufacturer, model, rev, _name, _quirk)		\
162 	{ .smia_manufacturer_id = manufacturer,				\
163 	  .model_id = model,						\
164 	  .revision_number_major = rev,					\
165 	  .flags = 0,							\
166 	  .name = _name,						\
167 	  .quirk = _quirk, }
168 
169 #define CCS_IDENT(manufacturer, model, rev, _name)			\
170 	{ .smia_manufacturer_id = manufacturer,				\
171 	  .model_id = model,						\
172 	  .revision_number_major = rev,					\
173 	  .flags = 0,							\
174 	  .name = _name, }
175 
176 struct ccs_csi_data_format {
177 	u32 code;
178 	u8 width;
179 	u8 compressed;
180 	u8 pixel_order;
181 };
182 
183 #define CCS_SUBDEVS			3
184 
185 #define CCS_PA_PAD_SRC			0
186 #define CCS_PAD_SINK			0
187 #define CCS_PAD_SRC			1
188 #define CCS_PADS			2
189 
190 struct ccs_binning_subtype {
191 	u8 horizontal:4;
192 	u8 vertical:4;
193 } __packed;
194 
195 struct ccs_subdev {
196 	struct v4l2_subdev sd;
197 	struct media_pad pads[CCS_PADS];
198 	struct v4l2_rect sink_fmt;
199 	struct v4l2_rect crop[CCS_PADS];
200 	struct v4l2_rect compose; /* compose on sink */
201 	unsigned short sink_pad;
202 	unsigned short source_pad;
203 	int npads;
204 	struct ccs_sensor *sensor;
205 	struct v4l2_ctrl_handler ctrl_handler;
206 };
207 
208 /*
209  * struct ccs_sensor - Main device structure
210  */
211 struct ccs_sensor {
212 	/*
213 	 * "mutex" is used to serialise access to all fields here
214 	 * except v4l2_ctrls at the end of the struct. "mutex" is also
215 	 * used to serialise access to file handle specific
216 	 * information.
217 	 */
218 	struct mutex mutex;
219 	struct ccs_subdev ssds[CCS_SUBDEVS];
220 	u32 ssds_used;
221 	struct ccs_subdev *src;
222 	struct ccs_subdev *binner;
223 	struct ccs_subdev *scaler;
224 	struct ccs_subdev *pixel_array;
225 	struct ccs_hwconfig hwcfg;
226 	struct regulator_bulk_data *regulators;
227 	struct clk *ext_clk;
228 	struct gpio_desc *xshutdown;
229 	struct gpio_desc *reset;
230 	void *ccs_limits;
231 	u8 nbinning_subtypes;
232 	struct ccs_binning_subtype binning_subtypes[CCS_LIM_BINNING_SUB_TYPE_MAX_N + 1];
233 	u32 mbus_frame_fmts;
234 	const struct ccs_csi_data_format *csi_format;
235 	const struct ccs_csi_data_format *internal_csi_format;
236 	u32 default_mbus_frame_fmts;
237 	int default_pixel_order;
238 	struct ccs_data_container sdata, mdata;
239 
240 	u8 binning_horizontal;
241 	u8 binning_vertical;
242 
243 	u8 scale_m;
244 	u8 scaling_mode;
245 
246 	u8 hvflip_inv_mask; /* H/VFLIP inversion due to sensor orientation */
247 	u8 frame_skip;
248 	u16 embedded_start; /* embedded data start line */
249 	u16 embedded_end;
250 	u16 image_start; /* image data start line */
251 	u16 visible_pixel_start; /* start pixel of the visible image */
252 
253 	bool streaming;
254 	bool dev_init_done;
255 	u8 compressed_min_bpp;
256 
257 	struct ccs_module_info minfo;
258 
259 	struct ccs_pll pll;
260 
261 	/* Is a default format supported for a given BPP? */
262 	unsigned long *valid_link_freqs;
263 
264 	/* Pixel array controls */
265 	struct v4l2_ctrl *exposure;
266 	struct v4l2_ctrl *hflip;
267 	struct v4l2_ctrl *vflip;
268 	struct v4l2_ctrl *vblank;
269 	struct v4l2_ctrl *hblank;
270 	struct v4l2_ctrl *pixel_rate_parray;
271 	struct v4l2_ctrl *luminance_level;
272 	/* src controls */
273 	struct v4l2_ctrl *link_freq;
274 	struct v4l2_ctrl *pixel_rate_csi;
275 	/* test pattern colour components */
276 	struct v4l2_ctrl *test_data[CCS_COLOUR_COMPONENTS];
277 };
278 
279 #define to_ccs_subdev(_sd)				\
280 	container_of(_sd, struct ccs_subdev, sd)
281 
282 #define to_ccs_sensor(_sd)	\
283 	(to_ccs_subdev(_sd)->sensor)
284 
285 void ccs_replace_limit(struct ccs_sensor *sensor,
286 		       unsigned int limit, unsigned int offset, u32 val);
287 u32 ccs_get_limit(struct ccs_sensor *sensor, unsigned int limit,
288 		  unsigned int offset);
289 
290 #endif /* __CCS_H__ */
291