972ef2e8b81f9095e338dc28af29dc52a38ae3bf
[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
41 typedef union i2c_smbus_data_union {
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     uint8_t read_write;     ///< operation direction
50     uint8_t command;        ///< ioctl command
51     int size;               ///< data size
52     i2c_smbus_data_t* data; ///< data
53 } i2c_smbus_ioctl_data_t;
54
55
56 // static mraa_adv_func_t* func_table;
57
58 int
59 mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size, i2c_smbus_data_t* data)
60 {
61     i2c_smbus_ioctl_data_t args;
62
63     args.read_write = read_write;
64     args.command = command;
65     args.size = size;
66     args.data = data;
67
68     return ioctl(fh, I2C_SMBUS, &args);
69 }
70
71 static mraa_i2c_context
72 mraa_i2c_init_internal(mraa_adv_func_t* advance_func, unsigned int bus)
73 {
74     mraa_result_t status = MRAA_SUCCESS;
75
76     if (advance_func == NULL)
77         return NULL;
78
79     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
80     if (dev == NULL) {
81         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
82         return NULL;
83     }
84
85     dev->advance_func = advance_func;
86     dev->busnum = bus;
87
88     if (IS_FUNC_DEFINED(dev, i2c_init_pre)) {
89         status = advance_func->i2c_init_pre(bus);
90         if (status != MRAA_SUCCESS)
91             goto init_internal_cleanup;
92     }
93
94     if (IS_FUNC_DEFINED(dev, i2c_init_bus_replace)) {
95         status = dev->advance_func->i2c_init_bus_replace(dev);
96         if (status != MRAA_SUCCESS)
97             goto init_internal_cleanup;
98     } else {
99         char filepath[32];
100         snprintf(filepath, 32, "/dev/i2c-%u", bus);
101         if ((dev->fh = open(filepath, O_RDWR)) < 1) {
102             syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
103             status = MRAA_ERROR_NO_RESOURCES;
104             goto init_internal_cleanup;
105         }
106
107         if (ioctl(dev->fh, I2C_FUNCS, &dev->funcs) < 0) {
108             syslog(LOG_CRIT, "i2c: Failed to get I2C_FUNC map from device");
109             dev->funcs = 0;
110         }
111     }
112
113     if (IS_FUNC_DEFINED(dev, i2c_init_post)) {
114         status = dev->advance_func->i2c_init_post(dev);
115         if (status != MRAA_SUCCESS)
116             goto init_internal_cleanup;
117     }
118
119
120 init_internal_cleanup:
121     if (status == MRAA_SUCCESS) {
122         return dev;
123         } else {
124         if (dev != NULL)
125             free(dev);
126         return NULL;
127    }
128 }
129
130
131 mraa_i2c_context
132 mraa_i2c_init(int bus)
133 {
134     mraa_board_t* board = plat;
135     if (board == NULL) {
136         syslog(LOG_ERR, "i2c: Platform Not Initialised");
137         return NULL;
138     }
139
140     if (mraa_is_sub_platform_id(bus)) {
141         syslog(LOG_NOTICE, "i2c: Using sub platform");
142         board = board->sub_platform;
143         if (board == NULL) {
144             syslog(LOG_ERR, "i2c: Sub platform Not Initialised");
145             return NULL;
146         }
147         bus = mraa_get_sub_platform_index(bus);
148     }
149     syslog(LOG_NOTICE, "i2c: Selected bus %d", bus);
150
151     if (board->i2c_bus_count == 0) {
152         syslog(LOG_ERR, "No i2c buses defined in platform");
153         return NULL;
154     }
155     if (bus >= board->i2c_bus_count) {
156         syslog(LOG_ERR, "Above i2c bus count");
157         return NULL;
158     }
159
160     if (board->i2c_bus[bus].bus_id == -1) {
161         syslog(LOG_ERR, "Invalid i2c bus, moving to default i2c bus");
162         bus = board->def_i2c_bus;
163     }
164
165     int pos = board->i2c_bus[bus].sda;
166     if (board->pins[pos].i2c.mux_total > 0) {
167         if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
168             syslog(LOG_ERR, "i2c: Failed to set-up i2c sda multiplexer");
169             return NULL;
170         }
171     }
172
173     pos = board->i2c_bus[bus].scl;
174     if (board->pins[pos].i2c.mux_total > 0) {
175         if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
176             syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer");
177             return NULL;
178         }
179     }
180
181     return mraa_i2c_init_internal(board->adv_func, (unsigned int) board->i2c_bus[bus].bus_id);
182 }
183
184
185 mraa_i2c_context
186 mraa_i2c_init_raw(unsigned int bus)
187 {
188     return mraa_i2c_init_internal(plat == NULL ? NULL : plat->adv_func, bus);
189 }
190
191
192 mraa_result_t
193 mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
194 {
195     if (IS_FUNC_DEFINED(dev, i2c_set_frequency_replace)) {
196         return dev->advance_func->i2c_set_frequency_replace(dev, mode);
197     }
198     return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
199 }
200
201 int
202 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
203 {
204     int bytes_read = 0;
205     if (IS_FUNC_DEFINED(dev, i2c_read_replace))
206         bytes_read = dev->advance_func->i2c_read_replace(dev, data, length);
207     else
208         bytes_read = read(dev->fh, data, length);
209    if (bytes_read == length)
210       return length;
211    else
212       return 0;
213 }
214
215 uint8_t
216 mraa_i2c_read_byte(mraa_i2c_context dev)
217 {
218     if (IS_FUNC_DEFINED(dev, i2c_read_byte_replace))
219         return dev->advance_func->i2c_read_byte_replace(dev);
220     i2c_smbus_data_t d;
221     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD, I2C_SMBUS_BYTE, &d) < 0) {
222         syslog(LOG_ERR, "i2c: Failed to write");
223         return 0;
224     }
225     return 0x0FF & d.byte;
226 }
227
228 uint8_t
229 mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
230 {
231     if (IS_FUNC_DEFINED(dev, i2c_read_byte_data_replace))
232         return dev->advance_func->i2c_read_byte_data_replace(dev, command);
233     i2c_smbus_data_t d;
234     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
235         syslog(LOG_ERR, "i2c: Failed to write");
236         return 0;
237     }
238     return 0x0FF & d.byte;
239 }
240
241 uint16_t
242 mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
243 {
244     if (IS_FUNC_DEFINED(dev, i2c_read_word_data_replace))
245         return dev->advance_func->i2c_read_word_data_replace(dev, command);
246     i2c_smbus_data_t d;
247     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
248         syslog(LOG_ERR, "i2c: Failed to write");
249         return 0;
250     }
251     return 0xFFFF & d.word;
252 }
253
254 int
255 mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
256 {
257     if (IS_FUNC_DEFINED(dev, i2c_read_bytes_data_replace))
258         return dev->advance_func->i2c_read_bytes_data_replace(dev, command, data, length);
259     struct i2c_rdwr_ioctl_data d;
260     struct i2c_msg m[2];
261
262     m[0].addr = dev->addr;
263     m[0].flags = 0x00;
264     m[0].len = 1;
265     m[0].buf = &command;
266     m[1].addr = dev->addr;
267     m[1].flags = I2C_M_RD;
268     m[1].len = length;
269     m[1].buf = data;
270
271     d.msgs = m;
272     d.nmsgs = 2;
273
274     return ioctl(dev->fh, I2C_RDWR, &d) < 0 ? -1 : length;
275 }
276
277 mraa_result_t
278 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
279 {
280     if (IS_FUNC_DEFINED(dev, i2c_write_replace))
281         return dev->advance_func->i2c_write_replace(dev, data, length);
282     i2c_smbus_data_t d;
283     int i;
284     uint8_t command = data[0];
285
286     data = &data[1];
287     length = length - 1;
288     if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
289         length = I2C_SMBUS_I2C_BLOCK_MAX;
290     }
291
292     for (i = 1; i <= length; i++) {
293         d.block[i] = data[i - 1];
294     }
295     d.block[0] = length;
296
297     return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &d);
298 }
299
300 mraa_result_t
301 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
302 {
303     if (IS_FUNC_DEFINED(dev, i2c_write_byte_replace)) {
304         return dev->advance_func->i2c_write_byte_replace(dev, data);
305     } else {
306         if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) < 0) {
307             syslog(LOG_ERR, "i2c: Failed to write");
308             return MRAA_ERROR_INVALID_HANDLE;
309         }
310         return MRAA_SUCCESS;
311     }
312 }
313
314 mraa_result_t
315 mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
316 {
317     if (IS_FUNC_DEFINED(dev, i2c_write_byte_data_replace))
318         return dev->advance_func->i2c_write_byte_data_replace(dev, data, command);
319     i2c_smbus_data_t d;
320     d.byte = data;
321     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
322         syslog(LOG_ERR, "i2c: Failed to write");
323         return MRAA_ERROR_INVALID_HANDLE;
324     }
325     return MRAA_SUCCESS;
326 }
327
328 mraa_result_t
329 mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
330 {
331     if (IS_FUNC_DEFINED(dev, i2c_write_word_data_replace))
332         return dev->advance_func->i2c_write_word_data_replace(dev, data, command);
333     i2c_smbus_data_t d;
334     d.word = data;
335     if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
336         syslog(LOG_ERR, "i2c: Failed to write");
337         return MRAA_ERROR_INVALID_HANDLE;
338     }
339     return MRAA_SUCCESS;
340 }
341
342 mraa_result_t
343 mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
344 {
345     if (dev == NULL) {
346         return MRAA_ERROR_INVALID_HANDLE;
347     }
348
349     dev->addr = (int) addr;
350     if (IS_FUNC_DEFINED(dev, i2c_address_replace)) {
351         return dev->advance_func->i2c_address_replace(dev, addr);
352     } else {
353         if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
354             syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
355             return MRAA_ERROR_INVALID_HANDLE;
356         }
357         return MRAA_SUCCESS;
358     }
359 }
360
361
362 mraa_result_t
363 mraa_i2c_stop(mraa_i2c_context dev)
364 {
365     free(dev);
366     return MRAA_SUCCESS;
367 }
368