Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp909229ybn; Wed, 2 Oct 2019 08:04:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqyG5B75ceSqNjwhUqoYij9IJ6/FcMk7TTqwjryLQR9oIvpnmxUl8k7EZw0shp9R+TZBJzfl X-Received: by 2002:a50:8d5e:: with SMTP id t30mr4358866edt.112.1570028673405; Wed, 02 Oct 2019 08:04:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570028673; cv=none; d=google.com; s=arc-20160816; b=wGGM98WLykzQHkvAkTwUOj8RwHtkLZtEAbU5iHlHw7RXfCTjs9r7wG9oh5QrvAFcFe 7xYRHJFPo3+Y8hKpVJfgqqWGqVZlg0Ms/AqH9bEd2o+I6fb95urnI/ov4OMprSRxcZck u7I5U20oolJVDj5Cejxoc8ougPNfjEM7jpxHhbYyRNyGUw5LirWxjLYAZSey7MWIOTV2 1Rd/DTmfR47D8DRa7+t+yHKvpIh3kc/+ee+5ewPc8SWwBvacJ+M7cMJMCaHFMpK5DMjY k/REvc5yixJvdyyVe5jcRFujAQVmXKBrJAWpcaYQKstkzEbjcIYkIpraqjP7LSfbRb8d Qyqg== 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=s0C2LU5NGVnyp9oQ1Z69dXpddwrAjwzN8spCK1YgshE=; b=RRGd4kNRlpxmQizJioijV0l/bHkU+K6/6FvUACmFKrFApQ+1iK07BE8J2v8o+VKIEC dup23yFd1Q7C712PBpMA4GhKkParMVWTnVcruvC/2keO3ROsMGgOVDbUaqQDmIxw+k17 keH/1yo4RnF0w8tCysnRigSiFtpLmtO7jQHWM3T+CP3dv+3sluoIRGIeep/hqjQcoJqf uB3SE+88+b6Uqj7DTW2je8uyxbeD3aFftcG5CQNqfo0OCl/xKIFq7qMNWIoCJ28N5LvT 50Yt9go9EK++pDfDzS0bGwkeYknoWUuHeSBxarW0Pyvb5n7LL+A4bObM6HhKIFe+f40P ZoZA== 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 g8si11682947edb.335.2019.10.02.08.04.08; Wed, 02 Oct 2019 08:04:33 -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 S1728428AbfJBOrJ (ORCPT + 99 others); Wed, 2 Oct 2019 10:47:09 -0400 Received: from relay12.mail.gandi.net ([217.70.178.232]:54017 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727427AbfJBOrI (ORCPT ); Wed, 2 Oct 2019 10:47:08 -0400 Received: from localhost (aclermont-ferrand-651-1-259-53.w86-207.abo.wanadoo.fr [86.207.98.53]) (Authenticated sender: kamel.bouhara@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 01E98200005; Wed, 2 Oct 2019 14:47:04 +0000 (UTC) From: Kamel Bouhara To: Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolas Ferre , Alexandre Belloni , Ludovic Desroches , linux-arm-kernel@lists.infradead.org Cc: devicetree@vger.kernel.org, Thomas Petazzoni , Kamel Bouhara Subject: [PATCH 2/4] i2c: at91: implement i2c bus recovery Date: Wed, 2 Oct 2019 16:46:56 +0200 Message-Id: <20191002144658.7718-3-kamel.bouhara@bootlin.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191002144658.7718-1-kamel.bouhara@bootlin.com> References: <20191002144658.7718-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 --- drivers/i2c/busses/i2c-at91-master.c | 63 ++++++++++++++++++++++++++++ drivers/i2c/busses/i2c-at91.h | 8 ++++ 2 files changed, 71 insertions(+) diff --git a/drivers/i2c/busses/i2c-at91-master.c b/drivers/i2c/busses/i2c-at91-master.c index a3fcc35ffd3b..df5bb93f952d 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 @@ -768,6 +770,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) { @@ -795,6 +854,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 499b506f6128..b89dab55e776 100644 --- a/drivers/i2c/busses/i2c-at91.h +++ b/drivers/i2c/busses/i2c-at91.h @@ -141,6 +141,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; @@ -158,6 +162,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