blob: e08cf98f504f12583174d23468a60bf4f9a28c14 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f224622007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f224622007-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 Brownc2b67052007-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
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500150static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
Luming Yu45bea152005-07-23 04:08:00 -0400151{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300152 if (acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300153 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
154 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500155 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400156 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300158 } else {
159 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500160 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300161 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500162 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400163 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300164 } else {
165 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
166 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300167 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400168 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300169 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400170
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500172}
173
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400174static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300175 const u8 * wdata, unsigned wdata_len,
176 u8 * rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400177{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300178 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500179 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400180 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400181
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300182 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500183 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300184 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300185 printk(KERN_ERR PREFIX
186 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300187 goto end;
188 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500189 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400190 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400191 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400192
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300193 if (!rdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500194 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300195 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300196 printk(KERN_ERR PREFIX
197 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300198 goto end;
199 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300200 } else if (command == ACPI_EC_COMMAND_QUERY) {
201 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400202 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400203
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300204 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500205 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300206 if (result) {
207 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300208 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300209 goto end;
210 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500211 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400212 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400213 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300214 end:
215 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400216}
217
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400218static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300219 const u8 * wdata, unsigned wdata_len,
220 u8 * rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400221{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400222 int status;
Len Brown50526df2005-08-11 17:32:05 -0400223 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400225 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300228 if (rdata)
229 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300231 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400232 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300234 if (ACPI_FAILURE(status)) {
235 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500239
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300240 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300241 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300242
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500243 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400244 if (status) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300245 printk(KERN_DEBUG PREFIX
246 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500247 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300250 status = acpi_ec_transaction_unlocked(ec, command,
251 wdata, wdata_len,
252 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400253
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300254 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400256 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300258 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300263/*
264 * Note: samsung nv5000 doesn't work with ec burst mode.
265 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
266 */
267int acpi_ec_burst_enable(struct acpi_ec *ec)
268{
269 u8 d;
270 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1);
271}
272
273int acpi_ec_burst_disable(struct acpi_ec *ec)
274{
275 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0);
276}
277
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300278static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400279{
280 int result;
281 u8 d;
282
283 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
284 &address, 1, &d, 1);
285 *data = d;
286 return result;
287}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400288
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400289static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
290{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300291 u8 wdata[2] = { address, data };
292 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400293 wdata, 2, NULL, 0);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * Externally callable EC access functions. For now, assume 1 EC only
298 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300299int ec_burst_enable(void)
300{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300301 if (!first_ec)
302 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300303 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300304}
305
306EXPORT_SYMBOL(ec_burst_enable);
307
308int ec_burst_disable(void)
309{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300310 if (!first_ec)
311 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300312 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300313}
314
315EXPORT_SYMBOL(ec_burst_disable);
316
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300317int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400320 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!first_ec)
323 return -ENODEV;
324
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300325 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (!err) {
328 *val = temp_data;
329 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400330 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return err;
332}
Len Brown50526df2005-08-11 17:32:05 -0400333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334EXPORT_SYMBOL(ec_read);
335
Len Brown50526df2005-08-11 17:32:05 -0400336int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
339
340 if (!first_ec)
341 return -ENODEV;
342
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300343 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return err;
346}
Len Brown50526df2005-08-11 17:32:05 -0400347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348EXPORT_SYMBOL(ec_write);
349
Randy Dunlap616362d2006-10-27 01:47:34 -0400350int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500351 const u8 * wdata, unsigned wdata_len,
352 u8 * rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400353{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400354 if (!first_ec)
355 return -ENODEV;
356
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300357 return acpi_ec_transaction(first_ec, command, wdata,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400358 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400359}
Luming Yu45bea152005-07-23 04:08:00 -0400360
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400361EXPORT_SYMBOL(ec_transaction);
362
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300363static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400364{
365 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300366 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400367
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300368 if (!ec || !data)
369 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400370
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300371 /*
372 * Query the EC to find out which _Qxx method we need to evaluate.
373 * Note that successful completion of the query causes the ACPI_EC_SCI
374 * bit to be cleared (and thus clearing the interrupt source).
375 */
Luming Yu45bea152005-07-23 04:08:00 -0400376
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300377 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
378 if (result)
379 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400380
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300381 if (!d)
382 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400383
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300384 *data = d;
385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* --------------------------------------------------------------------------
389 Event Management
390 -------------------------------------------------------------------------- */
391
Len Brown50526df2005-08-11 17:32:05 -0400392static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300394 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400395 u8 value = 0;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300396 char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400397
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300398 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300399 return;
Luming Yu45bea152005-07-23 04:08:00 -0400400
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400401 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400402
Guillaume Chazarainc6e19192006-12-24 22:19:02 +0100403 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
Luming Yu45bea152005-07-23 04:08:00 -0400404
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400405 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Len Brown50526df2005-08-11 17:32:05 -0400408static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Len Brown50526df2005-08-11 17:32:05 -0400410 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400411 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300412 struct acpi_ec *ec = data;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500413 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300414
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400415 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300416 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500417 }
418
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300419 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300420 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
421 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300422 status =
423 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
424 ec);
Len Brown50526df2005-08-11 17:32:05 -0400425 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300426
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500427 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400428 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431/* --------------------------------------------------------------------------
432 Address Space Management
433 -------------------------------------------------------------------------- */
434
435static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400436acpi_ec_space_setup(acpi_handle region_handle,
437 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 /*
440 * The EC object is in the handler context and is needed
441 * when calling the acpi_ec_space_handler.
442 */
Len Brown50526df2005-08-11 17:32:05 -0400443 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
444 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 return AE_OK;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400450acpi_ec_space_handler(u32 function,
451 acpi_physical_address address,
452 u32 bit_width,
453 acpi_integer * value,
454 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Len Brown50526df2005-08-11 17:32:05 -0400456 int result = 0;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300457 struct acpi_ec *ec = handler_context;
Len Brown50526df2005-08-11 17:32:05 -0400458 u64 temp = *value;
459 acpi_integer f_v = 0;
460 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Luming Yufa9cd542005-03-19 01:54:47 -0500465 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
Len Brown50526df2005-08-11 17:32:05 -0400469 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 switch (function) {
471 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500472 temp = 0;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300473 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500476 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 default:
479 result = -EINVAL;
480 goto out;
481 break;
482 }
483
484 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500485 if (bit_width) {
486 if (function == ACPI_READ)
487 f_v |= temp << 8 * i;
488 if (function == ACPI_WRITE)
489 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500491 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto next_byte;
493 }
494
Luming Yufa9cd542005-03-19 01:54:47 -0500495 if (function == ACPI_READ) {
496 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *value = f_v;
498 }
499
Len Brown50526df2005-08-11 17:32:05 -0400500 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 switch (result) {
502 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400503 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400506 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400509 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400512 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/* --------------------------------------------------------------------------
517 FS Interface (/proc)
518 -------------------------------------------------------------------------- */
519
Len Brown50526df2005-08-11 17:32:05 -0400520static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Len Brown50526df2005-08-11 17:32:05 -0400522static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300524 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!ec)
527 goto end;
528
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300529 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
530 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
531 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
532 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400533 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400534 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
539{
540 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
541}
542
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400543static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400544 .open = acpi_ec_info_open_fs,
545 .read = seq_read,
546 .llseek = seq_lseek,
547 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .owner = THIS_MODULE,
549};
550
Len Brown50526df2005-08-11 17:32:05 -0400551static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Len Brown50526df2005-08-11 17:32:05 -0400553 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!acpi_device_dir(device)) {
556 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400557 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400563 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400565 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 else {
567 entry->proc_fops = &acpi_ec_info_ops;
568 entry->data = acpi_driver_data(device);
569 entry->owner = THIS_MODULE;
570 }
571
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Len Brown50526df2005-08-11 17:32:05 -0400575static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (acpi_device_dir(device)) {
579 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
580 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
581 acpi_device_dir(device) = NULL;
582 }
583
Patrick Mocheld550d982006-06-27 00:41:40 -0400584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/* --------------------------------------------------------------------------
588 Driver Interface
589 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300590static acpi_status
591ec_parse_io_ports(struct acpi_resource *resource, void *context);
592
593static acpi_status
594ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
595
596static struct acpi_ec *make_acpi_ec(void)
597{
598 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
599 if (!ec)
600 return NULL;
601
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300602 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300603 atomic_set(&ec->event_count, 1);
604 mutex_init(&ec->lock);
605 init_waitqueue_head(&ec->wait);
606
607 return ec;
608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400610static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown50526df2005-08-11 17:32:05 -0400612 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400613 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300618 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
619 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
620
621 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300625 status = ec_parse_device(device->handle, 0, ec, NULL);
626 if (status != AE_CTRL_TERMINATE) {
627 kfree(ec);
628 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400629 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300630
631 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300632 if (boot_ec) {
633 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300634 /* We might have incorrect info for GL at boot time */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300635 mutex_lock(&boot_ec->lock);
636 boot_ec->global_lock = ec->global_lock;
637 mutex_unlock(&boot_ec->lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300638 kfree(ec);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300639 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300640 }
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300641 } else
642 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300643 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300646 acpi_ec_add_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400648 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300649 acpi_device_name(device), acpi_device_bid(device),
650 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400651
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Len Brown50526df2005-08-11 17:32:05 -0400655static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300657 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 ec = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300664 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300665 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300666 first_ec = NULL;
667
668 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300669 if (boot_ec != ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300670 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300675ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300677 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300679 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /*
683 * The first address region returned is the data port, and
684 * the second address region returned is the status/command
685 * port.
686 */
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300687 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400688 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300689 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400690 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300691 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return AE_OK;
695}
696
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300697static int ec_install_handlers(struct acpi_ec *ec)
698{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300699 acpi_status status;
700 status = acpi_install_gpe_handler(NULL, ec->gpe,
701 ACPI_GPE_EDGE_TRIGGERED,
702 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300703 if (ACPI_FAILURE(status))
704 return -ENODEV;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300705
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300706 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
707 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
708
709 status = acpi_install_address_space_handler(ec->handle,
710 ACPI_ADR_SPACE_EC,
711 &acpi_ec_space_handler,
712 &acpi_ec_space_setup, ec);
713 if (ACPI_FAILURE(status)) {
714 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
715 return -ENODEV;
716 }
717
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300718 /* EC is fully operational, allow queries */
719 atomic_set(&ec->query_pending, 0);
720
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300721 return 0;
722}
723
Len Brown50526df2005-08-11 17:32:05 -0400724static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 ec = acpi_driver_data(device);
732
733 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400734 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400736 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300737 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300739 /* Boot EC is already working */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300740 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 return 0;
742
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300743 return ec_install_handlers(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Len Brown50526df2005-08-11 17:32:05 -0400746static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300748 acpi_status status;
749 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300755 if (!ec)
756 return -EINVAL;
757
758 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300759 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400762 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400763 ACPI_ADR_SPACE_EC,
764 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300768 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400770 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300775static acpi_status
776ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Len Brown50526df2005-08-11 17:32:05 -0400778 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -0400779
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300780 struct acpi_ec *ec = context;
781 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
782 ec_parse_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400783 if (ACPI_FAILURE(status))
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300784 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400785
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300786 /* Get GPE bit assignment (EC events). */
787 /* TODO: Add support for _GPE returning a package */
788 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
789 if (ACPI_FAILURE(status))
790 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400791
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300792 /* Use the global lock for all EC transactions? */
793 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Luming Yu45bea152005-07-23 04:08:00 -0400794
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300795 ec->handle = handle;
Luming Yu45bea152005-07-23 04:08:00 -0400796
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300797 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
798 ec->gpe, ec->command_addr, ec->data_addr));
799
800 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Len Brown50526df2005-08-11 17:32:05 -0400803int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Len Brown50526df2005-08-11 17:32:05 -0400805 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300806 acpi_status status;
807 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300809 boot_ec = make_acpi_ec();
810 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300811 return -ENOMEM;
812 /*
813 * Generate a boot ec context
814 */
815
816 status = acpi_get_table(ACPI_SIG_ECDT, 1,
817 (struct acpi_table_header **)&ecdt_ptr);
818 if (ACPI_FAILURE(status))
819 goto error;
820
821 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
822
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300823 boot_ec->command_addr = ecdt_ptr->control.address;
824 boot_ec->data_addr = ecdt_ptr->data.address;
825 boot_ec->gpe = ecdt_ptr->gpe;
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300826 boot_ec->handle = ACPI_ROOT_OBJECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300828 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300829 if (!ret) {
830 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300831 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300832 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300833 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300834 kfree(boot_ec);
835 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return -ENODEV;
838}
839
Len Brown50526df2005-08-11 17:32:05 -0400840static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Len Brown50526df2005-08-11 17:32:05 -0400842 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
848 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400849 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Now register the driver for the EC */
852 result = acpi_bus_register_driver(&acpi_ec_driver);
853 if (result < 0) {
854 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400855 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Patrick Mocheld550d982006-06-27 00:41:40 -0400858 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861subsys_initcall(acpi_ec_init);
862
863/* EC driver currently not unloadable */
864#if 0
Len Brown50526df2005-08-11 17:32:05 -0400865static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 acpi_bus_unregister_driver(&acpi_ec_driver);
869
870 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
871
Patrick Mocheld550d982006-06-27 00:41:40 -0400872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Len Brown50526df2005-08-11 17:32:05 -0400874#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Len Brown02b28a332005-12-05 16:33:04 -0500876static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400877{
Len Brown02b28a332005-12-05 16:33:04 -0500878 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400879
Len Brown02b28a332005-12-05 16:33:04 -0500880 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400881 return 0;
882
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300883 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
884
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500885 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400886
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800887 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400888}
Len Brown50526df2005-08-11 17:32:05 -0400889
Len Brown53f11d42005-12-05 16:46:36 -0500890__setup("ec_intr=", acpi_ec_set_intr_mode);