Home | History | Annotate | Line # | Download | only in i2c
i2cvar.h revision 1.24.2.2
      1 /*	$NetBSD: i2cvar.h,v 1.24.2.2 2021/05/08 14:23:15 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #ifndef _DEV_I2C_I2CVAR_H_
     39 #define	_DEV_I2C_I2CVAR_H_
     40 
     41 #include <sys/device.h>
     42 #include <sys/mutex.h>
     43 #include <dev/i2c/i2c_io.h>
     44 #include <prop/proplib.h>
     45 
     46 /* Flags passed to i2c routines. */
     47 #define	I2C_F_WRITE	0		/* new transfer is a write */
     48 #define	I2C_F_READ	__BIT(0)	/* new transfer is a read */
     49 #define	I2C_F_LAST	__BIT(1)	/* last byte of read */
     50 #define	I2C_F_STOP	__BIT(2)	/* send stop after byte */
     51 #define	I2C_F_POLL	__BIT(3)	/* poll, don't sleep */
     52 #define	I2C_F_PEC	__BIT(4)	/* smbus packet error checking */
     53 #define	I2C_F_SPEED	__BITS(28,31)	/* I2C transfer speed selector */
     54 
     55 #define	I2C_SPEED_SM		0	/* standard mode (100Kb/s) */
     56 #define	I2C_SPEED_FM		1	/* fast mode (400Kb/s) */
     57 #define	I2C_SPEED_FMPLUS	2	/* fast mode+ (1Mb/s) */
     58 #define	I2C_SPEED_HS		3	/* high speed (3.4Mb/s) */
     59 
     60 /* i2c bus instance properties */
     61 #define	I2C_PROP_INDIRECT_PROBE_STRATEGY	\
     62 				"i2c-indirect-probe-strategy"
     63 #define	I2C_PROBE_STRATEGY_QUICK_WRITE		\
     64 				"smbus-quick-write"
     65 #define	I2C_PROBE_STRATEGY_RECEIVE_BYTE		\
     66 				"smbus-receive-byte"
     67 #define	I2C_PROBE_STRATEGY_NONE			\
     68 				"none"
     69 
     70 #define	I2C_PROP_INDIRECT_DEVICE_PERMITLIST	\
     71 				"i2c-indirect-device-permitlist"
     72 	/* value is a prop_array of prop_strings */
     73 
     74 struct ic_intr_list {
     75 	LIST_ENTRY(ic_intr_list) il_next;
     76 	int (*il_intr)(void *);
     77 	void *il_intrarg;
     78 };
     79 
     80 /*
     81  * This structure provides the interface between the i2c framework
     82  * and the underlying i2c controller.
     83  *
     84  * Note that this structure is designed specifically to allow us
     85  * to either use the autoconfiguration framework or not.  This
     86  * allows a driver for a board with a private i2c bus use generic
     87  * i2c client drivers for chips that might be on that board.
     88  */
     89 typedef struct i2c_controller {
     90 	void	*ic_cookie;		/* controller private */
     91 
     92 	/*
     93 	 * These provide synchronization in the presence of
     94 	 * multiple users of the i2c bus.  When a device
     95 	 * driver wishes to perform transfers on the i2c
     96 	 * bus, the driver should acquire the bus.  When
     97 	 * the driver is finished, it should release the
     98 	 * bus.
     99 	 *
    100 	 * The main synchronization logic is handled by the
    101 	 * generic i2c layer, but optional hooks to back-end
    102 	 * drivers are provided in case additional processing
    103 	 * is needed (e.g. enabling the i2c controller).
    104 	 */
    105 	kmutex_t ic_bus_lock;
    106 	int	(*ic_acquire_bus)(void *, int);
    107 	void	(*ic_release_bus)(void *, int);
    108 
    109 	/*
    110 	 * The preferred API for clients of the i2c interface
    111 	 * is the scripted API.  This handles i2c controllers
    112 	 * that do not provide raw access to the i2c signals.
    113 	 */
    114 	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
    115 		    void *, size_t, int);
    116 
    117 	int	(*ic_send_start)(void *, int);
    118 	int	(*ic_send_stop)(void *, int);
    119 	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
    120 	int	(*ic_read_byte)(void *, uint8_t *, int);
    121 	int	(*ic_write_byte)(void *, uint8_t, int);
    122 
    123 	LIST_HEAD(, ic_intr_list) ic_list;
    124 	LIST_HEAD(, ic_intr_list) ic_proc_list;
    125 	volatile int	ic_running;
    126 	volatile int	ic_pending;
    127 	struct lwp *ic_intr_thread;
    128 	const char *ic_devname;
    129 } *i2c_tag_t;
    130 
    131 /* Used to attach the i2c framework to the controller. */
    132 struct i2cbus_attach_args {
    133 	i2c_tag_t iba_tag;		/* the controller */
    134 	int iba_bus;			/* bus number (optional) */
    135 };
    136 
    137 /* Type of value stored in "ia_cookie" */
    138 enum i2c_cookie_type {
    139 	I2C_COOKIE_NONE,		/* Cookie is not valid */
    140 	I2C_COOKIE_OF,			/* Cookie is an OF node phandle */
    141 	I2C_COOKIE_ACPI,		/* Cookie is an ACPI handle */
    142 };
    143 
    144 /* Used to attach devices on the i2c bus. */
    145 struct i2c_attach_args {
    146 	i2c_tag_t	ia_tag;		/* our controller */
    147 	i2c_addr_t	ia_addr;	/* address of device */
    148 	int		ia_type;	/* bus type */
    149 
    150 	/* only set if using direct config */
    151 	const char *	ia_name;	/* name of the device */
    152 	const char *	ia_clist;	/* compatible strlist */
    153 	size_t		ia_clist_size;	/* size of compatible strlist */
    154 	prop_dictionary_t ia_prop;	/* property dictionary for the device */
    155 	devhandle_t	ia_devhandle;	/* device handle for the device */
    156 
    157 	/*
    158 	 * The following is of limited usefulness and should only be used
    159 	 * in rare cases where we really know what we are doing. Example:
    160 	 * a machine dependent i2c driver (located in sys/arch/$arch/dev)
    161 	 * needing to access some firmware properties.
    162 	 * Depending on the firmware in use, an identifier for the device
    163 	 * may be present. Example: on OpenFirmware machines the device
    164 	 * tree OF node - if available. This info is hard to transport
    165 	 * down to MD drivers through the MI i2c bus otherwise.
    166 	 *
    167 	 * On ACPI platforms this is the ACPI_HANDLE of the device.
    168 	 */
    169 	uintptr_t	ia_cookie;	/* OF node in openfirmware machines */
    170 	enum i2c_cookie_type ia_cookietype; /* Value type of cookie */
    171 };
    172 
    173 /*
    174  * i2c-enumerate-devices device call
    175  *
    176  *	Enumerate the devices connected to this I2C bus, filling out
    177  *	the i2c_attach_args and invoking the callback for each one.
    178  *	If the callback returns true, then enumeration continues.  If
    179  *	the callback returns false, enumeration is stopped.
    180  */
    181 struct i2c_enumerate_devices_args {
    182 	struct i2c_attach_args *ia;
    183 	bool (*callback)(device_t, struct i2c_enumerate_devices_args *);
    184 	unsigned int count;
    185 };
    186 
    187 /*
    188  * API presented to i2c controllers.
    189  */
    190 int	iicbus_print(void *, const char *);
    191 int	iicbus_print_multi(void *, const char *);
    192 void	iic_tag_init(i2c_tag_t);
    193 void	iic_tag_fini(i2c_tag_t);
    194 
    195 /*
    196  * API presented to i2c devices.
    197  */
    198 int	iic_compatible_match(const struct i2c_attach_args *,
    199 			     const struct device_compatible_entry *);
    200 bool	iic_use_direct_match(const struct i2c_attach_args *, const cfdata_t,
    201 			     const struct device_compatible_entry *, int *);
    202 const struct device_compatible_entry *
    203 	iic_compatible_lookup(const struct i2c_attach_args *,
    204 			      const struct device_compatible_entry *);
    205 
    206 /*
    207  * Constants to indicate the quality of a match made by a driver's
    208  * match routine, from lowest to higest:
    209  *
    210  *	-- Address only; no other checks were made.
    211  *
    212  *	-- Address + device probed and recognized.
    213  *
    214  *	-- Direct-config match by "compatible" string.
    215  *
    216  *	-- Direct-config match by specific driver name.
    217  */
    218 #define	I2C_MATCH_ADDRESS_ONLY		1
    219 #define	I2C_MATCH_ADDRESS_AND_PROBE	2
    220 #define	I2C_MATCH_DIRECT_COMPATIBLE	10
    221 #define	I2C_MATCH_DIRECT_COMPATIBLE_MAX	99
    222 #define	I2C_MATCH_DIRECT_SPECIFIC	100
    223 
    224 #ifdef _I2C_PRIVATE
    225 /*
    226  * Macros used internally by the i2c framework.
    227  */
    228 #define	iic_send_start(ic, flags)					\
    229 	(*(ic)->ic_send_start)((ic)->ic_cookie, (flags))
    230 #define	iic_send_stop(ic, flags)					\
    231 	(*(ic)->ic_send_stop)((ic)->ic_cookie, (flags))
    232 #define	iic_initiate_xfer(ic, addr, flags)				\
    233 	(*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags))
    234 
    235 #define	iic_read_byte(ic, bytep, flags)					\
    236 	(*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags))
    237 #define	iic_write_byte(ic, byte, flags)					\
    238 	(*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags))
    239 #endif /* _I2C_PRIVATE */
    240 
    241 /*
    242  * Simplified API for clients of the i2c framework.  Definitions
    243  * in <dev/i2c/i2c_io.h>.
    244  */
    245 int	iic_acquire_bus(i2c_tag_t, int);
    246 void	iic_release_bus(i2c_tag_t, int);
    247 int	iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *,
    248 	    size_t, void *, size_t, int);
    249 
    250 int	iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int);
    251 int	iic_smbus_write_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t, int);
    252 int	iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int);
    253 int	iic_smbus_read_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t *, int);
    254 int	iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int);
    255 int	iic_smbus_send_byte(i2c_tag_t, i2c_addr_t, uint8_t, int);
    256 int	iic_smbus_quick_read(i2c_tag_t, i2c_addr_t, int);
    257 int	iic_smbus_quick_write(i2c_tag_t, i2c_addr_t, int);
    258 int	iic_smbus_block_read(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
    259 	    size_t, int);
    260 int	iic_smbus_block_write(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
    261 	    size_t, int);
    262 
    263 void *	iic_smbus_intr_establish(i2c_tag_t, int (*)(void *), void *);
    264 void *	iic_smbus_intr_establish_proc(i2c_tag_t, int (*)(void *), void *);
    265 void	iic_smbus_intr_disestablish(i2c_tag_t, void *);
    266 void	iic_smbus_intr_disestablish_proc(i2c_tag_t, void *);
    267 int	iic_smbus_intr(i2c_tag_t);
    268 
    269 #endif /* _DEV_I2C_I2CVAR_H_ */
    270