mirror of
https://github.com/torvalds/linux.git
synced 2024-11-25 21:51:40 +00:00
rtc: mc13xxx: make rtc_read_time() more readable
Remove unnecessary locks when reading the time and make the read operation until the values of day matched between reading the seconds, it will make the mc13xxx_rtc_read_time() procedure more readable. Additionally, patch introduced a "seconds in a day" definition. Signed-off-by: Alexander Shiyan <shc_work@mail.ru> Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Cc: Sascha Hauer <kernel@pengutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
12de362108
commit
6a2b342228
@ -23,6 +23,8 @@
|
||||
#define MC13XXX_RTCDAY 22
|
||||
#define MC13XXX_RTCDAYA 23
|
||||
|
||||
#define SEC_PER_DAY (24 * 60 * 60)
|
||||
|
||||
struct mc13xxx_rtc {
|
||||
struct rtc_device *rtc;
|
||||
struct mc13xxx *mc13xxx;
|
||||
@ -61,42 +63,27 @@ static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
||||
{
|
||||
struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
|
||||
unsigned int seconds, days1, days2;
|
||||
unsigned long s1970;
|
||||
int ret;
|
||||
|
||||
if (!priv->valid)
|
||||
return -ENODATA;
|
||||
|
||||
mc13xxx_lock(priv->mc13xxx);
|
||||
do {
|
||||
int ret;
|
||||
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
|
||||
out:
|
||||
mc13xxx_unlock(priv->mc13xxx);
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
|
||||
if (ret)
|
||||
return ret;
|
||||
} while (days1 != days2);
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
if (days2 == days1 + 1) {
|
||||
if (seconds >= 86400 / 2)
|
||||
days2 = days1;
|
||||
else
|
||||
days1 = days2;
|
||||
}
|
||||
|
||||
if (days1 != days2)
|
||||
return -EIO;
|
||||
|
||||
s1970 = days1 * 86400 + seconds;
|
||||
|
||||
rtc_time_to_tm(s1970, tm);
|
||||
rtc_time_to_tm(days1 * SEC_PER_DAY + seconds, tm);
|
||||
|
||||
return rtc_valid_tm(tm);
|
||||
}
|
||||
@ -108,8 +95,8 @@ static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
|
||||
unsigned int alarmseconds;
|
||||
int ret;
|
||||
|
||||
seconds = secs % 86400;
|
||||
days = secs / 86400;
|
||||
seconds = secs % SEC_PER_DAY;
|
||||
days = secs / SEC_PER_DAY;
|
||||
|
||||
mc13xxx_lock(priv->mc13xxx);
|
||||
|
||||
@ -121,7 +108,7 @@ static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
|
||||
if (alarmseconds < 86400) {
|
||||
if (alarmseconds < SEC_PER_DAY) {
|
||||
ret = mc13xxx_reg_write(priv->mc13xxx,
|
||||
MC13XXX_RTCTODA, 0x1ffff);
|
||||
if (unlikely(ret))
|
||||
@ -145,7 +132,7 @@ static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
|
||||
goto out;
|
||||
|
||||
/* restore alarm */
|
||||
if (alarmseconds < 86400) {
|
||||
if (alarmseconds < SEC_PER_DAY) {
|
||||
ret = mc13xxx_reg_write(priv->mc13xxx,
|
||||
MC13XXX_RTCTODA, alarmseconds);
|
||||
if (unlikely(ret))
|
||||
@ -181,7 +168,7 @@ static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
||||
ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
if (seconds >= 86400) {
|
||||
if (seconds >= SEC_PER_DAY) {
|
||||
ret = -ENODATA;
|
||||
goto out;
|
||||
}
|
||||
@ -202,7 +189,7 @@ out:
|
||||
alarm->enabled = enabled;
|
||||
alarm->pending = pending;
|
||||
|
||||
s1970 = days * 86400 + seconds;
|
||||
s1970 = days * SEC_PER_DAY + seconds;
|
||||
|
||||
rtc_time_to_tm(s1970, &alarm->time);
|
||||
dev_dbg(dev, "%s: %lu\n", __func__, s1970);
|
||||
@ -240,8 +227,8 @@ static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
|
||||
seconds = s1970 % 86400;
|
||||
days = s1970 / 86400;
|
||||
seconds = s1970 % SEC_PER_DAY;
|
||||
days = s1970 / SEC_PER_DAY;
|
||||
|
||||
ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
|
||||
if (unlikely(ret))
|
||||
|
Loading…
Reference in New Issue
Block a user