blob: a75a6fc1a2d4a8fa59357112aef29acc0d00c34f [file] [log] [blame]
Jim Wrightc48551a2022-12-22 15:43:14 -06001/**
2 * Copyright © 2022 IBM 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
17#include "ucd90160_monitor.hpp"
18
Shawn McCarney328ca312023-07-11 08:18:41 -050019#include <fmt/format.h>
20#include <fmt/ranges.h>
21
22#include <phosphor-logging/log.hpp>
23
24#include <algorithm>
25#include <array>
26#include <span>
27
Jim Wrightc48551a2022-12-22 15:43:14 -060028namespace phosphor::power::sequencer
29{
30
Shawn McCarney328ca312023-07-11 08:18:41 -050031using namespace phosphor::logging;
32
33// Names of the UCD90160 GPIOs. The array indices correspond to the Pin IDs
34// defined in the UCD90160 PMBus interface documentation. These Pin IDs are the
35// same as the libgpiod line offsets used to obtain the GPIO values.
36static constexpr std::array<const char*, 26> gpioNames = {
37 "FPWM1_GPIO5", "FPWM2_GPIO6", "FPWM3_GPIO7", "FPWM4_GPIO8",
38 "FPWM5_GPIO9", "FPWM6_GPIO10", "FPWM7_GPIO11", "FPWM8_GPIO12",
39 "GPI1_PWM1", "GPI2_PWM2", "GPI3_PWM3", "GPI4_PWM4",
40 "GPIO14", "GPIO15", "TDO_GPIO20", "TCK_GPIO19",
41 "TMS_GPIO22", "TDI_GPIO21", "GPIO1", "GPIO2",
42 "GPIO3", "GPIO4", "GPIO13", "GPIO16",
43 "GPIO17", "GPIO18"};
44
Jim Wrightc48551a2022-12-22 15:43:14 -060045UCD90160Monitor::UCD90160Monitor(sdbusplus::bus_t& bus, std::uint8_t i2cBus,
46 std::uint16_t i2cAddress) :
47 UCD90xMonitor(bus, i2cBus, i2cAddress, "UCD90160", 16)
48{}
49
Shawn McCarney328ca312023-07-11 08:18:41 -050050void UCD90160Monitor::formatGpioValues(
51 const std::vector<int>& values, unsigned int numberLines,
52 std::map<std::string, std::string>& additionalData) const
53{
54 // Verify the expected number of GPIO values were passed in
55 if ((values.size() == gpioNames.size()) &&
56 (numberLines == gpioNames.size()))
57 {
58 // Store GPIO names and values in additional data and journal.
59 // Use groups of GPIOs in journal to minimize number of entries.
60 unsigned int groupSize{4};
61 for (unsigned int i = 0; i < gpioNames.size(); ++i)
62 {
63 additionalData.emplace(gpioNames[i], std::to_string(values[i]));
64 if ((i % groupSize) == 0)
65 {
66 unsigned int gpiosLeft = gpioNames.size() - i;
67 unsigned int count = std::min(groupSize, gpiosLeft);
68 log<level::INFO>(
69 fmt::format("GPIO values: {}: {}",
70 std::span{gpioNames.begin() + i, count},
71 std::span{values.begin() + i, count})
72 .c_str());
73 }
74 }
75 }
76 else
77 {
78 // Unexpected number of GPIO values. Store without names.
79 additionalData.emplace("GPIO_VALUES", fmt::format("{}", values));
80 log<level::INFO>(fmt::format("GPIO values: {}", values).c_str());
81 }
82}
83
Jim Wrightc48551a2022-12-22 15:43:14 -060084} // namespace phosphor::power::sequencer