blob: 334d1ccf9c922618da2b5addab4bd46015263037 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * matrox_w1.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +030037#include "../w1.h"
38#include "../w1_int.h"
39#include "../w1_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41MODULE_LICENSE("GPL");
42MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
43MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire prtocol) over VGA DDC(matrox gpio).");
44
45static struct pci_device_id matrox_w1_tbl[] = {
46 { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
47 { },
48};
49MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
50
51static int __devinit matrox_w1_probe(struct pci_dev *, const struct pci_device_id *);
52static void __devexit matrox_w1_remove(struct pci_dev *);
53
54static struct pci_driver matrox_w1_pci_driver = {
55 .name = "matrox_w1",
56 .id_table = matrox_w1_tbl,
57 .probe = matrox_w1_probe,
58 .remove = __devexit_p(matrox_w1_remove),
59};
60
Evgeniy Polyakov77859252005-05-20 22:33:25 +040061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Matrox G400 DDC registers.
63 */
64
65#define MATROX_G400_DDC_CLK (1<<4)
66#define MATROX_G400_DDC_DATA (1<<1)
67
68#define MATROX_BASE 0x3C00
69#define MATROX_STATUS 0x1e14
70
71#define MATROX_PORT_INDEX_OFFSET 0x00
72#define MATROX_PORT_DATA_OFFSET 0x0A
73
74#define MATROX_GET_CONTROL 0x2A
75#define MATROX_GET_DATA 0x2B
76#define MATROX_CURSOR_CTL 0x06
77
78struct matrox_device
79{
80 void __iomem *base_addr;
81 void __iomem *port_index;
82 void __iomem *port_data;
83 u8 data_mask;
84
85 unsigned long phys_addr;
86 void __iomem *virt_addr;
87 unsigned long found;
88
89 struct w1_bus_master *bus_master;
90};
91
Evgeniy Polyakovccd69942005-12-06 13:38:27 +030092static u8 matrox_w1_read_ddc_bit(void *);
93static void matrox_w1_write_ddc_bit(void *, u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * These functions read and write DDC Data bit.
97 *
98 * Using tristate pins, since i can't find any open-drain pin in whole motherboard.
99 * Unfortunately we can't connect to Intel's 82801xx IO controller
Uwe Zeisbergerc30fe7f2006-03-24 18:23:14 +0100100 * since we don't know motherboard schema, which has pretty unused(may be not) GPIO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * I've heard that PIIX also has open drain pin.
103 *
104 * Port mapping.
105 */
106static __inline__ u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg)
107{
108 u8 ret;
109
110 writeb(reg, dev->port_index);
111 ret = readb(dev->port_data);
112 barrier();
113
114 return ret;
115}
116
117static __inline__ void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val)
118{
119 writeb(reg, dev->port_index);
120 writeb(val, dev->port_data);
121 wmb();
122}
123
Evgeniy Polyakovccd69942005-12-06 13:38:27 +0300124static void matrox_w1_write_ddc_bit(void *data, u8 bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 u8 ret;
Evgeniy Polyakovccd69942005-12-06 13:38:27 +0300127 struct matrox_device *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (bit)
130 bit = 0;
131 else
132 bit = dev->data_mask;
133
134 ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL);
135 matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit));
136 matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00);
137}
138
Evgeniy Polyakovccd69942005-12-06 13:38:27 +0300139static u8 matrox_w1_read_ddc_bit(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 u8 ret;
Evgeniy Polyakovccd69942005-12-06 13:38:27 +0300142 struct matrox_device *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
145
146 return ret;
147}
148
149static void matrox_w1_hw_init(struct matrox_device *dev)
150{
151 matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF);
152 matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00);
153}
154
155static int __devinit matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
156{
157 struct matrox_device *dev;
158 int err;
159
160 assert(pdev != NULL);
161 assert(ent != NULL);
162
163 if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400)
164 return -ENODEV;
165
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700166 dev = kzalloc(sizeof(struct matrox_device) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 sizeof(struct w1_bus_master), GFP_KERNEL);
168 if (!dev) {
169 dev_err(&pdev->dev,
170 "%s: Failed to create new matrox_device object.\n",
171 __func__);
172 return -ENOMEM;
173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 dev->bus_master = (struct w1_bus_master *)(dev + 1);
177
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400178 /*
179 * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181
182 dev->phys_addr = pci_resource_start(pdev, 1);
183
184 dev->virt_addr = ioremap_nocache(dev->phys_addr, 16384);
185 if (!dev->virt_addr) {
186 dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n",
187 __func__, dev->phys_addr, 16384);
188 err = -EIO;
189 goto err_out_free_device;
190 }
191
192 dev->base_addr = dev->virt_addr + MATROX_BASE;
193 dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET;
194 dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET;
195 dev->data_mask = (MATROX_G400_DDC_DATA);
196
197 matrox_w1_hw_init(dev);
198
Evgeniy Polyakovccd69942005-12-06 13:38:27 +0300199 dev->bus_master->data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dev->bus_master->read_bit = &matrox_w1_read_ddc_bit;
201 dev->bus_master->write_bit = &matrox_w1_write_ddc_bit;
202
203 err = w1_add_master_device(dev->bus_master);
204 if (err)
205 goto err_out_free_device;
206
207 pci_set_drvdata(pdev, dev);
208
209 dev->found = 1;
210
211 dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n");
212
213 return 0;
214
215err_out_free_device:
Amol Lade45413e2006-10-05 14:26:02 +0400216 if (dev->virt_addr)
217 iounmap(dev->virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(dev);
219
220 return err;
221}
222
223static void __devexit matrox_w1_remove(struct pci_dev *pdev)
224{
225 struct matrox_device *dev = pci_get_drvdata(pdev);
226
227 assert(dev != NULL);
228
229 if (dev->found) {
230 w1_remove_master_device(dev->bus_master);
231 iounmap(dev->virt_addr);
232 }
233 kfree(dev);
234}
235
236static int __init matrox_w1_init(void)
237{
238 return pci_register_driver(&matrox_w1_pci_driver);
239}
240
241static void __exit matrox_w1_fini(void)
242{
243 pci_unregister_driver(&matrox_w1_pci_driver);
244}
245
246module_init(matrox_w1_init);
247module_exit(matrox_w1_fini);