summaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-cros-ec.c
blob: 887f5193e253da592a0340fd8c2b80af10aefc00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: GPL-2.0
// RTC driver for ChromeOS Embedded Controller.
//
// Copyright (C) 2017 Google, Inc.
// Author: Stephen Barber <smbarber@chromium.org>

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/slab.h>

#define DRV_NAME	"cros-ec-rtc"

/**
 * struct cros_ec_rtc - Driver data for EC RTC
 *
 * @cros_ec: Pointer to EC device
 * @rtc: Pointer to RTC device
 * @notifier: Notifier info for responding to EC events
 * @saved_alarm: Alarm to restore when interrupts are reenabled
 */
struct cros_ec_rtc {
	struct cros_ec_device *cros_ec;
	struct rtc_device *rtc;
	struct notifier_block notifier;
	u32 saved_alarm;
};

static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
			   u32 *response)
{
	int ret;
	struct {
		struct cros_ec_command msg;
		struct ec_response_rtc data;
	} __packed msg;

	memset(&msg, 0, sizeof(msg));
	msg.msg.command = command;
	msg.msg.insize = sizeof(msg.data);

	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
	if (ret < 0) {
		dev_err(cros_ec->dev,
			"error getting %s from EC: %d\n",
			command == EC_CMD_RTC_GET_VALUE ? "time" : "alarm",
			ret);
		return ret;
	}

	*response = msg.data.time;

	return 0;
}

static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
			   u32 param)
{
	int ret = 0;
	struct {
		struct cros_ec_command msg;
		struct ec_response_rtc data;
	} __packed msg;

	memset(&msg, 0, sizeof(msg));
	msg.msg.command = command;
	msg.msg.outsize = sizeof(msg.data);
	msg.data.time = param;

	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
	if (ret < 0) {
		dev_err(cros_ec->dev, "error setting %s on EC: %d\n",
			command == EC_CMD_RTC_SET_VALUE ? "time" : "alarm",
			ret);
		return ret;
	}

	return 0;
}

/* Read the current time from the EC. */
static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	u32 time;

	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
	if (ret) {
		dev_err(dev, "error getting time: %d\n", ret);
		return ret;
	}

	rtc_time64_to_tm(time, tm);

	return 0;
}

/* Set the current EC time. */
static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	time64_t time = rtc_tm_to_time64(tm);

	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
	if (ret < 0) {
		dev_err(dev, "error setting time: %d\n", ret);
		return ret;
	}

	return 0;
}

/* Read alarm time from RTC. */
static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	u32 current_time, alarm_offset;

	/*
	 * The EC host command for getting the alarm is relative (i.e. 5
	 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
	 * RTC time first so we can calculate the relative time.
	 */
	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
	if (ret < 0) {
		dev_err(dev, "error getting time: %d\n", ret);
		return ret;
	}

	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
	if (ret < 0) {
		dev_err(dev, "error getting alarm: %d\n", ret);
		return ret;
	}

	rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);

	return 0;
}

/* Set the EC's RTC alarm. */
static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	time64_t alarm_time;
	u32 current_time, alarm_offset;

	/*
	 * The EC host command for setting the alarm is relative
	 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
	 * Get the current RTC time first so we can calculate the
	 * relative time.
	 */
	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
	if (ret < 0) {
		dev_err(dev, "error getting time: %d\n", ret);
		return ret;
	}

	alarm_time = rtc_tm_to_time64(&alrm->time);

	if (alarm_time < 0 || alarm_time > U32_MAX)
		return -EINVAL;

	if (!alrm->enabled) {
		/*
		 * If the alarm is being disabled, send an alarm
		 * clear command.
		 */
		alarm_offset = EC_RTC_ALARM_CLEAR;
		cros_ec_rtc->saved_alarm = (u32)alarm_time;
	} else {
		/* Don't set an alarm in the past. */
		if ((u32)alarm_time <= current_time)
			return -ETIME;

		alarm_offset = (u32)alarm_time - current_time;
	}

	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
	if (ret < 0) {
		dev_err(dev, "error setting alarm: %d\n", ret);
		return ret;
	}

	return 0;
}

static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
					unsigned int enabled)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	u32 current_time, alarm_offset, alarm_value;

	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
	if (ret < 0) {
		dev_err(dev, "error getting time: %d\n", ret);
		return ret;
	}

	if (enabled) {
		/* Restore saved alarm if it's still in the future. */
		if (cros_ec_rtc->saved_alarm < current_time)
			alarm_offset = EC_RTC_ALARM_CLEAR;
		else
			alarm_offset = cros_ec_rtc->saved_alarm - current_time;

		ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
				      alarm_offset);
		if (ret < 0) {
			dev_err(dev, "error restoring alarm: %d\n", ret);
			return ret;
		}
	} else {
		/* Disable alarm, saving the old alarm value. */
		ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
				      &alarm_offset);
		if (ret < 0) {
			dev_err(dev, "error saving alarm: %d\n", ret);
			return ret;
		}

		alarm_value = current_time + alarm_offset;

		/*
		 * If the current EC alarm is already past, we don't want
		 * to set an alarm when we go through the alarm irq enable
		 * path.
		 */
		if (alarm_value < current_time)
			cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
		else
			cros_ec_rtc->saved_alarm = alarm_value;

		alarm_offset = EC_RTC_ALARM_CLEAR;
		ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
				      alarm_offset);
		if (ret < 0) {
			dev_err(dev, "error disabling alarm: %d\n", ret);
			return ret;
		}
	}

	return 0;
}

static int cros_ec_rtc_event(struct notifier_block *nb,
			     unsigned long queued_during_suspend,
			     void *_notify)
{
	struct cros_ec_rtc *cros_ec_rtc;
	struct rtc_device *rtc;
	struct cros_ec_device *cros_ec;
	u32 host_event;

	cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
	rtc = cros_ec_rtc->rtc;
	cros_ec = cros_ec_rtc->cros_ec;

	host_event = cros_ec_get_host_event(cros_ec);
	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
		rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
		return NOTIFY_OK;
	} else {
		return NOTIFY_DONE;
	}
}

static const struct rtc_class_ops cros_ec_rtc_ops = {
	.read_time = cros_ec_rtc_read_time,
	.set_time = cros_ec_rtc_set_time,
	.read_alarm = cros_ec_rtc_read_alarm,
	.set_alarm = cros_ec_rtc_set_alarm,
	.alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
};

#ifdef CONFIG_PM_SLEEP
static int cros_ec_rtc_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);

	if (device_may_wakeup(dev))
		return enable_irq_wake(cros_ec_rtc->cros_ec->irq);

	return 0;
}

static int cros_ec_rtc_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);

	if (device_may_wakeup(dev))
		return disable_irq_wake(cros_ec_rtc->cros_ec->irq);

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
			 cros_ec_rtc_resume);

static int cros_ec_rtc_probe(struct platform_device *pdev)
{
	struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
	struct cros_ec_rtc *cros_ec_rtc;
	struct rtc_time tm;
	int ret;

	cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
				   GFP_KERNEL);
	if (!cros_ec_rtc)
		return -ENOMEM;

	platform_set_drvdata(pdev, cros_ec_rtc);
	cros_ec_rtc->cros_ec = cros_ec;

	/* Get initial time */
	ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
	if (ret) {
		dev_err(&pdev->dev, "failed to read RTC time\n");
		return ret;
	}

	ret = device_init_wakeup(&pdev->dev, 1);
	if (ret) {
		dev_err(&pdev->dev, "failed to initialize wakeup\n");
		return ret;
	}

	cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
	if (IS_ERR(cros_ec_rtc->rtc))
		return PTR_ERR(cros_ec_rtc->rtc);

	cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
	cros_ec_rtc->rtc->range_max = U32_MAX;

	ret = devm_rtc_register_device(cros_ec_rtc->rtc);
	if (ret)
		return ret;

	/* Get RTC events from the EC. */
	cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
	ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
					       &cros_ec_rtc->notifier);
	if (ret) {
		dev_err(&pdev->dev, "failed to register notifier\n");
		return ret;
	}

	return 0;
}

static int cros_ec_rtc_remove(struct platform_device *pdev)
{
	struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;
	int ret;

	ret = blocking_notifier_chain_unregister(
				&cros_ec_rtc->cros_ec->event_notifier,
				&cros_ec_rtc->notifier);
	if (ret)
		dev_err(dev, "failed to unregister notifier\n");

	return 0;
}

static struct platform_driver cros_ec_rtc_driver = {
	.probe = cros_ec_rtc_probe,
	.remove = cros_ec_rtc_remove,
	.driver = {
		.name = DRV_NAME,
		.pm = &cros_ec_rtc_pm_ops,
	},
};

module_platform_driver(cros_ec_rtc_driver);

MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRV_NAME);