1 // SPDX-License-Identifier: GPL-2.0
2 // RTC driver for ChromeOS Embedded Controller.
3 //
4 // Copyright (C) 2017 Google, Inc.
5 // Author: Stephen Barber <smbarber@chromium.org>
6
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>
14
15 #define DRV_NAME "cros-ec-rtc"
16
17 #define SECS_PER_DAY (24 * 60 * 60)
18
19 /**
20 * struct cros_ec_rtc - Driver data for EC RTC
21 *
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
26 */
27 struct cros_ec_rtc {
28 struct cros_ec_device *cros_ec;
29 struct rtc_device *rtc;
30 struct notifier_block notifier;
31 u32 saved_alarm;
32 };
33
cros_ec_rtc_get(struct cros_ec_device * cros_ec,u32 command,u32 * response)34 static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
35 u32 *response)
36 {
37 DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
38 sizeof(struct ec_response_rtc));
39 int ret;
40
41 msg->command = command;
42 msg->insize = sizeof(struct ec_response_rtc);
43
44 ret = cros_ec_cmd_xfer_status(cros_ec, msg);
45 if (ret < 0)
46 return ret;
47
48 *response = ((struct ec_response_rtc *)msg->data)->time;
49
50 return 0;
51 }
52
cros_ec_rtc_set(struct cros_ec_device * cros_ec,u32 command,u32 param)53 static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
54 u32 param)
55 {
56 DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
57 sizeof(struct ec_response_rtc));
58 int ret;
59
60 msg->command = command;
61 msg->outsize = sizeof(struct ec_response_rtc);
62 ((struct ec_response_rtc *)msg->data)->time = param;
63
64 ret = cros_ec_cmd_xfer_status(cros_ec, msg);
65 if (ret < 0)
66 return ret;
67 return 0;
68 }
69
70 /* Read the current time from the EC. */
cros_ec_rtc_read_time(struct device * dev,struct rtc_time * tm)71 static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
72 {
73 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
74 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
75 int ret;
76 u32 time;
77
78 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
79 if (ret) {
80 dev_err(dev, "error getting time: %d\n", ret);
81 return ret;
82 }
83
84 rtc_time64_to_tm(time, tm);
85
86 return 0;
87 }
88
89 /* Set the current EC time. */
cros_ec_rtc_set_time(struct device * dev,struct rtc_time * tm)90 static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
91 {
92 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
93 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
94 int ret;
95 time64_t time = rtc_tm_to_time64(tm);
96
97 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
98 if (ret < 0) {
99 dev_err(dev, "error setting time: %d\n", ret);
100 return ret;
101 }
102
103 return 0;
104 }
105
106 /* Read alarm time from RTC. */
cros_ec_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)107 static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
108 {
109 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
110 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
111 int ret;
112 u32 current_time, alarm_offset;
113
114 /*
115 * The EC host command for getting the alarm is relative (i.e. 5
116 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
117 * RTC time first so we can calculate the relative time.
118 */
119 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
120 if (ret < 0) {
121 dev_err(dev, "error getting time: %d\n", ret);
122 return ret;
123 }
124
125 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
126 if (ret < 0) {
127 dev_err(dev, "error getting alarm: %d\n", ret);
128 return ret;
129 }
130
131 rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
132
133 return 0;
134 }
135
136 /* Set the EC's RTC alarm. */
cros_ec_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)137 static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
138 {
139 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
140 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
141 int ret;
142 time64_t alarm_time;
143 u32 current_time, alarm_offset;
144
145 /*
146 * The EC host command for setting the alarm is relative
147 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
148 * Get the current RTC time first so we can calculate the
149 * relative time.
150 */
151 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
152 if (ret < 0) {
153 dev_err(dev, "error getting time: %d\n", ret);
154 return ret;
155 }
156
157 alarm_time = rtc_tm_to_time64(&alrm->time);
158
159 if (alarm_time < 0 || alarm_time > U32_MAX)
160 return -EINVAL;
161
162 if (!alrm->enabled) {
163 /*
164 * If the alarm is being disabled, send an alarm
165 * clear command.
166 */
167 alarm_offset = EC_RTC_ALARM_CLEAR;
168 cros_ec_rtc->saved_alarm = (u32)alarm_time;
169 } else {
170 /* Don't set an alarm in the past. */
171 if ((u32)alarm_time <= current_time)
172 return -ETIME;
173
174 alarm_offset = (u32)alarm_time - current_time;
175 }
176
177 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
178 if (ret < 0) {
179 dev_err(dev, "error setting alarm in %u seconds: %d\n",
180 alarm_offset, ret);
181 /*
182 * The EC code returns -EINVAL if the alarm time is too
183 * far in the future. Convert it to the expected error code.
184 */
185 if (ret == -EINVAL)
186 ret = -ERANGE;
187 return ret;
188 }
189
190 return 0;
191 }
192
cros_ec_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)193 static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
194 unsigned int enabled)
195 {
196 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
197 struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
198 int ret;
199 u32 current_time, alarm_offset, alarm_value;
200
201 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
202 if (ret < 0) {
203 dev_err(dev, "error getting time: %d\n", ret);
204 return ret;
205 }
206
207 if (enabled) {
208 /* Restore saved alarm if it's still in the future. */
209 if (cros_ec_rtc->saved_alarm < current_time)
210 alarm_offset = EC_RTC_ALARM_CLEAR;
211 else
212 alarm_offset = cros_ec_rtc->saved_alarm - current_time;
213
214 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
215 alarm_offset);
216 if (ret < 0) {
217 dev_err(dev, "error restoring alarm: %d\n", ret);
218 return ret;
219 }
220 } else {
221 /* Disable alarm, saving the old alarm value. */
222 ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
223 &alarm_offset);
224 if (ret < 0) {
225 dev_err(dev, "error saving alarm: %d\n", ret);
226 return ret;
227 }
228
229 alarm_value = current_time + alarm_offset;
230
231 /*
232 * If the current EC alarm is already past, we don't want
233 * to set an alarm when we go through the alarm irq enable
234 * path.
235 */
236 if (alarm_value < current_time)
237 cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
238 else
239 cros_ec_rtc->saved_alarm = alarm_value;
240
241 alarm_offset = EC_RTC_ALARM_CLEAR;
242 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
243 alarm_offset);
244 if (ret < 0) {
245 dev_err(dev, "error disabling alarm: %d\n", ret);
246 return ret;
247 }
248 }
249
250 return 0;
251 }
252
cros_ec_rtc_event(struct notifier_block * nb,unsigned long queued_during_suspend,void * _notify)253 static int cros_ec_rtc_event(struct notifier_block *nb,
254 unsigned long queued_during_suspend,
255 void *_notify)
256 {
257 struct cros_ec_rtc *cros_ec_rtc;
258 struct rtc_device *rtc;
259 struct cros_ec_device *cros_ec;
260 u32 host_event;
261
262 cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
263 rtc = cros_ec_rtc->rtc;
264 cros_ec = cros_ec_rtc->cros_ec;
265
266 host_event = cros_ec_get_host_event(cros_ec);
267 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
268 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
269 return NOTIFY_OK;
270 } else {
271 return NOTIFY_DONE;
272 }
273 }
274
275 static const struct rtc_class_ops cros_ec_rtc_ops = {
276 .read_time = cros_ec_rtc_read_time,
277 .set_time = cros_ec_rtc_set_time,
278 .read_alarm = cros_ec_rtc_read_alarm,
279 .set_alarm = cros_ec_rtc_set_alarm,
280 .alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
281 };
282
283 #ifdef CONFIG_PM_SLEEP
cros_ec_rtc_suspend(struct device * dev)284 static int cros_ec_rtc_suspend(struct device *dev)
285 {
286 struct platform_device *pdev = to_platform_device(dev);
287 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
288
289 if (device_may_wakeup(dev))
290 return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
291
292 return 0;
293 }
294
cros_ec_rtc_resume(struct device * dev)295 static int cros_ec_rtc_resume(struct device *dev)
296 {
297 struct platform_device *pdev = to_platform_device(dev);
298 struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
299
300 if (device_may_wakeup(dev))
301 return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
302
303 return 0;
304 }
305 #endif
306
307 static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
308 cros_ec_rtc_resume);
309
cros_ec_rtc_probe(struct platform_device * pdev)310 static int cros_ec_rtc_probe(struct platform_device *pdev)
311 {
312 struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
313 struct cros_ec_device *cros_ec = ec_dev->ec_dev;
314 struct cros_ec_rtc *cros_ec_rtc;
315 struct rtc_time tm;
316 int ret;
317
318 cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
319 GFP_KERNEL);
320 if (!cros_ec_rtc)
321 return -ENOMEM;
322
323 platform_set_drvdata(pdev, cros_ec_rtc);
324 cros_ec_rtc->cros_ec = cros_ec;
325
326 /* Get initial time */
327 ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
328 if (ret) {
329 dev_err(&pdev->dev, "failed to read RTC time\n");
330 return ret;
331 }
332
333 ret = device_init_wakeup(&pdev->dev, true);
334 if (ret) {
335 dev_err(&pdev->dev, "failed to initialize wakeup\n");
336 return ret;
337 }
338
339 cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
340 if (IS_ERR(cros_ec_rtc->rtc))
341 return PTR_ERR(cros_ec_rtc->rtc);
342
343 cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
344 cros_ec_rtc->rtc->range_max = U32_MAX;
345
346 /*
347 * The RTC on some older Chromebooks can only handle alarms less than
348 * 24 hours in the future. The only way to find out is to try to set an
349 * alarm further in the future. If that fails, assume that the RTC
350 * connected to the EC can only handle less than 24 hours of alarm
351 * window.
352 */
353 ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, SECS_PER_DAY * 2);
354 if (ret == -EINVAL)
355 cros_ec_rtc->rtc->alarm_offset_max = SECS_PER_DAY - 1;
356
357 (void)cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
358 EC_RTC_ALARM_CLEAR);
359
360 ret = devm_rtc_register_device(cros_ec_rtc->rtc);
361 if (ret)
362 return ret;
363
364 /* Get RTC events from the EC. */
365 cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
366 ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
367 &cros_ec_rtc->notifier);
368 if (ret) {
369 dev_err(&pdev->dev, "failed to register notifier\n");
370 return ret;
371 }
372
373 return 0;
374 }
375
cros_ec_rtc_remove(struct platform_device * pdev)376 static void cros_ec_rtc_remove(struct platform_device *pdev)
377 {
378 struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
379 struct device *dev = &pdev->dev;
380 int ret;
381
382 ret = blocking_notifier_chain_unregister(
383 &cros_ec_rtc->cros_ec->event_notifier,
384 &cros_ec_rtc->notifier);
385 if (ret)
386 dev_err(dev, "failed to unregister notifier\n");
387 }
388
389 static const struct platform_device_id cros_ec_rtc_id[] = {
390 { .name = DRV_NAME },
391 { }
392 };
393 MODULE_DEVICE_TABLE(platform, cros_ec_rtc_id);
394
395 static struct platform_driver cros_ec_rtc_driver = {
396 .probe = cros_ec_rtc_probe,
397 .remove = cros_ec_rtc_remove,
398 .driver = {
399 .name = DRV_NAME,
400 .pm = &cros_ec_rtc_pm_ops,
401 },
402 .id_table = cros_ec_rtc_id,
403 };
404
405 module_platform_driver(cros_ec_rtc_driver);
406
407 MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
408 MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
409 MODULE_LICENSE("GPL v2");
410