xref: /linux-6.15/include/linux/i2c.h (revision e794dc30)
116216333SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
276cc9f0eSWolfram Sang /*
376cc9f0eSWolfram Sang  * i2c.h - definitions for the Linux i2c bus interface
476cc9f0eSWolfram Sang  * Copyright (C) 1995-2000 Simon G. Vogl
52f5a55c5SWolfram Sang  * Copyright (C) 2013-2019 Wolfram Sang <[email protected]>
676cc9f0eSWolfram Sang  *
776cc9f0eSWolfram Sang  * With some changes from Kyösti Mälkki <[email protected]> and
876cc9f0eSWolfram Sang  * Frodo Looijaard <[email protected]>
916216333SThomas Gleixner  */
101da177e4SLinus Torvalds #ifndef _LINUX_I2C_H
111da177e4SLinus Torvalds #define _LINUX_I2C_H
121da177e4SLinus Torvalds 
135213d7efSRuslan Babayev #include <linux/acpi.h>		/* for acpi_handle */
14b18c1ad6SSakari Ailus #include <linux/bits.h>
15a9d1b24dSGreg Kroah-Hartman #include <linux/mod_devicetable.h>
161da177e4SLinus Torvalds #include <linux/device.h>	/* for struct device */
174e57b681STim Schmielau #include <linux/sched.h>	/* for completion */
185c085d36SIngo Molnar #include <linux/mutex.h>
195a7b95fbSBibby Hsieh #include <linux/regulator/consumer.h>
20037741a6SIngo Molnar #include <linux/rtmutex.h>
214d5538f5SBenjamin Tissoires #include <linux/irqdomain.h>		/* for Host Notify IRQ */
22d12d42f7SGrant Likely #include <linux/of.h>		/* for struct device_node */
2306a67848SJonathan Cameron #include <linux/swab.h>		/* for swab16 */
24607ca46eSDavid Howells #include <uapi/linux/i2c.h>
251da177e4SLinus Torvalds 
2694959c0eSGreg Kroah-Hartman extern const struct bus_type i2c_bus_type;
27eb520344SRicardo B. Marliere extern const struct device_type i2c_adapter_type;
28eb520344SRicardo B. Marliere extern const struct device_type i2c_client_type;
29e9ca9eb9SJon Smirl 
301da177e4SLinus Torvalds /* --- General options ------------------------------------------------	*/
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds struct i2c_msg;
331da177e4SLinus Torvalds struct i2c_adapter;
341da177e4SLinus Torvalds struct i2c_client;
351da177e4SLinus Torvalds struct i2c_driver;
36dde67eb1SPeter Rosin struct i2c_device_identity;
371da177e4SLinus Torvalds union i2c_smbus_data;
384735c98fSJean Delvare struct i2c_board_info;
394b1acc43SWolfram Sang enum i2c_slave_event;
40d68222d4SWolfram Sang typedef int (*i2c_slave_cb_t)(struct i2c_client *client,
41d68222d4SWolfram Sang 			      enum i2c_slave_event event, u8 *val);
421da177e4SLinus Torvalds 
43e6282fc6SAndy Shevchenko /* I2C Frequency Modes */
44e6282fc6SAndy Shevchenko #define I2C_MAX_STANDARD_MODE_FREQ	100000
45e6282fc6SAndy Shevchenko #define I2C_MAX_FAST_MODE_FREQ		400000
46e6282fc6SAndy Shevchenko #define I2C_MAX_FAST_MODE_PLUS_FREQ	1000000
47e6282fc6SAndy Shevchenko #define I2C_MAX_TURBO_MODE_FREQ		1400000
48e6282fc6SAndy Shevchenko #define I2C_MAX_HIGH_SPEED_MODE_FREQ	3400000
49e6282fc6SAndy Shevchenko #define I2C_MAX_ULTRA_FAST_MODE_FREQ	5000000
50e6282fc6SAndy Shevchenko 
51de477254SPaul Gortmaker struct module;
52d3e1b617SDmitry Torokhov struct property_entry;
53de477254SPaul Gortmaker 
54901a891fSStefan Lengfeld #if IS_ENABLED(CONFIG_I2C)
553b4c747cSYicong Yang /* Return the Frequency mode string based on the bus frequency */
563b4c747cSYicong Yang const char *i2c_freq_mode_string(u32 bus_freq_hz);
573b4c747cSYicong Yang 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds  * The master routines are the ones normally used to transmit data to devices
601da177e4SLinus Torvalds  * on a bus (or read from them). Apart from two basic transfer functions to
611da177e4SLinus Torvalds  * transmit one message at a time, a more complex version can be used to
621da177e4SLinus Torvalds  * transmit an arbitrary number of messages without interruption.
63aca7ed09SRandy Dunlap  * @count must be less than 64k since msg.len is u16.
641da177e4SLinus Torvalds  */
65c807da53SLuca Ceresoli int i2c_transfer_buffer_flags(const struct i2c_client *client,
668a91732bSWolfram Sang 			      char *buf, int count, u16 flags);
678a91732bSWolfram Sang 
688a91732bSWolfram Sang /**
698a91732bSWolfram Sang  * i2c_master_recv - issue a single I2C message in master receive mode
708a91732bSWolfram Sang  * @client: Handle to slave device
718a91732bSWolfram Sang  * @buf: Where to store data read from slave
728a91732bSWolfram Sang  * @count: How many bytes to read, must be less than 64k since msg.len is u16
738a91732bSWolfram Sang  *
748a91732bSWolfram Sang  * Returns negative errno, or else the number of bytes read.
758a91732bSWolfram Sang  */
i2c_master_recv(const struct i2c_client * client,char * buf,int count)768a91732bSWolfram Sang static inline int i2c_master_recv(const struct i2c_client *client,
778a91732bSWolfram Sang 				  char *buf, int count)
788a91732bSWolfram Sang {
798a91732bSWolfram Sang 	return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
808a91732bSWolfram Sang };
818a91732bSWolfram Sang 
828a91732bSWolfram Sang /**
83ba98645cSWolfram Sang  * i2c_master_recv_dmasafe - issue a single I2C message in master receive mode
84ba98645cSWolfram Sang  *			     using a DMA safe buffer
85ba98645cSWolfram Sang  * @client: Handle to slave device
86ba98645cSWolfram Sang  * @buf: Where to store data read from slave, must be safe to use with DMA
87ba98645cSWolfram Sang  * @count: How many bytes to read, must be less than 64k since msg.len is u16
88ba98645cSWolfram Sang  *
89ba98645cSWolfram Sang  * Returns negative errno, or else the number of bytes read.
90ba98645cSWolfram Sang  */
i2c_master_recv_dmasafe(const struct i2c_client * client,char * buf,int count)91ba98645cSWolfram Sang static inline int i2c_master_recv_dmasafe(const struct i2c_client *client,
92ba98645cSWolfram Sang 					  char *buf, int count)
93ba98645cSWolfram Sang {
94ba98645cSWolfram Sang 	return i2c_transfer_buffer_flags(client, buf, count,
95ba98645cSWolfram Sang 					 I2C_M_RD | I2C_M_DMA_SAFE);
96ba98645cSWolfram Sang };
97ba98645cSWolfram Sang 
98ba98645cSWolfram Sang /**
998a91732bSWolfram Sang  * i2c_master_send - issue a single I2C message in master transmit mode
1008a91732bSWolfram Sang  * @client: Handle to slave device
1018a91732bSWolfram Sang  * @buf: Data that will be written to the slave
1028a91732bSWolfram Sang  * @count: How many bytes to write, must be less than 64k since msg.len is u16
1038a91732bSWolfram Sang  *
1048a91732bSWolfram Sang  * Returns negative errno, or else the number of bytes written.
1058a91732bSWolfram Sang  */
i2c_master_send(const struct i2c_client * client,const char * buf,int count)1068a91732bSWolfram Sang static inline int i2c_master_send(const struct i2c_client *client,
1078a91732bSWolfram Sang 				  const char *buf, int count)
1088a91732bSWolfram Sang {
1098a91732bSWolfram Sang 	return i2c_transfer_buffer_flags(client, (char *)buf, count, 0);
1108a91732bSWolfram Sang };
1111da177e4SLinus Torvalds 
112ba98645cSWolfram Sang /**
113ba98645cSWolfram Sang  * i2c_master_send_dmasafe - issue a single I2C message in master transmit mode
114ba98645cSWolfram Sang  *			     using a DMA safe buffer
115ba98645cSWolfram Sang  * @client: Handle to slave device
116ba98645cSWolfram Sang  * @buf: Data that will be written to the slave, must be safe to use with DMA
117ba98645cSWolfram Sang  * @count: How many bytes to write, must be less than 64k since msg.len is u16
118ba98645cSWolfram Sang  *
119ba98645cSWolfram Sang  * Returns negative errno, or else the number of bytes written.
120ba98645cSWolfram Sang  */
i2c_master_send_dmasafe(const struct i2c_client * client,const char * buf,int count)121ba98645cSWolfram Sang static inline int i2c_master_send_dmasafe(const struct i2c_client *client,
122ba98645cSWolfram Sang 					  const char *buf, int count)
123ba98645cSWolfram Sang {
124ba98645cSWolfram Sang 	return i2c_transfer_buffer_flags(client, (char *)buf, count,
125ba98645cSWolfram Sang 					 I2C_M_DMA_SAFE);
126ba98645cSWolfram Sang };
127ba98645cSWolfram Sang 
1281da177e4SLinus Torvalds /* Transfer num messages.
1291da177e4SLinus Torvalds  */
130c807da53SLuca Ceresoli int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
131b37d2a3aSJean Delvare /* Unlocked flavor */
132c807da53SLuca Ceresoli int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* This is the very generalized SMBus access routine. You probably do not
1351da177e4SLinus Torvalds    want to use this, though; one of the functions below may be much easier,
1361da177e4SLinus Torvalds    and probably just as fast.
1371da177e4SLinus Torvalds    Note that we use i2c_adapter here, because you do not need a specific
1381da177e4SLinus Torvalds    smbus adapter to call this function. */
13963453b59SPeter Rosin s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1403ae70deeSJean Delvare 		   unsigned short flags, char read_write, u8 command,
14163453b59SPeter Rosin 		   int protocol, union i2c_smbus_data *data);
14263453b59SPeter Rosin 
14363453b59SPeter Rosin /* Unlocked flavor */
14463453b59SPeter Rosin s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
14563453b59SPeter Rosin 		     unsigned short flags, char read_write, u8 command,
14663453b59SPeter Rosin 		     int protocol, union i2c_smbus_data *data);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /* Now follow the 'nice' access routines. These also document the calling
149ae7193f7SJean Delvare    conventions of i2c_smbus_xfer. */
1501da177e4SLinus Torvalds 
15187cf5127SQuan Nguyen u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count);
152c807da53SLuca Ceresoli s32 i2c_smbus_read_byte(const struct i2c_client *client);
153c807da53SLuca Ceresoli s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value);
154c807da53SLuca Ceresoli s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command);
155c807da53SLuca Ceresoli s32 i2c_smbus_write_byte_data(const struct i2c_client *client,
1561da177e4SLinus Torvalds 			      u8 command, u8 value);
157c807da53SLuca Ceresoli s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command);
158c807da53SLuca Ceresoli s32 i2c_smbus_write_word_data(const struct i2c_client *client,
1591da177e4SLinus Torvalds 			      u8 command, u16 value);
16006a67848SJonathan Cameron 
16106a67848SJonathan Cameron static inline s32
i2c_smbus_read_word_swapped(const struct i2c_client * client,u8 command)16206a67848SJonathan Cameron i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
16306a67848SJonathan Cameron {
16406a67848SJonathan Cameron 	s32 value = i2c_smbus_read_word_data(client, command);
16506a67848SJonathan Cameron 
16606a67848SJonathan Cameron 	return (value < 0) ? value : swab16(value);
16706a67848SJonathan Cameron }
16806a67848SJonathan Cameron 
16906a67848SJonathan Cameron static inline s32
i2c_smbus_write_word_swapped(const struct i2c_client * client,u8 command,u16 value)17006a67848SJonathan Cameron i2c_smbus_write_word_swapped(const struct i2c_client *client,
17106a67848SJonathan Cameron 			     u8 command, u16 value)
17206a67848SJonathan Cameron {
17306a67848SJonathan Cameron 	return i2c_smbus_write_word_data(client, command, swab16(value));
17406a67848SJonathan Cameron }
17506a67848SJonathan Cameron 
176b86a1bc8SJean Delvare /* Returns the number of read bytes */
177c807da53SLuca Ceresoli s32 i2c_smbus_read_block_data(const struct i2c_client *client,
178b86a1bc8SJean Delvare 			      u8 command, u8 *values);
179c807da53SLuca Ceresoli s32 i2c_smbus_write_block_data(const struct i2c_client *client,
1803ae70deeSJean Delvare 			       u8 command, u8 length, const u8 *values);
18131ec5bc5SJean Delvare /* Returns the number of read bytes */
182c807da53SLuca Ceresoli s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
1834b2643d7SJean Delvare 				  u8 command, u8 length, u8 *values);
184c807da53SLuca Ceresoli s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
185c807da53SLuca Ceresoli 				   u8 command, u8 length, const u8 *values);
186c807da53SLuca Ceresoli s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
18721bbd691SJean Delvare 					      u8 command, u8 length,
188c807da53SLuca Ceresoli 					      u8 *values);
189dde67eb1SPeter Rosin int i2c_get_device_id(const struct i2c_client *client,
190dde67eb1SPeter Rosin 		      struct i2c_device_identity *id);
19166223373SAngel Iglesias const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client);
19223af8400SJean Delvare #endif /* I2C */
1931da177e4SLinus Torvalds 
194dde67eb1SPeter Rosin /**
195dde67eb1SPeter Rosin  * struct i2c_device_identity - i2c client device identification
196dde67eb1SPeter Rosin  * @manufacturer_id: 0 - 4095, database maintained by NXP
197dde67eb1SPeter Rosin  * @part_id: 0 - 511, according to manufacturer
198dde67eb1SPeter Rosin  * @die_revision: 0 - 7, according to manufacturer
199dde67eb1SPeter Rosin  */
200dde67eb1SPeter Rosin struct i2c_device_identity {
201dde67eb1SPeter Rosin 	u16 manufacturer_id;
202dde67eb1SPeter Rosin #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS                0
203dde67eb1SPeter Rosin #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_1              1
204dde67eb1SPeter Rosin #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_2              2
205dde67eb1SPeter Rosin #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_3              3
206dde67eb1SPeter Rosin #define I2C_DEVICE_ID_RAMTRON_INTERNATIONAL             4
207dde67eb1SPeter Rosin #define I2C_DEVICE_ID_ANALOG_DEVICES                    5
208dde67eb1SPeter Rosin #define I2C_DEVICE_ID_STMICROELECTRONICS                6
209dde67eb1SPeter Rosin #define I2C_DEVICE_ID_ON_SEMICONDUCTOR                  7
210dde67eb1SPeter Rosin #define I2C_DEVICE_ID_SPRINTEK_CORPORATION              8
211dde67eb1SPeter Rosin #define I2C_DEVICE_ID_ESPROS_PHOTONICS_AG               9
212dde67eb1SPeter Rosin #define I2C_DEVICE_ID_FUJITSU_SEMICONDUCTOR            10
213dde67eb1SPeter Rosin #define I2C_DEVICE_ID_FLIR                             11
214dde67eb1SPeter Rosin #define I2C_DEVICE_ID_O2MICRO                          12
215dde67eb1SPeter Rosin #define I2C_DEVICE_ID_ATMEL                            13
216dde67eb1SPeter Rosin #define I2C_DEVICE_ID_NONE                         0xffff
217dde67eb1SPeter Rosin 	u16 part_id;
218dde67eb1SPeter Rosin 	u8 die_revision;
219dde67eb1SPeter Rosin };
220dde67eb1SPeter Rosin 
221b4f21054SBenjamin Tissoires enum i2c_alert_protocol {
222b4f21054SBenjamin Tissoires 	I2C_PROTOCOL_SMBUS_ALERT,
223e456cd37SBenjamin Tissoires 	I2C_PROTOCOL_SMBUS_HOST_NOTIFY,
224b4f21054SBenjamin Tissoires };
225b4f21054SBenjamin Tissoires 
2264735c98fSJean Delvare /**
227b18c1ad6SSakari Ailus  * enum i2c_driver_flags - Flags for an I2C device driver
228b18c1ad6SSakari Ailus  *
229b18c1ad6SSakari Ailus  * @I2C_DRV_ACPI_WAIVE_D0_PROBE: Don't put the device in D0 state for probe
230b18c1ad6SSakari Ailus  */
231b18c1ad6SSakari Ailus enum i2c_driver_flags {
232b18c1ad6SSakari Ailus 	I2C_DRV_ACPI_WAIVE_D0_PROBE = BIT(0),
233b18c1ad6SSakari Ailus };
234b18c1ad6SSakari Ailus 
235b18c1ad6SSakari Ailus /**
2364735c98fSJean Delvare  * struct i2c_driver - represent an I2C device driver
2374735c98fSJean Delvare  * @class: What kind of i2c device we instantiate (for detect)
23803c835f4SUwe Kleine-König  * @probe: Callback for device binding
239729d6dd5SJean Delvare  * @remove: Callback for device unbinding
24096e21e4fSJean Delvare  * @shutdown: Callback for device shutdown
24172d2e9f9SRandy Dunlap  * @alert: Alert callback, for example for the SMBus alert protocol
24296e21e4fSJean Delvare  * @command: Callback for bus-wide signaling (optional)
24396e21e4fSJean Delvare  * @driver: Device driver model driver
24496e21e4fSJean Delvare  * @id_table: List of I2C devices supported by this driver
2454735c98fSJean Delvare  * @detect: Callback for device detection
246c3813d6aSJean Delvare  * @address_list: The I2C addresses to probe (for detect)
2473bfa08feSWolfram Sang  * @clients: List of detected clients we created (for i2c-core use only)
248b18c1ad6SSakari Ailus  * @flags: A bitmask of flags defined in &enum i2c_driver_flags
24935d8b2e6SLaurent Riffard  *
25035d8b2e6SLaurent Riffard  * The driver.owner field should be set to the module owner of this driver.
25135d8b2e6SLaurent Riffard  * The driver.name field should be set to the name of this driver.
2524735c98fSJean Delvare  *
2530ec13867SVivien Didelot  * For automatic device detection, both @detect and @address_list must
2544735c98fSJean Delvare  * be defined. @class should also be set, otherwise only devices forced
2554735c98fSJean Delvare  * with module parameters will be created. The detect function must
2564735c98fSJean Delvare  * fill at least the name field of the i2c_board_info structure it is
2574735c98fSJean Delvare  * handed upon successful detection, and possibly also the flags field.
2584735c98fSJean Delvare  *
2594735c98fSJean Delvare  * If @detect is missing, the driver will still work fine for enumerated
2604735c98fSJean Delvare  * devices. Detected devices simply won't be supported. This is expected
2614735c98fSJean Delvare  * for the many I2C/SMBus devices which can't be detected reliably, and
2624735c98fSJean Delvare  * the ones which can always be enumerated in practice.
2634735c98fSJean Delvare  *
2644735c98fSJean Delvare  * The i2c_client structure which is handed to the @detect callback is
2654735c98fSJean Delvare  * not a real i2c_client. It is initialized just enough so that you can
2664735c98fSJean Delvare  * call i2c_smbus_read_byte_data and friends on it. Don't do anything
2674735c98fSJean Delvare  * else with it. In particular, calling dev_dbg and friends on it is
2684735c98fSJean Delvare  * not allowed.
2691da177e4SLinus Torvalds  */
2701da177e4SLinus Torvalds struct i2c_driver {
2711da177e4SLinus Torvalds 	unsigned int class;
2721da177e4SLinus Torvalds 
273729d6dd5SJean Delvare 	/* Standard driver model interfaces */
27403c835f4SUwe Kleine-König 	int (*probe)(struct i2c_client *client);
27503c835f4SUwe Kleine-König 	void (*remove)(struct i2c_client *client);
27603c835f4SUwe Kleine-König 
277b8a1a4cdSLee Jones 
278f37dd80aSDavid Brownell 	/* driver model interfaces that don't relate to enumeration  */
279d68222d4SWolfram Sang 	void (*shutdown)(struct i2c_client *client);
280f37dd80aSDavid Brownell 
281b5527a77SJean Delvare 	/* Alert callback, for example for the SMBus alert protocol.
282b5527a77SJean Delvare 	 * The format and meaning of the data value depends on the protocol.
283b5527a77SJean Delvare 	 * For the SMBus alert protocol, there is a single bit of data passed
284b5527a77SJean Delvare 	 * as the alert response's low bit ("event flag").
285e456cd37SBenjamin Tissoires 	 * For the SMBus Host Notify protocol, the data corresponds to the
286e456cd37SBenjamin Tissoires 	 * 16-bit payload data reported by the slave device acting as master.
287b5527a77SJean Delvare 	 */
288d68222d4SWolfram Sang 	void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
289b4f21054SBenjamin Tissoires 		      unsigned int data);
290b5527a77SJean Delvare 
2911da177e4SLinus Torvalds 	/* a ioctl like command that can be used to perform specific functions
2921da177e4SLinus Torvalds 	 * with the device.
2931da177e4SLinus Torvalds 	 */
2941da177e4SLinus Torvalds 	int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	struct device_driver driver;
297d2653e92SJean Delvare 	const struct i2c_device_id *id_table;
2984735c98fSJean Delvare 
2994735c98fSJean Delvare 	/* Device detection callback for automatic device creation */
300d68222d4SWolfram Sang 	int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
301c3813d6aSJean Delvare 	const unsigned short *address_list;
3023bfa08feSWolfram Sang 	struct list_head clients;
303b18c1ad6SSakari Ailus 
304b18c1ad6SSakari Ailus 	u32 flags;
3051da177e4SLinus Torvalds };
306d69d8048SGreg Kroah-Hartman #define to_i2c_driver(d) container_of_const(d, struct i2c_driver, driver)
3071da177e4SLinus Torvalds 
3082096b956SDavid Brownell /**
3092096b956SDavid Brownell  * struct i2c_client - represent an I2C slave device
3102caea56fSWolfram Sang  * @flags: see I2C_CLIENT_* for possible flags
3112096b956SDavid Brownell  * @addr: Address used on the I2C bus connected to the parent adapter.
3122096b956SDavid Brownell  * @name: Indicates the type of the device, usually a chip name that's
3132096b956SDavid Brownell  *	generic enough to hide second-sourcing and compatible revisions.
314d64f73beSDavid Brownell  * @adapter: manages the bus segment hosting this I2C device
3152096b956SDavid Brownell  * @dev: Driver model device node for the slave.
3163ac61258SRandy Dunlap  * @init_irq: IRQ that was set at initialization
317d64f73beSDavid Brownell  * @irq: indicates the IRQ generated by this device (if any)
318c4d3dfd8SWolfram Sang  * @detected: member of an i2c_driver.clients list or i2c-core's
319c4d3dfd8SWolfram Sang  *	userspace_devices list
3204b1acc43SWolfram Sang  * @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
3214b1acc43SWolfram Sang  *	calls it to pass on slave events to the slave driver.
3225b547582SDmitry Torokhov  * @devres_group_id: id of the devres group that will be created for resources
3235b547582SDmitry Torokhov  *	acquired when probing this device.
32427c3f0e6SWolfram Sang  * @debugfs: pointer to the debugfs subdirectory which the I2C core created
32527c3f0e6SWolfram Sang  *	for this client.
3262096b956SDavid Brownell  *
3272096b956SDavid Brownell  * An i2c_client identifies a single device (i.e. chip) connected to an
328d64f73beSDavid Brownell  * i2c bus. The behaviour exposed to Linux is defined by the driver
329d64f73beSDavid Brownell  * managing the device.
3301da177e4SLinus Torvalds  */
3311da177e4SLinus Torvalds struct i2c_client {
3322096b956SDavid Brownell 	unsigned short flags;		/* div., see below		*/
3332caea56fSWolfram Sang #define I2C_CLIENT_PEC		0x04	/* Use Packet Error Checking */
3342caea56fSWolfram Sang #define I2C_CLIENT_TEN		0x10	/* we have a ten bit chip address */
3352caea56fSWolfram Sang 					/* Must equal I2C_M_TEN below */
3362caea56fSWolfram Sang #define I2C_CLIENT_SLAVE	0x20	/* we are the slave */
3372caea56fSWolfram Sang #define I2C_CLIENT_HOST_NOTIFY	0x40	/* We want to use I2C host notify */
3382caea56fSWolfram Sang #define I2C_CLIENT_WAKE		0x80	/* for board_info; true iff can wake */
3392caea56fSWolfram Sang #define I2C_CLIENT_SCCB		0x9000	/* Use Omnivision SCCB protocol */
3402caea56fSWolfram Sang 					/* Must match I2C_M_STOP|IGNORE_NAK */
3412caea56fSWolfram Sang 
3425071860aSJean Delvare 	unsigned short addr;		/* chip address - NOTE: 7bit	*/
3431da177e4SLinus Torvalds 					/* addresses are stored in the	*/
3445071860aSJean Delvare 					/* _LOWER_ 7 bits		*/
3452096b956SDavid Brownell 	char name[I2C_NAME_SIZE];
3461da177e4SLinus Torvalds 	struct i2c_adapter *adapter;	/* the adapter we sit on	*/
3471da177e4SLinus Torvalds 	struct device dev;		/* the device structure		*/
34893b6604cSJim Broadus 	int init_irq;			/* irq set at initialization	*/
3498e29da9eSWolfram Sang 	int irq;			/* irq issued by device		*/
350c4d3dfd8SWolfram Sang 	struct list_head detected;
351d5fd120eSJean Delvare #if IS_ENABLED(CONFIG_I2C_SLAVE)
3524b1acc43SWolfram Sang 	i2c_slave_cb_t slave_cb;	/* callback for slave mode	*/
353d5fd120eSJean Delvare #endif
3545b547582SDmitry Torokhov 	void *devres_group_id;		/* ID of probe devres group	*/
355d06905d6SWolfram Sang 	struct dentry *debugfs;		/* per-client debugfs dir	*/
3561da177e4SLinus Torvalds };
3571da177e4SLinus Torvalds #define to_i2c_client(d) container_of(d, struct i2c_client, dev)
3581da177e4SLinus Torvalds 
359c807da53SLuca Ceresoli struct i2c_adapter *i2c_verify_adapter(struct device *dev);
360c807da53SLuca Ceresoli const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
3615f441fcaSLee Jones 					 const struct i2c_client *client);
3629b766b81SDavid Brownell 
363564d73c4SBiju Das const void *i2c_get_match_data(const struct i2c_client *client);
364564d73c4SBiju Das 
kobj_to_i2c_client(struct kobject * kobj)365a61fc683S[email protected] static inline struct i2c_client *kobj_to_i2c_client(struct kobject *kobj)
366a61fc683S[email protected] {
367b1d4dc15STian Tao 	struct device * const dev = kobj_to_dev(kobj);
368d75d53cdSMark M. Hoffman 	return to_i2c_client(dev);
369a61fc683S[email protected] }
370a61fc683S[email protected] 
i2c_get_clientdata(const struct i2c_client * client)3717df915e5SWolfram Sang static inline void *i2c_get_clientdata(const struct i2c_client *client)
3721da177e4SLinus Torvalds {
3737df915e5SWolfram Sang 	return dev_get_drvdata(&client->dev);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
i2c_set_clientdata(struct i2c_client * client,void * data)3767df915e5SWolfram Sang static inline void i2c_set_clientdata(struct i2c_client *client, void *data)
3771da177e4SLinus Torvalds {
3787df915e5SWolfram Sang 	dev_set_drvdata(&client->dev, data);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3814b1acc43SWolfram Sang /* I2C slave support */
3824b1acc43SWolfram Sang 
3834b1acc43SWolfram Sang enum i2c_slave_event {
3845b77d162SWolfram Sang 	I2C_SLAVE_READ_REQUESTED,
3855b77d162SWolfram Sang 	I2C_SLAVE_WRITE_REQUESTED,
3865b77d162SWolfram Sang 	I2C_SLAVE_READ_PROCESSED,
3875b77d162SWolfram Sang 	I2C_SLAVE_WRITE_RECEIVED,
3884b1acc43SWolfram Sang 	I2C_SLAVE_STOP,
3894b1acc43SWolfram Sang };
3904b1acc43SWolfram Sang 
391c807da53SLuca Ceresoli int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb);
392c807da53SLuca Ceresoli int i2c_slave_unregister(struct i2c_client *client);
393d714fb25SJae Hyun Yoo int i2c_slave_event(struct i2c_client *client,
394d714fb25SJae Hyun Yoo 		    enum i2c_slave_event event, u8 *val);
3955d388143SArnd Bergmann #if IS_ENABLED(CONFIG_I2C_SLAVE)
3965d388143SArnd Bergmann bool i2c_detect_slave_mode(struct device *dev);
3978f1a357dSAndy Shevchenko #else
i2c_detect_slave_mode(struct device * dev)3988f1a357dSAndy Shevchenko static inline bool i2c_detect_slave_mode(struct device *dev) { return false; }
399d5fd120eSJean Delvare #endif
4004b1acc43SWolfram Sang 
4019c1600edSDavid Brownell /**
4029c1600edSDavid Brownell  * struct i2c_board_info - template for device creation
403eb8a7908SJean Delvare  * @type: chip type, to initialize i2c_client.name
4049c1600edSDavid Brownell  * @flags: to initialize i2c_client.flags
4059c1600edSDavid Brownell  * @addr: stored in i2c_client.addr
406728fe6ceSHans de Goede  * @dev_name: Overrides the default <busnr>-<addr> dev_name if set
4079c1600edSDavid Brownell  * @platform_data: stored in i2c_client.dev.platform_data
40872d2e9f9SRandy Dunlap  * @of_node: pointer to OpenFirmware device node
409ce793486SRafael J. Wysocki  * @fwnode: device node supplied by the platform firmware
410714638e0SHeikki Krogerus  * @swnode: software node for the device
4114124c4ebSDmitry Torokhov  * @resources: resources associated with the device
4124124c4ebSDmitry Torokhov  * @num_resources: number of resources in the @resources array
4139c1600edSDavid Brownell  * @irq: stored in i2c_client.irq
414d64f73beSDavid Brownell  *
4159c1600edSDavid Brownell  * I2C doesn't actually support hardware probing, although controllers and
4169c1600edSDavid Brownell  * devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's
4179c1600edSDavid Brownell  * a device at a given address.  Drivers commonly need more information than
4189c1600edSDavid Brownell  * that, such as chip type, configuration, associated IRQ, and so on.
4199c1600edSDavid Brownell  *
4209c1600edSDavid Brownell  * i2c_board_info is used to build tables of information listing I2C devices
421729d6dd5SJean Delvare  * that are present.  This information is used to grow the driver model tree.
422729d6dd5SJean Delvare  * For mainboards this is done statically using i2c_register_board_info();
423729d6dd5SJean Delvare  * bus numbers identify adapters that aren't yet available.  For add-on boards,
424390fd047SWolfram Sang  * i2c_new_client_device() does this dynamically with the adapter already known.
4259c1600edSDavid Brownell  */
4269c1600edSDavid Brownell struct i2c_board_info {
4279c1600edSDavid Brownell 	char		type[I2C_NAME_SIZE];
4289c1600edSDavid Brownell 	unsigned short	flags;
4299c1600edSDavid Brownell 	unsigned short	addr;
430728fe6ceSHans de Goede 	const char	*dev_name;
4319c1600edSDavid Brownell 	void		*platform_data;
432d12d42f7SGrant Likely 	struct device_node *of_node;
433ce793486SRafael J. Wysocki 	struct fwnode_handle *fwnode;
434714638e0SHeikki Krogerus 	const struct software_node *swnode;
4354124c4ebSDmitry Torokhov 	const struct resource *resources;
4364124c4ebSDmitry Torokhov 	unsigned int	num_resources;
4379c1600edSDavid Brownell 	int		irq;
4389c1600edSDavid Brownell };
4399c1600edSDavid Brownell 
4409c1600edSDavid Brownell /**
4413760f736SJean Delvare  * I2C_BOARD_INFO - macro used to list an i2c device and its address
4423760f736SJean Delvare  * @dev_type: identifies the device type
4439c1600edSDavid Brownell  * @dev_addr: the device's address on the bus.
4449c1600edSDavid Brownell  *
4459c1600edSDavid Brownell  * This macro initializes essential fields of a struct i2c_board_info,
4469c1600edSDavid Brownell  * declaring what has been provided on a particular board.  Optional
4473760f736SJean Delvare  * fields (such as associated irq, or device-specific platform_data)
4483760f736SJean Delvare  * are provided using conventional syntax.
4499c1600edSDavid Brownell  */
4503760f736SJean Delvare #define I2C_BOARD_INFO(dev_type, dev_addr) \
451c758e8cfSBen Hutchings 	.type = dev_type, .addr = (dev_addr)
4529c1600edSDavid Brownell 
4539c1600edSDavid Brownell 
454901a891fSStefan Lengfeld #if IS_ENABLED(CONFIG_I2C)
455390fd047SWolfram Sang /*
456390fd047SWolfram Sang  * Add-on boards should register/unregister their devices; e.g. a board
4579c1600edSDavid Brownell  * with integrated I2C, a config eeprom, sensors, and a codec that's
4589c1600edSDavid Brownell  * used in conjunction with the primary hardware.
4599c1600edSDavid Brownell  */
460c807da53SLuca Ceresoli struct i2c_client *
461550113d4SWolfram Sang i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info);
462550113d4SWolfram Sang 
46312b5053aSJean Delvare /* If you don't know the exact address of an I2C device, use this variant
46412b5053aSJean Delvare  * instead, which can probe for device presence in a list of possible
4659a94241aSJean Delvare  * addresses. The "probe" callback function is optional. If it is provided,
4669a94241aSJean Delvare  * it must return 1 on successful probe, 0 otherwise. If it is not provided,
4679a94241aSJean Delvare  * a default probing method is used.
46812b5053aSJean Delvare  */
469c807da53SLuca Ceresoli struct i2c_client *
470c1d08475SWolfram Sang i2c_new_scanned_device(struct i2c_adapter *adap,
471c1d08475SWolfram Sang 		       struct i2c_board_info *info,
472c1d08475SWolfram Sang 		       unsigned short const *addr_list,
473c1d08475SWolfram Sang 		       int (*probe)(struct i2c_adapter *adap, unsigned short addr));
474c1d08475SWolfram Sang 
475d44f19d5SJean Delvare /* Common custom probe functions */
476c807da53SLuca Ceresoli int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr);
477d44f19d5SJean Delvare 
478c807da53SLuca Ceresoli struct i2c_client *
479550113d4SWolfram Sang i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address);
480550113d4SWolfram Sang 
481c807da53SLuca Ceresoli struct i2c_client *
482b8f5fe3bSHeiner Kallweit devm_i2c_new_dummy_device(struct device *dev, struct i2c_adapter *adap, u16 address);
483b8f5fe3bSHeiner Kallweit 
484c807da53SLuca Ceresoli struct i2c_client *
485af80559bSWolfram Sang i2c_new_ancillary_device(struct i2c_client *client,
4860f614d83SJean-Michel Hautbois 			 const char *name,
4870f614d83SJean-Michel Hautbois 			 u16 default_addr);
4880f614d83SJean-Michel Hautbois 
489c807da53SLuca Ceresoli void i2c_unregister_device(struct i2c_client *client);
490aa5c8b25SJonathan Cameron 
491aa5c8b25SJonathan Cameron struct i2c_client *i2c_verify_client(struct device *dev);
492aa5c8b25SJonathan Cameron #else
i2c_verify_client(struct device * dev)493aa5c8b25SJonathan Cameron static inline struct i2c_client *i2c_verify_client(struct device *dev)
494aa5c8b25SJonathan Cameron {
495aa5c8b25SJonathan Cameron 	return NULL;
496aa5c8b25SJonathan Cameron }
49723af8400SJean Delvare #endif /* I2C */
4989c1600edSDavid Brownell 
4999c1600edSDavid Brownell /* Mainboard arch_initcall() code should register all its I2C devices.
5009c1600edSDavid Brownell  * This is done at arch_initcall time, before declaring any i2c adapters.
5019c1600edSDavid Brownell  * Modules for add-on boards must use other calls.
5029c1600edSDavid Brownell  */
50326386763SDavid Brownell #ifdef CONFIG_I2C_BOARDINFO
504c807da53SLuca Ceresoli int
5053ae70deeSJean Delvare i2c_register_board_info(int busnum, struct i2c_board_info const *info,
5063ae70deeSJean Delvare 			unsigned n);
50726386763SDavid Brownell #else
50826386763SDavid Brownell static inline int
i2c_register_board_info(int busnum,struct i2c_board_info const * info,unsigned n)5093ae70deeSJean Delvare i2c_register_board_info(int busnum, struct i2c_board_info const *info,
5103ae70deeSJean Delvare 			unsigned n)
51126386763SDavid Brownell {
51226386763SDavid Brownell 	return 0;
51326386763SDavid Brownell }
51423af8400SJean Delvare #endif /* I2C_BOARDINFO */
5159c1600edSDavid Brownell 
51625ee33fbSBen Dooks /**
517385ac870SWolfram Sang  * struct i2c_algorithm - represent I2C transfer methods
518385ac870SWolfram Sang  * @xfer: Transfer a given number of messages defined by the msgs array via
519385ac870SWolfram Sang  *   the specified adapter.
520385ac870SWolfram Sang  * @xfer_atomic: Same as @xfer. Yet, only using atomic context so e.g. PMICs
521385ac870SWolfram Sang  *   can be accessed very late before shutdown. Optional.
522385ac870SWolfram Sang  * @smbus_xfer: Issue SMBus transactions to the given I2C adapter. If this
52325ee33fbSBen Dooks  *   is not present, then the bus layer will try and convert the SMBus calls
52425ee33fbSBen Dooks  *   into I2C transfers instead.
525385ac870SWolfram Sang  * @smbus_xfer_atomic: Same as @smbus_xfer. Yet, only using atomic context
52663b96983SWolfram Sang  *   so e.g. PMICs can be accessed very late before shutdown. Optional.
52725ee33fbSBen Dooks  * @functionality: Return the flags that this algorithm/adapter pair supports
528692b65c8SMauro Carvalho Chehab  *   from the ``I2C_FUNC_*`` flags.
5295d89b5bdSWolfram Sang  * @reg_target: Register given client to local target mode of this adapter
5305d89b5bdSWolfram Sang  * @unreg_target: Unregister given client from local target mode of this adapter
5315d89b5bdSWolfram Sang  *
5325d89b5bdSWolfram Sang  * @master_xfer: deprecated, use @xfer
5335d89b5bdSWolfram Sang  * @master_xfer_atomic: deprecated, use @xfer_atomic
5345d89b5bdSWolfram Sang  * @reg_slave: deprecated, use @reg_target
5355d89b5bdSWolfram Sang  * @unreg_slave: deprecated, use @unreg_target
5365d89b5bdSWolfram Sang  *
5371da177e4SLinus Torvalds  * i2c_algorithm is the interface to a class of hardware solutions which can
5381da177e4SLinus Torvalds  * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
5391da177e4SLinus Torvalds  * to name two of the most common.
54025ee33fbSBen Dooks  *
5415d89b5bdSWolfram Sang  * The return codes from the ``xfer{_atomic}`` fields should indicate the
54263b96983SWolfram Sang  * type of error code that occurred during the transfer, as documented in the
543a1182149SWolfram Sang  * Kernel Documentation file Documentation/i2c/fault-codes.rst. Otherwise, the
544a1182149SWolfram Sang  * number of messages executed should be returned.
5451da177e4SLinus Torvalds  */
5461da177e4SLinus Torvalds struct i2c_algorithm {
547f6ac28d6SWolfram Sang 	/*
5485d89b5bdSWolfram Sang 	 * If an adapter algorithm can't do I2C-level access, set xfer
549f6ac28d6SWolfram Sang 	 * to NULL. If an adapter algorithm can do SMBus access, set
550f6ac28d6SWolfram Sang 	 * smbus_xfer. If set to NULL, the SMBus protocol is simulated
551f6ac28d6SWolfram Sang 	 * using common I2C messages.
552f6ac28d6SWolfram Sang 	 */
553a93c2e5fSWolfram Sang 	union {
554a93c2e5fSWolfram Sang 		int (*xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
555a93c2e5fSWolfram Sang 			    int num);
5561da177e4SLinus Torvalds 		int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
5571da177e4SLinus Torvalds 				   int num);
558a93c2e5fSWolfram Sang 	};
559a93c2e5fSWolfram Sang 	union {
560a93c2e5fSWolfram Sang 		int (*xfer_atomic)(struct i2c_adapter *adap,
561a93c2e5fSWolfram Sang 				   struct i2c_msg *msgs, int num);
56263b96983SWolfram Sang 		int (*master_xfer_atomic)(struct i2c_adapter *adap,
56363b96983SWolfram Sang 					   struct i2c_msg *msgs, int num);
564a93c2e5fSWolfram Sang 	};
5651da177e4SLinus Torvalds 	int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
5661da177e4SLinus Torvalds 			  unsigned short flags, char read_write,
5671da177e4SLinus Torvalds 			  u8 command, int size, union i2c_smbus_data *data);
56863b96983SWolfram Sang 	int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,
56963b96983SWolfram Sang 				 unsigned short flags, char read_write,
57063b96983SWolfram Sang 				 u8 command, int size, union i2c_smbus_data *data);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* To determine what the adapter supports */
573f6ac28d6SWolfram Sang 	u32 (*functionality)(struct i2c_adapter *adap);
5744b1acc43SWolfram Sang 
575d5fd120eSJean Delvare #if IS_ENABLED(CONFIG_I2C_SLAVE)
576a93c2e5fSWolfram Sang 	union {
577a93c2e5fSWolfram Sang 		int (*reg_target)(struct i2c_client *client);
5784b1acc43SWolfram Sang 		int (*reg_slave)(struct i2c_client *client);
579a93c2e5fSWolfram Sang 	};
580a93c2e5fSWolfram Sang 	union {
581a93c2e5fSWolfram Sang 		int (*unreg_target)(struct i2c_client *client);
5824b1acc43SWolfram Sang 		int (*unreg_slave)(struct i2c_client *client);
583a93c2e5fSWolfram Sang 	};
584d5fd120eSJean Delvare #endif
5851da177e4SLinus Torvalds };
5861da177e4SLinus Torvalds 
5875f9296baSViresh Kumar /**
588d1ed7985SPeter Rosin  * struct i2c_lock_operations - represent I2C locking operations
589d1ed7985SPeter Rosin  * @lock_bus: Get exclusive access to an I2C bus segment
590d1ed7985SPeter Rosin  * @trylock_bus: Try to get exclusive access to an I2C bus segment
591d1ed7985SPeter Rosin  * @unlock_bus: Release exclusive access to an I2C bus segment
592d1ed7985SPeter Rosin  *
593d1ed7985SPeter Rosin  * The main operations are wrapped by i2c_lock_bus and i2c_unlock_bus.
594d1ed7985SPeter Rosin  */
595d1ed7985SPeter Rosin struct i2c_lock_operations {
596d68222d4SWolfram Sang 	void (*lock_bus)(struct i2c_adapter *adapter, unsigned int flags);
597d68222d4SWolfram Sang 	int (*trylock_bus)(struct i2c_adapter *adapter, unsigned int flags);
598d68222d4SWolfram Sang 	void (*unlock_bus)(struct i2c_adapter *adapter, unsigned int flags);
599d1ed7985SPeter Rosin };
600d1ed7985SPeter Rosin 
601d1ed7985SPeter Rosin /**
602e1dba01cSWolfram Sang  * struct i2c_timings - I2C timing information
603e1dba01cSWolfram Sang  * @bus_freq_hz: the bus frequency in Hz
604e1dba01cSWolfram Sang  * @scl_rise_ns: time SCL signal takes to rise in ns; t(r) in the I2C specification
605e1dba01cSWolfram Sang  * @scl_fall_ns: time SCL signal takes to fall in ns; t(f) in the I2C specification
606e1dba01cSWolfram Sang  * @scl_int_delay_ns: time IP core additionally needs to setup SCL in ns
607e1dba01cSWolfram Sang  * @sda_fall_ns: time SDA signal takes to fall in ns; t(f) in the I2C specification
6084717be73SAndy Shevchenko  * @sda_hold_ns: time IP core additionally needs to hold SDA in ns
609b84dfe1aSEugen Hristev  * @digital_filter_width_ns: width in ns of spikes on i2c lines that the IP core
610b84dfe1aSEugen Hristev  *	digital filter can filter out
611b84dfe1aSEugen Hristev  * @analog_filter_cutoff_freq_hz: threshold frequency for the low pass IP core
612b84dfe1aSEugen Hristev  *	analog filter
613e1dba01cSWolfram Sang  */
614e1dba01cSWolfram Sang struct i2c_timings {
615e1dba01cSWolfram Sang 	u32 bus_freq_hz;
616e1dba01cSWolfram Sang 	u32 scl_rise_ns;
617e1dba01cSWolfram Sang 	u32 scl_fall_ns;
618e1dba01cSWolfram Sang 	u32 scl_int_delay_ns;
619e1dba01cSWolfram Sang 	u32 sda_fall_ns;
6204717be73SAndy Shevchenko 	u32 sda_hold_ns;
621b84dfe1aSEugen Hristev 	u32 digital_filter_width_ns;
622b84dfe1aSEugen Hristev 	u32 analog_filter_cutoff_freq_hz;
623e1dba01cSWolfram Sang };
624e1dba01cSWolfram Sang 
625e1dba01cSWolfram Sang /**
6265f9296baSViresh Kumar  * struct i2c_bus_recovery_info - I2C bus recovery information
6275f9296baSViresh Kumar  * @recover_bus: Recover routine. Either pass driver's recover_bus() routine, or
628e1eb7d28SPhil Reid  *	i2c_generic_scl_recovery().
6295f9296baSViresh Kumar  * @get_scl: This gets current value of SCL line. Mandatory for generic SCL
630766a4f27SWolfram Sang  *      recovery. Populated internally for generic GPIO recovery.
631766a4f27SWolfram Sang  * @set_scl: This sets/clears the SCL line. Mandatory for generic SCL recovery.
632766a4f27SWolfram Sang  *      Populated internally for generic GPIO recovery.
633ffc59c49SWolfram Sang  * @get_sda: This gets current value of SDA line. This or set_sda() is mandatory
634ffc59c49SWolfram Sang  *	for generic SCL recovery. Populated internally, if sda_gpio is a valid
635ffc59c49SWolfram Sang  *	GPIO, for generic GPIO recovery.
636ffc59c49SWolfram Sang  * @set_sda: This sets/clears the SDA line. This or get_sda() is mandatory for
637ffc59c49SWolfram Sang  *	generic SCL recovery. Populated internally, if sda_gpio is a valid GPIO,
638ffc59c49SWolfram Sang  *	for generic GPIO recovery.
6397ca5f6beSWolfram Sang  * @get_bus_free: Returns the bus free state as seen from the IP core in case it
6407ca5f6beSWolfram Sang  *	has a more complex internal logic than just reading SDA. Optional.
6415f9296baSViresh Kumar  * @prepare_recovery: This will be called before starting recovery. Platform may
6425f9296baSViresh Kumar  *	configure padmux here for SDA/SCL line or something else they want.
6435f9296baSViresh Kumar  * @unprepare_recovery: This will be called after completing recovery. Platform
6445f9296baSViresh Kumar  *	may configure padmux here for SDA/SCL line or something else they want.
6453991c5c8SPhil Reid  * @scl_gpiod: gpiod of the SCL line. Only required for GPIO recovery.
6463991c5c8SPhil Reid  * @sda_gpiod: gpiod of the SDA line. Only required for GPIO recovery.
64775820314SCodrin Ciubotariu  * @pinctrl: pinctrl used by GPIO recovery to change the state of the I2C pins.
64875820314SCodrin Ciubotariu  *      Optional.
64975820314SCodrin Ciubotariu  * @pins_default: default pinctrl state of SCL/SDA lines, when they are assigned
65075820314SCodrin Ciubotariu  *      to the I2C bus. Optional. Populated internally for GPIO recovery, if
65175820314SCodrin Ciubotariu  *      state with the name PINCTRL_STATE_DEFAULT is found and pinctrl is valid.
65275820314SCodrin Ciubotariu  * @pins_gpio: recovery pinctrl state of SCL/SDA lines, when they are used as
65375820314SCodrin Ciubotariu  *      GPIOs. Optional. Populated internally for GPIO recovery, if this state
65475820314SCodrin Ciubotariu  *      is called "gpio" or "recovery" and pinctrl is valid.
6555f9296baSViresh Kumar  */
6565f9296baSViresh Kumar struct i2c_bus_recovery_info {
6576c92204eSWolfram Sang 	int (*recover_bus)(struct i2c_adapter *adap);
6585f9296baSViresh Kumar 
6596c92204eSWolfram Sang 	int (*get_scl)(struct i2c_adapter *adap);
6606c92204eSWolfram Sang 	void (*set_scl)(struct i2c_adapter *adap, int val);
6616c92204eSWolfram Sang 	int (*get_sda)(struct i2c_adapter *adap);
6628092178fSWolfram Sang 	void (*set_sda)(struct i2c_adapter *adap, int val);
6637ca5f6beSWolfram Sang 	int (*get_bus_free)(struct i2c_adapter *adap);
6645f9296baSViresh Kumar 
6656c92204eSWolfram Sang 	void (*prepare_recovery)(struct i2c_adapter *adap);
6666c92204eSWolfram Sang 	void (*unprepare_recovery)(struct i2c_adapter *adap);
6675f9296baSViresh Kumar 
6685f9296baSViresh Kumar 	/* gpio recovery */
6693991c5c8SPhil Reid 	struct gpio_desc *scl_gpiod;
6703991c5c8SPhil Reid 	struct gpio_desc *sda_gpiod;
67175820314SCodrin Ciubotariu 	struct pinctrl *pinctrl;
67275820314SCodrin Ciubotariu 	struct pinctrl_state *pins_default;
67375820314SCodrin Ciubotariu 	struct pinctrl_state *pins_gpio;
6745f9296baSViresh Kumar };
6755f9296baSViresh Kumar 
6765f9296baSViresh Kumar int i2c_recover_bus(struct i2c_adapter *adap);
6775f9296baSViresh Kumar 
6785f9296baSViresh Kumar /* Generic recovery routines */
6795f9296baSViresh Kumar int i2c_generic_scl_recovery(struct i2c_adapter *adap);
6805f9296baSViresh Kumar 
6812187f03aSWolfram Sang /**
6822187f03aSWolfram Sang  * struct i2c_adapter_quirks - describe flaws of an i2c adapter
6832187f03aSWolfram Sang  * @flags: see I2C_AQ_* for possible flags and read below
6842187f03aSWolfram Sang  * @max_num_msgs: maximum number of messages per transfer
6852187f03aSWolfram Sang  * @max_write_len: maximum length of a write message
6862187f03aSWolfram Sang  * @max_read_len: maximum length of a read message
6872187f03aSWolfram Sang  * @max_comb_1st_msg_len: maximum length of the first msg in a combined message
6882187f03aSWolfram Sang  * @max_comb_2nd_msg_len: maximum length of the second msg in a combined message
6892187f03aSWolfram Sang  *
6902187f03aSWolfram Sang  * Note about combined messages: Some I2C controllers can only send one message
6912187f03aSWolfram Sang  * per transfer, plus something called combined message or write-then-read.
6922187f03aSWolfram Sang  * This is (usually) a small write message followed by a read message and
6932187f03aSWolfram Sang  * barely enough to access register based devices like EEPROMs. There is a flag
6942187f03aSWolfram Sang  * to support this mode. It implies max_num_msg = 2 and does the length checks
6952187f03aSWolfram Sang  * with max_comb_*_len because combined message mode usually has its own
6962187f03aSWolfram Sang  * limitations. Because of HW implementations, some controllers can actually do
6972187f03aSWolfram Sang  * write-then-anything or other variants. To support that, write-then-read has
6982187f03aSWolfram Sang  * been broken out into smaller bits like write-first and read-second which can
6992187f03aSWolfram Sang  * be combined as needed.
7002187f03aSWolfram Sang  */
7012187f03aSWolfram Sang 
7022187f03aSWolfram Sang struct i2c_adapter_quirks {
7032187f03aSWolfram Sang 	u64 flags;
7042187f03aSWolfram Sang 	int max_num_msgs;
7052187f03aSWolfram Sang 	u16 max_write_len;
7062187f03aSWolfram Sang 	u16 max_read_len;
7072187f03aSWolfram Sang 	u16 max_comb_1st_msg_len;
7082187f03aSWolfram Sang 	u16 max_comb_2nd_msg_len;
7092187f03aSWolfram Sang };
7102187f03aSWolfram Sang 
7112187f03aSWolfram Sang /* enforce max_num_msgs = 2 and use max_comb_*_len for length checks */
7122187f03aSWolfram Sang #define I2C_AQ_COMB			BIT(0)
7132187f03aSWolfram Sang /* first combined message must be write */
7142187f03aSWolfram Sang #define I2C_AQ_COMB_WRITE_FIRST		BIT(1)
7152187f03aSWolfram Sang /* second combined message must be read */
7162187f03aSWolfram Sang #define I2C_AQ_COMB_READ_SECOND		BIT(2)
7172187f03aSWolfram Sang /* both combined messages must have the same target address */
7182187f03aSWolfram Sang #define I2C_AQ_COMB_SAME_ADDR		BIT(3)
7192187f03aSWolfram Sang /* convenience macro for typical write-then read case */
7202187f03aSWolfram Sang #define I2C_AQ_COMB_WRITE_THEN_READ	(I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \
7212187f03aSWolfram Sang 					 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR)
722749de3daSNicola Corna /* clock stretching is not supported */
723749de3daSNicola Corna #define I2C_AQ_NO_CLK_STRETCH		BIT(4)
724d9cfe2ceSWolfram Sang /* message cannot have length of 0 */
725d9cfe2ceSWolfram Sang #define I2C_AQ_NO_ZERO_LEN_READ		BIT(5)
726d9cfe2ceSWolfram Sang #define I2C_AQ_NO_ZERO_LEN_WRITE	BIT(6)
727d9cfe2ceSWolfram Sang #define I2C_AQ_NO_ZERO_LEN		(I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE)
728aca01415SBence Csókás /* adapter cannot do repeated START */
729aca01415SBence Csókás #define I2C_AQ_NO_REP_START		BIT(7)
7302187f03aSWolfram Sang 
7311da177e4SLinus Torvalds /*
7321da177e4SLinus Torvalds  * i2c_adapter is the structure used to identify a physical i2c bus along
7331da177e4SLinus Torvalds  * with the access algorithms necessary to access it.
7341da177e4SLinus Torvalds  */
7351da177e4SLinus Torvalds struct i2c_adapter {
7361da177e4SLinus Torvalds 	struct module *owner;
73714f55f7aSWolfram Sang 	unsigned int class;		  /* classes to allow probing for */
738af71ff69SDavid Brownell 	const struct i2c_algorithm *algo; /* the algorithm to access the bus */
7391da177e4SLinus Torvalds 	void *algo_data;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	/* data fields that are valid for all devices	*/
742d1ed7985SPeter Rosin 	const struct i2c_lock_operations *lock_ops;
743194684e5SMika Kuoppala 	struct rt_mutex bus_lock;
7446ef91fccSPeter Rosin 	struct rt_mutex mux_lock;
7451da177e4SLinus Torvalds 
746cd97f39bSJean Delvare 	int timeout;			/* in jiffies */
7471da177e4SLinus Torvalds 	int retries;
7481da177e4SLinus Torvalds 	struct device dev;		/* the adapter device */
7499ac6cb5fSWolfram Sang 	unsigned long locked_flags;	/* owned by the I2C core */
7509ac6cb5fSWolfram Sang #define I2C_ALF_IS_SUSPENDED		0
7514db61c2aSWolfram Sang #define I2C_ALF_SUSPEND_REPORTED	1
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	int nr;
7542096b956SDavid Brownell 	char name[48];
7551da177e4SLinus Torvalds 	struct completion dev_released;
7566629dcffSJean Delvare 
757c4d3dfd8SWolfram Sang 	struct mutex userspace_clients_lock;
758c4d3dfd8SWolfram Sang 	struct list_head userspace_clients;
759c4d3dfd8SWolfram Sang 
7605f9296baSViresh Kumar 	struct i2c_bus_recovery_info *bus_recovery_info;
7612187f03aSWolfram Sang 	const struct i2c_adapter_quirks *quirks;
7624d5538f5SBenjamin Tissoires 
7634d5538f5SBenjamin Tissoires 	struct irq_domain *host_notify_domain;
7645a7b95fbSBibby Hsieh 	struct regulator *bus_regulator;
76573febd77SWolfram Sang 
76673febd77SWolfram Sang 	struct dentry *debugfs;
7678d3cefafSHeiner Kallweit 
7688d3cefafSHeiner Kallweit 	/* 7bit address space */
7698d3cefafSHeiner Kallweit 	DECLARE_BITMAP(addrs_in_instantiation, 1 << 7);
7701da177e4SLinus Torvalds };
771ef2c8321SDavid Brownell #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
7721da177e4SLinus Torvalds 
i2c_get_adapdata(const struct i2c_adapter * adap)773e1714daaSWolfram Sang static inline void *i2c_get_adapdata(const struct i2c_adapter *adap)
7741da177e4SLinus Torvalds {
775e1714daaSWolfram Sang 	return dev_get_drvdata(&adap->dev);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
i2c_set_adapdata(struct i2c_adapter * adap,void * data)778e1714daaSWolfram Sang static inline void i2c_set_adapdata(struct i2c_adapter *adap, void *data)
7791da177e4SLinus Torvalds {
780e1714daaSWolfram Sang 	dev_set_drvdata(&adap->dev, data);
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
78397cc4d49SJean Delvare static inline struct i2c_adapter *
i2c_parent_is_i2c_adapter(const struct i2c_adapter * adapter)78497cc4d49SJean Delvare i2c_parent_is_i2c_adapter(const struct i2c_adapter *adapter)
7850826374bSMichael Lawnick {
7862fac2b89SStephen Warren #if IS_ENABLED(CONFIG_I2C_MUX)
78797cc4d49SJean Delvare 	struct device *parent = adapter->dev.parent;
78897cc4d49SJean Delvare 
78997cc4d49SJean Delvare 	if (parent != NULL && parent->type == &i2c_adapter_type)
79097cc4d49SJean Delvare 		return to_i2c_adapter(parent);
79197cc4d49SJean Delvare 	else
7923923172bSPhil Carmody #endif
79397cc4d49SJean Delvare 		return NULL;
7940826374bSMichael Lawnick }
7950826374bSMichael Lawnick 
796d68222d4SWolfram Sang int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data));
7977ae31482SJean Delvare 
798fe61e07eSJean Delvare /* Adapter locking functions, exported for shared pin cases */
7998320f495SPeter Rosin #define I2C_LOCK_ROOT_ADAPTER BIT(0)
8008320f495SPeter Rosin #define I2C_LOCK_SEGMENT      BIT(1)
8018320f495SPeter Rosin 
8028320f495SPeter Rosin /**
8038320f495SPeter Rosin  * i2c_lock_bus - Get exclusive access to an I2C bus segment
8048320f495SPeter Rosin  * @adapter: Target I2C bus segment
8058320f495SPeter Rosin  * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
8068320f495SPeter Rosin  *	locks only this branch in the adapter tree
8078320f495SPeter Rosin  */
8088320f495SPeter Rosin static inline void
i2c_lock_bus(struct i2c_adapter * adapter,unsigned int flags)8098320f495SPeter Rosin i2c_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
8108320f495SPeter Rosin {
811d1ed7985SPeter Rosin 	adapter->lock_ops->lock_bus(adapter, flags);
8128320f495SPeter Rosin }
8138320f495SPeter Rosin 
8148320f495SPeter Rosin /**
815fb79e09aSPeter Rosin  * i2c_trylock_bus - Try to get exclusive access to an I2C bus segment
816fb79e09aSPeter Rosin  * @adapter: Target I2C bus segment
817fb79e09aSPeter Rosin  * @flags: I2C_LOCK_ROOT_ADAPTER tries to locks the root i2c adapter,
818fb79e09aSPeter Rosin  *	I2C_LOCK_SEGMENT tries to lock only this branch in the adapter tree
819fb79e09aSPeter Rosin  *
820fb79e09aSPeter Rosin  * Return: true if the I2C bus segment is locked, false otherwise
821fb79e09aSPeter Rosin  */
822fb79e09aSPeter Rosin static inline int
i2c_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)823fb79e09aSPeter Rosin i2c_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
824fb79e09aSPeter Rosin {
825d1ed7985SPeter Rosin 	return adapter->lock_ops->trylock_bus(adapter, flags);
826fb79e09aSPeter Rosin }
827fb79e09aSPeter Rosin 
828fb79e09aSPeter Rosin /**
8298320f495SPeter Rosin  * i2c_unlock_bus - Release exclusive access to an I2C bus segment
8308320f495SPeter Rosin  * @adapter: Target I2C bus segment
8318320f495SPeter Rosin  * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
8328320f495SPeter Rosin  *	unlocks only this branch in the adapter tree
8338320f495SPeter Rosin  */
8348320f495SPeter Rosin static inline void
i2c_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)8358320f495SPeter Rosin i2c_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
8368320f495SPeter Rosin {
837d1ed7985SPeter Rosin 	adapter->lock_ops->unlock_bus(adapter, flags);
8388320f495SPeter Rosin }
8398320f495SPeter Rosin 
8409ac6cb5fSWolfram Sang /**
8419ac6cb5fSWolfram Sang  * i2c_mark_adapter_suspended - Report suspended state of the adapter to the core
8429ac6cb5fSWolfram Sang  * @adap: Adapter to mark as suspended
8439ac6cb5fSWolfram Sang  *
8449ac6cb5fSWolfram Sang  * When using this helper to mark an adapter as suspended, the core will reject
8459ac6cb5fSWolfram Sang  * further transfers to this adapter. The usage of this helper is optional but
8469ac6cb5fSWolfram Sang  * recommended for devices having distinct handlers for system suspend and
8479ac6cb5fSWolfram Sang  * runtime suspend. More complex devices are free to implement custom solutions
8489ac6cb5fSWolfram Sang  * to reject transfers when suspended.
8499ac6cb5fSWolfram Sang  */
i2c_mark_adapter_suspended(struct i2c_adapter * adap)8509ac6cb5fSWolfram Sang static inline void i2c_mark_adapter_suspended(struct i2c_adapter *adap)
8519ac6cb5fSWolfram Sang {
8529ac6cb5fSWolfram Sang 	i2c_lock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
8539ac6cb5fSWolfram Sang 	set_bit(I2C_ALF_IS_SUSPENDED, &adap->locked_flags);
8549ac6cb5fSWolfram Sang 	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
8559ac6cb5fSWolfram Sang }
8569ac6cb5fSWolfram Sang 
8579ac6cb5fSWolfram Sang /**
8589ac6cb5fSWolfram Sang  * i2c_mark_adapter_resumed - Report resumed state of the adapter to the core
8599ac6cb5fSWolfram Sang  * @adap: Adapter to mark as resumed
8609ac6cb5fSWolfram Sang  *
8619ac6cb5fSWolfram Sang  * When using this helper to mark an adapter as resumed, the core will allow
8629ac6cb5fSWolfram Sang  * further transfers to this adapter. See also further notes to
8639ac6cb5fSWolfram Sang  * @i2c_mark_adapter_suspended().
8649ac6cb5fSWolfram Sang  */
i2c_mark_adapter_resumed(struct i2c_adapter * adap)8659ac6cb5fSWolfram Sang static inline void i2c_mark_adapter_resumed(struct i2c_adapter *adap)
8669ac6cb5fSWolfram Sang {
8679ac6cb5fSWolfram Sang 	i2c_lock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
8689ac6cb5fSWolfram Sang 	clear_bit(I2C_ALF_IS_SUSPENDED, &adap->locked_flags);
8699ac6cb5fSWolfram Sang 	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
8709ac6cb5fSWolfram Sang }
8719ac6cb5fSWolfram Sang 
8721da177e4SLinus Torvalds /* i2c adapter classes (bitmask) */
8731da177e4SLinus Torvalds #define I2C_CLASS_HWMON		(1<<0)	/* lm_sensors, ... */
8749c800349SWolfram Sang /* Warn users that the adapter doesn't support classes anymore */
8759c800349SWolfram Sang #define I2C_CLASS_DEPRECATED	(1<<8)
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /* Internal numbers to terminate lists */
8781da177e4SLinus Torvalds #define I2C_CLIENT_END		0xfffeU
8791da177e4SLinus Torvalds 
880c7036673SHans Verkuil /* Construct an I2C_CLIENT_END-terminated array of i2c addresses */
881c7036673SHans Verkuil #define I2C_ADDRS(addr, addrs...) \
882c7036673SHans Verkuil 	((const unsigned short []){ addr, ## addrs, I2C_CLIENT_END })
883c7036673SHans Verkuil 
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /* ----- functions exported by i2c.o */
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds /* administration...
8881da177e4SLinus Torvalds  */
889901a891fSStefan Lengfeld #if IS_ENABLED(CONFIG_I2C)
890c807da53SLuca Ceresoli int i2c_add_adapter(struct i2c_adapter *adap);
89107740c92SYicong Yang int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter);
892c807da53SLuca Ceresoli void i2c_del_adapter(struct i2c_adapter *adap);
893c807da53SLuca Ceresoli int i2c_add_numbered_adapter(struct i2c_adapter *adap);
8941da177e4SLinus Torvalds 
895c807da53SLuca Ceresoli int i2c_register_driver(struct module *owner, struct i2c_driver *driver);
896c807da53SLuca Ceresoli void i2c_del_driver(struct i2c_driver *driver);
8971da177e4SLinus Torvalds 
898eb5589a8SPaul Gortmaker /* use a define to avoid include chaining to get THIS_MODULE */
899eb5589a8SPaul Gortmaker #define i2c_add_driver(driver) \
900eb5589a8SPaul Gortmaker 	i2c_register_driver(THIS_MODULE, driver)
901de59cf9eSGreg Kroah-Hartman 
i2c_client_has_driver(struct i2c_client * client)9028c9312a9SWolfram Sang static inline bool i2c_client_has_driver(struct i2c_client *client)
9038c9312a9SWolfram Sang {
9048c9312a9SWolfram Sang 	return !IS_ERR_OR_NULL(client) && client->dev.driver;
9058c9312a9SWolfram Sang }
9068c9312a9SWolfram Sang 
9071da177e4SLinus Torvalds /* call the i2c_client->command() of all attached clients with
9081da177e4SLinus Torvalds  * the given arguments */
909c807da53SLuca Ceresoli void i2c_clients_command(struct i2c_adapter *adap,
9101da177e4SLinus Torvalds 			 unsigned int cmd, void *arg);
9111da177e4SLinus Torvalds 
912c807da53SLuca Ceresoli struct i2c_adapter *i2c_get_adapter(int nr);
913c807da53SLuca Ceresoli void i2c_put_adapter(struct i2c_adapter *adap);
914c807da53SLuca Ceresoli unsigned int i2c_adapter_depth(struct i2c_adapter *adapter);
9151da177e4SLinus Torvalds 
916e1dba01cSWolfram Sang void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds /* Return the functionality mask */
i2c_get_functionality(struct i2c_adapter * adap)9191da177e4SLinus Torvalds static inline u32 i2c_get_functionality(struct i2c_adapter *adap)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	return adap->algo->functionality(adap);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /* Return 1 if adapter supports everything we need, 0 if not. */
i2c_check_functionality(struct i2c_adapter * adap,u32 func)9251da177e4SLinus Torvalds static inline int i2c_check_functionality(struct i2c_adapter *adap, u32 func)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	return (func & i2c_get_functionality(adap)) == func;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
930cfa576dbSNicola Corna /**
931cfa576dbSNicola Corna  * i2c_check_quirks() - Function for checking the quirk flags in an i2c adapter
932cfa576dbSNicola Corna  * @adap: i2c adapter
933cfa576dbSNicola Corna  * @quirks: quirk flags
934cfa576dbSNicola Corna  *
935cfa576dbSNicola Corna  * Return: true if the adapter has all the specified quirk flags, false if not
936cfa576dbSNicola Corna  */
i2c_check_quirks(struct i2c_adapter * adap,u64 quirks)937cfa576dbSNicola Corna static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
938cfa576dbSNicola Corna {
939cfa576dbSNicola Corna 	if (!adap->quirks)
940cfa576dbSNicola Corna 		return false;
941cfa576dbSNicola Corna 	return (adap->quirks->flags & quirks) == quirks;
942cfa576dbSNicola Corna }
943cfa576dbSNicola Corna 
9443ae70deeSJean Delvare /* Return the adapter number for a specific adapter */
i2c_adapter_id(struct i2c_adapter * adap)945cdcb1921SJean Delvare static inline int i2c_adapter_id(struct i2c_adapter *adap)
946cdcb1921SJean Delvare {
947cdcb1921SJean Delvare 	return adap->nr;
948cdcb1921SJean Delvare }
9497c92784aSLars-Peter Clausen 
i2c_8bit_addr_from_msg(const struct i2c_msg * msg)950a16d6ebcSWolfram Sang static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
951a16d6ebcSWolfram Sang {
952f311507cSHsin-Yu.Chen 	return (msg->addr << 1) | (msg->flags & I2C_M_RD);
953a16d6ebcSWolfram Sang }
954a16d6ebcSWolfram Sang 
955*e794dc30SAndy Shevchenko /*
956*e794dc30SAndy Shevchenko  * 10-bit address
957*e794dc30SAndy Shevchenko  *   addr_1: 5'b11110 | addr[9:8] | (R/nW)
958*e794dc30SAndy Shevchenko  *   addr_2: addr[7:0]
959*e794dc30SAndy Shevchenko  */
i2c_10bit_addr_hi_from_msg(const struct i2c_msg * msg)960*e794dc30SAndy Shevchenko static inline u8 i2c_10bit_addr_hi_from_msg(const struct i2c_msg *msg)
961*e794dc30SAndy Shevchenko {
962*e794dc30SAndy Shevchenko 	return 0xf0 | ((msg->addr & GENMASK(9, 8)) >> 7) | (msg->flags & I2C_M_RD);
963*e794dc30SAndy Shevchenko }
964*e794dc30SAndy Shevchenko 
i2c_10bit_addr_lo_from_msg(const struct i2c_msg * msg)965*e794dc30SAndy Shevchenko static inline u8 i2c_10bit_addr_lo_from_msg(const struct i2c_msg *msg)
966*e794dc30SAndy Shevchenko {
967*e794dc30SAndy Shevchenko 	return msg->addr & GENMASK(7, 0);
968*e794dc30SAndy Shevchenko }
969*e794dc30SAndy Shevchenko 
970e94bc5d1SWolfram Sang u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
97182fe39a6SWolfram Sang void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred);
972e94bc5d1SWolfram Sang 
9734d5538f5SBenjamin Tissoires int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
9747c92784aSLars-Peter Clausen /**
975c698d639SPaul Gortmaker  * module_i2c_driver() - Helper macro for registering a modular I2C driver
9767c92784aSLars-Peter Clausen  * @__i2c_driver: i2c_driver struct
9777c92784aSLars-Peter Clausen  *
9787c92784aSLars-Peter Clausen  * Helper macro for I2C drivers which do not do anything special in module
9797c92784aSLars-Peter Clausen  * init/exit. This eliminates a lot of boilerplate. Each module may only
9807c92784aSLars-Peter Clausen  * use this macro once, and calling it replaces module_init() and module_exit()
9817c92784aSLars-Peter Clausen  */
9827c92784aSLars-Peter Clausen #define module_i2c_driver(__i2c_driver) \
9837c92784aSLars-Peter Clausen 	module_driver(__i2c_driver, i2c_add_driver, \
9847c92784aSLars-Peter Clausen 			i2c_del_driver)
9857c92784aSLars-Peter Clausen 
986c698d639SPaul Gortmaker /**
987c698d639SPaul Gortmaker  * builtin_i2c_driver() - Helper macro for registering a builtin I2C driver
988c698d639SPaul Gortmaker  * @__i2c_driver: i2c_driver struct
989c698d639SPaul Gortmaker  *
990c698d639SPaul Gortmaker  * Helper macro for I2C drivers which do not do anything special in their
991c698d639SPaul Gortmaker  * init. This eliminates a lot of boilerplate. Each driver may only
992c698d639SPaul Gortmaker  * use this macro once, and calling it replaces device_initcall().
993c698d639SPaul Gortmaker  */
994c698d639SPaul Gortmaker #define builtin_i2c_driver(__i2c_driver) \
995c698d639SPaul Gortmaker 	builtin_driver(__i2c_driver, i2c_add_driver)
996c698d639SPaul Gortmaker 
997687b81d0SWolfram Sang /* must call put_device() when done with returned i2c_client device */
998373c612dSRussell King (Oracle) struct i2c_client *i2c_find_device_by_fwnode(struct fwnode_handle *fwnode);
999687b81d0SWolfram Sang 
1000687b81d0SWolfram Sang /* must call put_device() when done with returned i2c_adapter device */
1001373c612dSRussell King (Oracle) struct i2c_adapter *i2c_find_adapter_by_fwnode(struct fwnode_handle *fwnode);
1002687b81d0SWolfram Sang 
100348e9743dSVladimir Zapolskiy /* must call i2c_put_adapter() when done with returned i2c_adapter device */
1004373c612dSRussell King (Oracle) struct i2c_adapter *i2c_get_adapter_by_fwnode(struct fwnode_handle *fwnode);
1005373c612dSRussell King (Oracle) 
10061cbf3472SSakari Ailus #else /* I2C */
10071cbf3472SSakari Ailus 
10081cbf3472SSakari Ailus static inline struct i2c_client *
i2c_find_device_by_fwnode(struct fwnode_handle * fwnode)10091cbf3472SSakari Ailus i2c_find_device_by_fwnode(struct fwnode_handle *fwnode)
10101cbf3472SSakari Ailus {
10111cbf3472SSakari Ailus 	return NULL;
10121cbf3472SSakari Ailus }
10131cbf3472SSakari Ailus 
10141cbf3472SSakari Ailus static inline struct i2c_adapter *
i2c_find_adapter_by_fwnode(struct fwnode_handle * fwnode)10151cbf3472SSakari Ailus i2c_find_adapter_by_fwnode(struct fwnode_handle *fwnode)
10161cbf3472SSakari Ailus {
10171cbf3472SSakari Ailus 	return NULL;
10181cbf3472SSakari Ailus }
10191cbf3472SSakari Ailus 
10201cbf3472SSakari Ailus static inline struct i2c_adapter *
i2c_get_adapter_by_fwnode(struct fwnode_handle * fwnode)10211cbf3472SSakari Ailus i2c_get_adapter_by_fwnode(struct fwnode_handle *fwnode)
10221cbf3472SSakari Ailus {
10231cbf3472SSakari Ailus 	return NULL;
10241cbf3472SSakari Ailus }
10251cbf3472SSakari Ailus 
10261cbf3472SSakari Ailus #endif /* !I2C */
10271cbf3472SSakari Ailus 
1028373c612dSRussell King (Oracle) #if IS_ENABLED(CONFIG_OF)
1029373c612dSRussell King (Oracle) /* must call put_device() when done with returned i2c_client device */
of_find_i2c_device_by_node(struct device_node * node)1030373c612dSRussell King (Oracle) static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1031373c612dSRussell King (Oracle) {
1032373c612dSRussell King (Oracle) 	return i2c_find_device_by_fwnode(of_fwnode_handle(node));
1033373c612dSRussell King (Oracle) }
1034373c612dSRussell King (Oracle) 
1035373c612dSRussell King (Oracle) /* must call put_device() when done with returned i2c_adapter device */
of_find_i2c_adapter_by_node(struct device_node * node)1036373c612dSRussell King (Oracle) static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1037373c612dSRussell King (Oracle) {
1038373c612dSRussell King (Oracle) 	return i2c_find_adapter_by_fwnode(of_fwnode_handle(node));
1039373c612dSRussell King (Oracle) }
1040373c612dSRussell King (Oracle) 
1041373c612dSRussell King (Oracle) /* must call i2c_put_adapter() when done with returned i2c_adapter device */
of_get_i2c_adapter_by_node(struct device_node * node)1042373c612dSRussell King (Oracle) static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1043373c612dSRussell King (Oracle) {
1044373c612dSRussell King (Oracle) 	return i2c_get_adapter_by_fwnode(of_fwnode_handle(node));
1045373c612dSRussell King (Oracle) }
1046e1dba01cSWolfram Sang 
1047c807da53SLuca Ceresoli int of_i2c_get_board_info(struct device *dev, struct device_node *node,
1048298d4de1SLee Jones 			  struct i2c_board_info *info);
1049298d4de1SLee Jones 
1050298d4de1SLee Jones #else
1051da0086d0SBoris Brezillon 
of_find_i2c_device_by_node(struct device_node * node)1052da0086d0SBoris Brezillon static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1053da0086d0SBoris Brezillon {
1054687b81d0SWolfram Sang 	return NULL;
1055687b81d0SWolfram Sang }
1056687b81d0SWolfram Sang 
of_find_i2c_adapter_by_node(struct device_node * node)1057687b81d0SWolfram Sang static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1058687b81d0SWolfram Sang {
1059687b81d0SWolfram Sang 	return NULL;
1060687b81d0SWolfram Sang }
1061687b81d0SWolfram Sang 
of_get_i2c_adapter_by_node(struct device_node * node)1062687b81d0SWolfram Sang static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1063687b81d0SWolfram Sang {
1064687b81d0SWolfram Sang 	return NULL;
106548e9743dSVladimir Zapolskiy }
106648e9743dSVladimir Zapolskiy 
of_i2c_get_board_info(struct device * dev,struct device_node * node,struct i2c_board_info * info)106748e9743dSVladimir Zapolskiy static inline int of_i2c_get_board_info(struct device *dev,
106848e9743dSVladimir Zapolskiy 					struct device_node *node,
106948e9743dSVladimir Zapolskiy 					struct i2c_board_info *info)
1070298d4de1SLee Jones {
1071298d4de1SLee Jones 	return -ENOTSUPP;
1072298d4de1SLee Jones }
1073298d4de1SLee Jones 
1074298d4de1SLee Jones #endif /* CONFIG_OF */
1075298d4de1SLee Jones 
1076298d4de1SLee Jones struct acpi_resource;
1077298d4de1SLee Jones struct acpi_resource_i2c_serialbus;
1078da0086d0SBoris Brezillon 
1079da0086d0SBoris Brezillon #if IS_REACHABLE(CONFIG_ACPI) && IS_REACHABLE(CONFIG_I2C)
1080da0086d0SBoris Brezillon bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
1081da0086d0SBoris Brezillon 			       struct acpi_resource_i2c_serialbus **i2c);
1082da0086d0SBoris Brezillon int i2c_acpi_client_count(struct acpi_device *adev);
1083da0086d0SBoris Brezillon u32 i2c_acpi_find_bus_speed(struct device *dev);
1084da0086d0SBoris Brezillon struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode,
1085687b81d0SWolfram Sang 						 int index,
1086687b81d0SWolfram Sang 						 struct i2c_board_info *info);
1087b33a02aaSAndy Shevchenko struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle);
1088b33a02aaSAndy Shevchenko bool i2c_acpi_waive_d0_probe(struct device *dev);
1089b33a02aaSAndy Shevchenko #else
i2c_acpi_get_i2c_resource(struct acpi_resource * ares,struct acpi_resource_i2c_serialbus ** i2c)109071833e79SRichard Fitzgerald static inline bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
1091b33a02aaSAndy Shevchenko 					     struct acpi_resource_i2c_serialbus **i2c)
1092b33a02aaSAndy Shevchenko {
109320a1b3acSHans de Goede 	return false;
10945853b22dSJarkko Nikula }
i2c_acpi_client_count(struct acpi_device * adev)1095c537be0bSHans de Goede static inline int i2c_acpi_client_count(struct acpi_device *adev)
1096c537be0bSHans de Goede {
1097605f8fc2SHans de Goede 	return 0;
10985213d7efSRuslan Babayev }
i2c_acpi_find_bus_speed(struct device * dev)1099b18c1ad6SSakari Ailus static inline u32 i2c_acpi_find_bus_speed(struct device *dev)
11005853b22dSJarkko Nikula {
1101b33a02aaSAndy Shevchenko 	return 0;
1102b33a02aaSAndy Shevchenko }
i2c_acpi_new_device_by_fwnode(struct fwnode_handle * fwnode,int index,struct i2c_board_info * info)1103b33a02aaSAndy Shevchenko static inline struct i2c_client *i2c_acpi_new_device_by_fwnode(
1104b33a02aaSAndy Shevchenko 					struct fwnode_handle *fwnode, int index,
1105b33a02aaSAndy Shevchenko 					struct i2c_board_info *info)
110620a1b3acSHans de Goede {
110720a1b3acSHans de Goede 	return ERR_PTR(-ENODEV);
110820a1b3acSHans de Goede }
i2c_acpi_find_adapter_by_handle(acpi_handle handle)110920a1b3acSHans de Goede static inline struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
11105853b22dSJarkko Nikula {
11115853b22dSJarkko Nikula 	return NULL;
11125853b22dSJarkko Nikula }
i2c_acpi_waive_d0_probe(struct device * dev)11135853b22dSJarkko Nikula static inline bool i2c_acpi_waive_d0_probe(struct device *dev)
1114c537be0bSHans de Goede {
1115c537be0bSHans de Goede 	return false;
1116c537be0bSHans de Goede }
1117605f8fc2SHans de Goede #endif /* CONFIG_ACPI */
11188be23aecSWolfram Sang 
i2c_acpi_new_device(struct device * dev,int index,struct i2c_board_info * info)1119605f8fc2SHans de Goede static inline struct i2c_client *i2c_acpi_new_device(struct device *dev,
11205213d7efSRuslan Babayev 						     int index,
11215213d7efSRuslan Babayev 						     struct i2c_board_info *info)
11225213d7efSRuslan Babayev {
11235213d7efSRuslan Babayev 	return i2c_acpi_new_device_by_fwnode(dev_fwnode(dev), index, info);
1124b18c1ad6SSakari Ailus }
1125b18c1ad6SSakari Ailus 
1126b18c1ad6SSakari Ailus #endif /* _LINUX_I2C_H */
1127b18c1ad6SSakari Ailus