i2c-hydra.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/io.h>
  25. #include <asm/hydra.h>
  26. #define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
  27. #define HYDRA_CPD_PD1 0x00000002
  28. #define HYDRA_CPD_PD2 0x00000004
  29. #define HYDRA_CPD_PD3 0x00000008
  30. #define HYDRA_SCLK HYDRA_CPD_PD0
  31. #define HYDRA_SDAT HYDRA_CPD_PD1
  32. #define HYDRA_SCLK_OE 0x00000010
  33. #define HYDRA_SDAT_OE 0x00000020
  34. static inline void pdregw(void *data, u32 val)
  35. {
  36. struct Hydra *hydra = (struct Hydra *)data;
  37. writel(val, &hydra->CachePD);
  38. }
  39. static inline u32 pdregr(void *data)
  40. {
  41. struct Hydra *hydra = (struct Hydra *)data;
  42. return readl(&hydra->CachePD);
  43. }
  44. static void hydra_bit_setscl(void *data, int state)
  45. {
  46. u32 val = pdregr(data);
  47. if (state)
  48. val &= ~HYDRA_SCLK_OE;
  49. else {
  50. val &= ~HYDRA_SCLK;
  51. val |= HYDRA_SCLK_OE;
  52. }
  53. pdregw(data, val);
  54. }
  55. static void hydra_bit_setsda(void *data, int state)
  56. {
  57. u32 val = pdregr(data);
  58. if (state)
  59. val &= ~HYDRA_SDAT_OE;
  60. else {
  61. val &= ~HYDRA_SDAT;
  62. val |= HYDRA_SDAT_OE;
  63. }
  64. pdregw(data, val);
  65. }
  66. static int hydra_bit_getscl(void *data)
  67. {
  68. return (pdregr(data) & HYDRA_SCLK) != 0;
  69. }
  70. static int hydra_bit_getsda(void *data)
  71. {
  72. return (pdregr(data) & HYDRA_SDAT) != 0;
  73. }
  74. /* ------------------------------------------------------------------------ */
  75. static struct i2c_algo_bit_data hydra_bit_data = {
  76. .setsda = hydra_bit_setsda,
  77. .setscl = hydra_bit_setscl,
  78. .getsda = hydra_bit_getsda,
  79. .getscl = hydra_bit_getscl,
  80. .udelay = 5,
  81. .timeout = HZ
  82. };
  83. static struct i2c_adapter hydra_adap = {
  84. .owner = THIS_MODULE,
  85. .name = "Hydra i2c",
  86. .algo_data = &hydra_bit_data,
  87. };
  88. static const struct pci_device_id hydra_ids[] = {
  89. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
  90. { 0, }
  91. };
  92. MODULE_DEVICE_TABLE (pci, hydra_ids);
  93. static int hydra_probe(struct pci_dev *dev,
  94. const struct pci_device_id *id)
  95. {
  96. unsigned long base = pci_resource_start(dev, 0);
  97. int res;
  98. if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
  99. hydra_adap.name))
  100. return -EBUSY;
  101. hydra_bit_data.data = pci_ioremap_bar(dev, 0);
  102. if (hydra_bit_data.data == NULL) {
  103. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  104. return -ENODEV;
  105. }
  106. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  107. hydra_adap.dev.parent = &dev->dev;
  108. res = i2c_bit_add_bus(&hydra_adap);
  109. if (res < 0) {
  110. iounmap(hydra_bit_data.data);
  111. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  112. return res;
  113. }
  114. return 0;
  115. }
  116. static void hydra_remove(struct pci_dev *dev)
  117. {
  118. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  119. i2c_del_adapter(&hydra_adap);
  120. iounmap(hydra_bit_data.data);
  121. release_mem_region(pci_resource_start(dev, 0)+
  122. offsetof(struct Hydra, CachePD), 4);
  123. }
  124. static struct pci_driver hydra_driver = {
  125. .name = "hydra_smbus",
  126. .id_table = hydra_ids,
  127. .probe = hydra_probe,
  128. .remove = hydra_remove,
  129. };
  130. module_pci_driver(hydra_driver);
  131. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  132. MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
  133. MODULE_LICENSE("GPL");