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> 21*15c9a0acSGrant 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, 32b908b53dSAnton Vorontsov }; 33b908b53dSAnton Vorontsov 34863fbf49SAnton Vorontsov #ifdef CONFIG_OF_GPIO 35863fbf49SAnton Vorontsov 36863fbf49SAnton Vorontsov /* 37863fbf49SAnton Vorontsov * OF GPIO chip for memory mapped banks 38863fbf49SAnton Vorontsov */ 39863fbf49SAnton Vorontsov struct of_mm_gpio_chip { 40a19e3da5SAnton Vorontsov struct gpio_chip gc; 41863fbf49SAnton Vorontsov void (*save_regs)(struct of_mm_gpio_chip *mm_gc); 42863fbf49SAnton Vorontsov void __iomem *regs; 43863fbf49SAnton Vorontsov }; 44863fbf49SAnton Vorontsov 45863fbf49SAnton Vorontsov static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc) 46863fbf49SAnton Vorontsov { 47a19e3da5SAnton Vorontsov return container_of(gc, struct of_mm_gpio_chip, gc); 48863fbf49SAnton Vorontsov } 49863fbf49SAnton Vorontsov 50a6b09191SJohn Bonesio extern int of_get_named_gpio_flags(struct device_node *np, 51a6b09191SJohn Bonesio const char *list_name, int index, enum of_gpio_flags *flags); 52a6b09191SJohn Bonesio 5374982092SAnton Vorontsov extern unsigned int of_gpio_count(struct device_node *np); 54b908b53dSAnton Vorontsov 55863fbf49SAnton Vorontsov extern int of_mm_gpiochip_add(struct device_node *np, 56863fbf49SAnton Vorontsov struct of_mm_gpio_chip *mm_gc); 57594fa265SGrant Likely 58391c970cSAnton Vorontsov extern void of_gpiochip_add(struct gpio_chip *gc); 59391c970cSAnton Vorontsov extern void of_gpiochip_remove(struct gpio_chip *gc); 60594fa265SGrant Likely extern struct gpio_chip *of_node_to_gpiochip(struct device_node *np); 61*15c9a0acSGrant Likely extern int of_gpio_simple_xlate(struct gpio_chip *gc, 62*15c9a0acSGrant Likely const struct of_phandle_args *gpiospec, 63*15c9a0acSGrant Likely u32 *flags); 64594fa265SGrant Likely 65a19e3da5SAnton Vorontsov #else /* CONFIG_OF_GPIO */ 66863fbf49SAnton Vorontsov 67863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */ 68a6b09191SJohn Bonesio static inline int of_get_named_gpio_flags(struct device_node *np, 69a6b09191SJohn Bonesio const char *list_name, int index, enum of_gpio_flags *flags) 70863fbf49SAnton Vorontsov { 71863fbf49SAnton Vorontsov return -ENOSYS; 72863fbf49SAnton Vorontsov } 73863fbf49SAnton Vorontsov 7474982092SAnton Vorontsov static inline unsigned int of_gpio_count(struct device_node *np) 7574982092SAnton Vorontsov { 7674982092SAnton Vorontsov return 0; 7774982092SAnton Vorontsov } 7874982092SAnton Vorontsov 793038bbdfSJamie Iles static inline int of_gpio_simple_xlate(struct gpio_chip *gc, 80*15c9a0acSGrant Likely const struct of_phandle_args *gpiospec, 81*15c9a0acSGrant Likely u32 *flags) 823038bbdfSJamie Iles { 833038bbdfSJamie Iles return -ENOSYS; 843038bbdfSJamie Iles } 853038bbdfSJamie Iles 86391c970cSAnton Vorontsov static inline void of_gpiochip_add(struct gpio_chip *gc) { } 87391c970cSAnton Vorontsov static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 88391c970cSAnton Vorontsov 89863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */ 90863fbf49SAnton Vorontsov 91b908b53dSAnton Vorontsov /** 92a6b09191SJohn Bonesio * of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API 93a6b09191SJohn Bonesio * @np: device node to get GPIO from 94a6b09191SJohn Bonesio * @index: index of the GPIO 95a6b09191SJohn Bonesio * @flags: a flags pointer to fill in 96a6b09191SJohn Bonesio * 97a6b09191SJohn Bonesio * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 98a6b09191SJohn Bonesio * value on the error condition. If @flags is not NULL the function also fills 99a6b09191SJohn Bonesio * in flags for the GPIO. 100a6b09191SJohn Bonesio */ 101a6b09191SJohn Bonesio static inline int of_get_gpio_flags(struct device_node *np, int index, 102a6b09191SJohn Bonesio enum of_gpio_flags *flags) 103a6b09191SJohn Bonesio { 104a6b09191SJohn Bonesio return of_get_named_gpio_flags(np, "gpios", index, flags); 105a6b09191SJohn Bonesio } 106a6b09191SJohn Bonesio 107a6b09191SJohn Bonesio /** 108a6b09191SJohn Bonesio * of_get_named_gpio() - Get a GPIO number to use with GPIO API 109a6b09191SJohn Bonesio * @np: device node to get GPIO from 110a6b09191SJohn Bonesio * @propname: Name of property containing gpio specifier(s) 111a6b09191SJohn Bonesio * @index: index of the GPIO 112a6b09191SJohn Bonesio * 113a6b09191SJohn Bonesio * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 114a6b09191SJohn Bonesio * value on the error condition. 115a6b09191SJohn Bonesio */ 116a6b09191SJohn Bonesio static inline int of_get_named_gpio(struct device_node *np, 117a6b09191SJohn Bonesio const char *propname, int index) 118a6b09191SJohn Bonesio { 119a6b09191SJohn Bonesio return of_get_named_gpio_flags(np, propname, index, NULL); 120a6b09191SJohn Bonesio } 121a6b09191SJohn Bonesio 122a6b09191SJohn Bonesio /** 123a6b09191SJohn Bonesio * of_get_gpio() - Get a GPIO number to use with GPIO API 124b908b53dSAnton Vorontsov * @np: device node to get GPIO from 125b908b53dSAnton Vorontsov * @index: index of the GPIO 126b908b53dSAnton Vorontsov * 127b908b53dSAnton Vorontsov * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 128b908b53dSAnton Vorontsov * value on the error condition. 129b908b53dSAnton Vorontsov */ 130b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index) 131b908b53dSAnton Vorontsov { 132b908b53dSAnton Vorontsov return of_get_gpio_flags(np, index, NULL); 133b908b53dSAnton Vorontsov } 134b908b53dSAnton Vorontsov 135863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */ 136