ACPI: EC: Replace broken controller workarounds with poll mode.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / ec.c
CommitLineData
1da177e4 1/*
01f22462 2 * ec.c - ACPI Embedded Controller Driver (v2.0)
1da177e4 3 *
01f22462
AS
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
1da177e4
LT
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 */
28
d772b3b3
MN
29/* Uncomment next line to get verbose print outs*/
30/* #define DEBUG */
31
1da177e4
LT
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/delay.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
451566f4 39#include <linux/interrupt.h>
837012ed 40#include <linux/list.h>
1da177e4
LT
41#include <asm/io.h>
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44#include <acpi/actypes.h>
45
1da177e4 46#define ACPI_EC_CLASS "embedded_controller"
1da177e4
LT
47#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
837012ed 49
af3fd140
AS
50#undef PREFIX
51#define PREFIX "ACPI: EC: "
4350933a 52
703959d4 53/* EC status register */
1da177e4
LT
54#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
451566f4 56#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
1da177e4 57#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
4350933a 58
703959d4 59/* EC commands */
3261ff4d 60enum ec_command {
6ccedb10
AS
61 ACPI_EC_COMMAND_READ = 0x80,
62 ACPI_EC_COMMAND_WRITE = 0x81,
63 ACPI_EC_BURST_ENABLE = 0x82,
64 ACPI_EC_BURST_DISABLE = 0x83,
65 ACPI_EC_COMMAND_QUERY = 0x84,
3261ff4d 66};
837012ed 67
703959d4 68/* EC events */
3261ff4d 69enum ec_event {
703959d4 70 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
080e412c 71 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
703959d4
DS
72};
73
5c406412 74#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
703959d4 75#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
e6e82a30 76#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
703959d4 77
080e412c
AS
78enum {
79 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
80 EC_FLAGS_QUERY_PENDING, /* Query is pending */
7843932a 81 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
b77d81b2 82 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
845625cd 83 EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
080e412c
AS
84};
85
50526df6
LB
86static int acpi_ec_remove(struct acpi_device *device, int type);
87static int acpi_ec_start(struct acpi_device *device);
88static int acpi_ec_stop(struct acpi_device *device, int type);
703959d4 89static int acpi_ec_add(struct acpi_device *device);
1da177e4 90
1ba90e3a
TR
91static const struct acpi_device_id ec_device_ids[] = {
92 {"PNP0C09", 0},
93 {"", 0},
94};
95
1da177e4 96static struct acpi_driver acpi_ec_driver = {
c2b6705b 97 .name = "ec",
50526df6 98 .class = ACPI_EC_CLASS,
1ba90e3a 99 .ids = ec_device_ids,
50526df6 100 .ops = {
703959d4 101 .add = acpi_ec_add,
50526df6
LB
102 .remove = acpi_ec_remove,
103 .start = acpi_ec_start,
104 .stop = acpi_ec_stop,
105 },
1da177e4 106};
6ffb221a
DS
107
108/* If we find an EC via the ECDT, we need to keep a ptr to its context */
d033879c 109/* External interfaces use first EC only, so remember */
837012ed
AS
110typedef int (*acpi_ec_query_func) (void *data);
111
112struct acpi_ec_query_handler {
113 struct list_head node;
114 acpi_ec_query_func func;
115 acpi_handle handle;
116 void *data;
117 u8 query_bit;
118};
119
a854e08a 120static struct acpi_ec {
703959d4 121 acpi_handle handle;
a86e2772 122 unsigned long gpe;
6ffb221a
DS
123 unsigned long command_addr;
124 unsigned long data_addr;
703959d4 125 unsigned long global_lock;
080e412c 126 unsigned long flags;
c787a855 127 struct mutex lock;
703959d4 128 wait_queue_head_t wait;
837012ed 129 struct list_head list;
845625cd 130 struct delayed_work work;
4c611060 131 u8 handlers_installed;
d033879c 132} *boot_ec, *first_ec;
703959d4 133
1da177e4
LT
134/* --------------------------------------------------------------------------
135 Transaction Management
136 -------------------------------------------------------------------------- */
137
6ffb221a 138static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
1da177e4 139{
3ebe08a7 140 u8 x = inb(ec->command_addr);
86dae015 141 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
3ebe08a7 142 return x;
451566f4
DT
143}
144
6ffb221a 145static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
703959d4 146{
3ebe08a7 147 u8 x = inb(ec->data_addr);
86dae015 148 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
6ffb221a 149 return inb(ec->data_addr);
7c6db5e5
DS
150}
151
6ffb221a 152static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
45bea155 153{
86dae015 154 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
6ffb221a 155 outb(command, ec->command_addr);
45bea155
LY
156}
157
6ffb221a 158static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
45bea155 159{
86dae015 160 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
6ffb221a 161 outb(data, ec->data_addr);
703959d4 162}
45bea155 163
080e412c 164static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
6ffb221a 165{
080e412c 166 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
9e197219 167 return 0;
78d0af33 168 if (event == ACPI_EC_EVENT_OBF_1) {
080e412c 169 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
703959d4 170 return 1;
78d0af33 171 } else if (event == ACPI_EC_EVENT_IBF_0) {
080e412c 172 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
703959d4 173 return 1;
45bea155
LY
174 }
175
703959d4 176 return 0;
45bea155 177}
451566f4 178
845625cd
AS
179static void ec_schedule_ec_poll(struct acpi_ec *ec)
180{
181 if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
182 schedule_delayed_work(&ec->work,
183 msecs_to_jiffies(ACPI_EC_DELAY));
184}
185
186static void ec_switch_to_poll_mode(struct acpi_ec *ec)
187{
b77d81b2 188 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
845625cd
AS
189 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
190 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
191 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
192}
193
080e412c 194static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
7c6db5e5 195{
7843932a
AS
196 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
197 likely(!force_poll)) {
080e412c
AS
198 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
199 msecs_to_jiffies(ACPI_EC_DELAY)))
b77d81b2 200 return 0;
080e412c
AS
201 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
202 if (acpi_ec_check_status(ec, event)) {
b77d81b2
AS
203 /* missing GPEs, switch back to poll mode */
204 if (printk_ratelimit())
205 pr_info(PREFIX "missing confirmations, "
3ebe08a7 206 "switch off interrupt mode.\n");
b77d81b2
AS
207 ec_switch_to_poll_mode(ec);
208 ec_schedule_ec_poll(ec);
209 return 0;
703959d4 210 }
7843932a
AS
211 } else {
212 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
213 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
214 while (time_before(jiffies, delay)) {
215 if (acpi_ec_check_status(ec, event))
b77d81b2 216 return 0;
e6e82a30 217 udelay(ACPI_EC_UDELAY);
7843932a 218 }
af3fd140 219 }
dc0e8490
AS
220 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
221 acpi_ec_read_status(ec),
222 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
b77d81b2 223 return -ETIME;
1da177e4
LT
224}
225
703959d4 226static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
6ccedb10 227 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
228 u8 * rdata, unsigned rdata_len,
229 int force_poll)
45bea155 230{
af3fd140 231 int result = 0;
080e412c 232 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
3ebe08a7 233 pr_debug(PREFIX "transaction start\n");
dc0e8490 234 acpi_ec_write_cmd(ec, command);
78d0af33 235 for (; wdata_len > 0; --wdata_len) {
080e412c 236 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
af3fd140 237 if (result) {
3ebe08a7 238 pr_err(PREFIX
6ccedb10 239 "write_cmd timeout, command = %d\n", command);
af3fd140
AS
240 goto end;
241 }
080e412c 242 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
703959d4 243 acpi_ec_write_data(ec, *(wdata++));
3576cf61 244 }
45bea155 245
d91df1aa 246 if (!rdata_len) {
080e412c 247 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
af3fd140 248 if (result) {
3ebe08a7 249 pr_err(PREFIX
6ccedb10 250 "finish-write timeout, command = %d\n", command);
af3fd140
AS
251 goto end;
252 }
080e412c
AS
253 } else if (command == ACPI_EC_COMMAND_QUERY)
254 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
45bea155 255
78d0af33 256 for (; rdata_len > 0; --rdata_len) {
080e412c 257 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
af3fd140 258 if (result) {
3ebe08a7 259 pr_err(PREFIX "read timeout, command = %d\n", command);
af3fd140
AS
260 goto end;
261 }
0c5d31f4
AS
262 /* Don't expect GPE after last read */
263 if (rdata_len > 1)
264 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
6ffb221a 265 *(rdata++) = acpi_ec_read_data(ec);
7c6db5e5 266 }
af3fd140 267 end:
3ebe08a7 268 pr_debug(PREFIX "transaction end\n");
af3fd140 269 return result;
45bea155
LY
270}
271
3576cf61 272static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
6ccedb10 273 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
274 u8 * rdata, unsigned rdata_len,
275 int force_poll)
1da177e4 276{
d7a76e4c 277 int status;
50526df6 278 u32 glk;
1da177e4 279
d7a76e4c 280 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
d550d98d 281 return -EINVAL;
1da177e4 282
6ccedb10
AS
283 if (rdata)
284 memset(rdata, 0, rdata_len);
1da177e4 285
523953b4 286 mutex_lock(&ec->lock);
703959d4 287 if (ec->global_lock) {
1da177e4 288 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
c24e912b
AS
289 if (ACPI_FAILURE(status)) {
290 mutex_unlock(&ec->lock);
d550d98d 291 return -ENODEV;
c24e912b 292 }
1da177e4 293 }
451566f4 294
080e412c 295 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
716e084e 296 if (status) {
3ebe08a7
MN
297 pr_err(PREFIX "input buffer is not empty, "
298 "aborting transaction\n");
451566f4 299 goto end;
716e084e 300 }
1da177e4 301
6ccedb10
AS
302 status = acpi_ec_transaction_unlocked(ec, command,
303 wdata, wdata_len,
00eb43a1
LP
304 rdata, rdata_len,
305 force_poll);
1da177e4 306
6ccedb10 307 end:
1da177e4 308
703959d4 309 if (ec->global_lock)
1da177e4 310 acpi_release_global_lock(glk);
523953b4 311 mutex_unlock(&ec->lock);
1da177e4 312
d550d98d 313 return status;
1da177e4
LT
314}
315
c45aac43
AS
316/*
317 * Note: samsung nv5000 doesn't work with ec burst mode.
318 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
319 */
320int acpi_ec_burst_enable(struct acpi_ec *ec)
321{
322 u8 d;
00eb43a1 323 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
c45aac43
AS
324}
325
326int acpi_ec_burst_disable(struct acpi_ec *ec)
327{
00eb43a1 328 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
c45aac43
AS
329}
330
6ccedb10 331static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
3576cf61
DS
332{
333 int result;
334 u8 d;
335
336 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
00eb43a1 337 &address, 1, &d, 1, 0);
3576cf61
DS
338 *data = d;
339 return result;
340}
6ffb221a 341
3576cf61
DS
342static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
343{
6ccedb10
AS
344 u8 wdata[2] = { address, data };
345 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
00eb43a1 346 wdata, 2, NULL, 0, 0);
3576cf61
DS
347}
348
1da177e4
LT
349/*
350 * Externally callable EC access functions. For now, assume 1 EC only
351 */
c45aac43
AS
352int ec_burst_enable(void)
353{
c45aac43
AS
354 if (!first_ec)
355 return -ENODEV;
d033879c 356 return acpi_ec_burst_enable(first_ec);
c45aac43
AS
357}
358
359EXPORT_SYMBOL(ec_burst_enable);
360
361int ec_burst_disable(void)
362{
c45aac43
AS
363 if (!first_ec)
364 return -ENODEV;
d033879c 365 return acpi_ec_burst_disable(first_ec);
c45aac43
AS
366}
367
368EXPORT_SYMBOL(ec_burst_disable);
369
6ccedb10 370int ec_read(u8 addr, u8 * val)
1da177e4 371{
1da177e4 372 int err;
6ffb221a 373 u8 temp_data;
1da177e4
LT
374
375 if (!first_ec)
376 return -ENODEV;
377
d033879c 378 err = acpi_ec_read(first_ec, addr, &temp_data);
1da177e4
LT
379
380 if (!err) {
381 *val = temp_data;
382 return 0;
50526df6 383 } else
1da177e4
LT
384 return err;
385}
50526df6 386
1da177e4
LT
387EXPORT_SYMBOL(ec_read);
388
50526df6 389int ec_write(u8 addr, u8 val)
1da177e4 390{
1da177e4
LT
391 int err;
392
393 if (!first_ec)
394 return -ENODEV;
395
d033879c 396 err = acpi_ec_write(first_ec, addr, val);
1da177e4
LT
397
398 return err;
399}
50526df6 400
1da177e4
LT
401EXPORT_SYMBOL(ec_write);
402
616362de 403int ec_transaction(u8 command,
9e197219 404 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
405 u8 * rdata, unsigned rdata_len,
406 int force_poll)
45bea155 407{
d7a76e4c
LP
408 if (!first_ec)
409 return -ENODEV;
45bea155 410
d033879c 411 return acpi_ec_transaction(first_ec, command, wdata,
00eb43a1
LP
412 wdata_len, rdata, rdata_len,
413 force_poll);
45bea155 414}
1da177e4 415
ab9e43c6
LP
416EXPORT_SYMBOL(ec_transaction);
417
6ccedb10 418static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
3576cf61
DS
419{
420 int result;
6ccedb10 421 u8 d;
1da177e4 422
6ccedb10
AS
423 if (!ec || !data)
424 return -EINVAL;
1da177e4 425
6ccedb10
AS
426 /*
427 * Query the EC to find out which _Qxx method we need to evaluate.
428 * Note that successful completion of the query causes the ACPI_EC_SCI
429 * bit to be cleared (and thus clearing the interrupt source).
430 */
716e084e 431
00eb43a1 432 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
6ccedb10
AS
433 if (result)
434 return result;
1da177e4 435
6ccedb10
AS
436 if (!d)
437 return -ENODATA;
1da177e4 438
6ccedb10
AS
439 *data = d;
440 return 0;
1da177e4
LT
441}
442
1da177e4
LT
443/* --------------------------------------------------------------------------
444 Event Management
445 -------------------------------------------------------------------------- */
837012ed
AS
446int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
447 acpi_handle handle, acpi_ec_query_func func,
448 void *data)
449{
450 struct acpi_ec_query_handler *handler =
451 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
452 if (!handler)
453 return -ENOMEM;
454
455 handler->query_bit = query_bit;
456 handler->handle = handle;
457 handler->func = func;
458 handler->data = data;
459 mutex_lock(&ec->lock);
30c08574 460 list_add(&handler->node, &ec->list);
837012ed
AS
461 mutex_unlock(&ec->lock);
462 return 0;
463}
464
465EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
466
467void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
468{
1544fdbc 469 struct acpi_ec_query_handler *handler, *tmp;
837012ed 470 mutex_lock(&ec->lock);
1544fdbc 471 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed
AS
472 if (query_bit == handler->query_bit) {
473 list_del(&handler->node);
474 kfree(handler);
837012ed
AS
475 }
476 }
477 mutex_unlock(&ec->lock);
478}
479
480EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
1da177e4 481
50526df6 482static void acpi_ec_gpe_query(void *ec_cxt)
45bea155 483{
3d02b90b 484 struct acpi_ec *ec = ec_cxt;
6ffb221a 485 u8 value = 0;
837012ed 486 struct acpi_ec_query_handler *handler, copy;
45bea155 487
5d0c288b 488 if (!ec || acpi_ec_query(ec, &value))
e41334c0 489 return;
837012ed
AS
490 mutex_lock(&ec->lock);
491 list_for_each_entry(handler, &ec->list, node) {
492 if (value == handler->query_bit) {
493 /* have custom handler for this bit */
494 memcpy(&copy, handler, sizeof(copy));
495 mutex_unlock(&ec->lock);
496 if (copy.func) {
497 copy.func(copy.data);
498 } else if (copy.handle) {
499 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
500 }
501 return;
502 }
503 }
504 mutex_unlock(&ec->lock);
45bea155 505}
1da177e4 506
50526df6 507static u32 acpi_ec_gpe_handler(void *data)
1da177e4 508{
50526df6 509 acpi_status status = AE_OK;
3d02b90b 510 struct acpi_ec *ec = data;
845625cd 511 u8 state = acpi_ec_read_status(ec);
00eb43a1 512
3ebe08a7 513 pr_debug(PREFIX "~~~> interrupt\n");
080e412c 514 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
7843932a 515 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
af3fd140 516 wake_up(&ec->wait);
451566f4 517
845625cd 518 if (state & ACPI_EC_FLAG_SCI) {
080e412c
AS
519 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
520 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
521 acpi_ec_gpe_query, ec);
845625cd 522 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
b77d81b2 523 !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
845625cd 524 in_interrupt()) {
95b937e3 525 /* this is non-query, must be confirmation */
3ebe08a7
MN
526 if (printk_ratelimit())
527 pr_info(PREFIX "non-query interrupt received,"
528 " switching to interrupt mode\n");
7843932a 529 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
845625cd 530 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
95b937e3 531 }
845625cd 532 ec_schedule_ec_poll(ec);
7843932a 533 return ACPI_SUCCESS(status) ?
50526df6 534 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
1da177e4
LT
535}
536
845625cd
AS
537static void do_ec_poll(struct work_struct *work)
538{
539 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
540 (void)acpi_ec_gpe_handler(ec);
541}
542
1da177e4
LT
543/* --------------------------------------------------------------------------
544 Address Space Management
545 -------------------------------------------------------------------------- */
546
547static acpi_status
50526df6
LB
548acpi_ec_space_setup(acpi_handle region_handle,
549 u32 function, void *handler_context, void **return_context)
1da177e4
LT
550{
551 /*
552 * The EC object is in the handler context and is needed
553 * when calling the acpi_ec_space_handler.
554 */
50526df6
LB
555 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
556 handler_context : NULL;
1da177e4
LT
557
558 return AE_OK;
559}
560
1da177e4 561static acpi_status
5b7734b4
AS
562acpi_ec_space_handler(u32 function, acpi_physical_address address,
563 u32 bits, acpi_integer *value,
50526df6 564 void *handler_context, void *region_context)
1da177e4 565{
3d02b90b 566 struct acpi_ec *ec = handler_context;
3e71a87d 567 int result = 0, i;
5b7734b4 568 u8 temp = 0;
1da177e4 569
1da177e4 570 if ((address > 0xFF) || !value || !handler_context)
d550d98d 571 return AE_BAD_PARAMETER;
1da177e4 572
5b7734b4 573 if (function != ACPI_READ && function != ACPI_WRITE)
d550d98d 574 return AE_BAD_PARAMETER;
1da177e4 575
5b7734b4
AS
576 if (bits != 8 && acpi_strict)
577 return AE_BAD_PARAMETER;
1da177e4 578
b3b233c7
AS
579 acpi_ec_burst_enable(ec);
580
3e71a87d
AS
581 if (function == ACPI_READ) {
582 result = acpi_ec_read(ec, address, &temp);
583 *value = temp;
584 } else {
585 temp = 0xff & (*value);
586 result = acpi_ec_write(ec, address, temp);
587 }
588
589 for (i = 8; unlikely(bits - i > 0); i += 8) {
590 ++address;
5b7734b4
AS
591 if (function == ACPI_READ) {
592 result = acpi_ec_read(ec, address, &temp);
593 (*value) |= ((acpi_integer)temp) << i;
594 } else {
595 temp = 0xff & ((*value) >> i);
596 result = acpi_ec_write(ec, address, temp);
597 }
1da177e4
LT
598 }
599
b3b233c7
AS
600 acpi_ec_burst_disable(ec);
601
1da177e4
LT
602 switch (result) {
603 case -EINVAL:
d550d98d 604 return AE_BAD_PARAMETER;
1da177e4
LT
605 break;
606 case -ENODEV:
d550d98d 607 return AE_NOT_FOUND;
1da177e4
LT
608 break;
609 case -ETIME:
d550d98d 610 return AE_TIME;
1da177e4
LT
611 break;
612 default:
d550d98d 613 return AE_OK;
1da177e4 614 }
1da177e4
LT
615}
616
1da177e4
LT
617/* --------------------------------------------------------------------------
618 FS Interface (/proc)
619 -------------------------------------------------------------------------- */
620
50526df6 621static struct proc_dir_entry *acpi_ec_dir;
1da177e4 622
50526df6 623static int acpi_ec_read_info(struct seq_file *seq, void *offset)
1da177e4 624{
3d02b90b 625 struct acpi_ec *ec = seq->private;
1da177e4 626
1da177e4
LT
627 if (!ec)
628 goto end;
629
01f22462
AS
630 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
631 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
632 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
633 seq_printf(seq, "use global lock:\t%s\n",
703959d4 634 ec->global_lock ? "yes" : "no");
50526df6 635 end:
d550d98d 636 return 0;
1da177e4
LT
637}
638
639static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
640{
641 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
642}
643
703959d4 644static struct file_operations acpi_ec_info_ops = {
50526df6
LB
645 .open = acpi_ec_info_open_fs,
646 .read = seq_read,
647 .llseek = seq_lseek,
648 .release = single_release,
1da177e4
LT
649 .owner = THIS_MODULE,
650};
651
50526df6 652static int acpi_ec_add_fs(struct acpi_device *device)
1da177e4 653{
50526df6 654 struct proc_dir_entry *entry = NULL;
1da177e4 655
1da177e4
LT
656 if (!acpi_device_dir(device)) {
657 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
50526df6 658 acpi_ec_dir);
1da177e4 659 if (!acpi_device_dir(device))
d550d98d 660 return -ENODEV;
1da177e4
LT
661 }
662
663 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
50526df6 664 acpi_device_dir(device));
1da177e4 665 if (!entry)
d550d98d 666 return -ENODEV;
1da177e4
LT
667 else {
668 entry->proc_fops = &acpi_ec_info_ops;
669 entry->data = acpi_driver_data(device);
670 entry->owner = THIS_MODULE;
671 }
672
d550d98d 673 return 0;
1da177e4
LT
674}
675
50526df6 676static int acpi_ec_remove_fs(struct acpi_device *device)
1da177e4 677{
1da177e4
LT
678
679 if (acpi_device_dir(device)) {
680 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
681 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
682 acpi_device_dir(device) = NULL;
683 }
684
d550d98d 685 return 0;
1da177e4
LT
686}
687
1da177e4
LT
688/* --------------------------------------------------------------------------
689 Driver Interface
690 -------------------------------------------------------------------------- */
c0900c35
AS
691static acpi_status
692ec_parse_io_ports(struct acpi_resource *resource, void *context);
693
c0900c35
AS
694static struct acpi_ec *make_acpi_ec(void)
695{
696 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
697 if (!ec)
698 return NULL;
080e412c 699 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
c0900c35
AS
700 mutex_init(&ec->lock);
701 init_waitqueue_head(&ec->wait);
837012ed 702 INIT_LIST_HEAD(&ec->list);
845625cd 703 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
c0900c35
AS
704 return ec;
705}
837012ed 706
c019b193
AS
707static acpi_status
708acpi_ec_register_query_methods(acpi_handle handle, u32 level,
709 void *context, void **return_value)
710{
711 struct acpi_namespace_node *node = handle;
712 struct acpi_ec *ec = context;
713 int value = 0;
714 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
715 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
716 }
717 return AE_OK;
718}
719
cd8c93a4
AS
720static acpi_status
721ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
837012ed 722{
cd8c93a4
AS
723 acpi_status status;
724
725 struct acpi_ec *ec = context;
726 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
727 ec_parse_io_ports, ec);
728 if (ACPI_FAILURE(status))
729 return status;
837012ed
AS
730
731 /* Get GPE bit assignment (EC events). */
732 /* TODO: Add support for _GPE returning a package */
cd8c93a4
AS
733 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
734 if (ACPI_FAILURE(status))
735 return status;
c019b193
AS
736 /* Find and register all query methods */
737 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
738 acpi_ec_register_query_methods, ec, NULL);
837012ed
AS
739 /* Use the global lock for all EC transactions? */
740 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
837012ed 741 ec->handle = handle;
cd8c93a4 742 return AE_CTRL_TERMINATE;
837012ed
AS
743}
744
845625cd
AS
745static void ec_poll_stop(struct acpi_ec *ec)
746{
747 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
748 cancel_delayed_work(&ec->work);
749}
750
4c611060
AS
751static void ec_remove_handlers(struct acpi_ec *ec)
752{
845625cd 753 ec_poll_stop(ec);
4c611060
AS
754 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
755 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
3ebe08a7 756 pr_err(PREFIX "failed to remove space handler\n");
4c611060
AS
757 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
758 &acpi_ec_gpe_handler)))
3ebe08a7 759 pr_err(PREFIX "failed to remove gpe handler\n");
4c611060
AS
760 ec->handlers_installed = 0;
761}
762
703959d4 763static int acpi_ec_add(struct acpi_device *device)
1da177e4 764{
703959d4 765 struct acpi_ec *ec = NULL;
45bea155 766
45bea155 767 if (!device)
d550d98d 768 return -EINVAL;
c0900c35
AS
769 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
770 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
771
4c611060
AS
772 /* Check for boot EC */
773 if (boot_ec) {
774 if (boot_ec->handle == device->handle) {
775 /* Pre-loaded EC from DSDT, just move pointer */
776 ec = boot_ec;
777 boot_ec = NULL;
778 goto end;
779 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
780 /* ECDT-based EC, time to shut it down */
781 ec_remove_handlers(boot_ec);
782 kfree(boot_ec);
783 first_ec = boot_ec = NULL;
784 }
785 }
786
c0900c35 787 ec = make_acpi_ec();
45bea155 788 if (!ec)
d550d98d 789 return -ENOMEM;
703959d4 790
cd8c93a4
AS
791 if (ec_parse_device(device->handle, 0, ec, NULL) !=
792 AE_CTRL_TERMINATE) {
c0900c35
AS
793 kfree(ec);
794 return -EINVAL;
45bea155 795 }
c0900c35 796 ec->handle = device->handle;
4c611060
AS
797 end:
798 if (!first_ec)
799 first_ec = ec;
c0900c35 800 acpi_driver_data(device) = ec;
c0900c35 801 acpi_ec_add_fs(device);
3ebe08a7 802 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
4c611060 803 ec->gpe, ec->command_addr, ec->data_addr);
3ebe08a7 804 pr_info(PREFIX "driver started in %s mode\n",
95b937e3 805 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
c0900c35 806 return 0;
1da177e4
LT
807}
808
50526df6 809static int acpi_ec_remove(struct acpi_device *device, int type)
1da177e4 810{
01f22462 811 struct acpi_ec *ec;
07ddf768 812 struct acpi_ec_query_handler *handler, *tmp;
1da177e4 813
1da177e4 814 if (!device)
d550d98d 815 return -EINVAL;
1da177e4
LT
816
817 ec = acpi_driver_data(device);
837012ed 818 mutex_lock(&ec->lock);
07ddf768 819 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed
AS
820 list_del(&handler->node);
821 kfree(handler);
822 }
823 mutex_unlock(&ec->lock);
1da177e4 824 acpi_ec_remove_fs(device);
c0900c35 825 acpi_driver_data(device) = NULL;
d033879c 826 if (ec == first_ec)
c0900c35 827 first_ec = NULL;
4c611060 828 kfree(ec);
d550d98d 829 return 0;
1da177e4
LT
830}
831
1da177e4 832static acpi_status
c0900c35 833ec_parse_io_ports(struct acpi_resource *resource, void *context)
1da177e4 834{
3d02b90b 835 struct acpi_ec *ec = context;
1da177e4 836
01f22462 837 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1da177e4 838 return AE_OK;
1da177e4
LT
839
840 /*
841 * The first address region returned is the data port, and
842 * the second address region returned is the status/command
843 * port.
844 */
01f22462 845 if (ec->data_addr == 0)
6ffb221a 846 ec->data_addr = resource->data.io.minimum;
01f22462 847 else if (ec->command_addr == 0)
6ffb221a 848 ec->command_addr = resource->data.io.minimum;
01f22462 849 else
1da177e4 850 return AE_CTRL_TERMINATE;
1da177e4 851
1da177e4
LT
852 return AE_OK;
853}
854
e8284321
AS
855static int ec_install_handlers(struct acpi_ec *ec)
856{
c0900c35 857 acpi_status status;
4c611060
AS
858 if (ec->handlers_installed)
859 return 0;
c0900c35
AS
860 status = acpi_install_gpe_handler(NULL, ec->gpe,
861 ACPI_GPE_EDGE_TRIGGERED,
862 &acpi_ec_gpe_handler, ec);
e8284321
AS
863 if (ACPI_FAILURE(status))
864 return -ENODEV;
01f22462 865
e8284321
AS
866 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
867 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
868
869 status = acpi_install_address_space_handler(ec->handle,
870 ACPI_ADR_SPACE_EC,
871 &acpi_ec_space_handler,
872 &acpi_ec_space_setup, ec);
873 if (ACPI_FAILURE(status)) {
874 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
875 return -ENODEV;
876 }
877
4c611060 878 ec->handlers_installed = 1;
e8284321
AS
879 return 0;
880}
881
50526df6 882static int acpi_ec_start(struct acpi_device *device)
1da177e4 883{
c0900c35 884 struct acpi_ec *ec;
837012ed 885 int ret = 0;
1da177e4 886
1da177e4 887 if (!device)
d550d98d 888 return -EINVAL;
1da177e4
LT
889
890 ec = acpi_driver_data(device);
891
892 if (!ec)
d550d98d 893 return -EINVAL;
1da177e4 894
4c611060 895 ret = ec_install_handlers(ec);
837012ed
AS
896
897 /* EC is fully operational, allow queries */
080e412c 898 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
845625cd 899 ec_schedule_ec_poll(ec);
837012ed 900 return ret;
1da177e4
LT
901}
902
50526df6 903static int acpi_ec_stop(struct acpi_device *device, int type)
1da177e4 904{
c0900c35 905 struct acpi_ec *ec;
1da177e4 906 if (!device)
d550d98d 907 return -EINVAL;
1da177e4 908 ec = acpi_driver_data(device);
c0900c35
AS
909 if (!ec)
910 return -EINVAL;
4c611060 911 ec_remove_handlers(ec);
f9319f90 912
d550d98d 913 return 0;
1da177e4
LT
914}
915
c04209a7
AS
916int __init acpi_boot_ec_enable(void)
917{
918 if (!boot_ec || boot_ec->handlers_installed)
919 return 0;
920 if (!ec_install_handlers(boot_ec)) {
921 first_ec = boot_ec;
922 return 0;
923 }
924 return -EFAULT;
925}
926
c0900c35
AS
927int __init acpi_ec_ecdt_probe(void)
928{
929 int ret;
930 acpi_status status;
50526df6 931 struct acpi_table_ecdt *ecdt_ptr;
45bea155 932
d66d969d
AS
933 boot_ec = make_acpi_ec();
934 if (!boot_ec)
c0900c35
AS
935 return -ENOMEM;
936 /*
937 * Generate a boot ec context
938 */
15a58ed1
AS
939 status = acpi_get_table(ACPI_SIG_ECDT, 1,
940 (struct acpi_table_header **)&ecdt_ptr);
cd8c93a4 941 if (ACPI_SUCCESS(status)) {
3ebe08a7 942 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
cd8c93a4
AS
943 boot_ec->command_addr = ecdt_ptr->control.address;
944 boot_ec->data_addr = ecdt_ptr->data.address;
945 boot_ec->gpe = ecdt_ptr->gpe;
4af8e10a 946 boot_ec->handle = ACPI_ROOT_OBJECT;
cd8c93a4 947 } else {
5870a8cd
AS
948 /* This workaround is needed only on some broken machines,
949 * which require early EC, but fail to provide ECDT */
950 acpi_handle x;
cd8c93a4
AS
951 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
952 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
953 boot_ec, NULL);
2d8348b4
AS
954 /* Check that acpi_get_devices actually find something */
955 if (ACPI_FAILURE(status) || !boot_ec->handle)
cd8c93a4 956 goto error;
5870a8cd
AS
957 /* We really need to limit this workaround, the only ASUS,
958 * which needs it, has fake EC._INI method, so use it as flag.
c04209a7 959 * Keep boot_ec struct as it will be needed soon.
5870a8cd
AS
960 */
961 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
c04209a7 962 return -ENODEV;
cd8c93a4 963 }
1da177e4 964
d66d969d 965 ret = ec_install_handlers(boot_ec);
d033879c
AS
966 if (!ret) {
967 first_ec = boot_ec;
e8284321 968 return 0;
d033879c 969 }
c0900c35 970 error:
d66d969d
AS
971 kfree(boot_ec);
972 boot_ec = NULL;
1da177e4
LT
973 return -ENODEV;
974}
975
50526df6 976static int __init acpi_ec_init(void)
1da177e4 977{
50526df6 978 int result = 0;
1da177e4 979
1da177e4 980 if (acpi_disabled)
d550d98d 981 return 0;
1da177e4
LT
982
983 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
984 if (!acpi_ec_dir)
d550d98d 985 return -ENODEV;
1da177e4
LT
986
987 /* Now register the driver for the EC */
988 result = acpi_bus_register_driver(&acpi_ec_driver);
989 if (result < 0) {
990 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
d550d98d 991 return -ENODEV;
1da177e4
LT
992 }
993
d550d98d 994 return result;
1da177e4
LT
995}
996
997subsys_initcall(acpi_ec_init);
998
999/* EC driver currently not unloadable */
1000#if 0
50526df6 1001static void __exit acpi_ec_exit(void)
1da177e4 1002{
1da177e4
LT
1003
1004 acpi_bus_unregister_driver(&acpi_ec_driver);
1005
1006 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1007
d550d98d 1008 return;
1da177e4 1009}
7843932a 1010#endif /* 0 */