xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/sgm3784.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd.
3 /*
4  * V0.0X01.0X01 fix get wrong time info issue.
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/rk-camera-module.h>
14 #include <linux/rk-led-flash.h>
15 #include <linux/slab.h>
16 #include <linux/version.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-device.h>
19 #include <linux/compat.h>
20 
21 #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x1)
22 #define SGM3784_NAME			"sgm3784"
23 
24 #define SGM3784_REG_ID			0x00
25 #define SGM3784_ID			0x18
26 
27 #define SGM3784_REG_MODE		0x01
28 #define SGM3784_REG_LED0_FLASH_CUR	0x06
29 #define SGM3784_REG_LED0_TORCH_CUR	0x08
30 #define SGM3784_REG_LED1_FLASH_CUR	0x09
31 #define SGM3784_REG_LED1_TORCH_CUR	0x0b
32 
33 #define SGM3784_REG_FAULT		0x0c
34 #define SGM3784_REG_FL_OVP		BIT(7)
35 #define SGM3784_REG_FL_SC		BIT(6)
36 #define SGM3784_REG_FL_OT		BIT(5)
37 #define SGM3784_REG_FL_TO		BIT(4)
38 
39 #define SGM3784_REG_ENABLE		0x0f
40 #define SGM3784_ON			0x03
41 #define SGM3784_OFF			0x00
42 
43 #define SGM3784_MIN_FLASH_INTENSITY	0
44 #define SGM3784_MAX_FLASH_INTENSITY	1122000
45 #define SGM3784_FLASH_INTENSITY_DEFAULT	748000
46 
47 #define SGM3784_MIN_TORCH_INTENSITY	0
48 #define SGM3784_MAX_TORCH_INTENSITY	299200
49 #define SGM3784_TORCH_INTENSITY_DEFAULT	74800
50 #define SGM3784_INTENSITY_STEP		18700
51 #define TIMEOUT_MAX			1600000
52 #define TIMEOUT_STEP			100000
53 #define TIMEOUT_MIN			100000
54 
55 static int debug;
56 module_param(debug, int, 0644);
57 MODULE_PARM_DESC(debug, "debug level (0-2)");
58 
59 enum sgm3784_led_id {
60 	LED0 = 0,
61 	LED1,
62 	LED_MAX
63 };
64 
65 struct sgm3784_led {
66 	struct v4l2_subdev sd;
67 	struct v4l2_ctrl_handler ctrls;
68 	struct v4l2_ctrl *flash_brt;
69 	struct v4l2_ctrl *torch_brt;
70 	struct __kernel_old_timeval timestamp;
71 	u32 max_flash_timeout;
72 	u32 max_flash_intensity;
73 	u32 max_torch_intensity;
74 };
75 
76 struct sgm3784_flash {
77 	struct i2c_client *client;
78 	struct sgm3784_led leds[LED_MAX];
79 
80 	struct gpio_desc *en_gpio;
81 	struct gpio_desc *torch_gpio;
82 	struct gpio_desc *strobe_gpio;
83 
84 	struct mutex lock;
85 
86 	u32 flash_timeout;
87 	enum v4l2_flash_led_mode cur_mode;
88 
89 	u32 module_index;
90 	const char *module_facing;
91 };
92 
sgm3784_i2c_write(struct sgm3784_flash * flash,u8 reg,u8 val)93 static int sgm3784_i2c_write(struct sgm3784_flash *flash, u8 reg, u8 val)
94 {
95 	struct i2c_client *client = flash->client;
96 	int ret;
97 
98 	ret = i2c_smbus_write_byte_data(client, reg, val);
99 	if (ret < 0)
100 		dev_err(&client->dev,
101 			 "%s: reg:0x%x val:0x%x failed\n",
102 			 __func__, reg, val);
103 
104 	v4l2_dbg(2, debug, &flash->leds[0].sd,
105 		 "%s: reg:0x%x val:0x%x\n",
106 		  __func__, reg, val);
107 
108 	return ret;
109 }
110 
sgm3784_i2c_read(struct sgm3784_flash * flash,u8 reg)111 static int sgm3784_i2c_read(struct sgm3784_flash *flash, u8 reg)
112 {
113 	struct i2c_client *client = flash->client;
114 	int ret;
115 
116 	ret = i2c_smbus_read_byte_data(client, reg);
117 	if (ret < 0)
118 		dev_err(&client->dev,
119 			 "%s: reg:0x%x failed\n",
120 			 __func__, reg);
121 
122 	v4l2_dbg(2, debug, &flash->leds[0].sd,
123 		 "%s: reg:0x%x val:0x%x\n",
124 		 __func__, reg, ret);
125 	return ret;
126 }
127 
sgm3784_led_on(struct sgm3784_flash * flash,bool on)128 static int sgm3784_led_on(struct sgm3784_flash *flash, bool on)
129 {
130 	int ret;
131 	u8 val;
132 
133 	v4l2_dbg(1, debug, &flash->leds[0].sd,
134 		 "%s: on:%d\n", __func__, on);
135 
136 	val = on ? SGM3784_ON : SGM3784_OFF;
137 	ret = sgm3784_i2c_write(flash, SGM3784_REG_ENABLE, val);
138 	flash->leds[0].timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
139 	flash->leds[1].timestamp = flash->leds[0].timestamp;
140 	return ret;
141 }
142 
sgm3784_get_fault(struct sgm3784_flash * flash)143 static int sgm3784_get_fault(struct sgm3784_flash *flash)
144 {
145 	int fault = 0;
146 
147 	fault = sgm3784_i2c_read(flash, SGM3784_REG_FAULT);
148 	if (fault < 0)
149 		return fault;
150 
151 	if (!fault)
152 		return 0;
153 
154 	v4l2_info(&flash->leds[0].sd,
155 		 "%s: 0x%x\n", __func__, fault);
156 	return fault;
157 }
158 
sgm3784_set_timeout(struct sgm3784_flash * flash)159 static int sgm3784_set_timeout(struct sgm3784_flash *flash)
160 {
161 	int ret;
162 	u8 val;
163 
164 	v4l2_dbg(1, debug, &flash->leds[0].sd,
165 		 "%s: %d\n",
166 		 __func__, flash->flash_timeout);
167 
168 	ret = sgm3784_i2c_read(flash, 0x02);
169 	if (ret != 0)
170 		return ret;
171 
172 	val =  (flash->flash_timeout - TIMEOUT_MIN) / TIMEOUT_STEP;
173 	return sgm3784_i2c_write(flash, 0x02, val | (ret & 0xf0));
174 }
175 
sgm3784_torch_brt(struct sgm3784_flash * flash,enum sgm3784_led_id id)176 static int sgm3784_torch_brt(struct sgm3784_flash *flash,
177 			     enum sgm3784_led_id id)
178 {
179 	struct sgm3784_led *led = &flash->leds[id];
180 	u8 val, reg;
181 
182 	v4l2_dbg(1, debug, &led->sd,
183 		 "%s: %d\n", __func__, led->torch_brt->val);
184 
185 	val = led->torch_brt->val / SGM3784_INTENSITY_STEP;
186 	reg = id ? SGM3784_REG_LED1_TORCH_CUR : SGM3784_REG_LED0_TORCH_CUR;
187 	return sgm3784_i2c_write(flash, reg, val);
188 }
189 
sgm3784_flash_brt(struct sgm3784_flash * flash,enum sgm3784_led_id id)190 static int sgm3784_flash_brt(struct sgm3784_flash *flash,
191 			     enum sgm3784_led_id id)
192 {
193 	struct sgm3784_led *led = &flash->leds[id];
194 	u8 val, reg;
195 
196 	v4l2_dbg(1, debug, &led->sd,
197 		 "%s: %d\n", __func__, led->flash_brt->val);
198 
199 	val = led->flash_brt->val / SGM3784_INTENSITY_STEP;
200 	reg = id ? SGM3784_REG_LED1_FLASH_CUR : SGM3784_REG_LED0_FLASH_CUR;
201 	return sgm3784_i2c_write(flash, reg, val);
202 }
203 
sgm3784_set_mode(struct sgm3784_flash * flash,enum sgm3784_led_id id,unsigned int mode)204 static int sgm3784_set_mode(struct sgm3784_flash *flash,
205 			    enum sgm3784_led_id id, unsigned int mode)
206 {
207 	int ret = 0;
208 	u8 val;
209 
210 	v4l2_dbg(1, debug, &flash->leds[id].sd,
211 		 "%s: %d cur:%d\n", __func__,
212 		 mode, flash->cur_mode);
213 
214 	if (flash->cur_mode == mode)
215 		return 0;
216 
217 	sgm3784_led_on(flash, false);
218 
219 	flash->cur_mode = mode;
220 
221 	val =  (flash->flash_timeout - TIMEOUT_MIN) / TIMEOUT_STEP;
222 	if (mode == V4L2_FLASH_LED_MODE_FLASH) {
223 		if (flash->torch_gpio)
224 			gpiod_direction_output(flash->torch_gpio, 0);
225 		ret = sgm3784_i2c_write(flash, 0x01, 0xfb);
226 		ret |= sgm3784_i2c_write(flash, 0x02, val | 0xc0);
227 		ret |= sgm3784_i2c_write(flash, 0x03, 0x48);
228 		ret |= sgm3784_flash_brt(flash, LED0);
229 		ret |= sgm3784_flash_brt(flash, LED1);
230 		gpiod_direction_output(flash->strobe_gpio, 1);
231 	} else if (mode == V4L2_FLASH_LED_MODE_TORCH) {
232 		gpiod_direction_output(flash->strobe_gpio, 0);
233 		if (flash->torch_gpio) {
234 			/* torch mode */
235 			ret = sgm3784_i2c_write(flash, 0x01, 0xf8);
236 			ret |= sgm3784_i2c_write(flash, 0x02, val | 0xf0);
237 		} else {
238 			/* assist mode */
239 			ret = sgm3784_i2c_write(flash, 0x01, 0xfa);
240 			ret |= sgm3784_i2c_write(flash, 0x02, val | 0xc0);
241 		}
242 		ret |= sgm3784_i2c_write(flash, 0x03, 0x48);
243 		ret |= sgm3784_torch_brt(flash, LED0);
244 		ret |= sgm3784_torch_brt(flash, LED1);
245 		ret |= sgm3784_led_on(flash, true);
246 		if (flash->torch_gpio)
247 			gpiod_direction_output(flash->torch_gpio, 1);
248 	} else {
249 		ret = sgm3784_i2c_write(flash, 0x01, 0xf8);
250 		gpiod_direction_output(flash->strobe_gpio, 0);
251 		if (flash->torch_gpio)
252 			gpiod_direction_output(flash->torch_gpio, 0);
253 	}
254 
255 	return ret;
256 }
257 
sgm3784_strobe(struct sgm3784_flash * flash,bool on)258 static int sgm3784_strobe(struct sgm3784_flash *flash, bool on)
259 {
260 	v4l2_dbg(1, debug, &flash->leds[0].sd,
261 		 "%s: on %d\n", __func__, on);
262 
263 	if (flash->cur_mode != V4L2_FLASH_LED_MODE_FLASH)
264 		return -EBUSY;
265 
266 	return sgm3784_led_on(flash, on);
267 }
268 
sgm3784_get_ctrl(struct v4l2_ctrl * ctrl,enum sgm3784_led_id id)269 static int sgm3784_get_ctrl(struct v4l2_ctrl *ctrl, enum sgm3784_led_id id)
270 {
271 	struct sgm3784_led *led =
272 		container_of(ctrl->handler, struct sgm3784_led, ctrls);
273 	struct sgm3784_flash *flash =
274 		container_of(led, struct sgm3784_flash, leds[id]);
275 	int ret = 0;
276 
277 	v4l2_dbg(1, debug, &led->sd,
278 		 "%s: id 0x%x\n", __func__, ctrl->id);
279 
280 	mutex_lock(&flash->lock);
281 	switch (ctrl->id) {
282 	case V4L2_CID_FLASH_FAULT:
283 		ret = sgm3784_get_fault(flash);
284 		ctrl->val = 0;
285 		if (ret & SGM3784_REG_FL_SC)
286 			ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
287 		if (ret & SGM3784_REG_FL_OT)
288 			ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
289 		if (ret & SGM3784_REG_FL_TO)
290 			ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
291 		if (ret & SGM3784_REG_FL_OVP)
292 			ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
293 		ret = 0;
294 		break;
295 	default:
296 		v4l2_err(&led->sd,
297 			 "ctrl 0x%x not supported\n", ctrl->id);
298 		ret = -EINVAL;
299 		break;
300 	}
301 
302 	mutex_unlock(&flash->lock);
303 	return ret;
304 }
305 
sgm3784_set_ctrl(struct v4l2_ctrl * ctrl,enum sgm3784_led_id id)306 static int sgm3784_set_ctrl(struct v4l2_ctrl *ctrl, enum sgm3784_led_id id)
307 {
308 	struct sgm3784_led *led =
309 		container_of(ctrl->handler, struct sgm3784_led, ctrls);
310 	struct sgm3784_flash *flash =
311 		container_of(led, struct sgm3784_flash, leds[id]);
312 	int ret = 0;
313 
314 	v4l2_dbg(1, debug, &led->sd,
315 		 "%s: id 0x%x val 0x%x\n",
316 		 __func__, ctrl->id, ctrl->val);
317 
318 	mutex_lock(&flash->lock);
319 
320 	ret = sgm3784_get_fault(flash);
321 	if (ret != 0)
322 		goto err;
323 	if ((ret & (SGM3784_REG_FL_OVP |
324 		     SGM3784_REG_FL_OT |
325 		     SGM3784_REG_FL_SC)) &&
326 	    (ctrl->id == V4L2_CID_FLASH_STROBE ||
327 	     ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
328 	     ctrl->id == V4L2_CID_FLASH_LED_MODE)) {
329 		ret = -EBUSY;
330 		goto err;
331 	}
332 
333 	switch (ctrl->id) {
334 	case V4L2_CID_FLASH_LED_MODE:
335 		ret = sgm3784_set_mode(flash, id, ctrl->val);
336 		break;
337 	case V4L2_CID_FLASH_STROBE:
338 		ret = sgm3784_strobe(flash, true);
339 		break;
340 	case V4L2_CID_FLASH_STROBE_STOP:
341 		ret = sgm3784_strobe(flash, false);
342 		break;
343 	case V4L2_CID_FLASH_TIMEOUT:
344 		ret = sgm3784_set_timeout(flash);
345 		break;
346 	case V4L2_CID_FLASH_INTENSITY:
347 		ret = sgm3784_flash_brt(flash, id);
348 		break;
349 	case V4L2_CID_FLASH_TORCH_INTENSITY:
350 		ret = sgm3784_torch_brt(flash, id);
351 		break;
352 	default:
353 		v4l2_err(&led->sd,
354 			"ctrl 0x%x not supported\n", ctrl->id);
355 		ret = -EINVAL;
356 		break;
357 	}
358 
359 err:
360 	mutex_unlock(&flash->lock);
361 	return ret;
362 }
363 
sgm3784_led0_get_ctrl(struct v4l2_ctrl * ctrl)364 static int sgm3784_led0_get_ctrl(struct v4l2_ctrl *ctrl)
365 {
366 	return sgm3784_get_ctrl(ctrl, LED0);
367 }
368 
sgm3784_led0_set_ctrl(struct v4l2_ctrl * ctrl)369 static int sgm3784_led0_set_ctrl(struct v4l2_ctrl *ctrl)
370 {
371 	return sgm3784_set_ctrl(ctrl, LED0);
372 }
373 
sgm3784_led1_get_ctrl(struct v4l2_ctrl * ctrl)374 static int sgm3784_led1_get_ctrl(struct v4l2_ctrl *ctrl)
375 {
376 	return sgm3784_get_ctrl(ctrl, LED1);
377 }
378 
sgm3784_led1_set_ctrl(struct v4l2_ctrl * ctrl)379 static int sgm3784_led1_set_ctrl(struct v4l2_ctrl *ctrl)
380 {
381 	return sgm3784_set_ctrl(ctrl, LED1);
382 }
383 
384 static const struct v4l2_ctrl_ops sgm3784_ctrl_ops[LED_MAX] = {
385 	[LED0] = {
386 		.g_volatile_ctrl = sgm3784_led0_get_ctrl,
387 		.s_ctrl = sgm3784_led0_set_ctrl,
388 	},
389 	[LED1] = {
390 		.g_volatile_ctrl = sgm3784_led1_get_ctrl,
391 		.s_ctrl = sgm3784_led1_set_ctrl,
392 	}
393 };
394 
sgm3784_init_controls(struct sgm3784_flash * flash,enum sgm3784_led_id id)395 static int sgm3784_init_controls(struct sgm3784_flash *flash,
396 				 enum sgm3784_led_id id)
397 {
398 	struct v4l2_ctrl *fault;
399 	struct v4l2_ctrl_handler *hdl = &flash->leds[id].ctrls;
400 	const struct v4l2_ctrl_ops *ops = &sgm3784_ctrl_ops[id];
401 	struct sgm3784_led *led = &flash->leds[id];
402 
403 	v4l2_ctrl_handler_init(hdl, 8);
404 
405 	v4l2_ctrl_new_std_menu(hdl, ops,
406 			       V4L2_CID_FLASH_LED_MODE,
407 			       V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
408 	flash->cur_mode = V4L2_FLASH_LED_MODE_NONE;
409 
410 	v4l2_ctrl_new_std_menu(hdl, ops,
411 			       V4L2_CID_FLASH_STROBE_SOURCE,
412 			       V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
413 
414 	v4l2_ctrl_new_std(hdl, ops,
415 			  V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
416 
417 	v4l2_ctrl_new_std(hdl, ops,
418 			  V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
419 
420 	v4l2_ctrl_new_std(hdl, ops,
421 			  V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
422 			  led->max_flash_timeout,
423 			  TIMEOUT_STEP,
424 			  led->max_flash_timeout);
425 	flash->flash_timeout = led->max_flash_timeout;
426 
427 	flash->leds[id].flash_brt =
428 		v4l2_ctrl_new_std(hdl, ops,
429 				  V4L2_CID_FLASH_INTENSITY,
430 				  SGM3784_MIN_FLASH_INTENSITY,
431 				  led->max_flash_intensity,
432 				  SGM3784_INTENSITY_STEP,
433 				  SGM3784_FLASH_INTENSITY_DEFAULT);
434 
435 	flash->leds[id].torch_brt =
436 		v4l2_ctrl_new_std(hdl, ops,
437 				  V4L2_CID_FLASH_TORCH_INTENSITY,
438 				  SGM3784_MIN_TORCH_INTENSITY,
439 				  led->max_torch_intensity,
440 				  SGM3784_INTENSITY_STEP,
441 				  SGM3784_TORCH_INTENSITY_DEFAULT);
442 
443 	fault = v4l2_ctrl_new_std(hdl, ops,
444 				  V4L2_CID_FLASH_FAULT, 0,
445 				  V4L2_FLASH_FAULT_OVER_VOLTAGE
446 				  | V4L2_FLASH_FAULT_TIMEOUT
447 				  | V4L2_FLASH_FAULT_OVER_TEMPERATURE
448 				  | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
449 
450 	if (hdl->error)
451 		return hdl->error;
452 
453 	fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
454 
455 	flash->leds[id].sd.ctrl_handler = hdl;
456 	return 0;
457 }
458 
sgm3784_get_time_info(struct v4l2_subdev * sd,struct __kernel_old_timeval * ti)459 static void sgm3784_get_time_info(struct v4l2_subdev *sd,
460 				  struct __kernel_old_timeval *ti)
461 {
462 	struct sgm3784_led *led =
463 		container_of(sd, struct sgm3784_led, sd);
464 
465 	memset(ti, 0, sizeof(*ti));
466 	ti->tv_sec = led->timestamp.tv_sec;
467 	ti->tv_usec = led->timestamp.tv_usec;
468 	v4l2_dbg(1, debug, sd,
469 		 "%s: tv_sec:%ld, tv_usec:%ld\n", __func__, ti->tv_sec, ti->tv_usec);
470 }
471 
sgm3784_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)472 static long sgm3784_ioctl(struct v4l2_subdev *sd,
473 			 unsigned int cmd, void *arg)
474 {
475 	long ret = 0;
476 
477 	switch (cmd) {
478 	case RK_VIDIOC_FLASH_TIMEINFO:
479 		sgm3784_get_time_info(sd, (struct __kernel_old_timeval *)arg);
480 		break;
481 
482 	default:
483 		ret = -ENOIOCTLCMD;
484 		break;
485 	}
486 	return ret;
487 }
488 
489 #ifdef CONFIG_COMPAT
490 #define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
491 	_IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
492 
sgm3784_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)493 static long sgm3784_compat_ioctl32(struct v4l2_subdev *sd,
494 				  unsigned int cmd,
495 				  unsigned long arg)
496 {
497 	void __user *up = compat_ptr(arg);
498 	struct old_timeval32 *compat_t;
499 	long ret;
500 	struct __kernel_old_timeval t;
501 
502 	v4l2_dbg(1, debug, sd,
503 		 "%s: cmd 0x%x\n", __func__, cmd);
504 
505 	switch (cmd) {
506 	case RK_VIDIOC_COMPAT_FLASH_TIMEINFO:
507 		compat_t = kzalloc(sizeof(*compat_t), GFP_KERNEL);
508 		if (!compat_t) {
509 			ret = -ENOMEM;
510 			return ret;
511 		}
512 		ret = sgm3784_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, &t);
513 		if (!ret) {
514 			compat_t->tv_sec = t.tv_sec;
515 			compat_t->tv_usec = t.tv_usec;
516 			ret = copy_to_user(up, compat_t, sizeof(*compat_t));
517 			if (ret)
518 				ret = -EFAULT;
519 		}
520 		kfree(compat_t);
521 		break;
522 
523 	default:
524 		ret = -ENOIOCTLCMD;
525 		break;
526 	}
527 
528 	return ret;
529 
530 }
531 #endif
532 
sgm3784_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)533 static int sgm3784_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
534 {
535 	int rval;
536 
537 	rval = pm_runtime_get_sync(sd->dev);
538 	if (rval < 0) {
539 		pm_runtime_put_noidle(sd->dev);
540 		return rval;
541 	}
542 
543 	return 0;
544 }
545 
sgm3784_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)546 static int sgm3784_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
547 {
548 	pm_runtime_put(sd->dev);
549 
550 	return 0;
551 }
552 
553 static const struct v4l2_subdev_core_ops sgm3784_core_ops = {
554 	.ioctl = sgm3784_ioctl,
555 #ifdef CONFIG_COMPAT
556 	.compat_ioctl32 = sgm3784_compat_ioctl32
557 #endif
558 };
559 
560 static const struct v4l2_subdev_ops sgm3784_ops = {
561 	.core = &sgm3784_core_ops,
562 };
563 
564 static const struct v4l2_subdev_internal_ops sgm3784_internal_ops = {
565 	.open = sgm3784_open,
566 	.close = sgm3784_close,
567 };
568 
__sgm3784_set_power(struct sgm3784_flash * flash,bool on)569 static int __sgm3784_set_power(struct sgm3784_flash *flash, bool on)
570 {
571 	gpiod_direction_output(flash->en_gpio, on);
572 
573 	return 0;
574 }
575 
sgm3784_check_id(struct sgm3784_flash * flash)576 static int sgm3784_check_id(struct sgm3784_flash *flash)
577 {
578 	int ret = 0;
579 
580 	__sgm3784_set_power(flash, true);
581 	ret = sgm3784_i2c_read(flash, SGM3784_REG_ID);
582 	__sgm3784_set_power(flash, false);
583 	if (ret != SGM3784_ID) {
584 		dev_err(&flash->client->dev,
585 			"Read chip id error\n");
586 		return -ENODEV;
587 	}
588 
589 	dev_info(&flash->client->dev,
590 		 "Detected sgm3784 flash id:0x%x\n", ret);
591 	return 0;
592 }
593 
sgm3784_of_init(struct i2c_client * client,struct sgm3784_flash * flash)594 static int sgm3784_of_init(struct i2c_client *client,
595 			   struct sgm3784_flash *flash)
596 {
597 	struct device_node *node = client->dev.of_node;
598 	struct device_node *child;
599 	struct sgm3784_led *led;
600 	int ret = 0;
601 
602 	if (!node) {
603 		dev_err(&client->dev,
604 			"get device node failed\n");
605 		goto err;
606 	}
607 
608 	ret = of_property_read_u32(node,
609 				   RKMODULE_CAMERA_MODULE_INDEX,
610 				   &flash->module_index);
611 	ret |= of_property_read_string(node,
612 				       RKMODULE_CAMERA_MODULE_FACING,
613 				       &flash->module_facing);
614 	if (ret) {
615 		dev_err(&client->dev,
616 			"could not get module information!\n");
617 		goto err;
618 	}
619 
620 	flash->en_gpio = devm_gpiod_get(&client->dev,
621 					"enable", GPIOD_OUT_LOW);
622 	if (IS_ERR(flash->en_gpio)) {
623 		dev_err(&client->dev, "get enable-gpio failed\n");
624 		goto err;
625 	}
626 
627 	flash->torch_gpio = devm_gpiod_get(&client->dev,
628 					   "torch", GPIOD_OUT_LOW);
629 	if (IS_ERR(flash->torch_gpio)) {
630 		flash->torch_gpio = NULL;
631 		dev_warn(&client->dev,
632 			 "get torch-gpio failed, using assist light mode\n");
633 	}
634 
635 	flash->strobe_gpio = devm_gpiod_get(&client->dev,
636 					    "strobe", GPIOD_OUT_LOW);
637 	if (IS_ERR(flash->en_gpio)) {
638 		dev_err(&client->dev, "get strobe-gpio failed\n");
639 		goto err;
640 	}
641 
642 	for_each_child_of_node(node, child) {
643 		u32 id = 0;
644 
645 		of_property_read_u32(child, "reg", &id);
646 		if (id >= LED_MAX) {
647 			dev_err(&client->dev, "only support 2 leds\n");
648 			goto err;
649 		}
650 		led = &flash->leds[id];
651 		led->sd.fwnode = of_fwnode_handle(child);
652 		if (of_property_read_u32(child, "flash-max-timeout-us",
653 					 &led->max_flash_timeout)) {
654 			dev_err(&client->dev,
655 				"get led%d flash-max-timeout-us fail\n", id);
656 			goto err;
657 		}
658 		if (led->max_flash_timeout > TIMEOUT_MAX)
659 			led->max_flash_timeout = TIMEOUT_MAX;
660 
661 		if (of_property_read_u32(child, "flash-max-microamp",
662 					 &led->max_flash_intensity)) {
663 			dev_err(&client->dev,
664 				"get led%d flash-max-microamp fail\n", id);
665 			goto err;
666 		}
667 		if (led->max_flash_intensity > SGM3784_MAX_FLASH_INTENSITY)
668 			led->max_flash_intensity = SGM3784_MAX_FLASH_INTENSITY;
669 
670 		if (of_property_read_u32(child, "led-max-microamp",
671 					  &led->max_torch_intensity)) {
672 			dev_err(&client->dev,
673 				"get led%d led-max-microamp fail\n", id);
674 			goto err;
675 		}
676 		if (led->max_torch_intensity > SGM3784_MAX_TORCH_INTENSITY)
677 			led->max_torch_intensity = SGM3784_MAX_TORCH_INTENSITY;
678 
679 		dev_info(&client->dev,
680 			 "led%d max torch:%dUA flash:%dUA timeout:%dUS\n",
681 			 id, led->max_torch_intensity,
682 			 led->max_flash_intensity,
683 			 led->max_flash_timeout);
684 	}
685 
686 	return 0;
687 err:
688 	return -EINVAL;
689 }
690 
sgm3784_probe(struct i2c_client * client,const struct i2c_device_id * devid)691 static int sgm3784_probe(struct i2c_client *client,
692 			 const struct i2c_device_id *devid)
693 {
694 	struct sgm3784_flash *flash;
695 	struct v4l2_subdev *sd;
696 	char facing[2];
697 	int i, ret;
698 
699 	dev_info(&client->dev, "driver version: %02x.%02x.%02x",
700 		 DRIVER_VERSION >> 16,
701 		 (DRIVER_VERSION & 0xff00) >> 8,
702 		 DRIVER_VERSION & 0x00ff);
703 
704 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
705 	if (!flash)
706 		return -ENOMEM;
707 
708 	flash->client = client;
709 
710 	ret = sgm3784_of_init(client, flash);
711 	if (ret)
712 		return ret;
713 
714 	ret = sgm3784_check_id(flash);
715 	if (ret)
716 		return ret;
717 
718 	for (i = 0; i < LED_MAX; i++) {
719 		sd = &flash->leds[i].sd;
720 		v4l2_i2c_subdev_init(sd, client, &sgm3784_ops);
721 		sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
722 		sd->internal_ops = &sgm3784_internal_ops;
723 
724 		memset(facing, 0, sizeof(facing));
725 		if (strcmp(flash->module_facing, "back") == 0)
726 			facing[0] = 'b';
727 		else
728 			facing[0] = 'f';
729 		/* NOTE: to distinguish between two led
730 		 * name: led0 meet the main led
731 		 * name: led1 meet the secondary led
732 		 */
733 		snprintf(sd->name, sizeof(sd->name),
734 			 "m%02d_%s_%s_led%d %s",
735 			 flash->module_index, facing,
736 			 SGM3784_NAME, i, dev_name(sd->dev));
737 
738 		ret = sgm3784_init_controls(flash, i);
739 		if (ret)
740 			goto err;
741 
742 		ret = media_entity_pads_init(&sd->entity, 0, NULL);
743 		if (ret < 0)
744 			goto free_ctl;
745 
746 		sd->entity.function = MEDIA_ENT_F_FLASH;
747 		ret = v4l2_async_register_subdev(sd);
748 		if (ret)
749 			goto free_media;
750 	}
751 
752 	i2c_set_clientdata(client, flash);
753 
754 	mutex_init(&flash->lock);
755 
756 	pm_runtime_set_active(&client->dev);
757 	pm_runtime_enable(&client->dev);
758 	pm_runtime_idle(&client->dev);
759 
760 	dev_info(&client->dev, "probing successful\n");
761 
762 	return 0;
763 free_media:
764 	media_entity_cleanup(&flash->leds[i].sd.entity);
765 free_ctl:
766 	v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
767 err:
768 	for (--i; i >= 0; --i) {
769 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
770 		media_entity_cleanup(&flash->leds[i].sd.entity);
771 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
772 	}
773 	return ret;
774 }
775 
sgm3784_remove(struct i2c_client * client)776 static int sgm3784_remove(struct i2c_client *client)
777 {
778 	struct sgm3784_flash *flash = i2c_get_clientdata(client);
779 	unsigned int i;
780 
781 	pm_runtime_disable(&client->dev);
782 	for (i = 0; i < LED_MAX; i++) {
783 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
784 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
785 		media_entity_cleanup(&flash->leds[i].sd.entity);
786 	}
787 	mutex_destroy(&flash->lock);
788 	return 0;
789 }
790 
sgm3784_runtime_suspend(struct device * dev)791 static int __maybe_unused sgm3784_runtime_suspend(struct device *dev)
792 {
793 	struct i2c_client *client = to_i2c_client(dev);
794 	struct sgm3784_flash *flash = i2c_get_clientdata(client);
795 
796 	return __sgm3784_set_power(flash, false);
797 }
798 
sgm3784_runtime_resume(struct device * dev)799 static int __maybe_unused sgm3784_runtime_resume(struct device *dev)
800 {
801 	struct i2c_client *client = to_i2c_client(dev);
802 	struct sgm3784_flash *flash = i2c_get_clientdata(client);
803 
804 	return __sgm3784_set_power(flash, true);
805 }
806 
807 static const struct i2c_device_id sgm3784_id_table[] = {
808 	{ SGM3784_NAME, 0 },
809 	{ { 0 } }
810 };
811 MODULE_DEVICE_TABLE(i2c, sgm3784_id_table);
812 
813 static const struct of_device_id sgm3784_of_table[] = {
814 	{ .compatible = "sgmicro,gsm3784" },
815 	{ { 0 } }
816 };
817 
818 static const struct dev_pm_ops sgm3784_pm_ops = {
819 	SET_RUNTIME_PM_OPS(sgm3784_runtime_suspend,
820 			   sgm3784_runtime_resume, NULL)
821 };
822 
823 static struct i2c_driver sgm3784_i2c_driver = {
824 	.driver	= {
825 		.name = SGM3784_NAME,
826 		.pm = &sgm3784_pm_ops,
827 		.of_match_table = sgm3784_of_table,
828 	},
829 	.probe = sgm3784_probe,
830 	.remove	= sgm3784_remove,
831 	.id_table = sgm3784_id_table,
832 };
833 
834 module_i2c_driver(sgm3784_i2c_driver);
835 
836 MODULE_DESCRIPTION("SGM3784 LED flash driver");
837 MODULE_LICENSE("GPL v2");
838