]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
pinctrl: pinconf-generic: Add API for pinmux propertity in DTS file
authorXianwei Zhao <xianwei.zhao@amlogic.com>
Wed, 12 Feb 2025 05:20:51 +0000 (13:20 +0800)
committerLinus Walleij <linus.walleij@linaro.org>
Thu, 27 Feb 2025 23:03:17 +0000 (00:03 +0100)
When describing pin mux func through pinmux propertity,
a standard API is added for support. The pinmux contains pin
identification and mux values, which can include multiple
pins. And groups configuration use other word. DTS such as:

func-name {
group_alias: group-name{
pinmux= <pin_id << 8 | mux_value)>,
<pin_id << 8 | mux_value)>;
bias-pull-up;
drive-strength-microamp = <4000>;
};
};

Signed-off-by: Xianwei Zhao <xianwei.zhao@amlogic.com>
Link: https://lore.kernel.org/20250212-amlogic-pinctrl-v5-2-282bc2516804@amlogic.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/pinconf-generic.c
drivers/pinctrl/pinconf.h
include/linux/pinctrl/pinconf-generic.h

index 0b13d7f17b32563ca82ef8c428920cea8bf14618..b8e5e441e67c506e8dad09ff960559709e11174c 100644 (file)
@@ -233,6 +233,67 @@ static void parse_dt_cfg(struct device_node *np,
        }
 }
 
+/**
+ * pinconf_generic_parse_dt_pinmux()
+ * parse the pinmux properties into generic pin mux values.
+ * @np: node containing the pinmux properties
+ * @dev: pincontrol core device
+ * @pid: array with pin identity entries
+ * @pmux: array with pin mux value entries
+ * @npins: number of pins
+ *
+ * pinmux propertity: mux value [0,7]bits and pin identity [8,31]bits.
+ */
+int pinconf_generic_parse_dt_pinmux(struct device_node *np, struct device *dev,
+                                   unsigned int **pid, unsigned int **pmux,
+                                   unsigned int *npins)
+{
+       unsigned int *pid_t;
+       unsigned int *pmux_t;
+       struct property *prop;
+       unsigned int npins_t, i;
+       u32 value;
+       int ret;
+
+       prop = of_find_property(np, "pinmux", NULL);
+       if (!prop) {
+               dev_info(dev, "Missing pinmux property\n");
+               return -ENOENT;
+       }
+
+       if (!pid || !pmux || !npins) {
+               dev_err(dev, "paramers error\n");
+               return -EINVAL;
+       }
+
+       npins_t = prop->length / sizeof(u32);
+       pid_t = devm_kcalloc(dev, npins_t, sizeof(*pid_t), GFP_KERNEL);
+       pmux_t = devm_kcalloc(dev, npins_t, sizeof(*pmux_t), GFP_KERNEL);
+       if (!pid_t || !pmux_t) {
+               dev_err(dev, "kalloc memory fail\n");
+               return -ENOMEM;
+       }
+       for (i = 0; i < npins_t; i++) {
+               ret = of_property_read_u32_index(np, "pinmux", i, &value);
+               if (ret) {
+                       dev_err(dev, "get pinmux value fail\n");
+                       goto exit;
+               }
+               pmux_t[i] = value & 0xff;
+               pid_t[i] = (value >> 8) & 0xffffff;
+       }
+       *pid = pid_t;
+       *pmux = pmux_t;
+       *npins = npins_t;
+
+       return 0;
+exit:
+       devm_kfree(dev, pid_t);
+       devm_kfree(dev, pmux_t);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_pinmux);
+
 /**
  * pinconf_generic_parse_dt_config()
  * parse the config properties into generic pinconfig values.
@@ -295,6 +356,75 @@ out:
 }
 EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config);
 
+int pinconf_generic_dt_node_to_map_pinmux(struct pinctrl_dev *pctldev,
+                                         struct device_node *np,
+                                         struct pinctrl_map **map,
+                                         unsigned int *num_maps)
+{
+       struct device *dev = pctldev->dev;
+       struct device_node *pnode;
+       unsigned long *configs = NULL;
+       unsigned int num_configs = 0;
+       struct property *prop;
+       unsigned int reserved_maps;
+       int reserve;
+       int ret;
+
+       prop = of_find_property(np, "pinmux", NULL);
+       if (!prop) {
+               dev_info(dev, "Missing pinmux property\n");
+               return -ENOENT;
+       }
+
+       pnode = of_get_parent(np);
+       if (!pnode) {
+               dev_info(dev, "Missing function node\n");
+               return -EINVAL;
+       }
+
+       reserved_maps = 0;
+       *map = NULL;
+       *num_maps = 0;
+
+       ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
+                                             &num_configs);
+       if (ret < 0) {
+               dev_err(dev, "%pOF: could not parse node property\n", np);
+               return ret;
+       }
+
+       reserve = 1;
+       if (num_configs)
+               reserve++;
+
+       ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps,
+                                       num_maps, reserve);
+       if (ret < 0)
+               goto exit;
+
+       ret = pinctrl_utils_add_map_mux(pctldev, map,
+                                       &reserved_maps, num_maps, np->name,
+                                       pnode->name);
+       if (ret < 0)
+               goto exit;
+
+       if (num_configs) {
+               ret = pinctrl_utils_add_map_configs(pctldev, map, &reserved_maps,
+                                                   num_maps, np->name, configs,
+                                                   num_configs, PIN_MAP_TYPE_CONFIGS_GROUP);
+               if (ret < 0)
+                       goto exit;
+       }
+
+exit:
+       kfree(configs);
+       if (ret)
+               pinctrl_utils_free_map(pctldev, *map, *num_maps);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map_pinmux);
+
 int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
                struct device_node *np, struct pinctrl_map **map,
                unsigned int *reserved_maps, unsigned int *num_maps,
index a14c950bc70041f135b16a17f3747c61bdbf0f84..a171195b36159430058c36061148ceb10bbd9092 100644 (file)
@@ -138,4 +138,8 @@ int pinconf_generic_parse_dt_config(struct device_node *np,
                                    struct pinctrl_dev *pctldev,
                                    unsigned long **configs,
                                    unsigned int *nconfigs);
+
+int pinconf_generic_parse_dt_pinmux(struct device_node *np, struct device *dev,
+                                   unsigned int **pid, unsigned int **pmux,
+                                   unsigned int *npins);
 #endif
index 53cfde98433d8e5fc0a3e4e4b734a9ddb3311131..1bcf071b860ebbaf60351a8d17369ba6cd3eff1b 100644 (file)
@@ -232,4 +232,8 @@ static inline int pinconf_generic_dt_node_to_map_all(struct pinctrl_dev *pctldev
                        PIN_MAP_TYPE_INVALID);
 }
 
+int pinconf_generic_dt_node_to_map_pinmux(struct pinctrl_dev *pctldev,
+                                         struct device_node *np,
+                                         struct pinctrl_map **map,
+                                         unsigned int *num_maps);
 #endif /* __LINUX_PINCTRL_PINCONF_GENERIC_H */