xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/drivers/leds-pwm.c
4  *
5  * simple PWM based LED control
6  *
7  * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
8  *
9  * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/of.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
20 #include "leds.h"
21 
22 struct led_pwm {
23 	const char	*name;
24 	u8		active_low;
25 	u8		default_state;
26 	unsigned int	max_brightness;
27 };
28 
29 struct led_pwm_data {
30 	struct led_classdev	cdev;
31 	struct pwm_device	*pwm;
32 	struct pwm_state	pwmstate;
33 	unsigned int		active_low;
34 };
35 
36 struct led_pwm_priv {
37 	int num_leds;
38 	struct led_pwm_data leds[];
39 };
40 
41 static int led_pwm_set(struct led_classdev *led_cdev,
42 		       enum led_brightness brightness)
43 {
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;
48 
49 	duty *= brightness;
50 	do_div(duty, max);
51 
52 	if (led_dat->active_low)
53 		duty = led_dat->pwmstate.period - duty;
54 
55 	led_dat->pwmstate.duty_cycle = duty;
56 	/*
57 	 * Disabling a PWM doesn't guarantee that it emits the inactive level.
58 	 * So keep it on. Only for suspending the PWM should be disabled because
59 	 * otherwise it refuses to suspend. The possible downside is that the
60 	 * LED might stay (or even go) on.
61 	 */
62 	led_dat->pwmstate.enabled = !(led_cdev->flags & LED_SUSPENDED);
63 	return pwm_apply_might_sleep(led_dat->pwm, &led_dat->pwmstate);
64 }
65 
66 __attribute__((nonnull))
67 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
68 		       struct led_pwm *led, struct fwnode_handle *fwnode)
69 {
70 	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
71 	struct led_init_data init_data = { .fwnode = fwnode };
72 	int ret;
73 
74 	led_data->active_low = led->active_low;
75 	led_data->cdev.name = led->name;
76 	led_data->cdev.brightness = LED_OFF;
77 	led_data->cdev.max_brightness = led->max_brightness;
78 	led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
79 
80 	led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
81 	if (IS_ERR(led_data->pwm))
82 		return dev_err_probe(dev, PTR_ERR(led_data->pwm),
83 				     "unable to request PWM for %s\n",
84 				     led->name);
85 
86 	led_data->cdev.brightness_set_blocking = led_pwm_set;
87 
88 	/* init PWM state */
89 	switch (led->default_state) {
90 	case LEDS_DEFSTATE_KEEP:
91 		pwm_get_state(led_data->pwm, &led_data->pwmstate);
92 		if (led_data->pwmstate.period)
93 			break;
94 		led->default_state = LEDS_DEFSTATE_OFF;
95 		dev_warn(dev,
96 			"failed to read period for %s, default to off",
97 			led->name);
98 		fallthrough;
99 	default:
100 		pwm_init_state(led_data->pwm, &led_data->pwmstate);
101 		break;
102 	}
103 
104 	/* set brightness */
105 	switch (led->default_state) {
106 	case LEDS_DEFSTATE_ON:
107 		led_data->cdev.brightness = led->max_brightness;
108 		break;
109 	case LEDS_DEFSTATE_KEEP:
110 		{
111 		uint64_t brightness;
112 
113 		brightness = led->max_brightness;
114 		brightness *= led_data->pwmstate.duty_cycle;
115 		do_div(brightness, led_data->pwmstate.period);
116 		led_data->cdev.brightness = brightness;
117 		}
118 		break;
119 	}
120 
121 	ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
122 	if (ret) {
123 		dev_err(dev, "failed to register PWM led for %s: %d\n",
124 			led->name, ret);
125 		return ret;
126 	}
127 
128 	if (led->default_state != LEDS_DEFSTATE_KEEP) {
129 		ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
130 		if (ret) {
131 			dev_err(dev, "failed to set led PWM value for %s: %d",
132 				led->name, ret);
133 			return ret;
134 		}
135 	}
136 
137 	priv->num_leds++;
138 	return 0;
139 }
140 
141 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
142 {
143 	struct fwnode_handle *fwnode;
144 	struct led_pwm led;
145 	int ret;
146 
147 	device_for_each_child_node(dev, fwnode) {
148 		memset(&led, 0, sizeof(led));
149 
150 		ret = fwnode_property_read_string(fwnode, "label", &led.name);
151 		if (ret && is_of_node(fwnode))
152 			led.name = to_of_node(fwnode)->name;
153 
154 		if (!led.name) {
155 			ret = -EINVAL;
156 			goto err_child_out;
157 		}
158 
159 		led.active_low = fwnode_property_read_bool(fwnode,
160 							   "active-low");
161 		fwnode_property_read_u32(fwnode, "max-brightness",
162 					 &led.max_brightness);
163 
164 		led.default_state = led_init_default_state_get(fwnode);
165 
166 		ret = led_pwm_add(dev, priv, &led, fwnode);
167 		if (ret)
168 			goto err_child_out;
169 	}
170 
171 	return 0;
172 
173 err_child_out:
174 	fwnode_handle_put(fwnode);
175 	return ret;
176 }
177 
178 static int led_pwm_probe(struct platform_device *pdev)
179 {
180 	struct led_pwm_priv *priv;
181 	int ret = 0;
182 	int count;
183 
184 	count = device_get_child_node_count(&pdev->dev);
185 
186 	if (!count)
187 		return -EINVAL;
188 
189 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
190 			    GFP_KERNEL);
191 	if (!priv)
192 		return -ENOMEM;
193 
194 	ret = led_pwm_create_fwnode(&pdev->dev, priv);
195 
196 	if (ret)
197 		return ret;
198 
199 	platform_set_drvdata(pdev, priv);
200 
201 	return 0;
202 }
203 
204 static const struct of_device_id of_pwm_leds_match[] = {
205 	{ .compatible = "pwm-leds", },
206 	{},
207 };
208 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
209 
210 static struct platform_driver led_pwm_driver = {
211 	.probe		= led_pwm_probe,
212 	.driver		= {
213 		.name	= "leds_pwm",
214 		.of_match_table = of_pwm_leds_match,
215 	},
216 };
217 
218 module_platform_driver(led_pwm_driver);
219 
220 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
221 MODULE_DESCRIPTION("generic PWM LED driver");
222 MODULE_LICENSE("GPL v2");
223 MODULE_ALIAS("platform:leds-pwm");
224