diff options
author | Jan Luebbe <jluebbe@debian.org> | 2015-07-30 16:52:06 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2015-08-20 07:47:14 +0200 |
commit | e1944fb6b2c5bbdc6d8927e19cdf68aab6e92760 (patch) | |
tree | 27be9cb3fc2f4563f1cf2da1e86d3d08f383c145 /drivers/i2c | |
parent | e87360ad056bce90318aaa2da1627969922d9053 (diff) | |
download | barebox-e1944fb6b2c5bbdc6d8927e19cdf68aab6e92760.tar.gz barebox-e1944fb6b2c5bbdc6d8927e19cdf68aab6e92760.tar.xz |
i2c: add bus recovery infrastructure
This is based on the code introduced to the kernel in
5f9296ba21b3c395e53dd84e7ff9578f97f24295.
Signed-off-by: Jan Luebbe <jluebbe@debian.org>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/i2c.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c index f0df666b97..52aaea8170 100644 --- a/drivers/i2c/i2c.c +++ b/drivers/i2c/i2c.c @@ -23,6 +23,7 @@ #include <xfuncs.h> #include <init.h> #include <of.h> +#include <gpio.h> #include <i2c/i2c.h> @@ -228,6 +229,135 @@ int i2c_write_reg(struct i2c_client *client, u32 addr, const u8 *buf, u16 count) } EXPORT_SYMBOL(i2c_write_reg); +/* i2c bus recovery routines */ +int i2c_get_scl_gpio_value(struct i2c_adapter *adap) +{ + gpio_direction_input(adap->bus_recovery_info->scl_gpio); + return gpio_get_value(adap->bus_recovery_info->scl_gpio); +} + +void i2c_set_scl_gpio_value(struct i2c_adapter *adap, int val) +{ + if (val) + gpio_direction_input(adap->bus_recovery_info->scl_gpio); + else + gpio_direction_output(adap->bus_recovery_info->scl_gpio, 0); +} + +int i2c_get_sda_gpio_value(struct i2c_adapter *adap) +{ + return gpio_get_value(adap->bus_recovery_info->sda_gpio); +} + +static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) +{ + struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; + struct device_d *dev = &adap->dev; + int ret = 0; + + ret = gpio_request_one(bri->scl_gpio, GPIOF_IN, "i2c-scl"); + if (ret) { + dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); + return ret; + } + + if (bri->get_sda) { + if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { + /* work without SDA polling */ + dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", + bri->sda_gpio); + bri->get_sda = NULL; + } + } + + return ret; +} + +static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) +{ + struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; + + if (bri->get_sda) + gpio_free(bri->sda_gpio); + + gpio_free(bri->scl_gpio); +} + +/* + * We are generating clock pulses. ndelay() determines durating of clk pulses. + * We will generate clock with rate 100 KHz and so duration of both clock levels + * is: delay in ns = (10^6 / 100) / 2 + */ +#define RECOVERY_NDELAY 5000 +#define RECOVERY_CLK_CNT 9 + +static int i2c_generic_recovery(struct i2c_adapter *adap) +{ + struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; + int i = 0, val = 1, ret = 0; + + if (bri->prepare_recovery) + bri->prepare_recovery(adap); + + bri->set_scl(adap, val); + ndelay(RECOVERY_NDELAY); + + /* + * By this time SCL is high, as we need to give 9 falling-rising edges + */ + while (i++ < RECOVERY_CLK_CNT * 2) { + if (val) { + /* Break if SDA is high */ + if (bri->get_sda && bri->get_sda(adap)) + break; + /* SCL shouldn't be low here */ + if (!bri->get_scl(adap)) { + dev_err(&adap->dev, + "SCL is stuck low, exit recovery\n"); + ret = -EBUSY; + break; + } + } + + val = !val; + bri->set_scl(adap, val); + ndelay(RECOVERY_NDELAY); + } + + if (bri->unprepare_recovery) + bri->unprepare_recovery(adap); + + return ret; +} + +int i2c_generic_scl_recovery(struct i2c_adapter *adap) +{ + return i2c_generic_recovery(adap); +} + +int i2c_generic_gpio_recovery(struct i2c_adapter *adap) +{ + int ret; + + ret = i2c_get_gpios_for_recovery(adap); + if (ret) + return ret; + + ret = i2c_generic_recovery(adap); + i2c_put_gpios_for_recovery(adap); + + return ret; +} + +int i2c_recover_bus(struct i2c_adapter *adap) +{ + if (!adap->bus_recovery_info) + return -EOPNOTSUPP; + + dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); + return adap->bus_recovery_info->recover_bus(adap); +} + /** * i2c_new_device - instantiate one new I2C device * |