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