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