xref: /linux/drivers/leds/flash/leds-aat1290.c (revision 92815da4576a495cb6362cdfb132152fccc2222d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	LED Flash class driver for the AAT1290
4  *	1.5A Step-Up Current Regulator for Flash LEDs
5  *
6  *	Copyright (C) 2015, Samsung Electronics Co., Ltd.
7  *	Author: Jacek Anaszewski <j.anaszewski@samsung.com>
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/led-class-flash.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-flash-led-class.h>
21 
22 #define AAT1290_MOVIE_MODE_CURRENT_ADDR	17
23 #define AAT1290_MAX_MM_CURR_PERCENT_0	16
24 #define AAT1290_MAX_MM_CURR_PERCENT_100	1
25 
26 #define AAT1290_FLASH_SAFETY_TIMER_ADDR	18
27 
28 #define AAT1290_MOVIE_MODE_CONFIG_ADDR	19
29 #define AAT1290_MOVIE_MODE_OFF		1
30 #define AAT1290_MOVIE_MODE_ON		3
31 
32 #define AAT1290_MM_CURRENT_RATIO_ADDR	20
33 #define AAT1290_MM_TO_FL_1_92		1
34 
35 #define AAT1290_MM_TO_FL_RATIO		1000 / 1920
36 #define AAT1290_MAX_MM_CURRENT(fl_max)	(fl_max * AAT1290_MM_TO_FL_RATIO)
37 
38 #define AAT1290_LATCH_TIME_MIN_US	500
39 #define AAT1290_LATCH_TIME_MAX_US	1000
40 #define AAT1290_EN_SET_TICK_TIME_US	1
41 #define AAT1290_FLEN_OFF_DELAY_TIME_US	10
42 #define AAT1290_FLASH_TM_NUM_LEVELS	16
43 #define AAT1290_MM_CURRENT_SCALE_SIZE	15
44 
45 #define AAT1290_NAME			"aat1290"
46 
47 
48 struct aat1290_led_config_data {
49 	/* maximum LED current in movie mode */
50 	u32 max_mm_current;
51 	/* maximum LED current in flash mode */
52 	u32 max_flash_current;
53 	/* maximum flash timeout */
54 	u32 max_flash_tm;
55 	/* external strobe capability */
56 	bool has_external_strobe;
57 	/* max LED brightness level */
58 	enum led_brightness max_brightness;
59 };
60 
61 struct aat1290_led {
62 	/* platform device data */
63 	struct platform_device *pdev;
64 	/* secures access to the device */
65 	struct mutex lock;
66 
67 	/* corresponding LED Flash class device */
68 	struct led_classdev_flash fled_cdev;
69 	/* V4L2 Flash device */
70 	struct v4l2_flash *v4l2_flash;
71 
72 	/* FLEN pin */
73 	struct gpio_desc *gpio_fl_en;
74 	/* EN|SET pin  */
75 	struct gpio_desc *gpio_en_set;
76 	/* movie mode current scale */
77 	int *mm_current_scale;
78 	/* device mode */
79 	bool movie_mode;
80 };
81 
82 static struct aat1290_led *fled_cdev_to_led(
83 				struct led_classdev_flash *fled_cdev)
84 {
85 	return container_of(fled_cdev, struct aat1290_led, fled_cdev);
86 }
87 
88 static struct led_classdev_flash *led_cdev_to_fled_cdev(
89 				struct led_classdev *led_cdev)
90 {
91 	return container_of(led_cdev, struct led_classdev_flash, led_cdev);
92 }
93 
94 static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value)
95 {
96 	int i;
97 
98 	gpiod_direction_output(led->gpio_fl_en, 0);
99 	gpiod_direction_output(led->gpio_en_set, 0);
100 
101 	udelay(AAT1290_FLEN_OFF_DELAY_TIME_US);
102 
103 	/* write address */
104 	for (i = 0; i < addr; ++i) {
105 		udelay(AAT1290_EN_SET_TICK_TIME_US);
106 		gpiod_direction_output(led->gpio_en_set, 0);
107 		udelay(AAT1290_EN_SET_TICK_TIME_US);
108 		gpiod_direction_output(led->gpio_en_set, 1);
109 	}
110 
111 	usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
112 
113 	/* write data */
114 	for (i = 0; i < value; ++i) {
115 		udelay(AAT1290_EN_SET_TICK_TIME_US);
116 		gpiod_direction_output(led->gpio_en_set, 0);
117 		udelay(AAT1290_EN_SET_TICK_TIME_US);
118 		gpiod_direction_output(led->gpio_en_set, 1);
119 	}
120 
121 	usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
122 }
123 
124 static void aat1290_set_flash_safety_timer(struct aat1290_led *led,
125 					unsigned int micro_sec)
126 {
127 	struct led_classdev_flash *fled_cdev = &led->fled_cdev;
128 	struct led_flash_setting *flash_tm = &fled_cdev->timeout;
129 	int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS -
130 				(micro_sec / flash_tm->step) + 1;
131 
132 	aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR,
133 							flash_tm_reg);
134 }
135 
136 /* LED subsystem callbacks */
137 
138 static int aat1290_led_brightness_set(struct led_classdev *led_cdev,
139 					enum led_brightness brightness)
140 {
141 	struct led_classdev_flash *fled_cdev = led_cdev_to_fled_cdev(led_cdev);
142 	struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
143 
144 	mutex_lock(&led->lock);
145 
146 	if (brightness == 0) {
147 		gpiod_direction_output(led->gpio_fl_en, 0);
148 		gpiod_direction_output(led->gpio_en_set, 0);
149 		led->movie_mode = false;
150 	} else {
151 		if (!led->movie_mode) {
152 			aat1290_as2cwire_write(led,
153 				AAT1290_MM_CURRENT_RATIO_ADDR,
154 				AAT1290_MM_TO_FL_1_92);
155 			led->movie_mode = true;
156 		}
157 
158 		aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR,
159 				AAT1290_MAX_MM_CURR_PERCENT_0 - brightness);
160 		aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR,
161 				AAT1290_MOVIE_MODE_ON);
162 	}
163 
164 	mutex_unlock(&led->lock);
165 
166 	return 0;
167 }
168 
169 static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
170 					 bool state)
171 
172 {
173 	struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
174 	struct led_classdev *led_cdev = &fled_cdev->led_cdev;
175 	struct led_flash_setting *timeout = &fled_cdev->timeout;
176 
177 	mutex_lock(&led->lock);
178 
179 	if (state) {
180 		aat1290_set_flash_safety_timer(led, timeout->val);
181 		gpiod_direction_output(led->gpio_fl_en, 1);
182 	} else {
183 		gpiod_direction_output(led->gpio_fl_en, 0);
184 		gpiod_direction_output(led->gpio_en_set, 0);
185 	}
186 
187 	/*
188 	 * To reenter movie mode after a flash event the part must be cycled
189 	 * off and back on to reset the movie mode and reprogrammed via the
190 	 * AS2Cwire. Therefore the brightness and movie_mode properties needs
191 	 * to be updated here to reflect the actual state.
192 	 */
193 	led_cdev->brightness = 0;
194 	led->movie_mode = false;
195 
196 	mutex_unlock(&led->lock);
197 
198 	return 0;
199 }
200 
201 static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
202 						u32 timeout)
203 {
204 	/*
205 	 * Don't do anything - flash timeout is cached in the led-class-flash
206 	 * core and will be applied in the strobe_set op, as writing the
207 	 * safety timer register spuriously turns the torch mode on.
208 	 */
209 
210 	return 0;
211 }
212 
213 static int aat1290_led_parse_dt(struct aat1290_led *led,
214 			struct aat1290_led_config_data *cfg,
215 			struct device_node **sub_node)
216 {
217 	struct device *dev = &led->pdev->dev;
218 	struct device_node *child_node;
219 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
220 	struct pinctrl *pinctrl;
221 #endif
222 	int ret = 0;
223 
224 	led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS);
225 	if (IS_ERR(led->gpio_fl_en)) {
226 		ret = PTR_ERR(led->gpio_fl_en);
227 		dev_err(dev, "Unable to claim gpio \"flen\".\n");
228 		return ret;
229 	}
230 
231 	led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS);
232 	if (IS_ERR(led->gpio_en_set)) {
233 		ret = PTR_ERR(led->gpio_en_set);
234 		dev_err(dev, "Unable to claim gpio \"enset\".\n");
235 		return ret;
236 	}
237 
238 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
239 	pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev);
240 	if (IS_ERR(pinctrl)) {
241 		cfg->has_external_strobe = false;
242 		dev_info(dev,
243 			 "No support for external strobe detected.\n");
244 	} else {
245 		cfg->has_external_strobe = true;
246 	}
247 #endif
248 
249 	child_node = of_get_next_available_child(dev_of_node(dev), NULL);
250 	if (!child_node) {
251 		dev_err(dev, "No DT child node found for connected LED.\n");
252 		return -EINVAL;
253 	}
254 
255 	ret = of_property_read_u32(child_node, "led-max-microamp",
256 				&cfg->max_mm_current);
257 	/*
258 	 * led-max-microamp will default to 1/20 of flash-max-microamp
259 	 * in case it is missing.
260 	 */
261 	if (ret < 0)
262 		dev_warn(dev,
263 			"led-max-microamp DT property missing\n");
264 
265 	ret = of_property_read_u32(child_node, "flash-max-microamp",
266 				&cfg->max_flash_current);
267 	if (ret < 0) {
268 		dev_err(dev,
269 			"flash-max-microamp DT property missing\n");
270 		goto err_parse_dt;
271 	}
272 
273 	ret = of_property_read_u32(child_node, "flash-max-timeout-us",
274 				&cfg->max_flash_tm);
275 	if (ret < 0) {
276 		dev_err(dev,
277 			"flash-max-timeout-us DT property missing\n");
278 		goto err_parse_dt;
279 	}
280 
281 	*sub_node = child_node;
282 
283 err_parse_dt:
284 	of_node_put(child_node);
285 
286 	return ret;
287 }
288 
289 static void aat1290_led_validate_mm_current(struct aat1290_led *led,
290 					struct aat1290_led_config_data *cfg)
291 {
292 	int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE;
293 
294 	while (e - b > 1) {
295 		i = b + (e - b) / 2;
296 		if (cfg->max_mm_current < led->mm_current_scale[i])
297 			e = i;
298 		else
299 			b = i;
300 	}
301 
302 	cfg->max_mm_current = led->mm_current_scale[b];
303 	cfg->max_brightness = b + 1;
304 }
305 
306 static int init_mm_current_scale(struct aat1290_led *led,
307 			struct aat1290_led_config_data *cfg)
308 {
309 	static const int max_mm_current_percent[] = {
310 		20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
311 		63, 71, 79, 89, 100
312 	};
313 	int i, max_mm_current =
314 			AAT1290_MAX_MM_CURRENT(cfg->max_flash_current);
315 
316 	led->mm_current_scale = devm_kzalloc(&led->pdev->dev,
317 					sizeof(max_mm_current_percent),
318 					GFP_KERNEL);
319 	if (!led->mm_current_scale)
320 		return -ENOMEM;
321 
322 	for (i = 0; i < AAT1290_MM_CURRENT_SCALE_SIZE; ++i)
323 		led->mm_current_scale[i] = max_mm_current *
324 					  max_mm_current_percent[i] / 100;
325 
326 	return 0;
327 }
328 
329 static int aat1290_led_get_configuration(struct aat1290_led *led,
330 					struct aat1290_led_config_data *cfg,
331 					struct device_node **sub_node)
332 {
333 	int ret;
334 
335 	ret = aat1290_led_parse_dt(led, cfg, sub_node);
336 	if (ret < 0)
337 		return ret;
338 	/*
339 	 * Init non-linear movie mode current scale basing
340 	 * on the max flash current from led configuration.
341 	 */
342 	ret = init_mm_current_scale(led, cfg);
343 	if (ret < 0)
344 		return ret;
345 
346 	aat1290_led_validate_mm_current(led, cfg);
347 
348 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
349 #else
350 	devm_kfree(&led->pdev->dev, led->mm_current_scale);
351 #endif
352 
353 	return 0;
354 }
355 
356 static void aat1290_init_flash_timeout(struct aat1290_led *led,
357 				struct aat1290_led_config_data *cfg)
358 {
359 	struct led_classdev_flash *fled_cdev = &led->fled_cdev;
360 	struct led_flash_setting *setting;
361 
362 	/* Init flash timeout setting */
363 	setting = &fled_cdev->timeout;
364 	setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS;
365 	setting->max = cfg->max_flash_tm;
366 	setting->step = setting->min;
367 	setting->val = setting->max;
368 }
369 
370 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
371 static enum led_brightness aat1290_intensity_to_brightness(
372 					struct v4l2_flash *v4l2_flash,
373 					s32 intensity)
374 {
375 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
376 	struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
377 	int i;
378 
379 	for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i)
380 		if (intensity >= led->mm_current_scale[i])
381 			return i + 1;
382 
383 	return 1;
384 }
385 
386 static s32 aat1290_brightness_to_intensity(struct v4l2_flash *v4l2_flash,
387 					enum led_brightness brightness)
388 {
389 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
390 	struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
391 
392 	return led->mm_current_scale[brightness - 1];
393 }
394 
395 static int aat1290_led_external_strobe_set(struct v4l2_flash *v4l2_flash,
396 						bool enable)
397 {
398 	struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev);
399 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
400 	struct led_classdev *led_cdev = &fled_cdev->led_cdev;
401 	struct pinctrl *pinctrl;
402 
403 	gpiod_direction_output(led->gpio_fl_en, 0);
404 	gpiod_direction_output(led->gpio_en_set, 0);
405 
406 	led->movie_mode = false;
407 	led_cdev->brightness = 0;
408 
409 	pinctrl = devm_pinctrl_get_select(&led->pdev->dev,
410 						enable ? "isp" : "host");
411 	if (IS_ERR(pinctrl)) {
412 		dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n");
413 		return PTR_ERR(pinctrl);
414 	}
415 
416 	return 0;
417 }
418 
419 static void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
420 					struct aat1290_led_config_data *led_cfg,
421 					struct v4l2_flash_config *v4l2_sd_cfg)
422 {
423 	struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
424 	struct led_flash_setting *s;
425 
426 	strscpy(v4l2_sd_cfg->dev_name, led_cdev->dev->kobj.name,
427 		sizeof(v4l2_sd_cfg->dev_name));
428 
429 	s = &v4l2_sd_cfg->intensity;
430 	s->min = led->mm_current_scale[0];
431 	s->max = led_cfg->max_mm_current;
432 	s->step = 1;
433 	s->val = s->max;
434 
435 	v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe;
436 }
437 
438 static const struct v4l2_flash_ops v4l2_flash_ops = {
439 	.external_strobe_set = aat1290_led_external_strobe_set,
440 	.intensity_to_led_brightness = aat1290_intensity_to_brightness,
441 	.led_brightness_to_intensity = aat1290_brightness_to_intensity,
442 };
443 #else
444 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
445 				struct aat1290_led_config_data *led_cfg,
446 				struct v4l2_flash_config *v4l2_sd_cfg)
447 {
448 }
449 static const struct v4l2_flash_ops v4l2_flash_ops;
450 #endif
451 
452 static const struct led_flash_ops flash_ops = {
453 	.strobe_set = aat1290_led_flash_strobe_set,
454 	.timeout_set = aat1290_led_flash_timeout_set,
455 };
456 
457 static int aat1290_led_probe(struct platform_device *pdev)
458 {
459 	struct device *dev = &pdev->dev;
460 	struct device_node *sub_node = NULL;
461 	struct aat1290_led *led;
462 	struct led_classdev *led_cdev;
463 	struct led_classdev_flash *fled_cdev;
464 	struct led_init_data init_data = {};
465 	struct aat1290_led_config_data led_cfg = {};
466 	struct v4l2_flash_config v4l2_sd_cfg = {};
467 	int ret;
468 
469 	led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
470 	if (!led)
471 		return -ENOMEM;
472 
473 	led->pdev = pdev;
474 	platform_set_drvdata(pdev, led);
475 
476 	fled_cdev = &led->fled_cdev;
477 	fled_cdev->ops = &flash_ops;
478 	led_cdev = &fled_cdev->led_cdev;
479 
480 	ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node);
481 	if (ret < 0)
482 		return ret;
483 
484 	mutex_init(&led->lock);
485 
486 	/* Initialize LED Flash class device */
487 	led_cdev->brightness_set_blocking = aat1290_led_brightness_set;
488 	led_cdev->max_brightness = led_cfg.max_brightness;
489 	led_cdev->flags |= LED_DEV_CAP_FLASH;
490 
491 	aat1290_init_flash_timeout(led, &led_cfg);
492 
493 	init_data.fwnode = of_fwnode_handle(sub_node);
494 	init_data.devicename = AAT1290_NAME;
495 
496 	/* Register LED Flash class device */
497 	ret = led_classdev_flash_register_ext(&pdev->dev, fled_cdev,
498 					      &init_data);
499 	if (ret < 0)
500 		goto err_flash_register;
501 
502 	aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg);
503 
504 	/* Create V4L2 Flash subdev. */
505 	led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node),
506 					  fled_cdev, &v4l2_flash_ops,
507 					  &v4l2_sd_cfg);
508 	if (IS_ERR(led->v4l2_flash)) {
509 		ret = PTR_ERR(led->v4l2_flash);
510 		goto error_v4l2_flash_init;
511 	}
512 
513 	return 0;
514 
515 error_v4l2_flash_init:
516 	led_classdev_flash_unregister(fled_cdev);
517 err_flash_register:
518 	mutex_destroy(&led->lock);
519 
520 	return ret;
521 }
522 
523 static void aat1290_led_remove(struct platform_device *pdev)
524 {
525 	struct aat1290_led *led = platform_get_drvdata(pdev);
526 
527 	v4l2_flash_release(led->v4l2_flash);
528 	led_classdev_flash_unregister(&led->fled_cdev);
529 
530 	mutex_destroy(&led->lock);
531 }
532 
533 static const struct of_device_id aat1290_led_dt_match[] = {
534 	{ .compatible = "skyworks,aat1290" },
535 	{},
536 };
537 MODULE_DEVICE_TABLE(of, aat1290_led_dt_match);
538 
539 static struct platform_driver aat1290_led_driver = {
540 	.probe		= aat1290_led_probe,
541 	.remove_new	= aat1290_led_remove,
542 	.driver		= {
543 		.name	= "aat1290",
544 		.of_match_table = aat1290_led_dt_match,
545 	},
546 };
547 
548 module_platform_driver(aat1290_led_driver);
549 
550 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
551 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
552 MODULE_LICENSE("GPL v2");
553