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