All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Alex Elder <elder@riscstar.com>
Cc: lee@kernel.org, lgirdwood@gmail.com, broonie@kernel.org,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	mat.jonczyk@o2.pl, dlan@gentoo.org, paul.walmsley@sifive.com,
	palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr,
	troymitchell988@gmail.com, guodong@riscstar.com,
	linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
	linux-riscv@lists.infradead.org, spacemit@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 5/8] rtc: spacemit: support the SpacemiT P1 RTC
Date: Wed, 23 Jul 2025 23:39:56 +0200	[thread overview]
Message-ID: <20250723213956570da462@mail.local> (raw)
In-Reply-To: <20250710175107.1280221-6-elder@riscstar.com>

On 10/07/2025 12:51:03-0500, Alex Elder wrote:
> +static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
> +{
> +	struct p1_rtc *p1 = dev_get_drvdata(dev);
> +	struct regmap *regmap = p1->regmap;
> +	u32 count = RTC_READ_TRIES;
> +	u8 seconds;
> +	u8 time[6];
> +	int ret;
> +
> +	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
> +		return -ENODEV;		/* RTC is disabled */

That should be -EINVAL, as all the other drivers have standardized this.

With this fixed,
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>


> +
> +	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
> +	if (ret)
> +		return ret;
> +
> +	do {
> +		seconds = time[0];
> +		ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
> +		if (ret)
> +			return ret;
> +	} while (time[0] != seconds && --count);
> +
> +	if (!count)
> +		return -EIO;		/* Unable to get a consistent result */
> +
> +	t->tm_sec = time[0] & GENMASK(5, 0);
> +	t->tm_min = time[1] & GENMASK(5, 0);
> +	t->tm_hour = time[2] & GENMASK(4, 0);
> +	t->tm_mday = (time[3] & GENMASK(4, 0)) + 1;
> +	t->tm_mon = time[4] & GENMASK(3, 0);
> +	t->tm_year = (time[5] & GENMASK(5, 0)) + 100;
> +
> +	return 0;
> +}
> +
> +/*
> + * The P1 hardware documentation states that values in the registers are
> + * latched so when written they represent a consistent time snapshot.
> + * Nevertheless, this is not guaranteed by the implementation, so we must
> + * disable the RTC while updating it.
> + */
> +static int p1_rtc_set_time(struct device *dev, struct rtc_time *t)
> +{
> +	struct p1_rtc *p1 = dev_get_drvdata(dev);
> +	struct regmap *regmap = p1->regmap;
> +	u8 time[6];
> +	int ret;
> +
> +	time[0] = t->tm_sec;
> +	time[1] = t->tm_min;
> +	time[2] = t->tm_hour;
> +	time[3] = t->tm_mday - 1;
> +	time[4] = t->tm_mon;
> +	time[5] = t->tm_year - 100;
> +
> +	/* Disable the RTC to update; re-enable again when done */
> +	ret = regmap_clear_bits(regmap, RTC_CTRL, RTC_EN);
> +	if (ret)
> +		return ret;
> +
> +	/* If something goes wrong, leave the RTC disabled */
> +	ret = regmap_bulk_write(regmap, RTC_TIME, time, sizeof(time));
> +	if (ret)
> +		return ret;
> +
> +	return regmap_set_bits(regmap, RTC_CTRL, RTC_EN);
> +}
> +
> +static const struct rtc_class_ops p1_rtc_class_ops = {
> +	.read_time = p1_rtc_read_time,
> +	.set_time = p1_rtc_set_time,
> +};
> +
> +static int p1_rtc_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct rtc_device *rtc;
> +	struct p1_rtc *p1;
> +
> +	p1 = devm_kzalloc(dev, sizeof(*p1), GFP_KERNEL);
> +	if (!p1)
> +		return -ENOMEM;
> +	dev_set_drvdata(dev, p1);
> +
> +	p1->regmap = dev_get_regmap(dev->parent, NULL);
> +	if (!p1->regmap)
> +		return dev_err_probe(dev, -ENODEV, "failed to get regmap\n");
> +
> +	rtc = devm_rtc_allocate_device(dev);
> +	if (IS_ERR(rtc))
> +		return dev_err_probe(dev, PTR_ERR(rtc),
> +				     "error allocating device\n");
> +	p1->rtc = rtc;
> +
> +	rtc->ops = &p1_rtc_class_ops;
> +	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
> +	rtc->range_max = RTC_TIMESTAMP_END_2063;
> +
> +	clear_bit(RTC_FEATURE_ALARM, rtc->features);
> +	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
> +
> +	return devm_rtc_register_device(rtc);
> +}
> +
> +static struct platform_driver p1_rtc_driver = {
> +	.probe = p1_rtc_probe,
> +	.driver = {
> +		.name = MOD_NAME,
> +	},
> +};
> +
> +module_platform_driver(p1_rtc_driver);
> +
> +MODULE_DESCRIPTION("SpacemiT P1 RTC driver");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:" MOD_NAME);
> -- 
> 2.45.2
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

WARNING: multiple messages have this Message-ID (diff)
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Alex Elder <elder@riscstar.com>
Cc: lee@kernel.org, lgirdwood@gmail.com, broonie@kernel.org,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	mat.jonczyk@o2.pl, dlan@gentoo.org, paul.walmsley@sifive.com,
	palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr,
	troymitchell988@gmail.com, guodong@riscstar.com,
	linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
	linux-riscv@lists.infradead.org, spacemit@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 5/8] rtc: spacemit: support the SpacemiT P1 RTC
Date: Wed, 23 Jul 2025 23:39:56 +0200	[thread overview]
Message-ID: <20250723213956570da462@mail.local> (raw)
In-Reply-To: <20250710175107.1280221-6-elder@riscstar.com>

On 10/07/2025 12:51:03-0500, Alex Elder wrote:
> +static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
> +{
> +	struct p1_rtc *p1 = dev_get_drvdata(dev);
> +	struct regmap *regmap = p1->regmap;
> +	u32 count = RTC_READ_TRIES;
> +	u8 seconds;
> +	u8 time[6];
> +	int ret;
> +
> +	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
> +		return -ENODEV;		/* RTC is disabled */

That should be -EINVAL, as all the other drivers have standardized this.

With this fixed,
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>


> +
> +	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
> +	if (ret)
> +		return ret;
> +
> +	do {
> +		seconds = time[0];
> +		ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
> +		if (ret)
> +			return ret;
> +	} while (time[0] != seconds && --count);
> +
> +	if (!count)
> +		return -EIO;		/* Unable to get a consistent result */
> +
> +	t->tm_sec = time[0] & GENMASK(5, 0);
> +	t->tm_min = time[1] & GENMASK(5, 0);
> +	t->tm_hour = time[2] & GENMASK(4, 0);
> +	t->tm_mday = (time[3] & GENMASK(4, 0)) + 1;
> +	t->tm_mon = time[4] & GENMASK(3, 0);
> +	t->tm_year = (time[5] & GENMASK(5, 0)) + 100;
> +
> +	return 0;
> +}
> +
> +/*
> + * The P1 hardware documentation states that values in the registers are
> + * latched so when written they represent a consistent time snapshot.
> + * Nevertheless, this is not guaranteed by the implementation, so we must
> + * disable the RTC while updating it.
> + */
> +static int p1_rtc_set_time(struct device *dev, struct rtc_time *t)
> +{
> +	struct p1_rtc *p1 = dev_get_drvdata(dev);
> +	struct regmap *regmap = p1->regmap;
> +	u8 time[6];
> +	int ret;
> +
> +	time[0] = t->tm_sec;
> +	time[1] = t->tm_min;
> +	time[2] = t->tm_hour;
> +	time[3] = t->tm_mday - 1;
> +	time[4] = t->tm_mon;
> +	time[5] = t->tm_year - 100;
> +
> +	/* Disable the RTC to update; re-enable again when done */
> +	ret = regmap_clear_bits(regmap, RTC_CTRL, RTC_EN);
> +	if (ret)
> +		return ret;
> +
> +	/* If something goes wrong, leave the RTC disabled */
> +	ret = regmap_bulk_write(regmap, RTC_TIME, time, sizeof(time));
> +	if (ret)
> +		return ret;
> +
> +	return regmap_set_bits(regmap, RTC_CTRL, RTC_EN);
> +}
> +
> +static const struct rtc_class_ops p1_rtc_class_ops = {
> +	.read_time = p1_rtc_read_time,
> +	.set_time = p1_rtc_set_time,
> +};
> +
> +static int p1_rtc_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct rtc_device *rtc;
> +	struct p1_rtc *p1;
> +
> +	p1 = devm_kzalloc(dev, sizeof(*p1), GFP_KERNEL);
> +	if (!p1)
> +		return -ENOMEM;
> +	dev_set_drvdata(dev, p1);
> +
> +	p1->regmap = dev_get_regmap(dev->parent, NULL);
> +	if (!p1->regmap)
> +		return dev_err_probe(dev, -ENODEV, "failed to get regmap\n");
> +
> +	rtc = devm_rtc_allocate_device(dev);
> +	if (IS_ERR(rtc))
> +		return dev_err_probe(dev, PTR_ERR(rtc),
> +				     "error allocating device\n");
> +	p1->rtc = rtc;
> +
> +	rtc->ops = &p1_rtc_class_ops;
> +	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
> +	rtc->range_max = RTC_TIMESTAMP_END_2063;
> +
> +	clear_bit(RTC_FEATURE_ALARM, rtc->features);
> +	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
> +
> +	return devm_rtc_register_device(rtc);
> +}
> +
> +static struct platform_driver p1_rtc_driver = {
> +	.probe = p1_rtc_probe,
> +	.driver = {
> +		.name = MOD_NAME,
> +	},
> +};
> +
> +module_platform_driver(p1_rtc_driver);
> +
> +MODULE_DESCRIPTION("SpacemiT P1 RTC driver");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:" MOD_NAME);
> -- 
> 2.45.2
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2025-07-23 21:40 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-10 17:50 [PATCH v8 0/8] spacemit: introduce P1 PMIC support Alex Elder
2025-07-10 17:50 ` Alex Elder
2025-07-10 17:50 ` [PATCH v8 1/8] dt-bindings: mfd: add support the SpacemiT P1 PMIC Alex Elder
2025-07-10 17:50   ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 2/8] mfd: simple-mfd-i2c: specify max_register Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-23  9:51   ` Lee Jones
2025-07-23  9:51     ` Lee Jones
2025-07-23 12:42     ` Alex Elder
2025-07-23 12:42       ` Alex Elder
2025-07-24 10:14       ` Lee Jones
2025-07-24 10:14         ` Lee Jones
2025-07-10 17:51 ` [PATCH v8 3/8] mfd: simple-mfd-i2c: add SpacemiT P1 support Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 4/8] regulator: spacemit: support SpacemiT P1 regulators Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 5/8] rtc: spacemit: support the SpacemiT P1 RTC Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-23 21:39   ` Alexandre Belloni [this message]
2025-07-23 21:39     ` Alexandre Belloni
2025-07-23 23:37     ` Alex Elder
2025-07-23 23:37       ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 6/8] riscv: dts: spacemit: enable the i2c8 adapter Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 7/8] riscv: dts: spacemit: define fixed regulators Alex Elder
2025-07-10 17:51   ` Alex Elder
2025-07-10 17:51 ` [PATCH v8 8/8] riscv: dts: spacemit: define regulator constraints Alex Elder
2025-07-10 17:51   ` Alex Elder

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=20250723213956570da462@mail.local \
    --to=alexandre.belloni@bootlin.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlan@gentoo.org \
    --cc=elder@riscstar.com \
    --cc=guodong@riscstar.com \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=mat.jonczyk@o2.pl \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=robh@kernel.org \
    --cc=spacemit@lists.linux.dev \
    --cc=troymitchell988@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.