1863fbf49SAnton Vorontsov /* 2863fbf49SAnton Vorontsov * OF helpers for the GPIO API 3863fbf49SAnton Vorontsov * 4863fbf49SAnton Vorontsov * Copyright (c) 2007-2008 MontaVista Software, Inc. 5863fbf49SAnton Vorontsov * 6863fbf49SAnton Vorontsov * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 7863fbf49SAnton Vorontsov * 8863fbf49SAnton Vorontsov * This program is free software; you can redistribute it and/or modify 9863fbf49SAnton Vorontsov * it under the terms of the GNU General Public License as published by 10863fbf49SAnton Vorontsov * the Free Software Foundation; either version 2 of the License, or 11863fbf49SAnton Vorontsov * (at your option) any later version. 12863fbf49SAnton Vorontsov */ 13863fbf49SAnton Vorontsov 14863fbf49SAnton Vorontsov #ifndef __LINUX_OF_GPIO_H 15863fbf49SAnton Vorontsov #define __LINUX_OF_GPIO_H 16863fbf49SAnton Vorontsov 17b908b53dSAnton Vorontsov #include <linux/compiler.h> 18b908b53dSAnton Vorontsov #include <linux/kernel.h> 19863fbf49SAnton Vorontsov #include <linux/errno.h> 2018ad7a61SWolfgang Grandegger #include <linux/gpio.h> 2115c9a0acSGrant Likely #include <linux/of.h> 22863fbf49SAnton Vorontsov 23b908b53dSAnton Vorontsov struct device_node; 24b908b53dSAnton Vorontsov 25b908b53dSAnton Vorontsov /* 26b908b53dSAnton Vorontsov * This is Linux-specific flags. By default controllers' and Linux' mapping 27b908b53dSAnton Vorontsov * match, but GPIO controllers are free to translate their own flags to 28b908b53dSAnton Vorontsov * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended. 29b908b53dSAnton Vorontsov */ 30b908b53dSAnton Vorontsov enum of_gpio_flags { 31b908b53dSAnton Vorontsov OF_GPIO_ACTIVE_LOW = 0x1, 3290b665f6SLaurent Pinchart OF_GPIO_SINGLE_ENDED = 0x2, 33b908b53dSAnton Vorontsov }; 34b908b53dSAnton Vorontsov 35863fbf49SAnton Vorontsov #ifdef CONFIG_OF_GPIO 36863fbf49SAnton Vorontsov 37863fbf49SAnton Vorontsov /* 38863fbf49SAnton Vorontsov * OF GPIO chip for memory mapped banks 39863fbf49SAnton Vorontsov */ 40863fbf49SAnton Vorontsov struct of_mm_gpio_chip { 41a19e3da5SAnton Vorontsov struct gpio_chip gc; 42863fbf49SAnton Vorontsov void (*save_regs)(struct of_mm_gpio_chip *mm_gc); 43863fbf49SAnton Vorontsov void __iomem *regs; 44863fbf49SAnton Vorontsov }; 45863fbf49SAnton Vorontsov 46863fbf49SAnton Vorontsov static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc) 47863fbf49SAnton Vorontsov { 48a19e3da5SAnton Vorontsov return container_of(gc, struct of_mm_gpio_chip, gc); 49863fbf49SAnton Vorontsov } 50863fbf49SAnton Vorontsov 51f01d9075SAlexandre Courbot extern int of_get_named_gpio_flags(struct device_node *np, 52a6b09191SJohn Bonesio const char *list_name, int index, enum of_gpio_flags *flags); 53a6b09191SJohn Bonesio 543208b0f0SLinus Walleij extern int of_mm_gpiochip_add_data(struct device_node *np, 553208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 563208b0f0SLinus Walleij void *data); 573208b0f0SLinus Walleij static inline int of_mm_gpiochip_add(struct device_node *np, 583208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc) 593208b0f0SLinus Walleij { 603208b0f0SLinus Walleij return of_mm_gpiochip_add_data(np, mm_gc, NULL); 613208b0f0SLinus Walleij } 62d621e8baSRicardo Ribalda Delgado extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc); 63594fa265SGrant Likely 6428355f81STomeu Vizoso extern int of_gpiochip_add(struct gpio_chip *gc); 65391c970cSAnton Vorontsov extern void of_gpiochip_remove(struct gpio_chip *gc); 6615c9a0acSGrant Likely extern int of_gpio_simple_xlate(struct gpio_chip *gc, 6715c9a0acSGrant Likely const struct of_phandle_args *gpiospec, 6815c9a0acSGrant Likely u32 *flags); 69594fa265SGrant Likely 70a19e3da5SAnton Vorontsov #else /* CONFIG_OF_GPIO */ 71863fbf49SAnton Vorontsov 72863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */ 73f01d9075SAlexandre Courbot static inline int of_get_named_gpio_flags(struct device_node *np, 74a6b09191SJohn Bonesio const char *list_name, int index, enum of_gpio_flags *flags) 75863fbf49SAnton Vorontsov { 76427e0dc5SArnd Bergmann if (flags) 77427e0dc5SArnd Bergmann *flags = 0; 78427e0dc5SArnd Bergmann 79f01d9075SAlexandre Courbot return -ENOSYS; 80863fbf49SAnton Vorontsov } 81863fbf49SAnton Vorontsov 823038bbdfSJamie Iles static inline int of_gpio_simple_xlate(struct gpio_chip *gc, 8315c9a0acSGrant Likely const struct of_phandle_args *gpiospec, 8415c9a0acSGrant Likely u32 *flags) 853038bbdfSJamie Iles { 863038bbdfSJamie Iles return -ENOSYS; 873038bbdfSJamie Iles } 883038bbdfSJamie Iles 8928355f81STomeu Vizoso static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } 90391c970cSAnton Vorontsov static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 91391c970cSAnton Vorontsov 92863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */ 93863fbf49SAnton Vorontsov 94b908b53dSAnton Vorontsov /** 95e80beb27SGrant Likely * of_gpio_named_count() - Count GPIOs for a device 96ff64abefSJean-Christophe PLAGNIOL-VILLARD * @np: device node to count GPIOs for 97e80beb27SGrant Likely * @propname: property name containing gpio specifier(s) 98ff64abefSJean-Christophe PLAGNIOL-VILLARD * 99ff64abefSJean-Christophe PLAGNIOL-VILLARD * The function returns the count of GPIOs specified for a node. 100e80beb27SGrant Likely * Note that the empty GPIO specifiers count too. Returns either 101e80beb27SGrant Likely * Number of gpios defined in property, 102e80beb27SGrant Likely * -EINVAL for an incorrectly formed gpios property, or 103e80beb27SGrant Likely * -ENOENT for a missing gpios property 104ff64abefSJean-Christophe PLAGNIOL-VILLARD * 105e80beb27SGrant Likely * Example: 106ff64abefSJean-Christophe PLAGNIOL-VILLARD * gpios = <0 107e80beb27SGrant Likely * &gpio1 1 2 108ff64abefSJean-Christophe PLAGNIOL-VILLARD * 0 109e80beb27SGrant Likely * &gpio2 3 4>; 110ff64abefSJean-Christophe PLAGNIOL-VILLARD * 111e80beb27SGrant Likely * The above example defines four GPIOs, two of which are not specified. 112e80beb27SGrant Likely * This function will return '4' 113ff64abefSJean-Christophe PLAGNIOL-VILLARD */ 114e80beb27SGrant Likely static inline int of_gpio_named_count(struct device_node *np, const char* propname) 115e80beb27SGrant Likely { 116e80beb27SGrant Likely return of_count_phandle_with_args(np, propname, "#gpio-cells"); 117e80beb27SGrant Likely } 118e80beb27SGrant Likely 119e80beb27SGrant Likely /** 120e80beb27SGrant Likely * of_gpio_count() - Count GPIOs for a device 121e80beb27SGrant Likely * @np: device node to count GPIOs for 122e80beb27SGrant Likely * 123e80beb27SGrant Likely * Same as of_gpio_named_count, but hard coded to use the 'gpios' property 124e80beb27SGrant Likely */ 125e80beb27SGrant Likely static inline int of_gpio_count(struct device_node *np) 126ff64abefSJean-Christophe PLAGNIOL-VILLARD { 127ff64abefSJean-Christophe PLAGNIOL-VILLARD return of_gpio_named_count(np, "gpios"); 128ff64abefSJean-Christophe PLAGNIOL-VILLARD } 129ff64abefSJean-Christophe PLAGNIOL-VILLARD 130a6b09191SJohn Bonesio static inline int of_get_gpio_flags(struct device_node *np, int index, 131a6b09191SJohn Bonesio enum of_gpio_flags *flags) 132a6b09191SJohn Bonesio { 133a6b09191SJohn Bonesio return of_get_named_gpio_flags(np, "gpios", index, flags); 134a6b09191SJohn Bonesio } 135a6b09191SJohn Bonesio 136a6b09191SJohn Bonesio /** 137a6b09191SJohn Bonesio * of_get_named_gpio() - Get a GPIO number to use with GPIO API 138a6b09191SJohn Bonesio * @np: device node to get GPIO from 139a6b09191SJohn Bonesio * @propname: Name of property containing gpio specifier(s) 140a6b09191SJohn Bonesio * @index: index of the GPIO 141a6b09191SJohn Bonesio * 142a6b09191SJohn Bonesio * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 143a6b09191SJohn Bonesio * value on the error condition. 144a6b09191SJohn Bonesio */ 145a6b09191SJohn Bonesio static inline int of_get_named_gpio(struct device_node *np, 146a6b09191SJohn Bonesio const char *propname, int index) 147a6b09191SJohn Bonesio { 148a6b09191SJohn Bonesio return of_get_named_gpio_flags(np, propname, index, NULL); 149a6b09191SJohn Bonesio } 150a6b09191SJohn Bonesio 151a6b09191SJohn Bonesio /** 152a6b09191SJohn Bonesio * of_get_gpio() - Get a GPIO number to use with GPIO API 153b908b53dSAnton Vorontsov * @np: device node to get GPIO from 154b908b53dSAnton Vorontsov * @index: index of the GPIO 155b908b53dSAnton Vorontsov * 156b908b53dSAnton Vorontsov * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 157b908b53dSAnton Vorontsov * value on the error condition. 158b908b53dSAnton Vorontsov */ 159b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index) 160b908b53dSAnton Vorontsov { 161b908b53dSAnton Vorontsov return of_get_gpio_flags(np, index, NULL); 162b908b53dSAnton Vorontsov } 163b908b53dSAnton Vorontsov 164863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */ 165