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