Linux Audio

Check our new training course

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