[PATCH v3 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 configs: Enable RTC pcf2131 support
arch/arm/dts/imx93-11x11-evk-u-boot.dtsi | 8 ++ arch/arm/dts/imx93-11x11-evk.dts | 25 ++++ configs/imx93_11x11_evk_defconfig | 1 + drivers/rtc/pcf2127.c | 144 +++++++++++++++++++++-- 4 files changed, 165 insertions(+), 13 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 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 | 144 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 131 insertions(+), 13 deletions(-)
diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c index 2f3fafb496..58ab5a8601 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 +}; + +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 = 0; + 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 = 0; + 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 = 0; + 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,7 +154,10 @@ 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; } @@ -76,7 +165,8 @@ static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) { int ret = 0; - uchar buf[10] = { PCF2127_REG_CTRL1 }; + 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, };

On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
+bool is_pcf2131_type(struct udevice *dev)
static bool
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 = 0;
No need to initialize ret with 0.
+static int pcf2131_rtc_unlock(struct udevice *dev) +{
int ret = 0;
Ditto.
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 = 0;
Ditto.

-----Original Message----- From: Fabio Estevam festevam@gmail.com Sent: 2024年3月27日 6:10 To: Joy Zou joy.zou@nxp.com Cc: Peng Fan peng.fan@nxp.com; Ye Li ye.li@nxp.com; Jacky Bai ping.bai@nxp.com; sbabic@denx.de; sjg@chromium.org; saproj@gmail.com; judge.packham@gmail.com; dl-uboot-imx uboot-imx@nxp.com; u-boot@lists.denx.de Subject: [EXT] Re: [PATCH v3 1/3] drivers: rtc: add pcf2131 rtc driver
Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
+bool is_pcf2131_type(struct udevice *dev)
static bool
Will add static key word!
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 = 0;
No need to initialize ret with 0.
Will remove initialization 0.
+static int pcf2131_rtc_unlock(struct udevice *dev) {
int ret = 0;
Ditto.
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 = 0;
Ditto.
Will remove initialization 0. Thanks for your comments! BR Joy Zou

support rtc pcf2131 for imx93.
Signed-off-by: Joy Zou joy.zou@nxp.com --- 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 | 8 ++++++++ arch/arm/dts/imx93-11x11-evk.dts | 25 ++++++++++++++++++++++++ 2 files changed, 33 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..2dbe33f073 100644 --- a/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi +++ b/arch/arm/dts/imx93-11x11-evk-u-boot.dtsi @@ -112,6 +112,10 @@ bootph-some-ram; };
+&lpi2c3 { + bootph-pre-ram; +}; + &{/soc@0/bus@44000000/i2c@44350000/pmic@25} { bootph-pre-ram; bootph-some-ram; @@ -127,6 +131,10 @@ bootph-some-ram; };
+&pinctrl_lpi2c3 { + bootph-pre-ram; +}; + &fec { phy-reset-gpios = <&pcal6524 16 GPIO_ACTIVE_LOW>; phy-reset-duration = <15>; diff --git a/arch/arm/dts/imx93-11x11-evk.dts b/arch/arm/dts/imx93-11x11-evk.dts index 4322cc3e11..8b57de3b4c 100644 --- a/arch/arm/dts/imx93-11x11-evk.dts +++ b/arch/arm/dts/imx93-11x11-evk.dts @@ -192,6 +192,24 @@ }; };
+&lpi2c3 { + #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"; + }; +}; + &lpuart1 { /* console */ pinctrl-names = "default"; pinctrl-0 = <&pinctrl_uart1>; @@ -254,6 +272,13 @@ >; };
+ pinctrl_lpi2c3: lpi2c3grp { + fsl,pins = < + MX93_PAD_GPIO_IO28__LPI2C3_SDA 0x40000b9e + MX93_PAD_GPIO_IO29__LPI2C3_SCL 0x40000b9e + >; + }; + pinctrl_fec: fecgrp { fsl,pins = < MX93_PAD_ENET2_MDC__ENET1_MDC 0x57e

On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
+&lpi2c3 {
#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";
Please submit the RTC support to Linux first, then you can sync the devicetree with Linux in U-Boot.
In the meantime, you can add the RTC support to the -u-boot.dtsi.
Please consider using OF_UPSTREAM available in the U-Boot next branch.

-----Original Message----- From: Fabio Estevam festevam@gmail.com Sent: 2024年3月27日 6:14 To: Joy Zou joy.zou@nxp.com Cc: Peng Fan peng.fan@nxp.com; Ye Li ye.li@nxp.com; Jacky Bai ping.bai@nxp.com; sbabic@denx.de; sjg@chromium.org; saproj@gmail.com; judge.packham@gmail.com; dl-uboot-imx uboot-imx@nxp.com; u-boot@lists.denx.de Subject: [EXT] Re: [PATCH v3 2/3] imx: imx93_evk: add rtc pcf2131
Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
+&lpi2c3 {
#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";
Please submit the RTC support to Linux first, then you can sync the devicetree with Linux in U-Boot.
In the meantime, you can add the RTC support to the -u-boot.dtsi.
Please consider using OF_UPSTREAM available in the U-Boot next branch.
Will submit the RTC support patch to Linux firstly. Thanks for your comments! BR Joy Zou

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

On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
Enable CONFIG_RTC_PCF2127 configs to support pcf2131.
Subject should be imx93_11x11_evk specific:
imx93_11x11_evk: Add PCF2131 RTC support

-----Original Message----- From: Fabio Estevam festevam@gmail.com Sent: 2024年3月27日 6:12 To: Joy Zou joy.zou@nxp.com Cc: Peng Fan peng.fan@nxp.com; Ye Li ye.li@nxp.com; Jacky Bai ping.bai@nxp.com; sbabic@denx.de; sjg@chromium.org; saproj@gmail.com; judge.packham@gmail.com; dl-uboot-imx uboot-imx@nxp.com; u-boot@lists.denx.de Subject: [EXT] Re: [PATCH v3 3/3] configs: Enable RTC pcf2131 support
Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
On Tue, Mar 26, 2024 at 12:30 AM Joy Zou joy.zou@nxp.com wrote:
Enable CONFIG_RTC_PCF2127 configs to support pcf2131.
Subject should be imx93_11x11_evk specific:
imx93_11x11_evk: Add PCF2131 RTC support
Will modify the Subject. Thanks for your comments!
BR Joy Zou
participants (2)
-
Fabio Estevam
-
Joy Zou