0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/i2c.h>
0014 #include <linux/bcd.h>
0015 #include <linux/rtc.h>
0016 #include <linux/delay.h>
0017
0018
0019
0020
0021 #define MAX6900_REG_SC 0
0022 #define MAX6900_REG_MN 1
0023 #define MAX6900_REG_HR 2
0024 #define MAX6900_REG_DT 3
0025 #define MAX6900_REG_MO 4
0026 #define MAX6900_REG_DW 5
0027 #define MAX6900_REG_YR 6
0028 #define MAX6900_REG_CT 7
0029
0030 #define MAX6900_REG_CENTURY 9
0031 #define MAX6900_REG_LEN 10
0032
0033 #define MAX6900_BURST_LEN 8
0034
0035 #define MAX6900_REG_CT_WP (1 << 7)
0036
0037
0038
0039
0040 #define MAX6900_REG_CONTROL_WRITE 0x8e
0041 #define MAX6900_REG_CENTURY_WRITE 0x92
0042 #define MAX6900_REG_CENTURY_READ 0x93
0043 #define MAX6900_REG_RESERVED_READ 0x96
0044 #define MAX6900_REG_BURST_WRITE 0xbe
0045 #define MAX6900_REG_BURST_READ 0xbf
0046
0047 #define MAX6900_IDLE_TIME_AFTER_WRITE 3
0048
0049 static struct i2c_driver max6900_driver;
0050
0051 static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
0052 {
0053 u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
0054 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
0055 struct i2c_msg msgs[4] = {
0056 {
0057 .addr = client->addr,
0058 .flags = 0,
0059 .len = sizeof(reg_burst_read),
0060 .buf = reg_burst_read}
0061 ,
0062 {
0063 .addr = client->addr,
0064 .flags = I2C_M_RD,
0065 .len = MAX6900_BURST_LEN,
0066 .buf = buf}
0067 ,
0068 {
0069 .addr = client->addr,
0070 .flags = 0,
0071 .len = sizeof(reg_century_read),
0072 .buf = reg_century_read}
0073 ,
0074 {
0075 .addr = client->addr,
0076 .flags = I2C_M_RD,
0077 .len = sizeof(buf[MAX6900_REG_CENTURY]),
0078 .buf = &buf[MAX6900_REG_CENTURY]
0079 }
0080 };
0081 int rc;
0082
0083 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0084 if (rc != ARRAY_SIZE(msgs)) {
0085 dev_err(&client->dev, "%s: register read failed\n", __func__);
0086 return -EIO;
0087 }
0088 return 0;
0089 }
0090
0091 static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
0092 {
0093 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
0094 struct i2c_msg century_msgs[1] = {
0095 {
0096 .addr = client->addr,
0097 .flags = 0,
0098 .len = sizeof(i2c_century_buf),
0099 .buf = i2c_century_buf}
0100 };
0101 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
0102 struct i2c_msg burst_msgs[1] = {
0103 {
0104 .addr = client->addr,
0105 .flags = 0,
0106 .len = sizeof(i2c_burst_buf),
0107 .buf = i2c_burst_buf}
0108 };
0109 int rc;
0110
0111
0112
0113
0114
0115
0116
0117 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
0118
0119 rc = i2c_transfer(client->adapter, century_msgs,
0120 ARRAY_SIZE(century_msgs));
0121 if (rc != ARRAY_SIZE(century_msgs))
0122 goto write_failed;
0123
0124 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
0125
0126 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
0127
0128 rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
0129 if (rc != ARRAY_SIZE(burst_msgs))
0130 goto write_failed;
0131 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
0132
0133 return 0;
0134
0135 write_failed:
0136 dev_err(&client->dev, "%s: register write failed\n", __func__);
0137 return -EIO;
0138 }
0139
0140 static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
0141 {
0142 struct i2c_client *client = to_i2c_client(dev);
0143 int rc;
0144 u8 regs[MAX6900_REG_LEN];
0145
0146 rc = max6900_i2c_read_regs(client, regs);
0147 if (rc < 0)
0148 return rc;
0149
0150 tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
0151 tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
0152 tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
0153 tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
0154 tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
0155 tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
0156 bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
0157 tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
0158
0159 return 0;
0160 }
0161
0162 static int max6900_i2c_clear_write_protect(struct i2c_client *client)
0163 {
0164 return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
0165 }
0166
0167 static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
0168 {
0169 struct i2c_client *client = to_i2c_client(dev);
0170 u8 regs[MAX6900_REG_LEN];
0171 int rc;
0172
0173 rc = max6900_i2c_clear_write_protect(client);
0174 if (rc < 0)
0175 return rc;
0176
0177 regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
0178 regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
0179 regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
0180 regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
0181 regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
0182 regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
0183 regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
0184 regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
0185
0186 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
0187
0188 rc = max6900_i2c_write_regs(client, regs);
0189 if (rc < 0)
0190 return rc;
0191
0192 return 0;
0193 }
0194
0195 static const struct rtc_class_ops max6900_rtc_ops = {
0196 .read_time = max6900_rtc_read_time,
0197 .set_time = max6900_rtc_set_time,
0198 };
0199
0200 static int max6900_probe(struct i2c_client *client)
0201 {
0202 struct rtc_device *rtc;
0203
0204 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0205 return -ENODEV;
0206
0207 rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
0208 &max6900_rtc_ops, THIS_MODULE);
0209 if (IS_ERR(rtc))
0210 return PTR_ERR(rtc);
0211
0212 i2c_set_clientdata(client, rtc);
0213
0214 return 0;
0215 }
0216
0217 static const struct i2c_device_id max6900_id[] = {
0218 { "max6900", 0 },
0219 { }
0220 };
0221 MODULE_DEVICE_TABLE(i2c, max6900_id);
0222
0223 static struct i2c_driver max6900_driver = {
0224 .driver = {
0225 .name = "rtc-max6900",
0226 },
0227 .probe_new = max6900_probe,
0228 .id_table = max6900_id,
0229 };
0230
0231 module_i2c_driver(max6900_driver);
0232
0233 MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
0234 MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
0235 MODULE_LICENSE("GPL");