0001
0002
0003
0004
0005
0006
0007
0008
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
0034
0035
0036
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
0057
0058
0059
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
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
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
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
0272 err = rtc_read_alarm_internal(rtc, alarm);
0273 if (err)
0274 return err;
0275
0276
0277 if (rtc_valid_tm(&alarm->time) == 0) {
0278 rtc_add_offset(rtc, &alarm->time);
0279 return 0;
0280 }
0281
0282
0283 err = rtc_read_time(rtc, &now);
0284 if (err < 0)
0285 return err;
0286
0287
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
0294
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
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
0320
0321
0322 err = rtc_valid_tm(&alarm->time);
0323 if (err)
0324 goto done;
0325
0326
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
0334
0335
0336
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
0345
0346
0347
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
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
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
0431
0432
0433
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
0476
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
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
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 ;
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
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
0609
0610
0611
0612
0613
0614
0615
0616
0617 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
0618 {
0619 unsigned long flags;
0620
0621
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
0632
0633
0634
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
0643
0644
0645
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
0654
0655
0656
0657
0658
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
0678
0679
0680
0681
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
0725
0726
0727
0728
0729
0730
0731
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
0746
0747
0748
0749
0750
0751
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
0768
0769
0770
0771
0772
0773
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
0792
0793
0794
0795
0796
0797
0798
0799
0800
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
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
0855
0856
0857
0858
0859
0860
0861
0862
0863
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
0893
0894
0895
0896
0897
0898
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
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
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
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
0969
0970
0971
0972
0973
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
0985
0986
0987
0988
0989
0990
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
1011
1012
1013
1014
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
1026
1027
1028
1029
1030
1031
1032
1033
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
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
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 }