2 * I2C client/driver for the ST M41T80 family of i2c rtc chips.
4 * Author: Alexander Bigga <ab@mycable.de>
6 * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
8 * 2006 (c) mycable GmbH
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/i2c.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
24 #define M41T80_REG_SSEC 0
25 #define M41T80_REG_SEC 1
26 #define M41T80_REG_MIN 2
27 #define M41T80_REG_HOUR 3
28 #define M41T80_REG_WDAY 4
29 #define M41T80_REG_DAY 5
30 #define M41T80_REG_MON 6
31 #define M41T80_REG_YEAR 7
32 #define M41T80_REG_ALARM_MON 0xa
33 #define M41T80_REG_ALARM_DAY 0xb
34 #define M41T80_REG_ALARM_HOUR 0xc
35 #define M41T80_REG_ALARM_MIN 0xd
36 #define M41T80_REG_ALARM_SEC 0xe
37 #define M41T80_REG_FLAGS 0xf
38 #define M41T80_REG_SQW 0x13
40 #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
41 #define M41T80_ALARM_REG_SIZE \
42 (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
44 #define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */
45 #define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */
46 #define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */
47 #define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
48 #define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
49 #define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
51 #define M41T80_FEATURE_HT (1 << 0)
52 #define M41T80_FEATURE_BL (1 << 1)
54 #define DRV_VERSION "0.05"
56 struct m41t80_chip_info {
61 static const struct m41t80_chip_info m41t80_chip_info_tbl[] = {
68 .features = M41T80_FEATURE_HT,
72 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
76 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
80 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
84 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
88 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
92 .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
97 const struct m41t80_chip_info *chip;
98 struct rtc_device *rtc;
101 static int m41t80_get_datetime(struct i2c_client *client,
104 u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
105 struct i2c_msg msgs[] = {
107 .addr = client->addr,
113 .addr = client->addr,
115 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
116 .buf = buf + M41T80_REG_SEC,
120 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
121 dev_err(&client->dev, "read error\n");
125 tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f);
126 tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f);
127 tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f);
128 tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f);
129 tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
130 tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1;
132 /* assume 20YY not 19YY, and ignore the Century Bit */
133 tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
137 /* Sets the given date and time to the real time clock. */
138 static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
140 u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
142 u8 dt_addr[1] = { M41T80_REG_SEC };
143 struct i2c_msg msgs_in[] = {
145 .addr = client->addr,
151 .addr = client->addr,
153 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
154 .buf = buf + M41T80_REG_SEC,
157 struct i2c_msg msgs[] = {
159 .addr = client->addr,
161 .len = 1 + M41T80_DATETIME_REG_SIZE,
166 /* Read current reg values into buf[1..7] */
167 if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
168 dev_err(&client->dev, "read error\n");
172 wbuf[0] = 0; /* offset into rtc's regs */
173 /* Merge time-data and register flags into buf[0..7] */
174 buf[M41T80_REG_SSEC] = 0;
175 buf[M41T80_REG_SEC] =
176 BIN2BCD(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f);
177 buf[M41T80_REG_MIN] =
178 BIN2BCD(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f);
179 buf[M41T80_REG_HOUR] =
180 BIN2BCD(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ;
181 buf[M41T80_REG_WDAY] =
182 (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07);
183 buf[M41T80_REG_DAY] =
184 BIN2BCD(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f);
185 buf[M41T80_REG_MON] =
186 BIN2BCD(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f);
187 /* assume 20YY not 19YY */
188 buf[M41T80_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
190 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
191 dev_err(&client->dev, "write error\n");
197 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
198 static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
200 struct i2c_client *client = to_i2c_client(dev);
201 struct m41t80_data *clientdata = i2c_get_clientdata(client);
204 if (clientdata->chip->features & M41T80_FEATURE_BL) {
205 reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
206 seq_printf(seq, "battery\t\t: %s\n",
207 (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
212 #define m41t80_rtc_proc NULL
215 static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
217 return m41t80_get_datetime(to_i2c_client(dev), tm);
220 static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
222 return m41t80_set_datetime(to_i2c_client(dev), tm);
225 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
227 m41t80_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
229 struct i2c_client *client = to_i2c_client(dev);
240 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
245 rc &= ~M41T80_ALMON_AFE;
248 rc |= M41T80_ALMON_AFE;
251 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, rc) < 0)
258 #define m41t80_rtc_ioctl NULL
261 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
263 struct i2c_client *client = to_i2c_client(dev);
264 u8 wbuf[1 + M41T80_ALARM_REG_SIZE];
266 u8 *reg = buf - M41T80_REG_ALARM_MON;
267 u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
268 struct i2c_msg msgs_in[] = {
270 .addr = client->addr,
276 .addr = client->addr,
278 .len = M41T80_ALARM_REG_SIZE,
282 struct i2c_msg msgs[] = {
284 .addr = client->addr,
286 .len = 1 + M41T80_ALARM_REG_SIZE,
291 if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
292 dev_err(&client->dev, "read error\n");
295 reg[M41T80_REG_ALARM_MON] &= ~(0x1f | M41T80_ALMON_AFE);
296 reg[M41T80_REG_ALARM_DAY] = 0;
297 reg[M41T80_REG_ALARM_HOUR] &= ~(0x3f | 0x80);
298 reg[M41T80_REG_ALARM_MIN] = 0;
299 reg[M41T80_REG_ALARM_SEC] = 0;
301 wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
302 reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
303 BIN2BCD(t->time.tm_sec) : 0x80;
304 reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
305 BIN2BCD(t->time.tm_min) : 0x80;
306 reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ?
307 BIN2BCD(t->time.tm_hour) : 0x80;
308 reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ?
309 BIN2BCD(t->time.tm_mday) : 0x80;
310 if (t->time.tm_mon >= 0)
311 reg[M41T80_REG_ALARM_MON] |= BIN2BCD(t->time.tm_mon + 1);
313 reg[M41T80_REG_ALARM_DAY] |= 0x40;
315 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
316 dev_err(&client->dev, "write error\n");
321 reg[M41T80_REG_ALARM_MON] |= M41T80_ALMON_AFE;
322 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
323 reg[M41T80_REG_ALARM_MON]) < 0) {
324 dev_err(&client->dev, "write error\n");
331 static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
333 struct i2c_client *client = to_i2c_client(dev);
334 u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */
335 u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
336 u8 *reg = buf - M41T80_REG_ALARM_MON;
337 struct i2c_msg msgs[] = {
339 .addr = client->addr,
345 .addr = client->addr,
347 .len = M41T80_ALARM_REG_SIZE + 1,
352 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
353 dev_err(&client->dev, "read error\n");
358 t->time.tm_hour = -1;
359 t->time.tm_mday = -1;
361 if (!(reg[M41T80_REG_ALARM_SEC] & 0x80))
362 t->time.tm_sec = BCD2BIN(reg[M41T80_REG_ALARM_SEC] & 0x7f);
363 if (!(reg[M41T80_REG_ALARM_MIN] & 0x80))
364 t->time.tm_min = BCD2BIN(reg[M41T80_REG_ALARM_MIN] & 0x7f);
365 if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80))
366 t->time.tm_hour = BCD2BIN(reg[M41T80_REG_ALARM_HOUR] & 0x3f);
367 if (!(reg[M41T80_REG_ALARM_DAY] & 0x80))
368 t->time.tm_mday = BCD2BIN(reg[M41T80_REG_ALARM_DAY] & 0x3f);
369 if (!(reg[M41T80_REG_ALARM_DAY] & 0x40))
370 t->time.tm_mon = BCD2BIN(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1;
371 t->time.tm_year = -1;
372 t->time.tm_wday = -1;
373 t->time.tm_yday = -1;
374 t->time.tm_isdst = -1;
375 t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE);
376 t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF);
380 static struct rtc_class_ops m41t80_rtc_ops = {
381 .read_time = m41t80_rtc_read_time,
382 .set_time = m41t80_rtc_set_time,
383 .read_alarm = m41t80_rtc_read_alarm,
384 .set_alarm = m41t80_rtc_set_alarm,
385 .proc = m41t80_rtc_proc,
386 .ioctl = m41t80_rtc_ioctl,
389 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
390 static ssize_t m41t80_sysfs_show_flags(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct i2c_client *client = to_i2c_client(dev);
396 val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
399 return sprintf(buf, "%#x\n", val);
401 static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
403 static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct i2c_client *client = to_i2c_client(dev);
409 val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
412 val = (val >> 4) & 0xf;
422 return sprintf(buf, "%d\n", val);
424 static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
425 struct device_attribute *attr,
426 const char *buf, size_t count)
428 struct i2c_client *client = to_i2c_client(dev);
430 int val = simple_strtoul(buf, NULL, 0);
433 if (!is_power_of_2(val))
443 /* disable SQW, set SQW frequency & re-enable */
444 almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
447 sqw = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
450 sqw = (sqw & 0x0f) | (val << 4);
451 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
452 almon & ~M41T80_ALMON_SQWE) < 0 ||
453 i2c_smbus_write_byte_data(client, M41T80_REG_SQW, sqw) < 0)
455 if (val && i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
456 almon | M41T80_ALMON_SQWE) < 0)
460 static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
461 m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
463 static struct attribute *attrs[] = {
464 &dev_attr_flags.attr,
465 &dev_attr_sqwfreq.attr,
468 static struct attribute_group attr_group = {
472 static int m41t80_sysfs_register(struct device *dev)
474 return sysfs_create_group(&dev->kobj, &attr_group);
477 static int m41t80_sysfs_register(struct device *dev)
484 *****************************************************************************
488 *****************************************************************************
490 static int m41t80_probe(struct i2c_client *client)
493 struct rtc_device *rtc = NULL;
495 const struct m41t80_chip_info *chip;
496 struct m41t80_data *clientdata = NULL;
498 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
499 | I2C_FUNC_SMBUS_BYTE_DATA)) {
504 dev_info(&client->dev,
505 "chip found, driver version " DRV_VERSION "\n");
508 for (i = 0; i < ARRAY_SIZE(m41t80_chip_info_tbl); i++) {
509 if (!strcmp(m41t80_chip_info_tbl[i].name, client->name)) {
510 chip = &m41t80_chip_info_tbl[i];
515 dev_err(&client->dev, "%s is not supported\n", client->name);
520 clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
526 rtc = rtc_device_register(client->name, &client->dev,
527 &m41t80_rtc_ops, THIS_MODULE);
534 clientdata->rtc = rtc;
535 clientdata->chip = chip;
536 i2c_set_clientdata(client, clientdata);
538 /* Make sure HT (Halt Update) bit is cleared */
539 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
543 if (rc & M41T80_ALHOUR_HT) {
544 if (chip->features & M41T80_FEATURE_HT) {
545 m41t80_get_datetime(client, &tm);
546 dev_info(&client->dev, "HT bit was set!\n");
547 dev_info(&client->dev,
549 "%04i-%02i-%02i %02i:%02i:%02i\n",
551 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
552 tm.tm_min, tm.tm_sec);
554 if (i2c_smbus_write_byte_data(client,
555 M41T80_REG_ALARM_HOUR,
556 rc & ~M41T80_ALHOUR_HT) < 0)
560 /* Make sure ST (stop) bit is cleared */
561 rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
565 if (rc & M41T80_SEC_ST) {
566 if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
567 rc & ~M41T80_SEC_ST) < 0)
571 rc = m41t80_sysfs_register(&client->dev);
579 dev_err(&client->dev, "Can't clear ST bit\n");
583 dev_err(&client->dev, "Can't clear HT bit\n");
588 rtc_device_unregister(rtc);
593 static int m41t80_remove(struct i2c_client *client)
595 struct m41t80_data *clientdata = i2c_get_clientdata(client);
596 struct rtc_device *rtc = clientdata->rtc;
599 rtc_device_unregister(rtc);
605 static struct i2c_driver m41t80_driver = {
609 .probe = m41t80_probe,
610 .remove = m41t80_remove,
613 static int __init m41t80_rtc_init(void)
615 return i2c_add_driver(&m41t80_driver);
618 static void __exit m41t80_rtc_exit(void)
620 i2c_del_driver(&m41t80_driver);
623 MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
624 MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
625 MODULE_LICENSE("GPL");
626 MODULE_VERSION(DRV_VERSION);
628 module_init(m41t80_rtc_init);
629 module_exit(m41t80_rtc_exit);