blob: 187981a3e8811b6e45c0dd812bd1725976dbecca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38#include <acpi/actypes.h>
39
40#define _COMPONENT ACPI_EC_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050041ACPI_MODULE_NAME("ec");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_EC_COMPONENT 0x00100000
43#define ACPI_EC_CLASS "embedded_controller"
44#define ACPI_EC_HID "PNP0C09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_EC_DEVICE_NAME "Embedded Controller"
46#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030047#undef PREFIX
48#define PREFIX "ACPI: EC: "
Denis M. Sadykov703959d2006-09-26 19:50:33 +040049/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050052#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040054/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030055enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030056 ACPI_EC_COMMAND_READ = 0x80,
57 ACPI_EC_COMMAND_WRITE = 0x81,
58 ACPI_EC_BURST_ENABLE = 0x82,
59 ACPI_EC_BURST_DISABLE = 0x83,
60 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030061};
Denis M. Sadykov703959d2006-09-26 19:50:33 +040062/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030063enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040064 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030065 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040066};
67
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030068#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030071static enum ec_mode {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030072 EC_INTR = 1, /* Output buffer full */
73 EC_POLL, /* Input buffer empty */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030074} acpi_ec_mode = EC_INTR;
Denis M. Sadykov703959d2006-09-26 19:50:33 +040075
Len Brown50526df2005-08-11 17:32:05 -040076static int acpi_ec_remove(struct acpi_device *device, int type);
77static int acpi_ec_start(struct acpi_device *device);
78static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040079static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050082 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040083 .class = ACPI_EC_CLASS,
84 .ids = ACPI_EC_HID,
85 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040086 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040087 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
89 .stop = acpi_ec_stop,
90 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040092
93/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030094/* External interfaces use first EC only, so remember */
Adrian Bunka854e082006-12-19 12:56:12 -080095static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040096 acpi_handle handle;
97 unsigned long uid;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +030098 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040099 unsigned long command_addr;
100 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400101 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300102 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300103 atomic_t query_pending;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500104 atomic_t event_count;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400105 wait_queue_head_t wait;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300106} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* --------------------------------------------------------------------------
109 Transaction Management
110 -------------------------------------------------------------------------- */
111
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400112static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400114 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400117static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400119 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400120}
121
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400122static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400124 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400125}
126
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400127static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400128{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400129 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130}
131
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500132static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
133 unsigned old_count)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400134{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300135 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500136 if (old_count == atomic_read(&ec->event_count))
137 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300138 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400139 if (status & ACPI_EC_FLAG_OBF)
140 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300141 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400142 if (!(status & ACPI_EC_FLAG_IBF))
143 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400144 }
145
146 return 0;
147}
148
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500149static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
Luming Yu45bea152005-07-23 04:08:00 -0400150{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300151 if (acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300152 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
153 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500154 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400155 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400156 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300157 } else {
158 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500159 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300160 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500161 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400162 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300163 } else {
164 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
165 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300166 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400167 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300168 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400169
Patrick Mocheld550d982006-06-27 00:41:40 -0400170 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500171}
172
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300174 const u8 * wdata, unsigned wdata_len,
175 u8 * rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400176{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300177 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500178 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400179 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400180
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300181 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500182 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300183 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300184 printk(KERN_ERR PREFIX
185 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300186 goto end;
187 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500188 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400189 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400190 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400191
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300192 if (!rdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500193 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300194 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300195 printk(KERN_ERR PREFIX
196 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300197 goto end;
198 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300199 } else if (command == ACPI_EC_COMMAND_QUERY) {
200 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400201 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400202
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300203 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500204 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300205 if (result) {
206 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300207 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300208 goto end;
209 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500210 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400211 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400212 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300213 end:
214 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400215}
216
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400217static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300218 const u8 * wdata, unsigned wdata_len,
219 u8 * rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400220{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400221 int status;
Len Brown50526df2005-08-11 17:32:05 -0400222 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400224 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300227 if (rdata)
228 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300230 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400231 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300233 if (ACPI_FAILURE(status)) {
234 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400235 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500238
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300239 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300240 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300241
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500242 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400243 if (status) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300244 printk(KERN_DEBUG PREFIX
245 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500246 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300249 status = acpi_ec_transaction_unlocked(ec, command,
250 wdata, wdata_len,
251 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400252
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300253 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400255 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300257 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300262/*
263 * Note: samsung nv5000 doesn't work with ec burst mode.
264 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
265 */
266int acpi_ec_burst_enable(struct acpi_ec *ec)
267{
268 u8 d;
269 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1);
270}
271
272int acpi_ec_burst_disable(struct acpi_ec *ec)
273{
274 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0);
275}
276
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300277static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400278{
279 int result;
280 u8 d;
281
282 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
283 &address, 1, &d, 1);
284 *data = d;
285 return result;
286}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400287
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400288static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
289{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300290 u8 wdata[2] = { address, data };
291 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400292 wdata, 2, NULL, 0);
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * Externally callable EC access functions. For now, assume 1 EC only
297 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300298int ec_burst_enable(void)
299{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300300 if (!first_ec)
301 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300302 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300303}
304
305EXPORT_SYMBOL(ec_burst_enable);
306
307int ec_burst_disable(void)
308{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300309 if (!first_ec)
310 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300311 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300312}
313
314EXPORT_SYMBOL(ec_burst_disable);
315
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300316int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400319 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (!first_ec)
322 return -ENODEV;
323
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300324 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (!err) {
327 *val = temp_data;
328 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400329 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return err;
331}
Len Brown50526df2005-08-11 17:32:05 -0400332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333EXPORT_SYMBOL(ec_read);
334
Len Brown50526df2005-08-11 17:32:05 -0400335int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err;
338
339 if (!first_ec)
340 return -ENODEV;
341
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300342 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 return err;
345}
Len Brown50526df2005-08-11 17:32:05 -0400346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347EXPORT_SYMBOL(ec_write);
348
Randy Dunlap616362d2006-10-27 01:47:34 -0400349int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500350 const u8 * wdata, unsigned wdata_len,
351 u8 * rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400352{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400353 if (!first_ec)
354 return -ENODEV;
355
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300356 return acpi_ec_transaction(first_ec, command, wdata,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400357 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400358}
Luming Yu45bea152005-07-23 04:08:00 -0400359
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400360EXPORT_SYMBOL(ec_transaction);
361
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300362static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400363{
364 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300365 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400366
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300367 if (!ec || !data)
368 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400369
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300370 /*
371 * Query the EC to find out which _Qxx method we need to evaluate.
372 * Note that successful completion of the query causes the ACPI_EC_SCI
373 * bit to be cleared (and thus clearing the interrupt source).
374 */
Luming Yu45bea152005-07-23 04:08:00 -0400375
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300376 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
377 if (result)
378 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400379
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300380 if (!d)
381 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400382
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300383 *data = d;
384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/* --------------------------------------------------------------------------
388 Event Management
389 -------------------------------------------------------------------------- */
390
Len Brown50526df2005-08-11 17:32:05 -0400391static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300393 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400394 u8 value = 0;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300395 char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400396
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300397 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300398 return;
Luming Yu45bea152005-07-23 04:08:00 -0400399
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400400 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400401
Guillaume Chazarainc6e19192006-12-24 22:19:02 +0100402 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
Luming Yu45bea152005-07-23 04:08:00 -0400403
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400404 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Len Brown50526df2005-08-11 17:32:05 -0400407static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Len Brown50526df2005-08-11 17:32:05 -0400409 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400410 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300411 struct acpi_ec *ec = data;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500412 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300413
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400414 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300415 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500416 }
417
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300418 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300419 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
420 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300421 status =
422 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
423 ec);
Len Brown50526df2005-08-11 17:32:05 -0400424 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300425
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500426 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400427 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/* --------------------------------------------------------------------------
431 Address Space Management
432 -------------------------------------------------------------------------- */
433
434static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400435acpi_ec_space_setup(acpi_handle region_handle,
436 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 /*
439 * The EC object is in the handler context and is needed
440 * when calling the acpi_ec_space_handler.
441 */
Len Brown50526df2005-08-11 17:32:05 -0400442 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
443 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return AE_OK;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400449acpi_ec_space_handler(u32 function,
450 acpi_physical_address address,
451 u32 bit_width,
452 acpi_integer * value,
453 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Len Brown50526df2005-08-11 17:32:05 -0400455 int result = 0;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300456 struct acpi_ec *ec = handler_context;
Len Brown50526df2005-08-11 17:32:05 -0400457 u64 temp = *value;
458 acpi_integer f_v = 0;
459 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Luming Yufa9cd542005-03-19 01:54:47 -0500464 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
Len Brown50526df2005-08-11 17:32:05 -0400468 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 switch (function) {
470 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500471 temp = 0;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300472 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500475 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 default:
478 result = -EINVAL;
479 goto out;
480 break;
481 }
482
483 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500484 if (bit_width) {
485 if (function == ACPI_READ)
486 f_v |= temp << 8 * i;
487 if (function == ACPI_WRITE)
488 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500490 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto next_byte;
492 }
493
Luming Yufa9cd542005-03-19 01:54:47 -0500494 if (function == ACPI_READ) {
495 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *value = f_v;
497 }
498
Len Brown50526df2005-08-11 17:32:05 -0400499 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 switch (result) {
501 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400502 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400505 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400508 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/* --------------------------------------------------------------------------
516 FS Interface (/proc)
517 -------------------------------------------------------------------------- */
518
Len Brown50526df2005-08-11 17:32:05 -0400519static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Len Brown50526df2005-08-11 17:32:05 -0400521static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300523 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!ec)
526 goto end;
527
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300528 seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300530 (u32) ec->command_addr, (u32) ec->data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400532 ec->global_lock ? "yes" : "no");
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300533 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Len Brown50526df2005-08-11 17:32:05 -0400535 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
540{
541 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
542}
543
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400544static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400545 .open = acpi_ec_info_open_fs,
546 .read = seq_read,
547 .llseek = seq_lseek,
548 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .owner = THIS_MODULE,
550};
551
Len Brown50526df2005-08-11 17:32:05 -0400552static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Len Brown50526df2005-08-11 17:32:05 -0400554 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!acpi_device_dir(device)) {
557 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400558 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400564 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 else {
568 entry->proc_fops = &acpi_ec_info_ops;
569 entry->data = acpi_driver_data(device);
570 entry->owner = THIS_MODULE;
571 }
572
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Len Brown50526df2005-08-11 17:32:05 -0400576static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (acpi_device_dir(device)) {
580 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
581 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
582 acpi_device_dir(device) = NULL;
583 }
584
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/* --------------------------------------------------------------------------
589 Driver Interface
590 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300591static acpi_status
592ec_parse_io_ports(struct acpi_resource *resource, void *context);
593
594static acpi_status
595ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
596
597static struct acpi_ec *make_acpi_ec(void)
598{
599 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
600 if (!ec)
601 return NULL;
602
603 atomic_set(&ec->query_pending, 0);
604 atomic_set(&ec->event_count, 1);
605 mutex_init(&ec->lock);
606 init_waitqueue_head(&ec->wait);
607
608 return ec;
609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400611static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Len Brown50526df2005-08-11 17:32:05 -0400613 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400614 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300619 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
620 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
621
622 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400624 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300626 status = ec_parse_device(device->handle, 0, ec, NULL);
627 if (status != AE_CTRL_TERMINATE) {
628 kfree(ec);
629 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400630 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300631
632 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300633 if (boot_ec) {
634 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300635 /* We might have incorrect info for GL at boot time */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300636 mutex_lock(&boot_ec->lock);
637 boot_ec->global_lock = ec->global_lock;
638 mutex_unlock(&boot_ec->lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300639 kfree(ec);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300640 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300641 }
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300642 } else
643 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300644 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647 acpi_ec_add_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400649 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300650 acpi_device_name(device), acpi_device_bid(device),
651 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400652
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Len Brown50526df2005-08-11 17:32:05 -0400656static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400658 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 ec = acpi_driver_data(device);
664
665 acpi_ec_remove_fs(device);
666
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300667 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300668 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300669 first_ec = NULL;
670
671 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300672 if (boot_ec != ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300673 kfree(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Patrick Mocheld550d982006-06-27 00:41:40 -0400675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300679ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300681 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Bob Moore50eca3e2005-09-30 19:03:00 -0400683 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return AE_OK;
685 }
686
687 /*
688 * The first address region returned is the data port, and
689 * the second address region returned is the status/command
690 * port.
691 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400692 if (ec->data_addr == 0) {
693 ec->data_addr = resource->data.io.minimum;
694 } else if (ec->command_addr == 0) {
695 ec->command_addr = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 } else {
697 return AE_CTRL_TERMINATE;
698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return AE_OK;
701}
702
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300703static int ec_install_handlers(struct acpi_ec *ec)
704{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300705 acpi_status status;
706 status = acpi_install_gpe_handler(NULL, ec->gpe,
707 ACPI_GPE_EDGE_TRIGGERED,
708 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300709 if (ACPI_FAILURE(status))
710 return -ENODEV;
711 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
712 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
713
714 status = acpi_install_address_space_handler(ec->handle,
715 ACPI_ADR_SPACE_EC,
716 &acpi_ec_space_handler,
717 &acpi_ec_space_setup, ec);
718 if (ACPI_FAILURE(status)) {
719 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
720 return -ENODEV;
721 }
722
723 return 0;
724}
725
Len Brown50526df2005-08-11 17:32:05 -0400726static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300728 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400731 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 ec = acpi_driver_data(device);
734
735 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400738 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300739 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 /* Boot EC is already working */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300742 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300743 return 0;
744
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300745 return ec_install_handlers(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Len Brown50526df2005-08-11 17:32:05 -0400748static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300750 acpi_status status;
751 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300757 if (!ec)
758 return -EINVAL;
759
760 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300761 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400764 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400765 ACPI_ADR_SPACE_EC,
766 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400768 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300770 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Patrick Mocheld550d982006-06-27 00:41:40 -0400774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300777static acpi_status
778ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Len Brown50526df2005-08-11 17:32:05 -0400780 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -0400781
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300782 struct acpi_ec *ec = context;
783 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
784 ec_parse_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400785 if (ACPI_FAILURE(status))
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300786 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400787
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300788 /* Get GPE bit assignment (EC events). */
789 /* TODO: Add support for _GPE returning a package */
790 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
791 if (ACPI_FAILURE(status))
792 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400793
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300794 /* Use the global lock for all EC transactions? */
795 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Luming Yu45bea152005-07-23 04:08:00 -0400796
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300797 ec->handle = handle;
Luming Yu45bea152005-07-23 04:08:00 -0400798
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300799 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
800 ec->gpe, ec->command_addr, ec->data_addr));
801
802 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Len Brown50526df2005-08-11 17:32:05 -0400805int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Len Brown50526df2005-08-11 17:32:05 -0400807 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300808 acpi_status status;
809 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300811 boot_ec = make_acpi_ec();
812 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300813 return -ENOMEM;
814 /*
815 * Generate a boot ec context
816 */
817
818 status = acpi_get_table(ACPI_SIG_ECDT, 1,
819 (struct acpi_table_header **)&ecdt_ptr);
820 if (ACPI_FAILURE(status))
821 goto error;
822
823 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
824
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300825 boot_ec->command_addr = ecdt_ptr->control.address;
826 boot_ec->data_addr = ecdt_ptr->data.address;
827 boot_ec->gpe = ecdt_ptr->gpe;
828 boot_ec->uid = ecdt_ptr->uid;
829 boot_ec->handle = ACPI_ROOT_OBJECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300831 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300832 if (!ret) {
833 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300834 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300835 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300836 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300837 kfree(boot_ec);
838 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return -ENODEV;
841}
842
Len Brown50526df2005-08-11 17:32:05 -0400843static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Len Brown50526df2005-08-11 17:32:05 -0400845 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
851 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* Now register the driver for the EC */
855 result = acpi_bus_register_driver(&acpi_ec_driver);
856 if (result < 0) {
857 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400858 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Patrick Mocheld550d982006-06-27 00:41:40 -0400861 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864subsys_initcall(acpi_ec_init);
865
866/* EC driver currently not unloadable */
867#if 0
Len Brown50526df2005-08-11 17:32:05 -0400868static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 acpi_bus_unregister_driver(&acpi_ec_driver);
872
873 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
874
Patrick Mocheld550d982006-06-27 00:41:40 -0400875 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Len Brown50526df2005-08-11 17:32:05 -0400877#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Len Brown02b28a32005-12-05 16:33:04 -0500879static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400880{
Len Brown02b28a32005-12-05 16:33:04 -0500881 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400882
Len Brown02b28a32005-12-05 16:33:04 -0500883 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400884 return 0;
885
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300886 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
887
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500888 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400889
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800890 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400891}
Len Brown50526df2005-08-11 17:32:05 -0400892
Len Brown53f11d42005-12-05 16:46:36 -0500893__setup("ec_intr=", acpi_ec_set_intr_mode);