Lines Matching refs:rtc
15 #include <linux/rtc.h>
21 #include "rtc-core.h"
28 struct rtc_device *rtc = to_rtc_device(dev);
29 struct timerqueue_head *head = &rtc->timerqueue;
32 mutex_lock(&rtc->ops_lock);
35 mutex_unlock(&rtc->ops_lock);
37 cancel_work_sync(&rtc->irqwork);
39 ida_simple_remove(&rtc_ida, rtc->id);
40 kfree(rtc);
51 * in an rtc sync function, to read both xtime.tv_sec and
58 static void rtc_hctosys(struct rtc_device *rtc)
66 err = rtc_read_time(rtc, &tm);
68 dev_err(rtc->dev.parent,
84 dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
102 struct rtc_device *rtc = to_rtc_device(dev);
110 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
114 err = rtc_read_time(rtc, &tm);
116 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
127 * and rtc time stays close to constant.
147 struct rtc_device *rtc = to_rtc_device(dev);
157 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
160 /* snapshot the current rtc and system time at resume */
162 err = rtc_read_time(rtc, &tm);
164 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
172 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
204 struct rtc_device *rtc;
206 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
207 if (!rtc)
210 device_initialize(&rtc->dev);
213 rtc->set_offset_nsec = NSEC_PER_SEC / 2;
215 rtc->irq_freq = 1;
216 rtc->max_user_freq = 64;
217 rtc->dev.class = rtc_class;
218 rtc->dev.groups = rtc_get_dev_attribute_groups();
219 rtc->dev.release = rtc_device_release;
221 mutex_init(&rtc->ops_lock);
222 spin_lock_init(&rtc->irq_lock);
223 init_waitqueue_head(&rtc->irq_queue);
226 timerqueue_init_head(&rtc->timerqueue);
227 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
229 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
231 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
233 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
234 rtc->pie_timer.function = rtc_pie_update_irq;
235 rtc->pie_enabled = 0;
237 return rtc;
245 of_id = of_alias_get_id(dev->of_node, "rtc");
247 of_id = of_alias_get_id(dev->parent->of_node, "rtc");
261 static void rtc_device_get_offset(struct rtc_device *rtc)
272 if (rtc->range_min == rtc->range_max)
275 ret = device_property_read_u32(rtc->dev.parent, "start-year",
278 rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
279 rtc->set_start_time = true;
286 if (!rtc->set_start_time)
289 range_secs = rtc->range_max - rtc->range_min + 1;
292 * If the start_secs is larger than the maximum seconds (rtc->range_max)
294 * range (start_secs + rtc->range_max - rtc->range_min) is less than
295 * rtc->range_min, which means the minimum seconds (rtc->range_min) of
298 * rtc->offset_secs = rtc->start_secs - rtc->range_min;
300 * If the start_secs is larger than the minimum seconds (rtc->range_min)
305 * seconds of RTC hardware (rtc->range_min) should be mapped to
306 * rtc->range_max + 1, then the offset seconds formula should be:
307 * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
309 * If the start_secs is less than the minimum seconds (rtc->range_min),
311 * start_secs + rtc->range_max - rtc->range_min + 1, then the
313 * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
317 if (rtc->start_secs > rtc->range_max ||
318 rtc->start_secs + range_secs - 1 < rtc->range_min)
319 rtc->offset_secs = rtc->start_secs - rtc->range_min;
320 else if (rtc->start_secs > rtc->range_min)
321 rtc->offset_secs = range_secs;
322 else if (rtc->start_secs < rtc->range_min)
323 rtc->offset_secs = -range_secs;
325 rtc->offset_secs = 0;
331 * @rtc: the RTC class device to destroy
333 static void rtc_device_unregister(struct rtc_device *rtc)
335 mutex_lock(&rtc->ops_lock);
340 rtc_proc_del_device(rtc);
341 cdev_device_del(&rtc->char_dev, &rtc->dev);
342 rtc->ops = NULL;
343 mutex_unlock(&rtc->ops_lock);
344 put_device(&rtc->dev);
349 struct rtc_device *rtc = *(struct rtc_device **)res;
351 rtc_nvmem_unregister(rtc);
353 if (rtc->registered)
354 rtc_device_unregister(rtc);
356 put_device(&rtc->dev);
361 struct rtc_device **ptr, *rtc;
374 rtc = rtc_allocate_device();
375 if (!rtc) {
380 *ptr = rtc;
383 rtc->id = id;
384 rtc->dev.parent = dev;
385 dev_set_name(&rtc->dev, "rtc%d", id);
387 return rtc;
397 int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
402 if (!rtc->ops) {
403 dev_dbg(&rtc->dev, "no ops set\n");
407 rtc->owner = owner;
408 rtc_device_get_offset(rtc);
411 err = __rtc_read_alarm(rtc, &alrm);
413 rtc_initialize_alarm(rtc, &alrm);
415 rtc_dev_prepare(rtc);
417 err = cdev_device_add(&rtc->char_dev, &rtc->dev);
419 dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
420 MAJOR(rtc->dev.devt), rtc->id);
422 dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
423 MAJOR(rtc->dev.devt), rtc->id);
425 rtc_proc_add_device(rtc);
427 rtc->registered = true;
428 dev_info(rtc->dev.parent, "registered as %s\n",
429 dev_name(&rtc->dev));
432 if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
433 rtc_hctosys(rtc);
444 * @ops: the rtc operations structure
447 * @return a struct rtc on success, or an ERR_PTR on error
459 struct rtc_device *rtc;
462 rtc = devm_rtc_allocate_device(dev);
463 if (IS_ERR(rtc))
464 return rtc;
466 rtc->ops = ops;
468 err = __rtc_register_device(owner, rtc);
472 return rtc;
478 rtc_class = class_create(THIS_MODULE, "rtc");