From: Kun Yi <kunyi@google.com>
To: linux-leds@vger.kernel.org
Cc: Kun Yi <kunyi@google.com>,
jacek.anaszewski@gmail.com, pavel@ucw.cz, dmurphy@ti.com,
u.kleine-koenig@pengutronix.de, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] ledtrig-gpio: Request user input pin as GPIO
Date: Thu, 16 May 2019 14:42:08 -0700 [thread overview]
Message-ID: <20190516214209.139726-2-kunyi@google.com> (raw)
In-Reply-To: <20190516214209.139726-1-kunyi@google.com>
The ledtrig-gpio logic assumes the input pin can be directly converted
to IRQ using gpio_to_irq. This is problematic since there is no
guarantee on the pinmux function nor the direction of the pin. Request
the pin as an input GPIO before requesting it as an IRQ.
Tested: a free pin can be correctly requested as GPIO
Signed-off-by: Kun Yi <kunyi@google.com>
Change-Id: I657e3e108552612506775cc348a8b4b35d40cac5
---
drivers/leds/trigger/ledtrig-gpio.c | 31 +++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index ed0db8ed825f..f6d50e031492 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -117,6 +117,16 @@ static ssize_t gpio_trig_gpio_show(struct device *dev,
return sprintf(buf, "%u\n", gpio_data->gpio);
}
+static inline void free_used_gpio_if_valid(unsigned int gpio,
+ struct led_classdev *led)
+{
+ if (gpio == 0)
+ return;
+
+ free_irq(gpio_to_irq(gpio), led);
+ gpio_free(gpio);
+}
+
static ssize_t gpio_trig_gpio_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
@@ -135,20 +145,30 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
return n;
if (!gpio) {
- if (gpio_data->gpio != 0)
- free_irq(gpio_to_irq(gpio_data->gpio), led);
+ free_used_gpio_if_valid(gpio_data->gpio, led);
gpio_data->gpio = 0;
return n;
}
+ ret = gpio_request(gpio, "ledtrig-gpio");
+ if (ret) {
+ dev_err(dev, "gpio_request failed with error %d\n", ret);
+ return ret;
+ }
+
+ ret = gpio_direction_input(gpio);
+ if (ret) {
+ dev_err(dev, "gpio_direction_input failed with err %d\n", ret);
+ return ret;
+ }
+
ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
if (ret) {
dev_err(dev, "request_irq failed with error %d\n", ret);
} else {
- if (gpio_data->gpio != 0)
- free_irq(gpio_to_irq(gpio_data->gpio), led);
+ free_used_gpio_if_valid(gpio_data->gpio, led);
gpio_data->gpio = gpio;
/* After changing the GPIO, we need to update the LED. */
gpio_trig_irq(0, led);
@@ -184,8 +204,7 @@ static void gpio_trig_deactivate(struct led_classdev *led)
{
struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
- if (gpio_data->gpio != 0)
- free_irq(gpio_to_irq(gpio_data->gpio), led);
+ free_used_gpio_if_valid(gpio_data->gpio, led);
kfree(gpio_data);
}
--
2.21.0.1020.gf2820cf01a-goog
next prev parent reply other threads:[~2019-05-16 21:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-16 21:42 [PATCH 0/2] Fix LED GPIO trigger behavior Kun Yi
2019-05-16 21:42 ` Kun Yi [this message]
2019-05-17 6:26 ` [PATCH 1/2] ledtrig-gpio: Request user input pin as GPIO Uwe Kleine-König
2019-05-16 21:42 ` [PATCH 2/2] ledtrig-gpio: 0 is a valid GPIO number Kun Yi
2019-05-17 6:27 ` Uwe Kleine-König
2019-05-17 16:56 ` [PATCH 0/2] Fix LED GPIO trigger behavior Jacek Anaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190516214209.139726-2-kunyi@google.com \
--to=kunyi@google.com \
--cc=dmurphy@ti.com \
--cc=jacek.anaszewski@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@ucw.cz \
--cc=u.kleine-koenig@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox