matrox_w1.c 5.9 KB

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