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.
17 #include <sys/ioctl.h>
19 #include "peripheral_interface_i2c.h"
21 void peripheral_interface_i2c_close(peripheral_i2c_h i2c)
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)
31 struct i2c_smbus_ioctl_data data_arg;
32 union i2c_smbus_data data;
34 memset(&data, 0x0, sizeof(data.block));
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;
41 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
42 CHECK_ERROR(ret != 0);
44 *data_out = data.byte;
46 return PERIPHERAL_ERROR_NONE;
49 int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length)
51 int ret = read(i2c->fd, data_out, length);
53 return peripheral_interface_i2c_read_buffer(i2c, data_out, length);
55 return PERIPHERAL_ERROR_NONE;
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)
63 struct i2c_smbus_ioctl_data data_arg;
64 union i2c_smbus_data data;
66 memset(&data, 0x0, sizeof(data.block));
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;
73 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
74 CHECK_ERROR(ret != 0);
76 return PERIPHERAL_ERROR_NONE;
79 int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length)
81 int ret = write(i2c->fd, data_in, length);
83 return peripheral_interface_i2c_write_buffer(i2c, data_in, length);
85 return PERIPHERAL_ERROR_NONE;
88 int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
92 struct i2c_smbus_ioctl_data data_arg;
93 union i2c_smbus_data data;
95 memset(&data, 0x0, sizeof(data.block));
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;
102 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
103 CHECK_ERROR(ret != 0);
105 *data_out = data.byte;
107 return PERIPHERAL_ERROR_NONE;
110 int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in)
114 struct i2c_smbus_ioctl_data data_arg;
115 union i2c_smbus_data data;
117 memset(&data, 0x0, sizeof(data.block));
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;
126 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
127 CHECK_ERROR(ret != 0);
129 return PERIPHERAL_ERROR_NONE;
132 int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
136 struct i2c_smbus_ioctl_data data_arg;
137 union i2c_smbus_data data;
139 memset(&data, 0x0, sizeof(data.block));
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;
146 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
147 CHECK_ERROR(ret != 0);
149 *data_out = data.word;
151 return PERIPHERAL_ERROR_NONE;
154 int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in)
158 struct i2c_smbus_ioctl_data data_arg;
159 union i2c_smbus_data data;
161 memset(&data, 0x0, sizeof(data.block));
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;
170 ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
171 CHECK_ERROR(ret != 0);
173 return PERIPHERAL_ERROR_NONE;