From: linux@armlinux.org.uk (Russell King - ARM Linux)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] rtc: armada38x: add support for trimming the RTC
Date: Tue, 3 Oct 2017 14:56:21 +0100 [thread overview]
Message-ID: <20171003135620.GN20805@n2100.armlinux.org.uk> (raw)
In-Reply-To: <20171003132433.vkgjxuoeq26x3f47@piout.net>
On Tue, Oct 03, 2017 at 03:24:33PM +0200, Alexandre Belloni wrote:
> Hi Russell,
>
> On 29/09/2017 at 11:23:31 +0100, Russell King wrote:
> > +/*
> > + * The information given in the Armada 388 functional spec is complex.
> > + * They give two different formulas for calculating the offset value,
> > + * but when considering "Offset" as an 8-bit signed integer, they both
> > + * reduce down to (we shall rename "Offset" as "val" here):
> > + *
> > + * val = (f_ideal / f_measured - 1) / resolution where f_ideal = 32768
> > + *
> > + * Converting to time, f = 1/t:
> > + * val = (t_measured / t_ideal - 1) / resolution where t_ideal = 1/32768
> > + *
> > + * => t_measured / t_ideal = val * resolution + 1
> > + *
> > + * "offset" in the RTC interface is defined as:
> > + * t = t0 * (1 + offset * 1e-9)
> > + * where t is the desired period, t0 is the measured period with a zero
> > + * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
> > + * offset = (t_ideal / t_measured - 1) / 1e-9
> > + *
> > + * => t_ideal / t_measured = offset * 1e-9 + 1
> > + *
> > + * so:
> > + *
> > + * offset * 1e-9 + 1 = 1 / (val * resolution + 1)
> > + *
> > + * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
> > + * offset = 1e18 / (val * R + 1e9) - 1e9
> > + * val = (1e18 / (offset + 1e9) - 1e9) / R
> > + * with a common transformation:
> > + * f(x) = 1e18 / (x + 1e9) - 1e9
> > + * offset = f(val * R)
> > + * val = f(offset) / R
> > + *
> > + * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
> > + */
> > +static long armada38x_ppb_convert(long ppb)
> > +{
> > + long div = ppb + 1000000000L;
> > +
> > + return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
>
> The previous comment is perfect but it was not completely obvious to me
> why you are adding div / 2. Maybe you can add a small comment.
It's the standard way to turn a regular integer division into one which
rounds-to-closest - I consider it to be one of the basics which every
programmer should know, and therefore should be obvious.
Integer division in the form of:
r = (a + b / 2) / b
should be part of the programmers basic knowledge set.
--
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 8.8Mbps down 630kbps up
According to speedtest.net: 8.21Mbps down 510kbps up
next prev parent reply other threads:[~2017-10-03 13:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-29 10:23 [PATCH] rtc: armada38x: add support for trimming the RTC Russell King
2017-10-03 13:24 ` Alexandre Belloni
2017-10-03 13:56 ` Russell King - ARM Linux [this message]
2017-10-25 21:24 ` Alexandre Belloni
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171003135620.GN20805@n2100.armlinux.org.uk \
--to=linux@armlinux.org.uk \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).