Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / media / i2c / imx477.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A V4L2 driver for Sony IMX477 cameras.
4  * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
5  *
6  * Based on Sony imx219 camera driver
7  * Copyright (C) 2019-2020 Raspberry Pi (Trading) 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/of_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-fwnode.h>
22 #include <media/v4l2-mediabus.h>
23
24 static int dpc_enable = 1;
25 module_param(dpc_enable, int, 0644);
26 MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
27
28 static int trigger_mode;
29 module_param(trigger_mode, int, 0644);
30 MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
31
32 #define IMX477_REG_VALUE_08BIT          1
33 #define IMX477_REG_VALUE_16BIT          2
34
35 /* Chip ID */
36 #define IMX477_REG_CHIP_ID              0x0016
37 #define IMX477_CHIP_ID                  0x0477
38 #define IMX378_CHIP_ID                  0x0378
39
40 #define IMX477_REG_MODE_SELECT          0x0100
41 #define IMX477_MODE_STANDBY             0x00
42 #define IMX477_MODE_STREAMING           0x01
43
44 #define IMX477_REG_ORIENTATION          0x101
45
46 #define IMX477_XCLK_FREQ                24000000
47
48 #define IMX477_DEFAULT_LINK_FREQ        450000000
49
50 /* Pixel rate is fixed at 840MHz for all the modes */
51 #define IMX477_PIXEL_RATE               840000000
52
53 /* V_TIMING internal */
54 #define IMX477_REG_FRAME_LENGTH         0x0340
55 #define IMX477_FRAME_LENGTH_MAX         0xffdc
56
57 /* Long exposure multiplier */
58 #define IMX477_LONG_EXP_SHIFT_MAX       7
59 #define IMX477_LONG_EXP_SHIFT_REG       0x3100
60
61 /* Exposure control */
62 #define IMX477_REG_EXPOSURE             0x0202
63 #define IMX477_EXPOSURE_OFFSET          22
64 #define IMX477_EXPOSURE_MIN             4
65 #define IMX477_EXPOSURE_STEP            1
66 #define IMX477_EXPOSURE_DEFAULT         0x640
67 #define IMX477_EXPOSURE_MAX             (IMX477_FRAME_LENGTH_MAX - \
68                                          IMX477_EXPOSURE_OFFSET)
69
70 /* Analog gain control */
71 #define IMX477_REG_ANALOG_GAIN          0x0204
72 #define IMX477_ANA_GAIN_MIN             0
73 #define IMX477_ANA_GAIN_MAX             978
74 #define IMX477_ANA_GAIN_STEP            1
75 #define IMX477_ANA_GAIN_DEFAULT         0x0
76
77 /* Digital gain control */
78 #define IMX477_REG_DIGITAL_GAIN         0x020e
79 #define IMX477_DGTL_GAIN_MIN            0x0100
80 #define IMX477_DGTL_GAIN_MAX            0xffff
81 #define IMX477_DGTL_GAIN_DEFAULT        0x0100
82 #define IMX477_DGTL_GAIN_STEP           1
83
84 /* Test Pattern Control */
85 #define IMX477_REG_TEST_PATTERN         0x0600
86 #define IMX477_TEST_PATTERN_DISABLE     0
87 #define IMX477_TEST_PATTERN_SOLID_COLOR 1
88 #define IMX477_TEST_PATTERN_COLOR_BARS  2
89 #define IMX477_TEST_PATTERN_GREY_COLOR  3
90 #define IMX477_TEST_PATTERN_PN9         4
91
92 /* Test pattern colour components */
93 #define IMX477_REG_TEST_PATTERN_R       0x0602
94 #define IMX477_REG_TEST_PATTERN_GR      0x0604
95 #define IMX477_REG_TEST_PATTERN_B       0x0606
96 #define IMX477_REG_TEST_PATTERN_GB      0x0608
97 #define IMX477_TEST_PATTERN_COLOUR_MIN  0
98 #define IMX477_TEST_PATTERN_COLOUR_MAX  0x0fff
99 #define IMX477_TEST_PATTERN_COLOUR_STEP 1
100 #define IMX477_TEST_PATTERN_R_DEFAULT   IMX477_TEST_PATTERN_COLOUR_MAX
101 #define IMX477_TEST_PATTERN_GR_DEFAULT  0
102 #define IMX477_TEST_PATTERN_B_DEFAULT   0
103 #define IMX477_TEST_PATTERN_GB_DEFAULT  0
104
105 /* Trigger mode */
106 #define IMX477_REG_MC_MODE              0x3f0b
107 #define IMX477_REG_MS_SEL               0x3041
108 #define IMX477_REG_XVS_IO_CTRL          0x3040
109 #define IMX477_REG_EXTOUT_EN            0x4b81
110
111 /* Embedded metadata stream structure */
112 #define IMX477_EMBEDDED_LINE_WIDTH 16384
113 #define IMX477_NUM_EMBEDDED_LINES 1
114
115 enum pad_types {
116         IMAGE_PAD,
117         METADATA_PAD,
118         NUM_PADS
119 };
120
121 /* IMX477 native and active pixel array size. */
122 #define IMX477_NATIVE_WIDTH             4072U
123 #define IMX477_NATIVE_HEIGHT            3176U
124 #define IMX477_PIXEL_ARRAY_LEFT         8U
125 #define IMX477_PIXEL_ARRAY_TOP          16U
126 #define IMX477_PIXEL_ARRAY_WIDTH        4056U
127 #define IMX477_PIXEL_ARRAY_HEIGHT       3040U
128
129 struct imx477_reg {
130         u16 address;
131         u8 val;
132 };
133
134 struct imx477_reg_list {
135         unsigned int num_of_regs;
136         const struct imx477_reg *regs;
137 };
138
139 /* Mode : resolution and related config&values */
140 struct imx477_mode {
141         /* Frame width */
142         unsigned int width;
143
144         /* Frame height */
145         unsigned int height;
146
147         /* H-timing in pixels */
148         unsigned int line_length_pix;
149
150         /* Analog crop rectangle. */
151         struct v4l2_rect crop;
152
153         /* Highest possible framerate. */
154         struct v4l2_fract timeperframe_min;
155
156         /* Default framerate. */
157         struct v4l2_fract timeperframe_default;
158
159         /* Default register values */
160         struct imx477_reg_list reg_list;
161 };
162
163 static const struct imx477_reg mode_common_regs[] = {
164         {0x0136, 0x18},
165         {0x0137, 0x00},
166         {0x0138, 0x01},
167         {0xe000, 0x00},
168         {0xe07a, 0x01},
169         {0x0808, 0x02},
170         {0x4ae9, 0x18},
171         {0x4aea, 0x08},
172         {0xf61c, 0x04},
173         {0xf61e, 0x04},
174         {0x4ae9, 0x21},
175         {0x4aea, 0x80},
176         {0x38a8, 0x1f},
177         {0x38a9, 0xff},
178         {0x38aa, 0x1f},
179         {0x38ab, 0xff},
180         {0x55d4, 0x00},
181         {0x55d5, 0x00},
182         {0x55d6, 0x07},
183         {0x55d7, 0xff},
184         {0x55e8, 0x07},
185         {0x55e9, 0xff},
186         {0x55ea, 0x00},
187         {0x55eb, 0x00},
188         {0x574c, 0x07},
189         {0x574d, 0xff},
190         {0x574e, 0x00},
191         {0x574f, 0x00},
192         {0x5754, 0x00},
193         {0x5755, 0x00},
194         {0x5756, 0x07},
195         {0x5757, 0xff},
196         {0x5973, 0x04},
197         {0x5974, 0x01},
198         {0x5d13, 0xc3},
199         {0x5d14, 0x58},
200         {0x5d15, 0xa3},
201         {0x5d16, 0x1d},
202         {0x5d17, 0x65},
203         {0x5d18, 0x8c},
204         {0x5d1a, 0x06},
205         {0x5d1b, 0xa9},
206         {0x5d1c, 0x45},
207         {0x5d1d, 0x3a},
208         {0x5d1e, 0xab},
209         {0x5d1f, 0x15},
210         {0x5d21, 0x0e},
211         {0x5d22, 0x52},
212         {0x5d23, 0xaa},
213         {0x5d24, 0x7d},
214         {0x5d25, 0x57},
215         {0x5d26, 0xa8},
216         {0x5d37, 0x5a},
217         {0x5d38, 0x5a},
218         {0x5d77, 0x7f},
219         {0x7b75, 0x0e},
220         {0x7b76, 0x0b},
221         {0x7b77, 0x08},
222         {0x7b78, 0x0a},
223         {0x7b79, 0x47},
224         {0x7b7c, 0x00},
225         {0x7b7d, 0x00},
226         {0x8d1f, 0x00},
227         {0x8d27, 0x00},
228         {0x9004, 0x03},
229         {0x9200, 0x50},
230         {0x9201, 0x6c},
231         {0x9202, 0x71},
232         {0x9203, 0x00},
233         {0x9204, 0x71},
234         {0x9205, 0x01},
235         {0x9371, 0x6a},
236         {0x9373, 0x6a},
237         {0x9375, 0x64},
238         {0x991a, 0x00},
239         {0x996b, 0x8c},
240         {0x996c, 0x64},
241         {0x996d, 0x50},
242         {0x9a4c, 0x0d},
243         {0x9a4d, 0x0d},
244         {0xa001, 0x0a},
245         {0xa003, 0x0a},
246         {0xa005, 0x0a},
247         {0xa006, 0x01},
248         {0xa007, 0xc0},
249         {0xa009, 0xc0},
250         {0x3d8a, 0x01},
251         {0x4421, 0x04},
252         {0x7b3b, 0x01},
253         {0x7b4c, 0x00},
254         {0x9905, 0x00},
255         {0x9907, 0x00},
256         {0x9909, 0x00},
257         {0x990b, 0x00},
258         {0x9944, 0x3c},
259         {0x9947, 0x3c},
260         {0x994a, 0x8c},
261         {0x994b, 0x50},
262         {0x994c, 0x1b},
263         {0x994d, 0x8c},
264         {0x994e, 0x50},
265         {0x994f, 0x1b},
266         {0x9950, 0x8c},
267         {0x9951, 0x1b},
268         {0x9952, 0x0a},
269         {0x9953, 0x8c},
270         {0x9954, 0x1b},
271         {0x9955, 0x0a},
272         {0x9a13, 0x04},
273         {0x9a14, 0x04},
274         {0x9a19, 0x00},
275         {0x9a1c, 0x04},
276         {0x9a1d, 0x04},
277         {0x9a26, 0x05},
278         {0x9a27, 0x05},
279         {0x9a2c, 0x01},
280         {0x9a2d, 0x03},
281         {0x9a2f, 0x05},
282         {0x9a30, 0x05},
283         {0x9a41, 0x00},
284         {0x9a46, 0x00},
285         {0x9a47, 0x00},
286         {0x9c17, 0x35},
287         {0x9c1d, 0x31},
288         {0x9c29, 0x50},
289         {0x9c3b, 0x2f},
290         {0x9c41, 0x6b},
291         {0x9c47, 0x2d},
292         {0x9c4d, 0x40},
293         {0x9c6b, 0x00},
294         {0x9c71, 0xc8},
295         {0x9c73, 0x32},
296         {0x9c75, 0x04},
297         {0x9c7d, 0x2d},
298         {0x9c83, 0x40},
299         {0x9c94, 0x3f},
300         {0x9c95, 0x3f},
301         {0x9c96, 0x3f},
302         {0x9c97, 0x00},
303         {0x9c98, 0x00},
304         {0x9c99, 0x00},
305         {0x9c9a, 0x3f},
306         {0x9c9b, 0x3f},
307         {0x9c9c, 0x3f},
308         {0x9ca0, 0x0f},
309         {0x9ca1, 0x0f},
310         {0x9ca2, 0x0f},
311         {0x9ca3, 0x00},
312         {0x9ca4, 0x00},
313         {0x9ca5, 0x00},
314         {0x9ca6, 0x1e},
315         {0x9ca7, 0x1e},
316         {0x9ca8, 0x1e},
317         {0x9ca9, 0x00},
318         {0x9caa, 0x00},
319         {0x9cab, 0x00},
320         {0x9cac, 0x09},
321         {0x9cad, 0x09},
322         {0x9cae, 0x09},
323         {0x9cbd, 0x50},
324         {0x9cbf, 0x50},
325         {0x9cc1, 0x50},
326         {0x9cc3, 0x40},
327         {0x9cc5, 0x40},
328         {0x9cc7, 0x40},
329         {0x9cc9, 0x0a},
330         {0x9ccb, 0x0a},
331         {0x9ccd, 0x0a},
332         {0x9d17, 0x35},
333         {0x9d1d, 0x31},
334         {0x9d29, 0x50},
335         {0x9d3b, 0x2f},
336         {0x9d41, 0x6b},
337         {0x9d47, 0x42},
338         {0x9d4d, 0x5a},
339         {0x9d6b, 0x00},
340         {0x9d71, 0xc8},
341         {0x9d73, 0x32},
342         {0x9d75, 0x04},
343         {0x9d7d, 0x42},
344         {0x9d83, 0x5a},
345         {0x9d94, 0x3f},
346         {0x9d95, 0x3f},
347         {0x9d96, 0x3f},
348         {0x9d97, 0x00},
349         {0x9d98, 0x00},
350         {0x9d99, 0x00},
351         {0x9d9a, 0x3f},
352         {0x9d9b, 0x3f},
353         {0x9d9c, 0x3f},
354         {0x9d9d, 0x1f},
355         {0x9d9e, 0x1f},
356         {0x9d9f, 0x1f},
357         {0x9da0, 0x0f},
358         {0x9da1, 0x0f},
359         {0x9da2, 0x0f},
360         {0x9da3, 0x00},
361         {0x9da4, 0x00},
362         {0x9da5, 0x00},
363         {0x9da6, 0x1e},
364         {0x9da7, 0x1e},
365         {0x9da8, 0x1e},
366         {0x9da9, 0x00},
367         {0x9daa, 0x00},
368         {0x9dab, 0x00},
369         {0x9dac, 0x09},
370         {0x9dad, 0x09},
371         {0x9dae, 0x09},
372         {0x9dc9, 0x0a},
373         {0x9dcb, 0x0a},
374         {0x9dcd, 0x0a},
375         {0x9e17, 0x35},
376         {0x9e1d, 0x31},
377         {0x9e29, 0x50},
378         {0x9e3b, 0x2f},
379         {0x9e41, 0x6b},
380         {0x9e47, 0x2d},
381         {0x9e4d, 0x40},
382         {0x9e6b, 0x00},
383         {0x9e71, 0xc8},
384         {0x9e73, 0x32},
385         {0x9e75, 0x04},
386         {0x9e94, 0x0f},
387         {0x9e95, 0x0f},
388         {0x9e96, 0x0f},
389         {0x9e97, 0x00},
390         {0x9e98, 0x00},
391         {0x9e99, 0x00},
392         {0x9ea0, 0x0f},
393         {0x9ea1, 0x0f},
394         {0x9ea2, 0x0f},
395         {0x9ea3, 0x00},
396         {0x9ea4, 0x00},
397         {0x9ea5, 0x00},
398         {0x9ea6, 0x3f},
399         {0x9ea7, 0x3f},
400         {0x9ea8, 0x3f},
401         {0x9ea9, 0x00},
402         {0x9eaa, 0x00},
403         {0x9eab, 0x00},
404         {0x9eac, 0x09},
405         {0x9ead, 0x09},
406         {0x9eae, 0x09},
407         {0x9ec9, 0x0a},
408         {0x9ecb, 0x0a},
409         {0x9ecd, 0x0a},
410         {0x9f17, 0x35},
411         {0x9f1d, 0x31},
412         {0x9f29, 0x50},
413         {0x9f3b, 0x2f},
414         {0x9f41, 0x6b},
415         {0x9f47, 0x42},
416         {0x9f4d, 0x5a},
417         {0x9f6b, 0x00},
418         {0x9f71, 0xc8},
419         {0x9f73, 0x32},
420         {0x9f75, 0x04},
421         {0x9f94, 0x0f},
422         {0x9f95, 0x0f},
423         {0x9f96, 0x0f},
424         {0x9f97, 0x00},
425         {0x9f98, 0x00},
426         {0x9f99, 0x00},
427         {0x9f9a, 0x2f},
428         {0x9f9b, 0x2f},
429         {0x9f9c, 0x2f},
430         {0x9f9d, 0x00},
431         {0x9f9e, 0x00},
432         {0x9f9f, 0x00},
433         {0x9fa0, 0x0f},
434         {0x9fa1, 0x0f},
435         {0x9fa2, 0x0f},
436         {0x9fa3, 0x00},
437         {0x9fa4, 0x00},
438         {0x9fa5, 0x00},
439         {0x9fa6, 0x1e},
440         {0x9fa7, 0x1e},
441         {0x9fa8, 0x1e},
442         {0x9fa9, 0x00},
443         {0x9faa, 0x00},
444         {0x9fab, 0x00},
445         {0x9fac, 0x09},
446         {0x9fad, 0x09},
447         {0x9fae, 0x09},
448         {0x9fc9, 0x0a},
449         {0x9fcb, 0x0a},
450         {0x9fcd, 0x0a},
451         {0xa14b, 0xff},
452         {0xa151, 0x0c},
453         {0xa153, 0x50},
454         {0xa155, 0x02},
455         {0xa157, 0x00},
456         {0xa1ad, 0xff},
457         {0xa1b3, 0x0c},
458         {0xa1b5, 0x50},
459         {0xa1b9, 0x00},
460         {0xa24b, 0xff},
461         {0xa257, 0x00},
462         {0xa2ad, 0xff},
463         {0xa2b9, 0x00},
464         {0xb21f, 0x04},
465         {0xb35c, 0x00},
466         {0xb35e, 0x08},
467         {0x0112, 0x0c},
468         {0x0113, 0x0c},
469         {0x0114, 0x01},
470         {0x0350, 0x00},
471         {0xbcf1, 0x02},
472         {0x3ff9, 0x01},
473 };
474
475 /* 12 mpix 10fps */
476 static const struct imx477_reg mode_4056x3040_regs[] = {
477         {0x0342, 0x5d},
478         {0x0343, 0xc0},
479         {0x0344, 0x00},
480         {0x0345, 0x00},
481         {0x0346, 0x00},
482         {0x0347, 0x00},
483         {0x0348, 0x0f},
484         {0x0349, 0xd7},
485         {0x034a, 0x0b},
486         {0x034b, 0xdf},
487         {0x00e3, 0x00},
488         {0x00e4, 0x00},
489         {0x00fc, 0x0a},
490         {0x00fd, 0x0a},
491         {0x00fe, 0x0a},
492         {0x00ff, 0x0a},
493         {0x0220, 0x00},
494         {0x0221, 0x11},
495         {0x0381, 0x01},
496         {0x0383, 0x01},
497         {0x0385, 0x01},
498         {0x0387, 0x01},
499         {0x0900, 0x00},
500         {0x0901, 0x11},
501         {0x0902, 0x02},
502         {0x3140, 0x02},
503         {0x3c00, 0x00},
504         {0x3c01, 0x03},
505         {0x3c02, 0xa2},
506         {0x3f0d, 0x01},
507         {0x5748, 0x07},
508         {0x5749, 0xff},
509         {0x574a, 0x00},
510         {0x574b, 0x00},
511         {0x7b75, 0x0a},
512         {0x7b76, 0x0c},
513         {0x7b77, 0x07},
514         {0x7b78, 0x06},
515         {0x7b79, 0x3c},
516         {0x7b53, 0x01},
517         {0x9369, 0x5a},
518         {0x936b, 0x55},
519         {0x936d, 0x28},
520         {0x9304, 0x00},
521         {0x9305, 0x00},
522         {0x9e9a, 0x2f},
523         {0x9e9b, 0x2f},
524         {0x9e9c, 0x2f},
525         {0x9e9d, 0x00},
526         {0x9e9e, 0x00},
527         {0x9e9f, 0x00},
528         {0xa2a9, 0x60},
529         {0xa2b7, 0x00},
530         {0x0401, 0x00},
531         {0x0404, 0x00},
532         {0x0405, 0x10},
533         {0x0408, 0x00},
534         {0x0409, 0x00},
535         {0x040a, 0x00},
536         {0x040b, 0x00},
537         {0x040c, 0x0f},
538         {0x040d, 0xd8},
539         {0x040e, 0x0b},
540         {0x040f, 0xe0},
541         {0x034c, 0x0f},
542         {0x034d, 0xd8},
543         {0x034e, 0x0b},
544         {0x034f, 0xe0},
545         {0x0301, 0x05},
546         {0x0303, 0x02},
547         {0x0305, 0x04},
548         {0x0306, 0x01},
549         {0x0307, 0x5e},
550         {0x0309, 0x0c},
551         {0x030b, 0x02},
552         {0x030d, 0x02},
553         {0x030e, 0x00},
554         {0x030f, 0x96},
555         {0x0310, 0x01},
556         {0x0820, 0x07},
557         {0x0821, 0x08},
558         {0x0822, 0x00},
559         {0x0823, 0x00},
560         {0x080a, 0x00},
561         {0x080b, 0x7f},
562         {0x080c, 0x00},
563         {0x080d, 0x4f},
564         {0x080e, 0x00},
565         {0x080f, 0x77},
566         {0x0810, 0x00},
567         {0x0811, 0x5f},
568         {0x0812, 0x00},
569         {0x0813, 0x57},
570         {0x0814, 0x00},
571         {0x0815, 0x4f},
572         {0x0816, 0x01},
573         {0x0817, 0x27},
574         {0x0818, 0x00},
575         {0x0819, 0x3f},
576         {0xe04c, 0x00},
577         {0xe04d, 0x7f},
578         {0xe04e, 0x00},
579         {0xe04f, 0x1f},
580         {0x3e20, 0x01},
581         {0x3e37, 0x00},
582         {0x3f50, 0x00},
583         {0x3f56, 0x02},
584         {0x3f57, 0xae},
585 };
586
587 /* 2x2 binned. 40fps */
588 static const struct imx477_reg mode_2028x1520_regs[] = {
589         {0x0342, 0x31},
590         {0x0343, 0xc4},
591         {0x0344, 0x00},
592         {0x0345, 0x00},
593         {0x0346, 0x00},
594         {0x0347, 0x00},
595         {0x0348, 0x0f},
596         {0x0349, 0xd7},
597         {0x034a, 0x0b},
598         {0x034b, 0xdf},
599         {0x0220, 0x00},
600         {0x0221, 0x11},
601         {0x0381, 0x01},
602         {0x0383, 0x01},
603         {0x0385, 0x01},
604         {0x0387, 0x01},
605         {0x0900, 0x01},
606         {0x0901, 0x12},
607         {0x0902, 0x02},
608         {0x3140, 0x02},
609         {0x3c00, 0x00},
610         {0x3c01, 0x03},
611         {0x3c02, 0xa2},
612         {0x3f0d, 0x01},
613         {0x5748, 0x07},
614         {0x5749, 0xff},
615         {0x574a, 0x00},
616         {0x574b, 0x00},
617         {0x7b53, 0x01},
618         {0x9369, 0x73},
619         {0x936b, 0x64},
620         {0x936d, 0x5f},
621         {0x9304, 0x00},
622         {0x9305, 0x00},
623         {0x9e9a, 0x2f},
624         {0x9e9b, 0x2f},
625         {0x9e9c, 0x2f},
626         {0x9e9d, 0x00},
627         {0x9e9e, 0x00},
628         {0x9e9f, 0x00},
629         {0xa2a9, 0x60},
630         {0xa2b7, 0x00},
631         {0x0401, 0x01},
632         {0x0404, 0x00},
633         {0x0405, 0x20},
634         {0x0408, 0x00},
635         {0x0409, 0x00},
636         {0x040a, 0x00},
637         {0x040b, 0x00},
638         {0x040c, 0x0f},
639         {0x040d, 0xd8},
640         {0x040e, 0x0b},
641         {0x040f, 0xe0},
642         {0x034c, 0x07},
643         {0x034d, 0xec},
644         {0x034e, 0x05},
645         {0x034f, 0xf0},
646         {0x0301, 0x05},
647         {0x0303, 0x02},
648         {0x0305, 0x04},
649         {0x0306, 0x01},
650         {0x0307, 0x5e},
651         {0x0309, 0x0c},
652         {0x030b, 0x02},
653         {0x030d, 0x02},
654         {0x030e, 0x00},
655         {0x030f, 0x96},
656         {0x0310, 0x01},
657         {0x0820, 0x07},
658         {0x0821, 0x08},
659         {0x0822, 0x00},
660         {0x0823, 0x00},
661         {0x080a, 0x00},
662         {0x080b, 0x7f},
663         {0x080c, 0x00},
664         {0x080d, 0x4f},
665         {0x080e, 0x00},
666         {0x080f, 0x77},
667         {0x0810, 0x00},
668         {0x0811, 0x5f},
669         {0x0812, 0x00},
670         {0x0813, 0x57},
671         {0x0814, 0x00},
672         {0x0815, 0x4f},
673         {0x0816, 0x01},
674         {0x0817, 0x27},
675         {0x0818, 0x00},
676         {0x0819, 0x3f},
677         {0xe04c, 0x00},
678         {0xe04d, 0x7f},
679         {0xe04e, 0x00},
680         {0xe04f, 0x1f},
681         {0x3e20, 0x01},
682         {0x3e37, 0x00},
683         {0x3f50, 0x00},
684         {0x3f56, 0x01},
685         {0x3f57, 0x6c},
686 };
687
688 /* 1080p cropped mode */
689 static const struct imx477_reg mode_2028x1080_regs[] = {
690         {0x0342, 0x31},
691         {0x0343, 0xc4},
692         {0x0344, 0x00},
693         {0x0345, 0x00},
694         {0x0346, 0x01},
695         {0x0347, 0xb8},
696         {0x0348, 0x0f},
697         {0x0349, 0xd7},
698         {0x034a, 0x0a},
699         {0x034b, 0x27},
700         {0x0220, 0x00},
701         {0x0221, 0x11},
702         {0x0381, 0x01},
703         {0x0383, 0x01},
704         {0x0385, 0x01},
705         {0x0387, 0x01},
706         {0x0900, 0x01},
707         {0x0901, 0x12},
708         {0x0902, 0x02},
709         {0x3140, 0x02},
710         {0x3c00, 0x00},
711         {0x3c01, 0x03},
712         {0x3c02, 0xa2},
713         {0x3f0d, 0x01},
714         {0x5748, 0x07},
715         {0x5749, 0xff},
716         {0x574a, 0x00},
717         {0x574b, 0x00},
718         {0x7b53, 0x01},
719         {0x9369, 0x73},
720         {0x936b, 0x64},
721         {0x936d, 0x5f},
722         {0x9304, 0x00},
723         {0x9305, 0x00},
724         {0x9e9a, 0x2f},
725         {0x9e9b, 0x2f},
726         {0x9e9c, 0x2f},
727         {0x9e9d, 0x00},
728         {0x9e9e, 0x00},
729         {0x9e9f, 0x00},
730         {0xa2a9, 0x60},
731         {0xa2b7, 0x00},
732         {0x0401, 0x01},
733         {0x0404, 0x00},
734         {0x0405, 0x20},
735         {0x0408, 0x00},
736         {0x0409, 0x00},
737         {0x040a, 0x00},
738         {0x040b, 0x00},
739         {0x040c, 0x0f},
740         {0x040d, 0xd8},
741         {0x040e, 0x04},
742         {0x040f, 0x38},
743         {0x034c, 0x07},
744         {0x034d, 0xec},
745         {0x034e, 0x04},
746         {0x034f, 0x38},
747         {0x0301, 0x05},
748         {0x0303, 0x02},
749         {0x0305, 0x04},
750         {0x0306, 0x01},
751         {0x0307, 0x5e},
752         {0x0309, 0x0c},
753         {0x030b, 0x02},
754         {0x030d, 0x02},
755         {0x030e, 0x00},
756         {0x030f, 0x96},
757         {0x0310, 0x01},
758         {0x0820, 0x07},
759         {0x0821, 0x08},
760         {0x0822, 0x00},
761         {0x0823, 0x00},
762         {0x080a, 0x00},
763         {0x080b, 0x7f},
764         {0x080c, 0x00},
765         {0x080d, 0x4f},
766         {0x080e, 0x00},
767         {0x080f, 0x77},
768         {0x0810, 0x00},
769         {0x0811, 0x5f},
770         {0x0812, 0x00},
771         {0x0813, 0x57},
772         {0x0814, 0x00},
773         {0x0815, 0x4f},
774         {0x0816, 0x01},
775         {0x0817, 0x27},
776         {0x0818, 0x00},
777         {0x0819, 0x3f},
778         {0xe04c, 0x00},
779         {0xe04d, 0x7f},
780         {0xe04e, 0x00},
781         {0xe04f, 0x1f},
782         {0x3e20, 0x01},
783         {0x3e37, 0x00},
784         {0x3f50, 0x00},
785         {0x3f56, 0x01},
786         {0x3f57, 0x6c},
787 };
788
789 /* 4x4 binned. 120fps */
790 static const struct imx477_reg mode_1332x990_regs[] = {
791         {0x420b, 0x01},
792         {0x990c, 0x00},
793         {0x990d, 0x08},
794         {0x9956, 0x8c},
795         {0x9957, 0x64},
796         {0x9958, 0x50},
797         {0x9a48, 0x06},
798         {0x9a49, 0x06},
799         {0x9a4a, 0x06},
800         {0x9a4b, 0x06},
801         {0x9a4c, 0x06},
802         {0x9a4d, 0x06},
803         {0x0112, 0x0a},
804         {0x0113, 0x0a},
805         {0x0114, 0x01},
806         {0x0342, 0x1a},
807         {0x0343, 0x08},
808         {0x0340, 0x04},
809         {0x0341, 0x1a},
810         {0x0344, 0x00},
811         {0x0345, 0x00},
812         {0x0346, 0x02},
813         {0x0347, 0x10},
814         {0x0348, 0x0f},
815         {0x0349, 0xd7},
816         {0x034a, 0x09},
817         {0x034b, 0xcf},
818         {0x00e3, 0x00},
819         {0x00e4, 0x00},
820         {0x00fc, 0x0a},
821         {0x00fd, 0x0a},
822         {0x00fe, 0x0a},
823         {0x00ff, 0x0a},
824         {0xe013, 0x00},
825         {0x0220, 0x00},
826         {0x0221, 0x11},
827         {0x0381, 0x01},
828         {0x0383, 0x01},
829         {0x0385, 0x01},
830         {0x0387, 0x01},
831         {0x0900, 0x01},
832         {0x0901, 0x22},
833         {0x0902, 0x02},
834         {0x3140, 0x02},
835         {0x3c00, 0x00},
836         {0x3c01, 0x01},
837         {0x3c02, 0x9c},
838         {0x3f0d, 0x00},
839         {0x5748, 0x00},
840         {0x5749, 0x00},
841         {0x574a, 0x00},
842         {0x574b, 0xa4},
843         {0x7b75, 0x0e},
844         {0x7b76, 0x09},
845         {0x7b77, 0x08},
846         {0x7b78, 0x06},
847         {0x7b79, 0x34},
848         {0x7b53, 0x00},
849         {0x9369, 0x73},
850         {0x936b, 0x64},
851         {0x936d, 0x5f},
852         {0x9304, 0x03},
853         {0x9305, 0x80},
854         {0x9e9a, 0x2f},
855         {0x9e9b, 0x2f},
856         {0x9e9c, 0x2f},
857         {0x9e9d, 0x00},
858         {0x9e9e, 0x00},
859         {0x9e9f, 0x00},
860         {0xa2a9, 0x27},
861         {0xa2b7, 0x03},
862         {0x0401, 0x00},
863         {0x0404, 0x00},
864         {0x0405, 0x10},
865         {0x0408, 0x01},
866         {0x0409, 0x5c},
867         {0x040a, 0x00},
868         {0x040b, 0x00},
869         {0x040c, 0x05},
870         {0x040d, 0x34},
871         {0x040e, 0x03},
872         {0x040f, 0xde},
873         {0x034c, 0x05},
874         {0x034d, 0x34},
875         {0x034e, 0x03},
876         {0x034f, 0xde},
877         {0x0301, 0x05},
878         {0x0303, 0x02},
879         {0x0305, 0x02},
880         {0x0306, 0x00},
881         {0x0307, 0xaf},
882         {0x0309, 0x0a},
883         {0x030b, 0x02},
884         {0x030d, 0x02},
885         {0x030e, 0x00},
886         {0x030f, 0x96},
887         {0x0310, 0x01},
888         {0x0820, 0x07},
889         {0x0821, 0x08},
890         {0x0822, 0x00},
891         {0x0823, 0x00},
892         {0x080a, 0x00},
893         {0x080b, 0x7f},
894         {0x080c, 0x00},
895         {0x080d, 0x4f},
896         {0x080e, 0x00},
897         {0x080f, 0x77},
898         {0x0810, 0x00},
899         {0x0811, 0x5f},
900         {0x0812, 0x00},
901         {0x0813, 0x57},
902         {0x0814, 0x00},
903         {0x0815, 0x4f},
904         {0x0816, 0x01},
905         {0x0817, 0x27},
906         {0x0818, 0x00},
907         {0x0819, 0x3f},
908         {0xe04c, 0x00},
909         {0xe04d, 0x5f},
910         {0xe04e, 0x00},
911         {0xe04f, 0x1f},
912         {0x3e20, 0x01},
913         {0x3e37, 0x00},
914         {0x3f50, 0x00},
915         {0x3f56, 0x00},
916         {0x3f57, 0xbf},
917 };
918
919 /* Mode configs */
920 static const struct imx477_mode supported_modes_12bit[] = {
921         {
922                 /* 12MPix 10fps mode */
923                 .width = 4056,
924                 .height = 3040,
925                 .line_length_pix = 0x5dc0,
926                 .crop = {
927                         .left = IMX477_PIXEL_ARRAY_LEFT,
928                         .top = IMX477_PIXEL_ARRAY_TOP,
929                         .width = 4056,
930                         .height = 3040,
931                 },
932                 .timeperframe_min = {
933                         .numerator = 100,
934                         .denominator = 1000
935                 },
936                 .timeperframe_default = {
937                         .numerator = 100,
938                         .denominator = 1000
939                 },
940                 .reg_list = {
941                         .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
942                         .regs = mode_4056x3040_regs,
943                 },
944         },
945         {
946                 /* 2x2 binned 40fps mode */
947                 .width = 2028,
948                 .height = 1520,
949                 .line_length_pix = 0x31c4,
950                 .crop = {
951                         .left = IMX477_PIXEL_ARRAY_LEFT,
952                         .top = IMX477_PIXEL_ARRAY_TOP,
953                         .width = 4056,
954                         .height = 3040,
955                 },
956                 .timeperframe_min = {
957                         .numerator = 100,
958                         .denominator = 4000
959                 },
960                 .timeperframe_default = {
961                         .numerator = 100,
962                         .denominator = 3000
963                 },
964                 .reg_list = {
965                         .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
966                         .regs = mode_2028x1520_regs,
967                 },
968         },
969         {
970                 /* 1080p 50fps cropped mode */
971                 .width = 2028,
972                 .height = 1080,
973                 .line_length_pix = 0x31c4,
974                 .crop = {
975                         .left = IMX477_PIXEL_ARRAY_LEFT,
976                         .top = IMX477_PIXEL_ARRAY_TOP + 440,
977                         .width = 4056,
978                         .height = 2160,
979                 },
980                 .timeperframe_min = {
981                         .numerator = 100,
982                         .denominator = 5000
983                 },
984                 .timeperframe_default = {
985                         .numerator = 100,
986                         .denominator = 3000
987                 },
988                 .reg_list = {
989                         .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
990                         .regs = mode_2028x1080_regs,
991                 },
992         }
993 };
994
995 static const struct imx477_mode supported_modes_10bit[] = {
996         {
997                 /* 120fps. 2x2 binned and cropped */
998                 .width = 1332,
999                 .height = 990,
1000                 .line_length_pix = 6664,
1001                 .crop = {
1002                         /*
1003                          * FIXME: the analog crop rectangle is actually
1004                          * programmed with a horizontal displacement of 0
1005                          * pixels, not 4. It gets shrunk after going through
1006                          * the scaler. Move this information to the compose
1007                          * rectangle once the driver is expanded to represent
1008                          * its processing blocks with multiple subdevs.
1009                          */
1010                         .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1011                         .top = IMX477_PIXEL_ARRAY_TOP + 528,
1012                         .width = 2664,
1013                         .height = 1980,
1014                 },
1015                 .timeperframe_min = {
1016                         .numerator = 100,
1017                         .denominator = 12000
1018                 },
1019                 .timeperframe_default = {
1020                         .numerator = 100,
1021                         .denominator = 12000
1022                 },
1023                 .reg_list = {
1024                         .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1025                         .regs = mode_1332x990_regs,
1026                 }
1027         }
1028 };
1029
1030 /*
1031  * The supported formats.
1032  * This table MUST contain 4 entries per format, to cover the various flip
1033  * combinations in the order
1034  * - no flip
1035  * - h flip
1036  * - v flip
1037  * - h&v flips
1038  */
1039 static const u32 codes[] = {
1040         /* 12-bit modes. */
1041         MEDIA_BUS_FMT_SRGGB12_1X12,
1042         MEDIA_BUS_FMT_SGRBG12_1X12,
1043         MEDIA_BUS_FMT_SGBRG12_1X12,
1044         MEDIA_BUS_FMT_SBGGR12_1X12,
1045         /* 10-bit modes. */
1046         MEDIA_BUS_FMT_SRGGB10_1X10,
1047         MEDIA_BUS_FMT_SGRBG10_1X10,
1048         MEDIA_BUS_FMT_SGBRG10_1X10,
1049         MEDIA_BUS_FMT_SBGGR10_1X10,
1050 };
1051
1052 static const char * const imx477_test_pattern_menu[] = {
1053         "Disabled",
1054         "Color Bars",
1055         "Solid Color",
1056         "Grey Color Bars",
1057         "PN9"
1058 };
1059
1060 static const int imx477_test_pattern_val[] = {
1061         IMX477_TEST_PATTERN_DISABLE,
1062         IMX477_TEST_PATTERN_COLOR_BARS,
1063         IMX477_TEST_PATTERN_SOLID_COLOR,
1064         IMX477_TEST_PATTERN_GREY_COLOR,
1065         IMX477_TEST_PATTERN_PN9,
1066 };
1067
1068 /* regulator supplies */
1069 static const char * const imx477_supply_name[] = {
1070         /* Supplies can be enabled in any order */
1071         "VANA",  /* Analog (2.8V) supply */
1072         "VDIG",  /* Digital Core (1.05V) supply */
1073         "VDDL",  /* IF (1.8V) supply */
1074 };
1075
1076 #define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1077
1078 /*
1079  * Initialisation delay between XCLR low->high and the moment when the sensor
1080  * can start capture (i.e. can leave software standby), given by T7 in the
1081  * datasheet is 8ms.  This does include I2C setup time as well.
1082  *
1083  * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1084  * in the datasheet) is much smaller - 600us.
1085  */
1086 #define IMX477_XCLR_MIN_DELAY_US        8000
1087 #define IMX477_XCLR_DELAY_RANGE_US      1000
1088
1089 struct imx477_compatible_data {
1090         unsigned int chip_id;
1091         struct imx477_reg_list extra_regs;
1092 };
1093
1094 struct imx477 {
1095         struct v4l2_subdev sd;
1096         struct media_pad pad[NUM_PADS];
1097
1098         unsigned int fmt_code;
1099
1100         struct clk *xclk;
1101         u32 xclk_freq;
1102
1103         struct gpio_desc *reset_gpio;
1104         struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1105
1106         struct v4l2_ctrl_handler ctrl_handler;
1107         /* V4L2 Controls */
1108         struct v4l2_ctrl *pixel_rate;
1109         struct v4l2_ctrl *exposure;
1110         struct v4l2_ctrl *vflip;
1111         struct v4l2_ctrl *hflip;
1112         struct v4l2_ctrl *vblank;
1113         struct v4l2_ctrl *hblank;
1114
1115         /* Current mode */
1116         const struct imx477_mode *mode;
1117
1118         /*
1119          * Mutex for serialized access:
1120          * Protect sensor module set pad format and start/stop streaming safely.
1121          */
1122         struct mutex mutex;
1123
1124         /* Streaming on/off */
1125         bool streaming;
1126
1127         /* Rewrite common registers on stream on? */
1128         bool common_regs_written;
1129
1130         /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1131         unsigned int long_exp_shift;
1132
1133         /* Any extra information related to different compatible sensors */
1134         const struct imx477_compatible_data *compatible_data;
1135 };
1136
1137 static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1138 {
1139         return container_of(_sd, struct imx477, sd);
1140 }
1141
1142 static inline void get_mode_table(unsigned int code,
1143                                   const struct imx477_mode **mode_list,
1144                                   unsigned int *num_modes)
1145 {
1146         switch (code) {
1147         /* 12-bit */
1148         case MEDIA_BUS_FMT_SRGGB12_1X12:
1149         case MEDIA_BUS_FMT_SGRBG12_1X12:
1150         case MEDIA_BUS_FMT_SGBRG12_1X12:
1151         case MEDIA_BUS_FMT_SBGGR12_1X12:
1152                 *mode_list = supported_modes_12bit;
1153                 *num_modes = ARRAY_SIZE(supported_modes_12bit);
1154                 break;
1155         /* 10-bit */
1156         case MEDIA_BUS_FMT_SRGGB10_1X10:
1157         case MEDIA_BUS_FMT_SGRBG10_1X10:
1158         case MEDIA_BUS_FMT_SGBRG10_1X10:
1159         case MEDIA_BUS_FMT_SBGGR10_1X10:
1160                 *mode_list = supported_modes_10bit;
1161                 *num_modes = ARRAY_SIZE(supported_modes_10bit);
1162                 break;
1163         default:
1164                 *mode_list = NULL;
1165                 *num_modes = 0;
1166         }
1167 }
1168
1169 /* Read registers up to 2 at a time */
1170 static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1171 {
1172         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1173         struct i2c_msg msgs[2];
1174         u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1175         u8 data_buf[4] = { 0, };
1176         int ret;
1177
1178         if (len > 4)
1179                 return -EINVAL;
1180
1181         /* Write register address */
1182         msgs[0].addr = client->addr;
1183         msgs[0].flags = 0;
1184         msgs[0].len = ARRAY_SIZE(addr_buf);
1185         msgs[0].buf = addr_buf;
1186
1187         /* Read data from register */
1188         msgs[1].addr = client->addr;
1189         msgs[1].flags = I2C_M_RD;
1190         msgs[1].len = len;
1191         msgs[1].buf = &data_buf[4 - len];
1192
1193         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1194         if (ret != ARRAY_SIZE(msgs))
1195                 return -EIO;
1196
1197         *val = get_unaligned_be32(data_buf);
1198
1199         return 0;
1200 }
1201
1202 /* Write registers up to 2 at a time */
1203 static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1204 {
1205         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1206         u8 buf[6];
1207
1208         if (len > 4)
1209                 return -EINVAL;
1210
1211         put_unaligned_be16(reg, buf);
1212         put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1213         if (i2c_master_send(client, buf, len + 2) != len + 2)
1214                 return -EIO;
1215
1216         return 0;
1217 }
1218
1219 /* Write a list of registers */
1220 static int imx477_write_regs(struct imx477 *imx477,
1221                              const struct imx477_reg *regs, u32 len)
1222 {
1223         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1224         unsigned int i;
1225         int ret;
1226
1227         for (i = 0; i < len; i++) {
1228                 ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1229                 if (ret) {
1230                         dev_err_ratelimited(&client->dev,
1231                                             "Failed to write reg 0x%4.4x. error = %d\n",
1232                                             regs[i].address, ret);
1233
1234                         return ret;
1235                 }
1236         }
1237
1238         return 0;
1239 }
1240
1241 /* Get bayer order based on flip setting. */
1242 static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1243 {
1244         unsigned int i;
1245
1246         lockdep_assert_held(&imx477->mutex);
1247
1248         for (i = 0; i < ARRAY_SIZE(codes); i++)
1249                 if (codes[i] == code)
1250                         break;
1251
1252         if (i >= ARRAY_SIZE(codes))
1253                 i = 0;
1254
1255         i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1256             (imx477->hflip->val ? 1 : 0);
1257
1258         return codes[i];
1259 }
1260
1261 static void imx477_set_default_format(struct imx477 *imx477)
1262 {
1263         /* Set default mode to max resolution */
1264         imx477->mode = &supported_modes_12bit[0];
1265         imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1266 }
1267
1268 static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1269 {
1270         struct imx477 *imx477 = to_imx477(sd);
1271         struct v4l2_mbus_framefmt *try_fmt_img =
1272                 v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
1273         struct v4l2_mbus_framefmt *try_fmt_meta =
1274                 v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
1275         struct v4l2_rect *try_crop;
1276
1277         mutex_lock(&imx477->mutex);
1278
1279         /* Initialize try_fmt for the image pad */
1280         try_fmt_img->width = supported_modes_12bit[0].width;
1281         try_fmt_img->height = supported_modes_12bit[0].height;
1282         try_fmt_img->code = imx477_get_format_code(imx477,
1283                                                    MEDIA_BUS_FMT_SRGGB12_1X12);
1284         try_fmt_img->field = V4L2_FIELD_NONE;
1285
1286         /* Initialize try_fmt for the embedded metadata pad */
1287         try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1288         try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1289         try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1290         try_fmt_meta->field = V4L2_FIELD_NONE;
1291
1292         /* Initialize try_crop */
1293         try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
1294         try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1295         try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1296         try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1297         try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1298
1299         mutex_unlock(&imx477->mutex);
1300
1301         return 0;
1302 }
1303
1304 static void imx477_adjust_exposure_range(struct imx477 *imx477)
1305 {
1306         int exposure_max, exposure_def;
1307
1308         /* Honour the VBLANK limits when setting exposure. */
1309         exposure_max = imx477->mode->height + imx477->vblank->val -
1310                        IMX477_EXPOSURE_OFFSET;
1311         exposure_def = min(exposure_max, imx477->exposure->val);
1312         __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1313                                  exposure_max, imx477->exposure->step,
1314                                  exposure_def);
1315 }
1316
1317 static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1318 {
1319         int ret = 0;
1320
1321         imx477->long_exp_shift = 0;
1322
1323         while (val > IMX477_FRAME_LENGTH_MAX) {
1324                 imx477->long_exp_shift++;
1325                 val >>= 1;
1326         }
1327
1328         ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1329                                IMX477_REG_VALUE_16BIT, val);
1330         if (ret)
1331                 return ret;
1332
1333         return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1334                                 IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1335 }
1336
1337 static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1338 {
1339         struct imx477 *imx477 =
1340                 container_of(ctrl->handler, struct imx477, ctrl_handler);
1341         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1342         int ret = 0;
1343
1344         /*
1345          * The VBLANK control may change the limits of usable exposure, so check
1346          * and adjust if necessary.
1347          */
1348         if (ctrl->id == V4L2_CID_VBLANK)
1349                 imx477_adjust_exposure_range(imx477);
1350
1351         /*
1352          * Applying V4L2 control value only happens
1353          * when power is up for streaming
1354          */
1355         if (pm_runtime_get_if_in_use(&client->dev) == 0)
1356                 return 0;
1357
1358         switch (ctrl->id) {
1359         case V4L2_CID_ANALOGUE_GAIN:
1360                 ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1361                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1362                 break;
1363         case V4L2_CID_EXPOSURE:
1364                 ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1365                                        IMX477_REG_VALUE_16BIT, ctrl->val >>
1366                                                         imx477->long_exp_shift);
1367                 break;
1368         case V4L2_CID_DIGITAL_GAIN:
1369                 ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1370                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1371                 break;
1372         case V4L2_CID_TEST_PATTERN:
1373                 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1374                                        IMX477_REG_VALUE_16BIT,
1375                                        imx477_test_pattern_val[ctrl->val]);
1376                 break;
1377         case V4L2_CID_TEST_PATTERN_RED:
1378                 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1379                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1380                 break;
1381         case V4L2_CID_TEST_PATTERN_GREENR:
1382                 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1383                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1384                 break;
1385         case V4L2_CID_TEST_PATTERN_BLUE:
1386                 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1387                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1388                 break;
1389         case V4L2_CID_TEST_PATTERN_GREENB:
1390                 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1391                                        IMX477_REG_VALUE_16BIT, ctrl->val);
1392                 break;
1393         case V4L2_CID_HFLIP:
1394         case V4L2_CID_VFLIP:
1395                 ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1396                                        imx477->hflip->val |
1397                                        imx477->vflip->val << 1);
1398                 break;
1399         case V4L2_CID_VBLANK:
1400                 ret = imx477_set_frame_length(imx477,
1401                                               imx477->mode->height + ctrl->val);
1402                 break;
1403         default:
1404                 dev_info(&client->dev,
1405                          "ctrl(id:0x%x,val:0x%x) is not handled\n",
1406                          ctrl->id, ctrl->val);
1407                 ret = -EINVAL;
1408                 break;
1409         }
1410
1411         pm_runtime_put(&client->dev);
1412
1413         return ret;
1414 }
1415
1416 static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1417         .s_ctrl = imx477_set_ctrl,
1418 };
1419
1420 static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1421                                  struct v4l2_subdev_state *sd_state,
1422                                  struct v4l2_subdev_mbus_code_enum *code)
1423 {
1424         struct imx477 *imx477 = to_imx477(sd);
1425
1426         if (code->pad >= NUM_PADS)
1427                 return -EINVAL;
1428
1429         if (code->pad == IMAGE_PAD) {
1430                 if (code->index >= (ARRAY_SIZE(codes) / 4))
1431                         return -EINVAL;
1432
1433                 code->code = imx477_get_format_code(imx477,
1434                                                     codes[code->index * 4]);
1435         } else {
1436                 if (code->index > 0)
1437                         return -EINVAL;
1438
1439                 code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1440         }
1441
1442         return 0;
1443 }
1444
1445 static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1446                                   struct v4l2_subdev_state *sd_state,
1447                                   struct v4l2_subdev_frame_size_enum *fse)
1448 {
1449         struct imx477 *imx477 = to_imx477(sd);
1450
1451         if (fse->pad >= NUM_PADS)
1452                 return -EINVAL;
1453
1454         if (fse->pad == IMAGE_PAD) {
1455                 const struct imx477_mode *mode_list;
1456                 unsigned int num_modes;
1457
1458                 get_mode_table(fse->code, &mode_list, &num_modes);
1459
1460                 if (fse->index >= num_modes)
1461                         return -EINVAL;
1462
1463                 if (fse->code != imx477_get_format_code(imx477, fse->code))
1464                         return -EINVAL;
1465
1466                 fse->min_width = mode_list[fse->index].width;
1467                 fse->max_width = fse->min_width;
1468                 fse->min_height = mode_list[fse->index].height;
1469                 fse->max_height = fse->min_height;
1470         } else {
1471                 if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1472                         return -EINVAL;
1473
1474                 fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1475                 fse->max_width = fse->min_width;
1476                 fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1477                 fse->max_height = fse->min_height;
1478         }
1479
1480         return 0;
1481 }
1482
1483 static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1484 {
1485         fmt->colorspace = V4L2_COLORSPACE_RAW;
1486         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1487         fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1488                                                           fmt->colorspace,
1489                                                           fmt->ycbcr_enc);
1490         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1491 }
1492
1493 static void imx477_update_image_pad_format(struct imx477 *imx477,
1494                                            const struct imx477_mode *mode,
1495                                            struct v4l2_subdev_format *fmt)
1496 {
1497         fmt->format.width = mode->width;
1498         fmt->format.height = mode->height;
1499         fmt->format.field = V4L2_FIELD_NONE;
1500         imx477_reset_colorspace(&fmt->format);
1501 }
1502
1503 static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1504 {
1505         fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1506         fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1507         fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1508         fmt->format.field = V4L2_FIELD_NONE;
1509 }
1510
1511 static int imx477_get_pad_format(struct v4l2_subdev *sd,
1512                                  struct v4l2_subdev_state *sd_state,
1513                                  struct v4l2_subdev_format *fmt)
1514 {
1515         struct imx477 *imx477 = to_imx477(sd);
1516
1517         if (fmt->pad >= NUM_PADS)
1518                 return -EINVAL;
1519
1520         mutex_lock(&imx477->mutex);
1521
1522         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1523                 struct v4l2_mbus_framefmt *try_fmt =
1524                         v4l2_subdev_get_try_format(&imx477->sd, sd_state,
1525                                                    fmt->pad);
1526                 /* update the code which could change due to vflip or hflip: */
1527                 try_fmt->code = fmt->pad == IMAGE_PAD ?
1528                                 imx477_get_format_code(imx477, try_fmt->code) :
1529                                 MEDIA_BUS_FMT_SENSOR_DATA;
1530                 fmt->format = *try_fmt;
1531         } else {
1532                 if (fmt->pad == IMAGE_PAD) {
1533                         imx477_update_image_pad_format(imx477, imx477->mode,
1534                                                        fmt);
1535                         fmt->format.code =
1536                                imx477_get_format_code(imx477, imx477->fmt_code);
1537                 } else {
1538                         imx477_update_metadata_pad_format(fmt);
1539                 }
1540         }
1541
1542         mutex_unlock(&imx477->mutex);
1543         return 0;
1544 }
1545
1546 static
1547 unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
1548                                      const struct v4l2_fract *timeperframe)
1549 {
1550         u64 frame_length;
1551
1552         frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
1553         do_div(frame_length,
1554                (u64)timeperframe->denominator * mode->line_length_pix);
1555
1556         if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
1557                 frame_length = IMX477_FRAME_LENGTH_MAX;
1558
1559         return max_t(unsigned int, frame_length, mode->height);
1560 }
1561
1562 static void imx477_set_framing_limits(struct imx477 *imx477)
1563 {
1564         unsigned int frm_length_min, frm_length_default, hblank;
1565         const struct imx477_mode *mode = imx477->mode;
1566
1567         frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
1568         frm_length_default =
1569                      imx477_get_frame_length(mode, &mode->timeperframe_default);
1570
1571         /* Default to no long exposure multiplier. */
1572         imx477->long_exp_shift = 0;
1573
1574         /* Update limits and set FPS to default */
1575         __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
1576                                  ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
1577                                         IMX477_FRAME_LENGTH_MAX) - mode->height,
1578                                  1, frm_length_default - mode->height);
1579
1580         /* Setting this will adjust the exposure limits as well. */
1581         __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
1582
1583         /*
1584          * Currently PPL is fixed to the mode specified value, so hblank
1585          * depends on mode->width only, and is not changeable in any
1586          * way other than changing the mode.
1587          */
1588         hblank = mode->line_length_pix - mode->width;
1589         __v4l2_ctrl_modify_range(imx477->hblank, hblank, hblank, 1, hblank);
1590 }
1591
1592 static int imx477_set_pad_format(struct v4l2_subdev *sd,
1593                                  struct v4l2_subdev_state *sd_state,
1594                                  struct v4l2_subdev_format *fmt)
1595 {
1596         struct v4l2_mbus_framefmt *framefmt;
1597         const struct imx477_mode *mode;
1598         struct imx477 *imx477 = to_imx477(sd);
1599
1600         if (fmt->pad >= NUM_PADS)
1601                 return -EINVAL;
1602
1603         mutex_lock(&imx477->mutex);
1604
1605         if (fmt->pad == IMAGE_PAD) {
1606                 const struct imx477_mode *mode_list;
1607                 unsigned int num_modes;
1608
1609                 /* Bayer order varies with flips */
1610                 fmt->format.code = imx477_get_format_code(imx477,
1611                                                           fmt->format.code);
1612
1613                 get_mode_table(fmt->format.code, &mode_list, &num_modes);
1614
1615                 mode = v4l2_find_nearest_size(mode_list,
1616                                               num_modes,
1617                                               width, height,
1618                                               fmt->format.width,
1619                                               fmt->format.height);
1620                 imx477_update_image_pad_format(imx477, mode, fmt);
1621                 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1622                         framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1623                                                               fmt->pad);
1624                         *framefmt = fmt->format;
1625                 } else {
1626                         imx477->mode = mode;
1627                         imx477->fmt_code = fmt->format.code;
1628                         imx477_set_framing_limits(imx477);
1629                 }
1630         } else {
1631                 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1632                         framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1633                                                               fmt->pad);
1634                         *framefmt = fmt->format;
1635                 } else {
1636                         /* Only one embedded data mode is supported */
1637                         imx477_update_metadata_pad_format(fmt);
1638                 }
1639         }
1640
1641         mutex_unlock(&imx477->mutex);
1642
1643         return 0;
1644 }
1645
1646 static const struct v4l2_rect *
1647 __imx477_get_pad_crop(struct imx477 *imx477,
1648                       struct v4l2_subdev_state *sd_state,
1649                       unsigned int pad, enum v4l2_subdev_format_whence which)
1650 {
1651         switch (which) {
1652         case V4L2_SUBDEV_FORMAT_TRY:
1653                 return v4l2_subdev_get_try_crop(&imx477->sd, sd_state, pad);
1654         case V4L2_SUBDEV_FORMAT_ACTIVE:
1655                 return &imx477->mode->crop;
1656         }
1657
1658         return NULL;
1659 }
1660
1661 static int imx477_get_selection(struct v4l2_subdev *sd,
1662                                 struct v4l2_subdev_state *sd_state,
1663                                 struct v4l2_subdev_selection *sel)
1664 {
1665         switch (sel->target) {
1666         case V4L2_SEL_TGT_CROP: {
1667                 struct imx477 *imx477 = to_imx477(sd);
1668
1669                 mutex_lock(&imx477->mutex);
1670                 sel->r = *__imx477_get_pad_crop(imx477, sd_state, sel->pad,
1671                                                 sel->which);
1672                 mutex_unlock(&imx477->mutex);
1673
1674                 return 0;
1675         }
1676
1677         case V4L2_SEL_TGT_NATIVE_SIZE:
1678                 sel->r.left = 0;
1679                 sel->r.top = 0;
1680                 sel->r.width = IMX477_NATIVE_WIDTH;
1681                 sel->r.height = IMX477_NATIVE_HEIGHT;
1682
1683                 return 0;
1684
1685         case V4L2_SEL_TGT_CROP_DEFAULT:
1686         case V4L2_SEL_TGT_CROP_BOUNDS:
1687                 sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
1688                 sel->r.top = IMX477_PIXEL_ARRAY_TOP;
1689                 sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
1690                 sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
1691
1692                 return 0;
1693         }
1694
1695         return -EINVAL;
1696 }
1697
1698 /* Start streaming */
1699 static int imx477_start_streaming(struct imx477 *imx477)
1700 {
1701         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1702         const struct imx477_reg_list *reg_list;
1703         const struct imx477_reg_list *extra_regs;
1704         int ret;
1705
1706         if (!imx477->common_regs_written) {
1707                 ret = imx477_write_regs(imx477, mode_common_regs,
1708                                         ARRAY_SIZE(mode_common_regs));
1709                 if (!ret) {
1710                         extra_regs = &imx477->compatible_data->extra_regs;
1711                         ret = imx477_write_regs(imx477, extra_regs->regs,
1712                                                 extra_regs->num_of_regs);
1713                 }
1714
1715                 if (ret) {
1716                         dev_err(&client->dev, "%s failed to set common settings\n",
1717                                 __func__);
1718                         return ret;
1719                 }
1720                 imx477->common_regs_written = true;
1721         }
1722
1723         /* Apply default values of current mode */
1724         reg_list = &imx477->mode->reg_list;
1725         ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
1726         if (ret) {
1727                 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1728                 return ret;
1729         }
1730
1731         /* Set on-sensor DPC. */
1732         imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
1733         imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
1734
1735         /* Set vsync trigger mode */
1736         if (trigger_mode != 0) {
1737                 /* trigger_mode == 1 for source, 2 for sink */
1738                 const u32 val = (trigger_mode == 1) ? 1 : 0;
1739
1740                 imx477_write_reg(imx477, IMX477_REG_MC_MODE,
1741                                  IMX477_REG_VALUE_08BIT, 1);
1742                 imx477_write_reg(imx477, IMX477_REG_MS_SEL,
1743                                  IMX477_REG_VALUE_08BIT, val);
1744                 imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
1745                                  IMX477_REG_VALUE_08BIT, val);
1746                 imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
1747                                  IMX477_REG_VALUE_08BIT, val);
1748         }
1749
1750         /* Apply customized values from user */
1751         ret =  __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
1752         if (ret)
1753                 return ret;
1754
1755         /* set stream on register */
1756         return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1757                                 IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
1758 }
1759
1760 /* Stop streaming */
1761 static void imx477_stop_streaming(struct imx477 *imx477)
1762 {
1763         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1764         int ret;
1765
1766         /* set stream off register */
1767         ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1768                                IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
1769         if (ret)
1770                 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1771 }
1772
1773 static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
1774 {
1775         struct imx477 *imx477 = to_imx477(sd);
1776         struct i2c_client *client = v4l2_get_subdevdata(sd);
1777         int ret = 0;
1778
1779         mutex_lock(&imx477->mutex);
1780         if (imx477->streaming == enable) {
1781                 mutex_unlock(&imx477->mutex);
1782                 return 0;
1783         }
1784
1785         if (enable) {
1786                 ret = pm_runtime_get_sync(&client->dev);
1787                 if (ret < 0) {
1788                         pm_runtime_put_noidle(&client->dev);
1789                         goto err_unlock;
1790                 }
1791
1792                 /*
1793                  * Apply default & customized values
1794                  * and then start streaming.
1795                  */
1796                 ret = imx477_start_streaming(imx477);
1797                 if (ret)
1798                         goto err_rpm_put;
1799         } else {
1800                 imx477_stop_streaming(imx477);
1801                 pm_runtime_put(&client->dev);
1802         }
1803
1804         imx477->streaming = enable;
1805
1806         /* vflip and hflip cannot change during streaming */
1807         __v4l2_ctrl_grab(imx477->vflip, enable);
1808         __v4l2_ctrl_grab(imx477->hflip, enable);
1809
1810         mutex_unlock(&imx477->mutex);
1811
1812         return ret;
1813
1814 err_rpm_put:
1815         pm_runtime_put(&client->dev);
1816 err_unlock:
1817         mutex_unlock(&imx477->mutex);
1818
1819         return ret;
1820 }
1821
1822 /* Power/clock management functions */
1823 static int imx477_power_on(struct device *dev)
1824 {
1825         struct i2c_client *client = to_i2c_client(dev);
1826         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1827         struct imx477 *imx477 = to_imx477(sd);
1828         int ret;
1829
1830         ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
1831                                     imx477->supplies);
1832         if (ret) {
1833                 dev_err(&client->dev, "%s: failed to enable regulators\n",
1834                         __func__);
1835                 return ret;
1836         }
1837
1838         ret = clk_prepare_enable(imx477->xclk);
1839         if (ret) {
1840                 dev_err(&client->dev, "%s: failed to enable clock\n",
1841                         __func__);
1842                 goto reg_off;
1843         }
1844
1845         gpiod_set_value_cansleep(imx477->reset_gpio, 1);
1846         usleep_range(IMX477_XCLR_MIN_DELAY_US,
1847                      IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
1848
1849         return 0;
1850
1851 reg_off:
1852         regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1853         return ret;
1854 }
1855
1856 static int imx477_power_off(struct device *dev)
1857 {
1858         struct i2c_client *client = to_i2c_client(dev);
1859         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1860         struct imx477 *imx477 = to_imx477(sd);
1861
1862         gpiod_set_value_cansleep(imx477->reset_gpio, 0);
1863         regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1864         clk_disable_unprepare(imx477->xclk);
1865
1866         /* Force reprogramming of the common registers when powered up again. */
1867         imx477->common_regs_written = false;
1868
1869         return 0;
1870 }
1871
1872 static int __maybe_unused imx477_suspend(struct device *dev)
1873 {
1874         struct i2c_client *client = to_i2c_client(dev);
1875         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1876         struct imx477 *imx477 = to_imx477(sd);
1877
1878         if (imx477->streaming)
1879                 imx477_stop_streaming(imx477);
1880
1881         return 0;
1882 }
1883
1884 static int __maybe_unused imx477_resume(struct device *dev)
1885 {
1886         struct i2c_client *client = to_i2c_client(dev);
1887         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1888         struct imx477 *imx477 = to_imx477(sd);
1889         int ret;
1890
1891         if (imx477->streaming) {
1892                 ret = imx477_start_streaming(imx477);
1893                 if (ret)
1894                         goto error;
1895         }
1896
1897         return 0;
1898
1899 error:
1900         imx477_stop_streaming(imx477);
1901         imx477->streaming = 0;
1902         return ret;
1903 }
1904
1905 static int imx477_get_regulators(struct imx477 *imx477)
1906 {
1907         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1908         unsigned int i;
1909
1910         for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
1911                 imx477->supplies[i].supply = imx477_supply_name[i];
1912
1913         return devm_regulator_bulk_get(&client->dev,
1914                                        IMX477_NUM_SUPPLIES,
1915                                        imx477->supplies);
1916 }
1917
1918 /* Verify chip ID */
1919 static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
1920 {
1921         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1922         int ret;
1923         u32 val;
1924
1925         ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
1926                               IMX477_REG_VALUE_16BIT, &val);
1927         if (ret) {
1928                 dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1929                         expected_id, ret);
1930                 return ret;
1931         }
1932
1933         if (val != expected_id) {
1934                 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1935                         expected_id, val);
1936                 return -EIO;
1937         }
1938
1939         dev_info(&client->dev, "Device found is imx%x\n", val);
1940
1941         return 0;
1942 }
1943
1944 static const struct v4l2_subdev_core_ops imx477_core_ops = {
1945         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1946         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1947 };
1948
1949 static const struct v4l2_subdev_video_ops imx477_video_ops = {
1950         .s_stream = imx477_set_stream,
1951 };
1952
1953 static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
1954         .enum_mbus_code = imx477_enum_mbus_code,
1955         .get_fmt = imx477_get_pad_format,
1956         .set_fmt = imx477_set_pad_format,
1957         .get_selection = imx477_get_selection,
1958         .enum_frame_size = imx477_enum_frame_size,
1959 };
1960
1961 static const struct v4l2_subdev_ops imx477_subdev_ops = {
1962         .core = &imx477_core_ops,
1963         .video = &imx477_video_ops,
1964         .pad = &imx477_pad_ops,
1965 };
1966
1967 static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
1968         .open = imx477_open,
1969 };
1970
1971 /* Initialize control handlers */
1972 static int imx477_init_controls(struct imx477 *imx477)
1973 {
1974         struct v4l2_ctrl_handler *ctrl_hdlr;
1975         struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1976         struct v4l2_fwnode_device_properties props;
1977         unsigned int i;
1978         int ret;
1979
1980         ctrl_hdlr = &imx477->ctrl_handler;
1981         ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
1982         if (ret)
1983                 return ret;
1984
1985         mutex_init(&imx477->mutex);
1986         ctrl_hdlr->lock = &imx477->mutex;
1987
1988         /* By default, PIXEL_RATE is read only */
1989         imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
1990                                                V4L2_CID_PIXEL_RATE,
1991                                                IMX477_PIXEL_RATE,
1992                                                IMX477_PIXEL_RATE, 1,
1993                                                IMX477_PIXEL_RATE);
1994
1995         /*
1996          * Create the controls here, but mode specific limits are setup
1997          * in the imx477_set_framing_limits() call below.
1998          */
1999         imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2000                                            V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2001         imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2002                                            V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2003
2004         /* HBLANK is read-only for now, but does change with mode. */
2005         if (imx477->hblank)
2006                 imx477->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2007
2008         imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2009                                              V4L2_CID_EXPOSURE,
2010                                              IMX477_EXPOSURE_MIN,
2011                                              IMX477_EXPOSURE_MAX,
2012                                              IMX477_EXPOSURE_STEP,
2013                                              IMX477_EXPOSURE_DEFAULT);
2014
2015         v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2016                           IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2017                           IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2018
2019         v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2020                           IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2021                           IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2022
2023         imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2024                                           V4L2_CID_HFLIP, 0, 1, 1, 0);
2025         if (imx477->hflip)
2026                 imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2027
2028         imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2029                                           V4L2_CID_VFLIP, 0, 1, 1, 0);
2030         if (imx477->vflip)
2031                 imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2032
2033         v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2034                                      V4L2_CID_TEST_PATTERN,
2035                                      ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2036                                      0, 0, imx477_test_pattern_menu);
2037         for (i = 0; i < 4; i++) {
2038                 /*
2039                  * The assumption is that
2040                  * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2041                  * V4L2_CID_TEST_PATTERN_BLUE   == V4L2_CID_TEST_PATTERN_RED + 2
2042                  * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2043                  */
2044                 v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2045                                   V4L2_CID_TEST_PATTERN_RED + i,
2046                                   IMX477_TEST_PATTERN_COLOUR_MIN,
2047                                   IMX477_TEST_PATTERN_COLOUR_MAX,
2048                                   IMX477_TEST_PATTERN_COLOUR_STEP,
2049                                   IMX477_TEST_PATTERN_COLOUR_MAX);
2050                 /* The "Solid color" pattern is white by default */
2051         }
2052
2053         if (ctrl_hdlr->error) {
2054                 ret = ctrl_hdlr->error;
2055                 dev_err(&client->dev, "%s control init failed (%d)\n",
2056                         __func__, ret);
2057                 goto error;
2058         }
2059
2060         ret = v4l2_fwnode_device_parse(&client->dev, &props);
2061         if (ret)
2062                 goto error;
2063
2064         ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2065                                               &props);
2066         if (ret)
2067                 goto error;
2068
2069         imx477->sd.ctrl_handler = ctrl_hdlr;
2070
2071         /* Setup exposure and frame/line length limits. */
2072         imx477_set_framing_limits(imx477);
2073
2074         return 0;
2075
2076 error:
2077         v4l2_ctrl_handler_free(ctrl_hdlr);
2078         mutex_destroy(&imx477->mutex);
2079
2080         return ret;
2081 }
2082
2083 static void imx477_free_controls(struct imx477 *imx477)
2084 {
2085         v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2086         mutex_destroy(&imx477->mutex);
2087 }
2088
2089 static int imx477_check_hwcfg(struct device *dev)
2090 {
2091         struct fwnode_handle *endpoint;
2092         struct v4l2_fwnode_endpoint ep_cfg = {
2093                 .bus_type = V4L2_MBUS_CSI2_DPHY
2094         };
2095         int ret = -EINVAL;
2096
2097         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2098         if (!endpoint) {
2099                 dev_err(dev, "endpoint node not found\n");
2100                 return -EINVAL;
2101         }
2102
2103         if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2104                 dev_err(dev, "could not parse endpoint\n");
2105                 goto error_out;
2106         }
2107
2108         /* Check the number of MIPI CSI2 data lanes */
2109         if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2110                 dev_err(dev, "only 2 data lanes are currently supported\n");
2111                 goto error_out;
2112         }
2113
2114         /* Check the link frequency set in device tree */
2115         if (!ep_cfg.nr_of_link_frequencies) {
2116                 dev_err(dev, "link-frequency property not found in DT\n");
2117                 goto error_out;
2118         }
2119
2120         if (ep_cfg.nr_of_link_frequencies != 1 ||
2121             ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2122                 dev_err(dev, "Link frequency not supported: %lld\n",
2123                         ep_cfg.link_frequencies[0]);
2124                 goto error_out;
2125         }
2126
2127         ret = 0;
2128
2129 error_out:
2130         v4l2_fwnode_endpoint_free(&ep_cfg);
2131         fwnode_handle_put(endpoint);
2132
2133         return ret;
2134 }
2135
2136 static const struct imx477_compatible_data imx477_compatible = {
2137         .chip_id = IMX477_CHIP_ID,
2138         .extra_regs = {
2139                 .num_of_regs = 0,
2140                 .regs = NULL
2141         }
2142 };
2143
2144 static const struct imx477_reg imx378_regs[] = {
2145         {0x3e35, 0x01},
2146         {0x4421, 0x08},
2147         {0x3ff9, 0x00},
2148 };
2149
2150 static const struct imx477_compatible_data imx378_compatible = {
2151         .chip_id = IMX378_CHIP_ID,
2152         .extra_regs = {
2153                 .num_of_regs = ARRAY_SIZE(imx378_regs),
2154                 .regs = imx378_regs
2155         }
2156 };
2157
2158 static const struct of_device_id imx477_dt_ids[] = {
2159         { .compatible = "sony,imx477", .data = &imx477_compatible },
2160         { .compatible = "sony,imx378", .data = &imx378_compatible },
2161         { /* sentinel */ }
2162 };
2163
2164 static int imx477_probe(struct i2c_client *client)
2165 {
2166         struct device *dev = &client->dev;
2167         struct imx477 *imx477;
2168         const struct of_device_id *match;
2169         int ret;
2170
2171         imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2172         if (!imx477)
2173                 return -ENOMEM;
2174
2175         v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2176
2177         match = of_match_device(imx477_dt_ids, dev);
2178         if (!match)
2179                 return -ENODEV;
2180         imx477->compatible_data =
2181                 (const struct imx477_compatible_data *)match->data;
2182
2183         /* Check the hardware configuration in device tree */
2184         if (imx477_check_hwcfg(dev))
2185                 return -EINVAL;
2186
2187         /* Get system clock (xclk) */
2188         imx477->xclk = devm_clk_get(dev, NULL);
2189         if (IS_ERR(imx477->xclk)) {
2190                 dev_err(dev, "failed to get xclk\n");
2191                 return PTR_ERR(imx477->xclk);
2192         }
2193
2194         imx477->xclk_freq = clk_get_rate(imx477->xclk);
2195         if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2196                 dev_err(dev, "xclk frequency not supported: %d Hz\n",
2197                         imx477->xclk_freq);
2198                 return -EINVAL;
2199         }
2200
2201         ret = imx477_get_regulators(imx477);
2202         if (ret) {
2203                 dev_err(dev, "failed to get regulators\n");
2204                 return ret;
2205         }
2206
2207         /* Request optional enable pin */
2208         imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2209                                                      GPIOD_OUT_HIGH);
2210
2211         /*
2212          * The sensor must be powered for imx477_identify_module()
2213          * to be able to read the CHIP_ID register
2214          */
2215         ret = imx477_power_on(dev);
2216         if (ret)
2217                 return ret;
2218
2219         ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2220         if (ret)
2221                 goto error_power_off;
2222
2223         /* Initialize default format */
2224         imx477_set_default_format(imx477);
2225
2226         /* Enable runtime PM and turn off the device */
2227         pm_runtime_set_active(dev);
2228         pm_runtime_enable(dev);
2229         pm_runtime_idle(dev);
2230
2231         /* This needs the pm runtime to be registered. */
2232         ret = imx477_init_controls(imx477);
2233         if (ret)
2234                 goto error_power_off;
2235
2236         /* Initialize subdev */
2237         imx477->sd.internal_ops = &imx477_internal_ops;
2238         imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2239                             V4L2_SUBDEV_FL_HAS_EVENTS;
2240         imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2241
2242         /* Initialize source pads */
2243         imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2244         imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2245
2246         ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2247         if (ret) {
2248                 dev_err(dev, "failed to init entity pads: %d\n", ret);
2249                 goto error_handler_free;
2250         }
2251
2252         ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2253         if (ret < 0) {
2254                 dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2255                 goto error_media_entity;
2256         }
2257
2258         return 0;
2259
2260 error_media_entity:
2261         media_entity_cleanup(&imx477->sd.entity);
2262
2263 error_handler_free:
2264         imx477_free_controls(imx477);
2265
2266 error_power_off:
2267         pm_runtime_disable(&client->dev);
2268         pm_runtime_set_suspended(&client->dev);
2269         imx477_power_off(&client->dev);
2270
2271         return ret;
2272 }
2273
2274 static int imx477_remove(struct i2c_client *client)
2275 {
2276         struct v4l2_subdev *sd = i2c_get_clientdata(client);
2277         struct imx477 *imx477 = to_imx477(sd);
2278
2279         v4l2_async_unregister_subdev(sd);
2280         media_entity_cleanup(&sd->entity);
2281         imx477_free_controls(imx477);
2282
2283         pm_runtime_disable(&client->dev);
2284         if (!pm_runtime_status_suspended(&client->dev))
2285                 imx477_power_off(&client->dev);
2286         pm_runtime_set_suspended(&client->dev);
2287
2288         return 0;
2289 }
2290
2291 MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2292
2293 static const struct dev_pm_ops imx477_pm_ops = {
2294         SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2295         SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2296 };
2297
2298 static struct i2c_driver imx477_i2c_driver = {
2299         .driver = {
2300                 .name = "imx477",
2301                 .of_match_table = imx477_dt_ids,
2302                 .pm = &imx477_pm_ops,
2303         },
2304         .probe_new = imx477_probe,
2305         .remove = imx477_remove,
2306 };
2307
2308 module_i2c_driver(imx477_i2c_driver);
2309
2310 MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2311 MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2312 MODULE_LICENSE("GPL v2");