i2c-via.c 4.5 KB

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