/* SPDX-License-Identifier: GPL-2.0 */ /* * Internal GPIO functions. * * Copyright (C) 2013, Intel Corporation * Author: Mika Westerberg <[email protected]> */ #ifndef GPIOLIB_H #define GPIOLIB_H #include <linux/cdev.h> #include <linux/device.h> #include <linux/err.h> #include <linux/gpio/consumer.h> /* for enum gpiod_flags */ #include <linux/gpio/driver.h> #include <linux/module.h> #include <linux/notifier.h> #include <linux/srcu.h> #define GPIOCHIP_NAME … /** * struct gpio_device - internal state container for GPIO devices * @dev: the GPIO device struct * @chrdev: character device for the GPIO device * @id: numerical ID number for the GPIO chip * @mockdev: class device used by the deprecated sysfs interface (may be * NULL) * @owner: helps prevent removal of modules exporting active GPIOs * @chip: pointer to the corresponding gpiochip, holding static * data for this device * @descs: array of ngpio descriptors. * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users * @ngpio: the number of GPIO lines on this GPIO device, equal to the size * of the @descs array. * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep * implying that they cannot be used from atomic context * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned * at device creation time. * @label: a descriptive name for the GPIO device, such as the part number * or name of the IP component in a System on Chip. * @data: per-instance data assigned by the driver * @list: links gpio_device:s together for traversal * @line_state_notifier: used to notify subscribers about lines being * requested, released or reconfigured * @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered * @srcu: protects the pointer to the underlying GPIO chip * @pin_ranges: range of pins served by the GPIO driver * * This state container holds most of the runtime variable data * for a GPIO device and can hold references and live on after the * GPIO chip has been removed, if it is still being used from * userspace. */ struct gpio_device { … }; static inline struct gpio_device *to_gpio_device(struct device *dev) { … } /* gpio suffixes used for ACPI and device tree lookup */ extern const char *const gpio_suffixes[]; extern const size_t gpio_suffix_count; /** * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes * * @desc: Array of pointers to the GPIO descriptors * @size: Number of elements in desc * @chip: Parent GPIO chip * @get_mask: Get mask used in fastpath * @set_mask: Set mask used in fastpath * @invert_mask: Invert mask used in fastpath * * This structure is attached to struct gpiod_descs obtained from * gpiod_get_array() and can be passed back to get/set array functions in order * to activate fast processing path if applicable. */ struct gpio_array { … }; #define for_each_gpio_desc(gc, desc) … \ #define for_each_gpio_desc_with_flag(gc, desc, flag) … int gpiod_get_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array, struct gpio_array *array_info, unsigned long *value_bitmap); int gpiod_set_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array, struct gpio_array *array_info, unsigned long *value_bitmap); int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); struct gpio_desc_label { … }; /** * struct gpio_desc - Opaque descriptor for a GPIO * * @gdev: Pointer to the parent GPIO device * @flags: Binary descriptor flags * @label: Name of the consumer * @name: Line name * @hog: Pointer to the device node that hogs this line (if any) * * These are obtained using gpiod_get() and are preferable to the old * integer-based handles. * * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be * valid until the GPIO is released. */ struct gpio_desc { … }; #define gpiod_not_found(desc) … struct gpio_chip_guard { … }; DEFINE_CLASS(gpio_chip_guard, struct gpio_chip_guard, srcu_read_unlock(&_T.gdev->srcu, _T.idx), ({ … } int gpiod_request(struct gpio_desc *desc, const char *label); void gpiod_free(struct gpio_desc *desc); static inline int gpiod_request_user(struct gpio_desc *desc, const char *label) { … } struct gpio_desc *gpiod_find_and_request(struct device *consumer, struct fwnode_handle *fwnode, const char *con_id, unsigned int idx, enum gpiod_flags flags, const char *label, bool platform_lookup_allowed); int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, unsigned long lflags, enum gpiod_flags dflags); int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); int gpiod_hog(struct gpio_desc *desc, const char *name, unsigned long lflags, enum gpiod_flags dflags); int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum); const char *gpiod_get_label(struct gpio_desc *desc); /* * Return the GPIO number of the passed descriptor relative to its chip */ static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) { … } /* With descriptor prefix */ #define gpiod_err(desc, fmt, ...) … #define gpiod_warn(desc, fmt, ...) … #define gpiod_dbg(desc, fmt, ...) … /* With chip prefix */ #define chip_err(gc, fmt, ...) … #define chip_warn(gc, fmt, ...) … #define chip_info(gc, fmt, ...) … #define chip_dbg(gc, fmt, ...) … #endif /* GPIOLIB_H */