Lines Matching +full:i2c +full:- +full:gpio

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bitbanging I2C bus driver using the GPIO API
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c-algo-bit.h>
12 #include <linux/i2c.h>
16 #include <linux/platform_data/i2c-gpio.h>
43 gpiod_set_value_cansleep(priv->sda, state); in i2c_gpio_setsda_val()
48 * for pins that are configured as open drain and for output-only
49 * pins. The latter case will break the i2c protocol, but it will
56 gpiod_set_value_cansleep(priv->scl, state); in i2c_gpio_setscl_val()
63 return gpiod_get_value_cansleep(priv->sda); in i2c_gpio_getsda()
70 return gpiod_get_value_cansleep(priv->scl); in i2c_gpio_getscl()
75 #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
76 #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
77 #define getsda(bd) ((bd)->getsda((bd)->data))
78 #define getscl(bd) ((bd)->getscl((bd)->data))
85 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
86 *val = get##wire(&priv->bit_data); \
87 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
94 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
95 set##wire(&priv->bit_data, val); \
96 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
107 struct i2c_algo_bit_data *bit_data = &priv->bit_data; in i2c_gpio_incomplete_transfer()
110 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_incomplete_transfer()
114 udelay(bit_data->udelay); in i2c_gpio_incomplete_transfer()
117 for (i = pattern_size - 1; i >= 0; i--) { in i2c_gpio_incomplete_transfer()
119 udelay(bit_data->udelay / 2); in i2c_gpio_incomplete_transfer()
121 udelay((bit_data->udelay + 1) / 2); in i2c_gpio_incomplete_transfer()
123 udelay(bit_data->udelay); in i2c_gpio_incomplete_transfer()
126 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_incomplete_transfer()
135 return -EINVAL; in fops_incomplete_addr_phase_set()
152 return -EINVAL; in fops_incomplete_write_byte_set()
168 int ret, irq = gpiod_to_irq(priv->scl); in i2c_gpio_fi_act_on_scl_irq()
173 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_fi_act_on_scl_irq()
175 ret = gpiod_direction_input(priv->scl); in i2c_gpio_fi_act_on_scl_irq()
179 reinit_completion(&priv->scl_irq_completion); in i2c_gpio_fi_act_on_scl_irq()
186 wait_for_completion_interruptible(&priv->scl_irq_completion); in i2c_gpio_fi_act_on_scl_irq()
190 ret = gpiod_direction_output(priv->scl, 1) ?: ret; in i2c_gpio_fi_act_on_scl_irq()
192 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_fi_act_on_scl_irq()
201 setsda(&priv->bit_data, 0); in lose_arbitration_irq()
202 udelay(priv->scl_irq_data); in lose_arbitration_irq()
203 setsda(&priv->bit_data, 1); in lose_arbitration_irq()
205 complete(&priv->scl_irq_completion); in lose_arbitration_irq()
215 return -EINVAL; in fops_lose_arbitration_set()
217 priv->scl_irq_data = duration; in fops_lose_arbitration_set()
233 udelay(priv->scl_irq_data); in inject_panic_irq()
234 panic("I2C fault injector induced panic"); in inject_panic_irq()
244 return -EINVAL; in fops_inject_panic_set()
246 priv->scl_irq_data = duration; in fops_inject_panic_set()
259 init_completion(&priv->scl_irq_completion); in i2c_gpio_fault_injector_init()
261 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->adap.debugfs, in i2c_gpio_fault_injector_init()
263 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->adap.debugfs, in i2c_gpio_fault_injector_init()
265 if (priv->bit_data.getscl) { in i2c_gpio_fault_injector_init()
266 debugfs_create_file_unsafe("inject_panic", 0200, priv->adap.debugfs, in i2c_gpio_fault_injector_init()
268 debugfs_create_file_unsafe("lose_arbitration", 0200, priv->adap.debugfs, in i2c_gpio_fault_injector_init()
271 debugfs_create_file_unsafe("scl", 0600, priv->adap.debugfs, priv, &fops_scl); in i2c_gpio_fault_injector_init()
272 debugfs_create_file_unsafe("sda", 0600, priv->adap.debugfs, priv, &fops_sda); in i2c_gpio_fault_injector_init()
278 /* Get i2c-gpio properties from DT or ACPI table */
284 device_property_read_u32(dev, "i2c-gpio,delay-us", &pdata->udelay); in i2c_gpio_get_properties()
286 if (!device_property_read_u32(dev, "i2c-gpio,timeout-ms", &reg)) in i2c_gpio_get_properties()
287 pdata->timeout = msecs_to_jiffies(reg); in i2c_gpio_get_properties()
289 pdata->sda_is_open_drain = in i2c_gpio_get_properties()
290 device_property_read_bool(dev, "i2c-gpio,sda-open-drain"); in i2c_gpio_get_properties()
291 pdata->scl_is_open_drain = in i2c_gpio_get_properties()
292 device_property_read_bool(dev, "i2c-gpio,scl-open-drain"); in i2c_gpio_get_properties()
293 pdata->scl_is_output_only = in i2c_gpio_get_properties()
294 device_property_read_bool(dev, "i2c-gpio,scl-output-only"); in i2c_gpio_get_properties()
295 pdata->sda_is_output_only = in i2c_gpio_get_properties()
296 device_property_read_bool(dev, "i2c-gpio,sda-output-only"); in i2c_gpio_get_properties()
297 pdata->sda_has_no_pullup = in i2c_gpio_get_properties()
298 device_property_read_bool(dev, "i2c-gpio,sda-has-no-pullup"); in i2c_gpio_get_properties()
299 pdata->scl_has_no_pullup = in i2c_gpio_get_properties()
300 device_property_read_bool(dev, "i2c-gpio,scl-has-no-pullup"); in i2c_gpio_get_properties()
313 dev_dbg(dev, "got GPIO from name %s\n", con_id); in i2c_gpio_get_desc()
319 dev_dbg(dev, "got GPIO from index %u\n", index); in i2c_gpio_get_desc()
326 if (ret == -EINVAL) in i2c_gpio_get_desc()
327 retdesc = ERR_PTR(-EPROBE_DEFER); in i2c_gpio_get_desc()
329 /* This happens if the GPIO driver is not yet probed, let's defer */ in i2c_gpio_get_desc()
330 if (ret == -ENOENT) in i2c_gpio_get_desc()
331 retdesc = ERR_PTR(-EPROBE_DEFER); in i2c_gpio_get_desc()
333 if (PTR_ERR(retdesc) != -EPROBE_DEFER) in i2c_gpio_get_desc()
345 struct device *dev = &pdev->dev; in i2c_gpio_probe()
352 return -ENOMEM; in i2c_gpio_probe()
354 adap = &priv->adap; in i2c_gpio_probe()
355 bit_data = &priv->bit_data; in i2c_gpio_probe()
356 pdata = &priv->pdata; in i2c_gpio_probe()
370 * First get the GPIO pins; if it fails, we'll defer the probe. in i2c_gpio_probe()
375 * drain as this is required for an I2C bus. in i2c_gpio_probe()
377 if (pdata->sda_is_open_drain || pdata->sda_has_no_pullup) in i2c_gpio_probe()
381 priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags); in i2c_gpio_probe()
382 if (IS_ERR(priv->sda)) in i2c_gpio_probe()
383 return PTR_ERR(priv->sda); in i2c_gpio_probe()
385 if (pdata->scl_is_open_drain || pdata->scl_has_no_pullup) in i2c_gpio_probe()
389 priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags); in i2c_gpio_probe()
390 if (IS_ERR(priv->scl)) in i2c_gpio_probe()
391 return PTR_ERR(priv->scl); in i2c_gpio_probe()
393 if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl)) in i2c_gpio_probe()
394 dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing"); in i2c_gpio_probe()
396 bit_data->can_do_atomic = true; in i2c_gpio_probe()
398 bit_data->setsda = i2c_gpio_setsda_val; in i2c_gpio_probe()
399 bit_data->setscl = i2c_gpio_setscl_val; in i2c_gpio_probe()
401 if (!pdata->scl_is_output_only) in i2c_gpio_probe()
402 bit_data->getscl = i2c_gpio_getscl; in i2c_gpio_probe()
403 if (!pdata->sda_is_output_only) in i2c_gpio_probe()
404 bit_data->getsda = i2c_gpio_getsda; in i2c_gpio_probe()
406 if (pdata->udelay) in i2c_gpio_probe()
407 bit_data->udelay = pdata->udelay; in i2c_gpio_probe()
408 else if (pdata->scl_is_output_only) in i2c_gpio_probe()
409 bit_data->udelay = 50; /* 10 kHz */ in i2c_gpio_probe()
411 bit_data->udelay = 5; /* 100 kHz */ in i2c_gpio_probe()
413 if (pdata->timeout) in i2c_gpio_probe()
414 bit_data->timeout = pdata->timeout; in i2c_gpio_probe()
416 bit_data->timeout = HZ / 10; /* 100 ms */ in i2c_gpio_probe()
418 bit_data->data = priv; in i2c_gpio_probe()
420 adap->owner = THIS_MODULE; in i2c_gpio_probe()
422 strscpy(adap->name, dev_name(dev), sizeof(adap->name)); in i2c_gpio_probe()
424 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); in i2c_gpio_probe()
426 adap->algo_data = bit_data; in i2c_gpio_probe()
427 adap->class = I2C_CLASS_HWMON; in i2c_gpio_probe()
428 adap->dev.parent = dev; in i2c_gpio_probe()
429 device_set_node(&adap->dev, fwnode); in i2c_gpio_probe()
431 adap->nr = pdev->id; in i2c_gpio_probe()
439 * FIXME: using global GPIO numbers is not helpful. If/when we in i2c_gpio_probe()
440 * get accessors to get the actual name of the GPIO line, in i2c_gpio_probe()
444 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), in i2c_gpio_probe()
445 pdata->scl_is_output_only in i2c_gpio_probe()
459 adap = &priv->adap; in i2c_gpio_remove()
465 { .compatible = "i2c-gpio", },
479 .name = "i2c-gpio",
493 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); in i2c_gpio_init()
506 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
508 MODULE_ALIAS("platform:i2c-gpio");