From f60468fcf4509ed4452dd2cd930e501e14ee5f77 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 1 Jun 2017 11:32:33 +0900 Subject: [PATCH] Add new APIs for I2C smbus transaction This patch adds below I2C APIs. - peripheral_i2c_read_register_byte() - peripheral_i2c_write_register_byte() - peripheral_i2c_read_register_word() - peripheral_i2c_write_register_word() igned-off-by: Hyeongsik Min Change-Id: Ic0e96b4225b6596556b670487c905e6c77c4039c --- include/peripheral_gdbus_i2c.h | 1 + include/peripheral_io.h | 73 +++++++++++++++++++++++++++-- src/peripheral_gdbus_i2c.c | 26 +++++++++++ src/peripheral_i2c.c | 84 +++++++++++++++++++++++++++++++--- src/peripheral_io.xml | 9 ++++ 5 files changed, 182 insertions(+), 11 deletions(-) diff --git a/include/peripheral_gdbus_i2c.h b/include/peripheral_gdbus_i2c.h index c36cb04..7b2b899 100644 --- a/include/peripheral_gdbus_i2c.h +++ b/include/peripheral_gdbus_i2c.h @@ -24,5 +24,6 @@ int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c); int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); +int peripheral_gdbus_i2c_smbus_ioctl(peripheral_i2c_h i2c, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out); #endif /* __PERIPHERAL_GDBUS_I2C_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 8e126c8..5629ae3 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -328,7 +328,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c); int peripheral_i2c_close(peripheral_i2c_h i2c); /** - * @brief Reads data from the i2c device. + * @brief Reads data from the i2c slave device. * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device @@ -344,7 +344,7 @@ int peripheral_i2c_close(peripheral_i2c_h i2c); int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); /** - * @brief Write data to the i2c device. + * @brief Write data to the i2c slave device. * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device @@ -360,7 +360,7 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); /** - * @brief Reads single byte data from the i2c device. + * @brief Reads single byte data from the i2c slave device. * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device @@ -373,8 +373,9 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error */ int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data); + /** - * @brief Write single byte data to the i2c device. + * @brief Write single byte data to the i2c slave device. * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device @@ -388,6 +389,70 @@ int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data); */ int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data); +/** + * @brief Reads byte data from the register of i2c slave device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in] address The register address of the i2c slave device to read + * @param[out] data The byte output of slave device(register value) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t *data); + +/** + * @brief Write byte data to the register of i2c slave device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in] address The register address of the i2c slave device to write + * @param[in] data The byte value to write + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t data); + +/** + * @brief Reads word data from the register of i2c slave device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in] address The register address of the i2c slave device to read + * @param[out] data The word output(2 bytes) of slave device(register value) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t *data); + +/** + * @brief Write byte data to the register of i2c slave device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in] address The register address of the i2c slave device to write + * @param[in] data The word(2 bytes) value to write + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t data); + /** * @} */ diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index 1b296f1..875df8c 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -165,3 +165,29 @@ int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) return ret; } + +int peripheral_gdbus_i2c_smbus_ioctl(peripheral_i2c_h i2c, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_smbus_ioctl_sync( + i2c_proxy, + i2c->handle, + read_write, + command, + size, + data_in, + data_out, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 969e18b..0af6dc8 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -24,6 +24,16 @@ #include "peripheral_common.h" #include "peripheral_internal.h" +/* i2c_smbus_xfer read or write markers */ +#define I2C_SMBUS_READ 1 +#define I2C_SMBUS_WRITE 0 + +/* SMBus transaction types */ +#define I2C_SMBUS_QUICK 0 +#define I2C_SMBUS_BYTE 1 +#define I2C_SMBUS_BYTE_DATA 2 +#define I2C_SMBUS_WORD_DATA 3 + int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) { peripheral_i2c_h handle; @@ -75,11 +85,7 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_gdbus_i2c_read(i2c, data, length); - /* - _D("I2C read data : "); - for (int i = 0 ; i < length ; i++) - _D("[%02x]", data[i]); - */ + return ret; } @@ -93,17 +99,81 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data) { int ret = PERIPHERAL_ERROR_NONE; + uint16_t w_data, dummy = 0; if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_gdbus_i2c_read(i2c, data, 0x1); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, 0x0, I2C_SMBUS_BYTE, dummy, &w_data); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to read, ret : %d", ret); + + *data = (uint8_t)w_data; return ret; } int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data) { + int ret = PERIPHERAL_ERROR_NONE; + uint16_t dummy = 0; + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - return peripheral_gdbus_i2c_write(i2c, &data, 0x1); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, dummy, I2C_SMBUS_BYTE, (uint16_t)data, &dummy); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to read, ret : %d", ret); + + return ret; +} + +int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t *data) +{ + int ret; + uint16_t w_data, dummy = 0; + + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, dummy, &w_data); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Smbus transaction failed, ret : %d", ret); + + *data = (uint8_t)w_data; + + return ret; +} + +int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t data) +{ + int ret; + uint16_t dummy = 0; + + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, (uint16_t)data, &dummy); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Smbus transaction failed, ret : %d", ret); + + return ret; +} + +int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t *data) +{ + int ret; + uint16_t dummy = 0, data_out; + + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_WORD_DATA, dummy, &data_out); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Smbus transaction failed, ret : %d", ret); + + *data = data_out; + + return ret; +} + +int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t data) +{ + int ret; + uint16_t dummy = 0; + + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_WORD_DATA, data, &dummy); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Smbus transaction failed, ret : %d", ret); + + return ret; } diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index c6fff55..ea4b1f9 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -80,6 +80,15 @@ + + + + + + + + + -- 2.34.1