xref: /openbmc/linux/drivers/media/i2c/imx274.c (revision adf57b86)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * imx274.c - IMX274 CMOS Image Sensor driver
4  *
5  * Copyright (C) 2017, Leopard Imaging, Inc.
6  *
7  * Leon Luo <leonl@leopardimaging.com>
8  * Edwin Zou <edwinz@leopardimaging.com>
9  * Luca Ceresoli <luca@lucaceresoli.net>
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/v4l2-mediabus.h>
26 #include <linux/videodev2.h>
27 
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-subdev.h>
31 
32 /*
33  * See "SHR, SVR Setting" in datasheet
34  */
35 #define IMX274_DEFAULT_FRAME_LENGTH		(4550)
36 #define IMX274_MAX_FRAME_LENGTH			(0x000fffff)
37 
38 /*
39  * See "Frame Rate Adjustment" in datasheet
40  */
41 #define IMX274_PIXCLK_CONST1			(72000000)
42 #define IMX274_PIXCLK_CONST2			(1000000)
43 
44 /*
45  * The input gain is shifted by IMX274_GAIN_SHIFT to get
46  * decimal number. The real gain is
47  * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
48  */
49 #define IMX274_GAIN_SHIFT			(8)
50 #define IMX274_GAIN_SHIFT_MASK			((1 << IMX274_GAIN_SHIFT) - 1)
51 
52 /*
53  * See "Analog Gain" and "Digital Gain" in datasheet
54  * min gain is 1X
55  * max gain is calculated based on IMX274_GAIN_REG_MAX
56  */
57 #define IMX274_GAIN_REG_MAX			(1957)
58 #define IMX274_MIN_GAIN				(0x01 << IMX274_GAIN_SHIFT)
59 #define IMX274_MAX_ANALOG_GAIN			((2048 << IMX274_GAIN_SHIFT)\
60 					/ (2048 - IMX274_GAIN_REG_MAX))
61 #define IMX274_MAX_DIGITAL_GAIN			(8)
62 #define IMX274_DEF_GAIN				(20 << IMX274_GAIN_SHIFT)
63 #define IMX274_GAIN_CONST			(2048) /* for gain formula */
64 
65 /*
66  * 1 line time in us = (HMAX / 72), minimal is 4 lines
67  */
68 #define IMX274_MIN_EXPOSURE_TIME		(4 * 260 / 72)
69 
70 #define IMX274_MAX_WIDTH			(3840)
71 #define IMX274_MAX_HEIGHT			(2160)
72 #define IMX274_MAX_FRAME_RATE			(120)
73 #define IMX274_MIN_FRAME_RATE			(5)
74 #define IMX274_DEF_FRAME_RATE			(60)
75 
76 /*
77  * register SHR is limited to (SVR value + 1) x VMAX value - 4
78  */
79 #define IMX274_SHR_LIMIT_CONST			(4)
80 
81 /*
82  * Min and max sensor reset delay (microseconds)
83  */
84 #define IMX274_RESET_DELAY1			(2000)
85 #define IMX274_RESET_DELAY2			(2200)
86 
87 /*
88  * shift and mask constants
89  */
90 #define IMX274_SHIFT_8_BITS			(8)
91 #define IMX274_SHIFT_16_BITS			(16)
92 #define IMX274_MASK_LSB_2_BITS			(0x03)
93 #define IMX274_MASK_LSB_3_BITS			(0x07)
94 #define IMX274_MASK_LSB_4_BITS			(0x0f)
95 #define IMX274_MASK_LSB_8_BITS			(0x00ff)
96 
97 #define DRIVER_NAME "IMX274"
98 
99 /*
100  * IMX274 register definitions
101  */
102 #define IMX274_SHR_REG_MSB			0x300D /* SHR */
103 #define IMX274_SHR_REG_LSB			0x300C /* SHR */
104 #define IMX274_SVR_REG_MSB			0x300F /* SVR */
105 #define IMX274_SVR_REG_LSB			0x300E /* SVR */
106 #define IMX274_HTRIM_EN_REG			0x3037
107 #define IMX274_HTRIM_START_REG_LSB		0x3038
108 #define IMX274_HTRIM_START_REG_MSB		0x3039
109 #define IMX274_HTRIM_END_REG_LSB		0x303A
110 #define IMX274_HTRIM_END_REG_MSB		0x303B
111 #define IMX274_VWIDCUTEN_REG			0x30DD
112 #define IMX274_VWIDCUT_REG_LSB			0x30DE
113 #define IMX274_VWIDCUT_REG_MSB			0x30DF
114 #define IMX274_VWINPOS_REG_LSB			0x30E0
115 #define IMX274_VWINPOS_REG_MSB			0x30E1
116 #define IMX274_WRITE_VSIZE_REG_LSB		0x3130
117 #define IMX274_WRITE_VSIZE_REG_MSB		0x3131
118 #define IMX274_Y_OUT_SIZE_REG_LSB		0x3132
119 #define IMX274_Y_OUT_SIZE_REG_MSB		0x3133
120 #define IMX274_VMAX_REG_1			0x30FA /* VMAX, MSB */
121 #define IMX274_VMAX_REG_2			0x30F9 /* VMAX */
122 #define IMX274_VMAX_REG_3			0x30F8 /* VMAX, LSB */
123 #define IMX274_HMAX_REG_MSB			0x30F7 /* HMAX */
124 #define IMX274_HMAX_REG_LSB			0x30F6 /* HMAX */
125 #define IMX274_ANALOG_GAIN_ADDR_LSB		0x300A /* ANALOG GAIN LSB */
126 #define IMX274_ANALOG_GAIN_ADDR_MSB		0x300B /* ANALOG GAIN MSB */
127 #define IMX274_DIGITAL_GAIN_REG			0x3012 /* Digital Gain */
128 #define IMX274_VFLIP_REG			0x301A /* VERTICAL FLIP */
129 #define IMX274_TEST_PATTERN_REG			0x303D /* TEST PATTERN */
130 #define IMX274_STANDBY_REG			0x3000 /* STANDBY */
131 
132 #define IMX274_TABLE_WAIT_MS			0
133 #define IMX274_TABLE_END			1
134 
135 /* regulator supplies */
136 static const char * const imx274_supply_names[] = {
137 	"vddl",  /* IF (1.2V) supply */
138 	"vdig",  /* Digital Core (1.8V) supply */
139 	"vana",  /* Analog (2.8V) supply */
140 };
141 
142 #define IMX274_NUM_SUPPLIES ARRAY_SIZE(imx274_supply_names)
143 
144 /*
145  * imx274 I2C operation related structure
146  */
147 struct reg_8 {
148 	u16 addr;
149 	u8 val;
150 };
151 
152 static const struct regmap_config imx274_regmap_config = {
153 	.reg_bits = 16,
154 	.val_bits = 8,
155 	.cache_type = REGCACHE_RBTREE,
156 };
157 
158 /*
159  * Parameters for each imx274 readout mode.
160  *
161  * These are the values to configure the sensor in one of the
162  * implemented modes.
163  *
164  * @init_regs: registers to initialize the mode
165  * @wbin_ratio: width downscale factor (e.g. 3 for 1280; 3 = 3840/1280)
166  * @hbin_ratio: height downscale factor (e.g. 3 for 720; 3 = 2160/720)
167  * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
168  *                 Adjustment (CSI-2)" in the datasheet)
169  * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
170  *           datasheet)
171  * @max_fps: Maximum frames per second
172  * @nocpiop: Number of clocks per internal offset period (see "Integration Time
173  *           in Each Readout Drive Mode (CSI-2)" in the datasheet)
174  */
175 struct imx274_mode {
176 	const struct reg_8 *init_regs;
177 	u8 wbin_ratio;
178 	u8 hbin_ratio;
179 	int min_frame_len;
180 	int min_SHR;
181 	int max_fps;
182 	int nocpiop;
183 };
184 
185 /*
186  * imx274 test pattern related structure
187  */
188 enum {
189 	TEST_PATTERN_DISABLED = 0,
190 	TEST_PATTERN_ALL_000H,
191 	TEST_PATTERN_ALL_FFFH,
192 	TEST_PATTERN_ALL_555H,
193 	TEST_PATTERN_ALL_AAAH,
194 	TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
195 	TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
196 	TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
197 	TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
198 	TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
199 	TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
200 	TEST_PATTERN_H_COLOR_BARS,
201 	TEST_PATTERN_V_COLOR_BARS,
202 };
203 
204 static const char * const tp_qmenu[] = {
205 	"Disabled",
206 	"All 000h Pattern",
207 	"All FFFh Pattern",
208 	"All 555h Pattern",
209 	"All AAAh Pattern",
210 	"Vertical Stripe (555h / AAAh)",
211 	"Vertical Stripe (AAAh / 555h)",
212 	"Vertical Stripe (000h / 555h)",
213 	"Vertical Stripe (555h / 000h)",
214 	"Vertical Stripe (000h / FFFh)",
215 	"Vertical Stripe (FFFh / 000h)",
216 	"Vertical Color Bars",
217 	"Horizontal Color Bars",
218 };
219 
220 /*
221  * All-pixel scan mode (10-bit)
222  * imx274 mode1(refer to datasheet) register configuration with
223  * 3840x2160 resolution, raw10 data and mipi four lane output
224  */
225 static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
226 	{0x3004, 0x01},
227 	{0x3005, 0x01},
228 	{0x3006, 0x00},
229 	{0x3007, 0xa2},
230 
231 	{0x3018, 0xA2}, /* output XVS, HVS */
232 
233 	{0x306B, 0x05},
234 	{0x30E2, 0x01},
235 
236 	{0x30EE, 0x01},
237 	{0x3342, 0x0A},
238 	{0x3343, 0x00},
239 	{0x3344, 0x16},
240 	{0x3345, 0x00},
241 	{0x33A6, 0x01},
242 	{0x3528, 0x0E},
243 	{0x3554, 0x1F},
244 	{0x3555, 0x01},
245 	{0x3556, 0x01},
246 	{0x3557, 0x01},
247 	{0x3558, 0x01},
248 	{0x3559, 0x00},
249 	{0x355A, 0x00},
250 	{0x35BA, 0x0E},
251 	{0x366A, 0x1B},
252 	{0x366B, 0x1A},
253 	{0x366C, 0x19},
254 	{0x366D, 0x17},
255 	{0x3A41, 0x08},
256 
257 	{IMX274_TABLE_END, 0x00}
258 };
259 
260 /*
261  * Horizontal/vertical 2/2-line binning
262  * (Horizontal and vertical weightedbinning, 10-bit)
263  * imx274 mode3(refer to datasheet) register configuration with
264  * 1920x1080 resolution, raw10 data and mipi four lane output
265  */
266 static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
267 	{0x3004, 0x02},
268 	{0x3005, 0x21},
269 	{0x3006, 0x00},
270 	{0x3007, 0xb1},
271 
272 	{0x3018, 0xA2}, /* output XVS, HVS */
273 
274 	{0x306B, 0x05},
275 	{0x30E2, 0x02},
276 
277 	{0x30EE, 0x01},
278 	{0x3342, 0x0A},
279 	{0x3343, 0x00},
280 	{0x3344, 0x1A},
281 	{0x3345, 0x00},
282 	{0x33A6, 0x01},
283 	{0x3528, 0x0E},
284 	{0x3554, 0x00},
285 	{0x3555, 0x01},
286 	{0x3556, 0x01},
287 	{0x3557, 0x01},
288 	{0x3558, 0x01},
289 	{0x3559, 0x00},
290 	{0x355A, 0x00},
291 	{0x35BA, 0x0E},
292 	{0x366A, 0x1B},
293 	{0x366B, 0x1A},
294 	{0x366C, 0x19},
295 	{0x366D, 0x17},
296 	{0x3A41, 0x08},
297 
298 	{IMX274_TABLE_END, 0x00}
299 };
300 
301 /*
302  * Vertical 2/3 subsampling binning horizontal 3 binning
303  * imx274 mode5(refer to datasheet) register configuration with
304  * 1280x720 resolution, raw10 data and mipi four lane output
305  */
306 static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
307 	{0x3004, 0x03},
308 	{0x3005, 0x31},
309 	{0x3006, 0x00},
310 	{0x3007, 0xa9},
311 
312 	{0x3018, 0xA2}, /* output XVS, HVS */
313 
314 	{0x306B, 0x05},
315 	{0x30E2, 0x03},
316 
317 	{0x30EE, 0x01},
318 	{0x3342, 0x0A},
319 	{0x3343, 0x00},
320 	{0x3344, 0x1B},
321 	{0x3345, 0x00},
322 	{0x33A6, 0x01},
323 	{0x3528, 0x0E},
324 	{0x3554, 0x00},
325 	{0x3555, 0x01},
326 	{0x3556, 0x01},
327 	{0x3557, 0x01},
328 	{0x3558, 0x01},
329 	{0x3559, 0x00},
330 	{0x355A, 0x00},
331 	{0x35BA, 0x0E},
332 	{0x366A, 0x1B},
333 	{0x366B, 0x19},
334 	{0x366C, 0x17},
335 	{0x366D, 0x17},
336 	{0x3A41, 0x04},
337 
338 	{IMX274_TABLE_END, 0x00}
339 };
340 
341 /*
342  * Vertical 2/8 subsampling horizontal 3 binning
343  * imx274 mode6(refer to datasheet) register configuration with
344  * 1280x540 resolution, raw10 data and mipi four lane output
345  */
346 static const struct reg_8 imx274_mode6_1280x540_raw10[] = {
347 	{0x3004, 0x04}, /* mode setting */
348 	{0x3005, 0x31},
349 	{0x3006, 0x00},
350 	{0x3007, 0x02}, /* mode setting */
351 
352 	{0x3018, 0xA2}, /* output XVS, HVS */
353 
354 	{0x306B, 0x05},
355 	{0x30E2, 0x04}, /* mode setting */
356 
357 	{0x30EE, 0x01},
358 	{0x3342, 0x0A},
359 	{0x3343, 0x00},
360 	{0x3344, 0x16},
361 	{0x3345, 0x00},
362 	{0x33A6, 0x01},
363 	{0x3528, 0x0E},
364 	{0x3554, 0x1F},
365 	{0x3555, 0x01},
366 	{0x3556, 0x01},
367 	{0x3557, 0x01},
368 	{0x3558, 0x01},
369 	{0x3559, 0x00},
370 	{0x355A, 0x00},
371 	{0x35BA, 0x0E},
372 	{0x366A, 0x1B},
373 	{0x366B, 0x1A},
374 	{0x366C, 0x19},
375 	{0x366D, 0x17},
376 	{0x3A41, 0x04},
377 
378 	{IMX274_TABLE_END, 0x00}
379 };
380 
381 /*
382  * imx274 first step register configuration for
383  * starting stream
384  */
385 static const struct reg_8 imx274_start_1[] = {
386 	{IMX274_STANDBY_REG, 0x12},
387 
388 	/* PLRD: clock settings */
389 	{0x3120, 0xF0},
390 	{0x3121, 0x00},
391 	{0x3122, 0x02},
392 	{0x3129, 0x9C},
393 	{0x312A, 0x02},
394 	{0x312D, 0x02},
395 
396 	{0x310B, 0x00},
397 
398 	/* PLSTMG */
399 	{0x304C, 0x00}, /* PLSTMG01 */
400 	{0x304D, 0x03},
401 	{0x331C, 0x1A},
402 	{0x331D, 0x00},
403 	{0x3502, 0x02},
404 	{0x3529, 0x0E},
405 	{0x352A, 0x0E},
406 	{0x352B, 0x0E},
407 	{0x3538, 0x0E},
408 	{0x3539, 0x0E},
409 	{0x3553, 0x00},
410 	{0x357D, 0x05},
411 	{0x357F, 0x05},
412 	{0x3581, 0x04},
413 	{0x3583, 0x76},
414 	{0x3587, 0x01},
415 	{0x35BB, 0x0E},
416 	{0x35BC, 0x0E},
417 	{0x35BD, 0x0E},
418 	{0x35BE, 0x0E},
419 	{0x35BF, 0x0E},
420 	{0x366E, 0x00},
421 	{0x366F, 0x00},
422 	{0x3670, 0x00},
423 	{0x3671, 0x00},
424 
425 	/* PSMIPI */
426 	{0x3304, 0x32}, /* PSMIPI1 */
427 	{0x3305, 0x00},
428 	{0x3306, 0x32},
429 	{0x3307, 0x00},
430 	{0x3590, 0x32},
431 	{0x3591, 0x00},
432 	{0x3686, 0x32},
433 	{0x3687, 0x00},
434 
435 	{IMX274_TABLE_END, 0x00}
436 };
437 
438 /*
439  * imx274 second step register configuration for
440  * starting stream
441  */
442 static const struct reg_8 imx274_start_2[] = {
443 	{IMX274_STANDBY_REG, 0x00},
444 	{0x303E, 0x02}, /* SYS_MODE = 2 */
445 	{IMX274_TABLE_END, 0x00}
446 };
447 
448 /*
449  * imx274 third step register configuration for
450  * starting stream
451  */
452 static const struct reg_8 imx274_start_3[] = {
453 	{0x30F4, 0x00},
454 	{0x3018, 0xA2}, /* XHS VHS OUTPUT */
455 	{IMX274_TABLE_END, 0x00}
456 };
457 
458 /*
459  * imx274 register configuration for stopping stream
460  */
461 static const struct reg_8 imx274_stop[] = {
462 	{IMX274_STANDBY_REG, 0x01},
463 	{IMX274_TABLE_END, 0x00}
464 };
465 
466 /*
467  * imx274 disable test pattern register configuration
468  */
469 static const struct reg_8 imx274_tp_disabled[] = {
470 	{0x303C, 0x00},
471 	{0x377F, 0x00},
472 	{0x3781, 0x00},
473 	{0x370B, 0x00},
474 	{IMX274_TABLE_END, 0x00}
475 };
476 
477 /*
478  * imx274 test pattern register configuration
479  * reg 0x303D defines the test pattern modes
480  */
481 static const struct reg_8 imx274_tp_regs[] = {
482 	{0x303C, 0x11},
483 	{0x370E, 0x01},
484 	{0x377F, 0x01},
485 	{0x3781, 0x01},
486 	{0x370B, 0x11},
487 	{IMX274_TABLE_END, 0x00}
488 };
489 
490 /* nocpiop happens to be the same number for the implemented modes */
491 static const struct imx274_mode imx274_modes[] = {
492 	{
493 		/* mode 1, 4K */
494 		.wbin_ratio = 1, /* 3840 */
495 		.hbin_ratio = 1, /* 2160 */
496 		.init_regs = imx274_mode1_3840x2160_raw10,
497 		.min_frame_len = 4550,
498 		.min_SHR = 12,
499 		.max_fps = 60,
500 		.nocpiop = 112,
501 	},
502 	{
503 		/* mode 3, 1080p */
504 		.wbin_ratio = 2, /* 1920 */
505 		.hbin_ratio = 2, /* 1080 */
506 		.init_regs = imx274_mode3_1920x1080_raw10,
507 		.min_frame_len = 2310,
508 		.min_SHR = 8,
509 		.max_fps = 120,
510 		.nocpiop = 112,
511 	},
512 	{
513 		/* mode 5, 720p */
514 		.wbin_ratio = 3, /* 1280 */
515 		.hbin_ratio = 3, /* 720 */
516 		.init_regs = imx274_mode5_1280x720_raw10,
517 		.min_frame_len = 2310,
518 		.min_SHR = 8,
519 		.max_fps = 120,
520 		.nocpiop = 112,
521 	},
522 	{
523 		/* mode 6, 540p */
524 		.wbin_ratio = 3, /* 1280 */
525 		.hbin_ratio = 4, /* 540 */
526 		.init_regs = imx274_mode6_1280x540_raw10,
527 		.min_frame_len = 2310,
528 		.min_SHR = 4,
529 		.max_fps = 120,
530 		.nocpiop = 112,
531 	},
532 };
533 
534 /*
535  * struct imx274_ctrls - imx274 ctrl structure
536  * @handler: V4L2 ctrl handler structure
537  * @exposure: Pointer to expsure ctrl structure
538  * @gain: Pointer to gain ctrl structure
539  * @vflip: Pointer to vflip ctrl structure
540  * @test_pattern: Pointer to test pattern ctrl structure
541  */
542 struct imx274_ctrls {
543 	struct v4l2_ctrl_handler handler;
544 	struct v4l2_ctrl *exposure;
545 	struct v4l2_ctrl *gain;
546 	struct v4l2_ctrl *vflip;
547 	struct v4l2_ctrl *test_pattern;
548 };
549 
550 /*
551  * struct stim274 - imx274 device structure
552  * @sd: V4L2 subdevice structure
553  * @pad: Media pad structure
554  * @client: Pointer to I2C client
555  * @ctrls: imx274 control structure
556  * @crop: rect to be captured
557  * @compose: compose rect, i.e. output resolution
558  * @format: V4L2 media bus frame format structure
559  *          (width and height are in sync with the compose rect)
560  * @frame_rate: V4L2 frame rate structure
561  * @regmap: Pointer to regmap structure
562  * @reset_gpio: Pointer to reset gpio
563  * @supplies: List of analog and digital supply regulators
564  * @inck: Pointer to sensor input clock
565  * @lock: Mutex structure
566  * @mode: Parameters for the selected readout mode
567  */
568 struct stimx274 {
569 	struct v4l2_subdev sd;
570 	struct media_pad pad;
571 	struct i2c_client *client;
572 	struct imx274_ctrls ctrls;
573 	struct v4l2_rect crop;
574 	struct v4l2_mbus_framefmt format;
575 	struct v4l2_fract frame_interval;
576 	struct regmap *regmap;
577 	struct gpio_desc *reset_gpio;
578 	struct regulator_bulk_data supplies[IMX274_NUM_SUPPLIES];
579 	struct clk *inck;
580 	struct mutex lock; /* mutex lock for operations */
581 	const struct imx274_mode *mode;
582 };
583 
584 #define IMX274_ROUND(dim, step, flags)			\
585 	((flags) & V4L2_SEL_FLAG_GE			\
586 	 ? roundup((dim), (step))			\
587 	 : ((flags) & V4L2_SEL_FLAG_LE			\
588 	    ? rounddown((dim), (step))			\
589 	    : rounddown((dim) + (step) / 2, (step))))
590 
591 /*
592  * Function declaration
593  */
594 static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
595 static int imx274_set_exposure(struct stimx274 *priv, int val);
596 static int imx274_set_vflip(struct stimx274 *priv, int val);
597 static int imx274_set_test_pattern(struct stimx274 *priv, int val);
598 static int imx274_set_frame_interval(struct stimx274 *priv,
599 				     struct v4l2_fract frame_interval);
600 
601 static inline void msleep_range(unsigned int delay_base)
602 {
603 	usleep_range(delay_base * 1000, delay_base * 1000 + 500);
604 }
605 
606 /*
607  * v4l2_ctrl and v4l2_subdev related operations
608  */
609 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
610 {
611 	return &container_of(ctrl->handler,
612 			     struct stimx274, ctrls.handler)->sd;
613 }
614 
615 static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
616 {
617 	return container_of(sd, struct stimx274, sd);
618 }
619 
620 /*
621  * Writing a register table
622  *
623  * @priv: Pointer to device
624  * @table: Table containing register values (with optional delays)
625  *
626  * This is used to write register table into sensor's reg map.
627  *
628  * Return: 0 on success, errors otherwise
629  */
630 static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
631 {
632 	struct regmap *regmap = priv->regmap;
633 	int err = 0;
634 	const struct reg_8 *next;
635 	u8 val;
636 
637 	int range_start = -1;
638 	int range_count = 0;
639 	u8 range_vals[16];
640 	int max_range_vals = ARRAY_SIZE(range_vals);
641 
642 	for (next = table;; next++) {
643 		if ((next->addr != range_start + range_count) ||
644 		    (next->addr == IMX274_TABLE_END) ||
645 		    (next->addr == IMX274_TABLE_WAIT_MS) ||
646 		    (range_count == max_range_vals)) {
647 			if (range_count == 1)
648 				err = regmap_write(regmap,
649 						   range_start, range_vals[0]);
650 			else if (range_count > 1)
651 				err = regmap_bulk_write(regmap, range_start,
652 							&range_vals[0],
653 							range_count);
654 			else
655 				err = 0;
656 
657 			if (err)
658 				return err;
659 
660 			range_start = -1;
661 			range_count = 0;
662 
663 			/* Handle special address values */
664 			if (next->addr == IMX274_TABLE_END)
665 				break;
666 
667 			if (next->addr == IMX274_TABLE_WAIT_MS) {
668 				msleep_range(next->val);
669 				continue;
670 			}
671 		}
672 
673 		val = next->val;
674 
675 		if (range_start == -1)
676 			range_start = next->addr;
677 
678 		range_vals[range_count++] = val;
679 	}
680 	return 0;
681 }
682 
683 static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
684 {
685 	int err;
686 
687 	err = regmap_write(priv->regmap, addr, val);
688 	if (err)
689 		dev_err(&priv->client->dev,
690 			"%s : i2c write failed, %x = %x\n", __func__,
691 			addr, val);
692 	else
693 		dev_dbg(&priv->client->dev,
694 			"%s : addr 0x%x, val=0x%x\n", __func__,
695 			addr, val);
696 	return err;
697 }
698 
699 /**
700  * imx274_read_mbreg - Read a multibyte register.
701  *
702  * Uses a bulk read where possible.
703  *
704  * @priv: Pointer to device structure
705  * @addr: Address of the LSB register.  Other registers must be
706  *        consecutive, least-to-most significant.
707  * @val: Pointer to store the register value (cpu endianness)
708  * @nbytes: Number of bytes to read (range: [1..3]).
709  *          Other bytes are zet to 0.
710  *
711  * Return: 0 on success, errors otherwise
712  */
713 static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val,
714 			     size_t nbytes)
715 {
716 	__le32 val_le = 0;
717 	int err;
718 
719 	err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes);
720 	if (err) {
721 		dev_err(&priv->client->dev,
722 			"%s : i2c bulk read failed, %x (%zu bytes)\n",
723 			__func__, addr, nbytes);
724 	} else {
725 		*val = le32_to_cpu(val_le);
726 		dev_dbg(&priv->client->dev,
727 			"%s : addr 0x%x, val=0x%x (%zu bytes)\n",
728 			__func__, addr, *val, nbytes);
729 	}
730 
731 	return err;
732 }
733 
734 /**
735  * imx274_write_mbreg - Write a multibyte register.
736  *
737  * Uses a bulk write where possible.
738  *
739  * @priv: Pointer to device structure
740  * @addr: Address of the LSB register.  Other registers must be
741  *        consecutive, least-to-most significant.
742  * @val: Value to be written to the register (cpu endianness)
743  * @nbytes: Number of bytes to write (range: [1..3])
744  */
745 static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
746 			      size_t nbytes)
747 {
748 	__le32 val_le = cpu_to_le32(val);
749 	int err;
750 
751 	err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
752 	if (err)
753 		dev_err(&priv->client->dev,
754 			"%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
755 			__func__, addr, val, nbytes);
756 	else
757 		dev_dbg(&priv->client->dev,
758 			"%s : addr 0x%x, val=0x%x (%zu bytes)\n",
759 			__func__, addr, val, nbytes);
760 	return err;
761 }
762 
763 /*
764  * Set mode registers to start stream.
765  * @priv: Pointer to device structure
766  *
767  * Return: 0 on success, errors otherwise
768  */
769 static int imx274_mode_regs(struct stimx274 *priv)
770 {
771 	int err = 0;
772 
773 	err = imx274_write_table(priv, imx274_start_1);
774 	if (err)
775 		return err;
776 
777 	err = imx274_write_table(priv, priv->mode->init_regs);
778 
779 	return err;
780 }
781 
782 /*
783  * imx274_start_stream - Function for starting stream per mode index
784  * @priv: Pointer to device structure
785  *
786  * Return: 0 on success, errors otherwise
787  */
788 static int imx274_start_stream(struct stimx274 *priv)
789 {
790 	int err = 0;
791 
792 	err = __v4l2_ctrl_handler_setup(&priv->ctrls.handler);
793 	if (err) {
794 		dev_err(&priv->client->dev, "Error %d setup controls\n", err);
795 		return err;
796 	}
797 
798 	/*
799 	 * Refer to "Standby Cancel Sequence when using CSI-2" in
800 	 * imx274 datasheet, it should wait 10ms or more here.
801 	 * give it 1 extra ms for margin
802 	 */
803 	msleep_range(11);
804 	err = imx274_write_table(priv, imx274_start_2);
805 	if (err)
806 		return err;
807 
808 	/*
809 	 * Refer to "Standby Cancel Sequence when using CSI-2" in
810 	 * imx274 datasheet, it should wait 7ms or more here.
811 	 * give it 1 extra ms for margin
812 	 */
813 	msleep_range(8);
814 	err = imx274_write_table(priv, imx274_start_3);
815 	if (err)
816 		return err;
817 
818 	return 0;
819 }
820 
821 /*
822  * imx274_reset - Function called to reset the sensor
823  * @priv: Pointer to device structure
824  * @rst: Input value for determining the sensor's end state after reset
825  *
826  * Set the senor in reset and then
827  * if rst = 0, keep it in reset;
828  * if rst = 1, bring it out of reset.
829  *
830  */
831 static void imx274_reset(struct stimx274 *priv, int rst)
832 {
833 	gpiod_set_value_cansleep(priv->reset_gpio, 0);
834 	usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
835 	gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
836 	usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
837 }
838 
839 static int imx274_power_on(struct device *dev)
840 {
841 	struct i2c_client *client = to_i2c_client(dev);
842 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
843 	struct stimx274 *imx274 = to_imx274(sd);
844 	int ret;
845 
846 	/* keep sensor in reset before power on */
847 	imx274_reset(imx274, 0);
848 
849 	ret = clk_prepare_enable(imx274->inck);
850 	if (ret) {
851 		dev_err(&imx274->client->dev,
852 			"Failed to enable input clock: %d\n", ret);
853 		return ret;
854 	}
855 
856 	ret = regulator_bulk_enable(IMX274_NUM_SUPPLIES, imx274->supplies);
857 	if (ret) {
858 		dev_err(&imx274->client->dev,
859 			"Failed to enable regulators: %d\n", ret);
860 		goto fail_reg;
861 	}
862 
863 	udelay(2);
864 	imx274_reset(imx274, 1);
865 
866 	return 0;
867 
868 fail_reg:
869 	clk_disable_unprepare(imx274->inck);
870 	return ret;
871 }
872 
873 static int imx274_power_off(struct device *dev)
874 {
875 	struct i2c_client *client = to_i2c_client(dev);
876 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
877 	struct stimx274 *imx274 = to_imx274(sd);
878 
879 	imx274_reset(imx274, 0);
880 
881 	regulator_bulk_disable(IMX274_NUM_SUPPLIES, imx274->supplies);
882 
883 	clk_disable_unprepare(imx274->inck);
884 
885 	return 0;
886 }
887 
888 static int imx274_regulators_get(struct device *dev, struct stimx274 *imx274)
889 {
890 	unsigned int i;
891 
892 	for (i = 0; i < IMX274_NUM_SUPPLIES; i++)
893 		imx274->supplies[i].supply = imx274_supply_names[i];
894 
895 	return devm_regulator_bulk_get(dev, IMX274_NUM_SUPPLIES,
896 					imx274->supplies);
897 }
898 
899 /**
900  * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
901  * @ctrl: V4L2 control to be set
902  *
903  * This function is used to set the V4L2 controls for the imx274 sensor.
904  *
905  * Return: 0 on success, errors otherwise
906  */
907 static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
908 {
909 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
910 	struct stimx274 *imx274 = to_imx274(sd);
911 	int ret = -EINVAL;
912 
913 	if (!pm_runtime_get_if_in_use(&imx274->client->dev))
914 		return 0;
915 
916 	dev_dbg(&imx274->client->dev,
917 		"%s : s_ctrl: %s, value: %d\n", __func__,
918 		ctrl->name, ctrl->val);
919 
920 	switch (ctrl->id) {
921 	case V4L2_CID_EXPOSURE:
922 		dev_dbg(&imx274->client->dev,
923 			"%s : set V4L2_CID_EXPOSURE\n", __func__);
924 		ret = imx274_set_exposure(imx274, ctrl->val);
925 		break;
926 
927 	case V4L2_CID_GAIN:
928 		dev_dbg(&imx274->client->dev,
929 			"%s : set V4L2_CID_GAIN\n", __func__);
930 		ret = imx274_set_gain(imx274, ctrl);
931 		break;
932 
933 	case V4L2_CID_VFLIP:
934 		dev_dbg(&imx274->client->dev,
935 			"%s : set V4L2_CID_VFLIP\n", __func__);
936 		ret = imx274_set_vflip(imx274, ctrl->val);
937 		break;
938 
939 	case V4L2_CID_TEST_PATTERN:
940 		dev_dbg(&imx274->client->dev,
941 			"%s : set V4L2_CID_TEST_PATTERN\n", __func__);
942 		ret = imx274_set_test_pattern(imx274, ctrl->val);
943 		break;
944 	}
945 
946 	pm_runtime_put(&imx274->client->dev);
947 
948 	return ret;
949 }
950 
951 static int imx274_binning_goodness(struct stimx274 *imx274,
952 				   int w, int ask_w,
953 				   int h, int ask_h, u32 flags)
954 {
955 	struct device *dev = &imx274->client->dev;
956 	const int goodness = 100000;
957 	int val = 0;
958 
959 	if (flags & V4L2_SEL_FLAG_GE) {
960 		if (w < ask_w)
961 			val -= goodness;
962 		if (h < ask_h)
963 			val -= goodness;
964 	}
965 
966 	if (flags & V4L2_SEL_FLAG_LE) {
967 		if (w > ask_w)
968 			val -= goodness;
969 		if (h > ask_h)
970 			val -= goodness;
971 	}
972 
973 	val -= abs(w - ask_w);
974 	val -= abs(h - ask_h);
975 
976 	dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
977 		__func__, ask_w, ask_h, w, h, val);
978 
979 	return val;
980 }
981 
982 /**
983  * __imx274_change_compose - Helper function to change binning and set both
984  *	compose and format.
985  *
986  * We have two entry points to change binning: set_fmt and
987  * set_selection(COMPOSE). Both have to compute the new output size
988  * and set it in both the compose rect and the frame format size. We
989  * also need to do the same things after setting cropping to restore
990  * 1:1 binning.
991  *
992  * This function contains the common code for these three cases, it
993  * has many arguments in order to accommodate the needs of all of
994  * them.
995  *
996  * Must be called with imx274->lock locked.
997  *
998  * @imx274: The device object
999  * @sd_state: The subdev state we are editing for TRY requests
1000  * @which:  V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
1001  * @width:  Input-output parameter: set to the desired width before
1002  *          the call, contains the chosen value after returning successfully
1003  * @height: Input-output parameter for height (see @width)
1004  * @flags:  Selection flags from struct v4l2_subdev_selection, or 0 if not
1005  *          available (when called from set_fmt)
1006  */
1007 static int __imx274_change_compose(struct stimx274 *imx274,
1008 				   struct v4l2_subdev_state *sd_state,
1009 				   u32 which,
1010 				   u32 *width,
1011 				   u32 *height,
1012 				   u32 flags)
1013 {
1014 	struct device *dev = &imx274->client->dev;
1015 	const struct v4l2_rect *cur_crop;
1016 	struct v4l2_mbus_framefmt *tgt_fmt;
1017 	unsigned int i;
1018 	const struct imx274_mode *best_mode = &imx274_modes[0];
1019 	int best_goodness = INT_MIN;
1020 
1021 	if (which == V4L2_SUBDEV_FORMAT_TRY) {
1022 		cur_crop = &sd_state->pads->try_crop;
1023 		tgt_fmt = &sd_state->pads->try_fmt;
1024 	} else {
1025 		cur_crop = &imx274->crop;
1026 		tgt_fmt = &imx274->format;
1027 	}
1028 
1029 	for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) {
1030 		u8 wratio = imx274_modes[i].wbin_ratio;
1031 		u8 hratio = imx274_modes[i].hbin_ratio;
1032 
1033 		int goodness = imx274_binning_goodness(
1034 			imx274,
1035 			cur_crop->width / wratio, *width,
1036 			cur_crop->height / hratio, *height,
1037 			flags);
1038 
1039 		if (goodness >= best_goodness) {
1040 			best_goodness = goodness;
1041 			best_mode = &imx274_modes[i];
1042 		}
1043 	}
1044 
1045 	*width = cur_crop->width / best_mode->wbin_ratio;
1046 	*height = cur_crop->height / best_mode->hbin_ratio;
1047 
1048 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
1049 		imx274->mode = best_mode;
1050 
1051 	dev_dbg(dev, "%s: selected %ux%u binning\n",
1052 		__func__, best_mode->wbin_ratio, best_mode->hbin_ratio);
1053 
1054 	tgt_fmt->width = *width;
1055 	tgt_fmt->height = *height;
1056 	tgt_fmt->field = V4L2_FIELD_NONE;
1057 
1058 	return 0;
1059 }
1060 
1061 /**
1062  * imx274_get_fmt - Get the pad format
1063  * @sd: Pointer to V4L2 Sub device structure
1064  * @sd_state: Pointer to sub device state structure
1065  * @fmt: Pointer to pad level media bus format
1066  *
1067  * This function is used to get the pad format information.
1068  *
1069  * Return: 0 on success
1070  */
1071 static int imx274_get_fmt(struct v4l2_subdev *sd,
1072 			  struct v4l2_subdev_state *sd_state,
1073 			  struct v4l2_subdev_format *fmt)
1074 {
1075 	struct stimx274 *imx274 = to_imx274(sd);
1076 
1077 	mutex_lock(&imx274->lock);
1078 	fmt->format = imx274->format;
1079 	mutex_unlock(&imx274->lock);
1080 	return 0;
1081 }
1082 
1083 /**
1084  * imx274_set_fmt - This is used to set the pad format
1085  * @sd: Pointer to V4L2 Sub device structure
1086  * @sd_state: Pointer to sub device state information structure
1087  * @format: Pointer to pad level media bus format
1088  *
1089  * This function is used to set the pad format.
1090  *
1091  * Return: 0 on success
1092  */
1093 static int imx274_set_fmt(struct v4l2_subdev *sd,
1094 			  struct v4l2_subdev_state *sd_state,
1095 			  struct v4l2_subdev_format *format)
1096 {
1097 	struct v4l2_mbus_framefmt *fmt = &format->format;
1098 	struct stimx274 *imx274 = to_imx274(sd);
1099 	int err = 0;
1100 
1101 	mutex_lock(&imx274->lock);
1102 
1103 	err = __imx274_change_compose(imx274, sd_state, format->which,
1104 				      &fmt->width, &fmt->height, 0);
1105 
1106 	if (err)
1107 		goto out;
1108 
1109 	/*
1110 	 * __imx274_change_compose already set width and height in the
1111 	 * applicable format, but we need to keep all other format
1112 	 * values, so do a full copy here
1113 	 */
1114 	fmt->field = V4L2_FIELD_NONE;
1115 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1116 		sd_state->pads->try_fmt = *fmt;
1117 	else
1118 		imx274->format = *fmt;
1119 
1120 out:
1121 	mutex_unlock(&imx274->lock);
1122 
1123 	return err;
1124 }
1125 
1126 static int imx274_get_selection(struct v4l2_subdev *sd,
1127 				struct v4l2_subdev_state *sd_state,
1128 				struct v4l2_subdev_selection *sel)
1129 {
1130 	struct stimx274 *imx274 = to_imx274(sd);
1131 	const struct v4l2_rect *src_crop;
1132 	const struct v4l2_mbus_framefmt *src_fmt;
1133 	int ret = 0;
1134 
1135 	if (sel->pad != 0)
1136 		return -EINVAL;
1137 
1138 	if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
1139 		sel->r.left = 0;
1140 		sel->r.top = 0;
1141 		sel->r.width = IMX274_MAX_WIDTH;
1142 		sel->r.height = IMX274_MAX_HEIGHT;
1143 		return 0;
1144 	}
1145 
1146 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1147 		src_crop = &sd_state->pads->try_crop;
1148 		src_fmt = &sd_state->pads->try_fmt;
1149 	} else {
1150 		src_crop = &imx274->crop;
1151 		src_fmt = &imx274->format;
1152 	}
1153 
1154 	mutex_lock(&imx274->lock);
1155 
1156 	switch (sel->target) {
1157 	case V4L2_SEL_TGT_CROP:
1158 		sel->r = *src_crop;
1159 		break;
1160 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1161 		sel->r.top = 0;
1162 		sel->r.left = 0;
1163 		sel->r.width = src_crop->width;
1164 		sel->r.height = src_crop->height;
1165 		break;
1166 	case V4L2_SEL_TGT_COMPOSE:
1167 		sel->r.top = 0;
1168 		sel->r.left = 0;
1169 		sel->r.width = src_fmt->width;
1170 		sel->r.height = src_fmt->height;
1171 		break;
1172 	default:
1173 		ret = -EINVAL;
1174 	}
1175 
1176 	mutex_unlock(&imx274->lock);
1177 
1178 	return ret;
1179 }
1180 
1181 static int imx274_set_selection_crop(struct stimx274 *imx274,
1182 				     struct v4l2_subdev_state *sd_state,
1183 				     struct v4l2_subdev_selection *sel)
1184 {
1185 	struct v4l2_rect *tgt_crop;
1186 	struct v4l2_rect new_crop;
1187 	bool size_changed;
1188 
1189 	/*
1190 	 * h_step could be 12 or 24 depending on the binning. But we
1191 	 * won't know the binning until we choose the mode later in
1192 	 * __imx274_change_compose(). Thus let's be safe and use the
1193 	 * most conservative value in all cases.
1194 	 */
1195 	const u32 h_step = 24;
1196 
1197 	new_crop.width = min_t(u32,
1198 			       IMX274_ROUND(sel->r.width, h_step, sel->flags),
1199 			       IMX274_MAX_WIDTH);
1200 
1201 	/* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1202 	if (new_crop.width < 144)
1203 		new_crop.width = 144;
1204 
1205 	new_crop.left = min_t(u32,
1206 			      IMX274_ROUND(sel->r.left, h_step, 0),
1207 			      IMX274_MAX_WIDTH - new_crop.width);
1208 
1209 	new_crop.height = min_t(u32,
1210 				IMX274_ROUND(sel->r.height, 2, sel->flags),
1211 				IMX274_MAX_HEIGHT);
1212 
1213 	new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
1214 			     IMX274_MAX_HEIGHT - new_crop.height);
1215 
1216 	sel->r = new_crop;
1217 
1218 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1219 		tgt_crop = &sd_state->pads->try_crop;
1220 	else
1221 		tgt_crop = &imx274->crop;
1222 
1223 	mutex_lock(&imx274->lock);
1224 
1225 	size_changed = (new_crop.width != tgt_crop->width ||
1226 			new_crop.height != tgt_crop->height);
1227 
1228 	/* __imx274_change_compose needs the new size in *tgt_crop */
1229 	*tgt_crop = new_crop;
1230 
1231 	/* if crop size changed then reset the output image size */
1232 	if (size_changed)
1233 		__imx274_change_compose(imx274, sd_state, sel->which,
1234 					&new_crop.width, &new_crop.height,
1235 					sel->flags);
1236 
1237 	mutex_unlock(&imx274->lock);
1238 
1239 	return 0;
1240 }
1241 
1242 static int imx274_set_selection(struct v4l2_subdev *sd,
1243 				struct v4l2_subdev_state *sd_state,
1244 				struct v4l2_subdev_selection *sel)
1245 {
1246 	struct stimx274 *imx274 = to_imx274(sd);
1247 
1248 	if (sel->pad != 0)
1249 		return -EINVAL;
1250 
1251 	if (sel->target == V4L2_SEL_TGT_CROP)
1252 		return imx274_set_selection_crop(imx274, sd_state, sel);
1253 
1254 	if (sel->target == V4L2_SEL_TGT_COMPOSE) {
1255 		int err;
1256 
1257 		mutex_lock(&imx274->lock);
1258 		err =  __imx274_change_compose(imx274, sd_state, sel->which,
1259 					       &sel->r.width, &sel->r.height,
1260 					       sel->flags);
1261 		mutex_unlock(&imx274->lock);
1262 
1263 		/*
1264 		 * __imx274_change_compose already set width and
1265 		 * height in set->r, we still need to set top-left
1266 		 */
1267 		if (!err) {
1268 			sel->r.top = 0;
1269 			sel->r.left = 0;
1270 		}
1271 
1272 		return err;
1273 	}
1274 
1275 	return -EINVAL;
1276 }
1277 
1278 static int imx274_apply_trimming(struct stimx274 *imx274)
1279 {
1280 	u32 h_start;
1281 	u32 h_end;
1282 	u32 hmax;
1283 	u32 v_cut;
1284 	s32 v_pos;
1285 	u32 write_v_size;
1286 	u32 y_out_size;
1287 	int err;
1288 
1289 	h_start = imx274->crop.left + 12;
1290 	h_end = h_start + imx274->crop.width;
1291 
1292 	/* Use the minimum allowed value of HMAX */
1293 	/* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1294 	/* Note: 260 is the minimum HMAX in all implemented modes */
1295 	hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
1296 
1297 	/* invert v_pos if VFLIP */
1298 	v_pos = imx274->ctrls.vflip->cur.val ?
1299 		(-imx274->crop.top / 2) : (imx274->crop.top / 2);
1300 	v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
1301 	write_v_size = imx274->crop.height + 22;
1302 	y_out_size   = imx274->crop.height;
1303 
1304 	err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
1305 	if (!err)
1306 		err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
1307 	if (!err)
1308 		err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
1309 					 h_start, 2);
1310 	if (!err)
1311 		err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
1312 					 h_end, 2);
1313 	if (!err)
1314 		err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
1315 	if (!err)
1316 		err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
1317 					 v_cut, 2);
1318 	if (!err)
1319 		err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
1320 					 v_pos, 2);
1321 	if (!err)
1322 		err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
1323 					 write_v_size, 2);
1324 	if (!err)
1325 		err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
1326 					 y_out_size, 2);
1327 
1328 	return err;
1329 }
1330 
1331 /**
1332  * imx274_g_frame_interval - Get the frame interval
1333  * @sd: Pointer to V4L2 Sub device structure
1334  * @fi: Pointer to V4l2 Sub device frame interval structure
1335  *
1336  * This function is used to get the frame interval.
1337  *
1338  * Return: 0 on success
1339  */
1340 static int imx274_g_frame_interval(struct v4l2_subdev *sd,
1341 				   struct v4l2_subdev_frame_interval *fi)
1342 {
1343 	struct stimx274 *imx274 = to_imx274(sd);
1344 
1345 	fi->interval = imx274->frame_interval;
1346 	dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
1347 		__func__, imx274->frame_interval.numerator,
1348 		imx274->frame_interval.denominator);
1349 
1350 	return 0;
1351 }
1352 
1353 /**
1354  * imx274_s_frame_interval - Set the frame interval
1355  * @sd: Pointer to V4L2 Sub device structure
1356  * @fi: Pointer to V4l2 Sub device frame interval structure
1357  *
1358  * This function is used to set the frame intervavl.
1359  *
1360  * Return: 0 on success
1361  */
1362 static int imx274_s_frame_interval(struct v4l2_subdev *sd,
1363 				   struct v4l2_subdev_frame_interval *fi)
1364 {
1365 	struct stimx274 *imx274 = to_imx274(sd);
1366 	struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
1367 	int min, max, def;
1368 	int ret;
1369 
1370 	ret = pm_runtime_resume_and_get(&imx274->client->dev);
1371 	if (ret < 0)
1372 		return ret;
1373 
1374 	mutex_lock(&imx274->lock);
1375 	ret = imx274_set_frame_interval(imx274, fi->interval);
1376 
1377 	if (!ret) {
1378 		fi->interval = imx274->frame_interval;
1379 
1380 		/*
1381 		 * exposure time range is decided by frame interval
1382 		 * need to update it after frame interval changes
1383 		 */
1384 		min = IMX274_MIN_EXPOSURE_TIME;
1385 		max = fi->interval.numerator * 1000000
1386 			/ fi->interval.denominator;
1387 		def = max;
1388 		ret = __v4l2_ctrl_modify_range(ctrl, min, max, 1, def);
1389 		if (ret) {
1390 			dev_err(&imx274->client->dev,
1391 				"Exposure ctrl range update failed\n");
1392 			goto unlock;
1393 		}
1394 
1395 		/* update exposure time accordingly */
1396 		imx274_set_exposure(imx274, ctrl->val);
1397 
1398 		dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
1399 			fi->interval.numerator * 1000000
1400 			/ fi->interval.denominator);
1401 	}
1402 
1403 unlock:
1404 	mutex_unlock(&imx274->lock);
1405 	pm_runtime_put(&imx274->client->dev);
1406 
1407 	return ret;
1408 }
1409 
1410 /**
1411  * imx274_load_default - load default control values
1412  * @priv: Pointer to device structure
1413  *
1414  * Return: 0 on success, errors otherwise
1415  */
1416 static void imx274_load_default(struct stimx274 *priv)
1417 {
1418 	/* load default control values */
1419 	priv->frame_interval.numerator = 1;
1420 	priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1421 	priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
1422 	priv->ctrls.gain->val = IMX274_DEF_GAIN;
1423 	priv->ctrls.vflip->val = 0;
1424 	priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
1425 }
1426 
1427 /**
1428  * imx274_s_stream - It is used to start/stop the streaming.
1429  * @sd: V4L2 Sub device
1430  * @on: Flag (True / False)
1431  *
1432  * This function controls the start or stop of streaming for the
1433  * imx274 sensor.
1434  *
1435  * Return: 0 on success, errors otherwise
1436  */
1437 static int imx274_s_stream(struct v4l2_subdev *sd, int on)
1438 {
1439 	struct stimx274 *imx274 = to_imx274(sd);
1440 	int ret = 0;
1441 
1442 	dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
1443 		on ? "Stream Start" : "Stream Stop",
1444 		imx274->mode - &imx274_modes[0]);
1445 
1446 	mutex_lock(&imx274->lock);
1447 
1448 	if (on) {
1449 		ret = pm_runtime_resume_and_get(&imx274->client->dev);
1450 		if (ret < 0) {
1451 			mutex_unlock(&imx274->lock);
1452 			return ret;
1453 		}
1454 
1455 		/* load mode registers */
1456 		ret = imx274_mode_regs(imx274);
1457 		if (ret)
1458 			goto fail;
1459 
1460 		ret = imx274_apply_trimming(imx274);
1461 		if (ret)
1462 			goto fail;
1463 
1464 		/*
1465 		 * update frame rate & expsoure. if the last mode is different,
1466 		 * HMAX could be changed. As the result, frame rate & exposure
1467 		 * are changed.
1468 		 * gain is not affected.
1469 		 */
1470 		ret = imx274_set_frame_interval(imx274,
1471 						imx274->frame_interval);
1472 		if (ret)
1473 			goto fail;
1474 
1475 		/* start stream */
1476 		ret = imx274_start_stream(imx274);
1477 		if (ret)
1478 			goto fail;
1479 	} else {
1480 		/* stop stream */
1481 		ret = imx274_write_table(imx274, imx274_stop);
1482 		if (ret)
1483 			goto fail;
1484 
1485 		pm_runtime_put(&imx274->client->dev);
1486 	}
1487 
1488 	mutex_unlock(&imx274->lock);
1489 	dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
1490 	return 0;
1491 
1492 fail:
1493 	pm_runtime_put(&imx274->client->dev);
1494 	mutex_unlock(&imx274->lock);
1495 	dev_err(&imx274->client->dev, "s_stream failed\n");
1496 	return ret;
1497 }
1498 
1499 /*
1500  * imx274_get_frame_length - Function for obtaining current frame length
1501  * @priv: Pointer to device structure
1502  * @val: Pointer to obainted value
1503  *
1504  * frame_length = vmax x (svr + 1), in unit of hmax.
1505  *
1506  * Return: 0 on success
1507  */
1508 static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
1509 {
1510 	int err;
1511 	u32 svr;
1512 	u32 vmax;
1513 
1514 	err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1515 	if (err)
1516 		goto fail;
1517 
1518 	err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3);
1519 	if (err)
1520 		goto fail;
1521 
1522 	*val = vmax * (svr + 1);
1523 
1524 	return 0;
1525 
1526 fail:
1527 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1528 	return err;
1529 }
1530 
1531 static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
1532 				    u32 *frame_length)
1533 {
1534 	int err;
1535 
1536 	err = imx274_get_frame_length(priv, frame_length);
1537 	if (err)
1538 		return err;
1539 
1540 	if (*frame_length < priv->mode->min_frame_len)
1541 		*frame_length =  priv->mode->min_frame_len;
1542 
1543 	*val = *frame_length - *val; /* convert to raw shr */
1544 	if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
1545 		*val = *frame_length - IMX274_SHR_LIMIT_CONST;
1546 	else if (*val < priv->mode->min_SHR)
1547 		*val = priv->mode->min_SHR;
1548 
1549 	return 0;
1550 }
1551 
1552 /*
1553  * imx274_set_digital gain - Function called when setting digital gain
1554  * @priv: Pointer to device structure
1555  * @dgain: Value of digital gain.
1556  *
1557  * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1558  *
1559  * Return: 0 on success
1560  */
1561 static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
1562 {
1563 	u8 reg_val;
1564 
1565 	reg_val = ffs(dgain);
1566 
1567 	if (reg_val)
1568 		reg_val--;
1569 
1570 	reg_val = clamp(reg_val, (u8)0, (u8)3);
1571 
1572 	return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
1573 				reg_val & IMX274_MASK_LSB_4_BITS);
1574 }
1575 
1576 /*
1577  * imx274_set_gain - Function called when setting gain
1578  * @priv: Pointer to device structure
1579  * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1580  * @ctrl: v4l2 control pointer
1581  *
1582  * Set the gain based on input value.
1583  * The caller should hold the mutex lock imx274->lock if necessary
1584  *
1585  * Return: 0 on success
1586  */
1587 static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
1588 {
1589 	int err;
1590 	u32 gain, analog_gain, digital_gain, gain_reg;
1591 
1592 	gain = (u32)(ctrl->val);
1593 
1594 	dev_dbg(&priv->client->dev,
1595 		"%s : input gain = %d.%d\n", __func__,
1596 		gain >> IMX274_GAIN_SHIFT,
1597 		((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
1598 
1599 	if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
1600 		gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
1601 	else if (gain < IMX274_MIN_GAIN)
1602 		gain = IMX274_MIN_GAIN;
1603 
1604 	if (gain <= IMX274_MAX_ANALOG_GAIN)
1605 		digital_gain = 1;
1606 	else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
1607 		digital_gain = 2;
1608 	else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
1609 		digital_gain = 4;
1610 	else
1611 		digital_gain = IMX274_MAX_DIGITAL_GAIN;
1612 
1613 	analog_gain = gain / digital_gain;
1614 
1615 	dev_dbg(&priv->client->dev,
1616 		"%s : digital gain = %d, analog gain = %d.%d\n",
1617 		__func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
1618 		((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
1619 		>> IMX274_GAIN_SHIFT);
1620 
1621 	err = imx274_set_digital_gain(priv, digital_gain);
1622 	if (err)
1623 		goto fail;
1624 
1625 	/* convert to register value, refer to imx274 datasheet */
1626 	gain_reg = (u32)IMX274_GAIN_CONST -
1627 		(IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
1628 	if (gain_reg > IMX274_GAIN_REG_MAX)
1629 		gain_reg = IMX274_GAIN_REG_MAX;
1630 
1631 	err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
1632 				 2);
1633 	if (err)
1634 		goto fail;
1635 
1636 	if (IMX274_GAIN_CONST - gain_reg == 0) {
1637 		err = -EINVAL;
1638 		goto fail;
1639 	}
1640 
1641 	/* convert register value back to gain value */
1642 	ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
1643 			/ (IMX274_GAIN_CONST - gain_reg) * digital_gain;
1644 
1645 	dev_dbg(&priv->client->dev,
1646 		"%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1647 		__func__, gain_reg, ctrl->val);
1648 
1649 	return 0;
1650 
1651 fail:
1652 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1653 	return err;
1654 }
1655 
1656 /*
1657  * imx274_set_coarse_time - Function called when setting SHR value
1658  * @priv: Pointer to device structure
1659  * @val: Value for exposure time in number of line_length, or [HMAX]
1660  *
1661  * Set SHR value based on input value.
1662  *
1663  * Return: 0 on success
1664  */
1665 static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
1666 {
1667 	int err;
1668 	u32 coarse_time, frame_length;
1669 
1670 	coarse_time = *val;
1671 
1672 	/* convert exposure_time to appropriate SHR value */
1673 	err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
1674 	if (err)
1675 		goto fail;
1676 
1677 	err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
1678 	if (err)
1679 		goto fail;
1680 
1681 	*val = frame_length - coarse_time;
1682 	return 0;
1683 
1684 fail:
1685 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1686 	return err;
1687 }
1688 
1689 /*
1690  * imx274_set_exposure - Function called when setting exposure time
1691  * @priv: Pointer to device structure
1692  * @val: Variable for exposure time, in the unit of micro-second
1693  *
1694  * Set exposure time based on input value.
1695  * The caller should hold the mutex lock imx274->lock if necessary
1696  *
1697  * Return: 0 on success
1698  */
1699 static int imx274_set_exposure(struct stimx274 *priv, int val)
1700 {
1701 	int err;
1702 	u32 hmax;
1703 	u32 coarse_time; /* exposure time in unit of line (HMAX)*/
1704 
1705 	dev_dbg(&priv->client->dev,
1706 		"%s : EXPOSURE control input = %d\n", __func__, val);
1707 
1708 	/* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1709 
1710 	err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1711 	if (err)
1712 		goto fail;
1713 
1714 	if (hmax == 0) {
1715 		err = -EINVAL;
1716 		goto fail;
1717 	}
1718 
1719 	coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
1720 			- priv->mode->nocpiop) / hmax;
1721 
1722 	/* step 2: convert exposure_time into SHR value */
1723 
1724 	/* set SHR */
1725 	err = imx274_set_coarse_time(priv, &coarse_time);
1726 	if (err)
1727 		goto fail;
1728 
1729 	priv->ctrls.exposure->val =
1730 			(coarse_time * hmax + priv->mode->nocpiop)
1731 			/ (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
1732 
1733 	dev_dbg(&priv->client->dev,
1734 		"%s : EXPOSURE control success\n", __func__);
1735 	return 0;
1736 
1737 fail:
1738 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1739 
1740 	return err;
1741 }
1742 
1743 /*
1744  * imx274_set_vflip - Function called when setting vertical flip
1745  * @priv: Pointer to device structure
1746  * @val: Value for vflip setting
1747  *
1748  * Set vertical flip based on input value.
1749  * val = 0: normal, no vertical flip
1750  * val = 1: vertical flip enabled
1751  * The caller should hold the mutex lock imx274->lock if necessary
1752  *
1753  * Return: 0 on success
1754  */
1755 static int imx274_set_vflip(struct stimx274 *priv, int val)
1756 {
1757 	int err;
1758 
1759 	err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
1760 	if (err) {
1761 		dev_err(&priv->client->dev, "VFLIP control error\n");
1762 		return err;
1763 	}
1764 
1765 	dev_dbg(&priv->client->dev,
1766 		"%s : VFLIP control success\n", __func__);
1767 
1768 	return 0;
1769 }
1770 
1771 /*
1772  * imx274_set_test_pattern - Function called when setting test pattern
1773  * @priv: Pointer to device structure
1774  * @val: Variable for test pattern
1775  *
1776  * Set to different test patterns based on input value.
1777  *
1778  * Return: 0 on success
1779  */
1780 static int imx274_set_test_pattern(struct stimx274 *priv, int val)
1781 {
1782 	int err = 0;
1783 
1784 	if (val == TEST_PATTERN_DISABLED) {
1785 		err = imx274_write_table(priv, imx274_tp_disabled);
1786 	} else if (val <= TEST_PATTERN_V_COLOR_BARS) {
1787 		err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
1788 		if (!err)
1789 			err = imx274_write_table(priv, imx274_tp_regs);
1790 	} else {
1791 		err = -EINVAL;
1792 	}
1793 
1794 	if (!err)
1795 		dev_dbg(&priv->client->dev,
1796 			"%s : TEST PATTERN control success\n", __func__);
1797 	else
1798 		dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1799 
1800 	return err;
1801 }
1802 
1803 /*
1804  * imx274_set_frame_length - Function called when setting frame length
1805  * @priv: Pointer to device structure
1806  * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1807  *
1808  * Set frame length based on input value.
1809  *
1810  * Return: 0 on success
1811  */
1812 static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
1813 {
1814 	int err;
1815 	u32 frame_length;
1816 
1817 	dev_dbg(&priv->client->dev, "%s : input length = %d\n",
1818 		__func__, val);
1819 
1820 	frame_length = (u32)val;
1821 
1822 	err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
1823 	if (err)
1824 		goto fail;
1825 
1826 	return 0;
1827 
1828 fail:
1829 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1830 	return err;
1831 }
1832 
1833 /*
1834  * imx274_set_frame_interval - Function called when setting frame interval
1835  * @priv: Pointer to device structure
1836  * @frame_interval: Variable for frame interval
1837  *
1838  * Change frame interval by updating VMAX value
1839  * The caller should hold the mutex lock imx274->lock if necessary
1840  *
1841  * Return: 0 on success
1842  */
1843 static int imx274_set_frame_interval(struct stimx274 *priv,
1844 				     struct v4l2_fract frame_interval)
1845 {
1846 	int err;
1847 	u32 frame_length, req_frame_rate;
1848 	u32 svr;
1849 	u32 hmax;
1850 
1851 	dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
1852 		__func__, frame_interval.numerator,
1853 		frame_interval.denominator);
1854 
1855 	if (frame_interval.numerator == 0 || frame_interval.denominator == 0) {
1856 		frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1857 		frame_interval.numerator = 1;
1858 	}
1859 
1860 	req_frame_rate = (u32)(frame_interval.denominator
1861 				/ frame_interval.numerator);
1862 
1863 	/* boundary check */
1864 	if (req_frame_rate > priv->mode->max_fps) {
1865 		frame_interval.numerator = 1;
1866 		frame_interval.denominator = priv->mode->max_fps;
1867 	} else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
1868 		frame_interval.numerator = 1;
1869 		frame_interval.denominator = IMX274_MIN_FRAME_RATE;
1870 	}
1871 
1872 	/*
1873 	 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1874 	 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1875 	 */
1876 
1877 	err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1878 	if (err)
1879 		goto fail;
1880 
1881 	dev_dbg(&priv->client->dev,
1882 		"%s : register SVR = %d\n", __func__, svr);
1883 
1884 	err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1885 	if (err)
1886 		goto fail;
1887 
1888 	dev_dbg(&priv->client->dev,
1889 		"%s : register HMAX = %d\n", __func__, hmax);
1890 
1891 	if (hmax == 0 || frame_interval.denominator == 0) {
1892 		err = -EINVAL;
1893 		goto fail;
1894 	}
1895 
1896 	frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
1897 					* frame_interval.numerator
1898 					/ frame_interval.denominator;
1899 
1900 	err = imx274_set_frame_length(priv, frame_length);
1901 	if (err)
1902 		goto fail;
1903 
1904 	priv->frame_interval = frame_interval;
1905 	return 0;
1906 
1907 fail:
1908 	dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1909 	return err;
1910 }
1911 
1912 static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
1913 	.get_fmt = imx274_get_fmt,
1914 	.set_fmt = imx274_set_fmt,
1915 	.get_selection = imx274_get_selection,
1916 	.set_selection = imx274_set_selection,
1917 };
1918 
1919 static const struct v4l2_subdev_video_ops imx274_video_ops = {
1920 	.g_frame_interval = imx274_g_frame_interval,
1921 	.s_frame_interval = imx274_s_frame_interval,
1922 	.s_stream = imx274_s_stream,
1923 };
1924 
1925 static const struct v4l2_subdev_ops imx274_subdev_ops = {
1926 	.pad = &imx274_pad_ops,
1927 	.video = &imx274_video_ops,
1928 };
1929 
1930 static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
1931 	.s_ctrl	= imx274_s_ctrl,
1932 };
1933 
1934 static const struct of_device_id imx274_of_id_table[] = {
1935 	{ .compatible = "sony,imx274" },
1936 	{ }
1937 };
1938 MODULE_DEVICE_TABLE(of, imx274_of_id_table);
1939 
1940 static const struct i2c_device_id imx274_id[] = {
1941 	{ "IMX274", 0 },
1942 	{ }
1943 };
1944 MODULE_DEVICE_TABLE(i2c, imx274_id);
1945 
1946 static int imx274_probe(struct i2c_client *client)
1947 {
1948 	struct v4l2_subdev *sd;
1949 	struct stimx274 *imx274;
1950 	int ret;
1951 
1952 	/* initialize imx274 */
1953 	imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL);
1954 	if (!imx274)
1955 		return -ENOMEM;
1956 
1957 	mutex_init(&imx274->lock);
1958 
1959 	imx274->inck = devm_clk_get_optional(&client->dev, "inck");
1960 	if (IS_ERR(imx274->inck))
1961 		return PTR_ERR(imx274->inck);
1962 
1963 	ret = imx274_regulators_get(&client->dev, imx274);
1964 	if (ret) {
1965 		dev_err(&client->dev,
1966 			"Failed to get power regulators, err: %d\n", ret);
1967 		return ret;
1968 	}
1969 
1970 	/* initialize format */
1971 	imx274->mode = &imx274_modes[0];
1972 	imx274->crop.width = IMX274_MAX_WIDTH;
1973 	imx274->crop.height = IMX274_MAX_HEIGHT;
1974 	imx274->format.width = imx274->crop.width / imx274->mode->wbin_ratio;
1975 	imx274->format.height = imx274->crop.height / imx274->mode->hbin_ratio;
1976 	imx274->format.field = V4L2_FIELD_NONE;
1977 	imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
1978 	imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
1979 	imx274->frame_interval.numerator = 1;
1980 	imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1981 
1982 	/* initialize regmap */
1983 	imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
1984 	if (IS_ERR(imx274->regmap)) {
1985 		dev_err(&client->dev,
1986 			"regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
1987 		ret = -ENODEV;
1988 		goto err_regmap;
1989 	}
1990 
1991 	/* initialize subdevice */
1992 	imx274->client = client;
1993 	sd = &imx274->sd;
1994 	v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
1995 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1996 
1997 	/* initialize subdev media pad */
1998 	imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
1999 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
2000 	ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
2001 	if (ret < 0) {
2002 		dev_err(&client->dev,
2003 			"%s : media entity init Failed %d\n", __func__, ret);
2004 		goto err_regmap;
2005 	}
2006 
2007 	/* initialize sensor reset gpio */
2008 	imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
2009 						     GPIOD_OUT_HIGH);
2010 	if (IS_ERR(imx274->reset_gpio)) {
2011 		if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER)
2012 			dev_err(&client->dev, "Reset GPIO not setup in DT");
2013 		ret = PTR_ERR(imx274->reset_gpio);
2014 		goto err_me;
2015 	}
2016 
2017 	/* power on the sensor */
2018 	ret = imx274_power_on(&client->dev);
2019 	if (ret < 0) {
2020 		dev_err(&client->dev,
2021 			"%s : imx274 power on failed\n", __func__);
2022 		goto err_me;
2023 	}
2024 
2025 	/* initialize controls */
2026 	ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4);
2027 	if (ret < 0) {
2028 		dev_err(&client->dev,
2029 			"%s : ctrl handler init Failed\n", __func__);
2030 		goto err_power_off;
2031 	}
2032 
2033 	imx274->ctrls.handler.lock = &imx274->lock;
2034 
2035 	/* add new controls */
2036 	imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
2037 		&imx274->ctrls.handler, &imx274_ctrl_ops,
2038 		V4L2_CID_TEST_PATTERN,
2039 		ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
2040 
2041 	imx274->ctrls.gain = v4l2_ctrl_new_std(
2042 		&imx274->ctrls.handler,
2043 		&imx274_ctrl_ops,
2044 		V4L2_CID_GAIN, IMX274_MIN_GAIN,
2045 		IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
2046 		IMX274_DEF_GAIN);
2047 
2048 	imx274->ctrls.exposure = v4l2_ctrl_new_std(
2049 		&imx274->ctrls.handler,
2050 		&imx274_ctrl_ops,
2051 		V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
2052 		1000000 / IMX274_DEF_FRAME_RATE, 1,
2053 		IMX274_MIN_EXPOSURE_TIME);
2054 
2055 	imx274->ctrls.vflip = v4l2_ctrl_new_std(
2056 		&imx274->ctrls.handler,
2057 		&imx274_ctrl_ops,
2058 		V4L2_CID_VFLIP, 0, 1, 1, 0);
2059 
2060 	imx274->sd.ctrl_handler = &imx274->ctrls.handler;
2061 	if (imx274->ctrls.handler.error) {
2062 		ret = imx274->ctrls.handler.error;
2063 		goto err_ctrls;
2064 	}
2065 
2066 	/* load default control values */
2067 	imx274_load_default(imx274);
2068 
2069 	/* register subdevice */
2070 	ret = v4l2_async_register_subdev(sd);
2071 	if (ret < 0) {
2072 		dev_err(&client->dev,
2073 			"%s : v4l2_async_register_subdev failed %d\n",
2074 			__func__, ret);
2075 		goto err_ctrls;
2076 	}
2077 
2078 	pm_runtime_set_active(&client->dev);
2079 	pm_runtime_enable(&client->dev);
2080 	pm_runtime_idle(&client->dev);
2081 
2082 	dev_info(&client->dev, "imx274 : imx274 probe success !\n");
2083 	return 0;
2084 
2085 err_ctrls:
2086 	v4l2_ctrl_handler_free(&imx274->ctrls.handler);
2087 err_power_off:
2088 	imx274_power_off(&client->dev);
2089 err_me:
2090 	media_entity_cleanup(&sd->entity);
2091 err_regmap:
2092 	mutex_destroy(&imx274->lock);
2093 	return ret;
2094 }
2095 
2096 static int imx274_remove(struct i2c_client *client)
2097 {
2098 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2099 	struct stimx274 *imx274 = to_imx274(sd);
2100 
2101 	pm_runtime_disable(&client->dev);
2102 	if (!pm_runtime_status_suspended(&client->dev))
2103 		imx274_power_off(&client->dev);
2104 	pm_runtime_set_suspended(&client->dev);
2105 
2106 	v4l2_async_unregister_subdev(sd);
2107 	v4l2_ctrl_handler_free(&imx274->ctrls.handler);
2108 
2109 	media_entity_cleanup(&sd->entity);
2110 	mutex_destroy(&imx274->lock);
2111 	return 0;
2112 }
2113 
2114 static const struct dev_pm_ops imx274_pm_ops = {
2115 	SET_RUNTIME_PM_OPS(imx274_power_off, imx274_power_on, NULL)
2116 };
2117 
2118 static struct i2c_driver imx274_i2c_driver = {
2119 	.driver = {
2120 		.name	= DRIVER_NAME,
2121 		.pm = &imx274_pm_ops,
2122 		.of_match_table	= imx274_of_id_table,
2123 	},
2124 	.probe_new	= imx274_probe,
2125 	.remove		= imx274_remove,
2126 	.id_table	= imx274_id,
2127 };
2128 
2129 module_i2c_driver(imx274_i2c_driver);
2130 
2131 MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
2132 MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
2133 MODULE_LICENSE("GPL v2");
2134