linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Esteban Blanc" <eblanc@baylibre.com>
To: <andy.shevchenko@gmail.com>
Cc: <linus.walleij@linaro.org>, <lgirdwood@gmail.com>,
	<broonie@kernel.org>, <a.zummo@towertech.it>,
	<alexandre.belloni@bootlin.com>, <linux-kernel@vger.kernel.org>,
	<linux-gpio@vger.kernel.org>, <linux-rtc@vger.kernel.org>,
	<jpanis@baylibre.com>, <jneanne@baylibre.com>,
	<aseketeli@baylibre.com>, <u-kumar1@ti.com>
Subject: Re: [PATCH v5 1/3] rtc: tps6594: Add driver for TPS6594 RTC
Date: Thu, 25 May 2023 15:56:25 +0200	[thread overview]
Message-ID: <CSULDUAPQNFL.18PZXEW1IBRQM@burritosblues> (raw)
In-Reply-To: <ZGzBe6O_mw_pdSkH@surfacebook>

On Tue May 23, 2023 at 3:36 PM CEST,  wrote:
> Mon, May 22, 2023 at 06:31:13PM +0200, Esteban Blanc kirjoitti:
> > TPS6594 PMIC is a MFD. This patch adds support for
> > the RTC found inside TPS6594 family of PMIC.
> >
> > Alarm is also supported.
>
> ...
>
> > +	help
> > +	  TI Power Management IC TPS6594 supports RTC functionality
> > +	  along with alarm. This driver supports the RTC driver for
> > +	  the TPS6594 RTC module.
> > +
> > +	  This driver can also be built as a module. If so, the module
> > +	  will be called tps6594-rtc
>
> Grammar period at the end?

Thanks!

> > +#define TPS6594_GET_TIME_ON TPS6594_BIT_GET_TIME
> > +#define TPS6594_GET_TIME_OFF 0
>
> Not used.

Thanks.
>
> > +#define TPS6594_IT_ALARM_ON TPS6594_BIT_IT_ALARM
> > +#define TPS6594_IT_ALARM_OFF 0
>
> Used only once.

True. Is that a bad thing?

> > +#define TPS6594_AUTO_COMP_ON TPS6594_BIT_IT_ALARM
>
> No _OFF counterpart.
>
> That said the _OFF can be dropped completely. And the rest I see no value to
> have, just use those bit definitions directly?

I was thinking it would make this more readable. I will remove them, no
problem.

> > +static int tps6594_rtc_alarm_irq_enable(struct device *dev,
> > +					unsigned int enabled)
> > +{
> > +	struct tps6594 *tps = dev_get_drvdata(dev->parent);
> > +	u8 val = 0;
>
> Redundant assignment.
>

Thanks!

> > +	// Read shadowed RTC registers.
> > +	ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_SECONDS, rtc_data,
> > +			       NUM_TIME_REGS);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	tm->tm_sec = bcd2bin(rtc_data[0]);
> > +	tm->tm_min = bcd2bin(rtc_data[1]);
> > +	tm->tm_hour = bcd2bin(rtc_data[2]);
> > +	tm->tm_mday = bcd2bin(rtc_data[3]);
> > +	tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
> > +	tm->tm_year = bcd2bin(rtc_data[5]) + 100;
> > +	tm->tm_wday = bcd2bin(rtc_data[6]);
> > +
> > +	return ret;
>
> 	return 0;
>
> No?

`regmap_bulk_read` returns 0 on success so ret should be 0 here. I will
apply your suggestion anyway it is more readable. Thanks!

> > +static int tps6594_rtc_set_calibration(struct device *dev, int calibration)
> > +{
> > +	unsigned char comp_data[NUM_COMP_REGS];
> > +	struct tps6594 *tps = dev_get_drvdata(dev->parent);
> > +	__le16 value;
> > +	int ret;
> > +
> > +	/*
> > +	 * TPS6594 uses two's complement 16 bit value for compensation of RTC
> > +	 * crystal inaccuracies. One time every hour when seconds counter
> > +	 * increments from 0 to 1 compensation value will be added to internal
> > +	 * RTC counter value.
> > +	 *
> > +	 * Valid range for compensation value: [-32767 .. 32767].
>
> This is defined naturally by the bits available, correct?

Your right. Maybe `calibration` argument should be an s16 instead of an
int?

> > +	 */
> > +	if (calibration < -32767 || calibration > 32767) {
>
> So, this can be S16_MIN / S16_MAX range. The question here is what the
> -32768 meaning is and why it can't be used.

I will rewrite it using this 2 macros.

This range [-32767,32767] is specified in the datasheet. As for why
-32768 can't be used I have no idea.

> > +		dev_err(dev, "RTC calibration value out of range: %d\n",
> > +			calibration);
> > +		return -EINVAL;
>
> -ERANGE

Ok, thanks.

> > +	}
>
> > +	value = (__le16)calibration;
> > +
> > +	comp_data[0] = value & 0xFF;
> > +	comp_data[1] = (value >> 8) & 0xFF;
>
> Of course these three lines is not what expected.
>
> 	value = cpu_to_le16();

Sorry for the mistake. I've tried to find more information on those type
but I did not realize there was specific functions/macros for them.

I have learn something today :)

> > +	// Update all the compensation registers in one shot.
> > +	ret = regmap_bulk_write(tps->regmap, TPS6594_REG_RTC_COMP_LSB,
> > +				comp_data, NUM_COMP_REGS);
>
> 				&value, sizeof(value) ?

This is way cleaner indeed.

> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	// Enable automatic compensation.
> > +	return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
> > +			       TPS6594_BIT_AUTO_COMP);
> > +}

> > +	ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_COMP_LSB, comp_data,
> > +			       NUM_COMP_REGS);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	value = (__le16)comp_data[0] | ((__le16)comp_data[1] << 8);
> > +
> > +	*calibration = value;
>
> In the similar (complementary API) way as above.

Sure.

> > +	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
>
> Having
>
> 	struct device *dev = &pdev->dev;
>
> might make this and other lines shorter / neater.

Will do, thanks.

> > +					tps6594_rtc_interrupt, IRQF_ONESHOT,
> > +					TPS6594_IRQ_NAME_ALARM, &pdev->dev);
> > +	if (ret < 0)
> > +		return dev_err_probe(&pdev->dev, ret,
> > +				     "Failed to request_threaded_irq\n");

Thanks for your review.

Best regards,

--
Esteban Blanc
BayLibre


  parent reply	other threads:[~2023-05-25 13:56 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-22 16:31 [PATCH v5 0/3] TI TPS6594 PMIC support (RTC, pinctrl, regulators) Esteban Blanc
2023-05-22 16:31 ` [PATCH v5 1/3] rtc: tps6594: Add driver for TPS6594 RTC Esteban Blanc
2023-05-23 13:36   ` andy.shevchenko
2023-05-23 14:32     ` Alexandre Belloni
2023-05-25 13:56     ` Esteban Blanc [this message]
2023-05-22 16:31 ` [PATCH v5 2/3] pinctrl: tps6594: Add driver for TPS6594 pinctrl and GPIOs Esteban Blanc
2023-05-23 19:14   ` andy.shevchenko
2023-05-25 14:07     ` Esteban Blanc
2023-05-26 18:43       ` Andy Shevchenko
2023-05-31  7:58         ` Esteban Blanc
2023-05-22 16:31 ` [PATCH v5 3/3] regulator: tps6594-regulator: Add driver for TI TPS6594 regulators Esteban Blanc
2023-05-23 19:33   ` andy.shevchenko
2023-06-07 11:44     ` jerome Neanne
2023-06-07 14:52       ` Andy Shevchenko
2023-06-06 16:51 ` [PATCH v5 0/3] TI TPS6594 PMIC support (RTC, pinctrl, regulators) Mark Brown
2023-06-07 12:40 ` (subset) " Mark Brown

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=CSULDUAPQNFL.18PZXEW1IBRQM@burritosblues \
    --to=eblanc@baylibre.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=aseketeli@baylibre.com \
    --cc=broonie@kernel.org \
    --cc=jneanne@baylibre.com \
    --cc=jpanis@baylibre.com \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=u-kumar1@ti.com \
    /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).