blob: 6adee9cd16762e0504abfc7045ca7015c18c1980 [file] [log] [blame]
Matt Spinleree7adb72017-08-21 15:17:19 -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 Spinleree7adb72017-08-21 15:17:19 -050016#include "gpio.hpp"
17
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018#include <fcntl.h>
19#include <sys/ioctl.h>
20
21#include <cassert>
22#include <phosphor-logging/elog-errors.hpp>
23#include <phosphor-logging/elog.hpp>
24#include <phosphor-logging/log.hpp>
25#include <xyz/openbmc_project/Common/error.hpp>
26
Lei YUab093322019-10-09 16:43:22 +080027namespace phosphor
Matt Spinleree7adb72017-08-21 15:17:19 -050028{
29namespace gpio
30{
31
32using namespace phosphor::logging;
33
Matt Spinlerf0f02b92018-10-25 16:12:43 -050034using InternalFailure =
35 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinleree7adb72017-08-21 15:17:19 -050036
37Value GPIO::read()
38{
39 assert(direction == Direction::input);
40
41 requestLine();
42
43 gpiohandle_data data{};
44
Matt Spinlerf0f02b92018-10-25 16:12:43 -050045 auto rc = ioctl(lineFD(), GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
Matt Spinleree7adb72017-08-21 15:17:19 -050046
47 if (rc < 0)
48 {
49 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050050 log<level::ERR>("Failed GET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinleree7adb72017-08-21 15:17:19 -050051 elog<InternalFailure>();
52 }
53
54 return (data.values[0] == 0) ? Value::low : Value::high;
55}
56
Matt Spinler35aa1432018-01-18 11:13:20 -060057void GPIO::set(Value value)
58{
59 assert(direction == Direction::output);
Matt Spinleree7adb72017-08-21 15:17:19 -050060
Matt Spinler35aa1432018-01-18 11:13:20 -060061 requestLine(value);
62
63 gpiohandle_data data{};
64 data.values[0] = static_cast<gpioValue_t>(value);
65
66 auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
67 if (rc == -1)
68 {
69 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050070 log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinler35aa1432018-01-18 11:13:20 -060071 elog<InternalFailure>();
72 }
73}
74
75void GPIO::requestLine(Value defaultValue)
Matt Spinleree7adb72017-08-21 15:17:19 -050076{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050077 // Only need to do this once
Matt Spinleree7adb72017-08-21 15:17:19 -050078 if (lineFD)
79 {
80 return;
81 }
82
83 power::util::FileDescriptor fd{open(device.c_str(), 0)};
84 if (fd() == -1)
85 {
86 auto e = errno;
87 log<level::ERR>("Failed opening GPIO device",
Matt Spinler7475b1c2018-02-27 14:04:58 -060088 entry("DEVICE=%s", device.c_str()),
Matt Spinleree7adb72017-08-21 15:17:19 -050089 entry("ERRNO=%d", e));
90 elog<InternalFailure>();
91 }
92
Matt Spinlerf0f02b92018-10-25 16:12:43 -050093 // Make an ioctl call to request the GPIO line, which will
94 // return the descriptor to use to access it.
Matt Spinleree7adb72017-08-21 15:17:19 -050095 gpiohandle_request request{};
Lei YUab093322019-10-09 16:43:22 +080096 strncpy(request.consumer_label, "phosphor-power",
Matt Spinleree7adb72017-08-21 15:17:19 -050097 sizeof(request.consumer_label));
98
Matt Spinlerf0f02b92018-10-25 16:12:43 -050099 request.flags = (direction == Direction::input) ? GPIOHANDLE_REQUEST_INPUT
100 : GPIOHANDLE_REQUEST_OUTPUT;
Matt Spinleree7adb72017-08-21 15:17:19 -0500101
102 request.lineoffsets[0] = gpio;
103 request.lines = 1;
104
Matt Spinler35aa1432018-01-18 11:13:20 -0600105 if (direction == Direction::output)
106 {
107 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
108 }
109
Matt Spinleree7adb72017-08-21 15:17:19 -0500110 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
111 if (rc == -1)
112 {
113 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500114 log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
Matt Spinleree7adb72017-08-21 15:17:19 -0500115 entry("ERRNO=%d", e));
116 elog<InternalFailure>();
117 }
118
119 lineFD.set(request.fd);
120}
121
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500122} // namespace gpio
Lei YUab093322019-10-09 16:43:22 +0800123} // namespace phosphor