Commit | Line | Data |
---|---|---|
1c23af90 HS |
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> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
1c23af90 | 16 | #include <linux/platform_device.h> |
b2e63555 | 17 | #include <linux/gpio/consumer.h> |
4edd65e6 | 18 | #include <linux/of.h> |
8ffaa0f4 JCPV |
19 | |
20 | struct i2c_gpio_private_data { | |
b2e63555 LW |
21 | struct gpio_desc *sda; |
22 | struct gpio_desc *scl; | |
8ffaa0f4 JCPV |
23 | struct i2c_adapter adap; |
24 | struct i2c_algo_bit_data bit_data; | |
25 | struct i2c_gpio_platform_data pdata; | |
26 | }; | |
1c23af90 HS |
27 | |
28 | /* Toggle SDA by changing the direction of the pin */ | |
29 | static void i2c_gpio_setsda_dir(void *data, int state) | |
30 | { | |
b2e63555 LW |
31 | struct i2c_gpio_private_data *priv = data; |
32 | ||
33 | /* | |
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. | |
38 | */ | |
1c23af90 | 39 | if (state) |
b2e63555 | 40 | gpiod_direction_input(priv->sda); |
1c23af90 | 41 | else |
b2e63555 | 42 | gpiod_direction_output(priv->sda, 0); |
1c23af90 HS |
43 | } |
44 | ||
45 | /* | |
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.) | |
49 | */ | |
50 | static void i2c_gpio_setsda_val(void *data, int state) | |
51 | { | |
b2e63555 | 52 | struct i2c_gpio_private_data *priv = data; |
1c23af90 | 53 | |
b2e63555 | 54 | gpiod_set_value(priv->sda, state); |
1c23af90 HS |
55 | } |
56 | ||
57 | /* Toggle SCL by changing the direction of the pin. */ | |
58 | static void i2c_gpio_setscl_dir(void *data, int state) | |
59 | { | |
b2e63555 | 60 | struct i2c_gpio_private_data *priv = data; |
1c23af90 HS |
61 | |
62 | if (state) | |
b2e63555 | 63 | gpiod_direction_input(priv->scl); |
1c23af90 | 64 | else |
b2e63555 | 65 | gpiod_direction_output(priv->scl, 0); |
1c23af90 HS |
66 | } |
67 | ||
68 | /* | |
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. | |
73 | */ | |
74 | static void i2c_gpio_setscl_val(void *data, int state) | |
75 | { | |
b2e63555 | 76 | struct i2c_gpio_private_data *priv = data; |
1c23af90 | 77 | |
b2e63555 | 78 | gpiod_set_value(priv->scl, state); |
1c23af90 HS |
79 | } |
80 | ||
4d6ceed4 | 81 | static int i2c_gpio_getsda(void *data) |
1c23af90 | 82 | { |
b2e63555 | 83 | struct i2c_gpio_private_data *priv = data; |
1c23af90 | 84 | |
b2e63555 | 85 | return gpiod_get_value(priv->sda); |
1c23af90 HS |
86 | } |
87 | ||
4d6ceed4 | 88 | static int i2c_gpio_getscl(void *data) |
1c23af90 | 89 | { |
b2e63555 | 90 | struct i2c_gpio_private_data *priv = data; |
1c23af90 | 91 | |
b2e63555 | 92 | return gpiod_get_value(priv->scl); |
1b295c83 JD |
93 | } |
94 | ||
95 | static void of_i2c_gpio_get_props(struct device_node *np, | |
96 | struct i2c_gpio_platform_data *pdata) | |
97 | { | |
98 | u32 reg; | |
99 | ||
8ffaa0f4 JCPV |
100 | of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); |
101 | ||
102 | if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) | |
103 | pdata->timeout = msecs_to_jiffies(reg); | |
104 | ||
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"); | |
8ffaa0f4 JCPV |
111 | } |
112 | ||
0b255e92 | 113 | static int i2c_gpio_probe(struct platform_device *pdev) |
1c23af90 | 114 | { |
8ffaa0f4 | 115 | struct i2c_gpio_private_data *priv; |
1c23af90 HS |
116 | struct i2c_gpio_platform_data *pdata; |
117 | struct i2c_algo_bit_data *bit_data; | |
118 | struct i2c_adapter *adap; | |
119 | int ret; | |
120 | ||
b2e63555 LW |
121 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
122 | if (!priv) | |
123 | return -ENOMEM; | |
1c23af90 | 124 | |
b2e63555 LW |
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? */ | |
1b295c83 | 130 | if (ret == -EINVAL) |
b2e63555 | 131 | ret = -EPROBE_DEFER; |
a0682a31 | 132 | return ret; |
1b295c83 | 133 | } |
b2e63555 LW |
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? */ | |
1b295c83 | 138 | if (ret == -EINVAL) |
b2e63555 | 139 | ret = -EPROBE_DEFER; |
a0682a31 | 140 | return ret; |
1b295c83 JD |
141 | } |
142 | ||
1b295c83 JD |
143 | adap = &priv->adap; |
144 | bit_data = &priv->bit_data; | |
145 | pdata = &priv->pdata; | |
146 | ||
147 | if (pdev->dev.of_node) { | |
1b295c83 JD |
148 | of_i2c_gpio_get_props(pdev->dev.of_node, pdata); |
149 | } else { | |
b2e63555 LW |
150 | /* |
151 | * If all platform data settings are zero it is OK | |
152 | * to not provide any platform data from the board. | |
153 | */ | |
154 | if (dev_get_platdata(&pdev->dev)) | |
155 | memcpy(pdata, dev_get_platdata(&pdev->dev), | |
156 | sizeof(*pdata)); | |
1b295c83 | 157 | } |
1c23af90 | 158 | |
b2e63555 LW |
159 | /* |
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. | |
164 | */ | |
1c23af90 | 165 | if (pdata->sda_is_open_drain) { |
b2e63555 | 166 | gpiod_direction_output(priv->sda, 1); |
1c23af90 HS |
167 | bit_data->setsda = i2c_gpio_setsda_val; |
168 | } else { | |
b2e63555 | 169 | gpiod_direction_input(priv->sda); |
1c23af90 HS |
170 | bit_data->setsda = i2c_gpio_setsda_dir; |
171 | } | |
172 | ||
173 | if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { | |
b2e63555 | 174 | gpiod_direction_output(priv->scl, 1); |
1c23af90 HS |
175 | bit_data->setscl = i2c_gpio_setscl_val; |
176 | } else { | |
b2e63555 | 177 | gpiod_direction_input(priv->scl); |
1c23af90 HS |
178 | bit_data->setscl = i2c_gpio_setscl_dir; |
179 | } | |
180 | ||
181 | if (!pdata->scl_is_output_only) | |
182 | bit_data->getscl = i2c_gpio_getscl; | |
183 | bit_data->getsda = i2c_gpio_getsda; | |
184 | ||
185 | if (pdata->udelay) | |
186 | bit_data->udelay = pdata->udelay; | |
187 | else if (pdata->scl_is_output_only) | |
188 | bit_data->udelay = 50; /* 10 kHz */ | |
189 | else | |
190 | bit_data->udelay = 5; /* 100 kHz */ | |
191 | ||
192 | if (pdata->timeout) | |
193 | bit_data->timeout = pdata->timeout; | |
194 | else | |
195 | bit_data->timeout = HZ / 10; /* 100 ms */ | |
196 | ||
b2e63555 | 197 | bit_data->data = priv; |
1c23af90 HS |
198 | |
199 | adap->owner = THIS_MODULE; | |
58a7371a BS |
200 | if (pdev->dev.of_node) |
201 | strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); | |
202 | else | |
203 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); | |
204 | ||
1c23af90 | 205 | adap->algo_data = bit_data; |
3401b2ff | 206 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
1c23af90 | 207 | adap->dev.parent = &pdev->dev; |
8ffaa0f4 | 208 | adap->dev.of_node = pdev->dev.of_node; |
1c23af90 | 209 | |
44454baa | 210 | adap->nr = pdev->id; |
7e69c3ac | 211 | ret = i2c_bit_add_numbered_bus(adap); |
1c23af90 | 212 | if (ret) |
a0682a31 | 213 | return ret; |
1c23af90 | 214 | |
8ffaa0f4 | 215 | platform_set_drvdata(pdev, priv); |
1c23af90 | 216 | |
b2e63555 LW |
217 | /* |
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. | |
221 | */ | |
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), | |
1c23af90 HS |
224 | pdata->scl_is_output_only |
225 | ? ", no clock stretching" : ""); | |
226 | ||
227 | return 0; | |
1c23af90 HS |
228 | } |
229 | ||
0b255e92 | 230 | static int i2c_gpio_remove(struct platform_device *pdev) |
1c23af90 | 231 | { |
8ffaa0f4 | 232 | struct i2c_gpio_private_data *priv; |
1c23af90 HS |
233 | struct i2c_adapter *adap; |
234 | ||
8ffaa0f4 JCPV |
235 | priv = platform_get_drvdata(pdev); |
236 | adap = &priv->adap; | |
1c23af90 HS |
237 | |
238 | i2c_del_adapter(adap); | |
1c23af90 HS |
239 | |
240 | return 0; | |
241 | } | |
242 | ||
8ffaa0f4 JCPV |
243 | #if defined(CONFIG_OF) |
244 | static const struct of_device_id i2c_gpio_dt_ids[] = { | |
245 | { .compatible = "i2c-gpio", }, | |
246 | { /* sentinel */ } | |
247 | }; | |
248 | ||
249 | MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); | |
250 | #endif | |
251 | ||
1c23af90 HS |
252 | static struct platform_driver i2c_gpio_driver = { |
253 | .driver = { | |
254 | .name = "i2c-gpio", | |
8ffaa0f4 | 255 | .of_match_table = of_match_ptr(i2c_gpio_dt_ids), |
1c23af90 | 256 | }, |
1efe7c55 | 257 | .probe = i2c_gpio_probe, |
0b255e92 | 258 | .remove = i2c_gpio_remove, |
1c23af90 HS |
259 | }; |
260 | ||
261 | static int __init i2c_gpio_init(void) | |
262 | { | |
263 | int ret; | |
264 | ||
1efe7c55 | 265 | ret = platform_driver_register(&i2c_gpio_driver); |
1c23af90 HS |
266 | if (ret) |
267 | printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); | |
268 | ||
269 | return ret; | |
270 | } | |
b8680784 | 271 | subsys_initcall(i2c_gpio_init); |
1c23af90 HS |
272 | |
273 | static void __exit i2c_gpio_exit(void) | |
274 | { | |
275 | platform_driver_unregister(&i2c_gpio_driver); | |
276 | } | |
277 | module_exit(i2c_gpio_exit); | |
278 | ||
e05503ef | 279 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
1c23af90 HS |
280 | MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); |
281 | MODULE_LICENSE("GPL"); | |
add8eda7 | 282 | MODULE_ALIAS("platform:i2c-gpio"); |