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/consumer.h>
20 struct i2c_gpio_private_data {
21 struct gpio_desc *sda;
22 struct gpio_desc *scl;
23 struct i2c_adapter adap;
24 struct i2c_algo_bit_data bit_data;
25 struct i2c_gpio_platform_data pdata;
28 /* Toggle SDA by changing the direction of the pin */
29 static void i2c_gpio_setsda_dir(void *data, int state)
31 struct i2c_gpio_private_data *priv = data;
34 * This is a way of saying "do not drive
35 * me actively high" which means emulating open drain.
36 * The right way to do this is for gpiolib to
37 * handle this, by the function below.
40 gpiod_direction_input(priv->sda);
42 gpiod_direction_output(priv->sda, 0);
46 * Toggle SDA by changing the output value of the pin. This is only
47 * valid for pins configured as open drain (i.e. setting the value
48 * high effectively turns off the output driver.)
50 static void i2c_gpio_setsda_val(void *data, int state)
52 struct i2c_gpio_private_data *priv = data;
54 gpiod_set_value(priv->sda, state);
57 /* Toggle SCL by changing the direction of the pin. */
58 static void i2c_gpio_setscl_dir(void *data, int state)
60 struct i2c_gpio_private_data *priv = data;
63 gpiod_direction_input(priv->scl);
65 gpiod_direction_output(priv->scl, 0);
69 * Toggle SCL by changing the output value of the pin. This is used
70 * for pins that are configured as open drain and for output-only
71 * pins. The latter case will break the i2c protocol, but it will
72 * often work in practice.
74 static void i2c_gpio_setscl_val(void *data, int state)
76 struct i2c_gpio_private_data *priv = data;
78 gpiod_set_value(priv->scl, state);
81 static int i2c_gpio_getsda(void *data)
83 struct i2c_gpio_private_data *priv = data;
85 return gpiod_get_value(priv->sda);
88 static int i2c_gpio_getscl(void *data)
90 struct i2c_gpio_private_data *priv = data;
92 return gpiod_get_value(priv->scl);
95 static void of_i2c_gpio_get_props(struct device_node *np,
96 struct i2c_gpio_platform_data *pdata)
100 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
102 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®))
103 pdata->timeout = msecs_to_jiffies(reg);
105 pdata->sda_is_open_drain =
106 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
107 pdata->scl_is_open_drain =
108 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
109 pdata->scl_is_output_only =
110 of_property_read_bool(np, "i2c-gpio,scl-output-only");
113 static int i2c_gpio_probe(struct platform_device *pdev)
115 struct i2c_gpio_private_data *priv;
116 struct i2c_gpio_platform_data *pdata;
117 struct i2c_algo_bit_data *bit_data;
118 struct i2c_adapter *adap;
121 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
125 /* First get the GPIO pins; if it fails, we'll defer the probe. */
126 priv->sda = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_HIGH);
127 if (IS_ERR(priv->sda)) {
128 ret = PTR_ERR(priv->sda);
129 /* FIXME: hack in the old code, is this really necessary? */
134 priv->scl = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_OUT_LOW);
135 if (IS_ERR(priv->scl)) {
136 ret = PTR_ERR(priv->scl);
137 /* FIXME: hack in the old code, is this really necessary? */
144 bit_data = &priv->bit_data;
145 pdata = &priv->pdata;
147 if (pdev->dev.of_node) {
148 of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
151 * If all platform data settings are zero it is OK
152 * to not provide any platform data from the board.
154 if (dev_get_platdata(&pdev->dev))
155 memcpy(pdata, dev_get_platdata(&pdev->dev),
160 * FIXME: this is a hack emulating the open drain emulation
161 * that gpiolib can already do for us. Make all clients properly
162 * flag their lines as open drain and get rid of this property
163 * and the special callback.
165 if (pdata->sda_is_open_drain) {
166 gpiod_direction_output(priv->sda, 1);
167 bit_data->setsda = i2c_gpio_setsda_val;
169 gpiod_direction_input(priv->sda);
170 bit_data->setsda = i2c_gpio_setsda_dir;
173 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
174 gpiod_direction_output(priv->scl, 1);
175 bit_data->setscl = i2c_gpio_setscl_val;
177 gpiod_direction_input(priv->scl);
178 bit_data->setscl = i2c_gpio_setscl_dir;
181 if (!pdata->scl_is_output_only)
182 bit_data->getscl = i2c_gpio_getscl;
183 bit_data->getsda = i2c_gpio_getsda;
186 bit_data->udelay = pdata->udelay;
187 else if (pdata->scl_is_output_only)
188 bit_data->udelay = 50; /* 10 kHz */
190 bit_data->udelay = 5; /* 100 kHz */
193 bit_data->timeout = pdata->timeout;
195 bit_data->timeout = HZ / 10; /* 100 ms */
197 bit_data->data = priv;
199 adap->owner = THIS_MODULE;
200 if (pdev->dev.of_node)
201 strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
203 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
205 adap->algo_data = bit_data;
206 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
207 adap->dev.parent = &pdev->dev;
208 adap->dev.of_node = pdev->dev.of_node;
211 ret = i2c_bit_add_numbered_bus(adap);
215 platform_set_drvdata(pdev, priv);
218 * FIXME: using global GPIO numbers is not helpful. If/when we
219 * get accessors to get the actual name of the GPIO line,
220 * from the descriptor, then provide that instead.
222 dev_info(&pdev->dev, "using lines %u (SDA) and %u (SCL%s)\n",
223 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
224 pdata->scl_is_output_only
225 ? ", no clock stretching" : "");
230 static int i2c_gpio_remove(struct platform_device *pdev)
232 struct i2c_gpio_private_data *priv;
233 struct i2c_adapter *adap;
235 priv = platform_get_drvdata(pdev);
238 i2c_del_adapter(adap);
243 #if defined(CONFIG_OF)
244 static const struct of_device_id i2c_gpio_dt_ids[] = {
245 { .compatible = "i2c-gpio", },
249 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
252 static struct platform_driver i2c_gpio_driver = {
255 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
257 .probe = i2c_gpio_probe,
258 .remove = i2c_gpio_remove,
261 static int __init i2c_gpio_init(void)
265 ret = platform_driver_register(&i2c_gpio_driver);
267 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
271 subsys_initcall(i2c_gpio_init);
273 static void __exit i2c_gpio_exit(void)
275 platform_driver_unregister(&i2c_gpio_driver);
277 module_exit(i2c_gpio_exit);
279 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
280 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
281 MODULE_LICENSE("GPL");
282 MODULE_ALIAS("platform:i2c-gpio");