xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 31af04cd)
1 /*
2  * linux/drivers/leds-pwm.c
3  *
4  * simple PWM based LED control
5  *
6  * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
7  *
8  * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_platform.h>
19 #include <linux/leds.h>
20 #include <linux/err.h>
21 #include <linux/pwm.h>
22 #include <linux/leds_pwm.h>
23 #include <linux/slab.h>
24 
25 struct led_pwm_data {
26 	struct led_classdev	cdev;
27 	struct pwm_device	*pwm;
28 	unsigned int		active_low;
29 	unsigned int		period;
30 	int			duty;
31 };
32 
33 struct led_pwm_priv {
34 	int num_leds;
35 	struct led_pwm_data leds[0];
36 };
37 
38 static void __led_pwm_set(struct led_pwm_data *led_dat)
39 {
40 	int new_duty = led_dat->duty;
41 
42 	pwm_config(led_dat->pwm, new_duty, led_dat->period);
43 
44 	if (new_duty == 0)
45 		pwm_disable(led_dat->pwm);
46 	else
47 		pwm_enable(led_dat->pwm);
48 }
49 
50 static int led_pwm_set(struct led_classdev *led_cdev,
51 		       enum led_brightness brightness)
52 {
53 	struct led_pwm_data *led_dat =
54 		container_of(led_cdev, struct led_pwm_data, cdev);
55 	unsigned int max = led_dat->cdev.max_brightness;
56 	unsigned long long duty =  led_dat->period;
57 
58 	duty *= brightness;
59 	do_div(duty, max);
60 
61 	if (led_dat->active_low)
62 		duty = led_dat->period - duty;
63 
64 	led_dat->duty = duty;
65 
66 	__led_pwm_set(led_dat);
67 
68 	return 0;
69 }
70 
71 static inline size_t sizeof_pwm_leds_priv(int num_leds)
72 {
73 	return sizeof(struct led_pwm_priv) +
74 		      (sizeof(struct led_pwm_data) * num_leds);
75 }
76 
77 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
78 		       struct led_pwm *led, struct device_node *child)
79 {
80 	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
81 	struct pwm_args pargs;
82 	int ret;
83 
84 	led_data->active_low = led->active_low;
85 	led_data->cdev.name = led->name;
86 	led_data->cdev.default_trigger = led->default_trigger;
87 	led_data->cdev.brightness = LED_OFF;
88 	led_data->cdev.max_brightness = led->max_brightness;
89 	led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
90 
91 	if (child)
92 		led_data->pwm = devm_of_pwm_get(dev, child, NULL);
93 	else
94 		led_data->pwm = devm_pwm_get(dev, led->name);
95 	if (IS_ERR(led_data->pwm)) {
96 		ret = PTR_ERR(led_data->pwm);
97 		if (ret != -EPROBE_DEFER)
98 			dev_err(dev, "unable to request PWM for %s: %d\n",
99 				led->name, ret);
100 		return ret;
101 	}
102 
103 	led_data->cdev.brightness_set_blocking = led_pwm_set;
104 
105 	/*
106 	 * FIXME: pwm_apply_args() should be removed when switching to the
107 	 * atomic PWM API.
108 	 */
109 	pwm_apply_args(led_data->pwm);
110 
111 	pwm_get_args(led_data->pwm, &pargs);
112 
113 	led_data->period = pargs.period;
114 	if (!led_data->period && (led->pwm_period_ns > 0))
115 		led_data->period = led->pwm_period_ns;
116 
117 	ret = devm_of_led_classdev_register(dev, child, &led_data->cdev);
118 	if (ret == 0) {
119 		priv->num_leds++;
120 		led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
121 	} else {
122 		dev_err(dev, "failed to register PWM led for %s: %d\n",
123 			led->name, ret);
124 	}
125 
126 	return ret;
127 }
128 
129 static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
130 {
131 	struct device_node *child;
132 	struct led_pwm led;
133 	int ret = 0;
134 
135 	memset(&led, 0, sizeof(led));
136 
137 	for_each_child_of_node(dev->of_node, child) {
138 		led.name = of_get_property(child, "label", NULL) ? :
139 			   child->name;
140 
141 		led.default_trigger = of_get_property(child,
142 						"linux,default-trigger", NULL);
143 		led.active_low = of_property_read_bool(child, "active-low");
144 		of_property_read_u32(child, "max-brightness",
145 				     &led.max_brightness);
146 
147 		ret = led_pwm_add(dev, priv, &led, child);
148 		if (ret) {
149 			of_node_put(child);
150 			break;
151 		}
152 	}
153 
154 	return ret;
155 }
156 
157 static int led_pwm_probe(struct platform_device *pdev)
158 {
159 	struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
160 	struct led_pwm_priv *priv;
161 	int count, i;
162 	int ret = 0;
163 
164 	if (pdata)
165 		count = pdata->num_leds;
166 	else
167 		count = of_get_child_count(pdev->dev.of_node);
168 
169 	if (!count)
170 		return -EINVAL;
171 
172 	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
173 			    GFP_KERNEL);
174 	if (!priv)
175 		return -ENOMEM;
176 
177 	if (pdata) {
178 		for (i = 0; i < count; i++) {
179 			ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
180 					  NULL);
181 			if (ret)
182 				break;
183 		}
184 	} else {
185 		ret = led_pwm_create_of(&pdev->dev, priv);
186 	}
187 
188 	if (ret)
189 		return ret;
190 
191 	platform_set_drvdata(pdev, priv);
192 
193 	return 0;
194 }
195 
196 static const struct of_device_id of_pwm_leds_match[] = {
197 	{ .compatible = "pwm-leds", },
198 	{},
199 };
200 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
201 
202 static struct platform_driver led_pwm_driver = {
203 	.probe		= led_pwm_probe,
204 	.driver		= {
205 		.name	= "leds_pwm",
206 		.of_match_table = of_pwm_leds_match,
207 	},
208 };
209 
210 module_platform_driver(led_pwm_driver);
211 
212 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
213 MODULE_DESCRIPTION("generic PWM LED driver");
214 MODULE_LICENSE("GPL v2");
215 MODULE_ALIAS("platform:leds-pwm");
216