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> 20*ad49fcbeSLee 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 /* Early AB8500 chips will not clear the rtc read request bit */ 9347c16975SMattias Wallin if (abx500_get_chip_id(dev) == 0) { 94012e52e1SLinus Walleij usleep_range(1000, 1000); 950af62f4dSVirupax Sadashivpetimath } else { 960af62f4dSVirupax Sadashivpetimath /* Wait for some cycles after enabling the rtc read in ab8500 */ 970af62f4dSVirupax Sadashivpetimath while (time_before(jiffies, timeout)) { 9847c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, 9947c16975SMattias Wallin AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value); 1000af62f4dSVirupax Sadashivpetimath if (retval < 0) 1010af62f4dSVirupax Sadashivpetimath return retval; 1020af62f4dSVirupax Sadashivpetimath 10347c16975SMattias Wallin if (!(value & RTC_READ_REQUEST)) 1040af62f4dSVirupax Sadashivpetimath break; 1050af62f4dSVirupax Sadashivpetimath 106012e52e1SLinus Walleij usleep_range(1000, 5000); 1070af62f4dSVirupax Sadashivpetimath } 1080af62f4dSVirupax Sadashivpetimath } 1090af62f4dSVirupax Sadashivpetimath 1100af62f4dSVirupax Sadashivpetimath /* Read the Watchtime registers */ 1110af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 11247c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, 11347c16975SMattias Wallin AB8500_RTC, ab8500_rtc_time_regs[i], &value); 1140af62f4dSVirupax Sadashivpetimath if (retval < 0) 1150af62f4dSVirupax Sadashivpetimath return retval; 11647c16975SMattias Wallin buf[i] = value; 1170af62f4dSVirupax Sadashivpetimath } 1180af62f4dSVirupax Sadashivpetimath 1190af62f4dSVirupax Sadashivpetimath mins = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 1200af62f4dSVirupax Sadashivpetimath 1210af62f4dSVirupax Sadashivpetimath secs = (buf[3] << 8) | buf[4]; 1220af62f4dSVirupax Sadashivpetimath secs = secs / COUNTS_PER_SEC; 1230af62f4dSVirupax Sadashivpetimath secs = secs + (mins * 60); 1240af62f4dSVirupax Sadashivpetimath 1250af62f4dSVirupax Sadashivpetimath /* Add back the initially subtracted number of seconds */ 1260af62f4dSVirupax Sadashivpetimath secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 1270af62f4dSVirupax Sadashivpetimath 1280af62f4dSVirupax Sadashivpetimath rtc_time_to_tm(secs, tm); 1290af62f4dSVirupax Sadashivpetimath return rtc_valid_tm(tm); 1300af62f4dSVirupax Sadashivpetimath } 1310af62f4dSVirupax Sadashivpetimath 1320af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm) 1330af62f4dSVirupax Sadashivpetimath { 1340af62f4dSVirupax Sadashivpetimath int retval, i; 1350af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 1360af62f4dSVirupax Sadashivpetimath unsigned long no_secs, no_mins, secs = 0; 1370af62f4dSVirupax Sadashivpetimath 1380af62f4dSVirupax Sadashivpetimath if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) { 1390af62f4dSVirupax Sadashivpetimath dev_dbg(dev, "year should be equal to or greater than %d\n", 1400af62f4dSVirupax Sadashivpetimath AB8500_RTC_EPOCH); 1410af62f4dSVirupax Sadashivpetimath return -EINVAL; 1420af62f4dSVirupax Sadashivpetimath } 1430af62f4dSVirupax Sadashivpetimath 1440af62f4dSVirupax Sadashivpetimath /* Get the number of seconds since 1970 */ 1450af62f4dSVirupax Sadashivpetimath rtc_tm_to_time(tm, &secs); 1460af62f4dSVirupax Sadashivpetimath 1470af62f4dSVirupax Sadashivpetimath /* 1480af62f4dSVirupax Sadashivpetimath * Convert it to the number of seconds since 01-01-2000 00:00:00, since 1490af62f4dSVirupax Sadashivpetimath * we only have a small counter in the RTC. 1500af62f4dSVirupax Sadashivpetimath */ 1510af62f4dSVirupax Sadashivpetimath secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 1520af62f4dSVirupax Sadashivpetimath 1530af62f4dSVirupax Sadashivpetimath no_mins = secs / 60; 1540af62f4dSVirupax Sadashivpetimath 1550af62f4dSVirupax Sadashivpetimath no_secs = secs % 60; 1560af62f4dSVirupax Sadashivpetimath /* Make the seconds count as per the RTC resolution */ 1570af62f4dSVirupax Sadashivpetimath no_secs = no_secs * COUNTS_PER_SEC; 1580af62f4dSVirupax Sadashivpetimath 1590af62f4dSVirupax Sadashivpetimath buf[4] = no_secs & 0xFF; 1600af62f4dSVirupax Sadashivpetimath buf[3] = (no_secs >> 8) & 0xFF; 1610af62f4dSVirupax Sadashivpetimath 1620af62f4dSVirupax Sadashivpetimath buf[2] = no_mins & 0xFF; 1630af62f4dSVirupax Sadashivpetimath buf[1] = (no_mins >> 8) & 0xFF; 1640af62f4dSVirupax Sadashivpetimath buf[0] = (no_mins >> 16) & 0xFF; 1650af62f4dSVirupax Sadashivpetimath 1660af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 16747c16975SMattias Wallin retval = abx500_set_register_interruptible(dev, AB8500_RTC, 16847c16975SMattias Wallin ab8500_rtc_time_regs[i], buf[i]); 1690af62f4dSVirupax Sadashivpetimath if (retval < 0) 1700af62f4dSVirupax Sadashivpetimath return retval; 1710af62f4dSVirupax Sadashivpetimath } 1720af62f4dSVirupax Sadashivpetimath 1730af62f4dSVirupax Sadashivpetimath /* Request a data write */ 17447c16975SMattias Wallin return abx500_set_register_interruptible(dev, AB8500_RTC, 17547c16975SMattias Wallin AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST); 1760af62f4dSVirupax Sadashivpetimath } 1770af62f4dSVirupax Sadashivpetimath 1780af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 1790af62f4dSVirupax Sadashivpetimath { 1800af62f4dSVirupax Sadashivpetimath int retval, i; 18147c16975SMattias Wallin u8 rtc_ctrl, value; 1820af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 1830af62f4dSVirupax Sadashivpetimath unsigned long secs, mins; 1840af62f4dSVirupax Sadashivpetimath 1850af62f4dSVirupax Sadashivpetimath /* Check if the alarm is enabled or not */ 18647c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, AB8500_RTC, 18747c16975SMattias Wallin AB8500_RTC_STAT_REG, &rtc_ctrl); 18847c16975SMattias Wallin if (retval < 0) 18947c16975SMattias Wallin return retval; 1900af62f4dSVirupax Sadashivpetimath 1910af62f4dSVirupax Sadashivpetimath if (rtc_ctrl & RTC_ALARM_ENA) 1920af62f4dSVirupax Sadashivpetimath alarm->enabled = 1; 1930af62f4dSVirupax Sadashivpetimath else 1940af62f4dSVirupax Sadashivpetimath alarm->enabled = 0; 1950af62f4dSVirupax Sadashivpetimath 1960af62f4dSVirupax Sadashivpetimath alarm->pending = 0; 1970af62f4dSVirupax Sadashivpetimath 1980af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 19947c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, AB8500_RTC, 20047c16975SMattias Wallin ab8500_rtc_alarm_regs[i], &value); 2010af62f4dSVirupax Sadashivpetimath if (retval < 0) 2020af62f4dSVirupax Sadashivpetimath return retval; 20347c16975SMattias Wallin buf[i] = value; 2040af62f4dSVirupax Sadashivpetimath } 2050af62f4dSVirupax Sadashivpetimath 2060af62f4dSVirupax Sadashivpetimath mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]); 2070af62f4dSVirupax Sadashivpetimath secs = mins * 60; 2080af62f4dSVirupax Sadashivpetimath 2090af62f4dSVirupax Sadashivpetimath /* Add back the initially subtracted number of seconds */ 2100af62f4dSVirupax Sadashivpetimath secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 2110af62f4dSVirupax Sadashivpetimath 2120af62f4dSVirupax Sadashivpetimath rtc_time_to_tm(secs, &alarm->time); 2130af62f4dSVirupax Sadashivpetimath 2140af62f4dSVirupax Sadashivpetimath return rtc_valid_tm(&alarm->time); 2150af62f4dSVirupax Sadashivpetimath } 2160af62f4dSVirupax Sadashivpetimath 2170af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled) 2180af62f4dSVirupax Sadashivpetimath { 21947c16975SMattias Wallin return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC, 22047c16975SMattias Wallin AB8500_RTC_STAT_REG, RTC_ALARM_ENA, 2210af62f4dSVirupax Sadashivpetimath enabled ? RTC_ALARM_ENA : 0); 2220af62f4dSVirupax Sadashivpetimath } 2230af62f4dSVirupax Sadashivpetimath 2240af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 2250af62f4dSVirupax Sadashivpetimath { 2260af62f4dSVirupax Sadashivpetimath int retval, i; 2270af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 2280af62f4dSVirupax Sadashivpetimath unsigned long mins, secs = 0; 2290af62f4dSVirupax Sadashivpetimath 2300af62f4dSVirupax Sadashivpetimath if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) { 2310af62f4dSVirupax Sadashivpetimath dev_dbg(dev, "year should be equal to or greater than %d\n", 2320af62f4dSVirupax Sadashivpetimath AB8500_RTC_EPOCH); 2330af62f4dSVirupax Sadashivpetimath return -EINVAL; 2340af62f4dSVirupax Sadashivpetimath } 2350af62f4dSVirupax Sadashivpetimath 2360af62f4dSVirupax Sadashivpetimath /* Get the number of seconds since 1970 */ 2370af62f4dSVirupax Sadashivpetimath rtc_tm_to_time(&alarm->time, &secs); 2380af62f4dSVirupax Sadashivpetimath 2390af62f4dSVirupax Sadashivpetimath /* 2400af62f4dSVirupax Sadashivpetimath * Convert it to the number of seconds since 01-01-2000 00:00:00, since 2410af62f4dSVirupax Sadashivpetimath * we only have a small counter in the RTC. 2420af62f4dSVirupax Sadashivpetimath */ 2430af62f4dSVirupax Sadashivpetimath secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 2440af62f4dSVirupax Sadashivpetimath 2450af62f4dSVirupax Sadashivpetimath mins = secs / 60; 2460af62f4dSVirupax Sadashivpetimath 2470af62f4dSVirupax Sadashivpetimath buf[2] = mins & 0xFF; 2480af62f4dSVirupax Sadashivpetimath buf[1] = (mins >> 8) & 0xFF; 2490af62f4dSVirupax Sadashivpetimath buf[0] = (mins >> 16) & 0xFF; 2500af62f4dSVirupax Sadashivpetimath 2510af62f4dSVirupax Sadashivpetimath /* Set the alarm time */ 2520af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 25347c16975SMattias Wallin retval = abx500_set_register_interruptible(dev, AB8500_RTC, 25447c16975SMattias Wallin ab8500_rtc_alarm_regs[i], buf[i]); 2550af62f4dSVirupax Sadashivpetimath if (retval < 0) 2560af62f4dSVirupax Sadashivpetimath return retval; 2570af62f4dSVirupax Sadashivpetimath } 2580af62f4dSVirupax Sadashivpetimath 2590af62f4dSVirupax Sadashivpetimath return ab8500_rtc_irq_enable(dev, alarm->enabled); 2600af62f4dSVirupax Sadashivpetimath } 2610af62f4dSVirupax Sadashivpetimath 262dda367acSMark Godfrey 263dda367acSMark Godfrey static int ab8500_rtc_set_calibration(struct device *dev, int calibration) 264dda367acSMark Godfrey { 265dda367acSMark Godfrey int retval; 266dda367acSMark Godfrey u8 rtccal = 0; 267dda367acSMark Godfrey 268dda367acSMark Godfrey /* 269dda367acSMark Godfrey * Check that the calibration value (which is in units of 0.5 270dda367acSMark Godfrey * parts-per-million) is in the AB8500's range for RtcCalibration 271dda367acSMark Godfrey * register. -128 (0x80) is not permitted because the AB8500 uses 272dda367acSMark Godfrey * a sign-bit rather than two's complement, so 0x80 is just another 273dda367acSMark Godfrey * representation of zero. 274dda367acSMark Godfrey */ 275dda367acSMark Godfrey if ((calibration < -127) || (calibration > 127)) { 276dda367acSMark Godfrey dev_err(dev, "RtcCalibration value outside permitted range\n"); 277dda367acSMark Godfrey return -EINVAL; 278dda367acSMark Godfrey } 279dda367acSMark Godfrey 280dda367acSMark Godfrey /* 281dda367acSMark Godfrey * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) 282dda367acSMark Godfrey * so need to convert to this sort of representation before writing 283dda367acSMark Godfrey * into RtcCalibration register... 284dda367acSMark Godfrey */ 285dda367acSMark Godfrey if (calibration >= 0) 286dda367acSMark Godfrey rtccal = 0x7F & calibration; 287dda367acSMark Godfrey else 288dda367acSMark Godfrey rtccal = ~(calibration - 1) | 0x80; 289dda367acSMark Godfrey 290dda367acSMark Godfrey retval = abx500_set_register_interruptible(dev, AB8500_RTC, 291dda367acSMark Godfrey AB8500_RTC_CALIB_REG, rtccal); 292dda367acSMark Godfrey 293dda367acSMark Godfrey return retval; 294dda367acSMark Godfrey } 295dda367acSMark Godfrey 296dda367acSMark Godfrey static int ab8500_rtc_get_calibration(struct device *dev, int *calibration) 297dda367acSMark Godfrey { 298dda367acSMark Godfrey int retval; 299dda367acSMark Godfrey u8 rtccal = 0; 300dda367acSMark Godfrey 301dda367acSMark Godfrey retval = abx500_get_register_interruptible(dev, AB8500_RTC, 302dda367acSMark Godfrey AB8500_RTC_CALIB_REG, &rtccal); 303dda367acSMark Godfrey if (retval >= 0) { 304dda367acSMark Godfrey /* 305dda367acSMark Godfrey * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) 306dda367acSMark Godfrey * so need to convert value from RtcCalibration register into 307dda367acSMark Godfrey * a two's complement signed value... 308dda367acSMark Godfrey */ 309dda367acSMark Godfrey if (rtccal & 0x80) 310dda367acSMark Godfrey *calibration = 0 - (rtccal & 0x7F); 311dda367acSMark Godfrey else 312dda367acSMark Godfrey *calibration = 0x7F & rtccal; 313dda367acSMark Godfrey } 314dda367acSMark Godfrey 315dda367acSMark Godfrey return retval; 316dda367acSMark Godfrey } 317dda367acSMark Godfrey 318dda367acSMark Godfrey static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev, 319dda367acSMark Godfrey struct device_attribute *attr, 320dda367acSMark Godfrey const char *buf, size_t count) 321dda367acSMark Godfrey { 322dda367acSMark Godfrey int retval; 323dda367acSMark Godfrey int calibration = 0; 324dda367acSMark Godfrey 325dda367acSMark Godfrey if (sscanf(buf, " %i ", &calibration) != 1) { 326dda367acSMark Godfrey dev_err(dev, "Failed to store RTC calibration attribute\n"); 327dda367acSMark Godfrey return -EINVAL; 328dda367acSMark Godfrey } 329dda367acSMark Godfrey 330dda367acSMark Godfrey retval = ab8500_rtc_set_calibration(dev, calibration); 331dda367acSMark Godfrey 332dda367acSMark Godfrey return retval ? retval : count; 333dda367acSMark Godfrey } 334dda367acSMark Godfrey 335dda367acSMark Godfrey static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev, 336dda367acSMark Godfrey struct device_attribute *attr, char *buf) 337dda367acSMark Godfrey { 338dda367acSMark Godfrey int retval = 0; 339dda367acSMark Godfrey int calibration = 0; 340dda367acSMark Godfrey 341dda367acSMark Godfrey retval = ab8500_rtc_get_calibration(dev, &calibration); 342dda367acSMark Godfrey if (retval < 0) { 343dda367acSMark Godfrey dev_err(dev, "Failed to read RTC calibration attribute\n"); 344dda367acSMark Godfrey sprintf(buf, "0\n"); 345dda367acSMark Godfrey return retval; 346dda367acSMark Godfrey } 347dda367acSMark Godfrey 348dda367acSMark Godfrey return sprintf(buf, "%d\n", calibration); 349dda367acSMark Godfrey } 350dda367acSMark Godfrey 351dda367acSMark Godfrey static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR, 352dda367acSMark Godfrey ab8500_sysfs_show_rtc_calibration, 353dda367acSMark Godfrey ab8500_sysfs_store_rtc_calibration); 354dda367acSMark Godfrey 355dda367acSMark Godfrey static int ab8500_sysfs_rtc_register(struct device *dev) 356dda367acSMark Godfrey { 357dda367acSMark Godfrey return device_create_file(dev, &dev_attr_rtc_calibration); 358dda367acSMark Godfrey } 359dda367acSMark Godfrey 360dda367acSMark Godfrey static void ab8500_sysfs_rtc_unregister(struct device *dev) 361dda367acSMark Godfrey { 362dda367acSMark Godfrey device_remove_file(dev, &dev_attr_rtc_calibration); 363dda367acSMark Godfrey } 364dda367acSMark Godfrey 3650af62f4dSVirupax Sadashivpetimath static irqreturn_t rtc_alarm_handler(int irq, void *data) 3660af62f4dSVirupax Sadashivpetimath { 3670af62f4dSVirupax Sadashivpetimath struct rtc_device *rtc = data; 3680af62f4dSVirupax Sadashivpetimath unsigned long events = RTC_IRQF | RTC_AF; 3690af62f4dSVirupax Sadashivpetimath 3700af62f4dSVirupax Sadashivpetimath dev_dbg(&rtc->dev, "%s\n", __func__); 3710af62f4dSVirupax Sadashivpetimath rtc_update_irq(rtc, 1, events); 3720af62f4dSVirupax Sadashivpetimath 3730af62f4dSVirupax Sadashivpetimath return IRQ_HANDLED; 3740af62f4dSVirupax Sadashivpetimath } 3750af62f4dSVirupax Sadashivpetimath 3760af62f4dSVirupax Sadashivpetimath static const struct rtc_class_ops ab8500_rtc_ops = { 3770af62f4dSVirupax Sadashivpetimath .read_time = ab8500_rtc_read_time, 3780af62f4dSVirupax Sadashivpetimath .set_time = ab8500_rtc_set_time, 3790af62f4dSVirupax Sadashivpetimath .read_alarm = ab8500_rtc_read_alarm, 3800af62f4dSVirupax Sadashivpetimath .set_alarm = ab8500_rtc_set_alarm, 3810af62f4dSVirupax Sadashivpetimath .alarm_irq_enable = ab8500_rtc_irq_enable, 3820af62f4dSVirupax Sadashivpetimath }; 3830af62f4dSVirupax Sadashivpetimath 3840af62f4dSVirupax Sadashivpetimath static int __devinit ab8500_rtc_probe(struct platform_device *pdev) 3850af62f4dSVirupax Sadashivpetimath { 3860af62f4dSVirupax Sadashivpetimath int err; 3870af62f4dSVirupax Sadashivpetimath struct rtc_device *rtc; 38847c16975SMattias Wallin u8 rtc_ctrl; 3890af62f4dSVirupax Sadashivpetimath int irq; 3900af62f4dSVirupax Sadashivpetimath 3910af62f4dSVirupax Sadashivpetimath irq = platform_get_irq_byname(pdev, "ALARM"); 3920af62f4dSVirupax Sadashivpetimath if (irq < 0) 3930af62f4dSVirupax Sadashivpetimath return irq; 3940af62f4dSVirupax Sadashivpetimath 3950af62f4dSVirupax Sadashivpetimath /* For RTC supply test */ 39647c16975SMattias Wallin err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC, 39747c16975SMattias Wallin AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA); 3980af62f4dSVirupax Sadashivpetimath if (err < 0) 3990af62f4dSVirupax Sadashivpetimath return err; 4000af62f4dSVirupax Sadashivpetimath 4010af62f4dSVirupax Sadashivpetimath /* Wait for reset by the PorRtc */ 402012e52e1SLinus Walleij usleep_range(1000, 5000); 4030af62f4dSVirupax Sadashivpetimath 40447c16975SMattias Wallin err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC, 40547c16975SMattias Wallin AB8500_RTC_STAT_REG, &rtc_ctrl); 40647c16975SMattias Wallin if (err < 0) 40747c16975SMattias Wallin return err; 4080af62f4dSVirupax Sadashivpetimath 4090af62f4dSVirupax Sadashivpetimath /* Check if the RTC Supply fails */ 4100af62f4dSVirupax Sadashivpetimath if (!(rtc_ctrl & RTC_STATUS_DATA)) { 4110af62f4dSVirupax Sadashivpetimath dev_err(&pdev->dev, "RTC supply failure\n"); 4120af62f4dSVirupax Sadashivpetimath return -ENODEV; 4130af62f4dSVirupax Sadashivpetimath } 4140af62f4dSVirupax Sadashivpetimath 415b62581e6SAndrew Lynn device_init_wakeup(&pdev->dev, true); 416b62581e6SAndrew Lynn 4170af62f4dSVirupax Sadashivpetimath rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops, 4180af62f4dSVirupax Sadashivpetimath THIS_MODULE); 4190af62f4dSVirupax Sadashivpetimath if (IS_ERR(rtc)) { 4200af62f4dSVirupax Sadashivpetimath dev_err(&pdev->dev, "Registration failed\n"); 4210af62f4dSVirupax Sadashivpetimath err = PTR_ERR(rtc); 4220af62f4dSVirupax Sadashivpetimath return err; 4230af62f4dSVirupax Sadashivpetimath } 4240af62f4dSVirupax Sadashivpetimath 42510d065e6SRobert Marklund err = request_threaded_irq(irq, NULL, rtc_alarm_handler, 4263cfd16a5SLee Jones IRQF_NO_SUSPEND | IRQF_ONESHOT, "ab8500-rtc", rtc); 4270af62f4dSVirupax Sadashivpetimath if (err < 0) { 4280af62f4dSVirupax Sadashivpetimath rtc_device_unregister(rtc); 4290af62f4dSVirupax Sadashivpetimath return err; 4300af62f4dSVirupax Sadashivpetimath } 4310af62f4dSVirupax Sadashivpetimath 4320af62f4dSVirupax Sadashivpetimath platform_set_drvdata(pdev, rtc); 4330af62f4dSVirupax Sadashivpetimath 434dda367acSMark Godfrey err = ab8500_sysfs_rtc_register(&pdev->dev); 435dda367acSMark Godfrey if (err) { 436dda367acSMark Godfrey dev_err(&pdev->dev, "sysfs RTC failed to register\n"); 437dda367acSMark Godfrey return err; 438dda367acSMark Godfrey } 439dda367acSMark Godfrey 4400af62f4dSVirupax Sadashivpetimath return 0; 4410af62f4dSVirupax Sadashivpetimath } 4420af62f4dSVirupax Sadashivpetimath 4430af62f4dSVirupax Sadashivpetimath static int __devexit ab8500_rtc_remove(struct platform_device *pdev) 4440af62f4dSVirupax Sadashivpetimath { 4450af62f4dSVirupax Sadashivpetimath struct rtc_device *rtc = platform_get_drvdata(pdev); 4460af62f4dSVirupax Sadashivpetimath int irq = platform_get_irq_byname(pdev, "ALARM"); 4470af62f4dSVirupax Sadashivpetimath 448dda367acSMark Godfrey ab8500_sysfs_rtc_unregister(&pdev->dev); 449dda367acSMark Godfrey 4500af62f4dSVirupax Sadashivpetimath free_irq(irq, rtc); 4510af62f4dSVirupax Sadashivpetimath rtc_device_unregister(rtc); 4520af62f4dSVirupax Sadashivpetimath platform_set_drvdata(pdev, NULL); 4530af62f4dSVirupax Sadashivpetimath 4540af62f4dSVirupax Sadashivpetimath return 0; 4550af62f4dSVirupax Sadashivpetimath } 4560af62f4dSVirupax Sadashivpetimath 457*ad49fcbeSLee Jones static const struct of_device_id ab8500_rtc_match[] = { 458*ad49fcbeSLee Jones { .compatible = "stericsson,ab8500-rtc", }, 459*ad49fcbeSLee Jones {} 460*ad49fcbeSLee Jones }; 461*ad49fcbeSLee Jones 4620af62f4dSVirupax Sadashivpetimath static struct platform_driver ab8500_rtc_driver = { 4630af62f4dSVirupax Sadashivpetimath .driver = { 4640af62f4dSVirupax Sadashivpetimath .name = "ab8500-rtc", 4650af62f4dSVirupax Sadashivpetimath .owner = THIS_MODULE, 466*ad49fcbeSLee Jones .of_match_table = ab8500_rtc_match, 4670af62f4dSVirupax Sadashivpetimath }, 4680af62f4dSVirupax Sadashivpetimath .probe = ab8500_rtc_probe, 4690af62f4dSVirupax Sadashivpetimath .remove = __devexit_p(ab8500_rtc_remove), 4700af62f4dSVirupax Sadashivpetimath }; 4710af62f4dSVirupax Sadashivpetimath 4720c4eae66SAxel Lin module_platform_driver(ab8500_rtc_driver); 4730af62f4dSVirupax Sadashivpetimath 4740af62f4dSVirupax Sadashivpetimath MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>"); 4750af62f4dSVirupax Sadashivpetimath MODULE_DESCRIPTION("AB8500 RTC Driver"); 4760af62f4dSVirupax Sadashivpetimath MODULE_LICENSE("GPL v2"); 477