leds-ubnt-ledbar.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/delay.h>
  3. #include <linux/i2c.h>
  4. #include <linux/init.h>
  5. #include <linux/leds.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/of.h>
  9. #include <linux/gpio/consumer.h>
  10. /**
  11. * Driver for the Ubiquiti RGB LED controller (LEDBAR).
  12. * This Controller is based on a Holtek HT32F52241 and connected
  13. * via I2C.
  14. *
  15. * - The Controller needs an enable signal set to high when
  16. * performing a transaction. On the U6-LR, this is located
  17. * at Pin 18 (R6902)
  18. *
  19. * - The Pin is also printed when calling the "usetled" function
  20. * contained in the ubntapp bootloader application.
  21. */
  22. #define UBNT_LEDBAR_MAX_BRIGHTNESS 0xff
  23. #define UBNT_LEDBAR_TRANSACTION_LENGTH 8
  24. #define UBNT_LEDBAR_TRANSACTION_SUCCESS (char) 0xaa
  25. #define UBNT_LEDBAR_TRANSACTION_BLUE_IDX 2
  26. #define UBNT_LEDBAR_TRANSACTION_GREEN_IDX 3
  27. #define UBNT_LEDBAR_TRANSACTION_RED_IDX 4
  28. #define UBNT_LEDBAR_TRANSACTION_LED_COUNT_IDX 6
  29. struct ubnt_ledbar {
  30. struct mutex lock;
  31. u32 led_count;
  32. struct i2c_client *client;
  33. struct led_classdev led_red;
  34. struct led_classdev led_green;
  35. struct led_classdev led_blue;
  36. struct gpio_desc *enable_gpio;
  37. struct gpio_desc *reset_gpio;
  38. };
  39. static void ubnt_ledbar_perform_transaction(struct ubnt_ledbar *ledbar,
  40. const char *transaction, int len,
  41. char *result, int result_len)
  42. {
  43. int i;
  44. for (i = 0; i < len; i++)
  45. i2c_smbus_write_byte(ledbar->client, transaction[i]);
  46. for (i = 0; i < result_len; i++)
  47. result[i] = i2c_smbus_read_byte(ledbar->client);
  48. }
  49. static int ubnt_ledbar_apply_state(struct ubnt_ledbar *ledbar)
  50. {
  51. char setup_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x10, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x11};
  53. char led_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00};
  55. char i2c_response;
  56. int ret = 0;
  57. mutex_lock(&ledbar->lock);
  58. led_msg[UBNT_LEDBAR_TRANSACTION_BLUE_IDX] = ledbar->led_blue.brightness;
  59. led_msg[UBNT_LEDBAR_TRANSACTION_GREEN_IDX] = ledbar->led_green.brightness;
  60. led_msg[UBNT_LEDBAR_TRANSACTION_RED_IDX] = ledbar->led_red.brightness;
  61. led_msg[UBNT_LEDBAR_TRANSACTION_LED_COUNT_IDX] = ledbar->led_count;
  62. gpiod_set_value(ledbar->enable_gpio, 1);
  63. msleep(10);
  64. ubnt_ledbar_perform_transaction(ledbar, setup_msg, sizeof(setup_msg), &i2c_response, sizeof(i2c_response));
  65. if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
  66. dev_err(&ledbar->client->dev, "Error initializing LED transaction: %02hhx\n", i2c_response);
  67. ret = -EINVAL;
  68. goto out_gpio;
  69. }
  70. ubnt_ledbar_perform_transaction(ledbar, led_msg, sizeof(led_msg), &i2c_response, sizeof(i2c_response));
  71. if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
  72. dev_err(&ledbar->client->dev, "Failed LED transaction: %02hhx\n", i2c_response);
  73. ret = -EINVAL;
  74. goto out_gpio;
  75. }
  76. msleep(10);
  77. out_gpio:
  78. gpiod_set_value(ledbar->enable_gpio, 0);
  79. mutex_unlock(&ledbar->lock);
  80. return ret;
  81. }
  82. static void ubnt_ledbar_reset(struct ubnt_ledbar *ledbar)
  83. {
  84. static const char init_msg[16] = {0x02, 0x81, 0xfd, 0x7e,
  85. 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00};
  88. char init_response[4];
  89. if (!ledbar->reset_gpio)
  90. return;
  91. mutex_lock(&ledbar->lock);
  92. gpiod_set_value(ledbar->reset_gpio, 1);
  93. msleep(10);
  94. gpiod_set_value(ledbar->reset_gpio, 0);
  95. msleep(10);
  96. gpiod_set_value(ledbar->enable_gpio, 1);
  97. msleep(10);
  98. ubnt_ledbar_perform_transaction(ledbar, init_msg, sizeof(init_msg), init_response, sizeof(init_response));
  99. msleep(10);
  100. gpiod_set_value(ledbar->enable_gpio, 0);
  101. mutex_unlock(&ledbar->lock);
  102. }
  103. #define UBNT_LEDBAR_CONTROL_RGBS(name) \
  104. static int ubnt_ledbar_set_##name##_brightness(struct led_classdev *led_cdev,\
  105. enum led_brightness value) \
  106. { \
  107. struct ubnt_ledbar *ledbar = \
  108. container_of(led_cdev, struct ubnt_ledbar, led_##name); \
  109. int ret; \
  110. led_cdev->brightness = value; \
  111. ret = ubnt_ledbar_apply_state(ledbar); \
  112. return ret; \
  113. }
  114. UBNT_LEDBAR_CONTROL_RGBS(red);
  115. UBNT_LEDBAR_CONTROL_RGBS(green);
  116. UBNT_LEDBAR_CONTROL_RGBS(blue);
  117. static int ubnt_ledbar_init_led(struct device_node *np, struct ubnt_ledbar *ledbar,
  118. struct led_classdev *led_cdev)
  119. {
  120. struct led_init_data init_data = {};
  121. if (!np)
  122. return 0;
  123. init_data.fwnode = of_fwnode_handle(np);
  124. led_cdev->max_brightness = UBNT_LEDBAR_MAX_BRIGHTNESS;
  125. return devm_led_classdev_register_ext(&ledbar->client->dev, led_cdev, &init_data);
  126. }
  127. static int ubnt_ledbar_probe(struct i2c_client *client)
  128. {
  129. struct device_node *np = client->dev.of_node;
  130. struct ubnt_ledbar *ledbar;
  131. int err;
  132. ledbar = devm_kzalloc(&client->dev, sizeof(*ledbar), GFP_KERNEL);
  133. if (!ledbar)
  134. return -ENOMEM;
  135. ledbar->enable_gpio = devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_LOW);
  136. if (IS_ERR(ledbar->enable_gpio))
  137. return dev_err_probe(&client->dev, PTR_ERR(ledbar->enable_gpio), "Failed to get enable gpio");
  138. ledbar->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
  139. if (IS_ERR(ledbar->reset_gpio))
  140. return dev_err_probe(&client->dev, PTR_ERR(ledbar->reset_gpio), "Failed to get reset gpio");
  141. ledbar->led_count = 1;
  142. of_property_read_u32(np, "led-count", &ledbar->led_count);
  143. ledbar->client = client;
  144. err = devm_mutex_init(&client->dev, &ledbar->lock);
  145. if (err)
  146. return err;
  147. i2c_set_clientdata(client, ledbar);
  148. // Reset and initialize the MCU
  149. ubnt_ledbar_reset(ledbar);
  150. ledbar->led_red.brightness_set_blocking = ubnt_ledbar_set_red_brightness;
  151. ubnt_ledbar_init_led(of_get_child_by_name(np, "red"), ledbar, &ledbar->led_red);
  152. ledbar->led_green.brightness_set_blocking = ubnt_ledbar_set_green_brightness;
  153. ubnt_ledbar_init_led(of_get_child_by_name(np, "green"), ledbar, &ledbar->led_green);
  154. ledbar->led_blue.brightness_set_blocking = ubnt_ledbar_set_blue_brightness;
  155. ubnt_ledbar_init_led(of_get_child_by_name(np, "blue"), ledbar, &ledbar->led_blue);
  156. return ubnt_ledbar_apply_state(ledbar);
  157. }
  158. static const struct i2c_device_id ubnt_ledbar_id[] = {
  159. { "ubnt-ledbar", 0 },
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(i2c, ubnt_ledbar_id);
  163. static const struct of_device_id of_ubnt_ledbar_match[] = {
  164. { .compatible = "ubnt,ledbar", },
  165. {},
  166. };
  167. MODULE_DEVICE_TABLE(of, of_ubnt_ledbar_match);
  168. static struct i2c_driver ubnt_ledbar_driver = {
  169. .driver = {
  170. .name = "ubnt-ledbar",
  171. .of_match_table = of_ubnt_ledbar_match,
  172. },
  173. .probe = ubnt_ledbar_probe,
  174. .id_table = ubnt_ledbar_id,
  175. };
  176. module_i2c_driver(ubnt_ledbar_driver);
  177. MODULE_DESCRIPTION("Ubiquiti LEDBAR driver");
  178. MODULE_AUTHOR("David Bauer <[email protected]>");
  179. MODULE_LICENSE("GPL v2");