blob: 82f496c07675c66e3cf76020cfbf81fc3dc4139f [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 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
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
Len Brown50526df2005-08-11 17:32:05 -0400457acpi_ec_space_handler(u32 function,
458 acpi_physical_address address,
459 u32 bit_width,
460 acpi_integer * value,
461 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Len Brown50526df2005-08-11 17:32:05 -0400463 int result = 0;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300464 struct acpi_ec *ec = handler_context;
Len Brown50526df2005-08-11 17:32:05 -0400465 u64 temp = *value;
466 acpi_integer f_v = 0;
467 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Luming Yufa9cd542005-03-19 01:54:47 -0500472 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Len Brown50526df2005-08-11 17:32:05 -0400476 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 switch (function) {
478 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500479 temp = 0;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300480 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500483 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 default:
486 result = -EINVAL;
487 goto out;
488 break;
489 }
490
491 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500492 if (bit_width) {
493 if (function == ACPI_READ)
494 f_v |= temp << 8 * i;
495 if (function == ACPI_WRITE)
496 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500498 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto next_byte;
500 }
501
Luming Yufa9cd542005-03-19 01:54:47 -0500502 if (function == ACPI_READ) {
503 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *value = f_v;
505 }
506
Len Brown50526df2005-08-11 17:32:05 -0400507 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 switch (result) {
509 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
512 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400513 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400516 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400519 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/* --------------------------------------------------------------------------
524 FS Interface (/proc)
525 -------------------------------------------------------------------------- */
526
Len Brown50526df2005-08-11 17:32:05 -0400527static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Len Brown50526df2005-08-11 17:32:05 -0400529static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300531 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!ec)
534 goto end;
535
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300536 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
537 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
538 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
539 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400540 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400541 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
546{
547 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
548}
549
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400550static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400551 .open = acpi_ec_info_open_fs,
552 .read = seq_read,
553 .llseek = seq_lseek,
554 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .owner = THIS_MODULE,
556};
557
Len Brown50526df2005-08-11 17:32:05 -0400558static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown50526df2005-08-11 17:32:05 -0400560 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!acpi_device_dir(device)) {
563 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400564 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400570 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 else {
574 entry->proc_fops = &acpi_ec_info_ops;
575 entry->data = acpi_driver_data(device);
576 entry->owner = THIS_MODULE;
577 }
578
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Len Brown50526df2005-08-11 17:32:05 -0400582static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (acpi_device_dir(device)) {
586 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
587 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
588 acpi_device_dir(device) = NULL;
589 }
590
Patrick Mocheld550d982006-06-27 00:41:40 -0400591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* --------------------------------------------------------------------------
595 Driver Interface
596 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300597static acpi_status
598ec_parse_io_ports(struct acpi_resource *resource, void *context);
599
600static acpi_status
601ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
602
603static struct acpi_ec *make_acpi_ec(void)
604{
605 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
606 if (!ec)
607 return NULL;
608
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300609 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300610 atomic_set(&ec->event_count, 1);
611 mutex_init(&ec->lock);
612 init_waitqueue_head(&ec->wait);
613
614 return ec;
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400617static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Len Brown50526df2005-08-11 17:32:05 -0400619 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400620 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300625 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
626 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
627
628 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300632 status = ec_parse_device(device->handle, 0, ec, NULL);
633 if (status != AE_CTRL_TERMINATE) {
634 kfree(ec);
635 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400636 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300637
638 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300639 if (boot_ec) {
640 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300641 /* We might have incorrect info for GL at boot time */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300642 mutex_lock(&boot_ec->lock);
643 boot_ec->global_lock = ec->global_lock;
644 mutex_unlock(&boot_ec->lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300645 kfree(ec);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300646 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647 }
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300648 } else
649 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300650 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300653 acpi_ec_add_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400655 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300656 acpi_device_name(device), acpi_device_bid(device),
657 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400658
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Len Brown50526df2005-08-11 17:32:05 -0400662static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300664 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 ec = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300671 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300672 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300673 first_ec = NULL;
674
675 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300676 if (boot_ec != ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300677 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300682ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300684 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300686 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /*
690 * The first address region returned is the data port, and
691 * the second address region returned is the status/command
692 * port.
693 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300694 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400695 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300696 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400697 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300698 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return AE_OK;
702}
703
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300704static int ec_install_handlers(struct acpi_ec *ec)
705{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300706 acpi_status status;
707 status = acpi_install_gpe_handler(NULL, ec->gpe,
708 ACPI_GPE_EDGE_TRIGGERED,
709 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300710 if (ACPI_FAILURE(status))
711 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300712
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300713 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
714 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
715
716 status = acpi_install_address_space_handler(ec->handle,
717 ACPI_ADR_SPACE_EC,
718 &acpi_ec_space_handler,
719 &acpi_ec_space_setup, ec);
720 if (ACPI_FAILURE(status)) {
721 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
722 return -ENODEV;
723 }
724
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300725 /* EC is fully operational, allow queries */
726 atomic_set(&ec->query_pending, 0);
727
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300728 return 0;
729}
730
Len Brown50526df2005-08-11 17:32:05 -0400731static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300733 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 ec = acpi_driver_data(device);
739
740 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400741 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400743 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300744 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300746 /* Boot EC is already working */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300747 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300748 return 0;
749
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300750 return ec_install_handlers(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Len Brown50526df2005-08-11 17:32:05 -0400753static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300755 acpi_status status;
756 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400759 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762 if (!ec)
763 return -EINVAL;
764
765 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300766 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400769 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400770 ACPI_ADR_SPACE_EC,
771 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400773 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300775 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400777 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300782static acpi_status
783ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Len Brown50526df2005-08-11 17:32:05 -0400785 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -0400786
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300787 struct acpi_ec *ec = context;
788 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
789 ec_parse_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400790 if (ACPI_FAILURE(status))
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300791 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400792
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300793 /* Get GPE bit assignment (EC events). */
794 /* TODO: Add support for _GPE returning a package */
795 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
796 if (ACPI_FAILURE(status))
797 return status;
Luming Yu45bea152005-07-23 04:08:00 -0400798
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300799 /* Use the global lock for all EC transactions? */
800 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Luming Yu45bea152005-07-23 04:08:00 -0400801
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300802 ec->handle = handle;
Luming Yu45bea152005-07-23 04:08:00 -0400803
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300804 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
805 ec->gpe, ec->command_addr, ec->data_addr));
806
807 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Len Brown50526df2005-08-11 17:32:05 -0400810int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Len Brown50526df2005-08-11 17:32:05 -0400812 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300813 acpi_status status;
814 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300816 boot_ec = make_acpi_ec();
817 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300818 return -ENOMEM;
819 /*
820 * Generate a boot ec context
821 */
822
823 status = acpi_get_table(ACPI_SIG_ECDT, 1,
824 (struct acpi_table_header **)&ecdt_ptr);
825 if (ACPI_FAILURE(status))
826 goto error;
827
828 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
829
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300830 boot_ec->command_addr = ecdt_ptr->control.address;
831 boot_ec->data_addr = ecdt_ptr->data.address;
832 boot_ec->gpe = ecdt_ptr->gpe;
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300833 boot_ec->handle = ACPI_ROOT_OBJECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300835 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300836 if (!ret) {
837 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300838 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300839 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300840 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300841 kfree(boot_ec);
842 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 return -ENODEV;
845}
846
Len Brown50526df2005-08-11 17:32:05 -0400847static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Len Brown50526df2005-08-11 17:32:05 -0400849 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
855 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400856 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Now register the driver for the EC */
859 result = acpi_bus_register_driver(&acpi_ec_driver);
860 if (result < 0) {
861 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400862 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Patrick Mocheld550d982006-06-27 00:41:40 -0400865 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868subsys_initcall(acpi_ec_init);
869
870/* EC driver currently not unloadable */
871#if 0
Len Brown50526df2005-08-11 17:32:05 -0400872static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 acpi_bus_unregister_driver(&acpi_ec_driver);
876
877 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
878
Patrick Mocheld550d982006-06-27 00:41:40 -0400879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Len Brown50526df2005-08-11 17:32:05 -0400881#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Len Brown02b28a32005-12-05 16:33:04 -0500883static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400884{
Len Brown02b28a32005-12-05 16:33:04 -0500885 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400886
Len Brown02b28a32005-12-05 16:33:04 -0500887 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400888 return 0;
889
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300890 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
891
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500892 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400893
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800894 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400895}
Len Brown50526df2005-08-11 17:32:05 -0400896
Len Brown53f11d42005-12-05 16:46:36 -0500897__setup("ec_intr=", acpi_ec_set_intr_mode);