Linux RTC
 help / color / mirror / Atom feed
* [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support
@ 2026-11-26  7:11 Shiv Prakash Gupta
  2026-11-26  7:11 ` [PATCH v5 1/4] dt-bindings: rtc: Add NXP PCF8525 RTC Shiv Prakash Gupta
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Shiv Prakash Gupta @ 2026-11-26  7:11 UTC (permalink / raw)
  To: linux-kernel, linux-rtc, linux-hwmon, linux-watchdog, devicetree,
	alexandre.belloni, krzk+dt, robh, conor+dt, linux, wim
  Cc: vikash.bansal, priyanka.jain, lakshay.piplani, Shiv Prakash Gupta

The PCF8525 is an NXP I2C real-time clock using an external
32.768-kHz crystal. It provides alarms, external timestamp capture,
recording of the last calendar write, backup battery switch-over,
a hardware watchdog timer, and an on-chip temperature sensor.

This series introduces DT bindings, the core RTC driver, watchdog
support, and optional hardware-monitoring (temperature readout) via
the hwmon subsystem.

Changes in v5:
- Remove the reset-source property and its example entry from the
  binding; watchdog expiry is reported through INTB as an alarm-only
  watchdog.
- Remove the quartz-load-femtofarads property, as the crystal load
  capacitance is fixed and Control_5 bit 1 is reserved. Allow a single
  named inta or intb interrupt, or both in the order inta, intb, and
  use level-low interrupts in the example.
- Reject wakeup-source in the binding when only the intb interrupt is
  described, as the wakeup line is INTA.
- Program and read back the month and year alarm fields using the
  Rev. 3.0 alarm enable bit positions, and keep the alarm interrupt
  disabled while the alarm is rewritten.
- Use one helper for the CTRL2 write-0-to-clear flags.
- Mask the unused INTA sources and drain stale TSF/AF/MSF flags before
  unmasking INTA, failing probe if the drain fails. The runtime
  handler uses the same bounded drain and no longer claims an event
  whose acknowledge failed.
- Propagate I2C errors from the Timestamp1 read; only an invalid or
  empty timestamp is acknowledged so INTA cannot stay stuck low.
- Refresh the OTP after setting the time clears the oscillator-stop
  flag, even if clearing STOP fails.
- When interrupt-names is present, do not take INTA from the first
  interrupt if only INTB is named.
- Use RTC_FEATURE_ALARM_WAKEUP_ONLY when only wakeup is wired, and do
  not repeat the wakeup setup the I2C core already did.
- Set up wakeup before registering the RTC so that wakealarm is exposed
  without a wakeup-source property.
- Apply the start-year offset to the sysfs timestamps, make timestamp1
  read-only, and clear just TSF on a timestamp0 write instead of
  issuing the CTS command.
- Rate-limit the VLF and interrupt warnings, and use devm_mutex_init(),
  guard(mutex) and the regmap bit helpers.
- Register the watchdog only when the INTB interrupt is described,
  advertise WDIOF_ALARMONLY and clear WDTF in the handler.
- Mask all INTB sources before touching watchdog state, start from a
  stopped counter with the 1/4 Hz source, clear a stale WDTF and
  switch the nINTB pin last; on start, load the counter before
  enabling WD_CD and unmasking INTB, undoing it on error.
- Keep the watchdog counter and WD_CD across suspend and only mask the
  INTB route, re-arming on resume. On teardown leave the INTB route
  intact if the watchdog is still running (nowayout).
- Clear TEMP_RD_EN to enable the digital temperature readout, and drop
  the cleanup action since the bit only gates the readout register.
- Select the closest supported update_interval instead of failing with
  -EINVAL.

Changes in v4:
- Rework timestamp handling to serialize sysfs clear and IRQ
  processing, read Timestamp1 before clearing TSF, and restore TSM
  after the CTS command.
- Correct Timestamp2 handling to report the last calendar write
  instead of treating it as a second external timestamp capture.
- Rework the INTA threaded IRQ handler to drain pending AF/MSF/TSF
  events and re-read status registers, avoiding missed events on an
  edge-triggered IRQ.
- Preserve all CTRL2 Write-0-to-Clear status flags, including WDTF,
  across register read-modify-write operations.
- Rework watchdog support to use nINTB as the watchdog output without
  a Linux watchdog IRQ or pretimeout notification; remove unsupported
  pretimeout handling and write-only watchdog-value read.
- Establish a known watchdog state at probe, clear stale WDTF before
  enabling the watchdog output, and report the hardware-achieved
  timeout.
- Fix RTC feature and time-setting handling, including
  update-interrupt capability reporting and writing the
  hundredths-of-a-second register when setting the calendar.
- Improve interrupt/wakeup and device-managed cleanup ordering so
  hardware interrupt sources are safely masked on probe failure and
  driver removal.
- Rework hwmon registration and power-management handling so the
  temperature readout is enabled before exposure to userspace and
  cleanly disabled on failure, suspend, and removal.

Changes in v3:
- patch 2: protect the Write-0-to-Clear (W0C) status flags in the
  CTRL2/CTRL3 read-modify-write cycles so concurrently asserted
  interrupt events are not silently cleared
- patch 2: replace the timestamp spinlock with a mutex and re-read
  CTRL4 under it, closing the TOCTOU race between the threaded IRQ
  handler and the sysfs timestamp store
- patch 3: configure the nINTB/CLKOUT pin (CLKOE=0) before requesting
  the watchdog IRQ to avoid a spurious interrupt storm
- patch 3: add suspend/resume PM ops to stop and re-ping the watchdog
  across system sleep
- patch 3: add WDIOF_PRETIMEOUT so the watchdog core attaches a
  pretimeout governor
- patch 3: re-mask INTA/INTB interrupts on driver unbind via devm
  cleanup actions
- patch 4: enable TEMP_RD_EN before registering the hwmon device and
  add a devm cleanup to disable it on unbind

Changes in v2:
- Fix include: use <linux/pm_wakeirq.h> instead of <pm_wakeup.h>
- Add spinlock_t ts_lock to protect ts[]/ts_valid[] from IRQ/sysfs race
- Fix W0C ordering: clear AF/TSF flags before acting on alarm/timestamp
  events
- Use dev_pm_set_wake_irq() for suspend wakeup wiring
- Clear stale WDTF before unmasking watchdog interrupt on INTB to prevent
  boot loop when battery-backed flag persists across resets
- Add watchdog_notify_pretimeout() call in INTB IRQ handler
- Set WDOG_HW_RUNNING if bootloader left watchdog running at probe time
- Register hwmon device first, then enable PCF8525_CTRL5_TEMP_RD_EN only
  after successful registration

Shiv Prakash Gupta (4):
  dt-bindings: rtc: Add NXP PCF8525 RTC
  rtc: pcf8525: Add NXP PCF8525 RTC driver
  rtc: pcf8525: Add watchdog support
  rtc: pcf8525: Add temperature sensor support via hwmon

 .../devicetree/bindings/rtc/nxp,pcf8525.yaml  |   88 ++
 MAINTAINERS                                   |    8 +
 drivers/rtc/Kconfig                           |   24 +
 drivers/rtc/Makefile                          |    1 +
 drivers/rtc/rtc-pcf8525.c                     | 1404 +++++++++++++++++
 5 files changed, 1525 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml
 create mode 100644 drivers/rtc/rtc-pcf8525.c

-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 1/4] dt-bindings: rtc: Add NXP PCF8525 RTC
  2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
@ 2026-11-26  7:11 ` Shiv Prakash Gupta
  2026-11-26  7:11 ` [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver Shiv Prakash Gupta
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Shiv Prakash Gupta @ 2026-11-26  7:11 UTC (permalink / raw)
  To: linux-kernel, linux-rtc, linux-hwmon, linux-watchdog, devicetree,
	alexandre.belloni, krzk+dt, robh, conor+dt, linux, wim
  Cc: vikash.bansal, priyanka.jain, lakshay.piplani, Shiv Prakash Gupta

Add a Devicetree binding for the NXP PCF8525 I2C RTC. Describe the
optional INTA interrupt used for RTC alarm and timestamp events, and
the INTB interrupt used by the watchdog.

Add a property to select the temperature-compensation crystal model.
Also allow the common start-year and wakeup-source properties.

Signed-off-by: Lakshay Piplani <lakshay.piplani@nxp.com>
Signed-off-by: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
---
 .../devicetree/bindings/rtc/nxp,pcf8525.yaml  | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml

diff --git a/Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml b/Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml
new file mode 100644
index 000000000000..a2fe4c2d36a1
--- /dev/null
+++ b/Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/nxp,pcf8525.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NXP PCF8525 Real Time Clock
+
+description:
+  The PCF8525 is an I2C real-time clock with alarm, timestamp, watchdog,
+  backup battery switch-over, and temperature compensation for an external
+  32.768 kHz crystal.
+
+allOf:
+  - $ref: rtc.yaml#
+  - if:
+      required:
+        - wakeup-source
+        - interrupt-names
+    then:
+      properties:
+        interrupt-names:
+          contains:
+            const: inta
+
+maintainers:
+  - Lakshay Piplani <lakshay.piplani@nxp.com>
+  - Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
+
+properties:
+  compatible:
+    enum:
+      - nxp,pcf8525
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    maxItems: 2
+    description:
+      One interrupt may be INTA or INTB as identified by interrupt-names.
+      When both outputs are connected, INTA is followed by INTB.
+
+  interrupt-names:
+    oneOf:
+      - items:
+          - enum: [inta, intb]
+      - items:
+          - const: inta
+          - const: intb
+
+  nxp,xtal-type:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      Crystal model used by the temperature compensation engine.
+      Value 1 selects -0.035 ppm/degC^2 and value 2 selects
+      -0.04 ppm/degC^2.
+    enum: [1, 2]
+    default: 1
+
+  start-year: true
+
+  wakeup-source: true
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        rtc@51 {
+            compatible = "nxp,pcf8525";
+            reg = <0x51>;
+            interrupt-parent = <&gpio2>;
+            interrupts = <2 IRQ_TYPE_LEVEL_LOW>,
+                          <3 IRQ_TYPE_LEVEL_LOW>;
+            interrupt-names = "inta", "intb";
+            nxp,xtal-type = <2>;
+        };
+    };
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver
  2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
  2026-11-26  7:11 ` [PATCH v5 1/4] dt-bindings: rtc: Add NXP PCF8525 RTC Shiv Prakash Gupta
@ 2026-11-26  7:11 ` Shiv Prakash Gupta
  2026-11-26  7:30   ` sashiko-bot
  2026-11-26  7:11 ` [PATCH v5 3/4] rtc: pcf8525: Add watchdog support Shiv Prakash Gupta
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Shiv Prakash Gupta @ 2026-11-26  7:11 UTC (permalink / raw)
  To: linux-kernel, linux-rtc, linux-hwmon, linux-watchdog, devicetree,
	alexandre.belloni, krzk+dt, robh, conor+dt, linux, wim
  Cc: vikash.bansal, priyanka.jain, lakshay.piplani, Shiv Prakash Gupta

Add a driver for the NXP PCF8525 I2C real-time clock.

Signed-off-by: Lakshay Piplani <lakshay.piplani@nxp.com>
Signed-off-by: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
---
 MAINTAINERS               |   8 +
 drivers/rtc/Kconfig       |  12 +
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-pcf8525.c | 931 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 952 insertions(+)
 create mode 100644 drivers/rtc/rtc-pcf8525.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b756e..2b580150feab 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20331,6 +20331,14 @@ L:	linux-i3c@lists.infradead.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/i3c/nxp,p3h2840.yaml
 
+NXP PCF8525 RTC DRIVER
+M:	Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
+M:	Lakshay Piplani <lakshay.piplani@nxp.com>
+L:	linux-rtc@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/rtc/nxp,pcf8525.yaml
+F:	drivers/rtc/rtc-pcf8525.c
+
 NXP PF5300/PF5301/PF5302 PMIC REGULATOR DEVICE DRIVER
 M:	Woodrow Douglass <wdouglass@carnegierobotics.com>
 S:	Maintained
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 73ec9230edfd..848bcde41f2a 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -523,6 +523,18 @@ config RTC_DRV_PCF8523
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-pcf8523.
 
+config RTC_DRV_PCF8525
+	tristate "NXP PCF8525"
+	select REGMAP_I2C
+	help
+	  If you say yes here you get support for the NXP PCF8525 RTC chip.
+	  The PCF8525 is an I2C real-time clock with alarm, external timestamp
+	  capture and recording of the last calendar write, crystal aging offset
+	  correction, and backup power switchover.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called rtc-pcf8525.
+
 config RTC_DRV_PCF85363
 	tristate "NXP PCF85363"
 	select REGMAP_I2C
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 46f1a0fc2416..08e0d36d7cb9 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -132,6 +132,7 @@ obj-$(CONFIG_RTC_DRV_PCF2123)	+= rtc-pcf2123.o
 obj-$(CONFIG_RTC_DRV_PCF2127)	+= rtc-pcf2127.o
 obj-$(CONFIG_RTC_DRV_PCF85063)	+= rtc-pcf85063.o
 obj-$(CONFIG_RTC_DRV_PCF8523)	+= rtc-pcf8523.o
+obj-$(CONFIG_RTC_DRV_PCF8525)	+= rtc-pcf8525.o
 obj-$(CONFIG_RTC_DRV_PCF85363)	+= rtc-pcf85363.o
 obj-$(CONFIG_RTC_DRV_PCF8563)	+= rtc-pcf8563.o
 obj-$(CONFIG_RTC_DRV_PCF8583)	+= rtc-pcf8583.o
diff --git a/drivers/rtc/rtc-pcf8525.c b/drivers/rtc/rtc-pcf8525.c
new file mode 100644
index 000000000000..2b23b17d6750
--- /dev/null
+++ b/drivers/rtc/rtc-pcf8525.c
@@ -0,0 +1,931 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * NXP PCF8525 RTC driver with alarm and timestamp support.
+ *
+ * Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF8525.pdf
+ */
+
+#include <linux/bcd.h>
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/limits.h>
+#include <linux/math.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm_wakeirq.h>
+#include <linux/pm_wakeup.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/rtc.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+#include <linux/uaccess.h>
+
+#define PCF8525_REG_CTRL1		0x00
+#define PCF8525_REG_CTRL2		0x01
+#define PCF8525_REG_CTRL3		0x02
+#define PCF8525_REG_CTRL4		0x03
+#define PCF8525_REG_CTRL5		0x04
+#define PCF8525_REG_RESET		0x05
+
+#define PCF8525_REG_100THS		0x06
+#define PCF8525_REG_SECONDS		0x07
+
+#define PCF8525_REG_ALM_SEC		0x0E
+
+#define PCF8525_REG_CLKOUT		0x15
+
+#define PCF8525_REG_TS_CTL1		0x16
+#define PCF8525_REG_TS1_SEC		0x17
+#define PCF8525_REG_TS2_SEC		0x1E
+
+#define PCF8525_REG_AGING_OFFSET_HI	0x26
+
+#define PCF8525_REG_INTA_MASK1		0x28
+#define PCF8525_REG_INTA_MASK2		0x29
+
+#define PCF8525_REG_TEMP		0x2E
+
+#define PCF8525_CTRL1_STOP		BIT(5)
+#define PCF8525_CTRL1_12_24		BIT(2)
+
+#define PCF8525_CTRL2_MSF		BIT(7)
+#define PCF8525_CTRL2_WDTF		BIT(5)
+#define PCF8525_CTRL2_AF		BIT(4)
+#define PCF8525_CTRL2_AIE		BIT(1)
+#define PCF8525_CTRL2_W0C_FLAGS		(PCF8525_CTRL2_MSF | \
+					 PCF8525_CTRL2_WDTF | \
+					 PCF8525_CTRL2_AF)
+
+#define PCF8525_CTRL3_PWRMNG_MASK	GENMASK(6, 5)
+#define PCF8525_CTRL3_BF		BIT(3)
+
+#define PCF8525_PWRMNG_STANDARD		0
+#define PCF8525_PWRMNG_DIRECT		1
+#define PCF8525_PWRMNG_DISABLED		2
+
+#define PCF8525_CTRL4_TSF		BIT(7)
+
+#define PCF8525_CTRL5_TSIE		BIT(7)
+#define PCF8525_CTRL5_XTL_TYP		BIT(0)
+
+#define PCF8525_CLKOUT_OTPR		BIT(4)
+
+#define PCF8525_TS_CTL1_TSM		BIT(7)
+#define PCF8525_TS_CTL1_TSOFF		BIT(6)
+
+#define PCF8525_SC_OSF			BIT(7)
+#define PCF8525_MN_VLF			BIT(7)
+
+#define PCF8525_ALM_AE			BIT(7)
+#define PCF8525_ALM_AE_YEAR		BIT(7)
+#define PCF8525_ALM_AE_MONTH		BIT(6)
+
+#define PCF8525_RESET_CPR_CMD		0xA4
+
+/* A set bit in the INTx_MASKy registers masks the interrupt source. */
+#define PCF8525_MASK1_AIE		BIT(2)
+#define PCF8525_MASK1_ALL		GENMASK(5, 0)
+#define PCF8525_MASK2_TSIE		BIT(3)
+
+#define PCF8525_AGING_STEP_NUM		298L
+#define PCF8525_AGING_STEP_DEN		10L
+
+#define PCF8525_IRQ_MAX_PASSES		16
+
+struct pcf8525 {
+	struct rtc_device *rtc;
+	struct regmap *regmap;
+	struct mutex ts_mutex;	/* protects the TS1 cache and TSF */
+	time64_t ts;
+	bool ts_valid;
+	bool irq_enabled;
+};
+
+static const struct regmap_config pcf8525_regmap_cfg = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = PCF8525_REG_TEMP,
+};
+
+/*
+ * MSF, WDTF and AF are write-0-to-clear. Write 1 to every flag not in @mask
+ * so that a flag set in the meantime is not lost.
+ */
+static int pcf8525_update_ctrl2(struct pcf8525 *pcf8525, unsigned int mask,
+				unsigned int val)
+{
+	return regmap_update_bits(pcf8525->regmap, PCF8525_REG_CTRL2,
+				  mask | PCF8525_CTRL2_W0C_FLAGS,
+				  val | (PCF8525_CTRL2_W0C_FLAGS & ~mask));
+}
+
+static int pcf8525_configure_crystal(struct device *dev,
+				     struct pcf8525 *pcf8525)
+{
+	u32 val;
+
+	if (!device_property_read_u32(dev, "nxp,xtal-type", &val)) {
+		if (val == 1 || val == 2)
+			return regmap_assign_bits(pcf8525->regmap,
+						  PCF8525_REG_CTRL5,
+						  PCF8525_CTRL5_XTL_TYP,
+						  val == 2);
+
+		dev_warn(dev, "unsupported nxp,xtal-type %u\n", val);
+	}
+
+	return 0;
+}
+
+static int pcf8525_otp_refresh(struct pcf8525 *pcf8525)
+{
+	unsigned int val;
+	int ret;
+
+	ret = regmap_clear_bits(pcf8525->regmap, PCF8525_REG_CLKOUT,
+				PCF8525_CLKOUT_OTPR);
+	if (ret)
+		return ret;
+
+	ret = regmap_set_bits(pcf8525->regmap, PCF8525_REG_CLKOUT,
+			      PCF8525_CLKOUT_OTPR);
+	if (ret)
+		return ret;
+
+	return regmap_read_poll_timeout(pcf8525->regmap, PCF8525_REG_CLKOUT,
+				val, val & PCF8525_CLKOUT_OTPR,
+				1000, 100000);
+}
+
+static int pcf8525_rtc_read_offset(struct device *dev, long *offset)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	u8 buf[2];
+	s16 raw;
+	int ret;
+
+	ret = regmap_bulk_read(pcf8525->regmap, PCF8525_REG_AGING_OFFSET_HI,
+			       buf, sizeof(buf));
+	if (ret)
+		return ret;
+
+	raw = (s16)((buf[0] << 8) | buf[1]);
+	*offset = DIV_ROUND_CLOSEST((long)raw * PCF8525_AGING_STEP_NUM,
+				    PCF8525_AGING_STEP_DEN);
+
+	return 0;
+}
+
+static int pcf8525_rtc_set_offset(struct device *dev, long offset)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	long min_ppb, max_ppb, raw;
+	u8 buf[2];
+
+	min_ppb = DIV_ROUND_CLOSEST(S16_MIN * PCF8525_AGING_STEP_NUM,
+				    PCF8525_AGING_STEP_DEN);
+	max_ppb = DIV_ROUND_CLOSEST(S16_MAX * PCF8525_AGING_STEP_NUM,
+				    PCF8525_AGING_STEP_DEN);
+	if (offset < min_ppb || offset > max_ppb)
+		return -ERANGE;
+
+	raw = DIV_ROUND_CLOSEST(offset * PCF8525_AGING_STEP_DEN,
+				PCF8525_AGING_STEP_NUM);
+	if (raw < S16_MIN || raw > S16_MAX)
+		return -ERANGE;
+
+	buf[0] = (raw >> 8) & 0xff;
+	buf[1] = raw & 0xff;
+
+	/* The new correction takes effect once the low byte is written. */
+	return regmap_bulk_write(pcf8525->regmap, PCF8525_REG_AGING_OFFSET_HI,
+				 buf, sizeof(buf));
+}
+
+static int pcf8525_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	u8 buf[7];
+	int ret;
+
+	ret = regmap_bulk_read(pcf8525->regmap, PCF8525_REG_SECONDS, buf,
+			       sizeof(buf));
+	if (ret)
+		return ret;
+
+	if (buf[0] & PCF8525_SC_OSF)
+		return -EINVAL;
+
+	if (buf[1] & PCF8525_MN_VLF)
+		dev_warn_ratelimited(dev, "VLF set: clock integrity not guaranteed\n");
+
+	tm->tm_sec = bcd2bin(buf[0] & 0x7f);
+	tm->tm_min = bcd2bin(buf[1] & 0x7f);
+	tm->tm_hour = bcd2bin(buf[2] & 0x3f);
+	tm->tm_mday = bcd2bin(buf[3] & 0x3f);
+	tm->tm_wday = buf[4] & 0x07;
+	tm->tm_mon = bcd2bin(buf[5] & 0x1f) - 1;
+	tm->tm_year = bcd2bin(buf[6]) + 100;
+
+	return rtc_valid_tm(tm);
+}
+
+static int pcf8525_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	u8 old_time[7];
+	u8 buf[8];
+	bool refresh_otp;
+	int ret, ret2;
+
+	/*
+	 * Remember OSF, as clearing it needs an OTP refresh. Read the whole
+	 * calendar in one transaction.
+	 */
+	ret = regmap_bulk_read(pcf8525->regmap, PCF8525_REG_SECONDS,
+			       old_time, sizeof(old_time));
+	if (ret)
+		return ret;
+
+	refresh_otp = old_time[0] & PCF8525_SC_OSF;
+
+	ret = regmap_set_bits(pcf8525->regmap, PCF8525_REG_CTRL1,
+			      PCF8525_CTRL1_STOP);
+	if (ret)
+		return ret;
+
+	ret = regmap_write(pcf8525->regmap, PCF8525_REG_RESET,
+			   PCF8525_RESET_CPR_CMD);
+	if (!ret) {
+		buf[0] = 0;
+		buf[1] = bin2bcd(tm->tm_sec);
+		buf[2] = bin2bcd(tm->tm_min);
+		buf[3] = bin2bcd(tm->tm_hour);
+		buf[4] = bin2bcd(tm->tm_mday);
+		buf[5] = tm->tm_wday & 0x07;
+		buf[6] = bin2bcd(tm->tm_mon + 1);
+		buf[7] = bin2bcd(tm->tm_year - 100);
+
+		ret = regmap_bulk_write(pcf8525->regmap, PCF8525_REG_100THS,
+					buf, sizeof(buf));
+	}
+
+	ret2 = regmap_clear_bits(pcf8525->regmap, PCF8525_REG_CTRL1,
+				 PCF8525_CTRL1_STOP);
+
+	/* The write cleared OSF, so refresh the OTP even if STOP failed. */
+	if (!ret && refresh_otp) {
+		int otp_ret = pcf8525_otp_refresh(pcf8525);
+
+		if (!ret2)
+			ret2 = otp_ret;
+	}
+
+	if (ret)
+		return ret;
+
+	return ret2;
+}
+
+static int pcf8525_rtc_ioctl(struct device *dev, unsigned int cmd,
+			     unsigned long arg)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	unsigned int flags = 0;
+	unsigned int val;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL3, &val);
+		if (ret)
+			return ret;
+
+		if (val & PCF8525_CTRL3_BF)
+			flags |= RTC_VL_BACKUP_SWITCH;
+
+		return put_user(flags, (unsigned int __user *)arg);
+	case RTC_VL_CLR:
+		return regmap_clear_bits(pcf8525->regmap, PCF8525_REG_CTRL3,
+					 PCF8525_CTRL3_BF);
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
+static int pcf8525_param_get(struct device *dev, struct rtc_param *param)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	unsigned int val;
+	int ret;
+
+	if (param->param != RTC_PARAM_BACKUP_SWITCH_MODE)
+		return -EINVAL;
+
+	ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL3, &val);
+	if (ret)
+		return ret;
+
+	switch (FIELD_GET(PCF8525_CTRL3_PWRMNG_MASK, val)) {
+	case PCF8525_PWRMNG_STANDARD:
+		param->uvalue = RTC_BSM_LEVEL;
+		break;
+	case PCF8525_PWRMNG_DIRECT:
+		param->uvalue = RTC_BSM_DIRECT;
+		break;
+	default:
+		param->uvalue = RTC_BSM_DISABLED;
+		break;
+	}
+
+	return 0;
+}
+
+static int pcf8525_param_set(struct device *dev, struct rtc_param *param)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	unsigned int mode;
+
+	if (param->param != RTC_PARAM_BACKUP_SWITCH_MODE)
+		return -EINVAL;
+
+	switch (param->uvalue) {
+	case RTC_BSM_LEVEL:
+		mode = PCF8525_PWRMNG_STANDARD;
+		break;
+	case RTC_BSM_DIRECT:
+		mode = PCF8525_PWRMNG_DIRECT;
+		break;
+	case RTC_BSM_DISABLED:
+		mode = PCF8525_PWRMNG_DISABLED;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	/* BF is write-0-to-clear, so write 1 to keep a pending flag. */
+	return regmap_update_bits(pcf8525->regmap, PCF8525_REG_CTRL3,
+				  PCF8525_CTRL3_PWRMNG_MASK | PCF8525_CTRL3_BF,
+				  FIELD_PREP(PCF8525_CTRL3_PWRMNG_MASK, mode) |
+				  PCF8525_CTRL3_BF);
+}
+
+static int pcf8525_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	struct rtc_time *tm = &alrm->time;
+	unsigned int ctrl2;
+	u8 buf[7];
+	int ret;
+
+	ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL2, &ctrl2);
+	if (ret)
+		return ret;
+
+	ret = regmap_bulk_read(pcf8525->regmap, PCF8525_REG_ALM_SEC, buf,
+			       sizeof(buf));
+	if (ret)
+		return ret;
+
+	alrm->enabled = !!(ctrl2 & PCF8525_CTRL2_AIE);
+	alrm->pending = !!(ctrl2 & PCF8525_CTRL2_AF);
+
+	/* Fields with the enable bit cleared are matched; the rest stay -1. */
+	if (!(buf[0] & PCF8525_ALM_AE))
+		tm->tm_sec = bcd2bin(buf[0] & 0x7f);
+	if (!(buf[1] & PCF8525_ALM_AE))
+		tm->tm_min = bcd2bin(buf[1] & 0x7f);
+	if (!(buf[2] & PCF8525_ALM_AE))
+		tm->tm_hour = bcd2bin(buf[2] & 0x3f);
+	if (!(buf[3] & PCF8525_ALM_AE))
+		tm->tm_mday = bcd2bin(buf[3] & 0x3f);
+	if (!(buf[5] & PCF8525_ALM_AE_MONTH))
+		tm->tm_mon = bcd2bin(buf[5] & 0x1f) - 1;
+	if (!(buf[5] & PCF8525_ALM_AE_YEAR))
+		tm->tm_year = bcd2bin(buf[6]) + 100;
+
+	return 0;
+}
+
+static int pcf8525_alarm_irq_enable(struct device *dev, unsigned int enable)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+
+	return pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_AIE,
+				    enable ? PCF8525_CTRL2_AIE : 0);
+}
+
+static int pcf8525_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	struct rtc_time *tm = &alrm->time;
+	u8 buf[7];
+	int ret;
+
+	/* Keep the alarm disabled while the registers are being rewritten. */
+	ret = pcf8525_alarm_irq_enable(dev, 0);
+	if (ret)
+		return ret;
+
+	buf[0] = bin2bcd(tm->tm_sec);
+	buf[1] = bin2bcd(tm->tm_min);
+	buf[2] = bin2bcd(tm->tm_hour);
+	buf[3] = bin2bcd(tm->tm_mday);
+	buf[4] = PCF8525_ALM_AE;
+	buf[5] = bin2bcd(tm->tm_mon + 1);
+	buf[6] = bin2bcd(tm->tm_year - 100);
+
+	ret = regmap_bulk_write(pcf8525->regmap, PCF8525_REG_ALM_SEC, buf,
+				sizeof(buf));
+	if (ret)
+		return ret;
+
+	ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_AF, 0);
+	if (ret)
+		return ret;
+
+	return pcf8525_alarm_irq_enable(dev, alrm->enabled);
+}
+
+static int pcf8525_ts_read(struct pcf8525 *pcf8525, unsigned int reg,
+			   time64_t *ts)
+{
+	struct rtc_time tm = { };
+	u8 buf[6];
+	int ret;
+
+	ret = regmap_bulk_read(pcf8525->regmap, reg, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
+	/* The timestamps read as all zero until they have been captured. */
+	if (!(buf[3] & 0x3f) && !(buf[4] & 0x1f))
+		return -ENODATA;
+
+	tm.tm_sec = bcd2bin(buf[0] & 0x7f);
+	tm.tm_min = bcd2bin(buf[1] & 0x7f);
+	tm.tm_hour = bcd2bin(buf[2] & 0x3f);
+	tm.tm_mday = bcd2bin(buf[3] & 0x3f);
+	tm.tm_mon = bcd2bin(buf[4] & 0x1f) - 1;
+	tm.tm_year = bcd2bin(buf[5]) + 100;
+
+	ret = rtc_valid_tm(&tm);
+	if (ret)
+		return ret;
+
+	*ts = rtc_tm_to_time64(&tm);
+
+	return 0;
+}
+
+/* The RTC core only applies the start-year offset to calendar reads. */
+static time64_t pcf8525_ts_apply_offset(struct pcf8525 *pcf8525, time64_t ts)
+{
+	struct rtc_device *rtc = pcf8525->rtc;
+
+	if (!rtc->offset_secs)
+		return ts;
+
+	if ((rtc->start_secs > rtc->range_min && ts >= rtc->start_secs) ||
+	    (rtc->start_secs < rtc->range_min &&
+	     ts <= rtc->start_secs + rtc->range_max - rtc->range_min))
+		return ts;
+
+	return ts + rtc->offset_secs;
+}
+
+static ssize_t timestamp0_show(struct device *dev,
+			       struct device_attribute *attr, char *buf)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev->parent);
+	unsigned int ctrl4;
+	time64_t ts = 0;
+	int ret;
+
+	guard(mutex)(&pcf8525->ts_mutex);
+
+	if (pcf8525->irq_enabled) {
+		if (!pcf8525->ts_valid)
+			return 0;
+
+		ts = pcf8525->ts;
+	} else {
+		ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL4, &ctrl4);
+		if (ret)
+			return ret;
+
+		if (!(ctrl4 & PCF8525_CTRL4_TSF))
+			return 0;
+
+		ret = pcf8525_ts_read(pcf8525, PCF8525_REG_TS1_SEC, &ts);
+		if (ret)
+			return ret == -ENODATA ? 0 : ret;
+	}
+
+	return sysfs_emit(buf, "%lld\n",
+			  (long long)pcf8525_ts_apply_offset(pcf8525, ts));
+}
+
+static ssize_t timestamp0_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev->parent);
+	int ret;
+
+	/* Clear TSF only; the CTS command would also clear TS2. */
+	guard(mutex)(&pcf8525->ts_mutex);
+
+	ret = regmap_write(pcf8525->regmap, PCF8525_REG_CTRL4, 0);
+	if (ret)
+		return ret;
+
+	pcf8525->ts_valid = false;
+
+	return count;
+}
+
+static ssize_t timestamp1_show(struct device *dev,
+			       struct device_attribute *attr, char *buf)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev->parent);
+	time64_t ts = 0;
+	int ret;
+
+	/* TS2 holds the time of the last calendar write. */
+	ret = pcf8525_ts_read(pcf8525, PCF8525_REG_TS2_SEC, &ts);
+	if (ret)
+		return ret == -ENODATA ? 0 : ret;
+
+	return sysfs_emit(buf, "%lld\n",
+			  (long long)pcf8525_ts_apply_offset(pcf8525, ts));
+}
+
+static DEVICE_ATTR_RW(timestamp0);
+static DEVICE_ATTR_RO(timestamp1);
+
+static struct attribute *pcf8525_attrs[] = {
+	&dev_attr_timestamp0.attr,
+	&dev_attr_timestamp1.attr,
+	NULL
+};
+
+static const struct attribute_group pcf8525_attr_group = {
+	.attrs = pcf8525_attrs,
+};
+
+static int pcf8525_handle_ts(struct device *dev, struct pcf8525 *pcf8525)
+{
+	unsigned int ctrl4;
+	time64_t ts = 0;
+	int ret, err;
+
+	guard(mutex)(&pcf8525->ts_mutex);
+
+	ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL4, &ctrl4);
+	if (ret)
+		return ret;
+
+	/* User space may have acknowledged the flag while we waited. */
+	if (!(ctrl4 & PCF8525_CTRL4_TSF))
+		return 0;
+
+	/* Read TS1 before clearing TSF, which re-arms the capture. */
+	err = pcf8525_ts_read(pcf8525, PCF8525_REG_TS1_SEC, &ts);
+	if (err && err != -ENODATA && err != -EINVAL)
+		return err;
+	if (err)
+		dev_warn_ratelimited(dev, "unusable timestamp: %d\n", err);
+
+	/* Invalid timestamp data must not leave the level-low INTA stuck. */
+	ret = regmap_write(pcf8525->regmap, PCF8525_REG_CTRL4, 0);
+	if (ret)
+		return ret;
+
+	if (!err && !pcf8525->ts_valid) {
+		pcf8525->ts = ts;
+		pcf8525->ts_valid = true;
+	}
+
+	return 0;
+}
+
+static int pcf8525_service_inta(struct device *dev, struct pcf8525 *pcf8525,
+				bool *handled)
+{
+	unsigned int ctrl2, ctrl4;
+	int i, ret;
+
+	*handled = false;
+
+	/* Flags can be set again while INTA is low, so re-read until clean. */
+	for (i = 0; i < PCF8525_IRQ_MAX_PASSES; i++) {
+		ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL2, &ctrl2);
+		if (ret)
+			return ret;
+
+		ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL4, &ctrl4);
+		if (ret)
+			return ret;
+
+		if (!(ctrl2 & (PCF8525_CTRL2_AF | PCF8525_CTRL2_MSF)) &&
+		    !(ctrl4 & PCF8525_CTRL4_TSF))
+			return 0;
+
+		if (ctrl4 & PCF8525_CTRL4_TSF) {
+			ret = pcf8525_handle_ts(dev, pcf8525);
+			if (ret)
+				return ret;
+
+			*handled = true;
+		}
+
+		if (ctrl2 & PCF8525_CTRL2_AF) {
+			ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_AF, 0);
+			if (ret)
+				return ret;
+
+			rtc_update_irq(pcf8525->rtc, 1, RTC_IRQF | RTC_AF);
+			*handled = true;
+		}
+
+		if (ctrl2 & PCF8525_CTRL2_MSF) {
+			ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_MSF, 0);
+			if (ret)
+				return ret;
+
+			*handled = true;
+		}
+	}
+
+	return -EBUSY;
+}
+
+static irqreturn_t pcf8525_irq(int irq, void *data)
+{
+	struct device *dev = data;
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	bool handled;
+	int ret;
+
+	ret = pcf8525_service_inta(dev, pcf8525, &handled);
+	if (ret) {
+		dev_err_ratelimited(dev, "failed to service INTA: %d\n", ret);
+		return IRQ_NONE;
+	}
+
+	return handled ? IRQ_HANDLED : IRQ_NONE;
+}
+
+static const struct rtc_class_ops pcf8525_rtc_ops = {
+	.ioctl = pcf8525_rtc_ioctl,
+	.read_time = pcf8525_read_time,
+	.set_time = pcf8525_set_time,
+	.read_alarm = pcf8525_read_alarm,
+	.set_alarm = pcf8525_set_alarm,
+	.alarm_irq_enable = pcf8525_alarm_irq_enable,
+	.read_offset = pcf8525_rtc_read_offset,
+	.set_offset = pcf8525_rtc_set_offset,
+	.param_get = pcf8525_param_get,
+	.param_set = pcf8525_param_set,
+};
+
+static int pcf8525_get_inta_irq(struct i2c_client *client)
+{
+	struct fwnode_handle *fwnode = dev_fwnode(&client->dev);
+	int irq;
+
+	irq = fwnode_irq_get_byname(fwnode, "inta");
+	if (irq >= 0 || irq == -EPROBE_DEFER)
+		return irq;
+
+	if (irq != -ENOENT && irq != -EINVAL && irq != -ENODATA &&
+	    irq != -ENXIO)
+		return irq;
+
+	if (fwnode_property_present(fwnode, "interrupt-names"))
+		return 0;
+
+	/* Legacy firmware without interrupt-names uses the first IRQ as INTA. */
+	return client->irq;
+}
+
+static int pcf8525_mask_irqs_inta(struct pcf8525 *pcf8525)
+{
+	int ret;
+
+	ret = regmap_set_bits(pcf8525->regmap, PCF8525_REG_INTA_MASK1,
+			      PCF8525_MASK1_ALL);
+	if (ret)
+		return ret;
+
+	return regmap_set_bits(pcf8525->regmap, PCF8525_REG_INTA_MASK2,
+			       PCF8525_MASK2_TSIE);
+}
+
+static void pcf8525_mask_inta(void *data)
+{
+	pcf8525_mask_irqs_inta(data);
+}
+
+static int pcf8525_unmask_irqs_inta(struct pcf8525 *pcf8525, bool timestamp)
+{
+	int ret;
+
+	ret = regmap_update_bits(pcf8525->regmap, PCF8525_REG_INTA_MASK1,
+				 PCF8525_MASK1_ALL,
+				 PCF8525_MASK1_ALL & ~PCF8525_MASK1_AIE);
+	if (ret)
+		return ret;
+
+	if (!timestamp)
+		return 0;
+
+	return regmap_clear_bits(pcf8525->regmap, PCF8525_REG_INTA_MASK2,
+				 PCF8525_MASK2_TSIE);
+}
+
+static int pcf8525_setup_inta(struct i2c_client *client,
+			      struct pcf8525 *pcf8525, int irq)
+{
+	struct device *dev = &client->dev;
+	bool handled;
+	int ret;
+
+	/* INTA is still masked, so drain all stale flags before enabling it. */
+	ret = pcf8525_service_inta(dev, pcf8525, &handled);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to drain pending INTA\n");
+
+	ret = devm_request_threaded_irq(dev, irq, NULL, pcf8525_irq,
+					IRQF_ONESHOT, dev_name(dev), dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to request INTA IRQ\n");
+
+	pcf8525->irq_enabled = true;
+
+	ret = devm_add_action_or_reset(dev, pcf8525_mask_inta, pcf8525);
+	if (ret)
+		return ret;
+
+	ret = pcf8525_unmask_irqs_inta(pcf8525, true);
+	if (ret)
+		return ret;
+
+	/* The I2C core already set up wakeup for wakeup-source devices. */
+	if (client->flags & I2C_CLIENT_WAKE)
+		return 0;
+
+	return devm_pm_set_wake_irq(dev, irq);
+}
+
+static int pcf8525_setup_wakeup_only(struct device *dev,
+				     struct pcf8525 *pcf8525)
+{
+	int ret;
+
+	ret = devm_add_action_or_reset(dev, pcf8525_mask_inta, pcf8525);
+	if (ret)
+		return ret;
+
+	ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_AF, 0);
+	if (ret)
+		return ret;
+
+	return pcf8525_unmask_irqs_inta(pcf8525, false);
+}
+
+static int pcf8525_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct pcf8525 *pcf8525;
+	int irq, ret;
+	bool wakeup;
+
+	pcf8525 = devm_kzalloc_obj(dev, *pcf8525);
+	if (!pcf8525)
+		return -ENOMEM;
+
+	wakeup = device_property_read_bool(dev, "wakeup-source");
+
+	ret = devm_mutex_init(dev, &pcf8525->ts_mutex);
+	if (ret)
+		return ret;
+
+	pcf8525->regmap = devm_regmap_init_i2c(client, &pcf8525_regmap_cfg);
+	if (IS_ERR(pcf8525->regmap))
+		return dev_err_probe(dev, PTR_ERR(pcf8525->regmap),
+				     "failed to init regmap\n");
+
+	i2c_set_clientdata(client, pcf8525);
+
+	irq = pcf8525_get_inta_irq(client);
+	if (irq < 0)
+		return dev_err_probe(dev, irq, "failed to get INTA IRQ\n");
+
+	/* Isolate stale and unused INTA sources before enabling timestamps. */
+	if (irq > 0 || wakeup) {
+		ret = pcf8525_mask_irqs_inta(pcf8525);
+		if (ret)
+			return ret;
+	}
+
+	pcf8525->rtc = devm_rtc_allocate_device(dev);
+	if (IS_ERR(pcf8525->rtc))
+		return dev_err_probe(dev, PTR_ERR(pcf8525->rtc),
+				     "failed to allocate RTC device\n");
+
+	pcf8525->rtc->ops = &pcf8525_rtc_ops;
+	pcf8525->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
+	pcf8525->rtc->range_max = RTC_TIMESTAMP_END_2099;
+
+	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf8525->rtc->features);
+	set_bit(RTC_FEATURE_BACKUP_SWITCH_MODE, pcf8525->rtc->features);
+	if (irq <= 0 && !wakeup)
+		clear_bit(RTC_FEATURE_ALARM, pcf8525->rtc->features);
+	else if (irq <= 0)
+		set_bit(RTC_FEATURE_ALARM_WAKEUP_ONLY, pcf8525->rtc->features);
+
+	ret = regmap_clear_bits(pcf8525->regmap, PCF8525_REG_CTRL1,
+				PCF8525_CTRL1_12_24);
+	if (ret)
+		return ret;
+
+	ret = pcf8525_configure_crystal(dev, pcf8525);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(pcf8525->regmap, PCF8525_REG_TS_CTL1,
+				 PCF8525_TS_CTL1_TSM | PCF8525_TS_CTL1_TSOFF,
+				 PCF8525_TS_CTL1_TSM);
+	if (ret)
+		return ret;
+
+	ret = regmap_assign_bits(pcf8525->regmap, PCF8525_REG_CTRL5,
+				 PCF8525_CTRL5_TSIE, irq > 0);
+	if (ret)
+		return ret;
+
+	ret = rtc_add_group(pcf8525->rtc, &pcf8525_attr_group);
+	if (ret)
+		return ret;
+
+	/* Wakeup must be set up before registering so wakealarm is exposed. */
+	if (irq > 0 && !(client->flags & I2C_CLIENT_WAKE)) {
+		ret = devm_device_init_wakeup(dev);
+		if (ret)
+			return ret;
+	}
+
+	ret = devm_rtc_register_device(pcf8525->rtc);
+	if (ret)
+		return ret;
+
+	if (irq > 0) {
+		ret = pcf8525_setup_inta(client, pcf8525, irq);
+		if (ret)
+			return ret;
+	} else if (wakeup) {
+		ret = pcf8525_setup_wakeup_only(dev, pcf8525);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static const struct of_device_id pcf8525_of_match[] = {
+	{ .compatible = "nxp,pcf8525" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pcf8525_of_match);
+
+static const struct i2c_device_id pcf8525_i2c_id[] = {
+	{ .name = "pcf8525" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, pcf8525_i2c_id);
+
+static struct i2c_driver pcf8525_driver = {
+	.driver = {
+		.name = "rtc-pcf8525",
+		.of_match_table = pcf8525_of_match,
+	},
+	.probe = pcf8525_probe,
+	.id_table = pcf8525_i2c_id,
+};
+module_i2c_driver(pcf8525_driver);
+
+MODULE_AUTHOR("Lakshay Piplani <lakshay.piplani@nxp.com>");
+MODULE_AUTHOR("Shiv Prakash Gupta <shivprakash.gupta@nxp.com>");
+MODULE_DESCRIPTION("NXP PCF8525 RTC driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 3/4] rtc: pcf8525: Add watchdog support
  2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
  2026-11-26  7:11 ` [PATCH v5 1/4] dt-bindings: rtc: Add NXP PCF8525 RTC Shiv Prakash Gupta
  2026-11-26  7:11 ` [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver Shiv Prakash Gupta
@ 2026-11-26  7:11 ` Shiv Prakash Gupta
  2026-11-26  7:26   ` sashiko-bot
  2026-11-26  7:11 ` [PATCH v5 4/4] rtc: pcf8525: Add temperature sensor support via hwmon Shiv Prakash Gupta
  2026-11-26  7:30 ` [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support sashiko-bot
  4 siblings, 1 reply; 8+ messages in thread
From: Shiv Prakash Gupta @ 2026-11-26  7:11 UTC (permalink / raw)
  To: linux-kernel, linux-rtc, linux-hwmon, linux-watchdog, devicetree,
	alexandre.belloni, krzk+dt, robh, conor+dt, linux, wim
  Cc: vikash.bansal, priyanka.jain, lakshay.piplani, Shiv Prakash Gupta

Extend the PCF8525 driver with an optional alarm-only watchdog.

Signed-off-by: Lakshay Piplani <lakshay.piplani@nxp.com>
Signed-off-by: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
---
 drivers/rtc/Kconfig       |   4 +-
 drivers/rtc/rtc-pcf8525.c | 328 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 330 insertions(+), 2 deletions(-)

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 848bcde41f2a..68b17ad29ed4 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -526,11 +526,13 @@ config RTC_DRV_PCF8523
 config RTC_DRV_PCF8525
 	tristate "NXP PCF8525"
 	select REGMAP_I2C
+	select WATCHDOG_CORE if WATCHDOG
 	help
 	  If you say yes here you get support for the NXP PCF8525 RTC chip.
 	  The PCF8525 is an I2C real-time clock with alarm, external timestamp
 	  capture and recording of the last calendar write, crystal aging offset
-	  correction, and backup power switchover.
+	  correction, backup power switchover, and an integrated watchdog
+	  timer.
 
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-pcf8525.
diff --git a/drivers/rtc/rtc-pcf8525.c b/drivers/rtc/rtc-pcf8525.c
index 2b23b17d6750..25420e7ad21c 100644
--- a/drivers/rtc/rtc-pcf8525.c
+++ b/drivers/rtc/rtc-pcf8525.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * NXP PCF8525 RTC driver with alarm and timestamp support.
+ * NXP PCF8525 RTC driver with alarm, timestamp and watchdog support.
  *
  * Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF8525.pdf
  */
@@ -17,9 +17,11 @@
 #include <linux/interrupt.h>
 #include <linux/limits.h>
 #include <linux/math.h>
+#include <linux/minmax.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/pm.h>
 #include <linux/pm_wakeirq.h>
 #include <linux/pm_wakeup.h>
 #include <linux/property.h>
@@ -28,6 +30,7 @@
 #include <linux/sysfs.h>
 #include <linux/types.h>
 #include <linux/uaccess.h>
+#include <linux/watchdog.h>
 
 #define PCF8525_REG_CTRL1		0x00
 #define PCF8525_REG_CTRL2		0x01
@@ -51,7 +54,11 @@
 
 #define PCF8525_REG_INTA_MASK1		0x28
 #define PCF8525_REG_INTA_MASK2		0x29
+#define PCF8525_REG_INTB_MASK1		0x2A
+#define PCF8525_REG_INTB_MASK2		0x2B
 
+#define PCF8525_REG_WD_CTL		0x2C
+#define PCF8525_REG_WD_VAL		0x2D
 #define PCF8525_REG_TEMP		0x2E
 
 #define PCF8525_CTRL1_STOP		BIT(5)
@@ -78,6 +85,7 @@
 #define PCF8525_CTRL5_XTL_TYP		BIT(0)
 
 #define PCF8525_CLKOUT_OTPR		BIT(4)
+#define PCF8525_CLKOUT_CLKOE		BIT(3)
 
 #define PCF8525_TS_CTL1_TSM		BIT(7)
 #define PCF8525_TS_CTL1_TSOFF		BIT(6)
@@ -92,10 +100,21 @@
 #define PCF8525_RESET_CPR_CMD		0xA4
 
 /* A set bit in the INTx_MASKy registers masks the interrupt source. */
+#define PCF8525_MASK1_WD_CD		BIT(0)
 #define PCF8525_MASK1_AIE		BIT(2)
 #define PCF8525_MASK1_ALL		GENMASK(5, 0)
 #define PCF8525_MASK2_TSIE		BIT(3)
 
+#define PCF8525_WD_CTL_WD_CD		BIT(7)
+#define PCF8525_WD_CTL_TF_MASK		GENMASK(1, 0)
+#define PCF8525_WD_TF_QUARTER_HZ	2
+
+#define PCF8525_WD_VAL_STOP		0
+#define PCF8525_WD_VAL_MIN		2
+#define PCF8525_WD_VAL_MAX		255
+#define PCF8525_WD_TICK_S		4
+#define PCF8525_WD_DEFAULT_TIMEOUT_S	60
+
 #define PCF8525_AGING_STEP_NUM		298L
 #define PCF8525_AGING_STEP_DEN		10L
 
@@ -103,11 +122,13 @@
 
 struct pcf8525 {
 	struct rtc_device *rtc;
+	struct watchdog_device wdd;
 	struct regmap *regmap;
 	struct mutex ts_mutex;	/* protects the TS1 cache and TSF */
 	time64_t ts;
 	bool ts_valid;
 	bool irq_enabled;
+	int irq_intb;
 };
 
 static const struct regmap_config pcf8525_regmap_cfg = {
@@ -699,6 +720,244 @@ static const struct rtc_class_ops pcf8525_rtc_ops = {
 	.param_set = pcf8525_param_set,
 };
 
+/* WDTF is set when the counter reaches 1, so a value n gives n - 1 ticks. */
+static unsigned int pcf8525_wdt_timeout_to_val(unsigned int timeout)
+{
+	return clamp_val(DIV_ROUND_UP(timeout, PCF8525_WD_TICK_S) + 1,
+			 PCF8525_WD_VAL_MIN, PCF8525_WD_VAL_MAX);
+}
+
+static unsigned int pcf8525_wdt_val_to_timeout(unsigned int val)
+{
+	return (val - 1) * PCF8525_WD_TICK_S;
+}
+
+static int pcf8525_wdt_reload(struct watchdog_device *wdd)
+{
+	struct pcf8525 *pcf8525 = watchdog_get_drvdata(wdd);
+
+	return regmap_write(pcf8525->regmap, PCF8525_REG_WD_VAL,
+			    pcf8525_wdt_timeout_to_val(wdd->timeout));
+}
+
+/*
+ * Load the counter with INTB masked and enable WD_CD and the INTB route last.
+ * @rollback stops the timer if starting fails; resume must keep it running.
+ */
+static int pcf8525_wdt_arm(struct watchdog_device *wdd, bool rollback)
+{
+	struct pcf8525 *pcf8525 = watchdog_get_drvdata(wdd);
+	struct regmap *regmap = pcf8525->regmap;
+	int ret;
+
+	ret = regmap_set_bits(regmap, PCF8525_REG_INTB_MASK1,
+			      PCF8525_MASK1_WD_CD);
+	if (ret)
+		return ret;
+
+	ret = regmap_clear_bits(regmap, PCF8525_REG_WD_CTL,
+				PCF8525_WD_CTL_WD_CD);
+	if (ret)
+		return ret;
+
+	ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_WDTF, 0);
+	if (ret)
+		return ret;
+
+	ret = pcf8525_wdt_reload(wdd);
+	if (ret)
+		return ret;
+
+	ret = regmap_set_bits(regmap, PCF8525_REG_WD_CTL,
+			      PCF8525_WD_CTL_WD_CD);
+	if (ret)
+		goto rollback_start;
+
+	ret = regmap_clear_bits(regmap, PCF8525_REG_INTB_MASK1,
+				PCF8525_MASK1_WD_CD);
+	if (!ret || !rollback)
+		return ret;
+
+	regmap_clear_bits(regmap, PCF8525_REG_WD_CTL,
+			  PCF8525_WD_CTL_WD_CD);
+
+rollback_start:
+	if (rollback)
+		regmap_write(regmap, PCF8525_REG_WD_VAL, PCF8525_WD_VAL_STOP);
+
+	return ret;
+}
+
+static int pcf8525_wdt_start(struct watchdog_device *wdd)
+{
+	return pcf8525_wdt_arm(wdd, true);
+}
+
+static int pcf8525_wdt_ping(struct watchdog_device *wdd)
+{
+	return pcf8525_wdt_reload(wdd);
+}
+
+static int pcf8525_wdt_stop(struct watchdog_device *wdd)
+{
+	struct pcf8525 *pcf8525 = watchdog_get_drvdata(wdd);
+
+	/* Loading 0 clears WDTF/nINTB and stops the hardware counter. */
+	return regmap_write(pcf8525->regmap, PCF8525_REG_WD_VAL,
+			    PCF8525_WD_VAL_STOP);
+}
+
+static int pcf8525_wdt_set_timeout(struct watchdog_device *wdd,
+				   unsigned int timeout)
+{
+	wdd->timeout = pcf8525_wdt_val_to_timeout(pcf8525_wdt_timeout_to_val(timeout));
+
+	/* watchdog core reloads an active watchdog after .set_timeout(). */
+	return 0;
+}
+
+static const struct watchdog_info pcf8525_wdt_info = {
+	.identity = "NXP PCF8525 Watchdog",
+	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
+		   WDIOF_MAGICCLOSE | WDIOF_ALARMONLY,
+};
+
+static const struct watchdog_ops pcf8525_wdt_ops = {
+	.owner = THIS_MODULE,
+	.start = pcf8525_wdt_start,
+	.stop = pcf8525_wdt_stop,
+	.ping = pcf8525_wdt_ping,
+	.set_timeout = pcf8525_wdt_set_timeout,
+};
+
+static irqreturn_t pcf8525_wdt_irq(int irq, void *data)
+{
+	struct device *dev = data;
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	unsigned int ctrl2;
+	int ret;
+
+	ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL2, &ctrl2);
+	if (ret) {
+		dev_warn_ratelimited(dev, "failed to read watchdog status: %d\n", ret);
+		return IRQ_NONE;
+	}
+
+	if (!(ctrl2 & PCF8525_CTRL2_WDTF))
+		return IRQ_NONE;
+
+	ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_WDTF, 0);
+	if (ret) {
+		dev_warn_ratelimited(dev, "failed to clear watchdog alarm: %d\n", ret);
+		return IRQ_NONE;
+	}
+
+	dev_warn_ratelimited(dev, "watchdog alarm expired\n");
+
+	return IRQ_HANDLED;
+}
+
+/*
+ * Mask the alarm route; stopping the counter is left to the watchdog core,
+ * which honours nowayout.
+ */
+static void pcf8525_wdt_cleanup(void *data)
+{
+	struct pcf8525 *pcf8525 = data;
+
+	if (watchdog_active(&pcf8525->wdd) ||
+	    watchdog_hw_running(&pcf8525->wdd))
+		return;
+
+	/* Detach WDTF from INTB before the managed IRQ is released. */
+	regmap_set_bits(pcf8525->regmap, PCF8525_REG_INTB_MASK1,
+			PCF8525_MASK1_WD_CD);
+}
+
+static int pcf8525_watchdog_config(struct pcf8525 *pcf8525)
+{
+	struct regmap *regmap = pcf8525->regmap;
+	int ret;
+
+	/* Isolate every INTB source before touching watchdog state. */
+	ret = regmap_set_bits(regmap, PCF8525_REG_INTB_MASK1,
+			      PCF8525_MASK1_ALL);
+	if (ret)
+		return ret;
+
+	ret = regmap_set_bits(regmap, PCF8525_REG_INTB_MASK2,
+			      PCF8525_MASK2_TSIE);
+	if (ret)
+		return ret;
+
+	ret = regmap_set_bits(regmap, PCF8525_REG_INTA_MASK1,
+			      PCF8525_MASK1_WD_CD);
+	if (ret)
+		return ret;
+
+	/* WD_VAL is write-only, so establish a known stopped state. */
+	ret = regmap_write(regmap, PCF8525_REG_WD_VAL, PCF8525_WD_VAL_STOP);
+	if (ret)
+		return ret;
+
+	/* Select the 1/4 Hz clock and keep WD_CD disabled until .start(). */
+	ret = regmap_update_bits(regmap, PCF8525_REG_WD_CTL,
+				 PCF8525_WD_CTL_WD_CD | PCF8525_WD_CTL_TF_MASK,
+				 FIELD_PREP(PCF8525_WD_CTL_TF_MASK,
+					    PCF8525_WD_TF_QUARTER_HZ));
+	if (ret)
+		return ret;
+
+	ret = pcf8525_update_ctrl2(pcf8525, PCF8525_CTRL2_WDTF, 0);
+	if (ret)
+		return ret;
+
+	/* All INTB sources are quiescent; switch the physical pin last. */
+	return regmap_clear_bits(regmap, PCF8525_REG_CLKOUT,
+				 PCF8525_CLKOUT_CLKOE);
+}
+
+static int pcf8525_watchdog_init(struct device *dev, struct pcf8525 *pcf8525)
+{
+	struct watchdog_device *wdd = &pcf8525->wdd;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_WATCHDOG) || pcf8525->irq_intb <= 0)
+		return 0;
+
+	ret = pcf8525_watchdog_config(pcf8525);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to configure watchdog\n");
+
+	ret = devm_request_threaded_irq(dev, pcf8525->irq_intb, NULL,
+					pcf8525_wdt_irq, IRQF_ONESHOT,
+					"pcf8525-wdt", dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to request INTB IRQ\n");
+
+	wdd->parent = dev;
+	wdd->info = &pcf8525_wdt_info;
+	wdd->ops = &pcf8525_wdt_ops;
+	wdd->min_timeout = pcf8525_wdt_val_to_timeout(PCF8525_WD_VAL_MIN);
+	wdd->max_timeout = pcf8525_wdt_val_to_timeout(PCF8525_WD_VAL_MAX);
+	wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
+	wdd->timeout = PCF8525_WD_DEFAULT_TIMEOUT_S;
+	watchdog_init_timeout(wdd, 0, dev);
+	pcf8525_wdt_set_timeout(wdd, wdd->timeout);
+
+	watchdog_set_drvdata(wdd, pcf8525);
+	watchdog_stop_on_reboot(wdd);
+	watchdog_stop_on_unregister(wdd);
+	watchdog_stop_ping_on_suspend(wdd);
+
+	/* Unregister the watchdog, detach INTB, then free the IRQ. */
+	ret = devm_add_action_or_reset(dev, pcf8525_wdt_cleanup, pcf8525);
+	if (ret)
+		return ret;
+
+	return devm_watchdog_register_device(dev, wdd);
+}
+
 static int pcf8525_get_inta_irq(struct i2c_client *client)
 {
 	struct fwnode_handle *fwnode = dev_fwnode(&client->dev);
@@ -719,6 +978,31 @@ static int pcf8525_get_inta_irq(struct i2c_client *client)
 	return client->irq;
 }
 
+static int pcf8525_get_intb_irq(struct i2c_client *client)
+{
+	struct fwnode_handle *fwnode = dev_fwnode(&client->dev);
+	int irq;
+
+	irq = fwnode_irq_get_byname(fwnode, "intb");
+	if (irq >= 0 || irq == -EPROBE_DEFER)
+		return irq;
+
+	if (irq != -ENOENT && irq != -EINVAL && irq != -ENODATA &&
+	    irq != -ENXIO)
+		return irq;
+
+	if (fwnode_property_present(fwnode, "interrupt-names"))
+		return 0;
+
+	/* With no interrupt-names, INTB is the optional second interrupt. */
+	irq = fwnode_irq_get(fwnode, 1);
+	if (irq == -ENOENT || irq == -EINVAL || irq == -ENODATA ||
+	    irq == -ENXIO)
+		return 0;
+
+	return irq;
+}
+
 static int pcf8525_mask_irqs_inta(struct pcf8525 *pcf8525)
 {
 	int ret;
@@ -804,6 +1088,38 @@ static int pcf8525_setup_wakeup_only(struct device *dev,
 	return pcf8525_unmask_irqs_inta(pcf8525, false);
 }
 
+static int pcf8525_suspend(struct device *dev)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	int ret;
+
+	if (watchdog_active(&pcf8525->wdd)) {
+		/* Keep the counter and WD_CD state; only detach WDTF from INTB. */
+		ret = regmap_set_bits(pcf8525->regmap, PCF8525_REG_INTB_MASK1,
+				      PCF8525_MASK1_WD_CD);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int pcf8525_resume(struct device *dev)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	int ret;
+
+	if (watchdog_active(&pcf8525->wdd)) {
+		ret = pcf8525_wdt_arm(&pcf8525->wdd, false);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(pcf8525_pm_ops, pcf8525_suspend, pcf8525_resume);
+
 static int pcf8525_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -832,6 +1148,11 @@ static int pcf8525_probe(struct i2c_client *client)
 	if (irq < 0)
 		return dev_err_probe(dev, irq, "failed to get INTA IRQ\n");
 
+	pcf8525->irq_intb = pcf8525_get_intb_irq(client);
+	if (pcf8525->irq_intb < 0)
+		return dev_err_probe(dev, pcf8525->irq_intb,
+				     "failed to get INTB IRQ\n");
+
 	/* Isolate stale and unused INTA sources before enabling timestamps. */
 	if (irq > 0 || wakeup) {
 		ret = pcf8525_mask_irqs_inta(pcf8525);
@@ -879,6 +1200,10 @@ static int pcf8525_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	ret = pcf8525_watchdog_init(dev, pcf8525);
+	if (ret)
+		return ret;
+
 	/* Wakeup must be set up before registering so wakealarm is exposed. */
 	if (irq > 0 && !(client->flags & I2C_CLIENT_WAKE)) {
 		ret = devm_device_init_wakeup(dev);
@@ -919,6 +1244,7 @@ static struct i2c_driver pcf8525_driver = {
 	.driver = {
 		.name = "rtc-pcf8525",
 		.of_match_table = pcf8525_of_match,
+		.pm = pm_sleep_ptr(&pcf8525_pm_ops),
 	},
 	.probe = pcf8525_probe,
 	.id_table = pcf8525_i2c_id,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 4/4] rtc: pcf8525: Add temperature sensor support via hwmon
  2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
                   ` (2 preceding siblings ...)
  2026-11-26  7:11 ` [PATCH v5 3/4] rtc: pcf8525: Add watchdog support Shiv Prakash Gupta
@ 2026-11-26  7:11 ` Shiv Prakash Gupta
  2026-11-26  7:30 ` [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support sashiko-bot
  4 siblings, 0 replies; 8+ messages in thread
From: Shiv Prakash Gupta @ 2026-11-26  7:11 UTC (permalink / raw)
  To: linux-kernel, linux-rtc, linux-hwmon, linux-watchdog, devicetree,
	alexandre.belloni, krzk+dt, robh, conor+dt, linux, wim
  Cc: vikash.bansal, priyanka.jain, lakshay.piplani, Shiv Prakash Gupta

Expose the PCF8525 on-chip temperature sensor through the hwmon
subsystem as an optional feature controlled by
CONFIG_RTC_DRV_PCF8525_HWMON.

Signed-off-by: Lakshay Piplani <lakshay.piplani@nxp.com>
Signed-off-by: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>
---
 drivers/rtc/Kconfig       |  10 +++
 drivers/rtc/rtc-pcf8525.c | 149 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 158 insertions(+), 1 deletion(-)

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 68b17ad29ed4..d5c3468e9867 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -537,6 +537,16 @@ config RTC_DRV_PCF8525
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-pcf8525.
 
+config RTC_DRV_PCF8525_HWMON
+	bool "HWMON support for NXP PCF8525"
+	depends on RTC_DRV_PCF8525 && HWMON && !(RTC_DRV_PCF8525=y && HWMON=m)
+	default y
+	help
+	  Say Y here to expose the PCF8525 on-chip temperature sensor
+	  through the hwmon interface (temp1_input in millidegrees Celsius).
+	  The sensor resolution is 1 degree C.  The measurement interval is
+	  programmable from 1 to 32 minutes via hwmon chip_update_interval.
+
 config RTC_DRV_PCF85363
 	tristate "NXP PCF85363"
 	select REGMAP_I2C
diff --git a/drivers/rtc/rtc-pcf8525.c b/drivers/rtc/rtc-pcf8525.c
index 25420e7ad21c..e98dd0468cfb 100644
--- a/drivers/rtc/rtc-pcf8525.c
+++ b/drivers/rtc/rtc-pcf8525.c
@@ -1,10 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * NXP PCF8525 RTC driver with alarm, timestamp and watchdog support.
+ * NXP PCF8525 RTC driver with alarm, timestamp, watchdog and hwmon support.
  *
  * Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF8525.pdf
  */
 
+#include <linux/array_size.h>
 #include <linux/bcd.h>
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
@@ -13,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/hwmon.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/limits.h>
@@ -30,6 +32,7 @@
 #include <linux/sysfs.h>
 #include <linux/types.h>
 #include <linux/uaccess.h>
+#include <linux/util_macros.h>
 #include <linux/watchdog.h>
 
 #define PCF8525_REG_CTRL1		0x00
@@ -82,8 +85,10 @@
 #define PCF8525_CTRL4_TSF		BIT(7)
 
 #define PCF8525_CTRL5_TSIE		BIT(7)
+#define PCF8525_CTRL5_TEMP_RD_EN	BIT(2)
 #define PCF8525_CTRL5_XTL_TYP		BIT(0)
 
+#define PCF8525_CLKOUT_TCR_MASK		GENMASK(7, 5)
 #define PCF8525_CLKOUT_OTPR		BIT(4)
 #define PCF8525_CLKOUT_CLKOE		BIT(3)
 
@@ -232,6 +237,146 @@ static int pcf8525_rtc_set_offset(struct device *dev, long offset)
 				 buf, sizeof(buf));
 }
 
+static const int pcf8525_hwmon_interval_ms[] = {
+	32 * 60 * 1000,
+	16 * 60 * 1000,
+	8 * 60 * 1000,
+	4 * 60 * 1000,
+	2 * 60 * 1000,
+	60 * 1000,
+};
+
+static int pcf8525_hwmon_read_interval(struct pcf8525 *pcf8525, long *val)
+{
+	unsigned int regval;
+	unsigned int tcr;
+	int ret;
+
+	ret = regmap_read(pcf8525->regmap, PCF8525_REG_CLKOUT, &regval);
+	if (ret)
+		return ret;
+
+	/* TCR values 5 to 7 all select a period of one minute. */
+	tcr = min_t(unsigned int, FIELD_GET(PCF8525_CLKOUT_TCR_MASK, regval),
+		    ARRAY_SIZE(pcf8525_hwmon_interval_ms) - 1);
+	*val = pcf8525_hwmon_interval_ms[tcr];
+
+	return 0;
+}
+
+static int pcf8525_hwmon_write_interval(struct pcf8525 *pcf8525, long val)
+{
+	unsigned int tcr;
+
+	tcr = find_closest_descending(val, pcf8525_hwmon_interval_ms,
+				      ARRAY_SIZE(pcf8525_hwmon_interval_ms));
+
+	return regmap_update_bits(pcf8525->regmap, PCF8525_REG_CLKOUT,
+				  PCF8525_CLKOUT_TCR_MASK,
+				  FIELD_PREP(PCF8525_CLKOUT_TCR_MASK, tcr));
+}
+
+static umode_t pcf8525_hwmon_is_visible(const void *data,
+					enum hwmon_sensor_types type,
+					u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_chip:
+		if (attr == hwmon_chip_update_interval)
+			return 0644;
+		break;
+	case hwmon_temp:
+		if (attr == hwmon_temp_input)
+			return 0444;
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static int pcf8525_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			      u32 attr, int channel, long *val)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+	unsigned int regval;
+	int ret;
+
+	switch (type) {
+	case hwmon_chip:
+		if (attr == hwmon_chip_update_interval)
+			return pcf8525_hwmon_read_interval(pcf8525, val);
+		break;
+	case hwmon_temp:
+		if (attr != hwmon_temp_input)
+			break;
+
+		ret = regmap_read(pcf8525->regmap, PCF8525_REG_TEMP, &regval);
+		if (ret)
+			return ret;
+
+		*val = (s8)regval * 1000L;
+		return 0;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int pcf8525_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
+			       u32 attr, int channel, long val)
+{
+	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
+
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
+		return pcf8525_hwmon_write_interval(pcf8525, val);
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_channel_info * const pcf8525_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops pcf8525_hwmon_ops = {
+	.is_visible = pcf8525_hwmon_is_visible,
+	.read = pcf8525_hwmon_read,
+	.write = pcf8525_hwmon_write,
+};
+
+static const struct hwmon_chip_info pcf8525_hwmon_chip_info = {
+	.ops = &pcf8525_hwmon_ops,
+	.info = pcf8525_hwmon_info,
+};
+
+static void pcf8525_hwmon_register(struct device *dev, struct pcf8525 *pcf8525)
+{
+	struct device *hwmon_dev;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_RTC_DRV_PCF8525_HWMON))
+		return;
+
+	/* A cleared TEMP_RD_EN enables the digital readout in register 0x2e. */
+	ret = regmap_clear_bits(pcf8525->regmap, PCF8525_REG_CTRL5,
+				PCF8525_CTRL5_TEMP_RD_EN);
+	if (ret) {
+		dev_warn(dev, "failed to enable temperature readout: %d\n", ret);
+		return;
+	}
+
+	hwmon_dev = devm_hwmon_device_register_with_info(dev, "pcf8525", pcf8525,
+							 &pcf8525_hwmon_chip_info,
+							 NULL);
+	if (IS_ERR(hwmon_dev))
+		dev_warn(dev, "failed to register hwmon device: %ld\n",
+			 PTR_ERR(hwmon_dev));
+}
+
 static int pcf8525_read_time(struct device *dev, struct rtc_time *tm)
 {
 	struct pcf8525 *pcf8525 = dev_get_drvdata(dev);
@@ -1225,6 +1370,8 @@ static int pcf8525_probe(struct i2c_client *client)
 			return ret;
 	}
 
+	pcf8525_hwmon_register(dev, pcf8525);
+
 	return 0;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 3/4] rtc: pcf8525: Add watchdog support
  2026-11-26  7:11 ` [PATCH v5 3/4] rtc: pcf8525: Add watchdog support Shiv Prakash Gupta
@ 2026-11-26  7:26   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-26  7:26 UTC (permalink / raw)
  To: Shiv Prakash Gupta, linux-rtc, linux-hwmon, linux-watchdog,
	devicetree
  Cc: robh, conor+dt, Alexandre Belloni

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] rtc: pcf8525: probe failure in pcf8525_get_intb_irq() on
  single-IRQ DT nodes
--

commit 30bd2f8be3e00c36f24f374f227c2546b036b58f
Author: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>

rtc: pcf8525: Add watchdog support

This commit adds optional alarm-only watchdog support to the NXP PCF8525
RTC driver using the INTB interrupt line.

> diff --git a/drivers/rtc/rtc-pcf8525.c b/drivers/rtc/rtc-pcf8525.c
> index 2b23b17d6750c..25420e7ad21c8 100644
> --- a/drivers/rtc/rtc-pcf8525.c
> +++ b/drivers/rtc/rtc-pcf8525.c

[ ... ]

> @@ -719,6 +978,31 @@ static int pcf8525_get_inta_irq(struct i2c_client *client)
>  	return client->irq;
>  }
>  
> +static int pcf8525_get_intb_irq(struct i2c_client *client)
> +{
> +	struct fwnode_handle *fwnode = dev_fwnode(&client->dev);
> +	int irq;
> +
> +	irq = fwnode_irq_get_byname(fwnode, "intb");
> +	if (irq >= 0 || irq == -EPROBE_DEFER)
> +		return irq;
> +
> +	if (irq != -ENOENT && irq != -EINVAL && irq != -ENODATA &&
> +	    irq != -ENXIO)
> +		return irq;
> +
> +	if (fwnode_property_present(fwnode, "interrupt-names"))
> +		return 0;
> +
> +	/* With no interrupt-names, INTB is the optional second interrupt. */
> +	irq = fwnode_irq_get(fwnode, 1);
> +	if (irq == -ENOENT || irq == -EINVAL || irq == -ENODATA ||
> +	    irq == -ENXIO)
> +		return 0;
> +
> +	return irq;
> +}

[Severity: High]
Does fwnode_irq_get(fwnode, 1) fail pcf8525_probe() on Device Tree nodes
that specify a single interrupt without interrupt-names?

When the interrupts property has only one entry, fwnode_irq_get() ->
of_fwnode_irq_get() -> of_irq_get() -> of_irq_parse_one() ->
of_property_read_u32_index() -> of_find_property_value_of_size() returns
-EOVERFLOW because the property exists on the node with a length smaller
than index 1 requires.  Since -EOVERFLOW is not mapped to 0 here,
pcf8525_get_intb_irq() returns -EOVERFLOW and aborts pcf8525_probe().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126071124.2383368-1-shivprakash.gupta@nxp.com?part=3

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver
  2026-11-26  7:11 ` [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver Shiv Prakash Gupta
@ 2026-11-26  7:30   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-26  7:30 UTC (permalink / raw)
  To: Shiv Prakash Gupta, linux-rtc, linux-hwmon, linux-watchdog,
	devicetree
  Cc: robh, conor+dt, Alexandre Belloni

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] rtc: pcf8525: devm_rtc_register_device() called before INTA
  setup in probe
--

commit ac013f157325db026a034ab7ceb349efee94dcf3
Author: Shiv Prakash Gupta <shivprakash.gupta@nxp.com>

rtc: pcf8525: Add NXP PCF8525 RTC driver

This commit adds an I2C RTC driver for the NXP PCF8525 with calendar,
alarm, timestamp capture, crystal aging offset, and backup switchover
support.

> diff --git a/drivers/rtc/rtc-pcf8525.c b/drivers/rtc/rtc-pcf8525.c
> new file mode 100644
> index 0000000000000..2b23b17d6750c
> --- /dev/null
> +++ b/drivers/rtc/rtc-pcf8525.c
[ ... ]
> +static ssize_t timestamp0_show(struct device *dev,
> +			       struct device_attribute *attr, char *buf)
> +{
> +	struct pcf8525 *pcf8525 = dev_get_drvdata(dev->parent);
> +	unsigned int ctrl4;
> +	time64_t ts = 0;
> +	int ret;
> +
> +	guard(mutex)(&pcf8525->ts_mutex);
> +
> +	if (pcf8525->irq_enabled) {
> +		if (!pcf8525->ts_valid)
> +			return 0;
> +
> +		ts = pcf8525->ts;
> +	} else {
> +		ret = regmap_read(pcf8525->regmap, PCF8525_REG_CTRL4, &ctrl4);
> +		if (ret)
> +			return ret;
> +
> +		if (!(ctrl4 & PCF8525_CTRL4_TSF))
> +			return 0;
> +
> +		ret = pcf8525_ts_read(pcf8525, PCF8525_REG_TS1_SEC, &ts);
> +		if (ret)
> +			return ret == -ENODATA ? 0 : ret;
> +	}
> +
> +	return sysfs_emit(buf, "%lld\n",
> +			  (long long)pcf8525_ts_apply_offset(pcf8525, ts));
> +}
[ ... ]
> +static int pcf8525_setup_inta(struct i2c_client *client,
> +			      struct pcf8525 *pcf8525, int irq)
> +{
> +	struct device *dev = &client->dev;
> +	bool handled;
> +	int ret;
> +
> +	/* INTA is still masked, so drain all stale flags before enabling it. */
> +	ret = pcf8525_service_inta(dev, pcf8525, &handled);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to drain pending INTA\n");
> +
> +	ret = devm_request_threaded_irq(dev, irq, NULL, pcf8525_irq,
> +					IRQF_ONESHOT, dev_name(dev), dev);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to request INTA IRQ\n");
> +
> +	pcf8525->irq_enabled = true;
[ ... ]
> +static int pcf8525_probe(struct i2c_client *client)
> +{
[ ... ]
> +	ret = devm_rtc_register_device(pcf8525->rtc);
> +	if (ret)
> +		return ret;
> +
> +	if (irq > 0) {
> +		ret = pcf8525_setup_inta(client, pcf8525, irq);
> +		if (ret)
> +			return ret;
> +	} else if (wakeup) {
> +		ret = pcf8525_setup_wakeup_only(dev, pcf8525);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
Should pcf8525_setup_inta() and pcf8525_setup_wakeup_only() run before
devm_rtc_register_device()?

Registering the RTC device first exposes the sysfs attributes and /dev/rtcX
before pending INTA flags are drained and before pcf8525->irq_enabled is
set:

CPU0                              CPU1
pcf8525_probe()
  devm_rtc_register_device()
  pcf8525_setup_inta()
    pcf8525_service_inta()
      pcf8525_handle_ts()
        lock(&pcf8525->ts_mutex)
        regmap_write(CTRL4, 0)
        pcf8525->ts_valid = true
        unlock(&pcf8525->ts_mutex)
                                  timestamp0_show()
                                    lock(&pcf8525->ts_mutex)
                                    if (pcf8525->irq_enabled) // false
                                    regmap_read(CTRL4, &ctrl4)
                                    !(ctrl4 & TSF) -> returns 0
    devm_request_threaded_irq()
    pcf8525->irq_enabled = true

Because pcf8525_handle_ts() clears PCF8525_CTRL4_TSF in hardware and caches
the timestamp in pcf8525->ts while pcf8525->irq_enabled is still false, a
concurrent reader of timestamp0 takes the polling branch, sees TSF cleared,
and returns 0 instead of the captured timestamp.

In the wakeup-only path, pcf8525_setup_wakeup_only() also clears
PCF8525_CTRL2_AF and unmasks INTA after devm_rtc_register_device() has
already read the alarm state, and on driver unbind devres masks INTA and
frees the IRQ before unregistering the RTC device while
pcf8525->irq_enabled remains true.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126071124.2383368-1-shivprakash.gupta@nxp.com?part=2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support
  2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
                   ` (3 preceding siblings ...)
  2026-11-26  7:11 ` [PATCH v5 4/4] rtc: pcf8525: Add temperature sensor support via hwmon Shiv Prakash Gupta
@ 2026-11-26  7:30 ` sashiko-bot
  4 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-26  7:30 UTC (permalink / raw)
  To: Shiv Prakash Gupta, linux-rtc, linux-hwmon; +Cc: Alexandre Belloni

Sashiko has finished reviewing this 4-patch series:

- Patches 1 and 4: no issues found.
- Patch 2 ("rtc: pcf8525: Add NXP PCF8525 RTC driver"): 1 potential
  issue (1 Medium)
- Patch 3 ("rtc: pcf8525: Add watchdog support"): 1 potential issue (1
  High)

Detailed reviews have been sent as separate replies to patches 2 and 3.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126071124.2383368-1-shivprakash.gupta@nxp.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-26  7:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  7:11 [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support Shiv Prakash Gupta
2026-11-26  7:11 ` [PATCH v5 1/4] dt-bindings: rtc: Add NXP PCF8525 RTC Shiv Prakash Gupta
2026-11-26  7:11 ` [PATCH v5 2/4] rtc: pcf8525: Add NXP PCF8525 RTC driver Shiv Prakash Gupta
2026-11-26  7:30   ` sashiko-bot
2026-11-26  7:11 ` [PATCH v5 3/4] rtc: pcf8525: Add watchdog support Shiv Prakash Gupta
2026-11-26  7:26   ` sashiko-bot
2026-11-26  7:11 ` [PATCH v5 4/4] rtc: pcf8525: Add temperature sensor support via hwmon Shiv Prakash Gupta
2026-11-26  7:30 ` [PATCH v5 0/4] rtc: Add NXP PCF8525 RTC support sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox