i2c-gpio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include <linux/of_i2c.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_probe(struct device_node *np,
  75. struct i2c_gpio_platform_data *pdata)
  76. {
  77. u32 reg;
  78. if (of_gpio_count(np) < 2)
  79. return -ENODEV;
  80. pdata->sda_pin = of_get_gpio(np, 0);
  81. pdata->scl_pin = of_get_gpio(np, 1);
  82. if (!gpio_is_valid(pdata->sda_pin) || !gpio_is_valid(pdata->scl_pin)) {
  83. pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
  84. np->full_name, pdata->sda_pin, pdata->scl_pin);
  85. return -ENODEV;
  86. }
  87. of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
  88. if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
  89. pdata->timeout = msecs_to_jiffies(reg);
  90. pdata->sda_is_open_drain =
  91. of_property_read_bool(np, "i2c-gpio,sda-open-drain");
  92. pdata->scl_is_open_drain =
  93. of_property_read_bool(np, "i2c-gpio,scl-open-drain");
  94. pdata->scl_is_output_only =
  95. of_property_read_bool(np, "i2c-gpio,scl-output-only");
  96. return 0;
  97. }
  98. static int i2c_gpio_probe(struct platform_device *pdev)
  99. {
  100. struct i2c_gpio_private_data *priv;
  101. struct i2c_gpio_platform_data *pdata;
  102. struct i2c_algo_bit_data *bit_data;
  103. struct i2c_adapter *adap;
  104. int ret;
  105. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  106. if (!priv)
  107. return -ENOMEM;
  108. adap = &priv->adap;
  109. bit_data = &priv->bit_data;
  110. pdata = &priv->pdata;
  111. if (pdev->dev.of_node) {
  112. ret = of_i2c_gpio_probe(pdev->dev.of_node, pdata);
  113. if (ret)
  114. return ret;
  115. } else {
  116. if (!pdev->dev.platform_data)
  117. return -ENXIO;
  118. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  119. }
  120. ret = gpio_request(pdata->sda_pin, "sda");
  121. if (ret)
  122. goto err_request_sda;
  123. ret = gpio_request(pdata->scl_pin, "scl");
  124. if (ret)
  125. goto err_request_scl;
  126. if (pdata->sda_is_open_drain) {
  127. gpio_direction_output(pdata->sda_pin, 1);
  128. bit_data->setsda = i2c_gpio_setsda_val;
  129. } else {
  130. gpio_direction_input(pdata->sda_pin);
  131. bit_data->setsda = i2c_gpio_setsda_dir;
  132. }
  133. if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
  134. gpio_direction_output(pdata->scl_pin, 1);
  135. bit_data->setscl = i2c_gpio_setscl_val;
  136. } else {
  137. gpio_direction_input(pdata->scl_pin);
  138. bit_data->setscl = i2c_gpio_setscl_dir;
  139. }
  140. if (!pdata->scl_is_output_only)
  141. bit_data->getscl = i2c_gpio_getscl;
  142. bit_data->getsda = i2c_gpio_getsda;
  143. if (pdata->udelay)
  144. bit_data->udelay = pdata->udelay;
  145. else if (pdata->scl_is_output_only)
  146. bit_data->udelay = 50; /* 10 kHz */
  147. else
  148. bit_data->udelay = 5; /* 100 kHz */
  149. if (pdata->timeout)
  150. bit_data->timeout = pdata->timeout;
  151. else
  152. bit_data->timeout = HZ / 10; /* 100 ms */
  153. bit_data->data = pdata;
  154. adap->owner = THIS_MODULE;
  155. if (pdev->dev.of_node)
  156. strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  157. else
  158. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  159. adap->algo_data = bit_data;
  160. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  161. adap->dev.parent = &pdev->dev;
  162. adap->dev.of_node = pdev->dev.of_node;
  163. adap->nr = pdev->id;
  164. ret = i2c_bit_add_numbered_bus(adap);
  165. if (ret)
  166. goto err_add_bus;
  167. of_i2c_register_devices(adap);
  168. platform_set_drvdata(pdev, priv);
  169. dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
  170. pdata->sda_pin, pdata->scl_pin,
  171. pdata->scl_is_output_only
  172. ? ", no clock stretching" : "");
  173. return 0;
  174. err_add_bus:
  175. gpio_free(pdata->scl_pin);
  176. err_request_scl:
  177. gpio_free(pdata->sda_pin);
  178. err_request_sda:
  179. return ret;
  180. }
  181. static int i2c_gpio_remove(struct platform_device *pdev)
  182. {
  183. struct i2c_gpio_private_data *priv;
  184. struct i2c_gpio_platform_data *pdata;
  185. struct i2c_adapter *adap;
  186. priv = platform_get_drvdata(pdev);
  187. adap = &priv->adap;
  188. pdata = &priv->pdata;
  189. i2c_del_adapter(adap);
  190. gpio_free(pdata->scl_pin);
  191. gpio_free(pdata->sda_pin);
  192. return 0;
  193. }
  194. #if defined(CONFIG_OF)
  195. static const struct of_device_id i2c_gpio_dt_ids[] = {
  196. { .compatible = "i2c-gpio", },
  197. { /* sentinel */ }
  198. };
  199. MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
  200. #endif
  201. static struct platform_driver i2c_gpio_driver = {
  202. .driver = {
  203. .name = "i2c-gpio",
  204. .owner = THIS_MODULE,
  205. .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
  206. },
  207. .probe = i2c_gpio_probe,
  208. .remove = i2c_gpio_remove,
  209. };
  210. static int __init i2c_gpio_init(void)
  211. {
  212. int ret;
  213. ret = platform_driver_register(&i2c_gpio_driver);
  214. if (ret)
  215. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  216. return ret;
  217. }
  218. subsys_initcall(i2c_gpio_init);
  219. static void __exit i2c_gpio_exit(void)
  220. {
  221. platform_driver_unregister(&i2c_gpio_driver);
  222. }
  223. module_exit(i2c_gpio_exit);
  224. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  225. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  226. MODULE_LICENSE("GPL");
  227. MODULE_ALIAS("platform:i2c-gpio");