i2c-gpio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 __devinit 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 __devinit 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. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  156. adap->algo_data = bit_data;
  157. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  158. adap->dev.parent = &pdev->dev;
  159. adap->dev.of_node = pdev->dev.of_node;
  160. /*
  161. * If "dev->id" is negative we consider it as zero.
  162. * The reason to do so is to avoid sysfs names that only make
  163. * sense when there are multiple adapters.
  164. */
  165. adap->nr = (pdev->id != -1) ? pdev->id : 0;
  166. ret = i2c_bit_add_numbered_bus(adap);
  167. if (ret)
  168. goto err_add_bus;
  169. of_i2c_register_devices(adap);
  170. platform_set_drvdata(pdev, priv);
  171. dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
  172. pdata->sda_pin, pdata->scl_pin,
  173. pdata->scl_is_output_only
  174. ? ", no clock stretching" : "");
  175. return 0;
  176. err_add_bus:
  177. gpio_free(pdata->scl_pin);
  178. err_request_scl:
  179. gpio_free(pdata->sda_pin);
  180. err_request_sda:
  181. return ret;
  182. }
  183. static int __devexit i2c_gpio_remove(struct platform_device *pdev)
  184. {
  185. struct i2c_gpio_private_data *priv;
  186. struct i2c_gpio_platform_data *pdata;
  187. struct i2c_adapter *adap;
  188. priv = platform_get_drvdata(pdev);
  189. adap = &priv->adap;
  190. pdata = &priv->pdata;
  191. i2c_del_adapter(adap);
  192. gpio_free(pdata->scl_pin);
  193. gpio_free(pdata->sda_pin);
  194. return 0;
  195. }
  196. #if defined(CONFIG_OF)
  197. static const struct of_device_id i2c_gpio_dt_ids[] = {
  198. { .compatible = "i2c-gpio", },
  199. { /* sentinel */ }
  200. };
  201. MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
  202. #endif
  203. static struct platform_driver i2c_gpio_driver = {
  204. .driver = {
  205. .name = "i2c-gpio",
  206. .owner = THIS_MODULE,
  207. .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
  208. },
  209. .probe = i2c_gpio_probe,
  210. .remove = __devexit_p(i2c_gpio_remove),
  211. };
  212. static int __init i2c_gpio_init(void)
  213. {
  214. int ret;
  215. ret = platform_driver_register(&i2c_gpio_driver);
  216. if (ret)
  217. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  218. return ret;
  219. }
  220. subsys_initcall(i2c_gpio_init);
  221. static void __exit i2c_gpio_exit(void)
  222. {
  223. platform_driver_unregister(&i2c_gpio_driver);
  224. }
  225. module_exit(i2c_gpio_exit);
  226. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  227. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS("platform:i2c-gpio");