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