blob: d71e9fa5c8decfe919ba3cc4878b975c1d728546 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080020
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000021struct led_pwm {
22 const char *name;
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000023 u8 active_low;
24 unsigned int max_brightness;
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000025};
26
Luotao Fu41c42ff2009-02-11 13:24:40 -080027struct led_pwm_data {
28 struct led_classdev cdev;
29 struct pwm_device *pwm;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010030 struct pwm_state pwmstate;
Sachin Kamatdcba9102012-11-25 12:24:55 +053031 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080032};
33
Peter Ujfalusi0f868152012-12-21 01:43:56 -080034struct led_pwm_priv {
35 int num_leds;
Gustavo A. R. Silva7bbec6c2020-03-19 16:51:46 -050036 struct led_pwm_data leds[];
Peter Ujfalusi0f868152012-12-21 01:43:56 -080037};
38
Thierry Reding247bde12017-01-04 09:37:56 +010039static int led_pwm_set(struct led_classdev *led_cdev,
40 enum led_brightness brightness)
Luotao Fu41c42ff2009-02-11 13:24:40 -080041{
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;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010045 unsigned long long duty = led_dat->pwmstate.period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080046
Xiubo Lifc1aee02013-12-11 01:19:42 -080047 duty *= brightness;
48 do_div(duty, max);
Russell Kingd19a8a72014-04-06 15:20:18 -070049
50 if (led_dat->active_low)
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010051 duty = led_dat->pwmstate.period - duty;
Russell Kingd19a8a72014-04-06 15:20:18 -070052
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010053 led_dat->pwmstate.duty_cycle = duty;
54 led_dat->pwmstate.enabled = duty > 0;
55 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate);
Luotao Fu41c42ff2009-02-11 13:24:40 -080056}
57
Alexander Dahl19d2e0c2020-10-05 22:34:40 +020058__attribute__((nonnull))
Russell King5f7b03d2014-04-06 15:20:08 -070059static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020060 struct led_pwm *led, struct fwnode_handle *fwnode)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080061{
Russell King5f7b03d2014-04-06 15:20:08 -070062 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
Alexander Dahlde73f272020-09-19 07:31:44 +020063 struct led_init_data init_data = { .fwnode = fwnode };
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -080064 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080065
Russell King5f7b03d2014-04-06 15:20:08 -070066 led_data->active_low = led->active_low;
Russell King5f7b03d2014-04-06 15:20:08 -070067 led_data->cdev.name = led->name;
Russell King5f7b03d2014-04-06 15:20:08 -070068 led_data->cdev.brightness = LED_OFF;
69 led_data->cdev.max_brightness = led->max_brightness;
70 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080071
Alexander Dahl19d2e0c2020-10-05 22:34:40 +020072 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
Krzysztof Kozlowski7e8da602020-08-26 16:50:11 +020073 if (IS_ERR(led_data->pwm))
74 return dev_err_probe(dev, PTR_ERR(led_data->pwm),
75 "unable to request PWM for %s\n",
76 led->name);
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080077
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
Alexander Dahlde73f272020-09-19 07:31:44 +020082 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +000083 if (ret) {
Russell King5f7b03d2014-04-06 15:20:08 -070084 dev_err(dev, "failed to register PWM led for %s: %d\n",
85 led->name, ret);
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +000086 return ret;
Russell King5f7b03d2014-04-06 15:20:08 -070087 }
88
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +000089 ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
90 if (ret) {
91 dev_err(dev, "failed to set led PWM value for %s: %d",
92 led->name, ret);
93 return ret;
94 }
95
96 priv->num_leds++;
97 return 0;
Russell King5f7b03d2014-04-06 15:20:08 -070098}
99
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200100static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800101{
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200102 struct fwnode_handle *fwnode;
Russell Kingb795e6d2014-04-06 15:20:13 -0700103 struct led_pwm led;
Andy Shevchenko95138e02021-05-10 12:50:43 +0300104 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800105
Russell Kingb795e6d2014-04-06 15:20:13 -0700106 memset(&led, 0, sizeof(led));
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800107
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200108 device_for_each_child_node(dev, fwnode) {
109 ret = fwnode_property_read_string(fwnode, "label", &led.name);
110 if (ret && is_of_node(fwnode))
111 led.name = to_of_node(fwnode)->name;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800112
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200113 if (!led.name) {
Andy Shevchenko95138e02021-05-10 12:50:43 +0300114 ret = EINVAL;
115 goto err_child_out;
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200116 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800117
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200118 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);
Andy Shevchenko95138e02021-05-10 12:50:43 +0300124 if (ret)
125 goto err_child_out;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800126 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800127
Andy Shevchenko95138e02021-05-10 12:50:43 +0300128 return 0;
129
130err_child_out:
131 fwnode_handle_put(fwnode);
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800132 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800133}
134
Luotao Fu41c42ff2009-02-11 13:24:40 -0800135static int led_pwm_probe(struct platform_device *pdev)
136{
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800137 struct led_pwm_priv *priv;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800138 int ret = 0;
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200139 int count;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800140
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200141 count = device_get_child_node_count(&pdev->dev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800142
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800143 if (!count)
144 return -EINVAL;
145
Gustavo A. R. Silvad4b02202019-08-29 19:53:20 -0500146 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800147 GFP_KERNEL);
148 if (!priv)
149 return -ENOMEM;
150
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200151 ret = led_pwm_create_fwnode(&pdev->dev, priv);
Russell King5f7b03d2014-04-06 15:20:08 -0700152
Krzysztof Kozlowskie5a04362018-12-07 13:32:51 +0100153 if (ret)
Russell King5f7b03d2014-04-06 15:20:08 -0700154 return ret;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800155
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800156 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800157
158 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800159}
160
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800161static const struct of_device_id of_pwm_leds_match[] = {
162 { .compatible = "pwm-leds", },
163 {},
164};
165MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
166
Luotao Fu41c42ff2009-02-11 13:24:40 -0800167static struct platform_driver led_pwm_driver = {
168 .probe = led_pwm_probe,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800169 .driver = {
170 .name = "leds_pwm",
Sachin Kamat1e08f722013-09-28 04:38:31 -0700171 .of_match_table = of_pwm_leds_match,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800172 },
173};
174
Axel Lin892a8842012-01-10 15:09:24 -0800175module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800176
177MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
Uwe Kleine-König49651c62013-11-25 21:43:45 +0100178MODULE_DESCRIPTION("generic PWM LED driver");
179MODULE_LICENSE("GPL v2");
Luotao Fu41c42ff2009-02-11 13:24:40 -0800180MODULE_ALIAS("platform:leds-pwm");