.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * RTC subsystem, base class |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
---|
6 | 7 | * |
---|
7 | 8 | * 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 | + */ |
---|
13 | 10 | |
---|
14 | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
---|
15 | 12 | |
---|
.. | .. |
---|
23 | 20 | |
---|
24 | 21 | #include "rtc-core.h" |
---|
25 | 22 | |
---|
26 | | - |
---|
27 | 23 | static DEFINE_IDA(rtc_ida); |
---|
28 | 24 | struct class *rtc_class; |
---|
29 | 25 | |
---|
30 | 26 | static void rtc_device_release(struct device *dev) |
---|
31 | 27 | { |
---|
32 | 28 | 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 | + |
---|
33 | 39 | ida_simple_remove(&rtc_ida, rtc->id); |
---|
34 | 40 | kfree(rtc); |
---|
35 | 41 | } |
---|
.. | .. |
---|
37 | 43 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
---|
38 | 44 | /* Result of the last RTC to system clock attempt. */ |
---|
39 | 45 | 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 | +} |
---|
40 | 90 | #endif |
---|
41 | 91 | |
---|
42 | 92 | #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) |
---|
.. | .. |
---|
46 | 96 | */ |
---|
47 | 97 | |
---|
48 | 98 | static struct timespec64 old_rtc, old_system, old_delta; |
---|
49 | | - |
---|
50 | 99 | |
---|
51 | 100 | static int rtc_suspend(struct device *dev) |
---|
52 | 101 | { |
---|
.. | .. |
---|
71 | 120 | ktime_get_real_ts64(&old_system); |
---|
72 | 121 | old_rtc.tv_sec = rtc_tm_to_time64(&tm); |
---|
73 | 122 | |
---|
74 | | - |
---|
75 | 123 | /* |
---|
76 | 124 | * To avoid drift caused by repeated suspend/resumes, |
---|
77 | 125 | * which each can add ~1 second drift error, |
---|
.. | .. |
---|
83 | 131 | if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { |
---|
84 | 132 | /* |
---|
85 | 133 | * 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. |
---|
87 | 135 | */ |
---|
88 | 136 | old_delta = delta; |
---|
89 | 137 | } else { |
---|
.. | .. |
---|
136 | 184 | * to keep things accurate. |
---|
137 | 185 | */ |
---|
138 | 186 | sleep_time = timespec64_sub(sleep_time, |
---|
139 | | - timespec64_sub(new_system, old_system)); |
---|
| 187 | + timespec64_sub(new_system, old_system)); |
---|
140 | 188 | |
---|
141 | 189 | if (sleep_time.tv_sec >= 0) |
---|
142 | 190 | timekeeping_inject_sleeptime64(&sleep_time); |
---|
.. | .. |
---|
178 | 226 | timerqueue_init_head(&rtc->timerqueue); |
---|
179 | 227 | INIT_WORK(&rtc->irqwork, rtc_timer_do_work); |
---|
180 | 228 | /* 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); |
---|
182 | 230 | /* 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); |
---|
184 | 232 | /* Init pie timer */ |
---|
185 | 233 | hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
---|
186 | 234 | rtc->pie_timer.function = rtc_pie_update_irq; |
---|
.. | .. |
---|
278 | 326 | } |
---|
279 | 327 | |
---|
280 | 328 | /** |
---|
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 | | -/** |
---|
363 | 329 | * rtc_device_unregister - removes the previously registered RTC class device |
---|
364 | 330 | * |
---|
365 | 331 | * @rtc: the RTC class device to destroy |
---|
366 | 332 | */ |
---|
367 | | -void rtc_device_unregister(struct rtc_device *rtc) |
---|
| 333 | +static void rtc_device_unregister(struct rtc_device *rtc) |
---|
368 | 334 | { |
---|
369 | 335 | mutex_lock(&rtc->ops_lock); |
---|
370 | 336 | /* |
---|
.. | .. |
---|
377 | 343 | mutex_unlock(&rtc->ops_lock); |
---|
378 | 344 | put_device(&rtc->dev); |
---|
379 | 345 | } |
---|
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); |
---|
452 | 346 | |
---|
453 | 347 | static void devm_rtc_release_device(struct device *dev, void *res) |
---|
454 | 348 | { |
---|
.. | .. |
---|
505 | 399 | struct rtc_wkalrm alrm; |
---|
506 | 400 | int err; |
---|
507 | 401 | |
---|
508 | | - if (!rtc->ops) |
---|
| 402 | + if (!rtc->ops) { |
---|
| 403 | + dev_dbg(&rtc->dev, "no ops set\n"); |
---|
509 | 404 | return -EINVAL; |
---|
| 405 | + } |
---|
510 | 406 | |
---|
511 | 407 | rtc->owner = owner; |
---|
512 | 408 | rtc_device_get_offset(rtc); |
---|
.. | .. |
---|
534 | 430 | |
---|
535 | 431 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
---|
536 | 432 | if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE)) |
---|
537 | | - rtc_hctosys(); |
---|
| 433 | + rtc_hctosys(rtc); |
---|
538 | 434 | #endif |
---|
539 | 435 | |
---|
540 | 436 | return 0; |
---|
541 | 437 | } |
---|
542 | 438 | EXPORT_SYMBOL_GPL(__rtc_register_device); |
---|
543 | 439 | |
---|
| 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 | + |
---|
544 | 476 | static int __init rtc_init(void) |
---|
545 | 477 | { |
---|
546 | 478 | rtc_class = class_create(THIS_MODULE, "rtc"); |
---|