Merge branch 'hook'
[contrib/mraa.git] / include / smbus.h
1 /*
2  * Author: Robin Knight (robin.knight@roadnarrows.com)
3  *
4  * Copyright © 2009 RoadNarrows LLC.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that
9  * (1) The above copyright notice and the following two paragraphs
10  * appear in all copies of the source code and (2) redistributions
11  * including binaries reproduces these notices in the supporting
12  * documentation.   Substantial modifications to this software may be
13  * copyrighted by their authors and need not follow the licensing terms
14  * described here, provided that the new terms are clearly indicated in
15  * all files where they apply.
16  *
17  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
18  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
19  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
20  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
21  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
22  * THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
27  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
28  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
29  */
30
31 #pragma once
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <sys/types.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
44
45 #include "linux/i2c-dev.h"
46
47 typedef union i2c_smbus_data_union
48 {
49     uint8_t byte; ///< data byte
50     unsigned short word; ///< data short word
51     uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
52     ///< block[0] is used for length and one more for PEC
53 } i2c_smbus_data_t;
54
55 typedef struct i2c_smbus_ioctl_data_struct
56 {
57     uint8_t read_write; ///< operation direction
58     uint8_t command; ///< ioctl command
59     int size; ///< data size
60     i2c_smbus_data_t *data; ///< data
61 } i2c_smbus_ioctl_data_t;
62
63 // ---------------------------------------------------------------------------
64 // Prototypes
65 // ---------------------------------------------------------------------------
66
67 extern int i2c_smbus_access(int fd, uint8_t read_write, uint8_t command,
68                       int size, i2c_smbus_data_t *data);
69
70 extern int i2c_smbus_write_quick(int fd, uint8_t value);
71
72 extern int i2c_smbus_read_byte(int fd);
73
74 extern int i2c_smbus_write_byte(int fd, uint8_t value);
75
76 extern int i2c_smbus_read_byte_data(int fd, uint8_t command);
77
78 extern int i2c_smbus_write_byte_data(int fd, uint8_t command, uint8_t value);
79
80 extern int i2c_smbus_read_word_data(int fd, uint8_t command);
81
82 extern int i2c_smbus_write_word_data(int fd, uint8_t command, unsigned short value);
83
84 extern int i2c_smbus_process_call(int fd, uint8_t command, unsigned short value);
85
86 extern int i2c_smbus_read_block_data(int fd, uint8_t command, uint8_t *values);
87
88 extern int i2c_smbus_write_block_data(int fd, uint8_t command, uint8_t length,
89                                         const uint8_t *values);
90 extern int i2c_smbus_read_i2c_block_data(int fd, uint8_t command,
91                                           uint8_t *values);
92
93 extern int i2c_smbus_write_i2c_block_data(int fd, uint8_t command, uint8_t length,
94                                     const uint8_t *values);
95
96 extern int i2c_smbus_block_process_call(int fd, uint8_t command, uint8_t length,
97                                           uint8_t *values);
98
99 #ifdef __cplusplus
100 }
101 #endif