From patchwork Tue May 30 20:26:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9755219 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0D7D4601D2 for ; Tue, 30 May 2017 20:28:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0231B283BA for ; Tue, 30 May 2017 20:28:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E9DC828419; Tue, 30 May 2017 20:28:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 71415283C6 for ; Tue, 30 May 2017 20:28:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:References:To:Subject:From:Reply-To:Content-ID:Content-Description :Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KQsIQVhLsT6y12VQrKNaf2IrO0FSRLNxxZKbr+kt16A=; b=oBdZ6V0n1igtBp V/hLW7mTj+0nXIyeVVfwGJKEtuxR7ghydfOtdksK0PvliKuzAUz6/CRNPeL4Oax6tXMko6TeRQghS DBIJ/txCUeQbHQooChG3OYtHVQyp+t+7n5gR0dgVWpmFQsr+7Q4a3AXp+t6E2O/bOWkrACqXdm8Ac xaV7d4mD/lpBRIitZb/annSXnNA355IIf8sCdFBohY/+9R2JvrxSSGcLl9EtUJWbcG7OwREN8LV0V J6SZCjcNXbEHCAhCGnMPdM1HYH4JLBDdHmHU17THRycVJ7KGX8QJHjH/1FibjXmBbsk3RhbvawSfb JJQYwEVf0hwLU1bmm6kw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dFnkE-0004bF-Ku; Tue, 30 May 2017 20:27:58 +0000 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dFnk7-0004NJ-RO for linux-amlogic@lists.infradead.org; Tue, 30 May 2017 20:27:54 +0000 Received: by mail-wr0-x243.google.com with SMTP id 6so5268497wrb.1 for ; Tue, 30 May 2017 13:27:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=KalBGWPy1qhNbp4wKRLXCS901iuvMiY2R5zMcO6bBrY=; b=ms5uDInWOVIhZXcWdg/8U18BGUTdqABSWxjDeKMoHGC6+7sZqCkO+qiIzOLptFWMnv SNYPStwJmmc0OMdnYWvanSdePNTlJJv7oXIyGj/YxgDo87anzw+CqRxy43iM0u8W9A6I MHHCizVFE66lWTXH8zdeD7cj1S8cWZy8s5QDuyW1q0FVzevdwHmIWGX7Btx4/HU1GaY0 DzZ0/XB9o5L5/eaoWUnOvPP9iSE8nj63ej5yUURs+kGVMxG7eNvTrHW2M2dXIWVBAolo UTeYrisBCHZF9WaNxDKaGbCTy3T+pXlPt2Syllje1ueKDa7I7wJT4EW+Hlp8M4j9d8ca Q4oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=KalBGWPy1qhNbp4wKRLXCS901iuvMiY2R5zMcO6bBrY=; b=sFjmqfCHS0geQ0u0rc8hE6sdebMtwoDtL/s5VxajSzw5lym2iKXpk6jLyDUzcPedcN YsAJ8pwyoXD7Z7zQcT9/vh9IxPkfEOFYqlc8EgL9PtWh4z1iGaYLGdFRUW9OHsYi9uFA t6uNxvkSlFG/fuRPtnmGXamlzvqTPCSyyj8fhTsNPvt3thbnQiZIfJ672wDReKbCwNwq Fjoy7mVpMvlS8QKaTI9Ms3QIkdjDE8Dd+1F5mpUQfAfKehbvIy+EMQ0Si7VuIsVzSd7Y j2NV0TONqAxshHIlcxp8Ku9ZByoTQXWmGzN+wq/1+hcX5Srd8QJlmC2kJ6TNdHMy/0r+ jzIg== X-Gm-Message-State: AODbwcCW1ErxS77vZYFBpgKFLa35vO03M6C3CinT2+lScEJleQtOjBsL 5U08D3u4HTfVbQ== X-Received: by 10.223.148.97 with SMTP id 88mr4818660wrq.137.1496176051804; Tue, 30 May 2017 13:27:31 -0700 (PDT) Received: from ?IPv6:2003:c6:ebdd:1100:88ec:a768:3ada:81a6? (p200300C6EBDD110088ECA7683ADA81A6.dip0.t-ipconnect.de. [2003:c6:ebdd:1100:88ec:a768:3ada:81a6]) by smtp.googlemail.com with ESMTPSA id 4sm21448858wrv.33.2017.05.30.13.27.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 13:27:31 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH v5 6/9] pinctrl: meson: add support for GPIO interrupts To: Jerome Brunet , Mark Rutland , Marc Zyngier , Linus Walleij , Kevin Hilman , Thomas Gleixner , Rob Herring , Neil Armstrong References: <66c57893-0a9e-4f8c-a017-ab346abbdfd0@gmail.com> Message-ID: <12e8fe0e-d608-f560-9ddb-7287e10d3db2@gmail.com> Date: Tue, 30 May 2017 22:26:48 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <66c57893-0a9e-4f8c-a017-ab346abbdfd0@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170530_132752_167274_D8C9082D X-CRM114-Status: GOOD ( 17.16 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thierry Reding , devicetree@vger.kernel.org, linux-gpio@vger.kernel.org, "thierry.reding@gmail.com" , linux-amlogic@lists.infradead.org Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add support for GPIO interrupts and make use of the just introduced irqchip driver handling the GPIO interrupt-controller. Signed-off-by: Heiner Kallweit --- v5: - changed Kconfig entry based on Neil's suggestion - extended comments - fixed indentation --- drivers/pinctrl/Kconfig | 1 + drivers/pinctrl/meson/pinctrl-meson.c | 170 +++++++++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 1 deletion(-) diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig index 7ae04a97..86834dea 100644 --- a/drivers/pinctrl/Kconfig +++ b/drivers/pinctrl/Kconfig @@ -166,6 +166,7 @@ config PINCTRL_MESON select PINCONF select GENERIC_PINCONF select GPIOLIB + select GPIOLIB_IRQCHIP select OF_GPIO select REGMAP_MMIO diff --git a/drivers/pinctrl/meson/pinctrl-meson.c b/drivers/pinctrl/meson/pinctrl-meson.c index 66ed70c1..7bacd4e3 100644 --- a/drivers/pinctrl/meson/pinctrl-meson.c +++ b/drivers/pinctrl/meson/pinctrl-meson.c @@ -62,6 +62,8 @@ #include "../pinctrl-utils.h" #include "pinctrl-meson.h" +static struct irq_domain *meson_pinctrl_irq_domain; + /** * meson_get_bank() - find the bank containing a given pin * @@ -497,6 +499,154 @@ static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio) return !!(val & BIT(bit)); } +static struct meson_pinctrl *meson_gpio_data_to_pc(struct irq_data *data) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); + + return gpiochip_get_data(chip); +} + +static int meson_gpio_bank_hwirq(struct meson_bank *bank, unsigned int offset) +{ + int hwirq; + + if (bank->irq_first < 0) + /* this bank cannot generate irqs */ + return 0; + + hwirq = offset - bank->first + bank->irq_first; + + if (hwirq > bank->irq_last) + /* this pin cannot generate irqs */ + return 0; + + return hwirq; +} + +static int meson_gpio_to_hwirq(struct irq_data *data) +{ + struct meson_pinctrl *pc = meson_gpio_data_to_pc(data); + unsigned int offset = data->hwirq; + struct meson_bank *bank; + int hwirq, ret; + + offset += pc->data->pin_base; + + ret = meson_get_bank(pc, offset, &bank); + if (ret) + return ret; + + hwirq = meson_gpio_bank_hwirq(bank, offset); + if (!hwirq) + dev_dbg(pc->dev, "no interrupt for pin %u\n", offset); + + return hwirq; +} + +static void meson_gpio_irq_handler(struct irq_desc *desc) +{ + struct irq_chip *chip = irq_desc_get_chip(desc); + struct irq_data *gpio_irq_data = irq_desc_get_handler_data(desc); + + chained_irq_enter(chip, desc); + + if (gpio_irq_data) + generic_handle_irq(gpio_irq_data->irq); + + chained_irq_exit(chip, desc); +} + +static void meson_gpio_irq_unmask(struct irq_data *data) {} +static void meson_gpio_irq_mask(struct irq_data *data) {} + +static void meson_gpio_irq_shutdown(struct irq_data *data) +{ + int hwirq = meson_gpio_to_hwirq(data); + int irq; + + if (hwirq <= 0) + return; + + /* + * In case of IRQ_TYPE_EDGE_BOTH we need two parent interrupts, + * one for each edge. That's due to HW constraints. + * We use format 2 * GPIO_HWIRQ +(0|1) for the hwirq, so we can + * have one GPIO_HWIRQ twice and derive the GPIO_HWIRQ from hwirq + * by shifting hwirq one bit to the right. + */ + irq = irq_find_mapping(meson_pinctrl_irq_domain, hwirq * 2); + if (irq) { + irq_set_chained_handler_and_data(irq, handle_bad_irq, NULL); + irq_domain_free_irqs(irq, 1); + } + + irq = irq_find_mapping(meson_pinctrl_irq_domain, hwirq * 2 + 1); + if (irq) { + irq_set_chained_handler_and_data(irq, handle_bad_irq, NULL); + irq_domain_free_irqs(irq, 1); + } +} + +static int meson_gpio_irq_set_type(struct irq_data *data, unsigned int type) +{ + int hwirq = meson_gpio_to_hwirq(data); + struct irq_fwspec fwspec; + int irq, irq2, num_slots; + + if (irqd_is_activated(data)) + return -EBUSY; + + if (hwirq < 0) + return hwirq; + + if (!hwirq) + return -ENXIO; + + fwspec.fwnode = meson_pinctrl_irq_domain->fwnode; + fwspec.param_count = 2; + + /* + * The chip can create an interrupt for either rising or falling edge + * only. Therefore use two interrupts in case of IRQ_TYPE_EDGE_BOTH, + * first for falling edge and second one for rising edge. + */ + num_slots = (type == IRQ_TYPE_EDGE_BOTH) ? 2 : 1; + + /* see comment in meson_gpio_irq_shutdown why we shift one bit here */ + fwspec.param[0] = hwirq << 1; + if (num_slots == 1) + fwspec.param[1] = type; + else + fwspec.param[1] = IRQ_TYPE_EDGE_FALLING; + + irq = irq_create_fwspec_mapping(&fwspec); + if (!irq) + return -EINVAL; + + irq_set_chained_handler_and_data(irq, meson_gpio_irq_handler, data); + + if (num_slots > 1) { + fwspec.param[0]++; + fwspec.param[1] = IRQ_TYPE_EDGE_RISING; + irq2 = irq_create_fwspec_mapping(&fwspec); + if (!irq2) { + irq_domain_free_irqs(irq, 1); + return -EINVAL; + } + irq_set_chained_handler_and_data(irq2, meson_gpio_irq_handler, data); + } + + return 0; +} + +static struct irq_chip meson_gpio_irq_chip = { + .name = "GPIO", + .irq_set_type = meson_gpio_irq_set_type, + .irq_mask = meson_gpio_irq_mask, + .irq_unmask = meson_gpio_irq_unmask, + .irq_shutdown = meson_gpio_irq_shutdown, +}; + static const struct of_device_id meson_pinctrl_dt_match[] = { { .compatible = "amlogic,meson8-cbus-pinctrl", @@ -558,7 +708,8 @@ static int meson_gpiolib_register(struct meson_pinctrl *pc) return ret; } - return 0; + return gpiochip_irqchip_add(&pc->chip, &meson_gpio_irq_chip, 0, + handle_simple_irq, IRQ_TYPE_NONE); } static struct regmap_config meson_regmap_config = { @@ -637,6 +788,23 @@ static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc, return PTR_ERR(pc->reg_gpio); } + if (!meson_pinctrl_irq_domain) { + np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gpio-intc"); + if (!np) { + dev_err(pc->dev, "interrupt controller DT node not found\n"); + return -EINVAL; + } + + meson_pinctrl_irq_domain = irq_find_host(np); + if (!meson_pinctrl_irq_domain) { + dev_err(pc->dev, "interrupt controller not found\n"); + of_node_put(np); + return -EINVAL; + } + + of_node_put(np); + } + return 0; }