blob: be662511c58bf931aef8e57de31905d710952363 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 i2c Support for Via Technologies 82C586B South Bridge
3
Jan Engelhardt96de0e22007-10-19 23:21:04 +02004 Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
H Hartley Sweeten21782182010-05-21 18:41:01 +020028#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvared6072f82005-09-25 16:37:04 +020044static struct pci_driver vt586b_driver;
Jean Delvare60507092005-09-25 16:23:07 +020045static u16 pm_io_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
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*/
54static 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
59static 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
64static int bit_via_getscl(void *data)
65{
66 return (0 != (inb(I2C_IN) & I2C_SCL));
67}
68
69static int bit_via_getsda(void *data)
70{
71 return (0 != (inb(I2C_IN) & I2C_SDA));
72}
73
74
75static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .timeout = HZ
82};
83
84static struct i2c_adapter vt586b_adapter = {
85 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +020086 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .name = "VIA i2c",
88 .algo_data = &bit_data,
89};
90
91
Axel Lin3527bd52012-01-12 20:32:04 +010092static DEFINE_PCI_DEVICE_TABLE(vt586b_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
94 { 0, }
95};
96
97MODULE_DEVICE_TABLE (pci, vt586b_ids);
98
Bill Pemberton0b255e92012-11-27 15:59:38 -050099static int vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 u16 base;
102 u8 rev;
103 int res;
104
105 if (pm_io_base) {
106 dev_err(&dev->dev, "i2c-via: Will only support one host\n");
107 return -ENODEV;
108 }
109
110 pci_read_config_byte(dev, PM_CFG_REVID, &rev);
111
112 switch (rev) {
113 case 0x00:
114 base = PM_CFG_IOBASE0;
115 break;
116 case 0x01:
117 case 0x10:
118 base = PM_CFG_IOBASE1;
119 break;
120
121 default:
122 base = PM_CFG_IOBASE1;
123 /* later revision */
124 }
125
126 pci_read_config_word(dev, base, &pm_io_base);
127 pm_io_base &= (0xff << 8);
128
Jean Delvared6072f82005-09-25 16:37:04 +0200129 if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
131 return -ENODEV;
132 }
133
134 outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
135 outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
136
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100137 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 vt586b_adapter.dev.parent = &dev->dev;
139
140 res = i2c_bit_add_bus(&vt586b_adapter);
141 if ( res < 0 ) {
142 release_region(I2C_DIR, IOSPACE);
143 pm_io_base = 0;
144 return res;
145 }
146 return 0;
147}
148
Bill Pemberton0b255e92012-11-27 15:59:38 -0500149static void vt586b_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Jean Delvare32697112006-12-10 21:21:33 +0100151 i2c_del_adapter(&vt586b_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 release_region(I2C_DIR, IOSPACE);
153 pm_io_base = 0;
154}
155
156
157static struct pci_driver vt586b_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .name = "vt586b_smbus",
159 .id_table = vt586b_ids,
160 .probe = vt586b_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500161 .remove = vt586b_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Axel Lin56f21782012-07-24 14:13:56 +0200164module_pci_driver(vt586b_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200166MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
168MODULE_LICENSE("GPL");