i2c: extract ioctl setup to separate function 30/260430/4
authorAdrian Szyndela <adrian.s@samsung.com>
Thu, 24 Jun 2021 15:09:11 +0000 (17:09 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Fri, 25 Jun 2021 14:55:39 +0000 (16:55 +0200)
Change-Id: Id1af5824d0b25600bb0387b2008dae3ff290dcc5

src/peripheral_i2c.c

index b36ffab731bbe57a1e587a5ec1d45a04ff49cf51..7e975fc4ecd1b2772d1698c848dbf01cb9584b11 100644 (file)
@@ -158,6 +158,21 @@ int peripheral_i2c_close(peripheral_i2c_h i2c)
        return PERIPHERAL_ERROR_NONE;
 }
 
+static int i2c_readwrite(peripheral_i2c_h i2c, uint8_t read_write, uint32_t size, uint8_t command, union i2c_smbus_data *data)
+{
+       struct i2c_smbus_ioctl_data data_arg;
+
+       data_arg.read_write = read_write;
+       data_arg.size = size;
+       data_arg.data = data;
+       data_arg.command = command;
+
+       int ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+       CHECK_ERROR(ret != 0);
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
 int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
 {
        RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
@@ -168,18 +183,10 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length
        if (ret == length)
                return PERIPHERAL_ERROR_NONE;
 
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
+       union i2c_smbus_data data = {.block = {0,}};
 
-       data_arg.read_write = I2C_SMBUS_READ;
-       data_arg.size = I2C_SMBUS_BYTE;
-       data_arg.data = &data;
-       data_arg.command = *data_out;
-
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
+       ret = i2c_readwrite(i2c, I2C_SMBUS_READ, I2C_SMBUS_BYTE, *data_out, &data);
+       CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
        *data_out = data.byte;
 
@@ -196,20 +203,9 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length
        if (ret == length)
                return PERIPHERAL_ERROR_NONE;
 
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
-
-       data_arg.read_write = I2C_SMBUS_WRITE;
-       data_arg.size = I2C_SMBUS_BYTE;
-       data_arg.data = &data;
-       data_arg.command = *data_in;
+       union i2c_smbus_data data = {.block = {0,}};
 
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
+       return i2c_readwrite(i2c, I2C_SMBUS_WRITE, I2C_SMBUS_BYTE, *data_in, &data);
 }
 
 int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
@@ -218,20 +214,10 @@ int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t
        RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
        RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
 
-       int ret;
-
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
+       union i2c_smbus_data data = {.block = {0,}};
 
-       data_arg.read_write = I2C_SMBUS_READ;
-       data_arg.size = I2C_SMBUS_BYTE_DATA;
-       data_arg.data = &data;
-       data_arg.command = reg;
-
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
+       int ret = i2c_readwrite(i2c, I2C_SMBUS_READ, I2C_SMBUS_BYTE_DATA, reg, &data);
+       CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
        *data_out = data.byte;
 
@@ -243,24 +229,10 @@ int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_
        RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
        RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
 
-       int ret;
-
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
-
-       data_arg.read_write = I2C_SMBUS_WRITE;
-       data_arg.size = I2C_SMBUS_BYTE_DATA;
-       data_arg.data = &data;
-       data_arg.command = reg;
-
+       union i2c_smbus_data data = {.block = {0,}};
        data.byte = data_in;
 
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
+       return i2c_readwrite(i2c, I2C_SMBUS_WRITE, I2C_SMBUS_BYTE_DATA, reg, &data);
 }
 
 int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
@@ -269,20 +241,10 @@ int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_
        RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
        RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
 
-       int ret;
-
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
-
-       data_arg.read_write = I2C_SMBUS_READ;
-       data_arg.size = I2C_SMBUS_WORD_DATA;
-       data_arg.data = &data;
-       data_arg.command = reg;
+       union i2c_smbus_data data = {.block = {0,}};
 
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
+       int ret = i2c_readwrite(i2c, I2C_SMBUS_READ, I2C_SMBUS_WORD_DATA, reg, &data);
+       CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
        *data_out = data.word;
 
@@ -294,22 +256,9 @@ int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16
        RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
        RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
 
-       int ret;
-
-       struct i2c_smbus_ioctl_data data_arg;
-       union i2c_smbus_data data;
-
-       memset(&data, 0x0, sizeof(data.block));
-
-       data_arg.read_write = I2C_SMBUS_WRITE;
-       data_arg.size = I2C_SMBUS_WORD_DATA;
-       data_arg.data = &data;
-       data_arg.command = reg;
+       union i2c_smbus_data data = {.block = {0,}};
 
        data.word = data_in;
 
-       ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
-       CHECK_ERROR(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
+       return i2c_readwrite(i2c, I2C_SMBUS_WRITE, I2C_SMBUS_WORD_DATA, reg, &data);
 }