From patchwork Thu Oct 5 11:03:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 114918 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp456245qgn; Thu, 5 Oct 2017 04:03:48 -0700 (PDT) X-Received: by 10.84.235.67 with SMTP id g3mr23307572plt.326.1507201428867; Thu, 05 Oct 2017 04:03:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507201428; cv=none; d=google.com; s=arc-20160816; b=hdcoT0Jgu5kGfhZ3FnEFIkK4+gkmIVOLpvY4kgizIJMJpuFZV8ITdM+dAYEbKgw1Ar azYKHH6Vde+Mg3T/6lcrK+yTceyuteWEwZ5Jcl2fjqw6ScAqqtev54cwSN/97qEToErE K3MCLZ3uwuOWeIaDTLr9uwFlijcp/VlYLbcpaEg6wii4oBgJqeVRsmYB3PFYBc5iOpJl gP7Y7y4khqK2SjjDezB2HfjKHIIS3HUXWASdEbYTgxLIZVuLz5F1nmt3D6tp4BVLVujA 4l62ehCHB8h3mDLhDepgiNFL5j9xB3feO9QJpTem/RCueR+XERYbI+mkB9TIEolfaiES L7IQ== 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=gKrABHhe1QEt6ic49IW0E/MQWMxvtZhhcDR7JsU+QlI=; b=ntOVTZ61l/5ONTDs2741/hibXd4y+Ry+1IpW9Pehvgs/9x6DgXCaqdPJWG+JtcHAsJ 45pKLZermNt9obh51NwFJE+DyTdU3fIxKhUoXdYePeTlyWWeUoKKQc/qBQzc1n/msO5M C0nLA48LEFaGZn9EBnnqQ40ypY6XDQbA6Xo0uJ2Bu3cOqEgGUy3ys73uJStfwbOxIEQ7 h/GK/S1FKbLTv0UW1JOPyF3xFcoda+Y3ZArlgM+TDWucCSndbA/XhDbJGtaZ+7hMB43j bhrZb6GOiNMLYN72WKHfBrVLYO170KULb95o0pDHVfYq7lRsrf2qzNC3Q9I7hKXzuMoJ llNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hnOxxNia; 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 y10si1686719plk.155.2017.10.05.04.03.48; Thu, 05 Oct 2017 04:03:48 -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=hnOxxNia; 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 S1751908AbdJELDs (ORCPT + 3 others); Thu, 5 Oct 2017 07:03:48 -0400 Received: from mail-wm0-f54.google.com ([74.125.82.54]:49689 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751890AbdJELDr (ORCPT ); Thu, 5 Oct 2017 07:03:47 -0400 Received: by mail-wm0-f54.google.com with SMTP id b189so1358497wmd.4 for ; Thu, 05 Oct 2017 04:03:46 -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=gKrABHhe1QEt6ic49IW0E/MQWMxvtZhhcDR7JsU+QlI=; b=hnOxxNia/E/VeDkFOncQ8SpUjtmKhR5IW3YWMGfACMiFpf3N/Vb29Id8k5++2X1NDq tF8iwW+ojTb8uxYL9++TdnHaoXpODmKjqT/jlm/xNk55H3tdB/h2qFb3sOJ9Oro/Wbkx V/7t2uKNrwlRDgofSdn57KES9tYzcq8+iI4Sg= 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=gKrABHhe1QEt6ic49IW0E/MQWMxvtZhhcDR7JsU+QlI=; b=GAiTNipvKdxMiJT40anMo6d+m2IvKQqLB6mMwYH2r/Rry6Anl5ls0/+WyvTSn10QjC 2l85jixJSTaGvS7UVS2oLTeL35ZbpbX7dGKeQ6qVf6vnth38JprSmPs+3j70UV9FU2DI 4+OIbCLe9KuhlWsohS99QBXx8g0nBCyJIfnW3F0eg/gJaEq2QUfjmH90+24+r6CdCK5Z OKW/CAZoGDPFTDE3fAmmb31kAVCQWVMyXXeMgiEgkyl7B03HlzxRs+69JFs/eNSg+yzO QodPkUea3BNYGHnHVImQGAR0FR7QMFPrCPPybve01CE7g9czTaqQvWPy5VGhuItjJ2t/ vi5Q== X-Gm-Message-State: AHPjjUiknE6FAAvzSK8gwMYMKnBNdv6cKS2lZZ3YLaeFgaNwo6RQW1kL WwtgG3NHyKZ38o1R3UpJh6oPGeEBuCQ= X-Google-Smtp-Source: AOwi7QATjkwaYxbO2iJxPZcdHJ35iOEeBHgllpFspdHRrFdfKr5DnCXA8l7hAYfPQXXkDZmQcUVsTA== X-Received: by 10.28.134.18 with SMTP id i18mr20648107wmd.27.1507201426150; Thu, 05 Oct 2017 04:03:46 -0700 (PDT) Received: from genomnajs.local (catv-80-98-18-187.catv.broadband.hu. [80.98.18.187]) by smtp.gmail.com with ESMTPSA id e134sm17336478wma.31.2017.10.05.04.03.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Oct 2017 04:03:45 -0700 (PDT) From: Linus Walleij To: Wolfram Sang , linux-i2c@vger.kernel.org Cc: Linus Walleij Subject: [PATCH 7/7 v3] i2c: gpio: Add support for named gpios in DT Date: Thu, 5 Oct 2017 13:03:10 +0200 Message-Id: <20171005110310.5900-8-linus.walleij@linaro.org> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20171005110310.5900-1-linus.walleij@linaro.org> References: <20171005110310.5900-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 Tested-by: Geert Uytterhoeven Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Switched using %d to print error code rather than %ld 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 a702e493275c..d80ea6ce91bb 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: %d\n", ret); + + return retdesc; +} + static int i2c_gpio_probe(struct platform_device *pdev) { struct i2c_gpio_private_data *priv; @@ -124,14 +160,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 @@ -143,14 +175,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;