Home | History | Annotate | Line # | Download | only in i2c
      1 /*	$NetBSD: i2cvar.h,v 1.32 2025/09/21 14:43:19 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 /*
     75  * This structure provides the interface between the i2c framework
     76  * and the underlying i2c controller.
     77  *
     78  * Note that this structure is designed specifically to allow us
     79  * to either use the autoconfiguration framework or not.  This
     80  * allows a driver for a board with a private i2c bus use generic
     81  * i2c client drivers for chips that might be on that board.
     82  */
     83 typedef struct i2c_controller {
     84 	void	*ic_cookie;		/* controller private */
     85 
     86 	/*
     87 	 * Multi-channel i2c controllers and i2c muxes allow
     88 	 * for multiple busses to be driven by a single block
     89 	 * of controller logic.  Each of these busses is
     90 	 * represented by a logical controller with its own
     91 	 * i2c_tag_t.  By default, each i2c_tag_t is initialized
     92 	 * indicating a single-bus controller, but multi-channel
     93 	 * controllers and muxes should initialize this field to
     94 	 * indicate which logical bus number this i2c_tag_t is
     95 	 * associated with.
     96 	 */
     97 	int	ic_channel;
     98 
     99 	/*
    100 	 * These provide synchronization in the presence of
    101 	 * multiple users of the i2c bus.  When a device
    102 	 * driver wishes to perform transfers on the i2c
    103 	 * bus, the driver should acquire the bus.  When
    104 	 * the driver is finished, it should release the
    105 	 * bus.
    106 	 *
    107 	 * The main synchronization logic is handled by the
    108 	 * generic i2c layer, but optional hooks to back-end
    109 	 * drivers are provided in case additional processing
    110 	 * is needed (e.g. enabling the i2c controller).
    111 	 */
    112 	kmutex_t ic_bus_lock;
    113 	int	(*ic_acquire_bus)(void *, int);
    114 	void	(*ic_release_bus)(void *, int);
    115 
    116 	/*
    117 	 * The preferred API for clients of the i2c interface
    118 	 * is the scripted API.  This handles i2c controllers
    119 	 * that do not provide raw access to the i2c signals.
    120 	 */
    121 	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
    122 		    void *, size_t, int);
    123 
    124 	int	(*ic_send_start)(void *, int);
    125 	int	(*ic_send_stop)(void *, int);
    126 	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
    127 	int	(*ic_read_byte)(void *, uint8_t *, int);
    128 	int	(*ic_write_byte)(void *, uint8_t, int);
    129 
    130 	/* For future expansion; do not remove. */
    131 	struct i2c_tag_private	*ic_tag_private;
    132 } *i2c_tag_t;
    133 
    134 #define	I2C_CHANNEL_DEFAULT	-1
    135 
    136 static inline int
    137 i2c_tag_channel(i2c_tag_t tag)
    138 {
    139 	return tag->ic_channel == I2C_CHANNEL_DEFAULT ? 0
    140 						      : tag->ic_channel;
    141 }
    142 
    143 /* Used to attach the i2c framework to the controller. */
    144 struct i2cbus_attach_args {
    145 	i2c_tag_t iba_tag;		/* the controller */
    146 };
    147 
    148 /* Used to attach devices on the i2c bus. */
    149 struct i2c_attach_args {
    150 	i2c_tag_t	ia_tag;		/* our controller */
    151 	i2c_addr_t	ia_addr;	/* address of device */
    152 
    153 	/* only set if using direct config */
    154 	const char *	ia_name;	/* name of the device */
    155 	const char *	ia_clist;	/* compatible strlist */
    156 	size_t		ia_clist_size;	/* size of compatible strlist */
    157 	devhandle_t	ia_devhandle;	/* device handle for the device */
    158 };
    159 
    160 /*
    161  * API presented to i2c controllers.
    162  */
    163 int	iicbus_print(void *, const char *);
    164 void	iic_tag_init(i2c_tag_t);
    165 void	iic_tag_fini(i2c_tag_t);
    166 
    167 int	iic_acquire_bus_lock(kmutex_t *, int);
    168 void	iic_release_bus_lock(kmutex_t *);
    169 
    170 static inline device_t
    171 iicbus_attach_with_devhandle(device_t dev, i2c_tag_t tag, devhandle_t devhandle)
    172 {
    173 	struct i2cbus_attach_args iba = {
    174 		.iba_tag = tag,
    175 	};
    176 	return config_found(dev, &iba, iicbus_print,
    177 	    CFARGS(.iattr = "i2cbus",
    178 		   .devhandle = devhandle));
    179 }
    180 
    181 static inline device_t
    182 iicbus_attach(device_t dev, i2c_tag_t tag)
    183 {
    184 	return iicbus_attach_with_devhandle(dev, tag, device_handle(dev));
    185 }
    186 
    187 /*
    188  * API presented to i2c devices.
    189  */
    190 int	iic_compatible_match(const struct i2c_attach_args *,
    191 			     const struct device_compatible_entry *);
    192 bool	iic_use_direct_match(const struct i2c_attach_args *, const cfdata_t,
    193 			     const struct device_compatible_entry *, int *);
    194 const struct device_compatible_entry *
    195 	iic_compatible_lookup(const struct i2c_attach_args *,
    196 			      const struct device_compatible_entry *);
    197 
    198 /*
    199  * Constants to indicate the quality of a match made by a driver's
    200  * match routine, from lowest to highest:
    201  *
    202  *	-- Address only; no other checks were made.
    203  *
    204  *	-- Address + device probed and recognized.
    205  *
    206  *	-- Direct-config match by "compatible" string.
    207  *
    208  *	-- Direct-config match by specific driver name.
    209  */
    210 #define	I2C_MATCH_ADDRESS_ONLY		1
    211 #define	I2C_MATCH_ADDRESS_AND_PROBE	2
    212 #define	I2C_MATCH_DIRECT_COMPATIBLE	10
    213 #define	I2C_MATCH_DIRECT_COMPATIBLE_MAX	99
    214 #define	I2C_MATCH_DIRECT_SPECIFIC	100
    215 
    216 #ifdef _I2C_PRIVATE
    217 /*
    218  * Macros used internally by the i2c framework.
    219  */
    220 #define	iic_send_start(ic, flags)					\
    221 	(*(ic)->ic_send_start)((ic)->ic_cookie, (flags))
    222 #define	iic_send_stop(ic, flags)					\
    223 	(*(ic)->ic_send_stop)((ic)->ic_cookie, (flags))
    224 #define	iic_initiate_xfer(ic, addr, flags)				\
    225 	(*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags))
    226 
    227 #define	iic_read_byte(ic, bytep, flags)					\
    228 	(*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags))
    229 #define	iic_write_byte(ic, byte, flags)					\
    230 	(*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags))
    231 #endif /* _I2C_PRIVATE */
    232 
    233 /*
    234  * Simplified API for clients of the i2c framework.  Definitions
    235  * in <dev/i2c/i2c_io.h>.
    236  */
    237 int	iic_acquire_bus(i2c_tag_t, int);
    238 void	iic_release_bus(i2c_tag_t, int);
    239 int	iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *,
    240 	    size_t, void *, size_t, int);
    241 
    242 int	iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int);
    243 int	iic_smbus_write_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t, int);
    244 int	iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int);
    245 int	iic_smbus_read_word(i2c_tag_t, i2c_addr_t, uint8_t, uint16_t *, int);
    246 int	iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int);
    247 int	iic_smbus_send_byte(i2c_tag_t, i2c_addr_t, uint8_t, int);
    248 int	iic_smbus_quick_read(i2c_tag_t, i2c_addr_t, int);
    249 int	iic_smbus_quick_write(i2c_tag_t, i2c_addr_t, int);
    250 int	iic_smbus_block_read(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
    251 	    size_t, int);
    252 int	iic_smbus_block_write(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *,
    253 	    size_t, int);
    254 
    255 #endif /* _DEV_I2C_I2CVAR_H_ */
    256