blob: 2a205a64645213574ed82413221e120bb721d040 [file] [log] [blame]
Nick Crews0d2f2a32019-02-08 17:37:19 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * RTC interface for Wilco Embedded Controller with R/W abilities
4 *
5 * Copyright 2018 Google LLC
6 *
7 * The corresponding platform device is typically registered in
8 * drivers/platform/chrome/wilco_ec/core.c
9 */
10
11#include <linux/bcd.h>
12#include <linux/err.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/platform_data/wilco-ec.h>
17#include <linux/rtc.h>
18#include <linux/timekeeping.h>
19
20#define EC_COMMAND_CMOS 0x7c
21#define EC_CMOS_TOD_WRITE 0x02
22#define EC_CMOS_TOD_READ 0x08
23
Nick Crews14e14aa2019-04-04 16:54:15 -060024/* Message sent to the EC to request the current time. */
25struct ec_rtc_read_request {
26 u8 command;
27 u8 reserved;
28 u8 param;
29} __packed;
30static struct ec_rtc_read_request read_rq = {
31 .command = EC_COMMAND_CMOS,
32 .param = EC_CMOS_TOD_READ,
33};
34
Nick Crews0d2f2a32019-02-08 17:37:19 -070035/**
Nick Crews14e14aa2019-04-04 16:54:15 -060036 * struct ec_rtc_read_response - Format of RTC returned by EC.
37 * @reserved: Unused byte
Nick Crews0d2f2a32019-02-08 17:37:19 -070038 * @second: Second value (0..59)
39 * @minute: Minute value (0..59)
40 * @hour: Hour value (0..23)
41 * @day: Day value (1..31)
42 * @month: Month value (1..12)
43 * @year: Year value (full year % 100)
44 * @century: Century value (full year / 100)
45 *
46 * All values are presented in binary (not BCD).
47 */
Nick Crews14e14aa2019-04-04 16:54:15 -060048struct ec_rtc_read_response {
49 u8 reserved;
Nick Crews0d2f2a32019-02-08 17:37:19 -070050 u8 second;
51 u8 minute;
52 u8 hour;
53 u8 day;
54 u8 month;
55 u8 year;
56 u8 century;
57} __packed;
58
59/**
Nick Crews14e14aa2019-04-04 16:54:15 -060060 * struct ec_rtc_write_request - Format of RTC sent to the EC.
61 * @command: Always EC_COMMAND_CMOS
62 * @reserved: Unused byte
63 * @param: Always EC_CMOS_TOD_WRITE
Nick Crews0d2f2a32019-02-08 17:37:19 -070064 * @century: Century value (full year / 100)
65 * @year: Year value (full year % 100)
66 * @month: Month value (1..12)
67 * @day: Day value (1..31)
68 * @hour: Hour value (0..23)
69 * @minute: Minute value (0..59)
70 * @second: Second value (0..59)
71 * @weekday: Day of the week (0=Saturday)
72 *
73 * All values are presented in BCD.
74 */
Nick Crews14e14aa2019-04-04 16:54:15 -060075struct ec_rtc_write_request {
76 u8 command;
77 u8 reserved;
Nick Crews0d2f2a32019-02-08 17:37:19 -070078 u8 param;
79 u8 century;
80 u8 year;
81 u8 month;
82 u8 day;
83 u8 hour;
84 u8 minute;
85 u8 second;
86 u8 weekday;
87} __packed;
88
89static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
90{
91 struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
Nick Crews14e14aa2019-04-04 16:54:15 -060092 struct ec_rtc_read_response rtc;
93 struct wilco_ec_message msg;
Nick Crews0d2f2a32019-02-08 17:37:19 -070094 int ret;
95
Nick Crews14e14aa2019-04-04 16:54:15 -060096 memset(&msg, 0, sizeof(msg));
97 msg.type = WILCO_EC_MSG_LEGACY;
98 msg.request_data = &read_rq;
99 msg.request_size = sizeof(read_rq);
100 msg.response_data = &rtc;
101 msg.response_size = sizeof(rtc);
102
Nick Crews0d2f2a32019-02-08 17:37:19 -0700103 ret = wilco_ec_mailbox(ec, &msg);
104 if (ret < 0)
105 return ret;
106
107 tm->tm_sec = rtc.second;
108 tm->tm_min = rtc.minute;
109 tm->tm_hour = rtc.hour;
110 tm->tm_mday = rtc.day;
111 tm->tm_mon = rtc.month - 1;
112 tm->tm_year = rtc.year + (rtc.century * 100) - 1900;
Nick Crews9aa0d0b2019-10-04 08:26:08 -0600113 /* Ignore other tm fields, man rtc says userspace shouldn't use them. */
Nick Crews0d2f2a32019-02-08 17:37:19 -0700114
Nick Crews9aa0d0b2019-10-04 08:26:08 -0600115 if (rtc_valid_tm(tm)) {
116 dev_err(dev, "Time from RTC is invalid: %ptRr\n", tm);
117 return -EIO;
118 }
Nick Crews0d2f2a32019-02-08 17:37:19 -0700119
120 return 0;
121}
122
123static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
124{
125 struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
Nick Crews14e14aa2019-04-04 16:54:15 -0600126 struct ec_rtc_write_request rtc;
127 struct wilco_ec_message msg;
Nick Crews0d2f2a32019-02-08 17:37:19 -0700128 int year = tm->tm_year + 1900;
129 /*
130 * Convert from 0=Sunday to 0=Saturday for the EC
131 * We DO need to set weekday because the EC controls battery charging
132 * schedules that depend on the day of the week.
133 */
134 int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1;
135 int ret;
136
Nick Crews14e14aa2019-04-04 16:54:15 -0600137 rtc.command = EC_COMMAND_CMOS;
Nick Crews0d2f2a32019-02-08 17:37:19 -0700138 rtc.param = EC_CMOS_TOD_WRITE;
139 rtc.century = bin2bcd(year / 100);
140 rtc.year = bin2bcd(year % 100);
141 rtc.month = bin2bcd(tm->tm_mon + 1);
142 rtc.day = bin2bcd(tm->tm_mday);
143 rtc.hour = bin2bcd(tm->tm_hour);
144 rtc.minute = bin2bcd(tm->tm_min);
145 rtc.second = bin2bcd(tm->tm_sec);
146 rtc.weekday = bin2bcd(wday);
147
Nick Crews14e14aa2019-04-04 16:54:15 -0600148 memset(&msg, 0, sizeof(msg));
149 msg.type = WILCO_EC_MSG_LEGACY;
150 msg.request_data = &rtc;
151 msg.request_size = sizeof(rtc);
152
Nick Crews0d2f2a32019-02-08 17:37:19 -0700153 ret = wilco_ec_mailbox(ec, &msg);
154 if (ret < 0)
155 return ret;
156
157 return 0;
158}
159
160static const struct rtc_class_ops wilco_ec_rtc_ops = {
161 .read_time = wilco_ec_rtc_read,
162 .set_time = wilco_ec_rtc_write,
163};
164
165static int wilco_ec_rtc_probe(struct platform_device *pdev)
166{
167 struct rtc_device *rtc;
168
169 rtc = devm_rtc_allocate_device(&pdev->dev);
170 if (IS_ERR(rtc))
171 return PTR_ERR(rtc);
172
173 rtc->ops = &wilco_ec_rtc_ops;
174 /* EC only supports this century */
175 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
176 rtc->range_max = RTC_TIMESTAMP_END_2099;
177 rtc->owner = THIS_MODULE;
178
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100179 return devm_rtc_register_device(rtc);
Nick Crews0d2f2a32019-02-08 17:37:19 -0700180}
181
182static struct platform_driver wilco_ec_rtc_driver = {
183 .driver = {
184 .name = "rtc-wilco-ec",
185 },
186 .probe = wilco_ec_rtc_probe,
187};
188
189module_platform_driver(wilco_ec_rtc_driver);
190
191MODULE_ALIAS("platform:rtc-wilco-ec");
192MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
193MODULE_LICENSE("GPL v2");
194MODULE_DESCRIPTION("Wilco EC RTC driver");