@@ -70,6 +70,8 @@ static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
7070
7171#define GPIO_OFFSET_VALID (chip , offset ) (offset >= 0 && offset < chip->ngpio)
7272
73+ static DEFINE_MUTEX (gpio_lookup_lock );
74+ static LIST_HEAD (gpio_lookup_list );
7375static LIST_HEAD (gpio_chips );
7476
7577#ifdef CONFIG_GPIO_SYSFS
@@ -2192,6 +2194,207 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
21922194}
21932195EXPORT_SYMBOL_GPL (gpiod_set_value_cansleep );
21942196
2197+ /**
2198+ * gpiod_add_table() - register GPIO device consumers
2199+ * @table: array of consumers to register
2200+ * @num: number of consumers in table
2201+ */
2202+ void gpiod_add_table (struct gpiod_lookup * table , size_t size )
2203+ {
2204+ mutex_lock (& gpio_lookup_lock );
2205+
2206+ while (size -- ) {
2207+ list_add_tail (& table -> list , & gpio_lookup_list );
2208+ table ++ ;
2209+ }
2210+
2211+ mutex_unlock (& gpio_lookup_lock );
2212+ }
2213+
2214+ /*
2215+ * Caller must have a acquired gpio_lookup_lock
2216+ */
2217+ static struct gpio_chip * find_chip_by_name (const char * name )
2218+ {
2219+ struct gpio_chip * chip = NULL ;
2220+
2221+ list_for_each_entry (chip , & gpio_lookup_list , list ) {
2222+ if (chip -> label == NULL )
2223+ continue ;
2224+ if (!strcmp (chip -> label , name ))
2225+ break ;
2226+ }
2227+
2228+ return chip ;
2229+ }
2230+
2231+ #ifdef CONFIG_OF
2232+ static struct gpio_desc * of_find_gpio (struct device * dev , const char * con_id ,
2233+ unsigned int idx , unsigned long * flags )
2234+ {
2235+ char prop_name [32 ]; /* 32 is max size of property name */
2236+ enum of_gpio_flags of_flags ;
2237+ struct gpio_desc * desc ;
2238+
2239+ if (con_id )
2240+ snprintf (prop_name , 32 , "%s-gpios" , con_id );
2241+ else
2242+ snprintf (prop_name , 32 , "gpios" );
2243+
2244+ desc = of_get_named_gpiod_flags (dev -> of_node , prop_name , idx ,
2245+ & of_flags );
2246+
2247+ if (IS_ERR (desc ))
2248+ return desc ;
2249+
2250+ if (of_flags & OF_GPIO_ACTIVE_LOW )
2251+ * flags |= GPIOF_ACTIVE_LOW ;
2252+
2253+ return desc ;
2254+ }
2255+ #else
2256+ static struct gpio_desc * of_find_gpio (struct device * dev , const char * con_id ,
2257+ unsigned int idx , unsigned long * flags )
2258+ {
2259+ return ERR_PTR (- ENODEV );
2260+ }
2261+ #endif
2262+
2263+ static struct gpio_desc * gpiod_find (struct device * dev , const char * con_id ,
2264+ unsigned int idx , unsigned long * flags )
2265+ {
2266+ const char * dev_id = dev ? dev_name (dev ) : NULL ;
2267+ struct gpio_desc * desc = ERR_PTR (- ENODEV );
2268+ unsigned int match , best = 0 ;
2269+ struct gpiod_lookup * p ;
2270+
2271+ mutex_lock (& gpio_lookup_lock );
2272+
2273+ list_for_each_entry (p , & gpio_lookup_list , list ) {
2274+ match = 0 ;
2275+
2276+ if (p -> dev_id ) {
2277+ if (!dev_id || strcmp (p -> dev_id , dev_id ))
2278+ continue ;
2279+
2280+ match += 2 ;
2281+ }
2282+
2283+ if (p -> con_id ) {
2284+ if (!con_id || strcmp (p -> con_id , con_id ))
2285+ continue ;
2286+
2287+ match += 1 ;
2288+ }
2289+
2290+ if (p -> idx != idx )
2291+ continue ;
2292+
2293+ if (match > best ) {
2294+ struct gpio_chip * chip ;
2295+
2296+ chip = find_chip_by_name (p -> chip_label );
2297+
2298+ if (!chip ) {
2299+ dev_warn (dev , "cannot find GPIO chip %s\n" ,
2300+ p -> chip_label );
2301+ continue ;
2302+ }
2303+
2304+ if (chip -> ngpio >= p -> chip_hwnum ) {
2305+ dev_warn (dev , "GPIO chip %s has %d GPIOs\n" ,
2306+ chip -> label , chip -> ngpio );
2307+ continue ;
2308+ }
2309+
2310+ desc = gpio_to_desc (chip -> base + p -> chip_hwnum );
2311+ * flags = p -> flags ;
2312+
2313+ if (match != 3 )
2314+ best = match ;
2315+ else
2316+ break ;
2317+ }
2318+ }
2319+
2320+ mutex_unlock (& gpio_lookup_lock );
2321+
2322+ return desc ;
2323+ }
2324+
2325+ /**
2326+ * gpio_get - obtain a GPIO for a given GPIO function
2327+ * @dev: GPIO consumer
2328+ * @con_id: function within the GPIO consumer
2329+ *
2330+ * Return the GPIO descriptor corresponding to the function con_id of device
2331+ * dev, or an IS_ERR() condition if an error occured.
2332+ */
2333+ struct gpio_desc * __must_check gpiod_get (struct device * dev , const char * con_id )
2334+ {
2335+ return gpiod_get_index (dev , con_id , 0 );
2336+ }
2337+ EXPORT_SYMBOL_GPL (gpiod_get );
2338+
2339+ /**
2340+ * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2341+ * @dev: GPIO consumer
2342+ * @con_id: function within the GPIO consumer
2343+ * @idx: index of the GPIO to obtain in the consumer
2344+ *
2345+ * This variant of gpiod_get() allows to access GPIOs other than the first
2346+ * defined one for functions that define several GPIOs.
2347+ *
2348+ * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2349+ */
2350+ struct gpio_desc * __must_check gpiod_get_index (struct device * dev ,
2351+ const char * con_id ,
2352+ unsigned int idx )
2353+ {
2354+ struct gpio_desc * desc ;
2355+ int status ;
2356+ unsigned long flags = 0 ;
2357+
2358+ dev_dbg (dev , "GPIO lookup for consumer %s\n" , con_id );
2359+
2360+ /* Using device tree? */
2361+ if (IS_ENABLED (CONFIG_OF ) && dev && dev -> of_node ) {
2362+ dev_dbg (dev , "using device tree for GPIO lookup\n" );
2363+ desc = of_find_gpio (dev , con_id , idx , & flags );
2364+ } else {
2365+ dev_dbg (dev , "using lookup tables for GPIO lookup" );
2366+ desc = gpiod_find (dev , con_id , idx , & flags );
2367+ }
2368+
2369+ if (IS_ERR (desc )) {
2370+ dev_warn (dev , "lookup for GPIO %s failed\n" , con_id );
2371+ return desc ;
2372+ }
2373+
2374+ status = gpiod_request (desc , con_id );
2375+
2376+ if (status < 0 )
2377+ return ERR_PTR (status );
2378+
2379+ if (flags & GPIOF_ACTIVE_LOW )
2380+ set_bit (FLAG_ACTIVE_LOW , & desc -> flags );
2381+
2382+ return desc ;
2383+ }
2384+ EXPORT_SYMBOL_GPL (gpiod_get_index );
2385+
2386+ /**
2387+ * gpiod_put - dispose of a GPIO descriptor
2388+ * @desc: GPIO descriptor to dispose of
2389+ *
2390+ * No descriptor can be used after gpiod_put() has been called on it.
2391+ */
2392+ void gpiod_put (struct gpio_desc * desc )
2393+ {
2394+ gpiod_free (desc );
2395+ }
2396+ EXPORT_SYMBOL_GPL (gpiod_put );
2397+
21952398#ifdef CONFIG_DEBUG_FS
21962399
21972400static void gpiolib_dbg_show (struct seq_file * s , struct gpio_chip * chip )
0 commit comments