2 * SMBus driver for ACPI Embedded Controller ($Revision: 1.3 $)
4 * Copyright (c) 2002, 2005 Ducrot Bruno
5 * Copyright (c) 2005 Rich Townsend (tiny hacks & tweaks)
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.
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>
24 #define xudelay(t) udelay(t)
25 #define xmsleep(t) msleep(t)
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"
32 #define _COMPONENT ACPI_EC_HC_COMPONENT
34 ACPI_MODULE_NAME("i2c_ec");
36 static int acpi_ec_hc_add(struct acpi_device *device);
37 static int acpi_ec_hc_remove(struct acpi_device *device, int type);
39 static struct acpi_driver acpi_ec_hc_driver = {
41 .class = ACPI_EC_HC_CLASS,
42 .ids = ACPI_EC_HC_HID,
44 .add = acpi_ec_hc_add,
45 .remove = acpi_ec_hc_remove,
49 /* Various bit mask for EC_SC (R) */
57 /* Commands for EC_SC (W) */
65 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
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 */
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
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
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
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
111 static int acpi_ec_smb_read(struct acpi_ec_smbus *smbus, u8 address, u8 * data)
116 err = ec_read(smbus->base + address, &val);
120 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
124 static int acpi_ec_smb_write(struct acpi_ec_smbus *smbus, u8 address, u8 data)
128 err = ec_write(smbus->base + address, data);
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)
137 struct acpi_ec_smbus *smbus = adap->algo_data;
138 unsigned char protocol, len = 0, pec, temp[2] = { 0, 0 };
141 if (read_write == I2C_SMBUS_READ) {
142 protocol = ACPI_EC_SMB_PRTCL_READ;
144 protocol = ACPI_EC_SMB_PRTCL_WRITE;
146 pec = (flags & I2C_CLIENT_PEC) ? ACPI_EC_SMB_PRTCL_PEC : 0;
150 case I2C_SMBUS_QUICK:
151 protocol |= ACPI_EC_SMB_PRTCL_QUICK;
152 read_write = I2C_SMBUS_WRITE;
156 if (read_write == I2C_SMBUS_WRITE) {
157 acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->byte);
159 protocol |= ACPI_EC_SMB_PRTCL_BYTE;
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);
167 protocol |= ACPI_EC_SMB_PRTCL_BYTE_DATA;
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,
177 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA | pec;
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,
189 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA | pec;
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,
202 protocol |= ACPI_EC_SMB_PRTCL_I2C_BLOCK_DATA;
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;
213 case I2C_SMBUS_BLOCK_PROC_CALL:
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,
221 protocol = ACPI_EC_SMB_PRTCL_BLOCK_PROC_CALL | pec;
222 read_write = I2C_SMBUS_READ;
226 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "EC SMBus adapter: "
227 "Unsupported transaction %d\n", size));
231 acpi_ec_smb_write(smbus, ACPI_EC_SMB_ADDR, addr << 1);
232 acpi_ec_smb_write(smbus, ACPI_EC_SMB_PRTCL, protocol);
234 acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
236 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
238 acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
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);
244 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
245 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
249 if (read_write == I2C_SMBUS_WRITE) {
256 case I2C_SMBUS_BYTE_DATA:
257 acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA, &data->byte);
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];
267 case I2C_SMBUS_BLOCK_DATA:
268 case I2C_SMBUS_BLOCK_PROC_CALL:
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;
283 static u32 acpi_ec_smb_func(struct i2c_adapter *adapter)
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);
294 static const struct i2c_algorithm acpi_ec_smbus_algorithm = {
295 .smbus_xfer = acpi_ec_smb_access,
296 .functionality = acpi_ec_smb_func,
299 static int acpi_ec_hc_add(struct acpi_device *device)
303 struct acpi_ec_hc *ec_hc;
304 struct acpi_ec_smbus *smbus;
310 ec_hc = kzalloc(sizeof(struct acpi_ec_hc), GFP_KERNEL);
315 smbus = kzalloc(sizeof(struct acpi_ec_smbus), GFP_KERNEL);
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;
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"));
334 smbus->ec = acpi_driver_data(device->parent);
335 smbus->base = (val & 0xff00ull) >> 8;
336 smbus->alert = val & 0xffull;
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;
343 if (i2c_add_adapter(&smbus->adapter)) {
344 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
345 "EC SMBus adapter: Failed to register adapter\n"));
351 ec_hc->smbus = smbus;
353 printk(KERN_INFO PREFIX "%s [%s]\n",
354 acpi_device_name(device), acpi_device_bid(device));
359 static int acpi_ec_hc_remove(struct acpi_device *device, int type)
361 struct acpi_ec_hc *ec_hc;
366 ec_hc = acpi_driver_data(device);
368 i2c_del_adapter(&ec_hc->smbus->adapter);
375 static int __init acpi_ec_hc_init(void)
379 result = acpi_bus_register_driver(&acpi_ec_hc_driver);
386 static void __exit acpi_ec_hc_exit(void)
388 acpi_bus_unregister_driver(&acpi_ec_hc_driver);
391 struct acpi_ec_hc *acpi_get_ec_hc(struct acpi_device *device)
393 return acpi_driver_data(device->parent);
396 EXPORT_SYMBOL(acpi_get_ec_hc);
398 module_init(acpi_ec_hc_init);
399 module_exit(acpi_ec_hc_exit);
401 MODULE_LICENSE("GPL");
402 MODULE_AUTHOR("Ducrot Bruno");
403 MODULE_DESCRIPTION("ACPI EC SMBus driver");