Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 1 | #include "i2c.hpp" |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | |
| 5 | extern "C" |
| 6 | { |
| 7 | #include <i2c/smbus.h> |
| 8 | #include <linux/i2c-dev.h> |
| 9 | #include <linux/i2c.h> |
| 10 | } |
| 11 | |
| 12 | namespace phosphor::i2c |
| 13 | { |
| 14 | |
| 15 | int I2C::open() |
| 16 | { |
| 17 | int ret = 0; |
| 18 | fd = ::open(busStr.c_str(), O_RDWR); |
| 19 | if (fd < 0) |
| 20 | { |
| 21 | return fd; |
| 22 | } |
| 23 | |
| 24 | ret = ioctl(fd, I2C_SLAVE_FORCE, deviceNode); |
| 25 | if (ret < 0) |
| 26 | { |
| 27 | close(); |
| 28 | return ret; |
| 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | // NOLINTBEGIN(readability-static-accessed-through-instance) |
| 35 | sdbusplus::async::task<bool> I2C::sendReceive( |
| 36 | uint8_t* writeData, uint8_t writeSize, uint8_t* readData, |
| 37 | uint8_t readSize) const |
| 38 | // NOLINTEND(readability-static-accessed-through-instance) |
| 39 | { |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 40 | bool result = true; |
| 41 | |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 42 | if (fd <= 0) |
| 43 | { |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 44 | result = false; |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 45 | } |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 46 | else |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 47 | { |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 48 | struct i2c_msg msg[2]; |
| 49 | struct i2c_rdwr_ioctl_data readWriteData; |
| 50 | int msgIndex = 0; |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 51 | |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 52 | if (writeSize) |
| 53 | { |
| 54 | msg[msgIndex].addr = deviceNode; |
| 55 | msg[msgIndex].flags = 0; |
| 56 | msg[msgIndex].len = writeSize; |
| 57 | msg[msgIndex].buf = writeData; |
| 58 | msgIndex++; |
| 59 | } |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 60 | |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 61 | if (readSize) |
| 62 | { |
| 63 | msg[msgIndex].addr = deviceNode; |
| 64 | msg[msgIndex].flags = I2C_M_RD; |
| 65 | msg[msgIndex].len = readSize; |
| 66 | msg[msgIndex].buf = readData; |
| 67 | msgIndex++; |
| 68 | } |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 69 | |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 70 | readWriteData.msgs = msg; |
| 71 | readWriteData.nmsgs = msgIndex; |
| 72 | |
| 73 | if (ioctl(fd, I2C_RDWR, &readWriteData) < 0) |
| 74 | { |
| 75 | result = false; |
| 76 | } |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 77 | } |
Daniel Hsu | f6470b5 | 2025-02-26 15:03:47 +0800 | [diff] [blame^] | 78 | co_return result; |
Christopher Meis | 7e446a4 | 2024-10-22 09:36:41 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int I2C::close() const |
| 82 | { |
| 83 | return ::close(fd); |
| 84 | } |
| 85 | |
| 86 | } // namespace phosphor::i2c |