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; 5951c1064eSBartosz Golaszewski struct atomic_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 8406863620SBartosz Golaszewski struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, 8506863620SBartosz Golaszewski unsigned int hwnum); 86eec1d566SLukas Wunner int gpiod_get_array_value_complex(bool raw, bool can_sleep, 87eec1d566SLukas Wunner unsigned int array_size, 88eec1d566SLukas Wunner struct gpio_desc **desc_array, 8977588c14SJanusz Krzysztofik struct gpio_array *array_info, 90b9762bebSJanusz Krzysztofik unsigned long *value_bitmap); 913027743fSLaura Abbott int gpiod_set_array_value_complex(bool raw, bool can_sleep, 9244c7288fSLinus Walleij unsigned int array_size, 9344c7288fSLinus Walleij struct gpio_desc **desc_array, 9477588c14SJanusz Krzysztofik struct gpio_array *array_info, 95b9762bebSJanusz Krzysztofik unsigned long *value_bitmap); 961bd6b601SAlexandre Courbot 97f0b40863SSebastian Andrzej Siewior extern spinlock_t gpio_lock; 98ff2b1359SLinus Walleij extern struct list_head gpio_devices; 990eb4c6c2SAlexandre Courbot 1000eb4c6c2SAlexandre Courbot struct gpio_desc { 101fdeb8e15SLinus Walleij struct gpio_device *gdev; 1020eb4c6c2SAlexandre Courbot unsigned long flags; 1030eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */ 1040eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED 0 1050eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT 1 1060eb4c6c2SAlexandre Courbot #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 1070eb4c6c2SAlexandre Courbot #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 1080eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW 6 /* value has active low */ 1090eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 1100eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 1110eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 1124e9439ddSHans Verkuil #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 113f625d460SBenoit Parrot #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 114e10f72bfSAndrew Jeffery #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 115d449991cSThomas Petazzoni #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 116d449991cSThomas Petazzoni #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 1172148ad77SKent Gibson #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ 1180eb4c6c2SAlexandre Courbot 119c0017ed7SMarkus Pargmann /* Connection label */ 1200eb4c6c2SAlexandre Courbot const char *label; 121c0017ed7SMarkus Pargmann /* Name of the GPIO */ 122c0017ed7SMarkus Pargmann const char *name; 12363636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 12463636d95SGeert Uytterhoeven struct device_node *hog; 12563636d95SGeert Uytterhoeven #endif 1260eb4c6c2SAlexandre Courbot }; 1270eb4c6c2SAlexandre Courbot 1280eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label); 1290eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc); 130c29fd9ebSAndy Shevchenko int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 131c29fd9ebSAndy Shevchenko unsigned long lflags, enum gpiod_flags dflags); 132f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name, 133f625d460SBenoit Parrot unsigned long lflags, enum gpiod_flags dflags); 1340eb4c6c2SAlexandre Courbot 1350eb4c6c2SAlexandre Courbot /* 1360eb4c6c2SAlexandre Courbot * Return the GPIO number of the passed descriptor relative to its chip 1370eb4c6c2SAlexandre Courbot */ 1384a5c886eSMasahiro Yamada static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) 1390eb4c6c2SAlexandre Courbot { 140fdeb8e15SLinus Walleij return desc - &desc->gdev->descs[0]; 1410eb4c6c2SAlexandre Courbot } 1420eb4c6c2SAlexandre Courbot 1430eb4c6c2SAlexandre Courbot /* With descriptor prefix */ 1440eb4c6c2SAlexandre Courbot 1450eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...) \ 1460eb4c6c2SAlexandre Courbot pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1470eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1480eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...) \ 1490eb4c6c2SAlexandre Courbot pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1500eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1510eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...) \ 1520eb4c6c2SAlexandre Courbot pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1530eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1540eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...) \ 1550eb4c6c2SAlexandre Courbot pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1560eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1570eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...) \ 1580eb4c6c2SAlexandre Courbot pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1590eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1600eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...) \ 1610eb4c6c2SAlexandre Courbot pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1620eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1630eb4c6c2SAlexandre Courbot 1640eb4c6c2SAlexandre Courbot /* With chip prefix */ 1650eb4c6c2SAlexandre Courbot 1660eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...) \ 16734ffd85dSLinus Walleij dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1680eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...) \ 16934ffd85dSLinus Walleij dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1700eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...) \ 17134ffd85dSLinus Walleij dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1720eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...) \ 17334ffd85dSLinus Walleij dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1740eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...) \ 17534ffd85dSLinus Walleij dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1760eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...) \ 17734ffd85dSLinus Walleij dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1780eb4c6c2SAlexandre Courbot 1790eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS 1800eb4c6c2SAlexandre Courbot 181afbc4f31SLinus Walleij int gpiochip_sysfs_register(struct gpio_device *gdev); 182afbc4f31SLinus Walleij void gpiochip_sysfs_unregister(struct gpio_device *gdev); 1830eb4c6c2SAlexandre Courbot 1840eb4c6c2SAlexandre Courbot #else 1850eb4c6c2SAlexandre Courbot 186afbc4f31SLinus Walleij static inline int gpiochip_sysfs_register(struct gpio_device *gdev) 1870eb4c6c2SAlexandre Courbot { 1880eb4c6c2SAlexandre Courbot return 0; 1890eb4c6c2SAlexandre Courbot } 1900eb4c6c2SAlexandre Courbot 191afbc4f31SLinus Walleij static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) 1920eb4c6c2SAlexandre Courbot { 1930eb4c6c2SAlexandre Courbot } 1940eb4c6c2SAlexandre Courbot 1950eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */ 1960eb4c6c2SAlexandre Courbot 197664e3e5aSMika Westerberg #endif /* GPIOLIB_H */ 198