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