i2c-hydra.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. i2c-hydra.c - Part of lm_sensors, Linux kernel modules
  3. for hardware monitoring
  4. i2c Support for the Apple `Hydra' Mac I/O
  5. Copyright (c) 1999-2004 Geert Uytterhoeven <geert@linux-m68k.org>
  6. Based on i2c Support for Via Technologies 82C586B South Bridge
  7. Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
  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. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/types.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-algo-bit.h>
  26. #include <linux/init.h>
  27. #include <asm/io.h>
  28. #include <asm/hydra.h>
  29. #define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
  30. #define HYDRA_CPD_PD1 0x00000002
  31. #define HYDRA_CPD_PD2 0x00000004
  32. #define HYDRA_CPD_PD3 0x00000008
  33. #define HYDRA_SCLK HYDRA_CPD_PD0
  34. #define HYDRA_SDAT HYDRA_CPD_PD1
  35. #define HYDRA_SCLK_OE 0x00000010
  36. #define HYDRA_SDAT_OE 0x00000020
  37. static inline void pdregw(void *data, u32 val)
  38. {
  39. struct Hydra *hydra = (struct Hydra *)data;
  40. writel(val, &hydra->CachePD);
  41. }
  42. static inline u32 pdregr(void *data)
  43. {
  44. struct Hydra *hydra = (struct Hydra *)data;
  45. return readl(&hydra->CachePD);
  46. }
  47. static void hydra_bit_setscl(void *data, int state)
  48. {
  49. u32 val = pdregr(data);
  50. if (state)
  51. val &= ~HYDRA_SCLK_OE;
  52. else {
  53. val &= ~HYDRA_SCLK;
  54. val |= HYDRA_SCLK_OE;
  55. }
  56. pdregw(data, val);
  57. }
  58. static void hydra_bit_setsda(void *data, int state)
  59. {
  60. u32 val = pdregr(data);
  61. if (state)
  62. val &= ~HYDRA_SDAT_OE;
  63. else {
  64. val &= ~HYDRA_SDAT;
  65. val |= HYDRA_SDAT_OE;
  66. }
  67. pdregw(data, val);
  68. }
  69. static int hydra_bit_getscl(void *data)
  70. {
  71. return (pdregr(data) & HYDRA_SCLK) != 0;
  72. }
  73. static int hydra_bit_getsda(void *data)
  74. {
  75. return (pdregr(data) & HYDRA_SDAT) != 0;
  76. }
  77. /* ------------------------------------------------------------------------ */
  78. static struct i2c_algo_bit_data hydra_bit_data = {
  79. .setsda = hydra_bit_setsda,
  80. .setscl = hydra_bit_setscl,
  81. .getsda = hydra_bit_getsda,
  82. .getscl = hydra_bit_getscl,
  83. .udelay = 5,
  84. .mdelay = 5,
  85. .timeout = HZ
  86. };
  87. static struct i2c_adapter hydra_adap = {
  88. .owner = THIS_MODULE,
  89. .name = "Hydra i2c",
  90. .id = I2C_HW_B_HYDRA,
  91. .algo_data = &hydra_bit_data,
  92. };
  93. static struct pci_device_id hydra_ids[] = {
  94. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
  95. { 0, }
  96. };
  97. MODULE_DEVICE_TABLE (pci, hydra_ids);
  98. static int __devinit hydra_probe(struct pci_dev *dev,
  99. const struct pci_device_id *id)
  100. {
  101. unsigned long base = pci_resource_start(dev, 0);
  102. int res;
  103. if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
  104. hydra_adap.name))
  105. return -EBUSY;
  106. hydra_bit_data.data = ioremap(base, pci_resource_len(dev, 0));
  107. if (hydra_bit_data.data == NULL) {
  108. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  109. return -ENODEV;
  110. }
  111. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  112. hydra_adap.dev.parent = &dev->dev;
  113. res = i2c_bit_add_bus(&hydra_adap);
  114. if (res < 0) {
  115. iounmap(hydra_bit_data.data);
  116. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  117. return res;
  118. }
  119. return 0;
  120. }
  121. static void __devexit hydra_remove(struct pci_dev *dev)
  122. {
  123. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  124. i2c_bit_del_bus(&hydra_adap);
  125. iounmap(hydra_bit_data.data);
  126. release_mem_region(pci_resource_start(dev, 0)+
  127. offsetof(struct Hydra, CachePD), 4);
  128. }
  129. static struct pci_driver hydra_driver = {
  130. .name = "hydra_smbus",
  131. .id_table = hydra_ids,
  132. .probe = hydra_probe,
  133. .remove = __devexit_p(hydra_remove),
  134. };
  135. static int __init i2c_hydra_init(void)
  136. {
  137. return pci_register_driver(&hydra_driver);
  138. }
  139. static void __exit i2c_hydra_exit(void)
  140. {
  141. pci_unregister_driver(&hydra_driver);
  142. }
  143. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  144. MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
  145. MODULE_LICENSE("GPL");
  146. module_init(i2c_hydra_init);
  147. module_exit(i2c_hydra_exit);