blob: c793a5c14cda1bdbc8ee402e1efaf3d06eb0a700 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
4 Philip Edelbrock <phil@netroedge.com>
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006*/
7
8/* Note: we assume there can only be one SIS5595 with one SMBus interface */
9
10/*
11 Note: all have mfr. ID 0x1039.
12 SUPPORTED PCI ID
13 5595 0008
14
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.
18
19 NOT SUPPORTED PCI ID BLACKLIST PCI ID
20 540 0008 0540
21 550 0008 0550
22 5513 0008 5511
23 5581 0008 5597
24 5582 0008 5597
25 5597 0008 5597
26 5598 0008 5597/5598
27 630 0008 0630
28 645 0008 0645
29 646 0008 0646
30 648 0008 0648
31 650 0008 0650
32 651 0008 0651
33 730 0008 0730
34 735 0008 0735
35 745 0008 0745
36 746 0008 0746
37*/
38
39/* TO DO:
40 * Add Block Transfers (ugly, but supported by the adapter)
41 * Add adapter resets
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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>
Jean Delvare54fb4a052008-07-14 22:38:33 +020051#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static int blacklist[] = {
55 PCI_DEVICE_ID_SI_540,
56 PCI_DEVICE_ID_SI_550,
57 PCI_DEVICE_ID_SI_630,
58 PCI_DEVICE_ID_SI_645,
59 PCI_DEVICE_ID_SI_646,
60 PCI_DEVICE_ID_SI_648,
61 PCI_DEVICE_ID_SI_650,
62 PCI_DEVICE_ID_SI_651,
63 PCI_DEVICE_ID_SI_730,
64 PCI_DEVICE_ID_SI_735,
65 PCI_DEVICE_ID_SI_745,
66 PCI_DEVICE_ID_SI_746,
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
69 ID for recognition */
70 PCI_DEVICE_ID_SI_5597,
71 PCI_DEVICE_ID_SI_5598,
72 0, /* terminates the list */
73};
74
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
82#define SMB_ADDR 0x04
83#define SMB_CMD 0x05
84#define SMB_PCNT 0x06
85#define SMB_CNT 0x07
86#define SMB_BYTE 0x08
87#define SMB_DEV 0x10
88#define SMB_DB0 0x11
89#define SMB_DB1 0x12
90#define SMB_HAA 0x13
91
92/* PCI Address Constants */
93#define SMB_INDEX 0x38
94#define SMB_DAT 0x39
95#define SIS5595_ENABLE_REG 0x40
96#define ACPI_BASE 0x90
97
98/* Other settings */
99#define MAX_TIMEOUT 500
100
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
108
109/* insmod parameters */
110
111/* If force_addr is set to anything different from 0, we forcibly enable
112 the device at the given address. */
Jean Delvare60507092005-09-25 16:23:07 +0200113static u16 force_addr;
David Howellsc78babc2017-04-04 16:54:23 +0100114module_param_hw(force_addr, ushort, ioport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
116
Jean Delvared6072f82005-09-25 16:37:04 +0200117static struct pci_driver sis5595_driver;
Jean Delvare60507092005-09-25 16:23:07 +0200118static unsigned short sis5595_base;
Jean Delvare7375cd82007-07-12 14:12:30 +0200119static struct pci_dev *sis5595_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static u8 sis5595_read(u8 reg)
122{
123 outb(reg, sis5595_base + SMB_INDEX);
124 return inb(sis5595_base + SMB_DAT);
125}
126
127static void sis5595_write(u8 reg, u8 data)
128{
129 outb(reg, sis5595_base + SMB_INDEX);
130 outb(data, sis5595_base + SMB_DAT);
131}
132
Bill Pemberton0b255e92012-11-27 15:59:38 -0500133static int sis5595_setup(struct pci_dev *SIS5595_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 u16 a;
136 u8 val;
137 int *i;
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100138 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Look for imposters */
141 for (i = blacklist; *i != 0; i++) {
142 struct pci_dev *dev;
143 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
144 if (dev) {
145 dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
146 pci_dev_put(dev);
147 return -ENODEV;
148 }
149 }
150
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");
155 return -ENODEV;
156 }
157
158 if (force_addr)
159 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
160 dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
161
162 /* NB: We grab just the two SMBus registers here, but this may still
163 * interfere with ACPI :-( */
Jean Delvare54fb4a052008-07-14 22:38:33 +0200164 retval = acpi_check_region(sis5595_base + SMB_INDEX, 2,
165 sis5595_driver.name);
166 if (retval)
167 return retval;
168
Jean Delvared6072f82005-09-25 16:37:04 +0200169 if (!request_region(sis5595_base + SMB_INDEX, 2,
170 sis5595_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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);
173 return -ENODEV;
174 }
175
176 if (force_addr) {
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)
180 goto error;
181 if (pci_read_config_word(SIS5595_dev, ACPI_BASE, &a)
182 != PCIBIOS_SUCCESSFUL)
183 goto error;
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");
187 goto error;
188 }
189 }
190
191 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
192 != PCIBIOS_SUCCESSFUL)
193 goto error;
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)
198 goto error;
199 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
200 != PCIBIOS_SUCCESSFUL)
201 goto error;
202 if ((val & 0x80) == 0) {
203 /* doesn't work for some chips? */
204 dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
205 goto error;
206 }
207 }
208
209 /* Everything is happy */
210 return 0;
211
212error:
213 release_region(sis5595_base + SMB_INDEX, 2);
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static int sis5595_transaction(struct i2c_adapter *adap)
218{
219 int temp;
220 int result = 0;
221 int timeout = 0;
222
223 /* Make sure the SMBus host is ready to start transmitting */
224 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
225 if (temp != 0x00) {
Jean Delvare541e6a02005-06-23 22:18:08 +0200226 dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 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);
David Brownell97140342008-07-14 22:38:25 +0200231 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 } else {
Jean Delvarec5d21b72008-04-29 23:11:37 +0200233 dev_dbg(&adap->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 }
236
237 /* start the transaction by setting bit 4 */
238 sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
239
240 /* We will always wait for a fraction of a second! */
241 do {
242 msleep(1);
243 temp = sis5595_read(SMB_STS_LO);
244 } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
245
246 /* If the SMBus is still busy, we give up */
Roel Kluin4ccc28f2009-05-05 08:39:24 +0200247 if (timeout > MAX_TIMEOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev_dbg(&adap->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200249 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 if (temp & 0x10) {
253 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
David Brownell97140342008-07-14 22:38:25 +0200254 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
257 if (temp & 0x20) {
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 */
David Brownell97140342008-07-14 22:38:25 +0200261 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
264 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
265 if (temp != 0x00) {
266 sis5595_write(SMB_STS_LO, temp & 0xff);
267 sis5595_write(SMB_STS_HI, temp >> 8);
268 }
269
270 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
271 if (temp != 0x00)
272 dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
273
274 return result;
275}
276
David Brownell97140342008-07-14 22:38:25 +0200277/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static 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)
281{
David Brownell97140342008-07-14 22:38:25 +0200282 int status;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 switch (size) {
285 case I2C_SMBUS_QUICK:
286 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
287 size = SIS5595_QUICK;
288 break;
289 case I2C_SMBUS_BYTE:
290 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
291 if (read_write == I2C_SMBUS_WRITE)
292 sis5595_write(SMB_CMD, command);
293 size = SIS5595_BYTE;
294 break;
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;
301 break;
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);
310 }
311 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 default:
Jean Delvare1842cc22008-04-29 23:11:38 +0200314 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200315 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
319
David Brownell97140342008-07-14 22:38:25 +0200320 status = sis5595_transaction(adap);
321 if (status)
322 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if ((size != SIS5595_PROC_CALL) &&
325 ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
326 return 0;
327
328
329 switch (size) {
Jean Delvare1842cc22008-04-29 23:11:38 +0200330 case SIS5595_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 case SIS5595_BYTE_DATA:
332 data->byte = sis5595_read(SMB_BYTE);
333 break;
334 case SIS5595_WORD_DATA:
335 case SIS5595_PROC_CALL:
336 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
337 break;
338 }
339 return 0;
340}
341
342static u32 sis5595_func(struct i2c_adapter *adapter)
343{
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;
347}
348
Jean Delvare8f9082c2006-09-03 22:39:46 +0200349static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .smbus_xfer = sis5595_access,
351 .functionality = sis5595_func,
352};
353
354static struct i2c_adapter sis5595_adapter = {
355 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200356 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .algo = &smbus_algorithm,
358};
359
Jingoo Han392debf2013-12-03 08:11:20 +0900360static const struct pci_device_id sis5595_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
362 { 0, }
363};
364
365MODULE_DEVICE_TABLE (pci, sis5595_ids);
366
Bill Pemberton0b255e92012-11-27 15:59:38 -0500367static int sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jean Delvare7375cd82007-07-12 14:12:30 +0200369 int err;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (sis5595_setup(dev)) {
372 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
373 return -ENODEV;
374 }
375
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100376 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sis5595_adapter.dev.parent = &dev->dev;
378
Jean Delvare66c7acf2009-01-07 14:29:18 +0100379 snprintf(sis5595_adapter.name, sizeof(sis5595_adapter.name),
380 "SMBus SIS5595 adapter at %04x", sis5595_base + SMB_INDEX);
Jean Delvare7375cd82007-07-12 14:12:30 +0200381 err = i2c_add_adapter(&sis5595_adapter);
382 if (err) {
383 release_region(sis5595_base + SMB_INDEX, 2);
384 return err;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jean Delvare7375cd82007-07-12 14:12:30 +0200387 /* 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.
390 */
391 sis5595_pdev = pci_dev_get(dev);
392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static struct pci_driver sis5595_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .name = "sis5595_smbus",
397 .id_table = sis5595_ids,
398 .probe = sis5595_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
401static int __init i2c_sis5595_init(void)
402{
403 return pci_register_driver(&sis5595_driver);
404}
405
406static void __exit i2c_sis5595_exit(void)
407{
408 pci_unregister_driver(&sis5595_driver);
Jean Delvare7375cd82007-07-12 14:12:30 +0200409 if (sis5595_pdev) {
410 i2c_del_adapter(&sis5595_adapter);
411 release_region(sis5595_base + SMB_INDEX, 2);
412 pci_dev_put(sis5595_pdev);
413 sis5595_pdev = NULL;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
418MODULE_DESCRIPTION("SIS5595 SMBus driver");
419MODULE_LICENSE("GPL");
420
421module_init(i2c_sis5595_init);
422module_exit(i2c_sis5595_exit);