2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <system_info.h>
20 #include "peripheral_io.h"
21 #include "peripheral_handle.h"
22 #include "peripheral_gdbus_i2c.h"
23 #include "peripheral_interface_i2c.h"
24 #include "peripheral_log.h"
26 #define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c"
28 #define I2C_FEATURE_UNKNOWN -1
29 #define I2C_FEATURE_FALSE 0
30 #define I2C_FEATURE_TRUE 1
32 /* i2c_smbus_xfer read or write markers */
33 #define I2C_SMBUS_READ 1
34 #define I2C_SMBUS_WRITE 0
36 /* SMBus transaction types */
37 #define I2C_SMBUS_QUICK 0
38 #define I2C_SMBUS_BYTE 1
39 #define I2C_SMBUS_BYTE_DATA 2
40 #define I2C_SMBUS_WORD_DATA 3
42 static int i2c_feature = I2C_FEATURE_UNKNOWN;
44 static bool __is_feature_supported(void)
46 int ret = SYSTEM_INFO_ERROR_NONE;
49 if (i2c_feature == I2C_FEATURE_UNKNOWN) {
50 ret = system_info_get_platform_bool(PERIPHERAL_IO_I2C_FEATURE, &feature);
51 RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
53 i2c_feature = (feature ? I2C_FEATURE_TRUE : I2C_FEATURE_FALSE);
56 return (i2c_feature == I2C_FEATURE_TRUE ? true : false);
59 int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c)
61 peripheral_i2c_h handle;
62 int ret = PERIPHERAL_ERROR_NONE;
64 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
65 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle");
66 RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
68 handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
70 _E("Failed to allocate peripheral_i2c_h");
71 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
74 ret = peripheral_gdbus_i2c_open(handle, bus, address);
75 if (ret != PERIPHERAL_ERROR_NONE) {
76 _E("Failed to open i2c communication, ret : %d", ret);
86 int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c)
88 peripheral_i2c_h handle;
89 int ret = PERIPHERAL_ERROR_NONE;
91 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
92 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle");
93 RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
95 handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
97 _E("Failed to allocate peripheral_i2c_h");
98 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
101 ret = peripheral_gdbus_i2c_open_flags(handle, bus, address, flags);
102 if (ret != PERIPHERAL_ERROR_NONE) {
103 _E("Failed to open i2c communication, ret : %d", ret);
113 int peripheral_i2c_close(peripheral_i2c_h i2c)
115 int ret = PERIPHERAL_ERROR_NONE;
117 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
118 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
120 ret = peripheral_gdbus_i2c_close(i2c);
121 if (ret != PERIPHERAL_ERROR_NONE)
122 _E("Failed to close i2c communcation, ret : %d", ret);
124 peripheral_interface_i2c_close(i2c);
132 int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
134 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
135 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
136 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
138 return peripheral_interface_i2c_read(i2c, data, length);
141 int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
143 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
144 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
145 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
147 return peripheral_interface_i2c_write(i2c, data, length);
150 int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data)
152 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
153 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
154 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
156 return peripheral_interface_i2c_read_register_byte(i2c, reg, data);
159 int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data)
161 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
162 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
164 return peripheral_interface_i2c_write_register_byte(i2c, reg, data);
167 int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data)
169 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
170 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
171 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
173 return peripheral_interface_i2c_read_register_word(i2c, reg, data);
176 int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data)
178 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
179 RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL");
181 return peripheral_interface_i2c_write_register_word(i2c, reg, data);