Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
01f22462 | 2 | * ec.c - ACPI Embedded Controller Driver (v2.0) |
1da177e4 | 3 | * |
01f22462 AS |
4 | * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
5 | * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com> | |
1da177e4 LT |
6 | * Copyright (C) 2004 Luming Yu <luming.yu@intel.com> |
7 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
8 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
9 | * | |
10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
25 | * | |
26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
27 | */ | |
28 | ||
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> | |
451566f4 | 36 | #include <linux/interrupt.h> |
1da177e4 LT |
37 | #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 | |
f52fd66d | 43 | ACPI_MODULE_NAME("ec"); |
1da177e4 LT |
44 | #define ACPI_EC_COMPONENT 0x00100000 |
45 | #define ACPI_EC_CLASS "embedded_controller" | |
46 | #define ACPI_EC_HID "PNP0C09" | |
1da177e4 LT |
47 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
48 | #define ACPI_EC_FILE_INFO "info" | |
af3fd140 AS |
49 | #undef PREFIX |
50 | #define PREFIX "ACPI: EC: " | |
703959d4 | 51 | /* EC status register */ |
1da177e4 LT |
52 | #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ |
53 | #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ | |
451566f4 | 54 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
1da177e4 | 55 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
703959d4 | 56 | /* EC commands */ |
3261ff4d | 57 | enum ec_command { |
6ccedb10 AS |
58 | 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, | |
3261ff4d | 63 | }; |
703959d4 | 64 | /* EC events */ |
3261ff4d | 65 | enum ec_event { |
703959d4 | 66 | ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */ |
6ccedb10 | 67 | ACPI_EC_EVENT_IBF_0, /* Input buffer empty */ |
703959d4 DS |
68 | }; |
69 | ||
5c406412 | 70 | #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ |
703959d4 | 71 | #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ |
703959d4 | 72 | |
3261ff4d | 73 | static enum ec_mode { |
6ccedb10 AS |
74 | EC_INTR = 1, /* Output buffer full */ |
75 | EC_POLL, /* Input buffer empty */ | |
3261ff4d | 76 | } acpi_ec_mode = EC_INTR; |
703959d4 | 77 | |
50526df6 LB |
78 | static int acpi_ec_remove(struct acpi_device *device, int type); |
79 | static int acpi_ec_start(struct acpi_device *device); | |
80 | static int acpi_ec_stop(struct acpi_device *device, int type); | |
703959d4 | 81 | static int acpi_ec_add(struct acpi_device *device); |
1da177e4 LT |
82 | |
83 | static struct acpi_driver acpi_ec_driver = { | |
c2b6705b | 84 | .name = "ec", |
50526df6 LB |
85 | .class = ACPI_EC_CLASS, |
86 | .ids = ACPI_EC_HID, | |
87 | .ops = { | |
703959d4 | 88 | .add = acpi_ec_add, |
50526df6 LB |
89 | .remove = acpi_ec_remove, |
90 | .start = acpi_ec_start, | |
91 | .stop = acpi_ec_stop, | |
92 | }, | |
1da177e4 | 93 | }; |
6ffb221a DS |
94 | |
95 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ | |
d033879c | 96 | /* External interfaces use first EC only, so remember */ |
a854e08a | 97 | static struct acpi_ec { |
703959d4 | 98 | acpi_handle handle; |
a86e2772 | 99 | unsigned long gpe; |
6ffb221a DS |
100 | unsigned long command_addr; |
101 | unsigned long data_addr; | |
703959d4 | 102 | unsigned long global_lock; |
c787a855 | 103 | struct mutex lock; |
5d0c288b | 104 | atomic_t query_pending; |
9e197219 | 105 | atomic_t event_count; |
703959d4 | 106 | wait_queue_head_t wait; |
d033879c | 107 | } *boot_ec, *first_ec; |
703959d4 | 108 | |
1da177e4 LT |
109 | /* -------------------------------------------------------------------------- |
110 | Transaction Management | |
111 | -------------------------------------------------------------------------- */ | |
112 | ||
6ffb221a | 113 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 114 | { |
6ffb221a | 115 | return inb(ec->command_addr); |
451566f4 DT |
116 | } |
117 | ||
6ffb221a | 118 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 119 | { |
6ffb221a | 120 | return inb(ec->data_addr); |
7c6db5e5 DS |
121 | } |
122 | ||
6ffb221a | 123 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 124 | { |
6ffb221a | 125 | outb(command, ec->command_addr); |
45bea155 LY |
126 | } |
127 | ||
6ffb221a | 128 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 129 | { |
6ffb221a | 130 | outb(data, ec->data_addr); |
703959d4 | 131 | } |
45bea155 | 132 | |
9e197219 AS |
133 | static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event, |
134 | unsigned old_count) | |
6ffb221a | 135 | { |
bec5a1e0 | 136 | u8 status = acpi_ec_read_status(ec); |
9e197219 AS |
137 | if (old_count == atomic_read(&ec->event_count)) |
138 | return 0; | |
78d0af33 | 139 | if (event == ACPI_EC_EVENT_OBF_1) { |
703959d4 DS |
140 | if (status & ACPI_EC_FLAG_OBF) |
141 | return 1; | |
78d0af33 | 142 | } else if (event == ACPI_EC_EVENT_IBF_0) { |
703959d4 DS |
143 | if (!(status & ACPI_EC_FLAG_IBF)) |
144 | return 1; | |
45bea155 LY |
145 | } |
146 | ||
703959d4 | 147 | return 0; |
45bea155 | 148 | } |
451566f4 | 149 | |
00eb43a1 LP |
150 | static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, |
151 | unsigned count, int force_poll) | |
7c6db5e5 | 152 | { |
00eb43a1 | 153 | if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) { |
50c1e113 AS |
154 | unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); |
155 | while (time_before(jiffies, delay)) { | |
9e197219 | 156 | if (acpi_ec_check_status(ec, event, 0)) |
7c6db5e5 DS |
157 | return 0; |
158 | } | |
af3fd140 AS |
159 | } else { |
160 | if (wait_event_timeout(ec->wait, | |
9e197219 | 161 | acpi_ec_check_status(ec, event, count), |
af3fd140 | 162 | msecs_to_jiffies(ACPI_EC_DELAY)) || |
9e197219 | 163 | acpi_ec_check_status(ec, event, 0)) { |
703959d4 | 164 | return 0; |
af3fd140 AS |
165 | } else { |
166 | printk(KERN_ERR PREFIX "acpi_ec_wait timeout," | |
167 | " status = %d, expect_event = %d\n", | |
6ccedb10 | 168 | acpi_ec_read_status(ec), event); |
703959d4 | 169 | } |
af3fd140 | 170 | } |
1da177e4 | 171 | |
d550d98d | 172 | return -ETIME; |
1da177e4 LT |
173 | } |
174 | ||
703959d4 | 175 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, |
6ccedb10 | 176 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
177 | u8 * rdata, unsigned rdata_len, |
178 | int force_poll) | |
45bea155 | 179 | { |
af3fd140 | 180 | int result = 0; |
9e197219 | 181 | unsigned count = atomic_read(&ec->event_count); |
703959d4 | 182 | acpi_ec_write_cmd(ec, command); |
45bea155 | 183 | |
78d0af33 | 184 | for (; wdata_len > 0; --wdata_len) { |
00eb43a1 | 185 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll); |
af3fd140 | 186 | if (result) { |
6ccedb10 AS |
187 | printk(KERN_ERR PREFIX |
188 | "write_cmd timeout, command = %d\n", command); | |
af3fd140 AS |
189 | goto end; |
190 | } | |
9e197219 | 191 | count = atomic_read(&ec->event_count); |
703959d4 | 192 | acpi_ec_write_data(ec, *(wdata++)); |
3576cf61 | 193 | } |
45bea155 | 194 | |
d91df1aa | 195 | if (!rdata_len) { |
00eb43a1 | 196 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll); |
af3fd140 | 197 | if (result) { |
6ccedb10 AS |
198 | printk(KERN_ERR PREFIX |
199 | "finish-write timeout, command = %d\n", command); | |
af3fd140 AS |
200 | goto end; |
201 | } | |
5d0c288b AS |
202 | } else if (command == ACPI_EC_COMMAND_QUERY) { |
203 | atomic_set(&ec->query_pending, 0); | |
7c6db5e5 | 204 | } |
45bea155 | 205 | |
78d0af33 | 206 | for (; rdata_len > 0; --rdata_len) { |
00eb43a1 | 207 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll); |
af3fd140 AS |
208 | if (result) { |
209 | printk(KERN_ERR PREFIX "read timeout, command = %d\n", | |
6ccedb10 | 210 | command); |
af3fd140 AS |
211 | goto end; |
212 | } | |
9e197219 | 213 | count = atomic_read(&ec->event_count); |
6ffb221a | 214 | *(rdata++) = acpi_ec_read_data(ec); |
7c6db5e5 | 215 | } |
af3fd140 AS |
216 | end: |
217 | return result; | |
45bea155 LY |
218 | } |
219 | ||
3576cf61 | 220 | static int acpi_ec_transaction(struct acpi_ec *ec, u8 command, |
6ccedb10 | 221 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
222 | u8 * rdata, unsigned rdata_len, |
223 | int force_poll) | |
1da177e4 | 224 | { |
d7a76e4c | 225 | int status; |
50526df6 | 226 | u32 glk; |
1da177e4 | 227 | |
d7a76e4c | 228 | if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata)) |
d550d98d | 229 | return -EINVAL; |
1da177e4 | 230 | |
6ccedb10 AS |
231 | if (rdata) |
232 | memset(rdata, 0, rdata_len); | |
1da177e4 | 233 | |
523953b4 | 234 | mutex_lock(&ec->lock); |
703959d4 | 235 | if (ec->global_lock) { |
1da177e4 | 236 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b AS |
237 | if (ACPI_FAILURE(status)) { |
238 | mutex_unlock(&ec->lock); | |
d550d98d | 239 | return -ENODEV; |
c24e912b | 240 | } |
1da177e4 | 241 | } |
451566f4 | 242 | |
5d57a6a5 | 243 | /* Make sure GPE is enabled before doing transaction */ |
a86e2772 | 244 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); |
5d57a6a5 | 245 | |
00eb43a1 | 246 | status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0); |
716e084e | 247 | if (status) { |
6ccedb10 AS |
248 | printk(KERN_DEBUG PREFIX |
249 | "input buffer is not empty, aborting transaction\n"); | |
451566f4 | 250 | goto end; |
716e084e | 251 | } |
1da177e4 | 252 | |
6ccedb10 AS |
253 | status = acpi_ec_transaction_unlocked(ec, command, |
254 | wdata, wdata_len, | |
00eb43a1 LP |
255 | rdata, rdata_len, |
256 | force_poll); | |
1da177e4 | 257 | |
6ccedb10 | 258 | end: |
1da177e4 | 259 | |
703959d4 | 260 | if (ec->global_lock) |
1da177e4 | 261 | acpi_release_global_lock(glk); |
523953b4 | 262 | mutex_unlock(&ec->lock); |
1da177e4 | 263 | |
d550d98d | 264 | return status; |
1da177e4 LT |
265 | } |
266 | ||
c45aac43 AS |
267 | /* |
268 | * Note: samsung nv5000 doesn't work with ec burst mode. | |
269 | * http://bugzilla.kernel.org/show_bug.cgi?id=4980 | |
270 | */ | |
271 | int acpi_ec_burst_enable(struct acpi_ec *ec) | |
272 | { | |
273 | u8 d; | |
00eb43a1 | 274 | return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0); |
c45aac43 AS |
275 | } |
276 | ||
277 | int acpi_ec_burst_disable(struct acpi_ec *ec) | |
278 | { | |
00eb43a1 | 279 | return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0); |
c45aac43 AS |
280 | } |
281 | ||
6ccedb10 | 282 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
3576cf61 DS |
283 | { |
284 | int result; | |
285 | u8 d; | |
286 | ||
287 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, | |
00eb43a1 | 288 | &address, 1, &d, 1, 0); |
3576cf61 DS |
289 | *data = d; |
290 | return result; | |
291 | } | |
6ffb221a | 292 | |
3576cf61 DS |
293 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
294 | { | |
6ccedb10 AS |
295 | u8 wdata[2] = { address, data }; |
296 | return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, | |
00eb43a1 | 297 | wdata, 2, NULL, 0, 0); |
3576cf61 DS |
298 | } |
299 | ||
1da177e4 LT |
300 | /* |
301 | * Externally callable EC access functions. For now, assume 1 EC only | |
302 | */ | |
c45aac43 AS |
303 | int ec_burst_enable(void) |
304 | { | |
c45aac43 AS |
305 | if (!first_ec) |
306 | return -ENODEV; | |
d033879c | 307 | return acpi_ec_burst_enable(first_ec); |
c45aac43 AS |
308 | } |
309 | ||
310 | EXPORT_SYMBOL(ec_burst_enable); | |
311 | ||
312 | int ec_burst_disable(void) | |
313 | { | |
c45aac43 AS |
314 | if (!first_ec) |
315 | return -ENODEV; | |
d033879c | 316 | return acpi_ec_burst_disable(first_ec); |
c45aac43 AS |
317 | } |
318 | ||
319 | EXPORT_SYMBOL(ec_burst_disable); | |
320 | ||
6ccedb10 | 321 | int ec_read(u8 addr, u8 * val) |
1da177e4 | 322 | { |
1da177e4 | 323 | int err; |
6ffb221a | 324 | u8 temp_data; |
1da177e4 LT |
325 | |
326 | if (!first_ec) | |
327 | return -ENODEV; | |
328 | ||
d033879c | 329 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
330 | |
331 | if (!err) { | |
332 | *val = temp_data; | |
333 | return 0; | |
50526df6 | 334 | } else |
1da177e4 LT |
335 | return err; |
336 | } | |
50526df6 | 337 | |
1da177e4 LT |
338 | EXPORT_SYMBOL(ec_read); |
339 | ||
50526df6 | 340 | int ec_write(u8 addr, u8 val) |
1da177e4 | 341 | { |
1da177e4 LT |
342 | int err; |
343 | ||
344 | if (!first_ec) | |
345 | return -ENODEV; | |
346 | ||
d033879c | 347 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
348 | |
349 | return err; | |
350 | } | |
50526df6 | 351 | |
1da177e4 LT |
352 | EXPORT_SYMBOL(ec_write); |
353 | ||
616362de | 354 | int ec_transaction(u8 command, |
9e197219 | 355 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
356 | u8 * rdata, unsigned rdata_len, |
357 | int force_poll) | |
45bea155 | 358 | { |
d7a76e4c LP |
359 | if (!first_ec) |
360 | return -ENODEV; | |
45bea155 | 361 | |
d033879c | 362 | return acpi_ec_transaction(first_ec, command, wdata, |
00eb43a1 LP |
363 | wdata_len, rdata, rdata_len, |
364 | force_poll); | |
45bea155 | 365 | } |
1da177e4 | 366 | |
ab9e43c6 LP |
367 | EXPORT_SYMBOL(ec_transaction); |
368 | ||
6ccedb10 | 369 | static int acpi_ec_query(struct acpi_ec *ec, u8 * data) |
3576cf61 DS |
370 | { |
371 | int result; | |
6ccedb10 | 372 | u8 d; |
1da177e4 | 373 | |
6ccedb10 AS |
374 | if (!ec || !data) |
375 | return -EINVAL; | |
1da177e4 | 376 | |
6ccedb10 AS |
377 | /* |
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 | */ | |
716e084e | 382 | |
00eb43a1 | 383 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0); |
6ccedb10 AS |
384 | if (result) |
385 | return result; | |
1da177e4 | 386 | |
6ccedb10 AS |
387 | if (!d) |
388 | return -ENODATA; | |
1da177e4 | 389 | |
6ccedb10 AS |
390 | *data = d; |
391 | return 0; | |
1da177e4 LT |
392 | } |
393 | ||
1da177e4 LT |
394 | /* -------------------------------------------------------------------------- |
395 | Event Management | |
396 | -------------------------------------------------------------------------- */ | |
397 | ||
50526df6 | 398 | static void acpi_ec_gpe_query(void *ec_cxt) |
45bea155 | 399 | { |
3d02b90b | 400 | struct acpi_ec *ec = ec_cxt; |
6ffb221a | 401 | u8 value = 0; |
5d0c288b | 402 | char object_name[8]; |
45bea155 | 403 | |
5d0c288b | 404 | if (!ec || acpi_ec_query(ec, &value)) |
e41334c0 | 405 | return; |
45bea155 | 406 | |
6ffb221a | 407 | snprintf(object_name, 8, "_Q%2.2X", value); |
45bea155 | 408 | |
c6e19194 | 409 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name)); |
45bea155 | 410 | |
703959d4 | 411 | acpi_evaluate_object(ec->handle, object_name, NULL, NULL); |
45bea155 | 412 | } |
1da177e4 | 413 | |
50526df6 | 414 | static u32 acpi_ec_gpe_handler(void *data) |
1da177e4 | 415 | { |
50526df6 | 416 | acpi_status status = AE_OK; |
6ffb221a | 417 | u8 value; |
3d02b90b | 418 | struct acpi_ec *ec = data; |
00eb43a1 | 419 | |
9e197219 | 420 | atomic_inc(&ec->event_count); |
3d02b90b | 421 | |
8e0341ba | 422 | if (acpi_ec_mode == EC_INTR) { |
af3fd140 | 423 | wake_up(&ec->wait); |
451566f4 DT |
424 | } |
425 | ||
bec5a1e0 | 426 | value = acpi_ec_read_status(ec); |
5d0c288b AS |
427 | if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) { |
428 | atomic_set(&ec->query_pending, 1); | |
6ccedb10 AS |
429 | status = |
430 | acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, | |
431 | ec); | |
50526df6 | 432 | } |
e41334c0 | 433 | |
451566f4 | 434 | return status == AE_OK ? |
50526df6 | 435 | ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED; |
1da177e4 LT |
436 | } |
437 | ||
438 | /* -------------------------------------------------------------------------- | |
439 | Address Space Management | |
440 | -------------------------------------------------------------------------- */ | |
441 | ||
442 | static acpi_status | |
50526df6 LB |
443 | acpi_ec_space_setup(acpi_handle region_handle, |
444 | u32 function, void *handler_context, void **return_context) | |
1da177e4 LT |
445 | { |
446 | /* | |
447 | * The EC object is in the handler context and is needed | |
448 | * when calling the acpi_ec_space_handler. | |
449 | */ | |
50526df6 LB |
450 | *return_context = (function != ACPI_REGION_DEACTIVATE) ? |
451 | handler_context : NULL; | |
1da177e4 LT |
452 | |
453 | return AE_OK; | |
454 | } | |
455 | ||
1da177e4 | 456 | static acpi_status |
50526df6 LB |
457 | acpi_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) | |
1da177e4 | 462 | { |
50526df6 | 463 | int result = 0; |
3d02b90b | 464 | struct acpi_ec *ec = handler_context; |
50526df6 LB |
465 | u64 temp = *value; |
466 | acpi_integer f_v = 0; | |
467 | int i = 0; | |
1da177e4 | 468 | |
1da177e4 | 469 | if ((address > 0xFF) || !value || !handler_context) |
d550d98d | 470 | return AE_BAD_PARAMETER; |
1da177e4 | 471 | |
fa9cd547 | 472 | if (bit_width != 8 && acpi_strict) { |
d550d98d | 473 | return AE_BAD_PARAMETER; |
1da177e4 LT |
474 | } |
475 | ||
50526df6 | 476 | next_byte: |
1da177e4 LT |
477 | switch (function) { |
478 | case ACPI_READ: | |
fa9cd547 | 479 | temp = 0; |
6ccedb10 | 480 | result = acpi_ec_read(ec, (u8) address, (u8 *) & temp); |
1da177e4 LT |
481 | break; |
482 | case ACPI_WRITE: | |
fa9cd547 | 483 | result = acpi_ec_write(ec, (u8) address, (u8) temp); |
1da177e4 LT |
484 | break; |
485 | default: | |
486 | result = -EINVAL; | |
487 | goto out; | |
488 | break; | |
489 | } | |
490 | ||
491 | bit_width -= 8; | |
fa9cd547 LY |
492 | if (bit_width) { |
493 | if (function == ACPI_READ) | |
494 | f_v |= temp << 8 * i; | |
495 | if (function == ACPI_WRITE) | |
496 | temp >>= 8; | |
1da177e4 | 497 | i++; |
83ea7445 | 498 | address++; |
1da177e4 LT |
499 | goto next_byte; |
500 | } | |
501 | ||
fa9cd547 LY |
502 | if (function == ACPI_READ) { |
503 | f_v |= temp << 8 * i; | |
1da177e4 LT |
504 | *value = f_v; |
505 | } | |
506 | ||
50526df6 | 507 | out: |
1da177e4 LT |
508 | switch (result) { |
509 | case -EINVAL: | |
d550d98d | 510 | return AE_BAD_PARAMETER; |
1da177e4 LT |
511 | break; |
512 | case -ENODEV: | |
d550d98d | 513 | return AE_NOT_FOUND; |
1da177e4 LT |
514 | break; |
515 | case -ETIME: | |
d550d98d | 516 | return AE_TIME; |
1da177e4 LT |
517 | break; |
518 | default: | |
d550d98d | 519 | return AE_OK; |
1da177e4 | 520 | } |
1da177e4 LT |
521 | } |
522 | ||
1da177e4 LT |
523 | /* -------------------------------------------------------------------------- |
524 | FS Interface (/proc) | |
525 | -------------------------------------------------------------------------- */ | |
526 | ||
50526df6 | 527 | static struct proc_dir_entry *acpi_ec_dir; |
1da177e4 | 528 | |
50526df6 | 529 | static int acpi_ec_read_info(struct seq_file *seq, void *offset) |
1da177e4 | 530 | { |
3d02b90b | 531 | struct acpi_ec *ec = seq->private; |
1da177e4 | 532 | |
1da177e4 LT |
533 | if (!ec) |
534 | goto end; | |
535 | ||
01f22462 AS |
536 | 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", | |
703959d4 | 540 | ec->global_lock ? "yes" : "no"); |
50526df6 | 541 | end: |
d550d98d | 542 | return 0; |
1da177e4 LT |
543 | } |
544 | ||
545 | static 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 | ||
703959d4 | 550 | static struct file_operations acpi_ec_info_ops = { |
50526df6 LB |
551 | .open = acpi_ec_info_open_fs, |
552 | .read = seq_read, | |
553 | .llseek = seq_lseek, | |
554 | .release = single_release, | |
1da177e4 LT |
555 | .owner = THIS_MODULE, |
556 | }; | |
557 | ||
50526df6 | 558 | static int acpi_ec_add_fs(struct acpi_device *device) |
1da177e4 | 559 | { |
50526df6 | 560 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 561 | |
1da177e4 LT |
562 | if (!acpi_device_dir(device)) { |
563 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
50526df6 | 564 | acpi_ec_dir); |
1da177e4 | 565 | if (!acpi_device_dir(device)) |
d550d98d | 566 | return -ENODEV; |
1da177e4 LT |
567 | } |
568 | ||
569 | entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO, | |
50526df6 | 570 | acpi_device_dir(device)); |
1da177e4 | 571 | if (!entry) |
d550d98d | 572 | return -ENODEV; |
1da177e4 LT |
573 | else { |
574 | entry->proc_fops = &acpi_ec_info_ops; | |
575 | entry->data = acpi_driver_data(device); | |
576 | entry->owner = THIS_MODULE; | |
577 | } | |
578 | ||
d550d98d | 579 | return 0; |
1da177e4 LT |
580 | } |
581 | ||
50526df6 | 582 | static int acpi_ec_remove_fs(struct acpi_device *device) |
1da177e4 | 583 | { |
1da177e4 LT |
584 | |
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 | ||
d550d98d | 591 | return 0; |
1da177e4 LT |
592 | } |
593 | ||
1da177e4 LT |
594 | /* -------------------------------------------------------------------------- |
595 | Driver Interface | |
596 | -------------------------------------------------------------------------- */ | |
c0900c35 AS |
597 | static acpi_status |
598 | ec_parse_io_ports(struct acpi_resource *resource, void *context); | |
599 | ||
600 | static acpi_status | |
601 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval); | |
602 | ||
603 | static 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 | ||
9fd9f8e8 | 609 | atomic_set(&ec->query_pending, 1); |
c0900c35 AS |
610 | atomic_set(&ec->event_count, 1); |
611 | mutex_init(&ec->lock); | |
612 | init_waitqueue_head(&ec->wait); | |
613 | ||
614 | return ec; | |
615 | } | |
1da177e4 | 616 | |
703959d4 | 617 | static int acpi_ec_add(struct acpi_device *device) |
1da177e4 | 618 | { |
50526df6 | 619 | acpi_status status = AE_OK; |
703959d4 | 620 | struct acpi_ec *ec = NULL; |
45bea155 | 621 | |
45bea155 | 622 | if (!device) |
d550d98d | 623 | return -EINVAL; |
45bea155 | 624 | |
c0900c35 AS |
625 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
626 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
627 | ||
628 | ec = make_acpi_ec(); | |
45bea155 | 629 | if (!ec) |
d550d98d | 630 | return -ENOMEM; |
703959d4 | 631 | |
c0900c35 AS |
632 | status = ec_parse_device(device->handle, 0, ec, NULL); |
633 | if (status != AE_CTRL_TERMINATE) { | |
634 | kfree(ec); | |
635 | return -EINVAL; | |
45bea155 | 636 | } |
1da177e4 | 637 | |
c0900c35 | 638 | /* Check if we found the boot EC */ |
d66d969d AS |
639 | if (boot_ec) { |
640 | if (boot_ec->gpe == ec->gpe) { | |
c0900c35 | 641 | /* We might have incorrect info for GL at boot time */ |
d66d969d AS |
642 | mutex_lock(&boot_ec->lock); |
643 | boot_ec->global_lock = ec->global_lock; | |
644 | mutex_unlock(&boot_ec->lock); | |
c0900c35 | 645 | kfree(ec); |
d66d969d | 646 | ec = boot_ec; |
c0900c35 | 647 | } |
d033879c AS |
648 | } else |
649 | first_ec = ec; | |
c0900c35 | 650 | ec->handle = device->handle; |
c0900c35 | 651 | acpi_driver_data(device) = ec; |
1da177e4 | 652 | |
c0900c35 | 653 | acpi_ec_add_fs(device); |
1da177e4 | 654 | |
703959d4 | 655 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.", |
6ccedb10 AS |
656 | acpi_device_name(device), acpi_device_bid(device), |
657 | (u32) ec->gpe)); | |
1da177e4 | 658 | |
c0900c35 | 659 | return 0; |
1da177e4 LT |
660 | } |
661 | ||
50526df6 | 662 | static int acpi_ec_remove(struct acpi_device *device, int type) |
1da177e4 | 663 | { |
01f22462 | 664 | struct acpi_ec *ec; |
1da177e4 | 665 | |
1da177e4 | 666 | if (!device) |
d550d98d | 667 | return -EINVAL; |
1da177e4 LT |
668 | |
669 | ec = acpi_driver_data(device); | |
1da177e4 | 670 | acpi_ec_remove_fs(device); |
c0900c35 | 671 | acpi_driver_data(device) = NULL; |
d033879c | 672 | if (ec == first_ec) |
c0900c35 AS |
673 | first_ec = NULL; |
674 | ||
675 | /* Don't touch boot EC */ | |
d66d969d | 676 | if (boot_ec != ec) |
c0900c35 | 677 | kfree(ec); |
d550d98d | 678 | return 0; |
1da177e4 LT |
679 | } |
680 | ||
1da177e4 | 681 | static acpi_status |
c0900c35 | 682 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 683 | { |
3d02b90b | 684 | struct acpi_ec *ec = context; |
1da177e4 | 685 | |
01f22462 | 686 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 687 | return AE_OK; |
1da177e4 LT |
688 | |
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 | */ | |
01f22462 | 694 | if (ec->data_addr == 0) |
6ffb221a | 695 | ec->data_addr = resource->data.io.minimum; |
01f22462 | 696 | else if (ec->command_addr == 0) |
6ffb221a | 697 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 698 | else |
1da177e4 | 699 | return AE_CTRL_TERMINATE; |
1da177e4 | 700 | |
1da177e4 LT |
701 | return AE_OK; |
702 | } | |
703 | ||
e8284321 AS |
704 | static int ec_install_handlers(struct acpi_ec *ec) |
705 | { | |
c0900c35 AS |
706 | acpi_status status; |
707 | status = acpi_install_gpe_handler(NULL, ec->gpe, | |
708 | ACPI_GPE_EDGE_TRIGGERED, | |
709 | &acpi_ec_gpe_handler, ec); | |
e8284321 AS |
710 | if (ACPI_FAILURE(status)) |
711 | return -ENODEV; | |
01f22462 | 712 | |
e8284321 AS |
713 | 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 | ||
9fd9f8e8 AS |
725 | /* EC is fully operational, allow queries */ |
726 | atomic_set(&ec->query_pending, 0); | |
727 | ||
e8284321 AS |
728 | return 0; |
729 | } | |
730 | ||
50526df6 | 731 | static int acpi_ec_start(struct acpi_device *device) |
1da177e4 | 732 | { |
c0900c35 | 733 | struct acpi_ec *ec; |
1da177e4 | 734 | |
1da177e4 | 735 | if (!device) |
d550d98d | 736 | return -EINVAL; |
1da177e4 LT |
737 | |
738 | ec = acpi_driver_data(device); | |
739 | ||
740 | if (!ec) | |
d550d98d | 741 | return -EINVAL; |
1da177e4 | 742 | |
6ffb221a | 743 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx", |
a86e2772 | 744 | ec->gpe, ec->command_addr, ec->data_addr)); |
1da177e4 | 745 | |
c0900c35 | 746 | /* Boot EC is already working */ |
d66d969d | 747 | if (ec == boot_ec) |
c0900c35 AS |
748 | return 0; |
749 | ||
e8284321 | 750 | return ec_install_handlers(ec); |
1da177e4 LT |
751 | } |
752 | ||
50526df6 | 753 | static int acpi_ec_stop(struct acpi_device *device, int type) |
1da177e4 | 754 | { |
c0900c35 AS |
755 | acpi_status status; |
756 | struct acpi_ec *ec; | |
1da177e4 | 757 | |
1da177e4 | 758 | if (!device) |
d550d98d | 759 | return -EINVAL; |
1da177e4 LT |
760 | |
761 | ec = acpi_driver_data(device); | |
c0900c35 AS |
762 | if (!ec) |
763 | return -EINVAL; | |
764 | ||
765 | /* Don't touch boot EC */ | |
d66d969d | 766 | if (ec == boot_ec) |
c0900c35 | 767 | return 0; |
1da177e4 | 768 | |
703959d4 | 769 | status = acpi_remove_address_space_handler(ec->handle, |
50526df6 LB |
770 | ACPI_ADR_SPACE_EC, |
771 | &acpi_ec_space_handler); | |
1da177e4 | 772 | if (ACPI_FAILURE(status)) |
d550d98d | 773 | return -ENODEV; |
1da177e4 | 774 | |
6ccedb10 | 775 | status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); |
1da177e4 | 776 | if (ACPI_FAILURE(status)) |
d550d98d | 777 | return -ENODEV; |
1da177e4 | 778 | |
d550d98d | 779 | return 0; |
1da177e4 LT |
780 | } |
781 | ||
c0900c35 AS |
782 | static acpi_status |
783 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
45bea155 | 784 | { |
50526df6 | 785 | acpi_status status; |
c0900c35 AS |
786 | |
787 | struct acpi_ec *ec = context; | |
788 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, | |
789 | ec_parse_io_ports, ec); | |
790 | if (ACPI_FAILURE(status)) | |
791 | return status; | |
792 | ||
793 | /* 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; | |
798 | ||
799 | /* Use the global lock for all EC transactions? */ | |
800 | acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); | |
801 | ||
802 | ec->handle = handle; | |
803 | ||
804 | 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; | |
808 | } | |
809 | ||
810 | int __init acpi_ec_ecdt_probe(void) | |
811 | { | |
812 | int ret; | |
813 | acpi_status status; | |
50526df6 | 814 | struct acpi_table_ecdt *ecdt_ptr; |
45bea155 | 815 | |
d66d969d AS |
816 | boot_ec = make_acpi_ec(); |
817 | if (!boot_ec) | |
c0900c35 AS |
818 | return -ENOMEM; |
819 | /* | |
820 | * Generate a boot ec context | |
821 | */ | |
822 | ||
15a58ed1 AS |
823 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
824 | (struct acpi_table_header **)&ecdt_ptr); | |
45bea155 | 825 | if (ACPI_FAILURE(status)) |
c0900c35 | 826 | goto error; |
45bea155 | 827 | |
703959d4 | 828 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT")); |
45bea155 | 829 | |
d66d969d AS |
830 | boot_ec->command_addr = ecdt_ptr->control.address; |
831 | boot_ec->data_addr = ecdt_ptr->data.address; | |
832 | boot_ec->gpe = ecdt_ptr->gpe; | |
d66d969d | 833 | boot_ec->handle = ACPI_ROOT_OBJECT; |
1da177e4 | 834 | |
d66d969d | 835 | ret = ec_install_handlers(boot_ec); |
d033879c AS |
836 | if (!ret) { |
837 | first_ec = boot_ec; | |
e8284321 | 838 | return 0; |
d033879c | 839 | } |
c0900c35 | 840 | error: |
d66d969d AS |
841 | kfree(boot_ec); |
842 | boot_ec = NULL; | |
1da177e4 LT |
843 | |
844 | return -ENODEV; | |
845 | } | |
846 | ||
50526df6 | 847 | static int __init acpi_ec_init(void) |
1da177e4 | 848 | { |
50526df6 | 849 | int result = 0; |
1da177e4 | 850 | |
1da177e4 | 851 | if (acpi_disabled) |
d550d98d | 852 | return 0; |
1da177e4 LT |
853 | |
854 | acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir); | |
855 | if (!acpi_ec_dir) | |
d550d98d | 856 | return -ENODEV; |
1da177e4 LT |
857 | |
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); | |
d550d98d | 862 | return -ENODEV; |
1da177e4 LT |
863 | } |
864 | ||
d550d98d | 865 | return result; |
1da177e4 LT |
866 | } |
867 | ||
868 | subsys_initcall(acpi_ec_init); | |
869 | ||
870 | /* EC driver currently not unloadable */ | |
871 | #if 0 | |
50526df6 | 872 | static void __exit acpi_ec_exit(void) |
1da177e4 | 873 | { |
1da177e4 LT |
874 | |
875 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
876 | ||
877 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
878 | ||
d550d98d | 879 | return; |
1da177e4 | 880 | } |
50526df6 | 881 | #endif /* 0 */ |
1da177e4 | 882 | |
02b28a33 | 883 | static int __init acpi_ec_set_intr_mode(char *str) |
45bea155 | 884 | { |
02b28a33 | 885 | int intr; |
7b15f5e7 | 886 | |
02b28a33 | 887 | if (!get_option(&str, &intr)) |
7b15f5e7 LY |
888 | return 0; |
889 | ||
c0900c35 AS |
890 | acpi_ec_mode = (intr) ? EC_INTR : EC_POLL; |
891 | ||
9e197219 | 892 | printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling"); |
703959d4 | 893 | |
9b41046c | 894 | return 1; |
45bea155 | 895 | } |
50526df6 | 896 | |
53f11d4f | 897 | __setup("ec_intr=", acpi_ec_set_intr_mode); |