diff options
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 299 |
1 files changed, 265 insertions, 34 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 1ca9295b2c10..59eaa23767d8 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -315,6 +315,7 @@ EXPORT_SYMBOL_GPL(gpiochip_add); | |||
315 | 315 | ||
316 | /* Forward-declaration */ | 316 | /* Forward-declaration */ |
317 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); | 317 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
318 | static void gpiochip_free_hogs(struct gpio_chip *chip); | ||
318 | 319 | ||
319 | /** | 320 | /** |
320 | * gpiochip_remove() - unregister a gpio_chip | 321 | * gpiochip_remove() - unregister a gpio_chip |
@@ -333,6 +334,7 @@ void gpiochip_remove(struct gpio_chip *chip) | |||
333 | 334 | ||
334 | acpi_gpiochip_remove(chip); | 335 | acpi_gpiochip_remove(chip); |
335 | gpiochip_remove_pin_ranges(chip); | 336 | gpiochip_remove_pin_ranges(chip); |
337 | gpiochip_free_hogs(chip); | ||
336 | of_gpiochip_remove(chip); | 338 | of_gpiochip_remove(chip); |
337 | 339 | ||
338 | spin_lock_irqsave(&gpio_lock, flags); | 340 | spin_lock_irqsave(&gpio_lock, flags); |
@@ -866,6 +868,7 @@ static bool __gpiod_free(struct gpio_desc *desc) | |||
866 | clear_bit(FLAG_REQUESTED, &desc->flags); | 868 | clear_bit(FLAG_REQUESTED, &desc->flags); |
867 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); | 869 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
868 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); | 870 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
871 | clear_bit(FLAG_IS_HOGGED, &desc->flags); | ||
869 | ret = true; | 872 | ret = true; |
870 | } | 873 | } |
871 | 874 | ||
@@ -1659,19 +1662,18 @@ static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | |||
1659 | unsigned int idx, | 1662 | unsigned int idx, |
1660 | enum gpio_lookup_flags *flags) | 1663 | enum gpio_lookup_flags *flags) |
1661 | { | 1664 | { |
1662 | static const char * const suffixes[] = { "gpios", "gpio" }; | ||
1663 | char prop_name[32]; /* 32 is max size of property name */ | 1665 | char prop_name[32]; /* 32 is max size of property name */ |
1664 | enum of_gpio_flags of_flags; | 1666 | enum of_gpio_flags of_flags; |
1665 | struct gpio_desc *desc; | 1667 | struct gpio_desc *desc; |
1666 | unsigned int i; | 1668 | unsigned int i; |
1667 | 1669 | ||
1668 | for (i = 0; i < ARRAY_SIZE(suffixes); i++) { | 1670 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
1669 | if (con_id) | 1671 | if (con_id) |
1670 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, | 1672 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
1671 | suffixes[i]); | 1673 | gpio_suffixes[i]); |
1672 | else | 1674 | else |
1673 | snprintf(prop_name, sizeof(prop_name), "%s", | 1675 | snprintf(prop_name, sizeof(prop_name), "%s", |
1674 | suffixes[i]); | 1676 | gpio_suffixes[i]); |
1675 | 1677 | ||
1676 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, | 1678 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
1677 | &of_flags); | 1679 | &of_flags); |
@@ -1692,7 +1694,6 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, | |||
1692 | unsigned int idx, | 1694 | unsigned int idx, |
1693 | enum gpio_lookup_flags *flags) | 1695 | enum gpio_lookup_flags *flags) |
1694 | { | 1696 | { |
1695 | static const char * const suffixes[] = { "gpios", "gpio" }; | ||
1696 | struct acpi_device *adev = ACPI_COMPANION(dev); | 1697 | struct acpi_device *adev = ACPI_COMPANION(dev); |
1697 | struct acpi_gpio_info info; | 1698 | struct acpi_gpio_info info; |
1698 | struct gpio_desc *desc; | 1699 | struct gpio_desc *desc; |
@@ -1700,13 +1701,13 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, | |||
1700 | int i; | 1701 | int i; |
1701 | 1702 | ||
1702 | /* Try first from _DSD */ | 1703 | /* Try first from _DSD */ |
1703 | for (i = 0; i < ARRAY_SIZE(suffixes); i++) { | 1704 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
1704 | if (con_id && strcmp(con_id, "gpios")) { | 1705 | if (con_id && strcmp(con_id, "gpios")) { |
1705 | snprintf(propname, sizeof(propname), "%s-%s", | 1706 | snprintf(propname, sizeof(propname), "%s-%s", |
1706 | con_id, suffixes[i]); | 1707 | con_id, gpio_suffixes[i]); |
1707 | } else { | 1708 | } else { |
1708 | snprintf(propname, sizeof(propname), "%s", | 1709 | snprintf(propname, sizeof(propname), "%s", |
1709 | suffixes[i]); | 1710 | gpio_suffixes[i]); |
1710 | } | 1711 | } |
1711 | 1712 | ||
1712 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); | 1713 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); |
@@ -1805,6 +1806,70 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, | |||
1805 | return desc; | 1806 | return desc; |
1806 | } | 1807 | } |
1807 | 1808 | ||
1809 | static int dt_gpio_count(struct device *dev, const char *con_id) | ||
1810 | { | ||
1811 | int ret; | ||
1812 | char propname[32]; | ||
1813 | unsigned int i; | ||
1814 | |||
1815 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | ||
1816 | if (con_id) | ||
1817 | snprintf(propname, sizeof(propname), "%s-%s", | ||
1818 | con_id, gpio_suffixes[i]); | ||
1819 | else | ||
1820 | snprintf(propname, sizeof(propname), "%s", | ||
1821 | gpio_suffixes[i]); | ||
1822 | |||
1823 | ret = of_gpio_named_count(dev->of_node, propname); | ||
1824 | if (ret >= 0) | ||
1825 | break; | ||
1826 | } | ||
1827 | return ret; | ||
1828 | } | ||
1829 | |||
1830 | static int platform_gpio_count(struct device *dev, const char *con_id) | ||
1831 | { | ||
1832 | struct gpiod_lookup_table *table; | ||
1833 | struct gpiod_lookup *p; | ||
1834 | unsigned int count = 0; | ||
1835 | |||
1836 | table = gpiod_find_lookup_table(dev); | ||
1837 | if (!table) | ||
1838 | return -ENOENT; | ||
1839 | |||
1840 | for (p = &table->table[0]; p->chip_label; p++) { | ||
1841 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || | ||
1842 | (!con_id && !p->con_id)) | ||
1843 | count++; | ||
1844 | } | ||
1845 | if (!count) | ||
1846 | return -ENOENT; | ||
1847 | |||
1848 | return count; | ||
1849 | } | ||
1850 | |||
1851 | /** | ||
1852 | * gpiod_count - return the number of GPIOs associated with a device / function | ||
1853 | * or -ENOENT if no GPIO has been assigned to the requested function | ||
1854 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | ||
1855 | * @con_id: function within the GPIO consumer | ||
1856 | */ | ||
1857 | int gpiod_count(struct device *dev, const char *con_id) | ||
1858 | { | ||
1859 | int count = -ENOENT; | ||
1860 | |||
1861 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | ||
1862 | count = dt_gpio_count(dev, con_id); | ||
1863 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) | ||
1864 | count = acpi_gpio_count(dev, con_id); | ||
1865 | |||
1866 | if (count < 0) | ||
1867 | count = platform_gpio_count(dev, con_id); | ||
1868 | |||
1869 | return count; | ||
1870 | } | ||
1871 | EXPORT_SYMBOL_GPL(gpiod_count); | ||
1872 | |||
1808 | /** | 1873 | /** |
1809 | * gpiod_get - obtain a GPIO for a given GPIO function | 1874 | * gpiod_get - obtain a GPIO for a given GPIO function |
1810 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | 1875 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
@@ -1840,6 +1905,47 @@ struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev, | |||
1840 | } | 1905 | } |
1841 | EXPORT_SYMBOL_GPL(__gpiod_get_optional); | 1906 | EXPORT_SYMBOL_GPL(__gpiod_get_optional); |
1842 | 1907 | ||
1908 | |||
1909 | /** | ||
1910 | * gpiod_configure_flags - helper function to configure a given GPIO | ||
1911 | * @desc: gpio whose value will be assigned | ||
1912 | * @con_id: function within the GPIO consumer | ||
1913 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | ||
1914 | * of_get_gpio_hog() | ||
1915 | * @dflags: gpiod_flags - optional GPIO initialization flags | ||
1916 | * | ||
1917 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the | ||
1918 | * requested function and/or index, or another IS_ERR() code if an error | ||
1919 | * occurred while trying to acquire the GPIO. | ||
1920 | */ | ||
1921 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, | ||
1922 | unsigned long lflags, enum gpiod_flags dflags) | ||
1923 | { | ||
1924 | int status; | ||
1925 | |||
1926 | if (lflags & GPIO_ACTIVE_LOW) | ||
1927 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | ||
1928 | if (lflags & GPIO_OPEN_DRAIN) | ||
1929 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | ||
1930 | if (lflags & GPIO_OPEN_SOURCE) | ||
1931 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | ||
1932 | |||
1933 | /* No particular flag request, return here... */ | ||
1934 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | ||
1935 | pr_debug("no flags found for %s\n", con_id); | ||
1936 | return 0; | ||
1937 | } | ||
1938 | |||
1939 | /* Process flags */ | ||
1940 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) | ||
1941 | status = gpiod_direction_output(desc, | ||
1942 | dflags & GPIOD_FLAGS_BIT_DIR_VAL); | ||
1943 | else | ||
1944 | status = gpiod_direction_input(desc); | ||
1945 | |||
1946 | return status; | ||
1947 | } | ||
1948 | |||
1843 | /** | 1949 | /** |
1844 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | 1950 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
1845 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | 1951 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
@@ -1865,13 +1971,15 @@ struct gpio_desc *__must_check __gpiod_get_index(struct device *dev, | |||
1865 | 1971 | ||
1866 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | 1972 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
1867 | 1973 | ||
1868 | /* Using device tree? */ | 1974 | if (dev) { |
1869 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { | 1975 | /* Using device tree? */ |
1870 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | 1976 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
1871 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); | 1977 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
1872 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { | 1978 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); |
1873 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | 1979 | } else if (ACPI_COMPANION(dev)) { |
1874 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); | 1980 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
1981 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); | ||
1982 | } | ||
1875 | } | 1983 | } |
1876 | 1984 | ||
1877 | /* | 1985 | /* |
@@ -1889,28 +1997,10 @@ struct gpio_desc *__must_check __gpiod_get_index(struct device *dev, | |||
1889 | } | 1997 | } |
1890 | 1998 | ||
1891 | status = gpiod_request(desc, con_id); | 1999 | status = gpiod_request(desc, con_id); |
1892 | |||
1893 | if (status < 0) | 2000 | if (status < 0) |
1894 | return ERR_PTR(status); | 2001 | return ERR_PTR(status); |
1895 | 2002 | ||
1896 | if (lookupflags & GPIO_ACTIVE_LOW) | 2003 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
1897 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | ||
1898 | if (lookupflags & GPIO_OPEN_DRAIN) | ||
1899 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | ||
1900 | if (lookupflags & GPIO_OPEN_SOURCE) | ||
1901 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | ||
1902 | |||
1903 | /* No particular flag request, return here... */ | ||
1904 | if (!(flags & GPIOD_FLAGS_BIT_DIR_SET)) | ||
1905 | return desc; | ||
1906 | |||
1907 | /* Process flags */ | ||
1908 | if (flags & GPIOD_FLAGS_BIT_DIR_OUT) | ||
1909 | status = gpiod_direction_output(desc, | ||
1910 | flags & GPIOD_FLAGS_BIT_DIR_VAL); | ||
1911 | else | ||
1912 | status = gpiod_direction_input(desc); | ||
1913 | |||
1914 | if (status < 0) { | 2004 | if (status < 0) { |
1915 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); | 2005 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
1916 | gpiod_put(desc); | 2006 | gpiod_put(desc); |
@@ -2006,6 +2096,132 @@ struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev, | |||
2006 | EXPORT_SYMBOL_GPL(__gpiod_get_index_optional); | 2096 | EXPORT_SYMBOL_GPL(__gpiod_get_index_optional); |
2007 | 2097 | ||
2008 | /** | 2098 | /** |
2099 | * gpiod_hog - Hog the specified GPIO desc given the provided flags | ||
2100 | * @desc: gpio whose value will be assigned | ||
2101 | * @name: gpio line name | ||
2102 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | ||
2103 | * of_get_gpio_hog() | ||
2104 | * @dflags: gpiod_flags - optional GPIO initialization flags | ||
2105 | */ | ||
2106 | int gpiod_hog(struct gpio_desc *desc, const char *name, | ||
2107 | unsigned long lflags, enum gpiod_flags dflags) | ||
2108 | { | ||
2109 | struct gpio_chip *chip; | ||
2110 | struct gpio_desc *local_desc; | ||
2111 | int hwnum; | ||
2112 | int status; | ||
2113 | |||
2114 | chip = gpiod_to_chip(desc); | ||
2115 | hwnum = gpio_chip_hwgpio(desc); | ||
2116 | |||
2117 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); | ||
2118 | if (IS_ERR(local_desc)) { | ||
2119 | pr_debug("requesting own GPIO %s failed\n", name); | ||
2120 | return PTR_ERR(local_desc); | ||
2121 | } | ||
2122 | |||
2123 | status = gpiod_configure_flags(desc, name, lflags, dflags); | ||
2124 | if (status < 0) { | ||
2125 | pr_debug("setup of GPIO %s failed\n", name); | ||
2126 | gpiochip_free_own_desc(desc); | ||
2127 | return status; | ||
2128 | } | ||
2129 | |||
2130 | /* Mark GPIO as hogged so it can be identified and removed later */ | ||
2131 | set_bit(FLAG_IS_HOGGED, &desc->flags); | ||
2132 | |||
2133 | pr_info("GPIO line %d (%s) hogged as %s%s\n", | ||
2134 | desc_to_gpio(desc), name, | ||
2135 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", | ||
2136 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? | ||
2137 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); | ||
2138 | |||
2139 | return 0; | ||
2140 | } | ||
2141 | |||
2142 | /** | ||
2143 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog | ||
2144 | * @chip: gpio chip to act on | ||
2145 | * | ||
2146 | * This is only used by of_gpiochip_remove to free hogged gpios | ||
2147 | */ | ||
2148 | static void gpiochip_free_hogs(struct gpio_chip *chip) | ||
2149 | { | ||
2150 | int id; | ||
2151 | |||
2152 | for (id = 0; id < chip->ngpio; id++) { | ||
2153 | if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags)) | ||
2154 | gpiochip_free_own_desc(&chip->desc[id]); | ||
2155 | } | ||
2156 | } | ||
2157 | |||
2158 | /** | ||
2159 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function | ||
2160 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | ||
2161 | * @con_id: function within the GPIO consumer | ||
2162 | * @flags: optional GPIO initialization flags | ||
2163 | * | ||
2164 | * This function acquires all the GPIOs defined under a given function. | ||
2165 | * | ||
2166 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if | ||
2167 | * no GPIO has been assigned to the requested function, or another IS_ERR() | ||
2168 | * code if an error occurred while trying to acquire the GPIOs. | ||
2169 | */ | ||
2170 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, | ||
2171 | const char *con_id, | ||
2172 | enum gpiod_flags flags) | ||
2173 | { | ||
2174 | struct gpio_desc *desc; | ||
2175 | struct gpio_descs *descs; | ||
2176 | int count; | ||
2177 | |||
2178 | count = gpiod_count(dev, con_id); | ||
2179 | if (count < 0) | ||
2180 | return ERR_PTR(count); | ||
2181 | |||
2182 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, | ||
2183 | GFP_KERNEL); | ||
2184 | if (!descs) | ||
2185 | return ERR_PTR(-ENOMEM); | ||
2186 | |||
2187 | for (descs->ndescs = 0; descs->ndescs < count; ) { | ||
2188 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); | ||
2189 | if (IS_ERR(desc)) { | ||
2190 | gpiod_put_array(descs); | ||
2191 | return ERR_CAST(desc); | ||
2192 | } | ||
2193 | descs->desc[descs->ndescs] = desc; | ||
2194 | descs->ndescs++; | ||
2195 | } | ||
2196 | return descs; | ||
2197 | } | ||
2198 | EXPORT_SYMBOL_GPL(gpiod_get_array); | ||
2199 | |||
2200 | /** | ||
2201 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO | ||
2202 | * function | ||
2203 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | ||
2204 | * @con_id: function within the GPIO consumer | ||
2205 | * @flags: optional GPIO initialization flags | ||
2206 | * | ||
2207 | * This is equivalent to gpiod_get_array(), except that when no GPIO was | ||
2208 | * assigned to the requested function it will return NULL. | ||
2209 | */ | ||
2210 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | ||
2211 | const char *con_id, | ||
2212 | enum gpiod_flags flags) | ||
2213 | { | ||
2214 | struct gpio_descs *descs; | ||
2215 | |||
2216 | descs = gpiod_get_array(dev, con_id, flags); | ||
2217 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) | ||
2218 | return NULL; | ||
2219 | |||
2220 | return descs; | ||
2221 | } | ||
2222 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); | ||
2223 | |||
2224 | /** | ||
2009 | * gpiod_put - dispose of a GPIO descriptor | 2225 | * gpiod_put - dispose of a GPIO descriptor |
2010 | * @desc: GPIO descriptor to dispose of | 2226 | * @desc: GPIO descriptor to dispose of |
2011 | * | 2227 | * |
@@ -2017,6 +2233,21 @@ void gpiod_put(struct gpio_desc *desc) | |||
2017 | } | 2233 | } |
2018 | EXPORT_SYMBOL_GPL(gpiod_put); | 2234 | EXPORT_SYMBOL_GPL(gpiod_put); |
2019 | 2235 | ||
2236 | /** | ||
2237 | * gpiod_put_array - dispose of multiple GPIO descriptors | ||
2238 | * @descs: struct gpio_descs containing an array of descriptors | ||
2239 | */ | ||
2240 | void gpiod_put_array(struct gpio_descs *descs) | ||
2241 | { | ||
2242 | unsigned int i; | ||
2243 | |||
2244 | for (i = 0; i < descs->ndescs; i++) | ||
2245 | gpiod_put(descs->desc[i]); | ||
2246 | |||
2247 | kfree(descs); | ||
2248 | } | ||
2249 | EXPORT_SYMBOL_GPL(gpiod_put_array); | ||
2250 | |||
2020 | #ifdef CONFIG_DEBUG_FS | 2251 | #ifdef CONFIG_DEBUG_FS |
2021 | 2252 | ||
2022 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | 2253 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |