blob: ad4f09c7f0275057854a004c7b69e3b57524d2d4 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 i2c Support for Via Technologies 82C586B South Bridge
4
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007*/
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/pci.h>
12#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/i2c.h>
14#include <linux/i2c-algo-bit.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020015#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jean Delvared6072f82005-09-25 16:37:04 +020031static struct pci_driver vt586b_driver;
Jean Delvare60507092005-09-25 16:23:07 +020032static u16 pm_io_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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*/
41static 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
46static 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
51static int bit_via_getscl(void *data)
52{
53 return (0 != (inb(I2C_IN) & I2C_SCL));
54}
55
56static int bit_via_getsda(void *data)
57{
58 return (0 != (inb(I2C_IN) & I2C_SDA));
59}
60
61
62static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .timeout = HZ
69};
70
71static struct i2c_adapter vt586b_adapter = {
72 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +020073 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .name = "VIA i2c",
75 .algo_data = &bit_data,
76};
77
78
Jingoo Han392debf2013-12-03 08:11:20 +090079static const struct pci_device_id vt586b_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
81 { 0, }
82};
83
84MODULE_DEVICE_TABLE (pci, vt586b_ids);
85
Bill Pemberton0b255e92012-11-27 15:59:38 -050086static int vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 u16 base;
89 u8 rev;
90 int res;
91
92 if (pm_io_base) {
93 dev_err(&dev->dev, "i2c-via: Will only support one host\n");
94 return -ENODEV;
95 }
96
97 pci_read_config_byte(dev, PM_CFG_REVID, &rev);
98
99 switch (rev) {
100 case 0x00:
101 base = PM_CFG_IOBASE0;
102 break;
103 case 0x01:
104 case 0x10:
105 base = PM_CFG_IOBASE1;
106 break;
107
108 default:
109 base = PM_CFG_IOBASE1;
110 /* later revision */
111 }
112
113 pci_read_config_word(dev, base, &pm_io_base);
114 pm_io_base &= (0xff << 8);
115
Jean Delvared6072f82005-09-25 16:37:04 +0200116 if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
118 return -ENODEV;
119 }
120
121 outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
122 outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
123
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100124 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 vt586b_adapter.dev.parent = &dev->dev;
126
127 res = i2c_bit_add_bus(&vt586b_adapter);
128 if ( res < 0 ) {
129 release_region(I2C_DIR, IOSPACE);
130 pm_io_base = 0;
131 return res;
132 }
133 return 0;
134}
135
Bill Pemberton0b255e92012-11-27 15:59:38 -0500136static void vt586b_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Jean Delvare32697112006-12-10 21:21:33 +0100138 i2c_del_adapter(&vt586b_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 release_region(I2C_DIR, IOSPACE);
140 pm_io_base = 0;
141}
142
143
144static struct pci_driver vt586b_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .name = "vt586b_smbus",
146 .id_table = vt586b_ids,
147 .probe = vt586b_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500148 .remove = vt586b_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Axel Lin56f21782012-07-24 14:13:56 +0200151module_pci_driver(vt586b_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200153MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
155MODULE_LICENSE("GPL");