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