Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * OF helpers for the GPIO API
0004  *
0005  * Copyright (c) 2007-2008  MontaVista Software, Inc.
0006  *
0007  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
0008  */
0009 
0010 #ifndef __LINUX_OF_GPIO_H
0011 #define __LINUX_OF_GPIO_H
0012 
0013 #include <linux/compiler.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/gpio.h>     /* FIXME: Shouldn't be here */
0016 #include <linux/of.h>
0017 
0018 struct device_node;
0019 
0020 /*
0021  * This is Linux-specific flags. By default controllers' and Linux' mapping
0022  * match, but GPIO controllers are free to translate their own flags to
0023  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
0024  */
0025 enum of_gpio_flags {
0026     OF_GPIO_ACTIVE_LOW = 0x1,
0027     OF_GPIO_SINGLE_ENDED = 0x2,
0028     OF_GPIO_OPEN_DRAIN = 0x4,
0029     OF_GPIO_TRANSITORY = 0x8,
0030     OF_GPIO_PULL_UP = 0x10,
0031     OF_GPIO_PULL_DOWN = 0x20,
0032     OF_GPIO_PULL_DISABLE = 0x40,
0033 };
0034 
0035 #ifdef CONFIG_OF_GPIO
0036 
0037 #include <linux/kernel.h>
0038 
0039 /*
0040  * OF GPIO chip for memory mapped banks
0041  */
0042 struct of_mm_gpio_chip {
0043     struct gpio_chip gc;
0044     void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
0045     void __iomem *regs;
0046 };
0047 
0048 static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
0049 {
0050     return container_of(gc, struct of_mm_gpio_chip, gc);
0051 }
0052 
0053 extern int of_get_named_gpio_flags(const struct device_node *np,
0054         const char *list_name, int index, enum of_gpio_flags *flags);
0055 
0056 extern int of_mm_gpiochip_add_data(struct device_node *np,
0057                    struct of_mm_gpio_chip *mm_gc,
0058                    void *data);
0059 static inline int of_mm_gpiochip_add(struct device_node *np,
0060                      struct of_mm_gpio_chip *mm_gc)
0061 {
0062     return of_mm_gpiochip_add_data(np, mm_gc, NULL);
0063 }
0064 extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
0065 
0066 #else /* CONFIG_OF_GPIO */
0067 
0068 #include <linux/errno.h>
0069 
0070 /* Drivers may not strictly depend on the GPIO support, so let them link. */
0071 static inline int of_get_named_gpio_flags(const struct device_node *np,
0072         const char *list_name, int index, enum of_gpio_flags *flags)
0073 {
0074     if (flags)
0075         *flags = 0;
0076 
0077     return -ENOSYS;
0078 }
0079 
0080 #endif /* CONFIG_OF_GPIO */
0081 
0082 /**
0083  * of_gpio_named_count() - Count GPIOs for a device
0084  * @np:     device node to count GPIOs for
0085  * @propname:   property name containing gpio specifier(s)
0086  *
0087  * The function returns the count of GPIOs specified for a node.
0088  * Note that the empty GPIO specifiers count too. Returns either
0089  *   Number of gpios defined in property,
0090  *   -EINVAL for an incorrectly formed gpios property, or
0091  *   -ENOENT for a missing gpios property
0092  *
0093  * Example:
0094  * gpios = <0
0095  *          &gpio1 1 2
0096  *          0
0097  *          &gpio2 3 4>;
0098  *
0099  * The above example defines four GPIOs, two of which are not specified.
0100  * This function will return '4'
0101  */
0102 static inline int of_gpio_named_count(const struct device_node *np,
0103                       const char *propname)
0104 {
0105     return of_count_phandle_with_args(np, propname, "#gpio-cells");
0106 }
0107 
0108 /**
0109  * of_gpio_count() - Count GPIOs for a device
0110  * @np:     device node to count GPIOs for
0111  *
0112  * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
0113  */
0114 static inline int of_gpio_count(const struct device_node *np)
0115 {
0116     return of_gpio_named_count(np, "gpios");
0117 }
0118 
0119 static inline int of_get_gpio_flags(const struct device_node *np, int index,
0120               enum of_gpio_flags *flags)
0121 {
0122     return of_get_named_gpio_flags(np, "gpios", index, flags);
0123 }
0124 
0125 /**
0126  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
0127  * @np:     device node to get GPIO from
0128  * @propname:   Name of property containing gpio specifier(s)
0129  * @index:  index of the GPIO
0130  *
0131  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
0132  * value on the error condition.
0133  */
0134 static inline int of_get_named_gpio(const struct device_node *np,
0135                                    const char *propname, int index)
0136 {
0137     return of_get_named_gpio_flags(np, propname, index, NULL);
0138 }
0139 
0140 /**
0141  * of_get_gpio() - Get a GPIO number to use with GPIO API
0142  * @np:     device node to get GPIO from
0143  * @index:  index of the GPIO
0144  *
0145  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
0146  * value on the error condition.
0147  */
0148 static inline int of_get_gpio(const struct device_node *np, int index)
0149 {
0150     return of_get_gpio_flags(np, index, NULL);
0151 }
0152 
0153 #endif /* __LINUX_OF_GPIO_H */