i2c-gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/platform_device.h>
  16. #include <asm/gpio.h>
  17. /* Toggle SDA by changing the direction of the pin */
  18. static void i2c_gpio_setsda_dir(void *data, int state)
  19. {
  20. struct i2c_gpio_platform_data *pdata = data;
  21. if (state)
  22. gpio_direction_input(pdata->sda_pin);
  23. else
  24. gpio_direction_output(pdata->sda_pin, 0);
  25. }
  26. /*
  27. * Toggle SDA by changing the output value of the pin. This is only
  28. * valid for pins configured as open drain (i.e. setting the value
  29. * high effectively turns off the output driver.)
  30. */
  31. static void i2c_gpio_setsda_val(void *data, int state)
  32. {
  33. struct i2c_gpio_platform_data *pdata = data;
  34. gpio_set_value(pdata->sda_pin, state);
  35. }
  36. /* Toggle SCL by changing the direction of the pin. */
  37. static void i2c_gpio_setscl_dir(void *data, int state)
  38. {
  39. struct i2c_gpio_platform_data *pdata = data;
  40. if (state)
  41. gpio_direction_input(pdata->scl_pin);
  42. else
  43. gpio_direction_output(pdata->scl_pin, 0);
  44. }
  45. /*
  46. * Toggle SCL by changing the output value of the pin. This is used
  47. * for pins that are configured as open drain and for output-only
  48. * pins. The latter case will break the i2c protocol, but it will
  49. * often work in practice.
  50. */
  51. static void i2c_gpio_setscl_val(void *data, int state)
  52. {
  53. struct i2c_gpio_platform_data *pdata = data;
  54. gpio_set_value(pdata->scl_pin, state);
  55. }
  56. static int i2c_gpio_getsda(void *data)
  57. {
  58. struct i2c_gpio_platform_data *pdata = data;
  59. return gpio_get_value(pdata->sda_pin);
  60. }
  61. static int i2c_gpio_getscl(void *data)
  62. {
  63. struct i2c_gpio_platform_data *pdata = data;
  64. return gpio_get_value(pdata->scl_pin);
  65. }
  66. static int __init i2c_gpio_probe(struct platform_device *pdev)
  67. {
  68. struct i2c_gpio_platform_data *pdata;
  69. struct i2c_algo_bit_data *bit_data;
  70. struct i2c_adapter *adap;
  71. int ret;
  72. pdata = pdev->dev.platform_data;
  73. if (!pdata)
  74. return -ENXIO;
  75. ret = -ENOMEM;
  76. adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  77. if (!adap)
  78. goto err_alloc_adap;
  79. bit_data = kzalloc(sizeof(struct i2c_algo_bit_data), GFP_KERNEL);
  80. if (!bit_data)
  81. goto err_alloc_bit_data;
  82. ret = gpio_request(pdata->sda_pin, "sda");
  83. if (ret)
  84. goto err_request_sda;
  85. ret = gpio_request(pdata->scl_pin, "scl");
  86. if (ret)
  87. goto err_request_scl;
  88. if (pdata->sda_is_open_drain) {
  89. gpio_direction_output(pdata->sda_pin, 1);
  90. bit_data->setsda = i2c_gpio_setsda_val;
  91. } else {
  92. gpio_direction_input(pdata->sda_pin);
  93. bit_data->setsda = i2c_gpio_setsda_dir;
  94. }
  95. if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
  96. gpio_direction_output(pdata->scl_pin, 1);
  97. bit_data->setscl = i2c_gpio_setscl_val;
  98. } else {
  99. gpio_direction_input(pdata->scl_pin);
  100. bit_data->setscl = i2c_gpio_setscl_dir;
  101. }
  102. if (!pdata->scl_is_output_only)
  103. bit_data->getscl = i2c_gpio_getscl;
  104. bit_data->getsda = i2c_gpio_getsda;
  105. if (pdata->udelay)
  106. bit_data->udelay = pdata->udelay;
  107. else if (pdata->scl_is_output_only)
  108. bit_data->udelay = 50; /* 10 kHz */
  109. else
  110. bit_data->udelay = 5; /* 100 kHz */
  111. if (pdata->timeout)
  112. bit_data->timeout = pdata->timeout;
  113. else
  114. bit_data->timeout = HZ / 10; /* 100 ms */
  115. bit_data->data = pdata;
  116. adap->owner = THIS_MODULE;
  117. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
  118. adap->algo_data = bit_data;
  119. adap->dev.parent = &pdev->dev;
  120. /*
  121. * If "dev->id" is negative we consider it as zero.
  122. * The reason to do so is to avoid sysfs names that only make
  123. * sense when there are multiple adapters.
  124. */
  125. adap->nr = (pdev->id != -1) ? pdev->id : 0;
  126. ret = i2c_bit_add_numbered_bus(adap);
  127. if (ret)
  128. goto err_add_bus;
  129. platform_set_drvdata(pdev, adap);
  130. dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
  131. pdata->sda_pin, pdata->scl_pin,
  132. pdata->scl_is_output_only
  133. ? ", no clock stretching" : "");
  134. return 0;
  135. err_add_bus:
  136. gpio_free(pdata->scl_pin);
  137. err_request_scl:
  138. gpio_free(pdata->sda_pin);
  139. err_request_sda:
  140. kfree(bit_data);
  141. err_alloc_bit_data:
  142. kfree(adap);
  143. err_alloc_adap:
  144. return ret;
  145. }
  146. static int __exit i2c_gpio_remove(struct platform_device *pdev)
  147. {
  148. struct i2c_gpio_platform_data *pdata;
  149. struct i2c_adapter *adap;
  150. adap = platform_get_drvdata(pdev);
  151. pdata = pdev->dev.platform_data;
  152. i2c_del_adapter(adap);
  153. gpio_free(pdata->scl_pin);
  154. gpio_free(pdata->sda_pin);
  155. kfree(adap->algo_data);
  156. kfree(adap);
  157. return 0;
  158. }
  159. static struct platform_driver i2c_gpio_driver = {
  160. .driver = {
  161. .name = "i2c-gpio",
  162. .owner = THIS_MODULE,
  163. },
  164. .remove = __exit_p(i2c_gpio_remove),
  165. };
  166. static int __init i2c_gpio_init(void)
  167. {
  168. int ret;
  169. ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe);
  170. if (ret)
  171. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  172. return ret;
  173. }
  174. module_init(i2c_gpio_init);
  175. static void __exit i2c_gpio_exit(void)
  176. {
  177. platform_driver_unregister(&i2c_gpio_driver);
  178. }
  179. module_exit(i2c_gpio_exit);
  180. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  181. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  182. MODULE_LICENSE("GPL");