1cd13635eSNobuhiro Iwamatsu // SPDX-License-Identifier: GPL-2.0-only
2aa5bd7e9SDale Farnsworth /*
3aa5bd7e9SDale Farnsworth * rtc class driver for the Maxim MAX6900 chip
4aa5bd7e9SDale Farnsworth *
5cd13635eSNobuhiro Iwamatsu * Copyright (c) 2007 MontaVista, Software, Inc.
6cd13635eSNobuhiro Iwamatsu *
7aa5bd7e9SDale Farnsworth * Author: Dale Farnsworth <dale@farnsworth.org>
8aa5bd7e9SDale Farnsworth *
9aa5bd7e9SDale Farnsworth * based on previously existing rtc class drivers
10aa5bd7e9SDale Farnsworth */
11aa5bd7e9SDale Farnsworth
12aa5bd7e9SDale Farnsworth #include <linux/module.h>
13aa5bd7e9SDale Farnsworth #include <linux/i2c.h>
14aa5bd7e9SDale Farnsworth #include <linux/bcd.h>
15aa5bd7e9SDale Farnsworth #include <linux/rtc.h>
16aa5bd7e9SDale Farnsworth #include <linux/delay.h>
17aa5bd7e9SDale Farnsworth
18aa5bd7e9SDale Farnsworth /*
19aa5bd7e9SDale Farnsworth * register indices
20aa5bd7e9SDale Farnsworth */
21aa5bd7e9SDale Farnsworth #define MAX6900_REG_SC 0 /* seconds 00-59 */
22aa5bd7e9SDale Farnsworth #define MAX6900_REG_MN 1 /* minutes 00-59 */
23aa5bd7e9SDale Farnsworth #define MAX6900_REG_HR 2 /* hours 00-23 */
24aa5bd7e9SDale Farnsworth #define MAX6900_REG_DT 3 /* day of month 00-31 */
25aa5bd7e9SDale Farnsworth #define MAX6900_REG_MO 4 /* month 01-12 */
26aa5bd7e9SDale Farnsworth #define MAX6900_REG_DW 5 /* day of week 1-7 */
27aa5bd7e9SDale Farnsworth #define MAX6900_REG_YR 6 /* year 00-99 */
28aa5bd7e9SDale Farnsworth #define MAX6900_REG_CT 7 /* control */
298a2601f6SDale Farnsworth /* register 8 is undocumented */
308a2601f6SDale Farnsworth #define MAX6900_REG_CENTURY 9 /* century */
318a2601f6SDale Farnsworth #define MAX6900_REG_LEN 10
328a2601f6SDale Farnsworth
338a2601f6SDale Farnsworth #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
34aa5bd7e9SDale Farnsworth
35aa5bd7e9SDale Farnsworth #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
36aa5bd7e9SDale Farnsworth
37aa5bd7e9SDale Farnsworth /*
38aa5bd7e9SDale Farnsworth * register read/write commands
39aa5bd7e9SDale Farnsworth */
40aa5bd7e9SDale Farnsworth #define MAX6900_REG_CONTROL_WRITE 0x8e
418a2601f6SDale Farnsworth #define MAX6900_REG_CENTURY_WRITE 0x92
428a2601f6SDale Farnsworth #define MAX6900_REG_CENTURY_READ 0x93
43aa5bd7e9SDale Farnsworth #define MAX6900_REG_RESERVED_READ 0x96
448a2601f6SDale Farnsworth #define MAX6900_REG_BURST_WRITE 0xbe
458a2601f6SDale Farnsworth #define MAX6900_REG_BURST_READ 0xbf
46aa5bd7e9SDale Farnsworth
47aa5bd7e9SDale Farnsworth #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
48aa5bd7e9SDale Farnsworth
496fd5c03fSAlessandro Zummo static struct i2c_driver max6900_driver;
50aa5bd7e9SDale Farnsworth
max6900_i2c_read_regs(struct i2c_client * client,u8 * buf)51aa5bd7e9SDale Farnsworth static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
52aa5bd7e9SDale Farnsworth {
538a2601f6SDale Farnsworth u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
548a2601f6SDale Farnsworth u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
558a2601f6SDale Farnsworth struct i2c_msg msgs[4] = {
56aa5bd7e9SDale Farnsworth {
57aa5bd7e9SDale Farnsworth .addr = client->addr,
58aa5bd7e9SDale Farnsworth .flags = 0, /* write */
598a2601f6SDale Farnsworth .len = sizeof(reg_burst_read),
606fd5c03fSAlessandro Zummo .buf = reg_burst_read}
616fd5c03fSAlessandro Zummo ,
62aa5bd7e9SDale Farnsworth {
63aa5bd7e9SDale Farnsworth .addr = client->addr,
64aa5bd7e9SDale Farnsworth .flags = I2C_M_RD,
658a2601f6SDale Farnsworth .len = MAX6900_BURST_LEN,
666fd5c03fSAlessandro Zummo .buf = buf}
676fd5c03fSAlessandro Zummo ,
688a2601f6SDale Farnsworth {
698a2601f6SDale Farnsworth .addr = client->addr,
708a2601f6SDale Farnsworth .flags = 0, /* write */
718a2601f6SDale Farnsworth .len = sizeof(reg_century_read),
726fd5c03fSAlessandro Zummo .buf = reg_century_read}
736fd5c03fSAlessandro Zummo ,
748a2601f6SDale Farnsworth {
758a2601f6SDale Farnsworth .addr = client->addr,
768a2601f6SDale Farnsworth .flags = I2C_M_RD,
778a2601f6SDale Farnsworth .len = sizeof(buf[MAX6900_REG_CENTURY]),
788a2601f6SDale Farnsworth .buf = &buf[MAX6900_REG_CENTURY]
79aa5bd7e9SDale Farnsworth }
80aa5bd7e9SDale Farnsworth };
81aa5bd7e9SDale Farnsworth int rc;
82aa5bd7e9SDale Farnsworth
83aa5bd7e9SDale Farnsworth rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
84aa5bd7e9SDale Farnsworth if (rc != ARRAY_SIZE(msgs)) {
856fd5c03fSAlessandro Zummo dev_err(&client->dev, "%s: register read failed\n", __func__);
86aa5bd7e9SDale Farnsworth return -EIO;
87aa5bd7e9SDale Farnsworth }
88aa5bd7e9SDale Farnsworth return 0;
89aa5bd7e9SDale Farnsworth }
90aa5bd7e9SDale Farnsworth
max6900_i2c_write_regs(struct i2c_client * client,u8 const * buf)91aa5bd7e9SDale Farnsworth static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
92aa5bd7e9SDale Farnsworth {
938a2601f6SDale Farnsworth u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
948a2601f6SDale Farnsworth struct i2c_msg century_msgs[1] = {
95aa5bd7e9SDale Farnsworth {
96aa5bd7e9SDale Farnsworth .addr = client->addr,
97aa5bd7e9SDale Farnsworth .flags = 0, /* write */
988a2601f6SDale Farnsworth .len = sizeof(i2c_century_buf),
996fd5c03fSAlessandro Zummo .buf = i2c_century_buf}
1008a2601f6SDale Farnsworth };
1018a2601f6SDale Farnsworth u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
1028a2601f6SDale Farnsworth struct i2c_msg burst_msgs[1] = {
1038a2601f6SDale Farnsworth {
1048a2601f6SDale Farnsworth .addr = client->addr,
1058a2601f6SDale Farnsworth .flags = 0, /* write */
1068a2601f6SDale Farnsworth .len = sizeof(i2c_burst_buf),
1076fd5c03fSAlessandro Zummo .buf = i2c_burst_buf}
108aa5bd7e9SDale Farnsworth };
109aa5bd7e9SDale Farnsworth int rc;
110aa5bd7e9SDale Farnsworth
1118a2601f6SDale Farnsworth /*
1128a2601f6SDale Farnsworth * We have to make separate calls to i2c_transfer because of
1138a2601f6SDale Farnsworth * the need to delay after each write to the chip. Also,
1148a2601f6SDale Farnsworth * we write the century byte first, since we set the write-protect
1158a2601f6SDale Farnsworth * bit as part of the burst write.
1168a2601f6SDale Farnsworth */
1178a2601f6SDale Farnsworth i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
1186fd5c03fSAlessandro Zummo
1198a2601f6SDale Farnsworth rc = i2c_transfer(client->adapter, century_msgs,
1208a2601f6SDale Farnsworth ARRAY_SIZE(century_msgs));
1218a2601f6SDale Farnsworth if (rc != ARRAY_SIZE(century_msgs))
1228a2601f6SDale Farnsworth goto write_failed;
1236fd5c03fSAlessandro Zummo
1248a2601f6SDale Farnsworth msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
125aa5bd7e9SDale Farnsworth
1268a2601f6SDale Farnsworth memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
1278a2601f6SDale Farnsworth
1288a2601f6SDale Farnsworth rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
1298a2601f6SDale Farnsworth if (rc != ARRAY_SIZE(burst_msgs))
1308a2601f6SDale Farnsworth goto write_failed;
1318a2601f6SDale Farnsworth msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
1328a2601f6SDale Farnsworth
1338a2601f6SDale Farnsworth return 0;
1348a2601f6SDale Farnsworth
1358a2601f6SDale Farnsworth write_failed:
1366fd5c03fSAlessandro Zummo dev_err(&client->dev, "%s: register write failed\n", __func__);
137aa5bd7e9SDale Farnsworth return -EIO;
138aa5bd7e9SDale Farnsworth }
139aa5bd7e9SDale Farnsworth
max6900_rtc_read_time(struct device * dev,struct rtc_time * tm)140859a6ddbSAlexandre Belloni static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
141aa5bd7e9SDale Farnsworth {
142859a6ddbSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev);
143aa5bd7e9SDale Farnsworth int rc;
144aa5bd7e9SDale Farnsworth u8 regs[MAX6900_REG_LEN];
145aa5bd7e9SDale Farnsworth
146aa5bd7e9SDale Farnsworth rc = max6900_i2c_read_regs(client, regs);
147aa5bd7e9SDale Farnsworth if (rc < 0)
148aa5bd7e9SDale Farnsworth return rc;
149aa5bd7e9SDale Farnsworth
150fe20ba70SAdrian Bunk tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
151fe20ba70SAdrian Bunk tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
152fe20ba70SAdrian Bunk tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
153fe20ba70SAdrian Bunk tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
154fe20ba70SAdrian Bunk tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
155fe20ba70SAdrian Bunk tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
156fe20ba70SAdrian Bunk bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
157fe20ba70SAdrian Bunk tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
158aa5bd7e9SDale Farnsworth
159ceed43f7SAlexandre Belloni return 0;
160aa5bd7e9SDale Farnsworth }
161aa5bd7e9SDale Farnsworth
max6900_i2c_clear_write_protect(struct i2c_client * client)162aa5bd7e9SDale Farnsworth static int max6900_i2c_clear_write_protect(struct i2c_client *client)
163aa5bd7e9SDale Farnsworth {
164f16189acSSachin Kamat return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
165aa5bd7e9SDale Farnsworth }
166aa5bd7e9SDale Farnsworth
max6900_rtc_set_time(struct device * dev,struct rtc_time * tm)167859a6ddbSAlexandre Belloni static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
168aa5bd7e9SDale Farnsworth {
169859a6ddbSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev);
170aa5bd7e9SDale Farnsworth u8 regs[MAX6900_REG_LEN];
171aa5bd7e9SDale Farnsworth int rc;
172aa5bd7e9SDale Farnsworth
173aa5bd7e9SDale Farnsworth rc = max6900_i2c_clear_write_protect(client);
174aa5bd7e9SDale Farnsworth if (rc < 0)
175aa5bd7e9SDale Farnsworth return rc;
176aa5bd7e9SDale Farnsworth
177fe20ba70SAdrian Bunk regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
178fe20ba70SAdrian Bunk regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
179fe20ba70SAdrian Bunk regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
180fe20ba70SAdrian Bunk regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
181fe20ba70SAdrian Bunk regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
182fe20ba70SAdrian Bunk regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
183fe20ba70SAdrian Bunk regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
184fe20ba70SAdrian Bunk regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
1858a2601f6SDale Farnsworth /* set write protect */
1868a2601f6SDale Farnsworth regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
187aa5bd7e9SDale Farnsworth
188aa5bd7e9SDale Farnsworth rc = max6900_i2c_write_regs(client, regs);
189aa5bd7e9SDale Farnsworth if (rc < 0)
190aa5bd7e9SDale Farnsworth return rc;
191aa5bd7e9SDale Farnsworth
192aa5bd7e9SDale Farnsworth return 0;
193aa5bd7e9SDale Farnsworth }
194aa5bd7e9SDale Farnsworth
195aa5bd7e9SDale Farnsworth static const struct rtc_class_ops max6900_rtc_ops = {
196aa5bd7e9SDale Farnsworth .read_time = max6900_rtc_read_time,
197aa5bd7e9SDale Farnsworth .set_time = max6900_rtc_set_time,
198aa5bd7e9SDale Farnsworth };
199aa5bd7e9SDale Farnsworth
max6900_probe(struct i2c_client * client)2003f4a3322SStephen Kitt static int max6900_probe(struct i2c_client *client)
201aa5bd7e9SDale Farnsworth {
2026fd5c03fSAlessandro Zummo struct rtc_device *rtc;
203aa5bd7e9SDale Farnsworth
2046fd5c03fSAlessandro Zummo if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
2056fd5c03fSAlessandro Zummo return -ENODEV;
206aa5bd7e9SDale Farnsworth
207feb141d1SJingoo Han rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
208feb141d1SJingoo Han &max6900_rtc_ops, THIS_MODULE);
2096fd5c03fSAlessandro Zummo if (IS_ERR(rtc))
2106fd5c03fSAlessandro Zummo return PTR_ERR(rtc);
211aa5bd7e9SDale Farnsworth
212aa5bd7e9SDale Farnsworth i2c_set_clientdata(client, rtc);
213aa5bd7e9SDale Farnsworth
214aa5bd7e9SDale Farnsworth return 0;
215aa5bd7e9SDale Farnsworth }
216aa5bd7e9SDale Farnsworth
21745a63518SArvind Yadav static const struct i2c_device_id max6900_id[] = {
218fe102c71SAlessandro Zummo { "max6900", 0 },
219fe102c71SAlessandro Zummo { }
220fe102c71SAlessandro Zummo };
2218ad0f5b6SAxel Lin MODULE_DEVICE_TABLE(i2c, max6900_id);
222fe102c71SAlessandro Zummo
2236fd5c03fSAlessandro Zummo static struct i2c_driver max6900_driver = {
2246fd5c03fSAlessandro Zummo .driver = {
2256fd5c03fSAlessandro Zummo .name = "rtc-max6900",
2266fd5c03fSAlessandro Zummo },
227*31b0cecbSUwe Kleine-König .probe = max6900_probe,
228fe102c71SAlessandro Zummo .id_table = max6900_id,
2296fd5c03fSAlessandro Zummo };
2306fd5c03fSAlessandro Zummo
2310abc9201SAxel Lin module_i2c_driver(max6900_driver);
232aa5bd7e9SDale Farnsworth
233aa5bd7e9SDale Farnsworth MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
2346fd5c03fSAlessandro Zummo MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
235aa5bd7e9SDale Farnsworth MODULE_LICENSE("GPL");
236