ALSA: hda - Fix missing stream for second ADC on Realtek ALC260 HDA codec
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-ds1305.c
blob7836c9cec5578a559170e1c356284f795f6be409
1 /*
2 * rtc-ds1305.c -- driver for DS1305 and DS1306 SPI RTC chips
4 * Copyright (C) 2008 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/bcd.h>
14 #include <linux/slab.h>
15 #include <linux/rtc.h>
16 #include <linux/workqueue.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/ds1305.h>
23 * Registers ... mask DS1305_WRITE into register address to write,
24 * otherwise you're reading it. All non-bitmask values are BCD.
26 #define DS1305_WRITE 0x80
29 /* RTC date/time ... the main special cases are that we:
30 * - Need fancy "hours" encoding in 12hour mode
31 * - Don't rely on the "day-of-week" field (or tm_wday)
32 * - Are a 21st-century clock (2000 <= year < 2100)
34 #define DS1305_RTC_LEN 7 /* bytes for RTC regs */
36 #define DS1305_SEC 0x00 /* register addresses */
37 #define DS1305_MIN 0x01
38 #define DS1305_HOUR 0x02
39 # define DS1305_HR_12 0x40 /* set == 12 hr mode */
40 # define DS1305_HR_PM 0x20 /* set == PM (12hr mode) */
41 #define DS1305_WDAY 0x03
42 #define DS1305_MDAY 0x04
43 #define DS1305_MON 0x05
44 #define DS1305_YEAR 0x06
47 /* The two alarms have only sec/min/hour/wday fields (ALM_LEN).
48 * DS1305_ALM_DISABLE disables a match field (some combos are bad).
50 * NOTE that since we don't use WDAY, we limit ourselves to alarms
51 * only one day into the future (vs potentially up to a week).
53 * NOTE ALSO that while we could generate once-a-second IRQs (UIE), we
54 * don't currently support them. We'd either need to do it only when
55 * no alarm is pending (not the standard model), or to use the second
56 * alarm (implying that this is a DS1305 not DS1306, *and* that either
57 * it's wired up a second IRQ we know, or that INTCN is set)
59 #define DS1305_ALM_LEN 4 /* bytes for ALM regs */
60 #define DS1305_ALM_DISABLE 0x80
62 #define DS1305_ALM0(r) (0x07 + (r)) /* register addresses */
63 #define DS1305_ALM1(r) (0x0b + (r))
66 /* three control registers */
67 #define DS1305_CONTROL_LEN 3 /* bytes of control regs */
69 #define DS1305_CONTROL 0x0f /* register addresses */
70 # define DS1305_nEOSC 0x80 /* low enables oscillator */
71 # define DS1305_WP 0x40 /* write protect */
72 # define DS1305_INTCN 0x04 /* clear == only int0 used */
73 # define DS1306_1HZ 0x04 /* enable 1Hz output */
74 # define DS1305_AEI1 0x02 /* enable ALM1 IRQ */
75 # define DS1305_AEI0 0x01 /* enable ALM0 IRQ */
76 #define DS1305_STATUS 0x10
77 /* status has just AEIx bits, mirrored as IRQFx */
78 #define DS1305_TRICKLE 0x11
79 /* trickle bits are defined in <linux/spi/ds1305.h> */
81 /* a bunch of NVRAM */
82 #define DS1305_NVRAM_LEN 96 /* bytes of NVRAM */
84 #define DS1305_NVRAM 0x20 /* register addresses */
87 struct ds1305 {
88 struct spi_device *spi;
89 struct rtc_device *rtc;
91 struct work_struct work;
93 unsigned long flags;
94 #define FLAG_EXITING 0
96 bool hr12;
97 u8 ctrl[DS1305_CONTROL_LEN];
101 /*----------------------------------------------------------------------*/
104 * Utilities ... tolerate 12-hour AM/PM notation in case of non-Linux
105 * software (like a bootloader) which may require it.
108 static unsigned bcd2hour(u8 bcd)
110 if (bcd & DS1305_HR_12) {
111 unsigned hour = 0;
113 bcd &= ~DS1305_HR_12;
114 if (bcd & DS1305_HR_PM) {
115 hour = 12;
116 bcd &= ~DS1305_HR_PM;
118 hour += bcd2bin(bcd);
119 return hour - 1;
121 return bcd2bin(bcd);
124 static u8 hour2bcd(bool hr12, int hour)
126 if (hr12) {
127 hour++;
128 if (hour <= 12)
129 return DS1305_HR_12 | bin2bcd(hour);
130 hour -= 12;
131 return DS1305_HR_12 | DS1305_HR_PM | bin2bcd(hour);
133 return bin2bcd(hour);
136 /*----------------------------------------------------------------------*/
139 * Interface to RTC framework
142 #ifdef CONFIG_RTC_INTF_DEV
145 * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
147 static int ds1305_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
149 struct ds1305 *ds1305 = dev_get_drvdata(dev);
150 u8 buf[2];
151 int status = -ENOIOCTLCMD;
153 buf[0] = DS1305_WRITE | DS1305_CONTROL;
154 buf[1] = ds1305->ctrl[0];
156 switch (cmd) {
157 case RTC_AIE_OFF:
158 status = 0;
159 if (!(buf[1] & DS1305_AEI0))
160 goto done;
161 buf[1] &= ~DS1305_AEI0;
162 break;
164 case RTC_AIE_ON:
165 status = 0;
166 if (ds1305->ctrl[0] & DS1305_AEI0)
167 goto done;
168 buf[1] |= DS1305_AEI0;
169 break;
171 if (status == 0) {
172 status = spi_write_then_read(ds1305->spi, buf, sizeof buf,
173 NULL, 0);
174 if (status >= 0)
175 ds1305->ctrl[0] = buf[1];
178 done:
179 return status;
182 #else
183 #define ds1305_ioctl NULL
184 #endif
187 * Get/set of date and time is pretty normal.
190 static int ds1305_get_time(struct device *dev, struct rtc_time *time)
192 struct ds1305 *ds1305 = dev_get_drvdata(dev);
193 u8 addr = DS1305_SEC;
194 u8 buf[DS1305_RTC_LEN];
195 int status;
197 /* Use write-then-read to get all the date/time registers
198 * since dma from stack is nonportable
200 status = spi_write_then_read(ds1305->spi, &addr, sizeof addr,
201 buf, sizeof buf);
202 if (status < 0)
203 return status;
205 dev_vdbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
206 "read", buf[0], buf[1], buf[2], buf[3],
207 buf[4], buf[5], buf[6]);
209 /* Decode the registers */
210 time->tm_sec = bcd2bin(buf[DS1305_SEC]);
211 time->tm_min = bcd2bin(buf[DS1305_MIN]);
212 time->tm_hour = bcd2hour(buf[DS1305_HOUR]);
213 time->tm_wday = buf[DS1305_WDAY] - 1;
214 time->tm_mday = bcd2bin(buf[DS1305_MDAY]);
215 time->tm_mon = bcd2bin(buf[DS1305_MON]) - 1;
216 time->tm_year = bcd2bin(buf[DS1305_YEAR]) + 100;
218 dev_vdbg(dev, "%s secs=%d, mins=%d, "
219 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
220 "read", time->tm_sec, time->tm_min,
221 time->tm_hour, time->tm_mday,
222 time->tm_mon, time->tm_year, time->tm_wday);
224 /* Time may not be set */
225 return rtc_valid_tm(time);
228 static int ds1305_set_time(struct device *dev, struct rtc_time *time)
230 struct ds1305 *ds1305 = dev_get_drvdata(dev);
231 u8 buf[1 + DS1305_RTC_LEN];
232 u8 *bp = buf;
234 dev_vdbg(dev, "%s secs=%d, mins=%d, "
235 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
236 "write", time->tm_sec, time->tm_min,
237 time->tm_hour, time->tm_mday,
238 time->tm_mon, time->tm_year, time->tm_wday);
240 /* Write registers starting at the first time/date address. */
241 *bp++ = DS1305_WRITE | DS1305_SEC;
243 *bp++ = bin2bcd(time->tm_sec);
244 *bp++ = bin2bcd(time->tm_min);
245 *bp++ = hour2bcd(ds1305->hr12, time->tm_hour);
246 *bp++ = (time->tm_wday < 7) ? (time->tm_wday + 1) : 1;
247 *bp++ = bin2bcd(time->tm_mday);
248 *bp++ = bin2bcd(time->tm_mon + 1);
249 *bp++ = bin2bcd(time->tm_year - 100);
251 dev_dbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
252 "write", buf[1], buf[2], buf[3],
253 buf[4], buf[5], buf[6], buf[7]);
255 /* use write-then-read since dma from stack is nonportable */
256 return spi_write_then_read(ds1305->spi, buf, sizeof buf,
257 NULL, 0);
261 * Get/set of alarm is a bit funky:
263 * - First there's the inherent raciness of getting the (partitioned)
264 * status of an alarm that could trigger while we're reading parts
265 * of that status.
267 * - Second there's its limited range (we could increase it a bit by
268 * relying on WDAY), which means it will easily roll over.
270 * - Third there's the choice of two alarms and alarm signals.
271 * Here we use ALM0 and expect that nINT0 (open drain) is used;
272 * that's the only real option for DS1306 runtime alarms, and is
273 * natural on DS1305.
275 * - Fourth, there's also ALM1, and a second interrupt signal:
276 * + On DS1305 ALM1 uses nINT1 (when INTCN=1) else nINT0;
277 * + On DS1306 ALM1 only uses INT1 (an active high pulse)
278 * and it won't work when VCC1 is active.
280 * So to be most general, we should probably set both alarms to the
281 * same value, letting ALM1 be the wakeup event source on DS1306
282 * and handling several wiring options on DS1305.
284 * - Fifth, we support the polled mode (as well as possible; why not?)
285 * even when no interrupt line is wired to an IRQ.
289 * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
291 static int ds1305_get_alarm(struct device *dev, struct rtc_wkalrm *alm)
293 struct ds1305 *ds1305 = dev_get_drvdata(dev);
294 struct spi_device *spi = ds1305->spi;
295 u8 addr;
296 int status;
297 u8 buf[DS1305_ALM_LEN];
299 /* Refresh control register cache BEFORE reading ALM0 registers,
300 * since reading alarm registers acks any pending IRQ. That
301 * makes returning "pending" status a bit of a lie, but that bit
302 * of EFI status is at best fragile anyway (given IRQ handlers).
304 addr = DS1305_CONTROL;
305 status = spi_write_then_read(spi, &addr, sizeof addr,
306 ds1305->ctrl, sizeof ds1305->ctrl);
307 if (status < 0)
308 return status;
310 alm->enabled = !!(ds1305->ctrl[0] & DS1305_AEI0);
311 alm->pending = !!(ds1305->ctrl[1] & DS1305_AEI0);
313 /* get and check ALM0 registers */
314 addr = DS1305_ALM0(DS1305_SEC);
315 status = spi_write_then_read(spi, &addr, sizeof addr,
316 buf, sizeof buf);
317 if (status < 0)
318 return status;
320 dev_vdbg(dev, "%s: %02x %02x %02x %02x\n",
321 "alm0 read", buf[DS1305_SEC], buf[DS1305_MIN],
322 buf[DS1305_HOUR], buf[DS1305_WDAY]);
324 if ((DS1305_ALM_DISABLE & buf[DS1305_SEC])
325 || (DS1305_ALM_DISABLE & buf[DS1305_MIN])
326 || (DS1305_ALM_DISABLE & buf[DS1305_HOUR]))
327 return -EIO;
329 /* Stuff these values into alm->time and let RTC framework code
330 * fill in the rest ... and also handle rollover to tomorrow when
331 * that's needed.
333 alm->time.tm_sec = bcd2bin(buf[DS1305_SEC]);
334 alm->time.tm_min = bcd2bin(buf[DS1305_MIN]);
335 alm->time.tm_hour = bcd2hour(buf[DS1305_HOUR]);
336 alm->time.tm_mday = -1;
337 alm->time.tm_mon = -1;
338 alm->time.tm_year = -1;
339 /* next three fields are unused by Linux */
340 alm->time.tm_wday = -1;
341 alm->time.tm_mday = -1;
342 alm->time.tm_isdst = -1;
344 return 0;
348 * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
350 static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
352 struct ds1305 *ds1305 = dev_get_drvdata(dev);
353 struct spi_device *spi = ds1305->spi;
354 unsigned long now, later;
355 struct rtc_time tm;
356 int status;
357 u8 buf[1 + DS1305_ALM_LEN];
359 /* convert desired alarm to time_t */
360 status = rtc_tm_to_time(&alm->time, &later);
361 if (status < 0)
362 return status;
364 /* Read current time as time_t */
365 status = ds1305_get_time(dev, &tm);
366 if (status < 0)
367 return status;
368 status = rtc_tm_to_time(&tm, &now);
369 if (status < 0)
370 return status;
372 /* make sure alarm fires within the next 24 hours */
373 if (later <= now)
374 return -EINVAL;
375 if ((later - now) > 24 * 60 * 60)
376 return -EDOM;
378 /* disable alarm if needed */
379 if (ds1305->ctrl[0] & DS1305_AEI0) {
380 ds1305->ctrl[0] &= ~DS1305_AEI0;
382 buf[0] = DS1305_WRITE | DS1305_CONTROL;
383 buf[1] = ds1305->ctrl[0];
384 status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
385 if (status < 0)
386 return status;
389 /* write alarm */
390 buf[0] = DS1305_WRITE | DS1305_ALM0(DS1305_SEC);
391 buf[1 + DS1305_SEC] = bin2bcd(alm->time.tm_sec);
392 buf[1 + DS1305_MIN] = bin2bcd(alm->time.tm_min);
393 buf[1 + DS1305_HOUR] = hour2bcd(ds1305->hr12, alm->time.tm_hour);
394 buf[1 + DS1305_WDAY] = DS1305_ALM_DISABLE;
396 dev_dbg(dev, "%s: %02x %02x %02x %02x\n",
397 "alm0 write", buf[1 + DS1305_SEC], buf[1 + DS1305_MIN],
398 buf[1 + DS1305_HOUR], buf[1 + DS1305_WDAY]);
400 status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
401 if (status < 0)
402 return status;
404 /* enable alarm if requested */
405 if (alm->enabled) {
406 ds1305->ctrl[0] |= DS1305_AEI0;
408 buf[0] = DS1305_WRITE | DS1305_CONTROL;
409 buf[1] = ds1305->ctrl[0];
410 status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
413 return status;
416 #ifdef CONFIG_PROC_FS
418 static int ds1305_proc(struct device *dev, struct seq_file *seq)
420 struct ds1305 *ds1305 = dev_get_drvdata(dev);
421 char *diodes = "no";
422 char *resistors = "";
424 /* ctrl[2] is treated as read-only; no locking needed */
425 if ((ds1305->ctrl[2] & 0xf0) == DS1305_TRICKLE_MAGIC) {
426 switch (ds1305->ctrl[2] & 0x0c) {
427 case DS1305_TRICKLE_DS2:
428 diodes = "2 diodes, ";
429 break;
430 case DS1305_TRICKLE_DS1:
431 diodes = "1 diode, ";
432 break;
433 default:
434 goto done;
436 switch (ds1305->ctrl[2] & 0x03) {
437 case DS1305_TRICKLE_2K:
438 resistors = "2k Ohm";
439 break;
440 case DS1305_TRICKLE_4K:
441 resistors = "4k Ohm";
442 break;
443 case DS1305_TRICKLE_8K:
444 resistors = "8k Ohm";
445 break;
446 default:
447 diodes = "no";
448 break;
452 done:
453 return seq_printf(seq,
454 "trickle_charge\t: %s%s\n",
455 diodes, resistors);
458 #else
459 #define ds1305_proc NULL
460 #endif
462 static const struct rtc_class_ops ds1305_ops = {
463 .ioctl = ds1305_ioctl,
464 .read_time = ds1305_get_time,
465 .set_time = ds1305_set_time,
466 .read_alarm = ds1305_get_alarm,
467 .set_alarm = ds1305_set_alarm,
468 .proc = ds1305_proc,
471 static void ds1305_work(struct work_struct *work)
473 struct ds1305 *ds1305 = container_of(work, struct ds1305, work);
474 struct mutex *lock = &ds1305->rtc->ops_lock;
475 struct spi_device *spi = ds1305->spi;
476 u8 buf[3];
477 int status;
479 /* lock to protect ds1305->ctrl */
480 mutex_lock(lock);
482 /* Disable the IRQ, and clear its status ... for now, we "know"
483 * that if more than one alarm is active, they're in sync.
484 * Note that reading ALM data registers also clears IRQ status.
486 ds1305->ctrl[0] &= ~(DS1305_AEI1 | DS1305_AEI0);
487 ds1305->ctrl[1] = 0;
489 buf[0] = DS1305_WRITE | DS1305_CONTROL;
490 buf[1] = ds1305->ctrl[0];
491 buf[2] = 0;
493 status = spi_write_then_read(spi, buf, sizeof buf,
494 NULL, 0);
495 if (status < 0)
496 dev_dbg(&spi->dev, "clear irq --> %d\n", status);
498 mutex_unlock(lock);
500 if (!test_bit(FLAG_EXITING, &ds1305->flags))
501 enable_irq(spi->irq);
503 rtc_update_irq(ds1305->rtc, 1, RTC_AF | RTC_IRQF);
507 * This "real" IRQ handler hands off to a workqueue mostly to allow
508 * mutex locking for ds1305->ctrl ... unlike I2C, we could issue async
509 * I/O requests in IRQ context (to clear the IRQ status).
511 static irqreturn_t ds1305_irq(int irq, void *p)
513 struct ds1305 *ds1305 = p;
515 disable_irq(irq);
516 schedule_work(&ds1305->work);
517 return IRQ_HANDLED;
520 /*----------------------------------------------------------------------*/
523 * Interface for NVRAM
526 static void msg_init(struct spi_message *m, struct spi_transfer *x,
527 u8 *addr, size_t count, char *tx, char *rx)
529 spi_message_init(m);
530 memset(x, 0, 2 * sizeof(*x));
532 x->tx_buf = addr;
533 x->len = 1;
534 spi_message_add_tail(x, m);
536 x++;
538 x->tx_buf = tx;
539 x->rx_buf = rx;
540 x->len = count;
541 spi_message_add_tail(x, m);
544 static ssize_t
545 ds1305_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
546 char *buf, loff_t off, size_t count)
548 struct spi_device *spi;
549 u8 addr;
550 struct spi_message m;
551 struct spi_transfer x[2];
552 int status;
554 spi = container_of(kobj, struct spi_device, dev.kobj);
556 if (unlikely(off >= DS1305_NVRAM_LEN))
557 return 0;
558 if (count >= DS1305_NVRAM_LEN)
559 count = DS1305_NVRAM_LEN;
560 if ((off + count) > DS1305_NVRAM_LEN)
561 count = DS1305_NVRAM_LEN - off;
562 if (unlikely(!count))
563 return count;
565 addr = DS1305_NVRAM + off;
566 msg_init(&m, x, &addr, count, NULL, buf);
568 status = spi_sync(spi, &m);
569 if (status < 0)
570 dev_err(&spi->dev, "nvram %s error %d\n", "read", status);
571 return (status < 0) ? status : count;
574 static ssize_t
575 ds1305_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
576 char *buf, loff_t off, size_t count)
578 struct spi_device *spi;
579 u8 addr;
580 struct spi_message m;
581 struct spi_transfer x[2];
582 int status;
584 spi = container_of(kobj, struct spi_device, dev.kobj);
586 if (unlikely(off >= DS1305_NVRAM_LEN))
587 return -EFBIG;
588 if (count >= DS1305_NVRAM_LEN)
589 count = DS1305_NVRAM_LEN;
590 if ((off + count) > DS1305_NVRAM_LEN)
591 count = DS1305_NVRAM_LEN - off;
592 if (unlikely(!count))
593 return count;
595 addr = (DS1305_WRITE | DS1305_NVRAM) + off;
596 msg_init(&m, x, &addr, count, buf, NULL);
598 status = spi_sync(spi, &m);
599 if (status < 0)
600 dev_err(&spi->dev, "nvram %s error %d\n", "write", status);
601 return (status < 0) ? status : count;
604 static struct bin_attribute nvram = {
605 .attr.name = "nvram",
606 .attr.mode = S_IRUGO | S_IWUSR,
607 .read = ds1305_nvram_read,
608 .write = ds1305_nvram_write,
609 .size = DS1305_NVRAM_LEN,
612 /*----------------------------------------------------------------------*/
615 * Interface to SPI stack
618 static int __devinit ds1305_probe(struct spi_device *spi)
620 struct ds1305 *ds1305;
621 int status;
622 u8 addr, value;
623 struct ds1305_platform_data *pdata = spi->dev.platform_data;
624 bool write_ctrl = false;
626 /* Sanity check board setup data. This may be hooked up
627 * in 3wire mode, but we don't care. Note that unless
628 * there's an inverter in place, this needs SPI_CS_HIGH!
630 if ((spi->bits_per_word && spi->bits_per_word != 8)
631 || (spi->max_speed_hz > 2000000)
632 || !(spi->mode & SPI_CPHA))
633 return -EINVAL;
635 /* set up driver data */
636 ds1305 = kzalloc(sizeof *ds1305, GFP_KERNEL);
637 if (!ds1305)
638 return -ENOMEM;
639 ds1305->spi = spi;
640 spi_set_drvdata(spi, ds1305);
642 /* read and cache control registers */
643 addr = DS1305_CONTROL;
644 status = spi_write_then_read(spi, &addr, sizeof addr,
645 ds1305->ctrl, sizeof ds1305->ctrl);
646 if (status < 0) {
647 dev_dbg(&spi->dev, "can't %s, %d\n",
648 "read", status);
649 goto fail0;
652 dev_dbg(&spi->dev, "ctrl %s: %02x %02x %02x\n",
653 "read", ds1305->ctrl[0],
654 ds1305->ctrl[1], ds1305->ctrl[2]);
656 /* Sanity check register values ... partially compensating for the
657 * fact that SPI has no device handshake. A pullup on MISO would
658 * make these tests fail; but not all systems will have one. If
659 * some register is neither 0x00 nor 0xff, a chip is likely there.
661 if ((ds1305->ctrl[0] & 0x38) != 0 || (ds1305->ctrl[1] & 0xfc) != 0) {
662 dev_dbg(&spi->dev, "RTC chip is not present\n");
663 status = -ENODEV;
664 goto fail0;
666 if (ds1305->ctrl[2] == 0)
667 dev_dbg(&spi->dev, "chip may not be present\n");
669 /* enable writes if needed ... if we were paranoid it would
670 * make sense to enable them only when absolutely necessary.
672 if (ds1305->ctrl[0] & DS1305_WP) {
673 u8 buf[2];
675 ds1305->ctrl[0] &= ~DS1305_WP;
677 buf[0] = DS1305_WRITE | DS1305_CONTROL;
678 buf[1] = ds1305->ctrl[0];
679 status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
681 dev_dbg(&spi->dev, "clear WP --> %d\n", status);
682 if (status < 0)
683 goto fail0;
686 /* on DS1305, maybe start oscillator; like most low power
687 * oscillators, it may take a second to stabilize
689 if (ds1305->ctrl[0] & DS1305_nEOSC) {
690 ds1305->ctrl[0] &= ~DS1305_nEOSC;
691 write_ctrl = true;
692 dev_warn(&spi->dev, "SET TIME!\n");
695 /* ack any pending IRQs */
696 if (ds1305->ctrl[1]) {
697 ds1305->ctrl[1] = 0;
698 write_ctrl = true;
701 /* this may need one-time (re)init */
702 if (pdata) {
703 /* maybe enable trickle charge */
704 if (((ds1305->ctrl[2] & 0xf0) != DS1305_TRICKLE_MAGIC)) {
705 ds1305->ctrl[2] = DS1305_TRICKLE_MAGIC
706 | pdata->trickle;
707 write_ctrl = true;
710 /* on DS1306, configure 1 Hz signal */
711 if (pdata->is_ds1306) {
712 if (pdata->en_1hz) {
713 if (!(ds1305->ctrl[0] & DS1306_1HZ)) {
714 ds1305->ctrl[0] |= DS1306_1HZ;
715 write_ctrl = true;
717 } else {
718 if (ds1305->ctrl[0] & DS1306_1HZ) {
719 ds1305->ctrl[0] &= ~DS1306_1HZ;
720 write_ctrl = true;
726 if (write_ctrl) {
727 u8 buf[4];
729 buf[0] = DS1305_WRITE | DS1305_CONTROL;
730 buf[1] = ds1305->ctrl[0];
731 buf[2] = ds1305->ctrl[1];
732 buf[3] = ds1305->ctrl[2];
733 status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
734 if (status < 0) {
735 dev_dbg(&spi->dev, "can't %s, %d\n",
736 "write", status);
737 goto fail0;
740 dev_dbg(&spi->dev, "ctrl %s: %02x %02x %02x\n",
741 "write", ds1305->ctrl[0],
742 ds1305->ctrl[1], ds1305->ctrl[2]);
745 /* see if non-Linux software set up AM/PM mode */
746 addr = DS1305_HOUR;
747 status = spi_write_then_read(spi, &addr, sizeof addr,
748 &value, sizeof value);
749 if (status < 0) {
750 dev_dbg(&spi->dev, "read HOUR --> %d\n", status);
751 goto fail0;
754 ds1305->hr12 = (DS1305_HR_12 & value) != 0;
755 if (ds1305->hr12)
756 dev_dbg(&spi->dev, "AM/PM\n");
758 /* register RTC ... from here on, ds1305->ctrl needs locking */
759 ds1305->rtc = rtc_device_register("ds1305", &spi->dev,
760 &ds1305_ops, THIS_MODULE);
761 if (IS_ERR(ds1305->rtc)) {
762 status = PTR_ERR(ds1305->rtc);
763 dev_dbg(&spi->dev, "register rtc --> %d\n", status);
764 goto fail0;
767 /* Maybe set up alarm IRQ; be ready to handle it triggering right
768 * away. NOTE that we don't share this. The signal is active low,
769 * and we can't ack it before a SPI message delay. We temporarily
770 * disable the IRQ until it's acked, which lets us work with more
771 * IRQ trigger modes (not all IRQ controllers can do falling edge).
773 if (spi->irq) {
774 INIT_WORK(&ds1305->work, ds1305_work);
775 status = request_irq(spi->irq, ds1305_irq,
776 0, dev_name(&ds1305->rtc->dev), ds1305);
777 if (status < 0) {
778 dev_dbg(&spi->dev, "request_irq %d --> %d\n",
779 spi->irq, status);
780 goto fail1;
783 device_set_wakeup_capable(&spi->dev, 1);
786 /* export NVRAM */
787 status = sysfs_create_bin_file(&spi->dev.kobj, &nvram);
788 if (status < 0) {
789 dev_dbg(&spi->dev, "register nvram --> %d\n", status);
790 goto fail2;
793 return 0;
795 fail2:
796 free_irq(spi->irq, ds1305);
797 fail1:
798 rtc_device_unregister(ds1305->rtc);
799 fail0:
800 kfree(ds1305);
801 return status;
804 static int __devexit ds1305_remove(struct spi_device *spi)
806 struct ds1305 *ds1305 = spi_get_drvdata(spi);
808 sysfs_remove_bin_file(&spi->dev.kobj, &nvram);
810 /* carefully shut down irq and workqueue, if present */
811 if (spi->irq) {
812 set_bit(FLAG_EXITING, &ds1305->flags);
813 free_irq(spi->irq, ds1305);
814 flush_scheduled_work();
817 rtc_device_unregister(ds1305->rtc);
818 spi_set_drvdata(spi, NULL);
819 kfree(ds1305);
820 return 0;
823 static struct spi_driver ds1305_driver = {
824 .driver.name = "rtc-ds1305",
825 .driver.owner = THIS_MODULE,
826 .probe = ds1305_probe,
827 .remove = __devexit_p(ds1305_remove),
828 /* REVISIT add suspend/resume */
831 static int __init ds1305_init(void)
833 return spi_register_driver(&ds1305_driver);
835 module_init(ds1305_init);
837 static void __exit ds1305_exit(void)
839 spi_unregister_driver(&ds1305_driver);
841 module_exit(ds1305_exit);
843 MODULE_DESCRIPTION("RTC driver for DS1305 and DS1306 chips");
844 MODULE_LICENSE("GPL");
845 MODULE_ALIAS("spi:rtc-ds1305");