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 */ 165c589487SBrad Bishop #include <memory> 175c589487SBrad Bishop #include <phosphor-logging/elog-errors.hpp> 185c589487SBrad Bishop #include <phosphor-logging/elog.hpp> 195c589487SBrad Bishop #include <tuple> 205c589487SBrad Bishop #include <xyz/openbmc_project/Common/Callout/error.hpp> 215c589487SBrad Bishop #include "gpio.hpp" 225c589487SBrad Bishop #include "rpolicy.hpp" 235c589487SBrad Bishop #include "sdevent.hpp" 245c589487SBrad Bishop 255c589487SBrad Bishop namespace phosphor 265c589487SBrad Bishop { 275c589487SBrad Bishop namespace fan 285c589487SBrad Bishop { 295c589487SBrad Bishop namespace presence 305c589487SBrad Bishop { 315c589487SBrad Bishop 325c589487SBrad Bishop Gpio::Gpio( 335c589487SBrad Bishop const std::string& physDevice, 345c589487SBrad Bishop unsigned int physPin) : 355c589487SBrad Bishop currentState(false), 365c589487SBrad Bishop evdevfd(open("/dev/input/by-path/platform-gpio-keys-event", 375c589487SBrad Bishop O_RDONLY | O_NONBLOCK)), 385c589487SBrad Bishop evdev(evdevpp::evdev::newFromFD(evdevfd())), 395c589487SBrad Bishop phys(physDevice), 405c589487SBrad Bishop pin(physPin), 415c589487SBrad Bishop callback(nullptr) 425c589487SBrad Bishop { 435c589487SBrad Bishop 445c589487SBrad Bishop } 455c589487SBrad Bishop 465c589487SBrad Bishop bool Gpio::start() 475c589487SBrad Bishop { 485c589487SBrad Bishop callback = std::make_unique<sdevent::event::io::IO>( 495c589487SBrad Bishop util::SDEvent::getEvent(), 505c589487SBrad Bishop evdevfd(), 515c589487SBrad Bishop [this](auto& s){this->ioCallback(s);}); 525c589487SBrad Bishop currentState = present(); 535c589487SBrad Bishop return currentState; 545c589487SBrad Bishop } 555c589487SBrad Bishop 565c589487SBrad Bishop void Gpio::stop() 575c589487SBrad Bishop { 585c589487SBrad Bishop callback = nullptr; 595c589487SBrad Bishop } 605c589487SBrad Bishop 615c589487SBrad Bishop bool Gpio::present() 625c589487SBrad Bishop { 635c589487SBrad Bishop return evdev.fetch(EV_KEY, pin) != 0; 645c589487SBrad Bishop } 655c589487SBrad Bishop 665c589487SBrad Bishop void Gpio::fail() 675c589487SBrad Bishop { 685c589487SBrad Bishop using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error; 695c589487SBrad Bishop using namespace phosphor::logging; 705c589487SBrad Bishop using namespace xyz::openbmc_project::Common::Callout; 715c589487SBrad Bishop 725c589487SBrad Bishop report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>( 735c589487SBrad Bishop GPIO::CALLOUT_GPIO_NUM(pin), 745c589487SBrad Bishop GPIO::CALLOUT_ERRNO(0), 755c589487SBrad Bishop GPIO::CALLOUT_DEVICE_PATH(phys.c_str())); 765c589487SBrad Bishop } 775c589487SBrad Bishop 785c589487SBrad Bishop void Gpio::ioCallback(sdevent::source::Source& source) 795c589487SBrad Bishop { 805c589487SBrad Bishop unsigned int type, code, value; 815c589487SBrad Bishop 825c589487SBrad Bishop std::tie(type, code, value) = evdev.next(); 835c589487SBrad Bishop if (type != EV_KEY || code != pin) 845c589487SBrad Bishop { 855c589487SBrad Bishop return; 865c589487SBrad Bishop } 875c589487SBrad Bishop 885c589487SBrad Bishop bool newState = value != 0; 895c589487SBrad Bishop 905c589487SBrad Bishop if (currentState != newState) 915c589487SBrad Bishop { 92*11083ecaSBrad Bishop getPolicy().stateChanged(newState, *this); 935c589487SBrad Bishop currentState = newState; 945c589487SBrad Bishop } 955c589487SBrad Bishop } 965c589487SBrad Bishop } // namespace presence 975c589487SBrad Bishop } // namespace fan 985c589487SBrad Bishop } // namespace phosphor 99