matrox_w1.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "../w1.h"
  35. #include "../w1_int.h"
  36. MODULE_LICENSE("GPL");
  37. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  38. MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire protocol) over VGA DDC(matrox gpio).");
  39. static struct pci_device_id matrox_w1_tbl[] = {
  40. { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
  41. { },
  42. };
  43. MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
  44. static int matrox_w1_probe(struct pci_dev *, const struct pci_device_id *);
  45. static void matrox_w1_remove(struct pci_dev *);
  46. static struct pci_driver matrox_w1_pci_driver = {
  47. .name = "matrox_w1",
  48. .id_table = matrox_w1_tbl,
  49. .probe = matrox_w1_probe,
  50. .remove = matrox_w1_remove,
  51. };
  52. /*
  53. * Matrox G400 DDC registers.
  54. */
  55. #define MATROX_G400_DDC_CLK (1<<4)
  56. #define MATROX_G400_DDC_DATA (1<<1)
  57. #define MATROX_BASE 0x3C00
  58. #define MATROX_STATUS 0x1e14
  59. #define MATROX_PORT_INDEX_OFFSET 0x00
  60. #define MATROX_PORT_DATA_OFFSET 0x0A
  61. #define MATROX_GET_CONTROL 0x2A
  62. #define MATROX_GET_DATA 0x2B
  63. #define MATROX_CURSOR_CTL 0x06
  64. struct matrox_device
  65. {
  66. void __iomem *base_addr;
  67. void __iomem *port_index;
  68. void __iomem *port_data;
  69. u8 data_mask;
  70. unsigned long phys_addr;
  71. void __iomem *virt_addr;
  72. unsigned long found;
  73. struct w1_bus_master *bus_master;
  74. };
  75. static u8 matrox_w1_read_ddc_bit(void *);
  76. static void matrox_w1_write_ddc_bit(void *, u8);
  77. /*
  78. * These functions read and write DDC Data bit.
  79. *
  80. * Using tristate pins, since i can't find any open-drain pin in whole motherboard.
  81. * Unfortunately we can't connect to Intel's 82801xx IO controller
  82. * since we don't know motherboard schema, which has pretty unused(may be not) GPIO.
  83. *
  84. * I've heard that PIIX also has open drain pin.
  85. *
  86. * Port mapping.
  87. */
  88. static __inline__ u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg)
  89. {
  90. u8 ret;
  91. writeb(reg, dev->port_index);
  92. ret = readb(dev->port_data);
  93. barrier();
  94. return ret;
  95. }
  96. static __inline__ void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val)
  97. {
  98. writeb(reg, dev->port_index);
  99. writeb(val, dev->port_data);
  100. wmb();
  101. }
  102. static void matrox_w1_write_ddc_bit(void *data, u8 bit)
  103. {
  104. u8 ret;
  105. struct matrox_device *dev = data;
  106. if (bit)
  107. bit = 0;
  108. else
  109. bit = dev->data_mask;
  110. ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL);
  111. matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit));
  112. matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00);
  113. }
  114. static u8 matrox_w1_read_ddc_bit(void *data)
  115. {
  116. u8 ret;
  117. struct matrox_device *dev = data;
  118. ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
  119. return ret;
  120. }
  121. static void matrox_w1_hw_init(struct matrox_device *dev)
  122. {
  123. matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF);
  124. matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00);
  125. }
  126. static int matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  127. {
  128. struct matrox_device *dev;
  129. int err;
  130. if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400)
  131. return -ENODEV;
  132. dev = kzalloc(sizeof(struct matrox_device) +
  133. sizeof(struct w1_bus_master), GFP_KERNEL);
  134. if (!dev) {
  135. dev_err(&pdev->dev,
  136. "%s: Failed to create new matrox_device object.\n",
  137. __func__);
  138. return -ENOMEM;
  139. }
  140. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  141. /*
  142. * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c
  143. */
  144. dev->phys_addr = pci_resource_start(pdev, 1);
  145. dev->virt_addr = ioremap_nocache(dev->phys_addr, 16384);
  146. if (!dev->virt_addr) {
  147. dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n",
  148. __func__, dev->phys_addr, 16384);
  149. err = -EIO;
  150. goto err_out_free_device;
  151. }
  152. dev->base_addr = dev->virt_addr + MATROX_BASE;
  153. dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET;
  154. dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET;
  155. dev->data_mask = (MATROX_G400_DDC_DATA);
  156. matrox_w1_hw_init(dev);
  157. dev->bus_master->data = dev;
  158. dev->bus_master->read_bit = &matrox_w1_read_ddc_bit;
  159. dev->bus_master->write_bit = &matrox_w1_write_ddc_bit;
  160. err = w1_add_master_device(dev->bus_master);
  161. if (err)
  162. goto err_out_free_device;
  163. pci_set_drvdata(pdev, dev);
  164. dev->found = 1;
  165. dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n");
  166. return 0;
  167. err_out_free_device:
  168. if (dev->virt_addr)
  169. iounmap(dev->virt_addr);
  170. kfree(dev);
  171. return err;
  172. }
  173. static void matrox_w1_remove(struct pci_dev *pdev)
  174. {
  175. struct matrox_device *dev = pci_get_drvdata(pdev);
  176. if (dev->found) {
  177. w1_remove_master_device(dev->bus_master);
  178. iounmap(dev->virt_addr);
  179. }
  180. kfree(dev);
  181. }
  182. module_pci_driver(matrox_w1_pci_driver);