Home | History | Annotate | Line # | Download | only in linux
i2c.h revision 1.12
      1 /*	$NetBSD: i2c.h,v 1.12 2021/12/19 10:36:07 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 #include <linux/module.h>
     44 #include <linux/mutex.h>
     45 
     46 struct i2c_adapter;
     47 struct i2c_algorithm;
     48 struct i2c_msg;
     49 
     50 #define	I2C_NAME_SIZE	20
     51 
     52 /*
     53  * I2C_M_*: i2c_msg flags
     54  */
     55 #define	I2C_M_RD		0x01 /* xfer is read, not write */
     56 #define	I2C_M_NOSTART		0x02 /* don't initiate xfer */
     57 #define	I2C_M_TEN		0x04 /* 10-bit chip address */
     58 #define	I2C_M_STOP		0x08 /* send stop after msg */
     59 
     60 /*
     61  * I2C_CLASS_*: i2c_adapter classes
     62  */
     63 #define	I2C_CLASS_DDC	0x01
     64 
     65 /*
     66  * I2C_FUNC_*: i2c_adapter functionality bits
     67  */
     68 #define	I2C_FUNC_I2C			0x01
     69 #define	I2C_FUNC_NOSTART		0x02
     70 #define	I2C_FUNC_SMBUS_EMUL		0x04
     71 #define	I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x08
     72 #define	I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x10
     73 #define	I2C_FUNC_10BIT_ADDR		0x20
     74 
     75 /*
     76  * struct i2c_msg: A single i2c message request on a particular
     77  * address.  Read if I2C_M_RD is set, write otherwise.
     78  */
     79 struct i2c_msg {
     80 	i2c_addr_t	addr;
     81 	uint16_t	flags;	/* I2C_M_* */
     82 	uint16_t	len;
     83 	uint8_t		*buf;
     84 };
     85 
     86 /*
     87  * struct i2c_adapter: An i2c bus controller.
     88  */
     89 struct i2c_adapter {
     90 	char		 		name[I2C_NAME_SIZE];
     91 	const struct i2c_algorithm	*algo;
     92 	void				*algo_data;
     93 	const struct i2c_lock_operations *lock_ops;
     94 	int				retries;
     95 	struct module			*owner;
     96 	unsigned int			class; /* I2C_CLASS_* */
     97 	struct {
     98 		device_t	parent;
     99 	}				dev;
    100 	void				*i2ca_adapdata;
    101 };
    102 
    103 /*
    104  * struct i2c_algorithm: A procedure for transferring an i2c message on
    105  * an i2c bus, along with a set of flags describing its functionality.
    106  */
    107 struct i2c_algorithm {
    108 	int		(*master_xfer)(struct i2c_adapter *, struct i2c_msg *,
    109 			    int);
    110 	uint32_t	(*functionality)(struct i2c_adapter *);
    111 };
    112 
    113 /*
    114  * struct i2c_lock_operations: i2c bus lock operations.
    115  */
    116 struct i2c_lock_operations {
    117 	void	(*lock_bus)(struct i2c_adapter *, unsigned);
    118 	int	(*trylock_bus)(struct i2c_adapter *, unsigned);
    119 	void	(*unlock_bus)(struct i2c_adapter *, unsigned);
    120 };
    121 
    122 /*
    123  * struct i2c_board_info: Parameters to find an i2c bus and a slave on
    124  * it.  type is the name of an i2c driver; addr is the slave address;
    125  * platform_data is an extra parameter to pass to the i2c driver.
    126  */
    127 struct i2c_board_info {
    128 	char			type[I2C_NAME_SIZE];
    129 	uint16_t		addr;
    130 	uint16_t		flags;
    131 	void			*platform_data;
    132 };
    133 
    134 #define	I2C_BOARD_INFO(board_type, board_addr)		\
    135 	.type = (board_type),				\
    136 	.addr = (board_addr)
    137 
    138 /*
    139  * struct i2c_client: An i2c slave device at a particular address on a
    140  * particular bus.
    141  */
    142 struct i2c_client {
    143 	struct i2c_adapter	*adapter;
    144 	uint16_t		addr;
    145 	uint16_t		flags;
    146 };
    147 
    148 /*
    149  * struct i2c_device_id: Device id naming a class of i2c slave devices
    150  * and parameters to the driver for the devices.
    151  */
    152 struct i2c_device_id {
    153 	char		name[I2C_NAME_SIZE];
    154 	unsigned long	driver_data;
    155 };
    156 
    157 /*
    158  * struct i2c_driver: A driver for a class of i2c slave devices.  We
    159  * don't actually use this.
    160  */
    161 struct i2c_driver {
    162 	int	(*probe)(struct i2c_client *, const struct i2c_device_id *);
    163 	int	(*remove)(struct i2c_client *);
    164 	struct {
    165 		char			name[I2C_NAME_SIZE];
    166 		const struct dev_pm_ops	pm;
    167 	}	driver;
    168 };
    169 
    170 /*
    171  * Adapter management.  We don't register these in a global database
    172  * like Linux, so these are just stubs.
    173  */
    174 static inline int
    175 i2c_add_adapter(struct i2c_adapter *adapter __unused)
    176 {
    177 
    178 	return 0;
    179 }
    180 
    181 static inline void
    182 i2c_del_adapter(struct i2c_adapter *adapter __unused)
    183 {
    184 }
    185 
    186 static inline void *
    187 i2c_get_adapdata(const struct i2c_adapter *adapter)
    188 {
    189 
    190 	return adapter->i2ca_adapdata;
    191 }
    192 
    193 static inline void
    194 i2c_set_adapdata(struct i2c_adapter *adapter, void *data)
    195 {
    196 
    197 	adapter->i2ca_adapdata = data;
    198 }
    199 
    200 /* XXX Make the nm output a little more greppable...  */
    201 #define	i2c_master_recv		linux_i2c_master_recv
    202 #define	i2c_master_send		linux_i2c_master_send
    203 #define	i2c_new_device		linux_i2c_new_device
    204 #define	i2c_transfer		linux_i2c_transfer
    205 #define	i2c_unregister_device	linux_i2c_unregister_device
    206 
    207 int	i2c_master_send(const struct i2c_client *, const char *, int);
    208 int	i2c_master_recv(const struct i2c_client *, char *, int);
    209 struct i2c_client *
    210 	i2c_new_device(struct i2c_adapter *, const struct i2c_board_info *);
    211 int	i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int);
    212 void	i2c_unregister_device(struct i2c_client *);
    213 
    214 #endif  /* _LINUX_I2C_H_ */
    215