1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/bcd.h>
17#include <linux/rtc.h>
18#include <linux/delay.h>
19
20
21
22
23#define MAX6900_REG_SC 0
24#define MAX6900_REG_MN 1
25#define MAX6900_REG_HR 2
26#define MAX6900_REG_DT 3
27#define MAX6900_REG_MO 4
28#define MAX6900_REG_DW 5
29#define MAX6900_REG_YR 6
30#define MAX6900_REG_CT 7
31
32#define MAX6900_REG_CENTURY 9
33#define MAX6900_REG_LEN 10
34
35#define MAX6900_BURST_LEN 8
36
37#define MAX6900_REG_CT_WP (1 << 7)
38
39
40
41
42#define MAX6900_REG_CONTROL_WRITE 0x8e
43#define MAX6900_REG_CENTURY_WRITE 0x92
44#define MAX6900_REG_CENTURY_READ 0x93
45#define MAX6900_REG_RESERVED_READ 0x96
46#define MAX6900_REG_BURST_WRITE 0xbe
47#define MAX6900_REG_BURST_READ 0xbf
48
49#define MAX6900_IDLE_TIME_AFTER_WRITE 3
50
51static struct i2c_driver max6900_driver;
52
53static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
54{
55 u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
56 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
57 struct i2c_msg msgs[4] = {
58 {
59 .addr = client->addr,
60 .flags = 0,
61 .len = sizeof(reg_burst_read),
62 .buf = reg_burst_read}
63 ,
64 {
65 .addr = client->addr,
66 .flags = I2C_M_RD,
67 .len = MAX6900_BURST_LEN,
68 .buf = buf}
69 ,
70 {
71 .addr = client->addr,
72 .flags = 0,
73 .len = sizeof(reg_century_read),
74 .buf = reg_century_read}
75 ,
76 {
77 .addr = client->addr,
78 .flags = I2C_M_RD,
79 .len = sizeof(buf[MAX6900_REG_CENTURY]),
80 .buf = &buf[MAX6900_REG_CENTURY]
81 }
82 };
83 int rc;
84
85 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
86 if (rc != ARRAY_SIZE(msgs)) {
87 dev_err(&client->dev, "%s: register read failed\n", __func__);
88 return -EIO;
89 }
90 return 0;
91}
92
93static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
94{
95 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
96 struct i2c_msg century_msgs[1] = {
97 {
98 .addr = client->addr,
99 .flags = 0,
100 .len = sizeof(i2c_century_buf),
101 .buf = i2c_century_buf}
102 };
103 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
104 struct i2c_msg burst_msgs[1] = {
105 {
106 .addr = client->addr,
107 .flags = 0,
108 .len = sizeof(i2c_burst_buf),
109 .buf = i2c_burst_buf}
110 };
111 int rc;
112
113
114
115
116
117
118
119 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
120
121 rc = i2c_transfer(client->adapter, century_msgs,
122 ARRAY_SIZE(century_msgs));
123 if (rc != ARRAY_SIZE(century_msgs))
124 goto write_failed;
125
126 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
127
128 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
129
130 rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
131 if (rc != ARRAY_SIZE(burst_msgs))
132 goto write_failed;
133 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
134
135 return 0;
136
137 write_failed:
138 dev_err(&client->dev, "%s: register write failed\n", __func__);
139 return -EIO;
140}
141
142static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
143{
144 struct i2c_client *client = to_i2c_client(dev);
145 int rc;
146 u8 regs[MAX6900_REG_LEN];
147
148 rc = max6900_i2c_read_regs(client, regs);
149 if (rc < 0)
150 return rc;
151
152 tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
153 tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
154 tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
155 tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
156 tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
157 tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
158 bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
159 tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
160
161 return 0;
162}
163
164static int max6900_i2c_clear_write_protect(struct i2c_client *client)
165{
166 return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
167}
168
169static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 u8 regs[MAX6900_REG_LEN];
173 int rc;
174
175 rc = max6900_i2c_clear_write_protect(client);
176 if (rc < 0)
177 return rc;
178
179 regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
180 regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
181 regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
182 regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
183 regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
184 regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
185 regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
186 regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
187
188 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
189
190 rc = max6900_i2c_write_regs(client, regs);
191 if (rc < 0)
192 return rc;
193
194 return 0;
195}
196
197static const struct rtc_class_ops max6900_rtc_ops = {
198 .read_time = max6900_rtc_read_time,
199 .set_time = max6900_rtc_set_time,
200};
201
202static int
203max6900_probe(struct i2c_client *client, const struct i2c_device_id *id)
204{
205 struct rtc_device *rtc;
206
207 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
208 return -ENODEV;
209
210 rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
211 &max6900_rtc_ops, THIS_MODULE);
212 if (IS_ERR(rtc))
213 return PTR_ERR(rtc);
214
215 i2c_set_clientdata(client, rtc);
216
217 return 0;
218}
219
220static const struct i2c_device_id max6900_id[] = {
221 { "max6900", 0 },
222 { }
223};
224MODULE_DEVICE_TABLE(i2c, max6900_id);
225
226static struct i2c_driver max6900_driver = {
227 .driver = {
228 .name = "rtc-max6900",
229 },
230 .probe = max6900_probe,
231 .id_table = max6900_id,
232};
233
234module_i2c_driver(max6900_driver);
235
236MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
237MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
238MODULE_LICENSE("GPL");
239