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