blob: 0e825b981eda4d41d1b9aa46d74943d3f43b427c [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
George Liu2a8848c2023-08-01 13:49:28 +080021#include <phosphor-logging/lg2.hpp>
Patrick Venturedace6802018-11-01 16:52:10 -070022
Patrick Williams39084b42023-05-10 07:50:58 -050023#include <cassert>
George Liu2a8848c2023-08-01 13:49:28 +080024#include <cstring>
Patrick Williams39084b42023-05-10 07:50:58 -050025
Matt Spinler36df1972017-05-25 10:23:05 -050026namespace phosphor
27{
28namespace gpio
29{
30
Matt Spinler36df1972017-05-25 10:23:05 -050031void 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 {
George Liu2a8848c2023-08-01 13:49:28 +080043 lg2::error("Failed SET_LINE_VALUES ioctl: {ERRNO}", "ERRNO", errno);
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 {
George Liu2a8848c2023-08-01 13:49:28 +080059 lg2::error("Failed opening {DEVICE}: {ERRNO}", "DEVICE", device,
60 "ERRNO", errno);
Matt Spinler36df1972017-05-25 10:23:05 -050061 throw std::runtime_error("Failed opening GPIO device");
62 }
63
Patrick Venturedace6802018-11-01 16:52:10 -070064 // Make an ioctl call to request the GPIO line, which will
65 // return the descriptor to use to access it.
Matt Spinler36df1972017-05-25 10:23:05 -050066 gpiohandle_request request{};
Patrick Venturedace6802018-11-01 16:52:10 -070067 strncpy(request.consumer_label, "phosphor-gpio-util",
Matt Spinler36df1972017-05-25 10:23:05 -050068 sizeof(request.consumer_label));
69
Patrick Venturedace6802018-11-01 16:52:10 -070070 request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT
71 : GPIOHANDLE_REQUEST_INPUT;
Matt Spinler36df1972017-05-25 10:23:05 -050072
73 request.lineoffsets[0] = gpio;
74 request.lines = 1;
75
76 if (direction == Direction::output)
77 {
78 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
79 }
80
81 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
82 if (rc == -1)
83 {
George Liu2a8848c2023-08-01 13:49:28 +080084 lg2::error("Failed GET_LINEHANDLE ioctl {GPIO}: {ERRNO}", "GPIO", gpio,
85 "ERRNO", errno);
Matt Spinler36df1972017-05-25 10:23:05 -050086 throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
87 }
88
89 lineFD.set(request.fd);
90}
91
Patrick Venturedace6802018-11-01 16:52:10 -070092} // namespace gpio
93} // namespace phosphor