xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/aw36518.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * General device driver for awinic aw36518, FLASH LED Driver
4  *
5  * Copyright (C) 2022 Fuzhou Rockchip Electronics Co., Ltd.
6  *
7  * V0.0X01.0X00 init version.
8  * V0.0X01.0X01 fix power off torch not off issue.
9  * V0.0X01.0X02 fix get wrong time info issue.
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/rk-camera-module.h>
19 #include <linux/rk-led-flash.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <linux/compat.h>
25 
26 #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x02)
27 #define AW36518_NAME			"aw36518"
28 
29 #define AW36518_REG_ID			0x00
30 #define AW36518_ID			0x30
31 
32 #define AW36518_REG_MODE		0x01
33 #define AW36518_REG_LED0_FLASH_CUR	0x03
34 #define AW36518_REG_LED0_TORCH_CUR	0x05
35 #define AW36518_REG_LED1_FLASH_CUR	0x03
36 #define AW36518_REG_LED1_TORCH_CUR	0x05
37 #define AW36518_HW_TORCH		BIT(4)
38 #define AW36518_HW_STROBE		BIT(5)
39 
40 #define AW36518_REG_FAULT		0x0A
41 #define AW36518_REG_FL_SC		0x70
42 #define AW36518_REG_FL_OT		BIT(2)
43 #define AW36518_REG_FL_TO		BIT(0)
44 
45 #define AW36518_REG_FAULT2		0x0B
46 #define AW36518_REG_FL_OVP		BIT(1)
47 
48 #define AW36518_REG_ENABLE		0x01
49 #define LED_ON				0x03
50 #define LED_OFF				0x00
51 
52 /*  FLASH Brightness
53  *	min 2940uA, step 5870uA, max 1500000uA
54  */
55 #define AW36518_MIN_FLASH_INTENSITY	2940
56 #define AW36518_MAX_FLASH_INTENSITY	1500000
57 #define AW36518_FLASH_INTENSITY_DEFAULT	748430
58 #define AW36518_FLASH_INTENSITY_STEP	5870
59 #define AW36518_FLASH_BRT_uA_TO_REG(a)	\
60 	((a) < AW36518_MIN_FLASH_INTENSITY ? 0 :	\
61 	 (((a) - AW36518_MIN_FLASH_INTENSITY) / AW36518_FLASH_INTENSITY_STEP))
62 #define AW36518_FLASH_BRT_REG_TO_uA(a)		\
63 	((a) * AW36518_FLASH_INTENSITY_STEP + AW36518_MIN_FLASH_INTENSITY)
64 
65 /*  TORCH BRT
66  *	min 750uA, step 1510uA, max 3860000uA
67  */
68 #define AW36518_MIN_TORCH_INTENSITY	750
69 #define AW36518_MAX_TORCH_INTENSITY	386000
70 #define AW36518_TORCH_INTENSITY_DEFAULT	192000
71 #define AW36518_TORCH_INTENSITY_STEP	1510
72 #define AW36518_TORCH_BRT_uA_TO_REG(a)	\
73 	((a) < AW36518_MIN_TORCH_INTENSITY ? 0 :	\
74 	 (((a) - AW36518_MIN_TORCH_INTENSITY) / AW36518_TORCH_INTENSITY_STEP))
75 #define AW36518_TORCH_BRT_REG_TO_uA(a)		\
76 	((a) * AW36518_TORCH_INTENSITY_STEP + AW36518_MIN_TORCH_INTENSITY)
77 
78 
79 /*  FLASH TIMEOUT DURATION
80  *	min 40ms, step 40 or 200ms, max 1600ms
81  */
82 #define TIMEOUT_MAX			1600000
83 #define TIMEOUT_STEP			40000
84 #define TIMEOUT_MIN			40000
85 #define TIMEOUT_STEP2			200000
86 #define TIMEOUT_DEFAULT			600000
87 
88 #define AW36518_FLASH_TOUT_ms_TO_REG(a)	\
89 	((a) < TIMEOUT_MIN ? 0 :	\
90 	 (((a) - TIMEOUT_MIN) / TIMEOUT_STEP))
91 #define AW36518_FLASH_TOUT_REG_TO_ms(a)		\
92 	((a) * TIMEOUT_STEP + TIMEOUT_MIN)
93 
94 static int debug;
95 module_param(debug, int, 0644);
96 MODULE_PARM_DESC(debug, "debug level (0-2)");
97 
98 enum aw36518_led_id {
99 	LED0 = 0,
100 	LED1,
101 	LED_MAX
102 };
103 
104 struct aw36518_led {
105 	struct v4l2_subdev sd;
106 	struct v4l2_ctrl_handler ctrls;
107 	struct v4l2_ctrl *flash_brt;
108 	struct v4l2_ctrl *torch_brt;
109 	struct __kernel_old_timeval timestamp;
110 	u32 max_flash_timeout;
111 	u32 max_flash_intensity;
112 	u32 max_torch_intensity;
113 };
114 
115 struct aw36518_flash {
116 	struct i2c_client *client;
117 	struct aw36518_led leds[LED_MAX];
118 
119 	struct gpio_desc *en_gpio;
120 	struct gpio_desc *torch_gpio;
121 	struct gpio_desc *strobe_gpio;
122 	struct gpio_desc *tx_gpio;
123 
124 	struct mutex lock;
125 
126 	u32 flash_timeout;
127 	enum v4l2_flash_led_mode led_mode;
128 
129 	u32 module_index;
130 	const char *module_facing;
131 	bool			power_on;
132 };
133 
134 #define to_led(sd) container_of(sd, struct aw36518_led, sd)
135 
aw36518_i2c_write(struct aw36518_flash * flash,u8 reg,u8 val)136 static int aw36518_i2c_write(struct aw36518_flash *flash, u8 reg, u8 val)
137 {
138 	struct i2c_client *client = flash->client;
139 	int ret;
140 
141 	ret = i2c_smbus_write_byte_data(client, reg, val);
142 	if (ret < 0)
143 		dev_err(&client->dev,
144 			 "%s: reg:0x%x val:0x%x failed\n",
145 			 __func__, reg, val);
146 
147 	v4l2_dbg(2, debug, &flash->leds[0].sd,
148 		 "%s: reg:0x%x val:0x%x\n",
149 		  __func__, reg, val);
150 
151 	return ret;
152 }
153 
aw36518_i2c_read(struct aw36518_flash * flash,u8 reg)154 static int aw36518_i2c_read(struct aw36518_flash *flash, u8 reg)
155 {
156 	struct i2c_client *client = flash->client;
157 	int ret;
158 
159 	ret = i2c_smbus_read_byte_data(client, reg);
160 	if (ret < 0)
161 		dev_err(&client->dev,
162 			 "%s: reg:0x%x failed\n",
163 			 __func__, reg);
164 
165 	dev_dbg(&client->dev, "%s: reg:0x%x val:0x%x\n",
166 		 __func__, reg, ret);
167 	return ret;
168 }
169 
aw36518_led_on(struct aw36518_flash * flash,bool on)170 static int aw36518_led_on(struct aw36518_flash *flash, bool on)
171 {
172 	int ret;
173 	struct i2c_client *client = flash->client;
174 	int temp;
175 	u8 val = 0;
176 
177 	v4l2_dbg(1, debug, &flash->leds[0].sd,
178 		 "%s: on:%d\n", __func__, on);
179 
180 	temp = aw36518_i2c_read(flash, AW36518_REG_ENABLE);
181 	if (temp < 0)
182 		dev_err(&client->dev,
183 			 "%s: read reg:0x%x failed.\n",
184 			 __func__, AW36518_REG_ENABLE);
185 
186 	if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH && on)
187 		temp = temp | 0x0c;
188 	else
189 		temp = temp & 0xfc;
190 
191 	v4l2_dbg(1, debug, &flash->leds[0].sd,
192 		 "%s: temp:%d\n", __func__, temp);
193 	val = on ? LED_ON : LED_OFF;
194 	ret = aw36518_i2c_write(flash, AW36518_REG_ENABLE, val | temp);
195 	flash->leds[0].timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
196 	flash->leds[1].timestamp = flash->leds[0].timestamp;
197 	return ret;
198 }
199 
aw36518_get_fault(struct aw36518_flash * flash)200 static int aw36518_get_fault(struct aw36518_flash *flash)
201 {
202 	int fault = 0;
203 	int temp = 0;
204 
205 	fault = aw36518_i2c_read(flash, AW36518_REG_FAULT);
206 
207 	v4l2_dbg(1, debug, &flash->leds[0].sd,
208 		 "%s: 0x%x\n", __func__, fault);
209 	fault = fault & 0xfd;
210 
211 	temp = aw36518_i2c_read(flash, AW36518_REG_FAULT2);
212 	fault = fault | (temp & 0x2);
213 
214 	return fault;
215 }
216 
aw36518_timeout_cal(struct aw36518_flash * flash)217 static int aw36518_timeout_cal(struct aw36518_flash *flash)
218 {
219 	u8 val;
220 
221 	v4l2_dbg(1, debug, &flash->leds[0].sd,
222 		 "%s: timeout:%dUS\n", __func__, flash->flash_timeout);
223 
224 	if (flash->flash_timeout < 400000)
225 		val =  AW36518_FLASH_TOUT_ms_TO_REG(flash->flash_timeout);
226 	else {
227 		val =  (flash->flash_timeout - 0x400000) / TIMEOUT_STEP2;
228 		val = val + 0x09;
229 	}
230 
231 	return val;
232 }
233 
aw36518_set_timeout(struct aw36518_flash * flash,u32 timeout)234 static int aw36518_set_timeout(struct aw36518_flash *flash, u32 timeout)
235 {
236 	int ret;
237 	u8 val;
238 
239 	v4l2_dbg(1, debug, &flash->leds[0].sd,
240 		 "%s: %d\n",
241 		 __func__, flash->flash_timeout);
242 
243 	flash->flash_timeout = timeout;
244 	ret = aw36518_i2c_read(flash, 0x08);
245 	if (ret < 0)
246 		return ret;
247 
248 	val =  aw36518_timeout_cal(flash);
249 
250 	return aw36518_i2c_write(flash, 0x08, val | (ret & 0xf0));
251 }
252 
aw36518_torch_brt(struct aw36518_flash * flash,enum aw36518_led_id id)253 static int aw36518_torch_brt(struct aw36518_flash *flash,
254 			     enum aw36518_led_id id)
255 {
256 	struct aw36518_led *led = &flash->leds[id];
257 	u8 val, reg;
258 
259 	v4l2_dbg(1, debug, &led->sd,
260 		 "%s: %d\n", __func__, led->torch_brt->val);
261 
262 	val = AW36518_TORCH_BRT_uA_TO_REG(led->torch_brt->val);
263 	reg = id ? AW36518_REG_LED1_TORCH_CUR : AW36518_REG_LED0_TORCH_CUR;
264 	return aw36518_i2c_write(flash, reg, val);
265 }
266 
aw36518_flash_brt(struct aw36518_flash * flash,enum aw36518_led_id id)267 static int aw36518_flash_brt(struct aw36518_flash *flash,
268 			     enum aw36518_led_id id)
269 {
270 	struct aw36518_led *led = &flash->leds[id];
271 	u8 val, reg;
272 
273 	v4l2_dbg(1, debug, &led->sd,
274 		 "%s: %d\n", __func__, led->flash_brt->val);
275 
276 	val = AW36518_FLASH_BRT_uA_TO_REG(led->flash_brt->val);
277 	reg = id ? AW36518_REG_LED1_FLASH_CUR : AW36518_REG_LED0_FLASH_CUR;
278 	return aw36518_i2c_write(flash, reg, val);
279 }
280 
aw36518_set_mode(struct aw36518_flash * flash,enum aw36518_led_id id,unsigned int mode)281 static int aw36518_set_mode(struct aw36518_flash *flash,
282 			    enum aw36518_led_id id, unsigned int mode)
283 {
284 	int ret = 0;
285 
286 	v4l2_dbg(1, debug, &flash->leds[id].sd,
287 		 "%s: %d cur:%d\n", __func__,
288 		 mode, flash->led_mode);
289 
290 	if (flash->led_mode == mode)
291 		return 0;
292 
293 	aw36518_led_on(flash, false);
294 
295 	flash->led_mode = mode;
296 
297 	if (mode == V4L2_FLASH_LED_MODE_FLASH) {
298 		ret = aw36518_i2c_write(flash, 0x01, 0x0C);
299 		ret |= aw36518_flash_brt(flash, LED0);
300 	} else if (mode == V4L2_FLASH_LED_MODE_TORCH) {
301 		//ret = aw36518_i2c_write(flash, 0x01, 0x08);
302 		/* hw torch/strobe io trigger torch */
303 		ret = aw36518_i2c_write(flash, 0x01, AW36518_HW_TORCH);
304 		ret |= aw36518_torch_brt(flash, LED0);
305 		ret |= aw36518_led_on(flash, true);
306 		if (flash->torch_gpio) {
307 			v4l2_dbg(1, debug, &flash->leds[id].sd,
308 				 "%s:set torch gpio high.\n", __func__);
309 			gpiod_set_value_cansleep(flash->torch_gpio, 1);
310 		}
311 
312 	} else {
313 		ret = aw36518_i2c_write(flash, 0x01, 0x00);
314 		if (flash->torch_gpio) {
315 			v4l2_dbg(1, debug, &flash->leds[id].sd,
316 				 "%s:set torch gpio low.\n", __func__);
317 			gpiod_set_value_cansleep(flash->torch_gpio, 0);
318 		}
319 
320 	}
321 
322 	return ret;
323 }
324 
aw36518_strobe(struct aw36518_flash * flash,bool on)325 static int aw36518_strobe(struct aw36518_flash *flash, bool on)
326 {
327 	int ret;
328 
329 	v4l2_dbg(1, debug, &flash->leds[0].sd,
330 		 "%s: on %d\n", __func__, on);
331 
332 	if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
333 		return -EBUSY;
334 	ret = aw36518_led_on(flash, on);
335 
336 	return ret;
337 }
338 
aw36518_get_ctrl(struct v4l2_ctrl * ctrl,enum aw36518_led_id id)339 static int aw36518_get_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
340 {
341 	struct aw36518_led *led =
342 		container_of(ctrl->handler, struct aw36518_led, ctrls);
343 	struct aw36518_flash *flash =
344 		container_of(led, struct aw36518_flash, leds[id]);
345 	int ret = 0;
346 	struct i2c_client *client = flash->client;
347 
348 	v4l2_dbg(1, debug, &flash->leds[id].sd,
349 		 "%s: id 0x%x\n", __func__, ctrl->id);
350 
351 	mutex_lock(&flash->lock);
352 	switch (ctrl->id) {
353 	case V4L2_CID_FLASH_FAULT:
354 		ret = aw36518_get_fault(flash);
355 		ctrl->val = 0;
356 		if (ret & AW36518_REG_FL_SC)
357 			ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
358 		if (ret & AW36518_REG_FL_OT)
359 			ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
360 		if (ret & AW36518_REG_FL_TO)
361 			ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
362 		if (ret & AW36518_REG_FL_OVP)
363 			ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
364 		ret = 0;
365 		break;
366 	default:
367 		dev_err(&client->dev,
368 			 "ctrl 0x%x not supported\n", ctrl->id);
369 		ret = -EINVAL;
370 		break;
371 	}
372 
373 	mutex_unlock(&flash->lock);
374 	return ret;
375 }
376 
aw36518_set_ctrl(struct v4l2_ctrl * ctrl,enum aw36518_led_id id)377 static int aw36518_set_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
378 {
379 	struct aw36518_led *led =
380 		container_of(ctrl->handler, struct aw36518_led, ctrls);
381 	struct aw36518_flash *flash =
382 		container_of(led, struct aw36518_flash, leds[id]);
383 	int ret = 0;
384 	struct i2c_client *client = flash->client;
385 
386 	v4l2_dbg(1, debug, &led->sd,
387 		 "%s: id 0x%x val 0x%x\n",
388 		 __func__, ctrl->id, ctrl->val);
389 
390 	mutex_lock(&flash->lock);
391 
392 	ret = aw36518_get_fault(flash);
393 	if ((ret & (AW36518_REG_FL_OVP |
394 		     AW36518_REG_FL_OT |
395 		     AW36518_REG_FL_SC)) &&
396 	    (ctrl->id == V4L2_CID_FLASH_STROBE ||
397 	     ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
398 	     ctrl->id == V4L2_CID_FLASH_LED_MODE)) {
399 		ret = -EBUSY;
400 		goto err;
401 	}
402 
403 	switch (ctrl->id) {
404 	case V4L2_CID_FLASH_LED_MODE:
405 		ret = aw36518_set_mode(flash, id, ctrl->val);
406 		break;
407 	case V4L2_CID_FLASH_STROBE:
408 		ret = aw36518_strobe(flash, true);
409 		break;
410 	case V4L2_CID_FLASH_STROBE_STOP:
411 		ret = aw36518_strobe(flash, false);
412 		break;
413 	case V4L2_CID_FLASH_TIMEOUT:
414 		ret = aw36518_set_timeout(flash, ctrl->val);
415 		break;
416 	case V4L2_CID_FLASH_INTENSITY:
417 		ret = aw36518_flash_brt(flash, id);
418 		break;
419 	case V4L2_CID_FLASH_TORCH_INTENSITY:
420 		ret = aw36518_torch_brt(flash, id);
421 		break;
422 	default:
423 		dev_err(&client->dev,
424 			"ctrl 0x%x not supported\n", ctrl->id);
425 		ret = -EINVAL;
426 		break;
427 	}
428 
429 err:
430 	mutex_unlock(&flash->lock);
431 	return ret;
432 }
433 
aw36518_led0_get_ctrl(struct v4l2_ctrl * ctrl)434 static int aw36518_led0_get_ctrl(struct v4l2_ctrl *ctrl)
435 {
436 	return aw36518_get_ctrl(ctrl, LED0);
437 }
438 
aw36518_led0_set_ctrl(struct v4l2_ctrl * ctrl)439 static int aw36518_led0_set_ctrl(struct v4l2_ctrl *ctrl)
440 {
441 	return aw36518_set_ctrl(ctrl, LED0);
442 }
443 
aw36518_led1_get_ctrl(struct v4l2_ctrl * ctrl)444 static int aw36518_led1_get_ctrl(struct v4l2_ctrl *ctrl)
445 {
446 	return aw36518_get_ctrl(ctrl, LED1);
447 }
448 
aw36518_led1_set_ctrl(struct v4l2_ctrl * ctrl)449 static int aw36518_led1_set_ctrl(struct v4l2_ctrl *ctrl)
450 {
451 	return aw36518_set_ctrl(ctrl, LED1);
452 }
453 
454 static const struct v4l2_ctrl_ops aw36518_ctrl_ops[LED_MAX] = {
455 	[LED0] = {
456 		.g_volatile_ctrl = aw36518_led0_get_ctrl,
457 		.s_ctrl = aw36518_led0_set_ctrl,
458 	},
459 	[LED1] = {
460 		.g_volatile_ctrl = aw36518_led1_get_ctrl,
461 		.s_ctrl = aw36518_led1_set_ctrl,
462 	}
463 };
464 
aw36518_init_controls(struct aw36518_flash * flash,enum aw36518_led_id id)465 static int aw36518_init_controls(struct aw36518_flash *flash,
466 				 enum aw36518_led_id id)
467 {
468 	struct v4l2_ctrl *fault;
469 	struct v4l2_ctrl_handler *hdl = &flash->leds[id].ctrls;
470 	const struct v4l2_ctrl_ops *ops = &aw36518_ctrl_ops[id];
471 	struct aw36518_led *led = &flash->leds[id];
472 
473 	v4l2_ctrl_handler_init(hdl, 8);
474 
475 	v4l2_ctrl_new_std_menu(hdl, ops,
476 			       V4L2_CID_FLASH_LED_MODE,
477 			       V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
478 	flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
479 
480 	v4l2_ctrl_new_std_menu(hdl, ops,
481 			       V4L2_CID_FLASH_STROBE_SOURCE,
482 			       V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
483 
484 	v4l2_ctrl_new_std(hdl, ops,
485 			  V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
486 
487 	v4l2_ctrl_new_std(hdl, ops,
488 			  V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
489 
490 	v4l2_ctrl_new_std(hdl, ops,
491 			  V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
492 			  led->max_flash_timeout,
493 			  TIMEOUT_STEP,
494 			  led->max_flash_timeout);
495 	flash->flash_timeout = led->max_flash_timeout;
496 
497 	flash->leds[id].flash_brt =
498 		v4l2_ctrl_new_std(hdl, ops,
499 				  V4L2_CID_FLASH_INTENSITY,
500 				  AW36518_MIN_FLASH_INTENSITY,
501 				  led->max_flash_intensity,
502 				  AW36518_FLASH_INTENSITY_STEP,
503 				  AW36518_FLASH_INTENSITY_DEFAULT);
504 
505 	flash->leds[id].torch_brt =
506 		v4l2_ctrl_new_std(hdl, ops,
507 				  V4L2_CID_FLASH_TORCH_INTENSITY,
508 				  AW36518_MIN_TORCH_INTENSITY,
509 				  led->max_torch_intensity,
510 				  AW36518_TORCH_INTENSITY_STEP,
511 				  AW36518_TORCH_INTENSITY_DEFAULT);
512 
513 	fault = v4l2_ctrl_new_std(hdl, ops,
514 				  V4L2_CID_FLASH_FAULT, 0,
515 				  V4L2_FLASH_FAULT_OVER_VOLTAGE
516 				  | V4L2_FLASH_FAULT_TIMEOUT
517 				  | V4L2_FLASH_FAULT_OVER_TEMPERATURE
518 				  | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
519 
520 	if (hdl->error)
521 		return hdl->error;
522 
523 	fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
524 
525 	flash->leds[id].sd.ctrl_handler = hdl;
526 	return 0;
527 }
528 
aw36518_get_time_info(struct v4l2_subdev * sd,struct __kernel_old_timeval * ti)529 static void aw36518_get_time_info(struct v4l2_subdev *sd,
530 				  struct __kernel_old_timeval *ti)
531 {
532 	struct aw36518_led *led =
533 		container_of(sd, struct aw36518_led, sd);
534 
535 	memset(ti, 0, sizeof(*ti));
536 	ti->tv_sec = led->timestamp.tv_sec;
537 	ti->tv_usec = led->timestamp.tv_usec;
538 	v4l2_dbg(1, debug, sd,
539 		 "%s: tv_sec:%ld, tv_usec:%ld\n", __func__, ti->tv_sec, ti->tv_usec);
540 }
541 
aw36518_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)542 static long aw36518_ioctl(struct v4l2_subdev *sd,
543 			 unsigned int cmd, void *arg)
544 {
545 	long ret = 0;
546 
547 	switch (cmd) {
548 	case RK_VIDIOC_FLASH_TIMEINFO:
549 		aw36518_get_time_info(sd, (struct __kernel_old_timeval *)arg);
550 		break;
551 
552 	default:
553 		ret = -ENOIOCTLCMD;
554 		break;
555 	}
556 	return ret;
557 }
558 
559 #ifdef CONFIG_COMPAT
560 #define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
561 	_IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
562 
aw36518_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)563 static long aw36518_compat_ioctl32(struct v4l2_subdev *sd,
564 				  unsigned int cmd,
565 				  unsigned long arg)
566 {
567 	void __user *up = compat_ptr(arg);
568 	struct old_timeval32 *compat_t;
569 	long ret;
570 	struct __kernel_old_timeval t;
571 
572 	switch (cmd) {
573 	case RK_VIDIOC_COMPAT_FLASH_TIMEINFO:
574 		compat_t = kzalloc(sizeof(*compat_t), GFP_KERNEL);
575 		if (!compat_t) {
576 			ret = -ENOMEM;
577 			return ret;
578 		}
579 		ret = aw36518_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, &t);
580 		if (!ret) {
581 			compat_t->tv_sec = t.tv_sec;
582 			compat_t->tv_usec = t.tv_usec;
583 			ret = copy_to_user(up, compat_t, sizeof(*compat_t));
584 			if (ret)
585 				ret = -EFAULT;
586 		}
587 		kfree(compat_t);
588 		break;
589 
590 	default:
591 		ret = -ENOIOCTLCMD;
592 		break;
593 	}
594 
595 	return ret;
596 }
597 #endif
598 
aw36518_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)599 static int aw36518_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
600 {
601 	int rval;
602 
603 	rval = pm_runtime_get_sync(sd->dev);
604 	if (rval < 0) {
605 		pm_runtime_put_noidle(sd->dev);
606 		return rval;
607 	}
608 
609 	return 0;
610 }
611 
aw36518_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)612 static int aw36518_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
613 {
614 	pm_runtime_put(sd->dev);
615 
616 	return 0;
617 }
618 
aw36518_s_power(struct v4l2_subdev * sd,int on)619 static int aw36518_s_power(struct v4l2_subdev *sd, int on)
620 {
621 	struct aw36518_led *led = to_led(sd);
622 	struct aw36518_flash *flash =
623 		container_of(led, struct aw36518_flash, leds[0]);
624 	int ret = 0;
625 	struct i2c_client *client = flash->client;
626 
627 	dev_info(&client->dev, "%s on(%d)\n", __func__, on);
628 	mutex_lock(&flash->lock);
629 
630 	/* If the power state is not modified - no work to do. */
631 	if (flash->power_on == !!on)
632 		goto unlock_and_return;
633 
634 	if (on) {
635 		ret = pm_runtime_get_sync(&client->dev);
636 		if (ret < 0) {
637 			pm_runtime_put_noidle(&client->dev);
638 			goto unlock_and_return;
639 		}
640 		flash->power_on = true;
641 	} else {
642 		pm_runtime_put(&client->dev);
643 		flash->power_on = false;
644 	}
645 
646 unlock_and_return:
647 	mutex_unlock(&flash->lock);
648 
649 	return ret;
650 }
651 
652 static const struct v4l2_subdev_core_ops aw36518_core_ops = {
653 	.s_power = aw36518_s_power,
654 	.ioctl = aw36518_ioctl,
655 #ifdef CONFIG_COMPAT
656 	.compat_ioctl32 = aw36518_compat_ioctl32
657 #endif
658 };
659 
660 static const struct v4l2_subdev_ops aw36518_ops = {
661 	.core = &aw36518_core_ops,
662 };
663 
664 static const struct v4l2_subdev_internal_ops aw36518_internal_ops = {
665 	.open = aw36518_open,
666 	.close = aw36518_close,
667 };
668 
__aw36518_set_power(struct aw36518_flash * flash,bool on)669 static int __aw36518_set_power(struct aw36518_flash *flash, bool on)
670 {
671 	gpiod_direction_output(flash->en_gpio, on);
672 
673 	return 0;
674 }
675 
aw36518_check_id(struct aw36518_flash * flash)676 static int aw36518_check_id(struct aw36518_flash *flash)
677 {
678 	int ret = 0;
679 
680 	__aw36518_set_power(flash, true);
681 	ret = aw36518_i2c_read(flash, AW36518_REG_ID);
682 	if (ret != AW36518_ID) {
683 		dev_err(&flash->client->dev,
684 			"Read chip id error\n");
685 		return -ENODEV;
686 	}
687 
688 	dev_info(&flash->client->dev,
689 		 "Detected aw36518 flash id:0x%x\n", ret);
690 	return 0;
691 }
692 
aw36518_of_init(struct i2c_client * client,struct aw36518_flash * flash)693 static int aw36518_of_init(struct i2c_client *client,
694 			   struct aw36518_flash *flash)
695 {
696 	struct device_node *node = client->dev.of_node;
697 	struct device_node *child;
698 	struct aw36518_led *led;
699 	int ret = 0;
700 
701 	if (!node) {
702 		dev_err(&client->dev,
703 			"get device node failed\n");
704 		goto err;
705 	}
706 
707 	ret = of_property_read_u32(node,
708 				   RKMODULE_CAMERA_MODULE_INDEX,
709 				   &flash->module_index);
710 	ret |= of_property_read_string(node,
711 				       RKMODULE_CAMERA_MODULE_FACING,
712 				       &flash->module_facing);
713 	if (ret) {
714 		dev_err(&client->dev,
715 			"could not get module information!\n");
716 		goto err;
717 	}
718 
719 	flash->en_gpio = devm_gpiod_get(&client->dev,
720 					"enable", GPIOD_OUT_LOW);
721 	if (IS_ERR(flash->en_gpio)) {
722 		flash->en_gpio = NULL;
723 		dev_warn(&client->dev,
724 			 "get enable-gpio failed, using assist light mode\n");
725 	}
726 
727 	flash->torch_gpio = devm_gpiod_get(&client->dev,
728 					   "torch", GPIOD_OUT_LOW);
729 	if (IS_ERR(flash->torch_gpio)) {
730 		flash->torch_gpio = NULL;
731 		dev_warn(&client->dev,
732 			 "get torch-gpio failed, using assist light mode\n");
733 	}
734 
735 	flash->strobe_gpio = devm_gpiod_get(&client->dev,
736 					    "strobe", GPIOD_OUT_LOW);
737 	if (IS_ERR(flash->strobe_gpio)) {
738 		flash->strobe_gpio = NULL;
739 		dev_warn(&client->dev,
740 			 "get strobe-gpio failed, using assist light mode\n");
741 
742 	}
743 	flash->tx_gpio = devm_gpiod_get(&client->dev,
744 					    "tx", GPIOD_OUT_LOW);
745 	if (IS_ERR(flash->tx_gpio)) {
746 		flash->tx_gpio = NULL;
747 		dev_warn(&client->dev,
748 			 "get tx-gpio failed, using assist light mode\n");
749 	}
750 
751 	for_each_child_of_node(node, child) {
752 		u32 id = 0;
753 
754 		of_property_read_u32(child, "reg", &id);
755 		if (id >= LED_MAX) {
756 			dev_err(&client->dev, "only support 2 leds\n");
757 			goto err;
758 		}
759 		led = &flash->leds[id];
760 		led->sd.fwnode = of_fwnode_handle(child);
761 		if (of_property_read_u32(child, "flash-max-timeout-us",
762 					 &led->max_flash_timeout)) {
763 			dev_err(&client->dev,
764 				"get led%d flash-max-timeout-us fail\n", id);
765 			goto err;
766 		}
767 		if (led->max_flash_timeout > TIMEOUT_MAX)
768 			led->max_flash_timeout = TIMEOUT_MAX;
769 
770 		if (of_property_read_u32(child, "flash-max-microamp",
771 					 &led->max_flash_intensity)) {
772 			dev_err(&client->dev,
773 				"get led%d flash-max-microamp fail\n", id);
774 			goto err;
775 		}
776 		if (led->max_flash_intensity > AW36518_MAX_FLASH_INTENSITY)
777 			led->max_flash_intensity = AW36518_MAX_FLASH_INTENSITY;
778 
779 		if (of_property_read_u32(child, "led-max-microamp",
780 					  &led->max_torch_intensity)) {
781 			dev_err(&client->dev,
782 				"get led%d led-max-microamp fail\n", id);
783 			goto err;
784 		}
785 		if (led->max_torch_intensity > AW36518_MAX_TORCH_INTENSITY)
786 			led->max_torch_intensity = AW36518_MAX_TORCH_INTENSITY;
787 
788 		v4l2_dbg(1, debug, &led->sd,
789 			 "led%d max torch:%dUA flash:%dUA timeout:%dUS\n",
790 			 id, led->max_torch_intensity,
791 			 led->max_flash_intensity,
792 			 led->max_flash_timeout);
793 	}
794 
795 	return 0;
796 err:
797 	return -EINVAL;
798 }
799 
aw36518_init_device(struct aw36518_flash * flash)800 static int aw36518_init_device(struct aw36518_flash *flash)
801 {
802 	int ret;
803 	unsigned int reg_val;
804 	struct i2c_client *client = flash->client;
805 
806 	/* output disable */
807 	flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
808 	ret = aw36518_set_mode(flash, 0, 0);
809 	if (ret < 0)
810 		return ret;
811 	/* reset faults */
812 	reg_val = aw36518_i2c_read(flash, AW36518_REG_FAULT);
813 	dev_info(&client->dev, "%s: fault: 0x%x.\n", __func__, reg_val);
814 	/* tx input default low */
815 	if (flash->tx_gpio)
816 		gpiod_direction_output(flash->tx_gpio, 0);
817 	/* STROBE/Torch input default low */
818 	if (flash->torch_gpio)
819 		gpiod_set_value_cansleep(flash->torch_gpio, 0);
820 
821 	return ret;
822 }
823 
aw36518_probe(struct i2c_client * client,const struct i2c_device_id * devid)824 static int aw36518_probe(struct i2c_client *client,
825 			 const struct i2c_device_id *devid)
826 {
827 	struct aw36518_flash *flash;
828 	struct v4l2_subdev *sd;
829 	char facing[2];
830 	int i, ret;
831 
832 	dev_info(&client->dev, "driver version: %02x.%02x.%02x",
833 		 DRIVER_VERSION >> 16,
834 		 (DRIVER_VERSION & 0xff00) >> 8,
835 		 DRIVER_VERSION & 0x00ff);
836 
837 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
838 	if (!flash)
839 		return -ENOMEM;
840 
841 	flash->client = client;
842 
843 	ret = aw36518_of_init(client, flash);
844 	if (ret)
845 		return ret;
846 
847 	ret = aw36518_check_id(flash);
848 	if (ret)
849 		goto err_power_off;
850 
851 	for (i = 0; i < LED_MAX; i++) {
852 		sd = &flash->leds[i].sd;
853 		v4l2_i2c_subdev_init(sd, client, &aw36518_ops);
854 		sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
855 		sd->internal_ops = &aw36518_internal_ops;
856 
857 		memset(facing, 0, sizeof(facing));
858 		if (strcmp(flash->module_facing, "back") == 0)
859 			facing[0] = 'b';
860 		else
861 			facing[0] = 'f';
862 		/* NOTE: to distinguish between two led
863 		 * name: led0 meet the main led
864 		 * name: led1 meet the secondary led
865 		 */
866 		snprintf(sd->name, sizeof(sd->name),
867 			 "m%02d_%s_%s_led%d %s",
868 			 flash->module_index, facing,
869 			 AW36518_NAME, i, dev_name(sd->dev));
870 
871 		ret = aw36518_init_controls(flash, i);
872 		if (ret)
873 			goto err;
874 
875 		ret = media_entity_pads_init(&sd->entity, 0, NULL);
876 		if (ret < 0)
877 			goto free_ctl;
878 
879 		sd->entity.function = MEDIA_ENT_F_FLASH;
880 		ret = v4l2_async_register_subdev(sd);
881 		if (ret)
882 			goto free_media;
883 	}
884 
885 	ret = aw36518_init_device(flash);
886 	if (ret < 0)
887 		goto free_media;
888 
889 	i2c_set_clientdata(client, flash);
890 
891 	mutex_init(&flash->lock);
892 
893 	pm_runtime_set_active(&client->dev);
894 	pm_runtime_enable(&client->dev);
895 	pm_runtime_idle(&client->dev);
896 
897 	dev_info(&client->dev, "probing successful\n");
898 
899 	return 0;
900 free_media:
901 	media_entity_cleanup(&flash->leds[i].sd.entity);
902 free_ctl:
903 	v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
904 err:
905 	for (--i; i >= 0; --i) {
906 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
907 		media_entity_cleanup(&flash->leds[i].sd.entity);
908 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
909 	}
910 err_power_off:
911 	__aw36518_set_power(flash, false);
912 	return ret;
913 }
914 
aw36518_remove(struct i2c_client * client)915 static int aw36518_remove(struct i2c_client *client)
916 {
917 	struct aw36518_flash *flash = i2c_get_clientdata(client);
918 	unsigned int i;
919 
920 	pm_runtime_disable(&client->dev);
921 	for (i = 0; i < LED_MAX; i++) {
922 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
923 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
924 		media_entity_cleanup(&flash->leds[i].sd.entity);
925 	}
926 	mutex_destroy(&flash->lock);
927 	return 0;
928 }
929 
aw36518_runtime_suspend(struct device * dev)930 static int __maybe_unused aw36518_runtime_suspend(struct device *dev)
931 {
932 	struct i2c_client *client = to_i2c_client(dev);
933 	struct aw36518_flash *flash = i2c_get_clientdata(client);
934 
935 	return __aw36518_set_power(flash, false);
936 }
937 
aw36518_runtime_resume(struct device * dev)938 static int __maybe_unused aw36518_runtime_resume(struct device *dev)
939 {
940 	struct i2c_client *client = to_i2c_client(dev);
941 	struct aw36518_flash *flash = i2c_get_clientdata(client);
942 
943 	return __aw36518_set_power(flash, true);
944 }
945 
946 static const struct i2c_device_id aw36518_id_table[] = {
947 	{ AW36518_NAME, 0 },
948 	{ { 0 } }
949 };
950 MODULE_DEVICE_TABLE(i2c, aw36518_id_table);
951 
952 static const struct of_device_id aw36518_of_table[] = {
953 	{ .compatible = "awinic,aw36518" },
954 	{ { 0 } }
955 };
956 
957 static const struct dev_pm_ops aw36518_pm_ops = {
958 	SET_RUNTIME_PM_OPS(aw36518_runtime_suspend,
959 			   aw36518_runtime_resume, NULL)
960 };
961 
962 static struct i2c_driver aw36518_i2c_driver = {
963 	.driver	= {
964 		.name = AW36518_NAME,
965 		.pm = &aw36518_pm_ops,
966 		.of_match_table = aw36518_of_table,
967 	},
968 	.probe = aw36518_probe,
969 	.remove	= aw36518_remove,
970 	.id_table = aw36518_id_table,
971 };
972 
973 module_i2c_driver(aw36518_i2c_driver);
974 
975 MODULE_DESCRIPTION("AW36518 LED flash driver");
976 MODULE_LICENSE("GPL");
977