1 // SPDX-License-Identifier: GPL-2.0
3 * CZ.NIC's Turris Omnia LEDs driver
5 * 2020, 2023 by Marek BehĂșn <kabel@kernel.org>
9 #include <linux/led-class-multicolor.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
15 #define OMNIA_BOARD_LEDS 12
16 #define OMNIA_LED_NUM_CHANNELS 3
18 /* MCU controller commands at I2C address 0x2a */
19 #define OMNIA_MCU_I2C_ADDR 0x2a
21 #define CMD_GET_STATUS_WORD 0x01
22 #define STS_FEATURES_SUPPORTED BIT(2)
24 #define CMD_GET_FEATURES 0x10
25 #define FEAT_LED_GAMMA_CORRECTION BIT(5)
27 /* LED controller commands at I2C address 0x2b */
28 #define CMD_LED_MODE 0x03
29 #define CMD_LED_MODE_LED(l) ((l) & 0x0f)
30 #define CMD_LED_MODE_USER 0x10
32 #define CMD_LED_STATE 0x04
33 #define CMD_LED_STATE_LED(l) ((l) & 0x0f)
34 #define CMD_LED_STATE_ON 0x10
36 #define CMD_LED_COLOR 0x05
37 #define CMD_LED_SET_BRIGHTNESS 0x07
38 #define CMD_LED_GET_BRIGHTNESS 0x08
40 #define CMD_SET_GAMMA_CORRECTION 0x30
41 #define CMD_GET_GAMMA_CORRECTION 0x31
44 struct led_classdev_mc mc_cdev;
45 struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
46 u8 cached_channels[OMNIA_LED_NUM_CHANNELS];
51 #define to_omnia_led(l) container_of(l, struct omnia_led, mc_cdev)
54 struct i2c_client *client;
56 bool has_gamma_correction;
57 struct omnia_led leds[];
60 static int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd, u8 val)
62 u8 buf[2] = { cmd, val };
65 ret = i2c_master_send(client, buf, sizeof(buf));
67 return ret < 0 ? ret : 0;
70 static int omnia_cmd_read_raw(struct i2c_adapter *adapter, u8 addr, u8 cmd,
71 void *reply, size_t len)
73 struct i2c_msg msgs[2];
81 msgs[1].flags = I2C_M_RD;
85 ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
86 if (likely(ret == ARRAY_SIZE(msgs)))
94 static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
99 err = omnia_cmd_read_raw(client->adapter, client->addr, cmd, &reply, 1);
106 static int omnia_led_send_color_cmd(const struct i2c_client *client,
107 struct omnia_led *led)
112 cmd[0] = CMD_LED_COLOR;
114 cmd[2] = led->subled_info[0].brightness;
115 cmd[3] = led->subled_info[1].brightness;
116 cmd[4] = led->subled_info[2].brightness;
118 /* Send the color change command */
119 ret = i2c_master_send(client, cmd, 5);
123 /* Cache the RGB channel brightnesses */
124 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i)
125 led->cached_channels[i] = led->subled_info[i].brightness;
130 /* Determine if the computed RGB channels are different from the cached ones */
131 static bool omnia_led_channels_changed(struct omnia_led *led)
133 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i)
134 if (led->subled_info[i].brightness != led->cached_channels[i])
140 static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
141 enum led_brightness brightness)
143 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
144 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
145 struct omnia_led *led = to_omnia_led(mc_cdev);
148 mutex_lock(&leds->lock);
151 * Only recalculate RGB brightnesses from intensities if brightness is
152 * non-zero (if it is zero and the LED is in HW blinking mode, we use
153 * max_brightness as brightness). Otherwise we won't be using them and
154 * we can save ourselves some software divisions (Omnia's CPU does not
155 * implement the division instruction).
157 if (brightness || led->hwtrig) {
158 led_mc_calc_color_components(mc_cdev, brightness ?:
159 cdev->max_brightness);
162 * Send color command only if brightness is non-zero and the RGB
163 * channel brightnesses changed.
165 if (omnia_led_channels_changed(led))
166 err = omnia_led_send_color_cmd(leds->client, led);
170 * Send on/off state change only if (bool)brightness changed and the LED
171 * is not being blinked by HW.
173 if (!err && !led->hwtrig && !brightness != !led->on) {
174 u8 state = CMD_LED_STATE_LED(led->reg);
177 state |= CMD_LED_STATE_ON;
179 err = omnia_cmd_write_u8(leds->client, CMD_LED_STATE, state);
181 led->on = !!brightness;
184 mutex_unlock(&leds->lock);
189 static struct led_hw_trigger_type omnia_hw_trigger_type;
191 static int omnia_hwtrig_activate(struct led_classdev *cdev)
193 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
194 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
195 struct omnia_led *led = to_omnia_led(mc_cdev);
198 mutex_lock(&leds->lock);
202 * If the LED is off (brightness was set to 0), the last
203 * configured color was not necessarily sent to the MCU.
204 * Recompute with max_brightness and send if needed.
206 led_mc_calc_color_components(mc_cdev, cdev->max_brightness);
208 if (omnia_led_channels_changed(led))
209 err = omnia_led_send_color_cmd(leds->client, led);
213 /* Put the LED into MCU controlled mode */
214 err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
215 CMD_LED_MODE_LED(led->reg));
220 mutex_unlock(&leds->lock);
225 static void omnia_hwtrig_deactivate(struct led_classdev *cdev)
227 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
228 struct omnia_led *led = to_omnia_led(lcdev_to_mccdev(cdev));
231 mutex_lock(&leds->lock);
235 /* Put the LED into software mode */
236 err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
237 CMD_LED_MODE_LED(led->reg) |
240 mutex_unlock(&leds->lock);
243 dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
247 static struct led_trigger omnia_hw_trigger = {
249 .activate = omnia_hwtrig_activate,
250 .deactivate = omnia_hwtrig_deactivate,
251 .trigger_type = &omnia_hw_trigger_type,
254 static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
255 struct device_node *np)
257 struct led_init_data init_data = {};
258 struct device *dev = &client->dev;
259 struct led_classdev *cdev;
262 ret = of_property_read_u32(np, "reg", &led->reg);
263 if (ret || led->reg >= OMNIA_BOARD_LEDS) {
265 "Node %pOF: must contain 'reg' property with values between 0 and %i\n",
266 np, OMNIA_BOARD_LEDS - 1);
270 ret = of_property_read_u32(np, "color", &color);
271 if (ret || color != LED_COLOR_ID_RGB) {
273 "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n",
278 led->subled_info[0].color_index = LED_COLOR_ID_RED;
279 led->subled_info[1].color_index = LED_COLOR_ID_GREEN;
280 led->subled_info[2].color_index = LED_COLOR_ID_BLUE;
282 /* Initial color is white */
283 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) {
284 led->subled_info[i].intensity = 255;
285 led->subled_info[i].brightness = 255;
286 led->subled_info[i].channel = i;
289 led->mc_cdev.subled_info = led->subled_info;
290 led->mc_cdev.num_colors = OMNIA_LED_NUM_CHANNELS;
292 init_data.fwnode = &np->fwnode;
294 cdev = &led->mc_cdev.led_cdev;
295 cdev->max_brightness = 255;
296 cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
297 cdev->trigger_type = &omnia_hw_trigger_type;
299 * Use the omnia-mcu trigger as the default trigger. It may be rewritten
300 * by LED class from the linux,default-trigger property.
302 cdev->default_trigger = omnia_hw_trigger.name;
304 /* put the LED into software mode */
305 ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
306 CMD_LED_MODE_LED(led->reg) |
309 dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
314 /* disable the LED */
315 ret = omnia_cmd_write_u8(client, CMD_LED_STATE,
316 CMD_LED_STATE_LED(led->reg));
318 dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
322 /* Set initial color and cache it */
323 ret = omnia_led_send_color_cmd(client, led);
325 dev_err(dev, "Cannot set LED %pOF initial color: %i\n", np,
330 ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
333 dev_err(dev, "Cannot register LED %pOF: %i\n", np, ret);
341 * On the front panel of the Turris Omnia router there is also a button which
342 * can be used to control the intensity of all the LEDs at once, so that if they
343 * are too bright, user can dim them.
344 * The microcontroller cycles between 8 levels of this global brightness (from
345 * 100% to 0%), but this setting can have any integer value between 0 and 100.
346 * It is therefore convenient to be able to change this setting from software.
347 * We expose this setting via a sysfs attribute file called "brightness". This
348 * file lives in the device directory of the LED controller, not an individual
349 * LED, so it should not confuse users.
351 static ssize_t brightness_show(struct device *dev, struct device_attribute *a,
354 struct i2c_client *client = to_i2c_client(dev);
357 ret = omnia_cmd_read_u8(client, CMD_LED_GET_BRIGHTNESS);
362 return sysfs_emit(buf, "%d\n", ret);
365 static ssize_t brightness_store(struct device *dev, struct device_attribute *a,
366 const char *buf, size_t count)
368 struct i2c_client *client = to_i2c_client(dev);
369 unsigned long brightness;
372 if (kstrtoul(buf, 10, &brightness))
375 if (brightness > 100)
378 err = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
382 static DEVICE_ATTR_RW(brightness);
384 static ssize_t gamma_correction_show(struct device *dev,
385 struct device_attribute *a, char *buf)
387 struct i2c_client *client = to_i2c_client(dev);
388 struct omnia_leds *leds = i2c_get_clientdata(client);
391 if (leds->has_gamma_correction) {
392 ret = omnia_cmd_read_u8(client, CMD_GET_GAMMA_CORRECTION);
399 return sysfs_emit(buf, "%d\n", !!ret);
402 static ssize_t gamma_correction_store(struct device *dev,
403 struct device_attribute *a,
404 const char *buf, size_t count)
406 struct i2c_client *client = to_i2c_client(dev);
407 struct omnia_leds *leds = i2c_get_clientdata(client);
411 if (!leds->has_gamma_correction)
414 if (kstrtobool(buf, &val) < 0)
417 err = omnia_cmd_write_u8(client, CMD_SET_GAMMA_CORRECTION, val);
421 static DEVICE_ATTR_RW(gamma_correction);
423 static struct attribute *omnia_led_controller_attrs[] = {
424 &dev_attr_brightness.attr,
425 &dev_attr_gamma_correction.attr,
428 ATTRIBUTE_GROUPS(omnia_led_controller);
430 static int omnia_mcu_get_features(const struct i2c_client *client)
435 err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
436 CMD_GET_STATUS_WORD, &reply, sizeof(reply));
440 /* Check whether MCU firmware supports the CMD_GET_FEAUTRES command */
441 if (!(le16_to_cpu(reply) & STS_FEATURES_SUPPORTED))
444 err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
445 CMD_GET_FEATURES, &reply, sizeof(reply));
449 return le16_to_cpu(reply);
452 static int omnia_leds_probe(struct i2c_client *client)
454 struct device *dev = &client->dev;
455 struct device_node *np = dev_of_node(dev), *child;
456 struct omnia_leds *leds;
457 struct omnia_led *led;
460 count = of_get_available_child_count(np);
462 dev_err(dev, "LEDs are not defined in device tree!\n");
464 } else if (count > OMNIA_BOARD_LEDS) {
465 dev_err(dev, "Too many LEDs defined in device tree!\n");
469 leds = devm_kzalloc(dev, struct_size(leds, leds, count), GFP_KERNEL);
473 leds->client = client;
474 i2c_set_clientdata(client, leds);
476 ret = omnia_mcu_get_features(client);
478 dev_err(dev, "Cannot determine MCU supported features: %d\n",
483 leds->has_gamma_correction = ret & FEAT_LED_GAMMA_CORRECTION;
484 if (!leds->has_gamma_correction) {
486 "Your board's MCU firmware does not support the LED gamma correction feature.\n");
488 "Consider upgrading MCU firmware with the omnia-mcutool utility.\n");
491 mutex_init(&leds->lock);
493 ret = devm_led_trigger_register(dev, &omnia_hw_trigger);
495 dev_err(dev, "Cannot register private LED trigger: %d\n", ret);
499 led = &leds->leds[0];
500 for_each_available_child_of_node(np, child) {
501 ret = omnia_led_register(client, led, child);
513 static void omnia_leds_remove(struct i2c_client *client)
517 /* put all LEDs into default (HW triggered) mode */
518 omnia_cmd_write_u8(client, CMD_LED_MODE,
519 CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
521 /* set all LEDs color to [255, 255, 255] */
522 buf[0] = CMD_LED_COLOR;
523 buf[1] = OMNIA_BOARD_LEDS;
528 i2c_master_send(client, buf, 5);
531 static const struct of_device_id of_omnia_leds_match[] = {
532 { .compatible = "cznic,turris-omnia-leds", },
536 static const struct i2c_device_id omnia_id[] = {
540 MODULE_DEVICE_TABLE(i2c, omnia_id);
542 static struct i2c_driver omnia_leds_driver = {
543 .probe = omnia_leds_probe,
544 .remove = omnia_leds_remove,
545 .id_table = omnia_id,
547 .name = "leds-turris-omnia",
548 .of_match_table = of_omnia_leds_match,
549 .dev_groups = omnia_led_controller_groups,
553 module_i2c_driver(omnia_leds_driver);
555 MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
556 MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
557 MODULE_LICENSE("GPL v2");