/drivers/i2c/busses/i2c-voodoo3.c

https://bitbucket.org/evzijst/gittest · C · 254 lines · 179 code · 37 blank · 38 comment · 12 complexity · b2e3c8c09eaf8b9fb23f6fed38081114 MD5 · raw file

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