xref: /openbmc/linux/drivers/leds/leds-pwm.c (revision 0661cb2a)
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;
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 			ret = EINVAL;
115 			goto err_child_out;
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 			goto err_child_out;
126 	}
127 
128 	return 0;
129 
130 err_child_out:
131 	fwnode_handle_put(fwnode);
132 	return ret;
133 }
134 
135 static int led_pwm_probe(struct platform_device *pdev)
136 {
137 	struct led_pwm_priv *priv;
138 	int ret = 0;
139 	int count;
140 
141 	count = device_get_child_node_count(&pdev->dev);
142 
143 	if (!count)
144 		return -EINVAL;
145 
146 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
147 			    GFP_KERNEL);
148 	if (!priv)
149 		return -ENOMEM;
150 
151 	ret = led_pwm_create_fwnode(&pdev->dev, priv);
152 
153 	if (ret)
154 		return ret;
155 
156 	platform_set_drvdata(pdev, priv);
157 
158 	return 0;
159 }
160 
161 static const struct of_device_id of_pwm_leds_match[] = {
162 	{ .compatible = "pwm-leds", },
163 	{},
164 };
165 MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
166 
167 static struct platform_driver led_pwm_driver = {
168 	.probe		= led_pwm_probe,
169 	.driver		= {
170 		.name	= "leds_pwm",
171 		.of_match_table = of_pwm_leds_match,
172 	},
173 };
174 
175 module_platform_driver(led_pwm_driver);
176 
177 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
178 MODULE_DESCRIPTION("generic PWM LED driver");
179 MODULE_LICENSE("GPL v2");
180 MODULE_ALIAS("platform:leds-pwm");
181