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>
19 #include <linux/of_gpio.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
;
33 gpio_direction_input(pdata
->sda_pin
);
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
;
56 gpio_direction_input(pdata
->scl_pin
);
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_get_pins(struct device_node
*np
,
89 unsigned int *sda_pin
, unsigned int *scl_pin
)
91 if (of_gpio_count(np
) < 2)
94 *sda_pin
= of_get_gpio(np
, 0);
95 *scl_pin
= of_get_gpio(np
, 1);
97 if (*sda_pin
== -EPROBE_DEFER
|| *scl_pin
== -EPROBE_DEFER
)
100 if (!gpio_is_valid(*sda_pin
) || !gpio_is_valid(*scl_pin
)) {
101 pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
102 np
->full_name
, *sda_pin
, *scl_pin
);
109 static void of_i2c_gpio_get_props(struct device_node
*np
,
110 struct i2c_gpio_platform_data
*pdata
)
114 of_property_read_u32(np
, "i2c-gpio,delay-us", &pdata
->udelay
);
116 if (!of_property_read_u32(np
, "i2c-gpio,timeout-ms", ®
))
117 pdata
->timeout
= msecs_to_jiffies(reg
);
119 pdata
->sda_is_open_drain
=
120 of_property_read_bool(np
, "i2c-gpio,sda-open-drain");
121 pdata
->scl_is_open_drain
=
122 of_property_read_bool(np
, "i2c-gpio,scl-open-drain");
123 pdata
->scl_is_output_only
=
124 of_property_read_bool(np
, "i2c-gpio,scl-output-only");
127 static int i2c_gpio_probe(struct platform_device
*pdev
)
129 struct i2c_gpio_private_data
*priv
;
130 struct i2c_gpio_platform_data
*pdata
;
131 struct i2c_algo_bit_data
*bit_data
;
132 struct i2c_adapter
*adap
;
133 unsigned int sda_pin
, scl_pin
;
136 /* First get the GPIO pins; if it fails, we'll defer the probe. */
137 if (pdev
->dev
.of_node
) {
138 ret
= of_i2c_gpio_get_pins(pdev
->dev
.of_node
,
143 if (!dev_get_platdata(&pdev
->dev
))
145 pdata
= dev_get_platdata(&pdev
->dev
);
146 sda_pin
= pdata
->sda_pin
;
147 scl_pin
= pdata
->scl_pin
;
150 ret
= gpio_request(sda_pin
, "sda");
153 ret
= -EPROBE_DEFER
; /* Try again later */
154 goto err_request_sda
;
156 ret
= gpio_request(scl_pin
, "scl");
159 ret
= -EPROBE_DEFER
; /* Try again later */
160 goto err_request_scl
;
163 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
169 bit_data
= &priv
->bit_data
;
170 pdata
= &priv
->pdata
;
172 if (pdev
->dev
.of_node
) {
173 pdata
->sda_pin
= sda_pin
;
174 pdata
->scl_pin
= scl_pin
;
175 of_i2c_gpio_get_props(pdev
->dev
.of_node
, pdata
);
177 memcpy(pdata
, dev_get_platdata(&pdev
->dev
), sizeof(*pdata
));
180 if (pdata
->sda_is_open_drain
) {
181 gpio_direction_output(pdata
->sda_pin
, 1);
182 bit_data
->setsda
= i2c_gpio_setsda_val
;
184 gpio_direction_input(pdata
->sda_pin
);
185 bit_data
->setsda
= i2c_gpio_setsda_dir
;
188 if (pdata
->scl_is_open_drain
|| pdata
->scl_is_output_only
) {
189 gpio_direction_output(pdata
->scl_pin
, 1);
190 bit_data
->setscl
= i2c_gpio_setscl_val
;
192 gpio_direction_input(pdata
->scl_pin
);
193 bit_data
->setscl
= i2c_gpio_setscl_dir
;
196 if (!pdata
->scl_is_output_only
)
197 bit_data
->getscl
= i2c_gpio_getscl
;
198 bit_data
->getsda
= i2c_gpio_getsda
;
201 bit_data
->udelay
= pdata
->udelay
;
202 else if (pdata
->scl_is_output_only
)
203 bit_data
->udelay
= 50; /* 10 kHz */
205 bit_data
->udelay
= 5; /* 100 kHz */
208 bit_data
->timeout
= pdata
->timeout
;
210 bit_data
->timeout
= HZ
/ 10; /* 100 ms */
212 bit_data
->data
= pdata
;
214 adap
->owner
= THIS_MODULE
;
215 if (pdev
->dev
.of_node
)
216 strlcpy(adap
->name
, dev_name(&pdev
->dev
), sizeof(adap
->name
));
218 snprintf(adap
->name
, sizeof(adap
->name
), "i2c-gpio%d", pdev
->id
);
220 adap
->algo_data
= bit_data
;
221 adap
->class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
222 adap
->dev
.parent
= &pdev
->dev
;
223 adap
->dev
.of_node
= pdev
->dev
.of_node
;
226 ret
= i2c_bit_add_numbered_bus(adap
);
230 platform_set_drvdata(pdev
, priv
);
232 dev_info(&pdev
->dev
, "using pins %u (SDA) and %u (SCL%s)\n",
233 pdata
->sda_pin
, pdata
->scl_pin
,
234 pdata
->scl_is_output_only
235 ? ", no clock stretching" : "");
247 static int i2c_gpio_remove(struct platform_device
*pdev
)
249 struct i2c_gpio_private_data
*priv
;
250 struct i2c_gpio_platform_data
*pdata
;
251 struct i2c_adapter
*adap
;
253 priv
= platform_get_drvdata(pdev
);
255 pdata
= &priv
->pdata
;
257 i2c_del_adapter(adap
);
258 gpio_free(pdata
->scl_pin
);
259 gpio_free(pdata
->sda_pin
);
264 #if defined(CONFIG_OF)
265 static const struct of_device_id i2c_gpio_dt_ids
[] = {
266 { .compatible
= "i2c-gpio", },
270 MODULE_DEVICE_TABLE(of
, i2c_gpio_dt_ids
);
273 static struct platform_driver i2c_gpio_driver
= {
276 .owner
= THIS_MODULE
,
277 .of_match_table
= of_match_ptr(i2c_gpio_dt_ids
),
279 .probe
= i2c_gpio_probe
,
280 .remove
= i2c_gpio_remove
,
283 static int __init
i2c_gpio_init(void)
287 ret
= platform_driver_register(&i2c_gpio_driver
);
289 printk(KERN_ERR
"i2c-gpio: probe failed: %d\n", ret
);
293 subsys_initcall(i2c_gpio_init
);
295 static void __exit
i2c_gpio_exit(void)
297 platform_driver_unregister(&i2c_gpio_driver
);
299 module_exit(i2c_gpio_exit
);
301 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
302 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
303 MODULE_LICENSE("GPL");
304 MODULE_ALIAS("platform:i2c-gpio");