olpc-xo15-sci.c 5.3 KB

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