| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | // Copyright (c) 2018 Intel 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 | */ | 
|  | 16 |  | 
| George Liu | d6a1bae | 2022-06-20 13:47:31 +0800 | [diff] [blame] | 17 | #include "config.h" | 
| Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 18 |  | 
| George Liu | d6a1bae | 2022-06-20 13:47:31 +0800 | [diff] [blame] | 19 | #include "gpio.hpp" | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 20 |  | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 21 | #include <error.h> | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 22 | #include <fcntl.h> | 
| Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 23 | #include <unistd.h> | 
|  | 24 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 25 | #include <gpioplus/utility/aspeed.hpp> | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 27 | #include <phosphor-logging/lg2.hpp> | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 28 |  | 
| George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 29 | #include <filesystem> | 
|  | 30 | #include <fstream> | 
|  | 31 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 32 | const std::string gpioDev = "/sys/class/gpio"; | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 33 |  | 
| Patrick Williams | 8eca9bb | 2022-06-16 17:11:51 -0500 | [diff] [blame] | 34 | namespace fs = std::filesystem; | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 35 |  | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 36 | void closeGpio(int fd) | 
|  | 37 | { | 
|  | 38 | if (fd > 0) | 
|  | 39 | { | 
|  | 40 | ::close(fd); | 
|  | 41 | } | 
|  | 42 | } | 
|  | 43 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 44 | uint32_t getGpioBase() | 
|  | 45 | { | 
|  | 46 | // Look for a /sys/class/gpio/gpiochip*/label file | 
|  | 47 | // with a value of GPIO_BASE_LABEL_NAME.  Then read | 
|  | 48 | // the base value from the 'base' file in that directory. | 
|  | 49 | #ifdef LOOKUP_GPIO_BASE | 
|  | 50 | for (auto& f : fs::directory_iterator(gpioDev)) | 
|  | 51 | { | 
|  | 52 | std::string path{f.path()}; | 
|  | 53 | if (path.find("gpiochip") == std::string::npos) | 
|  | 54 | { | 
|  | 55 | continue; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | std::ifstream labelStream{path + "/label"}; | 
|  | 59 | std::string label; | 
|  | 60 | labelStream >> label; | 
|  | 61 |  | 
|  | 62 | if (label == GPIO_BASE_LABEL_NAME) | 
|  | 63 | { | 
|  | 64 | uint32_t base; | 
|  | 65 | std::ifstream baseStream{path + "/base"}; | 
|  | 66 | baseStream >> base; | 
|  | 67 | return base; | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 |  | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 71 | lg2::error("Could not find GPIO base"); | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 72 | throw std::runtime_error("Could not find GPIO base!"); | 
|  | 73 | #else | 
|  | 74 | return 0; | 
|  | 75 | #endif | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | uint32_t getGpioNum(const std::string& gpioPin) | 
|  | 79 | { | 
|  | 80 | // gpioplus promises that they will figure out how to easily | 
|  | 81 | // support multiple BMC vendors when the time comes. | 
|  | 82 | auto offset = gpioplus::utility::aspeed::nameToOffset(gpioPin); | 
|  | 83 |  | 
|  | 84 | return getGpioBase() + offset; | 
|  | 85 | } | 
|  | 86 |  | 
| Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 87 | int configGroupGpio(buttonConfig& buttonIFConfig) | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 88 | { | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 89 | int result = 0; | 
|  | 90 | // iterate the list of gpios from the button interface config | 
|  | 91 | // and initialize them | 
|  | 92 | for (auto& gpioCfg : buttonIFConfig.gpios) | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 93 | { | 
| Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 94 | result = configGpio(gpioCfg); | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 95 | if (result < 0) | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 96 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 97 | lg2::error("{NAME}: Error configuring gpio-{NUM}: {RESULT}", "NAME", | 
|  | 98 | buttonIFConfig.formFactorName, "NUM", gpioCfg.number, | 
|  | 99 | "RESULT", result); | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 100 |  | 
|  | 101 | break; | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 102 | } | 
|  | 103 | } | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 104 |  | 
|  | 105 | return result; | 
| Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 108 | int configGpio(gpioInfo& gpioConfig) | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 109 | { | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 110 | auto gpioNum = gpioConfig.number; | 
|  | 111 | auto gpioDirection = gpioConfig.direction; | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 112 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 113 | std::string devPath{gpioDev}; | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 114 |  | 
|  | 115 | std::fstream stream; | 
|  | 116 |  | 
|  | 117 | stream.exceptions(std::ifstream::failbit | std::ifstream::badbit); | 
|  | 118 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 119 | devPath += "/gpio" + std::to_string(gpioNum) + "/value"; | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 120 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 121 | fs::path fullPath(devPath); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 122 |  | 
| Matt Spinler | 8f2c95a | 2018-11-05 15:17:29 -0600 | [diff] [blame] | 123 | if (fs::exists(fullPath)) | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 124 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 125 | lg2::info("GPIO exported: {PATH}", "PATH", devPath); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 126 | } | 
|  | 127 | else | 
|  | 128 | { | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 129 | devPath = gpioDev + "/export"; | 
|  | 130 |  | 
|  | 131 | stream.open(devPath, std::fstream::out); | 
|  | 132 | try | 
|  | 133 | { | 
|  | 134 | stream << gpioNum; | 
|  | 135 | stream.close(); | 
|  | 136 | } | 
|  | 137 |  | 
| Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 138 | catch (const std::exception& e) | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 139 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 140 | lg2::error("{NUM} error in writing {PATH}: {ERROR}", "NUM", gpioNum, | 
|  | 141 | "PATH", devPath, "ERROR", e); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 142 | return -1; | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | if (gpioDirection == "out") | 
|  | 147 | { | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 148 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/value"; | 
|  | 149 |  | 
|  | 150 | uint32_t currentValue; | 
|  | 151 |  | 
|  | 152 | stream.open(devPath, std::fstream::in); | 
|  | 153 | try | 
|  | 154 | { | 
|  | 155 | stream >> currentValue; | 
|  | 156 | stream.close(); | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | catch (const std::exception& e) | 
|  | 160 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 161 | lg2::error("Error in reading {PATH}: {ERROR}", "PATH", devPath, | 
|  | 162 | "ERROR", e); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 163 | return -1; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | const char* direction = currentValue ? "high" : "low"; | 
|  | 167 |  | 
|  | 168 | devPath.clear(); | 
|  | 169 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/direction"; | 
|  | 170 |  | 
|  | 171 | stream.open(devPath, std::fstream::out); | 
|  | 172 | try | 
|  | 173 | { | 
|  | 174 | stream << direction; | 
|  | 175 | stream.close(); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | catch (const std::exception& e) | 
|  | 179 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 180 | lg2::error("Error in writing: {ERROR}", "ERROR", e); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 181 | return -1; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 | else if (gpioDirection == "in") | 
|  | 185 | { | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 186 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/direction"; | 
|  | 187 |  | 
|  | 188 | stream.open(devPath, std::fstream::out); | 
|  | 189 | try | 
|  | 190 | { | 
|  | 191 | stream << gpioDirection; | 
|  | 192 | stream.close(); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | catch (const std::exception& e) | 
|  | 196 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 197 | lg2::error("Error in writing: {ERROR}", "ERROR", e); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 198 | return -1; | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 | else if ((gpioDirection == "both")) | 
|  | 202 | { | 
| Tim Lee | 582b3f0 | 2019-01-03 15:39:27 +0800 | [diff] [blame] | 203 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/direction"; | 
|  | 204 |  | 
|  | 205 | stream.open(devPath, std::fstream::out); | 
|  | 206 | try | 
|  | 207 | { | 
| George Liu | 94afa4b | 2022-06-20 13:36:43 +0800 | [diff] [blame] | 208 | // Before set gpio configure as an interrupt pin, need to set | 
|  | 209 | // direction as 'in' or edge can't set as 'rising', 'falling' and | 
|  | 210 | // 'both' | 
|  | 211 | const char* in_direction = "in"; | 
| Tim Lee | 582b3f0 | 2019-01-03 15:39:27 +0800 | [diff] [blame] | 212 | stream << in_direction; | 
|  | 213 | stream.close(); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | catch (const std::exception& e) | 
|  | 217 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 218 | lg2::error("Error in writing: {ERROR}", "ERROR", e); | 
| Tim Lee | 582b3f0 | 2019-01-03 15:39:27 +0800 | [diff] [blame] | 219 | return -1; | 
|  | 220 | } | 
|  | 221 | devPath.clear(); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 222 |  | 
|  | 223 | // For gpio configured as ‘both’, it is an interrupt pin and trigged on | 
|  | 224 | // both rising and falling signals | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 225 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/edge"; | 
|  | 226 |  | 
|  | 227 | stream.open(devPath, std::fstream::out); | 
|  | 228 | try | 
|  | 229 | { | 
|  | 230 | stream << gpioDirection; | 
|  | 231 | stream.close(); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | catch (const std::exception& e) | 
|  | 235 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 236 | lg2::error("Error in writing: {ERROR}", "ERROR", e); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 237 | return -1; | 
|  | 238 | } | 
|  | 239 | } | 
|  | 240 |  | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 241 | devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/value"; | 
|  | 242 |  | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 243 | auto fd = ::open(devPath.c_str(), O_RDWR | O_NONBLOCK); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 244 |  | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 245 | if (fd < 0) | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 246 | { | 
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame^] | 247 | lg2::error("Open {PATH} error: {ERROR}", "PATH", devPath, "ERROR", | 
|  | 248 | errno); | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 249 | return -1; | 
|  | 250 | } | 
|  | 251 |  | 
| Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 252 | gpioConfig.fd = fd; | 
|  | 253 |  | 
| Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 254 | return 0; | 
|  | 255 | } |