i2c-gpio.c 7.1 KB

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