History

platform/chrome: wilco_ec: Add RTC driver

This Embedded Controller has an internal RTC that is exposed as a standard RTC class driver with read/write functionality. The driver is added to the drivers/rtc/ so that the maintainer of that directory will be able to comment on this change, as that maintainer is the expert on this system. In addition, the driver code is called indirectly after a corresponding device is registered from core.c, as opposed to core.c registering the driver callbacks directly. To test: > hwclock --show --rtc /dev/rtc1 2007-12-31 16:01:20.460959-08:00 > hwclock --systohc --rtc /dev/rtc1 > hwclock --show --rtc /dev/rtc1 2018-11-29 17:08:00.780793-08:00 > hwclock --show --rtc /dev/rtc1 2007-12-31 16:01:20.460959-08:00 > hwclock --systohc --rtc /dev/rtc1 > hwclock --show --rtc /dev/rtc1 2018-11-29 17:08:00.780793-08:00

Nick Crews committed 7 years ago

Commit 0d2f2a31 parent b787bb15 files · +209 −0Browse files

Unified · Split

5 files
  1. drivers/platform/chrome/wilco_ec/core.c +18 −0
  2. drivers/rtc/Kconfig +11 −0
  3. drivers/rtc/Makefile +1 −0
  4. drivers/rtc/rtc-wilco-ec.c +177 −0
  5. include/linux/platform_data/wilco-ec.h +2 −0
drivers/platform/chrome/wilco_ec/core.c+18−0
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev) · 41 unmodified lines
4242 {
4343 struct device *dev = &pdev->dev;
4444 struct wilco_ec_device *ec;
45+ int ret;
4546
4647 ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
4748 if (!ec)
@@ -78,13 +79,30 @@ static int wilco_ec_probe(struct platform_device *pdev) · 30 unmodified lines
7879 PLATFORM_DEVID_AUTO,
7980 NULL, 0);
8081
82+ /* Register a child device that will be found by the RTC driver. */
83+ ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
84+ PLATFORM_DEVID_AUTO,
85+ NULL, 0);
86+ if (IS_ERR(ec->rtc_pdev)) {
87+ dev_err(dev, "Failed to create RTC platform device\n");
88+ ret = PTR_ERR(ec->rtc_pdev);
89+ goto unregister_debugfs;
90+ }
91+
8192 return 0;
93+
94+unregister_debugfs:
95+ if (ec->debugfs_pdev)
96+ platform_device_unregister(ec->debugfs_pdev);
97+ cros_ec_lpc_mec_destroy();
98+ return ret;
8299 }
83100
84101 static int wilco_ec_remove(struct platform_device *pdev)
85102 {
86103 struct wilco_ec_device *ec = platform_get_drvdata(pdev);
87104
105+ platform_device_unregister(ec->rtc_pdev);
88106 if (ec->debugfs_pdev)
89107 platform_device_unregister(ec->debugfs_pdev);
90108
drivers/rtc/Kconfig+11−0
@@ -1814,4 +1814,15 @@ config RTC_DRV_GOLDFISH · 1813 unmodified lines
18141814 Goldfish is a code name for the virtual platform developed by Google
18151815 for Android emulation.
18161816
1817+config RTC_DRV_WILCO_EC
1818+ tristate "Wilco EC RTC"
1819+ depends on WILCO_EC
1820+ default m
1821+ help
1822+ If you say yes here, you get read/write support for the Real Time
1823+ Clock on the Wilco Embedded Controller (Wilco is a kind of Chromebook)
1824+
1825+ This can also be built as a module. If so, the module will
1826+ be named "rtc_wilco_ec".
1827+
18171828 endif # RTC_CLASS
drivers/rtc/Makefile+1−0
@@ -172,6 +172,7 @@ obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o · 171 unmodified lines
172172 obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o
173173 obj-$(CONFIG_RTC_DRV_VRTC) += rtc-mrst.o
174174 obj-$(CONFIG_RTC_DRV_VT8500) += rtc-vt8500.o
175+obj-$(CONFIG_RTC_DRV_WILCO_EC) += rtc-wilco-ec.o
175176 obj-$(CONFIG_RTC_DRV_WM831X) += rtc-wm831x.o
176177 obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o
177178 obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o
drivers/rtc/rtc-wilco-ec.c+177−0
@@ -0,0 +1,177 @@
1+// 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+
24+/**
25+ * struct ec_rtc_read - Format of RTC returned by EC.
26+ * @second: Second value (0..59)
27+ * @minute: Minute value (0..59)
28+ * @hour: Hour value (0..23)
29+ * @day: Day value (1..31)
30+ * @month: Month value (1..12)
31+ * @year: Year value (full year % 100)
32+ * @century: Century value (full year / 100)
33+ *
34+ * All values are presented in binary (not BCD).
35+ */
36+struct ec_rtc_read {
37+ u8 second;
38+ u8 minute;
39+ u8 hour;
40+ u8 day;
41+ u8 month;
42+ u8 year;
43+ u8 century;
44+} __packed;
45+
46+/**
47+ * struct ec_rtc_write - Format of RTC sent to the EC.
48+ * @param: EC_CMOS_TOD_WRITE
49+ * @century: Century value (full year / 100)
50+ * @year: Year value (full year % 100)
51+ * @month: Month value (1..12)
52+ * @day: Day value (1..31)
53+ * @hour: Hour value (0..23)
54+ * @minute: Minute value (0..59)
55+ * @second: Second value (0..59)
56+ * @weekday: Day of the week (0=Saturday)
57+ *
58+ * All values are presented in BCD.
59+ */
60+struct ec_rtc_write {
61+ u8 param;
62+ u8 century;
63+ u8 year;
64+ u8 month;
65+ u8 day;
66+ u8 hour;
67+ u8 minute;
68+ u8 second;
69+ u8 weekday;
70+} __packed;
71+
72+static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
73+{
74+ struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
75+ u8 param = EC_CMOS_TOD_READ;
76+ struct ec_rtc_read rtc;
77+ struct wilco_ec_message msg = {
78+ .type = WILCO_EC_MSG_LEGACY,
79+ .flags = WILCO_EC_FLAG_RAW_RESPONSE,
80+ .command = EC_COMMAND_CMOS,
81+ .request_data = &param,
82+ .request_size = sizeof(param),
83+ .response_data = &rtc,
84+ .response_size = sizeof(rtc),
85+ };
86+ int ret;
87+
88+ ret = wilco_ec_mailbox(ec, &msg);
89+ if (ret < 0)
90+ return ret;
91+
92+ tm->tm_sec = rtc.second;
93+ tm->tm_min = rtc.minute;
94+ tm->tm_hour = rtc.hour;
95+ tm->tm_mday = rtc.day;
96+ tm->tm_mon = rtc.month - 1;
97+ tm->tm_year = rtc.year + (rtc.century * 100) - 1900;
98+ tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
99+
100+ /* Don't compute day of week, we don't need it. */
101+ tm->tm_wday = -1;
102+
103+ return 0;
104+}
105+
106+static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
107+{
108+ struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
109+ struct ec_rtc_write rtc;
110+ struct wilco_ec_message msg = {
111+ .type = WILCO_EC_MSG_LEGACY,
112+ .flags = WILCO_EC_FLAG_RAW_RESPONSE,
113+ .command = EC_COMMAND_CMOS,
114+ .request_data = &rtc,
115+ .request_size = sizeof(rtc),
116+ };
117+ int year = tm->tm_year + 1900;
118+ /*
119+ * Convert from 0=Sunday to 0=Saturday for the EC
120+ * We DO need to set weekday because the EC controls battery charging
121+ * schedules that depend on the day of the week.
122+ */
123+ int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1;
124+ int ret;
125+
126+ rtc.param = EC_CMOS_TOD_WRITE;
127+ rtc.century = bin2bcd(year / 100);
128+ rtc.year = bin2bcd(year % 100);
129+ rtc.month = bin2bcd(tm->tm_mon + 1);
130+ rtc.day = bin2bcd(tm->tm_mday);
131+ rtc.hour = bin2bcd(tm->tm_hour);
132+ rtc.minute = bin2bcd(tm->tm_min);
133+ rtc.second = bin2bcd(tm->tm_sec);
134+ rtc.weekday = bin2bcd(wday);
135+
136+ ret = wilco_ec_mailbox(ec, &msg);
137+ if (ret < 0)
138+ return ret;
139+
140+ return 0;
141+}
142+
143+static const struct rtc_class_ops wilco_ec_rtc_ops = {
144+ .read_time = wilco_ec_rtc_read,
145+ .set_time = wilco_ec_rtc_write,
146+};
147+
148+static int wilco_ec_rtc_probe(struct platform_device *pdev)
149+{
150+ struct rtc_device *rtc;
151+
152+ rtc = devm_rtc_allocate_device(&pdev->dev);
153+ if (IS_ERR(rtc))
154+ return PTR_ERR(rtc);
155+
156+ rtc->ops = &wilco_ec_rtc_ops;
157+ /* EC only supports this century */
158+ rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
159+ rtc->range_max = RTC_TIMESTAMP_END_2099;
160+ rtc->owner = THIS_MODULE;
161+
162+ return rtc_register_device(rtc);
163+}
164+
165+static struct platform_driver wilco_ec_rtc_driver = {
166+ .driver = {
167+ .name = "rtc-wilco-ec",
168+ },
169+ .probe = wilco_ec_rtc_probe,
170+};
171+
172+module_platform_driver(wilco_ec_rtc_driver);
173+
174+MODULE_ALIAS("platform:rtc-wilco-ec");
175+MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
176+MODULE_LICENSE("GPL v2");
177+MODULE_DESCRIPTION("Wilco EC RTC driver");
include/linux/platform_data/wilco-ec.h+2−0
@@ -35,6 +35,7 @@ · 34 unmodified lines
3535 * is used to hold the request and the response.
3636 * @data_size: Size of the data buffer used for EC communication.
3737 * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
38+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
3839 */
3940 struct wilco_ec_device {
4041 struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device { · 4 unmodified lines
4546 void *data_buffer;
4647 size_t data_size;
4748 struct platform_device *debugfs_pdev;
49+ struct platform_device *rtc_pdev;
4850 };
4951
5052 /**

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t