linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mark Tomlinson <mark.tomlinson@alliedtelesis.co.nz>
To: gregory.clement@bootlin.com
Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
	Mark Tomlinson <mark.tomlinson@alliedtelesis.co.nz>
Subject: [PATCH] i2c: mv64xxx: Add bus error recovery
Date: Wed,  8 Jul 2020 09:10:36 +1200	[thread overview]
Message-ID: <20200707211036.12896-1-mark.tomlinson@alliedtelesis.co.nz> (raw)

This adds i2c bus recovery to the mv64xxx driver.

Implement bus recovery to recover from SCL/SDA stuck low.

This uses the generic recovery function, setting the clock/data lines as
GPIO pins, and sending 9 clocks to try and recover the bus.

Signed-off-by: Mark Tomlinson <mark.tomlinson@alliedtelesis.co.nz>
---
 drivers/i2c/busses/i2c-mv64xxx.c | 77 +++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index 829b8c98ae51..e58853ba3ef0 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -21,6 +21,7 @@
 #include <linux/io.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_gpio.h>
 #include <linux/of_irq.h>
 #include <linux/clk.h>
 #include <linux/err.h>
@@ -147,6 +148,10 @@ struct mv64xxx_i2c_data {
 	bool			irq_clear_inverted;
 	/* Clk div is 2 to the power n, not 2 to the power n + 1 */
 	bool			clk_n_base_0;
+	struct pinctrl		*pinctrl;
+	struct i2c_bus_recovery_info	rinfo;
+	struct pinctrl_state	*pin_default_state;
+	struct pinctrl_state	*pin_gpio_state;
 };
 
 static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
@@ -325,7 +330,8 @@ mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
 			 drv_data->msg->flags);
 		drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
 		mv64xxx_i2c_hw_init(drv_data);
-		drv_data->rc = -EIO;
+		i2c_recover_bus(&drv_data->adapter);
+		drv_data->rc = -EAGAIN;
 	}
 }
 
@@ -563,6 +569,7 @@ mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
 				"time_left: %d\n", drv_data->block,
 				(int)time_left);
 			mv64xxx_i2c_hw_init(drv_data);
+			i2c_recover_bus(&drv_data->adapter);
 		}
 	} else
 		spin_unlock_irqrestore(&drv_data->lock, flags);
@@ -872,6 +879,69 @@ mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
 }
 #endif /* CONFIG_OF */
 
+/*
+ * Switch to bit bang mode to prepare for i2c generic recovery.
+ */
+static void mv64xxx_i2c_prepare_recovery(struct i2c_adapter *adap)
+{
+	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
+
+	pinctrl_select_state(drv_data->pinctrl, drv_data->pin_gpio_state);
+}
+
+/*
+ * Return to normal i2c operation following recovery.
+ */
+static void mv64xxx_i2c_unprepare_recovery(struct i2c_adapter *adap)
+{
+	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
+
+	pinctrl_select_state(drv_data->pinctrl, drv_data->pin_default_state);
+}
+
+static int mv64xxx_i2c_init_recovery_info(struct mv64xxx_i2c_data *drv_data,
+					  struct platform_device *pd)
+{
+	struct i2c_bus_recovery_info *rinfo = &drv_data->rinfo;
+	struct device *dev = &pd->dev;
+
+	drv_data->pinctrl = devm_pinctrl_get(dev);
+	if (!drv_data->pinctrl || IS_ERR(drv_data->pinctrl)) {
+		dev_err(dev, "can't get pinctrl, bus recovery not supported\n");
+		return PTR_ERR(drv_data->pinctrl);
+	}
+
+	drv_data->pin_default_state = pinctrl_lookup_state(drv_data->pinctrl,
+			PINCTRL_STATE_DEFAULT);
+	drv_data->pin_gpio_state = pinctrl_lookup_state(drv_data->pinctrl,
+			"gpio");
+	rinfo->scl_gpiod = devm_gpiod_get(dev, "scl",
+					  GPIOD_OUT_HIGH_OPEN_DRAIN);
+	rinfo->sda_gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);
+	if (PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER ||
+	    PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
+
+	if (IS_ERR(rinfo->sda_gpiod) ||
+	    IS_ERR(rinfo->scl_gpiod) ||
+	    IS_ERR(drv_data->pin_default_state) ||
+	    IS_ERR(drv_data->pin_gpio_state)) {
+		dev_dbg(dev, "recovery information incomplete\n");
+		return 0;
+	}
+
+	dev_dbg(dev, "using scl-gpio %d and sda-gpio %d for recovery\n",
+		rinfo->scl_gpiod ? desc_to_gpio(rinfo->scl_gpiod) : -1,
+		rinfo->sda_gpiod ? desc_to_gpio(rinfo->sda_gpiod) : -1);
+
+	rinfo->prepare_recovery = mv64xxx_i2c_prepare_recovery;
+	rinfo->unprepare_recovery = mv64xxx_i2c_unprepare_recovery;
+	rinfo->recover_bus = i2c_generic_scl_recovery;
+	drv_data->adapter.bus_recovery_info = rinfo;
+
+	return 0;
+}
+
 static int
 mv64xxx_i2c_probe(struct platform_device *pd)
 {
@@ -939,6 +1009,10 @@ mv64xxx_i2c_probe(struct platform_device *pd)
 
 	mv64xxx_i2c_hw_init(drv_data);
 
+	rc = mv64xxx_i2c_init_recovery_info(drv_data, pd);
+	if (rc == -EPROBE_DEFER)
+		goto exit_reset;
+
 	rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
 			 MV64XXX_I2C_CTLR_NAME, drv_data);
 	if (rc) {
@@ -951,6 +1025,7 @@ mv64xxx_i2c_probe(struct platform_device *pd)
 			"mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
 		goto exit_free_irq;
 	}
+	i2c_recover_bus(&drv_data->adapter);
 
 	return 0;
 
-- 
2.27.0


             reply	other threads:[~2020-07-07 21:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-07 21:10 Mark Tomlinson [this message]
2020-07-29  3:11 ` [PATCH] i2c: mv64xxx: Add bus error recovery Chris Packham
2020-08-12 20:13 ` Wolfram Sang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200707211036.12896-1-mark.tomlinson@alliedtelesis.co.nz \
    --to=mark.tomlinson@alliedtelesis.co.nz \
    --cc=gregory.clement@bootlin.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).