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