i2c-hydra.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. i2c Support for the Apple `Hydra' Mac I/O
  4. Copyright (c) 1999-2004 Geert Uytterhoeven <[email protected]>
  5. Based on i2c Support for Via Technologies 82C586B South Bridge
  6. Copyright (c) 1998, 1999 Kyösti Mälkki <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/types.h>
  12. #include <linux/i2c.h>
  13. #include <linux/i2c-algo-bit.h>
  14. #include <linux/io.h>
  15. #include <asm/hydra.h>
  16. #define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
  17. #define HYDRA_CPD_PD1 0x00000002
  18. #define HYDRA_CPD_PD2 0x00000004
  19. #define HYDRA_CPD_PD3 0x00000008
  20. #define HYDRA_SCLK HYDRA_CPD_PD0
  21. #define HYDRA_SDAT HYDRA_CPD_PD1
  22. #define HYDRA_SCLK_OE 0x00000010
  23. #define HYDRA_SDAT_OE 0x00000020
  24. static inline void pdregw(void *data, u32 val)
  25. {
  26. struct Hydra *hydra = (struct Hydra *)data;
  27. writel(val, &hydra->CachePD);
  28. }
  29. static inline u32 pdregr(void *data)
  30. {
  31. struct Hydra *hydra = (struct Hydra *)data;
  32. return readl(&hydra->CachePD);
  33. }
  34. static void hydra_bit_setscl(void *data, int state)
  35. {
  36. u32 val = pdregr(data);
  37. if (state)
  38. val &= ~HYDRA_SCLK_OE;
  39. else {
  40. val &= ~HYDRA_SCLK;
  41. val |= HYDRA_SCLK_OE;
  42. }
  43. pdregw(data, val);
  44. }
  45. static void hydra_bit_setsda(void *data, int state)
  46. {
  47. u32 val = pdregr(data);
  48. if (state)
  49. val &= ~HYDRA_SDAT_OE;
  50. else {
  51. val &= ~HYDRA_SDAT;
  52. val |= HYDRA_SDAT_OE;
  53. }
  54. pdregw(data, val);
  55. }
  56. static int hydra_bit_getscl(void *data)
  57. {
  58. return (pdregr(data) & HYDRA_SCLK) != 0;
  59. }
  60. static int hydra_bit_getsda(void *data)
  61. {
  62. return (pdregr(data) & HYDRA_SDAT) != 0;
  63. }
  64. /* ------------------------------------------------------------------------ */
  65. static struct i2c_algo_bit_data hydra_bit_data = {
  66. .setsda = hydra_bit_setsda,
  67. .setscl = hydra_bit_setscl,
  68. .getsda = hydra_bit_getsda,
  69. .getscl = hydra_bit_getscl,
  70. .udelay = 5,
  71. .timeout = HZ
  72. };
  73. static struct i2c_adapter hydra_adap = {
  74. .owner = THIS_MODULE,
  75. .name = "Hydra i2c",
  76. .algo_data = &hydra_bit_data,
  77. };
  78. static const struct pci_device_id hydra_ids[] = {
  79. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
  80. { 0, }
  81. };
  82. MODULE_DEVICE_TABLE (pci, hydra_ids);
  83. static int hydra_probe(struct pci_dev *dev,
  84. const struct pci_device_id *id)
  85. {
  86. unsigned long base = pci_resource_start(dev, 0);
  87. int res;
  88. if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
  89. hydra_adap.name))
  90. return -EBUSY;
  91. hydra_bit_data.data = pci_ioremap_bar(dev, 0);
  92. if (hydra_bit_data.data == NULL) {
  93. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  94. return -ENODEV;
  95. }
  96. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  97. hydra_adap.dev.parent = &dev->dev;
  98. res = i2c_bit_add_bus(&hydra_adap);
  99. if (res < 0) {
  100. iounmap(hydra_bit_data.data);
  101. release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
  102. return res;
  103. }
  104. return 0;
  105. }
  106. static void hydra_remove(struct pci_dev *dev)
  107. {
  108. pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
  109. i2c_del_adapter(&hydra_adap);
  110. iounmap(hydra_bit_data.data);
  111. release_mem_region(pci_resource_start(dev, 0)+
  112. offsetof(struct Hydra, CachePD), 4);
  113. }
  114. static struct pci_driver hydra_driver = {
  115. .name = "hydra_smbus",
  116. .id_table = hydra_ids,
  117. .probe = hydra_probe,
  118. .remove = hydra_remove,
  119. };
  120. module_pci_driver(hydra_driver);
  121. MODULE_AUTHOR("Geert Uytterhoeven <[email protected]>");
  122. MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
  123. MODULE_LICENSE("GPL");