xref: /openbmc/linux/drivers/rtc/rtc-ds1305.c (revision 4071ea25cc08d41002746cca2d69ac700d67a2ac)
153e84b67SDavid Brownell /*
253e84b67SDavid Brownell  * rtc-ds1305.c -- driver for DS1305 and DS1306 SPI RTC chips
353e84b67SDavid Brownell  *
453e84b67SDavid Brownell  * Copyright (C) 2008 David Brownell
553e84b67SDavid Brownell  *
653e84b67SDavid Brownell  * This program is free software; you can redistribute it and/or modify
753e84b67SDavid Brownell  * it under the terms of the GNU General Public License version 2 as
853e84b67SDavid Brownell  * published by the Free Software Foundation.
953e84b67SDavid Brownell  *
1053e84b67SDavid Brownell  */
1153e84b67SDavid Brownell #include <linux/kernel.h>
1253e84b67SDavid Brownell #include <linux/init.h>
1353e84b67SDavid Brownell #include <linux/bcd.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
1553e84b67SDavid Brownell #include <linux/rtc.h>
1653e84b67SDavid Brownell #include <linux/workqueue.h>
1753e84b67SDavid Brownell 
1853e84b67SDavid Brownell #include <linux/spi/spi.h>
1953e84b67SDavid Brownell #include <linux/spi/ds1305.h>
202113852bSPaul Gortmaker #include <linux/module.h>
2153e84b67SDavid Brownell 
2253e84b67SDavid Brownell 
2353e84b67SDavid Brownell /*
2453e84b67SDavid Brownell  * Registers ... mask DS1305_WRITE into register address to write,
2553e84b67SDavid Brownell  * otherwise you're reading it.  All non-bitmask values are BCD.
2653e84b67SDavid Brownell  */
2753e84b67SDavid Brownell #define DS1305_WRITE		0x80
2853e84b67SDavid Brownell 
2953e84b67SDavid Brownell 
3053e84b67SDavid Brownell /* RTC date/time ... the main special cases are that we:
3153e84b67SDavid Brownell  *  - Need fancy "hours" encoding in 12hour mode
3253e84b67SDavid Brownell  *  - Don't rely on the "day-of-week" field (or tm_wday)
3353e84b67SDavid Brownell  *  - Are a 21st-century clock (2000 <= year < 2100)
3453e84b67SDavid Brownell  */
3553e84b67SDavid Brownell #define DS1305_RTC_LEN		7		/* bytes for RTC regs */
3653e84b67SDavid Brownell 
3753e84b67SDavid Brownell #define DS1305_SEC		0x00		/* register addresses */
3853e84b67SDavid Brownell #define DS1305_MIN		0x01
3953e84b67SDavid Brownell #define DS1305_HOUR		0x02
4053e84b67SDavid Brownell #	define DS1305_HR_12		0x40	/* set == 12 hr mode */
4153e84b67SDavid Brownell #	define DS1305_HR_PM		0x20	/* set == PM (12hr mode) */
4253e84b67SDavid Brownell #define DS1305_WDAY		0x03
4353e84b67SDavid Brownell #define DS1305_MDAY		0x04
4453e84b67SDavid Brownell #define DS1305_MON		0x05
4553e84b67SDavid Brownell #define DS1305_YEAR		0x06
4653e84b67SDavid Brownell 
4753e84b67SDavid Brownell 
4853e84b67SDavid Brownell /* The two alarms have only sec/min/hour/wday fields (ALM_LEN).
4953e84b67SDavid Brownell  * DS1305_ALM_DISABLE disables a match field (some combos are bad).
5053e84b67SDavid Brownell  *
5153e84b67SDavid Brownell  * NOTE that since we don't use WDAY, we limit ourselves to alarms
5253e84b67SDavid Brownell  * only one day into the future (vs potentially up to a week).
5353e84b67SDavid Brownell  *
5453e84b67SDavid Brownell  * NOTE ALSO that while we could generate once-a-second IRQs (UIE), we
5553e84b67SDavid Brownell  * don't currently support them.  We'd either need to do it only when
5653e84b67SDavid Brownell  * no alarm is pending (not the standard model), or to use the second
5753e84b67SDavid Brownell  * alarm (implying that this is a DS1305 not DS1306, *and* that either
5853e84b67SDavid Brownell  * it's wired up a second IRQ we know, or that INTCN is set)
5953e84b67SDavid Brownell  */
6053e84b67SDavid Brownell #define DS1305_ALM_LEN		4		/* bytes for ALM regs */
6153e84b67SDavid Brownell #define DS1305_ALM_DISABLE	0x80
6253e84b67SDavid Brownell 
6353e84b67SDavid Brownell #define DS1305_ALM0(r)		(0x07 + (r))	/* register addresses */
6453e84b67SDavid Brownell #define DS1305_ALM1(r)		(0x0b + (r))
6553e84b67SDavid Brownell 
6653e84b67SDavid Brownell 
6753e84b67SDavid Brownell /* three control registers */
6853e84b67SDavid Brownell #define DS1305_CONTROL_LEN	3		/* bytes of control regs */
6953e84b67SDavid Brownell 
7053e84b67SDavid Brownell #define DS1305_CONTROL		0x0f		/* register addresses */
7153e84b67SDavid Brownell #	define DS1305_nEOSC		0x80	/* low enables oscillator */
7253e84b67SDavid Brownell #	define DS1305_WP		0x40	/* write protect */
7353e84b67SDavid Brownell #	define DS1305_INTCN		0x04	/* clear == only int0 used */
7453e84b67SDavid Brownell #	define DS1306_1HZ		0x04	/* enable 1Hz output */
7553e84b67SDavid Brownell #	define DS1305_AEI1		0x02	/* enable ALM1 IRQ */
7653e84b67SDavid Brownell #	define DS1305_AEI0		0x01	/* enable ALM0 IRQ */
7753e84b67SDavid Brownell #define DS1305_STATUS		0x10
7853e84b67SDavid Brownell /* status has just AEIx bits, mirrored as IRQFx */
7953e84b67SDavid Brownell #define DS1305_TRICKLE		0x11
8053e84b67SDavid Brownell /* trickle bits are defined in <linux/spi/ds1305.h> */
8153e84b67SDavid Brownell 
8253e84b67SDavid Brownell /* a bunch of NVRAM */
8353e84b67SDavid Brownell #define DS1305_NVRAM_LEN	96		/* bytes of NVRAM */
8453e84b67SDavid Brownell 
8553e84b67SDavid Brownell #define DS1305_NVRAM		0x20		/* register addresses */
8653e84b67SDavid Brownell 
8753e84b67SDavid Brownell 
8853e84b67SDavid Brownell struct ds1305 {
8953e84b67SDavid Brownell 	struct spi_device	*spi;
9053e84b67SDavid Brownell 	struct rtc_device	*rtc;
9153e84b67SDavid Brownell 
9253e84b67SDavid Brownell 	struct work_struct	work;
9353e84b67SDavid Brownell 
9453e84b67SDavid Brownell 	unsigned long		flags;
9553e84b67SDavid Brownell #define FLAG_EXITING	0
9653e84b67SDavid Brownell 
9753e84b67SDavid Brownell 	bool			hr12;
9853e84b67SDavid Brownell 	u8			ctrl[DS1305_CONTROL_LEN];
9953e84b67SDavid Brownell };
10053e84b67SDavid Brownell 
10153e84b67SDavid Brownell 
10253e84b67SDavid Brownell /*----------------------------------------------------------------------*/
10353e84b67SDavid Brownell 
10453e84b67SDavid Brownell /*
10553e84b67SDavid Brownell  * Utilities ...  tolerate 12-hour AM/PM notation in case of non-Linux
10653e84b67SDavid Brownell  * software (like a bootloader) which may require it.
10753e84b67SDavid Brownell  */
10853e84b67SDavid Brownell 
10953e84b67SDavid Brownell static unsigned bcd2hour(u8 bcd)
11053e84b67SDavid Brownell {
11153e84b67SDavid Brownell 	if (bcd & DS1305_HR_12) {
11253e84b67SDavid Brownell 		unsigned	hour = 0;
11353e84b67SDavid Brownell 
11453e84b67SDavid Brownell 		bcd &= ~DS1305_HR_12;
11553e84b67SDavid Brownell 		if (bcd & DS1305_HR_PM) {
11653e84b67SDavid Brownell 			hour = 12;
11753e84b67SDavid Brownell 			bcd &= ~DS1305_HR_PM;
11853e84b67SDavid Brownell 		}
119fe20ba70SAdrian Bunk 		hour += bcd2bin(bcd);
12053e84b67SDavid Brownell 		return hour - 1;
12153e84b67SDavid Brownell 	}
122fe20ba70SAdrian Bunk 	return bcd2bin(bcd);
12353e84b67SDavid Brownell }
12453e84b67SDavid Brownell 
12553e84b67SDavid Brownell static u8 hour2bcd(bool hr12, int hour)
12653e84b67SDavid Brownell {
12753e84b67SDavid Brownell 	if (hr12) {
12853e84b67SDavid Brownell 		hour++;
12953e84b67SDavid Brownell 		if (hour <= 12)
130fe20ba70SAdrian Bunk 			return DS1305_HR_12 | bin2bcd(hour);
13153e84b67SDavid Brownell 		hour -= 12;
132fe20ba70SAdrian Bunk 		return DS1305_HR_12 | DS1305_HR_PM | bin2bcd(hour);
13353e84b67SDavid Brownell 	}
134fe20ba70SAdrian Bunk 	return bin2bcd(hour);
13553e84b67SDavid Brownell }
13653e84b67SDavid Brownell 
13753e84b67SDavid Brownell /*----------------------------------------------------------------------*/
13853e84b67SDavid Brownell 
13953e84b67SDavid Brownell /*
14053e84b67SDavid Brownell  * Interface to RTC framework
14153e84b67SDavid Brownell  */
14253e84b67SDavid Brownell 
14316380c15SJohn Stultz static int ds1305_alarm_irq_enable(struct device *dev, unsigned int enabled)
14453e84b67SDavid Brownell {
14553e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
14653e84b67SDavid Brownell 	u8		buf[2];
14716380c15SJohn Stultz 	long		err = -EINVAL;
14853e84b67SDavid Brownell 
14953e84b67SDavid Brownell 	buf[0] = DS1305_WRITE | DS1305_CONTROL;
15053e84b67SDavid Brownell 	buf[1] = ds1305->ctrl[0];
15153e84b67SDavid Brownell 
15216380c15SJohn Stultz 	if (enabled) {
15353e84b67SDavid Brownell 		if (ds1305->ctrl[0] & DS1305_AEI0)
15453e84b67SDavid Brownell 			goto done;
15553e84b67SDavid Brownell 		buf[1] |= DS1305_AEI0;
15616380c15SJohn Stultz 	} else {
15716380c15SJohn Stultz 		if (!(buf[1] & DS1305_AEI0))
15816380c15SJohn Stultz 			goto done;
15916380c15SJohn Stultz 		buf[1] &= ~DS1305_AEI0;
16053e84b67SDavid Brownell 	}
161465008faSSachin Kamat 	err = spi_write_then_read(ds1305->spi, buf, sizeof(buf), NULL, 0);
16216380c15SJohn Stultz 	if (err >= 0)
16353e84b67SDavid Brownell 		ds1305->ctrl[0] = buf[1];
16453e84b67SDavid Brownell done:
16516380c15SJohn Stultz 	return err;
16616380c15SJohn Stultz 
16753e84b67SDavid Brownell }
16853e84b67SDavid Brownell 
16953e84b67SDavid Brownell 
17053e84b67SDavid Brownell /*
17153e84b67SDavid Brownell  * Get/set of date and time is pretty normal.
17253e84b67SDavid Brownell  */
17353e84b67SDavid Brownell 
17453e84b67SDavid Brownell static int ds1305_get_time(struct device *dev, struct rtc_time *time)
17553e84b67SDavid Brownell {
17653e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
17753e84b67SDavid Brownell 	u8		addr = DS1305_SEC;
17853e84b67SDavid Brownell 	u8		buf[DS1305_RTC_LEN];
17953e84b67SDavid Brownell 	int		status;
18053e84b67SDavid Brownell 
18153e84b67SDavid Brownell 	/* Use write-then-read to get all the date/time registers
18253e84b67SDavid Brownell 	 * since dma from stack is nonportable
18353e84b67SDavid Brownell 	 */
184465008faSSachin Kamat 	status = spi_write_then_read(ds1305->spi, &addr, sizeof(addr),
185465008faSSachin Kamat 			buf, sizeof(buf));
18653e84b67SDavid Brownell 	if (status < 0)
18753e84b67SDavid Brownell 		return status;
18853e84b67SDavid Brownell 
18953e84b67SDavid Brownell 	dev_vdbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
19053e84b67SDavid Brownell 		"read", buf[0], buf[1], buf[2], buf[3],
19153e84b67SDavid Brownell 		buf[4], buf[5], buf[6]);
19253e84b67SDavid Brownell 
19353e84b67SDavid Brownell 	/* Decode the registers */
194fe20ba70SAdrian Bunk 	time->tm_sec = bcd2bin(buf[DS1305_SEC]);
195fe20ba70SAdrian Bunk 	time->tm_min = bcd2bin(buf[DS1305_MIN]);
19653e84b67SDavid Brownell 	time->tm_hour = bcd2hour(buf[DS1305_HOUR]);
19753e84b67SDavid Brownell 	time->tm_wday = buf[DS1305_WDAY] - 1;
198fe20ba70SAdrian Bunk 	time->tm_mday = bcd2bin(buf[DS1305_MDAY]);
199fe20ba70SAdrian Bunk 	time->tm_mon = bcd2bin(buf[DS1305_MON]) - 1;
200fe20ba70SAdrian Bunk 	time->tm_year = bcd2bin(buf[DS1305_YEAR]) + 100;
20153e84b67SDavid Brownell 
20253e84b67SDavid Brownell 	dev_vdbg(dev, "%s secs=%d, mins=%d, "
20353e84b67SDavid Brownell 		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
20453e84b67SDavid Brownell 		"read", time->tm_sec, time->tm_min,
20553e84b67SDavid Brownell 		time->tm_hour, time->tm_mday,
20653e84b67SDavid Brownell 		time->tm_mon, time->tm_year, time->tm_wday);
20753e84b67SDavid Brownell 
20853e84b67SDavid Brownell 	/* Time may not be set */
20953e84b67SDavid Brownell 	return rtc_valid_tm(time);
21053e84b67SDavid Brownell }
21153e84b67SDavid Brownell 
21253e84b67SDavid Brownell static int ds1305_set_time(struct device *dev, struct rtc_time *time)
21353e84b67SDavid Brownell {
21453e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
21553e84b67SDavid Brownell 	u8		buf[1 + DS1305_RTC_LEN];
21653e84b67SDavid Brownell 	u8		*bp = buf;
21753e84b67SDavid Brownell 
21853e84b67SDavid Brownell 	dev_vdbg(dev, "%s secs=%d, mins=%d, "
21953e84b67SDavid Brownell 		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
22053e84b67SDavid Brownell 		"write", time->tm_sec, time->tm_min,
22153e84b67SDavid Brownell 		time->tm_hour, time->tm_mday,
22253e84b67SDavid Brownell 		time->tm_mon, time->tm_year, time->tm_wday);
22353e84b67SDavid Brownell 
22453e84b67SDavid Brownell 	/* Write registers starting at the first time/date address. */
22553e84b67SDavid Brownell 	*bp++ = DS1305_WRITE | DS1305_SEC;
22653e84b67SDavid Brownell 
227fe20ba70SAdrian Bunk 	*bp++ = bin2bcd(time->tm_sec);
228fe20ba70SAdrian Bunk 	*bp++ = bin2bcd(time->tm_min);
22953e84b67SDavid Brownell 	*bp++ = hour2bcd(ds1305->hr12, time->tm_hour);
23053e84b67SDavid Brownell 	*bp++ = (time->tm_wday < 7) ? (time->tm_wday + 1) : 1;
231fe20ba70SAdrian Bunk 	*bp++ = bin2bcd(time->tm_mday);
232fe20ba70SAdrian Bunk 	*bp++ = bin2bcd(time->tm_mon + 1);
233fe20ba70SAdrian Bunk 	*bp++ = bin2bcd(time->tm_year - 100);
23453e84b67SDavid Brownell 
23553e84b67SDavid Brownell 	dev_dbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
23653e84b67SDavid Brownell 		"write", buf[1], buf[2], buf[3],
23753e84b67SDavid Brownell 		buf[4], buf[5], buf[6], buf[7]);
23853e84b67SDavid Brownell 
23953e84b67SDavid Brownell 	/* use write-then-read since dma from stack is nonportable */
240465008faSSachin Kamat 	return spi_write_then_read(ds1305->spi, buf, sizeof(buf),
24153e84b67SDavid Brownell 			NULL, 0);
24253e84b67SDavid Brownell }
24353e84b67SDavid Brownell 
24453e84b67SDavid Brownell /*
24553e84b67SDavid Brownell  * Get/set of alarm is a bit funky:
24653e84b67SDavid Brownell  *
24753e84b67SDavid Brownell  * - First there's the inherent raciness of getting the (partitioned)
24853e84b67SDavid Brownell  *   status of an alarm that could trigger while we're reading parts
24953e84b67SDavid Brownell  *   of that status.
25053e84b67SDavid Brownell  *
25153e84b67SDavid Brownell  * - Second there's its limited range (we could increase it a bit by
25253e84b67SDavid Brownell  *   relying on WDAY), which means it will easily roll over.
25353e84b67SDavid Brownell  *
25453e84b67SDavid Brownell  * - Third there's the choice of two alarms and alarm signals.
25553e84b67SDavid Brownell  *   Here we use ALM0 and expect that nINT0 (open drain) is used;
25653e84b67SDavid Brownell  *   that's the only real option for DS1306 runtime alarms, and is
25753e84b67SDavid Brownell  *   natural on DS1305.
25853e84b67SDavid Brownell  *
25953e84b67SDavid Brownell  * - Fourth, there's also ALM1, and a second interrupt signal:
26053e84b67SDavid Brownell  *     + On DS1305 ALM1 uses nINT1 (when INTCN=1) else nINT0;
26153e84b67SDavid Brownell  *     + On DS1306 ALM1 only uses INT1 (an active high pulse)
26253e84b67SDavid Brownell  *       and it won't work when VCC1 is active.
26353e84b67SDavid Brownell  *
26453e84b67SDavid Brownell  *   So to be most general, we should probably set both alarms to the
26553e84b67SDavid Brownell  *   same value, letting ALM1 be the wakeup event source on DS1306
26653e84b67SDavid Brownell  *   and handling several wiring options on DS1305.
26753e84b67SDavid Brownell  *
26853e84b67SDavid Brownell  * - Fifth, we support the polled mode (as well as possible; why not?)
26953e84b67SDavid Brownell  *   even when no interrupt line is wired to an IRQ.
27053e84b67SDavid Brownell  */
27153e84b67SDavid Brownell 
27253e84b67SDavid Brownell /*
27353e84b67SDavid Brownell  * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
27453e84b67SDavid Brownell  */
27553e84b67SDavid Brownell static int ds1305_get_alarm(struct device *dev, struct rtc_wkalrm *alm)
27653e84b67SDavid Brownell {
27753e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
27853e84b67SDavid Brownell 	struct spi_device *spi = ds1305->spi;
27953e84b67SDavid Brownell 	u8		addr;
28053e84b67SDavid Brownell 	int		status;
28153e84b67SDavid Brownell 	u8		buf[DS1305_ALM_LEN];
28253e84b67SDavid Brownell 
28353e84b67SDavid Brownell 	/* Refresh control register cache BEFORE reading ALM0 registers,
28453e84b67SDavid Brownell 	 * since reading alarm registers acks any pending IRQ.  That
28553e84b67SDavid Brownell 	 * makes returning "pending" status a bit of a lie, but that bit
28653e84b67SDavid Brownell 	 * of EFI status is at best fragile anyway (given IRQ handlers).
28753e84b67SDavid Brownell 	 */
28853e84b67SDavid Brownell 	addr = DS1305_CONTROL;
289465008faSSachin Kamat 	status = spi_write_then_read(spi, &addr, sizeof(addr),
290465008faSSachin Kamat 			ds1305->ctrl, sizeof(ds1305->ctrl));
29153e84b67SDavid Brownell 	if (status < 0)
29253e84b67SDavid Brownell 		return status;
29353e84b67SDavid Brownell 
29453e84b67SDavid Brownell 	alm->enabled = !!(ds1305->ctrl[0] & DS1305_AEI0);
29553e84b67SDavid Brownell 	alm->pending = !!(ds1305->ctrl[1] & DS1305_AEI0);
29653e84b67SDavid Brownell 
29753e84b67SDavid Brownell 	/* get and check ALM0 registers */
29853e84b67SDavid Brownell 	addr = DS1305_ALM0(DS1305_SEC);
299465008faSSachin Kamat 	status = spi_write_then_read(spi, &addr, sizeof(addr),
300465008faSSachin Kamat 			buf, sizeof(buf));
30153e84b67SDavid Brownell 	if (status < 0)
30253e84b67SDavid Brownell 		return status;
30353e84b67SDavid Brownell 
30453e84b67SDavid Brownell 	dev_vdbg(dev, "%s: %02x %02x %02x %02x\n",
30553e84b67SDavid Brownell 		"alm0 read", buf[DS1305_SEC], buf[DS1305_MIN],
30653e84b67SDavid Brownell 		buf[DS1305_HOUR], buf[DS1305_WDAY]);
30753e84b67SDavid Brownell 
30853e84b67SDavid Brownell 	if ((DS1305_ALM_DISABLE & buf[DS1305_SEC])
30953e84b67SDavid Brownell 			|| (DS1305_ALM_DISABLE & buf[DS1305_MIN])
31053e84b67SDavid Brownell 			|| (DS1305_ALM_DISABLE & buf[DS1305_HOUR]))
31153e84b67SDavid Brownell 		return -EIO;
31253e84b67SDavid Brownell 
31353e84b67SDavid Brownell 	/* Stuff these values into alm->time and let RTC framework code
31453e84b67SDavid Brownell 	 * fill in the rest ... and also handle rollover to tomorrow when
31553e84b67SDavid Brownell 	 * that's needed.
31653e84b67SDavid Brownell 	 */
317fe20ba70SAdrian Bunk 	alm->time.tm_sec = bcd2bin(buf[DS1305_SEC]);
318fe20ba70SAdrian Bunk 	alm->time.tm_min = bcd2bin(buf[DS1305_MIN]);
31953e84b67SDavid Brownell 	alm->time.tm_hour = bcd2hour(buf[DS1305_HOUR]);
32053e84b67SDavid Brownell 	alm->time.tm_mday = -1;
32153e84b67SDavid Brownell 	alm->time.tm_mon = -1;
32253e84b67SDavid Brownell 	alm->time.tm_year = -1;
32353e84b67SDavid Brownell 	/* next three fields are unused by Linux */
32453e84b67SDavid Brownell 	alm->time.tm_wday = -1;
32553e84b67SDavid Brownell 	alm->time.tm_mday = -1;
32653e84b67SDavid Brownell 	alm->time.tm_isdst = -1;
32753e84b67SDavid Brownell 
32853e84b67SDavid Brownell 	return 0;
32953e84b67SDavid Brownell }
33053e84b67SDavid Brownell 
33153e84b67SDavid Brownell /*
33253e84b67SDavid Brownell  * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
33353e84b67SDavid Brownell  */
33453e84b67SDavid Brownell static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
33553e84b67SDavid Brownell {
33653e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
33753e84b67SDavid Brownell 	struct spi_device *spi = ds1305->spi;
33853e84b67SDavid Brownell 	unsigned long	now, later;
33953e84b67SDavid Brownell 	struct rtc_time	tm;
34053e84b67SDavid Brownell 	int		status;
34153e84b67SDavid Brownell 	u8		buf[1 + DS1305_ALM_LEN];
34253e84b67SDavid Brownell 
34353e84b67SDavid Brownell 	/* convert desired alarm to time_t */
34453e84b67SDavid Brownell 	status = rtc_tm_to_time(&alm->time, &later);
34553e84b67SDavid Brownell 	if (status < 0)
34653e84b67SDavid Brownell 		return status;
34753e84b67SDavid Brownell 
34853e84b67SDavid Brownell 	/* Read current time as time_t */
34953e84b67SDavid Brownell 	status = ds1305_get_time(dev, &tm);
35053e84b67SDavid Brownell 	if (status < 0)
35153e84b67SDavid Brownell 		return status;
35253e84b67SDavid Brownell 	status = rtc_tm_to_time(&tm, &now);
35353e84b67SDavid Brownell 	if (status < 0)
35453e84b67SDavid Brownell 		return status;
35553e84b67SDavid Brownell 
35653e84b67SDavid Brownell 	/* make sure alarm fires within the next 24 hours */
35753e84b67SDavid Brownell 	if (later <= now)
35853e84b67SDavid Brownell 		return -EINVAL;
35953e84b67SDavid Brownell 	if ((later - now) > 24 * 60 * 60)
36053e84b67SDavid Brownell 		return -EDOM;
36153e84b67SDavid Brownell 
36253e84b67SDavid Brownell 	/* disable alarm if needed */
36353e84b67SDavid Brownell 	if (ds1305->ctrl[0] & DS1305_AEI0) {
36453e84b67SDavid Brownell 		ds1305->ctrl[0] &= ~DS1305_AEI0;
36553e84b67SDavid Brownell 
36653e84b67SDavid Brownell 		buf[0] = DS1305_WRITE | DS1305_CONTROL;
36753e84b67SDavid Brownell 		buf[1] = ds1305->ctrl[0];
36853e84b67SDavid Brownell 		status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
36953e84b67SDavid Brownell 		if (status < 0)
37053e84b67SDavid Brownell 			return status;
37153e84b67SDavid Brownell 	}
37253e84b67SDavid Brownell 
37353e84b67SDavid Brownell 	/* write alarm */
37453e84b67SDavid Brownell 	buf[0] = DS1305_WRITE | DS1305_ALM0(DS1305_SEC);
375fe20ba70SAdrian Bunk 	buf[1 + DS1305_SEC] = bin2bcd(alm->time.tm_sec);
376fe20ba70SAdrian Bunk 	buf[1 + DS1305_MIN] = bin2bcd(alm->time.tm_min);
37753e84b67SDavid Brownell 	buf[1 + DS1305_HOUR] = hour2bcd(ds1305->hr12, alm->time.tm_hour);
37853e84b67SDavid Brownell 	buf[1 + DS1305_WDAY] = DS1305_ALM_DISABLE;
37953e84b67SDavid Brownell 
38053e84b67SDavid Brownell 	dev_dbg(dev, "%s: %02x %02x %02x %02x\n",
38153e84b67SDavid Brownell 		"alm0 write", buf[1 + DS1305_SEC], buf[1 + DS1305_MIN],
38253e84b67SDavid Brownell 		buf[1 + DS1305_HOUR], buf[1 + DS1305_WDAY]);
38353e84b67SDavid Brownell 
384465008faSSachin Kamat 	status = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0);
38553e84b67SDavid Brownell 	if (status < 0)
38653e84b67SDavid Brownell 		return status;
38753e84b67SDavid Brownell 
38853e84b67SDavid Brownell 	/* enable alarm if requested */
38953e84b67SDavid Brownell 	if (alm->enabled) {
39053e84b67SDavid Brownell 		ds1305->ctrl[0] |= DS1305_AEI0;
39153e84b67SDavid Brownell 
39253e84b67SDavid Brownell 		buf[0] = DS1305_WRITE | DS1305_CONTROL;
39353e84b67SDavid Brownell 		buf[1] = ds1305->ctrl[0];
39453e84b67SDavid Brownell 		status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
39553e84b67SDavid Brownell 	}
39653e84b67SDavid Brownell 
39753e84b67SDavid Brownell 	return status;
39853e84b67SDavid Brownell }
39953e84b67SDavid Brownell 
40053e84b67SDavid Brownell #ifdef CONFIG_PROC_FS
40153e84b67SDavid Brownell 
40253e84b67SDavid Brownell static int ds1305_proc(struct device *dev, struct seq_file *seq)
40353e84b67SDavid Brownell {
40453e84b67SDavid Brownell 	struct ds1305	*ds1305 = dev_get_drvdata(dev);
40553e84b67SDavid Brownell 	char		*diodes = "no";
40653e84b67SDavid Brownell 	char		*resistors = "";
40753e84b67SDavid Brownell 
40853e84b67SDavid Brownell 	/* ctrl[2] is treated as read-only; no locking needed */
40953e84b67SDavid Brownell 	if ((ds1305->ctrl[2] & 0xf0) == DS1305_TRICKLE_MAGIC) {
41053e84b67SDavid Brownell 		switch (ds1305->ctrl[2] & 0x0c) {
41153e84b67SDavid Brownell 		case DS1305_TRICKLE_DS2:
41253e84b67SDavid Brownell 			diodes = "2 diodes, ";
41353e84b67SDavid Brownell 			break;
41453e84b67SDavid Brownell 		case DS1305_TRICKLE_DS1:
41553e84b67SDavid Brownell 			diodes = "1 diode, ";
41653e84b67SDavid Brownell 			break;
41753e84b67SDavid Brownell 		default:
41853e84b67SDavid Brownell 			goto done;
41953e84b67SDavid Brownell 		}
42053e84b67SDavid Brownell 		switch (ds1305->ctrl[2] & 0x03) {
42153e84b67SDavid Brownell 		case DS1305_TRICKLE_2K:
42253e84b67SDavid Brownell 			resistors = "2k Ohm";
42353e84b67SDavid Brownell 			break;
42453e84b67SDavid Brownell 		case DS1305_TRICKLE_4K:
42553e84b67SDavid Brownell 			resistors = "4k Ohm";
42653e84b67SDavid Brownell 			break;
42753e84b67SDavid Brownell 		case DS1305_TRICKLE_8K:
42853e84b67SDavid Brownell 			resistors = "8k Ohm";
42953e84b67SDavid Brownell 			break;
43053e84b67SDavid Brownell 		default:
43153e84b67SDavid Brownell 			diodes = "no";
43253e84b67SDavid Brownell 			break;
43353e84b67SDavid Brownell 		}
43453e84b67SDavid Brownell 	}
43553e84b67SDavid Brownell 
43653e84b67SDavid Brownell done:
43753e84b67SDavid Brownell 	return seq_printf(seq,
43853e84b67SDavid Brownell 			"trickle_charge\t: %s%s\n",
43953e84b67SDavid Brownell 			diodes, resistors);
44053e84b67SDavid Brownell }
44153e84b67SDavid Brownell 
44253e84b67SDavid Brownell #else
44353e84b67SDavid Brownell #define ds1305_proc	NULL
44453e84b67SDavid Brownell #endif
44553e84b67SDavid Brownell 
44653e84b67SDavid Brownell static const struct rtc_class_ops ds1305_ops = {
44753e84b67SDavid Brownell 	.read_time	= ds1305_get_time,
44853e84b67SDavid Brownell 	.set_time	= ds1305_set_time,
44953e84b67SDavid Brownell 	.read_alarm	= ds1305_get_alarm,
45053e84b67SDavid Brownell 	.set_alarm	= ds1305_set_alarm,
45153e84b67SDavid Brownell 	.proc		= ds1305_proc,
45216380c15SJohn Stultz 	.alarm_irq_enable = ds1305_alarm_irq_enable,
45353e84b67SDavid Brownell };
45453e84b67SDavid Brownell 
45553e84b67SDavid Brownell static void ds1305_work(struct work_struct *work)
45653e84b67SDavid Brownell {
45753e84b67SDavid Brownell 	struct ds1305	*ds1305 = container_of(work, struct ds1305, work);
45853e84b67SDavid Brownell 	struct mutex	*lock = &ds1305->rtc->ops_lock;
45953e84b67SDavid Brownell 	struct spi_device *spi = ds1305->spi;
46053e84b67SDavid Brownell 	u8		buf[3];
46153e84b67SDavid Brownell 	int		status;
46253e84b67SDavid Brownell 
46353e84b67SDavid Brownell 	/* lock to protect ds1305->ctrl */
46453e84b67SDavid Brownell 	mutex_lock(lock);
46553e84b67SDavid Brownell 
46653e84b67SDavid Brownell 	/* Disable the IRQ, and clear its status ... for now, we "know"
46753e84b67SDavid Brownell 	 * that if more than one alarm is active, they're in sync.
46853e84b67SDavid Brownell 	 * Note that reading ALM data registers also clears IRQ status.
46953e84b67SDavid Brownell 	 */
47053e84b67SDavid Brownell 	ds1305->ctrl[0] &= ~(DS1305_AEI1 | DS1305_AEI0);
47153e84b67SDavid Brownell 	ds1305->ctrl[1] = 0;
47253e84b67SDavid Brownell 
47353e84b67SDavid Brownell 	buf[0] = DS1305_WRITE | DS1305_CONTROL;
47453e84b67SDavid Brownell 	buf[1] = ds1305->ctrl[0];
47553e84b67SDavid Brownell 	buf[2] = 0;
47653e84b67SDavid Brownell 
477465008faSSachin Kamat 	status = spi_write_then_read(spi, buf, sizeof(buf),
47853e84b67SDavid Brownell 			NULL, 0);
47953e84b67SDavid Brownell 	if (status < 0)
48053e84b67SDavid Brownell 		dev_dbg(&spi->dev, "clear irq --> %d\n", status);
48153e84b67SDavid Brownell 
48253e84b67SDavid Brownell 	mutex_unlock(lock);
48353e84b67SDavid Brownell 
48453e84b67SDavid Brownell 	if (!test_bit(FLAG_EXITING, &ds1305->flags))
48553e84b67SDavid Brownell 		enable_irq(spi->irq);
48653e84b67SDavid Brownell 
48753e84b67SDavid Brownell 	rtc_update_irq(ds1305->rtc, 1, RTC_AF | RTC_IRQF);
48853e84b67SDavid Brownell }
48953e84b67SDavid Brownell 
49053e84b67SDavid Brownell /*
49153e84b67SDavid Brownell  * This "real" IRQ handler hands off to a workqueue mostly to allow
49253e84b67SDavid Brownell  * mutex locking for ds1305->ctrl ... unlike I2C, we could issue async
49353e84b67SDavid Brownell  * I/O requests in IRQ context (to clear the IRQ status).
49453e84b67SDavid Brownell  */
49553e84b67SDavid Brownell static irqreturn_t ds1305_irq(int irq, void *p)
49653e84b67SDavid Brownell {
49753e84b67SDavid Brownell 	struct ds1305		*ds1305 = p;
49853e84b67SDavid Brownell 
49953e84b67SDavid Brownell 	disable_irq(irq);
50053e84b67SDavid Brownell 	schedule_work(&ds1305->work);
50153e84b67SDavid Brownell 	return IRQ_HANDLED;
50253e84b67SDavid Brownell }
50353e84b67SDavid Brownell 
50453e84b67SDavid Brownell /*----------------------------------------------------------------------*/
50553e84b67SDavid Brownell 
50653e84b67SDavid Brownell /*
50753e84b67SDavid Brownell  * Interface for NVRAM
50853e84b67SDavid Brownell  */
50953e84b67SDavid Brownell 
51053e84b67SDavid Brownell static void msg_init(struct spi_message *m, struct spi_transfer *x,
51153e84b67SDavid Brownell 		u8 *addr, size_t count, char *tx, char *rx)
51253e84b67SDavid Brownell {
51353e84b67SDavid Brownell 	spi_message_init(m);
51453e84b67SDavid Brownell 	memset(x, 0, 2 * sizeof(*x));
51553e84b67SDavid Brownell 
51653e84b67SDavid Brownell 	x->tx_buf = addr;
51753e84b67SDavid Brownell 	x->len = 1;
51853e84b67SDavid Brownell 	spi_message_add_tail(x, m);
51953e84b67SDavid Brownell 
52053e84b67SDavid Brownell 	x++;
52153e84b67SDavid Brownell 
52253e84b67SDavid Brownell 	x->tx_buf = tx;
52353e84b67SDavid Brownell 	x->rx_buf = rx;
52453e84b67SDavid Brownell 	x->len = count;
52553e84b67SDavid Brownell 	spi_message_add_tail(x, m);
52653e84b67SDavid Brownell }
52753e84b67SDavid Brownell 
52853e84b67SDavid Brownell static ssize_t
5292c3c8beaSChris Wright ds1305_nvram_read(struct file *filp, struct kobject *kobj,
5302c3c8beaSChris Wright 		struct bin_attribute *attr,
53153e84b67SDavid Brownell 		char *buf, loff_t off, size_t count)
53253e84b67SDavid Brownell {
53353e84b67SDavid Brownell 	struct spi_device	*spi;
53453e84b67SDavid Brownell 	u8			addr;
53553e84b67SDavid Brownell 	struct spi_message	m;
53653e84b67SDavid Brownell 	struct spi_transfer	x[2];
53753e84b67SDavid Brownell 	int			status;
53853e84b67SDavid Brownell 
53953e84b67SDavid Brownell 	spi = container_of(kobj, struct spi_device, dev.kobj);
54053e84b67SDavid Brownell 
54153e84b67SDavid Brownell 	if (unlikely(off >= DS1305_NVRAM_LEN))
54253e84b67SDavid Brownell 		return 0;
54353e84b67SDavid Brownell 	if (count >= DS1305_NVRAM_LEN)
54453e84b67SDavid Brownell 		count = DS1305_NVRAM_LEN;
54553e84b67SDavid Brownell 	if ((off + count) > DS1305_NVRAM_LEN)
54653e84b67SDavid Brownell 		count = DS1305_NVRAM_LEN - off;
54753e84b67SDavid Brownell 	if (unlikely(!count))
54853e84b67SDavid Brownell 		return count;
54953e84b67SDavid Brownell 
55053e84b67SDavid Brownell 	addr = DS1305_NVRAM + off;
55153e84b67SDavid Brownell 	msg_init(&m, x, &addr, count, NULL, buf);
55253e84b67SDavid Brownell 
55353e84b67SDavid Brownell 	status = spi_sync(spi, &m);
55453e84b67SDavid Brownell 	if (status < 0)
55553e84b67SDavid Brownell 		dev_err(&spi->dev, "nvram %s error %d\n", "read", status);
55653e84b67SDavid Brownell 	return (status < 0) ? status : count;
55753e84b67SDavid Brownell }
55853e84b67SDavid Brownell 
55953e84b67SDavid Brownell static ssize_t
5602c3c8beaSChris Wright ds1305_nvram_write(struct file *filp, struct kobject *kobj,
5612c3c8beaSChris Wright 		struct bin_attribute *attr,
56253e84b67SDavid Brownell 		char *buf, loff_t off, size_t count)
56353e84b67SDavid Brownell {
56453e84b67SDavid Brownell 	struct spi_device	*spi;
56553e84b67SDavid Brownell 	u8			addr;
56653e84b67SDavid Brownell 	struct spi_message	m;
56753e84b67SDavid Brownell 	struct spi_transfer	x[2];
56853e84b67SDavid Brownell 	int			status;
56953e84b67SDavid Brownell 
57053e84b67SDavid Brownell 	spi = container_of(kobj, struct spi_device, dev.kobj);
57153e84b67SDavid Brownell 
57253e84b67SDavid Brownell 	if (unlikely(off >= DS1305_NVRAM_LEN))
57353e84b67SDavid Brownell 		return -EFBIG;
57453e84b67SDavid Brownell 	if (count >= DS1305_NVRAM_LEN)
57553e84b67SDavid Brownell 		count = DS1305_NVRAM_LEN;
57653e84b67SDavid Brownell 	if ((off + count) > DS1305_NVRAM_LEN)
57753e84b67SDavid Brownell 		count = DS1305_NVRAM_LEN - off;
57853e84b67SDavid Brownell 	if (unlikely(!count))
57953e84b67SDavid Brownell 		return count;
58053e84b67SDavid Brownell 
58153e84b67SDavid Brownell 	addr = (DS1305_WRITE | DS1305_NVRAM) + off;
58253e84b67SDavid Brownell 	msg_init(&m, x, &addr, count, buf, NULL);
58353e84b67SDavid Brownell 
58453e84b67SDavid Brownell 	status = spi_sync(spi, &m);
58553e84b67SDavid Brownell 	if (status < 0)
58653e84b67SDavid Brownell 		dev_err(&spi->dev, "nvram %s error %d\n", "write", status);
58753e84b67SDavid Brownell 	return (status < 0) ? status : count;
58853e84b67SDavid Brownell }
58953e84b67SDavid Brownell 
59053e84b67SDavid Brownell static struct bin_attribute nvram = {
59153e84b67SDavid Brownell 	.attr.name	= "nvram",
59253e84b67SDavid Brownell 	.attr.mode	= S_IRUGO | S_IWUSR,
59353e84b67SDavid Brownell 	.read		= ds1305_nvram_read,
59453e84b67SDavid Brownell 	.write		= ds1305_nvram_write,
59553e84b67SDavid Brownell 	.size		= DS1305_NVRAM_LEN,
59653e84b67SDavid Brownell };
59753e84b67SDavid Brownell 
59853e84b67SDavid Brownell /*----------------------------------------------------------------------*/
59953e84b67SDavid Brownell 
60053e84b67SDavid Brownell /*
60153e84b67SDavid Brownell  * Interface to SPI stack
60253e84b67SDavid Brownell  */
60353e84b67SDavid Brownell 
6045a167f45SGreg Kroah-Hartman static int ds1305_probe(struct spi_device *spi)
60553e84b67SDavid Brownell {
60653e84b67SDavid Brownell 	struct ds1305			*ds1305;
60753e84b67SDavid Brownell 	int				status;
60853e84b67SDavid Brownell 	u8				addr, value;
609cfa13b24SJingoo Han 	struct ds1305_platform_data	*pdata = dev_get_platdata(&spi->dev);
61053e84b67SDavid Brownell 	bool				write_ctrl = false;
61153e84b67SDavid Brownell 
61253e84b67SDavid Brownell 	/* Sanity check board setup data.  This may be hooked up
61353e84b67SDavid Brownell 	 * in 3wire mode, but we don't care.  Note that unless
61453e84b67SDavid Brownell 	 * there's an inverter in place, this needs SPI_CS_HIGH!
61553e84b67SDavid Brownell 	 */
61653e84b67SDavid Brownell 	if ((spi->bits_per_word && spi->bits_per_word != 8)
61753e84b67SDavid Brownell 			|| (spi->max_speed_hz > 2000000)
61853e84b67SDavid Brownell 			|| !(spi->mode & SPI_CPHA))
61953e84b67SDavid Brownell 		return -EINVAL;
62053e84b67SDavid Brownell 
62153e84b67SDavid Brownell 	/* set up driver data */
6220529bf46SSachin Kamat 	ds1305 = devm_kzalloc(&spi->dev, sizeof(*ds1305), GFP_KERNEL);
62353e84b67SDavid Brownell 	if (!ds1305)
62453e84b67SDavid Brownell 		return -ENOMEM;
62553e84b67SDavid Brownell 	ds1305->spi = spi;
62653e84b67SDavid Brownell 	spi_set_drvdata(spi, ds1305);
62753e84b67SDavid Brownell 
62853e84b67SDavid Brownell 	/* read and cache control registers */
62953e84b67SDavid Brownell 	addr = DS1305_CONTROL;
630465008faSSachin Kamat 	status = spi_write_then_read(spi, &addr, sizeof(addr),
631465008faSSachin Kamat 			ds1305->ctrl, sizeof(ds1305->ctrl));
63253e84b67SDavid Brownell 	if (status < 0) {
63353e84b67SDavid Brownell 		dev_dbg(&spi->dev, "can't %s, %d\n",
63453e84b67SDavid Brownell 				"read", status);
6350529bf46SSachin Kamat 		return status;
63653e84b67SDavid Brownell 	}
63753e84b67SDavid Brownell 
63801a4ca16SAndy Shevchenko 	dev_dbg(&spi->dev, "ctrl %s: %3ph\n", "read", ds1305->ctrl);
63953e84b67SDavid Brownell 
64053e84b67SDavid Brownell 	/* Sanity check register values ... partially compensating for the
64153e84b67SDavid Brownell 	 * fact that SPI has no device handshake.  A pullup on MISO would
64253e84b67SDavid Brownell 	 * make these tests fail; but not all systems will have one.  If
64353e84b67SDavid Brownell 	 * some register is neither 0x00 nor 0xff, a chip is likely there.
64453e84b67SDavid Brownell 	 */
64553e84b67SDavid Brownell 	if ((ds1305->ctrl[0] & 0x38) != 0 || (ds1305->ctrl[1] & 0xfc) != 0) {
64653e84b67SDavid Brownell 		dev_dbg(&spi->dev, "RTC chip is not present\n");
6470529bf46SSachin Kamat 		return -ENODEV;
64853e84b67SDavid Brownell 	}
64953e84b67SDavid Brownell 	if (ds1305->ctrl[2] == 0)
65053e84b67SDavid Brownell 		dev_dbg(&spi->dev, "chip may not be present\n");
65153e84b67SDavid Brownell 
65253e84b67SDavid Brownell 	/* enable writes if needed ... if we were paranoid it would
65353e84b67SDavid Brownell 	 * make sense to enable them only when absolutely necessary.
65453e84b67SDavid Brownell 	 */
65553e84b67SDavid Brownell 	if (ds1305->ctrl[0] & DS1305_WP) {
65653e84b67SDavid Brownell 		u8		buf[2];
65753e84b67SDavid Brownell 
65853e84b67SDavid Brownell 		ds1305->ctrl[0] &= ~DS1305_WP;
65953e84b67SDavid Brownell 
66053e84b67SDavid Brownell 		buf[0] = DS1305_WRITE | DS1305_CONTROL;
66153e84b67SDavid Brownell 		buf[1] = ds1305->ctrl[0];
662465008faSSachin Kamat 		status = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0);
66353e84b67SDavid Brownell 
66453e84b67SDavid Brownell 		dev_dbg(&spi->dev, "clear WP --> %d\n", status);
66553e84b67SDavid Brownell 		if (status < 0)
6660529bf46SSachin Kamat 			return status;
66753e84b67SDavid Brownell 	}
66853e84b67SDavid Brownell 
66953e84b67SDavid Brownell 	/* on DS1305, maybe start oscillator; like most low power
67053e84b67SDavid Brownell 	 * oscillators, it may take a second to stabilize
67153e84b67SDavid Brownell 	 */
67253e84b67SDavid Brownell 	if (ds1305->ctrl[0] & DS1305_nEOSC) {
67353e84b67SDavid Brownell 		ds1305->ctrl[0] &= ~DS1305_nEOSC;
67453e84b67SDavid Brownell 		write_ctrl = true;
67553e84b67SDavid Brownell 		dev_warn(&spi->dev, "SET TIME!\n");
67653e84b67SDavid Brownell 	}
67753e84b67SDavid Brownell 
67853e84b67SDavid Brownell 	/* ack any pending IRQs */
67953e84b67SDavid Brownell 	if (ds1305->ctrl[1]) {
68053e84b67SDavid Brownell 		ds1305->ctrl[1] = 0;
68153e84b67SDavid Brownell 		write_ctrl = true;
68253e84b67SDavid Brownell 	}
68353e84b67SDavid Brownell 
68453e84b67SDavid Brownell 	/* this may need one-time (re)init */
68553e84b67SDavid Brownell 	if (pdata) {
68653e84b67SDavid Brownell 		/* maybe enable trickle charge */
68753e84b67SDavid Brownell 		if (((ds1305->ctrl[2] & 0xf0) != DS1305_TRICKLE_MAGIC)) {
68853e84b67SDavid Brownell 			ds1305->ctrl[2] = DS1305_TRICKLE_MAGIC
68953e84b67SDavid Brownell 						| pdata->trickle;
69053e84b67SDavid Brownell 			write_ctrl = true;
69153e84b67SDavid Brownell 		}
69253e84b67SDavid Brownell 
69353e84b67SDavid Brownell 		/* on DS1306, configure 1 Hz signal */
69453e84b67SDavid Brownell 		if (pdata->is_ds1306) {
69553e84b67SDavid Brownell 			if (pdata->en_1hz) {
69653e84b67SDavid Brownell 				if (!(ds1305->ctrl[0] & DS1306_1HZ)) {
69753e84b67SDavid Brownell 					ds1305->ctrl[0] |= DS1306_1HZ;
69853e84b67SDavid Brownell 					write_ctrl = true;
69953e84b67SDavid Brownell 				}
70053e84b67SDavid Brownell 			} else {
70153e84b67SDavid Brownell 				if (ds1305->ctrl[0] & DS1306_1HZ) {
70253e84b67SDavid Brownell 					ds1305->ctrl[0] &= ~DS1306_1HZ;
70353e84b67SDavid Brownell 					write_ctrl = true;
70453e84b67SDavid Brownell 				}
70553e84b67SDavid Brownell 			}
70653e84b67SDavid Brownell 		}
70753e84b67SDavid Brownell 	}
70853e84b67SDavid Brownell 
70953e84b67SDavid Brownell 	if (write_ctrl) {
71053e84b67SDavid Brownell 		u8		buf[4];
71153e84b67SDavid Brownell 
71253e84b67SDavid Brownell 		buf[0] = DS1305_WRITE | DS1305_CONTROL;
71353e84b67SDavid Brownell 		buf[1] = ds1305->ctrl[0];
71453e84b67SDavid Brownell 		buf[2] = ds1305->ctrl[1];
71553e84b67SDavid Brownell 		buf[3] = ds1305->ctrl[2];
716465008faSSachin Kamat 		status = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0);
71753e84b67SDavid Brownell 		if (status < 0) {
71853e84b67SDavid Brownell 			dev_dbg(&spi->dev, "can't %s, %d\n",
71953e84b67SDavid Brownell 					"write", status);
7200529bf46SSachin Kamat 			return status;
72153e84b67SDavid Brownell 		}
72253e84b67SDavid Brownell 
72301a4ca16SAndy Shevchenko 		dev_dbg(&spi->dev, "ctrl %s: %3ph\n", "write", ds1305->ctrl);
72453e84b67SDavid Brownell 	}
72553e84b67SDavid Brownell 
72653e84b67SDavid Brownell 	/* see if non-Linux software set up AM/PM mode */
72753e84b67SDavid Brownell 	addr = DS1305_HOUR;
728465008faSSachin Kamat 	status = spi_write_then_read(spi, &addr, sizeof(addr),
729465008faSSachin Kamat 				&value, sizeof(value));
73053e84b67SDavid Brownell 	if (status < 0) {
73153e84b67SDavid Brownell 		dev_dbg(&spi->dev, "read HOUR --> %d\n", status);
7320529bf46SSachin Kamat 		return status;
73353e84b67SDavid Brownell 	}
73453e84b67SDavid Brownell 
73553e84b67SDavid Brownell 	ds1305->hr12 = (DS1305_HR_12 & value) != 0;
73653e84b67SDavid Brownell 	if (ds1305->hr12)
73753e84b67SDavid Brownell 		dev_dbg(&spi->dev, "AM/PM\n");
73853e84b67SDavid Brownell 
73953e84b67SDavid Brownell 	/* register RTC ... from here on, ds1305->ctrl needs locking */
7400529bf46SSachin Kamat 	ds1305->rtc = devm_rtc_device_register(&spi->dev, "ds1305",
74153e84b67SDavid Brownell 			&ds1305_ops, THIS_MODULE);
742b74d2caaSAlessandro Zummo 	if (IS_ERR(ds1305->rtc)) {
743b74d2caaSAlessandro Zummo 		status = PTR_ERR(ds1305->rtc);
74453e84b67SDavid Brownell 		dev_dbg(&spi->dev, "register rtc --> %d\n", status);
7450529bf46SSachin Kamat 		return status;
74653e84b67SDavid Brownell 	}
74753e84b67SDavid Brownell 
74853e84b67SDavid Brownell 	/* Maybe set up alarm IRQ; be ready to handle it triggering right
74953e84b67SDavid Brownell 	 * away.  NOTE that we don't share this.  The signal is active low,
75053e84b67SDavid Brownell 	 * and we can't ack it before a SPI message delay.  We temporarily
75153e84b67SDavid Brownell 	 * disable the IRQ until it's acked, which lets us work with more
75253e84b67SDavid Brownell 	 * IRQ trigger modes (not all IRQ controllers can do falling edge).
75353e84b67SDavid Brownell 	 */
75453e84b67SDavid Brownell 	if (spi->irq) {
75553e84b67SDavid Brownell 		INIT_WORK(&ds1305->work, ds1305_work);
7560529bf46SSachin Kamat 		status = devm_request_irq(&spi->dev, spi->irq, ds1305_irq,
757b74d2caaSAlessandro Zummo 				0, dev_name(&ds1305->rtc->dev), ds1305);
75853e84b67SDavid Brownell 		if (status < 0) {
759*4071ea25SAlessandro Zummo 			dev_err(&spi->dev, "request_irq %d --> %d\n",
76053e84b67SDavid Brownell 					spi->irq, status);
761*4071ea25SAlessandro Zummo 		} else {
76226b3c01fSAnton Vorontsov 			device_set_wakeup_capable(&spi->dev, 1);
76353e84b67SDavid Brownell 		}
764*4071ea25SAlessandro Zummo 	}
76553e84b67SDavid Brownell 
76653e84b67SDavid Brownell 	/* export NVRAM */
76753e84b67SDavid Brownell 	status = sysfs_create_bin_file(&spi->dev.kobj, &nvram);
76853e84b67SDavid Brownell 	if (status < 0) {
769*4071ea25SAlessandro Zummo 		dev_err(&spi->dev, "register nvram --> %d\n", status);
77053e84b67SDavid Brownell 	}
77153e84b67SDavid Brownell 
77253e84b67SDavid Brownell 	return 0;
77353e84b67SDavid Brownell }
77453e84b67SDavid Brownell 
7755a167f45SGreg Kroah-Hartman static int ds1305_remove(struct spi_device *spi)
77653e84b67SDavid Brownell {
77753e84b67SDavid Brownell 	struct ds1305 *ds1305 = spi_get_drvdata(spi);
77853e84b67SDavid Brownell 
77953e84b67SDavid Brownell 	sysfs_remove_bin_file(&spi->dev.kobj, &nvram);
78053e84b67SDavid Brownell 
78153e84b67SDavid Brownell 	/* carefully shut down irq and workqueue, if present */
78253e84b67SDavid Brownell 	if (spi->irq) {
78353e84b67SDavid Brownell 		set_bit(FLAG_EXITING, &ds1305->flags);
7840529bf46SSachin Kamat 		devm_free_irq(&spi->dev, spi->irq, ds1305);
7859db8995bSTejun Heo 		cancel_work_sync(&ds1305->work);
78653e84b67SDavid Brownell 	}
78753e84b67SDavid Brownell 
78853e84b67SDavid Brownell 	return 0;
78953e84b67SDavid Brownell }
79053e84b67SDavid Brownell 
79153e84b67SDavid Brownell static struct spi_driver ds1305_driver = {
79253e84b67SDavid Brownell 	.driver.name	= "rtc-ds1305",
79353e84b67SDavid Brownell 	.driver.owner	= THIS_MODULE,
79453e84b67SDavid Brownell 	.probe		= ds1305_probe,
7955a167f45SGreg Kroah-Hartman 	.remove		= ds1305_remove,
79653e84b67SDavid Brownell 	/* REVISIT add suspend/resume */
79753e84b67SDavid Brownell };
79853e84b67SDavid Brownell 
799109e9418SAxel Lin module_spi_driver(ds1305_driver);
80053e84b67SDavid Brownell 
80153e84b67SDavid Brownell MODULE_DESCRIPTION("RTC driver for DS1305 and DS1306 chips");
80253e84b67SDavid Brownell MODULE_LICENSE("GPL");
803e0626e38SAnton Vorontsov MODULE_ALIAS("spi:rtc-ds1305");
804