Lines Matching +full:led +full:- +full:8

1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED driver : leds-ktd2692.c
12 #include <linux/led-class-flash.h>
22 #define KTD2692_MM_MIN_CURR_THRESHOLD_SCALE 8
25 #define KTD2692_FLASH_MODE_TIMEOUT_LEVELS 8
64 /* maximum LED current in movie mode */
66 /* maximum LED current in flash mode */
70 /* max LED brightness level */
75 /* Related LED Flash class device */
95 static void ktd2692_expresswire_start(struct ktd2692_context *led) in ktd2692_expresswire_start() argument
97 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); in ktd2692_expresswire_start()
101 static void ktd2692_expresswire_reset(struct ktd2692_context *led) in ktd2692_expresswire_reset() argument
103 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); in ktd2692_expresswire_reset()
107 static void ktd2692_expresswire_end(struct ktd2692_context *led) in ktd2692_expresswire_end() argument
109 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); in ktd2692_expresswire_end()
111 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); in ktd2692_expresswire_end()
115 static void ktd2692_expresswire_set_bit(struct ktd2692_context *led, bool bit) in ktd2692_expresswire_set_bit() argument
134 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); in ktd2692_expresswire_set_bit()
136 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); in ktd2692_expresswire_set_bit()
139 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); in ktd2692_expresswire_set_bit()
141 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); in ktd2692_expresswire_set_bit()
146 static void ktd2692_expresswire_write(struct ktd2692_context *led, u8 value) in ktd2692_expresswire_write() argument
150 ktd2692_expresswire_start(led); in ktd2692_expresswire_write()
151 for (i = 7; i >= 0; i--) in ktd2692_expresswire_write()
152 ktd2692_expresswire_set_bit(led, value & BIT(i)); in ktd2692_expresswire_write()
153 ktd2692_expresswire_end(led); in ktd2692_expresswire_write()
160 struct ktd2692_context *led = fled_cdev_to_led(fled_cdev); in ktd2692_led_brightness_set() local
162 mutex_lock(&led->lock); in ktd2692_led_brightness_set()
165 led->mode = KTD2692_MODE_DISABLE; in ktd2692_led_brightness_set()
166 gpiod_direction_output(led->aux_gpio, KTD2692_LOW); in ktd2692_led_brightness_set()
168 ktd2692_expresswire_write(led, brightness | in ktd2692_led_brightness_set()
170 led->mode = KTD2692_MODE_MOVIE; in ktd2692_led_brightness_set()
173 ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE); in ktd2692_led_brightness_set()
174 mutex_unlock(&led->lock); in ktd2692_led_brightness_set()
182 struct ktd2692_context *led = fled_cdev_to_led(fled_cdev); in ktd2692_led_flash_strobe_set() local
183 struct led_flash_setting *timeout = &fled_cdev->timeout; in ktd2692_led_flash_strobe_set()
186 mutex_lock(&led->lock); in ktd2692_led_flash_strobe_set()
189 flash_tm_reg = GET_TIMEOUT_OFFSET(timeout->val, timeout->step); in ktd2692_led_flash_strobe_set()
190 ktd2692_expresswire_write(led, flash_tm_reg in ktd2692_led_flash_strobe_set()
193 led->mode = KTD2692_MODE_FLASH; in ktd2692_led_flash_strobe_set()
194 gpiod_direction_output(led->aux_gpio, KTD2692_HIGH); in ktd2692_led_flash_strobe_set()
196 led->mode = KTD2692_MODE_DISABLE; in ktd2692_led_flash_strobe_set()
197 gpiod_direction_output(led->aux_gpio, KTD2692_LOW); in ktd2692_led_flash_strobe_set()
200 ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE); in ktd2692_led_flash_strobe_set()
202 fled_cdev->led_cdev.brightness = LED_OFF; in ktd2692_led_flash_strobe_set()
203 led->mode = KTD2692_MODE_DISABLE; in ktd2692_led_flash_strobe_set()
205 mutex_unlock(&led->lock); in ktd2692_led_flash_strobe_set()
222 step = KTD2692_MM_TO_FL_RATIO(cfg->flash_max_microamp) in ktd2692_init_movie_current_max()
227 offset--; in ktd2692_init_movie_current_max()
228 } while ((movie_current_microamp > cfg->movie_max_microamp) && in ktd2692_init_movie_current_max()
231 cfg->max_brightness = offset; in ktd2692_init_movie_current_max()
239 setting = &fled_cdev->timeout; in ktd2692_init_flash_timeout()
240 setting->min = KTD2692_FLASH_MODE_TIMEOUT_DISABLE; in ktd2692_init_flash_timeout()
241 setting->max = cfg->flash_max_timeout; in ktd2692_init_flash_timeout()
242 setting->step = cfg->flash_max_timeout in ktd2692_init_flash_timeout()
243 / (KTD2692_FLASH_MODE_TIMEOUT_LEVELS - 1); in ktd2692_init_flash_timeout()
244 setting->val = cfg->flash_max_timeout; in ktd2692_init_flash_timeout()
247 static void ktd2692_setup(struct ktd2692_context *led) in ktd2692_setup() argument
249 led->mode = KTD2692_MODE_DISABLE; in ktd2692_setup()
250 ktd2692_expresswire_reset(led); in ktd2692_setup()
251 gpiod_direction_output(led->aux_gpio, KTD2692_LOW); in ktd2692_setup()
253 ktd2692_expresswire_write(led, (KTD2692_MM_MIN_CURR_THRESHOLD_SCALE - 1) in ktd2692_setup()
255 ktd2692_expresswire_write(led, KTD2692_FLASH_MODE_CURR_PERCENT(45) in ktd2692_setup()
259 static int ktd2692_parse_dt(struct ktd2692_context *led, struct device *dev, in ktd2692_parse_dt() argument
267 return -ENXIO; in ktd2692_parse_dt()
269 led->ctrl_gpio = devm_gpiod_get(dev, "ctrl", GPIOD_ASIS); in ktd2692_parse_dt()
270 ret = PTR_ERR_OR_ZERO(led->ctrl_gpio); in ktd2692_parse_dt()
272 dev_err(dev, "cannot get ctrl-gpios %d\n", ret); in ktd2692_parse_dt()
276 led->aux_gpio = devm_gpiod_get(dev, "aux", GPIOD_ASIS); in ktd2692_parse_dt()
277 ret = PTR_ERR_OR_ZERO(led->aux_gpio); in ktd2692_parse_dt()
279 dev_err(dev, "cannot get aux-gpios %d\n", ret); in ktd2692_parse_dt()
283 led->regulator = devm_regulator_get(dev, "vin"); in ktd2692_parse_dt()
284 if (IS_ERR(led->regulator)) in ktd2692_parse_dt()
285 led->regulator = NULL; in ktd2692_parse_dt()
287 if (led->regulator) { in ktd2692_parse_dt()
288 ret = regulator_enable(led->regulator); in ktd2692_parse_dt()
295 dev_err(dev, "No DT child node found for connected LED.\n"); in ktd2692_parse_dt()
296 return -EINVAL; in ktd2692_parse_dt()
299 led->fled_cdev.led_cdev.name = in ktd2692_parse_dt()
300 of_get_property(child_node, "label", NULL) ? : child_node->name; in ktd2692_parse_dt()
302 ret = of_property_read_u32(child_node, "led-max-microamp", in ktd2692_parse_dt()
303 &cfg->movie_max_microamp); in ktd2692_parse_dt()
305 dev_err(dev, "failed to parse led-max-microamp\n"); in ktd2692_parse_dt()
309 ret = of_property_read_u32(child_node, "flash-max-microamp", in ktd2692_parse_dt()
310 &cfg->flash_max_microamp); in ktd2692_parse_dt()
312 dev_err(dev, "failed to parse flash-max-microamp\n"); in ktd2692_parse_dt()
316 ret = of_property_read_u32(child_node, "flash-max-timeout-us", in ktd2692_parse_dt()
317 &cfg->flash_max_timeout); in ktd2692_parse_dt()
319 dev_err(dev, "failed to parse flash-max-timeout-us\n"); in ktd2692_parse_dt()
335 struct ktd2692_context *led; in ktd2692_probe() local
341 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); in ktd2692_probe()
342 if (!led) in ktd2692_probe()
343 return -ENOMEM; in ktd2692_probe()
345 fled_cdev = &led->fled_cdev; in ktd2692_probe()
346 led_cdev = &fled_cdev->led_cdev; in ktd2692_probe()
348 ret = ktd2692_parse_dt(led, &pdev->dev, &led_cfg); in ktd2692_probe()
355 fled_cdev->ops = &flash_ops; in ktd2692_probe()
357 led_cdev->max_brightness = led_cfg.max_brightness; in ktd2692_probe()
358 led_cdev->brightness_set_blocking = ktd2692_led_brightness_set; in ktd2692_probe()
359 led_cdev->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH; in ktd2692_probe()
361 mutex_init(&led->lock); in ktd2692_probe()
363 platform_set_drvdata(pdev, led); in ktd2692_probe()
365 ret = led_classdev_flash_register(&pdev->dev, fled_cdev); in ktd2692_probe()
367 dev_err(&pdev->dev, "can't register LED %s\n", led_cdev->name); in ktd2692_probe()
368 mutex_destroy(&led->lock); in ktd2692_probe()
372 ktd2692_setup(led); in ktd2692_probe()
379 struct ktd2692_context *led = platform_get_drvdata(pdev); in ktd2692_remove() local
382 led_classdev_flash_unregister(&led->fled_cdev); in ktd2692_remove()
384 if (led->regulator) { in ktd2692_remove()
385 ret = regulator_disable(led->regulator); in ktd2692_remove()
387 dev_err(&pdev->dev, in ktd2692_remove()
391 mutex_destroy(&led->lock); in ktd2692_remove()
414 MODULE_DESCRIPTION("Kinetic KTD2692 LED driver");