1 /* 2 * Copyright (C) ST-Ericsson SA 2010 3 * 4 * License terms: GNU General Public License (GPL) version 2 5 * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com> 6 * 7 * RTC clock driver for the RTC part of the AB8500 Power management chip. 8 * Based on RTC clock driver for the AB3100 Analog Baseband Chip by 9 * Linus Walleij <linus.walleij@stericsson.com> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/init.h> 15 #include <linux/platform_device.h> 16 #include <linux/rtc.h> 17 #include <linux/mfd/ab8500.h> 18 #include <linux/delay.h> 19 20 #define AB8500_RTC_SOFF_STAT_REG 0x0F00 21 #define AB8500_RTC_CC_CONF_REG 0x0F01 22 #define AB8500_RTC_READ_REQ_REG 0x0F02 23 #define AB8500_RTC_WATCH_TSECMID_REG 0x0F03 24 #define AB8500_RTC_WATCH_TSECHI_REG 0x0F04 25 #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x0F05 26 #define AB8500_RTC_WATCH_TMIN_MID_REG 0x0F06 27 #define AB8500_RTC_WATCH_TMIN_HI_REG 0x0F07 28 #define AB8500_RTC_ALRM_MIN_LOW_REG 0x0F08 29 #define AB8500_RTC_ALRM_MIN_MID_REG 0x0F09 30 #define AB8500_RTC_ALRM_MIN_HI_REG 0x0F0A 31 #define AB8500_RTC_STAT_REG 0x0F0B 32 #define AB8500_RTC_BKUP_CHG_REG 0x0F0C 33 #define AB8500_RTC_FORCE_BKUP_REG 0x0F0D 34 #define AB8500_RTC_CALIB_REG 0x0F0E 35 #define AB8500_RTC_SWITCH_STAT_REG 0x0F0F 36 #define AB8500_REV_REG 0x1080 37 38 /* RtcReadRequest bits */ 39 #define RTC_READ_REQUEST 0x01 40 #define RTC_WRITE_REQUEST 0x02 41 42 /* RtcCtrl bits */ 43 #define RTC_ALARM_ENA 0x04 44 #define RTC_STATUS_DATA 0x01 45 46 #define COUNTS_PER_SEC (0xF000 / 60) 47 #define AB8500_RTC_EPOCH 2000 48 49 static const unsigned long ab8500_rtc_time_regs[] = { 50 AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG, 51 AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG, 52 AB8500_RTC_WATCH_TSECMID_REG 53 }; 54 55 static const unsigned long ab8500_rtc_alarm_regs[] = { 56 AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG, 57 AB8500_RTC_ALRM_MIN_LOW_REG 58 }; 59 60 /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */ 61 static unsigned long get_elapsed_seconds(int year) 62 { 63 unsigned long secs; 64 struct rtc_time tm = { 65 .tm_year = year - 1900, 66 .tm_mday = 1, 67 }; 68 69 /* 70 * This function calculates secs from 1970 and not from 71 * 1900, even if we supply the offset from year 1900. 72 */ 73 rtc_tm_to_time(&tm, &secs); 74 return secs; 75 } 76 77 static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm) 78 { 79 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 80 unsigned long timeout = jiffies + HZ; 81 int retval, i; 82 unsigned long mins, secs; 83 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 84 85 /* Request a data read */ 86 retval = ab8500_write(ab8500, AB8500_RTC_READ_REQ_REG, 87 RTC_READ_REQUEST); 88 if (retval < 0) 89 return retval; 90 91 /* Early AB8500 chips will not clear the rtc read request bit */ 92 if (ab8500->revision == 0) { 93 msleep(1); 94 } else { 95 /* Wait for some cycles after enabling the rtc read in ab8500 */ 96 while (time_before(jiffies, timeout)) { 97 retval = ab8500_read(ab8500, AB8500_RTC_READ_REQ_REG); 98 if (retval < 0) 99 return retval; 100 101 if (!(retval & RTC_READ_REQUEST)) 102 break; 103 104 msleep(1); 105 } 106 } 107 108 /* Read the Watchtime registers */ 109 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 110 retval = ab8500_read(ab8500, ab8500_rtc_time_regs[i]); 111 if (retval < 0) 112 return retval; 113 buf[i] = retval; 114 } 115 116 mins = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 117 118 secs = (buf[3] << 8) | buf[4]; 119 secs = secs / COUNTS_PER_SEC; 120 secs = secs + (mins * 60); 121 122 /* Add back the initially subtracted number of seconds */ 123 secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 124 125 rtc_time_to_tm(secs, tm); 126 return rtc_valid_tm(tm); 127 } 128 129 static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm) 130 { 131 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 132 int retval, i; 133 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 134 unsigned long no_secs, no_mins, secs = 0; 135 136 if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) { 137 dev_dbg(dev, "year should be equal to or greater than %d\n", 138 AB8500_RTC_EPOCH); 139 return -EINVAL; 140 } 141 142 /* Get the number of seconds since 1970 */ 143 rtc_tm_to_time(tm, &secs); 144 145 /* 146 * Convert it to the number of seconds since 01-01-2000 00:00:00, since 147 * we only have a small counter in the RTC. 148 */ 149 secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 150 151 no_mins = secs / 60; 152 153 no_secs = secs % 60; 154 /* Make the seconds count as per the RTC resolution */ 155 no_secs = no_secs * COUNTS_PER_SEC; 156 157 buf[4] = no_secs & 0xFF; 158 buf[3] = (no_secs >> 8) & 0xFF; 159 160 buf[2] = no_mins & 0xFF; 161 buf[1] = (no_mins >> 8) & 0xFF; 162 buf[0] = (no_mins >> 16) & 0xFF; 163 164 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 165 retval = ab8500_write(ab8500, ab8500_rtc_time_regs[i], buf[i]); 166 if (retval < 0) 167 return retval; 168 } 169 170 /* Request a data write */ 171 return ab8500_write(ab8500, AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST); 172 } 173 174 static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 175 { 176 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 177 int retval, i; 178 int rtc_ctrl; 179 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 180 unsigned long secs, mins; 181 182 /* Check if the alarm is enabled or not */ 183 rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG); 184 if (rtc_ctrl < 0) 185 return rtc_ctrl; 186 187 if (rtc_ctrl & RTC_ALARM_ENA) 188 alarm->enabled = 1; 189 else 190 alarm->enabled = 0; 191 192 alarm->pending = 0; 193 194 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 195 retval = ab8500_read(ab8500, ab8500_rtc_alarm_regs[i]); 196 if (retval < 0) 197 return retval; 198 buf[i] = retval; 199 } 200 201 mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]); 202 secs = mins * 60; 203 204 /* Add back the initially subtracted number of seconds */ 205 secs += get_elapsed_seconds(AB8500_RTC_EPOCH); 206 207 rtc_time_to_tm(secs, &alarm->time); 208 209 return rtc_valid_tm(&alarm->time); 210 } 211 212 static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled) 213 { 214 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 215 216 return ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_ALARM_ENA, 217 enabled ? RTC_ALARM_ENA : 0); 218 } 219 220 static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 221 { 222 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 223 int retval, i; 224 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 225 unsigned long mins, secs = 0; 226 227 if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) { 228 dev_dbg(dev, "year should be equal to or greater than %d\n", 229 AB8500_RTC_EPOCH); 230 return -EINVAL; 231 } 232 233 /* Get the number of seconds since 1970 */ 234 rtc_tm_to_time(&alarm->time, &secs); 235 236 /* 237 * Convert it to the number of seconds since 01-01-2000 00:00:00, since 238 * we only have a small counter in the RTC. 239 */ 240 secs -= get_elapsed_seconds(AB8500_RTC_EPOCH); 241 242 mins = secs / 60; 243 244 buf[2] = mins & 0xFF; 245 buf[1] = (mins >> 8) & 0xFF; 246 buf[0] = (mins >> 16) & 0xFF; 247 248 /* Set the alarm time */ 249 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 250 retval = ab8500_write(ab8500, ab8500_rtc_alarm_regs[i], buf[i]); 251 if (retval < 0) 252 return retval; 253 } 254 255 return ab8500_rtc_irq_enable(dev, alarm->enabled); 256 } 257 258 static irqreturn_t rtc_alarm_handler(int irq, void *data) 259 { 260 struct rtc_device *rtc = data; 261 unsigned long events = RTC_IRQF | RTC_AF; 262 263 dev_dbg(&rtc->dev, "%s\n", __func__); 264 rtc_update_irq(rtc, 1, events); 265 266 return IRQ_HANDLED; 267 } 268 269 static const struct rtc_class_ops ab8500_rtc_ops = { 270 .read_time = ab8500_rtc_read_time, 271 .set_time = ab8500_rtc_set_time, 272 .read_alarm = ab8500_rtc_read_alarm, 273 .set_alarm = ab8500_rtc_set_alarm, 274 .alarm_irq_enable = ab8500_rtc_irq_enable, 275 }; 276 277 static int __devinit ab8500_rtc_probe(struct platform_device *pdev) 278 { 279 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent); 280 int err; 281 struct rtc_device *rtc; 282 int rtc_ctrl; 283 int irq; 284 285 irq = platform_get_irq_byname(pdev, "ALARM"); 286 if (irq < 0) 287 return irq; 288 289 /* For RTC supply test */ 290 err = ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_STATUS_DATA, 291 RTC_STATUS_DATA); 292 if (err < 0) 293 return err; 294 295 /* Wait for reset by the PorRtc */ 296 msleep(1); 297 298 rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG); 299 if (rtc_ctrl < 0) 300 return rtc_ctrl; 301 302 /* Check if the RTC Supply fails */ 303 if (!(rtc_ctrl & RTC_STATUS_DATA)) { 304 dev_err(&pdev->dev, "RTC supply failure\n"); 305 return -ENODEV; 306 } 307 308 rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops, 309 THIS_MODULE); 310 if (IS_ERR(rtc)) { 311 dev_err(&pdev->dev, "Registration failed\n"); 312 err = PTR_ERR(rtc); 313 return err; 314 } 315 316 err = request_threaded_irq(irq, NULL, rtc_alarm_handler, 0, 317 "ab8500-rtc", rtc); 318 if (err < 0) { 319 rtc_device_unregister(rtc); 320 return err; 321 } 322 323 platform_set_drvdata(pdev, rtc); 324 325 return 0; 326 } 327 328 static int __devexit ab8500_rtc_remove(struct platform_device *pdev) 329 { 330 struct rtc_device *rtc = platform_get_drvdata(pdev); 331 int irq = platform_get_irq_byname(pdev, "ALARM"); 332 333 free_irq(irq, rtc); 334 rtc_device_unregister(rtc); 335 platform_set_drvdata(pdev, NULL); 336 337 return 0; 338 } 339 340 static struct platform_driver ab8500_rtc_driver = { 341 .driver = { 342 .name = "ab8500-rtc", 343 .owner = THIS_MODULE, 344 }, 345 .probe = ab8500_rtc_probe, 346 .remove = __devexit_p(ab8500_rtc_remove), 347 }; 348 349 static int __init ab8500_rtc_init(void) 350 { 351 return platform_driver_register(&ab8500_rtc_driver); 352 } 353 354 static void __exit ab8500_rtc_exit(void) 355 { 356 platform_driver_unregister(&ab8500_rtc_driver); 357 } 358 359 module_init(ab8500_rtc_init); 360 module_exit(ab8500_rtc_exit); 361 MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>"); 362 MODULE_DESCRIPTION("AB8500 RTC Driver"); 363 MODULE_LICENSE("GPL v2"); 364