blob: 1aa63a46955a156ba8a33cce840f5e2ea766a801 [file] [log] [blame]
Brad Bishop5c589482017-06-14 22:32:20 -04001/**
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 */
Matthew Barth2d2caa32020-05-26 11:07:24 -050016#include "gpio.hpp"
17
18#include "rpolicy.hpp"
19
Brad Bishop5c589482017-06-14 22:32:20 -040020#include <phosphor-logging/elog-errors.hpp>
21#include <phosphor-logging/elog.hpp>
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070022#include <sdeventplus/event.hpp>
Brad Bishop5c589482017-06-14 22:32:20 -040023#include <xyz/openbmc_project/Common/Callout/error.hpp>
Matthew Barth2d2caa32020-05-26 11:07:24 -050024
25#include <functional>
26#include <tuple>
Brad Bishop5c589482017-06-14 22:32:20 -040027
28namespace phosphor
29{
30namespace fan
31{
32namespace presence
33{
34
Matthew Barth2d2caa32020-05-26 11:07:24 -050035Gpio::Gpio(const std::string& physDevice, const std::string& device,
36 unsigned int physPin) :
Brad Bishop5c589482017-06-14 22:32:20 -040037 currentState(false),
Brad Bishop2e9788d2017-07-28 22:24:03 -040038 evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)),
Matthew Barth2d2caa32020-05-26 11:07:24 -050039 evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin)
40{}
Brad Bishop5c589482017-06-14 22:32:20 -040041
42bool Gpio::start()
43{
Matthew Barth2d2caa32020-05-26 11:07:24 -050044 source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN,
45 std::bind(&Gpio::ioCallback, this));
Brad Bishop5c589482017-06-14 22:32:20 -040046 currentState = present();
47 return currentState;
48}
49
50void Gpio::stop()
51{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070052 source.reset();
Brad Bishop5c589482017-06-14 22:32:20 -040053}
54
55bool Gpio::present()
56{
57 return evdev.fetch(EV_KEY, pin) != 0;
58}
59
60void Gpio::fail()
61{
62 using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error;
63 using namespace phosphor::logging;
64 using namespace xyz::openbmc_project::Common::Callout;
65
66 report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050067 GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0),
68 GPIO::CALLOUT_DEVICE_PATH(phys.c_str()));
Brad Bishop5c589482017-06-14 22:32:20 -040069}
70
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070071void Gpio::ioCallback()
Brad Bishop5c589482017-06-14 22:32:20 -040072{
73 unsigned int type, code, value;
74
75 std::tie(type, code, value) = evdev.next();
76 if (type != EV_KEY || code != pin)
77 {
78 return;
79 }
80
81 bool newState = value != 0;
82
83 if (currentState != newState)
84 {
Brad Bishop11083ec2017-07-25 19:08:53 -040085 getPolicy().stateChanged(newState, *this);
Brad Bishop5c589482017-06-14 22:32:20 -040086 currentState = newState;
87 }
88}
89} // namespace presence
90} // namespace fan
91} // namespace phosphor