18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// RTC driver for ChromeOS Embedded Controller.
38c2ecf20Sopenharmony_ci//
48c2ecf20Sopenharmony_ci// Copyright (C) 2017 Google, Inc.
58c2ecf20Sopenharmony_ci// Author: Stephen Barber <smbarber@chromium.org>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h>
108c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/rtc.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define DRV_NAME	"cros-ec-rtc"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/**
188c2ecf20Sopenharmony_ci * struct cros_ec_rtc - Driver data for EC RTC
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * @cros_ec: Pointer to EC device
218c2ecf20Sopenharmony_ci * @rtc: Pointer to RTC device
228c2ecf20Sopenharmony_ci * @notifier: Notifier info for responding to EC events
238c2ecf20Sopenharmony_ci * @saved_alarm: Alarm to restore when interrupts are reenabled
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_cistruct cros_ec_rtc {
268c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec;
278c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
288c2ecf20Sopenharmony_ci	struct notifier_block notifier;
298c2ecf20Sopenharmony_ci	u32 saved_alarm;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
338c2ecf20Sopenharmony_ci			   u32 *response)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	int ret;
368c2ecf20Sopenharmony_ci	struct {
378c2ecf20Sopenharmony_ci		struct cros_ec_command msg;
388c2ecf20Sopenharmony_ci		struct ec_response_rtc data;
398c2ecf20Sopenharmony_ci	} __packed msg;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	memset(&msg, 0, sizeof(msg));
428c2ecf20Sopenharmony_ci	msg.msg.command = command;
438c2ecf20Sopenharmony_ci	msg.msg.insize = sizeof(msg.data);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
468c2ecf20Sopenharmony_ci	if (ret < 0) {
478c2ecf20Sopenharmony_ci		dev_err(cros_ec->dev,
488c2ecf20Sopenharmony_ci			"error getting %s from EC: %d\n",
498c2ecf20Sopenharmony_ci			command == EC_CMD_RTC_GET_VALUE ? "time" : "alarm",
508c2ecf20Sopenharmony_ci			ret);
518c2ecf20Sopenharmony_ci		return ret;
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	*response = msg.data.time;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return 0;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
608c2ecf20Sopenharmony_ci			   u32 param)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	int ret = 0;
638c2ecf20Sopenharmony_ci	struct {
648c2ecf20Sopenharmony_ci		struct cros_ec_command msg;
658c2ecf20Sopenharmony_ci		struct ec_response_rtc data;
668c2ecf20Sopenharmony_ci	} __packed msg;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	memset(&msg, 0, sizeof(msg));
698c2ecf20Sopenharmony_ci	msg.msg.command = command;
708c2ecf20Sopenharmony_ci	msg.msg.outsize = sizeof(msg.data);
718c2ecf20Sopenharmony_ci	msg.data.time = param;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
748c2ecf20Sopenharmony_ci	if (ret < 0) {
758c2ecf20Sopenharmony_ci		dev_err(cros_ec->dev, "error setting %s on EC: %d\n",
768c2ecf20Sopenharmony_ci			command == EC_CMD_RTC_SET_VALUE ? "time" : "alarm",
778c2ecf20Sopenharmony_ci			ret);
788c2ecf20Sopenharmony_ci		return ret;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* Read the current time from the EC. */
858c2ecf20Sopenharmony_cistatic int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
888c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
898c2ecf20Sopenharmony_ci	int ret;
908c2ecf20Sopenharmony_ci	u32 time;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
938c2ecf20Sopenharmony_ci	if (ret) {
948c2ecf20Sopenharmony_ci		dev_err(dev, "error getting time: %d\n", ret);
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	rtc_time64_to_tm(time, tm);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return 0;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Set the current EC time. */
1048c2ecf20Sopenharmony_cistatic int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
1078c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
1088c2ecf20Sopenharmony_ci	int ret;
1098c2ecf20Sopenharmony_ci	time64_t time = rtc_tm_to_time64(tm);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
1128c2ecf20Sopenharmony_ci	if (ret < 0) {
1138c2ecf20Sopenharmony_ci		dev_err(dev, "error setting time: %d\n", ret);
1148c2ecf20Sopenharmony_ci		return ret;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return 0;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Read alarm time from RTC. */
1218c2ecf20Sopenharmony_cistatic int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
1248c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
1258c2ecf20Sopenharmony_ci	int ret;
1268c2ecf20Sopenharmony_ci	u32 current_time, alarm_offset;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/*
1298c2ecf20Sopenharmony_ci	 * The EC host command for getting the alarm is relative (i.e. 5
1308c2ecf20Sopenharmony_ci	 * seconds from now) whereas rtc_wkalrm is absolute. Get the current
1318c2ecf20Sopenharmony_ci	 * RTC time first so we can calculate the relative time.
1328c2ecf20Sopenharmony_ci	 */
1338c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
1348c2ecf20Sopenharmony_ci	if (ret < 0) {
1358c2ecf20Sopenharmony_ci		dev_err(dev, "error getting time: %d\n", ret);
1368c2ecf20Sopenharmony_ci		return ret;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
1408c2ecf20Sopenharmony_ci	if (ret < 0) {
1418c2ecf20Sopenharmony_ci		dev_err(dev, "error getting alarm: %d\n", ret);
1428c2ecf20Sopenharmony_ci		return ret;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* Set the EC's RTC alarm. */
1518c2ecf20Sopenharmony_cistatic int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
1548c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
1558c2ecf20Sopenharmony_ci	int ret;
1568c2ecf20Sopenharmony_ci	time64_t alarm_time;
1578c2ecf20Sopenharmony_ci	u32 current_time, alarm_offset;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/*
1608c2ecf20Sopenharmony_ci	 * The EC host command for setting the alarm is relative
1618c2ecf20Sopenharmony_ci	 * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
1628c2ecf20Sopenharmony_ci	 * Get the current RTC time first so we can calculate the
1638c2ecf20Sopenharmony_ci	 * relative time.
1648c2ecf20Sopenharmony_ci	 */
1658c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
1668c2ecf20Sopenharmony_ci	if (ret < 0) {
1678c2ecf20Sopenharmony_ci		dev_err(dev, "error getting time: %d\n", ret);
1688c2ecf20Sopenharmony_ci		return ret;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	alarm_time = rtc_tm_to_time64(&alrm->time);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (alarm_time < 0 || alarm_time > U32_MAX)
1748c2ecf20Sopenharmony_ci		return -EINVAL;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (!alrm->enabled) {
1778c2ecf20Sopenharmony_ci		/*
1788c2ecf20Sopenharmony_ci		 * If the alarm is being disabled, send an alarm
1798c2ecf20Sopenharmony_ci		 * clear command.
1808c2ecf20Sopenharmony_ci		 */
1818c2ecf20Sopenharmony_ci		alarm_offset = EC_RTC_ALARM_CLEAR;
1828c2ecf20Sopenharmony_ci		cros_ec_rtc->saved_alarm = (u32)alarm_time;
1838c2ecf20Sopenharmony_ci	} else {
1848c2ecf20Sopenharmony_ci		/* Don't set an alarm in the past. */
1858c2ecf20Sopenharmony_ci		if ((u32)alarm_time <= current_time)
1868c2ecf20Sopenharmony_ci			return -ETIME;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		alarm_offset = (u32)alarm_time - current_time;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
1928c2ecf20Sopenharmony_ci	if (ret < 0) {
1938c2ecf20Sopenharmony_ci		dev_err(dev, "error setting alarm: %d\n", ret);
1948c2ecf20Sopenharmony_ci		return ret;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return 0;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int cros_ec_rtc_alarm_irq_enable(struct device *dev,
2018c2ecf20Sopenharmony_ci					unsigned int enabled)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
2048c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
2058c2ecf20Sopenharmony_ci	int ret;
2068c2ecf20Sopenharmony_ci	u32 current_time, alarm_offset, alarm_value;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &current_time);
2098c2ecf20Sopenharmony_ci	if (ret < 0) {
2108c2ecf20Sopenharmony_ci		dev_err(dev, "error getting time: %d\n", ret);
2118c2ecf20Sopenharmony_ci		return ret;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	if (enabled) {
2158c2ecf20Sopenharmony_ci		/* Restore saved alarm if it's still in the future. */
2168c2ecf20Sopenharmony_ci		if (cros_ec_rtc->saved_alarm < current_time)
2178c2ecf20Sopenharmony_ci			alarm_offset = EC_RTC_ALARM_CLEAR;
2188c2ecf20Sopenharmony_ci		else
2198c2ecf20Sopenharmony_ci			alarm_offset = cros_ec_rtc->saved_alarm - current_time;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
2228c2ecf20Sopenharmony_ci				      alarm_offset);
2238c2ecf20Sopenharmony_ci		if (ret < 0) {
2248c2ecf20Sopenharmony_ci			dev_err(dev, "error restoring alarm: %d\n", ret);
2258c2ecf20Sopenharmony_ci			return ret;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci	} else {
2288c2ecf20Sopenharmony_ci		/* Disable alarm, saving the old alarm value. */
2298c2ecf20Sopenharmony_ci		ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
2308c2ecf20Sopenharmony_ci				      &alarm_offset);
2318c2ecf20Sopenharmony_ci		if (ret < 0) {
2328c2ecf20Sopenharmony_ci			dev_err(dev, "error saving alarm: %d\n", ret);
2338c2ecf20Sopenharmony_ci			return ret;
2348c2ecf20Sopenharmony_ci		}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		alarm_value = current_time + alarm_offset;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		/*
2398c2ecf20Sopenharmony_ci		 * If the current EC alarm is already past, we don't want
2408c2ecf20Sopenharmony_ci		 * to set an alarm when we go through the alarm irq enable
2418c2ecf20Sopenharmony_ci		 * path.
2428c2ecf20Sopenharmony_ci		 */
2438c2ecf20Sopenharmony_ci		if (alarm_value < current_time)
2448c2ecf20Sopenharmony_ci			cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
2458c2ecf20Sopenharmony_ci		else
2468c2ecf20Sopenharmony_ci			cros_ec_rtc->saved_alarm = alarm_value;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci		alarm_offset = EC_RTC_ALARM_CLEAR;
2498c2ecf20Sopenharmony_ci		ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
2508c2ecf20Sopenharmony_ci				      alarm_offset);
2518c2ecf20Sopenharmony_ci		if (ret < 0) {
2528c2ecf20Sopenharmony_ci			dev_err(dev, "error disabling alarm: %d\n", ret);
2538c2ecf20Sopenharmony_ci			return ret;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int cros_ec_rtc_event(struct notifier_block *nb,
2618c2ecf20Sopenharmony_ci			     unsigned long queued_during_suspend,
2628c2ecf20Sopenharmony_ci			     void *_notify)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc;
2658c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
2668c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec;
2678c2ecf20Sopenharmony_ci	u32 host_event;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
2708c2ecf20Sopenharmony_ci	rtc = cros_ec_rtc->rtc;
2718c2ecf20Sopenharmony_ci	cros_ec = cros_ec_rtc->cros_ec;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	host_event = cros_ec_get_host_event(cros_ec);
2748c2ecf20Sopenharmony_ci	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
2758c2ecf20Sopenharmony_ci		rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
2768c2ecf20Sopenharmony_ci		return NOTIFY_OK;
2778c2ecf20Sopenharmony_ci	} else {
2788c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic const struct rtc_class_ops cros_ec_rtc_ops = {
2838c2ecf20Sopenharmony_ci	.read_time = cros_ec_rtc_read_time,
2848c2ecf20Sopenharmony_ci	.set_time = cros_ec_rtc_set_time,
2858c2ecf20Sopenharmony_ci	.read_alarm = cros_ec_rtc_read_alarm,
2868c2ecf20Sopenharmony_ci	.set_alarm = cros_ec_rtc_set_alarm,
2878c2ecf20Sopenharmony_ci	.alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
2888c2ecf20Sopenharmony_ci};
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2918c2ecf20Sopenharmony_cistatic int cros_ec_rtc_suspend(struct device *dev)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
2948c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev))
2978c2ecf20Sopenharmony_ci		return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	return 0;
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic int cros_ec_rtc_resume(struct device *dev)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
3058c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev))
3088c2ecf20Sopenharmony_ci		return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return 0;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci#endif
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
3158c2ecf20Sopenharmony_ci			 cros_ec_rtc_resume);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int cros_ec_rtc_probe(struct platform_device *pdev)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
3208c2ecf20Sopenharmony_ci	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
3218c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc;
3228c2ecf20Sopenharmony_ci	struct rtc_time tm;
3238c2ecf20Sopenharmony_ci	int ret;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
3268c2ecf20Sopenharmony_ci				   GFP_KERNEL);
3278c2ecf20Sopenharmony_ci	if (!cros_ec_rtc)
3288c2ecf20Sopenharmony_ci		return -ENOMEM;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, cros_ec_rtc);
3318c2ecf20Sopenharmony_ci	cros_ec_rtc->cros_ec = cros_ec;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Get initial time */
3348c2ecf20Sopenharmony_ci	ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
3358c2ecf20Sopenharmony_ci	if (ret) {
3368c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to read RTC time\n");
3378c2ecf20Sopenharmony_ci		return ret;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	ret = device_init_wakeup(&pdev->dev, 1);
3418c2ecf20Sopenharmony_ci	if (ret) {
3428c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to initialize wakeup\n");
3438c2ecf20Sopenharmony_ci		return ret;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
3478c2ecf20Sopenharmony_ci	if (IS_ERR(cros_ec_rtc->rtc))
3488c2ecf20Sopenharmony_ci		return PTR_ERR(cros_ec_rtc->rtc);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
3518c2ecf20Sopenharmony_ci	cros_ec_rtc->rtc->range_max = U32_MAX;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	ret = rtc_register_device(cros_ec_rtc->rtc);
3548c2ecf20Sopenharmony_ci	if (ret)
3558c2ecf20Sopenharmony_ci		return ret;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* Get RTC events from the EC. */
3588c2ecf20Sopenharmony_ci	cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
3598c2ecf20Sopenharmony_ci	ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
3608c2ecf20Sopenharmony_ci					       &cros_ec_rtc->notifier);
3618c2ecf20Sopenharmony_ci	if (ret) {
3628c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to register notifier\n");
3638c2ecf20Sopenharmony_ci		return ret;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int cros_ec_rtc_remove(struct platform_device *pdev)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
3728c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3738c2ecf20Sopenharmony_ci	int ret;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	ret = blocking_notifier_chain_unregister(
3768c2ecf20Sopenharmony_ci				&cros_ec_rtc->cros_ec->event_notifier,
3778c2ecf20Sopenharmony_ci				&cros_ec_rtc->notifier);
3788c2ecf20Sopenharmony_ci	if (ret) {
3798c2ecf20Sopenharmony_ci		dev_err(dev, "failed to unregister notifier\n");
3808c2ecf20Sopenharmony_ci		return ret;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic struct platform_driver cros_ec_rtc_driver = {
3878c2ecf20Sopenharmony_ci	.probe = cros_ec_rtc_probe,
3888c2ecf20Sopenharmony_ci	.remove = cros_ec_rtc_remove,
3898c2ecf20Sopenharmony_ci	.driver = {
3908c2ecf20Sopenharmony_ci		.name = DRV_NAME,
3918c2ecf20Sopenharmony_ci		.pm = &cros_ec_rtc_pm_ops,
3928c2ecf20Sopenharmony_ci	},
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cimodule_platform_driver(cros_ec_rtc_driver);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
3988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
3998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4008c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME);
401