5f493f2087d103ffe3e532b0820d58a179cf1fcb
[platform/core/api/peripheral-io.git] / src / peripheral_i2c.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <system_info.h>
19
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"
25
26 #define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c"
27
28 #define I2C_FEATURE_UNKNOWN -1
29 #define I2C_FEATURE_FALSE    0
30 #define I2C_FEATURE_TRUE     1
31
32 /* i2c_smbus_xfer read or write markers */
33 #define I2C_SMBUS_READ  1
34 #define I2C_SMBUS_WRITE 0
35
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
41
42 static int i2c_feature = I2C_FEATURE_UNKNOWN;
43
44 static bool __is_feature_supported(void)
45 {
46         int ret = SYSTEM_INFO_ERROR_NONE;
47         bool feature = false;
48
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");
52
53                 i2c_feature = (feature ? I2C_FEATURE_TRUE : I2C_FEATURE_FALSE);
54         }
55
56         return (i2c_feature == I2C_FEATURE_TRUE ? true : false);
57 }
58
59 int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c)
60 {
61         peripheral_i2c_h handle;
62         int ret = PERIPHERAL_ERROR_NONE;
63
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");
67
68         handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
69         if (handle == NULL) {
70                 _E("Failed to allocate peripheral_i2c_h");
71                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
72         }
73
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);
77                 free(handle);
78                 handle = NULL;
79         }
80
81         *i2c = handle;
82
83         return ret;
84 }
85
86 int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c)
87 {
88         peripheral_i2c_h handle;
89         int ret = PERIPHERAL_ERROR_NONE;
90
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");
94
95         handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
96         if (handle == NULL) {
97                 _E("Failed to allocate peripheral_i2c_h");
98                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
99         }
100
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);
104                 free(handle);
105                 handle = NULL;
106         }
107
108         *i2c = handle;
109
110         return ret;
111 }
112
113 int peripheral_i2c_close(peripheral_i2c_h i2c)
114 {
115         int ret = PERIPHERAL_ERROR_NONE;
116
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");
119
120         ret = peripheral_gdbus_i2c_close(i2c);
121         if (ret != PERIPHERAL_ERROR_NONE)
122                 _E("Failed to close i2c communcation, ret : %d", ret);
123
124         peripheral_interface_i2c_close(i2c);
125
126         free(i2c);
127         i2c = NULL;
128
129         return ret;
130 }
131
132 int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
133 {
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");
137
138         return peripheral_interface_i2c_read(i2c, data, length);
139 }
140
141 int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
142 {
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");
146
147         return peripheral_interface_i2c_write(i2c, data, length);
148 }
149
150 int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data)
151 {
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");
155
156         return peripheral_interface_i2c_read_register_byte(i2c, reg, data);
157 }
158
159 int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data)
160 {
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");
163
164         return peripheral_interface_i2c_write_register_byte(i2c, reg, data);
165 }
166
167 int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data)
168 {
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");
172
173         return peripheral_interface_i2c_read_register_word(i2c, reg, data);
174 }
175
176 int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data)
177 {
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");
180
181         return peripheral_interface_i2c_write_register_word(i2c, reg, data);
182 }