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/mfd/cros_ec.h>
9 #include <linux/mfd/cros_ec_commands.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/rtc.h>
13 #include <linux/slab.h>
15 #define DRV_NAME "cros-ec-rtc"
18 * struct cros_ec_rtc - Driver data for EC RTC
20 * @cros_ec: Pointer to EC device
21 * @rtc: Pointer to RTC device
22 * @notifier: Notifier info for responding to EC events
23 * @saved_alarm: Alarm to restore when interrupts are reenabled
26 struct cros_ec_device
*cros_ec
;
27 struct rtc_device
*rtc
;
28 struct notifier_block notifier
;
32 static int cros_ec_rtc_get(struct cros_ec_device
*cros_ec
, u32 command
,
37 struct cros_ec_command msg
;
38 struct ec_response_rtc data
;
41 memset(&msg
, 0, sizeof(msg
));
42 msg
.msg
.command
= command
;
43 msg
.msg
.insize
= sizeof(msg
.data
);
45 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
48 "error getting %s from EC: %d\n",
49 command
== EC_CMD_RTC_GET_VALUE
? "time" : "alarm",
54 *response
= msg
.data
.time
;
59 static int cros_ec_rtc_set(struct cros_ec_device
*cros_ec
, u32 command
,
64 struct cros_ec_command msg
;
65 struct ec_response_rtc data
;
68 memset(&msg
, 0, sizeof(msg
));
69 msg
.msg
.command
= command
;
70 msg
.msg
.outsize
= sizeof(msg
.data
);
71 msg
.data
.time
= param
;
73 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
75 dev_err(cros_ec
->dev
, "error setting %s on EC: %d\n",
76 command
== EC_CMD_RTC_SET_VALUE
? "time" : "alarm",
84 /* Read the current time from the EC. */
85 static int cros_ec_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
87 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
88 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
92 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, &time
);
94 dev_err(dev
, "error getting time: %d\n", ret
);
98 rtc_time64_to_tm(time
, tm
);
103 /* Set the current EC time. */
104 static int cros_ec_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
106 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
107 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
111 time
= rtc_tm_to_time64(tm
);
112 if (time
< 0 || time
> U32_MAX
)
115 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_VALUE
, (u32
)time
);
117 dev_err(dev
, "error setting time: %d\n", ret
);
124 /* Read alarm time from RTC. */
125 static int cros_ec_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
127 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
128 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
130 u32 current_time
, alarm_offset
;
133 * The EC host command for getting the alarm is relative (i.e. 5
134 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
135 * RTC time first so we can calculate the relative time.
137 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
139 dev_err(dev
, "error getting time: %d\n", ret
);
143 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
, &alarm_offset
);
145 dev_err(dev
, "error getting alarm: %d\n", ret
);
149 rtc_time64_to_tm(current_time
+ alarm_offset
, &alrm
->time
);
154 /* Set the EC's RTC alarm. */
155 static int cros_ec_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
157 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
158 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
161 u32 current_time
, alarm_offset
;
164 * The EC host command for setting the alarm is relative
165 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
166 * Get the current RTC time first so we can calculate the
169 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
171 dev_err(dev
, "error getting time: %d\n", ret
);
175 alarm_time
= rtc_tm_to_time64(&alrm
->time
);
177 if (alarm_time
< 0 || alarm_time
> U32_MAX
)
180 if (!alrm
->enabled
) {
182 * If the alarm is being disabled, send an alarm
185 alarm_offset
= EC_RTC_ALARM_CLEAR
;
186 cros_ec_rtc
->saved_alarm
= (u32
)alarm_time
;
188 /* Don't set an alarm in the past. */
189 if ((u32
)alarm_time
<= current_time
)
192 alarm_offset
= (u32
)alarm_time
- current_time
;
195 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
, alarm_offset
);
197 dev_err(dev
, "error setting alarm: %d\n", ret
);
204 static int cros_ec_rtc_alarm_irq_enable(struct device
*dev
,
205 unsigned int enabled
)
207 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
208 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
210 u32 current_time
, alarm_offset
, alarm_value
;
212 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
214 dev_err(dev
, "error getting time: %d\n", ret
);
219 /* Restore saved alarm if it's still in the future. */
220 if (cros_ec_rtc
->saved_alarm
< current_time
)
221 alarm_offset
= EC_RTC_ALARM_CLEAR
;
223 alarm_offset
= cros_ec_rtc
->saved_alarm
- current_time
;
225 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
228 dev_err(dev
, "error restoring alarm: %d\n", ret
);
232 /* Disable alarm, saving the old alarm value. */
233 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
,
236 dev_err(dev
, "error saving alarm: %d\n", ret
);
240 alarm_value
= current_time
+ alarm_offset
;
243 * If the current EC alarm is already past, we don't want
244 * to set an alarm when we go through the alarm irq enable
247 if (alarm_value
< current_time
)
248 cros_ec_rtc
->saved_alarm
= EC_RTC_ALARM_CLEAR
;
250 cros_ec_rtc
->saved_alarm
= alarm_value
;
252 alarm_offset
= EC_RTC_ALARM_CLEAR
;
253 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
256 dev_err(dev
, "error disabling alarm: %d\n", ret
);
264 static int cros_ec_rtc_event(struct notifier_block
*nb
,
265 unsigned long queued_during_suspend
,
268 struct cros_ec_rtc
*cros_ec_rtc
;
269 struct rtc_device
*rtc
;
270 struct cros_ec_device
*cros_ec
;
273 cros_ec_rtc
= container_of(nb
, struct cros_ec_rtc
, notifier
);
274 rtc
= cros_ec_rtc
->rtc
;
275 cros_ec
= cros_ec_rtc
->cros_ec
;
277 host_event
= cros_ec_get_host_event(cros_ec
);
278 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC
)) {
279 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
286 static const struct rtc_class_ops cros_ec_rtc_ops
= {
287 .read_time
= cros_ec_rtc_read_time
,
288 .set_time
= cros_ec_rtc_set_time
,
289 .read_alarm
= cros_ec_rtc_read_alarm
,
290 .set_alarm
= cros_ec_rtc_set_alarm
,
291 .alarm_irq_enable
= cros_ec_rtc_alarm_irq_enable
,
294 #ifdef CONFIG_PM_SLEEP
295 static int cros_ec_rtc_suspend(struct device
*dev
)
297 struct platform_device
*pdev
= to_platform_device(dev
);
298 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
300 if (device_may_wakeup(dev
))
301 enable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
306 static int cros_ec_rtc_resume(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 disable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
318 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops
, cros_ec_rtc_suspend
,
321 static int cros_ec_rtc_probe(struct platform_device
*pdev
)
323 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pdev
->dev
.parent
);
324 struct cros_ec_device
*cros_ec
= ec_dev
->ec_dev
;
325 struct cros_ec_rtc
*cros_ec_rtc
;
329 cros_ec_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*cros_ec_rtc
),
334 platform_set_drvdata(pdev
, cros_ec_rtc
);
335 cros_ec_rtc
->cros_ec
= cros_ec
;
337 /* Get initial time */
338 ret
= cros_ec_rtc_read_time(&pdev
->dev
, &tm
);
340 dev_err(&pdev
->dev
, "failed to read RTC time\n");
344 ret
= device_init_wakeup(&pdev
->dev
, 1);
346 dev_err(&pdev
->dev
, "failed to initialize wakeup\n");
350 cros_ec_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, DRV_NAME
,
353 if (IS_ERR(cros_ec_rtc
->rtc
)) {
354 ret
= PTR_ERR(cros_ec_rtc
->rtc
);
355 dev_err(&pdev
->dev
, "failed to register rtc device\n");
359 /* Get RTC events from the EC. */
360 cros_ec_rtc
->notifier
.notifier_call
= cros_ec_rtc_event
;
361 ret
= blocking_notifier_chain_register(&cros_ec
->event_notifier
,
362 &cros_ec_rtc
->notifier
);
364 dev_err(&pdev
->dev
, "failed to register notifier\n");
371 static int cros_ec_rtc_remove(struct platform_device
*pdev
)
373 struct cros_ec_rtc
*cros_ec_rtc
= platform_get_drvdata(pdev
);
374 struct device
*dev
= &pdev
->dev
;
377 ret
= blocking_notifier_chain_unregister(
378 &cros_ec_rtc
->cros_ec
->event_notifier
,
379 &cros_ec_rtc
->notifier
);
381 dev_err(dev
, "failed to unregister notifier\n");
388 static struct platform_driver cros_ec_rtc_driver
= {
389 .probe
= cros_ec_rtc_probe
,
390 .remove
= cros_ec_rtc_remove
,
393 .pm
= &cros_ec_rtc_pm_ops
,
397 module_platform_driver(cros_ec_rtc_driver
);
399 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
400 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
401 MODULE_LICENSE("GPL v2");
402 MODULE_ALIAS("platform:" DRV_NAME
);