xref: /illumos-gate/usr/src/uts/common/sys/i2c/i2c.h (revision 32002227574cf0a435dc03de622191ca53724f0a)
1*32002227SRobert Mustacchi /*
2*32002227SRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*32002227SRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*32002227SRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*32002227SRobert Mustacchi  * 1.0 of the CDDL.
6*32002227SRobert Mustacchi  *
7*32002227SRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*32002227SRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*32002227SRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*32002227SRobert Mustacchi  */
11*32002227SRobert Mustacchi 
12*32002227SRobert Mustacchi /*
13*32002227SRobert Mustacchi  * Copyright 2025 Oxide Computer Company
14*32002227SRobert Mustacchi  */
15*32002227SRobert Mustacchi 
16*32002227SRobert Mustacchi #ifndef _SYS_I2C_I2C_H
17*32002227SRobert Mustacchi #define	_SYS_I2C_I2C_H
18*32002227SRobert Mustacchi 
19*32002227SRobert Mustacchi /*
20*32002227SRobert Mustacchi  * General i2c definitions that should be shared between both userland and the
21*32002227SRobert Mustacchi  * kernel. Kernel device drivers include <sys/i2c/controller.h>,
22*32002227SRobert Mustacchi  * <sys/i2c/mux.h>, or <sys/i2c/client.h> depending on the type of device they
23*32002227SRobert Mustacchi  * are. Userland should generally use <libi2c.h> or <sys/i2c/ui2c.h> if it's the
24*32002227SRobert Mustacchi  * implementation of libi2c.
25*32002227SRobert Mustacchi  */
26*32002227SRobert Mustacchi 
27*32002227SRobert Mustacchi #include <sys/stdint.h>
28*32002227SRobert Mustacchi #include <sys/stdbool.h>
29*32002227SRobert Mustacchi 
30*32002227SRobert Mustacchi #ifdef __cplusplus
31*32002227SRobert Mustacchi extern "C" {
32*32002227SRobert Mustacchi #endif
33*32002227SRobert Mustacchi 
34*32002227SRobert Mustacchi /*
35*32002227SRobert Mustacchi  * Different allowed values for I2C and SMBus speeds. In the future, we'll
36*32002227SRobert Mustacchi  * determine how I3C based options like different supported clock rates and
37*32002227SRobert Mustacchi  * SDR/DDR fit in here.
38*32002227SRobert Mustacchi  */
39*32002227SRobert Mustacchi typedef enum {
40*32002227SRobert Mustacchi 	/*
41*32002227SRobert Mustacchi 	 * 100 kHz Standard operation.
42*32002227SRobert Mustacchi 	 */
43*32002227SRobert Mustacchi 	I2C_SPEED_STD	= 1 << 0,
44*32002227SRobert Mustacchi 	/*
45*32002227SRobert Mustacchi 	 * 400 kHz Fast-mode operation.
46*32002227SRobert Mustacchi 	 */
47*32002227SRobert Mustacchi 	I2C_SPEED_FAST	= 1 << 1,
48*32002227SRobert Mustacchi 	/*
49*32002227SRobert Mustacchi 	 * 1000 MHz Fast-mode plus operation.
50*32002227SRobert Mustacchi 	 */
51*32002227SRobert Mustacchi 	I2C_SPEED_FPLUS	= 1 << 2,
52*32002227SRobert Mustacchi 	/*
53*32002227SRobert Mustacchi 	 * 3400 MHz High-speed mode operation.
54*32002227SRobert Mustacchi 	 */
55*32002227SRobert Mustacchi 	I2C_SPEED_HIGH	= 1 << 3,
56*32002227SRobert Mustacchi 	/*
57*32002227SRobert Mustacchi 	 * 5000 MHz Ultra-Fast mode operation.
58*32002227SRobert Mustacchi 	 */
59*32002227SRobert Mustacchi 	I2C_SPEED_ULTRA	= 1 << 4
60*32002227SRobert Mustacchi } i2c_speed_t;
61*32002227SRobert Mustacchi 
62*32002227SRobert Mustacchi /*
63*32002227SRobert Mustacchi  * Different types of controllers.
64*32002227SRobert Mustacchi  */
65*32002227SRobert Mustacchi typedef enum {
66*32002227SRobert Mustacchi 	I2C_CTRL_TYPE_I2C = 1,
67*32002227SRobert Mustacchi 	I2C_CTRL_TYPE_I3C,
68*32002227SRobert Mustacchi 	I2C_CTRL_TYPE_SMBUS
69*32002227SRobert Mustacchi } i2c_ctrl_type_t;
70*32002227SRobert Mustacchi 
71*32002227SRobert Mustacchi /*
72*32002227SRobert Mustacchi  * This represents the series of errors that can be generated by the various I2C
73*32002227SRobert Mustacchi  * APIs. These are grouped into several different units that cover behavior
74*32002227SRobert Mustacchi  * specific to the core (impacting everything) to properties, user-specific
75*32002227SRobert Mustacchi  * behavior, kernel driver clients, etc.
76*32002227SRobert Mustacchi  */
77*32002227SRobert Mustacchi typedef enum {
78*32002227SRobert Mustacchi 	I2C_CORE_E_OK	= 0,
79*32002227SRobert Mustacchi 	/*
80*32002227SRobert Mustacchi 	 * Indicates that the controller I/O failed. The reason is specified in
81*32002227SRobert Mustacchi 	 * the controller error.
82*32002227SRobert Mustacchi 	 */
83*32002227SRobert Mustacchi 	I2C_CORE_E_CONTROLLER,
84*32002227SRobert Mustacchi 	/*
85*32002227SRobert Mustacchi 	 * The following pair indicate that a given address class or value for
86*32002227SRobert Mustacchi 	 * an address within a valid address class are wrong.
87*32002227SRobert Mustacchi 	 */
88*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_ADDR_TYPE,
89*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_ADDR,
90*32002227SRobert Mustacchi 	/*
91*32002227SRobert Mustacchi 	 * This indicates that the requested address type, while valid, is not
92*32002227SRobert Mustacchi 	 * supported. For example, trying to send to a 10-bit address on a
93*32002227SRobert Mustacchi 	 * controller that does not support it.
94*32002227SRobert Mustacchi 	 */
95*32002227SRobert Mustacchi 	I2C_CORE_E_UNSUP_ADDR_TYPE,
96*32002227SRobert Mustacchi 	/*
97*32002227SRobert Mustacchi 	 * Indicates that the address in question is reserved by a corresponding
98*32002227SRobert Mustacchi 	 * specification.
99*32002227SRobert Mustacchi 	 */
100*32002227SRobert Mustacchi 	I2C_CORE_E_ADDR_RSVD,
101*32002227SRobert Mustacchi 	/*
102*32002227SRobert Mustacchi 	 * Indicates that the address in question is already in use and
103*32002227SRobert Mustacchi 	 * therefore cannot be used.
104*32002227SRobert Mustacchi 	 */
105*32002227SRobert Mustacchi 	I2C_CORE_E_ADDR_IN_USE,
106*32002227SRobert Mustacchi 	/*
107*32002227SRobert Mustacchi 	 * Indicates that the address could be used, but it has exceeded the
108*32002227SRobert Mustacchi 	 * per-address usage count. This usually represents a place where the
109*32002227SRobert Mustacchi 	 * kernel can be improved.
110*32002227SRobert Mustacchi 	 */
111*32002227SRobert Mustacchi 	I2C_CORE_E_ADDR_REFCNT,
112*32002227SRobert Mustacchi 	/*
113*32002227SRobert Mustacchi 	 * Indicates that there is no device with the specified address.
114*32002227SRobert Mustacchi 	 */
115*32002227SRobert Mustacchi 	I2C_CORE_E_UNKNOWN_ADDR,
116*32002227SRobert Mustacchi 	/*
117*32002227SRobert Mustacchi 	 * Indicates that the request type can't be translated to something the
118*32002227SRobert Mustacchi 	 * underlying controller actually supports. For example, this would
119*32002227SRobert Mustacchi 	 * cover trying to translate certain kinds of I2C requests to an SMBus
120*32002227SRobert Mustacchi 	 * controller that supports limited types of operations or vice versa.
121*32002227SRobert Mustacchi 	 */
122*32002227SRobert Mustacchi 	I2C_CORE_E_CANT_XLATE_REQ,
123*32002227SRobert Mustacchi 	/*
124*32002227SRobert Mustacchi 	 * This indicates that a request had neither a read nor a write and
125*32002227SRobert Mustacchi 	 * therefore cannot continue. This constraint on I/O may be lifted in
126*32002227SRobert Mustacchi 	 * the future.
127*32002227SRobert Mustacchi 	 */
128*32002227SRobert Mustacchi 	I2C_CORE_E_NEED_READ_OR_WRITE,
129*32002227SRobert Mustacchi 	/*
130*32002227SRobert Mustacchi 	 * These indicate that invalid flags values, an invalid read length, or
131*32002227SRobert Mustacchi 	 * invalid write length was encountered.
132*32002227SRobert Mustacchi 	 */
133*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_I2C_REQ_FLAGS,
134*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_I2C_REQ_READ_LEN,
135*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_I2C_REQ_WRITE_LEN,
136*32002227SRobert Mustacchi 	/*
137*32002227SRobert Mustacchi 	 * These indicate similar situations in the SMBus request field.
138*32002227SRobert Mustacchi 	 */
139*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_SMBUS_REQ_FLAGS,
140*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_SMBUS_READ_LEN,
141*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_SMBUS_WRITE_LEN,
142*32002227SRobert Mustacchi 	I2C_CORE_E_BAD_SMBUS_OP,
143*32002227SRobert Mustacchi 	/*
144*32002227SRobert Mustacchi 	 * Indicates that the controller does not support the requested SMBus
145*32002227SRobert Mustacchi 	 * operation.
146*32002227SRobert Mustacchi 	 */
147*32002227SRobert Mustacchi 	I2C_CORE_E_UNSUP_SMBUS_OP,
148*32002227SRobert Mustacchi 	/*
149*32002227SRobert Mustacchi 	 * Indicates that the controller is already owned by someone and the
150*32002227SRobert Mustacchi 	 * caller asked not to block.
151*32002227SRobert Mustacchi 	 */
152*32002227SRobert Mustacchi 	I2C_CORE_E_LOCK_WOULD_BLOCK,
153*32002227SRobert Mustacchi 	/*
154*32002227SRobert Mustacchi 	 * Indicates that the caller took a signal while waiting to acquire the
155*32002227SRobert Mustacchi 	 * controller.
156*32002227SRobert Mustacchi 	 */
157*32002227SRobert Mustacchi 	I2C_CORE_E_LOCK_WAIT_SIGNAL,
158*32002227SRobert Mustacchi 	/*
159*32002227SRobert Mustacchi 	 * Indicates that a passed in nvlist was larger than the maximum value.
160*32002227SRobert Mustacchi 	 */
161*32002227SRobert Mustacchi 	I2C_IOCTL_E_NVL_TOO_BIG = 0x1000,
162*32002227SRobert Mustacchi 	/*
163*32002227SRobert Mustacchi 	 * Indicates that the nvlist was not parseable.
164*32002227SRobert Mustacchi 	 */
165*32002227SRobert Mustacchi 	I2C_IOCTL_E_NVL_INVALID,
166*32002227SRobert Mustacchi 	/*
167*32002227SRobert Mustacchi 	 * Indicates that the nvlist contained missing keys, keys that we don't
168*32002227SRobert Mustacchi 	 * know how to handle, and keys that are the wrong type. If this gets
169*32002227SRobert Mustacchi 	 * much more complex, the interface should change to the kgpio error
170*32002227SRobert Mustacchi 	 * style where there is an additional nvlist there.
171*32002227SRobert Mustacchi 	 */
172*32002227SRobert Mustacchi 	I2C_IOCTL_E_NVL_KEY_MISSING,
173*32002227SRobert Mustacchi 	I2C_IOCTL_E_NVL_KEY_UNKNOWN,
174*32002227SRobert Mustacchi 	I2C_IOCTL_E_NVL_KEY_BAD_TYPE,
175*32002227SRobert Mustacchi 	/*
176*32002227SRobert Mustacchi 	 * Indicates that a pointer to user data inside of an ioctl (not the
177*32002227SRobert Mustacchi 	 * overall ioctl itself) was not valid and generated a fault.
178*32002227SRobert Mustacchi 	 */
179*32002227SRobert Mustacchi 	I2C_IOCTL_E_BAD_USER_DATA,
180*32002227SRobert Mustacchi 	/*
181*32002227SRobert Mustacchi 	 * Indicates that there was no kernel memory available for the request.
182*32002227SRobert Mustacchi 	 */
183*32002227SRobert Mustacchi 	I2C_IOCTL_E_NO_KERN_MEM,
184*32002227SRobert Mustacchi 	/*
185*32002227SRobert Mustacchi 	 * Indicates that a string that is being used for a device name or
186*32002227SRobert Mustacchi 	 * compatible array contains illegal characters or is too long.
187*32002227SRobert Mustacchi 	 */
188*32002227SRobert Mustacchi 	I2C_IOCTL_E_BAD_DEV_NAME,
189*32002227SRobert Mustacchi 	/*
190*32002227SRobert Mustacchi 	 * Indicates that the length of the compatible range is longer than the
191*32002227SRobert Mustacchi 	 * system will allow to be set.
192*32002227SRobert Mustacchi 	 */
193*32002227SRobert Mustacchi 	I2C_IOCTL_E_COMPAT_LEN_RANGE,
194*32002227SRobert Mustacchi 	/*
195*32002227SRobert Mustacchi 	 * Indicates that something went wrong with trying to deal with nexus
196*32002227SRobert Mustacchi 	 * related operations on a child.
197*32002227SRobert Mustacchi 	 */
198*32002227SRobert Mustacchi 	I2C_IOCTL_E_NEXUS,
199*32002227SRobert Mustacchi 	/*
200*32002227SRobert Mustacchi 	 * Indicates that a nexus operations was attempted while trying to hold
201*32002227SRobert Mustacchi 	 * a bus lock.
202*32002227SRobert Mustacchi 	 */
203*32002227SRobert Mustacchi 	I2C_IOCTL_E_NO_BUS_LOCK_NEXUS,
204*32002227SRobert Mustacchi 	/*
205*32002227SRobert Mustacchi 	 * Indicates that an ioctl operation could not be started because the
206*32002227SRobert Mustacchi 	 * client in question already has one in flight that requires the
207*32002227SRobert Mustacchi 	 * controller lock.
208*32002227SRobert Mustacchi 	 */
209*32002227SRobert Mustacchi 	I2C_IOCTL_E_IN_PROGRESS,
210*32002227SRobert Mustacchi 	/*
211*32002227SRobert Mustacchi 	 * Indicates that the passed dev_info_t does not correspond to an i2c
212*32002227SRobert Mustacchi 	 * device.
213*32002227SRobert Mustacchi 	 */
214*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_DIP = 0x2000,
215*32002227SRobert Mustacchi 	/*
216*32002227SRobert Mustacchi 	 * Indicates that the regs[] index is invalid for the device.
217*32002227SRobert Mustacchi 	 */
218*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_IDX,
219*32002227SRobert Mustacchi 	/*
220*32002227SRobert Mustacchi 	 * Indicates that the specific set of flags are invalid.
221*32002227SRobert Mustacchi 	 */
222*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_CLAIM_FLAGS,
223*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_IO_FLAGS,
224*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_LOCK_FLAGS,
225*32002227SRobert Mustacchi 	/*
226*32002227SRobert Mustacchi 	 * Indicates that the caller was interrupted while trying to get access
227*32002227SRobert Mustacchi 	 * to the client for I/O.
228*32002227SRobert Mustacchi 	 */
229*32002227SRobert Mustacchi 	I2C_CLIENT_E_SIGNAL,
230*32002227SRobert Mustacchi 	/*
231*32002227SRobert Mustacchi 	 * Thee indicate that there are invalid values in the various register
232*32002227SRobert Mustacchi 	 * access attributes.
233*32002227SRobert Mustacchi 	 */
234*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_VERS,
235*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_FLAGS,
236*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_RLEN,
237*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_ALEN,
238*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_ENDIAN,
239*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ATTR_MAX,
240*32002227SRobert Mustacchi 	/*
241*32002227SRobert Mustacchi 	 * Indicates that while the register attributes are supported, the
242*32002227SRobert Mustacchi 	 * underlying hardware cannot support them.
243*32002227SRobert Mustacchi 	 */
244*32002227SRobert Mustacchi 	I2C_CLIENT_E_REG_ALEN_UNSUP_BY_CTRL,
245*32002227SRobert Mustacchi 	/*
246*32002227SRobert Mustacchi 	 * These indicate that I2C register operations were passed invalid
247*32002227SRobert Mustacchi 	 * values or that these values would lead to an overflow.
248*32002227SRobert Mustacchi 	 */
249*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_ADDR,
250*32002227SRobert Mustacchi 	I2C_CLIENT_E_BAD_REG_COUNT,
251*32002227SRobert Mustacchi 	I2C_CLIENT_E_REG_ADDR_OVERFLOW,
252*32002227SRobert Mustacchi 	I2C_CLIENT_E_REG_IO_TOO_LARGE,
253*32002227SRobert Mustacchi 	/*
254*32002227SRobert Mustacchi 	 * Indicates that the size in bytes is a partial number of registers.
255*32002227SRobert Mustacchi 	 */
256*32002227SRobert Mustacchi 	I2C_CLIENT_E_PARTIAL_REG,
257*32002227SRobert Mustacchi 	/*
258*32002227SRobert Mustacchi 	 * Indicates that the client has tried to claim a shared address, but
259*32002227SRobert Mustacchi 	 * they actually own the address directly.
260*32002227SRobert Mustacchi 	 */
261*32002227SRobert Mustacchi 	I2C_CLIENT_E_CLAIM_OWNED_ADDR,
262*32002227SRobert Mustacchi 	/*
263*32002227SRobert Mustacchi 	 * These two indicate that the property is unsupported by the device or
264*32002227SRobert Mustacchi 	 * a property ID that is unknown by the system.
265*32002227SRobert Mustacchi 	 */
266*32002227SRobert Mustacchi 	I2C_PROP_E_UNSUP = 0x3000,
267*32002227SRobert Mustacchi 	I2C_PROP_E_UNKNOWN,
268*32002227SRobert Mustacchi 	/*
269*32002227SRobert Mustacchi 	 * Indicates that the property can't be written to because it is
270*32002227SRobert Mustacchi 	 * read-only.
271*32002227SRobert Mustacchi 	 */
272*32002227SRobert Mustacchi 	I2C_PROP_E_READ_ONLY,
273*32002227SRobert Mustacchi 	/*
274*32002227SRobert Mustacchi 	 * Indicates that the property buffer is too small. The second indicates
275*32002227SRobert Mustacchi 	 * that the property buffer is too large and doesn't make sense for the
276*32002227SRobert Mustacchi 	 * property. The latter is only relevant when setting a property. The
277*32002227SRobert Mustacchi 	 * former can be triggered in all property interfaces.
278*32002227SRobert Mustacchi 	 */
279*32002227SRobert Mustacchi 	I2C_PROP_E_SMALL_BUF,
280*32002227SRobert Mustacchi 	I2C_PROP_E_TOO_BIG_BUF,
281*32002227SRobert Mustacchi 	/*
282*32002227SRobert Mustacchi 	 * Indicates that the property value is invalid.
283*32002227SRobert Mustacchi 	 */
284*32002227SRobert Mustacchi 	I2C_PROP_E_BAD_VAL,
285*32002227SRobert Mustacchi 	/*
286*32002227SRobert Mustacchi 	 * Indicates that the controller doesn't support setting properties.
287*32002227SRobert Mustacchi 	 */
288*32002227SRobert Mustacchi 	I2C_PROP_E_SET_UNSUP,
289*32002227SRobert Mustacchi 	/*
290*32002227SRobert Mustacchi 	 * Indicates that the mux flag is unknown.
291*32002227SRobert Mustacchi 	 */
292*32002227SRobert Mustacchi 	I2C_MUX_E_BAD_FLAG = 0x4000
293*32002227SRobert Mustacchi } i2c_errno_t;
294*32002227SRobert Mustacchi 
295*32002227SRobert Mustacchi /*
296*32002227SRobert Mustacchi  * These represent errors that the controller generates and/or detects while
297*32002227SRobert Mustacchi  * attempting to perform I/O. Some controllers provide relatively rich
298*32002227SRobert Mustacchi  * diagnostics as to what went wrong. Others, provide only generic classes of
299*32002227SRobert Mustacchi  * errors. Try to use the most specific error possible.
300*32002227SRobert Mustacchi  */
301*32002227SRobert Mustacchi typedef enum {
302*32002227SRobert Mustacchi 	I2C_CTRL_E_OK = 0,
303*32002227SRobert Mustacchi 	/*
304*32002227SRobert Mustacchi 	 * This is a generic class that represents something happened internal
305*32002227SRobert Mustacchi 	 * to the controller. In general, this should be used sparingly and only
306*32002227SRobert Mustacchi 	 * for something that only makes semantic sense on a single controller.
307*32002227SRobert Mustacchi 	 * This should not be a property of something related to I2C/SMBus/I3C
308*32002227SRobert Mustacchi 	 * directly.
309*32002227SRobert Mustacchi 	 */
310*32002227SRobert Mustacchi 	I2C_CTRL_E_INTERNAL,
311*32002227SRobert Mustacchi 	/*
312*32002227SRobert Mustacchi 	 * This is a variant of the above that indicates a driver programming
313*32002227SRobert Mustacchi 	 * error violated a controller condition.
314*32002227SRobert Mustacchi 	 */
315*32002227SRobert Mustacchi 	I2C_CTRL_E_DRIVER,
316*32002227SRobert Mustacchi 	/*
317*32002227SRobert Mustacchi 	 * Indicates that the controller was given a type of command that it
318*32002227SRobert Mustacchi 	 * does not support. For example, an SMBus 1.0 controller given an SMBus
319*32002227SRobert Mustacchi 	 * 2.0 command. The framework generally is the only tying that will
320*32002227SRobert Mustacchi 	 * return this error as drivers should not have to encounter them.
321*32002227SRobert Mustacchi 	 */
322*32002227SRobert Mustacchi 	I2C_CTRL_E_UNSUP_CMD,
323*32002227SRobert Mustacchi 	/*
324*32002227SRobert Mustacchi 	 * Indicates that prior to trying to perform the operation, the
325*32002227SRobert Mustacchi 	 * controller detected that the bus was busy and after a timeout was
326*32002227SRobert Mustacchi 	 * unable to get control of the bus.
327*32002227SRobert Mustacchi 	 */
328*32002227SRobert Mustacchi 	I2C_CTRL_E_BUS_BUSY,
329*32002227SRobert Mustacchi 	/*
330*32002227SRobert Mustacchi 	 * This error indicates that there was a no acknowledgement condition.
331*32002227SRobert Mustacchi 	 * This should be used both for 7-bit and 10-bit cases. Similarly, for
332*32002227SRobert Mustacchi 	 * now this should also be used for cases where no one acknowledges a
333*32002227SRobert Mustacchi 	 * general call.
334*32002227SRobert Mustacchi 	 */
335*32002227SRobert Mustacchi 	I2C_CTRL_E_ADDR_NACK,
336*32002227SRobert Mustacchi 	/*
337*32002227SRobert Mustacchi 	 * This is a variant on the address NACK. This is used when the address
338*32002227SRobert Mustacchi 	 * has been acknowledged, but subsequent data has not been. Some devices
339*32002227SRobert Mustacchi 	 * may not be able to make the distinction. If they cannot, use the
340*32002227SRobert Mustacchi 	 * catch-all NACK below.
341*32002227SRobert Mustacchi 	 */
342*32002227SRobert Mustacchi 	I2C_CTRL_E_DATA_NACK,
343*32002227SRobert Mustacchi 	/*
344*32002227SRobert Mustacchi 	 * This is a case where no NACK occurred, but the controller cannot be
345*32002227SRobert Mustacchi 	 * more specific as to where in the process it occurred.
346*32002227SRobert Mustacchi 	 */
347*32002227SRobert Mustacchi 	I2C_CTRL_E_NACK,
348*32002227SRobert Mustacchi 	/*
349*32002227SRobert Mustacchi 	 * Indicates that the controller lost arbitration on the bus. A common
350*32002227SRobert Mustacchi 	 * cause for this is a data collision.
351*32002227SRobert Mustacchi 	 */
352*32002227SRobert Mustacchi 	I2C_CTRL_E_ARB_LOST,
353*32002227SRobert Mustacchi 	/*
354*32002227SRobert Mustacchi 	 * Indicates that a device incorrectly issued an ACK when it was not
355*32002227SRobert Mustacchi 	 * expected in the operation.
356*32002227SRobert Mustacchi 	 */
357*32002227SRobert Mustacchi 	I2C_CTRL_E_BAD_ACK,
358*32002227SRobert Mustacchi 	/*
359*32002227SRobert Mustacchi 	 * Indicates that the controller failed to successfully finish
360*32002227SRobert Mustacchi 	 * transmitting the command within the default request timeout. This
361*32002227SRobert Mustacchi 	 * results in the controller aborting the command. Callers cannot assume
362*32002227SRobert Mustacchi 	 * anything about the number of bytes that made it out onto the bus.
363*32002227SRobert Mustacchi 	 */
364*32002227SRobert Mustacchi 	I2C_CTRL_E_REQ_TO,
365*32002227SRobert Mustacchi 	/*
366*32002227SRobert Mustacchi 	 * Indicates that an SMBus device returned a block read length that
367*32002227SRobert Mustacchi 	 * could not be supported by the device or is illegal according to the
368*32002227SRobert Mustacchi 	 * specification. For example, a 0 byte length or a length exceeding 32
369*32002227SRobert Mustacchi 	 * bytes for an SMBus 2.0 controller.
370*32002227SRobert Mustacchi 	 */
371*32002227SRobert Mustacchi 	I2C_CTRL_E_BAD_SMBUS_RLEN,
372*32002227SRobert Mustacchi 	/*
373*32002227SRobert Mustacchi 	 * Indicates that an SMBus device held the clock low for too long to
374*32002227SRobert Mustacchi 	 * effectively trime out the transaction. This is different from the
375*32002227SRobert Mustacchi 	 * request timeout as it indicates something the target did.
376*32002227SRobert Mustacchi 	 */
377*32002227SRobert Mustacchi 	I2C_CTRL_E_SMBUS_CLOCK_LOW
378*32002227SRobert Mustacchi } i2c_ctrl_error_t;
379*32002227SRobert Mustacchi 
380*32002227SRobert Mustacchi typedef struct {
381*32002227SRobert Mustacchi 	i2c_errno_t i2c_error;
382*32002227SRobert Mustacchi 	i2c_ctrl_error_t i2c_ctrl;
383*32002227SRobert Mustacchi } i2c_error_t;
384*32002227SRobert Mustacchi 
385*32002227SRobert Mustacchi /*
386*32002227SRobert Mustacchi  * General maximum length for an i2c related name, including the NUL.
387*32002227SRobert Mustacchi  */
388*32002227SRobert Mustacchi #define	I2C_NAME_MAX	32
389*32002227SRobert Mustacchi 
390*32002227SRobert Mustacchi typedef enum i2c_addr_type {
391*32002227SRobert Mustacchi 	I2C_ADDR_7BIT = 0,
392*32002227SRobert Mustacchi 	I2C_ADDR_10BIT
393*32002227SRobert Mustacchi } i2c_addr_type_t;
394*32002227SRobert Mustacchi 
395*32002227SRobert Mustacchi /*
396*32002227SRobert Mustacchi  * This represents the general form of our i2c addresses and what the nexus will
397*32002227SRobert Mustacchi  * give client devices and drivers in the form of regs[].
398*32002227SRobert Mustacchi  */
399*32002227SRobert Mustacchi typedef struct i2c_addr {
400*32002227SRobert Mustacchi 	uint16_t ia_type;
401*32002227SRobert Mustacchi 	uint16_t ia_addr;
402*32002227SRobert Mustacchi } i2c_addr_t;
403*32002227SRobert Mustacchi 
404*32002227SRobert Mustacchi /*
405*32002227SRobert Mustacchi  * This indicates where an address came from.
406*32002227SRobert Mustacchi  */
407*32002227SRobert Mustacchi typedef enum i2c_addr_source {
408*32002227SRobert Mustacchi 	/*
409*32002227SRobert Mustacchi 	 * Indicates that this came from the devices reg[] array. It was either
410*32002227SRobert Mustacchi 	 * put there as part of discovering information about the system by the
411*32002227SRobert Mustacchi 	 * platform (e.g. ACPI, device tree, etc.) or based on a user's specific
412*32002227SRobert Mustacchi 	 * device creation.
413*32002227SRobert Mustacchi 	 */
414*32002227SRobert Mustacchi 	I2C_ADDR_SOURCE_REG = 1,
415*32002227SRobert Mustacchi 	/*
416*32002227SRobert Mustacchi 	 * Indicates that the address was one that the driver claimed.
417*32002227SRobert Mustacchi 	 */
418*32002227SRobert Mustacchi 	I2C_ADDR_SOURCE_CLAIMED,
419*32002227SRobert Mustacchi 	/*
420*32002227SRobert Mustacchi 	 * Indicates that the address was one that the driver claimed and is
421*32002227SRobert Mustacchi 	 * shared across multiple instances.
422*32002227SRobert Mustacchi 	 */
423*32002227SRobert Mustacchi 	I2C_ADDR_SOURCE_SHARED
424*32002227SRobert Mustacchi } i2c_addr_source_t;
425*32002227SRobert Mustacchi 
426*32002227SRobert Mustacchi /*
427*32002227SRobert Mustacchi  * Well-known and other special addresses. I2C reserves several addresses for
428*32002227SRobert Mustacchi  * special purposes. SMBus has a more extensive of nominal assignments, but
429*32002227SRobert Mustacchi  * things definitely stomp in that space.
430*32002227SRobert Mustacchi  */
431*32002227SRobert Mustacchi typedef enum {
432*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_GEN_CALL	= 0x00,
433*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_C_BUS	= 0x01,
434*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_DIFF_BUS	= 0x02,
435*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_FUTURE	= 0x03,
436*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_HS_0	= 0x04,
437*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_HS_1	= 0x05,
438*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_HS_2	= 0x06,
439*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_HS_3	= 0x07,
440*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_10B_0	= 0x78,
441*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_10B_1	= 0x79,
442*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_10B_2	= 0x7a,
443*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_10B_3	= 0x7b,
444*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_DID_0	= 0x7c,
445*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_DID_1	= 0x7d,
446*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_DID_2	= 0x7e,
447*32002227SRobert Mustacchi 	I2C_RSVD_ADDR_DID_3	= 0x7f
448*32002227SRobert Mustacchi } i2c_rsvd_addr_t;
449*32002227SRobert Mustacchi 
450*32002227SRobert Mustacchi /*
451*32002227SRobert Mustacchi  * SMBus 2.0 controllers have a maximum byte size of 32 bytes. SMBus 3.0
452*32002227SRobert Mustacchi  * increases that to a maximum of 255 bytes. As such we size our maximum request
453*32002227SRobert Mustacchi  * sizes at 256 bytes in our structures.
454*32002227SRobert Mustacchi  */
455*32002227SRobert Mustacchi #define	SMBUS_V2_MAX_BLOCK	32
456*32002227SRobert Mustacchi #define	SMBUS_V3_MAX_BLOCK	255
457*32002227SRobert Mustacchi #define	I2C_REQ_MAX		256
458*32002227SRobert Mustacchi 
459*32002227SRobert Mustacchi typedef enum smbus_op {
460*32002227SRobert Mustacchi 	SMBUS_OP_QUICK_COMMAND,
461*32002227SRobert Mustacchi 	SMBUS_OP_SEND_BYTE,
462*32002227SRobert Mustacchi 	SMBUS_OP_RECV_BYTE,
463*32002227SRobert Mustacchi 	SMBUS_OP_WRITE_BYTE,
464*32002227SRobert Mustacchi 	SMBUS_OP_READ_BYTE,
465*32002227SRobert Mustacchi 	SMBUS_OP_WRITE_WORD,
466*32002227SRobert Mustacchi 	SMBUS_OP_READ_WORD,
467*32002227SRobert Mustacchi 	SMBUS_OP_PROCESS_CALL,
468*32002227SRobert Mustacchi 	SMBUS_OP_WRITE_BLOCK,
469*32002227SRobert Mustacchi 	SMBUS_OP_READ_BLOCK,
470*32002227SRobert Mustacchi 	/* Added in SMBUS 2.0 */
471*32002227SRobert Mustacchi 	SMBUS_OP_HOST_NOTIFY,
472*32002227SRobert Mustacchi 	SMBUS_OP_BLOCK_PROCESS_CALL,
473*32002227SRobert Mustacchi 	/* Added in SMBUS 3.x */
474*32002227SRobert Mustacchi 	SMBUS_OP_WRITE_U32,
475*32002227SRobert Mustacchi 	SMBUS_OP_READ_U32,
476*32002227SRobert Mustacchi 	SMBUS_OP_WRITE_U64,
477*32002227SRobert Mustacchi 	SMBUS_OP_READ_U64,
478*32002227SRobert Mustacchi 	/* I2C Compatibility */
479*32002227SRobert Mustacchi 	SMBUS_OP_I2C_WRITE_BLOCK,
480*32002227SRobert Mustacchi 	SMBUS_OP_I2C_READ_BLOCK
481*32002227SRobert Mustacchi } smbus_op_t;
482*32002227SRobert Mustacchi 
483*32002227SRobert Mustacchi typedef enum {
484*32002227SRobert Mustacchi 	/*
485*32002227SRobert Mustacchi 	 * Indicates that regardless of whether or not interrupts are supported,
486*32002227SRobert Mustacchi 	 * this request should be polled.
487*32002227SRobert Mustacchi 	 */
488*32002227SRobert Mustacchi 	I2C_IO_REQ_F_POLL		= 1 << 0,
489*32002227SRobert Mustacchi 	/*
490*32002227SRobert Mustacchi 	 * Indicates that this zero-byte I/O quick command is a write. If this
491*32002227SRobert Mustacchi 	 * flag is not set then a quick command is a read.
492*32002227SRobert Mustacchi 	 */
493*32002227SRobert Mustacchi 	I2C_IO_REQ_F_QUICK_WRITE	= 1 << 1,
494*32002227SRobert Mustacchi } i2c_req_flags_t;
495*32002227SRobert Mustacchi 
496*32002227SRobert Mustacchi typedef struct smbus_req {
497*32002227SRobert Mustacchi 	i2c_error_t smbr_error;
498*32002227SRobert Mustacchi 	smbus_op_t smbr_op;
499*32002227SRobert Mustacchi 	i2c_req_flags_t smbr_flags;
500*32002227SRobert Mustacchi 	i2c_addr_t smbr_addr;
501*32002227SRobert Mustacchi 	uint16_t smbr_wlen;
502*32002227SRobert Mustacchi 	uint16_t smbr_rlen;
503*32002227SRobert Mustacchi 	uint8_t smbr_cmd;
504*32002227SRobert Mustacchi 	uint8_t smbr_wdata[I2C_REQ_MAX];
505*32002227SRobert Mustacchi 	uint8_t smbr_rdata[I2C_REQ_MAX];
506*32002227SRobert Mustacchi } smbus_req_t;
507*32002227SRobert Mustacchi 
508*32002227SRobert Mustacchi typedef struct i2c_req {
509*32002227SRobert Mustacchi 	i2c_error_t ir_error;
510*32002227SRobert Mustacchi 	i2c_req_flags_t ir_flags;
511*32002227SRobert Mustacchi 	i2c_addr_t ir_addr;
512*32002227SRobert Mustacchi 	uint16_t ir_wlen;
513*32002227SRobert Mustacchi 	uint16_t ir_rlen;
514*32002227SRobert Mustacchi 	uint8_t ir_wdata[I2C_REQ_MAX];
515*32002227SRobert Mustacchi 	uint8_t ir_rdata[I2C_REQ_MAX];
516*32002227SRobert Mustacchi } i2c_req_t;
517*32002227SRobert Mustacchi 
518*32002227SRobert Mustacchi typedef enum i2c_prop_type {
519*32002227SRobert Mustacchi 	/*
520*32002227SRobert Mustacchi 	 * A property that is a standard, scalar uint32_t.
521*32002227SRobert Mustacchi 	 */
522*32002227SRobert Mustacchi 	I2C_PROP_TYPE_U32,
523*32002227SRobert Mustacchi 	/*
524*32002227SRobert Mustacchi 	 * A property that fits in a uint32_t, but represents a bitfield of
525*32002227SRobert Mustacchi 	 * values.
526*32002227SRobert Mustacchi 	 */
527*32002227SRobert Mustacchi 	I2C_PROP_TYPE_BIT32
528*32002227SRobert Mustacchi } i2c_prop_type_t;
529*32002227SRobert Mustacchi 
530*32002227SRobert Mustacchi typedef enum i2c_prop_perm {
531*32002227SRobert Mustacchi 	I2C_PROP_PERM_RO,
532*32002227SRobert Mustacchi 	I2C_PROP_PERM_RW
533*32002227SRobert Mustacchi } i2c_prop_perm_t;
534*32002227SRobert Mustacchi 
535*32002227SRobert Mustacchi typedef struct i2c_prop_u32_range {
536*32002227SRobert Mustacchi 	uint32_t ipur_min;
537*32002227SRobert Mustacchi 	uint32_t ipur_max;
538*32002227SRobert Mustacchi } i2c_prop_u32_range_t;
539*32002227SRobert Mustacchi 
540*32002227SRobert Mustacchi typedef union i2c_prop_val_range {
541*32002227SRobert Mustacchi 	uint32_t ipvr_bit32;
542*32002227SRobert Mustacchi 	i2c_prop_u32_range_t ipvr_u32;
543*32002227SRobert Mustacchi } i2c_prop_val_range_t;
544*32002227SRobert Mustacchi 
545*32002227SRobert Mustacchi typedef struct i2c_prop_range {
546*32002227SRobert Mustacchi 	uint32_t ipr_count;
547*32002227SRobert Mustacchi 	i2c_prop_type_t ipr_type;
548*32002227SRobert Mustacchi 	i2c_prop_val_range_t ipr_range[];
549*32002227SRobert Mustacchi } i2c_prop_range_t;
550*32002227SRobert Mustacchi 
551*32002227SRobert Mustacchi /*
552*32002227SRobert Mustacchi  * This enumeration contains a list of the properties that are supported.
553*32002227SRobert Mustacchi  *
554*32002227SRobert Mustacchi  * In earlier designs we had an initial set of properties for setup and hold
555*32002227SRobert Mustacchi  * time related aspects, but controllers don't really have a uniform design
556*32002227SRobert Mustacchi  * here. Some offer different values for RX and TX. Some offer only a single
557*32002227SRobert Mustacchi  * value.
558*32002227SRobert Mustacchi  */
559*32002227SRobert Mustacchi typedef enum i2c_prop {
560*32002227SRobert Mustacchi 	/*
561*32002227SRobert Mustacchi 	 * This is a uint32_t that is covered by the i2c_speed_t.
562*32002227SRobert Mustacchi 	 */
563*32002227SRobert Mustacchi 	I2C_PROP_BUS_SPEED	= 0,
564*32002227SRobert Mustacchi 	/*
565*32002227SRobert Mustacchi 	 * This is a uint32_t that indicates the number of ports the device has.
566*32002227SRobert Mustacchi 	 */
567*32002227SRobert Mustacchi 	I2C_PROP_NPORTS,
568*32002227SRobert Mustacchi 	/*
569*32002227SRobert Mustacchi 	 * This indicates the controller's type, which is covered by the
570*32002227SRobert Mustacchi 	 * i2c_ctrl_type_t.
571*32002227SRobert Mustacchi 	 */
572*32002227SRobert Mustacchi 	I2C_PROP_TYPE,
573*32002227SRobert Mustacchi 	/*
574*32002227SRobert Mustacchi 	 * SMBus operations that are supported by the controller. This is a
575*32002227SRobert Mustacchi 	 * uint32_t that covers smbus_prop_op_t.
576*32002227SRobert Mustacchi 	 */
577*32002227SRobert Mustacchi 	SMBUS_PROP_SUP_OPS,
578*32002227SRobert Mustacchi 	/*
579*32002227SRobert Mustacchi 	 * Maximum sizes that a controller can support for performing different
580*32002227SRobert Mustacchi 	 * kinds of I/O. We expect that most I2C controllers will only support a
581*32002227SRobert Mustacchi 	 *
582*32002227SRobert Mustacchi 	 * single read/write buffer. For SMBus controllers, they should specify
583*32002227SRobert Mustacchi 	 * the maximum block size. This covers block reads, writes, and calls.
584*32002227SRobert Mustacchi 	 * If a controller supports an I2C mode with a different maximum, then
585*32002227SRobert Mustacchi 	 * it can additionally specify the I2c properties.
586*32002227SRobert Mustacchi 	 */
587*32002227SRobert Mustacchi 	I2C_PROP_MAX_READ,
588*32002227SRobert Mustacchi 	I2C_PROP_MAX_WRITE,
589*32002227SRobert Mustacchi 	SMBUS_PROP_MAX_BLOCK,
590*32002227SRobert Mustacchi 	/*
591*32002227SRobert Mustacchi 	 * Properties for different timing parameters in I2C devices. These are
592*32002227SRobert Mustacchi 	 * all uint32_t values generally in clock cycles.
593*32002227SRobert Mustacchi 	 */
594*32002227SRobert Mustacchi 	I2C_PROP_STD_SCL_HIGH,
595*32002227SRobert Mustacchi 	I2C_PROP_STD_SCL_LOW,
596*32002227SRobert Mustacchi 	I2C_PROP_FAST_SCL_HIGH,
597*32002227SRobert Mustacchi 	I2C_PROP_FAST_SCL_LOW,
598*32002227SRobert Mustacchi 	I2C_PROP_HIGH_SCL_HIGH,
599*32002227SRobert Mustacchi 	I2C_PROP_HIGH_SCL_LOW
600*32002227SRobert Mustacchi } i2c_prop_t;
601*32002227SRobert Mustacchi 
602*32002227SRobert Mustacchi typedef enum smbus_prop_op {
603*32002227SRobert Mustacchi 	SMBUS_PROP_OP_QUICK_COMMAND = 1 << SMBUS_OP_QUICK_COMMAND,
604*32002227SRobert Mustacchi 	SMBUS_PROP_OP_SEND_BYTE = 1 << SMBUS_OP_SEND_BYTE,
605*32002227SRobert Mustacchi 	SMBUS_PROP_OP_RECV_BYTE = 1 << SMBUS_OP_RECV_BYTE,
606*32002227SRobert Mustacchi 	SMBUS_PROP_OP_WRITE_BYTE = 1 << SMBUS_OP_WRITE_BYTE,
607*32002227SRobert Mustacchi 	SMBUS_PROP_OP_READ_BYTE = 1 << SMBUS_OP_READ_BYTE,
608*32002227SRobert Mustacchi 	SMBUS_PROP_OP_WRITE_WORD = 1 << SMBUS_OP_WRITE_WORD,
609*32002227SRobert Mustacchi 	SMBUS_PROP_OP_READ_WORD = 1 << SMBUS_OP_READ_WORD,
610*32002227SRobert Mustacchi 	SMBUS_PROP_OP_PROCESS_CALL = 1 << SMBUS_OP_PROCESS_CALL,
611*32002227SRobert Mustacchi 	SMBUS_PROP_OP_WRITE_BLOCK = 1 << SMBUS_OP_WRITE_BLOCK,
612*32002227SRobert Mustacchi 	SMBUS_PROP_OP_READ_BLOCK = 1 << SMBUS_OP_READ_BLOCK,
613*32002227SRobert Mustacchi 	SMBUS_PROP_OP_HOST_NOTIFY = 1 << SMBUS_OP_HOST_NOTIFY,
614*32002227SRobert Mustacchi 	SMBUS_PROP_OP_BLOCK_PROCESS_CALL = 1 << SMBUS_OP_BLOCK_PROCESS_CALL,
615*32002227SRobert Mustacchi 	SMBUS_PROP_OP_WRITE_U32 = 1 << SMBUS_OP_WRITE_U32,
616*32002227SRobert Mustacchi 	SMBUS_PROP_OP_READ_U32 = 1 << SMBUS_OP_READ_U32,
617*32002227SRobert Mustacchi 	SMBUS_PROP_OP_WRITE_U64 = 1 << SMBUS_OP_WRITE_U64,
618*32002227SRobert Mustacchi 	SMBUS_PROP_OP_READ_U64 = 1 << SMBUS_OP_READ_U64,
619*32002227SRobert Mustacchi 	SMBUS_PROP_OP_I2C_WRITE_BLOCK = 1 << SMBUS_OP_I2C_WRITE_BLOCK,
620*32002227SRobert Mustacchi 	SMBUS_PROP_OP_I2C_READ_BLOCK = 1 << SMBUS_OP_I2C_READ_BLOCK
621*32002227SRobert Mustacchi } smbus_prop_op_t;
622*32002227SRobert Mustacchi 
623*32002227SRobert Mustacchi /*
624*32002227SRobert Mustacchi  * The size in bytes of the maximum property name (including the NUL) and the
625*32002227SRobert Mustacchi  * largest data size.
626*32002227SRobert Mustacchi  */
627*32002227SRobert Mustacchi #define	I2C_PROP_NAME_MAX	32
628*32002227SRobert Mustacchi #define	I2C_PROP_SIZE_MAX	256
629*32002227SRobert Mustacchi 
630*32002227SRobert Mustacchi #ifdef __cplusplus
631*32002227SRobert Mustacchi }
632*32002227SRobert Mustacchi #endif
633*32002227SRobert Mustacchi 
634*32002227SRobert Mustacchi #endif /* _SYS_I2C_I2C_H */
635