rtc-cros-ec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0
  2. // RTC driver for ChromeOS Embedded Controller.
  3. //
  4. // Copyright (C) 2017 Google, Inc.
  5. // Author: Stephen Barber <smbarber@chromium.org>
  6. #include <linux/kernel.h>
  7. #include <linux/mfd/cros_ec.h>
  8. #include <linux/mfd/cros_ec_commands.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/rtc.h>
  12. #include <linux/slab.h>
  13. #define DRV_NAME "cros-ec-rtc"
  14. /**
  15. * struct cros_ec_rtc - Driver data for EC RTC
  16. *
  17. * @cros_ec: Pointer to EC device
  18. * @rtc: Pointer to RTC device
  19. * @notifier: Notifier info for responding to EC events
  20. * @saved_alarm: Alarm to restore when interrupts are reenabled
  21. */
  22. struct cros_ec_rtc {
  23. struct cros_ec_device *cros_ec;
  24. struct rtc_device *rtc;
  25. struct notifier_block notifier;
  26. u32 saved_alarm;
  27. };
  28. static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
  29. u32 *response)
  30. {
  31. int ret;
  32. struct {
  33. struct cros_ec_command msg;
  34. struct ec_response_rtc data;
  35. } __packed msg;
  36. memset(&msg, 0, sizeof(msg));
  37. msg.msg.command = command;
  38. msg.msg.insize = sizeof(msg.data);
  39. ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
  40. if (ret < 0) {
  41. dev_err(cros_ec->dev,
  42. "error getting %s from EC: %d\n",
  43. command == EC_CMD_RTC_GET_VALUE ? "time" : "alarm",
  44. ret);
  45. return ret;
  46. }
  47. *response = msg.data.time;
  48. return 0;
  49. }
  50. static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
  51. u32 param)
  52. {
  53. int ret = 0;
  54. struct {
  55. struct cros_ec_command msg;
  56. struct ec_response_rtc data;
  57. } __packed msg;
  58. memset(&msg, 0, sizeof(msg));
  59. msg.msg.command = command;
  60. msg.msg.outsize = sizeof(msg.data);
  61. msg.data.time = param;
  62. ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
  63. if (ret < 0) {
  64. dev_err(cros_ec->dev, "error setting %s on EC: %d\n",
  65. command == EC_CMD_RTC_SET_VALUE ? "time" : "alarm",
  66. ret);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. /* Read the current time from the EC. */
  72. static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  75. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  76. int ret;
  77. u32 time;
  78. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
  79. if (ret) {
  80. dev_err(dev, "error getting time: %d\n", ret);
  81. return ret;
  82. }
  83. rtc_time64_to_tm(time, tm);
  84. return 0;
  85. }
  86. /* Set the current EC time. */
  87. static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
  88. {
  89. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  90. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  91. int ret;
  92. time64_t time;
  93. time = rtc_tm_to_time64(tm);
  94. if (time < 0 || time > U32_MAX)
  95. return -EINVAL;
  96. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
  97. if (ret < 0) {
  98. dev_err(dev, "error setting time: %d\n", ret);
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. /* Read alarm time from RTC. */
  104. static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  105. {
  106. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  107. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  108. int ret;
  109. u32 current_time, alarm_offset;
  110. /*
  111. * The EC host command for getting the alarm is relative (i.e. 5
  112. * seconds from now) whereas rtc_wkalrm is absolute. Get the current
  113. * RTC time first so we can calculate the relative time.
  114. */
  115. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  116. if (ret < 0) {
  117. dev_err(dev, "error getting time: %d\n", ret);
  118. return ret;
  119. }
  120. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
  121. if (ret < 0) {
  122. dev_err(dev, "error getting alarm: %d\n", ret);
  123. return ret;
  124. }
  125. rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
  126. return 0;
  127. }
  128. /* Set the EC's RTC alarm. */
  129. static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  130. {
  131. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  132. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  133. int ret;
  134. time64_t alarm_time;
  135. u32 current_time, alarm_offset;
  136. /*
  137. * The EC host command for setting the alarm is relative
  138. * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
  139. * Get the current RTC time first so we can calculate the
  140. * relative time.
  141. */
  142. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  143. if (ret < 0) {
  144. dev_err(dev, "error getting time: %d\n", ret);
  145. return ret;
  146. }
  147. alarm_time = rtc_tm_to_time64(&alrm->time);
  148. if (alarm_time < 0 || alarm_time > U32_MAX)
  149. return -EINVAL;
  150. if (!alrm->enabled) {
  151. /*
  152. * If the alarm is being disabled, send an alarm
  153. * clear command.
  154. */
  155. alarm_offset = EC_RTC_ALARM_CLEAR;
  156. cros_ec_rtc->saved_alarm = (u32)alarm_time;
  157. } else {
  158. /* Don't set an alarm in the past. */
  159. if ((u32)alarm_time <= current_time)
  160. return -ETIME;
  161. alarm_offset = (u32)alarm_time - current_time;
  162. }
  163. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
  164. if (ret < 0) {
  165. dev_err(dev, "error setting alarm: %d\n", ret);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
  171. unsigned int enabled)
  172. {
  173. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  174. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  175. int ret;
  176. u32 current_time, alarm_offset, alarm_value;
  177. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  178. if (ret < 0) {
  179. dev_err(dev, "error getting time: %d\n", ret);
  180. return ret;
  181. }
  182. if (enabled) {
  183. /* Restore saved alarm if it's still in the future. */
  184. if (cros_ec_rtc->saved_alarm < current_time)
  185. alarm_offset = EC_RTC_ALARM_CLEAR;
  186. else
  187. alarm_offset = cros_ec_rtc->saved_alarm - current_time;
  188. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
  189. alarm_offset);
  190. if (ret < 0) {
  191. dev_err(dev, "error restoring alarm: %d\n", ret);
  192. return ret;
  193. }
  194. } else {
  195. /* Disable alarm, saving the old alarm value. */
  196. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
  197. &alarm_offset);
  198. if (ret < 0) {
  199. dev_err(dev, "error saving alarm: %d\n", ret);
  200. return ret;
  201. }
  202. alarm_value = current_time + alarm_offset;
  203. /*
  204. * If the current EC alarm is already past, we don't want
  205. * to set an alarm when we go through the alarm irq enable
  206. * path.
  207. */
  208. if (alarm_value < current_time)
  209. cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
  210. else
  211. cros_ec_rtc->saved_alarm = alarm_value;
  212. alarm_offset = EC_RTC_ALARM_CLEAR;
  213. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
  214. alarm_offset);
  215. if (ret < 0) {
  216. dev_err(dev, "error disabling alarm: %d\n", ret);
  217. return ret;
  218. }
  219. }
  220. return 0;
  221. }
  222. static int cros_ec_rtc_event(struct notifier_block *nb,
  223. unsigned long queued_during_suspend,
  224. void *_notify)
  225. {
  226. struct cros_ec_rtc *cros_ec_rtc;
  227. struct rtc_device *rtc;
  228. struct cros_ec_device *cros_ec;
  229. u32 host_event;
  230. cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
  231. rtc = cros_ec_rtc->rtc;
  232. cros_ec = cros_ec_rtc->cros_ec;
  233. host_event = cros_ec_get_host_event(cros_ec);
  234. if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
  235. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  236. return NOTIFY_OK;
  237. } else {
  238. return NOTIFY_DONE;
  239. }
  240. }
  241. static const struct rtc_class_ops cros_ec_rtc_ops = {
  242. .read_time = cros_ec_rtc_read_time,
  243. .set_time = cros_ec_rtc_set_time,
  244. .read_alarm = cros_ec_rtc_read_alarm,
  245. .set_alarm = cros_ec_rtc_set_alarm,
  246. .alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
  247. };
  248. #ifdef CONFIG_PM_SLEEP
  249. static int cros_ec_rtc_suspend(struct device *dev)
  250. {
  251. struct platform_device *pdev = to_platform_device(dev);
  252. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
  253. if (device_may_wakeup(dev))
  254. return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
  255. return 0;
  256. }
  257. static int cros_ec_rtc_resume(struct device *dev)
  258. {
  259. struct platform_device *pdev = to_platform_device(dev);
  260. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
  261. if (device_may_wakeup(dev))
  262. return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
  263. return 0;
  264. }
  265. #endif
  266. static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
  267. cros_ec_rtc_resume);
  268. static int cros_ec_rtc_probe(struct platform_device *pdev)
  269. {
  270. struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
  271. struct cros_ec_device *cros_ec = ec_dev->ec_dev;
  272. struct cros_ec_rtc *cros_ec_rtc;
  273. struct rtc_time tm;
  274. int ret;
  275. cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
  276. GFP_KERNEL);
  277. if (!cros_ec_rtc)
  278. return -ENOMEM;
  279. platform_set_drvdata(pdev, cros_ec_rtc);
  280. cros_ec_rtc->cros_ec = cros_ec;
  281. /* Get initial time */
  282. ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
  283. if (ret) {
  284. dev_err(&pdev->dev, "failed to read RTC time\n");
  285. return ret;
  286. }
  287. ret = device_init_wakeup(&pdev->dev, 1);
  288. if (ret) {
  289. dev_err(&pdev->dev, "failed to initialize wakeup\n");
  290. return ret;
  291. }
  292. cros_ec_rtc->rtc = devm_rtc_device_register(&pdev->dev, DRV_NAME,
  293. &cros_ec_rtc_ops,
  294. THIS_MODULE);
  295. if (IS_ERR(cros_ec_rtc->rtc)) {
  296. ret = PTR_ERR(cros_ec_rtc->rtc);
  297. dev_err(&pdev->dev, "failed to register rtc device\n");
  298. return ret;
  299. }
  300. /* Get RTC events from the EC. */
  301. cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
  302. ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
  303. &cros_ec_rtc->notifier);
  304. if (ret) {
  305. dev_err(&pdev->dev, "failed to register notifier\n");
  306. return ret;
  307. }
  308. return 0;
  309. }
  310. static int cros_ec_rtc_remove(struct platform_device *pdev)
  311. {
  312. struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
  313. struct device *dev = &pdev->dev;
  314. int ret;
  315. ret = blocking_notifier_chain_unregister(
  316. &cros_ec_rtc->cros_ec->event_notifier,
  317. &cros_ec_rtc->notifier);
  318. if (ret) {
  319. dev_err(dev, "failed to unregister notifier\n");
  320. return ret;
  321. }
  322. return 0;
  323. }
  324. static struct platform_driver cros_ec_rtc_driver = {
  325. .probe = cros_ec_rtc_probe,
  326. .remove = cros_ec_rtc_remove,
  327. .driver = {
  328. .name = DRV_NAME,
  329. .pm = &cros_ec_rtc_pm_ops,
  330. },
  331. };
  332. module_platform_driver(cros_ec_rtc_driver);
  333. MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
  334. MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
  335. MODULE_LICENSE("GPL v2");
  336. MODULE_ALIAS("platform:" DRV_NAME);