1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 279a9becdSAlexandre Courbot #ifndef __LINUX_GPIO_DRIVER_H 379a9becdSAlexandre Courbot #define __LINUX_GPIO_DRIVER_H 479a9becdSAlexandre Courbot 5ff2b1359SLinus Walleij #include <linux/device.h> 614250520SLinus Walleij #include <linux/irq.h> 714250520SLinus Walleij #include <linux/irqchip/chained_irq.h> 814250520SLinus Walleij #include <linux/irqdomain.h> 9a0a8bcf4SGrygorii Strashko #include <linux/lockdep.h> 102956b5d9SMika Westerberg #include <linux/pinctrl/pinconf-generic.h> 1108a149c4SAndy Shevchenko #include <linux/pinctrl/pinctrl.h> 1285ebb1a6SAndy Shevchenko #include <linux/property.h> 1385ebb1a6SAndy Shevchenko #include <linux/types.h> 1479a9becdSAlexandre Courbot 1591a29af4SMarc Zyngier #include <asm/msi.h> 1691a29af4SMarc Zyngier 1779a9becdSAlexandre Courbot struct gpio_desc; 18c9a9972bSAlexandre Courbot struct of_phandle_args; 19c9a9972bSAlexandre Courbot struct device_node; 20f3ed0b66SStephen Rothwell struct seq_file; 21ff2b1359SLinus Walleij struct gpio_device; 22d47529b2SPaul Gortmaker struct module; 2321abf103SLinus Walleij enum gpiod_flags; 245923ea6cSLinus Walleij enum gpio_lookup_flags; 2579a9becdSAlexandre Courbot 26fdd61a01SLinus Walleij struct gpio_chip; 27fdd61a01SLinus Walleij 2891a29af4SMarc Zyngier union gpio_irq_fwspec { 2991a29af4SMarc Zyngier struct irq_fwspec fwspec; 3013e7accbSThomas Gleixner #ifdef CONFIG_GENERIC_MSI_IRQ 3191a29af4SMarc Zyngier msi_alloc_info_t msiinfo; 3291a29af4SMarc Zyngier #endif 3391a29af4SMarc Zyngier }; 3491a29af4SMarc Zyngier 359208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_IN 1 369208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_OUT 0 379208b1e7SMatti Vaittinen 38c44eafd7SThierry Reding /** 39c44eafd7SThierry Reding * struct gpio_irq_chip - GPIO interrupt controller 40c44eafd7SThierry Reding */ 41c44eafd7SThierry Reding struct gpio_irq_chip { 42c44eafd7SThierry Reding /** 43da80ff81SThierry Reding * @chip: 44da80ff81SThierry Reding * 45da80ff81SThierry Reding * GPIO IRQ chip implementation, provided by GPIO driver. 46da80ff81SThierry Reding */ 47da80ff81SThierry Reding struct irq_chip *chip; 48da80ff81SThierry Reding 49da80ff81SThierry Reding /** 50f0fbe7bcSThierry Reding * @domain: 51f0fbe7bcSThierry Reding * 52f0fbe7bcSThierry Reding * Interrupt translation domain; responsible for mapping between GPIO 53f0fbe7bcSThierry Reding * hwirq number and Linux IRQ number. 54f0fbe7bcSThierry Reding */ 55f0fbe7bcSThierry Reding struct irq_domain *domain; 56f0fbe7bcSThierry Reding 57f0fbe7bcSThierry Reding /** 58c44eafd7SThierry Reding * @domain_ops: 59c44eafd7SThierry Reding * 60c44eafd7SThierry Reding * Table of interrupt domain operations for this IRQ chip. 61c44eafd7SThierry Reding */ 62c44eafd7SThierry Reding const struct irq_domain_ops *domain_ops; 63c44eafd7SThierry Reding 64fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 65fdd61a01SLinus Walleij /** 66fdd61a01SLinus Walleij * @fwnode: 67fdd61a01SLinus Walleij * 68fdd61a01SLinus Walleij * Firmware node corresponding to this gpiochip/irqchip, necessary 69fdd61a01SLinus Walleij * for hierarchical irqdomain support. 70fdd61a01SLinus Walleij */ 71fdd61a01SLinus Walleij struct fwnode_handle *fwnode; 72fdd61a01SLinus Walleij 73fdd61a01SLinus Walleij /** 74fdd61a01SLinus Walleij * @parent_domain: 75fdd61a01SLinus Walleij * 76fdd61a01SLinus Walleij * If non-NULL, will be set as the parent of this GPIO interrupt 77fdd61a01SLinus Walleij * controller's IRQ domain to establish a hierarchical interrupt 78fdd61a01SLinus Walleij * domain. The presence of this will activate the hierarchical 79fdd61a01SLinus Walleij * interrupt support. 80fdd61a01SLinus Walleij */ 81fdd61a01SLinus Walleij struct irq_domain *parent_domain; 82fdd61a01SLinus Walleij 83fdd61a01SLinus Walleij /** 84fdd61a01SLinus Walleij * @child_to_parent_hwirq: 85fdd61a01SLinus Walleij * 86fdd61a01SLinus Walleij * This callback translates a child hardware IRQ offset to a parent 87fdd61a01SLinus Walleij * hardware IRQ offset on a hierarchical interrupt chip. The child 88fdd61a01SLinus Walleij * hardware IRQs correspond to the GPIO index 0..ngpio-1 (see the 89fdd61a01SLinus Walleij * ngpio field of struct gpio_chip) and the corresponding parent 90fdd61a01SLinus Walleij * hardware IRQ and type (such as IRQ_TYPE_*) shall be returned by 91fdd61a01SLinus Walleij * the driver. The driver can calculate this from an offset or using 92fdd61a01SLinus Walleij * a lookup table or whatever method is best for this chip. Return 93fdd61a01SLinus Walleij * 0 on successful translation in the driver. 94fdd61a01SLinus Walleij * 95fdd61a01SLinus Walleij * If some ranges of hardware IRQs do not have a corresponding parent 96fdd61a01SLinus Walleij * HWIRQ, return -EINVAL, but also make sure to fill in @valid_mask and 97fdd61a01SLinus Walleij * @need_valid_mask to make these GPIO lines unavailable for 98fdd61a01SLinus Walleij * translation. 99fdd61a01SLinus Walleij */ 100a0b66a73SLinus Walleij int (*child_to_parent_hwirq)(struct gpio_chip *gc, 101fdd61a01SLinus Walleij unsigned int child_hwirq, 102fdd61a01SLinus Walleij unsigned int child_type, 103fdd61a01SLinus Walleij unsigned int *parent_hwirq, 104fdd61a01SLinus Walleij unsigned int *parent_type); 105fdd61a01SLinus Walleij 106fdd61a01SLinus Walleij /** 10724258761SKevin Hao * @populate_parent_alloc_arg : 108fdd61a01SLinus Walleij * 10924258761SKevin Hao * This optional callback allocates and populates the specific struct 11024258761SKevin Hao * for the parent's IRQ domain. If this is not specified, then 111fdd61a01SLinus Walleij * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell 112fdd61a01SLinus Walleij * variant named &gpiochip_populate_parent_fwspec_fourcell is also 113fdd61a01SLinus Walleij * available. 114fdd61a01SLinus Walleij */ 11591a29af4SMarc Zyngier int (*populate_parent_alloc_arg)(struct gpio_chip *gc, 11691a29af4SMarc Zyngier union gpio_irq_fwspec *fwspec, 117fdd61a01SLinus Walleij unsigned int parent_hwirq, 118fdd61a01SLinus Walleij unsigned int parent_type); 119fdd61a01SLinus Walleij 120fdd61a01SLinus Walleij /** 121fdd61a01SLinus Walleij * @child_offset_to_irq: 122fdd61a01SLinus Walleij * 123fdd61a01SLinus Walleij * This optional callback is used to translate the child's GPIO line 124fdd61a01SLinus Walleij * offset on the GPIO chip to an IRQ number for the GPIO to_irq() 125fdd61a01SLinus Walleij * callback. If this is not specified, then a default callback will be 126fdd61a01SLinus Walleij * provided that returns the line offset. 127fdd61a01SLinus Walleij */ 128a0b66a73SLinus Walleij unsigned int (*child_offset_to_irq)(struct gpio_chip *gc, 129fdd61a01SLinus Walleij unsigned int pin); 130fdd61a01SLinus Walleij 131fdd61a01SLinus Walleij /** 132fdd61a01SLinus Walleij * @child_irq_domain_ops: 133fdd61a01SLinus Walleij * 134fdd61a01SLinus Walleij * The IRQ domain operations that will be used for this GPIO IRQ 135fdd61a01SLinus Walleij * chip. If no operations are provided, then default callbacks will 136fdd61a01SLinus Walleij * be populated to setup the IRQ hierarchy. Some drivers need to 137fdd61a01SLinus Walleij * supply their own translate function. 138fdd61a01SLinus Walleij */ 139fdd61a01SLinus Walleij struct irq_domain_ops child_irq_domain_ops; 140fdd61a01SLinus Walleij #endif 141fdd61a01SLinus Walleij 142c44eafd7SThierry Reding /** 143c7a0aa59SThierry Reding * @handler: 144c7a0aa59SThierry Reding * 145c7a0aa59SThierry Reding * The IRQ handler to use (often a predefined IRQ core function) for 146c7a0aa59SThierry Reding * GPIO IRQs, provided by GPIO driver. 147c7a0aa59SThierry Reding */ 148c7a0aa59SThierry Reding irq_flow_handler_t handler; 149c7a0aa59SThierry Reding 150c7a0aa59SThierry Reding /** 1513634eeb0SThierry Reding * @default_type: 1523634eeb0SThierry Reding * 1533634eeb0SThierry Reding * Default IRQ triggering type applied during GPIO driver 1543634eeb0SThierry Reding * initialization, provided by GPIO driver. 1553634eeb0SThierry Reding */ 1563634eeb0SThierry Reding unsigned int default_type; 1573634eeb0SThierry Reding 1583634eeb0SThierry Reding /** 159ca9df053SThierry Reding * @lock_key: 160ca9df053SThierry Reding * 16102ad0437SRandy Dunlap * Per GPIO IRQ chip lockdep class for IRQ lock. 162ca9df053SThierry Reding */ 163ca9df053SThierry Reding struct lock_class_key *lock_key; 16402ad0437SRandy Dunlap 16502ad0437SRandy Dunlap /** 16602ad0437SRandy Dunlap * @request_key: 16702ad0437SRandy Dunlap * 16802ad0437SRandy Dunlap * Per GPIO IRQ chip lockdep class for IRQ request. 16902ad0437SRandy Dunlap */ 17039c3fd58SAndrew Lunn struct lock_class_key *request_key; 171ca9df053SThierry Reding 172ca9df053SThierry Reding /** 173c44eafd7SThierry Reding * @parent_handler: 174c44eafd7SThierry Reding * 175c44eafd7SThierry Reding * The interrupt handler for the GPIO chip's parent interrupts, may be 176c44eafd7SThierry Reding * NULL if the parent interrupts are nested rather than cascaded. 177c44eafd7SThierry Reding */ 178c44eafd7SThierry Reding irq_flow_handler_t parent_handler; 179c44eafd7SThierry Reding 180c7e1c443SAkira Yokosawa union { 181c44eafd7SThierry Reding /** 182c44eafd7SThierry Reding * @parent_handler_data: 18348ec13d3SJoey Gouly * 184c7e1c443SAkira Yokosawa * If @per_parent_data is false, @parent_handler_data is a 185c7e1c443SAkira Yokosawa * single pointer used as the data associated with every 186c7e1c443SAkira Yokosawa * parent interrupt. 187c7e1c443SAkira Yokosawa */ 188c7e1c443SAkira Yokosawa void *parent_handler_data; 189c7e1c443SAkira Yokosawa 190c7e1c443SAkira Yokosawa /** 191cfe6807dSMarc Zyngier * @parent_handler_data_array: 192c44eafd7SThierry Reding * 19348ec13d3SJoey Gouly * If @per_parent_data is true, @parent_handler_data_array is 19448ec13d3SJoey Gouly * an array of @num_parents pointers, and is used to associate 19548ec13d3SJoey Gouly * different data for each parent. This cannot be NULL if 19648ec13d3SJoey Gouly * @per_parent_data is true. 197c44eafd7SThierry Reding */ 198cfe6807dSMarc Zyngier void **parent_handler_data_array; 199cfe6807dSMarc Zyngier }; 20039e5f096SThierry Reding 20139e5f096SThierry Reding /** 20239e5f096SThierry Reding * @num_parents: 20339e5f096SThierry Reding * 20439e5f096SThierry Reding * The number of interrupt parents of a GPIO chip. 20539e5f096SThierry Reding */ 20639e5f096SThierry Reding unsigned int num_parents; 20739e5f096SThierry Reding 20839e5f096SThierry Reding /** 20939e5f096SThierry Reding * @parents: 21039e5f096SThierry Reding * 21139e5f096SThierry Reding * A list of interrupt parents of a GPIO chip. This is owned by the 21239e5f096SThierry Reding * driver, so the core will only reference this list, not modify it. 21339e5f096SThierry Reding */ 21439e5f096SThierry Reding unsigned int *parents; 215dc6bafeeSThierry Reding 216dc6bafeeSThierry Reding /** 217e0d89728SThierry Reding * @map: 218e0d89728SThierry Reding * 219e0d89728SThierry Reding * A list of interrupt parents for each line of a GPIO chip. 220e0d89728SThierry Reding */ 221e0d89728SThierry Reding unsigned int *map; 222e0d89728SThierry Reding 223e0d89728SThierry Reding /** 22460ed54caSThierry Reding * @threaded: 225dc6bafeeSThierry Reding * 22660ed54caSThierry Reding * True if set the interrupt handling uses nested threads. 227dc6bafeeSThierry Reding */ 22860ed54caSThierry Reding bool threaded; 229dc7b0387SThierry Reding 230dc7b0387SThierry Reding /** 231cfe6807dSMarc Zyngier * @per_parent_data: 232cfe6807dSMarc Zyngier * 233cfe6807dSMarc Zyngier * True if parent_handler_data_array describes a @num_parents 234cfe6807dSMarc Zyngier * sized array to be used as parent data. 235cfe6807dSMarc Zyngier */ 236cfe6807dSMarc Zyngier bool per_parent_data; 237cfe6807dSMarc Zyngier 238cfe6807dSMarc Zyngier /** 2395467801fSShreeya Patel * @initialized: 2405467801fSShreeya Patel * 2415467801fSShreeya Patel * Flag to track GPIO chip irq member's initialization. 2425467801fSShreeya Patel * This flag will make sure GPIO chip irq members are not used 2435467801fSShreeya Patel * before they are initialized. 2445467801fSShreeya Patel */ 2455467801fSShreeya Patel bool initialized; 2465467801fSShreeya Patel 2475467801fSShreeya Patel /** 2489411e3aaSAndy Shevchenko * @init_hw: optional routine to initialize hardware before 2499411e3aaSAndy Shevchenko * an IRQ chip will be added. This is quite useful when 2509411e3aaSAndy Shevchenko * a particular driver wants to clear IRQ related registers 2519411e3aaSAndy Shevchenko * in order to avoid undesired events. 2529411e3aaSAndy Shevchenko */ 253a0b66a73SLinus Walleij int (*init_hw)(struct gpio_chip *gc); 2549411e3aaSAndy Shevchenko 2559411e3aaSAndy Shevchenko /** 2565fbe5b58SLinus Walleij * @init_valid_mask: optional routine to initialize @valid_mask, to be 2575fbe5b58SLinus Walleij * used if not all GPIO lines are valid interrupts. Sometimes some 2585fbe5b58SLinus Walleij * lines just cannot fire interrupts, and this routine, when defined, 2595fbe5b58SLinus Walleij * is passed a bitmap in "valid_mask" and it will have ngpios 2605fbe5b58SLinus Walleij * bits from 0..(ngpios-1) set to "1" as in valid. The callback can 2615fbe5b58SLinus Walleij * then directly set some bits to "0" if they cannot be used for 2625fbe5b58SLinus Walleij * interrupts. 263dc7b0387SThierry Reding */ 264a0b66a73SLinus Walleij void (*init_valid_mask)(struct gpio_chip *gc, 2655fbe5b58SLinus Walleij unsigned long *valid_mask, 2665fbe5b58SLinus Walleij unsigned int ngpios); 267dc7b0387SThierry Reding 268dc7b0387SThierry Reding /** 269dc7b0387SThierry Reding * @valid_mask: 270dc7b0387SThierry Reding * 2712d93018fSRandy Dunlap * If not %NULL, holds bitmask of GPIOs which are valid to be included 272dc7b0387SThierry Reding * in IRQ domain of the chip. 273dc7b0387SThierry Reding */ 274dc7b0387SThierry Reding unsigned long *valid_mask; 2758302cf58SThierry Reding 2768302cf58SThierry Reding /** 2778302cf58SThierry Reding * @first: 2788302cf58SThierry Reding * 2798302cf58SThierry Reding * Required for static IRQ allocation. If set, irq_domain_add_simple() 2808302cf58SThierry Reding * will allocate and map all IRQs during initialization. 2818302cf58SThierry Reding */ 2828302cf58SThierry Reding unsigned int first; 283461c1a7dSHans Verkuil 284461c1a7dSHans Verkuil /** 285461c1a7dSHans Verkuil * @irq_enable: 286461c1a7dSHans Verkuil * 287461c1a7dSHans Verkuil * Store old irq_chip irq_enable callback 288461c1a7dSHans Verkuil */ 289461c1a7dSHans Verkuil void (*irq_enable)(struct irq_data *data); 290461c1a7dSHans Verkuil 291461c1a7dSHans Verkuil /** 292461c1a7dSHans Verkuil * @irq_disable: 293461c1a7dSHans Verkuil * 294461c1a7dSHans Verkuil * Store old irq_chip irq_disable callback 295461c1a7dSHans Verkuil */ 296461c1a7dSHans Verkuil void (*irq_disable)(struct irq_data *data); 297a8173820SMaulik Shah /** 298a8173820SMaulik Shah * @irq_unmask: 299a8173820SMaulik Shah * 300a8173820SMaulik Shah * Store old irq_chip irq_unmask callback 301a8173820SMaulik Shah */ 302a8173820SMaulik Shah void (*irq_unmask)(struct irq_data *data); 303a8173820SMaulik Shah 304a8173820SMaulik Shah /** 305a8173820SMaulik Shah * @irq_mask: 306a8173820SMaulik Shah * 307a8173820SMaulik Shah * Store old irq_chip irq_mask callback 308a8173820SMaulik Shah */ 309a8173820SMaulik Shah void (*irq_mask)(struct irq_data *data); 310c44eafd7SThierry Reding }; 311c44eafd7SThierry Reding 31279a9becdSAlexandre Courbot /** 31379a9becdSAlexandre Courbot * struct gpio_chip - abstract a GPIO controller 314df4878e9SLinus Walleij * @label: a functional name for the GPIO device, such as a part 315df4878e9SLinus Walleij * number or the name of the SoC IP-block implementing it. 316ff2b1359SLinus Walleij * @gpiodev: the internal state holder, opaque struct 31758383c78SLinus Walleij * @parent: optional parent device providing the GPIOs 318990f6756SBartosz Golaszewski * @fwnode: optional fwnode providing this controller's properties 31979a9becdSAlexandre Courbot * @owner: helps prevent removal of modules exporting active GPIOs 32079a9becdSAlexandre Courbot * @request: optional hook for chip-specific activation, such as 32179a9becdSAlexandre Courbot * enabling module power and clock; may sleep 32279a9becdSAlexandre Courbot * @free: optional hook for chip-specific deactivation, such as 32379a9becdSAlexandre Courbot * disabling module power and clock; may sleep 32479a9becdSAlexandre Courbot * @get_direction: returns direction for signal "offset", 0=out, 1=in, 32536b52154SDouglas Anderson * (same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN), 32636b52154SDouglas Anderson * or negative error. It is recommended to always implement this 32736b52154SDouglas Anderson * function, even on input-only or output-only gpio chips. 32879a9becdSAlexandre Courbot * @direction_input: configures signal "offset" as input, or returns error 329e48d194dSLinus Walleij * This can be omitted on input-only or output-only gpio chips. 33079a9becdSAlexandre Courbot * @direction_output: configures signal "offset" as output, or returns error 331e48d194dSLinus Walleij * This can be omitted on input-only or output-only gpio chips. 33260befd2eSVladimir Zapolskiy * @get: returns value for signal "offset", 0=low, 1=high, or negative error 333eec1d566SLukas Wunner * @get_multiple: reads values for multiple signals defined by "mask" and 334eec1d566SLukas Wunner * stores them in "bits", returns 0 on success or negative error 33579a9becdSAlexandre Courbot * @set: assigns output value for signal "offset" 3365f424243SRojhalat Ibrahim * @set_multiple: assigns output values for multiple signals defined by "mask" 3372956b5d9SMika Westerberg * @set_config: optional hook for all kinds of settings. Uses the same 3382956b5d9SMika Westerberg * packed config format as generic pinconf. 339*9a7dcaefSAndy Shevchenko * @to_irq: optional hook supporting non-static gpiod_to_irq() mappings; 34079a9becdSAlexandre Courbot * implementation may not sleep 34179a9becdSAlexandre Courbot * @dbg_show: optional routine to show contents in debugfs; default code 34279a9becdSAlexandre Courbot * will be used when this is omitted, but custom code can show extra 34379a9becdSAlexandre Courbot * state (such as pullup/pulldown configuration). 344f99d479bSGeert Uytterhoeven * @init_valid_mask: optional routine to initialize @valid_mask, to be used if 345f99d479bSGeert Uytterhoeven * not all GPIOs are valid. 346b056ca1cSAndy Shevchenko * @add_pin_ranges: optional routine to initialize pin ranges, to be used when 347b056ca1cSAndy Shevchenko * requires special mapping of the pins that provides GPIO functionality. 348b056ca1cSAndy Shevchenko * It is called after adding GPIO chip and before adding IRQ chip. 34942112dd7SDipen Patel * @en_hw_timestamp: Dependent on GPIO chip, an optional routine to 35042112dd7SDipen Patel * enable hardware timestamp. 35142112dd7SDipen Patel * @dis_hw_timestamp: Dependent on GPIO chip, an optional routine to 35242112dd7SDipen Patel * disable hardware timestamp. 353af6c235dSLinus Walleij * @base: identifies the first GPIO number handled by this chip; 354af6c235dSLinus Walleij * or, if negative during registration, requests dynamic ID allocation. 355af6c235dSLinus Walleij * DEPRECATION: providing anything non-negative and nailing the base 35630bb6fb3SGeert Uytterhoeven * offset of GPIO chips is deprecated. Please pass -1 as base to 357af6c235dSLinus Walleij * let gpiolib select the chip base in all possible cases. We want to 358af6c235dSLinus Walleij * get rid of the static GPIO number space in the long run. 35979a9becdSAlexandre Courbot * @ngpio: the number of GPIOs handled by this controller; the last GPIO 36079a9becdSAlexandre Courbot * handled is (base + ngpio - 1). 3614e804c39SSergio Paracuellos * @offset: when multiple gpio chips belong to the same device this 3624e804c39SSergio Paracuellos * can be used as offset within the device so friendly names can 3634e804c39SSergio Paracuellos * be properly assigned. 36479a9becdSAlexandre Courbot * @names: if set, must be an array of strings to use as alternative 36579a9becdSAlexandre Courbot * names for the GPIOs in this chip. Any entry in the array 36679a9becdSAlexandre Courbot * may be NULL if there is no alias for the GPIO, however the 36779a9becdSAlexandre Courbot * array must be @ngpio entries long. A name can include a single printk 36879a9becdSAlexandre Courbot * format specifier for an unsigned int. It is substituted by the actual 36979a9becdSAlexandre Courbot * number of the gpio. 3709fb1f39eSLinus Walleij * @can_sleep: flag must be set iff get()/set() methods sleep, as they 3711c8732bbSLinus Walleij * must while accessing GPIO expander chips over I2C or SPI. This 3721c8732bbSLinus Walleij * implies that if the chip supports IRQs, these IRQs need to be threaded 3731c8732bbSLinus Walleij * as the chip access may sleep when e.g. reading out the IRQ status 3741c8732bbSLinus Walleij * registers. 3750f4630f3SLinus Walleij * @read_reg: reader function for generic GPIO 3760f4630f3SLinus Walleij * @write_reg: writer function for generic GPIO 37724efd94bSLinus Walleij * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing 37824efd94bSLinus Walleij * line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the 37924efd94bSLinus Walleij * generic GPIO core. It is for internal housekeeping only. 3800f4630f3SLinus Walleij * @reg_dat: data (in) register for generic GPIO 3810f4630f3SLinus Walleij * @reg_set: output set register (out=high) for generic GPIO 38208bcd3edSAnthony Best * @reg_clr: output clear register (out=low) for generic GPIO 383f69e00bdSLinus Walleij * @reg_dir_out: direction out setting register for generic GPIO 384f69e00bdSLinus Walleij * @reg_dir_in: direction in setting register for generic GPIO 385f69e00bdSLinus Walleij * @bgpio_dir_unreadable: indicates that the direction register(s) cannot 386f69e00bdSLinus Walleij * be read and we need to rely on out internal state tracking. 3870f4630f3SLinus Walleij * @bgpio_bits: number of register bits used for a generic GPIO i.e. 3880f4630f3SLinus Walleij * <register width> * 8 3890f4630f3SLinus Walleij * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep 3900f4630f3SLinus Walleij * shadowed and real data registers writes together. 3910f4630f3SLinus Walleij * @bgpio_data: shadowed data register for generic GPIO to clear/set bits 3920f4630f3SLinus Walleij * safely. 3930f4630f3SLinus Walleij * @bgpio_dir: shadowed direction register for generic GPIO to clear/set 394f69e00bdSLinus Walleij * direction safely. A "1" in this word means the line is set as 395f69e00bdSLinus Walleij * output. 39679a9becdSAlexandre Courbot * 39779a9becdSAlexandre Courbot * A gpio_chip can help platforms abstract various sources of GPIOs so 3982d93018fSRandy Dunlap * they can all be accessed through a common programming interface. 39979a9becdSAlexandre Courbot * Example sources would be SOC controllers, FPGAs, multifunction 40079a9becdSAlexandre Courbot * chips, dedicated GPIO expanders, and so on. 40179a9becdSAlexandre Courbot * 40279a9becdSAlexandre Courbot * Each chip controls a number of signals, identified in method calls 40379a9becdSAlexandre Courbot * by "offset" values in the range 0..(@ngpio - 1). When those signals 40479a9becdSAlexandre Courbot * are referenced through calls like gpio_get_value(gpio), the offset 40579a9becdSAlexandre Courbot * is calculated by subtracting @base from the gpio number. 40679a9becdSAlexandre Courbot */ 40779a9becdSAlexandre Courbot struct gpio_chip { 40879a9becdSAlexandre Courbot const char *label; 409ff2b1359SLinus Walleij struct gpio_device *gpiodev; 41058383c78SLinus Walleij struct device *parent; 411990f6756SBartosz Golaszewski struct fwnode_handle *fwnode; 41279a9becdSAlexandre Courbot struct module *owner; 41379a9becdSAlexandre Courbot 414a0b66a73SLinus Walleij int (*request)(struct gpio_chip *gc, 4158d091012SDouglas Anderson unsigned int offset); 416a0b66a73SLinus Walleij void (*free)(struct gpio_chip *gc, 4178d091012SDouglas Anderson unsigned int offset); 418a0b66a73SLinus Walleij int (*get_direction)(struct gpio_chip *gc, 4198d091012SDouglas Anderson unsigned int offset); 420a0b66a73SLinus Walleij int (*direction_input)(struct gpio_chip *gc, 4218d091012SDouglas Anderson unsigned int offset); 422a0b66a73SLinus Walleij int (*direction_output)(struct gpio_chip *gc, 4238d091012SDouglas Anderson unsigned int offset, int value); 424a0b66a73SLinus Walleij int (*get)(struct gpio_chip *gc, 4258d091012SDouglas Anderson unsigned int offset); 426a0b66a73SLinus Walleij int (*get_multiple)(struct gpio_chip *gc, 427eec1d566SLukas Wunner unsigned long *mask, 428eec1d566SLukas Wunner unsigned long *bits); 429a0b66a73SLinus Walleij void (*set)(struct gpio_chip *gc, 4308d091012SDouglas Anderson unsigned int offset, int value); 431a0b66a73SLinus Walleij void (*set_multiple)(struct gpio_chip *gc, 4325f424243SRojhalat Ibrahim unsigned long *mask, 4335f424243SRojhalat Ibrahim unsigned long *bits); 434a0b66a73SLinus Walleij int (*set_config)(struct gpio_chip *gc, 4358d091012SDouglas Anderson unsigned int offset, 4362956b5d9SMika Westerberg unsigned long config); 437a0b66a73SLinus Walleij int (*to_irq)(struct gpio_chip *gc, 4388d091012SDouglas Anderson unsigned int offset); 43979a9becdSAlexandre Courbot 44079a9becdSAlexandre Courbot void (*dbg_show)(struct seq_file *s, 441a0b66a73SLinus Walleij struct gpio_chip *gc); 442f8ec92a9SRicardo Ribalda Delgado 443a0b66a73SLinus Walleij int (*init_valid_mask)(struct gpio_chip *gc, 444c9fc5affSLinus Walleij unsigned long *valid_mask, 445c9fc5affSLinus Walleij unsigned int ngpios); 446f8ec92a9SRicardo Ribalda Delgado 447a0b66a73SLinus Walleij int (*add_pin_ranges)(struct gpio_chip *gc); 448b056ca1cSAndy Shevchenko 44942112dd7SDipen Patel int (*en_hw_timestamp)(struct gpio_chip *gc, 45042112dd7SDipen Patel u32 offset, 45142112dd7SDipen Patel unsigned long flags); 45242112dd7SDipen Patel int (*dis_hw_timestamp)(struct gpio_chip *gc, 45342112dd7SDipen Patel u32 offset, 45442112dd7SDipen Patel unsigned long flags); 45579a9becdSAlexandre Courbot int base; 45679a9becdSAlexandre Courbot u16 ngpio; 4574e804c39SSergio Paracuellos u16 offset; 45879a9becdSAlexandre Courbot const char *const *names; 4599fb1f39eSLinus Walleij bool can_sleep; 46079a9becdSAlexandre Courbot 4610f4630f3SLinus Walleij #if IS_ENABLED(CONFIG_GPIO_GENERIC) 4620f4630f3SLinus Walleij unsigned long (*read_reg)(void __iomem *reg); 4630f4630f3SLinus Walleij void (*write_reg)(void __iomem *reg, unsigned long data); 46424efd94bSLinus Walleij bool be_bits; 4650f4630f3SLinus Walleij void __iomem *reg_dat; 4660f4630f3SLinus Walleij void __iomem *reg_set; 4670f4630f3SLinus Walleij void __iomem *reg_clr; 468f69e00bdSLinus Walleij void __iomem *reg_dir_out; 469f69e00bdSLinus Walleij void __iomem *reg_dir_in; 470f69e00bdSLinus Walleij bool bgpio_dir_unreadable; 4710f4630f3SLinus Walleij int bgpio_bits; 4723c938cc5SSchspa Shi raw_spinlock_t bgpio_lock; 4730f4630f3SLinus Walleij unsigned long bgpio_data; 4740f4630f3SLinus Walleij unsigned long bgpio_dir; 475f310f2efSEnrico Weigelt #endif /* CONFIG_GPIO_GENERIC */ 4760f4630f3SLinus Walleij 47714250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP 47814250520SLinus Walleij /* 4797d75a871SPaul Bolle * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib 48014250520SLinus Walleij * to handle IRQs for most practical cases. 48114250520SLinus Walleij */ 482c44eafd7SThierry Reding 483c44eafd7SThierry Reding /** 484c44eafd7SThierry Reding * @irq: 485c44eafd7SThierry Reding * 486c44eafd7SThierry Reding * Integrates interrupt chip functionality with the GPIO chip. Can be 487c44eafd7SThierry Reding * used to handle IRQs for most practical cases. 488c44eafd7SThierry Reding */ 489c44eafd7SThierry Reding struct gpio_irq_chip irq; 490f310f2efSEnrico Weigelt #endif /* CONFIG_GPIOLIB_IRQCHIP */ 49114250520SLinus Walleij 492726cb3baSStephen Boyd /** 493726cb3baSStephen Boyd * @valid_mask: 494726cb3baSStephen Boyd * 4952d93018fSRandy Dunlap * If not %NULL, holds bitmask of GPIOs which are valid to be used 496726cb3baSStephen Boyd * from the chip. 497726cb3baSStephen Boyd */ 498726cb3baSStephen Boyd unsigned long *valid_mask; 499726cb3baSStephen Boyd 50079a9becdSAlexandre Courbot #if defined(CONFIG_OF_GPIO) 50179a9becdSAlexandre Courbot /* 5022d93018fSRandy Dunlap * If CONFIG_OF_GPIO is enabled, then all GPIO controllers described in 5032d93018fSRandy Dunlap * the device tree automatically may have an OF translation 50479a9becdSAlexandre Courbot */ 50567049c50SThierry Reding 50667049c50SThierry Reding /** 50767049c50SThierry Reding * @of_gpio_n_cells: 50867049c50SThierry Reding * 50967049c50SThierry Reding * Number of cells used to form the GPIO specifier. 51067049c50SThierry Reding */ 511e3b445d7SThierry Reding unsigned int of_gpio_n_cells; 51267049c50SThierry Reding 51367049c50SThierry Reding /** 51467049c50SThierry Reding * @of_xlate: 51567049c50SThierry Reding * 51667049c50SThierry Reding * Callback to translate a device tree GPIO specifier into a chip- 51767049c50SThierry Reding * relative GPIO number and flags. 51867049c50SThierry Reding */ 51979a9becdSAlexandre Courbot int (*of_xlate)(struct gpio_chip *gc, 52079a9becdSAlexandre Courbot const struct of_phandle_args *gpiospec, u32 *flags); 5213550bba2SStefan Wahren 5223550bba2SStefan Wahren /** 5233550bba2SStefan Wahren * @of_gpio_ranges_fallback: 5243550bba2SStefan Wahren * 5253550bba2SStefan Wahren * Optional hook for the case that no gpio-ranges property is defined 5263550bba2SStefan Wahren * within the device tree node "np" (usually DT before introduction 5273550bba2SStefan Wahren * of gpio-ranges). So this callback is helpful to provide the 5283550bba2SStefan Wahren * necessary backward compatibility for the pin ranges. 5293550bba2SStefan Wahren */ 5303550bba2SStefan Wahren int (*of_gpio_ranges_fallback)(struct gpio_chip *gc, 5313550bba2SStefan Wahren struct device_node *np); 5323550bba2SStefan Wahren 533f310f2efSEnrico Weigelt #endif /* CONFIG_OF_GPIO */ 53479a9becdSAlexandre Courbot }; 53579a9becdSAlexandre Courbot 536a0b66a73SLinus Walleij extern const char *gpiochip_is_requested(struct gpio_chip *gc, 5378d091012SDouglas Anderson unsigned int offset); 53879a9becdSAlexandre Courbot 539b3337eb2SAndy Shevchenko /** 540b3337eb2SAndy Shevchenko * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range 541b3337eb2SAndy Shevchenko * @chip: the chip to query 542b3337eb2SAndy Shevchenko * @i: loop variable 543b3337eb2SAndy Shevchenko * @base: first GPIO in the range 544b3337eb2SAndy Shevchenko * @size: amount of GPIOs to check starting from @base 545b3337eb2SAndy Shevchenko * @label: label of current GPIO 546b3337eb2SAndy Shevchenko */ 547b3337eb2SAndy Shevchenko #define for_each_requested_gpio_in_range(chip, i, base, size, label) \ 548b3337eb2SAndy Shevchenko for (i = 0; i < size; i++) \ 549b3337eb2SAndy Shevchenko if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else 550b3337eb2SAndy Shevchenko 551b3337eb2SAndy Shevchenko /* Iterates over all requested GPIO of the given @chip */ 552b3337eb2SAndy Shevchenko #define for_each_requested_gpio(chip, i, label) \ 553b3337eb2SAndy Shevchenko for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label) 554b3337eb2SAndy Shevchenko 55579a9becdSAlexandre Courbot /* add/remove chips */ 556a0b66a73SLinus Walleij extern int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, 55739c3fd58SAndrew Lunn struct lock_class_key *lock_key, 55839c3fd58SAndrew Lunn struct lock_class_key *request_key); 559959bc7b2SThierry Reding 560959bc7b2SThierry Reding /** 561959bc7b2SThierry Reding * gpiochip_add_data() - register a gpio_chip 5628fc3ed3aSColton Lewis * @gc: the chip to register, with gc->base initialized 563959bc7b2SThierry Reding * @data: driver-private data associated with this chip 564959bc7b2SThierry Reding * 565959bc7b2SThierry Reding * Context: potentially before irqs will work 566959bc7b2SThierry Reding * 567959bc7b2SThierry Reding * When gpiochip_add_data() is called very early during boot, so that GPIOs 5688fc3ed3aSColton Lewis * can be freely used, the gc->parent device must be registered before 569959bc7b2SThierry Reding * the gpio framework's arch_initcall(). Otherwise sysfs initialization 570959bc7b2SThierry Reding * for GPIOs will fail rudely. 571959bc7b2SThierry Reding * 572959bc7b2SThierry Reding * gpiochip_add_data() must only be called after gpiolib initialization, 5732d93018fSRandy Dunlap * i.e. after core_initcall(). 574959bc7b2SThierry Reding * 5758fc3ed3aSColton Lewis * If gc->base is negative, this requests dynamic assignment of 576959bc7b2SThierry Reding * a range of valid GPIOs. 577959bc7b2SThierry Reding * 578959bc7b2SThierry Reding * Returns: 579959bc7b2SThierry Reding * A negative errno if the chip can't be registered, such as because the 5808fc3ed3aSColton Lewis * gc->base is invalid or already associated with a different chip. 581959bc7b2SThierry Reding * Otherwise it returns zero as a success code. 582959bc7b2SThierry Reding */ 583959bc7b2SThierry Reding #ifdef CONFIG_LOCKDEP 584a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) ({ \ 58539c3fd58SAndrew Lunn static struct lock_class_key lock_key; \ 58639c3fd58SAndrew Lunn static struct lock_class_key request_key; \ 587a0b66a73SLinus Walleij gpiochip_add_data_with_key(gc, data, &lock_key, \ 58839c3fd58SAndrew Lunn &request_key); \ 589959bc7b2SThierry Reding }) 5905f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) ({ \ 5915f402bb1SAhmad Fatoum static struct lock_class_key lock_key; \ 5925f402bb1SAhmad Fatoum static struct lock_class_key request_key; \ 5935f402bb1SAhmad Fatoum devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \ 5945f402bb1SAhmad Fatoum &request_key); \ 5955f402bb1SAhmad Fatoum }) 596959bc7b2SThierry Reding #else 597a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL) 5985f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) \ 5995f402bb1SAhmad Fatoum devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL) 600f310f2efSEnrico Weigelt #endif /* CONFIG_LOCKDEP */ 601959bc7b2SThierry Reding 602a0b66a73SLinus Walleij static inline int gpiochip_add(struct gpio_chip *gc) 603b08ea35aSLinus Walleij { 604a0b66a73SLinus Walleij return gpiochip_add_data(gc, NULL); 605b08ea35aSLinus Walleij } 606a0b66a73SLinus Walleij extern void gpiochip_remove(struct gpio_chip *gc); 6075f402bb1SAhmad Fatoum extern int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data, 6085f402bb1SAhmad Fatoum struct lock_class_key *lock_key, 6095f402bb1SAhmad Fatoum struct lock_class_key *request_key); 6100cf3292cSLaxman Dewangan 61179a9becdSAlexandre Courbot extern struct gpio_chip *gpiochip_find(void *data, 612a0b66a73SLinus Walleij int (*match)(struct gpio_chip *gc, void *data)); 61379a9becdSAlexandre Courbot 614a0b66a73SLinus Walleij bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset); 615a0b66a73SLinus Walleij int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset); 616a0b66a73SLinus Walleij void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset); 617a0b66a73SLinus Walleij void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset); 618a0b66a73SLinus Walleij void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset); 61979a9becdSAlexandre Courbot 620704f0875SMarc Zyngier /* irq_data versions of the above */ 621704f0875SMarc Zyngier int gpiochip_irq_reqres(struct irq_data *data); 622704f0875SMarc Zyngier void gpiochip_irq_relres(struct irq_data *data); 623704f0875SMarc Zyngier 62436b78aaeSMarc Zyngier /* Paste this in your irq_chip structure */ 62536b78aaeSMarc Zyngier #define GPIOCHIP_IRQ_RESOURCE_HELPERS \ 62636b78aaeSMarc Zyngier .irq_request_resources = gpiochip_irq_reqres, \ 62736b78aaeSMarc Zyngier .irq_release_resources = gpiochip_irq_relres 62836b78aaeSMarc Zyngier 62936b78aaeSMarc Zyngier static inline void gpio_irq_chip_set_chip(struct gpio_irq_chip *girq, 63036b78aaeSMarc Zyngier const struct irq_chip *chip) 63136b78aaeSMarc Zyngier { 63236b78aaeSMarc Zyngier /* Yes, dropping const is ugly, but it isn't like we have a choice */ 63336b78aaeSMarc Zyngier girq->chip = (struct irq_chip *)chip; 63436b78aaeSMarc Zyngier } 63536b78aaeSMarc Zyngier 636143b65d6SLinus Walleij /* Line status inquiry for drivers */ 637a0b66a73SLinus Walleij bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset); 638a0b66a73SLinus Walleij bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset); 639143b65d6SLinus Walleij 64005f479bfSCharles Keepax /* Sleep persistence inquiry for drivers */ 641a0b66a73SLinus Walleij bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset); 642a0b66a73SLinus Walleij bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset); 64305f479bfSCharles Keepax 644b08ea35aSLinus Walleij /* get driver data */ 645a0b66a73SLinus Walleij void *gpiochip_get_data(struct gpio_chip *gc); 646b08ea35aSLinus Walleij 6470f4630f3SLinus Walleij struct bgpio_pdata { 6480f4630f3SLinus Walleij const char *label; 6490f4630f3SLinus Walleij int base; 6500f4630f3SLinus Walleij int ngpio; 6510f4630f3SLinus Walleij }; 6520f4630f3SLinus Walleij 653fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 654fdd61a01SLinus Walleij 65591a29af4SMarc Zyngier int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc, 65691a29af4SMarc Zyngier union gpio_irq_fwspec *gfwspec, 657fdd61a01SLinus Walleij unsigned int parent_hwirq, 658fdd61a01SLinus Walleij unsigned int parent_type); 65991a29af4SMarc Zyngier int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc, 66091a29af4SMarc Zyngier union gpio_irq_fwspec *gfwspec, 661fdd61a01SLinus Walleij unsigned int parent_hwirq, 662fdd61a01SLinus Walleij unsigned int parent_type); 663fdd61a01SLinus Walleij 664fdd61a01SLinus Walleij #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 665fdd61a01SLinus Walleij 6660f4630f3SLinus Walleij int bgpio_init(struct gpio_chip *gc, struct device *dev, 6670f4630f3SLinus Walleij unsigned long sz, void __iomem *dat, void __iomem *set, 6680f4630f3SLinus Walleij void __iomem *clr, void __iomem *dirout, void __iomem *dirin, 6690f4630f3SLinus Walleij unsigned long flags); 6700f4630f3SLinus Walleij 6710f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN BIT(0) 6720f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_SET BIT(1) /* reg_set is unreadable */ 6730f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_DIR BIT(2) /* reg_dir is unreadable */ 6740f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3) 6750f4630f3SLinus Walleij #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */ 6760f4630f3SLinus Walleij #define BGPIOF_NO_OUTPUT BIT(5) /* only input */ 677d19d2de6SChuanhong Guo #define BGPIOF_NO_SET_ON_INPUT BIT(6) 6780f4630f3SLinus Walleij 6791b95b4ebSThierry Reding int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, 6801b95b4ebSThierry Reding irq_hw_number_t hwirq); 6811b95b4ebSThierry Reding void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq); 6821b95b4ebSThierry Reding 683ef74f70eSBrian Masney int gpiochip_irq_domain_activate(struct irq_domain *domain, 684ef74f70eSBrian Masney struct irq_data *data, bool reserve); 685ef74f70eSBrian Masney void gpiochip_irq_domain_deactivate(struct irq_domain *domain, 686ef74f70eSBrian Masney struct irq_data *data); 687ef74f70eSBrian Masney 688a0b66a73SLinus Walleij bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc, 68964ff2c8eSStephen Boyd unsigned int offset); 69064ff2c8eSStephen Boyd 6919c7d2469SÁlvaro Fernández Rojas #ifdef CONFIG_GPIOLIB_IRQCHIP 6926a45b0e2SMichael Walle int gpiochip_irqchip_add_domain(struct gpio_chip *gc, 6936a45b0e2SMichael Walle struct irq_domain *domain); 6949c7d2469SÁlvaro Fernández Rojas #else 6959c7d2469SÁlvaro Fernández Rojas static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc, 6969c7d2469SÁlvaro Fernández Rojas struct irq_domain *domain) 6979c7d2469SÁlvaro Fernández Rojas { 6989c7d2469SÁlvaro Fernández Rojas WARN_ON(1); 6999c7d2469SÁlvaro Fernández Rojas return -EINVAL; 7009c7d2469SÁlvaro Fernández Rojas } 7019c7d2469SÁlvaro Fernández Rojas #endif 7026a45b0e2SMichael Walle 7038d091012SDouglas Anderson int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset); 7048d091012SDouglas Anderson void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset); 7058d091012SDouglas Anderson int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, 7062956b5d9SMika Westerberg unsigned long config); 707c771c2f4SJonas Gorski 708964cb341SLinus Walleij /** 709964cb341SLinus Walleij * struct gpio_pin_range - pin range controlled by a gpio chip 710950d55f5SThierry Reding * @node: list for maintaining set of pin ranges, used internally 711964cb341SLinus Walleij * @pctldev: pinctrl device which handles corresponding pins 712964cb341SLinus Walleij * @range: actual range of pins controlled by a gpio controller 713964cb341SLinus Walleij */ 714964cb341SLinus Walleij struct gpio_pin_range { 715964cb341SLinus Walleij struct list_head node; 716964cb341SLinus Walleij struct pinctrl_dev *pctldev; 717964cb341SLinus Walleij struct pinctrl_gpio_range range; 718964cb341SLinus Walleij }; 719964cb341SLinus Walleij 7209091373aSMasahiro Yamada #ifdef CONFIG_PINCTRL 7219091373aSMasahiro Yamada 722a0b66a73SLinus Walleij int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name, 723964cb341SLinus Walleij unsigned int gpio_offset, unsigned int pin_offset, 724964cb341SLinus Walleij unsigned int npins); 725a0b66a73SLinus Walleij int gpiochip_add_pingroup_range(struct gpio_chip *gc, 726964cb341SLinus Walleij struct pinctrl_dev *pctldev, 727964cb341SLinus Walleij unsigned int gpio_offset, const char *pin_group); 728a0b66a73SLinus Walleij void gpiochip_remove_pin_ranges(struct gpio_chip *gc); 729964cb341SLinus Walleij 730f310f2efSEnrico Weigelt #else /* ! CONFIG_PINCTRL */ 731964cb341SLinus Walleij 732964cb341SLinus Walleij static inline int 733a0b66a73SLinus Walleij gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name, 734964cb341SLinus Walleij unsigned int gpio_offset, unsigned int pin_offset, 735964cb341SLinus Walleij unsigned int npins) 736964cb341SLinus Walleij { 737964cb341SLinus Walleij return 0; 738964cb341SLinus Walleij } 739964cb341SLinus Walleij static inline int 740a0b66a73SLinus Walleij gpiochip_add_pingroup_range(struct gpio_chip *gc, 741964cb341SLinus Walleij struct pinctrl_dev *pctldev, 742964cb341SLinus Walleij unsigned int gpio_offset, const char *pin_group) 743964cb341SLinus Walleij { 744964cb341SLinus Walleij return 0; 745964cb341SLinus Walleij } 746964cb341SLinus Walleij 747964cb341SLinus Walleij static inline void 748a0b66a73SLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *gc) 749964cb341SLinus Walleij { 750964cb341SLinus Walleij } 751964cb341SLinus Walleij 752964cb341SLinus Walleij #endif /* CONFIG_PINCTRL */ 753964cb341SLinus Walleij 754a0b66a73SLinus Walleij struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, 75506863620SBartosz Golaszewski unsigned int hwnum, 75621abf103SLinus Walleij const char *label, 7575923ea6cSLinus Walleij enum gpio_lookup_flags lflags, 7585923ea6cSLinus Walleij enum gpiod_flags dflags); 759f7d4ad98SGuenter Roeck void gpiochip_free_own_desc(struct gpio_desc *desc); 760f7d4ad98SGuenter Roeck 761ae0755b5SLinus Walleij #ifdef CONFIG_GPIOLIB 762ae0755b5SLinus Walleij 763c7663fa2SYueHaibing /* lock/unlock as IRQ */ 764a0b66a73SLinus Walleij int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset); 765a0b66a73SLinus Walleij void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset); 766c7663fa2SYueHaibing 7679091373aSMasahiro Yamada 7689091373aSMasahiro Yamada struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); 7699091373aSMasahiro Yamada 770bb1e88ccSAlexandre Courbot #else /* CONFIG_GPIOLIB */ 771bb1e88ccSAlexandre Courbot 772bb1e88ccSAlexandre Courbot static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) 773bb1e88ccSAlexandre Courbot { 774bb1e88ccSAlexandre Courbot /* GPIO can never have been requested */ 775bb1e88ccSAlexandre Courbot WARN_ON(1); 776bb1e88ccSAlexandre Courbot return ERR_PTR(-ENODEV); 777bb1e88ccSAlexandre Courbot } 778bb1e88ccSAlexandre Courbot 779a0b66a73SLinus Walleij static inline int gpiochip_lock_as_irq(struct gpio_chip *gc, 780c7663fa2SYueHaibing unsigned int offset) 781c7663fa2SYueHaibing { 782c7663fa2SYueHaibing WARN_ON(1); 783c7663fa2SYueHaibing return -EINVAL; 784c7663fa2SYueHaibing } 785c7663fa2SYueHaibing 786a0b66a73SLinus Walleij static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc, 787c7663fa2SYueHaibing unsigned int offset) 788c7663fa2SYueHaibing { 789c7663fa2SYueHaibing WARN_ON(1); 790c7663fa2SYueHaibing } 791bb1e88ccSAlexandre Courbot #endif /* CONFIG_GPIOLIB */ 792bb1e88ccSAlexandre Courbot 79385ebb1a6SAndy Shevchenko #define for_each_gpiochip_node(dev, child) \ 79485ebb1a6SAndy Shevchenko device_for_each_child_node(dev, child) \ 79585ebb1a6SAndy Shevchenko if (!fwnode_property_present(child, "gpio-controller")) {} else 79685ebb1a6SAndy Shevchenko 7970b19dde9SAndy Shevchenko static inline unsigned int gpiochip_node_count(struct device *dev) 7980b19dde9SAndy Shevchenko { 7990b19dde9SAndy Shevchenko struct fwnode_handle *child; 8000b19dde9SAndy Shevchenko unsigned int count = 0; 8010b19dde9SAndy Shevchenko 8020b19dde9SAndy Shevchenko for_each_gpiochip_node(dev, child) 8030b19dde9SAndy Shevchenko count++; 8040b19dde9SAndy Shevchenko 8050b19dde9SAndy Shevchenko return count; 8060b19dde9SAndy Shevchenko } 8070b19dde9SAndy Shevchenko 808af47d803SAndy Shevchenko static inline struct fwnode_handle *gpiochip_node_get_first(struct device *dev) 809af47d803SAndy Shevchenko { 810af47d803SAndy Shevchenko struct fwnode_handle *fwnode; 811af47d803SAndy Shevchenko 812af47d803SAndy Shevchenko for_each_gpiochip_node(dev, fwnode) 813af47d803SAndy Shevchenko return fwnode; 814af47d803SAndy Shevchenko 815af47d803SAndy Shevchenko return NULL; 816af47d803SAndy Shevchenko } 817af47d803SAndy Shevchenko 8189091373aSMasahiro Yamada #endif /* __LINUX_GPIO_DRIVER_H */ 819