1 // SPDX-License-Identifier: GPL-2.0
2 // RTC driver for ChromeOS Embedded Controller.
4 // Copyright (C) 2017 Google, Inc.
5 // Author: Stephen Barber <smbarber@chromium.org>
7 #include <linux/kernel.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>
15 #define DRV_NAME "cros-ec-rtc"
17 #define SECS_PER_DAY (24 * 60 * 60)
20 * struct cros_ec_rtc - Driver data for EC RTC
22 * @cros_ec: Pointer to EC device
23 * @rtc: Pointer to RTC device
24 * @notifier: Notifier info for responding to EC events
25 * @saved_alarm: Alarm to restore when interrupts are reenabled
28 struct cros_ec_device *cros_ec;
29 struct rtc_device *rtc;
30 struct notifier_block notifier;
34 static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
39 struct cros_ec_command msg;
40 struct ec_response_rtc data;
43 memset(&msg, 0, sizeof(msg));
44 msg.msg.command = command;
45 msg.msg.insize = sizeof(msg.data);
47 ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
51 *response = msg.data.time;
56 static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
61 struct cros_ec_command msg;
62 struct ec_response_rtc data;
65 memset(&msg, 0, sizeof(msg));
66 msg.msg.command = command;
67 msg.msg.outsize = sizeof(msg.data);
68 msg.data.time = param;
70 ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
76 /* Read the current time from the EC. */
77 static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
79 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
80 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
84 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
86 dev_err(dev, "error getting time: %d\n", ret);
90 rtc_time64_to_tm(time, tm);
95 /* Set the current EC time. */
96 static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
98 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
99 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
101 time64_t time = rtc_tm_to_time64(tm);
103 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
105 dev_err(dev, "error setting time: %d\n", ret);
112 /* Read alarm time from RTC. */
113 static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
115 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
116 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
118 u32 current_time, alarm_offset;
121 * The EC host command for getting the alarm is relative (i.e. 5
122 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
123 * RTC time first so we can calculate the relative time.
125 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
127 dev_err(dev, "error getting time: %d\n", ret);
131 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
133 dev_err(dev, "error getting alarm: %d\n", ret);
137 rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
142 /* Set the EC's RTC alarm. */
143 static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
145 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
146 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
149 u32 current_time, alarm_offset;
152 * The EC host command for setting the alarm is relative
153 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
154 * Get the current RTC time first so we can calculate the
157 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
159 dev_err(dev, "error getting time: %d\n", ret);
163 alarm_time = rtc_tm_to_time64(&alrm->time);
165 if (alarm_time < 0 || alarm_time > U32_MAX)
168 if (!alrm->enabled) {
170 * If the alarm is being disabled, send an alarm
173 alarm_offset = EC_RTC_ALARM_CLEAR;
174 cros_ec_rtc->saved_alarm = (u32)alarm_time;
176 /* Don't set an alarm in the past. */
177 if ((u32)alarm_time <= current_time)
180 alarm_offset = (u32)alarm_time - current_time;
183 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
185 dev_err(dev, "error setting alarm in %u seconds: %d\n",
188 * The EC code returns -EINVAL if the alarm time is too
189 * far in the future. Convert it to the expected error code.
199 static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
200 unsigned int enabled)
202 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
203 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
205 u32 current_time, alarm_offset, alarm_value;
207 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
209 dev_err(dev, "error getting time: %d\n", ret);
214 /* Restore saved alarm if it's still in the future. */
215 if (cros_ec_rtc->saved_alarm < current_time)
216 alarm_offset = EC_RTC_ALARM_CLEAR;
218 alarm_offset = cros_ec_rtc->saved_alarm - current_time;
220 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
223 dev_err(dev, "error restoring alarm: %d\n", ret);
227 /* Disable alarm, saving the old alarm value. */
228 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
231 dev_err(dev, "error saving alarm: %d\n", ret);
235 alarm_value = current_time + alarm_offset;
238 * If the current EC alarm is already past, we don't want
239 * to set an alarm when we go through the alarm irq enable
242 if (alarm_value < current_time)
243 cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
245 cros_ec_rtc->saved_alarm = alarm_value;
247 alarm_offset = EC_RTC_ALARM_CLEAR;
248 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
251 dev_err(dev, "error disabling alarm: %d\n", ret);
259 static int cros_ec_rtc_event(struct notifier_block *nb,
260 unsigned long queued_during_suspend,
263 struct cros_ec_rtc *cros_ec_rtc;
264 struct rtc_device *rtc;
265 struct cros_ec_device *cros_ec;
268 cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
269 rtc = cros_ec_rtc->rtc;
270 cros_ec = cros_ec_rtc->cros_ec;
272 host_event = cros_ec_get_host_event(cros_ec);
273 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
274 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
281 static const struct rtc_class_ops cros_ec_rtc_ops = {
282 .read_time = cros_ec_rtc_read_time,
283 .set_time = cros_ec_rtc_set_time,
284 .read_alarm = cros_ec_rtc_read_alarm,
285 .set_alarm = cros_ec_rtc_set_alarm,
286 .alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
289 #ifdef CONFIG_PM_SLEEP
290 static int cros_ec_rtc_suspend(struct device *dev)
292 struct platform_device *pdev = to_platform_device(dev);
293 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
295 if (device_may_wakeup(dev))
296 return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
301 static int cros_ec_rtc_resume(struct device *dev)
303 struct platform_device *pdev = to_platform_device(dev);
304 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
306 if (device_may_wakeup(dev))
307 return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
313 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
316 static int cros_ec_rtc_probe(struct platform_device *pdev)
318 struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
319 struct cros_ec_device *cros_ec = ec_dev->ec_dev;
320 struct cros_ec_rtc *cros_ec_rtc;
324 cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
329 platform_set_drvdata(pdev, cros_ec_rtc);
330 cros_ec_rtc->cros_ec = cros_ec;
332 /* Get initial time */
333 ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
335 dev_err(&pdev->dev, "failed to read RTC time\n");
339 ret = device_init_wakeup(&pdev->dev, 1);
341 dev_err(&pdev->dev, "failed to initialize wakeup\n");
345 cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
346 if (IS_ERR(cros_ec_rtc->rtc))
347 return PTR_ERR(cros_ec_rtc->rtc);
349 cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
350 cros_ec_rtc->rtc->range_max = U32_MAX;
353 * The RTC on some older Chromebooks can only handle alarms less than
354 * 24 hours in the future. The only way to find out is to try to set an
355 * alarm further in the future. If that fails, assume that the RTC
356 * connected to the EC can only handle less than 24 hours of alarm
359 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, SECS_PER_DAY * 2);
361 cros_ec_rtc->rtc->alarm_offset_max = SECS_PER_DAY - 1;
363 (void)cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
366 ret = devm_rtc_register_device(cros_ec_rtc->rtc);
370 /* Get RTC events from the EC. */
371 cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
372 ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
373 &cros_ec_rtc->notifier);
375 dev_err(&pdev->dev, "failed to register notifier\n");
382 static void cros_ec_rtc_remove(struct platform_device *pdev)
384 struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
385 struct device *dev = &pdev->dev;
388 ret = blocking_notifier_chain_unregister(
389 &cros_ec_rtc->cros_ec->event_notifier,
390 &cros_ec_rtc->notifier);
392 dev_err(dev, "failed to unregister notifier\n");
395 static struct platform_driver cros_ec_rtc_driver = {
396 .probe = cros_ec_rtc_probe,
397 .remove_new = cros_ec_rtc_remove,
400 .pm = &cros_ec_rtc_pm_ops,
404 module_platform_driver(cros_ec_rtc_driver);
406 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
407 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
408 MODULE_LICENSE("GPL v2");
409 MODULE_ALIAS("platform:" DRV_NAME);