i2c-voodoo3.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
  3. Philip Edelbrock <phil@netroedge.com>,
  4. Ralph Metzler <rjkm@thp.uni-koeln.de>, and
  5. Mark D. Studebaker <mdsxyz123@yahoo.com>
  6. Based on code written by Ralph Metzler <rjkm@thp.uni-koeln.de> and
  7. Simon Vogl
  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 Voodoo3 to gain access to
  21. the BT869 and possibly other I2C devices. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/pci.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-algo-bit.h>
  28. #include <asm/io.h>
  29. /* the only registers we use */
  30. #define REG 0x78
  31. #define REG2 0x70
  32. /* bit locations in the register */
  33. #define DDC_ENAB 0x00040000
  34. #define DDC_SCL_OUT 0x00080000
  35. #define DDC_SDA_OUT 0x00100000
  36. #define DDC_SCL_IN 0x00200000
  37. #define DDC_SDA_IN 0x00400000
  38. #define I2C_ENAB 0x00800000
  39. #define I2C_SCL_OUT 0x01000000
  40. #define I2C_SDA_OUT 0x02000000
  41. #define I2C_SCL_IN 0x04000000
  42. #define I2C_SDA_IN 0x08000000
  43. /* initialization states */
  44. #define INIT2 0x2
  45. #define INIT3 0x4
  46. /* delays */
  47. #define CYCLE_DELAY 10
  48. #define TIMEOUT (HZ / 2)
  49. static void __iomem *ioaddr;
  50. /* The voo GPIO registers don't have individual masks for each bit
  51. so we always have to read before writing. */
  52. static void bit_vooi2c_setscl(void *data, int val)
  53. {
  54. unsigned int r;
  55. r = readl(ioaddr + REG);
  56. if (val)
  57. r |= I2C_SCL_OUT;
  58. else
  59. r &= ~I2C_SCL_OUT;
  60. writel(r, ioaddr + REG);
  61. readl(ioaddr + REG); /* flush posted write */
  62. }
  63. static void bit_vooi2c_setsda(void *data, int val)
  64. {
  65. unsigned int r;
  66. r = readl(ioaddr + REG);
  67. if (val)
  68. r |= I2C_SDA_OUT;
  69. else
  70. r &= ~I2C_SDA_OUT;
  71. writel(r, ioaddr + REG);
  72. readl(ioaddr + REG); /* flush posted write */
  73. }
  74. /* The GPIO pins are open drain, so the pins always remain outputs.
  75. We rely on the i2c-algo-bit routines to set the pins high before
  76. reading the input from other chips. */
  77. static int bit_vooi2c_getscl(void *data)
  78. {
  79. return (0 != (readl(ioaddr + REG) & I2C_SCL_IN));
  80. }
  81. static int bit_vooi2c_getsda(void *data)
  82. {
  83. return (0 != (readl(ioaddr + REG) & I2C_SDA_IN));
  84. }
  85. static void bit_vooddc_setscl(void *data, int val)
  86. {
  87. unsigned int r;
  88. r = readl(ioaddr + REG);
  89. if (val)
  90. r |= DDC_SCL_OUT;
  91. else
  92. r &= ~DDC_SCL_OUT;
  93. writel(r, ioaddr + REG);
  94. readl(ioaddr + REG); /* flush posted write */
  95. }
  96. static void bit_vooddc_setsda(void *data, int val)
  97. {
  98. unsigned int r;
  99. r = readl(ioaddr + REG);
  100. if (val)
  101. r |= DDC_SDA_OUT;
  102. else
  103. r &= ~DDC_SDA_OUT;
  104. writel(r, ioaddr + REG);
  105. readl(ioaddr + REG); /* flush posted write */
  106. }
  107. static int bit_vooddc_getscl(void *data)
  108. {
  109. return (0 != (readl(ioaddr + REG) & DDC_SCL_IN));
  110. }
  111. static int bit_vooddc_getsda(void *data)
  112. {
  113. return (0 != (readl(ioaddr + REG) & DDC_SDA_IN));
  114. }
  115. static int config_v3(struct pci_dev *dev)
  116. {
  117. unsigned long cadr;
  118. /* map Voodoo3 memory */
  119. cadr = dev->resource[0].start;
  120. cadr &= PCI_BASE_ADDRESS_MEM_MASK;
  121. ioaddr = ioremap_nocache(cadr, 0x1000);
  122. if (ioaddr) {
  123. writel(0x8160, ioaddr + REG2);
  124. writel(0xcffc0020, ioaddr + REG);
  125. dev_info(&dev->dev, "Using Banshee/Voodoo3 I2C device at %p\n", ioaddr);
  126. return 0;
  127. }
  128. return -ENODEV;
  129. }
  130. static struct i2c_algo_bit_data voo_i2c_bit_data = {
  131. .setsda = bit_vooi2c_setsda,
  132. .setscl = bit_vooi2c_setscl,
  133. .getsda = bit_vooi2c_getsda,
  134. .getscl = bit_vooi2c_getscl,
  135. .udelay = CYCLE_DELAY,
  136. .timeout = TIMEOUT
  137. };
  138. static struct i2c_adapter voodoo3_i2c_adapter = {
  139. .owner = THIS_MODULE,
  140. .id = I2C_HW_B_VOO,
  141. .class = I2C_CLASS_TV_ANALOG,
  142. .name = "I2C Voodoo3/Banshee adapter",
  143. .algo_data = &voo_i2c_bit_data,
  144. };
  145. static struct i2c_algo_bit_data voo_ddc_bit_data = {
  146. .setsda = bit_vooddc_setsda,
  147. .setscl = bit_vooddc_setscl,
  148. .getsda = bit_vooddc_getsda,
  149. .getscl = bit_vooddc_getscl,
  150. .udelay = CYCLE_DELAY,
  151. .timeout = TIMEOUT
  152. };
  153. static struct i2c_adapter voodoo3_ddc_adapter = {
  154. .owner = THIS_MODULE,
  155. .id = I2C_HW_B_VOO,
  156. .class = I2C_CLASS_DDC,
  157. .name = "DDC Voodoo3/Banshee adapter",
  158. .algo_data = &voo_ddc_bit_data,
  159. };
  160. static struct pci_device_id voodoo3_ids[] __devinitdata = {
  161. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3) },
  162. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE) },
  163. { 0, }
  164. };
  165. MODULE_DEVICE_TABLE (pci, voodoo3_ids);
  166. static int __devinit voodoo3_probe(struct pci_dev *dev, const struct pci_device_id *id)
  167. {
  168. int retval;
  169. retval = config_v3(dev);
  170. if (retval)
  171. return retval;
  172. /* set up the sysfs linkage to our parent device */
  173. voodoo3_i2c_adapter.dev.parent = &dev->dev;
  174. voodoo3_ddc_adapter.dev.parent = &dev->dev;
  175. retval = i2c_bit_add_bus(&voodoo3_i2c_adapter);
  176. if (retval)
  177. return retval;
  178. retval = i2c_bit_add_bus(&voodoo3_ddc_adapter);
  179. if (retval)
  180. i2c_del_adapter(&voodoo3_i2c_adapter);
  181. return retval;
  182. }
  183. static void __devexit voodoo3_remove(struct pci_dev *dev)
  184. {
  185. i2c_del_adapter(&voodoo3_i2c_adapter);
  186. i2c_del_adapter(&voodoo3_ddc_adapter);
  187. iounmap(ioaddr);
  188. }
  189. static struct pci_driver voodoo3_driver = {
  190. .name = "voodoo3_smbus",
  191. .id_table = voodoo3_ids,
  192. .probe = voodoo3_probe,
  193. .remove = __devexit_p(voodoo3_remove),
  194. };
  195. static int __init i2c_voodoo3_init(void)
  196. {
  197. return pci_register_driver(&voodoo3_driver);
  198. }
  199. static void __exit i2c_voodoo3_exit(void)
  200. {
  201. pci_unregister_driver(&voodoo3_driver);
  202. }
  203. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  204. "Philip Edelbrock <phil@netroedge.com>, "
  205. "Ralph Metzler <rjkm@thp.uni-koeln.de>, "
  206. "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
  207. MODULE_DESCRIPTION("Voodoo3 I2C/SMBus driver");
  208. MODULE_LICENSE("GPL");
  209. module_init(i2c_voodoo3_init);
  210. module_exit(i2c_voodoo3_exit);