xref: /linux/drivers/rtc/rtc-ab8500.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20af62f4dSVirupax Sadashivpetimath /*
30af62f4dSVirupax Sadashivpetimath  * Copyright (C) ST-Ericsson SA 2010
40af62f4dSVirupax Sadashivpetimath  *
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>
2193a6f916SSudeep 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
390af62f4dSVirupax Sadashivpetimath 
400af62f4dSVirupax Sadashivpetimath /* RtcReadRequest bits */
410af62f4dSVirupax Sadashivpetimath #define RTC_READ_REQUEST		0x01
420af62f4dSVirupax Sadashivpetimath #define RTC_WRITE_REQUEST		0x02
430af62f4dSVirupax Sadashivpetimath 
440af62f4dSVirupax Sadashivpetimath /* RtcCtrl bits */
450af62f4dSVirupax Sadashivpetimath #define RTC_ALARM_ENA			0x04
460af62f4dSVirupax Sadashivpetimath #define RTC_STATUS_DATA			0x01
470af62f4dSVirupax Sadashivpetimath 
480af62f4dSVirupax Sadashivpetimath #define COUNTS_PER_SEC			(0xF000 / 60)
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 
ab8500_rtc_read_time(struct device * dev,struct rtc_time * tm)610af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
620af62f4dSVirupax Sadashivpetimath {
630af62f4dSVirupax Sadashivpetimath 	unsigned long timeout = jiffies + HZ;
640af62f4dSVirupax Sadashivpetimath 	int retval, i;
650af62f4dSVirupax Sadashivpetimath 	unsigned long mins, secs;
660af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
6747c16975SMattias Wallin 	u8 value;
680af62f4dSVirupax Sadashivpetimath 
690af62f4dSVirupax Sadashivpetimath 	/* Request a data read */
7047c16975SMattias Wallin 	retval = abx500_set_register_interruptible(dev,
7147c16975SMattias Wallin 		AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
720af62f4dSVirupax Sadashivpetimath 	if (retval < 0)
730af62f4dSVirupax Sadashivpetimath 		return retval;
740af62f4dSVirupax Sadashivpetimath 
750af62f4dSVirupax Sadashivpetimath 	/* Wait for some cycles after enabling the rtc read in ab8500 */
760af62f4dSVirupax Sadashivpetimath 	while (time_before(jiffies, timeout)) {
7747c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
7847c16975SMattias Wallin 			AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
790af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
800af62f4dSVirupax Sadashivpetimath 			return retval;
810af62f4dSVirupax Sadashivpetimath 
8247c16975SMattias Wallin 		if (!(value & RTC_READ_REQUEST))
830af62f4dSVirupax Sadashivpetimath 			break;
840af62f4dSVirupax Sadashivpetimath 
85012e52e1SLinus Walleij 		usleep_range(1000, 5000);
860af62f4dSVirupax Sadashivpetimath 	}
870af62f4dSVirupax Sadashivpetimath 
880af62f4dSVirupax Sadashivpetimath 	/* Read the Watchtime registers */
890af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
9047c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev,
9147c16975SMattias Wallin 			AB8500_RTC, ab8500_rtc_time_regs[i], &value);
920af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
930af62f4dSVirupax Sadashivpetimath 			return retval;
9447c16975SMattias Wallin 		buf[i] = value;
950af62f4dSVirupax Sadashivpetimath 	}
960af62f4dSVirupax Sadashivpetimath 
970af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
980af62f4dSVirupax Sadashivpetimath 
990af62f4dSVirupax Sadashivpetimath 	secs =	(buf[3] << 8) | buf[4];
1000af62f4dSVirupax Sadashivpetimath 	secs =	secs / COUNTS_PER_SEC;
1010af62f4dSVirupax Sadashivpetimath 	secs =	secs + (mins * 60);
1020af62f4dSVirupax Sadashivpetimath 
103a5965a31SAlexandre Belloni 	rtc_time64_to_tm(secs, tm);
104ab62670eSAlexandre Belloni 	return 0;
1050af62f4dSVirupax Sadashivpetimath }
1060af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_set_time(struct device * dev,struct rtc_time * tm)1070af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
1080af62f4dSVirupax Sadashivpetimath {
1090af62f4dSVirupax Sadashivpetimath 	int retval, i;
1100af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
1110af62f4dSVirupax Sadashivpetimath 	unsigned long no_secs, no_mins, secs = 0;
1120af62f4dSVirupax Sadashivpetimath 
113a5965a31SAlexandre Belloni 	secs = rtc_tm_to_time64(tm);
1140af62f4dSVirupax Sadashivpetimath 
1150af62f4dSVirupax Sadashivpetimath 	no_mins = secs / 60;
1160af62f4dSVirupax Sadashivpetimath 
1170af62f4dSVirupax Sadashivpetimath 	no_secs = secs % 60;
1180af62f4dSVirupax Sadashivpetimath 	/* Make the seconds count as per the RTC resolution */
1190af62f4dSVirupax Sadashivpetimath 	no_secs = no_secs * COUNTS_PER_SEC;
1200af62f4dSVirupax Sadashivpetimath 
1210af62f4dSVirupax Sadashivpetimath 	buf[4] = no_secs & 0xFF;
1220af62f4dSVirupax Sadashivpetimath 	buf[3] = (no_secs >> 8) & 0xFF;
1230af62f4dSVirupax Sadashivpetimath 
1240af62f4dSVirupax Sadashivpetimath 	buf[2] = no_mins & 0xFF;
1250af62f4dSVirupax Sadashivpetimath 	buf[1] = (no_mins >> 8) & 0xFF;
1260af62f4dSVirupax Sadashivpetimath 	buf[0] = (no_mins >> 16) & 0xFF;
1270af62f4dSVirupax Sadashivpetimath 
1280af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
12947c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
13047c16975SMattias Wallin 			ab8500_rtc_time_regs[i], buf[i]);
1310af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1320af62f4dSVirupax Sadashivpetimath 			return retval;
1330af62f4dSVirupax Sadashivpetimath 	}
1340af62f4dSVirupax Sadashivpetimath 
1350af62f4dSVirupax Sadashivpetimath 	/* Request a data write */
13647c16975SMattias Wallin 	return abx500_set_register_interruptible(dev, AB8500_RTC,
13747c16975SMattias Wallin 		AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
1380af62f4dSVirupax Sadashivpetimath }
1390af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)1400af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
1410af62f4dSVirupax Sadashivpetimath {
1420af62f4dSVirupax Sadashivpetimath 	int retval, i;
14347c16975SMattias Wallin 	u8 rtc_ctrl, value;
1440af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
1450af62f4dSVirupax Sadashivpetimath 	unsigned long secs, mins;
1460af62f4dSVirupax Sadashivpetimath 
1470af62f4dSVirupax Sadashivpetimath 	/* Check if the alarm is enabled or not */
14847c16975SMattias Wallin 	retval = abx500_get_register_interruptible(dev, AB8500_RTC,
14947c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
15047c16975SMattias Wallin 	if (retval < 0)
15147c16975SMattias Wallin 		return retval;
1520af62f4dSVirupax Sadashivpetimath 
1530af62f4dSVirupax Sadashivpetimath 	if (rtc_ctrl & RTC_ALARM_ENA)
1540af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 1;
1550af62f4dSVirupax Sadashivpetimath 	else
1560af62f4dSVirupax Sadashivpetimath 		alarm->enabled = 0;
1570af62f4dSVirupax Sadashivpetimath 
1580af62f4dSVirupax Sadashivpetimath 	alarm->pending = 0;
1590af62f4dSVirupax Sadashivpetimath 
1600af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
16147c16975SMattias Wallin 		retval = abx500_get_register_interruptible(dev, AB8500_RTC,
16247c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], &value);
1630af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
1640af62f4dSVirupax Sadashivpetimath 			return retval;
16547c16975SMattias Wallin 		buf[i] = value;
1660af62f4dSVirupax Sadashivpetimath 	}
1670af62f4dSVirupax Sadashivpetimath 
1680af62f4dSVirupax Sadashivpetimath 	mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1690af62f4dSVirupax Sadashivpetimath 	secs = mins * 60;
1700af62f4dSVirupax Sadashivpetimath 
171a5965a31SAlexandre Belloni 	rtc_time64_to_tm(secs, &alarm->time);
1720af62f4dSVirupax Sadashivpetimath 
1739a90a5bcSAlexandre Belloni 	return 0;
1740af62f4dSVirupax Sadashivpetimath }
1750af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_irq_enable(struct device * dev,unsigned int enabled)1760af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
1770af62f4dSVirupax Sadashivpetimath {
17847c16975SMattias Wallin 	return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
17947c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
1800af62f4dSVirupax Sadashivpetimath 		enabled ? RTC_ALARM_ENA : 0);
1810af62f4dSVirupax Sadashivpetimath }
1820af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)1830af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
1840af62f4dSVirupax Sadashivpetimath {
1850af62f4dSVirupax Sadashivpetimath 	int retval, i;
1860af62f4dSVirupax Sadashivpetimath 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
187a5f82803SAlexandre Belloni 	unsigned long mins;
1880af62f4dSVirupax Sadashivpetimath 
189a5f82803SAlexandre Belloni 	mins = (unsigned long)rtc_tm_to_time64(&alarm->time) / 60;
1900af62f4dSVirupax Sadashivpetimath 
1910af62f4dSVirupax Sadashivpetimath 	buf[2] = mins & 0xFF;
1920af62f4dSVirupax Sadashivpetimath 	buf[1] = (mins >> 8) & 0xFF;
1930af62f4dSVirupax Sadashivpetimath 	buf[0] = (mins >> 16) & 0xFF;
1940af62f4dSVirupax Sadashivpetimath 
1950af62f4dSVirupax Sadashivpetimath 	/* Set the alarm time */
1960af62f4dSVirupax Sadashivpetimath 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
19747c16975SMattias Wallin 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
19847c16975SMattias Wallin 			ab8500_rtc_alarm_regs[i], buf[i]);
1990af62f4dSVirupax Sadashivpetimath 		if (retval < 0)
2000af62f4dSVirupax Sadashivpetimath 			return retval;
2010af62f4dSVirupax Sadashivpetimath 	}
2020af62f4dSVirupax Sadashivpetimath 
2030af62f4dSVirupax Sadashivpetimath 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
2040af62f4dSVirupax Sadashivpetimath }
2050af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_set_calibration(struct device * dev,int calibration)206dda367acSMark Godfrey static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
207dda367acSMark Godfrey {
208dda367acSMark Godfrey 	int retval;
209dda367acSMark Godfrey 	u8  rtccal = 0;
210dda367acSMark Godfrey 
211dda367acSMark Godfrey 	/*
212dda367acSMark Godfrey 	 * Check that the calibration value (which is in units of 0.5
213dda367acSMark Godfrey 	 * parts-per-million) is in the AB8500's range for RtcCalibration
214dda367acSMark Godfrey 	 * register. -128 (0x80) is not permitted because the AB8500 uses
215dda367acSMark Godfrey 	 * a sign-bit rather than two's complement, so 0x80 is just another
216dda367acSMark Godfrey 	 * representation of zero.
217dda367acSMark Godfrey 	 */
218dda367acSMark Godfrey 	if ((calibration < -127) || (calibration > 127)) {
219dda367acSMark Godfrey 		dev_err(dev, "RtcCalibration value outside permitted range\n");
220dda367acSMark Godfrey 		return -EINVAL;
221dda367acSMark Godfrey 	}
222dda367acSMark Godfrey 
223dda367acSMark Godfrey 	/*
224dda367acSMark Godfrey 	 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
225dda367acSMark Godfrey 	 * so need to convert to this sort of representation before writing
226dda367acSMark Godfrey 	 * into RtcCalibration register...
227dda367acSMark Godfrey 	 */
228dda367acSMark Godfrey 	if (calibration >= 0)
229dda367acSMark Godfrey 		rtccal = 0x7F & calibration;
230dda367acSMark Godfrey 	else
231dda367acSMark Godfrey 		rtccal = ~(calibration - 1) | 0x80;
232dda367acSMark Godfrey 
233dda367acSMark Godfrey 	retval = abx500_set_register_interruptible(dev, AB8500_RTC,
234dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, rtccal);
235dda367acSMark Godfrey 
236dda367acSMark Godfrey 	return retval;
237dda367acSMark Godfrey }
238dda367acSMark Godfrey 
ab8500_rtc_get_calibration(struct device * dev,int * calibration)239dda367acSMark Godfrey static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
240dda367acSMark Godfrey {
241dda367acSMark Godfrey 	int retval;
242dda367acSMark Godfrey 	u8  rtccal = 0;
243dda367acSMark Godfrey 
244dda367acSMark Godfrey 	retval =  abx500_get_register_interruptible(dev, AB8500_RTC,
245dda367acSMark Godfrey 			AB8500_RTC_CALIB_REG, &rtccal);
246dda367acSMark Godfrey 	if (retval >= 0) {
247dda367acSMark Godfrey 		/*
248dda367acSMark Godfrey 		 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
249dda367acSMark Godfrey 		 * so need to convert value from RtcCalibration register into
250dda367acSMark Godfrey 		 * a two's complement signed value...
251dda367acSMark Godfrey 		 */
252dda367acSMark Godfrey 		if (rtccal & 0x80)
253dda367acSMark Godfrey 			*calibration = 0 - (rtccal & 0x7F);
254dda367acSMark Godfrey 		else
255dda367acSMark Godfrey 			*calibration = 0x7F & rtccal;
256dda367acSMark Godfrey 	}
257dda367acSMark Godfrey 
258dda367acSMark Godfrey 	return retval;
259dda367acSMark Godfrey }
260dda367acSMark Godfrey 
ab8500_sysfs_store_rtc_calibration(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)261dda367acSMark Godfrey static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
262dda367acSMark Godfrey 				struct device_attribute *attr,
263dda367acSMark Godfrey 				const char *buf, size_t count)
264dda367acSMark Godfrey {
265dda367acSMark Godfrey 	int retval;
266dda367acSMark Godfrey 	int calibration = 0;
267dda367acSMark Godfrey 
268dda367acSMark Godfrey 	if (sscanf(buf, " %i ", &calibration) != 1) {
269dda367acSMark Godfrey 		dev_err(dev, "Failed to store RTC calibration attribute\n");
270dda367acSMark Godfrey 		return -EINVAL;
271dda367acSMark Godfrey 	}
272dda367acSMark Godfrey 
273dda367acSMark Godfrey 	retval = ab8500_rtc_set_calibration(dev, calibration);
274dda367acSMark Godfrey 
275dda367acSMark Godfrey 	return retval ? retval : count;
276dda367acSMark Godfrey }
277dda367acSMark Godfrey 
ab8500_sysfs_show_rtc_calibration(struct device * dev,struct device_attribute * attr,char * buf)278dda367acSMark Godfrey static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
279dda367acSMark Godfrey 				struct device_attribute *attr, char *buf)
280dda367acSMark Godfrey {
281dda367acSMark Godfrey 	int  retval = 0;
282dda367acSMark Godfrey 	int  calibration = 0;
283dda367acSMark Godfrey 
284dda367acSMark Godfrey 	retval = ab8500_rtc_get_calibration(dev, &calibration);
285dda367acSMark Godfrey 	if (retval < 0) {
286dda367acSMark Godfrey 		dev_err(dev, "Failed to read RTC calibration attribute\n");
287dda367acSMark Godfrey 		sprintf(buf, "0\n");
288dda367acSMark Godfrey 		return retval;
289dda367acSMark Godfrey 	}
290dda367acSMark Godfrey 
291dda367acSMark Godfrey 	return sprintf(buf, "%d\n", calibration);
292dda367acSMark Godfrey }
293dda367acSMark Godfrey 
294dda367acSMark Godfrey static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
295dda367acSMark Godfrey 		   ab8500_sysfs_show_rtc_calibration,
296dda367acSMark Godfrey 		   ab8500_sysfs_store_rtc_calibration);
297dda367acSMark Godfrey 
298b56295ddSAlexandre Belloni static struct attribute *ab8500_rtc_attrs[] = {
299b56295ddSAlexandre Belloni 	&dev_attr_rtc_calibration.attr,
300b56295ddSAlexandre Belloni 	NULL
301b56295ddSAlexandre Belloni };
302dda367acSMark Godfrey 
303b56295ddSAlexandre Belloni static const struct attribute_group ab8500_rtc_sysfs_files = {
304b56295ddSAlexandre Belloni 	.attrs	= ab8500_rtc_attrs,
305b56295ddSAlexandre Belloni };
306dda367acSMark Godfrey 
rtc_alarm_handler(int irq,void * data)3070af62f4dSVirupax Sadashivpetimath static irqreturn_t rtc_alarm_handler(int irq, void *data)
3080af62f4dSVirupax Sadashivpetimath {
3090af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc = data;
3100af62f4dSVirupax Sadashivpetimath 	unsigned long events = RTC_IRQF | RTC_AF;
3110af62f4dSVirupax Sadashivpetimath 
3120af62f4dSVirupax Sadashivpetimath 	dev_dbg(&rtc->dev, "%s\n", __func__);
3130af62f4dSVirupax Sadashivpetimath 	rtc_update_irq(rtc, 1, events);
3140af62f4dSVirupax Sadashivpetimath 
3150af62f4dSVirupax Sadashivpetimath 	return IRQ_HANDLED;
3160af62f4dSVirupax Sadashivpetimath }
3170af62f4dSVirupax Sadashivpetimath 
3180af62f4dSVirupax Sadashivpetimath static const struct rtc_class_ops ab8500_rtc_ops = {
3190af62f4dSVirupax Sadashivpetimath 	.read_time		= ab8500_rtc_read_time,
3200af62f4dSVirupax Sadashivpetimath 	.set_time		= ab8500_rtc_set_time,
3210af62f4dSVirupax Sadashivpetimath 	.read_alarm		= ab8500_rtc_read_alarm,
3220af62f4dSVirupax Sadashivpetimath 	.set_alarm		= ab8500_rtc_set_alarm,
3230af62f4dSVirupax Sadashivpetimath 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
3240af62f4dSVirupax Sadashivpetimath };
3250af62f4dSVirupax Sadashivpetimath 
3269a72f410SKrzysztof Kozlowski static const struct platform_device_id ab85xx_rtc_ids[] = {
32725d053cfSAlexandre Torgue 	{ "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
3288a67e931SFabio Estevam 	{ /* sentinel */ }
32925d053cfSAlexandre Torgue };
33063074cc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
33125d053cfSAlexandre Torgue 
ab8500_rtc_probe(struct platform_device * pdev)3325a167f45SGreg Kroah-Hartman static int ab8500_rtc_probe(struct platform_device *pdev)
3330af62f4dSVirupax Sadashivpetimath {
33425d053cfSAlexandre Torgue 	const struct platform_device_id *platid = platform_get_device_id(pdev);
3350af62f4dSVirupax Sadashivpetimath 	int err;
3360af62f4dSVirupax Sadashivpetimath 	struct rtc_device *rtc;
33747c16975SMattias Wallin 	u8 rtc_ctrl;
3380af62f4dSVirupax Sadashivpetimath 	int irq;
3390af62f4dSVirupax Sadashivpetimath 
3400af62f4dSVirupax Sadashivpetimath 	irq = platform_get_irq_byname(pdev, "ALARM");
3410af62f4dSVirupax Sadashivpetimath 	if (irq < 0)
3420af62f4dSVirupax Sadashivpetimath 		return irq;
3430af62f4dSVirupax Sadashivpetimath 
3440af62f4dSVirupax Sadashivpetimath 	/* For RTC supply test */
34547c16975SMattias Wallin 	err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
34647c16975SMattias Wallin 		AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
3470af62f4dSVirupax Sadashivpetimath 	if (err < 0)
3480af62f4dSVirupax Sadashivpetimath 		return err;
3490af62f4dSVirupax Sadashivpetimath 
3500af62f4dSVirupax Sadashivpetimath 	/* Wait for reset by the PorRtc */
351012e52e1SLinus Walleij 	usleep_range(1000, 5000);
3520af62f4dSVirupax Sadashivpetimath 
35347c16975SMattias Wallin 	err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
35447c16975SMattias Wallin 		AB8500_RTC_STAT_REG, &rtc_ctrl);
35547c16975SMattias Wallin 	if (err < 0)
35647c16975SMattias Wallin 		return err;
3570af62f4dSVirupax Sadashivpetimath 
3580af62f4dSVirupax Sadashivpetimath 	/* Check if the RTC Supply fails */
3590af62f4dSVirupax Sadashivpetimath 	if (!(rtc_ctrl & RTC_STATUS_DATA)) {
3600af62f4dSVirupax Sadashivpetimath 		dev_err(&pdev->dev, "RTC supply failure\n");
3610af62f4dSVirupax Sadashivpetimath 		return -ENODEV;
3620af62f4dSVirupax Sadashivpetimath 	}
3630af62f4dSVirupax Sadashivpetimath 
364b62581e6SAndrew Lynn 	device_init_wakeup(&pdev->dev, true);
365b62581e6SAndrew Lynn 
366b56295ddSAlexandre Belloni 	rtc = devm_rtc_allocate_device(&pdev->dev);
367b56295ddSAlexandre Belloni 	if (IS_ERR(rtc))
368b56295ddSAlexandre Belloni 		return PTR_ERR(rtc);
369b56295ddSAlexandre Belloni 
370b56295ddSAlexandre Belloni 	rtc->ops = (struct rtc_class_ops *)platid->driver_data;
3710af62f4dSVirupax Sadashivpetimath 
372fa11f7e7SJingoo Han 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
37393a6f916SSudeep Holla 			rtc_alarm_handler, IRQF_ONESHOT,
374fa11f7e7SJingoo Han 			"ab8500-rtc", rtc);
375fa11f7e7SJingoo Han 	if (err < 0)
3760af62f4dSVirupax Sadashivpetimath 		return err;
3770af62f4dSVirupax Sadashivpetimath 
37893a6f916SSudeep Holla 	dev_pm_set_wake_irq(&pdev->dev, irq);
3790af62f4dSVirupax Sadashivpetimath 	platform_set_drvdata(pdev, rtc);
3800af62f4dSVirupax Sadashivpetimath 
381a5f82803SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features);
382a5f82803SAlexandre Belloni 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
383c594d678SXunlei Pang 
38438ab97aeSAlexandre Belloni 	rtc->range_max = (1ULL << 24) * 60 - 1; // 24-bit minutes + 59 secs
38538ab97aeSAlexandre Belloni 	rtc->start_secs = RTC_TIMESTAMP_BEGIN_2000;
38638ab97aeSAlexandre Belloni 	rtc->set_start_time = true;
38738ab97aeSAlexandre Belloni 
388b56295ddSAlexandre Belloni 	err = rtc_add_group(rtc, &ab8500_rtc_sysfs_files);
389b56295ddSAlexandre Belloni 	if (err)
390b56295ddSAlexandre Belloni 		return err;
391b56295ddSAlexandre Belloni 
392fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(rtc);
3930af62f4dSVirupax Sadashivpetimath }
3940af62f4dSVirupax Sadashivpetimath 
ab8500_rtc_remove(struct platform_device * pdev)395*8a700af1SUwe Kleine-König static void ab8500_rtc_remove(struct platform_device *pdev)
3960af62f4dSVirupax Sadashivpetimath {
39793a6f916SSudeep Holla 	dev_pm_clear_wake_irq(&pdev->dev);
39893a6f916SSudeep Holla 	device_init_wakeup(&pdev->dev, false);
3990af62f4dSVirupax Sadashivpetimath }
4000af62f4dSVirupax Sadashivpetimath 
4010af62f4dSVirupax Sadashivpetimath static struct platform_driver ab8500_rtc_driver = {
4020af62f4dSVirupax Sadashivpetimath 	.driver = {
4030af62f4dSVirupax Sadashivpetimath 		.name = "ab8500-rtc",
4040af62f4dSVirupax Sadashivpetimath 	},
4050af62f4dSVirupax Sadashivpetimath 	.probe	= ab8500_rtc_probe,
406*8a700af1SUwe Kleine-König 	.remove_new = ab8500_rtc_remove,
40725d053cfSAlexandre Torgue 	.id_table = ab85xx_rtc_ids,
4080af62f4dSVirupax Sadashivpetimath };
4090af62f4dSVirupax Sadashivpetimath 
4100c4eae66SAxel Lin module_platform_driver(ab8500_rtc_driver);
4110af62f4dSVirupax Sadashivpetimath 
4120af62f4dSVirupax Sadashivpetimath MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
4130af62f4dSVirupax Sadashivpetimath MODULE_DESCRIPTION("AB8500 RTC Driver");
4140af62f4dSVirupax Sadashivpetimath MODULE_LICENSE("GPL v2");
415