blob: a1ea5f6a8d39c6c948f6545e48d1aee82b4130f3 [file] [log] [blame]
Luotao Fu41c42ff2009-02-11 13:24:40 -08001/*
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>
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080019#include <linux/of_platform.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080020#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080026
27struct led_pwm_data {
28 struct led_classdev cdev;
29 struct pwm_device *pwm;
Sachin Kamatdcba9102012-11-25 12:24:55 +053030 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080031 unsigned int period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080032};
33
Peter Ujfalusi0f868152012-12-21 01:43:56 -080034struct led_pwm_priv {
35 int num_leds;
36 struct led_pwm_data leds[0];
37};
38
Luotao Fu41c42ff2009-02-11 13:24:40 -080039static 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);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010044 unsigned int max = led_dat->cdev.max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080045 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
Peter Ujfalusi0f868152012-12-21 01:43:56 -080056static 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
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080062static 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;
114err:
115 while (priv->num_leds--)
116 led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
117
118 return NULL;
119}
120
Luotao Fu41c42ff2009-02-11 13:24:40 -0800121static int led_pwm_probe(struct platform_device *pdev)
122{
123 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800124 struct led_pwm_priv *priv;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800125 int i, ret = 0;
126
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800127 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;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800133
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800134 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];
Luotao Fu41c42ff2009-02-11 13:24:40 -0800137
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800138 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 }
Luotao Fu41c42ff2009-02-11 13:24:40 -0800146
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800147 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;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800159 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800160 priv->num_leds = pdata->num_leds;
161 } else {
162 priv = led_pwm_create_of(pdev);
163 if (!priv)
164 return -ENODEV;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800165 }
166
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800167 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800168
169 return 0;
170
171err:
Peter Ujfalusi8a66a572012-12-21 01:44:00 -0800172 while (i--)
173 led_classdev_unregister(&priv->leds[i].cdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800174
Luotao Fu41c42ff2009-02-11 13:24:40 -0800175 return ret;
176}
177
Bill Pemberton678e8a62012-11-19 13:26:00 -0500178static int led_pwm_remove(struct platform_device *pdev)
Luotao Fu41c42ff2009-02-11 13:24:40 -0800179{
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800180 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800181 int i;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800182
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800183 for (i = 0; i < priv->num_leds; i++)
184 led_classdev_unregister(&priv->leds[i].cdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800185
Luotao Fu41c42ff2009-02-11 13:24:40 -0800186 return 0;
187}
188
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800189static const struct of_device_id of_pwm_leds_match[] = {
190 { .compatible = "pwm-leds", },
191 {},
192};
193MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
194
Luotao Fu41c42ff2009-02-11 13:24:40 -0800195static struct platform_driver led_pwm_driver = {
196 .probe = led_pwm_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500197 .remove = led_pwm_remove,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800198 .driver = {
199 .name = "leds_pwm",
200 .owner = THIS_MODULE,
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800201 .of_match_table = of_match_ptr(of_pwm_leds_match),
Luotao Fu41c42ff2009-02-11 13:24:40 -0800202 },
203};
204
Axel Lin892a8842012-01-10 15:09:24 -0800205module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800206
207MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
208MODULE_DESCRIPTION("PWM LED driver for PXA");
209MODULE_LICENSE("GPL");
210MODULE_ALIAS("platform:leds-pwm");