olpc-xo15-sci.c 5.4 KB

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