xref: /linux/drivers/w1/masters/matrox_w1.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2bd529cfbSEvgeniy Polyakov /*
3bd529cfbSEvgeniy Polyakov  *	matrox_w1.c
4bd529cfbSEvgeniy Polyakov  *
5a8018766SEvgeniy Polyakov  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
6bd529cfbSEvgeniy Polyakov  */
7bd529cfbSEvgeniy Polyakov 
8bd529cfbSEvgeniy Polyakov #include <asm/types.h>
960063497SArun Sharma #include <linux/atomic.h>
1001bc199bSKrzysztof Kozlowski #include <linux/io.h>
11bd529cfbSEvgeniy Polyakov 
12bd529cfbSEvgeniy Polyakov #include <linux/delay.h>
13bd529cfbSEvgeniy Polyakov #include <linux/kernel.h>
14bd529cfbSEvgeniy Polyakov #include <linux/module.h>
15bd529cfbSEvgeniy Polyakov #include <linux/list.h>
16bd529cfbSEvgeniy Polyakov #include <linux/interrupt.h>
17bd529cfbSEvgeniy Polyakov #include <linux/spinlock.h>
18bd529cfbSEvgeniy Polyakov #include <linux/timer.h>
19bd529cfbSEvgeniy Polyakov #include <linux/slab.h>
20bd529cfbSEvgeniy Polyakov #include <linux/pci_ids.h>
21bd529cfbSEvgeniy Polyakov #include <linux/pci.h>
22bd529cfbSEvgeniy Polyakov 
23de0d6dbdSAndrew F. Davis #include <linux/w1.h>
24bd529cfbSEvgeniy Polyakov 
25bd529cfbSEvgeniy Polyakov /*
26bd529cfbSEvgeniy Polyakov  * Matrox G400 DDC registers.
27bd529cfbSEvgeniy Polyakov  */
28bd529cfbSEvgeniy Polyakov 
29bd529cfbSEvgeniy Polyakov #define MATROX_G400_DDC_CLK		(1<<4)
30bd529cfbSEvgeniy Polyakov #define MATROX_G400_DDC_DATA		(1<<1)
31bd529cfbSEvgeniy Polyakov 
32bd529cfbSEvgeniy Polyakov #define MATROX_BASE			0x3C00
33bd529cfbSEvgeniy Polyakov #define MATROX_STATUS			0x1e14
34bd529cfbSEvgeniy Polyakov 
35bd529cfbSEvgeniy Polyakov #define MATROX_PORT_INDEX_OFFSET	0x00
36bd529cfbSEvgeniy Polyakov #define MATROX_PORT_DATA_OFFSET		0x0A
37bd529cfbSEvgeniy Polyakov 
38bd529cfbSEvgeniy Polyakov #define MATROX_GET_CONTROL		0x2A
39bd529cfbSEvgeniy Polyakov #define MATROX_GET_DATA			0x2B
40bd529cfbSEvgeniy Polyakov #define MATROX_CURSOR_CTL		0x06
41bd529cfbSEvgeniy Polyakov 
42ad9c36beSKrzysztof Kozlowski struct matrox_device {
43bd529cfbSEvgeniy Polyakov 	void __iomem *base_addr;
44bd529cfbSEvgeniy Polyakov 	void __iomem *port_index;
45bd529cfbSEvgeniy Polyakov 	void __iomem *port_data;
46bd529cfbSEvgeniy Polyakov 	u8 data_mask;
47bd529cfbSEvgeniy Polyakov 
48bd529cfbSEvgeniy Polyakov 	unsigned long phys_addr;
49bd529cfbSEvgeniy Polyakov 	void __iomem *virt_addr;
50bd529cfbSEvgeniy Polyakov 	unsigned long found;
51bd529cfbSEvgeniy Polyakov 
52bd529cfbSEvgeniy Polyakov 	struct w1_bus_master *bus_master;
53bd529cfbSEvgeniy Polyakov };
54bd529cfbSEvgeniy Polyakov 
55bd529cfbSEvgeniy Polyakov /*
56bd529cfbSEvgeniy Polyakov  * These functions read and write DDC Data bit.
57bd529cfbSEvgeniy Polyakov  *
58bd529cfbSEvgeniy Polyakov  * Using tristate pins, since i can't find any open-drain pin in whole motherboard.
59bd529cfbSEvgeniy Polyakov  * Unfortunately we can't connect to Intel's 82801xx IO controller
60c30fe7f7SUwe Zeisberger  * since we don't know motherboard schema, which has pretty unused(may be not) GPIO.
61bd529cfbSEvgeniy Polyakov  *
62bd529cfbSEvgeniy Polyakov  * I've heard that PIIX also has open drain pin.
63bd529cfbSEvgeniy Polyakov  *
64bd529cfbSEvgeniy Polyakov  * Port mapping.
65bd529cfbSEvgeniy Polyakov  */
matrox_w1_read_reg(struct matrox_device * dev,u8 reg)66115ddc75SKrzysztof Kozlowski static inline u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg)
67bd529cfbSEvgeniy Polyakov {
68bd529cfbSEvgeniy Polyakov 	u8 ret;
69bd529cfbSEvgeniy Polyakov 
70bd529cfbSEvgeniy Polyakov 	writeb(reg, dev->port_index);
71bd529cfbSEvgeniy Polyakov 	ret = readb(dev->port_data);
72bd529cfbSEvgeniy Polyakov 	barrier();
73bd529cfbSEvgeniy Polyakov 
74bd529cfbSEvgeniy Polyakov 	return ret;
75bd529cfbSEvgeniy Polyakov }
76bd529cfbSEvgeniy Polyakov 
matrox_w1_write_reg(struct matrox_device * dev,u8 reg,u8 val)77115ddc75SKrzysztof Kozlowski static inline void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val)
78bd529cfbSEvgeniy Polyakov {
79bd529cfbSEvgeniy Polyakov 	writeb(reg, dev->port_index);
80bd529cfbSEvgeniy Polyakov 	writeb(val, dev->port_data);
81bd529cfbSEvgeniy Polyakov 	wmb();
82bd529cfbSEvgeniy Polyakov }
83bd529cfbSEvgeniy Polyakov 
matrox_w1_write_ddc_bit(void * data,u8 bit)84bd529cfbSEvgeniy Polyakov static void matrox_w1_write_ddc_bit(void *data, u8 bit)
85bd529cfbSEvgeniy Polyakov {
86bd529cfbSEvgeniy Polyakov 	u8 ret;
87bd529cfbSEvgeniy Polyakov 	struct matrox_device *dev = data;
88bd529cfbSEvgeniy Polyakov 
89bd529cfbSEvgeniy Polyakov 	if (bit)
90bd529cfbSEvgeniy Polyakov 		bit = 0;
91bd529cfbSEvgeniy Polyakov 	else
92bd529cfbSEvgeniy Polyakov 		bit = dev->data_mask;
93bd529cfbSEvgeniy Polyakov 
94bd529cfbSEvgeniy Polyakov 	ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL);
95bd529cfbSEvgeniy Polyakov 	matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit));
96bd529cfbSEvgeniy Polyakov 	matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00);
97bd529cfbSEvgeniy Polyakov }
98bd529cfbSEvgeniy Polyakov 
matrox_w1_read_ddc_bit(void * data)99bd529cfbSEvgeniy Polyakov static u8 matrox_w1_read_ddc_bit(void *data)
100bd529cfbSEvgeniy Polyakov {
101bd529cfbSEvgeniy Polyakov 	u8 ret;
102bd529cfbSEvgeniy Polyakov 	struct matrox_device *dev = data;
103bd529cfbSEvgeniy Polyakov 
104bd529cfbSEvgeniy Polyakov 	ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
105bd529cfbSEvgeniy Polyakov 
106bd529cfbSEvgeniy Polyakov 	return ret;
107bd529cfbSEvgeniy Polyakov }
108bd529cfbSEvgeniy Polyakov 
matrox_w1_hw_init(struct matrox_device * dev)109bd529cfbSEvgeniy Polyakov static void matrox_w1_hw_init(struct matrox_device *dev)
110bd529cfbSEvgeniy Polyakov {
111bd529cfbSEvgeniy Polyakov 	matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF);
112bd529cfbSEvgeniy Polyakov 	matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00);
113bd529cfbSEvgeniy Polyakov }
114bd529cfbSEvgeniy Polyakov 
matrox_w1_probe(struct pci_dev * pdev,const struct pci_device_id * ent)115479e2bceSBill Pemberton static int matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
116bd529cfbSEvgeniy Polyakov {
117bd529cfbSEvgeniy Polyakov 	struct matrox_device *dev;
118bd529cfbSEvgeniy Polyakov 	int err;
119bd529cfbSEvgeniy Polyakov 
120bd529cfbSEvgeniy Polyakov 	if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400)
121bd529cfbSEvgeniy Polyakov 		return -ENODEV;
122bd529cfbSEvgeniy Polyakov 
123dd00cc48SYoann Padioleau 	dev = kzalloc(sizeof(struct matrox_device) +
124bd529cfbSEvgeniy Polyakov 		       sizeof(struct w1_bus_master), GFP_KERNEL);
125*4f4129e4SKrzysztof Kozlowski 	if (!dev)
126bd529cfbSEvgeniy Polyakov 		return -ENOMEM;
127bd529cfbSEvgeniy Polyakov 
128bd529cfbSEvgeniy Polyakov 	dev->bus_master = (struct w1_bus_master *)(dev + 1);
129bd529cfbSEvgeniy Polyakov 
130bd529cfbSEvgeniy Polyakov 	/*
131bd529cfbSEvgeniy Polyakov 	 * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c
132bd529cfbSEvgeniy Polyakov 	 */
133bd529cfbSEvgeniy Polyakov 
134bd529cfbSEvgeniy Polyakov 	dev->phys_addr = pci_resource_start(pdev, 1);
135bd529cfbSEvgeniy Polyakov 
1364bdc0d67SChristoph Hellwig 	dev->virt_addr = ioremap(dev->phys_addr, 16384);
137bd529cfbSEvgeniy Polyakov 	if (!dev->virt_addr) {
138bd529cfbSEvgeniy Polyakov 		dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n",
139bd529cfbSEvgeniy Polyakov 			__func__, dev->phys_addr, 16384);
140bd529cfbSEvgeniy Polyakov 		err = -EIO;
141bd529cfbSEvgeniy Polyakov 		goto err_out_free_device;
142bd529cfbSEvgeniy Polyakov 	}
143bd529cfbSEvgeniy Polyakov 
144bd529cfbSEvgeniy Polyakov 	dev->base_addr = dev->virt_addr + MATROX_BASE;
145bd529cfbSEvgeniy Polyakov 	dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET;
146bd529cfbSEvgeniy Polyakov 	dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET;
147bd529cfbSEvgeniy Polyakov 	dev->data_mask = (MATROX_G400_DDC_DATA);
148bd529cfbSEvgeniy Polyakov 
149bd529cfbSEvgeniy Polyakov 	matrox_w1_hw_init(dev);
150bd529cfbSEvgeniy Polyakov 
151bd529cfbSEvgeniy Polyakov 	dev->bus_master->data = dev;
152bd529cfbSEvgeniy Polyakov 	dev->bus_master->read_bit = &matrox_w1_read_ddc_bit;
153bd529cfbSEvgeniy Polyakov 	dev->bus_master->write_bit = &matrox_w1_write_ddc_bit;
154bd529cfbSEvgeniy Polyakov 
155bd529cfbSEvgeniy Polyakov 	err = w1_add_master_device(dev->bus_master);
156bd529cfbSEvgeniy Polyakov 	if (err)
157bd529cfbSEvgeniy Polyakov 		goto err_out_free_device;
158bd529cfbSEvgeniy Polyakov 
159bd529cfbSEvgeniy Polyakov 	pci_set_drvdata(pdev, dev);
160bd529cfbSEvgeniy Polyakov 
161bd529cfbSEvgeniy Polyakov 	dev->found = 1;
162bd529cfbSEvgeniy Polyakov 
163bd529cfbSEvgeniy Polyakov 	dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n");
164bd529cfbSEvgeniy Polyakov 
165bd529cfbSEvgeniy Polyakov 	return 0;
166bd529cfbSEvgeniy Polyakov 
167bd529cfbSEvgeniy Polyakov err_out_free_device:
168e45413ebSAmol Lad 	if (dev->virt_addr)
169e45413ebSAmol Lad 		iounmap(dev->virt_addr);
170bd529cfbSEvgeniy Polyakov 	kfree(dev);
171bd529cfbSEvgeniy Polyakov 
172bd529cfbSEvgeniy Polyakov 	return err;
173bd529cfbSEvgeniy Polyakov }
174bd529cfbSEvgeniy Polyakov 
matrox_w1_remove(struct pci_dev * pdev)17582849a93SBill Pemberton static void matrox_w1_remove(struct pci_dev *pdev)
176bd529cfbSEvgeniy Polyakov {
177bd529cfbSEvgeniy Polyakov 	struct matrox_device *dev = pci_get_drvdata(pdev);
178bd529cfbSEvgeniy Polyakov 
179bd529cfbSEvgeniy Polyakov 	if (dev->found) {
180bd529cfbSEvgeniy Polyakov 		w1_remove_master_device(dev->bus_master);
181bd529cfbSEvgeniy Polyakov 		iounmap(dev->virt_addr);
182bd529cfbSEvgeniy Polyakov 	}
183bd529cfbSEvgeniy Polyakov 	kfree(dev);
184bd529cfbSEvgeniy Polyakov }
18550fa2951SAndrew F. Davis 
18650fa2951SAndrew F. Davis static struct pci_device_id matrox_w1_tbl[] = {
18750fa2951SAndrew F. Davis 	{ PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
18850fa2951SAndrew F. Davis 	{ },
18950fa2951SAndrew F. Davis };
19050fa2951SAndrew F. Davis MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
19150fa2951SAndrew F. Davis 
19250fa2951SAndrew F. Davis static struct pci_driver matrox_w1_pci_driver = {
19350fa2951SAndrew F. Davis 	.name = "matrox_w1",
19450fa2951SAndrew F. Davis 	.id_table = matrox_w1_tbl,
19550fa2951SAndrew F. Davis 	.probe = matrox_w1_probe,
19650fa2951SAndrew F. Davis 	.remove = matrox_w1_remove,
19750fa2951SAndrew F. Davis };
198e7390d7cSVaishali Thakkar module_pci_driver(matrox_w1_pci_driver);
19950fa2951SAndrew F. Davis 
20050fa2951SAndrew F. Davis MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
20150fa2951SAndrew F. Davis MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire protocol) over VGA DDC(matrox gpio).");
20250fa2951SAndrew F. Davis MODULE_LICENSE("GPL");
203