xref: /linux/drivers/i2c/busses/i2c-via.c (revision 883e3c9f40814377a239ca0becbcc77deab5ffe5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     i2c Support for Via Technologies 82C586B South Bridge
4 
5     Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
6 
7 */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/ioport.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c-algo-bit.h>
15 #include <linux/io.h>
16 
17 /* Power management registers */
18 #define PM_CFG_REVID	0x08	/* silicon revision code */
19 #define PM_CFG_IOBASE0	0x20
20 #define PM_CFG_IOBASE1	0x48
21 
22 #define I2C_DIR		(pm_io_base+0x40)
23 #define I2C_OUT		(pm_io_base+0x42)
24 #define I2C_IN		(pm_io_base+0x44)
25 #define I2C_SCL		0x02	/* clock bit in DIR/OUT/IN register */
26 #define I2C_SDA		0x04
27 
28 /* io-region reservation */
29 #define IOSPACE		0x06
30 
31 static struct pci_driver vt586b_driver;
32 static u16 pm_io_base;
33 
34 /*
35    It does not appear from the datasheet that the GPIO pins are
36    open drain. So a we set a low value by setting the direction to
37    output and a high value by setting the direction to input and
38    relying on the required I2C pullup. The data value is initialized
39    to 0 in via_init() and never changed.
40 */
bit_via_setscl(void * data,int state)41 static void bit_via_setscl(void *data, int state)
42 {
43 	outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
44 }
45 
bit_via_setsda(void * data,int state)46 static void bit_via_setsda(void *data, int state)
47 {
48 	outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
49 }
50 
bit_via_getscl(void * data)51 static int bit_via_getscl(void *data)
52 {
53 	return (0 != (inb(I2C_IN) & I2C_SCL));
54 }
55 
bit_via_getsda(void * data)56 static int bit_via_getsda(void *data)
57 {
58 	return (0 != (inb(I2C_IN) & I2C_SDA));
59 }
60 
61 
62 static struct i2c_algo_bit_data bit_data = {
63 	.setsda		= bit_via_setsda,
64 	.setscl		= bit_via_setscl,
65 	.getsda		= bit_via_getsda,
66 	.getscl		= bit_via_getscl,
67 	.udelay		= 5,
68 	.timeout	= HZ
69 };
70 
71 static struct i2c_adapter vt586b_adapter = {
72 	.owner		= THIS_MODULE,
73 	.class          = I2C_CLASS_HWMON,
74 	.name		= "VIA i2c",
75 	.algo_data	= &bit_data,
76 };
77 
78 
79 static const struct pci_device_id vt586b_ids[] = {
80 	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
81 	{ 0, }
82 };
83 
84 MODULE_DEVICE_TABLE (pci, vt586b_ids);
85 
vt586b_probe(struct pci_dev * dev,const struct pci_device_id * id)86 static int vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
87 {
88 	u16 base;
89 	u8 rev;
90 	int res;
91 
92 	if (pm_io_base)
93 		return dev_err_probe(&dev->dev, -ENODEV,
94 				     "Will only support one host\n");
95 
96 	pci_read_config_byte(dev, PM_CFG_REVID, &rev);
97 
98 	switch (rev) {
99 	case 0x00:
100 		base = PM_CFG_IOBASE0;
101 		break;
102 	case 0x01:
103 	case 0x10:
104 		base = PM_CFG_IOBASE1;
105 		break;
106 
107 	default:
108 		base = PM_CFG_IOBASE1;
109 		/* later revision */
110 	}
111 
112 	pci_read_config_word(dev, base, &pm_io_base);
113 	pm_io_base &= (0xff << 8);
114 
115 	if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name))
116 		return dev_err_probe(&dev->dev, -ENODEV,
117 				     "IO 0x%x-0x%x already in use\n",
118 				     I2C_DIR, I2C_DIR + IOSPACE);
119 
120 	outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
121 	outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
122 
123 	/* set up the sysfs linkage to our parent device */
124 	vt586b_adapter.dev.parent = &dev->dev;
125 
126 	res = i2c_bit_add_bus(&vt586b_adapter);
127 	if ( res < 0 ) {
128 		release_region(I2C_DIR, IOSPACE);
129 		pm_io_base = 0;
130 		return res;
131 	}
132 	return 0;
133 }
134 
vt586b_remove(struct pci_dev * dev)135 static void vt586b_remove(struct pci_dev *dev)
136 {
137 	i2c_del_adapter(&vt586b_adapter);
138 	release_region(I2C_DIR, IOSPACE);
139 	pm_io_base = 0;
140 }
141 
142 
143 static struct pci_driver vt586b_driver = {
144 	.name		= "vt586b_smbus",
145 	.id_table	= vt586b_ids,
146 	.probe		= vt586b_probe,
147 	.remove		= vt586b_remove,
148 };
149 
150 module_pci_driver(vt586b_driver);
151 
152 MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
153 MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
154 MODULE_LICENSE("GPL");
155