^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * GPIO tools - utility helpers library for the GPIO tools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 Linus Walleij
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Portions copied from iio_utils and lssio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (c) 2008 Jonathan Cameron
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifndef _GPIO_UTILS_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define _GPIO_UTILS_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static inline int check_prefix(const char *str, const char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return strlen(str) > strlen(prefix) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) strncmp(str, prefix, strlen(prefix)) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int num_lines, unsigned int flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct gpiohandle_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const char *consumer_label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int gpiotools_release_linehandle(const int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int gpiotools_request_line(const char *device_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int *lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int num_lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct gpio_v2_line_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) const char *consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int gpiotools_release_line(const int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int gpiotools_get(const char *device_name, unsigned int line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int gpiotools_gets(const char *device_name, unsigned int *lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int num_lines, unsigned int *values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int gpiotools_set(const char *device_name, unsigned int line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int gpiotools_sets(const char *device_name, unsigned int *lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int num_lines, unsigned int *values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* helper functions for gpio_v2_line_values bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static inline void gpiotools_set_bit(__u64 *b, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *b |= _BITULL(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static inline void gpiotools_change_bit(__u64 *b, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *b ^= _BITULL(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static inline void gpiotools_clear_bit(__u64 *b, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *b &= ~_BITULL(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static inline int gpiotools_test_bit(__u64 b, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return !!(b & _BITULL(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline void gpiotools_assign_bit(__u64 *b, int n, bool value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) gpiotools_set_bit(b, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) gpiotools_clear_bit(b, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #endif /* _GPIO_UTILS_H_ */