From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.121.34.13 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796016537; b=puMvBPD9BdM7NR8noNgLRk+Dp50kfEfeofk2soFf2/tFXu9xEMyRJezztk9A90ZSOQtk irSr4dJMocWrv1H0BzuTsYTrpsmkgNkfv2W46t7Qci8g/u4qJKpSM9JQA8m2eJOW4aihb y7BbQf3n1kjuUCmvD+oufUVx30Tqj61uEXI3Ddx/L1HTyISq6bT4POZYC62mga3KtYIYP KT3nfqcjnGII0dTw20gePwcfKYXCptoQNo6jPE13yXDBV4yb3dr6Q5fRnrfAp7/nZxRM6 ybtz/UZK8hvC2+fesFSQTMvI49EzRFypTAb1n2s93ams2HCOV7/JhVqK5Ma1WHaUKsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796016537; h=DMARC-Filter:Received:Received:Received:From:To:Cc:Subject:Date: Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Virus-Scanned; bh=lHtttvutLR3CzGtpbAOGsMAOZFENlpGaQ3/d4DQcPMI=; b=LxcF+Ce3nai1A0HJ9L3A3w7Y4SUpHyDz/4j3XNUVbM7xPHDIG00Tv4cAFzaEKkt6SVpg th4wYAz52sJbmgiEDLYzqgz+5/zIqN1h4JCyZN1YPTTWwbe2rvpRMnUtP9w9k4Ucgke8d 4z/tLBrxkYearn7aoAGsAuFqgo5VDfuNV807AFxekGnUiZRIML9IHH6VymfIumSVXDRAG 3rN1u00CvuuBDu5RqFDuSzl1QAphT8TsRrycrQKq1YN8rrfvgOWqTGsq/xJQsiNHcMNvz hqUXTKaakNqGpjBo4cVEAC0SMO5zkwJgWB59Lbva72NY3BngKyFMpdyHodO750bVwdg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.remote-ip=92.121.34.13 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97129606A689; Mon, 30 Nov 2026 05:28:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 97129606A689 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 1B4C01A0002; Mon, 30 Nov 2026 06:28:55 +0100 (CET) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id D96701A001A; Mon, 30 Nov 2026 06:28:54 +0100 (CET) Received: from lsv031125.swis.in-blr01.nxp.com (lsv031125.swis.in-blr01.nxp.com [10.12.177.172]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 8257518000B6; Mon, 30 Nov 2026 13:28:51 +0800 (+08) From: Shiv Prakash Gupta To: linux-kernel@vger.kernel.org, linux-rtc@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, alexandre.belloni@bootlin.com, krzk+dt@kernel.org, robh@kernel.org, conor+dt@kernel.org, linux@roeck-us.net, wim@linux-watchdog.org Cc: vikash.bansal@nxp.com, priyanka.jain@nxp.com, lakshay.piplani@nxp.com, Shiv Prakash Gupta Subject: [PATCH v7 4/4] rtc: pcf8525: Add temperature sensor support via hwmon Date: Mon, 30 Nov 2026 10:58:23 +0530 Message-Id: <20261130052823.3758338-5-shivprakash.gupta@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261130052823.3758338-1-shivprakash.gupta@nxp.com> References: <20261130052823.3758338-1-shivprakash.gupta@nxp.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Shiv Prakash Gupta --- 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 f85103529c9c..48bb48c9e38f 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 #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +32,7 @@ #include #include #include +#include #include #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) @@ -241,6 +246,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, ®val); + 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, ®val); + 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); @@ -1240,6 +1385,8 @@ static int pcf8525_probe(struct i2c_client *client) if (ret) return ret; + pcf8525_hwmon_register(dev, pcf8525); + return 0; } -- 2.34.1