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