i2c: add busnum to context
[contrib/mraa.git] / src / i2c / i2c.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Code is modified from the RoadNarrows-robotics i2c library (distributed under
6  * the MIT license, license is included verbatim under src/i2c/LICENSE)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include "i2c.h"
29 #include "mraa_internal.h"
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/ioctl.h>
38 #include "linux/i2c-dev.h"
39
40 typedef union i2c_smbus_data_union
41 {
42     uint8_t byte; ///< data byte
43     unsigned short word; ///< data short word
44     uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
45     ///< block[0] is used for length and one more for PEC
46 } i2c_smbus_data_t;
47
48 typedef struct i2c_smbus_ioctl_data_struct
49 {
50     uint8_t read_write; ///< operation direction
51     uint8_t command; ///< ioctl command
52     int size; ///< data size
53     i2c_smbus_data_t *data; ///< data
54 } i2c_smbus_ioctl_data_t;
55
56 int
57 mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size,
58         i2c_smbus_data_t *data)
59 {
60   i2c_smbus_ioctl_data_t args;
61
62   args.read_write = read_write;
63   args.command = command;
64   args.size = size;
65   args.data = data;
66
67   return ioctl(fh, I2C_SMBUS, &args);
68 }
69
70 mraa_i2c_context
71 mraa_i2c_init(int bus)
72 {
73     if (plat == NULL) {
74         syslog(LOG_ERR, "i2c: Platform Not Initialised");
75         return NULL;
76     }
77     if (plat->i2c_bus_count == 0) {
78         syslog(LOG_ERR, "No i2c buses defined in platform");
79         return NULL;
80     }
81     if (bus >= plat->i2c_bus_count) {
82         syslog(LOG_ERR, "Above i2c bus count");
83         return NULL;
84     }
85
86     if (plat->i2c_bus[bus].bus_id == -1) {
87         syslog(LOG_ERR, "Invalid i2c bus, moving to default i2c bus");
88         bus = plat->def_i2c_bus;
89     }
90
91     int pos = plat->i2c_bus[bus].sda;
92     if (plat->pins[pos].i2c.mux_total > 0)
93         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
94             syslog(LOG_ERR, "i2c: Failed to set-up i2c sda multiplexer");
95             return NULL;
96         }
97
98     pos = plat->i2c_bus[bus].scl;
99     if (plat->pins[pos].i2c.mux_total > 0)
100         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
101             syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer");
102             return NULL;
103         }
104
105     return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id);
106 }
107
108 mraa_i2c_context
109 mraa_i2c_init_raw(unsigned int bus)
110 {
111     if (advance_func->i2c_init_pre != NULL) {
112         if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
113             return NULL;
114     }
115     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
116     if (dev == NULL) {
117         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
118         return NULL;
119     }
120
121     char filepath[32];
122     dev->busnum = bus;
123     snprintf(filepath, 32, "/dev/i2c-%u", bus);
124     if ((dev->fh = open(filepath, O_RDWR)) < 1) {
125         syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
126     }
127
128     if (advance_func->i2c_init_post != NULL) {
129         mraa_result_t ret = advance_func->i2c_init_post(dev);
130         if (ret != MRAA_SUCCESS) {
131             free(dev);
132             return NULL;
133         }
134     }
135     return dev;
136 }
137
138 mraa_result_t
139 mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
140 {
141     return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
142 }
143
144 int
145 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
146 {
147     // this is the read(3) syscall not mraa_i2c_read()
148     if (read(dev->fh, data, length) == length) {
149         return length;
150     }
151     return 0;
152 }
153
154 uint8_t
155 mraa_i2c_read_byte(mraa_i2c_context dev)
156 {
157     i2c_smbus_data_t d;
158
159     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD,
160             I2C_SMBUS_BYTE, &d) < 0) {
161         syslog(LOG_ERR, "i2c: Failed to write");
162         return 0;
163     }
164     return 0x0FF & d.byte;
165 }
166
167 uint8_t
168 mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
169 {
170     i2c_smbus_data_t d;
171
172     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
173             I2C_SMBUS_BYTE_DATA, &d) < 0) {
174         syslog(LOG_ERR, "i2c: Failed to write");
175         return 0;
176     }
177     return 0x0FF & d.byte;
178 }
179
180 uint16_t
181 mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
182 {
183     i2c_smbus_data_t d;
184
185     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
186             I2C_SMBUS_WORD_DATA, &d) < 0) {
187         syslog(LOG_ERR, "i2c: Failed to write");
188         return 0;
189     }
190     return 0xFFFF & d.word;
191 }
192
193 mraa_result_t
194 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
195 {
196     i2c_smbus_data_t d;
197     int i;
198     uint8_t command = data[0];
199
200     data = &data[1];
201     length = length-1;
202     if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
203         length = I2C_SMBUS_I2C_BLOCK_MAX;
204     }
205
206     for (i=1; i<=length; i++) {
207         d.block[i] = data[i-1];
208     }
209     d.block[0] = length;
210
211     return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
212             I2C_SMBUS_I2C_BLOCK_DATA, &d);
213 }
214
215 mraa_result_t
216 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
217 {
218     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data,
219             I2C_SMBUS_BYTE, NULL) < 0) {
220         syslog(LOG_ERR, "i2c: Failed to write");
221         return MRAA_ERROR_INVALID_HANDLE;
222     }
223     return MRAA_SUCCESS;
224 }
225
226 mraa_result_t
227 mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
228         const uint8_t command)
229 {
230     i2c_smbus_data_t d;
231
232     d.byte = data;
233     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
234             I2C_SMBUS_BYTE_DATA, &d) < 0) {
235         syslog(LOG_ERR, "i2c: Failed to write");
236         return MRAA_ERROR_INVALID_HANDLE;
237     }
238     return MRAA_SUCCESS;
239 }
240
241 mraa_result_t
242 mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data,
243         const uint8_t command)
244 {
245     i2c_smbus_data_t d;
246
247     d.word = data;
248     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
249             I2C_SMBUS_WORD_DATA, &d) < 0) {
250         syslog(LOG_ERR, "i2c: Failed to write");
251         return MRAA_ERROR_INVALID_HANDLE;
252     }
253     return MRAA_SUCCESS;
254 }
255
256 mraa_result_t
257 mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
258 {
259     dev->addr = (int) addr;
260     if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
261         syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
262         return MRAA_ERROR_INVALID_HANDLE;
263     }
264     return MRAA_SUCCESS;
265 }
266
267 mraa_result_t
268 mraa_i2c_stop(mraa_i2c_context dev)
269 {
270     free(dev);
271     return MRAA_SUCCESS;
272 }