imx708: Do not reset vblank to a default value
[platform/kernel/linux-rpi.git] / drivers / media / i2c / imx708.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A V4L2 driver for Sony IMX708 cameras.
4  * Copyright (C) 2022, Raspberry Pi Ltd
5  *
6  * Based on Sony imx477 camera driver
7  * Copyright (C) 2020 Raspberry Pi Ltd
8  */
9 #include <asm/unaligned.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regulator/consumer.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-mediabus.h>
22
23 #define IMX708_REG_VALUE_08BIT          1
24 #define IMX708_REG_VALUE_16BIT          2
25
26 /* Chip ID */
27 #define IMX708_REG_CHIP_ID              0x0016
28 #define IMX708_CHIP_ID                  0x0708
29
30 #define IMX708_REG_MODE_SELECT          0x0100
31 #define IMX708_MODE_STANDBY             0x00
32 #define IMX708_MODE_STREAMING           0x01
33
34 #define IMX708_REG_ORIENTATION          0x101
35
36 #define IMX708_XCLK_FREQ                24000000
37
38 #define IMX708_DEFAULT_LINK_FREQ        450000000
39
40 /* Default initial pixel rate, will get updated for each mode. */
41 #define IMX708_INITIAL_PIXEL_RATE       590000000
42
43 /* V_TIMING internal */
44 #define IMX708_REG_FRAME_LENGTH         0x0340
45 #define IMX708_FRAME_LENGTH_MAX         0xffff
46
47 /* Exposure control */
48 #define IMX708_REG_EXPOSURE             0x0202
49 #define IMX708_EXPOSURE_OFFSET          48
50 #define IMX708_EXPOSURE_DEFAULT         0x640
51 #define IMX708_EXPOSURE_STEP            1
52 #define IMX708_EXPOSURE_MIN             1
53 #define IMX708_EXPOSURE_MAX             (IMX708_FRAME_LENGTH_MAX - \
54                                          IMX708_EXPOSURE_OFFSET)
55
56 /* Analog gain control */
57 #define IMX708_REG_ANALOG_GAIN          0x0204
58 #define IMX708_ANA_GAIN_MIN             112
59 #define IMX708_ANA_GAIN_MAX             960
60 #define IMX708_ANA_GAIN_STEP            1
61 #define IMX708_ANA_GAIN_DEFAULT    IMX708_ANA_GAIN_MIN
62
63 /* Digital gain control */
64 #define IMX708_REG_DIGITAL_GAIN         0x020e
65 #define IMX708_DGTL_GAIN_MIN            0x0100
66 #define IMX708_DGTL_GAIN_MAX            0xffff
67 #define IMX708_DGTL_GAIN_DEFAULT        0x0100
68 #define IMX708_DGTL_GAIN_STEP           1
69
70 /* Colour balance controls */
71 #define IMX708_REG_COLOUR_BALANCE_RED   0x0b90
72 #define IMX708_REG_COLOUR_BALANCE_BLUE  0x0b92
73 #define IMX708_COLOUR_BALANCE_MIN       0x01
74 #define IMX708_COLOUR_BALANCE_MAX       0xffff
75 #define IMX708_COLOUR_BALANCE_STEP      0x01
76 #define IMX708_COLOUR_BALANCE_DEFAULT   0x100
77
78 /* Test Pattern Control */
79 #define IMX708_REG_TEST_PATTERN         0x0600
80 #define IMX708_TEST_PATTERN_DISABLE     0
81 #define IMX708_TEST_PATTERN_SOLID_COLOR 1
82 #define IMX708_TEST_PATTERN_COLOR_BARS  2
83 #define IMX708_TEST_PATTERN_GREY_COLOR  3
84 #define IMX708_TEST_PATTERN_PN9         4
85
86 /* Test pattern colour components */
87 #define IMX708_REG_TEST_PATTERN_R       0x0602
88 #define IMX708_REG_TEST_PATTERN_GR      0x0604
89 #define IMX708_REG_TEST_PATTERN_B       0x0606
90 #define IMX708_REG_TEST_PATTERN_GB      0x0608
91 #define IMX708_TEST_PATTERN_COLOUR_MIN  0
92 #define IMX708_TEST_PATTERN_COLOUR_MAX  0x0fff
93 #define IMX708_TEST_PATTERN_COLOUR_STEP 1
94
95 #define IMX708_REG_BASE_SPC_GAINS_L     0x7b10
96 #define IMX708_REG_BASE_SPC_GAINS_R     0x7c00
97
98 /* HDR exposure ratio (long:med == med:short) */
99 #define IMX708_HDR_EXPOSURE_RATIO       4
100 #define IMX708_REG_MID_EXPOSURE 0x3116
101 #define IMX708_REG_SHT_EXPOSURE 0x0224
102 #define IMX708_REG_MID_ANALOG_GAIN      0x3118
103 #define IMX708_REG_SHT_ANALOG_GAIN      0x0216
104
105 /*
106  * Metadata buffer holds a variety of data, all sent with the same VC/DT (0x12).
107  * It comprises two scanlines (of up to 5760 bytes each, for 4608 pixels)
108  * of embedded data, one line of PDAF data, and two lines of AE-HIST data
109  * (AE histograms are valid for HDR mode and empty in non-HDR modes).
110  */
111 #define IMX708_EMBEDDED_LINE_WIDTH (5 * 5760)
112 #define IMX708_NUM_EMBEDDED_LINES 1
113
114 enum pad_types {
115         IMAGE_PAD,
116         METADATA_PAD,
117         NUM_PADS
118 };
119
120 /* IMX708 native and active pixel array size. */
121 #define IMX708_NATIVE_WIDTH             4640U
122 #define IMX708_NATIVE_HEIGHT            2658U
123 #define IMX708_PIXEL_ARRAY_LEFT         16U
124 #define IMX708_PIXEL_ARRAY_TOP          24U
125 #define IMX708_PIXEL_ARRAY_WIDTH        4608U
126 #define IMX708_PIXEL_ARRAY_HEIGHT       2592U
127
128 struct imx708_reg {
129         u16 address;
130         u8 val;
131 };
132
133 struct imx708_reg_list {
134         unsigned int num_of_regs;
135         const struct imx708_reg *regs;
136 };
137
138 /* Mode : resolution and related config&values */
139 struct imx708_mode {
140         /* Frame width */
141         unsigned int width;
142
143         /* Frame height */
144         unsigned int height;
145
146         /* H-timing in pixels */
147         unsigned int line_length_pix;
148
149         /* Analog crop rectangle. */
150         struct v4l2_rect crop;
151
152         /* Highest possible framerate. */
153         unsigned int vblank_min;
154
155         /* Default framerate. */
156         unsigned int vblank_default;
157
158         /* Default register values */
159         struct imx708_reg_list reg_list;
160
161         /* Not all modes have the same pixel rate. */
162         u64 pixel_rate;
163
164         /* Not all modes have the same minimum exposure. */
165         u32 exposure_lines_min;
166
167         /* Not all modes have the same exposure lines step. */
168         u32 exposure_lines_step;
169
170         /* HDR flag, currently not used at runtime */
171         bool hdr;
172 };
173
174 /* Default PDAF pixel correction gains */
175 static const u8 pdaf_gains[2][9] = {
176         { 0x4c, 0x4c, 0x4c, 0x46, 0x3e, 0x38, 0x35, 0x35, 0x35 },
177         { 0x35, 0x35, 0x35, 0x38, 0x3e, 0x46, 0x4c, 0x4c, 0x4c }
178 };
179
180 static const struct imx708_reg mode_common_regs[] = {
181         {0x0100, 0x00},
182         {0x0136, 0x18},
183         {0x0137, 0x00},
184         {0x33F0, 0x02},
185         {0x33F1, 0x05},
186         {0x3062, 0x00},
187         {0x3063, 0x12},
188         {0x3068, 0x00},
189         {0x3069, 0x12},
190         {0x306A, 0x00},
191         {0x306B, 0x30},
192         {0x3076, 0x00},
193         {0x3077, 0x30},
194         {0x3078, 0x00},
195         {0x3079, 0x30},
196         {0x5E54, 0x0C},
197         {0x6E44, 0x00},
198         {0xB0B6, 0x01},
199         {0xE829, 0x00},
200         {0xF001, 0x08},
201         {0xF003, 0x08},
202         {0xF00D, 0x10},
203         {0xF00F, 0x10},
204         {0xF031, 0x08},
205         {0xF033, 0x08},
206         {0xF03D, 0x10},
207         {0xF03F, 0x10},
208         {0x0112, 0x0A},
209         {0x0113, 0x0A},
210         {0x0114, 0x01},
211         {0x0B8E, 0x01},
212         {0x0B8F, 0x00},
213         {0x0B94, 0x01},
214         {0x0B95, 0x00},
215         {0x3400, 0x01},
216         {0x3478, 0x01},
217         {0x3479, 0x1c},
218         {0x3091, 0x01},
219         {0x3092, 0x00},
220         {0x3419, 0x00},
221         {0xBCF1, 0x02},
222         {0x3094, 0x01},
223         {0x3095, 0x01},
224         {0x3362, 0x00},
225         {0x3363, 0x00},
226         {0x3364, 0x00},
227         {0x3365, 0x00},
228         {0x0138, 0x01},
229 };
230
231 /* 10-bit. */
232 static const struct imx708_reg mode_4608x2592_regs[] = {
233         {0x0342, 0x3D},
234         {0x0343, 0x20},
235         {0x0340, 0x0A},
236         {0x0341, 0x59},
237         {0x0344, 0x00},
238         {0x0345, 0x00},
239         {0x0346, 0x00},
240         {0x0347, 0x00},
241         {0x0348, 0x11},
242         {0x0349, 0xFF},
243         {0x034A, 0X0A},
244         {0x034B, 0x1F},
245         {0x0220, 0x62},
246         {0x0222, 0x01},
247         {0x0900, 0x00},
248         {0x0901, 0x11},
249         {0x0902, 0x0A},
250         {0x3200, 0x01},
251         {0x3201, 0x01},
252         {0x32D5, 0x01},
253         {0x32D6, 0x00},
254         {0x32DB, 0x01},
255         {0x32DF, 0x00},
256         {0x350C, 0x00},
257         {0x350D, 0x00},
258         {0x0408, 0x00},
259         {0x0409, 0x00},
260         {0x040A, 0x00},
261         {0x040B, 0x00},
262         {0x040C, 0x12},
263         {0x040D, 0x00},
264         {0x040E, 0x0A},
265         {0x040F, 0x20},
266         {0x034C, 0x12},
267         {0x034D, 0x00},
268         {0x034E, 0x0A},
269         {0x034F, 0x20},
270         {0x0301, 0x05},
271         {0x0303, 0x02},
272         {0x0305, 0x02},
273         {0x0306, 0x00},
274         {0x0307, 0x7C},
275         {0x030B, 0x02},
276         {0x030D, 0x04},
277         {0x030E, 0x01},
278         {0x030F, 0x2C},
279         {0x0310, 0x01},
280         {0x3CA0, 0x00},
281         {0x3CA1, 0x64},
282         {0x3CA4, 0x00},
283         {0x3CA5, 0x00},
284         {0x3CA6, 0x00},
285         {0x3CA7, 0x00},
286         {0x3CAA, 0x00},
287         {0x3CAB, 0x00},
288         {0x3CB8, 0x00},
289         {0x3CB9, 0x08},
290         {0x3CBA, 0x00},
291         {0x3CBB, 0x00},
292         {0x3CBC, 0x00},
293         {0x3CBD, 0x3C},
294         {0x3CBE, 0x00},
295         {0x3CBF, 0x00},
296         {0x0202, 0x0A},
297         {0x0203, 0x29},
298         {0x0224, 0x01},
299         {0x0225, 0xF4},
300         {0x3116, 0x01},
301         {0x3117, 0xF4},
302         {0x0204, 0x00},
303         {0x0205, 0x00},
304         {0x0216, 0x00},
305         {0x0217, 0x00},
306         {0x0218, 0x01},
307         {0x0219, 0x00},
308         {0x020E, 0x01},
309         {0x020F, 0x00},
310         {0x3118, 0x00},
311         {0x3119, 0x00},
312         {0x311A, 0x01},
313         {0x311B, 0x00},
314         {0x341a, 0x00},
315         {0x341b, 0x00},
316         {0x341c, 0x00},
317         {0x341d, 0x00},
318         {0x341e, 0x01},
319         {0x341f, 0x20},
320         {0x3420, 0x00},
321         {0x3421, 0xd8},
322         {0xC428, 0x00},
323         {0xC429, 0x04},
324         {0x3366, 0x00},
325         {0x3367, 0x00},
326         {0x3368, 0x00},
327         {0x3369, 0x00},
328 };
329
330 static const struct imx708_reg mode_2x2binned_regs[] = {
331         {0x0342, 0x1E},
332         {0x0343, 0x90},
333         {0x0340, 0x05},
334         {0x0341, 0x38},
335         {0x0344, 0x00},
336         {0x0345, 0x00},
337         {0x0346, 0x00},
338         {0x0347, 0x00},
339         {0x0348, 0x11},
340         {0x0349, 0xFF},
341         {0x034A, 0X0A},
342         {0x034B, 0x1F},
343         {0x0220, 0x62},
344         {0x0222, 0x01},
345         {0x0900, 0x01},
346         {0x0901, 0x22},
347         {0x0902, 0x08},
348         {0x3200, 0x41},
349         {0x3201, 0x41},
350         {0x32D5, 0x00},
351         {0x32D6, 0x00},
352         {0x32DB, 0x01},
353         {0x32DF, 0x00},
354         {0x350C, 0x00},
355         {0x350D, 0x00},
356         {0x0408, 0x00},
357         {0x0409, 0x00},
358         {0x040A, 0x00},
359         {0x040B, 0x00},
360         {0x040C, 0x09},
361         {0x040D, 0x00},
362         {0x040E, 0x05},
363         {0x040F, 0x10},
364         {0x034C, 0x09},
365         {0x034D, 0x00},
366         {0x034E, 0x05},
367         {0x034F, 0x10},
368         {0x0301, 0x05},
369         {0x0303, 0x02},
370         {0x0305, 0x02},
371         {0x0306, 0x00},
372         {0x0307, 0x7A},
373         {0x030B, 0x02},
374         {0x030D, 0x04},
375         {0x030E, 0x01},
376         {0x030F, 0x2C},
377         {0x0310, 0x01},
378         {0x3CA0, 0x00},
379         {0x3CA1, 0x3C},
380         {0x3CA4, 0x00},
381         {0x3CA5, 0x3C},
382         {0x3CA6, 0x00},
383         {0x3CA7, 0x00},
384         {0x3CAA, 0x00},
385         {0x3CAB, 0x00},
386         {0x3CB8, 0x00},
387         {0x3CB9, 0x1C},
388         {0x3CBA, 0x00},
389         {0x3CBB, 0x08},
390         {0x3CBC, 0x00},
391         {0x3CBD, 0x1E},
392         {0x3CBE, 0x00},
393         {0x3CBF, 0x0A},
394         {0x0202, 0x05},
395         {0x0203, 0x08},
396         {0x0224, 0x01},
397         {0x0225, 0xF4},
398         {0x3116, 0x01},
399         {0x3117, 0xF4},
400         {0x0204, 0x00},
401         {0x0205, 0x70},
402         {0x0216, 0x00},
403         {0x0217, 0x70},
404         {0x0218, 0x01},
405         {0x0219, 0x00},
406         {0x020E, 0x01},
407         {0x020F, 0x00},
408         {0x3118, 0x00},
409         {0x3119, 0x70},
410         {0x311A, 0x01},
411         {0x311B, 0x00},
412         {0x341a, 0x00},
413         {0x341b, 0x00},
414         {0x341c, 0x00},
415         {0x341d, 0x00},
416         {0x341e, 0x00},
417         {0x341f, 0x90},
418         {0x3420, 0x00},
419         {0x3421, 0x6c},
420         {0x3366, 0x00},
421         {0x3367, 0x00},
422         {0x3368, 0x00},
423         {0x3369, 0x00},
424 };
425
426 static const struct imx708_reg mode_2x2binned_720p_regs[] = {
427         {0x0342, 0x14},
428         {0x0343, 0x60},
429         {0x0340, 0x04},
430         {0x0341, 0xB6},
431         {0x0344, 0x03},
432         {0x0345, 0x00},
433         {0x0346, 0x01},
434         {0x0347, 0xB0},
435         {0x0348, 0x0E},
436         {0x0349, 0xFF},
437         {0x034A, 0x08},
438         {0x034B, 0x6F},
439         {0x0220, 0x62},
440         {0x0222, 0x01},
441         {0x0900, 0x01},
442         {0x0901, 0x22},
443         {0x0902, 0x08},
444         {0x3200, 0x41},
445         {0x3201, 0x41},
446         {0x32D5, 0x00},
447         {0x32D6, 0x00},
448         {0x32DB, 0x01},
449         {0x32DF, 0x01},
450         {0x350C, 0x00},
451         {0x350D, 0x00},
452         {0x0408, 0x00},
453         {0x0409, 0x00},
454         {0x040A, 0x00},
455         {0x040B, 0x00},
456         {0x040C, 0x06},
457         {0x040D, 0x00},
458         {0x040E, 0x03},
459         {0x040F, 0x60},
460         {0x034C, 0x06},
461         {0x034D, 0x00},
462         {0x034E, 0x03},
463         {0x034F, 0x60},
464         {0x0301, 0x05},
465         {0x0303, 0x02},
466         {0x0305, 0x02},
467         {0x0306, 0x00},
468         {0x0307, 0x76},
469         {0x030B, 0x02},
470         {0x030D, 0x04},
471         {0x030E, 0x01},
472         {0x030F, 0x2C},
473         {0x0310, 0x01},
474         {0x3CA0, 0x00},
475         {0x3CA1, 0x3C},
476         {0x3CA4, 0x01},
477         {0x3CA5, 0x5E},
478         {0x3CA6, 0x00},
479         {0x3CA7, 0x00},
480         {0x3CAA, 0x00},
481         {0x3CAB, 0x00},
482         {0x3CB8, 0x00},
483         {0x3CB9, 0x0C},
484         {0x3CBA, 0x00},
485         {0x3CBB, 0x04},
486         {0x3CBC, 0x00},
487         {0x3CBD, 0x1E},
488         {0x3CBE, 0x00},
489         {0x3CBF, 0x05},
490         {0x0202, 0x04},
491         {0x0203, 0x86},
492         {0x0224, 0x01},
493         {0x0225, 0xF4},
494         {0x3116, 0x01},
495         {0x3117, 0xF4},
496         {0x0204, 0x00},
497         {0x0205, 0x70},
498         {0x0216, 0x00},
499         {0x0217, 0x70},
500         {0x0218, 0x01},
501         {0x0219, 0x00},
502         {0x020E, 0x01},
503         {0x020F, 0x00},
504         {0x3118, 0x00},
505         {0x3119, 0x70},
506         {0x311A, 0x01},
507         {0x311B, 0x00},
508         {0x341a, 0x00},
509         {0x341b, 0x00},
510         {0x341c, 0x00},
511         {0x341d, 0x00},
512         {0x341e, 0x00},
513         {0x341f, 0x60},
514         {0x3420, 0x00},
515         {0x3421, 0x48},
516         {0x3366, 0x00},
517         {0x3367, 0x00},
518         {0x3368, 0x00},
519         {0x3369, 0x00},
520 };
521
522 static const struct imx708_reg mode_hdr_regs[] = {
523         {0x0342, 0x14},
524         {0x0343, 0x60},
525         {0x0340, 0x0A},
526         {0x0341, 0x5B},
527         {0x0344, 0x00},
528         {0x0345, 0x00},
529         {0x0346, 0x00},
530         {0x0347, 0x00},
531         {0x0348, 0x11},
532         {0x0349, 0xFF},
533         {0x034A, 0X0A},
534         {0x034B, 0x1F},
535         {0x0220, 0x01},
536         {0x0222, IMX708_HDR_EXPOSURE_RATIO},
537         {0x0900, 0x00},
538         {0x0901, 0x11},
539         {0x0902, 0x0A},
540         {0x3200, 0x01},
541         {0x3201, 0x01},
542         {0x32D5, 0x00},
543         {0x32D6, 0x00},
544         {0x32DB, 0x01},
545         {0x32DF, 0x00},
546         {0x350C, 0x00},
547         {0x350D, 0x00},
548         {0x0408, 0x00},
549         {0x0409, 0x00},
550         {0x040A, 0x00},
551         {0x040B, 0x00},
552         {0x040C, 0x09},
553         {0x040D, 0x00},
554         {0x040E, 0x05},
555         {0x040F, 0x10},
556         {0x034C, 0x09},
557         {0x034D, 0x00},
558         {0x034E, 0x05},
559         {0x034F, 0x10},
560         {0x0301, 0x05},
561         {0x0303, 0x02},
562         {0x0305, 0x02},
563         {0x0306, 0x00},
564         {0x0307, 0xA2},
565         {0x030B, 0x02},
566         {0x030D, 0x04},
567         {0x030E, 0x01},
568         {0x030F, 0x2C},
569         {0x0310, 0x01},
570         {0x3CA0, 0x00},
571         {0x3CA1, 0x00},
572         {0x3CA4, 0x00},
573         {0x3CA5, 0x00},
574         {0x3CA6, 0x00},
575         {0x3CA7, 0x28},
576         {0x3CAA, 0x00},
577         {0x3CAB, 0x00},
578         {0x3CB8, 0x00},
579         {0x3CB9, 0x30},
580         {0x3CBA, 0x00},
581         {0x3CBB, 0x00},
582         {0x3CBC, 0x00},
583         {0x3CBD, 0x32},
584         {0x3CBE, 0x00},
585         {0x3CBF, 0x00},
586         {0x0202, 0x0A},
587         {0x0203, 0x2B},
588         {0x0224, 0x0A},
589         {0x0225, 0x2B},
590         {0x3116, 0x0A},
591         {0x3117, 0x2B},
592         {0x0204, 0x00},
593         {0x0205, 0x00},
594         {0x0216, 0x00},
595         {0x0217, 0x00},
596         {0x0218, 0x01},
597         {0x0219, 0x00},
598         {0x020E, 0x01},
599         {0x020F, 0x00},
600         {0x3118, 0x00},
601         {0x3119, 0x00},
602         {0x311A, 0x01},
603         {0x311B, 0x00},
604         {0x341a, 0x00},
605         {0x341b, 0x00},
606         {0x341c, 0x00},
607         {0x341d, 0x00},
608         {0x341e, 0x00},
609         {0x341f, 0x90},
610         {0x3420, 0x00},
611         {0x3421, 0x6c},
612         {0x3360, 0x01},
613         {0x3361, 0x01},
614         {0x3366, 0x09},
615         {0x3367, 0x00},
616         {0x3368, 0x05},
617         {0x3369, 0x10},
618 };
619
620 /* Mode configs. Keep separate lists for when HDR is enabled or not. */
621 static const struct imx708_mode supported_modes_10bit_no_hdr[] = {
622         {
623                 /* Full resolution. */
624                 .width = 4608,
625                 .height = 2592,
626                 .line_length_pix = 0x3d20,
627                 .crop = {
628                         .left = IMX708_PIXEL_ARRAY_LEFT,
629                         .top = IMX708_PIXEL_ARRAY_TOP,
630                         .width = 4608,
631                         .height = 2592,
632                 },
633                 .vblank_min = 58,
634                 .vblank_default = 58,
635                 .reg_list = {
636                         .num_of_regs = ARRAY_SIZE(mode_4608x2592_regs),
637                         .regs = mode_4608x2592_regs,
638                 },
639                 .pixel_rate = 595200000,
640                 .exposure_lines_min = 8,
641                 .exposure_lines_step = 1,
642                 .hdr = false
643         },
644         {
645                 /* regular 2x2 binned. */
646                 .width = 2304,
647                 .height = 1296,
648                 .line_length_pix = 0x1e90,
649                 .crop = {
650                         .left = IMX708_PIXEL_ARRAY_LEFT,
651                         .top = IMX708_PIXEL_ARRAY_TOP,
652                         .width = 4608,
653                         .height = 2592,
654                 },
655                 .vblank_min = 40,
656                 .vblank_default = 1198,
657                 .reg_list = {
658                         .num_of_regs = ARRAY_SIZE(mode_2x2binned_regs),
659                         .regs = mode_2x2binned_regs,
660                 },
661                 .pixel_rate = 585600000,
662                 .exposure_lines_min = 4,
663                 .exposure_lines_step = 2,
664                 .hdr = false
665         },
666         {
667                 /* 2x2 binned and cropped for 720p. */
668                 .width = 1536,
669                 .height = 864,
670                 .line_length_pix = 0x1460,
671                 .crop = {
672                         .left = IMX708_PIXEL_ARRAY_LEFT,
673                         .top = IMX708_PIXEL_ARRAY_TOP,
674                         .width = 4608,
675                         .height = 2592,
676                 },
677                 .vblank_min = 40,
678                 .vblank_default = 2755,
679                 .reg_list = {
680                         .num_of_regs = ARRAY_SIZE(mode_2x2binned_720p_regs),
681                         .regs = mode_2x2binned_720p_regs,
682                 },
683                 .pixel_rate = 566400000,
684                 .exposure_lines_min = 4,
685                 .exposure_lines_step = 2,
686                 .hdr = false
687         },
688 };
689
690 static const struct imx708_mode supported_modes_10bit_hdr[] = {
691         {
692                 /* There's only one HDR mode, which is 2x2 downscaled */
693                 .width = 2304,
694                 .height = 1296,
695                 .line_length_pix = 0x1460,
696                 .crop = {
697                         .left = IMX708_PIXEL_ARRAY_LEFT,
698                         .top = IMX708_PIXEL_ARRAY_TOP,
699                         .width = 4608,
700                         .height = 2592,
701                 },
702                 .vblank_min = 3673,
703                 .vblank_default = 3673,
704                 .reg_list = {
705                         .num_of_regs = ARRAY_SIZE(mode_hdr_regs),
706                         .regs = mode_hdr_regs,
707                 },
708                 .pixel_rate = 777600000,
709                 .exposure_lines_min = 8 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO,
710                 .exposure_lines_step = 2 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO,
711                 .hdr = true
712         }
713 };
714
715 /*
716  * The supported formats.
717  * This table MUST contain 4 entries per format, to cover the various flip
718  * combinations in the order
719  * - no flip
720  * - h flip
721  * - v flip
722  * - h&v flips
723  */
724 static const u32 codes[] = {
725         /* 10-bit modes. */
726         MEDIA_BUS_FMT_SRGGB10_1X10,
727         MEDIA_BUS_FMT_SGRBG10_1X10,
728         MEDIA_BUS_FMT_SGBRG10_1X10,
729         MEDIA_BUS_FMT_SBGGR10_1X10,
730 };
731
732 static const char * const imx708_test_pattern_menu[] = {
733         "Disabled",
734         "Color Bars",
735         "Solid Color",
736         "Grey Color Bars",
737         "PN9"
738 };
739
740 static const int imx708_test_pattern_val[] = {
741         IMX708_TEST_PATTERN_DISABLE,
742         IMX708_TEST_PATTERN_COLOR_BARS,
743         IMX708_TEST_PATTERN_SOLID_COLOR,
744         IMX708_TEST_PATTERN_GREY_COLOR,
745         IMX708_TEST_PATTERN_PN9,
746 };
747
748 /* regulator supplies */
749 static const char * const imx708_supply_name[] = {
750         /* Supplies can be enabled in any order */
751         "VANA1",  /* Analog1 (2.8V) supply */
752         "VANA2",  /* Analog2 (1.8V) supply */
753         "VDIG",  /* Digital Core (1.1V) supply */
754         "VDDL",  /* IF (1.8V) supply */
755 };
756
757 #define IMX708_NUM_SUPPLIES ARRAY_SIZE(imx708_supply_name)
758
759 /*
760  * Initialisation delay between XCLR low->high and the moment when the sensor
761  * can start capture (i.e. can leave software standby), given by T7 in the
762  * datasheet is 8ms.  This does include I2C setup time as well.
763  *
764  * Note, that delay between XCLR low->high and reading the CCI ID register (T6
765  * in the datasheet) is much smaller - 600us.
766  */
767 #define IMX708_XCLR_MIN_DELAY_US        8000
768 #define IMX708_XCLR_DELAY_RANGE_US      1000
769
770 struct imx708 {
771         struct v4l2_subdev sd;
772         struct media_pad pad[NUM_PADS];
773
774         struct v4l2_mbus_framefmt fmt;
775
776         struct clk *xclk;
777         u32 xclk_freq;
778
779         struct gpio_desc *reset_gpio;
780         struct regulator_bulk_data supplies[IMX708_NUM_SUPPLIES];
781
782         struct v4l2_ctrl_handler ctrl_handler;
783         /* V4L2 Controls */
784         struct v4l2_ctrl *pixel_rate;
785         struct v4l2_ctrl *exposure;
786         struct v4l2_ctrl *vflip;
787         struct v4l2_ctrl *hflip;
788         struct v4l2_ctrl *vblank;
789         struct v4l2_ctrl *hblank;
790         struct v4l2_ctrl *red_balance;
791         struct v4l2_ctrl *blue_balance;
792         struct v4l2_ctrl *notify_gains;
793         struct v4l2_ctrl *hdr_mode;
794
795         /* Current mode */
796         const struct imx708_mode *mode;
797
798         /*
799          * Mutex for serialized access:
800          * Protect sensor module set pad format and start/stop streaming safely.
801          */
802         struct mutex mutex;
803
804         /* Streaming on/off */
805         bool streaming;
806
807         /* Rewrite common registers on stream on? */
808         bool common_regs_written;
809 };
810
811 static inline struct imx708 *to_imx708(struct v4l2_subdev *_sd)
812 {
813         return container_of(_sd, struct imx708, sd);
814 }
815
816 static inline void get_mode_table(unsigned int code,
817                                   const struct imx708_mode **mode_list,
818                                   unsigned int *num_modes,
819                                   bool hdr_enable)
820 {
821         switch (code) {
822         /* 10-bit */
823         case MEDIA_BUS_FMT_SRGGB10_1X10:
824         case MEDIA_BUS_FMT_SGRBG10_1X10:
825         case MEDIA_BUS_FMT_SGBRG10_1X10:
826         case MEDIA_BUS_FMT_SBGGR10_1X10:
827                 if (hdr_enable) {
828                         *mode_list = supported_modes_10bit_hdr;
829                         *num_modes = ARRAY_SIZE(supported_modes_10bit_hdr);
830                 } else {
831                         *mode_list = supported_modes_10bit_no_hdr;
832                         *num_modes = ARRAY_SIZE(supported_modes_10bit_no_hdr);
833                 }
834                 break;
835         default:
836                 *mode_list = NULL;
837                 *num_modes = 0;
838         }
839 }
840
841 /* Read registers up to 2 at a time */
842 static int imx708_read_reg(struct imx708 *imx708, u16 reg, u32 len, u32 *val)
843 {
844         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
845         struct i2c_msg msgs[2];
846         u8 addr_buf[2] = { reg >> 8, reg & 0xff };
847         u8 data_buf[4] = { 0, };
848         int ret;
849
850         if (len > 4)
851                 return -EINVAL;
852
853         /* Write register address */
854         msgs[0].addr = client->addr;
855         msgs[0].flags = 0;
856         msgs[0].len = ARRAY_SIZE(addr_buf);
857         msgs[0].buf = addr_buf;
858
859         /* Read data from register */
860         msgs[1].addr = client->addr;
861         msgs[1].flags = I2C_M_RD;
862         msgs[1].len = len;
863         msgs[1].buf = &data_buf[4 - len];
864
865         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
866         if (ret != ARRAY_SIZE(msgs))
867                 return -EIO;
868
869         *val = get_unaligned_be32(data_buf);
870
871         return 0;
872 }
873
874 /* Write registers up to 2 at a time */
875 static int imx708_write_reg(struct imx708 *imx708, u16 reg, u32 len, u32 val)
876 {
877         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
878         u8 buf[6];
879
880         if (len > 4)
881                 return -EINVAL;
882
883         put_unaligned_be16(reg, buf);
884         put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
885         if (i2c_master_send(client, buf, len + 2) != len + 2)
886                 return -EIO;
887
888         return 0;
889 }
890
891 /* Write a list of registers */
892 static int imx708_write_regs(struct imx708 *imx708,
893                              const struct imx708_reg *regs, u32 len)
894 {
895         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
896         unsigned int i;
897         int ret;
898
899         for (i = 0; i < len; i++) {
900                 ret = imx708_write_reg(imx708, regs[i].address, 1, regs[i].val);
901                 if (ret) {
902                         dev_err_ratelimited(&client->dev,
903                                             "Failed to write reg 0x%4.4x. error = %d\n",
904                                             regs[i].address, ret);
905
906                         return ret;
907                 }
908         }
909
910         return 0;
911 }
912
913 /* Get bayer order based on flip setting. */
914 static u32 imx708_get_format_code(struct imx708 *imx708)
915 {
916         unsigned int i;
917
918         lockdep_assert_held(&imx708->mutex);
919
920         i = (imx708->vflip->val ? 2 : 0) |
921             (imx708->hflip->val ? 1 : 0);
922
923         return codes[i];
924 }
925
926 static void imx708_set_default_format(struct imx708 *imx708)
927 {
928         struct v4l2_mbus_framefmt *fmt = &imx708->fmt;
929
930         /* Set default mode to max resolution */
931         imx708->mode = &supported_modes_10bit_no_hdr[0];
932
933         /* fmt->code not set as it will always be computed based on flips */
934         fmt->colorspace = V4L2_COLORSPACE_RAW;
935         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
936         fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
937                                                           fmt->colorspace,
938                                                           fmt->ycbcr_enc);
939         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
940         fmt->width = imx708->mode->width;
941         fmt->height = imx708->mode->height;
942         fmt->field = V4L2_FIELD_NONE;
943 }
944
945 static int imx708_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
946 {
947         struct imx708 *imx708 = to_imx708(sd);
948         struct v4l2_mbus_framefmt *try_fmt_img =
949                 v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
950         struct v4l2_mbus_framefmt *try_fmt_meta =
951                 v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
952         struct v4l2_rect *try_crop;
953
954         mutex_lock(&imx708->mutex);
955
956         /* Initialize try_fmt for the image pad */
957         if (imx708->hdr_mode->val) {
958                 try_fmt_img->width = supported_modes_10bit_hdr[0].width;
959                 try_fmt_img->height = supported_modes_10bit_hdr[0].height;
960         } else {
961                 try_fmt_img->width = supported_modes_10bit_no_hdr[0].width;
962                 try_fmt_img->height = supported_modes_10bit_no_hdr[0].height;
963         }
964         try_fmt_img->code = imx708_get_format_code(imx708);
965         try_fmt_img->field = V4L2_FIELD_NONE;
966
967         /* Initialize try_fmt for the embedded metadata pad */
968         try_fmt_meta->width = IMX708_EMBEDDED_LINE_WIDTH;
969         try_fmt_meta->height = IMX708_NUM_EMBEDDED_LINES;
970         try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
971         try_fmt_meta->field = V4L2_FIELD_NONE;
972
973         /* Initialize try_crop */
974         try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
975         try_crop->left = IMX708_PIXEL_ARRAY_LEFT;
976         try_crop->top = IMX708_PIXEL_ARRAY_TOP;
977         try_crop->width = IMX708_PIXEL_ARRAY_WIDTH;
978         try_crop->height = IMX708_PIXEL_ARRAY_HEIGHT;
979
980         mutex_unlock(&imx708->mutex);
981
982         return 0;
983 }
984
985 static int imx708_set_exposure(struct imx708 *imx708, unsigned int val)
986 {
987         int ret;
988
989         val = max(val, imx708->mode->exposure_lines_min);
990         val -= val % imx708->mode->exposure_lines_step;
991
992         /*
993          * In HDR mode this will set the longest exposure. The sensor
994          * will automatically divide the medium and short ones by 4,16.
995          */
996         ret = imx708_write_reg(imx708, IMX708_REG_EXPOSURE,
997                                IMX708_REG_VALUE_16BIT, val);
998
999         return ret;
1000 }
1001
1002 static void imx708_adjust_exposure_range(struct imx708 *imx708,
1003                                          struct v4l2_ctrl *ctrl)
1004 {
1005         int exposure_max, exposure_def;
1006
1007         /* Honour the VBLANK limits when setting exposure. */
1008         exposure_max = imx708->mode->height + imx708->vblank->val -
1009                 IMX708_EXPOSURE_OFFSET;
1010         exposure_def = min(exposure_max, imx708->exposure->val);
1011         __v4l2_ctrl_modify_range(imx708->exposure, imx708->exposure->minimum,
1012                                  exposure_max, imx708->exposure->step,
1013                                  exposure_def);
1014 }
1015
1016 static int imx708_set_analogue_gain(struct imx708 *imx708, unsigned int val)
1017 {
1018         int ret;
1019
1020         /*
1021          * In HDR mode this will set the gain for the longest exposure,
1022          * and by default the sensor uses the same gain for all of them.
1023          */
1024         ret = imx708_write_reg(imx708, IMX708_REG_ANALOG_GAIN,
1025                                IMX708_REG_VALUE_16BIT, val);
1026
1027         return ret;
1028 }
1029
1030 static void imx708_set_framing_limits(struct imx708 *imx708)
1031 {
1032         unsigned int hblank;
1033         const struct imx708_mode *mode = imx708->mode;
1034
1035         __v4l2_ctrl_modify_range(imx708->pixel_rate,
1036                                  mode->pixel_rate, mode->pixel_rate,
1037                                  1, mode->pixel_rate);
1038
1039         /* Update limits and set FPS to default */
1040         __v4l2_ctrl_modify_range(imx708->vblank, mode->vblank_min,
1041                                  IMX708_FRAME_LENGTH_MAX - mode->height,
1042                                  1, mode->vblank_default);
1043
1044         /*
1045          * Currently PPL is fixed to the mode specified value, so hblank
1046          * depends on mode->width only, and is not changeable in any
1047          * way other than changing the mode.
1048          */
1049         hblank = mode->line_length_pix - mode->width;
1050         __v4l2_ctrl_modify_range(imx708->hblank, hblank, hblank, 1, hblank);
1051 }
1052
1053 static int imx708_set_ctrl(struct v4l2_ctrl *ctrl)
1054 {
1055         struct imx708 *imx708 =
1056                 container_of(ctrl->handler, struct imx708, ctrl_handler);
1057         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1058         const struct imx708_mode *mode_list;
1059         unsigned int code, num_modes;
1060         int ret = 0;
1061
1062         /*
1063          * The VBLANK control may change the limits of usable exposure, so check
1064          * and adjust if necessary.
1065          */
1066         if (ctrl->id == V4L2_CID_VBLANK)
1067                 imx708_adjust_exposure_range(imx708, ctrl);
1068
1069         /*
1070          * Applying V4L2 control value only happens
1071          * when power is up for streaming
1072          */
1073         if (pm_runtime_get_if_in_use(&client->dev) == 0)
1074                 return 0;
1075
1076         switch (ctrl->id) {
1077         case V4L2_CID_ANALOGUE_GAIN:
1078                 imx708_set_analogue_gain(imx708, ctrl->val);
1079                 break;
1080         case V4L2_CID_EXPOSURE:
1081                 ret = imx708_set_exposure(imx708, ctrl->val);
1082                 break;
1083         case V4L2_CID_DIGITAL_GAIN:
1084                 ret = imx708_write_reg(imx708, IMX708_REG_DIGITAL_GAIN,
1085                                        IMX708_REG_VALUE_16BIT, ctrl->val);
1086                 break;
1087         case V4L2_CID_TEST_PATTERN:
1088                 ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN,
1089                                        IMX708_REG_VALUE_16BIT,
1090                                        imx708_test_pattern_val[ctrl->val]);
1091                 break;
1092         case V4L2_CID_TEST_PATTERN_RED:
1093                 ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_R,
1094                                        IMX708_REG_VALUE_16BIT, ctrl->val);
1095                 break;
1096         case V4L2_CID_TEST_PATTERN_GREENR:
1097                 ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GR,
1098                                        IMX708_REG_VALUE_16BIT, ctrl->val);
1099                 break;
1100         case V4L2_CID_TEST_PATTERN_BLUE:
1101                 ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_B,
1102                                        IMX708_REG_VALUE_16BIT, ctrl->val);
1103                 break;
1104         case V4L2_CID_TEST_PATTERN_GREENB:
1105                 ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GB,
1106                                        IMX708_REG_VALUE_16BIT, ctrl->val);
1107                 break;
1108         case V4L2_CID_HFLIP:
1109         case V4L2_CID_VFLIP:
1110                 ret = imx708_write_reg(imx708, IMX708_REG_ORIENTATION, 1,
1111                                        imx708->hflip->val |
1112                                        imx708->vflip->val << 1);
1113                 break;
1114         case V4L2_CID_VBLANK:
1115                 ret = imx708_write_reg(imx708, IMX708_REG_FRAME_LENGTH,
1116                                        IMX708_REG_VALUE_16BIT,
1117                                        imx708->mode->height + ctrl->val);
1118                 break;
1119         case V4L2_CID_NOTIFY_GAINS:
1120                 ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_BLUE,
1121                                        IMX708_REG_VALUE_16BIT,
1122                                        imx708->notify_gains->p_new.p_u32[0]);
1123                 if (ret)
1124                         break;
1125                 ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_RED,
1126                                        IMX708_REG_VALUE_16BIT,
1127                                        imx708->notify_gains->p_new.p_u32[3]);
1128                 break;
1129         case V4L2_CID_WIDE_DYNAMIC_RANGE:
1130                 code = imx708_get_format_code(imx708);
1131                 get_mode_table(code, &mode_list, &num_modes, ctrl->val);
1132                 imx708->mode = v4l2_find_nearest_size(mode_list,
1133                                                       num_modes,
1134                                                       width, height,
1135                                                       imx708->mode->width,
1136                                                       imx708->mode->height);
1137                 imx708_set_framing_limits(imx708);
1138                 break;
1139         default:
1140                 dev_info(&client->dev,
1141                          "ctrl(id:0x%x,val:0x%x) is not handled\n",
1142                          ctrl->id, ctrl->val);
1143                 ret = -EINVAL;
1144                 break;
1145         }
1146
1147         pm_runtime_put(&client->dev);
1148
1149         return ret;
1150 }
1151
1152 static const struct v4l2_ctrl_ops imx708_ctrl_ops = {
1153         .s_ctrl = imx708_set_ctrl,
1154 };
1155
1156 static int imx708_enum_mbus_code(struct v4l2_subdev *sd,
1157                                  struct v4l2_subdev_state *sd_state,
1158                                  struct v4l2_subdev_mbus_code_enum *code)
1159 {
1160         struct imx708 *imx708 = to_imx708(sd);
1161
1162         if (code->pad >= NUM_PADS)
1163                 return -EINVAL;
1164
1165         if (code->pad == IMAGE_PAD) {
1166                 if (code->index >= (ARRAY_SIZE(codes) / 4))
1167                         return -EINVAL;
1168
1169                 code->code = imx708_get_format_code(imx708);
1170         } else {
1171                 if (code->index > 0)
1172                         return -EINVAL;
1173
1174                 code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1175         }
1176
1177         return 0;
1178 }
1179
1180 static int imx708_enum_frame_size(struct v4l2_subdev *sd,
1181                                   struct v4l2_subdev_state *sd_state,
1182                                   struct v4l2_subdev_frame_size_enum *fse)
1183 {
1184         struct imx708 *imx708 = to_imx708(sd);
1185
1186         if (fse->pad >= NUM_PADS)
1187                 return -EINVAL;
1188
1189         if (fse->pad == IMAGE_PAD) {
1190                 const struct imx708_mode *mode_list;
1191                 unsigned int num_modes;
1192
1193                 get_mode_table(fse->code, &mode_list, &num_modes,
1194                                imx708->hdr_mode->val);
1195
1196                 if (fse->index >= num_modes)
1197                         return -EINVAL;
1198
1199                 if (fse->code != imx708_get_format_code(imx708))
1200                         return -EINVAL;
1201
1202                 fse->min_width = mode_list[fse->index].width;
1203                 fse->max_width = fse->min_width;
1204                 fse->min_height = mode_list[fse->index].height;
1205                 fse->max_height = fse->min_height;
1206         } else {
1207                 if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1208                         return -EINVAL;
1209
1210                 fse->min_width = IMX708_EMBEDDED_LINE_WIDTH;
1211                 fse->max_width = fse->min_width;
1212                 fse->min_height = IMX708_NUM_EMBEDDED_LINES;
1213                 fse->max_height = fse->min_height;
1214         }
1215
1216         return 0;
1217 }
1218
1219 static void imx708_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1220 {
1221         fmt->colorspace = V4L2_COLORSPACE_RAW;
1222         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1223         fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1224                                                           fmt->colorspace,
1225                                                           fmt->ycbcr_enc);
1226         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1227 }
1228
1229 static void imx708_update_image_pad_format(struct imx708 *imx708,
1230                                            const struct imx708_mode *mode,
1231                                            struct v4l2_subdev_format *fmt)
1232 {
1233         fmt->format.width = mode->width;
1234         fmt->format.height = mode->height;
1235         fmt->format.field = V4L2_FIELD_NONE;
1236         imx708_reset_colorspace(&fmt->format);
1237 }
1238
1239 static void imx708_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1240 {
1241         fmt->format.width = IMX708_EMBEDDED_LINE_WIDTH;
1242         fmt->format.height = IMX708_NUM_EMBEDDED_LINES;
1243         fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1244         fmt->format.field = V4L2_FIELD_NONE;
1245 }
1246
1247 static int imx708_get_pad_format(struct v4l2_subdev *sd,
1248                                  struct v4l2_subdev_state *sd_state,
1249                                  struct v4l2_subdev_format *fmt)
1250 {
1251         struct imx708 *imx708 = to_imx708(sd);
1252
1253         if (fmt->pad >= NUM_PADS)
1254                 return -EINVAL;
1255
1256         mutex_lock(&imx708->mutex);
1257
1258         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1259                 struct v4l2_mbus_framefmt *try_fmt =
1260                         v4l2_subdev_get_try_format(&imx708->sd, sd_state,
1261                                                    fmt->pad);
1262                 /* update the code which could change due to vflip or hflip */
1263                 try_fmt->code = fmt->pad == IMAGE_PAD ?
1264                                 imx708_get_format_code(imx708) :
1265                                 MEDIA_BUS_FMT_SENSOR_DATA;
1266                 fmt->format = *try_fmt;
1267         } else {
1268                 if (fmt->pad == IMAGE_PAD) {
1269                         imx708_update_image_pad_format(imx708, imx708->mode,
1270                                                        fmt);
1271                         fmt->format.code = imx708_get_format_code(imx708);
1272                 } else {
1273                         imx708_update_metadata_pad_format(fmt);
1274                 }
1275         }
1276
1277         mutex_unlock(&imx708->mutex);
1278         return 0;
1279 }
1280
1281 static int imx708_set_pad_format(struct v4l2_subdev *sd,
1282                                  struct v4l2_subdev_state *sd_state,
1283                                  struct v4l2_subdev_format *fmt)
1284 {
1285         struct v4l2_mbus_framefmt *framefmt;
1286         const struct imx708_mode *mode;
1287         struct imx708 *imx708 = to_imx708(sd);
1288
1289         if (fmt->pad >= NUM_PADS)
1290                 return -EINVAL;
1291
1292         mutex_lock(&imx708->mutex);
1293
1294         if (fmt->pad == IMAGE_PAD) {
1295                 const struct imx708_mode *mode_list;
1296                 unsigned int num_modes;
1297
1298                 /* Bayer order varies with flips */
1299                 fmt->format.code = imx708_get_format_code(imx708);
1300
1301                 get_mode_table(fmt->format.code, &mode_list, &num_modes,
1302                                imx708->hdr_mode->val);
1303
1304                 mode = v4l2_find_nearest_size(mode_list,
1305                                               num_modes,
1306                                               width, height,
1307                                               fmt->format.width,
1308                                               fmt->format.height);
1309                 imx708_update_image_pad_format(imx708, mode, fmt);
1310                 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1311                         framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1312                                                               fmt->pad);
1313                         *framefmt = fmt->format;
1314                 } else {
1315                         imx708->mode = mode;
1316                         imx708_set_framing_limits(imx708);
1317                 }
1318         } else {
1319                 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1320                         framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1321                                                               fmt->pad);
1322                         *framefmt = fmt->format;
1323                 } else {
1324                         /* Only one embedded data mode is supported */
1325                         imx708_update_metadata_pad_format(fmt);
1326                 }
1327         }
1328
1329         mutex_unlock(&imx708->mutex);
1330
1331         return 0;
1332 }
1333
1334 static const struct v4l2_rect *
1335 __imx708_get_pad_crop(struct imx708 *imx708, struct v4l2_subdev_state *sd_state,
1336                       unsigned int pad, enum v4l2_subdev_format_whence which)
1337 {
1338         switch (which) {
1339         case V4L2_SUBDEV_FORMAT_TRY:
1340                 return v4l2_subdev_get_try_crop(&imx708->sd, sd_state, pad);
1341         case V4L2_SUBDEV_FORMAT_ACTIVE:
1342                 return &imx708->mode->crop;
1343         }
1344
1345         return NULL;
1346 }
1347
1348 static int imx708_get_selection(struct v4l2_subdev *sd,
1349                                 struct v4l2_subdev_state *sd_state,
1350                                 struct v4l2_subdev_selection *sel)
1351 {
1352         switch (sel->target) {
1353         case V4L2_SEL_TGT_CROP: {
1354                 struct imx708 *imx708 = to_imx708(sd);
1355
1356                 mutex_lock(&imx708->mutex);
1357                 sel->r = *__imx708_get_pad_crop(imx708, sd_state, sel->pad,
1358                                                 sel->which);
1359                 mutex_unlock(&imx708->mutex);
1360
1361                 return 0;
1362         }
1363
1364         case V4L2_SEL_TGT_NATIVE_SIZE:
1365                 sel->r.left = 0;
1366                 sel->r.top = 0;
1367                 sel->r.width = IMX708_NATIVE_WIDTH;
1368                 sel->r.height = IMX708_NATIVE_HEIGHT;
1369
1370                 return 0;
1371
1372         case V4L2_SEL_TGT_CROP_DEFAULT:
1373         case V4L2_SEL_TGT_CROP_BOUNDS:
1374                 sel->r.left = IMX708_PIXEL_ARRAY_LEFT;
1375                 sel->r.top = IMX708_PIXEL_ARRAY_TOP;
1376                 sel->r.width = IMX708_PIXEL_ARRAY_WIDTH;
1377                 sel->r.height = IMX708_PIXEL_ARRAY_HEIGHT;
1378
1379                 return 0;
1380         }
1381
1382         return -EINVAL;
1383 }
1384
1385 /* Start streaming */
1386 static int imx708_start_streaming(struct imx708 *imx708)
1387 {
1388         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1389         const struct imx708_reg_list *reg_list;
1390         int i, ret;
1391         u32 val;
1392
1393         if (!imx708->common_regs_written) {
1394                 ret = imx708_write_regs(imx708, mode_common_regs,
1395                                         ARRAY_SIZE(mode_common_regs));
1396                 if (ret) {
1397                         dev_err(&client->dev, "%s failed to set common settings\n",
1398                                 __func__);
1399                         return ret;
1400                 }
1401
1402                 ret = imx708_read_reg(imx708, IMX708_REG_BASE_SPC_GAINS_L,
1403                                       IMX708_REG_VALUE_08BIT, &val);
1404                 if (ret == 0 && val == 0x40) {
1405                         for (i = 0; i < 54 && ret == 0; i++) {
1406                                 ret = imx708_write_reg(imx708,
1407                                                        IMX708_REG_BASE_SPC_GAINS_L + i,
1408                                                        IMX708_REG_VALUE_08BIT,
1409                                                        pdaf_gains[0][i % 9]);
1410                         }
1411                         for (i = 0; i < 54 && ret == 0; i++) {
1412                                 ret = imx708_write_reg(imx708,
1413                                                        IMX708_REG_BASE_SPC_GAINS_R + i,
1414                                                        IMX708_REG_VALUE_08BIT,
1415                                                        pdaf_gains[1][i % 9]);
1416                         }
1417                 }
1418                 if (ret) {
1419                         dev_err(&client->dev, "%s failed to set PDAF gains\n",
1420                                 __func__);
1421                         return ret;
1422                 }
1423
1424                 imx708->common_regs_written = true;
1425         }
1426
1427         /* Apply default values of current mode */
1428         reg_list = &imx708->mode->reg_list;
1429         ret = imx708_write_regs(imx708, reg_list->regs, reg_list->num_of_regs);
1430         if (ret) {
1431                 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1432                 return ret;
1433         }
1434
1435         /* Apply customized values from user */
1436         ret =  __v4l2_ctrl_handler_setup(imx708->sd.ctrl_handler);
1437         if (ret)
1438                 return ret;
1439
1440         /* set stream on register */
1441         return imx708_write_reg(imx708, IMX708_REG_MODE_SELECT,
1442                                 IMX708_REG_VALUE_08BIT, IMX708_MODE_STREAMING);
1443 }
1444
1445 /* Stop streaming */
1446 static void imx708_stop_streaming(struct imx708 *imx708)
1447 {
1448         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1449         int ret;
1450
1451         /* set stream off register */
1452         ret = imx708_write_reg(imx708, IMX708_REG_MODE_SELECT,
1453                                IMX708_REG_VALUE_08BIT, IMX708_MODE_STANDBY);
1454         if (ret)
1455                 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1456 }
1457
1458 static int imx708_set_stream(struct v4l2_subdev *sd, int enable)
1459 {
1460         struct imx708 *imx708 = to_imx708(sd);
1461         struct i2c_client *client = v4l2_get_subdevdata(sd);
1462         int ret = 0;
1463
1464         mutex_lock(&imx708->mutex);
1465         if (imx708->streaming == enable) {
1466                 mutex_unlock(&imx708->mutex);
1467                 return 0;
1468         }
1469
1470         if (enable) {
1471                 ret = pm_runtime_get_sync(&client->dev);
1472                 if (ret < 0) {
1473                         pm_runtime_put_noidle(&client->dev);
1474                         goto err_unlock;
1475                 }
1476
1477                 /*
1478                  * Apply default & customized values
1479                  * and then start streaming.
1480                  */
1481                 ret = imx708_start_streaming(imx708);
1482                 if (ret)
1483                         goto err_rpm_put;
1484         } else {
1485                 imx708_stop_streaming(imx708);
1486                 pm_runtime_put(&client->dev);
1487         }
1488
1489         imx708->streaming = enable;
1490
1491         /* vflip/hflip and hdr mode cannot change during streaming */
1492         __v4l2_ctrl_grab(imx708->vflip, enable);
1493         __v4l2_ctrl_grab(imx708->hflip, enable);
1494         __v4l2_ctrl_grab(imx708->hdr_mode, enable);
1495
1496         mutex_unlock(&imx708->mutex);
1497
1498         return ret;
1499
1500 err_rpm_put:
1501         pm_runtime_put(&client->dev);
1502 err_unlock:
1503         mutex_unlock(&imx708->mutex);
1504
1505         return ret;
1506 }
1507
1508 /* Power/clock management functions */
1509 static int imx708_power_on(struct device *dev)
1510 {
1511         struct i2c_client *client = to_i2c_client(dev);
1512         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1513         struct imx708 *imx708 = to_imx708(sd);
1514         int ret;
1515
1516         ret = regulator_bulk_enable(IMX708_NUM_SUPPLIES,
1517                                     imx708->supplies);
1518         if (ret) {
1519                 dev_err(&client->dev, "%s: failed to enable regulators\n",
1520                         __func__);
1521                 return ret;
1522         }
1523
1524         ret = clk_prepare_enable(imx708->xclk);
1525         if (ret) {
1526                 dev_err(&client->dev, "%s: failed to enable clock\n",
1527                         __func__);
1528                 goto reg_off;
1529         }
1530
1531         gpiod_set_value_cansleep(imx708->reset_gpio, 1);
1532         usleep_range(IMX708_XCLR_MIN_DELAY_US,
1533                      IMX708_XCLR_MIN_DELAY_US + IMX708_XCLR_DELAY_RANGE_US);
1534
1535         return 0;
1536
1537 reg_off:
1538         regulator_bulk_disable(IMX708_NUM_SUPPLIES, imx708->supplies);
1539         return ret;
1540 }
1541
1542 static int imx708_power_off(struct device *dev)
1543 {
1544         struct i2c_client *client = to_i2c_client(dev);
1545         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1546         struct imx708 *imx708 = to_imx708(sd);
1547
1548         gpiod_set_value_cansleep(imx708->reset_gpio, 0);
1549         regulator_bulk_disable(IMX708_NUM_SUPPLIES, imx708->supplies);
1550         clk_disable_unprepare(imx708->xclk);
1551
1552         /* Force reprogramming of the common registers when powered up again. */
1553         imx708->common_regs_written = false;
1554
1555         return 0;
1556 }
1557
1558 static int __maybe_unused imx708_suspend(struct device *dev)
1559 {
1560         struct i2c_client *client = to_i2c_client(dev);
1561         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1562         struct imx708 *imx708 = to_imx708(sd);
1563
1564         if (imx708->streaming)
1565                 imx708_stop_streaming(imx708);
1566
1567         return 0;
1568 }
1569
1570 static int __maybe_unused imx708_resume(struct device *dev)
1571 {
1572         struct i2c_client *client = to_i2c_client(dev);
1573         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1574         struct imx708 *imx708 = to_imx708(sd);
1575         int ret;
1576
1577         if (imx708->streaming) {
1578                 ret = imx708_start_streaming(imx708);
1579                 if (ret)
1580                         goto error;
1581         }
1582
1583         return 0;
1584
1585 error:
1586         imx708_stop_streaming(imx708);
1587         imx708->streaming = 0;
1588         return ret;
1589 }
1590
1591 static int imx708_get_regulators(struct imx708 *imx708)
1592 {
1593         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1594         unsigned int i;
1595
1596         for (i = 0; i < IMX708_NUM_SUPPLIES; i++)
1597                 imx708->supplies[i].supply = imx708_supply_name[i];
1598
1599         return devm_regulator_bulk_get(&client->dev,
1600                                        IMX708_NUM_SUPPLIES,
1601                                        imx708->supplies);
1602 }
1603
1604 /* Verify chip ID */
1605 static int imx708_identify_module(struct imx708 *imx708)
1606 {
1607         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1608         int ret;
1609         u32 val;
1610
1611         ret = imx708_read_reg(imx708, IMX708_REG_CHIP_ID,
1612                               IMX708_REG_VALUE_16BIT, &val);
1613         if (ret) {
1614                 dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1615                         IMX708_CHIP_ID, ret);
1616                 return ret;
1617         }
1618
1619         if (val != IMX708_CHIP_ID) {
1620                 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1621                         IMX708_CHIP_ID, val);
1622                 return -EIO;
1623         }
1624
1625         ret = imx708_read_reg(imx708, 0x0000, IMX708_REG_VALUE_16BIT, &val);
1626         if (!ret) {
1627                 dev_info(&client->dev, "camera module ID 0x%04x\n", val);
1628                 snprintf(imx708->sd.name, sizeof(imx708->sd.name), "imx708%s%s",
1629                          val & 0x02 ? "_wide" : "",
1630                          val & 0x80 ? "_noir" : "");
1631         }
1632
1633         return 0;
1634 }
1635
1636 static const struct v4l2_subdev_core_ops imx708_core_ops = {
1637         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1638         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1639 };
1640
1641 static const struct v4l2_subdev_video_ops imx708_video_ops = {
1642         .s_stream = imx708_set_stream,
1643 };
1644
1645 static const struct v4l2_subdev_pad_ops imx708_pad_ops = {
1646         .enum_mbus_code = imx708_enum_mbus_code,
1647         .get_fmt = imx708_get_pad_format,
1648         .set_fmt = imx708_set_pad_format,
1649         .get_selection = imx708_get_selection,
1650         .enum_frame_size = imx708_enum_frame_size,
1651 };
1652
1653 static const struct v4l2_subdev_ops imx708_subdev_ops = {
1654         .core = &imx708_core_ops,
1655         .video = &imx708_video_ops,
1656         .pad = &imx708_pad_ops,
1657 };
1658
1659 static const struct v4l2_subdev_internal_ops imx708_internal_ops = {
1660         .open = imx708_open,
1661 };
1662
1663 static const struct v4l2_ctrl_config imx708_notify_gains_ctrl = {
1664         .ops = &imx708_ctrl_ops,
1665         .id = V4L2_CID_NOTIFY_GAINS,
1666         .type = V4L2_CTRL_TYPE_U32,
1667         .min = IMX708_COLOUR_BALANCE_MIN,
1668         .max = IMX708_COLOUR_BALANCE_MAX,
1669         .step = IMX708_COLOUR_BALANCE_STEP,
1670         .def = IMX708_COLOUR_BALANCE_DEFAULT,
1671         .dims = { 4 },
1672         .elem_size = sizeof(u32),
1673 };
1674
1675 /* Initialize control handlers */
1676 static int imx708_init_controls(struct imx708 *imx708)
1677 {
1678         struct v4l2_ctrl_handler *ctrl_hdlr;
1679         struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1680         struct v4l2_fwnode_device_properties props;
1681         unsigned int i;
1682         int ret;
1683
1684         ctrl_hdlr = &imx708->ctrl_handler;
1685         ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
1686         if (ret)
1687                 return ret;
1688
1689         mutex_init(&imx708->mutex);
1690         ctrl_hdlr->lock = &imx708->mutex;
1691
1692         /* By default, PIXEL_RATE is read only */
1693         imx708->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1694                                                V4L2_CID_PIXEL_RATE,
1695                                                IMX708_INITIAL_PIXEL_RATE,
1696                                                IMX708_INITIAL_PIXEL_RATE, 1,
1697                                                IMX708_INITIAL_PIXEL_RATE);
1698
1699         /*
1700          * Create the controls here, but mode specific limits are setup
1701          * in the imx708_set_framing_limits() call below.
1702          */
1703         imx708->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1704                                            V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
1705         imx708->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1706                                            V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
1707
1708         imx708->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1709                                              V4L2_CID_EXPOSURE,
1710                                              IMX708_EXPOSURE_MIN,
1711                                              IMX708_EXPOSURE_MAX,
1712                                              IMX708_EXPOSURE_STEP,
1713                                              IMX708_EXPOSURE_DEFAULT);
1714
1715         v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1716                           IMX708_ANA_GAIN_MIN, IMX708_ANA_GAIN_MAX,
1717                           IMX708_ANA_GAIN_STEP, IMX708_ANA_GAIN_DEFAULT);
1718
1719         v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1720                           IMX708_DGTL_GAIN_MIN, IMX708_DGTL_GAIN_MAX,
1721                           IMX708_DGTL_GAIN_STEP, IMX708_DGTL_GAIN_DEFAULT);
1722
1723         imx708->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1724                                           V4L2_CID_HFLIP, 0, 1, 1, 0);
1725
1726         imx708->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1727                                           V4L2_CID_VFLIP, 0, 1, 1, 0);
1728
1729         v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx708_ctrl_ops,
1730                                      V4L2_CID_TEST_PATTERN,
1731                                      ARRAY_SIZE(imx708_test_pattern_menu) - 1,
1732                                      0, 0, imx708_test_pattern_menu);
1733         for (i = 0; i < 4; i++) {
1734                 /*
1735                  * The assumption is that
1736                  * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
1737                  * V4L2_CID_TEST_PATTERN_BLUE   == V4L2_CID_TEST_PATTERN_RED + 2
1738                  * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
1739                  */
1740                 v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1741                                   V4L2_CID_TEST_PATTERN_RED + i,
1742                                   IMX708_TEST_PATTERN_COLOUR_MIN,
1743                                   IMX708_TEST_PATTERN_COLOUR_MAX,
1744                                   IMX708_TEST_PATTERN_COLOUR_STEP,
1745                                   IMX708_TEST_PATTERN_COLOUR_MAX);
1746                 /* The "Solid color" pattern is white by default */
1747         }
1748
1749         imx708->notify_gains = v4l2_ctrl_new_custom(ctrl_hdlr,
1750                                                     &imx708_notify_gains_ctrl, NULL);
1751
1752         imx708->hdr_mode = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1753                                              V4L2_CID_WIDE_DYNAMIC_RANGE,
1754                                              0, 1, 1, 0);
1755
1756         ret = v4l2_fwnode_device_parse(&client->dev, &props);
1757         if (ret)
1758                 goto error;
1759
1760         v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx708_ctrl_ops, &props);
1761
1762         if (ctrl_hdlr->error) {
1763                 ret = ctrl_hdlr->error;
1764                 dev_err(&client->dev, "%s control init failed (%d)\n",
1765                         __func__, ret);
1766                 goto error;
1767         }
1768
1769         imx708->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1770         imx708->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1771         imx708->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1772         imx708->hdr_mode->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1773
1774         imx708->sd.ctrl_handler = ctrl_hdlr;
1775
1776         /* Setup exposure and frame/line length limits. */
1777         imx708_set_framing_limits(imx708);
1778
1779         return 0;
1780
1781 error:
1782         v4l2_ctrl_handler_free(ctrl_hdlr);
1783         mutex_destroy(&imx708->mutex);
1784
1785         return ret;
1786 }
1787
1788 static void imx708_free_controls(struct imx708 *imx708)
1789 {
1790         v4l2_ctrl_handler_free(imx708->sd.ctrl_handler);
1791         mutex_destroy(&imx708->mutex);
1792 }
1793
1794 static int imx708_check_hwcfg(struct device *dev)
1795 {
1796         struct fwnode_handle *endpoint;
1797         struct v4l2_fwnode_endpoint ep_cfg = {
1798                 .bus_type = V4L2_MBUS_CSI2_DPHY
1799         };
1800         int ret = -EINVAL;
1801
1802         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1803         if (!endpoint) {
1804                 dev_err(dev, "endpoint node not found\n");
1805                 return -EINVAL;
1806         }
1807
1808         if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1809                 dev_err(dev, "could not parse endpoint\n");
1810                 goto error_out;
1811         }
1812
1813         /* Check the number of MIPI CSI2 data lanes */
1814         if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1815                 dev_err(dev, "only 2 data lanes are currently supported\n");
1816                 goto error_out;
1817         }
1818
1819         /* Check the link frequency set in device tree */
1820         if (!ep_cfg.nr_of_link_frequencies) {
1821                 dev_err(dev, "link-frequency property not found in DT\n");
1822                 goto error_out;
1823         }
1824
1825         if (ep_cfg.nr_of_link_frequencies != 1 ||
1826             ep_cfg.link_frequencies[0] != IMX708_DEFAULT_LINK_FREQ) {
1827                 dev_err(dev, "Link frequency not supported: %lld\n",
1828                         ep_cfg.link_frequencies[0]);
1829                 goto error_out;
1830         }
1831
1832         ret = 0;
1833
1834 error_out:
1835         v4l2_fwnode_endpoint_free(&ep_cfg);
1836         fwnode_handle_put(endpoint);
1837
1838         return ret;
1839 }
1840
1841 static int imx708_probe(struct i2c_client *client)
1842 {
1843         struct device *dev = &client->dev;
1844         struct imx708 *imx708;
1845         int ret;
1846
1847         imx708 = devm_kzalloc(&client->dev, sizeof(*imx708), GFP_KERNEL);
1848         if (!imx708)
1849                 return -ENOMEM;
1850
1851         v4l2_i2c_subdev_init(&imx708->sd, client, &imx708_subdev_ops);
1852
1853         /* Check the hardware configuration in device tree */
1854         if (imx708_check_hwcfg(dev))
1855                 return -EINVAL;
1856
1857         /* Get system clock (xclk) */
1858         imx708->xclk = devm_clk_get(dev, NULL);
1859         if (IS_ERR(imx708->xclk)) {
1860                 dev_err(dev, "failed to get xclk\n");
1861                 return PTR_ERR(imx708->xclk);
1862         }
1863
1864         imx708->xclk_freq = clk_get_rate(imx708->xclk);
1865         if (imx708->xclk_freq != IMX708_XCLK_FREQ) {
1866                 dev_err(dev, "xclk frequency not supported: %d Hz\n",
1867                         imx708->xclk_freq);
1868                 return -EINVAL;
1869         }
1870
1871         ret = imx708_get_regulators(imx708);
1872         if (ret) {
1873                 dev_err(dev, "failed to get regulators\n");
1874                 return ret;
1875         }
1876
1877         /* Request optional enable pin */
1878         imx708->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1879                                                      GPIOD_OUT_HIGH);
1880
1881         /*
1882          * The sensor must be powered for imx708_identify_module()
1883          * to be able to read the CHIP_ID register
1884          */
1885         ret = imx708_power_on(dev);
1886         if (ret)
1887                 return ret;
1888
1889         ret = imx708_identify_module(imx708);
1890         if (ret)
1891                 goto error_power_off;
1892
1893         /* Initialize default format */
1894         imx708_set_default_format(imx708);
1895
1896         /* Enable runtime PM and turn off the device */
1897         pm_runtime_set_active(dev);
1898         pm_runtime_enable(dev);
1899         pm_runtime_idle(dev);
1900
1901         /* This needs the pm runtime to be registered. */
1902         ret = imx708_init_controls(imx708);
1903         if (ret)
1904                 goto error_power_off;
1905
1906         /* Initialize subdev */
1907         imx708->sd.internal_ops = &imx708_internal_ops;
1908         imx708->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1909                             V4L2_SUBDEV_FL_HAS_EVENTS;
1910         imx708->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1911
1912         /* Initialize source pads */
1913         imx708->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
1914         imx708->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
1915
1916         ret = media_entity_pads_init(&imx708->sd.entity, NUM_PADS, imx708->pad);
1917         if (ret) {
1918                 dev_err(dev, "failed to init entity pads: %d\n", ret);
1919                 goto error_handler_free;
1920         }
1921
1922         ret = v4l2_async_register_subdev_sensor(&imx708->sd);
1923         if (ret < 0) {
1924                 dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
1925                 goto error_media_entity;
1926         }
1927
1928         return 0;
1929
1930 error_media_entity:
1931         media_entity_cleanup(&imx708->sd.entity);
1932
1933 error_handler_free:
1934         imx708_free_controls(imx708);
1935
1936 error_power_off:
1937         pm_runtime_disable(&client->dev);
1938         pm_runtime_set_suspended(&client->dev);
1939         imx708_power_off(&client->dev);
1940
1941         return ret;
1942 }
1943
1944 static int imx708_remove(struct i2c_client *client)
1945 {
1946         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1947         struct imx708 *imx708 = to_imx708(sd);
1948
1949         v4l2_async_unregister_subdev(sd);
1950         media_entity_cleanup(&sd->entity);
1951         imx708_free_controls(imx708);
1952
1953         pm_runtime_disable(&client->dev);
1954         if (!pm_runtime_status_suspended(&client->dev))
1955                 imx708_power_off(&client->dev);
1956         pm_runtime_set_suspended(&client->dev);
1957
1958         return 0;
1959 }
1960
1961 static const struct of_device_id imx708_dt_ids[] = {
1962         { .compatible = "sony,imx708" },
1963         { /* sentinel */ }
1964 };
1965 MODULE_DEVICE_TABLE(of, imx708_dt_ids);
1966
1967 static const struct dev_pm_ops imx708_pm_ops = {
1968         SET_SYSTEM_SLEEP_PM_OPS(imx708_suspend, imx708_resume)
1969         SET_RUNTIME_PM_OPS(imx708_power_off, imx708_power_on, NULL)
1970 };
1971
1972 static struct i2c_driver imx708_i2c_driver = {
1973         .driver = {
1974                 .name = "imx708",
1975                 .of_match_table = imx708_dt_ids,
1976                 .pm = &imx708_pm_ops,
1977         },
1978         .probe_new = imx708_probe,
1979         .remove = imx708_remove,
1980 };
1981
1982 module_i2c_driver(imx708_i2c_driver);
1983
1984 MODULE_AUTHOR("David Plowman <david.plowman@raspberrypi.com>");
1985 MODULE_DESCRIPTION("Sony IMX708 sensor driver");
1986 MODULE_LICENSE("GPL v2");