1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * sc210iot sensor driver
4 *
5 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6 *
7 * V0.0X01.0X00 first version.
8 * V0.0X01.0X01 add quick stream on/off
9 */
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/of_graph.h>
17 #include <linux/regmap.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/version.h>
21 #include <linux/rk-camera-module.h>
22 #include <linux/rk-preisp.h>
23 #include <media/v4l2-async.h>
24 #include <media/media-entity.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-fwnode.h>
28 #include <media/v4l2-subdev.h>
29 #include "../platform/rockchip/isp/rkisp_tb_helper.h"
30
31 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x01)
32
33 #define OF_CAMERA_PINCTRL_STATE_DEFAULT "rockchip,camera_default"
34 #define OF_CAMERA_PINCTRL_STATE_SLEEP "rockchip,camera_sleep"
35
36 #define SC210IOT_NAME "sc210iot"
37 #define SC210IOT_MEDIA_BUS_FMT MEDIA_BUS_FMT_SBGGR10_1X10
38 #define MIPI_FREQ 371250000
39 #define SC210IOT_XVCLK_FREQ 27000000
40
41 #define SC210IOT_REG_CHIP_ID_H 0x3108
42 #define SC210IOT_REG_CHIP_ID_L 0x3107
43
44 #define SC210IOT_REG_EXP_LONG_H 0x3e00
45 #define SC210IOT_REG_EXP_LONG_M 0x3e01
46 #define SC210IOT_REG_EXP_LONG_L 0x3e02
47
48 #define SC210IOT_REG_GAIN_LONG_3 0x3e06
49 #define SC210IOT_REG_GAIN_LONG_2 0x3e07
50 #define SC210IOT_REG_GAIN_LONG_1 0x3e08
51 #define SC210IOT_REG_GAIN_LONG_0 0x3e09
52
53 #define SC210IOT_REG_MIRROR_FLIP 0x3221
54 #define MIRROR_MASK 0x6
55 #define FLIP_MASK 0x60
56
57 #define SC210IOT_REG_CTRL_MODE 0x0100
58 #define SC210IOT_MODE_SW_STANDBY 0x0
59 #define SC210IOT_MODE_STREAMING BIT(0)
60
61 #define SC210IOT_CHIP_ID 0x17cb
62
63 #define SC210IOT_REG_VTS_H 0x320e
64 #define SC210IOT_REG_VTS_L 0x320f
65 #define SC210IOT_VTS_MAX 0x3FFF
66 #define SC210IOT_HTS_MAX 0xFFF
67
68 #define SC210IOT_EXPOSURE_MAX 0x3FFF
69 #define SC210IOT_EXPOSURE_MIN 1
70 #define SC210IOT_EXPOSURE_STEP 1
71
72 #define SC210IOT_GAIN_MIN 0x40
73 #define SC210IOT_GAIN_MAX 0x8000
74 #define SC210IOT_GAIN_STEP 1
75 #define SC210IOT_GAIN_DEFAULT 64
76
77 #define SC210IOT_SOFTWARE_RESET_REG 0x0103
78
79 #define SC210IOT_LANES 2
80
81 static const char * const sc210iot_supply_names[] = {
82 "dovdd", /* Digital I/O power */
83 "avdd", /* Analog power */
84 "dvdd", /* Digital power */
85 };
86
87 #define SC210IOT_NUM_SUPPLIES ARRAY_SIZE(sc210iot_supply_names)
88
89 #define to_sc210iot(sd) container_of(sd, struct sc210iot, subdev)
90
91 enum {
92 LINK_FREQ_INDEX,
93 };
94
95 struct gain_section {
96 u16 min_gain;
97 u16 max_gain;
98 u16 again_regs_start;
99 u16 again_regs_stop;
100 u16 again_deviation;
101 u16 dgain_regs_start;
102 u16 dgain_regs_stop;
103 u16 dgain_deviation;
104 u16 steps;
105 };
106
107 struct sc210iot_mode {
108 u32 width;
109 u32 height;
110 struct v4l2_fract max_fps;
111 u32 hts_def;
112 u32 vts_def;
113 u32 exp_def;
114 u32 link_freq_index;
115 const struct reg_sequence *reg_list;
116 u32 reg_num;
117 u32 hdr_mode;
118 u32 vc[PAD_MAX];
119 };
120
121 struct sc210iot {
122 struct device *dev;
123 struct clk *xvclk;
124 struct regmap *regmap;
125 struct gpio_desc *reset_gpio;
126 struct gpio_desc *pwdn_gpio;
127 struct regulator_bulk_data supplies[SC210IOT_NUM_SUPPLIES];
128 struct pinctrl *pinctrl;
129 struct pinctrl_state *pins_default;
130 struct pinctrl_state *pins_sleep;
131 struct v4l2_subdev subdev;
132 struct media_pad pad;
133 struct v4l2_ctrl_handler ctrl_handler;
134 struct v4l2_ctrl *exposure;
135 struct v4l2_ctrl *anal_gain;
136 struct v4l2_ctrl *hblank;
137 struct v4l2_ctrl *vblank;
138 struct v4l2_ctrl *h_flip;
139 struct v4l2_ctrl *v_flip;
140 struct v4l2_ctrl *link_freq;
141 struct v4l2_ctrl *pixel_rate;
142 struct mutex lock;
143 struct v4l2_fract cur_fps;
144 u32 cur_vts;
145 bool streaming;
146 bool power_on;
147 bool is_thunderboot;
148 bool is_thunderboot_ng;
149 bool is_first_streamoff;
150 unsigned int cfg_num;
151 const struct sc210iot_mode *cur_mode;
152 u32 module_index;
153 const char *module_facing;
154 const char *module_name;
155 const char *len_name;
156 bool has_init_exp;
157 struct preisp_hdrae_exp_s init_hdrae_exp;
158 };
159
160 static const struct regmap_config sc210iot_regmap_config = {
161 .reg_bits = 16,
162 .val_bits = 8,
163 .max_register = 0x6f00,
164 };
165
166 static const s64 link_freq_menu_items[] = {
167 MIPI_FREQ,
168 };
169
170 /*
171 * window size=1920*1080 mipi@2lane
172 * mclk=27M mipi_clk=371.25Mbps
173 * pixel_line_total=2200 line_frame_total=1125
174 * row_time=29.62us frame_rate=30fps
175 */
176 static const struct reg_sequence sc210iot_1080p_liner_30fps_settings[] = {
177 {0x0103, 0x01},
178 {0x0100, 0x00},
179 {0x36e9, 0x80},
180 {0x36f9, 0x80},
181 {0x301c, 0x78},
182 {0x3208, 0x07},
183 {0x3209, 0x80},
184 {0x320a, 0x04},
185 {0x320b, 0x38},
186 {0x320e, 0x04},
187 {0x320f, 0x65},
188 {0x3214, 0x11},
189 {0x3215, 0x11},
190 {0x3253, 0x0c},
191 {0x3274, 0x09},
192 {0x3301, 0x05},
193 {0x3304, 0x68},
194 {0x3306, 0x40},
195 {0x330b, 0xcc},
196 {0x331c, 0x01},
197 {0x331e, 0x61},
198 {0x3333, 0x10},
199 {0x3364, 0x17},
200 {0x3391, 0x18},
201 {0x3392, 0x38},
202 {0x3393, 0x08},
203 {0x3394, 0x0b},
204 {0x3395, 0x50},
205 {0x3620, 0x88},
206 {0x3622, 0x06},
207 {0x3630, 0xf8},
208 {0x3634, 0x44},
209 {0x3637, 0x16},
210 {0x363a, 0x1f},
211 {0x3670, 0x1c},
212 {0x3677, 0x84},
213 {0x3678, 0x86},
214 {0x3679, 0x8b},
215 {0x367e, 0x18},
216 {0x367f, 0x38},
217 {0x3690, 0x53},
218 {0x3691, 0x63},
219 {0x3692, 0x63},
220 {0x369c, 0x08},
221 {0x369d, 0x38},
222 {0x36a4, 0x08},
223 {0x36a5, 0x18},
224 {0x36a8, 0x08},
225 {0x36a9, 0x28},
226 {0x36aa, 0x2a},
227 {0x36fc, 0x11},
228 {0x36fd, 0x14},
229 {0x3e01, 0x8c},
230 {0x3e03, 0x0b},
231 {0x3e08, 0x03},
232 {0x3e09, 0x20},
233 {0x3e1b, 0x15},
234 {0x3f03, 0x01},
235 {0x36e9, 0x20},
236 {0x36f9, 0x24},
237 {0x0100, 0x01},
238 };
239
240 static const struct sc210iot_mode supported_modes[] = {
241 {
242 .width = 1920,
243 .height = 1080,
244 .max_fps = {
245 .numerator = 10000,
246 .denominator = 300000,
247 },
248 .exp_def = 0x460,
249 .hts_def = 0x898,
250 .vts_def = 0x465,
251 .link_freq_index = LINK_FREQ_INDEX,
252 .reg_list = sc210iot_1080p_liner_30fps_settings,
253 .reg_num = ARRAY_SIZE(sc210iot_1080p_liner_30fps_settings),
254 .hdr_mode = NO_HDR,
255 .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
256 },
257 };
258
259 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
to_pixel_rate(u32 index)260 static u64 to_pixel_rate(u32 index)
261 {
262 u64 pixel_rate = link_freq_menu_items[index] * 2 * SC210IOT_LANES;
263
264 do_div(pixel_rate, 10);
265 return pixel_rate;
266 }
267
sc210iot_read_reg(struct sc210iot * sc210iot,u16 addr,u8 * value)268 static inline int sc210iot_read_reg(struct sc210iot *sc210iot, u16 addr, u8 *value)
269 {
270 unsigned int val;
271 int ret;
272
273 ret = regmap_read(sc210iot->regmap, addr, &val);
274 if (ret) {
275 dev_err(sc210iot->dev, "i2c read failed at addr: %x\n", addr);
276 return ret;
277 }
278 *value = val & 0xff;
279 return 0;
280 }
281
282 static int __sc210iot_power_on(struct sc210iot *sc210iot);
283
sc210iot_write_reg(struct sc210iot * sc210iot,u16 addr,u8 value)284 static inline int sc210iot_write_reg(struct sc210iot *sc210iot, u16 addr, u8 value)
285 {
286 int ret;
287
288 ret = regmap_write(sc210iot->regmap, addr, value);
289 if (ret) {
290 dev_err(sc210iot->dev, "i2c write failed at addr: %x\n", addr);
291 return ret;
292 }
293 return ret;
294 }
295
296 static const struct gain_section gain_sections[] = {
297 { 64, 128, 0x0320, 0x033f, 64, 0x0080, 0x0080, 0, 32 },
298 { 128, 256, 0x0720, 0x073f, 128, 0x0080, 0x0080, 0, 32 },
299 { 256, 512, 0x0f20, 0x0f3f, 256, 0x0080, 0x0080, 0, 32 },
300 { 512, 1024, 0x1f20, 0x1f3f, 512, 0x0080, 0x0080, 0, 32 },
301 { 1024, 2048, 0x1f3f, 0x1f3f, 0, 0x0080, 0x00fc, 1024, 32 },
302 { 2048, 4096, 0x1f3f, 0x1f3f, 0, 0x0180, 0x01fc, 2048, 32 },
303 { 4096, 8192, 0x1f3f, 0x1f3f, 0, 0x0380, 0x03fc, 4096, 32 },
304 { 8192, 16384, 0x1f3f, 0x1f3f, 0, 0x0780, 0x07fc, 8192, 32 },
305 { 16384, 32768, 0x1f3f, 0x1f3f, 0, 0x0f80, 0x0ffc, 16384, 32 }
306 };
307
sc210iot_set_gain(struct sc210iot * sc210iot,u32 gain)308 static int sc210iot_set_gain(struct sc210iot *sc210iot, u32 gain)
309 {
310 int ret, i = 0;
311 int offset, step, step_len, reg_step_len;
312 int a_gain = 0, d_gain = 0;
313
314 dev_dbg(sc210iot->dev, "%s: gain : %d\n", __func__, gain);
315 if (sc210iot->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
316 sc210iot->is_thunderboot = false;
317 sc210iot->is_thunderboot_ng = true;
318 __sc210iot_power_on(sc210iot);
319 }
320
321 for (i = 0; i < ARRAY_SIZE(gain_sections) - 1; i++)
322 if ((gain_sections[i].min_gain <= gain) && (gain < gain_sections[i].max_gain))
323 break;
324 if (gain_sections[i].again_deviation) {
325 offset = gain - gain_sections[i].min_gain;
326 step_len = gain_sections[i].again_deviation / gain_sections[i].steps;
327 reg_step_len = 1;
328 step = offset / step_len;
329 a_gain = gain_sections[i].again_regs_start + step * reg_step_len;
330 d_gain = gain_sections[i].dgain_regs_start;
331 } else {
332 offset = gain - gain_sections[i].min_gain;
333 step_len = gain_sections[i].dgain_deviation / gain_sections[i].steps;
334 step = offset / step_len;
335 reg_step_len = 4;
336 a_gain = gain_sections[i].again_regs_start;
337 d_gain = gain_sections[i].dgain_regs_start + step * reg_step_len;
338 }
339
340 if (a_gain > gain_sections[i].again_regs_stop)
341 a_gain = gain_sections[i].again_regs_stop;
342 if (d_gain > gain_sections[i].dgain_regs_stop)
343 d_gain = gain_sections[i].dgain_regs_stop;
344
345 dev_dbg(sc210iot->dev, "%s: a_gain: 0x%x d_gain: 0x%x\n", __func__, a_gain, d_gain);
346
347 ret = sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_1, a_gain >> 8);
348 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_0, a_gain & 0xff);
349 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_3, d_gain >> 8);
350 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_2, d_gain & 0xff);
351 return ret;
352 }
353
sc210iot_set_exp(struct sc210iot * sc210iot,u32 exp)354 static int sc210iot_set_exp(struct sc210iot *sc210iot, u32 exp)
355 {
356 int ret;
357
358 dev_dbg(sc210iot->dev, "%s: exp : %d\n", __func__, exp);
359 ret = sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_H,
360 (exp >> 12) & 0xf);
361 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_M,
362 (exp >> 4) & 0xff);
363 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_L,
364 (exp & 0xf) << 4);
365 return ret;
366 }
367
sc210iot_modify_fps_info(struct sc210iot * sc210iot)368 static void sc210iot_modify_fps_info(struct sc210iot *sc210iot)
369 {
370 const struct sc210iot_mode *mode = sc210iot->cur_mode;
371
372 sc210iot->cur_fps.denominator = mode->max_fps.denominator * mode->vts_def /
373 sc210iot->cur_vts;
374 }
375
sc210iot_set_ctrl(struct v4l2_ctrl * ctrl)376 static int sc210iot_set_ctrl(struct v4l2_ctrl *ctrl)
377 {
378 struct sc210iot *sc210iot = container_of(ctrl->handler,
379 struct sc210iot, ctrl_handler);
380 s64 max;
381 int ret = 0;
382
383 /* Propagate change of current control to all related controls */
384 switch (ctrl->id) {
385 case V4L2_CID_VBLANK:
386 /* Update max exposure while meeting expected vblanking */
387 max = sc210iot->cur_mode->height + ctrl->val - 4;
388 __v4l2_ctrl_modify_range(sc210iot->exposure,
389 sc210iot->exposure->minimum, max,
390 sc210iot->exposure->step,
391 sc210iot->exposure->default_value);
392 break;
393 }
394 if (!pm_runtime_get_if_in_use(sc210iot->dev))
395 return 0;
396 switch (ctrl->id) {
397 case V4L2_CID_EXPOSURE:
398 ret = sc210iot_set_exp(sc210iot, ctrl->val << 1);
399 break;
400 case V4L2_CID_ANALOGUE_GAIN:
401 ret = sc210iot_set_gain(sc210iot, ctrl->val);
402 break;
403 case V4L2_CID_VBLANK:
404 dev_dbg(sc210iot->dev, "set vblank 0x%x\n", ctrl->val);
405 ret = sc210iot_write_reg(sc210iot, SC210IOT_REG_VTS_H,
406 (ctrl->val + sc210iot->cur_mode->height) >> 8);
407 ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_VTS_L,
408 (ctrl->val + sc210iot->cur_mode->height) & 0xff);
409 if (!ret)
410 sc210iot->cur_vts = ctrl->val + sc210iot->cur_mode->height;
411 sc210iot_modify_fps_info(sc210iot);
412 break;
413 case V4L2_CID_HFLIP:
414 regmap_update_bits(sc210iot->regmap, SC210IOT_REG_MIRROR_FLIP,
415 MIRROR_MASK, ctrl->val ? MIRROR_MASK : 0);
416 break;
417 case V4L2_CID_VFLIP:
418 regmap_update_bits(sc210iot->regmap, SC210IOT_REG_MIRROR_FLIP,
419 FLIP_MASK, ctrl->val ? FLIP_MASK : 0);
420 break;
421 default:
422 dev_warn(sc210iot->dev, "%s Unhandled id:0x%x, val:0x%x\n",
423 __func__, ctrl->id, ctrl->val);
424 break;
425 }
426 pm_runtime_put(sc210iot->dev);
427 return ret;
428 }
429
430 static const struct v4l2_ctrl_ops sc210iot_ctrl_ops = {
431 .s_ctrl = sc210iot_set_ctrl,
432 };
433
sc210iot_get_regulators(struct sc210iot * sc210iot)434 static int sc210iot_get_regulators(struct sc210iot *sc210iot)
435 {
436 unsigned int i;
437
438 for (i = 0; i < SC210IOT_NUM_SUPPLIES; i++)
439 sc210iot->supplies[i].supply = sc210iot_supply_names[i];
440 return devm_regulator_bulk_get(sc210iot->dev,
441 SC210IOT_NUM_SUPPLIES,
442 sc210iot->supplies);
443 }
444
sc210iot_initialize_controls(struct sc210iot * sc210iot)445 static int sc210iot_initialize_controls(struct sc210iot *sc210iot)
446 {
447 const struct sc210iot_mode *mode;
448 struct v4l2_ctrl_handler *handler;
449 s64 exposure_max, vblank_def;
450 u32 h_blank;
451 int ret;
452
453 handler = &sc210iot->ctrl_handler;
454 mode = sc210iot->cur_mode;
455 ret = v4l2_ctrl_handler_init(handler, 8);
456 if (ret)
457 return ret;
458 handler->lock = &sc210iot->lock;
459 sc210iot->link_freq = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
460 ARRAY_SIZE(link_freq_menu_items) - 1, 0,
461 link_freq_menu_items);
462 sc210iot->pixel_rate = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
463 0, to_pixel_rate(LINK_FREQ_INDEX),
464 1, to_pixel_rate(LINK_FREQ_INDEX));
465 h_blank = mode->hts_def - mode->width;
466 sc210iot->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
467 h_blank, h_blank, 1, h_blank);
468 if (sc210iot->hblank)
469 sc210iot->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
470 vblank_def = mode->vts_def - mode->height;
471 sc210iot->vblank = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
472 V4L2_CID_VBLANK, vblank_def,
473 SC210IOT_VTS_MAX - mode->height,
474 1, vblank_def);
475 exposure_max = mode->vts_def - 4;
476 sc210iot->exposure = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
477 V4L2_CID_EXPOSURE, SC210IOT_EXPOSURE_MIN,
478 exposure_max, SC210IOT_EXPOSURE_STEP,
479 mode->exp_def);
480 sc210iot->anal_gain = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
481 V4L2_CID_ANALOGUE_GAIN, SC210IOT_GAIN_MIN,
482 SC210IOT_GAIN_MAX, SC210IOT_GAIN_STEP,
483 SC210IOT_GAIN_DEFAULT);
484 sc210iot->h_flip = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
485 V4L2_CID_HFLIP, 0, 1, 1, 0);
486 sc210iot->v_flip = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
487 V4L2_CID_VFLIP, 0, 1, 1, 0);
488 if (handler->error) {
489 ret = handler->error;
490 dev_err(sc210iot->dev, "Failed to init controls(%d)\n", ret);
491 goto err_free_handler;
492 }
493 sc210iot->subdev.ctrl_handler = handler;
494 sc210iot->has_init_exp = false;
495 sc210iot->cur_fps = mode->max_fps;
496 sc210iot->cur_vts = mode->vts_def;
497 return 0;
498 err_free_handler:
499 v4l2_ctrl_handler_free(handler);
500 return ret;
501 }
502
__sc210iot_power_on(struct sc210iot * sc210iot)503 static int __sc210iot_power_on(struct sc210iot *sc210iot)
504 {
505 int ret;
506 struct device *dev = sc210iot->dev;
507
508 if (sc210iot->is_thunderboot)
509 return 0;
510
511 if (!IS_ERR_OR_NULL(sc210iot->pins_default)) {
512 ret = pinctrl_select_state(sc210iot->pinctrl,
513 sc210iot->pins_default);
514 if (ret < 0)
515 dev_err(dev, "could not set pins\n");
516 }
517 ret = clk_set_rate(sc210iot->xvclk, SC210IOT_XVCLK_FREQ);
518 if (ret < 0)
519 dev_warn(dev, "Failed to set xvclk rate\n");
520 if (clk_get_rate(sc210iot->xvclk) != SC210IOT_XVCLK_FREQ)
521 dev_warn(dev, "xvclk mismatched, modes are based on 27MHz\n");
522 ret = clk_prepare_enable(sc210iot->xvclk);
523 if (ret < 0) {
524 dev_err(dev, "Failed to enable xvclk\n");
525 return ret;
526 }
527 ret = regulator_bulk_enable(SC210IOT_NUM_SUPPLIES, sc210iot->supplies);
528 if (ret < 0) {
529 dev_err(dev, "Failed to enable regulators\n");
530 goto disable_clk;
531 }
532 if (!IS_ERR(sc210iot->reset_gpio))
533 gpiod_direction_output(sc210iot->reset_gpio, 1);
534 usleep_range(1000, 2000);
535 if (!IS_ERR(sc210iot->pwdn_gpio))
536 gpiod_direction_output(sc210iot->pwdn_gpio, 1);
537 if (!IS_ERR(sc210iot->reset_gpio))
538 gpiod_direction_output(sc210iot->reset_gpio, 0);
539 usleep_range(10000, 20000);
540 return 0;
541 disable_clk:
542 clk_disable_unprepare(sc210iot->xvclk);
543
544 if (!IS_ERR_OR_NULL(sc210iot->pins_sleep))
545 pinctrl_select_state(sc210iot->pinctrl, sc210iot->pins_sleep);
546
547 return ret;
548 }
549
__sc210iot_power_off(struct sc210iot * sc210iot)550 static void __sc210iot_power_off(struct sc210iot *sc210iot)
551 {
552 int ret;
553 struct device *dev = sc210iot->dev;
554
555 if (sc210iot->is_thunderboot) {
556 if (sc210iot->is_first_streamoff) {
557 sc210iot->is_thunderboot = false;
558 sc210iot->is_first_streamoff = false;
559 } else {
560 return;
561 }
562 }
563 if (!IS_ERR_OR_NULL(sc210iot->pins_sleep)) {
564 ret = pinctrl_select_state(sc210iot->pinctrl,
565 sc210iot->pins_sleep);
566 if (ret < 0)
567 dev_dbg(dev, "could not set pins\n");
568 }
569 if (!IS_ERR(sc210iot->reset_gpio))
570 gpiod_direction_output(sc210iot->reset_gpio, 1);
571 if (!IS_ERR(sc210iot->pwdn_gpio))
572 gpiod_direction_output(sc210iot->pwdn_gpio, 0);
573 if (sc210iot->is_thunderboot_ng) {
574 sc210iot->is_thunderboot_ng = false;
575 regulator_bulk_disable(SC210IOT_NUM_SUPPLIES, sc210iot->supplies);
576 }
577 clk_disable_unprepare(sc210iot->xvclk);
578 }
579
sc210iot_check_sensor_id(struct sc210iot * sc210iot)580 static int sc210iot_check_sensor_id(struct sc210iot *sc210iot)
581 {
582 u8 id_h = 0, id_l = 0;
583 u16 id = 0;
584 int ret = 0;
585
586 if (sc210iot->is_thunderboot) {
587 dev_info(sc210iot->dev, "Enable thunderboot mode, skip sensor id check\n");
588 return 0;
589 }
590
591 ret = sc210iot_read_reg(sc210iot, SC210IOT_REG_CHIP_ID_H, &id_h);
592 ret |= sc210iot_read_reg(sc210iot, SC210IOT_REG_CHIP_ID_L, &id_l);
593 if (ret) {
594 dev_err(sc210iot->dev, "Failed to read sensor id, (%d)\n", ret);
595 return ret;
596 }
597 id = id_h << 8 | id_l;
598 if (id != SC210IOT_CHIP_ID) {
599 dev_err(sc210iot->dev, "sensor id: %04X mismatched\n", id);
600 return -ENODEV;
601 }
602 dev_info(sc210iot->dev, "Detected SC210IOT sensor\n");
603 return 0;
604 }
605
sc210iot_get_module_inf(struct sc210iot * sc210iot,struct rkmodule_inf * inf)606 static void sc210iot_get_module_inf(struct sc210iot *sc210iot,
607 struct rkmodule_inf *inf)
608 {
609 memset(inf, 0, sizeof(*inf));
610 strlcpy(inf->base.lens, sc210iot->len_name, sizeof(inf->base.lens));
611 strlcpy(inf->base.sensor, SC210IOT_NAME, sizeof(inf->base.sensor));
612 strlcpy(inf->base.module, sc210iot->module_name, sizeof(inf->base.module));
613 }
614
sc210iot_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)615 static long sc210iot_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
616 {
617 struct sc210iot *sc210iot = to_sc210iot(sd);
618 struct rkmodule_hdr_cfg *hdr_cfg;
619 long ret = 0;
620 u32 stream = 0;
621
622 switch (cmd) {
623 case RKMODULE_GET_HDR_CFG:
624 hdr_cfg = (struct rkmodule_hdr_cfg *)arg;
625 hdr_cfg->esp.mode = HDR_NORMAL_VC;
626 hdr_cfg->hdr_mode = sc210iot->cur_mode->hdr_mode;
627 break;
628 case RKMODULE_GET_MODULE_INFO:
629 sc210iot_get_module_inf(sc210iot, (struct rkmodule_inf *)arg);
630 break;
631 case RKMODULE_SET_HDR_CFG:
632 break;
633 case RKMODULE_SET_QUICK_STREAM:
634
635 stream = *((u32 *)arg);
636
637 if (stream)
638 ret = sc210iot_write_reg(sc210iot,
639 SC210IOT_REG_CTRL_MODE,
640 SC210IOT_MODE_STREAMING);
641 else
642 ret = sc210iot_write_reg(sc210iot,
643 SC210IOT_REG_CTRL_MODE,
644 SC210IOT_MODE_SW_STANDBY);
645 break;
646 default:
647 ret = -ENOIOCTLCMD;
648 break;
649 }
650 return ret;
651 }
652
__sc210iot_start_stream(struct sc210iot * sc210iot)653 static int __sc210iot_start_stream(struct sc210iot *sc210iot)
654 {
655 int ret = 0;
656
657 if (!sc210iot->is_thunderboot) {
658 ret = regmap_multi_reg_write(sc210iot->regmap,
659 sc210iot->cur_mode->reg_list,
660 sc210iot->cur_mode->reg_num);
661 if (ret)
662 return ret;
663 }
664 __v4l2_ctrl_handler_setup(&sc210iot->ctrl_handler);
665 return sc210iot_write_reg(sc210iot,
666 SC210IOT_REG_CTRL_MODE,
667 SC210IOT_MODE_STREAMING);
668 }
669
__sc210iot_stop_stream(struct sc210iot * sc210iot)670 static int __sc210iot_stop_stream(struct sc210iot *sc210iot)
671 {
672 sc210iot->has_init_exp = false;
673 if (sc210iot->is_thunderboot)
674 sc210iot->is_first_streamoff = true;
675 return sc210iot_write_reg(sc210iot, SC210IOT_REG_CTRL_MODE,
676 SC210IOT_MODE_SW_STANDBY);
677 }
678
679 #ifdef CONFIG_COMPAT
sc210iot_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)680 static long sc210iot_compat_ioctl32(struct v4l2_subdev *sd,
681 unsigned int cmd, unsigned long arg)
682 {
683 void __user *up = compat_ptr(arg);
684 struct rkmodule_inf *inf;
685 struct rkmodule_hdr_cfg *hdr;
686 long ret = 0;
687 u32 stream = 0;
688
689 switch (cmd) {
690 case RKMODULE_GET_MODULE_INFO:
691 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
692 if (!inf) {
693 ret = -ENOMEM;
694 return ret;
695 }
696 ret = sc210iot_ioctl(sd, cmd, inf);
697 if (!ret) {
698 ret = copy_to_user(up, inf, sizeof(*inf));
699 if (ret)
700 ret = -EFAULT;
701 }
702 kfree(inf);
703 break;
704 case RKMODULE_GET_HDR_CFG:
705 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
706 if (!hdr) {
707 ret = -ENOMEM;
708 return ret;
709 }
710 ret = sc210iot_ioctl(sd, cmd, hdr);
711 if (!ret) {
712 ret = copy_to_user(up, hdr, sizeof(*hdr));
713 if (ret)
714 ret = -EFAULT;
715 }
716 kfree(hdr);
717 break;
718 case RKMODULE_SET_QUICK_STREAM:
719 ret = copy_from_user(&stream, up, sizeof(u32));
720 if (!ret)
721 ret = sc210iot_ioctl(sd, cmd, &stream);
722 else
723 ret = -EFAULT;
724 break;
725 default:
726 ret = -ENOIOCTLCMD;
727 break;
728 }
729 return ret;
730 }
731 #endif
732
sc210iot_s_stream(struct v4l2_subdev * sd,int on)733 static int sc210iot_s_stream(struct v4l2_subdev *sd, int on)
734 {
735 struct sc210iot *sc210iot = to_sc210iot(sd);
736 int ret = 0;
737
738 mutex_lock(&sc210iot->lock);
739 on = !!on;
740 if (on == sc210iot->streaming)
741 goto unlock_and_return;
742 if (on) {
743 if (sc210iot->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
744 sc210iot->is_thunderboot = false;
745 __sc210iot_power_on(sc210iot);
746 }
747 ret = pm_runtime_get_sync(sc210iot->dev);
748 if (ret < 0) {
749 pm_runtime_put_noidle(sc210iot->dev);
750 goto unlock_and_return;
751 }
752 ret = __sc210iot_start_stream(sc210iot);
753 if (ret) {
754 dev_err(sc210iot->dev, "Failed to start sc210iot stream\n");
755 pm_runtime_put(sc210iot->dev);
756 goto unlock_and_return;
757 }
758 } else {
759 __sc210iot_stop_stream(sc210iot);
760 pm_runtime_put(sc210iot->dev);
761 }
762 sc210iot->streaming = on;
763 unlock_and_return:
764 mutex_unlock(&sc210iot->lock);
765 return 0;
766 }
767
sc210iot_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)768 static int sc210iot_g_frame_interval(struct v4l2_subdev *sd,
769 struct v4l2_subdev_frame_interval *fi)
770 {
771 struct sc210iot *sc210iot = to_sc210iot(sd);
772 const struct sc210iot_mode *mode = sc210iot->cur_mode;
773
774 if (sc210iot->streaming)
775 fi->interval = sc210iot->cur_fps;
776 else
777 fi->interval = mode->max_fps;
778 return 0;
779 }
780
sc210iot_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)781 static int sc210iot_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
782 struct v4l2_mbus_config *config)
783 {
784 struct sc210iot *sc210iot = to_sc210iot(sd);
785
786 u32 val = 1 << (SC210IOT_LANES - 1) | V4L2_MBUS_CSI2_CHANNEL_0 |
787 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
788 config->type = V4L2_MBUS_CSI2_DPHY;
789 config->flags = (sc210iot->cur_mode->hdr_mode == NO_HDR) ?
790 val : (val | V4L2_MBUS_CSI2_CHANNEL_1);
791 return 0;
792 }
793
sc210iot_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)794 static int sc210iot_enum_mbus_code(struct v4l2_subdev *sd,
795 struct v4l2_subdev_pad_config *cfg,
796 struct v4l2_subdev_mbus_code_enum *code)
797 {
798 if (code->index != 0)
799 return -EINVAL;
800 code->code = SC210IOT_MEDIA_BUS_FMT;
801 return 0;
802 }
803
sc210iot_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)804 static int sc210iot_enum_frame_sizes(struct v4l2_subdev *sd,
805 struct v4l2_subdev_pad_config *cfg,
806 struct v4l2_subdev_frame_size_enum *fse)
807 {
808 struct sc210iot *sc210iot = to_sc210iot(sd);
809
810 if (fse->index >= sc210iot->cfg_num)
811 return -EINVAL;
812 if (fse->code != SC210IOT_MEDIA_BUS_FMT)
813 return -EINVAL;
814 fse->min_width = supported_modes[fse->index].width;
815 fse->max_width = supported_modes[fse->index].width;
816 fse->max_height = supported_modes[fse->index].height;
817 fse->min_height = supported_modes[fse->index].height;
818 return 0;
819 }
820
sc210iot_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)821 static int sc210iot_enum_frame_interval(struct v4l2_subdev *sd,
822 struct v4l2_subdev_pad_config *cfg,
823 struct v4l2_subdev_frame_interval_enum *fie)
824 {
825 struct sc210iot *sc210iot = to_sc210iot(sd);
826
827 if (fie->index >= sc210iot->cfg_num)
828 return -EINVAL;
829 fie->code = SC210IOT_MEDIA_BUS_FMT;
830 fie->width = supported_modes[fie->index].width;
831 fie->height = supported_modes[fie->index].height;
832 fie->interval = supported_modes[fie->index].max_fps;
833 fie->reserved[0] = supported_modes[fie->index].hdr_mode;
834 return 0;
835 }
836
sc210iot_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)837 static int sc210iot_set_fmt(struct v4l2_subdev *sd,
838 struct v4l2_subdev_pad_config *cfg,
839 struct v4l2_subdev_format *fmt)
840 {
841 struct sc210iot *sc210iot = to_sc210iot(sd);
842 const struct sc210iot_mode *mode;
843 s64 h_blank, vblank_def;
844
845 mutex_lock(&sc210iot->lock);
846 mode = v4l2_find_nearest_size(supported_modes,
847 ARRAY_SIZE(supported_modes),
848 width, height,
849 fmt->format.width, fmt->format.height);
850 fmt->format.code = SC210IOT_MEDIA_BUS_FMT;
851 fmt->format.width = mode->width;
852 fmt->format.height = mode->height;
853 fmt->format.field = V4L2_FIELD_NONE;
854 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
855 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
856 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
857 #else
858 mutex_unlock(&sc210iot->lock);
859 return -ENOTTY;
860 #endif
861 } else {
862 sc210iot->cur_mode = mode;
863 __v4l2_ctrl_s_ctrl(sc210iot->link_freq, mode->link_freq_index);
864 __v4l2_ctrl_s_ctrl_int64(sc210iot->pixel_rate,
865 to_pixel_rate(mode->link_freq_index));
866 h_blank = mode->hts_def - mode->width;
867 __v4l2_ctrl_modify_range(sc210iot->hblank, h_blank,
868 h_blank, 1, h_blank);
869 vblank_def = mode->vts_def - mode->height;
870 __v4l2_ctrl_modify_range(sc210iot->vblank, vblank_def,
871 SC210IOT_VTS_MAX - mode->height,
872 1, vblank_def);
873 sc210iot->cur_fps = mode->max_fps;
874 sc210iot->cur_vts = mode->vts_def;
875 }
876 mutex_unlock(&sc210iot->lock);
877 return 0;
878 }
879
sc210iot_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)880 static int sc210iot_get_fmt(struct v4l2_subdev *sd,
881 struct v4l2_subdev_pad_config *cfg,
882 struct v4l2_subdev_format *fmt)
883 {
884 struct sc210iot *sc210iot = to_sc210iot(sd);
885 const struct sc210iot_mode *mode = sc210iot->cur_mode;
886
887 mutex_lock(&sc210iot->lock);
888 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
889 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
890 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
891 #else
892 mutex_unlock(&sc210iot->lock);
893 return -ENOTTY;
894 #endif
895 } else {
896 fmt->format.width = mode->width;
897 fmt->format.height = mode->height;
898 fmt->format.code = SC210IOT_MEDIA_BUS_FMT;
899 fmt->format.field = V4L2_FIELD_NONE;
900 fmt->reserved[0] = mode->vc[PAD0];
901 }
902 mutex_unlock(&sc210iot->lock);
903 return 0;
904 }
905
906 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
sc210iot_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)907 static int sc210iot_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
908 {
909 struct sc210iot *sc210iot = to_sc210iot(sd);
910 struct v4l2_mbus_framefmt *try_fmt =
911 v4l2_subdev_get_try_format(sd, fh->pad, 0);
912 const struct sc210iot_mode *def_mode = &supported_modes[0];
913
914 mutex_lock(&sc210iot->lock);
915 /* Initialize try_fmt */
916 try_fmt->width = def_mode->width;
917 try_fmt->height = def_mode->height;
918 try_fmt->code = SC210IOT_MEDIA_BUS_FMT;
919 try_fmt->field = V4L2_FIELD_NONE;
920 mutex_unlock(&sc210iot->lock);
921 return 0;
922 }
923 #endif
924 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
925 static const struct v4l2_subdev_internal_ops sc210iot_internal_ops = {
926 .open = sc210iot_open,
927 };
928 #endif
929
sc210iot_s_power(struct v4l2_subdev * sd,int on)930 static int sc210iot_s_power(struct v4l2_subdev *sd, int on)
931 {
932 struct sc210iot *sc210iot = to_sc210iot(sd);
933 int ret = 0;
934
935 mutex_lock(&sc210iot->lock);
936 if (sc210iot->power_on == !!on)
937 goto unlock_and_return;
938 if (on) {
939 ret = pm_runtime_get_sync(sc210iot->dev);
940 if (ret < 0) {
941 pm_runtime_put_noidle(sc210iot->dev);
942 goto unlock_and_return;
943 }
944 if (!sc210iot->is_thunderboot) {
945 ret |= sc210iot_write_reg(sc210iot,
946 SC210IOT_SOFTWARE_RESET_REG, 0x01);
947 usleep_range(100, 200);
948 }
949 sc210iot->power_on = true;
950 } else {
951 pm_runtime_put(sc210iot->dev);
952 sc210iot->power_on = false;
953 }
954 unlock_and_return:
955 mutex_unlock(&sc210iot->lock);
956 return ret;
957 }
958
959 static const struct v4l2_subdev_core_ops sc210iot_core_ops = {
960 .s_power = sc210iot_s_power,
961 .ioctl = sc210iot_ioctl,
962 #ifdef CONFIG_COMPAT
963 .compat_ioctl32 = sc210iot_compat_ioctl32,
964 #endif
965 };
966
967 static const struct v4l2_subdev_video_ops sc210iot_video_ops = {
968 .s_stream = sc210iot_s_stream,
969 .g_frame_interval = sc210iot_g_frame_interval,
970 };
971
972 static const struct v4l2_subdev_pad_ops sc210iot_pad_ops = {
973 .enum_mbus_code = sc210iot_enum_mbus_code,
974 .enum_frame_size = sc210iot_enum_frame_sizes,
975 .enum_frame_interval = sc210iot_enum_frame_interval,
976 .get_fmt = sc210iot_get_fmt,
977 .set_fmt = sc210iot_set_fmt,
978 .get_mbus_config = sc210iot_g_mbus_config,
979 };
980
981 static const struct v4l2_subdev_ops sc210iot_subdev_ops = {
982 .core = &sc210iot_core_ops,
983 .video = &sc210iot_video_ops,
984 .pad = &sc210iot_pad_ops,
985 };
986
sc210iot_runtime_resume(struct device * dev)987 static int sc210iot_runtime_resume(struct device *dev)
988 {
989 struct i2c_client *client = to_i2c_client(dev);
990 struct v4l2_subdev *sd = i2c_get_clientdata(client);
991 struct sc210iot *sc210iot = to_sc210iot(sd);
992
993 __sc210iot_power_on(sc210iot);
994 return 0;
995 }
996
sc210iot_runtime_suspend(struct device * dev)997 static int sc210iot_runtime_suspend(struct device *dev)
998 {
999 struct i2c_client *client = to_i2c_client(dev);
1000 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1001 struct sc210iot *sc210iot = to_sc210iot(sd);
1002
1003 __sc210iot_power_off(sc210iot);
1004 return 0;
1005 }
1006
1007 static const struct dev_pm_ops sc210iot_pm_ops = {
1008 SET_RUNTIME_PM_OPS(sc210iot_runtime_suspend,
1009 sc210iot_runtime_resume, NULL)
1010 };
1011
sc210iot_probe(struct i2c_client * client,const struct i2c_device_id * id)1012 static int sc210iot_probe(struct i2c_client *client,
1013 const struct i2c_device_id *id)
1014 {
1015 struct device *dev = &client->dev;
1016 struct device_node *node = dev->of_node;
1017 struct sc210iot *sc210iot;
1018 struct v4l2_subdev *sd;
1019 char facing[2];
1020 int ret;
1021
1022 dev_info(dev, "driver version: %02x.%02x.%02x",
1023 DRIVER_VERSION >> 16,
1024 (DRIVER_VERSION & 0xff00) >> 8,
1025 DRIVER_VERSION & 0x00ff);
1026 sc210iot = devm_kzalloc(dev, sizeof(*sc210iot), GFP_KERNEL);
1027 if (!sc210iot)
1028 return -ENOMEM;
1029 sc210iot->dev = dev;
1030 sc210iot->regmap = devm_regmap_init_i2c(client, &sc210iot_regmap_config);
1031 if (IS_ERR(sc210iot->regmap)) {
1032 dev_err(dev, "Failed to initialize I2C\n");
1033 return -ENODEV;
1034 }
1035 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1036 &sc210iot->module_index);
1037 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1038 &sc210iot->module_facing);
1039 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1040 &sc210iot->module_name);
1041 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1042 &sc210iot->len_name);
1043 if (ret) {
1044 dev_err(dev, "Failed to get module information\n");
1045 return -EINVAL;
1046 }
1047
1048 sc210iot->is_thunderboot = IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP);
1049
1050 sc210iot->xvclk = devm_clk_get(sc210iot->dev, "xvclk");
1051 if (IS_ERR(sc210iot->xvclk)) {
1052 dev_err(sc210iot->dev, "Failed to get xvclk\n");
1053 return -EINVAL;
1054 }
1055 sc210iot->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_ASIS);
1056 if (IS_ERR(sc210iot->reset_gpio))
1057 dev_warn(dev, "Failed to get reset-gpios\n");
1058 sc210iot->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_ASIS);
1059 if (IS_ERR(sc210iot->pwdn_gpio))
1060 dev_warn(dev, "Failed to get pwdn-gpios\n");
1061 ret = sc210iot_get_regulators(sc210iot);
1062 if (ret) {
1063 dev_err(dev, "Failed to get regulators\n");
1064 return ret;
1065 }
1066 sc210iot->pinctrl = devm_pinctrl_get(dev);
1067 if (!IS_ERR(sc210iot->pinctrl)) {
1068 sc210iot->pins_default =
1069 pinctrl_lookup_state(sc210iot->pinctrl,
1070 OF_CAMERA_PINCTRL_STATE_DEFAULT);
1071 if (IS_ERR(sc210iot->pins_default))
1072 dev_info(dev, "could not get default pinstate\n");
1073
1074 sc210iot->pins_sleep =
1075 pinctrl_lookup_state(sc210iot->pinctrl,
1076 OF_CAMERA_PINCTRL_STATE_SLEEP);
1077 if (IS_ERR(sc210iot->pins_sleep))
1078 dev_info(dev, "could not get sleep pinstate\n");
1079 } else {
1080 dev_info(dev, "no pinctrl\n");
1081 }
1082 mutex_init(&sc210iot->lock);
1083 /* set default mode */
1084 sc210iot->cur_mode = &supported_modes[0];
1085 sc210iot->cfg_num = ARRAY_SIZE(supported_modes);
1086 sd = &sc210iot->subdev;
1087 v4l2_i2c_subdev_init(sd, client, &sc210iot_subdev_ops);
1088 ret = sc210iot_initialize_controls(sc210iot);
1089 if (ret)
1090 goto err_destroy_mutex;
1091 ret = __sc210iot_power_on(sc210iot);
1092 if (ret)
1093 goto err_free_handler;
1094 ret = sc210iot_check_sensor_id(sc210iot);
1095 if (ret)
1096 goto err_power_off;
1097 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1098 sd->internal_ops = &sc210iot_internal_ops;
1099 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1100 #endif
1101 #ifdef CONFIG_MEDIA_CONTROLLER
1102 sc210iot->pad.flags = MEDIA_PAD_FL_SOURCE;
1103 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1104 ret = media_entity_pads_init(&sd->entity, 1, &sc210iot->pad);
1105 if (ret < 0)
1106 goto err_power_off;
1107 #endif
1108 memset(facing, 0, sizeof(facing));
1109 if (strcmp(sc210iot->module_facing, "back") == 0)
1110 facing[0] = 'b';
1111 else
1112 facing[0] = 'f';
1113 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1114 sc210iot->module_index, facing,
1115 SC210IOT_NAME, dev_name(sd->dev));
1116 ret = v4l2_async_register_subdev_sensor_common(sd);
1117 if (ret) {
1118 dev_err(dev, "Failed to register v4l2 async subdev\n");
1119 goto err_clean_entity;
1120 }
1121
1122 pm_runtime_set_active(dev);
1123 pm_runtime_enable(dev);
1124 pm_runtime_idle(dev);
1125 return 0;
1126 err_clean_entity:
1127 #ifdef CONFIG_MEDIA_CONTROLLER
1128 media_entity_cleanup(&sd->entity);
1129 #endif
1130 err_power_off:
1131 __sc210iot_power_off(sc210iot);
1132 err_free_handler:
1133 v4l2_ctrl_handler_free(&sc210iot->ctrl_handler);
1134 err_destroy_mutex:
1135 mutex_destroy(&sc210iot->lock);
1136 return ret;
1137 }
1138
sc210iot_remove(struct i2c_client * client)1139 static int sc210iot_remove(struct i2c_client *client)
1140 {
1141 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1142 struct sc210iot *sc210iot = to_sc210iot(sd);
1143
1144 v4l2_async_unregister_subdev(sd);
1145 #ifdef CONFIG_MEDIA_CONTROLLER
1146 media_entity_cleanup(&sd->entity);
1147 #endif
1148 v4l2_ctrl_handler_free(&sc210iot->ctrl_handler);
1149 mutex_destroy(&sc210iot->lock);
1150 pm_runtime_disable(&client->dev);
1151 if (!pm_runtime_status_suspended(&client->dev))
1152 __sc210iot_power_off(sc210iot);
1153 pm_runtime_set_suspended(&client->dev);
1154 return 0;
1155 }
1156
1157 static const struct i2c_device_id sc210iot_match_id[] = {
1158 { "sc210iot", 0 },
1159 { },
1160 };
1161
1162 static const struct of_device_id sc210iot_of_match[] = {
1163 { .compatible = "smartsens,sc210iot" },
1164 {},
1165 };
1166
1167 MODULE_DEVICE_TABLE(of, sc210iot_of_match);
1168
1169 static struct i2c_driver sc210iot_i2c_driver = {
1170 .driver = {
1171 .name = SC210IOT_NAME,
1172 .pm = &sc210iot_pm_ops,
1173 .of_match_table = of_match_ptr(sc210iot_of_match),
1174 },
1175 .probe = &sc210iot_probe,
1176 .remove = &sc210iot_remove,
1177 .id_table = sc210iot_match_id,
1178 };
1179
sensor_mod_init(void)1180 static int __init sensor_mod_init(void)
1181 {
1182 return i2c_add_driver(&sc210iot_i2c_driver);
1183 }
1184
sensor_mod_exit(void)1185 static void __exit sensor_mod_exit(void)
1186 {
1187 i2c_del_driver(&sc210iot_i2c_driver);
1188 }
1189
1190 device_initcall_sync(sensor_mod_init);
1191 module_exit(sensor_mod_exit);
1192
1193 MODULE_DESCRIPTION("Smartsens sc210iot Image Sensor driver");
1194 MODULE_LICENSE("GPL v2");
1195