1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
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>
31 /* min/typical/max system clock (xclk) frequencies */
32 #define SC2235_XCLK_MIN 6000000
33 #define SC2235_XCLK_MAX 54000000
35 #define SC2235_CHIP_ID (0x2235)
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
52 SC2235_MODE_1080P_1920_1080 = 0,
56 enum sc2235_frame_rate {
60 SC2235_NUM_FRAMERATES,
63 struct sc2235_pixfmt {
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, },
73 static const int sc2235_framerates[] = {
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 */
86 #define SC2235_NUM_SUPPLIES ARRAY_SIZE(sc2235_supply_name)
95 struct sc2235_mode_info {
96 enum sc2235_mode_id id;
101 const struct reg_value *reg_data;
106 struct sc2235_ctrls {
107 struct v4l2_ctrl_handler handler;
108 struct v4l2_ctrl *pixel_rate;
110 struct v4l2_ctrl *auto_exp;
111 struct v4l2_ctrl *exposure;
114 struct v4l2_ctrl *auto_wb;
115 struct v4l2_ctrl *blue_balance;
116 struct v4l2_ctrl *red_balance;
119 struct v4l2_ctrl *auto_gain;
120 struct v4l2_ctrl *gain;
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;
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 */
140 struct regulator_bulk_data supplies[SC2235_NUM_SUPPLIES];
141 struct gpio_desc *reset_gpio;
142 struct gpio_desc *pwdn_gpio;
145 /* lock to protect all members below */
150 struct v4l2_mbus_framefmt fmt;
151 bool pending_fmt_change;
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;
158 struct sc2235_ctrls ctrls;
160 u32 prev_sysclk, prev_hts;
161 u32 ae_low, ae_high, ae_target;
163 bool pending_mode_change;
167 static inline struct sc2235_dev *to_sc2235_dev(struct v4l2_subdev *sd)
169 return container_of(sd, struct sc2235_dev, sd);
172 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
174 return &container_of(ctrl->handler, struct sc2235_dev,
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},
185 {0x3309, 0x60, 0, 0},
186 {0x331f, 0x4d, 0, 0},
187 {0x3321, 0x4f, 0, 0},
188 {0x33b5, 0x10, 0, 0},
190 {0x3303, 0x20, 0, 0},
191 {0x331e, 0x0d, 0, 0},
192 {0x3320, 0x0f, 0, 0},
194 {0x3622, 0x02, 0, 0},
195 {0x3633, 0x42, 0, 0},
196 {0x3634, 0x42, 0, 0},
198 {0x3306, 0x66, 0, 0},
199 {0x330b, 0xd1, 0, 0},
201 {0x3301, 0x0e, 0, 0},
203 {0x320c, 0x08, 0, 0},
204 {0x320d, 0x98, 0, 0},
206 {0x3364, 0x05, 0, 0}, // [2] 1: write at sampling ending
208 {0x363c, 0x28, 0, 0}, //bypass nvdd
209 {0x363b, 0x0a, 0, 0}, //HVDD
210 {0x3635, 0xa0, 0, 0}, //TXVDD
212 {0x4500, 0x59, 0, 0},
213 {0x3d08, 0x00, 0, 0},
214 {0x3908, 0x11, 0, 0},
216 {0x363c, 0x08, 0, 0},
218 {0x3e03, 0x03, 0, 0},
219 {0x3e01, 0x46, 0, 0},
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},
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},
236 {0x3632, 0x88, 0, 0}, //anti sm
237 {0x3309, 0xa0, 0, 0},
238 {0x331f, 0x8d, 0, 0},
239 {0x3321, 0x8f, 0, 0},
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},
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},
256 {0x3366, 0x7c, 0, 0}, // div_rst gap
258 {0x3635, 0xc1, 0, 0},
259 {0x363b, 0x09, 0, 0},
260 {0x363c, 0x07, 0, 0},
262 {0x391e, 0x00, 0, 0},
264 {0x3637, 0x14, 0, 0}, //fullwell 7K
266 {0x3306, 0x54, 0, 0},
267 {0x330b, 0xd8, 0, 0},
268 {0x366e, 0x08, 0, 0}, // ofs auto en [3]
269 {0x366f, 0x2f, 0, 0},
271 {0x3631, 0x84, 0, 0},
272 {0x3630, 0x48, 0, 0},
273 {0x3622, 0x06, 0, 0},
276 {0x3638, 0x1f, 0, 0},
277 {0x3625, 0x02, 0, 0},
278 {0x3636, 0x24, 0, 0},
281 {0x3348, 0x08, 0, 0},
282 {0x3e03, 0x0b, 0, 0},
285 {0x3342, 0x03, 0, 0},
286 {0x3343, 0xa0, 0, 0},
287 {0x334a, 0x03, 0, 0},
288 {0x334b, 0xa0, 0, 0},
291 {0x3343, 0xb0, 0, 0},
292 {0x334b, 0xb0, 0, 0},
296 {0x3802, 0x01, 0, 0},
297 {0x3235, 0x04, 0, 0},
298 {0x3236, 0x63, 0, 0}, // vts-2
301 {0x3343, 0xd0, 0, 0},
302 {0x334b, 0xd0, 0, 0},
303 {0x3348, 0x07, 0, 0},
304 {0x3349, 0x80, 0, 0},
307 {0x391b, 0x4d, 0, 0},
309 {0x3342, 0x04, 0, 0},
310 {0x3343, 0x20, 0, 0},
311 {0x334a, 0x04, 0, 0},
312 {0x334b, 0x20, 0, 0},
315 {0x3222, 0x29, 0, 0},
316 {0x3901, 0x02, 0, 0},
321 {0x3900, 0xD5, 0, 0}, // Bit[0]: blc_enable
322 {0x3902, 0x45, 0, 0}, // Bit[6]: blc_auto_en
325 {0x3907, 0x00, 0, 0},
326 {0x3908, 0x00, 0, 0},
329 {0x5000, 0x00, 0, 0}, // Bit[2]: white dead pixel cancel enable, Bit[1]: black dead pixel cancel enable
332 {0x3f00, 0x07, 0, 0}, // bit[2] = 1
333 {0x3f04, 0x08, 0, 0},
334 {0x3f05, 0x74, 0, 0}, // hts - { 0x24
337 {0x330b, 0xc8, 0, 0},
340 {0x3306, 0x4a, 0, 0},
341 {0x330b, 0xca, 0, 0},
342 {0x3639, 0x09, 0, 0},
345 {0x5780, 0xff, 0, 0},
346 {0x5781, 0x04, 0, 0},
347 {0x5785, 0x18, 0, 0},
350 {0x3039, 0x35, 0, 0}, //fps
351 {0x303a, 0x2e, 0, 0},
352 {0x3034, 0x05, 0, 0},
353 {0x3035, 0x2a, 0, 0},
355 {0x320c, 0x08, 0, 0},
356 {0x320d, 0xca, 0, 0},
357 {0x320e, 0x04, 0, 0},
358 {0x320f, 0xb0, 0, 0},
360 {0x3f04, 0x08, 0, 0},
361 {0x3f05, 0xa6, 0, 0}, // hts - { 0x24
363 {0x3235, 0x04, 0, 0},
364 {0x3236, 0xae, 0, 0}, // vts-2
367 {0x3313, 0x05, 0, 0},
368 {0x3678, 0x42, 0, 0},
370 //for AE control per frame
371 {0x3670, 0x00, 0, 0},
372 {0x3633, 0x42, 0, 0},
374 {0x3802, 0x00, 0, 0},
377 {0x3677, 0x3f, 0, 0},
378 {0x3306, 0x44, 0, 0}, //20180126[3c },4a]
379 {0x330b, 0xca, 0, 0}, //20180126[c2 },d3]
382 {0x3237, 0x08, 0, 0},
383 {0x3238, 0x9a, 0, 0}, //hts-0x30
386 {0x3640, 0x01, 0, 0},
387 {0x3641, 0x02, 0, 0},
389 {0x3301, 0x12, 0, 0}, //[8 },15]20180126
390 {0x3631, 0x84, 0, 0},
391 {0x366f, 0x2f, 0, 0},
392 {0x3622, 0xc6, 0, 0}, //20180117
394 {0x3e03, 0x03, 0, 0}, // Bit[3]: AGC table mapping method, Bit[1]: AGC manual, BIt[0]: AEC manual
396 // {0x0100, 0x00, 0, 0},
397 // {0x4501, 0xc8, 0, 0}, //bar testing
398 // {0x3902, 0x45, 0, 0},
401 static struct reg_value sc2235_setting_1080P_1920_1080[] = {
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),
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),
423 static int sc2235_write_reg(struct sc2235_dev *sensor, u16 reg, u8 val)
425 struct i2c_client *client = sensor->i2c_client;
434 msg.addr = client->addr;
435 msg.flags = client->flags;
437 msg.len = sizeof(buf);
439 ret = i2c_transfer(client->adapter, &msg, 1);
441 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
449 static int sc2235_read_reg(struct sc2235_dev *sensor, u16 reg, u8 *val)
451 struct i2c_client *client = sensor->i2c_client;
452 struct i2c_msg msg[2];
459 msg[0].addr = client->addr;
460 msg[0].flags = client->flags;
462 msg[0].len = sizeof(buf);
464 msg[1].addr = client->addr;
465 msg[1].flags = client->flags | I2C_M_RD;
469 ret = i2c_transfer(client->adapter, msg, 2);
471 dev_err(&client->dev, "%s: error: reg=%x\n",
480 static int sc2235_read_reg16(struct sc2235_dev *sensor, u16 reg, u16 *val)
485 ret = sc2235_read_reg(sensor, reg, &hi);
488 ret = sc2235_read_reg(sensor, reg + 1, &lo);
492 *val = ((u16)hi << 8) | (u16)lo;
496 static int sc2235_write_reg16(struct sc2235_dev *sensor, u16 reg, u16 val)
500 ret = sc2235_write_reg(sensor, reg, val >> 8);
504 return sc2235_write_reg(sensor, reg + 1, val & 0xff);
507 static int sc2235_mod_reg(struct sc2235_dev *sensor, u16 reg,
513 ret = sc2235_read_reg(sensor, reg, &readval);
521 return sc2235_write_reg(sensor, reg, val);
524 #define SC2235_PLL_PREDIV 3
526 #define SC2235_SYSDIV_MIN 0
527 #define SC2235_SYSDIV_MAX 7
529 #define SC2235_PLL_MULT_MIN 0
530 #define SC2235_PLL_MULT_MAX 63
533 static unsigned long sc2235_compute_sys_clk(struct sc2235_dev *sensor,
534 u8 pll_pre, u8 pll_mult,
537 unsigned long sysclk =
538 sensor->xclk_freq * (64 - pll_mult) / (pll_pre * (sysdiv + 1));
540 /* PLL1 output cannot exceed 1GHz. */
541 if (sysclk / 1000000 > 1000)
547 static unsigned long sc2235_calc_sys_clk(struct sc2235_dev *sensor,
549 u8 *pll_prediv, u8 *pll_mult,
552 unsigned long best = ~0;
553 u8 best_sysdiv = 1, best_mult = 1;
554 u8 _sysdiv, _pll_mult;
556 for (_sysdiv = SC2235_SYSDIV_MIN;
557 _sysdiv <= SC2235_SYSDIV_MAX;
559 for (_pll_mult = SC2235_PLL_MULT_MIN;
560 _pll_mult <= SC2235_PLL_MULT_MAX;
564 _rate = sc2235_compute_sys_clk(sensor,
569 * We have reached the maximum allowed PLL1 output,
576 * Prefer rates above the expected clock rate than
577 * below, even if that means being less precise.
582 if (abs(rate - _rate) < abs(rate - best)) {
584 best_sysdiv = _sysdiv;
585 best_mult = _pll_mult;
594 *sysdiv = best_sysdiv;
595 *pll_prediv = SC2235_PLL_PREDIV;
596 *pll_mult = best_mult;
602 static int sc2235_set_timings(struct sc2235_dev *sensor,
603 const struct sc2235_mode_info *mode)
610 static int sc2235_load_regs(struct sc2235_dev *sensor,
611 const struct sc2235_mode_info *mode)
613 const struct reg_value *regs = mode->reg_data;
620 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
621 delay_ms = regs->delay_ms;
622 reg_addr = regs->reg_addr;
627 ret = sc2235_mod_reg(sensor, reg_addr, mask, val);
629 ret = sc2235_write_reg(sensor, reg_addr, val);
634 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
637 return sc2235_set_timings(sensor, mode);
640 static int sc2235_set_autoexposure(struct sc2235_dev *sensor, bool on)
642 return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
643 BIT(0), on ? 0 : BIT(0));
646 static int sc2235_get_exposure(struct sc2235_dev *sensor)
648 int exp = 0, ret = 0;
651 ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_HI, &temp);
654 exp |= (int)temp << 8;
655 ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_LO, &temp);
663 static int sc2235_set_exposure(struct sc2235_dev *sensor, u32 exposure)
669 ret = sc2235_write_reg(sensor,
670 SC2235_REG_AEC_PK_EXPOSURE_LO,
674 return sc2235_write_reg(sensor,
675 SC2235_REG_AEC_PK_EXPOSURE_HI,
676 (exposure >> 8) & 0xff);
679 static int sc2235_get_gain(struct sc2235_dev *sensor)
684 ret = sc2235_read_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN, &gain);
688 return gain & 0x1fff;
691 static int sc2235_set_gain(struct sc2235_dev *sensor, int gain)
693 return sc2235_write_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN,
697 static int sc2235_set_autogain(struct sc2235_dev *sensor, bool on)
699 return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
700 BIT(1), on ? 0 : BIT(1));
703 static int sc2235_set_stream_dvp(struct sc2235_dev *sensor, bool on)
705 return sc2235_mod_reg(sensor, SC2235_REG_STREAM_ON,
710 static int sc2235_get_sysclk(struct sc2235_dev *sensor)
715 static int sc2235_set_night_mode(struct sc2235_dev *sensor)
720 static int sc2235_get_hts(struct sc2235_dev *sensor)
725 ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_HTS, &hts);
732 static int sc2235_get_vts(struct sc2235_dev *sensor)
737 ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_VTS, &vts);
744 static int sc2235_set_vts(struct sc2235_dev *sensor, int vts)
746 return sc2235_write_reg16(sensor, SC2235_REG_TIMING_VTS, vts);
749 static int sc2235_get_light_freq(struct sc2235_dev *sensor)
754 static int sc2235_set_bandingfilter(struct sc2235_dev *sensor)
759 static int sc2235_set_ae_target(struct sc2235_dev *sensor, int target)
764 static int sc2235_get_binning(struct sc2235_dev *sensor)
769 static int sc2235_set_binning(struct sc2235_dev *sensor, bool enable)
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)
780 const struct sc2235_mode_info *mode;
782 mode = v4l2_find_nearest_size(sc2235_mode_data,
783 ARRAY_SIZE(sc2235_mode_data),
788 (!nearest && (mode->hact != width || mode->vact != height)))
791 /* Check to see if the current mode exceeds the max frame rate */
792 if (sc2235_framerates[fr] > sc2235_framerates[mode->max_fps])
798 static u64 sc2235_calc_pixel_rate(struct sc2235_dev *sensor)
802 rate = sensor->current_mode->vtot * sensor->current_mode->htot;
803 rate *= sc2235_framerates[sensor->current_fr];
810 * sc2235_set_dvp_pclk() - Calculate the clock tree configuration values
811 * for the dvp output.
813 * @rate: The requested bandwidth per lane in bytes per second.
814 * 'Bandwidth Per Lane' is calculated as:
815 * rate = HTOT * VTOT * FPS;
817 * This function use the requested bandwidth to calculate:
818 * - rate = xclk * (64 - M) / (N * (S + 1));
825 static int sc2235_set_dvp_pclk(struct sc2235_dev *sensor,
828 u8 prediv, mult, sysdiv;
831 sc2235_calc_sys_clk(sensor, rate, &prediv, &mult,
839 * if sensor changes inside scaling or subsampling
840 * change mode directly
842 static int sc2235_set_mode_direct(struct sc2235_dev *sensor,
843 const struct sc2235_mode_info *mode)
848 /* Write capture setting */
849 return sc2235_load_regs(sensor, mode);
853 static int sc2235_set_mode(struct sc2235_dev *sensor)
856 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
857 const struct sc2235_mode_info *mode = sensor->current_mode;
859 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
862 /* auto gain and exposure must be turned off when changing modes */
864 ret = sc2235_set_autogain(sensor, false);
869 /* This issue will be addressed in the EVB board*/
870 /* This action will result in poor image display 2021 1111*/
872 ret = sc2235_set_autoexposure(sensor, false);
874 goto restore_auto_gain;
877 rate = sc2235_calc_pixel_rate(sensor);
878 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
879 ret = sc2235_set_dvp_pclk(sensor, rate);
885 ret = sc2235_set_mode_direct(sensor, mode);
887 goto restore_auto_exp_gain;
890 /* restore auto gain and exposure */
892 sc2235_set_autogain(sensor, true);
894 sc2235_set_autoexposure(sensor, true);
897 sensor->pending_mode_change = false;
898 sensor->last_mode = mode;
901 restore_auto_exp_gain:
903 sc2235_set_autoexposure(sensor, true);
906 sc2235_set_autogain(sensor, true);
911 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
912 struct v4l2_mbus_framefmt *format);
914 /* restore the last set video mode after chip power-on */
915 static int sc2235_restore_mode(struct sc2235_dev *sensor)
919 /* first load the initial register values */
920 ret = sc2235_load_regs(sensor, &sc2235_mode_init_data);
923 sensor->last_mode = &sc2235_mode_init_data;
924 /* now restore the last capture mode */
925 ret = sc2235_set_mode(sensor);
929 return sc2235_set_framefmt(sensor, &sensor->fmt);
932 static void sc2235_power(struct sc2235_dev *sensor, bool enable)
934 if (!sensor->pwdn_gpio)
936 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
939 static void sc2235_reset(struct sc2235_dev *sensor)
941 if (!sensor->reset_gpio)
944 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
946 /* camera power cycle */
947 sc2235_power(sensor, false);
948 usleep_range(5000, 10000);
949 sc2235_power(sensor, true);
950 usleep_range(5000, 10000);
952 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
953 usleep_range(1000, 2000);
955 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
956 usleep_range(20000, 25000);
959 static int sc2235_set_power_on(struct sc2235_dev *sensor)
961 struct i2c_client *client = sensor->i2c_client;
964 ret = clk_prepare_enable(sensor->xclk);
966 dev_err(&client->dev, "%s: failed to enable clock\n",
971 ret = regulator_bulk_enable(SC2235_NUM_SUPPLIES,
974 dev_err(&client->dev, "%s: failed to enable regulators\n",
979 sc2235_reset(sensor);
980 sc2235_power(sensor, true);
985 clk_disable_unprepare(sensor->xclk);
989 static void sc2235_set_power_off(struct sc2235_dev *sensor)
991 sc2235_power(sensor, false);
993 regulator_bulk_disable(SC2235_NUM_SUPPLIES, sensor->supplies);
994 clk_disable_unprepare(sensor->xclk);
997 static int sc2235_set_power_dvp(struct sc2235_dev *sensor, bool on)
999 unsigned int flags = sensor->ep.bus.parallel.flags;
1003 * configure parallel port control lines polarity
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...)
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);
1019 // ret = sc2235_write_reg(sensor,
1020 // SC2235_REG_POLARITY_CTRL00,
1028 static int sc2235_set_power(struct sc2235_dev *sensor, bool on)
1033 ret = sc2235_set_power_on(sensor);
1037 ret = sc2235_restore_mode(sensor);
1042 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
1043 ret = sc2235_set_power_dvp(sensor, on);
1048 sc2235_set_power_off(sensor);
1053 sc2235_set_power_off(sensor);
1058 static int sc2235_s_power(struct v4l2_subdev *sd, int on)
1060 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1063 mutex_lock(&sensor->lock);
1066 * If the power count is modified from 0 to != 0 or from != 0 to 0,
1067 * update the power state.
1069 if (sensor->power_count == !on) {
1070 ret = sc2235_set_power(sensor, !!on);
1075 /* Update the power count. */
1076 sensor->power_count += on ? 1 : -1;
1077 WARN_ON(sensor->power_count < 0);
1079 mutex_unlock(&sensor->lock);
1081 if (on && !ret && sensor->power_count == 1) {
1082 /* restore controls */
1083 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1089 static int sc2235_try_frame_interval(struct sc2235_dev *sensor,
1090 struct v4l2_fract *fi,
1091 u32 width, u32 height)
1093 const struct sc2235_mode_info *mode;
1094 enum sc2235_frame_rate rate = SC2235_15_FPS;
1095 int minfps, maxfps, best_fps, fps;
1098 minfps = sc2235_framerates[SC2235_15_FPS];
1099 maxfps = sc2235_framerates[SC2235_60_FPS];
1101 if (fi->numerator == 0) {
1102 fi->denominator = maxfps;
1104 rate = SC2235_60_FPS;
1108 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
1112 for (i = 0; i < ARRAY_SIZE(sc2235_framerates); i++) {
1113 int curr_fps = sc2235_framerates[i];
1115 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
1116 best_fps = curr_fps;
1122 fi->denominator = best_fps;
1125 mode = sc2235_find_mode(sensor, rate, width, height, false);
1126 return mode ? rate : -EINVAL;
1129 static int sc2235_get_fmt(struct v4l2_subdev *sd,
1130 struct v4l2_subdev_state *state,
1131 struct v4l2_subdev_format *format)
1133 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1134 struct v4l2_mbus_framefmt *fmt;
1136 if (format->pad != 0)
1139 mutex_lock(&sensor->lock);
1141 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1142 fmt = v4l2_subdev_get_try_format(&sensor->sd, state,
1147 format->format = *fmt;
1149 mutex_unlock(&sensor->lock);
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)
1159 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1160 const struct sc2235_mode_info *mode;
1163 mode = sc2235_find_mode(sensor, fr, fmt->width, fmt->height, true);
1166 fmt->width = mode->hact;
1167 fmt->height = mode->vact;
1172 for (i = 0; i < ARRAY_SIZE(sc2235_formats); i++)
1173 if (sc2235_formats[i].code == fmt->code)
1175 if (i >= ARRAY_SIZE(sc2235_formats))
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);
1187 static int sc2235_set_fmt(struct v4l2_subdev *sd,
1188 struct v4l2_subdev_state *state,
1189 struct v4l2_subdev_format *format)
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;
1197 if (format->pad != 0)
1199 mutex_lock(&sensor->lock);
1201 if (sensor->streaming) {
1206 ret = sc2235_try_fmt_internal(sd, mbus_fmt, 0, &new_mode);
1210 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1211 fmt = v4l2_subdev_get_try_format(sd, state, 0);
1215 if (mbus_fmt->code != sensor->fmt.code)
1216 sensor->pending_fmt_change = true;
1220 if (new_mode != sensor->current_mode) {
1221 sensor->current_mode = new_mode;
1222 sensor->pending_mode_change = true;
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;
1233 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1234 sc2235_calc_pixel_rate(sensor));
1236 mutex_unlock(&sensor->lock);
1240 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
1241 struct v4l2_mbus_framefmt *format)
1245 switch (format->code) {
1256 static int sc2235_set_ctrl_hue(struct sc2235_dev *sensor, int value)
1262 static int sc2235_set_ctrl_contrast(struct sc2235_dev *sensor, int value)
1268 static int sc2235_set_ctrl_saturation(struct sc2235_dev *sensor, int value)
1274 static int sc2235_set_ctrl_white_balance(struct sc2235_dev *sensor, int awb)
1280 static int sc2235_set_ctrl_exposure(struct sc2235_dev *sensor,
1281 enum v4l2_exposure_auto_type auto_exposure)
1283 struct sc2235_ctrls *ctrls = &sensor->ctrls;
1284 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
1287 if (ctrls->auto_exp->is_new) {
1288 ret = sc2235_set_autoexposure(sensor, auto_exp);
1293 if (!auto_exp && ctrls->exposure->is_new) {
1296 ret = sc2235_get_vts(sensor);
1302 if (ctrls->exposure->val < max_exp)
1303 ret = sc2235_set_exposure(sensor, ctrls->exposure->val);
1309 static int sc2235_set_ctrl_gain(struct sc2235_dev *sensor, bool auto_gain)
1311 struct sc2235_ctrls *ctrls = &sensor->ctrls;
1314 if (ctrls->auto_gain->is_new) {
1315 ret = sc2235_set_autogain(sensor, auto_gain);
1320 if (!auto_gain && ctrls->gain->is_new)
1321 ret = sc2235_set_gain(sensor, ctrls->gain->val);
1326 static const char * const test_pattern_menu[] = {
1332 #define SC2235_TEST_ENABLE BIT(3)
1333 #define SC2235_TEST_BLACK (3 << 0)
1335 static int sc2235_set_ctrl_test_pattern(struct sc2235_dev *sensor, int value)
1339 *For 7110 platform, refer to 1125 FW code configuration. This operation will cause the image to be white.
1342 ret = sc2235_mod_reg(sensor, SC2235_REG_TEST_SET0, BIT(3),
1345 ret |= sc2235_mod_reg(sensor, SC2235_REG_TEST_SET1, BIT(6),
1351 static int sc2235_set_ctrl_light_freq(struct sc2235_dev *sensor, int value)
1356 static int sc2235_set_ctrl_hflip(struct sc2235_dev *sensor, int value)
1358 return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1360 (!(value ^ sensor->upside_down)) ?
1361 (BIT(2) | BIT(1)) : 0);
1364 static int sc2235_set_ctrl_vflip(struct sc2235_dev *sensor, int value)
1366 return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1368 (value ^ sensor->upside_down) ?
1369 (BIT(6) | BIT(5)) : 0);
1372 static int sc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1374 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1375 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1378 /* v4l2_ctrl_lock() locks our own mutex */
1381 case V4L2_CID_AUTOGAIN:
1382 val = sc2235_get_gain(sensor);
1385 sensor->ctrls.gain->val = val;
1387 case V4L2_CID_EXPOSURE_AUTO:
1388 val = sc2235_get_exposure(sensor);
1391 sensor->ctrls.exposure->val = val;
1398 static int sc2235_s_ctrl(struct v4l2_ctrl *ctrl)
1400 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1401 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1404 /* v4l2_ctrl_lock() locks our own mutex */
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.
1411 if (sensor->power_count == 0)
1415 case V4L2_CID_AUTOGAIN:
1416 ret = sc2235_set_ctrl_gain(sensor, ctrl->val);
1418 case V4L2_CID_EXPOSURE_AUTO:
1419 ret = sc2235_set_ctrl_exposure(sensor, ctrl->val);
1421 case V4L2_CID_AUTO_WHITE_BALANCE:
1422 ret = sc2235_set_ctrl_white_balance(sensor, ctrl->val);
1425 ret = sc2235_set_ctrl_hue(sensor, ctrl->val);
1427 case V4L2_CID_CONTRAST:
1428 ret = sc2235_set_ctrl_contrast(sensor, ctrl->val);
1430 case V4L2_CID_SATURATION:
1431 ret = sc2235_set_ctrl_saturation(sensor, ctrl->val);
1433 case V4L2_CID_TEST_PATTERN:
1434 ret = sc2235_set_ctrl_test_pattern(sensor, ctrl->val);
1436 case V4L2_CID_POWER_LINE_FREQUENCY:
1437 ret = sc2235_set_ctrl_light_freq(sensor, ctrl->val);
1439 case V4L2_CID_HFLIP:
1440 ret = sc2235_set_ctrl_hflip(sensor, ctrl->val);
1442 case V4L2_CID_VFLIP:
1443 ret = sc2235_set_ctrl_vflip(sensor, ctrl->val);
1453 static const struct v4l2_ctrl_ops sc2235_ctrl_ops = {
1454 .g_volatile_ctrl = sc2235_g_volatile_ctrl,
1455 .s_ctrl = sc2235_s_ctrl,
1458 static int sc2235_init_controls(struct sc2235_dev *sensor)
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;
1465 v4l2_ctrl_handler_init(hdl, 32);
1467 /* we can use our own mutex for the ctrl lock */
1468 hdl->lock = &sensor->lock;
1470 /* Clock related controls */
1471 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
1473 sc2235_calc_pixel_rate(sensor));
1475 /* Auto/manual white balance */
1476 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1477 V4L2_CID_AUTO_WHITE_BALANCE,
1479 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1481 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1483 /* Auto/manual exposure */
1486 *For 7110 platform, This operation will cause the image to be white.
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,
1494 /* Auto/manual gain */
1495 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1497 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1500 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1501 V4L2_CID_EXPOSURE_AUTO,
1502 V4L2_EXPOSURE_MANUAL, 0,
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,
1509 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1512 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
1514 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
1516 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
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,
1524 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
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);
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;
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);
1546 sensor->sd.ctrl_handler = hdl;
1550 v4l2_ctrl_handler_free(hdl);
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)
1560 if (fse->index >= SC2235_NUM_MODES)
1564 sc2235_mode_data[fse->index].hact;
1565 fse->max_width = fse->min_width;
1567 sc2235_mode_data[fse->index].vact;
1568 fse->max_height = fse->min_height;
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)
1578 struct v4l2_fract tpf;
1584 if (fie->index >= SC2235_NUM_FRAMERATES)
1588 tpf.denominator = sc2235_framerates[fie->index];
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)
1595 if (i == SC2235_NUM_MODES)
1598 fie->interval = tpf;
1602 static int sc2235_g_frame_interval(struct v4l2_subdev *sd,
1603 struct v4l2_subdev_frame_interval *fi)
1605 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1607 mutex_lock(&sensor->lock);
1608 fi->interval = sensor->frame_interval;
1609 mutex_unlock(&sensor->lock);
1614 static int sc2235_s_frame_interval(struct v4l2_subdev *sd,
1615 struct v4l2_subdev_frame_interval *fi)
1617 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1618 const struct sc2235_mode_info *mode;
1619 int frame_rate, ret = 0;
1624 mutex_lock(&sensor->lock);
1626 if (sensor->streaming) {
1631 mode = sensor->current_mode;
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;
1641 mode = sc2235_find_mode(sensor, frame_rate, mode->hact,
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;
1655 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1656 sc2235_calc_pixel_rate(sensor));
1659 mutex_unlock(&sensor->lock);
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)
1669 if (code->index >= ARRAY_SIZE(sc2235_formats))
1672 code->code = sc2235_formats[code->index].code;
1676 static int sc2235_s_stream(struct v4l2_subdev *sd, int enable)
1678 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1681 mutex_lock(&sensor->lock);
1683 if (sensor->streaming == !enable) {
1684 if (enable && sensor->pending_mode_change) {
1685 ret = sc2235_set_mode(sensor);
1690 if (enable && sensor->pending_fmt_change) {
1691 ret = sc2235_set_framefmt(sensor, &sensor->fmt);
1694 sensor->pending_fmt_change = false;
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);
1706 sensor->streaming += enable ? 1 : -1;
1707 WARN_ON(sensor->streaming < 0);
1709 mutex_unlock(&sensor->lock);
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,
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,
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,
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,
1741 static int sc2235_get_regulators(struct sc2235_dev *sensor)
1745 for (i = 0; i < SC2235_NUM_SUPPLIES; i++)
1746 sensor->supplies[i].supply = sc2235_supply_name[i];
1748 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
1749 SC2235_NUM_SUPPLIES,
1753 static int sc2235_check_chip_id(struct sc2235_dev *sensor)
1755 struct i2c_client *client = sensor->i2c_client;
1759 ret = sc2235_set_power_on(sensor);
1763 ret = sc2235_read_reg16(sensor, SC2235_REG_CHIP_ID, &chip_id);
1765 dev_err(&client->dev, "%s: failed to read chip identifier\n",
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);
1775 dev_err(&client->dev, "%s: chip identifier, got 0x%x\n",
1779 sc2235_set_power_off(sensor);
1783 static int sc2235_probe(struct i2c_client *client)
1785 struct device *dev = &client->dev;
1786 struct fwnode_handle *endpoint;
1787 struct sc2235_dev *sensor;
1788 struct v4l2_mbus_framefmt *fmt;
1792 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1796 sensor->i2c_client = client;
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);
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;
1814 sensor->ae_target = 52;
1816 /* optional indication of physical rotation of sensor */
1817 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
1822 sensor->upside_down = true;
1827 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
1832 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
1835 dev_err(dev, "endpoint node not found\n");
1839 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
1840 fwnode_handle_put(endpoint);
1842 dev_err(dev, "Could not parse endpoint\n");
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);
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);
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",
1868 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
1870 if (IS_ERR(sensor->pwdn_gpio))
1871 return PTR_ERR(sensor->pwdn_gpio);
1873 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1875 if (IS_ERR(sensor->reset_gpio))
1876 return PTR_ERR(sensor->reset_gpio);
1878 v4l2_i2c_subdev_init(&sensor->sd, client, &sc2235_subdev_ops);
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);
1888 ret = sc2235_get_regulators(sensor);
1891 mutex_init(&sensor->lock);
1893 ret = sc2235_check_chip_id(sensor);
1895 goto entity_cleanup;
1897 ret = sc2235_init_controls(sensor);
1899 goto entity_cleanup;
1901 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
1908 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1910 media_entity_cleanup(&sensor->sd.entity);
1911 mutex_destroy(&sensor->lock);
1915 static int sc2235_remove(struct i2c_client *client)
1917 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1918 struct sc2235_dev *sensor = to_sc2235_dev(sd);
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);
1928 static const struct i2c_device_id sc2235_id[] = {
1932 MODULE_DEVICE_TABLE(i2c, sc2235_id);
1934 static const struct of_device_id sc2235_dt_ids[] = {
1935 { .compatible = "smartsens,sc2235" },
1938 MODULE_DEVICE_TABLE(of, sc2235_dt_ids);
1940 static struct i2c_driver sc2235_i2c_driver = {
1943 .of_match_table = sc2235_dt_ids,
1945 .id_table = sc2235_id,
1946 .probe_new = sc2235_probe,
1947 .remove = sc2235_remove,
1950 module_i2c_driver(sc2235_i2c_driver);
1952 MODULE_DESCRIPTION("SC2235 MIPI Camera Subdev Driver");
1953 MODULE_LICENSE("GPL");