1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
4 Philip Edelbrock <phil@netroedge.com>
8 /* Note: we assume there can only be one SIS5595 with one SMBus interface */
11 Note: all have mfr. ID 0x1039.
15 Note: these chips contain a 0008 device which is incompatible with the
16 5595. We recognize these by the presence of the listed
17 "blacklist" PCI ID and refuse to load.
19 NOT SUPPORTED PCI ID BLACKLIST PCI ID
40 * Add Block Transfers (ugly, but supported by the adapter)
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/delay.h>
47 #include <linux/pci.h>
48 #include <linux/ioport.h>
49 #include <linux/init.h>
50 #include <linux/i2c.h>
51 #include <linux/acpi.h>
54 static int blacklist
[] = {
67 PCI_DEVICE_ID_SI_5511
, /* 5513 chip has the 0008 device but that ID
68 shows up in other chips so we use the 5511
70 PCI_DEVICE_ID_SI_5597
,
71 PCI_DEVICE_ID_SI_5598
,
72 0, /* terminates the list */
75 /* Length of ISA address segment */
76 #define SIS5595_EXTENT 8
77 /* SIS5595 SMBus registers */
78 #define SMB_STS_LO 0x00
79 #define SMB_STS_HI 0x01
80 #define SMB_CTL_LO 0x02
81 #define SMB_CTL_HI 0x03
92 /* PCI Address Constants */
93 #define SMB_INDEX 0x38
95 #define SIS5595_ENABLE_REG 0x40
96 #define ACPI_BASE 0x90
99 #define MAX_TIMEOUT 500
101 /* SIS5595 constants */
102 #define SIS5595_QUICK 0x00
103 #define SIS5595_BYTE 0x02
104 #define SIS5595_BYTE_DATA 0x04
105 #define SIS5595_WORD_DATA 0x06
106 #define SIS5595_PROC_CALL 0x08
107 #define SIS5595_BLOCK_DATA 0x0A
109 /* insmod parameters */
111 /* If force_addr is set to anything different from 0, we forcibly enable
112 the device at the given address. */
113 static u16 force_addr
;
114 module_param_hw(force_addr
, ushort
, ioport
, 0);
115 MODULE_PARM_DESC(force_addr
, "Initialize the base address of the i2c controller");
117 static struct pci_driver sis5595_driver
;
118 static unsigned short sis5595_base
;
119 static struct pci_dev
*sis5595_pdev
;
121 static u8
sis5595_read(u8 reg
)
123 outb(reg
, sis5595_base
+ SMB_INDEX
);
124 return inb(sis5595_base
+ SMB_DAT
);
127 static void sis5595_write(u8 reg
, u8 data
)
129 outb(reg
, sis5595_base
+ SMB_INDEX
);
130 outb(data
, sis5595_base
+ SMB_DAT
);
133 static int sis5595_setup(struct pci_dev
*SIS5595_dev
)
140 /* Look for imposters */
141 for (i
= blacklist
; *i
!= 0; i
++) {
143 dev
= pci_get_device(PCI_VENDOR_ID_SI
, *i
, NULL
);
145 dev_err(&SIS5595_dev
->dev
, "Looked for SIS5595 but found unsupported device %.4x\n", *i
);
151 /* Determine the address of the SMBus areas */
152 pci_read_config_word(SIS5595_dev
, ACPI_BASE
, &sis5595_base
);
153 if (sis5595_base
== 0 && force_addr
== 0) {
154 dev_err(&SIS5595_dev
->dev
, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
159 sis5595_base
= force_addr
& ~(SIS5595_EXTENT
- 1);
160 dev_dbg(&SIS5595_dev
->dev
, "ACPI Base address: %04x\n", sis5595_base
);
162 /* NB: We grab just the two SMBus registers here, but this may still
163 * interfere with ACPI :-( */
164 retval
= acpi_check_region(sis5595_base
+ SMB_INDEX
, 2,
165 sis5595_driver
.name
);
169 if (!request_region(sis5595_base
+ SMB_INDEX
, 2,
170 sis5595_driver
.name
)) {
171 dev_err(&SIS5595_dev
->dev
, "SMBus registers 0x%04x-0x%04x already in use!\n",
172 sis5595_base
+ SMB_INDEX
, sis5595_base
+ SMB_INDEX
+ 1);
177 dev_info(&SIS5595_dev
->dev
, "forcing ISA address 0x%04X\n", sis5595_base
);
178 if (pci_write_config_word(SIS5595_dev
, ACPI_BASE
, sis5595_base
)
179 != PCIBIOS_SUCCESSFUL
)
181 if (pci_read_config_word(SIS5595_dev
, ACPI_BASE
, &a
)
182 != PCIBIOS_SUCCESSFUL
)
184 if ((a
& ~(SIS5595_EXTENT
- 1)) != sis5595_base
) {
185 /* doesn't work for some chips! */
186 dev_err(&SIS5595_dev
->dev
, "force address failed - not supported?\n");
191 if (pci_read_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, &val
)
192 != PCIBIOS_SUCCESSFUL
)
194 if ((val
& 0x80) == 0) {
195 dev_info(&SIS5595_dev
->dev
, "enabling ACPI\n");
196 if (pci_write_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, val
| 0x80)
197 != PCIBIOS_SUCCESSFUL
)
199 if (pci_read_config_byte(SIS5595_dev
, SIS5595_ENABLE_REG
, &val
)
200 != PCIBIOS_SUCCESSFUL
)
202 if ((val
& 0x80) == 0) {
203 /* doesn't work for some chips? */
204 dev_err(&SIS5595_dev
->dev
, "ACPI enable failed - not supported?\n");
209 /* Everything is happy */
213 release_region(sis5595_base
+ SMB_INDEX
, 2);
217 static int sis5595_transaction(struct i2c_adapter
*adap
)
223 /* Make sure the SMBus host is ready to start transmitting */
224 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
226 dev_dbg(&adap
->dev
, "SMBus busy (%04x). Resetting...\n", temp
);
227 sis5595_write(SMB_STS_LO
, temp
& 0xff);
228 sis5595_write(SMB_STS_HI
, temp
>> 8);
229 if ((temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8)) != 0x00) {
230 dev_dbg(&adap
->dev
, "Failed! (%02x)\n", temp
);
233 dev_dbg(&adap
->dev
, "Successful!\n");
237 /* start the transaction by setting bit 4 */
238 sis5595_write(SMB_CTL_LO
, sis5595_read(SMB_CTL_LO
) | 0x10);
240 /* We will always wait for a fraction of a second! */
243 temp
= sis5595_read(SMB_STS_LO
);
244 } while (!(temp
& 0x40) && (timeout
++ < MAX_TIMEOUT
));
246 /* If the SMBus is still busy, we give up */
247 if (timeout
> MAX_TIMEOUT
) {
248 dev_dbg(&adap
->dev
, "SMBus Timeout!\n");
253 dev_dbg(&adap
->dev
, "Error: Failed bus transaction\n");
258 dev_err(&adap
->dev
, "Bus collision! SMBus may be locked until "
259 "next hard reset (or not...)\n");
260 /* Clock stops and slave is stuck in mid-transmission */
264 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
266 sis5595_write(SMB_STS_LO
, temp
& 0xff);
267 sis5595_write(SMB_STS_HI
, temp
>> 8);
270 temp
= sis5595_read(SMB_STS_LO
) + (sis5595_read(SMB_STS_HI
) << 8);
272 dev_dbg(&adap
->dev
, "Failed reset at end of transaction (%02x)\n", temp
);
277 /* Return negative errno on error. */
278 static s32
sis5595_access(struct i2c_adapter
*adap
, u16 addr
,
279 unsigned short flags
, char read_write
,
280 u8 command
, int size
, union i2c_smbus_data
*data
)
285 case I2C_SMBUS_QUICK
:
286 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
287 size
= SIS5595_QUICK
;
290 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
291 if (read_write
== I2C_SMBUS_WRITE
)
292 sis5595_write(SMB_CMD
, command
);
295 case I2C_SMBUS_BYTE_DATA
:
296 sis5595_write(SMB_ADDR
, ((addr
& 0x7f) << 1) | (read_write
& 0x01));
297 sis5595_write(SMB_CMD
, command
);
298 if (read_write
== I2C_SMBUS_WRITE
)
299 sis5595_write(SMB_BYTE
, data
->byte
);
300 size
= SIS5595_BYTE_DATA
;
302 case I2C_SMBUS_PROC_CALL
:
303 case I2C_SMBUS_WORD_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
->word
& 0xff);
308 sis5595_write(SMB_BYTE
+ 1,
309 (data
->word
& 0xff00) >> 8);
311 size
= (size
== I2C_SMBUS_PROC_CALL
) ? SIS5595_PROC_CALL
: SIS5595_WORD_DATA
;
314 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
318 sis5595_write(SMB_CTL_LO
, ((size
& 0x0E)));
320 status
= sis5595_transaction(adap
);
324 if ((size
!= SIS5595_PROC_CALL
) &&
325 ((read_write
== I2C_SMBUS_WRITE
) || (size
== SIS5595_QUICK
)))
331 case SIS5595_BYTE_DATA
:
332 data
->byte
= sis5595_read(SMB_BYTE
);
334 case SIS5595_WORD_DATA
:
335 case SIS5595_PROC_CALL
:
336 data
->word
= sis5595_read(SMB_BYTE
) + (sis5595_read(SMB_BYTE
+ 1) << 8);
342 static u32
sis5595_func(struct i2c_adapter
*adapter
)
344 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
345 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
346 I2C_FUNC_SMBUS_PROC_CALL
;
349 static const struct i2c_algorithm smbus_algorithm
= {
350 .smbus_xfer
= sis5595_access
,
351 .functionality
= sis5595_func
,
354 static struct i2c_adapter sis5595_adapter
= {
355 .owner
= THIS_MODULE
,
356 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
357 .algo
= &smbus_algorithm
,
360 static const struct pci_device_id sis5595_ids
[] = {
361 { PCI_DEVICE(PCI_VENDOR_ID_SI
, PCI_DEVICE_ID_SI_503
) },
365 MODULE_DEVICE_TABLE (pci
, sis5595_ids
);
367 static int sis5595_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
371 if (sis5595_setup(dev
)) {
372 dev_err(&dev
->dev
, "SIS5595 not detected, module not inserted.\n");
376 /* set up the sysfs linkage to our parent device */
377 sis5595_adapter
.dev
.parent
= &dev
->dev
;
379 snprintf(sis5595_adapter
.name
, sizeof(sis5595_adapter
.name
),
380 "SMBus SIS5595 adapter at %04x", sis5595_base
+ SMB_INDEX
);
381 err
= i2c_add_adapter(&sis5595_adapter
);
383 release_region(sis5595_base
+ SMB_INDEX
, 2);
387 /* Always return failure here. This is to allow other drivers to bind
388 * to this pci device. We don't really want to have control over the
389 * pci device, we only wanted to read as few register values from it.
391 sis5595_pdev
= pci_dev_get(dev
);
395 static struct pci_driver sis5595_driver
= {
396 .name
= "sis5595_smbus",
397 .id_table
= sis5595_ids
,
398 .probe
= sis5595_probe
,
401 static int __init
i2c_sis5595_init(void)
403 return pci_register_driver(&sis5595_driver
);
406 static void __exit
i2c_sis5595_exit(void)
408 pci_unregister_driver(&sis5595_driver
);
410 i2c_del_adapter(&sis5595_adapter
);
411 release_region(sis5595_base
+ SMB_INDEX
, 2);
412 pci_dev_put(sis5595_pdev
);
417 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
418 MODULE_DESCRIPTION("SIS5595 SMBus driver");
419 MODULE_LICENSE("GPL");
421 module_init(i2c_sis5595_init
);
422 module_exit(i2c_sis5595_exit
);