olpc-xo15-sci.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Support for OLPC XO-1.5 System Control Interrupts (SCI)
  3. *
  4. * Copyright (C) 2009-2010 One Laptop per Child
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/power_supply.h>
  15. #include <acpi/acpi_bus.h>
  16. #include <acpi/acpi_drivers.h>
  17. #include <asm/olpc.h>
  18. #define DRV_NAME "olpc-xo15-sci"
  19. #define PFX DRV_NAME ": "
  20. #define XO15_SCI_CLASS DRV_NAME
  21. #define XO15_SCI_DEVICE_NAME "OLPC XO-1.5 SCI"
  22. static unsigned long xo15_sci_gpe;
  23. static void battery_status_changed(void)
  24. {
  25. struct power_supply *psy = power_supply_get_by_name("olpc-battery");
  26. if (psy) {
  27. power_supply_changed(psy);
  28. put_device(psy->dev);
  29. }
  30. }
  31. static void ac_status_changed(void)
  32. {
  33. struct power_supply *psy = power_supply_get_by_name("olpc-ac");
  34. if (psy) {
  35. power_supply_changed(psy);
  36. put_device(psy->dev);
  37. }
  38. }
  39. static void process_sci_queue(void)
  40. {
  41. u16 data;
  42. int r;
  43. do {
  44. r = olpc_ec_sci_query(&data);
  45. if (r || !data)
  46. break;
  47. pr_debug(PFX "SCI 0x%x received\n", data);
  48. switch (data) {
  49. case EC_SCI_SRC_BATERR:
  50. case EC_SCI_SRC_BATSOC:
  51. case EC_SCI_SRC_BATTERY:
  52. case EC_SCI_SRC_BATCRIT:
  53. battery_status_changed();
  54. break;
  55. case EC_SCI_SRC_ACPWR:
  56. ac_status_changed();
  57. break;
  58. }
  59. } while (data);
  60. if (r)
  61. pr_err(PFX "Failed to clear SCI queue");
  62. }
  63. static void process_sci_queue_work(struct work_struct *work)
  64. {
  65. process_sci_queue();
  66. }
  67. static DECLARE_WORK(sci_work, process_sci_queue_work);
  68. static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
  69. {
  70. schedule_work(&sci_work);
  71. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  72. }
  73. static int xo15_sci_add(struct acpi_device *device)
  74. {
  75. unsigned long long tmp;
  76. acpi_status status;
  77. if (!device)
  78. return -EINVAL;
  79. strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
  80. strcpy(acpi_device_class(device), XO15_SCI_CLASS);
  81. /* Get GPE bit assignment (EC events). */
  82. status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
  83. if (ACPI_FAILURE(status))
  84. return -EINVAL;
  85. xo15_sci_gpe = tmp;
  86. status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
  87. ACPI_GPE_EDGE_TRIGGERED,
  88. xo15_sci_gpe_handler, device);
  89. if (ACPI_FAILURE(status))
  90. return -ENODEV;
  91. dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
  92. /* Flush queue, and enable all SCI events */
  93. process_sci_queue();
  94. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  95. acpi_enable_gpe(NULL, xo15_sci_gpe);
  96. /* Enable wake-on-EC */
  97. if (device->wakeup.flags.valid)
  98. device_init_wakeup(&device->dev, true);
  99. return 0;
  100. }
  101. static int xo15_sci_remove(struct acpi_device *device, int type)
  102. {
  103. acpi_disable_gpe(NULL, xo15_sci_gpe);
  104. acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
  105. cancel_work_sync(&sci_work);
  106. return 0;
  107. }
  108. static int xo15_sci_resume(struct acpi_device *device)
  109. {
  110. /* Enable all EC events */
  111. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  112. /* Power/battery status might have changed */
  113. battery_status_changed();
  114. ac_status_changed();
  115. return 0;
  116. }
  117. static const struct acpi_device_id xo15_sci_device_ids[] = {
  118. {"XO15EC", 0},
  119. {"", 0},
  120. };
  121. static struct acpi_driver xo15_sci_drv = {
  122. .name = DRV_NAME,
  123. .class = XO15_SCI_CLASS,
  124. .ids = xo15_sci_device_ids,
  125. .ops = {
  126. .add = xo15_sci_add,
  127. .remove = xo15_sci_remove,
  128. .resume = xo15_sci_resume,
  129. },
  130. };
  131. static int __init xo15_sci_init(void)
  132. {
  133. return acpi_bus_register_driver(&xo15_sci_drv);
  134. }
  135. device_initcall(xo15_sci_init);