upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / i2c / busses / i2c-nforce2.c
1 /*
2     SMBus driver for nVidia nForce2 MCP
3
4     Added nForce3 Pro 150  Thomas Leibold <thomas@plx.com>,
5         Ported to 2.5 Patrick Dreker <patrick@dreker.de>,
6     Copyright (c) 2003  Hans-Frieder Vogt <hfvogt@arcor.de>,
7     Based on
8     SMBus 2.0 driver for AMD-8111 IO-Hub
9     Copyright (c) 2002 Vojtech Pavlik
10
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 /*
27     SUPPORTED DEVICES           PCI ID
28     nForce2 MCP                 0064
29     nForce2 Ultra 400 MCP       0084
30     nForce3 Pro150 MCP          00D4
31
32     This driver supports the 2 SMBuses that are included in the MCP2 of the
33     nForce2 chipset.
34 */
35
36 /* Note: we assume there can only be one nForce2, with two SMBus interfaces */
37
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/pci.h>
41 #include <linux/kernel.h>
42 #include <linux/stddef.h>
43 #include <linux/sched.h>
44 #include <linux/ioport.h>
45 #include <linux/init.h>
46 #include <linux/i2c.h>
47 #include <linux/delay.h>
48 #include <asm/io.h>
49
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@arcor.de>");
52 MODULE_DESCRIPTION("nForce2 SMBus driver");
53
54
55 struct nforce2_smbus {
56         struct pci_dev *dev;
57         struct i2c_adapter adapter;
58         int base;
59         int size;
60 };
61
62
63 /*
64  * nVidia nForce2 SMBus control register definitions
65  */
66 #define NFORCE_PCI_SMB1 0x50
67 #define NFORCE_PCI_SMB2 0x54
68
69
70 /*
71  * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
72  */
73 #define NVIDIA_SMB_PRTCL        (smbus->base + 0x00)    /* protocol, PEC */
74 #define NVIDIA_SMB_STS          (smbus->base + 0x01)    /* status */
75 #define NVIDIA_SMB_ADDR         (smbus->base + 0x02)    /* address */
76 #define NVIDIA_SMB_CMD          (smbus->base + 0x03)    /* command */
77 #define NVIDIA_SMB_DATA         (smbus->base + 0x04)    /* 32 data registers */
78 #define NVIDIA_SMB_BCNT         (smbus->base + 0x24)    /* number of data bytes */
79 #define NVIDIA_SMB_ALRM_A       (smbus->base + 0x25)    /* alarm address */
80 #define NVIDIA_SMB_ALRM_D       (smbus->base + 0x26)    /* 2 bytes alarm data */
81
82 #define NVIDIA_SMB_STS_DONE     0x80
83 #define NVIDIA_SMB_STS_ALRM     0x40
84 #define NVIDIA_SMB_STS_RES      0x20
85 #define NVIDIA_SMB_STS_STATUS   0x1f
86
87 #define NVIDIA_SMB_PRTCL_WRITE                  0x00
88 #define NVIDIA_SMB_PRTCL_READ                   0x01
89 #define NVIDIA_SMB_PRTCL_QUICK                  0x02
90 #define NVIDIA_SMB_PRTCL_BYTE                   0x04
91 #define NVIDIA_SMB_PRTCL_BYTE_DATA              0x06
92 #define NVIDIA_SMB_PRTCL_WORD_DATA              0x08
93 #define NVIDIA_SMB_PRTCL_BLOCK_DATA             0x0a
94 #define NVIDIA_SMB_PRTCL_PROC_CALL              0x0c
95 #define NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL        0x0d
96 #define NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA         0x4a
97 #define NVIDIA_SMB_PRTCL_PEC                    0x80
98
99
100 /* Other settings */
101 #define MAX_TIMEOUT 256
102
103
104
105 static s32 nforce2_access(struct i2c_adapter *adap, u16 addr,
106                        unsigned short flags, char read_write,
107                        u8 command, int size, union i2c_smbus_data *data);
108 static u32 nforce2_func(struct i2c_adapter *adapter);
109
110
111 static struct i2c_algorithm smbus_algorithm = {
112         .name = "Non-I2C SMBus adapter",
113         .id = I2C_ALGO_SMBUS,
114         .smbus_xfer = nforce2_access,
115         .functionality = nforce2_func,
116 };
117
118 static struct i2c_adapter nforce2_adapter = {
119         .owner          = THIS_MODULE,
120         .class          = I2C_CLASS_HWMON,
121         .algo           = &smbus_algorithm,
122         .name           = "unset",
123 };
124
125 /* Return -1 on error. See smbus.h for more information */
126 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
127                 unsigned short flags, char read_write,
128                 u8 command, int size, union i2c_smbus_data * data)
129 {
130         struct nforce2_smbus *smbus = adap->algo_data;
131         unsigned char protocol, pec, temp;
132         unsigned char len = 0; /* to keep the compiler quiet */
133         int timeout = 0;
134         int i;
135
136         protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
137                 NVIDIA_SMB_PRTCL_WRITE;
138         pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
139
140         switch (size) {
141
142                 case I2C_SMBUS_QUICK:
143                         protocol |= NVIDIA_SMB_PRTCL_QUICK;
144                         read_write = I2C_SMBUS_WRITE;
145                         break;
146
147                 case I2C_SMBUS_BYTE:
148                         if (read_write == I2C_SMBUS_WRITE)
149                                 outb_p(command, NVIDIA_SMB_CMD);
150                         protocol |= NVIDIA_SMB_PRTCL_BYTE;
151                         break;
152
153                 case I2C_SMBUS_BYTE_DATA:
154                         outb_p(command, NVIDIA_SMB_CMD);
155                         if (read_write == I2C_SMBUS_WRITE)
156                                 outb_p(data->byte, NVIDIA_SMB_DATA);
157                         protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
158                         break;
159
160                 case I2C_SMBUS_WORD_DATA:
161                         outb_p(command, NVIDIA_SMB_CMD);
162                         if (read_write == I2C_SMBUS_WRITE) {
163                                  outb_p(data->word, NVIDIA_SMB_DATA);
164                                  outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
165                         }
166                         protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
167                         break;
168
169                 case I2C_SMBUS_BLOCK_DATA:
170                         outb_p(command, NVIDIA_SMB_CMD);
171                         if (read_write == I2C_SMBUS_WRITE) {
172                                 len = min_t(u8, data->block[0], 32);
173                                 outb_p(len, NVIDIA_SMB_BCNT);
174                                 for (i = 0; i < len; i++)
175                                         outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
176                         }
177                         protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
178                         break;
179
180                 case I2C_SMBUS_I2C_BLOCK_DATA:
181                         len = min_t(u8, data->block[0], 32);
182                         outb_p(command, NVIDIA_SMB_CMD);
183                         outb_p(len, NVIDIA_SMB_BCNT);
184                         if (read_write == I2C_SMBUS_WRITE)
185                                 for (i = 0; i < len; i++)
186                                         outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
187                         protocol |= NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA;
188                         break;
189
190                 case I2C_SMBUS_PROC_CALL:
191                         dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
192                         return -1;
193                         /*
194                         outb_p(command, NVIDIA_SMB_CMD);
195                         outb_p(data->word, NVIDIA_SMB_DATA);
196                         outb_p(data->word >> 8, NVIDIA_SMB_DATA + 1);
197                         protocol = NVIDIA_SMB_PRTCL_PROC_CALL | pec;
198                         read_write = I2C_SMBUS_READ;
199                         break;
200                          */
201
202                 case I2C_SMBUS_BLOCK_PROC_CALL:
203                         dev_err(&adap->dev, "I2C_SMBUS_BLOCK_PROC_CALL not supported!\n");
204                         return -1;
205                         /*
206                         protocol |= pec;
207                         len = min_t(u8, data->block[0], 31);
208                         outb_p(command, NVIDIA_SMB_CMD);
209                         outb_p(len, NVIDIA_SMB_BCNT);
210                         for (i = 0; i < len; i++)
211                                 outb_p(data->block[i + 1], NVIDIA_SMB_DATA + i);
212                         protocol = NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL | pec;
213                         read_write = I2C_SMBUS_READ;
214                         break;
215                         */
216
217                 case I2C_SMBUS_WORD_DATA_PEC:
218                 case I2C_SMBUS_BLOCK_DATA_PEC:
219                 case I2C_SMBUS_PROC_CALL_PEC:
220                 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
221                         dev_err(&adap->dev, "Unexpected software PEC transaction %d\n.", size);
222                         return -1;
223
224                 default:
225                         dev_err(&adap->dev, "Unsupported transaction %d\n", size);
226                         return -1;
227         }
228
229         outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
230         outb_p(protocol, NVIDIA_SMB_PRTCL);
231
232         temp = inb_p(NVIDIA_SMB_STS);
233
234 #if 0
235         do {
236                 i2c_do_pause(1);
237                 temp = inb_p(NVIDIA_SMB_STS);
238         } while (((temp & NVIDIA_SMB_STS_DONE) == 0) && (timeout++ < MAX_TIMEOUT));
239 #endif
240         if (~temp & NVIDIA_SMB_STS_DONE) {
241                 udelay(500);
242                 temp = inb_p(NVIDIA_SMB_STS);
243         }
244         if (~temp & NVIDIA_SMB_STS_DONE) {
245                 msleep(10);
246                 temp = inb_p(NVIDIA_SMB_STS);
247         }
248
249         if ((timeout >= MAX_TIMEOUT) || (~temp & NVIDIA_SMB_STS_DONE)
250                 || (temp & NVIDIA_SMB_STS_STATUS))
251                 return -1;
252
253         if (read_write == I2C_SMBUS_WRITE)
254                 return 0;
255
256         switch (size) {
257
258                 case I2C_SMBUS_BYTE:
259                 case I2C_SMBUS_BYTE_DATA:
260                         data->byte = inb_p(NVIDIA_SMB_DATA);
261                         break;
262
263                 case I2C_SMBUS_WORD_DATA:
264                 /* case I2C_SMBUS_PROC_CALL: not supported */
265                         data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
266                         break;
267
268                 case I2C_SMBUS_BLOCK_DATA:
269                 /* case I2C_SMBUS_BLOCK_PROC_CALL: not supported */
270                         len = inb_p(NVIDIA_SMB_BCNT);
271                         len = min_t(u8, len, 32);
272                 case I2C_SMBUS_I2C_BLOCK_DATA:
273                         for (i = 0; i < len; i++)
274                                 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
275                         data->block[0] = len;
276                         break;
277         }
278
279         return 0;
280 }
281
282
283 static u32 nforce2_func(struct i2c_adapter *adapter)
284 {
285         /* other functionality might be possible, but is not tested */
286         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
287             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA /* |
288             I2C_FUNC_SMBUS_BLOCK_DATA */;
289 }
290
291
292 static struct pci_device_id nforce2_ids[] = {
293         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS,
294                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
295         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS,
296                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
297         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS,
298                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
299         { 0 }
300 };
301
302
303 MODULE_DEVICE_TABLE (pci, nforce2_ids);
304
305
306 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int reg,
307         struct nforce2_smbus *smbus, char *name)
308 {
309         u16 iobase;
310         int error;
311
312         if (pci_read_config_word(dev, reg, &iobase) != PCIBIOS_SUCCESSFUL) {
313                 dev_err(&smbus->adapter.dev, "Error reading PCI config for %s\n", name);
314                 return -1;
315         }
316         smbus->dev  = dev;
317         smbus->base = iobase & 0xfffc;
318         smbus->size = 8;
319
320         if (!request_region(smbus->base, smbus->size, "nForce2 SMBus")) {
321                 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
322                         smbus->base, smbus->base+smbus->size-1, name);
323                 return -1;
324         }
325         smbus->adapter = nforce2_adapter;
326         smbus->adapter.algo_data = smbus;
327         smbus->adapter.dev.parent = &dev->dev;
328         snprintf(smbus->adapter.name, I2C_NAME_SIZE,
329                 "SMBus nForce2 adapter at %04x", smbus->base);
330
331         error = i2c_add_adapter(&smbus->adapter);
332         if (error) {
333                 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
334                 release_region(smbus->base, smbus->size);
335                 return -1;
336         }
337         dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
338         return 0;
339 }
340
341
342 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
343 {
344         struct nforce2_smbus *smbuses;
345         int res1, res2;
346
347         /* we support 2 SMBus adapters */
348         if (!(smbuses = (void *)kmalloc(2*sizeof(struct nforce2_smbus),
349                                         GFP_KERNEL)))
350                 return -ENOMEM;
351         memset (smbuses, 0, 2*sizeof(struct nforce2_smbus));
352         pci_set_drvdata(dev, smbuses);
353
354         /* SMBus adapter 1 */
355         res1 = nforce2_probe_smb (dev, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
356         if (res1 < 0) {
357                 dev_err(&dev->dev, "Error probing SMB1.\n");
358                 smbuses[0].base = 0;    /* to have a check value */
359         }
360         res2 = nforce2_probe_smb (dev, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
361         if (res2 < 0) {
362                 dev_err(&dev->dev, "Error probing SMB2.\n");
363                 smbuses[1].base = 0;    /* to have a check value */
364         }
365         if ((res1 < 0) && (res2 < 0)) {
366                 /* we did not find even one of the SMBuses, so we give up */
367                 kfree(smbuses);
368                 return -ENODEV;
369         }
370
371         return 0;
372 }
373
374
375 static void __devexit nforce2_remove(struct pci_dev *dev)
376 {
377         struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
378
379         if (smbuses[0].base) {
380                 i2c_del_adapter(&smbuses[0].adapter);
381                 release_region(smbuses[0].base, smbuses[0].size);
382         }
383         if (smbuses[1].base) {
384                 i2c_del_adapter(&smbuses[1].adapter);
385                 release_region(smbuses[1].base, smbuses[1].size);
386         }
387         kfree(smbuses);
388 }
389
390 static struct pci_driver nforce2_driver = {
391         .name           = "nForce2_smbus",
392         .id_table       = nforce2_ids,
393         .probe          = nforce2_probe,
394         .remove         = __devexit_p(nforce2_remove),
395 };
396
397 static int __init nforce2_init(void)
398 {
399         return pci_register_driver(&nforce2_driver);
400 }
401
402 static void __exit nforce2_exit(void)
403 {
404         pci_unregister_driver(&nforce2_driver);
405 }
406
407 module_init(nforce2_init);
408 module_exit(nforce2_exit);
409