[PATCH] I2C: ALI1563 SMBus driver fix
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / drivers / i2c / busses / i2c-ali1563.c
CommitLineData
1da177e4
LT
1/**
2 * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
3 *
4 * Copyright (C) 2004 Patrick Mochel
4a4e5787 5 * 2005 Rudolf Marek <r.marek@sh.cvut.cz>
1da177e4
LT
6 *
7 * The 1563 southbridge is deceptively similar to the 1533, with a
8 * few notable exceptions. One of those happens to be the fact they
9 * upgraded the i2c core to be 2.0 compliant, and happens to be almost
10 * identical to the i2c controller found in the Intel 801 south
11 * bridges.
12 *
13 * This driver is based on a mix of the 15x3, 1535, and i801 drivers,
14 * with a little help from the ALi 1563 spec.
15 *
16 * This file is released under the GPLv2
17 */
18
19#include <linux/module.h>
20#include <linux/delay.h>
21#include <linux/i2c.h>
22#include <linux/pci.h>
23#include <linux/init.h>
24
25#define ALI1563_MAX_TIMEOUT 500
26#define ALI1563_SMBBA 0x80
27#define ALI1563_SMB_IOEN 1
28#define ALI1563_SMB_HOSTEN 2
29#define ALI1563_SMB_IOSIZE 16
30
31#define SMB_HST_STS (ali1563_smba + 0)
32#define SMB_HST_CNTL1 (ali1563_smba + 1)
33#define SMB_HST_CNTL2 (ali1563_smba + 2)
34#define SMB_HST_CMD (ali1563_smba + 3)
35#define SMB_HST_ADD (ali1563_smba + 4)
36#define SMB_HST_DAT0 (ali1563_smba + 5)
37#define SMB_HST_DAT1 (ali1563_smba + 6)
38#define SMB_BLK_DAT (ali1563_smba + 7)
39
40#define HST_STS_BUSY 0x01
41#define HST_STS_INTR 0x02
42#define HST_STS_DEVERR 0x04
43#define HST_STS_BUSERR 0x08
44#define HST_STS_FAIL 0x10
45#define HST_STS_DONE 0x80
46#define HST_STS_BAD 0x1c
47
48
49#define HST_CNTL1_TIMEOUT 0x80
50#define HST_CNTL1_LAST 0x40
51
52#define HST_CNTL2_KILL 0x04
53#define HST_CNTL2_START 0x40
54#define HST_CNTL2_QUICK 0x00
55#define HST_CNTL2_BYTE 0x01
56#define HST_CNTL2_BYTE_DATA 0x02
57#define HST_CNTL2_WORD_DATA 0x03
58#define HST_CNTL2_BLOCK 0x05
59
60
4a4e5787 61#define HST_CNTL2_SIZEMASK 0x38
1da177e4
LT
62
63static unsigned short ali1563_smba;
64
4a4e5787 65static int ali1563_transaction(struct i2c_adapter * a, int size)
1da177e4
LT
66{
67 u32 data;
68 int timeout;
69
70 dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
71 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
72 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
73 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
74 inb_p(SMB_HST_DAT1));
75
76 data = inb_p(SMB_HST_STS);
77 if (data & HST_STS_BAD) {
4a4e5787 78 dev_err(&a->dev, "ali1563: Trying to reset busy device\n");
1da177e4
LT
79 outb_p(data | HST_STS_BAD,SMB_HST_STS);
80 data = inb_p(SMB_HST_STS);
81 if (data & HST_STS_BAD)
82 return -EBUSY;
83 }
84 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
85
86 timeout = ALI1563_MAX_TIMEOUT;
87 do
88 msleep(1);
89 while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
90
91 dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
92 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
93 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
94 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
95 inb_p(SMB_HST_DAT1));
96
97 if (timeout && !(data & HST_STS_BAD))
98 return 0;
1da177e4 99
4a4e5787
RM
100 if (!timeout) {
101 dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n");
1da177e4
LT
102 /* Issue 'kill' to host controller */
103 outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2);
4a4e5787
RM
104 data = inb_p(SMB_HST_STS);
105 }
106
107 /* device error - no response, ignore the autodetection case */
108 if ((data & HST_STS_DEVERR) && (size != HST_CNTL2_QUICK)) {
109 dev_err(&a->dev, "Device error!\n");
110 }
111
112 /* bus collision */
113 if (data & HST_STS_BUSERR) {
114 dev_err(&a->dev, "Bus collision!\n");
115 /* Issue timeout, hoping it helps */
1da177e4 116 outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1);
4a4e5787
RM
117 }
118
119 if (data & HST_STS_FAIL) {
120 dev_err(&a->dev, "Cleaning fail after KILL!\n");
121 outb_p(0x0,SMB_HST_CNTL2);
122 }
123
1da177e4
LT
124 return -1;
125}
126
127static int ali1563_block_start(struct i2c_adapter * a)
128{
129 u32 data;
130 int timeout;
131
132 dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
133 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
134 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
135 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
136 inb_p(SMB_HST_DAT1));
137
138 data = inb_p(SMB_HST_STS);
139 if (data & HST_STS_BAD) {
140 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
141 outb_p(data | HST_STS_BAD,SMB_HST_STS);
142 data = inb_p(SMB_HST_STS);
143 if (data & HST_STS_BAD)
144 return -EBUSY;
145 }
146
147 /* Clear byte-ready bit */
148 outb_p(data | HST_STS_DONE, SMB_HST_STS);
149
150 /* Start transaction and wait for byte-ready bit to be set */
151 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
152
153 timeout = ALI1563_MAX_TIMEOUT;
154 do
155 msleep(1);
156 while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
157
158 dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
159 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
160 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
161 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
162 inb_p(SMB_HST_DAT1));
163
164 if (timeout && !(data & HST_STS_BAD))
165 return 0;
4a4e5787 166 dev_err(&a->dev, "SMBus Error: %s%s%s%s%s\n",
1da177e4
LT
167 timeout ? "Timeout " : "",
168 data & HST_STS_FAIL ? "Transaction Failed " : "",
169 data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
170 data & HST_STS_DEVERR ? "Device Error " : "",
171 !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
172 return -1;
173}
174
175static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw)
176{
177 int i, len;
178 int error = 0;
179
180 /* Do we need this? */
181 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
182
183 if (rw == I2C_SMBUS_WRITE) {
184 len = data->block[0];
185 if (len < 1)
186 len = 1;
187 else if (len > 32)
188 len = 32;
189 outb_p(len,SMB_HST_DAT0);
190 outb_p(data->block[1],SMB_BLK_DAT);
191 } else
192 len = 32;
193
194 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
195
196 for (i = 0; i < len; i++) {
197 if (rw == I2C_SMBUS_WRITE) {
198 outb_p(data->block[i + 1], SMB_BLK_DAT);
199 if ((error = ali1563_block_start(a)))
200 break;
201 } else {
202 if ((error = ali1563_block_start(a)))
203 break;
204 if (i == 0) {
205 len = inb_p(SMB_HST_DAT0);
206 if (len < 1)
207 len = 1;
208 else if (len > 32)
209 len = 32;
210 }
211 data->block[i+1] = inb_p(SMB_BLK_DAT);
212 }
213 }
214 /* Do we need this? */
215 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
216 return error;
217}
218
219static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
220 unsigned short flags, char rw, u8 cmd,
221 int size, union i2c_smbus_data * data)
222{
223 int error = 0;
224 int timeout;
225 u32 reg;
226
227 for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
228 if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY))
229 break;
230 }
231 if (!timeout)
232 dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg);
233 outb_p(0xff,SMB_HST_STS);
234
235 /* Map the size to what the chip understands */
236 switch (size) {
237 case I2C_SMBUS_PROC_CALL:
238 dev_err(&a->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
239 error = -EINVAL;
240 break;
241 case I2C_SMBUS_QUICK:
242 size = HST_CNTL2_QUICK;
243 break;
244 case I2C_SMBUS_BYTE:
245 size = HST_CNTL2_BYTE;
246 break;
247 case I2C_SMBUS_BYTE_DATA:
248 size = HST_CNTL2_BYTE_DATA;
249 break;
250 case I2C_SMBUS_WORD_DATA:
251 size = HST_CNTL2_WORD_DATA;
252 break;
253 case I2C_SMBUS_BLOCK_DATA:
254 size = HST_CNTL2_BLOCK;
255 break;
256 }
257
258 outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
4a4e5787 259 outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) | (size << 3), SMB_HST_CNTL2);
1da177e4
LT
260
261 /* Write the command register */
4a4e5787 262
1da177e4
LT
263 switch(size) {
264 case HST_CNTL2_BYTE:
265 if (rw== I2C_SMBUS_WRITE)
4a4e5787
RM
266 /* Beware it uses DAT0 register and not CMD! */
267 outb_p(cmd, SMB_HST_DAT0);
1da177e4
LT
268 break;
269 case HST_CNTL2_BYTE_DATA:
270 outb_p(cmd, SMB_HST_CMD);
271 if (rw == I2C_SMBUS_WRITE)
272 outb_p(data->byte, SMB_HST_DAT0);
273 break;
274 case HST_CNTL2_WORD_DATA:
275 outb_p(cmd, SMB_HST_CMD);
276 if (rw == I2C_SMBUS_WRITE) {
277 outb_p(data->word & 0xff, SMB_HST_DAT0);
278 outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
279 }
280 break;
281 case HST_CNTL2_BLOCK:
282 outb_p(cmd, SMB_HST_CMD);
283 error = ali1563_block(a,data,rw);
284 goto Done;
285 }
286
4a4e5787 287 if ((error = ali1563_transaction(a, size)))
1da177e4
LT
288 goto Done;
289
290 if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
291 goto Done;
292
293 switch (size) {
294 case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */
295 data->byte = inb_p(SMB_HST_DAT0);
296 break;
297 case HST_CNTL2_BYTE_DATA:
298 data->byte = inb_p(SMB_HST_DAT0);
299 break;
300 case HST_CNTL2_WORD_DATA:
301 data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
302 break;
303 }
304Done:
305 return error;
306}
307
308static u32 ali1563_func(struct i2c_adapter * a)
309{
310 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
311 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
312 I2C_FUNC_SMBUS_BLOCK_DATA;
313}
314
315
316static void ali1563_enable(struct pci_dev * dev)
317{
318 u16 ctrl;
319
320 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
321 ctrl |= 0x7;
322 pci_write_config_word(dev,ALI1563_SMBBA,ctrl);
323}
324
325static int __devinit ali1563_setup(struct pci_dev * dev)
326{
327 u16 ctrl;
328
329 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
330 printk("ali1563: SMBus control = %04x\n",ctrl);
331
332 /* Check if device is even enabled first */
333 if (!(ctrl & ALI1563_SMB_IOEN)) {
334 dev_warn(&dev->dev,"I/O space not enabled, trying manually\n");
335 ali1563_enable(dev);
336 }
337 if (!(ctrl & ALI1563_SMB_IOEN)) {
338 dev_warn(&dev->dev,"I/O space still not enabled, giving up\n");
339 goto Err;
340 }
341 if (!(ctrl & ALI1563_SMB_HOSTEN)) {
342 dev_warn(&dev->dev,"Host Controller not enabled\n");
343 goto Err;
344 }
345
346 /* SMB I/O Base in high 12 bits and must be aligned with the
347 * size of the I/O space. */
348 ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
349 if (!ali1563_smba) {
350 dev_warn(&dev->dev,"ali1563_smba Uninitialized\n");
351 goto Err;
352 }
353 if (!request_region(ali1563_smba,ALI1563_SMB_IOSIZE,"i2c-ali1563")) {
354 dev_warn(&dev->dev,"Could not allocate I/O space");
355 goto Err;
356 }
357
358 return 0;
359Err:
360 return -ENODEV;
361}
362
363static void ali1563_shutdown(struct pci_dev *dev)
364{
365 release_region(ali1563_smba,ALI1563_SMB_IOSIZE);
366}
367
368static struct i2c_algorithm ali1563_algorithm = {
369 .name = "Non-i2c SMBus adapter",
370 .id = I2C_ALGO_SMBUS,
371 .smbus_xfer = ali1563_access,
372 .functionality = ali1563_func,
373};
374
375static struct i2c_adapter ali1563_adapter = {
376 .owner = THIS_MODULE,
377 .class = I2C_CLASS_HWMON,
378 .algo = &ali1563_algorithm,
379};
380
381static int __devinit ali1563_probe(struct pci_dev * dev,
382 const struct pci_device_id * id_table)
383{
384 int error;
385
386 if ((error = ali1563_setup(dev)))
387 return error;
388 ali1563_adapter.dev.parent = &dev->dev;
389 sprintf(ali1563_adapter.name,"SMBus ALi 1563 Adapter @ %04x",
390 ali1563_smba);
391 if ((error = i2c_add_adapter(&ali1563_adapter)))
392 ali1563_shutdown(dev);
393 printk("%s: Returning %d\n",__FUNCTION__,error);
394 return error;
395}
396
397static void __devexit ali1563_remove(struct pci_dev * dev)
398{
399 i2c_del_adapter(&ali1563_adapter);
400 ali1563_shutdown(dev);
401}
402
403static struct pci_device_id __devinitdata ali1563_id_table[] = {
404 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
405 {},
406};
407
408MODULE_DEVICE_TABLE (pci, ali1563_id_table);
409
410static struct pci_driver ali1563_pci_driver = {
411 .name = "ali1563_i2c",
412 .id_table = ali1563_id_table,
413 .probe = ali1563_probe,
414 .remove = __devexit_p(ali1563_remove),
415};
416
417static int __init ali1563_init(void)
418{
419 return pci_register_driver(&ali1563_pci_driver);
420}
421
422module_init(ali1563_init);
423
424static void __exit ali1563_exit(void)
425{
426 pci_unregister_driver(&ali1563_pci_driver);
427}
428
429module_exit(ali1563_exit);
430
431MODULE_LICENSE("GPL");