Skip to content

Commit 33b3d9a

Browse files
Phil Elwellpopcornmix
authored andcommitted
BCM270x_DT: Add pwr_led, and the required "input" trigger
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: raspberrypi#1064
1 parent b5fd857 commit 33b3d9a

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎drivers/leds/leds-gpio.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ static void gpio_led_set(struct led_classdev *led_cdev,
4646
led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
4747
NULL, NULL);
4848
led_dat->blinking = 0;
49+
} else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
50+
gpiod_direction_input(led_dat->gpiod);
51+
led_dat->cdev.flags &= ~SET_GPIO_INPUT;
52+
} else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
53+
gpiod_direction_output(led_dat->gpiod, level);
54+
led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
4955
} else {
50-
if (led_dat->can_sleep)
56+
if (led_dat->can_sleep ||
57+
(led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) ))
5158
gpiod_set_value_cansleep(led_dat->gpiod, level);
5259
else
5360
gpiod_set_value(led_dat->gpiod, level);
@@ -61,6 +68,13 @@ static int gpio_led_set_blocking(struct led_classdev *led_cdev,
6168
return 0;
6269
}
6370

71+
static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
72+
{
73+
struct gpio_led_data *led_dat =
74+
container_of(led_cdev, struct gpio_led_data, cdev);
75+
return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
76+
}
77+
6478
static int gpio_blink_set(struct led_classdev *led_cdev,
6579
unsigned long *delay_on, unsigned long *delay_off)
6680
{
@@ -89,6 +103,7 @@ static int create_gpio_led(const struct gpio_led *template,
89103
led_dat->platform_gpio_blink_set = blink_set;
90104
led_dat->cdev.blink_set = gpio_blink_set;
91105
}
106+
led_dat->cdev.brightness_get = gpio_led_get;
92107
if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
93108
state = gpiod_get_value_cansleep(led_dat->gpiod);
94109
if (state < 0)

‎drivers/leds/trigger/Kconfig‎

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

117+
config LEDS_TRIGGER_INPUT
118+
tristate "LED Input Trigger"
119+
depends on LEDS_TRIGGERS
120+
help
121+
This allows the GPIOs assigned to be LEDs to be initialised to inputs.
122+
If unsure, say Y.
123+
117124
config LEDS_TRIGGER_PANIC
118125
bool "LED Panic Trigger"
119126
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 <phil@raspberrypi.org>
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 <phil@raspberrypi.org>");
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
@@ -79,6 +79,9 @@ struct led_classdev {
7979
#define LED_BRIGHT_HW_CHANGED BIT(21)
8080
#define LED_RETAIN_AT_SHUTDOWN BIT(22)
8181
#define LED_INIT_DEFAULT_TRIGGER BIT(23)
82+
/* Additions for Raspberry Pi PWR LED */
83+
#define SET_GPIO_INPUT BIT(30)
84+
#define SET_GPIO_OUTPUT BIT(31)
8285

8386
/* set_brightness_work / blink_timer flags, atomic, private. */
8487
unsigned long work_flags;

0 commit comments

Comments
 (0)