Merge 3.9-rc3 into staging-next
[linux/fpc-iii.git] / drivers / leds / leds-pwm.c
bloba1ea5f6a8d39c6c948f6545e48d1aee82b4130f3
1 /*
2 * linux/drivers/leds-pwm.c
4 * simple PWM based LED control
6 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
8 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
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.
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>
27 struct led_pwm_data {
28 struct led_classdev cdev;
29 struct pwm_device *pwm;
30 unsigned int active_low;
31 unsigned int period;
34 struct led_pwm_priv {
35 int num_leds;
36 struct led_pwm_data leds[0];
39 static void led_pwm_set(struct led_classdev *led_cdev,
40 enum led_brightness brightness)
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;
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);
56 static inline size_t sizeof_pwm_leds_priv(int num_leds)
58 return sizeof(struct led_pwm_priv) +
59 (sizeof(struct led_pwm_data) * num_leds);
62 static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
64 struct device_node *node = pdev->dev.of_node;
65 struct device_node *child;
66 struct led_pwm_priv *priv;
67 int count, ret;
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;
74 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
75 GFP_KERNEL);
76 if (!priv)
77 return NULL;
79 for_each_child_of_node(node, child) {
80 struct led_pwm_data *led_dat = &priv->leds[priv->num_leds];
82 led_dat->cdev.name = of_get_property(child, "label",
83 NULL) ? : child->name;
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;
91 /* Get the period from PWM core when n*/
92 led_dat->period = pwm_get_period(led_dat->pwm);
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);
99 led_dat->cdev.brightness_set = led_pwm_set;
100 led_dat->cdev.brightness = LED_OFF;
101 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
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;
110 priv->num_leds++;
113 return priv;
114 err:
115 while (priv->num_leds--)
116 led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
118 return NULL;
121 static int led_pwm_probe(struct platform_device *pdev)
123 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
124 struct led_pwm_priv *priv;
125 int i, ret = 0;
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;
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];
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;
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;
156 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
157 if (ret < 0)
158 goto err;
160 priv->num_leds = pdata->num_leds;
161 } else {
162 priv = led_pwm_create_of(pdev);
163 if (!priv)
164 return -ENODEV;
167 platform_set_drvdata(pdev, priv);
169 return 0;
171 err:
172 while (i--)
173 led_classdev_unregister(&priv->leds[i].cdev);
175 return ret;
178 static int led_pwm_remove(struct platform_device *pdev)
180 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
181 int i;
183 for (i = 0; i < priv->num_leds; i++)
184 led_classdev_unregister(&priv->leds[i].cdev);
186 return 0;
189 static const struct of_device_id of_pwm_leds_match[] = {
190 { .compatible = "pwm-leds", },
193 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
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),
205 module_platform_driver(led_pwm_driver);
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");