i2c-via.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. i2c Support for Via Technologies 82C586B South Bridge
  3. Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/ioport.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-algo-bit.h>
  22. #include <linux/io.h>
  23. /* Power management registers */
  24. #define PM_CFG_REVID 0x08 /* silicon revision code */
  25. #define PM_CFG_IOBASE0 0x20
  26. #define PM_CFG_IOBASE1 0x48
  27. #define I2C_DIR (pm_io_base+0x40)
  28. #define I2C_OUT (pm_io_base+0x42)
  29. #define I2C_IN (pm_io_base+0x44)
  30. #define I2C_SCL 0x02 /* clock bit in DIR/OUT/IN register */
  31. #define I2C_SDA 0x04
  32. /* io-region reservation */
  33. #define IOSPACE 0x06
  34. static struct pci_driver vt586b_driver;
  35. static u16 pm_io_base;
  36. /*
  37. It does not appear from the datasheet that the GPIO pins are
  38. open drain. So a we set a low value by setting the direction to
  39. output and a high value by setting the direction to input and
  40. relying on the required I2C pullup. The data value is initialized
  41. to 0 in via_init() and never changed.
  42. */
  43. static void bit_via_setscl(void *data, int state)
  44. {
  45. outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
  46. }
  47. static void bit_via_setsda(void *data, int state)
  48. {
  49. outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
  50. }
  51. static int bit_via_getscl(void *data)
  52. {
  53. return (0 != (inb(I2C_IN) & I2C_SCL));
  54. }
  55. static int bit_via_getsda(void *data)
  56. {
  57. return (0 != (inb(I2C_IN) & I2C_SDA));
  58. }
  59. static struct i2c_algo_bit_data bit_data = {
  60. .setsda = bit_via_setsda,
  61. .setscl = bit_via_setscl,
  62. .getsda = bit_via_getsda,
  63. .getscl = bit_via_getscl,
  64. .udelay = 5,
  65. .timeout = HZ
  66. };
  67. static struct i2c_adapter vt586b_adapter = {
  68. .owner = THIS_MODULE,
  69. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  70. .name = "VIA i2c",
  71. .algo_data = &bit_data,
  72. };
  73. static const struct pci_device_id vt586b_ids[] = {
  74. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
  75. { 0, }
  76. };
  77. MODULE_DEVICE_TABLE (pci, vt586b_ids);
  78. static int vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
  79. {
  80. u16 base;
  81. u8 rev;
  82. int res;
  83. if (pm_io_base) {
  84. dev_err(&dev->dev, "i2c-via: Will only support one host\n");
  85. return -ENODEV;
  86. }
  87. pci_read_config_byte(dev, PM_CFG_REVID, &rev);
  88. switch (rev) {
  89. case 0x00:
  90. base = PM_CFG_IOBASE0;
  91. break;
  92. case 0x01:
  93. case 0x10:
  94. base = PM_CFG_IOBASE1;
  95. break;
  96. default:
  97. base = PM_CFG_IOBASE1;
  98. /* later revision */
  99. }
  100. pci_read_config_word(dev, base, &pm_io_base);
  101. pm_io_base &= (0xff << 8);
  102. if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
  103. dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
  104. return -ENODEV;
  105. }
  106. outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
  107. outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
  108. /* set up the sysfs linkage to our parent device */
  109. vt586b_adapter.dev.parent = &dev->dev;
  110. res = i2c_bit_add_bus(&vt586b_adapter);
  111. if ( res < 0 ) {
  112. release_region(I2C_DIR, IOSPACE);
  113. pm_io_base = 0;
  114. return res;
  115. }
  116. return 0;
  117. }
  118. static void vt586b_remove(struct pci_dev *dev)
  119. {
  120. i2c_del_adapter(&vt586b_adapter);
  121. release_region(I2C_DIR, IOSPACE);
  122. pm_io_base = 0;
  123. }
  124. static struct pci_driver vt586b_driver = {
  125. .name = "vt586b_smbus",
  126. .id_table = vt586b_ids,
  127. .probe = vt586b_probe,
  128. .remove = vt586b_remove,
  129. };
  130. module_pci_driver(vt586b_driver);
  131. MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
  132. MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
  133. MODULE_LICENSE("GPL");