Linux Audio

Check our new training course

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