blob: 7f0d81c0ec99924ec4928fabad1b6e817e1a99a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +04002 * ec.c - ACPI Embedded Controller Driver (v2.1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +04004 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03005 * 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
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040029/* Uncomment next line to get verbose printout */
Márton Némethd772b3b2008-01-23 22:34:09 -050030/* #define DEBUG */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/delay.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050039#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040040#include <linux/list.h>
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040041#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45#include <acpi/actypes.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_EC_DEVICE_NAME "Embedded Controller"
49#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040050
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030051#undef PREFIX
52#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040053
Denis M. Sadykov703959d2006-09-26 19:50:33 +040054/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
56#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050057#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040059
Denis M. Sadykov703959d2006-09-26 19:50:33 +040060/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030061enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030062 ACPI_EC_COMMAND_READ = 0x80,
63 ACPI_EC_COMMAND_WRITE = 0x81,
64 ACPI_EC_BURST_ENABLE = 0x82,
65 ACPI_EC_BURST_DISABLE = 0x83,
66 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030067};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040068
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030069#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Alexey Starikovskiye6e82a32008-03-21 17:06:57 +030071#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040073#define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts
74 per one transaction */
75
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040076enum {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040077 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040078 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
79 * for status change */
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +030080 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040081 EC_FLAGS_GPE_STORM, /* GPE storm detected */
82 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
83 * OpReg are installed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040085
86/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030087/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040088typedef int (*acpi_ec_query_func) (void *data);
89
90struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
93 acpi_handle handle;
94 void *data;
95 u8 query_bit;
96};
97
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040098struct transaction_data {
99 const u8 *wdata;
100 u8 *rdata;
101 unsigned short irq_count;
102 u8 wlen;
103 u8 rlen;
104};
105
Adrian Bunka854e082006-12-19 12:56:12 -0800106static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400107 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300108 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400109 unsigned long command_addr;
110 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400111 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400112 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300113 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400114 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400115 struct list_head list;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400116 struct transaction_data *t;
117 spinlock_t t_lock;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300118} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400119
Zhao Yakui25008222008-08-12 10:40:10 +0800120/*
121 * Some Asus system have exchanged ECDT data/command IO addresses.
122 */
123static int print_ecdt_error(const struct dmi_system_id *id)
124{
125 printk(KERN_NOTICE PREFIX "%s detected - "
126 "ECDT has exchanged control/data I/O address\n",
127 id->ident);
128 return 0;
129}
130
131static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
132 {
133 print_ecdt_error, "Asus L4R", {
134 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
135 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
136 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
137 {
138 print_ecdt_error, "Asus M6R", {
139 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
141 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
142 {},
143};
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* --------------------------------------------------------------------------
146 Transaction Management
147 -------------------------------------------------------------------------- */
148
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300151 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500152 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300153 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400156static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300158 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500159 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400160 return x;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400161}
162
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400163static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400164{
Márton Németh86dae012008-01-23 22:33:06 -0500165 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400166 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400167}
168
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400169static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400170{
Márton Németh86dae012008-01-23 22:33:06 -0500171 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400172 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173}
174
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400175static int ec_transaction_done(struct acpi_ec *ec)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400176{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400177 unsigned long flags;
178 int ret = 0;
179 spin_lock_irqsave(&ec->t_lock, flags);
180 if (!ec->t || (!ec->t->wlen && !ec->t->rlen))
181 ret = 1;
182 spin_unlock_irqrestore(&ec->t_lock, flags);
183 return ret;
184}
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400185
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400186static void gpe_transaction(struct acpi_ec *ec, u8 status)
187{
188 unsigned long flags;
189 spin_lock_irqsave(&ec->t_lock, flags);
190 if (!ec->t)
191 goto unlock;
192 if (ec->t->wlen > 0) {
193 if ((status & ACPI_EC_FLAG_IBF) == 0) {
194 acpi_ec_write_data(ec, *(ec->t->wdata++));
195 --ec->t->wlen;
196 } else
197 /* false interrupt, state didn't change */
198 ++ec->t->irq_count;
199
200 } else if (ec->t->rlen > 0) {
201 if ((status & ACPI_EC_FLAG_OBF) == 1) {
202 *(ec->t->rdata++) = acpi_ec_read_data(ec);
203 --ec->t->rlen;
204 } else
205 /* false interrupt, state didn't change */
206 ++ec->t->irq_count;
207 }
208unlock:
209 spin_unlock_irqrestore(&ec->t_lock, flags);
210}
211
212static int acpi_ec_wait(struct acpi_ec *ec)
213{
214 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
215 msecs_to_jiffies(ACPI_EC_DELAY)))
216 return 0;
217 /* missing GPEs, switch back to poll mode */
218 if (printk_ratelimit())
219 pr_info(PREFIX "missing confirmations, "
220 "switch off interrupt mode.\n");
221 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
222 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
223 return 1;
224}
225
226static void acpi_ec_gpe_query(void *ec_cxt);
227
228static int ec_check_sci(struct acpi_ec *ec, u8 state)
229{
230 if (state & ACPI_EC_FLAG_SCI) {
231 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
232 return acpi_os_execute(OSL_EC_BURST_HANDLER,
233 acpi_ec_gpe_query, ec);
234 }
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400235 return 0;
236}
237
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400238static int ec_poll(struct acpi_ec *ec)
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300239{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400240 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
241 msleep(1);
242 while (time_before(jiffies, delay)) {
243 gpe_transaction(ec, acpi_ec_read_status(ec));
244 msleep(1);
245 if (ec_transaction_done(ec))
Zhao Yakui9d699ed2008-08-11 10:33:31 +0800246 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300247 }
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300248 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500249}
250
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400251static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300252 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200253 u8 * rdata, unsigned rdata_len,
254 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400255{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400256 unsigned long tmp;
257 struct transaction_data t = {.wdata = wdata, .rdata = rdata,
258 .wlen = wdata_len, .rlen = rdata_len,
259 .irq_count = 0};
260 int ret = 0;
Márton Németh3ebe08a2007-11-21 03:23:26 +0300261 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400262 /* disable GPE during transaction if storm is detected */
263 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
264 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
265 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
266 }
267 /* start transaction */
268 spin_lock_irqsave(&ec->t_lock, tmp);
269 /* following two actions should be kept atomic */
270 ec->t = &t;
Alexey Starikovskiydc0e8492008-03-21 17:07:09 +0300271 acpi_ec_write_cmd(ec, command);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400272 if (command == ACPI_EC_COMMAND_QUERY)
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400273 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400274 spin_unlock_irqrestore(&ec->t_lock, tmp);
275 /* if we selected poll mode or failed in GPE-mode do a poll loop */
276 if (force_poll ||
277 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
278 acpi_ec_wait(ec))
279 ret = ec_poll(ec);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300280 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400281 spin_lock_irqsave(&ec->t_lock, tmp);
282 ec->t = NULL;
283 spin_unlock_irqrestore(&ec->t_lock, tmp);
284 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
285 /* check if we received SCI during transaction */
286 ec_check_sci(ec, acpi_ec_read_status(ec));
287 /* it is safe to enable GPE outside of transaction */
288 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
289 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
290 t.irq_count > ACPI_EC_STORM_THRESHOLD) {
291 pr_debug(PREFIX "GPE storm detected\n");
292 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
293 }
294 return ret;
295}
296
297static int ec_check_ibf0(struct acpi_ec *ec)
298{
299 u8 status = acpi_ec_read_status(ec);
300 return (status & ACPI_EC_FLAG_IBF) == 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400301}
302
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400303static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300304 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200305 u8 * rdata, unsigned rdata_len,
306 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400307{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400308 int status;
Len Brown50526df2005-08-11 17:32:05 -0400309 u32 glk;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400310 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return -EINVAL;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300312 if (rdata)
313 memset(rdata, 0, rdata_len);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300314 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400315 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300317 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400318 status = -ENODEV;
319 goto unlock;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400322 if (!wait_event_timeout(ec->wait, ec_check_ibf0(ec),
323 msecs_to_jiffies(ACPI_EC_DELAY))) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300324 pr_err(PREFIX "input buffer is not empty, "
325 "aborting transaction\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400326 status = -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500327 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400328 }
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300329 status = acpi_ec_transaction_unlocked(ec, command,
330 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200331 rdata, rdata_len,
332 force_poll);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400333end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400334 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 acpi_release_global_lock(glk);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400336unlock:
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300337 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300341/*
342 * Note: samsung nv5000 doesn't work with ec burst mode.
343 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
344 */
345int acpi_ec_burst_enable(struct acpi_ec *ec)
346{
347 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200348 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300349}
350
351int acpi_ec_burst_disable(struct acpi_ec *ec)
352{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400353 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
354 acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE,
355 NULL, 0, NULL, 0, 0) : 0;
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300356}
357
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300358static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400359{
360 int result;
361 u8 d;
362
363 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200364 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400365 *data = d;
366 return result;
367}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400368
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400369static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
370{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300371 u8 wdata[2] = { address, data };
372 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200373 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
377 * Externally callable EC access functions. For now, assume 1 EC only
378 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300379int ec_burst_enable(void)
380{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300381 if (!first_ec)
382 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300383 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300384}
385
386EXPORT_SYMBOL(ec_burst_enable);
387
388int ec_burst_disable(void)
389{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300390 if (!first_ec)
391 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300392 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300393}
394
395EXPORT_SYMBOL(ec_burst_disable);
396
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400400 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (!first_ec)
403 return -ENODEV;
404
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300405 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (!err) {
408 *val = temp_data;
409 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400410 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return err;
412}
Len Brown50526df2005-08-11 17:32:05 -0400413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414EXPORT_SYMBOL(ec_read);
415
Len Brown50526df2005-08-11 17:32:05 -0400416int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int err;
419
420 if (!first_ec)
421 return -ENODEV;
422
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300423 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return err;
426}
Len Brown50526df2005-08-11 17:32:05 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428EXPORT_SYMBOL(ec_write);
429
Randy Dunlap616362d2006-10-27 01:47:34 -0400430int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500431 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200432 u8 * rdata, unsigned rdata_len,
433 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400434{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400435 if (!first_ec)
436 return -ENODEV;
437
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300438 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200439 wdata_len, rdata, rdata_len,
440 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400441}
Luming Yu45bea152005-07-23 04:08:00 -0400442
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400443EXPORT_SYMBOL(ec_transaction);
444
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300445static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400446{
447 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300448 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400449
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300450 if (!ec || !data)
451 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400452
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300453 /*
454 * Query the EC to find out which _Qxx method we need to evaluate.
455 * Note that successful completion of the query causes the ACPI_EC_SCI
456 * bit to be cleared (and thus clearing the interrupt source).
457 */
Luming Yu45bea152005-07-23 04:08:00 -0400458
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200459 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300460 if (result)
461 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400462
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300463 if (!d)
464 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400465
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300466 *data = d;
467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/* --------------------------------------------------------------------------
471 Event Management
472 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400473int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
474 acpi_handle handle, acpi_ec_query_func func,
475 void *data)
476{
477 struct acpi_ec_query_handler *handler =
478 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
479 if (!handler)
480 return -ENOMEM;
481
482 handler->query_bit = query_bit;
483 handler->handle = handle;
484 handler->func = func;
485 handler->data = data;
486 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400487 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400488 mutex_unlock(&ec->lock);
489 return 0;
490}
491
492EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
493
494void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
495{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200496 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400497 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200498 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400499 if (query_bit == handler->query_bit) {
500 list_del(&handler->node);
501 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400502 }
503 }
504 mutex_unlock(&ec->lock);
505}
506
507EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Len Brown50526df2005-08-11 17:32:05 -0400509static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300511 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400512 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400513 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400514
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300515 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300516 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400517 mutex_lock(&ec->lock);
518 list_for_each_entry(handler, &ec->list, node) {
519 if (value == handler->query_bit) {
520 /* have custom handler for this bit */
521 memcpy(&copy, handler, sizeof(copy));
522 mutex_unlock(&ec->lock);
523 if (copy.func) {
524 copy.func(copy.data);
525 } else if (copy.handle) {
526 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
527 }
528 return;
529 }
530 }
531 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Len Brown50526df2005-08-11 17:32:05 -0400534static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300536 struct acpi_ec *ec = data;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400537 u8 status;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200538
Márton Németh3ebe08a2007-11-21 03:23:26 +0300539 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400540 status = acpi_ec_read_status(ec);
541
542 gpe_transaction(ec, status);
543 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300544 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500545
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400546 ec_check_sci(ec, status);
547 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
548 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400549 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300550 if (printk_ratelimit())
551 pr_info(PREFIX "non-query interrupt received,"
552 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400553 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400554 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400555 return ACPI_INTERRUPT_HANDLED;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/* --------------------------------------------------------------------------
559 Address Space Management
560 -------------------------------------------------------------------------- */
561
562static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400563acpi_ec_space_handler(u32 function, acpi_physical_address address,
564 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400565 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300567 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300568 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400569 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400574 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400576
577 if (bits != 8 && acpi_strict)
578 return AE_BAD_PARAMETER;
579
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300580 acpi_ec_burst_enable(ec);
581
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300582 if (function == ACPI_READ) {
583 result = acpi_ec_read(ec, address, &temp);
584 *value = temp;
585 } else {
586 temp = 0xff & (*value);
587 result = acpi_ec_write(ec, address, temp);
588 }
589
590 for (i = 8; unlikely(bits - i > 0); i += 8) {
591 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400592 if (function == ACPI_READ) {
593 result = acpi_ec_read(ec, address, &temp);
594 (*value) |= ((acpi_integer)temp) << i;
595 } else {
596 temp = 0xff & ((*value) >> i);
597 result = acpi_ec_write(ec, address, temp);
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300601 acpi_ec_burst_disable(ec);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 switch (result) {
604 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
613 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400614 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/* --------------------------------------------------------------------------
619 FS Interface (/proc)
620 -------------------------------------------------------------------------- */
621
Len Brown50526df2005-08-11 17:32:05 -0400622static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Len Brown50526df2005-08-11 17:32:05 -0400624static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300626 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!ec)
629 goto end;
630
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300631 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
632 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
633 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
634 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400635 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400636 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
641{
642 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
643}
644
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400645static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400646 .open = acpi_ec_info_open_fs,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .owner = THIS_MODULE,
651};
652
Len Brown50526df2005-08-11 17:32:05 -0400653static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Len Brown50526df2005-08-11 17:32:05 -0400655 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!acpi_device_dir(device)) {
658 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400659 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700664 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
665 acpi_device_dir(device),
666 &acpi_ec_info_ops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400668 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Len Brown50526df2005-08-11 17:32:05 -0400672static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (acpi_device_dir(device)) {
676 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
677 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
678 acpi_device_dir(device) = NULL;
679 }
680
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/* --------------------------------------------------------------------------
685 Driver Interface
686 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300687static acpi_status
688ec_parse_io_ports(struct acpi_resource *resource, void *context);
689
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300690static struct acpi_ec *make_acpi_ec(void)
691{
692 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
693 if (!ec)
694 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400695 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300696 mutex_init(&ec->lock);
697 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400698 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400699 spin_lock_init(&ec->t_lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300700 return ec;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400703static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400704acpi_ec_register_query_methods(acpi_handle handle, u32 level,
705 void *context, void **return_value)
706{
707 struct acpi_namespace_node *node = handle;
708 struct acpi_ec *ec = context;
709 int value = 0;
710 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
711 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
712 }
713 return AE_OK;
714}
715
716static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400717ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400718{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400719 acpi_status status;
720
721 struct acpi_ec *ec = context;
722 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
723 ec_parse_io_ports, ec);
724 if (ACPI_FAILURE(status))
725 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400726
727 /* Get GPE bit assignment (EC events). */
728 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400729 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
730 if (ACPI_FAILURE(status))
731 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400732 /* Use the global lock for all EC transactions? */
733 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400734 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400735 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400736}
737
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400738static void ec_remove_handlers(struct acpi_ec *ec)
739{
740 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
741 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300742 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400743 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
744 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300745 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400746 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400747}
748
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400749static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400751 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400754 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300755 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
756 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
757
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400758 /* Check for boot EC */
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300759 if (boot_ec &&
760 (boot_ec->handle == device->handle ||
761 boot_ec->handle == ACPI_ROOT_OBJECT)) {
762 ec = boot_ec;
763 boot_ec = NULL;
764 } else {
765 ec = make_acpi_ec();
766 if (!ec)
767 return -ENOMEM;
768 if (ec_parse_device(device->handle, 0, ec, NULL) !=
769 AE_CTRL_TERMINATE) {
770 kfree(ec);
771 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400772 }
773 }
774
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300775 ec->handle = device->handle;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300776
777 /* Find and register all query methods */
778 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
779 acpi_ec_register_query_methods, ec, NULL);
780
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400781 if (!first_ec)
782 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300784 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300785 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400786 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300787 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400788 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Len Brown50526df2005-08-11 17:32:05 -0400792static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300794 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200795 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400801 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200802 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400803 list_del(&handler->node);
804 kfree(handler);
805 }
806 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300808 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300809 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300810 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400811 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300816ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300818 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300820 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * The first address region returned is the data port, and
825 * the second address region returned is the status/command
826 * port.
827 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300828 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400829 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300830 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400831 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300832 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return AE_OK;
836}
837
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300838static int ec_install_handlers(struct acpi_ec *ec)
839{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300840 acpi_status status;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400841 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400842 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300843 status = acpi_install_gpe_handler(NULL, ec->gpe,
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400844 ACPI_GPE_EDGE_TRIGGERED,
845 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300846 if (ACPI_FAILURE(status))
847 return -ENODEV;
848 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
849 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300850 status = acpi_install_address_space_handler(ec->handle,
851 ACPI_ADR_SPACE_EC,
852 &acpi_ec_space_handler,
Alexey Starikovskiy6d9e1122008-03-24 23:22:29 +0300853 NULL, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300854 if (ACPI_FAILURE(status)) {
855 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
856 return -ENODEV;
857 }
858
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400859 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300860 return 0;
861}
862
Len Brown50526df2005-08-11 17:32:05 -0400863static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300865 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400866 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 ec = acpi_driver_data(device);
872
873 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400876 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300877
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400878 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400879 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400880 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Len Brown50526df2005-08-11 17:32:05 -0400883static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300885 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400887 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300889 if (!ec)
890 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400891 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400892
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500896int __init acpi_boot_ec_enable(void)
897{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400898 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500899 return 0;
900 if (!ec_install_handlers(boot_ec)) {
901 first_ec = boot_ec;
902 return 0;
903 }
904 return -EFAULT;
905}
906
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300907static const struct acpi_device_id ec_device_ids[] = {
908 {"PNP0C09", 0},
909 {"", 0},
910};
911
Len Brown50526df2005-08-11 17:32:05 -0400912int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Len Brown50526df2005-08-11 17:32:05 -0400914 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300915 acpi_status status;
916 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300918 boot_ec = make_acpi_ec();
919 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300920 return -ENOMEM;
921 /*
922 * Generate a boot ec context
923 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300924 status = acpi_get_table(ACPI_SIG_ECDT, 1,
925 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400926 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300927 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400928 boot_ec->command_addr = ecdt_ptr->control.address;
929 boot_ec->data_addr = ecdt_ptr->data.address;
Zhao Yakui25008222008-08-12 10:40:10 +0800930 if (dmi_check_system(ec_dmi_table)) {
931 /*
932 * If the board falls into ec_dmi_table, it means
933 * that ECDT table gives the incorrect command/status
934 * & data I/O address. Just fix it.
935 */
936 boot_ec->data_addr = ecdt_ptr->control.address;
937 boot_ec->command_addr = ecdt_ptr->data.address;
938 }
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400939 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -0400940 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300941 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400942 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300943 /* This workaround is needed only on some broken machines,
944 * which require early EC, but fail to provide ECDT */
945 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400946 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
947 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
948 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400949 /* Check that acpi_get_devices actually find something */
950 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400951 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300952 /* We really need to limit this workaround, the only ASUS,
953 * which needs it, has fake EC._INI method, so use it as flag.
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500954 * Keep boot_ec struct as it will be needed soon.
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300955 */
956 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500957 return -ENODEV;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300960 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300961 if (!ret) {
962 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300963 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300964 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300965 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300966 kfree(boot_ec);
967 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -ENODEV;
969}
970
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300971static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
972{
973 struct acpi_ec *ec = acpi_driver_data(device);
974 /* Stop using GPE */
975 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
976 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
977 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
978 return 0;
979}
980
981static int acpi_ec_resume(struct acpi_device *device)
982{
983 struct acpi_ec *ec = acpi_driver_data(device);
984 /* Enable use of GPE back */
985 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
986 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
987 return 0;
988}
989
990static struct acpi_driver acpi_ec_driver = {
991 .name = "ec",
992 .class = ACPI_EC_CLASS,
993 .ids = ec_device_ids,
994 .ops = {
995 .add = acpi_ec_add,
996 .remove = acpi_ec_remove,
997 .start = acpi_ec_start,
998 .stop = acpi_ec_stop,
999 .suspend = acpi_ec_suspend,
1000 .resume = acpi_ec_resume,
1001 },
1002};
1003
Len Brown50526df2005-08-11 17:32:05 -04001004static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Len Brown50526df2005-08-11 17:32:05 -04001006 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1012 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1017 if (result < 0) {
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025subsys_initcall(acpi_ec_init);
1026
1027/* EC driver currently not unloadable */
1028#if 0
Len Brown50526df2005-08-11 17:32:05 -04001029static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1033
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1035
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
Alexey Starikovskiy78439322007-10-22 14:18:43 +04001038#endif /* 0 */