1fec38334SShawn McCarney /**
2fec38334SShawn McCarney * Copyright © 2024 IBM Corporation
3fec38334SShawn McCarney *
4fec38334SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License");
5fec38334SShawn McCarney * you may not use this file except in compliance with the License.
6fec38334SShawn McCarney * You may obtain a copy of the License at
7fec38334SShawn McCarney *
8fec38334SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0
9fec38334SShawn McCarney *
10fec38334SShawn McCarney * Unless required by applicable law or agreed to in writing, software
11fec38334SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS,
12fec38334SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fec38334SShawn McCarney * See the License for the specific language governing permissions and
14fec38334SShawn McCarney * limitations under the License.
15fec38334SShawn McCarney */
16fec38334SShawn McCarney
17fec38334SShawn McCarney #include "ucd90320_device.hpp"
18fec38334SShawn McCarney
19fec38334SShawn McCarney #include "format_utils.hpp"
20fec38334SShawn McCarney #include "standard_device.hpp"
21fec38334SShawn McCarney
22fec38334SShawn McCarney #include <array>
23fec38334SShawn McCarney #include <format>
24fec38334SShawn McCarney #include <span>
25fec38334SShawn McCarney
26fec38334SShawn McCarney namespace phosphor::power::sequencer
27fec38334SShawn McCarney {
28fec38334SShawn McCarney
29fec38334SShawn McCarney /**
30fec38334SShawn McCarney * Group of GPIO values that should be formatted together.
31fec38334SShawn McCarney */
32fec38334SShawn McCarney struct GPIOGroup
33fec38334SShawn McCarney {
34fec38334SShawn McCarney std::string additionalDataName;
35fec38334SShawn McCarney std::string journalName;
36fec38334SShawn McCarney unsigned int offset;
37fec38334SShawn McCarney unsigned int count;
38fec38334SShawn McCarney };
39fec38334SShawn McCarney
40fec38334SShawn McCarney /**
41fec38334SShawn McCarney * UCD90320-specific groups of GPIO values.
42fec38334SShawn McCarney *
43fec38334SShawn McCarney * The offsets correspond to the Pin IDs defined in the UCD90320 PMBus interface
44fec38334SShawn McCarney * documentation. These Pin IDs are the same as the libgpiod line offsets used
45fec38334SShawn McCarney * to obtain the GPIO values.
46fec38334SShawn McCarney */
47fec38334SShawn McCarney static const std::array<GPIOGroup, 5> gpioGroups = {
48fec38334SShawn McCarney GPIOGroup{"MAR01_24_GPIO_VALUES", "MAR01-24", 0, 24},
49fec38334SShawn McCarney GPIOGroup{"EN1_32_GPIO_VALUES", "EN1-32", 24, 32},
50fec38334SShawn McCarney GPIOGroup{"LGP01_16_GPIO_VALUES", "LGP01-16", 56, 16},
51fec38334SShawn McCarney GPIOGroup{"DMON1_8_GPIO_VALUES", "DMON1-8", 72, 8},
52fec38334SShawn McCarney GPIOGroup{"GPIO1_4_GPIO_VALUES", "GPIO1-4", 80, 4}};
53fec38334SShawn McCarney
storeGPIOValues(Services & services,const std::vector<int> & values,std::map<std::string,std::string> & additionalData)54fec38334SShawn McCarney void UCD90320Device::storeGPIOValues(
55fec38334SShawn McCarney Services& services, const std::vector<int>& values,
56fec38334SShawn McCarney std::map<std::string, std::string>& additionalData)
57fec38334SShawn McCarney {
58fec38334SShawn McCarney // Verify the expected number of GPIO values were passed in
59*f5402197SPatrick Williams unsigned int expectedCount =
60*f5402197SPatrick Williams gpioGroups.back().offset + gpioGroups.back().count;
61fec38334SShawn McCarney if (values.size() != expectedCount)
62fec38334SShawn McCarney {
63fec38334SShawn McCarney // Unexpected number of values; store as a plain list of integers
64fec38334SShawn McCarney StandardDevice::storeGPIOValues(services, values, additionalData);
65fec38334SShawn McCarney return;
66fec38334SShawn McCarney }
67fec38334SShawn McCarney
68fec38334SShawn McCarney // Store GPIO groups in additional data and journal
69fec38334SShawn McCarney services.logInfoMsg(std::format("Device {} GPIO values:", name));
70fec38334SShawn McCarney auto span = std::span{values};
71fec38334SShawn McCarney std::string valuesStr;
72fec38334SShawn McCarney for (const GPIOGroup& group : gpioGroups)
73fec38334SShawn McCarney {
74fec38334SShawn McCarney valuesStr =
75fec38334SShawn McCarney format_utils::toString(span.subspan(group.offset, group.count));
76fec38334SShawn McCarney additionalData.emplace(group.additionalDataName, valuesStr);
77fec38334SShawn McCarney services.logInfoMsg(
78fec38334SShawn McCarney std::format("{}: {}", group.journalName, valuesStr));
79fec38334SShawn McCarney }
80fec38334SShawn McCarney }
81fec38334SShawn McCarney
82fec38334SShawn McCarney } // namespace phosphor::power::sequencer
83