]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/commitdiff
BCM270x_DT: Add pwr_led, and the required "input" trigger
authorPhil Elwell <phil@raspberrypi.org>
Fri, 6 Feb 2015 13:50:57 +0000 (13:50 +0000)
committerStefan Bader <stefan.bader@canonical.com>
Tue, 8 Aug 2017 12:51:58 +0000 (14:51 +0200)
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: https://github.com/raspberrypi/linux/issues/1064

drivers/leds/leds-gpio.c
drivers/leds/trigger/Kconfig
drivers/leds/trigger/Makefile
drivers/leds/trigger/ledtrig-input.c [new file with mode: 0644]
include/linux/leds.h

index d400dcaf4d296444967065e98812c210c48a192d..6a27a693034825de2897bb7b338b60cc10c9e59f 100644 (file)
@@ -50,8 +50,15 @@ static void gpio_led_set(struct led_classdev *led_cdev,
                led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
                                                 NULL, NULL);
                led_dat->blinking = 0;
+       } else if (led_dat->cdev.flags & SET_GPIO_INPUT) {
+               gpiod_direction_input(led_dat->gpiod);
+               led_dat->cdev.flags &= ~SET_GPIO_INPUT;
+       } else if (led_dat->cdev.flags & SET_GPIO_OUTPUT) {
+               gpiod_direction_output(led_dat->gpiod, level);
+               led_dat->cdev.flags &= ~SET_GPIO_OUTPUT;
        } else {
-               if (led_dat->can_sleep)
+               if (led_dat->can_sleep ||
+                       (led_dat->cdev.flags & (SET_GPIO_INPUT | SET_GPIO_OUTPUT) ))
                        gpiod_set_value_cansleep(led_dat->gpiod, level);
                else
                        gpiod_set_value(led_dat->gpiod, level);
@@ -65,6 +72,13 @@ static int gpio_led_set_blocking(struct led_classdev *led_cdev,
        return 0;
 }
 
+static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
+{
+       struct gpio_led_data *led_dat =
+               container_of(led_cdev, struct gpio_led_data, cdev);
+       return gpiod_get_value_cansleep(led_dat->gpiod) ? LED_FULL : LED_OFF;
+}
+
 static int gpio_blink_set(struct led_classdev *led_cdev,
        unsigned long *delay_on, unsigned long *delay_off)
 {
@@ -122,6 +136,8 @@ static int create_gpio_led(const struct gpio_led *template,
                led_dat->platform_gpio_blink_set = blink_set;
                led_dat->cdev.blink_set = gpio_blink_set;
        }
+       led_dat->cdev.brightness_set = gpio_led_set;
+       led_dat->cdev.brightness_get = gpio_led_get;
        if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
                state = gpiod_get_value_cansleep(led_dat->gpiod);
                if (state < 0)
index 3f9ddb9fafa77f1f1fe5535c5e947838761e4f35..c1b6c83f3b63f993452893f136c5da54d0ada8e7 100644 (file)
@@ -116,6 +116,13 @@ config LEDS_TRIGGER_CAMERA
          This enables direct flash/torch on/off by the driver, kernel space.
          If unsure, say Y.
 
+config LEDS_TRIGGER_INPUT
+       tristate "LED Input Trigger"
+       depends on LEDS_TRIGGERS
+       help
+         This allows the GPIOs assigned to be LEDs to be initialised to inputs.
+         If unsure, say Y.
+
 config LEDS_TRIGGER_PANIC
        bool "LED Panic Trigger"
        depends on LEDS_TRIGGERS
index a72c43cffebf43bc7ebfd1eb1103b9ccff73d075..51b90c46890872fd1b148ec748edce0e0ce8fee1 100644 (file)
@@ -9,4 +9,5 @@ obj-$(CONFIG_LEDS_TRIGGER_CPU)          += ledtrig-cpu.o
 obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON)  += ledtrig-default-on.o
 obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT)   += ledtrig-transient.o
 obj-$(CONFIG_LEDS_TRIGGER_CAMERA)      += ledtrig-camera.o
+obj-$(CONFIG_LEDS_TRIGGER_INPUT)       += ledtrig-input.o
 obj-$(CONFIG_LEDS_TRIGGER_PANIC)       += ledtrig-panic.o
diff --git a/drivers/leds/trigger/ledtrig-input.c b/drivers/leds/trigger/ledtrig-input.c
new file mode 100644 (file)
index 0000000..27f8ebe
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Set LED GPIO to Input "Trigger"
+ *
+ * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
+ *
+ * Based on Nick Forbes's ledtrig-default-on.c.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/leds.h>
+#include <linux/gpio.h>
+#include "../leds.h"
+
+static void input_trig_activate(struct led_classdev *led_cdev)
+{
+       led_cdev->flags |= SET_GPIO_INPUT;
+       led_set_brightness(led_cdev, 0);
+}
+
+static void input_trig_deactivate(struct led_classdev *led_cdev)
+{
+       led_cdev->flags |= SET_GPIO_OUTPUT;
+       led_set_brightness(led_cdev, 0);
+}
+
+static struct led_trigger input_led_trigger = {
+       .name     = "input",
+       .activate = input_trig_activate,
+       .deactivate = input_trig_deactivate,
+};
+
+static int __init input_trig_init(void)
+{
+       return led_trigger_register(&input_led_trigger);
+}
+
+static void __exit input_trig_exit(void)
+{
+       led_trigger_unregister(&input_led_trigger);
+}
+
+module_init(input_trig_init);
+module_exit(input_trig_exit);
+
+MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.org>");
+MODULE_DESCRIPTION("Set LED GPIO to Input \"trigger\"");
+MODULE_LICENSE("GPL");
index 569cb531094c20a9aa2db478aaa6f348d2afd7f4..aca292f4b0932f61b5bd864251fd05b945497ed4 100644 (file)
@@ -46,6 +46,9 @@ struct led_classdev {
 #define LED_DEV_CAP_FLASH      (1 << 18)
 #define LED_HW_PLUGGABLE       (1 << 19)
 #define LED_PANIC_INDICATOR    (1 << 20)
+       /* Additions for Raspberry Pi PWR LED */
+#define SET_GPIO_INPUT         (1 << 30)
+#define SET_GPIO_OUTPUT                (1 << 31)
 
        /* set_brightness_work / blink_timer flags, atomic, private. */
        unsigned long           work_flags;