rtc-cros-ec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/module.h>
  8. #include <linux/platform_data/cros_ec_commands.h>
  9. #include <linux/platform_data/cros_ec_proto.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 = rtc_tm_to_time64(tm);
  93. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
  94. if (ret < 0) {
  95. dev_err(dev, "error setting time: %d\n", ret);
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. /* Read alarm time from RTC. */
  101. static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  102. {
  103. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  104. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  105. int ret;
  106. u32 current_time, alarm_offset;
  107. /*
  108. * The EC host command for getting the alarm is relative (i.e. 5
  109. * seconds from now) whereas rtc_wkalrm is absolute. Get the current
  110. * RTC time first so we can calculate the relative time.
  111. */
  112. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  113. if (ret < 0) {
  114. dev_err(dev, "error getting time: %d\n", ret);
  115. return ret;
  116. }
  117. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
  118. if (ret < 0) {
  119. dev_err(dev, "error getting alarm: %d\n", ret);
  120. return ret;
  121. }
  122. rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
  123. return 0;
  124. }
  125. /* Set the EC's RTC alarm. */
  126. static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  127. {
  128. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  129. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  130. int ret;
  131. time64_t alarm_time;
  132. u32 current_time, alarm_offset;
  133. /*
  134. * The EC host command for setting the alarm is relative
  135. * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
  136. * Get the current RTC time first so we can calculate the
  137. * relative time.
  138. */
  139. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  140. if (ret < 0) {
  141. dev_err(dev, "error getting time: %d\n", ret);
  142. return ret;
  143. }
  144. alarm_time = rtc_tm_to_time64(&alrm->time);
  145. if (alarm_time < 0 || alarm_time > U32_MAX)
  146. return -EINVAL;
  147. if (!alrm->enabled) {
  148. /*
  149. * If the alarm is being disabled, send an alarm
  150. * clear command.
  151. */
  152. alarm_offset = EC_RTC_ALARM_CLEAR;
  153. cros_ec_rtc->saved_alarm = (u32)alarm_time;
  154. } else {
  155. /* Don't set an alarm in the past. */
  156. if ((u32)alarm_time <= current_time)
  157. return -ETIME;
  158. alarm_offset = (u32)alarm_time - current_time;
  159. }
  160. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
  161. if (ret < 0) {
  162. dev_err(dev, "error setting alarm: %d\n", ret);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
  168. unsigned int enabled)
  169. {
  170. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  171. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  172. int ret;
  173. u32 current_time, alarm_offset, alarm_value;
  174. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  175. if (ret < 0) {
  176. dev_err(dev, "error getting time: %d\n", ret);
  177. return ret;
  178. }
  179. if (enabled) {
  180. /* Restore saved alarm if it's still in the future. */
  181. if (cros_ec_rtc->saved_alarm < current_time)
  182. alarm_offset = EC_RTC_ALARM_CLEAR;
  183. else
  184. alarm_offset = cros_ec_rtc->saved_alarm - current_time;
  185. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
  186. alarm_offset);
  187. if (ret < 0) {
  188. dev_err(dev, "error restoring alarm: %d\n", ret);
  189. return ret;
  190. }
  191. } else {
  192. /* Disable alarm, saving the old alarm value. */
  193. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
  194. &alarm_offset);
  195. if (ret < 0) {
  196. dev_err(dev, "error saving alarm: %d\n", ret);
  197. return ret;
  198. }
  199. alarm_value = current_time + alarm_offset;
  200. /*
  201. * If the current EC alarm is already past, we don't want
  202. * to set an alarm when we go through the alarm irq enable
  203. * path.
  204. */
  205. if (alarm_value < current_time)
  206. cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
  207. else
  208. cros_ec_rtc->saved_alarm = alarm_value;
  209. alarm_offset = EC_RTC_ALARM_CLEAR;
  210. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
  211. alarm_offset);
  212. if (ret < 0) {
  213. dev_err(dev, "error disabling alarm: %d\n", ret);
  214. return ret;
  215. }
  216. }
  217. return 0;
  218. }
  219. static int cros_ec_rtc_event(struct notifier_block *nb,
  220. unsigned long queued_during_suspend,
  221. void *_notify)
  222. {
  223. struct cros_ec_rtc *cros_ec_rtc;
  224. struct rtc_device *rtc;
  225. struct cros_ec_device *cros_ec;
  226. u32 host_event;
  227. cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
  228. rtc = cros_ec_rtc->rtc;
  229. cros_ec = cros_ec_rtc->cros_ec;
  230. host_event = cros_ec_get_host_event(cros_ec);
  231. if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
  232. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  233. return NOTIFY_OK;
  234. } else {
  235. return NOTIFY_DONE;
  236. }
  237. }
  238. static const struct rtc_class_ops cros_ec_rtc_ops = {
  239. .read_time = cros_ec_rtc_read_time,
  240. .set_time = cros_ec_rtc_set_time,
  241. .read_alarm = cros_ec_rtc_read_alarm,
  242. .set_alarm = cros_ec_rtc_set_alarm,
  243. .alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
  244. };
  245. #ifdef CONFIG_PM_SLEEP
  246. static int cros_ec_rtc_suspend(struct device *dev)
  247. {
  248. struct platform_device *pdev = to_platform_device(dev);
  249. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
  250. if (device_may_wakeup(dev))
  251. return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
  252. return 0;
  253. }
  254. static int cros_ec_rtc_resume(struct device *dev)
  255. {
  256. struct platform_device *pdev = to_platform_device(dev);
  257. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
  258. if (device_may_wakeup(dev))
  259. return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
  260. return 0;
  261. }
  262. #endif
  263. static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
  264. cros_ec_rtc_resume);
  265. static int cros_ec_rtc_probe(struct platform_device *pdev)
  266. {
  267. struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
  268. struct cros_ec_device *cros_ec = ec_dev->ec_dev;
  269. struct cros_ec_rtc *cros_ec_rtc;
  270. struct rtc_time tm;
  271. int ret;
  272. cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
  273. GFP_KERNEL);
  274. if (!cros_ec_rtc)
  275. return -ENOMEM;
  276. platform_set_drvdata(pdev, cros_ec_rtc);
  277. cros_ec_rtc->cros_ec = cros_ec;
  278. /* Get initial time */
  279. ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
  280. if (ret) {
  281. dev_err(&pdev->dev, "failed to read RTC time\n");
  282. return ret;
  283. }
  284. ret = device_init_wakeup(&pdev->dev, 1);
  285. if (ret) {
  286. dev_err(&pdev->dev, "failed to initialize wakeup\n");
  287. return ret;
  288. }
  289. cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  290. if (IS_ERR(cros_ec_rtc->rtc))
  291. return PTR_ERR(cros_ec_rtc->rtc);
  292. cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
  293. cros_ec_rtc->rtc->range_max = U32_MAX;
  294. ret = rtc_register_device(cros_ec_rtc->rtc);
  295. if (ret)
  296. return ret;
  297. /* Get RTC events from the EC. */
  298. cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
  299. ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
  300. &cros_ec_rtc->notifier);
  301. if (ret) {
  302. dev_err(&pdev->dev, "failed to register notifier\n");
  303. return ret;
  304. }
  305. return 0;
  306. }
  307. static int cros_ec_rtc_remove(struct platform_device *pdev)
  308. {
  309. struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
  310. struct device *dev = &pdev->dev;
  311. int ret;
  312. ret = blocking_notifier_chain_unregister(
  313. &cros_ec_rtc->cros_ec->event_notifier,
  314. &cros_ec_rtc->notifier);
  315. if (ret) {
  316. dev_err(dev, "failed to unregister notifier\n");
  317. return ret;
  318. }
  319. return 0;
  320. }
  321. static struct platform_driver cros_ec_rtc_driver = {
  322. .probe = cros_ec_rtc_probe,
  323. .remove = cros_ec_rtc_remove,
  324. .driver = {
  325. .name = DRV_NAME,
  326. .pm = &cros_ec_rtc_pm_ops,
  327. },
  328. };
  329. module_platform_driver(cros_ec_rtc_driver);
  330. MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
  331. MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
  332. MODULE_LICENSE("GPL v2");
  333. MODULE_ALIAS("platform:" DRV_NAME);