xref: /OK3568_Linux_fs/kernel/include/linux/i2c.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * i2c.h - definitions for the Linux i2c bus interface
4*4882a593Smuzhiyun  * Copyright (C) 1995-2000 Simon G. Vogl
5*4882a593Smuzhiyun  * Copyright (C) 2013-2019 Wolfram Sang <wsa@kernel.org>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
8*4882a593Smuzhiyun  * Frodo Looijaard <frodol@dds.nl>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun #ifndef _LINUX_I2C_H
11*4882a593Smuzhiyun #define _LINUX_I2C_H
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/acpi.h>		/* for acpi_handle */
14*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
15*4882a593Smuzhiyun #include <linux/device.h>	/* for struct device */
16*4882a593Smuzhiyun #include <linux/sched.h>	/* for completion */
17*4882a593Smuzhiyun #include <linux/mutex.h>
18*4882a593Smuzhiyun #include <linux/rtmutex.h>
19*4882a593Smuzhiyun #include <linux/irqdomain.h>		/* for Host Notify IRQ */
20*4882a593Smuzhiyun #include <linux/of.h>		/* for struct device_node */
21*4882a593Smuzhiyun #include <linux/swab.h>		/* for swab16 */
22*4882a593Smuzhiyun #include <uapi/linux/i2c.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun extern struct bus_type i2c_bus_type;
25*4882a593Smuzhiyun extern struct device_type i2c_adapter_type;
26*4882a593Smuzhiyun extern struct device_type i2c_client_type;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* --- General options ------------------------------------------------	*/
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun struct i2c_msg;
31*4882a593Smuzhiyun struct i2c_algorithm;
32*4882a593Smuzhiyun struct i2c_adapter;
33*4882a593Smuzhiyun struct i2c_client;
34*4882a593Smuzhiyun struct i2c_driver;
35*4882a593Smuzhiyun struct i2c_device_identity;
36*4882a593Smuzhiyun union i2c_smbus_data;
37*4882a593Smuzhiyun struct i2c_board_info;
38*4882a593Smuzhiyun enum i2c_slave_event;
39*4882a593Smuzhiyun typedef int (*i2c_slave_cb_t)(struct i2c_client *client,
40*4882a593Smuzhiyun 			      enum i2c_slave_event event, u8 *val);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* I2C Frequency Modes */
43*4882a593Smuzhiyun #define I2C_MAX_STANDARD_MODE_FREQ	100000
44*4882a593Smuzhiyun #define I2C_MAX_FAST_MODE_FREQ		400000
45*4882a593Smuzhiyun #define I2C_MAX_FAST_MODE_PLUS_FREQ	1000000
46*4882a593Smuzhiyun #define I2C_MAX_TURBO_MODE_FREQ		1400000
47*4882a593Smuzhiyun #define I2C_MAX_HIGH_SPEED_MODE_FREQ	3400000
48*4882a593Smuzhiyun #define I2C_MAX_ULTRA_FAST_MODE_FREQ	5000000
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun struct module;
51*4882a593Smuzhiyun struct property_entry;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C)
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * The master routines are the ones normally used to transmit data to devices
56*4882a593Smuzhiyun  * on a bus (or read from them). Apart from two basic transfer functions to
57*4882a593Smuzhiyun  * transmit one message at a time, a more complex version can be used to
58*4882a593Smuzhiyun  * transmit an arbitrary number of messages without interruption.
59*4882a593Smuzhiyun  * @count must be less than 64k since msg.len is u16.
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun int i2c_transfer_buffer_flags(const struct i2c_client *client,
62*4882a593Smuzhiyun 			      char *buf, int count, u16 flags);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun  * i2c_master_recv - issue a single I2C message in master receive mode
66*4882a593Smuzhiyun  * @client: Handle to slave device
67*4882a593Smuzhiyun  * @buf: Where to store data read from slave
68*4882a593Smuzhiyun  * @count: How many bytes to read, must be less than 64k since msg.len is u16
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Returns negative errno, or else the number of bytes read.
71*4882a593Smuzhiyun  */
i2c_master_recv(const struct i2c_client * client,char * buf,int count)72*4882a593Smuzhiyun static inline int i2c_master_recv(const struct i2c_client *client,
73*4882a593Smuzhiyun 				  char *buf, int count)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun  * i2c_master_recv_dmasafe - issue a single I2C message in master receive mode
80*4882a593Smuzhiyun  *			     using a DMA safe buffer
81*4882a593Smuzhiyun  * @client: Handle to slave device
82*4882a593Smuzhiyun  * @buf: Where to store data read from slave, must be safe to use with DMA
83*4882a593Smuzhiyun  * @count: How many bytes to read, must be less than 64k since msg.len is u16
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * Returns negative errno, or else the number of bytes read.
86*4882a593Smuzhiyun  */
i2c_master_recv_dmasafe(const struct i2c_client * client,char * buf,int count)87*4882a593Smuzhiyun static inline int i2c_master_recv_dmasafe(const struct i2c_client *client,
88*4882a593Smuzhiyun 					  char *buf, int count)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return i2c_transfer_buffer_flags(client, buf, count,
91*4882a593Smuzhiyun 					 I2C_M_RD | I2C_M_DMA_SAFE);
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun  * i2c_master_send - issue a single I2C message in master transmit mode
96*4882a593Smuzhiyun  * @client: Handle to slave device
97*4882a593Smuzhiyun  * @buf: Data that will be written to the slave
98*4882a593Smuzhiyun  * @count: How many bytes to write, must be less than 64k since msg.len is u16
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * Returns negative errno, or else the number of bytes written.
101*4882a593Smuzhiyun  */
i2c_master_send(const struct i2c_client * client,const char * buf,int count)102*4882a593Smuzhiyun static inline int i2c_master_send(const struct i2c_client *client,
103*4882a593Smuzhiyun 				  const char *buf, int count)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	return i2c_transfer_buffer_flags(client, (char *)buf, count, 0);
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun  * i2c_master_send_dmasafe - issue a single I2C message in master transmit mode
110*4882a593Smuzhiyun  *			     using a DMA safe buffer
111*4882a593Smuzhiyun  * @client: Handle to slave device
112*4882a593Smuzhiyun  * @buf: Data that will be written to the slave, must be safe to use with DMA
113*4882a593Smuzhiyun  * @count: How many bytes to write, must be less than 64k since msg.len is u16
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * Returns negative errno, or else the number of bytes written.
116*4882a593Smuzhiyun  */
i2c_master_send_dmasafe(const struct i2c_client * client,const char * buf,int count)117*4882a593Smuzhiyun static inline int i2c_master_send_dmasafe(const struct i2c_client *client,
118*4882a593Smuzhiyun 					  const char *buf, int count)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	return i2c_transfer_buffer_flags(client, (char *)buf, count,
121*4882a593Smuzhiyun 					 I2C_M_DMA_SAFE);
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* Transfer num messages.
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
127*4882a593Smuzhiyun /* Unlocked flavor */
128*4882a593Smuzhiyun int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /* This is the very generalized SMBus access routine. You probably do not
131*4882a593Smuzhiyun    want to use this, though; one of the functions below may be much easier,
132*4882a593Smuzhiyun    and probably just as fast.
133*4882a593Smuzhiyun    Note that we use i2c_adapter here, because you do not need a specific
134*4882a593Smuzhiyun    smbus adapter to call this function. */
135*4882a593Smuzhiyun s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
136*4882a593Smuzhiyun 		   unsigned short flags, char read_write, u8 command,
137*4882a593Smuzhiyun 		   int protocol, union i2c_smbus_data *data);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* Unlocked flavor */
140*4882a593Smuzhiyun s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
141*4882a593Smuzhiyun 		     unsigned short flags, char read_write, u8 command,
142*4882a593Smuzhiyun 		     int protocol, union i2c_smbus_data *data);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /* Now follow the 'nice' access routines. These also document the calling
145*4882a593Smuzhiyun    conventions of i2c_smbus_xfer. */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun s32 i2c_smbus_read_byte(const struct i2c_client *client);
148*4882a593Smuzhiyun s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value);
149*4882a593Smuzhiyun s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command);
150*4882a593Smuzhiyun s32 i2c_smbus_write_byte_data(const struct i2c_client *client,
151*4882a593Smuzhiyun 			      u8 command, u8 value);
152*4882a593Smuzhiyun s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command);
153*4882a593Smuzhiyun s32 i2c_smbus_write_word_data(const struct i2c_client *client,
154*4882a593Smuzhiyun 			      u8 command, u16 value);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun static inline s32
i2c_smbus_read_word_swapped(const struct i2c_client * client,u8 command)157*4882a593Smuzhiyun i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	s32 value = i2c_smbus_read_word_data(client, command);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	return (value < 0) ? value : swab16(value);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun static inline s32
i2c_smbus_write_word_swapped(const struct i2c_client * client,u8 command,u16 value)165*4882a593Smuzhiyun i2c_smbus_write_word_swapped(const struct i2c_client *client,
166*4882a593Smuzhiyun 			     u8 command, u16 value)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	return i2c_smbus_write_word_data(client, command, swab16(value));
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* Returns the number of read bytes */
172*4882a593Smuzhiyun s32 i2c_smbus_read_block_data(const struct i2c_client *client,
173*4882a593Smuzhiyun 			      u8 command, u8 *values);
174*4882a593Smuzhiyun s32 i2c_smbus_write_block_data(const struct i2c_client *client,
175*4882a593Smuzhiyun 			       u8 command, u8 length, const u8 *values);
176*4882a593Smuzhiyun /* Returns the number of read bytes */
177*4882a593Smuzhiyun s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
178*4882a593Smuzhiyun 				  u8 command, u8 length, u8 *values);
179*4882a593Smuzhiyun s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
180*4882a593Smuzhiyun 				   u8 command, u8 length, const u8 *values);
181*4882a593Smuzhiyun s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
182*4882a593Smuzhiyun 					      u8 command, u8 length,
183*4882a593Smuzhiyun 					      u8 *values);
184*4882a593Smuzhiyun int i2c_get_device_id(const struct i2c_client *client,
185*4882a593Smuzhiyun 		      struct i2c_device_identity *id);
186*4882a593Smuzhiyun #endif /* I2C */
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun  * struct i2c_device_identity - i2c client device identification
190*4882a593Smuzhiyun  * @manufacturer_id: 0 - 4095, database maintained by NXP
191*4882a593Smuzhiyun  * @part_id: 0 - 511, according to manufacturer
192*4882a593Smuzhiyun  * @die_revision: 0 - 7, according to manufacturer
193*4882a593Smuzhiyun  */
194*4882a593Smuzhiyun struct i2c_device_identity {
195*4882a593Smuzhiyun 	u16 manufacturer_id;
196*4882a593Smuzhiyun #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS                0
197*4882a593Smuzhiyun #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_1              1
198*4882a593Smuzhiyun #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_2              2
199*4882a593Smuzhiyun #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_3              3
200*4882a593Smuzhiyun #define I2C_DEVICE_ID_RAMTRON_INTERNATIONAL             4
201*4882a593Smuzhiyun #define I2C_DEVICE_ID_ANALOG_DEVICES                    5
202*4882a593Smuzhiyun #define I2C_DEVICE_ID_STMICROELECTRONICS                6
203*4882a593Smuzhiyun #define I2C_DEVICE_ID_ON_SEMICONDUCTOR                  7
204*4882a593Smuzhiyun #define I2C_DEVICE_ID_SPRINTEK_CORPORATION              8
205*4882a593Smuzhiyun #define I2C_DEVICE_ID_ESPROS_PHOTONICS_AG               9
206*4882a593Smuzhiyun #define I2C_DEVICE_ID_FUJITSU_SEMICONDUCTOR            10
207*4882a593Smuzhiyun #define I2C_DEVICE_ID_FLIR                             11
208*4882a593Smuzhiyun #define I2C_DEVICE_ID_O2MICRO                          12
209*4882a593Smuzhiyun #define I2C_DEVICE_ID_ATMEL                            13
210*4882a593Smuzhiyun #define I2C_DEVICE_ID_NONE                         0xffff
211*4882a593Smuzhiyun 	u16 part_id;
212*4882a593Smuzhiyun 	u8 die_revision;
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun enum i2c_alert_protocol {
216*4882a593Smuzhiyun 	I2C_PROTOCOL_SMBUS_ALERT,
217*4882a593Smuzhiyun 	I2C_PROTOCOL_SMBUS_HOST_NOTIFY,
218*4882a593Smuzhiyun };
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun  * struct i2c_driver - represent an I2C device driver
222*4882a593Smuzhiyun  * @class: What kind of i2c device we instantiate (for detect)
223*4882a593Smuzhiyun  * @probe: Callback for device binding - soon to be deprecated
224*4882a593Smuzhiyun  * @probe_new: New callback for device binding
225*4882a593Smuzhiyun  * @remove: Callback for device unbinding
226*4882a593Smuzhiyun  * @shutdown: Callback for device shutdown
227*4882a593Smuzhiyun  * @alert: Alert callback, for example for the SMBus alert protocol
228*4882a593Smuzhiyun  * @command: Callback for bus-wide signaling (optional)
229*4882a593Smuzhiyun  * @driver: Device driver model driver
230*4882a593Smuzhiyun  * @id_table: List of I2C devices supported by this driver
231*4882a593Smuzhiyun  * @detect: Callback for device detection
232*4882a593Smuzhiyun  * @address_list: The I2C addresses to probe (for detect)
233*4882a593Smuzhiyun  * @clients: List of detected clients we created (for i2c-core use only)
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * The driver.owner field should be set to the module owner of this driver.
236*4882a593Smuzhiyun  * The driver.name field should be set to the name of this driver.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * For automatic device detection, both @detect and @address_list must
239*4882a593Smuzhiyun  * be defined. @class should also be set, otherwise only devices forced
240*4882a593Smuzhiyun  * with module parameters will be created. The detect function must
241*4882a593Smuzhiyun  * fill at least the name field of the i2c_board_info structure it is
242*4882a593Smuzhiyun  * handed upon successful detection, and possibly also the flags field.
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * If @detect is missing, the driver will still work fine for enumerated
245*4882a593Smuzhiyun  * devices. Detected devices simply won't be supported. This is expected
246*4882a593Smuzhiyun  * for the many I2C/SMBus devices which can't be detected reliably, and
247*4882a593Smuzhiyun  * the ones which can always be enumerated in practice.
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  * The i2c_client structure which is handed to the @detect callback is
250*4882a593Smuzhiyun  * not a real i2c_client. It is initialized just enough so that you can
251*4882a593Smuzhiyun  * call i2c_smbus_read_byte_data and friends on it. Don't do anything
252*4882a593Smuzhiyun  * else with it. In particular, calling dev_dbg and friends on it is
253*4882a593Smuzhiyun  * not allowed.
254*4882a593Smuzhiyun  */
255*4882a593Smuzhiyun struct i2c_driver {
256*4882a593Smuzhiyun 	unsigned int class;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* Standard driver model interfaces */
259*4882a593Smuzhiyun 	int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
260*4882a593Smuzhiyun 	int (*remove)(struct i2c_client *client);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* New driver model interface to aid the seamless removal of the
263*4882a593Smuzhiyun 	 * current probe()'s, more commonly unused than used second parameter.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	int (*probe_new)(struct i2c_client *client);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/* driver model interfaces that don't relate to enumeration  */
268*4882a593Smuzhiyun 	void (*shutdown)(struct i2c_client *client);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* Alert callback, for example for the SMBus alert protocol.
271*4882a593Smuzhiyun 	 * The format and meaning of the data value depends on the protocol.
272*4882a593Smuzhiyun 	 * For the SMBus alert protocol, there is a single bit of data passed
273*4882a593Smuzhiyun 	 * as the alert response's low bit ("event flag").
274*4882a593Smuzhiyun 	 * For the SMBus Host Notify protocol, the data corresponds to the
275*4882a593Smuzhiyun 	 * 16-bit payload data reported by the slave device acting as master.
276*4882a593Smuzhiyun 	 */
277*4882a593Smuzhiyun 	void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
278*4882a593Smuzhiyun 		      unsigned int data);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* a ioctl like command that can be used to perform specific functions
281*4882a593Smuzhiyun 	 * with the device.
282*4882a593Smuzhiyun 	 */
283*4882a593Smuzhiyun 	int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	struct device_driver driver;
286*4882a593Smuzhiyun 	const struct i2c_device_id *id_table;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/* Device detection callback for automatic device creation */
289*4882a593Smuzhiyun 	int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
290*4882a593Smuzhiyun 	const unsigned short *address_list;
291*4882a593Smuzhiyun 	struct list_head clients;
292*4882a593Smuzhiyun };
293*4882a593Smuzhiyun #define to_i2c_driver(d) container_of(d, struct i2c_driver, driver)
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun  * struct i2c_client - represent an I2C slave device
297*4882a593Smuzhiyun  * @flags: see I2C_CLIENT_* for possible flags
298*4882a593Smuzhiyun  * @addr: Address used on the I2C bus connected to the parent adapter.
299*4882a593Smuzhiyun  * @name: Indicates the type of the device, usually a chip name that's
300*4882a593Smuzhiyun  *	generic enough to hide second-sourcing and compatible revisions.
301*4882a593Smuzhiyun  * @adapter: manages the bus segment hosting this I2C device
302*4882a593Smuzhiyun  * @dev: Driver model device node for the slave.
303*4882a593Smuzhiyun  * @init_irq: IRQ that was set at initialization
304*4882a593Smuzhiyun  * @irq: indicates the IRQ generated by this device (if any)
305*4882a593Smuzhiyun  * @detected: member of an i2c_driver.clients list or i2c-core's
306*4882a593Smuzhiyun  *	userspace_devices list
307*4882a593Smuzhiyun  * @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
308*4882a593Smuzhiyun  *	calls it to pass on slave events to the slave driver.
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * An i2c_client identifies a single device (i.e. chip) connected to an
311*4882a593Smuzhiyun  * i2c bus. The behaviour exposed to Linux is defined by the driver
312*4882a593Smuzhiyun  * managing the device.
313*4882a593Smuzhiyun  */
314*4882a593Smuzhiyun struct i2c_client {
315*4882a593Smuzhiyun 	unsigned short flags;		/* div., see below		*/
316*4882a593Smuzhiyun #define I2C_CLIENT_PEC		0x04	/* Use Packet Error Checking */
317*4882a593Smuzhiyun #define I2C_CLIENT_TEN		0x10	/* we have a ten bit chip address */
318*4882a593Smuzhiyun 					/* Must equal I2C_M_TEN below */
319*4882a593Smuzhiyun #define I2C_CLIENT_SLAVE	0x20	/* we are the slave */
320*4882a593Smuzhiyun #define I2C_CLIENT_HOST_NOTIFY	0x40	/* We want to use I2C host notify */
321*4882a593Smuzhiyun #define I2C_CLIENT_WAKE		0x80	/* for board_info; true iff can wake */
322*4882a593Smuzhiyun #define I2C_CLIENT_SCCB		0x9000	/* Use Omnivision SCCB protocol */
323*4882a593Smuzhiyun 					/* Must match I2C_M_STOP|IGNORE_NAK */
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	unsigned short addr;		/* chip address - NOTE: 7bit	*/
326*4882a593Smuzhiyun 					/* addresses are stored in the	*/
327*4882a593Smuzhiyun 					/* _LOWER_ 7 bits		*/
328*4882a593Smuzhiyun 	char name[I2C_NAME_SIZE];
329*4882a593Smuzhiyun 	struct i2c_adapter *adapter;	/* the adapter we sit on	*/
330*4882a593Smuzhiyun 	struct device dev;		/* the device structure		*/
331*4882a593Smuzhiyun 	int init_irq;			/* irq set at initialization	*/
332*4882a593Smuzhiyun 	int irq;			/* irq issued by device		*/
333*4882a593Smuzhiyun 	struct list_head detected;
334*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C_SLAVE)
335*4882a593Smuzhiyun 	i2c_slave_cb_t slave_cb;	/* callback for slave mode	*/
336*4882a593Smuzhiyun #endif
337*4882a593Smuzhiyun };
338*4882a593Smuzhiyun #define to_i2c_client(d) container_of(d, struct i2c_client, dev)
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun struct i2c_client *i2c_verify_client(struct device *dev);
341*4882a593Smuzhiyun struct i2c_adapter *i2c_verify_adapter(struct device *dev);
342*4882a593Smuzhiyun const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
343*4882a593Smuzhiyun 					 const struct i2c_client *client);
344*4882a593Smuzhiyun 
kobj_to_i2c_client(struct kobject * kobj)345*4882a593Smuzhiyun static inline struct i2c_client *kobj_to_i2c_client(struct kobject *kobj)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct device * const dev = kobj_to_dev(kobj);
348*4882a593Smuzhiyun 	return to_i2c_client(dev);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
i2c_get_clientdata(const struct i2c_client * client)351*4882a593Smuzhiyun static inline void *i2c_get_clientdata(const struct i2c_client *client)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	return dev_get_drvdata(&client->dev);
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
i2c_set_clientdata(struct i2c_client * client,void * data)356*4882a593Smuzhiyun static inline void i2c_set_clientdata(struct i2c_client *client, void *data)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	dev_set_drvdata(&client->dev, data);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /* I2C slave support */
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C_SLAVE)
364*4882a593Smuzhiyun enum i2c_slave_event {
365*4882a593Smuzhiyun 	I2C_SLAVE_READ_REQUESTED,
366*4882a593Smuzhiyun 	I2C_SLAVE_WRITE_REQUESTED,
367*4882a593Smuzhiyun 	I2C_SLAVE_READ_PROCESSED,
368*4882a593Smuzhiyun 	I2C_SLAVE_WRITE_RECEIVED,
369*4882a593Smuzhiyun 	I2C_SLAVE_STOP,
370*4882a593Smuzhiyun };
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb);
373*4882a593Smuzhiyun int i2c_slave_unregister(struct i2c_client *client);
374*4882a593Smuzhiyun bool i2c_detect_slave_mode(struct device *dev);
375*4882a593Smuzhiyun 
i2c_slave_event(struct i2c_client * client,enum i2c_slave_event event,u8 * val)376*4882a593Smuzhiyun static inline int i2c_slave_event(struct i2c_client *client,
377*4882a593Smuzhiyun 				  enum i2c_slave_event event, u8 *val)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	return client->slave_cb(client, event, val);
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun #else
i2c_detect_slave_mode(struct device * dev)382*4882a593Smuzhiyun static inline bool i2c_detect_slave_mode(struct device *dev) { return false; }
383*4882a593Smuzhiyun #endif
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun  * struct i2c_board_info - template for device creation
387*4882a593Smuzhiyun  * @type: chip type, to initialize i2c_client.name
388*4882a593Smuzhiyun  * @flags: to initialize i2c_client.flags
389*4882a593Smuzhiyun  * @addr: stored in i2c_client.addr
390*4882a593Smuzhiyun  * @dev_name: Overrides the default <busnr>-<addr> dev_name if set
391*4882a593Smuzhiyun  * @platform_data: stored in i2c_client.dev.platform_data
392*4882a593Smuzhiyun  * @of_node: pointer to OpenFirmware device node
393*4882a593Smuzhiyun  * @fwnode: device node supplied by the platform firmware
394*4882a593Smuzhiyun  * @properties: additional device properties for the device
395*4882a593Smuzhiyun  * @resources: resources associated with the device
396*4882a593Smuzhiyun  * @num_resources: number of resources in the @resources array
397*4882a593Smuzhiyun  * @irq: stored in i2c_client.irq
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * I2C doesn't actually support hardware probing, although controllers and
400*4882a593Smuzhiyun  * devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's
401*4882a593Smuzhiyun  * a device at a given address.  Drivers commonly need more information than
402*4882a593Smuzhiyun  * that, such as chip type, configuration, associated IRQ, and so on.
403*4882a593Smuzhiyun  *
404*4882a593Smuzhiyun  * i2c_board_info is used to build tables of information listing I2C devices
405*4882a593Smuzhiyun  * that are present.  This information is used to grow the driver model tree.
406*4882a593Smuzhiyun  * For mainboards this is done statically using i2c_register_board_info();
407*4882a593Smuzhiyun  * bus numbers identify adapters that aren't yet available.  For add-on boards,
408*4882a593Smuzhiyun  * i2c_new_client_device() does this dynamically with the adapter already known.
409*4882a593Smuzhiyun  */
410*4882a593Smuzhiyun struct i2c_board_info {
411*4882a593Smuzhiyun 	char		type[I2C_NAME_SIZE];
412*4882a593Smuzhiyun 	unsigned short	flags;
413*4882a593Smuzhiyun 	unsigned short	addr;
414*4882a593Smuzhiyun 	const char	*dev_name;
415*4882a593Smuzhiyun 	void		*platform_data;
416*4882a593Smuzhiyun 	struct device_node *of_node;
417*4882a593Smuzhiyun 	struct fwnode_handle *fwnode;
418*4882a593Smuzhiyun 	const struct property_entry *properties;
419*4882a593Smuzhiyun 	const struct resource *resources;
420*4882a593Smuzhiyun 	unsigned int	num_resources;
421*4882a593Smuzhiyun 	int		irq;
422*4882a593Smuzhiyun };
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun /**
425*4882a593Smuzhiyun  * I2C_BOARD_INFO - macro used to list an i2c device and its address
426*4882a593Smuzhiyun  * @dev_type: identifies the device type
427*4882a593Smuzhiyun  * @dev_addr: the device's address on the bus.
428*4882a593Smuzhiyun  *
429*4882a593Smuzhiyun  * This macro initializes essential fields of a struct i2c_board_info,
430*4882a593Smuzhiyun  * declaring what has been provided on a particular board.  Optional
431*4882a593Smuzhiyun  * fields (such as associated irq, or device-specific platform_data)
432*4882a593Smuzhiyun  * are provided using conventional syntax.
433*4882a593Smuzhiyun  */
434*4882a593Smuzhiyun #define I2C_BOARD_INFO(dev_type, dev_addr) \
435*4882a593Smuzhiyun 	.type = dev_type, .addr = (dev_addr)
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C)
439*4882a593Smuzhiyun /*
440*4882a593Smuzhiyun  * Add-on boards should register/unregister their devices; e.g. a board
441*4882a593Smuzhiyun  * with integrated I2C, a config eeprom, sensors, and a codec that's
442*4882a593Smuzhiyun  * used in conjunction with the primary hardware.
443*4882a593Smuzhiyun  */
444*4882a593Smuzhiyun struct i2c_client *
445*4882a593Smuzhiyun i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun /* If you don't know the exact address of an I2C device, use this variant
448*4882a593Smuzhiyun  * instead, which can probe for device presence in a list of possible
449*4882a593Smuzhiyun  * addresses. The "probe" callback function is optional. If it is provided,
450*4882a593Smuzhiyun  * it must return 1 on successful probe, 0 otherwise. If it is not provided,
451*4882a593Smuzhiyun  * a default probing method is used.
452*4882a593Smuzhiyun  */
453*4882a593Smuzhiyun struct i2c_client *
454*4882a593Smuzhiyun i2c_new_scanned_device(struct i2c_adapter *adap,
455*4882a593Smuzhiyun 		       struct i2c_board_info *info,
456*4882a593Smuzhiyun 		       unsigned short const *addr_list,
457*4882a593Smuzhiyun 		       int (*probe)(struct i2c_adapter *adap, unsigned short addr));
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /* Common custom probe functions */
460*4882a593Smuzhiyun int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun struct i2c_client *
463*4882a593Smuzhiyun i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun struct i2c_client *
466*4882a593Smuzhiyun devm_i2c_new_dummy_device(struct device *dev, struct i2c_adapter *adap, u16 address);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun struct i2c_client *
469*4882a593Smuzhiyun i2c_new_ancillary_device(struct i2c_client *client,
470*4882a593Smuzhiyun 			 const char *name,
471*4882a593Smuzhiyun 			 u16 default_addr);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun void i2c_unregister_device(struct i2c_client *client);
474*4882a593Smuzhiyun #endif /* I2C */
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun /* Mainboard arch_initcall() code should register all its I2C devices.
477*4882a593Smuzhiyun  * This is done at arch_initcall time, before declaring any i2c adapters.
478*4882a593Smuzhiyun  * Modules for add-on boards must use other calls.
479*4882a593Smuzhiyun  */
480*4882a593Smuzhiyun #ifdef CONFIG_I2C_BOARDINFO
481*4882a593Smuzhiyun int
482*4882a593Smuzhiyun i2c_register_board_info(int busnum, struct i2c_board_info const *info,
483*4882a593Smuzhiyun 			unsigned n);
484*4882a593Smuzhiyun #else
485*4882a593Smuzhiyun static inline int
i2c_register_board_info(int busnum,struct i2c_board_info const * info,unsigned n)486*4882a593Smuzhiyun i2c_register_board_info(int busnum, struct i2c_board_info const *info,
487*4882a593Smuzhiyun 			unsigned n)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	return 0;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun #endif /* I2C_BOARDINFO */
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun /**
494*4882a593Smuzhiyun  * struct i2c_algorithm - represent I2C transfer method
495*4882a593Smuzhiyun  * @master_xfer: Issue a set of i2c transactions to the given I2C adapter
496*4882a593Smuzhiyun  *   defined by the msgs array, with num messages available to transfer via
497*4882a593Smuzhiyun  *   the adapter specified by adap.
498*4882a593Smuzhiyun  * @master_xfer_atomic: same as @master_xfer. Yet, only using atomic context
499*4882a593Smuzhiyun  *   so e.g. PMICs can be accessed very late before shutdown. Optional.
500*4882a593Smuzhiyun  * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this
501*4882a593Smuzhiyun  *   is not present, then the bus layer will try and convert the SMBus calls
502*4882a593Smuzhiyun  *   into I2C transfers instead.
503*4882a593Smuzhiyun  * @smbus_xfer_atomic: same as @smbus_xfer. Yet, only using atomic context
504*4882a593Smuzhiyun  *   so e.g. PMICs can be accessed very late before shutdown. Optional.
505*4882a593Smuzhiyun  * @functionality: Return the flags that this algorithm/adapter pair supports
506*4882a593Smuzhiyun  *   from the ``I2C_FUNC_*`` flags.
507*4882a593Smuzhiyun  * @reg_slave: Register given client to I2C slave mode of this adapter
508*4882a593Smuzhiyun  * @unreg_slave: Unregister given client from I2C slave mode of this adapter
509*4882a593Smuzhiyun  *
510*4882a593Smuzhiyun  * The following structs are for those who like to implement new bus drivers:
511*4882a593Smuzhiyun  * i2c_algorithm is the interface to a class of hardware solutions which can
512*4882a593Smuzhiyun  * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
513*4882a593Smuzhiyun  * to name two of the most common.
514*4882a593Smuzhiyun  *
515*4882a593Smuzhiyun  * The return codes from the ``master_xfer{_atomic}`` fields should indicate the
516*4882a593Smuzhiyun  * type of error code that occurred during the transfer, as documented in the
517*4882a593Smuzhiyun  * Kernel Documentation file Documentation/i2c/fault-codes.rst.
518*4882a593Smuzhiyun  */
519*4882a593Smuzhiyun struct i2c_algorithm {
520*4882a593Smuzhiyun 	/*
521*4882a593Smuzhiyun 	 * If an adapter algorithm can't do I2C-level access, set master_xfer
522*4882a593Smuzhiyun 	 * to NULL. If an adapter algorithm can do SMBus access, set
523*4882a593Smuzhiyun 	 * smbus_xfer. If set to NULL, the SMBus protocol is simulated
524*4882a593Smuzhiyun 	 * using common I2C messages.
525*4882a593Smuzhiyun 	 *
526*4882a593Smuzhiyun 	 * master_xfer should return the number of messages successfully
527*4882a593Smuzhiyun 	 * processed, or a negative value on error
528*4882a593Smuzhiyun 	 */
529*4882a593Smuzhiyun 	int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
530*4882a593Smuzhiyun 			   int num);
531*4882a593Smuzhiyun 	int (*master_xfer_atomic)(struct i2c_adapter *adap,
532*4882a593Smuzhiyun 				   struct i2c_msg *msgs, int num);
533*4882a593Smuzhiyun 	int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
534*4882a593Smuzhiyun 			  unsigned short flags, char read_write,
535*4882a593Smuzhiyun 			  u8 command, int size, union i2c_smbus_data *data);
536*4882a593Smuzhiyun 	int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,
537*4882a593Smuzhiyun 				 unsigned short flags, char read_write,
538*4882a593Smuzhiyun 				 u8 command, int size, union i2c_smbus_data *data);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/* To determine what the adapter supports */
541*4882a593Smuzhiyun 	u32 (*functionality)(struct i2c_adapter *adap);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C_SLAVE)
544*4882a593Smuzhiyun 	int (*reg_slave)(struct i2c_client *client);
545*4882a593Smuzhiyun 	int (*unreg_slave)(struct i2c_client *client);
546*4882a593Smuzhiyun #endif
547*4882a593Smuzhiyun };
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun  * struct i2c_lock_operations - represent I2C locking operations
551*4882a593Smuzhiyun  * @lock_bus: Get exclusive access to an I2C bus segment
552*4882a593Smuzhiyun  * @trylock_bus: Try to get exclusive access to an I2C bus segment
553*4882a593Smuzhiyun  * @unlock_bus: Release exclusive access to an I2C bus segment
554*4882a593Smuzhiyun  *
555*4882a593Smuzhiyun  * The main operations are wrapped by i2c_lock_bus and i2c_unlock_bus.
556*4882a593Smuzhiyun  */
557*4882a593Smuzhiyun struct i2c_lock_operations {
558*4882a593Smuzhiyun 	void (*lock_bus)(struct i2c_adapter *adapter, unsigned int flags);
559*4882a593Smuzhiyun 	int (*trylock_bus)(struct i2c_adapter *adapter, unsigned int flags);
560*4882a593Smuzhiyun 	void (*unlock_bus)(struct i2c_adapter *adapter, unsigned int flags);
561*4882a593Smuzhiyun };
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun  * struct i2c_timings - I2C timing information
565*4882a593Smuzhiyun  * @bus_freq_hz: the bus frequency in Hz
566*4882a593Smuzhiyun  * @scl_rise_ns: time SCL signal takes to rise in ns; t(r) in the I2C specification
567*4882a593Smuzhiyun  * @scl_fall_ns: time SCL signal takes to fall in ns; t(f) in the I2C specification
568*4882a593Smuzhiyun  * @scl_int_delay_ns: time IP core additionally needs to setup SCL in ns
569*4882a593Smuzhiyun  * @sda_fall_ns: time SDA signal takes to fall in ns; t(f) in the I2C specification
570*4882a593Smuzhiyun  * @sda_hold_ns: time IP core additionally needs to hold SDA in ns
571*4882a593Smuzhiyun  * @digital_filter_width_ns: width in ns of spikes on i2c lines that the IP core
572*4882a593Smuzhiyun  *	digital filter can filter out
573*4882a593Smuzhiyun  * @analog_filter_cutoff_freq_hz: threshold frequency for the low pass IP core
574*4882a593Smuzhiyun  *	analog filter
575*4882a593Smuzhiyun  */
576*4882a593Smuzhiyun struct i2c_timings {
577*4882a593Smuzhiyun 	u32 bus_freq_hz;
578*4882a593Smuzhiyun 	u32 scl_rise_ns;
579*4882a593Smuzhiyun 	u32 scl_fall_ns;
580*4882a593Smuzhiyun 	u32 scl_int_delay_ns;
581*4882a593Smuzhiyun 	u32 sda_fall_ns;
582*4882a593Smuzhiyun 	u32 sda_hold_ns;
583*4882a593Smuzhiyun 	u32 digital_filter_width_ns;
584*4882a593Smuzhiyun 	u32 analog_filter_cutoff_freq_hz;
585*4882a593Smuzhiyun };
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /**
588*4882a593Smuzhiyun  * struct i2c_bus_recovery_info - I2C bus recovery information
589*4882a593Smuzhiyun  * @recover_bus: Recover routine. Either pass driver's recover_bus() routine, or
590*4882a593Smuzhiyun  *	i2c_generic_scl_recovery().
591*4882a593Smuzhiyun  * @get_scl: This gets current value of SCL line. Mandatory for generic SCL
592*4882a593Smuzhiyun  *      recovery. Populated internally for generic GPIO recovery.
593*4882a593Smuzhiyun  * @set_scl: This sets/clears the SCL line. Mandatory for generic SCL recovery.
594*4882a593Smuzhiyun  *      Populated internally for generic GPIO recovery.
595*4882a593Smuzhiyun  * @get_sda: This gets current value of SDA line. This or set_sda() is mandatory
596*4882a593Smuzhiyun  *	for generic SCL recovery. Populated internally, if sda_gpio is a valid
597*4882a593Smuzhiyun  *	GPIO, for generic GPIO recovery.
598*4882a593Smuzhiyun  * @set_sda: This sets/clears the SDA line. This or get_sda() is mandatory for
599*4882a593Smuzhiyun  *	generic SCL recovery. Populated internally, if sda_gpio is a valid GPIO,
600*4882a593Smuzhiyun  *	for generic GPIO recovery.
601*4882a593Smuzhiyun  * @get_bus_free: Returns the bus free state as seen from the IP core in case it
602*4882a593Smuzhiyun  *	has a more complex internal logic than just reading SDA. Optional.
603*4882a593Smuzhiyun  * @prepare_recovery: This will be called before starting recovery. Platform may
604*4882a593Smuzhiyun  *	configure padmux here for SDA/SCL line or something else they want.
605*4882a593Smuzhiyun  * @unprepare_recovery: This will be called after completing recovery. Platform
606*4882a593Smuzhiyun  *	may configure padmux here for SDA/SCL line or something else they want.
607*4882a593Smuzhiyun  * @scl_gpiod: gpiod of the SCL line. Only required for GPIO recovery.
608*4882a593Smuzhiyun  * @sda_gpiod: gpiod of the SDA line. Only required for GPIO recovery.
609*4882a593Smuzhiyun  * @pinctrl: pinctrl used by GPIO recovery to change the state of the I2C pins.
610*4882a593Smuzhiyun  *      Optional.
611*4882a593Smuzhiyun  * @pins_default: default pinctrl state of SCL/SDA lines, when they are assigned
612*4882a593Smuzhiyun  *      to the I2C bus. Optional. Populated internally for GPIO recovery, if
613*4882a593Smuzhiyun  *      state with the name PINCTRL_STATE_DEFAULT is found and pinctrl is valid.
614*4882a593Smuzhiyun  * @pins_gpio: recovery pinctrl state of SCL/SDA lines, when they are used as
615*4882a593Smuzhiyun  *      GPIOs. Optional. Populated internally for GPIO recovery, if this state
616*4882a593Smuzhiyun  *      is called "gpio" or "recovery" and pinctrl is valid.
617*4882a593Smuzhiyun  */
618*4882a593Smuzhiyun struct i2c_bus_recovery_info {
619*4882a593Smuzhiyun 	int (*recover_bus)(struct i2c_adapter *adap);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	int (*get_scl)(struct i2c_adapter *adap);
622*4882a593Smuzhiyun 	void (*set_scl)(struct i2c_adapter *adap, int val);
623*4882a593Smuzhiyun 	int (*get_sda)(struct i2c_adapter *adap);
624*4882a593Smuzhiyun 	void (*set_sda)(struct i2c_adapter *adap, int val);
625*4882a593Smuzhiyun 	int (*get_bus_free)(struct i2c_adapter *adap);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	void (*prepare_recovery)(struct i2c_adapter *adap);
628*4882a593Smuzhiyun 	void (*unprepare_recovery)(struct i2c_adapter *adap);
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	/* gpio recovery */
631*4882a593Smuzhiyun 	struct gpio_desc *scl_gpiod;
632*4882a593Smuzhiyun 	struct gpio_desc *sda_gpiod;
633*4882a593Smuzhiyun 	struct pinctrl *pinctrl;
634*4882a593Smuzhiyun 	struct pinctrl_state *pins_default;
635*4882a593Smuzhiyun 	struct pinctrl_state *pins_gpio;
636*4882a593Smuzhiyun };
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun int i2c_recover_bus(struct i2c_adapter *adap);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /* Generic recovery routines */
641*4882a593Smuzhiyun int i2c_generic_scl_recovery(struct i2c_adapter *adap);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun /**
644*4882a593Smuzhiyun  * struct i2c_adapter_quirks - describe flaws of an i2c adapter
645*4882a593Smuzhiyun  * @flags: see I2C_AQ_* for possible flags and read below
646*4882a593Smuzhiyun  * @max_num_msgs: maximum number of messages per transfer
647*4882a593Smuzhiyun  * @max_write_len: maximum length of a write message
648*4882a593Smuzhiyun  * @max_read_len: maximum length of a read message
649*4882a593Smuzhiyun  * @max_comb_1st_msg_len: maximum length of the first msg in a combined message
650*4882a593Smuzhiyun  * @max_comb_2nd_msg_len: maximum length of the second msg in a combined message
651*4882a593Smuzhiyun  *
652*4882a593Smuzhiyun  * Note about combined messages: Some I2C controllers can only send one message
653*4882a593Smuzhiyun  * per transfer, plus something called combined message or write-then-read.
654*4882a593Smuzhiyun  * This is (usually) a small write message followed by a read message and
655*4882a593Smuzhiyun  * barely enough to access register based devices like EEPROMs. There is a flag
656*4882a593Smuzhiyun  * to support this mode. It implies max_num_msg = 2 and does the length checks
657*4882a593Smuzhiyun  * with max_comb_*_len because combined message mode usually has its own
658*4882a593Smuzhiyun  * limitations. Because of HW implementations, some controllers can actually do
659*4882a593Smuzhiyun  * write-then-anything or other variants. To support that, write-then-read has
660*4882a593Smuzhiyun  * been broken out into smaller bits like write-first and read-second which can
661*4882a593Smuzhiyun  * be combined as needed.
662*4882a593Smuzhiyun  */
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun struct i2c_adapter_quirks {
665*4882a593Smuzhiyun 	u64 flags;
666*4882a593Smuzhiyun 	int max_num_msgs;
667*4882a593Smuzhiyun 	u16 max_write_len;
668*4882a593Smuzhiyun 	u16 max_read_len;
669*4882a593Smuzhiyun 	u16 max_comb_1st_msg_len;
670*4882a593Smuzhiyun 	u16 max_comb_2nd_msg_len;
671*4882a593Smuzhiyun };
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun /* enforce max_num_msgs = 2 and use max_comb_*_len for length checks */
674*4882a593Smuzhiyun #define I2C_AQ_COMB			BIT(0)
675*4882a593Smuzhiyun /* first combined message must be write */
676*4882a593Smuzhiyun #define I2C_AQ_COMB_WRITE_FIRST		BIT(1)
677*4882a593Smuzhiyun /* second combined message must be read */
678*4882a593Smuzhiyun #define I2C_AQ_COMB_READ_SECOND		BIT(2)
679*4882a593Smuzhiyun /* both combined messages must have the same target address */
680*4882a593Smuzhiyun #define I2C_AQ_COMB_SAME_ADDR		BIT(3)
681*4882a593Smuzhiyun /* convenience macro for typical write-then read case */
682*4882a593Smuzhiyun #define I2C_AQ_COMB_WRITE_THEN_READ	(I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \
683*4882a593Smuzhiyun 					 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR)
684*4882a593Smuzhiyun /* clock stretching is not supported */
685*4882a593Smuzhiyun #define I2C_AQ_NO_CLK_STRETCH		BIT(4)
686*4882a593Smuzhiyun /* message cannot have length of 0 */
687*4882a593Smuzhiyun #define I2C_AQ_NO_ZERO_LEN_READ		BIT(5)
688*4882a593Smuzhiyun #define I2C_AQ_NO_ZERO_LEN_WRITE	BIT(6)
689*4882a593Smuzhiyun #define I2C_AQ_NO_ZERO_LEN		(I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE)
690*4882a593Smuzhiyun /* adapter cannot do repeated START */
691*4882a593Smuzhiyun #define I2C_AQ_NO_REP_START		BIT(7)
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun /*
694*4882a593Smuzhiyun  * i2c_adapter is the structure used to identify a physical i2c bus along
695*4882a593Smuzhiyun  * with the access algorithms necessary to access it.
696*4882a593Smuzhiyun  */
697*4882a593Smuzhiyun struct i2c_adapter {
698*4882a593Smuzhiyun 	struct module *owner;
699*4882a593Smuzhiyun 	unsigned int class;		  /* classes to allow probing for */
700*4882a593Smuzhiyun 	const struct i2c_algorithm *algo; /* the algorithm to access the bus */
701*4882a593Smuzhiyun 	void *algo_data;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	/* data fields that are valid for all devices	*/
704*4882a593Smuzhiyun 	const struct i2c_lock_operations *lock_ops;
705*4882a593Smuzhiyun 	struct rt_mutex bus_lock;
706*4882a593Smuzhiyun 	struct rt_mutex mux_lock;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	int timeout;			/* in jiffies */
709*4882a593Smuzhiyun 	int retries;
710*4882a593Smuzhiyun 	struct device dev;		/* the adapter device */
711*4882a593Smuzhiyun 	unsigned long locked_flags;	/* owned by the I2C core */
712*4882a593Smuzhiyun #define I2C_ALF_IS_SUSPENDED		0
713*4882a593Smuzhiyun #define I2C_ALF_SUSPEND_REPORTED	1
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	int nr;
716*4882a593Smuzhiyun 	char name[48];
717*4882a593Smuzhiyun 	struct completion dev_released;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	struct mutex userspace_clients_lock;
720*4882a593Smuzhiyun 	struct list_head userspace_clients;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	struct i2c_bus_recovery_info *bus_recovery_info;
723*4882a593Smuzhiyun 	const struct i2c_adapter_quirks *quirks;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	struct irq_domain *host_notify_domain;
726*4882a593Smuzhiyun };
727*4882a593Smuzhiyun #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
728*4882a593Smuzhiyun 
i2c_get_adapdata(const struct i2c_adapter * adap)729*4882a593Smuzhiyun static inline void *i2c_get_adapdata(const struct i2c_adapter *adap)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun 	return dev_get_drvdata(&adap->dev);
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
i2c_set_adapdata(struct i2c_adapter * adap,void * data)734*4882a593Smuzhiyun static inline void i2c_set_adapdata(struct i2c_adapter *adap, void *data)
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun 	dev_set_drvdata(&adap->dev, data);
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun static inline struct i2c_adapter *
i2c_parent_is_i2c_adapter(const struct i2c_adapter * adapter)740*4882a593Smuzhiyun i2c_parent_is_i2c_adapter(const struct i2c_adapter *adapter)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C_MUX)
743*4882a593Smuzhiyun 	struct device *parent = adapter->dev.parent;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (parent != NULL && parent->type == &i2c_adapter_type)
746*4882a593Smuzhiyun 		return to_i2c_adapter(parent);
747*4882a593Smuzhiyun 	else
748*4882a593Smuzhiyun #endif
749*4882a593Smuzhiyun 		return NULL;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data));
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun /* Adapter locking functions, exported for shared pin cases */
755*4882a593Smuzhiyun #define I2C_LOCK_ROOT_ADAPTER BIT(0)
756*4882a593Smuzhiyun #define I2C_LOCK_SEGMENT      BIT(1)
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun /**
759*4882a593Smuzhiyun  * i2c_lock_bus - Get exclusive access to an I2C bus segment
760*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
761*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
762*4882a593Smuzhiyun  *	locks only this branch in the adapter tree
763*4882a593Smuzhiyun  */
764*4882a593Smuzhiyun static inline void
i2c_lock_bus(struct i2c_adapter * adapter,unsigned int flags)765*4882a593Smuzhiyun i2c_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	adapter->lock_ops->lock_bus(adapter, flags);
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun /**
771*4882a593Smuzhiyun  * i2c_trylock_bus - Try to get exclusive access to an I2C bus segment
772*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
773*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER tries to locks the root i2c adapter,
774*4882a593Smuzhiyun  *	I2C_LOCK_SEGMENT tries to lock only this branch in the adapter tree
775*4882a593Smuzhiyun  *
776*4882a593Smuzhiyun  * Return: true if the I2C bus segment is locked, false otherwise
777*4882a593Smuzhiyun  */
778*4882a593Smuzhiyun static inline int
i2c_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)779*4882a593Smuzhiyun i2c_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun 	return adapter->lock_ops->trylock_bus(adapter, flags);
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun /**
785*4882a593Smuzhiyun  * i2c_unlock_bus - Release exclusive access to an I2C bus segment
786*4882a593Smuzhiyun  * @adapter: Target I2C bus segment
787*4882a593Smuzhiyun  * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
788*4882a593Smuzhiyun  *	unlocks only this branch in the adapter tree
789*4882a593Smuzhiyun  */
790*4882a593Smuzhiyun static inline void
i2c_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)791*4882a593Smuzhiyun i2c_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun 	adapter->lock_ops->unlock_bus(adapter, flags);
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun  * i2c_mark_adapter_suspended - Report suspended state of the adapter to the core
798*4882a593Smuzhiyun  * @adap: Adapter to mark as suspended
799*4882a593Smuzhiyun  *
800*4882a593Smuzhiyun  * When using this helper to mark an adapter as suspended, the core will reject
801*4882a593Smuzhiyun  * further transfers to this adapter. The usage of this helper is optional but
802*4882a593Smuzhiyun  * recommended for devices having distinct handlers for system suspend and
803*4882a593Smuzhiyun  * runtime suspend. More complex devices are free to implement custom solutions
804*4882a593Smuzhiyun  * to reject transfers when suspended.
805*4882a593Smuzhiyun  */
i2c_mark_adapter_suspended(struct i2c_adapter * adap)806*4882a593Smuzhiyun static inline void i2c_mark_adapter_suspended(struct i2c_adapter *adap)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	i2c_lock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
809*4882a593Smuzhiyun 	set_bit(I2C_ALF_IS_SUSPENDED, &adap->locked_flags);
810*4882a593Smuzhiyun 	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun /**
814*4882a593Smuzhiyun  * i2c_mark_adapter_resumed - Report resumed state of the adapter to the core
815*4882a593Smuzhiyun  * @adap: Adapter to mark as resumed
816*4882a593Smuzhiyun  *
817*4882a593Smuzhiyun  * When using this helper to mark an adapter as resumed, the core will allow
818*4882a593Smuzhiyun  * further transfers to this adapter. See also further notes to
819*4882a593Smuzhiyun  * @i2c_mark_adapter_suspended().
820*4882a593Smuzhiyun  */
i2c_mark_adapter_resumed(struct i2c_adapter * adap)821*4882a593Smuzhiyun static inline void i2c_mark_adapter_resumed(struct i2c_adapter *adap)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun 	i2c_lock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
824*4882a593Smuzhiyun 	clear_bit(I2C_ALF_IS_SUSPENDED, &adap->locked_flags);
825*4882a593Smuzhiyun 	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun /* i2c adapter classes (bitmask) */
829*4882a593Smuzhiyun #define I2C_CLASS_HWMON		(1<<0)	/* lm_sensors, ... */
830*4882a593Smuzhiyun #define I2C_CLASS_DDC		(1<<3)	/* DDC bus on graphics adapters */
831*4882a593Smuzhiyun #define I2C_CLASS_SPD		(1<<7)	/* Memory modules */
832*4882a593Smuzhiyun /* Warn users that the adapter doesn't support classes anymore */
833*4882a593Smuzhiyun #define I2C_CLASS_DEPRECATED	(1<<8)
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun /* Internal numbers to terminate lists */
836*4882a593Smuzhiyun #define I2C_CLIENT_END		0xfffeU
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun /* Construct an I2C_CLIENT_END-terminated array of i2c addresses */
839*4882a593Smuzhiyun #define I2C_ADDRS(addr, addrs...) \
840*4882a593Smuzhiyun 	((const unsigned short []){ addr, ## addrs, I2C_CLIENT_END })
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun /* ----- functions exported by i2c.o */
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun /* administration...
846*4882a593Smuzhiyun  */
847*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_I2C)
848*4882a593Smuzhiyun int i2c_add_adapter(struct i2c_adapter *adap);
849*4882a593Smuzhiyun void i2c_del_adapter(struct i2c_adapter *adap);
850*4882a593Smuzhiyun int i2c_add_numbered_adapter(struct i2c_adapter *adap);
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun int i2c_register_driver(struct module *owner, struct i2c_driver *driver);
853*4882a593Smuzhiyun void i2c_del_driver(struct i2c_driver *driver);
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun /* use a define to avoid include chaining to get THIS_MODULE */
856*4882a593Smuzhiyun #define i2c_add_driver(driver) \
857*4882a593Smuzhiyun 	i2c_register_driver(THIS_MODULE, driver)
858*4882a593Smuzhiyun 
i2c_client_has_driver(struct i2c_client * client)859*4882a593Smuzhiyun static inline bool i2c_client_has_driver(struct i2c_client *client)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	return !IS_ERR_OR_NULL(client) && client->dev.driver;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun /* call the i2c_client->command() of all attached clients with
865*4882a593Smuzhiyun  * the given arguments */
866*4882a593Smuzhiyun void i2c_clients_command(struct i2c_adapter *adap,
867*4882a593Smuzhiyun 			 unsigned int cmd, void *arg);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun struct i2c_adapter *i2c_get_adapter(int nr);
870*4882a593Smuzhiyun void i2c_put_adapter(struct i2c_adapter *adap);
871*4882a593Smuzhiyun unsigned int i2c_adapter_depth(struct i2c_adapter *adapter);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun /* Return the functionality mask */
i2c_get_functionality(struct i2c_adapter * adap)876*4882a593Smuzhiyun static inline u32 i2c_get_functionality(struct i2c_adapter *adap)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun 	return adap->algo->functionality(adap);
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun /* Return 1 if adapter supports everything we need, 0 if not. */
i2c_check_functionality(struct i2c_adapter * adap,u32 func)882*4882a593Smuzhiyun static inline int i2c_check_functionality(struct i2c_adapter *adap, u32 func)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	return (func & i2c_get_functionality(adap)) == func;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun /**
888*4882a593Smuzhiyun  * i2c_check_quirks() - Function for checking the quirk flags in an i2c adapter
889*4882a593Smuzhiyun  * @adap: i2c adapter
890*4882a593Smuzhiyun  * @quirks: quirk flags
891*4882a593Smuzhiyun  *
892*4882a593Smuzhiyun  * Return: true if the adapter has all the specified quirk flags, false if not
893*4882a593Smuzhiyun  */
i2c_check_quirks(struct i2c_adapter * adap,u64 quirks)894*4882a593Smuzhiyun static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	if (!adap->quirks)
897*4882a593Smuzhiyun 		return false;
898*4882a593Smuzhiyun 	return (adap->quirks->flags & quirks) == quirks;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun /* Return the adapter number for a specific adapter */
i2c_adapter_id(struct i2c_adapter * adap)902*4882a593Smuzhiyun static inline int i2c_adapter_id(struct i2c_adapter *adap)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	return adap->nr;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun 
i2c_8bit_addr_from_msg(const struct i2c_msg * msg)907*4882a593Smuzhiyun static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
913*4882a593Smuzhiyun void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred);
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
916*4882a593Smuzhiyun /**
917*4882a593Smuzhiyun  * module_i2c_driver() - Helper macro for registering a modular I2C driver
918*4882a593Smuzhiyun  * @__i2c_driver: i2c_driver struct
919*4882a593Smuzhiyun  *
920*4882a593Smuzhiyun  * Helper macro for I2C drivers which do not do anything special in module
921*4882a593Smuzhiyun  * init/exit. This eliminates a lot of boilerplate. Each module may only
922*4882a593Smuzhiyun  * use this macro once, and calling it replaces module_init() and module_exit()
923*4882a593Smuzhiyun  */
924*4882a593Smuzhiyun #define module_i2c_driver(__i2c_driver) \
925*4882a593Smuzhiyun 	module_driver(__i2c_driver, i2c_add_driver, \
926*4882a593Smuzhiyun 			i2c_del_driver)
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun /**
929*4882a593Smuzhiyun  * builtin_i2c_driver() - Helper macro for registering a builtin I2C driver
930*4882a593Smuzhiyun  * @__i2c_driver: i2c_driver struct
931*4882a593Smuzhiyun  *
932*4882a593Smuzhiyun  * Helper macro for I2C drivers which do not do anything special in their
933*4882a593Smuzhiyun  * init. This eliminates a lot of boilerplate. Each driver may only
934*4882a593Smuzhiyun  * use this macro once, and calling it replaces device_initcall().
935*4882a593Smuzhiyun  */
936*4882a593Smuzhiyun #define builtin_i2c_driver(__i2c_driver) \
937*4882a593Smuzhiyun 	builtin_driver(__i2c_driver, i2c_add_driver)
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun #endif /* I2C */
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_OF)
942*4882a593Smuzhiyun /* must call put_device() when done with returned i2c_client device */
943*4882a593Smuzhiyun struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun /* must call put_device() when done with returned i2c_adapter device */
946*4882a593Smuzhiyun struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun /* must call i2c_put_adapter() when done with returned i2c_adapter device */
949*4882a593Smuzhiyun struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun const struct of_device_id
952*4882a593Smuzhiyun *i2c_of_match_device(const struct of_device_id *matches,
953*4882a593Smuzhiyun 		     struct i2c_client *client);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun int of_i2c_get_board_info(struct device *dev, struct device_node *node,
956*4882a593Smuzhiyun 			  struct i2c_board_info *info);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun #else
959*4882a593Smuzhiyun 
of_find_i2c_device_by_node(struct device_node * node)960*4882a593Smuzhiyun static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun 	return NULL;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
of_find_i2c_adapter_by_node(struct device_node * node)965*4882a593Smuzhiyun static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun 	return NULL;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
of_get_i2c_adapter_by_node(struct device_node * node)970*4882a593Smuzhiyun static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	return NULL;
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun static inline const struct of_device_id
i2c_of_match_device(const struct of_device_id * matches,struct i2c_client * client)976*4882a593Smuzhiyun *i2c_of_match_device(const struct of_device_id *matches,
977*4882a593Smuzhiyun 		     struct i2c_client *client)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	return NULL;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
of_i2c_get_board_info(struct device * dev,struct device_node * node,struct i2c_board_info * info)982*4882a593Smuzhiyun static inline int of_i2c_get_board_info(struct device *dev,
983*4882a593Smuzhiyun 					struct device_node *node,
984*4882a593Smuzhiyun 					struct i2c_board_info *info)
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	return -ENOTSUPP;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun #endif /* CONFIG_OF */
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun struct acpi_resource;
992*4882a593Smuzhiyun struct acpi_resource_i2c_serialbus;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_ACPI)
995*4882a593Smuzhiyun bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
996*4882a593Smuzhiyun 			       struct acpi_resource_i2c_serialbus **i2c);
997*4882a593Smuzhiyun u32 i2c_acpi_find_bus_speed(struct device *dev);
998*4882a593Smuzhiyun struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
999*4882a593Smuzhiyun 				       struct i2c_board_info *info);
1000*4882a593Smuzhiyun struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle);
1001*4882a593Smuzhiyun #else
i2c_acpi_get_i2c_resource(struct acpi_resource * ares,struct acpi_resource_i2c_serialbus ** i2c)1002*4882a593Smuzhiyun static inline bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
1003*4882a593Smuzhiyun 					     struct acpi_resource_i2c_serialbus **i2c)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun 	return false;
1006*4882a593Smuzhiyun }
i2c_acpi_find_bus_speed(struct device * dev)1007*4882a593Smuzhiyun static inline u32 i2c_acpi_find_bus_speed(struct device *dev)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun 	return 0;
1010*4882a593Smuzhiyun }
i2c_acpi_new_device(struct device * dev,int index,struct i2c_board_info * info)1011*4882a593Smuzhiyun static inline struct i2c_client *i2c_acpi_new_device(struct device *dev,
1012*4882a593Smuzhiyun 					int index, struct i2c_board_info *info)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun 	return ERR_PTR(-ENODEV);
1015*4882a593Smuzhiyun }
i2c_acpi_find_adapter_by_handle(acpi_handle handle)1016*4882a593Smuzhiyun static inline struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun 	return NULL;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun #endif /* CONFIG_ACPI */
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun #endif /* _LINUX_I2C_H */
1023