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 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; 38 39 static int sci_irq; 40 41 static bool lid_open; 42 static bool lid_inverted; 43 static int lid_wake_mode; 44 45 enum lid_wake_modes { 46 LID_WAKE_ALWAYS, 47 LID_WAKE_OPEN, 48 LID_WAKE_CLOSE, 49 }; 50 51 static const char * const lid_wake_mode_names[] = { 52 [LID_WAKE_ALWAYS] = "always", 53 [LID_WAKE_OPEN] = "open", 54 [LID_WAKE_CLOSE] = "close", 55 }; 56 57 static 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 67 static 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 */ 78 static 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 95 static 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 105 static 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 */ 129 static 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 139 static 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 } 145 static 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 } 160 static 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 */ 172 static 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 204 static void process_sci_queue_work(struct work_struct *work) 205 { 206 process_sci_queue(true); 207 } 208 209 static DECLARE_WORK(sci_work, process_sci_queue_work); 210 211 static 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 264 static 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 292 static 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 311 static 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 353 static 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 392 static void free_ec_sci(void) 393 { 394 gpio_free(OLPC_GPIO_ECSCI); 395 } 396 397 static 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 429 static void free_lid_events(void) 430 { 431 gpio_free(OLPC_GPIO_LID); 432 } 433 434 static 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 459 static void free_power_button(void) 460 { 461 input_unregister_device(power_button_idev); 462 input_free_device(power_button_idev); 463 } 464 465 static 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 490 static void free_ebook_switch(void) 491 { 492 input_unregister_device(ebook_switch_idev); 493 input_free_device(ebook_switch_idev); 494 } 495 496 static 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 526 err_create_attr: 527 input_unregister_device(lid_switch_idev); 528 err_register: 529 input_free_device(lid_switch_idev); 530 return r; 531 } 532 533 static 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 540 static 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 602 err_sci: 603 free_ec_sci(); 604 err_ecsci: 605 free_lid_events(); 606 err_lidevt: 607 free_lid_switch(); 608 err_lid: 609 free_ebook_switch(); 610 err_ebook: 611 free_power_button(); 612 return r; 613 } 614 615 static 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 629 static 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 639 static int __init xo1_sci_init(void) 640 { 641 return platform_driver_register(&xo1_sci_driver); 642 } 643 arch_initcall(xo1_sci_init); 644