blob: 9111cdede0eeecde47903f11405de3a2e0ee7e0b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Luotao Fu41c42ff2009-02-11 13:24:40 -08002/*
3 * linux/drivers/leds-pwm.c
4 *
5 * simple PWM based LED control
6 *
7 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
8 *
9 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
Luotao Fu41c42ff2009-02-11 13:24:40 -080010 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080014#include <linux/platform_device.h>
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080015#include <linux/of_platform.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080016#include <linux/leds.h>
17#include <linux/err.h>
18#include <linux/pwm.h>
19#include <linux/leds_pwm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080021
22struct led_pwm_data {
23 struct led_classdev cdev;
24 struct pwm_device *pwm;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010025 struct pwm_state pwmstate;
Sachin Kamatdcba9102012-11-25 12:24:55 +053026 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080027};
28
Peter Ujfalusi0f868152012-12-21 01:43:56 -080029struct led_pwm_priv {
30 int num_leds;
31 struct led_pwm_data leds[0];
32};
33
Thierry Reding247bde12017-01-04 09:37:56 +010034static int led_pwm_set(struct led_classdev *led_cdev,
35 enum led_brightness brightness)
Luotao Fu41c42ff2009-02-11 13:24:40 -080036{
37 struct led_pwm_data *led_dat =
38 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010039 unsigned int max = led_dat->cdev.max_brightness;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010040 unsigned long long duty = led_dat->pwmstate.period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080041
Xiubo Lifc1aee02013-12-11 01:19:42 -080042 duty *= brightness;
43 do_div(duty, max);
Russell Kingd19a8a72014-04-06 15:20:18 -070044
45 if (led_dat->active_low)
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010046 duty = led_dat->pwmstate.period - duty;
Russell Kingd19a8a72014-04-06 15:20:18 -070047
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010048 led_dat->pwmstate.duty_cycle = duty;
49 led_dat->pwmstate.enabled = duty > 0;
50 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate);
Luotao Fu41c42ff2009-02-11 13:24:40 -080051}
52
Russell King5f7b03d2014-04-06 15:20:08 -070053static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020054 struct led_pwm *led, struct fwnode_handle *fwnode)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080055{
Russell King5f7b03d2014-04-06 15:20:08 -070056 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -080057 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080058
Russell King5f7b03d2014-04-06 15:20:08 -070059 led_data->active_low = led->active_low;
Russell King5f7b03d2014-04-06 15:20:08 -070060 led_data->cdev.name = led->name;
61 led_data->cdev.default_trigger = led->default_trigger;
Russell King5f7b03d2014-04-06 15:20:08 -070062 led_data->cdev.brightness = LED_OFF;
63 led_data->cdev.max_brightness = led->max_brightness;
64 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080065
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020066 if (fwnode)
67 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
Russell Kingb795e6d2014-04-06 15:20:13 -070068 else
69 led_data->pwm = devm_pwm_get(dev, led->name);
Russell King5f7b03d2014-04-06 15:20:08 -070070 if (IS_ERR(led_data->pwm)) {
71 ret = PTR_ERR(led_data->pwm);
Jerome Brunet9aec3032018-09-06 15:59:04 +020072 if (ret != -EPROBE_DEFER)
73 dev_err(dev, "unable to request PWM for %s: %d\n",
74 led->name, ret);
Russell King5f7b03d2014-04-06 15:20:08 -070075 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080076 }
77
Thierry Reding247bde12017-01-04 09:37:56 +010078 led_data->cdev.brightness_set_blocking = led_pwm_set;
Russell King5f7b03d2014-04-06 15:20:08 -070079
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010080 pwm_init_state(led_data->pwm, &led_data->pwmstate);
Boris Brezillon1b506732016-04-14 21:17:26 +020081
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010082 if (!led_data->pwmstate.period)
83 led_data->pwmstate.period = led->pwm_period_ns;
Linus Torvalds7c574cf2014-06-12 13:08:09 -070084
Jacek Anaszewskib2b998c2019-06-09 20:19:03 +020085 ret = devm_led_classdev_register(dev, &led_data->cdev);
Russell King5f7b03d2014-04-06 15:20:08 -070086 if (ret == 0) {
87 priv->num_leds++;
Markus Hofstaetterf1670332015-11-11 12:40:29 +010088 led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
Russell King5f7b03d2014-04-06 15:20:08 -070089 } else {
90 dev_err(dev, "failed to register PWM led for %s: %d\n",
91 led->name, ret);
92 }
93
94 return ret;
95}
96
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020097static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080098{
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020099 struct fwnode_handle *fwnode;
Russell Kingb795e6d2014-04-06 15:20:13 -0700100 struct led_pwm led;
101 int ret = 0;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800102
Russell Kingb795e6d2014-04-06 15:20:13 -0700103 memset(&led, 0, sizeof(led));
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800104
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200105 device_for_each_child_node(dev, fwnode) {
106 ret = fwnode_property_read_string(fwnode, "label", &led.name);
107 if (ret && is_of_node(fwnode))
108 led.name = to_of_node(fwnode)->name;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800109
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200110 if (!led.name) {
111 fwnode_handle_put(fwnode);
112 return -EINVAL;
113 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800114
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200115 fwnode_property_read_string(fwnode, "linux,default-trigger",
116 &led.default_trigger);
117
118 led.active_low = fwnode_property_read_bool(fwnode,
119 "active-low");
120 fwnode_property_read_u32(fwnode, "max-brightness",
121 &led.max_brightness);
122
123 ret = led_pwm_add(dev, priv, &led, fwnode);
Russell Kingb795e6d2014-04-06 15:20:13 -0700124 if (ret) {
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200125 fwnode_handle_put(fwnode);
Russell Kingb795e6d2014-04-06 15:20:13 -0700126 break;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800127 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800128 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800129
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800130 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800131}
132
Luotao Fu41c42ff2009-02-11 13:24:40 -0800133static int led_pwm_probe(struct platform_device *pdev)
134{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700135 struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800136 struct led_pwm_priv *priv;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800137 int count, i;
138 int ret = 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800139
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800140 if (pdata)
141 count = pdata->num_leds;
142 else
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200143 count = device_get_child_node_count(&pdev->dev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800144
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800145 if (!count)
146 return -EINVAL;
147
Gustavo A. R. Silvad4b02202019-08-29 19:53:20 -0500148 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800149 GFP_KERNEL);
150 if (!priv)
151 return -ENOMEM;
152
153 if (pdata) {
154 for (i = 0; i < count; i++) {
Russell Kingb795e6d2014-04-06 15:20:13 -0700155 ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
156 NULL);
Russell King5f7b03d2014-04-06 15:20:08 -0700157 if (ret)
158 break;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800159 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800160 } else {
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200161 ret = led_pwm_create_fwnode(&pdev->dev, priv);
Russell King5f7b03d2014-04-06 15:20:08 -0700162 }
163
Krzysztof Kozlowskie5a04362018-12-07 13:32:51 +0100164 if (ret)
Russell King5f7b03d2014-04-06 15:20:08 -0700165 return ret;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800166
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800167 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800168
169 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800170}
171
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800172static const struct of_device_id of_pwm_leds_match[] = {
173 { .compatible = "pwm-leds", },
174 {},
175};
176MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
177
Luotao Fu41c42ff2009-02-11 13:24:40 -0800178static struct platform_driver led_pwm_driver = {
179 .probe = led_pwm_probe,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800180 .driver = {
181 .name = "leds_pwm",
Sachin Kamat1e08f722013-09-28 04:38:31 -0700182 .of_match_table = of_pwm_leds_match,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800183 },
184};
185
Axel Lin892a8842012-01-10 15:09:24 -0800186module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800187
188MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
Uwe Kleine-König49651c62013-11-25 21:43:45 +0100189MODULE_DESCRIPTION("generic PWM LED driver");
190MODULE_LICENSE("GPL v2");
Luotao Fu41c42ff2009-02-11 13:24:40 -0800191MODULE_ALIAS("platform:leds-pwm");