blob: 3c8e0e1bd24fd566427a9715bc7abf3baef4db02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 sis5595.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6
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.
11
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.
16
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.
20*/
21
22/* Note: we assume there can only be one SIS5595 with one SMBus interface */
23
24/*
25 Note: all have mfr. ID 0x1039.
26 SUPPORTED PCI ID
27 5595 0008
28
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.
32
33 NOT SUPPORTED PCI ID BLACKLIST PCI ID
34 540 0008 0540
35 550 0008 0550
36 5513 0008 5511
37 5581 0008 5597
38 5582 0008 5597
39 5597 0008 5597
40 5598 0008 5597/5598
41 630 0008 0630
42 645 0008 0645
43 646 0008 0646
44 648 0008 0648
45 650 0008 0650
46 651 0008 0651
47 730 0008 0730
48 735 0008 0735
49 745 0008 0745
50 746 0008 0746
51*/
52
53/* TO DO:
54 * Add Block Transfers (ugly, but supported by the adapter)
55 * Add adapter resets
56 */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/kernel.h>
59#include <linux/module.h>
60#include <linux/delay.h>
61#include <linux/pci.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020065#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/io.h>
67
68static int blacklist[] = {
69 PCI_DEVICE_ID_SI_540,
70 PCI_DEVICE_ID_SI_550,
71 PCI_DEVICE_ID_SI_630,
72 PCI_DEVICE_ID_SI_645,
73 PCI_DEVICE_ID_SI_646,
74 PCI_DEVICE_ID_SI_648,
75 PCI_DEVICE_ID_SI_650,
76 PCI_DEVICE_ID_SI_651,
77 PCI_DEVICE_ID_SI_730,
78 PCI_DEVICE_ID_SI_735,
79 PCI_DEVICE_ID_SI_745,
80 PCI_DEVICE_ID_SI_746,
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
83 ID for recognition */
84 PCI_DEVICE_ID_SI_5597,
85 PCI_DEVICE_ID_SI_5598,
86 0, /* terminates the list */
87};
88
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
96#define SMB_ADDR 0x04
97#define SMB_CMD 0x05
98#define SMB_PCNT 0x06
99#define SMB_CNT 0x07
100#define SMB_BYTE 0x08
101#define SMB_DEV 0x10
102#define SMB_DB0 0x11
103#define SMB_DB1 0x12
104#define SMB_HAA 0x13
105
106/* PCI Address Constants */
107#define SMB_INDEX 0x38
108#define SMB_DAT 0x39
109#define SIS5595_ENABLE_REG 0x40
110#define ACPI_BASE 0x90
111
112/* Other settings */
113#define MAX_TIMEOUT 500
114
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
122
123/* insmod parameters */
124
125/* If force_addr is set to anything different from 0, we forcibly enable
126 the device at the given address. */
Jean Delvare60507092005-09-25 16:23:07 +0200127static u16 force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(force_addr, ushort, 0);
129MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
130
Jean Delvared6072f82005-09-25 16:37:04 +0200131static struct pci_driver sis5595_driver;
Jean Delvare60507092005-09-25 16:23:07 +0200132static unsigned short sis5595_base;
Jean Delvare7375cd82007-07-12 14:12:30 +0200133static struct pci_dev *sis5595_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static u8 sis5595_read(u8 reg)
136{
137 outb(reg, sis5595_base + SMB_INDEX);
138 return inb(sis5595_base + SMB_DAT);
139}
140
141static void sis5595_write(u8 reg, u8 data)
142{
143 outb(reg, sis5595_base + SMB_INDEX);
144 outb(data, sis5595_base + SMB_DAT);
145}
146
147static int sis5595_setup(struct pci_dev *SIS5595_dev)
148{
149 u16 a;
150 u8 val;
151 int *i;
152 int retval = -ENODEV;
153
154 /* Look for imposters */
155 for (i = blacklist; *i != 0; i++) {
156 struct pci_dev *dev;
157 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
158 if (dev) {
159 dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
160 pci_dev_put(dev);
161 return -ENODEV;
162 }
163 }
164
165 /* Determine the address of the SMBus areas */
166 pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
167 if (sis5595_base == 0 && force_addr == 0) {
168 dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
169 return -ENODEV;
170 }
171
172 if (force_addr)
173 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
174 dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
175
176 /* NB: We grab just the two SMBus registers here, but this may still
177 * interfere with ACPI :-( */
Jean Delvare54fb4a052008-07-14 22:38:33 +0200178 retval = acpi_check_region(sis5595_base + SMB_INDEX, 2,
179 sis5595_driver.name);
180 if (retval)
181 return retval;
182
Jean Delvared6072f82005-09-25 16:37:04 +0200183 if (!request_region(sis5595_base + SMB_INDEX, 2,
184 sis5595_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
186 sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
187 return -ENODEV;
188 }
189
190 if (force_addr) {
191 dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
192 if (pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base)
193 != PCIBIOS_SUCCESSFUL)
194 goto error;
195 if (pci_read_config_word(SIS5595_dev, ACPI_BASE, &a)
196 != PCIBIOS_SUCCESSFUL)
197 goto error;
198 if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
199 /* doesn't work for some chips! */
200 dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
201 goto error;
202 }
203 }
204
205 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
206 != PCIBIOS_SUCCESSFUL)
207 goto error;
208 if ((val & 0x80) == 0) {
209 dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
210 if (pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80)
211 != PCIBIOS_SUCCESSFUL)
212 goto error;
213 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
214 != PCIBIOS_SUCCESSFUL)
215 goto error;
216 if ((val & 0x80) == 0) {
217 /* doesn't work for some chips? */
218 dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
219 goto error;
220 }
221 }
222
223 /* Everything is happy */
224 return 0;
225
226error:
227 release_region(sis5595_base + SMB_INDEX, 2);
228 return retval;
229}
230
231static int sis5595_transaction(struct i2c_adapter *adap)
232{
233 int temp;
234 int result = 0;
235 int timeout = 0;
236
237 /* Make sure the SMBus host is ready to start transmitting */
238 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
239 if (temp != 0x00) {
Jean Delvare541e6a02005-06-23 22:18:08 +0200240 dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 sis5595_write(SMB_STS_LO, temp & 0xff);
242 sis5595_write(SMB_STS_HI, temp >> 8);
243 if ((temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8)) != 0x00) {
244 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200245 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 } else {
Jean Delvarec5d21b72008-04-29 23:11:37 +0200247 dev_dbg(&adap->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250
251 /* start the transaction by setting bit 4 */
252 sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
253
254 /* We will always wait for a fraction of a second! */
255 do {
256 msleep(1);
257 temp = sis5595_read(SMB_STS_LO);
258 } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
259
260 /* If the SMBus is still busy, we give up */
261 if (timeout >= MAX_TIMEOUT) {
262 dev_dbg(&adap->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200263 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 if (temp & 0x10) {
267 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
David Brownell97140342008-07-14 22:38:25 +0200268 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 if (temp & 0x20) {
272 dev_err(&adap->dev, "Bus collision! SMBus may be locked until "
273 "next hard reset (or not...)\n");
274 /* Clock stops and slave is stuck in mid-transmission */
David Brownell97140342008-07-14 22:38:25 +0200275 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
279 if (temp != 0x00) {
280 sis5595_write(SMB_STS_LO, temp & 0xff);
281 sis5595_write(SMB_STS_HI, temp >> 8);
282 }
283
284 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
285 if (temp != 0x00)
286 dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
287
288 return result;
289}
290
David Brownell97140342008-07-14 22:38:25 +0200291/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static s32 sis5595_access(struct i2c_adapter *adap, u16 addr,
293 unsigned short flags, char read_write,
294 u8 command, int size, union i2c_smbus_data *data)
295{
David Brownell97140342008-07-14 22:38:25 +0200296 int status;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 switch (size) {
299 case I2C_SMBUS_QUICK:
300 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
301 size = SIS5595_QUICK;
302 break;
303 case I2C_SMBUS_BYTE:
304 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
305 if (read_write == I2C_SMBUS_WRITE)
306 sis5595_write(SMB_CMD, command);
307 size = SIS5595_BYTE;
308 break;
309 case I2C_SMBUS_BYTE_DATA:
310 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
311 sis5595_write(SMB_CMD, command);
312 if (read_write == I2C_SMBUS_WRITE)
313 sis5595_write(SMB_BYTE, data->byte);
314 size = SIS5595_BYTE_DATA;
315 break;
316 case I2C_SMBUS_PROC_CALL:
317 case I2C_SMBUS_WORD_DATA:
318 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
319 sis5595_write(SMB_CMD, command);
320 if (read_write == I2C_SMBUS_WRITE) {
321 sis5595_write(SMB_BYTE, data->word & 0xff);
322 sis5595_write(SMB_BYTE + 1,
323 (data->word & 0xff00) >> 8);
324 }
325 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 default:
Jean Delvare1842cc22008-04-29 23:11:38 +0200328 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200329 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
333
David Brownell97140342008-07-14 22:38:25 +0200334 status = sis5595_transaction(adap);
335 if (status)
336 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if ((size != SIS5595_PROC_CALL) &&
339 ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
340 return 0;
341
342
343 switch (size) {
Jean Delvare1842cc22008-04-29 23:11:38 +0200344 case SIS5595_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 case SIS5595_BYTE_DATA:
346 data->byte = sis5595_read(SMB_BYTE);
347 break;
348 case SIS5595_WORD_DATA:
349 case SIS5595_PROC_CALL:
350 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
351 break;
352 }
353 return 0;
354}
355
356static u32 sis5595_func(struct i2c_adapter *adapter)
357{
358 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
359 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
360 I2C_FUNC_SMBUS_PROC_CALL;
361}
362
Jean Delvare8f9082c2006-09-03 22:39:46 +0200363static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .smbus_xfer = sis5595_access,
365 .functionality = sis5595_func,
366};
367
368static struct i2c_adapter sis5595_adapter = {
369 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100370 .id = I2C_HW_SMBUS_SIS5595,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200371 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .algo = &smbus_algorithm,
373};
374
375static struct pci_device_id sis5595_ids[] __devinitdata = {
376 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
377 { 0, }
378};
379
380MODULE_DEVICE_TABLE (pci, sis5595_ids);
381
382static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
383{
Jean Delvare7375cd82007-07-12 14:12:30 +0200384 int err;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (sis5595_setup(dev)) {
387 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
388 return -ENODEV;
389 }
390
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100391 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sis5595_adapter.dev.parent = &dev->dev;
393
394 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
395 sis5595_base + SMB_INDEX);
Jean Delvare7375cd82007-07-12 14:12:30 +0200396 err = i2c_add_adapter(&sis5595_adapter);
397 if (err) {
398 release_region(sis5595_base + SMB_INDEX, 2);
399 return err;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jean Delvare7375cd82007-07-12 14:12:30 +0200402 /* Always return failure here. This is to allow other drivers to bind
403 * to this pci device. We don't really want to have control over the
404 * pci device, we only wanted to read as few register values from it.
405 */
406 sis5595_pdev = pci_dev_get(dev);
407 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static struct pci_driver sis5595_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .name = "sis5595_smbus",
412 .id_table = sis5595_ids,
413 .probe = sis5595_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414};
415
416static int __init i2c_sis5595_init(void)
417{
418 return pci_register_driver(&sis5595_driver);
419}
420
421static void __exit i2c_sis5595_exit(void)
422{
423 pci_unregister_driver(&sis5595_driver);
Jean Delvare7375cd82007-07-12 14:12:30 +0200424 if (sis5595_pdev) {
425 i2c_del_adapter(&sis5595_adapter);
426 release_region(sis5595_base + SMB_INDEX, 2);
427 pci_dev_put(sis5595_pdev);
428 sis5595_pdev = NULL;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
433MODULE_DESCRIPTION("SIS5595 SMBus driver");
434MODULE_LICENSE("GPL");
435
436module_init(i2c_sis5595_init);
437module_exit(i2c_sis5595_exit);