xref: /linux/include/uapi/linux/i2c.h (revision 2f6a470d6545841cf1891b87e360d3998ef024c8)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2deb0d335SWolfram Sang /*
3deb0d335SWolfram Sang  * i2c.h - definitions for the I2C bus interface
4deb0d335SWolfram Sang  *
5deb0d335SWolfram Sang  * Copyright (C) 1995-2000 Simon G. Vogl
6deb0d335SWolfram Sang  * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
7deb0d335SWolfram Sang  * Frodo Looijaard <frodol@dds.nl>
8deb0d335SWolfram Sang  */
9607ca46eSDavid Howells 
10607ca46eSDavid Howells #ifndef _UAPI_LINUX_I2C_H
11607ca46eSDavid Howells #define _UAPI_LINUX_I2C_H
12607ca46eSDavid Howells 
13607ca46eSDavid Howells #include <linux/types.h>
14607ca46eSDavid Howells 
15607ca46eSDavid Howells /**
16607ca46eSDavid Howells  * struct i2c_msg - an I2C transaction segment beginning with START
17bfb3939cSWolfram Sang  *
18bfb3939cSWolfram Sang  * @addr: Slave address, either 7 or 10 bits. When this is a 10 bit address,
19bfb3939cSWolfram Sang  *   %I2C_M_TEN must be set in @flags and the adapter must support
20bfb3939cSWolfram Sang  *   %I2C_FUNC_10BIT_ADDR.
21bfb3939cSWolfram Sang  *
22bfb3939cSWolfram Sang  * @flags:
23bfb3939cSWolfram Sang  *   Supported by all adapters:
24bfb3939cSWolfram Sang  *   %I2C_M_RD: read data (from slave to master). Guaranteed to be 0x0001!
25bfb3939cSWolfram Sang  *
26bfb3939cSWolfram Sang  *   Optional:
27bfb3939cSWolfram Sang  *   %I2C_M_DMA_SAFE: the buffer of this message is DMA safe. Makes only sense
28bfb3939cSWolfram Sang  *     in kernelspace, because userspace buffers are copied anyway
29bfb3939cSWolfram Sang  *
30bfb3939cSWolfram Sang  *   Only if I2C_FUNC_10BIT_ADDR is set:
31bfb3939cSWolfram Sang  *   %I2C_M_TEN: this is a 10 bit chip address
32bfb3939cSWolfram Sang  *
33bfb3939cSWolfram Sang  *   Only if I2C_FUNC_SMBUS_READ_BLOCK_DATA is set:
34bfb3939cSWolfram Sang  *   %I2C_M_RECV_LEN: message length will be first received byte
35bfb3939cSWolfram Sang  *
36bfb3939cSWolfram Sang  *   Only if I2C_FUNC_NOSTART is set:
37bfb3939cSWolfram Sang  *   %I2C_M_NOSTART: skip repeated start sequence
38bfb3939cSWolfram Sang 
39bfb3939cSWolfram Sang  *   Only if I2C_FUNC_PROTOCOL_MANGLING is set:
40bfb3939cSWolfram Sang  *   %I2C_M_NO_RD_ACK: in a read message, master ACK/NACK bit is skipped
41bfb3939cSWolfram Sang  *   %I2C_M_IGNORE_NAK: treat NACK from client as ACK
42bfb3939cSWolfram Sang  *   %I2C_M_REV_DIR_ADDR: toggles the Rd/Wr bit
43bfb3939cSWolfram Sang  *   %I2C_M_STOP: force a STOP condition after the message
44bfb3939cSWolfram Sang  *
45bfb3939cSWolfram Sang  * @len: Number of data bytes in @buf being read from or written to the I2C
46bfb3939cSWolfram Sang  *   slave address. For read transactions where %I2C_M_RECV_LEN is set, the
47bfb3939cSWolfram Sang  *   caller guarantees that this buffer can hold up to %I2C_SMBUS_BLOCK_MAX
48bfb3939cSWolfram Sang  *   bytes in addition to the initial length byte sent by the slave (plus,
49bfb3939cSWolfram Sang  *   if used, the SMBus PEC); and this value will be incremented by the number
50bfb3939cSWolfram Sang  *   of block data bytes received.
51bfb3939cSWolfram Sang  *
52607ca46eSDavid Howells  * @buf: The buffer into which data is read, or from which it's written.
53607ca46eSDavid Howells  *
54607ca46eSDavid Howells  * An i2c_msg is the low level representation of one segment of an I2C
55607ca46eSDavid Howells  * transaction.  It is visible to drivers in the @i2c_transfer() procedure,
56607ca46eSDavid Howells  * to userspace from i2c-dev, and to I2C adapter drivers through the
57607ca46eSDavid Howells  * @i2c_adapter.@master_xfer() method.
58607ca46eSDavid Howells  *
59607ca46eSDavid Howells  * Except when I2C "protocol mangling" is used, all I2C adapters implement
60607ca46eSDavid Howells  * the standard rules for I2C transactions.  Each transaction begins with a
61607ca46eSDavid Howells  * START.  That is followed by the slave address, and a bit encoding read
62607ca46eSDavid Howells  * versus write.  Then follow all the data bytes, possibly including a byte
63607ca46eSDavid Howells  * with SMBus PEC.  The transfer terminates with a NAK, or when all those
64607ca46eSDavid Howells  * bytes have been transferred and ACKed.  If this is the last message in a
65607ca46eSDavid Howells  * group, it is followed by a STOP.  Otherwise it is followed by the next
66607ca46eSDavid Howells  * @i2c_msg transaction segment, beginning with a (repeated) START.
67607ca46eSDavid Howells  *
68bfb3939cSWolfram Sang  * Alternatively, when the adapter supports %I2C_FUNC_PROTOCOL_MANGLING then
69607ca46eSDavid Howells  * passing certain @flags may have changed those standard protocol behaviors.
70607ca46eSDavid Howells  * Those flags are only for use with broken/nonconforming slaves, and with
71bfb3939cSWolfram Sang  * adapters which are known to support the specific mangling options they need.
72607ca46eSDavid Howells  */
73607ca46eSDavid Howells struct i2c_msg {
74bfb3939cSWolfram Sang 	__u16 addr;
75607ca46eSDavid Howells 	__u16 flags;
76bfb3939cSWolfram Sang #define I2C_M_RD		0x0001	/* guaranteed to be 0x0001! */
77bfb3939cSWolfram Sang #define I2C_M_TEN		0x0010	/* use only if I2C_FUNC_10BIT_ADDR */
78bfb3939cSWolfram Sang #define I2C_M_DMA_SAFE		0x0200	/* use only in kernel space */
79bfb3939cSWolfram Sang #define I2C_M_RECV_LEN		0x0400	/* use only if I2C_FUNC_SMBUS_READ_BLOCK_DATA */
80bfb3939cSWolfram Sang #define I2C_M_NO_RD_ACK		0x0800	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
81bfb3939cSWolfram Sang #define I2C_M_IGNORE_NAK	0x1000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
82bfb3939cSWolfram Sang #define I2C_M_REV_DIR_ADDR	0x2000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
83bfb3939cSWolfram Sang #define I2C_M_NOSTART		0x4000	/* use only if I2C_FUNC_NOSTART */
84bfb3939cSWolfram Sang #define I2C_M_STOP		0x8000	/* use only if I2C_FUNC_PROTOCOL_MANGLING */
85bfb3939cSWolfram Sang 	__u16 len;
86bfb3939cSWolfram Sang 	__u8 *buf;
87607ca46eSDavid Howells };
88607ca46eSDavid Howells 
89607ca46eSDavid Howells /* To determine what functionality is present */
90607ca46eSDavid Howells 
91607ca46eSDavid Howells #define I2C_FUNC_I2C			0x00000001
92bfb3939cSWolfram Sang #define I2C_FUNC_10BIT_ADDR		0x00000002 /* required for I2C_M_TEN */
93bfb3939cSWolfram Sang #define I2C_FUNC_PROTOCOL_MANGLING	0x00000004 /* required for I2C_M_IGNORE_NAK etc. */
94607ca46eSDavid Howells #define I2C_FUNC_SMBUS_PEC		0x00000008
95bfb3939cSWolfram Sang #define I2C_FUNC_NOSTART		0x00000010 /* required for I2C_M_NOSTART */
9683caf989SWolfram Sang #define I2C_FUNC_SLAVE			0x00000020
97bfb3939cSWolfram Sang #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x00008000 /* SMBus 2.0 or later */
98607ca46eSDavid Howells #define I2C_FUNC_SMBUS_QUICK		0x00010000
99607ca46eSDavid Howells #define I2C_FUNC_SMBUS_READ_BYTE	0x00020000
100607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WRITE_BYTE	0x00040000
101607ca46eSDavid Howells #define I2C_FUNC_SMBUS_READ_BYTE_DATA	0x00080000
102607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA	0x00100000
103607ca46eSDavid Howells #define I2C_FUNC_SMBUS_READ_WORD_DATA	0x00200000
104607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WRITE_WORD_DATA	0x00400000
105607ca46eSDavid Howells #define I2C_FUNC_SMBUS_PROC_CALL	0x00800000
106bfb3939cSWolfram Sang #define I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x01000000 /* required for I2C_M_RECV_LEN */
107607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
108607ca46eSDavid Howells #define I2C_FUNC_SMBUS_READ_I2C_BLOCK	0x04000000 /* I2C-like block xfer  */
109607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK	0x08000000 /* w/ 1-byte reg. addr. */
110bfb3939cSWolfram Sang #define I2C_FUNC_SMBUS_HOST_NOTIFY	0x10000000 /* SMBus 2.0 or later */
111607ca46eSDavid Howells 
112607ca46eSDavid Howells #define I2C_FUNC_SMBUS_BYTE		(I2C_FUNC_SMBUS_READ_BYTE | \
113607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_BYTE)
114607ca46eSDavid Howells #define I2C_FUNC_SMBUS_BYTE_DATA	(I2C_FUNC_SMBUS_READ_BYTE_DATA | \
115607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
116607ca46eSDavid Howells #define I2C_FUNC_SMBUS_WORD_DATA	(I2C_FUNC_SMBUS_READ_WORD_DATA | \
117607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_WORD_DATA)
118607ca46eSDavid Howells #define I2C_FUNC_SMBUS_BLOCK_DATA	(I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
119607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
120607ca46eSDavid Howells #define I2C_FUNC_SMBUS_I2C_BLOCK	(I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
121607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
122607ca46eSDavid Howells 
123607ca46eSDavid Howells #define I2C_FUNC_SMBUS_EMUL		(I2C_FUNC_SMBUS_QUICK | \
124607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_BYTE | \
125607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_BYTE_DATA | \
126607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WORD_DATA | \
127607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_PROC_CALL | \
128607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
129607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_I2C_BLOCK | \
130607ca46eSDavid Howells 					 I2C_FUNC_SMBUS_PEC)
131607ca46eSDavid Howells 
13221500aa8SWolfram Sang /* if I2C_M_RECV_LEN is also supported */
13321500aa8SWolfram Sang #define I2C_FUNC_SMBUS_EMUL_ALL		(I2C_FUNC_SMBUS_EMUL | \
13421500aa8SWolfram Sang 					 I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
13521500aa8SWolfram Sang 					 I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
13621500aa8SWolfram Sang 
137607ca46eSDavid Howells /*
138607ca46eSDavid Howells  * Data for SMBus Messages
139607ca46eSDavid Howells  */
140*2f6a470dSJakub Kicinski #define I2C_SMBUS_BLOCK_MAX	32	/* As specified in SMBus standard */
141607ca46eSDavid Howells union i2c_smbus_data {
142607ca46eSDavid Howells 	__u8 byte;
143607ca46eSDavid Howells 	__u16 word;
144607ca46eSDavid Howells 	__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
145607ca46eSDavid Howells 			       /* and one more for user-space compatibility */
146607ca46eSDavid Howells };
147607ca46eSDavid Howells 
148607ca46eSDavid Howells /* i2c_smbus_xfer read or write markers */
149607ca46eSDavid Howells #define I2C_SMBUS_READ	1
150607ca46eSDavid Howells #define I2C_SMBUS_WRITE	0
151607ca46eSDavid Howells 
152607ca46eSDavid Howells /* SMBus transaction types (size parameter in the above functions)
153607ca46eSDavid Howells    Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
154607ca46eSDavid Howells #define I2C_SMBUS_QUICK		    0
155607ca46eSDavid Howells #define I2C_SMBUS_BYTE		    1
156607ca46eSDavid Howells #define I2C_SMBUS_BYTE_DATA	    2
157607ca46eSDavid Howells #define I2C_SMBUS_WORD_DATA	    3
158607ca46eSDavid Howells #define I2C_SMBUS_PROC_CALL	    4
159607ca46eSDavid Howells #define I2C_SMBUS_BLOCK_DATA	    5
160607ca46eSDavid Howells #define I2C_SMBUS_I2C_BLOCK_BROKEN  6
161607ca46eSDavid Howells #define I2C_SMBUS_BLOCK_PROC_CALL   7		/* SMBus 2.0 */
162607ca46eSDavid Howells #define I2C_SMBUS_I2C_BLOCK_DATA    8
163607ca46eSDavid Howells 
164607ca46eSDavid Howells #endif /* _UAPI_LINUX_I2C_H */
165