leds-turris-omnia.c 7.6 KB

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