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