i2c-gpio.c 5.3 KB

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