xref: /linux/drivers/rtc/rtc-ab8500.c (revision 5a167f4543e45d45c5672a5cd6cb8ba5ddf4f3ea)
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>
210af62f4dSVirupax Sadashivpetimath 
2247c16975SMattias Wallin #define AB8500_RTC_SOFF_STAT_REG	0x00
2347c16975SMattias Wallin #define AB8500_RTC_CC_CONF_REG		0x01
2447c16975SMattias Wallin #define AB8500_RTC_READ_REQ_REG		0x02
2547c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECMID_REG	0x03
2647c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECHI_REG	0x04
2747c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_LOW_REG	0x05
2847c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_MID_REG	0x06
2947c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_HI_REG	0x07
3047c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_LOW_REG	0x08
3147c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_MID_REG	0x09
3247c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_HI_REG	0x0A
3347c16975SMattias Wallin #define AB8500_RTC_STAT_REG		0x0B
3447c16975SMattias Wallin #define AB8500_RTC_BKUP_CHG_REG		0x0C
3547c16975SMattias Wallin #define AB8500_RTC_FORCE_BKUP_REG	0x0D
3647c16975SMattias Wallin #define AB8500_RTC_CALIB_REG		0x0E
3747c16975SMattias Wallin #define AB8500_RTC_SWITCH_STAT_REG	0x0F
380af62f4dSVirupax Sadashivpetimath 
390af62f4dSVirupax Sadashivpetimath /* RtcReadRequest bits */
400af62f4dSVirupax Sadashivpetimath #define RTC_READ_REQUEST		0x01
410af62f4dSVirupax Sadashivpetimath #define RTC_WRITE_REQUEST		0x02
420af62f4dSVirupax Sadashivpetimath 
430af62f4dSVirupax Sadashivpetimath /* RtcCtrl bits */
440af62f4dSVirupax Sadashivpetimath #define RTC_ALARM_ENA			0x04
450af62f4dSVirupax Sadashivpetimath #define RTC_STATUS_DATA			0x01
460af62f4dSVirupax Sadashivpetimath 
470af62f4dSVirupax Sadashivpetimath #define COUNTS_PER_SEC			(0xF000 / 60)
480af62f4dSVirupax Sadashivpetimath #define AB8500_RTC_EPOCH		2000
490af62f4dSVirupax Sadashivpetimath 
5047c16975SMattias Wallin static const u8 ab8500_rtc_time_regs[] = {
510af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
520af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
530af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_WATCH_TSECMID_REG
540af62f4dSVirupax Sadashivpetimath };
550af62f4dSVirupax Sadashivpetimath 
5647c16975SMattias Wallin static const u8 ab8500_rtc_alarm_regs[] = {
570af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
580af62f4dSVirupax Sadashivpetimath 	AB8500_RTC_ALRM_MIN_LOW_REG
590af62f4dSVirupax Sadashivpetimath };
600af62f4dSVirupax Sadashivpetimath 
610af62f4dSVirupax Sadashivpetimath /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */
620af62f4dSVirupax Sadashivpetimath static unsigned long get_elapsed_seconds(int year)
630af62f4dSVirupax Sadashivpetimath {
640af62f4dSVirupax Sadashivpetimath 	unsigned long secs;
650af62f4dSVirupax Sadashivpetimath 	struct rtc_time tm = {
660af62f4dSVirupax Sadashivpetimath 		.tm_year = year - 1900,
670af62f4dSVirupax Sadashivpetimath 		.tm_mday = 1,
680af62f4dSVirupax Sadashivpetimath 	};
690af62f4dSVirupax Sadashivpetimath 
700af62f4dSVirupax Sadashivpetimath 	/*
710af62f4dSVirupax Sadashivpetimath 	 * This function calculates secs from 1970 and not from
720af62f4dSVirupax Sadashivpetimath 	 * 1900, even if we supply the offset from year 1900.
730af62f4dSVirupax Sadashivpetimath 	 */
740af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(&tm, &secs);
750af62f4dSVirupax Sadashivpetimath 	return secs;
760af62f4dSVirupax Sadashivpetimath }
770af62f4dSVirupax Sadashivpetimath 
780af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
790af62f4dSVirupax Sadashivpetimath {
800af62f4dSVirupax Sadashivpetimath 	unsigned long timeout = jiffies + HZ;
810af62f4dSVirupax Sadashivpetimath 	int retval, i;
820af62f4dSVirupax Sadashivpetimath 	unsigned long mins, secs;
830af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
8447c16975SMattias Wallin 	u8 value;
850af62f4dSVirupax Sadashivpetimath 
860af62f4dSVirupax Sadashivpetimath 	/* Request a data read */
8747c16975SMattias Wallin 	retval = abx500_set_register_interruptible(dev,
8847c16975SMattias Wallin 		AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
890af62f4dSVirupax Sadashivpetimath 	if (retval < 0)
900af62f4dSVirupax Sadashivpetimath 		return retval;
910af62f4dSVirupax Sadashivpetimath 
920af62f4dSVirupax Sadashivpetimath 	/* Wait for some cycles after enabling the rtc read in ab8500 */
930af62f4dSVirupax Sadashivpetimath 	while (time_before(jiffies, timeout)) {
9447c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
9547c16975SMattias Wallin 			AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
960af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
970af62f4dSVirupax Sadashivpetimath 			return retval;
980af62f4dSVirupax Sadashivpetimath 
9947c16975SMattias Wallin 		if (!(value & RTC_READ_REQUEST))
1000af62f4dSVirupax Sadashivpetimath 			break;
1010af62f4dSVirupax Sadashivpetimath 
102012e52e1SLinus Walleij 		usleep_range(1000, 5000);
1030af62f4dSVirupax Sadashivpetimath 	}
1040af62f4dSVirupax Sadashivpetimath 
1050af62f4dSVirupax Sadashivpetimath 	/* Read the Watchtime registers */
1060af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
10747c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
10847c16975SMattias Wallin 			AB8500_RTC, ab8500_rtc_time_regs[i], &value);
1090af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1100af62f4dSVirupax Sadashivpetimath 			return retval;
11147c16975SMattias Wallin 		buf[i] = value;
1120af62f4dSVirupax Sadashivpetimath 	}
1130af62f4dSVirupax Sadashivpetimath 
1140af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
1150af62f4dSVirupax Sadashivpetimath 
1160af62f4dSVirupax Sadashivpetimath 	secs =	(buf[3] << 8) | buf[4];
1170af62f4dSVirupax Sadashivpetimath 	secs =	secs / COUNTS_PER_SEC;
1180af62f4dSVirupax Sadashivpetimath 	secs =	secs + (mins * 60);
1190af62f4dSVirupax Sadashivpetimath 
1200af62f4dSVirupax Sadashivpetimath 	/* Add back the initially subtracted number of seconds */
1210af62f4dSVirupax Sadashivpetimath 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
1220af62f4dSVirupax Sadashivpetimath 
1230af62f4dSVirupax Sadashivpetimath 	rtc_time_to_tm(secs, tm);
1240af62f4dSVirupax Sadashivpetimath 	return rtc_valid_tm(tm);
1250af62f4dSVirupax Sadashivpetimath }
1260af62f4dSVirupax Sadashivpetimath 
1270af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
1280af62f4dSVirupax Sadashivpetimath {
1290af62f4dSVirupax Sadashivpetimath 	int retval, i;
1300af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
1310af62f4dSVirupax Sadashivpetimath 	unsigned long no_secs, no_mins, secs = 0;
1320af62f4dSVirupax Sadashivpetimath 
1330af62f4dSVirupax Sadashivpetimath 	if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
1340af62f4dSVirupax Sadashivpetimath 		dev_dbg(dev, "year should be equal to or greater than %d\n",
1350af62f4dSVirupax Sadashivpetimath 				AB8500_RTC_EPOCH);
1360af62f4dSVirupax Sadashivpetimath 		return -EINVAL;
1370af62f4dSVirupax Sadashivpetimath 	}
1380af62f4dSVirupax Sadashivpetimath 
1390af62f4dSVirupax Sadashivpetimath 	/* Get the number of seconds since 1970 */
1400af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(tm, &secs);
1410af62f4dSVirupax Sadashivpetimath 
1420af62f4dSVirupax Sadashivpetimath 	/*
1430af62f4dSVirupax Sadashivpetimath 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
1440af62f4dSVirupax Sadashivpetimath 	 * we only have a small counter in the RTC.
1450af62f4dSVirupax Sadashivpetimath 	 */
1460af62f4dSVirupax Sadashivpetimath 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
1470af62f4dSVirupax Sadashivpetimath 
1480af62f4dSVirupax Sadashivpetimath 	no_mins = secs / 60;
1490af62f4dSVirupax Sadashivpetimath 
1500af62f4dSVirupax Sadashivpetimath 	no_secs = secs % 60;
1510af62f4dSVirupax Sadashivpetimath 	/* Make the seconds count as per the RTC resolution */
1520af62f4dSVirupax Sadashivpetimath 	no_secs = no_secs * COUNTS_PER_SEC;
1530af62f4dSVirupax Sadashivpetimath 
1540af62f4dSVirupax Sadashivpetimath 	buf[4] = no_secs & 0xFF;
1550af62f4dSVirupax Sadashivpetimath 	buf[3] = (no_secs >> 8) & 0xFF;
1560af62f4dSVirupax Sadashivpetimath 
1570af62f4dSVirupax Sadashivpetimath 	buf[2] = no_mins & 0xFF;
1580af62f4dSVirupax Sadashivpetimath 	buf[1] = (no_mins >> 8) & 0xFF;
1590af62f4dSVirupax Sadashivpetimath 	buf[0] = (no_mins >> 16) & 0xFF;
1600af62f4dSVirupax Sadashivpetimath 
1610af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
16247c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
16347c16975SMattias Wallin 			ab8500_rtc_time_regs[i], buf[i]);
1640af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1650af62f4dSVirupax Sadashivpetimath 			return retval;
1660af62f4dSVirupax Sadashivpetimath 	}
1670af62f4dSVirupax Sadashivpetimath 
1680af62f4dSVirupax Sadashivpetimath 	/* Request a data write */
16947c16975SMattias Wallin 	return abx500_set_register_interruptible(dev, AB8500_RTC,
17047c16975SMattias Wallin 		AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
1710af62f4dSVirupax Sadashivpetimath }
1720af62f4dSVirupax Sadashivpetimath 
1730af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
1740af62f4dSVirupax Sadashivpetimath {
1750af62f4dSVirupax Sadashivpetimath 	int retval, i;
17647c16975SMattias Wallin 	u8 rtc_ctrl, value;
1770af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
1780af62f4dSVirupax Sadashivpetimath 	unsigned long secs, mins;
1790af62f4dSVirupax Sadashivpetimath 
1800af62f4dSVirupax Sadashivpetimath 	/* Check if the alarm is enabled or not */
18147c16975SMattias Wallin 	retval = abx500_get_register_interruptible(dev, AB8500_RTC,
18247c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
18347c16975SMattias Wallin 	if (retval < 0)
18447c16975SMattias Wallin 		return retval;
1850af62f4dSVirupax Sadashivpetimath 
1860af62f4dSVirupax Sadashivpetimath 	if (rtc_ctrl & RTC_ALARM_ENA)
1870af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 1;
1880af62f4dSVirupax Sadashivpetimath 	else
1890af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 0;
1900af62f4dSVirupax Sadashivpetimath 
1910af62f4dSVirupax Sadashivpetimath 	alarm->pending = 0;
1920af62f4dSVirupax Sadashivpetimath 
1930af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
19447c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev, AB8500_RTC,
19547c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], &value);
1960af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1970af62f4dSVirupax Sadashivpetimath 			return retval;
19847c16975SMattias Wallin 		buf[i] = value;
1990af62f4dSVirupax Sadashivpetimath 	}
2000af62f4dSVirupax Sadashivpetimath 
2010af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
2020af62f4dSVirupax Sadashivpetimath 	secs = mins * 60;
2030af62f4dSVirupax Sadashivpetimath 
2040af62f4dSVirupax Sadashivpetimath 	/* Add back the initially subtracted number of seconds */
2050af62f4dSVirupax Sadashivpetimath 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
2060af62f4dSVirupax Sadashivpetimath 
2070af62f4dSVirupax Sadashivpetimath 	rtc_time_to_tm(secs, &alarm->time);
2080af62f4dSVirupax Sadashivpetimath 
2090af62f4dSVirupax Sadashivpetimath 	return rtc_valid_tm(&alarm->time);
2100af62f4dSVirupax Sadashivpetimath }
2110af62f4dSVirupax Sadashivpetimath 
2120af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
2130af62f4dSVirupax Sadashivpetimath {
21447c16975SMattias Wallin 	return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
21547c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
2160af62f4dSVirupax Sadashivpetimath 		enabled ? RTC_ALARM_ENA : 0);
2170af62f4dSVirupax Sadashivpetimath }
2180af62f4dSVirupax Sadashivpetimath 
2190af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
2200af62f4dSVirupax Sadashivpetimath {
2210af62f4dSVirupax Sadashivpetimath 	int retval, i;
2220af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
223dc43d4a2SRamesh Chandrasekaran 	unsigned long mins, secs = 0, cursec = 0;
224dc43d4a2SRamesh Chandrasekaran 	struct rtc_time curtm;
2250af62f4dSVirupax Sadashivpetimath 
2260af62f4dSVirupax Sadashivpetimath 	if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
2270af62f4dSVirupax Sadashivpetimath 		dev_dbg(dev, "year should be equal to or greater than %d\n",
2280af62f4dSVirupax Sadashivpetimath 				AB8500_RTC_EPOCH);
2290af62f4dSVirupax Sadashivpetimath 		return -EINVAL;
2300af62f4dSVirupax Sadashivpetimath 	}
2310af62f4dSVirupax Sadashivpetimath 
2320af62f4dSVirupax Sadashivpetimath 	/* Get the number of seconds since 1970 */
2330af62f4dSVirupax Sadashivpetimath 	rtc_tm_to_time(&alarm->time, &secs);
2340af62f4dSVirupax Sadashivpetimath 
2350af62f4dSVirupax Sadashivpetimath 	/*
236dc43d4a2SRamesh Chandrasekaran 	 * Check whether alarm is set less than 1min.
237dc43d4a2SRamesh Chandrasekaran 	 * Since our RTC doesn't support alarm resolution less than 1min,
238dc43d4a2SRamesh Chandrasekaran 	 * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON
239dc43d4a2SRamesh Chandrasekaran 	 */
240dc43d4a2SRamesh Chandrasekaran 	ab8500_rtc_read_time(dev, &curtm); /* Read current time */
241dc43d4a2SRamesh Chandrasekaran 	rtc_tm_to_time(&curtm, &cursec);
242dc43d4a2SRamesh Chandrasekaran 	if ((secs - cursec) < 59) {
243dc43d4a2SRamesh Chandrasekaran 		dev_dbg(dev, "Alarm less than 1 minute not supported\r\n");
244dc43d4a2SRamesh Chandrasekaran 		return -EINVAL;
245dc43d4a2SRamesh Chandrasekaran 	}
246dc43d4a2SRamesh Chandrasekaran 
247dc43d4a2SRamesh Chandrasekaran 	/*
2480af62f4dSVirupax Sadashivpetimath 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
2490af62f4dSVirupax Sadashivpetimath 	 * we only have a small counter in the RTC.
2500af62f4dSVirupax Sadashivpetimath 	 */
2510af62f4dSVirupax Sadashivpetimath 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
2520af62f4dSVirupax Sadashivpetimath 
2530af62f4dSVirupax Sadashivpetimath 	mins = secs / 60;
2540af62f4dSVirupax Sadashivpetimath 
2550af62f4dSVirupax Sadashivpetimath 	buf[2] = mins & 0xFF;
2560af62f4dSVirupax Sadashivpetimath 	buf[1] = (mins >> 8) & 0xFF;
2570af62f4dSVirupax Sadashivpetimath 	buf[0] = (mins >> 16) & 0xFF;
2580af62f4dSVirupax Sadashivpetimath 
2590af62f4dSVirupax Sadashivpetimath 	/* Set the alarm time */
2600af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
26147c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
26247c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], buf[i]);
2630af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
2640af62f4dSVirupax Sadashivpetimath 			return retval;
2650af62f4dSVirupax Sadashivpetimath 	}
2660af62f4dSVirupax Sadashivpetimath 
2670af62f4dSVirupax Sadashivpetimath 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
2680af62f4dSVirupax Sadashivpetimath }
2690af62f4dSVirupax Sadashivpetimath 
270dda367acSMark Godfrey 
271dda367acSMark Godfrey static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
272dda367acSMark Godfrey {
273dda367acSMark Godfrey 	int retval;
274dda367acSMark Godfrey 	u8  rtccal = 0;
275dda367acSMark Godfrey 
276dda367acSMark Godfrey 	/*
277dda367acSMark Godfrey 	 * Check that the calibration value (which is in units of 0.5
278dda367acSMark Godfrey 	 * parts-per-million) is in the AB8500's range for RtcCalibration
279dda367acSMark Godfrey 	 * register. -128 (0x80) is not permitted because the AB8500 uses
280dda367acSMark Godfrey 	 * a sign-bit rather than two's complement, so 0x80 is just another
281dda367acSMark Godfrey 	 * representation of zero.
282dda367acSMark Godfrey 	 */
283dda367acSMark Godfrey 	if ((calibration < -127) || (calibration > 127)) {
284dda367acSMark Godfrey 		dev_err(dev, "RtcCalibration value outside permitted range\n");
285dda367acSMark Godfrey 		return -EINVAL;
286dda367acSMark Godfrey 	}
287dda367acSMark Godfrey 
288dda367acSMark Godfrey 	/*
289dda367acSMark Godfrey 	 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
290dda367acSMark Godfrey 	 * so need to convert to this sort of representation before writing
291dda367acSMark Godfrey 	 * into RtcCalibration register...
292dda367acSMark Godfrey 	 */
293dda367acSMark Godfrey 	if (calibration >= 0)
294dda367acSMark Godfrey 		rtccal = 0x7F & calibration;
295dda367acSMark Godfrey 	else
296dda367acSMark Godfrey 		rtccal = ~(calibration - 1) | 0x80;
297dda367acSMark Godfrey 
298dda367acSMark Godfrey 	retval = abx500_set_register_interruptible(dev, AB8500_RTC,
299dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, rtccal);
300dda367acSMark Godfrey 
301dda367acSMark Godfrey 	return retval;
302dda367acSMark Godfrey }
303dda367acSMark Godfrey 
304dda367acSMark Godfrey static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
305dda367acSMark Godfrey {
306dda367acSMark Godfrey 	int retval;
307dda367acSMark Godfrey 	u8  rtccal = 0;
308dda367acSMark Godfrey 
309dda367acSMark Godfrey 	retval =  abx500_get_register_interruptible(dev, AB8500_RTC,
310dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, &rtccal);
311dda367acSMark Godfrey 	if (retval >= 0) {
312dda367acSMark Godfrey 		/*
313dda367acSMark Godfrey 		 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
314dda367acSMark Godfrey 		 * so need to convert value from RtcCalibration register into
315dda367acSMark Godfrey 		 * a two's complement signed value...
316dda367acSMark Godfrey 		 */
317dda367acSMark Godfrey 		if (rtccal & 0x80)
318dda367acSMark Godfrey 			*calibration = 0 - (rtccal & 0x7F);
319dda367acSMark Godfrey 		else
320dda367acSMark Godfrey 			*calibration = 0x7F & rtccal;
321dda367acSMark Godfrey 	}
322dda367acSMark Godfrey 
323dda367acSMark Godfrey 	return retval;
324dda367acSMark Godfrey }
325dda367acSMark Godfrey 
326dda367acSMark Godfrey static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
327dda367acSMark Godfrey 				struct device_attribute *attr,
328dda367acSMark Godfrey 				const char *buf, size_t count)
329dda367acSMark Godfrey {
330dda367acSMark Godfrey 	int retval;
331dda367acSMark Godfrey 	int calibration = 0;
332dda367acSMark Godfrey 
333dda367acSMark Godfrey 	if (sscanf(buf, " %i ", &calibration) != 1) {
334dda367acSMark Godfrey 		dev_err(dev, "Failed to store RTC calibration attribute\n");
335dda367acSMark Godfrey 		return -EINVAL;
336dda367acSMark Godfrey 	}
337dda367acSMark Godfrey 
338dda367acSMark Godfrey 	retval = ab8500_rtc_set_calibration(dev, calibration);
339dda367acSMark Godfrey 
340dda367acSMark Godfrey 	return retval ? retval : count;
341dda367acSMark Godfrey }
342dda367acSMark Godfrey 
343dda367acSMark Godfrey static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
344dda367acSMark Godfrey 				struct device_attribute *attr, char *buf)
345dda367acSMark Godfrey {
346dda367acSMark Godfrey 	int  retval = 0;
347dda367acSMark Godfrey 	int  calibration = 0;
348dda367acSMark Godfrey 
349dda367acSMark Godfrey 	retval = ab8500_rtc_get_calibration(dev, &calibration);
350dda367acSMark Godfrey 	if (retval < 0) {
351dda367acSMark Godfrey 		dev_err(dev, "Failed to read RTC calibration attribute\n");
352dda367acSMark Godfrey 		sprintf(buf, "0\n");
353dda367acSMark Godfrey 		return retval;
354dda367acSMark Godfrey 	}
355dda367acSMark Godfrey 
356dda367acSMark Godfrey 	return sprintf(buf, "%d\n", calibration);
357dda367acSMark Godfrey }
358dda367acSMark Godfrey 
359dda367acSMark Godfrey static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
360dda367acSMark Godfrey 		   ab8500_sysfs_show_rtc_calibration,
361dda367acSMark Godfrey 		   ab8500_sysfs_store_rtc_calibration);
362dda367acSMark Godfrey 
363dda367acSMark Godfrey static int ab8500_sysfs_rtc_register(struct device *dev)
364dda367acSMark Godfrey {
365dda367acSMark Godfrey 	return device_create_file(dev, &dev_attr_rtc_calibration);
366dda367acSMark Godfrey }
367dda367acSMark Godfrey 
368dda367acSMark Godfrey static void ab8500_sysfs_rtc_unregister(struct device *dev)
369dda367acSMark Godfrey {
370dda367acSMark Godfrey 	device_remove_file(dev, &dev_attr_rtc_calibration);
371dda367acSMark Godfrey }
372dda367acSMark Godfrey 
3730af62f4dSVirupax Sadashivpetimath static irqreturn_t rtc_alarm_handler(int irq, void *data)
3740af62f4dSVirupax Sadashivpetimath {
3750af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc = data;
3760af62f4dSVirupax Sadashivpetimath 	unsigned long events = RTC_IRQF | RTC_AF;
3770af62f4dSVirupax Sadashivpetimath 
3780af62f4dSVirupax Sadashivpetimath 	dev_dbg(&rtc->dev, "%s\n", __func__);
3790af62f4dSVirupax Sadashivpetimath 	rtc_update_irq(rtc, 1, events);
3800af62f4dSVirupax Sadashivpetimath 
3810af62f4dSVirupax Sadashivpetimath 	return IRQ_HANDLED;
3820af62f4dSVirupax Sadashivpetimath }
3830af62f4dSVirupax Sadashivpetimath 
3840af62f4dSVirupax Sadashivpetimath static const struct rtc_class_ops ab8500_rtc_ops = {
3850af62f4dSVirupax Sadashivpetimath 	.read_time		= ab8500_rtc_read_time,
3860af62f4dSVirupax Sadashivpetimath 	.set_time		= ab8500_rtc_set_time,
3870af62f4dSVirupax Sadashivpetimath 	.read_alarm		= ab8500_rtc_read_alarm,
3880af62f4dSVirupax Sadashivpetimath 	.set_alarm		= ab8500_rtc_set_alarm,
3890af62f4dSVirupax Sadashivpetimath 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
3900af62f4dSVirupax Sadashivpetimath };
3910af62f4dSVirupax Sadashivpetimath 
392*5a167f45SGreg Kroah-Hartman static int ab8500_rtc_probe(struct platform_device *pdev)
3930af62f4dSVirupax Sadashivpetimath {
3940af62f4dSVirupax Sadashivpetimath 	int err;
3950af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc;
39647c16975SMattias Wallin 	u8 rtc_ctrl;
3970af62f4dSVirupax Sadashivpetimath 	int irq;
3980af62f4dSVirupax Sadashivpetimath 
3990af62f4dSVirupax Sadashivpetimath 	irq = platform_get_irq_byname(pdev, "ALARM");
4000af62f4dSVirupax Sadashivpetimath 	if (irq < 0)
4010af62f4dSVirupax Sadashivpetimath 		return irq;
4020af62f4dSVirupax Sadashivpetimath 
4030af62f4dSVirupax Sadashivpetimath 	/* For RTC supply test */
40447c16975SMattias Wallin 	err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
40547c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
4060af62f4dSVirupax Sadashivpetimath 	if (err < 0)
4070af62f4dSVirupax Sadashivpetimath 		return err;
4080af62f4dSVirupax Sadashivpetimath 
4090af62f4dSVirupax Sadashivpetimath 	/* Wait for reset by the PorRtc */
410012e52e1SLinus Walleij 	usleep_range(1000, 5000);
4110af62f4dSVirupax Sadashivpetimath 
41247c16975SMattias Wallin 	err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
41347c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
41447c16975SMattias Wallin 	if (err < 0)
41547c16975SMattias Wallin 		return err;
4160af62f4dSVirupax Sadashivpetimath 
4170af62f4dSVirupax Sadashivpetimath 	/* Check if the RTC Supply fails */
4180af62f4dSVirupax Sadashivpetimath 	if (!(rtc_ctrl & RTC_STATUS_DATA)) {
4190af62f4dSVirupax Sadashivpetimath 		dev_err(&pdev->dev, "RTC supply failure\n");
4200af62f4dSVirupax Sadashivpetimath 		return -ENODEV;
4210af62f4dSVirupax Sadashivpetimath 	}
4220af62f4dSVirupax Sadashivpetimath 
423b62581e6SAndrew Lynn 	device_init_wakeup(&pdev->dev, true);
424b62581e6SAndrew Lynn 
4250af62f4dSVirupax Sadashivpetimath 	rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops,
4260af62f4dSVirupax Sadashivpetimath 			THIS_MODULE);
4270af62f4dSVirupax Sadashivpetimath 	if (IS_ERR(rtc)) {
4280af62f4dSVirupax Sadashivpetimath 		dev_err(&pdev->dev, "Registration failed\n");
4290af62f4dSVirupax Sadashivpetimath 		err = PTR_ERR(rtc);
4300af62f4dSVirupax Sadashivpetimath 		return err;
4310af62f4dSVirupax Sadashivpetimath 	}
4320af62f4dSVirupax Sadashivpetimath 
43310d065e6SRobert Marklund 	err = request_threaded_irq(irq, NULL, rtc_alarm_handler,
4343cfd16a5SLee Jones 		IRQF_NO_SUSPEND | IRQF_ONESHOT, "ab8500-rtc", rtc);
4350af62f4dSVirupax Sadashivpetimath 	if (err < 0) {
4360af62f4dSVirupax Sadashivpetimath 		rtc_device_unregister(rtc);
4370af62f4dSVirupax Sadashivpetimath 		return err;
4380af62f4dSVirupax Sadashivpetimath 	}
4390af62f4dSVirupax Sadashivpetimath 
4400af62f4dSVirupax Sadashivpetimath 	platform_set_drvdata(pdev, rtc);
4410af62f4dSVirupax Sadashivpetimath 
442dda367acSMark Godfrey 	err = ab8500_sysfs_rtc_register(&pdev->dev);
443dda367acSMark Godfrey 	if (err) {
444dda367acSMark Godfrey 		dev_err(&pdev->dev, "sysfs RTC failed to register\n");
445dda367acSMark Godfrey 		return err;
446dda367acSMark Godfrey 	}
447dda367acSMark Godfrey 
4480af62f4dSVirupax Sadashivpetimath 	return 0;
4490af62f4dSVirupax Sadashivpetimath }
4500af62f4dSVirupax Sadashivpetimath 
451*5a167f45SGreg Kroah-Hartman static int ab8500_rtc_remove(struct platform_device *pdev)
4520af62f4dSVirupax Sadashivpetimath {
4530af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc = platform_get_drvdata(pdev);
4540af62f4dSVirupax Sadashivpetimath 	int irq = platform_get_irq_byname(pdev, "ALARM");
4550af62f4dSVirupax Sadashivpetimath 
456dda367acSMark Godfrey 	ab8500_sysfs_rtc_unregister(&pdev->dev);
457dda367acSMark Godfrey 
4580af62f4dSVirupax Sadashivpetimath 	free_irq(irq, rtc);
4590af62f4dSVirupax Sadashivpetimath 	rtc_device_unregister(rtc);
4600af62f4dSVirupax Sadashivpetimath 	platform_set_drvdata(pdev, NULL);
4610af62f4dSVirupax Sadashivpetimath 
4620af62f4dSVirupax Sadashivpetimath 	return 0;
4630af62f4dSVirupax Sadashivpetimath }
4640af62f4dSVirupax Sadashivpetimath 
4650af62f4dSVirupax Sadashivpetimath static struct platform_driver ab8500_rtc_driver = {
4660af62f4dSVirupax Sadashivpetimath 	.driver = {
4670af62f4dSVirupax Sadashivpetimath 		.name = "ab8500-rtc",
4680af62f4dSVirupax Sadashivpetimath 		.owner = THIS_MODULE,
4690af62f4dSVirupax Sadashivpetimath 	},
4700af62f4dSVirupax Sadashivpetimath 	.probe	= ab8500_rtc_probe,
471*5a167f45SGreg Kroah-Hartman 	.remove = ab8500_rtc_remove,
4720af62f4dSVirupax Sadashivpetimath };
4730af62f4dSVirupax Sadashivpetimath 
4740c4eae66SAxel Lin module_platform_driver(ab8500_rtc_driver);
4750af62f4dSVirupax Sadashivpetimath 
4760af62f4dSVirupax Sadashivpetimath MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
4770af62f4dSVirupax Sadashivpetimath MODULE_DESCRIPTION("AB8500 RTC Driver");
4780af62f4dSVirupax Sadashivpetimath MODULE_LICENSE("GPL v2");
479