18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * GPIO tools - utility helpers library for the GPIO tools
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Linus Walleij
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Portions copied from iio_utils and lssio:
88c2ecf20Sopenharmony_ci * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
98c2ecf20Sopenharmony_ci * Copyright (c) 2008 Jonathan Cameron
108c2ecf20Sopenharmony_ci * *
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#ifndef _GPIO_UTILS_H_
138c2ecf20Sopenharmony_ci#define _GPIO_UTILS_H_
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <stdbool.h>
168c2ecf20Sopenharmony_ci#include <string.h>
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline int check_prefix(const char *str, const char *prefix)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	return strlen(str) > strlen(prefix) &&
248c2ecf20Sopenharmony_ci		strncmp(str, prefix, strlen(prefix)) == 0;
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciint gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
288c2ecf20Sopenharmony_ci				 unsigned int num_lines, unsigned int flag,
298c2ecf20Sopenharmony_ci				 struct gpiohandle_data *data,
308c2ecf20Sopenharmony_ci				 const char *consumer_label);
318c2ecf20Sopenharmony_ciint gpiotools_release_linehandle(const int fd);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciint gpiotools_request_line(const char *device_name,
348c2ecf20Sopenharmony_ci			   unsigned int *lines,
358c2ecf20Sopenharmony_ci			   unsigned int num_lines,
368c2ecf20Sopenharmony_ci			   struct gpio_v2_line_config *config,
378c2ecf20Sopenharmony_ci			   const char *consumer);
388c2ecf20Sopenharmony_ciint gpiotools_set_values(const int fd, struct gpio_v2_line_values *values);
398c2ecf20Sopenharmony_ciint gpiotools_get_values(const int fd, struct gpio_v2_line_values *values);
408c2ecf20Sopenharmony_ciint gpiotools_release_line(const int fd);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciint gpiotools_get(const char *device_name, unsigned int line);
438c2ecf20Sopenharmony_ciint gpiotools_gets(const char *device_name, unsigned int *lines,
448c2ecf20Sopenharmony_ci		   unsigned int num_lines, unsigned int *values);
458c2ecf20Sopenharmony_ciint gpiotools_set(const char *device_name, unsigned int line,
468c2ecf20Sopenharmony_ci		  unsigned int value);
478c2ecf20Sopenharmony_ciint gpiotools_sets(const char *device_name, unsigned int *lines,
488c2ecf20Sopenharmony_ci		   unsigned int num_lines, unsigned int *values);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* helper functions for gpio_v2_line_values bits */
518c2ecf20Sopenharmony_cistatic inline void gpiotools_set_bit(__u64 *b, int n)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	*b |= _BITULL(n);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic inline void gpiotools_change_bit(__u64 *b, int n)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	*b ^= _BITULL(n);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic inline void gpiotools_clear_bit(__u64 *b, int n)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	*b &= ~_BITULL(n);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic inline int gpiotools_test_bit(__u64 b, int n)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	return !!(b & _BITULL(n));
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic inline void gpiotools_assign_bit(__u64 *b, int n, bool value)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	if (value)
748c2ecf20Sopenharmony_ci		gpiotools_set_bit(b, n);
758c2ecf20Sopenharmony_ci	else
768c2ecf20Sopenharmony_ci		gpiotools_clear_bit(b, n);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#endif /* _GPIO_UTILS_H_ */
80