Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp35031imm; Thu, 10 May 2018 15:00:22 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrmZrBVVBUK0kJKdKzGuz7hBdjyPwnycVHj/OsFua/CrCyYwyg/Sp0WIVdRQeoa3uGVrtwH X-Received: by 2002:a63:a002:: with SMTP id r2-v6mr2391325pge.240.1525989622358; Thu, 10 May 2018 15:00:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525989622; cv=none; d=google.com; s=arc-20160816; b=pme91nNyhEDHDAEwm/sPMmlXb0CEFuCyVH9FBGH5WaH6ufC0EvmB4O2sVVHtaYQY2J lmqKM+6DAXvceJoUf7Y5ehcK4H7EeUTTsPDkXpgPy2AAbzHLWk2ba6moa84LbdbGVdZe Aeid8H1h0aYEUFsRquFEjuIft3u+ucaNl12ySF/Ka3nzs/IBhCrNRz6d/MEIgSTiZ0tU dH9B/1j2CtnFnu8r+1cLo6xKeIgGow6X5zeHQZO55OUV/6VOr/f824lh9dqIiatG8XK3 hguWOwqkdq+ZKW2S5A/oUfmLY4sYASNp5P47nANPeIv9K8HaU/2ZoykBxbsmYWUfdp3P vmyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=MFhkAvoDlcvMjV/8scTgGeDFn4pNYp1WfDydMtHvkis=; b=LzVon6BnsRtDyydfgBwVHdnlPgqnn5dooHyn6C9mUazwcBqwAwaxecNXNTTWEPwFwc 3WJc65XzQNYLusPRpK3wKFE01XJ+HPrrzmdzbsodh1qVyv5Jk43+7ZsWU/XY0Catu577 HIdEmWfD/EE3BTLLCr3MFXMBH9z6RVv6QnA0VXXGE3QYGzU/fHj6eIXpNfKRzrRNS2Gx eIg0WwQK5iUN9lTdypcH7IVnWis/CmU1vMeKgziBPsT9hb7TJPjheNy6DmJQtn0egk/1 j39mpFyX0o3PLgqhNYs0y2zmkiVwGjqvwT3HMkrUPH4/QcTl4HE2aIirkMX6e+7Q72lE WqZA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e15-v6si1023706pgv.60.2018.05.10.15.00.07; Thu, 10 May 2018 15:00:22 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752720AbeEJV7w (ORCPT + 99 others); Thu, 10 May 2018 17:59:52 -0400 Received: from mail.bootlin.com ([62.4.15.54]:51100 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752698AbeEJV7s (ORCPT ); Thu, 10 May 2018 17:59:48 -0400 Received: by mail.bootlin.com (Postfix, from userid 110) id 36FE22084E; Thu, 10 May 2018 23:59:46 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost (AAmiens-551-1-45-18.w109-219.abo.wanadoo.fr [109.219.60.18]) by mail.bootlin.com (Postfix) with ESMTPSA id 0358320756; Thu, 10 May 2018 23:59:45 +0200 (CEST) Date: Thu, 10 May 2018 23:59:46 +0200 From: Alexandre Belloni To: Giulio Benetti Cc: a.zummo@towertech.it, robh+dt@kernel.org, mark.rutland@arm.com, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 3/4] rtc: ds1307: add offset sysfs for mt41txx chips. Message-ID: <20180510215946.GS10960@piout.net> References: <20180509193355.49466-1-giulio.benetti@micronovasrl.com> <20180509193355.49466-3-giulio.benetti@micronovasrl.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180509193355.49466-3-giulio.benetti@micronovasrl.com> User-Agent: Mutt/1.9.5 (2018-04-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/05/2018 21:33:54+0200, Giulio Benetti wrote: > m41txx chips can hold a calibration value to get correct clock bias. > If positive offset is passed, it means adding 512 cycles(@32.768Hz) > every tick(1s). > If negative offset is passed, it means subtracting 256 cycles(@32.768Hz) > every tick(1s). > > Add offset handling (ranging between (-31) and 31) via sysfs. > > Signed-off-by: Giulio Benetti > --- > drivers/rtc/rtc-ds1307.c | 70 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 70 insertions(+) > > diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c > index 0ab0c166da83..33895668b363 100644 > --- a/drivers/rtc/rtc-ds1307.c > +++ b/drivers/rtc/rtc-ds1307.c > @@ -114,6 +114,16 @@ enum ds_type { > # define RX8025_BIT_VDET 0x40 > # define RX8025_BIT_XST 0x20 > > +#define M41TXX_REG_CONTROL 0x07 > +# define M41TXX_BIT_OUT 0x80 > +# define M41TXX_BIT_FT 0x40 > +# define M41TXX_BIT_CALIB_SIGN 0x20 > +# define M41TXX_M_CALIBRATION 0x1f > + > +/* Min and max values supported with 'offset' interface by M41TXX */ > +#define M41TXX_MIN_OFFSET (-31) > +#define M41TXX_MAX_OFFSET (31) > + > struct ds1307 { > enum ds_type type; > unsigned long flags; > @@ -146,6 +156,9 @@ struct chip_desc { > > static int ds1307_get_time(struct device *dev, struct rtc_time *t); > static int ds1307_set_time(struct device *dev, struct rtc_time *t); > +static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t); > +static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t); > +static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled); > static u8 do_trickle_setup_ds1339(struct ds1307 *, u32 ohms, bool diode); > static irqreturn_t rx8130_irq(int irq, void *dev_id); > static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t); > @@ -155,6 +168,8 @@ static irqreturn_t mcp794xx_irq(int irq, void *dev_id); > static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t); > static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t); > static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled); > +static int m41txx_rtc_read_offset(struct device *dev, long *offset); > +static int m41txx_rtc_set_offset(struct device *dev, long offset); > > static const struct rtc_class_ops rx8130_rtc_ops = { > .read_time = ds1307_get_time, > @@ -172,6 +187,16 @@ static const struct rtc_class_ops mcp794xx_rtc_ops = { > .alarm_irq_enable = mcp794xx_alarm_irq_enable, > }; > > +static const struct rtc_class_ops m41txx_rtc_ops = { > + .read_time = ds1307_get_time, > + .set_time = ds1307_set_time, > + .read_alarm = ds1337_read_alarm, > + .set_alarm = ds1337_set_alarm, > + .alarm_irq_enable = ds1307_alarm_irq_enable, > + .read_offset = m41txx_rtc_read_offset, > + .set_offset = m41txx_rtc_set_offset, > +}; > + > static const struct chip_desc chips[last_ds_type] = { > [ds_1307] = { > .nvram_offset = 8, > @@ -227,10 +252,17 @@ static const struct chip_desc chips[last_ds_type] = { > .irq_handler = rx8130_irq, > .rtc_ops = &rx8130_rtc_ops, > }, > + [m41t0] = { > + .rtc_ops = &m41txx_rtc_ops, > + }, > + [m41t00] = { > + .rtc_ops = &m41txx_rtc_ops, > + }, > [m41t11] = { > /* this is battery backed SRAM */ > .nvram_offset = 8, > .nvram_size = 56, > + .rtc_ops = &m41txx_rtc_ops, > }, > [mcp794xx] = { > .alarm = 1, > @@ -972,6 +1004,44 @@ static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled) > enabled ? MCP794XX_BIT_ALM0_EN : 0); > } > > +static int m41txx_rtc_read_offset(struct device *dev, long *offset) > +{ > + struct ds1307 *ds1307 = dev_get_drvdata(dev); > + unsigned int ctrl_reg; > + u8 val; > + > + regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); > + > + val = ctrl_reg & M41TXX_M_CALIBRATION; > + > + /* check if positive */ > + if (ctrl_reg & M41TXX_BIT_CALIB_SIGN) > + *offset = val; > + else > + *offset = -val; > + This is definitively not right, you have to convert to/from ppb when reading/setting the offset. The datasheet gives you the correct steps in ppm. > + return 0; > +} > + > +static int m41txx_rtc_set_offset(struct device *dev, long offset) > +{ > + struct ds1307 *ds1307 = dev_get_drvdata(dev); > + unsigned int ctrl_reg; > + > + if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET)) > + return -ERANGE; > + > + regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); > + > + ctrl_reg &= ~M41TXX_M_CALIBRATION; > + ctrl_reg |= abs(offset) & M41TXX_M_CALIBRATION; > + > + if (offset >= 0) > + ctrl_reg |= M41TXX_BIT_CALIB_SIGN; > + > + return regmap_write(ds1307->regmap, M41TXX_REG_CONTROL, ctrl_reg); Please use regmap_update_bits > +} > + > /*----------------------------------------------------------------------*/ > > static int ds1307_nvram_read(void *priv, unsigned int offset, void *val, > -- > 2.17.0 > -- Alexandre Belloni, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com