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 | #pragma once |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 17 | |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 18 | #include <nlohmann/json.hpp> |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 19 | #include <sdbusplus/bus.hpp> |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 20 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 23 | |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 24 | // enum to represent gpio states |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 25 | enum class GpioState |
| 26 | { |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 27 | assert, |
| 28 | deassert, |
| 29 | invalid |
| 30 | }; |
| 31 | // enum to represent gpio polarity |
| 32 | enum class GpioPolarity |
| 33 | { |
| 34 | activeLow, |
| 35 | activeHigh |
| 36 | }; |
| 37 | |
| 38 | struct GPIOBufferValue |
| 39 | { |
| 40 | char assert; |
| 41 | char deassert; |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 42 | }; |
| 43 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 44 | // this struct has the gpio config for single gpio |
| 45 | struct gpioInfo |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 46 | { |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 47 | int fd; // io fd mapped with the gpio |
| 48 | uint32_t number; |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 49 | std::string name; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 50 | std::string direction; |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 51 | GpioPolarity polarity; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // this struct represents button interface |
| 55 | struct buttonConfig |
| 56 | { |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 57 | std::string formFactorName; // name of the button interface |
| 58 | std::vector<gpioInfo> gpios; // holds single or group gpio config |
| 59 | nlohmann::json extraJsonInfo; // corresponding to button interface |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * @brief iterates over the list of gpios and configures gpios them |
| 64 | * config which is set from gpio defs json file. |
| 65 | * The fd of the configured gpio is stored in buttonConfig.gpios container |
| 66 | * @return int returns 0 on successful config of all gpios |
| 67 | */ |
| 68 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 69 | int configGroupGpio(buttonConfig& buttonCfg); |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * @brief configures and initializes the single gpio |
| 73 | * @return int returns 0 on successful config of all gpios |
| 74 | */ |
| 75 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 76 | int configGpio(gpioInfo& gpioConfig); |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 77 | |
| 78 | uint32_t getGpioNum(const std::string& gpioPin); |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 79 | // Set gpio state based on polarity |
| 80 | void setGpioState(int fd, GpioPolarity polarity, GpioState state); |
| 81 | // Get gpio state based on polarity |
| 82 | GpioState getGpioState(int fd, GpioPolarity polarity); |
| 83 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 84 | void closeGpio(int fd); |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 85 | // global json object which holds gpio_defs.json configs |
| 86 | extern nlohmann::json gpioDefs; |