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. |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 26 | */ |
| 27 | enum ds_type { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 28 | ds_1307, |
| 29 | ds_1337, |
| 30 | ds_1338, |
| 31 | ds_1339, |
| 32 | ds_1340, |
| 33 | m41t00, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 34 | // rs5c372 too? different address... |
| 35 | }; |
| 36 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 37 | |
| 38 | /* RTC registers don't differ much, except for the century flag */ |
| 39 | #define DS1307_REG_SECS 0x00 /* 00-59 */ |
| 40 | # define DS1307_BIT_CH 0x80 |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 41 | # define DS1340_BIT_nEOSC 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 42 | #define DS1307_REG_MIN 0x01 /* 00-59 */ |
| 43 | #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */ |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 44 | # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */ |
| 45 | # define DS1307_BIT_PM 0x20 /* in REG_HOUR */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 46 | # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */ |
| 47 | # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */ |
| 48 | #define DS1307_REG_WDAY 0x03 /* 01-07 */ |
| 49 | #define DS1307_REG_MDAY 0x04 /* 01-31 */ |
| 50 | #define DS1307_REG_MONTH 0x05 /* 01-12 */ |
| 51 | # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */ |
| 52 | #define DS1307_REG_YEAR 0x06 /* 00-99 */ |
| 53 | |
| 54 | /* Other registers (control, status, alarms, trickle charge, NVRAM, etc) |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 55 | * start at 7, and they differ a LOT. Only control and status matter for |
| 56 | * basic RTC date and time functionality; be careful using them. |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 57 | */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 58 | #define DS1307_REG_CONTROL 0x07 /* or ds1338 */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 59 | # define DS1307_BIT_OUT 0x80 |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 60 | # define DS1338_BIT_OSF 0x20 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 61 | # define DS1307_BIT_SQWE 0x10 |
| 62 | # define DS1307_BIT_RS1 0x02 |
| 63 | # define DS1307_BIT_RS0 0x01 |
| 64 | #define DS1337_REG_CONTROL 0x0e |
| 65 | # define DS1337_BIT_nEOSC 0x80 |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 66 | # define DS1339_BIT_BBSQI 0x20 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 67 | # define DS1337_BIT_RS2 0x10 |
| 68 | # define DS1337_BIT_RS1 0x08 |
| 69 | # define DS1337_BIT_INTCN 0x04 |
| 70 | # define DS1337_BIT_A2IE 0x02 |
| 71 | # define DS1337_BIT_A1IE 0x01 |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 72 | #define DS1340_REG_CONTROL 0x07 |
| 73 | # define DS1340_BIT_OUT 0x80 |
| 74 | # define DS1340_BIT_FT 0x40 |
| 75 | # define DS1340_BIT_CALIB_SIGN 0x20 |
| 76 | # define DS1340_M_CALIBRATION 0x1f |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 77 | #define DS1340_REG_FLAG 0x09 |
| 78 | # define DS1340_BIT_OSF 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 79 | #define DS1337_REG_STATUS 0x0f |
| 80 | # define DS1337_BIT_OSF 0x80 |
| 81 | # define DS1337_BIT_A2I 0x02 |
| 82 | # define DS1337_BIT_A1I 0x01 |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 83 | #define DS1339_REG_ALARM1_SECS 0x07 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 84 | #define DS1339_REG_TRICKLE 0x10 |
| 85 | |
| 86 | |
| 87 | |
| 88 | struct ds1307 { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 89 | u8 regs[11]; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 90 | enum ds_type type; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 91 | unsigned long flags; |
| 92 | #define HAS_NVRAM 0 /* bit 0 == sysfs file active */ |
| 93 | #define HAS_ALARM 1 /* bit 1 == irq claimed */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 94 | struct i2c_client *client; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 95 | struct rtc_device *rtc; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 96 | struct work_struct work; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 97 | s32 (*read_block_data)(struct i2c_client *client, u8 command, |
| 98 | u8 length, u8 *values); |
| 99 | s32 (*write_block_data)(struct i2c_client *client, u8 command, |
| 100 | u8 length, const u8 *values); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 103 | struct chip_desc { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 104 | unsigned nvram56:1; |
| 105 | unsigned alarm:1; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 108 | static const struct chip_desc chips[] = { |
| 109 | [ds_1307] = { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 110 | .nvram56 = 1, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 111 | }, |
| 112 | [ds_1337] = { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 113 | .alarm = 1, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 114 | }, |
| 115 | [ds_1338] = { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 116 | .nvram56 = 1, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 117 | }, |
| 118 | [ds_1339] = { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 119 | .alarm = 1, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 120 | }, |
| 121 | [ds_1340] = { |
| 122 | }, |
| 123 | [m41t00] = { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 124 | }, }; |
| 125 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 126 | static const struct i2c_device_id ds1307_id[] = { |
| 127 | { "ds1307", ds_1307 }, |
| 128 | { "ds1337", ds_1337 }, |
| 129 | { "ds1338", ds_1338 }, |
| 130 | { "ds1339", ds_1339 }, |
| 131 | { "ds1340", ds_1340 }, |
| 132 | { "m41t00", m41t00 }, |
| 133 | { } |
| 134 | }; |
| 135 | MODULE_DEVICE_TABLE(i2c, ds1307_id); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 136 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 137 | /*----------------------------------------------------------------------*/ |
| 138 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 139 | #define BLOCK_DATA_MAX_TRIES 10 |
| 140 | |
| 141 | static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command, |
| 142 | u8 length, u8 *values) |
| 143 | { |
| 144 | s32 i, data; |
| 145 | |
| 146 | for (i = 0; i < length; i++) { |
| 147 | data = i2c_smbus_read_byte_data(client, command + i); |
| 148 | if (data < 0) |
| 149 | return data; |
| 150 | values[i] = data; |
| 151 | } |
| 152 | return i; |
| 153 | } |
| 154 | |
| 155 | static s32 ds1307_read_block_data(struct i2c_client *client, u8 command, |
| 156 | u8 length, u8 *values) |
| 157 | { |
| 158 | u8 oldvalues[I2C_SMBUS_BLOCK_MAX]; |
| 159 | s32 ret; |
| 160 | int tries = 0; |
| 161 | |
| 162 | dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length); |
| 163 | ret = ds1307_read_block_data_once(client, command, length, values); |
| 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | do { |
| 167 | if (++tries > BLOCK_DATA_MAX_TRIES) { |
| 168 | dev_err(&client->dev, |
| 169 | "ds1307_read_block_data failed\n"); |
| 170 | return -EIO; |
| 171 | } |
| 172 | memcpy(oldvalues, values, length); |
| 173 | ret = ds1307_read_block_data_once(client, command, length, |
| 174 | values); |
| 175 | if (ret < 0) |
| 176 | return ret; |
| 177 | } while (memcmp(oldvalues, values, length)); |
| 178 | return length; |
| 179 | } |
| 180 | |
| 181 | static s32 ds1307_write_block_data(struct i2c_client *client, u8 command, |
| 182 | u8 length, const u8 *values) |
| 183 | { |
| 184 | u8 currvalues[I2C_SMBUS_BLOCK_MAX]; |
| 185 | int tries = 0; |
| 186 | |
| 187 | dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length); |
| 188 | do { |
| 189 | s32 i, ret; |
| 190 | |
| 191 | if (++tries > BLOCK_DATA_MAX_TRIES) { |
| 192 | dev_err(&client->dev, |
| 193 | "ds1307_write_block_data failed\n"); |
| 194 | return -EIO; |
| 195 | } |
| 196 | for (i = 0; i < length; i++) { |
| 197 | ret = i2c_smbus_write_byte_data(client, command + i, |
| 198 | values[i]); |
| 199 | if (ret < 0) |
| 200 | return ret; |
| 201 | } |
| 202 | ret = ds1307_read_block_data_once(client, command, length, |
| 203 | currvalues); |
| 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | } while (memcmp(currvalues, values, length)); |
| 207 | return length; |
| 208 | } |
| 209 | |
| 210 | /*----------------------------------------------------------------------*/ |
| 211 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 212 | /* |
| 213 | * The IRQ logic includes a "real" handler running in IRQ context just |
| 214 | * long enough to schedule this workqueue entry. We need a task context |
| 215 | * to talk to the RTC, since I2C I/O calls require that; and disable the |
| 216 | * IRQ until we clear its status on the chip, so that this handler can |
| 217 | * work with any type of triggering (not just falling edge). |
| 218 | * |
| 219 | * The ds1337 and ds1339 both have two alarms, but we only use the first |
| 220 | * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm |
| 221 | * signal; ds1339 chips have only one alarm signal. |
| 222 | */ |
| 223 | static void ds1307_work(struct work_struct *work) |
| 224 | { |
| 225 | struct ds1307 *ds1307; |
| 226 | struct i2c_client *client; |
| 227 | struct mutex *lock; |
| 228 | int stat, control; |
| 229 | |
| 230 | ds1307 = container_of(work, struct ds1307, work); |
| 231 | client = ds1307->client; |
| 232 | lock = &ds1307->rtc->ops_lock; |
| 233 | |
| 234 | mutex_lock(lock); |
| 235 | stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS); |
| 236 | if (stat < 0) |
| 237 | goto out; |
| 238 | |
| 239 | if (stat & DS1337_BIT_A1I) { |
| 240 | stat &= ~DS1337_BIT_A1I; |
| 241 | i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat); |
| 242 | |
| 243 | control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); |
| 244 | if (control < 0) |
| 245 | goto out; |
| 246 | |
| 247 | control &= ~DS1337_BIT_A1IE; |
| 248 | i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control); |
| 249 | |
| 250 | /* rtc_update_irq() assumes that it is called |
| 251 | * from IRQ-disabled context. |
| 252 | */ |
| 253 | local_irq_disable(); |
| 254 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
| 255 | local_irq_enable(); |
| 256 | } |
| 257 | |
| 258 | out: |
| 259 | if (test_bit(HAS_ALARM, &ds1307->flags)) |
| 260 | enable_irq(client->irq); |
| 261 | mutex_unlock(lock); |
| 262 | } |
| 263 | |
| 264 | static irqreturn_t ds1307_irq(int irq, void *dev_id) |
| 265 | { |
| 266 | struct i2c_client *client = dev_id; |
| 267 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 268 | |
| 269 | disable_irq_nosync(irq); |
| 270 | schedule_work(&ds1307->work); |
| 271 | return IRQ_HANDLED; |
| 272 | } |
| 273 | |
| 274 | /*----------------------------------------------------------------------*/ |
| 275 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 276 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) |
| 277 | { |
| 278 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 279 | int tmp; |
| 280 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 281 | /* read the RTC date and time registers all at once */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 282 | tmp = ds1307->read_block_data(ds1307->client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 283 | DS1307_REG_SECS, 7, ds1307->regs); |
| 284 | if (tmp != 7) { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 285 | dev_err(dev, "%s error %d\n", "read", tmp); |
| 286 | return -EIO; |
| 287 | } |
| 288 | |
| 289 | dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", |
| 290 | "read", |
| 291 | ds1307->regs[0], ds1307->regs[1], |
| 292 | ds1307->regs[2], ds1307->regs[3], |
| 293 | ds1307->regs[4], ds1307->regs[5], |
| 294 | ds1307->regs[6]); |
| 295 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 296 | t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f); |
| 297 | t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 298 | tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 299 | t->tm_hour = bcd2bin(tmp); |
| 300 | t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1; |
| 301 | t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 302 | tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 303 | t->tm_mon = bcd2bin(tmp) - 1; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 304 | |
| 305 | /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 306 | t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 307 | |
| 308 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 309 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 310 | "read", t->tm_sec, t->tm_min, |
| 311 | t->tm_hour, t->tm_mday, |
| 312 | t->tm_mon, t->tm_year, t->tm_wday); |
| 313 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 314 | /* initial clock setting can be undefined */ |
| 315 | return rtc_valid_tm(t); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) |
| 319 | { |
| 320 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 321 | int result; |
| 322 | int tmp; |
| 323 | u8 *buf = ds1307->regs; |
| 324 | |
| 325 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 326 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
Jeff Garzik | 11966ad | 2006-10-04 04:41:53 -0400 | [diff] [blame] | 327 | "write", t->tm_sec, t->tm_min, |
| 328 | t->tm_hour, t->tm_mday, |
| 329 | t->tm_mon, t->tm_year, t->tm_wday); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 330 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 331 | buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec); |
| 332 | buf[DS1307_REG_MIN] = bin2bcd(t->tm_min); |
| 333 | buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour); |
| 334 | buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1); |
| 335 | buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday); |
| 336 | buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 337 | |
| 338 | /* assume 20YY not 19YY */ |
| 339 | tmp = t->tm_year - 100; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 340 | buf[DS1307_REG_YEAR] = bin2bcd(tmp); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 341 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 342 | switch (ds1307->type) { |
| 343 | case ds_1337: |
| 344 | case ds_1339: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 345 | buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 346 | break; |
| 347 | case ds_1340: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 348 | buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN |
| 349 | | DS1340_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 350 | break; |
| 351 | default: |
| 352 | break; |
| 353 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 354 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 355 | dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", |
| 356 | "write", buf[0], buf[1], buf[2], buf[3], |
| 357 | buf[4], buf[5], buf[6]); |
| 358 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 359 | result = ds1307->write_block_data(ds1307->client, 0, 7, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 360 | if (result < 0) { |
| 361 | dev_err(dev, "%s error %d\n", "write", result); |
| 362 | return result; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 367 | static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 368 | { |
| 369 | struct i2c_client *client = to_i2c_client(dev); |
| 370 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 371 | int ret; |
| 372 | |
| 373 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | /* read all ALARM1, ALARM2, and status registers at once */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 377 | ret = ds1307->read_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 378 | DS1339_REG_ALARM1_SECS, 9, ds1307->regs); |
| 379 | if (ret != 9) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 380 | dev_err(dev, "%s error %d\n", "alarm read", ret); |
| 381 | return -EIO; |
| 382 | } |
| 383 | |
| 384 | dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n", |
| 385 | "alarm read", |
| 386 | ds1307->regs[0], ds1307->regs[1], |
| 387 | ds1307->regs[2], ds1307->regs[3], |
| 388 | ds1307->regs[4], ds1307->regs[5], |
| 389 | ds1307->regs[6], ds1307->regs[7], |
| 390 | ds1307->regs[8]); |
| 391 | |
| 392 | /* report alarm time (ALARM1); assume 24 hour and day-of-month modes, |
| 393 | * and that all four fields are checked matches |
| 394 | */ |
| 395 | t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f); |
| 396 | t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f); |
| 397 | t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f); |
| 398 | t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f); |
| 399 | t->time.tm_mon = -1; |
| 400 | t->time.tm_year = -1; |
| 401 | t->time.tm_wday = -1; |
| 402 | t->time.tm_yday = -1; |
| 403 | t->time.tm_isdst = -1; |
| 404 | |
| 405 | /* ... and status */ |
| 406 | t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE); |
| 407 | t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I); |
| 408 | |
| 409 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 410 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 411 | "alarm read", t->time.tm_sec, t->time.tm_min, |
| 412 | t->time.tm_hour, t->time.tm_mday, |
| 413 | t->enabled, t->pending); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 418 | static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 419 | { |
| 420 | struct i2c_client *client = to_i2c_client(dev); |
| 421 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 422 | unsigned char *buf = ds1307->regs; |
| 423 | u8 control, status; |
| 424 | int ret; |
| 425 | |
| 426 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 430 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 431 | "alarm set", t->time.tm_sec, t->time.tm_min, |
| 432 | t->time.tm_hour, t->time.tm_mday, |
| 433 | t->enabled, t->pending); |
| 434 | |
| 435 | /* read current status of both alarms and the chip */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 436 | ret = ds1307->read_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 437 | DS1339_REG_ALARM1_SECS, 9, buf); |
| 438 | if (ret != 9) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 439 | dev_err(dev, "%s error %d\n", "alarm write", ret); |
| 440 | return -EIO; |
| 441 | } |
| 442 | control = ds1307->regs[7]; |
| 443 | status = ds1307->regs[8]; |
| 444 | |
| 445 | dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n", |
| 446 | "alarm set (old status)", |
| 447 | ds1307->regs[0], ds1307->regs[1], |
| 448 | ds1307->regs[2], ds1307->regs[3], |
| 449 | ds1307->regs[4], ds1307->regs[5], |
| 450 | ds1307->regs[6], control, status); |
| 451 | |
| 452 | /* set ALARM1, using 24 hour and day-of-month modes */ |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 453 | buf[0] = bin2bcd(t->time.tm_sec); |
| 454 | buf[1] = bin2bcd(t->time.tm_min); |
| 455 | buf[2] = bin2bcd(t->time.tm_hour); |
| 456 | buf[3] = bin2bcd(t->time.tm_mday); |
| 457 | |
| 458 | /* set ALARM2 to non-garbage */ |
| 459 | buf[4] = 0; |
| 460 | buf[5] = 0; |
| 461 | buf[6] = 0; |
| 462 | |
| 463 | /* optionally enable ALARM1 */ |
| 464 | buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE); |
| 465 | if (t->enabled) { |
| 466 | dev_dbg(dev, "alarm IRQ armed\n"); |
| 467 | buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */ |
| 468 | } |
| 469 | buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I); |
| 470 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 471 | ret = ds1307->write_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 472 | DS1339_REG_ALARM1_SECS, 9, buf); |
| 473 | if (ret < 0) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 474 | dev_err(dev, "can't set alarm time\n"); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 475 | return ret; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 482 | { |
| 483 | struct i2c_client *client = to_i2c_client(dev); |
| 484 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 485 | int ret; |
| 486 | |
| 487 | switch (cmd) { |
| 488 | case RTC_AIE_OFF: |
| 489 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 490 | return -ENOTTY; |
| 491 | |
| 492 | ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); |
| 493 | if (ret < 0) |
| 494 | return ret; |
| 495 | |
| 496 | ret &= ~DS1337_BIT_A1IE; |
| 497 | |
| 498 | ret = i2c_smbus_write_byte_data(client, |
| 499 | DS1337_REG_CONTROL, ret); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | |
| 503 | break; |
| 504 | |
| 505 | case RTC_AIE_ON: |
| 506 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 507 | return -ENOTTY; |
| 508 | |
| 509 | ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); |
| 510 | if (ret < 0) |
| 511 | return ret; |
| 512 | |
| 513 | ret |= DS1337_BIT_A1IE; |
| 514 | |
| 515 | ret = i2c_smbus_write_byte_data(client, |
| 516 | DS1337_REG_CONTROL, ret); |
| 517 | if (ret < 0) |
| 518 | return ret; |
| 519 | |
| 520 | break; |
| 521 | |
| 522 | default: |
| 523 | return -ENOIOCTLCMD; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 529 | static const struct rtc_class_ops ds13xx_rtc_ops = { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 530 | .read_time = ds1307_get_time, |
| 531 | .set_time = ds1307_set_time, |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 532 | .read_alarm = ds1337_read_alarm, |
| 533 | .set_alarm = ds1337_set_alarm, |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 534 | .ioctl = ds1307_ioctl, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 537 | /*----------------------------------------------------------------------*/ |
| 538 | |
| 539 | #define NVRAM_SIZE 56 |
| 540 | |
| 541 | static ssize_t |
| 542 | ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr, |
| 543 | char *buf, loff_t off, size_t count) |
| 544 | { |
| 545 | struct i2c_client *client; |
| 546 | struct ds1307 *ds1307; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 547 | int result; |
| 548 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 549 | client = kobj_to_i2c_client(kobj); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 550 | ds1307 = i2c_get_clientdata(client); |
| 551 | |
| 552 | if (unlikely(off >= NVRAM_SIZE)) |
| 553 | return 0; |
| 554 | if ((off + count) > NVRAM_SIZE) |
| 555 | count = NVRAM_SIZE - off; |
| 556 | if (unlikely(!count)) |
| 557 | return count; |
| 558 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 559 | result = ds1307->read_block_data(client, 8 + off, count, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 560 | if (result < 0) |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 561 | dev_err(&client->dev, "%s error %d\n", "nvram read", result); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 562 | return result; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static ssize_t |
| 566 | ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr, |
| 567 | char *buf, loff_t off, size_t count) |
| 568 | { |
| 569 | struct i2c_client *client; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 570 | struct ds1307 *ds1307; |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 571 | int result; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 572 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 573 | client = kobj_to_i2c_client(kobj); |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 574 | ds1307 = i2c_get_clientdata(client); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 575 | |
| 576 | if (unlikely(off >= NVRAM_SIZE)) |
| 577 | return -EFBIG; |
| 578 | if ((off + count) > NVRAM_SIZE) |
| 579 | count = NVRAM_SIZE - off; |
| 580 | if (unlikely(!count)) |
| 581 | return count; |
| 582 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 583 | result = ds1307->write_block_data(client, 8 + off, count, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 584 | if (result < 0) { |
| 585 | dev_err(&client->dev, "%s error %d\n", "nvram write", result); |
| 586 | return result; |
| 587 | } |
| 588 | return count; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static struct bin_attribute nvram = { |
| 592 | .attr = { |
| 593 | .name = "nvram", |
| 594 | .mode = S_IRUGO | S_IWUSR, |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 595 | }, |
| 596 | |
| 597 | .read = ds1307_nvram_read, |
| 598 | .write = ds1307_nvram_write, |
| 599 | .size = NVRAM_SIZE, |
| 600 | }; |
| 601 | |
| 602 | /*----------------------------------------------------------------------*/ |
| 603 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 604 | static struct i2c_driver ds1307_driver; |
| 605 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 606 | static int __devinit ds1307_probe(struct i2c_client *client, |
| 607 | const struct i2c_device_id *id) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 608 | { |
| 609 | struct ds1307 *ds1307; |
| 610 | int err = -ENODEV; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 611 | int tmp; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 612 | const struct chip_desc *chip = &chips[id->driver_data]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 613 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 614 | int want_irq = false; |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 615 | unsigned char *buf; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 616 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 617 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA) |
| 618 | && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 619 | return -EIO; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 620 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 621 | if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) |
| 622 | return -ENOMEM; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 623 | |
| 624 | ds1307->client = client; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 625 | i2c_set_clientdata(client, ds1307); |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 626 | ds1307->type = id->driver_data; |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 627 | buf = ds1307->regs; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 628 | if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 629 | ds1307->read_block_data = i2c_smbus_read_i2c_block_data; |
| 630 | ds1307->write_block_data = i2c_smbus_write_i2c_block_data; |
| 631 | } else { |
| 632 | ds1307->read_block_data = ds1307_read_block_data; |
| 633 | ds1307->write_block_data = ds1307_write_block_data; |
| 634 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 635 | |
| 636 | switch (ds1307->type) { |
| 637 | case ds_1337: |
| 638 | case ds_1339: |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 639 | /* has IRQ? */ |
| 640 | if (ds1307->client->irq > 0 && chip->alarm) { |
| 641 | INIT_WORK(&ds1307->work, ds1307_work); |
| 642 | want_irq = true; |
| 643 | } |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 644 | /* get registers that the "rtc" read below won't read... */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 645 | tmp = ds1307->read_block_data(ds1307->client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 646 | DS1337_REG_CONTROL, 2, buf); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 647 | if (tmp != 2) { |
| 648 | pr_debug("read error %d\n", tmp); |
| 649 | err = -EIO; |
| 650 | goto exit_free; |
| 651 | } |
| 652 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 653 | /* oscillator off? turn it on, so clock can tick. */ |
| 654 | if (ds1307->regs[0] & DS1337_BIT_nEOSC) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 655 | ds1307->regs[0] &= ~DS1337_BIT_nEOSC; |
| 656 | |
| 657 | /* Using IRQ? Disable the square wave and both alarms. |
| 658 | * For ds1339, be sure alarms can trigger when we're |
| 659 | * running on Vbackup (BBSQI); we assume ds1337 will |
| 660 | * ignore that bit |
| 661 | */ |
| 662 | if (want_irq) { |
| 663 | ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI; |
| 664 | ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE); |
| 665 | } |
| 666 | |
| 667 | i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, |
| 668 | ds1307->regs[0]); |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 669 | |
| 670 | /* oscillator fault? clear flag, and warn */ |
| 671 | if (ds1307->regs[1] & DS1337_BIT_OSF) { |
| 672 | i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, |
| 673 | ds1307->regs[1] & ~DS1337_BIT_OSF); |
| 674 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 675 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 676 | break; |
| 677 | default: |
| 678 | break; |
| 679 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 680 | |
| 681 | read_rtc: |
| 682 | /* read RTC registers */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame^] | 683 | tmp = ds1307->read_block_data(ds1307->client, 0, 8, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 684 | if (tmp != 8) { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 685 | pr_debug("read error %d\n", tmp); |
| 686 | err = -EIO; |
| 687 | goto exit_free; |
| 688 | } |
| 689 | |
| 690 | /* minimal sanity checking; some chips (like DS1340) don't |
| 691 | * specify the extra bits as must-be-zero, but there are |
| 692 | * still a few values that are clearly out-of-range. |
| 693 | */ |
| 694 | tmp = ds1307->regs[DS1307_REG_SECS]; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 695 | switch (ds1307->type) { |
| 696 | case ds_1307: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 697 | case m41t00: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 698 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 699 | if (tmp & DS1307_BIT_CH) { |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 700 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 701 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 702 | goto read_rtc; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 703 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 704 | break; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 705 | case ds_1338: |
| 706 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 707 | if (tmp & DS1307_BIT_CH) |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 708 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 709 | |
| 710 | /* oscillator fault? clear flag, and warn */ |
| 711 | if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { |
| 712 | i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL, |
David Brownell | bd16f9e | 2007-07-26 10:41:00 -0700 | [diff] [blame] | 713 | ds1307->regs[DS1307_REG_CONTROL] |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 714 | & ~DS1338_BIT_OSF); |
| 715 | dev_warn(&client->dev, "SET TIME!\n"); |
| 716 | goto read_rtc; |
| 717 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 718 | break; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 719 | case ds_1340: |
| 720 | /* clock halted? turn it on, so clock can tick. */ |
| 721 | if (tmp & DS1340_BIT_nEOSC) |
| 722 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 723 | |
| 724 | tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG); |
| 725 | if (tmp < 0) { |
| 726 | pr_debug("read error %d\n", tmp); |
| 727 | err = -EIO; |
| 728 | goto exit_free; |
| 729 | } |
| 730 | |
| 731 | /* oscillator fault? clear flag, and warn */ |
| 732 | if (tmp & DS1340_BIT_OSF) { |
| 733 | i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0); |
| 734 | dev_warn(&client->dev, "SET TIME!\n"); |
| 735 | } |
| 736 | break; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 737 | case ds_1337: |
| 738 | case ds_1339: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 739 | break; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 740 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 741 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 742 | tmp = ds1307->regs[DS1307_REG_HOUR]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 743 | switch (ds1307->type) { |
| 744 | case ds_1340: |
| 745 | case m41t00: |
| 746 | /* NOTE: ignores century bits; fix before deploying |
| 747 | * systems that will run through year 2100. |
| 748 | */ |
| 749 | break; |
| 750 | default: |
| 751 | if (!(tmp & DS1307_BIT_12HR)) |
| 752 | break; |
| 753 | |
| 754 | /* Be sure we're in 24 hour mode. Multi-master systems |
| 755 | * take note... |
| 756 | */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 757 | tmp = bcd2bin(tmp & 0x1f); |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 758 | if (tmp == 12) |
| 759 | tmp = 0; |
| 760 | if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
| 761 | tmp += 12; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 762 | i2c_smbus_write_byte_data(client, |
| 763 | DS1307_REG_HOUR, |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 764 | bin2bcd(tmp)); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 765 | } |
| 766 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 767 | ds1307->rtc = rtc_device_register(client->name, &client->dev, |
| 768 | &ds13xx_rtc_ops, THIS_MODULE); |
| 769 | if (IS_ERR(ds1307->rtc)) { |
| 770 | err = PTR_ERR(ds1307->rtc); |
| 771 | dev_err(&client->dev, |
| 772 | "unable to register the class device\n"); |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 773 | goto exit_free; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 776 | if (want_irq) { |
| 777 | err = request_irq(client->irq, ds1307_irq, 0, |
| 778 | ds1307->rtc->name, client); |
| 779 | if (err) { |
| 780 | dev_err(&client->dev, |
| 781 | "unable to request IRQ!\n"); |
| 782 | goto exit_irq; |
| 783 | } |
| 784 | set_bit(HAS_ALARM, &ds1307->flags); |
| 785 | dev_dbg(&client->dev, "got IRQ %d\n", client->irq); |
| 786 | } |
| 787 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 788 | if (chip->nvram56) { |
| 789 | err = sysfs_create_bin_file(&client->dev.kobj, &nvram); |
| 790 | if (err == 0) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 791 | set_bit(HAS_NVRAM, &ds1307->flags); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 792 | dev_info(&client->dev, "56 bytes nvram\n"); |
| 793 | } |
| 794 | } |
| 795 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 798 | exit_irq: |
| 799 | if (ds1307->rtc) |
| 800 | rtc_device_unregister(ds1307->rtc); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 801 | exit_free: |
| 802 | kfree(ds1307); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 803 | return err; |
| 804 | } |
| 805 | |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 806 | static int __devexit ds1307_remove(struct i2c_client *client) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 807 | { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 808 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 809 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 810 | if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) { |
| 811 | free_irq(client->irq, client); |
| 812 | cancel_work_sync(&ds1307->work); |
| 813 | } |
| 814 | |
| 815 | if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 816 | sysfs_remove_bin_file(&client->dev.kobj, &nvram); |
| 817 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 818 | rtc_device_unregister(ds1307->rtc); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 819 | kfree(ds1307); |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static struct i2c_driver ds1307_driver = { |
| 824 | .driver = { |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 825 | .name = "rtc-ds1307", |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 826 | .owner = THIS_MODULE, |
| 827 | }, |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 828 | .probe = ds1307_probe, |
| 829 | .remove = __devexit_p(ds1307_remove), |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 830 | .id_table = ds1307_id, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 831 | }; |
| 832 | |
| 833 | static int __init ds1307_init(void) |
| 834 | { |
| 835 | return i2c_add_driver(&ds1307_driver); |
| 836 | } |
| 837 | module_init(ds1307_init); |
| 838 | |
| 839 | static void __exit ds1307_exit(void) |
| 840 | { |
| 841 | i2c_del_driver(&ds1307_driver); |
| 842 | } |
| 843 | module_exit(ds1307_exit); |
| 844 | |
| 845 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); |
| 846 | MODULE_LICENSE("GPL"); |