ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / i2c / busses / i2c-sis5595.c
1 /*
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
58 #include <linux/config.h>
59 #include <linux/kernel.h>
60 #include <linux/module.h>
61 #include <linux/pci.h>
62 #include <linux/ioport.h>
63 #include <linux/init.h>
64 #include <linux/i2c.h>
65 #include <asm/io.h>
66
67 static int blacklist[] = {
68         PCI_DEVICE_ID_SI_540,
69         PCI_DEVICE_ID_SI_550,
70         PCI_DEVICE_ID_SI_630,
71         PCI_DEVICE_ID_SI_645,
72         PCI_DEVICE_ID_SI_646,
73         PCI_DEVICE_ID_SI_648,
74         PCI_DEVICE_ID_SI_650,
75         PCI_DEVICE_ID_SI_651,
76         PCI_DEVICE_ID_SI_730,
77         PCI_DEVICE_ID_SI_735,
78         PCI_DEVICE_ID_SI_745,
79         PCI_DEVICE_ID_SI_746,
80         PCI_DEVICE_ID_SI_5511,  /* 5513 chip has the 0008 device but that ID
81                                    shows up in other chips so we use the 5511
82                                    ID for recognition */
83         PCI_DEVICE_ID_SI_5597,
84         PCI_DEVICE_ID_SI_5598,
85         0,                      /* terminates the list */
86 };
87
88 /* Length of ISA address segment */
89 #define SIS5595_EXTENT          8
90 /* SIS5595 SMBus registers */
91 #define SMB_STS_LO              0x00
92 #define SMB_STS_HI              0x01
93 #define SMB_CTL_LO              0x02
94 #define SMB_CTL_HI              0x03
95 #define SMB_ADDR                0x04
96 #define SMB_CMD                 0x05
97 #define SMB_PCNT                0x06
98 #define SMB_CNT                 0x07
99 #define SMB_BYTE                0x08
100 #define SMB_DEV                 0x10
101 #define SMB_DB0                 0x11
102 #define SMB_DB1                 0x12
103 #define SMB_HAA                 0x13
104
105 /* PCI Address Constants */
106 #define SMB_INDEX               0x38
107 #define SMB_DAT                 0x39
108 #define SIS5595_ENABLE_REG      0x40
109 #define ACPI_BASE               0x90
110
111 /* Other settings */
112 #define MAX_TIMEOUT             500
113
114 /* SIS5595 constants */
115 #define SIS5595_QUICK           0x00
116 #define SIS5595_BYTE            0x02
117 #define SIS5595_BYTE_DATA       0x04
118 #define SIS5595_WORD_DATA       0x06
119 #define SIS5595_PROC_CALL       0x08
120 #define SIS5595_BLOCK_DATA      0x0A
121
122 /* insmod parameters */
123
124 /* If force_addr is set to anything different from 0, we forcibly enable
125    the device at the given address. */
126 static int force_addr = 0;
127 MODULE_PARM(force_addr, "i");
128 MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
129
130 static unsigned short sis5595_base = 0;
131
132 static u8 sis5595_read(u8 reg)
133 {
134         outb(reg, sis5595_base + SMB_INDEX);
135         return inb(sis5595_base + SMB_DAT);
136 }
137
138 static void sis5595_write(u8 reg, u8 data)
139 {
140         outb(reg, sis5595_base + SMB_INDEX);
141         outb(data, sis5595_base + SMB_DAT);
142 }
143
144 static int sis5595_setup(struct pci_dev *SIS5595_dev)
145 {
146         u16 a;
147         u8 val;
148         int *i;
149         int retval = -ENODEV;
150
151         /* Look for imposters */
152         for (i = blacklist; *i != 0; i++) {
153                 struct pci_dev *dev;
154                 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
155                 if (dev) {
156                         dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
157                         pci_dev_put(dev);
158                         return -ENODEV;
159                 }
160         }
161
162         /* Determine the address of the SMBus areas */
163         pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
164         if (sis5595_base == 0 && force_addr == 0) {
165                 dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
166                 return -ENODEV;
167         }
168
169         if (force_addr)
170                 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
171         dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
172
173         /* NB: We grab just the two SMBus registers here, but this may still
174          * interfere with ACPI :-(  */
175         if (!request_region(sis5595_base + SMB_INDEX, 2, "sis5595-smbus")) {
176                 dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
177                         sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
178                 return -ENODEV;
179         }
180
181         if (force_addr) {
182                 dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
183                 if (!pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base))
184                         goto error;
185                 if (!pci_read_config_word(SIS5595_dev, ACPI_BASE, &a))
186                         goto error;
187                 if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
188                         /* doesn't work for some chips! */
189                         dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
190                         goto error;
191                 }
192         }
193
194         if (!pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val))
195                 goto error;
196         if ((val & 0x80) == 0) {
197                 dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
198                 if (!pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80))
199                         goto error;
200                 if (!pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val))
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
212 error:
213         release_region(sis5595_base + SMB_INDEX, 2);
214         return retval;
215 }
216
217 static 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) {
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);
231                         return -1;
232                 } else {
233                         dev_dbg(&adap->dev, "Successfull!\n");
234                 }
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                 i2c_delay(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 */
247         if (timeout >= MAX_TIMEOUT) {
248                 dev_dbg(&adap->dev, "SMBus Timeout!\n");
249                 result = -1;
250         }
251
252         if (temp & 0x10) {
253                 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
254                 result = -1;
255         }
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 */
261                 result = -1;
262         }
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
277 /* Return -1 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)
281 {
282         switch (size) {
283         case I2C_SMBUS_QUICK:
284                 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
285                 size = SIS5595_QUICK;
286                 break;
287         case I2C_SMBUS_BYTE:
288                 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
289                 if (read_write == I2C_SMBUS_WRITE)
290                         sis5595_write(SMB_CMD, command);
291                 size = SIS5595_BYTE;
292                 break;
293         case I2C_SMBUS_BYTE_DATA:
294                 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
295                 sis5595_write(SMB_CMD, command);
296                 if (read_write == I2C_SMBUS_WRITE)
297                         sis5595_write(SMB_BYTE, data->byte);
298                 size = SIS5595_BYTE_DATA;
299                 break;
300         case I2C_SMBUS_PROC_CALL:
301         case I2C_SMBUS_WORD_DATA:
302                 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
303                 sis5595_write(SMB_CMD, command);
304                 if (read_write == I2C_SMBUS_WRITE) {
305                         sis5595_write(SMB_BYTE, data->word & 0xff);
306                         sis5595_write(SMB_BYTE + 1,
307                                       (data->word & 0xff00) >> 8);
308                 }
309                 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
310                 break;
311 /*
312         case I2C_SMBUS_BLOCK_DATA:
313                 printk(KERN_WARNING "sis5595.o: Block data not yet implemented!\n");
314                 return -1;
315                 break;
316 */
317         default:
318                 printk(KERN_WARNING "sis5595.o: Unsupported transaction %d\n", size);
319                 return -1;
320         }
321
322         sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
323
324         if (sis5595_transaction(adap))
325                 return -1;
326
327         if ((size != SIS5595_PROC_CALL) &&
328             ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
329                 return 0;
330
331
332         switch (size) {
333         case SIS5595_BYTE:      /* Where is the result put? I assume here it is in
334                                    SMB_DATA but it might just as well be in the
335                                    SMB_CMD. No clue in the docs */
336         case SIS5595_BYTE_DATA:
337                 data->byte = sis5595_read(SMB_BYTE);
338                 break;
339         case SIS5595_WORD_DATA:
340         case SIS5595_PROC_CALL:
341                 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
342                 break;
343         }
344         return 0;
345 }
346
347 static u32 sis5595_func(struct i2c_adapter *adapter)
348 {
349         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
350             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
351             I2C_FUNC_SMBUS_PROC_CALL;
352 }
353
354 static struct i2c_algorithm smbus_algorithm = {
355         .name           = "Non-I2C SMBus adapter",
356         .id             = I2C_ALGO_SMBUS,
357         .smbus_xfer     = sis5595_access,
358         .functionality  = sis5595_func,
359 };
360
361 static struct i2c_adapter sis5595_adapter = {
362         .owner          = THIS_MODULE,
363         .class          = I2C_ADAP_CLASS_SMBUS,
364         .name           = "unset",
365         .algo           = &smbus_algorithm,
366 };
367
368 static struct pci_device_id sis5595_ids[] __devinitdata = {
369         { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) }, 
370         { 0, }
371 };
372
373 static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
374 {
375         if (sis5595_setup(dev)) {
376                 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
377                 return -ENODEV;
378         }
379
380         /* set up the driverfs linkage to our parent device */
381         sis5595_adapter.dev.parent = &dev->dev;
382
383         sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
384                 sis5595_base + SMB_INDEX);
385         return i2c_add_adapter(&sis5595_adapter);
386 }
387
388 static void __devexit sis5595_remove(struct pci_dev *dev)
389 {
390         i2c_del_adapter(&sis5595_adapter);
391         release_region(sis5595_base + SMB_INDEX, 2);
392 }
393
394 static struct pci_driver sis5595_driver = {
395         .name           = "sis5595 smbus",
396         .id_table       = sis5595_ids,
397         .probe          = sis5595_probe,
398         .remove         = __devexit_p(sis5595_remove),
399 };
400
401 static int __init i2c_sis5595_init(void)
402 {
403         return pci_module_init(&sis5595_driver);
404 }
405
406 static void __exit i2c_sis5595_exit(void)
407 {
408         pci_unregister_driver(&sis5595_driver);
409 }
410
411 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
412 MODULE_DESCRIPTION("SIS5595 SMBus driver");
413 MODULE_LICENSE("GPL");
414
415 module_init(i2c_sis5595_init);
416 module_exit(i2c_sis5595_exit);