blob: df80c89ebe7fac93fce0b323aa101b6b56d2e5d9 [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>
Luotao Fu41c42ff2009-02-11 13:24:40 -080017#include <linux/platform_device.h>
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080018#include <linux/of_platform.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080019#include <linux/leds.h>
20#include <linux/err.h>
21#include <linux/pwm.h>
22#include <linux/leds_pwm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080024
25struct led_pwm_data {
26 struct led_classdev cdev;
27 struct pwm_device *pwm;
Sachin Kamatdcba9102012-11-25 12:24:55 +053028 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080029 unsigned int period;
Florian Vaussardc971ff12013-01-28 06:00:59 -080030 int duty;
Luotao Fu41c42ff2009-02-11 13:24:40 -080031};
32
Peter Ujfalusi0f868152012-12-21 01:43:56 -080033struct led_pwm_priv {
34 int num_leds;
35 struct led_pwm_data leds[0];
36};
37
Florian Vaussardc971ff12013-01-28 06:00:59 -080038static 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
Thierry Reding247bde12017-01-04 09:37:56 +010050static int led_pwm_set(struct led_classdev *led_cdev,
51 enum led_brightness brightness)
Luotao Fu41c42ff2009-02-11 13:24:40 -080052{
53 struct led_pwm_data *led_dat =
54 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010055 unsigned int max = led_dat->cdev.max_brightness;
Xiubo Lifc1aee02013-12-11 01:19:42 -080056 unsigned long long duty = led_dat->period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080057
Xiubo Lifc1aee02013-12-11 01:19:42 -080058 duty *= brightness;
59 do_div(duty, max);
Russell Kingd19a8a72014-04-06 15:20:18 -070060
61 if (led_dat->active_low)
62 duty = led_dat->period - duty;
63
Xiubo Lifc1aee02013-12-11 01:19:42 -080064 led_dat->duty = duty;
Florian Vaussardc971ff12013-01-28 06:00:59 -080065
Jacek Anaszewski9aa07622015-08-20 15:52:51 +020066 __led_pwm_set(led_dat);
Jacek Anaszewski9aa07622015-08-20 15:52:51 +020067
Jacek Anaszewski9aa07622015-08-20 15:52:51 +020068 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -080069}
70
Peter Ujfalusi0f868152012-12-21 01:43:56 -080071static 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
Russell King39236902014-04-06 15:20:03 -070077static void led_pwm_cleanup(struct led_pwm_priv *priv)
78{
Jacek Anaszewski9aa07622015-08-20 15:52:51 +020079 while (priv->num_leds--)
Russell King39236902014-04-06 15:20:03 -070080 led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
Russell King39236902014-04-06 15:20:03 -070081}
82
Russell King5f7b03d2014-04-06 15:20:08 -070083static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
Russell Kingb795e6d2014-04-06 15:20:13 -070084 struct led_pwm *led, struct device_node *child)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080085{
Russell King5f7b03d2014-04-06 15:20:08 -070086 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
Boris Brezillon1b506732016-04-14 21:17:26 +020087 struct pwm_args pargs;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -080088 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080089
Russell King5f7b03d2014-04-06 15:20:08 -070090 led_data->active_low = led->active_low;
Russell King5f7b03d2014-04-06 15:20:08 -070091 led_data->cdev.name = led->name;
92 led_data->cdev.default_trigger = led->default_trigger;
Russell King5f7b03d2014-04-06 15:20:08 -070093 led_data->cdev.brightness = LED_OFF;
94 led_data->cdev.max_brightness = led->max_brightness;
95 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080096
Russell Kingb795e6d2014-04-06 15:20:13 -070097 if (child)
98 led_data->pwm = devm_of_pwm_get(dev, child, NULL);
99 else
100 led_data->pwm = devm_pwm_get(dev, led->name);
Russell King5f7b03d2014-04-06 15:20:08 -0700101 if (IS_ERR(led_data->pwm)) {
102 ret = PTR_ERR(led_data->pwm);
103 dev_err(dev, "unable to request PWM for %s: %d\n",
104 led->name, ret);
105 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800106 }
107
Thierry Reding247bde12017-01-04 09:37:56 +0100108 led_data->cdev.brightness_set_blocking = led_pwm_set;
Russell King5f7b03d2014-04-06 15:20:08 -0700109
Boris Brezillon1b506732016-04-14 21:17:26 +0200110 /*
111 * FIXME: pwm_apply_args() should be removed when switching to the
112 * atomic PWM API.
113 */
114 pwm_apply_args(led_data->pwm);
115
116 pwm_get_args(led_data->pwm, &pargs);
117
118 led_data->period = pargs.period;
Linus Torvalds7c574cf2014-06-12 13:08:09 -0700119 if (!led_data->period && (led->pwm_period_ns > 0))
120 led_data->period = led->pwm_period_ns;
121
Russell King5f7b03d2014-04-06 15:20:08 -0700122 ret = led_classdev_register(dev, &led_data->cdev);
123 if (ret == 0) {
124 priv->num_leds++;
Markus Hofstaetterf1670332015-11-11 12:40:29 +0100125 led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
Russell King5f7b03d2014-04-06 15:20:08 -0700126 } else {
127 dev_err(dev, "failed to register PWM led for %s: %d\n",
128 led->name, ret);
129 }
130
131 return ret;
132}
133
Russell Kingb795e6d2014-04-06 15:20:13 -0700134static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800135{
136 struct device_node *child;
Russell Kingb795e6d2014-04-06 15:20:13 -0700137 struct led_pwm led;
138 int ret = 0;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800139
Russell Kingb795e6d2014-04-06 15:20:13 -0700140 memset(&led, 0, sizeof(led));
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800141
Russell Kingb795e6d2014-04-06 15:20:13 -0700142 for_each_child_of_node(dev->of_node, child) {
143 led.name = of_get_property(child, "label", NULL) ? :
144 child->name;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800145
Russell Kingb795e6d2014-04-06 15:20:13 -0700146 led.default_trigger = of_get_property(child,
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800147 "linux,default-trigger", NULL);
Russell Kingb0571e72014-04-06 15:20:24 -0700148 led.active_low = of_property_read_bool(child, "active-low");
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800149 of_property_read_u32(child, "max-brightness",
Russell Kingb795e6d2014-04-06 15:20:13 -0700150 &led.max_brightness);
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800151
Russell Kingb795e6d2014-04-06 15:20:13 -0700152 ret = led_pwm_add(dev, priv, &led, child);
153 if (ret) {
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800154 of_node_put(child);
Russell Kingb795e6d2014-04-06 15:20:13 -0700155 break;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800156 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800157 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800158
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800159 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800160}
161
Luotao Fu41c42ff2009-02-11 13:24:40 -0800162static int led_pwm_probe(struct platform_device *pdev)
163{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700164 struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800165 struct led_pwm_priv *priv;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800166 int count, i;
167 int ret = 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800168
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800169 if (pdata)
170 count = pdata->num_leds;
171 else
172 count = of_get_child_count(pdev->dev.of_node);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800173
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800174 if (!count)
175 return -EINVAL;
176
177 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
178 GFP_KERNEL);
179 if (!priv)
180 return -ENOMEM;
181
182 if (pdata) {
183 for (i = 0; i < count; i++) {
Russell Kingb795e6d2014-04-06 15:20:13 -0700184 ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
185 NULL);
Russell King5f7b03d2014-04-06 15:20:08 -0700186 if (ret)
187 break;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800188 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800189 } else {
Russell Kingb795e6d2014-04-06 15:20:13 -0700190 ret = led_pwm_create_of(&pdev->dev, priv);
Russell King5f7b03d2014-04-06 15:20:08 -0700191 }
192
193 if (ret) {
194 led_pwm_cleanup(priv);
195 return ret;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800196 }
197
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800198 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800199
200 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800201}
202
Bill Pemberton678e8a62012-11-19 13:26:00 -0500203static int led_pwm_remove(struct platform_device *pdev)
Luotao Fu41c42ff2009-02-11 13:24:40 -0800204{
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800205 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800206
Russell King39236902014-04-06 15:20:03 -0700207 led_pwm_cleanup(priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800208
Luotao Fu41c42ff2009-02-11 13:24:40 -0800209 return 0;
210}
211
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800212static const struct of_device_id of_pwm_leds_match[] = {
213 { .compatible = "pwm-leds", },
214 {},
215};
216MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
217
Luotao Fu41c42ff2009-02-11 13:24:40 -0800218static struct platform_driver led_pwm_driver = {
219 .probe = led_pwm_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500220 .remove = led_pwm_remove,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800221 .driver = {
222 .name = "leds_pwm",
Sachin Kamat1e08f722013-09-28 04:38:31 -0700223 .of_match_table = of_pwm_leds_match,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800224 },
225};
226
Axel Lin892a8842012-01-10 15:09:24 -0800227module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800228
229MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
Uwe Kleine-König49651c62013-11-25 21:43:45 +0100230MODULE_DESCRIPTION("generic PWM LED driver");
231MODULE_LICENSE("GPL v2");
Luotao Fu41c42ff2009-02-11 13:24:40 -0800232MODULE_ALIAS("platform:leds-pwm");