xref: /openbmc/linux/drivers/media/i2c/imx258.c (revision 79e790ff)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3 
4 #include <linux/acpi.h>
5 #include <linux/clk.h>
6 #include <linux/delay.h>
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 #include <asm/unaligned.h>
13 
14 #define IMX258_REG_VALUE_08BIT		1
15 #define IMX258_REG_VALUE_16BIT		2
16 
17 #define IMX258_REG_MODE_SELECT		0x0100
18 #define IMX258_MODE_STANDBY		0x00
19 #define IMX258_MODE_STREAMING		0x01
20 
21 /* Chip ID */
22 #define IMX258_REG_CHIP_ID		0x0016
23 #define IMX258_CHIP_ID			0x0258
24 
25 /* V_TIMING internal */
26 #define IMX258_VTS_30FPS		0x0c98
27 #define IMX258_VTS_30FPS_2K		0x0638
28 #define IMX258_VTS_30FPS_VGA		0x034c
29 #define IMX258_VTS_MAX			0xffff
30 
31 /*Frame Length Line*/
32 #define IMX258_FLL_MIN			0x08a6
33 #define IMX258_FLL_MAX			0xffff
34 #define IMX258_FLL_STEP			1
35 #define IMX258_FLL_DEFAULT		0x0c98
36 
37 /* HBLANK control - read only */
38 #define IMX258_PPL_DEFAULT		5352
39 
40 /* Exposure control */
41 #define IMX258_REG_EXPOSURE		0x0202
42 #define IMX258_EXPOSURE_MIN		4
43 #define IMX258_EXPOSURE_STEP		1
44 #define IMX258_EXPOSURE_DEFAULT		0x640
45 #define IMX258_EXPOSURE_MAX		65535
46 
47 /* Analog gain control */
48 #define IMX258_REG_ANALOG_GAIN		0x0204
49 #define IMX258_ANA_GAIN_MIN		0
50 #define IMX258_ANA_GAIN_MAX		0x1fff
51 #define IMX258_ANA_GAIN_STEP		1
52 #define IMX258_ANA_GAIN_DEFAULT		0x0
53 
54 /* Digital gain control */
55 #define IMX258_REG_GR_DIGITAL_GAIN	0x020e
56 #define IMX258_REG_R_DIGITAL_GAIN	0x0210
57 #define IMX258_REG_B_DIGITAL_GAIN	0x0212
58 #define IMX258_REG_GB_DIGITAL_GAIN	0x0214
59 #define IMX258_DGTL_GAIN_MIN		0
60 #define IMX258_DGTL_GAIN_MAX		4096	/* Max = 0xFFF */
61 #define IMX258_DGTL_GAIN_DEFAULT	1024
62 #define IMX258_DGTL_GAIN_STEP		1
63 
64 /* HDR control */
65 #define IMX258_REG_HDR			0x0220
66 #define IMX258_HDR_ON			BIT(0)
67 #define IMX258_REG_HDR_RATIO		0x0222
68 #define IMX258_HDR_RATIO_MIN		0
69 #define IMX258_HDR_RATIO_MAX		5
70 #define IMX258_HDR_RATIO_STEP		1
71 #define IMX258_HDR_RATIO_DEFAULT	0x0
72 
73 /* Test Pattern Control */
74 #define IMX258_REG_TEST_PATTERN		0x0600
75 
76 /* Orientation */
77 #define REG_MIRROR_FLIP_CONTROL		0x0101
78 #define REG_CONFIG_MIRROR_FLIP		0x03
79 #define REG_CONFIG_FLIP_TEST_PATTERN	0x02
80 
81 /* Input clock frequency in Hz */
82 #define IMX258_INPUT_CLOCK_FREQ		19200000
83 
84 struct imx258_reg {
85 	u16 address;
86 	u8 val;
87 };
88 
89 struct imx258_reg_list {
90 	u32 num_of_regs;
91 	const struct imx258_reg *regs;
92 };
93 
94 /* Link frequency config */
95 struct imx258_link_freq_config {
96 	u32 pixels_per_line;
97 
98 	/* PLL registers for this link frequency */
99 	struct imx258_reg_list reg_list;
100 };
101 
102 /* Mode : resolution and related config&values */
103 struct imx258_mode {
104 	/* Frame width */
105 	u32 width;
106 	/* Frame height */
107 	u32 height;
108 
109 	/* V-timing */
110 	u32 vts_def;
111 	u32 vts_min;
112 
113 	/* Index of Link frequency config to be used */
114 	u32 link_freq_index;
115 	/* Default register values */
116 	struct imx258_reg_list reg_list;
117 };
118 
119 /* 4208x3118 needs 1267Mbps/lane, 4 lanes */
120 static const struct imx258_reg mipi_data_rate_1267mbps[] = {
121 	{ 0x0301, 0x05 },
122 	{ 0x0303, 0x02 },
123 	{ 0x0305, 0x03 },
124 	{ 0x0306, 0x00 },
125 	{ 0x0307, 0xC6 },
126 	{ 0x0309, 0x0A },
127 	{ 0x030B, 0x01 },
128 	{ 0x030D, 0x02 },
129 	{ 0x030E, 0x00 },
130 	{ 0x030F, 0xD8 },
131 	{ 0x0310, 0x00 },
132 	{ 0x0820, 0x13 },
133 	{ 0x0821, 0x4C },
134 	{ 0x0822, 0xCC },
135 	{ 0x0823, 0xCC },
136 };
137 
138 static const struct imx258_reg mipi_data_rate_640mbps[] = {
139 	{ 0x0301, 0x05 },
140 	{ 0x0303, 0x02 },
141 	{ 0x0305, 0x03 },
142 	{ 0x0306, 0x00 },
143 	{ 0x0307, 0x64 },
144 	{ 0x0309, 0x0A },
145 	{ 0x030B, 0x01 },
146 	{ 0x030D, 0x02 },
147 	{ 0x030E, 0x00 },
148 	{ 0x030F, 0xD8 },
149 	{ 0x0310, 0x00 },
150 	{ 0x0820, 0x0A },
151 	{ 0x0821, 0x00 },
152 	{ 0x0822, 0x00 },
153 	{ 0x0823, 0x00 },
154 };
155 
156 static const struct imx258_reg mode_4208x3118_regs[] = {
157 	{ 0x0136, 0x13 },
158 	{ 0x0137, 0x33 },
159 	{ 0x3051, 0x00 },
160 	{ 0x3052, 0x00 },
161 	{ 0x4E21, 0x14 },
162 	{ 0x6B11, 0xCF },
163 	{ 0x7FF0, 0x08 },
164 	{ 0x7FF1, 0x0F },
165 	{ 0x7FF2, 0x08 },
166 	{ 0x7FF3, 0x1B },
167 	{ 0x7FF4, 0x23 },
168 	{ 0x7FF5, 0x60 },
169 	{ 0x7FF6, 0x00 },
170 	{ 0x7FF7, 0x01 },
171 	{ 0x7FF8, 0x00 },
172 	{ 0x7FF9, 0x78 },
173 	{ 0x7FFA, 0x00 },
174 	{ 0x7FFB, 0x00 },
175 	{ 0x7FFC, 0x00 },
176 	{ 0x7FFD, 0x00 },
177 	{ 0x7FFE, 0x00 },
178 	{ 0x7FFF, 0x03 },
179 	{ 0x7F76, 0x03 },
180 	{ 0x7F77, 0xFE },
181 	{ 0x7FA8, 0x03 },
182 	{ 0x7FA9, 0xFE },
183 	{ 0x7B24, 0x81 },
184 	{ 0x7B25, 0x00 },
185 	{ 0x6564, 0x07 },
186 	{ 0x6B0D, 0x41 },
187 	{ 0x653D, 0x04 },
188 	{ 0x6B05, 0x8C },
189 	{ 0x6B06, 0xF9 },
190 	{ 0x6B08, 0x65 },
191 	{ 0x6B09, 0xFC },
192 	{ 0x6B0A, 0xCF },
193 	{ 0x6B0B, 0xD2 },
194 	{ 0x6700, 0x0E },
195 	{ 0x6707, 0x0E },
196 	{ 0x9104, 0x00 },
197 	{ 0x4648, 0x7F },
198 	{ 0x7420, 0x00 },
199 	{ 0x7421, 0x1C },
200 	{ 0x7422, 0x00 },
201 	{ 0x7423, 0xD7 },
202 	{ 0x5F04, 0x00 },
203 	{ 0x5F05, 0xED },
204 	{ 0x0112, 0x0A },
205 	{ 0x0113, 0x0A },
206 	{ 0x0114, 0x03 },
207 	{ 0x0342, 0x14 },
208 	{ 0x0343, 0xE8 },
209 	{ 0x0340, 0x0C },
210 	{ 0x0341, 0x50 },
211 	{ 0x0344, 0x00 },
212 	{ 0x0345, 0x00 },
213 	{ 0x0346, 0x00 },
214 	{ 0x0347, 0x00 },
215 	{ 0x0348, 0x10 },
216 	{ 0x0349, 0x6F },
217 	{ 0x034A, 0x0C },
218 	{ 0x034B, 0x2E },
219 	{ 0x0381, 0x01 },
220 	{ 0x0383, 0x01 },
221 	{ 0x0385, 0x01 },
222 	{ 0x0387, 0x01 },
223 	{ 0x0900, 0x00 },
224 	{ 0x0901, 0x11 },
225 	{ 0x0401, 0x00 },
226 	{ 0x0404, 0x00 },
227 	{ 0x0405, 0x10 },
228 	{ 0x0408, 0x00 },
229 	{ 0x0409, 0x00 },
230 	{ 0x040A, 0x00 },
231 	{ 0x040B, 0x00 },
232 	{ 0x040C, 0x10 },
233 	{ 0x040D, 0x70 },
234 	{ 0x040E, 0x0C },
235 	{ 0x040F, 0x30 },
236 	{ 0x3038, 0x00 },
237 	{ 0x303A, 0x00 },
238 	{ 0x303B, 0x10 },
239 	{ 0x300D, 0x00 },
240 	{ 0x034C, 0x10 },
241 	{ 0x034D, 0x70 },
242 	{ 0x034E, 0x0C },
243 	{ 0x034F, 0x30 },
244 	{ 0x0350, 0x01 },
245 	{ 0x0202, 0x0C },
246 	{ 0x0203, 0x46 },
247 	{ 0x0204, 0x00 },
248 	{ 0x0205, 0x00 },
249 	{ 0x020E, 0x01 },
250 	{ 0x020F, 0x00 },
251 	{ 0x0210, 0x01 },
252 	{ 0x0211, 0x00 },
253 	{ 0x0212, 0x01 },
254 	{ 0x0213, 0x00 },
255 	{ 0x0214, 0x01 },
256 	{ 0x0215, 0x00 },
257 	{ 0x7BCD, 0x00 },
258 	{ 0x94DC, 0x20 },
259 	{ 0x94DD, 0x20 },
260 	{ 0x94DE, 0x20 },
261 	{ 0x95DC, 0x20 },
262 	{ 0x95DD, 0x20 },
263 	{ 0x95DE, 0x20 },
264 	{ 0x7FB0, 0x00 },
265 	{ 0x9010, 0x3E },
266 	{ 0x9419, 0x50 },
267 	{ 0x941B, 0x50 },
268 	{ 0x9519, 0x50 },
269 	{ 0x951B, 0x50 },
270 	{ 0x3030, 0x00 },
271 	{ 0x3032, 0x00 },
272 	{ 0x0220, 0x00 },
273 };
274 
275 static const struct imx258_reg mode_2104_1560_regs[] = {
276 	{ 0x0136, 0x13 },
277 	{ 0x0137, 0x33 },
278 	{ 0x3051, 0x00 },
279 	{ 0x3052, 0x00 },
280 	{ 0x4E21, 0x14 },
281 	{ 0x6B11, 0xCF },
282 	{ 0x7FF0, 0x08 },
283 	{ 0x7FF1, 0x0F },
284 	{ 0x7FF2, 0x08 },
285 	{ 0x7FF3, 0x1B },
286 	{ 0x7FF4, 0x23 },
287 	{ 0x7FF5, 0x60 },
288 	{ 0x7FF6, 0x00 },
289 	{ 0x7FF7, 0x01 },
290 	{ 0x7FF8, 0x00 },
291 	{ 0x7FF9, 0x78 },
292 	{ 0x7FFA, 0x00 },
293 	{ 0x7FFB, 0x00 },
294 	{ 0x7FFC, 0x00 },
295 	{ 0x7FFD, 0x00 },
296 	{ 0x7FFE, 0x00 },
297 	{ 0x7FFF, 0x03 },
298 	{ 0x7F76, 0x03 },
299 	{ 0x7F77, 0xFE },
300 	{ 0x7FA8, 0x03 },
301 	{ 0x7FA9, 0xFE },
302 	{ 0x7B24, 0x81 },
303 	{ 0x7B25, 0x00 },
304 	{ 0x6564, 0x07 },
305 	{ 0x6B0D, 0x41 },
306 	{ 0x653D, 0x04 },
307 	{ 0x6B05, 0x8C },
308 	{ 0x6B06, 0xF9 },
309 	{ 0x6B08, 0x65 },
310 	{ 0x6B09, 0xFC },
311 	{ 0x6B0A, 0xCF },
312 	{ 0x6B0B, 0xD2 },
313 	{ 0x6700, 0x0E },
314 	{ 0x6707, 0x0E },
315 	{ 0x9104, 0x00 },
316 	{ 0x4648, 0x7F },
317 	{ 0x7420, 0x00 },
318 	{ 0x7421, 0x1C },
319 	{ 0x7422, 0x00 },
320 	{ 0x7423, 0xD7 },
321 	{ 0x5F04, 0x00 },
322 	{ 0x5F05, 0xED },
323 	{ 0x0112, 0x0A },
324 	{ 0x0113, 0x0A },
325 	{ 0x0114, 0x03 },
326 	{ 0x0342, 0x14 },
327 	{ 0x0343, 0xE8 },
328 	{ 0x0340, 0x06 },
329 	{ 0x0341, 0x38 },
330 	{ 0x0344, 0x00 },
331 	{ 0x0345, 0x00 },
332 	{ 0x0346, 0x00 },
333 	{ 0x0347, 0x00 },
334 	{ 0x0348, 0x10 },
335 	{ 0x0349, 0x6F },
336 	{ 0x034A, 0x0C },
337 	{ 0x034B, 0x2E },
338 	{ 0x0381, 0x01 },
339 	{ 0x0383, 0x01 },
340 	{ 0x0385, 0x01 },
341 	{ 0x0387, 0x01 },
342 	{ 0x0900, 0x01 },
343 	{ 0x0901, 0x12 },
344 	{ 0x0401, 0x01 },
345 	{ 0x0404, 0x00 },
346 	{ 0x0405, 0x20 },
347 	{ 0x0408, 0x00 },
348 	{ 0x0409, 0x02 },
349 	{ 0x040A, 0x00 },
350 	{ 0x040B, 0x00 },
351 	{ 0x040C, 0x10 },
352 	{ 0x040D, 0x6A },
353 	{ 0x040E, 0x06 },
354 	{ 0x040F, 0x18 },
355 	{ 0x3038, 0x00 },
356 	{ 0x303A, 0x00 },
357 	{ 0x303B, 0x10 },
358 	{ 0x300D, 0x00 },
359 	{ 0x034C, 0x08 },
360 	{ 0x034D, 0x38 },
361 	{ 0x034E, 0x06 },
362 	{ 0x034F, 0x18 },
363 	{ 0x0350, 0x01 },
364 	{ 0x0202, 0x06 },
365 	{ 0x0203, 0x2E },
366 	{ 0x0204, 0x00 },
367 	{ 0x0205, 0x00 },
368 	{ 0x020E, 0x01 },
369 	{ 0x020F, 0x00 },
370 	{ 0x0210, 0x01 },
371 	{ 0x0211, 0x00 },
372 	{ 0x0212, 0x01 },
373 	{ 0x0213, 0x00 },
374 	{ 0x0214, 0x01 },
375 	{ 0x0215, 0x00 },
376 	{ 0x7BCD, 0x01 },
377 	{ 0x94DC, 0x20 },
378 	{ 0x94DD, 0x20 },
379 	{ 0x94DE, 0x20 },
380 	{ 0x95DC, 0x20 },
381 	{ 0x95DD, 0x20 },
382 	{ 0x95DE, 0x20 },
383 	{ 0x7FB0, 0x00 },
384 	{ 0x9010, 0x3E },
385 	{ 0x9419, 0x50 },
386 	{ 0x941B, 0x50 },
387 	{ 0x9519, 0x50 },
388 	{ 0x951B, 0x50 },
389 	{ 0x3030, 0x00 },
390 	{ 0x3032, 0x00 },
391 	{ 0x0220, 0x00 },
392 };
393 
394 static const struct imx258_reg mode_1048_780_regs[] = {
395 	{ 0x0136, 0x13 },
396 	{ 0x0137, 0x33 },
397 	{ 0x3051, 0x00 },
398 	{ 0x3052, 0x00 },
399 	{ 0x4E21, 0x14 },
400 	{ 0x6B11, 0xCF },
401 	{ 0x7FF0, 0x08 },
402 	{ 0x7FF1, 0x0F },
403 	{ 0x7FF2, 0x08 },
404 	{ 0x7FF3, 0x1B },
405 	{ 0x7FF4, 0x23 },
406 	{ 0x7FF5, 0x60 },
407 	{ 0x7FF6, 0x00 },
408 	{ 0x7FF7, 0x01 },
409 	{ 0x7FF8, 0x00 },
410 	{ 0x7FF9, 0x78 },
411 	{ 0x7FFA, 0x00 },
412 	{ 0x7FFB, 0x00 },
413 	{ 0x7FFC, 0x00 },
414 	{ 0x7FFD, 0x00 },
415 	{ 0x7FFE, 0x00 },
416 	{ 0x7FFF, 0x03 },
417 	{ 0x7F76, 0x03 },
418 	{ 0x7F77, 0xFE },
419 	{ 0x7FA8, 0x03 },
420 	{ 0x7FA9, 0xFE },
421 	{ 0x7B24, 0x81 },
422 	{ 0x7B25, 0x00 },
423 	{ 0x6564, 0x07 },
424 	{ 0x6B0D, 0x41 },
425 	{ 0x653D, 0x04 },
426 	{ 0x6B05, 0x8C },
427 	{ 0x6B06, 0xF9 },
428 	{ 0x6B08, 0x65 },
429 	{ 0x6B09, 0xFC },
430 	{ 0x6B0A, 0xCF },
431 	{ 0x6B0B, 0xD2 },
432 	{ 0x6700, 0x0E },
433 	{ 0x6707, 0x0E },
434 	{ 0x9104, 0x00 },
435 	{ 0x4648, 0x7F },
436 	{ 0x7420, 0x00 },
437 	{ 0x7421, 0x1C },
438 	{ 0x7422, 0x00 },
439 	{ 0x7423, 0xD7 },
440 	{ 0x5F04, 0x00 },
441 	{ 0x5F05, 0xED },
442 	{ 0x0112, 0x0A },
443 	{ 0x0113, 0x0A },
444 	{ 0x0114, 0x03 },
445 	{ 0x0342, 0x14 },
446 	{ 0x0343, 0xE8 },
447 	{ 0x0340, 0x03 },
448 	{ 0x0341, 0x4C },
449 	{ 0x0344, 0x00 },
450 	{ 0x0345, 0x00 },
451 	{ 0x0346, 0x00 },
452 	{ 0x0347, 0x00 },
453 	{ 0x0348, 0x10 },
454 	{ 0x0349, 0x6F },
455 	{ 0x034A, 0x0C },
456 	{ 0x034B, 0x2E },
457 	{ 0x0381, 0x01 },
458 	{ 0x0383, 0x01 },
459 	{ 0x0385, 0x01 },
460 	{ 0x0387, 0x01 },
461 	{ 0x0900, 0x01 },
462 	{ 0x0901, 0x14 },
463 	{ 0x0401, 0x01 },
464 	{ 0x0404, 0x00 },
465 	{ 0x0405, 0x40 },
466 	{ 0x0408, 0x00 },
467 	{ 0x0409, 0x06 },
468 	{ 0x040A, 0x00 },
469 	{ 0x040B, 0x00 },
470 	{ 0x040C, 0x10 },
471 	{ 0x040D, 0x64 },
472 	{ 0x040E, 0x03 },
473 	{ 0x040F, 0x0C },
474 	{ 0x3038, 0x00 },
475 	{ 0x303A, 0x00 },
476 	{ 0x303B, 0x10 },
477 	{ 0x300D, 0x00 },
478 	{ 0x034C, 0x04 },
479 	{ 0x034D, 0x18 },
480 	{ 0x034E, 0x03 },
481 	{ 0x034F, 0x0C },
482 	{ 0x0350, 0x01 },
483 	{ 0x0202, 0x03 },
484 	{ 0x0203, 0x42 },
485 	{ 0x0204, 0x00 },
486 	{ 0x0205, 0x00 },
487 	{ 0x020E, 0x01 },
488 	{ 0x020F, 0x00 },
489 	{ 0x0210, 0x01 },
490 	{ 0x0211, 0x00 },
491 	{ 0x0212, 0x01 },
492 	{ 0x0213, 0x00 },
493 	{ 0x0214, 0x01 },
494 	{ 0x0215, 0x00 },
495 	{ 0x7BCD, 0x00 },
496 	{ 0x94DC, 0x20 },
497 	{ 0x94DD, 0x20 },
498 	{ 0x94DE, 0x20 },
499 	{ 0x95DC, 0x20 },
500 	{ 0x95DD, 0x20 },
501 	{ 0x95DE, 0x20 },
502 	{ 0x7FB0, 0x00 },
503 	{ 0x9010, 0x3E },
504 	{ 0x9419, 0x50 },
505 	{ 0x941B, 0x50 },
506 	{ 0x9519, 0x50 },
507 	{ 0x951B, 0x50 },
508 	{ 0x3030, 0x00 },
509 	{ 0x3032, 0x00 },
510 	{ 0x0220, 0x00 },
511 };
512 
513 static const char * const imx258_test_pattern_menu[] = {
514 	"Disabled",
515 	"Solid Colour",
516 	"Eight Vertical Colour Bars",
517 	"Colour Bars With Fade to Grey",
518 	"Pseudorandom Sequence (PN9)",
519 };
520 
521 /* Configurations for supported link frequencies */
522 #define IMX258_LINK_FREQ_634MHZ	633600000ULL
523 #define IMX258_LINK_FREQ_320MHZ	320000000ULL
524 
525 enum {
526 	IMX258_LINK_FREQ_1267MBPS,
527 	IMX258_LINK_FREQ_640MBPS,
528 };
529 
530 /*
531  * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
532  * data rate => double data rate; number of lanes => 4; bits per pixel => 10
533  */
534 static u64 link_freq_to_pixel_rate(u64 f)
535 {
536 	f *= 2 * 4;
537 	do_div(f, 10);
538 
539 	return f;
540 }
541 
542 /* Menu items for LINK_FREQ V4L2 control */
543 static const s64 link_freq_menu_items[] = {
544 	IMX258_LINK_FREQ_634MHZ,
545 	IMX258_LINK_FREQ_320MHZ,
546 };
547 
548 /* Link frequency configs */
549 static const struct imx258_link_freq_config link_freq_configs[] = {
550 	[IMX258_LINK_FREQ_1267MBPS] = {
551 		.pixels_per_line = IMX258_PPL_DEFAULT,
552 		.reg_list = {
553 			.num_of_regs = ARRAY_SIZE(mipi_data_rate_1267mbps),
554 			.regs = mipi_data_rate_1267mbps,
555 		}
556 	},
557 	[IMX258_LINK_FREQ_640MBPS] = {
558 		.pixels_per_line = IMX258_PPL_DEFAULT,
559 		.reg_list = {
560 			.num_of_regs = ARRAY_SIZE(mipi_data_rate_640mbps),
561 			.regs = mipi_data_rate_640mbps,
562 		}
563 	},
564 };
565 
566 /* Mode configs */
567 static const struct imx258_mode supported_modes[] = {
568 	{
569 		.width = 4208,
570 		.height = 3118,
571 		.vts_def = IMX258_VTS_30FPS,
572 		.vts_min = IMX258_VTS_30FPS,
573 		.reg_list = {
574 			.num_of_regs = ARRAY_SIZE(mode_4208x3118_regs),
575 			.regs = mode_4208x3118_regs,
576 		},
577 		.link_freq_index = IMX258_LINK_FREQ_1267MBPS,
578 	},
579 	{
580 		.width = 2104,
581 		.height = 1560,
582 		.vts_def = IMX258_VTS_30FPS_2K,
583 		.vts_min = IMX258_VTS_30FPS_2K,
584 		.reg_list = {
585 			.num_of_regs = ARRAY_SIZE(mode_2104_1560_regs),
586 			.regs = mode_2104_1560_regs,
587 		},
588 		.link_freq_index = IMX258_LINK_FREQ_640MBPS,
589 	},
590 	{
591 		.width = 1048,
592 		.height = 780,
593 		.vts_def = IMX258_VTS_30FPS_VGA,
594 		.vts_min = IMX258_VTS_30FPS_VGA,
595 		.reg_list = {
596 			.num_of_regs = ARRAY_SIZE(mode_1048_780_regs),
597 			.regs = mode_1048_780_regs,
598 		},
599 		.link_freq_index = IMX258_LINK_FREQ_640MBPS,
600 	},
601 };
602 
603 struct imx258 {
604 	struct v4l2_subdev sd;
605 	struct media_pad pad;
606 
607 	struct v4l2_ctrl_handler ctrl_handler;
608 	/* V4L2 Controls */
609 	struct v4l2_ctrl *link_freq;
610 	struct v4l2_ctrl *pixel_rate;
611 	struct v4l2_ctrl *vblank;
612 	struct v4l2_ctrl *hblank;
613 	struct v4l2_ctrl *exposure;
614 
615 	/* Current mode */
616 	const struct imx258_mode *cur_mode;
617 
618 	/*
619 	 * Mutex for serialized access:
620 	 * Protect sensor module set pad format and start/stop streaming safely.
621 	 */
622 	struct mutex mutex;
623 
624 	/* Streaming on/off */
625 	bool streaming;
626 
627 	struct clk *clk;
628 };
629 
630 static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
631 {
632 	return container_of(_sd, struct imx258, sd);
633 }
634 
635 /* Read registers up to 2 at a time */
636 static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
637 {
638 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
639 	struct i2c_msg msgs[2];
640 	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
641 	u8 data_buf[4] = { 0, };
642 	int ret;
643 
644 	if (len > 4)
645 		return -EINVAL;
646 
647 	/* Write register address */
648 	msgs[0].addr = client->addr;
649 	msgs[0].flags = 0;
650 	msgs[0].len = ARRAY_SIZE(addr_buf);
651 	msgs[0].buf = addr_buf;
652 
653 	/* Read data from register */
654 	msgs[1].addr = client->addr;
655 	msgs[1].flags = I2C_M_RD;
656 	msgs[1].len = len;
657 	msgs[1].buf = &data_buf[4 - len];
658 
659 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
660 	if (ret != ARRAY_SIZE(msgs))
661 		return -EIO;
662 
663 	*val = get_unaligned_be32(data_buf);
664 
665 	return 0;
666 }
667 
668 /* Write registers up to 2 at a time */
669 static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
670 {
671 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
672 	u8 buf[6];
673 
674 	if (len > 4)
675 		return -EINVAL;
676 
677 	put_unaligned_be16(reg, buf);
678 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
679 	if (i2c_master_send(client, buf, len + 2) != len + 2)
680 		return -EIO;
681 
682 	return 0;
683 }
684 
685 /* Write a list of registers */
686 static int imx258_write_regs(struct imx258 *imx258,
687 			     const struct imx258_reg *regs, u32 len)
688 {
689 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
690 	unsigned int i;
691 	int ret;
692 
693 	for (i = 0; i < len; i++) {
694 		ret = imx258_write_reg(imx258, regs[i].address, 1,
695 					regs[i].val);
696 		if (ret) {
697 			dev_err_ratelimited(
698 				&client->dev,
699 				"Failed to write reg 0x%4.4x. error = %d\n",
700 				regs[i].address, ret);
701 
702 			return ret;
703 		}
704 	}
705 
706 	return 0;
707 }
708 
709 /* Open sub-device */
710 static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
711 {
712 	struct v4l2_mbus_framefmt *try_fmt =
713 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
714 
715 	/* Initialize try_fmt */
716 	try_fmt->width = supported_modes[0].width;
717 	try_fmt->height = supported_modes[0].height;
718 	try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
719 	try_fmt->field = V4L2_FIELD_NONE;
720 
721 	return 0;
722 }
723 
724 static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
725 {
726 	int ret;
727 
728 	ret = imx258_write_reg(imx258, IMX258_REG_GR_DIGITAL_GAIN,
729 				IMX258_REG_VALUE_16BIT,
730 				val);
731 	if (ret)
732 		return ret;
733 	ret = imx258_write_reg(imx258, IMX258_REG_GB_DIGITAL_GAIN,
734 				IMX258_REG_VALUE_16BIT,
735 				val);
736 	if (ret)
737 		return ret;
738 	ret = imx258_write_reg(imx258, IMX258_REG_R_DIGITAL_GAIN,
739 				IMX258_REG_VALUE_16BIT,
740 				val);
741 	if (ret)
742 		return ret;
743 	ret = imx258_write_reg(imx258, IMX258_REG_B_DIGITAL_GAIN,
744 				IMX258_REG_VALUE_16BIT,
745 				val);
746 	if (ret)
747 		return ret;
748 	return 0;
749 }
750 
751 static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
752 {
753 	struct imx258 *imx258 =
754 		container_of(ctrl->handler, struct imx258, ctrl_handler);
755 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
756 	int ret = 0;
757 
758 	/*
759 	 * Applying V4L2 control value only happens
760 	 * when power is up for streaming
761 	 */
762 	if (pm_runtime_get_if_in_use(&client->dev) == 0)
763 		return 0;
764 
765 	switch (ctrl->id) {
766 	case V4L2_CID_ANALOGUE_GAIN:
767 		ret = imx258_write_reg(imx258, IMX258_REG_ANALOG_GAIN,
768 				IMX258_REG_VALUE_16BIT,
769 				ctrl->val);
770 		break;
771 	case V4L2_CID_EXPOSURE:
772 		ret = imx258_write_reg(imx258, IMX258_REG_EXPOSURE,
773 				IMX258_REG_VALUE_16BIT,
774 				ctrl->val);
775 		break;
776 	case V4L2_CID_DIGITAL_GAIN:
777 		ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
778 				ctrl->val);
779 		break;
780 	case V4L2_CID_TEST_PATTERN:
781 		ret = imx258_write_reg(imx258, IMX258_REG_TEST_PATTERN,
782 				IMX258_REG_VALUE_16BIT,
783 				ctrl->val);
784 		ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
785 				IMX258_REG_VALUE_08BIT,
786 				!ctrl->val ? REG_CONFIG_MIRROR_FLIP :
787 				REG_CONFIG_FLIP_TEST_PATTERN);
788 		break;
789 	case V4L2_CID_WIDE_DYNAMIC_RANGE:
790 		if (!ctrl->val) {
791 			ret = imx258_write_reg(imx258, IMX258_REG_HDR,
792 					       IMX258_REG_VALUE_08BIT,
793 					       IMX258_HDR_RATIO_MIN);
794 		} else {
795 			ret = imx258_write_reg(imx258, IMX258_REG_HDR,
796 					       IMX258_REG_VALUE_08BIT,
797 					       IMX258_HDR_ON);
798 			if (ret)
799 				break;
800 			ret = imx258_write_reg(imx258, IMX258_REG_HDR_RATIO,
801 					       IMX258_REG_VALUE_08BIT,
802 					       BIT(IMX258_HDR_RATIO_MAX));
803 		}
804 		break;
805 	default:
806 		dev_info(&client->dev,
807 			 "ctrl(id:0x%x,val:0x%x) is not handled\n",
808 			 ctrl->id, ctrl->val);
809 		ret = -EINVAL;
810 		break;
811 	}
812 
813 	pm_runtime_put(&client->dev);
814 
815 	return ret;
816 }
817 
818 static const struct v4l2_ctrl_ops imx258_ctrl_ops = {
819 	.s_ctrl = imx258_set_ctrl,
820 };
821 
822 static int imx258_enum_mbus_code(struct v4l2_subdev *sd,
823 				  struct v4l2_subdev_pad_config *cfg,
824 				  struct v4l2_subdev_mbus_code_enum *code)
825 {
826 	/* Only one bayer order(GRBG) is supported */
827 	if (code->index > 0)
828 		return -EINVAL;
829 
830 	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
831 
832 	return 0;
833 }
834 
835 static int imx258_enum_frame_size(struct v4l2_subdev *sd,
836 				  struct v4l2_subdev_pad_config *cfg,
837 				  struct v4l2_subdev_frame_size_enum *fse)
838 {
839 	if (fse->index >= ARRAY_SIZE(supported_modes))
840 		return -EINVAL;
841 
842 	if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
843 		return -EINVAL;
844 
845 	fse->min_width = supported_modes[fse->index].width;
846 	fse->max_width = fse->min_width;
847 	fse->min_height = supported_modes[fse->index].height;
848 	fse->max_height = fse->min_height;
849 
850 	return 0;
851 }
852 
853 static void imx258_update_pad_format(const struct imx258_mode *mode,
854 				     struct v4l2_subdev_format *fmt)
855 {
856 	fmt->format.width = mode->width;
857 	fmt->format.height = mode->height;
858 	fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
859 	fmt->format.field = V4L2_FIELD_NONE;
860 }
861 
862 static int __imx258_get_pad_format(struct imx258 *imx258,
863 				   struct v4l2_subdev_pad_config *cfg,
864 				   struct v4l2_subdev_format *fmt)
865 {
866 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
867 		fmt->format = *v4l2_subdev_get_try_format(&imx258->sd, cfg,
868 							  fmt->pad);
869 	else
870 		imx258_update_pad_format(imx258->cur_mode, fmt);
871 
872 	return 0;
873 }
874 
875 static int imx258_get_pad_format(struct v4l2_subdev *sd,
876 				 struct v4l2_subdev_pad_config *cfg,
877 				 struct v4l2_subdev_format *fmt)
878 {
879 	struct imx258 *imx258 = to_imx258(sd);
880 	int ret;
881 
882 	mutex_lock(&imx258->mutex);
883 	ret = __imx258_get_pad_format(imx258, cfg, fmt);
884 	mutex_unlock(&imx258->mutex);
885 
886 	return ret;
887 }
888 
889 static int imx258_set_pad_format(struct v4l2_subdev *sd,
890 				 struct v4l2_subdev_pad_config *cfg,
891 				 struct v4l2_subdev_format *fmt)
892 {
893 	struct imx258 *imx258 = to_imx258(sd);
894 	const struct imx258_mode *mode;
895 	struct v4l2_mbus_framefmt *framefmt;
896 	s32 vblank_def;
897 	s32 vblank_min;
898 	s64 h_blank;
899 	s64 pixel_rate;
900 	s64 link_freq;
901 
902 	mutex_lock(&imx258->mutex);
903 
904 	/* Only one raw bayer(GBRG) order is supported */
905 	fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
906 
907 	mode = v4l2_find_nearest_size(supported_modes,
908 		ARRAY_SIZE(supported_modes), width, height,
909 		fmt->format.width, fmt->format.height);
910 	imx258_update_pad_format(mode, fmt);
911 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
912 		framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
913 		*framefmt = fmt->format;
914 	} else {
915 		imx258->cur_mode = mode;
916 		__v4l2_ctrl_s_ctrl(imx258->link_freq, mode->link_freq_index);
917 
918 		link_freq = link_freq_menu_items[mode->link_freq_index];
919 		pixel_rate = link_freq_to_pixel_rate(link_freq);
920 		__v4l2_ctrl_s_ctrl_int64(imx258->pixel_rate, pixel_rate);
921 		/* Update limits and set FPS to default */
922 		vblank_def = imx258->cur_mode->vts_def -
923 			     imx258->cur_mode->height;
924 		vblank_min = imx258->cur_mode->vts_min -
925 			     imx258->cur_mode->height;
926 		__v4l2_ctrl_modify_range(
927 			imx258->vblank, vblank_min,
928 			IMX258_VTS_MAX - imx258->cur_mode->height, 1,
929 			vblank_def);
930 		__v4l2_ctrl_s_ctrl(imx258->vblank, vblank_def);
931 		h_blank =
932 			link_freq_configs[mode->link_freq_index].pixels_per_line
933 			 - imx258->cur_mode->width;
934 		__v4l2_ctrl_modify_range(imx258->hblank, h_blank,
935 					 h_blank, 1, h_blank);
936 	}
937 
938 	mutex_unlock(&imx258->mutex);
939 
940 	return 0;
941 }
942 
943 /* Start streaming */
944 static int imx258_start_streaming(struct imx258 *imx258)
945 {
946 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
947 	const struct imx258_reg_list *reg_list;
948 	int ret, link_freq_index;
949 
950 	/* Setup PLL */
951 	link_freq_index = imx258->cur_mode->link_freq_index;
952 	reg_list = &link_freq_configs[link_freq_index].reg_list;
953 	ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
954 	if (ret) {
955 		dev_err(&client->dev, "%s failed to set plls\n", __func__);
956 		return ret;
957 	}
958 
959 	/* Apply default values of current mode */
960 	reg_list = &imx258->cur_mode->reg_list;
961 	ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
962 	if (ret) {
963 		dev_err(&client->dev, "%s failed to set mode\n", __func__);
964 		return ret;
965 	}
966 
967 	/* Set Orientation be 180 degree */
968 	ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
969 			       IMX258_REG_VALUE_08BIT, REG_CONFIG_MIRROR_FLIP);
970 	if (ret) {
971 		dev_err(&client->dev, "%s failed to set orientation\n",
972 			__func__);
973 		return ret;
974 	}
975 
976 	/* Apply customized values from user */
977 	ret =  __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
978 	if (ret)
979 		return ret;
980 
981 	/* set stream on register */
982 	return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
983 				IMX258_REG_VALUE_08BIT,
984 				IMX258_MODE_STREAMING);
985 }
986 
987 /* Stop streaming */
988 static int imx258_stop_streaming(struct imx258 *imx258)
989 {
990 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
991 	int ret;
992 
993 	/* set stream off register */
994 	ret = imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
995 		IMX258_REG_VALUE_08BIT, IMX258_MODE_STANDBY);
996 	if (ret)
997 		dev_err(&client->dev, "%s failed to set stream\n", __func__);
998 
999 	/*
1000 	 * Return success even if it was an error, as there is nothing the
1001 	 * caller can do about it.
1002 	 */
1003 	return 0;
1004 }
1005 
1006 static int imx258_power_on(struct device *dev)
1007 {
1008 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1009 	struct imx258 *imx258 = to_imx258(sd);
1010 	int ret;
1011 
1012 	ret = clk_prepare_enable(imx258->clk);
1013 	if (ret)
1014 		dev_err(dev, "failed to enable clock\n");
1015 
1016 	return ret;
1017 }
1018 
1019 static int imx258_power_off(struct device *dev)
1020 {
1021 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1022 	struct imx258 *imx258 = to_imx258(sd);
1023 
1024 	clk_disable_unprepare(imx258->clk);
1025 
1026 	return 0;
1027 }
1028 
1029 static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
1030 {
1031 	struct imx258 *imx258 = to_imx258(sd);
1032 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1033 	int ret = 0;
1034 
1035 	mutex_lock(&imx258->mutex);
1036 	if (imx258->streaming == enable) {
1037 		mutex_unlock(&imx258->mutex);
1038 		return 0;
1039 	}
1040 
1041 	if (enable) {
1042 		ret = pm_runtime_resume_and_get(&client->dev);
1043 		if (ret < 0)
1044 			goto err_unlock;
1045 
1046 		/*
1047 		 * Apply default & customized values
1048 		 * and then start streaming.
1049 		 */
1050 		ret = imx258_start_streaming(imx258);
1051 		if (ret)
1052 			goto err_rpm_put;
1053 	} else {
1054 		imx258_stop_streaming(imx258);
1055 		pm_runtime_put(&client->dev);
1056 	}
1057 
1058 	imx258->streaming = enable;
1059 	mutex_unlock(&imx258->mutex);
1060 
1061 	return ret;
1062 
1063 err_rpm_put:
1064 	pm_runtime_put(&client->dev);
1065 err_unlock:
1066 	mutex_unlock(&imx258->mutex);
1067 
1068 	return ret;
1069 }
1070 
1071 static int __maybe_unused imx258_suspend(struct device *dev)
1072 {
1073 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1074 	struct imx258 *imx258 = to_imx258(sd);
1075 
1076 	if (imx258->streaming)
1077 		imx258_stop_streaming(imx258);
1078 
1079 	return 0;
1080 }
1081 
1082 static int __maybe_unused imx258_resume(struct device *dev)
1083 {
1084 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1085 	struct imx258 *imx258 = to_imx258(sd);
1086 	int ret;
1087 
1088 	if (imx258->streaming) {
1089 		ret = imx258_start_streaming(imx258);
1090 		if (ret)
1091 			goto error;
1092 	}
1093 
1094 	return 0;
1095 
1096 error:
1097 	imx258_stop_streaming(imx258);
1098 	imx258->streaming = 0;
1099 	return ret;
1100 }
1101 
1102 /* Verify chip ID */
1103 static int imx258_identify_module(struct imx258 *imx258)
1104 {
1105 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1106 	int ret;
1107 	u32 val;
1108 
1109 	ret = imx258_read_reg(imx258, IMX258_REG_CHIP_ID,
1110 			      IMX258_REG_VALUE_16BIT, &val);
1111 	if (ret) {
1112 		dev_err(&client->dev, "failed to read chip id %x\n",
1113 			IMX258_CHIP_ID);
1114 		return ret;
1115 	}
1116 
1117 	if (val != IMX258_CHIP_ID) {
1118 		dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1119 			IMX258_CHIP_ID, val);
1120 		return -EIO;
1121 	}
1122 
1123 	return 0;
1124 }
1125 
1126 static const struct v4l2_subdev_video_ops imx258_video_ops = {
1127 	.s_stream = imx258_set_stream,
1128 };
1129 
1130 static const struct v4l2_subdev_pad_ops imx258_pad_ops = {
1131 	.enum_mbus_code = imx258_enum_mbus_code,
1132 	.get_fmt = imx258_get_pad_format,
1133 	.set_fmt = imx258_set_pad_format,
1134 	.enum_frame_size = imx258_enum_frame_size,
1135 };
1136 
1137 static const struct v4l2_subdev_ops imx258_subdev_ops = {
1138 	.video = &imx258_video_ops,
1139 	.pad = &imx258_pad_ops,
1140 };
1141 
1142 static const struct v4l2_subdev_internal_ops imx258_internal_ops = {
1143 	.open = imx258_open,
1144 };
1145 
1146 /* Initialize control handlers */
1147 static int imx258_init_controls(struct imx258 *imx258)
1148 {
1149 	struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1150 	struct v4l2_ctrl_handler *ctrl_hdlr;
1151 	s64 vblank_def;
1152 	s64 vblank_min;
1153 	s64 pixel_rate_min;
1154 	s64 pixel_rate_max;
1155 	int ret;
1156 
1157 	ctrl_hdlr = &imx258->ctrl_handler;
1158 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1159 	if (ret)
1160 		return ret;
1161 
1162 	mutex_init(&imx258->mutex);
1163 	ctrl_hdlr->lock = &imx258->mutex;
1164 	imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1165 				&imx258_ctrl_ops,
1166 				V4L2_CID_LINK_FREQ,
1167 				ARRAY_SIZE(link_freq_menu_items) - 1,
1168 				0,
1169 				link_freq_menu_items);
1170 
1171 	if (imx258->link_freq)
1172 		imx258->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1173 
1174 	pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1175 	pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
1176 	/* By default, PIXEL_RATE is read only */
1177 	imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1178 				V4L2_CID_PIXEL_RATE,
1179 				pixel_rate_min, pixel_rate_max,
1180 				1, pixel_rate_max);
1181 
1182 
1183 	vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
1184 	vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
1185 	imx258->vblank = v4l2_ctrl_new_std(
1186 				ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
1187 				vblank_min,
1188 				IMX258_VTS_MAX - imx258->cur_mode->height, 1,
1189 				vblank_def);
1190 
1191 	if (imx258->vblank)
1192 		imx258->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1193 
1194 	imx258->hblank = v4l2_ctrl_new_std(
1195 				ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
1196 				IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1197 				IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1198 				1,
1199 				IMX258_PPL_DEFAULT - imx258->cur_mode->width);
1200 
1201 	if (imx258->hblank)
1202 		imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1203 
1204 	imx258->exposure = v4l2_ctrl_new_std(
1205 				ctrl_hdlr, &imx258_ctrl_ops,
1206 				V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
1207 				IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
1208 				IMX258_EXPOSURE_DEFAULT);
1209 
1210 	v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1211 				IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
1212 				IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
1213 
1214 	v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1215 				IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
1216 				IMX258_DGTL_GAIN_STEP,
1217 				IMX258_DGTL_GAIN_DEFAULT);
1218 
1219 	v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_WIDE_DYNAMIC_RANGE,
1220 				0, 1, 1, IMX258_HDR_RATIO_DEFAULT);
1221 
1222 	v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
1223 				V4L2_CID_TEST_PATTERN,
1224 				ARRAY_SIZE(imx258_test_pattern_menu) - 1,
1225 				0, 0, imx258_test_pattern_menu);
1226 
1227 	if (ctrl_hdlr->error) {
1228 		ret = ctrl_hdlr->error;
1229 		dev_err(&client->dev, "%s control init failed (%d)\n",
1230 				__func__, ret);
1231 		goto error;
1232 	}
1233 
1234 	imx258->sd.ctrl_handler = ctrl_hdlr;
1235 
1236 	return 0;
1237 
1238 error:
1239 	v4l2_ctrl_handler_free(ctrl_hdlr);
1240 	mutex_destroy(&imx258->mutex);
1241 
1242 	return ret;
1243 }
1244 
1245 static void imx258_free_controls(struct imx258 *imx258)
1246 {
1247 	v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
1248 	mutex_destroy(&imx258->mutex);
1249 }
1250 
1251 static int imx258_probe(struct i2c_client *client)
1252 {
1253 	struct imx258 *imx258;
1254 	int ret;
1255 	u32 val = 0;
1256 
1257 	imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
1258 	if (!imx258)
1259 		return -ENOMEM;
1260 
1261 	imx258->clk = devm_clk_get_optional(&client->dev, NULL);
1262 	if (!imx258->clk) {
1263 		dev_dbg(&client->dev,
1264 			"no clock provided, using clock-frequency property\n");
1265 
1266 		device_property_read_u32(&client->dev, "clock-frequency", &val);
1267 		if (val != IMX258_INPUT_CLOCK_FREQ)
1268 			return -EINVAL;
1269 	} else if (IS_ERR(imx258->clk)) {
1270 		return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
1271 				     "error getting clock\n");
1272 	}
1273 	if (clk_get_rate(imx258->clk) != IMX258_INPUT_CLOCK_FREQ) {
1274 		dev_err(&client->dev, "input clock frequency not supported\n");
1275 		return -EINVAL;
1276 	}
1277 
1278 	/*
1279 	 * Check that the device is mounted upside down. The driver only
1280 	 * supports a single pixel order right now.
1281 	 */
1282 	ret = device_property_read_u32(&client->dev, "rotation", &val);
1283 	if (ret || val != 180)
1284 		return -EINVAL;
1285 
1286 	/* Initialize subdev */
1287 	v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
1288 
1289 	/* Will be powered off via pm_runtime_idle */
1290 	ret = imx258_power_on(&client->dev);
1291 	if (ret)
1292 		return ret;
1293 
1294 	/* Check module identity */
1295 	ret = imx258_identify_module(imx258);
1296 	if (ret)
1297 		goto error_identify;
1298 
1299 	/* Set default mode to max resolution */
1300 	imx258->cur_mode = &supported_modes[0];
1301 
1302 	ret = imx258_init_controls(imx258);
1303 	if (ret)
1304 		goto error_identify;
1305 
1306 	/* Initialize subdev */
1307 	imx258->sd.internal_ops = &imx258_internal_ops;
1308 	imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1309 	imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1310 
1311 	/* Initialize source pad */
1312 	imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
1313 
1314 	ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
1315 	if (ret)
1316 		goto error_handler_free;
1317 
1318 	ret = v4l2_async_register_subdev_sensor(&imx258->sd);
1319 	if (ret < 0)
1320 		goto error_media_entity;
1321 
1322 	pm_runtime_set_active(&client->dev);
1323 	pm_runtime_enable(&client->dev);
1324 	pm_runtime_idle(&client->dev);
1325 
1326 	return 0;
1327 
1328 error_media_entity:
1329 	media_entity_cleanup(&imx258->sd.entity);
1330 
1331 error_handler_free:
1332 	imx258_free_controls(imx258);
1333 
1334 error_identify:
1335 	imx258_power_off(&client->dev);
1336 
1337 	return ret;
1338 }
1339 
1340 static int imx258_remove(struct i2c_client *client)
1341 {
1342 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1343 	struct imx258 *imx258 = to_imx258(sd);
1344 
1345 	v4l2_async_unregister_subdev(sd);
1346 	media_entity_cleanup(&sd->entity);
1347 	imx258_free_controls(imx258);
1348 
1349 	pm_runtime_disable(&client->dev);
1350 	if (!pm_runtime_status_suspended(&client->dev))
1351 		imx258_power_off(&client->dev);
1352 	pm_runtime_set_suspended(&client->dev);
1353 
1354 	return 0;
1355 }
1356 
1357 static const struct dev_pm_ops imx258_pm_ops = {
1358 	SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
1359 	SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
1360 };
1361 
1362 #ifdef CONFIG_ACPI
1363 static const struct acpi_device_id imx258_acpi_ids[] = {
1364 	{ "SONY258A" },
1365 	{ /* sentinel */ }
1366 };
1367 
1368 MODULE_DEVICE_TABLE(acpi, imx258_acpi_ids);
1369 #endif
1370 
1371 static const struct of_device_id imx258_dt_ids[] = {
1372 	{ .compatible = "sony,imx258" },
1373 	{ /* sentinel */ }
1374 };
1375 MODULE_DEVICE_TABLE(of, imx258_dt_ids);
1376 
1377 static struct i2c_driver imx258_i2c_driver = {
1378 	.driver = {
1379 		.name = "imx258",
1380 		.pm = &imx258_pm_ops,
1381 		.acpi_match_table = ACPI_PTR(imx258_acpi_ids),
1382 		.of_match_table	= imx258_dt_ids,
1383 	},
1384 	.probe_new = imx258_probe,
1385 	.remove = imx258_remove,
1386 };
1387 
1388 module_i2c_driver(imx258_i2c_driver);
1389 
1390 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>");
1391 MODULE_AUTHOR("Chiang, Alan");
1392 MODULE_AUTHOR("Chen, Jason");
1393 MODULE_DESCRIPTION("Sony IMX258 sensor driver");
1394 MODULE_LICENSE("GPL v2");
1395