xref: /linux/include/uapi/linux/i2c.h (revision 0ae982df67760cd08affa935c0fe86c8a9311797)
1 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2 /*
3  * i2c.h - definitions for the I2C bus interface
4  *
5  * Copyright (C) 1995-2000 Simon G. Vogl
6  * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
7  * Frodo Looijaard <frodol@dds.nl>
8  */
9 
10 #ifndef _UAPI_LINUX_I2C_H
11 #define _UAPI_LINUX_I2C_H
12 
13 #include <linux/types.h>
14 
15 /**
16  * struct i2c_msg - an I2C transaction segment beginning with START
17  *
18  * @addr: Slave address, either 7 or 10 bits. When this is a 10 bit address,
19  *   %I2C_M_TEN must be set in @flags and the adapter must support
20  *   %I2C_FUNC_10BIT_ADDR.
21  *
22  * @flags:
23  *   Supported by all adapters:
24  *   %I2C_M_RD: read data (from slave to master). Guaranteed to be 0x0001! If
25  *   not set, the transaction is interpreted as write.
26  *
27  *   Optional:
28  *   %I2C_M_DMA_SAFE: the buffer of this message is DMA safe. Makes only sense
29  *     in kernelspace, because userspace buffers are copied anyway
30  *
31  *   Only if I2C_FUNC_10BIT_ADDR is set:
32  *   %I2C_M_TEN: this is a 10 bit chip address
33  *
34  *   Only if I2C_FUNC_SMBUS_READ_BLOCK_DATA is set:
35  *   %I2C_M_RECV_LEN: message length will be first received byte
36  *
37  *   Only if I2C_FUNC_NOSTART is set:
38  *   %I2C_M_NOSTART: skip repeated start sequence
39 
40  *   Only if I2C_FUNC_PROTOCOL_MANGLING is set:
41  *   %I2C_M_NO_RD_ACK: in a read message, master ACK/NACK bit is skipped
42  *   %I2C_M_IGNORE_NAK: treat NACK from client as ACK
43  *   %I2C_M_REV_DIR_ADDR: toggles the Rd/Wr bit
44  *   %I2C_M_STOP: force a STOP condition after the message
45  *
46  * @len: Number of data bytes in @buf being read from or written to the I2C
47  *   slave address. For read transactions where %I2C_M_RECV_LEN is set, the
48  *   caller guarantees that this buffer can hold up to %I2C_SMBUS_BLOCK_MAX
49  *   bytes in addition to the initial length byte sent by the slave (plus,
50  *   if used, the SMBus PEC); and this value will be incremented by the number
51  *   of block data bytes received.
52  *
53  * @buf: The buffer into which data is read, or from which it's written.
54  *
55  * An i2c_msg is the low level representation of one segment of an I2C
56  * transaction.  It is visible to drivers in the @i2c_transfer() procedure,
57  * to userspace from i2c-dev, and to I2C adapter drivers through the
58  * @i2c_adapter.@master_xfer() method.
59  *
60  * Except when I2C "protocol mangling" is used, all I2C adapters implement
61  * the standard rules for I2C transactions.  Each transaction begins with a
62  * START.  That is followed by the slave address, and a bit encoding read
63  * versus write.  Then follow all the data bytes, possibly including a byte
64  * with SMBus PEC.  The transfer terminates with a NAK, or when all those
65  * bytes have been transferred and ACKed.  If this is the last message in a
66  * group, it is followed by a STOP.  Otherwise it is followed by the next
67  * @i2c_msg transaction segment, beginning with a (repeated) START.
68  *
69  * Alternatively, when the adapter supports %I2C_FUNC_PROTOCOL_MANGLING then
70  * passing certain @flags may have changed those standard protocol behaviors.
71  * Those flags are only for use with broken/nonconforming slaves, and with
72  * adapters which are known to support the specific mangling options they need.
73  */
74 struct i2c_msg {
75 	__u16 addr;
76 	__u16 flags;
77 #define I2C_M_RD		0x0001	/* guaranteed to be 0x0001! */
78 #define I2C_M_TEN		0x0010	/* use only if I2C_FUNC_10BIT_ADDR */
79 #define I2C_M_DMA_SAFE		0x0200	/* use only in kernel space */
80 #define I2C_M_RECV_LEN		0x0400	/* use only if I2C_FUNC_SMBUS_READ_BLOCK_DATA */
81 #define I2C_M_NO_RD_ACK		0x0800	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
82 #define I2C_M_IGNORE_NAK	0x1000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
83 #define I2C_M_REV_DIR_ADDR	0x2000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
84 #define I2C_M_NOSTART		0x4000	/* use only if I2C_FUNC_NOSTART */
85 #define I2C_M_STOP		0x8000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
86 	__u16 len;
87 	__u8 *buf;
88 };
89 
90 /* To determine what functionality is present */
91 
92 #define I2C_FUNC_I2C			0x00000001
93 #define I2C_FUNC_10BIT_ADDR		0x00000002 /* required for I2C_M_TEN */
94 #define I2C_FUNC_PROTOCOL_MANGLING	0x00000004 /* required for I2C_M_IGNORE_NAK etc. */
95 #define I2C_FUNC_SMBUS_PEC		0x00000008
96 #define I2C_FUNC_NOSTART		0x00000010 /* required for I2C_M_NOSTART */
97 #define I2C_FUNC_SLAVE			0x00000020
98 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x00008000 /* SMBus 2.0 or later */
99 #define I2C_FUNC_SMBUS_QUICK		0x00010000
100 #define I2C_FUNC_SMBUS_READ_BYTE	0x00020000
101 #define I2C_FUNC_SMBUS_WRITE_BYTE	0x00040000
102 #define I2C_FUNC_SMBUS_READ_BYTE_DATA	0x00080000
103 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA	0x00100000
104 #define I2C_FUNC_SMBUS_READ_WORD_DATA	0x00200000
105 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA	0x00400000
106 #define I2C_FUNC_SMBUS_PROC_CALL	0x00800000
107 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x01000000 /* required for I2C_M_RECV_LEN */
108 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
109 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK	0x04000000 /* I2C-like block xfer  */
110 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK	0x08000000 /* w/ 1-byte reg. addr. */
111 #define I2C_FUNC_SMBUS_HOST_NOTIFY	0x10000000 /* SMBus 2.0 or later */
112 
113 #define I2C_FUNC_SMBUS_BYTE		(I2C_FUNC_SMBUS_READ_BYTE | \
114 					 I2C_FUNC_SMBUS_WRITE_BYTE)
115 #define I2C_FUNC_SMBUS_BYTE_DATA	(I2C_FUNC_SMBUS_READ_BYTE_DATA | \
116 					 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
117 #define I2C_FUNC_SMBUS_WORD_DATA	(I2C_FUNC_SMBUS_READ_WORD_DATA | \
118 					 I2C_FUNC_SMBUS_WRITE_WORD_DATA)
119 #define I2C_FUNC_SMBUS_BLOCK_DATA	(I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
120 					 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
121 #define I2C_FUNC_SMBUS_I2C_BLOCK	(I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
122 					 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
123 
124 #define I2C_FUNC_SMBUS_EMUL		(I2C_FUNC_SMBUS_QUICK | \
125 					 I2C_FUNC_SMBUS_BYTE | \
126 					 I2C_FUNC_SMBUS_BYTE_DATA | \
127 					 I2C_FUNC_SMBUS_WORD_DATA | \
128 					 I2C_FUNC_SMBUS_PROC_CALL | \
129 					 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
130 					 I2C_FUNC_SMBUS_I2C_BLOCK | \
131 					 I2C_FUNC_SMBUS_PEC)
132 
133 /* if I2C_M_RECV_LEN is also supported */
134 #define I2C_FUNC_SMBUS_EMUL_ALL		(I2C_FUNC_SMBUS_EMUL | \
135 					 I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
136 					 I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
137 
138 /*
139  * Data for SMBus Messages
140  */
141 #define I2C_SMBUS_BLOCK_MAX	32	/* As specified in SMBus standard */
142 union i2c_smbus_data {
143 	__u8 byte;
144 	__u16 word;
145 	__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
146 			       /* and one more for user-space compatibility */
147 };
148 
149 /* i2c_smbus_xfer read or write markers */
150 #define I2C_SMBUS_READ	1
151 #define I2C_SMBUS_WRITE	0
152 
153 /* SMBus transaction types (size parameter in the above functions)
154    Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
155 #define I2C_SMBUS_QUICK		    0
156 #define I2C_SMBUS_BYTE		    1
157 #define I2C_SMBUS_BYTE_DATA	    2
158 #define I2C_SMBUS_WORD_DATA	    3
159 #define I2C_SMBUS_PROC_CALL	    4
160 #define I2C_SMBUS_BLOCK_DATA	    5
161 #define I2C_SMBUS_I2C_BLOCK_BROKEN  6
162 #define I2C_SMBUS_BLOCK_PROC_CALL   7		/* SMBus 2.0 */
163 #define I2C_SMBUS_I2C_BLOCK_DATA    8
164 
165 #endif /* _UAPI_LINUX_I2C_H */
166