xref: /openbmc/linux/drivers/gpio/gpiolib.h (revision 3c702e99)
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
30ff2b1359SLinus Walleij  * @owner: helps prevent removal of modules exporting active GPIOs
31ff2b1359SLinus Walleij  * @chip: pointer to the corresponding gpiochip, holding static
32ff2b1359SLinus Walleij  * data for this device
33ff2b1359SLinus Walleij  * @list: links gpio_device:s together for traversal
34ff2b1359SLinus Walleij  *
35ff2b1359SLinus Walleij  * This state container holds most of the runtime variable data
36ff2b1359SLinus Walleij  * for a GPIO device and can hold references and live on after the
37ff2b1359SLinus Walleij  * GPIO chip has been removed, if it is still being used from
38ff2b1359SLinus Walleij  * userspace.
39ff2b1359SLinus Walleij  */
40ff2b1359SLinus Walleij struct gpio_device {
41ff2b1359SLinus Walleij 	int			id;
42ff2b1359SLinus Walleij 	struct device		dev;
433c702e99SLinus Walleij 	struct cdev		chrdev;
44ff2b1359SLinus Walleij 	struct module		*owner;
45ff2b1359SLinus Walleij 	struct gpio_chip	*chip;
46ff2b1359SLinus Walleij 	struct list_head        list;
47ff2b1359SLinus Walleij };
48ff2b1359SLinus Walleij 
49ff2b1359SLinus Walleij /**
505ccff852SMika Westerberg  * struct acpi_gpio_info - ACPI GPIO specific information
515ccff852SMika Westerberg  * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
525ccff852SMika Westerberg  * @active_low: in case of @gpioint, the pin is active low
535ccff852SMika Westerberg  */
545ccff852SMika Westerberg struct acpi_gpio_info {
555ccff852SMika Westerberg 	bool gpioint;
5652044723SChristophe RICARD 	int polarity;
5752044723SChristophe RICARD 	int triggering;
585ccff852SMika Westerberg };
595ccff852SMika Westerberg 
607f2e553aSRojhalat Ibrahim /* gpio suffixes used for ACPI and device tree lookup */
617f2e553aSRojhalat Ibrahim static const char * const gpio_suffixes[] = { "gpios", "gpio" };
627f2e553aSRojhalat Ibrahim 
63664e3e5aSMika Westerberg #ifdef CONFIG_ACPI
64664e3e5aSMika Westerberg void acpi_gpiochip_add(struct gpio_chip *chip);
65664e3e5aSMika Westerberg void acpi_gpiochip_remove(struct gpio_chip *chip);
665ccff852SMika Westerberg 
67afa82fabSMika Westerberg void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
68afa82fabSMika Westerberg void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
69afa82fabSMika Westerberg 
700d9a693cSMika Westerberg struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
710d9a693cSMika Westerberg 					  const char *propname, int index,
725ccff852SMika Westerberg 					  struct acpi_gpio_info *info);
73504a3374SRafael J. Wysocki struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
74504a3374SRafael J. Wysocki 				      const char *propname, int index,
75504a3374SRafael J. Wysocki 				      struct acpi_gpio_info *info);
7666858527SRojhalat Ibrahim 
7766858527SRojhalat Ibrahim int acpi_gpio_count(struct device *dev, const char *con_id);
7810cf4899SDmitry Torokhov 
7910cf4899SDmitry Torokhov bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
80664e3e5aSMika Westerberg #else
81664e3e5aSMika Westerberg static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
82664e3e5aSMika Westerberg static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
835ccff852SMika Westerberg 
84afa82fabSMika Westerberg static inline void
85afa82fabSMika Westerberg acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
86afa82fabSMika Westerberg 
87afa82fabSMika Westerberg static inline void
88afa82fabSMika Westerberg acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
89afa82fabSMika Westerberg 
905ccff852SMika Westerberg static inline struct gpio_desc *
910d9a693cSMika Westerberg acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
920d9a693cSMika Westerberg 			int index, struct acpi_gpio_info *info)
935ccff852SMika Westerberg {
945ccff852SMika Westerberg 	return ERR_PTR(-ENOSYS);
955ccff852SMika Westerberg }
96504a3374SRafael J. Wysocki static inline struct gpio_desc *
97504a3374SRafael J. Wysocki acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
98504a3374SRafael J. Wysocki 		    int index, struct acpi_gpio_info *info)
99504a3374SRafael J. Wysocki {
100504a3374SRafael J. Wysocki 	return ERR_PTR(-ENXIO);
101504a3374SRafael J. Wysocki }
10266858527SRojhalat Ibrahim static inline int acpi_gpio_count(struct device *dev, const char *con_id)
10366858527SRojhalat Ibrahim {
10466858527SRojhalat Ibrahim 	return -ENODEV;
10566858527SRojhalat Ibrahim }
10610cf4899SDmitry Torokhov 
10710cf4899SDmitry Torokhov static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
10810cf4899SDmitry Torokhov 					    const char *con_id)
10910cf4899SDmitry Torokhov {
11010cf4899SDmitry Torokhov 	return false;
11110cf4899SDmitry Torokhov }
112664e3e5aSMika Westerberg #endif
113664e3e5aSMika Westerberg 
114f01d9075SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
115f01d9075SAlexandre Courbot 		   const char *list_name, int index, enum of_gpio_flags *flags);
116f01d9075SAlexandre Courbot 
1171bd6b601SAlexandre Courbot struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
1181bd6b601SAlexandre Courbot 
1190eb4c6c2SAlexandre Courbot extern struct spinlock gpio_lock;
120ff2b1359SLinus Walleij extern struct list_head gpio_devices;
1210eb4c6c2SAlexandre Courbot 
1220eb4c6c2SAlexandre Courbot struct gpio_desc {
1230eb4c6c2SAlexandre Courbot 	struct gpio_chip	*chip;
1240eb4c6c2SAlexandre Courbot 	unsigned long		flags;
1250eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */
1260eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED	0
1270eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT	1
1280eb4c6c2SAlexandre Courbot #define FLAG_EXPORT	2	/* protected by sysfs_lock */
1290eb4c6c2SAlexandre Courbot #define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
1300eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW	6	/* value has active low */
1310eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
1320eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
1330eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
134f625d460SBenoit Parrot #define FLAG_IS_HOGGED	11	/* GPIO is hogged */
1350eb4c6c2SAlexandre Courbot 
136c0017ed7SMarkus Pargmann 	/* Connection label */
1370eb4c6c2SAlexandre Courbot 	const char		*label;
138c0017ed7SMarkus Pargmann 	/* Name of the GPIO */
139c0017ed7SMarkus Pargmann 	const char		*name;
1400eb4c6c2SAlexandre Courbot };
1410eb4c6c2SAlexandre Courbot 
1420eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label);
1430eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc);
144f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name,
145f625d460SBenoit Parrot 		unsigned long lflags, enum gpiod_flags dflags);
1460eb4c6c2SAlexandre Courbot 
1470eb4c6c2SAlexandre Courbot /*
1480eb4c6c2SAlexandre Courbot  * Return the GPIO number of the passed descriptor relative to its chip
1490eb4c6c2SAlexandre Courbot  */
1500eb4c6c2SAlexandre Courbot static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
1510eb4c6c2SAlexandre Courbot {
1520eb4c6c2SAlexandre Courbot 	return desc - &desc->chip->desc[0];
1530eb4c6c2SAlexandre Courbot }
1540eb4c6c2SAlexandre Courbot 
1550eb4c6c2SAlexandre Courbot /* With descriptor prefix */
1560eb4c6c2SAlexandre Courbot 
1570eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...)					       \
1580eb4c6c2SAlexandre Courbot 	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1590eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1600eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...)					       \
1610eb4c6c2SAlexandre Courbot 	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1620eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1630eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...)					       \
1640eb4c6c2SAlexandre Courbot 	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
1650eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1660eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...)					       \
1670eb4c6c2SAlexandre Courbot 	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1680eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1690eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...)					       \
1700eb4c6c2SAlexandre Courbot 	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1710eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1720eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...)					       \
1730eb4c6c2SAlexandre Courbot 	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1740eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1750eb4c6c2SAlexandre Courbot 
1760eb4c6c2SAlexandre Courbot /* With chip prefix */
1770eb4c6c2SAlexandre Courbot 
1780eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...)					\
17934ffd85dSLinus Walleij 	dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1800eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...)					\
18134ffd85dSLinus Walleij 	dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1820eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...)					\
18334ffd85dSLinus Walleij 	dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1840eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...)					\
18534ffd85dSLinus Walleij 	dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1860eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...)					\
18734ffd85dSLinus Walleij 	dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1880eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...)					\
18934ffd85dSLinus Walleij 	dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
1900eb4c6c2SAlexandre Courbot 
1910eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS
1920eb4c6c2SAlexandre Courbot 
193426577bdSJohan Hovold int gpiochip_sysfs_register(struct gpio_chip *chip);
194426577bdSJohan Hovold void gpiochip_sysfs_unregister(struct gpio_chip *chip);
1950eb4c6c2SAlexandre Courbot 
1960eb4c6c2SAlexandre Courbot #else
1970eb4c6c2SAlexandre Courbot 
198426577bdSJohan Hovold static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
1990eb4c6c2SAlexandre Courbot {
2000eb4c6c2SAlexandre Courbot 	return 0;
2010eb4c6c2SAlexandre Courbot }
2020eb4c6c2SAlexandre Courbot 
203426577bdSJohan Hovold static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
2040eb4c6c2SAlexandre Courbot {
2050eb4c6c2SAlexandre Courbot }
2060eb4c6c2SAlexandre Courbot 
2070eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */
2080eb4c6c2SAlexandre Courbot 
209664e3e5aSMika Westerberg #endif /* GPIOLIB_H */
210