Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[linux-2.6/lfs.git] / drivers / i2c / busses / i2c-gpio.c
blob7c1b762aa681fc727312c0e0c1db08dbff0c2c04
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/platform_device.h>
17 #include <asm/gpio.h>
19 /* Toggle SDA by changing the direction of the pin */
20 static void i2c_gpio_setsda_dir(void *data, int state)
22 struct i2c_gpio_platform_data *pdata = data;
24 if (state)
25 gpio_direction_input(pdata->sda_pin);
26 else
27 gpio_direction_output(pdata->sda_pin, 0);
31 * Toggle SDA by changing the output value of the pin. This is only
32 * valid for pins configured as open drain (i.e. setting the value
33 * high effectively turns off the output driver.)
35 static void i2c_gpio_setsda_val(void *data, int state)
37 struct i2c_gpio_platform_data *pdata = data;
39 gpio_set_value(pdata->sda_pin, state);
42 /* Toggle SCL by changing the direction of the pin. */
43 static void i2c_gpio_setscl_dir(void *data, int state)
45 struct i2c_gpio_platform_data *pdata = data;
47 if (state)
48 gpio_direction_input(pdata->scl_pin);
49 else
50 gpio_direction_output(pdata->scl_pin, 0);
54 * Toggle SCL by changing the output value of the pin. This is used
55 * for pins that are configured as open drain and for output-only
56 * pins. The latter case will break the i2c protocol, but it will
57 * often work in practice.
59 static void i2c_gpio_setscl_val(void *data, int state)
61 struct i2c_gpio_platform_data *pdata = data;
63 gpio_set_value(pdata->scl_pin, state);
66 static int i2c_gpio_getsda(void *data)
68 struct i2c_gpio_platform_data *pdata = data;
70 return gpio_get_value(pdata->sda_pin);
73 static int i2c_gpio_getscl(void *data)
75 struct i2c_gpio_platform_data *pdata = data;
77 return gpio_get_value(pdata->scl_pin);
80 static int __init i2c_gpio_probe(struct platform_device *pdev)
82 struct i2c_gpio_platform_data *pdata;
83 struct i2c_algo_bit_data *bit_data;
84 struct i2c_adapter *adap;
85 int ret;
87 pdata = pdev->dev.platform_data;
88 if (!pdata)
89 return -ENXIO;
91 ret = -ENOMEM;
92 adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
93 if (!adap)
94 goto err_alloc_adap;
95 bit_data = kzalloc(sizeof(struct i2c_algo_bit_data), GFP_KERNEL);
96 if (!bit_data)
97 goto err_alloc_bit_data;
99 ret = gpio_request(pdata->sda_pin, "sda");
100 if (ret)
101 goto err_request_sda;
102 ret = gpio_request(pdata->scl_pin, "scl");
103 if (ret)
104 goto err_request_scl;
106 if (pdata->sda_is_open_drain) {
107 gpio_direction_output(pdata->sda_pin, 1);
108 bit_data->setsda = i2c_gpio_setsda_val;
109 } else {
110 gpio_direction_input(pdata->sda_pin);
111 bit_data->setsda = i2c_gpio_setsda_dir;
114 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
115 gpio_direction_output(pdata->scl_pin, 1);
116 bit_data->setscl = i2c_gpio_setscl_val;
117 } else {
118 gpio_direction_input(pdata->scl_pin);
119 bit_data->setscl = i2c_gpio_setscl_dir;
122 if (!pdata->scl_is_output_only)
123 bit_data->getscl = i2c_gpio_getscl;
124 bit_data->getsda = i2c_gpio_getsda;
126 if (pdata->udelay)
127 bit_data->udelay = pdata->udelay;
128 else if (pdata->scl_is_output_only)
129 bit_data->udelay = 50; /* 10 kHz */
130 else
131 bit_data->udelay = 5; /* 100 kHz */
133 if (pdata->timeout)
134 bit_data->timeout = pdata->timeout;
135 else
136 bit_data->timeout = HZ / 10; /* 100 ms */
138 bit_data->data = pdata;
140 adap->owner = THIS_MODULE;
141 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
142 adap->algo_data = bit_data;
143 adap->class = I2C_CLASS_HWMON;
144 adap->dev.parent = &pdev->dev;
147 * If "dev->id" is negative we consider it as zero.
148 * The reason to do so is to avoid sysfs names that only make
149 * sense when there are multiple adapters.
151 adap->nr = (pdev->id != -1) ? pdev->id : 0;
152 ret = i2c_bit_add_numbered_bus(adap);
153 if (ret)
154 goto err_add_bus;
156 platform_set_drvdata(pdev, adap);
158 dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
159 pdata->sda_pin, pdata->scl_pin,
160 pdata->scl_is_output_only
161 ? ", no clock stretching" : "");
163 return 0;
165 err_add_bus:
166 gpio_free(pdata->scl_pin);
167 err_request_scl:
168 gpio_free(pdata->sda_pin);
169 err_request_sda:
170 kfree(bit_data);
171 err_alloc_bit_data:
172 kfree(adap);
173 err_alloc_adap:
174 return ret;
177 static int __exit i2c_gpio_remove(struct platform_device *pdev)
179 struct i2c_gpio_platform_data *pdata;
180 struct i2c_adapter *adap;
182 adap = platform_get_drvdata(pdev);
183 pdata = pdev->dev.platform_data;
185 i2c_del_adapter(adap);
186 gpio_free(pdata->scl_pin);
187 gpio_free(pdata->sda_pin);
188 kfree(adap->algo_data);
189 kfree(adap);
191 return 0;
194 static struct platform_driver i2c_gpio_driver = {
195 .driver = {
196 .name = "i2c-gpio",
197 .owner = THIS_MODULE,
199 .remove = __exit_p(i2c_gpio_remove),
202 static int __init i2c_gpio_init(void)
204 int ret;
206 ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe);
207 if (ret)
208 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
210 return ret;
212 module_init(i2c_gpio_init);
214 static void __exit i2c_gpio_exit(void)
216 platform_driver_unregister(&i2c_gpio_driver);
218 module_exit(i2c_gpio_exit);
220 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
221 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
222 MODULE_LICENSE("GPL");
223 MODULE_ALIAS("platform:i2c-gpio");