Linux Audio

Check our new training course

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