1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/leds-pwm.c
5 * simple PWM based LED control
7 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
9 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
26 unsigned int max_brightness;
30 struct led_classdev cdev;
31 struct pwm_device *pwm;
32 struct pwm_state pwmstate;
33 unsigned int active_low;
38 struct led_pwm_data leds[];
41 static int led_pwm_set(struct led_classdev *led_cdev,
42 enum led_brightness brightness)
44 struct led_pwm_data *led_dat =
45 container_of(led_cdev, struct led_pwm_data, cdev);
46 unsigned int max = led_dat->cdev.max_brightness;
47 unsigned long long duty = led_dat->pwmstate.period;
52 if (led_dat->active_low)
53 duty = led_dat->pwmstate.period - duty;
55 led_dat->pwmstate.duty_cycle = duty;
56 led_dat->pwmstate.enabled = duty > 0;
57 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate);
60 __attribute__((nonnull))
61 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
62 struct led_pwm *led, struct fwnode_handle *fwnode)
64 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
65 struct led_init_data init_data = { .fwnode = fwnode };
68 led_data->active_low = led->active_low;
69 led_data->cdev.name = led->name;
70 led_data->cdev.brightness = LED_OFF;
71 led_data->cdev.max_brightness = led->max_brightness;
72 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
74 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
75 if (IS_ERR(led_data->pwm))
76 return dev_err_probe(dev, PTR_ERR(led_data->pwm),
77 "unable to request PWM for %s\n",
80 led_data->cdev.brightness_set_blocking = led_pwm_set;
83 switch (led->default_state) {
84 case LEDS_DEFSTATE_KEEP:
85 pwm_get_state(led_data->pwm, &led_data->pwmstate);
86 if (led_data->pwmstate.period)
88 led->default_state = LEDS_DEFSTATE_OFF;
90 "failed to read period for %s, default to off",
94 pwm_init_state(led_data->pwm, &led_data->pwmstate);
99 switch (led->default_state) {
100 case LEDS_DEFSTATE_ON:
101 led_data->cdev.brightness = led->max_brightness;
103 case LEDS_DEFSTATE_KEEP:
107 brightness = led->max_brightness;
108 brightness *= led_data->pwmstate.duty_cycle;
109 do_div(brightness, led_data->pwmstate.period);
110 led_data->cdev.brightness = brightness;
115 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
117 dev_err(dev, "failed to register PWM led for %s: %d\n",
122 if (led->default_state != LEDS_DEFSTATE_KEEP) {
123 ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
125 dev_err(dev, "failed to set led PWM value for %s: %d",
135 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
137 struct fwnode_handle *fwnode;
141 device_for_each_child_node(dev, fwnode) {
142 memset(&led, 0, sizeof(led));
144 ret = fwnode_property_read_string(fwnode, "label", &led.name);
145 if (ret && is_of_node(fwnode))
146 led.name = to_of_node(fwnode)->name;
153 led.active_low = fwnode_property_read_bool(fwnode,
155 fwnode_property_read_u32(fwnode, "max-brightness",
156 &led.max_brightness);
158 led.default_state = led_init_default_state_get(fwnode);
160 ret = led_pwm_add(dev, priv, &led, fwnode);
168 fwnode_handle_put(fwnode);
172 static int led_pwm_probe(struct platform_device *pdev)
174 struct led_pwm_priv *priv;
178 count = device_get_child_node_count(&pdev->dev);
183 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
188 ret = led_pwm_create_fwnode(&pdev->dev, priv);
193 platform_set_drvdata(pdev, priv);
198 static const struct of_device_id of_pwm_leds_match[] = {
199 { .compatible = "pwm-leds", },
202 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
204 static struct platform_driver led_pwm_driver = {
205 .probe = led_pwm_probe,
208 .of_match_table = of_pwm_leds_match,
212 module_platform_driver(led_pwm_driver);
214 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
215 MODULE_DESCRIPTION("generic PWM LED driver");
216 MODULE_LICENSE("GPL v2");
217 MODULE_ALIAS("platform:leds-pwm");