xref: /linux/drivers/rtc/rtc-ab8500.c (revision 93a6f9168f2fbeb5bb000ba079e17a11bedc1a62)
10af62f4dSVirupax Sadashivpetimath /*
20af62f4dSVirupax Sadashivpetimath  * Copyright (C) ST-Ericsson SA 2010
30af62f4dSVirupax Sadashivpetimath  *
40af62f4dSVirupax Sadashivpetimath  * License terms: GNU General Public License (GPL) version 2
50af62f4dSVirupax Sadashivpetimath  * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>
60af62f4dSVirupax Sadashivpetimath  *
70af62f4dSVirupax Sadashivpetimath  * RTC clock driver for the RTC part of the AB8500 Power management chip.
80af62f4dSVirupax Sadashivpetimath  * Based on RTC clock driver for the AB3100 Analog Baseband Chip by
90af62f4dSVirupax Sadashivpetimath  * Linus Walleij <linus.walleij@stericsson.com>
100af62f4dSVirupax Sadashivpetimath  */
110af62f4dSVirupax Sadashivpetimath 
120af62f4dSVirupax Sadashivpetimath #include <linux/module.h>
130af62f4dSVirupax Sadashivpetimath #include <linux/kernel.h>
140af62f4dSVirupax Sadashivpetimath #include <linux/init.h>
150af62f4dSVirupax Sadashivpetimath #include <linux/platform_device.h>
160af62f4dSVirupax Sadashivpetimath #include <linux/rtc.h>
1747c16975SMattias Wallin #include <linux/mfd/abx500.h>
18ee66e653SLinus Walleij #include <linux/mfd/abx500/ab8500.h>
190af62f4dSVirupax Sadashivpetimath #include <linux/delay.h>
20ad49fcbeSLee Jones #include <linux/of.h>
21*93a6f916SSudeep Holla #include <linux/pm_wakeirq.h>
220af62f4dSVirupax Sadashivpetimath 
2347c16975SMattias Wallin #define AB8500_RTC_SOFF_STAT_REG	0x00
2447c16975SMattias Wallin #define AB8500_RTC_CC_CONF_REG		0x01
2547c16975SMattias Wallin #define AB8500_RTC_READ_REQ_REG		0x02
2647c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECMID_REG	0x03
2747c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECHI_REG	0x04
2847c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_LOW_REG	0x05
2947c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_MID_REG	0x06
3047c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_HI_REG	0x07
3147c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_LOW_REG	0x08
3247c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_MID_REG	0x09
3347c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_HI_REG	0x0A
3447c16975SMattias Wallin #define AB8500_RTC_STAT_REG		0x0B
3547c16975SMattias Wallin #define AB8500_RTC_BKUP_CHG_REG		0x0C
3647c16975SMattias Wallin #define AB8500_RTC_FORCE_BKUP_REG	0x0D
3747c16975SMattias Wallin #define AB8500_RTC_CALIB_REG		0x0E
3847c16975SMattias Wallin #define AB8500_RTC_SWITCH_STAT_REG	0x0F
3925d053cfSAlexandre Torgue #define AB8540_RTC_ALRM_SEC		0x22
4025d053cfSAlexandre Torgue #define AB8540_RTC_ALRM_MIN_LOW_REG	0x23
4125d053cfSAlexandre Torgue #define AB8540_RTC_ALRM_MIN_MID_REG	0x24
4225d053cfSAlexandre Torgue #define AB8540_RTC_ALRM_MIN_HI_REG	0x25
430af62f4dSVirupax Sadashivpetimath 
440af62f4dSVirupax Sadashivpetimath /* RtcReadRequest bits */
450af62f4dSVirupax Sadashivpetimath #define RTC_READ_REQUEST		0x01
460af62f4dSVirupax Sadashivpetimath #define RTC_WRITE_REQUEST		0x02
470af62f4dSVirupax Sadashivpetimath 
480af62f4dSVirupax Sadashivpetimath /* RtcCtrl bits */
490af62f4dSVirupax Sadashivpetimath #define RTC_ALARM_ENA			0x04
500af62f4dSVirupax Sadashivpetimath #define RTC_STATUS_DATA			0x01
510af62f4dSVirupax Sadashivpetimath 
520af62f4dSVirupax Sadashivpetimath #define COUNTS_PER_SEC			(0xF000 / 60)
530af62f4dSVirupax Sadashivpetimath #define AB8500_RTC_EPOCH		2000
540af62f4dSVirupax Sadashivpetimath 
5547c16975SMattias Wallin static const u8 ab8500_rtc_time_regs[] = {
560af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
570af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
580af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TSECMID_REG
590af62f4dSVirupax Sadashivpetimath };
600af62f4dSVirupax Sadashivpetimath 
6147c16975SMattias Wallin static const u8 ab8500_rtc_alarm_regs[] = {
620af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
630af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_ALRM_MIN_LOW_REG
640af62f4dSVirupax Sadashivpetimath };
650af62f4dSVirupax Sadashivpetimath 
6625d053cfSAlexandre Torgue static const u8 ab8540_rtc_alarm_regs[] = {
6725d053cfSAlexandre Torgue 	AB8540_RTC_ALRM_MIN_HI_REG, AB8540_RTC_ALRM_MIN_MID_REG,
6825d053cfSAlexandre Torgue 	AB8540_RTC_ALRM_MIN_LOW_REG, AB8540_RTC_ALRM_SEC
6925d053cfSAlexandre Torgue };
7025d053cfSAlexandre Torgue 
710af62f4dSVirupax Sadashivpetimath /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */
720af62f4dSVirupax Sadashivpetimath static unsigned long get_elapsed_seconds(int year)
730af62f4dSVirupax Sadashivpetimath {
740af62f4dSVirupax Sadashivpetimath 	unsigned long secs;
750af62f4dSVirupax Sadashivpetimath 	struct rtc_time tm = {
760af62f4dSVirupax Sadashivpetimath 		.tm_year = year - 1900,
770af62f4dSVirupax Sadashivpetimath 		.tm_mday = 1,
780af62f4dSVirupax Sadashivpetimath 	};
790af62f4dSVirupax Sadashivpetimath 
800af62f4dSVirupax Sadashivpetimath 	/*
810af62f4dSVirupax Sadashivpetimath 	 * This function calculates secs from 1970 and not from
820af62f4dSVirupax Sadashivpetimath 	 * 1900, even if we supply the offset from year 1900.
830af62f4dSVirupax Sadashivpetimath 	 */
840af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(&tm, &secs);
850af62f4dSVirupax Sadashivpetimath 	return secs;
860af62f4dSVirupax Sadashivpetimath }
870af62f4dSVirupax Sadashivpetimath 
880af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
890af62f4dSVirupax Sadashivpetimath {
900af62f4dSVirupax Sadashivpetimath 	unsigned long timeout = jiffies + HZ;
910af62f4dSVirupax Sadashivpetimath 	int retval, i;
920af62f4dSVirupax Sadashivpetimath 	unsigned long mins, secs;
930af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
9447c16975SMattias Wallin 	u8 value;
950af62f4dSVirupax Sadashivpetimath 
960af62f4dSVirupax Sadashivpetimath 	/* Request a data read */
9747c16975SMattias Wallin 	retval = abx500_set_register_interruptible(dev,
9847c16975SMattias Wallin 		AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
990af62f4dSVirupax Sadashivpetimath 	if (retval < 0)
1000af62f4dSVirupax Sadashivpetimath 		return retval;
1010af62f4dSVirupax Sadashivpetimath 
1020af62f4dSVirupax Sadashivpetimath 	/* Wait for some cycles after enabling the rtc read in ab8500 */
1030af62f4dSVirupax Sadashivpetimath 	while (time_before(jiffies, timeout)) {
10447c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
10547c16975SMattias Wallin 			AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
1060af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1070af62f4dSVirupax Sadashivpetimath 			return retval;
1080af62f4dSVirupax Sadashivpetimath 
10947c16975SMattias Wallin 		if (!(value & RTC_READ_REQUEST))
1100af62f4dSVirupax Sadashivpetimath 			break;
1110af62f4dSVirupax Sadashivpetimath 
112012e52e1SLinus Walleij 		usleep_range(1000, 5000);
1130af62f4dSVirupax Sadashivpetimath 	}
1140af62f4dSVirupax Sadashivpetimath 
1150af62f4dSVirupax Sadashivpetimath 	/* Read the Watchtime registers */
1160af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
11747c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
11847c16975SMattias Wallin 			AB8500_RTC, ab8500_rtc_time_regs[i], &value);
1190af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1200af62f4dSVirupax Sadashivpetimath 			return retval;
12147c16975SMattias Wallin 		buf[i] = value;
1220af62f4dSVirupax Sadashivpetimath 	}
1230af62f4dSVirupax Sadashivpetimath 
1240af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
1250af62f4dSVirupax Sadashivpetimath 
1260af62f4dSVirupax Sadashivpetimath 	secs =	(buf[3] << 8) | buf[4];
1270af62f4dSVirupax Sadashivpetimath 	secs =	secs / COUNTS_PER_SEC;
1280af62f4dSVirupax Sadashivpetimath 	secs =	secs + (mins * 60);
1290af62f4dSVirupax Sadashivpetimath 
1300af62f4dSVirupax Sadashivpetimath 	/* Add back the initially subtracted number of seconds */
1310af62f4dSVirupax Sadashivpetimath 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
1320af62f4dSVirupax Sadashivpetimath 
1330af62f4dSVirupax Sadashivpetimath 	rtc_time_to_tm(secs, tm);
1340af62f4dSVirupax Sadashivpetimath 	return rtc_valid_tm(tm);
1350af62f4dSVirupax Sadashivpetimath }
1360af62f4dSVirupax Sadashivpetimath 
1370af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
1380af62f4dSVirupax Sadashivpetimath {
1390af62f4dSVirupax Sadashivpetimath 	int retval, i;
1400af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
1410af62f4dSVirupax Sadashivpetimath 	unsigned long no_secs, no_mins, secs = 0;
1420af62f4dSVirupax Sadashivpetimath 
1430af62f4dSVirupax Sadashivpetimath 	if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
1440af62f4dSVirupax Sadashivpetimath 		dev_dbg(dev, "year should be equal to or greater than %d\n",
1450af62f4dSVirupax Sadashivpetimath 				AB8500_RTC_EPOCH);
1460af62f4dSVirupax Sadashivpetimath 		return -EINVAL;
1470af62f4dSVirupax Sadashivpetimath 	}
1480af62f4dSVirupax Sadashivpetimath 
1490af62f4dSVirupax Sadashivpetimath 	/* Get the number of seconds since 1970 */
1500af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(tm, &secs);
1510af62f4dSVirupax Sadashivpetimath 
1520af62f4dSVirupax Sadashivpetimath 	/*
1530af62f4dSVirupax Sadashivpetimath 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
1540af62f4dSVirupax Sadashivpetimath 	 * we only have a small counter in the RTC.
1550af62f4dSVirupax Sadashivpetimath 	 */
1560af62f4dSVirupax Sadashivpetimath 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
1570af62f4dSVirupax Sadashivpetimath 
1580af62f4dSVirupax Sadashivpetimath 	no_mins = secs / 60;
1590af62f4dSVirupax Sadashivpetimath 
1600af62f4dSVirupax Sadashivpetimath 	no_secs = secs % 60;
1610af62f4dSVirupax Sadashivpetimath 	/* Make the seconds count as per the RTC resolution */
1620af62f4dSVirupax Sadashivpetimath 	no_secs = no_secs * COUNTS_PER_SEC;
1630af62f4dSVirupax Sadashivpetimath 
1640af62f4dSVirupax Sadashivpetimath 	buf[4] = no_secs & 0xFF;
1650af62f4dSVirupax Sadashivpetimath 	buf[3] = (no_secs >> 8) & 0xFF;
1660af62f4dSVirupax Sadashivpetimath 
1670af62f4dSVirupax Sadashivpetimath 	buf[2] = no_mins & 0xFF;
1680af62f4dSVirupax Sadashivpetimath 	buf[1] = (no_mins >> 8) & 0xFF;
1690af62f4dSVirupax Sadashivpetimath 	buf[0] = (no_mins >> 16) & 0xFF;
1700af62f4dSVirupax Sadashivpetimath 
1710af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
17247c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
17347c16975SMattias Wallin 			ab8500_rtc_time_regs[i], buf[i]);
1740af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1750af62f4dSVirupax Sadashivpetimath 			return retval;
1760af62f4dSVirupax Sadashivpetimath 	}
1770af62f4dSVirupax Sadashivpetimath 
1780af62f4dSVirupax Sadashivpetimath 	/* Request a data write */
17947c16975SMattias Wallin 	return abx500_set_register_interruptible(dev, AB8500_RTC,
18047c16975SMattias Wallin 		AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
1810af62f4dSVirupax Sadashivpetimath }
1820af62f4dSVirupax Sadashivpetimath 
1830af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
1840af62f4dSVirupax Sadashivpetimath {
1850af62f4dSVirupax Sadashivpetimath 	int retval, i;
18647c16975SMattias Wallin 	u8 rtc_ctrl, value;
1870af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
1880af62f4dSVirupax Sadashivpetimath 	unsigned long secs, mins;
1890af62f4dSVirupax Sadashivpetimath 
1900af62f4dSVirupax Sadashivpetimath 	/* Check if the alarm is enabled or not */
19147c16975SMattias Wallin 	retval = abx500_get_register_interruptible(dev, AB8500_RTC,
19247c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
19347c16975SMattias Wallin 	if (retval < 0)
19447c16975SMattias Wallin 		return retval;
1950af62f4dSVirupax Sadashivpetimath 
1960af62f4dSVirupax Sadashivpetimath 	if (rtc_ctrl & RTC_ALARM_ENA)
1970af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 1;
1980af62f4dSVirupax Sadashivpetimath 	else
1990af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 0;
2000af62f4dSVirupax Sadashivpetimath 
2010af62f4dSVirupax Sadashivpetimath 	alarm->pending = 0;
2020af62f4dSVirupax Sadashivpetimath 
2030af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
20447c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev, AB8500_RTC,
20547c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], &value);
2060af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
2070af62f4dSVirupax Sadashivpetimath 			return retval;
20847c16975SMattias Wallin 		buf[i] = value;
2090af62f4dSVirupax Sadashivpetimath 	}
2100af62f4dSVirupax Sadashivpetimath 
2110af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
2120af62f4dSVirupax Sadashivpetimath 	secs = mins * 60;
2130af62f4dSVirupax Sadashivpetimath 
2140af62f4dSVirupax Sadashivpetimath 	/* Add back the initially subtracted number of seconds */
2150af62f4dSVirupax Sadashivpetimath 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
2160af62f4dSVirupax Sadashivpetimath 
2170af62f4dSVirupax Sadashivpetimath 	rtc_time_to_tm(secs, &alarm->time);
2180af62f4dSVirupax Sadashivpetimath 
2190af62f4dSVirupax Sadashivpetimath 	return rtc_valid_tm(&alarm->time);
2200af62f4dSVirupax Sadashivpetimath }
2210af62f4dSVirupax Sadashivpetimath 
2220af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
2230af62f4dSVirupax Sadashivpetimath {
22447c16975SMattias Wallin 	return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
22547c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
2260af62f4dSVirupax Sadashivpetimath 		enabled ? RTC_ALARM_ENA : 0);
2270af62f4dSVirupax Sadashivpetimath }
2280af62f4dSVirupax Sadashivpetimath 
2290af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
2300af62f4dSVirupax Sadashivpetimath {
2310af62f4dSVirupax Sadashivpetimath 	int retval, i;
2320af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
233dc43d4a2SRamesh Chandrasekaran 	unsigned long mins, secs = 0, cursec = 0;
234dc43d4a2SRamesh Chandrasekaran 	struct rtc_time curtm;
2350af62f4dSVirupax Sadashivpetimath 
2360af62f4dSVirupax Sadashivpetimath 	if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
2370af62f4dSVirupax Sadashivpetimath 		dev_dbg(dev, "year should be equal to or greater than %d\n",
2380af62f4dSVirupax Sadashivpetimath 				AB8500_RTC_EPOCH);
2390af62f4dSVirupax Sadashivpetimath 		return -EINVAL;
2400af62f4dSVirupax Sadashivpetimath 	}
2410af62f4dSVirupax Sadashivpetimath 
2420af62f4dSVirupax Sadashivpetimath 	/* Get the number of seconds since 1970 */
2430af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(&alarm->time, &secs);
2440af62f4dSVirupax Sadashivpetimath 
2450af62f4dSVirupax Sadashivpetimath 	/*
246dc43d4a2SRamesh Chandrasekaran 	 * Check whether alarm is set less than 1min.
247dc43d4a2SRamesh Chandrasekaran 	 * Since our RTC doesn't support alarm resolution less than 1min,
248dc43d4a2SRamesh Chandrasekaran 	 * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON
249dc43d4a2SRamesh Chandrasekaran 	 */
250dc43d4a2SRamesh Chandrasekaran 	ab8500_rtc_read_time(dev, &curtm); /* Read current time */
251dc43d4a2SRamesh Chandrasekaran 	rtc_tm_to_time(&curtm, &cursec);
252dc43d4a2SRamesh Chandrasekaran 	if ((secs - cursec) < 59) {
253dc43d4a2SRamesh Chandrasekaran 		dev_dbg(dev, "Alarm less than 1 minute not supported\r\n");
254dc43d4a2SRamesh Chandrasekaran 		return -EINVAL;
255dc43d4a2SRamesh Chandrasekaran 	}
256dc43d4a2SRamesh Chandrasekaran 
257dc43d4a2SRamesh Chandrasekaran 	/*
2580af62f4dSVirupax Sadashivpetimath 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
2590af62f4dSVirupax Sadashivpetimath 	 * we only have a small counter in the RTC.
2600af62f4dSVirupax Sadashivpetimath 	 */
2610af62f4dSVirupax Sadashivpetimath 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
2620af62f4dSVirupax Sadashivpetimath 
2630af62f4dSVirupax Sadashivpetimath 	mins = secs / 60;
2640af62f4dSVirupax Sadashivpetimath 
2650af62f4dSVirupax Sadashivpetimath 	buf[2] = mins & 0xFF;
2660af62f4dSVirupax Sadashivpetimath 	buf[1] = (mins >> 8) & 0xFF;
2670af62f4dSVirupax Sadashivpetimath 	buf[0] = (mins >> 16) & 0xFF;
2680af62f4dSVirupax Sadashivpetimath 
2690af62f4dSVirupax Sadashivpetimath 	/* Set the alarm time */
2700af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
27147c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
27247c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], buf[i]);
2730af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
2740af62f4dSVirupax Sadashivpetimath 			return retval;
2750af62f4dSVirupax Sadashivpetimath 	}
2760af62f4dSVirupax Sadashivpetimath 
2770af62f4dSVirupax Sadashivpetimath 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
2780af62f4dSVirupax Sadashivpetimath }
2790af62f4dSVirupax Sadashivpetimath 
28025d053cfSAlexandre Torgue static int ab8540_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
28125d053cfSAlexandre Torgue {
28225d053cfSAlexandre Torgue 	int retval, i;
28325d053cfSAlexandre Torgue 	unsigned char buf[ARRAY_SIZE(ab8540_rtc_alarm_regs)];
28425d053cfSAlexandre Torgue 	unsigned long mins, secs = 0;
28525d053cfSAlexandre Torgue 
28625d053cfSAlexandre Torgue 	if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
28725d053cfSAlexandre Torgue 		dev_dbg(dev, "year should be equal to or greater than %d\n",
28825d053cfSAlexandre Torgue 				AB8500_RTC_EPOCH);
28925d053cfSAlexandre Torgue 		return -EINVAL;
29025d053cfSAlexandre Torgue 	}
29125d053cfSAlexandre Torgue 
29225d053cfSAlexandre Torgue 	/* Get the number of seconds since 1970 */
29325d053cfSAlexandre Torgue 	rtc_tm_to_time(&alarm->time, &secs);
29425d053cfSAlexandre Torgue 
29525d053cfSAlexandre Torgue 	/*
29625d053cfSAlexandre Torgue 	 * Convert it to the number of seconds since 01-01-2000 00:00:00
29725d053cfSAlexandre Torgue 	 */
29825d053cfSAlexandre Torgue 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
29925d053cfSAlexandre Torgue 	mins = secs / 60;
30025d053cfSAlexandre Torgue 
30125d053cfSAlexandre Torgue 	buf[3] = secs % 60;
30225d053cfSAlexandre Torgue 	buf[2] = mins & 0xFF;
30325d053cfSAlexandre Torgue 	buf[1] = (mins >> 8) & 0xFF;
30425d053cfSAlexandre Torgue 	buf[0] = (mins >> 16) & 0xFF;
30525d053cfSAlexandre Torgue 
30625d053cfSAlexandre Torgue 	/* Set the alarm time */
30725d053cfSAlexandre Torgue 	for (i = 0; i < ARRAY_SIZE(ab8540_rtc_alarm_regs); i++) {
30825d053cfSAlexandre Torgue 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
30925d053cfSAlexandre Torgue 			ab8540_rtc_alarm_regs[i], buf[i]);
31025d053cfSAlexandre Torgue 		if (retval < 0)
31125d053cfSAlexandre Torgue 			return retval;
31225d053cfSAlexandre Torgue 	}
31325d053cfSAlexandre Torgue 
31425d053cfSAlexandre Torgue 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
31525d053cfSAlexandre Torgue }
316dda367acSMark Godfrey 
317dda367acSMark Godfrey static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
318dda367acSMark Godfrey {
319dda367acSMark Godfrey 	int retval;
320dda367acSMark Godfrey 	u8  rtccal = 0;
321dda367acSMark Godfrey 
322dda367acSMark Godfrey 	/*
323dda367acSMark Godfrey 	 * Check that the calibration value (which is in units of 0.5
324dda367acSMark Godfrey 	 * parts-per-million) is in the AB8500's range for RtcCalibration
325dda367acSMark Godfrey 	 * register. -128 (0x80) is not permitted because the AB8500 uses
326dda367acSMark Godfrey 	 * a sign-bit rather than two's complement, so 0x80 is just another
327dda367acSMark Godfrey 	 * representation of zero.
328dda367acSMark Godfrey 	 */
329dda367acSMark Godfrey 	if ((calibration < -127) || (calibration > 127)) {
330dda367acSMark Godfrey 		dev_err(dev, "RtcCalibration value outside permitted range\n");
331dda367acSMark Godfrey 		return -EINVAL;
332dda367acSMark Godfrey 	}
333dda367acSMark Godfrey 
334dda367acSMark Godfrey 	/*
335dda367acSMark Godfrey 	 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
336dda367acSMark Godfrey 	 * so need to convert to this sort of representation before writing
337dda367acSMark Godfrey 	 * into RtcCalibration register...
338dda367acSMark Godfrey 	 */
339dda367acSMark Godfrey 	if (calibration >= 0)
340dda367acSMark Godfrey 		rtccal = 0x7F & calibration;
341dda367acSMark Godfrey 	else
342dda367acSMark Godfrey 		rtccal = ~(calibration - 1) | 0x80;
343dda367acSMark Godfrey 
344dda367acSMark Godfrey 	retval = abx500_set_register_interruptible(dev, AB8500_RTC,
345dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, rtccal);
346dda367acSMark Godfrey 
347dda367acSMark Godfrey 	return retval;
348dda367acSMark Godfrey }
349dda367acSMark Godfrey 
350dda367acSMark Godfrey static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
351dda367acSMark Godfrey {
352dda367acSMark Godfrey 	int retval;
353dda367acSMark Godfrey 	u8  rtccal = 0;
354dda367acSMark Godfrey 
355dda367acSMark Godfrey 	retval =  abx500_get_register_interruptible(dev, AB8500_RTC,
356dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, &rtccal);
357dda367acSMark Godfrey 	if (retval >= 0) {
358dda367acSMark Godfrey 		/*
359dda367acSMark Godfrey 		 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
360dda367acSMark Godfrey 		 * so need to convert value from RtcCalibration register into
361dda367acSMark Godfrey 		 * a two's complement signed value...
362dda367acSMark Godfrey 		 */
363dda367acSMark Godfrey 		if (rtccal & 0x80)
364dda367acSMark Godfrey 			*calibration = 0 - (rtccal & 0x7F);
365dda367acSMark Godfrey 		else
366dda367acSMark Godfrey 			*calibration = 0x7F & rtccal;
367dda367acSMark Godfrey 	}
368dda367acSMark Godfrey 
369dda367acSMark Godfrey 	return retval;
370dda367acSMark Godfrey }
371dda367acSMark Godfrey 
372dda367acSMark Godfrey static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
373dda367acSMark Godfrey 				struct device_attribute *attr,
374dda367acSMark Godfrey 				const char *buf, size_t count)
375dda367acSMark Godfrey {
376dda367acSMark Godfrey 	int retval;
377dda367acSMark Godfrey 	int calibration = 0;
378dda367acSMark Godfrey 
379dda367acSMark Godfrey 	if (sscanf(buf, " %i ", &calibration) != 1) {
380dda367acSMark Godfrey 		dev_err(dev, "Failed to store RTC calibration attribute\n");
381dda367acSMark Godfrey 		return -EINVAL;
382dda367acSMark Godfrey 	}
383dda367acSMark Godfrey 
384dda367acSMark Godfrey 	retval = ab8500_rtc_set_calibration(dev, calibration);
385dda367acSMark Godfrey 
386dda367acSMark Godfrey 	return retval ? retval : count;
387dda367acSMark Godfrey }
388dda367acSMark Godfrey 
389dda367acSMark Godfrey static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
390dda367acSMark Godfrey 				struct device_attribute *attr, char *buf)
391dda367acSMark Godfrey {
392dda367acSMark Godfrey 	int  retval = 0;
393dda367acSMark Godfrey 	int  calibration = 0;
394dda367acSMark Godfrey 
395dda367acSMark Godfrey 	retval = ab8500_rtc_get_calibration(dev, &calibration);
396dda367acSMark Godfrey 	if (retval < 0) {
397dda367acSMark Godfrey 		dev_err(dev, "Failed to read RTC calibration attribute\n");
398dda367acSMark Godfrey 		sprintf(buf, "0\n");
399dda367acSMark Godfrey 		return retval;
400dda367acSMark Godfrey 	}
401dda367acSMark Godfrey 
402dda367acSMark Godfrey 	return sprintf(buf, "%d\n", calibration);
403dda367acSMark Godfrey }
404dda367acSMark Godfrey 
405dda367acSMark Godfrey static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
406dda367acSMark Godfrey 		   ab8500_sysfs_show_rtc_calibration,
407dda367acSMark Godfrey 		   ab8500_sysfs_store_rtc_calibration);
408dda367acSMark Godfrey 
409dda367acSMark Godfrey static int ab8500_sysfs_rtc_register(struct device *dev)
410dda367acSMark Godfrey {
411dda367acSMark Godfrey 	return device_create_file(dev, &dev_attr_rtc_calibration);
412dda367acSMark Godfrey }
413dda367acSMark Godfrey 
414dda367acSMark Godfrey static void ab8500_sysfs_rtc_unregister(struct device *dev)
415dda367acSMark Godfrey {
416dda367acSMark Godfrey 	device_remove_file(dev, &dev_attr_rtc_calibration);
417dda367acSMark Godfrey }
418dda367acSMark Godfrey 
4190af62f4dSVirupax Sadashivpetimath static irqreturn_t rtc_alarm_handler(int irq, void *data)
4200af62f4dSVirupax Sadashivpetimath {
4210af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc = data;
4220af62f4dSVirupax Sadashivpetimath 	unsigned long events = RTC_IRQF | RTC_AF;
4230af62f4dSVirupax Sadashivpetimath 
4240af62f4dSVirupax Sadashivpetimath 	dev_dbg(&rtc->dev, "%s\n", __func__);
4250af62f4dSVirupax Sadashivpetimath 	rtc_update_irq(rtc, 1, events);
4260af62f4dSVirupax Sadashivpetimath 
4270af62f4dSVirupax Sadashivpetimath 	return IRQ_HANDLED;
4280af62f4dSVirupax Sadashivpetimath }
4290af62f4dSVirupax Sadashivpetimath 
4300af62f4dSVirupax Sadashivpetimath static const struct rtc_class_ops ab8500_rtc_ops = {
4310af62f4dSVirupax Sadashivpetimath 	.read_time		= ab8500_rtc_read_time,
4320af62f4dSVirupax Sadashivpetimath 	.set_time		= ab8500_rtc_set_time,
4330af62f4dSVirupax Sadashivpetimath 	.read_alarm		= ab8500_rtc_read_alarm,
4340af62f4dSVirupax Sadashivpetimath 	.set_alarm		= ab8500_rtc_set_alarm,
4350af62f4dSVirupax Sadashivpetimath 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
4360af62f4dSVirupax Sadashivpetimath };
4370af62f4dSVirupax Sadashivpetimath 
43825d053cfSAlexandre Torgue static const struct rtc_class_ops ab8540_rtc_ops = {
43925d053cfSAlexandre Torgue 	.read_time		= ab8500_rtc_read_time,
44025d053cfSAlexandre Torgue 	.set_time		= ab8500_rtc_set_time,
44125d053cfSAlexandre Torgue 	.read_alarm		= ab8500_rtc_read_alarm,
44225d053cfSAlexandre Torgue 	.set_alarm		= ab8540_rtc_set_alarm,
44325d053cfSAlexandre Torgue 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
44425d053cfSAlexandre Torgue };
44525d053cfSAlexandre Torgue 
4469a72f410SKrzysztof Kozlowski static const struct platform_device_id ab85xx_rtc_ids[] = {
44725d053cfSAlexandre Torgue 	{ "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
44825d053cfSAlexandre Torgue 	{ "ab8540-rtc", (kernel_ulong_t)&ab8540_rtc_ops, },
4498a67e931SFabio Estevam 	{ /* sentinel */ }
45025d053cfSAlexandre Torgue };
45163074cc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
45225d053cfSAlexandre Torgue 
4535a167f45SGreg Kroah-Hartman static int ab8500_rtc_probe(struct platform_device *pdev)
4540af62f4dSVirupax Sadashivpetimath {
45525d053cfSAlexandre Torgue 	const struct platform_device_id *platid = platform_get_device_id(pdev);
4560af62f4dSVirupax Sadashivpetimath 	int err;
4570af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc;
45847c16975SMattias Wallin 	u8 rtc_ctrl;
4590af62f4dSVirupax Sadashivpetimath 	int irq;
4600af62f4dSVirupax Sadashivpetimath 
4610af62f4dSVirupax Sadashivpetimath 	irq = platform_get_irq_byname(pdev, "ALARM");
4620af62f4dSVirupax Sadashivpetimath 	if (irq < 0)
4630af62f4dSVirupax Sadashivpetimath 		return irq;
4640af62f4dSVirupax Sadashivpetimath 
4650af62f4dSVirupax Sadashivpetimath 	/* For RTC supply test */
46647c16975SMattias Wallin 	err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
46747c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
4680af62f4dSVirupax Sadashivpetimath 	if (err < 0)
4690af62f4dSVirupax Sadashivpetimath 		return err;
4700af62f4dSVirupax Sadashivpetimath 
4710af62f4dSVirupax Sadashivpetimath 	/* Wait for reset by the PorRtc */
472012e52e1SLinus Walleij 	usleep_range(1000, 5000);
4730af62f4dSVirupax Sadashivpetimath 
47447c16975SMattias Wallin 	err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
47547c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
47647c16975SMattias Wallin 	if (err < 0)
47747c16975SMattias Wallin 		return err;
4780af62f4dSVirupax Sadashivpetimath 
4790af62f4dSVirupax Sadashivpetimath 	/* Check if the RTC Supply fails */
4800af62f4dSVirupax Sadashivpetimath 	if (!(rtc_ctrl & RTC_STATUS_DATA)) {
4810af62f4dSVirupax Sadashivpetimath 		dev_err(&pdev->dev, "RTC supply failure\n");
4820af62f4dSVirupax Sadashivpetimath 		return -ENODEV;
4830af62f4dSVirupax Sadashivpetimath 	}
4840af62f4dSVirupax Sadashivpetimath 
485b62581e6SAndrew Lynn 	device_init_wakeup(&pdev->dev, true);
486b62581e6SAndrew Lynn 
487fa11f7e7SJingoo Han 	rtc = devm_rtc_device_register(&pdev->dev, "ab8500-rtc",
48825d053cfSAlexandre Torgue 				(struct rtc_class_ops *)platid->driver_data,
48925d053cfSAlexandre Torgue 				THIS_MODULE);
4900af62f4dSVirupax Sadashivpetimath 	if (IS_ERR(rtc)) {
4910af62f4dSVirupax Sadashivpetimath 		dev_err(&pdev->dev, "Registration failed\n");
4920af62f4dSVirupax Sadashivpetimath 		err = PTR_ERR(rtc);
4930af62f4dSVirupax Sadashivpetimath 		return err;
4940af62f4dSVirupax Sadashivpetimath 	}
4950af62f4dSVirupax Sadashivpetimath 
496fa11f7e7SJingoo Han 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
497*93a6f916SSudeep Holla 			rtc_alarm_handler, IRQF_ONESHOT,
498fa11f7e7SJingoo Han 			"ab8500-rtc", rtc);
499fa11f7e7SJingoo Han 	if (err < 0)
5000af62f4dSVirupax Sadashivpetimath 		return err;
5010af62f4dSVirupax Sadashivpetimath 
502*93a6f916SSudeep Holla 	dev_pm_set_wake_irq(&pdev->dev, irq);
5030af62f4dSVirupax Sadashivpetimath 	platform_set_drvdata(pdev, rtc);
5040af62f4dSVirupax Sadashivpetimath 
505dda367acSMark Godfrey 	err = ab8500_sysfs_rtc_register(&pdev->dev);
506dda367acSMark Godfrey 	if (err) {
507dda367acSMark Godfrey 		dev_err(&pdev->dev, "sysfs RTC failed to register\n");
508dda367acSMark Godfrey 		return err;
509dda367acSMark Godfrey 	}
510dda367acSMark Godfrey 
511c594d678SXunlei Pang 	rtc->uie_unsupported = 1;
512c594d678SXunlei Pang 
5130af62f4dSVirupax Sadashivpetimath 	return 0;
5140af62f4dSVirupax Sadashivpetimath }
5150af62f4dSVirupax Sadashivpetimath 
5165a167f45SGreg Kroah-Hartman static int ab8500_rtc_remove(struct platform_device *pdev)
5170af62f4dSVirupax Sadashivpetimath {
518*93a6f916SSudeep Holla 	dev_pm_clear_wake_irq(&pdev->dev);
519*93a6f916SSudeep Holla 	device_init_wakeup(&pdev->dev, false);
520dda367acSMark Godfrey 	ab8500_sysfs_rtc_unregister(&pdev->dev);
521dda367acSMark Godfrey 
5220af62f4dSVirupax Sadashivpetimath 	return 0;
5230af62f4dSVirupax Sadashivpetimath }
5240af62f4dSVirupax Sadashivpetimath 
5250af62f4dSVirupax Sadashivpetimath static struct platform_driver ab8500_rtc_driver = {
5260af62f4dSVirupax Sadashivpetimath 	.driver = {
5270af62f4dSVirupax Sadashivpetimath 		.name = "ab8500-rtc",
5280af62f4dSVirupax Sadashivpetimath 	},
5290af62f4dSVirupax Sadashivpetimath 	.probe	= ab8500_rtc_probe,
5305a167f45SGreg Kroah-Hartman 	.remove = ab8500_rtc_remove,
53125d053cfSAlexandre Torgue 	.id_table = ab85xx_rtc_ids,
5320af62f4dSVirupax Sadashivpetimath };
5330af62f4dSVirupax Sadashivpetimath 
5340c4eae66SAxel Lin module_platform_driver(ab8500_rtc_driver);
5350af62f4dSVirupax Sadashivpetimath 
5360af62f4dSVirupax Sadashivpetimath MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
5370af62f4dSVirupax Sadashivpetimath MODULE_DESCRIPTION("AB8500 RTC Driver");
5380af62f4dSVirupax Sadashivpetimath MODULE_LICENSE("GPL v2");
539