Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rtc class driver for the Maxim MAX6900 chip |
| 3 | * |
| 4 | * Author: Dale Farnsworth <dale@farnsworth.org> |
| 5 | * |
| 6 | * based on previously existing rtc class drivers |
| 7 | * |
| 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 |
| 11 | * or implied. |
| 12 | */ |
| 13 | |
| 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> |
| 19 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 20 | /* |
| 21 | * register indices |
| 22 | */ |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 23 | #define MAX6900_REG_SC 0 /* seconds 00-59 */ |
| 24 | #define MAX6900_REG_MN 1 /* minutes 00-59 */ |
| 25 | #define MAX6900_REG_HR 2 /* hours 00-23 */ |
| 26 | #define MAX6900_REG_DT 3 /* day of month 00-31 */ |
| 27 | #define MAX6900_REG_MO 4 /* month 01-12 */ |
| 28 | #define MAX6900_REG_DW 5 /* day of week 1-7 */ |
| 29 | #define MAX6900_REG_YR 6 /* year 00-99 */ |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 30 | #define MAX6900_REG_CT 7 /* control */ |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 31 | /* register 8 is undocumented */ |
| 32 | #define MAX6900_REG_CENTURY 9 /* century */ |
| 33 | #define MAX6900_REG_LEN 10 |
| 34 | |
| 35 | #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */ |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 36 | |
| 37 | #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */ |
| 38 | |
| 39 | /* |
| 40 | * register read/write commands |
| 41 | */ |
| 42 | #define MAX6900_REG_CONTROL_WRITE 0x8e |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 43 | #define MAX6900_REG_CENTURY_WRITE 0x92 |
| 44 | #define MAX6900_REG_CENTURY_READ 0x93 |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 45 | #define MAX6900_REG_RESERVED_READ 0x96 |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 46 | #define MAX6900_REG_BURST_WRITE 0xbe |
| 47 | #define MAX6900_REG_BURST_READ 0xbf |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 48 | |
| 49 | #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */ |
| 50 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 51 | static struct i2c_driver max6900_driver; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 52 | |
| 53 | static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf) |
| 54 | { |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 55 | u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ }; |
| 56 | u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ }; |
| 57 | struct i2c_msg msgs[4] = { |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 58 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 59 | .addr = client->addr, |
| 60 | .flags = 0, /* write */ |
| 61 | .len = sizeof(reg_burst_read), |
| 62 | .buf = reg_burst_read} |
| 63 | , |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 64 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 65 | .addr = client->addr, |
| 66 | .flags = I2C_M_RD, |
| 67 | .len = MAX6900_BURST_LEN, |
| 68 | .buf = buf} |
| 69 | , |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 70 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 71 | .addr = client->addr, |
| 72 | .flags = 0, /* write */ |
| 73 | .len = sizeof(reg_century_read), |
| 74 | .buf = reg_century_read} |
| 75 | , |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 76 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 77 | .addr = client->addr, |
| 78 | .flags = I2C_M_RD, |
| 79 | .len = sizeof(buf[MAX6900_REG_CENTURY]), |
| 80 | .buf = &buf[MAX6900_REG_CENTURY] |
| 81 | } |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 82 | }; |
| 83 | int rc; |
| 84 | |
| 85 | rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 86 | if (rc != ARRAY_SIZE(msgs)) { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 87 | dev_err(&client->dev, "%s: register read failed\n", __func__); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 88 | return -EIO; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf) |
| 94 | { |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 95 | u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE }; |
| 96 | struct i2c_msg century_msgs[1] = { |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 97 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 98 | .addr = client->addr, |
| 99 | .flags = 0, /* write */ |
| 100 | .len = sizeof(i2c_century_buf), |
| 101 | .buf = i2c_century_buf} |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 102 | }; |
| 103 | u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE }; |
| 104 | struct i2c_msg burst_msgs[1] = { |
| 105 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 106 | .addr = client->addr, |
| 107 | .flags = 0, /* write */ |
| 108 | .len = sizeof(i2c_burst_buf), |
| 109 | .buf = i2c_burst_buf} |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 110 | }; |
| 111 | int rc; |
| 112 | |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 113 | /* |
| 114 | * We have to make separate calls to i2c_transfer because of |
| 115 | * the need to delay after each write to the chip. Also, |
| 116 | * we write the century byte first, since we set the write-protect |
| 117 | * bit as part of the burst write. |
| 118 | */ |
| 119 | i2c_century_buf[1] = buf[MAX6900_REG_CENTURY]; |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 120 | |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 121 | rc = i2c_transfer(client->adapter, century_msgs, |
| 122 | ARRAY_SIZE(century_msgs)); |
| 123 | if (rc != ARRAY_SIZE(century_msgs)) |
| 124 | goto write_failed; |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 125 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 126 | msleep(MAX6900_IDLE_TIME_AFTER_WRITE); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 127 | |
| 128 | memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN); |
| 129 | |
| 130 | rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs)); |
| 131 | if (rc != ARRAY_SIZE(burst_msgs)) |
| 132 | goto write_failed; |
| 133 | msleep(MAX6900_IDLE_TIME_AFTER_WRITE); |
| 134 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 135 | return 0; |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 136 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 137 | write_failed: |
| 138 | dev_err(&client->dev, "%s: register write failed\n", __func__); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 139 | return -EIO; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 142 | static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) |
| 143 | { |
| 144 | int rc; |
| 145 | u8 regs[MAX6900_REG_LEN]; |
| 146 | |
| 147 | rc = max6900_i2c_read_regs(client, regs); |
| 148 | if (rc < 0) |
| 149 | return rc; |
| 150 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 151 | tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]); |
| 152 | tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]); |
| 153 | tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f); |
| 154 | tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]); |
| 155 | tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1; |
| 156 | tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) + |
| 157 | bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900; |
| 158 | tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 159 | |
Wan ZongShun | c814dc1 | 2010-08-10 18:02:16 -0700 | [diff] [blame] | 160 | return rtc_valid_tm(tm); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int max6900_i2c_clear_write_protect(struct i2c_client *client) |
| 164 | { |
Sachin Kamat | f16189a | 2013-11-12 15:10:27 -0800 | [diff] [blame] | 165 | return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 168 | static int |
| 169 | max6900_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm) |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 170 | { |
| 171 | u8 regs[MAX6900_REG_LEN]; |
| 172 | int rc; |
| 173 | |
| 174 | rc = max6900_i2c_clear_write_protect(client); |
| 175 | if (rc < 0) |
| 176 | return rc; |
| 177 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 178 | regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec); |
| 179 | regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min); |
| 180 | regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour); |
| 181 | regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday); |
| 182 | regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1); |
| 183 | regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday); |
| 184 | regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100); |
| 185 | regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 186 | /* set write protect */ |
| 187 | regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 188 | |
| 189 | rc = max6900_i2c_write_regs(client, regs); |
| 190 | if (rc < 0) |
| 191 | return rc; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 197 | { |
| 198 | return max6900_i2c_read_time(to_i2c_client(dev), tm); |
| 199 | } |
| 200 | |
| 201 | static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 202 | { |
| 203 | return max6900_i2c_set_time(to_i2c_client(dev), tm); |
| 204 | } |
| 205 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 206 | static const struct rtc_class_ops max6900_rtc_ops = { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 207 | .read_time = max6900_rtc_read_time, |
| 208 | .set_time = max6900_rtc_set_time, |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 211 | static int |
| 212 | max6900_probe(struct i2c_client *client, const struct i2c_device_id *id) |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 213 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 214 | struct rtc_device *rtc; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 215 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 216 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 217 | return -ENODEV; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 218 | |
Jingoo Han | feb141d | 2013-04-29 16:19:43 -0700 | [diff] [blame] | 219 | rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name, |
| 220 | &max6900_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 221 | if (IS_ERR(rtc)) |
| 222 | return PTR_ERR(rtc); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 223 | |
| 224 | i2c_set_clientdata(client, rtc); |
| 225 | |
| 226 | return 0; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 229 | static struct i2c_device_id max6900_id[] = { |
| 230 | { "max6900", 0 }, |
| 231 | { } |
| 232 | }; |
Axel Lin | 8ad0f5b | 2011-04-18 20:16:57 +0800 | [diff] [blame] | 233 | MODULE_DEVICE_TABLE(i2c, max6900_id); |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 234 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 235 | static struct i2c_driver max6900_driver = { |
| 236 | .driver = { |
| 237 | .name = "rtc-max6900", |
| 238 | }, |
| 239 | .probe = max6900_probe, |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 240 | .id_table = max6900_id, |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 243 | module_i2c_driver(max6900_driver); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 244 | |
| 245 | MODULE_DESCRIPTION("Maxim MAX6900 RTC driver"); |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 246 | MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>"); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 247 | MODULE_LICENSE("GPL"); |