1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Internal GPIO functions.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2013, Intel Corporation
6*4882a593Smuzhiyun * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifndef GPIOLIB_H
10*4882a593Smuzhiyun #define GPIOLIB_H
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/gpio/driver.h>
13*4882a593Smuzhiyun #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
14*4882a593Smuzhiyun #include <linux/err.h>
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/cdev.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define GPIOCHIP_NAME "gpiochip"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /**
22*4882a593Smuzhiyun * struct gpio_device - internal state container for GPIO devices
23*4882a593Smuzhiyun * @id: numerical ID number for the GPIO chip
24*4882a593Smuzhiyun * @dev: the GPIO device struct
25*4882a593Smuzhiyun * @chrdev: character device for the GPIO device
26*4882a593Smuzhiyun * @mockdev: class device used by the deprecated sysfs interface (may be
27*4882a593Smuzhiyun * NULL)
28*4882a593Smuzhiyun * @owner: helps prevent removal of modules exporting active GPIOs
29*4882a593Smuzhiyun * @chip: pointer to the corresponding gpiochip, holding static
30*4882a593Smuzhiyun * data for this device
31*4882a593Smuzhiyun * @descs: array of ngpio descriptors.
32*4882a593Smuzhiyun * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
33*4882a593Smuzhiyun * of the @descs array.
34*4882a593Smuzhiyun * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
35*4882a593Smuzhiyun * at device creation time.
36*4882a593Smuzhiyun * @label: a descriptive name for the GPIO device, such as the part number
37*4882a593Smuzhiyun * or name of the IP component in a System on Chip.
38*4882a593Smuzhiyun * @data: per-instance data assigned by the driver
39*4882a593Smuzhiyun * @list: links gpio_device:s together for traversal
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * This state container holds most of the runtime variable data
42*4882a593Smuzhiyun * for a GPIO device and can hold references and live on after the
43*4882a593Smuzhiyun * GPIO chip has been removed, if it is still being used from
44*4882a593Smuzhiyun * userspace.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun struct gpio_device {
47*4882a593Smuzhiyun int id;
48*4882a593Smuzhiyun struct device dev;
49*4882a593Smuzhiyun struct cdev chrdev;
50*4882a593Smuzhiyun struct device *mockdev;
51*4882a593Smuzhiyun struct module *owner;
52*4882a593Smuzhiyun struct gpio_chip *chip;
53*4882a593Smuzhiyun struct gpio_desc *descs;
54*4882a593Smuzhiyun int base;
55*4882a593Smuzhiyun u16 ngpio;
56*4882a593Smuzhiyun const char *label;
57*4882a593Smuzhiyun void *data;
58*4882a593Smuzhiyun struct list_head list;
59*4882a593Smuzhiyun struct blocking_notifier_head notifier;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #ifdef CONFIG_PINCTRL
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
64*4882a593Smuzhiyun * describe the actual pin range which they serve in an SoC. This
65*4882a593Smuzhiyun * information would be used by pinctrl subsystem to configure
66*4882a593Smuzhiyun * corresponding pins for gpio usage.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun struct list_head pin_ranges;
69*4882a593Smuzhiyun #endif
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* gpio suffixes used for ACPI and device tree lookup */
73*4882a593Smuzhiyun static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct gpio_array {
76*4882a593Smuzhiyun struct gpio_desc **desc;
77*4882a593Smuzhiyun unsigned int size;
78*4882a593Smuzhiyun struct gpio_chip *chip;
79*4882a593Smuzhiyun unsigned long *get_mask;
80*4882a593Smuzhiyun unsigned long *set_mask;
81*4882a593Smuzhiyun unsigned long invert_mask[];
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
85*4882a593Smuzhiyun int gpiod_get_array_value_complex(bool raw, bool can_sleep,
86*4882a593Smuzhiyun unsigned int array_size,
87*4882a593Smuzhiyun struct gpio_desc **desc_array,
88*4882a593Smuzhiyun struct gpio_array *array_info,
89*4882a593Smuzhiyun unsigned long *value_bitmap);
90*4882a593Smuzhiyun int gpiod_set_array_value_complex(bool raw, bool can_sleep,
91*4882a593Smuzhiyun unsigned int array_size,
92*4882a593Smuzhiyun struct gpio_desc **desc_array,
93*4882a593Smuzhiyun struct gpio_array *array_info,
94*4882a593Smuzhiyun unsigned long *value_bitmap);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun extern spinlock_t gpio_lock;
97*4882a593Smuzhiyun extern struct list_head gpio_devices;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun struct gpio_desc {
100*4882a593Smuzhiyun struct gpio_device *gdev;
101*4882a593Smuzhiyun unsigned long flags;
102*4882a593Smuzhiyun /* flag symbols are bit numbers */
103*4882a593Smuzhiyun #define FLAG_REQUESTED 0
104*4882a593Smuzhiyun #define FLAG_IS_OUT 1
105*4882a593Smuzhiyun #define FLAG_EXPORT 2 /* protected by sysfs_lock */
106*4882a593Smuzhiyun #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
107*4882a593Smuzhiyun #define FLAG_ACTIVE_LOW 6 /* value has active low */
108*4882a593Smuzhiyun #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
109*4882a593Smuzhiyun #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
110*4882a593Smuzhiyun #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
111*4882a593Smuzhiyun #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
112*4882a593Smuzhiyun #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
113*4882a593Smuzhiyun #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
114*4882a593Smuzhiyun #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
115*4882a593Smuzhiyun #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
116*4882a593Smuzhiyun #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
117*4882a593Smuzhiyun #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
118*4882a593Smuzhiyun #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* Connection label */
121*4882a593Smuzhiyun const char *label;
122*4882a593Smuzhiyun /* Name of the GPIO */
123*4882a593Smuzhiyun const char *name;
124*4882a593Smuzhiyun #ifdef CONFIG_OF_DYNAMIC
125*4882a593Smuzhiyun struct device_node *hog;
126*4882a593Smuzhiyun #endif
127*4882a593Smuzhiyun #ifdef CONFIG_GPIO_CDEV
128*4882a593Smuzhiyun /* debounce period in microseconds */
129*4882a593Smuzhiyun unsigned int debounce_period_us;
130*4882a593Smuzhiyun #endif
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun int gpiod_request(struct gpio_desc *desc, const char *label);
134*4882a593Smuzhiyun void gpiod_free(struct gpio_desc *desc);
135*4882a593Smuzhiyun int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
136*4882a593Smuzhiyun unsigned long lflags, enum gpiod_flags dflags);
137*4882a593Smuzhiyun int gpiod_hog(struct gpio_desc *desc, const char *name,
138*4882a593Smuzhiyun unsigned long lflags, enum gpiod_flags dflags);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * Return the GPIO number of the passed descriptor relative to its chip
142*4882a593Smuzhiyun */
gpio_chip_hwgpio(const struct gpio_desc * desc)143*4882a593Smuzhiyun static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun return desc - &desc->gdev->descs[0];
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* With descriptor prefix */
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun #define gpiod_emerg(desc, fmt, ...) \
151*4882a593Smuzhiyun pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
152*4882a593Smuzhiyun ##__VA_ARGS__)
153*4882a593Smuzhiyun #define gpiod_crit(desc, fmt, ...) \
154*4882a593Smuzhiyun pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
155*4882a593Smuzhiyun ##__VA_ARGS__)
156*4882a593Smuzhiyun #define gpiod_err(desc, fmt, ...) \
157*4882a593Smuzhiyun pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
158*4882a593Smuzhiyun ##__VA_ARGS__)
159*4882a593Smuzhiyun #define gpiod_warn(desc, fmt, ...) \
160*4882a593Smuzhiyun pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
161*4882a593Smuzhiyun ##__VA_ARGS__)
162*4882a593Smuzhiyun #define gpiod_info(desc, fmt, ...) \
163*4882a593Smuzhiyun pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
164*4882a593Smuzhiyun ##__VA_ARGS__)
165*4882a593Smuzhiyun #define gpiod_dbg(desc, fmt, ...) \
166*4882a593Smuzhiyun pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
167*4882a593Smuzhiyun ##__VA_ARGS__)
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* With chip prefix */
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun #define chip_emerg(gc, fmt, ...) \
172*4882a593Smuzhiyun dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
173*4882a593Smuzhiyun #define chip_crit(gc, fmt, ...) \
174*4882a593Smuzhiyun dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
175*4882a593Smuzhiyun #define chip_err(gc, fmt, ...) \
176*4882a593Smuzhiyun dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
177*4882a593Smuzhiyun #define chip_warn(gc, fmt, ...) \
178*4882a593Smuzhiyun dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
179*4882a593Smuzhiyun #define chip_info(gc, fmt, ...) \
180*4882a593Smuzhiyun dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
181*4882a593Smuzhiyun #define chip_dbg(gc, fmt, ...) \
182*4882a593Smuzhiyun dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun #endif /* GPIOLIB_H */
185