2 * Support for OLPC XO-1 System Control Interrupts (SCI)
4 * Copyright (C) 2010 One Laptop per Child
5 * Copyright (C) 2006 Red Hat, Inc.
6 * Copyright (C) 2006 Advanced Micro Devices, Inc.
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.
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>
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>
31 #define DRV_NAME "olpc-xo1-sci"
32 #define PFX DRV_NAME ": "
34 static unsigned long acpi_base;
35 static struct input_dev *power_button_idev;
36 static struct input_dev *ebook_switch_idev;
37 static struct input_dev *lid_switch_idev;
42 static bool lid_inverted;
43 static int lid_wake_mode;
51 static const char * const lid_wake_mode_names[] = {
52 [LID_WAKE_ALWAYS] = "always",
53 [LID_WAKE_OPEN] = "open",
54 [LID_WAKE_CLOSE] = "close",
57 static void battery_status_changed(void)
59 struct power_supply *psy = power_supply_get_by_name("olpc-battery");
62 power_supply_changed(psy);
67 static void ac_status_changed(void)
69 struct power_supply *psy = power_supply_get_by_name("olpc-ac");
72 power_supply_changed(psy);
77 /* Report current ebook switch state through input layer */
78 static void send_ebook_state(void)
82 if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
83 pr_err(PFX "failed to get ebook state\n");
87 if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
88 return; /* Nothing new to report. */
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);
95 static void flip_lid_inverter(void)
97 /* gpio is high; invert so we'll get l->h event interrupt */
99 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
101 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
102 lid_inverted = !lid_inverted;
105 static void detect_lid_state(void)
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
120 state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
121 lid_open = !state ^ !lid_inverted; /* x ^^ y */
128 /* Report current lid switch state through input layer */
129 static void send_lid_state(void)
131 if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
132 return; /* Nothing new to report. */
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);
139 static ssize_t lid_wake_mode_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
142 const char *mode = lid_wake_mode_names[lid_wake_mode];
143 return sprintf(buf, "%s\n", mode);
145 static ssize_t lid_wake_mode_set(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
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))
160 static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
164 * Process all items in the EC's SCI queue.
166 * This is handled in a workqueue because olpc_ec_cmd can be slow (and
169 * If propagate_events is false, the queue is drained without events being
170 * generated for the interrupts.
172 static void process_sci_queue(bool propagate_events)
178 r = olpc_ec_sci_query(&data);
182 pr_debug(PFX "SCI 0x%x received\n", 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();
191 case EC_SCI_SRC_ACPWR:
196 if (data == EC_SCI_SRC_EBOOK && propagate_events)
201 pr_err(PFX "Failed to clear SCI queue");
204 static void process_sci_queue_work(struct work_struct *work)
206 process_sci_queue(true);
209 static DECLARE_WORK(sci_work, process_sci_queue_work);
211 static irqreturn_t xo1_sci_intr(int irq, void *dev_id)
213 struct platform_device *pdev = dev_id;
217 sts = inl(acpi_base + CS5536_PM1_STS);
218 outl(sts | 0xffff, acpi_base + CS5536_PM1_STS);
220 gpe = inl(acpi_base + CS5536_PM_GPE0_STS);
221 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
223 dev_dbg(&pdev->dev, "sts %x gpe %x\n", sts, gpe);
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);
235 /* Report the wakeup event in all cases. */
236 pm_wakeup_event(&power_button_idev->dev, 0);
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");
246 pm_wakeup_event(rtc, 0);
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);
256 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
257 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
264 static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
266 if (device_may_wakeup(&power_button_idev->dev))
267 olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
269 olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
271 if (device_may_wakeup(&ebook_switch_idev->dev))
272 olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
274 olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
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)) {
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);
286 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
292 static int xo1_sci_resume(struct platform_device *pdev)
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.
300 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
302 /* Enable all EC events */
303 olpc_ec_mask_write(EC_SCI_SRC_ALL);
305 /* Power/battery status might have changed too */
306 battery_status_changed();
311 static int __devinit setup_sci_interrupt(struct platform_device *pdev)
317 rdmsr(0x51400020, lo, hi);
318 sci_irq = (lo >> 20) & 15;
321 dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
323 /* Zero means masked */
324 dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
327 wrmsrl(0x51400020, lo);
330 /* Select level triggered in PIC */
332 lo = inb(CS5536_PIC_INT_SEL1);
334 outb(lo, CS5536_PIC_INT_SEL1);
336 lo = inb(CS5536_PIC_INT_SEL2);
337 lo |= 1 << (sci_irq - 8);
338 outb(lo, CS5536_PIC_INT_SEL2);
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);
346 r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
348 dev_err(&pdev->dev, "can't request interrupt\n");
353 static int __devinit setup_ec_sci(void)
357 r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
361 gpio_direction_input(OLPC_GPIO_ECSCI);
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);
368 * Enable EC SCI events, and map them to both a PME and the SCI
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.
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.
381 cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
383 /* Set the SCI to cause a PME event on group 7 */
384 cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
386 /* And have group 7 also fire the SCI interrupt */
387 cs5535_pic_unreqz_select_high(7, sci_irq);
392 static void free_ec_sci(void)
394 gpio_free(OLPC_GPIO_ECSCI);
397 static int __devinit setup_lid_events(void)
401 r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
405 gpio_direction_input(OLPC_GPIO_LID);
407 cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
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);
417 /* Set the LID to cause an PME event on group 6 */
418 cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
420 /* Set PME group 6 to fire the SCI interrupt */
421 cs5535_gpio_set_irq(6, sci_irq);
423 /* Enable the event */
424 cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
429 static void free_lid_events(void)
431 gpio_free(OLPC_GPIO_LID);
434 static int __devinit setup_power_button(struct platform_device *pdev)
438 power_button_idev = input_allocate_device();
439 if (!power_button_idev)
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);
447 power_button_idev->dev.parent = &pdev->dev;
448 device_init_wakeup(&power_button_idev->dev, 1);
450 r = input_register_device(power_button_idev);
452 dev_err(&pdev->dev, "failed to register power button: %d\n", r);
453 input_free_device(power_button_idev);
459 static void free_power_button(void)
461 input_unregister_device(power_button_idev);
462 input_free_device(power_button_idev);
465 static int __devinit setup_ebook_switch(struct platform_device *pdev)
469 ebook_switch_idev = input_allocate_device();
470 if (!ebook_switch_idev)
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);
478 ebook_switch_idev->dev.parent = &pdev->dev;
479 device_set_wakeup_capable(&ebook_switch_idev->dev, true);
481 r = input_register_device(ebook_switch_idev);
483 dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
484 input_free_device(ebook_switch_idev);
490 static void free_ebook_switch(void)
492 input_unregister_device(ebook_switch_idev);
493 input_free_device(ebook_switch_idev);
496 static int __devinit setup_lid_switch(struct platform_device *pdev)
500 lid_switch_idev = input_allocate_device();
501 if (!lid_switch_idev)
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);
509 lid_switch_idev->dev.parent = &pdev->dev;
510 device_set_wakeup_capable(&lid_switch_idev->dev, true);
512 r = input_register_device(lid_switch_idev);
514 dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
518 r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
520 dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
521 goto err_create_attr;
527 input_unregister_device(lid_switch_idev);
529 input_free_device(lid_switch_idev);
533 static void free_lid_switch(void)
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);
540 static int __devinit xo1_sci_probe(struct platform_device *pdev)
542 struct resource *res;
545 /* don't run on non-XOs */
546 if (!machine_is_olpc())
549 r = mfd_cell_enable(pdev);
553 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
555 dev_err(&pdev->dev, "can't fetch device resource info\n");
558 acpi_base = res->start;
560 r = setup_power_button(pdev);
564 r = setup_ebook_switch(pdev);
568 r = setup_lid_switch(pdev);
572 r = setup_lid_events();
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);
584 /* Clear pending events */
585 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
586 process_sci_queue(false);
593 r = setup_sci_interrupt(pdev);
597 /* Enable all EC events */
598 olpc_ec_mask_write(EC_SCI_SRC_ALL);
615 static int __devexit xo1_sci_remove(struct platform_device *pdev)
617 mfd_cell_disable(pdev);
618 free_irq(sci_irq, pdev);
619 cancel_work_sync(&sci_work);
629 static struct platform_driver xo1_sci_driver = {
631 .name = "olpc-xo1-sci-acpi",
633 .probe = xo1_sci_probe,
634 .remove = __devexit_p(xo1_sci_remove),
635 .suspend = xo1_sci_suspend,
636 .resume = xo1_sci_resume,
639 static int __init xo1_sci_init(void)
641 return platform_driver_register(&xo1_sci_driver);
643 arch_initcall(xo1_sci_init);