Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 1 | /** |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 2 | * Copyright © 2016,2018 IBM Corporation |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 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 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 17 | #include "physical.hpp" |
| 18 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <string> |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 21 | namespace phosphor |
| 22 | { |
| 23 | namespace led |
| 24 | { |
| 25 | |
| 26 | /** @brief Populates key parameters */ |
| 27 | void Physical::setInitialState() |
| 28 | { |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 29 | auto trigger = led.getTrigger(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 30 | if (trigger == "timer") |
| 31 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 32 | // LED is blinking. Get the on and off delays and derive percent duty |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 33 | auto delayOn = led.getDelayOn(); |
| 34 | auto delayOff = led.getDelayOff(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 35 | |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 36 | // Calculate duty from millisecond delay values and derive percentage on |
Andrew Jeffery | e5c40fe | 2018-05-25 15:27:18 +0930 | [diff] [blame] | 37 | periodMs = delayOn + delayOff; |
| 38 | auto factor = periodMs / 100; |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 39 | auto dutyOn = delayOn / factor; |
| 40 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 41 | this->dutyOn(dutyOn); |
| 42 | } |
| 43 | else |
| 44 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 45 | // TODO: Periodicity is hardcoded for now. This will be changed to |
| 46 | // this->period() when configurable periodicity is implemented. |
Andrew Jeffery | e5c40fe | 2018-05-25 15:27:18 +0930 | [diff] [blame] | 47 | periodMs = 1000; |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 48 | |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 49 | // Cache current LED state |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 50 | auto brightness = led.getBrightness(); |
| 51 | if (brightness == ASSERT) |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 52 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 53 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state( |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 54 | Action::On); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 58 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state( |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 59 | Action::Off); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 65 | auto Physical::state(Action value) -> Action |
| 66 | { |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 67 | auto current = |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 68 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 69 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 70 | auto requested = |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 71 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 72 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 73 | driveLED(current, requested); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 74 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 75 | return value; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 76 | } |
| 77 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 78 | void Physical::driveLED(Action current, Action request) |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 79 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 80 | if (current == request) |
| 81 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 85 | if (request == Action::Blink) |
| 86 | { |
| 87 | return blinkOperation(); |
| 88 | } |
| 89 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 90 | if (request == Action::On || request == Action::Off) |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 91 | { |
| 92 | return stableStateOperation(request); |
| 93 | } |
| 94 | return; |
| 95 | } |
| 96 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 97 | void Physical::stableStateOperation(Action action) |
| 98 | { |
| 99 | auto value = (action == Action::On) ? ASSERT : DEASSERT; |
| 100 | |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 101 | led.setTrigger("none"); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 102 | led.setBrightness(value); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 106 | void Physical::blinkOperation() |
| 107 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 108 | auto dutyOn = this->dutyOn(); |
| 109 | |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 110 | // Convert percent duty to milliseconds for sysfs interface |
Andrew Jeffery | e5c40fe | 2018-05-25 15:27:18 +0930 | [diff] [blame] | 111 | auto factor = periodMs / 100; |
| 112 | led.setDelayOn(dutyOn * factor); |
Andrew Jeffery | e5c40fe | 2018-05-25 15:27:18 +0930 | [diff] [blame] | 113 | led.setDelayOff((100 - dutyOn) * factor); |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame^] | 114 | |
| 115 | led.setTrigger("timer"); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | } // namespace led |
| 120 | } // namespace phosphor |