xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 80ade22c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
10  */
11 
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>
20 
21 struct led_pwm {
22 	const char	*name;
23 	u8		active_low;
24 	unsigned int	max_brightness;
25 };
26 
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;
32 };
33 
34 struct led_pwm_priv {
35 	int num_leds;
36 	struct led_pwm_data leds[];
37 };
38 
39 static int led_pwm_set(struct led_classdev *led_cdev,
40 		       enum led_brightness brightness)
41 {
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;
46 
47 	duty *= brightness;
48 	do_div(duty, max);
49 
50 	if (led_dat->active_low)
51 		duty = led_dat->pwmstate.period - duty;
52 
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);
56 }
57 
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)
61 {
62 	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
63 	struct led_init_data init_data = { .fwnode = fwnode };
64 	int ret;
65 
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;
71 
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);
77 
78 	led_data->cdev.brightness_set_blocking = led_pwm_set;
79 
80 	pwm_init_state(led_data->pwm, &led_data->pwmstate);
81 
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;
87 	}
88 
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;
94 	}
95 
96 	priv->num_leds++;
97 	return 0;
98 }
99 
100 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
101 {
102 	struct fwnode_handle *fwnode;
103 	struct led_pwm led;
104 	int ret = 0;
105 
106 	memset(&led, 0, sizeof(led));
107 
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;
112 
113 		if (!led.name) {
114 			fwnode_handle_put(fwnode);
115 			return -EINVAL;
116 		}
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);
124 		if (ret) {
125 			fwnode_handle_put(fwnode);
126 			break;
127 		}
128 	}
129 
130 	return ret;
131 }
132 
133 static int led_pwm_probe(struct platform_device *pdev)
134 {
135 	struct led_pwm_priv *priv;
136 	int ret = 0;
137 	int count;
138 
139 	count = device_get_child_node_count(&pdev->dev);
140 
141 	if (!count)
142 		return -EINVAL;
143 
144 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
145 			    GFP_KERNEL);
146 	if (!priv)
147 		return -ENOMEM;
148 
149 	ret = led_pwm_create_fwnode(&pdev->dev, priv);
150 
151 	if (ret)
152 		return ret;
153 
154 	platform_set_drvdata(pdev, priv);
155 
156 	return 0;
157 }
158 
159 static const struct of_device_id of_pwm_leds_match[] = {
160 	{ .compatible = "pwm-leds", },
161 	{},
162 };
163 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
164 
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,
170 	},
171 };
172 
173 module_platform_driver(led_pwm_driver);
174 
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");
179