Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / media / i2c / imx290.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sony IMX462 / IMX290 / IMX327 CMOS Image Sensor Driver
4  *
5  * The IMX462, IMX290,and IMX327 are very similar 1920x1080 1/2.8 CMOS image
6  * sensors.
7  * IMX327 can support up to 60fps with 10 or 12bit readout.
8  * IMX290 adds support for 120fps, but only 10bit and when connected over 4
9  * CSI-2 lanes.
10  * IMX462 adds support for 120fps in both 10 and 12bit readout modes.
11  *
12  * The modules don't appear to have a mechanism to identify whether the mono or
13  * colour variant is connected, therefore it is done via compatible string.
14  *
15  * Copyright (C) 2019 FRAMOS GmbH.
16  *
17  * Copyright (C) 2019 Linaro Ltd.
18  * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
19  */
20
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/consumer.h>
30 #include <media/media-entity.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-fwnode.h>
34 #include <media/v4l2-subdev.h>
35
36 enum imx290_clk_index {
37         CLK_37_125,
38         CLK_74_25,
39 };
40
41 #define IMX290_STANDBY 0x3000
42 #define IMX290_REGHOLD 0x3001
43 #define IMX290_XMSTA 0x3002
44 #define IMX290_FLIP_WINMODE 0x3007
45 #define IMX290_FR_FDG_SEL 0x3009
46 #define IMX290_BLKLEVEL_LOW 0x300a
47 #define IMX290_BLKLEVEL_HIGH 0x300b
48 #define IMX290_GAIN 0x3014
49 #define IMX290_VMAX_LOW 0x3018
50 #define IMX290_VMAX_MAX 0x3fff
51 #define IMX290_HMAX_LOW 0x301c
52 #define IMX290_HMAX_HIGH 0x301d
53 #define IMX290_HMAX_MIN 2200 /* Min of 2200 pixels = 60fps */
54 #define IMX290_HMAX_MAX 0xffff
55
56 #define IMX290_EXPOSURE_MIN 1
57 #define IMX290_EXPOSURE_STEP 1
58 #define IMX290_EXPOSURE_LOW 0x3020
59 #define IMX290_PGCTRL 0x308c
60 #define IMX290_PHY_LANE_NUM 0x3407
61 #define IMX290_CSI_LANE_MODE 0x3443
62
63 #define IMX290_PGCTRL_REGEN BIT(0)
64 #define IMX290_PGCTRL_THRU BIT(1)
65 #define IMX290_PGCTRL_MODE(n) ((n) << 4)
66
67 #define IMX290_NATIVE_WIDTH             1945U
68 #define IMX290_NATIVE_HEIGHT            1109U
69 #define IMX290_PIXEL_ARRAY_LEFT         4U
70 #define IMX290_PIXEL_ARRAY_TOP          12U
71 #define IMX290_PIXEL_ARRAY_WIDTH        1937U
72 #define IMX290_PIXEL_ARRAY_HEIGHT       1097U
73
74 static const char * const imx290_supply_name[] = {
75         "vdda",
76         "vddd",
77         "vdddo",
78 };
79
80 #define IMX290_NUM_SUPPLIES ARRAY_SIZE(imx290_supply_name)
81
82 struct imx290_regval {
83         u16 reg;
84         u8 val;
85 };
86
87 struct imx290_mode {
88         u32 width;
89         u32 height;
90         u32 hmax;
91         u32 vmax;
92         u8 link_freq_index;
93         struct v4l2_rect crop;
94
95         const struct imx290_regval *mode_data;
96         u32 mode_data_size;
97         const struct imx290_regval *lane_data;
98         u32 lane_data_size;
99
100
101         /* Clock setup can vary. Index as enum imx290_clk_index */
102         const struct imx290_regval *clk_data[2];
103         u32 clk_size;
104 };
105
106 struct imx290 {
107         struct device *dev;
108         struct clk *xclk;
109         u32 xclk_freq;
110         struct regmap *regmap;
111         u8 nlanes;
112         u8 bpp;
113         u16 hmax_min;
114
115         const struct imx290_pixfmt *formats;
116
117         struct v4l2_subdev sd;
118         struct media_pad pad;
119         struct v4l2_mbus_framefmt current_format;
120         const struct imx290_mode *current_mode;
121
122         struct regulator_bulk_data supplies[IMX290_NUM_SUPPLIES];
123         struct gpio_desc *rst_gpio;
124
125         struct v4l2_ctrl_handler ctrls;
126         struct v4l2_ctrl *link_freq;
127         struct v4l2_ctrl *pixel_rate;
128         struct v4l2_ctrl *hblank;
129         struct v4l2_ctrl *vblank;
130         struct v4l2_ctrl *hflip;
131         struct v4l2_ctrl *vflip;
132         struct v4l2_ctrl *exposure;
133
134         struct mutex lock;
135 };
136
137 struct imx290_pixfmt {
138         u32 code;
139         u8 bpp;
140 };
141
142 #define IMX290_NUM_FORMATS 2
143
144 static const struct imx290_pixfmt imx290_colour_formats[IMX290_NUM_FORMATS] = {
145         { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
146         { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
147 };
148
149 static const struct imx290_pixfmt imx290_mono_formats[IMX290_NUM_FORMATS] = {
150         { MEDIA_BUS_FMT_Y10_1X10, 10 },
151         { MEDIA_BUS_FMT_Y12_1X12, 12 },
152 };
153
154 static const struct regmap_config imx290_regmap_config = {
155         .reg_bits = 16,
156         .val_bits = 8,
157         .cache_type = REGCACHE_RBTREE,
158 };
159
160 static const char * const imx290_test_pattern_menu[] = {
161         "Disabled",
162         "Sequence Pattern 1",
163         "Horizontal Color-bar Chart",
164         "Vertical Color-bar Chart",
165         "Sequence Pattern 2",
166         "Gradation Pattern 1",
167         "Gradation Pattern 2",
168         "000/555h Toggle Pattern",
169 };
170
171 static const struct imx290_regval imx290_global_init_settings[] = {
172         { 0x3007, 0x00 },
173         { 0x301a, 0x00 },
174         { 0x303a, 0x0c },
175         { 0x3040, 0x00 },
176         { 0x3041, 0x00 },
177         { 0x303c, 0x00 },
178         { 0x303d, 0x00 },
179         { 0x3042, 0x9c },
180         { 0x3043, 0x07 },
181         { 0x303e, 0x49 },
182         { 0x303f, 0x04 },
183         { 0x304b, 0x0a },
184         { 0x300f, 0x00 },
185         { 0x3010, 0x21 },
186         { 0x3012, 0x64 },
187         { 0x3016, 0x09 },
188         { 0x3070, 0x02 },
189         { 0x3071, 0x11 },
190         { 0x309b, 0x10 },
191         { 0x309c, 0x22 },
192         { 0x30a2, 0x02 },
193         { 0x30a6, 0x20 },
194         { 0x30a8, 0x20 },
195         { 0x30aa, 0x20 },
196         { 0x30ac, 0x20 },
197         { 0x30b0, 0x43 },
198         { 0x3119, 0x9e },
199         { 0x311c, 0x1e },
200         { 0x311e, 0x08 },
201         { 0x3128, 0x05 },
202         { 0x313d, 0x83 },
203         { 0x3150, 0x03 },
204         { 0x317e, 0x00 },
205         { 0x32b8, 0x50 },
206         { 0x32b9, 0x10 },
207         { 0x32ba, 0x00 },
208         { 0x32bb, 0x04 },
209         { 0x32c8, 0x50 },
210         { 0x32c9, 0x10 },
211         { 0x32ca, 0x00 },
212         { 0x32cb, 0x04 },
213         { 0x332c, 0xd3 },
214         { 0x332d, 0x10 },
215         { 0x332e, 0x0d },
216         { 0x3358, 0x06 },
217         { 0x3359, 0xe1 },
218         { 0x335a, 0x11 },
219         { 0x3360, 0x1e },
220         { 0x3361, 0x61 },
221         { 0x3362, 0x10 },
222         { 0x33b0, 0x50 },
223         { 0x33b2, 0x1a },
224         { 0x33b3, 0x04 },
225 };
226
227 static const struct imx290_regval imx290_37_125mhz_clock_1080p[] = {
228         { 0x305c, 0x18 },
229         { 0x305d, 0x03 },
230         { 0x305e, 0x20 },
231         { 0x305f, 0x01 },
232         { 0x315e, 0x1a },
233         { 0x3164, 0x1a },
234         { 0x3444, 0x20 },
235         { 0x3445, 0x25 },
236         { 0x3480, 0x49 },
237 };
238
239 static const struct imx290_regval imx290_74_250mhz_clock_1080p[] = {
240         { 0x305c, 0x0c },
241         { 0x305d, 0x03 },
242         { 0x305e, 0x10 },
243         { 0x305f, 0x01 },
244         { 0x315e, 0x1b },
245         { 0x3164, 0x1b },
246         { 0x3444, 0x40 },
247         { 0x3445, 0x4a },
248         { 0x3480, 0x92 },
249 };
250
251 static const struct imx290_regval imx290_1080p_common_settings[] = {
252         /* mode settings */
253         { IMX290_FR_FDG_SEL, 0x01 },
254         { 0x3007, 0x00 },
255         { 0x303a, 0x0c },
256         { 0x3414, 0x0a },
257         { 0x3472, 0x80 },
258         { 0x3473, 0x07 },
259         { 0x3418, 0x38 },
260         { 0x3419, 0x04 },
261         { 0x3012, 0x64 },
262         { 0x3013, 0x00 },
263 };
264
265 static const struct imx290_regval imx290_1080p_2lane_settings[] = {
266         { 0x3405, 0x00 },
267         /* data rate settings */
268         { IMX290_PHY_LANE_NUM, 0x01 },
269         { IMX290_CSI_LANE_MODE, 0x01 },
270         { 0x3446, 0x77 },
271         { 0x3447, 0x00 },
272         { 0x3448, 0x67 },
273         { 0x3449, 0x00 },
274         { 0x344a, 0x47 },
275         { 0x344b, 0x00 },
276         { 0x344c, 0x37 },
277         { 0x344d, 0x00 },
278         { 0x344e, 0x3f },
279         { 0x344f, 0x00 },
280         { 0x3450, 0xff },
281         { 0x3451, 0x00 },
282         { 0x3452, 0x3f },
283         { 0x3453, 0x00 },
284         { 0x3454, 0x37 },
285         { 0x3455, 0x00 },
286 };
287
288 static const struct imx290_regval imx290_1080p_4lane_settings[] = {
289         { 0x3405, 0x10 },
290         /* data rate settings */
291         { IMX290_PHY_LANE_NUM, 0x03 },
292         { IMX290_CSI_LANE_MODE, 0x03 },
293         { 0x3446, 0x57 },
294         { 0x3447, 0x00 },
295         { 0x3448, 0x37 },
296         { 0x3449, 0x00 },
297         { 0x344a, 0x1f },
298         { 0x344b, 0x00 },
299         { 0x344c, 0x1f },
300         { 0x344d, 0x00 },
301         { 0x344e, 0x1f },
302         { 0x344f, 0x00 },
303         { 0x3450, 0x77 },
304         { 0x3451, 0x00 },
305         { 0x3452, 0x1f },
306         { 0x3453, 0x00 },
307         { 0x3454, 0x17 },
308         { 0x3455, 0x00 },
309 };
310
311 static const struct imx290_regval imx290_37_125mhz_clock_720p[] = {
312         { 0x305c, 0x20 },
313         { 0x305d, 0x00 },
314         { 0x305e, 0x20 },
315         { 0x305f, 0x01 },
316         { 0x315e, 0x1a },
317         { 0x3164, 0x1a },
318         { 0x3444, 0x20 },
319         { 0x3445, 0x25 },
320         { 0x3480, 0x49 },
321 };
322
323 static const struct imx290_regval imx290_74_250mhz_clock_720p[] = {
324         { 0x305c, 0x10 },
325         { 0x305d, 0x00 },
326         { 0x305e, 0x10 },
327         { 0x305f, 0x01 },
328         { 0x315e, 0x1b },
329         { 0x3164, 0x1b },
330         { 0x3444, 0x40 },
331         { 0x3445, 0x4a },
332         { 0x3480, 0x92 },
333 };
334
335 static const struct imx290_regval imx290_720p_common_settings[] = {
336         /* mode settings */
337         { IMX290_FR_FDG_SEL, 0x01 },
338         { 0x3007, 0x10 },
339         { 0x303a, 0x06 },
340         { 0x3414, 0x04 },
341         { 0x3472, 0x00 },
342         { 0x3473, 0x05 },
343         { 0x3418, 0xd0 },
344         { 0x3419, 0x02 },
345         { 0x3012, 0x64 },
346         { 0x3013, 0x00 },
347 };
348
349 static const struct imx290_regval imx290_720p_2lane_settings[] = {
350         { 0x3405, 0x00 },
351         { IMX290_PHY_LANE_NUM, 0x01 },
352         { IMX290_CSI_LANE_MODE, 0x01 },
353         /* data rate settings */
354         { 0x3446, 0x67 },
355         { 0x3447, 0x00 },
356         { 0x3448, 0x57 },
357         { 0x3449, 0x00 },
358         { 0x344a, 0x2f },
359         { 0x344b, 0x00 },
360         { 0x344c, 0x27 },
361         { 0x344d, 0x00 },
362         { 0x344e, 0x2f },
363         { 0x344f, 0x00 },
364         { 0x3450, 0xbf },
365         { 0x3451, 0x00 },
366         { 0x3452, 0x2f },
367         { 0x3453, 0x00 },
368         { 0x3454, 0x27 },
369         { 0x3455, 0x00 },
370 };
371
372 static const struct imx290_regval imx290_720p_4lane_settings[] = {
373         { 0x3405, 0x10 },
374         { IMX290_PHY_LANE_NUM, 0x03 },
375         { IMX290_CSI_LANE_MODE, 0x03 },
376         /* data rate settings */
377         { 0x3446, 0x4f },
378         { 0x3447, 0x00 },
379         { 0x3448, 0x2f },
380         { 0x3449, 0x00 },
381         { 0x344a, 0x17 },
382         { 0x344b, 0x00 },
383         { 0x344c, 0x17 },
384         { 0x344d, 0x00 },
385         { 0x344e, 0x17 },
386         { 0x344f, 0x00 },
387         { 0x3450, 0x57 },
388         { 0x3451, 0x00 },
389         { 0x3452, 0x17 },
390         { 0x3453, 0x00 },
391         { 0x3454, 0x17 },
392         { 0x3455, 0x00 },
393 };
394
395 static const struct imx290_regval imx290_10bit_settings[] = {
396         { 0x3005, 0x00},
397         { 0x3046, 0x00},
398         { 0x3129, 0x1d},
399         { 0x317c, 0x12},
400         { 0x31ec, 0x37},
401         { 0x3441, 0x0a},
402         { 0x3442, 0x0a},
403         { 0x300a, 0x3c},
404         { 0x300b, 0x00},
405 };
406
407 static const struct imx290_regval imx290_12bit_settings[] = {
408         { 0x3005, 0x01 },
409         { 0x3046, 0x01 },
410         { 0x3129, 0x00 },
411         { 0x317c, 0x00 },
412         { 0x31ec, 0x0e },
413         { 0x3441, 0x0c },
414         { 0x3442, 0x0c },
415         { 0x300a, 0xf0 },
416         { 0x300b, 0x00 },
417 };
418
419 /* supported link frequencies */
420 #define FREQ_INDEX_1080P        0
421 #define FREQ_INDEX_720P         1
422 static const s64 imx290_link_freq_2lanes[] = {
423         [FREQ_INDEX_1080P] = 445500000,
424         [FREQ_INDEX_720P] = 297000000,
425 };
426 static const s64 imx290_link_freq_4lanes[] = {
427         [FREQ_INDEX_1080P] = 222750000,
428         [FREQ_INDEX_720P] = 148500000,
429 };
430
431 /*
432  * In this function and in the similar ones below We rely on imx290_probe()
433  * to ensure that nlanes is either 2 or 4.
434  */
435 static inline const s64 *imx290_link_freqs_ptr(const struct imx290 *imx290)
436 {
437         if (imx290->nlanes == 2)
438                 return imx290_link_freq_2lanes;
439         else
440                 return imx290_link_freq_4lanes;
441 }
442
443 static inline int imx290_link_freqs_num(const struct imx290 *imx290)
444 {
445         if (imx290->nlanes == 2)
446                 return ARRAY_SIZE(imx290_link_freq_2lanes);
447         else
448                 return ARRAY_SIZE(imx290_link_freq_4lanes);
449 }
450
451 /* Mode configs */
452 static const struct imx290_mode imx290_modes_2lanes[] = {
453         {
454                 .width = 1920,
455                 .height = 1080,
456                 .hmax = 0x0898,
457                 .vmax = 0x0465,
458                 .link_freq_index = FREQ_INDEX_1080P,
459                 .crop = {
460                         .left = 4 + 8,
461                         .top = 12 + 8,
462                         .width = 1920,
463                         .height = 1080,
464                 },
465                 .mode_data = imx290_1080p_common_settings,
466                 .mode_data_size = ARRAY_SIZE(imx290_1080p_common_settings),
467                 .lane_data = imx290_1080p_2lane_settings,
468                 .lane_data_size = ARRAY_SIZE(imx290_1080p_2lane_settings),
469                 .clk_data = {
470                         [CLK_37_125] = imx290_37_125mhz_clock_1080p,
471                         [CLK_74_25] = imx290_74_250mhz_clock_1080p,
472                 },
473                 .clk_size = ARRAY_SIZE(imx290_37_125mhz_clock_1080p),
474         },
475         {
476                 .width = 1280,
477                 .height = 720,
478                 .hmax = 0x0ce4,
479                 .vmax = 0x02ee,
480                 .link_freq_index = FREQ_INDEX_720P,
481                 .crop = {
482                         .left = 4 + 8 + 320,
483                         .top = 12 + 8 + 180,
484                         .width = 1280,
485                         .height = 720,
486                 },
487                 .mode_data = imx290_720p_common_settings,
488                 .mode_data_size = ARRAY_SIZE(imx290_720p_common_settings),
489                 .lane_data = imx290_720p_2lane_settings,
490                 .lane_data_size = ARRAY_SIZE(imx290_720p_2lane_settings),
491                 .clk_data = {
492                         [CLK_37_125] = imx290_37_125mhz_clock_720p,
493                         [CLK_74_25] = imx290_74_250mhz_clock_720p,
494                 },
495                 .clk_size = ARRAY_SIZE(imx290_37_125mhz_clock_720p),
496         },
497 };
498
499 static const struct imx290_mode imx290_modes_4lanes[] = {
500         {
501                 .width = 1920,
502                 .height = 1080,
503                 .hmax = 0x0898,
504                 .vmax = 0x0465,
505                 .link_freq_index = FREQ_INDEX_1080P,
506                 .crop = {
507                         .left = 4 + 8,
508                         .top = 12 + 8,
509                         .width = 1920,
510                         .height = 1080,
511                 },
512                 .mode_data = imx290_1080p_common_settings,
513                 .mode_data_size = ARRAY_SIZE(imx290_1080p_common_settings),
514                 .lane_data = imx290_1080p_4lane_settings,
515                 .lane_data_size = ARRAY_SIZE(imx290_1080p_4lane_settings),
516                 .clk_data = {
517                         [CLK_37_125] = imx290_37_125mhz_clock_1080p,
518                         [CLK_74_25] = imx290_74_250mhz_clock_1080p,
519                 },
520                 .clk_size = ARRAY_SIZE(imx290_37_125mhz_clock_1080p),
521         },
522         {
523                 .width = 1280,
524                 .height = 720,
525                 .hmax = 0x0ce4,
526                 .vmax = 0x02ee,
527                 .link_freq_index = FREQ_INDEX_720P,
528                 .crop = {
529                         .left = 4 + 8 + 320,
530                         .top = 12 + 8 + 180,
531                         .width = 1280,
532                         .height = 720,
533                 },
534                 .mode_data = imx290_720p_common_settings,
535                 .mode_data_size = ARRAY_SIZE(imx290_720p_common_settings),
536                 .lane_data = imx290_720p_4lane_settings,
537                 .lane_data_size = ARRAY_SIZE(imx290_720p_4lane_settings),
538                 .clk_data = {
539                         [CLK_37_125] = imx290_37_125mhz_clock_720p,
540                         [CLK_74_25] = imx290_74_250mhz_clock_720p,
541                 },
542                 .clk_size = ARRAY_SIZE(imx290_37_125mhz_clock_720p),
543         },
544 };
545
546 static inline const struct imx290_mode *imx290_modes_ptr(const struct imx290 *imx290)
547 {
548         if (imx290->nlanes == 2)
549                 return imx290_modes_2lanes;
550         else
551                 return imx290_modes_4lanes;
552 }
553
554 static inline int imx290_modes_num(const struct imx290 *imx290)
555 {
556         if (imx290->nlanes == 2)
557                 return ARRAY_SIZE(imx290_modes_2lanes);
558         else
559                 return ARRAY_SIZE(imx290_modes_4lanes);
560 }
561
562 static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd)
563 {
564         return container_of(_sd, struct imx290, sd);
565 }
566
567 static inline int imx290_read_reg(struct imx290 *imx290, u16 addr, u8 *value)
568 {
569         unsigned int regval;
570         int ret;
571
572         ret = regmap_read(imx290->regmap, addr, &regval);
573         if (ret) {
574                 dev_err(imx290->dev, "I2C read failed for addr: %x\n", addr);
575                 return ret;
576         }
577
578         *value = regval & 0xff;
579
580         return 0;
581 }
582
583 static int imx290_write_reg(struct imx290 *imx290, u16 addr, u8 value)
584 {
585         int ret;
586
587         ret = regmap_write(imx290->regmap, addr, value);
588         if (ret) {
589                 dev_err(imx290->dev, "I2C write failed for addr: %x\n", addr);
590                 return ret;
591         }
592
593         return ret;
594 }
595
596 static int imx290_set_register_array(struct imx290 *imx290,
597                                      const struct imx290_regval *settings,
598                                      unsigned int num_settings)
599 {
600         unsigned int i;
601         int ret;
602
603         for (i = 0; i < num_settings; ++i, ++settings) {
604                 ret = imx290_write_reg(imx290, settings->reg, settings->val);
605                 if (ret < 0)
606                         return ret;
607         }
608
609         /* Provide 10ms settle time */
610         usleep_range(10000, 11000);
611
612         return 0;
613 }
614
615 static int imx290_write_buffered_reg(struct imx290 *imx290, u16 address_low,
616                                      u8 nr_regs, u32 value)
617 {
618         unsigned int i;
619         int ret;
620
621         ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x01);
622         if (ret) {
623                 dev_err(imx290->dev, "Error setting hold register\n");
624                 return ret;
625         }
626
627         for (i = 0; i < nr_regs; i++) {
628                 ret = imx290_write_reg(imx290, address_low + i,
629                                        (u8)(value >> (i * 8)));
630                 if (ret) {
631                         dev_err(imx290->dev, "Error writing buffered registers\n");
632                         return ret;
633                 }
634         }
635
636         ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x00);
637         if (ret) {
638                 dev_err(imx290->dev, "Error setting hold register\n");
639                 return ret;
640         }
641
642         return ret;
643 }
644
645 static int imx290_set_gain(struct imx290 *imx290, u32 value)
646 {
647         int ret;
648
649         ret = imx290_write_buffered_reg(imx290, IMX290_GAIN, 1, value);
650         if (ret)
651                 dev_err(imx290->dev, "Unable to write gain\n");
652
653         return ret;
654 }
655
656 static int imx290_set_exposure(struct imx290 *imx290, u32 value)
657 {
658         u32 exposure = (imx290->current_mode->height + imx290->vblank->val) -
659                                                 value - 1;
660         int ret;
661
662         ret = imx290_write_buffered_reg(imx290, IMX290_EXPOSURE_LOW, 3,
663                                         exposure);
664         if (ret)
665                 dev_err(imx290->dev, "Unable to write exposure\n");
666
667         return ret;
668 }
669
670 static int imx290_set_hmax(struct imx290 *imx290, u32 val)
671 {
672         u32 hmax = val + imx290->current_mode->width;
673         int ret;
674
675         ret = imx290_write_buffered_reg(imx290, IMX290_HMAX_LOW, 2,
676                                         hmax);
677         if (ret)
678                 dev_err(imx290->dev, "Error setting HMAX register\n");
679
680         return ret;
681 }
682
683 static int imx290_set_vmax(struct imx290 *imx290, u32 val)
684 {
685         u32 vmax = val + imx290->current_mode->height;
686         int ret;
687
688         ret = imx290_write_buffered_reg(imx290, IMX290_VMAX_LOW, 3,
689                                         vmax);
690         if (ret)
691                 dev_err(imx290->dev, "Unable to write vmax\n");
692
693         /*
694          * Changing vblank changes the allowed range for exposure.
695          * We don't supply the current exposure as default here as it
696          * may lie outside the new range. We will reset it just below.
697          */
698         __v4l2_ctrl_modify_range(imx290->exposure,
699                                  IMX290_EXPOSURE_MIN,
700                                  vmax - 2,
701                                  IMX290_EXPOSURE_STEP,
702                                  vmax - 2);
703
704         /*
705          * Becuse of the way exposure works for this sensor, updating
706          * vblank causes the effective exposure to change, so we must
707          * set it back to the "new" correct value.
708          */
709         imx290_set_exposure(imx290, imx290->exposure->val);
710
711         return ret;
712 }
713
714 /* Stop streaming */
715 static int imx290_stop_streaming(struct imx290 *imx290)
716 {
717         int ret;
718
719         ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x01);
720         if (ret < 0)
721                 return ret;
722
723         msleep(30);
724
725         return imx290_write_reg(imx290, IMX290_XMSTA, 0x01);
726 }
727
728 static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
729 {
730         struct imx290 *imx290 = container_of(ctrl->handler,
731                                              struct imx290, ctrls);
732         int ret = 0;
733         u8 val;
734
735         /* V4L2 controls values will be applied only when power is already up */
736         if (!pm_runtime_get_if_in_use(imx290->dev))
737                 return 0;
738
739         switch (ctrl->id) {
740         case V4L2_CID_ANALOGUE_GAIN:
741                 ret = imx290_set_gain(imx290, ctrl->val);
742                 break;
743         case V4L2_CID_EXPOSURE:
744                 ret = imx290_set_exposure(imx290, ctrl->val);
745                 break;
746         case V4L2_CID_HBLANK:
747                 ret = imx290_set_hmax(imx290, ctrl->val);
748                 break;
749         case V4L2_CID_VBLANK:
750                 ret = imx290_set_vmax(imx290, ctrl->val);
751                 break;
752         case V4L2_CID_HFLIP:
753         case V4L2_CID_VFLIP:
754                 /* WINMODE is in bits [6:4], so need to read-modify-write */
755                 ret = imx290_read_reg(imx290, IMX290_FLIP_WINMODE, &val);
756                 if (ret)
757                         break;
758                 val &= ~0x03;
759                 val |= imx290->vflip->val | (imx290->hflip->val << 1);
760                 ret = imx290_write_reg(imx290, IMX290_FLIP_WINMODE, val);
761                 break;
762         case V4L2_CID_TEST_PATTERN:
763                 if (ctrl->val) {
764                         imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW, 0x00);
765                         imx290_write_reg(imx290, IMX290_BLKLEVEL_HIGH, 0x00);
766                         usleep_range(10000, 11000);
767                         imx290_write_reg(imx290, IMX290_PGCTRL,
768                                          (u8)(IMX290_PGCTRL_REGEN |
769                                          IMX290_PGCTRL_THRU |
770                                          IMX290_PGCTRL_MODE(ctrl->val)));
771                 } else {
772                         imx290_write_reg(imx290, IMX290_PGCTRL, 0x00);
773                         usleep_range(10000, 11000);
774                         if (imx290->bpp == 10)
775                                 imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW,
776                                                  0x3c);
777                         else /* 12 bits per pixel */
778                                 imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW,
779                                                  0xf0);
780                         imx290_write_reg(imx290, IMX290_BLKLEVEL_HIGH, 0x00);
781                 }
782                 break;
783         default:
784                 ret = -EINVAL;
785                 break;
786         }
787
788         pm_runtime_put(imx290->dev);
789
790         return ret;
791 }
792
793 static const struct v4l2_ctrl_ops imx290_ctrl_ops = {
794         .s_ctrl = imx290_set_ctrl,
795 };
796
797 static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
798                                  struct v4l2_subdev_state *sd_state,
799                                  struct v4l2_subdev_mbus_code_enum *code)
800 {
801         const struct imx290 *imx290 = to_imx290(sd);
802
803         if (code->index >= IMX290_NUM_FORMATS)
804                 return -EINVAL;
805
806         code->code = imx290->formats[code->index].code;
807
808         return 0;
809 }
810
811 static int imx290_enum_frame_size(struct v4l2_subdev *sd,
812                                   struct v4l2_subdev_state *sd_state,
813                                   struct v4l2_subdev_frame_size_enum *fse)
814 {
815         const struct imx290 *imx290 = to_imx290(sd);
816         const struct imx290_mode *imx290_modes = imx290_modes_ptr(imx290);
817
818         if (fse->code != imx290->formats[0].code &&
819             fse->code != imx290->formats[1].code)
820                 return -EINVAL;
821
822         if (fse->index >= imx290_modes_num(imx290))
823                 return -EINVAL;
824
825         fse->min_width = imx290_modes[fse->index].width;
826         fse->max_width = imx290_modes[fse->index].width;
827         fse->min_height = imx290_modes[fse->index].height;
828         fse->max_height = imx290_modes[fse->index].height;
829
830         return 0;
831 }
832
833 static int imx290_get_fmt(struct v4l2_subdev *sd,
834                           struct v4l2_subdev_state *sd_state,
835                           struct v4l2_subdev_format *fmt)
836 {
837         struct imx290 *imx290 = to_imx290(sd);
838         struct v4l2_mbus_framefmt *framefmt;
839
840         mutex_lock(&imx290->lock);
841
842         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
843                 framefmt = v4l2_subdev_get_try_format(&imx290->sd, sd_state,
844                                                       fmt->pad);
845         else
846                 framefmt = &imx290->current_format;
847
848         fmt->format = *framefmt;
849
850         mutex_unlock(&imx290->lock);
851
852         return 0;
853 }
854
855 static inline u8 imx290_get_link_freq_index(struct imx290 *imx290)
856 {
857         return imx290->current_mode->link_freq_index;
858 }
859
860 static u64 imx290_calc_pixel_rate(struct imx290 *imx290)
861 {
862         return 148500000;
863 }
864
865 static int imx290_set_fmt(struct v4l2_subdev *sd,
866                           struct v4l2_subdev_state *sd_state,
867                           struct v4l2_subdev_format *fmt)
868 {
869         struct imx290 *imx290 = to_imx290(sd);
870         const struct imx290_mode *mode;
871         struct v4l2_mbus_framefmt *format;
872         unsigned int i;
873
874         mutex_lock(&imx290->lock);
875
876         mode = v4l2_find_nearest_size(imx290_modes_ptr(imx290),
877                                       imx290_modes_num(imx290), width, height,
878                                       fmt->format.width, fmt->format.height);
879
880         fmt->format.width = mode->width;
881         fmt->format.height = mode->height;
882
883         for (i = 0; i < IMX290_NUM_FORMATS; i++)
884                 if (imx290->formats[i].code == fmt->format.code)
885                         break;
886
887         if (i >= IMX290_NUM_FORMATS)
888                 i = 0;
889
890         fmt->format.code = imx290->formats[i].code;
891         fmt->format.field = V4L2_FIELD_NONE;
892         fmt->format.colorspace = V4L2_COLORSPACE_RAW;
893         fmt->format.ycbcr_enc =
894                         V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
895         fmt->format.quantization =
896                 V4L2_MAP_QUANTIZATION_DEFAULT(true, fmt->format.colorspace,
897                                               fmt->format.ycbcr_enc);
898         fmt->format.xfer_func =
899                 V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
900
901         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
902                 format = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
903         } else {
904                 format = &imx290->current_format;
905                 imx290->current_mode = mode;
906                 imx290->bpp = imx290->formats[i].bpp;
907
908                 if (imx290->link_freq)
909                         __v4l2_ctrl_s_ctrl(imx290->link_freq,
910                                            imx290_get_link_freq_index(imx290));
911                 if (imx290->pixel_rate)
912                         __v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate,
913                                                  imx290_calc_pixel_rate(imx290));
914
915                 if (imx290->hblank) {
916                         __v4l2_ctrl_modify_range(imx290->hblank,
917                                                  imx290->hmax_min - mode->width,
918                                                  IMX290_HMAX_MAX - mode->width,
919                                                  1, mode->hmax - mode->width);
920                         __v4l2_ctrl_s_ctrl(imx290->hblank,
921                                            mode->hmax - mode->width);
922                 }
923                 if (imx290->vblank) {
924                         __v4l2_ctrl_modify_range(imx290->vblank,
925                                                  mode->vmax - mode->height,
926                                                  IMX290_VMAX_MAX - mode->height,
927                                                  1,
928                                                  mode->vmax - mode->height);
929                         __v4l2_ctrl_s_ctrl(imx290->vblank,
930                                            mode->vmax - mode->height);
931                 }
932                 if (imx290->exposure)
933                         __v4l2_ctrl_modify_range(imx290->exposure,
934                                                  IMX290_EXPOSURE_MIN,
935                                                  mode->vmax - 2,
936                                                  IMX290_EXPOSURE_STEP,
937                                                  mode->vmax - 2);
938         }
939
940         *format = fmt->format;
941
942         mutex_unlock(&imx290->lock);
943
944         return 0;
945 }
946
947 static int imx290_entity_init_cfg(struct v4l2_subdev *subdev,
948                                   struct v4l2_subdev_state *sd_state)
949 {
950         struct v4l2_subdev_format fmt = { 0 };
951
952         fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
953         fmt.format.width = 1920;
954         fmt.format.height = 1080;
955
956         imx290_set_fmt(subdev, sd_state, &fmt);
957
958         return 0;
959 }
960
961 static int imx290_write_current_format(struct imx290 *imx290)
962 {
963         int ret;
964
965         switch (imx290->current_format.code) {
966         case MEDIA_BUS_FMT_SRGGB10_1X10:
967         case MEDIA_BUS_FMT_Y10_1X10:
968                 ret = imx290_set_register_array(imx290, imx290_10bit_settings,
969                                                 ARRAY_SIZE(
970                                                         imx290_10bit_settings));
971                 if (ret < 0) {
972                         dev_err(imx290->dev, "Could not set format registers\n");
973                         return ret;
974                 }
975                 break;
976         case MEDIA_BUS_FMT_SRGGB12_1X12:
977         case MEDIA_BUS_FMT_Y12_1X12:
978                 ret = imx290_set_register_array(imx290, imx290_12bit_settings,
979                                                 ARRAY_SIZE(
980                                                         imx290_12bit_settings));
981                 if (ret < 0) {
982                         dev_err(imx290->dev, "Could not set format registers\n");
983                         return ret;
984                 }
985                 break;
986         default:
987                 dev_err(imx290->dev, "Unknown pixel format\n");
988                 return -EINVAL;
989         }
990
991         return 0;
992 }
993
994 static const struct v4l2_rect *
995 __imx290_get_pad_crop(struct imx290 *imx290,
996                       struct v4l2_subdev_state *sd_state,
997                       unsigned int pad, enum v4l2_subdev_format_whence which)
998 {
999         switch (which) {
1000         case V4L2_SUBDEV_FORMAT_TRY:
1001                 return v4l2_subdev_get_try_crop(&imx290->sd, sd_state, pad);
1002         case V4L2_SUBDEV_FORMAT_ACTIVE:
1003                 return &imx290->current_mode->crop;
1004         }
1005
1006         return NULL;
1007 }
1008
1009 static int imx290_get_selection(struct v4l2_subdev *sd,
1010                                 struct v4l2_subdev_state *sd_state,
1011                                 struct v4l2_subdev_selection *sel)
1012 {
1013         switch (sel->target) {
1014         case V4L2_SEL_TGT_CROP: {
1015                 struct imx290 *imx290 = to_imx290(sd);
1016
1017                 mutex_lock(&imx290->lock);
1018                 sel->r = *__imx290_get_pad_crop(imx290, sd_state, sel->pad,
1019                                                 sel->which);
1020                 mutex_unlock(&imx290->lock);
1021
1022                 return 0;
1023         }
1024
1025         case V4L2_SEL_TGT_NATIVE_SIZE:
1026                 sel->r.top = 0;
1027                 sel->r.left = 0;
1028                 sel->r.width = IMX290_NATIVE_WIDTH;
1029                 sel->r.height = IMX290_NATIVE_HEIGHT;
1030
1031                 return 0;
1032
1033         case V4L2_SEL_TGT_CROP_DEFAULT:
1034         case V4L2_SEL_TGT_CROP_BOUNDS:
1035                 sel->r.top = IMX290_PIXEL_ARRAY_TOP;
1036                 sel->r.left = IMX290_PIXEL_ARRAY_LEFT;
1037                 sel->r.width = IMX290_PIXEL_ARRAY_WIDTH;
1038                 sel->r.height = IMX290_PIXEL_ARRAY_HEIGHT;
1039
1040                 return 0;
1041         }
1042
1043         return -EINVAL;
1044 }
1045
1046 /* Start streaming */
1047 static int imx290_start_streaming(struct imx290 *imx290)
1048 {
1049         enum imx290_clk_index clk_idx = imx290->xclk_freq == 37125000 ?
1050                                         CLK_37_125 : CLK_74_25;
1051         int ret;
1052
1053         /* Set init register settings */
1054         ret = imx290_set_register_array(imx290, imx290_global_init_settings,
1055                                         ARRAY_SIZE(
1056                                                 imx290_global_init_settings));
1057         if (ret < 0) {
1058                 dev_err(imx290->dev, "Could not set init registers\n");
1059                 return ret;
1060         }
1061
1062         ret = imx290_set_register_array(imx290,
1063                                         imx290->current_mode->clk_data[clk_idx],
1064                                         imx290->current_mode->clk_size);
1065         if (ret < 0) {
1066                 dev_err(imx290->dev, "Could not set clock registers\n");
1067                 return ret;
1068         }
1069
1070         /* Apply the register values related to current frame format */
1071         ret = imx290_write_current_format(imx290);
1072         if (ret < 0) {
1073                 dev_err(imx290->dev, "Could not set frame format\n");
1074                 return ret;
1075         }
1076
1077         /* Apply default values of current mode */
1078         ret = imx290_set_register_array(imx290,
1079                                         imx290->current_mode->mode_data,
1080                                         imx290->current_mode->mode_data_size);
1081         if (ret < 0) {
1082                 dev_err(imx290->dev, "Could not set current mode\n");
1083                 return ret;
1084         }
1085
1086         /* Apply lane config registers of current mode */
1087         ret = imx290_set_register_array(imx290,
1088                                         imx290->current_mode->lane_data,
1089                                         imx290->current_mode->lane_data_size);
1090         if (ret < 0) {
1091                 dev_err(imx290->dev, "Could not set current mode\n");
1092                 return ret;
1093         }
1094
1095         /* Apply customized values from user */
1096         ret = v4l2_ctrl_handler_setup(imx290->sd.ctrl_handler);
1097         if (ret) {
1098                 dev_err(imx290->dev, "Could not sync v4l2 controls\n");
1099                 return ret;
1100         }
1101
1102         ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x00);
1103         if (ret < 0)
1104                 return ret;
1105
1106         msleep(30);
1107
1108         /* Start streaming */
1109         return imx290_write_reg(imx290, IMX290_XMSTA, 0x00);
1110 }
1111
1112 static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
1113 {
1114         struct imx290 *imx290 = to_imx290(sd);
1115         int ret = 0;
1116
1117         if (enable) {
1118                 ret = pm_runtime_resume_and_get(imx290->dev);
1119                 if (ret < 0)
1120                         goto unlock_and_return;
1121
1122                 ret = imx290_start_streaming(imx290);
1123                 if (ret) {
1124                         dev_err(imx290->dev, "Start stream failed\n");
1125                         pm_runtime_put(imx290->dev);
1126                         goto unlock_and_return;
1127                 }
1128         } else {
1129                 imx290_stop_streaming(imx290);
1130                 pm_runtime_put(imx290->dev);
1131         }
1132         /* vflip and hflip cannot change during streaming */
1133         __v4l2_ctrl_grab(imx290->vflip, enable);
1134         __v4l2_ctrl_grab(imx290->hflip, enable);
1135
1136 unlock_and_return:
1137
1138         return ret;
1139 }
1140
1141 static int imx290_get_regulators(struct device *dev, struct imx290 *imx290)
1142 {
1143         unsigned int i;
1144
1145         for (i = 0; i < IMX290_NUM_SUPPLIES; i++)
1146                 imx290->supplies[i].supply = imx290_supply_name[i];
1147
1148         return devm_regulator_bulk_get(dev, IMX290_NUM_SUPPLIES,
1149                                        imx290->supplies);
1150 }
1151
1152 static int imx290_power_on(struct device *dev)
1153 {
1154         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1155         struct imx290 *imx290 = to_imx290(sd);
1156         int ret;
1157
1158         ret = clk_prepare_enable(imx290->xclk);
1159         if (ret) {
1160                 dev_err(dev, "Failed to enable clock\n");
1161                 return ret;
1162         }
1163
1164         ret = regulator_bulk_enable(IMX290_NUM_SUPPLIES, imx290->supplies);
1165         if (ret) {
1166                 dev_err(dev, "Failed to enable regulators\n");
1167                 clk_disable_unprepare(imx290->xclk);
1168                 return ret;
1169         }
1170
1171         usleep_range(1, 2);
1172         gpiod_set_value_cansleep(imx290->rst_gpio, 0);
1173         usleep_range(30000, 31000);
1174
1175         return 0;
1176 }
1177
1178 static int imx290_power_off(struct device *dev)
1179 {
1180         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1181         struct imx290 *imx290 = to_imx290(sd);
1182
1183         clk_disable_unprepare(imx290->xclk);
1184         gpiod_set_value_cansleep(imx290->rst_gpio, 1);
1185         regulator_bulk_disable(IMX290_NUM_SUPPLIES, imx290->supplies);
1186
1187         return 0;
1188 }
1189
1190 static const struct dev_pm_ops imx290_pm_ops = {
1191         SET_RUNTIME_PM_OPS(imx290_power_off, imx290_power_on, NULL)
1192 };
1193
1194 static const struct v4l2_subdev_video_ops imx290_video_ops = {
1195         .s_stream = imx290_set_stream,
1196 };
1197
1198 static const struct v4l2_subdev_pad_ops imx290_pad_ops = {
1199         .init_cfg = imx290_entity_init_cfg,
1200         .enum_mbus_code = imx290_enum_mbus_code,
1201         .enum_frame_size = imx290_enum_frame_size,
1202         .get_fmt = imx290_get_fmt,
1203         .set_fmt = imx290_set_fmt,
1204         .get_selection = imx290_get_selection,
1205 };
1206
1207 static const struct v4l2_subdev_ops imx290_subdev_ops = {
1208         .video = &imx290_video_ops,
1209         .pad = &imx290_pad_ops,
1210 };
1211
1212 static const struct media_entity_operations imx290_subdev_entity_ops = {
1213         .link_validate = v4l2_subdev_link_validate,
1214 };
1215
1216 /*
1217  * Returns 0 if all link frequencies used by the driver for the given number
1218  * of MIPI data lanes are mentioned in the device tree, or the value of the
1219  * first missing frequency otherwise.
1220  */
1221 static s64 imx290_check_link_freqs(const struct imx290 *imx290,
1222                                    const struct v4l2_fwnode_endpoint *ep)
1223 {
1224         int i, j;
1225         const s64 *freqs = imx290_link_freqs_ptr(imx290);
1226         int freqs_count = imx290_link_freqs_num(imx290);
1227
1228         for (i = 0; i < freqs_count; i++) {
1229                 for (j = 0; j < ep->nr_of_link_frequencies; j++)
1230                         if (freqs[i] == ep->link_frequencies[j])
1231                                 break;
1232                 if (j == ep->nr_of_link_frequencies)
1233                         return freqs[i];
1234         }
1235         return 0;
1236 }
1237
1238 static const struct of_device_id imx290_of_match[] = {
1239         /*
1240          * imx327 supports 1080p60 at 10 and 12bit.
1241          * imx290 adds 10bit 1080p120.
1242          * imx462 adds 10 and 12bit 1080p120.
1243          * This driver currently maxes out at 1080p60, which is supported by all
1244          * of them, but add the compatible strings for future implementation.
1245          */
1246         { .compatible = "sony,imx327", .data = imx290_colour_formats },
1247         { .compatible = "sony,imx327-mono", .data = imx290_mono_formats },
1248         { .compatible = "sony,imx290", .data = imx290_colour_formats },
1249         { .compatible = "sony,imx290-mono", .data = imx290_mono_formats },
1250         { .compatible = "sony,imx462", .data = imx290_colour_formats },
1251         { .compatible = "sony,imx462-mono", .data = imx290_mono_formats },
1252         { /* sentinel */ }
1253 };
1254
1255 static int imx290_probe(struct i2c_client *client)
1256 {
1257         struct v4l2_fwnode_device_properties props;
1258         struct device *dev = &client->dev;
1259         struct fwnode_handle *endpoint;
1260         /* Only CSI2 is supported for now: */
1261         struct v4l2_fwnode_endpoint ep = {
1262                 .bus_type = V4L2_MBUS_CSI2_DPHY
1263         };
1264         const struct of_device_id *match;
1265         const struct imx290_mode *mode;
1266         struct imx290 *imx290;
1267         s64 fq;
1268         int ret;
1269
1270         imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
1271         if (!imx290)
1272                 return -ENOMEM;
1273
1274         imx290->dev = dev;
1275         imx290->regmap = devm_regmap_init_i2c(client, &imx290_regmap_config);
1276         if (IS_ERR(imx290->regmap)) {
1277                 dev_err(dev, "Unable to initialize I2C\n");
1278                 return -ENODEV;
1279         }
1280
1281         match = of_match_device(imx290_of_match, dev);
1282         if (!match)
1283                 return -ENODEV;
1284         imx290->formats = (const struct imx290_pixfmt *)match->data;
1285
1286         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1287         if (!endpoint) {
1288                 dev_err(dev, "Endpoint node not found\n");
1289                 return -EINVAL;
1290         }
1291
1292         ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
1293         fwnode_handle_put(endpoint);
1294         if (ret == -ENXIO) {
1295                 dev_err(dev, "Unsupported bus type, should be CSI2\n");
1296                 goto free_err;
1297         } else if (ret) {
1298                 dev_err(dev, "Parsing endpoint node failed\n");
1299                 goto free_err;
1300         }
1301
1302         /* Get number of data lanes */
1303         imx290->nlanes = ep.bus.mipi_csi2.num_data_lanes;
1304         if (imx290->nlanes != 2 && imx290->nlanes != 4) {
1305                 dev_err(dev, "Invalid data lanes: %d\n", imx290->nlanes);
1306                 ret = -EINVAL;
1307                 goto free_err;
1308         }
1309         imx290->hmax_min = IMX290_HMAX_MIN;
1310
1311         dev_dbg(dev, "Using %u data lanes\n", imx290->nlanes);
1312
1313         if (!ep.nr_of_link_frequencies) {
1314                 dev_err(dev, "link-frequency property not found in DT\n");
1315                 ret = -EINVAL;
1316                 goto free_err;
1317         }
1318
1319         /* Check that link frequences for all the modes are in device tree */
1320         fq = imx290_check_link_freqs(imx290, &ep);
1321         if (fq) {
1322                 dev_err(dev, "Link frequency of %lld is not supported\n", fq);
1323                 ret = -EINVAL;
1324                 goto free_err;
1325         }
1326
1327         /* get system clock (xclk) */
1328         imx290->xclk = devm_clk_get(dev, "xclk");
1329         if (IS_ERR(imx290->xclk)) {
1330                 dev_err(dev, "Could not get xclk");
1331                 ret = PTR_ERR(imx290->xclk);
1332                 goto free_err;
1333         }
1334
1335         ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1336                                        &imx290->xclk_freq);
1337         if (ret) {
1338                 dev_err(dev, "Could not get xclk frequency\n");
1339                 goto free_err;
1340         }
1341
1342         /* external clock must be 37.125 MHz */
1343         if (imx290->xclk_freq != 37125000 && imx290->xclk_freq != 74250000) {
1344                 dev_err(dev, "External clock frequency %u is not supported\n",
1345                         imx290->xclk_freq);
1346                 ret = -EINVAL;
1347                 goto free_err;
1348         }
1349
1350         ret = clk_set_rate(imx290->xclk, imx290->xclk_freq);
1351         if (ret) {
1352                 dev_err(dev, "Could not set xclk frequency\n");
1353                 goto free_err;
1354         }
1355
1356         ret = imx290_get_regulators(dev, imx290);
1357         if (ret < 0) {
1358                 dev_err(dev, "Cannot get regulators\n");
1359                 goto free_err;
1360         }
1361
1362         imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
1363                                                    GPIOD_OUT_HIGH);
1364         if (IS_ERR(imx290->rst_gpio)) {
1365                 dev_err(dev, "Cannot get reset gpio\n");
1366                 ret = PTR_ERR(imx290->rst_gpio);
1367                 goto free_err;
1368         }
1369
1370         mutex_init(&imx290->lock);
1371
1372         /*
1373          * Initialize the frame format. In particular, imx290->current_mode
1374          * and imx290->bpp are set to defaults: imx290_calc_pixel_rate() call
1375          * below relies on these fields.
1376          */
1377         imx290_entity_init_cfg(&imx290->sd, NULL);
1378
1379         v4l2_ctrl_handler_init(&imx290->ctrls, 11);
1380
1381         v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1382                           V4L2_CID_ANALOGUE_GAIN, 0, 100, 1, 0);
1383
1384         mode = imx290->current_mode;
1385         imx290->hblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1386                                            V4L2_CID_HBLANK,
1387                                            imx290->hmax_min - mode->width,
1388                                            IMX290_HMAX_MAX - mode->width, 1,
1389                                            mode->hmax - mode->width);
1390
1391         imx290->vblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1392                                            V4L2_CID_VBLANK,
1393                                            mode->vmax - mode->height,
1394                                            IMX290_VMAX_MAX - mode->height, 1,
1395                                            mode->vmax - mode->height);
1396
1397         imx290->exposure = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1398                                              V4L2_CID_EXPOSURE,
1399                                              IMX290_EXPOSURE_MIN,
1400                                              mode->vmax - 2,
1401                                              IMX290_EXPOSURE_STEP,
1402                                              mode->vmax - 2);
1403
1404         imx290->hflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1405                                           V4L2_CID_HFLIP, 0, 1, 1, 0);
1406         imx290->vflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1407                                           V4L2_CID_VFLIP, 0, 1, 1, 0);
1408
1409         imx290->link_freq =
1410                 v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
1411                                        V4L2_CID_LINK_FREQ,
1412                                        imx290_link_freqs_num(imx290) - 1, 0,
1413                                        imx290_link_freqs_ptr(imx290));
1414         if (imx290->link_freq)
1415                 imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1416
1417         imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
1418                                                V4L2_CID_PIXEL_RATE,
1419                                                1, INT_MAX, 1,
1420                                                imx290_calc_pixel_rate(imx290));
1421
1422         v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
1423                                      V4L2_CID_TEST_PATTERN,
1424                                      ARRAY_SIZE(imx290_test_pattern_menu) - 1,
1425                                      0, 0, imx290_test_pattern_menu);
1426
1427         ret = v4l2_fwnode_device_parse(&client->dev, &props);
1428         if (ret)
1429                 goto free_ctrl;
1430
1431         ret = v4l2_ctrl_new_fwnode_properties(&imx290->ctrls, &imx290_ctrl_ops,
1432                                               &props);
1433         if (ret)
1434                 goto free_ctrl;
1435
1436         imx290->sd.ctrl_handler = &imx290->ctrls;
1437
1438         if (imx290->ctrls.error) {
1439                 dev_err(dev, "Control initialization error %d\n",
1440                         imx290->ctrls.error);
1441                 ret = imx290->ctrls.error;
1442                 goto free_ctrl;
1443         }
1444
1445         v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
1446         imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1447         imx290->sd.dev = &client->dev;
1448         imx290->sd.entity.ops = &imx290_subdev_entity_ops;
1449         imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1450
1451         imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
1452         ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
1453         if (ret < 0) {
1454                 dev_err(dev, "Could not register media entity\n");
1455                 goto free_ctrl;
1456         }
1457
1458         /* Initialize the frame format (this also sets imx290->current_mode) */
1459         imx290_entity_init_cfg(&imx290->sd, NULL);
1460
1461         ret = v4l2_async_register_subdev(&imx290->sd);
1462         if (ret < 0) {
1463                 dev_err(dev, "Could not register v4l2 device\n");
1464                 goto free_entity;
1465         }
1466
1467         /* Power on the device to match runtime PM state below */
1468         ret = imx290_power_on(dev);
1469         if (ret < 0) {
1470                 dev_err(dev, "Could not power on the device\n");
1471                 goto free_entity;
1472         }
1473
1474         pm_runtime_set_active(dev);
1475         pm_runtime_enable(dev);
1476         pm_runtime_idle(dev);
1477
1478         v4l2_fwnode_endpoint_free(&ep);
1479
1480         return 0;
1481
1482 free_entity:
1483         media_entity_cleanup(&imx290->sd.entity);
1484 free_ctrl:
1485         v4l2_ctrl_handler_free(&imx290->ctrls);
1486         mutex_destroy(&imx290->lock);
1487 free_err:
1488         v4l2_fwnode_endpoint_free(&ep);
1489
1490         return ret;
1491 }
1492
1493 static int imx290_remove(struct i2c_client *client)
1494 {
1495         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1496         struct imx290 *imx290 = to_imx290(sd);
1497
1498         v4l2_async_unregister_subdev(sd);
1499         media_entity_cleanup(&sd->entity);
1500         v4l2_ctrl_handler_free(sd->ctrl_handler);
1501
1502         mutex_destroy(&imx290->lock);
1503
1504         pm_runtime_disable(imx290->dev);
1505         if (!pm_runtime_status_suspended(imx290->dev))
1506                 imx290_power_off(imx290->dev);
1507         pm_runtime_set_suspended(imx290->dev);
1508
1509         return 0;
1510 }
1511
1512 MODULE_DEVICE_TABLE(of, imx290_of_match);
1513
1514 static struct i2c_driver imx290_i2c_driver = {
1515         .probe_new  = imx290_probe,
1516         .remove = imx290_remove,
1517         .driver = {
1518                 .name  = "imx290",
1519                 .pm = &imx290_pm_ops,
1520                 .of_match_table = of_match_ptr(imx290_of_match),
1521         },
1522 };
1523
1524 module_i2c_driver(imx290_i2c_driver);
1525
1526 MODULE_DESCRIPTION("Sony IMX290 CMOS Image Sensor Driver");
1527 MODULE_AUTHOR("FRAMOS GmbH");
1528 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
1529 MODULE_LICENSE("GPL v2");