1 // SPDX-License-Identifier: GPL-2.0-only
3 * rtc class driver for the Maxim MAX6900 chip
5 * Copyright (c) 2007 MontaVista, Software, Inc.
7 * Author: Dale Farnsworth <dale@farnsworth.org>
9 * based on previously existing rtc class drivers
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/bcd.h>
15 #include <linux/rtc.h>
16 #include <linux/delay.h>
21 #define MAX6900_REG_SC 0 /* seconds 00-59 */
22 #define MAX6900_REG_MN 1 /* minutes 00-59 */
23 #define MAX6900_REG_HR 2 /* hours 00-23 */
24 #define MAX6900_REG_DT 3 /* day of month 00-31 */
25 #define MAX6900_REG_MO 4 /* month 01-12 */
26 #define MAX6900_REG_DW 5 /* day of week 1-7 */
27 #define MAX6900_REG_YR 6 /* year 00-99 */
28 #define MAX6900_REG_CT 7 /* control */
29 /* register 8 is undocumented */
30 #define MAX6900_REG_CENTURY 9 /* century */
31 #define MAX6900_REG_LEN 10
33 #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
35 #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
38 * register read/write commands
40 #define MAX6900_REG_CONTROL_WRITE 0x8e
41 #define MAX6900_REG_CENTURY_WRITE 0x92
42 #define MAX6900_REG_CENTURY_READ 0x93
43 #define MAX6900_REG_RESERVED_READ 0x96
44 #define MAX6900_REG_BURST_WRITE 0xbe
45 #define MAX6900_REG_BURST_READ 0xbf
47 #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
49 static struct i2c_driver max6900_driver;
51 static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
53 u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
54 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
55 struct i2c_msg msgs[4] = {
58 .flags = 0, /* write */
59 .len = sizeof(reg_burst_read),
60 .buf = reg_burst_read}
65 .len = MAX6900_BURST_LEN,
70 .flags = 0, /* write */
71 .len = sizeof(reg_century_read),
72 .buf = reg_century_read}
77 .len = sizeof(buf[MAX6900_REG_CENTURY]),
78 .buf = &buf[MAX6900_REG_CENTURY]
83 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
84 if (rc != ARRAY_SIZE(msgs)) {
85 dev_err(&client->dev, "%s: register read failed\n", __func__);
91 static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
93 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
94 struct i2c_msg century_msgs[1] = {
97 .flags = 0, /* write */
98 .len = sizeof(i2c_century_buf),
99 .buf = i2c_century_buf}
101 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
102 struct i2c_msg burst_msgs[1] = {
104 .addr = client->addr,
105 .flags = 0, /* write */
106 .len = sizeof(i2c_burst_buf),
107 .buf = i2c_burst_buf}
112 * We have to make separate calls to i2c_transfer because of
113 * the need to delay after each write to the chip. Also,
114 * we write the century byte first, since we set the write-protect
115 * bit as part of the burst write.
117 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
119 rc = i2c_transfer(client->adapter, century_msgs,
120 ARRAY_SIZE(century_msgs));
121 if (rc != ARRAY_SIZE(century_msgs))
124 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
126 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
128 rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
129 if (rc != ARRAY_SIZE(burst_msgs))
131 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
136 dev_err(&client->dev, "%s: register write failed\n", __func__);
140 static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
142 struct i2c_client *client = to_i2c_client(dev);
144 u8 regs[MAX6900_REG_LEN];
146 rc = max6900_i2c_read_regs(client, regs);
150 tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
151 tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
152 tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
153 tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
154 tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
155 tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
156 bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
157 tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
162 static int max6900_i2c_clear_write_protect(struct i2c_client *client)
164 return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
167 static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
169 struct i2c_client *client = to_i2c_client(dev);
170 u8 regs[MAX6900_REG_LEN];
173 rc = max6900_i2c_clear_write_protect(client);
177 regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
178 regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
179 regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
180 regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
181 regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
182 regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
183 regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
184 regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
185 /* set write protect */
186 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
188 rc = max6900_i2c_write_regs(client, regs);
195 static const struct rtc_class_ops max6900_rtc_ops = {
196 .read_time = max6900_rtc_read_time,
197 .set_time = max6900_rtc_set_time,
200 static int max6900_probe(struct i2c_client *client)
202 struct rtc_device *rtc;
204 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
207 rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
208 &max6900_rtc_ops, THIS_MODULE);
212 i2c_set_clientdata(client, rtc);
217 static const struct i2c_device_id max6900_id[] = {
221 MODULE_DEVICE_TABLE(i2c, max6900_id);
223 static struct i2c_driver max6900_driver = {
225 .name = "rtc-max6900",
227 .probe_new = max6900_probe,
228 .id_table = max6900_id,
231 module_i2c_driver(max6900_driver);
233 MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
234 MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
235 MODULE_LICENSE("GPL");