leds: lp55xx: fix the sysfs read operation
[linux-block.git] / drivers / leds / leds-pwm.c
CommitLineData
41c42ff5
LF
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>
08541cbc 19#include <linux/of_platform.h>
41c42ff5
LF
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>
5a0e3ad6 25#include <linux/slab.h>
41c42ff5
LF
26
27struct led_pwm_data {
28 struct led_classdev cdev;
29 struct pwm_device *pwm;
dcba9105 30 unsigned int active_low;
41c42ff5 31 unsigned int period;
41c42ff5
LF
32};
33
0f86815a
PU
34struct led_pwm_priv {
35 int num_leds;
36 struct led_pwm_data leds[0];
37};
38
41c42ff5
LF
39static 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);
e4590620 44 unsigned int max = led_dat->cdev.max_brightness;
41c42ff5
LF
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
0f86815a
PU
56static 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
08541cbc 62static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
41c42ff5 63{
08541cbc
PU
64 struct device_node *node = pdev->dev.of_node;
65 struct device_node *child;
0f86815a 66 struct led_pwm_priv *priv;
08541cbc 67 int count, ret;
41c42ff5 68
08541cbc
PU
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;
41c42ff5 73
08541cbc 74 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
0f86815a
PU
75 GFP_KERNEL);
76 if (!priv)
08541cbc 77 return NULL;
41c42ff5 78
08541cbc
PU
79 for_each_child_of_node(node, child) {
80 struct led_pwm_data *led_dat = &priv->leds[priv->num_leds];
41c42ff5 81
08541cbc
PU
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);
41c42ff5 86 if (IS_ERR(led_dat->pwm)) {
9ea6cdac 87 dev_err(&pdev->dev, "unable to request PWM for %s\n",
08541cbc 88 led_dat->cdev.name);
41c42ff5
LF
89 goto err;
90 }
08541cbc
PU
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);
41c42ff5 98
41c42ff5
LF
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);
08541cbc
PU
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);
41c42ff5 108 goto err;
08541cbc
PU
109 }
110 priv->num_leds++;
111 }
112
113 return priv;
114err:
115 while (priv->num_leds--)
116 led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
117
118 return NULL;
119}
120
121static 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;
41c42ff5
LF
165 }
166
0f86815a 167 platform_set_drvdata(pdev, priv);
41c42ff5
LF
168
169 return 0;
170
171err:
8a66a579
PU
172 while (i--)
173 led_classdev_unregister(&priv->leds[i].cdev);
41c42ff5 174
41c42ff5
LF
175 return ret;
176}
177
678e8a6b 178static int led_pwm_remove(struct platform_device *pdev)
41c42ff5 179{
0f86815a 180 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
41c42ff5 181 int i;
41c42ff5 182
0f86815a
PU
183 for (i = 0; i < priv->num_leds; i++)
184 led_classdev_unregister(&priv->leds[i].cdev);
41c42ff5 185
41c42ff5
LF
186 return 0;
187}
188
08541cbc
PU
189static const struct of_device_id of_pwm_leds_match[] = {
190 { .compatible = "pwm-leds", },
191 {},
192};
193MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
194
41c42ff5
LF
195static struct platform_driver led_pwm_driver = {
196 .probe = led_pwm_probe,
df07cf81 197 .remove = led_pwm_remove,
41c42ff5
LF
198 .driver = {
199 .name = "leds_pwm",
200 .owner = THIS_MODULE,
08541cbc 201 .of_match_table = of_match_ptr(of_pwm_leds_match),
41c42ff5
LF
202 },
203};
204
892a8843 205module_platform_driver(led_pwm_driver);
41c42ff5
LF
206
207MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
208MODULE_DESCRIPTION("PWM LED driver for PXA");
209MODULE_LICENSE("GPL");
210MODULE_ALIAS("platform:leds-pwm");