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