Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4012609ybg; Fri, 25 Oct 2019 12:05:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqytjr7i+S9Y266/PzWQ7M0bJ53DrRwY7Dqy1odmiW0cYiwL3KS9htEJIfoFxeuao7/IZ4RO X-Received: by 2002:a50:9eac:: with SMTP id a41mr5706766edf.237.1572030318300; Fri, 25 Oct 2019 12:05:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572030318; cv=none; d=google.com; s=arc-20160816; b=W3BefyVXchF0EnuAq3WTJv2O8jjFyrqXw4QO0bRXyhAjUe+H0/erLuYw8IjKXVh8Wx 82jI2ylnTYoHe11lwKVmgmCObe4kknqZB19N46Co7mk7LT0KrkKixCJ2Yooo3qofYDvQ wokMTVlKB7/BJpRlzED3GMS9dS/uu9rQLdv9Swe+CcBhw1dBZFg/JTgaf5MPWfXJhLr8 9JMXrU2N3h41gBA1hmrBMEvBdbYVicxGAwhG7rdYK6ouOl6944pkcjBl7uUvrsyVTEus 2puY92exgw5RBxEDVHS2FI6yOZY5H9WBQNyp1B2hN7SeBjaRVlJM7/Jil/r8zrSer5ZJ RaMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BILzPxBBmGl3R3gyOXqk3h2ESjqIqv4C24DGBvUS1Lo=; b=Kk3QO1qWMMfAi5DfXS0xJPylvilu/5umOQEq5EMGLVenBTNmvHUy0cKObeUDJs1Siq zt/BTXc37tP+KPlS4pIRvlVZXA8HaVHoqTbQFBA/hJZNVzbiNoR9F3CCRMg/d2w4fk0I fzq+UZAp7DbBi7cOXr4pvc/w1wuMXgAcP7lYwv1kSF7BsDr2j1PmDdFUoBoPRxLIqSBG jwD+vNDCBS2vqPzFbMCnmAovAuC0kxE9VXZ0ktNzOiWciz5FaY9PBBD0K6VpkFYW3dqm 5qGyb47twWtBqb5IyNxgT9Fl2at2dl+rR2f2xRi2jJ+6eZvOgAPC+ahDbXO4jA/JB9f2 KLcw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n4si1764753ejs.402.2019.10.25.12.04.54; Fri, 25 Oct 2019 12:05:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726962AbfJXUNt (ORCPT + 99 others); Thu, 24 Oct 2019 16:13:49 -0400 Received: from relay10.mail.gandi.net ([217.70.178.230]:38743 "EHLO relay10.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726536AbfJXUNc (ORCPT ); Thu, 24 Oct 2019 16:13:32 -0400 Received: from localhost (unknown [78.193.40.249]) (Authenticated sender: kamel.bouhara@bootlin.com) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 41AD6240005; Thu, 24 Oct 2019 20:13:30 +0000 (UTC) From: Kamel Bouhara To: Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Nicolas Ferre , Alexandre Belloni , Ludovic Desroches , linux-arm-kernel@lists.infradead.org, Thomas Petazzoni , Rob Herring , devicetree@vger.kernel.org, Kamel Bouhara Subject: [PATCH v2 3/5] i2c: at91: implement i2c bus recovery Date: Thu, 24 Oct 2019 22:13:00 +0200 Message-Id: <20191024201302.23376-4-kamel.bouhara@bootlin.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191024201302.23376-1-kamel.bouhara@bootlin.com> References: <20191024201302.23376-1-kamel.bouhara@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement i2c bus recovery when slaves devices might hold SDA low. In this case re-assign SCL/SDA to gpios and issue 9 dummy clock pulses until the slave release SDA. Signed-off-by: Kamel Bouhara --- Changes in v2: ============== - Add missing call to i2c_bus_recover() when a i2c transfer timeout. drivers/i2c/busses/i2c-at91-master.c | 64 ++++++++++++++++++++++++++++ drivers/i2c/busses/i2c-at91.h | 8 ++++ 2 files changed, 72 insertions(+) diff --git a/drivers/i2c/busses/i2c-at91-master.c b/drivers/i2c/busses/i2c-at91-master.c index 12d4fa946a82..8116f3cd373b 100644 --- a/drivers/i2c/busses/i2c-at91-master.c +++ b/drivers/i2c/busses/i2c-at91-master.c @@ -18,11 +18,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -555,6 +557,7 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev) if (time_left == 0) { dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR); dev_err(dev->dev, "controller timed out\n"); + i2c_recover_bus(&dev->adapter); at91_init_twi_bus(dev); ret = -ETIMEDOUT; goto error; @@ -790,6 +793,63 @@ static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr) return ret; } +static void at91_prepare_twi_recovery(struct i2c_adapter *adap) +{ + struct at91_twi_dev *dev = i2c_get_adapdata(adap); + + pinctrl_select_state(dev->pinctrl, dev->pinctrl_pins_gpio); +} + +static void at91_unprepare_twi_recovery(struct i2c_adapter *adap) +{ + struct at91_twi_dev *dev = i2c_get_adapdata(adap); + + pinctrl_select_state(dev->pinctrl, dev->pinctrl_pins_default); +} + +static int at91_init_twi_recovery_info(struct platform_device *pdev, + struct at91_twi_dev *dev) +{ + struct i2c_bus_recovery_info *rinfo = &dev->rinfo; + + dev->pinctrl = devm_pinctrl_get(&pdev->dev); + if (!dev->pinctrl || IS_ERR(dev->pinctrl)) { + dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n"); + return PTR_ERR(dev->pinctrl); + } + + dev->pinctrl_pins_default = pinctrl_lookup_state(dev->pinctrl, + PINCTRL_STATE_DEFAULT); + dev->pinctrl_pins_gpio = pinctrl_lookup_state(dev->pinctrl, + "gpio"); + rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN); + if (PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER) + return -EPROBE_DEFER; + + rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", + GPIOD_OUT_HIGH_OPEN_DRAIN); + if (PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER) + return -EPROBE_DEFER; + + if (IS_ERR(rinfo->sda_gpiod) || + IS_ERR(rinfo->scl_gpiod) || + IS_ERR(dev->pinctrl_pins_default) || + IS_ERR(dev->pinctrl_pins_gpio)) { + dev_info(&pdev->dev, "recovery information incomplete\n"); + return -EINVAL; + } + + dev_info(&pdev->dev, "using scl%s for recovery\n", + rinfo->sda_gpiod ? ",sda" : ""); + + rinfo->prepare_recovery = at91_prepare_twi_recovery; + rinfo->unprepare_recovery = at91_unprepare_twi_recovery; + rinfo->recover_bus = i2c_generic_scl_recovery; + dev->adapter.bus_recovery_info = rinfo; + + return 0; +} + int at91_twi_probe_master(struct platform_device *pdev, u32 phy_addr, struct at91_twi_dev *dev) { @@ -817,6 +877,10 @@ int at91_twi_probe_master(struct platform_device *pdev, at91_calc_twi_clock(dev); + rc = at91_init_twi_recovery_info(pdev, dev); + if (rc == -EPROBE_DEFER) + return rc; + dev->adapter.algo = &at91_twi_algorithm; dev->adapter.quirks = &at91_twi_quirks; diff --git a/drivers/i2c/busses/i2c-at91.h b/drivers/i2c/busses/i2c-at91.h index 0827c28a84db..167be7aa8e67 100644 --- a/drivers/i2c/busses/i2c-at91.h +++ b/drivers/i2c/busses/i2c-at91.h @@ -146,6 +146,10 @@ struct at91_twi_dev { u32 fifo_size; struct at91_twi_dma dma; bool slave_detected; + struct i2c_bus_recovery_info rinfo; + struct pinctrl *pinctrl; + struct pinctrl_state *pinctrl_pins_default; + struct pinctrl_state *pinctrl_pins_gpio; #ifdef CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL unsigned smr; struct i2c_client *slave; @@ -163,6 +167,10 @@ void at91_init_twi_bus_master(struct at91_twi_dev *dev); int at91_twi_probe_master(struct platform_device *pdev, u32 phy_addr, struct at91_twi_dev *dev); +void at91_twi_prepare_recovery(struct i2c_adapter *adap); +void at91_twi_unprepare_recovery(struct i2c_adapter *adap); +void at91_twi_init_recovery_info(struct at91_twi_dev *dev); + #ifdef CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL void at91_init_twi_bus_slave(struct at91_twi_dev *dev); int at91_twi_probe_slave(struct platform_device *pdev, u32 phy_addr, -- 2.23.0