Home | History | Annotate | Line # | Download | only in linux
i2c.h revision 1.9
      1  1.9  riastrad /*	$NetBSD: i2c.h,v 1.9 2021/12/19 00:59:25 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.2  riastrad /*-
      4  1.8  riastrad  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.2  riastrad  * All rights reserved.
      6  1.2  riastrad  *
      7  1.2  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  riastrad  * by Taylor R. Campbell.
      9  1.2  riastrad  *
     10  1.2  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.2  riastrad  * modification, are permitted provided that the following conditions
     12  1.2  riastrad  * are met:
     13  1.2  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.2  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.2  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.2  riastrad  *
     19  1.2  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  riastrad  */
     31  1.2  riastrad 
     32  1.2  riastrad #ifndef _LINUX_I2C_H_
     33  1.2  riastrad #define _LINUX_I2C_H_
     34  1.2  riastrad 
     35  1.2  riastrad #include <sys/types.h>
     36  1.2  riastrad #include <sys/device_if.h>
     37  1.2  riastrad #include <sys/queue.h>		/* XXX include order botch: i2cvar.h needs */
     38  1.8  riastrad #include <sys/systm.h>
     39  1.2  riastrad 
     40  1.2  riastrad #include <dev/i2c/i2cvar.h>
     41  1.2  riastrad 
     42  1.8  riastrad #include <linux/pm.h>
     43  1.8  riastrad 
     44  1.2  riastrad struct i2c_adapter;
     45  1.2  riastrad struct i2c_algorithm;
     46  1.2  riastrad struct i2c_msg;
     47  1.2  riastrad 
     48  1.2  riastrad #define	I2C_NAME_SIZE	20
     49  1.2  riastrad 
     50  1.8  riastrad /*
     51  1.8  riastrad  * I2C_M_*: i2c_msg flags
     52  1.8  riastrad  */
     53  1.8  riastrad #define	I2C_M_RD		0x01 /* xfer is read, not write */
     54  1.8  riastrad #define	I2C_M_NOSTART		0x02 /* don't initiate xfer */
     55  1.8  riastrad #define	I2C_M_TEN		0x04 /* 10-bit chip address */
     56  1.8  riastrad 
     57  1.8  riastrad /*
     58  1.8  riastrad  * I2C_CLASS_*: i2c_adapter classes
     59  1.8  riastrad  */
     60  1.2  riastrad #define	I2C_CLASS_DDC	0x01
     61  1.2  riastrad 
     62  1.8  riastrad /*
     63  1.8  riastrad  * I2C_FUNC_*: i2c_adapter functionality bits
     64  1.8  riastrad  */
     65  1.8  riastrad #define	I2C_FUNC_I2C			0x01
     66  1.8  riastrad #define	I2C_FUNC_NOSTART		0x02
     67  1.8  riastrad #define	I2C_FUNC_SMBUS_EMUL		0x04
     68  1.8  riastrad #define	I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x08
     69  1.8  riastrad #define	I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x10
     70  1.8  riastrad #define	I2C_FUNC_10BIT_ADDR		0x20
     71  1.8  riastrad 
     72  1.8  riastrad /*
     73  1.8  riastrad  * struct i2c_msg: A single i2c message request on a particular
     74  1.8  riastrad  * address.  Read if I2C_M_RD is set, write otherwise.
     75  1.8  riastrad  */
     76  1.8  riastrad struct i2c_msg {
     77  1.8  riastrad 	i2c_addr_t	addr;
     78  1.8  riastrad 	uint16_t	flags;	/* I2C_M_* */
     79  1.8  riastrad 	uint16_t	len;
     80  1.8  riastrad 	uint8_t		*buf;
     81  1.8  riastrad };
     82  1.8  riastrad 
     83  1.8  riastrad /*
     84  1.8  riastrad  * struct i2c_adapter: An i2c bus controller.
     85  1.8  riastrad  */
     86  1.8  riastrad struct i2c_adapter {
     87  1.8  riastrad 	char		 		name[I2C_NAME_SIZE];
     88  1.8  riastrad 	const struct i2c_algorithm	*algo;
     89  1.8  riastrad 	void				*algo_data;
     90  1.9  riastrad 	const struct i2c_lock_operations *lock_ops;
     91  1.8  riastrad 	int				retries;
     92  1.8  riastrad 	struct module			*owner;
     93  1.8  riastrad 	unsigned int			class; /* I2C_CLASS_* */
     94  1.8  riastrad 	struct {
     95  1.8  riastrad 		device_t	parent;
     96  1.8  riastrad 	}				dev;
     97  1.8  riastrad 	void				*i2ca_adapdata;
     98  1.8  riastrad };
     99  1.8  riastrad 
    100  1.8  riastrad /*
    101  1.8  riastrad  * struct i2c_algorithm: A procedure for transferring an i2c message on
    102  1.8  riastrad  * an i2c bus, along with a set of flags describing its functionality.
    103  1.8  riastrad  */
    104  1.8  riastrad struct i2c_algorithm {
    105  1.8  riastrad 	int		(*master_xfer)(struct i2c_adapter *, struct i2c_msg *,
    106  1.8  riastrad 			    int);
    107  1.8  riastrad 	uint32_t	(*functionality)(struct i2c_adapter *);
    108  1.8  riastrad };
    109  1.8  riastrad 
    110  1.8  riastrad /*
    111  1.9  riastrad  * struct i2c_lock_operations: i2c bus lock operations.
    112  1.9  riastrad  */
    113  1.9  riastrad struct i2c_lock_operations {
    114  1.9  riastrad 	void	(*lock_bus)(struct i2c_adapter *, unsigned);
    115  1.9  riastrad 	int	(*trylock_bus)(struct i2c_adapter *, unsigned);
    116  1.9  riastrad 	void	(*unlock_bus)(struct i2c_adapter *, unsigned);
    117  1.9  riastrad };
    118  1.9  riastrad 
    119  1.9  riastrad /*
    120  1.8  riastrad  * struct i2c_board_info: Parameters to find an i2c bus and a slave on
    121  1.8  riastrad  * it.  type is the name of an i2c driver; addr is the slave address;
    122  1.8  riastrad  * platform_data is an extra parameter to pass to the i2c driver.
    123  1.8  riastrad  */
    124  1.3  riastrad struct i2c_board_info {
    125  1.3  riastrad 	char			type[I2C_NAME_SIZE];
    126  1.3  riastrad 	uint16_t		addr;
    127  1.8  riastrad 	uint16_t		flags;
    128  1.5  riastrad 	void			*platform_data;
    129  1.3  riastrad };
    130  1.3  riastrad 
    131  1.6  riastrad #define	I2C_BOARD_INFO(board_type, board_addr)		\
    132  1.6  riastrad 	.type = (board_type),				\
    133  1.6  riastrad 	.addr = (board_addr)
    134  1.6  riastrad 
    135  1.8  riastrad /*
    136  1.8  riastrad  * struct i2c_client: An i2c slave device at a particular address on a
    137  1.8  riastrad  * particular bus.
    138  1.8  riastrad  */
    139  1.8  riastrad struct i2c_client {
    140  1.8  riastrad 	struct i2c_adapter	*adapter;
    141  1.8  riastrad 	uint16_t		addr;
    142  1.8  riastrad 	uint16_t		flags;
    143  1.8  riastrad };
    144  1.3  riastrad 
    145  1.8  riastrad /*
    146  1.8  riastrad  * struct i2c_device_id: Device id naming a class of i2c slave devices
    147  1.8  riastrad  * and parameters to the driver for the devices.
    148  1.8  riastrad  */
    149  1.8  riastrad struct i2c_device_id {
    150  1.8  riastrad 	char		name[I2C_NAME_SIZE];
    151  1.8  riastrad 	unsigned long	driver_data;
    152  1.4  riastrad };
    153  1.4  riastrad 
    154  1.8  riastrad /*
    155  1.8  riastrad  * struct i2c_driver: A driver for a class of i2c slave devices.  We
    156  1.8  riastrad  * don't actually use this.
    157  1.8  riastrad  */
    158  1.8  riastrad struct i2c_driver {
    159  1.8  riastrad 	int	(*probe)(struct i2c_client *, const struct i2c_device_id *);
    160  1.8  riastrad 	int	(*remove)(struct i2c_client *);
    161  1.2  riastrad 	struct {
    162  1.8  riastrad 		char			name[I2C_NAME_SIZE];
    163  1.8  riastrad 		const struct dev_pm_ops	pm;
    164  1.8  riastrad 	}	driver;
    165  1.2  riastrad };
    166  1.2  riastrad 
    167  1.8  riastrad /*
    168  1.8  riastrad  * Adapter management.  We don't register these in a global database
    169  1.8  riastrad  * like Linux, so these are just stubs.
    170  1.8  riastrad  */
    171  1.2  riastrad static inline int
    172  1.2  riastrad i2c_add_adapter(struct i2c_adapter *adapter __unused)
    173  1.2  riastrad {
    174  1.8  riastrad 
    175  1.2  riastrad 	return 0;
    176  1.2  riastrad }
    177  1.2  riastrad 
    178  1.2  riastrad static inline void
    179  1.2  riastrad i2c_del_adapter(struct i2c_adapter *adapter __unused)
    180  1.2  riastrad {
    181  1.2  riastrad }
    182  1.2  riastrad 
    183  1.3  riastrad static inline void *
    184  1.3  riastrad i2c_get_adapdata(const struct i2c_adapter *adapter)
    185  1.3  riastrad {
    186  1.3  riastrad 
    187  1.3  riastrad 	return adapter->i2ca_adapdata;
    188  1.3  riastrad }
    189  1.3  riastrad 
    190  1.3  riastrad static inline void
    191  1.3  riastrad i2c_set_adapdata(struct i2c_adapter *adapter, void *data)
    192  1.3  riastrad {
    193  1.3  riastrad 
    194  1.3  riastrad 	adapter->i2ca_adapdata = data;
    195  1.3  riastrad }
    196  1.3  riastrad 
    197  1.2  riastrad /* XXX Make the nm output a little more greppable...  */
    198  1.8  riastrad #define	i2c_master_recv		linux_i2c_master_recv
    199  1.8  riastrad #define	i2c_master_send		linux_i2c_master_send
    200  1.8  riastrad #define	i2c_new_device		linux_i2c_new_device
    201  1.8  riastrad #define	i2c_transfer		linux_i2c_transfer
    202  1.8  riastrad #define	i2c_unregister_device	linux_i2c_unregister_device
    203  1.8  riastrad 
    204  1.8  riastrad int	i2c_master_send(const struct i2c_client *, const char *, int);
    205  1.8  riastrad int	i2c_master_recv(const struct i2c_client *, char *, int);
    206  1.8  riastrad struct i2c_client *
    207  1.8  riastrad 	i2c_new_device(struct i2c_adapter *, const struct i2c_board_info *);
    208  1.2  riastrad int	i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int);
    209  1.8  riastrad void	i2c_unregister_device(struct i2c_client *);
    210  1.2  riastrad 
    211  1.2  riastrad #endif  /* _LINUX_I2C_H_ */
    212