summaryrefslogtreecommitdiffstats
path: root/cpukit/dev/include/linux/i2c.h
blob: b113545cce26b94bb070622c320dc78045059510 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * @file
 *
 * @brief RTEMS Port of Linux I2C API
 *
 * @ingroup I2CLinux
 */

/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef _UAPI_LINUX_I2C_H
#define _UAPI_LINUX_I2C_H

#include <stdint.h>

/**
 * @defgroup I2CLinux Linux I2C User-Space API
 *
 * @ingroup I2C
 *
 * @brief RTEMS port of Linux I2C user-space API.
 *
 * Additional documentation is available through the Linux sources, see:
 *
 * - /usr/src/linux/include/uapi/linux/i2c.h,
 * - /usr/src/linux/include/uapi/linux/i2c-dev.h
 * - https://www.kernel.org/doc/Documentation/i2c/i2c-protocol
 * - https://www.kernel.org/doc/Documentation/i2c/dev-interface
 *
 * @{
 */

/**
 * @name I2C Message Flags
 *
 * @{
 */

/**
 * @brief I2C message flag to indicate a 10-bit address.
 *
 * The controller must support this as indicated by the I2C_FUNC_10BIT_ADDR
 * functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_TEN 0x0010

/**
 * @brief I2C message flag to indicate a read transfer (from slave to master).
 *
 * @see i2c_msg.
 */
#define I2C_M_RD 0x0001

/**
 * @brief I2C message flag to signal a stop condition even if this is not the
 * last message.
 *
 * The controller must support this as indicated by the
 * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_STOP 0x8000

/**
 * @brief I2C message flag to omit start condition and slave address.
 *
 * The controller must support this as indicated by the
 * @ref I2C_FUNC_NOSTART functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_NOSTART 0x4000

/**
 * @brief I2C message flag to reverse the direction flag.
 *
 * The controller must support this as indicated by the
 * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_REV_DIR_ADDR 0x2000

/**
 * @brief I2C message flag to ignore a non-acknowledge.
 *
 * The controller must support this as indicated by the
 * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_IGNORE_NAK 0x1000

/**
 * @brief I2C message flag to omit a master acknowledge/non-acknowledge in a
 * read transfer.
 *
 * The controller must support this as indicated by the
 * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
 *
 * @see i2c_msg.
 */
#define I2C_M_NO_RD_ACK 0x0800

/**
 * @brief I2C message flag to indicate that the message data length is the
 * first received byte.
 *
 * The message data buffer must be large enough to store up to 32 bytes, the
 * initial length byte and the SMBus PEC (if used).  Initialize the message
 * length to one.  The message length is incremented by the count of received
 * data bytes.
 *
 * @see i2c_msg.
 */
#define I2C_M_RECV_LEN 0x0400

/** @} */

/**
 * @brief I2C transfer message.
 */
struct i2c_msg {
  /**
   * @brief The slave address.
   *
   * In case the @ref I2C_M_TEN flag is set, then this is a 10-bit address,
   * otherwise it is a 7-bit address.
   */
  uint16_t addr;

  /**
   * @brief The message flags.
   *
   * Valid flags are
   * - @ref I2C_M_TEN,
   * - @ref I2C_M_RD,
   * - @ref I2C_M_STOP,
   * - @ref I2C_M_NOSTART,
   * - @ref I2C_M_REV_DIR_ADDR,
   * - @ref I2C_M_IGNORE_NAK,
   * - @ref I2C_M_NO_RD_ACK, and
   * - @ref I2C_M_RECV_LEN.
   */
  uint16_t flags;

  /**
   * @brief The message data length in bytes.
   */
  uint16_t len;

  /**
   * @brief Pointer to the message data.
   */
  uint8_t *buf;
};

/**
 * @name I2C Controller Functionality
 *
 * @{
 */

#define I2C_FUNC_I2C 0x00000001
#define I2C_FUNC_10BIT_ADDR 0x00000002
#define I2C_FUNC_PROTOCOL_MANGLING 0x00000004
#define I2C_FUNC_SMBUS_PEC 0x00000008
#define I2C_FUNC_NOSTART 0x00000010
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000
#define I2C_FUNC_SMBUS_QUICK 0x00010000
#define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
#define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
#define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
#define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
#define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000

#define I2C_FUNC_SMBUS_BYTE \
  (I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE)

#define I2C_FUNC_SMBUS_BYTE_DATA \
  (I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)

#define I2C_FUNC_SMBUS_WORD_DATA \
  (I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA)

#define I2C_FUNC_SMBUS_BLOCK_DATA \
  (I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)

#define I2C_FUNC_SMBUS_I2C_BLOCK \
  (I2C_FUNC_SMBUS_READ_I2C_BLOCK | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)

#define I2C_FUNC_SMBUS_EMUL \
  (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA \
    | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL \
    | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK \
    | I2C_FUNC_SMBUS_PEC)

/** @} */

/**
 * @brief Maximum SMBus data block count.
 */
#define I2C_SMBUS_BLOCK_MAX 32

/**
 * @brief SMBus data.
 */
union i2c_smbus_data {
  uint8_t byte;
  uint16_t word;
  uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
};

/**
 * @name SMBus Transfer Read and Write Markers
 *
 * @{
 */

#define I2C_SMBUS_READ 1

#define I2C_SMBUS_WRITE 0

/** @} */

/**
 * @name SMBus Transaction Types
 *
 * @{
 */

#define I2C_SMBUS_QUICK 0

#define I2C_SMBUS_BYTE 1

#define I2C_SMBUS_BYTE_DATA 2

#define I2C_SMBUS_WORD_DATA 3

#define I2C_SMBUS_PROC_CALL 4

#define I2C_SMBUS_BLOCK_DATA 5

#define I2C_SMBUS_I2C_BLOCK_BROKEN 6

#define I2C_SMBUS_BLOCK_PROC_CALL 7

#define I2C_SMBUS_I2C_BLOCK_DATA 8

/** @} */

/** @} */

#endif /* _UAPI_LINUX_I2C_H */