From patchwork Sun Sep 17 09:39:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 112829 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp2503954qgf; Sun, 17 Sep 2017 02:40:20 -0700 (PDT) X-Received: by 10.98.26.74 with SMTP id a71mr29428010pfa.286.1505641219953; Sun, 17 Sep 2017 02:40:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505641219; cv=none; d=google.com; s=arc-20160816; b=G5ZZNZNRdI0xlNdvzl9ZpaTHFR7BY4YafUQumMuXxCMAkVjORJoBfouuJtIxkaigJ7 iTMiMZ132yBHwgNVMU5b0F3OMt7B/IwbdQiuItjzkBrjJQPQrnOx16WDy7pahqmrB6Gk la5SAza47p+rbel+v1sd1x0fUykwEzqEdhMDfHc2uvNH2ymFjFk9Bs6WagxMtsJEg800 ww5aPo4XtjKRZo3Lrjr0zV9xJ6XSNmVxfGR0ItvY5hCsLKD+7K+Tl+tt1KlqJTHNoNbZ AY9O9DVThijN6SWW/sZZi3XaK3cl3YO+DPhHrU0DTEN2/1znkaedP/iz9o1n8j0qXxCj f/2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Y1VLeRKXOxFcngVRxxUIqqi7JFtX1kvthA2oNKtATuI=; b=HxLG+FqF5NUk58PgwkfKDxt5kzEM+o+pHDdFniuX5MzOxrLq3OmEM/+jKo1uhCjbrR wnj3cMLNRpIsUMUMoL7Y13Xs8gYu5qBXWMG+dSQYChLQdL1O6jP8EE7ss0w0UV2f5UFf +mQk7X7LoEH4lOp4MysTsaDiXHD18DXcijD+8T7vV+RIvHvdre+ft3gbhgfuE4gREQ9x BzbICdZVrfT5T0MzLMSNFkBE2U+8WXPZHzeTxvVwWZPpuazjsbs/Em1esRADWJv4J5mP 1ZCTYZ745HJMFA2+uk+7EzNyGyFU12TrfCMt+A99UH5CCG1yAJr9RGFebXIAyRQnjsqA jzIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MSWejNoo; spf=pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-i2c-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a18si3059295pfk.150.2017.09.17.02.40.19; Sun, 17 Sep 2017 02:40:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MSWejNoo; spf=pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-i2c-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751298AbdIQJkT (ORCPT + 3 others); Sun, 17 Sep 2017 05:40:19 -0400 Received: from mail-lf0-f41.google.com ([209.85.215.41]:56577 "EHLO mail-lf0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751186AbdIQJkS (ORCPT ); Sun, 17 Sep 2017 05:40:18 -0400 Received: by mail-lf0-f41.google.com with SMTP id a18so5563285lfl.13 for ; Sun, 17 Sep 2017 02:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y1VLeRKXOxFcngVRxxUIqqi7JFtX1kvthA2oNKtATuI=; b=MSWejNoohC5PPHM0DMfoZpDvQmq0B3GA6vrjmZqDwCvAl7TqA1MH+6WZ45E/7Enqu6 dHWwmoeIkVtQZTABUE1yfDHhRcL99JN5BctIAUYY9NhpeHYEOEhw6sUKMBf/yq3X9575 Tboefojb6wMQkF3id0L3JYdyOaU72IWy81ZQw= 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=Y1VLeRKXOxFcngVRxxUIqqi7JFtX1kvthA2oNKtATuI=; b=ad+ihO3p7hwf88Lie7plvp5WdxvsuQnlbcqTJszQHsAmy/h6Wa+J2hCJ6PgiOoiZvt 5YuIXcYaxkucTpPgB6c2LqTJMaihWTNz+PzDZA2PIoQCSnRY6XFgdY0dBxpvy+aV7CQi q5F0Z0UjMIm5TXVGHp+2Hywt/2UGp+0rjfFQMmYAWLbGJ83Q+IiX2F3Lw5R3SFsTPDAJ o92t84YG70sJ1qKYRmIh5rc9in2jK355g/FgkNn+1RPmFidHseWVQhSbQFomDgPcmIkQ ZigKqdtK25DxZbFms2JUz/yjoo/yTQ7mFAjxobb0vivllCGIgh9knJ0PENCCm6RZwXQs kTWQ== X-Gm-Message-State: AHPjjUgoyR5Thi7O4zKWG/Fe8gpBVuJoISc3x2Qk2hAodMhMUhy+21FO RgnJ3W4hcycIL+DH X-Google-Smtp-Source: AOwi7QAmOLGzGESEjkytdRvn7bxaaTHsN1+LSWjF7+CDybZq/niGRNPrVjIxoUk3U/Uq/vJiRI2M/w== X-Received: by 10.25.79.8 with SMTP id d8mr2202310lfb.118.1505641216590; Sun, 17 Sep 2017 02:40:16 -0700 (PDT) Received: from fabina.bredbandsbolaget.se (c-2209e055.014-348-6c756e10.cust.bredbandsbolaget.se. [85.224.9.34]) by smtp.gmail.com with ESMTPSA id t84sm974559lfi.21.2017.09.17.02.40.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 17 Sep 2017 02:40:15 -0700 (PDT) From: Linus Walleij To: Wolfram Sang , linux-i2c@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, adi-buildroot-devel@lists.sourceforge.net, Geert Uytterhoeven , Linus Walleij Subject: [PATCH 7/7] i2c: gpio: Add support for named gpios in DT Date: Sun, 17 Sep 2017 11:39:06 +0200 Message-Id: <20170917093906.16325-8-linus.walleij@linaro.org> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170917093906.16325-1-linus.walleij@linaro.org> References: <20170917093906.16325-1-linus.walleij@linaro.org> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org This adds support for using the "sda" and "scl" GPIOs in device tree instead of anonymously using index 0 and 1 of the "gpios" property. We add a helper function to retrieve the GPIO descriptors and some explicit error handling since the probe may have to be deferred. At least this happened to me when moving to using named "sda" and "scl" lines (all of a sudden this started to probe before the GPIO driver) so we need to gracefully defer probe when we ge -ENOENT in the error pointer. Suggested-by: Geert Uytterhoeven Signed-off-by: Linus Walleij --- This is pretty much a rewrite of Geerts patch on top of my own changes to support descriptors. --- drivers/i2c/busses/i2c-gpio.c | 59 +++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 16 deletions(-) -- 2.13.5 diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c index beb5ce523684..2738b851f470 100644 --- a/drivers/i2c/busses/i2c-gpio.c +++ b/drivers/i2c/busses/i2c-gpio.c @@ -82,6 +82,42 @@ static void of_i2c_gpio_get_props(struct device_node *np, of_property_read_bool(np, "i2c-gpio,scl-output-only"); } +static struct gpio_desc *i2c_gpio_get_desc(struct device *dev, + const char *con_id, + unsigned int index, + enum gpiod_flags gflags) +{ + struct gpio_desc *retdesc; + int ret; + + retdesc = devm_gpiod_get(dev, con_id, gflags); + if (!IS_ERR(retdesc)) { + dev_dbg(dev, "got GPIO from name %s\n", con_id); + return retdesc; + } + + retdesc = devm_gpiod_get_index(dev, NULL, index, gflags); + if (!IS_ERR(retdesc)) { + dev_dbg(dev, "got GPIO from index %u\n", index); + return retdesc; + } + + ret = PTR_ERR(retdesc); + + /* FIXME: hack in the old code, is this really necessary? */ + if (ret == -EINVAL) + retdesc = ERR_PTR(-EPROBE_DEFER); + + /* This happens if the GPIO driver is not yet probed, let's defer */ + if (ret == -ENOENT) + retdesc = ERR_PTR(-EPROBE_DEFER); + + if (ret != -EPROBE_DEFER) + dev_err(dev, "error trying to get descriptor: %ld\n", ret); + + return retdesc; +} + static int i2c_gpio_probe(struct platform_device *pdev) { struct i2c_gpio_private_data *priv; @@ -125,14 +161,10 @@ static int i2c_gpio_probe(struct platform_device *pdev) gflags = GPIOD_OUT_HIGH; else gflags = GPIOD_OUT_HIGH_OPEN_DRAIN; - priv->sda = devm_gpiod_get_index(dev, NULL, 0, gflags); - if (IS_ERR(priv->sda)) { - ret = PTR_ERR(priv->sda); - /* FIXME: hack in the old code, is this really necessary? */ - if (ret == -EINVAL) - ret = -EPROBE_DEFER; - return ret; - } + priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags); + if (IS_ERR(priv->sda)) + return PTR_ERR(priv->sda); + /* * If the SCL line is marked from platform data or device tree as * "open drain" it means something outside of our control is making @@ -144,14 +176,9 @@ static int i2c_gpio_probe(struct platform_device *pdev) gflags = GPIOD_OUT_LOW; else gflags = GPIOD_OUT_LOW_OPEN_DRAIN; - priv->scl = devm_gpiod_get_index(dev, NULL, 1, gflags); - if (IS_ERR(priv->scl)) { - ret = PTR_ERR(priv->scl); - /* FIXME: hack in the old code, is this really necessary? */ - if (ret == -EINVAL) - ret = -EPROBE_DEFER; - return ret; - } + priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags); + if (IS_ERR(priv->scl)) + return PTR_ERR(priv->scl); bit_data->setsda = i2c_gpio_setsda_val; bit_data->setscl = i2c_gpio_setscl_val;