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.
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>
20 struct i2c_gpio_private_data {
21 struct i2c_adapter adap;
22 struct i2c_algo_bit_data bit_data;
23 struct i2c_gpio_platform_data pdata;
26 /* Toggle SDA by changing the direction of the pin */
27 static void i2c_gpio_setsda_dir(void *data, int state)
29 struct i2c_gpio_platform_data *pdata = data;
32 gpio_direction_input(pdata->sda_pin);
34 gpio_direction_output(pdata->sda_pin, 0);
38 * Toggle SDA by changing the output value of the pin. This is only
39 * valid for pins configured as open drain (i.e. setting the value
40 * high effectively turns off the output driver.)
42 static void i2c_gpio_setsda_val(void *data, int state)
44 struct i2c_gpio_platform_data *pdata = data;
46 gpio_set_value(pdata->sda_pin, state);
49 /* Toggle SCL by changing the direction of the pin. */
50 static void i2c_gpio_setscl_dir(void *data, int state)
52 struct i2c_gpio_platform_data *pdata = data;
55 gpio_direction_input(pdata->scl_pin);
57 gpio_direction_output(pdata->scl_pin, 0);
61 * Toggle SCL by changing the output value of the pin. This is used
62 * for pins that are configured as open drain and for output-only
63 * pins. The latter case will break the i2c protocol, but it will
64 * often work in practice.
66 static void i2c_gpio_setscl_val(void *data, int state)
68 struct i2c_gpio_platform_data *pdata = data;
70 gpio_set_value(pdata->scl_pin, state);
73 static int i2c_gpio_getsda(void *data)
75 struct i2c_gpio_platform_data *pdata = data;
77 return gpio_get_value(pdata->sda_pin);
80 static int i2c_gpio_getscl(void *data)
82 struct i2c_gpio_platform_data *pdata = data;
84 return gpio_get_value(pdata->scl_pin);
87 static int of_i2c_gpio_get_pins(struct device_node *np,
88 unsigned int *sda_pin, unsigned int *scl_pin)
90 if (of_gpio_count(np) < 2)
93 *sda_pin = of_get_gpio(np, 0);
94 *scl_pin = of_get_gpio(np, 1);
96 if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
97 pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
98 np->full_name, *sda_pin, *scl_pin);
105 static void of_i2c_gpio_get_props(struct device_node *np,
106 struct i2c_gpio_platform_data *pdata)
110 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
112 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®))
113 pdata->timeout = msecs_to_jiffies(reg);
115 pdata->sda_is_open_drain =
116 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
117 pdata->scl_is_open_drain =
118 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
119 pdata->scl_is_output_only =
120 of_property_read_bool(np, "i2c-gpio,scl-output-only");
123 static int i2c_gpio_probe(struct platform_device *pdev)
125 struct i2c_gpio_private_data *priv;
126 struct i2c_gpio_platform_data *pdata;
127 struct i2c_algo_bit_data *bit_data;
128 struct i2c_adapter *adap;
129 unsigned int sda_pin, scl_pin;
132 /* First get the GPIO pins; if it fails, we'll defer the probe. */
133 if (pdev->dev.of_node) {
134 ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
139 if (!dev_get_platdata(&pdev->dev))
141 pdata = dev_get_platdata(&pdev->dev);
142 sda_pin = pdata->sda_pin;
143 scl_pin = pdata->scl_pin;
146 ret = gpio_request(sda_pin, "sda");
149 ret = -EPROBE_DEFER; /* Try again later */
150 goto err_request_sda;
152 ret = gpio_request(scl_pin, "scl");
155 ret = -EPROBE_DEFER; /* Try again later */
156 goto err_request_scl;
159 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
165 bit_data = &priv->bit_data;
166 pdata = &priv->pdata;
168 if (pdev->dev.of_node) {
169 pdata->sda_pin = sda_pin;
170 pdata->scl_pin = scl_pin;
171 of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
173 memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
176 if (pdata->sda_is_open_drain) {
177 gpio_direction_output(pdata->sda_pin, 1);
178 bit_data->setsda = i2c_gpio_setsda_val;
180 gpio_direction_input(pdata->sda_pin);
181 bit_data->setsda = i2c_gpio_setsda_dir;
184 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
185 gpio_direction_output(pdata->scl_pin, 1);
186 bit_data->setscl = i2c_gpio_setscl_val;
188 gpio_direction_input(pdata->scl_pin);
189 bit_data->setscl = i2c_gpio_setscl_dir;
192 if (!pdata->scl_is_output_only)
193 bit_data->getscl = i2c_gpio_getscl;
194 bit_data->getsda = i2c_gpio_getsda;
197 bit_data->udelay = pdata->udelay;
198 else if (pdata->scl_is_output_only)
199 bit_data->udelay = 50; /* 10 kHz */
201 bit_data->udelay = 5; /* 100 kHz */
204 bit_data->timeout = pdata->timeout;
206 bit_data->timeout = HZ / 10; /* 100 ms */
208 bit_data->data = pdata;
210 adap->owner = THIS_MODULE;
211 if (pdev->dev.of_node)
212 strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
214 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
216 adap->algo_data = bit_data;
217 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
218 adap->dev.parent = &pdev->dev;
219 adap->dev.of_node = pdev->dev.of_node;
222 ret = i2c_bit_add_numbered_bus(adap);
226 platform_set_drvdata(pdev, priv);
228 dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
229 pdata->sda_pin, pdata->scl_pin,
230 pdata->scl_is_output_only
231 ? ", no clock stretching" : "");
243 static int i2c_gpio_remove(struct platform_device *pdev)
245 struct i2c_gpio_private_data *priv;
246 struct i2c_gpio_platform_data *pdata;
247 struct i2c_adapter *adap;
249 priv = platform_get_drvdata(pdev);
251 pdata = &priv->pdata;
253 i2c_del_adapter(adap);
254 gpio_free(pdata->scl_pin);
255 gpio_free(pdata->sda_pin);
260 #if defined(CONFIG_OF)
261 static const struct of_device_id i2c_gpio_dt_ids[] = {
262 { .compatible = "i2c-gpio", },
266 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
269 static struct platform_driver i2c_gpio_driver = {
272 .owner = THIS_MODULE,
273 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
275 .probe = i2c_gpio_probe,
276 .remove = i2c_gpio_remove,
279 static int __init i2c_gpio_init(void)
283 ret = platform_driver_register(&i2c_gpio_driver);
285 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
289 subsys_initcall(i2c_gpio_init);
291 static void __exit i2c_gpio_exit(void)
293 platform_driver_unregister(&i2c_gpio_driver);
295 module_exit(i2c_gpio_exit);
297 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
298 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
299 MODULE_LICENSE("GPL");
300 MODULE_ALIAS("platform:i2c-gpio");