Merge tag 'pwm/for-5.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[sfrench/cifs-2.6.git] / drivers / leds / leds-pwm.c
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 fwnode_handle *fwnode)
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 (fwnode)
89                 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, 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, to_of_node(fwnode),
115                                             &led_data->cdev);
116         if (ret == 0) {
117                 priv->num_leds++;
118                 led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
119         } else {
120                 dev_err(dev, "failed to register PWM led for %s: %d\n",
121                         led->name, ret);
122         }
123
124         return ret;
125 }
126
127 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
128 {
129         struct fwnode_handle *fwnode;
130         struct led_pwm led;
131         int ret = 0;
132
133         memset(&led, 0, sizeof(led));
134
135         device_for_each_child_node(dev, fwnode) {
136                 ret = fwnode_property_read_string(fwnode, "label", &led.name);
137                 if (ret && is_of_node(fwnode))
138                         led.name = to_of_node(fwnode)->name;
139
140                 if (!led.name) {
141                         fwnode_handle_put(fwnode);
142                         return -EINVAL;
143                 }
144
145                 fwnode_property_read_string(fwnode, "linux,default-trigger",
146                                             &led.default_trigger);
147
148                 led.active_low = fwnode_property_read_bool(fwnode,
149                                                            "active-low");
150                 fwnode_property_read_u32(fwnode, "max-brightness",
151                                          &led.max_brightness);
152
153                 ret = led_pwm_add(dev, priv, &led, fwnode);
154                 if (ret) {
155                         fwnode_handle_put(fwnode);
156                         break;
157                 }
158         }
159
160         return ret;
161 }
162
163 static int led_pwm_probe(struct platform_device *pdev)
164 {
165         struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
166         struct led_pwm_priv *priv;
167         int count, i;
168         int ret = 0;
169
170         if (pdata)
171                 count = pdata->num_leds;
172         else
173                 count = device_get_child_node_count(&pdev->dev);
174
175         if (!count)
176                 return -EINVAL;
177
178         priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
179                             GFP_KERNEL);
180         if (!priv)
181                 return -ENOMEM;
182
183         if (pdata) {
184                 for (i = 0; i < count; i++) {
185                         ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
186                                           NULL);
187                         if (ret)
188                                 break;
189                 }
190         } else {
191                 ret = led_pwm_create_fwnode(&pdev->dev, priv);
192         }
193
194         if (ret)
195                 return ret;
196
197         platform_set_drvdata(pdev, priv);
198
199         return 0;
200 }
201
202 static const struct of_device_id of_pwm_leds_match[] = {
203         { .compatible = "pwm-leds", },
204         {},
205 };
206 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
207
208 static struct platform_driver led_pwm_driver = {
209         .probe          = led_pwm_probe,
210         .driver         = {
211                 .name   = "leds_pwm",
212                 .of_match_table = of_pwm_leds_match,
213         },
214 };
215
216 module_platform_driver(led_pwm_driver);
217
218 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
219 MODULE_DESCRIPTION("generic PWM LED driver");
220 MODULE_LICENSE("GPL v2");
221 MODULE_ALIAS("platform:leds-pwm");