Lines Matching +full:led +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED Flash class driver for the AAT1290
4 * 1.5A Step-Up Current Regulator for Flash LEDs
12 #include <linux/led-class-flash.h>
20 #include <media/v4l2-flash-led-class.h>
24 #define AAT1290_MAX_MM_CURR_PERCENT_100 1
29 #define AAT1290_MOVIE_MODE_OFF 1
33 #define AAT1290_MM_TO_FL_1_92 1
40 #define AAT1290_EN_SET_TICK_TIME_US 1
49 /* maximum LED current in movie mode */
51 /* maximum LED current in flash mode */
57 /* max LED brightness level */
67 /* corresponding LED Flash class device */
96 static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value) in aat1290_as2cwire_write() argument
100 gpiod_direction_output(led->gpio_fl_en, 0); in aat1290_as2cwire_write()
101 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_as2cwire_write()
108 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_as2cwire_write()
110 gpiod_direction_output(led->gpio_en_set, 1); in aat1290_as2cwire_write()
118 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_as2cwire_write()
120 gpiod_direction_output(led->gpio_en_set, 1); in aat1290_as2cwire_write()
126 static void aat1290_set_flash_safety_timer(struct aat1290_led *led, in aat1290_set_flash_safety_timer() argument
129 struct led_classdev_flash *fled_cdev = &led->fled_cdev; in aat1290_set_flash_safety_timer()
130 struct led_flash_setting *flash_tm = &fled_cdev->timeout; in aat1290_set_flash_safety_timer()
131 int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS - in aat1290_set_flash_safety_timer()
132 (micro_sec / flash_tm->step) + 1; in aat1290_set_flash_safety_timer()
134 aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR, in aat1290_set_flash_safety_timer()
138 /* LED subsystem callbacks */
144 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); in aat1290_led_brightness_set() local
146 mutex_lock(&led->lock); in aat1290_led_brightness_set()
149 gpiod_direction_output(led->gpio_fl_en, 0); in aat1290_led_brightness_set()
150 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_led_brightness_set()
151 led->movie_mode = false; in aat1290_led_brightness_set()
153 if (!led->movie_mode) { in aat1290_led_brightness_set()
154 aat1290_as2cwire_write(led, in aat1290_led_brightness_set()
157 led->movie_mode = true; in aat1290_led_brightness_set()
160 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR, in aat1290_led_brightness_set()
161 AAT1290_MAX_MM_CURR_PERCENT_0 - brightness); in aat1290_led_brightness_set()
162 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR, in aat1290_led_brightness_set()
166 mutex_unlock(&led->lock); in aat1290_led_brightness_set()
175 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); in aat1290_led_flash_strobe_set() local
176 struct led_classdev *led_cdev = &fled_cdev->led_cdev; in aat1290_led_flash_strobe_set()
177 struct led_flash_setting *timeout = &fled_cdev->timeout; in aat1290_led_flash_strobe_set()
179 mutex_lock(&led->lock); in aat1290_led_flash_strobe_set()
182 aat1290_set_flash_safety_timer(led, timeout->val); in aat1290_led_flash_strobe_set()
183 gpiod_direction_output(led->gpio_fl_en, 1); in aat1290_led_flash_strobe_set()
185 gpiod_direction_output(led->gpio_fl_en, 0); in aat1290_led_flash_strobe_set()
186 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_led_flash_strobe_set()
195 led_cdev->brightness = 0; in aat1290_led_flash_strobe_set()
196 led->movie_mode = false; in aat1290_led_flash_strobe_set()
198 mutex_unlock(&led->lock); in aat1290_led_flash_strobe_set()
207 * Don't do anything - flash timeout is cached in the led-class-flash in aat1290_led_flash_timeout_set()
215 static int aat1290_led_parse_dt(struct aat1290_led *led, in aat1290_led_parse_dt() argument
219 struct device *dev = &led->pdev->dev; in aat1290_led_parse_dt()
226 led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS); in aat1290_led_parse_dt()
227 if (IS_ERR(led->gpio_fl_en)) { in aat1290_led_parse_dt()
228 ret = PTR_ERR(led->gpio_fl_en); in aat1290_led_parse_dt()
233 led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS); in aat1290_led_parse_dt()
234 if (IS_ERR(led->gpio_en_set)) { in aat1290_led_parse_dt()
235 ret = PTR_ERR(led->gpio_en_set); in aat1290_led_parse_dt()
241 pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev); in aat1290_led_parse_dt()
243 cfg->has_external_strobe = false; in aat1290_led_parse_dt()
247 cfg->has_external_strobe = true; in aat1290_led_parse_dt()
253 dev_err(dev, "No DT child node found for connected LED.\n"); in aat1290_led_parse_dt()
254 return -EINVAL; in aat1290_led_parse_dt()
257 ret = of_property_read_u32(child_node, "led-max-microamp", in aat1290_led_parse_dt()
258 &cfg->max_mm_current); in aat1290_led_parse_dt()
260 * led-max-microamp will default to 1/20 of flash-max-microamp in aat1290_led_parse_dt()
265 "led-max-microamp DT property missing\n"); in aat1290_led_parse_dt()
267 ret = of_property_read_u32(child_node, "flash-max-microamp", in aat1290_led_parse_dt()
268 &cfg->max_flash_current); in aat1290_led_parse_dt()
271 "flash-max-microamp DT property missing\n"); in aat1290_led_parse_dt()
275 ret = of_property_read_u32(child_node, "flash-max-timeout-us", in aat1290_led_parse_dt()
276 &cfg->max_flash_tm); in aat1290_led_parse_dt()
279 "flash-max-timeout-us DT property missing\n"); in aat1290_led_parse_dt()
291 static void aat1290_led_validate_mm_current(struct aat1290_led *led, in aat1290_led_validate_mm_current() argument
296 while (e - b > 1) { in aat1290_led_validate_mm_current()
297 i = b + (e - b) / 2; in aat1290_led_validate_mm_current()
298 if (cfg->max_mm_current < led->mm_current_scale[i]) in aat1290_led_validate_mm_current()
304 cfg->max_mm_current = led->mm_current_scale[b]; in aat1290_led_validate_mm_current()
305 cfg->max_brightness = b + 1; in aat1290_led_validate_mm_current()
308 static int init_mm_current_scale(struct aat1290_led *led, in init_mm_current_scale() argument
316 AAT1290_MAX_MM_CURRENT(cfg->max_flash_current); in init_mm_current_scale()
318 led->mm_current_scale = devm_kzalloc(&led->pdev->dev, in init_mm_current_scale()
321 if (!led->mm_current_scale) in init_mm_current_scale()
322 return -ENOMEM; in init_mm_current_scale()
325 led->mm_current_scale[i] = max_mm_current * in init_mm_current_scale()
331 static int aat1290_led_get_configuration(struct aat1290_led *led, in aat1290_led_get_configuration() argument
337 ret = aat1290_led_parse_dt(led, cfg, sub_node); in aat1290_led_get_configuration()
341 * Init non-linear movie mode current scale basing in aat1290_led_get_configuration()
342 * on the max flash current from led configuration. in aat1290_led_get_configuration()
344 ret = init_mm_current_scale(led, cfg); in aat1290_led_get_configuration()
348 aat1290_led_validate_mm_current(led, cfg); in aat1290_led_get_configuration()
352 devm_kfree(&led->pdev->dev, led->mm_current_scale); in aat1290_led_get_configuration()
358 static void aat1290_init_flash_timeout(struct aat1290_led *led, in aat1290_init_flash_timeout() argument
361 struct led_classdev_flash *fled_cdev = &led->fled_cdev; in aat1290_init_flash_timeout()
365 setting = &fled_cdev->timeout; in aat1290_init_flash_timeout()
366 setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS; in aat1290_init_flash_timeout()
367 setting->max = cfg->max_flash_tm; in aat1290_init_flash_timeout()
368 setting->step = setting->min; in aat1290_init_flash_timeout()
369 setting->val = setting->max; in aat1290_init_flash_timeout()
377 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in aat1290_intensity_to_brightness()
378 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); in aat1290_intensity_to_brightness() local
381 for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i) in aat1290_intensity_to_brightness()
382 if (intensity >= led->mm_current_scale[i]) in aat1290_intensity_to_brightness()
383 return i + 1; in aat1290_intensity_to_brightness()
385 return 1; in aat1290_intensity_to_brightness()
391 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in aat1290_brightness_to_intensity()
392 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); in aat1290_brightness_to_intensity() local
394 return led->mm_current_scale[brightness - 1]; in aat1290_brightness_to_intensity()
400 struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev); in aat1290_led_external_strobe_set() local
401 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in aat1290_led_external_strobe_set()
402 struct led_classdev *led_cdev = &fled_cdev->led_cdev; in aat1290_led_external_strobe_set()
405 gpiod_direction_output(led->gpio_fl_en, 0); in aat1290_led_external_strobe_set()
406 gpiod_direction_output(led->gpio_en_set, 0); in aat1290_led_external_strobe_set()
408 led->movie_mode = false; in aat1290_led_external_strobe_set()
409 led_cdev->brightness = 0; in aat1290_led_external_strobe_set()
411 pinctrl = devm_pinctrl_get_select(&led->pdev->dev, in aat1290_led_external_strobe_set()
414 dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n"); in aat1290_led_external_strobe_set()
421 static void aat1290_init_v4l2_flash_config(struct aat1290_led *led, in aat1290_init_v4l2_flash_config() argument
425 struct led_classdev *led_cdev = &led->fled_cdev.led_cdev; in aat1290_init_v4l2_flash_config()
428 strlcpy(v4l2_sd_cfg->dev_name, led_cdev->dev->kobj.name, in aat1290_init_v4l2_flash_config()
429 sizeof(v4l2_sd_cfg->dev_name)); in aat1290_init_v4l2_flash_config()
431 s = &v4l2_sd_cfg->intensity; in aat1290_init_v4l2_flash_config()
432 s->min = led->mm_current_scale[0]; in aat1290_init_v4l2_flash_config()
433 s->max = led_cfg->max_mm_current; in aat1290_init_v4l2_flash_config()
434 s->step = 1; in aat1290_init_v4l2_flash_config()
435 s->val = s->max; in aat1290_init_v4l2_flash_config()
437 v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe; in aat1290_init_v4l2_flash_config()
446 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led, in aat1290_init_v4l2_flash_config() argument
461 struct device *dev = &pdev->dev; in aat1290_led_probe()
463 struct aat1290_led *led; in aat1290_led_probe() local
471 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); in aat1290_led_probe()
472 if (!led) in aat1290_led_probe()
473 return -ENOMEM; in aat1290_led_probe()
475 led->pdev = pdev; in aat1290_led_probe()
476 platform_set_drvdata(pdev, led); in aat1290_led_probe()
478 fled_cdev = &led->fled_cdev; in aat1290_led_probe()
479 fled_cdev->ops = &flash_ops; in aat1290_led_probe()
480 led_cdev = &fled_cdev->led_cdev; in aat1290_led_probe()
482 ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node); in aat1290_led_probe()
486 mutex_init(&led->lock); in aat1290_led_probe()
488 /* Initialize LED Flash class device */ in aat1290_led_probe()
489 led_cdev->brightness_set_blocking = aat1290_led_brightness_set; in aat1290_led_probe()
490 led_cdev->max_brightness = led_cfg.max_brightness; in aat1290_led_probe()
491 led_cdev->flags |= LED_DEV_CAP_FLASH; in aat1290_led_probe()
493 aat1290_init_flash_timeout(led, &led_cfg); in aat1290_led_probe()
498 /* Register LED Flash class device */ in aat1290_led_probe()
499 ret = led_classdev_flash_register_ext(&pdev->dev, fled_cdev, in aat1290_led_probe()
504 aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg); in aat1290_led_probe()
507 led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node), in aat1290_led_probe()
510 if (IS_ERR(led->v4l2_flash)) { in aat1290_led_probe()
511 ret = PTR_ERR(led->v4l2_flash); in aat1290_led_probe()
520 mutex_destroy(&led->lock); in aat1290_led_probe()
527 struct aat1290_led *led = platform_get_drvdata(pdev); in aat1290_led_remove() local
529 v4l2_flash_release(led->v4l2_flash); in aat1290_led_remove()
530 led_classdev_flash_unregister(&led->fled_cdev); in aat1290_led_remove()
532 mutex_destroy(&led->lock); in aat1290_led_remove()