2 * RTC driver for Chrome OS Embedded Controller
4 * Copyright (c) 2017, Google, Inc
6 * Author: Stephen Barber <smbarber@chromium.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 #include <linux/kernel.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
24 #include <linux/slab.h>
26 #define DRV_NAME "cros-ec-rtc"
29 * struct cros_ec_rtc - Driver data for EC RTC
31 * @cros_ec: Pointer to EC device
32 * @rtc: Pointer to RTC device
33 * @notifier: Notifier info for responding to EC events
34 * @saved_alarm: Alarm to restore when interrupts are reenabled
37 struct cros_ec_device
*cros_ec
;
38 struct rtc_device
*rtc
;
39 struct notifier_block notifier
;
43 static int cros_ec_rtc_get(struct cros_ec_device
*cros_ec
, u32 command
,
48 struct cros_ec_command msg
;
49 struct ec_response_rtc data
;
52 memset(&msg
, 0, sizeof(msg
));
53 msg
.msg
.command
= command
;
54 msg
.msg
.insize
= sizeof(msg
.data
);
56 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
59 "error getting %s from EC: %d\n",
60 command
== EC_CMD_RTC_GET_VALUE
? "time" : "alarm",
65 *response
= msg
.data
.time
;
70 static int cros_ec_rtc_set(struct cros_ec_device
*cros_ec
, u32 command
,
75 struct cros_ec_command msg
;
76 struct ec_response_rtc data
;
79 memset(&msg
, 0, sizeof(msg
));
80 msg
.msg
.command
= command
;
81 msg
.msg
.outsize
= sizeof(msg
.data
);
82 msg
.data
.time
= param
;
84 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
86 dev_err(cros_ec
->dev
, "error setting %s on EC: %d\n",
87 command
== EC_CMD_RTC_SET_VALUE
? "time" : "alarm",
95 /* Read the current time from the EC. */
96 static int cros_ec_rtc_read_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
;
103 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, &time
);
105 dev_err(dev
, "error getting time: %d\n", ret
);
109 rtc_time64_to_tm(time
, tm
);
114 /* Set the current EC time. */
115 static int cros_ec_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
117 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
118 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
122 time
= rtc_tm_to_time64(tm
);
123 if (time
< 0 || time
> U32_MAX
)
126 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_VALUE
, (u32
)time
);
128 dev_err(dev
, "error setting time: %d\n", ret
);
135 /* Read alarm time from RTC. */
136 static int cros_ec_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
138 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
139 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
141 u32 current_time
, alarm_offset
;
144 * The EC host command for getting the alarm is relative (i.e. 5
145 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
146 * RTC time first so we can calculate the relative time.
148 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
150 dev_err(dev
, "error getting time: %d\n", ret
);
154 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
, &alarm_offset
);
156 dev_err(dev
, "error getting alarm: %d\n", ret
);
160 rtc_time64_to_tm(current_time
+ alarm_offset
, &alrm
->time
);
165 /* Set the EC's RTC alarm. */
166 static int cros_ec_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
168 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
169 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
172 u32 current_time
, alarm_offset
;
175 * The EC host command for setting the alarm is relative
176 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
177 * Get the current RTC time first so we can calculate the
180 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
182 dev_err(dev
, "error getting time: %d\n", ret
);
186 alarm_time
= rtc_tm_to_time64(&alrm
->time
);
188 if (alarm_time
< 0 || alarm_time
> U32_MAX
)
191 if (!alrm
->enabled
) {
193 * If the alarm is being disabled, send an alarm
196 alarm_offset
= EC_RTC_ALARM_CLEAR
;
197 cros_ec_rtc
->saved_alarm
= (u32
)alarm_time
;
199 /* Don't set an alarm in the past. */
200 if ((u32
)alarm_time
< current_time
)
201 alarm_offset
= EC_RTC_ALARM_CLEAR
;
203 alarm_offset
= (u32
)alarm_time
- current_time
;
206 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
, alarm_offset
);
208 dev_err(dev
, "error setting alarm: %d\n", ret
);
215 static int cros_ec_rtc_alarm_irq_enable(struct device
*dev
,
216 unsigned int enabled
)
218 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
219 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
221 u32 current_time
, alarm_offset
, alarm_value
;
223 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
225 dev_err(dev
, "error getting time: %d\n", ret
);
230 /* Restore saved alarm if it's still in the future. */
231 if (cros_ec_rtc
->saved_alarm
< current_time
)
232 alarm_offset
= EC_RTC_ALARM_CLEAR
;
234 alarm_offset
= cros_ec_rtc
->saved_alarm
- current_time
;
236 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
239 dev_err(dev
, "error restoring alarm: %d\n", ret
);
243 /* Disable alarm, saving the old alarm value. */
244 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
,
247 dev_err(dev
, "error saving alarm: %d\n", ret
);
251 alarm_value
= current_time
+ alarm_offset
;
254 * If the current EC alarm is already past, we don't want
255 * to set an alarm when we go through the alarm irq enable
258 if (alarm_value
< current_time
)
259 cros_ec_rtc
->saved_alarm
= EC_RTC_ALARM_CLEAR
;
261 cros_ec_rtc
->saved_alarm
= alarm_value
;
263 alarm_offset
= EC_RTC_ALARM_CLEAR
;
264 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
267 dev_err(dev
, "error disabling alarm: %d\n", ret
);
275 static int cros_ec_rtc_event(struct notifier_block
*nb
,
276 unsigned long queued_during_suspend
,
279 struct cros_ec_rtc
*cros_ec_rtc
;
280 struct rtc_device
*rtc
;
281 struct cros_ec_device
*cros_ec
;
284 cros_ec_rtc
= container_of(nb
, struct cros_ec_rtc
, notifier
);
285 rtc
= cros_ec_rtc
->rtc
;
286 cros_ec
= cros_ec_rtc
->cros_ec
;
288 host_event
= cros_ec_get_host_event(cros_ec
);
289 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC
)) {
290 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
297 static const struct rtc_class_ops cros_ec_rtc_ops
= {
298 .read_time
= cros_ec_rtc_read_time
,
299 .set_time
= cros_ec_rtc_set_time
,
300 .read_alarm
= cros_ec_rtc_read_alarm
,
301 .set_alarm
= cros_ec_rtc_set_alarm
,
302 .alarm_irq_enable
= cros_ec_rtc_alarm_irq_enable
,
305 #ifdef CONFIG_PM_SLEEP
306 static int cros_ec_rtc_suspend(struct device
*dev
)
308 struct platform_device
*pdev
= to_platform_device(dev
);
309 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
311 if (device_may_wakeup(dev
))
312 enable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
317 static int cros_ec_rtc_resume(struct device
*dev
)
319 struct platform_device
*pdev
= to_platform_device(dev
);
320 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
322 if (device_may_wakeup(dev
))
323 disable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
329 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops
, cros_ec_rtc_suspend
,
332 static int cros_ec_rtc_probe(struct platform_device
*pdev
)
334 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pdev
->dev
.parent
);
335 struct cros_ec_device
*cros_ec
= ec_dev
->ec_dev
;
336 struct cros_ec_rtc
*cros_ec_rtc
;
340 cros_ec_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*cros_ec_rtc
),
345 platform_set_drvdata(pdev
, cros_ec_rtc
);
346 cros_ec_rtc
->cros_ec
= cros_ec
;
348 /* Get initial time */
349 ret
= cros_ec_rtc_read_time(&pdev
->dev
, &tm
);
351 dev_err(&pdev
->dev
, "failed to read RTC time\n");
355 ret
= device_init_wakeup(&pdev
->dev
, 1);
357 dev_err(&pdev
->dev
, "failed to initialize wakeup\n");
361 cros_ec_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, DRV_NAME
,
364 if (IS_ERR(cros_ec_rtc
->rtc
)) {
365 ret
= PTR_ERR(cros_ec_rtc
->rtc
);
366 dev_err(&pdev
->dev
, "failed to register rtc device\n");
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 int 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");
399 static struct platform_driver cros_ec_rtc_driver
= {
400 .probe
= cros_ec_rtc_probe
,
401 .remove
= cros_ec_rtc_remove
,
404 .pm
= &cros_ec_rtc_pm_ops
,
408 module_platform_driver(cros_ec_rtc_driver
);
410 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
411 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
412 MODULE_LICENSE("GPL");
413 MODULE_ALIAS("platform:" DRV_NAME
);