riscv:defconfig:jh7110: Fix the problem of failing to mount fat-fs
[platform/kernel/linux-starfive.git] / drivers / media / platform / starfive / v4l2_driver / sc2235.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4  * Copyright (C) 2014-2017 Mentor Graphics Inc.
5  * Copyright (C) 2021 StarFive Technology Co., Ltd.
6  */
7
8 #include <linux/clk.h>
9 #include <linux/clk-provider.h>
10 #include <linux/clkdev.h>
11 #include <linux/ctype.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include "stfcamss.h"
30
31 /* min/typical/max system clock (xclk) frequencies */
32 #define SC2235_XCLK_MIN                 6000000
33 #define SC2235_XCLK_MAX                 54000000
34
35 #define SC2235_CHIP_ID          (0x2235)
36
37 #define SC2235_REG_CHIP_ID                              0x3107
38 #define SC2235_REG_AEC_PK_MANUAL                0x3e03
39 #define SC2235_REG_AEC_PK_EXPOSURE_HI   0x3e01
40 #define SC2235_REG_AEC_PK_EXPOSURE_LO   0x3e02
41 #define SC2235_REG_AEC_PK_REAL_GAIN             0x3e08
42 #define SC2235_REG_TIMING_HTS                   0x320c
43 #define SC2235_REG_TIMING_VTS                   0x320e
44 #define SC2235_REG_TEST_SET0                    0x4501
45 #define SC2235_REG_TEST_SET1                    0x3902
46 #define SC2235_REG_TIMING_TC_REG21              0x3221
47 #define SC2235_REG_SC_PLL_CTRL0                 0x3039
48 #define SC2235_REG_SC_PLL_CTRL1                 0x303a
49 #define SC2235_REG_STREAM_ON            0x0100
50
51 enum sc2235_mode_id {
52         SC2235_MODE_1080P_1920_1080 = 0,
53         SC2235_NUM_MODES,
54 };
55
56 enum sc2235_frame_rate {
57         SC2235_15_FPS = 0,
58         SC2235_30_FPS,
59         SC2235_60_FPS,
60         SC2235_NUM_FRAMERATES,
61 };
62
63 struct sc2235_pixfmt {
64         u32 code;
65         u32 colorspace;
66 };
67
68 static const struct sc2235_pixfmt sc2235_formats[] = {
69         //{ MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_COLORSPACE_SRGB, },
70         { MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB, },
71 };
72
73 static const int sc2235_framerates[] = {
74         [SC2235_15_FPS] = 15,
75         [SC2235_30_FPS] = 30,
76         [SC2235_60_FPS] = 60,
77 };
78
79 /* regulator supplies */
80 static const char * const sc2235_supply_name[] = {
81         "DOVDD", /* Digital I/O (1.8V) supply */
82         "AVDD",  /* Analog (2.8V) supply */
83         "DVDD",  /* Digital Core (1.5V) supply */
84 };
85
86 #define SC2235_NUM_SUPPLIES ARRAY_SIZE(sc2235_supply_name)
87
88 struct reg_value {
89         u16 reg_addr;
90         u8 val;
91         u8 mask;
92         u32 delay_ms;
93 };
94
95 struct sc2235_mode_info {
96         enum sc2235_mode_id id;
97         u32 hact;
98         u32 htot;
99         u32 vact;
100         u32 vtot;
101         const struct reg_value *reg_data;
102         u32 reg_data_size;
103         u32 max_fps;
104 };
105
106 struct sc2235_ctrls {
107         struct v4l2_ctrl_handler handler;
108         struct v4l2_ctrl *pixel_rate;
109         struct {
110                 struct v4l2_ctrl *auto_exp;
111                 struct v4l2_ctrl *exposure;
112         };
113         struct {
114                 struct v4l2_ctrl *auto_wb;
115                 struct v4l2_ctrl *blue_balance;
116                 struct v4l2_ctrl *red_balance;
117         };
118         struct {
119                 struct v4l2_ctrl *auto_gain;
120                 struct v4l2_ctrl *gain;
121         };
122         struct v4l2_ctrl *brightness;
123         struct v4l2_ctrl *light_freq;
124         struct v4l2_ctrl *saturation;
125         struct v4l2_ctrl *contrast;
126         struct v4l2_ctrl *hue;
127         struct v4l2_ctrl *test_pattern;
128         struct v4l2_ctrl *hflip;
129         struct v4l2_ctrl *vflip;
130 };
131
132 struct sensor_pinctrl_info {
133         struct pinctrl *pinctrl;
134         struct pinctrl_state *reset_state_low;
135         struct pinctrl_state *reset_state_high;
136         bool use_pinctrl;
137 };
138
139 struct sc2235_dev {
140         struct i2c_client *i2c_client;
141         struct v4l2_subdev sd;
142         struct media_pad pad;
143         struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
144         struct clk *xclk; /* system clock to SC2235 */
145         u32 xclk_freq;
146
147         struct regulator_bulk_data supplies[SC2235_NUM_SUPPLIES];
148         struct gpio_desc *reset_gpio;
149         struct gpio_desc *pwdn_gpio;
150         bool   upside_down;
151
152         /* lock to protect all members below */
153         struct mutex lock;
154
155         int power_count;
156
157         struct v4l2_mbus_framefmt fmt;
158         bool pending_fmt_change;
159
160         const struct sc2235_mode_info *current_mode;
161         const struct sc2235_mode_info *last_mode;
162         enum sc2235_frame_rate current_fr;
163         struct v4l2_fract frame_interval;
164
165         struct sc2235_ctrls ctrls;
166
167         u32 prev_sysclk, prev_hts;
168         u32 ae_low, ae_high, ae_target;
169
170         bool pending_mode_change;
171         bool streaming;
172
173         struct sensor_pinctrl_info sc2235_pctrl;
174 };
175
176 int sc2235_sensor_pinctrl_init(
177         struct sensor_pinctrl_info *sensor_pctrl, struct device *dev)
178 {
179         sensor_pctrl->pinctrl = devm_pinctrl_get(dev);
180         if (IS_ERR_OR_NULL(sensor_pctrl->pinctrl)) {
181                 pr_err("Getting pinctrl handle failed\n");
182                 return -EINVAL;
183         }
184
185         sensor_pctrl->reset_state_low
186                 = pinctrl_lookup_state(sensor_pctrl->pinctrl, "reset_low");
187         if (IS_ERR_OR_NULL(sensor_pctrl->reset_state_low)) {
188                 pr_err("Failed to get the reset_low pinctrl handle\n");
189                 return -EINVAL;
190         }
191
192         sensor_pctrl->reset_state_high
193                 = pinctrl_lookup_state(sensor_pctrl->pinctrl, "reset_high");
194         if (IS_ERR_OR_NULL(sensor_pctrl->reset_state_high)) {
195                 pr_err("Failed to get the reset_high pinctrl handle\n");
196                 return -EINVAL;
197         }
198
199         sensor_pctrl->use_pinctrl = true;
200
201         return 0;
202 }
203
204 static inline struct sc2235_dev *to_sc2235_dev(struct v4l2_subdev *sd)
205 {
206         return container_of(sd, struct sc2235_dev, sd);
207 }
208
209 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
210 {
211         return &container_of(ctrl->handler, struct sc2235_dev,
212                                 ctrls.handler)->sd;
213 }
214
215 /* sc2235 initial register 7fps*/
216 static struct reg_value sc2235_init_tbl_1080p_7fps[] = {
217         {0x0103, 0x01, 0, 0},
218         {0x0100, 0x00, 0, 0},
219         {0x3039, 0x75, 0, 0},
220         {0x320c, 0x08, 0, 0},
221         {0x320d, 0x98, 0, 0},
222         {0x3222, 0x29, 0, 0},
223         {0x3235, 0x04, 0, 0},
224         {0x3236, 0x63, 0, 0},
225         {0x3237, 0x08, 0, 0},
226         {0x3238, 0x68, 0, 0},
227         {0x3301, 0x04, 0, 0},
228         {0x3303, 0x20, 0, 0},
229         {0x3306, 0x1a, 0, 0},
230         {0x3309, 0xa0, 0, 0},
231         {0x330b, 0x54, 0, 0},
232         {0x330e, 0x30, 0, 0},
233         {0x3313, 0x05, 0, 0},
234         {0x331e, 0x0d, 0, 0},
235         {0x331f, 0x8d, 0, 0},
236         {0x3320, 0x0f, 0, 0},
237         {0x3321, 0x8f, 0, 0},
238         {0x3340, 0x06, 0, 0},
239         {0x3341, 0x50, 0, 0},
240         {0x3342, 0x04, 0, 0},
241         {0x3343, 0x20, 0, 0},
242         {0x3348, 0x07, 0, 0},
243         {0x3349, 0x80, 0, 0},
244         {0x334a, 0x04, 0, 0},
245         {0x334b, 0x20, 0, 0},
246         {0x335e, 0x01, 0, 0},
247         {0x335f, 0x03, 0, 0},
248         {0x3364, 0x05, 0, 0},
249         {0x3366, 0x7c, 0, 0},
250         {0x3367, 0x08, 0, 0},
251         {0x3368, 0x02, 0, 0},
252         {0x3369, 0x00, 0, 0},
253         {0x336a, 0x00, 0, 0},
254         {0x336b, 0x00, 0, 0},
255         {0x337c, 0x04, 0, 0},
256         {0x337d, 0x06, 0, 0},
257         {0x337f, 0x03, 0, 0},
258         {0x3380, 0x04, 0, 0},
259         {0x3381, 0x0a, 0, 0},
260         {0x33a0, 0x05, 0, 0},
261         {0x33b5, 0x10, 0, 0},
262         {0x3621, 0x28, 0, 0},
263         {0x3622, 0x06, 0, 0},
264         {0x3625, 0x02, 0, 0},
265         {0x3630, 0x48, 0, 0},
266         {0x3631, 0x84, 0, 0},
267         {0x3632, 0x88, 0, 0},
268         {0x3633, 0x42, 0, 0},
269         {0x3634, 0x42, 0, 0},
270         {0x3636, 0x24, 0, 0},
271         {0x3635, 0xc1, 0, 0},
272         {0x3637, 0x14, 0, 0},
273         {0x3638, 0x1f, 0, 0},
274         {0x363b, 0x09, 0, 0},
275         {0x3639, 0x09, 0, 0},
276         {0x363c, 0x07, 0, 0},
277         {0x366e, 0x08, 0, 0},
278         {0x3670, 0x08, 0, 0},
279         {0x366f, 0x2f, 0, 0},
280         {0x3677, 0x1f, 0, 0},
281         {0x3678, 0x42, 0, 0},
282         {0x3679, 0x43, 0, 0},
283         {0x367e, 0x07, 0, 0},
284         {0x367f, 0x0f, 0, 0},
285         {0x3802, 0x01, 0, 0},
286         {0x3901, 0x02, 0, 0},
287         {0x3908, 0x11, 0, 0},
288         {0x391b, 0x4d, 0, 0},
289         {0x391e, 0x00, 0, 0},
290         {0x3e01, 0x46, 0, 0},
291         {0x3e03, 0x0b, 0, 0},
292         {0x3f00, 0x07, 0, 0},
293         {0x3f04, 0x08, 0, 0},
294         {0x3f05, 0x74, 0, 0},
295         {0x4500, 0x59, 0, 0},
296         {0x5780, 0xff, 0, 0},
297         {0x5781, 0x04, 0, 0},
298         {0x5785, 0x18, 0, 0},
299         //{0x0100, 0x01, 0, 0},
300         {0x330b, 0x5d, 0, 0},
301         {0x3301, 0x0a, 0, 0},
302         {0x3631, 0x88, 0, 0},
303         {0x366f, 0x2f, 0, 0},
304 };
305
306 /* sc2235 initial register 30fps*/
307 static struct reg_value sc2235_init_regs_tbl_1080[] = {
308         {0x0103, 0x01, 0, 50},
309         {0x0100, 0x00, 0, 0},
310         {0x3039, 0x80, 0, 0},
311         {0x3621, 0x28, 0, 0},
312
313         {0x3309, 0x60, 0, 0},
314         {0x331f, 0x4d, 0, 0},
315         {0x3321, 0x4f, 0, 0},
316         {0x33b5, 0x10, 0, 0},
317
318         {0x3303, 0x20, 0, 0},
319         {0x331e, 0x0d, 0, 0},
320         {0x3320, 0x0f, 0, 0},
321
322         {0x3622, 0x02, 0, 0},
323         {0x3633, 0x42, 0, 0},
324         {0x3634, 0x42, 0, 0},
325
326         {0x3306, 0x66, 0, 0},
327         {0x330b, 0xd1, 0, 0},
328
329         {0x3301, 0x0e, 0, 0},
330
331         {0x320c, 0x08, 0, 0},
332         {0x320d, 0x98, 0, 0},
333
334         {0x3364, 0x05, 0, 0},           // [2] 1: write at sampling ending
335
336         {0x363c, 0x28, 0, 0},           //bypass nvdd
337         {0x363b, 0x0a, 0, 0},           //HVDD
338         {0x3635, 0xa0, 0, 0},           //TXVDD
339
340         {0x4500, 0x59, 0, 0},
341         {0x3d08, 0x00, 0, 0},
342         {0x3908, 0x11, 0, 0},
343
344         {0x363c, 0x08, 0, 0},
345
346         {0x3e03, 0x03, 0, 0},
347         {0x3e01, 0x46, 0, 0},
348
349         //0703
350         {0x3381, 0x0a, 0, 0},
351         {0x3348, 0x09, 0, 0},
352         {0x3349, 0x50, 0, 0},
353         {0x334a, 0x02, 0, 0},
354         {0x334b, 0x60, 0, 0},
355
356         {0x3380, 0x04, 0, 0},
357         {0x3340, 0x06, 0, 0},
358         {0x3341, 0x50, 0, 0},
359         {0x3342, 0x02, 0, 0},
360         {0x3343, 0x60, 0, 0},
361
362         //0707
363
364         {0x3632, 0x88, 0, 0},           //anti sm
365         {0x3309, 0xa0, 0, 0},
366         {0x331f, 0x8d, 0, 0},
367         {0x3321, 0x8f, 0, 0},
368
369         {0x335e, 0x01, 0, 0},           //ana dithering
370         {0x335f, 0x03, 0, 0},
371         {0x337c, 0x04, 0, 0},
372         {0x337d, 0x06, 0, 0},
373         {0x33a0, 0x05, 0, 0},
374         {0x3301, 0x05, 0, 0},
375
376         {0x337f, 0x03, 0, 0},
377         {0x3368, 0x02, 0, 0},
378         {0x3369, 0x00, 0, 0},
379         {0x336a, 0x00, 0, 0},
380         {0x336b, 0x00, 0, 0},
381         {0x3367, 0x08, 0, 0},
382         {0x330e, 0x30, 0, 0},
383
384         {0x3366, 0x7c, 0, 0},           // div_rst gap
385
386         {0x3635, 0xc1, 0, 0},
387         {0x363b, 0x09, 0, 0},
388         {0x363c, 0x07, 0, 0},
389
390         {0x391e, 0x00, 0, 0},
391
392         {0x3637, 0x14, 0, 0},           //fullwell 7K
393
394         {0x3306, 0x54, 0, 0},
395         {0x330b, 0xd8, 0, 0},
396         {0x366e, 0x08, 0, 0},           // ofs auto en [3]
397         {0x366f, 0x2f, 0, 0},
398
399         {0x3631, 0x84, 0, 0},
400         {0x3630, 0x48, 0, 0},
401         {0x3622, 0x06, 0, 0},
402
403         //ramp by sc
404         {0x3638, 0x1f, 0, 0},
405         {0x3625, 0x02, 0, 0},
406         {0x3636, 0x24, 0, 0},
407
408         //0714
409         {0x3348, 0x08, 0, 0},
410         {0x3e03, 0x0b, 0, 0},
411
412         //7.17 fpn
413         {0x3342, 0x03, 0, 0},
414         {0x3343, 0xa0, 0, 0},
415         {0x334a, 0x03, 0, 0},
416         {0x334b, 0xa0, 0, 0},
417
418         //0718
419         {0x3343, 0xb0, 0, 0},
420         {0x334b, 0xb0, 0, 0},
421
422         //0720
423         //digital ctrl
424         {0x3802, 0x01, 0, 0},
425         {0x3235, 0x04, 0, 0},
426         {0x3236, 0x63, 0, 0},           // vts-2
427
428         //fpn
429         {0x3343, 0xd0, 0, 0},
430         {0x334b, 0xd0, 0, 0},
431         {0x3348, 0x07, 0, 0},
432         {0x3349, 0x80, 0, 0},
433
434         //0724
435         {0x391b, 0x4d, 0, 0},
436
437         {0x3342, 0x04, 0, 0},
438         {0x3343, 0x20, 0, 0},
439         {0x334a, 0x04, 0, 0},
440         {0x334b, 0x20, 0, 0},
441
442         //0804
443         {0x3222, 0x29, 0, 0},
444         {0x3901, 0x02, 0, 0},
445
446         //0808
447
448         // auto blc
449         {0x3900, 0xD5, 0, 0},           // Bit[0]: blc_enable
450         {0x3902, 0x45, 0, 0},           // Bit[6]: blc_auto_en
451
452         // blc target
453         {0x3907, 0x00, 0, 0},
454         {0x3908, 0x00, 0, 0},
455
456         // auto dpc
457         {0x5000, 0x00, 0, 0},           // Bit[2]: white dead pixel cancel enable, Bit[1]: black dead pixel cancel enable
458
459         //digital ctrl
460         {0x3f00, 0x07, 0, 0},           // bit[2] = 1
461         {0x3f04, 0x08, 0, 0},
462         {0x3f05, 0x74, 0, 0},           // hts - { 0x24
463
464         //0809
465         {0x330b, 0xc8, 0, 0},
466
467         //0817
468         {0x3306, 0x4a, 0, 0},
469         {0x330b, 0xca, 0, 0},
470         {0x3639, 0x09, 0, 0},
471
472         //manual DPC
473         {0x5780, 0xff, 0, 0},
474         {0x5781, 0x04, 0, 0},
475         {0x5785, 0x18, 0, 0},
476
477         //0822
478         {0x3039, 0x35, 0, 0},           //fps
479         {0x303a, 0x2e, 0, 0},
480         {0x3034, 0x05, 0, 0},
481         {0x3035, 0x2a, 0, 0},
482
483         {0x320c, 0x08, 0, 0},
484         {0x320d, 0xca, 0, 0},
485         {0x320e, 0x04, 0, 0},
486         {0x320f, 0xb0, 0, 0},
487
488         {0x3f04, 0x08, 0, 0},
489         {0x3f05, 0xa6, 0, 0},           // hts - { 0x24
490
491         {0x3235, 0x04, 0, 0},
492         {0x3236, 0xae, 0, 0},           // vts-2
493
494         //0825
495         {0x3313, 0x05, 0, 0},
496         {0x3678, 0x42, 0, 0},
497
498         //for AE control per frame
499         {0x3670, 0x00, 0, 0},
500         {0x3633, 0x42, 0, 0},
501
502         {0x3802, 0x00, 0, 0},
503
504         //20180126
505         {0x3677, 0x3f, 0, 0},
506         {0x3306, 0x44, 0, 0},           //20180126[3c },4a]
507         {0x330b, 0xca, 0, 0},           //20180126[c2 },d3]
508
509         //20180202
510         {0x3237, 0x08, 0, 0},
511         {0x3238, 0x9a, 0, 0},           //hts-0x30
512
513         //20180417
514         {0x3640, 0x01, 0, 0},
515         {0x3641, 0x02, 0, 0},
516
517         {0x3301, 0x12, 0, 0},           //[8 },15]20180126
518         {0x3631, 0x84, 0, 0},
519         {0x366f, 0x2f, 0, 0},
520         {0x3622, 0xc6, 0, 0},           //20180117
521
522         {0x3e03, 0x03, 0, 0},           // Bit[3]: AGC table mapping method, Bit[1]: AGC manual, BIt[0]: AEC manual
523
524         // {0x0100, 0x00, 0, 0},
525         // {0x4501, 0xc8, 0, 0},        //bar testing
526         // {0x3902, 0x45, 0, 0},
527 };
528
529 static struct reg_value sc2235_setting_1080P_1920_1080[] = {
530
531 };
532
533 /* power-on sensor init reg table */
534 static const struct sc2235_mode_info sc2235_mode_init_data = {
535         SC2235_MODE_1080P_1920_1080,
536         1920, 0x8ca, 1080, 0x4b0,
537         sc2235_init_regs_tbl_1080,
538         ARRAY_SIZE(sc2235_init_regs_tbl_1080),
539         SC2235_60_FPS,
540 };
541
542 static const struct sc2235_mode_info
543 sc2235_mode_data[SC2235_NUM_MODES] = {
544         {SC2235_MODE_1080P_1920_1080,
545          1920, 0x8ca, 1080, 0x4b0,
546          sc2235_setting_1080P_1920_1080,
547          ARRAY_SIZE(sc2235_setting_1080P_1920_1080),
548          SC2235_60_FPS},
549 };
550
551 static int sc2235_write_reg(struct sc2235_dev *sensor, u16 reg, u8 val)
552 {
553         struct i2c_client *client = sensor->i2c_client;
554         struct i2c_msg msg;
555         u8 buf[3];
556         int ret;
557
558         buf[0] = reg >> 8;
559         buf[1] = reg & 0xff;
560         buf[2] = val;
561
562         msg.addr = client->addr;
563         msg.flags = client->flags;
564         msg.buf = buf;
565         msg.len = sizeof(buf);
566
567         ret = i2c_transfer(client->adapter, &msg, 1);
568         if (ret < 0) {
569                 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
570                         __func__, reg, val);
571                 return ret;
572         }
573
574         return 0;
575 }
576
577 static int sc2235_read_reg(struct sc2235_dev *sensor, u16 reg, u8 *val)
578 {
579         struct i2c_client *client = sensor->i2c_client;
580         struct i2c_msg msg[2];
581         u8 buf[2];
582         int ret;
583
584         buf[0] = reg >> 8;
585         buf[1] = reg & 0xff;
586
587         msg[0].addr = client->addr;
588         msg[0].flags = client->flags;
589         msg[0].buf = buf;
590         msg[0].len = sizeof(buf);
591
592         msg[1].addr = client->addr;
593         msg[1].flags = client->flags | I2C_M_RD;
594         msg[1].buf = buf;
595         msg[1].len = 1;
596
597         ret = i2c_transfer(client->adapter, msg, 2);
598         if (ret < 0) {
599                 dev_err(&client->dev, "%s: error: reg=%x\n",
600                         __func__, reg);
601                 return ret;
602         }
603
604         *val = buf[0];
605         return 0;
606 }
607
608 static int sc2235_read_reg16(struct sc2235_dev *sensor, u16 reg, u16 *val)
609 {
610         u8 hi, lo;
611         int ret;
612
613         ret = sc2235_read_reg(sensor, reg, &hi);
614         if (ret)
615                 return ret;
616         ret = sc2235_read_reg(sensor, reg + 1, &lo);
617         if (ret)
618                 return ret;
619
620         *val = ((u16)hi << 8) | (u16)lo;
621         return 0;
622 }
623
624 static int sc2235_write_reg16(struct sc2235_dev *sensor, u16 reg, u16 val)
625 {
626         int ret;
627
628         ret = sc2235_write_reg(sensor, reg, val >> 8);
629         if (ret)
630                 return ret;
631
632         return sc2235_write_reg(sensor, reg + 1, val & 0xff);
633 }
634
635 static int sc2235_mod_reg(struct sc2235_dev *sensor, u16 reg,
636                         u8 mask, u8 val)
637 {
638         u8 readval;
639         int ret;
640
641         ret = sc2235_read_reg(sensor, reg, &readval);
642         if (ret)
643                 return ret;
644
645         readval &= ~mask;
646         val &= mask;
647         val |= readval;
648
649         return sc2235_write_reg(sensor, reg, val);
650 }
651
652 #define SC2235_PLL_PREDIV       3
653
654 #define SC2235_SYSDIV_MIN       0
655 #define SC2235_SYSDIV_MAX       7
656
657 #define SC2235_PLL_MULT_MIN     0
658 #define SC2235_PLL_MULT_MAX     63
659
660 #ifdef UNUSED_CODE
661 static unsigned long sc2235_compute_sys_clk(struct sc2235_dev *sensor,
662                                         u8 pll_pre, u8 pll_mult,
663                                         u8 sysdiv)
664 {
665         unsigned long sysclk =
666                 sensor->xclk_freq * (64 - pll_mult) / (pll_pre * (sysdiv + 1));
667
668         /* PLL1 output cannot exceed 1GHz. */
669         if (sysclk / 1000000 > 1000)
670                 return 0;
671
672         return sysclk;
673 }
674
675 static unsigned long sc2235_calc_sys_clk(struct sc2235_dev *sensor,
676                                         unsigned long rate,
677                                         u8 *pll_prediv, u8 *pll_mult,
678                                         u8 *sysdiv)
679 {
680         unsigned long best = ~0;
681         u8 best_sysdiv = 1, best_mult = 1;
682         u8 _sysdiv, _pll_mult;
683
684         for (_sysdiv = SC2235_SYSDIV_MIN;
685                 _sysdiv <= SC2235_SYSDIV_MAX;
686                 _sysdiv++) {
687                 for (_pll_mult = SC2235_PLL_MULT_MIN;
688                         _pll_mult <= SC2235_PLL_MULT_MAX;
689                         _pll_mult++) {
690                         unsigned long _rate;
691
692                         _rate = sc2235_compute_sys_clk(sensor,
693                                                         SC2235_PLL_PREDIV,
694                                                         _pll_mult, _sysdiv);
695
696                         /*
697                          * We have reached the maximum allowed PLL1 output,
698                          * increase sysdiv.
699                          */
700                         if (!_rate)
701                                 break;
702
703                         /*
704                          * Prefer rates above the expected clock rate than
705                          * below, even if that means being less precise.
706                          */
707                         if (_rate < rate)
708                                 continue;
709
710                         if (abs(rate - _rate) < abs(rate - best)) {
711                                 best = _rate;
712                                 best_sysdiv = _sysdiv;
713                                 best_mult = _pll_mult;
714                         }
715
716                         if (_rate == rate)
717                                 goto out;
718                 }
719         }
720
721 out:
722         *sysdiv = best_sysdiv;
723         *pll_prediv = SC2235_PLL_PREDIV;
724         *pll_mult = best_mult;
725
726         return best;
727 }
728 #endif
729
730 static int sc2235_set_timings(struct sc2235_dev *sensor,
731                                 const struct sc2235_mode_info *mode)
732 {
733         int ret = 0;
734
735         return ret;
736 }
737
738 static int sc2235_load_regs(struct sc2235_dev *sensor,
739                                 const struct sc2235_mode_info *mode)
740 {
741         const struct reg_value *regs = mode->reg_data;
742         unsigned int i;
743         u32 delay_ms;
744         u16 reg_addr;
745         u8 mask, val;
746         int ret = 0;
747
748         st_info(ST_SENSOR, "%s, mode = 0x%x\n", __func__, mode->id);
749         for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
750                 delay_ms = regs->delay_ms;
751                 reg_addr = regs->reg_addr;
752                 val = regs->val;
753                 mask = regs->mask;
754
755                 if (mask)
756                         ret = sc2235_mod_reg(sensor, reg_addr, mask, val);
757                 else
758                         ret = sc2235_write_reg(sensor, reg_addr, val);
759                 if (ret)
760                         break;
761
762                 if (delay_ms)
763                         usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
764         }
765
766         return sc2235_set_timings(sensor, mode);
767 }
768
769 static int sc2235_set_autoexposure(struct sc2235_dev *sensor, bool on)
770 {
771         return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
772                                 BIT(0), on ? 0 : BIT(0));
773 }
774
775 static int sc2235_get_exposure(struct sc2235_dev *sensor)
776 {
777         int exp = 0, ret = 0;
778         u8 temp;
779
780         ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_HI, &temp);
781         if (ret)
782                 return ret;
783         exp |= (int)temp << 8;
784         ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_LO, &temp);
785         if (ret)
786                 return ret;
787         exp |= (int)temp;
788
789         return exp >> 4;
790 }
791
792 static int sc2235_set_exposure(struct sc2235_dev *sensor, u32 exposure)
793 {
794         int ret;
795
796         exposure <<= 4;
797
798         ret = sc2235_write_reg(sensor,
799                                 SC2235_REG_AEC_PK_EXPOSURE_LO,
800                                 exposure & 0xff);
801         if (ret)
802                 return ret;
803         return sc2235_write_reg(sensor,
804                                 SC2235_REG_AEC_PK_EXPOSURE_HI,
805                                 (exposure >> 8) & 0xff);
806 }
807
808 static int sc2235_get_gain(struct sc2235_dev *sensor)
809 {
810         u16 gain;
811         int ret;
812
813         ret = sc2235_read_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN, &gain);
814         if (ret)
815                 return ret;
816
817         return gain & 0x1fff;
818 }
819
820 static int sc2235_set_gain(struct sc2235_dev *sensor, int gain)
821 {
822         return sc2235_write_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN,
823                                         (u16)gain & 0x1fff);
824 }
825
826 static int sc2235_set_autogain(struct sc2235_dev *sensor, bool on)
827 {
828         return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
829                                 BIT(1), on ? 0 : BIT(1));
830 }
831
832 static int sc2235_set_stream_dvp(struct sc2235_dev *sensor, bool on)
833 {
834         return sc2235_mod_reg(sensor, SC2235_REG_STREAM_ON,
835                                 BIT(0), on);
836 }
837
838 #ifdef UNUSED_CODE
839 static int sc2235_get_hts(struct sc2235_dev *sensor)
840 {
841         u16 hts;
842         int ret;
843
844         ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_HTS, &hts);
845         if (ret)
846                 return ret;
847         return hts;
848 }
849 #endif
850
851 static int sc2235_get_vts(struct sc2235_dev *sensor)
852 {
853         u16 vts;
854         int ret;
855
856         ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_VTS, &vts);
857         if (ret)
858                 return ret;
859         return vts;
860 }
861
862 #ifdef UNUSED_CODE
863 static int sc2235_set_vts(struct sc2235_dev *sensor, int vts)
864 {
865         return sc2235_write_reg16(sensor, SC2235_REG_TIMING_VTS, vts);
866 }
867 #endif
868
869 static const struct sc2235_mode_info *
870 sc2235_find_mode(struct sc2235_dev *sensor, enum sc2235_frame_rate fr,
871                  int width, int height, bool nearest)
872 {
873         const struct sc2235_mode_info *mode;
874
875         mode = v4l2_find_nearest_size(sc2235_mode_data,
876                                         ARRAY_SIZE(sc2235_mode_data),
877                                         hact, vact,
878                                         width, height);
879
880         if (!mode ||
881                 (!nearest && (mode->hact != width || mode->vact != height)))
882                 return NULL;
883
884         /* Check to see if the current mode exceeds the max frame rate */
885         if (sc2235_framerates[fr] > sc2235_framerates[mode->max_fps])
886                 return NULL;
887
888         return mode;
889 }
890
891 static u64 sc2235_calc_pixel_rate(struct sc2235_dev *sensor)
892 {
893         u64 rate;
894
895         rate = sensor->current_mode->vtot * sensor->current_mode->htot;
896         rate *= sc2235_framerates[sensor->current_fr];
897
898         return rate;
899 }
900
901 #ifdef UNUSED_CODE
902 /*
903  * sc2235_set_dvp_pclk() - Calculate the clock tree configuration values
904  *                              for the dvp output.
905  *
906  * @rate: The requested bandwidth per lane in bytes per second.
907  *      'Bandwidth Per Lane' is calculated as:
908  *      rate = HTOT * VTOT * FPS;
909  *
910  * This function use the requested bandwidth to calculate:
911  * - rate = xclk * (64 - M) / (N * (S + 1));
912  *
913  */
914
915 #define PLL_PREDIV  1
916 #define PLL_SYSEL   0
917
918 static int sc2235_set_dvp_pclk(struct sc2235_dev *sensor,
919                                 unsigned long rate)
920 {
921         u8 prediv, mult, sysdiv;
922         int ret = 0;
923
924         sc2235_calc_sys_clk(sensor, rate, &prediv, &mult,
925                                 &sysdiv);
926
927         st_info(ST_SENSOR, "%s, prediv = %d, mult = %d, sysdiv = %d\n",
928                         __func__, prediv, mult, sysdiv);
929
930         ret = sc2235_mod_reg(sensor, SC2235_REG_SC_PLL_CTRL0, 0x7f,
931                         (sysdiv << 4) | (prediv << 1) | ((mult & 0x20) >> 5));
932         if (ret)
933                 return ret;
934
935         return sc2235_mod_reg(sensor, SC2235_REG_SC_PLL_CTRL1,
936                         0xf8, mult << 3);
937 }
938
939 /*
940  * if sensor changes inside scaling or subsampling
941  * change mode directly
942  */
943 static int sc2235_set_mode_direct(struct sc2235_dev *sensor,
944                                 const struct sc2235_mode_info *mode)
945 {
946         if (!mode->reg_data)
947                 return -EINVAL;
948
949         /* Write capture setting */
950         return sc2235_load_regs(sensor, mode);
951 }
952 #endif
953
954 static int sc2235_set_mode(struct sc2235_dev *sensor)
955 {
956 #ifdef UNUSED_CODE
957         bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
958         const struct sc2235_mode_info *mode = sensor->current_mode;
959 #endif
960         bool auto_gain = sensor->ctrls.auto_gain->val == 1;
961         int ret = 0;
962
963         /* auto gain and exposure must be turned off when changing modes */
964         if (auto_gain) {
965                 ret = sc2235_set_autogain(sensor, false);
966                 if (ret)
967                         return ret;
968         }
969 #ifdef UNUSED_CODE
970         /* This issue will be addressed in the EVB board*/
971         /* This action will result in poor image display 2021 1111*/
972         if (auto_exp) {
973                 ret = sc2235_set_autoexposure(sensor, false);
974                 if (ret)
975                         goto restore_auto_gain;
976         }
977
978         rate = sc2235_calc_pixel_rate(sensor);
979         if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
980                 ret = sc2235_set_dvp_pclk(sensor, rate);
981
982
983         if (ret < 0)
984                 return 0;
985
986         ret = sc2235_set_mode_direct(sensor, mode);
987         if (ret < 0)
988                 goto restore_auto_exp_gain;
989
990
991         /* restore auto gain and exposure */
992         if (auto_gain)
993                 sc2235_set_autogain(sensor, true);
994         if (auto_exp)
995                 sc2235_set_autoexposure(sensor, true);
996
997
998         sensor->pending_mode_change = false;
999         sensor->last_mode = mode;
1000         return 0;
1001
1002 restore_auto_exp_gain:
1003         if (auto_exp)
1004                 sc2235_set_autoexposure(sensor, true);
1005 restore_auto_gain:
1006         if (auto_gain)
1007                 sc2235_set_autogain(sensor, true);
1008 #endif
1009         return ret;
1010 }
1011
1012 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
1013                                 struct v4l2_mbus_framefmt *format);
1014
1015 /* restore the last set video mode after chip power-on */
1016 static int sc2235_restore_mode(struct sc2235_dev *sensor)
1017 {
1018         int ret;
1019
1020         /* first load the initial register values */
1021         ret = sc2235_load_regs(sensor, &sc2235_mode_init_data);
1022         if (ret < 0)
1023                 return ret;
1024         sensor->last_mode = &sc2235_mode_init_data;
1025         /* now restore the last capture mode */
1026         ret = sc2235_set_mode(sensor);
1027         if (ret < 0)
1028                 return ret;
1029
1030         return sc2235_set_framefmt(sensor, &sensor->fmt);
1031 }
1032
1033 static void sc2235_power(struct sc2235_dev *sensor, bool enable)
1034 {
1035         if (!sensor->pwdn_gpio)
1036                 return;
1037         gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1038 }
1039
1040 static void sc2235_reset(struct sc2235_dev *sensor)
1041 {
1042         if (!sensor->reset_gpio)
1043                 return;
1044
1045         gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1046
1047         /* camera power cycle */
1048         sc2235_power(sensor, false);
1049         usleep_range(5000, 10000);
1050         sc2235_power(sensor, true);
1051         usleep_range(5000, 10000);
1052
1053         gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1054         usleep_range(1000, 2000);
1055
1056         gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1057         usleep_range(20000, 25000);
1058 }
1059
1060 static int sc2235_set_power_on(struct sc2235_dev *sensor)
1061 {
1062         struct i2c_client *client = sensor->i2c_client;
1063         struct sensor_pinctrl_info *sensor_pctrl = &sensor->sc2235_pctrl;
1064         int ret;
1065
1066         ret = clk_prepare_enable(sensor->xclk);
1067         if (ret) {
1068                 dev_err(&client->dev, "%s: failed to enable clock\n",
1069                         __func__);
1070                 return ret;
1071         }
1072
1073         ret = regulator_bulk_enable(SC2235_NUM_SUPPLIES,
1074                                         sensor->supplies);
1075         if (ret) {
1076                 dev_err(&client->dev, "%s: failed to enable regulators\n",
1077                         __func__);
1078                 goto xclk_off;
1079         }
1080
1081         if (sensor_pctrl->use_pinctrl) {
1082                 ret = pinctrl_select_state(
1083                         sensor_pctrl->pinctrl,
1084                         sensor_pctrl->reset_state_high);
1085                 if (ret)
1086                         pr_err("cannot set reset pin to high\n");
1087         } else {
1088                 sc2235_reset(sensor);
1089                 sc2235_power(sensor, true);
1090         }
1091
1092         return 0;
1093
1094 xclk_off:
1095         clk_disable_unprepare(sensor->xclk);
1096         return ret;
1097 }
1098
1099 static void sc2235_set_power_off(struct sc2235_dev *sensor)
1100 {
1101         struct sensor_pinctrl_info *sensor_pctrl = &sensor->sc2235_pctrl;
1102         int ret;
1103
1104         if (sensor_pctrl->use_pinctrl) {
1105                 ret = pinctrl_select_state(
1106                         sensor_pctrl->pinctrl,
1107                         sensor_pctrl->reset_state_low);
1108                 if (ret)
1109                         pr_err("cannot set reset pin to low\n");
1110         } else {
1111                 sc2235_power(sensor, false);
1112         }
1113
1114         regulator_bulk_disable(SC2235_NUM_SUPPLIES, sensor->supplies);
1115         clk_disable_unprepare(sensor->xclk);
1116 }
1117
1118 static int sc2235_set_power_dvp(struct sc2235_dev *sensor, bool on)
1119 {
1120         unsigned int flags = sensor->ep.bus.parallel.flags;
1121         u8 polarities = 0;
1122
1123         /*
1124          * configure parallel port control lines polarity
1125          *
1126          * POLARITY CTRL0
1127          * - [5]:       PCLK polarity (0: active low, 1: active high)
1128          * - [1]:       HREF polarity (0: active low, 1: active high)
1129          * - [0]:       VSYNC polarity (mismatch here between
1130          *              datasheet and hardware, 0 is active high
1131          *              and 1 is active low...)
1132          */
1133         if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1134                 polarities |= BIT(1);
1135         if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1136                 polarities |= BIT(0);
1137         if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1138                 polarities |= BIT(5);
1139
1140         // ret = sc2235_write_reg(sensor,
1141         //              SC2235_REG_POLARITY_CTRL00,
1142         //              polarities);
1143         // if (ret)
1144         //      return ret;
1145
1146         return 0;
1147 }
1148
1149 static int sc2235_set_power(struct sc2235_dev *sensor, bool on)
1150 {
1151         int ret = 0;
1152
1153         if (on) {
1154                 ret = sc2235_set_power_on(sensor);
1155                 if (ret)
1156                         return ret;
1157
1158                 ret = sc2235_restore_mode(sensor);
1159                 if (ret)
1160                         goto power_off;
1161         }
1162
1163         if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
1164                 ret = sc2235_set_power_dvp(sensor, on);
1165         if (ret)
1166                 goto power_off;
1167
1168         if (!on)
1169                 sc2235_set_power_off(sensor);
1170
1171         return 0;
1172
1173 power_off:
1174         sc2235_set_power_off(sensor);
1175
1176         return ret;
1177 }
1178
1179 static int sc2235_s_power(struct v4l2_subdev *sd, int on)
1180 {
1181         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1182         int ret = 0;
1183
1184         mutex_lock(&sensor->lock);
1185
1186         /*
1187          * If the power count is modified from 0 to != 0 or from != 0 to 0,
1188          * update the power state.
1189          */
1190         if (sensor->power_count == !on) {
1191                 ret = sc2235_set_power(sensor, !!on);
1192                 if (ret)
1193                         goto out;
1194         }
1195
1196         /* Update the power count. */
1197         sensor->power_count += on ? 1 : -1;
1198         WARN_ON(sensor->power_count < 0);
1199 out:
1200         mutex_unlock(&sensor->lock);
1201
1202         if (on && !ret && sensor->power_count == 1) {
1203                 /* restore controls */
1204                 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1205         }
1206
1207         return ret;
1208 }
1209
1210 static int sc2235_try_frame_interval(struct sc2235_dev *sensor,
1211                                 struct v4l2_fract *fi,
1212                                 u32 width, u32 height)
1213 {
1214         const struct sc2235_mode_info *mode;
1215         enum sc2235_frame_rate rate = SC2235_15_FPS;
1216         int minfps, maxfps, best_fps, fps;
1217         int i;
1218
1219         minfps = sc2235_framerates[SC2235_15_FPS];
1220         maxfps = sc2235_framerates[SC2235_60_FPS];
1221
1222         if (fi->numerator == 0) {
1223                 fi->denominator = maxfps;
1224                 fi->numerator = 1;
1225                 rate = SC2235_60_FPS;
1226                 goto find_mode;
1227         }
1228
1229         fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
1230                         minfps, maxfps);
1231
1232         best_fps = minfps;
1233         for (i = 0; i < ARRAY_SIZE(sc2235_framerates); i++) {
1234                 int curr_fps = sc2235_framerates[i];
1235
1236                 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
1237                         best_fps = curr_fps;
1238                         rate = i;
1239                 }
1240         }
1241
1242         fi->numerator = 1;
1243         fi->denominator = best_fps;
1244
1245 find_mode:
1246         mode = sc2235_find_mode(sensor, rate, width, height, false);
1247         return mode ? rate : -EINVAL;
1248 }
1249
1250 static int sc2235_get_fmt(struct v4l2_subdev *sd,
1251                         struct v4l2_subdev_state *state,
1252                         struct v4l2_subdev_format *format)
1253 {
1254         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1255         struct v4l2_mbus_framefmt *fmt;
1256
1257         if (format->pad != 0)
1258                 return -EINVAL;
1259
1260         mutex_lock(&sensor->lock);
1261
1262         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1263                 fmt = v4l2_subdev_get_try_format(&sensor->sd, state,
1264                                                 format->pad);
1265         else
1266                 fmt = &sensor->fmt;
1267
1268         format->format = *fmt;
1269
1270         mutex_unlock(&sensor->lock);
1271
1272         return 0;
1273 }
1274
1275 static int sc2235_try_fmt_internal(struct v4l2_subdev *sd,
1276                                 struct v4l2_mbus_framefmt *fmt,
1277                                 enum sc2235_frame_rate fr,
1278                                 const struct sc2235_mode_info **new_mode)
1279 {
1280         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1281         const struct sc2235_mode_info *mode;
1282         int i;
1283
1284         mode = sc2235_find_mode(sensor, fr, fmt->width, fmt->height, true);
1285         if (!mode)
1286                 return -EINVAL;
1287         fmt->width = mode->hact;
1288         fmt->height = mode->vact;
1289
1290         if (new_mode)
1291                 *new_mode = mode;
1292
1293         for (i = 0; i < ARRAY_SIZE(sc2235_formats); i++)
1294                 if (sc2235_formats[i].code == fmt->code)
1295                         break;
1296         if (i >= ARRAY_SIZE(sc2235_formats))
1297                 i = 0;
1298
1299         fmt->code = sc2235_formats[i].code;
1300         fmt->colorspace = sc2235_formats[i].colorspace;
1301         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1302         fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1303         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1304
1305         return 0;
1306 }
1307
1308 static int sc2235_set_fmt(struct v4l2_subdev *sd,
1309                         struct v4l2_subdev_state *state,
1310                         struct v4l2_subdev_format *format)
1311 {
1312         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1313         const struct sc2235_mode_info *new_mode;
1314         struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
1315         struct v4l2_mbus_framefmt *fmt;
1316         int ret;
1317
1318         if (format->pad != 0)
1319                 return -EINVAL;
1320         mutex_lock(&sensor->lock);
1321
1322         if (sensor->streaming) {
1323                 ret = -EBUSY;
1324                 goto out;
1325         }
1326
1327         ret = sc2235_try_fmt_internal(sd, mbus_fmt, 0, &new_mode);
1328         if (ret)
1329                 goto out;
1330
1331         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1332                 fmt = v4l2_subdev_get_try_format(sd, state, 0);
1333         else
1334                 fmt = &sensor->fmt;
1335
1336         if (mbus_fmt->code != sensor->fmt.code)
1337                 sensor->pending_fmt_change = true;
1338
1339         *fmt = *mbus_fmt;
1340
1341         if (new_mode != sensor->current_mode) {
1342                 sensor->current_mode = new_mode;
1343                 sensor->pending_mode_change = true;
1344         }
1345         if (new_mode->max_fps < sensor->current_fr) {
1346                 sensor->current_fr = new_mode->max_fps;
1347                 sensor->frame_interval.numerator = 1;
1348                 sensor->frame_interval.denominator =
1349                         sc2235_framerates[sensor->current_fr];
1350                 sensor->current_mode = new_mode;
1351                 sensor->pending_mode_change = true;
1352         }
1353
1354         __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1355                                 sc2235_calc_pixel_rate(sensor));
1356 out:
1357         mutex_unlock(&sensor->lock);
1358         return ret;
1359 }
1360
1361 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
1362                                 struct v4l2_mbus_framefmt *format)
1363 {
1364         int ret = 0;
1365
1366         switch (format->code) {
1367         default:
1368                 return ret;
1369         }
1370         return ret;
1371 }
1372
1373 /*
1374  * Sensor Controls.
1375  */
1376
1377 static int sc2235_set_ctrl_hue(struct sc2235_dev *sensor, int value)
1378 {
1379         int ret = 0;
1380         return ret;
1381 }
1382
1383 static int sc2235_set_ctrl_contrast(struct sc2235_dev *sensor, int value)
1384 {
1385         int ret = 0;
1386         return ret;
1387 }
1388
1389 static int sc2235_set_ctrl_saturation(struct sc2235_dev *sensor, int value)
1390 {
1391         int ret  = 0;
1392         return ret;
1393 }
1394
1395 static int sc2235_set_ctrl_white_balance(struct sc2235_dev *sensor, int awb)
1396 {
1397         int ret = 0;
1398         return ret;
1399 }
1400
1401 static int sc2235_set_ctrl_exposure(struct sc2235_dev *sensor,
1402                                 enum v4l2_exposure_auto_type auto_exposure)
1403 {
1404         struct sc2235_ctrls *ctrls = &sensor->ctrls;
1405         bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
1406         int ret = 0;
1407
1408         if (ctrls->auto_exp->is_new) {
1409                 ret = sc2235_set_autoexposure(sensor, auto_exp);
1410                 if (ret)
1411                         return ret;
1412         }
1413
1414         if (!auto_exp && ctrls->exposure->is_new) {
1415                 u16 max_exp = 0;
1416
1417                 ret = sc2235_get_vts(sensor);
1418                 if (ret < 0)
1419                         return ret;
1420                 max_exp += ret - 4;
1421                 ret = 0;
1422
1423                 if (ctrls->exposure->val < max_exp)
1424                         ret = sc2235_set_exposure(sensor, ctrls->exposure->val);
1425         }
1426
1427         return ret;
1428 }
1429
1430 static int sc2235_set_ctrl_gain(struct sc2235_dev *sensor, bool auto_gain)
1431 {
1432         struct sc2235_ctrls *ctrls = &sensor->ctrls;
1433         int ret = 0;
1434
1435         if (ctrls->auto_gain->is_new) {
1436                 ret = sc2235_set_autogain(sensor, auto_gain);
1437                 if (ret)
1438                         return ret;
1439         }
1440
1441         if (!auto_gain && ctrls->gain->is_new)
1442                 ret = sc2235_set_gain(sensor, ctrls->gain->val);
1443
1444         return ret;
1445 }
1446
1447 static const char * const test_pattern_menu[] = {
1448         "Disabled",
1449         "Black bars",
1450         "Auto Black bars",
1451 };
1452
1453 #define SC2235_TEST_ENABLE              BIT(3)
1454 #define SC2235_TEST_BLACK               (3 << 0)
1455
1456 static int sc2235_set_ctrl_test_pattern(struct sc2235_dev *sensor, int value)
1457 {
1458         int ret = 0;
1459         /*
1460          *For 7110 platform, refer to 1125 FW code configuration. This operation will cause the image to be white.
1461          */
1462 #ifdef UNUSED_CODE
1463         ret = sc2235_mod_reg(sensor, SC2235_REG_TEST_SET0, BIT(3),
1464                                 !!value << 3);
1465
1466         ret |= sc2235_mod_reg(sensor, SC2235_REG_TEST_SET1, BIT(6),
1467                                 (value >> 1) << 6);
1468 #endif
1469         return ret;
1470 }
1471
1472 static int sc2235_set_ctrl_light_freq(struct sc2235_dev *sensor, int value)
1473 {
1474         return 0;
1475 }
1476
1477 static int sc2235_set_ctrl_hflip(struct sc2235_dev *sensor, int value)
1478 {
1479         return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1480                                 BIT(2) | BIT(1),
1481                                 (!(value ^ sensor->upside_down)) ?
1482                                 (BIT(2) | BIT(1)) : 0);
1483 }
1484
1485 static int sc2235_set_ctrl_vflip(struct sc2235_dev *sensor, int value)
1486 {
1487         return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1488                                 BIT(6) | BIT(5),
1489                                 (value ^ sensor->upside_down) ?
1490                                 (BIT(6) | BIT(5)) : 0);
1491 }
1492
1493 static int sc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1494 {
1495         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1496         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1497         int val;
1498
1499         /* v4l2_ctrl_lock() locks our own mutex */
1500
1501         switch (ctrl->id) {
1502         case V4L2_CID_AUTOGAIN:
1503                 val = sc2235_get_gain(sensor);
1504                 if (val < 0)
1505                         return val;
1506                 sensor->ctrls.gain->val = val;
1507                 break;
1508         case V4L2_CID_EXPOSURE_AUTO:
1509                 val = sc2235_get_exposure(sensor);
1510                 if (val < 0)
1511                         return val;
1512                 sensor->ctrls.exposure->val = val;
1513                 break;
1514         }
1515
1516         return 0;
1517 }
1518
1519 static int sc2235_s_ctrl(struct v4l2_ctrl *ctrl)
1520 {
1521         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1522         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1523         int ret;
1524
1525         /* v4l2_ctrl_lock() locks our own mutex */
1526
1527         /*
1528          * If the device is not powered up by the host driver do
1529          * not apply any controls to H/W at this time. Instead
1530          * the controls will be restored right after power-up.
1531          */
1532         if (sensor->power_count == 0)
1533                 return 0;
1534
1535         switch (ctrl->id) {
1536         case V4L2_CID_AUTOGAIN:
1537                 ret = sc2235_set_ctrl_gain(sensor, ctrl->val);
1538                 break;
1539         case V4L2_CID_EXPOSURE_AUTO:
1540                 ret = sc2235_set_ctrl_exposure(sensor, ctrl->val);
1541                 break;
1542         case V4L2_CID_AUTO_WHITE_BALANCE:
1543                 ret = sc2235_set_ctrl_white_balance(sensor, ctrl->val);
1544                 break;
1545         case V4L2_CID_HUE:
1546                 ret = sc2235_set_ctrl_hue(sensor, ctrl->val);
1547                 break;
1548         case V4L2_CID_CONTRAST:
1549                 ret = sc2235_set_ctrl_contrast(sensor, ctrl->val);
1550                 break;
1551         case V4L2_CID_SATURATION:
1552                 ret = sc2235_set_ctrl_saturation(sensor, ctrl->val);
1553                 break;
1554         case V4L2_CID_TEST_PATTERN:
1555                 ret = sc2235_set_ctrl_test_pattern(sensor, ctrl->val);
1556                 break;
1557         case V4L2_CID_POWER_LINE_FREQUENCY:
1558                 ret = sc2235_set_ctrl_light_freq(sensor, ctrl->val);
1559                 break;
1560         case V4L2_CID_HFLIP:
1561                 ret = sc2235_set_ctrl_hflip(sensor, ctrl->val);
1562                 break;
1563         case V4L2_CID_VFLIP:
1564                 ret = sc2235_set_ctrl_vflip(sensor, ctrl->val);
1565                 break;
1566         default:
1567                 ret = -EINVAL;
1568                 break;
1569         }
1570
1571         return ret;
1572 }
1573
1574 static const struct v4l2_ctrl_ops sc2235_ctrl_ops = {
1575         .g_volatile_ctrl = sc2235_g_volatile_ctrl,
1576         .s_ctrl = sc2235_s_ctrl,
1577 };
1578
1579 static int sc2235_init_controls(struct sc2235_dev *sensor)
1580 {
1581         const struct v4l2_ctrl_ops *ops = &sc2235_ctrl_ops;
1582         struct sc2235_ctrls *ctrls = &sensor->ctrls;
1583         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1584         int ret;
1585
1586         v4l2_ctrl_handler_init(hdl, 32);
1587
1588         /* we can use our own mutex for the ctrl lock */
1589         hdl->lock = &sensor->lock;
1590
1591         /* Clock related controls */
1592         ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
1593                                                 0, INT_MAX, 1,
1594                                                 sc2235_calc_pixel_rate(sensor));
1595
1596         /* Auto/manual white balance */
1597         ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1598                                         V4L2_CID_AUTO_WHITE_BALANCE,
1599                                         0, 1, 1, 1);
1600         ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1601                                                 0, 4095, 1, 0);
1602         ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1603                                                 0, 4095, 1, 0);
1604         /* Auto/manual exposure */
1605 #ifdef UNUSED_CODE
1606         /*
1607          *For 7110 platform, This operation will cause the image to be white.
1608          */
1609         ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1610                                                 V4L2_CID_EXPOSURE_AUTO,
1611                                                 V4L2_EXPOSURE_MANUAL, 0,
1612                                                 V4L2_EXPOSURE_AUTO);
1613         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1614                                         0, 65535, 1, 0);
1615         /* Auto/manual gain */
1616         ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1617                                                 0, 1, 1, 1);
1618         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1619                                         0, 1023, 1, 0);
1620 #else
1621         ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1622                                                 V4L2_CID_EXPOSURE_AUTO,
1623                                                 V4L2_EXPOSURE_MANUAL, 0,
1624                                                 1);
1625         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1626                                         0, 65535, 1, 0x4600);
1627         /* Auto/manual gain */
1628         ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1629                                                 0, 1, 1, 0);
1630         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1631                                         0, 1023, 1, 0x10);
1632 #endif
1633         ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
1634                                                 0, 255, 1, 64);
1635         ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
1636                                         0, 359, 1, 0);
1637         ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
1638                                                 0, 255, 1, 0);
1639         ctrls->test_pattern =
1640                 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
1641                                         ARRAY_SIZE(test_pattern_menu) - 1,
1642                                         0, 0, test_pattern_menu);   //0x02
1643         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
1644                                         0, 1, 1, 1);
1645         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
1646                                         0, 1, 1, 0);
1647
1648         ctrls->light_freq =
1649                 v4l2_ctrl_new_std_menu(hdl, ops,
1650                                         V4L2_CID_POWER_LINE_FREQUENCY,
1651                                         V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
1652                                         V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
1653
1654         if (hdl->error) {
1655                 ret = hdl->error;
1656                 goto free_ctrls;
1657         }
1658
1659         ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1660         ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
1661         ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
1662
1663         v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
1664         v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
1665         v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
1666
1667         sensor->sd.ctrl_handler = hdl;
1668         return 0;
1669
1670 free_ctrls:
1671         v4l2_ctrl_handler_free(hdl);
1672         return ret;
1673 }
1674
1675 static int sc2235_enum_frame_size(struct v4l2_subdev *sd,
1676                                 struct v4l2_subdev_state *state,
1677                                 struct v4l2_subdev_frame_size_enum *fse)
1678 {
1679         if (fse->pad != 0)
1680                 return -EINVAL;
1681         if (fse->index >= SC2235_NUM_MODES)
1682                 return -EINVAL;
1683
1684         fse->min_width =
1685                 sc2235_mode_data[fse->index].hact;
1686         fse->max_width = fse->min_width;
1687         fse->min_height =
1688                 sc2235_mode_data[fse->index].vact;
1689         fse->max_height = fse->min_height;
1690
1691         return 0;
1692 }
1693
1694 static int sc2235_enum_frame_interval(
1695         struct v4l2_subdev *sd,
1696         struct v4l2_subdev_state *state,
1697         struct v4l2_subdev_frame_interval_enum *fie)
1698 {
1699         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1700         struct v4l2_fract tpf;
1701         int ret;
1702
1703         if (fie->pad != 0)
1704                 return -EINVAL;
1705         if (fie->index >= SC2235_NUM_FRAMERATES)
1706                 return -EINVAL;
1707
1708         tpf.numerator = 1;
1709         tpf.denominator = sc2235_framerates[fie->index];
1710
1711         ret = sc2235_try_frame_interval(sensor, &tpf,
1712                                         fie->width, fie->height);
1713         if (ret < 0)
1714                 return -EINVAL;
1715
1716         fie->interval = tpf;
1717         return 0;
1718 }
1719
1720 static int sc2235_g_frame_interval(struct v4l2_subdev *sd,
1721                                 struct v4l2_subdev_frame_interval *fi)
1722 {
1723         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1724
1725         mutex_lock(&sensor->lock);
1726         fi->interval = sensor->frame_interval;
1727         mutex_unlock(&sensor->lock);
1728
1729         return 0;
1730 }
1731
1732 static int sc2235_s_frame_interval(struct v4l2_subdev *sd,
1733                                 struct v4l2_subdev_frame_interval *fi)
1734 {
1735         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1736         const struct sc2235_mode_info *mode;
1737         int frame_rate, ret = 0;
1738
1739         if (fi->pad != 0)
1740                 return -EINVAL;
1741
1742         mutex_lock(&sensor->lock);
1743
1744         if (sensor->streaming) {
1745                 ret = -EBUSY;
1746                 goto out;
1747         }
1748
1749         mode = sensor->current_mode;
1750
1751         frame_rate = sc2235_try_frame_interval(sensor, &fi->interval,
1752                                                 mode->hact, mode->vact);
1753         if (frame_rate < 0) {
1754                 /* Always return a valid frame interval value */
1755                 fi->interval = sensor->frame_interval;
1756                 goto out;
1757         }
1758
1759         mode = sc2235_find_mode(sensor, frame_rate, mode->hact,
1760                                 mode->vact, true);
1761         if (!mode) {
1762                 ret = -EINVAL;
1763                 goto out;
1764         }
1765
1766         if (mode != sensor->current_mode ||
1767             frame_rate != sensor->current_fr) {
1768                 sensor->current_fr = frame_rate;
1769                 sensor->frame_interval = fi->interval;
1770                 sensor->current_mode = mode;
1771                 sensor->pending_mode_change = true;
1772
1773                 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1774                                         sc2235_calc_pixel_rate(sensor));
1775         }
1776 out:
1777         mutex_unlock(&sensor->lock);
1778         return ret;
1779 }
1780
1781 static int sc2235_enum_mbus_code(struct v4l2_subdev *sd,
1782                                 struct v4l2_subdev_state *state,
1783                                 struct v4l2_subdev_mbus_code_enum *code)
1784 {
1785         if (code->pad != 0)
1786                 return -EINVAL;
1787         if (code->index >= ARRAY_SIZE(sc2235_formats))
1788                 return -EINVAL;
1789
1790         code->code = sc2235_formats[code->index].code;
1791         return 0;
1792 }
1793
1794 static int sc2235_s_stream(struct v4l2_subdev *sd, int enable)
1795 {
1796         struct sc2235_dev *sensor = to_sc2235_dev(sd);
1797         int ret = 0;
1798
1799         mutex_lock(&sensor->lock);
1800
1801         if (sensor->streaming == !enable) {
1802                 if (enable && sensor->pending_mode_change) {
1803                         ret = sc2235_set_mode(sensor);
1804                         if (ret)
1805                                 goto out;
1806                 }
1807
1808                 if (enable && sensor->pending_fmt_change) {
1809                         ret = sc2235_set_framefmt(sensor, &sensor->fmt);
1810                         if (ret)
1811                                 goto out;
1812                         sensor->pending_fmt_change = false;
1813                 }
1814
1815                 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
1816                         ret = sc2235_set_stream_dvp(sensor, enable);
1817
1818                 if (!ret)
1819                         sensor->streaming = enable;
1820         }
1821 out:
1822         mutex_unlock(&sensor->lock);
1823
1824         return ret;
1825 }
1826
1827 static const struct v4l2_subdev_core_ops sc2235_core_ops = {
1828         .s_power = sc2235_s_power,
1829         .log_status = v4l2_ctrl_subdev_log_status,
1830         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1831         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1832 };
1833
1834 static const struct v4l2_subdev_video_ops sc2235_video_ops = {
1835         .g_frame_interval = sc2235_g_frame_interval,
1836         .s_frame_interval = sc2235_s_frame_interval,
1837         .s_stream = sc2235_s_stream,
1838 };
1839
1840 static const struct v4l2_subdev_pad_ops sc2235_pad_ops = {
1841         .enum_mbus_code = sc2235_enum_mbus_code,
1842         .get_fmt = sc2235_get_fmt,
1843         .set_fmt = sc2235_set_fmt,
1844         .enum_frame_size = sc2235_enum_frame_size,
1845         .enum_frame_interval = sc2235_enum_frame_interval,
1846 };
1847
1848 static const struct v4l2_subdev_ops sc2235_subdev_ops = {
1849         .core = &sc2235_core_ops,
1850         .video = &sc2235_video_ops,
1851         .pad = &sc2235_pad_ops,
1852 };
1853
1854 static int sc2235_get_regulators(struct sc2235_dev *sensor)
1855 {
1856         int i;
1857
1858         for (i = 0; i < SC2235_NUM_SUPPLIES; i++)
1859                 sensor->supplies[i].supply = sc2235_supply_name[i];
1860
1861         return devm_regulator_bulk_get(&sensor->i2c_client->dev,
1862                                         SC2235_NUM_SUPPLIES,
1863                                         sensor->supplies);
1864 }
1865
1866 static int sc2235_check_chip_id(struct sc2235_dev *sensor)
1867 {
1868         struct i2c_client *client = sensor->i2c_client;
1869         int ret = 0;
1870         u16 chip_id;
1871
1872         ret = sc2235_set_power_on(sensor);
1873         if (ret)
1874                 return ret;
1875
1876         ret = sc2235_read_reg16(sensor, SC2235_REG_CHIP_ID, &chip_id);
1877         if (ret) {
1878                 dev_err(&client->dev, "%s: failed to read chip identifier\n",
1879                         __func__);
1880                 goto power_off;
1881         }
1882
1883         if (chip_id != SC2235_CHIP_ID) {
1884                 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x%x, got 0x%x\n",
1885                         __func__, SC2235_CHIP_ID, chip_id);
1886                 ret = -ENXIO;
1887         }
1888         dev_err(&client->dev, "%s: chip identifier, got 0x%x\n",
1889                 __func__, chip_id);
1890
1891 power_off:
1892         sc2235_set_power_off(sensor);
1893         return ret;
1894 }
1895
1896 static int sc2235_probe(struct i2c_client *client)
1897 {
1898         struct device *dev = &client->dev;
1899         struct fwnode_handle *endpoint;
1900         struct sc2235_dev *sensor;
1901         struct v4l2_mbus_framefmt *fmt;
1902         u32 rotation;
1903         int ret;
1904
1905         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1906         if (!sensor)
1907                 return -ENOMEM;
1908
1909         sensor->i2c_client = client;
1910
1911         fmt = &sensor->fmt;
1912         fmt->code = MEDIA_BUS_FMT_SGBRG10_1X10;
1913         fmt->colorspace = V4L2_COLORSPACE_SRGB;
1914         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1915         fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1916         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1917         fmt->width = 1920;
1918         fmt->height = 1080;
1919         fmt->field = V4L2_FIELD_NONE;
1920         sensor->frame_interval.numerator = 1;
1921         sensor->frame_interval.denominator = sc2235_framerates[SC2235_30_FPS];
1922         sensor->current_fr = SC2235_30_FPS;
1923         sensor->current_mode =
1924                 &sc2235_mode_data[SC2235_MODE_1080P_1920_1080];
1925         sensor->last_mode = sensor->current_mode;
1926
1927         sensor->ae_target = 52;
1928
1929         /* optional indication of physical rotation of sensor */
1930         ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
1931                                         &rotation);
1932         if (!ret) {
1933                 switch (rotation) {
1934                 case 180:
1935                         sensor->upside_down = true;
1936                         fallthrough;
1937                 case 0:
1938                         break;
1939                 default:
1940                         dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
1941                                 rotation);
1942                 }
1943         }
1944
1945         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
1946                                                 NULL);
1947         if (!endpoint) {
1948                 dev_err(dev, "endpoint node not found\n");
1949                 return -EINVAL;
1950         }
1951
1952         ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
1953         fwnode_handle_put(endpoint);
1954         if (ret) {
1955                 dev_err(dev, "Could not parse endpoint\n");
1956                 return ret;
1957         }
1958
1959         if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
1960             sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
1961             sensor->ep.bus_type != V4L2_MBUS_BT656) {
1962                 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
1963                 return -EINVAL;
1964         }
1965
1966         /* get system clock (xclk) */
1967         sensor->xclk = devm_clk_get(dev, "xclk");
1968         if (IS_ERR(sensor->xclk)) {
1969                 dev_err(dev, "failed to get xclk\n");
1970                 return PTR_ERR(sensor->xclk);
1971         }
1972
1973         sensor->xclk_freq = clk_get_rate(sensor->xclk);
1974         if (sensor->xclk_freq < SC2235_XCLK_MIN ||
1975             sensor->xclk_freq > SC2235_XCLK_MAX) {
1976                 dev_err(dev, "xclk frequency out of range: %d Hz\n",
1977                         sensor->xclk_freq);
1978                 return -EINVAL;
1979         }
1980
1981         ret = sc2235_sensor_pinctrl_init(&sensor->sc2235_pctrl, dev);
1982         if (ret) {
1983                 pr_err("Can't get pinctrl, use gpio to ctrl\n");
1984                 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1985                 sensor->sc2235_pctrl.use_pinctrl = false;
1986         }
1987
1988         v4l2_i2c_subdev_init(&sensor->sd, client, &sc2235_subdev_ops);
1989
1990         sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1991                         V4L2_SUBDEV_FL_HAS_EVENTS;
1992         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1993         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1994         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
1995         if (ret)
1996                 return ret;
1997
1998         ret = sc2235_get_regulators(sensor);
1999         if (ret)
2000                 return ret;
2001         mutex_init(&sensor->lock);
2002
2003         ret = sc2235_check_chip_id(sensor);
2004         if (ret)
2005                 goto entity_cleanup;
2006
2007         ret = sc2235_init_controls(sensor);
2008         if (ret)
2009                 goto entity_cleanup;
2010
2011         ret = v4l2_async_register_subdev_sensor(&sensor->sd);
2012         if (ret)
2013                 goto free_ctrls;
2014
2015         return 0;
2016
2017 free_ctrls:
2018         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2019 entity_cleanup:
2020         media_entity_cleanup(&sensor->sd.entity);
2021         mutex_destroy(&sensor->lock);
2022         return ret;
2023 }
2024
2025 static int sc2235_remove(struct i2c_client *client)
2026 {
2027         struct v4l2_subdev *sd = i2c_get_clientdata(client);
2028         struct sc2235_dev *sensor = to_sc2235_dev(sd);
2029
2030         v4l2_async_unregister_subdev(&sensor->sd);
2031         media_entity_cleanup(&sensor->sd.entity);
2032         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2033         mutex_destroy(&sensor->lock);
2034
2035         return 0;
2036 }
2037
2038 static const struct i2c_device_id sc2235_id[] = {
2039         {"sc2235", 0},
2040         {},
2041 };
2042 MODULE_DEVICE_TABLE(i2c, sc2235_id);
2043
2044 static const struct of_device_id sc2235_dt_ids[] = {
2045         { .compatible = "sc2235" },
2046         { /* sentinel */ }
2047 };
2048 MODULE_DEVICE_TABLE(of, sc2235_dt_ids);
2049
2050 static struct i2c_driver sc2235_i2c_driver = {
2051         .driver = {
2052                 .name  = "sc2235",
2053                 .of_match_table = sc2235_dt_ids,
2054         },
2055         .id_table = sc2235_id,
2056         .probe_new = sc2235_probe,
2057         .remove   = sc2235_remove,
2058 };
2059
2060 module_i2c_driver(sc2235_i2c_driver);
2061
2062 MODULE_DESCRIPTION("SC2235 MIPI Camera Subdev Driver");
2063 MODULE_LICENSE("GPL");