+++ /dev/null
-/*
- * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __PERIPHERAL_INTERFACE_I2C_H__
-#define __PERIPHERAL_INTERFACE_I2C_H__
-
-#include "peripheral_interface_common.h"
-
-#define I2C_BUFFER_MAX 64
-
-#define I2C_SLAVE 0x0703 /* Use this slave address */
-#define I2C_SMBUS 0x0720 /* SMBus transfer */
-
-/* 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
-
-/*
- * Data for SMBus Messages
- */
-#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
-
-union i2c_smbus_data {
- uint8_t byte;
- uint16_t word;
- uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
- /* and one more for user-space compatibility */
-};
-
-/* This is the structure as used in the I2C_SMBUS ioctl call */
-struct i2c_smbus_ioctl_data {
- uint8_t read_write;
- uint8_t command;
- uint32_t size;
- union i2c_smbus_data *data;
-};
-
-void peripheral_interface_i2c_close(peripheral_i2c_h i2c);
-int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length);
-int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length);
-int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out);
-int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in);
-int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out);
-int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in);
-
-#endif /* __PERIPHERAL_INTERFACE_I2C_H__ */
+++ /dev/null
-/*
- * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <sys/ioctl.h>
-
-#include "peripheral_interface_i2c.h"
-
-void peripheral_interface_i2c_close(peripheral_i2c_h i2c)
-{
- close(i2c->fd);
-}
-
-/* It was developed temporarily because of the I2C Stub. */
-static int peripheral_interface_i2c_read_buffer(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
-{
- 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_BYTE;
- data_arg.data = &data;
- data_arg.command = *data_out;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- *data_out = data.byte;
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
-{
- int ret = read(i2c->fd, data_out, length);
- if (ret != length)
- return peripheral_interface_i2c_read_buffer(i2c, data_out, length);
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-/* It was developed temporarily because of the I2C Stub. */
-static int peripheral_interface_i2c_write_buffer(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
-{
- 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_arg.data = &data;
- data_arg.command = *data_in;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
-{
- int ret = write(i2c->fd, data_in, length);
- if (ret != length)
- return peripheral_interface_i2c_write_buffer(i2c, data_in, length);
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
-{
- 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_BYTE_DATA;
- data_arg.data = &data;
- data_arg.command = reg;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- *data_out = data.byte;
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in)
-{
- 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;
-
- data.byte = data_in;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
-{
- 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;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- *data_out = data.word;
-
- return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in)
-{
- 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;
-
- data.word = data_in;
-
- ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
- CHECK_ERROR(ret != 0);
-
- return PERIPHERAL_ERROR_NONE;
-}
\ No newline at end of file
#include <system_info.h>
#include "peripheral_io.h"
-#include "peripheral_handle.h"
-#include "peripheral_interface_i2c.h"
+#include "peripheral_interface_common.h"
#include "peripheral_log.h"
#define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c"
#define I2C_FEATURE_FALSE 0
#define I2C_FEATURE_TRUE 1
+#define I2C_SLAVE 0x0703 /* Use this slave address */
+#define I2C_SMBUS 0x0720 /* SMBus transfer */
+
/* i2c_smbus_xfer read or write markers */
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_WORD_DATA 3
+/*
+ * Data for SMBus Messages
+ */
+#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
+
+union i2c_smbus_data {
+ uint8_t byte;
+ uint16_t word;
+ uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
+ /* and one more for user-space compatibility */
+};
+
+/* This is the structure as used in the I2C_SMBUS ioctl call */
+struct i2c_smbus_ioctl_data {
+ uint8_t read_write;
+ uint8_t command;
+ uint32_t size;
+ union i2c_smbus_data *data;
+};
+
static int i2c_feature = I2C_FEATURE_UNKNOWN;
static bool __is_feature_supported(void)
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");
- peripheral_interface_i2c_close(i2c);
+ close(i2c->fd);
free(i2c);
i2c = NULL;
return ret;
}
+static int peripheral_i2c_read_buffer(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
+{
+ 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_BYTE;
+ data_arg.data = &data;
+ data_arg.command = *data_out;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ *data_out = data.byte;
+
+ return PERIPHERAL_ERROR_NONE;
+}
+
int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
{
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");
RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
- return peripheral_interface_i2c_read(i2c, data, length);
+ int ret = read(i2c->fd, data, length);
+ if (ret != length)
+ return peripheral_i2c_read_buffer(i2c, data, length);
+
+ return PERIPHERAL_ERROR_NONE;
+}
+
+static int peripheral_i2c_write_buffer(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
+{
+ 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_arg.data = &data;
+ data_arg.command = *data_in;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ return PERIPHERAL_ERROR_NONE;
}
int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
- return peripheral_interface_i2c_write(i2c, data, length);
+ int ret = write(i2c->fd, data, length);
+ if (ret != length)
+ return peripheral_i2c_write_buffer(i2c, data, length);
+
+ return PERIPHERAL_ERROR_NONE;
}
-int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data)
+int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
{
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");
- RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
+ RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
+
+ int ret;
- return peripheral_interface_i2c_read_register_byte(i2c, reg, data);
+ 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_BYTE_DATA;
+ data_arg.data = &data;
+ data_arg.command = reg;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ *data_out = data.byte;
+
+ return PERIPHERAL_ERROR_NONE;
}
-int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data)
+int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in)
{
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");
- return peripheral_interface_i2c_write_register_byte(i2c, reg, data);
+ 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;
+
+ data.byte = data_in;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ return PERIPHERAL_ERROR_NONE;
}
-int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data)
+int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
{
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");
- RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
+ RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
- return peripheral_interface_i2c_read_register_word(i2c, reg, data);
+ 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;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ *data_out = data.word;
+
+ return PERIPHERAL_ERROR_NONE;
}
-int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data)
+int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in)
{
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");
- return peripheral_interface_i2c_write_register_word(i2c, reg, data);
+ 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;
+
+ data.word = data_in;
+
+ ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
+ CHECK_ERROR(ret != 0);
+
+ return PERIPHERAL_ERROR_NONE;
}