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