/*
* I2C client/driver for the ST M41T80 family of i2c rtc chips.
*
* Author: Alexander Bigga <ab@mycable.de>
*
* Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
*
* 2006 (c) mycable GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/i2c.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#ifdef CONFIG_RTC_DRV_M41T80_WDT
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/reboot.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#endif
#define M41T80_REG_SSEC 0
#define M41T80_REG_SEC 1
#define M41T80_REG_MIN 2
#define M41T80_REG_HOUR 3
#define M41T80_REG_WDAY 4
#define M41T80_REG_DAY 5
#define M41T80_REG_MON 6
#define M41T80_REG_YEAR 7
#define M41T80_REG_ALARM_MON 0xa
#define M41T80_REG_ALARM_DAY 0xb
#define M41T80_REG_ALARM_HOUR 0xc
#define M41T80_REG_ALARM_MIN 0xd
#define M41T80_REG_ALARM_SEC 0xe
#define M41T80_REG_FLAGS 0xf
#define M41T80_REG_SQW 0x13
#define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
#define M41T80_ALARM_REG_SIZE \
(M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
#define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */
#define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */
#define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */
#define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
#define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
#define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
#define M41T80_FEATURE_HT (1 << 0)
#define M41T80_FEATURE_BL (1 << 1)
#define DRV_VERSION "0.05"
struct m41t80_chip_info {
const char *name;
u8 features;
};
static const struct m41t80_chip_info m41t80_chip_info_tbl[] = {
{
.name = "m41t80",
.features = 0,
},
{
.name = "m41t81",
.features = M41T80_FEATURE_HT,
},
{
.name = "m41t81s",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
{
.name = "m41t82",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
{
.name = "m41t83",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
{
.name = "m41st84",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
{
.name = "m41st85",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
{
.name = "m41st87",
.features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
},
};
struct m41t80_data {
const struct m41t80_chip_info *chip;
struct rtc_device *rtc;
};
static int m41t80_get_datetime(struct i2c_client *client,
struct rtc_time *tm)
{
u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = 1,
.buf = dt_addr,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
.buf = buf + M41T80_REG_SEC,
},
};
if (i2c_transfer(client->adapter, msgs, 2) < 0) {
dev_err(&client->dev, "read error\n");
return -EIO;
}
tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f);
tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f);
tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f);
tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f);
tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1;
/* assume 20YY not 19YY, and ignore the Century Bit */
tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
return 0;
}
/* Sets the given date and time to the real time clock. */
static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
{
u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
u8 *buf = &wbuf[1];
u8 dt_addr[1] = { M41T80_REG_SEC };
struct i2c_msg msgs_in[] = {
{
.addr = client->addr,
.flags = 0,
.len = 1,
.buf = dt_addr,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
.buf = buf + M41T80_REG_SEC,
},
};
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = 1 + M41T80_DATETIME_REG_SIZE,
.buf = wbuf,
},
};
/* Read current reg values into buf[1..7] */
if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
dev_err(&client->dev, "read error\n");
return -EIO;
}
wbuf[0] = 0; /* offset into rtc's regs */
<