blob: 0b671a89eb1f913a19f62af6e52c75fa3078043d [file] [log] [blame]
Matt Spinler36df1972017-05-25 10:23:05 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinler36df1972017-05-25 10:23:05 -050016#include "gpio.hpp"
17
Patrick Venturedace6802018-11-01 16:52:10 -070018#include <fcntl.h>
19#include <sys/ioctl.h>
20
21#include <phosphor-logging/log.hpp>
22
Patrick Williams39084b42023-05-10 07:50:58 -050023#include <cassert>
24
Matt Spinler36df1972017-05-25 10:23:05 -050025namespace phosphor
26{
27namespace gpio
28{
29
30using namespace phosphor::logging;
31
32void GPIO::set(Value value)
33{
34 assert(direction == Direction::output);
35
36 requestLine(value);
37
38 gpiohandle_data data{};
39 data.values[0] = static_cast<gpioValue_t>(value);
40
41 auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
42 if (rc == -1)
43 {
44 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070045 log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinler36df1972017-05-25 10:23:05 -050046 throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
47 }
48}
49
Matt Spinler36df1972017-05-25 10:23:05 -050050void GPIO::requestLine(Value defaultValue)
51{
Patrick Venturedace6802018-11-01 16:52:10 -070052 // Only need to do this once
Matt Spinler36df1972017-05-25 10:23:05 -050053 if (lineFD)
54 {
55 return;
56 }
57
58 FileDescriptor fd{open(device.c_str(), 0)};
59 if (fd() == -1)
60 {
61 auto e = errno;
62 log<level::ERR>("Failed opening GPIO device",
Joseph Reynolds32c3f632018-05-17 12:11:18 -050063 entry("DEVICE=%s", device.c_str()),
Matt Spinler36df1972017-05-25 10:23:05 -050064 entry("ERRNO=%d", e));
65 throw std::runtime_error("Failed opening GPIO device");
66 }
67
Patrick Venturedace6802018-11-01 16:52:10 -070068 // Make an ioctl call to request the GPIO line, which will
69 // return the descriptor to use to access it.
Matt Spinler36df1972017-05-25 10:23:05 -050070 gpiohandle_request request{};
Patrick Venturedace6802018-11-01 16:52:10 -070071 strncpy(request.consumer_label, "phosphor-gpio-util",
Matt Spinler36df1972017-05-25 10:23:05 -050072 sizeof(request.consumer_label));
73
Patrick Venturedace6802018-11-01 16:52:10 -070074 request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT
75 : GPIOHANDLE_REQUEST_INPUT;
Matt Spinler36df1972017-05-25 10:23:05 -050076
77 request.lineoffsets[0] = gpio;
78 request.lines = 1;
79
80 if (direction == Direction::output)
81 {
82 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
83 }
84
85 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
86 if (rc == -1)
87 {
88 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070089 log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
Matt Spinler36df1972017-05-25 10:23:05 -050090 entry("ERRNO=%d", e));
91 throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
92 }
93
94 lineFD.set(request.fd);
95}
96
Patrick Venturedace6802018-11-01 16:52:10 -070097} // namespace gpio
98} // namespace phosphor