David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips. |
| 3 | * |
| 4 | * Copyright (C) 2005 James Chapman (ds1337 core) |
| 5 | * Copyright (C) 2006 David Brownell |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/bcd.h> |
| 19 | |
| 20 | |
| 21 | |
| 22 | /* We can't determine type by probing, but if we expect pre-Linux code |
| 23 | * to have set the chip up as a clock (turning on the oscillator and |
| 24 | * setting the date and time), Linux can ignore the non-clock features. |
| 25 | * That's a natural job for a factory or repair bench. |
| 26 | * |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 27 | * This is currently a simple no-alarms driver. If your board has the |
| 28 | * alarm irq wired up on a ds1337 or ds1339, and you want to use that, |
| 29 | * then look at the rtc-rs5c372 driver for code to steal... |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 30 | */ |
| 31 | enum ds_type { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 32 | ds_1307, |
| 33 | ds_1337, |
| 34 | ds_1338, |
| 35 | ds_1339, |
| 36 | ds_1340, |
| 37 | m41t00, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 38 | // rs5c372 too? different address... |
| 39 | }; |
| 40 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 41 | |
| 42 | /* RTC registers don't differ much, except for the century flag */ |
| 43 | #define DS1307_REG_SECS 0x00 /* 00-59 */ |
| 44 | # define DS1307_BIT_CH 0x80 |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 45 | # define DS1340_BIT_nEOSC 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 46 | #define DS1307_REG_MIN 0x01 /* 00-59 */ |
| 47 | #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */ |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 48 | # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */ |
| 49 | # define DS1307_BIT_PM 0x20 /* in REG_HOUR */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 50 | # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */ |
| 51 | # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */ |
| 52 | #define DS1307_REG_WDAY 0x03 /* 01-07 */ |
| 53 | #define DS1307_REG_MDAY 0x04 /* 01-31 */ |
| 54 | #define DS1307_REG_MONTH 0x05 /* 01-12 */ |
| 55 | # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */ |
| 56 | #define DS1307_REG_YEAR 0x06 /* 00-99 */ |
| 57 | |
| 58 | /* Other registers (control, status, alarms, trickle charge, NVRAM, etc) |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 59 | * start at 7, and they differ a LOT. Only control and status matter for |
| 60 | * basic RTC date and time functionality; be careful using them. |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 61 | */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 62 | #define DS1307_REG_CONTROL 0x07 /* or ds1338 */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 63 | # define DS1307_BIT_OUT 0x80 |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 64 | # define DS1338_BIT_OSF 0x20 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 65 | # define DS1307_BIT_SQWE 0x10 |
| 66 | # define DS1307_BIT_RS1 0x02 |
| 67 | # define DS1307_BIT_RS0 0x01 |
| 68 | #define DS1337_REG_CONTROL 0x0e |
| 69 | # define DS1337_BIT_nEOSC 0x80 |
| 70 | # define DS1337_BIT_RS2 0x10 |
| 71 | # define DS1337_BIT_RS1 0x08 |
| 72 | # define DS1337_BIT_INTCN 0x04 |
| 73 | # define DS1337_BIT_A2IE 0x02 |
| 74 | # define DS1337_BIT_A1IE 0x01 |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 75 | #define DS1340_REG_CONTROL 0x07 |
| 76 | # define DS1340_BIT_OUT 0x80 |
| 77 | # define DS1340_BIT_FT 0x40 |
| 78 | # define DS1340_BIT_CALIB_SIGN 0x20 |
| 79 | # define DS1340_M_CALIBRATION 0x1f |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 80 | #define DS1340_REG_FLAG 0x09 |
| 81 | # define DS1340_BIT_OSF 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 82 | #define DS1337_REG_STATUS 0x0f |
| 83 | # define DS1337_BIT_OSF 0x80 |
| 84 | # define DS1337_BIT_A2I 0x02 |
| 85 | # define DS1337_BIT_A1I 0x01 |
| 86 | #define DS1339_REG_TRICKLE 0x10 |
| 87 | |
| 88 | |
| 89 | |
| 90 | struct ds1307 { |
| 91 | u8 reg_addr; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 92 | bool has_nvram; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 93 | u8 regs[8]; |
| 94 | enum ds_type type; |
| 95 | struct i2c_msg msg[2]; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 96 | struct i2c_client *client; |
| 97 | struct i2c_client dev; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 98 | struct rtc_device *rtc; |
| 99 | }; |
| 100 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 101 | struct chip_desc { |
| 102 | char name[9]; |
| 103 | unsigned nvram56:1; |
| 104 | unsigned alarm:1; |
| 105 | enum ds_type type; |
| 106 | }; |
| 107 | |
| 108 | static const struct chip_desc chips[] = { { |
| 109 | .name = "ds1307", |
| 110 | .type = ds_1307, |
| 111 | .nvram56 = 1, |
| 112 | }, { |
| 113 | .name = "ds1337", |
| 114 | .type = ds_1337, |
| 115 | .alarm = 1, |
| 116 | }, { |
| 117 | .name = "ds1338", |
| 118 | .type = ds_1338, |
| 119 | .nvram56 = 1, |
| 120 | }, { |
| 121 | .name = "ds1339", |
| 122 | .type = ds_1339, |
| 123 | .alarm = 1, |
| 124 | }, { |
| 125 | .name = "ds1340", |
| 126 | .type = ds_1340, |
| 127 | }, { |
| 128 | .name = "m41t00", |
| 129 | .type = m41t00, |
| 130 | }, }; |
| 131 | |
| 132 | static inline const struct chip_desc *find_chip(const char *s) |
| 133 | { |
| 134 | unsigned i; |
| 135 | |
| 136 | for (i = 0; i < ARRAY_SIZE(chips); i++) |
| 137 | if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0) |
| 138 | return &chips[i]; |
| 139 | return NULL; |
| 140 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 141 | |
| 142 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) |
| 143 | { |
| 144 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 145 | int tmp; |
| 146 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 147 | /* read the RTC date and time registers all at once */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 148 | ds1307->msg[1].flags = I2C_M_RD; |
| 149 | ds1307->msg[1].len = 7; |
| 150 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 151 | tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent), |
| 152 | ds1307->msg, 2); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 153 | if (tmp != 2) { |
| 154 | dev_err(dev, "%s error %d\n", "read", tmp); |
| 155 | return -EIO; |
| 156 | } |
| 157 | |
| 158 | dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", |
| 159 | "read", |
| 160 | ds1307->regs[0], ds1307->regs[1], |
| 161 | ds1307->regs[2], ds1307->regs[3], |
| 162 | ds1307->regs[4], ds1307->regs[5], |
| 163 | ds1307->regs[6]); |
| 164 | |
| 165 | t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f); |
| 166 | t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); |
| 167 | tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f; |
| 168 | t->tm_hour = BCD2BIN(tmp); |
| 169 | t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1; |
| 170 | t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); |
| 171 | tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f; |
| 172 | t->tm_mon = BCD2BIN(tmp) - 1; |
| 173 | |
| 174 | /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */ |
| 175 | t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100; |
| 176 | |
| 177 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 178 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 179 | "read", t->tm_sec, t->tm_min, |
| 180 | t->tm_hour, t->tm_mday, |
| 181 | t->tm_mon, t->tm_year, t->tm_wday); |
| 182 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 183 | /* initial clock setting can be undefined */ |
| 184 | return rtc_valid_tm(t); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) |
| 188 | { |
| 189 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 190 | int result; |
| 191 | int tmp; |
| 192 | u8 *buf = ds1307->regs; |
| 193 | |
| 194 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 195 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
Jeff Garzik | 11966ad | 2006-10-04 04:41:53 -0400 | [diff] [blame] | 196 | "write", t->tm_sec, t->tm_min, |
| 197 | t->tm_hour, t->tm_mday, |
| 198 | t->tm_mon, t->tm_year, t->tm_wday); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 199 | |
| 200 | *buf++ = 0; /* first register addr */ |
| 201 | buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec); |
| 202 | buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min); |
| 203 | buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour); |
| 204 | buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1); |
| 205 | buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday); |
| 206 | buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1); |
| 207 | |
| 208 | /* assume 20YY not 19YY */ |
| 209 | tmp = t->tm_year - 100; |
| 210 | buf[DS1307_REG_YEAR] = BIN2BCD(tmp); |
| 211 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 212 | switch (ds1307->type) { |
| 213 | case ds_1337: |
| 214 | case ds_1339: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 215 | buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 216 | break; |
| 217 | case ds_1340: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 218 | buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN |
| 219 | | DS1340_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 224 | |
| 225 | ds1307->msg[1].flags = 0; |
| 226 | ds1307->msg[1].len = 8; |
| 227 | |
| 228 | dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", |
| 229 | "write", buf[0], buf[1], buf[2], buf[3], |
| 230 | buf[4], buf[5], buf[6]); |
| 231 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 232 | result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent), |
| 233 | &ds1307->msg[1], 1); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 234 | if (result != 1) { |
| 235 | dev_err(dev, "%s error %d\n", "write", tmp); |
| 236 | return -EIO; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 241 | static const struct rtc_class_ops ds13xx_rtc_ops = { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 242 | .read_time = ds1307_get_time, |
| 243 | .set_time = ds1307_set_time, |
| 244 | }; |
| 245 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 246 | /*----------------------------------------------------------------------*/ |
| 247 | |
| 248 | #define NVRAM_SIZE 56 |
| 249 | |
| 250 | static ssize_t |
| 251 | ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr, |
| 252 | char *buf, loff_t off, size_t count) |
| 253 | { |
| 254 | struct i2c_client *client; |
| 255 | struct ds1307 *ds1307; |
| 256 | struct i2c_msg msg[2]; |
| 257 | int result; |
| 258 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 259 | client = kobj_to_i2c_client(kobj); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 260 | ds1307 = i2c_get_clientdata(client); |
| 261 | |
| 262 | if (unlikely(off >= NVRAM_SIZE)) |
| 263 | return 0; |
| 264 | if ((off + count) > NVRAM_SIZE) |
| 265 | count = NVRAM_SIZE - off; |
| 266 | if (unlikely(!count)) |
| 267 | return count; |
| 268 | |
| 269 | msg[0].addr = client->addr; |
| 270 | msg[0].flags = 0; |
| 271 | msg[0].len = 1; |
| 272 | msg[0].buf = buf; |
| 273 | |
| 274 | buf[0] = 8 + off; |
| 275 | |
| 276 | msg[1].addr = client->addr; |
| 277 | msg[1].flags = I2C_M_RD; |
| 278 | msg[1].len = count; |
| 279 | msg[1].buf = buf; |
| 280 | |
| 281 | result = i2c_transfer(to_i2c_adapter(client->dev.parent), msg, 2); |
| 282 | if (result != 2) { |
| 283 | dev_err(&client->dev, "%s error %d\n", "nvram read", result); |
| 284 | return -EIO; |
| 285 | } |
| 286 | return count; |
| 287 | } |
| 288 | |
| 289 | static ssize_t |
| 290 | ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr, |
| 291 | char *buf, loff_t off, size_t count) |
| 292 | { |
| 293 | struct i2c_client *client; |
| 294 | u8 buffer[NVRAM_SIZE + 1]; |
| 295 | int ret; |
| 296 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 297 | client = kobj_to_i2c_client(kobj); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 298 | |
| 299 | if (unlikely(off >= NVRAM_SIZE)) |
| 300 | return -EFBIG; |
| 301 | if ((off + count) > NVRAM_SIZE) |
| 302 | count = NVRAM_SIZE - off; |
| 303 | if (unlikely(!count)) |
| 304 | return count; |
| 305 | |
| 306 | buffer[0] = 8 + off; |
| 307 | memcpy(buffer + 1, buf, count); |
| 308 | |
| 309 | ret = i2c_master_send(client, buffer, count + 1); |
| 310 | return (ret < 0) ? ret : (ret - 1); |
| 311 | } |
| 312 | |
| 313 | static struct bin_attribute nvram = { |
| 314 | .attr = { |
| 315 | .name = "nvram", |
| 316 | .mode = S_IRUGO | S_IWUSR, |
| 317 | .owner = THIS_MODULE, |
| 318 | }, |
| 319 | |
| 320 | .read = ds1307_nvram_read, |
| 321 | .write = ds1307_nvram_write, |
| 322 | .size = NVRAM_SIZE, |
| 323 | }; |
| 324 | |
| 325 | /*----------------------------------------------------------------------*/ |
| 326 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 327 | static struct i2c_driver ds1307_driver; |
| 328 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame^] | 329 | static int __devinit ds1307_probe(struct i2c_client *client, |
| 330 | const struct i2c_device_id *id) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 331 | { |
| 332 | struct ds1307 *ds1307; |
| 333 | int err = -ENODEV; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 334 | int tmp; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 335 | const struct chip_desc *chip; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 336 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 337 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 338 | chip = find_chip(client->name); |
| 339 | if (!chip) { |
| 340 | dev_err(&client->dev, "unknown chip type '%s'\n", |
| 341 | client->name); |
| 342 | return -ENODEV; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 345 | if (!i2c_check_functionality(adapter, |
| 346 | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 347 | return -EIO; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 348 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 349 | if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) |
| 350 | return -ENOMEM; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 351 | |
| 352 | ds1307->client = client; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 353 | i2c_set_clientdata(client, ds1307); |
| 354 | |
| 355 | ds1307->msg[0].addr = client->addr; |
| 356 | ds1307->msg[0].flags = 0; |
| 357 | ds1307->msg[0].len = 1; |
| 358 | ds1307->msg[0].buf = &ds1307->reg_addr; |
| 359 | |
| 360 | ds1307->msg[1].addr = client->addr; |
| 361 | ds1307->msg[1].flags = I2C_M_RD; |
| 362 | ds1307->msg[1].len = sizeof(ds1307->regs); |
| 363 | ds1307->msg[1].buf = ds1307->regs; |
| 364 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 365 | ds1307->type = chip->type; |
| 366 | |
| 367 | switch (ds1307->type) { |
| 368 | case ds_1337: |
| 369 | case ds_1339: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 370 | ds1307->reg_addr = DS1337_REG_CONTROL; |
| 371 | ds1307->msg[1].len = 2; |
| 372 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 373 | /* get registers that the "rtc" read below won't read... */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 374 | tmp = i2c_transfer(adapter, ds1307->msg, 2); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 375 | if (tmp != 2) { |
| 376 | pr_debug("read error %d\n", tmp); |
| 377 | err = -EIO; |
| 378 | goto exit_free; |
| 379 | } |
| 380 | |
| 381 | ds1307->reg_addr = 0; |
| 382 | ds1307->msg[1].len = sizeof(ds1307->regs); |
| 383 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 384 | /* oscillator off? turn it on, so clock can tick. */ |
| 385 | if (ds1307->regs[0] & DS1337_BIT_nEOSC) |
| 386 | i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, |
| 387 | ds1307->regs[0] & ~DS1337_BIT_nEOSC); |
| 388 | |
| 389 | /* oscillator fault? clear flag, and warn */ |
| 390 | if (ds1307->regs[1] & DS1337_BIT_OSF) { |
| 391 | i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, |
| 392 | ds1307->regs[1] & ~DS1337_BIT_OSF); |
| 393 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 394 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 395 | break; |
| 396 | default: |
| 397 | break; |
| 398 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 399 | |
| 400 | read_rtc: |
| 401 | /* read RTC registers */ |
| 402 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 403 | tmp = i2c_transfer(adapter, ds1307->msg, 2); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 404 | if (tmp != 2) { |
| 405 | pr_debug("read error %d\n", tmp); |
| 406 | err = -EIO; |
| 407 | goto exit_free; |
| 408 | } |
| 409 | |
| 410 | /* minimal sanity checking; some chips (like DS1340) don't |
| 411 | * specify the extra bits as must-be-zero, but there are |
| 412 | * still a few values that are clearly out-of-range. |
| 413 | */ |
| 414 | tmp = ds1307->regs[DS1307_REG_SECS]; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 415 | switch (ds1307->type) { |
| 416 | case ds_1307: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 417 | case m41t00: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 418 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 419 | if (tmp & DS1307_BIT_CH) { |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 420 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 421 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 422 | goto read_rtc; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 423 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 424 | break; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 425 | case ds_1338: |
| 426 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 427 | if (tmp & DS1307_BIT_CH) |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 428 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 429 | |
| 430 | /* oscillator fault? clear flag, and warn */ |
| 431 | if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { |
| 432 | i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL, |
David Brownell | bd16f9e | 2007-07-26 10:41:00 -0700 | [diff] [blame] | 433 | ds1307->regs[DS1307_REG_CONTROL] |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 434 | & ~DS1338_BIT_OSF); |
| 435 | dev_warn(&client->dev, "SET TIME!\n"); |
| 436 | goto read_rtc; |
| 437 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 438 | break; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 439 | case ds_1340: |
| 440 | /* clock halted? turn it on, so clock can tick. */ |
| 441 | if (tmp & DS1340_BIT_nEOSC) |
| 442 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 443 | |
| 444 | tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG); |
| 445 | if (tmp < 0) { |
| 446 | pr_debug("read error %d\n", tmp); |
| 447 | err = -EIO; |
| 448 | goto exit_free; |
| 449 | } |
| 450 | |
| 451 | /* oscillator fault? clear flag, and warn */ |
| 452 | if (tmp & DS1340_BIT_OSF) { |
| 453 | i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0); |
| 454 | dev_warn(&client->dev, "SET TIME!\n"); |
| 455 | } |
| 456 | break; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 457 | case ds_1337: |
| 458 | case ds_1339: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 459 | break; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 460 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 461 | |
| 462 | tmp = ds1307->regs[DS1307_REG_SECS]; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 463 | tmp = BCD2BIN(tmp & 0x7f); |
| 464 | if (tmp > 60) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 465 | goto exit_bad; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 466 | tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); |
| 467 | if (tmp > 60) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 468 | goto exit_bad; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 469 | |
| 470 | tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); |
| 471 | if (tmp == 0 || tmp > 31) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 472 | goto exit_bad; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 473 | |
| 474 | tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f); |
| 475 | if (tmp == 0 || tmp > 12) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 476 | goto exit_bad; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 477 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 478 | tmp = ds1307->regs[DS1307_REG_HOUR]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 479 | switch (ds1307->type) { |
| 480 | case ds_1340: |
| 481 | case m41t00: |
| 482 | /* NOTE: ignores century bits; fix before deploying |
| 483 | * systems that will run through year 2100. |
| 484 | */ |
| 485 | break; |
| 486 | default: |
| 487 | if (!(tmp & DS1307_BIT_12HR)) |
| 488 | break; |
| 489 | |
| 490 | /* Be sure we're in 24 hour mode. Multi-master systems |
| 491 | * take note... |
| 492 | */ |
| 493 | tmp = BCD2BIN(tmp & 0x1f); |
| 494 | if (tmp == 12) |
| 495 | tmp = 0; |
| 496 | if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
| 497 | tmp += 12; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 498 | i2c_smbus_write_byte_data(client, |
| 499 | DS1307_REG_HOUR, |
| 500 | BIN2BCD(tmp)); |
| 501 | } |
| 502 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 503 | ds1307->rtc = rtc_device_register(client->name, &client->dev, |
| 504 | &ds13xx_rtc_ops, THIS_MODULE); |
| 505 | if (IS_ERR(ds1307->rtc)) { |
| 506 | err = PTR_ERR(ds1307->rtc); |
| 507 | dev_err(&client->dev, |
| 508 | "unable to register the class device\n"); |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 509 | goto exit_free; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 510 | } |
| 511 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 512 | if (chip->nvram56) { |
| 513 | err = sysfs_create_bin_file(&client->dev.kobj, &nvram); |
| 514 | if (err == 0) { |
| 515 | ds1307->has_nvram = true; |
| 516 | dev_info(&client->dev, "56 bytes nvram\n"); |
| 517 | } |
| 518 | } |
| 519 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 522 | exit_bad: |
| 523 | dev_dbg(&client->dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", |
| 524 | "bogus register", |
| 525 | ds1307->regs[0], ds1307->regs[1], |
| 526 | ds1307->regs[2], ds1307->regs[3], |
| 527 | ds1307->regs[4], ds1307->regs[5], |
| 528 | ds1307->regs[6]); |
| 529 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 530 | exit_free: |
| 531 | kfree(ds1307); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 532 | return err; |
| 533 | } |
| 534 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 535 | static int __devexit ds1307_remove(struct i2c_client *client) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 536 | { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 537 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 538 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 539 | if (ds1307->has_nvram) |
| 540 | sysfs_remove_bin_file(&client->dev.kobj, &nvram); |
| 541 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 542 | rtc_device_unregister(ds1307->rtc); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 543 | kfree(ds1307); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static struct i2c_driver ds1307_driver = { |
| 548 | .driver = { |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 549 | .name = "rtc-ds1307", |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 550 | .owner = THIS_MODULE, |
| 551 | }, |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 552 | .probe = ds1307_probe, |
| 553 | .remove = __devexit_p(ds1307_remove), |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | static int __init ds1307_init(void) |
| 557 | { |
| 558 | return i2c_add_driver(&ds1307_driver); |
| 559 | } |
| 560 | module_init(ds1307_init); |
| 561 | |
| 562 | static void __exit ds1307_exit(void) |
| 563 | { |
| 564 | i2c_del_driver(&ds1307_driver); |
| 565 | } |
| 566 | module_exit(ds1307_exit); |
| 567 | |
| 568 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); |
| 569 | MODULE_LICENSE("GPL"); |