xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 9a69abf8)
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/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_platform.h>
20 #include <linux/fb.h>
21 #include <linux/leds.h>
22 #include <linux/err.h>
23 #include <linux/pwm.h>
24 #include <linux/leds_pwm.h>
25 #include <linux/slab.h>
26 
27 struct led_pwm_data {
28 	struct led_classdev	cdev;
29 	struct pwm_device	*pwm;
30 	unsigned int		active_low;
31 	unsigned int		period;
32 };
33 
34 struct led_pwm_priv {
35 	int num_leds;
36 	struct led_pwm_data leds[0];
37 };
38 
39 static void led_pwm_set(struct led_classdev *led_cdev,
40 	enum led_brightness brightness)
41 {
42 	struct led_pwm_data *led_dat =
43 		container_of(led_cdev, struct led_pwm_data, cdev);
44 	unsigned int max = led_dat->cdev.max_brightness;
45 	unsigned int period =  led_dat->period;
46 
47 	if (brightness == 0) {
48 		pwm_config(led_dat->pwm, 0, period);
49 		pwm_disable(led_dat->pwm);
50 	} else {
51 		pwm_config(led_dat->pwm, brightness * period / max, period);
52 		pwm_enable(led_dat->pwm);
53 	}
54 }
55 
56 static inline size_t sizeof_pwm_leds_priv(int num_leds)
57 {
58 	return sizeof(struct led_pwm_priv) +
59 		      (sizeof(struct led_pwm_data) * num_leds);
60 }
61 
62 static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
63 {
64 	struct device_node *node = pdev->dev.of_node;
65 	struct device_node *child;
66 	struct led_pwm_priv *priv;
67 	int count, ret;
68 
69 	/* count LEDs in this device, so we know how much to allocate */
70 	count = of_get_child_count(node);
71 	if (!count)
72 		return NULL;
73 
74 	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
75 			    GFP_KERNEL);
76 	if (!priv)
77 		return NULL;
78 
79 	for_each_child_of_node(node, child) {
80 		struct led_pwm_data *led_dat = &priv->leds[priv->num_leds];
81 
82 		led_dat->cdev.name = of_get_property(child, "label",
83 						     NULL) ? : child->name;
84 
85 		led_dat->pwm = devm_of_pwm_get(&pdev->dev, child, NULL);
86 		if (IS_ERR(led_dat->pwm)) {
87 			dev_err(&pdev->dev, "unable to request PWM for %s\n",
88 				led_dat->cdev.name);
89 			goto err;
90 		}
91 		/* Get the period from PWM core when n*/
92 		led_dat->period = pwm_get_period(led_dat->pwm);
93 
94 		led_dat->cdev.default_trigger = of_get_property(child,
95 						"linux,default-trigger", NULL);
96 		of_property_read_u32(child, "max-brightness",
97 				     &led_dat->cdev.max_brightness);
98 
99 		led_dat->cdev.brightness_set = led_pwm_set;
100 		led_dat->cdev.brightness = LED_OFF;
101 		led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
102 
103 		ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
104 		if (ret < 0) {
105 			dev_err(&pdev->dev, "failed to register for %s\n",
106 				led_dat->cdev.name);
107 			of_node_put(child);
108 			goto err;
109 		}
110 		priv->num_leds++;
111 	}
112 
113 	return priv;
114 err:
115 	while (priv->num_leds--)
116 		led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
117 
118 	return NULL;
119 }
120 
121 static int led_pwm_probe(struct platform_device *pdev)
122 {
123 	struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
124 	struct led_pwm_priv *priv;
125 	int i, ret = 0;
126 
127 	if (pdata && pdata->num_leds) {
128 		priv = devm_kzalloc(&pdev->dev,
129 				    sizeof_pwm_leds_priv(pdata->num_leds),
130 				    GFP_KERNEL);
131 		if (!priv)
132 			return -ENOMEM;
133 
134 		for (i = 0; i < pdata->num_leds; i++) {
135 			struct led_pwm *cur_led = &pdata->leds[i];
136 			struct led_pwm_data *led_dat = &priv->leds[i];
137 
138 			led_dat->pwm = devm_pwm_get(&pdev->dev, cur_led->name);
139 			if (IS_ERR(led_dat->pwm)) {
140 				ret = PTR_ERR(led_dat->pwm);
141 				dev_err(&pdev->dev,
142 					"unable to request PWM for %s\n",
143 					cur_led->name);
144 				goto err;
145 			}
146 
147 			led_dat->cdev.name = cur_led->name;
148 			led_dat->cdev.default_trigger = cur_led->default_trigger;
149 			led_dat->active_low = cur_led->active_low;
150 			led_dat->period = cur_led->pwm_period_ns;
151 			led_dat->cdev.brightness_set = led_pwm_set;
152 			led_dat->cdev.brightness = LED_OFF;
153 			led_dat->cdev.max_brightness = cur_led->max_brightness;
154 			led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
155 
156 			ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
157 			if (ret < 0)
158 				goto err;
159 		}
160 		priv->num_leds = pdata->num_leds;
161 	} else {
162 		priv = led_pwm_create_of(pdev);
163 		if (!priv)
164 			return -ENODEV;
165 	}
166 
167 	platform_set_drvdata(pdev, priv);
168 
169 	return 0;
170 
171 err:
172 	while (i--)
173 		led_classdev_unregister(&priv->leds[i].cdev);
174 
175 	return ret;
176 }
177 
178 static int led_pwm_remove(struct platform_device *pdev)
179 {
180 	struct led_pwm_priv *priv = platform_get_drvdata(pdev);
181 	int i;
182 
183 	for (i = 0; i < priv->num_leds; i++)
184 		led_classdev_unregister(&priv->leds[i].cdev);
185 
186 	return 0;
187 }
188 
189 static const struct of_device_id of_pwm_leds_match[] = {
190 	{ .compatible = "pwm-leds", },
191 	{},
192 };
193 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
194 
195 static struct platform_driver led_pwm_driver = {
196 	.probe		= led_pwm_probe,
197 	.remove		= led_pwm_remove,
198 	.driver		= {
199 		.name	= "leds_pwm",
200 		.owner	= THIS_MODULE,
201 		.of_match_table = of_match_ptr(of_pwm_leds_match),
202 	},
203 };
204 
205 module_platform_driver(led_pwm_driver);
206 
207 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
208 MODULE_DESCRIPTION("PWM LED driver for PXA");
209 MODULE_LICENSE("GPL");
210 MODULE_ALIAS("platform:leds-pwm");
211