1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CZ.NIC's Turris Omnia LEDs driver
4  *
5  * 2020, 2023 by Marek Behún <kabel@kernel.org>
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/led-class-multicolor.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/of.h>
13 #include "leds.h"
14 
15 #define OMNIA_BOARD_LEDS	12
16 #define OMNIA_LED_NUM_CHANNELS	3
17 
18 #define CMD_LED_MODE		3
19 #define CMD_LED_MODE_LED(l)	((l) & 0x0f)
20 #define CMD_LED_MODE_USER	0x10
21 
22 #define CMD_LED_STATE		4
23 #define CMD_LED_STATE_LED(l)	((l) & 0x0f)
24 #define CMD_LED_STATE_ON	0x10
25 
26 #define CMD_LED_COLOR		5
27 #define CMD_LED_SET_BRIGHTNESS	7
28 #define CMD_LED_GET_BRIGHTNESS	8
29 
30 struct omnia_led {
31 	struct led_classdev_mc mc_cdev;
32 	struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
33 	int reg;
34 };
35 
36 #define to_omnia_led(l)		container_of(l, struct omnia_led, mc_cdev)
37 
38 struct omnia_leds {
39 	struct i2c_client *client;
40 	struct mutex lock;
41 	struct omnia_led leds[];
42 };
43 
44 static int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd, u8 val)
45 {
46 	u8 buf[2] = { cmd, val };
47 
48 	return i2c_master_send(client, buf, sizeof(buf));
49 }
50 
51 static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
52 {
53 	struct i2c_msg msgs[2];
54 	u8 reply;
55 	int ret;
56 
57 	msgs[0].addr = client->addr;
58 	msgs[0].flags = 0;
59 	msgs[0].len = 1;
60 	msgs[0].buf = &cmd;
61 	msgs[1].addr = client->addr;
62 	msgs[1].flags = I2C_M_RD;
63 	msgs[1].len = 1;
64 	msgs[1].buf = &reply;
65 
66 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
67 	if (likely(ret == ARRAY_SIZE(msgs)))
68 		return reply;
69 	else if (ret < 0)
70 		return ret;
71 	else
72 		return -EIO;
73 }
74 
75 static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
76 					     enum led_brightness brightness)
77 {
78 	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
79 	struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
80 	struct omnia_led *led = to_omnia_led(mc_cdev);
81 	u8 buf[5], state;
82 	int ret;
83 
84 	mutex_lock(&leds->lock);
85 
86 	led_mc_calc_color_components(&led->mc_cdev, brightness);
87 
88 	buf[0] = CMD_LED_COLOR;
89 	buf[1] = led->reg;
90 	buf[2] = mc_cdev->subled_info[0].brightness;
91 	buf[3] = mc_cdev->subled_info[1].brightness;
92 	buf[4] = mc_cdev->subled_info[2].brightness;
93 
94 	state = CMD_LED_STATE_LED(led->reg);
95 	if (buf[2] || buf[3] || buf[4])
96 		state |= CMD_LED_STATE_ON;
97 
98 	ret = omnia_cmd_write_u8(leds->client, CMD_LED_STATE, state);
99 	if (ret >= 0 && (state & CMD_LED_STATE_ON))
100 		ret = i2c_master_send(leds->client, buf, 5);
101 
102 	mutex_unlock(&leds->lock);
103 
104 	return ret;
105 }
106 
107 static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
108 			      struct device_node *np)
109 {
110 	struct led_init_data init_data = {};
111 	struct device *dev = &client->dev;
112 	struct led_classdev *cdev;
113 	int ret, color;
114 
115 	ret = of_property_read_u32(np, "reg", &led->reg);
116 	if (ret || led->reg >= OMNIA_BOARD_LEDS) {
117 		dev_warn(dev,
118 			 "Node %pOF: must contain 'reg' property with values between 0 and %i\n",
119 			 np, OMNIA_BOARD_LEDS - 1);
120 		return 0;
121 	}
122 
123 	ret = of_property_read_u32(np, "color", &color);
124 	if (ret || color != LED_COLOR_ID_RGB) {
125 		dev_warn(dev,
126 			 "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n",
127 			 np);
128 		return 0;
129 	}
130 
131 	led->subled_info[0].color_index = LED_COLOR_ID_RED;
132 	led->subled_info[0].channel = 0;
133 	led->subled_info[1].color_index = LED_COLOR_ID_GREEN;
134 	led->subled_info[1].channel = 1;
135 	led->subled_info[2].color_index = LED_COLOR_ID_BLUE;
136 	led->subled_info[2].channel = 2;
137 
138 	led->mc_cdev.subled_info = led->subled_info;
139 	led->mc_cdev.num_colors = OMNIA_LED_NUM_CHANNELS;
140 
141 	init_data.fwnode = &np->fwnode;
142 
143 	cdev = &led->mc_cdev.led_cdev;
144 	cdev->max_brightness = 255;
145 	cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
146 
147 	/* put the LED into software mode */
148 	ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
149 				 CMD_LED_MODE_LED(led->reg) |
150 				 CMD_LED_MODE_USER);
151 	if (ret < 0) {
152 		dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
153 			ret);
154 		return ret;
155 	}
156 
157 	/* disable the LED */
158 	ret = omnia_cmd_write_u8(client, CMD_LED_STATE,
159 				 CMD_LED_STATE_LED(led->reg));
160 	if (ret < 0) {
161 		dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
162 		return ret;
163 	}
164 
165 	ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
166 							&init_data);
167 	if (ret < 0) {
168 		dev_err(dev, "Cannot register LED %pOF: %i\n", np, ret);
169 		return ret;
170 	}
171 
172 	return 1;
173 }
174 
175 /*
176  * On the front panel of the Turris Omnia router there is also a button which
177  * can be used to control the intensity of all the LEDs at once, so that if they
178  * are too bright, user can dim them.
179  * The microcontroller cycles between 8 levels of this global brightness (from
180  * 100% to 0%), but this setting can have any integer value between 0 and 100.
181  * It is therefore convenient to be able to change this setting from software.
182  * We expose this setting via a sysfs attribute file called "brightness". This
183  * file lives in the device directory of the LED controller, not an individual
184  * LED, so it should not confuse users.
185  */
186 static ssize_t brightness_show(struct device *dev, struct device_attribute *a,
187 			       char *buf)
188 {
189 	struct i2c_client *client = to_i2c_client(dev);
190 	int ret;
191 
192 	ret = omnia_cmd_read_u8(client, CMD_LED_GET_BRIGHTNESS);
193 
194 	if (ret < 0)
195 		return ret;
196 
197 	return sysfs_emit(buf, "%d\n", ret);
198 }
199 
200 static ssize_t brightness_store(struct device *dev, struct device_attribute *a,
201 				const char *buf, size_t count)
202 {
203 	struct i2c_client *client = to_i2c_client(dev);
204 	unsigned long brightness;
205 	int ret;
206 
207 	if (kstrtoul(buf, 10, &brightness))
208 		return -EINVAL;
209 
210 	if (brightness > 100)
211 		return -EINVAL;
212 
213 	ret = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
214 
215 	return ret < 0 ? ret : count;
216 }
217 static DEVICE_ATTR_RW(brightness);
218 
219 static struct attribute *omnia_led_controller_attrs[] = {
220 	&dev_attr_brightness.attr,
221 	NULL,
222 };
223 ATTRIBUTE_GROUPS(omnia_led_controller);
224 
225 static int omnia_leds_probe(struct i2c_client *client)
226 {
227 	struct device *dev = &client->dev;
228 	struct device_node *np = dev_of_node(dev), *child;
229 	struct omnia_leds *leds;
230 	struct omnia_led *led;
231 	int ret, count;
232 
233 	count = of_get_available_child_count(np);
234 	if (!count) {
235 		dev_err(dev, "LEDs are not defined in device tree!\n");
236 		return -ENODEV;
237 	} else if (count > OMNIA_BOARD_LEDS) {
238 		dev_err(dev, "Too many LEDs defined in device tree!\n");
239 		return -EINVAL;
240 	}
241 
242 	leds = devm_kzalloc(dev, struct_size(leds, leds, count), GFP_KERNEL);
243 	if (!leds)
244 		return -ENOMEM;
245 
246 	leds->client = client;
247 	i2c_set_clientdata(client, leds);
248 
249 	mutex_init(&leds->lock);
250 
251 	led = &leds->leds[0];
252 	for_each_available_child_of_node(np, child) {
253 		ret = omnia_led_register(client, led, child);
254 		if (ret < 0) {
255 			of_node_put(child);
256 			return ret;
257 		}
258 
259 		led += ret;
260 	}
261 
262 	return 0;
263 }
264 
265 static void omnia_leds_remove(struct i2c_client *client)
266 {
267 	u8 buf[5];
268 
269 	/* put all LEDs into default (HW triggered) mode */
270 	omnia_cmd_write_u8(client, CMD_LED_MODE,
271 			   CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
272 
273 	/* set all LEDs color to [255, 255, 255] */
274 	buf[0] = CMD_LED_COLOR;
275 	buf[1] = OMNIA_BOARD_LEDS;
276 	buf[2] = 255;
277 	buf[3] = 255;
278 	buf[4] = 255;
279 
280 	i2c_master_send(client, buf, 5);
281 }
282 
283 static const struct of_device_id of_omnia_leds_match[] = {
284 	{ .compatible = "cznic,turris-omnia-leds", },
285 	{},
286 };
287 
288 static const struct i2c_device_id omnia_id[] = {
289 	{ "omnia", 0 },
290 	{ }
291 };
292 MODULE_DEVICE_TABLE(i2c, omnia_id);
293 
294 static struct i2c_driver omnia_leds_driver = {
295 	.probe		= omnia_leds_probe,
296 	.remove		= omnia_leds_remove,
297 	.id_table	= omnia_id,
298 	.driver		= {
299 		.name	= "leds-turris-omnia",
300 		.of_match_table = of_omnia_leds_match,
301 		.dev_groups = omnia_led_controller_groups,
302 	},
303 };
304 
305 module_i2c_driver(omnia_leds_driver);
306 
307 MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
308 MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
309 MODULE_LICENSE("GPL v2");
310