xref: /linux/drivers/media/i2c/imx219.c (revision 518b21ba139cefa2ee7f9fcf516fdc6743e8db68)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A V4L2 driver for Sony IMX219 cameras.
4  * Copyright (C) 2019, Raspberry Pi (Trading) Ltd
5  *
6  * Based on Sony imx258 camera driver
7  * Copyright (C) 2018 Intel Corporation
8  *
9  * DT / fwnode changes, and regulator / GPIO control taken from imx214 driver
10  * Copyright 2018 Qtechnology A/S
11  *
12  * Flip handling taken from the Sony IMX319 driver.
13  * Copyright (C) 2018 Intel Corporation
14  *
15  */
16 
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/minmax.h>
22 #include <linux/module.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regulator/consumer.h>
25 
26 #include <media/v4l2-cci.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/v4l2-mediabus.h>
31 
32 /* Chip ID */
33 #define IMX219_REG_CHIP_ID		CCI_REG16(0x0000)
34 #define IMX219_CHIP_ID			0x0219
35 
36 #define IMX219_REG_MODE_SELECT		CCI_REG8(0x0100)
37 #define IMX219_MODE_STANDBY		0x00
38 #define IMX219_MODE_STREAMING		0x01
39 
40 #define IMX219_REG_CSI_LANE_MODE	CCI_REG8(0x0114)
41 #define IMX219_CSI_2_LANE_MODE		0x01
42 #define IMX219_CSI_4_LANE_MODE		0x03
43 
44 #define IMX219_REG_DPHY_CTRL		CCI_REG8(0x0128)
45 #define IMX219_DPHY_CTRL_TIMING_AUTO	0
46 #define IMX219_DPHY_CTRL_TIMING_MANUAL	1
47 
48 #define IMX219_REG_EXCK_FREQ		CCI_REG16(0x012a)
49 #define IMX219_EXCK_FREQ(n)		((n) * 256)		/* n expressed in MHz */
50 
51 /* Analog gain control */
52 #define IMX219_REG_ANALOG_GAIN		CCI_REG8(0x0157)
53 #define IMX219_ANA_GAIN_MIN		0
54 #define IMX219_ANA_GAIN_MAX		232
55 #define IMX219_ANA_GAIN_STEP		1
56 #define IMX219_ANA_GAIN_DEFAULT		0x0
57 
58 /* Digital gain control */
59 #define IMX219_REG_DIGITAL_GAIN		CCI_REG16(0x0158)
60 #define IMX219_DGTL_GAIN_MIN		0x0100
61 #define IMX219_DGTL_GAIN_MAX		0x0fff
62 #define IMX219_DGTL_GAIN_DEFAULT	0x0100
63 #define IMX219_DGTL_GAIN_STEP		1
64 
65 /* Exposure control */
66 #define IMX219_REG_EXPOSURE		CCI_REG16(0x015a)
67 #define IMX219_EXPOSURE_MIN		4
68 #define IMX219_EXPOSURE_STEP		1
69 #define IMX219_EXPOSURE_DEFAULT		0x640
70 #define IMX219_EXPOSURE_MAX		65535
71 
72 /* V_TIMING internal */
73 #define IMX219_REG_FRM_LENGTH_A		CCI_REG16(0x0160)
74 #define IMX219_FLL_MAX			0xffff
75 #define IMX219_VBLANK_MIN		32
76 #define IMX219_REG_LINE_LENGTH_A	CCI_REG16(0x0162)
77 #define IMX219_LLP_MIN			0x0d78
78 #define IMX219_BINNED_LLP_MIN		0x0de8
79 #define IMX219_LLP_MAX			0x7ff0
80 
81 #define IMX219_REG_X_ADD_STA_A		CCI_REG16(0x0164)
82 #define IMX219_REG_X_ADD_END_A		CCI_REG16(0x0166)
83 #define IMX219_REG_Y_ADD_STA_A		CCI_REG16(0x0168)
84 #define IMX219_REG_Y_ADD_END_A		CCI_REG16(0x016a)
85 #define IMX219_REG_X_OUTPUT_SIZE	CCI_REG16(0x016c)
86 #define IMX219_REG_Y_OUTPUT_SIZE	CCI_REG16(0x016e)
87 #define IMX219_REG_X_ODD_INC_A		CCI_REG8(0x0170)
88 #define IMX219_REG_Y_ODD_INC_A		CCI_REG8(0x0171)
89 #define IMX219_REG_ORIENTATION		CCI_REG8(0x0172)
90 
91 /* Binning  Mode */
92 #define IMX219_REG_BINNING_MODE_H	CCI_REG8(0x0174)
93 #define IMX219_REG_BINNING_MODE_V	CCI_REG8(0x0175)
94 #define IMX219_BINNING_NONE		0x00
95 #define IMX219_BINNING_X2		0x01
96 #define IMX219_BINNING_X2_ANALOG	0x03
97 
98 #define IMX219_REG_CSI_DATA_FORMAT_A	CCI_REG16(0x018c)
99 
100 /* PLL Settings */
101 #define IMX219_REG_VTPXCK_DIV		CCI_REG8(0x0301)
102 #define IMX219_REG_VTSYCK_DIV		CCI_REG8(0x0303)
103 #define IMX219_REG_PREPLLCK_VT_DIV	CCI_REG8(0x0304)
104 #define IMX219_REG_PREPLLCK_OP_DIV	CCI_REG8(0x0305)
105 #define IMX219_REG_PLL_VT_MPY		CCI_REG16(0x0306)
106 #define IMX219_REG_OPPXCK_DIV		CCI_REG8(0x0309)
107 #define IMX219_REG_OPSYCK_DIV		CCI_REG8(0x030b)
108 #define IMX219_REG_PLL_OP_MPY		CCI_REG16(0x030c)
109 
110 /* Test Pattern Control */
111 #define IMX219_REG_TEST_PATTERN		CCI_REG16(0x0600)
112 #define IMX219_TEST_PATTERN_DISABLE	0
113 #define IMX219_TEST_PATTERN_SOLID_COLOR	1
114 #define IMX219_TEST_PATTERN_COLOR_BARS	2
115 #define IMX219_TEST_PATTERN_GREY_COLOR	3
116 #define IMX219_TEST_PATTERN_PN9		4
117 
118 /* Test pattern colour components */
119 #define IMX219_REG_TESTP_RED		CCI_REG16(0x0602)
120 #define IMX219_REG_TESTP_GREENR		CCI_REG16(0x0604)
121 #define IMX219_REG_TESTP_BLUE		CCI_REG16(0x0606)
122 #define IMX219_REG_TESTP_GREENB		CCI_REG16(0x0608)
123 #define IMX219_TESTP_COLOUR_MIN		0
124 #define IMX219_TESTP_COLOUR_MAX		0x03ff
125 #define IMX219_TESTP_COLOUR_STEP	1
126 
127 #define IMX219_REG_TP_WINDOW_WIDTH	CCI_REG16(0x0624)
128 #define IMX219_REG_TP_WINDOW_HEIGHT	CCI_REG16(0x0626)
129 
130 /* External clock frequency is 24.0M */
131 #define IMX219_XCLK_FREQ		24000000
132 
133 /* Pixel rate is fixed for all the modes */
134 #define IMX219_PIXEL_RATE		182400000
135 #define IMX219_PIXEL_RATE_4LANE		281600000
136 
137 #define IMX219_DEFAULT_LINK_FREQ	456000000
138 #define IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED	363000000
139 #define IMX219_DEFAULT_LINK_FREQ_4LANE	364000000
140 
141 /* IMX219 native and active pixel array size. */
142 #define IMX219_NATIVE_WIDTH		3296U
143 #define IMX219_NATIVE_HEIGHT		2480U
144 #define IMX219_PIXEL_ARRAY_LEFT		8U
145 #define IMX219_PIXEL_ARRAY_TOP		8U
146 #define IMX219_PIXEL_ARRAY_WIDTH	3280U
147 #define IMX219_PIXEL_ARRAY_HEIGHT	2464U
148 
149 /* Mode : resolution and related config&values */
150 struct imx219_mode {
151 	/* Frame width */
152 	unsigned int width;
153 	/* Frame height */
154 	unsigned int height;
155 
156 	/* V-timing */
157 	unsigned int fll_def;
158 };
159 
160 static const struct cci_reg_sequence imx219_common_regs[] = {
161 	{ IMX219_REG_MODE_SELECT, 0x00 },	/* Mode Select */
162 
163 	/* To Access Addresses 3000-5fff, send the following commands */
164 	{ CCI_REG8(0x30eb), 0x05 },
165 	{ CCI_REG8(0x30eb), 0x0c },
166 	{ CCI_REG8(0x300a), 0xff },
167 	{ CCI_REG8(0x300b), 0xff },
168 	{ CCI_REG8(0x30eb), 0x05 },
169 	{ CCI_REG8(0x30eb), 0x09 },
170 
171 	/* Undocumented registers */
172 	{ CCI_REG8(0x455e), 0x00 },
173 	{ CCI_REG8(0x471e), 0x4b },
174 	{ CCI_REG8(0x4767), 0x0f },
175 	{ CCI_REG8(0x4750), 0x14 },
176 	{ CCI_REG8(0x4540), 0x00 },
177 	{ CCI_REG8(0x47b4), 0x14 },
178 	{ CCI_REG8(0x4713), 0x30 },
179 	{ CCI_REG8(0x478b), 0x10 },
180 	{ CCI_REG8(0x478f), 0x10 },
181 	{ CCI_REG8(0x4793), 0x10 },
182 	{ CCI_REG8(0x4797), 0x0e },
183 	{ CCI_REG8(0x479b), 0x0e },
184 
185 	/* Frame Bank Register Group "A" */
186 	{ IMX219_REG_X_ODD_INC_A, 1 },
187 	{ IMX219_REG_Y_ODD_INC_A, 1 },
188 
189 	/* Output setup registers */
190 	{ IMX219_REG_DPHY_CTRL, IMX219_DPHY_CTRL_TIMING_AUTO },
191 	{ IMX219_REG_EXCK_FREQ, IMX219_EXCK_FREQ(IMX219_XCLK_FREQ / 1000000) },
192 };
193 
194 static const struct cci_reg_sequence imx219_2lane_regs[] = {
195 	/* PLL Clock Table */
196 	{ IMX219_REG_VTPXCK_DIV, 5 },
197 	{ IMX219_REG_VTSYCK_DIV, 1 },
198 	{ IMX219_REG_PREPLLCK_VT_DIV, 3 },	/* 0x03 = AUTO set */
199 	{ IMX219_REG_PREPLLCK_OP_DIV, 3 },	/* 0x03 = AUTO set */
200 	{ IMX219_REG_PLL_VT_MPY, 57 },
201 	{ IMX219_REG_OPSYCK_DIV, 1 },
202 	{ IMX219_REG_PLL_OP_MPY, 114 },
203 
204 	/* 2-Lane CSI Mode */
205 	{ IMX219_REG_CSI_LANE_MODE, IMX219_CSI_2_LANE_MODE },
206 };
207 
208 static const struct cci_reg_sequence imx219_4lane_regs[] = {
209 	/* PLL Clock Table */
210 	{ IMX219_REG_VTPXCK_DIV, 5 },
211 	{ IMX219_REG_VTSYCK_DIV, 1 },
212 	{ IMX219_REG_PREPLLCK_VT_DIV, 3 },	/* 0x03 = AUTO set */
213 	{ IMX219_REG_PREPLLCK_OP_DIV, 3 },	/* 0x03 = AUTO set */
214 	{ IMX219_REG_PLL_VT_MPY, 88 },
215 	{ IMX219_REG_OPSYCK_DIV, 1 },
216 	{ IMX219_REG_PLL_OP_MPY, 91 },
217 
218 	/* 4-Lane CSI Mode */
219 	{ IMX219_REG_CSI_LANE_MODE, IMX219_CSI_4_LANE_MODE },
220 };
221 
222 static const s64 imx219_link_freq_menu[] = {
223 	IMX219_DEFAULT_LINK_FREQ,
224 };
225 
226 static const s64 imx219_link_freq_4lane_menu[] = {
227 	IMX219_DEFAULT_LINK_FREQ_4LANE,
228 	/*
229 	 * This will never be advertised to userspace, but will be used for
230 	 * v4l2_link_freq_to_bitmap
231 	 */
232 	IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED,
233 };
234 
235 static const char * const imx219_test_pattern_menu[] = {
236 	"Disabled",
237 	"Color Bars",
238 	"Solid Color",
239 	"Grey Color Bars",
240 	"PN9"
241 };
242 
243 static const int imx219_test_pattern_val[] = {
244 	IMX219_TEST_PATTERN_DISABLE,
245 	IMX219_TEST_PATTERN_COLOR_BARS,
246 	IMX219_TEST_PATTERN_SOLID_COLOR,
247 	IMX219_TEST_PATTERN_GREY_COLOR,
248 	IMX219_TEST_PATTERN_PN9,
249 };
250 
251 /* regulator supplies */
252 static const char * const imx219_supply_name[] = {
253 	/* Supplies can be enabled in any order */
254 	"VANA",  /* Analog (2.8V) supply */
255 	"VDIG",  /* Digital Core (1.8V) supply */
256 	"VDDL",  /* IF (1.2V) supply */
257 };
258 
259 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
260 
261 /*
262  * The supported formats.
263  * This table MUST contain 4 entries per format, to cover the various flip
264  * combinations in the order
265  * - no flip
266  * - h flip
267  * - v flip
268  * - h&v flips
269  */
270 static const u32 imx219_mbus_formats[] = {
271 	MEDIA_BUS_FMT_SRGGB10_1X10,
272 	MEDIA_BUS_FMT_SGRBG10_1X10,
273 	MEDIA_BUS_FMT_SGBRG10_1X10,
274 	MEDIA_BUS_FMT_SBGGR10_1X10,
275 
276 	MEDIA_BUS_FMT_SRGGB8_1X8,
277 	MEDIA_BUS_FMT_SGRBG8_1X8,
278 	MEDIA_BUS_FMT_SGBRG8_1X8,
279 	MEDIA_BUS_FMT_SBGGR8_1X8,
280 };
281 
282 /*
283  * Initialisation delay between XCLR low->high and the moment when the sensor
284  * can start capture (i.e. can leave software stanby) must be not less than:
285  *   t4 + max(t5, t6 + <time to initialize the sensor register over I2C>)
286  * where
287  *   t4 is fixed, and is max 200uS,
288  *   t5 is fixed, and is 6000uS,
289  *   t6 depends on the sensor external clock, and is max 32000 clock periods.
290  * As per sensor datasheet, the external clock must be from 6MHz to 27MHz.
291  * So for any acceptable external clock t6 is always within the range of
292  * 1185 to 5333 uS, and is always less than t5.
293  * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then
294  * initialize the sensor over I2C, and then exit the software standby.
295  *
296  * This start-up time can be optimized a bit more, if we start the writes
297  * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor
298  * initialization over I2C may complete before (t4+t5) expires, and we must
299  * ensure that capture is not started before (t4+t5).
300  *
301  * This delay doesn't account for the power supply startup time. If needed,
302  * this should be taken care of via the regulator framework. E.g. in the
303  * case of DT for regulator-fixed one should define the startup-delay-us
304  * property.
305  */
306 #define IMX219_XCLR_MIN_DELAY_US	6200
307 #define IMX219_XCLR_DELAY_RANGE_US	1000
308 
309 /* Mode configs */
310 static const struct imx219_mode supported_modes[] = {
311 	{
312 		/* 8MPix 15fps mode */
313 		.width = 3280,
314 		.height = 2464,
315 		.fll_def = 3526,
316 	},
317 	{
318 		/* 1080P 30fps cropped */
319 		.width = 1920,
320 		.height = 1080,
321 		.fll_def = 1763,
322 	},
323 	{
324 		/* 2x2 binned 60fps mode */
325 		.width = 1640,
326 		.height = 1232,
327 		.fll_def = 1707,
328 	},
329 	{
330 		/* 640x480 60fps mode */
331 		.width = 640,
332 		.height = 480,
333 		.fll_def = 1707,
334 	},
335 };
336 
337 struct imx219 {
338 	struct v4l2_subdev sd;
339 	struct media_pad pad;
340 
341 	struct regmap *regmap;
342 	struct clk *xclk; /* system clock to IMX219 */
343 	u32 xclk_freq;
344 
345 	struct gpio_desc *reset_gpio;
346 	struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES];
347 
348 	struct v4l2_ctrl_handler ctrl_handler;
349 	/* V4L2 Controls */
350 	struct v4l2_ctrl *pixel_rate;
351 	struct v4l2_ctrl *link_freq;
352 	struct v4l2_ctrl *exposure;
353 	struct v4l2_ctrl *vflip;
354 	struct v4l2_ctrl *hflip;
355 	struct v4l2_ctrl *vblank;
356 	struct v4l2_ctrl *hblank;
357 
358 	/* Two or Four lanes */
359 	u8 lanes;
360 };
361 
to_imx219(struct v4l2_subdev * _sd)362 static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd)
363 {
364 	return container_of(_sd, struct imx219, sd);
365 }
366 
367 /* Get bayer order based on flip setting. */
imx219_get_format_code(struct imx219 * imx219,u32 code)368 static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
369 {
370 	unsigned int i;
371 
372 	for (i = 0; i < ARRAY_SIZE(imx219_mbus_formats); i++)
373 		if (imx219_mbus_formats[i] == code)
374 			break;
375 
376 	if (i >= ARRAY_SIZE(imx219_mbus_formats))
377 		i = 0;
378 
379 	i = (i & ~3) | (imx219->vflip->val ? 2 : 0) |
380 	    (imx219->hflip->val ? 1 : 0);
381 
382 	return imx219_mbus_formats[i];
383 }
384 
imx219_get_format_bpp(const struct v4l2_mbus_framefmt * format)385 static u32 imx219_get_format_bpp(const struct v4l2_mbus_framefmt *format)
386 {
387 	switch (format->code) {
388 	case MEDIA_BUS_FMT_SRGGB8_1X8:
389 	case MEDIA_BUS_FMT_SGRBG8_1X8:
390 	case MEDIA_BUS_FMT_SGBRG8_1X8:
391 	case MEDIA_BUS_FMT_SBGGR8_1X8:
392 		return 8;
393 
394 	case MEDIA_BUS_FMT_SRGGB10_1X10:
395 	case MEDIA_BUS_FMT_SGRBG10_1X10:
396 	case MEDIA_BUS_FMT_SGBRG10_1X10:
397 	case MEDIA_BUS_FMT_SBGGR10_1X10:
398 	default:
399 		return 10;
400 	}
401 }
402 
imx219_get_binning(struct v4l2_subdev_state * state,u8 * bin_h,u8 * bin_v)403 static void imx219_get_binning(struct v4l2_subdev_state *state, u8 *bin_h,
404 			       u8 *bin_v)
405 {
406 	const struct v4l2_mbus_framefmt *format =
407 		v4l2_subdev_state_get_format(state, 0);
408 	const struct v4l2_rect *crop = v4l2_subdev_state_get_crop(state, 0);
409 	u32 hbin = crop->width / format->width;
410 	u32 vbin = crop->height / format->height;
411 
412 	*bin_h = IMX219_BINNING_NONE;
413 	*bin_v = IMX219_BINNING_NONE;
414 
415 	/*
416 	 * Use analog binning only if both dimensions are binned, as it crops
417 	 * the other dimension.
418 	 */
419 	if (hbin == 2 && vbin == 2) {
420 		*bin_h = IMX219_BINNING_X2_ANALOG;
421 		*bin_v = IMX219_BINNING_X2_ANALOG;
422 
423 		return;
424 	}
425 
426 	if (hbin == 2)
427 		*bin_h = IMX219_BINNING_X2;
428 	if (vbin == 2)
429 		*bin_v = IMX219_BINNING_X2;
430 }
431 
imx219_get_rate_factor(struct v4l2_subdev_state * state)432 static inline u32 imx219_get_rate_factor(struct v4l2_subdev_state *state)
433 {
434 	u8 bin_h, bin_v;
435 
436 	imx219_get_binning(state, &bin_h, &bin_v);
437 
438 	return (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ? 2 : 1;
439 }
440 
441 /* -----------------------------------------------------------------------------
442  * Controls
443  */
444 
imx219_set_ctrl(struct v4l2_ctrl * ctrl)445 static int imx219_set_ctrl(struct v4l2_ctrl *ctrl)
446 {
447 	struct imx219 *imx219 =
448 		container_of(ctrl->handler, struct imx219, ctrl_handler);
449 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
450 	const struct v4l2_mbus_framefmt *format;
451 	struct v4l2_subdev_state *state;
452 	u32 rate_factor;
453 	int ret = 0;
454 
455 	state = v4l2_subdev_get_locked_active_state(&imx219->sd);
456 	format = v4l2_subdev_state_get_format(state, 0);
457 	rate_factor = imx219_get_rate_factor(state);
458 
459 	if (ctrl->id == V4L2_CID_VBLANK) {
460 		int exposure_max, exposure_def;
461 
462 		/* Update max exposure while meeting expected vblanking */
463 		exposure_max = format->height + ctrl->val - 4;
464 		exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
465 			exposure_max : IMX219_EXPOSURE_DEFAULT;
466 		__v4l2_ctrl_modify_range(imx219->exposure,
467 					 imx219->exposure->minimum,
468 					 exposure_max, imx219->exposure->step,
469 					 exposure_def);
470 	}
471 
472 	/*
473 	 * Applying V4L2 control value only happens
474 	 * when power is up for streaming
475 	 */
476 	if (pm_runtime_get_if_in_use(&client->dev) == 0)
477 		return 0;
478 
479 	switch (ctrl->id) {
480 	case V4L2_CID_ANALOGUE_GAIN:
481 		cci_write(imx219->regmap, IMX219_REG_ANALOG_GAIN,
482 			  ctrl->val, &ret);
483 		break;
484 	case V4L2_CID_EXPOSURE:
485 		cci_write(imx219->regmap, IMX219_REG_EXPOSURE,
486 			  ctrl->val / rate_factor, &ret);
487 		break;
488 	case V4L2_CID_DIGITAL_GAIN:
489 		cci_write(imx219->regmap, IMX219_REG_DIGITAL_GAIN,
490 			  ctrl->val, &ret);
491 		break;
492 	case V4L2_CID_TEST_PATTERN:
493 		cci_write(imx219->regmap, IMX219_REG_TEST_PATTERN,
494 			  imx219_test_pattern_val[ctrl->val], &ret);
495 		break;
496 	case V4L2_CID_HFLIP:
497 	case V4L2_CID_VFLIP:
498 		cci_write(imx219->regmap, IMX219_REG_ORIENTATION,
499 			  imx219->hflip->val | imx219->vflip->val << 1, &ret);
500 		break;
501 	case V4L2_CID_VBLANK:
502 		cci_write(imx219->regmap, IMX219_REG_FRM_LENGTH_A,
503 			  (format->height + ctrl->val) / rate_factor, &ret);
504 		break;
505 	case V4L2_CID_HBLANK:
506 		cci_write(imx219->regmap, IMX219_REG_LINE_LENGTH_A,
507 			  format->width + ctrl->val, &ret);
508 		break;
509 	case V4L2_CID_TEST_PATTERN_RED:
510 		cci_write(imx219->regmap, IMX219_REG_TESTP_RED,
511 			  ctrl->val, &ret);
512 		break;
513 	case V4L2_CID_TEST_PATTERN_GREENR:
514 		cci_write(imx219->regmap, IMX219_REG_TESTP_GREENR,
515 			  ctrl->val, &ret);
516 		break;
517 	case V4L2_CID_TEST_PATTERN_BLUE:
518 		cci_write(imx219->regmap, IMX219_REG_TESTP_BLUE,
519 			  ctrl->val, &ret);
520 		break;
521 	case V4L2_CID_TEST_PATTERN_GREENB:
522 		cci_write(imx219->regmap, IMX219_REG_TESTP_GREENB,
523 			  ctrl->val, &ret);
524 		break;
525 	default:
526 		dev_info(&client->dev,
527 			 "ctrl(id:0x%x,val:0x%x) is not handled\n",
528 			 ctrl->id, ctrl->val);
529 		ret = -EINVAL;
530 		break;
531 	}
532 
533 	pm_runtime_put(&client->dev);
534 
535 	return ret;
536 }
537 
538 static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
539 	.s_ctrl = imx219_set_ctrl,
540 };
541 
imx219_get_pixel_rate(struct imx219 * imx219)542 static unsigned long imx219_get_pixel_rate(struct imx219 *imx219)
543 {
544 	return (imx219->lanes == 2) ? IMX219_PIXEL_RATE : IMX219_PIXEL_RATE_4LANE;
545 }
546 
547 /* Initialize control handlers */
imx219_init_controls(struct imx219 * imx219)548 static int imx219_init_controls(struct imx219 *imx219)
549 {
550 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
551 	const struct imx219_mode *mode = &supported_modes[0];
552 	struct v4l2_ctrl_handler *ctrl_hdlr;
553 	struct v4l2_fwnode_device_properties props;
554 	int exposure_max, exposure_def;
555 	int i, ret;
556 
557 	ctrl_hdlr = &imx219->ctrl_handler;
558 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
559 	if (ret)
560 		return ret;
561 
562 	/* By default, PIXEL_RATE is read only */
563 	imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
564 					       V4L2_CID_PIXEL_RATE,
565 					       imx219_get_pixel_rate(imx219),
566 					       imx219_get_pixel_rate(imx219), 1,
567 					       imx219_get_pixel_rate(imx219));
568 
569 	imx219->link_freq =
570 		v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx219_ctrl_ops,
571 				       V4L2_CID_LINK_FREQ,
572 				       ARRAY_SIZE(imx219_link_freq_menu) - 1, 0,
573 				       (imx219->lanes == 2) ? imx219_link_freq_menu :
574 				       imx219_link_freq_4lane_menu);
575 	if (imx219->link_freq)
576 		imx219->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
577 
578 	/* Initial blanking and exposure. Limits are updated during set_fmt */
579 	imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
580 					   V4L2_CID_VBLANK, IMX219_VBLANK_MIN,
581 					   IMX219_FLL_MAX - mode->height, 1,
582 					   mode->fll_def - mode->height);
583 	imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
584 					   V4L2_CID_HBLANK,
585 					   IMX219_LLP_MIN - mode->width,
586 					   IMX219_LLP_MAX - mode->width, 1,
587 					   IMX219_LLP_MIN - mode->width);
588 	exposure_max = mode->fll_def - 4;
589 	exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
590 		exposure_max : IMX219_EXPOSURE_DEFAULT;
591 	imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
592 					     V4L2_CID_EXPOSURE,
593 					     IMX219_EXPOSURE_MIN, exposure_max,
594 					     IMX219_EXPOSURE_STEP,
595 					     exposure_def);
596 
597 	v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
598 			  IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX,
599 			  IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT);
600 
601 	v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
602 			  IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX,
603 			  IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT);
604 
605 	imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
606 					  V4L2_CID_HFLIP, 0, 1, 1, 0);
607 	if (imx219->hflip)
608 		imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
609 
610 	imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
611 					  V4L2_CID_VFLIP, 0, 1, 1, 0);
612 	if (imx219->vflip)
613 		imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
614 
615 	v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops,
616 				     V4L2_CID_TEST_PATTERN,
617 				     ARRAY_SIZE(imx219_test_pattern_menu) - 1,
618 				     0, 0, imx219_test_pattern_menu);
619 	for (i = 0; i < 4; i++) {
620 		/*
621 		 * The assumption is that
622 		 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
623 		 * V4L2_CID_TEST_PATTERN_BLUE   == V4L2_CID_TEST_PATTERN_RED + 2
624 		 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
625 		 */
626 		v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
627 				  V4L2_CID_TEST_PATTERN_RED + i,
628 				  IMX219_TESTP_COLOUR_MIN,
629 				  IMX219_TESTP_COLOUR_MAX,
630 				  IMX219_TESTP_COLOUR_STEP,
631 				  IMX219_TESTP_COLOUR_MAX);
632 		/* The "Solid color" pattern is white by default */
633 	}
634 
635 	if (ctrl_hdlr->error) {
636 		ret = ctrl_hdlr->error;
637 		dev_err_probe(&client->dev, ret, "Control init failed\n");
638 		goto error;
639 	}
640 
641 	ret = v4l2_fwnode_device_parse(&client->dev, &props);
642 	if (ret)
643 		goto error;
644 
645 	ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx219_ctrl_ops,
646 					      &props);
647 	if (ret)
648 		goto error;
649 
650 	imx219->sd.ctrl_handler = ctrl_hdlr;
651 
652 	return 0;
653 
654 error:
655 	v4l2_ctrl_handler_free(ctrl_hdlr);
656 
657 	return ret;
658 }
659 
imx219_free_controls(struct imx219 * imx219)660 static void imx219_free_controls(struct imx219 *imx219)
661 {
662 	v4l2_ctrl_handler_free(imx219->sd.ctrl_handler);
663 }
664 
665 /* -----------------------------------------------------------------------------
666  * Subdev operations
667  */
668 
imx219_set_framefmt(struct imx219 * imx219,struct v4l2_subdev_state * state)669 static int imx219_set_framefmt(struct imx219 *imx219,
670 			       struct v4l2_subdev_state *state)
671 {
672 	const struct v4l2_mbus_framefmt *format;
673 	const struct v4l2_rect *crop;
674 	u8 bin_h, bin_v;
675 	u32 bpp;
676 	int ret = 0;
677 
678 	format = v4l2_subdev_state_get_format(state, 0);
679 	crop = v4l2_subdev_state_get_crop(state, 0);
680 	bpp = imx219_get_format_bpp(format);
681 
682 	cci_write(imx219->regmap, IMX219_REG_X_ADD_STA_A,
683 		  crop->left - IMX219_PIXEL_ARRAY_LEFT, &ret);
684 	cci_write(imx219->regmap, IMX219_REG_X_ADD_END_A,
685 		  crop->left - IMX219_PIXEL_ARRAY_LEFT + crop->width - 1, &ret);
686 	cci_write(imx219->regmap, IMX219_REG_Y_ADD_STA_A,
687 		  crop->top - IMX219_PIXEL_ARRAY_TOP, &ret);
688 	cci_write(imx219->regmap, IMX219_REG_Y_ADD_END_A,
689 		  crop->top - IMX219_PIXEL_ARRAY_TOP + crop->height - 1, &ret);
690 
691 	imx219_get_binning(state, &bin_h, &bin_v);
692 	cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_H, bin_h, &ret);
693 	cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_V, bin_v, &ret);
694 
695 	cci_write(imx219->regmap, IMX219_REG_X_OUTPUT_SIZE,
696 		  format->width, &ret);
697 	cci_write(imx219->regmap, IMX219_REG_Y_OUTPUT_SIZE,
698 		  format->height, &ret);
699 
700 	cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_WIDTH,
701 		  format->width, &ret);
702 	cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_HEIGHT,
703 		  format->height, &ret);
704 
705 	cci_write(imx219->regmap, IMX219_REG_CSI_DATA_FORMAT_A,
706 		  (bpp << 8) | bpp, &ret);
707 	cci_write(imx219->regmap, IMX219_REG_OPPXCK_DIV, bpp, &ret);
708 
709 	return ret;
710 }
711 
imx219_configure_lanes(struct imx219 * imx219)712 static int imx219_configure_lanes(struct imx219 *imx219)
713 {
714 	/* Write the appropriate PLL settings for the number of MIPI lanes */
715 	return cci_multi_reg_write(imx219->regmap,
716 				  imx219->lanes == 2 ? imx219_2lane_regs : imx219_4lane_regs,
717 				  imx219->lanes == 2 ? ARRAY_SIZE(imx219_2lane_regs) :
718 				  ARRAY_SIZE(imx219_4lane_regs), NULL);
719 };
720 
imx219_enable_streams(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 pad,u64 streams_mask)721 static int imx219_enable_streams(struct v4l2_subdev *sd,
722 				 struct v4l2_subdev_state *state, u32 pad,
723 				 u64 streams_mask)
724 {
725 	struct imx219 *imx219 = to_imx219(sd);
726 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
727 	int ret;
728 
729 	ret = pm_runtime_resume_and_get(&client->dev);
730 	if (ret < 0)
731 		return ret;
732 
733 	/* Send all registers that are common to all modes */
734 	ret = cci_multi_reg_write(imx219->regmap, imx219_common_regs,
735 				  ARRAY_SIZE(imx219_common_regs), NULL);
736 	if (ret) {
737 		dev_err(&client->dev, "%s failed to send mfg header\n", __func__);
738 		goto err_rpm_put;
739 	}
740 
741 	/* Configure two or four Lane mode */
742 	ret = imx219_configure_lanes(imx219);
743 	if (ret) {
744 		dev_err(&client->dev, "%s failed to configure lanes\n", __func__);
745 		goto err_rpm_put;
746 	}
747 
748 	/* Apply format and crop settings. */
749 	ret = imx219_set_framefmt(imx219, state);
750 	if (ret) {
751 		dev_err(&client->dev, "%s failed to set frame format: %d\n",
752 			__func__, ret);
753 		goto err_rpm_put;
754 	}
755 
756 	/* Apply customized values from user */
757 	ret =  __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler);
758 	if (ret)
759 		goto err_rpm_put;
760 
761 	/* set stream on register */
762 	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
763 			IMX219_MODE_STREAMING, NULL);
764 	if (ret)
765 		goto err_rpm_put;
766 
767 	/* vflip and hflip cannot change during streaming */
768 	__v4l2_ctrl_grab(imx219->vflip, true);
769 	__v4l2_ctrl_grab(imx219->hflip, true);
770 
771 	return 0;
772 
773 err_rpm_put:
774 	pm_runtime_put_autosuspend(&client->dev);
775 	return ret;
776 }
777 
imx219_disable_streams(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 pad,u64 streams_mask)778 static int imx219_disable_streams(struct v4l2_subdev *sd,
779 				  struct v4l2_subdev_state *state, u32 pad,
780 				  u64 streams_mask)
781 {
782 	struct imx219 *imx219 = to_imx219(sd);
783 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
784 	int ret;
785 
786 	/* set stream off register */
787 	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
788 			IMX219_MODE_STANDBY, NULL);
789 	if (ret)
790 		dev_err(&client->dev, "%s failed to set stream\n", __func__);
791 
792 	__v4l2_ctrl_grab(imx219->vflip, false);
793 	__v4l2_ctrl_grab(imx219->hflip, false);
794 
795 	pm_runtime_put_autosuspend(&client->dev);
796 
797 	return ret;
798 }
799 
imx219_update_pad_format(struct imx219 * imx219,const struct imx219_mode * mode,struct v4l2_mbus_framefmt * fmt,u32 code)800 static void imx219_update_pad_format(struct imx219 *imx219,
801 				     const struct imx219_mode *mode,
802 				     struct v4l2_mbus_framefmt *fmt, u32 code)
803 {
804 	/* Bayer order varies with flips */
805 	fmt->code = imx219_get_format_code(imx219, code);
806 	fmt->width = mode->width;
807 	fmt->height = mode->height;
808 	fmt->field = V4L2_FIELD_NONE;
809 	fmt->colorspace = V4L2_COLORSPACE_RAW;
810 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
811 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
812 	fmt->xfer_func = V4L2_XFER_FUNC_NONE;
813 }
814 
imx219_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_mbus_code_enum * code)815 static int imx219_enum_mbus_code(struct v4l2_subdev *sd,
816 				 struct v4l2_subdev_state *state,
817 				 struct v4l2_subdev_mbus_code_enum *code)
818 {
819 	struct imx219 *imx219 = to_imx219(sd);
820 
821 	if (code->index >= (ARRAY_SIZE(imx219_mbus_formats) / 4))
822 		return -EINVAL;
823 
824 	code->code = imx219_get_format_code(imx219, imx219_mbus_formats[code->index * 4]);
825 
826 	return 0;
827 }
828 
imx219_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_size_enum * fse)829 static int imx219_enum_frame_size(struct v4l2_subdev *sd,
830 				  struct v4l2_subdev_state *state,
831 				  struct v4l2_subdev_frame_size_enum *fse)
832 {
833 	struct imx219 *imx219 = to_imx219(sd);
834 	u32 code;
835 
836 	if (fse->index >= ARRAY_SIZE(supported_modes))
837 		return -EINVAL;
838 
839 	code = imx219_get_format_code(imx219, fse->code);
840 	if (fse->code != code)
841 		return -EINVAL;
842 
843 	fse->min_width = supported_modes[fse->index].width;
844 	fse->max_width = fse->min_width;
845 	fse->min_height = supported_modes[fse->index].height;
846 	fse->max_height = fse->min_height;
847 
848 	return 0;
849 }
850 
imx219_set_pad_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * fmt)851 static int imx219_set_pad_format(struct v4l2_subdev *sd,
852 				 struct v4l2_subdev_state *state,
853 				 struct v4l2_subdev_format *fmt)
854 {
855 	struct imx219 *imx219 = to_imx219(sd);
856 	const struct imx219_mode *mode;
857 	struct v4l2_mbus_framefmt *format;
858 	struct v4l2_rect *crop;
859 	u8 bin_h, bin_v;
860 	u32 prev_line_len;
861 
862 	format = v4l2_subdev_state_get_format(state, 0);
863 	prev_line_len = format->width + imx219->hblank->val;
864 
865 	mode = v4l2_find_nearest_size(supported_modes,
866 				      ARRAY_SIZE(supported_modes),
867 				      width, height,
868 				      fmt->format.width, fmt->format.height);
869 
870 	imx219_update_pad_format(imx219, mode, &fmt->format, fmt->format.code);
871 	*format = fmt->format;
872 
873 	/*
874 	 * Use binning to maximize the crop rectangle size, and centre it in the
875 	 * sensor.
876 	 */
877 	bin_h = min(IMX219_PIXEL_ARRAY_WIDTH / format->width, 2U);
878 	bin_v = min(IMX219_PIXEL_ARRAY_HEIGHT / format->height, 2U);
879 
880 	crop = v4l2_subdev_state_get_crop(state, 0);
881 	crop->width = format->width * bin_h;
882 	crop->height = format->height * bin_v;
883 	crop->left = (IMX219_NATIVE_WIDTH - crop->width) / 2;
884 	crop->top = (IMX219_NATIVE_HEIGHT - crop->height) / 2;
885 
886 	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
887 		int exposure_max;
888 		int exposure_def;
889 		int hblank, llp_min;
890 		int pixel_rate;
891 
892 		/* Update limits and set FPS to default */
893 		__v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
894 					 IMX219_FLL_MAX - mode->height, 1,
895 					 mode->fll_def - mode->height);
896 		__v4l2_ctrl_s_ctrl(imx219->vblank,
897 				   mode->fll_def - mode->height);
898 		/* Update max exposure while meeting expected vblanking */
899 		exposure_max = mode->fll_def - 4;
900 		exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
901 			exposure_max : IMX219_EXPOSURE_DEFAULT;
902 		__v4l2_ctrl_modify_range(imx219->exposure,
903 					 imx219->exposure->minimum,
904 					 exposure_max, imx219->exposure->step,
905 					 exposure_def);
906 
907 		/*
908 		 * With analog binning the default minimum line length of 3448
909 		 * can cause artefacts with RAW10 formats, because the ADC
910 		 * operates on two lines together. So we switch to a higher
911 		 * minimum of 3560.
912 		 */
913 		imx219_get_binning(state, &bin_h, &bin_v);
914 		llp_min = (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ?
915 				  IMX219_BINNED_LLP_MIN : IMX219_LLP_MIN;
916 		__v4l2_ctrl_modify_range(imx219->hblank, llp_min - mode->width,
917 					 IMX219_LLP_MAX - mode->width, 1,
918 					 llp_min - mode->width);
919 		/*
920 		 * Retain PPL setting from previous mode so that the
921 		 * line time does not change on a mode change.
922 		 * Limits have to be recomputed as the controls define
923 		 * the blanking only, so PPL values need to have the
924 		 * mode width subtracted.
925 		 */
926 		hblank = prev_line_len - mode->width;
927 		__v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
928 
929 		/* Scale the pixel rate based on the mode specific factor */
930 		pixel_rate = imx219_get_pixel_rate(imx219) *
931 			     imx219_get_rate_factor(state);
932 		__v4l2_ctrl_modify_range(imx219->pixel_rate, pixel_rate,
933 					 pixel_rate, 1, pixel_rate);
934 	}
935 
936 	return 0;
937 }
938 
imx219_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)939 static int imx219_get_selection(struct v4l2_subdev *sd,
940 				struct v4l2_subdev_state *state,
941 				struct v4l2_subdev_selection *sel)
942 {
943 	switch (sel->target) {
944 	case V4L2_SEL_TGT_CROP:
945 		sel->r = *v4l2_subdev_state_get_crop(state, 0);
946 		return 0;
947 
948 	case V4L2_SEL_TGT_NATIVE_SIZE:
949 		sel->r.top = 0;
950 		sel->r.left = 0;
951 		sel->r.width = IMX219_NATIVE_WIDTH;
952 		sel->r.height = IMX219_NATIVE_HEIGHT;
953 
954 		return 0;
955 
956 	case V4L2_SEL_TGT_CROP_DEFAULT:
957 	case V4L2_SEL_TGT_CROP_BOUNDS:
958 		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
959 		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
960 		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
961 		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
962 
963 		return 0;
964 	}
965 
966 	return -EINVAL;
967 }
968 
imx219_init_state(struct v4l2_subdev * sd,struct v4l2_subdev_state * state)969 static int imx219_init_state(struct v4l2_subdev *sd,
970 			     struct v4l2_subdev_state *state)
971 {
972 	struct v4l2_subdev_format fmt = {
973 		.which = V4L2_SUBDEV_FORMAT_TRY,
974 		.pad = 0,
975 		.format = {
976 			.code = MEDIA_BUS_FMT_SRGGB10_1X10,
977 			.width = supported_modes[0].width,
978 			.height = supported_modes[0].height,
979 		},
980 	};
981 
982 	imx219_set_pad_format(sd, state, &fmt);
983 
984 	return 0;
985 }
986 
987 static const struct v4l2_subdev_video_ops imx219_video_ops = {
988 	.s_stream = v4l2_subdev_s_stream_helper,
989 };
990 
991 static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
992 	.enum_mbus_code = imx219_enum_mbus_code,
993 	.get_fmt = v4l2_subdev_get_fmt,
994 	.set_fmt = imx219_set_pad_format,
995 	.get_selection = imx219_get_selection,
996 	.enum_frame_size = imx219_enum_frame_size,
997 	.enable_streams = imx219_enable_streams,
998 	.disable_streams = imx219_disable_streams,
999 };
1000 
1001 static const struct v4l2_subdev_ops imx219_subdev_ops = {
1002 	.video = &imx219_video_ops,
1003 	.pad = &imx219_pad_ops,
1004 };
1005 
1006 static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
1007 	.init_state = imx219_init_state,
1008 };
1009 
1010 /* -----------------------------------------------------------------------------
1011  * Power management
1012  */
1013 
imx219_power_on(struct device * dev)1014 static int imx219_power_on(struct device *dev)
1015 {
1016 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1017 	struct imx219 *imx219 = to_imx219(sd);
1018 	int ret;
1019 
1020 	ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
1021 				    imx219->supplies);
1022 	if (ret) {
1023 		dev_err(dev, "%s: failed to enable regulators\n",
1024 			__func__);
1025 		return ret;
1026 	}
1027 
1028 	ret = clk_prepare_enable(imx219->xclk);
1029 	if (ret) {
1030 		dev_err(dev, "%s: failed to enable clock\n",
1031 			__func__);
1032 		goto reg_off;
1033 	}
1034 
1035 	gpiod_set_value_cansleep(imx219->reset_gpio, 1);
1036 	usleep_range(IMX219_XCLR_MIN_DELAY_US,
1037 		     IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US);
1038 
1039 	return 0;
1040 
1041 reg_off:
1042 	regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1043 
1044 	return ret;
1045 }
1046 
imx219_power_off(struct device * dev)1047 static int imx219_power_off(struct device *dev)
1048 {
1049 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1050 	struct imx219 *imx219 = to_imx219(sd);
1051 
1052 	gpiod_set_value_cansleep(imx219->reset_gpio, 0);
1053 	regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1054 	clk_disable_unprepare(imx219->xclk);
1055 
1056 	return 0;
1057 }
1058 
1059 /* -----------------------------------------------------------------------------
1060  * Probe & remove
1061  */
1062 
imx219_get_regulators(struct imx219 * imx219)1063 static int imx219_get_regulators(struct imx219 *imx219)
1064 {
1065 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1066 	unsigned int i;
1067 
1068 	for (i = 0; i < IMX219_NUM_SUPPLIES; i++)
1069 		imx219->supplies[i].supply = imx219_supply_name[i];
1070 
1071 	return devm_regulator_bulk_get(&client->dev,
1072 				       IMX219_NUM_SUPPLIES,
1073 				       imx219->supplies);
1074 }
1075 
1076 /* Verify chip ID */
imx219_identify_module(struct imx219 * imx219)1077 static int imx219_identify_module(struct imx219 *imx219)
1078 {
1079 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1080 	int ret;
1081 	u64 val;
1082 
1083 	ret = cci_read(imx219->regmap, IMX219_REG_CHIP_ID, &val, NULL);
1084 	if (ret)
1085 		return dev_err_probe(&client->dev, ret,
1086 				     "failed to read chip id %x\n",
1087 				     IMX219_CHIP_ID);
1088 
1089 	if (val != IMX219_CHIP_ID)
1090 		return dev_err_probe(&client->dev, -EIO,
1091 				     "chip id mismatch: %x!=%llx\n",
1092 				     IMX219_CHIP_ID, val);
1093 
1094 	return 0;
1095 }
1096 
imx219_check_hwcfg(struct device * dev,struct imx219 * imx219)1097 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
1098 {
1099 	struct fwnode_handle *endpoint;
1100 	struct v4l2_fwnode_endpoint ep_cfg = {
1101 		.bus_type = V4L2_MBUS_CSI2_DPHY
1102 	};
1103 	unsigned long link_freq_bitmap;
1104 	int ret = -EINVAL;
1105 
1106 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1107 	if (!endpoint)
1108 		return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
1109 
1110 	if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1111 		dev_err_probe(dev, -EINVAL, "could not parse endpoint\n");
1112 		goto error_out;
1113 	}
1114 
1115 	/* Check the number of MIPI CSI2 data lanes */
1116 	if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2 &&
1117 	    ep_cfg.bus.mipi_csi2.num_data_lanes != 4) {
1118 		dev_err_probe(dev, -EINVAL,
1119 			      "only 2 or 4 data lanes are currently supported\n");
1120 		goto error_out;
1121 	}
1122 	imx219->lanes = ep_cfg.bus.mipi_csi2.num_data_lanes;
1123 
1124 	/* Check the link frequency set in device tree */
1125 	switch (imx219->lanes) {
1126 	case 2:
1127 		ret = v4l2_link_freq_to_bitmap(dev,
1128 					       ep_cfg.link_frequencies,
1129 					       ep_cfg.nr_of_link_frequencies,
1130 					       imx219_link_freq_menu,
1131 					       ARRAY_SIZE(imx219_link_freq_menu),
1132 					       &link_freq_bitmap);
1133 		break;
1134 	case 4:
1135 		ret = v4l2_link_freq_to_bitmap(dev,
1136 					       ep_cfg.link_frequencies,
1137 					       ep_cfg.nr_of_link_frequencies,
1138 					       imx219_link_freq_4lane_menu,
1139 					       ARRAY_SIZE(imx219_link_freq_4lane_menu),
1140 					       &link_freq_bitmap);
1141 
1142 		if (!ret && (link_freq_bitmap & BIT(1))) {
1143 			dev_warn(dev, "Link frequency of %d not supported, but has been incorrectly advertised previously\n",
1144 				 IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED);
1145 			dev_warn(dev, "Using link frequency of %d\n",
1146 				 IMX219_DEFAULT_LINK_FREQ_4LANE);
1147 			link_freq_bitmap |= BIT(0);
1148 		}
1149 		break;
1150 	}
1151 
1152 	if (ret || !(link_freq_bitmap & BIT(0))) {
1153 		ret = -EINVAL;
1154 		dev_err_probe(dev, -EINVAL,
1155 			      "Link frequency not supported: %lld\n",
1156 			      ep_cfg.link_frequencies[0]);
1157 	}
1158 
1159 error_out:
1160 	v4l2_fwnode_endpoint_free(&ep_cfg);
1161 	fwnode_handle_put(endpoint);
1162 
1163 	return ret;
1164 }
1165 
imx219_probe(struct i2c_client * client)1166 static int imx219_probe(struct i2c_client *client)
1167 {
1168 	struct device *dev = &client->dev;
1169 	struct imx219 *imx219;
1170 	int ret;
1171 
1172 	imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL);
1173 	if (!imx219)
1174 		return -ENOMEM;
1175 
1176 	v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops);
1177 	imx219->sd.internal_ops = &imx219_internal_ops;
1178 
1179 	/* Check the hardware configuration in device tree */
1180 	if (imx219_check_hwcfg(dev, imx219))
1181 		return -EINVAL;
1182 
1183 	imx219->regmap = devm_cci_regmap_init_i2c(client, 16);
1184 	if (IS_ERR(imx219->regmap))
1185 		return dev_err_probe(dev, PTR_ERR(imx219->regmap),
1186 				     "failed to initialize CCI\n");
1187 
1188 	/* Get system clock (xclk) */
1189 	imx219->xclk = devm_clk_get(dev, NULL);
1190 	if (IS_ERR(imx219->xclk))
1191 		return dev_err_probe(dev, PTR_ERR(imx219->xclk),
1192 				     "failed to get xclk\n");
1193 
1194 	imx219->xclk_freq = clk_get_rate(imx219->xclk);
1195 	if (imx219->xclk_freq != IMX219_XCLK_FREQ)
1196 		return dev_err_probe(dev, -EINVAL,
1197 				     "xclk frequency not supported: %d Hz\n",
1198 				     imx219->xclk_freq);
1199 
1200 	ret = imx219_get_regulators(imx219);
1201 	if (ret)
1202 		return dev_err_probe(dev, ret, "failed to get regulators\n");
1203 
1204 	/* Request optional enable pin */
1205 	imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1206 						     GPIOD_OUT_HIGH);
1207 
1208 	/*
1209 	 * The sensor must be powered for imx219_identify_module()
1210 	 * to be able to read the CHIP_ID register
1211 	 */
1212 	ret = imx219_power_on(dev);
1213 	if (ret)
1214 		return ret;
1215 
1216 	ret = imx219_identify_module(imx219);
1217 	if (ret)
1218 		goto error_power_off;
1219 
1220 	/*
1221 	 * Sensor doesn't enter LP-11 state upon power up until and unless
1222 	 * streaming is started, so upon power up switch the modes to:
1223 	 * streaming -> standby
1224 	 */
1225 	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
1226 			IMX219_MODE_STREAMING, NULL);
1227 	if (ret < 0)
1228 		goto error_power_off;
1229 
1230 	usleep_range(100, 110);
1231 
1232 	/* put sensor back to standby mode */
1233 	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
1234 			IMX219_MODE_STANDBY, NULL);
1235 	if (ret < 0)
1236 		goto error_power_off;
1237 
1238 	usleep_range(100, 110);
1239 
1240 	ret = imx219_init_controls(imx219);
1241 	if (ret)
1242 		goto error_power_off;
1243 
1244 	/* Initialize subdev */
1245 	imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1246 	imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1247 
1248 	/* Initialize source pad */
1249 	imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
1250 
1251 	ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
1252 	if (ret) {
1253 		dev_err_probe(dev, ret, "failed to init entity pads\n");
1254 		goto error_handler_free;
1255 	}
1256 
1257 	imx219->sd.state_lock = imx219->ctrl_handler.lock;
1258 	ret = v4l2_subdev_init_finalize(&imx219->sd);
1259 	if (ret < 0) {
1260 		dev_err_probe(dev, ret, "subdev init error\n");
1261 		goto error_media_entity;
1262 	}
1263 
1264 	pm_runtime_set_active(dev);
1265 	pm_runtime_enable(dev);
1266 
1267 	ret = v4l2_async_register_subdev_sensor(&imx219->sd);
1268 	if (ret < 0) {
1269 		dev_err_probe(dev, ret,
1270 			      "failed to register sensor sub-device\n");
1271 		goto error_subdev_cleanup;
1272 	}
1273 
1274 	pm_runtime_idle(dev);
1275 	pm_runtime_set_autosuspend_delay(dev, 1000);
1276 	pm_runtime_use_autosuspend(dev);
1277 
1278 	return 0;
1279 
1280 error_subdev_cleanup:
1281 	v4l2_subdev_cleanup(&imx219->sd);
1282 	pm_runtime_disable(dev);
1283 	pm_runtime_set_suspended(dev);
1284 
1285 error_media_entity:
1286 	media_entity_cleanup(&imx219->sd.entity);
1287 
1288 error_handler_free:
1289 	imx219_free_controls(imx219);
1290 
1291 error_power_off:
1292 	imx219_power_off(dev);
1293 
1294 	return ret;
1295 }
1296 
imx219_remove(struct i2c_client * client)1297 static void imx219_remove(struct i2c_client *client)
1298 {
1299 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1300 	struct imx219 *imx219 = to_imx219(sd);
1301 
1302 	v4l2_async_unregister_subdev(sd);
1303 	v4l2_subdev_cleanup(sd);
1304 	media_entity_cleanup(&sd->entity);
1305 	imx219_free_controls(imx219);
1306 
1307 	pm_runtime_disable(&client->dev);
1308 	if (!pm_runtime_status_suspended(&client->dev)) {
1309 		imx219_power_off(&client->dev);
1310 		pm_runtime_set_suspended(&client->dev);
1311 	}
1312 }
1313 
1314 static const struct of_device_id imx219_dt_ids[] = {
1315 	{ .compatible = "sony,imx219" },
1316 	{ /* sentinel */ }
1317 };
1318 MODULE_DEVICE_TABLE(of, imx219_dt_ids);
1319 
1320 static const struct dev_pm_ops imx219_pm_ops = {
1321 	SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL)
1322 };
1323 
1324 static struct i2c_driver imx219_i2c_driver = {
1325 	.driver = {
1326 		.name = "imx219",
1327 		.of_match_table	= imx219_dt_ids,
1328 		.pm = &imx219_pm_ops,
1329 	},
1330 	.probe = imx219_probe,
1331 	.remove = imx219_remove,
1332 };
1333 
1334 module_i2c_driver(imx219_i2c_driver);
1335 
1336 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1337 MODULE_DESCRIPTION("Sony IMX219 sensor driver");
1338 MODULE_LICENSE("GPL v2");
1339