From patchwork Tue Mar 25 12:51:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 27036 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f70.google.com (mail-qa0-f70.google.com [209.85.216.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2B4F220539 for ; Tue, 25 Mar 2014 12:52:45 +0000 (UTC) Received: by mail-qa0-f70.google.com with SMTP id hw13sf831315qab.9 for ; Tue, 25 Mar 2014 05:52:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=k6pckGqT2fs0uNlkEEi2m4eLwNl+X7QvsigYjRUKD1Y=; b=cDP0w4aPBQ8Oar3n50xCY5X2/3m5EvZBJcznTiRU+s/x/0VCeSZ3AdNR3ZxKsm8HsT RunzLK/X86zL8ZGxXdlBAGLYKJulGKoyphSO+wzGYC6PGiRZReu4819bKgEAqQVDIG2N bgY6ZsLziT+QhMathyDOCei0EYLA33d/v0+oxhSAIardatsm5punAyS/pz6wEZg/ZpWU 4pWqqL1nUZkw0LBqQtHxk42tG6Vkmq/UkocTcbH5HFGuAr9LVT67R6kbGRilZaedouw8 g6CPZCyIvZXxXMncW7dfq88lGOH5ZQfsoBAtN+gJcohjTb1W8764ylZ992GG7WvRadpM ucPQ== X-Gm-Message-State: ALoCoQm4FG9zHeObhcGfOSi7uJ/BkNQ2xPCiukdAB1bjIKRyfWgWyE3SXur8fHswm0VpeNfn41Qx X-Received: by 10.58.91.196 with SMTP id cg4mr3593104veb.41.1395751964826; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.81.209 with SMTP id f75ls198054qgd.48.gmail; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) X-Received: by 10.58.126.4 with SMTP id mu4mr23607720veb.0.1395751964758; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) Received: from mail-ve0-f177.google.com (mail-ve0-f177.google.com [209.85.128.177]) by mx.google.com with ESMTPS id w1si3694144vet.173.2014.03.25.05.52.44 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 25 Mar 2014 05:52:44 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.177; Received: by mail-ve0-f177.google.com with SMTP id sa20so438453veb.22 for ; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) X-Received: by 10.220.225.5 with SMTP id iq5mr73000vcb.42.1395751964654; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp301688vck; Tue, 25 Mar 2014 05:52:44 -0700 (PDT) X-Received: by 10.67.5.131 with SMTP id cm3mr901007pad.92.1395751963130; Tue, 25 Mar 2014 05:52:43 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u6si7530840paa.421.2014.03.25.05.52.42; Tue, 25 Mar 2014 05:52:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753149AbaCYMwj (ORCPT + 1 other); Tue, 25 Mar 2014 08:52:39 -0400 Received: from mail-we0-f181.google.com ([74.125.82.181]:56402 "EHLO mail-we0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753125AbaCYMwg (ORCPT ); Tue, 25 Mar 2014 08:52:36 -0400 Received: by mail-we0-f181.google.com with SMTP id q58so287912wes.12 for ; Tue, 25 Mar 2014 05:52:35 -0700 (PDT) X-Received: by 10.180.89.102 with SMTP id bn6mr23299648wib.28.1395751955340; Tue, 25 Mar 2014 05:52:35 -0700 (PDT) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id v6sm50116103wif.0.2014.03.25.05.52.33 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Mar 2014 05:52:34 -0700 (PDT) From: Linus Walleij To: linux-gpio@vger.kernel.org, Thomas Gleixner Cc: Alexandre Courbot , linux-kernel@vger.kernel.org, Linus Walleij Subject: [PATCH 1/4] gpio: add IRQ chip helpers in gpiolib Date: Tue, 25 Mar 2014 13:51:31 +0100 Message-Id: <1395751894-7110-2-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 1.8.5.3 In-Reply-To: <1395751894-7110-1-git-send-email-linus.walleij@linaro.org> References: <1395751894-7110-1-git-send-email-linus.walleij@linaro.org> Sender: linux-gpio-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-gpio@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This provides a function gpiochip_irqchip_add() to set up an irqchip for a GPIO controller, and a function gpiochip_set_chained_irqchip() to chain it to a parent irqchip. Most GPIOs are of the type where a number of lines form a cascaded interrupt controller chained onto the primary system interrupt controller (or further down the chain) so let's add this helper and factor the code to request the lines to be used as IRQs, the .to_irq() function and the irqdomain into the core as well. Suggested-by: Thomas Gleixner Signed-off-by: Linus Walleij Acked-by: Alexandre Courbot --- drivers/gpio/Kconfig | 3 + drivers/gpio/gpiolib.c | 188 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/gpio/driver.h | 29 +++++++ 3 files changed, 220 insertions(+) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 2e461e459d88..51c782532687 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -55,6 +55,9 @@ config GPIO_ACPI def_bool y depends on ACPI +config GPIOLIB_IRQCHIP + bool + config DEBUG_GPIO bool "Debug GPIO calls" depends on DEBUG_KERNEL diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 584d2b465f84..f76c952e08b2 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1254,6 +1254,9 @@ fail: } EXPORT_SYMBOL_GPL(gpiochip_add); +/* Forward-declaration */ +static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); + /** * gpiochip_remove() - unregister a gpio_chip * @chip: the chip to unregister @@ -1270,6 +1273,7 @@ int gpiochip_remove(struct gpio_chip *chip) spin_lock_irqsave(&gpio_lock, flags); + gpiochip_irqchip_remove(chip); gpiochip_remove_pin_ranges(chip); of_gpiochip_remove(chip); @@ -1339,6 +1343,190 @@ static struct gpio_chip *find_chip_by_name(const char *name) return gpiochip_find((void *)name, gpiochip_match_name); } +#ifdef CONFIG_GPIOLIB_IRQCHIP + +/* + * The following is irqchip helper code for gpiochips. + */ + +/** + * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip + * @gpiochip: the gpiochip to add the irqchip to + * @irqchip: the irqchip to add to the gpiochip + * @parent_irq: the irq number corresponding to the parent IRQ for this + * chained irqchip + * @parent_handler: the parent interrupt handler for the accumulated IRQ + * coming out of the gpiochip + */ +void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, + struct irq_chip *irqchip, + int parent_irq, + irq_flow_handler_t parent_handler) +{ + irq_set_chained_handler(parent_irq, parent_handler); + /* + * The parent irqchip is already using the chip_data for this + * irqchip, so our callbacks simply use the handler_data. + */ + irq_set_handler_data(parent_irq, gpiochip); +} +EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); + +/** + * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip + * @d: the irqdomain used by this irqchip + * @irq: the global irq number used by this GPIO irqchip irq + * @hwirq: the local IRQ/GPIO line offset on this gpiochip + * + * This function will set up the mapping for a certain IRQ line on a + * gpiochip by assigning the gpiochip as chip data, and using the irqchip + * stored inside the gpiochip. + */ +static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, + irq_hw_number_t hwirq) +{ + struct gpio_chip *chip = d->host_data; + + irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); + irq_set_chip_data(irq, chip); +#ifdef CONFIG_ARM + set_irq_flags(irq, IRQF_VALID); +#else + irq_set_noprobe(irq); +#endif + irq_set_irq_type(irq, chip->irq_default_type); + + return 0; +} + +static const struct irq_domain_ops gpiochip_domain_ops = { + .map = gpiochip_irq_map, + /* Virtually all GPIO irqchips are twocell:ed */ + .xlate = irq_domain_xlate_twocell, +}; + +static int gpiochip_irq_reqres(struct irq_data *d) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(d); + + if (gpio_lock_as_irq(chip, d->hwirq)) { + chip_err(chip, + "unable to lock HW IRQ %lu for IRQ\n", + d->hwirq); + return -EINVAL; + } + return 0; +} + +static void gpiochip_irq_relres(struct irq_data *d) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(d); + + gpio_unlock_as_irq(chip, d->hwirq); +} + +static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) +{ + return irq_find_mapping(chip->irqdomain, offset); +} + +/** + * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip + * @gpiochip: the gpiochip to remove the irqchip from + * + * This is called only from gpiochip_remove() + */ +static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) +{ + if (gpiochip->irqdomain) + irq_domain_remove(gpiochip->irqdomain); + + if (gpiochip->irqchip) { + gpiochip->irqchip->irq_request_resources = NULL; + gpiochip->irqchip->irq_release_resources = NULL; + gpiochip->irqchip = NULL; + } +} + +/** + * gpiochip_irqchip_add() - adds an irqchip to a gpiochip + * @gpiochip: the gpiochip to add the irqchip to + * @irqchip: the irqchip to add to the gpiochip + * @first_irq: if not dynamically assigned, the base (first) IRQ to + * allocate gpiochip irqs from + * @handler: the irq handler to use (often a predefined irq core function) + * @type: the default type for IRQs on this irqchip + * + * This function closely associates a certain irqchip with a certain + * gpiochip, providing an irq domain to translate the local IRQs to + * global irqs in the gpiolib core, and making sure that the gpiochip + * is passed as chip data to all related functions. Driver callbacks + * need to use container_of() to get their local state containers back + * from the gpiochip passed as chip data. An irqdomain will be stored + * in the gpiochip that shall be used by the driver to handle IRQ number + * translation. The gpiochip will need to be initialized and registered + * before calling this function. + * + * This function will handle two cell:ed simple IRQs. Everything else + * need to be open coded. + */ +int gpiochip_irqchip_add(struct gpio_chip *gpiochip, + struct irq_chip *irqchip, + unsigned int first_irq, + irq_flow_handler_t handler, + unsigned int type) +{ + struct device_node *of_node; + unsigned int offset; + + if (!gpiochip || !irqchip) + return -EINVAL; + + if (!gpiochip->dev) { + pr_err("missing gpiochip .dev parent pointer\n"); + return -EINVAL; + } + of_node = gpiochip->dev->of_node; +#if CONFIG_OF_GPIO + /* + * If the gpiochip has an assigned OF node this takes precendence + * FIXME: get rid of this and use gpiochip->dev->of_node everywhere + */ + if (gpiochip->of_node) + of_node = gpiochip->of_node; +#endif + gpiochip->irqchip = irqchip; + gpiochip->irq_handler = handler; + gpiochip->irq_default_type = type; + gpiochip->to_irq = gpiochip_to_irq; + gpiochip->irqdomain = irq_domain_add_simple(of_node, + gpiochip->ngpio, first_irq, + &gpiochip_domain_ops, gpiochip); + if (!gpiochip->irqdomain) { + gpiochip->irqchip = NULL; + return -EINVAL; + } + irqchip->irq_request_resources = gpiochip_irq_reqres; + irqchip->irq_release_resources = gpiochip_irq_relres; + + /* + * Prepare the mapping since the irqchip shall be orthogonal to + * to any gpiochip calls. If the first_irq was zero, this is + * necessary to allocate descriptors for all IRQs. + */ + for (offset = 0; offset < gpiochip->ngpio; offset++) + irq_create_mapping(gpiochip->irqdomain, offset); + + return 0; +} +EXPORT_SYMBOL_GPL(gpiochip_irqchip_add); + +#else /* CONFIG_GPIOLIB_IRQCHIP */ + +static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} + +#endif /* CONFIG_GPIOLIB_IRQCHIP */ + #ifdef CONFIG_PINCTRL /** diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 9fe283642253..c1c5c2368fc8 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -3,6 +3,9 @@ #include #include +#include +#include +#include struct device; struct gpio_desc; @@ -97,6 +100,17 @@ struct gpio_chip { bool can_sleep; bool exported; +#ifdef CONFIG_GPIOLIB_IRQCHIP + /* + * With CONFIG_GPIO_IRQCHIP we get an irqchip inside the gpiolib + * to handle IRQs for most practical cases. + */ + struct irq_chip *irqchip; + struct irq_domain *irqdomain; + irq_flow_handler_t irq_handler; + unsigned int irq_default_type; +#endif + #if defined(CONFIG_OF_GPIO) /* * If CONFIG_OF is enabled, then all GPIO controllers described in the @@ -190,6 +204,21 @@ struct gpiod_lookup_table { void gpiod_add_lookup_table(struct gpiod_lookup_table *table); +#ifdef CONFIG_GPIOLIB_IRQCHIP + +void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, + struct irq_chip *irqchip, + int parent_irq, + irq_flow_handler_t parent_handler); + +int gpiochip_irqchip_add(struct gpio_chip *gpiochip, + struct irq_chip *irqchip, + unsigned int first_irq, + irq_flow_handler_t handler, + unsigned int type); + +#endif /* CONFIG_GPIO_IRQCHIP */ + #else /* CONFIG_GPIOLIB */ static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)