i2c-i810.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. i2c-i810.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998, 1999, 2000 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. /*
  23. This interfaces to the I810/I815 to provide access to
  24. the DDC Bus and the I2C Bus.
  25. SUPPORTED DEVICES PCI ID
  26. i810AA 7121
  27. i810AB 7123
  28. i810E 7125
  29. i815 1132
  30. i845G 2562
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/pci.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-algo-bit.h>
  38. #include <asm/io.h>
  39. /* GPIO register locations */
  40. #define I810_IOCONTROL_OFFSET 0x5000
  41. #define I810_HVSYNC 0x00 /* not used */
  42. #define I810_GPIOA 0x10
  43. #define I810_GPIOB 0x14
  44. /* bit locations in the registers */
  45. #define SCL_DIR_MASK 0x0001
  46. #define SCL_DIR 0x0002
  47. #define SCL_VAL_MASK 0x0004
  48. #define SCL_VAL_OUT 0x0008
  49. #define SCL_VAL_IN 0x0010
  50. #define SDA_DIR_MASK 0x0100
  51. #define SDA_DIR 0x0200
  52. #define SDA_VAL_MASK 0x0400
  53. #define SDA_VAL_OUT 0x0800
  54. #define SDA_VAL_IN 0x1000
  55. /* initialization states */
  56. #define INIT1 0x1
  57. #define INIT2 0x2
  58. #define INIT3 0x4
  59. /* delays */
  60. #define CYCLE_DELAY 10
  61. #define TIMEOUT (HZ / 2)
  62. static void __iomem *ioaddr;
  63. /* The i810 GPIO registers have individual masks for each bit
  64. so we never have to read before writing. Nice. */
  65. static void bit_i810i2c_setscl(void *data, int val)
  66. {
  67. writel((val ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK,
  68. ioaddr + I810_GPIOB);
  69. readl(ioaddr + I810_GPIOB); /* flush posted write */
  70. }
  71. static void bit_i810i2c_setsda(void *data, int val)
  72. {
  73. writel((val ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK,
  74. ioaddr + I810_GPIOB);
  75. readl(ioaddr + I810_GPIOB); /* flush posted write */
  76. }
  77. /* The GPIO pins are open drain, so the pins could always remain outputs.
  78. However, some chip versions don't latch the inputs unless they
  79. are set as inputs.
  80. We rely on the i2c-algo-bit routines to set the pins high before
  81. reading the input from other chips. Following guidance in the 815
  82. prog. ref. guide, we do a "dummy write" of 0 to the register before
  83. reading which forces the input value to be latched. We presume this
  84. applies to the 810 as well; shouldn't hurt anyway. This is necessary to get
  85. i2c_algo_bit bit_test=1 to pass. */
  86. static int bit_i810i2c_getscl(void *data)
  87. {
  88. writel(SCL_DIR_MASK, ioaddr + I810_GPIOB);
  89. writel(0, ioaddr + I810_GPIOB);
  90. return (0 != (readl(ioaddr + I810_GPIOB) & SCL_VAL_IN));
  91. }
  92. static int bit_i810i2c_getsda(void *data)
  93. {
  94. writel(SDA_DIR_MASK, ioaddr + I810_GPIOB);
  95. writel(0, ioaddr + I810_GPIOB);
  96. return (0 != (readl(ioaddr + I810_GPIOB) & SDA_VAL_IN));
  97. }
  98. static void bit_i810ddc_setscl(void *data, int val)
  99. {
  100. writel((val ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK,
  101. ioaddr + I810_GPIOA);
  102. readl(ioaddr + I810_GPIOA); /* flush posted write */
  103. }
  104. static void bit_i810ddc_setsda(void *data, int val)
  105. {
  106. writel((val ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK,
  107. ioaddr + I810_GPIOA);
  108. readl(ioaddr + I810_GPIOA); /* flush posted write */
  109. }
  110. static int bit_i810ddc_getscl(void *data)
  111. {
  112. writel(SCL_DIR_MASK, ioaddr + I810_GPIOA);
  113. writel(0, ioaddr + I810_GPIOA);
  114. return (0 != (readl(ioaddr + I810_GPIOA) & SCL_VAL_IN));
  115. }
  116. static int bit_i810ddc_getsda(void *data)
  117. {
  118. writel(SDA_DIR_MASK, ioaddr + I810_GPIOA);
  119. writel(0, ioaddr + I810_GPIOA);
  120. return (0 != (readl(ioaddr + I810_GPIOA) & SDA_VAL_IN));
  121. }
  122. static int config_i810(struct pci_dev *dev)
  123. {
  124. unsigned long cadr;
  125. /* map I810 memory */
  126. cadr = dev->resource[1].start;
  127. cadr += I810_IOCONTROL_OFFSET;
  128. cadr &= PCI_BASE_ADDRESS_MEM_MASK;
  129. ioaddr = ioremap_nocache(cadr, 0x1000);
  130. if (ioaddr) {
  131. bit_i810i2c_setscl(NULL, 1);
  132. bit_i810i2c_setsda(NULL, 1);
  133. bit_i810ddc_setscl(NULL, 1);
  134. bit_i810ddc_setsda(NULL, 1);
  135. return 0;
  136. }
  137. return -ENODEV;
  138. }
  139. static struct i2c_algo_bit_data i810_i2c_bit_data = {
  140. .setsda = bit_i810i2c_setsda,
  141. .setscl = bit_i810i2c_setscl,
  142. .getsda = bit_i810i2c_getsda,
  143. .getscl = bit_i810i2c_getscl,
  144. .udelay = CYCLE_DELAY,
  145. .mdelay = CYCLE_DELAY,
  146. .timeout = TIMEOUT,
  147. };
  148. static struct i2c_adapter i810_i2c_adapter = {
  149. .owner = THIS_MODULE,
  150. .name = "I810/I815 I2C Adapter",
  151. .algo_data = &i810_i2c_bit_data,
  152. };
  153. static struct i2c_algo_bit_data i810_ddc_bit_data = {
  154. .setsda = bit_i810ddc_setsda,
  155. .setscl = bit_i810ddc_setscl,
  156. .getsda = bit_i810ddc_getsda,
  157. .getscl = bit_i810ddc_getscl,
  158. .udelay = CYCLE_DELAY,
  159. .mdelay = CYCLE_DELAY,
  160. .timeout = TIMEOUT,
  161. };
  162. static struct i2c_adapter i810_ddc_adapter = {
  163. .owner = THIS_MODULE,
  164. .name = "I810/I815 DDC Adapter",
  165. .algo_data = &i810_ddc_bit_data,
  166. };
  167. static struct pci_device_id i810_ids[] __devinitdata = {
  168. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG1) },
  169. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG3) },
  170. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810E_IG) },
  171. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_CGC) },
  172. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845G_IG) },
  173. { 0, },
  174. };
  175. MODULE_DEVICE_TABLE (pci, i810_ids);
  176. static int __devinit i810_probe(struct pci_dev *dev, const struct pci_device_id *id)
  177. {
  178. int retval;
  179. retval = config_i810(dev);
  180. if (retval)
  181. return retval;
  182. dev_info(&dev->dev, "i810/i815 i2c device found.\n");
  183. /* set up the sysfs linkage to our parent device */
  184. i810_i2c_adapter.dev.parent = &dev->dev;
  185. i810_ddc_adapter.dev.parent = &dev->dev;
  186. retval = i2c_bit_add_bus(&i810_i2c_adapter);
  187. if (retval)
  188. return retval;
  189. retval = i2c_bit_add_bus(&i810_ddc_adapter);
  190. if (retval)
  191. i2c_bit_del_bus(&i810_i2c_adapter);
  192. return retval;
  193. }
  194. static void __devexit i810_remove(struct pci_dev *dev)
  195. {
  196. i2c_bit_del_bus(&i810_ddc_adapter);
  197. i2c_bit_del_bus(&i810_i2c_adapter);
  198. iounmap(ioaddr);
  199. }
  200. static struct pci_driver i810_driver = {
  201. .name = "i810_smbus",
  202. .id_table = i810_ids,
  203. .probe = i810_probe,
  204. .remove = __devexit_p(i810_remove),
  205. };
  206. static int __init i2c_i810_init(void)
  207. {
  208. return pci_register_driver(&i810_driver);
  209. }
  210. static void __exit i2c_i810_exit(void)
  211. {
  212. pci_unregister_driver(&i810_driver);
  213. }
  214. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  215. "Philip Edelbrock <phil@netroedge.com>, "
  216. "Ralph Metzler <rjkm@thp.uni-koeln.de>, "
  217. "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
  218. MODULE_DESCRIPTION("I810/I815 I2C/DDC driver");
  219. MODULE_LICENSE("GPL");
  220. module_init(i2c_i810_init);
  221. module_exit(i2c_i810_exit);