Skip to content

Commit 98ba365

Browse files
Phil Elwellpopcornmix
authored andcommitted
leds: Add the "input" trigger, for pwr_led
The "input" trigger makes the associated GPIO an input. This is to support the Raspberry Pi PWR LED, which is driven by external hardware in normal use. N.B. pwr_led is not available on Model A or B boards. leds-gpio: Implement the brightness_get method The power LED uses some clever logic that means it is driven by a voltage measuring circuit when configured as input, otherwise it is driven by the GPIO output value. This patch wires up the brightness_get method for leds-gpio so that user-space can monitor the LED value via /sys/class/gpio/led1/brightness. Using the input trigger this returns an indication of the system power health, otherwise it is just whatever value the trigger has written most recently. See: #1064
1 parent 1c200ad commit 98ba365

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

drivers/leds/leds-gpio.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ static void gpio_led_set(struct led_classdev *led_cdev,
5252
led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
5353
NULL, NULL);
5454
led_dat->blinking = 0;
55+
} else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
56+
gpiod_direction_input(led_dat->gpiod);
57+
led_dat->cdev.flags &= ~SET_GPIO_INPUT;
58+
} else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
59+
gpiod_direction_output(led_dat->gpiod, level);
60+
led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
5561
} else {
56-
if (led_dat->can_sleep)
62+
if (led_dat->can_sleep ||
63+
(led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) ))
5764
gpiod_set_value_cansleep(led_dat->gpiod, level);
5865
else
5966
gpiod_set_value(led_dat->gpiod, level);
@@ -67,6 +74,13 @@ static int gpio_led_set_blocking(struct led_classdev *led_cdev,
6774
return 0;
6875
}
6976

77+
static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
78+
{
79+
struct gpio_led_data *led_dat =
80+
container_of(led_cdev, struct gpio_led_data, cdev);
81+
return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
82+
}
83+
7084
static int gpio_blink_set(struct led_classdev *led_cdev,
7185
unsigned long *delay_on, unsigned long *delay_off)
7286
{
@@ -96,6 +110,7 @@ static int create_gpio_led(const struct gpio_led *template,
96110
led_dat->platform_gpio_blink_set = blink_set;
97111
led_dat->cdev.blink_set = gpio_blink_set;
98112
}
113+
led_dat->cdev.brightness_get = gpio_led_get;
99114
if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
100115
state = gpiod_get_value_cansleep(led_dat->gpiod);
101116
if (state < 0)

drivers/leds/trigger/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ config LEDS_TRIGGER_CAMERA
113113
This enables direct flash/torch on/off by the driver, kernel space.
114114
If unsure, say Y.
115115

116+
config LEDS_TRIGGER_INPUT
117+
tristate "LED Input Trigger"
118+
depends on LEDS_TRIGGERS
119+
help
120+
This allows the GPIOs assigned to be LEDs to be initialised to inputs.
121+
If unsure, say Y.
122+
116123
config LEDS_TRIGGER_PANIC
117124
bool "LED Panic Trigger"
118125
help

drivers/leds/trigger/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ obj-$(CONFIG_LEDS_TRIGGER_ACTIVITY) += ledtrig-activity.o
1111
obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
1212
obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
1313
obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
14+
obj-$(CONFIG_LEDS_TRIGGER_INPUT) += ledtrig-input.o
1415
obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
1516
obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
1617
obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Set LED GPIO to Input "Trigger"
3+
*
4+
* Copyright 2015 Phil Elwell <[email protected]>
5+
*
6+
* Based on Nick Forbes's ledtrig-default-on.c.
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License version 2 as
10+
* published by the Free Software Foundation.
11+
*
12+
*/
13+
14+
#include <linux/module.h>
15+
#include <linux/kernel.h>
16+
#include <linux/init.h>
17+
#include <linux/leds.h>
18+
#include <linux/gpio.h>
19+
#include "../leds.h"
20+
21+
static int input_trig_activate(struct led_classdev *led_cdev)
22+
{
23+
led_cdev->flags |= SET_GPIO_INPUT;
24+
led_set_brightness(led_cdev, 0);
25+
return 0;
26+
}
27+
28+
static void input_trig_deactivate(struct led_classdev *led_cdev)
29+
{
30+
led_cdev->flags |= SET_GPIO_OUTPUT;
31+
led_set_brightness(led_cdev, 0);
32+
}
33+
34+
static struct led_trigger input_led_trigger = {
35+
.name = "input",
36+
.activate = input_trig_activate,
37+
.deactivate = input_trig_deactivate,
38+
};
39+
40+
static int __init input_trig_init(void)
41+
{
42+
return led_trigger_register(&input_led_trigger);
43+
}
44+
45+
static void __exit input_trig_exit(void)
46+
{
47+
led_trigger_unregister(&input_led_trigger);
48+
}
49+
50+
module_init(input_trig_init);
51+
module_exit(input_trig_exit);
52+
53+
MODULE_AUTHOR("Phil Elwell <[email protected]>");
54+
MODULE_DESCRIPTION("Set LED GPIO to Input \"trigger\"");
55+
MODULE_LICENSE("GPL");

include/linux/leds.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ struct led_classdev {
115115
#define LED_BRIGHT_HW_CHANGED BIT(21)
116116
#define LED_RETAIN_AT_SHUTDOWN BIT(22)
117117
#define LED_INIT_DEFAULT_TRIGGER BIT(23)
118+
/* Additions for Raspberry Pi PWR LED */
119+
#define SET_GPIO_INPUT BIT(30)
120+
#define SET_GPIO_OUTPUT BIT(31)
118121

119122
/* set_brightness_work / blink_timer flags, atomic, private. */
120123
unsigned long work_flags;

0 commit comments

Comments
 (0)