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 * 4594740e47SNikita Kiryanov * @param gp 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 */ 118ae7123f8SSimon Glass #define GPIOD_IS_IN (1 << 2) /* GPIO is an output */ 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 /** 1303669e0e7SSimon Glass * dm_gpio_is_valid() - Check if a GPIO is gpio_is_valie 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 */ 1363669e0e7SSimon Glass static inline bool dm_gpio_is_valid(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 1706449a506SSimon Glass * @namep: If non-NULL, this is set to the nane 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 1896449a506SSimon Glass * @namep: If non-NULL, this is set to the nane 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 /** 21096495d90SSimon Glass * struct struct dm_gpio_ops - Driver model GPIO operations 21196495d90SSimon Glass * 21296495d90SSimon Glass * Refer to functions above for description. These function largely copy 21396495d90SSimon Glass * the old API. 21496495d90SSimon Glass * 21596495d90SSimon Glass * This is trying to be close to Linux GPIO API. Once the U-Boot uses the 21696495d90SSimon Glass * new DM GPIO API, this should be really easy to flip over to the Linux 21796495d90SSimon Glass * GPIO API-alike interface. 21896495d90SSimon Glass * 21925ca385dSMarcel Ziswiler * Also it would be useful to standardise additional functions like 22096495d90SSimon Glass * pullup, slew rate and drive strength. 22196495d90SSimon Glass * 22296495d90SSimon Glass * gpio_request)( and gpio_free() are optional - if NULL then they will 22396495d90SSimon Glass * not be called. 22496495d90SSimon Glass * 22596495d90SSimon Glass * Note that @offset is the offset from the base GPIO of the device. So 22696495d90SSimon Glass * offset 0 is the device's first GPIO and offset o-1 is the last GPIO, 22796495d90SSimon Glass * where o is the number of GPIO lines controlled by the device. A device 22896495d90SSimon Glass * is typically used to control a single bank of GPIOs. Within complex 22996495d90SSimon Glass * SoCs there may be many banks and therefore many devices all referring 23096495d90SSimon Glass * to the different IO addresses within the SoC. 23196495d90SSimon Glass * 23225ca385dSMarcel Ziswiler * The uclass combines all GPIO devices together to provide a consistent 23396495d90SSimon Glass * numbering from 0 to n-1, where n is the number of GPIOs in total across 23496495d90SSimon Glass * all devices. Be careful not to confuse offset with gpio in the parameters. 23596495d90SSimon Glass */ 23696495d90SSimon Glass struct dm_gpio_ops { 23754c5d08aSHeiko Schocher int (*request)(struct udevice *dev, unsigned offset, const char *label); 23854c5d08aSHeiko Schocher int (*free)(struct udevice *dev, unsigned offset); 23954c5d08aSHeiko Schocher int (*direction_input)(struct udevice *dev, unsigned offset); 24054c5d08aSHeiko Schocher int (*direction_output)(struct udevice *dev, unsigned offset, 24196495d90SSimon Glass int value); 24254c5d08aSHeiko Schocher int (*get_value)(struct udevice *dev, unsigned offset); 24354c5d08aSHeiko Schocher int (*set_value)(struct udevice *dev, unsigned offset, int value); 24489e64054SSimon Glass /** 24589e64054SSimon Glass * get_function() Get the GPIO function 24689e64054SSimon Glass * 24789e64054SSimon Glass * @dev: Device to check 24889e64054SSimon Glass * @offset: GPIO offset within that device 24989e64054SSimon Glass * @return current function - GPIOF_... 25089e64054SSimon Glass */ 25154c5d08aSHeiko Schocher int (*get_function)(struct udevice *dev, unsigned offset); 2520dac4d51SSimon Glass 2530dac4d51SSimon Glass /** 2540dac4d51SSimon Glass * xlate() - Translate phandle arguments into a GPIO description 2550dac4d51SSimon Glass * 2560dac4d51SSimon Glass * This function should set up the fields in desc according to the 2570dac4d51SSimon Glass * information in the arguments. The uclass will have set up: 2580dac4d51SSimon Glass * 2590dac4d51SSimon Glass * @desc->dev to @dev 2600dac4d51SSimon Glass * @desc->flags to 0 2610dac4d51SSimon Glass * @desc->offset to the value of the first argument in args, if any, 2620dac4d51SSimon Glass * otherwise -1 (which is invalid) 2630dac4d51SSimon Glass * 2640dac4d51SSimon Glass * This method is optional so if the above defaults suit it can be 2650dac4d51SSimon Glass * omitted. Typical behaviour is to set up the GPIOD_ACTIVE_LOW flag 2660dac4d51SSimon Glass * in desc->flags. 2670dac4d51SSimon Glass * 2680dac4d51SSimon Glass * Note that @dev is passed in as a parameter to follow driver model 2690dac4d51SSimon Glass * uclass conventions, even though it is already available as 2700dac4d51SSimon Glass * desc->dev. 2710dac4d51SSimon Glass * 2720dac4d51SSimon Glass * @dev: GPIO device 2730dac4d51SSimon Glass * @desc: Place to put GPIO description 2740dac4d51SSimon Glass * @args: Arguments provided in descripion 2750dac4d51SSimon Glass * @return 0 if OK, -ve on error 2760dac4d51SSimon Glass */ 2770dac4d51SSimon Glass int (*xlate)(struct udevice *dev, struct gpio_desc *desc, 2780dac4d51SSimon Glass struct fdtdec_phandle_args *args); 27996495d90SSimon Glass }; 28096495d90SSimon Glass 28196495d90SSimon Glass /** 28296495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass 28396495d90SSimon Glass * 28496495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering 28525ca385dSMarcel Ziswiler * scheme. To do this it maintains some private information about each 28696495d90SSimon Glass * device. 28796495d90SSimon Glass * 28896495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe 28996495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler. 29096495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs 29196495d90SSimon Glass * it contains. 29296495d90SSimon Glass * 29396495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called 29496495d90SSimon Glass * 'A0', 'A1', etc. 29596495d90SSimon Glass * @gpio_count: Number of GPIOs in this device 29696495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device 29796495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that 29896495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0. 299b892d127SSimon Glass * @name: Array of pointers to the name for each GPIO in this bank. The 300b892d127SSimon Glass * value of the pointer will be NULL if the GPIO has not been claimed. 30196495d90SSimon Glass */ 30296495d90SSimon Glass struct gpio_dev_priv { 30396495d90SSimon Glass const char *bank_name; 30496495d90SSimon Glass unsigned gpio_count; 30596495d90SSimon Glass unsigned gpio_base; 306b892d127SSimon Glass char **name; 30796495d90SSimon Glass }; 30896495d90SSimon Glass 30996495d90SSimon Glass /* Access the GPIO operations for a device */ 31096495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops) 31196495d90SSimon Glass 31296495d90SSimon Glass /** 31396495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device 31496495d90SSimon Glass * 31596495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number 31696495d90SSimon Glass * of GPIOs it controls. 31796495d90SSimon Glass * 31896495d90SSimon Glass * @dev: Device to look up 31996495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank 32096495d90SSimon Glass * @return bank name of this device 32196495d90SSimon Glass */ 32254c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count); 32396495d90SSimon Glass 32496495d90SSimon Glass /** 32596495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details 32696495d90SSimon Glass * 32796495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO 32896495d90SSimon Glass * number. 32996495d90SSimon Glass * 33096495d90SSimon Glass * @name: GPIO name to look up 33196495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO 33296495d90SSimon Glass * @offsetp: Returns the offset number within this device 33396495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0 33496495d90SSimon Glass */ 33554c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp, 33696495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop); 33796495d90SSimon Glass 338e5901c94SSimon Glass /** 339*962f5cafSSimon Glass * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int 340e5901c94SSimon Glass * 341e5901c94SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 342e5901c94SSimon Glass * etc. then returns the resulting integer. 343e5901c94SSimon Glass * 344e5901c94SSimon Glass * @gpio_list: List of GPIOs to collect 345*962f5cafSSimon Glass * @return resulting integer value, or -ve on error 346e5901c94SSimon Glass */ 347*962f5cafSSimon Glass int gpio_get_values_as_int(const int *gpio_list); 348*962f5cafSSimon Glass 349*962f5cafSSimon Glass /** 350*962f5cafSSimon Glass * gpio_claim_vector() - claim a number of GPIOs for input 351*962f5cafSSimon Glass * 352*962f5cafSSimon Glass * @gpio_num_array: array of gpios to claim, terminated by -1 353*962f5cafSSimon Glass * @fmt: format string for GPIO names, e.g. "board_id%d" 354*962f5cafSSimon Glass * @return 0 if OK, -ve on error 355*962f5cafSSimon Glass */ 356*962f5cafSSimon Glass int gpio_claim_vector(const int *gpio_num_array, const char *fmt); 3575b5ac645SJeroen Hofstee 3583669e0e7SSimon Glass /** 3593669e0e7SSimon Glass * gpio_request_by_name() - Locate and request a GPIO by name 3603669e0e7SSimon Glass * 3613669e0e7SSimon Glass * This operates by looking up the given list name in the device (device 3623669e0e7SSimon Glass * tree property) and requesting the GPIO for use. The property must exist 3633669e0e7SSimon Glass * in @dev's node. 3643669e0e7SSimon Glass * 3653669e0e7SSimon Glass * Use @flags to specify whether the GPIO should be an input or output. In 3663669e0e7SSimon Glass * principle this can also come from the device tree binding but most 3673669e0e7SSimon Glass * bindings don't provide this information. Specifically, when the GPIO uclass 3683669e0e7SSimon Glass * calls the xlate() method, it can return default flags, which are then 3693669e0e7SSimon Glass * ORed with this @flags. 3703669e0e7SSimon Glass * 3713669e0e7SSimon Glass * If we find that requesting the GPIO is not always needed we could add a 3723669e0e7SSimon Glass * new function or a new GPIOD_NO_REQUEST flag. 3733669e0e7SSimon Glass * 3743669e0e7SSimon Glass * At present driver model has no reference counting so if one device 3753669e0e7SSimon Glass * requests a GPIO which subsequently is unbound, the @desc->dev pointer 3763669e0e7SSimon Glass * will be invalid. However this will only happen if the GPIO device is 3773669e0e7SSimon Glass * unbound, not if it is removed, so this seems like a reasonable limitation 3783669e0e7SSimon Glass * for now. There is no real use case for unbinding drivers in normal 3793669e0e7SSimon Glass * operation. 3803669e0e7SSimon Glass * 3813669e0e7SSimon Glass * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in 3823669e0e7SSimon Glass * generate terms and each specific device may add additional details in 3833669e0e7SSimon Glass * a binding file in the same directory. 3843669e0e7SSimon Glass * 3853669e0e7SSimon Glass * @dev: Device requesting the GPIO 3863669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 3873669e0e7SSimon Glass * @index: Index number of the GPIO in that list use request (0=first) 3883669e0e7SSimon Glass * @desc: Returns GPIO description information. If there is no such 3893669e0e7SSimon Glass * GPIO, dev->dev will be NULL. 3903669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 3913669e0e7SSimon Glass * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is 3923669e0e7SSimon Glass * something wrong with the list, or other -ve for another error (e.g. 3933669e0e7SSimon Glass * -EBUSY if a GPIO was already requested) 3943669e0e7SSimon Glass */ 3953669e0e7SSimon Glass int gpio_request_by_name(struct udevice *dev, const char *list_name, 3963669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 3973669e0e7SSimon Glass 3983669e0e7SSimon Glass /** 3993669e0e7SSimon Glass * gpio_request_list_by_name() - Request a list of GPIOs 4003669e0e7SSimon Glass * 4013669e0e7SSimon Glass * Reads all the GPIOs from a list and requetss them. See 4023669e0e7SSimon Glass * gpio_request_by_name() for additional details. Lists should not be 4033669e0e7SSimon Glass * misused to hold unrelated or optional GPIOs. They should only be used 4043669e0e7SSimon Glass * for things like parallel data lines. A zero phandle terminates the list 4053669e0e7SSimon Glass * the list. 4063669e0e7SSimon Glass * 4073669e0e7SSimon Glass * This function will either succeed, and request all GPIOs in the list, or 4083669e0e7SSimon Glass * fail and request none (it will free already-requested GPIOs in case of 4093669e0e7SSimon Glass * an error part-way through). 4103669e0e7SSimon Glass * 4113669e0e7SSimon Glass * @dev: Device requesting the GPIO 4123669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4133669e0e7SSimon Glass * @desc_list: Returns a list of GPIO description information 4143669e0e7SSimon Glass * @max_count: Maximum number of GPIOs to return (@desc_list must be at least 4153669e0e7SSimon Glass * this big) 4163669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...) 4173669e0e7SSimon Glass * @return number of GPIOs requested, or -ve on error 4183669e0e7SSimon Glass */ 4193669e0e7SSimon Glass int gpio_request_list_by_name(struct udevice *dev, const char *list_name, 4203669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 4213669e0e7SSimon Glass int flags); 4223669e0e7SSimon Glass 4233669e0e7SSimon Glass /** 4243669e0e7SSimon Glass * gpio_get_list_count() - Returns the number of GPIOs in a list 4253669e0e7SSimon Glass * 4263669e0e7SSimon Glass * Counts the GPIOs in a list. See gpio_request_by_name() for additional 4273669e0e7SSimon Glass * details. 4283669e0e7SSimon Glass * 4293669e0e7SSimon Glass * @dev: Device requesting the GPIO 4303669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios") 4313669e0e7SSimon Glass * @return number of GPIOs (0 for an empty property) or -ENOENT if the list 4323669e0e7SSimon Glass * does not exist 4333669e0e7SSimon Glass */ 4343669e0e7SSimon Glass int gpio_get_list_count(struct udevice *dev, const char *list_name); 4353669e0e7SSimon Glass 4363669e0e7SSimon Glass /** 4373669e0e7SSimon Glass * gpio_request_by_name_nodev() - request GPIOs without a device 4383669e0e7SSimon Glass * 4393669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4403669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4413669e0e7SSimon Glass */ 4423669e0e7SSimon Glass int gpio_request_by_name_nodev(const void *blob, int node, 4433669e0e7SSimon Glass const char *list_name, 4443669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags); 4453669e0e7SSimon Glass 4463669e0e7SSimon Glass /** 4473669e0e7SSimon Glass * gpio_request_list_by_name_nodev() - request GPIOs without a device 4483669e0e7SSimon Glass * 4493669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a 4503669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4513669e0e7SSimon Glass */ 4523669e0e7SSimon Glass int gpio_request_list_by_name_nodev(const void *blob, int node, 4533669e0e7SSimon Glass const char *list_name, 4543669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count, 4553669e0e7SSimon Glass int flags); 4563669e0e7SSimon Glass 4573669e0e7SSimon Glass /** 4583669e0e7SSimon Glass * dm_gpio_free() - Free a single GPIO 4593669e0e7SSimon Glass * 4603669e0e7SSimon Glass * This frees a single GPIOs previously returned from gpio_request_by_name(). 4613669e0e7SSimon Glass * 4623669e0e7SSimon Glass * @dev: Device which requested the GPIO 4633669e0e7SSimon Glass * @desc: GPIO to free 4643669e0e7SSimon Glass * @return 0 if OK, -ve on error 4653669e0e7SSimon Glass */ 4663669e0e7SSimon Glass int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc); 4673669e0e7SSimon Glass 4683669e0e7SSimon Glass /** 4693669e0e7SSimon Glass * gpio_free_list() - Free a list of GPIOs 4703669e0e7SSimon Glass * 4713669e0e7SSimon Glass * This frees a list of GPIOs previously returned from 4723669e0e7SSimon Glass * gpio_request_list_by_name(). 4733669e0e7SSimon Glass * 4743669e0e7SSimon Glass * @dev: Device which requested the GPIOs 4753669e0e7SSimon Glass * @desc: List of GPIOs to free 4763669e0e7SSimon Glass * @count: Number of GPIOs in the list 4773669e0e7SSimon Glass * @return 0 if OK, -ve on error 4783669e0e7SSimon Glass */ 4793669e0e7SSimon Glass int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count); 4803669e0e7SSimon Glass 4813669e0e7SSimon Glass /** 4823669e0e7SSimon Glass * gpio_free_list_nodev() - free GPIOs without a device 4833669e0e7SSimon Glass * 4843669e0e7SSimon Glass * This is a version of gpio_free_list() that does not use a 4853669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model 4863669e0e7SSimon Glass */ 4873669e0e7SSimon Glass int gpio_free_list_nodev(struct gpio_desc *desc, int count); 4883669e0e7SSimon Glass 4893669e0e7SSimon Glass /** 4903669e0e7SSimon Glass * dm_gpio_get_value() - Get the value of a GPIO 4913669e0e7SSimon Glass * 4923669e0e7SSimon Glass * This is the driver model version of the existing gpio_get_value() function 4933669e0e7SSimon Glass * and should be used instead of that. 4943669e0e7SSimon Glass * 4953669e0e7SSimon Glass * For now, these functions have a dm_ prefix since they conflict with 4963669e0e7SSimon Glass * existing names. 4973669e0e7SSimon Glass * 4983669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 4993669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5003669e0e7SSimon Glass * @return GPIO value (0 for inactive, 1 for active) or -ve on error 5013669e0e7SSimon Glass */ 5023669e0e7SSimon Glass int dm_gpio_get_value(struct gpio_desc *desc); 5033669e0e7SSimon Glass 5043669e0e7SSimon Glass int dm_gpio_set_value(struct gpio_desc *desc, int value); 5053669e0e7SSimon Glass 5063669e0e7SSimon Glass /** 5073669e0e7SSimon Glass * dm_gpio_set_dir() - Set the direction for a GPIO 5083669e0e7SSimon Glass * 5093669e0e7SSimon Glass * This sets up the direction according tot the provided flags. It will do 5103669e0e7SSimon Glass * nothing unless the direction is actually specified. 5113669e0e7SSimon Glass * 5123669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5133669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5143669e0e7SSimon Glass * @return 0 if OK, -ve on error 5153669e0e7SSimon Glass */ 5163669e0e7SSimon Glass int dm_gpio_set_dir(struct gpio_desc *desc); 5173669e0e7SSimon Glass 5183669e0e7SSimon Glass /** 5193669e0e7SSimon Glass * dm_gpio_set_dir_flags() - Set direction using specific flags 5203669e0e7SSimon Glass * 5213669e0e7SSimon Glass * This is like dm_gpio_set_dir() except that the flags value is provided 5223669e0e7SSimon Glass * instead of being used from desc->flags. This is needed because in many 5233669e0e7SSimon Glass * cases the GPIO description does not include direction information. 5243669e0e7SSimon Glass * Note that desc->flags is updated by this function. 5253669e0e7SSimon Glass * 5263669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5273669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5283669e0e7SSimon Glass * @flags: New flags to use 5293669e0e7SSimon Glass * @return 0 if OK, -ve on error, in which case desc->flags is not updated 5303669e0e7SSimon Glass */ 5313669e0e7SSimon Glass int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags); 5323669e0e7SSimon Glass 5333669e0e7SSimon Glass /** 5343669e0e7SSimon Glass * gpio_get_number() - Get the global GPIO number of a GPIO 5353669e0e7SSimon Glass * 5363669e0e7SSimon Glass * This should only be used for debugging or interest. It returns the nummber 5373669e0e7SSimon Glass * that should be used for gpio_get_value() etc. to access this GPIO. 5383669e0e7SSimon Glass * 5393669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags, 5403669e0e7SSimon Glass * previously returned by gpio_request_by_name() 5413669e0e7SSimon Glass * @return GPIO number, or -ve if not found 5423669e0e7SSimon Glass */ 5433669e0e7SSimon Glass int gpio_get_number(struct gpio_desc *desc); 5443669e0e7SSimon Glass 5455f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */ 546