1 From 3bb38201fc6c1aef7709c3c744824db23f06c629 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"
7 The "input" trigger makes the associated GPIO an input. This is to support
8 the Raspberry Pi PWR LED, which is driven by external hardware in normal use.
10 N.B. pwr_led is not available on Model A or B boards.
12 leds-gpio: Implement the brightness_get method
14 The power LED uses some clever logic that means it is driven
15 by a voltage measuring circuit when configured as input, otherwise
16 it is driven by the GPIO output value. This patch wires up the
17 brightness_get method for leds-gpio so that user-space can monitor
18 the LED value via /sys/class/gpio/led1/brightness. Using the input
19 trigger this returns an indication of the system power health,
20 otherwise it is just whatever value the trigger has written most
23 See: https://github.com/raspberrypi/linux/issues/1064
25 drivers/leds/leds-gpio.c | 17 ++++++++-
26 drivers/leds/trigger/Kconfig | 7 ++++
27 drivers/leds/trigger/Makefile | 1 +
28 drivers/leds/trigger/ledtrig-input.c | 55 ++++++++++++++++++++++++++++
29 include/linux/leds.h | 3 ++
30 5 files changed, 82 insertions(+), 1 deletion(-)
31 create mode 100644 drivers/leds/trigger/ledtrig-input.c
33 --- a/drivers/leds/leds-gpio.c
34 +++ b/drivers/leds/leds-gpio.c
35 @@ -46,8 +46,15 @@ static void gpio_led_set(struct led_clas
36 led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
38 led_dat->blinking = 0;
39 + } else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
40 + gpiod_direction_input(led_dat->gpiod);
41 + led_dat->cdev.flags &= ~SET_GPIO_INPUT;
42 + } else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
43 + gpiod_direction_output(led_dat->gpiod, level);
44 + led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
46 - if (led_dat->can_sleep)
47 + if (led_dat->can_sleep ||
48 + (led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) ))
49 gpiod_set_value_cansleep(led_dat->gpiod, level);
51 gpiod_set_value(led_dat->gpiod, level);
52 @@ -61,6 +68,13 @@ static int gpio_led_set_blocking(struct
56 +static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
58 + struct gpio_led_data *led_dat =
59 + container_of(led_cdev, struct gpio_led_data, cdev);
60 + return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
63 static int gpio_blink_set(struct led_classdev *led_cdev,
64 unsigned long *delay_on, unsigned long *delay_off)
66 @@ -89,6 +103,7 @@ static int create_gpio_led(const struct
67 led_dat->platform_gpio_blink_set = blink_set;
68 led_dat->cdev.blink_set = gpio_blink_set;
70 + led_dat->cdev.brightness_get = gpio_led_get;
71 if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
72 state = gpiod_get_value_cansleep(led_dat->gpiod);
74 --- a/drivers/leds/trigger/Kconfig
75 +++ b/drivers/leds/trigger/Kconfig
76 @@ -114,6 +114,13 @@ config LEDS_TRIGGER_CAMERA
77 This enables direct flash/torch on/off by the driver, kernel space.
80 +config LEDS_TRIGGER_INPUT
81 + tristate "LED Input Trigger"
82 + depends on LEDS_TRIGGERS
84 + This allows the GPIOs assigned to be LEDs to be initialised to inputs.
87 config LEDS_TRIGGER_PANIC
88 bool "LED Panic Trigger"
90 --- a/drivers/leds/trigger/Makefile
91 +++ b/drivers/leds/trigger/Makefile
92 @@ -11,6 +11,7 @@ obj-$(CONFIG_LEDS_TRIGGER_ACTIVITY) += l
93 obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
94 obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
95 obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
96 +obj-$(CONFIG_LEDS_TRIGGER_INPUT) += ledtrig-input.o
97 obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
98 obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
99 obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
101 +++ b/drivers/leds/trigger/ledtrig-input.c
104 + * Set LED GPIO to Input "Trigger"
106 + * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
108 + * Based on Nick Forbes's ledtrig-default-on.c.
110 + * This program is free software; you can redistribute it and/or modify
111 + * it under the terms of the GNU General Public License version 2 as
112 + * published by the Free Software Foundation.
116 +#include <linux/module.h>
117 +#include <linux/kernel.h>
118 +#include <linux/init.h>
119 +#include <linux/leds.h>
120 +#include <linux/gpio.h>
121 +#include "../leds.h"
123 +static int input_trig_activate(struct led_classdev *led_cdev)
125 + led_cdev->flags |= SET_GPIO_INPUT;
126 + led_set_brightness(led_cdev, 0);
130 +static void input_trig_deactivate(struct led_classdev *led_cdev)
132 + led_cdev->flags |= SET_GPIO_OUTPUT;
133 + led_set_brightness(led_cdev, 0);
136 +static struct led_trigger input_led_trigger = {
138 + .activate = input_trig_activate,
139 + .deactivate = input_trig_deactivate,
142 +static int __init input_trig_init(void)
144 + return led_trigger_register(&input_led_trigger);
147 +static void __exit input_trig_exit(void)
149 + led_trigger_unregister(&input_led_trigger);
152 +module_init(input_trig_init);
153 +module_exit(input_trig_exit);
155 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.org>");
156 +MODULE_DESCRIPTION("Set LED GPIO to Input \"trigger\"");
157 +MODULE_LICENSE("GPL");
158 --- a/include/linux/leds.h
159 +++ b/include/linux/leds.h
160 @@ -79,6 +79,9 @@ struct led_classdev {
161 #define LED_BRIGHT_HW_CHANGED BIT(21)
162 #define LED_RETAIN_AT_SHUTDOWN BIT(22)
163 #define LED_INIT_DEFAULT_TRIGGER BIT(23)
164 + /* Additions for Raspberry Pi PWR LED */
165 +#define SET_GPIO_INPUT BIT(30)
166 +#define SET_GPIO_OUTPUT BIT(31)
168 /* set_brightness_work / blink_timer flags, atomic, private. */
169 unsigned long work_flags;