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 * 139d2cb8e8SSimon Glass * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined 149d2cb8e8SSimon Glass * by the SOC/architecture. 159d2cb8e8SSimon Glass * 169d2cb8e8SSimon Glass * Each GPIO can be an input or output. If an input then its value can 179d2cb8e8SSimon Glass * be read as 0 or 1. If an output then its value can be set to 0 or 1. 189d2cb8e8SSimon Glass * If you try to write an input then the value is undefined. If you try 199d2cb8e8SSimon Glass * to read an output, barring something very unusual, you will get 209d2cb8e8SSimon Glass * back the value of the output that you previously set. 219d2cb8e8SSimon Glass * 229d2cb8e8SSimon Glass * In some cases the operation may fail, for example if the GPIO number 239d2cb8e8SSimon Glass * is out of range, or the GPIO is not available because its pin is 249d2cb8e8SSimon Glass * being used by another function. In that case, functions may return 259d2cb8e8SSimon Glass * an error value of -1. 269d2cb8e8SSimon Glass */ 279d2cb8e8SSimon Glass 289d2cb8e8SSimon Glass /** 2925ca385dSMarcel Ziswiler * Request a GPIO. This should be called before any of the other functions 3025ca385dSMarcel Ziswiler * are used on this GPIO. 319d2cb8e8SSimon Glass * 32b892d127SSimon Glass * Note: With driver model, the label is allocated so there is no need for 33b892d127SSimon Glass * the caller to preserve it. 34b892d127SSimon Glass * 3594740e47SNikita Kiryanov * @param gp GPIO number 3694740e47SNikita Kiryanov * @param label User label for this GPIO 379d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 389d2cb8e8SSimon Glass */ 395f533aebSJoe Hershberger int gpio_request(unsigned gpio, const char *label); 405f533aebSJoe Hershberger 415f533aebSJoe Hershberger /** 425f533aebSJoe Hershberger * Stop using the GPIO. This function should not alter pin configuration. 435f533aebSJoe Hershberger * 445f533aebSJoe Hershberger * @param gpio GPIO number 455f533aebSJoe Hershberger * @return 0 if ok, -1 on error 465f533aebSJoe Hershberger */ 475f533aebSJoe Hershberger int gpio_free(unsigned gpio); 485f533aebSJoe Hershberger 495f533aebSJoe Hershberger /** 505f533aebSJoe Hershberger * Make a GPIO an input. 515f533aebSJoe Hershberger * 525f533aebSJoe Hershberger * @param gpio GPIO number 535f533aebSJoe Hershberger * @return 0 if ok, -1 on error 545f533aebSJoe Hershberger */ 555f533aebSJoe Hershberger int gpio_direction_input(unsigned gpio); 569d2cb8e8SSimon Glass 579d2cb8e8SSimon Glass /** 589d2cb8e8SSimon Glass * Make a GPIO an output, and set its value. 599d2cb8e8SSimon Glass * 605f533aebSJoe Hershberger * @param gpio GPIO number 619d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 629d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 639d2cb8e8SSimon Glass */ 645f533aebSJoe Hershberger int gpio_direction_output(unsigned gpio, int value); 659d2cb8e8SSimon Glass 669d2cb8e8SSimon Glass /** 679d2cb8e8SSimon Glass * Get a GPIO's value. This will work whether the GPIO is an input 689d2cb8e8SSimon Glass * or an output. 699d2cb8e8SSimon Glass * 705f533aebSJoe Hershberger * @param gpio GPIO number 719d2cb8e8SSimon Glass * @return 0 if low, 1 if high, -1 on error 729d2cb8e8SSimon Glass */ 735f533aebSJoe Hershberger int gpio_get_value(unsigned gpio); 749d2cb8e8SSimon Glass 759d2cb8e8SSimon Glass /** 765f533aebSJoe Hershberger * Set an output GPIO's value. The GPIO must already be an output or 779d2cb8e8SSimon Glass * this function may have no effect. 789d2cb8e8SSimon Glass * 795f533aebSJoe Hershberger * @param gpio GPIO number 809d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 819d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 829d2cb8e8SSimon Glass */ 835f533aebSJoe Hershberger int gpio_set_value(unsigned gpio, int value); 8496495d90SSimon Glass 8589e64054SSimon Glass /* State of a GPIO, as reported by get_function() */ 866449a506SSimon Glass enum gpio_func_t { 8796495d90SSimon Glass GPIOF_INPUT = 0, 8896495d90SSimon Glass GPIOF_OUTPUT, 8989e64054SSimon Glass GPIOF_UNUSED, /* Not claimed */ 9089e64054SSimon Glass GPIOF_UNKNOWN, /* Not known */ 9189e64054SSimon Glass GPIOF_FUNC, /* Not used as a GPIO */ 9289e64054SSimon Glass 9389e64054SSimon Glass GPIOF_COUNT, 9496495d90SSimon Glass }; 9596495d90SSimon Glass 9654c5d08aSHeiko Schocher struct udevice; 9796495d90SSimon Glass 98ae7123f8SSimon Glass struct gpio_desc { 99ae7123f8SSimon Glass struct udevice *dev; /* Device, NULL for invalid GPIO */ 100ae7123f8SSimon Glass unsigned long flags; 101ae7123f8SSimon Glass #define GPIOD_REQUESTED (1 << 0) /* Requested/claimed */ 102ae7123f8SSimon Glass #define GPIOD_IS_OUT (1 << 1) /* GPIO is an output */ 103ae7123f8SSimon Glass #define GPIOD_IS_IN (1 << 2) /* GPIO is an output */ 104ae7123f8SSimon Glass #define GPIOD_ACTIVE_LOW (1 << 3) /* value has active low */ 105ae7123f8SSimon Glass #define GPIOD_IS_OUT_ACTIVE (1 << 4) /* set output active */ 106ae7123f8SSimon Glass 107ae7123f8SSimon Glass uint offset; /* GPIO offset within the device */ 108ae7123f8SSimon Glass /* 109ae7123f8SSimon Glass * We could consider adding the GPIO label in here. Possibly we could 110ae7123f8SSimon Glass * use this structure for internal GPIO information. 111ae7123f8SSimon Glass */ 112ae7123f8SSimon Glass }; 113ae7123f8SSimon Glass 11496495d90SSimon Glass /** 1150757535aSSimon Glass * gpio_get_status() - get the current GPIO status as a string 1160757535aSSimon Glass * 1170757535aSSimon Glass * Obtain the current GPIO status as a string which can be presented to the 1180757535aSSimon Glass * user. A typical string is: 1190757535aSSimon Glass * 1200757535aSSimon Glass * "b4: in: 1 [x] sdmmc_cd" 1210757535aSSimon Glass * 1220757535aSSimon Glass * which means this is GPIO bank b, offset 4, currently set to input, current 1230757535aSSimon Glass * value 1, [x] means that it is requested and the owner is 'sdmmc_cd' 1240757535aSSimon Glass * 1250757535aSSimon Glass * @dev: Device to check 1260757535aSSimon Glass * @offset: Offset of device GPIO to check 1270757535aSSimon Glass * @buf: Place to put string 1280757535aSSimon Glass * @buffsize: Size of string including \0 1290757535aSSimon Glass */ 1300757535aSSimon Glass int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize); 1310757535aSSimon Glass 1320757535aSSimon Glass /** 1336449a506SSimon Glass * gpio_get_function() - get the current function for a GPIO pin 1346449a506SSimon Glass * 1356449a506SSimon Glass * Note this returns GPIOF_UNUSED if the GPIO is not requested. 1366449a506SSimon Glass * 1376449a506SSimon Glass * @dev: Device to check 1386449a506SSimon Glass * @offset: Offset of device GPIO to check 1396449a506SSimon Glass * @namep: If non-NULL, this is set to the nane given when the GPIO 1406449a506SSimon Glass * was requested, or -1 if it has not been requested 1416449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function, 1426449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid. 1436449a506SSimon Glass * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the 1446449a506SSimon Glass * function from enum gpio_func_t. 1456449a506SSimon Glass */ 1466449a506SSimon Glass int gpio_get_function(struct udevice *dev, int offset, const char **namep); 1476449a506SSimon Glass 1486449a506SSimon Glass /** 1496449a506SSimon Glass * gpio_get_raw_function() - get the current raw function for a GPIO pin 1506449a506SSimon Glass * 1516449a506SSimon Glass * Note this does not return GPIOF_UNUSED - it will always return the GPIO 1526449a506SSimon Glass * driver's view of a pin function, even if it is not correctly set up. 1536449a506SSimon Glass * 1546449a506SSimon Glass * @dev: Device to check 1556449a506SSimon Glass * @offset: Offset of device GPIO to check 1566449a506SSimon Glass * @namep: If non-NULL, this is set to the nane given when the GPIO 1576449a506SSimon Glass * was requested, or -1 if it has not been requested 1586449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function, 1596449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid. 1606449a506SSimon Glass * Otherwise returns the function from enum gpio_func_t. 1616449a506SSimon Glass */ 1626449a506SSimon Glass int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep); 1636449a506SSimon Glass 1646449a506SSimon Glass /** 165d44f597bSSimon Glass * gpio_requestf() - request a GPIO using a format string for the owner 166d44f597bSSimon Glass * 167d44f597bSSimon Glass * This is a helper function for gpio_request(). It allows you to provide 168d44f597bSSimon Glass * a printf()-format string for the GPIO owner. It calls gpio_request() with 169d44f597bSSimon Glass * the string that is created 170d44f597bSSimon Glass */ 171d44f597bSSimon Glass int gpio_requestf(unsigned gpio, const char *fmt, ...) 172d44f597bSSimon Glass __attribute__ ((format (__printf__, 2, 3))); 173d44f597bSSimon Glass 174*0dac4d51SSimon Glass struct fdtdec_phandle_args; 175*0dac4d51SSimon Glass 176d44f597bSSimon Glass /** 17796495d90SSimon Glass * struct struct dm_gpio_ops - Driver model GPIO operations 17896495d90SSimon Glass * 17996495d90SSimon Glass * Refer to functions above for description. These function largely copy 18096495d90SSimon Glass * the old API. 18196495d90SSimon Glass * 18296495d90SSimon Glass * This is trying to be close to Linux GPIO API. Once the U-Boot uses the 18396495d90SSimon Glass * new DM GPIO API, this should be really easy to flip over to the Linux 18496495d90SSimon Glass * GPIO API-alike interface. 18596495d90SSimon Glass * 18625ca385dSMarcel Ziswiler * Also it would be useful to standardise additional functions like 18796495d90SSimon Glass * pullup, slew rate and drive strength. 18896495d90SSimon Glass * 18996495d90SSimon Glass * gpio_request)( and gpio_free() are optional - if NULL then they will 19096495d90SSimon Glass * not be called. 19196495d90SSimon Glass * 19296495d90SSimon Glass * Note that @offset is the offset from the base GPIO of the device. So 19396495d90SSimon Glass * offset 0 is the device's first GPIO and offset o-1 is the last GPIO, 19496495d90SSimon Glass * where o is the number of GPIO lines controlled by the device. A device 19596495d90SSimon Glass * is typically used to control a single bank of GPIOs. Within complex 19696495d90SSimon Glass * SoCs there may be many banks and therefore many devices all referring 19796495d90SSimon Glass * to the different IO addresses within the SoC. 19896495d90SSimon Glass * 19925ca385dSMarcel Ziswiler * The uclass combines all GPIO devices together to provide a consistent 20096495d90SSimon Glass * numbering from 0 to n-1, where n is the number of GPIOs in total across 20196495d90SSimon Glass * all devices. Be careful not to confuse offset with gpio in the parameters. 20296495d90SSimon Glass */ 20396495d90SSimon Glass struct dm_gpio_ops { 20454c5d08aSHeiko Schocher int (*request)(struct udevice *dev, unsigned offset, const char *label); 20554c5d08aSHeiko Schocher int (*free)(struct udevice *dev, unsigned offset); 20654c5d08aSHeiko Schocher int (*direction_input)(struct udevice *dev, unsigned offset); 20754c5d08aSHeiko Schocher int (*direction_output)(struct udevice *dev, unsigned offset, 20896495d90SSimon Glass int value); 20954c5d08aSHeiko Schocher int (*get_value)(struct udevice *dev, unsigned offset); 21054c5d08aSHeiko Schocher int (*set_value)(struct udevice *dev, unsigned offset, int value); 21189e64054SSimon Glass /** 21289e64054SSimon Glass * get_function() Get the GPIO function 21389e64054SSimon Glass * 21489e64054SSimon Glass * @dev: Device to check 21589e64054SSimon Glass * @offset: GPIO offset within that device 21689e64054SSimon Glass * @return current function - GPIOF_... 21789e64054SSimon Glass */ 21854c5d08aSHeiko Schocher int (*get_function)(struct udevice *dev, unsigned offset); 219*0dac4d51SSimon Glass 220*0dac4d51SSimon Glass /** 221*0dac4d51SSimon Glass * xlate() - Translate phandle arguments into a GPIO description 222*0dac4d51SSimon Glass * 223*0dac4d51SSimon Glass * This function should set up the fields in desc according to the 224*0dac4d51SSimon Glass * information in the arguments. The uclass will have set up: 225*0dac4d51SSimon Glass * 226*0dac4d51SSimon Glass * @desc->dev to @dev 227*0dac4d51SSimon Glass * @desc->flags to 0 228*0dac4d51SSimon Glass * @desc->offset to the value of the first argument in args, if any, 229*0dac4d51SSimon Glass * otherwise -1 (which is invalid) 230*0dac4d51SSimon Glass * 231*0dac4d51SSimon Glass * This method is optional so if the above defaults suit it can be 232*0dac4d51SSimon Glass * omitted. Typical behaviour is to set up the GPIOD_ACTIVE_LOW flag 233*0dac4d51SSimon Glass * in desc->flags. 234*0dac4d51SSimon Glass * 235*0dac4d51SSimon Glass * Note that @dev is passed in as a parameter to follow driver model 236*0dac4d51SSimon Glass * uclass conventions, even though it is already available as 237*0dac4d51SSimon Glass * desc->dev. 238*0dac4d51SSimon Glass * 239*0dac4d51SSimon Glass * @dev: GPIO device 240*0dac4d51SSimon Glass * @desc: Place to put GPIO description 241*0dac4d51SSimon Glass * @args: Arguments provided in descripion 242*0dac4d51SSimon Glass * @return 0 if OK, -ve on error 243*0dac4d51SSimon Glass */ 244*0dac4d51SSimon Glass int (*xlate)(struct udevice *dev, struct gpio_desc *desc, 245*0dac4d51SSimon Glass struct fdtdec_phandle_args *args); 24696495d90SSimon Glass }; 24796495d90SSimon Glass 24896495d90SSimon Glass /** 24996495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass 25096495d90SSimon Glass * 25196495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering 25225ca385dSMarcel Ziswiler * scheme. To do this it maintains some private information about each 25396495d90SSimon Glass * device. 25496495d90SSimon Glass * 25596495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe 25696495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler. 25796495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs 25896495d90SSimon Glass * it contains. 25996495d90SSimon Glass * 26096495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called 26196495d90SSimon Glass * 'A0', 'A1', etc. 26296495d90SSimon Glass * @gpio_count: Number of GPIOs in this device 26396495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device 26496495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that 26596495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0. 266b892d127SSimon Glass * @name: Array of pointers to the name for each GPIO in this bank. The 267b892d127SSimon Glass * value of the pointer will be NULL if the GPIO has not been claimed. 26896495d90SSimon Glass */ 26996495d90SSimon Glass struct gpio_dev_priv { 27096495d90SSimon Glass const char *bank_name; 27196495d90SSimon Glass unsigned gpio_count; 27296495d90SSimon Glass unsigned gpio_base; 273b892d127SSimon Glass char **name; 27496495d90SSimon Glass }; 27596495d90SSimon Glass 27696495d90SSimon Glass /* Access the GPIO operations for a device */ 27796495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops) 27896495d90SSimon Glass 27996495d90SSimon Glass /** 28096495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device 28196495d90SSimon Glass * 28296495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number 28396495d90SSimon Glass * of GPIOs it controls. 28496495d90SSimon Glass * 28596495d90SSimon Glass * @dev: Device to look up 28696495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank 28796495d90SSimon Glass * @return bank name of this device 28896495d90SSimon Glass */ 28954c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count); 29096495d90SSimon Glass 29196495d90SSimon Glass /** 29296495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details 29396495d90SSimon Glass * 29496495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO 29596495d90SSimon Glass * number. 29696495d90SSimon Glass * 29796495d90SSimon Glass * @name: GPIO name to look up 29896495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO 29996495d90SSimon Glass * @offsetp: Returns the offset number within this device 30096495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0 30196495d90SSimon Glass */ 30254c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp, 30396495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop); 30496495d90SSimon Glass 305e5901c94SSimon Glass /** 306e5901c94SSimon Glass * get_gpios() - Turn the values of a list of GPIOs into an integer 307e5901c94SSimon Glass * 308e5901c94SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1, 309e5901c94SSimon Glass * etc. then returns the resulting integer. 310e5901c94SSimon Glass * 311e5901c94SSimon Glass * @gpio_list: List of GPIOs to collect 312e5901c94SSimon Glass * @return resulting integer value 313e5901c94SSimon Glass */ 314e5901c94SSimon Glass unsigned gpio_get_values_as_int(const int *gpio_list); 3155b5ac645SJeroen Hofstee 3165f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */ 317