[PATCH] solaris emulation: incorrect tty locking
[linux-2.6.22.y-op.git] / drivers / i2c / busses / i2c-nforce2.c
blobe0292e414ab2ccadb5110cc6e94af0827b0adcce
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
33 nForce4 MCP-04 0034
34 nForce4 MCP51 0264
35 nForce4 MCP55 0368
37 This driver supports the 2 SMBuses that are included in the MCP of the
38 nForce2/3/4 chipsets.
41 /* Note: we assume there can only be one nForce2, with two SMBus interfaces */
43 #include <linux/module.h>
44 #include <linux/pci.h>
45 #include <linux/kernel.h>
46 #include <linux/stddef.h>
47 #include <linux/sched.h>
48 #include <linux/ioport.h>
49 #include <linux/init.h>
50 #include <linux/i2c.h>
51 #include <linux/delay.h>
52 #include <asm/io.h>
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@arcor.de>");
56 MODULE_DESCRIPTION("nForce2 SMBus driver");
59 struct nforce2_smbus {
60 struct pci_dev *dev;
61 struct i2c_adapter adapter;
62 int base;
63 int size;
68 * nVidia nForce2 SMBus control register definitions
69 * (Newer incarnations use standard BARs 4 and 5 instead)
71 #define NFORCE_PCI_SMB1 0x50
72 #define NFORCE_PCI_SMB2 0x54
76 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
78 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
79 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
80 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
81 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
82 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
83 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data bytes */
84 #define NVIDIA_SMB_ALRM_A (smbus->base + 0x25) /* alarm address */
85 #define NVIDIA_SMB_ALRM_D (smbus->base + 0x26) /* 2 bytes alarm data */
87 #define NVIDIA_SMB_STS_DONE 0x80
88 #define NVIDIA_SMB_STS_ALRM 0x40
89 #define NVIDIA_SMB_STS_RES 0x20
90 #define NVIDIA_SMB_STS_STATUS 0x1f
92 #define NVIDIA_SMB_PRTCL_WRITE 0x00
93 #define NVIDIA_SMB_PRTCL_READ 0x01
94 #define NVIDIA_SMB_PRTCL_QUICK 0x02
95 #define NVIDIA_SMB_PRTCL_BYTE 0x04
96 #define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
97 #define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
98 #define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
99 #define NVIDIA_SMB_PRTCL_PROC_CALL 0x0c
100 #define NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
101 #define NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
102 #define NVIDIA_SMB_PRTCL_PEC 0x80
104 static struct pci_driver nforce2_driver;
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 const 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,
123 /* Return -1 on error. See smbus.h for more information */
124 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
125 unsigned short flags, char read_write,
126 u8 command, int size, union i2c_smbus_data * data)
128 struct nforce2_smbus *smbus = adap->algo_data;
129 unsigned char protocol, pec, temp;
130 unsigned char len = 0; /* to keep the compiler quiet */
131 int i;
133 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
134 NVIDIA_SMB_PRTCL_WRITE;
135 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
137 switch (size) {
139 case I2C_SMBUS_QUICK:
140 protocol |= NVIDIA_SMB_PRTCL_QUICK;
141 read_write = I2C_SMBUS_WRITE;
142 break;
144 case I2C_SMBUS_BYTE:
145 if (read_write == I2C_SMBUS_WRITE)
146 outb_p(command, NVIDIA_SMB_CMD);
147 protocol |= NVIDIA_SMB_PRTCL_BYTE;
148 break;
150 case I2C_SMBUS_BYTE_DATA:
151 outb_p(command, NVIDIA_SMB_CMD);
152 if (read_write == I2C_SMBUS_WRITE)
153 outb_p(data->byte, NVIDIA_SMB_DATA);
154 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
155 break;
157 case I2C_SMBUS_WORD_DATA:
158 outb_p(command, NVIDIA_SMB_CMD);
159 if (read_write == I2C_SMBUS_WRITE) {
160 outb_p(data->word, NVIDIA_SMB_DATA);
161 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
163 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
164 break;
166 case I2C_SMBUS_BLOCK_DATA:
167 outb_p(command, NVIDIA_SMB_CMD);
168 if (read_write == I2C_SMBUS_WRITE) {
169 len = min_t(u8, data->block[0], 32);
170 outb_p(len, NVIDIA_SMB_BCNT);
171 for (i = 0; i < len; i++)
172 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
174 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
175 break;
177 case I2C_SMBUS_I2C_BLOCK_DATA:
178 len = min_t(u8, data->block[0], 32);
179 outb_p(command, NVIDIA_SMB_CMD);
180 outb_p(len, NVIDIA_SMB_BCNT);
181 if (read_write == I2C_SMBUS_WRITE)
182 for (i = 0; i < len; i++)
183 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
184 protocol |= NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA;
185 break;
187 case I2C_SMBUS_PROC_CALL:
188 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
189 return -1;
191 case I2C_SMBUS_BLOCK_PROC_CALL:
192 dev_err(&adap->dev, "I2C_SMBUS_BLOCK_PROC_CALL not supported!\n");
193 return -1;
195 default:
196 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
197 return -1;
200 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
201 outb_p(protocol, NVIDIA_SMB_PRTCL);
203 temp = inb_p(NVIDIA_SMB_STS);
205 if (~temp & NVIDIA_SMB_STS_DONE) {
206 udelay(500);
207 temp = inb_p(NVIDIA_SMB_STS);
209 if (~temp & NVIDIA_SMB_STS_DONE) {
210 msleep(10);
211 temp = inb_p(NVIDIA_SMB_STS);
214 if ((~temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
215 dev_dbg(&adap->dev, "SMBus Timeout! (0x%02x)\n", temp);
216 return -1;
219 if (read_write == I2C_SMBUS_WRITE)
220 return 0;
222 switch (size) {
224 case I2C_SMBUS_BYTE:
225 case I2C_SMBUS_BYTE_DATA:
226 data->byte = inb_p(NVIDIA_SMB_DATA);
227 break;
229 case I2C_SMBUS_WORD_DATA:
230 /* case I2C_SMBUS_PROC_CALL: not supported */
231 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
232 break;
234 case I2C_SMBUS_BLOCK_DATA:
235 /* case I2C_SMBUS_BLOCK_PROC_CALL: not supported */
236 len = inb_p(NVIDIA_SMB_BCNT);
237 len = min_t(u8, len, 32);
238 case I2C_SMBUS_I2C_BLOCK_DATA:
239 for (i = 0; i < len; i++)
240 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
241 data->block[0] = len;
242 break;
245 return 0;
249 static u32 nforce2_func(struct i2c_adapter *adapter)
251 /* other functionality might be possible, but is not tested */
252 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
253 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA /* |
254 I2C_FUNC_SMBUS_BLOCK_DATA */;
258 static struct pci_device_id nforce2_ids[] = {
259 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
260 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
261 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
262 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
263 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
264 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
265 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
266 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
267 { 0 }
271 MODULE_DEVICE_TABLE (pci, nforce2_ids);
274 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
275 int alt_reg, struct nforce2_smbus *smbus, const char *name)
277 int error;
279 smbus->base = pci_resource_start(dev, bar);
280 if (smbus->base) {
281 smbus->size = pci_resource_len(dev, bar);
282 } else {
283 /* Older incarnations of the device used non-standard BARs */
284 u16 iobase;
286 if (pci_read_config_word(dev, alt_reg, &iobase)
287 != PCIBIOS_SUCCESSFUL) {
288 dev_err(&dev->dev, "Error reading PCI config for %s\n",
289 name);
290 return -1;
293 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
294 smbus->size = 8;
296 smbus->dev = dev;
298 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
299 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
300 smbus->base, smbus->base+smbus->size-1, name);
301 return -1;
303 smbus->adapter = nforce2_adapter;
304 smbus->adapter.algo_data = smbus;
305 smbus->adapter.dev.parent = &dev->dev;
306 snprintf(smbus->adapter.name, I2C_NAME_SIZE,
307 "SMBus nForce2 adapter at %04x", smbus->base);
309 error = i2c_add_adapter(&smbus->adapter);
310 if (error) {
311 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
312 release_region(smbus->base, smbus->size);
313 return -1;
315 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
316 return 0;
320 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
322 struct nforce2_smbus *smbuses;
323 int res1, res2;
325 /* we support 2 SMBus adapters */
326 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
327 return -ENOMEM;
328 pci_set_drvdata(dev, smbuses);
330 /* SMBus adapter 1 */
331 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
332 if (res1 < 0) {
333 dev_err(&dev->dev, "Error probing SMB1.\n");
334 smbuses[0].base = 0; /* to have a check value */
336 /* SMBus adapter 2 */
337 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
338 if (res2 < 0) {
339 dev_err(&dev->dev, "Error probing SMB2.\n");
340 smbuses[1].base = 0; /* to have a check value */
342 if ((res1 < 0) && (res2 < 0)) {
343 /* we did not find even one of the SMBuses, so we give up */
344 kfree(smbuses);
345 return -ENODEV;
348 return 0;
352 static void __devexit nforce2_remove(struct pci_dev *dev)
354 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
356 if (smbuses[0].base) {
357 i2c_del_adapter(&smbuses[0].adapter);
358 release_region(smbuses[0].base, smbuses[0].size);
360 if (smbuses[1].base) {
361 i2c_del_adapter(&smbuses[1].adapter);
362 release_region(smbuses[1].base, smbuses[1].size);
364 kfree(smbuses);
367 static struct pci_driver nforce2_driver = {
368 .name = "nForce2_smbus",
369 .id_table = nforce2_ids,
370 .probe = nforce2_probe,
371 .remove = __devexit_p(nforce2_remove),
374 static int __init nforce2_init(void)
376 return pci_register_driver(&nforce2_driver);
379 static void __exit nforce2_exit(void)
381 pci_unregister_driver(&nforce2_driver);
384 module_init(nforce2_init);
385 module_exit(nforce2_exit);