[PATCH v4 0/3] Add pcf2131 rtc support

The patchset supports pcf2131 rtc. For the details, please check the patch commit log.
Joy Zou (3): drivers: rtc: add pcf2131 rtc driver imx: imx93_evk: add rtc PCF2131 imx93_11x11_evk: Add PCF2131 RTC support
arch/arm/dts/imx93-11x11-evk-u-boot.dtsi | 34 ++++++ configs/imx93_11x11_evk_defconfig | 1 + drivers/rtc/pcf2127.c | 146 ++++++++++++++++++++--- 3 files changed, 167 insertions(+), 14 deletions(-)

Adding support for pcf2131 RTC chip.
The pcf2131 is similar to the pcf2127. The driver support rtc register read/write by using rtc cmd and rtc date set/get by using date cmd.
The pcf2131 is special when write access to time registers. it requires setting the STOP and CPR bits. STOP bit needs to be cleared after time registers are updated.
Signed-off-by: Joy Zou joy.zou@nxp.com --- Changes in v4: 1. Add static keyword for the is_pcf2131_type function. 2. remove unnecessary ret initialization.
Changes in v3: 1.merge pcf2131 into pcf2127 in order to keep same with kernel.
Changes in v2: 1. delete the unnecessary initialization. 2. retrun directly function insteand of redundancy return ret. 3. delete the unnecessary comment line. --- drivers/rtc/pcf2127.c | 146 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 132 insertions(+), 14 deletions(-)
diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c index 2f3fafb496..2b000347f8 100644 --- a/drivers/rtc/pcf2127.c +++ b/drivers/rtc/pcf2127.c @@ -23,6 +23,38 @@ #define PCF2127_REG_MO 0x08 #define PCF2127_REG_YR 0x09
+#define PCF2131_REG_CTRL1 0x00 +#define PCF2131_BIT_CTRL1_STOP BIT(5) +#define PCF2131_REG_SR_RESET 0x05 +#define PCF2131_SR_VAL_Clr_Pres 0xa4 +#define PCF2131_REG_SC 0x07 +#define PCF2131_REG_MN 0x08 +#define PCF2131_REG_HR 0x09 +#define PCF2131_REG_DM 0x0a +#define PCF2131_REG_DW 0x0b +#define PCF2131_REG_MO 0x0c +#define PCF2131_REG_YR 0x0d + +enum { + NXP_CHIP_TYPE_PCF2127 = 0, + NXP_CHIP_TYPE_PCF2129, + NXP_CHIP_TYPE_PCA2129, + NXP_CHIP_TYPE_PCF2131, + NXP_CHIP_TYPE_AMOUNT +}; + +static bool is_pcf2131_type(struct udevice *dev) +{ + int type; + + type = dev_get_driver_data(dev); + + if (type == NXP_CHIP_TYPE_PCF2131) + return true; + else + return false; +} + static int pcf2127_rtc_read(struct udevice *dev, uint offset, u8 *buffer, uint len) { struct dm_i2c_chip *chip = dev_get_parent_plat(dev); @@ -43,10 +75,64 @@ static int pcf2127_rtc_read(struct udevice *dev, uint offset, u8 *buffer, uint l return dm_i2c_xfer(dev, &msg, 1); }
+static int pcf2131_rtc_lock(struct udevice *dev) +{ + int ret; + uchar buf[6] = { PCF2131_REG_CTRL1 }; + + ret = pcf2127_rtc_read(dev, PCF2131_REG_CTRL1, buf, sizeof(buf)); + if (ret < 0) + return ret; + + buf[PCF2131_REG_CTRL1] |= PCF2131_BIT_CTRL1_STOP; + ret = dm_i2c_write(dev, PCF2131_REG_CTRL1, &buf[PCF2131_REG_CTRL1], 1); + if (ret < 0) + return ret; + + buf[PCF2131_REG_SR_RESET] = PCF2131_SR_VAL_Clr_Pres; + + return dm_i2c_write(dev, PCF2131_REG_SR_RESET, &buf[PCF2131_REG_SR_RESET], 1); +} + +static int pcf2131_rtc_unlock(struct udevice *dev) +{ + int ret; + uchar buf[6] = { PCF2131_REG_CTRL1 }; + + ret = pcf2127_rtc_read(dev, PCF2131_REG_CTRL1, buf, sizeof(buf)); + if (ret < 0) + return ret; + + buf[PCF2131_REG_CTRL1] &= ~PCF2131_BIT_CTRL1_STOP; + return dm_i2c_write(dev, PCF2131_REG_CTRL1, &buf[PCF2131_REG_CTRL1], 1); +} + static int pcf2127_rtc_write(struct udevice *dev, uint offset, const u8 *buffer, uint len) { - return dm_i2c_write(dev, offset, buffer, len); + int ret; + bool flag; + + flag = is_pcf2131_type(dev); + if (flag) { + ret = pcf2131_rtc_lock(dev); + if (ret < 0) + return ret; + } + + ret = dm_i2c_write(dev, offset, buffer, len); + if (ret < 0) { + if (flag) + pcf2131_rtc_unlock(dev); + return ret; + } + + if (flag) { + ret = pcf2131_rtc_unlock(dev); + if (ret < 0) + return ret; + } + return ret; }
static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) @@ -68,15 +154,19 @@ static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
/* write register's data */ - ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i); + if (is_pcf2131_type(dev)) + ret = pcf2127_rtc_write(dev, PCF2131_REG_SC, buf, i); + else + ret = pcf2127_rtc_write(dev, PCF2127_REG_SC, buf, i);
return ret; }
static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) { - int ret = 0; - uchar buf[10] = { PCF2127_REG_CTRL1 }; + int ret; + bool flag; + uchar buf[14] = { PCF2127_REG_CTRL1 };
ret = pcf2127_rtc_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); if (ret < 0) @@ -85,15 +175,28 @@ static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) if (buf[PCF2127_REG_CTRL3] & 0x04) puts("### Warning: RTC Low Voltage - date/time not reliable\n");
- tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); - tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); - tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); - tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); - tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F); - tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900; + flag = is_pcf2131_type(dev); + if (flag) { + tm->tm_sec = bcd2bin(buf[PCF2131_REG_SC] & 0x7F); + tm->tm_min = bcd2bin(buf[PCF2131_REG_MN] & 0x7F); + tm->tm_hour = bcd2bin(buf[PCF2131_REG_HR] & 0x3F); + tm->tm_mday = bcd2bin(buf[PCF2131_REG_DM] & 0x3F); + tm->tm_mon = bcd2bin(buf[PCF2131_REG_MO] & 0x1F); + tm->tm_year = bcd2bin(buf[PCF2131_REG_YR]) + 1900; + } else { + tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); + tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); + tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); + tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); + tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F); + tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900; + } if (tm->tm_year < 1970) tm->tm_year += 100; /* assume we are in 1970...2069 */ - tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; + if (flag) + tm->tm_wday = buf[PCF2131_REG_DW] & 0x07; + else + tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; tm->tm_yday = 0; tm->tm_isdst = 0;
@@ -111,6 +214,19 @@ static int pcf2127_rtc_reset(struct udevice *dev) return 0; }
+static int pcf2127_probe(struct udevice *dev) +{ + struct udevice *bus, *udev; + struct dm_i2c_chip *chip = dev_get_parent_plat(dev); + + if (is_pcf2131_type(dev)) { + bus = dev_get_parent(dev); + return dm_i2c_probe(bus, chip->chip_addr, 0, &udev); + } + + return 0; +} + static const struct rtc_ops pcf2127_rtc_ops = { .get = pcf2127_rtc_get, .set = pcf2127_rtc_set, @@ -120,9 +236,10 @@ static const struct rtc_ops pcf2127_rtc_ops = { };
static const struct udevice_id pcf2127_rtc_ids[] = { - { .compatible = "nxp,pcf2127" }, - { .compatible = "nxp,pcf2129" }, - { .compatible = "nxp,pca2129" }, + { .compatible = "nxp,pcf2127", .data = NXP_CHIP_TYPE_PCF2127, }, + { .compatible = "nxp,pcf2129", .data = NXP_CHIP_TYPE_PCF2129, }, + { .compatible = "nxp,pca2129", .data = NXP_CHIP_TYPE_PCA2129, }, + { .compatible = "nxp,pcf2131", .data = NXP_CHIP_TYPE_PCF2131, }, { } };
@@ -131,4 +248,5 @@ U_BOOT_DRIVER(rtc_pcf2127) = { .id = UCLASS_RTC, .of_match = pcf2127_rtc_ids, .ops = &pcf2127_rtc_ops, + .probe = pcf2127_probe, };

support rtc PCF2131 for imx93.
Signed-off-by: Joy Zou joy.zou@nxp.com --- Changes in v4: 1. remove arch/arm/dts/imx93_11x11_evk.dts change because it can sync from kernel dts. 2. add the RTC support to the -u-boot.dtsi. 3. assign the rtc0 to rtc@53 in order to avoid date reset fail.
Changes in v3: 1. remove arch/arm/dts/imx93.dtsi modification because this change have existed.
Changes in v2: 1. use the flag bootph-pre-ram instead of uboot,dm-spl. --- arch/arm/dts/imx93-11x11-evk-u-boot.dtsi | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
diff --git a/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi b/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi index a99ba99bfb..85aaf0844f 100644 --- a/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi +++ b/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi @@ -19,6 +19,11 @@ method = "smc"; }; }; + + aliases { + rtc0 = &pcf2131; + }; + };
&{/soc@0} { @@ -112,6 +117,25 @@ bootph-some-ram; };
+&lpi2c3 { + bootph-pre-ram; + #address-cells = <1>; + #size-cells = <0>; + clock-frequency = <400000>; + pinctrl-names = "default", "sleep"; + pinctrl-0 = <&pinctrl_lpi2c3>; + pinctrl-1 = <&pinctrl_lpi2c3>; + status = "okay"; + + pcf2131: rtc@53 { + compatible = "nxp,pcf2131"; + reg = <0x53>; + interrupt-parent = <&pcal6524>; + interrupts = <1 IRQ_TYPE_LEVEL_LOW>; + status = "okay"; + }; +}; + &{/soc@0/bus@44000000/i2c@44350000/pmic@25} { bootph-pre-ram; bootph-some-ram; @@ -127,6 +151,16 @@ bootph-some-ram; };
+&iomuxc { + pinctrl_lpi2c3: lpi2c3grp { + bootph-pre-ram; + fsl,pins = < + MX93_PAD_GPIO_IO28__LPI2C3_SDA 0x40000b9e + MX93_PAD_GPIO_IO29__LPI2C3_SCL 0x40000b9e + >; + }; +}; + &fec { phy-reset-gpios = <&pcal6524 16 GPIO_ACTIVE_LOW>; phy-reset-duration = <15>;

Hi Joy,
On Sun, Apr 7, 2024 at 5:57 AM Joy Zou joy.zou@nxp.com wrote:
support rtc PCF2131 for imx93.
Please improve the commit log:
- Start with a capital letter. - You are adding the RTC support for the imx93-evk board, not for imx93.
aliases {
rtc0 = &pcf2131;
};
There is no need for this extra blank line. Please drop it.
BTW, you should also add the alias in the patch you sent to Linux.
pcf2131: rtc@53 {
compatible = "nxp,pcf2131";
reg = <0x53>;
interrupt-parent = <&pcal6524>;
interrupts = <1 IRQ_TYPE_LEVEL_LOW>;
status = "okay";
The status should be dropped as per the feedback you received in the kernel submission.

Enable CONFIG_RTC_PCF2127 configs to support pcf2131.
Signed-off-by: Joy Zou joy.zou@nxp.com --- Changes in v3: 1. Change CONFIG_RTC_PCF2131 into CONFIG_RTC_PCF2127 because the pcf2131 driver have been merged into the pcf2127. --- configs/imx93_11x11_evk_defconfig | 1 + 1 file changed, 1 insertion(+)
diff --git a/configs/imx93_11x11_evk_defconfig b/configs/imx93_11x11_evk_defconfig index 63613477c7..55e2673291 100644 --- a/configs/imx93_11x11_evk_defconfig +++ b/configs/imx93_11x11_evk_defconfig @@ -14,6 +14,7 @@ CONFIG_DEFAULT_DEVICE_TREE="imx93-11x11-evk" CONFIG_SPL_TEXT_BASE=0x2049A000 CONFIG_TARGET_IMX93_11X11_EVK=y CONFIG_SYS_MONITOR_LEN=524288 +CONFIG_RTC_PCF2127=y CONFIG_SPL_SERIAL=y CONFIG_SPL_DRIVERS_MISC=y CONFIG_SPL_STACK=0x20519dd0
participants (2)
-
Fabio Estevam
-
Joy Zou