i2c-gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/debugfs.h>
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/i2c-algo-bit.h>
  14. #include <linux/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>
  21. struct i2c_gpio_private_data {
  22. struct gpio_desc *sda;
  23. struct gpio_desc *scl;
  24. struct i2c_adapter adap;
  25. struct i2c_algo_bit_data bit_data;
  26. struct i2c_gpio_platform_data pdata;
  27. #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
  28. struct dentry *debug_dir;
  29. #endif
  30. };
  31. /*
  32. * Toggle SDA by changing the output value of the pin. This is only
  33. * valid for pins configured as open drain (i.e. setting the value
  34. * high effectively turns off the output driver.)
  35. */
  36. static void i2c_gpio_setsda_val(void *data, int state)
  37. {
  38. struct i2c_gpio_private_data *priv = data;
  39. gpiod_set_value_cansleep(priv->sda, state);
  40. }
  41. /*
  42. * Toggle SCL by changing the output value of the pin. This is used
  43. * for pins that are configured as open drain and for output-only
  44. * pins. The latter case will break the i2c protocol, but it will
  45. * often work in practice.
  46. */
  47. static void i2c_gpio_setscl_val(void *data, int state)
  48. {
  49. struct i2c_gpio_private_data *priv = data;
  50. gpiod_set_value_cansleep(priv->scl, state);
  51. }
  52. static int i2c_gpio_getsda(void *data)
  53. {
  54. struct i2c_gpio_private_data *priv = data;
  55. return gpiod_get_value_cansleep(priv->sda);
  56. }
  57. static int i2c_gpio_getscl(void *data)
  58. {
  59. struct i2c_gpio_private_data *priv = data;
  60. return gpiod_get_value_cansleep(priv->scl);
  61. }
  62. #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
  63. static struct dentry *i2c_gpio_debug_dir;
  64. #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
  65. #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
  66. #define getsda(bd) ((bd)->getsda((bd)->data))
  67. #define getscl(bd) ((bd)->getscl((bd)->data))
  68. #define WIRE_ATTRIBUTE(wire) \
  69. static int fops_##wire##_get(void *data, u64 *val) \
  70. { \
  71. struct i2c_gpio_private_data *priv = data; \
  72. \
  73. i2c_lock_adapter(&priv->adap); \
  74. *val = get##wire(&priv->bit_data); \
  75. i2c_unlock_adapter(&priv->adap); \
  76. return 0; \
  77. } \
  78. static int fops_##wire##_set(void *data, u64 val) \
  79. { \
  80. struct i2c_gpio_private_data *priv = data; \
  81. \
  82. i2c_lock_adapter(&priv->adap); \
  83. set##wire(&priv->bit_data, val); \
  84. i2c_unlock_adapter(&priv->adap); \
  85. return 0; \
  86. } \
  87. DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
  88. WIRE_ATTRIBUTE(scl);
  89. WIRE_ATTRIBUTE(sda);
  90. static int fops_incomplete_transfer_set(void *data, u64 addr)
  91. {
  92. struct i2c_gpio_private_data *priv = data;
  93. struct i2c_algo_bit_data *bit_data = &priv->bit_data;
  94. int i, pattern;
  95. if (addr > 0x7f)
  96. return -EINVAL;
  97. /* ADDR (7 bit) + RD (1 bit) + SDA hi (1 bit) */
  98. pattern = (addr << 2) | 3;
  99. i2c_lock_adapter(&priv->adap);
  100. /* START condition */
  101. setsda(bit_data, 0);
  102. udelay(bit_data->udelay);
  103. /* Send ADDR+RD, request ACK, don't send STOP */
  104. for (i = 8; i >= 0; i--) {
  105. setscl(bit_data, 0);
  106. udelay(bit_data->udelay / 2);
  107. setsda(bit_data, (pattern >> i) & 1);
  108. udelay((bit_data->udelay + 1) / 2);
  109. setscl(bit_data, 1);
  110. udelay(bit_data->udelay);
  111. }
  112. i2c_unlock_adapter(&priv->adap);
  113. return 0;
  114. }
  115. DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n");
  116. static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
  117. {
  118. struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
  119. /*
  120. * If there will be a debugfs-dir per i2c adapter somewhen, put the
  121. * 'fault-injector' dir there. Until then, we have a global dir with
  122. * all adapters as subdirs.
  123. */
  124. if (!i2c_gpio_debug_dir) {
  125. i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
  126. if (!i2c_gpio_debug_dir)
  127. return;
  128. }
  129. priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
  130. if (!priv->debug_dir)
  131. return;
  132. debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
  133. debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
  134. debugfs_create_file_unsafe("incomplete_transfer", 0200, priv->debug_dir,
  135. priv, &fops_incomplete_transfer);
  136. }
  137. static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
  138. {
  139. struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
  140. debugfs_remove_recursive(priv->debug_dir);
  141. }
  142. #else
  143. static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
  144. static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
  145. #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
  146. static void of_i2c_gpio_get_props(struct device_node *np,
  147. struct i2c_gpio_platform_data *pdata)
  148. {
  149. u32 reg;
  150. of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
  151. if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
  152. pdata->timeout = msecs_to_jiffies(reg);
  153. pdata->sda_is_open_drain =
  154. of_property_read_bool(np, "i2c-gpio,sda-open-drain");
  155. pdata->scl_is_open_drain =
  156. of_property_read_bool(np, "i2c-gpio,scl-open-drain");
  157. pdata->scl_is_output_only =
  158. of_property_read_bool(np, "i2c-gpio,scl-output-only");
  159. }
  160. static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
  161. const char *con_id,
  162. unsigned int index,
  163. enum gpiod_flags gflags)
  164. {
  165. struct gpio_desc *retdesc;
  166. int ret;
  167. retdesc = devm_gpiod_get(dev, con_id, gflags);
  168. if (!IS_ERR(retdesc)) {
  169. dev_dbg(dev, "got GPIO from name %s\n", con_id);
  170. return retdesc;
  171. }
  172. retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
  173. if (!IS_ERR(retdesc)) {
  174. dev_dbg(dev, "got GPIO from index %u\n", index);
  175. return retdesc;
  176. }
  177. ret = PTR_ERR(retdesc);
  178. /* FIXME: hack in the old code, is this really necessary? */
  179. if (ret == -EINVAL)
  180. retdesc = ERR_PTR(-EPROBE_DEFER);
  181. /* This happens if the GPIO driver is not yet probed, let's defer */
  182. if (ret == -ENOENT)
  183. retdesc = ERR_PTR(-EPROBE_DEFER);
  184. if (ret != -EPROBE_DEFER)
  185. dev_err(dev, "error trying to get descriptor: %d\n", ret);
  186. return retdesc;
  187. }
  188. static int i2c_gpio_probe(struct platform_device *pdev)
  189. {
  190. struct i2c_gpio_private_data *priv;
  191. struct i2c_gpio_platform_data *pdata;
  192. struct i2c_algo_bit_data *bit_data;
  193. struct i2c_adapter *adap;
  194. struct device *dev = &pdev->dev;
  195. struct device_node *np = dev->of_node;
  196. enum gpiod_flags gflags;
  197. int ret;
  198. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  199. if (!priv)
  200. return -ENOMEM;
  201. adap = &priv->adap;
  202. bit_data = &priv->bit_data;
  203. pdata = &priv->pdata;
  204. if (np) {
  205. of_i2c_gpio_get_props(np, pdata);
  206. } else {
  207. /*
  208. * If all platform data settings are zero it is OK
  209. * to not provide any platform data from the board.
  210. */
  211. if (dev_get_platdata(dev))
  212. memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
  213. }
  214. /*
  215. * First get the GPIO pins; if it fails, we'll defer the probe.
  216. * If the SDA line is marked from platform data or device tree as
  217. * "open drain" it means something outside of our control is making
  218. * this line being handled as open drain, and we should just handle
  219. * it as any other output. Else we enforce open drain as this is
  220. * required for an I2C bus.
  221. */
  222. if (pdata->sda_is_open_drain)
  223. gflags = GPIOD_OUT_HIGH;
  224. else
  225. gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
  226. priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
  227. if (IS_ERR(priv->sda))
  228. return PTR_ERR(priv->sda);
  229. /*
  230. * If the SCL line is marked from platform data or device tree as
  231. * "open drain" it means something outside of our control is making
  232. * this line being handled as open drain, and we should just handle
  233. * it as any other output. Else we enforce open drain as this is
  234. * required for an I2C bus.
  235. */
  236. if (pdata->scl_is_open_drain)
  237. gflags = GPIOD_OUT_LOW;
  238. else
  239. gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
  240. priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
  241. if (IS_ERR(priv->scl))
  242. return PTR_ERR(priv->scl);
  243. if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
  244. dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
  245. bit_data->setsda = i2c_gpio_setsda_val;
  246. bit_data->setscl = i2c_gpio_setscl_val;
  247. if (!pdata->scl_is_output_only)
  248. bit_data->getscl = i2c_gpio_getscl;
  249. bit_data->getsda = i2c_gpio_getsda;
  250. if (pdata->udelay)
  251. bit_data->udelay = pdata->udelay;
  252. else if (pdata->scl_is_output_only)
  253. bit_data->udelay = 50; /* 10 kHz */
  254. else
  255. bit_data->udelay = 5; /* 100 kHz */
  256. if (pdata->timeout)
  257. bit_data->timeout = pdata->timeout;
  258. else
  259. bit_data->timeout = HZ / 10; /* 100 ms */
  260. bit_data->data = priv;
  261. adap->owner = THIS_MODULE;
  262. if (np)
  263. strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
  264. else
  265. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  266. adap->algo_data = bit_data;
  267. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  268. adap->dev.parent = dev;
  269. adap->dev.of_node = np;
  270. adap->nr = pdev->id;
  271. ret = i2c_bit_add_numbered_bus(adap);
  272. if (ret)
  273. return ret;
  274. platform_set_drvdata(pdev, priv);
  275. /*
  276. * FIXME: using global GPIO numbers is not helpful. If/when we
  277. * get accessors to get the actual name of the GPIO line,
  278. * from the descriptor, then provide that instead.
  279. */
  280. dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
  281. desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
  282. pdata->scl_is_output_only
  283. ? ", no clock stretching" : "");
  284. i2c_gpio_fault_injector_init(pdev);
  285. return 0;
  286. }
  287. static int i2c_gpio_remove(struct platform_device *pdev)
  288. {
  289. struct i2c_gpio_private_data *priv;
  290. struct i2c_adapter *adap;
  291. i2c_gpio_fault_injector_exit(pdev);
  292. priv = platform_get_drvdata(pdev);
  293. adap = &priv->adap;
  294. i2c_del_adapter(adap);
  295. return 0;
  296. }
  297. #if defined(CONFIG_OF)
  298. static const struct of_device_id i2c_gpio_dt_ids[] = {
  299. { .compatible = "i2c-gpio", },
  300. { /* sentinel */ }
  301. };
  302. MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
  303. #endif
  304. static struct platform_driver i2c_gpio_driver = {
  305. .driver = {
  306. .name = "i2c-gpio",
  307. .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
  308. },
  309. .probe = i2c_gpio_probe,
  310. .remove = i2c_gpio_remove,
  311. };
  312. static int __init i2c_gpio_init(void)
  313. {
  314. int ret;
  315. ret = platform_driver_register(&i2c_gpio_driver);
  316. if (ret)
  317. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  318. return ret;
  319. }
  320. subsys_initcall(i2c_gpio_init);
  321. static void __exit i2c_gpio_exit(void)
  322. {
  323. platform_driver_unregister(&i2c_gpio_driver);
  324. }
  325. module_exit(i2c_gpio_exit);
  326. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  327. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  328. MODULE_LICENSE("GPL");
  329. MODULE_ALIAS("platform:i2c-gpio");