i2c-via.c 3.9 KB

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