rtc-cros-ec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_data/cros_ec_commands.h>
  10. #include <linux/platform_data/cros_ec_proto.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/rtc.h>
  13. #include <linux/slab.h>
  14. #define DRV_NAME "cros-ec-rtc"
  15. #define SECS_PER_DAY (24 * 60 * 60)
  16. /**
  17. * struct cros_ec_rtc - Driver data for EC RTC
  18. *
  19. * @cros_ec: Pointer to EC device
  20. * @rtc: Pointer to RTC device
  21. * @notifier: Notifier info for responding to EC events
  22. * @saved_alarm: Alarm to restore when interrupts are reenabled
  23. */
  24. struct cros_ec_rtc {
  25. struct cros_ec_device *cros_ec;
  26. struct rtc_device *rtc;
  27. struct notifier_block notifier;
  28. u32 saved_alarm;
  29. };
  30. static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
  31. u32 *response)
  32. {
  33. int ret;
  34. struct {
  35. struct cros_ec_command msg;
  36. struct ec_response_rtc data;
  37. } __packed msg;
  38. memset(&msg, 0, sizeof(msg));
  39. msg.msg.command = command;
  40. msg.msg.insize = sizeof(msg.data);
  41. ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
  42. if (ret < 0)
  43. return ret;
  44. *response = msg.data.time;
  45. return 0;
  46. }
  47. static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
  48. u32 param)
  49. {
  50. int ret;
  51. struct {
  52. struct cros_ec_command msg;
  53. struct ec_response_rtc data;
  54. } __packed msg;
  55. memset(&msg, 0, sizeof(msg));
  56. msg.msg.command = command;
  57. msg.msg.outsize = sizeof(msg.data);
  58. msg.data.time = param;
  59. ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
  60. if (ret < 0)
  61. return ret;
  62. return 0;
  63. }
  64. /* Read the current time from the EC. */
  65. static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  68. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  69. int ret;
  70. u32 time;
  71. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
  72. if (ret) {
  73. dev_err(dev, "error getting time: %d\n", ret);
  74. return ret;
  75. }
  76. rtc_time64_to_tm(time, tm);
  77. return 0;
  78. }
  79. /* Set the current EC time. */
  80. static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
  81. {
  82. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  83. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  84. int ret;
  85. time64_t time = rtc_tm_to_time64(tm);
  86. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
  87. if (ret < 0) {
  88. dev_err(dev, "error setting time: %d\n", ret);
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. /* Read alarm time from RTC. */
  94. static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  95. {
  96. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  97. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  98. int ret;
  99. u32 current_time, alarm_offset;
  100. /*
  101. * The EC host command for getting the alarm is relative (i.e. 5
  102. * seconds from now) whereas rtc_wkalrm is absolute. Get the current
  103. * RTC time first so we can calculate the relative time.
  104. */
  105. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  106. if (ret < 0) {
  107. dev_err(dev, "error getting time: %d\n", ret);
  108. return ret;
  109. }
  110. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
  111. if (ret < 0) {
  112. dev_err(dev, "error getting alarm: %d\n", ret);
  113. return ret;
  114. }
  115. rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
  116. return 0;
  117. }
  118. /* Set the EC's RTC alarm. */
  119. static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  120. {
  121. struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
  122. struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
  123. int ret;
  124. time64_t alarm_time;
  125. u32 current_time, alarm_offset;
  126. /*
  127. * The EC host command for setting the alarm is relative
  128. * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
  129. * Get the current RTC time first so we can calculate the
  130. * relative time.
  131. */
  132. ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
  133. if (ret < 0) {
  134. dev_err(dev, "error getting time: %d\n", ret);
  135. return ret;
  136. }
  137. alarm_time = rtc_tm_to_time64(&alrm->time);
  138. if (alarm_time < 0 || alarm_time > U32_MAX)
  139. return -EINVAL;
  140. if (!alrm->enabled) {
  141. /*
  142. * If the alarm is being disabled, send an alarm
  143. * clear command.
  144. */
  145. alarm_offset = EC_RTC_ALARM_CLEAR;
  146. cros_ec_rtc->saved_alarm = (u32)alarm_time;
  147. } else {
  148. /* Don't set an alarm in the past. */
  149. if ((u32)alarm_time <= current_time)
  150. return -ETIME;
  151. alarm_offset = (u32)alarm_time - current_time;
  152. }
  153. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
  154. if (ret < 0) {
  155. dev_err(dev, "error setting alarm in %u seconds: %d\n",
  156. alarm_offset, ret);
  157. /*
  158. * The EC code returns -EINVAL if the alarm time is too
  159. * far in the future. Convert it to the expected error code.
  160. */
  161. if (ret == -EINVAL)
  162. ret = -ERANGE;
  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. /*
  295. * The RTC on some older Chromebooks can only handle alarms less than
  296. * 24 hours in the future. The only way to find out is to try to set an
  297. * alarm further in the future. If that fails, assume that the RTC
  298. * connected to the EC can only handle less than 24 hours of alarm
  299. * window.
  300. */
  301. ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, SECS_PER_DAY * 2);
  302. if (ret == -EINVAL)
  303. cros_ec_rtc->rtc->alarm_offset_max = SECS_PER_DAY - 1;
  304. (void)cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
  305. EC_RTC_ALARM_CLEAR);
  306. ret = devm_rtc_register_device(cros_ec_rtc->rtc);
  307. if (ret)
  308. return ret;
  309. /* Get RTC events from the EC. */
  310. cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
  311. ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
  312. &cros_ec_rtc->notifier);
  313. if (ret) {
  314. dev_err(&pdev->dev, "failed to register notifier\n");
  315. return ret;
  316. }
  317. return 0;
  318. }
  319. static void cros_ec_rtc_remove(struct platform_device *pdev)
  320. {
  321. struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
  322. struct device *dev = &pdev->dev;
  323. int ret;
  324. ret = blocking_notifier_chain_unregister(
  325. &cros_ec_rtc->cros_ec->event_notifier,
  326. &cros_ec_rtc->notifier);
  327. if (ret)
  328. dev_err(dev, "failed to unregister notifier\n");
  329. }
  330. static const struct platform_device_id cros_ec_rtc_id[] = {
  331. { DRV_NAME, 0 },
  332. {}
  333. };
  334. MODULE_DEVICE_TABLE(platform, cros_ec_rtc_id);
  335. static struct platform_driver cros_ec_rtc_driver = {
  336. .probe = cros_ec_rtc_probe,
  337. .remove_new = cros_ec_rtc_remove,
  338. .driver = {
  339. .name = DRV_NAME,
  340. .pm = &cros_ec_rtc_pm_ops,
  341. },
  342. .id_table = cros_ec_rtc_id,
  343. };
  344. module_platform_driver(cros_ec_rtc_driver);
  345. MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
  346. MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
  347. MODULE_LICENSE("GPL v2");