1664e3e5aSMika Westerberg /* 2664e3e5aSMika Westerberg * Internal GPIO functions. 3664e3e5aSMika Westerberg * 4664e3e5aSMika Westerberg * Copyright (C) 2013, Intel Corporation 5664e3e5aSMika Westerberg * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 6664e3e5aSMika Westerberg * 7664e3e5aSMika Westerberg * This program is free software; you can redistribute it and/or modify 8664e3e5aSMika Westerberg * it under the terms of the GNU General Public License version 2 as 9664e3e5aSMika Westerberg * published by the Free Software Foundation. 10664e3e5aSMika Westerberg */ 11664e3e5aSMika Westerberg 12664e3e5aSMika Westerberg #ifndef GPIOLIB_H 13664e3e5aSMika Westerberg #define GPIOLIB_H 14664e3e5aSMika Westerberg 15ff2b1359SLinus Walleij #include <linux/gpio/driver.h> 165ccff852SMika Westerberg #include <linux/err.h> 175ccff852SMika Westerberg #include <linux/device.h> 18ff2b1359SLinus Walleij #include <linux/module.h> 19ff2b1359SLinus Walleij #include <linux/cdev.h> 205ccff852SMika Westerberg 21f01d9075SAlexandre Courbot enum of_gpio_flags; 2229ab875bSLinus Walleij enum gpiod_flags; 23ce793486SRafael J. Wysocki struct acpi_device; 24ce793486SRafael J. Wysocki 255ccff852SMika Westerberg /** 26ff2b1359SLinus Walleij * struct gpio_device - internal state container for GPIO devices 27ff2b1359SLinus Walleij * @id: numerical ID number for the GPIO chip 28ff2b1359SLinus Walleij * @dev: the GPIO device struct 293c702e99SLinus Walleij * @chrdev: character device for the GPIO device 30afbc4f31SLinus Walleij * @mockdev: class device used by the deprecated sysfs interface (may be 31afbc4f31SLinus Walleij * NULL) 32ff2b1359SLinus Walleij * @owner: helps prevent removal of modules exporting active GPIOs 33ff2b1359SLinus Walleij * @chip: pointer to the corresponding gpiochip, holding static 34ff2b1359SLinus Walleij * data for this device 35ff2b1359SLinus Walleij * @list: links gpio_device:s together for traversal 36ff2b1359SLinus Walleij * 37ff2b1359SLinus Walleij * This state container holds most of the runtime variable data 38ff2b1359SLinus Walleij * for a GPIO device and can hold references and live on after the 39ff2b1359SLinus Walleij * GPIO chip has been removed, if it is still being used from 40ff2b1359SLinus Walleij * userspace. 41ff2b1359SLinus Walleij */ 42ff2b1359SLinus Walleij struct gpio_device { 43ff2b1359SLinus Walleij int id; 44ff2b1359SLinus Walleij struct device dev; 453c702e99SLinus Walleij struct cdev chrdev; 46afbc4f31SLinus Walleij struct device *mockdev; 47ff2b1359SLinus Walleij struct module *owner; 48ff2b1359SLinus Walleij struct gpio_chip *chip; 49ff2b1359SLinus Walleij struct list_head list; 50ff2b1359SLinus Walleij }; 51ff2b1359SLinus Walleij 52ff2b1359SLinus Walleij /** 535ccff852SMika Westerberg * struct acpi_gpio_info - ACPI GPIO specific information 545ccff852SMika Westerberg * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo 555ccff852SMika Westerberg * @active_low: in case of @gpioint, the pin is active low 565ccff852SMika Westerberg */ 575ccff852SMika Westerberg struct acpi_gpio_info { 585ccff852SMika Westerberg bool gpioint; 5952044723SChristophe RICARD int polarity; 6052044723SChristophe RICARD int triggering; 615ccff852SMika Westerberg }; 625ccff852SMika Westerberg 637f2e553aSRojhalat Ibrahim /* gpio suffixes used for ACPI and device tree lookup */ 647f2e553aSRojhalat Ibrahim static const char * const gpio_suffixes[] = { "gpios", "gpio" }; 657f2e553aSRojhalat Ibrahim 66664e3e5aSMika Westerberg #ifdef CONFIG_ACPI 67664e3e5aSMika Westerberg void acpi_gpiochip_add(struct gpio_chip *chip); 68664e3e5aSMika Westerberg void acpi_gpiochip_remove(struct gpio_chip *chip); 695ccff852SMika Westerberg 70afa82fabSMika Westerberg void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 71afa82fabSMika Westerberg void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 72afa82fabSMika Westerberg 730d9a693cSMika Westerberg struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, 740d9a693cSMika Westerberg const char *propname, int index, 755ccff852SMika Westerberg struct acpi_gpio_info *info); 76504a3374SRafael J. Wysocki struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, 77504a3374SRafael J. Wysocki const char *propname, int index, 78504a3374SRafael J. Wysocki struct acpi_gpio_info *info); 7966858527SRojhalat Ibrahim 8066858527SRojhalat Ibrahim int acpi_gpio_count(struct device *dev, const char *con_id); 8110cf4899SDmitry Torokhov 8210cf4899SDmitry Torokhov bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id); 83664e3e5aSMika Westerberg #else 84664e3e5aSMika Westerberg static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } 85664e3e5aSMika Westerberg static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } 865ccff852SMika Westerberg 87afa82fabSMika Westerberg static inline void 88afa82fabSMika Westerberg acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } 89afa82fabSMika Westerberg 90afa82fabSMika Westerberg static inline void 91afa82fabSMika Westerberg acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } 92afa82fabSMika Westerberg 935ccff852SMika Westerberg static inline struct gpio_desc * 940d9a693cSMika Westerberg acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname, 950d9a693cSMika Westerberg int index, struct acpi_gpio_info *info) 965ccff852SMika Westerberg { 975ccff852SMika Westerberg return ERR_PTR(-ENOSYS); 985ccff852SMika Westerberg } 99504a3374SRafael J. Wysocki static inline struct gpio_desc * 100504a3374SRafael J. Wysocki acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname, 101504a3374SRafael J. Wysocki int index, struct acpi_gpio_info *info) 102504a3374SRafael J. Wysocki { 103504a3374SRafael J. Wysocki return ERR_PTR(-ENXIO); 104504a3374SRafael J. Wysocki } 10566858527SRojhalat Ibrahim static inline int acpi_gpio_count(struct device *dev, const char *con_id) 10666858527SRojhalat Ibrahim { 10766858527SRojhalat Ibrahim return -ENODEV; 10866858527SRojhalat Ibrahim } 10910cf4899SDmitry Torokhov 11010cf4899SDmitry Torokhov static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev, 11110cf4899SDmitry Torokhov const char *con_id) 11210cf4899SDmitry Torokhov { 11310cf4899SDmitry Torokhov return false; 11410cf4899SDmitry Torokhov } 115664e3e5aSMika Westerberg #endif 116664e3e5aSMika Westerberg 117f01d9075SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 118f01d9075SAlexandre Courbot const char *list_name, int index, enum of_gpio_flags *flags); 119f01d9075SAlexandre Courbot 1201bd6b601SAlexandre Courbot struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); 1211bd6b601SAlexandre Courbot 1220eb4c6c2SAlexandre Courbot extern struct spinlock gpio_lock; 123ff2b1359SLinus Walleij extern struct list_head gpio_devices; 1240eb4c6c2SAlexandre Courbot 1250eb4c6c2SAlexandre Courbot struct gpio_desc { 1260eb4c6c2SAlexandre Courbot struct gpio_chip *chip; 1270eb4c6c2SAlexandre Courbot unsigned long flags; 1280eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */ 1290eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED 0 1300eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT 1 1310eb4c6c2SAlexandre Courbot #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 1320eb4c6c2SAlexandre Courbot #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 1330eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW 6 /* value has active low */ 1340eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 1350eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 1360eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 137f625d460SBenoit Parrot #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 1380eb4c6c2SAlexandre Courbot 139c0017ed7SMarkus Pargmann /* Connection label */ 1400eb4c6c2SAlexandre Courbot const char *label; 141c0017ed7SMarkus Pargmann /* Name of the GPIO */ 142c0017ed7SMarkus Pargmann const char *name; 1430eb4c6c2SAlexandre Courbot }; 1440eb4c6c2SAlexandre Courbot 1450eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label); 1460eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc); 147f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name, 148f625d460SBenoit Parrot unsigned long lflags, enum gpiod_flags dflags); 1490eb4c6c2SAlexandre Courbot 1500eb4c6c2SAlexandre Courbot /* 1510eb4c6c2SAlexandre Courbot * Return the GPIO number of the passed descriptor relative to its chip 1520eb4c6c2SAlexandre Courbot */ 1530eb4c6c2SAlexandre Courbot static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc) 1540eb4c6c2SAlexandre Courbot { 1550eb4c6c2SAlexandre Courbot return desc - &desc->chip->desc[0]; 1560eb4c6c2SAlexandre Courbot } 1570eb4c6c2SAlexandre Courbot 1580eb4c6c2SAlexandre Courbot /* With descriptor prefix */ 1590eb4c6c2SAlexandre Courbot 1600eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...) \ 1610eb4c6c2SAlexandre Courbot pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1620eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1630eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...) \ 1640eb4c6c2SAlexandre Courbot pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1650eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1660eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...) \ 1670eb4c6c2SAlexandre Courbot pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1680eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1690eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...) \ 1700eb4c6c2SAlexandre Courbot pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1710eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1720eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...) \ 1730eb4c6c2SAlexandre Courbot pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1740eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1750eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...) \ 1760eb4c6c2SAlexandre Courbot pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1770eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1780eb4c6c2SAlexandre Courbot 1790eb4c6c2SAlexandre Courbot /* With chip prefix */ 1800eb4c6c2SAlexandre Courbot 1810eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...) \ 18234ffd85dSLinus Walleij dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1830eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...) \ 18434ffd85dSLinus Walleij dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1850eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...) \ 18634ffd85dSLinus Walleij dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1870eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...) \ 18834ffd85dSLinus Walleij dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1890eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...) \ 19034ffd85dSLinus Walleij dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1910eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...) \ 19234ffd85dSLinus Walleij dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 1930eb4c6c2SAlexandre Courbot 1940eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS 1950eb4c6c2SAlexandre Courbot 196afbc4f31SLinus Walleij int gpiochip_sysfs_register(struct gpio_device *gdev); 197afbc4f31SLinus Walleij void gpiochip_sysfs_unregister(struct gpio_device *gdev); 1980eb4c6c2SAlexandre Courbot 1990eb4c6c2SAlexandre Courbot #else 2000eb4c6c2SAlexandre Courbot 201afbc4f31SLinus Walleij static inline int gpiochip_sysfs_register(struct gpio_device *gdev) 2020eb4c6c2SAlexandre Courbot { 2030eb4c6c2SAlexandre Courbot return 0; 2040eb4c6c2SAlexandre Courbot } 2050eb4c6c2SAlexandre Courbot 206afbc4f31SLinus Walleij static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) 2070eb4c6c2SAlexandre Courbot { 2080eb4c6c2SAlexandre Courbot } 2090eb4c6c2SAlexandre Courbot 2100eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */ 2110eb4c6c2SAlexandre Courbot 212664e3e5aSMika Westerberg #endif /* GPIOLIB_H */ 213