xref: /linux/drivers/gpio/gpiolib.h (revision 9d796e66230205cd3366f5660387bd9ecca9d336)
1 /*
2  * Internal GPIO functions.
3  *
4  * Copyright (C) 2013, Intel Corporation
5  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #ifndef GPIOLIB_H
13 #define GPIOLIB_H
14 
15 #include <linux/err.h>
16 #include <linux/device.h>
17 
18 enum of_gpio_flags;
19 
20 struct acpi_device;
21 
22 /**
23  * struct acpi_gpio_info - ACPI GPIO specific information
24  * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
25  * @active_low: in case of @gpioint, the pin is active low
26  */
27 struct acpi_gpio_info {
28 	bool gpioint;
29 	bool active_low;
30 };
31 
32 #ifdef CONFIG_ACPI
33 void acpi_gpiochip_add(struct gpio_chip *chip);
34 void acpi_gpiochip_remove(struct gpio_chip *chip);
35 
36 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
37 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
38 
39 struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
40 					  const char *propname, int index,
41 					  struct acpi_gpio_info *info);
42 #else
43 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
44 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
45 
46 static inline void
47 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
48 
49 static inline void
50 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
51 
52 static inline struct gpio_desc *
53 acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
54 			int index, struct acpi_gpio_info *info)
55 {
56 	return ERR_PTR(-ENOSYS);
57 }
58 #endif
59 
60 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
61 		   const char *list_name, int index, enum of_gpio_flags *flags);
62 
63 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
64 
65 extern struct spinlock gpio_lock;
66 extern struct list_head gpio_chips;
67 
68 struct gpio_desc {
69 	struct gpio_chip	*chip;
70 	unsigned long		flags;
71 /* flag symbols are bit numbers */
72 #define FLAG_REQUESTED	0
73 #define FLAG_IS_OUT	1
74 #define FLAG_EXPORT	2	/* protected by sysfs_lock */
75 #define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
76 #define FLAG_TRIG_FALL	4	/* trigger on falling edge */
77 #define FLAG_TRIG_RISE	5	/* trigger on rising edge */
78 #define FLAG_ACTIVE_LOW	6	/* value has active low */
79 #define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
80 #define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
81 #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
82 #define FLAG_SYSFS_DIR	10	/* show sysfs direction attribute */
83 
84 #define ID_SHIFT	16	/* add new flags before this one */
85 
86 #define GPIO_FLAGS_MASK		((1 << ID_SHIFT) - 1)
87 #define GPIO_TRIGGER_MASK	(BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
88 
89 	const char		*label;
90 };
91 
92 int gpiod_request(struct gpio_desc *desc, const char *label);
93 void gpiod_free(struct gpio_desc *desc);
94 
95 /*
96  * Return the GPIO number of the passed descriptor relative to its chip
97  */
98 static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
99 {
100 	return desc - &desc->chip->desc[0];
101 }
102 
103 /* With descriptor prefix */
104 
105 #define gpiod_emerg(desc, fmt, ...)					       \
106 	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
107 		 ##__VA_ARGS__)
108 #define gpiod_crit(desc, fmt, ...)					       \
109 	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
110 		 ##__VA_ARGS__)
111 #define gpiod_err(desc, fmt, ...)					       \
112 	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
113 		 ##__VA_ARGS__)
114 #define gpiod_warn(desc, fmt, ...)					       \
115 	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
116 		 ##__VA_ARGS__)
117 #define gpiod_info(desc, fmt, ...)					       \
118 	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
119 		 ##__VA_ARGS__)
120 #define gpiod_dbg(desc, fmt, ...)					       \
121 	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
122 		 ##__VA_ARGS__)
123 
124 /* With chip prefix */
125 
126 #define chip_emerg(chip, fmt, ...)					\
127 	pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
128 #define chip_crit(chip, fmt, ...)					\
129 	pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
130 #define chip_err(chip, fmt, ...)					\
131 	pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
132 #define chip_warn(chip, fmt, ...)					\
133 	pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
134 #define chip_info(chip, fmt, ...)					\
135 	pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
136 #define chip_dbg(chip, fmt, ...)					\
137 	pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
138 
139 #ifdef CONFIG_GPIO_SYSFS
140 
141 int gpiochip_export(struct gpio_chip *chip);
142 void gpiochip_unexport(struct gpio_chip *chip);
143 
144 #else
145 
146 static inline int gpiochip_export(struct gpio_chip *chip)
147 {
148 	return 0;
149 }
150 
151 static inline void gpiochip_unexport(struct gpio_chip *chip)
152 {
153 }
154 
155 #endif /* CONFIG_GPIO_SYSFS */
156 
157 #endif /* GPIOLIB_H */
158