blob: fcb3e87a98871a8c8ec1eb19acdc7835c0ed6b63 [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 King5f7b03d2014-04-06 15:20:08 -070077static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
Russell Kingb795e6d2014-04-06 15:20:13 -070078 struct led_pwm *led, struct device_node *child)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080079{
Russell King5f7b03d2014-04-06 15:20:08 -070080 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
Boris Brezillon1b506732016-04-14 21:17:26 +020081 struct pwm_args pargs;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -080082 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080083
Russell King5f7b03d2014-04-06 15:20:08 -070084 led_data->active_low = led->active_low;
Russell King5f7b03d2014-04-06 15:20:08 -070085 led_data->cdev.name = led->name;
86 led_data->cdev.default_trigger = led->default_trigger;
Russell King5f7b03d2014-04-06 15:20:08 -070087 led_data->cdev.brightness = LED_OFF;
88 led_data->cdev.max_brightness = led->max_brightness;
89 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080090
Russell Kingb795e6d2014-04-06 15:20:13 -070091 if (child)
92 led_data->pwm = devm_of_pwm_get(dev, child, NULL);
93 else
94 led_data->pwm = devm_pwm_get(dev, led->name);
Russell King5f7b03d2014-04-06 15:20:08 -070095 if (IS_ERR(led_data->pwm)) {
96 ret = PTR_ERR(led_data->pwm);
Jerome Brunet9aec3032018-09-06 15:59:04 +020097 if (ret != -EPROBE_DEFER)
98 dev_err(dev, "unable to request PWM for %s: %d\n",
99 led->name, ret);
Russell King5f7b03d2014-04-06 15:20:08 -0700100 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800101 }
102
Thierry Reding247bde12017-01-04 09:37:56 +0100103 led_data->cdev.brightness_set_blocking = led_pwm_set;
Russell King5f7b03d2014-04-06 15:20:08 -0700104
Boris Brezillon1b506732016-04-14 21:17:26 +0200105 /*
106 * FIXME: pwm_apply_args() should be removed when switching to the
107 * atomic PWM API.
108 */
109 pwm_apply_args(led_data->pwm);
110
111 pwm_get_args(led_data->pwm, &pargs);
112
113 led_data->period = pargs.period;
Linus Torvalds7c574cf2014-06-12 13:08:09 -0700114 if (!led_data->period && (led->pwm_period_ns > 0))
115 led_data->period = led->pwm_period_ns;
116
Krzysztof Kozlowskie5a04362018-12-07 13:32:51 +0100117 ret = devm_led_classdev_register(dev, &led_data->cdev);
Russell King5f7b03d2014-04-06 15:20:08 -0700118 if (ret == 0) {
119 priv->num_leds++;
Markus Hofstaetterf1670332015-11-11 12:40:29 +0100120 led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
Russell King5f7b03d2014-04-06 15:20:08 -0700121 } else {
122 dev_err(dev, "failed to register PWM led for %s: %d\n",
123 led->name, ret);
124 }
125
126 return ret;
127}
128
Russell Kingb795e6d2014-04-06 15:20:13 -0700129static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800130{
131 struct device_node *child;
Russell Kingb795e6d2014-04-06 15:20:13 -0700132 struct led_pwm led;
133 int ret = 0;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800134
Russell Kingb795e6d2014-04-06 15:20:13 -0700135 memset(&led, 0, sizeof(led));
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800136
Russell Kingb795e6d2014-04-06 15:20:13 -0700137 for_each_child_of_node(dev->of_node, child) {
138 led.name = of_get_property(child, "label", NULL) ? :
139 child->name;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800140
Russell Kingb795e6d2014-04-06 15:20:13 -0700141 led.default_trigger = of_get_property(child,
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800142 "linux,default-trigger", NULL);
Russell Kingb0571e72014-04-06 15:20:24 -0700143 led.active_low = of_property_read_bool(child, "active-low");
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800144 of_property_read_u32(child, "max-brightness",
Russell Kingb795e6d2014-04-06 15:20:13 -0700145 &led.max_brightness);
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800146
Russell Kingb795e6d2014-04-06 15:20:13 -0700147 ret = led_pwm_add(dev, priv, &led, child);
148 if (ret) {
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800149 of_node_put(child);
Russell Kingb795e6d2014-04-06 15:20:13 -0700150 break;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800151 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800152 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800153
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800154 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800155}
156
Luotao Fu41c42ff2009-02-11 13:24:40 -0800157static int led_pwm_probe(struct platform_device *pdev)
158{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700159 struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800160 struct led_pwm_priv *priv;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800161 int count, i;
162 int ret = 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800163
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800164 if (pdata)
165 count = pdata->num_leds;
166 else
167 count = of_get_child_count(pdev->dev.of_node);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800168
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800169 if (!count)
170 return -EINVAL;
171
172 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
173 GFP_KERNEL);
174 if (!priv)
175 return -ENOMEM;
176
177 if (pdata) {
178 for (i = 0; i < count; i++) {
Russell Kingb795e6d2014-04-06 15:20:13 -0700179 ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
180 NULL);
Russell King5f7b03d2014-04-06 15:20:08 -0700181 if (ret)
182 break;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800183 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800184 } else {
Russell Kingb795e6d2014-04-06 15:20:13 -0700185 ret = led_pwm_create_of(&pdev->dev, priv);
Russell King5f7b03d2014-04-06 15:20:08 -0700186 }
187
Krzysztof Kozlowskie5a04362018-12-07 13:32:51 +0100188 if (ret)
Russell King5f7b03d2014-04-06 15:20:08 -0700189 return ret;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800190
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800191 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800192
193 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800194}
195
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800196static const struct of_device_id of_pwm_leds_match[] = {
197 { .compatible = "pwm-leds", },
198 {},
199};
200MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
201
Luotao Fu41c42ff2009-02-11 13:24:40 -0800202static struct platform_driver led_pwm_driver = {
203 .probe = led_pwm_probe,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800204 .driver = {
205 .name = "leds_pwm",
Sachin Kamat1e08f722013-09-28 04:38:31 -0700206 .of_match_table = of_pwm_leds_match,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800207 },
208};
209
Axel Lin892a8842012-01-10 15:09:24 -0800210module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800211
212MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
Uwe Kleine-König49651c62013-11-25 21:43:45 +0100213MODULE_DESCRIPTION("generic PWM LED driver");
214MODULE_LICENSE("GPL v2");
Luotao Fu41c42ff2009-02-11 13:24:40 -0800215MODULE_ALIAS("platform:leds-pwm");