815-v6.7-3-leds-turris-omnia-Support-HW-controlled-mode-via-pri.patch 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. From e965e0f6de60874fc0a0caed9a9e0122999e0c7b Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Marek=20Beh=C3=BAn?= <[email protected]>
  3. Date: Mon, 18 Sep 2023 18:11:03 +0200
  4. Subject: [PATCH 5/6] leds: turris-omnia: Support HW controlled mode via
  5. private trigger
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. Add support for enabling MCU controlled mode of the Turris Omnia LEDs
  10. via a LED private trigger called "omnia-mcu". Recall that private LED
  11. triggers will only be listed in the sysfs trigger file for LEDs that
  12. support them (currently there is no user of this mechanism).
  13. When in MCU controlled mode, the user can still set LED color, but the
  14. blinking is done by MCU, which does different things for different LEDs:
  15. - WAN LED is blinked according to the LED[0] pin of the WAN PHY
  16. - LAN LEDs are blinked according to the LED[0] output of the
  17. corresponding port of the LAN switch
  18. - PCIe LEDs are blinked according to the logical OR of the MiniPCIe port
  19. LED pins
  20. In the future I want to make the netdev trigger to transparently offload
  21. the blinking to the HW if user sets compatible settings for the netdev
  22. trigger (for LEDs associated with network devices).
  23. There was some work on this already, and hopefully we will be able to
  24. complete it sometime, but for now there are still multiple blockers for
  25. this, and even if there weren't, we still would not be able to configure
  26. HW controlled mode for the LEDs associated with MiniPCIe ports.
  27. In the meantime let's support HW controlled mode via the private LED
  28. trigger mechanism. If, in the future, we manage to complete the netdev
  29. trigger offloading, we can still keep this private trigger for backwards
  30. compatibility, if needed.
  31. We also set "omnia-mcu" to cdev->default_trigger, so that the MCU keeps
  32. control until the user first wants to take over it. If a different
  33. default trigger is specified in device-tree via the
  34. 'linux,default-trigger' property, LED class will overwrite
  35. cdev->default_trigger, and so the DT property will be respected.
  36. Signed-off-by: Marek Behún <[email protected]>
  37. Link: https://lore.kernel.org/r/[email protected]
  38. Signed-off-by: Lee Jones <[email protected]>
  39. ---
  40. drivers/leds/Kconfig | 1 +
  41. drivers/leds/leds-turris-omnia.c | 98 +++++++++++++++++++++++++++++---
  42. 2 files changed, 91 insertions(+), 8 deletions(-)
  43. --- a/drivers/leds/Kconfig
  44. +++ b/drivers/leds/Kconfig
  45. @@ -164,6 +164,7 @@ config LEDS_TURRIS_OMNIA
  46. depends on I2C
  47. depends on MACH_ARMADA_38X || COMPILE_TEST
  48. depends on OF
  49. + select LEDS_TRIGGERS
  50. help
  51. This option enables basic support for the LEDs found on the front
  52. side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
  53. --- a/drivers/leds/leds-turris-omnia.c
  54. +++ b/drivers/leds/leds-turris-omnia.c
  55. @@ -31,7 +31,7 @@ struct omnia_led {
  56. struct led_classdev_mc mc_cdev;
  57. struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
  58. u8 cached_channels[OMNIA_LED_NUM_CHANNELS];
  59. - bool on;
  60. + bool on, hwtrig;
  61. int reg;
  62. };
  63. @@ -120,12 +120,14 @@ static int omnia_led_brightness_set_bloc
  64. /*
  65. * Only recalculate RGB brightnesses from intensities if brightness is
  66. - * non-zero. Otherwise we won't be using them and we can save ourselves
  67. - * some software divisions (Omnia's CPU does not implement the division
  68. - * instruction).
  69. + * non-zero (if it is zero and the LED is in HW blinking mode, we use
  70. + * max_brightness as brightness). Otherwise we won't be using them and
  71. + * we can save ourselves some software divisions (Omnia's CPU does not
  72. + * implement the division instruction).
  73. */
  74. - if (brightness) {
  75. - led_mc_calc_color_components(mc_cdev, brightness);
  76. + if (brightness || led->hwtrig) {
  77. + led_mc_calc_color_components(mc_cdev, brightness ?:
  78. + cdev->max_brightness);
  79. /*
  80. * Send color command only if brightness is non-zero and the RGB
  81. @@ -135,8 +137,11 @@ static int omnia_led_brightness_set_bloc
  82. err = omnia_led_send_color_cmd(leds->client, led);
  83. }
  84. - /* Send on/off state change only if (bool)brightness changed */
  85. - if (!err && !brightness != !led->on) {
  86. + /*
  87. + * Send on/off state change only if (bool)brightness changed and the LED
  88. + * is not being blinked by HW.
  89. + */
  90. + if (!err && !led->hwtrig && !brightness != !led->on) {
  91. u8 state = CMD_LED_STATE_LED(led->reg);
  92. if (brightness)
  93. @@ -152,6 +157,71 @@ static int omnia_led_brightness_set_bloc
  94. return err;
  95. }
  96. +static struct led_hw_trigger_type omnia_hw_trigger_type;
  97. +
  98. +static int omnia_hwtrig_activate(struct led_classdev *cdev)
  99. +{
  100. + struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
  101. + struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
  102. + struct omnia_led *led = to_omnia_led(mc_cdev);
  103. + int err = 0;
  104. +
  105. + mutex_lock(&leds->lock);
  106. +
  107. + if (!led->on) {
  108. + /*
  109. + * If the LED is off (brightness was set to 0), the last
  110. + * configured color was not necessarily sent to the MCU.
  111. + * Recompute with max_brightness and send if needed.
  112. + */
  113. + led_mc_calc_color_components(mc_cdev, cdev->max_brightness);
  114. +
  115. + if (omnia_led_channels_changed(led))
  116. + err = omnia_led_send_color_cmd(leds->client, led);
  117. + }
  118. +
  119. + if (!err) {
  120. + /* Put the LED into MCU controlled mode */
  121. + err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
  122. + CMD_LED_MODE_LED(led->reg));
  123. + if (!err)
  124. + led->hwtrig = true;
  125. + }
  126. +
  127. + mutex_unlock(&leds->lock);
  128. +
  129. + return err;
  130. +}
  131. +
  132. +static void omnia_hwtrig_deactivate(struct led_classdev *cdev)
  133. +{
  134. + struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
  135. + struct omnia_led *led = to_omnia_led(lcdev_to_mccdev(cdev));
  136. + int err;
  137. +
  138. + mutex_lock(&leds->lock);
  139. +
  140. + led->hwtrig = false;
  141. +
  142. + /* Put the LED into software mode */
  143. + err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
  144. + CMD_LED_MODE_LED(led->reg) |
  145. + CMD_LED_MODE_USER);
  146. +
  147. + mutex_unlock(&leds->lock);
  148. +
  149. + if (err < 0)
  150. + dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
  151. + err);
  152. +}
  153. +
  154. +static struct led_trigger omnia_hw_trigger = {
  155. + .name = "omnia-mcu",
  156. + .activate = omnia_hwtrig_activate,
  157. + .deactivate = omnia_hwtrig_deactivate,
  158. + .trigger_type = &omnia_hw_trigger_type,
  159. +};
  160. +
  161. static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
  162. struct device_node *np)
  163. {
  164. @@ -195,6 +265,12 @@ static int omnia_led_register(struct i2c
  165. cdev = &led->mc_cdev.led_cdev;
  166. cdev->max_brightness = 255;
  167. cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
  168. + cdev->trigger_type = &omnia_hw_trigger_type;
  169. + /*
  170. + * Use the omnia-mcu trigger as the default trigger. It may be rewritten
  171. + * by LED class from the linux,default-trigger property.
  172. + */
  173. + cdev->default_trigger = omnia_hw_trigger.name;
  174. /* put the LED into software mode */
  175. ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
  176. @@ -309,6 +385,12 @@ static int omnia_leds_probe(struct i2c_c
  177. mutex_init(&leds->lock);
  178. + ret = devm_led_trigger_register(dev, &omnia_hw_trigger);
  179. + if (ret < 0) {
  180. + dev_err(dev, "Cannot register private LED trigger: %d\n", ret);
  181. + return ret;
  182. + }
  183. +
  184. led = &leds->leds[0];
  185. for_each_available_child_of_node(np, child) {
  186. ret = omnia_led_register(client, led, child);