1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Multi-color LED built with monochromatic LED devices
4 *
5 * This driver groups several monochromatic LED devices in a single multicolor LED device.
6 *
7 * Compared to handling this grouping in user-space, the benefits are:
8 * - The state of the monochromatic LED relative to each other is always consistent.
9 * - The sysfs interface of the LEDs can be used for the group as a whole.
10 *
11 * Copyright 2023 Jean-Jacques Hiblot <jjhiblot@traphandler.com>
12 */
13
14 #include <linux/err.h>
15 #include <linux/leds.h>
16 #include <linux/led-class-multicolor.h>
17 #include <linux/math.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21
22 struct leds_multicolor {
23 struct led_classdev_mc mc_cdev;
24 struct led_classdev **monochromatics;
25 };
26
leds_gmc_set(struct led_classdev * cdev,enum led_brightness brightness)27 static int leds_gmc_set(struct led_classdev *cdev, enum led_brightness brightness)
28 {
29 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
30 struct leds_multicolor *priv = container_of(mc_cdev, struct leds_multicolor, mc_cdev);
31 const unsigned int group_max_brightness = mc_cdev->led_cdev.max_brightness;
32 int i;
33
34 for (i = 0; i < mc_cdev->num_colors; i++) {
35 struct led_classdev *mono = priv->monochromatics[i];
36 const unsigned int mono_max_brightness = mono->max_brightness;
37 unsigned int intensity = mc_cdev->subled_info[i].intensity;
38 int mono_brightness;
39
40 /*
41 * Scale the brightness according to relative intensity of the
42 * color AND the max brightness of the monochromatic LED.
43 */
44 mono_brightness = DIV_ROUND_CLOSEST(brightness * intensity * mono_max_brightness,
45 group_max_brightness * group_max_brightness);
46
47 led_set_brightness(mono, mono_brightness);
48 }
49
50 return 0;
51 }
52
restore_sysfs_write_access(void * data)53 static void restore_sysfs_write_access(void *data)
54 {
55 struct led_classdev *led_cdev = data;
56
57 /* Restore the write access to the LED */
58 mutex_lock(&led_cdev->led_access);
59 led_sysfs_enable(led_cdev);
60 mutex_unlock(&led_cdev->led_access);
61 }
62
leds_gmc_probe(struct platform_device * pdev)63 static int leds_gmc_probe(struct platform_device *pdev)
64 {
65 struct device *dev = &pdev->dev;
66 struct led_init_data init_data = {};
67 struct led_classdev *cdev;
68 struct mc_subled *subled;
69 struct leds_multicolor *priv;
70 unsigned int max_brightness = 0;
71 int i, ret, count = 0, common_flags = 0;
72
73 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
74 if (!priv)
75 return -ENOMEM;
76
77 for (;;) {
78 struct led_classdev *led_cdev;
79
80 led_cdev = devm_of_led_get_optional(dev, count);
81 if (IS_ERR(led_cdev))
82 return dev_err_probe(dev, PTR_ERR(led_cdev), "Unable to get LED #%d",
83 count);
84 if (!led_cdev)
85 break;
86
87 priv->monochromatics = devm_krealloc_array(dev, priv->monochromatics,
88 count + 1, sizeof(*priv->monochromatics),
89 GFP_KERNEL);
90 if (!priv->monochromatics)
91 return -ENOMEM;
92
93 common_flags |= led_cdev->flags;
94 priv->monochromatics[count] = led_cdev;
95
96 max_brightness = max(max_brightness, led_cdev->max_brightness);
97
98 count++;
99 }
100
101 subled = devm_kcalloc(dev, count, sizeof(*subled), GFP_KERNEL);
102 if (!subled)
103 return -ENOMEM;
104 priv->mc_cdev.subled_info = subled;
105
106 for (i = 0; i < count; i++) {
107 struct led_classdev *led_cdev = priv->monochromatics[i];
108
109 subled[i].color_index = led_cdev->color;
110
111 /* Configure the LED intensity to its maximum */
112 subled[i].intensity = max_brightness;
113 }
114
115 /* Initialise the multicolor's LED class device */
116 cdev = &priv->mc_cdev.led_cdev;
117 cdev->brightness_set_blocking = leds_gmc_set;
118 cdev->max_brightness = max_brightness;
119 cdev->color = LED_COLOR_ID_MULTI;
120 priv->mc_cdev.num_colors = count;
121
122 /* we only need suspend/resume if a sub-led requests it */
123 if (common_flags & LED_CORE_SUSPENDRESUME)
124 cdev->flags = LED_CORE_SUSPENDRESUME;
125
126 init_data.fwnode = dev_fwnode(dev);
127 ret = devm_led_classdev_multicolor_register_ext(dev, &priv->mc_cdev, &init_data);
128 if (ret)
129 return dev_err_probe(dev, ret, "failed to register multicolor LED for %s.\n",
130 cdev->name);
131
132 ret = leds_gmc_set(cdev, cdev->brightness);
133 if (ret)
134 return dev_err_probe(dev, ret, "failed to set LED value for %s.", cdev->name);
135
136 for (i = 0; i < count; i++) {
137 struct led_classdev *led_cdev = priv->monochromatics[i];
138
139 /*
140 * Make the individual LED sysfs interface read-only to prevent the user
141 * to change the brightness of the individual LEDs of the group.
142 */
143 mutex_lock(&led_cdev->led_access);
144 led_sysfs_disable(led_cdev);
145 mutex_unlock(&led_cdev->led_access);
146
147 /* Restore the write access to the LED sysfs when the group is destroyed */
148 devm_add_action_or_reset(dev, restore_sysfs_write_access, led_cdev);
149 }
150
151 return 0;
152 }
153
154 static const struct of_device_id of_leds_group_multicolor_match[] = {
155 { .compatible = "leds-group-multicolor" },
156 {}
157 };
158 MODULE_DEVICE_TABLE(of, of_leds_group_multicolor_match);
159
160 static struct platform_driver leds_group_multicolor_driver = {
161 .probe = leds_gmc_probe,
162 .driver = {
163 .name = "leds_group_multicolor",
164 .of_match_table = of_leds_group_multicolor_match,
165 }
166 };
167 module_platform_driver(leds_group_multicolor_driver);
168
169 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
170 MODULE_DESCRIPTION("LEDs group multicolor driver");
171 MODULE_LICENSE("GPL");
172 MODULE_ALIAS("platform:leds-group-multicolor");
173