return ret;
}
+int peripheral_bus_i2c_smbus_ioctl(pb_data_h handle, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out)
+{
+ peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
+ struct i2c_smbus_ioctl_data data_arg;
+ union i2c_smbus_data data;
+ int ret;
+
+ memset(&data, 0x0, sizeof(data.block));
+
+ data_arg.read_write = read_write;
+ data_arg.size = size;
+ data_arg.data = &data;
+
+ RETV_IF(size < I2C_SMBUS_BYTE || size > I2C_SMBUS_WORD_DATA, PERIPHERAL_ERROR_INVALID_PARAMETER);
+ RETV_IF(read_write > I2C_SMBUS_READ, PERIPHERAL_ERROR_INVALID_PARAMETER);
+
+ if (read_write == I2C_SMBUS_WRITE) {
+ data_arg.command = command;
+ if (size == I2C_SMBUS_BYTE_DATA)
+ data.byte = (uint8_t)data_in;
+ else if (size == I2C_SMBUS_WORD_DATA)
+ data.word = data_in;
+ else if (size == I2C_SMBUS_BYTE)
+ data_arg.command = (uint8_t)data_in; // Data should be set to command.
+ } else if (read_write == I2C_SMBUS_READ && size != I2C_SMBUS_BYTE) {
+ data_arg.command = command;
+ }
+
+ ret = i2c_smbus_ioctl(i2c->fd, &data_arg);
+ if (ret < 0)
+ _E("Ioctl failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
+
+ if (ret == 0 && read_write == I2C_SMBUS_READ) {
+ if (size == I2C_SMBUS_BYTE_DATA || size == I2C_SMBUS_BYTE)
+ *data_out = (uint8_t)data.byte;
+ else if (size == I2C_SMBUS_WORD_DATA)
+ *data_out = data.word;
+ }
+
+ return ret;
+}
+
int peripheral_bus_i2c_read(pb_data_h handle, int length, GVariant **data_array)
{
peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
}
ret = i2c_read(i2c->fd, i2c->buffer, length);
- if (ret < 0)
- _E("Read Failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
- *data_array = peripheral_bus_build_variant_ay(i2c->buffer, length);
+ /* If read() is failed, try to call ioctl with SMBus */
+ if (ret < 0) {
+ _D("Try to call ioctl() instead of read(), bus : %d, address : 0x%x",
+ i2c->bus, i2c->address);
+ ret = peripheral_bus_i2c_smbus_ioctl(handle, I2C_SMBUS_READ,
+ 0x0, I2C_SMBUS_BYTE, 0x0, (uint16_t *)&i2c->buffer[0]);
+ if (ret < 0)
+ _E("Read Failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
+ }
+
+ *data_array = peripheral_bus_build_variant_ay(i2c->buffer, I2C_SMBUS_BYTE);
return ret;
g_variant_iter_free(iter);
ret = i2c_write(i2c->fd, i2c->buffer, length);
- if (ret < 0)
- _E("Write Failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
-
- return ret;
-}
-
-int peripheral_bus_i2c_smbus_ioctl(pb_data_h handle, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out)
-{
- peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
- struct i2c_smbus_ioctl_data data_arg;
- union i2c_smbus_data data;
- int ret;
-
- memset(&data, 0x0, sizeof(data.block));
- data_arg.read_write = read_write;
- data_arg.size = size;
- data_arg.data = &data;
-
- RETV_IF(size < I2C_SMBUS_BYTE || size > I2C_SMBUS_WORD_DATA, PERIPHERAL_ERROR_INVALID_PARAMETER);
- RETV_IF(read_write > I2C_SMBUS_READ, PERIPHERAL_ERROR_INVALID_PARAMETER);
-
- if (read_write == I2C_SMBUS_WRITE) {
- data_arg.command = command;
- if (size == I2C_SMBUS_BYTE_DATA)
- data.byte = (uint8_t)data_in;
- else if (size == I2C_SMBUS_WORD_DATA)
- data.word = data_in;
- else if (size == I2C_SMBUS_BYTE)
- data_arg.command = (uint8_t)data_in; // Data should be set to command.
- } else if (read_write == I2C_SMBUS_READ && size != I2C_SMBUS_BYTE) {
- data_arg.command = command;
- }
-
- ret = i2c_smbus_ioctl(i2c->fd, &data_arg);
- if (ret < 0)
- _E("Ioctl failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
-
- if (ret == 0 && read_write == I2C_SMBUS_READ) {
- if (size == I2C_SMBUS_BYTE_DATA || size == I2C_SMBUS_BYTE)
- *data_out = (uint8_t)data.byte;
- else if (size == I2C_SMBUS_WORD_DATA)
- *data_out = data.word;
+ /* If write() is failed, try to call ioctl with SMBus */
+ if (ret < 0) {
+ _D("Try to call ioctl() instead of write(), bus : %d, address : 0x%x",
+ i2c->bus, i2c->address);
+ ret = peripheral_bus_i2c_smbus_ioctl(handle, I2C_SMBUS_WRITE,
+ 0x0, I2C_SMBUS_BYTE, i2c->buffer[0], NULL);
+ if (ret < 0)
+ _E("Write Failed, bus : %d, address : 0x%x", i2c->bus, i2c->address);
}
return ret;
}
+