xref: /openbmc/phosphor-power/phosphor-power-sequencer/src/basic_device.hpp (revision 739446763f6f398dac57e82e44b07ea198df4761)
1 /**
2  * Copyright © 2025 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 #pragma once
17 
18 #include "gpio.hpp"
19 #include "power_sequencer_device.hpp"
20 #include "rail.hpp"
21 #include "services.hpp"
22 
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace phosphor::power::sequencer
30 {
31 
32 /**
33  * @class BasicDevice
34  *
35  * PowerSequencerDevice sub-class that implements basic functionality.
36  *
37  * BasicDevice implements the following:
38  * - Data members and methods for the power sequencer properties from the JSON
39  *   configuration file.
40  * - Methods that utilize the named GPIOs, such as powerOn() and getPowerGood().
41  */
42 class BasicDevice : public PowerSequencerDevice
43 {
44   public:
45     BasicDevice() = delete;
46     BasicDevice(const BasicDevice&) = delete;
47     BasicDevice(BasicDevice&&) = delete;
48     BasicDevice& operator=(const BasicDevice&) = delete;
49     BasicDevice& operator=(BasicDevice&&) = delete;
50     virtual ~BasicDevice() = default;
51 
52     /**
53      * Constructor.
54      *
55      * Throws an exception if an error occurs during initialization.
56      *
57      * @param name device name
58      * @param bus I2C bus for the device
59      * @param address I2C address for the device
60      * @param powerControlGPIOName name of the GPIO that turns this device on
61      *                             and off
62      * @param powerGoodGPIOName name of the GPIO that reads the power good
63      *                          signal from this device
64      * @param rails voltage rails that are enabled and monitored by this device
65      * @param services System services like hardware presence and the journal
66      */
BasicDevice(const std::string & name,uint8_t bus,uint16_t address,const std::string & powerControlGPIOName,const std::string & powerGoodGPIOName,std::vector<std::unique_ptr<Rail>> rails,Services & services)67     explicit BasicDevice(const std::string& name, uint8_t bus, uint16_t address,
68                          const std::string& powerControlGPIOName,
69                          const std::string& powerGoodGPIOName,
70                          std::vector<std::unique_ptr<Rail>> rails,
71                          Services& services) :
72         name{name}, bus{bus}, address{address},
73         powerControlGPIOName{powerControlGPIOName},
74         powerGoodGPIOName{powerGoodGPIOName}, rails{std::move(rails)}
75     {
76         powerControlGPIO = services.createGPIO(powerControlGPIOName);
77 
78         powerGoodGPIO = services.createGPIO(powerGoodGPIOName);
79         powerGoodGPIO->requestRead();
80     }
81 
82     /** @copydoc PowerSequencerDevice::getName() */
getName() const83     virtual const std::string& getName() const override
84     {
85         return name;
86     }
87 
88     /** @copydoc PowerSequencerDevice::getBus() */
getBus() const89     virtual uint8_t getBus() const override
90     {
91         return bus;
92     }
93 
94     /** @copydoc PowerSequencerDevice::getAddress() */
getAddress() const95     virtual uint16_t getAddress() const override
96     {
97         return address;
98     }
99 
100     /** @copydoc PowerSequencerDevice::getPowerControlGPIOName() */
getPowerControlGPIOName() const101     virtual const std::string& getPowerControlGPIOName() const override
102     {
103         return powerControlGPIOName;
104     }
105 
106     /** @copydoc PowerSequencerDevice::getPowerGoodGPIOName() */
getPowerGoodGPIOName() const107     virtual const std::string& getPowerGoodGPIOName() const override
108     {
109         return powerGoodGPIOName;
110     }
111 
112     /** @copydoc PowerSequencerDevice::getRails() */
getRails() const113     virtual const std::vector<std::unique_ptr<Rail>>& getRails() const override
114     {
115         return rails;
116     }
117 
118     /** @copydoc PowerSequencerDevice::getPowerControlGPIO() */
getPowerControlGPIO()119     virtual GPIO& getPowerControlGPIO() override
120     {
121         return *powerControlGPIO;
122     }
123 
124     /** @copydoc PowerSequencerDevice::getPowerGoodGPIO() */
getPowerGoodGPIO()125     virtual GPIO& getPowerGoodGPIO() override
126     {
127         return *powerGoodGPIO;
128     }
129 
130     /** @copydoc PowerSequencerDevice::powerOn() */
powerOn()131     virtual void powerOn() override
132     {
133         powerControlGPIO->requestWrite(1);
134         powerControlGPIO->setValue(1);
135         powerControlGPIO->release();
136     }
137 
138     /** @copydoc PowerSequencerDevice::powerOff() */
powerOff()139     virtual void powerOff() override
140     {
141         powerControlGPIO->requestWrite(0);
142         powerControlGPIO->setValue(0);
143         powerControlGPIO->release();
144     }
145 
146     /** @copydoc PowerSequencerDevice::getPowerGood() */
getPowerGood()147     virtual bool getPowerGood() override
148     {
149         return (powerGoodGPIO->getValue() == 1);
150     }
151 
152   protected:
153     /**
154      * Device name.
155      */
156     std::string name{};
157 
158     /**
159      * I2C bus for the device.
160      */
161     uint8_t bus;
162 
163     /**
164      * I2C address for the device.
165      */
166     uint16_t address;
167 
168     /**
169      * Name of the GPIO that turns this device on and off.
170      */
171     std::string powerControlGPIOName{};
172 
173     /**
174      * Name of the GPIO that reads the power good signal from this device.
175      */
176     std::string powerGoodGPIOName{};
177 
178     /**
179      * Voltage rails that are enabled and monitored by this device.
180      */
181     std::vector<std::unique_ptr<Rail>> rails{};
182 
183     /**
184      * GPIO that turns this device on and off.
185      */
186     std::unique_ptr<GPIO> powerControlGPIO{};
187 
188     /**
189      * GPIO that reads the power good signal from this device.
190      */
191     std::unique_ptr<GPIO> powerGoodGPIO{};
192 };
193 
194 } // namespace phosphor::power::sequencer
195