Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Bitbanging I2C bus driver using the GPIO API |
| 4 | * |
| 5 | * Copyright (C) 2007 Atmel Corporation |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 6 | */ |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 7 | #include <linux/completion.h> |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/delay.h> |
Wolfram Sang | 2f8aa46 | 2019-01-19 13:16:55 +0100 | [diff] [blame] | 10 | #include <linux/gpio/consumer.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 11 | #include <linux/i2c-algo-bit.h> |
Wolfram Sang | 2f8aa46 | 2019-01-19 13:16:55 +0100 | [diff] [blame] | 12 | #include <linux/i2c.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 13 | #include <linux/init.h> |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
Sachin Kamat | 4edd65e | 2013-10-16 15:26:33 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
Wolfram Sang | 2f8aa46 | 2019-01-19 13:16:55 +0100 | [diff] [blame] | 17 | #include <linux/platform_data/i2c-gpio.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 20 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 21 | struct i2c_gpio_private_data { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 22 | struct gpio_desc *sda; |
| 23 | struct gpio_desc *scl; |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 24 | struct i2c_adapter adap; |
| 25 | struct i2c_algo_bit_data bit_data; |
| 26 | struct i2c_gpio_platform_data pdata; |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 27 | #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR |
| 28 | struct dentry *debug_dir; |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 29 | /* these must be protected by bus lock */ |
| 30 | struct completion scl_irq_completion; |
| 31 | u64 scl_irq_data; |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 32 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 33 | }; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 34 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Toggle SDA by changing the output value of the pin. This is only |
| 37 | * valid for pins configured as open drain (i.e. setting the value |
| 38 | * high effectively turns off the output driver.) |
| 39 | */ |
| 40 | static void i2c_gpio_setsda_val(void *data, int state) |
| 41 | { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 42 | struct i2c_gpio_private_data *priv = data; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 43 | |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 44 | gpiod_set_value_cansleep(priv->sda, state); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 47 | /* |
| 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 |
| 50 | * pins. The latter case will break the i2c protocol, but it will |
| 51 | * often work in practice. |
| 52 | */ |
| 53 | static void i2c_gpio_setscl_val(void *data, int state) |
| 54 | { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 55 | struct i2c_gpio_private_data *priv = data; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 56 | |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 57 | gpiod_set_value_cansleep(priv->scl, state); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Atsushi Nemoto | 4d6ceed | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 60 | static int i2c_gpio_getsda(void *data) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 61 | { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 62 | struct i2c_gpio_private_data *priv = data; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 63 | |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 64 | return gpiod_get_value_cansleep(priv->sda); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Atsushi Nemoto | 4d6ceed | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 67 | static int i2c_gpio_getscl(void *data) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 68 | { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 69 | struct i2c_gpio_private_data *priv = data; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 70 | |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 71 | return gpiod_get_value_cansleep(priv->scl); |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 74 | #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR |
| 75 | static struct dentry *i2c_gpio_debug_dir; |
| 76 | |
| 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)) |
| 81 | |
| 82 | #define WIRE_ATTRIBUTE(wire) \ |
Peter Rosin | 3f3a89e | 2018-06-20 07:18:03 +0200 | [diff] [blame] | 83 | static int fops_##wire##_get(void *data, u64 *val) \ |
| 84 | { \ |
| 85 | struct i2c_gpio_private_data *priv = data; \ |
| 86 | \ |
| 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); \ |
| 90 | return 0; \ |
| 91 | } \ |
| 92 | static int fops_##wire##_set(void *data, u64 val) \ |
| 93 | { \ |
| 94 | struct i2c_gpio_private_data *priv = data; \ |
| 95 | \ |
| 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); \ |
| 99 | return 0; \ |
| 100 | } \ |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 101 | DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n") |
| 102 | |
| 103 | WIRE_ATTRIBUTE(scl); |
| 104 | WIRE_ATTRIBUTE(sda); |
| 105 | |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 106 | static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv, |
| 107 | u32 pattern, u8 pattern_size) |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 108 | { |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 109 | struct i2c_algo_bit_data *bit_data = &priv->bit_data; |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 110 | int i; |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 111 | |
Peter Rosin | 3f3a89e | 2018-06-20 07:18:03 +0200 | [diff] [blame] | 112 | i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 113 | |
| 114 | /* START condition */ |
| 115 | setsda(bit_data, 0); |
| 116 | udelay(bit_data->udelay); |
| 117 | |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 118 | /* Send pattern, request ACK, don't send STOP */ |
| 119 | for (i = pattern_size - 1; i >= 0; i--) { |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 120 | setscl(bit_data, 0); |
| 121 | udelay(bit_data->udelay / 2); |
| 122 | setsda(bit_data, (pattern >> i) & 1); |
| 123 | udelay((bit_data->udelay + 1) / 2); |
| 124 | setscl(bit_data, 1); |
| 125 | udelay(bit_data->udelay); |
| 126 | } |
| 127 | |
Peter Rosin | 3f3a89e | 2018-06-20 07:18:03 +0200 | [diff] [blame] | 128 | i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int fops_incomplete_addr_phase_set(void *data, u64 addr) |
| 132 | { |
| 133 | struct i2c_gpio_private_data *priv = data; |
| 134 | u32 pattern; |
| 135 | |
| 136 | if (addr > 0x7f) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */ |
| 140 | pattern = (addr << 2) | 3; |
| 141 | |
| 142 | i2c_gpio_incomplete_transfer(priv, pattern, 9); |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 146 | DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n"); |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 147 | |
Wolfram Sang | bbe8997 | 2018-06-29 11:12:47 +0200 | [diff] [blame] | 148 | static int fops_incomplete_write_byte_set(void *data, u64 addr) |
| 149 | { |
| 150 | struct i2c_gpio_private_data *priv = data; |
| 151 | u32 pattern; |
| 152 | |
| 153 | if (addr > 0x7f) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */ |
| 157 | pattern = (addr << 2) | 1; |
| 158 | /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */ |
| 159 | pattern = (pattern << 9) | 1; |
| 160 | |
| 161 | i2c_gpio_incomplete_transfer(priv, pattern, 18); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n"); |
| 166 | |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 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 | |
Wolfram Sang | bb6bdd5 | 2019-02-19 17:39:46 +0100 | [diff] [blame] | 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 | |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 257 | static void i2c_gpio_fault_injector_init(struct platform_device *pdev) |
| 258 | { |
| 259 | struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev); |
| 260 | |
| 261 | /* |
| 262 | * If there will be a debugfs-dir per i2c adapter somewhen, put the |
| 263 | * 'fault-injector' dir there. Until then, we have a global dir with |
| 264 | * all adapters as subdirs. |
| 265 | */ |
| 266 | if (!i2c_gpio_debug_dir) { |
| 267 | i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL); |
| 268 | if (!i2c_gpio_debug_dir) |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir); |
| 273 | if (!priv->debug_dir) |
| 274 | return; |
| 275 | |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 276 | init_completion(&priv->scl_irq_completion); |
| 277 | |
Wolfram Sang | 16d55da | 2018-06-29 11:12:46 +0200 | [diff] [blame] | 278 | debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir, |
| 279 | priv, &fops_incomplete_addr_phase); |
Wolfram Sang | bbe8997 | 2018-06-29 11:12:47 +0200 | [diff] [blame] | 280 | debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir, |
| 281 | priv, &fops_incomplete_write_byte); |
Wolfram Sang | bb6bdd5 | 2019-02-19 17:39:46 +0100 | [diff] [blame] | 282 | if (priv->bit_data.getscl) { |
| 283 | debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir, |
| 284 | priv, &fops_inject_panic); |
Wolfram Sang | 63e57b6 | 2019-02-19 17:39:45 +0100 | [diff] [blame] | 285 | debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir, |
| 286 | priv, &fops_lose_arbitration); |
Wolfram Sang | bb6bdd5 | 2019-02-19 17:39:46 +0100 | [diff] [blame] | 287 | } |
Wolfram Sang | c6324fa | 2019-01-21 15:16:56 +0100 | [diff] [blame] | 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); |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void i2c_gpio_fault_injector_exit(struct platform_device *pdev) |
| 293 | { |
| 294 | struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev); |
| 295 | |
| 296 | debugfs_remove_recursive(priv->debug_dir); |
| 297 | } |
| 298 | #else |
| 299 | static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {} |
| 300 | static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {} |
| 301 | #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/ |
| 302 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 303 | static void of_i2c_gpio_get_props(struct device_node *np, |
| 304 | struct i2c_gpio_platform_data *pdata) |
| 305 | { |
| 306 | u32 reg; |
| 307 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 308 | of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); |
| 309 | |
| 310 | if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) |
| 311 | pdata->timeout = msecs_to_jiffies(reg); |
| 312 | |
| 313 | pdata->sda_is_open_drain = |
| 314 | of_property_read_bool(np, "i2c-gpio,sda-open-drain"); |
| 315 | pdata->scl_is_open_drain = |
| 316 | of_property_read_bool(np, "i2c-gpio,scl-open-drain"); |
| 317 | pdata->scl_is_output_only = |
| 318 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 319 | } |
| 320 | |
Linus Walleij | 05c7477 | 2017-09-16 23:56:49 +0200 | [diff] [blame] | 321 | static struct gpio_desc *i2c_gpio_get_desc(struct device *dev, |
| 322 | const char *con_id, |
| 323 | unsigned int index, |
| 324 | enum gpiod_flags gflags) |
| 325 | { |
| 326 | struct gpio_desc *retdesc; |
| 327 | int ret; |
| 328 | |
| 329 | retdesc = devm_gpiod_get(dev, con_id, gflags); |
| 330 | if (!IS_ERR(retdesc)) { |
| 331 | dev_dbg(dev, "got GPIO from name %s\n", con_id); |
| 332 | return retdesc; |
| 333 | } |
| 334 | |
| 335 | retdesc = devm_gpiod_get_index(dev, NULL, index, gflags); |
| 336 | if (!IS_ERR(retdesc)) { |
| 337 | dev_dbg(dev, "got GPIO from index %u\n", index); |
| 338 | return retdesc; |
| 339 | } |
| 340 | |
| 341 | ret = PTR_ERR(retdesc); |
| 342 | |
| 343 | /* FIXME: hack in the old code, is this really necessary? */ |
| 344 | if (ret == -EINVAL) |
| 345 | retdesc = ERR_PTR(-EPROBE_DEFER); |
| 346 | |
| 347 | /* This happens if the GPIO driver is not yet probed, let's defer */ |
| 348 | if (ret == -ENOENT) |
| 349 | retdesc = ERR_PTR(-EPROBE_DEFER); |
| 350 | |
Hamish Martin | 3747cd2 | 2020-03-10 10:16:18 +1300 | [diff] [blame] | 351 | if (PTR_ERR(retdesc) != -EPROBE_DEFER) |
Linus Walleij | 05c7477 | 2017-09-16 23:56:49 +0200 | [diff] [blame] | 352 | dev_err(dev, "error trying to get descriptor: %d\n", ret); |
| 353 | |
| 354 | return retdesc; |
| 355 | } |
| 356 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 357 | static int i2c_gpio_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 358 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 359 | struct i2c_gpio_private_data *priv; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 360 | struct i2c_gpio_platform_data *pdata; |
| 361 | struct i2c_algo_bit_data *bit_data; |
| 362 | struct i2c_adapter *adap; |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 363 | struct device *dev = &pdev->dev; |
| 364 | struct device_node *np = dev->of_node; |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 365 | enum gpiod_flags gflags; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 366 | int ret; |
| 367 | |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 368 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
Jingoo Han | a0682a3 | 2013-12-17 15:48:19 +0900 | [diff] [blame] | 369 | if (!priv) |
| 370 | return -ENOMEM; |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 371 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 372 | adap = &priv->adap; |
| 373 | bit_data = &priv->bit_data; |
| 374 | pdata = &priv->pdata; |
| 375 | |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 376 | if (np) { |
| 377 | of_i2c_gpio_get_props(np, pdata); |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 378 | } else { |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 379 | /* |
| 380 | * If all platform data settings are zero it is OK |
| 381 | * to not provide any platform data from the board. |
| 382 | */ |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 383 | if (dev_get_platdata(dev)) |
| 384 | memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata)); |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 385 | } |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 386 | |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 387 | /* |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 388 | * First get the GPIO pins; if it fails, we'll defer the probe. |
Wolfram Sang | 60c1d56 | 2019-02-05 13:58:41 +0100 | [diff] [blame] | 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. |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 394 | */ |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 395 | if (pdata->sda_is_open_drain) |
| 396 | gflags = GPIOD_OUT_HIGH; |
| 397 | else |
| 398 | gflags = GPIOD_OUT_HIGH_OPEN_DRAIN; |
Linus Walleij | 05c7477 | 2017-09-16 23:56:49 +0200 | [diff] [blame] | 399 | priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags); |
| 400 | if (IS_ERR(priv->sda)) |
| 401 | return PTR_ERR(priv->sda); |
| 402 | |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 403 | if (pdata->scl_is_open_drain) |
Wolfram Sang | 12b731d | 2018-06-16 21:56:36 +0900 | [diff] [blame] | 404 | gflags = GPIOD_OUT_HIGH; |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 405 | else |
Wolfram Sang | 12b731d | 2018-06-16 21:56:36 +0900 | [diff] [blame] | 406 | gflags = GPIOD_OUT_HIGH_OPEN_DRAIN; |
Linus Walleij | 05c7477 | 2017-09-16 23:56:49 +0200 | [diff] [blame] | 407 | priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags); |
| 408 | if (IS_ERR(priv->scl)) |
| 409 | return PTR_ERR(priv->scl); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 410 | |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 411 | if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl)) |
| 412 | dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing"); |
Wolfram Sang | e155e38 | 2019-04-03 14:40:19 +0200 | [diff] [blame] | 413 | else |
| 414 | bit_data->can_do_atomic = true; |
Jan Kundrát | f11a044 | 2017-12-22 22:47:16 +0100 | [diff] [blame] | 415 | |
Linus Walleij | 7bb7502 | 2017-09-10 19:54:21 +0200 | [diff] [blame] | 416 | bit_data->setsda = i2c_gpio_setsda_val; |
| 417 | bit_data->setscl = i2c_gpio_setscl_val; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 418 | |
| 419 | if (!pdata->scl_is_output_only) |
| 420 | bit_data->getscl = i2c_gpio_getscl; |
| 421 | bit_data->getsda = i2c_gpio_getsda; |
| 422 | |
| 423 | if (pdata->udelay) |
| 424 | bit_data->udelay = pdata->udelay; |
| 425 | else if (pdata->scl_is_output_only) |
| 426 | bit_data->udelay = 50; /* 10 kHz */ |
| 427 | else |
| 428 | bit_data->udelay = 5; /* 100 kHz */ |
| 429 | |
| 430 | if (pdata->timeout) |
| 431 | bit_data->timeout = pdata->timeout; |
| 432 | else |
| 433 | bit_data->timeout = HZ / 10; /* 100 ms */ |
| 434 | |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 435 | bit_data->data = priv; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 436 | |
| 437 | adap->owner = THIS_MODULE; |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 438 | if (np) |
| 439 | strlcpy(adap->name, dev_name(dev), sizeof(adap->name)); |
Bo Shen | 58a7371 | 2012-10-15 15:51:17 +0800 | [diff] [blame] | 440 | else |
| 441 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); |
| 442 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 443 | adap->algo_data = bit_data; |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 444 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 445 | adap->dev.parent = dev; |
| 446 | adap->dev.of_node = np; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 447 | |
Karol Lewandowski | 44454ba | 2012-03-27 11:10:28 +0200 | [diff] [blame] | 448 | adap->nr = pdev->id; |
Atsushi Nemoto | 7e69c3a | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 449 | ret = i2c_bit_add_numbered_bus(adap); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 450 | if (ret) |
Jingoo Han | a0682a3 | 2013-12-17 15:48:19 +0900 | [diff] [blame] | 451 | return ret; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 452 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 453 | platform_set_drvdata(pdev, priv); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 454 | |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 455 | /* |
| 456 | * FIXME: using global GPIO numbers is not helpful. If/when we |
| 457 | * get accessors to get the actual name of the GPIO line, |
| 458 | * from the descriptor, then provide that instead. |
| 459 | */ |
Linus Walleij | b9ab051 | 2017-09-10 23:15:35 +0200 | [diff] [blame] | 460 | dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n", |
Linus Walleij | b2e6355 | 2017-09-10 01:30:46 +0200 | [diff] [blame] | 461 | desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 462 | pdata->scl_is_output_only |
| 463 | ? ", no clock stretching" : ""); |
| 464 | |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 465 | i2c_gpio_fault_injector_init(pdev); |
| 466 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 467 | return 0; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 470 | static int i2c_gpio_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 471 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 472 | struct i2c_gpio_private_data *priv; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 473 | struct i2c_adapter *adap; |
| 474 | |
Wolfram Sang | 14911c6 | 2017-11-28 16:53:32 +0100 | [diff] [blame] | 475 | i2c_gpio_fault_injector_exit(pdev); |
| 476 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 477 | priv = platform_get_drvdata(pdev); |
| 478 | adap = &priv->adap; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 479 | |
| 480 | i2c_del_adapter(adap); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 485 | #if defined(CONFIG_OF) |
| 486 | static const struct of_device_id i2c_gpio_dt_ids[] = { |
| 487 | { .compatible = "i2c-gpio", }, |
| 488 | { /* sentinel */ } |
| 489 | }; |
| 490 | |
| 491 | MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); |
| 492 | #endif |
| 493 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 494 | static struct platform_driver i2c_gpio_driver = { |
| 495 | .driver = { |
| 496 | .name = "i2c-gpio", |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 497 | .of_match_table = of_match_ptr(i2c_gpio_dt_ids), |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 498 | }, |
Ben Dooks | 1efe7c5 | 2008-07-28 12:04:09 +0100 | [diff] [blame] | 499 | .probe = i2c_gpio_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 500 | .remove = i2c_gpio_remove, |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | static int __init i2c_gpio_init(void) |
| 504 | { |
| 505 | int ret; |
| 506 | |
Ben Dooks | 1efe7c5 | 2008-07-28 12:04:09 +0100 | [diff] [blame] | 507 | ret = platform_driver_register(&i2c_gpio_driver); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 508 | if (ret) |
| 509 | printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); |
| 510 | |
| 511 | return ret; |
| 512 | } |
Marek Szyprowski | b868078 | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 513 | subsys_initcall(i2c_gpio_init); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 514 | |
| 515 | static void __exit i2c_gpio_exit(void) |
| 516 | { |
| 517 | platform_driver_unregister(&i2c_gpio_driver); |
| 518 | } |
| 519 | module_exit(i2c_gpio_exit); |
| 520 | |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 521 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 522 | MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); |
Wolfram Sang | 61adf63 | 2020-12-14 17:00:44 +0100 | [diff] [blame] | 523 | MODULE_LICENSE("GPL v2"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 524 | MODULE_ALIAS("platform:i2c-gpio"); |