Lines Matching +full:led +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED driver : leds-ktd2692.c
12 #include <linux/led-class-flash.h>
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
118 * The Low Bit(0) and High Bit(1) is based on a time detection in ktd2692_expresswire_set_bit()
122 * Time_(L_HB) : Low time of the High Bit(1) in ktd2692_expresswire_set_bit()
123 * Time_(H_HB) : High time of the High Bit(1) in ktd2692_expresswire_set_bit()
127 * High Bit(1) : 2 * Time_(L_HB) < Time_(H_HB) in ktd2692_expresswire_set_bit()
131 * [ Low Bit (0) ] [ High Bit(1) ] in ktd2692_expresswire_set_bit()
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()
262 struct ktd2692_context *led = dev_get_drvdata(dev); in regulator_disable_action() local
265 ret = regulator_disable(led->regulator); in regulator_disable_action()
270 static int ktd2692_parse_dt(struct ktd2692_context *led, struct device *dev, in ktd2692_parse_dt() argument
278 return -ENXIO; in ktd2692_parse_dt()
280 led->ctrl_gpio = devm_gpiod_get(dev, "ctrl", GPIOD_ASIS); in ktd2692_parse_dt()
281 ret = PTR_ERR_OR_ZERO(led->ctrl_gpio); in ktd2692_parse_dt()
283 return dev_err_probe(dev, ret, "cannot get ctrl-gpios\n"); in ktd2692_parse_dt()
285 led->aux_gpio = devm_gpiod_get_optional(dev, "aux", GPIOD_ASIS); in ktd2692_parse_dt()
286 if (IS_ERR(led->aux_gpio)) in ktd2692_parse_dt()
287 return dev_err_probe(dev, PTR_ERR(led->aux_gpio), "cannot get aux-gpios\n"); in ktd2692_parse_dt()
289 led->regulator = devm_regulator_get(dev, "vin"); in ktd2692_parse_dt()
290 if (IS_ERR(led->regulator)) in ktd2692_parse_dt()
291 led->regulator = NULL; in ktd2692_parse_dt()
293 if (led->regulator) { in ktd2692_parse_dt()
294 ret = regulator_enable(led->regulator); in ktd2692_parse_dt()
307 dev_err(dev, "No DT child node found for connected LED.\n"); in ktd2692_parse_dt()
308 return -EINVAL; in ktd2692_parse_dt()
311 led->fled_cdev.led_cdev.name = in ktd2692_parse_dt()
312 of_get_property(child_node, "label", NULL) ? : child_node->name; in ktd2692_parse_dt()
314 ret = of_property_read_u32(child_node, "led-max-microamp", in ktd2692_parse_dt()
315 &cfg->movie_max_microamp); in ktd2692_parse_dt()
317 dev_err(dev, "failed to parse led-max-microamp\n"); in ktd2692_parse_dt()
321 ret = of_property_read_u32(child_node, "flash-max-microamp", in ktd2692_parse_dt()
322 &cfg->flash_max_microamp); in ktd2692_parse_dt()
324 dev_err(dev, "failed to parse flash-max-microamp\n"); in ktd2692_parse_dt()
328 ret = of_property_read_u32(child_node, "flash-max-timeout-us", in ktd2692_parse_dt()
329 &cfg->flash_max_timeout); in ktd2692_parse_dt()
331 dev_err(dev, "failed to parse flash-max-timeout-us\n"); in ktd2692_parse_dt()
347 struct ktd2692_context *led; in ktd2692_probe() local
353 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); in ktd2692_probe()
354 if (!led) in ktd2692_probe()
355 return -ENOMEM; in ktd2692_probe()
357 fled_cdev = &led->fled_cdev; in ktd2692_probe()
358 led_cdev = &fled_cdev->led_cdev; in ktd2692_probe()
360 ret = ktd2692_parse_dt(led, &pdev->dev, &led_cfg); in ktd2692_probe()
367 fled_cdev->ops = &flash_ops; in ktd2692_probe()
369 led_cdev->max_brightness = led_cfg.max_brightness; in ktd2692_probe()
370 led_cdev->brightness_set_blocking = ktd2692_led_brightness_set; in ktd2692_probe()
371 led_cdev->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH; in ktd2692_probe()
373 mutex_init(&led->lock); in ktd2692_probe()
375 platform_set_drvdata(pdev, led); in ktd2692_probe()
377 ret = led_classdev_flash_register(&pdev->dev, fled_cdev); in ktd2692_probe()
379 dev_err(&pdev->dev, "can't register LED %s\n", led_cdev->name); in ktd2692_probe()
380 mutex_destroy(&led->lock); in ktd2692_probe()
384 ktd2692_setup(led); in ktd2692_probe()
391 struct ktd2692_context *led = platform_get_drvdata(pdev); in ktd2692_remove() local
393 led_classdev_flash_unregister(&led->fled_cdev); in ktd2692_remove()
395 mutex_destroy(&led->lock); in ktd2692_remove()
418 MODULE_DESCRIPTION("Kinetic KTD2692 LED driver");