i2c-algo-pcf: Drop unused struct members
[pandora-kernel.git] / drivers / i2c / busses / i2c-amd8111.c
1 /*
2  * SMBus 2.0 driver for AMD-8111 IO-Hub.
3  *
4  * Copyright (c) 2002 Vojtech Pavlik
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation version 2.
9  */
10
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/stddef.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <asm/io.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
23 MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
24
25 struct amd_smbus {
26         struct pci_dev *dev;
27         struct i2c_adapter adapter;
28         int base;
29         int size;
30 };
31
32 static struct pci_driver amd8111_driver;
33
34 /*
35  * AMD PCI control registers definitions.
36  */
37
38 #define AMD_PCI_MISC    0x48
39
40 #define AMD_PCI_MISC_SCI        0x04    /* deliver SCI */
41 #define AMD_PCI_MISC_INT        0x02    /* deliver PCI IRQ */
42 #define AMD_PCI_MISC_SPEEDUP    0x01    /* 16x clock speedup */
43
44 /*
45  * ACPI 2.0 chapter 13 PCI interface definitions.
46  */
47
48 #define AMD_EC_DATA     0x00    /* data register */
49 #define AMD_EC_SC       0x04    /* status of controller */
50 #define AMD_EC_CMD      0x04    /* command register */
51 #define AMD_EC_ICR      0x08    /* interrupt control register */
52
53 #define AMD_EC_SC_SMI   0x04    /* smi event pending */
54 #define AMD_EC_SC_SCI   0x02    /* sci event pending */
55 #define AMD_EC_SC_BURST 0x01    /* burst mode enabled */
56 #define AMD_EC_SC_CMD   0x08    /* byte in data reg is command */
57 #define AMD_EC_SC_IBF   0x02    /* data ready for embedded controller */
58 #define AMD_EC_SC_OBF   0x01    /* data ready for host */
59
60 #define AMD_EC_CMD_RD   0x80    /* read EC */
61 #define AMD_EC_CMD_WR   0x81    /* write EC */
62 #define AMD_EC_CMD_BE   0x82    /* enable burst mode */
63 #define AMD_EC_CMD_BD   0x83    /* disable burst mode */
64 #define AMD_EC_CMD_QR   0x84    /* query EC */
65
66 /*
67  * ACPI 2.0 chapter 13 access of registers of the EC
68  */
69
70 static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
71 {
72         int timeout = 500;
73
74         while (timeout-- && (inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF))
75                 udelay(1);
76
77         if (!timeout) {
78                 dev_warn(&smbus->dev->dev,
79                          "Timeout while waiting for IBF to clear\n");
80                 return -ETIMEDOUT;
81         }
82
83         return 0;
84 }
85
86 static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
87 {
88         int timeout = 500;
89
90         while (timeout-- && (~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF))
91                 udelay(1);
92
93         if (!timeout) {
94                 dev_warn(&smbus->dev->dev,
95                          "Timeout while waiting for OBF to set\n");
96                 return -ETIMEDOUT;
97         }
98
99         return 0;
100 }
101
102 static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address,
103                 unsigned char *data)
104 {
105         int status;
106
107         status = amd_ec_wait_write(smbus);
108         if (status)
109                 return status;
110         outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);
111
112         status = amd_ec_wait_write(smbus);
113         if (status)
114                 return status;
115         outb(address, smbus->base + AMD_EC_DATA);
116
117         status = amd_ec_wait_read(smbus);
118         if (status)
119                 return status;
120         *data = inb(smbus->base + AMD_EC_DATA);
121
122         return 0;
123 }
124
125 static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address,
126                 unsigned char data)
127 {
128         int status;
129
130         status = amd_ec_wait_write(smbus);
131         if (status)
132                 return status;
133         outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);
134
135         status = amd_ec_wait_write(smbus);
136         if (status)
137                 return status;
138         outb(address, smbus->base + AMD_EC_DATA);
139
140         status = amd_ec_wait_write(smbus);
141         if (status)
142                 return status;
143         outb(data, smbus->base + AMD_EC_DATA);
144
145         return 0;
146 }
147
148 /*
149  * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
150  */
151
152 #define AMD_SMB_PRTCL   0x00    /* protocol, PEC */
153 #define AMD_SMB_STS     0x01    /* status */
154 #define AMD_SMB_ADDR    0x02    /* address */
155 #define AMD_SMB_CMD     0x03    /* command */
156 #define AMD_SMB_DATA    0x04    /* 32 data registers */
157 #define AMD_SMB_BCNT    0x24    /* number of data bytes */
158 #define AMD_SMB_ALRM_A  0x25    /* alarm address */
159 #define AMD_SMB_ALRM_D  0x26    /* 2 bytes alarm data */
160
161 #define AMD_SMB_STS_DONE        0x80
162 #define AMD_SMB_STS_ALRM        0x40
163 #define AMD_SMB_STS_RES         0x20
164 #define AMD_SMB_STS_STATUS      0x1f
165
166 #define AMD_SMB_STATUS_OK       0x00
167 #define AMD_SMB_STATUS_FAIL     0x07
168 #define AMD_SMB_STATUS_DNAK     0x10
169 #define AMD_SMB_STATUS_DERR     0x11
170 #define AMD_SMB_STATUS_CMD_DENY 0x12
171 #define AMD_SMB_STATUS_UNKNOWN  0x13
172 #define AMD_SMB_STATUS_ACC_DENY 0x17
173 #define AMD_SMB_STATUS_TIMEOUT  0x18
174 #define AMD_SMB_STATUS_NOTSUP   0x19
175 #define AMD_SMB_STATUS_BUSY     0x1A
176 #define AMD_SMB_STATUS_PEC      0x1F
177
178 #define AMD_SMB_PRTCL_WRITE             0x00
179 #define AMD_SMB_PRTCL_READ              0x01
180 #define AMD_SMB_PRTCL_QUICK             0x02
181 #define AMD_SMB_PRTCL_BYTE              0x04
182 #define AMD_SMB_PRTCL_BYTE_DATA         0x06
183 #define AMD_SMB_PRTCL_WORD_DATA         0x08
184 #define AMD_SMB_PRTCL_BLOCK_DATA        0x0a
185 #define AMD_SMB_PRTCL_PROC_CALL         0x0c
186 #define AMD_SMB_PRTCL_BLOCK_PROC_CALL   0x0d
187 #define AMD_SMB_PRTCL_I2C_BLOCK_DATA    0x4a
188 #define AMD_SMB_PRTCL_PEC               0x80
189
190
191 static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
192                 unsigned short flags, char read_write, u8 command, int size,
193                 union i2c_smbus_data * data)
194 {
195         struct amd_smbus *smbus = adap->algo_data;
196         unsigned char protocol, len, pec, temp[2];
197         int i;
198
199         protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ
200                                                   : AMD_SMB_PRTCL_WRITE;
201         pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
202
203         switch (size) {
204                 case I2C_SMBUS_QUICK:
205                         protocol |= AMD_SMB_PRTCL_QUICK;
206                         read_write = I2C_SMBUS_WRITE;
207                         break;
208
209                 case I2C_SMBUS_BYTE:
210                         if (read_write == I2C_SMBUS_WRITE)
211                                 amd_ec_write(smbus, AMD_SMB_CMD, command);
212                         protocol |= AMD_SMB_PRTCL_BYTE;
213                         break;
214
215                 case I2C_SMBUS_BYTE_DATA:
216                         amd_ec_write(smbus, AMD_SMB_CMD, command);
217                         if (read_write == I2C_SMBUS_WRITE)
218                                 amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
219                         protocol |= AMD_SMB_PRTCL_BYTE_DATA;
220                         break;
221
222                 case I2C_SMBUS_WORD_DATA:
223                         amd_ec_write(smbus, AMD_SMB_CMD, command);
224                         if (read_write == I2C_SMBUS_WRITE) {
225                                 amd_ec_write(smbus, AMD_SMB_DATA,
226                                              data->word & 0xff);
227                                 amd_ec_write(smbus, AMD_SMB_DATA + 1,
228                                              data->word >> 8);
229                         }
230                         protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
231                         break;
232
233                 case I2C_SMBUS_BLOCK_DATA:
234                         amd_ec_write(smbus, AMD_SMB_CMD, command);
235                         if (read_write == I2C_SMBUS_WRITE) {
236                                 len = min_t(u8, data->block[0],
237                                             I2C_SMBUS_BLOCK_MAX);
238                                 amd_ec_write(smbus, AMD_SMB_BCNT, len);
239                                 for (i = 0; i < len; i++)
240                                         amd_ec_write(smbus, AMD_SMB_DATA + i,
241                                                      data->block[i + 1]);
242                         }
243                         protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
244                         break;
245
246                 case I2C_SMBUS_I2C_BLOCK_DATA:
247                         len = min_t(u8, data->block[0],
248                                     I2C_SMBUS_BLOCK_MAX);
249                         amd_ec_write(smbus, AMD_SMB_CMD, command);
250                         amd_ec_write(smbus, AMD_SMB_BCNT, len);
251                         if (read_write == I2C_SMBUS_WRITE)
252                                 for (i = 0; i < len; i++)
253                                         amd_ec_write(smbus, AMD_SMB_DATA + i,
254                                                      data->block[i + 1]);
255                         protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
256                         break;
257
258                 case I2C_SMBUS_PROC_CALL:
259                         amd_ec_write(smbus, AMD_SMB_CMD, command);
260                         amd_ec_write(smbus, AMD_SMB_DATA, data->word & 0xff);
261                         amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
262                         protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
263                         read_write = I2C_SMBUS_READ;
264                         break;
265
266                 case I2C_SMBUS_BLOCK_PROC_CALL:
267                         len = min_t(u8, data->block[0],
268                                     I2C_SMBUS_BLOCK_MAX - 1);
269                         amd_ec_write(smbus, AMD_SMB_CMD, command);
270                         amd_ec_write(smbus, AMD_SMB_BCNT, len);
271                         for (i = 0; i < len; i++)
272                                 amd_ec_write(smbus, AMD_SMB_DATA + i,
273                                              data->block[i + 1]);
274                         protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
275                         read_write = I2C_SMBUS_READ;
276                         break;
277
278                 default:
279                         dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
280                         return -EOPNOTSUPP;
281         }
282
283         amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
284         amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
285
286         /* FIXME this discards status from ec_read(); so temp[0] will
287          * hold stack garbage ... the rest of this routine will act
288          * nonsensically.  Ignored ec_write() status might explain
289          * some such failures...
290          */
291         amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
292
293         if (~temp[0] & AMD_SMB_STS_DONE) {
294                 udelay(500);
295                 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
296         }
297
298         if (~temp[0] & AMD_SMB_STS_DONE) {
299                 msleep(1);
300                 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
301         }
302
303         if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
304                 return -EIO;
305
306         if (read_write == I2C_SMBUS_WRITE)
307                 return 0;
308
309         switch (size) {
310                 case I2C_SMBUS_BYTE:
311                 case I2C_SMBUS_BYTE_DATA:
312                         amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
313                         break;
314
315                 case I2C_SMBUS_WORD_DATA:
316                 case I2C_SMBUS_PROC_CALL:
317                         amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
318                         amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
319                         data->word = (temp[1] << 8) | temp[0];
320                         break;
321
322                 case I2C_SMBUS_BLOCK_DATA:
323                 case I2C_SMBUS_BLOCK_PROC_CALL:
324                         amd_ec_read(smbus, AMD_SMB_BCNT, &len);
325                         len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
326                 case I2C_SMBUS_I2C_BLOCK_DATA:
327                         for (i = 0; i < len; i++)
328                                 amd_ec_read(smbus, AMD_SMB_DATA + i,
329                                             data->block + i + 1);
330                         data->block[0] = len;
331                         break;
332         }
333
334         return 0;
335 }
336
337
338 static u32 amd8111_func(struct i2c_adapter *adapter)
339 {
340         return  I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
341                 I2C_FUNC_SMBUS_BYTE_DATA |
342                 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
343                 I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
344                 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC;
345 }
346
347 static const struct i2c_algorithm smbus_algorithm = {
348         .smbus_xfer = amd8111_access,
349         .functionality = amd8111_func,
350 };
351
352
353 static struct pci_device_id amd8111_ids[] = {
354         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
355         { 0, }
356 };
357
358 MODULE_DEVICE_TABLE (pci, amd8111_ids);
359
360 static int __devinit amd8111_probe(struct pci_dev *dev,
361                 const struct pci_device_id *id)
362 {
363         struct amd_smbus *smbus;
364         int error;
365
366         if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
367                 return -ENODEV;
368
369         smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
370         if (!smbus)
371                 return -ENOMEM;
372
373         smbus->dev = dev;
374         smbus->base = pci_resource_start(dev, 0);
375         smbus->size = pci_resource_len(dev, 0);
376
377         if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
378                 error = -EBUSY;
379                 goto out_kfree;
380         }
381
382         smbus->adapter.owner = THIS_MODULE;
383         snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
384                 "SMBus2 AMD8111 adapter at %04x", smbus->base);
385         smbus->adapter.id = I2C_HW_SMBUS_AMD8111;
386         smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
387         smbus->adapter.algo = &smbus_algorithm;
388         smbus->adapter.algo_data = smbus;
389
390         /* set up the sysfs linkage to our parent device */
391         smbus->adapter.dev.parent = &dev->dev;
392
393         pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
394         error = i2c_add_adapter(&smbus->adapter);
395         if (error)
396                 goto out_release_region;
397
398         pci_set_drvdata(dev, smbus);
399         return 0;
400
401  out_release_region:
402         release_region(smbus->base, smbus->size);
403  out_kfree:
404         kfree(smbus);
405         return error;
406 }
407
408 static void __devexit amd8111_remove(struct pci_dev *dev)
409 {
410         struct amd_smbus *smbus = pci_get_drvdata(dev);
411
412         i2c_del_adapter(&smbus->adapter);
413         release_region(smbus->base, smbus->size);
414         kfree(smbus);
415 }
416
417 static struct pci_driver amd8111_driver = {
418         .name           = "amd8111_smbus2",
419         .id_table       = amd8111_ids,
420         .probe          = amd8111_probe,
421         .remove         = __devexit_p(amd8111_remove),
422 };
423
424 static int __init i2c_amd8111_init(void)
425 {
426         return pci_register_driver(&amd8111_driver);
427 }
428
429 static void __exit i2c_amd8111_exit(void)
430 {
431         pci_unregister_driver(&amd8111_driver);
432 }
433
434 module_init(i2c_amd8111_init);
435 module_exit(i2c_amd8111_exit);