18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_GPIO_H 38c2ecf20Sopenharmony_ci#define _ASM_GENERIC_GPIO_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/types.h> 68c2ecf20Sopenharmony_ci#include <linux/errno.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/compiler.h> 118c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h> 128c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* Platforms may implement their GPIO interface with library code, 158c2ecf20Sopenharmony_ci * at a small performance cost for non-inlined operations and some 168c2ecf20Sopenharmony_ci * extra memory (for code and for per-GPIO table entries). 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * While the GPIO programming interface defines valid GPIO numbers 198c2ecf20Sopenharmony_ci * to be in the range 0..MAX_INT, this library restricts them to the 208c2ecf20Sopenharmony_ci * smaller range 0..ARCH_NR_GPIOS-1. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of 238c2ecf20Sopenharmony_ci * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is 248c2ecf20Sopenharmony_ci * actually an estimate of a board-specific value. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#ifndef ARCH_NR_GPIOS 288c2ecf20Sopenharmony_ci#if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0 298c2ecf20Sopenharmony_ci#define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO 308c2ecf20Sopenharmony_ci#else 318c2ecf20Sopenharmony_ci#define ARCH_NR_GPIOS 512 328c2ecf20Sopenharmony_ci#endif 338c2ecf20Sopenharmony_ci#endif 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* 368c2ecf20Sopenharmony_ci * "valid" GPIO numbers are nonnegative and may be passed to 378c2ecf20Sopenharmony_ci * setup routines like gpio_request(). only some valid numbers 388c2ecf20Sopenharmony_ci * can successfully be requested and used. 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * Invalid GPIO numbers are useful for indicating no-such-GPIO in 418c2ecf20Sopenharmony_ci * platform data and other tables. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic inline bool gpio_is_valid(int number) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci return number >= 0 && number < ARCH_NR_GPIOS; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct device; 508c2ecf20Sopenharmony_cistruct gpio; 518c2ecf20Sopenharmony_cistruct seq_file; 528c2ecf20Sopenharmony_cistruct module; 538c2ecf20Sopenharmony_cistruct device_node; 548c2ecf20Sopenharmony_cistruct gpio_desc; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* caller holds gpio_lock *OR* gpio is marked as requested */ 578c2ecf20Sopenharmony_cistatic inline struct gpio_chip *gpio_to_chip(unsigned gpio) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci return gpiod_to_chip(gpio_to_desc(gpio)); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* Always use the library code for GPIO management calls, 638c2ecf20Sopenharmony_ci * or when sleeping may be involved. 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_ciextern int gpio_request(unsigned gpio, const char *label); 668c2ecf20Sopenharmony_ciextern void gpio_free(unsigned gpio); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic inline int gpio_direction_input(unsigned gpio) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci return gpiod_direction_input(gpio_to_desc(gpio)); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_cistatic inline int gpio_direction_output(unsigned gpio, int value) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci return gpiod_direction_output_raw(gpio_to_desc(gpio), value); 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic inline int gpio_set_debounce(unsigned gpio, unsigned debounce) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci return gpiod_set_debounce(gpio_to_desc(gpio), debounce); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic inline int gpio_get_value_cansleep(unsigned gpio) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio)); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_cistatic inline void gpio_set_value_cansleep(unsigned gpio, int value) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* A platform's <asm/gpio.h> code may want to inline the I/O calls when 938c2ecf20Sopenharmony_ci * the GPIO is constant and refers to some always-present controller, 948c2ecf20Sopenharmony_ci * giving direct access to chip registers and tight bitbanging loops. 958c2ecf20Sopenharmony_ci */ 968c2ecf20Sopenharmony_cistatic inline int __gpio_get_value(unsigned gpio) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci return gpiod_get_raw_value(gpio_to_desc(gpio)); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_cistatic inline void __gpio_set_value(unsigned gpio, int value) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci return gpiod_set_raw_value(gpio_to_desc(gpio), value); 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic inline int __gpio_cansleep(unsigned gpio) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci return gpiod_cansleep(gpio_to_desc(gpio)); 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic inline int __gpio_to_irq(unsigned gpio) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci return gpiod_to_irq(gpio_to_desc(gpio)); 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ciextern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); 1168c2ecf20Sopenharmony_ciextern int gpio_request_array(const struct gpio *array, size_t num); 1178c2ecf20Sopenharmony_ciextern void gpio_free_array(const struct gpio *array, size_t num); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci/* 1208c2ecf20Sopenharmony_ci * A sysfs interface can be exported by individual drivers if they want, 1218c2ecf20Sopenharmony_ci * but more typically is configured entirely from userspace. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_cistatic inline int gpio_export(unsigned gpio, bool direction_may_change) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci return gpiod_export(gpio_to_desc(gpio), direction_may_change); 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic inline int gpio_export_link(struct device *dev, const char *name, 1298c2ecf20Sopenharmony_ci unsigned gpio) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci return gpiod_export_link(dev, name, gpio_to_desc(gpio)); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic inline void gpio_unexport(unsigned gpio) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci gpiod_unexport(gpio_to_desc(gpio)); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci#else /* !CONFIG_GPIOLIB */ 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci#include <linux/kernel.h> 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic inline bool gpio_is_valid(int number) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci /* only non-negative numbers are valid */ 1468c2ecf20Sopenharmony_ci return number >= 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci/* platforms that don't directly support access to GPIOs through I2C, SPI, 1508c2ecf20Sopenharmony_ci * or other blocking infrastructure can use these wrappers. 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic inline int gpio_cansleep(unsigned gpio) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci return 0; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic inline int gpio_get_value_cansleep(unsigned gpio) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci might_sleep(); 1618c2ecf20Sopenharmony_ci return __gpio_get_value(gpio); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic inline void gpio_set_value_cansleep(unsigned gpio, int value) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci might_sleep(); 1678c2ecf20Sopenharmony_ci __gpio_set_value(gpio, value); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci#endif /* !CONFIG_GPIOLIB */ 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_GPIO_H */ 173