Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[linux-btrfs-devel.git] / drivers / i2c / busses / i2c-gpio.c
bloba651779d9ff7a2faf7a41f8d437f7d97b1f66b30
1 /*
2 * Bitbanging I2C bus driver using the GPIO API
4 * Copyright (C) 2007 Atmel Corporation
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>
18 #include <asm/gpio.h>
20 /* Toggle SDA by changing the direction of the pin */
21 static void i2c_gpio_setsda_dir(void *data, int state)
23 struct i2c_gpio_platform_data *pdata = data;
25 if (state)
26 gpio_direction_input(pdata->sda_pin);
27 else
28 gpio_direction_output(pdata->sda_pin, 0);
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.)
36 static void i2c_gpio_setsda_val(void *data, int state)
38 struct i2c_gpio_platform_data *pdata = data;
40 gpio_set_value(pdata->sda_pin, state);
43 /* Toggle SCL by changing the direction of the pin. */
44 static void i2c_gpio_setscl_dir(void *data, int state)
46 struct i2c_gpio_platform_data *pdata = data;
48 if (state)
49 gpio_direction_input(pdata->scl_pin);
50 else
51 gpio_direction_output(pdata->scl_pin, 0);
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.
60 static void i2c_gpio_setscl_val(void *data, int state)
62 struct i2c_gpio_platform_data *pdata = data;
64 gpio_set_value(pdata->scl_pin, state);
67 static int i2c_gpio_getsda(void *data)
69 struct i2c_gpio_platform_data *pdata = data;
71 return gpio_get_value(pdata->sda_pin);
74 static int i2c_gpio_getscl(void *data)
76 struct i2c_gpio_platform_data *pdata = data;
78 return gpio_get_value(pdata->scl_pin);
81 static int __devinit i2c_gpio_probe(struct platform_device *pdev)
83 struct i2c_gpio_platform_data *pdata;
84 struct i2c_algo_bit_data *bit_data;
85 struct i2c_adapter *adap;
86 int ret;
88 pdata = pdev->dev.platform_data;
89 if (!pdata)
90 return -ENXIO;
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;
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;
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;
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;
123 if (!pdata->scl_is_output_only)
124 bit_data->getscl = i2c_gpio_getscl;
125 bit_data->getsda = i2c_gpio_getsda;
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 */
134 if (pdata->timeout)
135 bit_data->timeout = pdata->timeout;
136 else
137 bit_data->timeout = HZ / 10; /* 100 ms */
139 bit_data->data = pdata;
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;
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.
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;
157 platform_set_drvdata(pdev, adap);
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" : "");
164 return 0;
166 err_add_bus:
167 gpio_free(pdata->scl_pin);
168 err_request_scl:
169 gpio_free(pdata->sda_pin);
170 err_request_sda:
171 kfree(bit_data);
172 err_alloc_bit_data:
173 kfree(adap);
174 err_alloc_adap:
175 return ret;
178 static int __devexit i2c_gpio_remove(struct platform_device *pdev)
180 struct i2c_gpio_platform_data *pdata;
181 struct i2c_adapter *adap;
183 adap = platform_get_drvdata(pdev);
184 pdata = pdev->dev.platform_data;
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);
192 return 0;
195 static struct platform_driver i2c_gpio_driver = {
196 .driver = {
197 .name = "i2c-gpio",
198 .owner = THIS_MODULE,
200 .probe = i2c_gpio_probe,
201 .remove = __devexit_p(i2c_gpio_remove),
204 static int __init i2c_gpio_init(void)
206 int ret;
208 ret = platform_driver_register(&i2c_gpio_driver);
209 if (ret)
210 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
212 return ret;
214 subsys_initcall(i2c_gpio_init);
216 static void __exit i2c_gpio_exit(void)
218 platform_driver_unregister(&i2c_gpio_driver);
220 module_exit(i2c_gpio_exit);
222 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
223 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
224 MODULE_LICENSE("GPL");
225 MODULE_ALIAS("platform:i2c-gpio");