xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 22d55f02)
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 inline size_t sizeof_pwm_leds_priv(int num_leds)
69 {
70 	return sizeof(struct led_pwm_priv) +
71 		      (sizeof(struct led_pwm_data) * num_leds);
72 }
73 
74 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
75 		       struct led_pwm *led, struct device_node *child)
76 {
77 	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
78 	struct pwm_args pargs;
79 	int ret;
80 
81 	led_data->active_low = led->active_low;
82 	led_data->cdev.name = led->name;
83 	led_data->cdev.default_trigger = led->default_trigger;
84 	led_data->cdev.brightness = LED_OFF;
85 	led_data->cdev.max_brightness = led->max_brightness;
86 	led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
87 
88 	if (child)
89 		led_data->pwm = devm_of_pwm_get(dev, child, NULL);
90 	else
91 		led_data->pwm = devm_pwm_get(dev, led->name);
92 	if (IS_ERR(led_data->pwm)) {
93 		ret = PTR_ERR(led_data->pwm);
94 		if (ret != -EPROBE_DEFER)
95 			dev_err(dev, "unable to request PWM for %s: %d\n",
96 				led->name, ret);
97 		return ret;
98 	}
99 
100 	led_data->cdev.brightness_set_blocking = led_pwm_set;
101 
102 	/*
103 	 * FIXME: pwm_apply_args() should be removed when switching to the
104 	 * atomic PWM API.
105 	 */
106 	pwm_apply_args(led_data->pwm);
107 
108 	pwm_get_args(led_data->pwm, &pargs);
109 
110 	led_data->period = pargs.period;
111 	if (!led_data->period && (led->pwm_period_ns > 0))
112 		led_data->period = led->pwm_period_ns;
113 
114 	ret = devm_of_led_classdev_register(dev, child, &led_data->cdev);
115 	if (ret == 0) {
116 		priv->num_leds++;
117 		led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
118 	} else {
119 		dev_err(dev, "failed to register PWM led for %s: %d\n",
120 			led->name, ret);
121 	}
122 
123 	return ret;
124 }
125 
126 static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
127 {
128 	struct device_node *child;
129 	struct led_pwm led;
130 	int ret = 0;
131 
132 	memset(&led, 0, sizeof(led));
133 
134 	for_each_child_of_node(dev->of_node, child) {
135 		led.name = of_get_property(child, "label", NULL) ? :
136 			   child->name;
137 
138 		led.default_trigger = of_get_property(child,
139 						"linux,default-trigger", NULL);
140 		led.active_low = of_property_read_bool(child, "active-low");
141 		of_property_read_u32(child, "max-brightness",
142 				     &led.max_brightness);
143 
144 		ret = led_pwm_add(dev, priv, &led, child);
145 		if (ret) {
146 			of_node_put(child);
147 			break;
148 		}
149 	}
150 
151 	return ret;
152 }
153 
154 static int led_pwm_probe(struct platform_device *pdev)
155 {
156 	struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
157 	struct led_pwm_priv *priv;
158 	int count, i;
159 	int ret = 0;
160 
161 	if (pdata)
162 		count = pdata->num_leds;
163 	else
164 		count = of_get_child_count(pdev->dev.of_node);
165 
166 	if (!count)
167 		return -EINVAL;
168 
169 	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
170 			    GFP_KERNEL);
171 	if (!priv)
172 		return -ENOMEM;
173 
174 	if (pdata) {
175 		for (i = 0; i < count; i++) {
176 			ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
177 					  NULL);
178 			if (ret)
179 				break;
180 		}
181 	} else {
182 		ret = led_pwm_create_of(&pdev->dev, priv);
183 	}
184 
185 	if (ret)
186 		return ret;
187 
188 	platform_set_drvdata(pdev, priv);
189 
190 	return 0;
191 }
192 
193 static const struct of_device_id of_pwm_leds_match[] = {
194 	{ .compatible = "pwm-leds", },
195 	{},
196 };
197 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
198 
199 static struct platform_driver led_pwm_driver = {
200 	.probe		= led_pwm_probe,
201 	.driver		= {
202 		.name	= "leds_pwm",
203 		.of_match_table = of_pwm_leds_match,
204 	},
205 };
206 
207 module_platform_driver(led_pwm_driver);
208 
209 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
210 MODULE_DESCRIPTION("generic PWM LED driver");
211 MODULE_LICENSE("GPL v2");
212 MODULE_ALIAS("platform:leds-pwm");
213