i2c-gpio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 __devinit 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->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  120. adap->dev.parent = &pdev->dev;
  121. /*
  122. * If "dev->id" is negative we consider it as zero.
  123. * The reason to do so is to avoid sysfs names that only make
  124. * sense when there are multiple adapters.
  125. */
  126. adap->nr = (pdev->id != -1) ? pdev->id : 0;
  127. ret = i2c_bit_add_numbered_bus(adap);
  128. if (ret)
  129. goto err_add_bus;
  130. platform_set_drvdata(pdev, adap);
  131. dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
  132. pdata->sda_pin, pdata->scl_pin,
  133. pdata->scl_is_output_only
  134. ? ", no clock stretching" : "");
  135. return 0;
  136. err_add_bus:
  137. gpio_free(pdata->scl_pin);
  138. err_request_scl:
  139. gpio_free(pdata->sda_pin);
  140. err_request_sda:
  141. kfree(bit_data);
  142. err_alloc_bit_data:
  143. kfree(adap);
  144. err_alloc_adap:
  145. return ret;
  146. }
  147. static int __devexit i2c_gpio_remove(struct platform_device *pdev)
  148. {
  149. struct i2c_gpio_platform_data *pdata;
  150. struct i2c_adapter *adap;
  151. adap = platform_get_drvdata(pdev);
  152. pdata = pdev->dev.platform_data;
  153. i2c_del_adapter(adap);
  154. gpio_free(pdata->scl_pin);
  155. gpio_free(pdata->sda_pin);
  156. kfree(adap->algo_data);
  157. kfree(adap);
  158. return 0;
  159. }
  160. static struct platform_driver i2c_gpio_driver = {
  161. .driver = {
  162. .name = "i2c-gpio",
  163. .owner = THIS_MODULE,
  164. },
  165. .probe = i2c_gpio_probe,
  166. .remove = __devexit_p(i2c_gpio_remove),
  167. };
  168. static int __init i2c_gpio_init(void)
  169. {
  170. int ret;
  171. ret = platform_driver_register(&i2c_gpio_driver);
  172. if (ret)
  173. printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
  174. return ret;
  175. }
  176. module_init(i2c_gpio_init);
  177. static void __exit i2c_gpio_exit(void)
  178. {
  179. platform_driver_unregister(&i2c_gpio_driver);
  180. }
  181. module_exit(i2c_gpio_exit);
  182. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  183. MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
  184. MODULE_LICENSE("GPL");
  185. MODULE_ALIAS("platform:i2c-gpio");