1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Software Node helpers for the GPIO API 4 * 5 * Copyright 2022 Google LLC 6 */ 7 8 #define pr_fmt(fmt) "gpiolib: swnode: " fmt 9 10 #include <linux/err.h> 11 #include <linux/errno.h> 12 #include <linux/export.h> 13 #include <linux/init.h> 14 #include <linux/kernel.h> 15 #include <linux/printk.h> 16 #include <linux/property.h> 17 #include <linux/string.h> 18 19 #include <linux/gpio/consumer.h> 20 #include <linux/gpio/driver.h> 21 #include <linux/gpio/property.h> 22 23 #include "gpiolib.h" 24 #include "gpiolib-swnode.h" 25 26 static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode) 27 { 28 const struct software_node *gdev_node; 29 30 gdev_node = to_software_node(fwnode); 31 if (!gdev_node) 32 goto fwnode_lookup; 33 34 /* 35 * Check for a special node that identifies undefined GPIOs, this is 36 * primarily used as a key for internal chip selects in SPI bindings. 37 */ 38 if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) && 39 gdev_node == &swnode_gpio_undefined) 40 return ERR_PTR(-ENOENT); 41 42 fwnode_lookup: 43 return gpio_device_find_by_fwnode(fwnode) ?: ERR_PTR(-EPROBE_DEFER); 44 } 45 46 static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode, 47 const char *propname, unsigned int idx, 48 struct fwnode_reference_args *args) 49 { 50 /* 51 * We expect all swnode-described GPIOs have GPIO number and 52 * polarity arguments, hence nargs is set to 2. 53 */ 54 return fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, args); 55 } 56 57 struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode, 58 const char *con_id, unsigned int idx, 59 unsigned long *flags) 60 { 61 const struct software_node *swnode; 62 struct fwnode_reference_args args; 63 struct gpio_desc *desc; 64 char propname[32]; /* 32 is max size of property name */ 65 int ret = 0; 66 67 swnode = to_software_node(fwnode); 68 if (!swnode) 69 return ERR_PTR(-EINVAL); 70 71 for_each_gpio_property_name(propname, con_id) { 72 ret = swnode_gpio_get_reference(fwnode, propname, idx, &args); 73 if (ret == 0) 74 break; 75 if (ret == -ENOTCONN) 76 /* 77 * -ENOTCONN for a software node reference lookup means 78 * that a remote struct software_node exists but has 79 * not yet been registered as a firmware node. Defer 80 * until this happens. 81 */ 82 return ERR_PTR(-EPROBE_DEFER); 83 } 84 if (ret) { 85 pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n", 86 __func__, propname, fwnode, idx); 87 return ERR_PTR(ret); 88 } 89 90 struct gpio_device *gdev __free(gpio_device_put) = 91 swnode_get_gpio_device(args.fwnode); 92 fwnode_handle_put(args.fwnode); 93 if (IS_ERR(gdev)) 94 return ERR_CAST(gdev); 95 96 desc = gpio_device_get_desc(gdev, args.args[0]); 97 *flags = args.args[1]; /* We expect native GPIO flags */ 98 99 pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n", 100 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc)); 101 102 return desc; 103 } 104 105 /** 106 * swnode_gpio_count - count the GPIOs associated with a device / function 107 * @fwnode: firmware node of the GPIO consumer, can be %NULL for 108 * system-global GPIOs 109 * @con_id: function within the GPIO consumer 110 * 111 * Returns: 112 * The number of GPIOs associated with a device / function or %-ENOENT, 113 * if no GPIO has been assigned to the requested function. 114 */ 115 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id) 116 { 117 struct fwnode_reference_args args; 118 char propname[32]; 119 int count; 120 121 /* 122 * This is not very efficient, but GPIO lists usually have only 123 * 1 or 2 entries. 124 */ 125 for_each_gpio_property_name(propname, con_id) { 126 count = 0; 127 while (swnode_gpio_get_reference(fwnode, propname, count, &args) == 0) { 128 fwnode_handle_put(args.fwnode); 129 count++; 130 } 131 if (count) 132 return count; 133 } 134 135 return -ENOENT; 136 } 137 138 #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) 139 /* 140 * A special node that identifies undefined GPIOs, this is primarily used as 141 * a key for internal chip selects in SPI bindings. 142 */ 143 const struct software_node swnode_gpio_undefined = { 144 .name = "swnode-gpio-undefined", 145 }; 146 EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, "GPIO_SWNODE"); 147 148 static int __init swnode_gpio_init(void) 149 { 150 int ret; 151 152 ret = software_node_register(&swnode_gpio_undefined); 153 if (ret < 0) 154 pr_err("failed to register swnode: %d\n", ret); 155 156 return ret; 157 } 158 subsys_initcall(swnode_gpio_init); 159 160 static void __exit swnode_gpio_cleanup(void) 161 { 162 software_node_unregister(&swnode_gpio_undefined); 163 } 164 __exitcall(swnode_gpio_cleanup); 165 #endif 166