blob: 162330b9d1dc0069ab94397e071d1d04e5c78865 [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
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/i2c.h>
16#include <linux/string.h>
17#include <linux/rtc.h>
18#include <linux/bcd.h>
19
20
21
22/* We can't determine type by probing, but if we expect pre-Linux code
23 * to have set the chip up as a clock (turning on the oscillator and
24 * setting the date and time), Linux can ignore the non-clock features.
25 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070026 */
27enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070028 ds_1307,
29 ds_1337,
30 ds_1338,
31 ds_1339,
32 ds_1340,
33 m41t00,
David Brownell1abb0dc2006-06-25 05:48:17 -070034 // rs5c372 too? different address...
35};
36
David Brownell1abb0dc2006-06-25 05:48:17 -070037
38/* RTC registers don't differ much, except for the century flag */
39#define DS1307_REG_SECS 0x00 /* 00-59 */
40# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070041# define DS1340_BIT_nEOSC 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070042#define DS1307_REG_MIN 0x01 /* 00-59 */
43#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070044# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
45# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070046# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
47# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
48#define DS1307_REG_WDAY 0x03 /* 01-07 */
49#define DS1307_REG_MDAY 0x04 /* 01-31 */
50#define DS1307_REG_MONTH 0x05 /* 01-12 */
51# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
52#define DS1307_REG_YEAR 0x06 /* 00-99 */
53
54/* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070055 * start at 7, and they differ a LOT. Only control and status matter for
56 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070057 */
David Brownell045e0e82007-07-17 04:04:55 -070058#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070059# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070060# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070061# define DS1307_BIT_SQWE 0x10
62# define DS1307_BIT_RS1 0x02
63# define DS1307_BIT_RS0 0x01
64#define DS1337_REG_CONTROL 0x0e
65# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070066# define DS1339_BIT_BBSQI 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070067# define DS1337_BIT_RS2 0x10
68# define DS1337_BIT_RS1 0x08
69# define DS1337_BIT_INTCN 0x04
70# define DS1337_BIT_A2IE 0x02
71# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070072#define DS1340_REG_CONTROL 0x07
73# define DS1340_BIT_OUT 0x80
74# define DS1340_BIT_FT 0x40
75# define DS1340_BIT_CALIB_SIGN 0x20
76# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070077#define DS1340_REG_FLAG 0x09
78# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070079#define DS1337_REG_STATUS 0x0f
80# define DS1337_BIT_OSF 0x80
81# define DS1337_BIT_A2I 0x02
82# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070083#define DS1339_REG_ALARM1_SECS 0x07
David Brownell1abb0dc2006-06-25 05:48:17 -070084#define DS1339_REG_TRICKLE 0x10
85
86
87
88struct ds1307 {
89 u8 reg_addr;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070090 u8 regs[11];
David Brownell1abb0dc2006-06-25 05:48:17 -070091 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070092 unsigned long flags;
93#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
94#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell1abb0dc2006-06-25 05:48:17 -070095 struct i2c_msg msg[2];
David Brownell045e0e82007-07-17 04:04:55 -070096 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -070097 struct rtc_device *rtc;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070098 struct work_struct work;
David Brownell1abb0dc2006-06-25 05:48:17 -070099};
100
David Brownell045e0e82007-07-17 04:04:55 -0700101struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700102 unsigned nvram56:1;
103 unsigned alarm:1;
David Brownell045e0e82007-07-17 04:04:55 -0700104};
105
Jean Delvare3760f732008-04-29 23:11:40 +0200106static const struct chip_desc chips[] = {
107[ds_1307] = {
David Brownell045e0e82007-07-17 04:04:55 -0700108 .nvram56 = 1,
Jean Delvare3760f732008-04-29 23:11:40 +0200109},
110[ds_1337] = {
David Brownell045e0e82007-07-17 04:04:55 -0700111 .alarm = 1,
Jean Delvare3760f732008-04-29 23:11:40 +0200112},
113[ds_1338] = {
David Brownell045e0e82007-07-17 04:04:55 -0700114 .nvram56 = 1,
Jean Delvare3760f732008-04-29 23:11:40 +0200115},
116[ds_1339] = {
David Brownell045e0e82007-07-17 04:04:55 -0700117 .alarm = 1,
Jean Delvare3760f732008-04-29 23:11:40 +0200118},
119[ds_1340] = {
120},
121[m41t00] = {
David Brownell045e0e82007-07-17 04:04:55 -0700122}, };
123
Jean Delvare3760f732008-04-29 23:11:40 +0200124static const struct i2c_device_id ds1307_id[] = {
125 { "ds1307", ds_1307 },
126 { "ds1337", ds_1337 },
127 { "ds1338", ds_1338 },
128 { "ds1339", ds_1339 },
129 { "ds1340", ds_1340 },
130 { "m41t00", m41t00 },
131 { }
132};
133MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700134
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700135/*----------------------------------------------------------------------*/
136
137/*
138 * The IRQ logic includes a "real" handler running in IRQ context just
139 * long enough to schedule this workqueue entry. We need a task context
140 * to talk to the RTC, since I2C I/O calls require that; and disable the
141 * IRQ until we clear its status on the chip, so that this handler can
142 * work with any type of triggering (not just falling edge).
143 *
144 * The ds1337 and ds1339 both have two alarms, but we only use the first
145 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
146 * signal; ds1339 chips have only one alarm signal.
147 */
148static void ds1307_work(struct work_struct *work)
149{
150 struct ds1307 *ds1307;
151 struct i2c_client *client;
152 struct mutex *lock;
153 int stat, control;
154
155 ds1307 = container_of(work, struct ds1307, work);
156 client = ds1307->client;
157 lock = &ds1307->rtc->ops_lock;
158
159 mutex_lock(lock);
160 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
161 if (stat < 0)
162 goto out;
163
164 if (stat & DS1337_BIT_A1I) {
165 stat &= ~DS1337_BIT_A1I;
166 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
167
168 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
169 if (control < 0)
170 goto out;
171
172 control &= ~DS1337_BIT_A1IE;
173 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
174
175 /* rtc_update_irq() assumes that it is called
176 * from IRQ-disabled context.
177 */
178 local_irq_disable();
179 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
180 local_irq_enable();
181 }
182
183out:
184 if (test_bit(HAS_ALARM, &ds1307->flags))
185 enable_irq(client->irq);
186 mutex_unlock(lock);
187}
188
189static irqreturn_t ds1307_irq(int irq, void *dev_id)
190{
191 struct i2c_client *client = dev_id;
192 struct ds1307 *ds1307 = i2c_get_clientdata(client);
193
194 disable_irq_nosync(irq);
195 schedule_work(&ds1307->work);
196 return IRQ_HANDLED;
197}
198
199/*----------------------------------------------------------------------*/
200
David Brownell1abb0dc2006-06-25 05:48:17 -0700201static int ds1307_get_time(struct device *dev, struct rtc_time *t)
202{
203 struct ds1307 *ds1307 = dev_get_drvdata(dev);
204 int tmp;
205
David Brownell045e0e82007-07-17 04:04:55 -0700206 /* read the RTC date and time registers all at once */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700207 ds1307->reg_addr = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -0700208 ds1307->msg[1].flags = I2C_M_RD;
209 ds1307->msg[1].len = 7;
210
David Brownell045e0e82007-07-17 04:04:55 -0700211 tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
212 ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700213 if (tmp != 2) {
214 dev_err(dev, "%s error %d\n", "read", tmp);
215 return -EIO;
216 }
217
218 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
219 "read",
220 ds1307->regs[0], ds1307->regs[1],
221 ds1307->regs[2], ds1307->regs[3],
222 ds1307->regs[4], ds1307->regs[5],
223 ds1307->regs[6]);
224
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700225 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
226 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700227 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700228 t->tm_hour = bcd2bin(tmp);
229 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
230 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700231 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700232 t->tm_mon = bcd2bin(tmp) - 1;
David Brownell1abb0dc2006-06-25 05:48:17 -0700233
234 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700235 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700236
237 dev_dbg(dev, "%s secs=%d, mins=%d, "
238 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
239 "read", t->tm_sec, t->tm_min,
240 t->tm_hour, t->tm_mday,
241 t->tm_mon, t->tm_year, t->tm_wday);
242
David Brownell045e0e82007-07-17 04:04:55 -0700243 /* initial clock setting can be undefined */
244 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700245}
246
247static int ds1307_set_time(struct device *dev, struct rtc_time *t)
248{
249 struct ds1307 *ds1307 = dev_get_drvdata(dev);
250 int result;
251 int tmp;
252 u8 *buf = ds1307->regs;
253
254 dev_dbg(dev, "%s secs=%d, mins=%d, "
255 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400256 "write", t->tm_sec, t->tm_min,
257 t->tm_hour, t->tm_mday,
258 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700259
260 *buf++ = 0; /* first register addr */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700261 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
262 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
263 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
264 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
265 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
266 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700267
268 /* assume 20YY not 19YY */
269 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700270 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700271
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700272 switch (ds1307->type) {
273 case ds_1337:
274 case ds_1339:
David Brownell1abb0dc2006-06-25 05:48:17 -0700275 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700276 break;
277 case ds_1340:
David Brownell1abb0dc2006-06-25 05:48:17 -0700278 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
279 | DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700280 break;
281 default:
282 break;
283 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700284
285 ds1307->msg[1].flags = 0;
286 ds1307->msg[1].len = 8;
287
288 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
289 "write", buf[0], buf[1], buf[2], buf[3],
290 buf[4], buf[5], buf[6]);
291
David Brownell045e0e82007-07-17 04:04:55 -0700292 result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
293 &ds1307->msg[1], 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700294 if (result != 1) {
295 dev_err(dev, "%s error %d\n", "write", tmp);
296 return -EIO;
297 }
298 return 0;
299}
300
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700301static int ds1307_read_alarm(struct device *dev, struct rtc_wkalrm *t)
302{
303 struct i2c_client *client = to_i2c_client(dev);
304 struct ds1307 *ds1307 = i2c_get_clientdata(client);
305 int ret;
306
307 if (!test_bit(HAS_ALARM, &ds1307->flags))
308 return -EINVAL;
309
310 /* read all ALARM1, ALARM2, and status registers at once */
311 ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
312 ds1307->msg[1].flags = I2C_M_RD;
313 ds1307->msg[1].len = 9;
314
315 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
316 ds1307->msg, 2);
317 if (ret != 2) {
318 dev_err(dev, "%s error %d\n", "alarm read", ret);
319 return -EIO;
320 }
321
322 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
323 "alarm read",
324 ds1307->regs[0], ds1307->regs[1],
325 ds1307->regs[2], ds1307->regs[3],
326 ds1307->regs[4], ds1307->regs[5],
327 ds1307->regs[6], ds1307->regs[7],
328 ds1307->regs[8]);
329
330 /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
331 * and that all four fields are checked matches
332 */
333 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
334 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
335 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
336 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
337 t->time.tm_mon = -1;
338 t->time.tm_year = -1;
339 t->time.tm_wday = -1;
340 t->time.tm_yday = -1;
341 t->time.tm_isdst = -1;
342
343 /* ... and status */
344 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
345 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
346
347 dev_dbg(dev, "%s secs=%d, mins=%d, "
348 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
349 "alarm read", t->time.tm_sec, t->time.tm_min,
350 t->time.tm_hour, t->time.tm_mday,
351 t->enabled, t->pending);
352
353 return 0;
354}
355
356static int ds1307_set_alarm(struct device *dev, struct rtc_wkalrm *t)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct ds1307 *ds1307 = i2c_get_clientdata(client);
360 unsigned char *buf = ds1307->regs;
361 u8 control, status;
362 int ret;
363
364 if (!test_bit(HAS_ALARM, &ds1307->flags))
365 return -EINVAL;
366
367 dev_dbg(dev, "%s secs=%d, mins=%d, "
368 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
369 "alarm set", t->time.tm_sec, t->time.tm_min,
370 t->time.tm_hour, t->time.tm_mday,
371 t->enabled, t->pending);
372
373 /* read current status of both alarms and the chip */
374 ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
375 ds1307->msg[1].flags = I2C_M_RD;
376 ds1307->msg[1].len = 9;
377
378 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
379 ds1307->msg, 2);
380 if (ret != 2) {
381 dev_err(dev, "%s error %d\n", "alarm write", ret);
382 return -EIO;
383 }
384 control = ds1307->regs[7];
385 status = ds1307->regs[8];
386
387 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
388 "alarm set (old status)",
389 ds1307->regs[0], ds1307->regs[1],
390 ds1307->regs[2], ds1307->regs[3],
391 ds1307->regs[4], ds1307->regs[5],
392 ds1307->regs[6], control, status);
393
394 /* set ALARM1, using 24 hour and day-of-month modes */
395 *buf++ = DS1339_REG_ALARM1_SECS; /* first register addr */
396 buf[0] = bin2bcd(t->time.tm_sec);
397 buf[1] = bin2bcd(t->time.tm_min);
398 buf[2] = bin2bcd(t->time.tm_hour);
399 buf[3] = bin2bcd(t->time.tm_mday);
400
401 /* set ALARM2 to non-garbage */
402 buf[4] = 0;
403 buf[5] = 0;
404 buf[6] = 0;
405
406 /* optionally enable ALARM1 */
407 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
408 if (t->enabled) {
409 dev_dbg(dev, "alarm IRQ armed\n");
410 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
411 }
412 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
413
414 ds1307->msg[1].flags = 0;
415 ds1307->msg[1].len = 10;
416
417 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
418 &ds1307->msg[1], 1);
419 if (ret != 1) {
420 dev_err(dev, "can't set alarm time\n");
421 return -EIO;
422 }
423
424 return 0;
425}
426
427static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
428{
429 struct i2c_client *client = to_i2c_client(dev);
430 struct ds1307 *ds1307 = i2c_get_clientdata(client);
431 int ret;
432
433 switch (cmd) {
434 case RTC_AIE_OFF:
435 if (!test_bit(HAS_ALARM, &ds1307->flags))
436 return -ENOTTY;
437
438 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
439 if (ret < 0)
440 return ret;
441
442 ret &= ~DS1337_BIT_A1IE;
443
444 ret = i2c_smbus_write_byte_data(client,
445 DS1337_REG_CONTROL, ret);
446 if (ret < 0)
447 return ret;
448
449 break;
450
451 case RTC_AIE_ON:
452 if (!test_bit(HAS_ALARM, &ds1307->flags))
453 return -ENOTTY;
454
455 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
456 if (ret < 0)
457 return ret;
458
459 ret |= DS1337_BIT_A1IE;
460
461 ret = i2c_smbus_write_byte_data(client,
462 DS1337_REG_CONTROL, ret);
463 if (ret < 0)
464 return ret;
465
466 break;
467
468 default:
469 return -ENOIOCTLCMD;
470 }
471
472 return 0;
473}
474
David Brownellff8371a2006-09-30 23:28:17 -0700475static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700476 .read_time = ds1307_get_time,
477 .set_time = ds1307_set_time,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700478 .read_alarm = ds1307_read_alarm,
479 .set_alarm = ds1307_set_alarm,
480 .ioctl = ds1307_ioctl,
David Brownell1abb0dc2006-06-25 05:48:17 -0700481};
482
David Brownell682d73f2007-11-14 16:58:32 -0800483/*----------------------------------------------------------------------*/
484
485#define NVRAM_SIZE 56
486
487static ssize_t
488ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
489 char *buf, loff_t off, size_t count)
490{
491 struct i2c_client *client;
492 struct ds1307 *ds1307;
493 struct i2c_msg msg[2];
494 int result;
495
frederic Rodofcd8db02008-02-06 01:38:55 -0800496 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800497 ds1307 = i2c_get_clientdata(client);
498
499 if (unlikely(off >= NVRAM_SIZE))
500 return 0;
501 if ((off + count) > NVRAM_SIZE)
502 count = NVRAM_SIZE - off;
503 if (unlikely(!count))
504 return count;
505
506 msg[0].addr = client->addr;
507 msg[0].flags = 0;
508 msg[0].len = 1;
509 msg[0].buf = buf;
510
511 buf[0] = 8 + off;
512
513 msg[1].addr = client->addr;
514 msg[1].flags = I2C_M_RD;
515 msg[1].len = count;
516 msg[1].buf = buf;
517
518 result = i2c_transfer(to_i2c_adapter(client->dev.parent), msg, 2);
519 if (result != 2) {
520 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
521 return -EIO;
522 }
523 return count;
524}
525
526static ssize_t
527ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
528 char *buf, loff_t off, size_t count)
529{
530 struct i2c_client *client;
531 u8 buffer[NVRAM_SIZE + 1];
532 int ret;
533
frederic Rodofcd8db02008-02-06 01:38:55 -0800534 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800535
536 if (unlikely(off >= NVRAM_SIZE))
537 return -EFBIG;
538 if ((off + count) > NVRAM_SIZE)
539 count = NVRAM_SIZE - off;
540 if (unlikely(!count))
541 return count;
542
543 buffer[0] = 8 + off;
544 memcpy(buffer + 1, buf, count);
545
546 ret = i2c_master_send(client, buffer, count + 1);
547 return (ret < 0) ? ret : (ret - 1);
548}
549
550static struct bin_attribute nvram = {
551 .attr = {
552 .name = "nvram",
553 .mode = S_IRUGO | S_IWUSR,
David Brownell682d73f2007-11-14 16:58:32 -0800554 },
555
556 .read = ds1307_nvram_read,
557 .write = ds1307_nvram_write,
558 .size = NVRAM_SIZE,
559};
560
561/*----------------------------------------------------------------------*/
562
David Brownell1abb0dc2006-06-25 05:48:17 -0700563static struct i2c_driver ds1307_driver;
564
Jean Delvared2653e92008-04-29 23:11:39 +0200565static int __devinit ds1307_probe(struct i2c_client *client,
566 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -0700567{
568 struct ds1307 *ds1307;
569 int err = -ENODEV;
David Brownell1abb0dc2006-06-25 05:48:17 -0700570 int tmp;
Jean Delvare3760f732008-04-29 23:11:40 +0200571 const struct chip_desc *chip = &chips[id->driver_data];
David Brownellc065f352007-07-17 04:05:10 -0700572 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700573 int want_irq = false;
David Brownell1abb0dc2006-06-25 05:48:17 -0700574
David Brownellc065f352007-07-17 04:05:10 -0700575 if (!i2c_check_functionality(adapter,
576 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
577 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -0700578
David Brownellc065f352007-07-17 04:05:10 -0700579 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
580 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -0700581
582 ds1307->client = client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700583 i2c_set_clientdata(client, ds1307);
584
585 ds1307->msg[0].addr = client->addr;
586 ds1307->msg[0].flags = 0;
587 ds1307->msg[0].len = 1;
588 ds1307->msg[0].buf = &ds1307->reg_addr;
589
590 ds1307->msg[1].addr = client->addr;
591 ds1307->msg[1].flags = I2C_M_RD;
592 ds1307->msg[1].len = sizeof(ds1307->regs);
593 ds1307->msg[1].buf = ds1307->regs;
594
Jean Delvare3760f732008-04-29 23:11:40 +0200595 ds1307->type = id->driver_data;
David Brownell045e0e82007-07-17 04:04:55 -0700596
597 switch (ds1307->type) {
598 case ds_1337:
599 case ds_1339:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700600 /* has IRQ? */
601 if (ds1307->client->irq > 0 && chip->alarm) {
602 INIT_WORK(&ds1307->work, ds1307_work);
603 want_irq = true;
604 }
605
David Brownell1abb0dc2006-06-25 05:48:17 -0700606 ds1307->reg_addr = DS1337_REG_CONTROL;
607 ds1307->msg[1].len = 2;
608
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700609 /* get registers that the "rtc" read below won't read... */
David Brownell045e0e82007-07-17 04:04:55 -0700610 tmp = i2c_transfer(adapter, ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700611 if (tmp != 2) {
612 pr_debug("read error %d\n", tmp);
613 err = -EIO;
614 goto exit_free;
615 }
616
617 ds1307->reg_addr = 0;
618 ds1307->msg[1].len = sizeof(ds1307->regs);
619
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700620 /* oscillator off? turn it on, so clock can tick. */
621 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700622 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
623
624 /* Using IRQ? Disable the square wave and both alarms.
625 * For ds1339, be sure alarms can trigger when we're
626 * running on Vbackup (BBSQI); we assume ds1337 will
627 * ignore that bit
628 */
629 if (want_irq) {
630 ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI;
631 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
632 }
633
634 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
635 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700636
637 /* oscillator fault? clear flag, and warn */
638 if (ds1307->regs[1] & DS1337_BIT_OSF) {
639 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
640 ds1307->regs[1] & ~DS1337_BIT_OSF);
641 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -0700642 }
David Brownell045e0e82007-07-17 04:04:55 -0700643 break;
644 default:
645 break;
646 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700647
648read_rtc:
649 /* read RTC registers */
650
David Brownell045e0e82007-07-17 04:04:55 -0700651 tmp = i2c_transfer(adapter, ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700652 if (tmp != 2) {
653 pr_debug("read error %d\n", tmp);
654 err = -EIO;
655 goto exit_free;
656 }
657
658 /* minimal sanity checking; some chips (like DS1340) don't
659 * specify the extra bits as must-be-zero, but there are
660 * still a few values that are clearly out-of-range.
661 */
662 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -0700663 switch (ds1307->type) {
664 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -0700665 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700666 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700667 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700668 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
669 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -0700670 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -0700671 }
David Brownell045e0e82007-07-17 04:04:55 -0700672 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700673 case ds_1338:
674 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700675 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700676 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
677
678 /* oscillator fault? clear flag, and warn */
679 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
680 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
David Brownellbd16f9e2007-07-26 10:41:00 -0700681 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700682 & ~DS1338_BIT_OSF);
683 dev_warn(&client->dev, "SET TIME!\n");
684 goto read_rtc;
685 }
David Brownell045e0e82007-07-17 04:04:55 -0700686 break;
frederic Rodofcd8db02008-02-06 01:38:55 -0800687 case ds_1340:
688 /* clock halted? turn it on, so clock can tick. */
689 if (tmp & DS1340_BIT_nEOSC)
690 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
691
692 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
693 if (tmp < 0) {
694 pr_debug("read error %d\n", tmp);
695 err = -EIO;
696 goto exit_free;
697 }
698
699 /* oscillator fault? clear flag, and warn */
700 if (tmp & DS1340_BIT_OSF) {
701 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
702 dev_warn(&client->dev, "SET TIME!\n");
703 }
704 break;
David Brownellc065f352007-07-17 04:05:10 -0700705 case ds_1337:
706 case ds_1339:
David Brownell045e0e82007-07-17 04:04:55 -0700707 break;
David Brownell1abb0dc2006-06-25 05:48:17 -0700708 }
David Brownell045e0e82007-07-17 04:04:55 -0700709
710 tmp = ds1307->regs[DS1307_REG_SECS];
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700711 tmp = bcd2bin(tmp & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700712 if (tmp > 60)
David Brownellc065f352007-07-17 04:05:10 -0700713 goto exit_bad;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700714 tmp = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700715 if (tmp > 60)
David Brownellc065f352007-07-17 04:05:10 -0700716 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700717
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700718 tmp = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700719 if (tmp == 0 || tmp > 31)
David Brownellc065f352007-07-17 04:05:10 -0700720 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700721
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700722 tmp = bcd2bin(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700723 if (tmp == 0 || tmp > 12)
David Brownellc065f352007-07-17 04:05:10 -0700724 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700725
David Brownell1abb0dc2006-06-25 05:48:17 -0700726 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -0700727 switch (ds1307->type) {
728 case ds_1340:
729 case m41t00:
730 /* NOTE: ignores century bits; fix before deploying
731 * systems that will run through year 2100.
732 */
733 break;
734 default:
735 if (!(tmp & DS1307_BIT_12HR))
736 break;
737
738 /* Be sure we're in 24 hour mode. Multi-master systems
739 * take note...
740 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700741 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -0700742 if (tmp == 12)
743 tmp = 0;
744 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
745 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -0700746 i2c_smbus_write_byte_data(client,
747 DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700748 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -0700749 }
750
David Brownell1abb0dc2006-06-25 05:48:17 -0700751 ds1307->rtc = rtc_device_register(client->name, &client->dev,
752 &ds13xx_rtc_ops, THIS_MODULE);
753 if (IS_ERR(ds1307->rtc)) {
754 err = PTR_ERR(ds1307->rtc);
755 dev_err(&client->dev,
756 "unable to register the class device\n");
David Brownellc065f352007-07-17 04:05:10 -0700757 goto exit_free;
David Brownell1abb0dc2006-06-25 05:48:17 -0700758 }
759
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700760 if (want_irq) {
761 err = request_irq(client->irq, ds1307_irq, 0,
762 ds1307->rtc->name, client);
763 if (err) {
764 dev_err(&client->dev,
765 "unable to request IRQ!\n");
766 goto exit_irq;
767 }
768 set_bit(HAS_ALARM, &ds1307->flags);
769 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
770 }
771
David Brownell682d73f2007-11-14 16:58:32 -0800772 if (chip->nvram56) {
773 err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
774 if (err == 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700775 set_bit(HAS_NVRAM, &ds1307->flags);
David Brownell682d73f2007-11-14 16:58:32 -0800776 dev_info(&client->dev, "56 bytes nvram\n");
777 }
778 }
779
David Brownell1abb0dc2006-06-25 05:48:17 -0700780 return 0;
781
David Brownellc065f352007-07-17 04:05:10 -0700782exit_bad:
783 dev_dbg(&client->dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
784 "bogus register",
785 ds1307->regs[0], ds1307->regs[1],
786 ds1307->regs[2], ds1307->regs[3],
787 ds1307->regs[4], ds1307->regs[5],
788 ds1307->regs[6]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700789exit_irq:
790 if (ds1307->rtc)
791 rtc_device_unregister(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -0700792exit_free:
793 kfree(ds1307);
David Brownell1abb0dc2006-06-25 05:48:17 -0700794 return err;
795}
796
David Brownellc065f352007-07-17 04:05:10 -0700797static int __devexit ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -0700798{
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700799 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -0700800
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700801 if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
802 free_irq(client->irq, client);
803 cancel_work_sync(&ds1307->work);
804 }
805
806 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
David Brownell682d73f2007-11-14 16:58:32 -0800807 sysfs_remove_bin_file(&client->dev.kobj, &nvram);
808
David Brownell1abb0dc2006-06-25 05:48:17 -0700809 rtc_device_unregister(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -0700810 kfree(ds1307);
811 return 0;
812}
813
814static struct i2c_driver ds1307_driver = {
815 .driver = {
David Brownellc065f352007-07-17 04:05:10 -0700816 .name = "rtc-ds1307",
David Brownell1abb0dc2006-06-25 05:48:17 -0700817 .owner = THIS_MODULE,
818 },
David Brownellc065f352007-07-17 04:05:10 -0700819 .probe = ds1307_probe,
820 .remove = __devexit_p(ds1307_remove),
Jean Delvare3760f732008-04-29 23:11:40 +0200821 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -0700822};
823
824static int __init ds1307_init(void)
825{
826 return i2c_add_driver(&ds1307_driver);
827}
828module_init(ds1307_init);
829
830static void __exit ds1307_exit(void)
831{
832 i2c_del_driver(&ds1307_driver);
833}
834module_exit(ds1307_exit);
835
836MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
837MODULE_LICENSE("GPL");