matrox_w1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * matrox_w1.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  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. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <asm/types.h>
  22. #include <linux/atomic.h>
  23. #include <asm/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/list.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/timer.h>
  31. #include <linux/slab.h>
  32. #include <linux/pci_ids.h>
  33. #include <linux/pci.h>
  34. #include <linux/w1.h>
  35. /*
  36. * Matrox G400 DDC registers.
  37. */
  38. #define MATROX_G400_DDC_CLK (1<<4)
  39. #define MATROX_G400_DDC_DATA (1<<1)
  40. #define MATROX_BASE 0x3C00
  41. #define MATROX_STATUS 0x1e14
  42. #define MATROX_PORT_INDEX_OFFSET 0x00
  43. #define MATROX_PORT_DATA_OFFSET 0x0A
  44. #define MATROX_GET_CONTROL 0x2A
  45. #define MATROX_GET_DATA 0x2B
  46. #define MATROX_CURSOR_CTL 0x06
  47. struct matrox_device
  48. {
  49. void __iomem *base_addr;
  50. void __iomem *port_index;
  51. void __iomem *port_data;
  52. u8 data_mask;
  53. unsigned long phys_addr;
  54. void __iomem *virt_addr;
  55. unsigned long found;
  56. struct w1_bus_master *bus_master;
  57. };
  58. /*
  59. * These functions read and write DDC Data bit.
  60. *
  61. * Using tristate pins, since i can't find any open-drain pin in whole motherboard.
  62. * Unfortunately we can't connect to Intel's 82801xx IO controller
  63. * since we don't know motherboard schema, which has pretty unused(may be not) GPIO.
  64. *
  65. * I've heard that PIIX also has open drain pin.
  66. *
  67. * Port mapping.
  68. */
  69. static __inline__ u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg)
  70. {
  71. u8 ret;
  72. writeb(reg, dev->port_index);
  73. ret = readb(dev->port_data);
  74. barrier();
  75. return ret;
  76. }
  77. static __inline__ void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val)
  78. {
  79. writeb(reg, dev->port_index);
  80. writeb(val, dev->port_data);
  81. wmb();
  82. }
  83. static void matrox_w1_write_ddc_bit(void *data, u8 bit)
  84. {
  85. u8 ret;
  86. struct matrox_device *dev = data;
  87. if (bit)
  88. bit = 0;
  89. else
  90. bit = dev->data_mask;
  91. ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL);
  92. matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit));
  93. matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00);
  94. }
  95. static u8 matrox_w1_read_ddc_bit(void *data)
  96. {
  97. u8 ret;
  98. struct matrox_device *dev = data;
  99. ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
  100. return ret;
  101. }
  102. static void matrox_w1_hw_init(struct matrox_device *dev)
  103. {
  104. matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF);
  105. matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00);
  106. }
  107. static int matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  108. {
  109. struct matrox_device *dev;
  110. int err;
  111. if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400)
  112. return -ENODEV;
  113. dev = kzalloc(sizeof(struct matrox_device) +
  114. sizeof(struct w1_bus_master), GFP_KERNEL);
  115. if (!dev) {
  116. dev_err(&pdev->dev,
  117. "%s: Failed to create new matrox_device object.\n",
  118. __func__);
  119. return -ENOMEM;
  120. }
  121. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  122. /*
  123. * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c
  124. */
  125. dev->phys_addr = pci_resource_start(pdev, 1);
  126. dev->virt_addr = ioremap_nocache(dev->phys_addr, 16384);
  127. if (!dev->virt_addr) {
  128. dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n",
  129. __func__, dev->phys_addr, 16384);
  130. err = -EIO;
  131. goto err_out_free_device;
  132. }
  133. dev->base_addr = dev->virt_addr + MATROX_BASE;
  134. dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET;
  135. dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET;
  136. dev->data_mask = (MATROX_G400_DDC_DATA);
  137. matrox_w1_hw_init(dev);
  138. dev->bus_master->data = dev;
  139. dev->bus_master->read_bit = &matrox_w1_read_ddc_bit;
  140. dev->bus_master->write_bit = &matrox_w1_write_ddc_bit;
  141. err = w1_add_master_device(dev->bus_master);
  142. if (err)
  143. goto err_out_free_device;
  144. pci_set_drvdata(pdev, dev);
  145. dev->found = 1;
  146. dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n");
  147. return 0;
  148. err_out_free_device:
  149. if (dev->virt_addr)
  150. iounmap(dev->virt_addr);
  151. kfree(dev);
  152. return err;
  153. }
  154. static void matrox_w1_remove(struct pci_dev *pdev)
  155. {
  156. struct matrox_device *dev = pci_get_drvdata(pdev);
  157. if (dev->found) {
  158. w1_remove_master_device(dev->bus_master);
  159. iounmap(dev->virt_addr);
  160. }
  161. kfree(dev);
  162. }
  163. static struct pci_device_id matrox_w1_tbl[] = {
  164. { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
  165. { },
  166. };
  167. MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
  168. static struct pci_driver matrox_w1_pci_driver = {
  169. .name = "matrox_w1",
  170. .id_table = matrox_w1_tbl,
  171. .probe = matrox_w1_probe,
  172. .remove = matrox_w1_remove,
  173. };
  174. module_pci_driver(matrox_w1_pci_driver);
  175. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  176. MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire protocol) over VGA DDC(matrox gpio).");
  177. MODULE_LICENSE("GPL");