blob: da5801e6c9e8cfc9ef4e5ca571b82b655af525c2 [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
Matt Spinler36df1972017-05-25 10:23:05 -050023namespace phosphor
24{
25namespace gpio
26{
27
28using namespace phosphor::logging;
29
30void GPIO::set(Value value)
31{
32 assert(direction == Direction::output);
33
34 requestLine(value);
35
36 gpiohandle_data data{};
37 data.values[0] = static_cast<gpioValue_t>(value);
38
39 auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
40 if (rc == -1)
41 {
42 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070043 log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinler36df1972017-05-25 10:23:05 -050044 throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
45 }
46}
47
Matt Spinler36df1972017-05-25 10:23:05 -050048void GPIO::requestLine(Value defaultValue)
49{
Patrick Venturedace6802018-11-01 16:52:10 -070050 // Only need to do this once
Matt Spinler36df1972017-05-25 10:23:05 -050051 if (lineFD)
52 {
53 return;
54 }
55
56 FileDescriptor fd{open(device.c_str(), 0)};
57 if (fd() == -1)
58 {
59 auto e = errno;
60 log<level::ERR>("Failed opening GPIO device",
Joseph Reynolds32c3f632018-05-17 12:11:18 -050061 entry("DEVICE=%s", device.c_str()),
Matt Spinler36df1972017-05-25 10:23:05 -050062 entry("ERRNO=%d", e));
63 throw std::runtime_error("Failed opening GPIO device");
64 }
65
Patrick Venturedace6802018-11-01 16:52:10 -070066 // Make an ioctl call to request the GPIO line, which will
67 // return the descriptor to use to access it.
Matt Spinler36df1972017-05-25 10:23:05 -050068 gpiohandle_request request{};
Patrick Venturedace6802018-11-01 16:52:10 -070069 strncpy(request.consumer_label, "phosphor-gpio-util",
Matt Spinler36df1972017-05-25 10:23:05 -050070 sizeof(request.consumer_label));
71
Patrick Venturedace6802018-11-01 16:52:10 -070072 request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT
73 : GPIOHANDLE_REQUEST_INPUT;
Matt Spinler36df1972017-05-25 10:23:05 -050074
75 request.lineoffsets[0] = gpio;
76 request.lines = 1;
77
78 if (direction == Direction::output)
79 {
80 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
81 }
82
83 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
84 if (rc == -1)
85 {
86 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070087 log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
Matt Spinler36df1972017-05-25 10:23:05 -050088 entry("ERRNO=%d", e));
89 throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
90 }
91
92 lineFD.set(request.fd);
93}
94
Patrick Venturedace6802018-11-01 16:52:10 -070095} // namespace gpio
96} // namespace phosphor