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 /** 2994740e47SNikita Kiryanov * Request a gpio. This should be called before any of the other functions 3094740e47SNikita Kiryanov * are used on this gpio. 319d2cb8e8SSimon Glass * 3294740e47SNikita Kiryanov * @param gp GPIO number 3394740e47SNikita Kiryanov * @param label User label for this GPIO 349d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 359d2cb8e8SSimon Glass */ 365f533aebSJoe Hershberger int gpio_request(unsigned gpio, const char *label); 375f533aebSJoe Hershberger 385f533aebSJoe Hershberger /** 395f533aebSJoe Hershberger * Stop using the GPIO. This function should not alter pin configuration. 405f533aebSJoe Hershberger * 415f533aebSJoe Hershberger * @param gpio GPIO number 425f533aebSJoe Hershberger * @return 0 if ok, -1 on error 435f533aebSJoe Hershberger */ 445f533aebSJoe Hershberger int gpio_free(unsigned gpio); 455f533aebSJoe Hershberger 465f533aebSJoe Hershberger /** 475f533aebSJoe Hershberger * Make a GPIO an input. 485f533aebSJoe Hershberger * 495f533aebSJoe Hershberger * @param gpio GPIO number 505f533aebSJoe Hershberger * @return 0 if ok, -1 on error 515f533aebSJoe Hershberger */ 525f533aebSJoe Hershberger int gpio_direction_input(unsigned gpio); 539d2cb8e8SSimon Glass 549d2cb8e8SSimon Glass /** 559d2cb8e8SSimon Glass * Make a GPIO an output, and set its value. 569d2cb8e8SSimon Glass * 575f533aebSJoe Hershberger * @param gpio GPIO number 589d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 599d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 609d2cb8e8SSimon Glass */ 615f533aebSJoe Hershberger int gpio_direction_output(unsigned gpio, int value); 629d2cb8e8SSimon Glass 639d2cb8e8SSimon Glass /** 649d2cb8e8SSimon Glass * Get a GPIO's value. This will work whether the GPIO is an input 659d2cb8e8SSimon Glass * or an output. 669d2cb8e8SSimon Glass * 675f533aebSJoe Hershberger * @param gpio GPIO number 689d2cb8e8SSimon Glass * @return 0 if low, 1 if high, -1 on error 699d2cb8e8SSimon Glass */ 705f533aebSJoe Hershberger int gpio_get_value(unsigned gpio); 719d2cb8e8SSimon Glass 729d2cb8e8SSimon Glass /** 735f533aebSJoe Hershberger * Set an output GPIO's value. The GPIO must already be an output or 749d2cb8e8SSimon Glass * this function may have no effect. 759d2cb8e8SSimon Glass * 765f533aebSJoe Hershberger * @param gpio GPIO number 779d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high) 789d2cb8e8SSimon Glass * @return 0 if ok, -1 on error 799d2cb8e8SSimon Glass */ 805f533aebSJoe Hershberger int gpio_set_value(unsigned gpio, int value); 81*96495d90SSimon Glass 82*96495d90SSimon Glass /* State of a GPIO, as reported by get_state() */ 83*96495d90SSimon Glass enum { 84*96495d90SSimon Glass GPIOF_INPUT = 0, 85*96495d90SSimon Glass GPIOF_OUTPUT, 86*96495d90SSimon Glass GPIOF_UNKNOWN, 87*96495d90SSimon Glass }; 88*96495d90SSimon Glass 89*96495d90SSimon Glass struct device; 90*96495d90SSimon Glass 91*96495d90SSimon Glass /** 92*96495d90SSimon Glass * struct struct dm_gpio_ops - Driver model GPIO operations 93*96495d90SSimon Glass * 94*96495d90SSimon Glass * Refer to functions above for description. These function largely copy 95*96495d90SSimon Glass * the old API. 96*96495d90SSimon Glass * 97*96495d90SSimon Glass * This is trying to be close to Linux GPIO API. Once the U-Boot uses the 98*96495d90SSimon Glass * new DM GPIO API, this should be really easy to flip over to the Linux 99*96495d90SSimon Glass * GPIO API-alike interface. 100*96495d90SSimon Glass * 101*96495d90SSimon Glass * Akso it would be useful to standardise additional functions like 102*96495d90SSimon Glass * pullup, slew rate and drive strength. 103*96495d90SSimon Glass * 104*96495d90SSimon Glass * gpio_request)( and gpio_free() are optional - if NULL then they will 105*96495d90SSimon Glass * not be called. 106*96495d90SSimon Glass * 107*96495d90SSimon Glass * Note that @offset is the offset from the base GPIO of the device. So 108*96495d90SSimon Glass * offset 0 is the device's first GPIO and offset o-1 is the last GPIO, 109*96495d90SSimon Glass * where o is the number of GPIO lines controlled by the device. A device 110*96495d90SSimon Glass * is typically used to control a single bank of GPIOs. Within complex 111*96495d90SSimon Glass * SoCs there may be many banks and therefore many devices all referring 112*96495d90SSimon Glass * to the different IO addresses within the SoC. 113*96495d90SSimon Glass * 114*96495d90SSimon Glass * The uclass combines all GPIO devices togther to provide a consistent 115*96495d90SSimon Glass * numbering from 0 to n-1, where n is the number of GPIOs in total across 116*96495d90SSimon Glass * all devices. Be careful not to confuse offset with gpio in the parameters. 117*96495d90SSimon Glass */ 118*96495d90SSimon Glass struct dm_gpio_ops { 119*96495d90SSimon Glass int (*request)(struct device *dev, unsigned offset, const char *label); 120*96495d90SSimon Glass int (*free)(struct device *dev, unsigned offset); 121*96495d90SSimon Glass int (*direction_input)(struct device *dev, unsigned offset); 122*96495d90SSimon Glass int (*direction_output)(struct device *dev, unsigned offset, 123*96495d90SSimon Glass int value); 124*96495d90SSimon Glass int (*get_value)(struct device *dev, unsigned offset); 125*96495d90SSimon Glass int (*set_value)(struct device *dev, unsigned offset, int value); 126*96495d90SSimon Glass int (*get_function)(struct device *dev, unsigned offset); 127*96495d90SSimon Glass int (*get_state)(struct device *dev, unsigned offset, char *state, 128*96495d90SSimon Glass int maxlen); 129*96495d90SSimon Glass }; 130*96495d90SSimon Glass 131*96495d90SSimon Glass /** 132*96495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass 133*96495d90SSimon Glass * 134*96495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering 135*96495d90SSimon Glass * scheme. To do this it maintains some private information aobut each 136*96495d90SSimon Glass * device. 137*96495d90SSimon Glass * 138*96495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe 139*96495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler. 140*96495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs 141*96495d90SSimon Glass * it contains. 142*96495d90SSimon Glass * 143*96495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called 144*96495d90SSimon Glass * 'A0', 'A1', etc. 145*96495d90SSimon Glass * @gpio_count: Number of GPIOs in this device 146*96495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device 147*96495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that 148*96495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0. 149*96495d90SSimon Glass */ 150*96495d90SSimon Glass struct gpio_dev_priv { 151*96495d90SSimon Glass const char *bank_name; 152*96495d90SSimon Glass unsigned gpio_count; 153*96495d90SSimon Glass unsigned gpio_base; 154*96495d90SSimon Glass }; 155*96495d90SSimon Glass 156*96495d90SSimon Glass /* Access the GPIO operations for a device */ 157*96495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops) 158*96495d90SSimon Glass 159*96495d90SSimon Glass /** 160*96495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device 161*96495d90SSimon Glass * 162*96495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number 163*96495d90SSimon Glass * of GPIOs it controls. 164*96495d90SSimon Glass * 165*96495d90SSimon Glass * @dev: Device to look up 166*96495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank 167*96495d90SSimon Glass * @return bank name of this device 168*96495d90SSimon Glass */ 169*96495d90SSimon Glass const char *gpio_get_bank_info(struct device *dev, int *offset_count); 170*96495d90SSimon Glass 171*96495d90SSimon Glass /** 172*96495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details 173*96495d90SSimon Glass * 174*96495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO 175*96495d90SSimon Glass * number. 176*96495d90SSimon Glass * 177*96495d90SSimon Glass * @name: GPIO name to look up 178*96495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO 179*96495d90SSimon Glass * @offsetp: Returns the offset number within this device 180*96495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0 181*96495d90SSimon Glass */ 182*96495d90SSimon Glass int gpio_lookup_name(const char *name, struct device **devp, 183*96495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop); 184*96495d90SSimon Glass 1855f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */ 186