0073-BCM270x_DT-Add-pwr_led-and-the-required-input-trigge.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. From 1d7fe535e241fb25ba677a9f152d4e233b10273b Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <phil@raspberrypi.org>
  3. Date: Fri, 6 Feb 2015 13:50:57 +0000
  4. Subject: [PATCH] BCM270x_DT: Add pwr_led, and the required "input" trigger
  5. The "input" trigger makes the associated GPIO an input. This is to support
  6. the Raspberry Pi PWR LED, which is driven by external hardware in normal use.
  7. N.B. pwr_led is not available on Model A or B boards.
  8. leds-gpio: Implement the brightness_get method
  9. The power LED uses some clever logic that means it is driven
  10. by a voltage measuring circuit when configured as input, otherwise
  11. it is driven by the GPIO output value. This patch wires up the
  12. brightness_get method for leds-gpio so that user-space can monitor
  13. the LED value via /sys/class/gpio/led1/brightness. Using the input
  14. trigger this returns an indication of the system power health,
  15. otherwise it is just whatever value the trigger has written most
  16. recently.
  17. See: https://github.com/raspberrypi/linux/issues/1064
  18. ---
  19. drivers/leds/leds-gpio.c | 18 +++++++++++-
  20. drivers/leds/trigger/Kconfig | 7 +++++
  21. drivers/leds/trigger/Makefile | 1 +
  22. drivers/leds/trigger/ledtrig-input.c | 54 ++++++++++++++++++++++++++++++++++++
  23. include/linux/leds.h | 3 ++
  24. 5 files changed, 82 insertions(+), 1 deletion(-)
  25. create mode 100644 drivers/leds/trigger/ledtrig-input.c
  26. --- a/drivers/leds/leds-gpio.c
  27. +++ b/drivers/leds/leds-gpio.c
  28. @@ -41,6 +41,13 @@ static void gpio_led_work(struct work_st
  29. led_dat->platform_gpio_blink_set(led_dat->gpiod,
  30. led_dat->new_level, NULL, NULL);
  31. led_dat->blinking = 0;
  32. + } else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
  33. + gpiod_direction_input(led_dat->gpiod);
  34. + led_dat->cdev.flags &= ~SET_GPIO_INPUT;
  35. + }
  36. + else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
  37. + gpiod_direction_output(led_dat->gpiod, led_dat->new_level);
  38. + led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
  39. } else
  40. gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level);
  41. }
  42. @@ -61,7 +68,8 @@ static void gpio_led_set(struct led_clas
  43. * seem to have a reliable way to know if we're already in one; so
  44. * let's just assume the worst.
  45. */
  46. - if (led_dat->can_sleep) {
  47. + if (led_dat->can_sleep ||
  48. + (led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) )) {
  49. led_dat->new_level = level;
  50. schedule_work(&led_dat->work);
  51. } else {
  52. @@ -74,6 +82,13 @@ static void gpio_led_set(struct led_clas
  53. }
  54. }
  55. +static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
  56. +{
  57. + struct gpio_led_data *led_dat =
  58. + container_of(led_cdev, struct gpio_led_data, cdev);
  59. + return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
  60. +}
  61. +
  62. static int gpio_blink_set(struct led_classdev *led_cdev,
  63. unsigned long *delay_on, unsigned long *delay_off)
  64. {
  65. @@ -129,6 +144,7 @@ static int create_gpio_led(const struct
  66. led_dat->cdev.blink_set = gpio_blink_set;
  67. }
  68. led_dat->cdev.brightness_set = gpio_led_set;
  69. + led_dat->cdev.brightness_get = gpio_led_get;
  70. if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
  71. state = !!gpiod_get_value_cansleep(led_dat->gpiod);
  72. else
  73. --- a/drivers/leds/trigger/Kconfig
  74. +++ b/drivers/leds/trigger/Kconfig
  75. @@ -115,4 +115,11 @@ config LEDS_TRIGGER_NETDEV
  76. This allows LEDs to be controlled by network device activity.
  77. If unsure, say Y.
  78. +config LEDS_TRIGGER_INPUT
  79. + tristate "LED Input Trigger"
  80. + depends on LEDS_TRIGGERS
  81. + help
  82. + This allows the GPIOs assigned to be LEDs to be initialised to inputs.
  83. + If unsure, say Y.
  84. +
  85. endif # LEDS_TRIGGERS
  86. --- a/drivers/leds/trigger/Makefile
  87. +++ b/drivers/leds/trigger/Makefile
  88. @@ -8,3 +8,4 @@ obj-$(CONFIG_LEDS_TRIGGER_CPU) += ledtr
  89. obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
  90. obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
  91. obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
  92. +obj-$(CONFIG_LEDS_TRIGGER_INPUT) += ledtrig-input.o
  93. --- /dev/null
  94. +++ b/drivers/leds/trigger/ledtrig-input.c
  95. @@ -0,0 +1,54 @@
  96. +/*
  97. + * Set LED GPIO to Input "Trigger"
  98. + *
  99. + * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
  100. + *
  101. + * Based on Nick Forbes's ledtrig-default-on.c.
  102. + *
  103. + * This program is free software; you can redistribute it and/or modify
  104. + * it under the terms of the GNU General Public License version 2 as
  105. + * published by the Free Software Foundation.
  106. + *
  107. + */
  108. +
  109. +#include <linux/module.h>
  110. +#include <linux/kernel.h>
  111. +#include <linux/init.h>
  112. +#include <linux/leds.h>
  113. +#include <linux/gpio.h>
  114. +#include "../leds.h"
  115. +
  116. +static void input_trig_activate(struct led_classdev *led_cdev)
  117. +{
  118. + led_cdev->flags |= SET_GPIO_INPUT;
  119. + led_set_brightness_async(led_cdev, 0);
  120. +}
  121. +
  122. +static void input_trig_deactivate(struct led_classdev *led_cdev)
  123. +{
  124. + led_cdev->flags |= SET_GPIO_OUTPUT;
  125. + led_set_brightness_async(led_cdev, 0);
  126. +}
  127. +
  128. +static struct led_trigger input_led_trigger = {
  129. + .name = "input",
  130. + .activate = input_trig_activate,
  131. + .deactivate = input_trig_deactivate,
  132. +};
  133. +
  134. +static int __init input_trig_init(void)
  135. +{
  136. + return led_trigger_register(&input_led_trigger);
  137. +}
  138. +
  139. +static void __exit input_trig_exit(void)
  140. +{
  141. + led_trigger_unregister(&input_led_trigger);
  142. +}
  143. +
  144. +module_init(input_trig_init);
  145. +module_exit(input_trig_exit);
  146. +
  147. +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.org>");
  148. +MODULE_DESCRIPTION("Set LED GPIO to Input \"trigger\"");
  149. +MODULE_LICENSE("GPL");
  150. --- a/include/linux/leds.h
  151. +++ b/include/linux/leds.h
  152. @@ -48,6 +48,9 @@ struct led_classdev {
  153. #define SET_BRIGHTNESS_ASYNC (1 << 21)
  154. #define SET_BRIGHTNESS_SYNC (1 << 22)
  155. #define LED_DEV_CAP_FLASH (1 << 23)
  156. + /* Additions for Raspberry Pi PWR LED */
  157. +#define SET_GPIO_INPUT (1 << 30)
  158. +#define SET_GPIO_OUTPUT (1 << 31)
  159. /* Set LED brightness level */
  160. /* Must not sleep, use a workqueue if needed */