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

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