olpc-xo15-sci.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. struct acpi_object_list arg_list;
  39. union acpi_object arg;
  40. acpi_status status;
  41. arg_list.count = 1;
  42. arg_list.pointer = &arg;
  43. arg.type = ACPI_TYPE_INTEGER;
  44. arg.integer.value = wake_on_close;
  45. status = acpi_evaluate_object(NULL, "\\_SB.PCI0.LID.LIDW", &arg_list, NULL);
  46. if (ACPI_FAILURE(status)) {
  47. pr_warning(PFX "failed to set lid behavior\n");
  48. return 1;
  49. }
  50. lid_wake_on_close = wake_on_close;
  51. return 0;
  52. }
  53. static ssize_t
  54. lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
  55. {
  56. return sprintf(buf, "%u\n", lid_wake_on_close);
  57. }
  58. static ssize_t lid_wake_on_close_store(struct kobject *s,
  59. struct kobj_attribute *attr,
  60. const char *buf, size_t n)
  61. {
  62. unsigned int val;
  63. if (sscanf(buf, "%u", &val) != 1)
  64. return -EINVAL;
  65. set_lid_wake_behavior(!!val);
  66. return n;
  67. }
  68. static struct kobj_attribute lid_wake_on_close_attr =
  69. __ATTR(lid_wake_on_close, 0644,
  70. lid_wake_on_close_show,
  71. lid_wake_on_close_store);
  72. static void battery_status_changed(void)
  73. {
  74. struct power_supply *psy = power_supply_get_by_name("olpc-battery");
  75. if (psy) {
  76. power_supply_changed(psy);
  77. put_device(psy->dev);
  78. }
  79. }
  80. static void ac_status_changed(void)
  81. {
  82. struct power_supply *psy = power_supply_get_by_name("olpc-ac");
  83. if (psy) {
  84. power_supply_changed(psy);
  85. put_device(psy->dev);
  86. }
  87. }
  88. static void process_sci_queue(void)
  89. {
  90. u16 data;
  91. int r;
  92. do {
  93. r = olpc_ec_sci_query(&data);
  94. if (r || !data)
  95. break;
  96. pr_debug(PFX "SCI 0x%x received\n", data);
  97. switch (data) {
  98. case EC_SCI_SRC_BATERR:
  99. case EC_SCI_SRC_BATSOC:
  100. case EC_SCI_SRC_BATTERY:
  101. case EC_SCI_SRC_BATCRIT:
  102. battery_status_changed();
  103. break;
  104. case EC_SCI_SRC_ACPWR:
  105. ac_status_changed();
  106. break;
  107. }
  108. } while (data);
  109. if (r)
  110. pr_err(PFX "Failed to clear SCI queue");
  111. }
  112. static void process_sci_queue_work(struct work_struct *work)
  113. {
  114. process_sci_queue();
  115. }
  116. static DECLARE_WORK(sci_work, process_sci_queue_work);
  117. static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
  118. {
  119. schedule_work(&sci_work);
  120. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  121. }
  122. static int xo15_sci_add(struct acpi_device *device)
  123. {
  124. unsigned long long tmp;
  125. acpi_status status;
  126. int r;
  127. if (!device)
  128. return -EINVAL;
  129. strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
  130. strcpy(acpi_device_class(device), XO15_SCI_CLASS);
  131. /* Get GPE bit assignment (EC events). */
  132. status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
  133. if (ACPI_FAILURE(status))
  134. return -EINVAL;
  135. xo15_sci_gpe = tmp;
  136. status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
  137. ACPI_GPE_EDGE_TRIGGERED,
  138. xo15_sci_gpe_handler, device);
  139. if (ACPI_FAILURE(status))
  140. return -ENODEV;
  141. dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
  142. r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
  143. if (r)
  144. goto err_sysfs;
  145. /* Flush queue, and enable all SCI events */
  146. process_sci_queue();
  147. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  148. acpi_enable_gpe(NULL, xo15_sci_gpe);
  149. /* Enable wake-on-EC */
  150. if (device->wakeup.flags.valid)
  151. device_init_wakeup(&device->dev, true);
  152. return 0;
  153. err_sysfs:
  154. acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
  155. cancel_work_sync(&sci_work);
  156. return r;
  157. }
  158. static int xo15_sci_remove(struct acpi_device *device)
  159. {
  160. acpi_disable_gpe(NULL, xo15_sci_gpe);
  161. acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
  162. cancel_work_sync(&sci_work);
  163. sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
  164. return 0;
  165. }
  166. static int xo15_sci_resume(struct device *dev)
  167. {
  168. /* Enable all EC events */
  169. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  170. /* Power/battery status might have changed */
  171. battery_status_changed();
  172. ac_status_changed();
  173. return 0;
  174. }
  175. static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
  176. static const struct acpi_device_id xo15_sci_device_ids[] = {
  177. {"XO15EC", 0},
  178. {"", 0},
  179. };
  180. static struct acpi_driver xo15_sci_drv = {
  181. .name = DRV_NAME,
  182. .class = XO15_SCI_CLASS,
  183. .ids = xo15_sci_device_ids,
  184. .ops = {
  185. .add = xo15_sci_add,
  186. .remove = xo15_sci_remove,
  187. },
  188. .drv.pm = &xo15_sci_pm,
  189. };
  190. static int __init xo15_sci_init(void)
  191. {
  192. return acpi_bus_register_driver(&xo15_sci_drv);
  193. }
  194. device_initcall(xo15_sci_init);