xref: /openbsd/sys/dev/i2c/i2cvar.h (revision d3689130)
1 /*	$OpenBSD: i2cvar.h,v 1.19 2022/08/31 15:14:01 kettenis Exp $	*/
2 /*	$NetBSD: i2cvar.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2003 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef _DEV_I2C_I2CVAR_H_
40 #define	_DEV_I2C_I2CVAR_H_
41 
42 #include <dev/i2c/i2c_io.h>
43 
44 struct device;
45 
46 /* Flags passed to i2c routines. */
47 #define	I2C_F_WRITE		0x00	/* new transfer is a write */
48 #define	I2C_F_READ		0x01	/* new transfer is a read */
49 #define	I2C_F_LAST		0x02	/* last byte of read */
50 #define	I2C_F_STOP		0x04	/* send stop after byte */
51 #define	I2C_F_POLL		0x08	/* poll, don't sleep */
52 
53 /*
54  * This structure provides the interface between the i2c framework
55  * and the underlying i2c controller.
56  *
57  * Note that this structure is designed specifically to allow us
58  * to either use the autoconfiguration framework or not.  This
59  * allows a driver for a board with a private i2c bus use generic
60  * i2c client drivers for chips that might be on that board.
61  */
62 typedef struct i2c_controller {
63 	void	*ic_cookie;		/* controller private */
64 
65 	/*
66 	 * These provide synchronization in the presence of
67 	 * multiple users of the i2c bus.  When a device
68 	 * driver wishes to perform transfers on the i2c
69 	 * bus, the driver should acquire the bus.  When
70 	 * the driver is finished, it should release the
71 	 * bus.
72 	 *
73 	 * This is provided by the back-end since a single
74 	 * controller may present e.g. i2c and smbus views
75 	 * of the same set of i2c wires.
76 	 */
77 	int	(*ic_acquire_bus)(void *, int);
78 	void	(*ic_release_bus)(void *, int);
79 
80 	/*
81 	 * The preferred API for clients of the i2c interface
82 	 * is the scripted API.  This handles i2c controllers
83 	 * that do not provide raw access to the i2c signals.
84 	 */
85 	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
86 		    void *, size_t, int);
87 
88 	int	(*ic_send_start)(void *, int);
89 	int	(*ic_send_stop)(void *, int);
90 	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
91 	int	(*ic_read_byte)(void *, uint8_t *, int);
92 	int	(*ic_write_byte)(void *, uint8_t, int);
93 
94 	void	*(*ic_intr_establish)(void *, void *, int, int (*)(void *),
95 		    void *, const char *);
96 	void	(*ic_intr_disestablish)(void *, void *);
97 	const char *(*ic_intr_string)(void *, void *);
98 } *i2c_tag_t;
99 
100 /* Used to attach the i2c framework to the controller. */
101 struct i2cbus_attach_args {
102 	const char *iba_name;		/* bus name ("iic") */
103 	i2c_tag_t iba_tag;		/* the controller */
104 	void	(*iba_bus_scan)(struct device *, struct i2cbus_attach_args *,
105 		    void *);
106 	void	*iba_bus_scan_arg;
107 };
108 
109 /* Used to attach devices on the i2c bus. */
110 struct i2c_attach_args {
111 	i2c_tag_t	ia_tag;		/* our controller */
112 	i2c_addr_t	ia_addr;	/* address of device */
113 	int		ia_size;	/* size (for EEPROMs) */
114 	char		*ia_name;	/* chip name */
115 	size_t		ia_namelen;	/* length of name concatenation */
116 	void		*ia_cookie;	/* pass extra info from bus to dev */
117 	void		*ia_intr;	/* interrupt info */
118 	int		ia_poll;	/* to force polling */
119 };
120 
121 /*
122  * API presented to i2c controllers.
123  */
124 int	iicbus_print(void *, const char *);
125 
126 #ifdef _I2C_PRIVATE
127 /*
128  * Macros used internally by the i2c framework.
129  */
130 #define	iic_send_start(ic, flags)					\
131 	(*(ic)->ic_send_start)((ic)->ic_cookie, (flags))
132 #define	iic_send_stop(ic, flags)					\
133 	(*(ic)->ic_send_stop)((ic)->ic_cookie, (flags))
134 #define	iic_initiate_xfer(ic, addr, flags)				\
135 	(*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags))
136 
137 #define	iic_read_byte(ic, bytep, flags)					\
138 	(*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags))
139 #define	iic_write_byte(ic, byte, flags)					\
140 	(*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags))
141 
142 void	iic_scan(struct device *, struct i2cbus_attach_args *);
143 int	iic_print(void *, const char *);
144 #endif /* _I2C_PRIVATE */
145 
146 /*
147  * Simplified API for clients of the i2c framework.  Definitions
148  * in <dev/i2c/i2c_io.h>.
149  */
150 #define	iic_acquire_bus(ic, flags)					\
151 	(*(ic)->ic_acquire_bus)((ic)->ic_cookie, (flags))
152 #define	iic_release_bus(ic, flags)					\
153 	(*(ic)->ic_release_bus)((ic)->ic_cookie, (flags))
154 
155 int	iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *,
156 	    size_t, void *, size_t, int);
157 
158 int	iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int);
159 int	iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int);
160 int	iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int);
161 
162 #define iic_intr_establish(ic, ih, level, func, arg, name)		\
163 	(*(ic)->ic_intr_establish)((ic)->ic_cookie, (ih), (level),	\
164 	    (func), (arg), (name))
165 #define iic_intr_disestablish(ic, ih)					\
166 	(*(ic)->ic_intr_disestablish)((ic)->ic_cookie, (ih))
167 #define iic_intr_string(ic, ih)						\
168 	(*(ic)->ic_intr_string)((ic)->ic_cookie, (ih))
169 
170 void	iic_ignore_addr(u_int8_t addr);
171 int	iic_is_compatible(const struct i2c_attach_args *, const char *);
172 
173 #endif /* _DEV_I2C_I2CVAR_H_ */
174