xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/sc2239.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sc2239 driver
4  *
5  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6  *
7  * V0.0X01.0X00 first version.
8  * V0.0X01.0X01 add quick stream support.
9  */
10 //#define DEBUG
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/sysfs.h>
20 #include <linux/rk-camera-module.h>
21 #include <linux/slab.h>
22 #include <linux/version.h>
23 #include <media/media-entity.h>
24 #include <media/v4l2-async.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-subdev.h>
27 
28 #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x01)
29 
30 #ifndef V4L2_CID_DIGITAL_GAIN
31 #define V4L2_CID_DIGITAL_GAIN		V4L2_CID_GAIN
32 #endif
33 
34 #define SC2239_LANES			1
35 #define SC2239_BITS_PER_SAMPLE		10
36 #define SC2239_LINK_FREQ		371250000	// 742.5Mbps
37 
38 #define SC2239_PIXEL_RATE		(SC2239_LINK_FREQ * 2 * \
39 					SC2239_LANES / SC2239_BITS_PER_SAMPLE)
40 
41 #define SC2239_XVCLK_FREQ		24000000
42 
43 #define CHIP_ID				0xcb10
44 #define SC2239_REG_CHIP_ID		0x3107
45 
46 #define SC2239_REG_CTRL_MODE		0x0100
47 #define SC2239_MODE_SW_STANDBY		0x0
48 #define SC2239_MODE_STREAMING		BIT(0)
49 
50 #define SC2239_REG_EXPOSURE		0x3e00
51 #define	SC2239_EXPOSURE_MIN		1
52 #define	SC2239_EXPOSURE_STEP		1
53 #define SC2239_VTS_MAX			0xffff
54 
55 #define SC2239_REG_COARSE_AGAIN		0x3e08
56 #define SC2239_REG_FINE_AGAIN		0x3e09
57 #define	ANALOG_GAIN_MIN			0x20
58 #define	ANALOG_GAIN_MAX			0x1F8
59 #define	ANALOG_GAIN_STEP		1
60 #define	ANALOG_GAIN_DEFAULT		0x40
61 
62 #define SC4238_GROUP_UPDATE_ADDRESS	0x3812
63 #define SC4238_GROUP_UPDATE_START_DATA	0x00
64 #define SC4238_GROUP_UPDATE_END_DATA	0x30
65 
66 #define SC2239_REG_TEST_PATTERN		0x4501
67 #define SC2239_TEST_PATTERN_BIT_MASK	BIT(3)
68 
69 #define SC2239_REG_VTS			0x320e
70 
71 #define REG_NULL			0xFFFF
72 #define DELAY_MS			0xEEEE	/* Array delay token */
73 
74 #define SC2239_REG_VALUE_08BIT		1
75 #define SC2239_REG_VALUE_16BIT		2
76 #define SC2239_REG_VALUE_24BIT		3
77 
78 #define PIX_FORMAT MEDIA_BUS_FMT_SBGGR10_1X10
79 
80 #define SC2239_NAME			"sc2239"
81 
82 static const char * const sc2239_supply_names[] = {
83 	"avdd",		/* Analog power */
84 	"dovdd",	/* Digital I/O power */
85 	"dvdd",		/* Digital core power */
86 };
87 
88 #define SC2239_NUM_SUPPLIES ARRAY_SIZE(sc2239_supply_names)
89 
90 struct regval {
91 	u16 addr;
92 	u8 val;
93 };
94 
95 struct sc2239_mode {
96 	u32 width;
97 	u32 height;
98 	struct v4l2_fract max_fps;
99 	u32 hts_def;
100 	u32 vts_def;
101 	u32 exp_def;
102 	const struct regval *reg_list;
103 };
104 
105 struct sc2239 {
106 	struct i2c_client	*client;
107 	struct clk		*xvclk;
108 	struct gpio_desc	*reset_gpio;
109 	struct gpio_desc	*pwdn_gpio;
110 	struct regulator_bulk_data supplies[SC2239_NUM_SUPPLIES];
111 	struct v4l2_subdev	subdev;
112 	struct media_pad	pad;
113 	struct v4l2_ctrl_handler ctrl_handler;
114 	struct v4l2_ctrl	*exposure;
115 	struct v4l2_ctrl	*anal_gain;
116 	struct v4l2_ctrl	*digi_gain;
117 	struct v4l2_ctrl	*hblank;
118 	struct v4l2_ctrl	*vblank;
119 	struct v4l2_ctrl	*test_pattern;
120 	struct mutex		mutex;
121 	struct v4l2_fract	cur_fps;
122 	u32			cur_vts;
123 	bool			streaming;
124 	bool			power_on;
125 	const struct sc2239_mode *cur_mode;
126 	u32			module_index;
127 	const char		*module_facing;
128 	const char		*module_name;
129 	const char		*len_name;
130 	u32			old_gain;
131 };
132 
133 #define to_sc2239(sd) container_of(sd, struct sc2239, subdev)
134 
135 /*
136  * Xclk 24Mhz
137  * max_framerate 30fps
138  * mipi_datarate per lane 742.5Mbps, 1 lane
139  */
140 static const struct regval sc2239_global_regs[] = {
141 	{0x0103, 0x01},
142 	{0x0100, 0x00},
143 	{0x36e9, 0x80},
144 	{0x36f9, 0x80},
145 	{0x301f, 0x34},
146 	{0x3038, 0x44},
147 	{0x3253, 0x12},
148 	{0x3301, 0x05},
149 	{0x3304, 0xa8},
150 	{0x3306, 0x68},
151 	{0x3308, 0x10},
152 	{0x3309, 0x48},
153 	{0x330a, 0x01},
154 	{0x330b, 0x40},
155 	{0x331e, 0xa1},
156 	{0x331f, 0x41},
157 	{0x3333, 0x10},
158 	{0x3364, 0x17},
159 	{0x3390, 0x08},
160 	{0x3391, 0x18},
161 	{0x3392, 0x38},
162 	{0x3393, 0x08},
163 	{0x3394, 0x0d},
164 	{0x3395, 0x70},
165 	{0x33af, 0x20},
166 	{0x360f, 0x01},
167 	{0x3630, 0x00},
168 	{0x3634, 0x64},
169 	{0x3637, 0x10},
170 	{0x363c, 0x05},
171 	{0x3670, 0x0c},
172 	{0x3671, 0xc2},
173 	{0x3672, 0x02},
174 	{0x3673, 0x02},
175 	{0x3677, 0x84},
176 	{0x3678, 0x84},
177 	{0x3679, 0x8e},
178 	{0x367a, 0x18},
179 	{0x367b, 0x38},
180 	{0x367e, 0x08},
181 	{0x367f, 0x38},
182 	{0x3690, 0x64},
183 	{0x3691, 0x64},
184 	{0x3692, 0x64},
185 	{0x369c, 0x08},
186 	{0x369d, 0x18},
187 	{0x36ea, 0x75},
188 	{0x36ed, 0x24},
189 	{0x36fa, 0x75},
190 	{0x36fb, 0x00},
191 	{0x36fc, 0x10},
192 	{0x36fd, 0x34},
193 	{0x3904, 0x08},
194 	{0x3908, 0x82},
195 	{0x3933, 0x82},
196 	{0x3934, 0x1b},
197 	{0x3940, 0x77},
198 	{0x3941, 0x18},
199 	{0x3942, 0x02},
200 	{0x3943, 0x1c},
201 	{0x3944, 0x0b},
202 	{0x3945, 0x80},
203 	{0x3e01, 0x8c},
204 	{0x3e02, 0x20},
205 	{0x4509, 0x20},
206 	{0x4800, 0x64},
207 	{0x4819, 0x09},
208 	{0x481b, 0x05},
209 	{0x481d, 0x14},
210 	{0x4821, 0x0a},
211 	{0x4823, 0x05},
212 	{0x5000, 0x06},
213 	{0x5780, 0x7f},
214 	{0x5781, 0x04},
215 	{0x5782, 0x03},
216 	{0x5783, 0x02},
217 	{0x5784, 0x01},
218 	{0x5785, 0x18},
219 	{0x5786, 0x10},
220 	{0x5787, 0x08},
221 	{0x5788, 0x02},
222 	{0x5789, 0x20},
223 	{0x578a, 0x7f},
224 	{0x36e9, 0x29},
225 	{0x36f9, 0x29},
226 	{DELAY_MS, 0x0a},
227 	{REG_NULL, 0x00},
228 };
229 
230 /*
231  * Xclk 24Mhz
232  * max_framerate 30fps
233  * mipi_datarate per lane 742.5Mbps, 1lane
234  */
235 static const struct regval sc2239_1920x1080_regs_1lane[] = {
236 	{REG_NULL, 0x00},
237 };
238 
239 static const struct sc2239_mode supported_modes[] = {
240 	{
241 		.width = 1920,
242 		.height = 1080,
243 		.max_fps = {
244 			.numerator = 10000,
245 			.denominator = 300000,
246 		},
247 		.exp_def = 0x0400,
248 		.hts_def = 0x44C * 2,
249 		.vts_def = 0x0465,
250 		.reg_list = sc2239_1920x1080_regs_1lane,
251 	},
252 };
253 
254 static const char * const sc2239_test_pattern_menu[] = {
255 	"Disabled",
256 	"Vertical Color Bar Type 1",
257 	"Vertical Color Bar Type 2",
258 	"Vertical Color Bar Type 3",
259 	"Vertical Color Bar Type 4"
260 };
261 
262 static const s64 link_freq_menu_items[] = {
263 	SC2239_LINK_FREQ
264 };
265 
266 /* Write registers up to 4 at a time */
sc2239_write_reg(struct i2c_client * client,u16 reg,u32 len,u32 val)267 static int sc2239_write_reg(struct i2c_client *client,
268 	u16 reg, u32 len, u32 val)
269 {
270 	u32 buf_i, val_i;
271 	u8 buf[6];
272 	u8 *val_p;
273 	__be32 val_be;
274 	u32 ret;
275 
276 	if (len > 4)
277 		return -EINVAL;
278 
279 	buf[0] = reg >> 8;
280 	buf[1] = reg & 0xff;
281 
282 	val_be = cpu_to_be32(val);
283 	val_p = (u8 *)&val_be;
284 	buf_i = 2;
285 	val_i = 4 - len;
286 
287 	while (val_i < 4)
288 		buf[buf_i++] = val_p[val_i++];
289 
290 	ret = i2c_master_send(client, buf, len + 2);
291 	if (ret != len + 2)
292 		return -EIO;
293 
294 	return 0;
295 }
296 
sc2239_write_array(struct i2c_client * client,const struct regval * regs)297 static int sc2239_write_array(struct i2c_client *client,
298 	const struct regval *regs)
299 {
300 	u32 i;
301 	int delay_ms = 0, ret = 0;
302 
303 	for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++) {
304 		if (regs[i].addr == DELAY_MS) {
305 			delay_ms = regs[i].val;
306 			dev_info(&client->dev, "delay(%d) ms !\n", delay_ms);
307 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
308 			i++;
309 			continue;
310 		}
311 		ret = sc2239_write_reg(client, regs[i].addr,
312 				       SC2239_REG_VALUE_08BIT, regs[i].val);
313 	}
314 
315 	return ret;
316 }
317 
318 /* Read registers up to 4 at a time */
sc2239_read_reg(struct i2c_client * client,u16 reg,unsigned int len,u32 * val)319 static int sc2239_read_reg(struct i2c_client *client,
320 	u16 reg, unsigned int len, u32 *val)
321 {
322 	struct i2c_msg msgs[2];
323 	u8 *data_be_p;
324 	__be32 data_be = 0;
325 	__be16 reg_addr_be = cpu_to_be16(reg);
326 	int ret;
327 
328 	if (len > 4 || !len)
329 		return -EINVAL;
330 
331 	data_be_p = (u8 *)&data_be;
332 	/* Write register address */
333 	msgs[0].addr = client->addr;
334 	msgs[0].flags = 0;
335 	msgs[0].len = 2;
336 	msgs[0].buf = (u8 *)&reg_addr_be;
337 
338 	/* Read data from register */
339 	msgs[1].addr = client->addr;
340 	msgs[1].flags = I2C_M_RD;
341 	msgs[1].len = len;
342 	msgs[1].buf = &data_be_p[4 - len];
343 
344 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
345 	if (ret != ARRAY_SIZE(msgs))
346 		return -EIO;
347 
348 	*val = be32_to_cpu(data_be);
349 
350 	return 0;
351 }
352 
sc2239_get_reso_dist(const struct sc2239_mode * mode,struct v4l2_mbus_framefmt * framefmt)353 static int sc2239_get_reso_dist(const struct sc2239_mode *mode,
354 	struct v4l2_mbus_framefmt *framefmt)
355 {
356 	return abs(mode->width - framefmt->width) +
357 	       abs(mode->height - framefmt->height);
358 }
359 
360 static const struct sc2239_mode *
sc2239_find_best_fit(struct v4l2_subdev_format * fmt)361 sc2239_find_best_fit(struct v4l2_subdev_format *fmt)
362 {
363 	struct v4l2_mbus_framefmt *framefmt = &fmt->format;
364 	int dist;
365 	int cur_best_fit = 0;
366 	int cur_best_fit_dist = -1;
367 	unsigned int i;
368 
369 	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
370 		dist = sc2239_get_reso_dist(&supported_modes[i], framefmt);
371 		if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
372 			cur_best_fit_dist = dist;
373 			cur_best_fit = i;
374 		}
375 	}
376 
377 	return &supported_modes[cur_best_fit];
378 }
379 
sc2239_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)380 static int sc2239_set_fmt(struct v4l2_subdev *sd,
381 			  struct v4l2_subdev_pad_config *cfg,
382 			  struct v4l2_subdev_format *fmt)
383 {
384 	struct sc2239 *sc2239 = to_sc2239(sd);
385 	const struct sc2239_mode *mode;
386 	s64 h_blank, vblank_def;
387 
388 	mutex_lock(&sc2239->mutex);
389 
390 	mode = sc2239_find_best_fit(fmt);
391 	fmt->format.code = PIX_FORMAT;
392 	fmt->format.width = mode->width;
393 	fmt->format.height = mode->height;
394 	fmt->format.field = V4L2_FIELD_NONE;
395 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
396 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
397 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
398 #else
399 		mutex_unlock(&sc2239->mutex);
400 		return -ENOTTY;
401 #endif
402 	} else {
403 		sc2239->cur_mode = mode;
404 		h_blank = mode->hts_def - mode->width;
405 		__v4l2_ctrl_modify_range(sc2239->hblank, h_blank,
406 					 h_blank, 1, h_blank);
407 		vblank_def = mode->vts_def - mode->height;
408 		__v4l2_ctrl_modify_range(sc2239->vblank, vblank_def,
409 					 SC2239_VTS_MAX - mode->height,
410 					 1, vblank_def);
411 		sc2239->cur_fps = mode->max_fps;
412 		sc2239->cur_vts = mode->vts_def;
413 	}
414 
415 	mutex_unlock(&sc2239->mutex);
416 
417 	return 0;
418 }
419 
sc2239_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)420 static int sc2239_get_fmt(struct v4l2_subdev *sd,
421 			  struct v4l2_subdev_pad_config *cfg,
422 			  struct v4l2_subdev_format *fmt)
423 {
424 	struct sc2239 *sc2239 = to_sc2239(sd);
425 	const struct sc2239_mode *mode = sc2239->cur_mode;
426 
427 	mutex_lock(&sc2239->mutex);
428 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
429 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
430 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
431 #else
432 		mutex_unlock(&sc2239->mutex);
433 		return -ENOTTY;
434 #endif
435 	} else {
436 		fmt->format.width = mode->width;
437 		fmt->format.height = mode->height;
438 		fmt->format.code = PIX_FORMAT;
439 		fmt->format.field = V4L2_FIELD_NONE;
440 	}
441 	mutex_unlock(&sc2239->mutex);
442 
443 	return 0;
444 }
445 
sc2239_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)446 static int sc2239_enum_mbus_code(struct v4l2_subdev *sd,
447 				 struct v4l2_subdev_pad_config *cfg,
448 				 struct v4l2_subdev_mbus_code_enum *code)
449 {
450 	if (code->index != 0)
451 		return -EINVAL;
452 	code->code = PIX_FORMAT;
453 
454 	return 0;
455 }
456 
sc2239_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)457 static int sc2239_enum_frame_sizes(struct v4l2_subdev *sd,
458 				   struct v4l2_subdev_pad_config *cfg,
459 				   struct v4l2_subdev_frame_size_enum *fse)
460 {
461 	if (fse->index >= ARRAY_SIZE(supported_modes))
462 		return -EINVAL;
463 
464 	if (fse->code != PIX_FORMAT)
465 		return -EINVAL;
466 
467 	fse->min_width  = supported_modes[fse->index].width;
468 	fse->max_width  = supported_modes[fse->index].width;
469 	fse->max_height = supported_modes[fse->index].height;
470 	fse->min_height = supported_modes[fse->index].height;
471 
472 	return 0;
473 }
474 
sc2239_enable_test_pattern(struct sc2239 * sc2239,u32 pattern)475 static int sc2239_enable_test_pattern(struct sc2239 *sc2239, u32 pattern)
476 {
477 	u32 val = 0;
478 	int ret = 0;
479 
480 	ret = sc2239_read_reg(sc2239->client, SC2239_REG_TEST_PATTERN,
481 			       SC2239_REG_VALUE_08BIT, &val);
482 	if (pattern)
483 		val |= SC2239_TEST_PATTERN_BIT_MASK;
484 	else
485 		val &= ~SC2239_TEST_PATTERN_BIT_MASK;
486 
487 	ret |= sc2239_write_reg(sc2239->client, SC2239_REG_TEST_PATTERN,
488 				SC2239_REG_VALUE_08BIT, val);
489 	return ret;
490 }
491 
sc2239_get_module_inf(struct sc2239 * sc2239,struct rkmodule_inf * inf)492 static void sc2239_get_module_inf(struct sc2239 *sc2239,
493 				  struct rkmodule_inf *inf)
494 {
495 	memset(inf, 0, sizeof(*inf));
496 	strlcpy(inf->base.sensor, SC2239_NAME, sizeof(inf->base.sensor));
497 	strlcpy(inf->base.module, sc2239->module_name,
498 		sizeof(inf->base.module));
499 	strlcpy(inf->base.lens, sc2239->len_name, sizeof(inf->base.lens));
500 }
501 
sc2239_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)502 static long sc2239_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
503 {
504 	struct sc2239 *sc2239 = to_sc2239(sd);
505 	long ret = 0;
506 	u32  stream;
507 
508 	switch (cmd) {
509 	case RKMODULE_GET_MODULE_INFO:
510 		sc2239_get_module_inf(sc2239, (struct rkmodule_inf *)arg);
511 		break;
512 	case RKMODULE_SET_QUICK_STREAM:
513 		stream = *((u32 *)arg);
514 
515 		if (stream)
516 			ret = sc2239_write_reg(sc2239->client, SC2239_REG_CTRL_MODE,
517 				SC2239_REG_VALUE_08BIT, SC2239_MODE_STREAMING);
518 		else
519 			ret = sc2239_write_reg(sc2239->client, SC2239_REG_CTRL_MODE,
520 				SC2239_REG_VALUE_08BIT, SC2239_MODE_SW_STANDBY);
521 		break;
522 	default:
523 		ret = -ENOIOCTLCMD;
524 		break;
525 	}
526 
527 	return ret;
528 }
529 
530 #ifdef CONFIG_COMPAT
sc2239_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)531 static long sc2239_compat_ioctl32(struct v4l2_subdev *sd,
532 				  unsigned int cmd, unsigned long arg)
533 {
534 	void __user *up = compat_ptr(arg);
535 	struct rkmodule_inf *inf;
536 	struct rkmodule_awb_cfg *cfg;
537 	long ret;
538 	u32  stream;
539 
540 	switch (cmd) {
541 	case RKMODULE_GET_MODULE_INFO:
542 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
543 		if (!inf) {
544 			ret = -ENOMEM;
545 			return ret;
546 		}
547 
548 		ret = sc2239_ioctl(sd, cmd, inf);
549 		if (!ret) {
550 			ret = copy_to_user(up, inf, sizeof(*inf));
551 			if (ret)
552 				ret = -EFAULT;
553 		}
554 		kfree(inf);
555 		break;
556 	case RKMODULE_AWB_CFG:
557 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
558 		if (!cfg) {
559 			ret = -ENOMEM;
560 			return ret;
561 		}
562 
563 		ret = copy_from_user(cfg, up, sizeof(*cfg));
564 		if (!ret)
565 			ret = sc2239_ioctl(sd, cmd, cfg);
566 		else
567 			ret = -EFAULT;
568 		kfree(cfg);
569 		break;
570 	case RKMODULE_SET_QUICK_STREAM:
571 		ret = copy_from_user(&stream, up, sizeof(u32));
572 		if (!ret)
573 			ret = sc2239_ioctl(sd, cmd, &stream);
574 		else
575 			ret = -EFAULT;
576 		break;
577 	default:
578 		ret = -ENOIOCTLCMD;
579 		break;
580 	}
581 
582 	return ret;
583 }
584 #endif
585 
sc2239_set_ctrl_gain(struct sc2239 * sc2239,u32 a_gain)586 static int sc2239_set_ctrl_gain(struct sc2239 *sc2239, u32 a_gain)
587 {
588 	int ret = 0;
589 	u32 coarse_again, fine_again, fine_again_reg, coarse_again_reg;
590 	u32 switch_value;
591 
592 	if ( a_gain != sc2239->old_gain) {
593 		if (a_gain < 0x40) { /*1x ~ 2x*/
594 			fine_again = a_gain - 32;
595 			coarse_again = 0x03;
596 			fine_again_reg = ((0x01 << 5) & 0x20) |
597 				(fine_again & 0x1f);
598 			coarse_again_reg = coarse_again  & 0x1F;
599 			switch_value = 0x64;
600 		} else if (a_gain < 0x80) { /*2x ~ 4x*/
601 			fine_again = (a_gain >> 1) - 32;
602 			coarse_again = 0x7;
603 			fine_again_reg = ((0x01 << 5) & 0x20) |
604 				(fine_again & 0x1f);
605 			coarse_again_reg = coarse_again  & 0x1F;
606 			switch_value = 0x64;
607 		} else if (a_gain < 0x100) { /*4x ~ 8x*/
608 			fine_again = (a_gain >> 2) - 32;
609 			coarse_again = 0xf;
610 			fine_again_reg = ((0x01 << 5) & 0x20) |
611 				(fine_again & 0x1f);
612 			coarse_again_reg = coarse_again  & 0x1F;
613 			switch_value = 0x44;
614 		} else { /*8x ~ 16x*/
615 			fine_again = (a_gain >> 3) - 32;
616 			coarse_again = 0x1f;
617 			fine_again_reg = ((0x01 << 5) & 0x20) |
618 				(fine_again & 0x1f);
619 			coarse_again_reg = coarse_again  & 0x1F;
620 			switch_value = 0x24;
621 		}
622 
623 		ret = sc2239_write_reg(sc2239->client, 0x3634,
624 			SC2239_REG_VALUE_08BIT, switch_value);
625 		ret |= sc2239_write_reg(sc2239->client,
626 			SC2239_REG_COARSE_AGAIN,
627 			SC2239_REG_VALUE_08BIT,
628 			coarse_again_reg);
629 		ret |= sc2239_write_reg(sc2239->client,
630 			SC2239_REG_FINE_AGAIN,
631 			SC2239_REG_VALUE_08BIT,
632 			fine_again_reg);
633 		sc2239->old_gain = a_gain;
634 	}
635 	return ret;
636 }
637 
__sc2239_start_stream(struct sc2239 * sc2239)638 static int __sc2239_start_stream(struct sc2239 *sc2239)
639 {
640 	int ret;
641 
642 	ret = sc2239_write_array(sc2239->client, sc2239->cur_mode->reg_list);
643 	if (ret)
644 		return ret;
645 
646 	/* In case these controls are set before streaming */
647 	mutex_unlock(&sc2239->mutex);
648 	ret = v4l2_ctrl_handler_setup(&sc2239->ctrl_handler);
649 	mutex_lock(&sc2239->mutex);
650 	if (ret)
651 		return ret;
652 
653 	return sc2239_write_reg(sc2239->client, SC2239_REG_CTRL_MODE,
654 			SC2239_REG_VALUE_08BIT, SC2239_MODE_STREAMING);
655 }
656 
__sc2239_stop_stream(struct sc2239 * sc2239)657 static int __sc2239_stop_stream(struct sc2239 *sc2239)
658 {
659 	return sc2239_write_reg(sc2239->client, SC2239_REG_CTRL_MODE,
660 			SC2239_REG_VALUE_08BIT, SC2239_MODE_SW_STANDBY);
661 }
662 
sc2239_s_stream(struct v4l2_subdev * sd,int on)663 static int sc2239_s_stream(struct v4l2_subdev *sd, int on)
664 {
665 	struct sc2239 *sc2239 = to_sc2239(sd);
666 	struct i2c_client *client = sc2239->client;
667 	int ret = 0;
668 
669 	dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
670 				sc2239->cur_mode->width,
671 				sc2239->cur_mode->height,
672 		DIV_ROUND_CLOSEST(sc2239->cur_mode->max_fps.denominator,
673 				  sc2239->cur_mode->max_fps.numerator));
674 
675 	mutex_lock(&sc2239->mutex);
676 	on = !!on;
677 	if (on == sc2239->streaming)
678 		goto unlock_and_return;
679 
680 	if (on) {
681 		ret = pm_runtime_get_sync(&client->dev);
682 		if (ret < 0) {
683 			pm_runtime_put_noidle(&client->dev);
684 			goto unlock_and_return;
685 		}
686 
687 		ret = __sc2239_start_stream(sc2239);
688 		if (ret) {
689 			v4l2_err(sd, "start stream failed while write regs\n");
690 			pm_runtime_put(&client->dev);
691 			goto unlock_and_return;
692 		}
693 	} else {
694 		__sc2239_stop_stream(sc2239);
695 		pm_runtime_put(&client->dev);
696 	}
697 
698 	sc2239->streaming = on;
699 
700 unlock_and_return:
701 	mutex_unlock(&sc2239->mutex);
702 
703 	return ret;
704 }
705 
sc2239_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)706 static int sc2239_g_frame_interval(struct v4l2_subdev *sd,
707 				   struct v4l2_subdev_frame_interval *fi)
708 {
709 	struct sc2239 *sc2239 = to_sc2239(sd);
710 	const struct sc2239_mode *mode = sc2239->cur_mode;
711 
712 	if (sc2239->streaming)
713 		fi->interval = sc2239->cur_fps;
714 	else
715 		fi->interval = mode->max_fps;
716 
717 	return 0;
718 }
719 
sc2239_s_power(struct v4l2_subdev * sd,int on)720 static int sc2239_s_power(struct v4l2_subdev *sd, int on)
721 {
722 	struct sc2239 *sc2239 = to_sc2239(sd);
723 	struct i2c_client *client = sc2239->client;
724 	int ret = 0;
725 
726 	mutex_lock(&sc2239->mutex);
727 
728 	/* If the power state is not modified - no work to do. */
729 	if (sc2239->power_on == !!on)
730 		goto unlock_and_return;
731 
732 	if (on) {
733 		ret = pm_runtime_get_sync(&client->dev);
734 		if (ret < 0) {
735 			pm_runtime_put_noidle(&client->dev);
736 			goto unlock_and_return;
737 		}
738 		ret = sc2239_write_array(sc2239->client, sc2239_global_regs);
739 		if (ret) {
740 			v4l2_err(sd, "could not set init registers\n");
741 			pm_runtime_put_noidle(&client->dev);
742 			goto unlock_and_return;
743 		}
744 		sc2239->power_on = true;
745 	} else {
746 		pm_runtime_put(&client->dev);
747 		sc2239->power_on = false;
748 	}
749 
750 unlock_and_return:
751 	mutex_unlock(&sc2239->mutex);
752 
753 	return ret;
754 }
755 
756 /* Calculate the delay in us by clock rate and clock cycles */
sc2239_cal_delay(u32 cycles)757 static inline u32 sc2239_cal_delay(u32 cycles)
758 {
759 	return DIV_ROUND_UP(cycles, SC2239_XVCLK_FREQ / 1000 / 1000);
760 }
761 
__sc2239_power_on(struct sc2239 * sc2239)762 static int __sc2239_power_on(struct sc2239 *sc2239)
763 {
764 	int ret;
765 	u32 delay_us;
766 	struct device *dev = &sc2239->client->dev;
767 
768 	ret = clk_set_rate(sc2239->xvclk, SC2239_XVCLK_FREQ);
769 	if (ret < 0)
770 		dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
771 	if (clk_get_rate(sc2239->xvclk) != SC2239_XVCLK_FREQ)
772 		dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
773 	ret = clk_prepare_enable(sc2239->xvclk);
774 	if (ret < 0) {
775 		dev_err(dev, "Failed to enable xvclk\n");
776 		return ret;
777 	}
778 	if (!IS_ERR(sc2239->reset_gpio))
779 		gpiod_set_value_cansleep(sc2239->reset_gpio, 0);
780 
781 	ret = regulator_bulk_enable(SC2239_NUM_SUPPLIES, sc2239->supplies);
782 	if (ret < 0) {
783 		dev_err(dev, "Failed to enable regulators\n");
784 		goto disable_clk;
785 	}
786 
787 	if (!IS_ERR(sc2239->reset_gpio))
788 		gpiod_set_value_cansleep(sc2239->reset_gpio, 1);
789 
790 	if (!IS_ERR(sc2239->pwdn_gpio))
791 		gpiod_set_value_cansleep(sc2239->pwdn_gpio, 1);
792 
793 	/* 8192 cycles prior to first SCCB transaction */
794 	delay_us = sc2239_cal_delay(8192);
795 	usleep_range(delay_us, delay_us * 2);
796 
797 	return 0;
798 
799 disable_clk:
800 	clk_disable_unprepare(sc2239->xvclk);
801 
802 	return ret;
803 }
804 
__sc2239_power_off(struct sc2239 * sc2239)805 static void __sc2239_power_off(struct sc2239 *sc2239)
806 {
807 	if (!IS_ERR(sc2239->pwdn_gpio))
808 		gpiod_set_value_cansleep(sc2239->pwdn_gpio, 0);
809 	clk_disable_unprepare(sc2239->xvclk);
810 	if (!IS_ERR(sc2239->reset_gpio))
811 		gpiod_set_value_cansleep(sc2239->reset_gpio, 0);
812 
813 	regulator_bulk_disable(SC2239_NUM_SUPPLIES, sc2239->supplies);
814 }
815 
sc2239_runtime_resume(struct device * dev)816 static int sc2239_runtime_resume(struct device *dev)
817 {
818 	struct i2c_client *client = to_i2c_client(dev);
819 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
820 	struct sc2239 *sc2239 = to_sc2239(sd);
821 
822 	return __sc2239_power_on(sc2239);
823 }
824 
sc2239_runtime_suspend(struct device * dev)825 static int sc2239_runtime_suspend(struct device *dev)
826 {
827 	struct i2c_client *client = to_i2c_client(dev);
828 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
829 	struct sc2239 *sc2239 = to_sc2239(sd);
830 
831 	__sc2239_power_off(sc2239);
832 
833 	return 0;
834 }
835 
836 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
sc2239_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)837 static int sc2239_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
838 {
839 	struct sc2239 *sc2239 = to_sc2239(sd);
840 	struct v4l2_mbus_framefmt *try_fmt =
841 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
842 	const struct sc2239_mode *def_mode = &supported_modes[0];
843 
844 	mutex_lock(&sc2239->mutex);
845 	/* Initialize try_fmt */
846 	try_fmt->width = def_mode->width;
847 	try_fmt->height = def_mode->height;
848 	try_fmt->code = PIX_FORMAT;
849 	try_fmt->field = V4L2_FIELD_NONE;
850 
851 	mutex_unlock(&sc2239->mutex);
852 	/* No crop or compose */
853 
854 	return 0;
855 }
856 #endif
857 
sc2239_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)858 static int sc2239_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
859 				 struct v4l2_mbus_config *config)
860 {
861 	u32 val = 1 << (SC2239_LANES - 1) |
862 		V4L2_MBUS_CSI2_CHANNEL_0 |
863 		V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
864 
865 	config->type = V4L2_MBUS_CSI2_DPHY;
866 	config->flags = val;
867 
868 	return 0;
869 }
870 
sc2239_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)871 static int sc2239_enum_frame_interval(struct v4l2_subdev *sd,
872 				      struct v4l2_subdev_pad_config *cfg,
873 				      struct v4l2_subdev_frame_interval_enum *fie)
874 {
875 	if (fie->index >= ARRAY_SIZE(supported_modes))
876 		return -EINVAL;
877 
878 	fie->code = PIX_FORMAT;
879 
880 	fie->width = supported_modes[fie->index].width;
881 	fie->height = supported_modes[fie->index].height;
882 	fie->interval = supported_modes[fie->index].max_fps;
883 	return 0;
884 }
885 
886 static const struct dev_pm_ops sc2239_pm_ops = {
887 	SET_RUNTIME_PM_OPS(sc2239_runtime_suspend,
888 			   sc2239_runtime_resume, NULL)
889 };
890 
891 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
892 static const struct v4l2_subdev_internal_ops sc2239_internal_ops = {
893 	.open = sc2239_open,
894 };
895 #endif
896 
897 static const struct v4l2_subdev_core_ops sc2239_core_ops = {
898 	.s_power = sc2239_s_power,
899 	.ioctl = sc2239_ioctl,
900 #ifdef CONFIG_COMPAT
901 	.compat_ioctl32 = sc2239_compat_ioctl32,
902 #endif
903 };
904 
905 static const struct v4l2_subdev_video_ops sc2239_video_ops = {
906 	.s_stream = sc2239_s_stream,
907 	.g_frame_interval = sc2239_g_frame_interval,
908 };
909 
910 static const struct v4l2_subdev_pad_ops sc2239_pad_ops = {
911 	.enum_mbus_code = sc2239_enum_mbus_code,
912 	.enum_frame_size = sc2239_enum_frame_sizes,
913 	.enum_frame_interval = sc2239_enum_frame_interval,
914 	.get_fmt = sc2239_get_fmt,
915 	.set_fmt = sc2239_set_fmt,
916 	.get_mbus_config = sc2239_g_mbus_config,
917 };
918 
919 static const struct v4l2_subdev_ops sc2239_subdev_ops = {
920 	.core	= &sc2239_core_ops,
921 	.video	= &sc2239_video_ops,
922 	.pad	= &sc2239_pad_ops,
923 };
924 
sc2239_modify_fps_info(struct sc2239 * sc2239)925 static void sc2239_modify_fps_info(struct sc2239 *sc2239)
926 {
927 	const struct sc2239_mode *mode = sc2239->cur_mode;
928 
929 	sc2239->cur_fps.denominator = mode->max_fps.denominator * mode->vts_def /
930 				      sc2239->cur_vts;
931 }
932 
sc2239_set_ctrl(struct v4l2_ctrl * ctrl)933 static int sc2239_set_ctrl(struct v4l2_ctrl *ctrl)
934 {
935 	struct sc2239 *sc2239 = container_of(ctrl->handler,
936 					     struct sc2239, ctrl_handler);
937 	struct i2c_client *client = sc2239->client;
938 	s64 max;
939 	int ret = 0;
940 
941 	dev_dbg(&client->dev, "ctrl->id(0x%x) val 0x%x\n",
942 		ctrl->id, ctrl->val);
943 	/* Propagate change of current control to all related controls */
944 	switch (ctrl->id) {
945 	case V4L2_CID_VBLANK:
946 		/* Update max exposure while meeting expected vblanking */
947 		max = sc2239->cur_mode->height + ctrl->val - 4;
948 		__v4l2_ctrl_modify_range(sc2239->exposure,
949 					 sc2239->exposure->minimum, max,
950 					 sc2239->exposure->step,
951 					 sc2239->exposure->default_value);
952 		break;
953 	}
954 
955 	if (!pm_runtime_get_if_in_use(&client->dev))
956 		return 0;
957 
958 	switch (ctrl->id) {
959 	case V4L2_CID_EXPOSURE:
960 		/* 4 least significant bits of expsoure are fractional part */
961 		ret = sc2239_write_reg(sc2239->client, SC2239_REG_EXPOSURE,
962 				       SC2239_REG_VALUE_24BIT, ctrl->val << 5);
963 		dev_dbg(&client->dev, "set exposure 0x%x\n",
964 			ctrl->val);
965 		break;
966 	case V4L2_CID_ANALOGUE_GAIN:
967 		ret = sc2239_set_ctrl_gain(sc2239, ctrl->val);
968 		dev_dbg(&client->dev, "set analog gain 0x%x\n",
969 			ctrl->val);
970 		break;
971 	case V4L2_CID_VBLANK:
972 		ret = sc2239_write_reg(sc2239->client, SC2239_REG_VTS,
973 				       SC2239_REG_VALUE_16BIT,
974 				       ctrl->val + sc2239->cur_mode->height);
975 		if (!ret)
976 			sc2239->cur_vts = ctrl->val + sc2239->cur_mode->height;
977 		sc2239_modify_fps_info(sc2239);
978 		break;
979 	case V4L2_CID_TEST_PATTERN:
980 		ret = sc2239_enable_test_pattern(sc2239, ctrl->val);
981 		break;
982 	default:
983 		dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
984 			 __func__, ctrl->id, ctrl->val);
985 		break;
986 	}
987 
988 	pm_runtime_put(&client->dev);
989 
990 	return ret;
991 }
992 
993 static const struct v4l2_ctrl_ops sc2239_ctrl_ops = {
994 	.s_ctrl = sc2239_set_ctrl,
995 };
996 
sc2239_initialize_controls(struct sc2239 * sc2239)997 static int sc2239_initialize_controls(struct sc2239 *sc2239)
998 {
999 	const struct sc2239_mode *mode;
1000 	struct v4l2_ctrl_handler *handler;
1001 	struct v4l2_ctrl *ctrl;
1002 	s64 exposure_max, vblank_def;
1003 	u32 h_blank;
1004 	int ret;
1005 
1006 	handler = &sc2239->ctrl_handler;
1007 	mode = sc2239->cur_mode;
1008 	ret = v4l2_ctrl_handler_init(handler, 8);
1009 	if (ret)
1010 		return ret;
1011 	handler->lock = &sc2239->mutex;
1012 
1013 	ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
1014 				      0, 0, link_freq_menu_items);
1015 	if (ctrl)
1016 		ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1017 
1018 	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
1019 			  0, SC2239_PIXEL_RATE, 1, SC2239_PIXEL_RATE);
1020 
1021 	h_blank = mode->hts_def - mode->width;
1022 	sc2239->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
1023 				h_blank, h_blank, 1, h_blank);
1024 	if (sc2239->hblank)
1025 		sc2239->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1026 
1027 	vblank_def = mode->vts_def - mode->height;
1028 	sc2239->vblank = v4l2_ctrl_new_std(handler, &sc2239_ctrl_ops,
1029 				V4L2_CID_VBLANK, vblank_def,
1030 				SC2239_VTS_MAX - mode->height,
1031 				1, vblank_def);
1032 
1033 	exposure_max = mode->vts_def - 4;
1034 	sc2239->exposure = v4l2_ctrl_new_std(handler, &sc2239_ctrl_ops,
1035 				V4L2_CID_EXPOSURE, SC2239_EXPOSURE_MIN,
1036 				exposure_max, SC2239_EXPOSURE_STEP,
1037 				mode->exp_def);
1038 
1039 	sc2239->anal_gain = v4l2_ctrl_new_std(handler, &sc2239_ctrl_ops,
1040 				V4L2_CID_ANALOGUE_GAIN, ANALOG_GAIN_MIN,
1041 				ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
1042 				ANALOG_GAIN_DEFAULT);
1043 
1044 	sc2239->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
1045 				&sc2239_ctrl_ops, V4L2_CID_TEST_PATTERN,
1046 				ARRAY_SIZE(sc2239_test_pattern_menu) - 1,
1047 				0, 0, sc2239_test_pattern_menu);
1048 
1049 	if (handler->error) {
1050 		ret = handler->error;
1051 		dev_err(&sc2239->client->dev,
1052 			"Failed to init controls(%d)\n", ret);
1053 		goto err_free_handler;
1054 	}
1055 
1056 	sc2239->subdev.ctrl_handler = handler;
1057 	sc2239->old_gain = ANALOG_GAIN_DEFAULT;
1058 	sc2239->cur_fps = mode->max_fps;
1059 	sc2239->cur_vts = mode->vts_def;
1060 
1061 	return 0;
1062 
1063 err_free_handler:
1064 	v4l2_ctrl_handler_free(handler);
1065 
1066 	return ret;
1067 }
1068 
sc2239_check_sensor_id(struct sc2239 * sc2239,struct i2c_client * client)1069 static int sc2239_check_sensor_id(struct sc2239 *sc2239,
1070 				  struct i2c_client *client)
1071 {
1072 	struct device *dev = &sc2239->client->dev;
1073 	u32 id = 0;
1074 	int ret;
1075 
1076 	ret = sc2239_read_reg(client, SC2239_REG_CHIP_ID,
1077 			      SC2239_REG_VALUE_16BIT, &id);
1078 	if (id != CHIP_ID) {
1079 		dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
1080 		return -ENODEV;
1081 	}
1082 
1083 	dev_info(dev, "Detected SC2239 CHIP ID = 0x%04x sensor\n", CHIP_ID);
1084 
1085 	return 0;
1086 }
1087 
sc2239_configure_regulators(struct sc2239 * sc2239)1088 static int sc2239_configure_regulators(struct sc2239 *sc2239)
1089 {
1090 	unsigned int i;
1091 
1092 	for (i = 0; i < SC2239_NUM_SUPPLIES; i++)
1093 		sc2239->supplies[i].supply = sc2239_supply_names[i];
1094 
1095 	return devm_regulator_bulk_get(&sc2239->client->dev,
1096 				       SC2239_NUM_SUPPLIES,
1097 				       sc2239->supplies);
1098 }
1099 
sc2239_probe(struct i2c_client * client,const struct i2c_device_id * id)1100 static int sc2239_probe(struct i2c_client *client,
1101 			const struct i2c_device_id *id)
1102 {
1103 	struct device *dev = &client->dev;
1104 	struct device_node *node = dev->of_node;
1105 	struct sc2239 *sc2239;
1106 	struct v4l2_subdev *sd;
1107 	char facing[2];
1108 	int ret;
1109 
1110 	dev_info(dev, "driver version: %02x.%02x.%02x",
1111 		DRIVER_VERSION >> 16,
1112 		(DRIVER_VERSION & 0xff00) >> 8,
1113 		DRIVER_VERSION & 0x00ff);
1114 
1115 	sc2239 = devm_kzalloc(dev, sizeof(*sc2239), GFP_KERNEL);
1116 	if (!sc2239)
1117 		return -ENOMEM;
1118 
1119 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1120 				   &sc2239->module_index);
1121 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1122 				       &sc2239->module_facing);
1123 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1124 				       &sc2239->module_name);
1125 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1126 				       &sc2239->len_name);
1127 	if (ret) {
1128 		dev_err(dev, "could not get module information!\n");
1129 		return -EINVAL;
1130 	}
1131 
1132 	sc2239->client = client;
1133 	sc2239->cur_mode = &supported_modes[0];
1134 
1135 	sc2239->xvclk = devm_clk_get(dev, "xvclk");
1136 	if (IS_ERR(sc2239->xvclk)) {
1137 		dev_err(dev, "Failed to get xvclk\n");
1138 		return -EINVAL;
1139 	}
1140 
1141 	sc2239->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1142 	if (IS_ERR(sc2239->reset_gpio))
1143 		dev_warn(dev, "Failed to get reset-gpios\n");
1144 
1145 	sc2239->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1146 	if (IS_ERR(sc2239->pwdn_gpio))
1147 		dev_warn(dev, "Failed to get pwdn-gpios\n");
1148 	ret = sc2239_configure_regulators(sc2239);
1149 	if (ret) {
1150 		dev_err(dev, "Failed to get power regulators\n");
1151 		return ret;
1152 	}
1153 
1154 	mutex_init(&sc2239->mutex);
1155 
1156 	sd = &sc2239->subdev;
1157 	v4l2_i2c_subdev_init(sd, client, &sc2239_subdev_ops);
1158 	ret = sc2239_initialize_controls(sc2239);
1159 	if (ret)
1160 		goto err_destroy_mutex;
1161 
1162 	ret = __sc2239_power_on(sc2239);
1163 	if (ret)
1164 		goto err_free_handler;
1165 
1166 	ret = sc2239_check_sensor_id(sc2239, client);
1167 	if (ret)
1168 		goto err_power_off;
1169 
1170 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1171 	sd->internal_ops = &sc2239_internal_ops;
1172 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1173 		     V4L2_SUBDEV_FL_HAS_EVENTS;
1174 #endif
1175 #if defined(CONFIG_MEDIA_CONTROLLER)
1176 	sc2239->pad.flags = MEDIA_PAD_FL_SOURCE;
1177 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1178 	ret = media_entity_pads_init(&sd->entity, 1, &sc2239->pad);
1179 	if (ret < 0)
1180 		goto err_power_off;
1181 #endif
1182 
1183 	memset(facing, 0, sizeof(facing));
1184 	if (strcmp(sc2239->module_facing, "back") == 0)
1185 		facing[0] = 'b';
1186 	else
1187 		facing[0] = 'f';
1188 
1189 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1190 		 sc2239->module_index, facing,
1191 		 SC2239_NAME, dev_name(sd->dev));
1192 	ret = v4l2_async_register_subdev_sensor_common(sd);
1193 	if (ret) {
1194 		dev_err(dev, "v4l2 async register subdev failed\n");
1195 		goto err_clean_entity;
1196 	}
1197 
1198 	pm_runtime_set_active(dev);
1199 	pm_runtime_enable(dev);
1200 	pm_runtime_idle(dev);
1201 
1202 	return 0;
1203 
1204 err_clean_entity:
1205 #if defined(CONFIG_MEDIA_CONTROLLER)
1206 	media_entity_cleanup(&sd->entity);
1207 #endif
1208 err_power_off:
1209 	__sc2239_power_off(sc2239);
1210 err_free_handler:
1211 	v4l2_ctrl_handler_free(&sc2239->ctrl_handler);
1212 err_destroy_mutex:
1213 	mutex_destroy(&sc2239->mutex);
1214 
1215 	return ret;
1216 }
1217 
sc2239_remove(struct i2c_client * client)1218 static int sc2239_remove(struct i2c_client *client)
1219 {
1220 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1221 	struct sc2239 *sc2239 = to_sc2239(sd);
1222 
1223 	v4l2_async_unregister_subdev(sd);
1224 #if defined(CONFIG_MEDIA_CONTROLLER)
1225 	media_entity_cleanup(&sd->entity);
1226 #endif
1227 	v4l2_ctrl_handler_free(&sc2239->ctrl_handler);
1228 	mutex_destroy(&sc2239->mutex);
1229 
1230 	pm_runtime_disable(&client->dev);
1231 	if (!pm_runtime_status_suspended(&client->dev))
1232 		__sc2239_power_off(sc2239);
1233 	pm_runtime_set_suspended(&client->dev);
1234 
1235 	return 0;
1236 }
1237 
1238 #if IS_ENABLED(CONFIG_OF)
1239 static const struct of_device_id sc2239_of_match[] = {
1240 	{ .compatible = "smartsens,sc2239" },
1241 	{},
1242 };
1243 MODULE_DEVICE_TABLE(of, sc2239_of_match);
1244 #endif
1245 
1246 static const struct i2c_device_id sc2239_match_id[] = {
1247 	{ "smartsens,sc2239", 0 },
1248 	{ },
1249 };
1250 
1251 static struct i2c_driver sc2239_i2c_driver = {
1252 	.driver = {
1253 		.name = SC2239_NAME,
1254 		.pm = &sc2239_pm_ops,
1255 		.of_match_table = of_match_ptr(sc2239_of_match),
1256 	},
1257 	.probe		= &sc2239_probe,
1258 	.remove		= &sc2239_remove,
1259 	.id_table	= sc2239_match_id,
1260 };
1261 
sensor_mod_init(void)1262 static int __init sensor_mod_init(void)
1263 {
1264 	return i2c_add_driver(&sc2239_i2c_driver);
1265 }
1266 
sensor_mod_exit(void)1267 static void __exit sensor_mod_exit(void)
1268 {
1269 	i2c_del_driver(&sc2239_i2c_driver);
1270 }
1271 
1272 device_initcall_sync(sensor_mod_init);
1273 module_exit(sensor_mod_exit);
1274 
1275 MODULE_DESCRIPTION("Smartsens sc2239 sensor driver");
1276 MODULE_AUTHOR("zack.zeng");
1277 MODULE_LICENSE("GPL v2");
1278