[POWERPC] cell: add support for proper device-tree
[linux-2.6] / drivers / acpi / i2c_ec.c
1 /*
2  * SMBus driver for ACPI Embedded Controller ($Revision: 1.3 $)
3  *
4  * Copyright (c) 2002, 2005 Ducrot Bruno
5  * Copyright (c) 2005 Rich Townsend (tiny hacks & tweaks)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation version 2.
10  */
11
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/stddef.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
19 #include <linux/acpi.h>
20 #include <linux/delay.h>
21
22 #include "i2c_ec.h"
23
24 #define xudelay(t)      udelay(t)
25 #define xmsleep(t)      msleep(t)
26
27 #define ACPI_EC_HC_COMPONENT    0x00080000
28 #define ACPI_EC_HC_CLASS        "ec_hc_smbus"
29 #define ACPI_EC_HC_HID          "ACPI0001"
30 #define ACPI_EC_HC_DEVICE_NAME  "EC HC smbus"
31
32 #define _COMPONENT              ACPI_EC_HC_COMPONENT
33
34 ACPI_MODULE_NAME("i2c_ec");
35
36 static int acpi_ec_hc_add(struct acpi_device *device);
37 static int acpi_ec_hc_remove(struct acpi_device *device, int type);
38
39 static struct acpi_driver acpi_ec_hc_driver = {
40         .name = "i2c_ec",
41         .class = ACPI_EC_HC_CLASS,
42         .ids = ACPI_EC_HC_HID,
43         .ops = {
44                 .add = acpi_ec_hc_add,
45                 .remove = acpi_ec_hc_remove,
46                 },
47 };
48
49 /* Various bit mask for EC_SC (R) */
50 #define OBF             0x01
51 #define IBF             0x02
52 #define CMD             0x08
53 #define BURST           0x10
54 #define SCI_EVT         0x20
55 #define SMI_EVT         0x40
56
57 /* Commands for EC_SC (W) */
58 #define RD_EC           0x80
59 #define WR_EC           0x81
60 #define BE_EC           0x82
61 #define BD_EC           0x83
62 #define QR_EC           0x84
63
64 /*
65  * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
66  */
67
68 #define ACPI_EC_SMB_PRTCL       0x00    /* protocol, PEC */
69 #define ACPI_EC_SMB_STS         0x01    /* status */
70 #define ACPI_EC_SMB_ADDR        0x02    /* address */
71 #define ACPI_EC_SMB_CMD         0x03    /* command */
72 #define ACPI_EC_SMB_DATA        0x04    /* 32 data registers */
73 #define ACPI_EC_SMB_BCNT        0x24    /* number of data bytes */
74 #define ACPI_EC_SMB_ALRM_A      0x25    /* alarm address */
75 #define ACPI_EC_SMB_ALRM_D      0x26    /* 2 bytes alarm data */
76
77 #define ACPI_EC_SMB_STS_DONE    0x80
78 #define ACPI_EC_SMB_STS_ALRM    0x40
79 #define ACPI_EC_SMB_STS_RES     0x20
80 #define ACPI_EC_SMB_STS_STATUS  0x1f
81
82 #define ACPI_EC_SMB_STATUS_OK           0x00
83 #define ACPI_EC_SMB_STATUS_FAIL         0x07
84 #define ACPI_EC_SMB_STATUS_DNAK         0x10
85 #define ACPI_EC_SMB_STATUS_DERR         0x11
86 #define ACPI_EC_SMB_STATUS_CMD_DENY     0x12
87 #define ACPI_EC_SMB_STATUS_UNKNOWN      0x13
88 #define ACPI_EC_SMB_STATUS_ACC_DENY     0x17
89 #define ACPI_EC_SMB_STATUS_TIMEOUT      0x18
90 #define ACPI_EC_SMB_STATUS_NOTSUP       0x19
91 #define ACPI_EC_SMB_STATUS_BUSY         0x1A
92 #define ACPI_EC_SMB_STATUS_PEC          0x1F
93
94 #define ACPI_EC_SMB_PRTCL_WRITE                 0x00
95 #define ACPI_EC_SMB_PRTCL_READ                  0x01
96 #define ACPI_EC_SMB_PRTCL_QUICK                 0x02
97 #define ACPI_EC_SMB_PRTCL_BYTE                  0x04
98 #define ACPI_EC_SMB_PRTCL_BYTE_DATA             0x06
99 #define ACPI_EC_SMB_PRTCL_WORD_DATA             0x08
100 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA            0x0a
101 #define ACPI_EC_SMB_PRTCL_PROC_CALL             0x0c
102 #define ACPI_EC_SMB_PRTCL_BLOCK_PROC_CALL       0x0d
103 #define ACPI_EC_SMB_PRTCL_I2C_BLOCK_DATA        0x4a
104 #define ACPI_EC_SMB_PRTCL_PEC                   0x80
105
106 /* Length of pre/post transaction sleep (msec) */
107 #define ACPI_EC_SMB_TRANSACTION_SLEEP           1
108 #define ACPI_EC_SMB_ACCESS_SLEEP1               1
109 #define ACPI_EC_SMB_ACCESS_SLEEP2               10
110
111 static int acpi_ec_smb_read(struct acpi_ec_smbus *smbus, u8 address, u8 * data)
112 {
113         u8 val;
114         int err;
115
116         err = ec_read(smbus->base + address, &val);
117         if (!err) {
118                 *data = val;
119         }
120         xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
121         return (err);
122 }
123
124 static int acpi_ec_smb_write(struct acpi_ec_smbus *smbus, u8 address, u8 data)
125 {
126         int err;
127
128         err = ec_write(smbus->base + address, data);
129         return (err);
130 }
131
132 static int
133 acpi_ec_smb_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
134                    char read_write, u8 command, int size,
135                    union i2c_smbus_data *data)
136 {
137         struct acpi_ec_smbus *smbus = adap->algo_data;
138         unsigned char protocol, len = 0, pec, temp[2] = { 0, 0 };
139         int i;
140
141         if (read_write == I2C_SMBUS_READ) {
142                 protocol = ACPI_EC_SMB_PRTCL_READ;
143         } else {
144                 protocol = ACPI_EC_SMB_PRTCL_WRITE;
145         }
146         pec = (flags & I2C_CLIENT_PEC) ? ACPI_EC_SMB_PRTCL_PEC : 0;
147
148         switch (size) {
149
150         case I2C_SMBUS_QUICK:
151                 protocol |= ACPI_EC_SMB_PRTCL_QUICK;
152                 read_write = I2C_SMBUS_WRITE;
153                 break;
154
155         case I2C_SMBUS_BYTE:
156                 if (read_write == I2C_SMBUS_WRITE) {
157                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->byte);
158                 }
159                 protocol |= ACPI_EC_SMB_PRTCL_BYTE;
160                 break;
161
162         case I2C_SMBUS_BYTE_DATA:
163                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
164                 if (read_write == I2C_SMBUS_WRITE) {
165                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->byte);
166                 }
167                 protocol |= ACPI_EC_SMB_PRTCL_BYTE_DATA;
168                 break;
169
170         case I2C_SMBUS_WORD_DATA:
171                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
172                 if (read_write == I2C_SMBUS_WRITE) {
173                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->word);
174                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + 1,
175                                           data->word >> 8);
176                 }
177                 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA | pec;
178                 break;
179
180         case I2C_SMBUS_BLOCK_DATA:
181                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
182                 if (read_write == I2C_SMBUS_WRITE) {
183                         len = min_t(u8, data->block[0], 32);
184                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
185                         for (i = 0; i < len; i++)
186                                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
187                                                   data->block[i + 1]);
188                 }
189                 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA | pec;
190                 break;
191
192         case I2C_SMBUS_I2C_BLOCK_DATA:
193                 len = min_t(u8, data->block[0], 32);
194                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
195                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
196                 if (read_write == I2C_SMBUS_WRITE) {
197                         for (i = 0; i < len; i++) {
198                                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
199                                                   data->block[i + 1]);
200                         }
201                 }
202                 protocol |= ACPI_EC_SMB_PRTCL_I2C_BLOCK_DATA;
203                 break;
204
205         case I2C_SMBUS_PROC_CALL:
206                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
207                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->word);
208                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + 1, data->word >> 8);
209                 protocol = ACPI_EC_SMB_PRTCL_PROC_CALL | pec;
210                 read_write = I2C_SMBUS_READ;
211                 break;
212
213         case I2C_SMBUS_BLOCK_PROC_CALL:
214                 protocol |= pec;
215                 len = min_t(u8, data->block[0], 31);
216                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
217                 acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
218                 for (i = 0; i < len; i++)
219                         acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
220                                           data->block[i + 1]);
221                 protocol = ACPI_EC_SMB_PRTCL_BLOCK_PROC_CALL | pec;
222                 read_write = I2C_SMBUS_READ;
223                 break;
224
225         default:
226                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "EC SMBus adapter: "
227                                   "Unsupported transaction %d\n", size));
228                 return (-1);
229         }
230
231         acpi_ec_smb_write(smbus, ACPI_EC_SMB_ADDR, addr << 1);
232         acpi_ec_smb_write(smbus, ACPI_EC_SMB_PRTCL, protocol);
233
234         acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
235
236         if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
237                 xudelay(500);
238                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
239         }
240         if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
241                 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
242                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
243         }
244         if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
245             || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
246                 return (-1);
247         }
248
249         if (read_write == I2C_SMBUS_WRITE) {
250                 return (0);
251         }
252
253         switch (size) {
254
255         case I2C_SMBUS_BYTE:
256         case I2C_SMBUS_BYTE_DATA:
257                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA, &data->byte);
258                 break;
259
260         case I2C_SMBUS_WORD_DATA:
261         case I2C_SMBUS_PROC_CALL:
262                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA, temp + 0);
263                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA + 1, temp + 1);
264                 data->word = (temp[1] << 8) | temp[0];
265                 break;
266
267         case I2C_SMBUS_BLOCK_DATA:
268         case I2C_SMBUS_BLOCK_PROC_CALL:
269                 len = 0;
270                 acpi_ec_smb_read(smbus, ACPI_EC_SMB_BCNT, &len);
271                 len = min_t(u8, len, 32);
272         case I2C_SMBUS_I2C_BLOCK_DATA:
273                 for (i = 0; i < len; i++)
274                         acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA + i,
275                                          data->block + i + 1);
276                 data->block[0] = len;
277                 break;
278         }
279
280         return (0);
281 }
282
283 static u32 acpi_ec_smb_func(struct i2c_adapter *adapter)
284 {
285
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                 I2C_FUNC_SMBUS_PROC_CALL |
290                 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
291                 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_HWPEC_CALC);
292 }
293
294 static const struct i2c_algorithm acpi_ec_smbus_algorithm = {
295         .smbus_xfer = acpi_ec_smb_access,
296         .functionality = acpi_ec_smb_func,
297 };
298
299 static int acpi_ec_hc_add(struct acpi_device *device)
300 {
301         int status;
302         unsigned long val;
303         struct acpi_ec_hc *ec_hc;
304         struct acpi_ec_smbus *smbus;
305
306         if (!device) {
307                 return -EINVAL;
308         }
309
310         ec_hc = kzalloc(sizeof(struct acpi_ec_hc), GFP_KERNEL);
311         if (!ec_hc) {
312                 return -ENOMEM;
313         }
314
315         smbus = kzalloc(sizeof(struct acpi_ec_smbus), GFP_KERNEL);
316         if (!smbus) {
317                 kfree(ec_hc);
318                 return -ENOMEM;
319         }
320
321         ec_hc->handle = device->handle;
322         strcpy(acpi_device_name(device), ACPI_EC_HC_DEVICE_NAME);
323         strcpy(acpi_device_class(device), ACPI_EC_HC_CLASS);
324         acpi_driver_data(device) = ec_hc;
325
326         status = acpi_evaluate_integer(ec_hc->handle, "_EC", NULL, &val);
327         if (ACPI_FAILURE(status)) {
328                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error obtaining _EC\n"));
329                 kfree(ec_hc);
330                 kfree(smbus);
331                 return -EIO;
332         }
333
334         smbus->ec = acpi_driver_data(device->parent);
335         smbus->base = (val & 0xff00ull) >> 8;
336         smbus->alert = val & 0xffull;
337
338         smbus->adapter.owner = THIS_MODULE;
339         smbus->adapter.algo = &acpi_ec_smbus_algorithm;
340         smbus->adapter.algo_data = smbus;
341         smbus->adapter.dev.parent = &device->dev;
342
343         if (i2c_add_adapter(&smbus->adapter)) {
344                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
345                                   "EC SMBus adapter: Failed to register adapter\n"));
346                 kfree(smbus);
347                 kfree(ec_hc);
348                 return -EIO;
349         }
350
351         ec_hc->smbus = smbus;
352
353         printk(KERN_INFO PREFIX "%s [%s]\n",
354                acpi_device_name(device), acpi_device_bid(device));
355
356         return AE_OK;
357 }
358
359 static int acpi_ec_hc_remove(struct acpi_device *device, int type)
360 {
361         struct acpi_ec_hc *ec_hc;
362
363         if (!device) {
364                 return -EINVAL;
365         }
366         ec_hc = acpi_driver_data(device);
367
368         i2c_del_adapter(&ec_hc->smbus->adapter);
369         kfree(ec_hc->smbus);
370         kfree(ec_hc);
371
372         return AE_OK;
373 }
374
375 static int __init acpi_ec_hc_init(void)
376 {
377         int result;
378
379         result = acpi_bus_register_driver(&acpi_ec_hc_driver);
380         if (result < 0) {
381                 return -ENODEV;
382         }
383         return 0;
384 }
385
386 static void __exit acpi_ec_hc_exit(void)
387 {
388         acpi_bus_unregister_driver(&acpi_ec_hc_driver);
389 }
390
391 struct acpi_ec_hc *acpi_get_ec_hc(struct acpi_device *device)
392 {
393         return acpi_driver_data(device->parent);
394 }
395
396 EXPORT_SYMBOL(acpi_get_ec_hc);
397
398 module_init(acpi_ec_hc_init);
399 module_exit(acpi_ec_hc_exit);
400
401 MODULE_LICENSE("GPL");
402 MODULE_AUTHOR("Ducrot Bruno");
403 MODULE_DESCRIPTION("ACPI EC SMBus driver");