i2c-via.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/init.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-algo-bit.h>
  23. #include <linux/io.h>
  24. /* Power management registers */
  25. #define PM_CFG_REVID 0x08 /* silicon revision code */
  26. #define PM_CFG_IOBASE0 0x20
  27. #define PM_CFG_IOBASE1 0x48
  28. #define I2C_DIR (pm_io_base+0x40)
  29. #define I2C_OUT (pm_io_base+0x42)
  30. #define I2C_IN (pm_io_base+0x44)
  31. #define I2C_SCL 0x02 /* clock bit in DIR/OUT/IN register */
  32. #define I2C_SDA 0x04
  33. /* io-region reservation */
  34. #define IOSPACE 0x06
  35. static struct pci_driver vt586b_driver;
  36. static u16 pm_io_base;
  37. /*
  38. It does not appear from the datasheet that the GPIO pins are
  39. open drain. So a we set a low value by setting the direction to
  40. output and a high value by setting the direction to input and
  41. relying on the required I2C pullup. The data value is initialized
  42. to 0 in via_init() and never changed.
  43. */
  44. static void bit_via_setscl(void *data, int state)
  45. {
  46. outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
  47. }
  48. static void bit_via_setsda(void *data, int state)
  49. {
  50. outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
  51. }
  52. static int bit_via_getscl(void *data)
  53. {
  54. return (0 != (inb(I2C_IN) & I2C_SCL));
  55. }
  56. static int bit_via_getsda(void *data)
  57. {
  58. return (0 != (inb(I2C_IN) & I2C_SDA));
  59. }
  60. static struct i2c_algo_bit_data bit_data = {
  61. .setsda = bit_via_setsda,
  62. .setscl = bit_via_setscl,
  63. .getsda = bit_via_getsda,
  64. .getscl = bit_via_getscl,
  65. .udelay = 5,
  66. .timeout = HZ
  67. };
  68. static struct i2c_adapter vt586b_adapter = {
  69. .owner = THIS_MODULE,
  70. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  71. .name = "VIA i2c",
  72. .algo_data = &bit_data,
  73. };
  74. static DEFINE_PCI_DEVICE_TABLE(vt586b_ids) = {
  75. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
  76. { 0, }
  77. };
  78. MODULE_DEVICE_TABLE (pci, vt586b_ids);
  79. static int __devinit vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
  80. {
  81. u16 base;
  82. u8 rev;
  83. int res;
  84. if (pm_io_base) {
  85. dev_err(&dev->dev, "i2c-via: Will only support one host\n");
  86. return -ENODEV;
  87. }
  88. pci_read_config_byte(dev, PM_CFG_REVID, &rev);
  89. switch (rev) {
  90. case 0x00:
  91. base = PM_CFG_IOBASE0;
  92. break;
  93. case 0x01:
  94. case 0x10:
  95. base = PM_CFG_IOBASE1;
  96. break;
  97. default:
  98. base = PM_CFG_IOBASE1;
  99. /* later revision */
  100. }
  101. pci_read_config_word(dev, base, &pm_io_base);
  102. pm_io_base &= (0xff << 8);
  103. if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
  104. dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
  105. return -ENODEV;
  106. }
  107. outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
  108. outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
  109. /* set up the sysfs linkage to our parent device */
  110. vt586b_adapter.dev.parent = &dev->dev;
  111. res = i2c_bit_add_bus(&vt586b_adapter);
  112. if ( res < 0 ) {
  113. release_region(I2C_DIR, IOSPACE);
  114. pm_io_base = 0;
  115. return res;
  116. }
  117. return 0;
  118. }
  119. static void __devexit vt586b_remove(struct pci_dev *dev)
  120. {
  121. i2c_del_adapter(&vt586b_adapter);
  122. release_region(I2C_DIR, IOSPACE);
  123. pm_io_base = 0;
  124. }
  125. static struct pci_driver vt586b_driver = {
  126. .name = "vt586b_smbus",
  127. .id_table = vt586b_ids,
  128. .probe = vt586b_probe,
  129. .remove = __devexit_p(vt586b_remove),
  130. };
  131. static int __init i2c_vt586b_init(void)
  132. {
  133. return pci_register_driver(&vt586b_driver);
  134. }
  135. static void __exit i2c_vt586b_exit(void)
  136. {
  137. pci_unregister_driver(&vt586b_driver);
  138. }
  139. MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
  140. MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
  141. MODULE_LICENSE("GPL");
  142. module_init(i2c_vt586b_init);
  143. module_exit(i2c_vt586b_exit);