i2c: replace gdbus with direct implementation
[platform/core/api/peripheral-io.git] / src / interface / peripheral_interface_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 <sys/ioctl.h>
18
19 #include "peripheral_interface_i2c.h"
20
21 void peripheral_interface_i2c_close(peripheral_i2c_h i2c)
22 {
23         close(i2c->fd);
24 }
25
26 /* It was developed temporarily because of the I2C Stub. */
27 static int peripheral_interface_i2c_read_buffer(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
28 {
29         int ret;
30
31         struct i2c_smbus_ioctl_data data_arg;
32         union i2c_smbus_data data;
33
34         memset(&data, 0x0, sizeof(data.block));
35
36         data_arg.read_write = I2C_SMBUS_READ;
37         data_arg.size = I2C_SMBUS_BYTE;
38         data_arg.data = &data;
39         data_arg.command = *data_out;
40
41         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
42         CHECK_ERROR(ret != 0);
43
44         *data_out = data.byte;
45
46         return PERIPHERAL_ERROR_NONE;
47 }
48
49 int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
50 {
51         int ret = read(i2c->fd, data_out, length);
52         if (ret != length)
53                 return peripheral_interface_i2c_read_buffer(i2c, data_out, length);
54
55         return PERIPHERAL_ERROR_NONE;
56 }
57
58 /* It was developed temporarily because of the I2C Stub. */
59 static int peripheral_interface_i2c_write_buffer(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
60 {
61         int ret;
62
63         struct i2c_smbus_ioctl_data data_arg;
64         union i2c_smbus_data data;
65
66         memset(&data, 0x0, sizeof(data.block));
67
68         data_arg.read_write = I2C_SMBUS_WRITE;
69         data_arg.size = I2C_SMBUS_BYTE;
70         data_arg.data = &data;
71         data_arg.command = *data_in;
72
73         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
74         CHECK_ERROR(ret != 0);
75
76         return PERIPHERAL_ERROR_NONE;
77 }
78
79 int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
80 {
81         int ret = write(i2c->fd, data_in, length);
82         if (ret != length)
83                 return peripheral_interface_i2c_write_buffer(i2c, data_in, length);
84
85         return PERIPHERAL_ERROR_NONE;
86 }
87
88 int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
89 {
90         int ret;
91
92         struct i2c_smbus_ioctl_data data_arg;
93         union i2c_smbus_data data;
94
95         memset(&data, 0x0, sizeof(data.block));
96
97         data_arg.read_write = I2C_SMBUS_READ;
98         data_arg.size = I2C_SMBUS_BYTE_DATA;
99         data_arg.data = &data;
100         data_arg.command = reg;
101
102         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
103         CHECK_ERROR(ret != 0);
104
105         *data_out = data.byte;
106
107         return PERIPHERAL_ERROR_NONE;
108 }
109
110 int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in)
111 {
112         int ret;
113
114         struct i2c_smbus_ioctl_data data_arg;
115         union i2c_smbus_data data;
116
117         memset(&data, 0x0, sizeof(data.block));
118
119         data_arg.read_write = I2C_SMBUS_WRITE;
120         data_arg.size = I2C_SMBUS_BYTE_DATA;
121         data_arg.data = &data;
122         data_arg.command = reg;
123
124         data.byte = data_in;
125
126         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
127         CHECK_ERROR(ret != 0);
128
129         return PERIPHERAL_ERROR_NONE;
130 }
131
132 int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
133 {
134         int ret;
135
136         struct i2c_smbus_ioctl_data data_arg;
137         union i2c_smbus_data data;
138
139         memset(&data, 0x0, sizeof(data.block));
140
141         data_arg.read_write = I2C_SMBUS_READ;
142         data_arg.size = I2C_SMBUS_WORD_DATA;
143         data_arg.data = &data;
144         data_arg.command = reg;
145
146         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
147         CHECK_ERROR(ret != 0);
148
149         *data_out = data.word;
150
151         return PERIPHERAL_ERROR_NONE;
152 }
153
154 int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in)
155 {
156         int ret;
157
158         struct i2c_smbus_ioctl_data data_arg;
159         union i2c_smbus_data data;
160
161         memset(&data, 0x0, sizeof(data.block));
162
163         data_arg.read_write = I2C_SMBUS_WRITE;
164         data_arg.size = I2C_SMBUS_WORD_DATA;
165         data_arg.data = &data;
166         data_arg.command = reg;
167
168         data.word = data_in;
169
170         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
171         CHECK_ERROR(ret != 0);
172
173         return PERIPHERAL_ERROR_NONE;
174 }