Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / leds-pwm.c
blobf53f9309ca6cca95adca94ad922ebd6aed98d9ec
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/leds-pwm.c
5 * simple PWM based LED control
7 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
9 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
21 struct led_pwm {
22 const char *name;
23 u8 active_low;
24 unsigned int max_brightness;
27 struct led_pwm_data {
28 struct led_classdev cdev;
29 struct pwm_device *pwm;
30 struct pwm_state pwmstate;
31 unsigned int active_low;
34 struct led_pwm_priv {
35 int num_leds;
36 struct led_pwm_data leds[];
39 static int led_pwm_set(struct led_classdev *led_cdev,
40 enum led_brightness brightness)
42 struct led_pwm_data *led_dat =
43 container_of(led_cdev, struct led_pwm_data, cdev);
44 unsigned int max = led_dat->cdev.max_brightness;
45 unsigned long long duty = led_dat->pwmstate.period;
47 duty *= brightness;
48 do_div(duty, max);
50 if (led_dat->active_low)
51 duty = led_dat->pwmstate.period - duty;
53 led_dat->pwmstate.duty_cycle = duty;
54 led_dat->pwmstate.enabled = duty > 0;
55 return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate);
58 __attribute__((nonnull))
59 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
60 struct led_pwm *led, struct fwnode_handle *fwnode)
62 struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
63 struct led_init_data init_data = { .fwnode = fwnode };
64 int ret;
66 led_data->active_low = led->active_low;
67 led_data->cdev.name = led->name;
68 led_data->cdev.brightness = LED_OFF;
69 led_data->cdev.max_brightness = led->max_brightness;
70 led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
72 led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
73 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);
78 led_data->cdev.brightness_set_blocking = led_pwm_set;
80 pwm_init_state(led_data->pwm, &led_data->pwmstate);
82 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
83 if (ret) {
84 dev_err(dev, "failed to register PWM led for %s: %d\n",
85 led->name, ret);
86 return ret;
89 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;
96 priv->num_leds++;
97 return 0;
100 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
102 struct fwnode_handle *fwnode;
103 struct led_pwm led;
104 int ret = 0;
106 memset(&led, 0, sizeof(led));
108 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;
113 if (!led.name) {
114 fwnode_handle_put(fwnode);
115 return -EINVAL;
118 led.active_low = fwnode_property_read_bool(fwnode,
119 "active-low");
120 fwnode_property_read_u32(fwnode, "max-brightness",
121 &led.max_brightness);
123 ret = led_pwm_add(dev, priv, &led, fwnode);
124 if (ret) {
125 fwnode_handle_put(fwnode);
126 break;
130 return ret;
133 static int led_pwm_probe(struct platform_device *pdev)
135 struct led_pwm_priv *priv;
136 int ret = 0;
137 int count;
139 count = device_get_child_node_count(&pdev->dev);
141 if (!count)
142 return -EINVAL;
144 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
145 GFP_KERNEL);
146 if (!priv)
147 return -ENOMEM;
149 ret = led_pwm_create_fwnode(&pdev->dev, priv);
151 if (ret)
152 return ret;
154 platform_set_drvdata(pdev, priv);
156 return 0;
159 static const struct of_device_id of_pwm_leds_match[] = {
160 { .compatible = "pwm-leds", },
163 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
165 static struct platform_driver led_pwm_driver = {
166 .probe = led_pwm_probe,
167 .driver = {
168 .name = "leds_pwm",
169 .of_match_table = of_pwm_leds_match,
173 module_platform_driver(led_pwm_driver);
175 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
176 MODULE_DESCRIPTION("generic PWM LED driver");
177 MODULE_LICENSE("GPL v2");
178 MODULE_ALIAS("platform:leds-pwm");