i2c-gpio.c 6.8 KB

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