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