e1000e: cleanup PARENTHESIS_ALIGNMENT checkpatch checks
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-gpio.c
blobf3fa4332bbdf9fa93ad497002fee91ab858d06ee
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>
17 #include <linux/gpio.h>
18 #include <linux/of_gpio.h>
19 #include <linux/of_i2c.h>
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;
27 /* Toggle SDA by changing the direction of the pin */
28 static void i2c_gpio_setsda_dir(void *data, int state)
30 struct i2c_gpio_platform_data *pdata = data;
32 if (state)
33 gpio_direction_input(pdata->sda_pin);
34 else
35 gpio_direction_output(pdata->sda_pin, 0);
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.)
43 static void i2c_gpio_setsda_val(void *data, int state)
45 struct i2c_gpio_platform_data *pdata = data;
47 gpio_set_value(pdata->sda_pin, state);
50 /* Toggle SCL by changing the direction of the pin. */
51 static void i2c_gpio_setscl_dir(void *data, int state)
53 struct i2c_gpio_platform_data *pdata = data;
55 if (state)
56 gpio_direction_input(pdata->scl_pin);
57 else
58 gpio_direction_output(pdata->scl_pin, 0);
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.
67 static void i2c_gpio_setscl_val(void *data, int state)
69 struct i2c_gpio_platform_data *pdata = data;
71 gpio_set_value(pdata->scl_pin, state);
74 static int i2c_gpio_getsda(void *data)
76 struct i2c_gpio_platform_data *pdata = data;
78 return gpio_get_value(pdata->sda_pin);
81 static int i2c_gpio_getscl(void *data)
83 struct i2c_gpio_platform_data *pdata = data;
85 return gpio_get_value(pdata->scl_pin);
88 static int of_i2c_gpio_probe(struct device_node *np,
89 struct i2c_gpio_platform_data *pdata)
91 u32 reg;
93 if (of_gpio_count(np) < 2)
94 return -ENODEV;
96 pdata->sda_pin = of_get_gpio(np, 0);
97 pdata->scl_pin = of_get_gpio(np, 1);
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;
105 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
107 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
108 pdata->timeout = msecs_to_jiffies(reg);
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");
117 return 0;
120 static int i2c_gpio_probe(struct platform_device *pdev)
122 struct i2c_gpio_private_data *priv;
123 struct i2c_gpio_platform_data *pdata;
124 struct i2c_algo_bit_data *bit_data;
125 struct i2c_adapter *adap;
126 int ret;
128 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;
135 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));
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;
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;
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;
168 if (!pdata->scl_is_output_only)
169 bit_data->getscl = i2c_gpio_getscl;
170 bit_data->getsda = i2c_gpio_getsda;
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 */
179 if (pdata->timeout)
180 bit_data->timeout = pdata->timeout;
181 else
182 bit_data->timeout = HZ / 10; /* 100 ms */
184 bit_data->data = pdata;
186 adap->owner = THIS_MODULE;
187 if (pdev->dev.of_node)
188 strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
189 else
190 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
192 adap->algo_data = bit_data;
193 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
194 adap->dev.parent = &pdev->dev;
195 adap->dev.of_node = pdev->dev.of_node;
197 adap->nr = pdev->id;
198 ret = i2c_bit_add_numbered_bus(adap);
199 if (ret)
200 goto err_add_bus;
202 of_i2c_register_devices(adap);
204 platform_set_drvdata(pdev, priv);
206 dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
207 pdata->sda_pin, pdata->scl_pin,
208 pdata->scl_is_output_only
209 ? ", no clock stretching" : "");
211 return 0;
213 err_add_bus:
214 gpio_free(pdata->scl_pin);
215 err_request_scl:
216 gpio_free(pdata->sda_pin);
217 err_request_sda:
218 return ret;
221 static int i2c_gpio_remove(struct platform_device *pdev)
223 struct i2c_gpio_private_data *priv;
224 struct i2c_gpio_platform_data *pdata;
225 struct i2c_adapter *adap;
227 priv = platform_get_drvdata(pdev);
228 adap = &priv->adap;
229 pdata = &priv->pdata;
231 i2c_del_adapter(adap);
232 gpio_free(pdata->scl_pin);
233 gpio_free(pdata->sda_pin);
235 return 0;
238 #if defined(CONFIG_OF)
239 static const struct of_device_id i2c_gpio_dt_ids[] = {
240 { .compatible = "i2c-gpio", },
241 { /* sentinel */ }
244 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
245 #endif
247 static struct platform_driver i2c_gpio_driver = {
248 .driver = {
249 .name = "i2c-gpio",
250 .owner = THIS_MODULE,
251 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
253 .probe = i2c_gpio_probe,
254 .remove = i2c_gpio_remove,
257 static int __init i2c_gpio_init(void)
259 int ret;
261 ret = platform_driver_register(&i2c_gpio_driver);
262 if (ret)
263 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
265 return ret;
267 subsys_initcall(i2c_gpio_init);
269 static void __exit i2c_gpio_exit(void)
271 platform_driver_unregister(&i2c_gpio_driver);
273 module_exit(i2c_gpio_exit);
275 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
276 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
277 MODULE_LICENSE("GPL");
278 MODULE_ALIAS("platform:i2c-gpio");