Add new APIs for I2C smbus transaction 64/132064/3
authorHyeongsik Min <hyeongsik.min@samsung.com>
Thu, 1 Jun 2017 02:32:33 +0000 (11:32 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Thu, 1 Jun 2017 06:11:54 +0000 (15:11 +0900)
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 <hyeongsik.min@samsung.com>

Change-Id: Ic0e96b4225b6596556b670487c905e6c77c4039c

include/peripheral_gdbus_i2c.h
include/peripheral_io.h
src/peripheral_gdbus_i2c.c
src/peripheral_i2c.c
src/peripheral_io.xml

index c36cb044f5765c0fa2859b6aa6b7b4274011fb09..7b2b89992961d89d662c8ae53ffbb90873860513 100644 (file)
@@ -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__ */
index 8e126c8366b5cc00ec06819c5ad9b8c04d5474cc..5629ae3780852afbd1ce39ea59dfca4b9ba7a5d3 100644 (file)
@@ -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);
+
 /**
 * @}
 */
index 1b296f1453fe4e1ba69ba7831d3f154de6e3adb6..875df8c4222a270d66794b1177adf2ff946a5f54 100644 (file)
@@ -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;
+}
index 969e18b8dbc6e149199ffab42718e8cb546695f0..0af6dc883ee8acb84c9738fae08de402b205b76b 100644 (file)
 #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;
 }
index c6fff55136d42a7cf106be28311facda96ec6068..ea4b1f93b9b1a48e6a12b22b85756e9c098b5a32 100644 (file)
                        </arg>
                        <arg type="i" name="result" direction="out"/>
                </method>
+               <method name="SmbusIoctl">
+                       <arg type="u" name="handle" direction="in"/>
+                       <arg type="y" name="read_write" direction="in"/>
+                       <arg type="y" name="command" direction="in"/>
+                       <arg type="u" name="size" direction="in"/>
+                       <arg type="q" name="data_in" direction="in"/>
+                       <arg type="q" name="data_out" direction="out"/>
+                       <arg type="i" name="result" direction="out"/>
+               </method>
        </interface>
        <interface name="org.tizen.peripheral_io.pwm">
                <method name="Open">