2 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
3 Philip Edelbrock <phil@netroedge.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 /* Note: we assume there can only be one SIS5595 with one SMBus interface */
19 Note: all have mfr. ID 0x1039.
23 Note: these chips contain a 0008 device which is incompatible with the
24 5595. We recognize these by the presence of the listed
25 "blacklist" PCI ID and refuse to load.
27 NOT SUPPORTED PCI ID BLACKLIST PCI ID
48 * Add Block Transfers (ugly, but supported by the adapter)
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/delay.h>
55 #include <linux/pci.h>
56 #include <linux/ioport.h>
57 #include <linux/init.h>
58 #include <linux/i2c.h>
59 #include <linux/acpi.h>
62 static int blacklist
[] = {
75 PCI_DEVICE_ID_SI_5511
, /* 5513 chip has the 0008 device but that ID
76 shows up in other chips so we use the 5511
78 PCI_DEVICE_ID_SI_5597
,
79 PCI_DEVICE_ID_SI_5598
,
80 0, /* terminates the list */
83 /* Length of ISA address segment */
84 #define SIS5595_EXTENT 8
85 /* SIS5595 SMBus registers */
86 #define SMB_STS_LO 0x00
87 #define SMB_STS_HI 0x01
88 #define SMB_CTL_LO 0x02
89 #define SMB_CTL_HI 0x03
100 /* PCI Address Constants */
101 #define SMB_INDEX 0x38
103 #define SIS5595_ENABLE_REG 0x40
104 #define ACPI_BASE 0x90
107 #define MAX_TIMEOUT 500
109 /* SIS5595 constants */
110 #define SIS5595_QUICK 0x00
111 #define SIS5595_BYTE 0x02
112 #define SIS5595_BYTE_DATA 0x04
113 #define SIS5595_WORD_DATA 0x06
114 #define SIS5595_PROC_CALL 0x08
115 #define SIS5595_BLOCK_DATA 0x0A
117 /* insmod parameters */
119 /* If force_addr is set to anything different from 0, we forcibly enable
120 the device at the given address. */
121 static u16 force_addr
;
122 module_param_hw(force_addr
, ushort
, ioport
, 0);
123 MODULE_PARM_DESC(force_addr
, "Initialize the base address of the i2c controller");
125 static struct pci_driver sis5595_driver
;
126 static unsigned short sis5595_base
;
127 static struct pci_dev
*sis5595_pdev
;
129 static u8
sis5595_read(u8 reg
)
131 outb(reg
, sis5595_base
+ SMB_INDEX
);
132 return inb(sis5595_base
+ SMB_DAT
);
135 static void sis5595_write(u8 reg
, u8 data
)
137 outb(reg
, sis5595_base
+ SMB_INDEX
);
138 outb(data
, sis5595_base
+ SMB_DAT
);
141 static int sis5595_setup(struct pci_dev
*SIS5595_dev
)
148 /* Look for imposters */
149 for (i
= blacklist
; *i
!= 0; i
++) {
151 dev
= pci_get_device(PCI_VENDOR_ID_SI
, *i
, NULL
);
153 dev_err(&SIS5595_dev
->dev
, "Looked for SIS5595 but found unsupported device %.4x\n", *i
);
159 /* Determine the address of the SMBus areas */
160 pci_read_config_word(SIS5595_dev
, ACPI_BASE
, &sis5595_base
);
161 if (sis5595_base
== 0 && force_addr
== 0) {
162 dev_err(&SIS5595_dev
->dev
, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
167 sis5595_base
= force_addr
& ~(SIS5595_EXTENT
- 1);
168 dev_dbg(&SIS5595_dev
->dev
, "ACPI Base address: %04x\n", sis5595_base
);
170 /* NB: We grab just the two SMBus registers here, but this may still
171 * interfere with ACPI :-( */
172 retval
= acpi_check_region(sis5595_base
+ SMB_INDEX
, 2,
173 sis5595_driver
.name
);
177 if (!request_region(sis5595_base
+ SMB_INDEX
, 2,
178 sis5595_driver
.name
)) {
179 dev_err(&SIS5595_dev
->dev
, "SMBus registers 0x%04x-0x%04x already in use!\n",
180 sis5595_base
+ SMB_INDEX
, sis5595_base
+ SMB_INDEX
+ 1);
185 dev_info(&SIS5595_dev
->dev
, "forcing ISA address 0x%04X\n", sis5595_base
);
186 if (pci_write_config_word(SIS5595_dev
, ACPI_BASE
, sis5595_base
)
187 != PCIBIOS_SUCCESSFUL
)
189 if (pci_read_config_word(SIS5595_dev
, ACPI_BASE
, &a
)
190 != PCIBIOS_SUCCESSFUL
)
192 if ((a
& ~(SIS5595_EXTENT
- 1)) != sis5595_base
) {
193 /* doesn't work for some chips! */
194 dev_err(&SIS5595_dev
->dev
, "force address failed - not supported?\n");
199 if (pci_read_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, &val
)
200 != PCIBIOS_SUCCESSFUL
)
202 if ((val
& 0x80) == 0) {
203 dev_info(&SIS5595_dev
->dev
, "enabling ACPI\n");
204 if (pci_write_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, val
| 0x80)
205 != PCIBIOS_SUCCESSFUL
)
207 if (pci_read_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, &val
)
208 != PCIBIOS_SUCCESSFUL
)
210 if ((val
& 0x80) == 0) {
211 /* doesn't work for some chips? */
212 dev_err(&SIS5595_dev
->dev
, "ACPI enable failed - not supported?\n");
217 /* Everything is happy */
221 release_region(sis5595_base
+ SMB_INDEX
, 2);
225 static int sis5595_transaction(struct i2c_adapter
*adap
)
231 /* Make sure the SMBus host is ready to start transmitting */
232 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
234 dev_dbg(&adap
->dev
, "SMBus busy (%04x). Resetting...\n", temp
);
235 sis5595_write(SMB_STS_LO
, temp
& 0xff);
236 sis5595_write(SMB_STS_HI
, temp
>> 8);
237 if ((temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8)) != 0x00) {
238 dev_dbg(&adap
->dev
, "Failed! (%02x)\n", temp
);
241 dev_dbg(&adap
->dev
, "Successful!\n");
245 /* start the transaction by setting bit 4 */
246 sis5595_write(SMB_CTL_LO
, sis5595_read(SMB_CTL_LO
) | 0x10);
248 /* We will always wait for a fraction of a second! */
251 temp
= sis5595_read(SMB_STS_LO
);
252 } while (!(temp
& 0x40) && (timeout
++ < MAX_TIMEOUT
));
254 /* If the SMBus is still busy, we give up */
255 if (timeout
> MAX_TIMEOUT
) {
256 dev_dbg(&adap
->dev
, "SMBus Timeout!\n");
261 dev_dbg(&adap
->dev
, "Error: Failed bus transaction\n");
266 dev_err(&adap
->dev
, "Bus collision! SMBus may be locked until "
267 "next hard reset (or not...)\n");
268 /* Clock stops and slave is stuck in mid-transmission */
272 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
274 sis5595_write(SMB_STS_LO
, temp
& 0xff);
275 sis5595_write(SMB_STS_HI
, temp
>> 8);
278 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
280 dev_dbg(&adap
->dev
, "Failed reset at end of transaction (%02x)\n", temp
);
285 /* Return negative errno on error. */
286 static s32
sis5595_access(struct i2c_adapter
*adap
, u16 addr
,
287 unsigned short flags
, char read_write
,
288 u8 command
, int size
, union i2c_smbus_data
*data
)
293 case I2C_SMBUS_QUICK
:
294 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
295 size
= SIS5595_QUICK
;
298 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
299 if (read_write
== I2C_SMBUS_WRITE
)
300 sis5595_write(SMB_CMD
, command
);
303 case I2C_SMBUS_BYTE_DATA
:
304 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
305 sis5595_write(SMB_CMD
, command
);
306 if (read_write
== I2C_SMBUS_WRITE
)
307 sis5595_write(SMB_BYTE
, data
->byte
);
308 size
= SIS5595_BYTE_DATA
;
310 case I2C_SMBUS_PROC_CALL
:
311 case I2C_SMBUS_WORD_DATA
:
312 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
313 sis5595_write(SMB_CMD
, command
);
314 if (read_write
== I2C_SMBUS_WRITE
) {
315 sis5595_write(SMB_BYTE
, data
->word
& 0xff);
316 sis5595_write(SMB_BYTE
+ 1,
317 (data
->word
& 0xff00) >> 8);
319 size
= (size
== I2C_SMBUS_PROC_CALL
) ? SIS5595_PROC_CALL
: SIS5595_WORD_DATA
;
322 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
326 sis5595_write(SMB_CTL_LO
, ((size
& 0x0E)));
328 status
= sis5595_transaction(adap
);
332 if ((size
!= SIS5595_PROC_CALL
) &&
333 ((read_write
== I2C_SMBUS_WRITE
) || (size
== SIS5595_QUICK
)))
339 case SIS5595_BYTE_DATA
:
340 data
->byte
= sis5595_read(SMB_BYTE
);
342 case SIS5595_WORD_DATA
:
343 case SIS5595_PROC_CALL
:
344 data
->word
= sis5595_read(SMB_BYTE
) + (sis5595_read(SMB_BYTE
+ 1) << 8);
350 static u32
sis5595_func(struct i2c_adapter
*adapter
)
352 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
353 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
354 I2C_FUNC_SMBUS_PROC_CALL
;
357 static const struct i2c_algorithm smbus_algorithm
= {
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
| I2C_CLASS_SPD
,
365 .algo
= &smbus_algorithm
,
368 static const struct pci_device_id sis5595_ids
[] = {
369 { PCI_DEVICE(PCI_VENDOR_ID_SI
, PCI_DEVICE_ID_SI_503
) },
373 MODULE_DEVICE_TABLE (pci
, sis5595_ids
);
375 static int sis5595_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
379 if (sis5595_setup(dev
)) {
380 dev_err(&dev
->dev
, "SIS5595 not detected, module not inserted.\n");
384 /* set up the sysfs linkage to our parent device */
385 sis5595_adapter
.dev
.parent
= &dev
->dev
;
387 snprintf(sis5595_adapter
.name
, sizeof(sis5595_adapter
.name
),
388 "SMBus SIS5595 adapter at %04x", sis5595_base
+ SMB_INDEX
);
389 err
= i2c_add_adapter(&sis5595_adapter
);
391 release_region(sis5595_base
+ SMB_INDEX
, 2);
395 /* Always return failure here. This is to allow other drivers to bind
396 * to this pci device. We don't really want to have control over the
397 * pci device, we only wanted to read as few register values from it.
399 sis5595_pdev
= pci_dev_get(dev
);
403 static struct pci_driver sis5595_driver
= {
404 .name
= "sis5595_smbus",
405 .id_table
= sis5595_ids
,
406 .probe
= sis5595_probe
,
409 static int __init
i2c_sis5595_init(void)
411 return pci_register_driver(&sis5595_driver
);
414 static void __exit
i2c_sis5595_exit(void)
416 pci_unregister_driver(&sis5595_driver
);
418 i2c_del_adapter(&sis5595_adapter
);
419 release_region(sis5595_base
+ SMB_INDEX
, 2);
420 pci_dev_put(sis5595_pdev
);
425 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
426 MODULE_DESCRIPTION("SIS5595 SMBus driver");
427 MODULE_LICENSE("GPL");
429 module_init(i2c_sis5595_init
);
430 module_exit(i2c_sis5595_exit
);