blob: 81bc4f5f5a6509948259958841db54e723d092ae [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
Matt Spinlerf0f02b92018-10-25 16:12:43 -050021#include <phosphor-logging/elog-errors.hpp>
22#include <phosphor-logging/elog.hpp>
23#include <phosphor-logging/log.hpp>
24#include <xyz/openbmc_project/Common/error.hpp>
25
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060026#include <cassert>
27
Lei YUab093322019-10-09 16:43:22 +080028namespace phosphor
Matt Spinleree7adb72017-08-21 15:17:19 -050029{
30namespace gpio
31{
32
33using namespace phosphor::logging;
34
Matt Spinlerf0f02b92018-10-25 16:12:43 -050035using InternalFailure =
36 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinleree7adb72017-08-21 15:17:19 -050037
38Value GPIO::read()
39{
40 assert(direction == Direction::input);
41
42 requestLine();
43
44 gpiohandle_data data{};
45
Matt Spinlerf0f02b92018-10-25 16:12:43 -050046 auto rc = ioctl(lineFD(), GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
Matt Spinleree7adb72017-08-21 15:17:19 -050047
48 if (rc < 0)
49 {
50 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050051 log<level::ERR>("Failed GET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinleree7adb72017-08-21 15:17:19 -050052 elog<InternalFailure>();
53 }
54
55 return (data.values[0] == 0) ? Value::low : Value::high;
56}
57
Matt Spinler35aa1432018-01-18 11:13:20 -060058void GPIO::set(Value value)
59{
60 assert(direction == Direction::output);
Matt Spinleree7adb72017-08-21 15:17:19 -050061
Matt Spinler35aa1432018-01-18 11:13:20 -060062 requestLine(value);
63
64 gpiohandle_data data{};
65 data.values[0] = static_cast<gpioValue_t>(value);
66
67 auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
68 if (rc == -1)
69 {
70 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050071 log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
Matt Spinler35aa1432018-01-18 11:13:20 -060072 elog<InternalFailure>();
73 }
74}
75
76void GPIO::requestLine(Value defaultValue)
Matt Spinleree7adb72017-08-21 15:17:19 -050077{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050078 // Only need to do this once
Matt Spinleree7adb72017-08-21 15:17:19 -050079 if (lineFD)
80 {
81 return;
82 }
83
84 power::util::FileDescriptor fd{open(device.c_str(), 0)};
85 if (fd() == -1)
86 {
87 auto e = errno;
88 log<level::ERR>("Failed opening GPIO device",
Matt Spinler7475b1c2018-02-27 14:04:58 -060089 entry("DEVICE=%s", device.c_str()),
Matt Spinleree7adb72017-08-21 15:17:19 -050090 entry("ERRNO=%d", e));
91 elog<InternalFailure>();
92 }
93
Matt Spinlerf0f02b92018-10-25 16:12:43 -050094 // Make an ioctl call to request the GPIO line, which will
95 // return the descriptor to use to access it.
Matt Spinleree7adb72017-08-21 15:17:19 -050096 gpiohandle_request request{};
Lei YUab093322019-10-09 16:43:22 +080097 strncpy(request.consumer_label, "phosphor-power",
Matt Spinleree7adb72017-08-21 15:17:19 -050098 sizeof(request.consumer_label));
99
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500100 request.flags = (direction == Direction::input) ? GPIOHANDLE_REQUEST_INPUT
101 : GPIOHANDLE_REQUEST_OUTPUT;
Matt Spinleree7adb72017-08-21 15:17:19 -0500102
103 request.lineoffsets[0] = gpio;
104 request.lines = 1;
105
Matt Spinler35aa1432018-01-18 11:13:20 -0600106 if (direction == Direction::output)
107 {
108 request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
109 }
110
Matt Spinleree7adb72017-08-21 15:17:19 -0500111 auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
112 if (rc == -1)
113 {
114 auto e = errno;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500115 log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
Matt Spinleree7adb72017-08-21 15:17:19 -0500116 entry("ERRNO=%d", e));
117 elog<InternalFailure>();
118 }
119
120 lineFD.set(request.fd);
121}
122
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500123} // namespace gpio
Lei YUab093322019-10-09 16:43:22 +0800124} // namespace phosphor