1dae5f0afSLinus Walleij /* SPDX-License-Identifier: GPL-2.0 */ 2664e3e5aSMika Westerberg /* 3664e3e5aSMika Westerberg * Internal GPIO functions. 4664e3e5aSMika Westerberg * 5664e3e5aSMika Westerberg * Copyright (C) 2013, Intel Corporation 6664e3e5aSMika Westerberg * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 7664e3e5aSMika Westerberg */ 8664e3e5aSMika Westerberg 9664e3e5aSMika Westerberg #ifndef GPIOLIB_H 10664e3e5aSMika Westerberg #define GPIOLIB_H 11664e3e5aSMika Westerberg 12ff2b1359SLinus Walleij #include <linux/gpio/driver.h> 1363f2dc0aSLinus Walleij #include <linux/gpio/consumer.h> /* for enum gpiod_flags */ 145ccff852SMika Westerberg #include <linux/err.h> 155ccff852SMika Westerberg #include <linux/device.h> 16ff2b1359SLinus Walleij #include <linux/module.h> 17ff2b1359SLinus Walleij #include <linux/cdev.h> 185ccff852SMika Westerberg 19ddd8891eSGeert Uytterhoeven #define GPIOCHIP_NAME "gpiochip" 20ddd8891eSGeert Uytterhoeven 215ccff852SMika Westerberg /** 22ff2b1359SLinus Walleij * struct gpio_device - internal state container for GPIO devices 23ff2b1359SLinus Walleij * @id: numerical ID number for the GPIO chip 24ff2b1359SLinus Walleij * @dev: the GPIO device struct 253c702e99SLinus Walleij * @chrdev: character device for the GPIO device 26afbc4f31SLinus Walleij * @mockdev: class device used by the deprecated sysfs interface (may be 27afbc4f31SLinus Walleij * NULL) 28ff2b1359SLinus Walleij * @owner: helps prevent removal of modules exporting active GPIOs 29ff2b1359SLinus Walleij * @chip: pointer to the corresponding gpiochip, holding static 30ff2b1359SLinus Walleij * data for this device 311c3cdb18SLinus Walleij * @descs: array of ngpio descriptors. 32fdeb8e15SLinus Walleij * @ngpio: the number of GPIO lines on this GPIO device, equal to the size 33fdeb8e15SLinus Walleij * of the @descs array. 34fdeb8e15SLinus Walleij * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned 35fdeb8e15SLinus Walleij * at device creation time. 36df4878e9SLinus Walleij * @label: a descriptive name for the GPIO device, such as the part number 37df4878e9SLinus Walleij * or name of the IP component in a System on Chip. 3843c54ecaSLinus Walleij * @data: per-instance data assigned by the driver 39ff2b1359SLinus Walleij * @list: links gpio_device:s together for traversal 40ff2b1359SLinus Walleij * 41ff2b1359SLinus Walleij * This state container holds most of the runtime variable data 42ff2b1359SLinus Walleij * for a GPIO device and can hold references and live on after the 43ff2b1359SLinus Walleij * GPIO chip has been removed, if it is still being used from 44ff2b1359SLinus Walleij * userspace. 45ff2b1359SLinus Walleij */ 46ff2b1359SLinus Walleij struct gpio_device { 47ff2b1359SLinus Walleij int id; 48ff2b1359SLinus Walleij struct device dev; 493c702e99SLinus Walleij struct cdev chrdev; 50afbc4f31SLinus Walleij struct device *mockdev; 51ff2b1359SLinus Walleij struct module *owner; 52ff2b1359SLinus Walleij struct gpio_chip *chip; 531c3cdb18SLinus Walleij struct gpio_desc *descs; 54fdeb8e15SLinus Walleij int base; 55fdeb8e15SLinus Walleij u16 ngpio; 563b469b0aSBartosz Golaszewski const char *label; 5743c54ecaSLinus Walleij void *data; 58ff2b1359SLinus Walleij struct list_head list; 596accc376SKent Gibson struct blocking_notifier_head notifier; 6020ec3e39SLinus Walleij 6120ec3e39SLinus Walleij #ifdef CONFIG_PINCTRL 6220ec3e39SLinus Walleij /* 6320ec3e39SLinus Walleij * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally 6420ec3e39SLinus Walleij * describe the actual pin range which they serve in an SoC. This 6520ec3e39SLinus Walleij * information would be used by pinctrl subsystem to configure 6620ec3e39SLinus Walleij * corresponding pins for gpio usage. 6720ec3e39SLinus Walleij */ 6820ec3e39SLinus Walleij struct list_head pin_ranges; 6920ec3e39SLinus Walleij #endif 70ff2b1359SLinus Walleij }; 71ff2b1359SLinus Walleij 727f2e553aSRojhalat Ibrahim /* gpio suffixes used for ACPI and device tree lookup */ 73b23ec599SAndy Shevchenko static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" }; 747f2e553aSRojhalat Ibrahim 75bf9346f5SJanusz Krzysztofik struct gpio_array { 76bf9346f5SJanusz Krzysztofik struct gpio_desc **desc; 77bf9346f5SJanusz Krzysztofik unsigned int size; 78bf9346f5SJanusz Krzysztofik struct gpio_chip *chip; 79bf9346f5SJanusz Krzysztofik unsigned long *get_mask; 80bf9346f5SJanusz Krzysztofik unsigned long *set_mask; 81bf9346f5SJanusz Krzysztofik unsigned long invert_mask[]; 82bf9346f5SJanusz Krzysztofik }; 83bf9346f5SJanusz Krzysztofik 84a5e93436SGeert Uytterhoeven struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum); 85eec1d566SLukas Wunner int gpiod_get_array_value_complex(bool raw, bool can_sleep, 86eec1d566SLukas Wunner unsigned int array_size, 87eec1d566SLukas Wunner struct gpio_desc **desc_array, 8877588c14SJanusz Krzysztofik struct gpio_array *array_info, 89b9762bebSJanusz Krzysztofik unsigned long *value_bitmap); 903027743fSLaura Abbott int gpiod_set_array_value_complex(bool raw, bool can_sleep, 9144c7288fSLinus Walleij unsigned int array_size, 9244c7288fSLinus Walleij struct gpio_desc **desc_array, 9377588c14SJanusz Krzysztofik struct gpio_array *array_info, 94b9762bebSJanusz Krzysztofik unsigned long *value_bitmap); 951bd6b601SAlexandre Courbot 96f0b40863SSebastian Andrzej Siewior extern spinlock_t gpio_lock; 97ff2b1359SLinus Walleij extern struct list_head gpio_devices; 980eb4c6c2SAlexandre Courbot 990eb4c6c2SAlexandre Courbot struct gpio_desc { 100fdeb8e15SLinus Walleij struct gpio_device *gdev; 1010eb4c6c2SAlexandre Courbot unsigned long flags; 1020eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */ 1030eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED 0 1040eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT 1 1050eb4c6c2SAlexandre Courbot #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 1060eb4c6c2SAlexandre Courbot #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 1070eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW 6 /* value has active low */ 1080eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 1090eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 1100eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 1114e9439ddSHans Verkuil #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 112f625d460SBenoit Parrot #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 113e10f72bfSAndrew Jeffery #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 114d449991cSThomas Petazzoni #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 115d449991cSThomas Petazzoni #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 1162148ad77SKent Gibson #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ 11773e03419SKent Gibson #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ 11873e03419SKent Gibson #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ 1190eb4c6c2SAlexandre Courbot 120c0017ed7SMarkus Pargmann /* Connection label */ 1210eb4c6c2SAlexandre Courbot const char *label; 122c0017ed7SMarkus Pargmann /* Name of the GPIO */ 123c0017ed7SMarkus Pargmann const char *name; 12463636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 12563636d95SGeert Uytterhoeven struct device_node *hog; 12663636d95SGeert Uytterhoeven #endif 12765cff704SKent Gibson #ifdef CONFIG_GPIO_CDEV 12865cff704SKent Gibson /* debounce period in microseconds */ 12965cff704SKent Gibson unsigned int debounce_period_us; 13065cff704SKent Gibson #endif 1310eb4c6c2SAlexandre Courbot }; 1320eb4c6c2SAlexandre Courbot 133*7b58696dSAndy Shevchenko #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) 134*7b58696dSAndy Shevchenko 1350eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label); 1360eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc); 137c29fd9ebSAndy Shevchenko int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 138c29fd9ebSAndy Shevchenko unsigned long lflags, enum gpiod_flags dflags); 139f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name, 140f625d460SBenoit Parrot unsigned long lflags, enum gpiod_flags dflags); 1410eb4c6c2SAlexandre Courbot 1420eb4c6c2SAlexandre Courbot /* 1430eb4c6c2SAlexandre Courbot * Return the GPIO number of the passed descriptor relative to its chip 1440eb4c6c2SAlexandre Courbot */ 1454a5c886eSMasahiro Yamada static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) 1460eb4c6c2SAlexandre Courbot { 147fdeb8e15SLinus Walleij return desc - &desc->gdev->descs[0]; 1480eb4c6c2SAlexandre Courbot } 1490eb4c6c2SAlexandre Courbot 1500eb4c6c2SAlexandre Courbot /* With descriptor prefix */ 1510eb4c6c2SAlexandre Courbot 1520eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...) \ 1530eb4c6c2SAlexandre Courbot pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1540eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1550eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...) \ 1560eb4c6c2SAlexandre Courbot pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1570eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1580eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...) \ 1590eb4c6c2SAlexandre Courbot pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1600eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1610eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...) \ 1620eb4c6c2SAlexandre Courbot pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1630eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1640eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...) \ 1650eb4c6c2SAlexandre Courbot pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1660eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1670eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...) \ 1680eb4c6c2SAlexandre Courbot pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1690eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1700eb4c6c2SAlexandre Courbot 1710eb4c6c2SAlexandre Courbot /* With chip prefix */ 1720eb4c6c2SAlexandre Courbot 173a5e93436SGeert Uytterhoeven #define chip_emerg(gc, fmt, ...) \ 174a5e93436SGeert Uytterhoeven dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 175a5e93436SGeert Uytterhoeven #define chip_crit(gc, fmt, ...) \ 176a5e93436SGeert Uytterhoeven dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 177a5e93436SGeert Uytterhoeven #define chip_err(gc, fmt, ...) \ 178a5e93436SGeert Uytterhoeven dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 179a5e93436SGeert Uytterhoeven #define chip_warn(gc, fmt, ...) \ 180a5e93436SGeert Uytterhoeven dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 181a5e93436SGeert Uytterhoeven #define chip_info(gc, fmt, ...) \ 182a5e93436SGeert Uytterhoeven dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 183a5e93436SGeert Uytterhoeven #define chip_dbg(gc, fmt, ...) \ 184a5e93436SGeert Uytterhoeven dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 1850eb4c6c2SAlexandre Courbot 186664e3e5aSMika Westerberg #endif /* GPIOLIB_H */ 187