Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     i2c Support for Via Technologies 82C586B South Bridge
0004 
0005     Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
0006 
0007 */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/pci.h>
0012 #include <linux/ioport.h>
0013 #include <linux/i2c.h>
0014 #include <linux/i2c-algo-bit.h>
0015 #include <linux/io.h>
0016 
0017 /* Power management registers */
0018 #define PM_CFG_REVID    0x08    /* silicon revision code */
0019 #define PM_CFG_IOBASE0  0x20
0020 #define PM_CFG_IOBASE1  0x48
0021 
0022 #define I2C_DIR     (pm_io_base+0x40)
0023 #define I2C_OUT     (pm_io_base+0x42)
0024 #define I2C_IN      (pm_io_base+0x44)
0025 #define I2C_SCL     0x02    /* clock bit in DIR/OUT/IN register */
0026 #define I2C_SDA     0x04
0027 
0028 /* io-region reservation */
0029 #define IOSPACE     0x06
0030 
0031 static struct pci_driver vt586b_driver;
0032 static u16 pm_io_base;
0033 
0034 /*
0035    It does not appear from the datasheet that the GPIO pins are
0036    open drain. So a we set a low value by setting the direction to
0037    output and a high value by setting the direction to input and
0038    relying on the required I2C pullup. The data value is initialized
0039    to 0 in via_init() and never changed.
0040 */
0041 static void bit_via_setscl(void *data, int state)
0042 {
0043     outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
0044 }
0045 
0046 static void bit_via_setsda(void *data, int state)
0047 {
0048     outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
0049 }
0050 
0051 static int bit_via_getscl(void *data)
0052 {
0053     return (0 != (inb(I2C_IN) & I2C_SCL));
0054 }
0055 
0056 static int bit_via_getsda(void *data)
0057 {
0058     return (0 != (inb(I2C_IN) & I2C_SDA));
0059 }
0060 
0061 
0062 static struct i2c_algo_bit_data bit_data = {
0063     .setsda     = bit_via_setsda,
0064     .setscl     = bit_via_setscl,
0065     .getsda     = bit_via_getsda,
0066     .getscl     = bit_via_getscl,
0067     .udelay     = 5,
0068     .timeout    = HZ
0069 };
0070 
0071 static struct i2c_adapter vt586b_adapter = {
0072     .owner      = THIS_MODULE,
0073     .class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
0074     .name       = "VIA i2c",
0075     .algo_data  = &bit_data,
0076 };
0077 
0078 
0079 static const struct pci_device_id vt586b_ids[] = {
0080     { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
0081     { 0, }
0082 };
0083 
0084 MODULE_DEVICE_TABLE (pci, vt586b_ids);
0085 
0086 static int vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
0087 {
0088     u16 base;
0089     u8 rev;
0090     int res;
0091 
0092     if (pm_io_base) {
0093         dev_err(&dev->dev, "i2c-via: Will only support one host\n");
0094         return -ENODEV;
0095     }
0096 
0097     pci_read_config_byte(dev, PM_CFG_REVID, &rev);
0098 
0099     switch (rev) {
0100     case 0x00:
0101         base = PM_CFG_IOBASE0;
0102         break;
0103     case 0x01:
0104     case 0x10:
0105         base = PM_CFG_IOBASE1;
0106         break;
0107 
0108     default:
0109         base = PM_CFG_IOBASE1;
0110         /* later revision */
0111     }
0112 
0113     pci_read_config_word(dev, base, &pm_io_base);
0114     pm_io_base &= (0xff << 8);
0115 
0116     if (!request_region(I2C_DIR, IOSPACE, vt586b_driver.name)) {
0117         dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
0118         return -ENODEV;
0119     }
0120 
0121     outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
0122     outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
0123 
0124     /* set up the sysfs linkage to our parent device */
0125     vt586b_adapter.dev.parent = &dev->dev;
0126 
0127     res = i2c_bit_add_bus(&vt586b_adapter);
0128     if ( res < 0 ) {
0129         release_region(I2C_DIR, IOSPACE);
0130         pm_io_base = 0;
0131         return res;
0132     }
0133     return 0;
0134 }
0135 
0136 static void vt586b_remove(struct pci_dev *dev)
0137 {
0138     i2c_del_adapter(&vt586b_adapter);
0139     release_region(I2C_DIR, IOSPACE);
0140     pm_io_base = 0;
0141 }
0142 
0143 
0144 static struct pci_driver vt586b_driver = {
0145     .name       = "vt586b_smbus",
0146     .id_table   = vt586b_ids,
0147     .probe      = vt586b_probe,
0148     .remove     = vt586b_remove,
0149 };
0150 
0151 module_pci_driver(vt586b_driver);
0152 
0153 MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
0154 MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
0155 MODULE_LICENSE("GPL");