Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * OF helpers for the GPIO API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2007-2008  MontaVista Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/pinctrl/pinctrl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/gpio/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "gpiolib-of.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * @dev:    Consuming device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * @con_id: Function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * Some elder GPIO controllers need special quirks. Currently we handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * the Freescale and PPC GPIO controller with bindings that doesn't use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * established "cs-gpios" for chip selects but instead rely on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * "gpios" for the chip select lines. If we detect this, we redirect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * the counting of "cs-gpios" to count "gpios" transparent to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	if (!con_id || strcmp(con_id, "cs"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	if (!of_device_is_compatible(np, "fsl,spi") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	    !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	return of_gpio_named_count(np, "gpios");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * FIXME: get rid of those external users by converting them to GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * descriptors and let them all use gpiod_count()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) int of_gpio_get_count(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	char propname[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	ret = of_gpio_spi_cs_get_count(dev, con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		if (con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 			snprintf(propname, sizeof(propname), "%s-%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 				 con_id, gpio_suffixes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 			snprintf(propname, sizeof(propname), "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 				 gpio_suffixes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		ret = of_gpio_named_count(dev->of_node, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return ret ? ret : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	struct of_phandle_args *gpiospec = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	return chip->gpiodev->dev.of_node == gpiospec->np &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 				chip->of_xlate &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static struct gpio_chip *of_find_gpiochip_by_xlate(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 					struct of_phandle_args *gpiospec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 					struct of_phandle_args *gpiospec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 					enum of_gpio_flags *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	if (chip->of_gpio_n_cells != gpiospec->args_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	ret = chip->of_xlate(chip, gpiospec, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	return gpiochip_get_desc(chip, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * to set the .valid_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * @gc: the target gpio_chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * Return: true if the valid mask needs to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct device_node *np = gc->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	if (size > 0 && size % 2 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static void of_gpio_flags_quirks(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 				 const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 				 enum of_gpio_flags *flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 				 int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	 * Some GPIO fixed regulator quirks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	 * Note that active low is the default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (IS_ENABLED(CONFIG_REGULATOR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	    (of_device_is_compatible(np, "regulator-fixed") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	     (!(strcmp(propname, "enable-gpio") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		strcmp(propname, "enable-gpios")) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	      of_device_is_compatible(np, "regulator-gpio")))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		bool active_low = !of_property_read_bool(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 							 "enable-active-high");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		 * The regulator GPIO handles are specified such that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		 * presence or absence of "enable-active-high" solely controls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		 * the polarity of the GPIO line. Any phandle flags must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		 * be actively ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			pr_warn("%s GPIO handle specifies active low - ignored\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 				of_node_full_name(np));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			*flags &= ~OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		if (active_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			*flags |= OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	 * Legacy open drain handling for fixed voltage regulators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	if (IS_ENABLED(CONFIG_REGULATOR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	    of_property_read_bool(np, "gpio-open-drain")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			of_node_full_name(np));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	 * Legacy handling of SPI active high chip select. If we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	 * property named "cs-gpios" we need to inspect the child node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	 * to determine if the flags should have inverted semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	    of_property_read_bool(np, "cs-gpios")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		u32 cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		for_each_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			ret = of_property_read_u32(child, "reg", &cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 			if (cs == index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 				 * SPI children have active low chip selects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				 * by default. This can be specified negatively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				 * by just omitting "spi-cs-high" in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 				 * device node, or actively by tagging on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 				 * GPIO_ACTIVE_LOW as flag in the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 				 * tree. If the line is simultaneously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 				 * tagged as active low in the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 				 * and has the "spi-cs-high" set, we get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 				 * conflict and the "spi-cs-high" flag will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 				 * take precedence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				if (of_property_read_bool(child, "spi-cs-high")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 					if (*flags & OF_GPIO_ACTIVE_LOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 						pr_warn("%s GPIO handle specifies active low - ignored\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 							of_node_full_name(child));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 						*flags &= ~OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 						pr_info("%s enforce active low on chipselect handle\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 							of_node_full_name(child));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 					*flags |= OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 				of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* Legacy handling of stmmac's active-low PHY reset line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	    !strcmp(propname, "snps,reset-gpio") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	    of_property_read_bool(np, "snps,reset-active-low"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		*flags |= OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  * @np:		device node to get GPIO from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * @propname:	property name containing gpio specifier(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * @index:	index of the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * @flags:	a flags pointer to fill in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  * value on the error condition. If @flags is not NULL the function also fills
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  * in flags for the GPIO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		     const char *propname, int index, enum of_gpio_flags *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct of_phandle_args gpiospec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct gpio_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 					     &gpiospec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			__func__, propname, np, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	chip = of_find_gpiochip_by_xlate(&gpiospec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (!chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		desc = ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		of_gpio_flags_quirks(np, propname, flags, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		 __func__, propname, np, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		 PTR_ERR_OR_ZERO(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	of_node_put(gpiospec.np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			    int index, enum of_gpio_flags *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		return desc_to_gpio(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * @node:	handle of the OF node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * @propname:	name of the DT property representing the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * @index:	index of the GPIO to obtain for the consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * @dflags:	GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  * @label:	label to attach to the requested GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  * On successful request the GPIO pin is configured in accordance with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  * provided @dflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  * In case of error an ERR_PTR() is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 					 const char *propname, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 					 enum gpiod_flags dflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 					 const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	enum of_gpio_flags flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	bool active_low = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	bool single_ended = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	bool open_drain = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	bool transitory = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	desc = of_get_named_gpiod_flags(node, propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 					index, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (!desc || IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	active_low = flags & OF_GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	single_ended = flags & OF_GPIO_SINGLE_ENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	open_drain = flags & OF_GPIO_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	transitory = flags & OF_GPIO_TRANSITORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	ret = gpiod_request(desc, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (active_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		lflags |= GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (single_ended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		if (open_drain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			lflags |= GPIO_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			lflags |= GPIO_OPEN_SOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	if (transitory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		lflags |= GPIO_TRANSITORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (flags & OF_GPIO_PULL_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		lflags |= GPIO_PULL_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (flags & OF_GPIO_PULL_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		lflags |= GPIO_PULL_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		gpiod_put(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  * The SPI GPIO bindings happened before we managed to establish that GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * properties should be named "foo-gpios" so we have this special kludge for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  * them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 					  enum of_gpio_flags *of_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	char prop_name[32]; /* 32 is max size of property name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	 * Hopefully the compiler stubs the rest of the function if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 * is false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	/* Allow this specifically for "spi-gpio" devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * The old Freescale bindings use simply "gpios" as name for the chip select
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  * with a special quirk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 					     const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 					     unsigned int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 					     unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	/* Allow this specifically for Freescale and PPC devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (!of_device_is_compatible(np, "fsl,spi") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	    !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	/* Allow only if asking for "cs-gpios" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (!con_id || strcmp(con_id, "cs"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	 * While all other SPI controllers use "cs-gpios" the Freescale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	 * uses just "gpios" so translate to that when "cs-gpios" is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	 * requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	return of_find_gpio(dev, NULL, idx, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * Some regulator bindings happened before we managed to establish that GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * properties should be named "foo-gpios" so we have this special kludge for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 						enum of_gpio_flags *of_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	/* These are the connection IDs we accept as legacy GPIO phandles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	const char *whitelist[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		"wlf,ldoena", /* Arizona */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		"wlf,ldo1ena", /* WM8994 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		"wlf,ldo2ena", /* WM8994 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	if (!IS_ENABLED(CONFIG_REGULATOR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	if (!con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 					      const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 					      enum of_gpio_flags *of_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (!con_id || strcmp(con_id, "wlf,reset"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) static struct gpio_desc *of_find_usb_gpio(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 					  const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 					  enum of_gpio_flags *of_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	 * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	 * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	 * suffix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (!con_id || strcmp(con_id, "fcs,int_n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			       unsigned int idx, unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	char prop_name[32]; /* 32 is max size of property name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	enum of_gpio_flags of_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		if (con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 				 gpio_suffixes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			snprintf(prop_name, sizeof(prop_name), "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 				 gpio_suffixes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 						&of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (PTR_ERR(desc) == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		/* Special handling for SPI GPIOs if used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (PTR_ERR(desc) == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		/* This quirk looks up flags and all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		if (!IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	if (PTR_ERR(desc) == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		/* Special handling for regulator GPIOs if used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (PTR_ERR(desc) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		desc = of_find_arizona_gpio(dev, con_id, &of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (PTR_ERR(desc) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		desc = of_find_usb_gpio(dev, con_id, &of_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (of_flags & OF_GPIO_ACTIVE_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		*flags |= GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (of_flags & OF_GPIO_OPEN_DRAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			*flags |= GPIO_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			*flags |= GPIO_OPEN_SOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (of_flags & OF_GPIO_TRANSITORY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		*flags |= GPIO_TRANSITORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (of_flags & OF_GPIO_PULL_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		*flags |= GPIO_PULL_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (of_flags & OF_GPIO_PULL_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		*flags |= GPIO_PULL_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564)  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565)  * @np:		device node to get GPIO from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)  * @chip:	GPIO chip whose hog is parsed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  * @idx:	Index of the GPIO to parse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  * @name:	GPIO line name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569)  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570)  *		of_find_gpio() or of_parse_own_gpio()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571)  * @dflags:	gpiod_flags - optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573)  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)  * value on the error condition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 					   struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 					   unsigned int idx, const char **name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 					   unsigned long *lflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 					   enum gpiod_flags *dflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct device_node *chip_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	enum of_gpio_flags xlate_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct of_phandle_args gpiospec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	chip_np = chip->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	if (!chip_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	xlate_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	*dflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	gpiospec.np = chip_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	gpiospec.args_count = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	for (i = 0; i < tmp; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 						 &gpiospec.args[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		*lflags |= GPIO_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (xlate_flags & OF_GPIO_TRANSITORY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		*lflags |= GPIO_TRANSITORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (xlate_flags & OF_GPIO_PULL_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		*lflags |= GPIO_PULL_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (xlate_flags & OF_GPIO_PULL_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		*lflags |= GPIO_PULL_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (of_property_read_bool(np, "input"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		*dflags |= GPIOD_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	else if (of_property_read_bool(np, "output-low"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		*dflags |= GPIOD_OUT_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	else if (of_property_read_bool(np, "output-high"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		*dflags |= GPIOD_OUT_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			desc_to_gpio(desc), np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (name && of_property_read_string(np, "line-name", name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		*name = np->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644)  * of_gpiochip_add_hog - Add all hogs in a hog device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * @chip:	gpio chip to act on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * @hog:	device node describing the hogs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * Returns error if it fails otherwise 0 on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	enum gpiod_flags dflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	unsigned long lflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	for (i = 0;; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		ret = gpiod_hog(desc, name, lflags, dflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) #ifdef CONFIG_OF_DYNAMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		desc->hog = hog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677)  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678)  * @chip:	gpio chip to act on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  * This is only used by of_gpiochip_add to request/set GPIO initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  * configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * It returns error if it fails otherwise 0 on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	for_each_available_child_of_node(chip->of_node, np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		if (!of_property_read_bool(np, "gpio-hog"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		ret = of_gpiochip_add_hog(chip, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		of_node_set_flag(np, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) #ifdef CONFIG_OF_DYNAMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * @chip:	gpio chip to act on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * @hog:	device node describing the hogs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static void of_gpiochip_remove_hog(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 				   struct device_node *hog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	struct gpio_desc *descs = chip->gpiodev->descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	for (i = 0; i < chip->ngpio; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		    descs[i].hog == hog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			gpiochip_free_own_desc(&descs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	return chip->gpiodev->dev.of_node == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	return gpiochip_find(np, of_gpiochip_match_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			  void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	struct of_reconfig_data *rd = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	struct gpio_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	 * This only supports adding and removing complete gpio-hog nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	 * Modifying an existing gpio-hog node is not supported (except for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 * changing its "status" property, which is treated the same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	 * addition/removal).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	switch (of_reconfig_get_state_change(action, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	case OF_RECONFIG_CHANGE_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			return NOTIFY_OK;	/* not for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		chip = of_find_gpiochip_by_node(rd->dn->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (chip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			return NOTIFY_OK;	/* not for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		ret = of_gpiochip_add_hog(chip, rd->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			       rd->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			of_node_clear_flag(rd->dn, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			return notifier_from_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	case OF_RECONFIG_CHANGE_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			return NOTIFY_OK;	/* already depopulated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		chip = of_find_gpiochip_by_node(rd->dn->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		if (chip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			return NOTIFY_OK;	/* not for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		of_gpiochip_remove_hog(chip, rd->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		of_node_clear_flag(rd->dn, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) struct notifier_block gpio_of_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	.notifier_call = of_gpio_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) #endif /* CONFIG_OF_DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  * @gc:		pointer to the gpio_chip structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  * @gpiospec:	GPIO specifier as found in the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  * @flags:	a flags pointer to fill in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  * This is simple translation function, suitable for the most 1:1 mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * GPIO chips. This function performs only one sanity check: whether GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * is less than ngpios (that is specified in the gpio_chip).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) static int of_gpio_simple_xlate(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 				const struct of_phandle_args *gpiospec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 				u32 *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	 * We're discouraging gpio_cells < 2, since that way you'll have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	 * write your own xlate function (that will have to retrieve the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	 * number and the flags from a single gpio cell -- this is possible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	 * but not recommended).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (gc->of_gpio_n_cells < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (gpiospec->args[0] >= gc->ngpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		*flags = gpiospec->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	return gpiospec->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827)  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828)  * @np:		device node of the GPIO chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829)  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830)  * @data:	driver data to store in the struct gpio_chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832)  * To use this function you should allocate and fill mm_gc with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  * 1) In the gpio_chip structure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  *    - all the callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  *    - of_gpio_n_cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  *    - of_xlate callback (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * 3) In the of_mm_gpio_chip structure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  *    - save_regs callback (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  * If succeeded, this function will map bank's memory and will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  * do all necessary work for you. Then you'll able to use .regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * to manage GPIOs from the callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) int of_mm_gpiochip_add_data(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 			    struct of_mm_gpio_chip *mm_gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			    void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct gpio_chip *gc = &mm_gc->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (!gc->label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		goto err0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	mm_gc->regs = of_iomap(np, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (!mm_gc->regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	gc->base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (mm_gc->save_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		mm_gc->save_regs(mm_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	mm_gc->gc.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	ret = gpiochip_add_data(gc, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	iounmap(mm_gc->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	kfree(gc->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) err0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884)  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885)  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	struct gpio_chip *gc = &mm_gc->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (!mm_gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	gpiochip_remove(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	iounmap(mm_gc->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	kfree(gc->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	int len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	u32 start, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct device_node *np = chip->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (len < 0 || len % 2 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	for (i = 0; i < len; i += 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		of_property_read_u32_index(np, "gpio-reserved-ranges",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 					   i, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		of_property_read_u32_index(np, "gpio-reserved-ranges",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 					   i + 1, &count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		if (start >= chip->ngpio || start + count >= chip->ngpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		bitmap_clear(chip->valid_mask, start, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) #ifdef CONFIG_PINCTRL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct device_node *np = chip->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct of_phandle_args pinspec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct pinctrl_dev *pctldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	int index = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	static const char group_names_propname[] = "gpio-ranges-group-names";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct property *group_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	group_names = of_find_property(np, group_names_propname, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	for (;; index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				index, &pinspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		pctldev = of_pinctrl_get(pinspec.np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		of_node_put(pinspec.np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		if (!pctldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		if (pinspec.args[2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			if (group_names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				of_property_read_string_index(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 						group_names_propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 						index, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				if (strlen(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 						np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			/* npins != 0: linear range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			ret = gpiochip_add_pin_range(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 					pinctrl_dev_get_devname(pctldev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 					pinspec.args[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 					pinspec.args[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 					pinspec.args[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			/* npins == 0: special range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			if (pinspec.args[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 				pr_err("%pOF: Illegal gpio-range format.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 					np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			if (!group_names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 					np, group_names_propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			ret = of_property_read_string_index(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 						group_names_propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 						index, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			if (!strlen(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 				np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			ret = gpiochip_add_pingroup_range(chip, pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 						pinspec.args[0], name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) int of_gpiochip_add(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (!chip->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (!chip->of_xlate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		chip->of_gpio_n_cells = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		chip->of_xlate = of_gpio_simple_xlate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	of_gpiochip_init_valid_mask(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	ret = of_gpiochip_add_pin_range(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	of_node_get(chip->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	ret = of_gpiochip_scan_gpios(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		of_node_put(chip->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) void of_gpiochip_remove(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	of_node_put(chip->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	/* If the gpiochip has an assigned OF node this takes precedence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	if (gc->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		gdev->dev.of_node = gc->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		gc->of_node = gdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (gdev->dev.of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		gdev->dev.fwnode = of_fwnode_handle(gdev->dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }