1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Bitbanging I2C bus driver using the GPIO API
4 *
5 * Copyright (C) 2007 Atmel Corporation
6 */
7#include <linux/completion.h>
8#include <linux/debugfs.h>
9#include <linux/delay.h>
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>
16#include <linux/platform_data/i2c-gpio.h>
17#include <linux/platform_device.h>
18#include <linux/property.h>
19#include <linux/slab.h>
20
21struct i2c_gpio_private_data {
22 struct gpio_desc *sda;
23 struct gpio_desc *scl;
24 struct i2c_adapter adap;
25 struct i2c_algo_bit_data bit_data;
26 struct i2c_gpio_platform_data pdata;
27#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
28 struct dentry *debug_dir;
29 /* these must be protected by bus lock */
30 struct completion scl_irq_completion;
31 u64 scl_irq_data;
32#endif
33};
34
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 */
40static void i2c_gpio_setsda_val(void *data, int state)
41{
42 struct i2c_gpio_private_data *priv = data;
43
44 gpiod_set_value_cansleep(desc: priv->sda, value: state);
45}
46
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 */
53static void i2c_gpio_setscl_val(void *data, int state)
54{
55 struct i2c_gpio_private_data *priv = data;
56
57 gpiod_set_value_cansleep(desc: priv->scl, value: state);
58}
59
60static int i2c_gpio_getsda(void *data)
61{
62 struct i2c_gpio_private_data *priv = data;
63
64 return gpiod_get_value_cansleep(desc: priv->sda);
65}
66
67static int i2c_gpio_getscl(void *data)
68{
69 struct i2c_gpio_private_data *priv = data;
70
71 return gpiod_get_value_cansleep(desc: priv->scl);
72}
73
74#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
75static 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) \
83static 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} \
92static 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} \
101DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
102
103WIRE_ATTRIBUTE(scl);
104WIRE_ATTRIBUTE(sda);
105
106static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
107 u32 pattern, u8 pattern_size)
108{
109 struct i2c_algo_bit_data *bit_data = &priv->bit_data;
110 int i;
111
112 i2c_lock_bus(adapter: &priv->adap, I2C_LOCK_ROOT_ADAPTER);
113
114 /* START condition */
115 setsda(bit_data, 0);
116 udelay(bit_data->udelay);
117
118 /* Send pattern, request ACK, don't send STOP */
119 for (i = pattern_size - 1; i >= 0; i--) {
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
128 i2c_unlock_bus(adapter: &priv->adap, I2C_LOCK_ROOT_ADAPTER);
129}
130
131static 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, pattern_size: 9);
143
144 return 0;
145}
146DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
147
148static 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, pattern_size: 18);
162
163 return 0;
164}
165DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
166
167static 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(desc: priv->scl);
171
172 if (irq < 0)
173 return irq;
174
175 i2c_lock_bus(adapter: &priv->adap, I2C_LOCK_ROOT_ADAPTER);
176
177 ret = gpiod_direction_input(desc: priv->scl);
178 if (ret)
179 goto unlock;
180
181 reinit_completion(x: &priv->scl_irq_completion);
182
183 ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
184 name: "i2c_gpio_fault_injector_scl_irq", dev: priv);
185 if (ret)
186 goto output;
187
188 wait_for_completion_interruptible(x: &priv->scl_irq_completion);
189
190 free_irq(irq, priv);
191 output:
192 ret = gpiod_direction_output(desc: priv->scl, value: 1) ?: ret;
193 unlock:
194 i2c_unlock_bus(adapter: &priv->adap, I2C_LOCK_ROOT_ADAPTER);
195
196 return ret;
197}
198
199static 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
212static 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, handler: lose_arbitration_irq);
228}
229DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
230
231static 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(fmt: "I2C fault injector induced panic");
237
238 return IRQ_HANDLED;
239}
240
241static 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, handler: inject_panic_irq);
254}
255DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
256
257static 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(name: "i2c-fault-injector", NULL);
268
269 priv->debug_dir = debugfs_create_dir(name: pdev->name, parent: i2c_gpio_debug_dir);
270
271 init_completion(x: &priv->scl_irq_completion);
272
273 debugfs_create_file_unsafe(name: "incomplete_address_phase", mode: 0200, parent: priv->debug_dir,
274 data: priv, fops: &fops_incomplete_addr_phase);
275 debugfs_create_file_unsafe(name: "incomplete_write_byte", mode: 0200, parent: priv->debug_dir,
276 data: priv, fops: &fops_incomplete_write_byte);
277 if (priv->bit_data.getscl) {
278 debugfs_create_file_unsafe(name: "inject_panic", mode: 0200, parent: priv->debug_dir,
279 data: priv, fops: &fops_inject_panic);
280 debugfs_create_file_unsafe(name: "lose_arbitration", mode: 0200, parent: priv->debug_dir,
281 data: priv, fops: &fops_lose_arbitration);
282 }
283 debugfs_create_file_unsafe(name: "scl", mode: 0600, parent: priv->debug_dir, data: priv, fops: &fops_scl);
284 debugfs_create_file_unsafe(name: "sda", mode: 0600, parent: priv->debug_dir, data: priv, fops: &fops_sda);
285}
286
287static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
288{
289 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
290
291 debugfs_remove_recursive(dentry: priv->debug_dir);
292}
293#else
294static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
295static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
296#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
297
298/* Get i2c-gpio properties from DT or ACPI table */
299static void i2c_gpio_get_properties(struct device *dev,
300 struct i2c_gpio_platform_data *pdata)
301{
302 u32 reg;
303
304 device_property_read_u32(dev, propname: "i2c-gpio,delay-us", val: &pdata->udelay);
305
306 if (!device_property_read_u32(dev, propname: "i2c-gpio,timeout-ms", val: &reg))
307 pdata->timeout = msecs_to_jiffies(m: reg);
308
309 pdata->sda_is_open_drain =
310 device_property_read_bool(dev, propname: "i2c-gpio,sda-open-drain");
311 pdata->scl_is_open_drain =
312 device_property_read_bool(dev, propname: "i2c-gpio,scl-open-drain");
313 pdata->scl_is_output_only =
314 device_property_read_bool(dev, propname: "i2c-gpio,scl-output-only");
315 pdata->sda_is_output_only =
316 device_property_read_bool(dev, propname: "i2c-gpio,sda-output-only");
317 pdata->sda_has_no_pullup =
318 device_property_read_bool(dev, propname: "i2c-gpio,sda-has-no-pullup");
319 pdata->scl_has_no_pullup =
320 device_property_read_bool(dev, propname: "i2c-gpio,scl-has-no-pullup");
321}
322
323static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
324 const char *con_id,
325 unsigned int index,
326 enum gpiod_flags gflags)
327{
328 struct gpio_desc *retdesc;
329 int ret;
330
331 retdesc = devm_gpiod_get(dev, con_id, flags: gflags);
332 if (!IS_ERR(ptr: retdesc)) {
333 dev_dbg(dev, "got GPIO from name %s\n", con_id);
334 return retdesc;
335 }
336
337 retdesc = devm_gpiod_get_index(dev, NULL, idx: index, flags: gflags);
338 if (!IS_ERR(ptr: retdesc)) {
339 dev_dbg(dev, "got GPIO from index %u\n", index);
340 return retdesc;
341 }
342
343 ret = PTR_ERR(ptr: retdesc);
344
345 /* FIXME: hack in the old code, is this really necessary? */
346 if (ret == -EINVAL)
347 retdesc = ERR_PTR(error: -EPROBE_DEFER);
348
349 /* This happens if the GPIO driver is not yet probed, let's defer */
350 if (ret == -ENOENT)
351 retdesc = ERR_PTR(error: -EPROBE_DEFER);
352
353 if (PTR_ERR(ptr: retdesc) != -EPROBE_DEFER)
354 dev_err(dev, "error trying to get descriptor: %d\n", ret);
355
356 return retdesc;
357}
358
359static int i2c_gpio_probe(struct platform_device *pdev)
360{
361 struct i2c_gpio_private_data *priv;
362 struct i2c_gpio_platform_data *pdata;
363 struct i2c_algo_bit_data *bit_data;
364 struct i2c_adapter *adap;
365 struct device *dev = &pdev->dev;
366 struct fwnode_handle *fwnode = dev_fwnode(dev);
367 enum gpiod_flags gflags;
368 int ret;
369
370 priv = devm_kzalloc(dev, size: sizeof(*priv), GFP_KERNEL);
371 if (!priv)
372 return -ENOMEM;
373
374 adap = &priv->adap;
375 bit_data = &priv->bit_data;
376 pdata = &priv->pdata;
377
378 if (fwnode) {
379 i2c_gpio_get_properties(dev, pdata);
380 } else {
381 /*
382 * If all platform data settings are zero it is OK
383 * to not provide any platform data from the board.
384 */
385 if (dev_get_platdata(dev))
386 memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
387 }
388
389 /*
390 * First get the GPIO pins; if it fails, we'll defer the probe.
391 * If the SCL/SDA lines are marked "open drain" by platform data or
392 * device tree then this means that something outside of our control is
393 * marking these lines to be handled as open drain, and we should just
394 * handle them as we handle any other output. Else we enforce open
395 * drain as this is required for an I2C bus.
396 */
397 if (pdata->sda_is_open_drain || pdata->sda_has_no_pullup)
398 gflags = GPIOD_OUT_HIGH;
399 else
400 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
401 priv->sda = i2c_gpio_get_desc(dev, con_id: "sda", index: 0, gflags);
402 if (IS_ERR(ptr: priv->sda))
403 return PTR_ERR(ptr: priv->sda);
404
405 if (pdata->scl_is_open_drain || pdata->scl_has_no_pullup)
406 gflags = GPIOD_OUT_HIGH;
407 else
408 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
409 priv->scl = i2c_gpio_get_desc(dev, con_id: "scl", index: 1, gflags);
410 if (IS_ERR(ptr: priv->scl))
411 return PTR_ERR(ptr: priv->scl);
412
413 if (gpiod_cansleep(desc: priv->sda) || gpiod_cansleep(desc: priv->scl))
414 dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
415 else
416 bit_data->can_do_atomic = true;
417
418 bit_data->setsda = i2c_gpio_setsda_val;
419 bit_data->setscl = i2c_gpio_setscl_val;
420
421 if (!pdata->scl_is_output_only)
422 bit_data->getscl = i2c_gpio_getscl;
423 if (!pdata->sda_is_output_only)
424 bit_data->getsda = i2c_gpio_getsda;
425
426 if (pdata->udelay)
427 bit_data->udelay = pdata->udelay;
428 else if (pdata->scl_is_output_only)
429 bit_data->udelay = 50; /* 10 kHz */
430 else
431 bit_data->udelay = 5; /* 100 kHz */
432
433 if (pdata->timeout)
434 bit_data->timeout = pdata->timeout;
435 else
436 bit_data->timeout = HZ / 10; /* 100 ms */
437
438 bit_data->data = priv;
439
440 adap->owner = THIS_MODULE;
441 if (fwnode)
442 strscpy(p: adap->name, q: dev_name(dev), size: sizeof(adap->name));
443 else
444 snprintf(buf: adap->name, size: sizeof(adap->name), fmt: "i2c-gpio%d", pdev->id);
445
446 adap->algo_data = bit_data;
447 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
448 adap->dev.parent = dev;
449 device_set_node(dev: &adap->dev, fwnode);
450
451 adap->nr = pdev->id;
452 ret = i2c_bit_add_numbered_bus(adap);
453 if (ret)
454 return ret;
455
456 platform_set_drvdata(pdev, data: priv);
457
458 /*
459 * FIXME: using global GPIO numbers is not helpful. If/when we
460 * get accessors to get the actual name of the GPIO line,
461 * from the descriptor, then provide that instead.
462 */
463 dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
464 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
465 pdata->scl_is_output_only
466 ? ", no clock stretching" : "");
467
468 i2c_gpio_fault_injector_init(pdev);
469
470 return 0;
471}
472
473static void i2c_gpio_remove(struct platform_device *pdev)
474{
475 struct i2c_gpio_private_data *priv;
476 struct i2c_adapter *adap;
477
478 i2c_gpio_fault_injector_exit(pdev);
479
480 priv = platform_get_drvdata(pdev);
481 adap = &priv->adap;
482
483 i2c_del_adapter(adap);
484}
485
486static const struct of_device_id i2c_gpio_dt_ids[] = {
487 { .compatible = "i2c-gpio", },
488 { /* sentinel */ }
489};
490
491MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
492
493static const struct acpi_device_id i2c_gpio_acpi_match[] = {
494 { "LOON0005" }, /* LoongArch */
495 { }
496};
497MODULE_DEVICE_TABLE(acpi, i2c_gpio_acpi_match);
498
499static struct platform_driver i2c_gpio_driver = {
500 .driver = {
501 .name = "i2c-gpio",
502 .of_match_table = i2c_gpio_dt_ids,
503 .acpi_match_table = i2c_gpio_acpi_match,
504 },
505 .probe = i2c_gpio_probe,
506 .remove_new = i2c_gpio_remove,
507};
508
509static int __init i2c_gpio_init(void)
510{
511 int ret;
512
513 ret = platform_driver_register(&i2c_gpio_driver);
514 if (ret)
515 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
516
517 return ret;
518}
519subsys_initcall(i2c_gpio_init);
520
521static void __exit i2c_gpio_exit(void)
522{
523 platform_driver_unregister(&i2c_gpio_driver);
524}
525module_exit(i2c_gpio_exit);
526
527MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
528MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
529MODULE_LICENSE("GPL v2");
530MODULE_ALIAS("platform:i2c-gpio");
531

source code of linux/drivers/i2c/busses/i2c-gpio.c