1 From 8404a4fe589f3b7a42eb4d3e61719d76dfb83658 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 073/423] 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 | 18 +++++++++++-
26 drivers/leds/trigger/Kconfig | 7 +++++
27 drivers/leds/trigger/Makefile | 1 +
28 drivers/leds/trigger/ledtrig-input.c | 54 ++++++++++++++++++++++++++++++++++++
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 @@ -42,6 +42,13 @@ static void gpio_led_work(struct work_st
36 led_dat->platform_gpio_blink_set(led_dat->gpiod,
37 led_dat->new_level, NULL, NULL);
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;
43 + else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
44 + gpiod_direction_output(led_dat->gpiod, led_dat->new_level);
45 + led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
47 gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level);
49 @@ -62,7 +69,8 @@ static void gpio_led_set(struct led_clas
50 * seem to have a reliable way to know if we're already in one; so
51 * let's just assume the worst.
53 - if (led_dat->can_sleep) {
54 + if (led_dat->can_sleep ||
55 + (led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) )) {
56 led_dat->new_level = level;
57 schedule_work(&led_dat->work);
59 @@ -75,6 +83,13 @@ static void gpio_led_set(struct led_clas
63 +static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
65 + struct gpio_led_data *led_dat =
66 + container_of(led_cdev, struct gpio_led_data, cdev);
67 + return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
70 static int gpio_blink_set(struct led_classdev *led_cdev,
71 unsigned long *delay_on, unsigned long *delay_off)
73 @@ -131,6 +146,7 @@ static int create_gpio_led(const struct
74 led_dat->cdev.blink_set = gpio_blink_set;
76 led_dat->cdev.brightness_set = gpio_led_set;
77 + led_dat->cdev.brightness_get = gpio_led_get;
78 if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
79 state = !!gpiod_get_value_cansleep(led_dat->gpiod);
81 --- a/drivers/leds/trigger/Kconfig
82 +++ b/drivers/leds/trigger/Kconfig
83 @@ -126,4 +126,11 @@ config LEDS_TRIGGER_USBDEV
84 This allows LEDs to be controlled by the presence/activity of
85 an USB device. If unsure, say N.
87 +config LEDS_TRIGGER_INPUT
88 + tristate "LED Input Trigger"
89 + depends on LEDS_TRIGGERS
91 + This allows the GPIOs assigned to be LEDs to be initialised to inputs.
95 --- a/drivers/leds/trigger/Makefile
96 +++ b/drivers/leds/trigger/Makefile
97 @@ -8,3 +8,4 @@ obj-$(CONFIG_LEDS_TRIGGER_CPU) += ledtr
98 obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
99 obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
100 obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
101 +obj-$(CONFIG_LEDS_TRIGGER_INPUT) += ledtrig-input.o
103 +++ b/drivers/leds/trigger/ledtrig-input.c
106 + * Set LED GPIO to Input "Trigger"
108 + * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
110 + * Based on Nick Forbes's ledtrig-default-on.c.
112 + * This program is free software; you can redistribute it and/or modify
113 + * it under the terms of the GNU General Public License version 2 as
114 + * published by the Free Software Foundation.
118 +#include <linux/module.h>
119 +#include <linux/kernel.h>
120 +#include <linux/init.h>
121 +#include <linux/leds.h>
122 +#include <linux/gpio.h>
123 +#include "../leds.h"
125 +static void input_trig_activate(struct led_classdev *led_cdev)
127 + led_cdev->flags |= SET_GPIO_INPUT;
128 + led_set_brightness_async(led_cdev, 0);
131 +static void input_trig_deactivate(struct led_classdev *led_cdev)
133 + led_cdev->flags |= SET_GPIO_OUTPUT;
134 + led_set_brightness_async(led_cdev, 0);
137 +static struct led_trigger input_led_trigger = {
139 + .activate = input_trig_activate,
140 + .deactivate = input_trig_deactivate,
143 +static int __init input_trig_init(void)
145 + return led_trigger_register(&input_led_trigger);
148 +static void __exit input_trig_exit(void)
150 + led_trigger_unregister(&input_led_trigger);
153 +module_init(input_trig_init);
154 +module_exit(input_trig_exit);
156 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.org>");
157 +MODULE_DESCRIPTION("Set LED GPIO to Input \"trigger\"");
158 +MODULE_LICENSE("GPL");
159 --- a/include/linux/leds.h
160 +++ b/include/linux/leds.h
161 @@ -48,6 +48,9 @@ struct led_classdev {
162 #define SET_BRIGHTNESS_ASYNC (1 << 21)
163 #define SET_BRIGHTNESS_SYNC (1 << 22)
164 #define LED_DEV_CAP_FLASH (1 << 23)
165 + /* Additions for Raspberry Pi PWR LED */
166 +#define SET_GPIO_INPUT (1 << 30)
167 +#define SET_GPIO_OUTPUT (1 << 31)
169 /* Set LED brightness level */
170 /* Must not sleep, use a workqueue if needed */