Skip to contentSkip to conversation

rtc: pcf2127: use const per-variant regmap_config tables

PATCH In review Akshat Rawat 3 participants 3 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: pcf2127: use const per-variant regmap_config tables

Both pcf2127_i2c_probe() and pcf2127_spi_probe() declare config as a static struct regmap_config and update it in place based on the chip variant.

In pcf2127_spi_probe(), if a PCF2131 probes first, read_flag_mask and write_flag_mask are overwritten to 0 and never restored. Any PCF2127 or PCF2129 probed later reuses those zeroed masks instead of 0xa0/0x20, breaking SPI register access. Modifying static max_register and flag masks also races if multiple instances probe concurrently.

Replace the shared mutable config structs in both probe paths with per-variant static const struct regmap_config tables, and drop the now-unused max_register field from struct pcf21xx_config.

Fixes fd28ceb4603f ("rtc: pcf2127: add variant-specific configuration structure")
Fixes fa78e9b606a4 ("rtc: pcf2127: fix SPI command byte for PCF2131")

---
Compile-tested (x86_64 gcc 15.2.0, W=1, CONFIG_WERROR=y) with I2C+SPI, I2C-only, and SPI-only configs; not tested on hardware.

 drivers/rtc/rtc-pcf2127.c | 65 +++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 23 deletions(-)
drivers/rtc/rtc-pcf2127.c +42 -23
index 27c5fb9b2aaa..b665728c6c1d 100644
@@ -186,7 +186,6 @@ struct pcf21xx_ts_config {
 
 struct pcf21xx_config {
 	int type; /* IC variant */
-	int max_register;
 	unsigned int has_nvmem:1;
 	unsigned int has_bit_wd_ctl_cd0:1;
 	unsigned int wd_val_reg_readable:1; /* If watchdog value register can be read. */
@@ -1020,7 +1019,6 @@ static struct attribute *pcf2131_attrs[] = {
 static struct pcf21xx_config pcf21xx_cfg[] = {
 	[PCF2127] = {
 		.type = PCF2127,
-		.max_register = 0x1d,
 		.has_nvmem = 1,
 		.has_bit_wd_ctl_cd0 = 1,
 		.wd_val_reg_readable = 1,
@@ -1048,7 +1046,6 @@ static struct pcf21xx_config pcf21xx_cfg[] = {
 	},
 	[PCF2129] = {
 		.type = PCF2129,
-		.max_register = 0x19,
 		.has_nvmem = 0,
 		.has_bit_wd_ctl_cd0 = 0,
 		.wd_val_reg_readable = 1,
@@ -1076,7 +1073,6 @@ static struct pcf21xx_config pcf21xx_cfg[] = {
 	},
 	[PCF2131] = {
 		.type = PCF2131,
-		.max_register = 0x36,
 		.has_nvmem = 0,
 		.has_bit_wd_ctl_cd0 = 0,
 		.wd_val_reg_readable = 0,
@@ -1452,13 +1448,27 @@ static const struct i2c_device_id pcf2127_i2c_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
 
+static const struct regmap_config pcf2127_i2c_regmap_config[PCF21XX_LAST_ID] = {
+	[PCF2127] = {
+		.reg_bits = 8,
+		.val_bits = 8,
+		.max_register = 0x1d,
+	},
+	[PCF2129] = {
+		.reg_bits = 8,
+		.val_bits = 8,
+		.max_register = 0x19,
+	},
+	[PCF2131] = {
+		.reg_bits = 8,
+		.val_bits = 8,
+		.max_register = 0x36,
+	},
+};
+
 static int pcf2127_i2c_probe(struct i2c_client *client)
 {
 	struct regmap *regmap;
-	static struct regmap_config config = {
-		.reg_bits = 8,
-		.val_bits = 8,
-	};
 	const struct pcf21xx_config *variant;
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
@@ -1468,10 +1478,9 @@ static int pcf2127_i2c_probe(struct i2c_client *client)
 	if (!variant)
 		return -ENODEV;
 
-	config.max_register = variant->max_register,
-
 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
-					&client->dev, &config);
+				  &client->dev,
+				  &pcf2127_i2c_regmap_config[variant->type]);
 	if (IS_ERR(regmap)) {
 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
 			__func__, PTR_ERR(regmap));
@@ -1518,14 +1527,30 @@ static void pcf2127_i2c_unregister_driver(void)
 static struct spi_driver pcf2127_spi_driver;
 static const struct spi_device_id pcf2127_spi_id[];
 
-static int pcf2127_spi_probe(struct spi_device *spi)
-{
-	static struct regmap_config config = {
+static const struct regmap_config pcf2127_spi_regmap_config[PCF21XX_LAST_ID] = {
+	[PCF2127] = {
+		.reg_bits = 8,
+		.val_bits = 8,
+		.read_flag_mask = 0xa0,
+		.write_flag_mask = 0x20,
+		.max_register = 0x1d,
+	},
+	[PCF2129] = {
 		.reg_bits = 8,
 		.val_bits = 8,
 		.read_flag_mask = 0xa0,
 		.write_flag_mask = 0x20,
-	};
+		.max_register = 0x19,
+	},
+	[PCF2131] = {
+		.reg_bits = 8,
+		.val_bits = 8,
+		.max_register = 0x36,
+	},
+};
+
+static int pcf2127_spi_probe(struct spi_device *spi)
+{
 	struct regmap *regmap;
 	const struct pcf21xx_config *variant;
 
@@ -1541,14 +1566,8 @@ static int pcf2127_spi_probe(struct spi_device *spi)
 		variant = &pcf21xx_cfg[type];
 	}
 
-	if (variant->type == PCF2131) {
-		config.read_flag_mask = 0x0;
-		config.write_flag_mask = 0x0;
-	}
-
-	config.max_register = variant->max_register;
-
-	regmap = devm_regmap_init_spi(spi, &config);
+	regmap = devm_regmap_init_spi(spi,
+				      &pcf2127_spi_regmap_config[variant->type]);
 	if (IS_ERR(regmap)) {
 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
 			__func__, PTR_ERR(regmap));
Signature · 1 line
2.56.1.windows.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Both pcf2127_i2c_probe() and pcf2127_spi_probe() declare config as a static struct regmap_config and update it in place based on the chip variant.

In pcf2127_spi_probe(), if a PCF2131 probes first, read_flag_mask and write_flag_mask are overwritten to 0 and never restored. Any PCF2127 or PCF2129 probed later reuses those zeroed masks instead of 0xa0/0x20, breaking SPI register access. Modifying static max_register and flag masks also races if multiple instances probe concurrently.

Quoted 8 lines

Replace the shared mutable config structs in both probe paths with per-variant static const struct regmap_config tables, and drop the now-unused max_register field from struct pcf21xx_config.

Fixes fd28ceb4603f ("rtc: pcf2127: add variant-specific configuration structure")
Fixes fa78e9b606a4 ("rtc: pcf2127: fix SPI command byte for PCF2131")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Akshat Rawat

Hi Akshat,

Quoted 2 lines · On Wed, 25 Nov 2026 12:44:15 +0000 Akshat Rawat <[email protected]>…

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Your commit title message should indicate why you do it, not how. If you fix a bug, it should say so.

Quoted 3 lines

Both pcf2127_i2c_probe() and pcf2127_spi_probe() declare config as a static struct regmap_config and update it in place based on the chip variant.

Your entire patch can be simplified by simply removing the static qualifier from structs in pcf2127_i2c_probe() and pcf2127_spi_probe().

Quoted 157 lines

In pcf2127_spi_probe(), if a PCF2131 probes first, read_flag_mask and write_flag_mask are overwritten to 0 and never restored. Any PCF2127 or PCF2129 probed later reuses those zeroed masks instead of 0xa0/0x20, breaking SPI register access. Modifying static max_register and flag masks also races if multiple instances probe concurrently.

Replace the shared mutable config structs in both probe paths with per-variant static const struct regmap_config tables, and drop the now-unused max_register field from struct pcf21xx_config.

Fixes fd28ceb4603f ("rtc: pcf2127: add variant-specific configuration structure")
Fixes fa78e9b606a4 ("rtc: pcf2127: fix SPI command byte for PCF2131")

---
Compile-tested (x86_64 gcc 15.2.0, W=1, CONFIG_WERROR=y) with I2C+SPI, I2C-only, and SPI-only configs; not tested on hardware.

 drivers/rtc/rtc-pcf2127.c | 65 +++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 23 deletions(-)
drivers/rtc/rtc-pcf2127.c +42 -24
index 27c5fb9b2aaa..b665728c6c1d 100644
@@ -186,7 +186,6 @@ struct pcf21xx_ts_config {
 
 struct pcf21xx_config {
        int type; /* IC variant */
-       int max_register;
        unsigned int has_nvmem:1;
        unsigned int has_bit_wd_ctl_cd0:1;
        unsigned int wd_val_reg_readable:1; /* If watchdog value register can be read. */
@@ -1020,7 +1019,6 @@ static struct attribute *pcf2131_attrs[] = {
 static struct pcf21xx_config pcf21xx_cfg[] = {
        [PCF2127] = {
                .type = PCF2127,
-               .max_register = 0x1d,
                .has_nvmem = 1,
                .has_bit_wd_ctl_cd0 = 1,
                .wd_val_reg_readable = 1,
@@ -1048,7 +1046,6 @@ static struct pcf21xx_config pcf21xx_cfg[] = {
        },
        [PCF2129] = {
                .type = PCF2129,
-               .max_register = 0x19,
                .has_nvmem = 0,
                .has_bit_wd_ctl_cd0 = 0,
                .wd_val_reg_readable = 1,
@@ -1076,7 +1073,6 @@ static struct pcf21xx_config pcf21xx_cfg[] = {
        },
        [PCF2131] = {
                .type = PCF2131,
-               .max_register = 0x36,
                .has_nvmem = 0,
                .has_bit_wd_ctl_cd0 = 0,
                .wd_val_reg_readable = 0,
@@ -1452,13 +1448,27 @@ static const struct i2c_device_id pcf2127_i2c_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
 
+static const struct regmap_config pcf2127_i2c_regmap_config[PCF21XX_LAST_ID] = {
+       [PCF2127] = {
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0x1d,
+       },
+       [PCF2129] = {
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0x19,
+       },
+       [PCF2131] = {
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0x36,
+       },
+};
+
 static int pcf2127_i2c_probe(struct i2c_client *client)
 {
        struct regmap *regmap;
-       static struct regmap_config config = {
-               .reg_bits = 8,
-               .val_bits = 8,
-       };
        const struct pcf21xx_config *variant;
 
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
@@ -1468,10 +1478,9 @@ static int pcf2127_i2c_probe(struct i2c_client *client)
        if (!variant)
                return -ENODEV;
 
-       config.max_register = variant->max_register,
-
        regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
-                                       &client->dev, &config);
+                                 &client->dev,
+                                 &pcf2127_i2c_regmap_config[variant->type]);
        if (IS_ERR(regmap)) {
                dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
                        __func__, PTR_ERR(regmap));
@@ -1518,14 +1527,30 @@ static void pcf2127_i2c_unregister_driver(void)
 static struct spi_driver pcf2127_spi_driver;
 static const struct spi_device_id pcf2127_spi_id[];
 
-static int pcf2127_spi_probe(struct spi_device *spi)
-{
-       static struct regmap_config config = {
+static const struct regmap_config pcf2127_spi_regmap_config[PCF21XX_LAST_ID] = {
+       [PCF2127] = {
+               .reg_bits = 8,
+               .val_bits = 8,
+               .read_flag_mask = 0xa0,
+               .write_flag_mask = 0x20,
+               .max_register = 0x1d,
+       },
+       [PCF2129] = {
                .reg_bits = 8,
                .val_bits = 8,
                .read_flag_mask = 0xa0,
                .write_flag_mask = 0x20,
-       };
+               .max_register = 0x19,
+       },
+       [PCF2131] = {
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0x36,
+       },
+};
+
+static int pcf2127_spi_probe(struct spi_device *spi)
+{
        struct regmap *regmap;
        const struct pcf21xx_config *variant;
 
@@ -1541,14 +1566,8 @@ static int pcf2127_spi_probe(struct spi_device *spi)
                variant = &pcf21xx_cfg[type];
        }
 
-       if (variant->type == PCF2131) {
-               config.read_flag_mask = 0x0;
-               config.write_flag_mask = 0x0;
-       }
-
-       config.max_register = variant->max_register;
-
-       regmap = devm_regmap_init_spi(spi, &config);
+       regmap = devm_regmap_init_spi(spi,
+                                     &pcf2127_spi_regmap_config[variant->type]);
        if (IS_ERR(regmap)) {
                dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
                        __func__, PTR_ERR(regmap));
--

2.56.1.windows.1

Signature · 1 line
Hugo Villeneuve