Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpio: move hogs into GPIO core

Refactor line hogging code by moving the parts duplicated in
gpiolib-acpi-core.c and gpiolib-of.c into gpiolib.c, leaving just the
OF-specific bits in the latter.

This makes fwnode the primary API for setting up hogs and allows to use
software nodes in addition to ACPI and OF nodes.

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Linus Walleij <linusw@kernel.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://patch.msgid.link/20260309-gpio-hog-fwnode-v2-2-4e61f3dbf06a@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

+125 -199
-70
drivers/gpio/gpiolib-acpi-core.c
··· 1220 1220 } 1221 1221 } 1222 1222 1223 - static struct gpio_desc * 1224 - acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip, 1225 - struct fwnode_handle *fwnode, 1226 - const char **name, 1227 - unsigned long *lflags, 1228 - enum gpiod_flags *dflags) 1229 - { 1230 - struct gpio_chip *chip = achip->chip; 1231 - struct gpio_desc *desc; 1232 - u32 gpios[2]; 1233 - int ret; 1234 - 1235 - *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 1236 - *dflags = GPIOD_ASIS; 1237 - *name = NULL; 1238 - 1239 - ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, 1240 - ARRAY_SIZE(gpios)); 1241 - if (ret < 0) 1242 - return ERR_PTR(ret); 1243 - 1244 - desc = gpiochip_get_desc(chip, gpios[0]); 1245 - if (IS_ERR(desc)) 1246 - return desc; 1247 - 1248 - if (gpios[1]) 1249 - *lflags |= GPIO_ACTIVE_LOW; 1250 - 1251 - if (fwnode_property_present(fwnode, "input")) 1252 - *dflags |= GPIOD_IN; 1253 - else if (fwnode_property_present(fwnode, "output-low")) 1254 - *dflags |= GPIOD_OUT_LOW; 1255 - else if (fwnode_property_present(fwnode, "output-high")) 1256 - *dflags |= GPIOD_OUT_HIGH; 1257 - else 1258 - return ERR_PTR(-EINVAL); 1259 - 1260 - fwnode_property_read_string(fwnode, "line-name", name); 1261 - 1262 - return desc; 1263 - } 1264 - 1265 - static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip) 1266 - { 1267 - struct gpio_chip *chip = achip->chip; 1268 - 1269 - device_for_each_child_node_scoped(chip->parent, fwnode) { 1270 - unsigned long lflags; 1271 - enum gpiod_flags dflags; 1272 - struct gpio_desc *desc; 1273 - const char *name; 1274 - int ret; 1275 - 1276 - if (!fwnode_property_present(fwnode, "gpio-hog")) 1277 - continue; 1278 - 1279 - desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name, 1280 - &lflags, &dflags); 1281 - if (IS_ERR(desc)) 1282 - continue; 1283 - 1284 - ret = gpiod_hog(desc, name, lflags, dflags); 1285 - if (ret) { 1286 - dev_err(chip->parent, "Failed to hog GPIO\n"); 1287 - return; 1288 - } 1289 - } 1290 - } 1291 - 1292 1223 void acpi_gpiochip_add(struct gpio_chip *chip) 1293 1224 { 1294 1225 struct acpi_gpio_chip *acpi_gpio; ··· 1252 1321 } 1253 1322 1254 1323 acpi_gpiochip_request_regions(acpi_gpio); 1255 - acpi_gpiochip_scan_gpios(acpi_gpio); 1256 1324 acpi_dev_clear_dependencies(adev); 1257 1325 } 1258 1326
+16 -127
drivers/gpio/gpiolib-of.c
··· 10 10 #include <linux/device.h> 11 11 #include <linux/err.h> 12 12 #include <linux/errno.h> 13 + #include <linux/fwnode.h> 13 14 #include <linux/io.h> 14 15 #include <linux/module.h> 15 16 #include <linux/of.h> ··· 713 712 return desc; 714 713 } 715 714 716 - /** 717 - * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 718 - * @np: device node to get GPIO from 719 - * @chip: GPIO chip whose hog is parsed 720 - * @idx: Index of the GPIO to parse 721 - * @name: GPIO line name 722 - * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 723 - * of_find_gpio() or of_parse_own_gpio() 724 - * @dflags: gpiod_flags - optional GPIO initialization flags 725 - * 726 - * Returns: 727 - * GPIO descriptor to use with Linux GPIO API, or one of the errno 728 - * value on the error condition. 729 - */ 730 - static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 731 - struct gpio_chip *chip, 732 - unsigned int idx, const char **name, 733 - unsigned long *lflags, 734 - enum gpiod_flags *dflags) 715 + int of_gpiochip_get_lflags(struct gpio_chip *chip, 716 + struct fwnode_reference_args *gpiospec, 717 + unsigned long *lflags) 735 718 { 736 - struct device_node *chip_np; 737 719 enum of_gpio_flags xlate_flags; 738 - struct of_phandle_args gpiospec; 720 + struct of_phandle_args args; 739 721 struct gpio_desc *desc; 740 - unsigned int i; 741 - u32 tmp; 742 - int ret; 743 722 744 - chip_np = dev_of_node(&chip->gpiodev->dev); 745 - if (!chip_np) 746 - return ERR_PTR(-EINVAL); 723 + args.np = to_of_node(gpiospec->fwnode); 724 + args.args_count = gpiospec->nargs; 747 725 748 - xlate_flags = 0; 749 - *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 750 - *dflags = GPIOD_ASIS; 726 + for (int i = 0; i < args.args_count; i++) 727 + args.args[i] = gpiospec->args[i]; 751 728 752 - ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 753 - if (ret) 754 - return ERR_PTR(ret); 755 - 756 - gpiospec.np = chip_np; 757 - gpiospec.args_count = tmp; 758 - 759 - for (i = 0; i < tmp; i++) { 760 - ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 761 - &gpiospec.args[i]); 762 - if (ret) 763 - return ERR_PTR(ret); 764 - } 765 - 766 - desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 729 + desc = of_xlate_and_get_gpiod_flags(chip, &args, &xlate_flags); 767 730 if (IS_ERR(desc)) 768 - return desc; 731 + return PTR_ERR(desc); 769 732 770 733 *lflags = of_convert_gpio_flags(xlate_flags); 771 - 772 - if (of_property_read_bool(np, "input")) 773 - *dflags |= GPIOD_IN; 774 - else if (of_property_read_bool(np, "output-low")) 775 - *dflags |= GPIOD_OUT_LOW; 776 - else if (of_property_read_bool(np, "output-high")) 777 - *dflags |= GPIOD_OUT_HIGH; 778 - else { 779 - pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 780 - desc_to_gpio(desc), np); 781 - return ERR_PTR(-EINVAL); 782 - } 783 - 784 - if (name && of_property_read_string(np, "line-name", name)) 785 - *name = np->name; 786 - 787 - return desc; 788 - } 789 - 790 - /** 791 - * of_gpiochip_add_hog - Add all hogs in a hog device node 792 - * @chip: gpio chip to act on 793 - * @hog: device node describing the hogs 794 - * 795 - * Returns: 796 - * 0 on success, or negative errno on failure. 797 - */ 798 - static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) 799 - { 800 - enum gpiod_flags dflags; 801 - struct gpio_desc *desc; 802 - unsigned long lflags; 803 - const char *name; 804 - unsigned int i; 805 - int ret; 806 - 807 - for (i = 0;; i++) { 808 - desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); 809 - if (IS_ERR(desc)) 810 - break; 811 - 812 - ret = gpiod_hog(desc, name, lflags, dflags); 813 - if (ret < 0) 814 - return ret; 815 - 816 - #ifdef CONFIG_OF_DYNAMIC 817 - WRITE_ONCE(desc->hog, hog); 818 - #endif 819 - } 820 - 821 - return 0; 822 - } 823 - 824 - /** 825 - * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 826 - * @chip: gpio chip to act on 827 - * 828 - * This is only used by of_gpiochip_add to request/set GPIO initial 829 - * configuration. 830 - * 831 - * Returns: 832 - * 0 on success, or negative errno on failure. 833 - */ 834 - static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 835 - { 836 - int ret; 837 - 838 - for_each_available_child_of_node_scoped(dev_of_node(&chip->gpiodev->dev), np) { 839 - if (!of_property_read_bool(np, "gpio-hog")) 840 - continue; 841 - 842 - ret = of_gpiochip_add_hog(chip, np); 843 - if (ret < 0) 844 - return ret; 845 - 846 - of_node_set_flag(np, OF_POPULATED); 847 - } 848 734 849 735 return 0; 850 736 } ··· 787 899 if (!gdev) 788 900 return NOTIFY_DONE; /* not for us */ 789 901 790 - ret = of_gpiochip_add_hog(gpio_device_get_chip(gdev), rd->dn); 902 + ret = gpiochip_add_hog(gpio_device_get_chip(gdev), of_fwnode_handle(rd->dn)); 791 903 if (ret < 0) { 792 904 pr_err("%s: failed to add hogs for %pOF\n", __func__, 793 905 rd->dn); ··· 1066 1178 1067 1179 of_node_get(np); 1068 1180 1069 - ret = of_gpiochip_scan_gpios(chip); 1070 - if (ret) 1071 - of_node_put(np); 1181 + for_each_available_child_of_node_scoped(np, child) { 1182 + if (of_property_read_bool(child, "gpio-hog")) 1183 + of_node_set_flag(child, OF_POPULATED); 1184 + } 1072 1185 1073 1186 return ret; 1074 1187 }
+10
drivers/gpio/gpiolib-of.h
··· 10 10 11 11 struct device_node; 12 12 struct fwnode_handle; 13 + struct fwnode_reference_args; 13 14 14 15 struct gpio_chip; 15 16 struct gpio_desc; ··· 25 24 void of_gpiochip_remove(struct gpio_chip *gc); 26 25 bool of_gpiochip_instance_match(struct gpio_chip *gc, unsigned int index); 27 26 int of_gpio_count(const struct fwnode_handle *fwnode, const char *con_id); 27 + int of_gpiochip_get_lflags(struct gpio_chip *chip, 28 + struct fwnode_reference_args *gpiospec, 29 + unsigned long *lflags); 28 30 #else 29 31 static inline struct gpio_desc *of_find_gpio(struct device_node *np, 30 32 const char *con_id, ··· 47 43 const char *con_id) 48 44 { 49 45 return 0; 46 + } 47 + static inline int of_gpiochip_get_lflags(struct gpio_chip *chip, 48 + struct fwnode_reference_args *gpiospec, 49 + unsigned long *lflags) 50 + { 51 + return -ENOENT; 50 52 } 51 53 #endif /* CONFIG_OF_GPIO */ 52 54
+96 -2
drivers/gpio/gpiolib.c
··· 948 948 __func__, gc->label, hog->chip_hwnum, rv); 949 949 } 950 950 951 - static void machine_gpiochip_add(struct gpio_chip *gc) 951 + static void gpiochip_machine_hog_lines(struct gpio_chip *gc) 952 952 { 953 953 struct gpiod_hog *hog; 954 954 ··· 958 958 if (!strcmp(gc->label, hog->chip_label)) 959 959 gpiochip_machine_hog(gc, hog); 960 960 } 961 + } 962 + 963 + int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode) 964 + { 965 + struct fwnode_handle *gc_node = dev_fwnode(&gc->gpiodev->dev); 966 + struct fwnode_reference_args gpiospec; 967 + enum gpiod_flags dflags; 968 + struct gpio_desc *desc; 969 + unsigned long lflags; 970 + const char *name; 971 + int ret, argc; 972 + u32 gpios[3]; /* We support up to three-cell bindings. */ 973 + u32 cells; 974 + 975 + lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 976 + dflags = GPIOD_ASIS; 977 + name = NULL; 978 + 979 + argc = fwnode_property_count_u32(fwnode, "gpios"); 980 + if (argc < 0) 981 + return argc; 982 + if (argc > 3) 983 + return -EINVAL; 984 + 985 + ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, argc); 986 + if (ret < 0) 987 + return ret; 988 + 989 + if (is_of_node(fwnode)) { 990 + /* 991 + * OF-nodes need some additional special handling for 992 + * translating of devicetree flags. 993 + */ 994 + ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells); 995 + if (ret) 996 + return ret; 997 + if (!ret && argc != cells) 998 + return -EINVAL; 999 + 1000 + memset(&gpiospec, 0, sizeof(gpiospec)); 1001 + gpiospec.fwnode = fwnode; 1002 + gpiospec.nargs = argc; 1003 + 1004 + for (int i = 0; i < argc; i++) 1005 + gpiospec.args[i] = gpios[i]; 1006 + 1007 + ret = of_gpiochip_get_lflags(gc, &gpiospec, &lflags); 1008 + if (ret) 1009 + return ret; 1010 + } else { 1011 + /* 1012 + * GPIO_ACTIVE_LOW is currently the only lookup flag 1013 + * supported for non-OF firmware nodes. 1014 + */ 1015 + if (gpios[1]) 1016 + lflags |= GPIO_ACTIVE_LOW; 1017 + } 1018 + 1019 + if (fwnode_property_present(fwnode, "input")) 1020 + dflags |= GPIOD_IN; 1021 + else if (fwnode_property_present(fwnode, "output-low")) 1022 + dflags |= GPIOD_OUT_LOW; 1023 + else if (fwnode_property_present(fwnode, "output-high")) 1024 + dflags |= GPIOD_OUT_HIGH; 1025 + else 1026 + return -EINVAL; 1027 + 1028 + fwnode_property_read_string(fwnode, "line-name", &name); 1029 + 1030 + desc = gpiochip_get_desc(gc, gpios[0]); 1031 + if (IS_ERR(desc)) 1032 + return PTR_ERR(desc); 1033 + 1034 + return gpiod_hog(desc, name, lflags, dflags); 1035 + } 1036 + 1037 + static int gpiochip_hog_lines(struct gpio_chip *gc) 1038 + { 1039 + int ret; 1040 + 1041 + device_for_each_child_node_scoped(&gc->gpiodev->dev, fwnode) { 1042 + if (!fwnode_property_present(fwnode, "gpio-hog")) 1043 + continue; 1044 + 1045 + ret = gpiochip_add_hog(gc, fwnode); 1046 + if (ret) 1047 + return ret; 1048 + } 1049 + 1050 + gpiochip_machine_hog_lines(gc); 1051 + 1052 + return 0; 961 1053 } 962 1054 963 1055 static void gpiochip_setup_devs(void) ··· 1301 1209 1302 1210 acpi_gpiochip_add(gc); 1303 1211 1304 - machine_gpiochip_add(gc); 1212 + ret = gpiochip_hog_lines(gc); 1213 + if (ret) 1214 + goto err_remove_of_chip; 1305 1215 1306 1216 ret = gpiochip_irqchip_init_valid_mask(gc); 1307 1217 if (ret)
+3
drivers/gpio/gpiolib.h
··· 23 23 24 24 #define GPIOCHIP_NAME "gpiochip" 25 25 26 + struct fwnode_handle; 27 + 26 28 /** 27 29 * struct gpio_device - internal state container for GPIO devices 28 30 * @dev: the GPIO device struct ··· 276 274 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); 277 275 int gpiod_hog(struct gpio_desc *desc, const char *name, 278 276 unsigned long lflags, enum gpiod_flags dflags); 277 + int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode); 279 278 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); 280 279 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum); 281 280 const char *gpiod_get_label(struct gpio_desc *desc);