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/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"
19 * struct cros_ec_rtc - Driver data for EC RTC
21 * @cros_ec: Pointer to EC device
22 * @rtc: Pointer to RTC device
23 * @notifier: Notifier info for responding to EC events
24 * @saved_alarm: Alarm to restore when interrupts are reenabled
27 struct cros_ec_device
*cros_ec
;
28 struct rtc_device
*rtc
;
29 struct notifier_block notifier
;
33 static int cros_ec_rtc_get(struct cros_ec_device
*cros_ec
, u32 command
,
38 struct cros_ec_command msg
;
39 struct ec_response_rtc data
;
42 memset(&msg
, 0, sizeof(msg
));
43 msg
.msg
.command
= command
;
44 msg
.msg
.insize
= sizeof(msg
.data
);
46 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
49 "error getting %s from EC: %d\n",
50 command
== EC_CMD_RTC_GET_VALUE
? "time" : "alarm",
55 *response
= msg
.data
.time
;
60 static int cros_ec_rtc_set(struct cros_ec_device
*cros_ec
, u32 command
,
65 struct cros_ec_command msg
;
66 struct ec_response_rtc data
;
69 memset(&msg
, 0, sizeof(msg
));
70 msg
.msg
.command
= command
;
71 msg
.msg
.outsize
= sizeof(msg
.data
);
72 msg
.data
.time
= param
;
74 ret
= cros_ec_cmd_xfer_status(cros_ec
, &msg
.msg
);
76 dev_err(cros_ec
->dev
, "error setting %s on EC: %d\n",
77 command
== EC_CMD_RTC_SET_VALUE
? "time" : "alarm",
85 /* Read the current time from the EC. */
86 static int cros_ec_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
88 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
89 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
93 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, &time
);
95 dev_err(dev
, "error getting time: %d\n", ret
);
99 rtc_time64_to_tm(time
, tm
);
104 /* Set the current EC time. */
105 static int cros_ec_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
107 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
108 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
112 time
= rtc_tm_to_time64(tm
);
113 if (time
< 0 || time
> U32_MAX
)
116 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_VALUE
, (u32
)time
);
118 dev_err(dev
, "error setting time: %d\n", ret
);
125 /* Read alarm time from RTC. */
126 static int cros_ec_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
128 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
129 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
131 u32 current_time
, alarm_offset
;
134 * The EC host command for getting the alarm is relative (i.e. 5
135 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
136 * RTC time first so we can calculate the relative time.
138 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
140 dev_err(dev
, "error getting time: %d\n", ret
);
144 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
, &alarm_offset
);
146 dev_err(dev
, "error getting alarm: %d\n", ret
);
150 rtc_time64_to_tm(current_time
+ alarm_offset
, &alrm
->time
);
155 /* Set the EC's RTC alarm. */
156 static int cros_ec_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
158 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
159 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
162 u32 current_time
, alarm_offset
;
165 * The EC host command for setting the alarm is relative
166 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
167 * Get the current RTC time first so we can calculate the
170 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
172 dev_err(dev
, "error getting time: %d\n", ret
);
176 alarm_time
= rtc_tm_to_time64(&alrm
->time
);
178 if (alarm_time
< 0 || alarm_time
> U32_MAX
)
181 if (!alrm
->enabled
) {
183 * If the alarm is being disabled, send an alarm
186 alarm_offset
= EC_RTC_ALARM_CLEAR
;
187 cros_ec_rtc
->saved_alarm
= (u32
)alarm_time
;
189 /* Don't set an alarm in the past. */
190 if ((u32
)alarm_time
<= current_time
)
193 alarm_offset
= (u32
)alarm_time
- current_time
;
196 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
, alarm_offset
);
198 dev_err(dev
, "error setting alarm: %d\n", ret
);
205 static int cros_ec_rtc_alarm_irq_enable(struct device
*dev
,
206 unsigned int enabled
)
208 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(dev
);
209 struct cros_ec_device
*cros_ec
= cros_ec_rtc
->cros_ec
;
211 u32 current_time
, alarm_offset
, alarm_value
;
213 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_VALUE
, ¤t_time
);
215 dev_err(dev
, "error getting time: %d\n", ret
);
220 /* Restore saved alarm if it's still in the future. */
221 if (cros_ec_rtc
->saved_alarm
< current_time
)
222 alarm_offset
= EC_RTC_ALARM_CLEAR
;
224 alarm_offset
= cros_ec_rtc
->saved_alarm
- current_time
;
226 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
229 dev_err(dev
, "error restoring alarm: %d\n", ret
);
233 /* Disable alarm, saving the old alarm value. */
234 ret
= cros_ec_rtc_get(cros_ec
, EC_CMD_RTC_GET_ALARM
,
237 dev_err(dev
, "error saving alarm: %d\n", ret
);
241 alarm_value
= current_time
+ alarm_offset
;
244 * If the current EC alarm is already past, we don't want
245 * to set an alarm when we go through the alarm irq enable
248 if (alarm_value
< current_time
)
249 cros_ec_rtc
->saved_alarm
= EC_RTC_ALARM_CLEAR
;
251 cros_ec_rtc
->saved_alarm
= alarm_value
;
253 alarm_offset
= EC_RTC_ALARM_CLEAR
;
254 ret
= cros_ec_rtc_set(cros_ec
, EC_CMD_RTC_SET_ALARM
,
257 dev_err(dev
, "error disabling alarm: %d\n", ret
);
265 static int cros_ec_rtc_event(struct notifier_block
*nb
,
266 unsigned long queued_during_suspend
,
269 struct cros_ec_rtc
*cros_ec_rtc
;
270 struct rtc_device
*rtc
;
271 struct cros_ec_device
*cros_ec
;
274 cros_ec_rtc
= container_of(nb
, struct cros_ec_rtc
, notifier
);
275 rtc
= cros_ec_rtc
->rtc
;
276 cros_ec
= cros_ec_rtc
->cros_ec
;
278 host_event
= cros_ec_get_host_event(cros_ec
);
279 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC
)) {
280 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
287 static const struct rtc_class_ops cros_ec_rtc_ops
= {
288 .read_time
= cros_ec_rtc_read_time
,
289 .set_time
= cros_ec_rtc_set_time
,
290 .read_alarm
= cros_ec_rtc_read_alarm
,
291 .set_alarm
= cros_ec_rtc_set_alarm
,
292 .alarm_irq_enable
= cros_ec_rtc_alarm_irq_enable
,
295 #ifdef CONFIG_PM_SLEEP
296 static int cros_ec_rtc_suspend(struct device
*dev
)
298 struct platform_device
*pdev
= to_platform_device(dev
);
299 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
301 if (device_may_wakeup(dev
))
302 return enable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
307 static int cros_ec_rtc_resume(struct device
*dev
)
309 struct platform_device
*pdev
= to_platform_device(dev
);
310 struct cros_ec_rtc
*cros_ec_rtc
= dev_get_drvdata(&pdev
->dev
);
312 if (device_may_wakeup(dev
))
313 return disable_irq_wake(cros_ec_rtc
->cros_ec
->irq
);
319 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops
, cros_ec_rtc_suspend
,
322 static int cros_ec_rtc_probe(struct platform_device
*pdev
)
324 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pdev
->dev
.parent
);
325 struct cros_ec_device
*cros_ec
= ec_dev
->ec_dev
;
326 struct cros_ec_rtc
*cros_ec_rtc
;
330 cros_ec_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*cros_ec_rtc
),
335 platform_set_drvdata(pdev
, cros_ec_rtc
);
336 cros_ec_rtc
->cros_ec
= cros_ec
;
338 /* Get initial time */
339 ret
= cros_ec_rtc_read_time(&pdev
->dev
, &tm
);
341 dev_err(&pdev
->dev
, "failed to read RTC time\n");
345 ret
= device_init_wakeup(&pdev
->dev
, 1);
347 dev_err(&pdev
->dev
, "failed to initialize wakeup\n");
351 cros_ec_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, DRV_NAME
,
354 if (IS_ERR(cros_ec_rtc
->rtc
)) {
355 ret
= PTR_ERR(cros_ec_rtc
->rtc
);
356 dev_err(&pdev
->dev
, "failed to register rtc device\n");
360 /* Get RTC events from the EC. */
361 cros_ec_rtc
->notifier
.notifier_call
= cros_ec_rtc_event
;
362 ret
= blocking_notifier_chain_register(&cros_ec
->event_notifier
,
363 &cros_ec_rtc
->notifier
);
365 dev_err(&pdev
->dev
, "failed to register notifier\n");
372 static int cros_ec_rtc_remove(struct platform_device
*pdev
)
374 struct cros_ec_rtc
*cros_ec_rtc
= platform_get_drvdata(pdev
);
375 struct device
*dev
= &pdev
->dev
;
378 ret
= blocking_notifier_chain_unregister(
379 &cros_ec_rtc
->cros_ec
->event_notifier
,
380 &cros_ec_rtc
->notifier
);
382 dev_err(dev
, "failed to unregister notifier\n");
389 static struct platform_driver cros_ec_rtc_driver
= {
390 .probe
= cros_ec_rtc_probe
,
391 .remove
= cros_ec_rtc_remove
,
394 .pm
= &cros_ec_rtc_pm_ops
,
398 module_platform_driver(cros_ec_rtc_driver
);
400 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
401 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
402 MODULE_LICENSE("GPL v2");
403 MODULE_ALIAS("platform:" DRV_NAME
);