0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/rtc.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/idr.h>
0018 #include <linux/slab.h>
0019 #include <linux/workqueue.h>
0020
0021 #include "rtc-core.h"
0022
0023 static DEFINE_IDA(rtc_ida);
0024 struct class *rtc_class;
0025
0026 static void rtc_device_release(struct device *dev)
0027 {
0028 struct rtc_device *rtc = to_rtc_device(dev);
0029 struct timerqueue_head *head = &rtc->timerqueue;
0030 struct timerqueue_node *node;
0031
0032 mutex_lock(&rtc->ops_lock);
0033 while ((node = timerqueue_getnext(head)))
0034 timerqueue_del(head, node);
0035 mutex_unlock(&rtc->ops_lock);
0036
0037 cancel_work_sync(&rtc->irqwork);
0038
0039 ida_free(&rtc_ida, rtc->id);
0040 mutex_destroy(&rtc->ops_lock);
0041 kfree(rtc);
0042 }
0043
0044 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0045
0046 int rtc_hctosys_ret = -ENODEV;
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 static void rtc_hctosys(struct rtc_device *rtc)
0060 {
0061 int err;
0062 struct rtc_time tm;
0063 struct timespec64 tv64 = {
0064 .tv_nsec = NSEC_PER_SEC >> 1,
0065 };
0066
0067 err = rtc_read_time(rtc, &tm);
0068 if (err) {
0069 dev_err(rtc->dev.parent,
0070 "hctosys: unable to read the hardware clock\n");
0071 goto err_read;
0072 }
0073
0074 tv64.tv_sec = rtc_tm_to_time64(&tm);
0075
0076 #if BITS_PER_LONG == 32
0077 if (tv64.tv_sec > INT_MAX) {
0078 err = -ERANGE;
0079 goto err_read;
0080 }
0081 #endif
0082
0083 err = do_settimeofday64(&tv64);
0084
0085 dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
0086 &tm, (long long)tv64.tv_sec);
0087
0088 err_read:
0089 rtc_hctosys_ret = err;
0090 }
0091 #endif
0092
0093 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
0094
0095
0096
0097
0098
0099 static struct timespec64 old_rtc, old_system, old_delta;
0100
0101 static int rtc_suspend(struct device *dev)
0102 {
0103 struct rtc_device *rtc = to_rtc_device(dev);
0104 struct rtc_time tm;
0105 struct timespec64 delta, delta_delta;
0106 int err;
0107
0108 if (timekeeping_rtc_skipsuspend())
0109 return 0;
0110
0111 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
0112 return 0;
0113
0114
0115 err = rtc_read_time(rtc, &tm);
0116 if (err < 0) {
0117 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
0118 return 0;
0119 }
0120
0121 ktime_get_real_ts64(&old_system);
0122 old_rtc.tv_sec = rtc_tm_to_time64(&tm);
0123
0124
0125
0126
0127
0128
0129
0130 delta = timespec64_sub(old_system, old_rtc);
0131 delta_delta = timespec64_sub(delta, old_delta);
0132 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
0133
0134
0135
0136
0137 old_delta = delta;
0138 } else {
0139
0140 old_system = timespec64_sub(old_system, delta_delta);
0141 }
0142
0143 return 0;
0144 }
0145
0146 static int rtc_resume(struct device *dev)
0147 {
0148 struct rtc_device *rtc = to_rtc_device(dev);
0149 struct rtc_time tm;
0150 struct timespec64 new_system, new_rtc;
0151 struct timespec64 sleep_time;
0152 int err;
0153
0154 if (timekeeping_rtc_skipresume())
0155 return 0;
0156
0157 rtc_hctosys_ret = -ENODEV;
0158 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
0159 return 0;
0160
0161
0162 ktime_get_real_ts64(&new_system);
0163 err = rtc_read_time(rtc, &tm);
0164 if (err < 0) {
0165 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
0166 return 0;
0167 }
0168
0169 new_rtc.tv_sec = rtc_tm_to_time64(&tm);
0170 new_rtc.tv_nsec = 0;
0171
0172 if (new_rtc.tv_sec < old_rtc.tv_sec) {
0173 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
0174 return 0;
0175 }
0176
0177
0178 sleep_time = timespec64_sub(new_rtc, old_rtc);
0179
0180
0181
0182
0183
0184
0185
0186
0187 sleep_time = timespec64_sub(sleep_time,
0188 timespec64_sub(new_system, old_system));
0189
0190 if (sleep_time.tv_sec >= 0)
0191 timekeeping_inject_sleeptime64(&sleep_time);
0192 rtc_hctosys_ret = 0;
0193 return 0;
0194 }
0195
0196 static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
0197 #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops)
0198 #else
0199 #define RTC_CLASS_DEV_PM_OPS NULL
0200 #endif
0201
0202
0203 static struct rtc_device *rtc_allocate_device(void)
0204 {
0205 struct rtc_device *rtc;
0206
0207 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
0208 if (!rtc)
0209 return NULL;
0210
0211 device_initialize(&rtc->dev);
0212
0213
0214
0215
0216
0217
0218
0219 rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC;
0220
0221 rtc->irq_freq = 1;
0222 rtc->max_user_freq = 64;
0223 rtc->dev.class = rtc_class;
0224 rtc->dev.groups = rtc_get_dev_attribute_groups();
0225 rtc->dev.release = rtc_device_release;
0226
0227 mutex_init(&rtc->ops_lock);
0228 spin_lock_init(&rtc->irq_lock);
0229 init_waitqueue_head(&rtc->irq_queue);
0230
0231
0232 timerqueue_init_head(&rtc->timerqueue);
0233 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
0234
0235 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
0236
0237 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
0238
0239 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0240 rtc->pie_timer.function = rtc_pie_update_irq;
0241 rtc->pie_enabled = 0;
0242
0243 set_bit(RTC_FEATURE_ALARM, rtc->features);
0244 set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
0245
0246 return rtc;
0247 }
0248
0249 static int rtc_device_get_id(struct device *dev)
0250 {
0251 int of_id = -1, id = -1;
0252
0253 if (dev->of_node)
0254 of_id = of_alias_get_id(dev->of_node, "rtc");
0255 else if (dev->parent && dev->parent->of_node)
0256 of_id = of_alias_get_id(dev->parent->of_node, "rtc");
0257
0258 if (of_id >= 0) {
0259 id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
0260 if (id < 0)
0261 dev_warn(dev, "/aliases ID %d not available\n", of_id);
0262 }
0263
0264 if (id < 0)
0265 id = ida_alloc(&rtc_ida, GFP_KERNEL);
0266
0267 return id;
0268 }
0269
0270 static void rtc_device_get_offset(struct rtc_device *rtc)
0271 {
0272 time64_t range_secs;
0273 u32 start_year;
0274 int ret;
0275
0276
0277
0278
0279
0280
0281 if (rtc->range_min == rtc->range_max)
0282 return;
0283
0284 ret = device_property_read_u32(rtc->dev.parent, "start-year",
0285 &start_year);
0286 if (!ret) {
0287 rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
0288 rtc->set_start_time = true;
0289 }
0290
0291
0292
0293
0294
0295 if (!rtc->set_start_time)
0296 return;
0297
0298 range_secs = rtc->range_max - rtc->range_min + 1;
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326 if (rtc->start_secs > rtc->range_max ||
0327 rtc->start_secs + range_secs - 1 < rtc->range_min)
0328 rtc->offset_secs = rtc->start_secs - rtc->range_min;
0329 else if (rtc->start_secs > rtc->range_min)
0330 rtc->offset_secs = range_secs;
0331 else if (rtc->start_secs < rtc->range_min)
0332 rtc->offset_secs = -range_secs;
0333 else
0334 rtc->offset_secs = 0;
0335 }
0336
0337 static void devm_rtc_unregister_device(void *data)
0338 {
0339 struct rtc_device *rtc = data;
0340
0341 mutex_lock(&rtc->ops_lock);
0342
0343
0344
0345
0346 rtc_proc_del_device(rtc);
0347 if (!test_bit(RTC_NO_CDEV, &rtc->flags))
0348 cdev_device_del(&rtc->char_dev, &rtc->dev);
0349 rtc->ops = NULL;
0350 mutex_unlock(&rtc->ops_lock);
0351 }
0352
0353 static void devm_rtc_release_device(void *res)
0354 {
0355 struct rtc_device *rtc = res;
0356
0357 put_device(&rtc->dev);
0358 }
0359
0360 struct rtc_device *devm_rtc_allocate_device(struct device *dev)
0361 {
0362 struct rtc_device *rtc;
0363 int id, err;
0364
0365 id = rtc_device_get_id(dev);
0366 if (id < 0)
0367 return ERR_PTR(id);
0368
0369 rtc = rtc_allocate_device();
0370 if (!rtc) {
0371 ida_free(&rtc_ida, id);
0372 return ERR_PTR(-ENOMEM);
0373 }
0374
0375 rtc->id = id;
0376 rtc->dev.parent = dev;
0377 err = dev_set_name(&rtc->dev, "rtc%d", id);
0378 if (err)
0379 return ERR_PTR(err);
0380
0381 err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
0382 if (err)
0383 return ERR_PTR(err);
0384
0385 return rtc;
0386 }
0387 EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
0388
0389 int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
0390 {
0391 struct rtc_wkalrm alrm;
0392 int err;
0393
0394 if (!rtc->ops) {
0395 dev_dbg(&rtc->dev, "no ops set\n");
0396 return -EINVAL;
0397 }
0398
0399 if (!rtc->ops->set_alarm)
0400 clear_bit(RTC_FEATURE_ALARM, rtc->features);
0401
0402 if (rtc->ops->set_offset)
0403 set_bit(RTC_FEATURE_CORRECTION, rtc->features);
0404
0405 rtc->owner = owner;
0406 rtc_device_get_offset(rtc);
0407
0408
0409 err = __rtc_read_alarm(rtc, &alrm);
0410 if (!err && !rtc_valid_tm(&alrm.time))
0411 rtc_initialize_alarm(rtc, &alrm);
0412
0413 rtc_dev_prepare(rtc);
0414
0415 err = cdev_device_add(&rtc->char_dev, &rtc->dev);
0416 if (err) {
0417 set_bit(RTC_NO_CDEV, &rtc->flags);
0418 dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
0419 MAJOR(rtc->dev.devt), rtc->id);
0420 } else {
0421 dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
0422 MAJOR(rtc->dev.devt), rtc->id);
0423 }
0424
0425 rtc_proc_add_device(rtc);
0426
0427 dev_info(rtc->dev.parent, "registered as %s\n",
0428 dev_name(&rtc->dev));
0429
0430 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0431 if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
0432 rtc_hctosys(rtc);
0433 #endif
0434
0435 return devm_add_action_or_reset(rtc->dev.parent,
0436 devm_rtc_unregister_device, rtc);
0437 }
0438 EXPORT_SYMBOL_GPL(__devm_rtc_register_device);
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454 struct rtc_device *devm_rtc_device_register(struct device *dev,
0455 const char *name,
0456 const struct rtc_class_ops *ops,
0457 struct module *owner)
0458 {
0459 struct rtc_device *rtc;
0460 int err;
0461
0462 rtc = devm_rtc_allocate_device(dev);
0463 if (IS_ERR(rtc))
0464 return rtc;
0465
0466 rtc->ops = ops;
0467
0468 err = __devm_rtc_register_device(owner, rtc);
0469 if (err)
0470 return ERR_PTR(err);
0471
0472 return rtc;
0473 }
0474 EXPORT_SYMBOL_GPL(devm_rtc_device_register);
0475
0476 static int __init rtc_init(void)
0477 {
0478 rtc_class = class_create(THIS_MODULE, "rtc");
0479 if (IS_ERR(rtc_class)) {
0480 pr_err("couldn't create class\n");
0481 return PTR_ERR(rtc_class);
0482 }
0483 rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
0484 rtc_dev_init();
0485 return 0;
0486 }
0487 subsys_initcall(rtc_init);