i2c-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/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>
  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_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
  74. *val = get##wire(&priv->bit_data); \
  75. i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
  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_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
  83. set##wire(&priv->bit_data, val); \
  84. i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
  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 void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
  91. u32 pattern, u8 pattern_size)
  92. {
  93. struct i2c_algo_bit_data *bit_data = &priv->bit_data;
  94. int i;
  95. i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
  96. /* START condition */
  97. setsda(bit_data, 0);
  98. udelay(bit_data->udelay);
  99. /* Send pattern, request ACK, don't send STOP */
  100. for (i = pattern_size - 1; i >= 0; i--) {
  101. setscl(bit_data, 0);
  102. udelay(bit_data->udelay / 2);
  103. setsda(bit_data, (pattern >> i) & 1);
  104. udelay((bit_data->udelay + 1) / 2);
  105. setscl(bit_data, 1);
  106. udelay(bit_data->udelay);
  107. }
  108. i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
  109. }
  110. static int fops_incomplete_addr_phase_set(void *data, u64 addr)
  111. {
  112. struct i2c_gpio_private_data *priv = data;
  113. u32 pattern;
  114. if (addr > 0x7f)
  115. return -EINVAL;
  116. /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
  117. pattern = (addr << 2) | 3;
  118. i2c_gpio_incomplete_transfer(priv, pattern, 9);
  119. return 0;
  120. }
  121. DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
  122. static int fops_incomplete_write_byte_set(void *data, u64 addr)
  123. {
  124. struct i2c_gpio_private_data *priv = data;
  125. u32 pattern;
  126. if (addr > 0x7f)
  127. return -EINVAL;
  128. /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
  129. pattern = (addr << 2) | 1;
  130. /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
  131. pattern = (pattern << 9) | 1;
  132. i2c_gpio_incomplete_transfer(priv, pattern, 18);
  133. return 0;
  134. }
  135. DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
  136. static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
  137. {
  138. struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
  139. /*
  140. * If there will be a debugfs-dir per i2c adapter somewhen, put the
  141. * 'fault-injector' dir there. Until then, we have a global dir with
  142. * all adapters as subdirs.
  143. */
  144. if (!i2c_gpio_debug_dir) {
  145. i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
  146. if (!i2c_gpio_debug_dir)
  147. return;
  148. }
  149. priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
  150. if (!priv->debug_dir)
  151. return;
  152. debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
  153. debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
  154. debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
  155. priv, &fops_incomplete_addr_phase);
  156. debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
  157. priv, &fops_incomplete_write_byte);
  158. }
  159. static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
  160. {
  161. struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
  162. debugfs_remove_recursive(priv->debug_dir);
  163. }
  164. #else
  165. static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
  166. static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
  167. #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
  168. static void of_i2c_gpio_get_props(struct device_node *np,
  169. struct i2c_gpio_platform_data *pdata)
  170. {
  171. u32 reg;
  172. of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
  173. if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
  174. pdata->timeout = msecs_to_jiffies(reg);
  175. pdata->sda_is_open_drain =
  176. of_property_read_bool(np, "i2c-gpio,sda-open-drain");
  177. pdata->scl_is_open_drain =
  178. of_property_read_bool(np, "i2c-gpio,scl-open-drain");
  179. pdata->scl_is_output_only =
  180. of_property_read_bool(np, "i2c-gpio,scl-output-only");
  181. }
  182. static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
  183. const char *con_id,
  184. unsigned int index,
  185. enum gpiod_flags gflags)
  186. {
  187. struct gpio_desc *retdesc;
  188. int ret;
  189. retdesc = devm_gpiod_get(dev, con_id, gflags);
  190. if (!IS_ERR(retdesc)) {
  191. dev_dbg(dev, "got GPIO from name %s\n", con_id);
  192. return retdesc;
  193. }
  194. retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
  195. if (!IS_ERR(retdesc)) {
  196. dev_dbg(dev, "got GPIO from index %u\n", index);
  197. return retdesc;
  198. }
  199. ret = PTR_ERR(retdesc);
  200. /* FIXME: hack in the old code, is this really necessary? */
  201. if (ret == -EINVAL)
  202. retdesc = ERR_PTR(-EPROBE_DEFER);
  203. /* This happens if the GPIO driver is not yet probed, let's defer */
  204. if (ret == -ENOENT)
  205. retdesc = ERR_PTR(-EPROBE_DEFER);
  206. if (PTR_ERR(retdesc) != -EPROBE_DEFER)
  207. dev_err(dev, "error trying to get descriptor: %d\n", ret);
  208. return retdesc;
  209. }
  210. static int i2c_gpio_probe(struct platform_device *pdev)
  211. {
  212. struct i2c_gpio_private_data *priv;
  213. struct i2c_gpio_platform_data *pdata;
  214. struct i2c_algo_bit_data *bit_data;
  215. struct i2c_adapter *adap;
  216. struct device *dev = &pdev->dev;
  217. struct device_node *np = dev->of_node;
  218. enum gpiod_flags gflags;
  219. int ret;
  220. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  221. if (!priv)
  222. return -ENOMEM;
  223. adap = &priv->adap;
  224. bit_data = &priv->bit_data;
  225. pdata = &priv->pdata;
  226. if (np) {
  227. of_i2c_gpio_get_props(np, pdata);
  228. } else {
  229. /*
  230. * If all platform data settings are zero it is OK
  231. * to not provide any platform data from the board.
  232. */
  233. if (dev_get_platdata(dev))
  234. memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
  235. }
  236. /*
  237. * First get the GPIO pins; if it fails, we'll defer the probe.
  238. * If the SDA line is marked from platform data or device tree as
  239. * "open drain" it means something outside of our control is making
  240. * this line being handled as open drain, and we should just handle
  241. * it as any other output. Else we enforce open drain as this is
  242. * required for an I2C bus.
  243. */
  244. if (pdata->sda_is_open_drain)
  245. gflags = GPIOD_OUT_HIGH;
  246. else
  247. gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
  248. priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
  249. if (IS_ERR(priv->sda))
  250. return PTR_ERR(priv->sda);
  251. /*
  252. * If the SCL line is marked from platform data or device tree as
  253. * "open drain" it means something outside of our control is making
  254. * this line being handled as open drain, and we should just handle
  255. * it as any other output. Else we enforce open drain as this is
  256. * required for an I2C bus.
  257. */
  258. if (pdata->scl_is_open_drain)
  259. gflags = GPIOD_OUT_HIGH;
  260. else
  261. gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
  262. priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
  263. if (IS_ERR(priv->scl))
  264. return PTR_ERR(priv->scl);
  265. if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
  266. dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
  267. bit_data->setsda = i2c_gpio_setsda_val;
  268. bit_data->setscl = i2c_gpio_setscl_val;
  269. if (!pdata->scl_is_output_only)
  270. bit_data->getscl = i2c_gpio_getscl;
  271. bit_data->getsda = i2c_gpio_getsda;
  272. if (pdata->udelay)
  273. bit_data->udelay = pdata->udelay;
  274. else if (pdata->scl_is_output_only)
  275. bit_data->udelay = 50; /* 10 kHz */
  276. else
  277. bit_data->udelay = 5; /* 100 kHz */
  278. if (pdata->timeout)
  279. bit_data->timeout = pdata->timeout;
  280. else
  281. bit_data->timeout = HZ / 10; /* 100 ms */
  282. bit_data->data = priv;
  283. adap->owner = THIS_MODULE;
  284. if (np)
  285. strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
  286. else
  287. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  288. adap->algo_data = bit_data;
  289. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  290. adap->dev.parent = dev;
  291. adap->dev.of_node = np;
  292. adap->nr = pdev->id;
  293. ret = i2c_bit_add_numbered_bus(adap);
  294. if (ret)
  295. return ret;
  296. platform_set_drvdata(pdev, priv);
  297. /*
  298. * FIXME: using global GPIO numbers is not helpful. If/when we
  299. * get accessors to get the actual name of the GPIO line,
  300. * from the descriptor, then provide that instead.
  301. */
  302. dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
  303. desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
  304. pdata->scl_is_output_only
  305. ? ", no clock stretching" : "");
  306. i2c_gpio_fault_injector_init(pdev);
  307. return 0;
  308. }
  309. static int i2c_gpio_remove(struct platform_device *pdev)
  310. {
  311. struct i2c_gpio_private_data *priv;
  312. struct i2c_adapter *adap;
  313. i2c_gpio_fault_injector_exit(pdev);
  314. priv = platform_get_drvdata(pdev);
  315. adap = &priv->adap;
  316. i2c_del_adapter(adap);
  317. return 0;
  318. }
  319. #if defined(CONFIG_OF)
  320. static const struct of_device_id i2c_gpio_dt_ids[] = {
  321. { .compatible = "i2c-gpio", },
  322. { /* sentinel */ }
  323. };
  324. MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
  325. #endif
  326. static struct platform_driver i2c_gpio_driver = {
  327. .driver = {
  328. .name = "i2c-gpio",
  329. .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
  330. },
  331. .probe = i2c_gpio_probe,
  332. .remove = i2c_gpio_remove,
  333. };
  334. static int __init i2c_gpio_init(void)
  335. {
  336. int ret;
  337. ret = platform_driver_register(&i2c_gpio_driver);
  338. if (ret)
  339. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  340. return ret;
  341. }
  342. subsys_initcall(i2c_gpio_init);
  343. static void __exit i2c_gpio_exit(void)
  344. {
  345. platform_driver_unregister(&i2c_gpio_driver);
  346. }
  347. module_exit(i2c_gpio_exit);
  348. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  349. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  350. MODULE_LICENSE("GPL");
  351. MODULE_ALIAS("platform:i2c-gpio");