i2c-hydra.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. i2c Support for the Apple `Hydra' Mac I/O
  3. Copyright (c) 1999-2004 Geert Uytterhoeven <geert@linux-m68k.org>
  4. Based on i2c Support for Via Technologies 82C586B South Bridge
  5. Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/types.h>
  22. #include <linux/i2c.h>
  23. #include <linux/i2c-algo-bit.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <asm/hydra.h>
  27. #define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
  28. #define HYDRA_CPD_PD1 0x00000002
  29. #define HYDRA_CPD_PD2 0x00000004
  30. #define HYDRA_CPD_PD3 0x00000008
  31. #define HYDRA_SCLK HYDRA_CPD_PD0
  32. #define HYDRA_SDAT HYDRA_CPD_PD1
  33. #define HYDRA_SCLK_OE 0x00000010
  34. #define HYDRA_SDAT_OE 0x00000020
  35. static inline void pdregw(void *data, u32 val)
  36. {
  37. struct Hydra *hydra = (struct Hydra *)data;
  38. writel(val, &hydra->CachePD);
  39. }
  40. static inline u32 pdregr(void *data)
  41. {
  42. struct Hydra *hydra = (struct Hydra *)data;
  43. return readl(&hydra->CachePD);
  44. }
  45. static void hydra_bit_setscl(void *data, int state)
  46. {
  47. u32 val = pdregr(data);
  48. if (state)
  49. val &= ~HYDRA_SCLK_OE;
  50. else {
  51. val &= ~HYDRA_SCLK;
  52. val |= HYDRA_SCLK_OE;
  53. }
  54. pdregw(data, val);
  55. }
  56. static void hydra_bit_setsda(void *data, int state)
  57. {
  58. u32 val = pdregr(data);
  59. if (state)
  60. val &= ~HYDRA_SDAT_OE;
  61. else {
  62. val &= ~HYDRA_SDAT;
  63. val |= HYDRA_SDAT_OE;
  64. }
  65. pdregw(data, val);
  66. }
  67. static int hydra_bit_getscl(void *data)
  68. {
  69. return (pdregr(data) & HYDRA_SCLK) != 0;
  70. }
  71. static int hydra_bit_getsda(void *data)
  72. {
  73. return (pdregr(data) & HYDRA_SDAT) != 0;
  74. }
  75. /* ------------------------------------------------------------------------ */
  76. static struct i2c_algo_bit_data hydra_bit_data = {
  77. .setsda = hydra_bit_setsda,
  78. .setscl = hydra_bit_setscl,
  79. .getsda = hydra_bit_getsda,
  80. .getscl = hydra_bit_getscl,
  81. .udelay = 5,
  82. .timeout = HZ
  83. };
  84. static struct i2c_adapter hydra_adap = {
  85. .owner = THIS_MODULE,
  86. .name = "Hydra i2c",
  87. .algo_data = &hydra_bit_data,
  88. };
  89. static DEFINE_PCI_DEVICE_TABLE(hydra_ids) = {
  90. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
  91. { 0, }
  92. };
  93. MODULE_DEVICE_TABLE (pci, hydra_ids);
  94. static int __devinit hydra_probe(struct pci_dev *dev,
  95. const struct pci_device_id *id)
  96. {
  97. unsigned long base = pci_resource_start(dev, 0);
  98. int res;
  99. if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
  100. hydra_adap.name))
  101. return -EBUSY;
  102. hydra_bit_data.data = pci_ioremap_bar(dev, 0);
  103. if (hydra_bit_data.data == NULL) {
  104. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  105. return -ENODEV;
  106. }
  107. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  108. hydra_adap.dev.parent = &dev->dev;
  109. res = i2c_bit_add_bus(&hydra_adap);
  110. if (res < 0) {
  111. iounmap(hydra_bit_data.data);
  112. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  113. return res;
  114. }
  115. return 0;
  116. }
  117. static void __devexit hydra_remove(struct pci_dev *dev)
  118. {
  119. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  120. i2c_del_adapter(&hydra_adap);
  121. iounmap(hydra_bit_data.data);
  122. release_mem_region(pci_resource_start(dev, 0)+
  123. offsetof(struct Hydra, CachePD), 4);
  124. }
  125. static struct pci_driver hydra_driver = {
  126. .name = "hydra_smbus",
  127. .id_table = hydra_ids,
  128. .probe = hydra_probe,
  129. .remove = __devexit_p(hydra_remove),
  130. };
  131. static int __init i2c_hydra_init(void)
  132. {
  133. return pci_register_driver(&hydra_driver);
  134. }
  135. static void __exit i2c_hydra_exit(void)
  136. {
  137. pci_unregister_driver(&hydra_driver);
  138. }
  139. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  140. MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
  141. MODULE_LICENSE("GPL");
  142. module_init(i2c_hydra_init);
  143. module_exit(i2c_hydra_exit);