olpc-xo15-sci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <linux/olpc-ec.h>
  16. #include <linux/acpi.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 bool lid_wake_on_close;
  24. /*
  25. * The normal ACPI LID wakeup behavior is wake-on-open, but not
  26. * wake-on-close. This is implemented as standard by the XO-1.5 DSDT.
  27. *
  28. * We provide here a sysfs attribute that will additionally enable
  29. * wake-on-close behavior. This is useful (e.g.) when we oportunistically
  30. * suspend with the display running; if the lid is then closed, we want to
  31. * wake up to turn the display off.
  32. *
  33. * This is controlled through a custom method in the XO-1.5 DSDT.
  34. */
  35. static int set_lid_wake_behavior(bool wake_on_close)
  36. {
  37. acpi_status status;
  38. status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close);
  39. if (ACPI_FAILURE(status)) {
  40. pr_warning(PFX "failed to set lid behavior\n");
  41. return 1;
  42. }
  43. lid_wake_on_close = wake_on_close;
  44. return 0;
  45. }
  46. static ssize_t
  47. lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
  48. {
  49. return sprintf(buf, "%u\n", lid_wake_on_close);
  50. }
  51. static ssize_t lid_wake_on_close_store(struct kobject *s,
  52. struct kobj_attribute *attr,
  53. const char *buf, size_t n)
  54. {
  55. unsigned int val;
  56. if (sscanf(buf, "%u", &val) != 1)
  57. return -EINVAL;
  58. set_lid_wake_behavior(!!val);
  59. return n;
  60. }
  61. static struct kobj_attribute lid_wake_on_close_attr =
  62. __ATTR(lid_wake_on_close, 0644,
  63. lid_wake_on_close_show,
  64. lid_wake_on_close_store);
  65. static void battery_status_changed(void)
  66. {
  67. struct power_supply *psy = power_supply_get_by_name("olpc-battery");
  68. if (psy) {
  69. power_supply_changed(psy);
  70. power_supply_put(psy);
  71. }
  72. }
  73. static void ac_status_changed(void)
  74. {
  75. struct power_supply *psy = power_supply_get_by_name("olpc-ac");
  76. if (psy) {
  77. power_supply_changed(psy);
  78. power_supply_put(psy);
  79. }
  80. }
  81. static void process_sci_queue(void)
  82. {
  83. u16 data;
  84. int r;
  85. do {
  86. r = olpc_ec_sci_query(&data);
  87. if (r || !data)
  88. break;
  89. pr_debug(PFX "SCI 0x%x received\n", data);
  90. switch (data) {
  91. case EC_SCI_SRC_BATERR:
  92. case EC_SCI_SRC_BATSOC:
  93. case EC_SCI_SRC_BATTERY:
  94. case EC_SCI_SRC_BATCRIT:
  95. battery_status_changed();
  96. break;
  97. case EC_SCI_SRC_ACPWR:
  98. ac_status_changed();
  99. break;
  100. }
  101. } while (data);
  102. if (r)
  103. pr_err(PFX "Failed to clear SCI queue");
  104. }
  105. static void process_sci_queue_work(struct work_struct *work)
  106. {
  107. process_sci_queue();
  108. }
  109. static DECLARE_WORK(sci_work, process_sci_queue_work);
  110. static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
  111. {
  112. schedule_work(&sci_work);
  113. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  114. }
  115. static int xo15_sci_add(struct acpi_device *device)
  116. {
  117. unsigned long long tmp;
  118. acpi_status status;
  119. int r;
  120. if (!device)
  121. return -EINVAL;
  122. strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
  123. strcpy(acpi_device_class(device), XO15_SCI_CLASS);
  124. /* Get GPE bit assignment (EC events). */
  125. status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
  126. if (ACPI_FAILURE(status))
  127. return -EINVAL;
  128. xo15_sci_gpe = tmp;
  129. status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
  130. ACPI_GPE_EDGE_TRIGGERED,
  131. xo15_sci_gpe_handler, device);
  132. if (ACPI_FAILURE(status))
  133. return -ENODEV;
  134. dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
  135. r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
  136. if (r)
  137. goto err_sysfs;
  138. /* Flush queue, and enable all SCI events */
  139. process_sci_queue();
  140. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  141. acpi_enable_gpe(NULL, xo15_sci_gpe);
  142. /* Enable wake-on-EC */
  143. if (device->wakeup.flags.valid)
  144. device_init_wakeup(&device->dev, true);
  145. return 0;
  146. err_sysfs:
  147. acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
  148. cancel_work_sync(&sci_work);
  149. return r;
  150. }
  151. static int xo15_sci_remove(struct acpi_device *device)
  152. {
  153. acpi_disable_gpe(NULL, xo15_sci_gpe);
  154. acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
  155. cancel_work_sync(&sci_work);
  156. sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
  157. return 0;
  158. }
  159. #ifdef CONFIG_PM_SLEEP
  160. static int xo15_sci_resume(struct device *dev)
  161. {
  162. /* Enable all EC events */
  163. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  164. /* Power/battery status might have changed */
  165. battery_status_changed();
  166. ac_status_changed();
  167. return 0;
  168. }
  169. #endif
  170. static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
  171. static const struct acpi_device_id xo15_sci_device_ids[] = {
  172. {"XO15EC", 0},
  173. {"", 0},
  174. };
  175. static struct acpi_driver xo15_sci_drv = {
  176. .name = DRV_NAME,
  177. .class = XO15_SCI_CLASS,
  178. .ids = xo15_sci_device_ids,
  179. .ops = {
  180. .add = xo15_sci_add,
  181. .remove = xo15_sci_remove,
  182. },
  183. .drv.pm = &xo15_sci_pm,
  184. };
  185. static int __init xo15_sci_init(void)
  186. {
  187. return acpi_bus_register_driver(&xo15_sci_drv);
  188. }
  189. device_initcall(xo15_sci_init);