[PATCH] extcon/rtk-type-c: Use flexible array member for config storage
From: Rosen Penev
Date: Mon Oct 12 2026 - 20:05:17 EST
Convert type_c_cfg from a pointer to a flexible array member embedded
in the type_c_data structure. This improves memory layout by combining
two separate allocations into one, reducing pointer indirection and
aligning with modern kernel FAM patterns.
Move device tree config retrieval earlier in probe to validate before
allocation. Allocate both type_c and type_c_cfg in a single devm_kzalloc
call using struct_size().
Assisted-by: LLM
Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
drivers/extcon/extcon-rtk-type-c.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/drivers/extcon/extcon-rtk-type-c.c b/drivers/extcon/extcon-rtk-type-c.c
index da0b3fd3676e..ae1a4726fb63 100644
--- a/drivers/extcon/extcon-rtk-type-c.c
+++ b/drivers/extcon/extcon-rtk-type-c.c
@@ -63,7 +63,6 @@ struct type_c_data {
struct gpio_desc *rd_ctrl_gpio_desc;
/* Parameters */
- struct type_c_cfg *type_c_cfg;
u32 dfp_mode_rp_en;
u32 ufp_mode_rd_en;
u32 cc1_code;
@@ -99,6 +98,8 @@ struct type_c_data {
struct dentry *debug_dir;
struct typec_port *port;
+
+ struct type_c_cfg type_c_cfg[];
};
/* Type C register offset */
@@ -1321,10 +1322,18 @@ static int extcon_rtk_type_c_probe(struct platform_device *pdev)
const struct type_c_cfg *type_c_cfg;
int ret = 0;
- type_c = devm_kzalloc(dev, sizeof(*type_c), GFP_KERNEL);
+ type_c_cfg = of_device_get_match_data(dev);
+ if (!type_c_cfg) {
+ dev_err(dev, "type_c config are not assigned!\n");
+ return -EINVAL;
+ }
+
+ type_c = devm_kzalloc(dev, struct_size(type_c, type_c_cfg, 1), GFP_KERNEL);
if (!type_c)
return -ENOMEM;
+ memcpy(type_c->type_c_cfg, type_c_cfg, sizeof(*type_c_cfg));
+
type_c->reg_base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(type_c->reg_base))
return PTR_ERR(type_c->reg_base);
@@ -1361,20 +1370,6 @@ static int extcon_rtk_type_c_probe(struct platform_device *pdev)
}
}
- type_c_cfg = of_device_get_match_data(dev);
- if (!type_c_cfg) {
- dev_err(dev, "type_c config are not assigned!\n");
- ret = -EINVAL;
- goto err;
- }
-
- type_c->type_c_cfg = devm_kzalloc(dev, sizeof(*type_c->type_c_cfg),
- GFP_KERNEL);
- if (!type_c->type_c_cfg)
- return -ENOMEM;
-
- memcpy(type_c->type_c_cfg, type_c_cfg, sizeof(*type_c_cfg));
-
if (setup_type_c_parameter(type_c)) {
dev_err(dev, "ERROR: %s to setup type c parameter!!", __func__);
ret = -EINVAL;
--
2.56.0