NFC: pn533: Fix wrong GFP flag usage
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-gpio.c
blob005e6e0330c278276a0d602fcfebdc3429218cfd
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/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/i2c-algo-bit.h>
14 #include <linux/platform_data/i2c-gpio.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/of.h>
22 struct i2c_gpio_private_data {
23 struct gpio_desc *sda;
24 struct gpio_desc *scl;
25 struct i2c_adapter adap;
26 struct i2c_algo_bit_data bit_data;
27 struct i2c_gpio_platform_data pdata;
28 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
29 struct dentry *debug_dir;
30 #endif
34 * Toggle SDA by changing the output value of the pin. This is only
35 * valid for pins configured as open drain (i.e. setting the value
36 * high effectively turns off the output driver.)
38 static void i2c_gpio_setsda_val(void *data, int state)
40 struct i2c_gpio_private_data *priv = data;
42 gpiod_set_value_cansleep(priv->sda, state);
46 * Toggle SCL by changing the output value of the pin. This is used
47 * for pins that are configured as open drain and for output-only
48 * pins. The latter case will break the i2c protocol, but it will
49 * often work in practice.
51 static void i2c_gpio_setscl_val(void *data, int state)
53 struct i2c_gpio_private_data *priv = data;
55 gpiod_set_value_cansleep(priv->scl, state);
58 static int i2c_gpio_getsda(void *data)
60 struct i2c_gpio_private_data *priv = data;
62 return gpiod_get_value_cansleep(priv->sda);
65 static int i2c_gpio_getscl(void *data)
67 struct i2c_gpio_private_data *priv = data;
69 return gpiod_get_value_cansleep(priv->scl);
72 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
73 static struct dentry *i2c_gpio_debug_dir;
75 #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
76 #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
77 #define getsda(bd) ((bd)->getsda((bd)->data))
78 #define getscl(bd) ((bd)->getscl((bd)->data))
80 #define WIRE_ATTRIBUTE(wire) \
81 static int fops_##wire##_get(void *data, u64 *val) \
82 { \
83 struct i2c_gpio_private_data *priv = data; \
85 i2c_lock_adapter(&priv->adap); \
86 *val = get##wire(&priv->bit_data); \
87 i2c_unlock_adapter(&priv->adap); \
88 return 0; \
89 } \
90 static int fops_##wire##_set(void *data, u64 val) \
91 { \
92 struct i2c_gpio_private_data *priv = data; \
94 i2c_lock_adapter(&priv->adap); \
95 set##wire(&priv->bit_data, val); \
96 i2c_unlock_adapter(&priv->adap); \
97 return 0; \
98 } \
99 DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
101 WIRE_ATTRIBUTE(scl);
102 WIRE_ATTRIBUTE(sda);
104 static int fops_incomplete_transfer_set(void *data, u64 addr)
106 struct i2c_gpio_private_data *priv = data;
107 struct i2c_algo_bit_data *bit_data = &priv->bit_data;
108 int i, pattern;
110 if (addr > 0x7f)
111 return -EINVAL;
113 /* ADDR (7 bit) + RD (1 bit) + SDA hi (1 bit) */
114 pattern = (addr << 2) | 3;
116 i2c_lock_adapter(&priv->adap);
118 /* START condition */
119 setsda(bit_data, 0);
120 udelay(bit_data->udelay);
122 /* Send ADDR+RD, request ACK, don't send STOP */
123 for (i = 8; i >= 0; i--) {
124 setscl(bit_data, 0);
125 udelay(bit_data->udelay / 2);
126 setsda(bit_data, (pattern >> i) & 1);
127 udelay((bit_data->udelay + 1) / 2);
128 setscl(bit_data, 1);
129 udelay(bit_data->udelay);
132 i2c_unlock_adapter(&priv->adap);
134 return 0;
136 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n");
138 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
140 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
143 * If there will be a debugfs-dir per i2c adapter somewhen, put the
144 * 'fault-injector' dir there. Until then, we have a global dir with
145 * all adapters as subdirs.
147 if (!i2c_gpio_debug_dir) {
148 i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
149 if (!i2c_gpio_debug_dir)
150 return;
153 priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
154 if (!priv->debug_dir)
155 return;
157 debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
158 debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
159 debugfs_create_file_unsafe("incomplete_transfer", 0200, priv->debug_dir,
160 priv, &fops_incomplete_transfer);
163 static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
165 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
167 debugfs_remove_recursive(priv->debug_dir);
169 #else
170 static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
171 static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
172 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
174 static void of_i2c_gpio_get_props(struct device_node *np,
175 struct i2c_gpio_platform_data *pdata)
177 u32 reg;
179 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
181 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
182 pdata->timeout = msecs_to_jiffies(reg);
184 pdata->sda_is_open_drain =
185 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
186 pdata->scl_is_open_drain =
187 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
188 pdata->scl_is_output_only =
189 of_property_read_bool(np, "i2c-gpio,scl-output-only");
192 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
193 const char *con_id,
194 unsigned int index,
195 enum gpiod_flags gflags)
197 struct gpio_desc *retdesc;
198 int ret;
200 retdesc = devm_gpiod_get(dev, con_id, gflags);
201 if (!IS_ERR(retdesc)) {
202 dev_dbg(dev, "got GPIO from name %s\n", con_id);
203 return retdesc;
206 retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
207 if (!IS_ERR(retdesc)) {
208 dev_dbg(dev, "got GPIO from index %u\n", index);
209 return retdesc;
212 ret = PTR_ERR(retdesc);
214 /* FIXME: hack in the old code, is this really necessary? */
215 if (ret == -EINVAL)
216 retdesc = ERR_PTR(-EPROBE_DEFER);
218 /* This happens if the GPIO driver is not yet probed, let's defer */
219 if (ret == -ENOENT)
220 retdesc = ERR_PTR(-EPROBE_DEFER);
222 if (ret != -EPROBE_DEFER)
223 dev_err(dev, "error trying to get descriptor: %d\n", ret);
225 return retdesc;
228 static int i2c_gpio_probe(struct platform_device *pdev)
230 struct i2c_gpio_private_data *priv;
231 struct i2c_gpio_platform_data *pdata;
232 struct i2c_algo_bit_data *bit_data;
233 struct i2c_adapter *adap;
234 struct device *dev = &pdev->dev;
235 struct device_node *np = dev->of_node;
236 enum gpiod_flags gflags;
237 int ret;
239 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
240 if (!priv)
241 return -ENOMEM;
243 adap = &priv->adap;
244 bit_data = &priv->bit_data;
245 pdata = &priv->pdata;
247 if (np) {
248 of_i2c_gpio_get_props(np, pdata);
249 } else {
251 * If all platform data settings are zero it is OK
252 * to not provide any platform data from the board.
254 if (dev_get_platdata(dev))
255 memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
259 * First get the GPIO pins; if it fails, we'll defer the probe.
260 * If the SDA line is marked from platform data or device tree as
261 * "open drain" it means something outside of our control is making
262 * this line being handled as open drain, and we should just handle
263 * it as any other output. Else we enforce open drain as this is
264 * required for an I2C bus.
266 if (pdata->sda_is_open_drain)
267 gflags = GPIOD_OUT_HIGH;
268 else
269 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
270 priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
271 if (IS_ERR(priv->sda))
272 return PTR_ERR(priv->sda);
275 * If the SCL line is marked from platform data or device tree as
276 * "open drain" it means something outside of our control is making
277 * this line being handled as open drain, and we should just handle
278 * it as any other output. Else we enforce open drain as this is
279 * required for an I2C bus.
281 if (pdata->scl_is_open_drain)
282 gflags = GPIOD_OUT_LOW;
283 else
284 gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
285 priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
286 if (IS_ERR(priv->scl))
287 return PTR_ERR(priv->scl);
289 if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
290 dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
292 bit_data->setsda = i2c_gpio_setsda_val;
293 bit_data->setscl = i2c_gpio_setscl_val;
295 if (!pdata->scl_is_output_only)
296 bit_data->getscl = i2c_gpio_getscl;
297 bit_data->getsda = i2c_gpio_getsda;
299 if (pdata->udelay)
300 bit_data->udelay = pdata->udelay;
301 else if (pdata->scl_is_output_only)
302 bit_data->udelay = 50; /* 10 kHz */
303 else
304 bit_data->udelay = 5; /* 100 kHz */
306 if (pdata->timeout)
307 bit_data->timeout = pdata->timeout;
308 else
309 bit_data->timeout = HZ / 10; /* 100 ms */
311 bit_data->data = priv;
313 adap->owner = THIS_MODULE;
314 if (np)
315 strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
316 else
317 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
319 adap->algo_data = bit_data;
320 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
321 adap->dev.parent = dev;
322 adap->dev.of_node = np;
324 adap->nr = pdev->id;
325 ret = i2c_bit_add_numbered_bus(adap);
326 if (ret)
327 return ret;
329 platform_set_drvdata(pdev, priv);
332 * FIXME: using global GPIO numbers is not helpful. If/when we
333 * get accessors to get the actual name of the GPIO line,
334 * from the descriptor, then provide that instead.
336 dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
337 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
338 pdata->scl_is_output_only
339 ? ", no clock stretching" : "");
341 i2c_gpio_fault_injector_init(pdev);
343 return 0;
346 static int i2c_gpio_remove(struct platform_device *pdev)
348 struct i2c_gpio_private_data *priv;
349 struct i2c_adapter *adap;
351 i2c_gpio_fault_injector_exit(pdev);
353 priv = platform_get_drvdata(pdev);
354 adap = &priv->adap;
356 i2c_del_adapter(adap);
358 return 0;
361 #if defined(CONFIG_OF)
362 static const struct of_device_id i2c_gpio_dt_ids[] = {
363 { .compatible = "i2c-gpio", },
364 { /* sentinel */ }
367 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
368 #endif
370 static struct platform_driver i2c_gpio_driver = {
371 .driver = {
372 .name = "i2c-gpio",
373 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
375 .probe = i2c_gpio_probe,
376 .remove = i2c_gpio_remove,
379 static int __init i2c_gpio_init(void)
381 int ret;
383 ret = platform_driver_register(&i2c_gpio_driver);
384 if (ret)
385 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
387 return ret;
389 subsys_initcall(i2c_gpio_init);
391 static void __exit i2c_gpio_exit(void)
393 platform_driver_unregister(&i2c_gpio_driver);
395 module_exit(i2c_gpio_exit);
397 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
398 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
399 MODULE_LICENSE("GPL");
400 MODULE_ALIAS("platform:i2c-gpio");