1 /**
2  * Copyright © 2024 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 "ucd90320_device.hpp"
18 
19 #include "format_utils.hpp"
20 #include "standard_device.hpp"
21 
22 #include <array>
23 #include <format>
24 #include <span>
25 
26 namespace phosphor::power::sequencer
27 {
28 
29 /**
30  * Group of GPIO values that should be formatted together.
31  */
32 struct GPIOGroup
33 {
34     std::string additionalDataName;
35     std::string journalName;
36     unsigned int offset;
37     unsigned int count;
38 };
39 
40 /**
41  * UCD90320-specific groups of GPIO values.
42  *
43  * The offsets correspond to the Pin IDs defined in the UCD90320 PMBus interface
44  * documentation.  These Pin IDs are the same as the libgpiod line offsets used
45  * to obtain the GPIO values.
46  */
47 static const std::array<GPIOGroup, 5> gpioGroups = {
48     GPIOGroup{"MAR01_24_GPIO_VALUES", "MAR01-24", 0, 24},
49     GPIOGroup{"EN1_32_GPIO_VALUES", "EN1-32", 24, 32},
50     GPIOGroup{"LGP01_16_GPIO_VALUES", "LGP01-16", 56, 16},
51     GPIOGroup{"DMON1_8_GPIO_VALUES", "DMON1-8", 72, 8},
52     GPIOGroup{"GPIO1_4_GPIO_VALUES", "GPIO1-4", 80, 4}};
53 
storeGPIOValues(Services & services,const std::vector<int> & values,std::map<std::string,std::string> & additionalData)54 void UCD90320Device::storeGPIOValues(
55     Services& services, const std::vector<int>& values,
56     std::map<std::string, std::string>& additionalData)
57 {
58     // Verify the expected number of GPIO values were passed in
59     unsigned int expectedCount = gpioGroups.back().offset +
60                                  gpioGroups.back().count;
61     if (values.size() != expectedCount)
62     {
63         // Unexpected number of values; store as a plain list of integers
64         StandardDevice::storeGPIOValues(services, values, additionalData);
65         return;
66     }
67 
68     // Store GPIO groups in additional data and journal
69     services.logInfoMsg(std::format("Device {} GPIO values:", name));
70     auto span = std::span{values};
71     std::string valuesStr;
72     for (const GPIOGroup& group : gpioGroups)
73     {
74         valuesStr =
75             format_utils::toString(span.subspan(group.offset, group.count));
76         additionalData.emplace(group.additionalDataName, valuesStr);
77         services.logInfoMsg(
78             std::format("{}: {}", group.journalName, valuesStr));
79     }
80 }
81 
82 } // namespace phosphor::power::sequencer
83