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 /** 2106c880b77SEric Nelson * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate 2116c880b77SEric Nelson * 2126c880b77SEric Nelson * This routine sets the offset field to args[0] and the flags field to 2136c880b77SEric Nelson * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1]. 2146c880b77SEric Nelson * 2156c880b77SEric Nelson */ 2166c880b77SEric Nelson int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc, 2176c880b77SEric Nelson struct fdtdec_phandle_args *args); 2186c880b77SEric Nelson 2196c880b77SEric 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); 254*53ecdfb9Smario.six@gdsys.cc int (*get_open_drain)(struct udevice *dev, unsigned offset); 255*53ecdfb9Smario.six@gdsys.cc int (*set_open_drain)(struct udevice *dev, unsigned offset, int value); 25689e64054SSimon Glass /** 25789e64054SSimon Glass * get_function() Get the GPIO function 25889e64054SSimon Glass * 25989e64054SSimon Glass * @dev: Device to check 26089e64054SSimon Glass * @offset: GPIO offset within that device 26189e64054SSimon Glass * @return current function - GPIOF_... 26289e64054SSimon Glass */ 26354c5d08aSHeiko Schocher int (*get_function)(struct udevice *dev, unsigned offset); 2640dac4d51SSimon Glass 2650dac4d51SSimon Glass /** 2660dac4d51SSimon Glass * xlate() - Translate phandle arguments into a GPIO description 2670dac4d51SSimon Glass * 2680dac4d51SSimon Glass * This function should set up the fields in desc according to the 2690dac4d51SSimon Glass * information in the arguments. The uclass will have set up: 2700dac4d51SSimon Glass * 2710dac4d51SSimon Glass * @desc->dev to @dev 2720dac4d51SSimon Glass * @desc->flags to 0 2736c880b77SEric Nelson * @desc->offset to 0 2740dac4d51SSimon Glass * 2756c880b77SEric Nelson * This method is optional and defaults to gpio_xlate_offs_flags, 2766c880b77SEric Nelson * which will parse offset and the GPIO_ACTIVE_LOW flag in the first 2776c880b77SEric Nelson * two arguments. 2780dac4d51SSimon Glass * 2790dac4d51SSimon Glass * Note that @dev is passed in as a parameter to follow driver model 2800dac4d51SSimon Glass * uclass conventions, even though it is already available as 2810dac4d51SSimon Glass * desc->dev. 2820dac4d51SSimon Glass * 2830dac4d51SSimon Glass * @dev: GPIO device 2840dac4d51SSimon Glass * @desc: Place to put GPIO description 285d9a607f2SMasahiro Yamada * @args: Arguments provided in description 2860dac4d51SSimon Glass * @return 0 if OK, -ve on error 2870dac4d51SSimon Glass */ 2880dac4d51SSimon Glass int (*xlate)(struct udevice *dev, struct gpio_desc *desc, 2890dac4d51SSimon Glass struct fdtdec_phandle_args *args); 29096495d90SSimon Glass }; 29196495d90SSimon Glass 29296495d90SSimon Glass /** 29396495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass 29496495d90SSimon Glass * 29596495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering 29625ca385dSMarcel Ziswiler * scheme. To do this it maintains some private information about each 29796495d90SSimon Glass * device. 29896495d90SSimon Glass * 29996495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe 30096495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler. 30196495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs 30296495d90SSimon Glass * it contains. 30396495d90SSimon Glass * 30496495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called 30596495d90SSimon Glass * 'A0', 'A1', etc. 30696495d90SSimon Glass * @gpio_count: Number of GPIOs in this device 30796495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device 30896495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that 30996495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0. 310b892d127SSimon Glass * @name: Array of pointers to the name for each GPIO in this bank. The 311b892d127SSimon Glass * value of the pointer will be NULL if the GPIO has not been claimed. 31296495d90SSimon Glass */ 31396495d90SSimon Glass struct gpio_dev_priv { 31496495d90SSimon Glass const char *bank_name; 31596495d90SSimon Glass unsigned gpio_count; 31696495d90SSimon Glass unsigned gpio_base; 317b892d127SSimon Glass char **name; 31896495d90SSimon Glass }; 31996495d90SSimon Glass 32096495d90SSimon Glass /* Access the GPIO operations for a device */ 32196495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops) 32296495d90SSimon Glass 32396495d90SSimon Glass /** 32496495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device 32596495d90SSimon Glass * 32696495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number 32796495d90SSimon Glass * of GPIOs it controls. 32896495d90SSimon Glass * 32996495d90SSimon Glass * @dev: Device to look up 33096495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank 33196495d90SSimon Glass * @return bank name of this device 33296495d90SSimon Glass */ 33354c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count); 33496495d90SSimon Glass 33596495d90SSimon Glass /** 33632ec1598SSimon Glass * dm_gpio_lookup_name() - Look up a named GPIO and return its description 33732ec1598SSimon Glass * 33832ec1598SSimon Glass * The name of a GPIO is typically its bank name followed by a number from 0. 33932ec1598SSimon Glass * For example A0 is the first GPIO in bank A. Each bank is a separate driver 34032ec1598SSimon Glass * model device. 34132ec1598SSimon Glass * 34232ec1598SSimon Glass * @name: Name to look up 34332ec1598SSimon Glass * @desc: Returns description, on success 34432ec1598SSimon Glass * @return 0 if OK, -ve on error 34532ec1598SSimon Glass */ 34632ec1598SSimon Glass int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc); 34732ec1598SSimon Glass 34832ec1598SSimon Glass /** 34996495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details 35096495d90SSimon Glass * 35196495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO 35296495d90SSimon Glass * number. 35396495d90SSimon Glass * 35496495d90SSimon Glass * @name: GPIO name to look up 35596495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO 35696495d90SSimon Glass * @offsetp: Returns the offset number within this device 35796495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0 35896495d90SSimon Glass */ 35954c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp, 36096495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop); 36196495d90SSimon Glass 362e5901c94SSimon Glass /** 363962f5cafSSimon Glass * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int 364e5901c94SSimon Glass * 365e5901c94SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 366e5901c94SSimon Glass * etc. then returns the resulting integer. 367e5901c94SSimon Glass * 368e5901c94SSimon Glass * @gpio_list: List of GPIOs to collect 369962f5cafSSimon Glass * @return resulting integer value, or -ve on error 370e5901c94SSimon Glass */ 371962f5cafSSimon Glass int gpio_get_values_as_int(const int *gpio_list); 372962f5cafSSimon Glass 373962f5cafSSimon Glass /** 374bbf24780SSimon Glass * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int 375bbf24780SSimon Glass * 376bbf24780SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 377bbf24780SSimon Glass * etc. then returns the resulting integer. 378bbf24780SSimon Glass * 379bbf24780SSimon Glass * @desc_list: List of GPIOs to collect 380bbf24780SSimon Glass * @count: Number of GPIOs 381bbf24780SSimon Glass * @return resulting integer value, or -ve on error 382bbf24780SSimon Glass */ 38317c43f1aSSimon Glass int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count); 384bbf24780SSimon Glass 385bbf24780SSimon Glass /** 386962f5cafSSimon Glass * gpio_claim_vector() - claim a number of GPIOs for input 387962f5cafSSimon Glass * 388962f5cafSSimon Glass * @gpio_num_array: array of gpios to claim, terminated by -1 389962f5cafSSimon Glass * @fmt: format string for GPIO names, e.g. "board_id%d" 390962f5cafSSimon Glass * @return 0 if OK, -ve on error 391962f5cafSSimon Glass */ 392962f5cafSSimon Glass int gpio_claim_vector(const int *gpio_num_array, const char *fmt); 3935b5ac645SJeroen Hofstee 3943669e0e7SSimon Glass /** 3953669e0e7SSimon Glass * gpio_request_by_name() - Locate and request a GPIO by name 3963669e0e7SSimon Glass * 3973669e0e7SSimon Glass * This operates by looking up the given list name in the device (device 3983669e0e7SSimon Glass * tree property) and requesting the GPIO for use. The property must exist 3993669e0e7SSimon Glass * in @dev's node. 4003669e0e7SSimon Glass * 4013669e0e7SSimon Glass * Use @flags to specify whether the GPIO should be an input or output. In 4023669e0e7SSimon Glass * principle this can also come from the device tree binding but most 4033669e0e7SSimon Glass * bindings don't provide this information. Specifically, when the GPIO uclass 4043669e0e7SSimon Glass * calls the xlate() method, it can return default flags, which are then 4053669e0e7SSimon Glass * ORed with this @flags. 4063669e0e7SSimon Glass * 4073669e0e7SSimon Glass * If we find that requesting the GPIO is not always needed we could add a 4083669e0e7SSimon Glass * new function or a new GPIOD_NO_REQUEST flag. 4093669e0e7SSimon Glass * 4103669e0e7SSimon Glass * At present driver model has no reference counting so if one device 4113669e0e7SSimon Glass * requests a GPIO which subsequently is unbound, the @desc->dev pointer 4123669e0e7SSimon Glass * will be invalid. However this will only happen if the GPIO device is 4133669e0e7SSimon Glass * unbound, not if it is removed, so this seems like a reasonable limitation 4143669e0e7SSimon Glass * for now. There is no real use case for unbinding drivers in normal 4153669e0e7SSimon Glass * operation. 4163669e0e7SSimon Glass * 4173669e0e7SSimon Glass * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in 4183669e0e7SSimon Glass * generate terms and each specific device may add additional details in 4193669e0e7SSimon Glass * a binding file in the same directory. 4203669e0e7SSimon Glass * 4213669e0e7SSimon Glass * @dev: Device requesting the GPIO 4223669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4233669e0e7SSimon Glass * @index: Index number of the GPIO in that list use request (0=first) 4243669e0e7SSimon Glass * @desc: Returns GPIO description information. If there is no such 4253669e0e7SSimon Glass * GPIO, dev->dev will be NULL. 4263669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 4273669e0e7SSimon Glass * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is 4283669e0e7SSimon Glass * something wrong with the list, or other -ve for another error (e.g. 4293669e0e7SSimon Glass * -EBUSY if a GPIO was already requested) 4303669e0e7SSimon Glass */ 4313669e0e7SSimon Glass int gpio_request_by_name(struct udevice *dev, const char *list_name, 4323669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 4333669e0e7SSimon Glass 4343669e0e7SSimon Glass /** 4353669e0e7SSimon Glass * gpio_request_list_by_name() - Request a list of GPIOs 4363669e0e7SSimon Glass * 437d9a607f2SMasahiro Yamada * Reads all the GPIOs from a list and requests them. See 4383669e0e7SSimon Glass * gpio_request_by_name() for additional details. Lists should not be 4393669e0e7SSimon Glass * misused to hold unrelated or optional GPIOs. They should only be used 4403669e0e7SSimon Glass * for things like parallel data lines. A zero phandle terminates the list 4413669e0e7SSimon Glass * the list. 4423669e0e7SSimon Glass * 4433669e0e7SSimon Glass * This function will either succeed, and request all GPIOs in the list, or 4443669e0e7SSimon Glass * fail and request none (it will free already-requested GPIOs in case of 4453669e0e7SSimon Glass * an error part-way through). 4463669e0e7SSimon Glass * 4473669e0e7SSimon Glass * @dev: Device requesting the GPIO 4483669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4493669e0e7SSimon Glass * @desc_list: Returns a list of GPIO description information 4503669e0e7SSimon Glass * @max_count: Maximum number of GPIOs to return (@desc_list must be at least 4513669e0e7SSimon Glass * this big) 4523669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 4533669e0e7SSimon Glass * @return number of GPIOs requested, or -ve on error 4543669e0e7SSimon Glass */ 4553669e0e7SSimon Glass int gpio_request_list_by_name(struct udevice *dev, const char *list_name, 4563669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 4573669e0e7SSimon Glass int flags); 4583669e0e7SSimon Glass 4593669e0e7SSimon Glass /** 460efa677fbSSimon Glass * dm_gpio_request() - manually request a GPIO 461efa677fbSSimon Glass * 462efa677fbSSimon Glass * Note: This function should only be used for testing / debugging. Instead. 463efa677fbSSimon Glass * use gpio_request_by_name() to pull GPIOs from the device tree. 464efa677fbSSimon Glass * 465efa677fbSSimon Glass * @desc: GPIO description of GPIO to request (see dm_gpio_lookup_name()) 466efa677fbSSimon Glass * @label: Label to attach to the GPIO while claimed 467efa677fbSSimon Glass * @return 0 if OK, -ve on error 468efa677fbSSimon Glass */ 469efa677fbSSimon Glass int dm_gpio_request(struct gpio_desc *desc, const char *label); 470efa677fbSSimon Glass 471efa677fbSSimon Glass /** 4723669e0e7SSimon Glass * gpio_get_list_count() - Returns the number of GPIOs in a list 4733669e0e7SSimon Glass * 4743669e0e7SSimon Glass * Counts the GPIOs in a list. See gpio_request_by_name() for additional 4753669e0e7SSimon Glass * details. 4763669e0e7SSimon Glass * 4773669e0e7SSimon Glass * @dev: Device requesting the GPIO 4783669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4793669e0e7SSimon Glass * @return number of GPIOs (0 for an empty property) or -ENOENT if the list 4803669e0e7SSimon Glass * does not exist 4813669e0e7SSimon Glass */ 4823669e0e7SSimon Glass int gpio_get_list_count(struct udevice *dev, const char *list_name); 4833669e0e7SSimon Glass 4843669e0e7SSimon Glass /** 4853669e0e7SSimon Glass * gpio_request_by_name_nodev() - request GPIOs without a device 4863669e0e7SSimon Glass * 4873669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4883669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4893669e0e7SSimon Glass */ 4903669e0e7SSimon Glass int gpio_request_by_name_nodev(const void *blob, int node, 4913669e0e7SSimon Glass const char *list_name, 4923669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 4933669e0e7SSimon Glass 4943669e0e7SSimon Glass /** 4953669e0e7SSimon Glass * gpio_request_list_by_name_nodev() - request GPIOs without a device 4963669e0e7SSimon Glass * 4973669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4983669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4993669e0e7SSimon Glass */ 5003669e0e7SSimon Glass int gpio_request_list_by_name_nodev(const void *blob, int node, 5013669e0e7SSimon Glass const char *list_name, 5023669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 5033669e0e7SSimon Glass int flags); 5043669e0e7SSimon Glass 5053669e0e7SSimon Glass /** 5063669e0e7SSimon Glass * dm_gpio_free() - Free a single GPIO 5073669e0e7SSimon Glass * 5083669e0e7SSimon Glass * This frees a single GPIOs previously returned from gpio_request_by_name(). 5093669e0e7SSimon Glass * 5103669e0e7SSimon Glass * @dev: Device which requested the GPIO 5113669e0e7SSimon Glass * @desc: GPIO to free 5123669e0e7SSimon Glass * @return 0 if OK, -ve on error 5133669e0e7SSimon Glass */ 5143669e0e7SSimon Glass int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc); 5153669e0e7SSimon Glass 5163669e0e7SSimon Glass /** 5173669e0e7SSimon Glass * gpio_free_list() - Free a list of GPIOs 5183669e0e7SSimon Glass * 5193669e0e7SSimon Glass * This frees a list of GPIOs previously returned from 5203669e0e7SSimon Glass * gpio_request_list_by_name(). 5213669e0e7SSimon Glass * 5223669e0e7SSimon Glass * @dev: Device which requested the GPIOs 5233669e0e7SSimon Glass * @desc: List of GPIOs to free 5243669e0e7SSimon Glass * @count: Number of GPIOs in the list 5253669e0e7SSimon Glass * @return 0 if OK, -ve on error 5263669e0e7SSimon Glass */ 5273669e0e7SSimon Glass int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count); 5283669e0e7SSimon Glass 5293669e0e7SSimon Glass /** 5303669e0e7SSimon Glass * gpio_free_list_nodev() - free GPIOs without a device 5313669e0e7SSimon Glass * 5323669e0e7SSimon Glass * This is a version of gpio_free_list() that does not use a 5333669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 5343669e0e7SSimon Glass */ 5353669e0e7SSimon Glass int gpio_free_list_nodev(struct gpio_desc *desc, int count); 5363669e0e7SSimon Glass 5373669e0e7SSimon Glass /** 5383669e0e7SSimon Glass * dm_gpio_get_value() - Get the value of a GPIO 5393669e0e7SSimon Glass * 5403669e0e7SSimon Glass * This is the driver model version of the existing gpio_get_value() function 5413669e0e7SSimon Glass * and should be used instead of that. 5423669e0e7SSimon Glass * 5433669e0e7SSimon Glass * For now, these functions have a dm_ prefix since they conflict with 5443669e0e7SSimon Glass * existing names. 5453669e0e7SSimon Glass * 5463669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5473669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5483669e0e7SSimon Glass * @return GPIO value (0 for inactive, 1 for active) or -ve on error 5493669e0e7SSimon Glass */ 55017c43f1aSSimon Glass int dm_gpio_get_value(const struct gpio_desc *desc); 5513669e0e7SSimon Glass 55217c43f1aSSimon Glass int dm_gpio_set_value(const struct gpio_desc *desc, int value); 5533669e0e7SSimon Glass 5543669e0e7SSimon Glass /** 555*53ecdfb9Smario.six@gdsys.cc * dm_gpio_get_open_drain() - Check if open-drain-mode of a GPIO is active 556*53ecdfb9Smario.six@gdsys.cc * 557*53ecdfb9Smario.six@gdsys.cc * This checks if open-drain-mode for a GPIO is enabled or not. This method is 558*53ecdfb9Smario.six@gdsys.cc * optional. 559*53ecdfb9Smario.six@gdsys.cc * 560*53ecdfb9Smario.six@gdsys.cc * @desc: GPIO description containing device, offset and flags, 561*53ecdfb9Smario.six@gdsys.cc * previously returned by gpio_request_by_name() 562*53ecdfb9Smario.six@gdsys.cc * @return Value of open drain mode for GPIO (0 for inactive, 1 for active) or 563*53ecdfb9Smario.six@gdsys.cc * -ve on error 564*53ecdfb9Smario.six@gdsys.cc */ 565*53ecdfb9Smario.six@gdsys.cc int dm_gpio_get_open_drain(struct gpio_desc *desc); 566*53ecdfb9Smario.six@gdsys.cc 567*53ecdfb9Smario.six@gdsys.cc /** 568*53ecdfb9Smario.six@gdsys.cc * dm_gpio_set_open_drain() - Switch open-drain-mode of a GPIO on or off 569*53ecdfb9Smario.six@gdsys.cc * 570*53ecdfb9Smario.six@gdsys.cc * This enables or disables open-drain mode for a GPIO. This method is 571*53ecdfb9Smario.six@gdsys.cc * optional; if the driver does not support it, nothing happens when the method 572*53ecdfb9Smario.six@gdsys.cc * is called. 573*53ecdfb9Smario.six@gdsys.cc * 574*53ecdfb9Smario.six@gdsys.cc * In open-drain mode, instead of actively driving the output (Push-pull 575*53ecdfb9Smario.six@gdsys.cc * output), the GPIO's pin is connected to the collector (for a NPN transistor) 576*53ecdfb9Smario.six@gdsys.cc * or the drain (for a MOSFET) of a transistor, respectively. The pin then 577*53ecdfb9Smario.six@gdsys.cc * either forms an open circuit or a connection to ground, depending on the 578*53ecdfb9Smario.six@gdsys.cc * state of the transistor. 579*53ecdfb9Smario.six@gdsys.cc * 580*53ecdfb9Smario.six@gdsys.cc * @desc: GPIO description containing device, offset and flags, 581*53ecdfb9Smario.six@gdsys.cc * previously returned by gpio_request_by_name() 582*53ecdfb9Smario.six@gdsys.cc * @return 0 if OK, -ve on error 583*53ecdfb9Smario.six@gdsys.cc */ 584*53ecdfb9Smario.six@gdsys.cc int dm_gpio_set_open_drain(struct gpio_desc *desc, int value); 585*53ecdfb9Smario.six@gdsys.cc 586*53ecdfb9Smario.six@gdsys.cc /** 5873669e0e7SSimon Glass * dm_gpio_set_dir() - Set the direction for a GPIO 5883669e0e7SSimon Glass * 5893669e0e7SSimon Glass * This sets up the direction according tot the provided flags. It will do 5903669e0e7SSimon Glass * nothing unless the direction is actually specified. 5913669e0e7SSimon Glass * 5923669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5933669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5943669e0e7SSimon Glass * @return 0 if OK, -ve on error 5953669e0e7SSimon Glass */ 5963669e0e7SSimon Glass int dm_gpio_set_dir(struct gpio_desc *desc); 5973669e0e7SSimon Glass 5983669e0e7SSimon Glass /** 5993669e0e7SSimon Glass * dm_gpio_set_dir_flags() - Set direction using specific flags 6003669e0e7SSimon Glass * 6013669e0e7SSimon Glass * This is like dm_gpio_set_dir() except that the flags value is provided 6023669e0e7SSimon Glass * instead of being used from desc->flags. This is needed because in many 6033669e0e7SSimon Glass * cases the GPIO description does not include direction information. 6043669e0e7SSimon Glass * Note that desc->flags is updated by this function. 6053669e0e7SSimon Glass * 6063669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 6073669e0e7SSimon Glass * previously returned by gpio_request_by_name() 6083669e0e7SSimon Glass * @flags: New flags to use 6093669e0e7SSimon Glass * @return 0 if OK, -ve on error, in which case desc->flags is not updated 6103669e0e7SSimon Glass */ 6113669e0e7SSimon Glass int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags); 6123669e0e7SSimon Glass 6133669e0e7SSimon Glass /** 6143669e0e7SSimon Glass * gpio_get_number() - Get the global GPIO number of a GPIO 6153669e0e7SSimon Glass * 616d9a607f2SMasahiro Yamada * This should only be used for debugging or interest. It returns the number 6173669e0e7SSimon Glass * that should be used for gpio_get_value() etc. to access this GPIO. 6183669e0e7SSimon Glass * 6193669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 6203669e0e7SSimon Glass * previously returned by gpio_request_by_name() 6213669e0e7SSimon Glass * @return GPIO number, or -ve if not found 6223669e0e7SSimon Glass */ 62317c43f1aSSimon Glass int gpio_get_number(const struct gpio_desc *desc); 6243669e0e7SSimon Glass 6255f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */ 626