blob: c6b7704bb6eccd82b16a9868ae1151754efdac29 [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
William A. Kennington III1dc2b792018-11-12 17:11:14 -080021#include <cassert>
Patrick Venturedace6802018-11-01 16:52:10 -070022#include <phosphor-logging/log.hpp>
23
Matt Spinler36df1972017-05-25 10:23:05 -050024namespace phosphor
25{
26namespace gpio
27{
28
29using namespace phosphor::logging;
30
31void GPIO::set(Value value)
32{
33 assert(direction == Direction::output);
34
35 requestLine(value);
36
37 gpiohandle_data data{};
38 data.values[0] = static_cast<gpioValue_t>(value);
39
40 auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
41 if (rc == -1)
42 {
43 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070044 log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinler36df1972017-05-25 10:23:05 -050045 throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
46 }
47}
48
Matt Spinler36df1972017-05-25 10:23:05 -050049void GPIO::requestLine(Value defaultValue)
50{
Patrick Venturedace6802018-11-01 16:52:10 -070051 // Only need to do this once
Matt Spinler36df1972017-05-25 10:23:05 -050052 if (lineFD)
53 {
54 return;
55 }
56
57 FileDescriptor fd{open(device.c_str(), 0)};
58 if (fd() == -1)
59 {
60 auto e = errno;
61 log<level::ERR>("Failed opening GPIO device",
Joseph Reynolds32c3f632018-05-17 12:11:18 -050062 entry("DEVICE=%s", device.c_str()),
Matt Spinler36df1972017-05-25 10:23:05 -050063 entry("ERRNO=%d", e));
64 throw std::runtime_error("Failed opening GPIO device");
65 }
66
Patrick Venturedace6802018-11-01 16:52:10 -070067 // Make an ioctl call to request the GPIO line, which will
68 // return the descriptor to use to access it.
Matt Spinler36df1972017-05-25 10:23:05 -050069 gpiohandle_request request{};
Patrick Venturedace6802018-11-01 16:52:10 -070070 strncpy(request.consumer_label, "phosphor-gpio-util",
Matt Spinler36df1972017-05-25 10:23:05 -050071 sizeof(request.consumer_label));
72
Patrick Venturedace6802018-11-01 16:52:10 -070073 request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT
74 : GPIOHANDLE_REQUEST_INPUT;
Matt Spinler36df1972017-05-25 10:23:05 -050075
76 request.lineoffsets[0] = gpio;
77 request.lines = 1;
78
79 if (direction == Direction::output)
80 {
81 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
82 }
83
84 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
85 if (rc == -1)
86 {
87 auto e = errno;
Patrick Venturedace6802018-11-01 16:52:10 -070088 log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
Matt Spinler36df1972017-05-25 10:23:05 -050089 entry("ERRNO=%d", e));
90 throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
91 }
92
93 lineFD.set(request.fd);
94}
95
Patrick Venturedace6802018-11-01 16:52:10 -070096} // namespace gpio
97} // namespace phosphor