2 sis5595.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* Note: we assume there can only be one SIS5595 with one SMBus interface */
25 Note: all have mfr. ID 0x1039.
29 Note: these chips contain a 0008 device which is incompatible with the
30 5595. We recognize these by the presence of the listed
31 "blacklist" PCI ID and refuse to load.
33 NOT SUPPORTED PCI ID BLACKLIST PCI ID
54 * Add Block Transfers (ugly, but supported by the adapter)
58 #include <linux/config.h>
59 #include <linux/kernel.h>
60 #include <linux/module.h>
61 #include <linux/delay.h>
62 #include <linux/pci.h>
63 #include <linux/ioport.h>
64 #include <linux/init.h>
65 #include <linux/i2c.h>
68 static int blacklist[] = {
81 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but that ID
82 shows up in other chips so we use the 5511
84 PCI_DEVICE_ID_SI_5597,
85 PCI_DEVICE_ID_SI_5598,
86 0, /* terminates the list */
89 /* Length of ISA address segment */
90 #define SIS5595_EXTENT 8
91 /* SIS5595 SMBus registers */
92 #define SMB_STS_LO 0x00
93 #define SMB_STS_HI 0x01
94 #define SMB_CTL_LO 0x02
95 #define SMB_CTL_HI 0x03
100 #define SMB_BYTE 0x08
106 /* PCI Address Constants */
107 #define SMB_INDEX 0x38
109 #define SIS5595_ENABLE_REG 0x40
110 #define ACPI_BASE 0x90
113 #define MAX_TIMEOUT 500
115 /* SIS5595 constants */
116 #define SIS5595_QUICK 0x00
117 #define SIS5595_BYTE 0x02
118 #define SIS5595_BYTE_DATA 0x04
119 #define SIS5595_WORD_DATA 0x06
120 #define SIS5595_PROC_CALL 0x08
121 #define SIS5595_BLOCK_DATA 0x0A
123 /* insmod parameters */
125 /* If force_addr is set to anything different from 0, we forcibly enable
126 the device at the given address. */
127 static u16 force_addr = 0;
128 module_param(force_addr, ushort, 0);
129 MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
131 static unsigned short sis5595_base = 0;
133 static u8 sis5595_read(u8 reg)
135 outb(reg, sis5595_base + SMB_INDEX);
136 return inb(sis5595_base + SMB_DAT);
139 static void sis5595_write(u8 reg, u8 data)
141 outb(reg, sis5595_base + SMB_INDEX);
142 outb(data, sis5595_base + SMB_DAT);
145 static int sis5595_setup(struct pci_dev *SIS5595_dev)
150 int retval = -ENODEV;
152 /* Look for imposters */
153 for (i = blacklist; *i != 0; i++) {
155 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
157 dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
163 /* Determine the address of the SMBus areas */
164 pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
165 if (sis5595_base == 0 && force_addr == 0) {
166 dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
171 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
172 dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
174 /* NB: We grab just the two SMBus registers here, but this may still
175 * interfere with ACPI :-( */
176 if (!request_region(sis5595_base + SMB_INDEX, 2, "sis5595-smbus")) {
177 dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
178 sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
183 dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
184 if (!pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base))
186 if (!pci_read_config_word(SIS5595_dev, ACPI_BASE, &a))
188 if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
189 /* doesn't work for some chips! */
190 dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
195 if (!pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val))
197 if ((val & 0x80) == 0) {
198 dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
199 if (!pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80))
201 if (!pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val))
203 if ((val & 0x80) == 0) {
204 /* doesn't work for some chips? */
205 dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
210 /* Everything is happy */
214 release_region(sis5595_base + SMB_INDEX, 2);
218 static int sis5595_transaction(struct i2c_adapter *adap)
224 /* Make sure the SMBus host is ready to start transmitting */
225 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
227 dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting... \n", temp);
228 sis5595_write(SMB_STS_LO, temp & 0xff);
229 sis5595_write(SMB_STS_HI, temp >> 8);
230 if ((temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8)) != 0x00) {
231 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
234 dev_dbg(&adap->dev, "Successfull!\n");
238 /* start the transaction by setting bit 4 */
239 sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
241 /* We will always wait for a fraction of a second! */
244 temp = sis5595_read(SMB_STS_LO);
245 } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
247 /* If the SMBus is still busy, we give up */
248 if (timeout >= MAX_TIMEOUT) {
249 dev_dbg(&adap->dev, "SMBus Timeout!\n");
254 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
259 dev_err(&adap->dev, "Bus collision! SMBus may be locked until "
260 "next hard reset (or not...)\n");
261 /* Clock stops and slave is stuck in mid-transmission */
265 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
267 sis5595_write(SMB_STS_LO, temp & 0xff);
268 sis5595_write(SMB_STS_HI, temp >> 8);
271 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
273 dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
278 /* Return -1 on error. */
279 static s32 sis5595_access(struct i2c_adapter *adap, u16 addr,
280 unsigned short flags, char read_write,
281 u8 command, int size, union i2c_smbus_data *data)
284 case I2C_SMBUS_QUICK:
285 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
286 size = SIS5595_QUICK;
289 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
290 if (read_write == I2C_SMBUS_WRITE)
291 sis5595_write(SMB_CMD, command);
294 case I2C_SMBUS_BYTE_DATA:
295 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
296 sis5595_write(SMB_CMD, command);
297 if (read_write == I2C_SMBUS_WRITE)
298 sis5595_write(SMB_BYTE, data->byte);
299 size = SIS5595_BYTE_DATA;
301 case I2C_SMBUS_PROC_CALL:
302 case I2C_SMBUS_WORD_DATA:
303 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
304 sis5595_write(SMB_CMD, command);
305 if (read_write == I2C_SMBUS_WRITE) {
306 sis5595_write(SMB_BYTE, data->word & 0xff);
307 sis5595_write(SMB_BYTE + 1,
308 (data->word & 0xff00) >> 8);
310 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
313 case I2C_SMBUS_BLOCK_DATA:
314 printk(KERN_WARNING "sis5595.o: Block data not yet implemented!\n");
319 printk(KERN_WARNING "sis5595.o: Unsupported transaction %d\n", size);
323 sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
325 if (sis5595_transaction(adap))
328 if ((size != SIS5595_PROC_CALL) &&
329 ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
334 case SIS5595_BYTE: /* Where is the result put? I assume here it is in
335 SMB_DATA but it might just as well be in the
336 SMB_CMD. No clue in the docs */
337 case SIS5595_BYTE_DATA:
338 data->byte = sis5595_read(SMB_BYTE);
340 case SIS5595_WORD_DATA:
341 case SIS5595_PROC_CALL:
342 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
348 static u32 sis5595_func(struct i2c_adapter *adapter)
350 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
351 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
352 I2C_FUNC_SMBUS_PROC_CALL;
355 static struct i2c_algorithm smbus_algorithm = {
356 .name = "Non-I2C SMBus adapter",
357 .id = I2C_ALGO_SMBUS,
358 .smbus_xfer = sis5595_access,
359 .functionality = sis5595_func,
362 static struct i2c_adapter sis5595_adapter = {
363 .owner = THIS_MODULE,
364 .class = I2C_CLASS_HWMON,
366 .algo = &smbus_algorithm,
369 static struct pci_device_id sis5595_ids[] __devinitdata = {
370 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
374 static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
376 if (sis5595_setup(dev)) {
377 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
381 /* set up the driverfs linkage to our parent device */
382 sis5595_adapter.dev.parent = &dev->dev;
384 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
385 sis5595_base + SMB_INDEX);
386 return i2c_add_adapter(&sis5595_adapter);
389 static void __devexit sis5595_remove(struct pci_dev *dev)
391 i2c_del_adapter(&sis5595_adapter);
392 release_region(sis5595_base + SMB_INDEX, 2);
395 static struct pci_driver sis5595_driver = {
396 .name = "sis5595_smbus",
397 .id_table = sis5595_ids,
398 .probe = sis5595_probe,
399 .remove = __devexit_p(sis5595_remove),
402 static int __init i2c_sis5595_init(void)
404 return pci_module_init(&sis5595_driver);
407 static void __exit i2c_sis5595_exit(void)
409 pci_unregister_driver(&sis5595_driver);
412 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
413 MODULE_DESCRIPTION("SIS5595 SMBus driver");
414 MODULE_LICENSE("GPL");
416 module_init(i2c_sis5595_init);
417 module_exit(i2c_sis5595_exit);