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