Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * GPIO tools - utility helpers library for the GPIO tools
0004  *
0005  * Copyright (C) 2015 Linus Walleij
0006  *
0007  * Portions copied from iio_utils and lssio:
0008  * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
0009  * Copyright (c) 2008 Jonathan Cameron
0010  * *
0011  */
0012 #ifndef _GPIO_UTILS_H_
0013 #define _GPIO_UTILS_H_
0014 
0015 #include <stdbool.h>
0016 #include <string.h>
0017 #include <linux/types.h>
0018 
0019 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
0020 
0021 static inline int check_prefix(const char *str, const char *prefix)
0022 {
0023     return strlen(str) > strlen(prefix) &&
0024         strncmp(str, prefix, strlen(prefix)) == 0;
0025 }
0026 
0027 int gpiotools_request_line(const char *device_name,
0028                unsigned int *lines,
0029                unsigned int num_lines,
0030                struct gpio_v2_line_config *config,
0031                const char *consumer);
0032 int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values);
0033 int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values);
0034 int gpiotools_release_line(const int fd);
0035 
0036 int gpiotools_get(const char *device_name, unsigned int line);
0037 int gpiotools_gets(const char *device_name, unsigned int *lines,
0038            unsigned int num_lines, unsigned int *values);
0039 int gpiotools_set(const char *device_name, unsigned int line,
0040           unsigned int value);
0041 int gpiotools_sets(const char *device_name, unsigned int *lines,
0042            unsigned int num_lines, unsigned int *values);
0043 
0044 /* helper functions for gpio_v2_line_values bits */
0045 static inline void gpiotools_set_bit(__u64 *b, int n)
0046 {
0047     *b |= _BITULL(n);
0048 }
0049 
0050 static inline void gpiotools_change_bit(__u64 *b, int n)
0051 {
0052     *b ^= _BITULL(n);
0053 }
0054 
0055 static inline void gpiotools_clear_bit(__u64 *b, int n)
0056 {
0057     *b &= ~_BITULL(n);
0058 }
0059 
0060 static inline int gpiotools_test_bit(__u64 b, int n)
0061 {
0062     return !!(b & _BITULL(n));
0063 }
0064 
0065 static inline void gpiotools_assign_bit(__u64 *b, int n, bool value)
0066 {
0067     if (value)
0068         gpiotools_set_bit(b, n);
0069     else
0070         gpiotools_clear_bit(b, n);
0071 }
0072 
0073 #endif /* _GPIO_UTILS_H_ */