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> 180af62f4dSVirupax Sadashivpetimath #include <linux/mfd/ab8500.h> 190af62f4dSVirupax Sadashivpetimath #include <linux/delay.h> 200af62f4dSVirupax Sadashivpetimath 2147c16975SMattias Wallin #define AB8500_RTC_SOFF_STAT_REG 0x00 2247c16975SMattias Wallin #define AB8500_RTC_CC_CONF_REG 0x01 2347c16975SMattias Wallin #define AB8500_RTC_READ_REQ_REG 0x02 2447c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECMID_REG 0x03 2547c16975SMattias Wallin #define AB8500_RTC_WATCH_TSECHI_REG 0x04 2647c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05 2747c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_MID_REG 0x06 2847c16975SMattias Wallin #define AB8500_RTC_WATCH_TMIN_HI_REG 0x07 2947c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08 3047c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_MID_REG 0x09 3147c16975SMattias Wallin #define AB8500_RTC_ALRM_MIN_HI_REG 0x0A 3247c16975SMattias Wallin #define AB8500_RTC_STAT_REG 0x0B 3347c16975SMattias Wallin #define AB8500_RTC_BKUP_CHG_REG 0x0C 3447c16975SMattias Wallin #define AB8500_RTC_FORCE_BKUP_REG 0x0D 3547c16975SMattias Wallin #define AB8500_RTC_CALIB_REG 0x0E 3647c16975SMattias Wallin #define AB8500_RTC_SWITCH_STAT_REG 0x0F 370af62f4dSVirupax Sadashivpetimath 380af62f4dSVirupax Sadashivpetimath /* RtcReadRequest bits */ 390af62f4dSVirupax Sadashivpetimath #define RTC_READ_REQUEST 0x01 400af62f4dSVirupax Sadashivpetimath #define RTC_WRITE_REQUEST 0x02 410af62f4dSVirupax Sadashivpetimath 420af62f4dSVirupax Sadashivpetimath /* RtcCtrl bits */ 430af62f4dSVirupax Sadashivpetimath #define RTC_ALARM_ENA 0x04 440af62f4dSVirupax Sadashivpetimath #define RTC_STATUS_DATA 0x01 450af62f4dSVirupax Sadashivpetimath 460af62f4dSVirupax Sadashivpetimath #define COUNTS_PER_SEC (0xF000 / 60) 470af62f4dSVirupax Sadashivpetimath #define AB8500_RTC_EPOCH 2000 480af62f4dSVirupax Sadashivpetimath 4947c16975SMattias Wallin static const u8 ab8500_rtc_time_regs[] = { 500af62f4dSVirupax Sadashivpetimath AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG, 510af62f4dSVirupax Sadashivpetimath AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG, 520af62f4dSVirupax Sadashivpetimath AB8500_RTC_WATCH_TSECMID_REG 530af62f4dSVirupax Sadashivpetimath }; 540af62f4dSVirupax Sadashivpetimath 5547c16975SMattias Wallin static const u8 ab8500_rtc_alarm_regs[] = { 560af62f4dSVirupax Sadashivpetimath AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG, 570af62f4dSVirupax Sadashivpetimath AB8500_RTC_ALRM_MIN_LOW_REG 580af62f4dSVirupax Sadashivpetimath }; 590af62f4dSVirupax Sadashivpetimath 600af62f4dSVirupax Sadashivpetimath /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */ 610af62f4dSVirupax Sadashivpetimath static unsigned long get_elapsed_seconds(int year) 620af62f4dSVirupax Sadashivpetimath { 630af62f4dSVirupax Sadashivpetimath unsigned long secs; 640af62f4dSVirupax Sadashivpetimath struct rtc_time tm = { 650af62f4dSVirupax Sadashivpetimath .tm_year = year - 1900, 660af62f4dSVirupax Sadashivpetimath .tm_mday = 1, 670af62f4dSVirupax Sadashivpetimath }; 680af62f4dSVirupax Sadashivpetimath 690af62f4dSVirupax Sadashivpetimath /* 700af62f4dSVirupax Sadashivpetimath * This function calculates secs from 1970 and not from 710af62f4dSVirupax Sadashivpetimath * 1900, even if we supply the offset from year 1900. 720af62f4dSVirupax Sadashivpetimath */ 730af62f4dSVirupax Sadashivpetimath rtc_tm_to_time(&tm, &secs); 740af62f4dSVirupax Sadashivpetimath return secs; 750af62f4dSVirupax Sadashivpetimath } 760af62f4dSVirupax Sadashivpetimath 770af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm) 780af62f4dSVirupax Sadashivpetimath { 790af62f4dSVirupax Sadashivpetimath unsigned long timeout = jiffies + HZ; 800af62f4dSVirupax Sadashivpetimath int retval, i; 810af62f4dSVirupax Sadashivpetimath unsigned long mins, secs; 820af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 8347c16975SMattias Wallin u8 value; 840af62f4dSVirupax Sadashivpetimath 850af62f4dSVirupax Sadashivpetimath /* Request a data read */ 8647c16975SMattias Wallin retval = abx500_set_register_interruptible(dev, 8747c16975SMattias Wallin AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST); 880af62f4dSVirupax Sadashivpetimath if (retval < 0) 890af62f4dSVirupax Sadashivpetimath return retval; 900af62f4dSVirupax Sadashivpetimath 910af62f4dSVirupax Sadashivpetimath /* Early AB8500 chips will not clear the rtc read request bit */ 9247c16975SMattias Wallin if (abx500_get_chip_id(dev) == 0) { 93012e52e1SLinus Walleij usleep_range(1000, 1000); 940af62f4dSVirupax Sadashivpetimath } else { 950af62f4dSVirupax Sadashivpetimath /* Wait for some cycles after enabling the rtc read in ab8500 */ 960af62f4dSVirupax Sadashivpetimath while (time_before(jiffies, timeout)) { 9747c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, 9847c16975SMattias Wallin AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value); 990af62f4dSVirupax Sadashivpetimath if (retval < 0) 1000af62f4dSVirupax Sadashivpetimath return retval; 1010af62f4dSVirupax Sadashivpetimath 10247c16975SMattias Wallin if (!(value & RTC_READ_REQUEST)) 1030af62f4dSVirupax Sadashivpetimath break; 1040af62f4dSVirupax Sadashivpetimath 105012e52e1SLinus Walleij usleep_range(1000, 5000); 1060af62f4dSVirupax Sadashivpetimath } 1070af62f4dSVirupax Sadashivpetimath } 1080af62f4dSVirupax Sadashivpetimath 1090af62f4dSVirupax Sadashivpetimath /* Read the Watchtime registers */ 1100af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 11147c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, 11247c16975SMattias Wallin AB8500_RTC, ab8500_rtc_time_regs[i], &value); 1130af62f4dSVirupax Sadashivpetimath if (retval < 0) 1140af62f4dSVirupax Sadashivpetimath return retval; 11547c16975SMattias Wallin buf[i] = value; 1160af62f4dSVirupax Sadashivpetimath } 1170af62f4dSVirupax Sadashivpetimath 1180af62f4dSVirupax Sadashivpetimath mins = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 1190af62f4dSVirupax Sadashivpetimath 1200af62f4dSVirupax Sadashivpetimath secs = (buf[3] << 8) | buf[4]; 1210af62f4dSVirupax Sadashivpetimath secs = secs / COUNTS_PER_SEC; 1220af62f4dSVirupax Sadashivpetimath secs = secs + (mins * 60); 1230af62f4dSVirupax Sadashivpetimath 1240af62f4dSVirupax Sadashivpetimath /* Add back the initially subtracted number of seconds */ 1250af62f4dSVirupax Sadashivpetimath secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 1260af62f4dSVirupax Sadashivpetimath 1270af62f4dSVirupax Sadashivpetimath rtc_time_to_tm(secs, tm); 1280af62f4dSVirupax Sadashivpetimath return rtc_valid_tm(tm); 1290af62f4dSVirupax Sadashivpetimath } 1300af62f4dSVirupax Sadashivpetimath 1310af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm) 1320af62f4dSVirupax Sadashivpetimath { 1330af62f4dSVirupax Sadashivpetimath int retval, i; 1340af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 1350af62f4dSVirupax Sadashivpetimath unsigned long no_secs, no_mins, secs = 0; 1360af62f4dSVirupax Sadashivpetimath 1370af62f4dSVirupax Sadashivpetimath if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) { 1380af62f4dSVirupax Sadashivpetimath dev_dbg(dev, "year should be equal to or greater than %d\n", 1390af62f4dSVirupax Sadashivpetimath AB8500_RTC_EPOCH); 1400af62f4dSVirupax Sadashivpetimath return -EINVAL; 1410af62f4dSVirupax Sadashivpetimath } 1420af62f4dSVirupax Sadashivpetimath 1430af62f4dSVirupax Sadashivpetimath /* Get the number of seconds since 1970 */ 1440af62f4dSVirupax Sadashivpetimath rtc_tm_to_time(tm, &secs); 1450af62f4dSVirupax Sadashivpetimath 1460af62f4dSVirupax Sadashivpetimath /* 1470af62f4dSVirupax Sadashivpetimath * Convert it to the number of seconds since 01-01-2000 00:00:00, since 1480af62f4dSVirupax Sadashivpetimath * we only have a small counter in the RTC. 1490af62f4dSVirupax Sadashivpetimath */ 1500af62f4dSVirupax Sadashivpetimath secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 1510af62f4dSVirupax Sadashivpetimath 1520af62f4dSVirupax Sadashivpetimath no_mins = secs / 60; 1530af62f4dSVirupax Sadashivpetimath 1540af62f4dSVirupax Sadashivpetimath no_secs = secs % 60; 1550af62f4dSVirupax Sadashivpetimath /* Make the seconds count as per the RTC resolution */ 1560af62f4dSVirupax Sadashivpetimath no_secs = no_secs * COUNTS_PER_SEC; 1570af62f4dSVirupax Sadashivpetimath 1580af62f4dSVirupax Sadashivpetimath buf[4] = no_secs & 0xFF; 1590af62f4dSVirupax Sadashivpetimath buf[3] = (no_secs >> 8) & 0xFF; 1600af62f4dSVirupax Sadashivpetimath 1610af62f4dSVirupax Sadashivpetimath buf[2] = no_mins & 0xFF; 1620af62f4dSVirupax Sadashivpetimath buf[1] = (no_mins >> 8) & 0xFF; 1630af62f4dSVirupax Sadashivpetimath buf[0] = (no_mins >> 16) & 0xFF; 1640af62f4dSVirupax Sadashivpetimath 1650af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 16647c16975SMattias Wallin retval = abx500_set_register_interruptible(dev, AB8500_RTC, 16747c16975SMattias Wallin ab8500_rtc_time_regs[i], buf[i]); 1680af62f4dSVirupax Sadashivpetimath if (retval < 0) 1690af62f4dSVirupax Sadashivpetimath return retval; 1700af62f4dSVirupax Sadashivpetimath } 1710af62f4dSVirupax Sadashivpetimath 1720af62f4dSVirupax Sadashivpetimath /* Request a data write */ 17347c16975SMattias Wallin return abx500_set_register_interruptible(dev, AB8500_RTC, 17447c16975SMattias Wallin AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST); 1750af62f4dSVirupax Sadashivpetimath } 1760af62f4dSVirupax Sadashivpetimath 1770af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 1780af62f4dSVirupax Sadashivpetimath { 1790af62f4dSVirupax Sadashivpetimath int retval, i; 18047c16975SMattias Wallin u8 rtc_ctrl, value; 1810af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 1820af62f4dSVirupax Sadashivpetimath unsigned long secs, mins; 1830af62f4dSVirupax Sadashivpetimath 1840af62f4dSVirupax Sadashivpetimath /* Check if the alarm is enabled or not */ 18547c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, AB8500_RTC, 18647c16975SMattias Wallin AB8500_RTC_STAT_REG, &rtc_ctrl); 18747c16975SMattias Wallin if (retval < 0) 18847c16975SMattias Wallin return retval; 1890af62f4dSVirupax Sadashivpetimath 1900af62f4dSVirupax Sadashivpetimath if (rtc_ctrl & RTC_ALARM_ENA) 1910af62f4dSVirupax Sadashivpetimath alarm->enabled = 1; 1920af62f4dSVirupax Sadashivpetimath else 1930af62f4dSVirupax Sadashivpetimath alarm->enabled = 0; 1940af62f4dSVirupax Sadashivpetimath 1950af62f4dSVirupax Sadashivpetimath alarm->pending = 0; 1960af62f4dSVirupax Sadashivpetimath 1970af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 19847c16975SMattias Wallin retval = abx500_get_register_interruptible(dev, AB8500_RTC, 19947c16975SMattias Wallin ab8500_rtc_alarm_regs[i], &value); 2000af62f4dSVirupax Sadashivpetimath if (retval < 0) 2010af62f4dSVirupax Sadashivpetimath return retval; 20247c16975SMattias Wallin buf[i] = value; 2030af62f4dSVirupax Sadashivpetimath } 2040af62f4dSVirupax Sadashivpetimath 2050af62f4dSVirupax Sadashivpetimath mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]); 2060af62f4dSVirupax Sadashivpetimath secs = mins * 60; 2070af62f4dSVirupax Sadashivpetimath 2080af62f4dSVirupax Sadashivpetimath /* Add back the initially subtracted number of seconds */ 2090af62f4dSVirupax Sadashivpetimath secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 2100af62f4dSVirupax Sadashivpetimath 2110af62f4dSVirupax Sadashivpetimath rtc_time_to_tm(secs, &alarm->time); 2120af62f4dSVirupax Sadashivpetimath 2130af62f4dSVirupax Sadashivpetimath return rtc_valid_tm(&alarm->time); 2140af62f4dSVirupax Sadashivpetimath } 2150af62f4dSVirupax Sadashivpetimath 2160af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled) 2170af62f4dSVirupax Sadashivpetimath { 21847c16975SMattias Wallin return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC, 21947c16975SMattias Wallin AB8500_RTC_STAT_REG, RTC_ALARM_ENA, 2200af62f4dSVirupax Sadashivpetimath enabled ? RTC_ALARM_ENA : 0); 2210af62f4dSVirupax Sadashivpetimath } 2220af62f4dSVirupax Sadashivpetimath 2230af62f4dSVirupax Sadashivpetimath static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 2240af62f4dSVirupax Sadashivpetimath { 2250af62f4dSVirupax Sadashivpetimath int retval, i; 2260af62f4dSVirupax Sadashivpetimath unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 2270af62f4dSVirupax Sadashivpetimath unsigned long mins, secs = 0; 2280af62f4dSVirupax Sadashivpetimath 2290af62f4dSVirupax Sadashivpetimath if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) { 2300af62f4dSVirupax Sadashivpetimath dev_dbg(dev, "year should be equal to or greater than %d\n", 2310af62f4dSVirupax Sadashivpetimath AB8500_RTC_EPOCH); 2320af62f4dSVirupax Sadashivpetimath return -EINVAL; 2330af62f4dSVirupax Sadashivpetimath } 2340af62f4dSVirupax Sadashivpetimath 2350af62f4dSVirupax Sadashivpetimath /* Get the number of seconds since 1970 */ 2360af62f4dSVirupax Sadashivpetimath rtc_tm_to_time(&alarm->time, &secs); 2370af62f4dSVirupax Sadashivpetimath 2380af62f4dSVirupax Sadashivpetimath /* 2390af62f4dSVirupax Sadashivpetimath * Convert it to the number of seconds since 01-01-2000 00:00:00, since 2400af62f4dSVirupax Sadashivpetimath * we only have a small counter in the RTC. 2410af62f4dSVirupax Sadashivpetimath */ 2420af62f4dSVirupax Sadashivpetimath secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 2430af62f4dSVirupax Sadashivpetimath 2440af62f4dSVirupax Sadashivpetimath mins = secs / 60; 2450af62f4dSVirupax Sadashivpetimath 2460af62f4dSVirupax Sadashivpetimath buf[2] = mins & 0xFF; 2470af62f4dSVirupax Sadashivpetimath buf[1] = (mins >> 8) & 0xFF; 2480af62f4dSVirupax Sadashivpetimath buf[0] = (mins >> 16) & 0xFF; 2490af62f4dSVirupax Sadashivpetimath 2500af62f4dSVirupax Sadashivpetimath /* Set the alarm time */ 2510af62f4dSVirupax Sadashivpetimath for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 25247c16975SMattias Wallin retval = abx500_set_register_interruptible(dev, AB8500_RTC, 25347c16975SMattias Wallin ab8500_rtc_alarm_regs[i], buf[i]); 2540af62f4dSVirupax Sadashivpetimath if (retval < 0) 2550af62f4dSVirupax Sadashivpetimath return retval; 2560af62f4dSVirupax Sadashivpetimath } 2570af62f4dSVirupax Sadashivpetimath 2580af62f4dSVirupax Sadashivpetimath return ab8500_rtc_irq_enable(dev, alarm->enabled); 2590af62f4dSVirupax Sadashivpetimath } 2600af62f4dSVirupax Sadashivpetimath 261dda367acSMark Godfrey 262dda367acSMark Godfrey static int ab8500_rtc_set_calibration(struct device *dev, int calibration) 263dda367acSMark Godfrey { 264dda367acSMark Godfrey int retval; 265dda367acSMark Godfrey u8 rtccal = 0; 266dda367acSMark Godfrey 267dda367acSMark Godfrey /* 268dda367acSMark Godfrey * Check that the calibration value (which is in units of 0.5 269dda367acSMark Godfrey * parts-per-million) is in the AB8500's range for RtcCalibration 270dda367acSMark Godfrey * register. -128 (0x80) is not permitted because the AB8500 uses 271dda367acSMark Godfrey * a sign-bit rather than two's complement, so 0x80 is just another 272dda367acSMark Godfrey * representation of zero. 273dda367acSMark Godfrey */ 274dda367acSMark Godfrey if ((calibration < -127) || (calibration > 127)) { 275dda367acSMark Godfrey dev_err(dev, "RtcCalibration value outside permitted range\n"); 276dda367acSMark Godfrey return -EINVAL; 277dda367acSMark Godfrey } 278dda367acSMark Godfrey 279dda367acSMark Godfrey /* 280dda367acSMark Godfrey * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) 281dda367acSMark Godfrey * so need to convert to this sort of representation before writing 282dda367acSMark Godfrey * into RtcCalibration register... 283dda367acSMark Godfrey */ 284dda367acSMark Godfrey if (calibration >= 0) 285dda367acSMark Godfrey rtccal = 0x7F & calibration; 286dda367acSMark Godfrey else 287dda367acSMark Godfrey rtccal = ~(calibration - 1) | 0x80; 288dda367acSMark Godfrey 289dda367acSMark Godfrey retval = abx500_set_register_interruptible(dev, AB8500_RTC, 290dda367acSMark Godfrey AB8500_RTC_CALIB_REG, rtccal); 291dda367acSMark Godfrey 292dda367acSMark Godfrey return retval; 293dda367acSMark Godfrey } 294dda367acSMark Godfrey 295dda367acSMark Godfrey static int ab8500_rtc_get_calibration(struct device *dev, int *calibration) 296dda367acSMark Godfrey { 297dda367acSMark Godfrey int retval; 298dda367acSMark Godfrey u8 rtccal = 0; 299dda367acSMark Godfrey 300dda367acSMark Godfrey retval = abx500_get_register_interruptible(dev, AB8500_RTC, 301dda367acSMark Godfrey AB8500_RTC_CALIB_REG, &rtccal); 302dda367acSMark Godfrey if (retval >= 0) { 303dda367acSMark Godfrey /* 304dda367acSMark Godfrey * The AB8500 uses sign (in bit7) and magnitude (in bits0-7) 305dda367acSMark Godfrey * so need to convert value from RtcCalibration register into 306dda367acSMark Godfrey * a two's complement signed value... 307dda367acSMark Godfrey */ 308dda367acSMark Godfrey if (rtccal & 0x80) 309dda367acSMark Godfrey *calibration = 0 - (rtccal & 0x7F); 310dda367acSMark Godfrey else 311dda367acSMark Godfrey *calibration = 0x7F & rtccal; 312dda367acSMark Godfrey } 313dda367acSMark Godfrey 314dda367acSMark Godfrey return retval; 315dda367acSMark Godfrey } 316dda367acSMark Godfrey 317dda367acSMark Godfrey static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev, 318dda367acSMark Godfrey struct device_attribute *attr, 319dda367acSMark Godfrey const char *buf, size_t count) 320dda367acSMark Godfrey { 321dda367acSMark Godfrey int retval; 322dda367acSMark Godfrey int calibration = 0; 323dda367acSMark Godfrey 324dda367acSMark Godfrey if (sscanf(buf, " %i ", &calibration) != 1) { 325dda367acSMark Godfrey dev_err(dev, "Failed to store RTC calibration attribute\n"); 326dda367acSMark Godfrey return -EINVAL; 327dda367acSMark Godfrey } 328dda367acSMark Godfrey 329dda367acSMark Godfrey retval = ab8500_rtc_set_calibration(dev, calibration); 330dda367acSMark Godfrey 331dda367acSMark Godfrey return retval ? retval : count; 332dda367acSMark Godfrey } 333dda367acSMark Godfrey 334dda367acSMark Godfrey static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev, 335dda367acSMark Godfrey struct device_attribute *attr, char *buf) 336dda367acSMark Godfrey { 337dda367acSMark Godfrey int retval = 0; 338dda367acSMark Godfrey int calibration = 0; 339dda367acSMark Godfrey 340dda367acSMark Godfrey retval = ab8500_rtc_get_calibration(dev, &calibration); 341dda367acSMark Godfrey if (retval < 0) { 342dda367acSMark Godfrey dev_err(dev, "Failed to read RTC calibration attribute\n"); 343dda367acSMark Godfrey sprintf(buf, "0\n"); 344dda367acSMark Godfrey return retval; 345dda367acSMark Godfrey } 346dda367acSMark Godfrey 347dda367acSMark Godfrey return sprintf(buf, "%d\n", calibration); 348dda367acSMark Godfrey } 349dda367acSMark Godfrey 350dda367acSMark Godfrey static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR, 351dda367acSMark Godfrey ab8500_sysfs_show_rtc_calibration, 352dda367acSMark Godfrey ab8500_sysfs_store_rtc_calibration); 353dda367acSMark Godfrey 354dda367acSMark Godfrey static int ab8500_sysfs_rtc_register(struct device *dev) 355dda367acSMark Godfrey { 356dda367acSMark Godfrey return device_create_file(dev, &dev_attr_rtc_calibration); 357dda367acSMark Godfrey } 358dda367acSMark Godfrey 359dda367acSMark Godfrey static void ab8500_sysfs_rtc_unregister(struct device *dev) 360dda367acSMark Godfrey { 361dda367acSMark Godfrey device_remove_file(dev, &dev_attr_rtc_calibration); 362dda367acSMark Godfrey } 363dda367acSMark Godfrey 3640af62f4dSVirupax Sadashivpetimath static irqreturn_t rtc_alarm_handler(int irq, void *data) 3650af62f4dSVirupax Sadashivpetimath { 3660af62f4dSVirupax Sadashivpetimath struct rtc_device *rtc = data; 3670af62f4dSVirupax Sadashivpetimath unsigned long events = RTC_IRQF | RTC_AF; 3680af62f4dSVirupax Sadashivpetimath 3690af62f4dSVirupax Sadashivpetimath dev_dbg(&rtc->dev, "%s\n", __func__); 3700af62f4dSVirupax Sadashivpetimath rtc_update_irq(rtc, 1, events); 3710af62f4dSVirupax Sadashivpetimath 3720af62f4dSVirupax Sadashivpetimath return IRQ_HANDLED; 3730af62f4dSVirupax Sadashivpetimath } 3740af62f4dSVirupax Sadashivpetimath 3750af62f4dSVirupax Sadashivpetimath static const struct rtc_class_ops ab8500_rtc_ops = { 3760af62f4dSVirupax Sadashivpetimath .read_time = ab8500_rtc_read_time, 3770af62f4dSVirupax Sadashivpetimath .set_time = ab8500_rtc_set_time, 3780af62f4dSVirupax Sadashivpetimath .read_alarm = ab8500_rtc_read_alarm, 3790af62f4dSVirupax Sadashivpetimath .set_alarm = ab8500_rtc_set_alarm, 3800af62f4dSVirupax Sadashivpetimath .alarm_irq_enable = ab8500_rtc_irq_enable, 3810af62f4dSVirupax Sadashivpetimath }; 3820af62f4dSVirupax Sadashivpetimath 3830af62f4dSVirupax Sadashivpetimath static int __devinit ab8500_rtc_probe(struct platform_device *pdev) 3840af62f4dSVirupax Sadashivpetimath { 3850af62f4dSVirupax Sadashivpetimath int err; 3860af62f4dSVirupax Sadashivpetimath struct rtc_device *rtc; 38747c16975SMattias Wallin u8 rtc_ctrl; 3880af62f4dSVirupax Sadashivpetimath int irq; 3890af62f4dSVirupax Sadashivpetimath 3900af62f4dSVirupax Sadashivpetimath irq = platform_get_irq_byname(pdev, "ALARM"); 3910af62f4dSVirupax Sadashivpetimath if (irq < 0) 3920af62f4dSVirupax Sadashivpetimath return irq; 3930af62f4dSVirupax Sadashivpetimath 3940af62f4dSVirupax Sadashivpetimath /* For RTC supply test */ 39547c16975SMattias Wallin err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC, 39647c16975SMattias Wallin AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA); 3970af62f4dSVirupax Sadashivpetimath if (err < 0) 3980af62f4dSVirupax Sadashivpetimath return err; 3990af62f4dSVirupax Sadashivpetimath 4000af62f4dSVirupax Sadashivpetimath /* Wait for reset by the PorRtc */ 401012e52e1SLinus Walleij usleep_range(1000, 5000); 4020af62f4dSVirupax Sadashivpetimath 40347c16975SMattias Wallin err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC, 40447c16975SMattias Wallin AB8500_RTC_STAT_REG, &rtc_ctrl); 40547c16975SMattias Wallin if (err < 0) 40647c16975SMattias Wallin return err; 4070af62f4dSVirupax Sadashivpetimath 4080af62f4dSVirupax Sadashivpetimath /* Check if the RTC Supply fails */ 4090af62f4dSVirupax Sadashivpetimath if (!(rtc_ctrl & RTC_STATUS_DATA)) { 4100af62f4dSVirupax Sadashivpetimath dev_err(&pdev->dev, "RTC supply failure\n"); 4110af62f4dSVirupax Sadashivpetimath return -ENODEV; 4120af62f4dSVirupax Sadashivpetimath } 4130af62f4dSVirupax Sadashivpetimath 414b62581e6SAndrew Lynn device_init_wakeup(&pdev->dev, true); 415b62581e6SAndrew Lynn 4160af62f4dSVirupax Sadashivpetimath rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops, 4170af62f4dSVirupax Sadashivpetimath THIS_MODULE); 4180af62f4dSVirupax Sadashivpetimath if (IS_ERR(rtc)) { 4190af62f4dSVirupax Sadashivpetimath dev_err(&pdev->dev, "Registration failed\n"); 4200af62f4dSVirupax Sadashivpetimath err = PTR_ERR(rtc); 4210af62f4dSVirupax Sadashivpetimath return err; 4220af62f4dSVirupax Sadashivpetimath } 4230af62f4dSVirupax Sadashivpetimath 42410d065e6SRobert Marklund err = request_threaded_irq(irq, NULL, rtc_alarm_handler, 42510d065e6SRobert Marklund IRQF_NO_SUSPEND, "ab8500-rtc", rtc); 4260af62f4dSVirupax Sadashivpetimath if (err < 0) { 4270af62f4dSVirupax Sadashivpetimath rtc_device_unregister(rtc); 4280af62f4dSVirupax Sadashivpetimath return err; 4290af62f4dSVirupax Sadashivpetimath } 4300af62f4dSVirupax Sadashivpetimath 4310af62f4dSVirupax Sadashivpetimath platform_set_drvdata(pdev, rtc); 4320af62f4dSVirupax Sadashivpetimath 433dda367acSMark Godfrey 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 4570af62f4dSVirupax Sadashivpetimath static struct platform_driver ab8500_rtc_driver = { 4580af62f4dSVirupax Sadashivpetimath .driver = { 4590af62f4dSVirupax Sadashivpetimath .name = "ab8500-rtc", 4600af62f4dSVirupax Sadashivpetimath .owner = THIS_MODULE, 4610af62f4dSVirupax Sadashivpetimath }, 4620af62f4dSVirupax Sadashivpetimath .probe = ab8500_rtc_probe, 4630af62f4dSVirupax Sadashivpetimath .remove = __devexit_p(ab8500_rtc_remove), 4640af62f4dSVirupax Sadashivpetimath }; 4650af62f4dSVirupax Sadashivpetimath 466*0c4eae66SAxel Lin module_platform_driver(ab8500_rtc_driver); 4670af62f4dSVirupax Sadashivpetimath 4680af62f4dSVirupax Sadashivpetimath MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>"); 4690af62f4dSVirupax Sadashivpetimath MODULE_DESCRIPTION("AB8500 RTC Driver"); 4700af62f4dSVirupax Sadashivpetimath MODULE_LICENSE("GPL v2"); 471