From patchwork Fri Oct 13 15:49:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 825524 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-tegra-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="VPIQ7Ung"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yDBxk4PtNz9sRm for ; Sat, 14 Oct 2017 02:50:18 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758672AbdJMPtt (ORCPT ); Fri, 13 Oct 2017 11:49:49 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:44042 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758650AbdJMPtq (ORCPT ); Fri, 13 Oct 2017 11:49:46 -0400 Received: by mail-qt0-f195.google.com with SMTP id 8so20393849qtv.1; Fri, 13 Oct 2017 08:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=o6FO7M/8mTCUR9wrupgllsDbmDyWbT1020s5j2KJrgY=; b=VPIQ7UngjGU2IXun8X13rHfjo8I+W1pO8NrB7eN/3eE625iaVXO5Hsj9WFLwqJHB7d grYWU08nmn+KJKFvgUdC2DqckDC/n2rqLc1kh/XjNTdBO9iJTSNUEcYsfnPkGjQAmD0j 3vFpLM3Oi3ZA7r89P2l4YWEfEUvEsxICc9XuJy+dxi8Z4H8n2peHzL0uzNwt5gWy94gG QAnc6yfeJdvP3M8t7Gv8mqLXgE4LW5D7Xbf2x4J2AE400ch20NSMTYiMu9uCuVjhfAy1 b9D4Y7MYDJ9ocFEGgBRXWOkLsLZa1drWOV6KE8loczBaNQ0YM9o+JdOpqHOomVGMm5+S mYEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=o6FO7M/8mTCUR9wrupgllsDbmDyWbT1020s5j2KJrgY=; b=GZTjdEJa7cRXx7BeGdwsCVjqELbOHNwxRYJs4cIq53j9LtROBbVLWV83iXgcsjYJZa ydHOkuDgfQ51C8qq5V8KB9e57EMmxHXWDrHhF3f9q7Q6lHbZD7HaEj3UhcOhjPuSLM/S BccT8H7aH8o16mEmVOt9abh8dJLxI1PYFpCTqYJOBTUWA9Hfvi2ySCjngIrij45KswZa t9tlryC1mwEc4pcp80vlwMtdQcFdwMUTDUcteDh85mjHxwBdfVerf1/rFramIMeZhTaK iLAoGra9umrm78+mQhNCMkJGYT+sT9z2dcUyJtH5xB3HJUdxKcK0qRuje56InDTNrvuZ /Kfg== X-Gm-Message-State: AMCzsaW7pJgO6G349upwRWdHNFf79xlu+hRhT1B78w2UVrOljRzbh/ri O7NvOkDnWOPNlrfl7bHa8V8aXw== X-Google-Smtp-Source: ABhQp+Qeutbv/LN1h0smER5mN7k/RHYt4I/BfWo83hHamG8pfJ9OJhi4FzsXWRyOUi4D1TNpLafisg== X-Received: by 10.237.63.129 with SMTP id s1mr2768208qth.89.1507909785321; Fri, 13 Oct 2017 08:49:45 -0700 (PDT) Received: from localhost (p200300E41BE4FD00CEAD5B94E1CFD280.dip0.t-ipconnect.de. [2003:e4:1be4:fd00:cead:5b94:e1cf:d280]) by smtp.gmail.com with ESMTPSA id r16sm765043qtc.4.2017.10.13.08.49.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 13 Oct 2017 08:49:44 -0700 (PDT) From: Thierry Reding To: Linus Walleij Cc: Jonathan Hunter , Grygorii Strashko , linux-gpio@vger.kernel.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 11/13] gpio: Implement tighter IRQ chip integration Date: Fri, 13 Oct 2017 17:49:11 +0200 Message-Id: <20171013154913.29448-12-thierry.reding@gmail.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171013154913.29448-1-thierry.reding@gmail.com> References: <20171013154913.29448-1-thierry.reding@gmail.com> Sender: linux-tegra-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-tegra@vger.kernel.org From: Thierry Reding Currently GPIO drivers are required to add the GPIO chip and its corresponding IRQ chip separately, which can result in a lot of boilerplate. Use the newly introduced struct gpio_irq_chip, embedded in struct gpio_chip, that drivers can fill in if they want the GPIO core to automatically register the IRQ chip associated with a GPIO chip. Signed-off-by: Thierry Reding --- drivers/gpio/gpiolib.c | 125 +++++++++++++++++++++++++++++++++++++++++++- include/linux/gpio/driver.h | 7 +++ 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index eb487af120d9..672b61024413 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -72,6 +72,7 @@ static LIST_HEAD(gpio_lookup_list); LIST_HEAD(gpio_devices); static void gpiochip_free_hogs(struct gpio_chip *chip); +static int gpiochip_add_irqchip(struct gpio_chip *gpiochip); static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); @@ -1260,6 +1261,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data) if (status) goto err_remove_from_list; + status = gpiochip_add_irqchip(chip); + if (status) + goto err_remove_chip; + status = of_gpiochip_add(chip); if (status) goto err_remove_chip; @@ -1701,9 +1706,120 @@ static void gpiochip_irq_relres(struct irq_data *d) static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) { + unsigned int irq; + int err; + if (!gpiochip_irqchip_irq_valid(chip, offset)) return -ENXIO; - return irq_create_mapping(chip->irq.domain, offset); + + irq = irq_create_mapping(chip->irq.domain, offset); + if (!irq) + return 0; + + if (chip->irq.map) { + err = irq_set_parent(irq, chip->irq.map[offset]); + if (err < 0) + return err; + } + + return irq; +} + +/** + * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip + * @gpiochip: the GPIO chip to add the IRQ chip to + */ +static int gpiochip_add_irqchip(struct gpio_chip *gpiochip) +{ + struct irq_chip *irqchip = gpiochip->irq.chip; + const struct irq_domain_ops *ops; + struct device_node *np; + unsigned int type; + unsigned int i; + + if (!irqchip) + return 0; + + if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { + chip_err(gpiochip, "you cannot have chained interrupts on a " + "chip that may sleep\n"); + return -EINVAL; + } + + type = gpiochip->irq.default_type; + np = gpiochip->parent->of_node; + +#ifdef CONFIG_OF_GPIO + /* + * If the gpiochip has an assigned OF node this takes precedence + * FIXME: get rid of this and use gpiochip->parent->of_node + * everywhere + */ + if (gpiochip->of_node) + np = gpiochip->of_node; +#endif + + /* + * Specifying a default trigger is a terrible idea if DT or ACPI is + * used to configure the interrupts, as you may end up with + * conflicting triggers. Tell the user, and reset to NONE. + */ + if (WARN(np && type != IRQ_TYPE_NONE, + "%s: Ignoring %u default trigger\n", np->full_name, type)) + type = IRQ_TYPE_NONE; + + if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { + acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), + "Ignoring %u default trigger\n", type); + type = IRQ_TYPE_NONE; + } + + gpiochip->to_irq = gpiochip_to_irq; + gpiochip->irq.default_type = type; + + if (gpiochip->irq.domain_ops) + ops = gpiochip->irq.domain_ops; + else + ops = &gpiochip_domain_ops; + + gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio, + gpiochip->irq.first, + ops, gpiochip); + if (!gpiochip->irq.domain) + return -EINVAL; + + /* + * It is possible for a driver to override this, but only if the + * alternative functions are both implemented. + */ + if (!irqchip->irq_request_resources && + !irqchip->irq_release_resources) { + irqchip->irq_request_resources = gpiochip_irq_reqres; + irqchip->irq_release_resources = gpiochip_irq_relres; + } + + if (gpiochip->irq.parent_handler) { + void *data = gpiochip->irq.parent_handler_data ?: gpiochip; + + for (i = 0; i < gpiochip->irq.num_parents; i++) { + /* + * The parent IRQ chip is already using the chip_data + * for this IRQ chip, so our callbacks simply use the + * handler_data. + */ + irq_set_chained_handler_and_data(gpiochip->irq.parents[i], + gpiochip->irq.parent_handler, + data); + } + + gpiochip->irq.nested = false; + } else { + gpiochip->irq.nested = true; + } + + acpi_gpiochip_request_interrupts(gpiochip); + + return 0; } /** @@ -1718,7 +1834,7 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) acpi_gpiochip_free_interrupts(gpiochip); - if (gpiochip->irq.num_parents > 0) { + if (gpiochip->irq.chip) { struct gpio_irq_chip *irq = &gpiochip->irq; unsigned int i; @@ -1852,6 +1968,11 @@ EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); #else /* CONFIG_GPIOLIB_IRQCHIP */ +static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip) +{ + return 0; +} + static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) { diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 4ae8dd945617..6e344ddd4df5 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -108,6 +108,13 @@ struct gpio_irq_chip { */ unsigned int *parents; + /** + * @map: + * + * A list of interrupt parents for each line of a GPIO chip. + */ + unsigned int *map; + /** * @nested: *