xref: /openbmc/phosphor-power/tools/i2c/i2c.hpp (revision 34fb8bda)
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 
56*34fb8bdaSLei YU     /** @brief Check i2c adapter write functionality
57*34fb8bdaSLei YU      *
58*34fb8bdaSLei YU      * Check if the i2c adapter has the functionality specified by the SMBus
59*34fb8bdaSLei YU      * transaction type
60*34fb8bdaSLei YU      *
61*34fb8bdaSLei YU      * @param[in] type - The SMBus transaction type defined in linux/i2c.h
62*34fb8bdaSLei YU      *
63*34fb8bdaSLei YU      * @throw I2CException if the function is not supported
64*34fb8bdaSLei YU      */
65*34fb8bdaSLei YU     void checkWriteFuncs(int type);
66*34fb8bdaSLei 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 
82ab1327c3SLei YU     /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*) */
83ab1327c3SLei YU     void read(uint8_t addr, uint8_t& size, uint8_t* data) override;
84ab1327c3SLei YU 
85ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t) */
86ab1327c3SLei YU     void write(uint8_t data) override;
87ab1327c3SLei YU 
88ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
89ab1327c3SLei YU     void write(uint8_t addr, uint8_t data) override;
90ab1327c3SLei YU 
91ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
92ab1327c3SLei YU     void write(uint8_t addr, uint16_t data) override;
93ab1327c3SLei YU 
94ab1327c3SLei YU     /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*) */
95ab1327c3SLei YU     void write(uint8_t addr, uint8_t size, const uint8_t* data) override;
96ab1327c3SLei YU 
97ab1327c3SLei YU     /** @brief Create an I2CInterface instance
98ab1327c3SLei YU      *
99ab1327c3SLei YU      * @param[in] busId - The i2c bus ID
100ab1327c3SLei YU      * @param[in] devAddr - The device address of the i2c
101ab1327c3SLei YU      *
102ab1327c3SLei YU      * @return The unique_ptr holding the I2CInterface
103ab1327c3SLei YU      */
104ab1327c3SLei YU     static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
105ab1327c3SLei YU };
106ab1327c3SLei YU 
107ab1327c3SLei YU } // namespace i2c
108