Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * RTC subsystem, interface functions
0004  *
0005  * Copyright (C) 2005 Tower Technologies
0006  * Author: Alessandro Zummo <a.zummo@towertech.it>
0007  *
0008  * based on arch/arm/common/rtctime.c
0009  */
0010 
0011 #include <linux/rtc.h>
0012 #include <linux/sched.h>
0013 #include <linux/module.h>
0014 #include <linux/log2.h>
0015 #include <linux/workqueue.h>
0016 
0017 #define CREATE_TRACE_POINTS
0018 #include <trace/events/rtc.h>
0019 
0020 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
0021 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
0022 
0023 static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
0024 {
0025     time64_t secs;
0026 
0027     if (!rtc->offset_secs)
0028         return;
0029 
0030     secs = rtc_tm_to_time64(tm);
0031 
0032     /*
0033      * Since the reading time values from RTC device are always in the RTC
0034      * original valid range, but we need to skip the overlapped region
0035      * between expanded range and original range, which is no need to add
0036      * the offset.
0037      */
0038     if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
0039         (rtc->start_secs < rtc->range_min &&
0040          secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
0041         return;
0042 
0043     rtc_time64_to_tm(secs + rtc->offset_secs, tm);
0044 }
0045 
0046 static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
0047 {
0048     time64_t secs;
0049 
0050     if (!rtc->offset_secs)
0051         return;
0052 
0053     secs = rtc_tm_to_time64(tm);
0054 
0055     /*
0056      * If the setting time values are in the valid range of RTC hardware
0057      * device, then no need to subtract the offset when setting time to RTC
0058      * device. Otherwise we need to subtract the offset to make the time
0059      * values are valid for RTC hardware device.
0060      */
0061     if (secs >= rtc->range_min && secs <= rtc->range_max)
0062         return;
0063 
0064     rtc_time64_to_tm(secs - rtc->offset_secs, tm);
0065 }
0066 
0067 static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
0068 {
0069     if (rtc->range_min != rtc->range_max) {
0070         time64_t time = rtc_tm_to_time64(tm);
0071         time64_t range_min = rtc->set_start_time ? rtc->start_secs :
0072             rtc->range_min;
0073         timeu64_t range_max = rtc->set_start_time ?
0074             (rtc->start_secs + rtc->range_max - rtc->range_min) :
0075             rtc->range_max;
0076 
0077         if (time < range_min || time > range_max)
0078             return -ERANGE;
0079     }
0080 
0081     return 0;
0082 }
0083 
0084 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
0085 {
0086     int err;
0087 
0088     if (!rtc->ops) {
0089         err = -ENODEV;
0090     } else if (!rtc->ops->read_time) {
0091         err = -EINVAL;
0092     } else {
0093         memset(tm, 0, sizeof(struct rtc_time));
0094         err = rtc->ops->read_time(rtc->dev.parent, tm);
0095         if (err < 0) {
0096             dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
0097                 err);
0098             return err;
0099         }
0100 
0101         rtc_add_offset(rtc, tm);
0102 
0103         err = rtc_valid_tm(tm);
0104         if (err < 0)
0105             dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
0106     }
0107     return err;
0108 }
0109 
0110 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
0111 {
0112     int err;
0113 
0114     err = mutex_lock_interruptible(&rtc->ops_lock);
0115     if (err)
0116         return err;
0117 
0118     err = __rtc_read_time(rtc, tm);
0119     mutex_unlock(&rtc->ops_lock);
0120 
0121     trace_rtc_read_time(rtc_tm_to_time64(tm), err);
0122     return err;
0123 }
0124 EXPORT_SYMBOL_GPL(rtc_read_time);
0125 
0126 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
0127 {
0128     int err, uie;
0129 
0130     err = rtc_valid_tm(tm);
0131     if (err != 0)
0132         return err;
0133 
0134     err = rtc_valid_range(rtc, tm);
0135     if (err)
0136         return err;
0137 
0138     rtc_subtract_offset(rtc, tm);
0139 
0140 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0141     uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
0142 #else
0143     uie = rtc->uie_rtctimer.enabled;
0144 #endif
0145     if (uie) {
0146         err = rtc_update_irq_enable(rtc, 0);
0147         if (err)
0148             return err;
0149     }
0150 
0151     err = mutex_lock_interruptible(&rtc->ops_lock);
0152     if (err)
0153         return err;
0154 
0155     if (!rtc->ops)
0156         err = -ENODEV;
0157     else if (rtc->ops->set_time)
0158         err = rtc->ops->set_time(rtc->dev.parent, tm);
0159     else
0160         err = -EINVAL;
0161 
0162     pm_stay_awake(rtc->dev.parent);
0163     mutex_unlock(&rtc->ops_lock);
0164     /* A timer might have just expired */
0165     schedule_work(&rtc->irqwork);
0166 
0167     if (uie) {
0168         err = rtc_update_irq_enable(rtc, 1);
0169         if (err)
0170             return err;
0171     }
0172 
0173     trace_rtc_set_time(rtc_tm_to_time64(tm), err);
0174     return err;
0175 }
0176 EXPORT_SYMBOL_GPL(rtc_set_time);
0177 
0178 static int rtc_read_alarm_internal(struct rtc_device *rtc,
0179                    struct rtc_wkalrm *alarm)
0180 {
0181     int err;
0182 
0183     err = mutex_lock_interruptible(&rtc->ops_lock);
0184     if (err)
0185         return err;
0186 
0187     if (!rtc->ops) {
0188         err = -ENODEV;
0189     } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) {
0190         err = -EINVAL;
0191     } else {
0192         alarm->enabled = 0;
0193         alarm->pending = 0;
0194         alarm->time.tm_sec = -1;
0195         alarm->time.tm_min = -1;
0196         alarm->time.tm_hour = -1;
0197         alarm->time.tm_mday = -1;
0198         alarm->time.tm_mon = -1;
0199         alarm->time.tm_year = -1;
0200         alarm->time.tm_wday = -1;
0201         alarm->time.tm_yday = -1;
0202         alarm->time.tm_isdst = -1;
0203         err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
0204     }
0205 
0206     mutex_unlock(&rtc->ops_lock);
0207 
0208     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
0209     return err;
0210 }
0211 
0212 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0213 {
0214     int err;
0215     struct rtc_time before, now;
0216     int first_time = 1;
0217     time64_t t_now, t_alm;
0218     enum { none, day, month, year } missing = none;
0219     unsigned int days;
0220 
0221     /* The lower level RTC driver may return -1 in some fields,
0222      * creating invalid alarm->time values, for reasons like:
0223      *
0224      *   - The hardware may not be capable of filling them in;
0225      *     many alarms match only on time-of-day fields, not
0226      *     day/month/year calendar data.
0227      *
0228      *   - Some hardware uses illegal values as "wildcard" match
0229      *     values, which non-Linux firmware (like a BIOS) may try
0230      *     to set up as e.g. "alarm 15 minutes after each hour".
0231      *     Linux uses only oneshot alarms.
0232      *
0233      * When we see that here, we deal with it by using values from
0234      * a current RTC timestamp for any missing (-1) values.  The
0235      * RTC driver prevents "periodic alarm" modes.
0236      *
0237      * But this can be racey, because some fields of the RTC timestamp
0238      * may have wrapped in the interval since we read the RTC alarm,
0239      * which would lead to us inserting inconsistent values in place
0240      * of the -1 fields.
0241      *
0242      * Reading the alarm and timestamp in the reverse sequence
0243      * would have the same race condition, and not solve the issue.
0244      *
0245      * So, we must first read the RTC timestamp,
0246      * then read the RTC alarm value,
0247      * and then read a second RTC timestamp.
0248      *
0249      * If any fields of the second timestamp have changed
0250      * when compared with the first timestamp, then we know
0251      * our timestamp may be inconsistent with that used by
0252      * the low-level rtc_read_alarm_internal() function.
0253      *
0254      * So, when the two timestamps disagree, we just loop and do
0255      * the process again to get a fully consistent set of values.
0256      *
0257      * This could all instead be done in the lower level driver,
0258      * but since more than one lower level RTC implementation needs it,
0259      * then it's probably best best to do it here instead of there..
0260      */
0261 
0262     /* Get the "before" timestamp */
0263     err = rtc_read_time(rtc, &before);
0264     if (err < 0)
0265         return err;
0266     do {
0267         if (!first_time)
0268             memcpy(&before, &now, sizeof(struct rtc_time));
0269         first_time = 0;
0270 
0271         /* get the RTC alarm values, which may be incomplete */
0272         err = rtc_read_alarm_internal(rtc, alarm);
0273         if (err)
0274             return err;
0275 
0276         /* full-function RTCs won't have such missing fields */
0277         if (rtc_valid_tm(&alarm->time) == 0) {
0278             rtc_add_offset(rtc, &alarm->time);
0279             return 0;
0280         }
0281 
0282         /* get the "after" timestamp, to detect wrapped fields */
0283         err = rtc_read_time(rtc, &now);
0284         if (err < 0)
0285             return err;
0286 
0287         /* note that tm_sec is a "don't care" value here: */
0288     } while (before.tm_min  != now.tm_min ||
0289          before.tm_hour != now.tm_hour ||
0290          before.tm_mon  != now.tm_mon ||
0291          before.tm_year != now.tm_year);
0292 
0293     /* Fill in the missing alarm fields using the timestamp; we
0294      * know there's at least one since alarm->time is invalid.
0295      */
0296     if (alarm->time.tm_sec == -1)
0297         alarm->time.tm_sec = now.tm_sec;
0298     if (alarm->time.tm_min == -1)
0299         alarm->time.tm_min = now.tm_min;
0300     if (alarm->time.tm_hour == -1)
0301         alarm->time.tm_hour = now.tm_hour;
0302 
0303     /* For simplicity, only support date rollover for now */
0304     if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
0305         alarm->time.tm_mday = now.tm_mday;
0306         missing = day;
0307     }
0308     if ((unsigned int)alarm->time.tm_mon >= 12) {
0309         alarm->time.tm_mon = now.tm_mon;
0310         if (missing == none)
0311             missing = month;
0312     }
0313     if (alarm->time.tm_year == -1) {
0314         alarm->time.tm_year = now.tm_year;
0315         if (missing == none)
0316             missing = year;
0317     }
0318 
0319     /* Can't proceed if alarm is still invalid after replacing
0320      * missing fields.
0321      */
0322     err = rtc_valid_tm(&alarm->time);
0323     if (err)
0324         goto done;
0325 
0326     /* with luck, no rollover is needed */
0327     t_now = rtc_tm_to_time64(&now);
0328     t_alm = rtc_tm_to_time64(&alarm->time);
0329     if (t_now < t_alm)
0330         goto done;
0331 
0332     switch (missing) {
0333     /* 24 hour rollover ... if it's now 10am Monday, an alarm that
0334      * that will trigger at 5am will do so at 5am Tuesday, which
0335      * could also be in the next month or year.  This is a common
0336      * case, especially for PCs.
0337      */
0338     case day:
0339         dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
0340         t_alm += 24 * 60 * 60;
0341         rtc_time64_to_tm(t_alm, &alarm->time);
0342         break;
0343 
0344     /* Month rollover ... if it's the 31th, an alarm on the 3rd will
0345      * be next month.  An alarm matching on the 30th, 29th, or 28th
0346      * may end up in the month after that!  Many newer PCs support
0347      * this type of alarm.
0348      */
0349     case month:
0350         dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
0351         do {
0352             if (alarm->time.tm_mon < 11) {
0353                 alarm->time.tm_mon++;
0354             } else {
0355                 alarm->time.tm_mon = 0;
0356                 alarm->time.tm_year++;
0357             }
0358             days = rtc_month_days(alarm->time.tm_mon,
0359                           alarm->time.tm_year);
0360         } while (days < alarm->time.tm_mday);
0361         break;
0362 
0363     /* Year rollover ... easy except for leap years! */
0364     case year:
0365         dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
0366         do {
0367             alarm->time.tm_year++;
0368         } while (!is_leap_year(alarm->time.tm_year + 1900) &&
0369              rtc_valid_tm(&alarm->time) != 0);
0370         break;
0371 
0372     default:
0373         dev_warn(&rtc->dev, "alarm rollover not handled\n");
0374     }
0375 
0376     err = rtc_valid_tm(&alarm->time);
0377 
0378 done:
0379     if (err)
0380         dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
0381              &alarm->time);
0382 
0383     return err;
0384 }
0385 
0386 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0387 {
0388     int err;
0389 
0390     err = mutex_lock_interruptible(&rtc->ops_lock);
0391     if (err)
0392         return err;
0393     if (!rtc->ops) {
0394         err = -ENODEV;
0395     } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) {
0396         err = -EINVAL;
0397     } else {
0398         memset(alarm, 0, sizeof(struct rtc_wkalrm));
0399         alarm->enabled = rtc->aie_timer.enabled;
0400         alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
0401     }
0402     mutex_unlock(&rtc->ops_lock);
0403 
0404     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
0405     return err;
0406 }
0407 EXPORT_SYMBOL_GPL(rtc_read_alarm);
0408 
0409 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0410 {
0411     struct rtc_time tm;
0412     time64_t now, scheduled;
0413     int err;
0414 
0415     err = rtc_valid_tm(&alarm->time);
0416     if (err)
0417         return err;
0418 
0419     scheduled = rtc_tm_to_time64(&alarm->time);
0420 
0421     /* Make sure we're not setting alarms in the past */
0422     err = __rtc_read_time(rtc, &tm);
0423     if (err)
0424         return err;
0425     now = rtc_tm_to_time64(&tm);
0426 
0427     if (scheduled <= now)
0428         return -ETIME;
0429     /*
0430      * XXX - We just checked to make sure the alarm time is not
0431      * in the past, but there is still a race window where if
0432      * the is alarm set for the next second and the second ticks
0433      * over right here, before we set the alarm.
0434      */
0435 
0436     rtc_subtract_offset(rtc, &alarm->time);
0437 
0438     if (!rtc->ops)
0439         err = -ENODEV;
0440     else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
0441         err = -EINVAL;
0442     else
0443         err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
0444 
0445     trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
0446     return err;
0447 }
0448 
0449 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0450 {
0451     ktime_t alarm_time;
0452     int err;
0453 
0454     if (!rtc->ops)
0455         return -ENODEV;
0456     else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
0457         return -EINVAL;
0458 
0459     err = rtc_valid_tm(&alarm->time);
0460     if (err != 0)
0461         return err;
0462 
0463     err = rtc_valid_range(rtc, &alarm->time);
0464     if (err)
0465         return err;
0466 
0467     err = mutex_lock_interruptible(&rtc->ops_lock);
0468     if (err)
0469         return err;
0470     if (rtc->aie_timer.enabled)
0471         rtc_timer_remove(rtc, &rtc->aie_timer);
0472 
0473     alarm_time = rtc_tm_to_ktime(alarm->time);
0474     /*
0475      * Round down so we never miss a deadline, checking for past deadline is
0476      * done in __rtc_set_alarm
0477      */
0478     if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features))
0479         alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC);
0480 
0481     rtc->aie_timer.node.expires = alarm_time;
0482     rtc->aie_timer.period = 0;
0483     if (alarm->enabled)
0484         err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
0485 
0486     mutex_unlock(&rtc->ops_lock);
0487 
0488     return err;
0489 }
0490 EXPORT_SYMBOL_GPL(rtc_set_alarm);
0491 
0492 /* Called once per device from rtc_device_register */
0493 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0494 {
0495     int err;
0496     struct rtc_time now;
0497 
0498     err = rtc_valid_tm(&alarm->time);
0499     if (err != 0)
0500         return err;
0501 
0502     err = rtc_read_time(rtc, &now);
0503     if (err)
0504         return err;
0505 
0506     err = mutex_lock_interruptible(&rtc->ops_lock);
0507     if (err)
0508         return err;
0509 
0510     rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
0511     rtc->aie_timer.period = 0;
0512 
0513     /* Alarm has to be enabled & in the future for us to enqueue it */
0514     if (alarm->enabled && (rtc_tm_to_ktime(now) <
0515              rtc->aie_timer.node.expires)) {
0516         rtc->aie_timer.enabled = 1;
0517         timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
0518         trace_rtc_timer_enqueue(&rtc->aie_timer);
0519     }
0520     mutex_unlock(&rtc->ops_lock);
0521     return err;
0522 }
0523 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
0524 
0525 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
0526 {
0527     int err;
0528 
0529     err = mutex_lock_interruptible(&rtc->ops_lock);
0530     if (err)
0531         return err;
0532 
0533     if (rtc->aie_timer.enabled != enabled) {
0534         if (enabled)
0535             err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
0536         else
0537             rtc_timer_remove(rtc, &rtc->aie_timer);
0538     }
0539 
0540     if (err)
0541         /* nothing */;
0542     else if (!rtc->ops)
0543         err = -ENODEV;
0544     else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
0545         err = -EINVAL;
0546     else
0547         err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
0548 
0549     mutex_unlock(&rtc->ops_lock);
0550 
0551     trace_rtc_alarm_irq_enable(enabled, err);
0552     return err;
0553 }
0554 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
0555 
0556 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
0557 {
0558     int err;
0559 
0560     err = mutex_lock_interruptible(&rtc->ops_lock);
0561     if (err)
0562         return err;
0563 
0564 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0565     if (enabled == 0 && rtc->uie_irq_active) {
0566         mutex_unlock(&rtc->ops_lock);
0567         return rtc_dev_update_irq_enable_emul(rtc, 0);
0568     }
0569 #endif
0570     /* make sure we're changing state */
0571     if (rtc->uie_rtctimer.enabled == enabled)
0572         goto out;
0573 
0574     if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) ||
0575         !test_bit(RTC_FEATURE_ALARM, rtc->features)) {
0576         mutex_unlock(&rtc->ops_lock);
0577 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0578         return rtc_dev_update_irq_enable_emul(rtc, enabled);
0579 #else
0580         return -EINVAL;
0581 #endif
0582     }
0583 
0584     if (enabled) {
0585         struct rtc_time tm;
0586         ktime_t now, onesec;
0587 
0588         err = __rtc_read_time(rtc, &tm);
0589         if (err)
0590             goto out;
0591         onesec = ktime_set(1, 0);
0592         now = rtc_tm_to_ktime(tm);
0593         rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
0594         rtc->uie_rtctimer.period = ktime_set(1, 0);
0595         err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
0596     } else {
0597         rtc_timer_remove(rtc, &rtc->uie_rtctimer);
0598     }
0599 
0600 out:
0601     mutex_unlock(&rtc->ops_lock);
0602 
0603     return err;
0604 }
0605 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
0606 
0607 /**
0608  * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
0609  * @rtc: pointer to the rtc device
0610  * @num: number of occurence of the event
0611  * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
0612  *
0613  * This function is called when an AIE, UIE or PIE mode interrupt
0614  * has occurred (or been emulated).
0615  *
0616  */
0617 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
0618 {
0619     unsigned long flags;
0620 
0621     /* mark one irq of the appropriate mode */
0622     spin_lock_irqsave(&rtc->irq_lock, flags);
0623     rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
0624     spin_unlock_irqrestore(&rtc->irq_lock, flags);
0625 
0626     wake_up_interruptible(&rtc->irq_queue);
0627     kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
0628 }
0629 
0630 /**
0631  * rtc_aie_update_irq - AIE mode rtctimer hook
0632  * @rtc: pointer to the rtc_device
0633  *
0634  * This functions is called when the aie_timer expires.
0635  */
0636 void rtc_aie_update_irq(struct rtc_device *rtc)
0637 {
0638     rtc_handle_legacy_irq(rtc, 1, RTC_AF);
0639 }
0640 
0641 /**
0642  * rtc_uie_update_irq - UIE mode rtctimer hook
0643  * @rtc: pointer to the rtc_device
0644  *
0645  * This functions is called when the uie_timer expires.
0646  */
0647 void rtc_uie_update_irq(struct rtc_device *rtc)
0648 {
0649     rtc_handle_legacy_irq(rtc, 1,  RTC_UF);
0650 }
0651 
0652 /**
0653  * rtc_pie_update_irq - PIE mode hrtimer hook
0654  * @timer: pointer to the pie mode hrtimer
0655  *
0656  * This function is used to emulate PIE mode interrupts
0657  * using an hrtimer. This function is called when the periodic
0658  * hrtimer expires.
0659  */
0660 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
0661 {
0662     struct rtc_device *rtc;
0663     ktime_t period;
0664     u64 count;
0665 
0666     rtc = container_of(timer, struct rtc_device, pie_timer);
0667 
0668     period = NSEC_PER_SEC / rtc->irq_freq;
0669     count = hrtimer_forward_now(timer, period);
0670 
0671     rtc_handle_legacy_irq(rtc, count, RTC_PF);
0672 
0673     return HRTIMER_RESTART;
0674 }
0675 
0676 /**
0677  * rtc_update_irq - Triggered when a RTC interrupt occurs.
0678  * @rtc: the rtc device
0679  * @num: how many irqs are being reported (usually one)
0680  * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
0681  * Context: any
0682  */
0683 void rtc_update_irq(struct rtc_device *rtc,
0684             unsigned long num, unsigned long events)
0685 {
0686     if (IS_ERR_OR_NULL(rtc))
0687         return;
0688 
0689     pm_stay_awake(rtc->dev.parent);
0690     schedule_work(&rtc->irqwork);
0691 }
0692 EXPORT_SYMBOL_GPL(rtc_update_irq);
0693 
0694 struct rtc_device *rtc_class_open(const char *name)
0695 {
0696     struct device *dev;
0697     struct rtc_device *rtc = NULL;
0698 
0699     dev = class_find_device_by_name(rtc_class, name);
0700     if (dev)
0701         rtc = to_rtc_device(dev);
0702 
0703     if (rtc) {
0704         if (!try_module_get(rtc->owner)) {
0705             put_device(dev);
0706             rtc = NULL;
0707         }
0708     }
0709 
0710     return rtc;
0711 }
0712 EXPORT_SYMBOL_GPL(rtc_class_open);
0713 
0714 void rtc_class_close(struct rtc_device *rtc)
0715 {
0716     module_put(rtc->owner);
0717     put_device(&rtc->dev);
0718 }
0719 EXPORT_SYMBOL_GPL(rtc_class_close);
0720 
0721 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
0722 {
0723     /*
0724      * We always cancel the timer here first, because otherwise
0725      * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
0726      * when we manage to start the timer before the callback
0727      * returns HRTIMER_RESTART.
0728      *
0729      * We cannot use hrtimer_cancel() here as a running callback
0730      * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
0731      * would spin forever.
0732      */
0733     if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
0734         return -1;
0735 
0736     if (enabled) {
0737         ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
0738 
0739         hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
0740     }
0741     return 0;
0742 }
0743 
0744 /**
0745  * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
0746  * @rtc: the rtc device
0747  * @enabled: true to enable periodic IRQs
0748  * Context: any
0749  *
0750  * Note that rtc_irq_set_freq() should previously have been used to
0751  * specify the desired frequency of periodic IRQ.
0752  */
0753 int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
0754 {
0755     int err = 0;
0756 
0757     while (rtc_update_hrtimer(rtc, enabled) < 0)
0758         cpu_relax();
0759 
0760     rtc->pie_enabled = enabled;
0761 
0762     trace_rtc_irq_set_state(enabled, err);
0763     return err;
0764 }
0765 
0766 /**
0767  * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
0768  * @rtc: the rtc device
0769  * @freq: positive frequency
0770  * Context: any
0771  *
0772  * Note that rtc_irq_set_state() is used to enable or disable the
0773  * periodic IRQs.
0774  */
0775 int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
0776 {
0777     int err = 0;
0778 
0779     if (freq <= 0 || freq > RTC_MAX_FREQ)
0780         return -EINVAL;
0781 
0782     rtc->irq_freq = freq;
0783     while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
0784         cpu_relax();
0785 
0786     trace_rtc_irq_set_freq(freq, err);
0787     return err;
0788 }
0789 
0790 /**
0791  * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
0792  * @rtc: rtc device
0793  * @timer: timer being added.
0794  *
0795  * Enqueues a timer onto the rtc devices timerqueue and sets
0796  * the next alarm event appropriately.
0797  *
0798  * Sets the enabled bit on the added timer.
0799  *
0800  * Must hold ops_lock for proper serialization of timerqueue
0801  */
0802 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
0803 {
0804     struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
0805     struct rtc_time tm;
0806     ktime_t now;
0807     int err;
0808 
0809     err = __rtc_read_time(rtc, &tm);
0810     if (err)
0811         return err;
0812 
0813     timer->enabled = 1;
0814     now = rtc_tm_to_ktime(tm);
0815 
0816     /* Skip over expired timers */
0817     while (next) {
0818         if (next->expires >= now)
0819             break;
0820         next = timerqueue_iterate_next(next);
0821     }
0822 
0823     timerqueue_add(&rtc->timerqueue, &timer->node);
0824     trace_rtc_timer_enqueue(timer);
0825     if (!next || ktime_before(timer->node.expires, next->expires)) {
0826         struct rtc_wkalrm alarm;
0827 
0828         alarm.time = rtc_ktime_to_tm(timer->node.expires);
0829         alarm.enabled = 1;
0830         err = __rtc_set_alarm(rtc, &alarm);
0831         if (err == -ETIME) {
0832             pm_stay_awake(rtc->dev.parent);
0833             schedule_work(&rtc->irqwork);
0834         } else if (err) {
0835             timerqueue_del(&rtc->timerqueue, &timer->node);
0836             trace_rtc_timer_dequeue(timer);
0837             timer->enabled = 0;
0838             return err;
0839         }
0840     }
0841     return 0;
0842 }
0843 
0844 static void rtc_alarm_disable(struct rtc_device *rtc)
0845 {
0846     if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
0847         return;
0848 
0849     rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
0850     trace_rtc_alarm_irq_enable(0, 0);
0851 }
0852 
0853 /**
0854  * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
0855  * @rtc: rtc device
0856  * @timer: timer being removed.
0857  *
0858  * Removes a timer onto the rtc devices timerqueue and sets
0859  * the next alarm event appropriately.
0860  *
0861  * Clears the enabled bit on the removed timer.
0862  *
0863  * Must hold ops_lock for proper serialization of timerqueue
0864  */
0865 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
0866 {
0867     struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
0868 
0869     timerqueue_del(&rtc->timerqueue, &timer->node);
0870     trace_rtc_timer_dequeue(timer);
0871     timer->enabled = 0;
0872     if (next == &timer->node) {
0873         struct rtc_wkalrm alarm;
0874         int err;
0875 
0876         next = timerqueue_getnext(&rtc->timerqueue);
0877         if (!next) {
0878             rtc_alarm_disable(rtc);
0879             return;
0880         }
0881         alarm.time = rtc_ktime_to_tm(next->expires);
0882         alarm.enabled = 1;
0883         err = __rtc_set_alarm(rtc, &alarm);
0884         if (err == -ETIME) {
0885             pm_stay_awake(rtc->dev.parent);
0886             schedule_work(&rtc->irqwork);
0887         }
0888     }
0889 }
0890 
0891 /**
0892  * rtc_timer_do_work - Expires rtc timers
0893  * @work: work item
0894  *
0895  * Expires rtc timers. Reprograms next alarm event if needed.
0896  * Called via worktask.
0897  *
0898  * Serializes access to timerqueue via ops_lock mutex
0899  */
0900 void rtc_timer_do_work(struct work_struct *work)
0901 {
0902     struct rtc_timer *timer;
0903     struct timerqueue_node *next;
0904     ktime_t now;
0905     struct rtc_time tm;
0906 
0907     struct rtc_device *rtc =
0908         container_of(work, struct rtc_device, irqwork);
0909 
0910     mutex_lock(&rtc->ops_lock);
0911 again:
0912     __rtc_read_time(rtc, &tm);
0913     now = rtc_tm_to_ktime(tm);
0914     while ((next = timerqueue_getnext(&rtc->timerqueue))) {
0915         if (next->expires > now)
0916             break;
0917 
0918         /* expire timer */
0919         timer = container_of(next, struct rtc_timer, node);
0920         timerqueue_del(&rtc->timerqueue, &timer->node);
0921         trace_rtc_timer_dequeue(timer);
0922         timer->enabled = 0;
0923         if (timer->func)
0924             timer->func(timer->rtc);
0925 
0926         trace_rtc_timer_fired(timer);
0927         /* Re-add/fwd periodic timers */
0928         if (ktime_to_ns(timer->period)) {
0929             timer->node.expires = ktime_add(timer->node.expires,
0930                             timer->period);
0931             timer->enabled = 1;
0932             timerqueue_add(&rtc->timerqueue, &timer->node);
0933             trace_rtc_timer_enqueue(timer);
0934         }
0935     }
0936 
0937     /* Set next alarm */
0938     if (next) {
0939         struct rtc_wkalrm alarm;
0940         int err;
0941         int retry = 3;
0942 
0943         alarm.time = rtc_ktime_to_tm(next->expires);
0944         alarm.enabled = 1;
0945 reprogram:
0946         err = __rtc_set_alarm(rtc, &alarm);
0947         if (err == -ETIME) {
0948             goto again;
0949         } else if (err) {
0950             if (retry-- > 0)
0951                 goto reprogram;
0952 
0953             timer = container_of(next, struct rtc_timer, node);
0954             timerqueue_del(&rtc->timerqueue, &timer->node);
0955             trace_rtc_timer_dequeue(timer);
0956             timer->enabled = 0;
0957             dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
0958             goto again;
0959         }
0960     } else {
0961         rtc_alarm_disable(rtc);
0962     }
0963 
0964     pm_relax(rtc->dev.parent);
0965     mutex_unlock(&rtc->ops_lock);
0966 }
0967 
0968 /* rtc_timer_init - Initializes an rtc_timer
0969  * @timer: timer to be intiialized
0970  * @f: function pointer to be called when timer fires
0971  * @rtc: pointer to the rtc_device
0972  *
0973  * Kernel interface to initializing an rtc_timer.
0974  */
0975 void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
0976             struct rtc_device *rtc)
0977 {
0978     timerqueue_init(&timer->node);
0979     timer->enabled = 0;
0980     timer->func = f;
0981     timer->rtc = rtc;
0982 }
0983 
0984 /* rtc_timer_start - Sets an rtc_timer to fire in the future
0985  * @ rtc: rtc device to be used
0986  * @ timer: timer being set
0987  * @ expires: time at which to expire the timer
0988  * @ period: period that the timer will recur
0989  *
0990  * Kernel interface to set an rtc_timer
0991  */
0992 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
0993             ktime_t expires, ktime_t period)
0994 {
0995     int ret = 0;
0996 
0997     mutex_lock(&rtc->ops_lock);
0998     if (timer->enabled)
0999         rtc_timer_remove(rtc, timer);
1000 
1001     timer->node.expires = expires;
1002     timer->period = period;
1003 
1004     ret = rtc_timer_enqueue(rtc, timer);
1005 
1006     mutex_unlock(&rtc->ops_lock);
1007     return ret;
1008 }
1009 
1010 /* rtc_timer_cancel - Stops an rtc_timer
1011  * @ rtc: rtc device to be used
1012  * @ timer: timer being set
1013  *
1014  * Kernel interface to cancel an rtc_timer
1015  */
1016 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1017 {
1018     mutex_lock(&rtc->ops_lock);
1019     if (timer->enabled)
1020         rtc_timer_remove(rtc, timer);
1021     mutex_unlock(&rtc->ops_lock);
1022 }
1023 
1024 /**
1025  * rtc_read_offset - Read the amount of rtc offset in parts per billion
1026  * @rtc: rtc device to be used
1027  * @offset: the offset in parts per billion
1028  *
1029  * see below for details.
1030  *
1031  * Kernel interface to read rtc clock offset
1032  * Returns 0 on success, or a negative number on error.
1033  * If read_offset() is not implemented for the rtc, return -EINVAL
1034  */
1035 int rtc_read_offset(struct rtc_device *rtc, long *offset)
1036 {
1037     int ret;
1038 
1039     if (!rtc->ops)
1040         return -ENODEV;
1041 
1042     if (!rtc->ops->read_offset)
1043         return -EINVAL;
1044 
1045     mutex_lock(&rtc->ops_lock);
1046     ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1047     mutex_unlock(&rtc->ops_lock);
1048 
1049     trace_rtc_read_offset(*offset, ret);
1050     return ret;
1051 }
1052 
1053 /**
1054  * rtc_set_offset - Adjusts the duration of the average second
1055  * @rtc: rtc device to be used
1056  * @offset: the offset in parts per billion
1057  *
1058  * Some rtc's allow an adjustment to the average duration of a second
1059  * to compensate for differences in the actual clock rate due to temperature,
1060  * the crystal, capacitor, etc.
1061  *
1062  * The adjustment applied is as follows:
1063  *   t = t0 * (1 + offset * 1e-9)
1064  * where t0 is the measured length of 1 RTC second with offset = 0
1065  *
1066  * Kernel interface to adjust an rtc clock offset.
1067  * Return 0 on success, or a negative number on error.
1068  * If the rtc offset is not setable (or not implemented), return -EINVAL
1069  */
1070 int rtc_set_offset(struct rtc_device *rtc, long offset)
1071 {
1072     int ret;
1073 
1074     if (!rtc->ops)
1075         return -ENODEV;
1076 
1077     if (!rtc->ops->set_offset)
1078         return -EINVAL;
1079 
1080     mutex_lock(&rtc->ops_lock);
1081     ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1082     mutex_unlock(&rtc->ops_lock);
1083 
1084     trace_rtc_set_offset(offset, ret);
1085     return ret;
1086 }