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