17 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
24 #include <linux/string.h>
25 #ifdef CONFIG_RTC_DRV_M41T80_WDT
29 #include <linux/reboot.h>
30 #include <linux/watchdog.h>
33 #define M41T80_REG_SSEC 0
34 #define M41T80_REG_SEC 1
35 #define M41T80_REG_MIN 2
36 #define M41T80_REG_HOUR 3
37 #define M41T80_REG_WDAY 4
38 #define M41T80_REG_DAY 5
39 #define M41T80_REG_MON 6
40 #define M41T80_REG_YEAR 7
41 #define M41T80_REG_ALARM_MON 0xa
42 #define M41T80_REG_ALARM_DAY 0xb
43 #define M41T80_REG_ALARM_HOUR 0xc
44 #define M41T80_REG_ALARM_MIN 0xd
45 #define M41T80_REG_ALARM_SEC 0xe
46 #define M41T80_REG_FLAGS 0xf
47 #define M41T80_REG_SQW 0x13
49 #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
50 #define M41T80_ALARM_REG_SIZE \
51 (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
53 #define M41T80_SEC_ST (1 << 7)
54 #define M41T80_ALMON_AFE (1 << 7)
55 #define M41T80_ALMON_SQWE (1 << 6)
56 #define M41T80_ALHOUR_HT (1 << 6)
57 #define M41T80_FLAGS_AF (1 << 6)
58 #define M41T80_FLAGS_BATT_LOW (1 << 4)
59 #define M41T80_WATCHDOG_RB2 (1 << 7)
60 #define M41T80_WATCHDOG_RB1 (1 << 1)
61 #define M41T80_WATCHDOG_RB0 (1 << 0)
63 #define M41T80_FEATURE_HT (1 << 0)
64 #define M41T80_FEATURE_BL (1 << 1)
65 #define M41T80_FEATURE_SQ (1 << 2)
66 #define M41T80_FEATURE_WD (1 << 3)
67 #define M41T80_FEATURE_SQ_ALT (1 << 4)
69 #define DRV_VERSION "0.05"
104 .addr = client->
addr,
142 .addr = client->
addr,
188 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
192 struct m41t80_data *clientdata = i2c_get_clientdata(client);
203 #define m41t80_rtc_proc NULL
206 static int m41t80_rtc_read_time(
struct device *dev,
struct rtc_time *tm)
211 static int m41t80_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
221 .read_time = m41t80_rtc_read_time,
222 .set_time = m41t80_rtc_set_time,
226 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
236 return sprintf(buf,
"%#x\n", val);
244 struct m41t80_data *clientdata = i2c_get_clientdata(client);
256 val = (val >> 4) & 0xf;
266 return sprintf(buf,
"%d\n", val);
270 const char *buf,
size_t count)
273 struct m41t80_data *clientdata = i2c_get_clientdata(client);
274 int almon, sqw, reg_sqw;
301 sqw = (sqw & 0x0f) | (val << 4);
312 m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
315 &dev_attr_flags.attr,
316 &dev_attr_sqwfreq.attr,
323 static int m41t80_sysfs_register(
struct device *dev)
328 static int m41t80_sysfs_register(
struct device *dev)
334 #ifdef CONFIG_RTC_DRV_M41T80_WDT
347 static int wdt_margin =
WD_TIMO;
351 static unsigned long wdt_is_open;
360 static void wdt_ping(
void)
371 struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
376 i2c_data[1] = (wdt_margin & 0xFC) | 0x83;
381 i2c_data[1] = wdt_margin<<2 | 0x82;
398 static void wdt_disable(
void)
400 unsigned char i2c_data[2], i2c_buf[0x10];
409 .addr = save_client->
addr,
443 size_t count, loff_t *ppos)
453 size_t count, loff_t *ppos)
469 static int wdt_ioctl(
struct file *file,
unsigned int cmd,
476 .firmware_version = 1,
477 .identity =
"M41T80 WTD"
483 sizeof(ident)) ? -
EFAULT : 0;
487 return put_user(boot_flag, (
int __user *)arg);
492 if (
get_user(new_margin, (
int __user *)arg))
495 if (new_margin < 1 || new_margin > 124)
497 wdt_margin = new_margin;
501 return put_user(wdt_margin, (
int __user *)arg);
508 pr_info(
"rtc-m41t80: disable watchdog\n");
513 pr_info(
"rtc-m41t80: enable watchdog\n");
522 static long wdt_unlocked_ioctl(
struct file *file,
unsigned int cmd,
528 ret = wdt_ioctl(file, cmd, arg);
540 static int wdt_open(
struct inode *
inode,
struct file *file)
564 static int wdt_release(
struct inode *inode,
struct file *file)
594 .unlocked_ioctl = wdt_unlocked_ioctl,
597 .release = wdt_release,
623 static int m41t80_probe(
struct i2c_client *client,
640 clientdata = kzalloc(
sizeof(*clientdata),
GFP_KERNEL);
646 clientdata->
features =
id->driver_data;
647 i2c_set_clientdata(client, clientdata);
666 m41t80_get_datetime(client, &tm);
670 "%04i-%02i-%02i %02i:%02i:%02i\n",
677 rc & ~M41T80_ALHOUR_HT) < 0)
688 rc & ~M41T80_SEC_ST) < 0)
692 rc = m41t80_sysfs_register(&client->
dev);
696 #ifdef CONFIG_RTC_DRV_M41T80_WDT
713 dev_err(&client->
dev,
"Can't clear ST bit\n");
717 dev_err(&client->
dev,
"Can't clear HT bit\n");
727 static int m41t80_remove(
struct i2c_client *client)
729 struct m41t80_data *clientdata = i2c_get_clientdata(client);
732 #ifdef CONFIG_RTC_DRV_M41T80_WDT
747 .name =
"rtc-m41t80",
749 .probe = m41t80_probe,
750 .remove = m41t80_remove,
751 .id_table = m41t80_id,