xref: /openbmc/linux/drivers/i2c/busses/i2c-via.c (revision 545e4006)
1 /*
2     i2c Support for Via Technologies 82C586B South Bridge
3 
4     Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-algo-bit.h>
28 #include <asm/io.h>
29 
30 /* Power management registers */
31 #define PM_CFG_REVID	0x08	/* silicon revision code */
32 #define PM_CFG_IOBASE0	0x20
33 #define PM_CFG_IOBASE1	0x48
34 
35 #define I2C_DIR		(pm_io_base+0x40)
36 #define I2C_OUT		(pm_io_base+0x42)
37 #define I2C_IN		(pm_io_base+0x44)
38 #define I2C_SCL		0x02	/* clock bit in DIR/OUT/IN register */
39 #define I2C_SDA		0x04
40 
41 /* io-region reservation */
42 #define IOSPACE		0x06
43 
44 static struct pci_driver vt586b_driver;
45 static u16 pm_io_base;
46 
47 /*
48    It does not appear from the datasheet that the GPIO pins are
49    open drain. So a we set a low value by setting the direction to
50    output and a high value by setting the direction to input and
51    relying on the required I2C pullup. The data value is initialized
52    to 0 in via_init() and never changed.
53 */
54 static void bit_via_setscl(void *data, int state)
55 {
56 	outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
57 }
58 
59 static void bit_via_setsda(void *data, int state)
60 {
61 	outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
62 }
63 
64 static int bit_via_getscl(void *data)
65 {
66 	return (0 != (inb(I2C_IN) & I2C_SCL));
67 }
68 
69 static int bit_via_getsda(void *data)
70 {
71 	return (0 != (inb(I2C_IN) & I2C_SDA));
72 }
73 
74 
75 static struct i2c_algo_bit_data bit_data = {
76 	.setsda		= bit_via_setsda,
77 	.setscl		= bit_via_setscl,
78 	.getsda		= bit_via_getsda,
79 	.getscl		= bit_via_getscl,
80 	.udelay		= 5,
81 	.timeout	= HZ
82 };
83 
84 static struct i2c_adapter vt586b_adapter = {
85 	.owner		= THIS_MODULE,
86 	.id		= I2C_HW_B_VIA,
87 	.class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
88 	.name		= "VIA i2c",
89 	.algo_data	= &bit_data,
90 };
91 
92 
93 static struct pci_device_id vt586b_ids[] __devinitdata = {
94 	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
95 	{ 0, }
96 };
97 
98 MODULE_DEVICE_TABLE (pci, vt586b_ids);
99 
100 static int __devinit vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
101 {
102 	u16 base;
103 	u8 rev;
104 	int res;
105 
106 	if (pm_io_base) {
107 		dev_err(&dev->dev, "i2c-via: Will only support one host\n");
108 		return -ENODEV;
109 	}
110 
111 	pci_read_config_byte(dev, PM_CFG_REVID, &rev);
112 
113 	switch (rev) {
114 	case 0x00:
115 		base = PM_CFG_IOBASE0;
116 		break;
117 	case 0x01:
118 	case 0x10:
119 		base = PM_CFG_IOBASE1;
120 		break;
121 
122 	default:
123 		base = PM_CFG_IOBASE1;
124 		/* later revision */
125 	}
126 
127 	pci_read_config_word(dev, base, &pm_io_base);
128 	pm_io_base &= (0xff << 8);
129 
130 	if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
131 		dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
132 		return -ENODEV;
133 	}
134 
135 	outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
136 	outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
137 
138 	/* set up the sysfs linkage to our parent device */
139 	vt586b_adapter.dev.parent = &dev->dev;
140 
141 	res = i2c_bit_add_bus(&vt586b_adapter);
142 	if ( res < 0 ) {
143 		release_region(I2C_DIR, IOSPACE);
144 		pm_io_base = 0;
145 		return res;
146 	}
147 	return 0;
148 }
149 
150 static void __devexit vt586b_remove(struct pci_dev *dev)
151 {
152 	i2c_del_adapter(&vt586b_adapter);
153 	release_region(I2C_DIR, IOSPACE);
154 	pm_io_base = 0;
155 }
156 
157 
158 static struct pci_driver vt586b_driver = {
159 	.name		= "vt586b_smbus",
160 	.id_table	= vt586b_ids,
161 	.probe		= vt586b_probe,
162 	.remove		= __devexit_p(vt586b_remove),
163 };
164 
165 static int __init i2c_vt586b_init(void)
166 {
167 	return pci_register_driver(&vt586b_driver);
168 }
169 
170 static void __exit i2c_vt586b_exit(void)
171 {
172 	pci_unregister_driver(&vt586b_driver);
173 }
174 
175 
176 MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
177 MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
178 MODULE_LICENSE("GPL");
179 
180 module_init(i2c_vt586b_init);
181 module_exit(i2c_vt586b_exit);
182