15c589487SBrad Bishop /** 25c589487SBrad Bishop * Copyright © 2017 IBM Corporation 35c589487SBrad Bishop * 45c589487SBrad Bishop * Licensed under the Apache License, Version 2.0 (the "License"); 55c589487SBrad Bishop * you may not use this file except in compliance with the License. 65c589487SBrad Bishop * You may obtain a copy of the License at 75c589487SBrad Bishop * 85c589487SBrad Bishop * http://www.apache.org/licenses/LICENSE-2.0 95c589487SBrad Bishop * 105c589487SBrad Bishop * Unless required by applicable law or agreed to in writing, software 115c589487SBrad Bishop * distributed under the License is distributed on an "AS IS" BASIS, 125c589487SBrad Bishop * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135c589487SBrad Bishop * See the License for the specific language governing permissions and 145c589487SBrad Bishop * limitations under the License. 155c589487SBrad Bishop */ 16*2d2caa34SMatthew Barth #include "gpio.hpp" 17*2d2caa34SMatthew Barth 18*2d2caa34SMatthew Barth #include "rpolicy.hpp" 19*2d2caa34SMatthew Barth 205c589487SBrad Bishop #include <phosphor-logging/elog-errors.hpp> 215c589487SBrad Bishop #include <phosphor-logging/elog.hpp> 221cfc2f11SWilliam A. Kennington III #include <sdeventplus/event.hpp> 235c589487SBrad Bishop #include <xyz/openbmc_project/Common/Callout/error.hpp> 24*2d2caa34SMatthew Barth 25*2d2caa34SMatthew Barth #include <functional> 26*2d2caa34SMatthew Barth #include <tuple> 275c589487SBrad Bishop 285c589487SBrad Bishop namespace phosphor 295c589487SBrad Bishop { 305c589487SBrad Bishop namespace fan 315c589487SBrad Bishop { 325c589487SBrad Bishop namespace presence 335c589487SBrad Bishop { 345c589487SBrad Bishop 35*2d2caa34SMatthew Barth Gpio::Gpio(const std::string& physDevice, const std::string& device, 365c589487SBrad Bishop unsigned int physPin) : 375c589487SBrad Bishop currentState(false), 382e9788d7SBrad Bishop evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)), 39*2d2caa34SMatthew Barth evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin) 40*2d2caa34SMatthew Barth {} 415c589487SBrad Bishop 425c589487SBrad Bishop bool Gpio::start() 435c589487SBrad Bishop { 44*2d2caa34SMatthew Barth source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN, 451cfc2f11SWilliam A. Kennington III std::bind(&Gpio::ioCallback, this)); 465c589487SBrad Bishop currentState = present(); 475c589487SBrad Bishop return currentState; 485c589487SBrad Bishop } 495c589487SBrad Bishop 505c589487SBrad Bishop void Gpio::stop() 515c589487SBrad Bishop { 521cfc2f11SWilliam A. Kennington III source.reset(); 535c589487SBrad Bishop } 545c589487SBrad Bishop 555c589487SBrad Bishop bool Gpio::present() 565c589487SBrad Bishop { 575c589487SBrad Bishop return evdev.fetch(EV_KEY, pin) != 0; 585c589487SBrad Bishop } 595c589487SBrad Bishop 605c589487SBrad Bishop void Gpio::fail() 615c589487SBrad Bishop { 625c589487SBrad Bishop using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error; 635c589487SBrad Bishop using namespace phosphor::logging; 645c589487SBrad Bishop using namespace xyz::openbmc_project::Common::Callout; 655c589487SBrad Bishop 665c589487SBrad Bishop report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>( 67*2d2caa34SMatthew Barth GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0), 685c589487SBrad Bishop GPIO::CALLOUT_DEVICE_PATH(phys.c_str())); 695c589487SBrad Bishop } 705c589487SBrad Bishop 711cfc2f11SWilliam A. Kennington III void Gpio::ioCallback() 725c589487SBrad Bishop { 735c589487SBrad Bishop unsigned int type, code, value; 745c589487SBrad Bishop 755c589487SBrad Bishop std::tie(type, code, value) = evdev.next(); 765c589487SBrad Bishop if (type != EV_KEY || code != pin) 775c589487SBrad Bishop { 785c589487SBrad Bishop return; 795c589487SBrad Bishop } 805c589487SBrad Bishop 815c589487SBrad Bishop bool newState = value != 0; 825c589487SBrad Bishop 835c589487SBrad Bishop if (currentState != newState) 845c589487SBrad Bishop { 8511083ecaSBrad Bishop getPolicy().stateChanged(newState, *this); 865c589487SBrad Bishop currentState = newState; 875c589487SBrad Bishop } 885c589487SBrad Bishop } 895c589487SBrad Bishop } // namespace presence 905c589487SBrad Bishop } // namespace fan 915c589487SBrad Bishop } // namespace phosphor 92