xref: /openbmc/phosphor-power/tools/i2c/i2c.hpp (revision 1d103428)
1ab1327c3SLei YU #pragma once
2ab1327c3SLei YU 
3ab1327c3SLei YU #include "i2c_interface.hpp"
4ab1327c3SLei YU 
5ab1327c3SLei YU namespace i2c
6ab1327c3SLei YU {
7ab1327c3SLei YU 
8ab1327c3SLei YU class I2CDevice : public I2CInterface
9ab1327c3SLei YU {
10ab1327c3SLei YU   private:
11ab1327c3SLei YU     I2CDevice() = delete;
12ab1327c3SLei YU 
139af82a5cSLei YU     /** @brief Constructor
149af82a5cSLei YU      *
159af82a5cSLei YU      * Construct I2CDevice object from the bus id and device address
169af82a5cSLei YU      *
179af82a5cSLei YU      * @param[in] busId - The i2c bus ID
189af82a5cSLei YU      * @param[in] devAddr - The device address of the I2C device
199af82a5cSLei YU      */
209af82a5cSLei YU     explicit I2CDevice(uint8_t busId, uint8_t devAddr) :
219af82a5cSLei YU         busId(busId), devAddr(devAddr)
22ab1327c3SLei YU     {
239af82a5cSLei YU         busStr = "/dev/i2c-" + std::to_string(busId);
249af82a5cSLei YU         open();
25ab1327c3SLei YU     }
26ab1327c3SLei YU 
279af82a5cSLei YU     /** @brief Open the i2c device */
289af82a5cSLei YU     void open();
299af82a5cSLei YU 
309af82a5cSLei YU     /** @brief Close the i2c device */
319af82a5cSLei YU     void close();
329af82a5cSLei YU 
339af82a5cSLei YU     /** @brief The I2C bus ID */
349af82a5cSLei YU     uint8_t busId;
359af82a5cSLei YU 
369af82a5cSLei YU     /** @brief The i2c device address in the bus */
379af82a5cSLei YU     uint8_t devAddr;
389af82a5cSLei YU 
399af82a5cSLei YU     /** @brief The file descriptor of the opened i2c device */
409af82a5cSLei YU     int fd;
419af82a5cSLei YU 
429af82a5cSLei YU     /** @brief The i2c bus path in /dev */
439af82a5cSLei YU     std::string busStr;
449af82a5cSLei YU 
4592e89eb5SLei YU     /** @brief Check i2c adapter read functionality
4692e89eb5SLei YU      *
4792e89eb5SLei YU      * Check if the i2c adapter has the functionality specified by the SMBus
4892e89eb5SLei YU      * transaction type
4992e89eb5SLei YU      *
5092e89eb5SLei YU      * @param[in] type - The SMBus transaction type defined in linux/i2c.h
5192e89eb5SLei YU      *
5292e89eb5SLei YU      * @throw I2CException if the function is not supported
5392e89eb5SLei YU      */
5492e89eb5SLei YU     void checkReadFuncs(int type);
5592e89eb5SLei YU 
5634fb8bdaSLei YU     /** @brief Check i2c adapter write functionality
5734fb8bdaSLei YU      *
5834fb8bdaSLei YU      * Check if the i2c adapter has the functionality specified by the SMBus
5934fb8bdaSLei YU      * transaction type
6034fb8bdaSLei YU      *
6134fb8bdaSLei YU      * @param[in] type - The SMBus transaction type defined in linux/i2c.h
6234fb8bdaSLei YU      *
6334fb8bdaSLei YU      * @throw I2CException if the function is not supported
6434fb8bdaSLei YU      */
6534fb8bdaSLei YU     void checkWriteFuncs(int type);
6634fb8bdaSLei YU 
67ab1327c3SLei YU   public:
689af82a5cSLei YU     ~I2CDevice()
699af82a5cSLei YU     {
709af82a5cSLei YU         close();
719af82a5cSLei YU     }
72ab1327c3SLei YU 
73ab1327c3SLei YU     /** @copydoc I2CInterface::read(uint8_t&) */
74ab1327c3SLei YU     void read(uint8_t& data) override;
75ab1327c3SLei YU 
76ab1327c3SLei YU     /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */
77ab1327c3SLei YU     void read(uint8_t addr, uint8_t& data) override;
78ab1327c3SLei YU 
79ab1327c3SLei YU     /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */
80ab1327c3SLei YU     void read(uint8_t addr, uint16_t& data) override;
81ab1327c3SLei YU 
82*1d103428SLei YU     /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */
83*1d103428SLei YU     void read(uint8_t addr, uint8_t& size, uint8_t* data,
84*1d103428SLei YU               Mode mode = Mode::SMBUS) override;
85ab1327c3SLei YU 
86ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t) */
87ab1327c3SLei YU     void write(uint8_t data) override;
88ab1327c3SLei YU 
89ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
90ab1327c3SLei YU     void write(uint8_t addr, uint8_t data) override;
91ab1327c3SLei YU 
92ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
93ab1327c3SLei YU     void write(uint8_t addr, uint16_t data) override;
94ab1327c3SLei YU 
95*1d103428SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */
96*1d103428SLei YU     void write(uint8_t addr, uint8_t size, const uint8_t* data,
97*1d103428SLei YU                Mode mode = Mode::SMBUS) override;
98ab1327c3SLei YU 
99ab1327c3SLei YU     /** @brief Create an I2CInterface instance
100ab1327c3SLei YU      *
101ab1327c3SLei YU      * @param[in] busId - The i2c bus ID
102ab1327c3SLei YU      * @param[in] devAddr - The device address of the i2c
103ab1327c3SLei YU      *
104ab1327c3SLei YU      * @return The unique_ptr holding the I2CInterface
105ab1327c3SLei YU      */
106ab1327c3SLei YU     static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
107ab1327c3SLei YU };
108ab1327c3SLei YU 
109ab1327c3SLei YU } // namespace i2c
110