blob: acab4a4818974df5487cdc27efcc3fad7d084308 [file] [log] [blame]
Rich Townsend3f86b832006-07-01 11:36:54 -04001/*
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>
Rich Townsend3f86b832006-07-01 11:36:54 -040017#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"
Rich Townsend3f86b832006-07-01 11:36:54 -040030#define ACPI_EC_HC_DEVICE_NAME "EC HC smbus"
31
32#define _COMPONENT ACPI_EC_HC_COMPONENT
33
Len Brownf52fd662007-02-12 22:42:12 -050034ACPI_MODULE_NAME("i2c_ec");
Rich Townsend3f86b832006-07-01 11:36:54 -040035
36static int acpi_ec_hc_add(struct acpi_device *device);
37static int acpi_ec_hc_remove(struct acpi_device *device, int type);
38
39static struct acpi_driver acpi_ec_hc_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050040 .name = "i2c_ec",
Rich Townsend3f86b832006-07-01 11:36:54 -040041 .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
111static int acpi_ec_smb_read(struct acpi_ec_smbus *smbus, u8 address, u8 * data)
112{
113 u8 val;
114 int err;
115
Rich Townsend3f86b832006-07-01 11:36:54 -0400116 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
124static int acpi_ec_smb_write(struct acpi_ec_smbus *smbus, u8 address, u8 data)
125{
126 int err;
127
Rich Townsend3f86b832006-07-01 11:36:54 -0400128 err = ec_write(smbus->base + address, data);
129 return (err);
130}
131
132static int
133acpi_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
Rich Townsend3f86b832006-07-01 11:36:54 -0400141 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
283static u32 acpi_ec_smb_func(struct i2c_adapter *adapter)
284{
Rich Townsend3f86b832006-07-01 11:36:54 -0400285
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
Jean Delvare8f9082c2006-09-03 22:39:46 +0200294static const struct i2c_algorithm acpi_ec_smbus_algorithm = {
Rich Townsend3f86b832006-07-01 11:36:54 -0400295 .smbus_xfer = acpi_ec_smb_access,
296 .functionality = acpi_ec_smb_func,
297};
298
299static 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
Rich Townsend3f86b832006-07-01 11:36:54 -0400306 if (!device) {
Len Brown635227e2006-07-01 16:48:23 -0400307 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -0400308 }
309
Burman Yan36bcbec2006-12-19 12:56:11 -0800310 ec_hc = kzalloc(sizeof(struct acpi_ec_hc), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -0400311 if (!ec_hc) {
Len Brown635227e2006-07-01 16:48:23 -0400312 return -ENOMEM;
Rich Townsend3f86b832006-07-01 11:36:54 -0400313 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400314
Burman Yan36bcbec2006-12-19 12:56:11 -0800315 smbus = kzalloc(sizeof(struct acpi_ec_smbus), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -0400316 if (!smbus) {
317 kfree(ec_hc);
Len Brown635227e2006-07-01 16:48:23 -0400318 return -ENOMEM;
Rich Townsend3f86b832006-07-01 11:36:54 -0400319 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400320
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"));
Jean Delvare0ee6a172006-08-11 08:30:31 +0200329 kfree(ec_hc);
Rich Townsend3f86b832006-07-01 11:36:54 -0400330 kfree(smbus);
Len Brown635227e2006-07-01 16:48:23 -0400331 return -EIO;
Rich Townsend3f86b832006-07-01 11:36:54 -0400332 }
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;
Jean Delvare12a917f2007-02-13 22:09:03 +0100341 smbus->adapter.dev.parent = &device->dev;
Rich Townsend3f86b832006-07-01 11:36:54 -0400342
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);
Len Brown635227e2006-07-01 16:48:23 -0400348 return -EIO;
Rich Townsend3f86b832006-07-01 11:36:54 -0400349 }
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
Len Brown635227e2006-07-01 16:48:23 -0400356 return AE_OK;
Rich Townsend3f86b832006-07-01 11:36:54 -0400357}
358
359static int acpi_ec_hc_remove(struct acpi_device *device, int type)
360{
361 struct acpi_ec_hc *ec_hc;
362
Rich Townsend3f86b832006-07-01 11:36:54 -0400363 if (!device) {
Len Brown635227e2006-07-01 16:48:23 -0400364 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -0400365 }
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
Len Brown635227e2006-07-01 16:48:23 -0400372 return AE_OK;
Rich Townsend3f86b832006-07-01 11:36:54 -0400373}
374
375static int __init acpi_ec_hc_init(void)
376{
377 int result;
378
Rich Townsend3f86b832006-07-01 11:36:54 -0400379 result = acpi_bus_register_driver(&acpi_ec_hc_driver);
380 if (result < 0) {
Len Brown635227e2006-07-01 16:48:23 -0400381 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400382 }
Len Brown635227e2006-07-01 16:48:23 -0400383 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400384}
385
386static void __exit acpi_ec_hc_exit(void)
387{
Rich Townsend3f86b832006-07-01 11:36:54 -0400388 acpi_bus_unregister_driver(&acpi_ec_hc_driver);
389}
390
391struct acpi_ec_hc *acpi_get_ec_hc(struct acpi_device *device)
392{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200393 return acpi_driver_data(device->parent);
Rich Townsend3f86b832006-07-01 11:36:54 -0400394}
395
396EXPORT_SYMBOL(acpi_get_ec_hc);
397
398module_init(acpi_ec_hc_init);
399module_exit(acpi_ec_hc_exit);
400
401MODULE_LICENSE("GPL");
402MODULE_AUTHOR("Ducrot Bruno");
403MODULE_DESCRIPTION("ACPI EC SMBus driver");