xref: /openbmc/linux/drivers/media/i2c/imx219.c (revision 78bb17f7)
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/clk-provider.h>
19 #include <linux/clkdev.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regulator/consumer.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/v4l2-mediabus.h>
31 #include <asm/unaligned.h>
32 
33 #define IMX219_REG_VALUE_08BIT		1
34 #define IMX219_REG_VALUE_16BIT		2
35 
36 #define IMX219_REG_MODE_SELECT		0x0100
37 #define IMX219_MODE_STANDBY		0x00
38 #define IMX219_MODE_STREAMING		0x01
39 
40 /* Chip ID */
41 #define IMX219_REG_CHIP_ID		0x0000
42 #define IMX219_CHIP_ID			0x0219
43 
44 /* External clock frequency is 24.0M */
45 #define IMX219_XCLK_FREQ		24000000
46 
47 /* Pixel rate is fixed at 182.4M for all the modes */
48 #define IMX219_PIXEL_RATE		182400000
49 
50 #define IMX219_DEFAULT_LINK_FREQ	456000000
51 
52 /* V_TIMING internal */
53 #define IMX219_REG_VTS			0x0160
54 #define IMX219_VTS_15FPS		0x0dc6
55 #define IMX219_VTS_30FPS_1080P		0x06e3
56 #define IMX219_VTS_30FPS_BINNED		0x06e3
57 #define IMX219_VTS_30FPS_640x480	0x06e3
58 #define IMX219_VTS_MAX			0xffff
59 
60 #define IMX219_VBLANK_MIN		4
61 
62 /*Frame Length Line*/
63 #define IMX219_FLL_MIN			0x08a6
64 #define IMX219_FLL_MAX			0xffff
65 #define IMX219_FLL_STEP			1
66 #define IMX219_FLL_DEFAULT		0x0c98
67 
68 /* HBLANK control - read only */
69 #define IMX219_PPL_DEFAULT		3448
70 
71 /* Exposure control */
72 #define IMX219_REG_EXPOSURE		0x015a
73 #define IMX219_EXPOSURE_MIN		4
74 #define IMX219_EXPOSURE_STEP		1
75 #define IMX219_EXPOSURE_DEFAULT		0x640
76 #define IMX219_EXPOSURE_MAX		65535
77 
78 /* Analog gain control */
79 #define IMX219_REG_ANALOG_GAIN		0x0157
80 #define IMX219_ANA_GAIN_MIN		0
81 #define IMX219_ANA_GAIN_MAX		232
82 #define IMX219_ANA_GAIN_STEP		1
83 #define IMX219_ANA_GAIN_DEFAULT		0x0
84 
85 /* Digital gain control */
86 #define IMX219_REG_DIGITAL_GAIN		0x0158
87 #define IMX219_DGTL_GAIN_MIN		0x0100
88 #define IMX219_DGTL_GAIN_MAX		0x0fff
89 #define IMX219_DGTL_GAIN_DEFAULT	0x0100
90 #define IMX219_DGTL_GAIN_STEP		1
91 
92 #define IMX219_REG_ORIENTATION		0x0172
93 
94 /* Test Pattern Control */
95 #define IMX219_REG_TEST_PATTERN		0x0600
96 #define IMX219_TEST_PATTERN_DISABLE	0
97 #define IMX219_TEST_PATTERN_SOLID_COLOR	1
98 #define IMX219_TEST_PATTERN_COLOR_BARS	2
99 #define IMX219_TEST_PATTERN_GREY_COLOR	3
100 #define IMX219_TEST_PATTERN_PN9		4
101 
102 /* Test pattern colour components */
103 #define IMX219_REG_TESTP_RED		0x0602
104 #define IMX219_REG_TESTP_GREENR		0x0604
105 #define IMX219_REG_TESTP_BLUE		0x0606
106 #define IMX219_REG_TESTP_GREENB		0x0608
107 #define IMX219_TESTP_COLOUR_MIN		0
108 #define IMX219_TESTP_COLOUR_MAX		0x03ff
109 #define IMX219_TESTP_COLOUR_STEP	1
110 #define IMX219_TESTP_RED_DEFAULT	IMX219_TESTP_COLOUR_MAX
111 #define IMX219_TESTP_GREENR_DEFAULT	0
112 #define IMX219_TESTP_BLUE_DEFAULT	0
113 #define IMX219_TESTP_GREENB_DEFAULT	0
114 
115 struct imx219_reg {
116 	u16 address;
117 	u8 val;
118 };
119 
120 struct imx219_reg_list {
121 	unsigned int num_of_regs;
122 	const struct imx219_reg *regs;
123 };
124 
125 /* Mode : resolution and related config&values */
126 struct imx219_mode {
127 	/* Frame width */
128 	unsigned int width;
129 	/* Frame height */
130 	unsigned int height;
131 
132 	/* V-timing */
133 	unsigned int vts_def;
134 
135 	/* Default register values */
136 	struct imx219_reg_list reg_list;
137 };
138 
139 /*
140  * Register sets lifted off the i2C interface from the Raspberry Pi firmware
141  * driver.
142  * 3280x2464 = mode 2, 1920x1080 = mode 1, 1640x1232 = mode 4, 640x480 = mode 7.
143  */
144 static const struct imx219_reg mode_3280x2464_regs[] = {
145 	{0x0100, 0x00},
146 	{0x30eb, 0x0c},
147 	{0x30eb, 0x05},
148 	{0x300a, 0xff},
149 	{0x300b, 0xff},
150 	{0x30eb, 0x05},
151 	{0x30eb, 0x09},
152 	{0x0114, 0x01},
153 	{0x0128, 0x00},
154 	{0x012a, 0x18},
155 	{0x012b, 0x00},
156 	{0x0164, 0x00},
157 	{0x0165, 0x00},
158 	{0x0166, 0x0c},
159 	{0x0167, 0xcf},
160 	{0x0168, 0x00},
161 	{0x0169, 0x00},
162 	{0x016a, 0x09},
163 	{0x016b, 0x9f},
164 	{0x016c, 0x0c},
165 	{0x016d, 0xd0},
166 	{0x016e, 0x09},
167 	{0x016f, 0xa0},
168 	{0x0170, 0x01},
169 	{0x0171, 0x01},
170 	{0x0174, 0x00},
171 	{0x0175, 0x00},
172 	{0x0301, 0x05},
173 	{0x0303, 0x01},
174 	{0x0304, 0x03},
175 	{0x0305, 0x03},
176 	{0x0306, 0x00},
177 	{0x0307, 0x39},
178 	{0x030b, 0x01},
179 	{0x030c, 0x00},
180 	{0x030d, 0x72},
181 	{0x0624, 0x0c},
182 	{0x0625, 0xd0},
183 	{0x0626, 0x09},
184 	{0x0627, 0xa0},
185 	{0x455e, 0x00},
186 	{0x471e, 0x4b},
187 	{0x4767, 0x0f},
188 	{0x4750, 0x14},
189 	{0x4540, 0x00},
190 	{0x47b4, 0x14},
191 	{0x4713, 0x30},
192 	{0x478b, 0x10},
193 	{0x478f, 0x10},
194 	{0x4793, 0x10},
195 	{0x4797, 0x0e},
196 	{0x479b, 0x0e},
197 	{0x0162, 0x0d},
198 	{0x0163, 0x78},
199 };
200 
201 static const struct imx219_reg mode_1920_1080_regs[] = {
202 	{0x0100, 0x00},
203 	{0x30eb, 0x05},
204 	{0x30eb, 0x0c},
205 	{0x300a, 0xff},
206 	{0x300b, 0xff},
207 	{0x30eb, 0x05},
208 	{0x30eb, 0x09},
209 	{0x0114, 0x01},
210 	{0x0128, 0x00},
211 	{0x012a, 0x18},
212 	{0x012b, 0x00},
213 	{0x0162, 0x0d},
214 	{0x0163, 0x78},
215 	{0x0164, 0x02},
216 	{0x0165, 0xa8},
217 	{0x0166, 0x0a},
218 	{0x0167, 0x27},
219 	{0x0168, 0x02},
220 	{0x0169, 0xb4},
221 	{0x016a, 0x06},
222 	{0x016b, 0xeb},
223 	{0x016c, 0x07},
224 	{0x016d, 0x80},
225 	{0x016e, 0x04},
226 	{0x016f, 0x38},
227 	{0x0170, 0x01},
228 	{0x0171, 0x01},
229 	{0x0174, 0x00},
230 	{0x0175, 0x00},
231 	{0x0301, 0x05},
232 	{0x0303, 0x01},
233 	{0x0304, 0x03},
234 	{0x0305, 0x03},
235 	{0x0306, 0x00},
236 	{0x0307, 0x39},
237 	{0x030b, 0x01},
238 	{0x030c, 0x00},
239 	{0x030d, 0x72},
240 	{0x0624, 0x07},
241 	{0x0625, 0x80},
242 	{0x0626, 0x04},
243 	{0x0627, 0x38},
244 	{0x455e, 0x00},
245 	{0x471e, 0x4b},
246 	{0x4767, 0x0f},
247 	{0x4750, 0x14},
248 	{0x4540, 0x00},
249 	{0x47b4, 0x14},
250 	{0x4713, 0x30},
251 	{0x478b, 0x10},
252 	{0x478f, 0x10},
253 	{0x4793, 0x10},
254 	{0x4797, 0x0e},
255 	{0x479b, 0x0e},
256 	{0x0162, 0x0d},
257 	{0x0163, 0x78},
258 };
259 
260 static const struct imx219_reg mode_1640_1232_regs[] = {
261 	{0x0100, 0x00},
262 	{0x30eb, 0x0c},
263 	{0x30eb, 0x05},
264 	{0x300a, 0xff},
265 	{0x300b, 0xff},
266 	{0x30eb, 0x05},
267 	{0x30eb, 0x09},
268 	{0x0114, 0x01},
269 	{0x0128, 0x00},
270 	{0x012a, 0x18},
271 	{0x012b, 0x00},
272 	{0x0164, 0x00},
273 	{0x0165, 0x00},
274 	{0x0166, 0x0c},
275 	{0x0167, 0xcf},
276 	{0x0168, 0x00},
277 	{0x0169, 0x00},
278 	{0x016a, 0x09},
279 	{0x016b, 0x9f},
280 	{0x016c, 0x06},
281 	{0x016d, 0x68},
282 	{0x016e, 0x04},
283 	{0x016f, 0xd0},
284 	{0x0170, 0x01},
285 	{0x0171, 0x01},
286 	{0x0174, 0x01},
287 	{0x0175, 0x01},
288 	{0x0301, 0x05},
289 	{0x0303, 0x01},
290 	{0x0304, 0x03},
291 	{0x0305, 0x03},
292 	{0x0306, 0x00},
293 	{0x0307, 0x39},
294 	{0x030b, 0x01},
295 	{0x030c, 0x00},
296 	{0x030d, 0x72},
297 	{0x0624, 0x06},
298 	{0x0625, 0x68},
299 	{0x0626, 0x04},
300 	{0x0627, 0xd0},
301 	{0x455e, 0x00},
302 	{0x471e, 0x4b},
303 	{0x4767, 0x0f},
304 	{0x4750, 0x14},
305 	{0x4540, 0x00},
306 	{0x47b4, 0x14},
307 	{0x4713, 0x30},
308 	{0x478b, 0x10},
309 	{0x478f, 0x10},
310 	{0x4793, 0x10},
311 	{0x4797, 0x0e},
312 	{0x479b, 0x0e},
313 	{0x0162, 0x0d},
314 	{0x0163, 0x78},
315 };
316 
317 static const struct imx219_reg mode_640_480_regs[] = {
318 	{0x0100, 0x00},
319 	{0x30eb, 0x05},
320 	{0x30eb, 0x0c},
321 	{0x300a, 0xff},
322 	{0x300b, 0xff},
323 	{0x30eb, 0x05},
324 	{0x30eb, 0x09},
325 	{0x0114, 0x01},
326 	{0x0128, 0x00},
327 	{0x012a, 0x18},
328 	{0x012b, 0x00},
329 	{0x0162, 0x0d},
330 	{0x0163, 0x78},
331 	{0x0164, 0x03},
332 	{0x0165, 0xe8},
333 	{0x0166, 0x08},
334 	{0x0167, 0xe7},
335 	{0x0168, 0x02},
336 	{0x0169, 0xf0},
337 	{0x016a, 0x06},
338 	{0x016b, 0xaf},
339 	{0x016c, 0x02},
340 	{0x016d, 0x80},
341 	{0x016e, 0x01},
342 	{0x016f, 0xe0},
343 	{0x0170, 0x01},
344 	{0x0171, 0x01},
345 	{0x0174, 0x03},
346 	{0x0175, 0x03},
347 	{0x0301, 0x05},
348 	{0x0303, 0x01},
349 	{0x0304, 0x03},
350 	{0x0305, 0x03},
351 	{0x0306, 0x00},
352 	{0x0307, 0x39},
353 	{0x030b, 0x01},
354 	{0x030c, 0x00},
355 	{0x030d, 0x72},
356 	{0x0624, 0x06},
357 	{0x0625, 0x68},
358 	{0x0626, 0x04},
359 	{0x0627, 0xd0},
360 	{0x455e, 0x00},
361 	{0x471e, 0x4b},
362 	{0x4767, 0x0f},
363 	{0x4750, 0x14},
364 	{0x4540, 0x00},
365 	{0x47b4, 0x14},
366 	{0x4713, 0x30},
367 	{0x478b, 0x10},
368 	{0x478f, 0x10},
369 	{0x4793, 0x10},
370 	{0x4797, 0x0e},
371 	{0x479b, 0x0e},
372 };
373 
374 static const struct imx219_reg raw8_framefmt_regs[] = {
375 	{0x018c, 0x08},
376 	{0x018d, 0x08},
377 	{0x0309, 0x08},
378 };
379 
380 static const struct imx219_reg raw10_framefmt_regs[] = {
381 	{0x018c, 0x0a},
382 	{0x018d, 0x0a},
383 	{0x0309, 0x0a},
384 };
385 
386 static const char * const imx219_test_pattern_menu[] = {
387 	"Disabled",
388 	"Color Bars",
389 	"Solid Color",
390 	"Grey Color Bars",
391 	"PN9"
392 };
393 
394 static const int imx219_test_pattern_val[] = {
395 	IMX219_TEST_PATTERN_DISABLE,
396 	IMX219_TEST_PATTERN_COLOR_BARS,
397 	IMX219_TEST_PATTERN_SOLID_COLOR,
398 	IMX219_TEST_PATTERN_GREY_COLOR,
399 	IMX219_TEST_PATTERN_PN9,
400 };
401 
402 /* regulator supplies */
403 static const char * const imx219_supply_name[] = {
404 	/* Supplies can be enabled in any order */
405 	"VANA",  /* Analog (2.8V) supply */
406 	"VDIG",  /* Digital Core (1.8V) supply */
407 	"VDDL",  /* IF (1.2V) supply */
408 };
409 
410 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
411 
412 /*
413  * The supported formats.
414  * This table MUST contain 4 entries per format, to cover the various flip
415  * combinations in the order
416  * - no flip
417  * - h flip
418  * - v flip
419  * - h&v flips
420  */
421 static const u32 codes[] = {
422 	MEDIA_BUS_FMT_SRGGB10_1X10,
423 	MEDIA_BUS_FMT_SGRBG10_1X10,
424 	MEDIA_BUS_FMT_SGBRG10_1X10,
425 	MEDIA_BUS_FMT_SBGGR10_1X10,
426 
427 	MEDIA_BUS_FMT_SRGGB8_1X8,
428 	MEDIA_BUS_FMT_SGRBG8_1X8,
429 	MEDIA_BUS_FMT_SGBRG8_1X8,
430 	MEDIA_BUS_FMT_SBGGR8_1X8,
431 };
432 
433 /*
434  * Initialisation delay between XCLR low->high and the moment when the sensor
435  * can start capture (i.e. can leave software stanby) must be not less than:
436  *   t4 + max(t5, t6 + <time to initialize the sensor register over I2C>)
437  * where
438  *   t4 is fixed, and is max 200uS,
439  *   t5 is fixed, and is 6000uS,
440  *   t6 depends on the sensor external clock, and is max 32000 clock periods.
441  * As per sensor datasheet, the external clock must be from 6MHz to 27MHz.
442  * So for any acceptable external clock t6 is always within the range of
443  * 1185 to 5333 uS, and is always less than t5.
444  * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then
445  * initialize the sensor over I2C, and then exit the software standby.
446  *
447  * This start-up time can be optimized a bit more, if we start the writes
448  * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor
449  * initialization over I2C may complete before (t4+t5) expires, and we must
450  * ensure that capture is not started before (t4+t5).
451  *
452  * This delay doesn't account for the power supply startup time. If needed,
453  * this should be taken care of via the regulator framework. E.g. in the
454  * case of DT for regulator-fixed one should define the startup-delay-us
455  * property.
456  */
457 #define IMX219_XCLR_MIN_DELAY_US	6200
458 #define IMX219_XCLR_DELAY_RANGE_US	1000
459 
460 /* Mode configs */
461 static const struct imx219_mode supported_modes[] = {
462 	{
463 		/* 8MPix 15fps mode */
464 		.width = 3280,
465 		.height = 2464,
466 		.vts_def = IMX219_VTS_15FPS,
467 		.reg_list = {
468 			.num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
469 			.regs = mode_3280x2464_regs,
470 		},
471 	},
472 	{
473 		/* 1080P 30fps cropped */
474 		.width = 1920,
475 		.height = 1080,
476 		.vts_def = IMX219_VTS_30FPS_1080P,
477 		.reg_list = {
478 			.num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
479 			.regs = mode_1920_1080_regs,
480 		},
481 	},
482 	{
483 		/* 2x2 binned 30fps mode */
484 		.width = 1640,
485 		.height = 1232,
486 		.vts_def = IMX219_VTS_30FPS_BINNED,
487 		.reg_list = {
488 			.num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
489 			.regs = mode_1640_1232_regs,
490 		},
491 	},
492 	{
493 		/* 640x480 30fps mode */
494 		.width = 640,
495 		.height = 480,
496 		.vts_def = IMX219_VTS_30FPS_640x480,
497 		.reg_list = {
498 			.num_of_regs = ARRAY_SIZE(mode_640_480_regs),
499 			.regs = mode_640_480_regs,
500 		},
501 	},
502 };
503 
504 struct imx219 {
505 	struct v4l2_subdev sd;
506 	struct media_pad pad;
507 
508 	struct v4l2_mbus_framefmt fmt;
509 
510 	struct clk *xclk; /* system clock to IMX219 */
511 	u32 xclk_freq;
512 
513 	struct gpio_desc *reset_gpio;
514 	struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES];
515 
516 	struct v4l2_ctrl_handler ctrl_handler;
517 	/* V4L2 Controls */
518 	struct v4l2_ctrl *pixel_rate;
519 	struct v4l2_ctrl *exposure;
520 	struct v4l2_ctrl *vflip;
521 	struct v4l2_ctrl *hflip;
522 	struct v4l2_ctrl *vblank;
523 	struct v4l2_ctrl *hblank;
524 
525 	/* Current mode */
526 	const struct imx219_mode *mode;
527 
528 	/*
529 	 * Mutex for serialized access:
530 	 * Protect sensor module set pad format and start/stop streaming safely.
531 	 */
532 	struct mutex mutex;
533 
534 	/* Streaming on/off */
535 	bool streaming;
536 };
537 
538 static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd)
539 {
540 	return container_of(_sd, struct imx219, sd);
541 }
542 
543 /* Read registers up to 2 at a time */
544 static int imx219_read_reg(struct imx219 *imx219, u16 reg, u32 len, u32 *val)
545 {
546 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
547 	struct i2c_msg msgs[2];
548 	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
549 	u8 data_buf[4] = { 0, };
550 	int ret;
551 
552 	if (len > 4)
553 		return -EINVAL;
554 
555 	/* Write register address */
556 	msgs[0].addr = client->addr;
557 	msgs[0].flags = 0;
558 	msgs[0].len = ARRAY_SIZE(addr_buf);
559 	msgs[0].buf = addr_buf;
560 
561 	/* Read data from register */
562 	msgs[1].addr = client->addr;
563 	msgs[1].flags = I2C_M_RD;
564 	msgs[1].len = len;
565 	msgs[1].buf = &data_buf[4 - len];
566 
567 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
568 	if (ret != ARRAY_SIZE(msgs))
569 		return -EIO;
570 
571 	*val = get_unaligned_be32(data_buf);
572 
573 	return 0;
574 }
575 
576 /* Write registers up to 2 at a time */
577 static int imx219_write_reg(struct imx219 *imx219, u16 reg, u32 len, u32 val)
578 {
579 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
580 	u8 buf[6];
581 
582 	if (len > 4)
583 		return -EINVAL;
584 
585 	put_unaligned_be16(reg, buf);
586 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
587 	if (i2c_master_send(client, buf, len + 2) != len + 2)
588 		return -EIO;
589 
590 	return 0;
591 }
592 
593 /* Write a list of registers */
594 static int imx219_write_regs(struct imx219 *imx219,
595 			     const struct imx219_reg *regs, u32 len)
596 {
597 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
598 	unsigned int i;
599 	int ret;
600 
601 	for (i = 0; i < len; i++) {
602 		ret = imx219_write_reg(imx219, regs[i].address, 1, regs[i].val);
603 		if (ret) {
604 			dev_err_ratelimited(&client->dev,
605 					    "Failed to write reg 0x%4.4x. error = %d\n",
606 					    regs[i].address, ret);
607 
608 			return ret;
609 		}
610 	}
611 
612 	return 0;
613 }
614 
615 /* Get bayer order based on flip setting. */
616 static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
617 {
618 	unsigned int i;
619 
620 	lockdep_assert_held(&imx219->mutex);
621 
622 	for (i = 0; i < ARRAY_SIZE(codes); i++)
623 		if (codes[i] == code)
624 			break;
625 
626 	if (i >= ARRAY_SIZE(codes))
627 		i = 0;
628 
629 	i = (i & ~3) | (imx219->vflip->val ? 2 : 0) |
630 	    (imx219->hflip->val ? 1 : 0);
631 
632 	return codes[i];
633 }
634 
635 static void imx219_set_default_format(struct imx219 *imx219)
636 {
637 	struct v4l2_mbus_framefmt *fmt;
638 
639 	fmt = &imx219->fmt;
640 	fmt->code = MEDIA_BUS_FMT_SRGGB10_1X10;
641 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
642 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
643 	fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
644 							  fmt->colorspace,
645 							  fmt->ycbcr_enc);
646 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
647 	fmt->width = supported_modes[0].width;
648 	fmt->height = supported_modes[0].height;
649 	fmt->field = V4L2_FIELD_NONE;
650 }
651 
652 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
653 {
654 	struct imx219 *imx219 = to_imx219(sd);
655 	struct v4l2_mbus_framefmt *try_fmt =
656 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
657 
658 	mutex_lock(&imx219->mutex);
659 
660 	/* Initialize try_fmt */
661 	try_fmt->width = supported_modes[0].width;
662 	try_fmt->height = supported_modes[0].height;
663 	try_fmt->code = imx219_get_format_code(imx219,
664 					       MEDIA_BUS_FMT_SRGGB10_1X10);
665 	try_fmt->field = V4L2_FIELD_NONE;
666 
667 	mutex_unlock(&imx219->mutex);
668 
669 	return 0;
670 }
671 
672 static int imx219_set_ctrl(struct v4l2_ctrl *ctrl)
673 {
674 	struct imx219 *imx219 =
675 		container_of(ctrl->handler, struct imx219, ctrl_handler);
676 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
677 	int ret;
678 
679 	if (ctrl->id == V4L2_CID_VBLANK) {
680 		int exposure_max, exposure_def;
681 
682 		/* Update max exposure while meeting expected vblanking */
683 		exposure_max = imx219->mode->height + ctrl->val - 4;
684 		exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
685 			exposure_max : IMX219_EXPOSURE_DEFAULT;
686 		__v4l2_ctrl_modify_range(imx219->exposure,
687 					 imx219->exposure->minimum,
688 					 exposure_max, imx219->exposure->step,
689 					 exposure_def);
690 	}
691 
692 	/*
693 	 * Applying V4L2 control value only happens
694 	 * when power is up for streaming
695 	 */
696 	if (pm_runtime_get_if_in_use(&client->dev) == 0)
697 		return 0;
698 
699 	switch (ctrl->id) {
700 	case V4L2_CID_ANALOGUE_GAIN:
701 		ret = imx219_write_reg(imx219, IMX219_REG_ANALOG_GAIN,
702 				       IMX219_REG_VALUE_08BIT, ctrl->val);
703 		break;
704 	case V4L2_CID_EXPOSURE:
705 		ret = imx219_write_reg(imx219, IMX219_REG_EXPOSURE,
706 				       IMX219_REG_VALUE_16BIT, ctrl->val);
707 		break;
708 	case V4L2_CID_DIGITAL_GAIN:
709 		ret = imx219_write_reg(imx219, IMX219_REG_DIGITAL_GAIN,
710 				       IMX219_REG_VALUE_16BIT, ctrl->val);
711 		break;
712 	case V4L2_CID_TEST_PATTERN:
713 		ret = imx219_write_reg(imx219, IMX219_REG_TEST_PATTERN,
714 				       IMX219_REG_VALUE_16BIT,
715 				       imx219_test_pattern_val[ctrl->val]);
716 		break;
717 	case V4L2_CID_HFLIP:
718 	case V4L2_CID_VFLIP:
719 		ret = imx219_write_reg(imx219, IMX219_REG_ORIENTATION, 1,
720 				       imx219->hflip->val |
721 				       imx219->vflip->val << 1);
722 		break;
723 	case V4L2_CID_VBLANK:
724 		ret = imx219_write_reg(imx219, IMX219_REG_VTS,
725 				       IMX219_REG_VALUE_16BIT,
726 				       imx219->mode->height + ctrl->val);
727 		break;
728 	case V4L2_CID_TEST_PATTERN_RED:
729 		ret = imx219_write_reg(imx219, IMX219_REG_TESTP_RED,
730 				       IMX219_REG_VALUE_16BIT, ctrl->val);
731 		break;
732 	case V4L2_CID_TEST_PATTERN_GREENR:
733 		ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENR,
734 				       IMX219_REG_VALUE_16BIT, ctrl->val);
735 		break;
736 	case V4L2_CID_TEST_PATTERN_BLUE:
737 		ret = imx219_write_reg(imx219, IMX219_REG_TESTP_BLUE,
738 				       IMX219_REG_VALUE_16BIT, ctrl->val);
739 		break;
740 	case V4L2_CID_TEST_PATTERN_GREENB:
741 		ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENB,
742 				       IMX219_REG_VALUE_16BIT, ctrl->val);
743 		break;
744 	default:
745 		dev_info(&client->dev,
746 			 "ctrl(id:0x%x,val:0x%x) is not handled\n",
747 			 ctrl->id, ctrl->val);
748 		ret = -EINVAL;
749 		break;
750 	}
751 
752 	pm_runtime_put(&client->dev);
753 
754 	return ret;
755 }
756 
757 static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
758 	.s_ctrl = imx219_set_ctrl,
759 };
760 
761 static int imx219_enum_mbus_code(struct v4l2_subdev *sd,
762 				 struct v4l2_subdev_pad_config *cfg,
763 				 struct v4l2_subdev_mbus_code_enum *code)
764 {
765 	struct imx219 *imx219 = to_imx219(sd);
766 
767 	if (code->index >= (ARRAY_SIZE(codes) / 4))
768 		return -EINVAL;
769 
770 	code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
771 
772 	return 0;
773 }
774 
775 static int imx219_enum_frame_size(struct v4l2_subdev *sd,
776 				  struct v4l2_subdev_pad_config *cfg,
777 				  struct v4l2_subdev_frame_size_enum *fse)
778 {
779 	struct imx219 *imx219 = to_imx219(sd);
780 
781 	if (fse->index >= ARRAY_SIZE(supported_modes))
782 		return -EINVAL;
783 
784 	if (fse->code != imx219_get_format_code(imx219, imx219->fmt.code))
785 		return -EINVAL;
786 
787 	fse->min_width = supported_modes[fse->index].width;
788 	fse->max_width = fse->min_width;
789 	fse->min_height = supported_modes[fse->index].height;
790 	fse->max_height = fse->min_height;
791 
792 	return 0;
793 }
794 
795 static void imx219_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
796 {
797 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
798 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
799 	fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
800 							  fmt->colorspace,
801 							  fmt->ycbcr_enc);
802 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
803 }
804 
805 static void imx219_update_pad_format(struct imx219 *imx219,
806 				     const struct imx219_mode *mode,
807 				     struct v4l2_subdev_format *fmt)
808 {
809 	fmt->format.width = mode->width;
810 	fmt->format.height = mode->height;
811 	fmt->format.field = V4L2_FIELD_NONE;
812 	imx219_reset_colorspace(&fmt->format);
813 }
814 
815 static int __imx219_get_pad_format(struct imx219 *imx219,
816 				   struct v4l2_subdev_pad_config *cfg,
817 				   struct v4l2_subdev_format *fmt)
818 {
819 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
820 		struct v4l2_mbus_framefmt *try_fmt =
821 			v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad);
822 		/* update the code which could change due to vflip or hflip: */
823 		try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
824 		fmt->format = *try_fmt;
825 	} else {
826 		imx219_update_pad_format(imx219, imx219->mode, fmt);
827 		fmt->format.code = imx219_get_format_code(imx219,
828 							  imx219->fmt.code);
829 	}
830 
831 	return 0;
832 }
833 
834 static int imx219_get_pad_format(struct v4l2_subdev *sd,
835 				 struct v4l2_subdev_pad_config *cfg,
836 				 struct v4l2_subdev_format *fmt)
837 {
838 	struct imx219 *imx219 = to_imx219(sd);
839 	int ret;
840 
841 	mutex_lock(&imx219->mutex);
842 	ret = __imx219_get_pad_format(imx219, cfg, fmt);
843 	mutex_unlock(&imx219->mutex);
844 
845 	return ret;
846 }
847 
848 static int imx219_set_pad_format(struct v4l2_subdev *sd,
849 				 struct v4l2_subdev_pad_config *cfg,
850 				 struct v4l2_subdev_format *fmt)
851 {
852 	struct imx219 *imx219 = to_imx219(sd);
853 	const struct imx219_mode *mode;
854 	struct v4l2_mbus_framefmt *framefmt;
855 	int exposure_max, exposure_def, hblank;
856 	unsigned int i;
857 
858 	mutex_lock(&imx219->mutex);
859 
860 	for (i = 0; i < ARRAY_SIZE(codes); i++)
861 		if (codes[i] == fmt->format.code)
862 			break;
863 	if (i >= ARRAY_SIZE(codes))
864 		i = 0;
865 
866 	/* Bayer order varies with flips */
867 	fmt->format.code = imx219_get_format_code(imx219, codes[i]);
868 
869 	mode = v4l2_find_nearest_size(supported_modes,
870 				      ARRAY_SIZE(supported_modes),
871 				      width, height,
872 				      fmt->format.width, fmt->format.height);
873 	imx219_update_pad_format(imx219, mode, fmt);
874 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
875 		framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
876 		*framefmt = fmt->format;
877 	} else if (imx219->mode != mode ||
878 		   imx219->fmt.code != fmt->format.code) {
879 		imx219->fmt = fmt->format;
880 		imx219->mode = mode;
881 		/* Update limits and set FPS to default */
882 		__v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
883 					 IMX219_VTS_MAX - mode->height, 1,
884 					 mode->vts_def - mode->height);
885 		__v4l2_ctrl_s_ctrl(imx219->vblank,
886 				   mode->vts_def - mode->height);
887 		/* Update max exposure while meeting expected vblanking */
888 		exposure_max = mode->vts_def - 4;
889 		exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
890 			exposure_max : IMX219_EXPOSURE_DEFAULT;
891 		__v4l2_ctrl_modify_range(imx219->exposure,
892 					 imx219->exposure->minimum,
893 					 exposure_max, imx219->exposure->step,
894 					 exposure_def);
895 		/*
896 		 * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
897 		 * depends on mode->width only, and is not changeble in any
898 		 * way other than changing the mode.
899 		 */
900 		hblank = IMX219_PPL_DEFAULT - mode->width;
901 		__v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
902 					 hblank);
903 	}
904 
905 	mutex_unlock(&imx219->mutex);
906 
907 	return 0;
908 }
909 
910 static int imx219_set_framefmt(struct imx219 *imx219)
911 {
912 	switch (imx219->fmt.code) {
913 	case MEDIA_BUS_FMT_SRGGB8_1X8:
914 	case MEDIA_BUS_FMT_SGRBG8_1X8:
915 	case MEDIA_BUS_FMT_SGBRG8_1X8:
916 	case MEDIA_BUS_FMT_SBGGR8_1X8:
917 		return imx219_write_regs(imx219, raw8_framefmt_regs,
918 					ARRAY_SIZE(raw8_framefmt_regs));
919 
920 	case MEDIA_BUS_FMT_SRGGB10_1X10:
921 	case MEDIA_BUS_FMT_SGRBG10_1X10:
922 	case MEDIA_BUS_FMT_SGBRG10_1X10:
923 	case MEDIA_BUS_FMT_SBGGR10_1X10:
924 		return imx219_write_regs(imx219, raw10_framefmt_regs,
925 					ARRAY_SIZE(raw10_framefmt_regs));
926 	}
927 
928 	return -EINVAL;
929 }
930 
931 static int imx219_start_streaming(struct imx219 *imx219)
932 {
933 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
934 	const struct imx219_reg_list *reg_list;
935 	int ret;
936 
937 	/* Apply default values of current mode */
938 	reg_list = &imx219->mode->reg_list;
939 	ret = imx219_write_regs(imx219, reg_list->regs, reg_list->num_of_regs);
940 	if (ret) {
941 		dev_err(&client->dev, "%s failed to set mode\n", __func__);
942 		return ret;
943 	}
944 
945 	ret = imx219_set_framefmt(imx219);
946 	if (ret) {
947 		dev_err(&client->dev, "%s failed to set frame format: %d\n",
948 			__func__, ret);
949 		return ret;
950 	}
951 
952 	/* Apply customized values from user */
953 	ret =  __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler);
954 	if (ret)
955 		return ret;
956 
957 	/* set stream on register */
958 	return imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
959 				IMX219_REG_VALUE_08BIT, IMX219_MODE_STREAMING);
960 }
961 
962 static void imx219_stop_streaming(struct imx219 *imx219)
963 {
964 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
965 	int ret;
966 
967 	/* set stream off register */
968 	ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
969 			       IMX219_REG_VALUE_08BIT, IMX219_MODE_STANDBY);
970 	if (ret)
971 		dev_err(&client->dev, "%s failed to set stream\n", __func__);
972 }
973 
974 static int imx219_set_stream(struct v4l2_subdev *sd, int enable)
975 {
976 	struct imx219 *imx219 = to_imx219(sd);
977 	struct i2c_client *client = v4l2_get_subdevdata(sd);
978 	int ret = 0;
979 
980 	mutex_lock(&imx219->mutex);
981 	if (imx219->streaming == enable) {
982 		mutex_unlock(&imx219->mutex);
983 		return 0;
984 	}
985 
986 	if (enable) {
987 		ret = pm_runtime_get_sync(&client->dev);
988 		if (ret < 0) {
989 			pm_runtime_put_noidle(&client->dev);
990 			goto err_unlock;
991 		}
992 
993 		/*
994 		 * Apply default & customized values
995 		 * and then start streaming.
996 		 */
997 		ret = imx219_start_streaming(imx219);
998 		if (ret)
999 			goto err_rpm_put;
1000 	} else {
1001 		imx219_stop_streaming(imx219);
1002 		pm_runtime_put(&client->dev);
1003 	}
1004 
1005 	imx219->streaming = enable;
1006 
1007 	/* vflip and hflip cannot change during streaming */
1008 	__v4l2_ctrl_grab(imx219->vflip, enable);
1009 	__v4l2_ctrl_grab(imx219->hflip, enable);
1010 
1011 	mutex_unlock(&imx219->mutex);
1012 
1013 	return ret;
1014 
1015 err_rpm_put:
1016 	pm_runtime_put(&client->dev);
1017 err_unlock:
1018 	mutex_unlock(&imx219->mutex);
1019 
1020 	return ret;
1021 }
1022 
1023 /* Power/clock management functions */
1024 static int imx219_power_on(struct device *dev)
1025 {
1026 	struct i2c_client *client = to_i2c_client(dev);
1027 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1028 	struct imx219 *imx219 = to_imx219(sd);
1029 	int ret;
1030 
1031 	ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
1032 				    imx219->supplies);
1033 	if (ret) {
1034 		dev_err(&client->dev, "%s: failed to enable regulators\n",
1035 			__func__);
1036 		return ret;
1037 	}
1038 
1039 	ret = clk_prepare_enable(imx219->xclk);
1040 	if (ret) {
1041 		dev_err(&client->dev, "%s: failed to enable clock\n",
1042 			__func__);
1043 		goto reg_off;
1044 	}
1045 
1046 	gpiod_set_value_cansleep(imx219->reset_gpio, 1);
1047 	usleep_range(IMX219_XCLR_MIN_DELAY_US,
1048 		     IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US);
1049 
1050 	return 0;
1051 
1052 reg_off:
1053 	regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1054 
1055 	return ret;
1056 }
1057 
1058 static int imx219_power_off(struct device *dev)
1059 {
1060 	struct i2c_client *client = to_i2c_client(dev);
1061 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1062 	struct imx219 *imx219 = to_imx219(sd);
1063 
1064 	gpiod_set_value_cansleep(imx219->reset_gpio, 0);
1065 	regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1066 	clk_disable_unprepare(imx219->xclk);
1067 
1068 	return 0;
1069 }
1070 
1071 static int __maybe_unused imx219_suspend(struct device *dev)
1072 {
1073 	struct i2c_client *client = to_i2c_client(dev);
1074 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1075 	struct imx219 *imx219 = to_imx219(sd);
1076 
1077 	if (imx219->streaming)
1078 		imx219_stop_streaming(imx219);
1079 
1080 	return 0;
1081 }
1082 
1083 static int __maybe_unused imx219_resume(struct device *dev)
1084 {
1085 	struct i2c_client *client = to_i2c_client(dev);
1086 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1087 	struct imx219 *imx219 = to_imx219(sd);
1088 	int ret;
1089 
1090 	if (imx219->streaming) {
1091 		ret = imx219_start_streaming(imx219);
1092 		if (ret)
1093 			goto error;
1094 	}
1095 
1096 	return 0;
1097 
1098 error:
1099 	imx219_stop_streaming(imx219);
1100 	imx219->streaming = 0;
1101 
1102 	return ret;
1103 }
1104 
1105 static int imx219_get_regulators(struct imx219 *imx219)
1106 {
1107 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1108 	unsigned int i;
1109 
1110 	for (i = 0; i < IMX219_NUM_SUPPLIES; i++)
1111 		imx219->supplies[i].supply = imx219_supply_name[i];
1112 
1113 	return devm_regulator_bulk_get(&client->dev,
1114 				       IMX219_NUM_SUPPLIES,
1115 				       imx219->supplies);
1116 }
1117 
1118 /* Verify chip ID */
1119 static int imx219_identify_module(struct imx219 *imx219)
1120 {
1121 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1122 	int ret;
1123 	u32 val;
1124 
1125 	ret = imx219_read_reg(imx219, IMX219_REG_CHIP_ID,
1126 			      IMX219_REG_VALUE_16BIT, &val);
1127 	if (ret) {
1128 		dev_err(&client->dev, "failed to read chip id %x\n",
1129 			IMX219_CHIP_ID);
1130 		return ret;
1131 	}
1132 
1133 	if (val != IMX219_CHIP_ID) {
1134 		dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1135 			IMX219_CHIP_ID, val);
1136 		return -EIO;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 static const struct v4l2_subdev_core_ops imx219_core_ops = {
1143 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1144 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
1145 };
1146 
1147 static const struct v4l2_subdev_video_ops imx219_video_ops = {
1148 	.s_stream = imx219_set_stream,
1149 };
1150 
1151 static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
1152 	.enum_mbus_code = imx219_enum_mbus_code,
1153 	.get_fmt = imx219_get_pad_format,
1154 	.set_fmt = imx219_set_pad_format,
1155 	.enum_frame_size = imx219_enum_frame_size,
1156 };
1157 
1158 static const struct v4l2_subdev_ops imx219_subdev_ops = {
1159 	.core = &imx219_core_ops,
1160 	.video = &imx219_video_ops,
1161 	.pad = &imx219_pad_ops,
1162 };
1163 
1164 static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
1165 	.open = imx219_open,
1166 };
1167 
1168 /* Initialize control handlers */
1169 static int imx219_init_controls(struct imx219 *imx219)
1170 {
1171 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1172 	struct v4l2_ctrl_handler *ctrl_hdlr;
1173 	unsigned int height = imx219->mode->height;
1174 	int exposure_max, exposure_def, hblank;
1175 	int i, ret;
1176 
1177 	ctrl_hdlr = &imx219->ctrl_handler;
1178 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 9);
1179 	if (ret)
1180 		return ret;
1181 
1182 	mutex_init(&imx219->mutex);
1183 	ctrl_hdlr->lock = &imx219->mutex;
1184 
1185 	/* By default, PIXEL_RATE is read only */
1186 	imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1187 					       V4L2_CID_PIXEL_RATE,
1188 					       IMX219_PIXEL_RATE,
1189 					       IMX219_PIXEL_RATE, 1,
1190 					       IMX219_PIXEL_RATE);
1191 
1192 	/* Initial vblank/hblank/exposure parameters based on current mode */
1193 	imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1194 					   V4L2_CID_VBLANK, IMX219_VBLANK_MIN,
1195 					   IMX219_VTS_MAX - height, 1,
1196 					   imx219->mode->vts_def - height);
1197 	hblank = IMX219_PPL_DEFAULT - imx219->mode->width;
1198 	imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1199 					   V4L2_CID_HBLANK, hblank, hblank,
1200 					   1, hblank);
1201 	if (imx219->hblank)
1202 		imx219->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1203 	exposure_max = imx219->mode->vts_def - 4;
1204 	exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
1205 		exposure_max : IMX219_EXPOSURE_DEFAULT;
1206 	imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1207 					     V4L2_CID_EXPOSURE,
1208 					     IMX219_EXPOSURE_MIN, exposure_max,
1209 					     IMX219_EXPOSURE_STEP,
1210 					     exposure_def);
1211 
1212 	v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1213 			  IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX,
1214 			  IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT);
1215 
1216 	v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1217 			  IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX,
1218 			  IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT);
1219 
1220 	imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1221 					  V4L2_CID_HFLIP, 0, 1, 1, 0);
1222 	if (imx219->hflip)
1223 		imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1224 
1225 	imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1226 					  V4L2_CID_VFLIP, 0, 1, 1, 0);
1227 	if (imx219->vflip)
1228 		imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1229 
1230 	v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops,
1231 				     V4L2_CID_TEST_PATTERN,
1232 				     ARRAY_SIZE(imx219_test_pattern_menu) - 1,
1233 				     0, 0, imx219_test_pattern_menu);
1234 	for (i = 0; i < 4; i++) {
1235 		/*
1236 		 * The assumption is that
1237 		 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
1238 		 * V4L2_CID_TEST_PATTERN_BLUE   == V4L2_CID_TEST_PATTERN_RED + 2
1239 		 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
1240 		 */
1241 		v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1242 				  V4L2_CID_TEST_PATTERN_RED + i,
1243 				  IMX219_TESTP_COLOUR_MIN,
1244 				  IMX219_TESTP_COLOUR_MAX,
1245 				  IMX219_TESTP_COLOUR_STEP,
1246 				  IMX219_TESTP_COLOUR_MAX);
1247 		/* The "Solid color" pattern is white by default */
1248 	}
1249 
1250 	if (ctrl_hdlr->error) {
1251 		ret = ctrl_hdlr->error;
1252 		dev_err(&client->dev, "%s control init failed (%d)\n",
1253 			__func__, ret);
1254 		goto error;
1255 	}
1256 
1257 	imx219->sd.ctrl_handler = ctrl_hdlr;
1258 
1259 	return 0;
1260 
1261 error:
1262 	v4l2_ctrl_handler_free(ctrl_hdlr);
1263 	mutex_destroy(&imx219->mutex);
1264 
1265 	return ret;
1266 }
1267 
1268 static void imx219_free_controls(struct imx219 *imx219)
1269 {
1270 	v4l2_ctrl_handler_free(imx219->sd.ctrl_handler);
1271 	mutex_destroy(&imx219->mutex);
1272 }
1273 
1274 static int imx219_check_hwcfg(struct device *dev)
1275 {
1276 	struct fwnode_handle *endpoint;
1277 	struct v4l2_fwnode_endpoint ep_cfg = {
1278 		.bus_type = V4L2_MBUS_CSI2_DPHY
1279 	};
1280 	int ret = -EINVAL;
1281 
1282 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1283 	if (!endpoint) {
1284 		dev_err(dev, "endpoint node not found\n");
1285 		return -EINVAL;
1286 	}
1287 
1288 	if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1289 		dev_err(dev, "could not parse endpoint\n");
1290 		goto error_out;
1291 	}
1292 
1293 	/* Check the number of MIPI CSI2 data lanes */
1294 	if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1295 		dev_err(dev, "only 2 data lanes are currently supported\n");
1296 		goto error_out;
1297 	}
1298 
1299 	/* Check the link frequency set in device tree */
1300 	if (!ep_cfg.nr_of_link_frequencies) {
1301 		dev_err(dev, "link-frequency property not found in DT\n");
1302 		goto error_out;
1303 	}
1304 
1305 	if (ep_cfg.nr_of_link_frequencies != 1 ||
1306 	    ep_cfg.link_frequencies[0] != IMX219_DEFAULT_LINK_FREQ) {
1307 		dev_err(dev, "Link frequency not supported: %lld\n",
1308 			ep_cfg.link_frequencies[0]);
1309 		goto error_out;
1310 	}
1311 
1312 	ret = 0;
1313 
1314 error_out:
1315 	v4l2_fwnode_endpoint_free(&ep_cfg);
1316 	fwnode_handle_put(endpoint);
1317 
1318 	return ret;
1319 }
1320 
1321 static int imx219_probe(struct i2c_client *client)
1322 {
1323 	struct device *dev = &client->dev;
1324 	struct imx219 *imx219;
1325 	int ret;
1326 
1327 	imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL);
1328 	if (!imx219)
1329 		return -ENOMEM;
1330 
1331 	v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops);
1332 
1333 	/* Check the hardware configuration in device tree */
1334 	if (imx219_check_hwcfg(dev))
1335 		return -EINVAL;
1336 
1337 	/* Get system clock (xclk) */
1338 	imx219->xclk = devm_clk_get(dev, NULL);
1339 	if (IS_ERR(imx219->xclk)) {
1340 		dev_err(dev, "failed to get xclk\n");
1341 		return PTR_ERR(imx219->xclk);
1342 	}
1343 
1344 	imx219->xclk_freq = clk_get_rate(imx219->xclk);
1345 	if (imx219->xclk_freq != IMX219_XCLK_FREQ) {
1346 		dev_err(dev, "xclk frequency not supported: %d Hz\n",
1347 			imx219->xclk_freq);
1348 		return -EINVAL;
1349 	}
1350 
1351 	ret = imx219_get_regulators(imx219);
1352 	if (ret) {
1353 		dev_err(dev, "failed to get regulators\n");
1354 		return ret;
1355 	}
1356 
1357 	/* Request optional enable pin */
1358 	imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1359 						     GPIOD_OUT_HIGH);
1360 
1361 	/*
1362 	 * The sensor must be powered for imx219_identify_module()
1363 	 * to be able to read the CHIP_ID register
1364 	 */
1365 	ret = imx219_power_on(dev);
1366 	if (ret)
1367 		return ret;
1368 
1369 	ret = imx219_identify_module(imx219);
1370 	if (ret)
1371 		goto error_power_off;
1372 
1373 	/* Set default mode to max resolution */
1374 	imx219->mode = &supported_modes[0];
1375 
1376 	/* sensor doesn't enter LP-11 state upon power up until and unless
1377 	 * streaming is started, so upon power up switch the modes to:
1378 	 * streaming -> standby
1379 	 */
1380 	ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1381 			       IMX219_REG_VALUE_08BIT, IMX219_MODE_STREAMING);
1382 	if (ret < 0)
1383 		goto error_power_off;
1384 	usleep_range(100, 110);
1385 
1386 	/* put sensor back to standby mode */
1387 	ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1388 			       IMX219_REG_VALUE_08BIT, IMX219_MODE_STANDBY);
1389 	if (ret < 0)
1390 		goto error_power_off;
1391 	usleep_range(100, 110);
1392 
1393 	ret = imx219_init_controls(imx219);
1394 	if (ret)
1395 		goto error_power_off;
1396 
1397 	/* Initialize subdev */
1398 	imx219->sd.internal_ops = &imx219_internal_ops;
1399 	imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1400 	imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1401 
1402 	/* Initialize source pad */
1403 	imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
1404 
1405 	/* Initialize default format */
1406 	imx219_set_default_format(imx219);
1407 
1408 	ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
1409 	if (ret) {
1410 		dev_err(dev, "failed to init entity pads: %d\n", ret);
1411 		goto error_handler_free;
1412 	}
1413 
1414 	ret = v4l2_async_register_subdev_sensor_common(&imx219->sd);
1415 	if (ret < 0) {
1416 		dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
1417 		goto error_media_entity;
1418 	}
1419 
1420 	/* Enable runtime PM and turn off the device */
1421 	pm_runtime_set_active(dev);
1422 	pm_runtime_enable(dev);
1423 	pm_runtime_idle(dev);
1424 
1425 	return 0;
1426 
1427 error_media_entity:
1428 	media_entity_cleanup(&imx219->sd.entity);
1429 
1430 error_handler_free:
1431 	imx219_free_controls(imx219);
1432 
1433 error_power_off:
1434 	imx219_power_off(dev);
1435 
1436 	return ret;
1437 }
1438 
1439 static int imx219_remove(struct i2c_client *client)
1440 {
1441 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1442 	struct imx219 *imx219 = to_imx219(sd);
1443 
1444 	v4l2_async_unregister_subdev(sd);
1445 	media_entity_cleanup(&sd->entity);
1446 	imx219_free_controls(imx219);
1447 
1448 	pm_runtime_disable(&client->dev);
1449 	if (!pm_runtime_status_suspended(&client->dev))
1450 		imx219_power_off(&client->dev);
1451 	pm_runtime_set_suspended(&client->dev);
1452 
1453 	return 0;
1454 }
1455 
1456 static const struct of_device_id imx219_dt_ids[] = {
1457 	{ .compatible = "sony,imx219" },
1458 	{ /* sentinel */ }
1459 };
1460 MODULE_DEVICE_TABLE(of, imx219_dt_ids);
1461 
1462 static const struct dev_pm_ops imx219_pm_ops = {
1463 	SET_SYSTEM_SLEEP_PM_OPS(imx219_suspend, imx219_resume)
1464 	SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL)
1465 };
1466 
1467 static struct i2c_driver imx219_i2c_driver = {
1468 	.driver = {
1469 		.name = "imx219",
1470 		.of_match_table	= imx219_dt_ids,
1471 		.pm = &imx219_pm_ops,
1472 	},
1473 	.probe_new = imx219_probe,
1474 	.remove = imx219_remove,
1475 };
1476 
1477 module_i2c_driver(imx219_i2c_driver);
1478 
1479 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1480 MODULE_DESCRIPTION("Sony IMX219 sensor driver");
1481 MODULE_LICENSE("GPL v2");
1482