summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/i2cvar.h
blob: 13b0193998472f57eb40db8f14100c27076ae30d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*	$NetBSD: i2cvar.h,v 1.18 2018/12/10 00:31:45 thorpej Exp $	*/

/*
 * Copyright (c) 2003 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _DEV_I2C_I2CVAR_H_
#define	_DEV_I2C_I2CVAR_H_

#include <sys/device.h>
#include <dev/i2c/i2c_io.h>
#include <prop/proplib.h>

/* Flags passed to i2c routines. */
#define	I2C_F_WRITE		0x00	/* new transfer is a write */
#define	I2C_F_READ		0x01	/* new transfer is a read */
#define	I2C_F_LAST		0x02	/* last byte of read */
#define	I2C_F_STOP		0x04	/* send stop after byte */
#define	I2C_F_POLL		0x08	/* poll, don't sleep */
#define	I2C_F_PEC		0x10	/* smbus packet error checking */

/* i2c bus instance properties */
#define	I2C_PROP_INDIRECT_PROBE_STRATEGY	\
				"i2c-indirect-probe-strategy"
#define	I2C_PROBE_STRATEGY_QUICK_WRITE		\
				"smbus-quick-write"
#define	I2C_PROBE_STRATEGY_RECEIVE_BYTE		\
				"smbus-receive-byte"
#define	I2C_PROBE_STRATEGY_NONE			\
				"none"

#define	I2C_PROP_INDIRECT_DEVICE_WHITELIST	\
				"i2c-indirect-device-whitelist"
	/* value is a prop_array of prop_strings */

struct ic_intr_list {
	LIST_ENTRY(ic_intr_list) il_next;
	int (*il_intr)(void *);
	void *il_intrarg;
};

/*
 * This structure provides the interface between the i2c framework
 * and the underlying i2c controller.
 *
 * Note that this structure is designed specifically to allow us
 * to either use the autoconfiguration framework or not.  This
 * allows a driver for a board with a private i2c bus use generic
 * i2c client drivers for chips that might be on that board.
 */
typedef struct i2c_controller {
	void	*ic_cookie;		/* controller private */

	/*
	 * These provide synchronization in the presence of
	 * multiple users of the i2c bus.  When a device
	 * driver wishes to perform transfers on the i2c
	 * bus, the driver should acquire the bus.  When
	 * the driver is finished, it should release the
	 * bus.
	 *
	 * This is provided by the back-end since a single
	 * controller may present e.g. i2c and smbus views
	 * of the same set of i2c wires.
	 */
	int	(*ic_acquire_bus)(void *, int);
	void	(*ic_release_bus)(void *, int);

	/*
	 * The preferred API for clients of the i2c interface
	 * is the scripted API.  This handles i2c controllers
	 * that do not provide raw access to the i2c signals.
	 */
	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
		    void *, size_t, int);

	int	(*ic_send_start)(void *, int);
	int	(*ic_send_stop)(void *, int);
	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
	int	(*ic_read_byte)(void *, uint8_t *, int);
	int	(*ic_write_byte)(void *, uint8_t, int);

	LIST_HEAD(, ic_intr_list) ic_list;
	LIST_HEAD(, ic_intr_list) ic_proc_list;
	volatile int	ic_running;
	volatile int	ic_pending;
	struct lwp *ic_intr_thread;
	const char *ic_devname;
} *i2c_tag_t;

/* I2C bus types */
#define	I2C_TYPE_SMBUS	1

/* Used to attach the i2c framework to the controller. */
struct i2cbus_attach_args {
	i2c_tag_t iba_tag;		/* the controller */
	int iba_type;			/* bus type */
	prop_array_t iba_child_devices;	/* child devices (direct config) */
};

/* Used to attach devices on the i2c bus. */
struct i2c_attach_args {
	i2c_tag_t	ia_tag;		/* our controller */
	i2c_addr_t	ia_addr;	/* address of device */
	int		ia_type;	/* bus type */
	/* only set if using direct config */
	const char *	ia_name;	/* name of the device */
	int		ia_ncompat;	/* number of pointers in the
					   ia_compat array */
	const char **	ia_compat;	/* chip names */
	prop_dictionary_t ia_prop;	/* dictionnary for this device */
	/*
	 * The following is of limited usefulness and should only be used
	 * in rare cases where we really know what we are doing. Example:
	 * a machine dependent i2c driver (located in sys/arch/$arch/dev)
	 * needing to access some firmware properties.
	 * Depending on the firmware in use, an identifier for the device
	 * may be present. Example: on OpenFirmware machines the device
	 * tree OF node - if available. This info is hard to transport
	 * down to MD drivers through the MI i2c bus otherwise.
	 * 
	 * On ACPI platforms this is the ACPI_HANDLE of the device.
	 */
	uintptr_t	ia_cookie;	/* OF node in openfirmware machines */
};

/*
 * API presented to i2c controllers.
 */
int	iicbus_print(void *, const char *);

/*
 * API presented to i2c devices.
 */
int	iic_compatible_match(const struct i2c_attach_args *,
			     const struct device_compatible_entry *,
			     const struct device_compatible_entry **);
bool	iic_use_direct_match(const struct i2c_attach_args *, const cfdata_t,
			     const struct device_compatible_entry *, int *);

/*
 * Constants to indicate the quality of a match made by a driver's
 * match routine, from lowest to higest:
 *
 *	-- Address only; no other checks were made.
 *
 *	-- Address + device probed and recognized.
 *
 *	-- Direct-config match by "compatible" string.
 *
 *	-- Direct-config match by specific driver name.
 */
#define	I2C_MATCH_ADDRESS_ONLY		1
#define	I2C_MATCH_ADDRESS_AND_PROBE	2
#define	I2C_MATCH_DIRECT_COMPATIBLE	10
#define	I2C_MATCH_DIRECT_COMPATIBLE_MAX	99
#define	I2C_MATCH_DIRECT_SPECIFIC	100

#ifdef _I2C_PRIVATE
/*
 * Macros used internally by the i2c framework.
 */
#define	iic_send_start(ic, flags)					\
	(*(ic)->ic_send_start)((ic)->ic_cookie, (flags))
#define	iic_send_stop(ic, flags)					\
	(*(ic)->ic_send_stop)((ic)->ic_cookie, (flags))
#define	iic_initiate_xfer(ic, addr, flags)				\
	(*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags))

#define	iic_read_byte(ic, bytep, flags)					\
	(*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags))
#define	iic_write_byte(ic, byte, flags)					\
	(*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags))
#endif /* _I2C_PRIVATE */

/*
 * Simplified API for clients of the i2c framework.  Definitions
 * in <dev/i2c/i2c_io.h>.
 */
int	iic_acquire_bus(i2c_tag_t, int);
void	iic_release_bus(i2c_tag_t, int);
int	iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *,
	    size_t, void *, size_t, int);

int	iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int);
int	iic_smbus_write_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t, int);
int	iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int);
int	iic_smbus_read_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t *, int);
int	iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int);
int	iic_smbus_send_byte(i2c_tag_t, i2c_addr_t, uint8_t, int);
int	iic_smbus_quick_read(i2c_tag_t, i2c_addr_t, int);
int	iic_smbus_quick_write(i2c_tag_t, i2c_addr_t, int);
int	iic_smbus_block_read(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
	    size_t, int);
int	iic_smbus_block_write(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
	    size_t, int);

void *	iic_smbus_intr_establish(i2c_tag_t, int (*)(void *), void *);
void *	iic_smbus_intr_establish_proc(i2c_tag_t, int (*)(void *), void *);
void	iic_smbus_intr_disestablish(i2c_tag_t, void *);
void	iic_smbus_intr_disestablish_proc(i2c_tag_t, void *);
int	iic_smbus_intr(i2c_tag_t);

#endif /* _DEV_I2C_I2CVAR_H_ */