Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -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 | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 16 | #include "gpio.hpp" |
| 17 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/ioctl.h> |
| 20 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/elog-errors.hpp> |
| 22 | #include <phosphor-logging/elog.hpp> |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 23 | #include <phosphor-logging/lg2.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Common/error.hpp> |
| 25 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 26 | #include <cassert> |
| 27 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 28 | namespace phosphor |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 29 | { |
| 30 | namespace gpio |
| 31 | { |
| 32 | |
| 33 | using namespace phosphor::logging; |
| 34 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 35 | using InternalFailure = |
| 36 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 37 | |
| 38 | Value GPIO::read() |
| 39 | { |
| 40 | assert(direction == Direction::input); |
| 41 | |
| 42 | requestLine(); |
| 43 | |
| 44 | gpiohandle_data data{}; |
| 45 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 46 | auto rc = ioctl(lineFD(), GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 47 | |
| 48 | if (rc < 0) |
| 49 | { |
| 50 | auto e = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 51 | lg2::error("Failed GET_LINE_VALUES ioctl ERRNO={ERRNO}", "ERRNO", e); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 52 | elog<InternalFailure>(); |
| 53 | } |
| 54 | |
| 55 | return (data.values[0] == 0) ? Value::low : Value::high; |
| 56 | } |
| 57 | |
Matt Spinler | 35aa143 | 2018-01-18 11:13:20 -0600 | [diff] [blame] | 58 | void GPIO::set(Value value) |
| 59 | { |
| 60 | assert(direction == Direction::output); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 35aa143 | 2018-01-18 11:13:20 -0600 | [diff] [blame] | 62 | 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; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 71 | lg2::error("Failed SET_LINE_VALUES ioctl ERRNO={ERRNO}", "ERRNO", e); |
Matt Spinler | 35aa143 | 2018-01-18 11:13:20 -0600 | [diff] [blame] | 72 | elog<InternalFailure>(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void GPIO::requestLine(Value defaultValue) |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 77 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 78 | // Only need to do this once |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 79 | 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; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 88 | lg2::error("Failed opening GPIO device DEVICE={DEVICE} ERRNO={ERRNO}", |
| 89 | "DEVICE", device, "ERRNO", e); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 90 | elog<InternalFailure>(); |
| 91 | } |
| 92 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 93 | // Make an ioctl call to request the GPIO line, which will |
| 94 | // return the descriptor to use to access it. |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 95 | gpiohandle_request request{}; |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 96 | strncpy(request.consumer_label, "phosphor-power", |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 97 | sizeof(request.consumer_label)); |
| 98 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 99 | request.flags = (direction == Direction::input) ? GPIOHANDLE_REQUEST_INPUT |
| 100 | : GPIOHANDLE_REQUEST_OUTPUT; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 101 | |
| 102 | request.lineoffsets[0] = gpio; |
| 103 | request.lines = 1; |
| 104 | |
Matt Spinler | 35aa143 | 2018-01-18 11:13:20 -0600 | [diff] [blame] | 105 | if (direction == Direction::output) |
| 106 | { |
| 107 | request.default_values[0] = static_cast<gpioValue_t>(defaultValue); |
| 108 | } |
| 109 | |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 110 | auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request); |
| 111 | if (rc == -1) |
| 112 | { |
| 113 | auto e = errno; |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 114 | lg2::error("Failed GET_LINEHANDLE ioctl GPIO={GPIO} ERRNO={ERRNO}", |
| 115 | "GPIO", gpio, "ERRNO", e); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 116 | elog<InternalFailure>(); |
| 117 | } |
| 118 | |
| 119 | lineFD.set(request.fd); |
| 120 | } |
| 121 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 122 | } // namespace gpio |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 123 | } // namespace phosphor |