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