blob: 2351da9841b3b81b1d6eac47aa7e97df3240aafb [file] [log] [blame]
Kuiying Wanga9d39e32018-08-14 13:47:32 +08001/*
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 Venture0d9377d2018-11-01 19:34:59 -070017
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053018#include <nlohmann/json.hpp>
Patrick Venture0d9377d2018-11-01 19:34:59 -070019#include <sdbusplus/bus.hpp>
George Liu5b98f4d2022-06-20 13:31:14 +080020
Naveen Mosesdd5495c2021-12-03 22:40:46 +053021#include <string>
22#include <vector>
Patrick Venture0d9377d2018-11-01 19:34:59 -070023
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080024struct ButtonConfig;
25
Naveen Mosesd219fa32022-07-20 00:01:46 +053026// enum to represent gpio states
Naveen Mosesa6d4e652022-04-13 19:27:25 +053027enum class GpioState
28{
Naveen Mosesd219fa32022-07-20 00:01:46 +053029 assert,
30 deassert,
31 invalid
32};
33// enum to represent gpio polarity
34enum class GpioPolarity
35{
36 activeLow,
37 activeHigh
38};
39
40struct GPIOBufferValue
41{
42 char assert;
43 char deassert;
Naveen Mosesa6d4e652022-04-13 19:27:25 +053044};
45
Naveen Mosesdd5495c2021-12-03 22:40:46 +053046// this struct has the gpio config for single gpio
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080047struct GpioInfo
Matt Spinler8605bdf2018-11-05 14:55:46 -060048{
Naveen Mosesdd5495c2021-12-03 22:40:46 +053049 int fd; // io fd mapped with the gpio
50 uint32_t number;
Naveen Mosesd219fa32022-07-20 00:01:46 +053051 std::string name;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053052 std::string direction;
Naveen Mosesd219fa32022-07-20 00:01:46 +053053 GpioPolarity polarity;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053054};
55
Naveen Mosesdd5495c2021-12-03 22:40:46 +053056/**
57 * @brief iterates over the list of gpios and configures gpios them
58 * config which is set from gpio defs json file.
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080059 * The fd of the configured gpio is stored in ButtonConfig.gpios container
Naveen Mosesdd5495c2021-12-03 22:40:46 +053060 * @return int returns 0 on successful config of all gpios
61 */
62
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080063int configGroupGpio(ButtonConfig& buttonCfg);
Naveen Mosesdd5495c2021-12-03 22:40:46 +053064
65/**
66 * @brief configures and initializes the single gpio
67 * @return int returns 0 on successful config of all gpios
68 */
69
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080070int configGpio(GpioInfo& gpioConfig, ButtonConfig& buttonIFConfig);
Naveen Mosesdd5495c2021-12-03 22:40:46 +053071
72uint32_t getGpioNum(const std::string& gpioPin);
Naveen Mosesd219fa32022-07-20 00:01:46 +053073// Set gpio state based on polarity
74void setGpioState(int fd, GpioPolarity polarity, GpioState state);
75// Get gpio state based on polarity
76GpioState getGpioState(int fd, GpioPolarity polarity);
77
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053078// global json object which holds gpio_defs.json configs
79extern nlohmann::json gpioDefs;