xref: /openbmc/linux/drivers/i2c/busses/i2c-gpio.c (revision b9b77222)
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  */
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  */
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 
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 
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_adapter(&priv->adap);			\
86 	*val = get##wire(&priv->bit_data);		\
87 	i2c_unlock_adapter(&priv->adap);		\
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_adapter(&priv->adap);			\
95 	set##wire(&priv->bit_data, val);		\
96 	i2c_unlock_adapter(&priv->adap);		\
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 
104 static int fops_incomplete_transfer_set(void *data, u64 addr)
105 {
106 	struct i2c_gpio_private_data *priv = data;
107 	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
108 	int i, pattern;
109 
110 	if (addr > 0x7f)
111 		return -EINVAL;
112 
113 	/* ADDR (7 bit) + RD (1 bit) + SDA hi (1 bit) */
114 	pattern = (addr << 2) | 3;
115 
116 	i2c_lock_adapter(&priv->adap);
117 
118 	/* START condition */
119 	setsda(bit_data, 0);
120 	udelay(bit_data->udelay);
121 
122 	/* Send ADDR+RD, request ACK, don't send STOP */
123 	for (i = 8; i >= 0; i--) {
124 		setscl(bit_data, 0);
125 		udelay(bit_data->udelay / 2);
126 		setsda(bit_data, (pattern >> i) & 1);
127 		udelay((bit_data->udelay + 1) / 2);
128 		setscl(bit_data, 1);
129 		udelay(bit_data->udelay);
130 	}
131 
132 	i2c_unlock_adapter(&priv->adap);
133 
134 	return 0;
135 }
136 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n");
137 
138 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
139 {
140 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
141 
142 	/*
143 	 * If there will be a debugfs-dir per i2c adapter somewhen, put the
144 	 * 'fault-injector' dir there. Until then, we have a global dir with
145 	 * all adapters as subdirs.
146 	 */
147 	if (!i2c_gpio_debug_dir) {
148 		i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
149 		if (!i2c_gpio_debug_dir)
150 			return;
151 	}
152 
153 	priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
154 	if (!priv->debug_dir)
155 		return;
156 
157 	debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
158 	debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
159 	debugfs_create_file_unsafe("incomplete_transfer", 0200, priv->debug_dir,
160 				   priv, &fops_incomplete_transfer);
161 }
162 
163 static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
164 {
165 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
166 
167 	debugfs_remove_recursive(priv->debug_dir);
168 }
169 #else
170 static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
171 static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
172 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
173 
174 static void of_i2c_gpio_get_props(struct device_node *np,
175 				  struct i2c_gpio_platform_data *pdata)
176 {
177 	u32 reg;
178 
179 	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
180 
181 	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
182 		pdata->timeout = msecs_to_jiffies(reg);
183 
184 	pdata->sda_is_open_drain =
185 		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
186 	pdata->scl_is_open_drain =
187 		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
188 	pdata->scl_is_output_only =
189 		of_property_read_bool(np, "i2c-gpio,scl-output-only");
190 }
191 
192 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
193 					   const char *con_id,
194 					   unsigned int index,
195 					   enum gpiod_flags gflags)
196 {
197 	struct gpio_desc *retdesc;
198 	int ret;
199 
200 	retdesc = devm_gpiod_get(dev, con_id, gflags);
201 	if (!IS_ERR(retdesc)) {
202 		dev_dbg(dev, "got GPIO from name %s\n", con_id);
203 		return retdesc;
204 	}
205 
206 	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
207 	if (!IS_ERR(retdesc)) {
208 		dev_dbg(dev, "got GPIO from index %u\n", index);
209 		return retdesc;
210 	}
211 
212 	ret = PTR_ERR(retdesc);
213 
214 	/* FIXME: hack in the old code, is this really necessary? */
215 	if (ret == -EINVAL)
216 		retdesc = ERR_PTR(-EPROBE_DEFER);
217 
218 	/* This happens if the GPIO driver is not yet probed, let's defer */
219 	if (ret == -ENOENT)
220 		retdesc = ERR_PTR(-EPROBE_DEFER);
221 
222 	if (ret != -EPROBE_DEFER)
223 		dev_err(dev, "error trying to get descriptor: %d\n", ret);
224 
225 	return retdesc;
226 }
227 
228 static int i2c_gpio_probe(struct platform_device *pdev)
229 {
230 	struct i2c_gpio_private_data *priv;
231 	struct i2c_gpio_platform_data *pdata;
232 	struct i2c_algo_bit_data *bit_data;
233 	struct i2c_adapter *adap;
234 	struct device *dev = &pdev->dev;
235 	struct device_node *np = dev->of_node;
236 	enum gpiod_flags gflags;
237 	int ret;
238 
239 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
240 	if (!priv)
241 		return -ENOMEM;
242 
243 	adap = &priv->adap;
244 	bit_data = &priv->bit_data;
245 	pdata = &priv->pdata;
246 
247 	if (np) {
248 		of_i2c_gpio_get_props(np, pdata);
249 	} else {
250 		/*
251 		 * If all platform data settings are zero it is OK
252 		 * to not provide any platform data from the board.
253 		 */
254 		if (dev_get_platdata(dev))
255 			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
256 	}
257 
258 	/*
259 	 * First get the GPIO pins; if it fails, we'll defer the probe.
260 	 * If the SDA line is marked from platform data or device tree as
261 	 * "open drain" it means something outside of our control is making
262 	 * this line being handled as open drain, and we should just handle
263 	 * it as any other output. Else we enforce open drain as this is
264 	 * required for an I2C bus.
265 	 */
266 	if (pdata->sda_is_open_drain)
267 		gflags = GPIOD_OUT_HIGH;
268 	else
269 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
270 	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
271 	if (IS_ERR(priv->sda))
272 		return PTR_ERR(priv->sda);
273 
274 	/*
275 	 * If the SCL line is marked from platform data or device tree as
276 	 * "open drain" it means something outside of our control is making
277 	 * this line being handled as open drain, and we should just handle
278 	 * it as any other output. Else we enforce open drain as this is
279 	 * required for an I2C bus.
280 	 */
281 	if (pdata->scl_is_open_drain)
282 		gflags = GPIOD_OUT_HIGH;
283 	else
284 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
285 	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
286 	if (IS_ERR(priv->scl))
287 		return PTR_ERR(priv->scl);
288 
289 	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
290 		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
291 
292 	bit_data->setsda = i2c_gpio_setsda_val;
293 	bit_data->setscl = i2c_gpio_setscl_val;
294 
295 	if (!pdata->scl_is_output_only)
296 		bit_data->getscl = i2c_gpio_getscl;
297 	bit_data->getsda = i2c_gpio_getsda;
298 
299 	if (pdata->udelay)
300 		bit_data->udelay = pdata->udelay;
301 	else if (pdata->scl_is_output_only)
302 		bit_data->udelay = 50;			/* 10 kHz */
303 	else
304 		bit_data->udelay = 5;			/* 100 kHz */
305 
306 	if (pdata->timeout)
307 		bit_data->timeout = pdata->timeout;
308 	else
309 		bit_data->timeout = HZ / 10;		/* 100 ms */
310 
311 	bit_data->data = priv;
312 
313 	adap->owner = THIS_MODULE;
314 	if (np)
315 		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
316 	else
317 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
318 
319 	adap->algo_data = bit_data;
320 	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
321 	adap->dev.parent = dev;
322 	adap->dev.of_node = np;
323 
324 	adap->nr = pdev->id;
325 	ret = i2c_bit_add_numbered_bus(adap);
326 	if (ret)
327 		return ret;
328 
329 	platform_set_drvdata(pdev, priv);
330 
331 	/*
332 	 * FIXME: using global GPIO numbers is not helpful. If/when we
333 	 * get accessors to get the actual name of the GPIO line,
334 	 * from the descriptor, then provide that instead.
335 	 */
336 	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
337 		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
338 		 pdata->scl_is_output_only
339 		 ? ", no clock stretching" : "");
340 
341 	i2c_gpio_fault_injector_init(pdev);
342 
343 	return 0;
344 }
345 
346 static int i2c_gpio_remove(struct platform_device *pdev)
347 {
348 	struct i2c_gpio_private_data *priv;
349 	struct i2c_adapter *adap;
350 
351 	i2c_gpio_fault_injector_exit(pdev);
352 
353 	priv = platform_get_drvdata(pdev);
354 	adap = &priv->adap;
355 
356 	i2c_del_adapter(adap);
357 
358 	return 0;
359 }
360 
361 #if defined(CONFIG_OF)
362 static const struct of_device_id i2c_gpio_dt_ids[] = {
363 	{ .compatible = "i2c-gpio", },
364 	{ /* sentinel */ }
365 };
366 
367 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
368 #endif
369 
370 static struct platform_driver i2c_gpio_driver = {
371 	.driver		= {
372 		.name	= "i2c-gpio",
373 		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
374 	},
375 	.probe		= i2c_gpio_probe,
376 	.remove		= i2c_gpio_remove,
377 };
378 
379 static int __init i2c_gpio_init(void)
380 {
381 	int ret;
382 
383 	ret = platform_driver_register(&i2c_gpio_driver);
384 	if (ret)
385 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
386 
387 	return ret;
388 }
389 subsys_initcall(i2c_gpio_init);
390 
391 static void __exit i2c_gpio_exit(void)
392 {
393 	platform_driver_unregister(&i2c_gpio_driver);
394 }
395 module_exit(i2c_gpio_exit);
396 
397 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
398 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
399 MODULE_LICENSE("GPL");
400 MODULE_ALIAS("platform:i2c-gpio");
401