Lines Matching +full:pwm +full:- +full:leds
1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/leds-pwm.c
5 * simple PWM based LED control
9 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
16 #include <linux/leds.h>
18 #include <linux/pwm.h>
20 #include "leds.h"
31 struct pwm_device *pwm; member
38 struct led_pwm_data leds[]; member
46 unsigned int max = led_dat->cdev.max_brightness; in led_pwm_set()
47 unsigned long long duty = led_dat->pwmstate.period; in led_pwm_set()
52 if (led_dat->active_low) in led_pwm_set()
53 duty = led_dat->pwmstate.period - duty; in led_pwm_set()
55 led_dat->pwmstate.duty_cycle = duty; in led_pwm_set()
56 led_dat->pwmstate.enabled = duty > 0; in led_pwm_set()
57 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate); in led_pwm_set()
64 struct led_pwm_data *led_data = &priv->leds[priv->num_leds]; in led_pwm_add()
68 led_data->active_low = led->active_low; in led_pwm_add()
69 led_data->cdev.name = led->name; in led_pwm_add()
70 led_data->cdev.brightness = LED_OFF; in led_pwm_add()
71 led_data->cdev.max_brightness = led->max_brightness; in led_pwm_add()
72 led_data->cdev.flags = LED_CORE_SUSPENDRESUME; in led_pwm_add()
74 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL); in led_pwm_add()
75 if (IS_ERR(led_data->pwm)) in led_pwm_add()
76 return dev_err_probe(dev, PTR_ERR(led_data->pwm), in led_pwm_add()
77 "unable to request PWM for %s\n", in led_pwm_add()
78 led->name); in led_pwm_add()
80 led_data->cdev.brightness_set_blocking = led_pwm_set; in led_pwm_add()
82 /* init PWM state */ in led_pwm_add()
83 switch (led->default_state) { in led_pwm_add()
85 pwm_get_state(led_data->pwm, &led_data->pwmstate); in led_pwm_add()
86 if (led_data->pwmstate.period) in led_pwm_add()
88 led->default_state = LEDS_DEFSTATE_OFF; in led_pwm_add()
91 led->name); in led_pwm_add()
94 pwm_init_state(led_data->pwm, &led_data->pwmstate); in led_pwm_add()
99 switch (led->default_state) { in led_pwm_add()
101 led_data->cdev.brightness = led->max_brightness; in led_pwm_add()
107 brightness = led->max_brightness; in led_pwm_add()
108 brightness *= led_data->pwmstate.duty_cycle; in led_pwm_add()
109 do_div(brightness, led_data->pwmstate.period); in led_pwm_add()
110 led_data->cdev.brightness = brightness; in led_pwm_add()
115 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); in led_pwm_add()
117 dev_err(dev, "failed to register PWM led for %s: %d\n", in led_pwm_add()
118 led->name, ret); in led_pwm_add()
122 if (led->default_state != LEDS_DEFSTATE_KEEP) { in led_pwm_add()
123 ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness); in led_pwm_add()
125 dev_err(dev, "failed to set led PWM value for %s: %d", in led_pwm_add()
126 led->name, ret); in led_pwm_add()
131 priv->num_leds++; in led_pwm_add()
146 led.name = to_of_node(fwnode)->name; in led_pwm_create_fwnode()
154 "active-low"); in led_pwm_create_fwnode()
155 fwnode_property_read_u32(fwnode, "max-brightness", in led_pwm_create_fwnode()
178 count = device_get_child_node_count(&pdev->dev); in led_pwm_probe()
181 return -EINVAL; in led_pwm_probe()
183 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count), in led_pwm_probe()
186 return -ENOMEM; in led_pwm_probe()
188 ret = led_pwm_create_fwnode(&pdev->dev, priv); in led_pwm_probe()
199 { .compatible = "pwm-leds", },
215 MODULE_DESCRIPTION("generic PWM LED driver");
217 MODULE_ALIAS("platform:leds-pwm");