From patchwork Sun Feb 23 16:05:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Shiyan X-Patchwork-Id: 3704311 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 697299F35F for ; Sun, 23 Feb 2014 16:06:13 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 31671201EF for ; Sun, 23 Feb 2014 16:06:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5FB2B201E9 for ; Sun, 23 Feb 2014 16:06:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751158AbaBWQGJ (ORCPT ); Sun, 23 Feb 2014 11:06:09 -0500 Received: from smtp17.mail.ru ([94.100.176.154]:38003 "EHLO smtp17.mail.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751314AbaBWQGI (ORCPT ); Sun, 23 Feb 2014 11:06:08 -0500 X-Greylist: delayed 91185 seconds by postgrey-1.27 at vger.kernel.org; Sun, 23 Feb 2014 11:06:07 EST DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.ru; s=mail2; h=Message-Id:Date:Subject:Cc:To:From; bh=6MOW3hHxs1HpFq8oh6OAvaLrnlQlERBdv/x4UTc3tQY=; b=ALafLIUEh3cPfvNsk6bMnt6VJjHVFjJq1XrAYg+oiDDqVGi8/54Kr9TinuLhdAkdShM7Z6PnVlOPu4gdscKfh++q4TysMAoRf9i8RVuRPvSBIdecXOaE9tXQ5uVTiRNUhuIHWDLtis2SRKDam9CTWvb3ZfMe9lj4kQF5Y8YXcnc=; Received: from [188.134.40.128] (port=47074 helo=shc.zet) by smtp17.mail.ru with esmtpa (envelope-from ) id 1WHbZ7-0002rA-0R; Sun, 23 Feb 2014 20:06:05 +0400 From: Alexander Shiyan To: linux-mmc@vger.kernel.org Cc: Alexander Shiyan Subject: [PATCH RFC] mmc: slot-gpio: Convert to gpiod_* API Date: Sun, 23 Feb 2014 20:05:52 +0400 Message-Id: <1393171552-8808-1-git-send-email-shc_work@mail.ru> X-Mailer: git-send-email 1.8.3.2 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM,RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Mras: Ok Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RDNS_NONE, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a RFC patch only, please do not apply it. This introduce a conversion slot-gpio to gpiod-* API. Usage of gpiod-* API allows us to remove all pieces of storage GPIOs in the driver's platform_data in the future. Patch saves calls to existing functions that allows us to convert all drivers to use new interface, one by one. Ready for comments and suggestions. --- drivers/mmc/core/slot-gpio.c | 221 ++++++++++++++++++++++++++---------------- include/linux/mmc/slot-gpio.h | 10 +- 2 files changed, 148 insertions(+), 83 deletions(-) diff --git a/drivers/mmc/core/slot-gpio.c b/drivers/mmc/core/slot-gpio.c index 46596b71..6fc0483 100644 --- a/drivers/mmc/core/slot-gpio.c +++ b/drivers/mmc/core/slot-gpio.c @@ -14,17 +14,14 @@ #include #include #include -#include -#include struct mmc_gpio { - int ro_gpio; - int cd_gpio; - char *ro_label; - char cd_label[0]; + struct gpio_desc *ro_gpio; + struct gpio_desc *cd_gpio; + char *cd_label; }; -static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) +static irqreturn_t mmc_gpio_cd_irq(int irq, void *dev_id) { /* Schedule a card detection after a debounce timeout */ struct mmc_host *host = dev_id; @@ -37,34 +34,26 @@ static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) return IRQ_HANDLED; } -static int mmc_gpio_alloc(struct mmc_host *host) +static int mmc_gpiod_alloc(struct mmc_host *host) { size_t len = strlen(dev_name(host->parent)) + 4; - struct mmc_gpio *ctx; - - mutex_lock(&host->slot.lock); + struct mmc_gpio *ctx = host->slot.handler_priv; - ctx = host->slot.handler_priv; if (!ctx) { /* * devm_kzalloc() can be called after device_initialize(), even * before device_add(), i.e., between mmc_alloc_host() and * mmc_add_host() */ - ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len, + ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + len, GFP_KERNEL); if (ctx) { - ctx->ro_label = ctx->cd_label + len; - snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent)); - snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent)); - ctx->cd_gpio = -EINVAL; - ctx->ro_gpio = -EINVAL; + snprintf(ctx->cd_label, len, "%s cd", + dev_name(host->parent)); host->slot.handler_priv = ctx; } } - mutex_unlock(&host->slot.lock); - return ctx ? 0 : -ENOMEM; } @@ -72,10 +61,10 @@ int mmc_gpio_get_ro(struct mmc_host *host) { struct mmc_gpio *ctx = host->slot.handler_priv; - if (!ctx || !gpio_is_valid(ctx->ro_gpio)) + if (!ctx || IS_ERR_OR_NULL(ctx->ro_gpio)) return -ENOSYS; - return !gpio_get_value_cansleep(ctx->ro_gpio) ^ + return !gpiod_get_value_cansleep(ctx->ro_gpio) ^ !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH); } EXPORT_SYMBOL(mmc_gpio_get_ro); @@ -84,18 +73,17 @@ int mmc_gpio_get_cd(struct mmc_host *host) { struct mmc_gpio *ctx = host->slot.handler_priv; - if (!ctx || !gpio_is_valid(ctx->cd_gpio)) + if (!ctx || IS_ERR_OR_NULL(ctx->cd_gpio)) return -ENOSYS; - return !gpio_get_value_cansleep(ctx->cd_gpio) ^ + return !gpiod_get_value_cansleep(ctx->cd_gpio) ^ !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH); } EXPORT_SYMBOL(mmc_gpio_get_cd); /** - * mmc_gpio_request_ro - request a gpio for write-protection + * mmc_gpiod_request_ro - request a gpio for write-protection * @host: mmc host - * @gpio: gpio number requested * * As devm_* managed functions are used in mmc_gpio_request_ro(), client * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up, @@ -106,35 +94,45 @@ EXPORT_SYMBOL(mmc_gpio_get_cd); * * Returns zero on success, else an error. */ -int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio) +int mmc_gpiod_request_ro(struct mmc_host *host) { - struct mmc_gpio *ctx; + struct mmc_gpio *ctx = host->slot.handler_priv; int ret; - if (!gpio_is_valid(gpio)) - return -EINVAL; - - ret = mmc_gpio_alloc(host); - if (ret < 0) + ret = mmc_gpiod_alloc(host); + if (ret) return ret; - ctx = host->slot.handler_priv; + ctx->ro_gpio = devm_gpiod_get(&host->class_dev, "wp"); + if (IS_ERR(ctx->ro_gpio)) + return PTR_ERR(ctx->ro_gpio); + + return gpiod_direction_input(ctx->ro_gpio); +} +EXPORT_SYMBOL(mmc_gpiod_request_ro); + +int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio) +{ + struct mmc_gpio *ctx = host->slot.handler_priv; + int ret; + + ret = mmc_gpiod_alloc(host); + if (ret) + return ret; - ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN, - ctx->ro_label); - if (ret < 0) + ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_IN, "wp"); + if (ret) return ret; - ctx->ro_gpio = gpio; + ctx->ro_gpio = gpio_to_desc(gpio); - return 0; + return IS_ERR(ctx->ro_gpio) ? PTR_ERR(ctx->ro_gpio) : 0; } EXPORT_SYMBOL(mmc_gpio_request_ro); /** - * mmc_gpio_request_cd - request a gpio for card-detection + * mmc_gpiod_request_cd - request a gpio for card-detection * @host: mmc host - * @gpio: gpio number requested * @debounce: debounce time in microseconds * * As devm_* managed functions are used in mmc_gpio_request_cd(), client @@ -150,60 +148,98 @@ EXPORT_SYMBOL(mmc_gpio_request_ro); * * Returns zero on success, else an error. */ -int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio, - unsigned int debounce) +int mmc_gpiod_request_cd(struct mmc_host *host, unsigned int debounce) { - struct mmc_gpio *ctx; - int irq = gpio_to_irq(gpio); + struct mmc_gpio *ctx = host->slot.handler_priv; int ret; - ret = mmc_gpio_alloc(host); - if (ret < 0) + ret = mmc_gpiod_alloc(host); + if (ret) return ret; - ctx = host->slot.handler_priv; + ctx->cd_gpio = devm_gpiod_get(&host->class_dev, "cd"); + if (IS_ERR(ctx->cd_gpio)) + return PTR_ERR(ctx->cd_gpio); - ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN, - ctx->cd_label); - if (ret < 0) - /* - * don't bother freeing memory. It might still get used by other - * slot functions, in any case it will be freed, when the device - * is destroyed. - */ + ret = gpiod_direction_input(ctx->cd_gpio); + if (ret) return ret; if (debounce) { - ret = gpio_set_debounce(gpio, debounce); - if (ret < 0) + ret = gpiod_set_debounce(ctx->cd_gpio, debounce); + if (ret) return ret; } /* - * Even if gpio_to_irq() returns a valid IRQ number, the platform might + * Even if gpiod_to_irq() returns a valid IRQ number, the platform might * still prefer to poll, e.g., because that IRQ number is already used * by another unit and cannot be shared. */ - if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL) - irq = -EINVAL; - - if (irq >= 0) { - ret = devm_request_threaded_irq(&host->class_dev, irq, - NULL, mmc_gpio_cd_irqt, + if (!(host->caps & MMC_CAP_NEEDS_POLL)) { + host->slot.cd_irq = gpiod_to_irq(ctx->cd_gpio); + ret = devm_request_threaded_irq(&host->class_dev, + host->slot.cd_irq, NULL, mmc_gpio_cd_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, ctx->cd_label, host); - if (ret < 0) - irq = ret; + if (ret) + host->slot.cd_irq = ret; + } else { + host->slot.cd_irq = -EINVAL; } - host->slot.cd_irq = irq; - - if (irq < 0) + if (host->slot.cd_irq < 0) host->caps |= MMC_CAP_NEEDS_POLL; - ctx->cd_gpio = gpio; + return ret; +} +EXPORT_SYMBOL(mmc_gpiod_request_cd); + +int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio, + unsigned int debounce) +{ + struct mmc_gpio *ctx = host->slot.handler_priv; + int ret; + + ret = mmc_gpiod_alloc(host); + if (ret) + return ret; - return 0; + ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_IN, "cd"); + if (ret) + return ret; + + ctx->cd_gpio = gpio_to_desc(gpio); + if (IS_ERR(ctx->cd_gpio)) + return PTR_ERR(ctx->cd_gpio); + + if (debounce) { + ret = gpiod_set_debounce(ctx->cd_gpio, debounce); + if (ret) + return ret; + } + + /* + * Even if gpiod_to_irq() returns a valid IRQ number, the platform might + * still prefer to poll, e.g., because that IRQ number is already used + * by another unit and cannot be shared. + */ + if (!(host->caps & MMC_CAP_NEEDS_POLL)) { + host->slot.cd_irq = gpiod_to_irq(ctx->cd_gpio); + ret = devm_request_threaded_irq(&host->class_dev, + host->slot.cd_irq, NULL, mmc_gpio_cd_irq, + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, + ctx->cd_label, host); + if (ret) + host->slot.cd_irq = ret; + } else { + host->slot.cd_irq = -EINVAL; + } + + if (host->slot.cd_irq < 0) + host->caps |= MMC_CAP_NEEDS_POLL; + + return ret; } EXPORT_SYMBOL(mmc_gpio_request_cd); @@ -214,18 +250,41 @@ EXPORT_SYMBOL(mmc_gpio_request_cd); * It's provided only for cases that client drivers need to manually free * up the write-protection gpio requested by mmc_gpio_request_ro(). */ +void mmc_gpiod_free(struct mmc_host *host) +{ + struct mmc_gpio *ctx = host->slot.handler_priv; + + if (!ctx) + return; + + if (!IS_ERR_OR_NULL(ctx->ro_gpio)) + devm_gpiod_put(&host->class_dev, ctx->ro_gpio); + + if (!IS_ERR_OR_NULL(ctx->cd_gpio)) { + if (host->slot.cd_irq >= 0) { + devm_free_irq(&host->class_dev, host->slot.cd_irq, host); + host->slot.cd_irq = -EINVAL; + } + + devm_gpiod_put(&host->class_dev, ctx->cd_gpio); + } + + devm_kfree(&host->class_dev, ctx); + + ctx = NULL; +} +EXPORT_SYMBOL(mmc_gpiod_free); + void mmc_gpio_free_ro(struct mmc_host *host) { struct mmc_gpio *ctx = host->slot.handler_priv; - int gpio; - if (!ctx || !gpio_is_valid(ctx->ro_gpio)) + if (!ctx || IS_ERR_OR_NULL(ctx->ro_gpio)) return; - gpio = ctx->ro_gpio; - ctx->ro_gpio = -EINVAL; + devm_gpiod_put(&host->class_dev, ctx->ro_gpio); - devm_gpio_free(&host->class_dev, gpio); + ctx->ro_gpio = NULL; } EXPORT_SYMBOL(mmc_gpio_free_ro); @@ -239,9 +298,8 @@ EXPORT_SYMBOL(mmc_gpio_free_ro); void mmc_gpio_free_cd(struct mmc_host *host) { struct mmc_gpio *ctx = host->slot.handler_priv; - int gpio; - if (!ctx || !gpio_is_valid(ctx->cd_gpio)) + if (!ctx || IS_ERR_OR_NULL(ctx->cd_gpio)) return; if (host->slot.cd_irq >= 0) { @@ -249,9 +307,8 @@ void mmc_gpio_free_cd(struct mmc_host *host) host->slot.cd_irq = -EINVAL; } - gpio = ctx->cd_gpio; - ctx->cd_gpio = -EINVAL; + devm_gpiod_put(&host->class_dev, ctx->cd_gpio); - devm_gpio_free(&host->class_dev, gpio); + ctx->cd_gpio = NULL; } EXPORT_SYMBOL(mmc_gpio_free_cd); diff --git a/include/linux/mmc/slot-gpio.h b/include/linux/mmc/slot-gpio.h index b0c73e4..a31ed96 100644 --- a/include/linux/mmc/slot-gpio.h +++ b/include/linux/mmc/slot-gpio.h @@ -14,10 +14,18 @@ struct mmc_host; int mmc_gpio_get_ro(struct mmc_host *host); +#define mmc_gpiod_get_ro(host) mmc_gpio_get_ro(host) +int mmc_gpiod_request_ro(struct mmc_host *host); + +int mmc_gpio_get_cd(struct mmc_host *host); +#define mmc_gpiod_get_cd(host) mmc_gpio_get_cd(host) +int mmc_gpiod_request_cd(struct mmc_host *host, unsigned int debounce); + +void mmc_gpiod_free(struct mmc_host *host); + int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio); void mmc_gpio_free_ro(struct mmc_host *host); -int mmc_gpio_get_cd(struct mmc_host *host); int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio, unsigned int debounce); void mmc_gpio_free_cd(struct mmc_host *host);