19d2cb8e8SSimon Glass /* 29d2cb8e8SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 35f533aebSJoe Hershberger * Copyright (c) 2011, NVIDIA Corp. All rights reserved. 41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 59d2cb8e8SSimon Glass */ 69d2cb8e8SSimon Glass 75f533aebSJoe Hershberger #ifndef _ASM_GENERIC_GPIO_H_ 85f533aebSJoe Hershberger #define _ASM_GENERIC_GPIO_H_ 95f533aebSJoe Hershberger 109d2cb8e8SSimon Glass /* 119d2cb8e8SSimon Glass * Generic GPIO API for U-Boot 129d2cb8e8SSimon Glass * 135d1c17e9SSimon Glass * -- 145d1c17e9SSimon Glass * NB: This is deprecated. Please use the driver model functions instead: 155d1c17e9SSimon Glass * 165d1c17e9SSimon Glass * - gpio_request_by_name() 175d1c17e9SSimon Glass * - dm_gpio_get_value() etc. 185d1c17e9SSimon Glass * 195d1c17e9SSimon Glass * For now we need a dm_ prefix on some functions to avoid name collision. 205d1c17e9SSimon Glass * -- 215d1c17e9SSimon Glass * 229d2cb8e8SSimon Glass * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined 239d2cb8e8SSimon Glass * by the SOC/architecture. 249d2cb8e8SSimon Glass * 259d2cb8e8SSimon Glass * Each GPIO can be an input or output. If an input then its value can 269d2cb8e8SSimon Glass * be read as 0 or 1. If an output then its value can be set to 0 or 1. 279d2cb8e8SSimon Glass * If you try to write an input then the value is undefined. If you try 289d2cb8e8SSimon Glass * to read an output, barring something very unusual, you will get 299d2cb8e8SSimon Glass * back the value of the output that you previously set. 309d2cb8e8SSimon Glass * 319d2cb8e8SSimon Glass * In some cases the operation may fail, for example if the GPIO number 329d2cb8e8SSimon Glass * is out of range, or the GPIO is not available because its pin is 339d2cb8e8SSimon Glass * being used by another function. In that case, functions may return 349d2cb8e8SSimon Glass * an error value of -1. 359d2cb8e8SSimon Glass */ 369d2cb8e8SSimon Glass 379d2cb8e8SSimon Glass /** 385d1c17e9SSimon Glass * @deprecated Please use driver model instead 3925ca385dSMarcel Ziswiler * Request a GPIO. This should be called before any of the other functions 4025ca385dSMarcel Ziswiler * are used on this GPIO. 419d2cb8e8SSimon Glass * 42b892d127SSimon Glass * Note: With driver model, the label is allocated so there is no need for 43b892d127SSimon Glass * the caller to preserve it. 44b892d127SSimon Glass * 45d9a607f2SMasahiro Yamada * @param gpio GPIO number 4694740e47SNikita Kiryanov * @param label User label for this GPIO 479d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 489d2cb8e8SSimon Glass */ 495f533aebSJoe Hershberger int gpio_request(unsigned gpio, const char *label); 505f533aebSJoe Hershberger 515f533aebSJoe Hershberger /** 525d1c17e9SSimon Glass * @deprecated Please use driver model instead 535f533aebSJoe Hershberger * Stop using the GPIO. This function should not alter pin configuration. 545f533aebSJoe Hershberger * 555f533aebSJoe Hershberger * @param gpio GPIO number 565f533aebSJoe Hershberger * @return 0 if ok, -1 on error 575f533aebSJoe Hershberger */ 585f533aebSJoe Hershberger int gpio_free(unsigned gpio); 595f533aebSJoe Hershberger 605f533aebSJoe Hershberger /** 615d1c17e9SSimon Glass * @deprecated Please use driver model instead 625f533aebSJoe Hershberger * Make a GPIO an input. 635f533aebSJoe Hershberger * 645f533aebSJoe Hershberger * @param gpio GPIO number 655f533aebSJoe Hershberger * @return 0 if ok, -1 on error 665f533aebSJoe Hershberger */ 675f533aebSJoe Hershberger int gpio_direction_input(unsigned gpio); 689d2cb8e8SSimon Glass 699d2cb8e8SSimon Glass /** 705d1c17e9SSimon Glass * @deprecated Please use driver model instead 719d2cb8e8SSimon Glass * Make a GPIO an output, and set its value. 729d2cb8e8SSimon Glass * 735f533aebSJoe Hershberger * @param gpio GPIO number 749d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 759d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 769d2cb8e8SSimon Glass */ 775f533aebSJoe Hershberger int gpio_direction_output(unsigned gpio, int value); 789d2cb8e8SSimon Glass 799d2cb8e8SSimon Glass /** 805d1c17e9SSimon Glass * @deprecated Please use driver model instead 819d2cb8e8SSimon Glass * Get a GPIO's value. This will work whether the GPIO is an input 829d2cb8e8SSimon Glass * or an output. 839d2cb8e8SSimon Glass * 845f533aebSJoe Hershberger * @param gpio GPIO number 859d2cb8e8SSimon Glass * @return 0 if low, 1 if high, -1 on error 869d2cb8e8SSimon Glass */ 875f533aebSJoe Hershberger int gpio_get_value(unsigned gpio); 889d2cb8e8SSimon Glass 899d2cb8e8SSimon Glass /** 905d1c17e9SSimon Glass * @deprecated Please use driver model instead 915f533aebSJoe Hershberger * Set an output GPIO's value. The GPIO must already be an output or 929d2cb8e8SSimon Glass * this function may have no effect. 939d2cb8e8SSimon Glass * 945f533aebSJoe Hershberger * @param gpio GPIO number 959d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 969d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 979d2cb8e8SSimon Glass */ 985f533aebSJoe Hershberger int gpio_set_value(unsigned gpio, int value); 9996495d90SSimon Glass 10089e64054SSimon Glass /* State of a GPIO, as reported by get_function() */ 1016449a506SSimon Glass enum gpio_func_t { 10296495d90SSimon Glass GPIOF_INPUT = 0, 10396495d90SSimon Glass GPIOF_OUTPUT, 10489e64054SSimon Glass GPIOF_UNUSED, /* Not claimed */ 10589e64054SSimon Glass GPIOF_UNKNOWN, /* Not known */ 10689e64054SSimon Glass GPIOF_FUNC, /* Not used as a GPIO */ 10789e64054SSimon Glass 10889e64054SSimon Glass GPIOF_COUNT, 10996495d90SSimon Glass }; 11096495d90SSimon Glass 11154c5d08aSHeiko Schocher struct udevice; 11296495d90SSimon Glass 113ae7123f8SSimon Glass struct gpio_desc { 114ae7123f8SSimon Glass struct udevice *dev; /* Device, NULL for invalid GPIO */ 115ae7123f8SSimon Glass unsigned long flags; 116ae7123f8SSimon Glass #define GPIOD_REQUESTED (1 << 0) /* Requested/claimed */ 117ae7123f8SSimon Glass #define GPIOD_IS_OUT (1 << 1) /* GPIO is an output */ 118f9523961SSimon Glass #define GPIOD_IS_IN (1 << 2) /* GPIO is an input */ 119ae7123f8SSimon Glass #define GPIOD_ACTIVE_LOW (1 << 3) /* value has active low */ 120ae7123f8SSimon Glass #define GPIOD_IS_OUT_ACTIVE (1 << 4) /* set output active */ 121ae7123f8SSimon Glass 122ae7123f8SSimon Glass uint offset; /* GPIO offset within the device */ 123ae7123f8SSimon Glass /* 124ae7123f8SSimon Glass * We could consider adding the GPIO label in here. Possibly we could 125ae7123f8SSimon Glass * use this structure for internal GPIO information. 126ae7123f8SSimon Glass */ 127ae7123f8SSimon Glass }; 128ae7123f8SSimon Glass 12996495d90SSimon Glass /** 130d9a607f2SMasahiro Yamada * dm_gpio_is_valid() - Check if a GPIO is valid 1313669e0e7SSimon Glass * 1323669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 1333669e0e7SSimon Glass * previously returned by gpio_request_by_name() 1343669e0e7SSimon Glass * @return true if valid, false if not 1353669e0e7SSimon Glass */ 13617c43f1aSSimon Glass static inline bool dm_gpio_is_valid(const struct gpio_desc *desc) 1373669e0e7SSimon Glass { 1383669e0e7SSimon Glass return desc->dev != NULL; 1393669e0e7SSimon Glass } 1403669e0e7SSimon Glass 1413669e0e7SSimon Glass /** 1420757535aSSimon Glass * gpio_get_status() - get the current GPIO status as a string 1430757535aSSimon Glass * 1440757535aSSimon Glass * Obtain the current GPIO status as a string which can be presented to the 1450757535aSSimon Glass * user. A typical string is: 1460757535aSSimon Glass * 1470757535aSSimon Glass * "b4: in: 1 [x] sdmmc_cd" 1480757535aSSimon Glass * 1490757535aSSimon Glass * which means this is GPIO bank b, offset 4, currently set to input, current 1500757535aSSimon Glass * value 1, [x] means that it is requested and the owner is 'sdmmc_cd' 1510757535aSSimon Glass * 1525d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc 1535d1c17e9SSimon Glass * 1540757535aSSimon Glass * @dev: Device to check 1550757535aSSimon Glass * @offset: Offset of device GPIO to check 1560757535aSSimon Glass * @buf: Place to put string 1570757535aSSimon Glass * @buffsize: Size of string including \0 1580757535aSSimon Glass */ 1590757535aSSimon Glass int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize); 1600757535aSSimon Glass 1610757535aSSimon Glass /** 1626449a506SSimon Glass * gpio_get_function() - get the current function for a GPIO pin 1636449a506SSimon Glass * 1646449a506SSimon Glass * Note this returns GPIOF_UNUSED if the GPIO is not requested. 1656449a506SSimon Glass * 1665d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc 1675d1c17e9SSimon Glass * 1686449a506SSimon Glass * @dev: Device to check 1696449a506SSimon Glass * @offset: Offset of device GPIO to check 170d9a607f2SMasahiro Yamada * @namep: If non-NULL, this is set to the name given when the GPIO 1716449a506SSimon Glass * was requested, or -1 if it has not been requested 1726449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function, 1736449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid. 1746449a506SSimon Glass * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the 1756449a506SSimon Glass * function from enum gpio_func_t. 1766449a506SSimon Glass */ 1776449a506SSimon Glass int gpio_get_function(struct udevice *dev, int offset, const char **namep); 1786449a506SSimon Glass 1796449a506SSimon Glass /** 1806449a506SSimon Glass * gpio_get_raw_function() - get the current raw function for a GPIO pin 1816449a506SSimon Glass * 1826449a506SSimon Glass * Note this does not return GPIOF_UNUSED - it will always return the GPIO 1836449a506SSimon Glass * driver's view of a pin function, even if it is not correctly set up. 1846449a506SSimon Glass * 1855d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc 1865d1c17e9SSimon Glass * 1876449a506SSimon Glass * @dev: Device to check 1886449a506SSimon Glass * @offset: Offset of device GPIO to check 189d9a607f2SMasahiro Yamada * @namep: If non-NULL, this is set to the name given when the GPIO 1906449a506SSimon Glass * was requested, or -1 if it has not been requested 1916449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function, 1926449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid. 1936449a506SSimon Glass * Otherwise returns the function from enum gpio_func_t. 1946449a506SSimon Glass */ 1956449a506SSimon Glass int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep); 1966449a506SSimon Glass 1976449a506SSimon Glass /** 198d44f597bSSimon Glass * gpio_requestf() - request a GPIO using a format string for the owner 199d44f597bSSimon Glass * 200d44f597bSSimon Glass * This is a helper function for gpio_request(). It allows you to provide 201d44f597bSSimon Glass * a printf()-format string for the GPIO owner. It calls gpio_request() with 202d44f597bSSimon Glass * the string that is created 203d44f597bSSimon Glass */ 204d44f597bSSimon Glass int gpio_requestf(unsigned gpio, const char *fmt, ...) 205d44f597bSSimon Glass __attribute__ ((format (__printf__, 2, 3))); 206d44f597bSSimon Glass 2070dac4d51SSimon Glass struct fdtdec_phandle_args; 2080dac4d51SSimon Glass 209d44f597bSSimon Glass /** 210*6c880b77SEric Nelson * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate 211*6c880b77SEric Nelson * 212*6c880b77SEric Nelson * This routine sets the offset field to args[0] and the flags field to 213*6c880b77SEric Nelson * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1]. 214*6c880b77SEric Nelson * 215*6c880b77SEric Nelson */ 216*6c880b77SEric Nelson int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc, 217*6c880b77SEric Nelson struct fdtdec_phandle_args *args); 218*6c880b77SEric Nelson 219*6c880b77SEric Nelson /** 22096495d90SSimon Glass * struct struct dm_gpio_ops - Driver model GPIO operations 22196495d90SSimon Glass * 22296495d90SSimon Glass * Refer to functions above for description. These function largely copy 22396495d90SSimon Glass * the old API. 22496495d90SSimon Glass * 22596495d90SSimon Glass * This is trying to be close to Linux GPIO API. Once the U-Boot uses the 22696495d90SSimon Glass * new DM GPIO API, this should be really easy to flip over to the Linux 22796495d90SSimon Glass * GPIO API-alike interface. 22896495d90SSimon Glass * 22925ca385dSMarcel Ziswiler * Also it would be useful to standardise additional functions like 23096495d90SSimon Glass * pullup, slew rate and drive strength. 23196495d90SSimon Glass * 232d9a607f2SMasahiro Yamada * gpio_request() and gpio_free() are optional - if NULL then they will 23396495d90SSimon Glass * not be called. 23496495d90SSimon Glass * 23596495d90SSimon Glass * Note that @offset is the offset from the base GPIO of the device. So 23696495d90SSimon Glass * offset 0 is the device's first GPIO and offset o-1 is the last GPIO, 23796495d90SSimon Glass * where o is the number of GPIO lines controlled by the device. A device 23896495d90SSimon Glass * is typically used to control a single bank of GPIOs. Within complex 23996495d90SSimon Glass * SoCs there may be many banks and therefore many devices all referring 24096495d90SSimon Glass * to the different IO addresses within the SoC. 24196495d90SSimon Glass * 24225ca385dSMarcel Ziswiler * The uclass combines all GPIO devices together to provide a consistent 24396495d90SSimon Glass * numbering from 0 to n-1, where n is the number of GPIOs in total across 24496495d90SSimon Glass * all devices. Be careful not to confuse offset with gpio in the parameters. 24596495d90SSimon Glass */ 24696495d90SSimon Glass struct dm_gpio_ops { 24754c5d08aSHeiko Schocher int (*request)(struct udevice *dev, unsigned offset, const char *label); 24854c5d08aSHeiko Schocher int (*free)(struct udevice *dev, unsigned offset); 24954c5d08aSHeiko Schocher int (*direction_input)(struct udevice *dev, unsigned offset); 25054c5d08aSHeiko Schocher int (*direction_output)(struct udevice *dev, unsigned offset, 25196495d90SSimon Glass int value); 25254c5d08aSHeiko Schocher int (*get_value)(struct udevice *dev, unsigned offset); 25354c5d08aSHeiko Schocher int (*set_value)(struct udevice *dev, unsigned offset, int value); 25489e64054SSimon Glass /** 25589e64054SSimon Glass * get_function() Get the GPIO function 25689e64054SSimon Glass * 25789e64054SSimon Glass * @dev: Device to check 25889e64054SSimon Glass * @offset: GPIO offset within that device 25989e64054SSimon Glass * @return current function - GPIOF_... 26089e64054SSimon Glass */ 26154c5d08aSHeiko Schocher int (*get_function)(struct udevice *dev, unsigned offset); 2620dac4d51SSimon Glass 2630dac4d51SSimon Glass /** 2640dac4d51SSimon Glass * xlate() - Translate phandle arguments into a GPIO description 2650dac4d51SSimon Glass * 2660dac4d51SSimon Glass * This function should set up the fields in desc according to the 2670dac4d51SSimon Glass * information in the arguments. The uclass will have set up: 2680dac4d51SSimon Glass * 2690dac4d51SSimon Glass * @desc->dev to @dev 2700dac4d51SSimon Glass * @desc->flags to 0 271*6c880b77SEric Nelson * @desc->offset to 0 2720dac4d51SSimon Glass * 273*6c880b77SEric Nelson * This method is optional and defaults to gpio_xlate_offs_flags, 274*6c880b77SEric Nelson * which will parse offset and the GPIO_ACTIVE_LOW flag in the first 275*6c880b77SEric Nelson * two arguments. 2760dac4d51SSimon Glass * 2770dac4d51SSimon Glass * Note that @dev is passed in as a parameter to follow driver model 2780dac4d51SSimon Glass * uclass conventions, even though it is already available as 2790dac4d51SSimon Glass * desc->dev. 2800dac4d51SSimon Glass * 2810dac4d51SSimon Glass * @dev: GPIO device 2820dac4d51SSimon Glass * @desc: Place to put GPIO description 283d9a607f2SMasahiro Yamada * @args: Arguments provided in description 2840dac4d51SSimon Glass * @return 0 if OK, -ve on error 2850dac4d51SSimon Glass */ 2860dac4d51SSimon Glass int (*xlate)(struct udevice *dev, struct gpio_desc *desc, 2870dac4d51SSimon Glass struct fdtdec_phandle_args *args); 28896495d90SSimon Glass }; 28996495d90SSimon Glass 29096495d90SSimon Glass /** 29196495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass 29296495d90SSimon Glass * 29396495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering 29425ca385dSMarcel Ziswiler * scheme. To do this it maintains some private information about each 29596495d90SSimon Glass * device. 29696495d90SSimon Glass * 29796495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe 29896495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler. 29996495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs 30096495d90SSimon Glass * it contains. 30196495d90SSimon Glass * 30296495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called 30396495d90SSimon Glass * 'A0', 'A1', etc. 30496495d90SSimon Glass * @gpio_count: Number of GPIOs in this device 30596495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device 30696495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that 30796495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0. 308b892d127SSimon Glass * @name: Array of pointers to the name for each GPIO in this bank. The 309b892d127SSimon Glass * value of the pointer will be NULL if the GPIO has not been claimed. 31096495d90SSimon Glass */ 31196495d90SSimon Glass struct gpio_dev_priv { 31296495d90SSimon Glass const char *bank_name; 31396495d90SSimon Glass unsigned gpio_count; 31496495d90SSimon Glass unsigned gpio_base; 315b892d127SSimon Glass char **name; 31696495d90SSimon Glass }; 31796495d90SSimon Glass 31896495d90SSimon Glass /* Access the GPIO operations for a device */ 31996495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops) 32096495d90SSimon Glass 32196495d90SSimon Glass /** 32296495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device 32396495d90SSimon Glass * 32496495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number 32596495d90SSimon Glass * of GPIOs it controls. 32696495d90SSimon Glass * 32796495d90SSimon Glass * @dev: Device to look up 32896495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank 32996495d90SSimon Glass * @return bank name of this device 33096495d90SSimon Glass */ 33154c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count); 33296495d90SSimon Glass 33396495d90SSimon Glass /** 33432ec1598SSimon Glass * dm_gpio_lookup_name() - Look up a named GPIO and return its description 33532ec1598SSimon Glass * 33632ec1598SSimon Glass * The name of a GPIO is typically its bank name followed by a number from 0. 33732ec1598SSimon Glass * For example A0 is the first GPIO in bank A. Each bank is a separate driver 33832ec1598SSimon Glass * model device. 33932ec1598SSimon Glass * 34032ec1598SSimon Glass * @name: Name to look up 34132ec1598SSimon Glass * @desc: Returns description, on success 34232ec1598SSimon Glass * @return 0 if OK, -ve on error 34332ec1598SSimon Glass */ 34432ec1598SSimon Glass int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc); 34532ec1598SSimon Glass 34632ec1598SSimon Glass /** 34796495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details 34896495d90SSimon Glass * 34996495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO 35096495d90SSimon Glass * number. 35196495d90SSimon Glass * 35296495d90SSimon Glass * @name: GPIO name to look up 35396495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO 35496495d90SSimon Glass * @offsetp: Returns the offset number within this device 35596495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0 35696495d90SSimon Glass */ 35754c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp, 35896495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop); 35996495d90SSimon Glass 360e5901c94SSimon Glass /** 361962f5cafSSimon Glass * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int 362e5901c94SSimon Glass * 363e5901c94SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 364e5901c94SSimon Glass * etc. then returns the resulting integer. 365e5901c94SSimon Glass * 366e5901c94SSimon Glass * @gpio_list: List of GPIOs to collect 367962f5cafSSimon Glass * @return resulting integer value, or -ve on error 368e5901c94SSimon Glass */ 369962f5cafSSimon Glass int gpio_get_values_as_int(const int *gpio_list); 370962f5cafSSimon Glass 371962f5cafSSimon Glass /** 372bbf24780SSimon Glass * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int 373bbf24780SSimon Glass * 374bbf24780SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 375bbf24780SSimon Glass * etc. then returns the resulting integer. 376bbf24780SSimon Glass * 377bbf24780SSimon Glass * @desc_list: List of GPIOs to collect 378bbf24780SSimon Glass * @count: Number of GPIOs 379bbf24780SSimon Glass * @return resulting integer value, or -ve on error 380bbf24780SSimon Glass */ 38117c43f1aSSimon Glass int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count); 382bbf24780SSimon Glass 383bbf24780SSimon Glass /** 384962f5cafSSimon Glass * gpio_claim_vector() - claim a number of GPIOs for input 385962f5cafSSimon Glass * 386962f5cafSSimon Glass * @gpio_num_array: array of gpios to claim, terminated by -1 387962f5cafSSimon Glass * @fmt: format string for GPIO names, e.g. "board_id%d" 388962f5cafSSimon Glass * @return 0 if OK, -ve on error 389962f5cafSSimon Glass */ 390962f5cafSSimon Glass int gpio_claim_vector(const int *gpio_num_array, const char *fmt); 3915b5ac645SJeroen Hofstee 3923669e0e7SSimon Glass /** 3933669e0e7SSimon Glass * gpio_request_by_name() - Locate and request a GPIO by name 3943669e0e7SSimon Glass * 3953669e0e7SSimon Glass * This operates by looking up the given list name in the device (device 3963669e0e7SSimon Glass * tree property) and requesting the GPIO for use. The property must exist 3973669e0e7SSimon Glass * in @dev's node. 3983669e0e7SSimon Glass * 3993669e0e7SSimon Glass * Use @flags to specify whether the GPIO should be an input or output. In 4003669e0e7SSimon Glass * principle this can also come from the device tree binding but most 4013669e0e7SSimon Glass * bindings don't provide this information. Specifically, when the GPIO uclass 4023669e0e7SSimon Glass * calls the xlate() method, it can return default flags, which are then 4033669e0e7SSimon Glass * ORed with this @flags. 4043669e0e7SSimon Glass * 4053669e0e7SSimon Glass * If we find that requesting the GPIO is not always needed we could add a 4063669e0e7SSimon Glass * new function or a new GPIOD_NO_REQUEST flag. 4073669e0e7SSimon Glass * 4083669e0e7SSimon Glass * At present driver model has no reference counting so if one device 4093669e0e7SSimon Glass * requests a GPIO which subsequently is unbound, the @desc->dev pointer 4103669e0e7SSimon Glass * will be invalid. However this will only happen if the GPIO device is 4113669e0e7SSimon Glass * unbound, not if it is removed, so this seems like a reasonable limitation 4123669e0e7SSimon Glass * for now. There is no real use case for unbinding drivers in normal 4133669e0e7SSimon Glass * operation. 4143669e0e7SSimon Glass * 4153669e0e7SSimon Glass * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in 4163669e0e7SSimon Glass * generate terms and each specific device may add additional details in 4173669e0e7SSimon Glass * a binding file in the same directory. 4183669e0e7SSimon Glass * 4193669e0e7SSimon Glass * @dev: Device requesting the GPIO 4203669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4213669e0e7SSimon Glass * @index: Index number of the GPIO in that list use request (0=first) 4223669e0e7SSimon Glass * @desc: Returns GPIO description information. If there is no such 4233669e0e7SSimon Glass * GPIO, dev->dev will be NULL. 4243669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 4253669e0e7SSimon Glass * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is 4263669e0e7SSimon Glass * something wrong with the list, or other -ve for another error (e.g. 4273669e0e7SSimon Glass * -EBUSY if a GPIO was already requested) 4283669e0e7SSimon Glass */ 4293669e0e7SSimon Glass int gpio_request_by_name(struct udevice *dev, const char *list_name, 4303669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 4313669e0e7SSimon Glass 4323669e0e7SSimon Glass /** 4333669e0e7SSimon Glass * gpio_request_list_by_name() - Request a list of GPIOs 4343669e0e7SSimon Glass * 435d9a607f2SMasahiro Yamada * Reads all the GPIOs from a list and requests them. See 4363669e0e7SSimon Glass * gpio_request_by_name() for additional details. Lists should not be 4373669e0e7SSimon Glass * misused to hold unrelated or optional GPIOs. They should only be used 4383669e0e7SSimon Glass * for things like parallel data lines. A zero phandle terminates the list 4393669e0e7SSimon Glass * the list. 4403669e0e7SSimon Glass * 4413669e0e7SSimon Glass * This function will either succeed, and request all GPIOs in the list, or 4423669e0e7SSimon Glass * fail and request none (it will free already-requested GPIOs in case of 4433669e0e7SSimon Glass * an error part-way through). 4443669e0e7SSimon Glass * 4453669e0e7SSimon Glass * @dev: Device requesting the GPIO 4463669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4473669e0e7SSimon Glass * @desc_list: Returns a list of GPIO description information 4483669e0e7SSimon Glass * @max_count: Maximum number of GPIOs to return (@desc_list must be at least 4493669e0e7SSimon Glass * this big) 4503669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 4513669e0e7SSimon Glass * @return number of GPIOs requested, or -ve on error 4523669e0e7SSimon Glass */ 4533669e0e7SSimon Glass int gpio_request_list_by_name(struct udevice *dev, const char *list_name, 4543669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 4553669e0e7SSimon Glass int flags); 4563669e0e7SSimon Glass 4573669e0e7SSimon Glass /** 458efa677fbSSimon Glass * dm_gpio_request() - manually request a GPIO 459efa677fbSSimon Glass * 460efa677fbSSimon Glass * Note: This function should only be used for testing / debugging. Instead. 461efa677fbSSimon Glass * use gpio_request_by_name() to pull GPIOs from the device tree. 462efa677fbSSimon Glass * 463efa677fbSSimon Glass * @desc: GPIO description of GPIO to request (see dm_gpio_lookup_name()) 464efa677fbSSimon Glass * @label: Label to attach to the GPIO while claimed 465efa677fbSSimon Glass * @return 0 if OK, -ve on error 466efa677fbSSimon Glass */ 467efa677fbSSimon Glass int dm_gpio_request(struct gpio_desc *desc, const char *label); 468efa677fbSSimon Glass 469efa677fbSSimon Glass /** 4703669e0e7SSimon Glass * gpio_get_list_count() - Returns the number of GPIOs in a list 4713669e0e7SSimon Glass * 4723669e0e7SSimon Glass * Counts the GPIOs in a list. See gpio_request_by_name() for additional 4733669e0e7SSimon Glass * details. 4743669e0e7SSimon Glass * 4753669e0e7SSimon Glass * @dev: Device requesting the GPIO 4763669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4773669e0e7SSimon Glass * @return number of GPIOs (0 for an empty property) or -ENOENT if the list 4783669e0e7SSimon Glass * does not exist 4793669e0e7SSimon Glass */ 4803669e0e7SSimon Glass int gpio_get_list_count(struct udevice *dev, const char *list_name); 4813669e0e7SSimon Glass 4823669e0e7SSimon Glass /** 4833669e0e7SSimon Glass * gpio_request_by_name_nodev() - request GPIOs without a device 4843669e0e7SSimon Glass * 4853669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4863669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4873669e0e7SSimon Glass */ 4883669e0e7SSimon Glass int gpio_request_by_name_nodev(const void *blob, int node, 4893669e0e7SSimon Glass const char *list_name, 4903669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 4913669e0e7SSimon Glass 4923669e0e7SSimon Glass /** 4933669e0e7SSimon Glass * gpio_request_list_by_name_nodev() - request GPIOs without a device 4943669e0e7SSimon Glass * 4953669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4963669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4973669e0e7SSimon Glass */ 4983669e0e7SSimon Glass int gpio_request_list_by_name_nodev(const void *blob, int node, 4993669e0e7SSimon Glass const char *list_name, 5003669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 5013669e0e7SSimon Glass int flags); 5023669e0e7SSimon Glass 5033669e0e7SSimon Glass /** 5043669e0e7SSimon Glass * dm_gpio_free() - Free a single GPIO 5053669e0e7SSimon Glass * 5063669e0e7SSimon Glass * This frees a single GPIOs previously returned from gpio_request_by_name(). 5073669e0e7SSimon Glass * 5083669e0e7SSimon Glass * @dev: Device which requested the GPIO 5093669e0e7SSimon Glass * @desc: GPIO to free 5103669e0e7SSimon Glass * @return 0 if OK, -ve on error 5113669e0e7SSimon Glass */ 5123669e0e7SSimon Glass int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc); 5133669e0e7SSimon Glass 5143669e0e7SSimon Glass /** 5153669e0e7SSimon Glass * gpio_free_list() - Free a list of GPIOs 5163669e0e7SSimon Glass * 5173669e0e7SSimon Glass * This frees a list of GPIOs previously returned from 5183669e0e7SSimon Glass * gpio_request_list_by_name(). 5193669e0e7SSimon Glass * 5203669e0e7SSimon Glass * @dev: Device which requested the GPIOs 5213669e0e7SSimon Glass * @desc: List of GPIOs to free 5223669e0e7SSimon Glass * @count: Number of GPIOs in the list 5233669e0e7SSimon Glass * @return 0 if OK, -ve on error 5243669e0e7SSimon Glass */ 5253669e0e7SSimon Glass int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count); 5263669e0e7SSimon Glass 5273669e0e7SSimon Glass /** 5283669e0e7SSimon Glass * gpio_free_list_nodev() - free GPIOs without a device 5293669e0e7SSimon Glass * 5303669e0e7SSimon Glass * This is a version of gpio_free_list() that does not use a 5313669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 5323669e0e7SSimon Glass */ 5333669e0e7SSimon Glass int gpio_free_list_nodev(struct gpio_desc *desc, int count); 5343669e0e7SSimon Glass 5353669e0e7SSimon Glass /** 5363669e0e7SSimon Glass * dm_gpio_get_value() - Get the value of a GPIO 5373669e0e7SSimon Glass * 5383669e0e7SSimon Glass * This is the driver model version of the existing gpio_get_value() function 5393669e0e7SSimon Glass * and should be used instead of that. 5403669e0e7SSimon Glass * 5413669e0e7SSimon Glass * For now, these functions have a dm_ prefix since they conflict with 5423669e0e7SSimon Glass * existing names. 5433669e0e7SSimon Glass * 5443669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5453669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5463669e0e7SSimon Glass * @return GPIO value (0 for inactive, 1 for active) or -ve on error 5473669e0e7SSimon Glass */ 54817c43f1aSSimon Glass int dm_gpio_get_value(const struct gpio_desc *desc); 5493669e0e7SSimon Glass 55017c43f1aSSimon Glass int dm_gpio_set_value(const struct gpio_desc *desc, int value); 5513669e0e7SSimon Glass 5523669e0e7SSimon Glass /** 5533669e0e7SSimon Glass * dm_gpio_set_dir() - Set the direction for a GPIO 5543669e0e7SSimon Glass * 5553669e0e7SSimon Glass * This sets up the direction according tot the provided flags. It will do 5563669e0e7SSimon Glass * nothing unless the direction is actually specified. 5573669e0e7SSimon Glass * 5583669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5593669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5603669e0e7SSimon Glass * @return 0 if OK, -ve on error 5613669e0e7SSimon Glass */ 5623669e0e7SSimon Glass int dm_gpio_set_dir(struct gpio_desc *desc); 5633669e0e7SSimon Glass 5643669e0e7SSimon Glass /** 5653669e0e7SSimon Glass * dm_gpio_set_dir_flags() - Set direction using specific flags 5663669e0e7SSimon Glass * 5673669e0e7SSimon Glass * This is like dm_gpio_set_dir() except that the flags value is provided 5683669e0e7SSimon Glass * instead of being used from desc->flags. This is needed because in many 5693669e0e7SSimon Glass * cases the GPIO description does not include direction information. 5703669e0e7SSimon Glass * Note that desc->flags is updated by this function. 5713669e0e7SSimon Glass * 5723669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5733669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5743669e0e7SSimon Glass * @flags: New flags to use 5753669e0e7SSimon Glass * @return 0 if OK, -ve on error, in which case desc->flags is not updated 5763669e0e7SSimon Glass */ 5773669e0e7SSimon Glass int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags); 5783669e0e7SSimon Glass 5793669e0e7SSimon Glass /** 5803669e0e7SSimon Glass * gpio_get_number() - Get the global GPIO number of a GPIO 5813669e0e7SSimon Glass * 582d9a607f2SMasahiro Yamada * This should only be used for debugging or interest. It returns the number 5833669e0e7SSimon Glass * that should be used for gpio_get_value() etc. to access this GPIO. 5843669e0e7SSimon Glass * 5853669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5863669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5873669e0e7SSimon Glass * @return GPIO number, or -ve if not found 5883669e0e7SSimon Glass */ 58917c43f1aSSimon Glass int gpio_get_number(const struct gpio_desc *desc); 5903669e0e7SSimon Glass 5915f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */ 592