From patchwork Tue Jan 2 05:10:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 123093 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp8567408qgn; Mon, 1 Jan 2018 21:11:05 -0800 (PST) X-Google-Smtp-Source: ACJfBosgaU9WEQD1dELn5MPqQA+WDIEUbDA2hhR1LARVKPFQ4BeIF3CUmj7f58GymfDVlibKpdkw X-Received: by 10.99.183.14 with SMTP id t14mr20766540pgf.147.1514869865137; Mon, 01 Jan 2018 21:11:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514869865; cv=none; d=google.com; s=arc-20160816; b=smf0gphZgk4ZOGWJFK6/FhZzV0tqr3aZfn5R2nmIpC4Kwhj92PQNj3T32huCbJDbei rvGB92o995i19G3apbiwh1W5q+bDbsEm4VuHObBLaEK7AePOS3oS7SUfsyQvyCB78Tn9 XYVciqZdOYaHLSL3i3/BgpNMQw5zc6ia3uukz8r/1VgEsAHKajao4s4XPVSnePTjJRir WK3WLOAEPCwVwYqr2BZeTPA/S4WAvFMLC9Ier0E5NWEwS/Ayzm5WrbhVZmbXmRAhxY0o JQq8nKM59FT6OtDpypvPgH8NnaQNxV2yUB3jl4h7kNICPIKqhtqsiHwtG3n/y1jVORU4 sQKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=TXZPNBPzJLI0ZLb1NHwNqn1Zn9JmE68VFOlRVLYxhSU=; b=gjrqwGV+X9v2YRIZNNYRlgy3y3zt/nOjEEl0EPqft0XHwWu3synqKhVKb5lWoIRFvD E65YdVQt+8fTDAq6zYzJENpFGQJEITX8SgoDJu9Mj4nf+sP7smtW1/jF+infN/QsEHeu 4SCSltxMVrT46AOLGydDVEqsR76mChNFkoK0eOOul1956HXt6m/5Q7F63fzpRtUXwx2h M7pbjz1w+zykVlQJb+mE49GUDSJL+yC18eJhYYVxgpli7SJMc/8fBOzAauyvXL91Qq2U N4yupHdQEDX46CHAie1DHkD2OiVWDhoVrtl4wdAPidtaX8jEcnx5dn4A+0oabsYuTkIu YHbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LUVqwrtR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m12si7594330pgp.734.2018.01.01.21.11.04; Mon, 01 Jan 2018 21:11:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LUVqwrtR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751662AbeABFK7 (ORCPT + 28 others); Tue, 2 Jan 2018 00:10:59 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:36089 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751493AbeABFKe (ORCPT ); Tue, 2 Jan 2018 00:10:34 -0500 Received: by mail-pf0-f195.google.com with SMTP id p84so25280315pfd.3 for ; Mon, 01 Jan 2018 21:10:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=TXZPNBPzJLI0ZLb1NHwNqn1Zn9JmE68VFOlRVLYxhSU=; b=LUVqwrtRp+5nsH4Q7OpoNpvBwmNXcUF9K2LYZHCa2/WGxg2rKOrOREG+0Evu/2lZhT nua5mP3s7S/lV5b92PAzaj3y3J2bEgCJlNZSqv5Q9Zv24DHjjeUbwP5XmRhtXwOMWNoJ rxS0DINrruAyGnYdiI0iuB5CSmZi22nEAQ3Gg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=TXZPNBPzJLI0ZLb1NHwNqn1Zn9JmE68VFOlRVLYxhSU=; b=QVohWd7dUAopJ8YvqmyH90BzpwLdQIFTOI+NrsQLU2c/bvDrgESiIW4Qz/Ay1rEmXr dZbn4J7Xap96Ly0TCrlkD+5zwyhB4m3fuslprtfiYEFDyaScfSPm/0ekV3IFHhicL3qb FzdIkQZmpj3sIM1HqJnMyFNGP44w+uPLHdZ9ntyi0zIdzYT2EykqxrkdhwzTZcn86plV ckt0GZ2HxBtkGnMxB89rAHS4zjzn6+blectz6BgjVpmLHXeR/Bpxy1z4/Cw+WvbiVHt6 0w31dSVra3lk6Rp9ToOlKlReMx0ZJufs8uVajYmltCiZgGXlRIsG1mJqmoa2Nm+9SBH6 O7Fw== X-Gm-Message-State: AKGB3mIpMqQ9BmMBBCphXnSlwbOevO5e/NP7ecQOzZAU/h2zi5EZOBQ9 2DoVi58SSB2LXYLJplNkpUirWA== X-Received: by 10.98.197.68 with SMTP id j65mr45124569pfg.176.1514869834246; Mon, 01 Jan 2018 21:10:34 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 6sm63989675pgf.54.2018.01.01.21.10.31 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Jan 2018 21:10:33 -0800 (PST) From: Baolin Wang To: a.zummo@towertech.it, alexandre.belloni@free-electrons.com, corbet@lwn.net Cc: arnd@arndb.de, broonie@kernel.org, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, baolin.wang@linaro.org Subject: [RFC PATCH 3/4] rtc: Add one offset seconds to expand RTC range Date: Tue, 2 Jan 2018 13:10:07 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <9ab56bdcaeb52241e22b738d6babe7b01728f64c.1514869621.git.baolin.wang@linaro.org> References: <9ab56bdcaeb52241e22b738d6babe7b01728f64c.1514869621.git.baolin.wang@linaro.org> In-Reply-To: <9ab56bdcaeb52241e22b738d6babe7b01728f64c.1514869621.git.baolin.wang@linaro.org> References: <9ab56bdcaeb52241e22b738d6babe7b01728f64c.1514869621.git.baolin.wang@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >From our investigation for all RTC drivers, 1 driver will be expired before year 2017, 7 drivers will be expired before year 2038, 23 drivers will be expired before year 2069, 72 drivers will be expired before 2100 and 104 drivers will be expired before 2106. Especially for these early expired drivers, we need to expand the RTC range to make the RTC can still work after the expired year. So we can expand the RTC range by adding one offset to the time when reading from hardware, and subtracting it when writing back. For example, if you have an RTC that can do 100 years, and currently is configured to be based in Jan 1 1970, so it can represents times from 1970 to 2069. Then if you change the start year from 1970 to 2000, which means it can represents times from 2000 to 2099. By adding or subtracting the offset produced by moving the wrap point, all times between 1970 and 1999 from RTC hardware could get interpreted as times from 2070 to 2099, but the interpretation of dates between 2000 and 2069 would not change. Signed-off-by: Baolin Wang --- drivers/rtc/class.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/rtc/interface.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-- include/linux/rtc.h | 2 ++ 3 files changed, 106 insertions(+), 2 deletions(-) -- 1.7.9.5 diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index 31fc0f1..8e59cf0 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -211,6 +211,55 @@ static int rtc_device_get_id(struct device *dev) return id; } +static void rtc_device_get_offset(struct rtc_device *rtc) +{ + u32 start_year; + int ret; + + rtc->offset_secs = 0; + rtc->start_secs = rtc->min_hw_secs; + + /* + * If RTC driver did not implement the range of RTC hardware device, + * then we can not expand the RTC range by adding or subtracting one + * offset. + */ + if (!rtc->max_hw_secs) + return; + + ret = device_property_read_u32(rtc->dev.parent, "start-year", + &start_year); + if (ret) + return; + + /* + * Record the start time values in seconds, which are used to valid if + * the setting time values are in the new expanded range. + */ + rtc->start_secs = max_t(time64_t, mktime64(start_year, 1, 1, 0, 0, 0), + rtc->min_hw_secs); + + /* + * If the start_secs is larger than the maximum seconds (max_hw_secs) + * support by RTC hardware, which means the minimum seconds + * (min_hw_secs) of RTC hardware will be mapped to start_secs by adding + * one offset, so the offset seconds calculation formula should be: + * rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs; + * + * If the start_secs is less than max_hw_secs, then there is one region + * is overlapped between the original RTC hardware range and the new + * expanded range, and this overlapped region do not need to be mapped + * into the new expanded range due to it is valid for RTC device. So + * the minimum seconds of RTC hardware (min_hw_secs) should be mapped to + * max_hw_secs + 1, then the offset seconds formula should be: + * rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1; + */ + if (rtc->start_secs > rtc->max_hw_secs) + rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs; + else + rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1; +} + /** * rtc_device_register - register w/ RTC class * @dev: the device to register @@ -253,6 +302,8 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, goto exit_ida; } + rtc_device_get_offset(rtc); + /* Check to see if there is an ALARM already set in hw */ err = __rtc_read_alarm(rtc, &alrm); @@ -449,6 +500,8 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc) return err; } + rtc_device_get_offset(rtc); + /* Check to see if there is an ALARM already set in hw */ err = __rtc_read_alarm(rtc, &alrm); if (!err && !rtc_valid_tm(&alrm.time)) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index c8090e3..eb96a90 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -20,6 +20,46 @@ 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 less than + * rtc->max_hw_secs, then if the reading time values are larger than + * the rtc->start_secs, which means they did not subtract the offset + * when writing into RTC device, so we do not need to add the offset. + */ + if (secs >= rtc->start_secs) + 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->max_hw_secs) + return; + + rtc_time64_to_tm(secs - rtc->offset_secs, tm); +} + static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) { int err; @@ -36,6 +76,8 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) 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"); @@ -69,6 +111,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) if (err) return err; + rtc_subtract_offset(rtc, tm); + err = mutex_lock_interruptible(&rtc->ops_lock); if (err) return err; @@ -123,6 +167,8 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *al } mutex_unlock(&rtc->ops_lock); + + rtc_add_offset(rtc, &alarm->time); return err; } @@ -338,6 +384,7 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) if (err) return err; + rtc_subtract_offset(rtc, &alarm->time); scheduled = rtc_tm_to_time64(&alarm->time); /* Make sure we're not setting alarms in the past */ @@ -1074,7 +1121,8 @@ int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs, * @ tm: time values need to valid. * * Only the rtc->max_hw_secs was set, then we can valid if the setting time - * values are beyond the RTC range. + * values are beyond the RTC range. When drivers set one start time values, + * we need to valid if the setting time values are in the new expanded range. */ int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) { @@ -1084,7 +1132,8 @@ int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) return 0; secs = rtc_tm_to_time64(tm); - if (secs < rtc->min_hw_secs || secs > rtc->max_hw_secs) + if (secs < rtc->start_secs || + secs > (rtc->start_secs + rtc->max_hw_secs - rtc->min_hw_secs)) return -EINVAL; return 0; diff --git a/include/linux/rtc.h b/include/linux/rtc.h index 19a8989..11879b7 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -156,6 +156,8 @@ struct rtc_device { time64_t max_hw_secs; time64_t min_hw_secs; + time64_t start_secs; + time64_t offset_secs; #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL struct work_struct uie_task;