i2c-ixp2000.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * drivers/i2c/busses/i2c-ixp2000.c
  3. *
  4. * I2C adapter for IXP2000 systems using GPIOs for I2C bus
  5. *
  6. * Author: Deepak Saxena <dsaxena@plexity.net>
  7. * Based on IXDP2400 code by: Naeem M. Afzal <naeem.m.afzal@intel.com>
  8. * Made generic by: Jeff Daly <jeffrey.daly@intel.com>
  9. *
  10. * Copyright (c) 2003-2004 MontaVista Software Inc.
  11. *
  12. * This file is licensed under the terms of the GNU General Public
  13. * License version 2. This program is licensed "as is" without any
  14. * warranty of any kind, whether express or implied.
  15. *
  16. * From Jeff Daly:
  17. *
  18. * I2C adapter driver for Intel IXDP2xxx platforms. This should work for any
  19. * IXP2000 platform if it uses the HW GPIO in the same manner. Basically,
  20. * SDA and SCL GPIOs have external pullups. Setting the respective GPIO to
  21. * an input will make the signal a '1' via the pullup. Setting them to
  22. * outputs will pull them down.
  23. *
  24. * The GPIOs are open drain signals and are used as configuration strap inputs
  25. * during power-up so there's generally a buffer on the board that needs to be
  26. * 'enabled' to drive the GPIOs.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/module.h>
  32. #include <linux/i2c.h>
  33. #include <linux/i2c-algo-bit.h>
  34. #include <mach/hardware.h> /* Pick up IXP2000-specific bits */
  35. #include <mach/gpio.h>
  36. static inline int ixp2000_scl_pin(void *data)
  37. {
  38. return ((struct ixp2000_i2c_pins*)data)->scl_pin;
  39. }
  40. static inline int ixp2000_sda_pin(void *data)
  41. {
  42. return ((struct ixp2000_i2c_pins*)data)->sda_pin;
  43. }
  44. static void ixp2000_bit_setscl(void *data, int val)
  45. {
  46. int i = 5000;
  47. if (val) {
  48. gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
  49. while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
  50. } else {
  51. gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
  52. }
  53. }
  54. static void ixp2000_bit_setsda(void *data, int val)
  55. {
  56. if (val) {
  57. gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
  58. } else {
  59. gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
  60. }
  61. }
  62. static int ixp2000_bit_getscl(void *data)
  63. {
  64. return gpio_line_get(ixp2000_scl_pin(data));
  65. }
  66. static int ixp2000_bit_getsda(void *data)
  67. {
  68. return gpio_line_get(ixp2000_sda_pin(data));
  69. }
  70. struct ixp2000_i2c_data {
  71. struct ixp2000_i2c_pins *gpio_pins;
  72. struct i2c_adapter adapter;
  73. struct i2c_algo_bit_data algo_data;
  74. };
  75. static int ixp2000_i2c_remove(struct platform_device *plat_dev)
  76. {
  77. struct ixp2000_i2c_data *drv_data = platform_get_drvdata(plat_dev);
  78. platform_set_drvdata(plat_dev, NULL);
  79. i2c_del_adapter(&drv_data->adapter);
  80. kfree(drv_data);
  81. return 0;
  82. }
  83. static int ixp2000_i2c_probe(struct platform_device *plat_dev)
  84. {
  85. int err;
  86. struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
  87. struct ixp2000_i2c_data *drv_data =
  88. kzalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
  89. if (!drv_data)
  90. return -ENOMEM;
  91. drv_data->gpio_pins = gpio;
  92. drv_data->algo_data.data = gpio;
  93. drv_data->algo_data.setsda = ixp2000_bit_setsda;
  94. drv_data->algo_data.setscl = ixp2000_bit_setscl;
  95. drv_data->algo_data.getsda = ixp2000_bit_getsda;
  96. drv_data->algo_data.getscl = ixp2000_bit_getscl;
  97. drv_data->algo_data.udelay = 6;
  98. drv_data->algo_data.timeout = 100;
  99. drv_data->adapter.id = I2C_HW_B_IXP2000,
  100. strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name,
  101. sizeof(drv_data->adapter.name));
  102. drv_data->adapter.algo_data = &drv_data->algo_data,
  103. drv_data->adapter.dev.parent = &plat_dev->dev;
  104. gpio_line_config(gpio->sda_pin, GPIO_IN);
  105. gpio_line_config(gpio->scl_pin, GPIO_IN);
  106. gpio_line_set(gpio->scl_pin, 0);
  107. gpio_line_set(gpio->sda_pin, 0);
  108. if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
  109. dev_err(&plat_dev->dev, "Could not install, error %d\n", err);
  110. kfree(drv_data);
  111. return err;
  112. }
  113. platform_set_drvdata(plat_dev, drv_data);
  114. return 0;
  115. }
  116. static struct platform_driver ixp2000_i2c_driver = {
  117. .probe = ixp2000_i2c_probe,
  118. .remove = ixp2000_i2c_remove,
  119. .driver = {
  120. .name = "IXP2000-I2C",
  121. .owner = THIS_MODULE,
  122. },
  123. };
  124. static int __init ixp2000_i2c_init(void)
  125. {
  126. return platform_driver_register(&ixp2000_i2c_driver);
  127. }
  128. static void __exit ixp2000_i2c_exit(void)
  129. {
  130. platform_driver_unregister(&ixp2000_i2c_driver);
  131. }
  132. module_init(ixp2000_i2c_init);
  133. module_exit(ixp2000_i2c_exit);
  134. MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
  135. MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
  136. MODULE_LICENSE("GPL");
  137. MODULE_ALIAS("platform:IXP2000-I2C");