Loading...
1/*
2 * Support for OLPC XO-1 System Control Interrupts (SCI)
3 *
4 * Copyright (C) 2010 One Laptop per Child
5 * Copyright (C) 2006 Red Hat, Inc.
6 * Copyright (C) 2006 Advanced Micro Devices, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/cs5535.h>
15#include <linux/device.h>
16#include <linux/gpio.h>
17#include <linux/input.h>
18#include <linux/interrupt.h>
19#include <linux/platform_device.h>
20#include <linux/pm.h>
21#include <linux/pm_wakeup.h>
22#include <linux/mfd/core.h>
23#include <linux/power_supply.h>
24#include <linux/suspend.h>
25#include <linux/workqueue.h>
26
27#include <asm/io.h>
28#include <asm/msr.h>
29#include <asm/olpc.h>
30
31#define DRV_NAME "olpc-xo1-sci"
32#define PFX DRV_NAME ": "
33
34static unsigned long acpi_base;
35static struct input_dev *power_button_idev;
36static struct input_dev *ebook_switch_idev;
37static struct input_dev *lid_switch_idev;
38
39static int sci_irq;
40
41static bool lid_open;
42static bool lid_inverted;
43static int lid_wake_mode;
44
45enum lid_wake_modes {
46 LID_WAKE_ALWAYS,
47 LID_WAKE_OPEN,
48 LID_WAKE_CLOSE,
49};
50
51static const char * const lid_wake_mode_names[] = {
52 [LID_WAKE_ALWAYS] = "always",
53 [LID_WAKE_OPEN] = "open",
54 [LID_WAKE_CLOSE] = "close",
55};
56
57static void battery_status_changed(void)
58{
59 struct power_supply *psy = power_supply_get_by_name("olpc-battery");
60
61 if (psy) {
62 power_supply_changed(psy);
63 put_device(psy->dev);
64 }
65}
66
67static void ac_status_changed(void)
68{
69 struct power_supply *psy = power_supply_get_by_name("olpc-ac");
70
71 if (psy) {
72 power_supply_changed(psy);
73 put_device(psy->dev);
74 }
75}
76
77/* Report current ebook switch state through input layer */
78static void send_ebook_state(void)
79{
80 unsigned char state;
81
82 if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
83 pr_err(PFX "failed to get ebook state\n");
84 return;
85 }
86
87 if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
88 return; /* Nothing new to report. */
89
90 input_report_switch(ebook_switch_idev, SW_TABLET_MODE, state);
91 input_sync(ebook_switch_idev);
92 pm_wakeup_event(&ebook_switch_idev->dev, 0);
93}
94
95static void flip_lid_inverter(void)
96{
97 /* gpio is high; invert so we'll get l->h event interrupt */
98 if (lid_inverted)
99 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
100 else
101 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
102 lid_inverted = !lid_inverted;
103}
104
105static void detect_lid_state(void)
106{
107 /*
108 * the edge detector hookup on the gpio inputs on the geode is
109 * odd, to say the least. See http://dev.laptop.org/ticket/5703
110 * for details, but in a nutshell: we don't use the edge
111 * detectors. instead, we make use of an anomoly: with the both
112 * edge detectors turned off, we still get an edge event on a
113 * positive edge transition. to take advantage of this, we use the
114 * front-end inverter to ensure that that's the edge we're always
115 * going to see next.
116 */
117
118 int state;
119
120 state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
121 lid_open = !state ^ !lid_inverted; /* x ^^ y */
122 if (!state)
123 return;
124
125 flip_lid_inverter();
126}
127
128/* Report current lid switch state through input layer */
129static void send_lid_state(void)
130{
131 if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
132 return; /* Nothing new to report. */
133
134 input_report_switch(lid_switch_idev, SW_LID, !lid_open);
135 input_sync(lid_switch_idev);
136 pm_wakeup_event(&lid_switch_idev->dev, 0);
137}
138
139static ssize_t lid_wake_mode_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
141{
142 const char *mode = lid_wake_mode_names[lid_wake_mode];
143 return sprintf(buf, "%s\n", mode);
144}
145static ssize_t lid_wake_mode_set(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 int i;
150 for (i = 0; i < ARRAY_SIZE(lid_wake_mode_names); i++) {
151 const char *mode = lid_wake_mode_names[i];
152 if (strlen(mode) != count || strncasecmp(mode, buf, count))
153 continue;
154
155 lid_wake_mode = i;
156 return count;
157 }
158 return -EINVAL;
159}
160static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
161 lid_wake_mode_set);
162
163/*
164 * Process all items in the EC's SCI queue.
165 *
166 * This is handled in a workqueue because olpc_ec_cmd can be slow (and
167 * can even timeout).
168 *
169 * If propagate_events is false, the queue is drained without events being
170 * generated for the interrupts.
171 */
172static void process_sci_queue(bool propagate_events)
173{
174 int r;
175 u16 data;
176
177 do {
178 r = olpc_ec_sci_query(&data);
179 if (r || !data)
180 break;
181
182 pr_debug(PFX "SCI 0x%x received\n", data);
183
184 switch (data) {
185 case EC_SCI_SRC_BATERR:
186 case EC_SCI_SRC_BATSOC:
187 case EC_SCI_SRC_BATTERY:
188 case EC_SCI_SRC_BATCRIT:
189 battery_status_changed();
190 break;
191 case EC_SCI_SRC_ACPWR:
192 ac_status_changed();
193 break;
194 }
195
196 if (data == EC_SCI_SRC_EBOOK && propagate_events)
197 send_ebook_state();
198 } while (data);
199
200 if (r)
201 pr_err(PFX "Failed to clear SCI queue");
202}
203
204static void process_sci_queue_work(struct work_struct *work)
205{
206 process_sci_queue(true);
207}
208
209static DECLARE_WORK(sci_work, process_sci_queue_work);
210
211static irqreturn_t xo1_sci_intr(int irq, void *dev_id)
212{
213 struct platform_device *pdev = dev_id;
214 u32 sts;
215 u32 gpe;
216
217 sts = inl(acpi_base + CS5536_PM1_STS);
218 outl(sts | 0xffff, acpi_base + CS5536_PM1_STS);
219
220 gpe = inl(acpi_base + CS5536_PM_GPE0_STS);
221 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
222
223 dev_dbg(&pdev->dev, "sts %x gpe %x\n", sts, gpe);
224
225 if (sts & CS5536_PWRBTN_FLAG) {
226 if (!(sts & CS5536_WAK_FLAG)) {
227 /* Only report power button input when it was pressed
228 * during regular operation (as opposed to when it
229 * was used to wake the system). */
230 input_report_key(power_button_idev, KEY_POWER, 1);
231 input_sync(power_button_idev);
232 input_report_key(power_button_idev, KEY_POWER, 0);
233 input_sync(power_button_idev);
234 }
235 /* Report the wakeup event in all cases. */
236 pm_wakeup_event(&power_button_idev->dev, 0);
237 }
238
239 if ((sts & (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) ==
240 (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) {
241 /* When the system is woken by the RTC alarm, report the
242 * event on the rtc device. */
243 struct device *rtc = bus_find_device_by_name(
244 &platform_bus_type, NULL, "rtc_cmos");
245 if (rtc) {
246 pm_wakeup_event(rtc, 0);
247 put_device(rtc);
248 }
249 }
250
251 if (gpe & CS5536_GPIOM7_PME_FLAG) { /* EC GPIO */
252 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
253 schedule_work(&sci_work);
254 }
255
256 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
257 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
258 detect_lid_state();
259 send_lid_state();
260
261 return IRQ_HANDLED;
262}
263
264static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
265{
266 if (device_may_wakeup(&power_button_idev->dev))
267 olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
268 else
269 olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
270
271 if (device_may_wakeup(&ebook_switch_idev->dev))
272 olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
273 else
274 olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
275
276 if (!device_may_wakeup(&lid_switch_idev->dev)) {
277 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
278 } else if ((lid_open && lid_wake_mode == LID_WAKE_OPEN) ||
279 (!lid_open && lid_wake_mode == LID_WAKE_CLOSE)) {
280 flip_lid_inverter();
281
282 /* we may have just caused an event */
283 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
284 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
285
286 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
287 }
288
289 return 0;
290}
291
292static int xo1_sci_resume(struct platform_device *pdev)
293{
294 /*
295 * We don't know what may have happened while we were asleep.
296 * Reestablish our lid setup so we're sure to catch all transitions.
297 */
298 detect_lid_state();
299 send_lid_state();
300 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
301
302 /* Enable all EC events */
303 olpc_ec_mask_write(EC_SCI_SRC_ALL);
304
305 /* Power/battery status might have changed too */
306 battery_status_changed();
307 ac_status_changed();
308 return 0;
309}
310
311static int __devinit setup_sci_interrupt(struct platform_device *pdev)
312{
313 u32 lo, hi;
314 u32 sts;
315 int r;
316
317 rdmsr(0x51400020, lo, hi);
318 sci_irq = (lo >> 20) & 15;
319
320 if (sci_irq) {
321 dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
322 } else {
323 /* Zero means masked */
324 dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
325 sci_irq = 3;
326 lo |= 0x00300000;
327 wrmsrl(0x51400020, lo);
328 }
329
330 /* Select level triggered in PIC */
331 if (sci_irq < 8) {
332 lo = inb(CS5536_PIC_INT_SEL1);
333 lo |= 1 << sci_irq;
334 outb(lo, CS5536_PIC_INT_SEL1);
335 } else {
336 lo = inb(CS5536_PIC_INT_SEL2);
337 lo |= 1 << (sci_irq - 8);
338 outb(lo, CS5536_PIC_INT_SEL2);
339 }
340
341 /* Enable interesting SCI events, and clear pending interrupts */
342 sts = inl(acpi_base + CS5536_PM1_STS);
343 outl(((CS5536_PM_PWRBTN | CS5536_PM_RTC) << 16) | 0xffff,
344 acpi_base + CS5536_PM1_STS);
345
346 r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
347 if (r)
348 dev_err(&pdev->dev, "can't request interrupt\n");
349
350 return r;
351}
352
353static int __devinit setup_ec_sci(void)
354{
355 int r;
356
357 r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
358 if (r)
359 return r;
360
361 gpio_direction_input(OLPC_GPIO_ECSCI);
362
363 /* Clear pending EC SCI events */
364 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
365 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);
366
367 /*
368 * Enable EC SCI events, and map them to both a PME and the SCI
369 * interrupt.
370 *
371 * Ordinarily, in addition to functioning as GPIOs, Geode GPIOs can
372 * be mapped to regular interrupts *or* Geode-specific Power
373 * Management Events (PMEs) - events that bring the system out of
374 * suspend. In this case, we want both of those things - the system
375 * wakeup, *and* the ability to get an interrupt when an event occurs.
376 *
377 * To achieve this, we map the GPIO to a PME, and then we use one
378 * of the many generic knobs on the CS5535 PIC to additionally map the
379 * PME to the regular SCI interrupt line.
380 */
381 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
382
383 /* Set the SCI to cause a PME event on group 7 */
384 cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
385
386 /* And have group 7 also fire the SCI interrupt */
387 cs5535_pic_unreqz_select_high(7, sci_irq);
388
389 return 0;
390}
391
392static void free_ec_sci(void)
393{
394 gpio_free(OLPC_GPIO_ECSCI);
395}
396
397static int __devinit setup_lid_events(void)
398{
399 int r;
400
401 r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
402 if (r)
403 return r;
404
405 gpio_direction_input(OLPC_GPIO_LID);
406
407 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
408 lid_inverted = 0;
409
410 /* Clear edge detection and event enable for now */
411 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
412 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
413 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);
414 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
415 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
416
417 /* Set the LID to cause an PME event on group 6 */
418 cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
419
420 /* Set PME group 6 to fire the SCI interrupt */
421 cs5535_gpio_set_irq(6, sci_irq);
422
423 /* Enable the event */
424 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
425
426 return 0;
427}
428
429static void free_lid_events(void)
430{
431 gpio_free(OLPC_GPIO_LID);
432}
433
434static int __devinit setup_power_button(struct platform_device *pdev)
435{
436 int r;
437
438 power_button_idev = input_allocate_device();
439 if (!power_button_idev)
440 return -ENOMEM;
441
442 power_button_idev->name = "Power Button";
443 power_button_idev->phys = DRV_NAME "/input0";
444 set_bit(EV_KEY, power_button_idev->evbit);
445 set_bit(KEY_POWER, power_button_idev->keybit);
446
447 power_button_idev->dev.parent = &pdev->dev;
448 device_init_wakeup(&power_button_idev->dev, 1);
449
450 r = input_register_device(power_button_idev);
451 if (r) {
452 dev_err(&pdev->dev, "failed to register power button: %d\n", r);
453 input_free_device(power_button_idev);
454 }
455
456 return r;
457}
458
459static void free_power_button(void)
460{
461 input_unregister_device(power_button_idev);
462 input_free_device(power_button_idev);
463}
464
465static int __devinit setup_ebook_switch(struct platform_device *pdev)
466{
467 int r;
468
469 ebook_switch_idev = input_allocate_device();
470 if (!ebook_switch_idev)
471 return -ENOMEM;
472
473 ebook_switch_idev->name = "EBook Switch";
474 ebook_switch_idev->phys = DRV_NAME "/input1";
475 set_bit(EV_SW, ebook_switch_idev->evbit);
476 set_bit(SW_TABLET_MODE, ebook_switch_idev->swbit);
477
478 ebook_switch_idev->dev.parent = &pdev->dev;
479 device_set_wakeup_capable(&ebook_switch_idev->dev, true);
480
481 r = input_register_device(ebook_switch_idev);
482 if (r) {
483 dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
484 input_free_device(ebook_switch_idev);
485 }
486
487 return r;
488}
489
490static void free_ebook_switch(void)
491{
492 input_unregister_device(ebook_switch_idev);
493 input_free_device(ebook_switch_idev);
494}
495
496static int __devinit setup_lid_switch(struct platform_device *pdev)
497{
498 int r;
499
500 lid_switch_idev = input_allocate_device();
501 if (!lid_switch_idev)
502 return -ENOMEM;
503
504 lid_switch_idev->name = "Lid Switch";
505 lid_switch_idev->phys = DRV_NAME "/input2";
506 set_bit(EV_SW, lid_switch_idev->evbit);
507 set_bit(SW_LID, lid_switch_idev->swbit);
508
509 lid_switch_idev->dev.parent = &pdev->dev;
510 device_set_wakeup_capable(&lid_switch_idev->dev, true);
511
512 r = input_register_device(lid_switch_idev);
513 if (r) {
514 dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
515 goto err_register;
516 }
517
518 r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
519 if (r) {
520 dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
521 goto err_create_attr;
522 }
523
524 return 0;
525
526err_create_attr:
527 input_unregister_device(lid_switch_idev);
528err_register:
529 input_free_device(lid_switch_idev);
530 return r;
531}
532
533static void free_lid_switch(void)
534{
535 device_remove_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
536 input_unregister_device(lid_switch_idev);
537 input_free_device(lid_switch_idev);
538}
539
540static int __devinit xo1_sci_probe(struct platform_device *pdev)
541{
542 struct resource *res;
543 int r;
544
545 /* don't run on non-XOs */
546 if (!machine_is_olpc())
547 return -ENODEV;
548
549 r = mfd_cell_enable(pdev);
550 if (r)
551 return r;
552
553 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
554 if (!res) {
555 dev_err(&pdev->dev, "can't fetch device resource info\n");
556 return -EIO;
557 }
558 acpi_base = res->start;
559
560 r = setup_power_button(pdev);
561 if (r)
562 return r;
563
564 r = setup_ebook_switch(pdev);
565 if (r)
566 goto err_ebook;
567
568 r = setup_lid_switch(pdev);
569 if (r)
570 goto err_lid;
571
572 r = setup_lid_events();
573 if (r)
574 goto err_lidevt;
575
576 r = setup_ec_sci();
577 if (r)
578 goto err_ecsci;
579
580 /* Enable PME generation for EC-generated events */
581 outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
582 acpi_base + CS5536_PM_GPE0_EN);
583
584 /* Clear pending events */
585 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
586 process_sci_queue(false);
587
588 /* Initial sync */
589 send_ebook_state();
590 detect_lid_state();
591 send_lid_state();
592
593 r = setup_sci_interrupt(pdev);
594 if (r)
595 goto err_sci;
596
597 /* Enable all EC events */
598 olpc_ec_mask_write(EC_SCI_SRC_ALL);
599
600 return r;
601
602err_sci:
603 free_ec_sci();
604err_ecsci:
605 free_lid_events();
606err_lidevt:
607 free_lid_switch();
608err_lid:
609 free_ebook_switch();
610err_ebook:
611 free_power_button();
612 return r;
613}
614
615static int __devexit xo1_sci_remove(struct platform_device *pdev)
616{
617 mfd_cell_disable(pdev);
618 free_irq(sci_irq, pdev);
619 cancel_work_sync(&sci_work);
620 free_ec_sci();
621 free_lid_events();
622 free_lid_switch();
623 free_ebook_switch();
624 free_power_button();
625 acpi_base = 0;
626 return 0;
627}
628
629static struct platform_driver xo1_sci_driver = {
630 .driver = {
631 .name = "olpc-xo1-sci-acpi",
632 },
633 .probe = xo1_sci_probe,
634 .remove = __devexit_p(xo1_sci_remove),
635 .suspend = xo1_sci_suspend,
636 .resume = xo1_sci_resume,
637};
638
639static int __init xo1_sci_init(void)
640{
641 return platform_driver_register(&xo1_sci_driver);
642}
643arch_initcall(xo1_sci_init);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Support for OLPC XO-1 System Control Interrupts (SCI)
4 *
5 * Copyright (C) 2010 One Laptop per Child
6 * Copyright (C) 2006 Red Hat, Inc.
7 * Copyright (C) 2006 Advanced Micro Devices, Inc.
8 */
9
10#include <linux/cs5535.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
13#include <linux/input.h>
14#include <linux/interrupt.h>
15#include <linux/platform_device.h>
16#include <linux/pm.h>
17#include <linux/pm_wakeup.h>
18#include <linux/power_supply.h>
19#include <linux/suspend.h>
20#include <linux/workqueue.h>
21#include <linux/olpc-ec.h>
22
23#include <asm/io.h>
24#include <asm/msr.h>
25#include <asm/olpc.h>
26
27#define DRV_NAME "olpc-xo1-sci"
28#define PFX DRV_NAME ": "
29
30static unsigned long acpi_base;
31static struct input_dev *power_button_idev;
32static struct input_dev *ebook_switch_idev;
33static struct input_dev *lid_switch_idev;
34
35static int sci_irq;
36
37static bool lid_open;
38static bool lid_inverted;
39static int lid_wake_mode;
40
41enum lid_wake_modes {
42 LID_WAKE_ALWAYS,
43 LID_WAKE_OPEN,
44 LID_WAKE_CLOSE,
45};
46
47static const char * const lid_wake_mode_names[] = {
48 [LID_WAKE_ALWAYS] = "always",
49 [LID_WAKE_OPEN] = "open",
50 [LID_WAKE_CLOSE] = "close",
51};
52
53static void battery_status_changed(void)
54{
55 struct power_supply *psy = power_supply_get_by_name("olpc_battery");
56
57 if (psy) {
58 power_supply_changed(psy);
59 power_supply_put(psy);
60 }
61}
62
63static void ac_status_changed(void)
64{
65 struct power_supply *psy = power_supply_get_by_name("olpc_ac");
66
67 if (psy) {
68 power_supply_changed(psy);
69 power_supply_put(psy);
70 }
71}
72
73/* Report current ebook switch state through input layer */
74static void send_ebook_state(void)
75{
76 unsigned char state;
77
78 if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
79 pr_err(PFX "failed to get ebook state\n");
80 return;
81 }
82
83 if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
84 return; /* Nothing new to report. */
85
86 input_report_switch(ebook_switch_idev, SW_TABLET_MODE, state);
87 input_sync(ebook_switch_idev);
88 pm_wakeup_event(&ebook_switch_idev->dev, 0);
89}
90
91static void flip_lid_inverter(void)
92{
93 /* gpio is high; invert so we'll get l->h event interrupt */
94 if (lid_inverted)
95 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
96 else
97 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
98 lid_inverted = !lid_inverted;
99}
100
101static void detect_lid_state(void)
102{
103 /*
104 * the edge detector hookup on the gpio inputs on the geode is
105 * odd, to say the least. See http://dev.laptop.org/ticket/5703
106 * for details, but in a nutshell: we don't use the edge
107 * detectors. instead, we make use of an anomaly: with the both
108 * edge detectors turned off, we still get an edge event on a
109 * positive edge transition. to take advantage of this, we use the
110 * front-end inverter to ensure that that's the edge we're always
111 * going to see next.
112 */
113
114 int state;
115
116 state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
117 lid_open = !state ^ !lid_inverted; /* x ^^ y */
118 if (!state)
119 return;
120
121 flip_lid_inverter();
122}
123
124/* Report current lid switch state through input layer */
125static void send_lid_state(void)
126{
127 if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
128 return; /* Nothing new to report. */
129
130 input_report_switch(lid_switch_idev, SW_LID, !lid_open);
131 input_sync(lid_switch_idev);
132 pm_wakeup_event(&lid_switch_idev->dev, 0);
133}
134
135static ssize_t lid_wake_mode_show(struct device *dev,
136 struct device_attribute *attr, char *buf)
137{
138 const char *mode = lid_wake_mode_names[lid_wake_mode];
139 return sprintf(buf, "%s\n", mode);
140}
141static ssize_t lid_wake_mode_set(struct device *dev,
142 struct device_attribute *attr,
143 const char *buf, size_t count)
144{
145 int i;
146 for (i = 0; i < ARRAY_SIZE(lid_wake_mode_names); i++) {
147 const char *mode = lid_wake_mode_names[i];
148 if (strlen(mode) != count || strncasecmp(mode, buf, count))
149 continue;
150
151 lid_wake_mode = i;
152 return count;
153 }
154 return -EINVAL;
155}
156static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
157 lid_wake_mode_set);
158
159static struct attribute *lid_attrs[] = {
160 &dev_attr_lid_wake_mode.attr,
161 NULL,
162};
163ATTRIBUTE_GROUPS(lid);
164
165/*
166 * Process all items in the EC's SCI queue.
167 *
168 * This is handled in a workqueue because olpc_ec_cmd can be slow (and
169 * can even timeout).
170 *
171 * If propagate_events is false, the queue is drained without events being
172 * generated for the interrupts.
173 */
174static void process_sci_queue(bool propagate_events)
175{
176 int r;
177 u16 data;
178
179 do {
180 r = olpc_ec_sci_query(&data);
181 if (r || !data)
182 break;
183
184 pr_debug(PFX "SCI 0x%x received\n", data);
185
186 switch (data) {
187 case EC_SCI_SRC_BATERR:
188 case EC_SCI_SRC_BATSOC:
189 case EC_SCI_SRC_BATTERY:
190 case EC_SCI_SRC_BATCRIT:
191 battery_status_changed();
192 break;
193 case EC_SCI_SRC_ACPWR:
194 ac_status_changed();
195 break;
196 }
197
198 if (data == EC_SCI_SRC_EBOOK && propagate_events)
199 send_ebook_state();
200 } while (data);
201
202 if (r)
203 pr_err(PFX "Failed to clear SCI queue");
204}
205
206static void process_sci_queue_work(struct work_struct *work)
207{
208 process_sci_queue(true);
209}
210
211static DECLARE_WORK(sci_work, process_sci_queue_work);
212
213static irqreturn_t xo1_sci_intr(int irq, void *dev_id)
214{
215 struct platform_device *pdev = dev_id;
216 u32 sts;
217 u32 gpe;
218
219 sts = inl(acpi_base + CS5536_PM1_STS);
220 outl(sts | 0xffff, acpi_base + CS5536_PM1_STS);
221
222 gpe = inl(acpi_base + CS5536_PM_GPE0_STS);
223 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
224
225 dev_dbg(&pdev->dev, "sts %x gpe %x\n", sts, gpe);
226
227 if (sts & CS5536_PWRBTN_FLAG) {
228 if (!(sts & CS5536_WAK_FLAG)) {
229 /* Only report power button input when it was pressed
230 * during regular operation (as opposed to when it
231 * was used to wake the system). */
232 input_report_key(power_button_idev, KEY_POWER, 1);
233 input_sync(power_button_idev);
234 input_report_key(power_button_idev, KEY_POWER, 0);
235 input_sync(power_button_idev);
236 }
237 /* Report the wakeup event in all cases. */
238 pm_wakeup_event(&power_button_idev->dev, 0);
239 }
240
241 if ((sts & (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) ==
242 (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) {
243 /* When the system is woken by the RTC alarm, report the
244 * event on the rtc device. */
245 struct device *rtc = bus_find_device_by_name(
246 &platform_bus_type, NULL, "rtc_cmos");
247 if (rtc) {
248 pm_wakeup_event(rtc, 0);
249 put_device(rtc);
250 }
251 }
252
253 if (gpe & CS5536_GPIOM7_PME_FLAG) { /* EC GPIO */
254 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
255 schedule_work(&sci_work);
256 }
257
258 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
259 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
260 detect_lid_state();
261 send_lid_state();
262
263 return IRQ_HANDLED;
264}
265
266static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
267{
268 if (device_may_wakeup(&power_button_idev->dev))
269 olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
270 else
271 olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
272
273 if (device_may_wakeup(&ebook_switch_idev->dev))
274 olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
275 else
276 olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
277
278 if (!device_may_wakeup(&lid_switch_idev->dev)) {
279 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
280 } else if ((lid_open && lid_wake_mode == LID_WAKE_OPEN) ||
281 (!lid_open && lid_wake_mode == LID_WAKE_CLOSE)) {
282 flip_lid_inverter();
283
284 /* we may have just caused an event */
285 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
286 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
287
288 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
289 }
290
291 return 0;
292}
293
294static int xo1_sci_resume(struct platform_device *pdev)
295{
296 /*
297 * We don't know what may have happened while we were asleep.
298 * Reestablish our lid setup so we're sure to catch all transitions.
299 */
300 detect_lid_state();
301 send_lid_state();
302 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
303
304 /* Enable all EC events */
305 olpc_ec_mask_write(EC_SCI_SRC_ALL);
306
307 /* Power/battery status might have changed too */
308 battery_status_changed();
309 ac_status_changed();
310 return 0;
311}
312
313static int setup_sci_interrupt(struct platform_device *pdev)
314{
315 u32 lo, hi;
316 u32 sts;
317 int r;
318
319 rdmsr(0x51400020, lo, hi);
320 sci_irq = (lo >> 20) & 15;
321
322 if (sci_irq) {
323 dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
324 } else {
325 /* Zero means masked */
326 dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
327 sci_irq = 3;
328 lo |= 0x00300000;
329 wrmsrl(0x51400020, lo);
330 }
331
332 /* Select level triggered in PIC */
333 if (sci_irq < 8) {
334 lo = inb(CS5536_PIC_INT_SEL1);
335 lo |= 1 << sci_irq;
336 outb(lo, CS5536_PIC_INT_SEL1);
337 } else {
338 lo = inb(CS5536_PIC_INT_SEL2);
339 lo |= 1 << (sci_irq - 8);
340 outb(lo, CS5536_PIC_INT_SEL2);
341 }
342
343 /* Enable interesting SCI events, and clear pending interrupts */
344 sts = inl(acpi_base + CS5536_PM1_STS);
345 outl(((CS5536_PM_PWRBTN | CS5536_PM_RTC) << 16) | 0xffff,
346 acpi_base + CS5536_PM1_STS);
347
348 r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
349 if (r)
350 dev_err(&pdev->dev, "can't request interrupt\n");
351
352 return r;
353}
354
355static int setup_ec_sci(void)
356{
357 int r;
358
359 r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
360 if (r)
361 return r;
362
363 gpio_direction_input(OLPC_GPIO_ECSCI);
364
365 /* Clear pending EC SCI events */
366 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
367 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);
368
369 /*
370 * Enable EC SCI events, and map them to both a PME and the SCI
371 * interrupt.
372 *
373 * Ordinarily, in addition to functioning as GPIOs, Geode GPIOs can
374 * be mapped to regular interrupts *or* Geode-specific Power
375 * Management Events (PMEs) - events that bring the system out of
376 * suspend. In this case, we want both of those things - the system
377 * wakeup, *and* the ability to get an interrupt when an event occurs.
378 *
379 * To achieve this, we map the GPIO to a PME, and then we use one
380 * of the many generic knobs on the CS5535 PIC to additionally map the
381 * PME to the regular SCI interrupt line.
382 */
383 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
384
385 /* Set the SCI to cause a PME event on group 7 */
386 cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
387
388 /* And have group 7 also fire the SCI interrupt */
389 cs5535_pic_unreqz_select_high(7, sci_irq);
390
391 return 0;
392}
393
394static void free_ec_sci(void)
395{
396 gpio_free(OLPC_GPIO_ECSCI);
397}
398
399static int setup_lid_events(void)
400{
401 int r;
402
403 r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
404 if (r)
405 return r;
406
407 gpio_direction_input(OLPC_GPIO_LID);
408
409 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
410 lid_inverted = 0;
411
412 /* Clear edge detection and event enable for now */
413 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
414 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
415 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);
416 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
417 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
418
419 /* Set the LID to cause an PME event on group 6 */
420 cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
421
422 /* Set PME group 6 to fire the SCI interrupt */
423 cs5535_gpio_set_irq(6, sci_irq);
424
425 /* Enable the event */
426 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
427
428 return 0;
429}
430
431static void free_lid_events(void)
432{
433 gpio_free(OLPC_GPIO_LID);
434}
435
436static int setup_power_button(struct platform_device *pdev)
437{
438 int r;
439
440 power_button_idev = input_allocate_device();
441 if (!power_button_idev)
442 return -ENOMEM;
443
444 power_button_idev->name = "Power Button";
445 power_button_idev->phys = DRV_NAME "/input0";
446 set_bit(EV_KEY, power_button_idev->evbit);
447 set_bit(KEY_POWER, power_button_idev->keybit);
448
449 power_button_idev->dev.parent = &pdev->dev;
450 device_init_wakeup(&power_button_idev->dev, 1);
451
452 r = input_register_device(power_button_idev);
453 if (r) {
454 dev_err(&pdev->dev, "failed to register power button: %d\n", r);
455 input_free_device(power_button_idev);
456 }
457
458 return r;
459}
460
461static void free_power_button(void)
462{
463 input_unregister_device(power_button_idev);
464}
465
466static int setup_ebook_switch(struct platform_device *pdev)
467{
468 int r;
469
470 ebook_switch_idev = input_allocate_device();
471 if (!ebook_switch_idev)
472 return -ENOMEM;
473
474 ebook_switch_idev->name = "EBook Switch";
475 ebook_switch_idev->phys = DRV_NAME "/input1";
476 set_bit(EV_SW, ebook_switch_idev->evbit);
477 set_bit(SW_TABLET_MODE, ebook_switch_idev->swbit);
478
479 ebook_switch_idev->dev.parent = &pdev->dev;
480 device_set_wakeup_capable(&ebook_switch_idev->dev, true);
481
482 r = input_register_device(ebook_switch_idev);
483 if (r) {
484 dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
485 input_free_device(ebook_switch_idev);
486 }
487
488 return r;
489}
490
491static void free_ebook_switch(void)
492{
493 input_unregister_device(ebook_switch_idev);
494}
495
496static int setup_lid_switch(struct platform_device *pdev)
497{
498 int r;
499
500 lid_switch_idev = input_allocate_device();
501 if (!lid_switch_idev)
502 return -ENOMEM;
503
504 lid_switch_idev->name = "Lid Switch";
505 lid_switch_idev->phys = DRV_NAME "/input2";
506 set_bit(EV_SW, lid_switch_idev->evbit);
507 set_bit(SW_LID, lid_switch_idev->swbit);
508
509 lid_switch_idev->dev.parent = &pdev->dev;
510 device_set_wakeup_capable(&lid_switch_idev->dev, true);
511
512 r = input_register_device(lid_switch_idev);
513 if (r) {
514 dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
515 goto err_register;
516 }
517
518 return 0;
519
520err_register:
521 input_free_device(lid_switch_idev);
522 return r;
523}
524
525static void free_lid_switch(void)
526{
527 input_unregister_device(lid_switch_idev);
528}
529
530static int xo1_sci_probe(struct platform_device *pdev)
531{
532 struct resource *res;
533 int r;
534
535 /* don't run on non-XOs */
536 if (!machine_is_olpc())
537 return -ENODEV;
538
539 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
540 if (!res) {
541 dev_err(&pdev->dev, "can't fetch device resource info\n");
542 return -EIO;
543 }
544 acpi_base = res->start;
545
546 r = setup_power_button(pdev);
547 if (r)
548 return r;
549
550 r = setup_ebook_switch(pdev);
551 if (r)
552 goto err_ebook;
553
554 r = setup_lid_switch(pdev);
555 if (r)
556 goto err_lid;
557
558 r = setup_lid_events();
559 if (r)
560 goto err_lidevt;
561
562 r = setup_ec_sci();
563 if (r)
564 goto err_ecsci;
565
566 /* Enable PME generation for EC-generated events */
567 outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
568 acpi_base + CS5536_PM_GPE0_EN);
569
570 /* Clear pending events */
571 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
572 process_sci_queue(false);
573
574 /* Initial sync */
575 send_ebook_state();
576 detect_lid_state();
577 send_lid_state();
578
579 r = setup_sci_interrupt(pdev);
580 if (r)
581 goto err_sci;
582
583 /* Enable all EC events */
584 olpc_ec_mask_write(EC_SCI_SRC_ALL);
585
586 return r;
587
588err_sci:
589 free_ec_sci();
590err_ecsci:
591 free_lid_events();
592err_lidevt:
593 free_lid_switch();
594err_lid:
595 free_ebook_switch();
596err_ebook:
597 free_power_button();
598 return r;
599}
600
601static int xo1_sci_remove(struct platform_device *pdev)
602{
603 free_irq(sci_irq, pdev);
604 cancel_work_sync(&sci_work);
605 free_ec_sci();
606 free_lid_events();
607 free_lid_switch();
608 free_ebook_switch();
609 free_power_button();
610 acpi_base = 0;
611 return 0;
612}
613
614static struct platform_driver xo1_sci_driver = {
615 .driver = {
616 .name = "olpc-xo1-sci-acpi",
617 .dev_groups = lid_groups,
618 },
619 .probe = xo1_sci_probe,
620 .remove = xo1_sci_remove,
621 .suspend = xo1_sci_suspend,
622 .resume = xo1_sci_resume,
623};
624
625static int __init xo1_sci_init(void)
626{
627 return platform_driver_register(&xo1_sci_driver);
628}
629arch_initcall(xo1_sci_init);