hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/rtc/class.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * RTC subsystem, base class
34 *
....@@ -5,11 +6,7 @@
56 * Author: Alessandro Zummo <a.zummo@towertech.it>
67 *
78 * class skeleton from drivers/hwmon/hwmon.c
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2 as
11
- * published by the Free Software Foundation.
12
-*/
9
+ */
1310
1411 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1512
....@@ -23,13 +20,22 @@
2320
2421 #include "rtc-core.h"
2522
26
-
2723 static DEFINE_IDA(rtc_ida);
2824 struct class *rtc_class;
2925
3026 static void rtc_device_release(struct device *dev)
3127 {
3228 struct rtc_device *rtc = to_rtc_device(dev);
29
+ struct timerqueue_head *head = &rtc->timerqueue;
30
+ struct timerqueue_node *node;
31
+
32
+ mutex_lock(&rtc->ops_lock);
33
+ while ((node = timerqueue_getnext(head)))
34
+ timerqueue_del(head, node);
35
+ mutex_unlock(&rtc->ops_lock);
36
+
37
+ cancel_work_sync(&rtc->irqwork);
38
+
3339 ida_simple_remove(&rtc_ida, rtc->id);
3440 kfree(rtc);
3541 }
....@@ -37,6 +43,50 @@
3743 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
3844 /* Result of the last RTC to system clock attempt. */
3945 int rtc_hctosys_ret = -ENODEV;
46
+
47
+/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
48
+ * whether it stores the most close value or the value with partial
49
+ * seconds truncated. However, it is important that we use it to store
50
+ * the truncated value. This is because otherwise it is necessary,
51
+ * in an rtc sync function, to read both xtime.tv_sec and
52
+ * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
53
+ * of >32bits is not possible. So storing the most close value would
54
+ * slow down the sync API. So here we have the truncated value and
55
+ * the best guess is to add 0.5s.
56
+ */
57
+
58
+static void rtc_hctosys(struct rtc_device *rtc)
59
+{
60
+ int err;
61
+ struct rtc_time tm;
62
+ struct timespec64 tv64 = {
63
+ .tv_nsec = NSEC_PER_SEC >> 1,
64
+ };
65
+
66
+ err = rtc_read_time(rtc, &tm);
67
+ if (err) {
68
+ dev_err(rtc->dev.parent,
69
+ "hctosys: unable to read the hardware clock\n");
70
+ goto err_read;
71
+ }
72
+
73
+ tv64.tv_sec = rtc_tm_to_time64(&tm);
74
+
75
+#if BITS_PER_LONG == 32
76
+ if (tv64.tv_sec > INT_MAX) {
77
+ err = -ERANGE;
78
+ goto err_read;
79
+ }
80
+#endif
81
+
82
+ err = do_settimeofday64(&tv64);
83
+
84
+ dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
85
+ &tm, (long long)tv64.tv_sec);
86
+
87
+err_read:
88
+ rtc_hctosys_ret = err;
89
+}
4090 #endif
4191
4292 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
....@@ -46,7 +96,6 @@
4696 */
4797
4898 static struct timespec64 old_rtc, old_system, old_delta;
49
-
5099
51100 static int rtc_suspend(struct device *dev)
52101 {
....@@ -71,7 +120,6 @@
71120 ktime_get_real_ts64(&old_system);
72121 old_rtc.tv_sec = rtc_tm_to_time64(&tm);
73122
74
-
75123 /*
76124 * To avoid drift caused by repeated suspend/resumes,
77125 * which each can add ~1 second drift error,
....@@ -83,7 +131,7 @@
83131 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
84132 /*
85133 * if delta_delta is too large, assume time correction
86
- * has occured and set old_delta to the current delta.
134
+ * has occurred and set old_delta to the current delta.
87135 */
88136 old_delta = delta;
89137 } else {
....@@ -136,7 +184,7 @@
136184 * to keep things accurate.
137185 */
138186 sleep_time = timespec64_sub(sleep_time,
139
- timespec64_sub(new_system, old_system));
187
+ timespec64_sub(new_system, old_system));
140188
141189 if (sleep_time.tv_sec >= 0)
142190 timekeeping_inject_sleeptime64(&sleep_time);
....@@ -178,9 +226,9 @@
178226 timerqueue_init_head(&rtc->timerqueue);
179227 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
180228 /* Init aie timer */
181
- rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
229
+ rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
182230 /* Init uie timer */
183
- rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
231
+ rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
184232 /* Init pie timer */
185233 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
186234 rtc->pie_timer.function = rtc_pie_update_irq;
....@@ -278,93 +326,11 @@
278326 }
279327
280328 /**
281
- * rtc_device_register - register w/ RTC class
282
- * @dev: the device to register
283
- *
284
- * rtc_device_unregister() must be called when the class device is no
285
- * longer needed.
286
- *
287
- * Returns the pointer to the new struct class device.
288
- */
289
-struct rtc_device *rtc_device_register(const char *name, struct device *dev,
290
- const struct rtc_class_ops *ops,
291
- struct module *owner)
292
-{
293
- struct rtc_device *rtc;
294
- struct rtc_wkalrm alrm;
295
- int id, err;
296
-
297
- id = rtc_device_get_id(dev);
298
- if (id < 0) {
299
- err = id;
300
- goto exit;
301
- }
302
-
303
- rtc = rtc_allocate_device();
304
- if (!rtc) {
305
- err = -ENOMEM;
306
- goto exit_ida;
307
- }
308
-
309
- rtc->id = id;
310
- rtc->ops = ops;
311
- rtc->owner = owner;
312
- rtc->dev.parent = dev;
313
-
314
- dev_set_name(&rtc->dev, "rtc%d", id);
315
-
316
- rtc_device_get_offset(rtc);
317
-
318
- /* Check to see if there is an ALARM already set in hw */
319
- err = __rtc_read_alarm(rtc, &alrm);
320
-
321
- if (!err && !rtc_valid_tm(&alrm.time))
322
- rtc_initialize_alarm(rtc, &alrm);
323
-
324
- rtc_dev_prepare(rtc);
325
-
326
- err = cdev_device_add(&rtc->char_dev, &rtc->dev);
327
- if (err) {
328
- dev_warn(&rtc->dev, "%s: failed to add char device %d:%d\n",
329
- name, MAJOR(rtc->dev.devt), rtc->id);
330
-
331
- /* This will free both memory and the ID */
332
- put_device(&rtc->dev);
333
- goto exit;
334
- } else {
335
- dev_dbg(&rtc->dev, "%s: dev (%d:%d)\n", name,
336
- MAJOR(rtc->dev.devt), rtc->id);
337
- }
338
-
339
- rtc_proc_add_device(rtc);
340
-
341
- dev_info(dev, "rtc core: registered %s as %s\n",
342
- name, dev_name(&rtc->dev));
343
-
344
-#ifdef CONFIG_RTC_HCTOSYS_DEVICE
345
- if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
346
- rtc_hctosys();
347
-#endif
348
-
349
- return rtc;
350
-
351
-exit_ida:
352
- ida_simple_remove(&rtc_ida, id);
353
-
354
-exit:
355
- dev_err(dev, "rtc core: unable to register %s, err = %d\n",
356
- name, err);
357
- return ERR_PTR(err);
358
-}
359
-EXPORT_SYMBOL_GPL(rtc_device_register);
360
-
361
-
362
-/**
363329 * rtc_device_unregister - removes the previously registered RTC class device
364330 *
365331 * @rtc: the RTC class device to destroy
366332 */
367
-void rtc_device_unregister(struct rtc_device *rtc)
333
+static void rtc_device_unregister(struct rtc_device *rtc)
368334 {
369335 mutex_lock(&rtc->ops_lock);
370336 /*
....@@ -377,78 +343,6 @@
377343 mutex_unlock(&rtc->ops_lock);
378344 put_device(&rtc->dev);
379345 }
380
-EXPORT_SYMBOL_GPL(rtc_device_unregister);
381
-
382
-static void devm_rtc_device_release(struct device *dev, void *res)
383
-{
384
- struct rtc_device *rtc = *(struct rtc_device **)res;
385
-
386
- rtc_nvmem_unregister(rtc);
387
- rtc_device_unregister(rtc);
388
-}
389
-
390
-static int devm_rtc_device_match(struct device *dev, void *res, void *data)
391
-{
392
- struct rtc **r = res;
393
-
394
- return *r == data;
395
-}
396
-
397
-/**
398
- * devm_rtc_device_register - resource managed rtc_device_register()
399
- * @dev: the device to register
400
- * @name: the name of the device
401
- * @ops: the rtc operations structure
402
- * @owner: the module owner
403
- *
404
- * @return a struct rtc on success, or an ERR_PTR on error
405
- *
406
- * Managed rtc_device_register(). The rtc_device returned from this function
407
- * are automatically freed on driver detach. See rtc_device_register()
408
- * for more information.
409
- */
410
-
411
-struct rtc_device *devm_rtc_device_register(struct device *dev,
412
- const char *name,
413
- const struct rtc_class_ops *ops,
414
- struct module *owner)
415
-{
416
- struct rtc_device **ptr, *rtc;
417
-
418
- ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
419
- if (!ptr)
420
- return ERR_PTR(-ENOMEM);
421
-
422
- rtc = rtc_device_register(name, dev, ops, owner);
423
- if (!IS_ERR(rtc)) {
424
- *ptr = rtc;
425
- devres_add(dev, ptr);
426
- } else {
427
- devres_free(ptr);
428
- }
429
-
430
- return rtc;
431
-}
432
-EXPORT_SYMBOL_GPL(devm_rtc_device_register);
433
-
434
-/**
435
- * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
436
- * @dev: the device to unregister
437
- * @rtc: the RTC class device to unregister
438
- *
439
- * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
440
- * function will not need to be called and the resource management code will
441
- * ensure that the resource is freed.
442
- */
443
-void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
444
-{
445
- int rc;
446
-
447
- rc = devres_release(dev, devm_rtc_device_release,
448
- devm_rtc_device_match, rtc);
449
- WARN_ON(rc);
450
-}
451
-EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
452346
453347 static void devm_rtc_release_device(struct device *dev, void *res)
454348 {
....@@ -505,8 +399,10 @@
505399 struct rtc_wkalrm alrm;
506400 int err;
507401
508
- if (!rtc->ops)
402
+ if (!rtc->ops) {
403
+ dev_dbg(&rtc->dev, "no ops set\n");
509404 return -EINVAL;
405
+ }
510406
511407 rtc->owner = owner;
512408 rtc_device_get_offset(rtc);
....@@ -534,13 +430,49 @@
534430
535431 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
536432 if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
537
- rtc_hctosys();
433
+ rtc_hctosys(rtc);
538434 #endif
539435
540436 return 0;
541437 }
542438 EXPORT_SYMBOL_GPL(__rtc_register_device);
543439
440
+/**
441
+ * devm_rtc_device_register - resource managed rtc_device_register()
442
+ * @dev: the device to register
443
+ * @name: the name of the device (unused)
444
+ * @ops: the rtc operations structure
445
+ * @owner: the module owner
446
+ *
447
+ * @return a struct rtc on success, or an ERR_PTR on error
448
+ *
449
+ * Managed rtc_device_register(). The rtc_device returned from this function
450
+ * are automatically freed on driver detach.
451
+ * This function is deprecated, use devm_rtc_allocate_device and
452
+ * rtc_register_device instead
453
+ */
454
+struct rtc_device *devm_rtc_device_register(struct device *dev,
455
+ const char *name,
456
+ const struct rtc_class_ops *ops,
457
+ struct module *owner)
458
+{
459
+ struct rtc_device *rtc;
460
+ int err;
461
+
462
+ rtc = devm_rtc_allocate_device(dev);
463
+ if (IS_ERR(rtc))
464
+ return rtc;
465
+
466
+ rtc->ops = ops;
467
+
468
+ err = __rtc_register_device(owner, rtc);
469
+ if (err)
470
+ return ERR_PTR(err);
471
+
472
+ return rtc;
473
+}
474
+EXPORT_SYMBOL_GPL(devm_rtc_device_register);
475
+
544476 static int __init rtc_init(void)
545477 {
546478 rtc_class = class_create(THIS_MODULE, "rtc");