| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Bitbanging I2C bus driver using the GPIO API |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2007 Atmel Corporation |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | */ |
|---|
| 7 | +#include <linux/completion.h> |
|---|
| 10 | 8 | #include <linux/debugfs.h> |
|---|
| 11 | 9 | #include <linux/delay.h> |
|---|
| 12 | | -#include <linux/i2c.h> |
|---|
| 13 | | -#include <linux/i2c-algo-bit.h> |
|---|
| 14 | | -#include <linux/platform_data/i2c-gpio.h> |
|---|
| 15 | | -#include <linux/init.h> |
|---|
| 16 | | -#include <linux/module.h> |
|---|
| 17 | | -#include <linux/slab.h> |
|---|
| 18 | | -#include <linux/platform_device.h> |
|---|
| 19 | 10 | #include <linux/gpio/consumer.h> |
|---|
| 11 | +#include <linux/i2c-algo-bit.h> |
|---|
| 12 | +#include <linux/i2c.h> |
|---|
| 13 | +#include <linux/init.h> |
|---|
| 14 | +#include <linux/interrupt.h> |
|---|
| 15 | +#include <linux/module.h> |
|---|
| 20 | 16 | #include <linux/of.h> |
|---|
| 17 | +#include <linux/platform_data/i2c-gpio.h> |
|---|
| 18 | +#include <linux/platform_device.h> |
|---|
| 19 | +#include <linux/slab.h> |
|---|
| 21 | 20 | |
|---|
| 22 | 21 | struct i2c_gpio_private_data { |
|---|
| 23 | 22 | struct gpio_desc *sda; |
|---|
| .. | .. |
|---|
| 27 | 26 | struct i2c_gpio_platform_data pdata; |
|---|
| 28 | 27 | #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR |
|---|
| 29 | 28 | struct dentry *debug_dir; |
|---|
| 29 | + /* these must be protected by bus lock */ |
|---|
| 30 | + struct completion scl_irq_completion; |
|---|
| 31 | + u64 scl_irq_data; |
|---|
| 30 | 32 | #endif |
|---|
| 31 | 33 | }; |
|---|
| 32 | 34 | |
|---|
| .. | .. |
|---|
| 162 | 164 | } |
|---|
| 163 | 165 | DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n"); |
|---|
| 164 | 166 | |
|---|
| 167 | +static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv, |
|---|
| 168 | + irqreturn_t handler(int, void*)) |
|---|
| 169 | +{ |
|---|
| 170 | + int ret, irq = gpiod_to_irq(priv->scl); |
|---|
| 171 | + |
|---|
| 172 | + if (irq < 0) |
|---|
| 173 | + return irq; |
|---|
| 174 | + |
|---|
| 175 | + i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); |
|---|
| 176 | + |
|---|
| 177 | + ret = gpiod_direction_input(priv->scl); |
|---|
| 178 | + if (ret) |
|---|
| 179 | + goto unlock; |
|---|
| 180 | + |
|---|
| 181 | + reinit_completion(&priv->scl_irq_completion); |
|---|
| 182 | + |
|---|
| 183 | + ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING, |
|---|
| 184 | + "i2c_gpio_fault_injector_scl_irq", priv); |
|---|
| 185 | + if (ret) |
|---|
| 186 | + goto output; |
|---|
| 187 | + |
|---|
| 188 | + wait_for_completion_interruptible(&priv->scl_irq_completion); |
|---|
| 189 | + |
|---|
| 190 | + free_irq(irq, priv); |
|---|
| 191 | + output: |
|---|
| 192 | + ret = gpiod_direction_output(priv->scl, 1) ?: ret; |
|---|
| 193 | + unlock: |
|---|
| 194 | + i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); |
|---|
| 195 | + |
|---|
| 196 | + return ret; |
|---|
| 197 | +} |
|---|
| 198 | + |
|---|
| 199 | +static irqreturn_t lose_arbitration_irq(int irq, void *dev_id) |
|---|
| 200 | +{ |
|---|
| 201 | + struct i2c_gpio_private_data *priv = dev_id; |
|---|
| 202 | + |
|---|
| 203 | + setsda(&priv->bit_data, 0); |
|---|
| 204 | + udelay(priv->scl_irq_data); |
|---|
| 205 | + setsda(&priv->bit_data, 1); |
|---|
| 206 | + |
|---|
| 207 | + complete(&priv->scl_irq_completion); |
|---|
| 208 | + |
|---|
| 209 | + return IRQ_HANDLED; |
|---|
| 210 | +} |
|---|
| 211 | + |
|---|
| 212 | +static int fops_lose_arbitration_set(void *data, u64 duration) |
|---|
| 213 | +{ |
|---|
| 214 | + struct i2c_gpio_private_data *priv = data; |
|---|
| 215 | + |
|---|
| 216 | + if (duration > 100 * 1000) |
|---|
| 217 | + return -EINVAL; |
|---|
| 218 | + |
|---|
| 219 | + priv->scl_irq_data = duration; |
|---|
| 220 | + /* |
|---|
| 221 | + * Interrupt on falling SCL. This ensures that the master under test has |
|---|
| 222 | + * really started the transfer. Interrupt on falling SDA did only |
|---|
| 223 | + * exercise 'bus busy' detection on some HW but not 'arbitration lost'. |
|---|
| 224 | + * Note that the interrupt latency may cause the first bits to be |
|---|
| 225 | + * transmitted correctly. |
|---|
| 226 | + */ |
|---|
| 227 | + return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq); |
|---|
| 228 | +} |
|---|
| 229 | +DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n"); |
|---|
| 230 | + |
|---|
| 231 | +static irqreturn_t inject_panic_irq(int irq, void *dev_id) |
|---|
| 232 | +{ |
|---|
| 233 | + struct i2c_gpio_private_data *priv = dev_id; |
|---|
| 234 | + |
|---|
| 235 | + udelay(priv->scl_irq_data); |
|---|
| 236 | + panic("I2C fault injector induced panic"); |
|---|
| 237 | + |
|---|
| 238 | + return IRQ_HANDLED; |
|---|
| 239 | +} |
|---|
| 240 | + |
|---|
| 241 | +static int fops_inject_panic_set(void *data, u64 duration) |
|---|
| 242 | +{ |
|---|
| 243 | + struct i2c_gpio_private_data *priv = data; |
|---|
| 244 | + |
|---|
| 245 | + if (duration > 100 * 1000) |
|---|
| 246 | + return -EINVAL; |
|---|
| 247 | + |
|---|
| 248 | + priv->scl_irq_data = duration; |
|---|
| 249 | + /* |
|---|
| 250 | + * Interrupt on falling SCL. This ensures that the master under test has |
|---|
| 251 | + * really started the transfer. |
|---|
| 252 | + */ |
|---|
| 253 | + return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq); |
|---|
| 254 | +} |
|---|
| 255 | +DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n"); |
|---|
| 256 | + |
|---|
| 165 | 257 | static void i2c_gpio_fault_injector_init(struct platform_device *pdev) |
|---|
| 166 | 258 | { |
|---|
| 167 | 259 | struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev); |
|---|
| .. | .. |
|---|
| 181 | 273 | if (!priv->debug_dir) |
|---|
| 182 | 274 | return; |
|---|
| 183 | 275 | |
|---|
| 184 | | - debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl); |
|---|
| 185 | | - debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda); |
|---|
| 276 | + init_completion(&priv->scl_irq_completion); |
|---|
| 277 | + |
|---|
| 186 | 278 | debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir, |
|---|
| 187 | 279 | priv, &fops_incomplete_addr_phase); |
|---|
| 188 | 280 | debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir, |
|---|
| 189 | 281 | priv, &fops_incomplete_write_byte); |
|---|
| 282 | + if (priv->bit_data.getscl) { |
|---|
| 283 | + debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir, |
|---|
| 284 | + priv, &fops_inject_panic); |
|---|
| 285 | + debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir, |
|---|
| 286 | + priv, &fops_lose_arbitration); |
|---|
| 287 | + } |
|---|
| 288 | + debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl); |
|---|
| 289 | + debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda); |
|---|
| 190 | 290 | } |
|---|
| 191 | 291 | |
|---|
| 192 | 292 | static void i2c_gpio_fault_injector_exit(struct platform_device *pdev) |
|---|
| .. | .. |
|---|
| 286 | 386 | |
|---|
| 287 | 387 | /* |
|---|
| 288 | 388 | * First get the GPIO pins; if it fails, we'll defer the probe. |
|---|
| 289 | | - * If the SDA line is marked from platform data or device tree as |
|---|
| 290 | | - * "open drain" it means something outside of our control is making |
|---|
| 291 | | - * this line being handled as open drain, and we should just handle |
|---|
| 292 | | - * it as any other output. Else we enforce open drain as this is |
|---|
| 293 | | - * required for an I2C bus. |
|---|
| 389 | + * If the SCL/SDA lines are marked "open drain" by platform data or |
|---|
| 390 | + * device tree then this means that something outside of our control is |
|---|
| 391 | + * marking these lines to be handled as open drain, and we should just |
|---|
| 392 | + * handle them as we handle any other output. Else we enforce open |
|---|
| 393 | + * drain as this is required for an I2C bus. |
|---|
| 294 | 394 | */ |
|---|
| 295 | 395 | if (pdata->sda_is_open_drain) |
|---|
| 296 | 396 | gflags = GPIOD_OUT_HIGH; |
|---|
| .. | .. |
|---|
| 300 | 400 | if (IS_ERR(priv->sda)) |
|---|
| 301 | 401 | return PTR_ERR(priv->sda); |
|---|
| 302 | 402 | |
|---|
| 303 | | - /* |
|---|
| 304 | | - * If the SCL line is marked from platform data or device tree as |
|---|
| 305 | | - * "open drain" it means something outside of our control is making |
|---|
| 306 | | - * this line being handled as open drain, and we should just handle |
|---|
| 307 | | - * it as any other output. Else we enforce open drain as this is |
|---|
| 308 | | - * required for an I2C bus. |
|---|
| 309 | | - */ |
|---|
| 310 | 403 | if (pdata->scl_is_open_drain) |
|---|
| 311 | 404 | gflags = GPIOD_OUT_HIGH; |
|---|
| 312 | 405 | else |
|---|
| .. | .. |
|---|
| 317 | 410 | |
|---|
| 318 | 411 | if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl)) |
|---|
| 319 | 412 | dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing"); |
|---|
| 413 | + else |
|---|
| 414 | + bit_data->can_do_atomic = true; |
|---|
| 320 | 415 | |
|---|
| 321 | 416 | bit_data->setsda = i2c_gpio_setsda_val; |
|---|
| 322 | 417 | bit_data->setscl = i2c_gpio_setscl_val; |
|---|