2 * rtc class driver for the Maxim MAX6900 chip
4 * Author: Dale Farnsworth <dale@farnsworth.org>
6 * based on previously existing rtc class drivers
8 * 2007 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
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>
20 #define DRV_NAME "max6900"
21 #define DRV_VERSION "0.1"
26 #define MAX6900_REG_SC 0 /* seconds 00-59 */
27 #define MAX6900_REG_MN 1 /* minutes 00-59 */
28 #define MAX6900_REG_HR 2 /* hours 00-23 */
29 #define MAX6900_REG_DT 3 /* day of month 00-31 */
30 #define MAX6900_REG_MO 4 /* month 01-12 */
31 #define MAX6900_REG_DW 5 /* day of week 1-7 */
32 #define MAX6900_REG_YR 6 /* year 00-99 */
33 #define MAX6900_REG_CT 7 /* control */
34 /* register 8 is undocumented */
35 #define MAX6900_REG_CENTURY 9 /* century */
36 #define MAX6900_REG_LEN 10
38 #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */
40 #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
44 * register read/write commands
46 #define MAX6900_REG_CONTROL_WRITE 0x8e
47 #define MAX6900_REG_CENTURY_WRITE 0x92
48 #define MAX6900_REG_CENTURY_READ 0x93
49 #define MAX6900_REG_RESERVED_READ 0x96
50 #define MAX6900_REG_BURST_WRITE 0xbe
51 #define MAX6900_REG_BURST_READ 0xbf
53 #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
55 #define MAX6900_I2C_ADDR 0xa0
57 static const unsigned short normal_i2c[] = {
58 MAX6900_I2C_ADDR >> 1,
62 I2C_CLIENT_INSMOD; /* defines addr_data */
64 static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
66 static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
68 u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
69 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
70 struct i2c_msg msgs[4] = {
73 .flags = 0, /* write */
74 .len = sizeof(reg_burst_read),
80 .len = MAX6900_BURST_LEN,
85 .flags = 0, /* write */
86 .len = sizeof(reg_century_read),
87 .buf = reg_century_read
92 .len = sizeof(buf[MAX6900_REG_CENTURY]),
93 .buf = &buf[MAX6900_REG_CENTURY]
98 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
99 if (rc != ARRAY_SIZE(msgs)) {
100 dev_err(&client->dev, "%s: register read failed\n",
107 static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
109 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
110 struct i2c_msg century_msgs[1] = {
112 .addr = client->addr,
113 .flags = 0, /* write */
114 .len = sizeof(i2c_century_buf),
115 .buf = i2c_century_buf
118 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
119 struct i2c_msg burst_msgs[1] = {
121 .addr = client->addr,
122 .flags = 0, /* write */
123 .len = sizeof(i2c_burst_buf),
130 * We have to make separate calls to i2c_transfer because of
131 * the need to delay after each write to the chip. Also,
132 * we write the century byte first, since we set the write-protect
133 * bit as part of the burst write.
135 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
136 rc = i2c_transfer(client->adapter, century_msgs,
137 ARRAY_SIZE(century_msgs));
138 if (rc != ARRAY_SIZE(century_msgs))
140 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
142 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
144 rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
145 if (rc != ARRAY_SIZE(burst_msgs))
147 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
152 dev_err(&client->dev, "%s: register write failed\n",
157 static int max6900_i2c_validate_client(struct i2c_client *client)
159 u8 regs[MAX6900_REG_LEN];
164 0xc0, /* day of month */
166 0xf8, /* day of week */
174 reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
175 if (reserved != 0x07)
178 rc = max6900_i2c_read_regs(client, regs);
182 for (i = 0; i < ARRAY_SIZE(zero_mask); ++i) {
183 if (regs[i] & zero_mask[i])
190 static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
193 u8 regs[MAX6900_REG_LEN];
195 rc = max6900_i2c_read_regs(client, regs);
199 tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]);
200 tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]);
201 tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f);
202 tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]);
203 tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1;
204 tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) +
205 BCD2BIN(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
206 tm->tm_wday = BCD2BIN(regs[MAX6900_REG_DW]);
211 static int max6900_i2c_clear_write_protect(struct i2c_client *client)
214 rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
216 dev_err(&client->dev, "%s: control register write failed\n",
223 static int max6900_i2c_set_time(struct i2c_client *client,
224 struct rtc_time const *tm)
226 u8 regs[MAX6900_REG_LEN];
229 rc = max6900_i2c_clear_write_protect(client);
233 regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec);
234 regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min);
235 regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour);
236 regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday);
237 regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1);
238 regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday);
239 regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year % 100);
240 regs[MAX6900_REG_CENTURY] = BIN2BCD((tm->tm_year + 1900) / 100);
241 /* set write protect */
242 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
244 rc = max6900_i2c_write_regs(client, regs);
251 static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
253 return max6900_i2c_read_time(to_i2c_client(dev), tm);
256 static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
258 return max6900_i2c_set_time(to_i2c_client(dev), tm);
261 static int max6900_attach_adapter(struct i2c_adapter *adapter)
263 return i2c_probe(adapter, &addr_data, max6900_probe);
266 static int max6900_detach_client(struct i2c_client *client)
268 struct rtc_device *const rtc = i2c_get_clientdata(client);
271 rtc_device_unregister(rtc);
273 return i2c_detach_client(client);
276 static struct i2c_driver max6900_driver = {
280 .id = I2C_DRIVERID_MAX6900,
281 .attach_adapter = max6900_attach_adapter,
282 .detach_client = max6900_detach_client,
285 static const struct rtc_class_ops max6900_rtc_ops = {
286 .read_time = max6900_rtc_read_time,
287 .set_time = max6900_rtc_set_time,
290 static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind)
293 struct i2c_client *client = NULL;
294 struct rtc_device *rtc = NULL;
296 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
301 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
302 if (client == NULL) {
308 client->adapter = adapter;
309 client->driver = &max6900_driver;
310 strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
313 rc = max6900_i2c_validate_client(client);
318 rc = i2c_attach_client(client);
322 dev_info(&client->dev,
323 "chip found, driver version " DRV_VERSION "\n");
325 rtc = rtc_device_register(max6900_driver.driver.name,
327 &max6900_rtc_ops, THIS_MODULE);
333 i2c_set_clientdata(client, rtc);
338 i2c_detach_client(client);
344 static int __init max6900_init(void)
346 return i2c_add_driver(&max6900_driver);
349 static void __exit max6900_exit(void)
351 i2c_del_driver(&max6900_driver);
354 MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
355 MODULE_LICENSE("GPL");
356 MODULE_VERSION(DRV_VERSION);
358 module_init(max6900_init);
359 module_exit(max6900_exit);