Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 16 | #include "gpio.hpp" |
| 17 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/ioctl.h> |
| 20 | |
| 21 | #include <phosphor-logging/log.hpp> |
| 22 | |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 23 | #include <cassert> |
| 24 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace gpio |
| 28 | { |
| 29 | |
| 30 | using namespace phosphor::logging; |
| 31 | |
| 32 | void GPIO::set(Value value) |
| 33 | { |
| 34 | assert(direction == Direction::output); |
| 35 | |
| 36 | requestLine(value); |
| 37 | |
| 38 | gpiohandle_data data{}; |
| 39 | data.values[0] = static_cast<gpioValue_t>(value); |
| 40 | |
| 41 | auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); |
| 42 | if (rc == -1) |
| 43 | { |
| 44 | auto e = errno; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 45 | log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e)); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 46 | throw std::runtime_error("Failed SET_LINE_VALUES ioctl"); |
| 47 | } |
| 48 | } |
| 49 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 50 | void GPIO::requestLine(Value defaultValue) |
| 51 | { |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 52 | // Only need to do this once |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 53 | if (lineFD) |
| 54 | { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | FileDescriptor fd{open(device.c_str(), 0)}; |
| 59 | if (fd() == -1) |
| 60 | { |
| 61 | auto e = errno; |
| 62 | log<level::ERR>("Failed opening GPIO device", |
Joseph Reynolds | 32c3f63 | 2018-05-17 12:11:18 -0500 | [diff] [blame] | 63 | entry("DEVICE=%s", device.c_str()), |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 64 | entry("ERRNO=%d", e)); |
| 65 | throw std::runtime_error("Failed opening GPIO device"); |
| 66 | } |
| 67 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 68 | // Make an ioctl call to request the GPIO line, which will |
| 69 | // return the descriptor to use to access it. |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 70 | gpiohandle_request request{}; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 71 | strncpy(request.consumer_label, "phosphor-gpio-util", |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 72 | sizeof(request.consumer_label)); |
| 73 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 74 | request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT |
| 75 | : GPIOHANDLE_REQUEST_INPUT; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 76 | |
| 77 | request.lineoffsets[0] = gpio; |
| 78 | request.lines = 1; |
| 79 | |
| 80 | if (direction == Direction::output) |
| 81 | { |
| 82 | request.default_values[0] = static_cast<gpioValue_t>(defaultValue); |
| 83 | } |
| 84 | |
| 85 | auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request); |
| 86 | if (rc == -1) |
| 87 | { |
| 88 | auto e = errno; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 89 | log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio), |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 90 | entry("ERRNO=%d", e)); |
| 91 | throw std::runtime_error("Failed GET_LINEHANDLE ioctl"); |
| 92 | } |
| 93 | |
| 94 | lineFD.set(request.fd); |
| 95 | } |
| 96 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 97 | } // namespace gpio |
| 98 | } // namespace phosphor |