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 | |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame^] | 21 | #include <phosphor-logging/lg2.hpp> |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 22 | |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 23 | #include <cassert> |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame^] | 24 | #include <cstring> |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 25 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 26 | namespace phosphor |
| 27 | { |
| 28 | namespace gpio |
| 29 | { |
| 30 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 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 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame^] | 43 | lg2::error("Failed SET_LINE_VALUES ioctl: {ERRNO}", "ERRNO", errno); |
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 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame^] | 59 | lg2::error("Failed opening {DEVICE}: {ERRNO}", "DEVICE", device, |
| 60 | "ERRNO", errno); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 61 | throw std::runtime_error("Failed opening GPIO device"); |
| 62 | } |
| 63 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 64 | // Make an ioctl call to request the GPIO line, which will |
| 65 | // return the descriptor to use to access it. |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 66 | gpiohandle_request request{}; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 67 | strncpy(request.consumer_label, "phosphor-gpio-util", |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 68 | sizeof(request.consumer_label)); |
| 69 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 70 | request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT |
| 71 | : GPIOHANDLE_REQUEST_INPUT; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 72 | |
| 73 | request.lineoffsets[0] = gpio; |
| 74 | request.lines = 1; |
| 75 | |
| 76 | if (direction == Direction::output) |
| 77 | { |
| 78 | request.default_values[0] = static_cast<gpioValue_t>(defaultValue); |
| 79 | } |
| 80 | |
| 81 | auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request); |
| 82 | if (rc == -1) |
| 83 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame^] | 84 | lg2::error("Failed GET_LINEHANDLE ioctl {GPIO}: {ERRNO}", "GPIO", gpio, |
| 85 | "ERRNO", errno); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 86 | throw std::runtime_error("Failed GET_LINEHANDLE ioctl"); |
| 87 | } |
| 88 | |
| 89 | lineFD.set(request.fd); |
| 90 | } |
| 91 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 92 | } // namespace gpio |
| 93 | } // namespace phosphor |