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;
132 struct sensor_pinctrl_info {
133 struct pinctrl *pinctrl;
134 struct pinctrl_state *reset_state_low;
135 struct pinctrl_state *reset_state_high;
140 struct i2c_client *i2c_client;
141 struct v4l2_subdev sd;
142 struct media_pad pad;
143 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
144 struct clk *xclk; /* system clock to SC2235 */
147 struct regulator_bulk_data supplies[SC2235_NUM_SUPPLIES];
148 struct gpio_desc *reset_gpio;
149 struct gpio_desc *pwdn_gpio;
152 /* lock to protect all members below */
157 struct v4l2_mbus_framefmt fmt;
158 bool pending_fmt_change;
160 const struct sc2235_mode_info *current_mode;
161 const struct sc2235_mode_info *last_mode;
162 enum sc2235_frame_rate current_fr;
163 struct v4l2_fract frame_interval;
165 struct sc2235_ctrls ctrls;
167 u32 prev_sysclk, prev_hts;
168 u32 ae_low, ae_high, ae_target;
170 bool pending_mode_change;
173 struct sensor_pinctrl_info sc2235_pctrl;
176 int sc2235_sensor_pinctrl_init(
177 struct sensor_pinctrl_info *sensor_pctrl, struct device *dev)
179 sensor_pctrl->pinctrl = devm_pinctrl_get(dev);
180 if (IS_ERR_OR_NULL(sensor_pctrl->pinctrl)) {
181 pr_err("Getting pinctrl handle failed\n");
185 sensor_pctrl->reset_state_low
186 = pinctrl_lookup_state(sensor_pctrl->pinctrl, "reset_low");
187 if (IS_ERR_OR_NULL(sensor_pctrl->reset_state_low)) {
188 pr_err("Failed to get the reset_low pinctrl handle\n");
192 sensor_pctrl->reset_state_high
193 = pinctrl_lookup_state(sensor_pctrl->pinctrl, "reset_high");
194 if (IS_ERR_OR_NULL(sensor_pctrl->reset_state_high)) {
195 pr_err("Failed to get the reset_high pinctrl handle\n");
199 sensor_pctrl->use_pinctrl = true;
204 static inline struct sc2235_dev *to_sc2235_dev(struct v4l2_subdev *sd)
206 return container_of(sd, struct sc2235_dev, sd);
209 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
211 return &container_of(ctrl->handler, struct sc2235_dev,
215 /* sc2235 initial register 7fps*/
216 static struct reg_value sc2235_init_tbl_1080p_7fps[] = {
217 {0x0103, 0x01, 0, 0},
218 {0x0100, 0x00, 0, 0},
219 {0x3039, 0x75, 0, 0},
220 {0x320c, 0x08, 0, 0},
221 {0x320d, 0x98, 0, 0},
222 {0x3222, 0x29, 0, 0},
223 {0x3235, 0x04, 0, 0},
224 {0x3236, 0x63, 0, 0},
225 {0x3237, 0x08, 0, 0},
226 {0x3238, 0x68, 0, 0},
227 {0x3301, 0x04, 0, 0},
228 {0x3303, 0x20, 0, 0},
229 {0x3306, 0x1a, 0, 0},
230 {0x3309, 0xa0, 0, 0},
231 {0x330b, 0x54, 0, 0},
232 {0x330e, 0x30, 0, 0},
233 {0x3313, 0x05, 0, 0},
234 {0x331e, 0x0d, 0, 0},
235 {0x331f, 0x8d, 0, 0},
236 {0x3320, 0x0f, 0, 0},
237 {0x3321, 0x8f, 0, 0},
238 {0x3340, 0x06, 0, 0},
239 {0x3341, 0x50, 0, 0},
240 {0x3342, 0x04, 0, 0},
241 {0x3343, 0x20, 0, 0},
242 {0x3348, 0x07, 0, 0},
243 {0x3349, 0x80, 0, 0},
244 {0x334a, 0x04, 0, 0},
245 {0x334b, 0x20, 0, 0},
246 {0x335e, 0x01, 0, 0},
247 {0x335f, 0x03, 0, 0},
248 {0x3364, 0x05, 0, 0},
249 {0x3366, 0x7c, 0, 0},
250 {0x3367, 0x08, 0, 0},
251 {0x3368, 0x02, 0, 0},
252 {0x3369, 0x00, 0, 0},
253 {0x336a, 0x00, 0, 0},
254 {0x336b, 0x00, 0, 0},
255 {0x337c, 0x04, 0, 0},
256 {0x337d, 0x06, 0, 0},
257 {0x337f, 0x03, 0, 0},
258 {0x3380, 0x04, 0, 0},
259 {0x3381, 0x0a, 0, 0},
260 {0x33a0, 0x05, 0, 0},
261 {0x33b5, 0x10, 0, 0},
262 {0x3621, 0x28, 0, 0},
263 {0x3622, 0x06, 0, 0},
264 {0x3625, 0x02, 0, 0},
265 {0x3630, 0x48, 0, 0},
266 {0x3631, 0x84, 0, 0},
267 {0x3632, 0x88, 0, 0},
268 {0x3633, 0x42, 0, 0},
269 {0x3634, 0x42, 0, 0},
270 {0x3636, 0x24, 0, 0},
271 {0x3635, 0xc1, 0, 0},
272 {0x3637, 0x14, 0, 0},
273 {0x3638, 0x1f, 0, 0},
274 {0x363b, 0x09, 0, 0},
275 {0x3639, 0x09, 0, 0},
276 {0x363c, 0x07, 0, 0},
277 {0x366e, 0x08, 0, 0},
278 {0x3670, 0x08, 0, 0},
279 {0x366f, 0x2f, 0, 0},
280 {0x3677, 0x1f, 0, 0},
281 {0x3678, 0x42, 0, 0},
282 {0x3679, 0x43, 0, 0},
283 {0x367e, 0x07, 0, 0},
284 {0x367f, 0x0f, 0, 0},
285 {0x3802, 0x01, 0, 0},
286 {0x3901, 0x02, 0, 0},
287 {0x3908, 0x11, 0, 0},
288 {0x391b, 0x4d, 0, 0},
289 {0x391e, 0x00, 0, 0},
290 {0x3e01, 0x46, 0, 0},
291 {0x3e03, 0x0b, 0, 0},
292 {0x3f00, 0x07, 0, 0},
293 {0x3f04, 0x08, 0, 0},
294 {0x3f05, 0x74, 0, 0},
295 {0x4500, 0x59, 0, 0},
296 {0x5780, 0xff, 0, 0},
297 {0x5781, 0x04, 0, 0},
298 {0x5785, 0x18, 0, 0},
299 //{0x0100, 0x01, 0, 0},
300 {0x330b, 0x5d, 0, 0},
301 {0x3301, 0x0a, 0, 0},
302 {0x3631, 0x88, 0, 0},
303 {0x366f, 0x2f, 0, 0},
306 /* sc2235 initial register 30fps*/
307 static struct reg_value sc2235_init_regs_tbl_1080[] = {
308 {0x0103, 0x01, 0, 50},
309 {0x0100, 0x00, 0, 0},
310 {0x3039, 0x80, 0, 0},
311 {0x3621, 0x28, 0, 0},
313 {0x3309, 0x60, 0, 0},
314 {0x331f, 0x4d, 0, 0},
315 {0x3321, 0x4f, 0, 0},
316 {0x33b5, 0x10, 0, 0},
318 {0x3303, 0x20, 0, 0},
319 {0x331e, 0x0d, 0, 0},
320 {0x3320, 0x0f, 0, 0},
322 {0x3622, 0x02, 0, 0},
323 {0x3633, 0x42, 0, 0},
324 {0x3634, 0x42, 0, 0},
326 {0x3306, 0x66, 0, 0},
327 {0x330b, 0xd1, 0, 0},
329 {0x3301, 0x0e, 0, 0},
331 {0x320c, 0x08, 0, 0},
332 {0x320d, 0x98, 0, 0},
334 {0x3364, 0x05, 0, 0}, // [2] 1: write at sampling ending
336 {0x363c, 0x28, 0, 0}, //bypass nvdd
337 {0x363b, 0x0a, 0, 0}, //HVDD
338 {0x3635, 0xa0, 0, 0}, //TXVDD
340 {0x4500, 0x59, 0, 0},
341 {0x3d08, 0x00, 0, 0},
342 {0x3908, 0x11, 0, 0},
344 {0x363c, 0x08, 0, 0},
346 {0x3e03, 0x03, 0, 0},
347 {0x3e01, 0x46, 0, 0},
350 {0x3381, 0x0a, 0, 0},
351 {0x3348, 0x09, 0, 0},
352 {0x3349, 0x50, 0, 0},
353 {0x334a, 0x02, 0, 0},
354 {0x334b, 0x60, 0, 0},
356 {0x3380, 0x04, 0, 0},
357 {0x3340, 0x06, 0, 0},
358 {0x3341, 0x50, 0, 0},
359 {0x3342, 0x02, 0, 0},
360 {0x3343, 0x60, 0, 0},
364 {0x3632, 0x88, 0, 0}, //anti sm
365 {0x3309, 0xa0, 0, 0},
366 {0x331f, 0x8d, 0, 0},
367 {0x3321, 0x8f, 0, 0},
369 {0x335e, 0x01, 0, 0}, //ana dithering
370 {0x335f, 0x03, 0, 0},
371 {0x337c, 0x04, 0, 0},
372 {0x337d, 0x06, 0, 0},
373 {0x33a0, 0x05, 0, 0},
374 {0x3301, 0x05, 0, 0},
376 {0x337f, 0x03, 0, 0},
377 {0x3368, 0x02, 0, 0},
378 {0x3369, 0x00, 0, 0},
379 {0x336a, 0x00, 0, 0},
380 {0x336b, 0x00, 0, 0},
381 {0x3367, 0x08, 0, 0},
382 {0x330e, 0x30, 0, 0},
384 {0x3366, 0x7c, 0, 0}, // div_rst gap
386 {0x3635, 0xc1, 0, 0},
387 {0x363b, 0x09, 0, 0},
388 {0x363c, 0x07, 0, 0},
390 {0x391e, 0x00, 0, 0},
392 {0x3637, 0x14, 0, 0}, //fullwell 7K
394 {0x3306, 0x54, 0, 0},
395 {0x330b, 0xd8, 0, 0},
396 {0x366e, 0x08, 0, 0}, // ofs auto en [3]
397 {0x366f, 0x2f, 0, 0},
399 {0x3631, 0x84, 0, 0},
400 {0x3630, 0x48, 0, 0},
401 {0x3622, 0x06, 0, 0},
404 {0x3638, 0x1f, 0, 0},
405 {0x3625, 0x02, 0, 0},
406 {0x3636, 0x24, 0, 0},
409 {0x3348, 0x08, 0, 0},
410 {0x3e03, 0x0b, 0, 0},
413 {0x3342, 0x03, 0, 0},
414 {0x3343, 0xa0, 0, 0},
415 {0x334a, 0x03, 0, 0},
416 {0x334b, 0xa0, 0, 0},
419 {0x3343, 0xb0, 0, 0},
420 {0x334b, 0xb0, 0, 0},
424 {0x3802, 0x01, 0, 0},
425 {0x3235, 0x04, 0, 0},
426 {0x3236, 0x63, 0, 0}, // vts-2
429 {0x3343, 0xd0, 0, 0},
430 {0x334b, 0xd0, 0, 0},
431 {0x3348, 0x07, 0, 0},
432 {0x3349, 0x80, 0, 0},
435 {0x391b, 0x4d, 0, 0},
437 {0x3342, 0x04, 0, 0},
438 {0x3343, 0x20, 0, 0},
439 {0x334a, 0x04, 0, 0},
440 {0x334b, 0x20, 0, 0},
443 {0x3222, 0x29, 0, 0},
444 {0x3901, 0x02, 0, 0},
449 {0x3900, 0xD5, 0, 0}, // Bit[0]: blc_enable
450 {0x3902, 0x45, 0, 0}, // Bit[6]: blc_auto_en
453 {0x3907, 0x00, 0, 0},
454 {0x3908, 0x00, 0, 0},
457 {0x5000, 0x00, 0, 0}, // Bit[2]: white dead pixel cancel enable, Bit[1]: black dead pixel cancel enable
460 {0x3f00, 0x07, 0, 0}, // bit[2] = 1
461 {0x3f04, 0x08, 0, 0},
462 {0x3f05, 0x74, 0, 0}, // hts - { 0x24
465 {0x330b, 0xc8, 0, 0},
468 {0x3306, 0x4a, 0, 0},
469 {0x330b, 0xca, 0, 0},
470 {0x3639, 0x09, 0, 0},
473 {0x5780, 0xff, 0, 0},
474 {0x5781, 0x04, 0, 0},
475 {0x5785, 0x18, 0, 0},
478 {0x3039, 0x35, 0, 0}, //fps
479 {0x303a, 0x2e, 0, 0},
480 {0x3034, 0x05, 0, 0},
481 {0x3035, 0x2a, 0, 0},
483 {0x320c, 0x08, 0, 0},
484 {0x320d, 0xca, 0, 0},
485 {0x320e, 0x04, 0, 0},
486 {0x320f, 0xb0, 0, 0},
488 {0x3f04, 0x08, 0, 0},
489 {0x3f05, 0xa6, 0, 0}, // hts - { 0x24
491 {0x3235, 0x04, 0, 0},
492 {0x3236, 0xae, 0, 0}, // vts-2
495 {0x3313, 0x05, 0, 0},
496 {0x3678, 0x42, 0, 0},
498 //for AE control per frame
499 {0x3670, 0x00, 0, 0},
500 {0x3633, 0x42, 0, 0},
502 {0x3802, 0x00, 0, 0},
505 {0x3677, 0x3f, 0, 0},
506 {0x3306, 0x44, 0, 0}, //20180126[3c },4a]
507 {0x330b, 0xca, 0, 0}, //20180126[c2 },d3]
510 {0x3237, 0x08, 0, 0},
511 {0x3238, 0x9a, 0, 0}, //hts-0x30
514 {0x3640, 0x01, 0, 0},
515 {0x3641, 0x02, 0, 0},
517 {0x3301, 0x12, 0, 0}, //[8 },15]20180126
518 {0x3631, 0x84, 0, 0},
519 {0x366f, 0x2f, 0, 0},
520 {0x3622, 0xc6, 0, 0}, //20180117
522 {0x3e03, 0x03, 0, 0}, // Bit[3]: AGC table mapping method, Bit[1]: AGC manual, BIt[0]: AEC manual
524 // {0x0100, 0x00, 0, 0},
525 // {0x4501, 0xc8, 0, 0}, //bar testing
526 // {0x3902, 0x45, 0, 0},
529 static struct reg_value sc2235_setting_1080P_1920_1080[] = {
533 /* power-on sensor init reg table */
534 static const struct sc2235_mode_info sc2235_mode_init_data = {
535 SC2235_MODE_1080P_1920_1080,
536 1920, 0x8ca, 1080, 0x4b0,
537 sc2235_init_regs_tbl_1080,
538 ARRAY_SIZE(sc2235_init_regs_tbl_1080),
542 static const struct sc2235_mode_info
543 sc2235_mode_data[SC2235_NUM_MODES] = {
544 {SC2235_MODE_1080P_1920_1080,
545 1920, 0x8ca, 1080, 0x4b0,
546 sc2235_setting_1080P_1920_1080,
547 ARRAY_SIZE(sc2235_setting_1080P_1920_1080),
551 static int sc2235_write_reg(struct sc2235_dev *sensor, u16 reg, u8 val)
553 struct i2c_client *client = sensor->i2c_client;
562 msg.addr = client->addr;
563 msg.flags = client->flags;
565 msg.len = sizeof(buf);
567 ret = i2c_transfer(client->adapter, &msg, 1);
569 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
577 static int sc2235_read_reg(struct sc2235_dev *sensor, u16 reg, u8 *val)
579 struct i2c_client *client = sensor->i2c_client;
580 struct i2c_msg msg[2];
587 msg[0].addr = client->addr;
588 msg[0].flags = client->flags;
590 msg[0].len = sizeof(buf);
592 msg[1].addr = client->addr;
593 msg[1].flags = client->flags | I2C_M_RD;
597 ret = i2c_transfer(client->adapter, msg, 2);
599 dev_err(&client->dev, "%s: error: reg=%x\n",
608 static int sc2235_read_reg16(struct sc2235_dev *sensor, u16 reg, u16 *val)
613 ret = sc2235_read_reg(sensor, reg, &hi);
616 ret = sc2235_read_reg(sensor, reg + 1, &lo);
620 *val = ((u16)hi << 8) | (u16)lo;
624 static int sc2235_write_reg16(struct sc2235_dev *sensor, u16 reg, u16 val)
628 ret = sc2235_write_reg(sensor, reg, val >> 8);
632 return sc2235_write_reg(sensor, reg + 1, val & 0xff);
635 static int sc2235_mod_reg(struct sc2235_dev *sensor, u16 reg,
641 ret = sc2235_read_reg(sensor, reg, &readval);
649 return sc2235_write_reg(sensor, reg, val);
652 #define SC2235_PLL_PREDIV 3
654 #define SC2235_SYSDIV_MIN 0
655 #define SC2235_SYSDIV_MAX 7
657 #define SC2235_PLL_MULT_MIN 0
658 #define SC2235_PLL_MULT_MAX 63
661 static unsigned long sc2235_compute_sys_clk(struct sc2235_dev *sensor,
662 u8 pll_pre, u8 pll_mult,
665 unsigned long sysclk =
666 sensor->xclk_freq * (64 - pll_mult) / (pll_pre * (sysdiv + 1));
668 /* PLL1 output cannot exceed 1GHz. */
669 if (sysclk / 1000000 > 1000)
675 static unsigned long sc2235_calc_sys_clk(struct sc2235_dev *sensor,
677 u8 *pll_prediv, u8 *pll_mult,
680 unsigned long best = ~0;
681 u8 best_sysdiv = 1, best_mult = 1;
682 u8 _sysdiv, _pll_mult;
684 for (_sysdiv = SC2235_SYSDIV_MIN;
685 _sysdiv <= SC2235_SYSDIV_MAX;
687 for (_pll_mult = SC2235_PLL_MULT_MIN;
688 _pll_mult <= SC2235_PLL_MULT_MAX;
692 _rate = sc2235_compute_sys_clk(sensor,
697 * We have reached the maximum allowed PLL1 output,
704 * Prefer rates above the expected clock rate than
705 * below, even if that means being less precise.
710 if (abs(rate - _rate) < abs(rate - best)) {
712 best_sysdiv = _sysdiv;
713 best_mult = _pll_mult;
722 *sysdiv = best_sysdiv;
723 *pll_prediv = SC2235_PLL_PREDIV;
724 *pll_mult = best_mult;
730 static int sc2235_set_timings(struct sc2235_dev *sensor,
731 const struct sc2235_mode_info *mode)
738 static int sc2235_load_regs(struct sc2235_dev *sensor,
739 const struct sc2235_mode_info *mode)
741 const struct reg_value *regs = mode->reg_data;
748 st_info(ST_SENSOR, "%s, mode = 0x%x\n", __func__, mode->id);
749 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
750 delay_ms = regs->delay_ms;
751 reg_addr = regs->reg_addr;
756 ret = sc2235_mod_reg(sensor, reg_addr, mask, val);
758 ret = sc2235_write_reg(sensor, reg_addr, val);
763 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
766 return sc2235_set_timings(sensor, mode);
769 static int sc2235_set_autoexposure(struct sc2235_dev *sensor, bool on)
771 return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
772 BIT(0), on ? 0 : BIT(0));
775 static int sc2235_get_exposure(struct sc2235_dev *sensor)
777 int exp = 0, ret = 0;
780 ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_HI, &temp);
783 exp |= (int)temp << 8;
784 ret = sc2235_read_reg(sensor, SC2235_REG_AEC_PK_EXPOSURE_LO, &temp);
792 static int sc2235_set_exposure(struct sc2235_dev *sensor, u32 exposure)
798 ret = sc2235_write_reg(sensor,
799 SC2235_REG_AEC_PK_EXPOSURE_LO,
803 return sc2235_write_reg(sensor,
804 SC2235_REG_AEC_PK_EXPOSURE_HI,
805 (exposure >> 8) & 0xff);
808 static int sc2235_get_gain(struct sc2235_dev *sensor)
813 ret = sc2235_read_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN, &gain);
817 return gain & 0x1fff;
820 static int sc2235_set_gain(struct sc2235_dev *sensor, int gain)
822 return sc2235_write_reg16(sensor, SC2235_REG_AEC_PK_REAL_GAIN,
826 static int sc2235_set_autogain(struct sc2235_dev *sensor, bool on)
828 return sc2235_mod_reg(sensor, SC2235_REG_AEC_PK_MANUAL,
829 BIT(1), on ? 0 : BIT(1));
832 static int sc2235_set_stream_dvp(struct sc2235_dev *sensor, bool on)
834 return sc2235_mod_reg(sensor, SC2235_REG_STREAM_ON,
839 static int sc2235_get_hts(struct sc2235_dev *sensor)
844 ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_HTS, &hts);
851 static int sc2235_get_vts(struct sc2235_dev *sensor)
856 ret = sc2235_read_reg16(sensor, SC2235_REG_TIMING_VTS, &vts);
863 static int sc2235_set_vts(struct sc2235_dev *sensor, int vts)
865 return sc2235_write_reg16(sensor, SC2235_REG_TIMING_VTS, vts);
869 static const struct sc2235_mode_info *
870 sc2235_find_mode(struct sc2235_dev *sensor, enum sc2235_frame_rate fr,
871 int width, int height, bool nearest)
873 const struct sc2235_mode_info *mode;
875 mode = v4l2_find_nearest_size(sc2235_mode_data,
876 ARRAY_SIZE(sc2235_mode_data),
881 (!nearest && (mode->hact != width || mode->vact != height)))
884 /* Check to see if the current mode exceeds the max frame rate */
885 if (sc2235_framerates[fr] > sc2235_framerates[mode->max_fps])
891 static u64 sc2235_calc_pixel_rate(struct sc2235_dev *sensor)
895 rate = sensor->current_mode->vtot * sensor->current_mode->htot;
896 rate *= sc2235_framerates[sensor->current_fr];
903 * sc2235_set_dvp_pclk() - Calculate the clock tree configuration values
904 * for the dvp output.
906 * @rate: The requested bandwidth per lane in bytes per second.
907 * 'Bandwidth Per Lane' is calculated as:
908 * rate = HTOT * VTOT * FPS;
910 * This function use the requested bandwidth to calculate:
911 * - rate = xclk * (64 - M) / (N * (S + 1));
918 static int sc2235_set_dvp_pclk(struct sc2235_dev *sensor,
921 u8 prediv, mult, sysdiv;
924 sc2235_calc_sys_clk(sensor, rate, &prediv, &mult,
927 st_info(ST_SENSOR, "%s, prediv = %d, mult = %d, sysdiv = %d\n",
928 __func__, prediv, mult, sysdiv);
930 ret = sc2235_mod_reg(sensor, SC2235_REG_SC_PLL_CTRL0, 0x7f,
931 (sysdiv << 4) | (prediv << 1) | ((mult & 0x20) >> 5));
935 return sc2235_mod_reg(sensor, SC2235_REG_SC_PLL_CTRL1,
940 * if sensor changes inside scaling or subsampling
941 * change mode directly
943 static int sc2235_set_mode_direct(struct sc2235_dev *sensor,
944 const struct sc2235_mode_info *mode)
949 /* Write capture setting */
950 return sc2235_load_regs(sensor, mode);
954 static int sc2235_set_mode(struct sc2235_dev *sensor)
957 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
958 const struct sc2235_mode_info *mode = sensor->current_mode;
960 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
963 /* auto gain and exposure must be turned off when changing modes */
965 ret = sc2235_set_autogain(sensor, false);
970 /* This issue will be addressed in the EVB board*/
971 /* This action will result in poor image display 2021 1111*/
973 ret = sc2235_set_autoexposure(sensor, false);
975 goto restore_auto_gain;
978 rate = sc2235_calc_pixel_rate(sensor);
979 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
980 ret = sc2235_set_dvp_pclk(sensor, rate);
986 ret = sc2235_set_mode_direct(sensor, mode);
988 goto restore_auto_exp_gain;
991 /* restore auto gain and exposure */
993 sc2235_set_autogain(sensor, true);
995 sc2235_set_autoexposure(sensor, true);
998 sensor->pending_mode_change = false;
999 sensor->last_mode = mode;
1002 restore_auto_exp_gain:
1004 sc2235_set_autoexposure(sensor, true);
1007 sc2235_set_autogain(sensor, true);
1012 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
1013 struct v4l2_mbus_framefmt *format);
1015 /* restore the last set video mode after chip power-on */
1016 static int sc2235_restore_mode(struct sc2235_dev *sensor)
1020 /* first load the initial register values */
1021 ret = sc2235_load_regs(sensor, &sc2235_mode_init_data);
1024 sensor->last_mode = &sc2235_mode_init_data;
1025 /* now restore the last capture mode */
1026 ret = sc2235_set_mode(sensor);
1030 return sc2235_set_framefmt(sensor, &sensor->fmt);
1033 static void sc2235_power(struct sc2235_dev *sensor, bool enable)
1035 if (!sensor->pwdn_gpio)
1037 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1040 static void sc2235_reset(struct sc2235_dev *sensor)
1042 if (!sensor->reset_gpio)
1045 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1047 /* camera power cycle */
1048 sc2235_power(sensor, false);
1049 usleep_range(5000, 10000);
1050 sc2235_power(sensor, true);
1051 usleep_range(5000, 10000);
1053 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1054 usleep_range(1000, 2000);
1056 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1057 usleep_range(20000, 25000);
1060 static int sc2235_set_power_on(struct sc2235_dev *sensor)
1062 struct i2c_client *client = sensor->i2c_client;
1063 struct sensor_pinctrl_info *sensor_pctrl = &sensor->sc2235_pctrl;
1066 ret = clk_prepare_enable(sensor->xclk);
1068 dev_err(&client->dev, "%s: failed to enable clock\n",
1073 ret = regulator_bulk_enable(SC2235_NUM_SUPPLIES,
1076 dev_err(&client->dev, "%s: failed to enable regulators\n",
1081 if (sensor_pctrl->use_pinctrl) {
1082 ret = pinctrl_select_state(
1083 sensor_pctrl->pinctrl,
1084 sensor_pctrl->reset_state_high);
1086 pr_err("cannot set reset pin to high\n");
1088 sc2235_reset(sensor);
1089 sc2235_power(sensor, true);
1095 clk_disable_unprepare(sensor->xclk);
1099 static void sc2235_set_power_off(struct sc2235_dev *sensor)
1101 struct sensor_pinctrl_info *sensor_pctrl = &sensor->sc2235_pctrl;
1104 if (sensor_pctrl->use_pinctrl) {
1105 ret = pinctrl_select_state(
1106 sensor_pctrl->pinctrl,
1107 sensor_pctrl->reset_state_low);
1109 pr_err("cannot set reset pin to low\n");
1111 sc2235_power(sensor, false);
1114 regulator_bulk_disable(SC2235_NUM_SUPPLIES, sensor->supplies);
1115 clk_disable_unprepare(sensor->xclk);
1118 static int sc2235_set_power_dvp(struct sc2235_dev *sensor, bool on)
1120 unsigned int flags = sensor->ep.bus.parallel.flags;
1124 * configure parallel port control lines polarity
1127 * - [5]: PCLK polarity (0: active low, 1: active high)
1128 * - [1]: HREF polarity (0: active low, 1: active high)
1129 * - [0]: VSYNC polarity (mismatch here between
1130 * datasheet and hardware, 0 is active high
1131 * and 1 is active low...)
1133 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1134 polarities |= BIT(1);
1135 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1136 polarities |= BIT(0);
1137 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1138 polarities |= BIT(5);
1140 // ret = sc2235_write_reg(sensor,
1141 // SC2235_REG_POLARITY_CTRL00,
1149 static int sc2235_set_power(struct sc2235_dev *sensor, bool on)
1154 ret = sc2235_set_power_on(sensor);
1158 ret = sc2235_restore_mode(sensor);
1163 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
1164 ret = sc2235_set_power_dvp(sensor, on);
1169 sc2235_set_power_off(sensor);
1174 sc2235_set_power_off(sensor);
1179 static int sc2235_s_power(struct v4l2_subdev *sd, int on)
1181 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1184 mutex_lock(&sensor->lock);
1187 * If the power count is modified from 0 to != 0 or from != 0 to 0,
1188 * update the power state.
1190 if (sensor->power_count == !on) {
1191 ret = sc2235_set_power(sensor, !!on);
1196 /* Update the power count. */
1197 sensor->power_count += on ? 1 : -1;
1198 WARN_ON(sensor->power_count < 0);
1200 mutex_unlock(&sensor->lock);
1202 if (on && !ret && sensor->power_count == 1) {
1203 /* restore controls */
1204 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1210 static int sc2235_try_frame_interval(struct sc2235_dev *sensor,
1211 struct v4l2_fract *fi,
1212 u32 width, u32 height)
1214 const struct sc2235_mode_info *mode;
1215 enum sc2235_frame_rate rate = SC2235_15_FPS;
1216 int minfps, maxfps, best_fps, fps;
1219 minfps = sc2235_framerates[SC2235_15_FPS];
1220 maxfps = sc2235_framerates[SC2235_60_FPS];
1222 if (fi->numerator == 0) {
1223 fi->denominator = maxfps;
1225 rate = SC2235_60_FPS;
1229 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
1233 for (i = 0; i < ARRAY_SIZE(sc2235_framerates); i++) {
1234 int curr_fps = sc2235_framerates[i];
1236 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
1237 best_fps = curr_fps;
1243 fi->denominator = best_fps;
1246 mode = sc2235_find_mode(sensor, rate, width, height, false);
1247 return mode ? rate : -EINVAL;
1250 static int sc2235_get_fmt(struct v4l2_subdev *sd,
1251 struct v4l2_subdev_state *state,
1252 struct v4l2_subdev_format *format)
1254 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1255 struct v4l2_mbus_framefmt *fmt;
1257 if (format->pad != 0)
1260 mutex_lock(&sensor->lock);
1262 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1263 fmt = v4l2_subdev_get_try_format(&sensor->sd, state,
1268 format->format = *fmt;
1270 mutex_unlock(&sensor->lock);
1275 static int sc2235_try_fmt_internal(struct v4l2_subdev *sd,
1276 struct v4l2_mbus_framefmt *fmt,
1277 enum sc2235_frame_rate fr,
1278 const struct sc2235_mode_info **new_mode)
1280 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1281 const struct sc2235_mode_info *mode;
1284 mode = sc2235_find_mode(sensor, fr, fmt->width, fmt->height, true);
1287 fmt->width = mode->hact;
1288 fmt->height = mode->vact;
1293 for (i = 0; i < ARRAY_SIZE(sc2235_formats); i++)
1294 if (sc2235_formats[i].code == fmt->code)
1296 if (i >= ARRAY_SIZE(sc2235_formats))
1299 fmt->code = sc2235_formats[i].code;
1300 fmt->colorspace = sc2235_formats[i].colorspace;
1301 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1302 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1303 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1308 static int sc2235_set_fmt(struct v4l2_subdev *sd,
1309 struct v4l2_subdev_state *state,
1310 struct v4l2_subdev_format *format)
1312 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1313 const struct sc2235_mode_info *new_mode;
1314 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
1315 struct v4l2_mbus_framefmt *fmt;
1318 if (format->pad != 0)
1320 mutex_lock(&sensor->lock);
1322 if (sensor->streaming) {
1327 ret = sc2235_try_fmt_internal(sd, mbus_fmt, 0, &new_mode);
1331 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1332 fmt = v4l2_subdev_get_try_format(sd, state, 0);
1336 if (mbus_fmt->code != sensor->fmt.code)
1337 sensor->pending_fmt_change = true;
1341 if (new_mode != sensor->current_mode) {
1342 sensor->current_mode = new_mode;
1343 sensor->pending_mode_change = true;
1345 if (new_mode->max_fps < sensor->current_fr) {
1346 sensor->current_fr = new_mode->max_fps;
1347 sensor->frame_interval.numerator = 1;
1348 sensor->frame_interval.denominator =
1349 sc2235_framerates[sensor->current_fr];
1350 sensor->current_mode = new_mode;
1351 sensor->pending_mode_change = true;
1354 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1355 sc2235_calc_pixel_rate(sensor));
1357 mutex_unlock(&sensor->lock);
1361 static int sc2235_set_framefmt(struct sc2235_dev *sensor,
1362 struct v4l2_mbus_framefmt *format)
1366 switch (format->code) {
1377 static int sc2235_set_ctrl_hue(struct sc2235_dev *sensor, int value)
1383 static int sc2235_set_ctrl_contrast(struct sc2235_dev *sensor, int value)
1389 static int sc2235_set_ctrl_saturation(struct sc2235_dev *sensor, int value)
1395 static int sc2235_set_ctrl_white_balance(struct sc2235_dev *sensor, int awb)
1401 static int sc2235_set_ctrl_exposure(struct sc2235_dev *sensor,
1402 enum v4l2_exposure_auto_type auto_exposure)
1404 struct sc2235_ctrls *ctrls = &sensor->ctrls;
1405 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
1408 if (ctrls->auto_exp->is_new) {
1409 ret = sc2235_set_autoexposure(sensor, auto_exp);
1414 if (!auto_exp && ctrls->exposure->is_new) {
1417 ret = sc2235_get_vts(sensor);
1423 if (ctrls->exposure->val < max_exp)
1424 ret = sc2235_set_exposure(sensor, ctrls->exposure->val);
1430 static int sc2235_set_ctrl_gain(struct sc2235_dev *sensor, bool auto_gain)
1432 struct sc2235_ctrls *ctrls = &sensor->ctrls;
1435 if (ctrls->auto_gain->is_new) {
1436 ret = sc2235_set_autogain(sensor, auto_gain);
1441 if (!auto_gain && ctrls->gain->is_new)
1442 ret = sc2235_set_gain(sensor, ctrls->gain->val);
1447 static const char * const test_pattern_menu[] = {
1453 #define SC2235_TEST_ENABLE BIT(3)
1454 #define SC2235_TEST_BLACK (3 << 0)
1456 static int sc2235_set_ctrl_test_pattern(struct sc2235_dev *sensor, int value)
1460 *For 7110 platform, refer to 1125 FW code configuration. This operation will cause the image to be white.
1463 ret = sc2235_mod_reg(sensor, SC2235_REG_TEST_SET0, BIT(3),
1466 ret |= sc2235_mod_reg(sensor, SC2235_REG_TEST_SET1, BIT(6),
1472 static int sc2235_set_ctrl_light_freq(struct sc2235_dev *sensor, int value)
1477 static int sc2235_set_ctrl_hflip(struct sc2235_dev *sensor, int value)
1479 return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1481 (!(value ^ sensor->upside_down)) ?
1482 (BIT(2) | BIT(1)) : 0);
1485 static int sc2235_set_ctrl_vflip(struct sc2235_dev *sensor, int value)
1487 return sc2235_mod_reg(sensor, SC2235_REG_TIMING_TC_REG21,
1489 (value ^ sensor->upside_down) ?
1490 (BIT(6) | BIT(5)) : 0);
1493 static int sc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1495 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1496 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1499 /* v4l2_ctrl_lock() locks our own mutex */
1502 case V4L2_CID_AUTOGAIN:
1503 val = sc2235_get_gain(sensor);
1506 sensor->ctrls.gain->val = val;
1508 case V4L2_CID_EXPOSURE_AUTO:
1509 val = sc2235_get_exposure(sensor);
1512 sensor->ctrls.exposure->val = val;
1519 static int sc2235_s_ctrl(struct v4l2_ctrl *ctrl)
1521 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1522 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1525 /* v4l2_ctrl_lock() locks our own mutex */
1528 * If the device is not powered up by the host driver do
1529 * not apply any controls to H/W at this time. Instead
1530 * the controls will be restored right after power-up.
1532 if (sensor->power_count == 0)
1536 case V4L2_CID_AUTOGAIN:
1537 ret = sc2235_set_ctrl_gain(sensor, ctrl->val);
1539 case V4L2_CID_EXPOSURE_AUTO:
1540 ret = sc2235_set_ctrl_exposure(sensor, ctrl->val);
1542 case V4L2_CID_AUTO_WHITE_BALANCE:
1543 ret = sc2235_set_ctrl_white_balance(sensor, ctrl->val);
1546 ret = sc2235_set_ctrl_hue(sensor, ctrl->val);
1548 case V4L2_CID_CONTRAST:
1549 ret = sc2235_set_ctrl_contrast(sensor, ctrl->val);
1551 case V4L2_CID_SATURATION:
1552 ret = sc2235_set_ctrl_saturation(sensor, ctrl->val);
1554 case V4L2_CID_TEST_PATTERN:
1555 ret = sc2235_set_ctrl_test_pattern(sensor, ctrl->val);
1557 case V4L2_CID_POWER_LINE_FREQUENCY:
1558 ret = sc2235_set_ctrl_light_freq(sensor, ctrl->val);
1560 case V4L2_CID_HFLIP:
1561 ret = sc2235_set_ctrl_hflip(sensor, ctrl->val);
1563 case V4L2_CID_VFLIP:
1564 ret = sc2235_set_ctrl_vflip(sensor, ctrl->val);
1574 static const struct v4l2_ctrl_ops sc2235_ctrl_ops = {
1575 .g_volatile_ctrl = sc2235_g_volatile_ctrl,
1576 .s_ctrl = sc2235_s_ctrl,
1579 static int sc2235_init_controls(struct sc2235_dev *sensor)
1581 const struct v4l2_ctrl_ops *ops = &sc2235_ctrl_ops;
1582 struct sc2235_ctrls *ctrls = &sensor->ctrls;
1583 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1586 v4l2_ctrl_handler_init(hdl, 32);
1588 /* we can use our own mutex for the ctrl lock */
1589 hdl->lock = &sensor->lock;
1591 /* Clock related controls */
1592 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
1594 sc2235_calc_pixel_rate(sensor));
1596 /* Auto/manual white balance */
1597 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1598 V4L2_CID_AUTO_WHITE_BALANCE,
1600 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1602 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1604 /* Auto/manual exposure */
1607 *For 7110 platform, This operation will cause the image to be white.
1609 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1610 V4L2_CID_EXPOSURE_AUTO,
1611 V4L2_EXPOSURE_MANUAL, 0,
1612 V4L2_EXPOSURE_AUTO);
1613 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1615 /* Auto/manual gain */
1616 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1618 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1621 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1622 V4L2_CID_EXPOSURE_AUTO,
1623 V4L2_EXPOSURE_MANUAL, 0,
1625 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1626 0, 65535, 1, 0x4600);
1627 /* Auto/manual gain */
1628 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1630 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1633 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
1635 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
1637 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
1639 ctrls->test_pattern =
1640 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
1641 ARRAY_SIZE(test_pattern_menu) - 1,
1642 0, 0, test_pattern_menu); //0x02
1643 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
1645 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
1649 v4l2_ctrl_new_std_menu(hdl, ops,
1650 V4L2_CID_POWER_LINE_FREQUENCY,
1651 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
1652 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
1659 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1660 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
1661 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
1663 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
1664 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
1665 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
1667 sensor->sd.ctrl_handler = hdl;
1671 v4l2_ctrl_handler_free(hdl);
1675 static int sc2235_enum_frame_size(struct v4l2_subdev *sd,
1676 struct v4l2_subdev_state *state,
1677 struct v4l2_subdev_frame_size_enum *fse)
1681 if (fse->index >= SC2235_NUM_MODES)
1685 sc2235_mode_data[fse->index].hact;
1686 fse->max_width = fse->min_width;
1688 sc2235_mode_data[fse->index].vact;
1689 fse->max_height = fse->min_height;
1694 static int sc2235_enum_frame_interval(
1695 struct v4l2_subdev *sd,
1696 struct v4l2_subdev_state *state,
1697 struct v4l2_subdev_frame_interval_enum *fie)
1699 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1700 struct v4l2_fract tpf;
1705 if (fie->index >= SC2235_NUM_FRAMERATES)
1709 tpf.denominator = sc2235_framerates[fie->index];
1711 ret = sc2235_try_frame_interval(sensor, &tpf,
1712 fie->width, fie->height);
1716 fie->interval = tpf;
1720 static int sc2235_g_frame_interval(struct v4l2_subdev *sd,
1721 struct v4l2_subdev_frame_interval *fi)
1723 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1725 mutex_lock(&sensor->lock);
1726 fi->interval = sensor->frame_interval;
1727 mutex_unlock(&sensor->lock);
1732 static int sc2235_s_frame_interval(struct v4l2_subdev *sd,
1733 struct v4l2_subdev_frame_interval *fi)
1735 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1736 const struct sc2235_mode_info *mode;
1737 int frame_rate, ret = 0;
1742 mutex_lock(&sensor->lock);
1744 if (sensor->streaming) {
1749 mode = sensor->current_mode;
1751 frame_rate = sc2235_try_frame_interval(sensor, &fi->interval,
1752 mode->hact, mode->vact);
1753 if (frame_rate < 0) {
1754 /* Always return a valid frame interval value */
1755 fi->interval = sensor->frame_interval;
1759 mode = sc2235_find_mode(sensor, frame_rate, mode->hact,
1766 if (mode != sensor->current_mode ||
1767 frame_rate != sensor->current_fr) {
1768 sensor->current_fr = frame_rate;
1769 sensor->frame_interval = fi->interval;
1770 sensor->current_mode = mode;
1771 sensor->pending_mode_change = true;
1773 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
1774 sc2235_calc_pixel_rate(sensor));
1777 mutex_unlock(&sensor->lock);
1781 static int sc2235_enum_mbus_code(struct v4l2_subdev *sd,
1782 struct v4l2_subdev_state *state,
1783 struct v4l2_subdev_mbus_code_enum *code)
1787 if (code->index >= ARRAY_SIZE(sc2235_formats))
1790 code->code = sc2235_formats[code->index].code;
1794 static int sc2235_s_stream(struct v4l2_subdev *sd, int enable)
1796 struct sc2235_dev *sensor = to_sc2235_dev(sd);
1799 mutex_lock(&sensor->lock);
1801 if (sensor->streaming == !enable) {
1802 if (enable && sensor->pending_mode_change) {
1803 ret = sc2235_set_mode(sensor);
1808 if (enable && sensor->pending_fmt_change) {
1809 ret = sc2235_set_framefmt(sensor, &sensor->fmt);
1812 sensor->pending_fmt_change = false;
1815 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL)
1816 ret = sc2235_set_stream_dvp(sensor, enable);
1819 sensor->streaming = enable;
1822 mutex_unlock(&sensor->lock);
1827 static const struct v4l2_subdev_core_ops sc2235_core_ops = {
1828 .s_power = sc2235_s_power,
1829 .log_status = v4l2_ctrl_subdev_log_status,
1830 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1831 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1834 static const struct v4l2_subdev_video_ops sc2235_video_ops = {
1835 .g_frame_interval = sc2235_g_frame_interval,
1836 .s_frame_interval = sc2235_s_frame_interval,
1837 .s_stream = sc2235_s_stream,
1840 static const struct v4l2_subdev_pad_ops sc2235_pad_ops = {
1841 .enum_mbus_code = sc2235_enum_mbus_code,
1842 .get_fmt = sc2235_get_fmt,
1843 .set_fmt = sc2235_set_fmt,
1844 .enum_frame_size = sc2235_enum_frame_size,
1845 .enum_frame_interval = sc2235_enum_frame_interval,
1848 static const struct v4l2_subdev_ops sc2235_subdev_ops = {
1849 .core = &sc2235_core_ops,
1850 .video = &sc2235_video_ops,
1851 .pad = &sc2235_pad_ops,
1854 static int sc2235_get_regulators(struct sc2235_dev *sensor)
1858 for (i = 0; i < SC2235_NUM_SUPPLIES; i++)
1859 sensor->supplies[i].supply = sc2235_supply_name[i];
1861 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
1862 SC2235_NUM_SUPPLIES,
1866 static int sc2235_check_chip_id(struct sc2235_dev *sensor)
1868 struct i2c_client *client = sensor->i2c_client;
1872 ret = sc2235_set_power_on(sensor);
1876 ret = sc2235_read_reg16(sensor, SC2235_REG_CHIP_ID, &chip_id);
1878 dev_err(&client->dev, "%s: failed to read chip identifier\n",
1883 if (chip_id != SC2235_CHIP_ID) {
1884 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x%x, got 0x%x\n",
1885 __func__, SC2235_CHIP_ID, chip_id);
1888 dev_err(&client->dev, "%s: chip identifier, got 0x%x\n",
1892 sc2235_set_power_off(sensor);
1896 static int sc2235_probe(struct i2c_client *client)
1898 struct device *dev = &client->dev;
1899 struct fwnode_handle *endpoint;
1900 struct sc2235_dev *sensor;
1901 struct v4l2_mbus_framefmt *fmt;
1905 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1909 sensor->i2c_client = client;
1912 fmt->code = MEDIA_BUS_FMT_SGBRG10_1X10;
1913 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1914 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1915 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1916 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1919 fmt->field = V4L2_FIELD_NONE;
1920 sensor->frame_interval.numerator = 1;
1921 sensor->frame_interval.denominator = sc2235_framerates[SC2235_30_FPS];
1922 sensor->current_fr = SC2235_30_FPS;
1923 sensor->current_mode =
1924 &sc2235_mode_data[SC2235_MODE_1080P_1920_1080];
1925 sensor->last_mode = sensor->current_mode;
1927 sensor->ae_target = 52;
1929 /* optional indication of physical rotation of sensor */
1930 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
1935 sensor->upside_down = true;
1940 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
1945 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
1948 dev_err(dev, "endpoint node not found\n");
1952 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
1953 fwnode_handle_put(endpoint);
1955 dev_err(dev, "Could not parse endpoint\n");
1959 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
1960 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
1961 sensor->ep.bus_type != V4L2_MBUS_BT656) {
1962 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
1966 /* get system clock (xclk) */
1967 sensor->xclk = devm_clk_get(dev, "xclk");
1968 if (IS_ERR(sensor->xclk)) {
1969 dev_err(dev, "failed to get xclk\n");
1970 return PTR_ERR(sensor->xclk);
1973 sensor->xclk_freq = clk_get_rate(sensor->xclk);
1974 if (sensor->xclk_freq < SC2235_XCLK_MIN ||
1975 sensor->xclk_freq > SC2235_XCLK_MAX) {
1976 dev_err(dev, "xclk frequency out of range: %d Hz\n",
1981 ret = sc2235_sensor_pinctrl_init(&sensor->sc2235_pctrl, dev);
1983 pr_err("Can't get pinctrl, use gpio to ctrl\n");
1984 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1985 sensor->sc2235_pctrl.use_pinctrl = false;
1988 v4l2_i2c_subdev_init(&sensor->sd, client, &sc2235_subdev_ops);
1990 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1991 V4L2_SUBDEV_FL_HAS_EVENTS;
1992 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1993 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1994 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
1998 ret = sc2235_get_regulators(sensor);
2001 mutex_init(&sensor->lock);
2003 ret = sc2235_check_chip_id(sensor);
2005 goto entity_cleanup;
2007 ret = sc2235_init_controls(sensor);
2009 goto entity_cleanup;
2011 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
2018 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2020 media_entity_cleanup(&sensor->sd.entity);
2021 mutex_destroy(&sensor->lock);
2025 static int sc2235_remove(struct i2c_client *client)
2027 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2028 struct sc2235_dev *sensor = to_sc2235_dev(sd);
2030 v4l2_async_unregister_subdev(&sensor->sd);
2031 media_entity_cleanup(&sensor->sd.entity);
2032 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2033 mutex_destroy(&sensor->lock);
2038 static const struct i2c_device_id sc2235_id[] = {
2042 MODULE_DEVICE_TABLE(i2c, sc2235_id);
2044 static const struct of_device_id sc2235_dt_ids[] = {
2045 { .compatible = "sc2235" },
2048 MODULE_DEVICE_TABLE(of, sc2235_dt_ids);
2050 static struct i2c_driver sc2235_i2c_driver = {
2053 .of_match_table = sc2235_dt_ids,
2055 .id_table = sc2235_id,
2056 .probe_new = sc2235_probe,
2057 .remove = sc2235_remove,
2060 module_i2c_driver(sc2235_i2c_driver);
2062 MODULE_DESCRIPTION("SC2235 MIPI Camera Subdev Driver");
2063 MODULE_LICENSE("GPL");