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