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/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/platform_device.h>
13 #include <linux/rtc.h>
14 #include <linux/slab.h>
16 #define DRV_NAME "cros-ec-rtc"
18 #define SECS_PER_DAY (24 * 60 * 60)
21 * struct cros_ec_rtc - Driver data for EC RTC
23 * @cros_ec: Pointer to EC device
24 * @rtc: Pointer to RTC device
25 * @notifier: Notifier info for responding to EC events
26 * @saved_alarm: Alarm to restore when interrupts are reenabled
29 struct cros_ec_device
*cros_ec
;
30 struct rtc_device
*rtc
;
31 struct notifier_block notifier
;
35 static int cros_ec_rtc_get(struct cros_ec_device
*cros_ec
, u32 command
,
40 struct cros_ec_command msg
;
41 struct ec_response_rtc data
;
44 memset(&msg
, 0, sizeof(msg
));
45 msg
.msg
.command
= command
;
46 msg
.msg
.insize
= sizeof(msg
.data
);
48 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
52 *response
= msg
.data
.time
;
57 static int cros_ec_rtc_set(struct cros_ec_device
*cros_ec
, u32 command
,
62 struct cros_ec_command msg
;
63 struct ec_response_rtc data
;
66 memset(&msg
, 0, sizeof(msg
));
67 msg
.msg
.command
= command
;
68 msg
.msg
.outsize
= sizeof(msg
.data
);
69 msg
.data
.time
= param
;
71 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
77 /* Read the current time from the EC. */
78 static int cros_ec_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
80 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
81 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
85 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, &time
);
87 dev_err(dev
, "error getting time: %d\n", ret
);
91 rtc_time64_to_tm(time
, tm
);
96 /* Set the current EC time. */
97 static int cros_ec_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
99 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
100 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
102 time64_t time
= rtc_tm_to_time64(tm
);
104 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_VALUE
, (u32
)time
);
106 dev_err(dev
, "error setting time: %d\n", ret
);
113 /* Read alarm time from RTC. */
114 static int cros_ec_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
116 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
117 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
119 u32 current_time
, alarm_offset
;
122 * The EC host command for getting the alarm is relative (i.e. 5
123 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
124 * RTC time first so we can calculate the relative time.
126 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
128 dev_err(dev
, "error getting time: %d\n", ret
);
132 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
, &alarm_offset
);
134 dev_err(dev
, "error getting alarm: %d\n", ret
);
138 rtc_time64_to_tm(current_time
+ alarm_offset
, &alrm
->time
);
143 /* Set the EC's RTC alarm. */
144 static int cros_ec_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
146 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
147 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
150 u32 current_time
, alarm_offset
;
153 * The EC host command for setting the alarm is relative
154 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
155 * Get the current RTC time first so we can calculate the
158 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
160 dev_err(dev
, "error getting time: %d\n", ret
);
164 alarm_time
= rtc_tm_to_time64(&alrm
->time
);
166 if (alarm_time
< 0 || alarm_time
> U32_MAX
)
169 if (!alrm
->enabled
) {
171 * If the alarm is being disabled, send an alarm
174 alarm_offset
= EC_RTC_ALARM_CLEAR
;
175 cros_ec_rtc
->saved_alarm
= (u32
)alarm_time
;
177 /* Don't set an alarm in the past. */
178 if ((u32
)alarm_time
<= current_time
)
181 alarm_offset
= (u32
)alarm_time
- current_time
;
184 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
, alarm_offset
);
186 dev_err(dev
, "error setting alarm in %u seconds: %d\n",
189 * The EC code returns -EINVAL if the alarm time is too
190 * far in the future. Convert it to the expected error code.
200 static int cros_ec_rtc_alarm_irq_enable(struct device
*dev
,
201 unsigned int enabled
)
203 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
204 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
206 u32 current_time
, alarm_offset
, alarm_value
;
208 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
210 dev_err(dev
, "error getting time: %d\n", ret
);
215 /* Restore saved alarm if it's still in the future. */
216 if (cros_ec_rtc
->saved_alarm
< current_time
)
217 alarm_offset
= EC_RTC_ALARM_CLEAR
;
219 alarm_offset
= cros_ec_rtc
->saved_alarm
- current_time
;
221 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
224 dev_err(dev
, "error restoring alarm: %d\n", ret
);
228 /* Disable alarm, saving the old alarm value. */
229 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
,
232 dev_err(dev
, "error saving alarm: %d\n", ret
);
236 alarm_value
= current_time
+ alarm_offset
;
239 * If the current EC alarm is already past, we don't want
240 * to set an alarm when we go through the alarm irq enable
243 if (alarm_value
< current_time
)
244 cros_ec_rtc
->saved_alarm
= EC_RTC_ALARM_CLEAR
;
246 cros_ec_rtc
->saved_alarm
= alarm_value
;
248 alarm_offset
= EC_RTC_ALARM_CLEAR
;
249 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
252 dev_err(dev
, "error disabling alarm: %d\n", ret
);
260 static int cros_ec_rtc_event(struct notifier_block
*nb
,
261 unsigned long queued_during_suspend
,
264 struct cros_ec_rtc
*cros_ec_rtc
;
265 struct rtc_device
*rtc
;
266 struct cros_ec_device
*cros_ec
;
269 cros_ec_rtc
= container_of(nb
, struct cros_ec_rtc
, notifier
);
270 rtc
= cros_ec_rtc
->rtc
;
271 cros_ec
= cros_ec_rtc
->cros_ec
;
273 host_event
= cros_ec_get_host_event(cros_ec
);
274 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC
)) {
275 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
282 static const struct rtc_class_ops cros_ec_rtc_ops
= {
283 .read_time
= cros_ec_rtc_read_time
,
284 .set_time
= cros_ec_rtc_set_time
,
285 .read_alarm
= cros_ec_rtc_read_alarm
,
286 .set_alarm
= cros_ec_rtc_set_alarm
,
287 .alarm_irq_enable
= cros_ec_rtc_alarm_irq_enable
,
290 #ifdef CONFIG_PM_SLEEP
291 static int cros_ec_rtc_suspend(struct device
*dev
)
293 struct platform_device
*pdev
= to_platform_device(dev
);
294 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
296 if (device_may_wakeup(dev
))
297 return enable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
302 static int cros_ec_rtc_resume(struct device
*dev
)
304 struct platform_device
*pdev
= to_platform_device(dev
);
305 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
307 if (device_may_wakeup(dev
))
308 return disable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
314 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops
, cros_ec_rtc_suspend
,
317 static int cros_ec_rtc_probe(struct platform_device
*pdev
)
319 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pdev
->dev
.parent
);
320 struct cros_ec_device
*cros_ec
= ec_dev
->ec_dev
;
321 struct cros_ec_rtc
*cros_ec_rtc
;
325 cros_ec_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*cros_ec_rtc
),
330 platform_set_drvdata(pdev
, cros_ec_rtc
);
331 cros_ec_rtc
->cros_ec
= cros_ec
;
333 /* Get initial time */
334 ret
= cros_ec_rtc_read_time(&pdev
->dev
, &tm
);
336 dev_err(&pdev
->dev
, "failed to read RTC time\n");
340 ret
= device_init_wakeup(&pdev
->dev
, 1);
342 dev_err(&pdev
->dev
, "failed to initialize wakeup\n");
346 cros_ec_rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
347 if (IS_ERR(cros_ec_rtc
->rtc
))
348 return PTR_ERR(cros_ec_rtc
->rtc
);
350 cros_ec_rtc
->rtc
->ops
= &cros_ec_rtc_ops
;
351 cros_ec_rtc
->rtc
->range_max
= U32_MAX
;
354 * The RTC on some older Chromebooks can only handle alarms less than
355 * 24 hours in the future. The only way to find out is to try to set an
356 * alarm further in the future. If that fails, assume that the RTC
357 * connected to the EC can only handle less than 24 hours of alarm
360 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
, SECS_PER_DAY
* 2);
362 cros_ec_rtc
->rtc
->alarm_offset_max
= SECS_PER_DAY
- 1;
364 (void)cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
367 ret
= devm_rtc_register_device(cros_ec_rtc
->rtc
);
371 /* Get RTC events from the EC. */
372 cros_ec_rtc
->notifier
.notifier_call
= cros_ec_rtc_event
;
373 ret
= blocking_notifier_chain_register(&cros_ec
->event_notifier
,
374 &cros_ec_rtc
->notifier
);
376 dev_err(&pdev
->dev
, "failed to register notifier\n");
383 static void cros_ec_rtc_remove(struct platform_device
*pdev
)
385 struct cros_ec_rtc
*cros_ec_rtc
= platform_get_drvdata(pdev
);
386 struct device
*dev
= &pdev
->dev
;
389 ret
= blocking_notifier_chain_unregister(
390 &cros_ec_rtc
->cros_ec
->event_notifier
,
391 &cros_ec_rtc
->notifier
);
393 dev_err(dev
, "failed to unregister notifier\n");
396 static const struct platform_device_id cros_ec_rtc_id
[] = {
400 MODULE_DEVICE_TABLE(platform
, cros_ec_rtc_id
);
402 static struct platform_driver cros_ec_rtc_driver
= {
403 .probe
= cros_ec_rtc_probe
,
404 .remove
= cros_ec_rtc_remove
,
407 .pm
= &cros_ec_rtc_pm_ops
,
409 .id_table
= cros_ec_rtc_id
,
412 module_platform_driver(cros_ec_rtc_driver
);
414 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
415 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
416 MODULE_LICENSE("GPL v2");