blob: 3014370ae51dcda5d9785aa17ff624f3c980624f [file] [log] [blame]
Kuiying Wang64ff7ce2018-08-15 11:17:06 +08001/*
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 Feist676d2c32019-02-14 10:07:27 -080018bool PowerControl::forcePowerOff()
Kuiying Wang80f6d922018-11-13 16:34:07 +080019{
James Feist676d2c32019-02-14 10:07:27 -080020 return true;
Kuiying Wang80f6d922018-11-13 16:34:07 +080021}
22
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080023int32_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 Venture8fbed2e2018-11-01 19:25:31 -070039
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080040 ret = ::lseek(power_up_fd, 0, SEEK_SET);
41 if (ret < 0)
42 {
Patrick Venture8fbed2e2018-11-01 19:25:31 -070043 phosphor::logging::log<phosphor::logging::level::ERR>("lseek error!");
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080044 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 Venture8fbed2e2018-11-01 19:25:31 -070061 "write error for setting 0 !");
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080062 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
63 IOError();
64 }
65
66 std::this_thread::sleep_for(
Patrick Venture8fbed2e2018-11-01 19:25:31 -070067 std::chrono::milliseconds(POWER_UP_PIN_PULSE_TIME_MS));
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080068
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 Venture8fbed2e2018-11-01 19:25:31 -070074 "write error for setting 1 !");
Kuiying Wang64ff7ce2018-08-15 11:17:06 +080075 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
76 IOError();
77 }
78
79 state(newState);
80 return 0;
81}
82
83int32_t PowerControl::getPowerState()
84{
85 return state();
86}