1*4882a593Smuzhiyun================================== 2*4882a593SmuzhiyunGPIO Descriptor Consumer Interface 3*4882a593Smuzhiyun================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThis document describes the consumer interface of the GPIO framework. Note that 6*4882a593Smuzhiyunit describes the new descriptor-based interface. For a description of the 7*4882a593Smuzhiyundeprecated integer-based GPIO interface please refer to gpio-legacy.txt. 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunGuidelines for GPIOs consumers 11*4882a593Smuzhiyun============================== 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunDrivers that can't work without standard GPIO calls should have Kconfig entries 14*4882a593Smuzhiyunthat depend on GPIOLIB or select GPIOLIB. The functions that allow a driver to 15*4882a593Smuzhiyunobtain and use GPIOs are available by including the following file: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #include <linux/gpio/consumer.h> 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunThere are static inline stubs for all functions in the header file in the case 20*4882a593Smuzhiyunwhere GPIOLIB is disabled. When these stubs are called they will emit 21*4882a593Smuzhiyunwarnings. These stubs are used for two use cases: 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun- Simple compile coverage with e.g. COMPILE_TEST - it does not matter that 24*4882a593Smuzhiyun the current platform does not enable or select GPIOLIB because we are not 25*4882a593Smuzhiyun going to execute the system anyway. 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun- Truly optional GPIOLIB support - where the driver does not really make use 28*4882a593Smuzhiyun of the GPIOs on certain compile-time configurations for certain systems, but 29*4882a593Smuzhiyun will use it under other compile-time configurations. In this case the 30*4882a593Smuzhiyun consumer must make sure not to call into these functions, or the user will 31*4882a593Smuzhiyun be met with console warnings that may be perceived as intimidating. 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunAll the functions that work with the descriptor-based GPIO interface are 34*4882a593Smuzhiyunprefixed with ``gpiod_``. The ``gpio_`` prefix is used for the legacy 35*4882a593Smuzhiyuninterface. No other function in the kernel should use these prefixes. The use 36*4882a593Smuzhiyunof the legacy functions is strongly discouraged, new code should use 37*4882a593Smuzhiyun<linux/gpio/consumer.h> and descriptors exclusively. 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunObtaining and Disposing GPIOs 41*4882a593Smuzhiyun============================= 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunWith the descriptor-based interface, GPIOs are identified with an opaque, 44*4882a593Smuzhiyunnon-forgeable handler that must be obtained through a call to one of the 45*4882a593Smuzhiyungpiod_get() functions. Like many other kernel subsystems, gpiod_get() takes the 46*4882a593Smuzhiyundevice that will use the GPIO and the function the requested GPIO is supposed to 47*4882a593Smuzhiyunfulfill:: 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun struct gpio_desc *gpiod_get(struct device *dev, const char *con_id, 50*4882a593Smuzhiyun enum gpiod_flags flags) 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunIf a function is implemented by using several GPIOs together (e.g. a simple LED 53*4882a593Smuzhiyundevice that displays digits), an additional index argument can be specified:: 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun struct gpio_desc *gpiod_get_index(struct device *dev, 56*4882a593Smuzhiyun const char *con_id, unsigned int idx, 57*4882a593Smuzhiyun enum gpiod_flags flags) 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunFor a more detailed description of the con_id parameter in the DeviceTree case 60*4882a593Smuzhiyunsee Documentation/driver-api/gpio/board.rst 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunThe flags parameter is used to optionally specify a direction and initial value 63*4882a593Smuzhiyunfor the GPIO. Values can be: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun* GPIOD_ASIS or 0 to not initialize the GPIO at all. The direction must be set 66*4882a593Smuzhiyun later with one of the dedicated functions. 67*4882a593Smuzhiyun* GPIOD_IN to initialize the GPIO as input. 68*4882a593Smuzhiyun* GPIOD_OUT_LOW to initialize the GPIO as output with a value of 0. 69*4882a593Smuzhiyun* GPIOD_OUT_HIGH to initialize the GPIO as output with a value of 1. 70*4882a593Smuzhiyun* GPIOD_OUT_LOW_OPEN_DRAIN same as GPIOD_OUT_LOW but also enforce the line 71*4882a593Smuzhiyun to be electrically used with open drain. 72*4882a593Smuzhiyun* GPIOD_OUT_HIGH_OPEN_DRAIN same as GPIOD_OUT_HIGH but also enforce the line 73*4882a593Smuzhiyun to be electrically used with open drain. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunThe two last flags are used for use cases where open drain is mandatory, such 76*4882a593Smuzhiyunas I2C: if the line is not already configured as open drain in the mappings 77*4882a593Smuzhiyun(see board.txt), then open drain will be enforced anyway and a warning will be 78*4882a593Smuzhiyunprinted that the board configuration needs to be updated to match the use case. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunBoth functions return either a valid GPIO descriptor, or an error code checkable 81*4882a593Smuzhiyunwith IS_ERR() (they will never return a NULL pointer). -ENOENT will be returned 82*4882a593Smuzhiyunif and only if no GPIO has been assigned to the device/function/index triplet, 83*4882a593Smuzhiyunother error codes are used for cases where a GPIO has been assigned but an error 84*4882a593Smuzhiyunoccurred while trying to acquire it. This is useful to discriminate between mere 85*4882a593Smuzhiyunerrors and an absence of GPIO for optional GPIO parameters. For the common 86*4882a593Smuzhiyunpattern where a GPIO is optional, the gpiod_get_optional() and 87*4882a593Smuzhiyungpiod_get_index_optional() functions can be used. These functions return NULL 88*4882a593Smuzhiyuninstead of -ENOENT if no GPIO has been assigned to the requested function:: 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun struct gpio_desc *gpiod_get_optional(struct device *dev, 91*4882a593Smuzhiyun const char *con_id, 92*4882a593Smuzhiyun enum gpiod_flags flags) 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun struct gpio_desc *gpiod_get_index_optional(struct device *dev, 95*4882a593Smuzhiyun const char *con_id, 96*4882a593Smuzhiyun unsigned int index, 97*4882a593Smuzhiyun enum gpiod_flags flags) 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunNote that gpio_get*_optional() functions (and their managed variants), unlike 100*4882a593Smuzhiyunthe rest of gpiolib API, also return NULL when gpiolib support is disabled. 101*4882a593SmuzhiyunThis is helpful to driver authors, since they do not need to special case 102*4882a593Smuzhiyun-ENOSYS return codes. System integrators should however be careful to enable 103*4882a593Smuzhiyungpiolib on systems that need it. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunFor a function using multiple GPIOs all of those can be obtained with one call:: 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun struct gpio_descs *gpiod_get_array(struct device *dev, 108*4882a593Smuzhiyun const char *con_id, 109*4882a593Smuzhiyun enum gpiod_flags flags) 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunThis function returns a struct gpio_descs which contains an array of 112*4882a593Smuzhiyundescriptors. It also contains a pointer to a gpiolib private structure which, 113*4882a593Smuzhiyunif passed back to get/set array functions, may speed up I/O proocessing:: 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun struct gpio_descs { 116*4882a593Smuzhiyun struct gpio_array *info; 117*4882a593Smuzhiyun unsigned int ndescs; 118*4882a593Smuzhiyun struct gpio_desc *desc[]; 119*4882a593Smuzhiyun } 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunThe following function returns NULL instead of -ENOENT if no GPIOs have been 122*4882a593Smuzhiyunassigned to the requested function:: 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun struct gpio_descs *gpiod_get_array_optional(struct device *dev, 125*4882a593Smuzhiyun const char *con_id, 126*4882a593Smuzhiyun enum gpiod_flags flags) 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunDevice-managed variants of these functions are also defined:: 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id, 131*4882a593Smuzhiyun enum gpiod_flags flags) 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun struct gpio_desc *devm_gpiod_get_index(struct device *dev, 134*4882a593Smuzhiyun const char *con_id, 135*4882a593Smuzhiyun unsigned int idx, 136*4882a593Smuzhiyun enum gpiod_flags flags) 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun struct gpio_desc *devm_gpiod_get_optional(struct device *dev, 139*4882a593Smuzhiyun const char *con_id, 140*4882a593Smuzhiyun enum gpiod_flags flags) 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun struct gpio_desc *devm_gpiod_get_index_optional(struct device *dev, 143*4882a593Smuzhiyun const char *con_id, 144*4882a593Smuzhiyun unsigned int index, 145*4882a593Smuzhiyun enum gpiod_flags flags) 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun struct gpio_descs *devm_gpiod_get_array(struct device *dev, 148*4882a593Smuzhiyun const char *con_id, 149*4882a593Smuzhiyun enum gpiod_flags flags) 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun struct gpio_descs *devm_gpiod_get_array_optional(struct device *dev, 152*4882a593Smuzhiyun const char *con_id, 153*4882a593Smuzhiyun enum gpiod_flags flags) 154*4882a593Smuzhiyun 155*4882a593SmuzhiyunA GPIO descriptor can be disposed of using the gpiod_put() function:: 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun void gpiod_put(struct gpio_desc *desc) 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunFor an array of GPIOs this function can be used:: 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun void gpiod_put_array(struct gpio_descs *descs) 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunIt is strictly forbidden to use a descriptor after calling these functions. 164*4882a593SmuzhiyunIt is also not allowed to individually release descriptors (using gpiod_put()) 165*4882a593Smuzhiyunfrom an array acquired with gpiod_get_array(). 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunThe device-managed variants are, unsurprisingly:: 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs) 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunUsing GPIOs 175*4882a593Smuzhiyun=========== 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunSetting Direction 178*4882a593Smuzhiyun----------------- 179*4882a593SmuzhiyunThe first thing a driver must do with a GPIO is setting its direction. If no 180*4882a593Smuzhiyundirection-setting flags have been given to gpiod_get*(), this is done by 181*4882a593Smuzhiyuninvoking one of the gpiod_direction_*() functions:: 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun int gpiod_direction_input(struct gpio_desc *desc) 184*4882a593Smuzhiyun int gpiod_direction_output(struct gpio_desc *desc, int value) 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunThe return value is zero for success, else a negative errno. It should be 187*4882a593Smuzhiyunchecked, since the get/set calls don't return errors and since misconfiguration 188*4882a593Smuzhiyunis possible. You should normally issue these calls from a task context. However, 189*4882a593Smuzhiyunfor spinlock-safe GPIOs it is OK to use them before tasking is enabled, as part 190*4882a593Smuzhiyunof early board setup. 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunFor output GPIOs, the value provided becomes the initial output value. This 193*4882a593Smuzhiyunhelps avoid signal glitching during system startup. 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunA driver can also query the current direction of a GPIO:: 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun int gpiod_get_direction(const struct gpio_desc *desc) 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunThis function returns 0 for output, 1 for input, or an error code in case of error. 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunBe aware that there is no default direction for GPIOs. Therefore, **using a GPIO 202*4882a593Smuzhiyunwithout setting its direction first is illegal and will result in undefined 203*4882a593Smuzhiyunbehavior!** 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunSpinlock-Safe GPIO Access 207*4882a593Smuzhiyun------------------------- 208*4882a593SmuzhiyunMost GPIO controllers can be accessed with memory read/write instructions. Those 209*4882a593Smuzhiyundon't need to sleep, and can safely be done from inside hard (non-threaded) IRQ 210*4882a593Smuzhiyunhandlers and similar contexts. 211*4882a593Smuzhiyun 212*4882a593SmuzhiyunUse the following calls to access GPIOs from an atomic context:: 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun int gpiod_get_value(const struct gpio_desc *desc); 215*4882a593Smuzhiyun void gpiod_set_value(struct gpio_desc *desc, int value); 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunThe values are boolean, zero for low, nonzero for high. When reading the value 218*4882a593Smuzhiyunof an output pin, the value returned should be what's seen on the pin. That 219*4882a593Smuzhiyunwon't always match the specified output value, because of issues including 220*4882a593Smuzhiyunopen-drain signaling and output latencies. 221*4882a593Smuzhiyun 222*4882a593SmuzhiyunThe get/set calls do not return errors because "invalid GPIO" should have been 223*4882a593Smuzhiyunreported earlier from gpiod_direction_*(). However, note that not all platforms 224*4882a593Smuzhiyuncan read the value of output pins; those that can't should always return zero. 225*4882a593SmuzhiyunAlso, using these calls for GPIOs that can't safely be accessed without sleeping 226*4882a593Smuzhiyun(see below) is an error. 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun 229*4882a593SmuzhiyunGPIO Access That May Sleep 230*4882a593Smuzhiyun-------------------------- 231*4882a593SmuzhiyunSome GPIO controllers must be accessed using message based buses like I2C or 232*4882a593SmuzhiyunSPI. Commands to read or write those GPIO values require waiting to get to the 233*4882a593Smuzhiyunhead of a queue to transmit a command and get its response. This requires 234*4882a593Smuzhiyunsleeping, which can't be done from inside IRQ handlers. 235*4882a593Smuzhiyun 236*4882a593SmuzhiyunPlatforms that support this type of GPIO distinguish them from other GPIOs by 237*4882a593Smuzhiyunreturning nonzero from this call:: 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun int gpiod_cansleep(const struct gpio_desc *desc) 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunTo access such GPIOs, a different set of accessors is defined:: 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun int gpiod_get_value_cansleep(const struct gpio_desc *desc) 244*4882a593Smuzhiyun void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunAccessing such GPIOs requires a context which may sleep, for example a threaded 247*4882a593SmuzhiyunIRQ handler, and those accessors must be used instead of spinlock-safe 248*4882a593Smuzhiyunaccessors without the cansleep() name suffix. 249*4882a593Smuzhiyun 250*4882a593SmuzhiyunOther than the fact that these accessors might sleep, and will work on GPIOs 251*4882a593Smuzhiyunthat can't be accessed from hardIRQ handlers, these calls act the same as the 252*4882a593Smuzhiyunspinlock-safe calls. 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunThe active low and open drain semantics 256*4882a593Smuzhiyun--------------------------------------- 257*4882a593SmuzhiyunAs a consumer should not have to care about the physical line level, all of the 258*4882a593Smuzhiyungpiod_set_value_xxx() or gpiod_set_array_value_xxx() functions operate with 259*4882a593Smuzhiyunthe *logical* value. With this they take the active low property into account. 260*4882a593SmuzhiyunThis means that they check whether the GPIO is configured to be active low, 261*4882a593Smuzhiyunand if so, they manipulate the passed value before the physical line level is 262*4882a593Smuzhiyundriven. 263*4882a593Smuzhiyun 264*4882a593SmuzhiyunThe same is applicable for open drain or open source output lines: those do not 265*4882a593Smuzhiyunactively drive their output high (open drain) or low (open source), they just 266*4882a593Smuzhiyunswitch their output to a high impedance value. The consumer should not need to 267*4882a593Smuzhiyuncare. (For details read about open drain in driver.txt.) 268*4882a593Smuzhiyun 269*4882a593SmuzhiyunWith this, all the gpiod_set_(array)_value_xxx() functions interpret the 270*4882a593Smuzhiyunparameter "value" as "asserted" ("1") or "de-asserted" ("0"). The physical line 271*4882a593Smuzhiyunlevel will be driven accordingly. 272*4882a593Smuzhiyun 273*4882a593SmuzhiyunAs an example, if the active low property for a dedicated GPIO is set, and the 274*4882a593Smuzhiyungpiod_set_(array)_value_xxx() passes "asserted" ("1"), the physical line level 275*4882a593Smuzhiyunwill be driven low. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunTo summarize:: 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun Function (example) line property physical line 280*4882a593Smuzhiyun gpiod_set_raw_value(desc, 0); don't care low 281*4882a593Smuzhiyun gpiod_set_raw_value(desc, 1); don't care high 282*4882a593Smuzhiyun gpiod_set_value(desc, 0); default (active high) low 283*4882a593Smuzhiyun gpiod_set_value(desc, 1); default (active high) high 284*4882a593Smuzhiyun gpiod_set_value(desc, 0); active low high 285*4882a593Smuzhiyun gpiod_set_value(desc, 1); active low low 286*4882a593Smuzhiyun gpiod_set_value(desc, 0); open drain low 287*4882a593Smuzhiyun gpiod_set_value(desc, 1); open drain high impedance 288*4882a593Smuzhiyun gpiod_set_value(desc, 0); open source high impedance 289*4882a593Smuzhiyun gpiod_set_value(desc, 1); open source high 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunIt is possible to override these semantics using the set_raw/get_raw functions 292*4882a593Smuzhiyunbut it should be avoided as much as possible, especially by system-agnostic drivers 293*4882a593Smuzhiyunwhich should not need to care about the actual physical line level and worry about 294*4882a593Smuzhiyunthe logical value instead. 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun 297*4882a593SmuzhiyunAccessing raw GPIO values 298*4882a593Smuzhiyun------------------------- 299*4882a593SmuzhiyunConsumers exist that need to manage the logical state of a GPIO line, i.e. the value 300*4882a593Smuzhiyuntheir device will actually receive, no matter what lies between it and the GPIO 301*4882a593Smuzhiyunline. 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunThe following set of calls ignore the active-low or open drain property of a GPIO and 304*4882a593Smuzhiyunwork on the raw line value:: 305*4882a593Smuzhiyun 306*4882a593Smuzhiyun int gpiod_get_raw_value(const struct gpio_desc *desc) 307*4882a593Smuzhiyun void gpiod_set_raw_value(struct gpio_desc *desc, int value) 308*4882a593Smuzhiyun int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 309*4882a593Smuzhiyun void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) 310*4882a593Smuzhiyun int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunThe active low state of a GPIO can also be queried using the following call:: 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun int gpiod_is_active_low(const struct gpio_desc *desc) 315*4882a593Smuzhiyun 316*4882a593SmuzhiyunNote that these functions should only be used with great moderation; a driver 317*4882a593Smuzhiyunshould not have to care about the physical line level or open drain semantics. 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun 320*4882a593SmuzhiyunAccess multiple GPIOs with a single function call 321*4882a593Smuzhiyun------------------------------------------------- 322*4882a593SmuzhiyunThe following functions get or set the values of an array of GPIOs:: 323*4882a593Smuzhiyun 324*4882a593Smuzhiyun int gpiod_get_array_value(unsigned int array_size, 325*4882a593Smuzhiyun struct gpio_desc **desc_array, 326*4882a593Smuzhiyun struct gpio_array *array_info, 327*4882a593Smuzhiyun unsigned long *value_bitmap); 328*4882a593Smuzhiyun int gpiod_get_raw_array_value(unsigned int array_size, 329*4882a593Smuzhiyun struct gpio_desc **desc_array, 330*4882a593Smuzhiyun struct gpio_array *array_info, 331*4882a593Smuzhiyun unsigned long *value_bitmap); 332*4882a593Smuzhiyun int gpiod_get_array_value_cansleep(unsigned int array_size, 333*4882a593Smuzhiyun struct gpio_desc **desc_array, 334*4882a593Smuzhiyun struct gpio_array *array_info, 335*4882a593Smuzhiyun unsigned long *value_bitmap); 336*4882a593Smuzhiyun int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 337*4882a593Smuzhiyun struct gpio_desc **desc_array, 338*4882a593Smuzhiyun struct gpio_array *array_info, 339*4882a593Smuzhiyun unsigned long *value_bitmap); 340*4882a593Smuzhiyun 341*4882a593Smuzhiyun int gpiod_set_array_value(unsigned int array_size, 342*4882a593Smuzhiyun struct gpio_desc **desc_array, 343*4882a593Smuzhiyun struct gpio_array *array_info, 344*4882a593Smuzhiyun unsigned long *value_bitmap) 345*4882a593Smuzhiyun int gpiod_set_raw_array_value(unsigned int array_size, 346*4882a593Smuzhiyun struct gpio_desc **desc_array, 347*4882a593Smuzhiyun struct gpio_array *array_info, 348*4882a593Smuzhiyun unsigned long *value_bitmap) 349*4882a593Smuzhiyun int gpiod_set_array_value_cansleep(unsigned int array_size, 350*4882a593Smuzhiyun struct gpio_desc **desc_array, 351*4882a593Smuzhiyun struct gpio_array *array_info, 352*4882a593Smuzhiyun unsigned long *value_bitmap) 353*4882a593Smuzhiyun int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 354*4882a593Smuzhiyun struct gpio_desc **desc_array, 355*4882a593Smuzhiyun struct gpio_array *array_info, 356*4882a593Smuzhiyun unsigned long *value_bitmap) 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunThe array can be an arbitrary set of GPIOs. The functions will try to access 359*4882a593SmuzhiyunGPIOs belonging to the same bank or chip simultaneously if supported by the 360*4882a593Smuzhiyuncorresponding chip driver. In that case a significantly improved performance 361*4882a593Smuzhiyuncan be expected. If simultaneous access is not possible the GPIOs will be 362*4882a593Smuzhiyunaccessed sequentially. 363*4882a593Smuzhiyun 364*4882a593SmuzhiyunThe functions take three arguments: 365*4882a593Smuzhiyun * array_size - the number of array elements 366*4882a593Smuzhiyun * desc_array - an array of GPIO descriptors 367*4882a593Smuzhiyun * array_info - optional information obtained from gpiod_get_array() 368*4882a593Smuzhiyun * value_bitmap - a bitmap to store the GPIOs' values (get) or 369*4882a593Smuzhiyun a bitmap of values to assign to the GPIOs (set) 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunThe descriptor array can be obtained using the gpiod_get_array() function 372*4882a593Smuzhiyunor one of its variants. If the group of descriptors returned by that function 373*4882a593Smuzhiyunmatches the desired group of GPIOs, those GPIOs can be accessed by simply using 374*4882a593Smuzhiyunthe struct gpio_descs returned by gpiod_get_array():: 375*4882a593Smuzhiyun 376*4882a593Smuzhiyun struct gpio_descs *my_gpio_descs = gpiod_get_array(...); 377*4882a593Smuzhiyun gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc, 378*4882a593Smuzhiyun my_gpio_descs->info, my_gpio_value_bitmap); 379*4882a593Smuzhiyun 380*4882a593SmuzhiyunIt is also possible to access a completely arbitrary array of descriptors. The 381*4882a593Smuzhiyundescriptors may be obtained using any combination of gpiod_get() and 382*4882a593Smuzhiyungpiod_get_array(). Afterwards the array of descriptors has to be setup 383*4882a593Smuzhiyunmanually before it can be passed to one of the above functions. In that case, 384*4882a593Smuzhiyunarray_info should be set to NULL. 385*4882a593Smuzhiyun 386*4882a593SmuzhiyunNote that for optimal performance GPIOs belonging to the same chip should be 387*4882a593Smuzhiyuncontiguous within the array of descriptors. 388*4882a593Smuzhiyun 389*4882a593SmuzhiyunStill better performance may be achieved if array indexes of the descriptors 390*4882a593Smuzhiyunmatch hardware pin numbers of a single chip. If an array passed to a get/set 391*4882a593Smuzhiyunarray function matches the one obtained from gpiod_get_array() and array_info 392*4882a593Smuzhiyunassociated with the array is also passed, the function may take a fast bitmap 393*4882a593Smuzhiyunprocessing path, passing the value_bitmap argument directly to the respective 394*4882a593Smuzhiyun.get/set_multiple() callback of the chip. That allows for utilization of GPIO 395*4882a593Smuzhiyunbanks as data I/O ports without much loss of performance. 396*4882a593Smuzhiyun 397*4882a593SmuzhiyunThe return value of gpiod_get_array_value() and its variants is 0 on success 398*4882a593Smuzhiyunor negative on error. Note the difference to gpiod_get_value(), which returns 399*4882a593Smuzhiyun0 or 1 on success to convey the GPIO value. With the array functions, the GPIO 400*4882a593Smuzhiyunvalues are stored in value_array rather than passed back as return value. 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun 403*4882a593SmuzhiyunGPIOs mapped to IRQs 404*4882a593Smuzhiyun-------------------- 405*4882a593SmuzhiyunGPIO lines can quite often be used as IRQs. You can get the IRQ number 406*4882a593Smuzhiyuncorresponding to a given GPIO using the following call:: 407*4882a593Smuzhiyun 408*4882a593Smuzhiyun int gpiod_to_irq(const struct gpio_desc *desc) 409*4882a593Smuzhiyun 410*4882a593SmuzhiyunIt will return an IRQ number, or a negative errno code if the mapping can't be 411*4882a593Smuzhiyundone (most likely because that particular GPIO cannot be used as IRQ). It is an 412*4882a593Smuzhiyununchecked error to use a GPIO that wasn't set up as an input using 413*4882a593Smuzhiyungpiod_direction_input(), or to use an IRQ number that didn't originally come 414*4882a593Smuzhiyunfrom gpiod_to_irq(). gpiod_to_irq() is not allowed to sleep. 415*4882a593Smuzhiyun 416*4882a593SmuzhiyunNon-error values returned from gpiod_to_irq() can be passed to request_irq() or 417*4882a593Smuzhiyunfree_irq(). They will often be stored into IRQ resources for platform devices, 418*4882a593Smuzhiyunby the board-specific initialization code. Note that IRQ trigger options are 419*4882a593Smuzhiyunpart of the IRQ interface, e.g. IRQF_TRIGGER_FALLING, as are system wakeup 420*4882a593Smuzhiyuncapabilities. 421*4882a593Smuzhiyun 422*4882a593Smuzhiyun 423*4882a593SmuzhiyunGPIOs and ACPI 424*4882a593Smuzhiyun============== 425*4882a593Smuzhiyun 426*4882a593SmuzhiyunOn ACPI systems, GPIOs are described by GpioIo()/GpioInt() resources listed by 427*4882a593Smuzhiyunthe _CRS configuration objects of devices. Those resources do not provide 428*4882a593Smuzhiyunconnection IDs (names) for GPIOs, so it is necessary to use an additional 429*4882a593Smuzhiyunmechanism for this purpose. 430*4882a593Smuzhiyun 431*4882a593SmuzhiyunSystems compliant with ACPI 5.1 or newer may provide a _DSD configuration object 432*4882a593Smuzhiyunwhich, among other things, may be used to provide connection IDs for specific 433*4882a593SmuzhiyunGPIOs described by the GpioIo()/GpioInt() resources in _CRS. If that is the 434*4882a593Smuzhiyuncase, it will be handled by the GPIO subsystem automatically. However, if the 435*4882a593Smuzhiyun_DSD is not present, the mappings between GpioIo()/GpioInt() resources and GPIO 436*4882a593Smuzhiyunconnection IDs need to be provided by device drivers. 437*4882a593Smuzhiyun 438*4882a593SmuzhiyunFor details refer to Documentation/firmware-guide/acpi/gpio-properties.rst 439*4882a593Smuzhiyun 440*4882a593Smuzhiyun 441*4882a593SmuzhiyunInteracting With the Legacy GPIO Subsystem 442*4882a593Smuzhiyun========================================== 443*4882a593SmuzhiyunMany kernel subsystems still handle GPIOs using the legacy integer-based 444*4882a593Smuzhiyuninterface. Although it is strongly encouraged to upgrade them to the safer 445*4882a593Smuzhiyundescriptor-based API, the following two functions allow you to convert a GPIO 446*4882a593Smuzhiyundescriptor into the GPIO integer namespace and vice-versa:: 447*4882a593Smuzhiyun 448*4882a593Smuzhiyun int desc_to_gpio(const struct gpio_desc *desc) 449*4882a593Smuzhiyun struct gpio_desc *gpio_to_desc(unsigned gpio) 450*4882a593Smuzhiyun 451*4882a593SmuzhiyunThe GPIO number returned by desc_to_gpio() can be safely used as long as the 452*4882a593SmuzhiyunGPIO descriptor has not been freed. All the same, a GPIO number passed to 453*4882a593Smuzhiyungpio_to_desc() must have been properly acquired, and usage of the returned GPIO 454*4882a593Smuzhiyundescriptor is only possible after the GPIO number has been released. 455*4882a593Smuzhiyun 456*4882a593SmuzhiyunFreeing a GPIO obtained by one API with the other API is forbidden and an 457*4882a593Smuzhiyununchecked error. 458