blob: 4166b84cb514f6ff68d918d4e0ebf3fbd0c97bc5 [file] [log] [blame]
Andrew Sharp8f267952008-02-06 01:38:46 -08001/*
2 * An rtc driver for the Dallas DS1511
3 *
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
Andrew Sharp5b73a412009-09-23 16:03:20 -07005 * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com>
Andrew Sharp8f267952008-02-06 01:38:46 -08006 *
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 * Real time clock driver for the Dallas 1511 chip, which also
12 * contains a watchdog timer. There is a tiny amount of code that
13 * platform code could use to mess with the watchdog device a little
14 * bit, but not a full watchdog driver.
15 */
16
17#include <linux/bcd.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/rtc.h>
23#include <linux/platform_device.h>
24#include <linux/io.h>
25
26#define DRV_VERSION "0.6"
27
28enum ds1511reg {
29 DS1511_SEC = 0x0,
30 DS1511_MIN = 0x1,
31 DS1511_HOUR = 0x2,
32 DS1511_DOW = 0x3,
33 DS1511_DOM = 0x4,
34 DS1511_MONTH = 0x5,
35 DS1511_YEAR = 0x6,
36 DS1511_CENTURY = 0x7,
37 DS1511_AM1_SEC = 0x8,
38 DS1511_AM2_MIN = 0x9,
39 DS1511_AM3_HOUR = 0xa,
40 DS1511_AM4_DATE = 0xb,
41 DS1511_WD_MSEC = 0xc,
42 DS1511_WD_SEC = 0xd,
43 DS1511_CONTROL_A = 0xe,
44 DS1511_CONTROL_B = 0xf,
45 DS1511_RAMADDR_LSB = 0x10,
46 DS1511_RAMDATA = 0x13
47};
48
49#define DS1511_BLF1 0x80
50#define DS1511_BLF2 0x40
51#define DS1511_PRS 0x20
52#define DS1511_PAB 0x10
53#define DS1511_TDF 0x08
54#define DS1511_KSF 0x04
55#define DS1511_WDF 0x02
56#define DS1511_IRQF 0x01
57#define DS1511_TE 0x80
58#define DS1511_CS 0x40
59#define DS1511_BME 0x20
60#define DS1511_TPE 0x10
61#define DS1511_TIE 0x08
62#define DS1511_KIE 0x04
63#define DS1511_WDE 0x02
64#define DS1511_WDS 0x01
65#define DS1511_RAM_MAX 0xff
66
67#define RTC_CMD DS1511_CONTROL_B
68#define RTC_CMD1 DS1511_CONTROL_A
69
70#define RTC_ALARM_SEC DS1511_AM1_SEC
71#define RTC_ALARM_MIN DS1511_AM2_MIN
72#define RTC_ALARM_HOUR DS1511_AM3_HOUR
73#define RTC_ALARM_DATE DS1511_AM4_DATE
74
75#define RTC_SEC DS1511_SEC
76#define RTC_MIN DS1511_MIN
77#define RTC_HOUR DS1511_HOUR
78#define RTC_DOW DS1511_DOW
79#define RTC_DOM DS1511_DOM
80#define RTC_MON DS1511_MONTH
81#define RTC_YEAR DS1511_YEAR
82#define RTC_CENTURY DS1511_CENTURY
83
84#define RTC_TIE DS1511_TIE
85#define RTC_TE DS1511_TE
86
87struct rtc_plat_data {
88 struct rtc_device *rtc;
89 void __iomem *ioaddr; /* virtual base address */
Andrew Sharp8f267952008-02-06 01:38:46 -080090 int size; /* amount of memory mapped */
91 int irq;
92 unsigned int irqen;
93 int alrm_sec;
94 int alrm_min;
95 int alrm_hour;
96 int alrm_mday;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -080097 spinlock_t lock;
Andrew Sharp8f267952008-02-06 01:38:46 -080098};
99
100static DEFINE_SPINLOCK(ds1511_lock);
101
102static __iomem char *ds1511_base;
103static u32 reg_spacing = 1;
104
105 static noinline void
106rtc_write(uint8_t val, uint32_t reg)
107{
108 writeb(val, ds1511_base + (reg * reg_spacing));
109}
110
111 static inline void
112rtc_write_alarm(uint8_t val, enum ds1511reg reg)
113{
114 rtc_write((val | 0x80), reg);
115}
116
117 static noinline uint8_t
118rtc_read(enum ds1511reg reg)
119{
120 return readb(ds1511_base + (reg * reg_spacing));
121}
122
123 static inline void
124rtc_disable_update(void)
125{
126 rtc_write((rtc_read(RTC_CMD) & ~RTC_TE), RTC_CMD);
127}
128
129 static void
130rtc_enable_update(void)
131{
132 rtc_write((rtc_read(RTC_CMD) | RTC_TE), RTC_CMD);
133}
134
135/*
136 * #define DS1511_WDOG_RESET_SUPPORT
137 *
138 * Uncomment this if you want to use these routines in
139 * some platform code.
140 */
141#ifdef DS1511_WDOG_RESET_SUPPORT
142/*
143 * just enough code to set the watchdog timer so that it
144 * will reboot the system
145 */
146 void
147ds1511_wdog_set(unsigned long deciseconds)
148{
149 /*
150 * the wdog timer can take 99.99 seconds
151 */
152 deciseconds %= 10000;
153 /*
154 * set the wdog values in the wdog registers
155 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700156 rtc_write(bin2bcd(deciseconds % 100), DS1511_WD_MSEC);
157 rtc_write(bin2bcd(deciseconds / 100), DS1511_WD_SEC);
Andrew Sharp8f267952008-02-06 01:38:46 -0800158 /*
159 * set wdog enable and wdog 'steering' bit to issue a reset
160 */
161 rtc_write(DS1511_WDE | DS1511_WDS, RTC_CMD);
162}
163
164 void
165ds1511_wdog_disable(void)
166{
167 /*
168 * clear wdog enable and wdog 'steering' bits
169 */
170 rtc_write(rtc_read(RTC_CMD) & ~(DS1511_WDE | DS1511_WDS), RTC_CMD);
171 /*
172 * clear the wdog counter
173 */
174 rtc_write(0, DS1511_WD_MSEC);
175 rtc_write(0, DS1511_WD_SEC);
176}
177#endif
178
179/*
180 * set the rtc chip's idea of the time.
181 * stupidly, some callers call with year unmolested;
182 * and some call with year = year - 1900. thanks.
183 */
Adrian Bunka3ed1072008-04-28 02:11:55 -0700184static int ds1511_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
Andrew Sharp8f267952008-02-06 01:38:46 -0800185{
186 u8 mon, day, dow, hrs, min, sec, yrs, cen;
Steven Rostedt9a0f4ae2008-05-06 20:42:30 -0700187 unsigned long flags;
Andrew Sharp8f267952008-02-06 01:38:46 -0800188
189 /*
190 * won't have to change this for a while
191 */
192 if (rtc_tm->tm_year < 1900) {
193 rtc_tm->tm_year += 1900;
194 }
195
196 if (rtc_tm->tm_year < 1970) {
197 return -EINVAL;
198 }
199 yrs = rtc_tm->tm_year % 100;
200 cen = rtc_tm->tm_year / 100;
201 mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
202 day = rtc_tm->tm_mday;
203 dow = rtc_tm->tm_wday & 0x7; /* automatic BCD */
204 hrs = rtc_tm->tm_hour;
205 min = rtc_tm->tm_min;
206 sec = rtc_tm->tm_sec;
207
208 if ((mon > 12) || (day == 0)) {
209 return -EINVAL;
210 }
211
212 if (day > rtc_month_days(rtc_tm->tm_mon, rtc_tm->tm_year)) {
213 return -EINVAL;
214 }
215
216 if ((hrs >= 24) || (min >= 60) || (sec >= 60)) {
217 return -EINVAL;
218 }
219
220 /*
221 * each register is a different number of valid bits
222 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700223 sec = bin2bcd(sec) & 0x7f;
224 min = bin2bcd(min) & 0x7f;
225 hrs = bin2bcd(hrs) & 0x3f;
226 day = bin2bcd(day) & 0x3f;
227 mon = bin2bcd(mon) & 0x1f;
228 yrs = bin2bcd(yrs) & 0xff;
229 cen = bin2bcd(cen) & 0xff;
Andrew Sharp8f267952008-02-06 01:38:46 -0800230
231 spin_lock_irqsave(&ds1511_lock, flags);
232 rtc_disable_update();
233 rtc_write(cen, RTC_CENTURY);
234 rtc_write(yrs, RTC_YEAR);
235 rtc_write((rtc_read(RTC_MON) & 0xe0) | mon, RTC_MON);
236 rtc_write(day, RTC_DOM);
237 rtc_write(hrs, RTC_HOUR);
238 rtc_write(min, RTC_MIN);
239 rtc_write(sec, RTC_SEC);
240 rtc_write(dow, RTC_DOW);
241 rtc_enable_update();
242 spin_unlock_irqrestore(&ds1511_lock, flags);
243
244 return 0;
245}
246
Adrian Bunka3ed1072008-04-28 02:11:55 -0700247static int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
Andrew Sharp8f267952008-02-06 01:38:46 -0800248{
249 unsigned int century;
Steven Rostedt9a0f4ae2008-05-06 20:42:30 -0700250 unsigned long flags;
Andrew Sharp8f267952008-02-06 01:38:46 -0800251
252 spin_lock_irqsave(&ds1511_lock, flags);
253 rtc_disable_update();
254
255 rtc_tm->tm_sec = rtc_read(RTC_SEC) & 0x7f;
256 rtc_tm->tm_min = rtc_read(RTC_MIN) & 0x7f;
257 rtc_tm->tm_hour = rtc_read(RTC_HOUR) & 0x3f;
258 rtc_tm->tm_mday = rtc_read(RTC_DOM) & 0x3f;
259 rtc_tm->tm_wday = rtc_read(RTC_DOW) & 0x7;
260 rtc_tm->tm_mon = rtc_read(RTC_MON) & 0x1f;
261 rtc_tm->tm_year = rtc_read(RTC_YEAR) & 0x7f;
262 century = rtc_read(RTC_CENTURY);
263
264 rtc_enable_update();
265 spin_unlock_irqrestore(&ds1511_lock, flags);
266
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700267 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
268 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
269 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
270 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
271 rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
272 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
273 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
274 century = bcd2bin(century) * 100;
Andrew Sharp8f267952008-02-06 01:38:46 -0800275
276 /*
277 * Account for differences between how the RTC uses the values
278 * and how they are defined in a struct rtc_time;
279 */
280 century += rtc_tm->tm_year;
281 rtc_tm->tm_year = century - 1900;
282
283 rtc_tm->tm_mon--;
284
285 if (rtc_valid_tm(rtc_tm) < 0) {
286 dev_err(dev, "retrieved date/time is not valid.\n");
287 rtc_time_to_tm(0, rtc_tm);
288 }
289 return 0;
290}
291
292/*
293 * write the alarm register settings
294 *
295 * we only have the use to interrupt every second, otherwise
296 * known as the update interrupt, or the interrupt if the whole
297 * date/hours/mins/secs matches. the ds1511 has many more
298 * permutations, but the kernel doesn't.
299 */
300 static void
301ds1511_rtc_update_alarm(struct rtc_plat_data *pdata)
302{
303 unsigned long flags;
304
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800305 spin_lock_irqsave(&pdata->lock, flags);
Andrew Sharp8f267952008-02-06 01:38:46 -0800306 rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700307 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800308 RTC_ALARM_DATE);
309 rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700310 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800311 RTC_ALARM_HOUR);
312 rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700313 0x80 : bin2bcd(pdata->alrm_min) & 0x7f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800314 RTC_ALARM_MIN);
315 rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700316 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f,
Andrew Sharp8f267952008-02-06 01:38:46 -0800317 RTC_ALARM_SEC);
318 rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD);
319 rtc_read(RTC_CMD1); /* clear interrupts */
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800320 spin_unlock_irqrestore(&pdata->lock, flags);
Andrew Sharp8f267952008-02-06 01:38:46 -0800321}
322
323 static int
324ds1511_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
325{
326 struct platform_device *pdev = to_platform_device(dev);
327 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
328
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800329 if (pdata->irq <= 0)
Andrew Sharp8f267952008-02-06 01:38:46 -0800330 return -EINVAL;
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800331
Andrew Sharp8f267952008-02-06 01:38:46 -0800332 pdata->alrm_mday = alrm->time.tm_mday;
333 pdata->alrm_hour = alrm->time.tm_hour;
334 pdata->alrm_min = alrm->time.tm_min;
335 pdata->alrm_sec = alrm->time.tm_sec;
336 if (alrm->enabled) {
337 pdata->irqen |= RTC_AF;
338 }
339 ds1511_rtc_update_alarm(pdata);
340 return 0;
341}
342
343 static int
344ds1511_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
345{
346 struct platform_device *pdev = to_platform_device(dev);
347 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
348
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800349 if (pdata->irq <= 0)
Andrew Sharp8f267952008-02-06 01:38:46 -0800350 return -EINVAL;
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800351
Andrew Sharp8f267952008-02-06 01:38:46 -0800352 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
353 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
354 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
355 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
356 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
357 return 0;
358}
359
360 static irqreturn_t
361ds1511_interrupt(int irq, void *dev_id)
362{
363 struct platform_device *pdev = dev_id;
364 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800365 unsigned long events = 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800366
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800367 spin_lock(&pdata->lock);
Andrew Sharp8f267952008-02-06 01:38:46 -0800368 /*
369 * read and clear interrupt
370 */
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800371 if (rtc_read(RTC_CMD1) & DS1511_IRQF) {
372 events = RTC_IRQF;
373 if (rtc_read(RTC_ALARM_SEC) & 0x80)
374 events |= RTC_UF;
375 else
376 events |= RTC_AF;
377 if (likely(pdata->rtc))
378 rtc_update_irq(pdata->rtc, 1, events);
Andrew Sharp8f267952008-02-06 01:38:46 -0800379 }
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800380 spin_unlock(&pdata->lock);
381 return events ? IRQ_HANDLED : IRQ_NONE;
Andrew Sharp8f267952008-02-06 01:38:46 -0800382}
383
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800384static int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800385{
386 struct platform_device *pdev = to_platform_device(dev);
387 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
388
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800389 if (pdata->irq <= 0)
390 return -EINVAL;
391 if (enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800392 pdata->irqen |= RTC_AF;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800393 else
394 pdata->irqen &= ~RTC_AF;
395 ds1511_rtc_update_alarm(pdata);
396 return 0;
397}
398
399static int ds1511_rtc_update_irq_enable(struct device *dev,
400 unsigned int enabled)
401{
402 struct platform_device *pdev = to_platform_device(dev);
403 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
404
405 if (pdata->irq <= 0)
406 return -EINVAL;
407 if (enabled)
Andrew Sharp8f267952008-02-06 01:38:46 -0800408 pdata->irqen |= RTC_UF;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800409 else
410 pdata->irqen &= ~RTC_UF;
411 ds1511_rtc_update_alarm(pdata);
Andrew Sharp8f267952008-02-06 01:38:46 -0800412 return 0;
413}
414
415static const struct rtc_class_ops ds1511_rtc_ops = {
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800416 .read_time = ds1511_rtc_read_time,
417 .set_time = ds1511_rtc_set_time,
418 .read_alarm = ds1511_rtc_read_alarm,
419 .set_alarm = ds1511_rtc_set_alarm,
420 .alarm_irq_enable = ds1511_rtc_alarm_irq_enable,
421 .update_irq_enable = ds1511_rtc_update_irq_enable,
Andrew Sharp8f267952008-02-06 01:38:46 -0800422};
423
424 static ssize_t
425ds1511_nvram_read(struct kobject *kobj, struct bin_attribute *ba,
426 char *buf, loff_t pos, size_t size)
427{
428 ssize_t count;
429
430 /*
431 * if count is more than one, turn on "burst" mode
432 * turn it off when you're done
433 */
434 if (size > 1) {
435 rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
436 }
437 if (pos > DS1511_RAM_MAX) {
438 pos = DS1511_RAM_MAX;
439 }
440 if (size + pos > DS1511_RAM_MAX + 1) {
441 size = DS1511_RAM_MAX - pos + 1;
442 }
443 rtc_write(pos, DS1511_RAMADDR_LSB);
444 for (count = 0; size > 0; count++, size--) {
445 *buf++ = rtc_read(DS1511_RAMDATA);
446 }
447 if (count > 1) {
448 rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
449 }
450 return count;
451}
452
453 static ssize_t
454ds1511_nvram_write(struct kobject *kobj, struct bin_attribute *bin_attr,
455 char *buf, loff_t pos, size_t size)
456{
457 ssize_t count;
458
459 /*
460 * if count is more than one, turn on "burst" mode
461 * turn it off when you're done
462 */
463 if (size > 1) {
464 rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
465 }
466 if (pos > DS1511_RAM_MAX) {
467 pos = DS1511_RAM_MAX;
468 }
469 if (size + pos > DS1511_RAM_MAX + 1) {
470 size = DS1511_RAM_MAX - pos + 1;
471 }
472 rtc_write(pos, DS1511_RAMADDR_LSB);
473 for (count = 0; size > 0; count++, size--) {
474 rtc_write(*buf++, DS1511_RAMDATA);
475 }
476 if (count > 1) {
477 rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
478 }
479 return count;
480}
481
482static struct bin_attribute ds1511_nvram_attr = {
483 .attr = {
484 .name = "nvram",
485 .mode = S_IRUGO | S_IWUGO,
Andrew Sharp8f267952008-02-06 01:38:46 -0800486 },
487 .size = DS1511_RAM_MAX,
488 .read = ds1511_nvram_read,
489 .write = ds1511_nvram_write,
490};
491
492 static int __devinit
493ds1511_rtc_probe(struct platform_device *pdev)
494{
495 struct rtc_device *rtc;
496 struct resource *res;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800497 struct rtc_plat_data *pdata;
Andrew Sharp8f267952008-02-06 01:38:46 -0800498 int ret = 0;
499
500 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
501 if (!res) {
502 return -ENODEV;
503 }
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800504 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
505 if (!pdata)
Andrew Sharp8f267952008-02-06 01:38:46 -0800506 return -ENOMEM;
Andrew Sharp8f267952008-02-06 01:38:46 -0800507 pdata->size = res->end - res->start + 1;
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800508 if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size,
509 pdev->name))
510 return -EBUSY;
511 ds1511_base = devm_ioremap(&pdev->dev, res->start, pdata->size);
512 if (!ds1511_base)
513 return -ENOMEM;
Andrew Sharp8f267952008-02-06 01:38:46 -0800514 pdata->ioaddr = ds1511_base;
515 pdata->irq = platform_get_irq(pdev, 0);
516
517 /*
518 * turn on the clock and the crystal, etc.
519 */
520 rtc_write(0, RTC_CMD);
521 rtc_write(0, RTC_CMD1);
522 /*
523 * clear the wdog counter
524 */
525 rtc_write(0, DS1511_WD_MSEC);
526 rtc_write(0, DS1511_WD_SEC);
527 /*
528 * start the clock
529 */
530 rtc_enable_update();
531
532 /*
533 * check for a dying bat-tree
534 */
535 if (rtc_read(RTC_CMD1) & DS1511_BLF1) {
536 dev_warn(&pdev->dev, "voltage-low detected.\n");
537 }
538
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800539 spin_lock_init(&pdata->lock);
540 platform_set_drvdata(pdev, pdata);
Andrew Sharp8f267952008-02-06 01:38:46 -0800541 /*
542 * if the platform has an interrupt in mind for this device,
543 * then by all means, set it
544 */
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800545 if (pdata->irq > 0) {
Andrew Sharp8f267952008-02-06 01:38:46 -0800546 rtc_read(RTC_CMD1);
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800547 if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt,
Andrew Sharp8f267952008-02-06 01:38:46 -0800548 IRQF_DISABLED | IRQF_SHARED, pdev->name, pdev) < 0) {
549
550 dev_warn(&pdev->dev, "interrupt not available.\n");
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800551 pdata->irq = 0;
Andrew Sharp8f267952008-02-06 01:38:46 -0800552 }
553 }
554
555 rtc = rtc_device_register(pdev->name, &pdev->dev, &ds1511_rtc_ops,
556 THIS_MODULE);
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800557 if (IS_ERR(rtc))
558 return PTR_ERR(rtc);
Andrew Sharp8f267952008-02-06 01:38:46 -0800559 pdata->rtc = rtc;
Andrew Sharp8f267952008-02-06 01:38:46 -0800560
Atsushi Nemotoba4f3e42009-12-15 16:45:58 -0800561 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
562 if (ret)
563 rtc_device_unregister(pdata->rtc);
Andrew Sharp8f267952008-02-06 01:38:46 -0800564 return ret;
565}
566
567 static int __devexit
568ds1511_rtc_remove(struct platform_device *pdev)
569{
570 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
571
572 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
573 rtc_device_unregister(pdata->rtc);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800574 if (pdata->irq > 0) {
Andrew Sharp8f267952008-02-06 01:38:46 -0800575 /*
576 * disable the alarm interrupt
577 */
578 rtc_write(rtc_read(RTC_CMD) & ~RTC_TIE, RTC_CMD);
579 rtc_read(RTC_CMD1);
Andrew Sharp8f267952008-02-06 01:38:46 -0800580 }
Andrew Sharp8f267952008-02-06 01:38:46 -0800581 return 0;
582}
583
Kay Sieversad28a072008-04-10 21:29:25 -0700584/* work with hotplug and coldplug */
585MODULE_ALIAS("platform:ds1511");
586
Andrew Sharp8f267952008-02-06 01:38:46 -0800587static struct platform_driver ds1511_rtc_driver = {
588 .probe = ds1511_rtc_probe,
589 .remove = __devexit_p(ds1511_rtc_remove),
590 .driver = {
591 .name = "ds1511",
592 .owner = THIS_MODULE,
593 },
594};
595
596 static int __init
597ds1511_rtc_init(void)
598{
599 return platform_driver_register(&ds1511_rtc_driver);
600}
601
602 static void __exit
603ds1511_rtc_exit(void)
604{
Hannes Eder4367fa52008-11-30 14:05:45 +0100605 platform_driver_unregister(&ds1511_rtc_driver);
Andrew Sharp8f267952008-02-06 01:38:46 -0800606}
607
608module_init(ds1511_rtc_init);
609module_exit(ds1511_rtc_exit);
610
Andrew Sharp5b73a412009-09-23 16:03:20 -0700611MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
Andrew Sharp8f267952008-02-06 01:38:46 -0800612MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
613MODULE_LICENSE("GPL");
614MODULE_VERSION(DRV_VERSION);