[PATCH] I2C: Kill i2c_algorithm.id (4/7)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / busses / i2c-nforce2.c
blobe0b7a913431e0c1c7664f474a904e8db44a57c92
1 /*
2 SMBus driver for nVidia nForce2 MCP
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
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.
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.
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.
27 SUPPORTED DEVICES PCI ID
28 nForce2 MCP 0064
29 nForce2 Ultra 400 MCP 0084
30 nForce3 Pro150 MCP 00D4
31 nForce3 250Gb MCP 00E4
32 nForce4 MCP 0052
34 This driver supports the 2 SMBuses that are included in the MCP of the
35 nForce2/3/4 chipsets.
38 /* Note: we assume there can only be one nForce2, with two SMBus interfaces */
40 #include <linux/module.h>
41 #include <linux/pci.h>
42 #include <linux/kernel.h>
43 #include <linux/stddef.h>
44 #include <linux/sched.h>
45 #include <linux/ioport.h>
46 #include <linux/init.h>
47 #include <linux/i2c.h>
48 #include <linux/delay.h>
49 #include <asm/io.h>
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@arcor.de>");
53 MODULE_DESCRIPTION("nForce2 SMBus driver");
56 struct nforce2_smbus {
57 struct pci_dev *dev;
58 struct i2c_adapter adapter;
59 int base;
60 int size;
65 * nVidia nForce2 SMBus control register definitions
67 #define NFORCE_PCI_SMB1 0x50
68 #define NFORCE_PCI_SMB2 0x54
72 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
74 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
75 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
76 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
77 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
78 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
79 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data bytes */
80 #define NVIDIA_SMB_ALRM_A (smbus->base + 0x25) /* alarm address */
81 #define NVIDIA_SMB_ALRM_D (smbus->base + 0x26) /* 2 bytes alarm data */
83 #define NVIDIA_SMB_STS_DONE 0x80
84 #define NVIDIA_SMB_STS_ALRM 0x40
85 #define NVIDIA_SMB_STS_RES 0x20
86 #define NVIDIA_SMB_STS_STATUS 0x1f
88 #define NVIDIA_SMB_PRTCL_WRITE 0x00
89 #define NVIDIA_SMB_PRTCL_READ 0x01
90 #define NVIDIA_SMB_PRTCL_QUICK 0x02
91 #define NVIDIA_SMB_PRTCL_BYTE 0x04
92 #define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
93 #define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
94 #define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
95 #define NVIDIA_SMB_PRTCL_PROC_CALL 0x0c
96 #define NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
97 #define NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
98 #define NVIDIA_SMB_PRTCL_PEC 0x80
101 /* Other settings */
102 #define MAX_TIMEOUT 256
106 static s32 nforce2_access(struct i2c_adapter *adap, u16 addr,
107 unsigned short flags, char read_write,
108 u8 command, int size, union i2c_smbus_data *data);
109 static u32 nforce2_func(struct i2c_adapter *adapter);
112 static struct i2c_algorithm smbus_algorithm = {
113 .smbus_xfer = nforce2_access,
114 .functionality = nforce2_func,
117 static struct i2c_adapter nforce2_adapter = {
118 .owner = THIS_MODULE,
119 .class = I2C_CLASS_HWMON,
120 .algo = &smbus_algorithm,
121 .name = "unset",
124 /* Return -1 on error. See smbus.h for more information */
125 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
126 unsigned short flags, char read_write,
127 u8 command, int size, union i2c_smbus_data * data)
129 struct nforce2_smbus *smbus = adap->algo_data;
130 unsigned char protocol, pec, temp;
131 unsigned char len = 0; /* to keep the compiler quiet */
132 int i;
134 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
135 NVIDIA_SMB_PRTCL_WRITE;
136 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
138 switch (size) {
140 case I2C_SMBUS_QUICK:
141 protocol |= NVIDIA_SMB_PRTCL_QUICK;
142 read_write = I2C_SMBUS_WRITE;
143 break;
145 case I2C_SMBUS_BYTE:
146 if (read_write == I2C_SMBUS_WRITE)
147 outb_p(command, NVIDIA_SMB_CMD);
148 protocol |= NVIDIA_SMB_PRTCL_BYTE;
149 break;
151 case I2C_SMBUS_BYTE_DATA:
152 outb_p(command, NVIDIA_SMB_CMD);
153 if (read_write == I2C_SMBUS_WRITE)
154 outb_p(data->byte, NVIDIA_SMB_DATA);
155 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
156 break;
158 case I2C_SMBUS_WORD_DATA:
159 outb_p(command, NVIDIA_SMB_CMD);
160 if (read_write == I2C_SMBUS_WRITE) {
161 outb_p(data->word, NVIDIA_SMB_DATA);
162 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
164 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
165 break;
167 case I2C_SMBUS_BLOCK_DATA:
168 outb_p(command, NVIDIA_SMB_CMD);
169 if (read_write == I2C_SMBUS_WRITE) {
170 len = min_t(u8, data->block[0], 32);
171 outb_p(len, NVIDIA_SMB_BCNT);
172 for (i = 0; i < len; i++)
173 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
175 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
176 break;
178 case I2C_SMBUS_I2C_BLOCK_DATA:
179 len = min_t(u8, data->block[0], 32);
180 outb_p(command, NVIDIA_SMB_CMD);
181 outb_p(len, NVIDIA_SMB_BCNT);
182 if (read_write == I2C_SMBUS_WRITE)
183 for (i = 0; i < len; i++)
184 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
185 protocol |= NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA;
186 break;
188 case I2C_SMBUS_PROC_CALL:
189 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
190 return -1;
192 case I2C_SMBUS_BLOCK_PROC_CALL:
193 dev_err(&adap->dev, "I2C_SMBUS_BLOCK_PROC_CALL not supported!\n");
194 return -1;
196 case I2C_SMBUS_WORD_DATA_PEC:
197 case I2C_SMBUS_BLOCK_DATA_PEC:
198 case I2C_SMBUS_PROC_CALL_PEC:
199 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
200 dev_err(&adap->dev, "Unexpected software PEC transaction %d\n.", size);
201 return -1;
203 default:
204 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
205 return -1;
208 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
209 outb_p(protocol, NVIDIA_SMB_PRTCL);
211 temp = inb_p(NVIDIA_SMB_STS);
213 if (~temp & NVIDIA_SMB_STS_DONE) {
214 udelay(500);
215 temp = inb_p(NVIDIA_SMB_STS);
217 if (~temp & NVIDIA_SMB_STS_DONE) {
218 msleep(10);
219 temp = inb_p(NVIDIA_SMB_STS);
222 if ((~temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
223 dev_dbg(&adap->dev, "SMBus Timeout! (0x%02x)\n", temp);
224 return -1;
227 if (read_write == I2C_SMBUS_WRITE)
228 return 0;
230 switch (size) {
232 case I2C_SMBUS_BYTE:
233 case I2C_SMBUS_BYTE_DATA:
234 data->byte = inb_p(NVIDIA_SMB_DATA);
235 break;
237 case I2C_SMBUS_WORD_DATA:
238 /* case I2C_SMBUS_PROC_CALL: not supported */
239 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
240 break;
242 case I2C_SMBUS_BLOCK_DATA:
243 /* case I2C_SMBUS_BLOCK_PROC_CALL: not supported */
244 len = inb_p(NVIDIA_SMB_BCNT);
245 len = min_t(u8, len, 32);
246 case I2C_SMBUS_I2C_BLOCK_DATA:
247 for (i = 0; i < len; i++)
248 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
249 data->block[0] = len;
250 break;
253 return 0;
257 static u32 nforce2_func(struct i2c_adapter *adapter)
259 /* other functionality might be possible, but is not tested */
260 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
261 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA /* |
262 I2C_FUNC_SMBUS_BLOCK_DATA */;
266 static struct pci_device_id nforce2_ids[] = {
267 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
268 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
269 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
270 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
271 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
272 { 0 }
276 MODULE_DEVICE_TABLE (pci, nforce2_ids);
279 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int reg,
280 struct nforce2_smbus *smbus, char *name)
282 u16 iobase;
283 int error;
285 if (pci_read_config_word(dev, reg, &iobase) != PCIBIOS_SUCCESSFUL) {
286 dev_err(&smbus->adapter.dev, "Error reading PCI config for %s\n", name);
287 return -1;
289 smbus->dev = dev;
290 smbus->base = iobase & 0xfffc;
291 smbus->size = 8;
293 if (!request_region(smbus->base, smbus->size, "nForce2 SMBus")) {
294 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
295 smbus->base, smbus->base+smbus->size-1, name);
296 return -1;
298 smbus->adapter = nforce2_adapter;
299 smbus->adapter.algo_data = smbus;
300 smbus->adapter.dev.parent = &dev->dev;
301 snprintf(smbus->adapter.name, I2C_NAME_SIZE,
302 "SMBus nForce2 adapter at %04x", smbus->base);
304 error = i2c_add_adapter(&smbus->adapter);
305 if (error) {
306 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
307 release_region(smbus->base, smbus->size);
308 return -1;
310 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
311 return 0;
315 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
317 struct nforce2_smbus *smbuses;
318 int res1, res2;
320 /* we support 2 SMBus adapters */
321 if (!(smbuses = (void *)kmalloc(2*sizeof(struct nforce2_smbus),
322 GFP_KERNEL)))
323 return -ENOMEM;
324 memset (smbuses, 0, 2*sizeof(struct nforce2_smbus));
325 pci_set_drvdata(dev, smbuses);
327 /* SMBus adapter 1 */
328 res1 = nforce2_probe_smb (dev, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
329 if (res1 < 0) {
330 dev_err(&dev->dev, "Error probing SMB1.\n");
331 smbuses[0].base = 0; /* to have a check value */
333 res2 = nforce2_probe_smb (dev, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
334 if (res2 < 0) {
335 dev_err(&dev->dev, "Error probing SMB2.\n");
336 smbuses[1].base = 0; /* to have a check value */
338 if ((res1 < 0) && (res2 < 0)) {
339 /* we did not find even one of the SMBuses, so we give up */
340 kfree(smbuses);
341 return -ENODEV;
344 return 0;
348 static void __devexit nforce2_remove(struct pci_dev *dev)
350 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
352 if (smbuses[0].base) {
353 i2c_del_adapter(&smbuses[0].adapter);
354 release_region(smbuses[0].base, smbuses[0].size);
356 if (smbuses[1].base) {
357 i2c_del_adapter(&smbuses[1].adapter);
358 release_region(smbuses[1].base, smbuses[1].size);
360 kfree(smbuses);
363 static struct pci_driver nforce2_driver = {
364 .name = "nForce2_smbus",
365 .id_table = nforce2_ids,
366 .probe = nforce2_probe,
367 .remove = __devexit_p(nforce2_remove),
370 static int __init nforce2_init(void)
372 return pci_register_driver(&nforce2_driver);
375 static void __exit nforce2_exit(void)
377 pci_unregister_driver(&nforce2_driver);
380 module_init(nforce2_init);
381 module_exit(nforce2_exit);