Lines Matching +full:scl +full:- +full:open +full:- +full:drain

1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <linux/i2c-algo-bit.h>
17 #include <linux/platform_data/i2c-gpio.h>
23 struct gpio_desc *scl; member
37 * valid for pins configured as open drain (i.e. setting the value
44 gpiod_set_value_cansleep(priv->sda, state); in i2c_gpio_setsda_val()
48 * Toggle SCL by changing the output value of the pin. This is used
49 * for pins that are configured as open drain and for output-only
57 gpiod_set_value_cansleep(priv->scl, state); in i2c_gpio_setscl_val()
64 return gpiod_get_value_cansleep(priv->sda); in i2c_gpio_getsda()
71 return gpiod_get_value_cansleep(priv->scl); in i2c_gpio_getscl()
77 #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
78 #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
79 #define getsda(bd) ((bd)->getsda((bd)->data))
80 #define getscl(bd) ((bd)->getscl((bd)->data))
87 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
88 *val = get##wire(&priv->bit_data); \
89 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
96 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
97 set##wire(&priv->bit_data, val); \
98 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
103 WIRE_ATTRIBUTE(scl);
109 struct i2c_algo_bit_data *bit_data = &priv->bit_data; in i2c_gpio_incomplete_transfer()
112 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_incomplete_transfer()
116 udelay(bit_data->udelay); in i2c_gpio_incomplete_transfer()
119 for (i = pattern_size - 1; i >= 0; i--) { in i2c_gpio_incomplete_transfer()
121 udelay(bit_data->udelay / 2); in i2c_gpio_incomplete_transfer()
123 udelay((bit_data->udelay + 1) / 2); in i2c_gpio_incomplete_transfer()
125 udelay(bit_data->udelay); in i2c_gpio_incomplete_transfer()
128 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_incomplete_transfer()
137 return -EINVAL; in fops_incomplete_addr_phase_set()
154 return -EINVAL; in fops_incomplete_write_byte_set()
170 int ret, irq = gpiod_to_irq(priv->scl); in i2c_gpio_fi_act_on_scl_irq()
175 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_fi_act_on_scl_irq()
177 ret = gpiod_direction_input(priv->scl); in i2c_gpio_fi_act_on_scl_irq()
181 reinit_completion(&priv->scl_irq_completion); in i2c_gpio_fi_act_on_scl_irq()
188 wait_for_completion_interruptible(&priv->scl_irq_completion); in i2c_gpio_fi_act_on_scl_irq()
192 ret = gpiod_direction_output(priv->scl, 1) ?: ret; in i2c_gpio_fi_act_on_scl_irq()
194 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); in i2c_gpio_fi_act_on_scl_irq()
203 setsda(&priv->bit_data, 0); in lose_arbitration_irq()
204 udelay(priv->scl_irq_data); in lose_arbitration_irq()
205 setsda(&priv->bit_data, 1); in lose_arbitration_irq()
207 complete(&priv->scl_irq_completion); in lose_arbitration_irq()
217 return -EINVAL; in fops_lose_arbitration_set()
219 priv->scl_irq_data = duration; in fops_lose_arbitration_set()
221 * Interrupt on falling SCL. This ensures that the master under test has in fops_lose_arbitration_set()
235 udelay(priv->scl_irq_data); in inject_panic_irq()
246 return -EINVAL; in fops_inject_panic_set()
248 priv->scl_irq_data = duration; in fops_inject_panic_set()
250 * Interrupt on falling SCL. This ensures that the master under test has in fops_inject_panic_set()
262 * If there will be a debugfs-dir per i2c adapter somewhen, put the in i2c_gpio_fault_injector_init()
263 * 'fault-injector' dir there. Until then, we have a global dir with in i2c_gpio_fault_injector_init()
267 i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL); in i2c_gpio_fault_injector_init()
272 priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir); in i2c_gpio_fault_injector_init()
273 if (!priv->debug_dir) in i2c_gpio_fault_injector_init()
276 init_completion(&priv->scl_irq_completion); in i2c_gpio_fault_injector_init()
278 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir, in i2c_gpio_fault_injector_init()
280 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir, in i2c_gpio_fault_injector_init()
282 if (priv->bit_data.getscl) { in i2c_gpio_fault_injector_init()
283 debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir, in i2c_gpio_fault_injector_init()
285 debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir, in i2c_gpio_fault_injector_init()
288 debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl); in i2c_gpio_fault_injector_init()
289 debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda); in i2c_gpio_fault_injector_init()
296 debugfs_remove_recursive(priv->debug_dir); in i2c_gpio_fault_injector_exit()
308 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); in of_i2c_gpio_get_props()
310 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg)) in of_i2c_gpio_get_props()
311 pdata->timeout = msecs_to_jiffies(reg); in of_i2c_gpio_get_props()
313 pdata->sda_is_open_drain = in of_i2c_gpio_get_props()
314 of_property_read_bool(np, "i2c-gpio,sda-open-drain"); in of_i2c_gpio_get_props()
315 pdata->scl_is_open_drain = in of_i2c_gpio_get_props()
316 of_property_read_bool(np, "i2c-gpio,scl-open-drain"); in of_i2c_gpio_get_props()
317 pdata->scl_is_output_only = in of_i2c_gpio_get_props()
318 of_property_read_bool(np, "i2c-gpio,scl-output-only"); in of_i2c_gpio_get_props()
344 if (ret == -EINVAL) in i2c_gpio_get_desc()
345 retdesc = ERR_PTR(-EPROBE_DEFER); in i2c_gpio_get_desc()
348 if (ret == -ENOENT) in i2c_gpio_get_desc()
349 retdesc = ERR_PTR(-EPROBE_DEFER); in i2c_gpio_get_desc()
351 if (PTR_ERR(retdesc) != -EPROBE_DEFER) in i2c_gpio_get_desc()
363 struct device *dev = &pdev->dev; in i2c_gpio_probe()
364 struct device_node *np = dev->of_node; in i2c_gpio_probe()
370 return -ENOMEM; in i2c_gpio_probe()
372 adap = &priv->adap; in i2c_gpio_probe()
373 bit_data = &priv->bit_data; in i2c_gpio_probe()
374 pdata = &priv->pdata; in i2c_gpio_probe()
389 * If the SCL/SDA lines are marked "open drain" by platform data or in i2c_gpio_probe()
391 * marking these lines to be handled as open drain, and we should just in i2c_gpio_probe()
392 * handle them as we handle any other output. Else we enforce open in i2c_gpio_probe()
393 * drain as this is required for an I2C bus. in i2c_gpio_probe()
395 if (pdata->sda_is_open_drain) in i2c_gpio_probe()
399 priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags); in i2c_gpio_probe()
400 if (IS_ERR(priv->sda)) in i2c_gpio_probe()
401 return PTR_ERR(priv->sda); in i2c_gpio_probe()
403 if (pdata->scl_is_open_drain) in i2c_gpio_probe()
407 priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags); in i2c_gpio_probe()
408 if (IS_ERR(priv->scl)) in i2c_gpio_probe()
409 return PTR_ERR(priv->scl); in i2c_gpio_probe()
411 if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl)) in i2c_gpio_probe()
414 bit_data->can_do_atomic = true; in i2c_gpio_probe()
416 bit_data->setsda = i2c_gpio_setsda_val; in i2c_gpio_probe()
417 bit_data->setscl = i2c_gpio_setscl_val; in i2c_gpio_probe()
419 if (!pdata->scl_is_output_only) in i2c_gpio_probe()
420 bit_data->getscl = i2c_gpio_getscl; in i2c_gpio_probe()
421 bit_data->getsda = i2c_gpio_getsda; in i2c_gpio_probe()
423 if (pdata->udelay) in i2c_gpio_probe()
424 bit_data->udelay = pdata->udelay; in i2c_gpio_probe()
425 else if (pdata->scl_is_output_only) in i2c_gpio_probe()
426 bit_data->udelay = 50; /* 10 kHz */ in i2c_gpio_probe()
428 bit_data->udelay = 5; /* 100 kHz */ in i2c_gpio_probe()
430 if (pdata->timeout) in i2c_gpio_probe()
431 bit_data->timeout = pdata->timeout; in i2c_gpio_probe()
433 bit_data->timeout = HZ / 10; /* 100 ms */ in i2c_gpio_probe()
435 bit_data->data = priv; in i2c_gpio_probe()
437 adap->owner = THIS_MODULE; in i2c_gpio_probe()
439 strscpy(adap->name, dev_name(dev), sizeof(adap->name)); in i2c_gpio_probe()
441 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); in i2c_gpio_probe()
443 adap->algo_data = bit_data; in i2c_gpio_probe()
444 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; in i2c_gpio_probe()
445 adap->dev.parent = dev; in i2c_gpio_probe()
446 adap->dev.of_node = np; in i2c_gpio_probe()
448 adap->nr = pdev->id; in i2c_gpio_probe()
460 dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n", in i2c_gpio_probe()
461 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), in i2c_gpio_probe()
462 pdata->scl_is_output_only in i2c_gpio_probe()
478 adap = &priv->adap; in i2c_gpio_remove()
487 { .compatible = "i2c-gpio", },
496 .name = "i2c-gpio",
509 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); in i2c_gpio_init()
522 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
524 MODULE_ALIAS("platform:i2c-gpio");