i2c-ixp4xx.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * drivers/i2c/busses/i2c-ixp4xx.c
  3. *
  4. * Intel's IXP4xx XScale NPU chipsets (IXP420, 421, 422, 425) do not have
  5. * an on board I2C controller but provide 16 GPIO pins that are often
  6. * used to create an I2C bus. This driver provides an i2c_adapter
  7. * interface that plugs in under algo_bit and drives the GPIO pins
  8. * as instructed by the alogorithm driver.
  9. *
  10. * Author: Deepak Saxena <dsaxena@plexity.net>
  11. *
  12. * Copyright (c) 2003-2004 MontaVista Software Inc.
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. *
  18. * NOTE: Since different platforms will use different GPIO pins for
  19. * I2C, this driver uses an IXP4xx-specific platform_data
  20. * pointer to pass the GPIO numbers to the driver. This
  21. * allows us to support all the different IXP4xx platforms
  22. * w/o having to put #ifdefs in this driver.
  23. *
  24. * See arch/arm/mach-ixp4xx/ixdp425.c for an example of building a
  25. * device list and filling in the ixp4xx_i2c_pins data structure
  26. * that is passed as the platform_data to this driver.
  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 <asm/hardware.h> /* Pick up IXP4xx-specific bits */
  35. static inline int ixp4xx_scl_pin(void *data)
  36. {
  37. return ((struct ixp4xx_i2c_pins*)data)->scl_pin;
  38. }
  39. static inline int ixp4xx_sda_pin(void *data)
  40. {
  41. return ((struct ixp4xx_i2c_pins*)data)->sda_pin;
  42. }
  43. static void ixp4xx_bit_setscl(void *data, int val)
  44. {
  45. gpio_line_set(ixp4xx_scl_pin(data), 0);
  46. gpio_line_config(ixp4xx_scl_pin(data),
  47. val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
  48. }
  49. static void ixp4xx_bit_setsda(void *data, int val)
  50. {
  51. gpio_line_set(ixp4xx_sda_pin(data), 0);
  52. gpio_line_config(ixp4xx_sda_pin(data),
  53. val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
  54. }
  55. static int ixp4xx_bit_getscl(void *data)
  56. {
  57. int scl;
  58. gpio_line_config(ixp4xx_scl_pin(data), IXP4XX_GPIO_IN );
  59. gpio_line_get(ixp4xx_scl_pin(data), &scl);
  60. return scl;
  61. }
  62. static int ixp4xx_bit_getsda(void *data)
  63. {
  64. int sda;
  65. gpio_line_config(ixp4xx_sda_pin(data), IXP4XX_GPIO_IN );
  66. gpio_line_get(ixp4xx_sda_pin(data), &sda);
  67. return sda;
  68. }
  69. struct ixp4xx_i2c_data {
  70. struct ixp4xx_i2c_pins *gpio_pins;
  71. struct i2c_adapter adapter;
  72. struct i2c_algo_bit_data algo_data;
  73. };
  74. static int ixp4xx_i2c_remove(struct platform_device *plat_dev)
  75. {
  76. struct ixp4xx_i2c_data *drv_data = platform_get_drvdata(plat_dev);
  77. platform_set_drvdata(plat_dev, NULL);
  78. i2c_bit_del_bus(&drv_data->adapter);
  79. kfree(drv_data);
  80. return 0;
  81. }
  82. static int ixp4xx_i2c_probe(struct platform_device *plat_dev)
  83. {
  84. int err;
  85. struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data;
  86. struct ixp4xx_i2c_data *drv_data =
  87. kzalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL);
  88. if(!drv_data)
  89. return -ENOMEM;
  90. drv_data->gpio_pins = gpio;
  91. /*
  92. * We could make a lot of these structures static, but
  93. * certain platforms may have multiple GPIO-based I2C
  94. * buses for various device domains, so we need per-device
  95. * algo_data->data.
  96. */
  97. drv_data->algo_data.data = gpio;
  98. drv_data->algo_data.setsda = ixp4xx_bit_setsda;
  99. drv_data->algo_data.setscl = ixp4xx_bit_setscl;
  100. drv_data->algo_data.getsda = ixp4xx_bit_getsda;
  101. drv_data->algo_data.getscl = ixp4xx_bit_getscl;
  102. drv_data->algo_data.udelay = 10;
  103. drv_data->algo_data.timeout = 100;
  104. drv_data->adapter.id = I2C_HW_B_IXP4XX;
  105. drv_data->adapter.class = I2C_CLASS_HWMON;
  106. strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name,
  107. I2C_NAME_SIZE);
  108. drv_data->adapter.algo_data = &drv_data->algo_data;
  109. drv_data->adapter.dev.parent = &plat_dev->dev;
  110. gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN);
  111. gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN);
  112. gpio_line_set(gpio->scl_pin, 0);
  113. gpio_line_set(gpio->sda_pin, 0);
  114. if ((err = i2c_bit_add_bus(&drv_data->adapter) != 0)) {
  115. printk(KERN_ERR "ERROR: Could not install %s\n", plat_dev->dev.bus_id);
  116. kfree(drv_data);
  117. return err;
  118. }
  119. platform_set_drvdata(plat_dev, drv_data);
  120. return 0;
  121. }
  122. static struct platform_driver ixp4xx_i2c_driver = {
  123. .probe = ixp4xx_i2c_probe,
  124. .remove = ixp4xx_i2c_remove,
  125. .driver = {
  126. .name = "IXP4XX-I2C",
  127. .owner = THIS_MODULE,
  128. },
  129. };
  130. static int __init ixp4xx_i2c_init(void)
  131. {
  132. return platform_driver_register(&ixp4xx_i2c_driver);
  133. }
  134. static void __exit ixp4xx_i2c_exit(void)
  135. {
  136. platform_driver_unregister(&ixp4xx_i2c_driver);
  137. }
  138. module_init(ixp4xx_i2c_init);
  139. module_exit(ixp4xx_i2c_exit);
  140. MODULE_DESCRIPTION("GPIO-based I2C adapter for IXP4xx systems");
  141. MODULE_LICENSE("GPL");
  142. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");