/*
* RTC subsystem, interface functions
*
* Copyright (C) 2005 Tower Technologies
* Author: Alessandro Zummo <a.zummo@towertech.it>
*
* based on arch/arm/common/rtctime.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/rtc.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/log2.h>
#include <linux/workqueue.h>
#define CREATE_TRACE_POINTS
#include <trace/events/rtc.h>
static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
{
time64_t secs;
if (!rtc->offset_secs)
return;
secs = rtc_tm_to_time64(tm);
/*
* Since the reading time values from RTC device are always in the RTC
* original valid range, but we need to skip the overlapped region
* between expanded range and original range, which is no need to add
* the offset.
*/
if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
(rtc->start_secs < rtc->range_min &&
secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
return;
rtc_time64_to_tm(secs + rtc->offset_secs, tm);
}
static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
{
time64_t secs;
if (!rtc->offset_secs)
return;
secs = rtc_tm_to_time64(tm);
/*
* If the setting time values are in the valid range of RTC hardware
* device, then no need to subtract the offset when setting time to RTC
* device. Otherwise we need to subtract the offset to make the time
* values are valid for RTC hardware device.
*/
if (secs >= rtc->range_min && secs <= rtc->range_max)
return;
rtc_time64_to_tm(secs - rtc->offset_secs, tm);
}
static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
{
if (rtc->range_min != rtc->range_max) {
time64_t time = rtc_tm_to_time64(tm);
time64_t range_min = rtc->set_start_time ? rtc->start_secs :
rtc->range_min;
time64_t range_max = rtc->set_start_time ?
(rtc->start_secs + rtc->range_max - rtc->range_min) :
rtc->range_max;
if (time < range_min || time > range_max)
return -ERANGE;
}
return 0;
}
static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
{
int err;
if (!rtc->ops)
err = -ENODEV;
else if (!rtc->ops->read_time)
err = -EINVAL;
else {
memset(tm, 0, sizeof(struct rtc_time));
err = rtc->ops->read_time(rtc->dev.parent, tm);
if (err < 0) {
dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
err);
return err;
}
rtc_add_offset(rtc, tm);
err = rtc_valid_tm(tm);
if (err < 0)
dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
}
return err;
}
int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
{
int err;
err = mutex_lock_interruptible(&rtc->ops_lock);
if (err)
return err;
err = __rtc_read_time(rtc, tm);
mutex_unlock(&rtc->ops_lock);
trace_rtc_read_time(rtc_tm_to_time64(tm), err);
return err;
}
EXPORT_SYMBOL_GPL(rtc_read_time);
int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
{
int err;
err = rtc_valid_tm(tm);
if (err != 0)
return err;
err = rtc_valid_range(rtc, tm);
if (err)
return err;
rtc_subtract_offset(rtc, tm);
err = mutex_lock_interruptible(&rtc->ops_lock);
if (err)
return err;
if (!rtc->ops)
err = -ENODEV;
else if (rtc->ops->set_time)
err = rtc->ops->set_time(rtc->dev.parent, tm);
else if (rtc->ops->set_mmss64) {
time64_t secs64 = rtc_tm_to_time64(tm);
err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
} else if (rtc->ops->set_mmss) {
time64_t secs64 = rtc_tm_to_time64(tm);
err = rtc->ops->set_mmss(rtc->dev.parent,