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