xref: /linux/drivers/rtc/rtc-wilco-ec.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
10d2f2a3dSNick Crews // SPDX-License-Identifier: GPL-2.0
20d2f2a3dSNick Crews /*
30d2f2a3dSNick Crews  * RTC interface for Wilco Embedded Controller with R/W abilities
40d2f2a3dSNick Crews  *
50d2f2a3dSNick Crews  * Copyright 2018 Google LLC
60d2f2a3dSNick Crews  *
70d2f2a3dSNick Crews  * The corresponding platform device is typically registered in
80d2f2a3dSNick Crews  * drivers/platform/chrome/wilco_ec/core.c
90d2f2a3dSNick Crews  */
100d2f2a3dSNick Crews 
110d2f2a3dSNick Crews #include <linux/bcd.h>
120d2f2a3dSNick Crews #include <linux/err.h>
130d2f2a3dSNick Crews #include <linux/kernel.h>
140d2f2a3dSNick Crews #include <linux/module.h>
150d2f2a3dSNick Crews #include <linux/platform_device.h>
160d2f2a3dSNick Crews #include <linux/platform_data/wilco-ec.h>
170d2f2a3dSNick Crews #include <linux/rtc.h>
180d2f2a3dSNick Crews #include <linux/timekeeping.h>
190d2f2a3dSNick Crews 
200d2f2a3dSNick Crews #define EC_COMMAND_CMOS			0x7c
210d2f2a3dSNick Crews #define EC_CMOS_TOD_WRITE		0x02
220d2f2a3dSNick Crews #define EC_CMOS_TOD_READ		0x08
230d2f2a3dSNick Crews 
2414e14aafSNick Crews /* Message sent to the EC to request the current time. */
2514e14aafSNick Crews struct ec_rtc_read_request {
2614e14aafSNick Crews 	u8 command;
2714e14aafSNick Crews 	u8 reserved;
2814e14aafSNick Crews 	u8 param;
2914e14aafSNick Crews } __packed;
3014e14aafSNick Crews static struct ec_rtc_read_request read_rq = {
3114e14aafSNick Crews 	.command = EC_COMMAND_CMOS,
3214e14aafSNick Crews 	.param = EC_CMOS_TOD_READ,
3314e14aafSNick Crews };
3414e14aafSNick Crews 
350d2f2a3dSNick Crews /**
3614e14aafSNick Crews  * struct ec_rtc_read_response - Format of RTC returned by EC.
3714e14aafSNick Crews  * @reserved: Unused byte
380d2f2a3dSNick Crews  * @second: Second value (0..59)
390d2f2a3dSNick Crews  * @minute: Minute value (0..59)
400d2f2a3dSNick Crews  * @hour: Hour value (0..23)
410d2f2a3dSNick Crews  * @day: Day value (1..31)
420d2f2a3dSNick Crews  * @month: Month value (1..12)
430d2f2a3dSNick Crews  * @year: Year value (full year % 100)
440d2f2a3dSNick Crews  * @century: Century value (full year / 100)
450d2f2a3dSNick Crews  *
460d2f2a3dSNick Crews  * All values are presented in binary (not BCD).
470d2f2a3dSNick Crews  */
4814e14aafSNick Crews struct ec_rtc_read_response {
4914e14aafSNick Crews 	u8 reserved;
500d2f2a3dSNick Crews 	u8 second;
510d2f2a3dSNick Crews 	u8 minute;
520d2f2a3dSNick Crews 	u8 hour;
530d2f2a3dSNick Crews 	u8 day;
540d2f2a3dSNick Crews 	u8 month;
550d2f2a3dSNick Crews 	u8 year;
560d2f2a3dSNick Crews 	u8 century;
570d2f2a3dSNick Crews } __packed;
580d2f2a3dSNick Crews 
590d2f2a3dSNick Crews /**
6014e14aafSNick Crews  * struct ec_rtc_write_request - Format of RTC sent to the EC.
6114e14aafSNick Crews  * @command: Always EC_COMMAND_CMOS
6214e14aafSNick Crews  * @reserved: Unused byte
6314e14aafSNick Crews  * @param: Always EC_CMOS_TOD_WRITE
640d2f2a3dSNick Crews  * @century: Century value (full year / 100)
650d2f2a3dSNick Crews  * @year: Year value (full year % 100)
660d2f2a3dSNick Crews  * @month: Month value (1..12)
670d2f2a3dSNick Crews  * @day: Day value (1..31)
680d2f2a3dSNick Crews  * @hour: Hour value (0..23)
690d2f2a3dSNick Crews  * @minute: Minute value (0..59)
700d2f2a3dSNick Crews  * @second: Second value (0..59)
710d2f2a3dSNick Crews  * @weekday: Day of the week (0=Saturday)
720d2f2a3dSNick Crews  *
730d2f2a3dSNick Crews  * All values are presented in BCD.
740d2f2a3dSNick Crews  */
7514e14aafSNick Crews struct ec_rtc_write_request {
7614e14aafSNick Crews 	u8 command;
7714e14aafSNick Crews 	u8 reserved;
780d2f2a3dSNick Crews 	u8 param;
790d2f2a3dSNick Crews 	u8 century;
800d2f2a3dSNick Crews 	u8 year;
810d2f2a3dSNick Crews 	u8 month;
820d2f2a3dSNick Crews 	u8 day;
830d2f2a3dSNick Crews 	u8 hour;
840d2f2a3dSNick Crews 	u8 minute;
850d2f2a3dSNick Crews 	u8 second;
860d2f2a3dSNick Crews 	u8 weekday;
870d2f2a3dSNick Crews } __packed;
880d2f2a3dSNick Crews 
wilco_ec_rtc_read(struct device * dev,struct rtc_time * tm)890d2f2a3dSNick Crews static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
900d2f2a3dSNick Crews {
910d2f2a3dSNick Crews 	struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
9214e14aafSNick Crews 	struct ec_rtc_read_response rtc;
9314e14aafSNick Crews 	struct wilco_ec_message msg;
940d2f2a3dSNick Crews 	int ret;
950d2f2a3dSNick Crews 
9614e14aafSNick Crews 	memset(&msg, 0, sizeof(msg));
9714e14aafSNick Crews 	msg.type = WILCO_EC_MSG_LEGACY;
9814e14aafSNick Crews 	msg.request_data = &read_rq;
9914e14aafSNick Crews 	msg.request_size = sizeof(read_rq);
10014e14aafSNick Crews 	msg.response_data = &rtc;
10114e14aafSNick Crews 	msg.response_size = sizeof(rtc);
10214e14aafSNick Crews 
1030d2f2a3dSNick Crews 	ret = wilco_ec_mailbox(ec, &msg);
1040d2f2a3dSNick Crews 	if (ret < 0)
1050d2f2a3dSNick Crews 		return ret;
1060d2f2a3dSNick Crews 
1070d2f2a3dSNick Crews 	tm->tm_sec	= rtc.second;
1080d2f2a3dSNick Crews 	tm->tm_min	= rtc.minute;
1090d2f2a3dSNick Crews 	tm->tm_hour	= rtc.hour;
1100d2f2a3dSNick Crews 	tm->tm_mday	= rtc.day;
1110d2f2a3dSNick Crews 	tm->tm_mon	= rtc.month - 1;
1120d2f2a3dSNick Crews 	tm->tm_year	= rtc.year + (rtc.century * 100) - 1900;
1139aa0d0beSNick Crews 	/* Ignore other tm fields, man rtc says userspace shouldn't use them. */
1140d2f2a3dSNick Crews 
1159aa0d0beSNick Crews 	if (rtc_valid_tm(tm)) {
1169aa0d0beSNick Crews 		dev_err(dev, "Time from RTC is invalid: %ptRr\n", tm);
1179aa0d0beSNick Crews 		return -EIO;
1189aa0d0beSNick Crews 	}
1190d2f2a3dSNick Crews 
1200d2f2a3dSNick Crews 	return 0;
1210d2f2a3dSNick Crews }
1220d2f2a3dSNick Crews 
wilco_ec_rtc_write(struct device * dev,struct rtc_time * tm)1230d2f2a3dSNick Crews static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
1240d2f2a3dSNick Crews {
1250d2f2a3dSNick Crews 	struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
12614e14aafSNick Crews 	struct ec_rtc_write_request rtc;
12714e14aafSNick Crews 	struct wilco_ec_message msg;
1280d2f2a3dSNick Crews 	int year = tm->tm_year + 1900;
1290d2f2a3dSNick Crews 	/*
1300d2f2a3dSNick Crews 	 * Convert from 0=Sunday to 0=Saturday for the EC
1310d2f2a3dSNick Crews 	 * We DO need to set weekday because the EC controls battery charging
1320d2f2a3dSNick Crews 	 * schedules that depend on the day of the week.
1330d2f2a3dSNick Crews 	 */
1340d2f2a3dSNick Crews 	int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1;
1350d2f2a3dSNick Crews 	int ret;
1360d2f2a3dSNick Crews 
13714e14aafSNick Crews 	rtc.command	= EC_COMMAND_CMOS;
1380d2f2a3dSNick Crews 	rtc.param	= EC_CMOS_TOD_WRITE;
1390d2f2a3dSNick Crews 	rtc.century	= bin2bcd(year / 100);
1400d2f2a3dSNick Crews 	rtc.year	= bin2bcd(year % 100);
1410d2f2a3dSNick Crews 	rtc.month	= bin2bcd(tm->tm_mon + 1);
1420d2f2a3dSNick Crews 	rtc.day		= bin2bcd(tm->tm_mday);
1430d2f2a3dSNick Crews 	rtc.hour	= bin2bcd(tm->tm_hour);
1440d2f2a3dSNick Crews 	rtc.minute	= bin2bcd(tm->tm_min);
1450d2f2a3dSNick Crews 	rtc.second	= bin2bcd(tm->tm_sec);
1460d2f2a3dSNick Crews 	rtc.weekday	= bin2bcd(wday);
1470d2f2a3dSNick Crews 
14814e14aafSNick Crews 	memset(&msg, 0, sizeof(msg));
14914e14aafSNick Crews 	msg.type = WILCO_EC_MSG_LEGACY;
15014e14aafSNick Crews 	msg.request_data = &rtc;
15114e14aafSNick Crews 	msg.request_size = sizeof(rtc);
15214e14aafSNick Crews 
1530d2f2a3dSNick Crews 	ret = wilco_ec_mailbox(ec, &msg);
1540d2f2a3dSNick Crews 	if (ret < 0)
1550d2f2a3dSNick Crews 		return ret;
1560d2f2a3dSNick Crews 
1570d2f2a3dSNick Crews 	return 0;
1580d2f2a3dSNick Crews }
1590d2f2a3dSNick Crews 
1600d2f2a3dSNick Crews static const struct rtc_class_ops wilco_ec_rtc_ops = {
1610d2f2a3dSNick Crews 	.read_time = wilco_ec_rtc_read,
1620d2f2a3dSNick Crews 	.set_time = wilco_ec_rtc_write,
1630d2f2a3dSNick Crews };
1640d2f2a3dSNick Crews 
wilco_ec_rtc_probe(struct platform_device * pdev)1650d2f2a3dSNick Crews static int wilco_ec_rtc_probe(struct platform_device *pdev)
1660d2f2a3dSNick Crews {
1670d2f2a3dSNick Crews 	struct rtc_device *rtc;
1680d2f2a3dSNick Crews 
1690d2f2a3dSNick Crews 	rtc = devm_rtc_allocate_device(&pdev->dev);
1700d2f2a3dSNick Crews 	if (IS_ERR(rtc))
1710d2f2a3dSNick Crews 		return PTR_ERR(rtc);
1720d2f2a3dSNick Crews 
1730d2f2a3dSNick Crews 	rtc->ops = &wilco_ec_rtc_ops;
1740d2f2a3dSNick Crews 	/* EC only supports this century */
1750d2f2a3dSNick Crews 	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
1760d2f2a3dSNick Crews 	rtc->range_max = RTC_TIMESTAMP_END_2099;
1770d2f2a3dSNick Crews 	rtc->owner = THIS_MODULE;
1780d2f2a3dSNick Crews 
179*fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(rtc);
1800d2f2a3dSNick Crews }
1810d2f2a3dSNick Crews 
1820d2f2a3dSNick Crews static struct platform_driver wilco_ec_rtc_driver = {
1830d2f2a3dSNick Crews 	.driver = {
1840d2f2a3dSNick Crews 		.name = "rtc-wilco-ec",
1850d2f2a3dSNick Crews 	},
1860d2f2a3dSNick Crews 	.probe = wilco_ec_rtc_probe,
1870d2f2a3dSNick Crews };
1880d2f2a3dSNick Crews 
1890d2f2a3dSNick Crews module_platform_driver(wilco_ec_rtc_driver);
1900d2f2a3dSNick Crews 
1910d2f2a3dSNick Crews MODULE_ALIAS("platform:rtc-wilco-ec");
1920d2f2a3dSNick Crews MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
1930d2f2a3dSNick Crews MODULE_LICENSE("GPL v2");
1940d2f2a3dSNick Crews MODULE_DESCRIPTION("Wilco EC RTC driver");
195