xref: /openbmc/linux/drivers/gpio/gpiolib.h (revision 29ab875b)
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 
155ccff852SMika Westerberg #include <linux/err.h>
165ccff852SMika Westerberg #include <linux/device.h>
175ccff852SMika Westerberg 
18f01d9075SAlexandre Courbot enum of_gpio_flags;
1929ab875bSLinus Walleij enum gpiod_flags;
20ce793486SRafael J. Wysocki struct acpi_device;
21ce793486SRafael J. Wysocki 
225ccff852SMika Westerberg /**
235ccff852SMika Westerberg  * struct acpi_gpio_info - ACPI GPIO specific information
245ccff852SMika Westerberg  * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
255ccff852SMika Westerberg  * @active_low: in case of @gpioint, the pin is active low
265ccff852SMika Westerberg  */
275ccff852SMika Westerberg struct acpi_gpio_info {
285ccff852SMika Westerberg 	bool gpioint;
295ccff852SMika Westerberg 	bool active_low;
305ccff852SMika Westerberg };
315ccff852SMika Westerberg 
327f2e553aSRojhalat Ibrahim /* gpio suffixes used for ACPI and device tree lookup */
337f2e553aSRojhalat Ibrahim static const char * const gpio_suffixes[] = { "gpios", "gpio" };
347f2e553aSRojhalat Ibrahim 
35664e3e5aSMika Westerberg #ifdef CONFIG_ACPI
36664e3e5aSMika Westerberg void acpi_gpiochip_add(struct gpio_chip *chip);
37664e3e5aSMika Westerberg void acpi_gpiochip_remove(struct gpio_chip *chip);
385ccff852SMika Westerberg 
39afa82fabSMika Westerberg void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
40afa82fabSMika Westerberg void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
41afa82fabSMika Westerberg 
420d9a693cSMika Westerberg struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
430d9a693cSMika Westerberg 					  const char *propname, int index,
445ccff852SMika Westerberg 					  struct acpi_gpio_info *info);
45504a3374SRafael J. Wysocki struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
46504a3374SRafael J. Wysocki 				      const char *propname, int index,
47504a3374SRafael J. Wysocki 				      struct acpi_gpio_info *info);
4866858527SRojhalat Ibrahim 
4966858527SRojhalat Ibrahim int acpi_gpio_count(struct device *dev, const char *con_id);
509c3c9bc9SDmitry Torokhov 
519c3c9bc9SDmitry Torokhov bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
52664e3e5aSMika Westerberg #else
53664e3e5aSMika Westerberg static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
54664e3e5aSMika Westerberg static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
555ccff852SMika Westerberg 
56afa82fabSMika Westerberg static inline void
57afa82fabSMika Westerberg acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
58afa82fabSMika Westerberg 
59afa82fabSMika Westerberg static inline void
60afa82fabSMika Westerberg acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
61afa82fabSMika Westerberg 
625ccff852SMika Westerberg static inline struct gpio_desc *
630d9a693cSMika Westerberg acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
640d9a693cSMika Westerberg 			int index, struct acpi_gpio_info *info)
655ccff852SMika Westerberg {
665ccff852SMika Westerberg 	return ERR_PTR(-ENOSYS);
675ccff852SMika Westerberg }
68504a3374SRafael J. Wysocki static inline struct gpio_desc *
69504a3374SRafael J. Wysocki acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
70504a3374SRafael J. Wysocki 		    int index, struct acpi_gpio_info *info)
71504a3374SRafael J. Wysocki {
72504a3374SRafael J. Wysocki 	return ERR_PTR(-ENXIO);
73504a3374SRafael J. Wysocki }
7466858527SRojhalat Ibrahim static inline int acpi_gpio_count(struct device *dev, const char *con_id)
7566858527SRojhalat Ibrahim {
7666858527SRojhalat Ibrahim 	return -ENODEV;
7766858527SRojhalat Ibrahim }
789c3c9bc9SDmitry Torokhov 
799c3c9bc9SDmitry Torokhov static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
809c3c9bc9SDmitry Torokhov 					    const char *con_id)
819c3c9bc9SDmitry Torokhov {
829c3c9bc9SDmitry Torokhov 	return false;
839c3c9bc9SDmitry Torokhov }
84664e3e5aSMika Westerberg #endif
85664e3e5aSMika Westerberg 
86f01d9075SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
87f01d9075SAlexandre Courbot 		   const char *list_name, int index, enum of_gpio_flags *flags);
88f01d9075SAlexandre Courbot 
891bd6b601SAlexandre Courbot struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
901bd6b601SAlexandre Courbot 
910eb4c6c2SAlexandre Courbot extern struct spinlock gpio_lock;
920eb4c6c2SAlexandre Courbot extern struct list_head gpio_chips;
930eb4c6c2SAlexandre Courbot 
940eb4c6c2SAlexandre Courbot struct gpio_desc {
950eb4c6c2SAlexandre Courbot 	struct gpio_chip	*chip;
960eb4c6c2SAlexandre Courbot 	unsigned long		flags;
970eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */
980eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED	0
990eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT	1
1000eb4c6c2SAlexandre Courbot #define FLAG_EXPORT	2	/* protected by sysfs_lock */
1010eb4c6c2SAlexandre Courbot #define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
1020eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW	6	/* value has active low */
1030eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
1040eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
1050eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
106f625d460SBenoit Parrot #define FLAG_IS_HOGGED	11	/* GPIO is hogged */
1070eb4c6c2SAlexandre Courbot 
108c0017ed7SMarkus Pargmann 	/* Connection label */
1090eb4c6c2SAlexandre Courbot 	const char		*label;
110c0017ed7SMarkus Pargmann 	/* Name of the GPIO */
111c0017ed7SMarkus Pargmann 	const char		*name;
1120eb4c6c2SAlexandre Courbot };
1130eb4c6c2SAlexandre Courbot 
1140eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label);
1150eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc);
116f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name,
117f625d460SBenoit Parrot 		unsigned long lflags, enum gpiod_flags dflags);
1180eb4c6c2SAlexandre Courbot 
1190eb4c6c2SAlexandre Courbot /*
1200eb4c6c2SAlexandre Courbot  * Return the GPIO number of the passed descriptor relative to its chip
1210eb4c6c2SAlexandre Courbot  */
1220eb4c6c2SAlexandre Courbot static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
1230eb4c6c2SAlexandre Courbot {
1240eb4c6c2SAlexandre Courbot 	return desc - &desc->chip->desc[0];
1250eb4c6c2SAlexandre Courbot }
1260eb4c6c2SAlexandre Courbot 
1270eb4c6c2SAlexandre Courbot /* With descriptor prefix */
1280eb4c6c2SAlexandre Courbot 
1290eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...)					       \
1300eb4c6c2SAlexandre Courbot 	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1310eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1320eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...)					       \
1330eb4c6c2SAlexandre Courbot 	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1340eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1350eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...)					       \
1360eb4c6c2SAlexandre Courbot 	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
1370eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1380eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...)					       \
1390eb4c6c2SAlexandre Courbot 	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1400eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1410eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...)					       \
1420eb4c6c2SAlexandre Courbot 	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1430eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1440eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...)					       \
1450eb4c6c2SAlexandre Courbot 	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1460eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1470eb4c6c2SAlexandre Courbot 
1480eb4c6c2SAlexandre Courbot /* With chip prefix */
1490eb4c6c2SAlexandre Courbot 
1500eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...)					\
1510eb4c6c2SAlexandre Courbot 	pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1520eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...)					\
1530eb4c6c2SAlexandre Courbot 	pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1540eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...)					\
1550eb4c6c2SAlexandre Courbot 	pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1560eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...)					\
1570eb4c6c2SAlexandre Courbot 	pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1580eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...)					\
1590eb4c6c2SAlexandre Courbot 	pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1600eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...)					\
1610eb4c6c2SAlexandre Courbot 	pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1620eb4c6c2SAlexandre Courbot 
1630eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS
1640eb4c6c2SAlexandre Courbot 
165426577bdSJohan Hovold int gpiochip_sysfs_register(struct gpio_chip *chip);
166426577bdSJohan Hovold void gpiochip_sysfs_unregister(struct gpio_chip *chip);
1670eb4c6c2SAlexandre Courbot 
1680eb4c6c2SAlexandre Courbot #else
1690eb4c6c2SAlexandre Courbot 
170426577bdSJohan Hovold static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
1710eb4c6c2SAlexandre Courbot {
1720eb4c6c2SAlexandre Courbot 	return 0;
1730eb4c6c2SAlexandre Courbot }
1740eb4c6c2SAlexandre Courbot 
175426577bdSJohan Hovold static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
1760eb4c6c2SAlexandre Courbot {
1770eb4c6c2SAlexandre Courbot }
1780eb4c6c2SAlexandre Courbot 
1790eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */
1800eb4c6c2SAlexandre Courbot 
181664e3e5aSMika Westerberg #endif /* GPIOLIB_H */
182