blob: 5534b234509b5a31980f84d86583e85b37764a21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03004 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
33#include <linux/delay.h>
34#include <linux/proc_fs.h>
35#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050036#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/io.h>
38#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
40#include <acpi/actypes.h>
41
42#define _COMPONENT ACPI_EC_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050043ACPI_MODULE_NAME("ec");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_EC_COMPONENT 0x00100000
45#define ACPI_EC_CLASS "embedded_controller"
46#define ACPI_EC_HID "PNP0C09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030049#undef PREFIX
50#define PREFIX "ACPI: EC: "
Denis M. Sadykov703959d2006-09-26 19:50:33 +040051/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
53#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050054#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040056/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030057enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030058 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030063};
Denis M. Sadykov703959d2006-09-26 19:50:33 +040064/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030065enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040066 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030067 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040068};
69
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030070#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040071#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030073static enum ec_mode {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030074 EC_INTR = 1, /* Output buffer full */
75 EC_POLL, /* Input buffer empty */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030076} acpi_ec_mode = EC_INTR;
Denis M. Sadykov703959d2006-09-26 19:50:33 +040077
Len Brown50526df2005-08-11 17:32:05 -040078static int acpi_ec_remove(struct acpi_device *device, int type);
79static int acpi_ec_start(struct acpi_device *device);
80static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040081static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050084 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040085 .class = ACPI_EC_CLASS,
86 .ids = ACPI_EC_HID,
87 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040088 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040089 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
91 .stop = acpi_ec_stop,
92 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040094
95/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030096/* External interfaces use first EC only, so remember */
Adrian Bunka854e082006-12-19 12:56:12 -080097static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040098 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +030099 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400100 unsigned long command_addr;
101 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400102 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300103 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300104 atomic_t query_pending;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500105 atomic_t event_count;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400106 wait_queue_head_t wait;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300107} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* --------------------------------------------------------------------------
110 Transaction Management
111 -------------------------------------------------------------------------- */
112
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400113static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400115 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400118static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400119{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400120 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400121}
122
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400123static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400124{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400125 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126}
127
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400128static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400129{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400130 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400131}
132
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500133static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
134 unsigned old_count)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300136 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500137 if (old_count == atomic_read(&ec->event_count))
138 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300139 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400140 if (status & ACPI_EC_FLAG_OBF)
141 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300142 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400143 if (!(status & ACPI_EC_FLAG_IBF))
144 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400145 }
146
147 return 0;
148}
149
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200150static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
151 unsigned count, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400152{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200153 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300154 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
155 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500156 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400158 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300159 } else {
160 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500161 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300162 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500163 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400164 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300165 } else {
166 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
167 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300168 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400169 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300170 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400171
Patrick Mocheld550d982006-06-27 00:41:40 -0400172 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500173}
174
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400175static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300176 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200177 u8 * rdata, unsigned rdata_len,
178 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400179{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300180 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500181 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400182 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400183
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300184 for (; wdata_len > 0; --wdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200185 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300186 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300187 printk(KERN_ERR PREFIX
188 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300189 goto end;
190 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500191 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400192 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400193 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400194
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300195 if (!rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200196 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300197 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300198 printk(KERN_ERR PREFIX
199 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300200 goto end;
201 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300202 } else if (command == ACPI_EC_COMMAND_QUERY) {
203 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400204 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400205
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300206 for (; rdata_len > 0; --rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200207 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300208 if (result) {
209 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300210 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300211 goto end;
212 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500213 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400214 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400215 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300216 end:
217 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400218}
219
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400220static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300221 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200222 u8 * rdata, unsigned rdata_len,
223 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400224{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400225 int status;
Len Brown50526df2005-08-11 17:32:05 -0400226 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400228 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300231 if (rdata)
232 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300234 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400235 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300237 if (ACPI_FAILURE(status)) {
238 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500242
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300243 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300244 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300245
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200246 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400247 if (status) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300248 printk(KERN_DEBUG PREFIX
249 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500250 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300253 status = acpi_ec_transaction_unlocked(ec, command,
254 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200255 rdata, rdata_len,
256 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400257
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300258 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400260 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300262 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Patrick Mocheld550d982006-06-27 00:41:40 -0400264 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300267/*
268 * Note: samsung nv5000 doesn't work with ec burst mode.
269 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
270 */
271int acpi_ec_burst_enable(struct acpi_ec *ec)
272{
273 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200274 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300275}
276
277int acpi_ec_burst_disable(struct acpi_ec *ec)
278{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200279 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300280}
281
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300282static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400283{
284 int result;
285 u8 d;
286
287 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200288 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400289 *data = d;
290 return result;
291}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400292
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400293static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
294{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300295 u8 wdata[2] = { address, data };
296 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200297 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
301 * Externally callable EC access functions. For now, assume 1 EC only
302 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300303int ec_burst_enable(void)
304{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300305 if (!first_ec)
306 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300307 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300308}
309
310EXPORT_SYMBOL(ec_burst_enable);
311
312int ec_burst_disable(void)
313{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300314 if (!first_ec)
315 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300316 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300317}
318
319EXPORT_SYMBOL(ec_burst_disable);
320
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300321int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400324 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (!first_ec)
327 return -ENODEV;
328
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300329 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (!err) {
332 *val = temp_data;
333 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400334 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return err;
336}
Len Brown50526df2005-08-11 17:32:05 -0400337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338EXPORT_SYMBOL(ec_read);
339
Len Brown50526df2005-08-11 17:32:05 -0400340int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err;
343
344 if (!first_ec)
345 return -ENODEV;
346
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300347 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return err;
350}
Len Brown50526df2005-08-11 17:32:05 -0400351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352EXPORT_SYMBOL(ec_write);
353
Randy Dunlap616362d2006-10-27 01:47:34 -0400354int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500355 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200356 u8 * rdata, unsigned rdata_len,
357 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400358{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400359 if (!first_ec)
360 return -ENODEV;
361
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300362 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200363 wdata_len, rdata, rdata_len,
364 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400365}
Luming Yu45bea152005-07-23 04:08:00 -0400366
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400367EXPORT_SYMBOL(ec_transaction);
368
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300369static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400370{
371 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300372 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400373
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300374 if (!ec || !data)
375 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400376
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300377 /*
378 * Query the EC to find out which _Qxx method we need to evaluate.
379 * Note that successful completion of the query causes the ACPI_EC_SCI
380 * bit to be cleared (and thus clearing the interrupt source).
381 */
Luming Yu45bea152005-07-23 04:08:00 -0400382
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200383 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300384 if (result)
385 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400386
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387 if (!d)
388 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400389
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300390 *data = d;
391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/* --------------------------------------------------------------------------
395 Event Management
396 -------------------------------------------------------------------------- */
397
Len Brown50526df2005-08-11 17:32:05 -0400398static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300400 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400401 u8 value = 0;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300402 char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400403
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300404 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300405 return;
Luming Yu45bea152005-07-23 04:08:00 -0400406
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400407 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400408
Guillaume Chazarainc6e19192006-12-24 22:19:02 +0100409 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
Luming Yu45bea152005-07-23 04:08:00 -0400410
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400411 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Len Brown50526df2005-08-11 17:32:05 -0400414static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Len Brown50526df2005-08-11 17:32:05 -0400416 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400417 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300418 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200419
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500420 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300421
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400422 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300423 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500424 }
425
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300426 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300427 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
428 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300429 status =
430 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
431 ec);
Len Brown50526df2005-08-11 17:32:05 -0400432 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300433
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500434 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400435 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438/* --------------------------------------------------------------------------
439 Address Space Management
440 -------------------------------------------------------------------------- */
441
442static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400443acpi_ec_space_setup(acpi_handle region_handle,
444 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 /*
447 * The EC object is in the handler context and is needed
448 * when calling the acpi_ec_space_handler.
449 */
Len Brown50526df2005-08-11 17:32:05 -0400450 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
451 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return AE_OK;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400457acpi_ec_space_handler(u32 function, acpi_physical_address address,
458 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400459 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300461 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400462 int result = 0, i = 0;
463 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400468 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400469 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400470
471 if (bits != 8 && acpi_strict)
472 return AE_BAD_PARAMETER;
473
474 while (bits - i > 0) {
475 if (function == ACPI_READ) {
476 result = acpi_ec_read(ec, address, &temp);
477 (*value) |= ((acpi_integer)temp) << i;
478 } else {
479 temp = 0xff & ((*value) >> i);
480 result = acpi_ec_write(ec, address, temp);
481 }
482 i += 8;
483 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 switch (result) {
487 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400491 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400497 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* --------------------------------------------------------------------------
502 FS Interface (/proc)
503 -------------------------------------------------------------------------- */
504
Len Brown50526df2005-08-11 17:32:05 -0400505static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Len Brown50526df2005-08-11 17:32:05 -0400507static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300509 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!ec)
512 goto end;
513
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300514 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
515 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
516 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
517 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400518 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400519 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
524{
525 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
526}
527
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400528static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400529 .open = acpi_ec_info_open_fs,
530 .read = seq_read,
531 .llseek = seq_lseek,
532 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .owner = THIS_MODULE,
534};
535
Len Brown50526df2005-08-11 17:32:05 -0400536static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Len Brown50526df2005-08-11 17:32:05 -0400538 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!acpi_device_dir(device)) {
541 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400542 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400548 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 else {
552 entry->proc_fops = &acpi_ec_info_ops;
553 entry->data = acpi_driver_data(device);
554 entry->owner = THIS_MODULE;
555 }
556
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Len Brown50526df2005-08-11 17:32:05 -0400560static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if (acpi_device_dir(device)) {
564 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
565 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
566 acpi_device_dir(device) = NULL;
567 }
568
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* --------------------------------------------------------------------------
573 Driver Interface
574 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300575static acpi_status
576ec_parse_io_ports(struct acpi_resource *resource, void *context);
577
578static acpi_status
579ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
580
581static struct acpi_ec *make_acpi_ec(void)
582{
583 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
584 if (!ec)
585 return NULL;
586
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300587 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300588 atomic_set(&ec->event_count, 1);
589 mutex_init(&ec->lock);
590 init_waitqueue_head(&ec->wait);
591
592 return ec;
593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400595static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Len Brown50526df2005-08-11 17:32:05 -0400597 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400598 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400601 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300603 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
604 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
605
606 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300610 status = ec_parse_device(device->handle, 0, ec, NULL);
611 if (status != AE_CTRL_TERMINATE) {
612 kfree(ec);
613 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400614 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300615
616 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300617 if (boot_ec) {
618 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300619 /* We might have incorrect info for GL at boot time */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300620 mutex_lock(&boot_ec->lock);
621 boot_ec->global_lock = ec->global_lock;
622 mutex_unlock(&boot_ec->lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300623 kfree(ec);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300624 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300625 }
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300626 } else
627 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300628 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300631 acpi_ec_add_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400633 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300634 acpi_device_name(device), acpi_device_bid(device),
635 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400636
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Len Brown50526df2005-08-11 17:32:05 -0400640static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300642 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 ec = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300649 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300650 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300651 first_ec = NULL;
652
653 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300654 if (boot_ec != ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300655 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300660ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300662 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300664 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
668 * The first address region returned is the data port, and
669 * the second address region returned is the status/command
670 * port.
671 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300672 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400673 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300674 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400675 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300676 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return AE_OK;
680}
681
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300682static int ec_install_handlers(struct acpi_ec *ec)
683{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300684 acpi_status status;
685 status = acpi_install_gpe_handler(NULL, ec->gpe,
686 ACPI_GPE_EDGE_TRIGGERED,
687 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300688 if (ACPI_FAILURE(status))
689 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300690
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300691 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
692 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
693
694 status = acpi_install_address_space_handler(ec->handle,
695 ACPI_ADR_SPACE_EC,
696 &acpi_ec_space_handler,
697 &acpi_ec_space_setup, ec);
698 if (ACPI_FAILURE(status)) {
699 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
700 return -ENODEV;
701 }
702
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300703 /* EC is fully operational, allow queries */
704 atomic_set(&ec->query_pending, 0);
705
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300706 return 0;
707}
708
Len Brown50526df2005-08-11 17:32:05 -0400709static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300711 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 ec = acpi_driver_data(device);
717
718 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400721 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300722 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300724 /* Boot EC is already working */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300725 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 return 0;
727
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300728 return ec_install_handlers(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Len Brown50526df2005-08-11 17:32:05 -0400731static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300733 acpi_status status;
734 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400737 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300740 if (!ec)
741 return -EINVAL;
742
743 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300744 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400747 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400748 ACPI_ADR_SPACE_EC,
749 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400751 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300753 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300760static acpi_status
761ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Len Brown50526df2005-08-11 17:32:05 -0400763 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -0400764
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300765 struct acpi_ec *ec = context;
766 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
767 ec_parse_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400768 if (ACPI_FAILURE(status))
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300769 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400770
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300771 /* Get GPE bit assignment (EC events). */
772 /* TODO: Add support for _GPE returning a package */
773 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
774 if (ACPI_FAILURE(status))
775 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400776
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300777 /* Use the global lock for all EC transactions? */
778 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Luming Yu45bea152005-07-23 04:08:00 -0400779
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300780 ec->handle = handle;
Luming Yu45bea152005-07-23 04:08:00 -0400781
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300782 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
783 ec->gpe, ec->command_addr, ec->data_addr));
784
785 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Len Brown50526df2005-08-11 17:32:05 -0400788int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Len Brown50526df2005-08-11 17:32:05 -0400790 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300791 acpi_status status;
792 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300794 boot_ec = make_acpi_ec();
795 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300796 return -ENOMEM;
797 /*
798 * Generate a boot ec context
799 */
800
801 status = acpi_get_table(ACPI_SIG_ECDT, 1,
802 (struct acpi_table_header **)&ecdt_ptr);
803 if (ACPI_FAILURE(status))
804 goto error;
805
806 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
807
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300808 boot_ec->command_addr = ecdt_ptr->control.address;
809 boot_ec->data_addr = ecdt_ptr->data.address;
810 boot_ec->gpe = ecdt_ptr->gpe;
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300811 boot_ec->handle = ACPI_ROOT_OBJECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300813 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300814 if (!ret) {
815 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300816 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300817 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300818 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300819 kfree(boot_ec);
820 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 return -ENODEV;
823}
824
Len Brown50526df2005-08-11 17:32:05 -0400825static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Len Brown50526df2005-08-11 17:32:05 -0400827 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
833 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400834 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* Now register the driver for the EC */
837 result = acpi_bus_register_driver(&acpi_ec_driver);
838 if (result < 0) {
839 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400840 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
Patrick Mocheld550d982006-06-27 00:41:40 -0400843 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846subsys_initcall(acpi_ec_init);
847
848/* EC driver currently not unloadable */
849#if 0
Len Brown50526df2005-08-11 17:32:05 -0400850static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 acpi_bus_unregister_driver(&acpi_ec_driver);
854
855 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
856
Patrick Mocheld550d982006-06-27 00:41:40 -0400857 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Len Brown50526df2005-08-11 17:32:05 -0400859#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Len Brown02b28a32005-12-05 16:33:04 -0500861static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400862{
Len Brown02b28a32005-12-05 16:33:04 -0500863 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400864
Len Brown02b28a32005-12-05 16:33:04 -0500865 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400866 return 0;
867
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300868 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
869
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500870 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400871
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800872 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400873}
Len Brown50526df2005-08-11 17:32:05 -0400874
Len Brown53f11d42005-12-05 16:46:36 -0500875__setup("ec_intr=", acpi_ec_set_intr_mode);