staging/ion: sync ion.h with include/linux/ion.h
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / i2c / busses / i2c-gpio.c
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_gpio.h>
19 #include <linux/of_i2c.h>
20
21 struct 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 */
28 static 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  */
43 static 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. */
51 static 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  */
67 static 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
74 static 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
81 static 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
88 static 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 (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
98                 pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
99                        np->full_name, *sda_pin, *scl_pin);
100                 return -ENODEV;
101         }
102
103         return 0;
104 }
105
106 static void of_i2c_gpio_get_props(struct device_node *np,
107                                   struct i2c_gpio_platform_data *pdata)
108 {
109         u32 reg;
110
111         of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
112
113         if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
114                 pdata->timeout = msecs_to_jiffies(reg);
115
116         pdata->sda_is_open_drain =
117                 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
118         pdata->scl_is_open_drain =
119                 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
120         pdata->scl_is_output_only =
121                 of_property_read_bool(np, "i2c-gpio,scl-output-only");
122 }
123
124 static int i2c_gpio_probe(struct platform_device *pdev)
125 {
126         struct i2c_gpio_private_data *priv;
127         struct i2c_gpio_platform_data *pdata;
128         struct i2c_algo_bit_data *bit_data;
129         struct i2c_adapter *adap;
130         unsigned int sda_pin, scl_pin;
131         int ret;
132
133
134         /* First get the GPIO pins; if it fails, we'll defer the probe. */
135         if (pdev->dev.of_node) {
136                 ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
137                                            &sda_pin, &scl_pin);
138                 if (ret)
139                         return ret;
140         } else {
141                 if (!pdev->dev.platform_data)
142                         return -ENXIO;
143                 pdata = pdev->dev.platform_data;
144                 sda_pin = pdata->sda_pin;
145                 scl_pin = pdata->scl_pin;
146         }
147
148         ret = gpio_request(sda_pin, "sda");
149         if (ret) {
150                 if (ret == -EINVAL)
151                         ret = -EPROBE_DEFER;    /* Try again later */
152                 goto err_request_sda;
153         }
154         ret = gpio_request(scl_pin, "scl");
155         if (ret) {
156                 if (ret == -EINVAL)
157                         ret = -EPROBE_DEFER;    /* Try again later */
158                 goto err_request_scl;
159         }
160
161         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
162         if (!priv) {
163                 ret = -ENOMEM;
164                 goto err_add_bus;
165         }
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, pdev->dev.platform_data, 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 #ifdef CONFIG_OF
219         if (pdev->dev.of_node) {
220                 pdev->id = of_alias_get_id(pdev->dev.of_node, "i2c_gpio");
221         }
222 #endif
223
224         adap->algo_data = bit_data;
225         adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
226         adap->dev.parent = &pdev->dev;
227         adap->dev.of_node = pdev->dev.of_node;
228
229         adap->nr = pdev->id;
230         ret = i2c_bit_add_numbered_bus(adap);
231         if (ret)
232                 goto err_add_bus;
233
234         of_i2c_register_devices(adap);
235
236         platform_set_drvdata(pdev, priv);
237
238         dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
239                  pdata->sda_pin, pdata->scl_pin,
240                  pdata->scl_is_output_only
241                  ? ", no clock stretching" : "");
242
243         return 0;
244
245 err_add_bus:
246         gpio_free(scl_pin);
247 err_request_scl:
248         gpio_free(sda_pin);
249 err_request_sda:
250         return ret;
251 }
252
253 static int i2c_gpio_remove(struct platform_device *pdev)
254 {
255         struct i2c_gpio_private_data *priv;
256         struct i2c_gpio_platform_data *pdata;
257         struct i2c_adapter *adap;
258
259         priv = platform_get_drvdata(pdev);
260         adap = &priv->adap;
261         pdata = &priv->pdata;
262
263         i2c_del_adapter(adap);
264         gpio_free(pdata->scl_pin);
265         gpio_free(pdata->sda_pin);
266
267         return 0;
268 }
269
270 #if defined(CONFIG_OF)
271 static const struct of_device_id i2c_gpio_dt_ids[] = {
272         { .compatible = "i2c-gpio", },
273         { /* sentinel */ }
274 };
275
276 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
277 #endif
278
279 static struct platform_driver i2c_gpio_driver = {
280         .driver         = {
281                 .name   = "i2c-gpio",
282                 .owner  = THIS_MODULE,
283                 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
284         },
285         .probe          = i2c_gpio_probe,
286         .remove         = i2c_gpio_remove,
287 };
288
289 static int __init i2c_gpio_init(void)
290 {
291         int ret;
292
293         ret = platform_driver_register(&i2c_gpio_driver);
294         if (ret)
295                 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
296
297         return ret;
298 }
299 subsys_initcall(i2c_gpio_init);
300
301 static void __exit i2c_gpio_exit(void)
302 {
303         platform_driver_unregister(&i2c_gpio_driver);
304 }
305 module_exit(i2c_gpio_exit);
306
307 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
308 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
309 MODULE_LICENSE("GPL");
310 MODULE_ALIAS("platform:i2c-gpio");