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 COMSUMER "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.
36 * gpiotools_request_linehandle() - request gpio lines in a gpiochip
37 * @device_name: The name of gpiochip without prefix "/dev/",
39 * @lines: An array desired lines, specified by offset
40 * index for the associated GPIO device.
41 * @nline: The number of lines to request.
42 * @flag: The new flag for requsted gpio. Reference
43 * "linux/gpio.h" for the meaning of flag.
44 * @data: Default value will be set to gpio when flag is
45 * GPIOHANDLE_REQUEST_OUTPUT.
46 * @consumer_label: The name of consumer, such as "sysfs",
47 * "powerkey". This is useful for other users to
50 * Request gpio lines through the ioctl provided by chardev. User
51 * could call gpiotools_set_values() and gpiotools_get_values() to
52 * read and write respectively through the returned fd. Call
53 * gpiotools_release_linehandle() to release these lines after that.
55 * Return: On success return the fd;
56 * On failure return the errno.
58 int gpiotools_request_linehandle(const char *device_name
, unsigned int *lines
,
59 unsigned int nlines
, unsigned int flag
,
60 struct gpiohandle_data
*data
,
61 const char *consumer_label
)
63 struct gpiohandle_request req
;
69 ret
= asprintf(&chrdev_name
, "/dev/%s", device_name
);
73 fd
= open(chrdev_name
, 0);
76 fprintf(stderr
, "Failed to open %s, %s\n",
77 chrdev_name
, strerror(errno
));
78 goto exit_close_error
;
81 for (i
= 0; i
< nlines
; i
++)
82 req
.lineoffsets
[i
] = lines
[i
];
85 strcpy(req
.consumer_label
, consumer_label
);
87 if (flag
& GPIOHANDLE_REQUEST_OUTPUT
)
88 memcpy(req
.default_values
, data
, sizeof(req
.default_values
));
90 ret
= ioctl(fd
, GPIO_GET_LINEHANDLE_IOCTL
, &req
);
93 fprintf(stderr
, "Failed to issue %s (%d), %s\n",
94 "GPIO_GET_LINEHANDLE_IOCTL", ret
, strerror(errno
));
99 perror("Failed to close GPIO character device file");
101 return ret
< 0 ? ret
: req
.fd
;
104 * gpiotools_set_values(): Set the value of gpio(s)
105 * @fd: The fd returned by
106 * gpiotools_request_linehandle().
107 * @data: The array of values want to set.
109 * Return: On success return 0;
110 * On failure return the errno.
112 int gpiotools_set_values(const int fd
, struct gpiohandle_data
*data
)
116 ret
= ioctl(fd
, GPIOHANDLE_SET_LINE_VALUES_IOCTL
, data
);
119 fprintf(stderr
, "Failed to issue %s (%d), %s\n",
120 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret
,
128 * gpiotools_get_values(): Get the value of gpio(s)
129 * @fd: The fd returned by
130 * gpiotools_request_linehandle().
131 * @data: The array of values get from hardware.
133 * Return: On success return 0;
134 * On failure return the errno.
136 int gpiotools_get_values(const int fd
, struct gpiohandle_data
*data
)
140 ret
= ioctl(fd
, GPIOHANDLE_GET_LINE_VALUES_IOCTL
, data
);
143 fprintf(stderr
, "Failed to issue %s (%d), %s\n",
144 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret
,
152 * gpiotools_release_linehandle(): Release the line(s) of gpiochip
153 * @fd: The fd returned by
154 * gpiotools_request_linehandle().
156 * Return: On success return 0;
157 * On failure return the errno.
159 int gpiotools_release_linehandle(const int fd
)
165 perror("Failed to close GPIO LINEHANDLE device file");
173 * gpiotools_get(): Get value from specific line
174 * @device_name: The name of gpiochip without prefix "/dev/",
175 * such as "gpiochip0"
176 * @line: number of line, such as 2.
178 * Return: On success return 0;
179 * On failure return the errno.
181 int gpiotools_get(const char *device_name
, unsigned int line
)
183 struct gpiohandle_data data
;
184 unsigned int lines
[] = {line
};
186 gpiotools_gets(device_name
, lines
, 1, &data
);
187 return data
.values
[0];
192 * gpiotools_gets(): Get values from specific lines.
193 * @device_name: The name of gpiochip without prefix "/dev/",
194 * such as "gpiochip0".
195 * @lines: An array desired lines, specified by offset
196 * index for the associated GPIO device.
197 * @nline: The number of lines to request.
198 * @data: The array of values get from gpiochip.
200 * Return: On success return 0;
201 * On failure return the errno.
203 int gpiotools_gets(const char *device_name
, unsigned int *lines
,
204 unsigned int nlines
, struct gpiohandle_data
*data
)
210 ret
= gpiotools_request_linehandle(device_name
, lines
, nlines
,
211 GPIOHANDLE_REQUEST_INPUT
, data
,
217 ret
= gpiotools_get_values(fd
, data
);
218 ret_close
= gpiotools_release_linehandle(fd
);
219 return ret
< 0 ? ret
: ret_close
;
223 * gpiotools_set(): Set value to specific line
224 * @device_name: The name of gpiochip without prefix "/dev/",
225 * such as "gpiochip0"
226 * @line: number of line, such as 2.
227 * @value: The value of gpio, must be 0(low) or 1(high).
229 * Return: On success return 0;
230 * On failure return the errno.
232 int gpiotools_set(const char *device_name
, unsigned int line
,
235 struct gpiohandle_data data
;
236 unsigned int lines
[] = {line
};
238 data
.values
[0] = value
;
239 return gpiotools_sets(device_name
, lines
, 1, &data
);
243 * gpiotools_sets(): Set values to specific lines.
244 * @device_name: The name of gpiochip without prefix "/dev/",
245 * such as "gpiochip0".
246 * @lines: An array desired lines, specified by offset
247 * index for the associated GPIO device.
248 * @nline: The number of lines to request.
249 * @data: The array of values set to gpiochip, must be
252 * Return: On success return 0;
253 * On failure return the errno.
255 int gpiotools_sets(const char *device_name
, unsigned int *lines
,
256 unsigned int nlines
, struct gpiohandle_data
*data
)
260 ret
= gpiotools_request_linehandle(device_name
, lines
, nlines
,
261 GPIOHANDLE_REQUEST_OUTPUT
, data
,
266 return gpiotools_release_linehandle(ret
);