xref: /openbmc/linux/drivers/gpio/gpiolib.h (revision 426577bd)
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;
19f01d9075SAlexandre Courbot 
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);
4566858527SRojhalat Ibrahim 
4666858527SRojhalat Ibrahim int acpi_gpio_count(struct device *dev, const char *con_id);
47664e3e5aSMika Westerberg #else
48664e3e5aSMika Westerberg static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
49664e3e5aSMika Westerberg static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
505ccff852SMika Westerberg 
51afa82fabSMika Westerberg static inline void
52afa82fabSMika Westerberg acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
53afa82fabSMika Westerberg 
54afa82fabSMika Westerberg static inline void
55afa82fabSMika Westerberg acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
56afa82fabSMika Westerberg 
575ccff852SMika Westerberg static inline struct gpio_desc *
580d9a693cSMika Westerberg acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
590d9a693cSMika Westerberg 			int index, struct acpi_gpio_info *info)
605ccff852SMika Westerberg {
615ccff852SMika Westerberg 	return ERR_PTR(-ENOSYS);
625ccff852SMika Westerberg }
6366858527SRojhalat Ibrahim 
6466858527SRojhalat Ibrahim static inline int acpi_gpio_count(struct device *dev, const char *con_id)
6566858527SRojhalat Ibrahim {
6666858527SRojhalat Ibrahim 	return -ENODEV;
6766858527SRojhalat Ibrahim }
68664e3e5aSMika Westerberg #endif
69664e3e5aSMika Westerberg 
70f01d9075SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
71f01d9075SAlexandre Courbot 		   const char *list_name, int index, enum of_gpio_flags *flags);
72f01d9075SAlexandre Courbot 
731bd6b601SAlexandre Courbot struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
741bd6b601SAlexandre Courbot 
750eb4c6c2SAlexandre Courbot extern struct spinlock gpio_lock;
760eb4c6c2SAlexandre Courbot extern struct list_head gpio_chips;
770eb4c6c2SAlexandre Courbot 
780eb4c6c2SAlexandre Courbot struct gpio_desc {
790eb4c6c2SAlexandre Courbot 	struct gpio_chip	*chip;
800eb4c6c2SAlexandre Courbot 	unsigned long		flags;
810eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */
820eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED	0
830eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT	1
840eb4c6c2SAlexandre Courbot #define FLAG_EXPORT	2	/* protected by sysfs_lock */
850eb4c6c2SAlexandre Courbot #define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
860eb4c6c2SAlexandre Courbot #define FLAG_TRIG_FALL	4	/* trigger on falling edge */
870eb4c6c2SAlexandre Courbot #define FLAG_TRIG_RISE	5	/* trigger on rising edge */
880eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW	6	/* value has active low */
890eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
900eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
910eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
92ebbeba12SJohan Hovold #define FLAG_SYSFS_DIR	10	/* show sysfs direction attribute */
93f625d460SBenoit Parrot #define FLAG_IS_HOGGED	11	/* GPIO is hogged */
940eb4c6c2SAlexandre Courbot 
950eb4c6c2SAlexandre Courbot #define ID_SHIFT	16	/* add new flags before this one */
960eb4c6c2SAlexandre Courbot 
970eb4c6c2SAlexandre Courbot #define GPIO_FLAGS_MASK		((1 << ID_SHIFT) - 1)
980eb4c6c2SAlexandre Courbot #define GPIO_TRIGGER_MASK	(BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
990eb4c6c2SAlexandre Courbot 
1000eb4c6c2SAlexandre Courbot 	const char		*label;
1010eb4c6c2SAlexandre Courbot };
1020eb4c6c2SAlexandre Courbot 
1030eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label);
1040eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc);
105f625d460SBenoit Parrot int gpiod_hog(struct gpio_desc *desc, const char *name,
106f625d460SBenoit Parrot 		unsigned long lflags, enum gpiod_flags dflags);
1070eb4c6c2SAlexandre Courbot 
1080eb4c6c2SAlexandre Courbot /*
1090eb4c6c2SAlexandre Courbot  * Return the GPIO number of the passed descriptor relative to its chip
1100eb4c6c2SAlexandre Courbot  */
1110eb4c6c2SAlexandre Courbot static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
1120eb4c6c2SAlexandre Courbot {
1130eb4c6c2SAlexandre Courbot 	return desc - &desc->chip->desc[0];
1140eb4c6c2SAlexandre Courbot }
1150eb4c6c2SAlexandre Courbot 
1160eb4c6c2SAlexandre Courbot /* With descriptor prefix */
1170eb4c6c2SAlexandre Courbot 
1180eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...)					       \
1190eb4c6c2SAlexandre Courbot 	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1200eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1210eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...)					       \
1220eb4c6c2SAlexandre Courbot 	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1230eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1240eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...)					       \
1250eb4c6c2SAlexandre Courbot 	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
1260eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1270eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...)					       \
1280eb4c6c2SAlexandre Courbot 	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1290eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1300eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...)					       \
1310eb4c6c2SAlexandre Courbot 	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
1320eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1330eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...)					       \
1340eb4c6c2SAlexandre Courbot 	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
1350eb4c6c2SAlexandre Courbot 		 ##__VA_ARGS__)
1360eb4c6c2SAlexandre Courbot 
1370eb4c6c2SAlexandre Courbot /* With chip prefix */
1380eb4c6c2SAlexandre Courbot 
1390eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...)					\
1400eb4c6c2SAlexandre Courbot 	pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1410eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...)					\
1420eb4c6c2SAlexandre Courbot 	pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1430eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...)					\
1440eb4c6c2SAlexandre Courbot 	pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1450eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...)					\
1460eb4c6c2SAlexandre Courbot 	pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1470eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...)					\
1480eb4c6c2SAlexandre Courbot 	pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1490eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...)					\
1500eb4c6c2SAlexandre Courbot 	pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
1510eb4c6c2SAlexandre Courbot 
1520eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS
1530eb4c6c2SAlexandre Courbot 
154426577bdSJohan Hovold int gpiochip_sysfs_register(struct gpio_chip *chip);
155426577bdSJohan Hovold void gpiochip_sysfs_unregister(struct gpio_chip *chip);
1560eb4c6c2SAlexandre Courbot 
1570eb4c6c2SAlexandre Courbot #else
1580eb4c6c2SAlexandre Courbot 
159426577bdSJohan Hovold static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
1600eb4c6c2SAlexandre Courbot {
1610eb4c6c2SAlexandre Courbot 	return 0;
1620eb4c6c2SAlexandre Courbot }
1630eb4c6c2SAlexandre Courbot 
164426577bdSJohan Hovold static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
1650eb4c6c2SAlexandre Courbot {
1660eb4c6c2SAlexandre Courbot }
1670eb4c6c2SAlexandre Courbot 
1680eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */
1690eb4c6c2SAlexandre Courbot 
170664e3e5aSMika Westerberg #endif /* GPIOLIB_H */
171