i2c-via.c 4.5 KB

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