162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Internal GPIO functions. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2013, Intel Corporation 662306a36Sopenharmony_ci * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#ifndef GPIOLIB_H 1062306a36Sopenharmony_ci#define GPIOLIB_H 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/cdev.h> 1362306a36Sopenharmony_ci#include <linux/device.h> 1462306a36Sopenharmony_ci#include <linux/err.h> 1562306a36Sopenharmony_ci#include <linux/gpio/consumer.h> /* for enum gpiod_flags */ 1662306a36Sopenharmony_ci#include <linux/gpio/driver.h> 1762306a36Sopenharmony_ci#include <linux/module.h> 1862306a36Sopenharmony_ci#include <linux/notifier.h> 1962306a36Sopenharmony_ci#include <linux/rwsem.h> 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#define GPIOCHIP_NAME "gpiochip" 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/** 2462306a36Sopenharmony_ci * struct gpio_device - internal state container for GPIO devices 2562306a36Sopenharmony_ci * @dev: the GPIO device struct 2662306a36Sopenharmony_ci * @chrdev: character device for the GPIO device 2762306a36Sopenharmony_ci * @id: numerical ID number for the GPIO chip 2862306a36Sopenharmony_ci * @mockdev: class device used by the deprecated sysfs interface (may be 2962306a36Sopenharmony_ci * NULL) 3062306a36Sopenharmony_ci * @owner: helps prevent removal of modules exporting active GPIOs 3162306a36Sopenharmony_ci * @chip: pointer to the corresponding gpiochip, holding static 3262306a36Sopenharmony_ci * data for this device 3362306a36Sopenharmony_ci * @descs: array of ngpio descriptors. 3462306a36Sopenharmony_ci * @ngpio: the number of GPIO lines on this GPIO device, equal to the size 3562306a36Sopenharmony_ci * of the @descs array. 3662306a36Sopenharmony_ci * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned 3762306a36Sopenharmony_ci * at device creation time. 3862306a36Sopenharmony_ci * @label: a descriptive name for the GPIO device, such as the part number 3962306a36Sopenharmony_ci * or name of the IP component in a System on Chip. 4062306a36Sopenharmony_ci * @data: per-instance data assigned by the driver 4162306a36Sopenharmony_ci * @list: links gpio_device:s together for traversal 4262306a36Sopenharmony_ci * @line_state_notifier: used to notify subscribers about lines being 4362306a36Sopenharmony_ci * requested, released or reconfigured 4462306a36Sopenharmony_ci * @device_notifier: used to notify character device wait queues about the GPIO 4562306a36Sopenharmony_ci * device being unregistered 4662306a36Sopenharmony_ci * @sem: protects the structure from a NULL-pointer dereference of @chip by 4762306a36Sopenharmony_ci * user-space operations when the device gets unregistered during 4862306a36Sopenharmony_ci * a hot-unplug event 4962306a36Sopenharmony_ci * @pin_ranges: range of pins served by the GPIO driver 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * This state container holds most of the runtime variable data 5262306a36Sopenharmony_ci * for a GPIO device and can hold references and live on after the 5362306a36Sopenharmony_ci * GPIO chip has been removed, if it is still being used from 5462306a36Sopenharmony_ci * userspace. 5562306a36Sopenharmony_ci */ 5662306a36Sopenharmony_cistruct gpio_device { 5762306a36Sopenharmony_ci struct device dev; 5862306a36Sopenharmony_ci struct cdev chrdev; 5962306a36Sopenharmony_ci int id; 6062306a36Sopenharmony_ci struct device *mockdev; 6162306a36Sopenharmony_ci struct module *owner; 6262306a36Sopenharmony_ci struct gpio_chip *chip; 6362306a36Sopenharmony_ci struct gpio_desc *descs; 6462306a36Sopenharmony_ci int base; 6562306a36Sopenharmony_ci u16 ngpio; 6662306a36Sopenharmony_ci const char *label; 6762306a36Sopenharmony_ci void *data; 6862306a36Sopenharmony_ci struct list_head list; 6962306a36Sopenharmony_ci struct blocking_notifier_head line_state_notifier; 7062306a36Sopenharmony_ci struct blocking_notifier_head device_notifier; 7162306a36Sopenharmony_ci struct rw_semaphore sem; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci#ifdef CONFIG_PINCTRL 7462306a36Sopenharmony_ci /* 7562306a36Sopenharmony_ci * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally 7662306a36Sopenharmony_ci * describe the actual pin range which they serve in an SoC. This 7762306a36Sopenharmony_ci * information would be used by pinctrl subsystem to configure 7862306a36Sopenharmony_ci * corresponding pins for gpio usage. 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_ci struct list_head pin_ranges; 8162306a36Sopenharmony_ci#endif 8262306a36Sopenharmony_ci}; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_cistatic inline struct gpio_device *to_gpio_device(struct device *dev) 8562306a36Sopenharmony_ci{ 8662306a36Sopenharmony_ci return container_of(dev, struct gpio_device, dev); 8762306a36Sopenharmony_ci} 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* gpio suffixes used for ACPI and device tree lookup */ 9062306a36Sopenharmony_cistatic __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" }; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci/** 9362306a36Sopenharmony_ci * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes 9462306a36Sopenharmony_ci * 9562306a36Sopenharmony_ci * @desc: Array of pointers to the GPIO descriptors 9662306a36Sopenharmony_ci * @size: Number of elements in desc 9762306a36Sopenharmony_ci * @chip: Parent GPIO chip 9862306a36Sopenharmony_ci * @get_mask: Get mask used in fastpath 9962306a36Sopenharmony_ci * @set_mask: Set mask used in fastpath 10062306a36Sopenharmony_ci * @invert_mask: Invert mask used in fastpath 10162306a36Sopenharmony_ci * 10262306a36Sopenharmony_ci * This structure is attached to struct gpiod_descs obtained from 10362306a36Sopenharmony_ci * gpiod_get_array() and can be passed back to get/set array functions in order 10462306a36Sopenharmony_ci * to activate fast processing path if applicable. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_cistruct gpio_array { 10762306a36Sopenharmony_ci struct gpio_desc **desc; 10862306a36Sopenharmony_ci unsigned int size; 10962306a36Sopenharmony_ci struct gpio_chip *chip; 11062306a36Sopenharmony_ci unsigned long *get_mask; 11162306a36Sopenharmony_ci unsigned long *set_mask; 11262306a36Sopenharmony_ci unsigned long invert_mask[]; 11362306a36Sopenharmony_ci}; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_cistruct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum); 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci#define for_each_gpio_desc(gc, desc) \ 11862306a36Sopenharmony_ci for (unsigned int __i = 0; \ 11962306a36Sopenharmony_ci __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \ 12062306a36Sopenharmony_ci __i++) \ 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci#define for_each_gpio_desc_with_flag(gc, desc, flag) \ 12362306a36Sopenharmony_ci for_each_gpio_desc(gc, desc) \ 12462306a36Sopenharmony_ci if (!test_bit(flag, &desc->flags)) {} else 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ciint gpiod_get_array_value_complex(bool raw, bool can_sleep, 12762306a36Sopenharmony_ci unsigned int array_size, 12862306a36Sopenharmony_ci struct gpio_desc **desc_array, 12962306a36Sopenharmony_ci struct gpio_array *array_info, 13062306a36Sopenharmony_ci unsigned long *value_bitmap); 13162306a36Sopenharmony_ciint gpiod_set_array_value_complex(bool raw, bool can_sleep, 13262306a36Sopenharmony_ci unsigned int array_size, 13362306a36Sopenharmony_ci struct gpio_desc **desc_array, 13462306a36Sopenharmony_ci struct gpio_array *array_info, 13562306a36Sopenharmony_ci unsigned long *value_bitmap); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciextern spinlock_t gpio_lock; 13862306a36Sopenharmony_ciextern struct list_head gpio_devices; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_civoid gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci/** 14362306a36Sopenharmony_ci * struct gpio_desc - Opaque descriptor for a GPIO 14462306a36Sopenharmony_ci * 14562306a36Sopenharmony_ci * @gdev: Pointer to the parent GPIO device 14662306a36Sopenharmony_ci * @flags: Binary descriptor flags 14762306a36Sopenharmony_ci * @label: Name of the consumer 14862306a36Sopenharmony_ci * @name: Line name 14962306a36Sopenharmony_ci * @hog: Pointer to the device node that hogs this line (if any) 15062306a36Sopenharmony_ci * @debounce_period_us: Debounce period in microseconds 15162306a36Sopenharmony_ci * 15262306a36Sopenharmony_ci * These are obtained using gpiod_get() and are preferable to the old 15362306a36Sopenharmony_ci * integer-based handles. 15462306a36Sopenharmony_ci * 15562306a36Sopenharmony_ci * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be 15662306a36Sopenharmony_ci * valid until the GPIO is released. 15762306a36Sopenharmony_ci */ 15862306a36Sopenharmony_cistruct gpio_desc { 15962306a36Sopenharmony_ci struct gpio_device *gdev; 16062306a36Sopenharmony_ci unsigned long flags; 16162306a36Sopenharmony_ci/* flag symbols are bit numbers */ 16262306a36Sopenharmony_ci#define FLAG_REQUESTED 0 16362306a36Sopenharmony_ci#define FLAG_IS_OUT 1 16462306a36Sopenharmony_ci#define FLAG_EXPORT 2 /* protected by sysfs_lock */ 16562306a36Sopenharmony_ci#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 16662306a36Sopenharmony_ci#define FLAG_ACTIVE_LOW 6 /* value has active low */ 16762306a36Sopenharmony_ci#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 16862306a36Sopenharmony_ci#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 16962306a36Sopenharmony_ci#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 17062306a36Sopenharmony_ci#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 17162306a36Sopenharmony_ci#define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 17262306a36Sopenharmony_ci#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 17362306a36Sopenharmony_ci#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 17462306a36Sopenharmony_ci#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 17562306a36Sopenharmony_ci#define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ 17662306a36Sopenharmony_ci#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ 17762306a36Sopenharmony_ci#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ 17862306a36Sopenharmony_ci#define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */ 17962306a36Sopenharmony_ci#define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */ 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci /* Connection label */ 18262306a36Sopenharmony_ci const char *label; 18362306a36Sopenharmony_ci /* Name of the GPIO */ 18462306a36Sopenharmony_ci const char *name; 18562306a36Sopenharmony_ci#ifdef CONFIG_OF_DYNAMIC 18662306a36Sopenharmony_ci struct device_node *hog; 18762306a36Sopenharmony_ci#endif 18862306a36Sopenharmony_ci#ifdef CONFIG_GPIO_CDEV 18962306a36Sopenharmony_ci /* debounce period in microseconds */ 19062306a36Sopenharmony_ci unsigned int debounce_period_us; 19162306a36Sopenharmony_ci#endif 19262306a36Sopenharmony_ci}; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci#define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ciint gpiod_request(struct gpio_desc *desc, const char *label); 19762306a36Sopenharmony_civoid gpiod_free(struct gpio_desc *desc); 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_cistatic inline int gpiod_request_user(struct gpio_desc *desc, const char *label) 20062306a36Sopenharmony_ci{ 20162306a36Sopenharmony_ci int ret; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci ret = gpiod_request(desc, label); 20462306a36Sopenharmony_ci if (ret == -EPROBE_DEFER) 20562306a36Sopenharmony_ci ret = -ENODEV; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci return ret; 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_cistruct gpio_desc *gpiod_find_and_request(struct device *consumer, 21162306a36Sopenharmony_ci struct fwnode_handle *fwnode, 21262306a36Sopenharmony_ci const char *con_id, 21362306a36Sopenharmony_ci unsigned int idx, 21462306a36Sopenharmony_ci enum gpiod_flags flags, 21562306a36Sopenharmony_ci const char *label, 21662306a36Sopenharmony_ci bool platform_lookup_allowed); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ciint gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 21962306a36Sopenharmony_ci unsigned long lflags, enum gpiod_flags dflags); 22062306a36Sopenharmony_ciint gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); 22162306a36Sopenharmony_ciint gpiod_hog(struct gpio_desc *desc, const char *name, 22262306a36Sopenharmony_ci unsigned long lflags, enum gpiod_flags dflags); 22362306a36Sopenharmony_ciint gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/* 22662306a36Sopenharmony_ci * Return the GPIO number of the passed descriptor relative to its chip 22762306a36Sopenharmony_ci */ 22862306a36Sopenharmony_cistatic inline int gpio_chip_hwgpio(const struct gpio_desc *desc) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci return desc - &desc->gdev->descs[0]; 23162306a36Sopenharmony_ci} 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci/* With descriptor prefix */ 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci#define gpiod_emerg(desc, fmt, ...) \ 23662306a36Sopenharmony_ci pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 23762306a36Sopenharmony_ci ##__VA_ARGS__) 23862306a36Sopenharmony_ci#define gpiod_crit(desc, fmt, ...) \ 23962306a36Sopenharmony_ci pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 24062306a36Sopenharmony_ci ##__VA_ARGS__) 24162306a36Sopenharmony_ci#define gpiod_err(desc, fmt, ...) \ 24262306a36Sopenharmony_ci pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 24362306a36Sopenharmony_ci ##__VA_ARGS__) 24462306a36Sopenharmony_ci#define gpiod_warn(desc, fmt, ...) \ 24562306a36Sopenharmony_ci pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 24662306a36Sopenharmony_ci ##__VA_ARGS__) 24762306a36Sopenharmony_ci#define gpiod_info(desc, fmt, ...) \ 24862306a36Sopenharmony_ci pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 24962306a36Sopenharmony_ci ##__VA_ARGS__) 25062306a36Sopenharmony_ci#define gpiod_dbg(desc, fmt, ...) \ 25162306a36Sopenharmony_ci pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 25262306a36Sopenharmony_ci ##__VA_ARGS__) 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci/* With chip prefix */ 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci#define chip_emerg(gc, fmt, ...) \ 25762306a36Sopenharmony_ci dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 25862306a36Sopenharmony_ci#define chip_crit(gc, fmt, ...) \ 25962306a36Sopenharmony_ci dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 26062306a36Sopenharmony_ci#define chip_err(gc, fmt, ...) \ 26162306a36Sopenharmony_ci dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 26262306a36Sopenharmony_ci#define chip_warn(gc, fmt, ...) \ 26362306a36Sopenharmony_ci dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 26462306a36Sopenharmony_ci#define chip_info(gc, fmt, ...) \ 26562306a36Sopenharmony_ci dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 26662306a36Sopenharmony_ci#define chip_dbg(gc, fmt, ...) \ 26762306a36Sopenharmony_ci dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci#endif /* GPIOLIB_H */ 270