1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * CZ.NIC's Turris Omnia LEDs driver 4 * 5 * 2020, 2023, 2024 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 <linux/turris-omnia-mcu-interface.h> 14 15 #define OMNIA_BOARD_LEDS 12 16 #define OMNIA_LED_NUM_CHANNELS 3 17 18 /* MCU controller I2C address 0x2a, needed for detecting MCU features */ 19 #define OMNIA_MCU_I2C_ADDR 0x2a 20 21 /** 22 * struct omnia_led - per-LED part of driver private data structure 23 * @mc_cdev: multi-color LED class device 24 * @subled_info: per-channel information 25 * @cached_channels: cached values of per-channel brightness that was sent to the MCU 26 * @on: whether the LED was set on 27 * @hwtrig: whether the LED blinking was offloaded to the MCU 28 * @reg: LED identifier to the MCU 29 */ 30 struct omnia_led { 31 struct led_classdev_mc mc_cdev; 32 struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS]; 33 u8 cached_channels[OMNIA_LED_NUM_CHANNELS]; 34 bool on, hwtrig; 35 int reg; 36 }; 37 38 #define to_omnia_led(l) container_of(l, struct omnia_led, mc_cdev) 39 40 /** 41 * struct omnia_leds - driver private data structure 42 * @client: I2C client device 43 * @lock: mutex to protect cached state 44 * @has_gamma_correction: whether the MCU firmware supports gamma correction 45 * @brightness_knode: kernel node of the "brightness" device sysfs attribute (this is the 46 * driver specific global brightness, not the LED classdev brightness) 47 * @leds: flexible array of per-LED data 48 */ 49 struct omnia_leds { 50 struct i2c_client *client; 51 struct mutex lock; 52 bool has_gamma_correction; 53 struct kernfs_node *brightness_knode; 54 struct omnia_led leds[]; 55 }; 56 57 static int omnia_cmd_set_color(const struct i2c_client *client, u8 led, u8 r, u8 g, u8 b) 58 { 59 u8 buf[5] = { OMNIA_CMD_LED_COLOR, led, r, g, b }; 60 61 return omnia_cmd_write(client, buf, sizeof(buf)); 62 } 63 64 static int omnia_led_send_color_cmd(const struct i2c_client *client, 65 struct omnia_led *led) 66 { 67 int ret; 68 69 /* Send the color change command */ 70 ret = omnia_cmd_set_color(client, led->reg, led->subled_info[0].brightness, 71 led->subled_info[1].brightness, led->subled_info[2].brightness); 72 if (ret < 0) 73 return ret; 74 75 /* Cache the RGB channel brightnesses */ 76 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) 77 led->cached_channels[i] = led->subled_info[i].brightness; 78 79 return 0; 80 } 81 82 /* Determine if the computed RGB channels are different from the cached ones */ 83 static bool omnia_led_channels_changed(struct omnia_led *led) 84 { 85 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) 86 if (led->subled_info[i].brightness != led->cached_channels[i]) 87 return true; 88 89 return false; 90 } 91 92 static int omnia_led_brightness_set_blocking(struct led_classdev *cdev, 93 enum led_brightness brightness) 94 { 95 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); 96 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent); 97 struct omnia_led *led = to_omnia_led(mc_cdev); 98 int err = 0; 99 100 mutex_lock(&leds->lock); 101 102 /* 103 * Only recalculate RGB brightnesses from intensities if brightness is 104 * non-zero (if it is zero and the LED is in HW blinking mode, we use 105 * max_brightness as brightness). Otherwise we won't be using them and 106 * we can save ourselves some software divisions (Omnia's CPU does not 107 * implement the division instruction). 108 */ 109 if (brightness || led->hwtrig) { 110 led_mc_calc_color_components(mc_cdev, brightness ?: 111 cdev->max_brightness); 112 113 /* 114 * Send color command only if brightness is non-zero and the RGB 115 * channel brightnesses changed. 116 */ 117 if (omnia_led_channels_changed(led)) 118 err = omnia_led_send_color_cmd(leds->client, led); 119 } 120 121 /* 122 * Send on/off state change only if (bool)brightness changed and the LED 123 * is not being blinked by HW. 124 */ 125 if (!err && !led->hwtrig && !brightness != !led->on) { 126 u8 state = OMNIA_CMD_LED_STATE_LED(led->reg); 127 128 if (brightness) 129 state |= OMNIA_CMD_LED_STATE_ON; 130 131 err = omnia_cmd_write_u8(leds->client, OMNIA_CMD_LED_STATE, state); 132 if (!err) 133 led->on = !!brightness; 134 } 135 136 mutex_unlock(&leds->lock); 137 138 return err; 139 } 140 141 static struct led_hw_trigger_type omnia_hw_trigger_type; 142 143 static int omnia_hwtrig_activate(struct led_classdev *cdev) 144 { 145 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); 146 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent); 147 struct omnia_led *led = to_omnia_led(mc_cdev); 148 int err = 0; 149 150 mutex_lock(&leds->lock); 151 152 if (!led->on) { 153 /* 154 * If the LED is off (brightness was set to 0), the last 155 * configured color was not necessarily sent to the MCU. 156 * Recompute with max_brightness and send if needed. 157 */ 158 led_mc_calc_color_components(mc_cdev, cdev->max_brightness); 159 160 if (omnia_led_channels_changed(led)) 161 err = omnia_led_send_color_cmd(leds->client, led); 162 } 163 164 if (!err) { 165 /* Put the LED into MCU controlled mode */ 166 err = omnia_cmd_write_u8(leds->client, OMNIA_CMD_LED_MODE, 167 OMNIA_CMD_LED_MODE_LED(led->reg)); 168 if (!err) 169 led->hwtrig = true; 170 } 171 172 mutex_unlock(&leds->lock); 173 174 return err; 175 } 176 177 static void omnia_hwtrig_deactivate(struct led_classdev *cdev) 178 { 179 struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent); 180 struct omnia_led *led = to_omnia_led(lcdev_to_mccdev(cdev)); 181 int err; 182 183 mutex_lock(&leds->lock); 184 185 led->hwtrig = false; 186 187 /* Put the LED into software mode */ 188 err = omnia_cmd_write_u8(leds->client, OMNIA_CMD_LED_MODE, 189 OMNIA_CMD_LED_MODE_LED(led->reg) | OMNIA_CMD_LED_MODE_USER); 190 191 mutex_unlock(&leds->lock); 192 193 if (err) 194 dev_err(cdev->dev, "Cannot put LED to software mode: %i\n", 195 err); 196 } 197 198 static struct led_trigger omnia_hw_trigger = { 199 .name = "omnia-mcu", 200 .activate = omnia_hwtrig_activate, 201 .deactivate = omnia_hwtrig_deactivate, 202 .trigger_type = &omnia_hw_trigger_type, 203 }; 204 205 static int omnia_led_register(struct i2c_client *client, struct omnia_led *led, 206 struct device_node *np) 207 { 208 struct led_init_data init_data = {}; 209 struct device *dev = &client->dev; 210 struct led_classdev *cdev; 211 int ret, color; 212 213 ret = of_property_read_u32(np, "reg", &led->reg); 214 if (ret || led->reg >= OMNIA_BOARD_LEDS) { 215 dev_warn(dev, 216 "Node %pOF: must contain 'reg' property with values between 0 and %i\n", 217 np, OMNIA_BOARD_LEDS - 1); 218 return 0; 219 } 220 221 ret = of_property_read_u32(np, "color", &color); 222 if (ret || color != LED_COLOR_ID_RGB) { 223 dev_warn(dev, 224 "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n", 225 np); 226 return 0; 227 } 228 229 led->subled_info[0].color_index = LED_COLOR_ID_RED; 230 led->subled_info[1].color_index = LED_COLOR_ID_GREEN; 231 led->subled_info[2].color_index = LED_COLOR_ID_BLUE; 232 233 /* Initial color is white */ 234 for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) { 235 led->subled_info[i].intensity = 255; 236 led->subled_info[i].brightness = 255; 237 led->subled_info[i].channel = i; 238 } 239 240 led->mc_cdev.subled_info = led->subled_info; 241 led->mc_cdev.num_colors = OMNIA_LED_NUM_CHANNELS; 242 243 init_data.fwnode = &np->fwnode; 244 245 cdev = &led->mc_cdev.led_cdev; 246 cdev->max_brightness = 255; 247 cdev->brightness_set_blocking = omnia_led_brightness_set_blocking; 248 cdev->trigger_type = &omnia_hw_trigger_type; 249 /* 250 * Use the omnia-mcu trigger as the default trigger. It may be rewritten 251 * by LED class from the linux,default-trigger property. 252 */ 253 cdev->default_trigger = omnia_hw_trigger.name; 254 255 /* Put the LED into software mode */ 256 ret = omnia_cmd_write_u8(client, OMNIA_CMD_LED_MODE, OMNIA_CMD_LED_MODE_LED(led->reg) | 257 OMNIA_CMD_LED_MODE_USER); 258 if (ret) 259 return dev_err_probe(dev, ret, "Cannot set LED %pOF to software mode\n", np); 260 261 /* Disable the LED */ 262 ret = omnia_cmd_write_u8(client, OMNIA_CMD_LED_STATE, OMNIA_CMD_LED_STATE_LED(led->reg)); 263 if (ret) 264 return dev_err_probe(dev, ret, "Cannot set LED %pOF brightness\n", np); 265 266 /* Set initial color and cache it */ 267 ret = omnia_led_send_color_cmd(client, led); 268 if (ret < 0) 269 return dev_err_probe(dev, ret, "Cannot set LED %pOF initial color\n", np); 270 271 ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev, 272 &init_data); 273 if (ret < 0) 274 return dev_err_probe(dev, ret, "Cannot register LED %pOF\n", np); 275 276 return 1; 277 } 278 279 /* 280 * On the front panel of the Turris Omnia router there is also a button which 281 * can be used to control the intensity of all the LEDs at once, so that if they 282 * are too bright, user can dim them. 283 * The microcontroller cycles between 8 levels of this global brightness (from 284 * 100% to 0%), but this setting can have any integer value between 0 and 100. 285 * It is therefore convenient to be able to change this setting from software. 286 * We expose this setting via a sysfs attribute file called "brightness". This 287 * file lives in the device directory of the LED controller, not an individual 288 * LED, so it should not confuse users. 289 */ 290 static ssize_t brightness_show(struct device *dev, struct device_attribute *a, 291 char *buf) 292 { 293 struct i2c_client *client = to_i2c_client(dev); 294 u8 reply; 295 int err; 296 297 err = omnia_cmd_read_u8(client, OMNIA_CMD_GET_BRIGHTNESS, &reply); 298 if (err < 0) 299 return err; 300 301 return sysfs_emit(buf, "%d\n", reply); 302 } 303 304 static ssize_t brightness_store(struct device *dev, struct device_attribute *a, 305 const char *buf, size_t count) 306 { 307 struct i2c_client *client = to_i2c_client(dev); 308 unsigned long brightness; 309 int err; 310 311 if (kstrtoul(buf, 10, &brightness)) 312 return -EINVAL; 313 314 if (brightness > 100) 315 return -EINVAL; 316 317 err = omnia_cmd_write_u8(client, OMNIA_CMD_SET_BRIGHTNESS, brightness); 318 319 return err ?: count; 320 } 321 static DEVICE_ATTR_RW(brightness); 322 323 static ssize_t gamma_correction_show(struct device *dev, 324 struct device_attribute *a, char *buf) 325 { 326 struct i2c_client *client = to_i2c_client(dev); 327 struct omnia_leds *leds = i2c_get_clientdata(client); 328 u8 reply = 0; 329 int err; 330 331 if (leds->has_gamma_correction) { 332 err = omnia_cmd_read_u8(client, OMNIA_CMD_GET_GAMMA_CORRECTION, &reply); 333 if (err < 0) 334 return err; 335 } 336 337 return sysfs_emit(buf, "%d\n", !!reply); 338 } 339 340 static ssize_t gamma_correction_store(struct device *dev, 341 struct device_attribute *a, 342 const char *buf, size_t count) 343 { 344 struct i2c_client *client = to_i2c_client(dev); 345 struct omnia_leds *leds = i2c_get_clientdata(client); 346 bool val; 347 int err; 348 349 if (!leds->has_gamma_correction) 350 return -EOPNOTSUPP; 351 352 if (kstrtobool(buf, &val) < 0) 353 return -EINVAL; 354 355 err = omnia_cmd_write_u8(client, OMNIA_CMD_SET_GAMMA_CORRECTION, val); 356 357 return err ?: count; 358 } 359 static DEVICE_ATTR_RW(gamma_correction); 360 361 static struct attribute *omnia_led_controller_attrs[] = { 362 &dev_attr_brightness.attr, 363 &dev_attr_gamma_correction.attr, 364 NULL, 365 }; 366 ATTRIBUTE_GROUPS(omnia_led_controller); 367 368 static irqreturn_t omnia_brightness_changed_threaded_fn(int irq, void *data) 369 { 370 struct omnia_leds *leds = data; 371 372 if (unlikely(!leds->brightness_knode)) { 373 /* 374 * Note that sysfs_get_dirent() may sleep. This is okay, because we are in threaded 375 * context. 376 */ 377 leds->brightness_knode = sysfs_get_dirent(leds->client->dev.kobj.sd, "brightness"); 378 if (!leds->brightness_knode) 379 return IRQ_NONE; 380 } 381 382 sysfs_notify_dirent(leds->brightness_knode); 383 384 return IRQ_HANDLED; 385 } 386 387 static void omnia_brightness_knode_put(void *data) 388 { 389 struct omnia_leds *leds = data; 390 391 if (leds->brightness_knode) 392 sysfs_put(leds->brightness_knode); 393 } 394 395 static int omnia_request_brightness_irq(struct omnia_leds *leds) 396 { 397 struct device *dev = &leds->client->dev; 398 int ret; 399 400 if (!leds->client->irq) { 401 dev_info(dev, 402 "Brightness change interrupt supported by MCU firmware but not described in device-tree\n"); 403 404 return 0; 405 } 406 407 /* 408 * Registering the brightness_knode destructor before requesting the IRQ ensures that on 409 * removal the brightness_knode sysfs node is put only after the IRQ is freed. 410 * This is needed because the interrupt handler uses the knode. 411 */ 412 ret = devm_add_action(dev, omnia_brightness_knode_put, leds); 413 if (ret < 0) 414 return ret; 415 416 return devm_request_threaded_irq(dev, leds->client->irq, NULL, 417 omnia_brightness_changed_threaded_fn, IRQF_ONESHOT, 418 "leds-turris-omnia", leds); 419 } 420 421 static int omnia_mcu_get_features(const struct i2c_client *mcu_client) 422 { 423 u16 reply; 424 int err; 425 426 err = omnia_cmd_read_u16(mcu_client, OMNIA_CMD_GET_STATUS_WORD, &reply); 427 if (err) 428 return err; 429 430 /* Check whether MCU firmware supports the OMNIA_CMD_GET_FEAUTRES command */ 431 if (!(reply & OMNIA_STS_FEATURES_SUPPORTED)) 432 return 0; 433 434 err = omnia_cmd_read_u16(mcu_client, OMNIA_CMD_GET_FEATURES, &reply); 435 if (err) 436 return err; 437 438 return reply; 439 } 440 441 static int omnia_match_mcu_client(struct device *dev, void *data) 442 { 443 struct i2c_client *client; 444 445 client = i2c_verify_client(dev); 446 if (!client) 447 return 0; 448 449 return client->addr == OMNIA_MCU_I2C_ADDR; 450 } 451 452 static int omnia_find_mcu_and_get_features(struct device *dev) 453 { 454 struct device *mcu_dev; 455 int ret; 456 457 mcu_dev = device_find_child(dev->parent, NULL, omnia_match_mcu_client); 458 if (!mcu_dev) 459 return -ENODEV; 460 461 ret = omnia_mcu_get_features(i2c_verify_client(mcu_dev)); 462 463 put_device(mcu_dev); 464 465 return ret; 466 } 467 468 static int omnia_leds_probe(struct i2c_client *client) 469 { 470 struct device *dev = &client->dev; 471 struct device_node *np = dev_of_node(dev); 472 struct omnia_leds *leds; 473 struct omnia_led *led; 474 int ret, count; 475 476 count = of_get_available_child_count(np); 477 if (count == 0) 478 return dev_err_probe(dev, -ENODEV, "LEDs are not defined in device tree!\n"); 479 if (count > OMNIA_BOARD_LEDS) 480 return dev_err_probe(dev, -EINVAL, "Too many LEDs defined in device tree!\n"); 481 482 leds = devm_kzalloc(dev, struct_size(leds, leds, count), GFP_KERNEL); 483 if (!leds) 484 return -ENOMEM; 485 486 leds->client = client; 487 i2c_set_clientdata(client, leds); 488 489 ret = omnia_find_mcu_and_get_features(dev); 490 if (ret < 0) 491 return dev_err_probe(dev, ret, "Cannot determine MCU supported features\n"); 492 493 leds->has_gamma_correction = ret & OMNIA_FEAT_LED_GAMMA_CORRECTION; 494 495 if (ret & OMNIA_FEAT_BRIGHTNESS_INT) { 496 ret = omnia_request_brightness_irq(leds); 497 if (ret < 0) 498 return dev_err_probe(dev, ret, "Cannot request brightness IRQ\n"); 499 } 500 501 mutex_init(&leds->lock); 502 503 ret = devm_led_trigger_register(dev, &omnia_hw_trigger); 504 if (ret < 0) 505 return dev_err_probe(dev, ret, "Cannot register private LED trigger\n"); 506 507 led = &leds->leds[0]; 508 for_each_available_child_of_node_scoped(np, child) { 509 ret = omnia_led_register(client, led, child); 510 if (ret < 0) 511 return ret; 512 513 led += ret; 514 } 515 516 return 0; 517 } 518 519 static void omnia_leds_remove(struct i2c_client *client) 520 { 521 /* Put all LEDs into default (HW triggered) mode */ 522 omnia_cmd_write_u8(client, OMNIA_CMD_LED_MODE, OMNIA_CMD_LED_MODE_LED(OMNIA_BOARD_LEDS)); 523 524 /* Set all LEDs color to [255, 255, 255] */ 525 omnia_cmd_set_color(client, OMNIA_BOARD_LEDS, 255, 255, 255); 526 } 527 528 static const struct of_device_id of_omnia_leds_match[] = { 529 { .compatible = "cznic,turris-omnia-leds", }, 530 {}, 531 }; 532 MODULE_DEVICE_TABLE(of, of_omnia_leds_match); 533 534 static const struct i2c_device_id omnia_id[] = { 535 { "omnia" }, 536 { } 537 }; 538 MODULE_DEVICE_TABLE(i2c, omnia_id); 539 540 static struct i2c_driver omnia_leds_driver = { 541 .probe = omnia_leds_probe, 542 .remove = omnia_leds_remove, 543 .id_table = omnia_id, 544 .driver = { 545 .name = "leds-turris-omnia", 546 .of_match_table = of_omnia_leds_match, 547 .dev_groups = omnia_led_controller_groups, 548 }, 549 }; 550 551 module_i2c_driver(omnia_leds_driver); 552 553 MODULE_AUTHOR("Marek Behun <kabel@kernel.org>"); 554 MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs"); 555 MODULE_LICENSE("GPL v2"); 556