rtc-wilco-ec.c (9b0dcd0e5a27958b57e3e390f63c098d63a055da) rtc-wilco-ec.c (14e14aaf61321ba30d0bbdf4c4668f260ca1141c)
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

--- 7 unchanged lines hidden (view full) ---

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
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

--- 7 unchanged lines hidden (view full) ---

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/* 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
24/**
35/**
25 * struct ec_rtc_read - Format of RTC returned by EC.
36 * struct ec_rtc_read_response - Format of RTC returned by EC.
37 * @reserved: Unused byte
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 */
38 * @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 */
36struct ec_rtc_read {
48struct ec_rtc_read_response {
49 u8 reserved;
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/**
50 u8 second;
51 u8 minute;
52 u8 hour;
53 u8 day;
54 u8 month;
55 u8 year;
56 u8 century;
57} __packed;
58
59/**
47 * struct ec_rtc_write - Format of RTC sent to the EC.
48 * @param: EC_CMOS_TOD_WRITE
60 * 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
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 */
64 * @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 */
60struct ec_rtc_write {
75struct ec_rtc_write_request {
76 u8 command;
77 u8 reserved;
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
72static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
73{
74 struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
78 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);
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 };
92 struct ec_rtc_read_response rtc;
93 struct wilco_ec_message msg;
86 int ret;
87
94 int ret;
95
96 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
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;

--- 5 unchanged lines hidden (view full) ---

101 tm->tm_wday = -1;
102
103 return 0;
104}
105
106static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
107{
108 struct wilco_ec_device *ec = dev_get_drvdata(dev->parent);
103 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;

--- 5 unchanged lines hidden (view full) ---

116 tm->tm_wday = -1;
117
118 return 0;
119}
120
121static int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm)
122{
123 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 };
124 struct ec_rtc_write_request rtc;
125 struct wilco_ec_message msg;
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 int year = tm->tm_year + 1900;
127 /*
128 * Convert from 0=Sunday to 0=Saturday for the EC
129 * We DO need to set weekday because the EC controls battery charging
130 * schedules that depend on the day of the week.
131 */
132 int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1;
133 int ret;
134
135 rtc.command = EC_COMMAND_CMOS;
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 rtc.param = EC_CMOS_TOD_WRITE;
137 rtc.century = bin2bcd(year / 100);
138 rtc.year = bin2bcd(year % 100);
139 rtc.month = bin2bcd(tm->tm_mon + 1);
140 rtc.day = bin2bcd(tm->tm_mday);
141 rtc.hour = bin2bcd(tm->tm_hour);
142 rtc.minute = bin2bcd(tm->tm_min);
143 rtc.second = bin2bcd(tm->tm_sec);
144 rtc.weekday = bin2bcd(wday);
145
146 memset(&msg, 0, sizeof(msg));
147 msg.type = WILCO_EC_MSG_LEGACY;
148 msg.request_data = &rtc;
149 msg.request_size = sizeof(rtc);
150
136 ret = wilco_ec_mailbox(ec, &msg);
137 if (ret < 0)
138 return ret;
139
140 return 0;
141}
142
143static const struct rtc_class_ops wilco_ec_rtc_ops = {

--- 34 unchanged lines hidden ---
151 ret = wilco_ec_mailbox(ec, &msg);
152 if (ret < 0)
153 return ret;
154
155 return 0;
156}
157
158static const struct rtc_class_ops wilco_ec_rtc_ops = {

--- 34 unchanged lines hidden ---