10c86edc0SAlessandro Zummo /* 20c86edc0SAlessandro Zummo * RTC subsystem, interface functions 30c86edc0SAlessandro Zummo * 40c86edc0SAlessandro Zummo * Copyright (C) 2005 Tower Technologies 50c86edc0SAlessandro Zummo * Author: Alessandro Zummo <[email protected]> 60c86edc0SAlessandro Zummo * 70c86edc0SAlessandro Zummo * based on arch/arm/common/rtctime.c 80c86edc0SAlessandro Zummo * 90c86edc0SAlessandro Zummo * This program is free software; you can redistribute it and/or modify 100c86edc0SAlessandro Zummo * it under the terms of the GNU General Public License version 2 as 110c86edc0SAlessandro Zummo * published by the Free Software Foundation. 120c86edc0SAlessandro Zummo */ 130c86edc0SAlessandro Zummo 140c86edc0SAlessandro Zummo #include <linux/rtc.h> 15d43c36dcSAlexey Dobriyan #include <linux/sched.h> 162113852bSPaul Gortmaker #include <linux/module.h> 1797144c67SDavid Brownell #include <linux/log2.h> 186610e089SJohn Stultz #include <linux/workqueue.h> 196610e089SJohn Stultz 2029a1f599SBaolin Wang #define CREATE_TRACE_POINTS 2129a1f599SBaolin Wang #include <trace/events/rtc.h> 2229a1f599SBaolin Wang 23aa0be0f4SJohn Stultz static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer); 24aa0be0f4SJohn Stultz static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer); 25aa0be0f4SJohn Stultz 2698951564SBaolin Wang static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm) 2798951564SBaolin Wang { 2898951564SBaolin Wang time64_t secs; 2998951564SBaolin Wang 3098951564SBaolin Wang if (!rtc->offset_secs) 3198951564SBaolin Wang return; 3298951564SBaolin Wang 3398951564SBaolin Wang secs = rtc_tm_to_time64(tm); 3498951564SBaolin Wang 3598951564SBaolin Wang /* 3698951564SBaolin Wang * Since the reading time values from RTC device are always in the RTC 3798951564SBaolin Wang * original valid range, but we need to skip the overlapped region 3898951564SBaolin Wang * between expanded range and original range, which is no need to add 3998951564SBaolin Wang * the offset. 4098951564SBaolin Wang */ 4198951564SBaolin Wang if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) || 4298951564SBaolin Wang (rtc->start_secs < rtc->range_min && 4398951564SBaolin Wang secs <= (rtc->start_secs + rtc->range_max - rtc->range_min))) 4498951564SBaolin Wang return; 4598951564SBaolin Wang 4698951564SBaolin Wang rtc_time64_to_tm(secs + rtc->offset_secs, tm); 4798951564SBaolin Wang } 4898951564SBaolin Wang 4998951564SBaolin Wang static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm) 5098951564SBaolin Wang { 5198951564SBaolin Wang time64_t secs; 5298951564SBaolin Wang 5398951564SBaolin Wang if (!rtc->offset_secs) 5498951564SBaolin Wang return; 5598951564SBaolin Wang 5698951564SBaolin Wang secs = rtc_tm_to_time64(tm); 5798951564SBaolin Wang 5898951564SBaolin Wang /* 5998951564SBaolin Wang * If the setting time values are in the valid range of RTC hardware 6098951564SBaolin Wang * device, then no need to subtract the offset when setting time to RTC 6198951564SBaolin Wang * device. Otherwise we need to subtract the offset to make the time 6298951564SBaolin Wang * values are valid for RTC hardware device. 6398951564SBaolin Wang */ 6498951564SBaolin Wang if (secs >= rtc->range_min && secs <= rtc->range_max) 6598951564SBaolin Wang return; 6698951564SBaolin Wang 6798951564SBaolin Wang rtc_time64_to_tm(secs - rtc->offset_secs, tm); 6898951564SBaolin Wang } 6998951564SBaolin Wang 704c4e5df1SBaolin Wang static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) 714c4e5df1SBaolin Wang { 724c4e5df1SBaolin Wang if (rtc->range_min != rtc->range_max) { 734c4e5df1SBaolin Wang time64_t time = rtc_tm_to_time64(tm); 7498951564SBaolin Wang time64_t range_min = rtc->set_start_time ? rtc->start_secs : 7598951564SBaolin Wang rtc->range_min; 7698951564SBaolin Wang time64_t range_max = rtc->set_start_time ? 7798951564SBaolin Wang (rtc->start_secs + rtc->range_max - rtc->range_min) : 7898951564SBaolin Wang rtc->range_max; 794c4e5df1SBaolin Wang 8098951564SBaolin Wang if (time < range_min || time > range_max) 814c4e5df1SBaolin Wang return -ERANGE; 824c4e5df1SBaolin Wang } 834c4e5df1SBaolin Wang 844c4e5df1SBaolin Wang return 0; 854c4e5df1SBaolin Wang } 864c4e5df1SBaolin Wang 876610e089SJohn Stultz static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) 886610e089SJohn Stultz { 896610e089SJohn Stultz int err; 906610e089SJohn Stultz if (!rtc->ops) 916610e089SJohn Stultz err = -ENODEV; 926610e089SJohn Stultz else if (!rtc->ops->read_time) 936610e089SJohn Stultz err = -EINVAL; 946610e089SJohn Stultz else { 956610e089SJohn Stultz memset(tm, 0, sizeof(struct rtc_time)); 966610e089SJohn Stultz err = rtc->ops->read_time(rtc->dev.parent, tm); 9716682c86SHyogi Gim if (err < 0) { 98d0bddb51SAaro Koskinen dev_dbg(&rtc->dev, "read_time: fail to read: %d\n", 99d0bddb51SAaro Koskinen err); 10016682c86SHyogi Gim return err; 10116682c86SHyogi Gim } 10216682c86SHyogi Gim 10398951564SBaolin Wang rtc_add_offset(rtc, tm); 10498951564SBaolin Wang 10516682c86SHyogi Gim err = rtc_valid_tm(tm); 10616682c86SHyogi Gim if (err < 0) 107d0bddb51SAaro Koskinen dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n"); 1086610e089SJohn Stultz } 1096610e089SJohn Stultz return err; 1106610e089SJohn Stultz } 1110c86edc0SAlessandro Zummo 112ab6a2d70SDavid Brownell int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) 1130c86edc0SAlessandro Zummo { 1140c86edc0SAlessandro Zummo int err; 1150c86edc0SAlessandro Zummo 1160c86edc0SAlessandro Zummo err = mutex_lock_interruptible(&rtc->ops_lock); 1170c86edc0SAlessandro Zummo if (err) 118b68bb263SDavid Brownell return err; 1190c86edc0SAlessandro Zummo 1206610e089SJohn Stultz err = __rtc_read_time(rtc, tm); 1210c86edc0SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 12229a1f599SBaolin Wang 12329a1f599SBaolin Wang trace_rtc_read_time(rtc_tm_to_time64(tm), err); 1240c86edc0SAlessandro Zummo return err; 1250c86edc0SAlessandro Zummo } 1260c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_read_time); 1270c86edc0SAlessandro Zummo 128ab6a2d70SDavid Brownell int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) 1290c86edc0SAlessandro Zummo { 1300c86edc0SAlessandro Zummo int err; 1310c86edc0SAlessandro Zummo 1320c86edc0SAlessandro Zummo err = rtc_valid_tm(tm); 1330c86edc0SAlessandro Zummo if (err != 0) 1340c86edc0SAlessandro Zummo return err; 1350c86edc0SAlessandro Zummo 1364c4e5df1SBaolin Wang err = rtc_valid_range(rtc, tm); 1374c4e5df1SBaolin Wang if (err) 1384c4e5df1SBaolin Wang return err; 13971db049eSAlexandre Belloni 14098951564SBaolin Wang rtc_subtract_offset(rtc, tm); 14198951564SBaolin Wang 1420c86edc0SAlessandro Zummo err = mutex_lock_interruptible(&rtc->ops_lock); 1430c86edc0SAlessandro Zummo if (err) 144b68bb263SDavid Brownell return err; 1450c86edc0SAlessandro Zummo 1460c86edc0SAlessandro Zummo if (!rtc->ops) 1470c86edc0SAlessandro Zummo err = -ENODEV; 148bbccf83fSAlessandro Zummo else if (rtc->ops->set_time) 149cd966209SDavid Brownell err = rtc->ops->set_time(rtc->dev.parent, tm); 1508e4ff1a8SXunlei Pang else if (rtc->ops->set_mmss64) { 1518e4ff1a8SXunlei Pang time64_t secs64 = rtc_tm_to_time64(tm); 1528e4ff1a8SXunlei Pang 1538e4ff1a8SXunlei Pang err = rtc->ops->set_mmss64(rtc->dev.parent, secs64); 1548e4ff1a8SXunlei Pang } else if (rtc->ops->set_mmss) { 155bc10aa93SXunlei Pang time64_t secs64 = rtc_tm_to_time64(tm); 156bc10aa93SXunlei Pang err = rtc->ops->set_mmss(rtc->dev.parent, secs64); 157bbccf83fSAlessandro Zummo } else 158bbccf83fSAlessandro Zummo err = -EINVAL; 1590c86edc0SAlessandro Zummo 16014d0e347SZoran Markovic pm_stay_awake(rtc->dev.parent); 1610c86edc0SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 1625f9679d2SNeilBrown /* A timer might have just expired */ 1635f9679d2SNeilBrown schedule_work(&rtc->irqwork); 16429a1f599SBaolin Wang 16529a1f599SBaolin Wang trace_rtc_set_time(rtc_tm_to_time64(tm), err); 1660c86edc0SAlessandro Zummo return err; 1670c86edc0SAlessandro Zummo } 1680c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_set_time); 1690c86edc0SAlessandro Zummo 170f44f7f96SJohn Stultz static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 171f44f7f96SJohn Stultz { 172f44f7f96SJohn Stultz int err; 173f44f7f96SJohn Stultz 174f44f7f96SJohn Stultz err = mutex_lock_interruptible(&rtc->ops_lock); 175f44f7f96SJohn Stultz if (err) 176f44f7f96SJohn Stultz return err; 177f44f7f96SJohn Stultz 178f44f7f96SJohn Stultz if (rtc->ops == NULL) 179f44f7f96SJohn Stultz err = -ENODEV; 180f44f7f96SJohn Stultz else if (!rtc->ops->read_alarm) 181f44f7f96SJohn Stultz err = -EINVAL; 182f44f7f96SJohn Stultz else { 183d68778b8SUwe Kleine-König alarm->enabled = 0; 184d68778b8SUwe Kleine-König alarm->pending = 0; 185d68778b8SUwe Kleine-König alarm->time.tm_sec = -1; 186d68778b8SUwe Kleine-König alarm->time.tm_min = -1; 187d68778b8SUwe Kleine-König alarm->time.tm_hour = -1; 188d68778b8SUwe Kleine-König alarm->time.tm_mday = -1; 189d68778b8SUwe Kleine-König alarm->time.tm_mon = -1; 190d68778b8SUwe Kleine-König alarm->time.tm_year = -1; 191d68778b8SUwe Kleine-König alarm->time.tm_wday = -1; 192d68778b8SUwe Kleine-König alarm->time.tm_yday = -1; 193d68778b8SUwe Kleine-König alarm->time.tm_isdst = -1; 194f44f7f96SJohn Stultz err = rtc->ops->read_alarm(rtc->dev.parent, alarm); 195f44f7f96SJohn Stultz } 196f44f7f96SJohn Stultz 197f44f7f96SJohn Stultz mutex_unlock(&rtc->ops_lock); 19829a1f599SBaolin Wang 19929a1f599SBaolin Wang trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); 200f44f7f96SJohn Stultz return err; 201f44f7f96SJohn Stultz } 202f44f7f96SJohn Stultz 203f44f7f96SJohn Stultz int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 204f44f7f96SJohn Stultz { 205f44f7f96SJohn Stultz int err; 206f44f7f96SJohn Stultz struct rtc_time before, now; 207f44f7f96SJohn Stultz int first_time = 1; 208bc10aa93SXunlei Pang time64_t t_now, t_alm; 209f44f7f96SJohn Stultz enum { none, day, month, year } missing = none; 210f44f7f96SJohn Stultz unsigned days; 211f44f7f96SJohn Stultz 212f44f7f96SJohn Stultz /* The lower level RTC driver may return -1 in some fields, 213f44f7f96SJohn Stultz * creating invalid alarm->time values, for reasons like: 214f44f7f96SJohn Stultz * 215f44f7f96SJohn Stultz * - The hardware may not be capable of filling them in; 216f44f7f96SJohn Stultz * many alarms match only on time-of-day fields, not 217f44f7f96SJohn Stultz * day/month/year calendar data. 218f44f7f96SJohn Stultz * 219f44f7f96SJohn Stultz * - Some hardware uses illegal values as "wildcard" match 220f44f7f96SJohn Stultz * values, which non-Linux firmware (like a BIOS) may try 221f44f7f96SJohn Stultz * to set up as e.g. "alarm 15 minutes after each hour". 222f44f7f96SJohn Stultz * Linux uses only oneshot alarms. 223f44f7f96SJohn Stultz * 224f44f7f96SJohn Stultz * When we see that here, we deal with it by using values from 225f44f7f96SJohn Stultz * a current RTC timestamp for any missing (-1) values. The 226f44f7f96SJohn Stultz * RTC driver prevents "periodic alarm" modes. 227f44f7f96SJohn Stultz * 228f44f7f96SJohn Stultz * But this can be racey, because some fields of the RTC timestamp 229f44f7f96SJohn Stultz * may have wrapped in the interval since we read the RTC alarm, 230f44f7f96SJohn Stultz * which would lead to us inserting inconsistent values in place 231f44f7f96SJohn Stultz * of the -1 fields. 232f44f7f96SJohn Stultz * 233f44f7f96SJohn Stultz * Reading the alarm and timestamp in the reverse sequence 234f44f7f96SJohn Stultz * would have the same race condition, and not solve the issue. 235f44f7f96SJohn Stultz * 236f44f7f96SJohn Stultz * So, we must first read the RTC timestamp, 237f44f7f96SJohn Stultz * then read the RTC alarm value, 238f44f7f96SJohn Stultz * and then read a second RTC timestamp. 239f44f7f96SJohn Stultz * 240f44f7f96SJohn Stultz * If any fields of the second timestamp have changed 241f44f7f96SJohn Stultz * when compared with the first timestamp, then we know 242f44f7f96SJohn Stultz * our timestamp may be inconsistent with that used by 243f44f7f96SJohn Stultz * the low-level rtc_read_alarm_internal() function. 244f44f7f96SJohn Stultz * 245f44f7f96SJohn Stultz * So, when the two timestamps disagree, we just loop and do 246f44f7f96SJohn Stultz * the process again to get a fully consistent set of values. 247f44f7f96SJohn Stultz * 248f44f7f96SJohn Stultz * This could all instead be done in the lower level driver, 249f44f7f96SJohn Stultz * but since more than one lower level RTC implementation needs it, 250f44f7f96SJohn Stultz * then it's probably best best to do it here instead of there.. 251f44f7f96SJohn Stultz */ 252f44f7f96SJohn Stultz 253f44f7f96SJohn Stultz /* Get the "before" timestamp */ 254f44f7f96SJohn Stultz err = rtc_read_time(rtc, &before); 255f44f7f96SJohn Stultz if (err < 0) 256f44f7f96SJohn Stultz return err; 257f44f7f96SJohn Stultz do { 258f44f7f96SJohn Stultz if (!first_time) 259f44f7f96SJohn Stultz memcpy(&before, &now, sizeof(struct rtc_time)); 260f44f7f96SJohn Stultz first_time = 0; 261f44f7f96SJohn Stultz 262f44f7f96SJohn Stultz /* get the RTC alarm values, which may be incomplete */ 263f44f7f96SJohn Stultz err = rtc_read_alarm_internal(rtc, alarm); 264f44f7f96SJohn Stultz if (err) 265f44f7f96SJohn Stultz return err; 266f44f7f96SJohn Stultz 267f44f7f96SJohn Stultz /* full-function RTCs won't have such missing fields */ 268f44f7f96SJohn Stultz if (rtc_valid_tm(&alarm->time) == 0) 269f44f7f96SJohn Stultz return 0; 270f44f7f96SJohn Stultz 271f44f7f96SJohn Stultz /* get the "after" timestamp, to detect wrapped fields */ 272f44f7f96SJohn Stultz err = rtc_read_time(rtc, &now); 273f44f7f96SJohn Stultz if (err < 0) 274f44f7f96SJohn Stultz return err; 275f44f7f96SJohn Stultz 276f44f7f96SJohn Stultz /* note that tm_sec is a "don't care" value here: */ 277f44f7f96SJohn Stultz } while ( before.tm_min != now.tm_min 278f44f7f96SJohn Stultz || before.tm_hour != now.tm_hour 279f44f7f96SJohn Stultz || before.tm_mon != now.tm_mon 280f44f7f96SJohn Stultz || before.tm_year != now.tm_year); 281f44f7f96SJohn Stultz 282f44f7f96SJohn Stultz /* Fill in the missing alarm fields using the timestamp; we 283f44f7f96SJohn Stultz * know there's at least one since alarm->time is invalid. 284f44f7f96SJohn Stultz */ 285f44f7f96SJohn Stultz if (alarm->time.tm_sec == -1) 286f44f7f96SJohn Stultz alarm->time.tm_sec = now.tm_sec; 287f44f7f96SJohn Stultz if (alarm->time.tm_min == -1) 288f44f7f96SJohn Stultz alarm->time.tm_min = now.tm_min; 289f44f7f96SJohn Stultz if (alarm->time.tm_hour == -1) 290f44f7f96SJohn Stultz alarm->time.tm_hour = now.tm_hour; 291f44f7f96SJohn Stultz 292f44f7f96SJohn Stultz /* For simplicity, only support date rollover for now */ 293e74a8f2eSBen Hutchings if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) { 294f44f7f96SJohn Stultz alarm->time.tm_mday = now.tm_mday; 295f44f7f96SJohn Stultz missing = day; 296f44f7f96SJohn Stultz } 297e74a8f2eSBen Hutchings if ((unsigned)alarm->time.tm_mon >= 12) { 298f44f7f96SJohn Stultz alarm->time.tm_mon = now.tm_mon; 299f44f7f96SJohn Stultz if (missing == none) 300f44f7f96SJohn Stultz missing = month; 301f44f7f96SJohn Stultz } 302f44f7f96SJohn Stultz if (alarm->time.tm_year == -1) { 303f44f7f96SJohn Stultz alarm->time.tm_year = now.tm_year; 304f44f7f96SJohn Stultz if (missing == none) 305f44f7f96SJohn Stultz missing = year; 306f44f7f96SJohn Stultz } 307f44f7f96SJohn Stultz 308da96aea0SVaibhav Jain /* Can't proceed if alarm is still invalid after replacing 309da96aea0SVaibhav Jain * missing fields. 310da96aea0SVaibhav Jain */ 311da96aea0SVaibhav Jain err = rtc_valid_tm(&alarm->time); 312da96aea0SVaibhav Jain if (err) 313da96aea0SVaibhav Jain goto done; 314da96aea0SVaibhav Jain 315f44f7f96SJohn Stultz /* with luck, no rollover is needed */ 316bc10aa93SXunlei Pang t_now = rtc_tm_to_time64(&now); 317bc10aa93SXunlei Pang t_alm = rtc_tm_to_time64(&alarm->time); 318f44f7f96SJohn Stultz if (t_now < t_alm) 319f44f7f96SJohn Stultz goto done; 320f44f7f96SJohn Stultz 321f44f7f96SJohn Stultz switch (missing) { 322f44f7f96SJohn Stultz 323f44f7f96SJohn Stultz /* 24 hour rollover ... if it's now 10am Monday, an alarm that 324f44f7f96SJohn Stultz * that will trigger at 5am will do so at 5am Tuesday, which 325f44f7f96SJohn Stultz * could also be in the next month or year. This is a common 326f44f7f96SJohn Stultz * case, especially for PCs. 327f44f7f96SJohn Stultz */ 328f44f7f96SJohn Stultz case day: 329f44f7f96SJohn Stultz dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); 330f44f7f96SJohn Stultz t_alm += 24 * 60 * 60; 331bc10aa93SXunlei Pang rtc_time64_to_tm(t_alm, &alarm->time); 332f44f7f96SJohn Stultz break; 333f44f7f96SJohn Stultz 334f44f7f96SJohn Stultz /* Month rollover ... if it's the 31th, an alarm on the 3rd will 335f44f7f96SJohn Stultz * be next month. An alarm matching on the 30th, 29th, or 28th 336f44f7f96SJohn Stultz * may end up in the month after that! Many newer PCs support 337f44f7f96SJohn Stultz * this type of alarm. 338f44f7f96SJohn Stultz */ 339f44f7f96SJohn Stultz case month: 340f44f7f96SJohn Stultz dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); 341f44f7f96SJohn Stultz do { 342f44f7f96SJohn Stultz if (alarm->time.tm_mon < 11) 343f44f7f96SJohn Stultz alarm->time.tm_mon++; 344f44f7f96SJohn Stultz else { 345f44f7f96SJohn Stultz alarm->time.tm_mon = 0; 346f44f7f96SJohn Stultz alarm->time.tm_year++; 347f44f7f96SJohn Stultz } 348f44f7f96SJohn Stultz days = rtc_month_days(alarm->time.tm_mon, 349f44f7f96SJohn Stultz alarm->time.tm_year); 350f44f7f96SJohn Stultz } while (days < alarm->time.tm_mday); 351f44f7f96SJohn Stultz break; 352f44f7f96SJohn Stultz 353f44f7f96SJohn Stultz /* Year rollover ... easy except for leap years! */ 354f44f7f96SJohn Stultz case year: 355f44f7f96SJohn Stultz dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); 356f44f7f96SJohn Stultz do { 357f44f7f96SJohn Stultz alarm->time.tm_year++; 358ee1d9014SAles Novak } while (!is_leap_year(alarm->time.tm_year + 1900) 359ee1d9014SAles Novak && rtc_valid_tm(&alarm->time) != 0); 360f44f7f96SJohn Stultz break; 361f44f7f96SJohn Stultz 362f44f7f96SJohn Stultz default: 363f44f7f96SJohn Stultz dev_warn(&rtc->dev, "alarm rollover not handled\n"); 364f44f7f96SJohn Stultz } 365f44f7f96SJohn Stultz 366ee1d9014SAles Novak err = rtc_valid_tm(&alarm->time); 367ee1d9014SAles Novak 368da96aea0SVaibhav Jain done: 369ee1d9014SAles Novak if (err) { 370ee1d9014SAles Novak dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n", 371ee1d9014SAles Novak alarm->time.tm_year + 1900, alarm->time.tm_mon + 1, 372ee1d9014SAles Novak alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min, 373ee1d9014SAles Novak alarm->time.tm_sec); 374ee1d9014SAles Novak } 375ee1d9014SAles Novak 376ee1d9014SAles Novak return err; 377f44f7f96SJohn Stultz } 378f44f7f96SJohn Stultz 3796610e089SJohn Stultz int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 3800c86edc0SAlessandro Zummo { 3810c86edc0SAlessandro Zummo int err; 3820c86edc0SAlessandro Zummo 3830c86edc0SAlessandro Zummo err = mutex_lock_interruptible(&rtc->ops_lock); 3840c86edc0SAlessandro Zummo if (err) 385b68bb263SDavid Brownell return err; 386d5553a55SJohn Stultz if (rtc->ops == NULL) 387d5553a55SJohn Stultz err = -ENODEV; 388d5553a55SJohn Stultz else if (!rtc->ops->read_alarm) 389d5553a55SJohn Stultz err = -EINVAL; 390d5553a55SJohn Stultz else { 391d5553a55SJohn Stultz memset(alarm, 0, sizeof(struct rtc_wkalrm)); 3926610e089SJohn Stultz alarm->enabled = rtc->aie_timer.enabled; 3936610e089SJohn Stultz alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); 394d5553a55SJohn Stultz } 3950c86edc0SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 3960e36a9a4SMark Lord 39729a1f599SBaolin Wang trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); 398d5553a55SJohn Stultz return err; 3990e36a9a4SMark Lord } 4000c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_read_alarm); 4010c86edc0SAlessandro Zummo 402d576fe49SMark Brown static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 4036610e089SJohn Stultz { 4046610e089SJohn Stultz struct rtc_time tm; 405bc10aa93SXunlei Pang time64_t now, scheduled; 4066610e089SJohn Stultz int err; 4076610e089SJohn Stultz 4086610e089SJohn Stultz err = rtc_valid_tm(&alarm->time); 4096610e089SJohn Stultz if (err) 4106610e089SJohn Stultz return err; 41198951564SBaolin Wang 41298951564SBaolin Wang rtc_subtract_offset(rtc, &alarm->time); 413bc10aa93SXunlei Pang scheduled = rtc_tm_to_time64(&alarm->time); 4146610e089SJohn Stultz 4156610e089SJohn Stultz /* Make sure we're not setting alarms in the past */ 4166610e089SJohn Stultz err = __rtc_read_time(rtc, &tm); 417ca6dc2daSHyogi Gim if (err) 418ca6dc2daSHyogi Gim return err; 419bc10aa93SXunlei Pang now = rtc_tm_to_time64(&tm); 4206610e089SJohn Stultz if (scheduled <= now) 4216610e089SJohn Stultz return -ETIME; 4226610e089SJohn Stultz /* 4236610e089SJohn Stultz * XXX - We just checked to make sure the alarm time is not 4246610e089SJohn Stultz * in the past, but there is still a race window where if 4256610e089SJohn Stultz * the is alarm set for the next second and the second ticks 4266610e089SJohn Stultz * over right here, before we set the alarm. 4276610e089SJohn Stultz */ 4286610e089SJohn Stultz 429157e8bf8SLinus Torvalds if (!rtc->ops) 430157e8bf8SLinus Torvalds err = -ENODEV; 431157e8bf8SLinus Torvalds else if (!rtc->ops->set_alarm) 432157e8bf8SLinus Torvalds err = -EINVAL; 433157e8bf8SLinus Torvalds else 434157e8bf8SLinus Torvalds err = rtc->ops->set_alarm(rtc->dev.parent, alarm); 435157e8bf8SLinus Torvalds 43629a1f599SBaolin Wang trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err); 437157e8bf8SLinus Torvalds return err; 4386610e089SJohn Stultz } 4396610e089SJohn Stultz 440ab6a2d70SDavid Brownell int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 4410c86edc0SAlessandro Zummo { 4420c86edc0SAlessandro Zummo int err; 4430c86edc0SAlessandro Zummo 444*abfdff44SAlexandre Belloni if (!rtc->ops) 445*abfdff44SAlexandre Belloni return -ENODEV; 446*abfdff44SAlexandre Belloni else if (!rtc->ops->set_alarm) 447*abfdff44SAlexandre Belloni return -EINVAL; 448*abfdff44SAlexandre Belloni 449f8245c26SDavid Brownell err = rtc_valid_tm(&alarm->time); 450f8245c26SDavid Brownell if (err != 0) 451f8245c26SDavid Brownell return err; 452f8245c26SDavid Brownell 4534c4e5df1SBaolin Wang err = rtc_valid_range(rtc, &alarm->time); 4544c4e5df1SBaolin Wang if (err) 4554c4e5df1SBaolin Wang return err; 45671db049eSAlexandre Belloni 4570c86edc0SAlessandro Zummo err = mutex_lock_interruptible(&rtc->ops_lock); 4580c86edc0SAlessandro Zummo if (err) 459b68bb263SDavid Brownell return err; 4603ff2e13cSSachin Kamat if (rtc->aie_timer.enabled) 46196c8f06aSThomas Gleixner rtc_timer_remove(rtc, &rtc->aie_timer); 4623ff2e13cSSachin Kamat 4636610e089SJohn Stultz rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); 4648b0e1953SThomas Gleixner rtc->aie_timer.period = 0; 4653ff2e13cSSachin Kamat if (alarm->enabled) 466aa0be0f4SJohn Stultz err = rtc_timer_enqueue(rtc, &rtc->aie_timer); 4673ff2e13cSSachin Kamat 4680c86edc0SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 46998951564SBaolin Wang 47098951564SBaolin Wang rtc_add_offset(rtc, &alarm->time); 471aa0be0f4SJohn Stultz return err; 4720c86edc0SAlessandro Zummo } 4730c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_set_alarm); 4740c86edc0SAlessandro Zummo 475f6d5b331SJohn Stultz /* Called once per device from rtc_device_register */ 476f6d5b331SJohn Stultz int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 477f6d5b331SJohn Stultz { 478f6d5b331SJohn Stultz int err; 479bd729d72SJohn Stultz struct rtc_time now; 480f6d5b331SJohn Stultz 481f6d5b331SJohn Stultz err = rtc_valid_tm(&alarm->time); 482f6d5b331SJohn Stultz if (err != 0) 483f6d5b331SJohn Stultz return err; 484f6d5b331SJohn Stultz 485bd729d72SJohn Stultz err = rtc_read_time(rtc, &now); 486bd729d72SJohn Stultz if (err) 487bd729d72SJohn Stultz return err; 488bd729d72SJohn Stultz 489f6d5b331SJohn Stultz err = mutex_lock_interruptible(&rtc->ops_lock); 490f6d5b331SJohn Stultz if (err) 491f6d5b331SJohn Stultz return err; 492f6d5b331SJohn Stultz 493f6d5b331SJohn Stultz rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); 4948b0e1953SThomas Gleixner rtc->aie_timer.period = 0; 495bd729d72SJohn Stultz 4966785b3b6SUwe Kleine-König /* Alarm has to be enabled & in the future for us to enqueue it */ 4972456e855SThomas Gleixner if (alarm->enabled && (rtc_tm_to_ktime(now) < 4982456e855SThomas Gleixner rtc->aie_timer.node.expires)) { 499bd729d72SJohn Stultz 500f6d5b331SJohn Stultz rtc->aie_timer.enabled = 1; 501f6d5b331SJohn Stultz timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); 50229a1f599SBaolin Wang trace_rtc_timer_enqueue(&rtc->aie_timer); 503f6d5b331SJohn Stultz } 504f6d5b331SJohn Stultz mutex_unlock(&rtc->ops_lock); 505f6d5b331SJohn Stultz return err; 506f6d5b331SJohn Stultz } 507f6d5b331SJohn Stultz EXPORT_SYMBOL_GPL(rtc_initialize_alarm); 508f6d5b331SJohn Stultz 509099e6576SAlessandro Zummo int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) 510099e6576SAlessandro Zummo { 511099e6576SAlessandro Zummo int err = mutex_lock_interruptible(&rtc->ops_lock); 512099e6576SAlessandro Zummo if (err) 513099e6576SAlessandro Zummo return err; 514099e6576SAlessandro Zummo 5156610e089SJohn Stultz if (rtc->aie_timer.enabled != enabled) { 516aa0be0f4SJohn Stultz if (enabled) 517aa0be0f4SJohn Stultz err = rtc_timer_enqueue(rtc, &rtc->aie_timer); 518aa0be0f4SJohn Stultz else 51996c8f06aSThomas Gleixner rtc_timer_remove(rtc, &rtc->aie_timer); 5206610e089SJohn Stultz } 521aa0be0f4SJohn Stultz 522aa0be0f4SJohn Stultz if (err) 523516373b8SUwe Kleine-König /* nothing */; 524516373b8SUwe Kleine-König else if (!rtc->ops) 525099e6576SAlessandro Zummo err = -ENODEV; 526099e6576SAlessandro Zummo else if (!rtc->ops->alarm_irq_enable) 527099e6576SAlessandro Zummo err = -EINVAL; 528099e6576SAlessandro Zummo else 529099e6576SAlessandro Zummo err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); 530099e6576SAlessandro Zummo 531099e6576SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 53229a1f599SBaolin Wang 53329a1f599SBaolin Wang trace_rtc_alarm_irq_enable(enabled, err); 534099e6576SAlessandro Zummo return err; 535099e6576SAlessandro Zummo } 536099e6576SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); 537099e6576SAlessandro Zummo 538099e6576SAlessandro Zummo int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) 539099e6576SAlessandro Zummo { 540099e6576SAlessandro Zummo int err = mutex_lock_interruptible(&rtc->ops_lock); 541099e6576SAlessandro Zummo if (err) 542099e6576SAlessandro Zummo return err; 543099e6576SAlessandro Zummo 544456d66ecSJohn Stultz #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 545456d66ecSJohn Stultz if (enabled == 0 && rtc->uie_irq_active) { 546456d66ecSJohn Stultz mutex_unlock(&rtc->ops_lock); 547456d66ecSJohn Stultz return rtc_dev_update_irq_enable_emul(rtc, 0); 548456d66ecSJohn Stultz } 549456d66ecSJohn Stultz #endif 5506610e089SJohn Stultz /* make sure we're changing state */ 5516610e089SJohn Stultz if (rtc->uie_rtctimer.enabled == enabled) 5526610e089SJohn Stultz goto out; 5536610e089SJohn Stultz 5544a649903SJohn Stultz if (rtc->uie_unsupported) { 5554a649903SJohn Stultz err = -EINVAL; 5564a649903SJohn Stultz goto out; 5574a649903SJohn Stultz } 5584a649903SJohn Stultz 5596610e089SJohn Stultz if (enabled) { 5606610e089SJohn Stultz struct rtc_time tm; 5616610e089SJohn Stultz ktime_t now, onesec; 5626610e089SJohn Stultz 5636610e089SJohn Stultz __rtc_read_time(rtc, &tm); 5646610e089SJohn Stultz onesec = ktime_set(1, 0); 5656610e089SJohn Stultz now = rtc_tm_to_ktime(tm); 5666610e089SJohn Stultz rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); 5676610e089SJohn Stultz rtc->uie_rtctimer.period = ktime_set(1, 0); 568aa0be0f4SJohn Stultz err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); 569aa0be0f4SJohn Stultz } else 57096c8f06aSThomas Gleixner rtc_timer_remove(rtc, &rtc->uie_rtctimer); 571099e6576SAlessandro Zummo 5726610e089SJohn Stultz out: 573099e6576SAlessandro Zummo mutex_unlock(&rtc->ops_lock); 574456d66ecSJohn Stultz #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 575456d66ecSJohn Stultz /* 576456d66ecSJohn Stultz * Enable emulation if the driver did not provide 577456d66ecSJohn Stultz * the update_irq_enable function pointer or if returned 578456d66ecSJohn Stultz * -EINVAL to signal that it has been configured without 579456d66ecSJohn Stultz * interrupts or that are not available at the moment. 580456d66ecSJohn Stultz */ 581456d66ecSJohn Stultz if (err == -EINVAL) 582456d66ecSJohn Stultz err = rtc_dev_update_irq_enable_emul(rtc, enabled); 583456d66ecSJohn Stultz #endif 584099e6576SAlessandro Zummo return err; 5856610e089SJohn Stultz 586099e6576SAlessandro Zummo } 587099e6576SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_update_irq_enable); 588099e6576SAlessandro Zummo 5896610e089SJohn Stultz 590d728b1e6SDavid Brownell /** 5916610e089SJohn Stultz * rtc_handle_legacy_irq - AIE, UIE and PIE event hook 5926610e089SJohn Stultz * @rtc: pointer to the rtc device 5936610e089SJohn Stultz * 5946610e089SJohn Stultz * This function is called when an AIE, UIE or PIE mode interrupt 59525985edcSLucas De Marchi * has occurred (or been emulated). 5966610e089SJohn Stultz * 5976610e089SJohn Stultz * Triggers the registered irq_task function callback. 5986610e089SJohn Stultz */ 599456d66ecSJohn Stultz void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) 6006610e089SJohn Stultz { 6016610e089SJohn Stultz unsigned long flags; 6026610e089SJohn Stultz 6036610e089SJohn Stultz /* mark one irq of the appropriate mode */ 6046610e089SJohn Stultz spin_lock_irqsave(&rtc->irq_lock, flags); 6056610e089SJohn Stultz rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); 6066610e089SJohn Stultz spin_unlock_irqrestore(&rtc->irq_lock, flags); 6076610e089SJohn Stultz 6086610e089SJohn Stultz /* call the task func */ 6096610e089SJohn Stultz spin_lock_irqsave(&rtc->irq_task_lock, flags); 6106610e089SJohn Stultz if (rtc->irq_task) 6116610e089SJohn Stultz rtc->irq_task->func(rtc->irq_task->private_data); 6126610e089SJohn Stultz spin_unlock_irqrestore(&rtc->irq_task_lock, flags); 6136610e089SJohn Stultz 6146610e089SJohn Stultz wake_up_interruptible(&rtc->irq_queue); 6156610e089SJohn Stultz kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); 6166610e089SJohn Stultz } 6176610e089SJohn Stultz 6186610e089SJohn Stultz 6196610e089SJohn Stultz /** 6206610e089SJohn Stultz * rtc_aie_update_irq - AIE mode rtctimer hook 6216610e089SJohn Stultz * @private: pointer to the rtc_device 6226610e089SJohn Stultz * 6236610e089SJohn Stultz * This functions is called when the aie_timer expires. 6246610e089SJohn Stultz */ 6256610e089SJohn Stultz void rtc_aie_update_irq(void *private) 6266610e089SJohn Stultz { 6276610e089SJohn Stultz struct rtc_device *rtc = (struct rtc_device *)private; 6286610e089SJohn Stultz rtc_handle_legacy_irq(rtc, 1, RTC_AF); 6296610e089SJohn Stultz } 6306610e089SJohn Stultz 6316610e089SJohn Stultz 6326610e089SJohn Stultz /** 6336610e089SJohn Stultz * rtc_uie_update_irq - UIE mode rtctimer hook 6346610e089SJohn Stultz * @private: pointer to the rtc_device 6356610e089SJohn Stultz * 6366610e089SJohn Stultz * This functions is called when the uie_timer expires. 6376610e089SJohn Stultz */ 6386610e089SJohn Stultz void rtc_uie_update_irq(void *private) 6396610e089SJohn Stultz { 6406610e089SJohn Stultz struct rtc_device *rtc = (struct rtc_device *)private; 6416610e089SJohn Stultz rtc_handle_legacy_irq(rtc, 1, RTC_UF); 6426610e089SJohn Stultz } 6436610e089SJohn Stultz 6446610e089SJohn Stultz 6456610e089SJohn Stultz /** 6466610e089SJohn Stultz * rtc_pie_update_irq - PIE mode hrtimer hook 6476610e089SJohn Stultz * @timer: pointer to the pie mode hrtimer 6486610e089SJohn Stultz * 6496610e089SJohn Stultz * This function is used to emulate PIE mode interrupts 6506610e089SJohn Stultz * using an hrtimer. This function is called when the periodic 6516610e089SJohn Stultz * hrtimer expires. 6526610e089SJohn Stultz */ 6536610e089SJohn Stultz enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer) 6546610e089SJohn Stultz { 6556610e089SJohn Stultz struct rtc_device *rtc; 6566610e089SJohn Stultz ktime_t period; 6576610e089SJohn Stultz int count; 6586610e089SJohn Stultz rtc = container_of(timer, struct rtc_device, pie_timer); 6596610e089SJohn Stultz 6608b0e1953SThomas Gleixner period = NSEC_PER_SEC / rtc->irq_freq; 6616610e089SJohn Stultz count = hrtimer_forward_now(timer, period); 6626610e089SJohn Stultz 6636610e089SJohn Stultz rtc_handle_legacy_irq(rtc, count, RTC_PF); 6646610e089SJohn Stultz 6656610e089SJohn Stultz return HRTIMER_RESTART; 6666610e089SJohn Stultz } 6676610e089SJohn Stultz 6686610e089SJohn Stultz /** 6696610e089SJohn Stultz * rtc_update_irq - Triggered when a RTC interrupt occurs. 670ab6a2d70SDavid Brownell * @rtc: the rtc device 671d728b1e6SDavid Brownell * @num: how many irqs are being reported (usually one) 672d728b1e6SDavid Brownell * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF 673e6229becSAtsushi Nemoto * Context: any 674d728b1e6SDavid Brownell */ 675ab6a2d70SDavid Brownell void rtc_update_irq(struct rtc_device *rtc, 6760c86edc0SAlessandro Zummo unsigned long num, unsigned long events) 6770c86edc0SAlessandro Zummo { 678e7cba884Sviresh kumar if (IS_ERR_OR_NULL(rtc)) 679131c9cc8SAlessandro Zummo return; 680131c9cc8SAlessandro Zummo 6817523ceedSNeilBrown pm_stay_awake(rtc->dev.parent); 6826610e089SJohn Stultz schedule_work(&rtc->irqwork); 6830c86edc0SAlessandro Zummo } 6840c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_update_irq); 6850c86edc0SAlessandro Zummo 6869f3b795aSMichał Mirosław static int __rtc_match(struct device *dev, const void *data) 68771da8905SDave Young { 6889f3b795aSMichał Mirosław const char *name = data; 68971da8905SDave Young 690d4afc76cSKay Sievers if (strcmp(dev_name(dev), name) == 0) 69171da8905SDave Young return 1; 69271da8905SDave Young return 0; 69371da8905SDave Young } 69471da8905SDave Young 6959f3b795aSMichał Mirosław struct rtc_device *rtc_class_open(const char *name) 6960c86edc0SAlessandro Zummo { 697cd966209SDavid Brownell struct device *dev; 698ab6a2d70SDavid Brownell struct rtc_device *rtc = NULL; 6990c86edc0SAlessandro Zummo 700695794aeSGreg Kroah-Hartman dev = class_find_device(rtc_class, NULL, name, __rtc_match); 701cd966209SDavid Brownell if (dev) 702cd966209SDavid Brownell rtc = to_rtc_device(dev); 7030c86edc0SAlessandro Zummo 704ab6a2d70SDavid Brownell if (rtc) { 705ab6a2d70SDavid Brownell if (!try_module_get(rtc->owner)) { 706cd966209SDavid Brownell put_device(dev); 707ab6a2d70SDavid Brownell rtc = NULL; 708ab6a2d70SDavid Brownell } 7090c86edc0SAlessandro Zummo } 7100c86edc0SAlessandro Zummo 711ab6a2d70SDavid Brownell return rtc; 7120c86edc0SAlessandro Zummo } 7130c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_class_open); 7140c86edc0SAlessandro Zummo 715ab6a2d70SDavid Brownell void rtc_class_close(struct rtc_device *rtc) 7160c86edc0SAlessandro Zummo { 717ab6a2d70SDavid Brownell module_put(rtc->owner); 718cd966209SDavid Brownell put_device(&rtc->dev); 7190c86edc0SAlessandro Zummo } 7200c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_class_close); 7210c86edc0SAlessandro Zummo 722ab6a2d70SDavid Brownell int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) 7230c86edc0SAlessandro Zummo { 7240c86edc0SAlessandro Zummo int retval = -EBUSY; 7250c86edc0SAlessandro Zummo 7260c86edc0SAlessandro Zummo if (task == NULL || task->func == NULL) 7270c86edc0SAlessandro Zummo return -EINVAL; 7280c86edc0SAlessandro Zummo 729d691eb90SAlessandro Zummo /* Cannot register while the char dev is in use */ 730372a302eSJiri Kosina if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) 731d691eb90SAlessandro Zummo return -EBUSY; 732d691eb90SAlessandro Zummo 733d728b1e6SDavid Brownell spin_lock_irq(&rtc->irq_task_lock); 7340c86edc0SAlessandro Zummo if (rtc->irq_task == NULL) { 7350c86edc0SAlessandro Zummo rtc->irq_task = task; 7360c86edc0SAlessandro Zummo retval = 0; 7370c86edc0SAlessandro Zummo } 738d728b1e6SDavid Brownell spin_unlock_irq(&rtc->irq_task_lock); 7390c86edc0SAlessandro Zummo 740372a302eSJiri Kosina clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); 741d691eb90SAlessandro Zummo 7420c86edc0SAlessandro Zummo return retval; 7430c86edc0SAlessandro Zummo } 7440c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_irq_register); 7450c86edc0SAlessandro Zummo 746ab6a2d70SDavid Brownell void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) 7470c86edc0SAlessandro Zummo { 748d728b1e6SDavid Brownell spin_lock_irq(&rtc->irq_task_lock); 7490c86edc0SAlessandro Zummo if (rtc->irq_task == task) 7500c86edc0SAlessandro Zummo rtc->irq_task = NULL; 751d728b1e6SDavid Brownell spin_unlock_irq(&rtc->irq_task_lock); 7520c86edc0SAlessandro Zummo } 7530c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_irq_unregister); 7540c86edc0SAlessandro Zummo 7553c8bb90eSThomas Gleixner static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled) 7563c8bb90eSThomas Gleixner { 7573c8bb90eSThomas Gleixner /* 7583c8bb90eSThomas Gleixner * We always cancel the timer here first, because otherwise 7593c8bb90eSThomas Gleixner * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); 7603c8bb90eSThomas Gleixner * when we manage to start the timer before the callback 7613c8bb90eSThomas Gleixner * returns HRTIMER_RESTART. 7623c8bb90eSThomas Gleixner * 7633c8bb90eSThomas Gleixner * We cannot use hrtimer_cancel() here as a running callback 7643c8bb90eSThomas Gleixner * could be blocked on rtc->irq_task_lock and hrtimer_cancel() 7653c8bb90eSThomas Gleixner * would spin forever. 7663c8bb90eSThomas Gleixner */ 7673c8bb90eSThomas Gleixner if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) 7683c8bb90eSThomas Gleixner return -1; 7693c8bb90eSThomas Gleixner 7703c8bb90eSThomas Gleixner if (enabled) { 7718b0e1953SThomas Gleixner ktime_t period = NSEC_PER_SEC / rtc->irq_freq; 7723c8bb90eSThomas Gleixner 7733c8bb90eSThomas Gleixner hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); 7743c8bb90eSThomas Gleixner } 7753c8bb90eSThomas Gleixner return 0; 7763c8bb90eSThomas Gleixner } 7773c8bb90eSThomas Gleixner 77897144c67SDavid Brownell /** 77997144c67SDavid Brownell * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs 78097144c67SDavid Brownell * @rtc: the rtc device 78197144c67SDavid Brownell * @task: currently registered with rtc_irq_register() 78297144c67SDavid Brownell * @enabled: true to enable periodic IRQs 78397144c67SDavid Brownell * Context: any 78497144c67SDavid Brownell * 78597144c67SDavid Brownell * Note that rtc_irq_set_freq() should previously have been used to 78697144c67SDavid Brownell * specify the desired frequency of periodic IRQ task->func() callbacks. 78797144c67SDavid Brownell */ 788ab6a2d70SDavid Brownell int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled) 7890c86edc0SAlessandro Zummo { 7900c86edc0SAlessandro Zummo int err = 0; 7910c86edc0SAlessandro Zummo unsigned long flags; 7920c86edc0SAlessandro Zummo 7933c8bb90eSThomas Gleixner retry: 7940c86edc0SAlessandro Zummo spin_lock_irqsave(&rtc->irq_task_lock, flags); 795d691eb90SAlessandro Zummo if (rtc->irq_task != NULL && task == NULL) 796d691eb90SAlessandro Zummo err = -EBUSY; 7970734e27fSChris Brand else if (rtc->irq_task != task) 798d691eb90SAlessandro Zummo err = -EACCES; 7990734e27fSChris Brand else { 8003c8bb90eSThomas Gleixner if (rtc_update_hrtimer(rtc, enabled) < 0) { 8013c8bb90eSThomas Gleixner spin_unlock_irqrestore(&rtc->irq_task_lock, flags); 8023c8bb90eSThomas Gleixner cpu_relax(); 8033c8bb90eSThomas Gleixner goto retry; 8046610e089SJohn Stultz } 8056610e089SJohn Stultz rtc->pie_enabled = enabled; 8063c8bb90eSThomas Gleixner } 8076610e089SJohn Stultz spin_unlock_irqrestore(&rtc->irq_task_lock, flags); 80829a1f599SBaolin Wang 80929a1f599SBaolin Wang trace_rtc_irq_set_state(enabled, err); 8100c86edc0SAlessandro Zummo return err; 8110c86edc0SAlessandro Zummo } 8120c86edc0SAlessandro Zummo EXPORT_SYMBOL_GPL(rtc_irq_set_state); 8130c86edc0SAlessandro Zummo 81497144c67SDavid Brownell /** 81597144c67SDavid Brownell * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ 81697144c67SDavid Brownell * @rtc: the rtc device 81797144c67SDavid Brownell * @task: currently registered with rtc_irq_register() 81897144c67SDavid Brownell * @freq: positive frequency with which task->func() will be called 81997144c67SDavid Brownell * Context: any 82097144c67SDavid Brownell * 82197144c67SDavid Brownell * Note that rtc_irq_set_state() is used to enable or disable the 82297144c67SDavid Brownell * periodic IRQs. 82397144c67SDavid Brownell */ 824ab6a2d70SDavid Brownell int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq) 8250c86edc0SAlessandro Zummo { 82656f10c63SAlessandro Zummo int err = 0; 8270c86edc0SAlessandro Zummo unsigned long flags; 8280c86edc0SAlessandro Zummo 8296e7a333eSThomas Gleixner if (freq <= 0 || freq > RTC_MAX_FREQ) 83083a06bf5SMarcelo Roberto Jimenez return -EINVAL; 8313c8bb90eSThomas Gleixner retry: 8320c86edc0SAlessandro Zummo spin_lock_irqsave(&rtc->irq_task_lock, flags); 833d691eb90SAlessandro Zummo if (rtc->irq_task != NULL && task == NULL) 834d691eb90SAlessandro Zummo err = -EBUSY; 8350734e27fSChris Brand else if (rtc->irq_task != task) 836d691eb90SAlessandro Zummo err = -EACCES; 8370734e27fSChris Brand else { 8380c86edc0SAlessandro Zummo rtc->irq_freq = freq; 8393c8bb90eSThomas Gleixner if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) { 8403c8bb90eSThomas Gleixner spin_unlock_irqrestore(&rtc->irq_task_lock, flags); 8413c8bb90eSThomas Gleixner cpu_relax(); 8423c8bb90eSThomas Gleixner goto retry; 8430c86edc0SAlessandro Zummo } 8446610e089SJohn Stultz } 8456610e089SJohn Stultz spin_unlock_irqrestore(&rtc->irq_task_lock, flags); 84629a1f599SBaolin Wang 84729a1f599SBaolin Wang trace_rtc_irq_set_freq(freq, err); 8480c86edc0SAlessandro Zummo return err; 8490c86edc0SAlessandro Zummo } 8502601a464SDavid Brownell EXPORT_SYMBOL_GPL(rtc_irq_set_freq); 8516610e089SJohn Stultz 8526610e089SJohn Stultz /** 85396c8f06aSThomas Gleixner * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue 8546610e089SJohn Stultz * @rtc rtc device 8556610e089SJohn Stultz * @timer timer being added. 8566610e089SJohn Stultz * 8576610e089SJohn Stultz * Enqueues a timer onto the rtc devices timerqueue and sets 8586610e089SJohn Stultz * the next alarm event appropriately. 8596610e089SJohn Stultz * 860aa0be0f4SJohn Stultz * Sets the enabled bit on the added timer. 861aa0be0f4SJohn Stultz * 8626610e089SJohn Stultz * Must hold ops_lock for proper serialization of timerqueue 8636610e089SJohn Stultz */ 864aa0be0f4SJohn Stultz static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) 8656610e089SJohn Stultz { 8662b2f5ff0SColin Ian King struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); 8672b2f5ff0SColin Ian King struct rtc_time tm; 8682b2f5ff0SColin Ian King ktime_t now; 8692b2f5ff0SColin Ian King 870aa0be0f4SJohn Stultz timer->enabled = 1; 8712b2f5ff0SColin Ian King __rtc_read_time(rtc, &tm); 8722b2f5ff0SColin Ian King now = rtc_tm_to_ktime(tm); 8732b2f5ff0SColin Ian King 8742b2f5ff0SColin Ian King /* Skip over expired timers */ 8752b2f5ff0SColin Ian King while (next) { 8762456e855SThomas Gleixner if (next->expires >= now) 8772b2f5ff0SColin Ian King break; 8782b2f5ff0SColin Ian King next = timerqueue_iterate_next(next); 8792b2f5ff0SColin Ian King } 8802b2f5ff0SColin Ian King 8816610e089SJohn Stultz timerqueue_add(&rtc->timerqueue, &timer->node); 88229a1f599SBaolin Wang trace_rtc_timer_enqueue(timer); 88374717b28SAlexandre Belloni if (!next || ktime_before(timer->node.expires, next->expires)) { 8846610e089SJohn Stultz struct rtc_wkalrm alarm; 8856610e089SJohn Stultz int err; 8866610e089SJohn Stultz alarm.time = rtc_ktime_to_tm(timer->node.expires); 8876610e089SJohn Stultz alarm.enabled = 1; 8886610e089SJohn Stultz err = __rtc_set_alarm(rtc, &alarm); 88914d0e347SZoran Markovic if (err == -ETIME) { 89014d0e347SZoran Markovic pm_stay_awake(rtc->dev.parent); 8916610e089SJohn Stultz schedule_work(&rtc->irqwork); 89214d0e347SZoran Markovic } else if (err) { 893aa0be0f4SJohn Stultz timerqueue_del(&rtc->timerqueue, &timer->node); 89429a1f599SBaolin Wang trace_rtc_timer_dequeue(timer); 895aa0be0f4SJohn Stultz timer->enabled = 0; 896aa0be0f4SJohn Stultz return err; 8976610e089SJohn Stultz } 8986610e089SJohn Stultz } 899aa0be0f4SJohn Stultz return 0; 900aa0be0f4SJohn Stultz } 9016610e089SJohn Stultz 90241c7f742SRabin Vincent static void rtc_alarm_disable(struct rtc_device *rtc) 90341c7f742SRabin Vincent { 90441c7f742SRabin Vincent if (!rtc->ops || !rtc->ops->alarm_irq_enable) 90541c7f742SRabin Vincent return; 90641c7f742SRabin Vincent 90741c7f742SRabin Vincent rtc->ops->alarm_irq_enable(rtc->dev.parent, false); 90829a1f599SBaolin Wang trace_rtc_alarm_irq_enable(0, 0); 90941c7f742SRabin Vincent } 91041c7f742SRabin Vincent 9116610e089SJohn Stultz /** 91296c8f06aSThomas Gleixner * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue 9136610e089SJohn Stultz * @rtc rtc device 9146610e089SJohn Stultz * @timer timer being removed. 9156610e089SJohn Stultz * 9166610e089SJohn Stultz * Removes a timer onto the rtc devices timerqueue and sets 9176610e089SJohn Stultz * the next alarm event appropriately. 9186610e089SJohn Stultz * 919aa0be0f4SJohn Stultz * Clears the enabled bit on the removed timer. 920aa0be0f4SJohn Stultz * 9216610e089SJohn Stultz * Must hold ops_lock for proper serialization of timerqueue 9226610e089SJohn Stultz */ 923aa0be0f4SJohn Stultz static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) 9246610e089SJohn Stultz { 9256610e089SJohn Stultz struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); 9266610e089SJohn Stultz timerqueue_del(&rtc->timerqueue, &timer->node); 92729a1f599SBaolin Wang trace_rtc_timer_dequeue(timer); 928aa0be0f4SJohn Stultz timer->enabled = 0; 9296610e089SJohn Stultz if (next == &timer->node) { 9306610e089SJohn Stultz struct rtc_wkalrm alarm; 9316610e089SJohn Stultz int err; 9326610e089SJohn Stultz next = timerqueue_getnext(&rtc->timerqueue); 93341c7f742SRabin Vincent if (!next) { 93441c7f742SRabin Vincent rtc_alarm_disable(rtc); 9356610e089SJohn Stultz return; 93641c7f742SRabin Vincent } 9376610e089SJohn Stultz alarm.time = rtc_ktime_to_tm(next->expires); 9386610e089SJohn Stultz alarm.enabled = 1; 9396610e089SJohn Stultz err = __rtc_set_alarm(rtc, &alarm); 94014d0e347SZoran Markovic if (err == -ETIME) { 94114d0e347SZoran Markovic pm_stay_awake(rtc->dev.parent); 9426610e089SJohn Stultz schedule_work(&rtc->irqwork); 9436610e089SJohn Stultz } 9446610e089SJohn Stultz } 94514d0e347SZoran Markovic } 9466610e089SJohn Stultz 9476610e089SJohn Stultz /** 94896c8f06aSThomas Gleixner * rtc_timer_do_work - Expires rtc timers 9496610e089SJohn Stultz * @rtc rtc device 9506610e089SJohn Stultz * @timer timer being removed. 9516610e089SJohn Stultz * 9526610e089SJohn Stultz * Expires rtc timers. Reprograms next alarm event if needed. 9536610e089SJohn Stultz * Called via worktask. 9546610e089SJohn Stultz * 9556610e089SJohn Stultz * Serializes access to timerqueue via ops_lock mutex 9566610e089SJohn Stultz */ 95796c8f06aSThomas Gleixner void rtc_timer_do_work(struct work_struct *work) 9586610e089SJohn Stultz { 9596610e089SJohn Stultz struct rtc_timer *timer; 9606610e089SJohn Stultz struct timerqueue_node *next; 9616610e089SJohn Stultz ktime_t now; 9626610e089SJohn Stultz struct rtc_time tm; 9636610e089SJohn Stultz 9646610e089SJohn Stultz struct rtc_device *rtc = 9656610e089SJohn Stultz container_of(work, struct rtc_device, irqwork); 9666610e089SJohn Stultz 9676610e089SJohn Stultz mutex_lock(&rtc->ops_lock); 9686610e089SJohn Stultz again: 9696610e089SJohn Stultz __rtc_read_time(rtc, &tm); 9706610e089SJohn Stultz now = rtc_tm_to_ktime(tm); 9716610e089SJohn Stultz while ((next = timerqueue_getnext(&rtc->timerqueue))) { 9722456e855SThomas Gleixner if (next->expires > now) 9736610e089SJohn Stultz break; 9746610e089SJohn Stultz 9756610e089SJohn Stultz /* expire timer */ 9766610e089SJohn Stultz timer = container_of(next, struct rtc_timer, node); 9776610e089SJohn Stultz timerqueue_del(&rtc->timerqueue, &timer->node); 97829a1f599SBaolin Wang trace_rtc_timer_dequeue(timer); 9796610e089SJohn Stultz timer->enabled = 0; 9806610e089SJohn Stultz if (timer->task.func) 9816610e089SJohn Stultz timer->task.func(timer->task.private_data); 9826610e089SJohn Stultz 98329a1f599SBaolin Wang trace_rtc_timer_fired(timer); 9846610e089SJohn Stultz /* Re-add/fwd periodic timers */ 9856610e089SJohn Stultz if (ktime_to_ns(timer->period)) { 9866610e089SJohn Stultz timer->node.expires = ktime_add(timer->node.expires, 9876610e089SJohn Stultz timer->period); 9886610e089SJohn Stultz timer->enabled = 1; 9896610e089SJohn Stultz timerqueue_add(&rtc->timerqueue, &timer->node); 99029a1f599SBaolin Wang trace_rtc_timer_enqueue(timer); 9916610e089SJohn Stultz } 9926610e089SJohn Stultz } 9936610e089SJohn Stultz 9946610e089SJohn Stultz /* Set next alarm */ 9956610e089SJohn Stultz if (next) { 9966610e089SJohn Stultz struct rtc_wkalrm alarm; 9976610e089SJohn Stultz int err; 9986528b889SXunlei Pang int retry = 3; 9996528b889SXunlei Pang 10006610e089SJohn Stultz alarm.time = rtc_ktime_to_tm(next->expires); 10016610e089SJohn Stultz alarm.enabled = 1; 10026528b889SXunlei Pang reprogram: 10036610e089SJohn Stultz err = __rtc_set_alarm(rtc, &alarm); 10046610e089SJohn Stultz if (err == -ETIME) 10056610e089SJohn Stultz goto again; 10066528b889SXunlei Pang else if (err) { 10076528b889SXunlei Pang if (retry-- > 0) 10086528b889SXunlei Pang goto reprogram; 10096528b889SXunlei Pang 10106528b889SXunlei Pang timer = container_of(next, struct rtc_timer, node); 10116528b889SXunlei Pang timerqueue_del(&rtc->timerqueue, &timer->node); 101229a1f599SBaolin Wang trace_rtc_timer_dequeue(timer); 10136528b889SXunlei Pang timer->enabled = 0; 10146528b889SXunlei Pang dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); 10156528b889SXunlei Pang goto again; 10166528b889SXunlei Pang } 101741c7f742SRabin Vincent } else 101841c7f742SRabin Vincent rtc_alarm_disable(rtc); 10196610e089SJohn Stultz 102014d0e347SZoran Markovic pm_relax(rtc->dev.parent); 10216610e089SJohn Stultz mutex_unlock(&rtc->ops_lock); 10226610e089SJohn Stultz } 10236610e089SJohn Stultz 10246610e089SJohn Stultz 102596c8f06aSThomas Gleixner /* rtc_timer_init - Initializes an rtc_timer 10266610e089SJohn Stultz * @timer: timer to be intiialized 10276610e089SJohn Stultz * @f: function pointer to be called when timer fires 10286610e089SJohn Stultz * @data: private data passed to function pointer 10296610e089SJohn Stultz * 10306610e089SJohn Stultz * Kernel interface to initializing an rtc_timer. 10316610e089SJohn Stultz */ 103296c8f06aSThomas Gleixner void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data) 10336610e089SJohn Stultz { 10346610e089SJohn Stultz timerqueue_init(&timer->node); 10356610e089SJohn Stultz timer->enabled = 0; 10366610e089SJohn Stultz timer->task.func = f; 10376610e089SJohn Stultz timer->task.private_data = data; 10386610e089SJohn Stultz } 10396610e089SJohn Stultz 104096c8f06aSThomas Gleixner /* rtc_timer_start - Sets an rtc_timer to fire in the future 10416610e089SJohn Stultz * @ rtc: rtc device to be used 10426610e089SJohn Stultz * @ timer: timer being set 10436610e089SJohn Stultz * @ expires: time at which to expire the timer 10446610e089SJohn Stultz * @ period: period that the timer will recur 10456610e089SJohn Stultz * 10466610e089SJohn Stultz * Kernel interface to set an rtc_timer 10476610e089SJohn Stultz */ 104896c8f06aSThomas Gleixner int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, 10496610e089SJohn Stultz ktime_t expires, ktime_t period) 10506610e089SJohn Stultz { 10516610e089SJohn Stultz int ret = 0; 10526610e089SJohn Stultz mutex_lock(&rtc->ops_lock); 10536610e089SJohn Stultz if (timer->enabled) 105496c8f06aSThomas Gleixner rtc_timer_remove(rtc, timer); 10556610e089SJohn Stultz 10566610e089SJohn Stultz timer->node.expires = expires; 10576610e089SJohn Stultz timer->period = period; 10586610e089SJohn Stultz 1059aa0be0f4SJohn Stultz ret = rtc_timer_enqueue(rtc, timer); 10606610e089SJohn Stultz 10616610e089SJohn Stultz mutex_unlock(&rtc->ops_lock); 10626610e089SJohn Stultz return ret; 10636610e089SJohn Stultz } 10646610e089SJohn Stultz 106596c8f06aSThomas Gleixner /* rtc_timer_cancel - Stops an rtc_timer 10666610e089SJohn Stultz * @ rtc: rtc device to be used 10676610e089SJohn Stultz * @ timer: timer being set 10686610e089SJohn Stultz * 10696610e089SJohn Stultz * Kernel interface to cancel an rtc_timer 10706610e089SJohn Stultz */ 107173744a64SKrzysztof Kozlowski void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer) 10726610e089SJohn Stultz { 10736610e089SJohn Stultz mutex_lock(&rtc->ops_lock); 10746610e089SJohn Stultz if (timer->enabled) 107596c8f06aSThomas Gleixner rtc_timer_remove(rtc, timer); 10766610e089SJohn Stultz mutex_unlock(&rtc->ops_lock); 10776610e089SJohn Stultz } 10786610e089SJohn Stultz 1079b3967067SJoshua Clayton /** 1080b3967067SJoshua Clayton * rtc_read_offset - Read the amount of rtc offset in parts per billion 1081b3967067SJoshua Clayton * @ rtc: rtc device to be used 1082b3967067SJoshua Clayton * @ offset: the offset in parts per billion 1083b3967067SJoshua Clayton * 1084b3967067SJoshua Clayton * see below for details. 1085b3967067SJoshua Clayton * 1086b3967067SJoshua Clayton * Kernel interface to read rtc clock offset 1087b3967067SJoshua Clayton * Returns 0 on success, or a negative number on error. 1088b3967067SJoshua Clayton * If read_offset() is not implemented for the rtc, return -EINVAL 1089b3967067SJoshua Clayton */ 1090b3967067SJoshua Clayton int rtc_read_offset(struct rtc_device *rtc, long *offset) 1091b3967067SJoshua Clayton { 1092b3967067SJoshua Clayton int ret; 10936610e089SJohn Stultz 1094b3967067SJoshua Clayton if (!rtc->ops) 1095b3967067SJoshua Clayton return -ENODEV; 1096b3967067SJoshua Clayton 1097b3967067SJoshua Clayton if (!rtc->ops->read_offset) 1098b3967067SJoshua Clayton return -EINVAL; 1099b3967067SJoshua Clayton 1100b3967067SJoshua Clayton mutex_lock(&rtc->ops_lock); 1101b3967067SJoshua Clayton ret = rtc->ops->read_offset(rtc->dev.parent, offset); 1102b3967067SJoshua Clayton mutex_unlock(&rtc->ops_lock); 110329a1f599SBaolin Wang 110429a1f599SBaolin Wang trace_rtc_read_offset(*offset, ret); 1105b3967067SJoshua Clayton return ret; 1106b3967067SJoshua Clayton } 1107b3967067SJoshua Clayton 1108b3967067SJoshua Clayton /** 1109b3967067SJoshua Clayton * rtc_set_offset - Adjusts the duration of the average second 1110b3967067SJoshua Clayton * @ rtc: rtc device to be used 1111b3967067SJoshua Clayton * @ offset: the offset in parts per billion 1112b3967067SJoshua Clayton * 1113b3967067SJoshua Clayton * Some rtc's allow an adjustment to the average duration of a second 1114b3967067SJoshua Clayton * to compensate for differences in the actual clock rate due to temperature, 1115b3967067SJoshua Clayton * the crystal, capacitor, etc. 1116b3967067SJoshua Clayton * 11178a25c8f6SRussell King * The adjustment applied is as follows: 11188a25c8f6SRussell King * t = t0 * (1 + offset * 1e-9) 11198a25c8f6SRussell King * where t0 is the measured length of 1 RTC second with offset = 0 11208a25c8f6SRussell King * 1121b3967067SJoshua Clayton * Kernel interface to adjust an rtc clock offset. 1122b3967067SJoshua Clayton * Return 0 on success, or a negative number on error. 1123b3967067SJoshua Clayton * If the rtc offset is not setable (or not implemented), return -EINVAL 1124b3967067SJoshua Clayton */ 1125b3967067SJoshua Clayton int rtc_set_offset(struct rtc_device *rtc, long offset) 1126b3967067SJoshua Clayton { 1127b3967067SJoshua Clayton int ret; 1128b3967067SJoshua Clayton 1129b3967067SJoshua Clayton if (!rtc->ops) 1130b3967067SJoshua Clayton return -ENODEV; 1131b3967067SJoshua Clayton 1132b3967067SJoshua Clayton if (!rtc->ops->set_offset) 1133b3967067SJoshua Clayton return -EINVAL; 1134b3967067SJoshua Clayton 1135b3967067SJoshua Clayton mutex_lock(&rtc->ops_lock); 1136b3967067SJoshua Clayton ret = rtc->ops->set_offset(rtc->dev.parent, offset); 1137b3967067SJoshua Clayton mutex_unlock(&rtc->ops_lock); 113829a1f599SBaolin Wang 113929a1f599SBaolin Wang trace_rtc_set_offset(offset, ret); 1140b3967067SJoshua Clayton return ret; 1141b3967067SJoshua Clayton } 1142