Lines Matching +full:close +full:- +full:range

1 // SPDX-License-Identifier: GPL-2.0
21 #include "rtc-core.h"
28 struct timerqueue_head *head = &rtc->timerqueue; in rtc_device_release()
31 mutex_lock(&rtc->ops_lock); in rtc_device_release()
34 mutex_unlock(&rtc->ops_lock); in rtc_device_release()
36 cancel_work_sync(&rtc->irqwork); in rtc_device_release()
38 ida_free(&rtc_ida, rtc->id); in rtc_device_release()
39 mutex_destroy(&rtc->ops_lock); in rtc_device_release()
45 int rtc_hctosys_ret = -ENODEV;
48 * whether it stores the most close value or the value with partial
53 * of >32bits is not possible. So storing the most close value would
68 dev_err(rtc->dev.parent, in rtc_hctosys()
77 err = -ERANGE; in rtc_hctosys()
84 dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n", in rtc_hctosys()
110 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) in rtc_suspend()
116 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev)); in rtc_suspend()
127 * and rtc time stays close to constant. in rtc_suspend()
131 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { in rtc_suspend()
156 rtc_hctosys_ret = -ENODEV; in rtc_resume()
157 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) in rtc_resume()
164 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev)); in rtc_resume()
172 pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); in rtc_resume()
182 * some run-time may pass on either sides of the sleep time in rtc_resume()
183 * so subtract kernel run-time between rtc_suspend to rtc_resume in rtc_resume()
215 device_initialize(&rtc->dev); in rtc_allocate_device()
221 * time of 5ms which is at least halfways close to reality. in rtc_allocate_device()
223 rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC; in rtc_allocate_device()
225 rtc->irq_freq = 1; in rtc_allocate_device()
226 rtc->max_user_freq = 64; in rtc_allocate_device()
227 rtc->dev.class = &rtc_class; in rtc_allocate_device()
228 rtc->dev.groups = rtc_get_dev_attribute_groups(); in rtc_allocate_device()
229 rtc->dev.release = rtc_device_release; in rtc_allocate_device()
231 mutex_init(&rtc->ops_lock); in rtc_allocate_device()
232 spin_lock_init(&rtc->irq_lock); in rtc_allocate_device()
233 init_waitqueue_head(&rtc->irq_queue); in rtc_allocate_device()
236 timerqueue_init_head(&rtc->timerqueue); in rtc_allocate_device()
237 INIT_WORK(&rtc->irqwork, rtc_timer_do_work); in rtc_allocate_device()
239 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc); in rtc_allocate_device()
241 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc); in rtc_allocate_device()
243 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in rtc_allocate_device()
244 rtc->pie_timer.function = rtc_pie_update_irq; in rtc_allocate_device()
245 rtc->pie_enabled = 0; in rtc_allocate_device()
247 set_bit(RTC_FEATURE_ALARM, rtc->features); in rtc_allocate_device()
248 set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features); in rtc_allocate_device()
255 int of_id = -1, id = -1; in rtc_device_get_id()
257 if (dev->of_node) in rtc_device_get_id()
258 of_id = of_alias_get_id(dev->of_node, "rtc"); in rtc_device_get_id()
259 else if (dev->parent && dev->parent->of_node) in rtc_device_get_id()
260 of_id = of_alias_get_id(dev->parent->of_node, "rtc"); in rtc_device_get_id()
281 * If RTC driver did not implement the range of RTC hardware device, in rtc_device_get_offset()
282 * then we can not expand the RTC range by adding or subtracting one in rtc_device_get_offset()
285 if (rtc->range_min == rtc->range_max) in rtc_device_get_offset()
288 ret = device_property_read_u32(rtc->dev.parent, "start-year", in rtc_device_get_offset()
291 rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0); in rtc_device_get_offset()
292 rtc->set_start_time = true; in rtc_device_get_offset()
297 * need to expand the RTC range. in rtc_device_get_offset()
299 if (!rtc->set_start_time) in rtc_device_get_offset()
302 range_secs = rtc->range_max - rtc->range_min + 1; in rtc_device_get_offset()
305 * If the start_secs is larger than the maximum seconds (rtc->range_max) in rtc_device_get_offset()
307 * range (start_secs + rtc->range_max - rtc->range_min) is less than in rtc_device_get_offset()
308 * rtc->range_min, which means the minimum seconds (rtc->range_min) of in rtc_device_get_offset()
311 * rtc->offset_secs = rtc->start_secs - rtc->range_min; in rtc_device_get_offset()
313 * If the start_secs is larger than the minimum seconds (rtc->range_min) in rtc_device_get_offset()
315 * between the original RTC hardware range and the new expanded range, in rtc_device_get_offset()
317 * expanded range due to it is valid for RTC device. So the minimum in rtc_device_get_offset()
318 * seconds of RTC hardware (rtc->range_min) should be mapped to in rtc_device_get_offset()
319 * rtc->range_max + 1, then the offset seconds formula should be: in rtc_device_get_offset()
320 * rtc->offset_secs = rtc->range_max - rtc->range_min + 1; in rtc_device_get_offset()
322 * If the start_secs is less than the minimum seconds (rtc->range_min), in rtc_device_get_offset()
324 * start_secs + rtc->range_max - rtc->range_min + 1, then the in rtc_device_get_offset()
326 * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1); in rtc_device_get_offset()
330 if (rtc->start_secs > rtc->range_max || in rtc_device_get_offset()
331 rtc->start_secs + range_secs - 1 < rtc->range_min) in rtc_device_get_offset()
332 rtc->offset_secs = rtc->start_secs - rtc->range_min; in rtc_device_get_offset()
333 else if (rtc->start_secs > rtc->range_min) in rtc_device_get_offset()
334 rtc->offset_secs = range_secs; in rtc_device_get_offset()
335 else if (rtc->start_secs < rtc->range_min) in rtc_device_get_offset()
336 rtc->offset_secs = -range_secs; in rtc_device_get_offset()
338 rtc->offset_secs = 0; in rtc_device_get_offset()
345 mutex_lock(&rtc->ops_lock); in devm_rtc_unregister_device()
351 if (!test_bit(RTC_NO_CDEV, &rtc->flags)) in devm_rtc_unregister_device()
352 cdev_device_del(&rtc->char_dev, &rtc->dev); in devm_rtc_unregister_device()
353 rtc->ops = NULL; in devm_rtc_unregister_device()
354 mutex_unlock(&rtc->ops_lock); in devm_rtc_unregister_device()
361 put_device(&rtc->dev); in devm_rtc_release_device()
376 return ERR_PTR(-ENOMEM); in devm_rtc_allocate_device()
379 rtc->id = id; in devm_rtc_allocate_device()
380 rtc->dev.parent = dev; in devm_rtc_allocate_device()
385 err = dev_set_name(&rtc->dev, "rtc%d", id); in devm_rtc_allocate_device()
398 if (!rtc->ops) { in __devm_rtc_register_device()
399 dev_dbg(&rtc->dev, "no ops set\n"); in __devm_rtc_register_device()
400 return -EINVAL; in __devm_rtc_register_device()
403 if (!rtc->ops->set_alarm) in __devm_rtc_register_device()
404 clear_bit(RTC_FEATURE_ALARM, rtc->features); in __devm_rtc_register_device()
406 if (rtc->ops->set_offset) in __devm_rtc_register_device()
407 set_bit(RTC_FEATURE_CORRECTION, rtc->features); in __devm_rtc_register_device()
409 rtc->owner = owner; in __devm_rtc_register_device()
419 err = cdev_device_add(&rtc->char_dev, &rtc->dev); in __devm_rtc_register_device()
421 set_bit(RTC_NO_CDEV, &rtc->flags); in __devm_rtc_register_device()
422 dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n", in __devm_rtc_register_device()
423 MAJOR(rtc->dev.devt), rtc->id); in __devm_rtc_register_device()
425 dev_dbg(rtc->dev.parent, "char device (%d:%d)\n", in __devm_rtc_register_device()
426 MAJOR(rtc->dev.devt), rtc->id); in __devm_rtc_register_device()
431 dev_info(rtc->dev.parent, "registered as %s\n", in __devm_rtc_register_device()
432 dev_name(&rtc->dev)); in __devm_rtc_register_device()
435 if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE)) in __devm_rtc_register_device()
439 return devm_add_action_or_reset(rtc->dev.parent, in __devm_rtc_register_device()
445 * devm_rtc_device_register - resource managed rtc_device_register()
470 rtc->ops = ops; in devm_rtc_device_register()