xref: /openbmc/linux/drivers/i2c/busses/i2c-gpio.c (revision be709d48)
1 /*
2  * Bitbanging I2C bus driver using the GPIO API
3  *
4  * 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  */
10 #include <linux/completion.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c-algo-bit.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_data/i2c-gpio.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 
24 struct i2c_gpio_private_data {
25 	struct gpio_desc *sda;
26 	struct gpio_desc *scl;
27 	struct i2c_adapter adap;
28 	struct i2c_algo_bit_data bit_data;
29 	struct i2c_gpio_platform_data pdata;
30 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
31 	struct dentry *debug_dir;
32 	/* these must be protected by bus lock */
33 	struct completion scl_irq_completion;
34 	u64 scl_irq_data;
35 #endif
36 };
37 
38 /*
39  * Toggle SDA by changing the output value of the pin. This is only
40  * valid for pins configured as open drain (i.e. setting the value
41  * high effectively turns off the output driver.)
42  */
43 static void i2c_gpio_setsda_val(void *data, int state)
44 {
45 	struct i2c_gpio_private_data *priv = data;
46 
47 	gpiod_set_value_cansleep(priv->sda, state);
48 }
49 
50 /*
51  * Toggle SCL by changing the output value of the pin. This is used
52  * for pins that are configured as open drain and for output-only
53  * pins. The latter case will break the i2c protocol, but it will
54  * often work in practice.
55  */
56 static void i2c_gpio_setscl_val(void *data, int state)
57 {
58 	struct i2c_gpio_private_data *priv = data;
59 
60 	gpiod_set_value_cansleep(priv->scl, state);
61 }
62 
63 static int i2c_gpio_getsda(void *data)
64 {
65 	struct i2c_gpio_private_data *priv = data;
66 
67 	return gpiod_get_value_cansleep(priv->sda);
68 }
69 
70 static int i2c_gpio_getscl(void *data)
71 {
72 	struct i2c_gpio_private_data *priv = data;
73 
74 	return gpiod_get_value_cansleep(priv->scl);
75 }
76 
77 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
78 static struct dentry *i2c_gpio_debug_dir;
79 
80 #define setsda(bd, val)	((bd)->setsda((bd)->data, val))
81 #define setscl(bd, val)	((bd)->setscl((bd)->data, val))
82 #define getsda(bd)	((bd)->getsda((bd)->data))
83 #define getscl(bd)	((bd)->getscl((bd)->data))
84 
85 #define WIRE_ATTRIBUTE(wire) \
86 static int fops_##wire##_get(void *data, u64 *val)		\
87 {								\
88 	struct i2c_gpio_private_data *priv = data;		\
89 								\
90 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
91 	*val = get##wire(&priv->bit_data);			\
92 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
93 	return 0;						\
94 }								\
95 static int fops_##wire##_set(void *data, u64 val)		\
96 {								\
97 	struct i2c_gpio_private_data *priv = data;		\
98 								\
99 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
100 	set##wire(&priv->bit_data, val);			\
101 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
102 	return 0;						\
103 }								\
104 DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
105 
106 WIRE_ATTRIBUTE(scl);
107 WIRE_ATTRIBUTE(sda);
108 
109 static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
110 					u32 pattern, u8 pattern_size)
111 {
112 	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
113 	int i;
114 
115 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
116 
117 	/* START condition */
118 	setsda(bit_data, 0);
119 	udelay(bit_data->udelay);
120 
121 	/* Send pattern, request ACK, don't send STOP */
122 	for (i = pattern_size - 1; i >= 0; i--) {
123 		setscl(bit_data, 0);
124 		udelay(bit_data->udelay / 2);
125 		setsda(bit_data, (pattern >> i) & 1);
126 		udelay((bit_data->udelay + 1) / 2);
127 		setscl(bit_data, 1);
128 		udelay(bit_data->udelay);
129 	}
130 
131 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
132 }
133 
134 static int fops_incomplete_addr_phase_set(void *data, u64 addr)
135 {
136 	struct i2c_gpio_private_data *priv = data;
137 	u32 pattern;
138 
139 	if (addr > 0x7f)
140 		return -EINVAL;
141 
142 	/* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
143 	pattern = (addr << 2) | 3;
144 
145 	i2c_gpio_incomplete_transfer(priv, pattern, 9);
146 
147 	return 0;
148 }
149 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
150 
151 static int fops_incomplete_write_byte_set(void *data, u64 addr)
152 {
153 	struct i2c_gpio_private_data *priv = data;
154 	u32 pattern;
155 
156 	if (addr > 0x7f)
157 		return -EINVAL;
158 
159 	/* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
160 	pattern = (addr << 2) | 1;
161 	/* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
162 	pattern = (pattern << 9) | 1;
163 
164 	i2c_gpio_incomplete_transfer(priv, pattern, 18);
165 
166 	return 0;
167 }
168 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
169 
170 static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
171 				       irqreturn_t handler(int, void*))
172 {
173 	int ret, irq = gpiod_to_irq(priv->scl);
174 
175 	if (irq < 0)
176 		return irq;
177 
178 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
179 
180 	ret = gpiod_direction_input(priv->scl);
181 	if (ret)
182 		goto unlock;
183 
184 	reinit_completion(&priv->scl_irq_completion);
185 
186 	ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
187 			  "i2c_gpio_fault_injector_scl_irq", priv);
188 	if (ret)
189 		goto output;
190 
191 	wait_for_completion_interruptible(&priv->scl_irq_completion);
192 
193 	free_irq(irq, priv);
194  output:
195 	ret = gpiod_direction_output(priv->scl, 1) ?: ret;
196  unlock:
197 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
198 
199 	return ret;
200 }
201 
202 static irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
203 {
204 	struct i2c_gpio_private_data *priv = dev_id;
205 
206 	setsda(&priv->bit_data, 0);
207 	udelay(priv->scl_irq_data);
208 	setsda(&priv->bit_data, 1);
209 
210 	complete(&priv->scl_irq_completion);
211 
212 	return IRQ_HANDLED;
213 }
214 
215 static int fops_lose_arbitration_set(void *data, u64 duration)
216 {
217 	struct i2c_gpio_private_data *priv = data;
218 
219 	if (duration > 100 * 1000)
220 		return -EINVAL;
221 
222 	priv->scl_irq_data = duration;
223 	/*
224 	 * Interrupt on falling SCL. This ensures that the master under test has
225 	 * really started the transfer. Interrupt on falling SDA did only
226 	 * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
227 	 * Note that the interrupt latency may cause the first bits to be
228 	 * transmitted correctly.
229 	 */
230 	return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
231 }
232 DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
233 
234 static irqreturn_t inject_panic_irq(int irq, void *dev_id)
235 {
236 	struct i2c_gpio_private_data *priv = dev_id;
237 
238 	udelay(priv->scl_irq_data);
239 	panic("I2C fault injector induced panic");
240 
241 	return IRQ_HANDLED;
242 }
243 
244 static int fops_inject_panic_set(void *data, u64 duration)
245 {
246 	struct i2c_gpio_private_data *priv = data;
247 
248 	if (duration > 100 * 1000)
249 		return -EINVAL;
250 
251 	priv->scl_irq_data = duration;
252 	/*
253 	 * Interrupt on falling SCL. This ensures that the master under test has
254 	 * really started the transfer.
255 	 */
256 	return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
257 }
258 DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
259 
260 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
261 {
262 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
263 
264 	/*
265 	 * If there will be a debugfs-dir per i2c adapter somewhen, put the
266 	 * 'fault-injector' dir there. Until then, we have a global dir with
267 	 * all adapters as subdirs.
268 	 */
269 	if (!i2c_gpio_debug_dir) {
270 		i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
271 		if (!i2c_gpio_debug_dir)
272 			return;
273 	}
274 
275 	priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
276 	if (!priv->debug_dir)
277 		return;
278 
279 	init_completion(&priv->scl_irq_completion);
280 
281 	debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
282 				   priv, &fops_incomplete_addr_phase);
283 	debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
284 				   priv, &fops_incomplete_write_byte);
285 	if (priv->bit_data.getscl) {
286 		debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir,
287 					   priv, &fops_inject_panic);
288 		debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir,
289 					   priv, &fops_lose_arbitration);
290 	}
291 	debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
292 	debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
293 }
294 
295 static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
296 {
297 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
298 
299 	debugfs_remove_recursive(priv->debug_dir);
300 }
301 #else
302 static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
303 static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
304 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
305 
306 static void of_i2c_gpio_get_props(struct device_node *np,
307 				  struct i2c_gpio_platform_data *pdata)
308 {
309 	u32 reg;
310 
311 	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
312 
313 	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
314 		pdata->timeout = msecs_to_jiffies(reg);
315 
316 	pdata->sda_is_open_drain =
317 		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
318 	pdata->scl_is_open_drain =
319 		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
320 	pdata->scl_is_output_only =
321 		of_property_read_bool(np, "i2c-gpio,scl-output-only");
322 }
323 
324 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
325 					   const char *con_id,
326 					   unsigned int index,
327 					   enum gpiod_flags gflags)
328 {
329 	struct gpio_desc *retdesc;
330 	int ret;
331 
332 	retdesc = devm_gpiod_get(dev, con_id, gflags);
333 	if (!IS_ERR(retdesc)) {
334 		dev_dbg(dev, "got GPIO from name %s\n", con_id);
335 		return retdesc;
336 	}
337 
338 	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
339 	if (!IS_ERR(retdesc)) {
340 		dev_dbg(dev, "got GPIO from index %u\n", index);
341 		return retdesc;
342 	}
343 
344 	ret = PTR_ERR(retdesc);
345 
346 	/* FIXME: hack in the old code, is this really necessary? */
347 	if (ret == -EINVAL)
348 		retdesc = ERR_PTR(-EPROBE_DEFER);
349 
350 	/* This happens if the GPIO driver is not yet probed, let's defer */
351 	if (ret == -ENOENT)
352 		retdesc = ERR_PTR(-EPROBE_DEFER);
353 
354 	if (ret != -EPROBE_DEFER)
355 		dev_err(dev, "error trying to get descriptor: %d\n", ret);
356 
357 	return retdesc;
358 }
359 
360 static int i2c_gpio_probe(struct platform_device *pdev)
361 {
362 	struct i2c_gpio_private_data *priv;
363 	struct i2c_gpio_platform_data *pdata;
364 	struct i2c_algo_bit_data *bit_data;
365 	struct i2c_adapter *adap;
366 	struct device *dev = &pdev->dev;
367 	struct device_node *np = dev->of_node;
368 	enum gpiod_flags gflags;
369 	int ret;
370 
371 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
372 	if (!priv)
373 		return -ENOMEM;
374 
375 	adap = &priv->adap;
376 	bit_data = &priv->bit_data;
377 	pdata = &priv->pdata;
378 
379 	if (np) {
380 		of_i2c_gpio_get_props(np, pdata);
381 	} else {
382 		/*
383 		 * If all platform data settings are zero it is OK
384 		 * to not provide any platform data from the board.
385 		 */
386 		if (dev_get_platdata(dev))
387 			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
388 	}
389 
390 	/*
391 	 * First get the GPIO pins; if it fails, we'll defer the probe.
392 	 * If the SCL/SDA lines are marked "open drain" by platform data or
393 	 * device tree then this means that something outside of our control is
394 	 * marking these lines to be handled as open drain, and we should just
395 	 * handle them as we handle any other output. Else we enforce open
396 	 * drain as this is required for an I2C bus.
397 	 */
398 	if (pdata->sda_is_open_drain)
399 		gflags = GPIOD_OUT_HIGH;
400 	else
401 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
402 	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
403 	if (IS_ERR(priv->sda))
404 		return PTR_ERR(priv->sda);
405 
406 	if (pdata->scl_is_open_drain)
407 		gflags = GPIOD_OUT_HIGH;
408 	else
409 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
410 	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
411 	if (IS_ERR(priv->scl))
412 		return PTR_ERR(priv->scl);
413 
414 	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
415 		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
416 
417 	bit_data->setsda = i2c_gpio_setsda_val;
418 	bit_data->setscl = i2c_gpio_setscl_val;
419 
420 	if (!pdata->scl_is_output_only)
421 		bit_data->getscl = i2c_gpio_getscl;
422 	bit_data->getsda = i2c_gpio_getsda;
423 
424 	if (pdata->udelay)
425 		bit_data->udelay = pdata->udelay;
426 	else if (pdata->scl_is_output_only)
427 		bit_data->udelay = 50;			/* 10 kHz */
428 	else
429 		bit_data->udelay = 5;			/* 100 kHz */
430 
431 	if (pdata->timeout)
432 		bit_data->timeout = pdata->timeout;
433 	else
434 		bit_data->timeout = HZ / 10;		/* 100 ms */
435 
436 	bit_data->data = priv;
437 
438 	adap->owner = THIS_MODULE;
439 	if (np)
440 		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
441 	else
442 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
443 
444 	adap->algo_data = bit_data;
445 	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
446 	adap->dev.parent = dev;
447 	adap->dev.of_node = np;
448 
449 	adap->nr = pdev->id;
450 	ret = i2c_bit_add_numbered_bus(adap);
451 	if (ret)
452 		return ret;
453 
454 	platform_set_drvdata(pdev, priv);
455 
456 	/*
457 	 * FIXME: using global GPIO numbers is not helpful. If/when we
458 	 * get accessors to get the actual name of the GPIO line,
459 	 * from the descriptor, then provide that instead.
460 	 */
461 	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
462 		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
463 		 pdata->scl_is_output_only
464 		 ? ", no clock stretching" : "");
465 
466 	i2c_gpio_fault_injector_init(pdev);
467 
468 	return 0;
469 }
470 
471 static int i2c_gpio_remove(struct platform_device *pdev)
472 {
473 	struct i2c_gpio_private_data *priv;
474 	struct i2c_adapter *adap;
475 
476 	i2c_gpio_fault_injector_exit(pdev);
477 
478 	priv = platform_get_drvdata(pdev);
479 	adap = &priv->adap;
480 
481 	i2c_del_adapter(adap);
482 
483 	return 0;
484 }
485 
486 #if defined(CONFIG_OF)
487 static const struct of_device_id i2c_gpio_dt_ids[] = {
488 	{ .compatible = "i2c-gpio", },
489 	{ /* sentinel */ }
490 };
491 
492 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
493 #endif
494 
495 static struct platform_driver i2c_gpio_driver = {
496 	.driver		= {
497 		.name	= "i2c-gpio",
498 		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
499 	},
500 	.probe		= i2c_gpio_probe,
501 	.remove		= i2c_gpio_remove,
502 };
503 
504 static int __init i2c_gpio_init(void)
505 {
506 	int ret;
507 
508 	ret = platform_driver_register(&i2c_gpio_driver);
509 	if (ret)
510 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
511 
512 	return ret;
513 }
514 subsys_initcall(i2c_gpio_init);
515 
516 static void __exit i2c_gpio_exit(void)
517 {
518 	platform_driver_unregister(&i2c_gpio_driver);
519 }
520 module_exit(i2c_gpio_exit);
521 
522 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
523 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
524 MODULE_LICENSE("GPL");
525 MODULE_ALIAS("platform:i2c-gpio");
526