1 From b19a2e678c297e81b8b9ab6db1f4775be1c678a8 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
6 The "input" trigger makes the associated GPIO an input. This is to support
7 the Raspberry Pi PWR LED, which is driven by external hardware in normal use.
9 N.B. pwr_led is not available on Model A or B boards.
11 leds-gpio: Implement the brightness_get method
13 The power LED uses some clever logic that means it is driven
14 by a voltage measuring circuit when configured as input, otherwise
15 it is driven by the GPIO output value. This patch wires up the
16 brightness_get method for leds-gpio so that user-space can monitor
17 the LED value via /sys/class/gpio/led1/brightness. Using the input
18 trigger this returns an indication of the system power health,
19 otherwise it is just whatever value the trigger has written most
22 See: https://github.com/raspberrypi/linux/issues/1064
24 drivers/leds/leds-gpio.c | 18 +++++++++++-
25 drivers/leds/trigger/Kconfig | 7 +++++
26 drivers/leds/trigger/Makefile | 1 +
27 drivers/leds/trigger/ledtrig-input.c | 54 ++++++++++++++++++++++++++++++++++++
28 include/linux/leds.h | 3 ++
29 5 files changed, 82 insertions(+), 1 deletion(-)
30 create mode 100644 drivers/leds/trigger/ledtrig-input.c
32 --- a/drivers/leds/leds-gpio.c
33 +++ b/drivers/leds/leds-gpio.c
34 @@ -42,6 +42,13 @@ static void gpio_led_work(struct work_st
35 led_dat->platform_gpio_blink_set(led_dat->gpiod,
36 led_dat->new_level, NULL, NULL);
37 led_dat->blinking = 0;
38 + } else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
39 + gpiod_direction_input(led_dat->gpiod);
40 + led_dat->cdev.flags &= ~SET_GPIO_INPUT;
42 + else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
43 + gpiod_direction_output(led_dat->gpiod, led_dat->new_level);
44 + led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
46 gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level);
48 @@ -62,7 +69,8 @@ static void gpio_led_set(struct led_clas
49 * seem to have a reliable way to know if we're already in one; so
50 * let's just assume the worst.
52 - if (led_dat->can_sleep) {
53 + if (led_dat->can_sleep ||
54 + (led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) )) {
55 led_dat->new_level = level;
56 schedule_work(&led_dat->work);
58 @@ -75,6 +83,13 @@ static void gpio_led_set(struct led_clas
62 +static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
64 + struct gpio_led_data *led_dat =
65 + container_of(led_cdev, struct gpio_led_data, cdev);
66 + return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
69 static int gpio_blink_set(struct led_classdev *led_cdev,
70 unsigned long *delay_on, unsigned long *delay_off)
72 @@ -131,6 +146,7 @@ static int create_gpio_led(const struct
73 led_dat->cdev.blink_set = gpio_blink_set;
75 led_dat->cdev.brightness_set = gpio_led_set;
76 + led_dat->cdev.brightness_get = gpio_led_get;
77 if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
78 state = !!gpiod_get_value_cansleep(led_dat->gpiod);
80 --- a/drivers/leds/trigger/Kconfig
81 +++ b/drivers/leds/trigger/Kconfig
82 @@ -122,4 +122,11 @@ config LEDS_TRIGGER_USBDEV
83 This allows LEDs to be controlled by the presence/activity of
84 an USB device. If unsure, say N.
86 +config LEDS_TRIGGER_INPUT
87 + tristate "LED Input Trigger"
88 + depends on LEDS_TRIGGERS
90 + This allows the GPIOs assigned to be LEDs to be initialised to inputs.
94 --- a/drivers/leds/trigger/Makefile
95 +++ b/drivers/leds/trigger/Makefile
96 @@ -8,3 +8,4 @@ obj-$(CONFIG_LEDS_TRIGGER_CPU) += ledtr
97 obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
98 obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
99 obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
100 +obj-$(CONFIG_LEDS_TRIGGER_INPUT) += ledtrig-input.o
102 +++ b/drivers/leds/trigger/ledtrig-input.c
105 + * Set LED GPIO to Input "Trigger"
107 + * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
109 + * Based on Nick Forbes's ledtrig-default-on.c.
111 + * This program is free software; you can redistribute it and/or modify
112 + * it under the terms of the GNU General Public License version 2 as
113 + * published by the Free Software Foundation.
117 +#include <linux/module.h>
118 +#include <linux/kernel.h>
119 +#include <linux/init.h>
120 +#include <linux/leds.h>
121 +#include <linux/gpio.h>
122 +#include "../leds.h"
124 +static void input_trig_activate(struct led_classdev *led_cdev)
126 + led_cdev->flags |= SET_GPIO_INPUT;
127 + led_set_brightness_async(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_async(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 @@ -48,6 +48,9 @@ struct led_classdev {
161 #define SET_BRIGHTNESS_ASYNC (1 << 21)
162 #define SET_BRIGHTNESS_SYNC (1 << 22)
163 #define LED_DEV_CAP_FLASH (1 << 23)
164 + /* Additions for Raspberry Pi PWR LED */
165 +#define SET_GPIO_INPUT (1 << 30)
166 +#define SET_GPIO_OUTPUT (1 << 31)
168 /* Set LED brightness level */
169 /* Must not sleep, use a workqueue if needed */