1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO tools - helpers library for the GPIO tools
5 * Copyright (C) 2015 Linus Walleij
6 * Copyright (C) 2016 Bamvor Jian Zhang
16 #include <sys/ioctl.h>
17 #include <linux/gpio.h>
18 #include "gpio-utils.h"
20 #define CONSUMER "gpio-utils"
23 * DOC: Operation of gpio
25 * Provide the api of gpiochip for chardev interface. There are two
26 * types of api. The first one provide as same function as each
27 * ioctl, including request and release for lines of gpio, read/write
28 * the value of gpio. If the user want to do lots of read and write of
29 * lines of gpio, user should use this type of api.
31 * The second one provide the easy to use api for user. Each of the
32 * following api will request gpio lines, do the operation and then
33 * release these lines.
37 * gpiotools_request_line() - request gpio lines in a gpiochip
38 * @device_name: The name of gpiochip without prefix "/dev/",
40 * @lines: An array desired lines, specified by offset
41 * index for the associated GPIO device.
42 * @num_lines: The number of lines to request.
43 * @config: The new config for requested gpio. Reference
44 * "linux/gpio.h" for config details.
45 * @consumer: The name of consumer, such as "sysfs",
46 * "powerkey". This is useful for other users to
49 * Request gpio lines through the ioctl provided by chardev. User
50 * could call gpiotools_set_values() and gpiotools_get_values() to
51 * read and write respectively through the returned fd. Call
52 * gpiotools_release_line() to release these lines after that.
54 * Return: On success return the fd;
55 * On failure return the errno.
57 int gpiotools_request_line(const char *device_name, unsigned int *lines,
58 unsigned int num_lines,
59 struct gpio_v2_line_config *config,
62 struct gpio_v2_line_request req;
68 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
72 fd = open(chrdev_name, 0);
75 fprintf(stderr, "Failed to open %s, %s\n",
76 chrdev_name, strerror(errno));
80 memset(&req, 0, sizeof(req));
81 for (i = 0; i < num_lines; i++)
82 req.offsets[i] = lines[i];
85 strcpy(req.consumer, consumer);
86 req.num_lines = num_lines;
88 ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
91 fprintf(stderr, "Failed to issue %s (%d), %s\n",
92 "GPIO_GET_LINE_IOCTL", ret, strerror(errno));
96 perror("Failed to close GPIO character device file");
99 return ret < 0 ? ret : req.fd;
103 * gpiotools_set_values() - Set the value of gpio(s)
104 * @fd: The fd returned by
105 * gpiotools_request_line().
106 * @values: The array of values want to set.
108 * Return: On success return 0;
109 * On failure return the errno.
111 int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values)
115 ret = ioctl(fd, GPIO_V2_LINE_SET_VALUES_IOCTL, values);
118 fprintf(stderr, "Failed to issue %s (%d), %s\n",
119 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret,
127 * gpiotools_get_values() - Get the value of gpio(s)
128 * @fd: The fd returned by
129 * gpiotools_request_line().
130 * @values: The array of values get from hardware.
132 * Return: On success return 0;
133 * On failure return the errno.
135 int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values)
139 ret = ioctl(fd, GPIO_V2_LINE_GET_VALUES_IOCTL, values);
142 fprintf(stderr, "Failed to issue %s (%d), %s\n",
143 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret,
151 * gpiotools_release_line() - Release the line(s) of gpiochip
152 * @fd: The fd returned by
153 * gpiotools_request_line().
155 * Return: On success return 0;
156 * On failure return the errno.
158 int gpiotools_release_line(const int fd)
164 perror("Failed to close GPIO LINE device file");
172 * gpiotools_get() - Get value from specific line
173 * @device_name: The name of gpiochip without prefix "/dev/",
174 * such as "gpiochip0"
175 * @line: number of line, such as 2.
177 * Return: On success return 0;
178 * On failure return the errno.
180 int gpiotools_get(const char *device_name, unsigned int line)
184 unsigned int lines[] = {line};
186 ret = gpiotools_gets(device_name, lines, 1, &value);
194 * gpiotools_gets() - Get values from specific lines.
195 * @device_name: The name of gpiochip without prefix "/dev/",
196 * such as "gpiochip0".
197 * @lines: An array desired lines, specified by offset
198 * index for the associated GPIO device.
199 * @num_lines: The number of lines to request.
200 * @values: The array of values get from gpiochip.
202 * Return: On success return 0;
203 * On failure return the errno.
205 int gpiotools_gets(const char *device_name, unsigned int *lines,
206 unsigned int num_lines, unsigned int *values)
211 struct gpio_v2_line_config config;
212 struct gpio_v2_line_values lv;
214 memset(&config, 0, sizeof(config));
215 config.flags = GPIO_V2_LINE_FLAG_INPUT;
216 ret = gpiotools_request_line(device_name, lines, num_lines,
222 for (i = 0; i < num_lines; i++)
223 gpiotools_set_bit(&lv.mask, i);
224 ret = gpiotools_get_values(fd, &lv);
226 for (i = 0; i < num_lines; i++)
227 values[i] = gpiotools_test_bit(lv.bits, i);
228 ret_close = gpiotools_release_line(fd);
229 return ret < 0 ? ret : ret_close;
233 * gpiotools_set() - Set value to specific line
234 * @device_name: The name of gpiochip without prefix "/dev/",
235 * such as "gpiochip0"
236 * @line: number of line, such as 2.
237 * @value: The value of gpio, must be 0(low) or 1(high).
239 * Return: On success return 0;
240 * On failure return the errno.
242 int gpiotools_set(const char *device_name, unsigned int line,
245 unsigned int lines[] = {line};
247 return gpiotools_sets(device_name, lines, 1, &value);
251 * gpiotools_sets() - Set values to specific lines.
252 * @device_name: The name of gpiochip without prefix "/dev/",
253 * such as "gpiochip0".
254 * @lines: An array desired lines, specified by offset
255 * index for the associated GPIO device.
256 * @num_lines: The number of lines to request.
257 * @values: The array of values set to gpiochip, must be
260 * Return: On success return 0;
261 * On failure return the errno.
263 int gpiotools_sets(const char *device_name, unsigned int *lines,
264 unsigned int num_lines, unsigned int *values)
267 struct gpio_v2_line_config config;
269 memset(&config, 0, sizeof(config));
270 config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
271 config.num_attrs = 1;
272 config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES;
273 for (i = 0; i < num_lines; i++) {
274 gpiotools_set_bit(&config.attrs[0].mask, i);
275 gpiotools_assign_bit(&config.attrs[0].attr.values,
278 ret = gpiotools_request_line(device_name, lines, num_lines,
283 return gpiotools_release_line(ret);