Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1252615yba; Thu, 16 May 2019 17:42:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRmaqYk1lV0zYRMxgN3njtChEtE5rGWLstm4wcwt1S310AHXhkGIiOPrSD5Ngbr2tqxNAx X-Received: by 2002:a62:604:: with SMTP id 4mr57528423pfg.38.1558053746627; Thu, 16 May 2019 17:42:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558053746; cv=none; d=google.com; s=arc-20160816; b=DEnwfF6Dd7w1fU6C/jn0VeOyCbITS+4/7tTOowglMcKywfSQPJynkMoullp9CxS7UH epkInzKKewmkMG2hOCFkD2uHXzU15m5wRXV2y4anRIOmiElgg+3kwkRU5FVHdpNCrL5u vCnIRe85A5QotoTBVEsPcElOzuW6Jld7eSltylLUDj+Bg/Pyl5DeL/6ZBNY0yJBCibUE lXoXYfh8M3Pl9M7/N1Bb9CFj5Iru+FGZEnBqwlL2+q8MvgJwApXkXhgmqImxdiwquy/i mVN4a/oAXs4EQ1KKIS2MNwHh5nZwxFIraTQNmOqQukHGjc3HwTM7G+iN4FuBh/z9PGWB 8L+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=dzrCuR0CYk8j8JHGMP9OKcrg17nxFH7UW7yzX+clpRM=; b=CWrhH0zD0rqaUuichJkAiAE7T+qvXT+UEvOtvulitF/+eVsOjnSsZhVyPjaeiv915x PZ/iCJmOPwD2ht7u3Amq4CMMjhzV4/KCyb/G3nnkQL5G96OWrppUJ1PfW8IDWRkHmX+d Y40UWiPiEcPDfRJc06U7nqFttNuvkjDKTJBZaT/EkSCmLX0FhmQAjMnJ8xsqrheeUSBR DbSAO1Sonk030SMKvaMaNvpDIdZzmO6cF4P1+6yjGi7F9jXgOXG+rt4el1z63EDq+32H dYjt0gpUdNLCBdSg0vxJoVwsDVvIpvFyCyCySXpbOu+rAXrUrfj5rZa6xikReWJEo9Yq z4zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="W/s8Jblq"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i96si6412450plb.331.2019.05.16.17.42.11; Thu, 16 May 2019 17:42:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="W/s8Jblq"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728808AbfEPVmd (ORCPT + 99 others); Thu, 16 May 2019 17:42:33 -0400 Received: from mail-vk1-f201.google.com ([209.85.221.201]:40468 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726732AbfEPVmc (ORCPT ); Thu, 16 May 2019 17:42:32 -0400 Received: by mail-vk1-f201.google.com with SMTP id d64so1799562vkg.7 for ; Thu, 16 May 2019 14:42:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dzrCuR0CYk8j8JHGMP9OKcrg17nxFH7UW7yzX+clpRM=; b=W/s8Jblq1GsRFbjb5XVhpcEHui0zgHCgxnU2c7Tlg/gWmEhESkOmAX5zkR411ILh84 z/8x13syAaDRUFmdNz+oEXNQVB1F9ESw4iM1KZeGb7lfpNEYp7bVsrYDy+lDjNwqIhSi 6TL+oG1fIfRaeU+io8iPpU6hK2lDJOUHm+EsHbMObGSl/AQnACA03W/ria/AUZl50mDf kA0Fd+P3qjt+XINUh0rq3YiJezttup4RP6jTDNyC4ltADmKTlEGnkTYQPoxEDWWBqRUy HbIKRULRlW6J3sbQeo3dlKlBzy01pqL+t0LmB142tNM0/C8QXmGaWYvuCUJHIuwHkyNN tXpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dzrCuR0CYk8j8JHGMP9OKcrg17nxFH7UW7yzX+clpRM=; b=YkdrMmY04f0oq6CFFQ1gt2aP/w8aotJpTvoBIFdfLP6IxqyKgHD+7b20mBtgyjOE/d zomJ35uuuk/R4vYvFAB7G5iEMzV070dIUoN6V5DxRyu/ISZPaWaLPgiBDkD0suY2Sx4s mAu01i6e64DsmiURQHiLvmNLtaAAc6EXxZUoxqWSXGW+JwsOdlBQuUhpTN5jGamZiJWE C5Na2i6Y+flE9vfaSvGcYYHqcR4DKeaAjhwuYiBkafpSWidbYbJOUpiVnlFmzJ2lbrvq mngMst09M5XDITP+UcUXOoSiUp2DAGqiDOmU8IGhEYeABQpcJAp1qbPG64Mpa0+nG5Ny Cr0A== X-Gm-Message-State: APjAAAXYHgbAmXY8VsZBIUzA82nGq9RpPKWK30VJAxklCWsFUwTpONfp YmpetsDTo1L5okcNFnDEXQHnRwfESw== X-Received: by 2002:a1f:fe81:: with SMTP id l123mr589442vki.51.1558042951597; Thu, 16 May 2019 14:42:31 -0700 (PDT) Date: Thu, 16 May 2019 14:42:08 -0700 In-Reply-To: <20190516214209.139726-1-kunyi@google.com> Message-Id: <20190516214209.139726-2-kunyi@google.com> Mime-Version: 1.0 References: <20190516214209.139726-1-kunyi@google.com> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [PATCH 1/2] ledtrig-gpio: Request user input pin as GPIO From: Kun Yi To: linux-leds@vger.kernel.org Cc: Kun Yi , jacek.anaszewski@gmail.com, pavel@ucw.cz, dmurphy@ti.com, u.kleine-koenig@pengutronix.de, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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