acpi_tad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ACPI Time and Alarm (TAD) Device Driver
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  7. *
  8. * This driver is based on Section 9.18 of the ACPI 6.2 specification revision.
  9. *
  10. * It only supports the system wakeup capabilities of the TAD.
  11. *
  12. * Provided are sysfs attributes, available under the TAD platform device,
  13. * allowing user space to manage the AC and DC wakeup timers of the TAD:
  14. * set and read their values, set and check their expire timer wake policies,
  15. * check and clear their status and check the capabilities of the TAD reported
  16. * by AML. The DC timer attributes are only present if the TAD supports a
  17. * separate DC alarm timer.
  18. *
  19. * The wakeup events handling and power management of the TAD is expected to
  20. * be taken care of by the ACPI PM domain attached to its platform device.
  21. */
  22. #include <linux/acpi.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/suspend.h>
  28. MODULE_LICENSE("GPL v2");
  29. MODULE_AUTHOR("Rafael J. Wysocki");
  30. /* ACPI TAD capability flags (ACPI 6.2, Section 9.18.2) */
  31. #define ACPI_TAD_AC_WAKE BIT(0)
  32. #define ACPI_TAD_DC_WAKE BIT(1)
  33. #define ACPI_TAD_RT BIT(2)
  34. #define ACPI_TAD_RT_IN_MS BIT(3)
  35. #define ACPI_TAD_S4_S5__GWS BIT(4)
  36. #define ACPI_TAD_AC_S4_WAKE BIT(5)
  37. #define ACPI_TAD_AC_S5_WAKE BIT(6)
  38. #define ACPI_TAD_DC_S4_WAKE BIT(7)
  39. #define ACPI_TAD_DC_S5_WAKE BIT(8)
  40. /* ACPI TAD alarm timer selection */
  41. #define ACPI_TAD_AC_TIMER (u32)0
  42. #define ACPI_TAD_DC_TIMER (u32)1
  43. /* Special value for disabled timer or expired timer wake policy. */
  44. #define ACPI_TAD_WAKE_DISABLED (~(u32)0)
  45. struct acpi_tad_driver_data {
  46. u32 capabilities;
  47. };
  48. static int acpi_tad_wake_set(struct device *dev, char *method, u32 timer_id,
  49. u32 value)
  50. {
  51. acpi_handle handle = ACPI_HANDLE(dev);
  52. union acpi_object args[] = {
  53. { .type = ACPI_TYPE_INTEGER, },
  54. { .type = ACPI_TYPE_INTEGER, },
  55. };
  56. struct acpi_object_list arg_list = {
  57. .pointer = args,
  58. .count = ARRAY_SIZE(args),
  59. };
  60. unsigned long long retval;
  61. acpi_status status;
  62. args[0].integer.value = timer_id;
  63. args[1].integer.value = value;
  64. pm_runtime_get_sync(dev);
  65. status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
  66. pm_runtime_put_sync(dev);
  67. if (ACPI_FAILURE(status) || retval)
  68. return -EIO;
  69. return 0;
  70. }
  71. static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
  72. u32 timer_id, const char *specval)
  73. {
  74. u32 value;
  75. if (sysfs_streq(buf, specval)) {
  76. value = ACPI_TAD_WAKE_DISABLED;
  77. } else {
  78. int ret = kstrtou32(buf, 0, &value);
  79. if (ret)
  80. return ret;
  81. if (value == ACPI_TAD_WAKE_DISABLED)
  82. return -EINVAL;
  83. }
  84. return acpi_tad_wake_set(dev, method, timer_id, value);
  85. }
  86. static ssize_t acpi_tad_wake_read(struct device *dev, char *buf, char *method,
  87. u32 timer_id, const char *specval)
  88. {
  89. acpi_handle handle = ACPI_HANDLE(dev);
  90. union acpi_object args[] = {
  91. { .type = ACPI_TYPE_INTEGER, },
  92. };
  93. struct acpi_object_list arg_list = {
  94. .pointer = args,
  95. .count = ARRAY_SIZE(args),
  96. };
  97. unsigned long long retval;
  98. acpi_status status;
  99. args[0].integer.value = timer_id;
  100. pm_runtime_get_sync(dev);
  101. status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
  102. pm_runtime_put_sync(dev);
  103. if (ACPI_FAILURE(status))
  104. return -EIO;
  105. if ((u32)retval == ACPI_TAD_WAKE_DISABLED)
  106. return sprintf(buf, "%s\n", specval);
  107. return sprintf(buf, "%u\n", (u32)retval);
  108. }
  109. static const char *alarm_specval = "disabled";
  110. static int acpi_tad_alarm_write(struct device *dev, const char *buf,
  111. u32 timer_id)
  112. {
  113. return acpi_tad_wake_write(dev, buf, "_STV", timer_id, alarm_specval);
  114. }
  115. static ssize_t acpi_tad_alarm_read(struct device *dev, char *buf, u32 timer_id)
  116. {
  117. return acpi_tad_wake_read(dev, buf, "_TIV", timer_id, alarm_specval);
  118. }
  119. static const char *policy_specval = "never";
  120. static int acpi_tad_policy_write(struct device *dev, const char *buf,
  121. u32 timer_id)
  122. {
  123. return acpi_tad_wake_write(dev, buf, "_STP", timer_id, policy_specval);
  124. }
  125. static ssize_t acpi_tad_policy_read(struct device *dev, char *buf, u32 timer_id)
  126. {
  127. return acpi_tad_wake_read(dev, buf, "_TIP", timer_id, policy_specval);
  128. }
  129. static int acpi_tad_clear_status(struct device *dev, u32 timer_id)
  130. {
  131. acpi_handle handle = ACPI_HANDLE(dev);
  132. union acpi_object args[] = {
  133. { .type = ACPI_TYPE_INTEGER, },
  134. };
  135. struct acpi_object_list arg_list = {
  136. .pointer = args,
  137. .count = ARRAY_SIZE(args),
  138. };
  139. unsigned long long retval;
  140. acpi_status status;
  141. args[0].integer.value = timer_id;
  142. pm_runtime_get_sync(dev);
  143. status = acpi_evaluate_integer(handle, "_CWS", &arg_list, &retval);
  144. pm_runtime_put_sync(dev);
  145. if (ACPI_FAILURE(status) || retval)
  146. return -EIO;
  147. return 0;
  148. }
  149. static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
  150. {
  151. int ret, value;
  152. ret = kstrtoint(buf, 0, &value);
  153. if (ret)
  154. return ret;
  155. if (value)
  156. return -EINVAL;
  157. return acpi_tad_clear_status(dev, timer_id);
  158. }
  159. static ssize_t acpi_tad_status_read(struct device *dev, char *buf, u32 timer_id)
  160. {
  161. acpi_handle handle = ACPI_HANDLE(dev);
  162. union acpi_object args[] = {
  163. { .type = ACPI_TYPE_INTEGER, },
  164. };
  165. struct acpi_object_list arg_list = {
  166. .pointer = args,
  167. .count = ARRAY_SIZE(args),
  168. };
  169. unsigned long long retval;
  170. acpi_status status;
  171. args[0].integer.value = timer_id;
  172. pm_runtime_get_sync(dev);
  173. status = acpi_evaluate_integer(handle, "_GWS", &arg_list, &retval);
  174. pm_runtime_put_sync(dev);
  175. if (ACPI_FAILURE(status))
  176. return -EIO;
  177. return sprintf(buf, "0x%02X\n", (u32)retval);
  178. }
  179. static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
  180. char *buf)
  181. {
  182. struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
  183. return sprintf(buf, "0x%02X\n", dd->capabilities);
  184. }
  185. static DEVICE_ATTR_RO(caps);
  186. static ssize_t ac_alarm_store(struct device *dev, struct device_attribute *attr,
  187. const char *buf, size_t count)
  188. {
  189. int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_AC_TIMER);
  190. return ret ? ret : count;
  191. }
  192. static ssize_t ac_alarm_show(struct device *dev, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. return acpi_tad_alarm_read(dev, buf, ACPI_TAD_AC_TIMER);
  196. }
  197. static DEVICE_ATTR(ac_alarm, S_IRUSR | S_IWUSR, ac_alarm_show, ac_alarm_store);
  198. static ssize_t ac_policy_store(struct device *dev, struct device_attribute *attr,
  199. const char *buf, size_t count)
  200. {
  201. int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_AC_TIMER);
  202. return ret ? ret : count;
  203. }
  204. static ssize_t ac_policy_show(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. return acpi_tad_policy_read(dev, buf, ACPI_TAD_AC_TIMER);
  208. }
  209. static DEVICE_ATTR(ac_policy, S_IRUSR | S_IWUSR, ac_policy_show, ac_policy_store);
  210. static ssize_t ac_status_store(struct device *dev, struct device_attribute *attr,
  211. const char *buf, size_t count)
  212. {
  213. int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_AC_TIMER);
  214. return ret ? ret : count;
  215. }
  216. static ssize_t ac_status_show(struct device *dev, struct device_attribute *attr,
  217. char *buf)
  218. {
  219. return acpi_tad_status_read(dev, buf, ACPI_TAD_AC_TIMER);
  220. }
  221. static DEVICE_ATTR(ac_status, S_IRUSR | S_IWUSR, ac_status_show, ac_status_store);
  222. static struct attribute *acpi_tad_attrs[] = {
  223. &dev_attr_caps.attr,
  224. &dev_attr_ac_alarm.attr,
  225. &dev_attr_ac_policy.attr,
  226. &dev_attr_ac_status.attr,
  227. NULL,
  228. };
  229. static const struct attribute_group acpi_tad_attr_group = {
  230. .attrs = acpi_tad_attrs,
  231. };
  232. static ssize_t dc_alarm_store(struct device *dev, struct device_attribute *attr,
  233. const char *buf, size_t count)
  234. {
  235. int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_DC_TIMER);
  236. return ret ? ret : count;
  237. }
  238. static ssize_t dc_alarm_show(struct device *dev, struct device_attribute *attr,
  239. char *buf)
  240. {
  241. return acpi_tad_alarm_read(dev, buf, ACPI_TAD_DC_TIMER);
  242. }
  243. static DEVICE_ATTR(dc_alarm, S_IRUSR | S_IWUSR, dc_alarm_show, dc_alarm_store);
  244. static ssize_t dc_policy_store(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_DC_TIMER);
  248. return ret ? ret : count;
  249. }
  250. static ssize_t dc_policy_show(struct device *dev, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. return acpi_tad_policy_read(dev, buf, ACPI_TAD_DC_TIMER);
  254. }
  255. static DEVICE_ATTR(dc_policy, S_IRUSR | S_IWUSR, dc_policy_show, dc_policy_store);
  256. static ssize_t dc_status_store(struct device *dev, struct device_attribute *attr,
  257. const char *buf, size_t count)
  258. {
  259. int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_DC_TIMER);
  260. return ret ? ret : count;
  261. }
  262. static ssize_t dc_status_show(struct device *dev, struct device_attribute *attr,
  263. char *buf)
  264. {
  265. return acpi_tad_status_read(dev, buf, ACPI_TAD_DC_TIMER);
  266. }
  267. static DEVICE_ATTR(dc_status, S_IRUSR | S_IWUSR, dc_status_show, dc_status_store);
  268. static struct attribute *acpi_tad_dc_attrs[] = {
  269. &dev_attr_dc_alarm.attr,
  270. &dev_attr_dc_policy.attr,
  271. &dev_attr_dc_status.attr,
  272. NULL,
  273. };
  274. static const struct attribute_group acpi_tad_dc_attr_group = {
  275. .attrs = acpi_tad_dc_attrs,
  276. };
  277. static int acpi_tad_disable_timer(struct device *dev, u32 timer_id)
  278. {
  279. return acpi_tad_wake_set(dev, "_STV", timer_id, ACPI_TAD_WAKE_DISABLED);
  280. }
  281. static int acpi_tad_remove(struct platform_device *pdev)
  282. {
  283. struct device *dev = &pdev->dev;
  284. struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
  285. device_init_wakeup(dev, false);
  286. pm_runtime_get_sync(dev);
  287. if (dd->capabilities & ACPI_TAD_DC_WAKE)
  288. sysfs_remove_group(&dev->kobj, &acpi_tad_dc_attr_group);
  289. sysfs_remove_group(&dev->kobj, &acpi_tad_attr_group);
  290. acpi_tad_disable_timer(dev, ACPI_TAD_AC_TIMER);
  291. acpi_tad_clear_status(dev, ACPI_TAD_AC_TIMER);
  292. if (dd->capabilities & ACPI_TAD_DC_WAKE) {
  293. acpi_tad_disable_timer(dev, ACPI_TAD_DC_TIMER);
  294. acpi_tad_clear_status(dev, ACPI_TAD_DC_TIMER);
  295. }
  296. pm_runtime_put_sync(dev);
  297. pm_runtime_disable(dev);
  298. return 0;
  299. }
  300. static int acpi_tad_probe(struct platform_device *pdev)
  301. {
  302. struct device *dev = &pdev->dev;
  303. acpi_handle handle = ACPI_HANDLE(dev);
  304. struct acpi_tad_driver_data *dd;
  305. acpi_status status;
  306. unsigned long long caps;
  307. int ret;
  308. /*
  309. * Initialization failure messages are mostly about firmware issues, so
  310. * print them at the "info" level.
  311. */
  312. status = acpi_evaluate_integer(handle, "_GCP", NULL, &caps);
  313. if (ACPI_FAILURE(status)) {
  314. dev_info(dev, "Unable to get capabilities\n");
  315. return -ENODEV;
  316. }
  317. if (!(caps & ACPI_TAD_AC_WAKE)) {
  318. dev_info(dev, "Unsupported capabilities\n");
  319. return -ENODEV;
  320. }
  321. if (!acpi_has_method(handle, "_PRW")) {
  322. dev_info(dev, "Missing _PRW\n");
  323. return -ENODEV;
  324. }
  325. dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
  326. if (!dd)
  327. return -ENOMEM;
  328. dd->capabilities = caps;
  329. dev_set_drvdata(dev, dd);
  330. /*
  331. * Assume that the ACPI PM domain has been attached to the device and
  332. * simply enable system wakeup and runtime PM and put the device into
  333. * runtime suspend. Everything else should be taken care of by the ACPI
  334. * PM domain callbacks.
  335. */
  336. device_init_wakeup(dev, true);
  337. dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
  338. DPM_FLAG_LEAVE_SUSPENDED);
  339. /*
  340. * The platform bus type layer tells the ACPI PM domain powers up the
  341. * device, so set the runtime PM status of it to "active".
  342. */
  343. pm_runtime_set_active(dev);
  344. pm_runtime_enable(dev);
  345. pm_runtime_suspend(dev);
  346. ret = sysfs_create_group(&dev->kobj, &acpi_tad_attr_group);
  347. if (ret)
  348. goto fail;
  349. if (caps & ACPI_TAD_DC_WAKE) {
  350. ret = sysfs_create_group(&dev->kobj, &acpi_tad_dc_attr_group);
  351. if (ret)
  352. goto fail;
  353. }
  354. return 0;
  355. fail:
  356. acpi_tad_remove(pdev);
  357. return ret;
  358. }
  359. static const struct acpi_device_id acpi_tad_ids[] = {
  360. {"ACPI000E", 0},
  361. {}
  362. };
  363. static struct platform_driver acpi_tad_driver = {
  364. .driver = {
  365. .name = "acpi-tad",
  366. .acpi_match_table = acpi_tad_ids,
  367. },
  368. .probe = acpi_tad_probe,
  369. .remove = acpi_tad_remove,
  370. };
  371. MODULE_DEVICE_TABLE(acpi, acpi_tad_ids);
  372. module_platform_driver(acpi_tad_driver);