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