Linux Audio

Check our new training course

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