brcm2708: update linux 4.4 patches to latest version
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0074-BCM270x_DT-Add-pwr_led-and-the-required-input-trigge.patch
1 From a2b52a3af1314e2b691d9518afb3051193758671 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 074/170] BCM270x_DT: Add pwr_led, and the required "input"
5 trigger
6
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.
9
10 N.B. pwr_led is not available on Model A or B boards.
11
12 leds-gpio: Implement the brightness_get method
13
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
21 recently.
22
23 See: https://github.com/raspberrypi/linux/issues/1064
24 ---
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
32
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;
42 + }
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;
46 } else
47 gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level);
48 }
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.
52 */
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);
58 } else {
59 @@ -75,6 +83,13 @@ static void gpio_led_set(struct led_clas
60 }
61 }
62
63 +static enum led_brightness gpio_led_get(struct led_classdev *led_cdev)
64 +{
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;
68 +}
69 +
70 static int gpio_blink_set(struct led_classdev *led_cdev,
71 unsigned long *delay_on, unsigned long *delay_off)
72 {
73 @@ -131,6 +146,7 @@ static int create_gpio_led(const struct
74 led_dat->cdev.blink_set = gpio_blink_set;
75 }
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);
80 else
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.
86
87 +config LEDS_TRIGGER_INPUT
88 + tristate "LED Input Trigger"
89 + depends on LEDS_TRIGGERS
90 + help
91 + This allows the GPIOs assigned to be LEDs to be initialised to inputs.
92 + If unsure, say Y.
93 +
94 endif # LEDS_TRIGGERS
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
102 --- /dev/null
103 +++ b/drivers/leds/trigger/ledtrig-input.c
104 @@ -0,0 +1,54 @@
105 +/*
106 + * Set LED GPIO to Input "Trigger"
107 + *
108 + * Copyright 2015 Phil Elwell <phil@raspberrypi.org>
109 + *
110 + * Based on Nick Forbes's ledtrig-default-on.c.
111 + *
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.
115 + *
116 + */
117 +
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"
124 +
125 +static void input_trig_activate(struct led_classdev *led_cdev)
126 +{
127 + led_cdev->flags |= SET_GPIO_INPUT;
128 + led_set_brightness_async(led_cdev, 0);
129 +}
130 +
131 +static void input_trig_deactivate(struct led_classdev *led_cdev)
132 +{
133 + led_cdev->flags |= SET_GPIO_OUTPUT;
134 + led_set_brightness_async(led_cdev, 0);
135 +}
136 +
137 +static struct led_trigger input_led_trigger = {
138 + .name = "input",
139 + .activate = input_trig_activate,
140 + .deactivate = input_trig_deactivate,
141 +};
142 +
143 +static int __init input_trig_init(void)
144 +{
145 + return led_trigger_register(&input_led_trigger);
146 +}
147 +
148 +static void __exit input_trig_exit(void)
149 +{
150 + led_trigger_unregister(&input_led_trigger);
151 +}
152 +
153 +module_init(input_trig_init);
154 +module_exit(input_trig_exit);
155 +
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)
168
169 /* Set LED brightness level */
170 /* Must not sleep, use a workqueue if needed */