blob: 6832180c1c54fd2cede277d38cfff1efa3d8f1e1 [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>
Denis Osterland-Heim3d3d65b2021-06-08 08:35:54 +020020#include "leds.h"
Luotao Fu41c42ff2009-02-11 13:24:40 -080021
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000022struct led_pwm {
23 const char *name;
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000024 u8 active_low;
Denis Osterland-Heim3d3d65b2021-06-08 08:35:54 +020025 u8 default_state;
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000026 unsigned int max_brightness;
Denis Osterland-Heim141f15c2020-03-21 08:15:53 +000027};
28
Luotao Fu41c42ff2009-02-11 13:24:40 -080029struct led_pwm_data {
30 struct led_classdev cdev;
31 struct pwm_device *pwm;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010032 struct pwm_state pwmstate;
Sachin Kamatdcba9102012-11-25 12:24:55 +053033 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080034};
35
Peter Ujfalusi0f868152012-12-21 01:43:56 -080036struct led_pwm_priv {
37 int num_leds;
Gustavo A. R. Silva7bbec6c2020-03-19 16:51:46 -050038 struct led_pwm_data leds[];
Peter Ujfalusi0f868152012-12-21 01:43:56 -080039};
40
Thierry Reding247bde12017-01-04 09:37:56 +010041static int led_pwm_set(struct led_classdev *led_cdev,
42 enum led_brightness brightness)
Luotao Fu41c42ff2009-02-11 13:24:40 -080043{
44 struct led_pwm_data *led_dat =
45 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010046 unsigned int max = led_dat->cdev.max_brightness;
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010047 unsigned long long duty = led_dat->pwmstate.period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080048
Xiubo Lifc1aee02013-12-11 01:19:42 -080049 duty *= brightness;
50 do_div(duty, max);
Russell Kingd19a8a72014-04-06 15:20:18 -070051
52 if (led_dat->active_low)
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010053 duty = led_dat->pwmstate.period - duty;
Russell Kingd19a8a72014-04-06 15:20:18 -070054
Uwe Kleine-Königdd47a832020-01-24 17:54:08 +010055 led_dat->pwmstate.duty_cycle = duty;
56 led_dat->pwmstate.enabled = duty > 0;
57 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate);
Luotao Fu41c42ff2009-02-11 13:24:40 -080058}
59
Alexander Dahl19d2e0c2020-10-05 22:34:40 +020060__attribute__((nonnull))
Russell King5f7b03d2014-04-06 15:20:08 -070061static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
Nikolaus Voss3f467eb2019-06-12 10:36:08 +020062 struct led_pwm *led, struct fwnode_handle *fwnode)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080063{
Russell King5f7b03d2014-04-06 15:20:08 -070064 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
Alexander Dahlde73f272020-09-19 07:31:44 +020065 struct led_init_data init_data = { .fwnode = fwnode };
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -080066 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080067
Russell King5f7b03d2014-04-06 15:20:08 -070068 led_data->active_low = led->active_low;
Russell King5f7b03d2014-04-06 15:20:08 -070069 led_data->cdev.name = led->name;
Russell King5f7b03d2014-04-06 15:20:08 -070070 led_data->cdev.brightness = LED_OFF;
71 led_data->cdev.max_brightness = led->max_brightness;
72 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080073
Alexander Dahl19d2e0c2020-10-05 22:34:40 +020074 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
Krzysztof Kozlowski7e8da602020-08-26 16:50:11 +020075 if (IS_ERR(led_data->pwm))
76 return dev_err_probe(dev, PTR_ERR(led_data->pwm),
77 "unable to request PWM for %s\n",
78 led->name);
Peter Ujfalusi08541cb2012-12-21 01:44:01 -080079
Thierry Reding247bde12017-01-04 09:37:56 +010080 led_data->cdev.brightness_set_blocking = led_pwm_set;
Russell King5f7b03d2014-04-06 15:20:08 -070081
Denis Osterland-Heim3d3d65b2021-06-08 08:35:54 +020082 /* init PWM state */
83 switch (led->default_state) {
84 case LEDS_DEFSTATE_KEEP:
85 pwm_get_state(led_data->pwm, &led_data->pwmstate);
86 if (led_data->pwmstate.period)
87 break;
88 led->default_state = LEDS_DEFSTATE_OFF;
89 dev_warn(dev,
90 "failed to read period for %s, default to off",
91 led->name);
92 fallthrough;
93 default:
94 pwm_init_state(led_data->pwm, &led_data->pwmstate);
95 break;
96 }
97
98 /* set brightness */
99 switch (led->default_state) {
100 case LEDS_DEFSTATE_ON:
101 led_data->cdev.brightness = led->max_brightness;
102 break;
103 case LEDS_DEFSTATE_KEEP:
104 {
105 uint64_t brightness;
106
107 brightness = led->max_brightness;
108 brightness *= led_data->pwmstate.duty_cycle;
109 do_div(brightness, led_data->pwmstate.period);
110 led_data->cdev.brightness = brightness;
111 }
112 break;
113 }
Boris Brezillon1b506732016-04-14 21:17:26 +0200114
Alexander Dahlde73f272020-09-19 07:31:44 +0200115 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +0000116 if (ret) {
Russell King5f7b03d2014-04-06 15:20:08 -0700117 dev_err(dev, "failed to register PWM led for %s: %d\n",
118 led->name, ret);
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +0000119 return ret;
Russell King5f7b03d2014-04-06 15:20:08 -0700120 }
121
Denis Osterland-Heim3d3d65b2021-06-08 08:35:54 +0200122 if (led->default_state != LEDS_DEFSTATE_KEEP) {
123 ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
124 if (ret) {
125 dev_err(dev, "failed to set led PWM value for %s: %d",
126 led->name, ret);
127 return ret;
128 }
Denis Osterland-Heim44c606b2020-04-21 13:09:14 +0000129 }
130
131 priv->num_leds++;
132 return 0;
Russell King5f7b03d2014-04-06 15:20:08 -0700133}
134
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200135static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800136{
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200137 struct fwnode_handle *fwnode;
Russell Kingb795e6d2014-04-06 15:20:13 -0700138 struct led_pwm led;
Andy Shevchenko95138e02021-05-10 12:50:43 +0300139 int ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800140
Russell Kingb795e6d2014-04-06 15:20:13 -0700141 memset(&led, 0, sizeof(led));
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800142
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200143 device_for_each_child_node(dev, fwnode) {
144 ret = fwnode_property_read_string(fwnode, "label", &led.name);
145 if (ret && is_of_node(fwnode))
146 led.name = to_of_node(fwnode)->name;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800147
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200148 if (!led.name) {
Andy Shevchenko95138e02021-05-10 12:50:43 +0300149 ret = EINVAL;
150 goto err_child_out;
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200151 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800152
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200153 led.active_low = fwnode_property_read_bool(fwnode,
154 "active-low");
155 fwnode_property_read_u32(fwnode, "max-brightness",
156 &led.max_brightness);
157
Denis Osterland-Heim3d3d65b2021-06-08 08:35:54 +0200158 led.default_state = led_init_default_state_get(fwnode);
159
Nikolaus Voss3f467eb2019-06-12 10:36:08 +0200160 ret = led_pwm_add(dev, priv, &led, fwnode);
Andy Shevchenko95138e02021-05-10 12:50:43 +0300161 if (ret)
162 goto err_child_out;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800163 }
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800164
Andy Shevchenko95138e02021-05-10 12:50:43 +0300165 return 0;
166
167err_child_out:
168 fwnode_handle_put(fwnode);
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800169 return ret;
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800170}
171
Luotao Fu41c42ff2009-02-11 13:24:40 -0800172static int led_pwm_probe(struct platform_device *pdev)
173{
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800174 struct led_pwm_priv *priv;
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800175 int ret = 0;
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200176 int count;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800177
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200178 count = device_get_child_node_count(&pdev->dev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800179
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800180 if (!count)
181 return -EINVAL;
182
Gustavo A. R. Silvad4b02202019-08-29 19:53:20 -0500183 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
Peter Ujfalusiaa1a6d62013-11-28 01:06:38 -0800184 GFP_KERNEL);
185 if (!priv)
186 return -ENOMEM;
187
Alexander Dahl19d2e0c2020-10-05 22:34:40 +0200188 ret = led_pwm_create_fwnode(&pdev->dev, priv);
Russell King5f7b03d2014-04-06 15:20:08 -0700189
Krzysztof Kozlowskie5a04362018-12-07 13:32:51 +0100190 if (ret)
Russell King5f7b03d2014-04-06 15:20:08 -0700191 return ret;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800192
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800193 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800194
195 return 0;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800196}
197
Peter Ujfalusi08541cb2012-12-21 01:44:01 -0800198static const struct of_device_id of_pwm_leds_match[] = {
199 { .compatible = "pwm-leds", },
200 {},
201};
202MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
203
Luotao Fu41c42ff2009-02-11 13:24:40 -0800204static struct platform_driver led_pwm_driver = {
205 .probe = led_pwm_probe,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800206 .driver = {
207 .name = "leds_pwm",
Sachin Kamat1e08f722013-09-28 04:38:31 -0700208 .of_match_table = of_pwm_leds_match,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800209 },
210};
211
Axel Lin892a8842012-01-10 15:09:24 -0800212module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800213
214MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
Uwe Kleine-König49651c62013-11-25 21:43:45 +0100215MODULE_DESCRIPTION("generic PWM LED driver");
216MODULE_LICENSE("GPL v2");
Luotao Fu41c42ff2009-02-11 13:24:40 -0800217MODULE_ALIAS("platform:leds-pwm");