From patchwork Mon Sep 5 20:28:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sanjeev Premi X-Patchwork-Id: 113438 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 0C923B6F76 for ; Tue, 6 Sep 2011 06:29:29 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 8B68128085; Mon, 5 Sep 2011 22:29:23 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SSYzH427Mrns; Mon, 5 Sep 2011 22:29:23 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D2FFF2807D; Mon, 5 Sep 2011 22:29:15 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 997992807B for ; Mon, 5 Sep 2011 22:29:11 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Zcc5FwUyDE38 for ; Mon, 5 Sep 2011 22:29:09 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from arroyo.ext.ti.com (arroyo.ext.ti.com [192.94.94.40]) by theia.denx.de (Postfix) with ESMTPS id 4A75F2807D for ; Mon, 5 Sep 2011 22:29:07 +0200 (CEST) Received: from dbdp20.itg.ti.com ([172.24.170.38]) by arroyo.ext.ti.com (8.13.7/8.13.7) with ESMTP id p85KT2l4032298 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 5 Sep 2011 15:29:05 -0500 Received: from dbde70.ent.ti.com (localhost [127.0.0.1]) by dbdp20.itg.ti.com (8.13.8/8.13.8) with ESMTP id p85KT2YR009902 for ; Tue, 6 Sep 2011 01:59:02 +0530 (IST) Received: from dbdp31.itg.ti.com (172.24.170.98) by DBDE70.ent.ti.com (172.24.170.148) with Microsoft SMTP Server id 8.3.106.1; Tue, 6 Sep 2011 01:59:02 +0530 Received: from localhost.localdomain (dbdp20.itg.ti.com [172.24.170.38]) by dbdp31.itg.ti.com (8.13.8/8.13.8) with ESMTP id p85KSvd7018686; Tue, 6 Sep 2011 01:59:01 +0530 (IST) From: Sanjeev Premi To: Date: Tue, 6 Sep 2011 01:58:46 +0530 Message-ID: <1315254528-8644-2-git-send-email-premi@ti.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1315254528-8644-1-git-send-email-premi@ti.com> References: <1315254528-8644-1-git-send-email-premi@ti.com> MIME-Version: 1.0 Subject: [U-Boot] [PATCH 1/3] omap: gpio: Use generic API X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Convert all OMAP specific functions to use the common API definitions in include/asm/gpio.h. In the process, made few additional changes: - Use -EINVAL consistently. -1 was used in many places. - Removed one-liner static functions that were used only once. Replaced the content as necessary. - Combines implementation of functions omap_get_gpio_dataout() and omap_get_gpio_datain(). To do so, new static function _get_gpio_direction() was added. Signed-off-by: Sanjeev Premi --- Changes since RFC: - Rebased against u-boot-arm.git - Dummy header is no longer necessary due to additional changes on the arm tree. - Added function _get_gpio_direction(). - Fold omap_get_gpio_dataout() and omap_get_gpio_datain() into single gpio_get_value(). arch/arm/cpu/armv7/omap-common/gpio.c | 113 ++++++++++++++++++++++++--------- arch/arm/include/asm/omap_gpio.h | 15 ----- 2 files changed, 82 insertions(+), 46 deletions(-) diff --git a/arch/arm/cpu/armv7/omap-common/gpio.c b/arch/arm/cpu/armv7/omap-common/gpio.c index 4749524..5b14c0f 100644 --- a/arch/arm/cpu/armv7/omap-common/gpio.c +++ b/arch/arm/cpu/armv7/omap-common/gpio.c @@ -40,6 +40,9 @@ #include #include +#define OMAP_GPIO_DIR_OUT 0 +#define OMAP_GPIO_DIR_IN 1 + static inline const struct gpio_bank *get_gpio_bank(int gpio) { return &omap_gpio_bank[gpio >> 5]; @@ -53,17 +56,17 @@ static inline int get_gpio_index(int gpio) static inline int gpio_valid(int gpio) { if (gpio < 0) - return -1; + return -EINVAL; if (gpio < 192) return 0; - return -1; + return -EINVAL; } static int check_gpio(int gpio) { if (gpio_valid(gpio) < 0) { printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); - return -1; + return -EINVAL; } return 0; } @@ -89,14 +92,29 @@ static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, __raw_writel(l, reg); } -void omap_set_gpio_direction(int gpio, int is_input) +/** + * Get the direction of the GPIO by reading the GPIO_OE register + * corresponding to the specified bank. + */ +static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) { - const struct gpio_bank *bank; + void *reg = bank->base; + u32 v; - if (check_gpio(gpio) < 0) - return; - bank = get_gpio_bank(gpio); - _set_gpio_direction(bank, get_gpio_index(gpio), is_input); + switch (bank->method) { + case METHOD_GPIO_24XX: + reg += OMAP_GPIO_OE; + break; + default: + return -EINVAL; + } + + v = __raw_readl(reg); + + if (v & (1 << gpio)) + return OMAP_GPIO_DIR_IN; + else + return OMAP_GPIO_DIR_OUT; } static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, @@ -121,20 +139,27 @@ static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, __raw_writel(l, reg); } -void omap_set_gpio_dataout(int gpio, int enable) +/** + * Set value of the specified gpio + */ +void gpio_set_value(int gpio, int value) { const struct gpio_bank *bank; if (check_gpio(gpio) < 0) return; bank = get_gpio_bank(gpio); - _set_gpio_dataout(bank, get_gpio_index(gpio), enable); + _set_gpio_dataout(bank, get_gpio_index(gpio), value); } -int omap_get_gpio_datain(int gpio) +/** + * Get value of the specified gpio + */ +int gpio_get_value(int gpio) { const struct gpio_bank *bank; void *reg; + int input; if (check_gpio(gpio) < 0) return -EINVAL; @@ -142,7 +167,17 @@ int omap_get_gpio_datain(int gpio) reg = bank->base; switch (bank->method) { case METHOD_GPIO_24XX: - reg += OMAP_GPIO_DATAIN; + input = _get_gpio_direction(bank, get_gpio_index(gpio)); + switch (input) { + case OMAP_GPIO_DIR_IN: + reg += OMAP_GPIO_DATAIN; + break; + case OMAP_GPIO_DIR_OUT: + reg += OMAP_GPIO_DATAOUT; + break; + default: + return -EINVAL; + } break; default: return -EINVAL; @@ -151,32 +186,45 @@ int omap_get_gpio_datain(int gpio) & (1 << get_gpio_index(gpio))) != 0; } -int omap_get_gpio_dataout(int gpio) +/** + * Set gpio direction as input + */ +int gpio_direction_input(unsigned gpio) { - struct gpio_bank *bank; - void *reg; + const struct gpio_bank *bank; if (check_gpio(gpio) < 0) return -EINVAL; + bank = get_gpio_bank(gpio); - reg = bank->base; - switch (bank->method) { - case METHOD_GPIO_24XX: - reg += OMAP_GPIO_DATAOUT; - break; - default: - return -EINVAL; - } - return (__raw_readl(reg) - & (1 << get_gpio_index(gpio))) != 0; + _set_gpio_direction(bank, get_gpio_index(gpio), 1); + + return 0; } -static void _reset_gpio(const struct gpio_bank *bank, int gpio) +/** + * Set gpio direction as output + */ +int gpio_direction_output(unsigned gpio, int value) { - _set_gpio_direction(bank, get_gpio_index(gpio), 1); + const struct gpio_bank *bank; + + if (check_gpio(gpio) < 0) + return -EINVAL; + + bank = get_gpio_bank(gpio); + _set_gpio_dataout(bank, get_gpio_index(gpio), value); + _set_gpio_direction(bank, get_gpio_index(gpio), 0); + + return 0; } -int omap_request_gpio(int gpio) +/** + * Request a gpio before using it. + * + * NOTE: Argument 'label' is unused. + */ +int gpio_request(int gpio, const char *label) { if (check_gpio(gpio) < 0) return -EINVAL; @@ -184,7 +232,10 @@ int omap_request_gpio(int gpio) return 0; } -void omap_free_gpio(int gpio) +/** + * Reset and free the gpio after using it. + */ +void gpio_free(unsigned gpio) { const struct gpio_bank *bank; @@ -192,5 +243,5 @@ void omap_free_gpio(int gpio) return; bank = get_gpio_bank(gpio); - _reset_gpio(bank, gpio); + _set_gpio_direction(bank, get_gpio_index(gpio), 1); } diff --git a/arch/arm/include/asm/omap_gpio.h b/arch/arm/include/asm/omap_gpio.h index 8741572..516cc42 100644 --- a/arch/arm/include/asm/omap_gpio.h +++ b/arch/arm/include/asm/omap_gpio.h @@ -49,19 +49,4 @@ extern const struct gpio_bank *const omap_gpio_bank; #define METHOD_GPIO_24XX 4 -/* This is the interface */ - -/* Request a gpio before using it */ -int omap_request_gpio(int gpio); -/* Reset and free a gpio after using it */ -void omap_free_gpio(int gpio); -/* Sets the gpio as input or output */ -void omap_set_gpio_direction(int gpio, int is_input); -/* Set or clear a gpio output */ -void omap_set_gpio_dataout(int gpio, int enable); -/* Get the value of a gpio input */ -int omap_get_gpio_datain(int gpio); -/* Get the value of a gpio output */ -int omap_get_gpio_dataout(int gpio); - #endif /* _GPIO_H_ */