Linux Audio

Check our new training course

Loading...
v3.1
  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/i2c.h>
 11#include <linux/i2c-algo-bit.h>
 12#include <linux/i2c-gpio.h>
 13#include <linux/init.h>
 14#include <linux/module.h>
 15#include <linux/slab.h>
 16#include <linux/platform_device.h>
 
 
 17
 18#include <asm/gpio.h>
 19
 20/* Toggle SDA by changing the direction of the pin */
 21static void i2c_gpio_setsda_dir(void *data, int state)
 22{
 23	struct i2c_gpio_platform_data *pdata = data;
 24
 25	if (state)
 26		gpio_direction_input(pdata->sda_pin);
 27	else
 28		gpio_direction_output(pdata->sda_pin, 0);
 29}
 30
 31/*
 32 * Toggle SDA by changing the output value of the pin. This is only
 33 * valid for pins configured as open drain (i.e. setting the value
 34 * high effectively turns off the output driver.)
 35 */
 36static void i2c_gpio_setsda_val(void *data, int state)
 37{
 38	struct i2c_gpio_platform_data *pdata = data;
 39
 40	gpio_set_value(pdata->sda_pin, state);
 41}
 42
 43/* Toggle SCL by changing the direction of the pin. */
 44static void i2c_gpio_setscl_dir(void *data, int state)
 45{
 46	struct i2c_gpio_platform_data *pdata = data;
 47
 48	if (state)
 49		gpio_direction_input(pdata->scl_pin);
 50	else
 51		gpio_direction_output(pdata->scl_pin, 0);
 52}
 53
 54/*
 55 * Toggle SCL by changing the output value of the pin. This is used
 56 * for pins that are configured as open drain and for output-only
 57 * pins. The latter case will break the i2c protocol, but it will
 58 * often work in practice.
 59 */
 60static void i2c_gpio_setscl_val(void *data, int state)
 61{
 62	struct i2c_gpio_platform_data *pdata = data;
 63
 64	gpio_set_value(pdata->scl_pin, state);
 65}
 66
 67static int i2c_gpio_getsda(void *data)
 68{
 69	struct i2c_gpio_platform_data *pdata = data;
 70
 71	return gpio_get_value(pdata->sda_pin);
 72}
 73
 74static int i2c_gpio_getscl(void *data)
 75{
 76	struct i2c_gpio_platform_data *pdata = data;
 77
 78	return gpio_get_value(pdata->scl_pin);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79}
 
 80
 81static int __devinit i2c_gpio_probe(struct platform_device *pdev)
 82{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 83	struct i2c_gpio_platform_data *pdata;
 84	struct i2c_algo_bit_data *bit_data;
 85	struct i2c_adapter *adap;
 
 
 
 86	int ret;
 87
 88	pdata = pdev->dev.platform_data;
 89	if (!pdata)
 90		return -ENXIO;
 91
 92	ret = -ENOMEM;
 93	adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
 94	if (!adap)
 95		goto err_alloc_adap;
 96	bit_data = kzalloc(sizeof(struct i2c_algo_bit_data), GFP_KERNEL);
 97	if (!bit_data)
 98		goto err_alloc_bit_data;
 99
100	ret = gpio_request(pdata->sda_pin, "sda");
101	if (ret)
102		goto err_request_sda;
103	ret = gpio_request(pdata->scl_pin, "scl");
104	if (ret)
105		goto err_request_scl;
106
107	if (pdata->sda_is_open_drain) {
108		gpio_direction_output(pdata->sda_pin, 1);
109		bit_data->setsda = i2c_gpio_setsda_val;
110	} else {
111		gpio_direction_input(pdata->sda_pin);
112		bit_data->setsda = i2c_gpio_setsda_dir;
 
 
 
 
113	}
114
115	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
116		gpio_direction_output(pdata->scl_pin, 1);
117		bit_data->setscl = i2c_gpio_setscl_val;
118	} else {
119		gpio_direction_input(pdata->scl_pin);
120		bit_data->setscl = i2c_gpio_setscl_dir;
121	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
123	if (!pdata->scl_is_output_only)
124		bit_data->getscl = i2c_gpio_getscl;
125	bit_data->getsda = i2c_gpio_getsda;
126
127	if (pdata->udelay)
128		bit_data->udelay = pdata->udelay;
129	else if (pdata->scl_is_output_only)
130		bit_data->udelay = 50;			/* 10 kHz */
131	else
132		bit_data->udelay = 5;			/* 100 kHz */
133
134	if (pdata->timeout)
135		bit_data->timeout = pdata->timeout;
136	else
137		bit_data->timeout = HZ / 10;		/* 100 ms */
138
139	bit_data->data = pdata;
140
141	adap->owner = THIS_MODULE;
142	snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
 
 
 
 
143	adap->algo_data = bit_data;
144	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
145	adap->dev.parent = &pdev->dev;
 
146
147	/*
148	 * If "dev->id" is negative we consider it as zero.
149	 * The reason to do so is to avoid sysfs names that only make
150	 * sense when there are multiple adapters.
151	 */
152	adap->nr = (pdev->id != -1) ? pdev->id : 0;
153	ret = i2c_bit_add_numbered_bus(adap);
154	if (ret)
155		goto err_add_bus;
156
157	platform_set_drvdata(pdev, adap);
158
159	dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
160		 pdata->sda_pin, pdata->scl_pin,
 
 
 
 
 
161		 pdata->scl_is_output_only
162		 ? ", no clock stretching" : "");
163
164	return 0;
165
166err_add_bus:
167	gpio_free(pdata->scl_pin);
168err_request_scl:
169	gpio_free(pdata->sda_pin);
170err_request_sda:
171	kfree(bit_data);
172err_alloc_bit_data:
173	kfree(adap);
174err_alloc_adap:
175	return ret;
176}
177
178static int __devexit i2c_gpio_remove(struct platform_device *pdev)
179{
180	struct i2c_gpio_platform_data *pdata;
181	struct i2c_adapter *adap;
182
183	adap = platform_get_drvdata(pdev);
184	pdata = pdev->dev.platform_data;
 
 
185
186	i2c_del_adapter(adap);
187	gpio_free(pdata->scl_pin);
188	gpio_free(pdata->sda_pin);
189	kfree(adap->algo_data);
190	kfree(adap);
191
192	return 0;
193}
194
 
 
 
 
 
 
 
 
 
195static struct platform_driver i2c_gpio_driver = {
196	.driver		= {
197		.name	= "i2c-gpio",
198		.owner	= THIS_MODULE,
199	},
200	.probe		= i2c_gpio_probe,
201	.remove		= __devexit_p(i2c_gpio_remove),
202};
203
204static int __init i2c_gpio_init(void)
205{
206	int ret;
207
208	ret = platform_driver_register(&i2c_gpio_driver);
209	if (ret)
210		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
211
212	return ret;
213}
214subsys_initcall(i2c_gpio_init);
215
216static void __exit i2c_gpio_exit(void)
217{
218	platform_driver_unregister(&i2c_gpio_driver);
219}
220module_exit(i2c_gpio_exit);
221
222MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
223MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
224MODULE_LICENSE("GPL");
225MODULE_ALIAS("platform:i2c-gpio");
v4.17
  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/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
 22struct 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 */
 38static 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 */
 51static 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
 58static 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
 65static 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
 73static 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) \
 81static 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}							\
 90static 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}							\
 99DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
100
101WIRE_ATTRIBUTE(scl);
102WIRE_ATTRIBUTE(sda);
103
104static 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}
136DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n");
137
138static 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
163static 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
170static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
171static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
172#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
173
174static 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
192static 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
228static 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_LOW;
283	else
284		gflags = GPIOD_OUT_LOW_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
346static 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)
362static const struct of_device_id i2c_gpio_dt_ids[] = {
363	{ .compatible = "i2c-gpio", },
364	{ /* sentinel */ }
365};
366
367MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
368#endif
369
370static 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
379static 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}
389subsys_initcall(i2c_gpio_init);
390
391static void __exit i2c_gpio_exit(void)
392{
393	platform_driver_unregister(&i2c_gpio_driver);
394}
395module_exit(i2c_gpio_exit);
396
397MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
398MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
399MODULE_LICENSE("GPL");
400MODULE_ALIAS("platform:i2c-gpio");