Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +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 | #include "power_control.hpp" |
| 17 | |
| 18 | int32_t PowerControl::setPowerState(int32_t newState) |
| 19 | { |
| 20 | int ret = 0; |
| 21 | int count = 0; |
| 22 | char buf = '0'; |
| 23 | |
| 24 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 25 | "setPowerState", phosphor::logging::entry("NEWSTATE=%d", newState)); |
| 26 | |
| 27 | if (state() == newState) |
| 28 | { |
| 29 | phosphor::logging::log<phosphor::logging::level::WARNING>( |
| 30 | "Same powerstate", |
| 31 | phosphor::logging::entry("NEWSTATE=%d", newState)); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | ret = ::lseek(power_up_fd, 0, SEEK_SET); |
| 36 | if (ret < 0) |
| 37 | { |
| 38 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 39 | "lseek error!"); |
| 40 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 41 | IOError(); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | This power control just handle out pin "POWER_UP_PIN", change it |
| 46 | to low "0" form high "1" and set it back to high after over 200ms, |
| 47 | which will notify host (PCH) to switch power. Host to determine it |
| 48 | is power on or power off operation based on current power status. |
| 49 | For BMC (power control), just need to notify host (PCH) to switch |
| 50 | power, don't need to judge it should power on or off. |
| 51 | */ |
| 52 | buf = '0'; |
| 53 | ret = ::write(power_up_fd, &buf, sizeof(buf)); |
| 54 | if (ret < 0) |
| 55 | { |
| 56 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 57 | "write error for setting 0 !"); |
| 58 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 59 | IOError(); |
| 60 | } |
| 61 | |
| 62 | std::this_thread::sleep_for( |
| 63 | std::chrono::milliseconds(POWER_UP_PIN_PULSE_TIME_MS)); |
| 64 | |
| 65 | buf = '1'; |
| 66 | ret = ::write(power_up_fd, &buf, sizeof(buf)); |
| 67 | if (ret < 0) |
| 68 | { |
| 69 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 70 | "write error for setting 1 !"); |
| 71 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 72 | IOError(); |
| 73 | } |
| 74 | |
| 75 | state(newState); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int32_t PowerControl::getPowerState() |
| 80 | { |
| 81 | return state(); |
| 82 | } |