1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PWM-based multi-color LED control
4 *
5 * Copyright 2022 Sven Schwermer <sven.schwermer@disruptive-technologies.com>
6 */
7
8 #include <linux/err.h>
9 #include <linux/kernel.h>
10 #include <linux/led-class-multicolor.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/pwm.h>
17
18 struct pwm_led {
19 struct pwm_device *pwm;
20 struct pwm_state state;
21 bool active_low;
22 };
23
24 struct pwm_mc_led {
25 struct led_classdev_mc mc_cdev;
26 struct mutex lock;
27 struct pwm_led leds[];
28 };
29
led_pwm_mc_set(struct led_classdev * cdev,enum led_brightness brightness)30 static int led_pwm_mc_set(struct led_classdev *cdev,
31 enum led_brightness brightness)
32 {
33 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
34 struct pwm_mc_led *priv = container_of(mc_cdev, struct pwm_mc_led, mc_cdev);
35 unsigned long long duty;
36 int ret = 0;
37 int i;
38
39 led_mc_calc_color_components(mc_cdev, brightness);
40
41 mutex_lock(&priv->lock);
42
43 for (i = 0; i < mc_cdev->num_colors; i++) {
44 duty = priv->leds[i].state.period;
45 duty *= mc_cdev->subled_info[i].brightness;
46 do_div(duty, cdev->max_brightness);
47
48 if (priv->leds[i].active_low)
49 duty = priv->leds[i].state.period - duty;
50
51 priv->leds[i].state.duty_cycle = duty;
52 /*
53 * Disabling a PWM doesn't guarantee that it emits the inactive level.
54 * So keep it on. Only for suspending the PWM should be disabled because
55 * otherwise it refuses to suspend. The possible downside is that the
56 * LED might stay (or even go) on.
57 */
58 priv->leds[i].state.enabled = !(cdev->flags & LED_SUSPENDED);
59 ret = pwm_apply_might_sleep(priv->leds[i].pwm,
60 &priv->leds[i].state);
61 if (ret)
62 break;
63 }
64
65 mutex_unlock(&priv->lock);
66
67 return ret;
68 }
69
iterate_subleds(struct device * dev,struct pwm_mc_led * priv,struct fwnode_handle * mcnode)70 static int iterate_subleds(struct device *dev, struct pwm_mc_led *priv,
71 struct fwnode_handle *mcnode)
72 {
73 struct mc_subled *subled = priv->mc_cdev.subled_info;
74 struct fwnode_handle *fwnode;
75 struct pwm_led *pwmled;
76 u32 color;
77 int ret;
78
79 /* iterate over the nodes inside the multi-led node */
80 fwnode_for_each_child_node(mcnode, fwnode) {
81 pwmled = &priv->leds[priv->mc_cdev.num_colors];
82 pwmled->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
83 if (IS_ERR(pwmled->pwm)) {
84 ret = dev_err_probe(dev, PTR_ERR(pwmled->pwm), "unable to request PWM\n");
85 goto release_fwnode;
86 }
87 pwm_init_state(pwmled->pwm, &pwmled->state);
88 pwmled->active_low = fwnode_property_read_bool(fwnode, "active-low");
89
90 ret = fwnode_property_read_u32(fwnode, "color", &color);
91 if (ret) {
92 dev_err(dev, "cannot read color: %d\n", ret);
93 goto release_fwnode;
94 }
95
96 subled[priv->mc_cdev.num_colors].color_index = color;
97 fwnode_property_read_u32(fwnode, "default-intensity",
98 &subled[priv->mc_cdev.num_colors].intensity);
99 priv->mc_cdev.num_colors++;
100 }
101
102 return 0;
103
104 release_fwnode:
105 fwnode_handle_put(fwnode);
106 return ret;
107 }
108
led_pwm_mc_probe(struct platform_device * pdev)109 static int led_pwm_mc_probe(struct platform_device *pdev)
110 {
111 struct fwnode_handle *mcnode;
112 struct led_init_data init_data = {};
113 struct led_classdev *cdev;
114 struct mc_subled *subled;
115 struct pwm_mc_led *priv;
116 unsigned int count;
117 int ret = 0;
118
119 mcnode = device_get_named_child_node(&pdev->dev, "multi-led");
120 if (!mcnode)
121 return dev_err_probe(&pdev->dev, -ENODEV,
122 "expected multi-led node\n");
123
124 /* count the nodes inside the multi-led node */
125 count = fwnode_get_child_node_count(mcnode);
126
127 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
128 GFP_KERNEL);
129 if (!priv) {
130 ret = -ENOMEM;
131 goto release_mcnode;
132 }
133 mutex_init(&priv->lock);
134
135 subled = devm_kcalloc(&pdev->dev, count, sizeof(*subled), GFP_KERNEL);
136 if (!subled) {
137 ret = -ENOMEM;
138 goto release_mcnode;
139 }
140 priv->mc_cdev.subled_info = subled;
141
142 /* init the multicolor's LED class device */
143 cdev = &priv->mc_cdev.led_cdev;
144 ret = fwnode_property_read_u32(mcnode, "max-brightness",
145 &cdev->max_brightness);
146 if (ret)
147 goto release_mcnode;
148
149 cdev->flags = LED_CORE_SUSPENDRESUME;
150 cdev->brightness_set_blocking = led_pwm_mc_set;
151
152 ret = iterate_subleds(&pdev->dev, priv, mcnode);
153 if (ret)
154 goto release_mcnode;
155
156 init_data.fwnode = mcnode;
157 ret = devm_led_classdev_multicolor_register_ext(&pdev->dev,
158 &priv->mc_cdev,
159 &init_data);
160 if (ret) {
161 dev_err(&pdev->dev,
162 "failed to register multicolor PWM led for %s: %d\n",
163 cdev->name, ret);
164 goto release_mcnode;
165 }
166
167 ret = led_pwm_mc_set(cdev, cdev->brightness);
168 if (ret)
169 return dev_err_probe(&pdev->dev, ret,
170 "failed to set led PWM value for %s\n",
171 cdev->name);
172
173 platform_set_drvdata(pdev, priv);
174 return 0;
175
176 release_mcnode:
177 fwnode_handle_put(mcnode);
178 return ret;
179 }
180
181 static const struct of_device_id of_pwm_leds_mc_match[] = {
182 { .compatible = "pwm-leds-multicolor", },
183 {}
184 };
185 MODULE_DEVICE_TABLE(of, of_pwm_leds_mc_match);
186
187 static struct platform_driver led_pwm_mc_driver = {
188 .probe = led_pwm_mc_probe,
189 .driver = {
190 .name = "leds_pwm_multicolor",
191 .of_match_table = of_pwm_leds_mc_match,
192 },
193 };
194 module_platform_driver(led_pwm_mc_driver);
195
196 MODULE_AUTHOR("Sven Schwermer <sven.schwermer@disruptive-technologies.com>");
197 MODULE_DESCRIPTION("multi-color PWM LED driver");
198 MODULE_LICENSE("GPL v2");
199 MODULE_ALIAS("platform:leds-pwm-multicolor");
200