diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/i2c/busses/Kconfig | 8 | ||||
-rw-r--r-- | drivers/i2c/busses/Makefile | 1 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-gpio.c | 215 |
3 files changed, 224 insertions, 0 deletions
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index 8ccf6e4e91d0..33a8a67f161b 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig | |||
@@ -117,6 +117,14 @@ config I2C_ELEKTOR | |||
117 | This support is also available as a module. If so, the module | 117 | This support is also available as a module. If so, the module |
118 | will be called i2c-elektor. | 118 | will be called i2c-elektor. |
119 | 119 | ||
120 | config I2C_GPIO | ||
121 | tristate "GPIO-based bitbanging I2C" | ||
122 | depends on GENERIC_GPIO | ||
123 | select I2C_ALGOBIT | ||
124 | help | ||
125 | This is a very simple bitbanging I2C driver utilizing the | ||
126 | arch-neutral GPIO API to control the SCL and SDA lines. | ||
127 | |||
120 | config I2C_HYDRA | 128 | config I2C_HYDRA |
121 | tristate "CHRP Apple Hydra Mac I/O I2C interface" | 129 | tristate "CHRP Apple Hydra Mac I/O I2C interface" |
122 | depends on PCI && PPC_CHRP && EXPERIMENTAL | 130 | depends on PCI && PPC_CHRP && EXPERIMENTAL |
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index e51493cfbee2..5689165f6d6a 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile | |||
@@ -12,6 +12,7 @@ obj-$(CONFIG_I2C_AT91) += i2c-at91.o | |||
12 | obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o | 12 | obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o |
13 | obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o | 13 | obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o |
14 | obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o | 14 | obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o |
15 | obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o | ||
15 | obj-$(CONFIG_I2C_HYDRA) += i2c-hydra.o | 16 | obj-$(CONFIG_I2C_HYDRA) += i2c-hydra.o |
16 | obj-$(CONFIG_I2C_I801) += i2c-i801.o | 17 | obj-$(CONFIG_I2C_I801) += i2c-i801.o |
17 | obj-$(CONFIG_I2C_I810) += i2c-i810.o | 18 | obj-$(CONFIG_I2C_I810) += i2c-i810.o |
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c new file mode 100644 index 000000000000..a7dd54654a9a --- /dev/null +++ b/drivers/i2c/busses/i2c-gpio.c | |||
@@ -0,0 +1,215 @@ | |||
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/platform_device.h> | ||
16 | |||
17 | #include <asm/gpio.h> | ||
18 | |||
19 | /* Toggle SDA by changing the direction of the pin */ | ||
20 | static void i2c_gpio_setsda_dir(void *data, int state) | ||
21 | { | ||
22 | struct i2c_gpio_platform_data *pdata = data; | ||
23 | |||
24 | if (state) | ||
25 | gpio_direction_input(pdata->sda_pin); | ||
26 | else | ||
27 | gpio_direction_output(pdata->sda_pin, 0); | ||
28 | } | ||
29 | |||
30 | /* | ||
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.) | ||
34 | */ | ||
35 | static void i2c_gpio_setsda_val(void *data, int state) | ||
36 | { | ||
37 | struct i2c_gpio_platform_data *pdata = data; | ||
38 | |||
39 | gpio_set_value(pdata->sda_pin, state); | ||
40 | } | ||
41 | |||
42 | /* Toggle SCL by changing the direction of the pin. */ | ||
43 | static void i2c_gpio_setscl_dir(void *data, int state) | ||
44 | { | ||
45 | struct i2c_gpio_platform_data *pdata = data; | ||
46 | |||
47 | if (state) | ||
48 | gpio_direction_input(pdata->scl_pin); | ||
49 | else | ||
50 | gpio_direction_output(pdata->scl_pin, 0); | ||
51 | } | ||
52 | |||
53 | /* | ||
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. | ||
58 | */ | ||
59 | static void i2c_gpio_setscl_val(void *data, int state) | ||
60 | { | ||
61 | struct i2c_gpio_platform_data *pdata = data; | ||
62 | |||
63 | gpio_set_value(pdata->scl_pin, state); | ||
64 | } | ||
65 | |||
66 | int i2c_gpio_getsda(void *data) | ||
67 | { | ||
68 | struct i2c_gpio_platform_data *pdata = data; | ||
69 | |||
70 | return gpio_get_value(pdata->sda_pin); | ||
71 | } | ||
72 | |||
73 | int i2c_gpio_getscl(void *data) | ||
74 | { | ||
75 | struct i2c_gpio_platform_data *pdata = data; | ||
76 | |||
77 | return gpio_get_value(pdata->scl_pin); | ||
78 | } | ||
79 | |||
80 | static int __init i2c_gpio_probe(struct platform_device *pdev) | ||
81 | { | ||
82 | struct i2c_gpio_platform_data *pdata; | ||
83 | struct i2c_algo_bit_data *bit_data; | ||
84 | struct i2c_adapter *adap; | ||
85 | int ret; | ||
86 | |||
87 | pdata = pdev->dev.platform_data; | ||
88 | if (!pdata) | ||
89 | return -ENXIO; | ||
90 | |||
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; | ||
98 | |||
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; | ||
105 | |||
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; | ||
112 | } | ||
113 | |||
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; | ||
120 | } | ||
121 | |||
122 | if (!pdata->scl_is_output_only) | ||
123 | bit_data->getscl = i2c_gpio_getscl; | ||
124 | bit_data->getsda = i2c_gpio_getsda; | ||
125 | |||
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 */ | ||
132 | |||
133 | if (pdata->timeout) | ||
134 | bit_data->timeout = pdata->timeout; | ||
135 | else | ||
136 | bit_data->timeout = HZ / 10; /* 100 ms */ | ||
137 | |||
138 | bit_data->data = pdata; | ||
139 | |||
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->dev.parent = &pdev->dev; | ||
144 | |||
145 | ret = i2c_bit_add_bus(adap); | ||
146 | if (ret) | ||
147 | goto err_add_bus; | ||
148 | |||
149 | platform_set_drvdata(pdev, adap); | ||
150 | |||
151 | dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n", | ||
152 | pdata->sda_pin, pdata->scl_pin, | ||
153 | pdata->scl_is_output_only | ||
154 | ? ", no clock stretching" : ""); | ||
155 | |||
156 | return 0; | ||
157 | |||
158 | err_add_bus: | ||
159 | gpio_free(pdata->scl_pin); | ||
160 | err_request_scl: | ||
161 | gpio_free(pdata->sda_pin); | ||
162 | err_request_sda: | ||
163 | kfree(bit_data); | ||
164 | err_alloc_bit_data: | ||
165 | kfree(adap); | ||
166 | err_alloc_adap: | ||
167 | return ret; | ||
168 | } | ||
169 | |||
170 | static int __exit i2c_gpio_remove(struct platform_device *pdev) | ||
171 | { | ||
172 | struct i2c_gpio_platform_data *pdata; | ||
173 | struct i2c_adapter *adap; | ||
174 | |||
175 | adap = platform_get_drvdata(pdev); | ||
176 | pdata = pdev->dev.platform_data; | ||
177 | |||
178 | i2c_del_adapter(adap); | ||
179 | gpio_free(pdata->scl_pin); | ||
180 | gpio_free(pdata->sda_pin); | ||
181 | kfree(adap->algo_data); | ||
182 | kfree(adap); | ||
183 | |||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | static struct platform_driver i2c_gpio_driver = { | ||
188 | .driver = { | ||
189 | .name = "i2c-gpio", | ||
190 | .owner = THIS_MODULE, | ||
191 | }, | ||
192 | .remove = __exit_p(i2c_gpio_remove), | ||
193 | }; | ||
194 | |||
195 | static int __init i2c_gpio_init(void) | ||
196 | { | ||
197 | int ret; | ||
198 | |||
199 | ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe); | ||
200 | if (ret) | ||
201 | printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); | ||
202 | |||
203 | return ret; | ||
204 | } | ||
205 | module_init(i2c_gpio_init); | ||
206 | |||
207 | static void __exit i2c_gpio_exit(void) | ||
208 | { | ||
209 | platform_driver_unregister(&i2c_gpio_driver); | ||
210 | } | ||
211 | module_exit(i2c_gpio_exit); | ||
212 | |||
213 | MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>"); | ||
214 | MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); | ||
215 | MODULE_LICENSE("GPL"); | ||