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