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/debugfs.h>
11 #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 #include <linux/gpio/consumer.h>
20 #include <linux/of.h>
21 
22 struct i2c_gpio_private_data {
23 	struct gpio_desc *sda;
24 	struct gpio_desc *scl;
25 	struct i2c_adapter adap;
26 	struct i2c_algo_bit_data bit_data;
27 	struct i2c_gpio_platform_data pdata;
28 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
29 	struct dentry *debug_dir;
30 #endif
31 };
32 
33 /*
34  * Toggle SDA by changing the output value of the pin. This is only
35  * valid for pins configured as open drain (i.e. setting the value
36  * high effectively turns off the output driver.)
37  */
i2c_gpio_setsda_val(void * data,int state)38 static void i2c_gpio_setsda_val(void *data, int state)
39 {
40 	struct i2c_gpio_private_data *priv = data;
41 
42 	gpiod_set_value_cansleep(priv->sda, state);
43 }
44 
45 /*
46  * Toggle SCL by changing the output value of the pin. This is used
47  * for pins that are configured as open drain and for output-only
48  * pins. The latter case will break the i2c protocol, but it will
49  * often work in practice.
50  */
i2c_gpio_setscl_val(void * data,int state)51 static void i2c_gpio_setscl_val(void *data, int state)
52 {
53 	struct i2c_gpio_private_data *priv = data;
54 
55 	gpiod_set_value_cansleep(priv->scl, state);
56 }
57 
i2c_gpio_getsda(void * data)58 static int i2c_gpio_getsda(void *data)
59 {
60 	struct i2c_gpio_private_data *priv = data;
61 
62 	return gpiod_get_value_cansleep(priv->sda);
63 }
64 
i2c_gpio_getscl(void * data)65 static int i2c_gpio_getscl(void *data)
66 {
67 	struct i2c_gpio_private_data *priv = data;
68 
69 	return gpiod_get_value_cansleep(priv->scl);
70 }
71 
72 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
73 static struct dentry *i2c_gpio_debug_dir;
74 
75 #define setsda(bd, val)	((bd)->setsda((bd)->data, val))
76 #define setscl(bd, val)	((bd)->setscl((bd)->data, val))
77 #define getsda(bd)	((bd)->getsda((bd)->data))
78 #define getscl(bd)	((bd)->getscl((bd)->data))
79 
80 #define WIRE_ATTRIBUTE(wire) \
81 static int fops_##wire##_get(void *data, u64 *val)		\
82 {								\
83 	struct i2c_gpio_private_data *priv = data;		\
84 								\
85 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
86 	*val = get##wire(&priv->bit_data);			\
87 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
88 	return 0;						\
89 }								\
90 static int fops_##wire##_set(void *data, u64 val)		\
91 {								\
92 	struct i2c_gpio_private_data *priv = data;		\
93 								\
94 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
95 	set##wire(&priv->bit_data, val);			\
96 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
97 	return 0;						\
98 }								\
99 DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
100 
101 WIRE_ATTRIBUTE(scl);
102 WIRE_ATTRIBUTE(sda);
103 
i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data * priv,u32 pattern,u8 pattern_size)104 static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
105 					u32 pattern, u8 pattern_size)
106 {
107 	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
108 	int i;
109 
110 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
111 
112 	/* START condition */
113 	setsda(bit_data, 0);
114 	udelay(bit_data->udelay);
115 
116 	/* Send pattern, request ACK, don't send STOP */
117 	for (i = pattern_size - 1; i >= 0; i--) {
118 		setscl(bit_data, 0);
119 		udelay(bit_data->udelay / 2);
120 		setsda(bit_data, (pattern >> i) & 1);
121 		udelay((bit_data->udelay + 1) / 2);
122 		setscl(bit_data, 1);
123 		udelay(bit_data->udelay);
124 	}
125 
126 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
127 }
128 
fops_incomplete_addr_phase_set(void * data,u64 addr)129 static int fops_incomplete_addr_phase_set(void *data, u64 addr)
130 {
131 	struct i2c_gpio_private_data *priv = data;
132 	u32 pattern;
133 
134 	if (addr > 0x7f)
135 		return -EINVAL;
136 
137 	/* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
138 	pattern = (addr << 2) | 3;
139 
140 	i2c_gpio_incomplete_transfer(priv, pattern, 9);
141 
142 	return 0;
143 }
144 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
145 
fops_incomplete_write_byte_set(void * data,u64 addr)146 static int fops_incomplete_write_byte_set(void *data, u64 addr)
147 {
148 	struct i2c_gpio_private_data *priv = data;
149 	u32 pattern;
150 
151 	if (addr > 0x7f)
152 		return -EINVAL;
153 
154 	/* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
155 	pattern = (addr << 2) | 1;
156 	/* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
157 	pattern = (pattern << 9) | 1;
158 
159 	i2c_gpio_incomplete_transfer(priv, pattern, 18);
160 
161 	return 0;
162 }
163 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
164 
i2c_gpio_fault_injector_init(struct platform_device * pdev)165 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
166 {
167 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
168 
169 	/*
170 	 * If there will be a debugfs-dir per i2c adapter somewhen, put the
171 	 * 'fault-injector' dir there. Until then, we have a global dir with
172 	 * all adapters as subdirs.
173 	 */
174 	if (!i2c_gpio_debug_dir) {
175 		i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
176 		if (!i2c_gpio_debug_dir)
177 			return;
178 	}
179 
180 	priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
181 	if (!priv->debug_dir)
182 		return;
183 
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);
186 	debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
187 				   priv, &fops_incomplete_addr_phase);
188 	debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
189 				   priv, &fops_incomplete_write_byte);
190 }
191 
i2c_gpio_fault_injector_exit(struct platform_device * pdev)192 static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
193 {
194 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
195 
196 	debugfs_remove_recursive(priv->debug_dir);
197 }
198 #else
i2c_gpio_fault_injector_init(struct platform_device * pdev)199 static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
i2c_gpio_fault_injector_exit(struct platform_device * pdev)200 static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
201 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
202 
of_i2c_gpio_get_props(struct device_node * np,struct i2c_gpio_platform_data * pdata)203 static void of_i2c_gpio_get_props(struct device_node *np,
204 				  struct i2c_gpio_platform_data *pdata)
205 {
206 	u32 reg;
207 
208 	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
209 
210 	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
211 		pdata->timeout = msecs_to_jiffies(reg);
212 
213 	pdata->sda_is_open_drain =
214 		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
215 	pdata->scl_is_open_drain =
216 		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
217 	pdata->scl_is_output_only =
218 		of_property_read_bool(np, "i2c-gpio,scl-output-only");
219 }
220 
i2c_gpio_get_desc(struct device * dev,const char * con_id,unsigned int index,enum gpiod_flags gflags)221 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
222 					   const char *con_id,
223 					   unsigned int index,
224 					   enum gpiod_flags gflags)
225 {
226 	struct gpio_desc *retdesc;
227 	int ret;
228 
229 	retdesc = devm_gpiod_get(dev, con_id, gflags);
230 	if (!IS_ERR(retdesc)) {
231 		dev_dbg(dev, "got GPIO from name %s\n", con_id);
232 		return retdesc;
233 	}
234 
235 	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
236 	if (!IS_ERR(retdesc)) {
237 		dev_dbg(dev, "got GPIO from index %u\n", index);
238 		return retdesc;
239 	}
240 
241 	ret = PTR_ERR(retdesc);
242 
243 	/* FIXME: hack in the old code, is this really necessary? */
244 	if (ret == -EINVAL)
245 		retdesc = ERR_PTR(-EPROBE_DEFER);
246 
247 	/* This happens if the GPIO driver is not yet probed, let's defer */
248 	if (ret == -ENOENT)
249 		retdesc = ERR_PTR(-EPROBE_DEFER);
250 
251 	if (ret != -EPROBE_DEFER)
252 		dev_err(dev, "error trying to get descriptor: %d\n", ret);
253 
254 	return retdesc;
255 }
256 
i2c_gpio_probe(struct platform_device * pdev)257 static int i2c_gpio_probe(struct platform_device *pdev)
258 {
259 	struct i2c_gpio_private_data *priv;
260 	struct i2c_gpio_platform_data *pdata;
261 	struct i2c_algo_bit_data *bit_data;
262 	struct i2c_adapter *adap;
263 	struct device *dev = &pdev->dev;
264 	struct device_node *np = dev->of_node;
265 	enum gpiod_flags gflags;
266 	int ret;
267 
268 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
269 	if (!priv)
270 		return -ENOMEM;
271 
272 	adap = &priv->adap;
273 	bit_data = &priv->bit_data;
274 	pdata = &priv->pdata;
275 
276 	if (np) {
277 		of_i2c_gpio_get_props(np, pdata);
278 	} else {
279 		/*
280 		 * If all platform data settings are zero it is OK
281 		 * to not provide any platform data from the board.
282 		 */
283 		if (dev_get_platdata(dev))
284 			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
285 	}
286 
287 	/*
288 	 * 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.
294 	 */
295 	if (pdata->sda_is_open_drain)
296 		gflags = GPIOD_OUT_HIGH;
297 	else
298 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
299 	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
300 	if (IS_ERR(priv->sda))
301 		return PTR_ERR(priv->sda);
302 
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 	if (pdata->scl_is_open_drain)
311 		gflags = GPIOD_OUT_HIGH;
312 	else
313 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
314 	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
315 	if (IS_ERR(priv->scl))
316 		return PTR_ERR(priv->scl);
317 
318 	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
319 		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
320 
321 	bit_data->setsda = i2c_gpio_setsda_val;
322 	bit_data->setscl = i2c_gpio_setscl_val;
323 
324 	if (!pdata->scl_is_output_only)
325 		bit_data->getscl = i2c_gpio_getscl;
326 	bit_data->getsda = i2c_gpio_getsda;
327 
328 	if (pdata->udelay)
329 		bit_data->udelay = pdata->udelay;
330 	else if (pdata->scl_is_output_only)
331 		bit_data->udelay = 50;			/* 10 kHz */
332 	else
333 		bit_data->udelay = 5;			/* 100 kHz */
334 
335 	if (pdata->timeout)
336 		bit_data->timeout = pdata->timeout;
337 	else
338 		bit_data->timeout = HZ / 10;		/* 100 ms */
339 
340 	bit_data->data = priv;
341 
342 	adap->owner = THIS_MODULE;
343 	if (np)
344 		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
345 	else
346 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
347 
348 	adap->algo_data = bit_data;
349 	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
350 	adap->dev.parent = dev;
351 	adap->dev.of_node = np;
352 
353 	adap->nr = pdev->id;
354 	ret = i2c_bit_add_numbered_bus(adap);
355 	if (ret)
356 		return ret;
357 
358 	platform_set_drvdata(pdev, priv);
359 
360 	/*
361 	 * FIXME: using global GPIO numbers is not helpful. If/when we
362 	 * get accessors to get the actual name of the GPIO line,
363 	 * from the descriptor, then provide that instead.
364 	 */
365 	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
366 		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
367 		 pdata->scl_is_output_only
368 		 ? ", no clock stretching" : "");
369 
370 	i2c_gpio_fault_injector_init(pdev);
371 
372 	return 0;
373 }
374 
i2c_gpio_remove(struct platform_device * pdev)375 static int i2c_gpio_remove(struct platform_device *pdev)
376 {
377 	struct i2c_gpio_private_data *priv;
378 	struct i2c_adapter *adap;
379 
380 	i2c_gpio_fault_injector_exit(pdev);
381 
382 	priv = platform_get_drvdata(pdev);
383 	adap = &priv->adap;
384 
385 	i2c_del_adapter(adap);
386 
387 	return 0;
388 }
389 
390 #if defined(CONFIG_OF)
391 static const struct of_device_id i2c_gpio_dt_ids[] = {
392 	{ .compatible = "i2c-gpio", },
393 	{ /* sentinel */ }
394 };
395 
396 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
397 #endif
398 
399 static struct platform_driver i2c_gpio_driver = {
400 	.driver		= {
401 		.name	= "i2c-gpio",
402 		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
403 	},
404 	.probe		= i2c_gpio_probe,
405 	.remove		= i2c_gpio_remove,
406 };
407 
i2c_gpio_init(void)408 static int __init i2c_gpio_init(void)
409 {
410 	int ret;
411 
412 	ret = platform_driver_register(&i2c_gpio_driver);
413 	if (ret)
414 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
415 
416 	return ret;
417 }
418 subsys_initcall(i2c_gpio_init);
419 
i2c_gpio_exit(void)420 static void __exit i2c_gpio_exit(void)
421 {
422 	platform_driver_unregister(&i2c_gpio_driver);
423 }
424 module_exit(i2c_gpio_exit);
425 
426 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
427 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
428 MODULE_LICENSE("GPL");
429 MODULE_ALIAS("platform:i2c-gpio");
430