i2c-gpio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of.h>
  19. #include <linux/of_gpio.h>
  20. struct i2c_gpio_private_data {
  21. struct i2c_adapter adap;
  22. struct i2c_algo_bit_data bit_data;
  23. struct i2c_gpio_platform_data pdata;
  24. };
  25. /* Toggle SDA by changing the direction of the pin */
  26. static void i2c_gpio_setsda_dir(void *data, int state)
  27. {
  28. struct i2c_gpio_platform_data *pdata = data;
  29. if (state)
  30. gpio_direction_input(pdata->sda_pin);
  31. else
  32. gpio_direction_output(pdata->sda_pin, 0);
  33. }
  34. /*
  35. * Toggle SDA by changing the output value of the pin. This is only
  36. * valid for pins configured as open drain (i.e. setting the value
  37. * high effectively turns off the output driver.)
  38. */
  39. static void i2c_gpio_setsda_val(void *data, int state)
  40. {
  41. struct i2c_gpio_platform_data *pdata = data;
  42. gpio_set_value(pdata->sda_pin, state);
  43. }
  44. /* Toggle SCL by changing the direction of the pin. */
  45. static void i2c_gpio_setscl_dir(void *data, int state)
  46. {
  47. struct i2c_gpio_platform_data *pdata = data;
  48. if (state)
  49. gpio_direction_input(pdata->scl_pin);
  50. else
  51. gpio_direction_output(pdata->scl_pin, 0);
  52. }
  53. /*
  54. * Toggle SCL by changing the output value of the pin. This is used
  55. * for pins that are configured as open drain and for output-only
  56. * pins. The latter case will break the i2c protocol, but it will
  57. * often work in practice.
  58. */
  59. static void i2c_gpio_setscl_val(void *data, int state)
  60. {
  61. struct i2c_gpio_platform_data *pdata = data;
  62. gpio_set_value(pdata->scl_pin, state);
  63. }
  64. static int i2c_gpio_getsda(void *data)
  65. {
  66. struct i2c_gpio_platform_data *pdata = data;
  67. return gpio_get_value(pdata->sda_pin);
  68. }
  69. static int i2c_gpio_getscl(void *data)
  70. {
  71. struct i2c_gpio_platform_data *pdata = data;
  72. return gpio_get_value(pdata->scl_pin);
  73. }
  74. static int of_i2c_gpio_get_pins(struct device_node *np,
  75. unsigned int *sda_pin, unsigned int *scl_pin)
  76. {
  77. if (of_gpio_count(np) < 2)
  78. return -ENODEV;
  79. *sda_pin = of_get_gpio(np, 0);
  80. *scl_pin = of_get_gpio(np, 1);
  81. if (*sda_pin == -EPROBE_DEFER || *scl_pin == -EPROBE_DEFER)
  82. return -EPROBE_DEFER;
  83. if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
  84. pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
  85. np->full_name, *sda_pin, *scl_pin);
  86. return -ENODEV;
  87. }
  88. return 0;
  89. }
  90. static void of_i2c_gpio_get_props(struct device_node *np,
  91. struct i2c_gpio_platform_data *pdata)
  92. {
  93. u32 reg;
  94. of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
  95. if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
  96. pdata->timeout = msecs_to_jiffies(reg);
  97. pdata->sda_is_open_drain =
  98. of_property_read_bool(np, "i2c-gpio,sda-open-drain");
  99. pdata->scl_is_open_drain =
  100. of_property_read_bool(np, "i2c-gpio,scl-open-drain");
  101. pdata->scl_is_output_only =
  102. of_property_read_bool(np, "i2c-gpio,scl-output-only");
  103. }
  104. static int i2c_gpio_probe(struct platform_device *pdev)
  105. {
  106. struct i2c_gpio_private_data *priv;
  107. struct i2c_gpio_platform_data *pdata;
  108. struct i2c_algo_bit_data *bit_data;
  109. struct i2c_adapter *adap;
  110. unsigned int sda_pin, scl_pin;
  111. int ret;
  112. /* First get the GPIO pins; if it fails, we'll defer the probe. */
  113. if (pdev->dev.of_node) {
  114. ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
  115. &sda_pin, &scl_pin);
  116. if (ret)
  117. return ret;
  118. } else {
  119. if (!dev_get_platdata(&pdev->dev))
  120. return -ENXIO;
  121. pdata = dev_get_platdata(&pdev->dev);
  122. sda_pin = pdata->sda_pin;
  123. scl_pin = pdata->scl_pin;
  124. }
  125. ret = devm_gpio_request(&pdev->dev, sda_pin, "sda");
  126. if (ret) {
  127. if (ret == -EINVAL)
  128. ret = -EPROBE_DEFER; /* Try again later */
  129. return ret;
  130. }
  131. ret = devm_gpio_request(&pdev->dev, scl_pin, "scl");
  132. if (ret) {
  133. if (ret == -EINVAL)
  134. ret = -EPROBE_DEFER; /* Try again later */
  135. return ret;
  136. }
  137. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  138. if (!priv)
  139. return -ENOMEM;
  140. adap = &priv->adap;
  141. bit_data = &priv->bit_data;
  142. pdata = &priv->pdata;
  143. if (pdev->dev.of_node) {
  144. pdata->sda_pin = sda_pin;
  145. pdata->scl_pin = scl_pin;
  146. of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
  147. } else {
  148. memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
  149. }
  150. if (pdata->sda_is_open_drain) {
  151. gpio_direction_output(pdata->sda_pin, 1);
  152. bit_data->setsda = i2c_gpio_setsda_val;
  153. } else {
  154. gpio_direction_input(pdata->sda_pin);
  155. bit_data->setsda = i2c_gpio_setsda_dir;
  156. }
  157. if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
  158. gpio_direction_output(pdata->scl_pin, 1);
  159. bit_data->setscl = i2c_gpio_setscl_val;
  160. } else {
  161. gpio_direction_input(pdata->scl_pin);
  162. bit_data->setscl = i2c_gpio_setscl_dir;
  163. }
  164. if (!pdata->scl_is_output_only)
  165. bit_data->getscl = i2c_gpio_getscl;
  166. bit_data->getsda = i2c_gpio_getsda;
  167. if (pdata->udelay)
  168. bit_data->udelay = pdata->udelay;
  169. else if (pdata->scl_is_output_only)
  170. bit_data->udelay = 50; /* 10 kHz */
  171. else
  172. bit_data->udelay = 5; /* 100 kHz */
  173. if (pdata->timeout)
  174. bit_data->timeout = pdata->timeout;
  175. else
  176. bit_data->timeout = HZ / 10; /* 100 ms */
  177. bit_data->data = pdata;
  178. adap->owner = THIS_MODULE;
  179. if (pdev->dev.of_node)
  180. strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  181. else
  182. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  183. adap->algo_data = bit_data;
  184. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  185. adap->dev.parent = &pdev->dev;
  186. adap->dev.of_node = pdev->dev.of_node;
  187. adap->nr = pdev->id;
  188. ret = i2c_bit_add_numbered_bus(adap);
  189. if (ret)
  190. return ret;
  191. platform_set_drvdata(pdev, priv);
  192. dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
  193. pdata->sda_pin, pdata->scl_pin,
  194. pdata->scl_is_output_only
  195. ? ", no clock stretching" : "");
  196. return 0;
  197. }
  198. static int i2c_gpio_remove(struct platform_device *pdev)
  199. {
  200. struct i2c_gpio_private_data *priv;
  201. struct i2c_adapter *adap;
  202. priv = platform_get_drvdata(pdev);
  203. adap = &priv->adap;
  204. i2c_del_adapter(adap);
  205. return 0;
  206. }
  207. #if defined(CONFIG_OF)
  208. static const struct of_device_id i2c_gpio_dt_ids[] = {
  209. { .compatible = "i2c-gpio", },
  210. { /* sentinel */ }
  211. };
  212. MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
  213. #endif
  214. static struct platform_driver i2c_gpio_driver = {
  215. .driver = {
  216. .name = "i2c-gpio",
  217. .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
  218. },
  219. .probe = i2c_gpio_probe,
  220. .remove = i2c_gpio_remove,
  221. };
  222. static int __init i2c_gpio_init(void)
  223. {
  224. int ret;
  225. ret = platform_driver_register(&i2c_gpio_driver);
  226. if (ret)
  227. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  228. return ret;
  229. }
  230. subsys_initcall(i2c_gpio_init);
  231. static void __exit i2c_gpio_exit(void)
  232. {
  233. platform_driver_unregister(&i2c_gpio_driver);
  234. }
  235. module_exit(i2c_gpio_exit);
  236. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  237. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  238. MODULE_LICENSE("GPL");
  239. MODULE_ALIAS("platform:i2c-gpio");