blob: c293d0cdb10483502784653f8617d0f0ecb55562 [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
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 Fuchsa2166852009-03-31 15:24:58 -07006 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
David Brownell1abb0dc2006-06-25 05:48:17 -07007 *
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 Anders40ce9722012-03-23 15:02:37 -070023/*
24 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070025 * 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 Brownell1abb0dc2006-06-25 05:48:17 -070028 */
29enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070030 ds_1307,
31 ds_1337,
32 ds_1338,
33 ds_1339,
34 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070035 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070036 ds_3231,
David Brownell045e0e82007-07-17 04:04:55 -070037 m41t00,
David Anders43fcb812011-11-02 13:37:53 -070038 mcp7941x,
Matthias Fuchsa2166852009-03-31 15:24:58 -070039 rx_8025,
Wolfram Sang32d322b2012-03-23 15:02:36 -070040 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070041 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070042};
43
David Brownell1abb0dc2006-06-25 05:48:17 -070044
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 Giomettibe5f59f2007-07-17 04:05:06 -070048# define DS1340_BIT_nEOSC 0x80
David Anders43fcb812011-11-02 13:37:53 -070049# define MCP7941X_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070050#define DS1307_REG_MIN 0x01 /* 00-59 */
51#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070052# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
53# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070054# 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 Anders43fcb812011-11-02 13:37:53 -070057# define MCP7941X_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070058#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 Anders40ce9722012-03-23 15:02:37 -070063/*
64 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070065 * 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 Brownell1abb0dc2006-06-25 05:48:17 -070067 */
David Brownell045e0e82007-07-17 04:04:55 -070068#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070069# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070070# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070071# 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 Giometticb49a5e2008-10-15 22:02:58 -070076# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070077# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070078# 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 Brownell045e0e82007-07-17 04:04:55 -070083#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 Giomettibe5f59f2007-07-17 04:05:06 -070088#define DS1340_REG_FLAG 0x09
89# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070090#define DS1337_REG_STATUS 0x0f
91# define DS1337_BIT_OSF 0x80
92# define DS1337_BIT_A2I 0x02
93# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070094#define DS1339_REG_ALARM1_SECS 0x07
David Brownell1abb0dc2006-06-25 05:48:17 -070095#define DS1339_REG_TRICKLE 0x10
96
Matthias Fuchsa2166852009-03-31 15:24:58 -070097#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 Brownell1abb0dc2006-06-25 05:48:17 -0700103
104
105struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700106 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700107 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700108 u16 nvram_offset;
109 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700110 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700111 unsigned long flags;
112#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
113#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell045e0e82007-07-17 04:04:55 -0700114 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700115 struct rtc_device *rtc;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700116 struct work_struct work;
Jean Delvare0cc43a12011-01-10 22:11:23 +0100117 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700118 u8 length, u8 *values);
Jean Delvare0cc43a12011-01-10 22:11:23 +0100119 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700120 u8 length, const u8 *values);
David Brownell1abb0dc2006-06-25 05:48:17 -0700121};
122
David Brownell045e0e82007-07-17 04:04:55 -0700123struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700124 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700125 u16 nvram_offset;
126 u16 nvram_size;
David Brownell045e0e82007-07-17 04:04:55 -0700127};
128
Wolfram Sang32d322b2012-03-23 15:02:36 -0700129static const struct chip_desc chips[last_ds_type] = {
130 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700131 .nvram_offset = 8,
132 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700133 },
134 [ds_1337] = {
135 .alarm = 1,
136 },
137 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700138 .nvram_offset = 8,
139 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700140 },
141 [ds_1339] = {
142 .alarm = 1,
143 },
144 [ds_3231] = {
145 .alarm = 1,
146 },
Austin Boyle9eab0a72012-03-23 15:02:38 -0700147 [mcp7941x] = {
148 /* this is battery backed SRAM */
149 .nvram_offset = 0x20,
150 .nvram_size = 0x40,
151 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700152};
David Brownell045e0e82007-07-17 04:04:55 -0700153
Jean Delvare3760f732008-04-29 23:11:40 +0200154static const struct i2c_device_id ds1307_id[] = {
155 { "ds1307", ds_1307 },
156 { "ds1337", ds_1337 },
157 { "ds1338", ds_1338 },
158 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700159 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200160 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700161 { "ds3231", ds_3231 },
Jean Delvare3760f732008-04-29 23:11:40 +0200162 { "m41t00", m41t00 },
David Anders43fcb812011-11-02 13:37:53 -0700163 { "mcp7941x", mcp7941x },
Priyanka Jain31c17712011-06-27 16:18:04 -0700164 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700165 { "rx8025", rx_8025 },
Jean Delvare3760f732008-04-29 23:11:40 +0200166 { }
167};
168MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700169
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700170/*----------------------------------------------------------------------*/
171
Ed Swierk30e7b032009-03-31 15:24:56 -0700172#define BLOCK_DATA_MAX_TRIES 10
173
Jean Delvare0cc43a12011-01-10 22:11:23 +0100174static s32 ds1307_read_block_data_once(const struct i2c_client *client,
175 u8 command, u8 length, u8 *values)
Ed Swierk30e7b032009-03-31 15:24:56 -0700176{
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 Delvare0cc43a12011-01-10 22:11:23 +0100188static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700189 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 Delvare0cc43a12011-01-10 22:11:23 +0100214static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700215 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 Giometticb49a5e2008-10-15 22:02:58 -0700245/*
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 */
256static 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 Giometticb49a5e2008-10-15 22:02:58 -0700283 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700284 }
285
286out:
287 if (test_bit(HAS_ALARM, &ds1307->flags))
288 enable_irq(client->irq);
289 mutex_unlock(lock);
290}
291
292static 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 Brownell1abb0dc2006-06-25 05:48:17 -0700304static 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 Brownell045e0e82007-07-17 04:04:55 -0700309 /* read the RTC date and time registers all at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700310 tmp = ds1307->read_block_data(ds1307->client,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700311 ds1307->offset, 7, ds1307->regs);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800312 if (tmp != 7) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700313 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 Bunkfe20ba72008-10-18 20:28:41 -0700324 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
325 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700326 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700327 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 Brownell1abb0dc2006-06-25 05:48:17 -0700330 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700331 t->tm_mon = bcd2bin(tmp) - 1;
David Brownell1abb0dc2006-06-25 05:48:17 -0700332
333 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700334 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700335
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 Brownell045e0e82007-07-17 04:04:55 -0700342 /* initial clock setting can be undefined */
343 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700344}
345
346static 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 Garzik11966ad2006-10-04 04:41:53 -0400355 "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 Brownell1abb0dc2006-06-25 05:48:17 -0700358
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700359 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 Brownell1abb0dc2006-06-25 05:48:17 -0700365
366 /* assume 20YY not 19YY */
367 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700368 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700369
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700370 switch (ds1307->type) {
371 case ds_1337:
372 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700373 case ds_3231:
David Brownell1abb0dc2006-06-25 05:48:17 -0700374 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700375 break;
376 case ds_1340:
David Brownell1abb0dc2006-06-25 05:48:17 -0700377 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
378 | DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700379 break;
David Anders43fcb812011-11-02 13:37:53 -0700380 case mcp7941x:
David Anders40ce9722012-03-23 15:02:37 -0700381 /*
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 Anders43fcb812011-11-02 13:37:53 -0700386 buf[DS1307_REG_SECS] |= MCP7941X_BIT_ST;
387 buf[DS1307_REG_WDAY] |= MCP7941X_BIT_VBATEN;
388 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700389 default:
390 break;
391 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700392
David Brownell1abb0dc2006-06-25 05:48:17 -0700393 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 Tjernlund33df2ee2009-06-17 16:26:08 -0700397 result = ds1307->write_block_data(ds1307->client,
398 ds1307->offset, 7, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800399 if (result < 0) {
400 dev_err(dev, "%s error %d\n", "write", result);
401 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700402 }
403 return 0;
404}
405
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800406static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700407{
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 Swierk30e7b032009-03-31 15:24:56 -0700416 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800417 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
418 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700419 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 Anders40ce9722012-03-23 15:02:37 -0700431 /*
432 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700433 * 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 Reitel74d88eb2009-01-07 18:07:16 -0800458static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700459{
David Anders40ce9722012-03-23 15:02:37 -0700460 struct i2c_client *client = to_i2c_client(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700461 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 Swierk30e7b032009-03-31 15:24:56 -0700476 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800477 DS1339_REG_ALARM1_SECS, 9, buf);
478 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700479 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 Giometticb49a5e2008-10-15 22:02:58 -0700493 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 Swierk30e7b032009-03-31 15:24:56 -0700511 ret = ds1307->write_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800512 DS1339_REG_ALARM1_SECS, 9, buf);
513 if (ret < 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700514 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800515 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700516 }
517
518 return 0;
519}
520
John Stultz16380c12011-02-02 17:02:41 -0800521static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700522{
523 struct i2c_client *client = to_i2c_client(dev);
524 struct ds1307 *ds1307 = i2c_get_clientdata(client);
525 int ret;
526
John Stultz16380c12011-02-02 17:02:41 -0800527 if (!test_bit(HAS_ALARM, &ds1307->flags))
528 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700529
John Stultz16380c12011-02-02 17:02:41 -0800530 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
531 if (ret < 0)
532 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700533
John Stultz16380c12011-02-02 17:02:41 -0800534 if (enabled)
535 ret |= DS1337_BIT_A1IE;
536 else
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700537 ret &= ~DS1337_BIT_A1IE;
538
John Stultz16380c12011-02-02 17:02:41 -0800539 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
540 if (ret < 0)
541 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700542
543 return 0;
544}
545
David Brownellff8371a2006-09-30 23:28:17 -0700546static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700547 .read_time = ds1307_get_time,
548 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800549 .read_alarm = ds1337_read_alarm,
550 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800551 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700552};
553
David Brownell682d73f2007-11-14 16:58:32 -0800554/*----------------------------------------------------------------------*/
555
David Brownell682d73f2007-11-14 16:58:32 -0800556static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700557ds1307_nvram_read(struct file *filp, struct kobject *kobj,
558 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800559 char *buf, loff_t off, size_t count)
560{
561 struct i2c_client *client;
562 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800563 int result;
564
frederic Rodofcd8db02008-02-06 01:38:55 -0800565 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800566 ds1307 = i2c_get_clientdata(client);
567
Austin Boyle9eab0a72012-03-23 15:02:38 -0700568 if (unlikely(off >= ds1307->nvram->size))
David Brownell682d73f2007-11-14 16:58:32 -0800569 return 0;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700570 if ((off + count) > ds1307->nvram->size)
571 count = ds1307->nvram->size - off;
David Brownell682d73f2007-11-14 16:58:32 -0800572 if (unlikely(!count))
573 return count;
574
Austin Boyle9eab0a72012-03-23 15:02:38 -0700575 result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
576 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800577 if (result < 0)
David Brownell682d73f2007-11-14 16:58:32 -0800578 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800579 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800580}
581
582static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700583ds1307_nvram_write(struct file *filp, struct kobject *kobj,
584 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800585 char *buf, loff_t off, size_t count)
586{
587 struct i2c_client *client;
Ed Swierk30e7b032009-03-31 15:24:56 -0700588 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800589 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800590
frederic Rodofcd8db02008-02-06 01:38:55 -0800591 client = kobj_to_i2c_client(kobj);
Ed Swierk30e7b032009-03-31 15:24:56 -0700592 ds1307 = i2c_get_clientdata(client);
David Brownell682d73f2007-11-14 16:58:32 -0800593
Austin Boyle9eab0a72012-03-23 15:02:38 -0700594 if (unlikely(off >= ds1307->nvram->size))
David Brownell682d73f2007-11-14 16:58:32 -0800595 return -EFBIG;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700596 if ((off + count) > ds1307->nvram->size)
597 count = ds1307->nvram->size - off;
David Brownell682d73f2007-11-14 16:58:32 -0800598 if (unlikely(!count))
599 return count;
600
Austin Boyle9eab0a72012-03-23 15:02:38 -0700601 result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
602 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800603 if (result < 0) {
604 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
605 return result;
606 }
607 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800608}
609
David Brownell682d73f2007-11-14 16:58:32 -0800610/*----------------------------------------------------------------------*/
611
Jean Delvared2653e92008-04-29 23:11:39 +0200612static int __devinit ds1307_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -0700614{
615 struct ds1307 *ds1307;
616 int err = -ENODEV;
David Brownell1abb0dc2006-06-25 05:48:17 -0700617 int tmp;
Jean Delvare3760f732008-04-29 23:11:40 +0200618 const struct chip_desc *chip = &chips[id->driver_data];
David Brownellc065f352007-07-17 04:05:10 -0700619 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700620 int want_irq = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800621 unsigned char *buf;
Wolfram Sang97f902b2009-06-17 16:26:10 -0700622 static const int bbsqi_bitpos[] = {
623 [ds_1337] = 0,
624 [ds_1339] = DS1339_BIT_BBSQI,
625 [ds_3231] = DS3231_BIT_BBSQW,
626 };
David Brownell1abb0dc2006-06-25 05:48:17 -0700627
Ed Swierk30e7b032009-03-31 15:24:56 -0700628 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
629 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
David Brownellc065f352007-07-17 04:05:10 -0700630 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -0700631
David Anders40ce9722012-03-23 15:02:37 -0700632 ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL);
633 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -0700634 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -0700635
David Brownell1abb0dc2006-06-25 05:48:17 -0700636 i2c_set_clientdata(client, ds1307);
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700637
638 ds1307->client = client;
639 ds1307->type = id->driver_data;
640 ds1307->offset = 0;
641
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800642 buf = ds1307->regs;
Ed Swierk30e7b032009-03-31 15:24:56 -0700643 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 Brownell045e0e82007-07-17 04:04:55 -0700650
651 switch (ds1307->type) {
652 case ds_1337:
653 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700654 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700655 /* get registers that the "rtc" read below won't read... */
Ed Swierk30e7b032009-03-31 15:24:56 -0700656 tmp = ds1307->read_block_data(ds1307->client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800657 DS1337_REG_CONTROL, 2, buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700658 if (tmp != 2) {
659 pr_debug("read error %d\n", tmp);
660 err = -EIO;
661 goto exit_free;
662 }
663
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700664 /* oscillator off? turn it on, so clock can tick. */
665 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700666 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
667
David Anders40ce9722012-03-23 15:02:37 -0700668 /*
669 * Using IRQ? Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -0700670 * For some variants, be sure alarms can trigger when we're
671 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700672 */
Wolfram Sangb24a7262012-03-23 15:02:37 -0700673 if (ds1307->client->irq > 0 && chip->alarm) {
674 INIT_WORK(&ds1307->work, ds1307_work);
675
Wolfram Sang97f902b2009-06-17 16:26:10 -0700676 ds1307->regs[0] |= DS1337_BIT_INTCN
677 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700678 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -0700679
680 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700681 }
682
683 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
684 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700685
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 Brownell1abb0dc2006-06-25 05:48:17 -0700691 }
David Brownell045e0e82007-07-17 04:04:55 -0700692 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -0700693
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 Tjernlund33df2ee2009-06-17 16:26:08 -0700759 case ds_1388:
760 ds1307->offset = 1; /* Seconds starts at 1 */
761 break;
David Brownell045e0e82007-07-17 04:04:55 -0700762 default:
763 break;
764 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700765
766read_rtc:
767 /* read RTC registers */
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -0700768 tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800769 if (tmp != 8) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700770 pr_debug("read error %d\n", tmp);
771 err = -EIO;
772 goto exit_free;
773 }
774
David Anders40ce9722012-03-23 15:02:37 -0700775 /*
776 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -0700777 * 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 Brownell045e0e82007-07-17 04:04:55 -0700781 switch (ds1307->type) {
782 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -0700783 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700784 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700785 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700786 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
787 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -0700788 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -0700789 }
David Brownell045e0e82007-07-17 04:04:55 -0700790 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700791 case ds_1338:
792 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700793 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700794 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 Brownellbd16f9e2007-07-26 10:41:00 -0700799 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700800 & ~DS1338_BIT_OSF);
801 dev_warn(&client->dev, "SET TIME!\n");
802 goto read_rtc;
803 }
David Brownell045e0e82007-07-17 04:04:55 -0700804 break;
frederic Rodofcd8db02008-02-06 01:38:55 -0800805 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 Anders43fcb812011-11-02 13:37:53 -0700823 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 Sang32d322b2012-03-23 15:02:36 -0700840 default:
David Brownell045e0e82007-07-17 04:04:55 -0700841 break;
David Brownell1abb0dc2006-06-25 05:48:17 -0700842 }
David Brownell045e0e82007-07-17 04:04:55 -0700843
David Brownell1abb0dc2006-06-25 05:48:17 -0700844 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -0700845 switch (ds1307->type) {
846 case ds_1340:
847 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -0700848 /*
849 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -0700850 * systems that will run through year 2100.
851 */
852 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -0700853 case rx_8025:
854 break;
David Brownellc065f352007-07-17 04:05:10 -0700855 default:
856 if (!(tmp & DS1307_BIT_12HR))
857 break;
858
David Anders40ce9722012-03-23 15:02:37 -0700859 /*
860 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -0700861 * take note...
862 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700863 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -0700864 if (tmp == 12)
865 tmp = 0;
866 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
867 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -0700868 i2c_smbus_write_byte_data(client,
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -0700869 ds1307->offset + DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700870 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -0700871 }
872
David Brownell1abb0dc2006-06-25 05:48:17 -0700873 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 Brownellc065f352007-07-17 04:05:10 -0700879 goto exit_free;
David Brownell1abb0dc2006-06-25 05:48:17 -0700880 }
881
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700882 if (want_irq) {
Dmitry Eremin-Solenikov43d15bc2009-12-15 16:46:14 -0800883 err = request_irq(client->irq, ds1307_irq, IRQF_SHARED,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700884 ds1307->rtc->name, client);
885 if (err) {
886 dev_err(&client->dev,
887 "unable to request IRQ!\n");
888 goto exit_irq;
889 }
Anton Vorontsov26b3c012009-12-17 15:27:23 -0800890
891 device_set_wakeup_capable(&client->dev, 1);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700892 set_bit(HAS_ALARM, &ds1307->flags);
893 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
894 }
895
Austin Boyle9eab0a72012-03-23 15:02:38 -0700896 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 Brownell682d73f2007-11-14 16:58:32 -0800902 }
Austin Boyle9eab0a72012-03-23 15:02:38 -0700903 ds1307->nvram->attr.name = "nvram";
904 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
Anatolij Gustschin3f5ec5e2012-04-25 16:01:51 -0700905 sysfs_bin_attr_init(ds1307->nvram);
Austin Boyle9eab0a72012-03-23 15:02:38 -0700906 ds1307->nvram->read = ds1307_nvram_read,
907 ds1307->nvram->write = ds1307_nvram_write,
908 ds1307->nvram->size = chip->nvram_size;
909 ds1307->nvram_offset = chip->nvram_offset;
910 err = sysfs_create_bin_file(&client->dev.kobj, ds1307->nvram);
911 if (err) {
912 kfree(ds1307->nvram);
913 goto exit_nvram;
914 }
915 set_bit(HAS_NVRAM, &ds1307->flags);
916 dev_info(&client->dev, "%zu bytes nvram\n", ds1307->nvram->size);
David Brownell682d73f2007-11-14 16:58:32 -0800917 }
918
David Brownell1abb0dc2006-06-25 05:48:17 -0700919 return 0;
920
Austin Boyle9eab0a72012-03-23 15:02:38 -0700921exit_nvram:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700922exit_irq:
Julia Lawall72445af2009-09-22 16:46:29 -0700923 rtc_device_unregister(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -0700924exit_free:
925 kfree(ds1307);
David Brownell1abb0dc2006-06-25 05:48:17 -0700926 return err;
927}
928
David Brownellc065f352007-07-17 04:05:10 -0700929static int __devexit ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -0700930{
David Anders40ce9722012-03-23 15:02:37 -0700931 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -0700932
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700933 if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
934 free_irq(client->irq, client);
935 cancel_work_sync(&ds1307->work);
936 }
937
Austin Boyle9eab0a72012-03-23 15:02:38 -0700938 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) {
939 sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
940 kfree(ds1307->nvram);
941 }
David Brownell682d73f2007-11-14 16:58:32 -0800942
David Brownell1abb0dc2006-06-25 05:48:17 -0700943 rtc_device_unregister(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -0700944 kfree(ds1307);
945 return 0;
946}
947
948static struct i2c_driver ds1307_driver = {
949 .driver = {
David Brownellc065f352007-07-17 04:05:10 -0700950 .name = "rtc-ds1307",
David Brownell1abb0dc2006-06-25 05:48:17 -0700951 .owner = THIS_MODULE,
952 },
David Brownellc065f352007-07-17 04:05:10 -0700953 .probe = ds1307_probe,
954 .remove = __devexit_p(ds1307_remove),
Jean Delvare3760f732008-04-29 23:11:40 +0200955 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -0700956};
957
Axel Lin0abc9202012-03-23 15:02:31 -0700958module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -0700959
960MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
961MODULE_LICENSE("GPL");