i2c-ixp2000.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <linux/slab.h>
  35. #include <mach/hardware.h> /* Pick up IXP2000-specific bits */
  36. #include <mach/gpio-ixp2000.h>
  37. static inline int ixp2000_scl_pin(void *data)
  38. {
  39. return ((struct ixp2000_i2c_pins*)data)->scl_pin;
  40. }
  41. static inline int ixp2000_sda_pin(void *data)
  42. {
  43. return ((struct ixp2000_i2c_pins*)data)->sda_pin;
  44. }
  45. static void ixp2000_bit_setscl(void *data, int val)
  46. {
  47. int i = 5000;
  48. if (val) {
  49. gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
  50. while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
  51. } else {
  52. gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
  53. }
  54. }
  55. static void ixp2000_bit_setsda(void *data, int val)
  56. {
  57. if (val) {
  58. gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
  59. } else {
  60. gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
  61. }
  62. }
  63. static int ixp2000_bit_getscl(void *data)
  64. {
  65. return gpio_line_get(ixp2000_scl_pin(data));
  66. }
  67. static int ixp2000_bit_getsda(void *data)
  68. {
  69. return gpio_line_get(ixp2000_sda_pin(data));
  70. }
  71. struct ixp2000_i2c_data {
  72. struct ixp2000_i2c_pins *gpio_pins;
  73. struct i2c_adapter adapter;
  74. struct i2c_algo_bit_data algo_data;
  75. };
  76. static int ixp2000_i2c_remove(struct platform_device *plat_dev)
  77. {
  78. struct ixp2000_i2c_data *drv_data = platform_get_drvdata(plat_dev);
  79. platform_set_drvdata(plat_dev, NULL);
  80. i2c_del_adapter(&drv_data->adapter);
  81. kfree(drv_data);
  82. return 0;
  83. }
  84. static int ixp2000_i2c_probe(struct platform_device *plat_dev)
  85. {
  86. int err;
  87. struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
  88. struct ixp2000_i2c_data *drv_data =
  89. kzalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
  90. if (!drv_data)
  91. return -ENOMEM;
  92. drv_data->gpio_pins = gpio;
  93. drv_data->algo_data.data = gpio;
  94. drv_data->algo_data.setsda = ixp2000_bit_setsda;
  95. drv_data->algo_data.setscl = ixp2000_bit_setscl;
  96. drv_data->algo_data.getsda = ixp2000_bit_getsda;
  97. drv_data->algo_data.getscl = ixp2000_bit_getscl;
  98. drv_data->algo_data.udelay = 6;
  99. drv_data->algo_data.timeout = HZ;
  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. module_platform_driver(ixp2000_i2c_driver);
  125. MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
  126. MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
  127. MODULE_LICENSE("GPL");
  128. MODULE_ALIAS("platform:IXP2000-I2C");