i2c-savage4.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. i2c-savage4.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 1998-2003 The LM Sensors Team
  5. Alexander Wold <awold@bigfoot.com>
  6. Mark D. Studebaker <mdsxyz123@yahoo.com>
  7. Based on i2c-voodoo3.c.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /* This interfaces to the I2C bus of the Savage4 to gain access to
  21. the BT869 and possibly other I2C devices. The DDC bus is not
  22. yet supported because its register is not memory-mapped.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/pci.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-algo-bit.h>
  30. #include <asm/io.h>
  31. /* device IDs */
  32. #define PCI_CHIP_SAVAGE4 0x8A22
  33. #define PCI_CHIP_SAVAGE2000 0x9102
  34. #define REG 0xff20 /* Serial Port 1 Register */
  35. /* bit locations in the register */
  36. #define I2C_ENAB 0x00000020
  37. #define I2C_SCL_OUT 0x00000001
  38. #define I2C_SDA_OUT 0x00000002
  39. #define I2C_SCL_IN 0x00000008
  40. #define I2C_SDA_IN 0x00000010
  41. /* delays */
  42. #define CYCLE_DELAY 10
  43. #define TIMEOUT (HZ / 2)
  44. static void __iomem *ioaddr;
  45. /* The sav GPIO registers don't have individual masks for each bit
  46. so we always have to read before writing. */
  47. static void bit_savi2c_setscl(void *data, int val)
  48. {
  49. unsigned int r;
  50. r = readl(ioaddr + REG);
  51. if(val)
  52. r |= I2C_SCL_OUT;
  53. else
  54. r &= ~I2C_SCL_OUT;
  55. writel(r, ioaddr + REG);
  56. readl(ioaddr + REG); /* flush posted write */
  57. }
  58. static void bit_savi2c_setsda(void *data, int val)
  59. {
  60. unsigned int r;
  61. r = readl(ioaddr + REG);
  62. if(val)
  63. r |= I2C_SDA_OUT;
  64. else
  65. r &= ~I2C_SDA_OUT;
  66. writel(r, ioaddr + REG);
  67. readl(ioaddr + REG); /* flush posted write */
  68. }
  69. /* The GPIO pins are open drain, so the pins always remain outputs.
  70. We rely on the i2c-algo-bit routines to set the pins high before
  71. reading the input from other chips. */
  72. static int bit_savi2c_getscl(void *data)
  73. {
  74. return (0 != (readl(ioaddr + REG) & I2C_SCL_IN));
  75. }
  76. static int bit_savi2c_getsda(void *data)
  77. {
  78. return (0 != (readl(ioaddr + REG) & I2C_SDA_IN));
  79. }
  80. /* Configures the chip */
  81. static int config_s4(struct pci_dev *dev)
  82. {
  83. unsigned long cadr;
  84. /* map memory */
  85. cadr = dev->resource[0].start;
  86. cadr &= PCI_BASE_ADDRESS_MEM_MASK;
  87. ioaddr = ioremap_nocache(cadr, 0x0080000);
  88. if (ioaddr) {
  89. /* writel(0x8160, ioaddr + REG2); */
  90. writel(0x00000020, ioaddr + REG);
  91. dev_info(&dev->dev, "Using Savage4 at %p\n", ioaddr);
  92. return 0;
  93. }
  94. return -ENODEV;
  95. }
  96. static struct i2c_algo_bit_data sav_i2c_bit_data = {
  97. .setsda = bit_savi2c_setsda,
  98. .setscl = bit_savi2c_setscl,
  99. .getsda = bit_savi2c_getsda,
  100. .getscl = bit_savi2c_getscl,
  101. .udelay = CYCLE_DELAY,
  102. .timeout = TIMEOUT
  103. };
  104. static struct i2c_adapter savage4_i2c_adapter = {
  105. .owner = THIS_MODULE,
  106. .id = I2C_HW_B_SAVAGE,
  107. .name = "I2C Savage4 adapter",
  108. .algo_data = &sav_i2c_bit_data,
  109. };
  110. static struct pci_device_id savage4_ids[] __devinitdata = {
  111. { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE4) },
  112. { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE2000) },
  113. { 0, }
  114. };
  115. MODULE_DEVICE_TABLE (pci, savage4_ids);
  116. static int __devinit savage4_probe(struct pci_dev *dev, const struct pci_device_id *id)
  117. {
  118. int retval;
  119. retval = config_s4(dev);
  120. if (retval)
  121. return retval;
  122. /* set up the sysfs linkage to our parent device */
  123. savage4_i2c_adapter.dev.parent = &dev->dev;
  124. return i2c_bit_add_bus(&savage4_i2c_adapter);
  125. }
  126. static void __devexit savage4_remove(struct pci_dev *dev)
  127. {
  128. i2c_del_adapter(&savage4_i2c_adapter);
  129. iounmap(ioaddr);
  130. }
  131. static struct pci_driver savage4_driver = {
  132. .name = "savage4_smbus",
  133. .id_table = savage4_ids,
  134. .probe = savage4_probe,
  135. .remove = __devexit_p(savage4_remove),
  136. };
  137. static int __init i2c_savage4_init(void)
  138. {
  139. return pci_register_driver(&savage4_driver);
  140. }
  141. static void __exit i2c_savage4_exit(void)
  142. {
  143. pci_unregister_driver(&savage4_driver);
  144. }
  145. MODULE_AUTHOR("Alexander Wold <awold@bigfoot.com> "
  146. "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
  147. MODULE_DESCRIPTION("Savage4 I2C/SMBus driver");
  148. MODULE_LICENSE("GPL");
  149. module_init(i2c_savage4_init);
  150. module_exit(i2c_savage4_exit);