olpc-xo15-sci.c 5.1 KB

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