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 <asm/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. .id = I2C_HW_B_HYDRA,
  88. .algo_data = &hydra_bit_data,
  89. };
  90. static struct pci_device_id hydra_ids[] = {
  91. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
  92. { 0, }
  93. };
  94. MODULE_DEVICE_TABLE (pci, hydra_ids);
  95. static int __devinit hydra_probe(struct pci_dev *dev,
  96. const struct pci_device_id *id)
  97. {
  98. unsigned long base = pci_resource_start(dev, 0);
  99. int res;
  100. if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
  101. hydra_adap.name))
  102. return -EBUSY;
  103. hydra_bit_data.data = ioremap(base, pci_resource_len(dev, 0));
  104. if (hydra_bit_data.data == NULL) {
  105. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  106. return -ENODEV;
  107. }
  108. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  109. hydra_adap.dev.parent = &dev->dev;
  110. res = i2c_bit_add_bus(&hydra_adap);
  111. if (res < 0) {
  112. iounmap(hydra_bit_data.data);
  113. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  114. return res;
  115. }
  116. return 0;
  117. }
  118. static void __devexit hydra_remove(struct pci_dev *dev)
  119. {
  120. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  121. i2c_del_adapter(&hydra_adap);
  122. iounmap(hydra_bit_data.data);
  123. release_mem_region(pci_resource_start(dev, 0)+
  124. offsetof(struct Hydra, CachePD), 4);
  125. }
  126. static struct pci_driver hydra_driver = {
  127. .name = "hydra_smbus",
  128. .id_table = hydra_ids,
  129. .probe = hydra_probe,
  130. .remove = __devexit_p(hydra_remove),
  131. };
  132. static int __init i2c_hydra_init(void)
  133. {
  134. return pci_register_driver(&hydra_driver);
  135. }
  136. static void __exit i2c_hydra_exit(void)
  137. {
  138. pci_unregister_driver(&hydra_driver);
  139. }
  140. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  141. MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
  142. MODULE_LICENSE("GPL");
  143. module_init(i2c_hydra_init);
  144. module_exit(i2c_hydra_exit);