xref: /openbsd-src/sys/dev/i2c/i2cvar.h (revision d36891302982b78f8cc3f1cfa46408d6f898f1c7)
1*d3689130Skettenis /*	$OpenBSD: i2cvar.h,v 1.19 2022/08/31 15:14:01 kettenis Exp $	*/
21da3bef2Sgrange /*	$NetBSD: i2cvar.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $	*/
31da3bef2Sgrange 
41da3bef2Sgrange /*
51da3bef2Sgrange  * Copyright (c) 2003 Wasabi Systems, Inc.
61da3bef2Sgrange  * All rights reserved.
71da3bef2Sgrange  *
81da3bef2Sgrange  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
91da3bef2Sgrange  *
101da3bef2Sgrange  * Redistribution and use in source and binary forms, with or without
111da3bef2Sgrange  * modification, are permitted provided that the following conditions
121da3bef2Sgrange  * are met:
131da3bef2Sgrange  * 1. Redistributions of source code must retain the above copyright
141da3bef2Sgrange  *    notice, this list of conditions and the following disclaimer.
151da3bef2Sgrange  * 2. Redistributions in binary form must reproduce the above copyright
161da3bef2Sgrange  *    notice, this list of conditions and the following disclaimer in the
171da3bef2Sgrange  *    documentation and/or other materials provided with the distribution.
181da3bef2Sgrange  * 3. All advertising materials mentioning features or use of this software
191da3bef2Sgrange  *    must display the following acknowledgement:
201da3bef2Sgrange  *      This product includes software developed for the NetBSD Project by
211da3bef2Sgrange  *      Wasabi Systems, Inc.
221da3bef2Sgrange  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
231da3bef2Sgrange  *    or promote products derived from this software without specific prior
241da3bef2Sgrange  *    written permission.
251da3bef2Sgrange  *
261da3bef2Sgrange  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
271da3bef2Sgrange  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281da3bef2Sgrange  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291da3bef2Sgrange  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
301da3bef2Sgrange  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311da3bef2Sgrange  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321da3bef2Sgrange  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331da3bef2Sgrange  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341da3bef2Sgrange  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351da3bef2Sgrange  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361da3bef2Sgrange  * POSSIBILITY OF SUCH DAMAGE.
371da3bef2Sgrange  */
381da3bef2Sgrange 
391da3bef2Sgrange #ifndef _DEV_I2C_I2CVAR_H_
401da3bef2Sgrange #define	_DEV_I2C_I2CVAR_H_
411da3bef2Sgrange 
421da3bef2Sgrange #include <dev/i2c/i2c_io.h>
431da3bef2Sgrange 
4493f089d0Skettenis struct device;
4593f089d0Skettenis 
461da3bef2Sgrange /* Flags passed to i2c routines. */
471da3bef2Sgrange #define	I2C_F_WRITE		0x00	/* new transfer is a write */
481da3bef2Sgrange #define	I2C_F_READ		0x01	/* new transfer is a read */
491da3bef2Sgrange #define	I2C_F_LAST		0x02	/* last byte of read */
501da3bef2Sgrange #define	I2C_F_STOP		0x04	/* send stop after byte */
511da3bef2Sgrange #define	I2C_F_POLL		0x08	/* poll, don't sleep */
521da3bef2Sgrange 
531da3bef2Sgrange /*
541da3bef2Sgrange  * This structure provides the interface between the i2c framework
551da3bef2Sgrange  * and the underlying i2c controller.
561da3bef2Sgrange  *
571da3bef2Sgrange  * Note that this structure is designed specifically to allow us
581da3bef2Sgrange  * to either use the autoconfiguration framework or not.  This
591da3bef2Sgrange  * allows a driver for a board with a private i2c bus use generic
601da3bef2Sgrange  * i2c client drivers for chips that might be on that board.
611da3bef2Sgrange  */
621da3bef2Sgrange typedef struct i2c_controller {
631da3bef2Sgrange 	void	*ic_cookie;		/* controller private */
641da3bef2Sgrange 
651da3bef2Sgrange 	/*
661da3bef2Sgrange 	 * These provide synchronization in the presence of
671da3bef2Sgrange 	 * multiple users of the i2c bus.  When a device
681da3bef2Sgrange 	 * driver wishes to perform transfers on the i2c
691da3bef2Sgrange 	 * bus, the driver should acquire the bus.  When
701da3bef2Sgrange 	 * the driver is finished, it should release the
711da3bef2Sgrange 	 * bus.
721da3bef2Sgrange 	 *
731da3bef2Sgrange 	 * This is provided by the back-end since a single
741da3bef2Sgrange 	 * controller may present e.g. i2c and smbus views
751da3bef2Sgrange 	 * of the same set of i2c wires.
761da3bef2Sgrange 	 */
771da3bef2Sgrange 	int	(*ic_acquire_bus)(void *, int);
781da3bef2Sgrange 	void	(*ic_release_bus)(void *, int);
791da3bef2Sgrange 
801da3bef2Sgrange 	/*
811da3bef2Sgrange 	 * The preferred API for clients of the i2c interface
821da3bef2Sgrange 	 * is the scripted API.  This handles i2c controllers
831da3bef2Sgrange 	 * that do not provide raw access to the i2c signals.
841da3bef2Sgrange 	 */
851da3bef2Sgrange 	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
861da3bef2Sgrange 		    void *, size_t, int);
871da3bef2Sgrange 
881da3bef2Sgrange 	int	(*ic_send_start)(void *, int);
891da3bef2Sgrange 	int	(*ic_send_stop)(void *, int);
901da3bef2Sgrange 	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
911da3bef2Sgrange 	int	(*ic_read_byte)(void *, uint8_t *, int);
921da3bef2Sgrange 	int	(*ic_write_byte)(void *, uint8_t, int);
93985c1510Skettenis 
94985c1510Skettenis 	void	*(*ic_intr_establish)(void *, void *, int, int (*)(void *),
95985c1510Skettenis 		    void *, const char *);
96*d3689130Skettenis 	void	(*ic_intr_disestablish)(void *, void *);
9751cf21f2Skettenis 	const char *(*ic_intr_string)(void *, void *);
981da3bef2Sgrange } *i2c_tag_t;
991da3bef2Sgrange 
1001da3bef2Sgrange /* Used to attach the i2c framework to the controller. */
1011da3bef2Sgrange struct i2cbus_attach_args {
1021da3bef2Sgrange 	const char *iba_name;		/* bus name ("iic") */
1031da3bef2Sgrange 	i2c_tag_t iba_tag;		/* the controller */
104a5a63700Sderaadt 	void	(*iba_bus_scan)(struct device *, struct i2cbus_attach_args *,
105a5a63700Sderaadt 		    void *);
106a5a63700Sderaadt 	void	*iba_bus_scan_arg;
1071da3bef2Sgrange };
1081da3bef2Sgrange 
1091da3bef2Sgrange /* Used to attach devices on the i2c bus. */
1101da3bef2Sgrange struct i2c_attach_args {
1111da3bef2Sgrange 	i2c_tag_t	ia_tag;		/* our controller */
1121da3bef2Sgrange 	i2c_addr_t	ia_addr;	/* address of device */
1131da3bef2Sgrange 	int		ia_size;	/* size (for EEPROMs) */
11460e1d370Sderaadt 	char		*ia_name;	/* chip name */
1157349a5e9Svisa 	size_t		ia_namelen;	/* length of name concatenation */
1164314413fSdlg 	void		*ia_cookie;	/* pass extra info from bus to dev */
117985c1510Skettenis 	void		*ia_intr;	/* interrupt info */
118d03221a8Sjcs 	int		ia_poll;	/* to force polling */
1191da3bef2Sgrange };
1201da3bef2Sgrange 
1211da3bef2Sgrange /*
1221da3bef2Sgrange  * API presented to i2c controllers.
1231da3bef2Sgrange  */
1241da3bef2Sgrange int	iicbus_print(void *, const char *);
1251da3bef2Sgrange 
1261da3bef2Sgrange #ifdef _I2C_PRIVATE
1271da3bef2Sgrange /*
1281da3bef2Sgrange  * Macros used internally by the i2c framework.
1291da3bef2Sgrange  */
1301da3bef2Sgrange #define	iic_send_start(ic, flags)					\
1311da3bef2Sgrange 	(*(ic)->ic_send_start)((ic)->ic_cookie, (flags))
1321da3bef2Sgrange #define	iic_send_stop(ic, flags)					\
1331da3bef2Sgrange 	(*(ic)->ic_send_stop)((ic)->ic_cookie, (flags))
1341da3bef2Sgrange #define	iic_initiate_xfer(ic, addr, flags)				\
1351da3bef2Sgrange 	(*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags))
1361da3bef2Sgrange 
1371da3bef2Sgrange #define	iic_read_byte(ic, bytep, flags)					\
1381da3bef2Sgrange 	(*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags))
1391da3bef2Sgrange #define	iic_write_byte(ic, byte, flags)					\
1401da3bef2Sgrange 	(*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags))
1411165b4a3Sgrange 
1421165b4a3Sgrange void	iic_scan(struct device *, struct i2cbus_attach_args *);
1436c21fe44Sderaadt int	iic_print(void *, const char *);
1441da3bef2Sgrange #endif /* _I2C_PRIVATE */
1451da3bef2Sgrange 
1461da3bef2Sgrange /*
1471da3bef2Sgrange  * Simplified API for clients of the i2c framework.  Definitions
1481da3bef2Sgrange  * in <dev/i2c/i2c_io.h>.
1491da3bef2Sgrange  */
1501da3bef2Sgrange #define	iic_acquire_bus(ic, flags)					\
1511da3bef2Sgrange 	(*(ic)->ic_acquire_bus)((ic)->ic_cookie, (flags))
1521da3bef2Sgrange #define	iic_release_bus(ic, flags)					\
1531da3bef2Sgrange 	(*(ic)->ic_release_bus)((ic)->ic_cookie, (flags))
1541da3bef2Sgrange 
1551da3bef2Sgrange int	iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *,
1561da3bef2Sgrange 	    size_t, void *, size_t, int);
1571da3bef2Sgrange 
1581da3bef2Sgrange int	iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int);
1591da3bef2Sgrange int	iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int);
1601da3bef2Sgrange int	iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int);
1611da3bef2Sgrange 
162985c1510Skettenis #define iic_intr_establish(ic, ih, level, func, arg, name)		\
163985c1510Skettenis 	(*(ic)->ic_intr_establish)((ic)->ic_cookie, (ih), (level),	\
164985c1510Skettenis 	    (func), (arg), (name))
165*d3689130Skettenis #define iic_intr_disestablish(ic, ih)					\
166*d3689130Skettenis 	(*(ic)->ic_intr_disestablish)((ic)->ic_cookie, (ih))
16751cf21f2Skettenis #define iic_intr_string(ic, ih)						\
16851cf21f2Skettenis 	(*(ic)->ic_intr_string)((ic)->ic_cookie, (ih))
169985c1510Skettenis 
170b8fabd7dSderaadt void	iic_ignore_addr(u_int8_t addr);
1717349a5e9Svisa int	iic_is_compatible(const struct i2c_attach_args *, const char *);
172b8fabd7dSderaadt 
1731da3bef2Sgrange #endif /* _DEV_I2C_I2CVAR_H_ */
174