xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision d9e32672)
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_platform.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/leds_pwm.h>
20 #include <linux/slab.h>
21 
22 struct led_pwm_data {
23 	struct led_classdev	cdev;
24 	struct pwm_device	*pwm;
25 	unsigned int		active_low;
26 	unsigned int		period;
27 	int			duty;
28 };
29 
30 struct led_pwm_priv {
31 	int num_leds;
32 	struct led_pwm_data leds[0];
33 };
34 
35 static void __led_pwm_set(struct led_pwm_data *led_dat)
36 {
37 	int new_duty = led_dat->duty;
38 
39 	pwm_config(led_dat->pwm, new_duty, led_dat->period);
40 
41 	if (new_duty == 0)
42 		pwm_disable(led_dat->pwm);
43 	else
44 		pwm_enable(led_dat->pwm);
45 }
46 
47 static int led_pwm_set(struct led_classdev *led_cdev,
48 		       enum led_brightness brightness)
49 {
50 	struct led_pwm_data *led_dat =
51 		container_of(led_cdev, struct led_pwm_data, cdev);
52 	unsigned int max = led_dat->cdev.max_brightness;
53 	unsigned long long duty =  led_dat->period;
54 
55 	duty *= brightness;
56 	do_div(duty, max);
57 
58 	if (led_dat->active_low)
59 		duty = led_dat->period - duty;
60 
61 	led_dat->duty = duty;
62 
63 	__led_pwm_set(led_dat);
64 
65 	return 0;
66 }
67 
68 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
69 		       struct led_pwm *led, struct fwnode_handle *fwnode)
70 {
71 	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
72 	struct pwm_args pargs;
73 	int ret;
74 
75 	led_data->active_low = led->active_low;
76 	led_data->cdev.name = led->name;
77 	led_data->cdev.default_trigger = led->default_trigger;
78 	led_data->cdev.brightness = LED_OFF;
79 	led_data->cdev.max_brightness = led->max_brightness;
80 	led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
81 
82 	if (fwnode)
83 		led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
84 	else
85 		led_data->pwm = devm_pwm_get(dev, led->name);
86 	if (IS_ERR(led_data->pwm)) {
87 		ret = PTR_ERR(led_data->pwm);
88 		if (ret != -EPROBE_DEFER)
89 			dev_err(dev, "unable to request PWM for %s: %d\n",
90 				led->name, ret);
91 		return ret;
92 	}
93 
94 	led_data->cdev.brightness_set_blocking = led_pwm_set;
95 
96 	/*
97 	 * FIXME: pwm_apply_args() should be removed when switching to the
98 	 * atomic PWM API.
99 	 */
100 	pwm_apply_args(led_data->pwm);
101 
102 	pwm_get_args(led_data->pwm, &pargs);
103 
104 	led_data->period = pargs.period;
105 	if (!led_data->period && (led->pwm_period_ns > 0))
106 		led_data->period = led->pwm_period_ns;
107 
108 	ret = devm_led_classdev_register(dev, &led_data->cdev);
109 	if (ret == 0) {
110 		priv->num_leds++;
111 		led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
112 	} else {
113 		dev_err(dev, "failed to register PWM led for %s: %d\n",
114 			led->name, ret);
115 	}
116 
117 	return ret;
118 }
119 
120 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
121 {
122 	struct fwnode_handle *fwnode;
123 	struct led_pwm led;
124 	int ret = 0;
125 
126 	memset(&led, 0, sizeof(led));
127 
128 	device_for_each_child_node(dev, fwnode) {
129 		ret = fwnode_property_read_string(fwnode, "label", &led.name);
130 		if (ret && is_of_node(fwnode))
131 			led.name = to_of_node(fwnode)->name;
132 
133 		if (!led.name) {
134 			fwnode_handle_put(fwnode);
135 			return -EINVAL;
136 		}
137 
138 		fwnode_property_read_string(fwnode, "linux,default-trigger",
139 					    &led.default_trigger);
140 
141 		led.active_low = fwnode_property_read_bool(fwnode,
142 							   "active-low");
143 		fwnode_property_read_u32(fwnode, "max-brightness",
144 					 &led.max_brightness);
145 
146 		ret = led_pwm_add(dev, priv, &led, fwnode);
147 		if (ret) {
148 			fwnode_handle_put(fwnode);
149 			break;
150 		}
151 	}
152 
153 	return ret;
154 }
155 
156 static int led_pwm_probe(struct platform_device *pdev)
157 {
158 	struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
159 	struct led_pwm_priv *priv;
160 	int count, i;
161 	int ret = 0;
162 
163 	if (pdata)
164 		count = pdata->num_leds;
165 	else
166 		count = device_get_child_node_count(&pdev->dev);
167 
168 	if (!count)
169 		return -EINVAL;
170 
171 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
172 			    GFP_KERNEL);
173 	if (!priv)
174 		return -ENOMEM;
175 
176 	if (pdata) {
177 		for (i = 0; i < count; i++) {
178 			ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
179 					  NULL);
180 			if (ret)
181 				break;
182 		}
183 	} else {
184 		ret = led_pwm_create_fwnode(&pdev->dev, priv);
185 	}
186 
187 	if (ret)
188 		return ret;
189 
190 	platform_set_drvdata(pdev, priv);
191 
192 	return 0;
193 }
194 
195 static const struct of_device_id of_pwm_leds_match[] = {
196 	{ .compatible = "pwm-leds", },
197 	{},
198 };
199 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
200 
201 static struct platform_driver led_pwm_driver = {
202 	.probe		= led_pwm_probe,
203 	.driver		= {
204 		.name	= "leds_pwm",
205 		.of_match_table = of_pwm_leds_match,
206 	},
207 };
208 
209 module_platform_driver(led_pwm_driver);
210 
211 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
212 MODULE_DESCRIPTION("generic PWM LED driver");
213 MODULE_LICENSE("GPL v2");
214 MODULE_ALIAS("platform:leds-pwm");
215