131bbf9de63e89963db9d7feac259dcdd8cde9eb
[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 #define MAX_ERR_LEN 255
22
23 int peripheral_interface_i2c_close(peripheral_i2c_h i2c)
24 {
25         int ret = close(i2c->fd);
26         CHECK_ERROR(ret != 0);
27
28         return 0;
29 }
30
31 int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
32 {
33         int ret = read(i2c->fd, data, length);
34         CHECK_ERROR(ret != length);
35
36         return 0;
37 }
38
39 int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length)
40 {
41         int ret = write(i2c->fd, data, length);
42         CHECK_ERROR(ret != length);
43
44         return 0;
45 }
46
47 int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out)
48 {
49         int ret;
50
51         struct i2c_smbus_ioctl_data data_arg;
52         union i2c_smbus_data data;
53
54         memset(&data, 0x0, sizeof(data.block));
55
56         data_arg.read_write = I2C_SMBUS_READ;
57         data_arg.size = I2C_SMBUS_BYTE_DATA;
58         data_arg.data = &data;
59         data_arg.command = reg;
60
61         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
62         CHECK_ERROR(ret != 0);
63
64         *data_out = data.byte;
65
66         return 0;
67 }
68
69 int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in)
70 {
71         int ret;
72
73         struct i2c_smbus_ioctl_data data_arg;
74         union i2c_smbus_data data;
75
76         memset(&data, 0x0, sizeof(data.block));
77
78         data_arg.read_write = I2C_SMBUS_WRITE;
79         data_arg.size = I2C_SMBUS_BYTE_DATA;
80         data_arg.data = &data;
81         data_arg.command = reg;
82
83         data.byte = data_in;
84
85         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
86         CHECK_ERROR(ret != 0);
87
88         return 0;
89 }
90
91 int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out)
92 {
93         int ret;
94
95         struct i2c_smbus_ioctl_data data_arg;
96         union i2c_smbus_data data;
97
98         memset(&data, 0x0, sizeof(data.block));
99
100         data_arg.read_write = I2C_SMBUS_READ;
101         data_arg.size = I2C_SMBUS_WORD_DATA;
102         data_arg.data = &data;
103         data_arg.command = reg;
104
105         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
106         CHECK_ERROR(ret != 0);
107
108         *data_out = data.word;
109
110         return 0;
111 }
112
113 int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in)
114 {
115         int ret;
116
117         struct i2c_smbus_ioctl_data data_arg;
118         union i2c_smbus_data data;
119
120         memset(&data, 0x0, sizeof(data.block));
121
122         data_arg.read_write = I2C_SMBUS_WRITE;
123         data_arg.size = I2C_SMBUS_WORD_DATA;
124         data_arg.data = &data;
125         data_arg.command = reg;
126
127         data.word = data_in;
128
129         ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg);
130         CHECK_ERROR(ret != 0);
131
132         return 0;
133 }