blob: c0330a41db039d15ddfece5a81a58abf50d6c1b9 [file] [log] [blame]
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +020016#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +080017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
19#include <linux/of_i2c.h>
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +020020
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +080021struct 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};
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +020026
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
Atsushi Nemoto4d6ceed2007-07-12 14:12:30 +020074static int i2c_gpio_getsda(void *data)
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +020075{
76 struct i2c_gpio_platform_data *pdata = data;
77
78 return gpio_get_value(pdata->sda_pin);
79}
80
Atsushi Nemoto4d6ceed2007-07-12 14:12:30 +020081static int i2c_gpio_getscl(void *data)
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +020082{
83 struct i2c_gpio_platform_data *pdata = data;
84
85 return gpio_get_value(pdata->scl_pin);
86}
87
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +080088static int __devinit of_i2c_gpio_probe(struct device_node *np,
89 struct i2c_gpio_platform_data *pdata)
90{
91 u32 reg;
92
93 if (of_gpio_count(np) < 2)
94 return -ENODEV;
95
96 pdata->sda_pin = of_get_gpio(np, 0);
97 pdata->scl_pin = of_get_gpio(np, 1);
98
99 if (!gpio_is_valid(pdata->sda_pin) || !gpio_is_valid(pdata->scl_pin)) {
100 pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
101 np->full_name, pdata->sda_pin, pdata->scl_pin);
102 return -ENODEV;
103 }
104
105 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
106
107 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
108 pdata->timeout = msecs_to_jiffies(reg);
109
110 pdata->sda_is_open_drain =
111 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
112 pdata->scl_is_open_drain =
113 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
114 pdata->scl_is_output_only =
115 of_property_read_bool(np, "i2c-gpio,scl-output-only");
116
117 return 0;
118}
119
Ben Dooks1efe7c52008-07-28 12:04:09 +0100120static int __devinit i2c_gpio_probe(struct platform_device *pdev)
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200121{
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800122 struct i2c_gpio_private_data *priv;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200123 struct i2c_gpio_platform_data *pdata;
124 struct i2c_algo_bit_data *bit_data;
125 struct i2c_adapter *adap;
126 int ret;
127
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800128 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
129 if (!priv)
130 return -ENOMEM;
131 adap = &priv->adap;
132 bit_data = &priv->bit_data;
133 pdata = &priv->pdata;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200134
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800135 if (pdev->dev.of_node) {
136 ret = of_i2c_gpio_probe(pdev->dev.of_node, pdata);
137 if (ret)
138 return ret;
139 } else {
140 if (!pdev->dev.platform_data)
141 return -ENXIO;
142 memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
143 }
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200144
145 ret = gpio_request(pdata->sda_pin, "sda");
146 if (ret)
147 goto err_request_sda;
148 ret = gpio_request(pdata->scl_pin, "scl");
149 if (ret)
150 goto err_request_scl;
151
152 if (pdata->sda_is_open_drain) {
153 gpio_direction_output(pdata->sda_pin, 1);
154 bit_data->setsda = i2c_gpio_setsda_val;
155 } else {
156 gpio_direction_input(pdata->sda_pin);
157 bit_data->setsda = i2c_gpio_setsda_dir;
158 }
159
160 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
161 gpio_direction_output(pdata->scl_pin, 1);
162 bit_data->setscl = i2c_gpio_setscl_val;
163 } else {
164 gpio_direction_input(pdata->scl_pin);
165 bit_data->setscl = i2c_gpio_setscl_dir;
166 }
167
168 if (!pdata->scl_is_output_only)
169 bit_data->getscl = i2c_gpio_getscl;
170 bit_data->getsda = i2c_gpio_getsda;
171
172 if (pdata->udelay)
173 bit_data->udelay = pdata->udelay;
174 else if (pdata->scl_is_output_only)
175 bit_data->udelay = 50; /* 10 kHz */
176 else
177 bit_data->udelay = 5; /* 100 kHz */
178
179 if (pdata->timeout)
180 bit_data->timeout = pdata->timeout;
181 else
182 bit_data->timeout = HZ / 10; /* 100 ms */
183
184 bit_data->data = pdata;
185
186 adap->owner = THIS_MODULE;
187 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
188 adap->algo_data = bit_data;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200189 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200190 adap->dev.parent = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800191 adap->dev.of_node = pdev->dev.of_node;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200192
Atsushi Nemoto7e69c3a2007-07-12 14:12:30 +0200193 /*
194 * If "dev->id" is negative we consider it as zero.
195 * The reason to do so is to avoid sysfs names that only make
196 * sense when there are multiple adapters.
197 */
David Brownell9a3180e2007-09-09 22:29:13 +0200198 adap->nr = (pdev->id != -1) ? pdev->id : 0;
Atsushi Nemoto7e69c3a2007-07-12 14:12:30 +0200199 ret = i2c_bit_add_numbered_bus(adap);
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200200 if (ret)
201 goto err_add_bus;
202
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800203 of_i2c_register_devices(adap);
204
205 platform_set_drvdata(pdev, priv);
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200206
207 dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
208 pdata->sda_pin, pdata->scl_pin,
209 pdata->scl_is_output_only
210 ? ", no clock stretching" : "");
211
212 return 0;
213
214err_add_bus:
215 gpio_free(pdata->scl_pin);
216err_request_scl:
217 gpio_free(pdata->sda_pin);
218err_request_sda:
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200219 return ret;
220}
221
Ben Dooks1efe7c52008-07-28 12:04:09 +0100222static int __devexit i2c_gpio_remove(struct platform_device *pdev)
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200223{
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800224 struct i2c_gpio_private_data *priv;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200225 struct i2c_gpio_platform_data *pdata;
226 struct i2c_adapter *adap;
227
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800228 priv = platform_get_drvdata(pdev);
229 adap = &priv->adap;
230 pdata = &priv->pdata;
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200231
232 i2c_del_adapter(adap);
233 gpio_free(pdata->scl_pin);
234 gpio_free(pdata->sda_pin);
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200235
236 return 0;
237}
238
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800239#if defined(CONFIG_OF)
240static const struct of_device_id i2c_gpio_dt_ids[] = {
241 { .compatible = "i2c-gpio", },
242 { /* sentinel */ }
243};
244
245MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
246#endif
247
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200248static struct platform_driver i2c_gpio_driver = {
249 .driver = {
250 .name = "i2c-gpio",
251 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARD8ffaa0f2012-02-05 18:22:34 +0800252 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200253 },
Ben Dooks1efe7c52008-07-28 12:04:09 +0100254 .probe = i2c_gpio_probe,
255 .remove = __devexit_p(i2c_gpio_remove),
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200256};
257
258static int __init i2c_gpio_init(void)
259{
260 int ret;
261
Ben Dooks1efe7c52008-07-28 12:04:09 +0100262 ret = platform_driver_register(&i2c_gpio_driver);
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200263 if (ret)
264 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
265
266 return ret;
267}
Marek Szyprowskib8680782010-05-21 18:40:58 +0200268subsys_initcall(i2c_gpio_init);
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200269
270static void __exit i2c_gpio_exit(void)
271{
272 platform_driver_unregister(&i2c_gpio_driver);
273}
274module_exit(i2c_gpio_exit);
275
Jean Delvaree05503e2011-05-18 16:49:24 +0200276MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen1c23af92007-05-01 23:26:34 +0200277MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
278MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200279MODULE_ALIAS("platform:i2c-gpio");