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