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.10.11
  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#include <linux/gpio.h>
 18#include <linux/of.h>
 19#include <linux/of_gpio.h>
 20
 21struct i2c_gpio_private_data {
 22	struct i2c_adapter adap;
 23	struct i2c_algo_bit_data bit_data;
 24	struct i2c_gpio_platform_data pdata;
 25};
 26
 27/* Toggle SDA by changing the direction of the pin */
 28static void i2c_gpio_setsda_dir(void *data, int state)
 29{
 30	struct i2c_gpio_platform_data *pdata = data;
 31
 32	if (state)
 33		gpio_direction_input(pdata->sda_pin);
 34	else
 35		gpio_direction_output(pdata->sda_pin, 0);
 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 */
 43static void i2c_gpio_setsda_val(void *data, int state)
 44{
 45	struct i2c_gpio_platform_data *pdata = data;
 46
 47	gpio_set_value(pdata->sda_pin, state);
 48}
 49
 50/* Toggle SCL by changing the direction of the pin. */
 51static void i2c_gpio_setscl_dir(void *data, int state)
 52{
 53	struct i2c_gpio_platform_data *pdata = data;
 54
 55	if (state)
 56		gpio_direction_input(pdata->scl_pin);
 57	else
 58		gpio_direction_output(pdata->scl_pin, 0);
 59}
 60
 61/*
 62 * Toggle SCL by changing the output value of the pin. This is used
 63 * for pins that are configured as open drain and for output-only
 64 * pins. The latter case will break the i2c protocol, but it will
 65 * often work in practice.
 66 */
 67static void i2c_gpio_setscl_val(void *data, int state)
 68{
 69	struct i2c_gpio_platform_data *pdata = data;
 70
 71	gpio_set_value(pdata->scl_pin, state);
 72}
 73
 74static int i2c_gpio_getsda(void *data)
 75{
 76	struct i2c_gpio_platform_data *pdata = data;
 77
 78	return gpio_get_value(pdata->sda_pin);
 79}
 80
 81static int i2c_gpio_getscl(void *data)
 82{
 83	struct i2c_gpio_platform_data *pdata = data;
 84
 85	return gpio_get_value(pdata->scl_pin);
 86}
 87
 88static int of_i2c_gpio_get_pins(struct device_node *np,
 89				unsigned int *sda_pin, unsigned int *scl_pin)
 90{
 91	if (of_gpio_count(np) < 2)
 92		return -ENODEV;
 93
 94	*sda_pin = of_get_gpio(np, 0);
 95	*scl_pin = of_get_gpio(np, 1);
 96
 97	if (*sda_pin == -EPROBE_DEFER || *scl_pin == -EPROBE_DEFER)
 98		return -EPROBE_DEFER;
 99
100	if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
101		pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
102		       np->full_name, *sda_pin, *scl_pin);
103		return -ENODEV;
104	}
105
106	return 0;
107}
108
109static void of_i2c_gpio_get_props(struct device_node *np,
110				  struct i2c_gpio_platform_data *pdata)
111{
112	u32 reg;
113
114	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
115
116	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
117		pdata->timeout = msecs_to_jiffies(reg);
118
119	pdata->sda_is_open_drain =
120		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
121	pdata->scl_is_open_drain =
122		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
123	pdata->scl_is_output_only =
124		of_property_read_bool(np, "i2c-gpio,scl-output-only");
125}
126
127static int i2c_gpio_probe(struct platform_device *pdev)
128{
129	struct i2c_gpio_private_data *priv;
130	struct i2c_gpio_platform_data *pdata;
131	struct i2c_algo_bit_data *bit_data;
132	struct i2c_adapter *adap;
133	unsigned int sda_pin, scl_pin;
134	int ret;
135
136	/* First get the GPIO pins; if it fails, we'll defer the probe. */
137	if (pdev->dev.of_node) {
138		ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
139					   &sda_pin, &scl_pin);
140		if (ret)
141			return ret;
142	} else {
143		if (!dev_get_platdata(&pdev->dev))
144			return -ENXIO;
145		pdata = dev_get_platdata(&pdev->dev);
146		sda_pin = pdata->sda_pin;
147		scl_pin = pdata->scl_pin;
148	}
149
150	ret = devm_gpio_request(&pdev->dev, sda_pin, "sda");
151	if (ret) {
152		if (ret == -EINVAL)
153			ret = -EPROBE_DEFER;	/* Try again later */
154		return ret;
155	}
156	ret = devm_gpio_request(&pdev->dev, scl_pin, "scl");
157	if (ret) {
158		if (ret == -EINVAL)
159			ret = -EPROBE_DEFER;	/* Try again later */
160		return ret;
161	}
162
163	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
164	if (!priv)
165		return -ENOMEM;
166	adap = &priv->adap;
167	bit_data = &priv->bit_data;
168	pdata = &priv->pdata;
169
170	if (pdev->dev.of_node) {
171		pdata->sda_pin = sda_pin;
172		pdata->scl_pin = scl_pin;
173		of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
174	} else {
175		memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
176	}
177
178	if (pdata->sda_is_open_drain) {
179		gpio_direction_output(pdata->sda_pin, 1);
180		bit_data->setsda = i2c_gpio_setsda_val;
181	} else {
182		gpio_direction_input(pdata->sda_pin);
183		bit_data->setsda = i2c_gpio_setsda_dir;
184	}
185
186	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
187		gpio_direction_output(pdata->scl_pin, 1);
188		bit_data->setscl = i2c_gpio_setscl_val;
189	} else {
190		gpio_direction_input(pdata->scl_pin);
191		bit_data->setscl = i2c_gpio_setscl_dir;
192	}
193
194	if (!pdata->scl_is_output_only)
195		bit_data->getscl = i2c_gpio_getscl;
196	bit_data->getsda = i2c_gpio_getsda;
197
198	if (pdata->udelay)
199		bit_data->udelay = pdata->udelay;
200	else if (pdata->scl_is_output_only)
201		bit_data->udelay = 50;			/* 10 kHz */
202	else
203		bit_data->udelay = 5;			/* 100 kHz */
204
205	if (pdata->timeout)
206		bit_data->timeout = pdata->timeout;
207	else
208		bit_data->timeout = HZ / 10;		/* 100 ms */
209
210	bit_data->data = pdata;
211
212	adap->owner = THIS_MODULE;
213	if (pdev->dev.of_node)
214		strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
215	else
216		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
217
218	adap->algo_data = bit_data;
219	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
220	adap->dev.parent = &pdev->dev;
221	adap->dev.of_node = pdev->dev.of_node;
222
223	adap->nr = pdev->id;
 
 
 
 
 
224	ret = i2c_bit_add_numbered_bus(adap);
225	if (ret)
226		return ret;
227
228	platform_set_drvdata(pdev, priv);
229
230	dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
231		 pdata->sda_pin, pdata->scl_pin,
232		 pdata->scl_is_output_only
233		 ? ", no clock stretching" : "");
234
235	return 0;
 
 
 
 
 
 
 
 
 
 
 
236}
237
238static int i2c_gpio_remove(struct platform_device *pdev)
239{
240	struct i2c_gpio_private_data *priv;
241	struct i2c_adapter *adap;
242
243	priv = platform_get_drvdata(pdev);
244	adap = &priv->adap;
245
246	i2c_del_adapter(adap);
 
 
 
 
247
248	return 0;
249}
250
251#if defined(CONFIG_OF)
252static const struct of_device_id i2c_gpio_dt_ids[] = {
253	{ .compatible = "i2c-gpio", },
254	{ /* sentinel */ }
255};
256
257MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
258#endif
259
260static struct platform_driver i2c_gpio_driver = {
261	.driver		= {
262		.name	= "i2c-gpio",
263		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
264	},
265	.probe		= i2c_gpio_probe,
266	.remove		= i2c_gpio_remove,
267};
268
269static int __init i2c_gpio_init(void)
270{
271	int ret;
272
273	ret = platform_driver_register(&i2c_gpio_driver);
274	if (ret)
275		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
276
277	return ret;
278}
279subsys_initcall(i2c_gpio_init);
280
281static void __exit i2c_gpio_exit(void)
282{
283	platform_driver_unregister(&i2c_gpio_driver);
284}
285module_exit(i2c_gpio_exit);
286
287MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
288MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
289MODULE_LICENSE("GPL");
290MODULE_ALIAS("platform:i2c-gpio");