|
@@ -27,7 +27,9 @@
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
#include <linux/kernel.h>
|
|
|
+#include <linux/delay.h>
|
|
|
#include <linux/errno.h>
|
|
|
+#include <linux/gpio.h>
|
|
|
#include <linux/slab.h>
|
|
|
#include <linux/i2c.h>
|
|
|
#include <linux/init.h>
|
|
@@ -109,6 +111,130 @@ static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
|
|
|
#define i2c_device_uevent NULL
|
|
|
#endif /* CONFIG_HOTPLUG */
|
|
|
|
|
|
+/* i2c bus recovery routines */
|
|
|
+static int get_scl_gpio_value(struct i2c_adapter *adap)
|
|
|
+{
|
|
|
+ return gpio_get_value(adap->bus_recovery_info->scl_gpio);
|
|
|
+}
|
|
|
+
|
|
|
+static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
|
|
|
+{
|
|
|
+ gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
|
|
|
+}
|
|
|
+
|
|
|
+static int 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 *dev = &adap->dev;
|
|
|
+ int ret = 0;
|
|
|
+
|
|
|
+ ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
|
|
|
+ GPIOF_OUT_INIT_HIGH, "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(bri);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 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(bri);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+int i2c_generic_scl_recovery(struct i2c_adapter *adap)
|
|
|
+{
|
|
|
+ adap->bus_recovery_info->set_scl(adap, 1);
|
|
|
+ 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);
|
|
|
+}
|
|
|
+
|
|
|
static int i2c_device_probe(struct device *dev)
|
|
|
{
|
|
|
struct i2c_client *client = i2c_verify_client(dev);
|
|
@@ -902,6 +1028,39 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
|
|
|
"Failed to create compatibility class link\n");
|
|
|
#endif
|
|
|
|
|
|
+ /* bus recovery specific initialization */
|
|
|
+ if (adap->bus_recovery_info) {
|
|
|
+ struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
|
|
|
+
|
|
|
+ if (!bri->recover_bus) {
|
|
|
+ dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
|
|
|
+ adap->bus_recovery_info = NULL;
|
|
|
+ goto exit_recovery;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Generic GPIO recovery */
|
|
|
+ if (bri->recover_bus == i2c_generic_gpio_recovery) {
|
|
|
+ if (!gpio_is_valid(bri->scl_gpio)) {
|
|
|
+ dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
|
|
|
+ adap->bus_recovery_info = NULL;
|
|
|
+ goto exit_recovery;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (gpio_is_valid(bri->sda_gpio))
|
|
|
+ bri->get_sda = get_sda_gpio_value;
|
|
|
+ else
|
|
|
+ bri->get_sda = NULL;
|
|
|
+
|
|
|
+ bri->get_scl = get_scl_gpio_value;
|
|
|
+ bri->set_scl = set_scl_gpio_value;
|
|
|
+ } else if (!bri->set_scl || !bri->get_scl) {
|
|
|
+ /* Generic SCL recovery */
|
|
|
+ dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
|
|
|
+ adap->bus_recovery_info = NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+exit_recovery:
|
|
|
/* create pre-declared device nodes */
|
|
|
if (adap->nr < __i2c_first_dynamic_bus_num)
|
|
|
i2c_scan_static_board_info(adap);
|