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