rtc-cros-ec.c 10 KB

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