1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2018 Cadence Design Systems Inc.
4  *
5  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6  */
7 
8 #ifndef I3C_MASTER_H
9 #define I3C_MASTER_H
10 
11 #include <asm/bitops.h>
12 #include <hwspinlock.h>
13 #include <i2c.h>
14 #include <linux/bitops.h>
15 #include <linux/i3c/ccc.h>
16 #include <linux/i3c/device.h>
17 
18 #define I3C_HOT_JOIN_ADDR		0x2
19 #define I3C_BROADCAST_ADDR		0x7e
20 #define I3C_MAX_ADDR			GENMASK(6, 0)
21 
22 struct i3c_master_controller;
23 struct i3c_bus;
24 struct i2c_device;
25 struct i3c_device;
26 
27 /**
28  * struct i3c_i2c_dev_desc - Common part of the I3C/I2C device descriptor
29  * @node: node element used to insert the slot into the I2C or I3C device
30  *	  list
31  * @master: I3C master that instantiated this device. Will be used to do
32  *	    I2C/I3C transfers
33  * @master_priv: master private data assigned to the device. Can be used to
34  *		 add master specific information
35  *
36  * This structure is describing common I3C/I2C dev information.
37  */
38 struct i3c_i2c_dev_desc {
39 	struct list_head node;
40 	struct i3c_master_controller *master;
41 	void *master_priv;
42 };
43 
44 #define I3C_LVR_I2C_INDEX_MASK		GENMASK(7, 5)
45 #define I3C_LVR_I2C_INDEX(x)		((x) << 5)
46 #define I3C_LVR_I2C_FM_MODE		BIT(4)
47 
48 #define I2C_MAX_ADDR			GENMASK(6, 0)
49 #define I2C_NAME_SIZE	20
50 #define I2C_MODULE_PREFIX "i2c:"
51 #define I2C_CLIENT_TEN		0x10
52 
53 /**
54  * struct i2c_board_info - template for device creation
55  * @type: chip type, to initialize i2c_client.name
56  * @flags: to initialize i2c_client.flags
57  * @addr: stored in i2c_client.addr
58  * @dev_name: Overrides the default <busnr>-<addr> dev_name if set
59  * @platform_data: stored in i2c_client.dev.platform_data
60  * @of_node: pointer to OpenFirmware device node
61  * @fwnode: device node supplied by the platform firmware
62  * @swnode: software node for the device
63  * @resources: resources associated with the device
64  * @num_resources: number of resources in the @resources array
65  * @irq: stored in i2c_client.irq
66  *
67  * I2C doesn't actually support hardware probing, although controllers and
68  * devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's
69  * a device at a given address.  Drivers commonly need more information than
70  * that, such as chip type, configuration, associated IRQ, and so on.
71  *
72  * i2c_board_info is used to build tables of information listing I2C devices
73  * that are present.  This information is used to grow the driver model tree.
74  * For mainboards this is done statically using i2c_register_board_info();
75  * bus numbers identify adapters that aren't yet available.  For add-on boards,
76  * i2c_new_client_device() does this dynamically with the adapter already known.
77  */
78 struct i2c_board_info {
79 	char		type[I2C_NAME_SIZE];
80 	unsigned short	flags;
81 	unsigned short	addr;
82 	const char	*dev_name;
83 	void		*platform_data;
84 	struct device_node *of_node;
85 };
86 
87 /**
88  * struct i2c_dev_boardinfo - I2C device board information
89  * @node: used to insert the boardinfo object in the I2C boardinfo list
90  * @base: regular I2C board information
91  * @lvr: LVR (Legacy Virtual Register) needed by the I3C core to know about
92  *	 the I2C device limitations
93  *
94  * This structure is used to attach board-level information to an I2C device.
95  * Each I2C device connected on the I3C bus should have one.
96  */
97 struct i2c_dev_boardinfo {
98 	struct list_head node;
99 	struct i2c_board_info base;
100 	u8 lvr;
101 };
102 
103 /**
104  * struct i2c_dev_desc - I2C device descriptor
105  * @common: common part of the I2C device descriptor
106  * @boardinfo: pointer to the boardinfo attached to this I2C device
107  * @dev: I2C device object registered to the I2C framework
108  * @addr: I2C device address
109  * @lvr: LVR (Legacy Virtual Register) needed by the I3C core to know about
110  *	 the I2C device limitations
111  *
112  * Each I2C device connected on the bus will have an i2c_dev_desc.
113  * This object is created by the core and later attached to the controller
114  * using &struct_i3c_master_controller->ops->attach_i2c_dev().
115  *
116  * &struct_i2c_dev_desc is the internal representation of an I2C device
117  * connected on an I3C bus. This object is also passed to all
118  * &struct_i3c_master_controller_ops hooks.
119  */
120 struct i2c_dev_desc {
121 	struct i3c_i2c_dev_desc common;
122 	const struct i2c_dev_boardinfo *boardinfo;
123 	struct i2c_client *dev;
124 	u16 addr;
125 	u8 lvr;
126 };
127 
128 /**
129  * struct i3c_ibi_slot - I3C IBI (In-Band Interrupt) slot
130  * @work: work associated to this slot. The IBI handler will be called from
131  *	  there
132  * @dev: the I3C device that has generated this IBI
133  * @len: length of the payload associated to this IBI
134  * @data: payload buffer
135  *
136  * An IBI slot is an object pre-allocated by the controller and used when an
137  * IBI comes in.
138  * Every time an IBI comes in, the I3C master driver should find a free IBI
139  * slot in its IBI slot pool, retrieve the IBI payload and queue the IBI using
140  * i3c_master_queue_ibi().
141  *
142  * How IBI slots are allocated is left to the I3C master driver, though, for
143  * simple kmalloc-based allocation, the generic IBI slot pool can be used.
144  */
145 struct i3c_ibi_slot {
146 	struct work_struct work;
147 	struct i3c_dev_desc *dev;
148 	unsigned int len;
149 	void *data;
150 };
151 
152 /**
153  * struct i3c_device_ibi_info - IBI information attached to a specific device
154  * @all_ibis_handled: used to be informed when no more IBIs are waiting to be
155  *		      processed. Used by i3c_device_disable_ibi() to wait for
156  *		      all IBIs to be dequeued
157  * @pending_ibis: count the number of pending IBIs. Each pending IBI has its
158  *		  work element queued to the controller workqueue
159  * @max_payload_len: maximum payload length for an IBI coming from this device.
160  *		     this value is specified when calling
161  *		     i3c_device_request_ibi() and should not change at run
162  *		     time. All messages IBIs exceeding this limit should be
163  *		     rejected by the master
164  * @num_slots: number of IBI slots reserved for this device
165  * @enabled: reflect the IBI status
166  * @handler: IBI handler specified at i3c_device_request_ibi() call time. This
167  *	     handler will be called from the controller workqueue, and as such
168  *	     is allowed to sleep (though it is recommended to process the IBI
169  *	     as fast as possible to not stall processing of other IBIs queued
170  *	     on the same workqueue).
171  *	     New I3C messages can be sent from the IBI handler
172  *
173  * The &struct_i3c_device_ibi_info object is allocated when
174  * i3c_device_request_ibi() is called and attached to a specific device. This
175  * object is here to manage IBIs coming from a specific I3C device.
176  *
177  * Note that this structure is the generic view of the IBI management
178  * infrastructure. I3C master drivers may have their own internal
179  * representation which they can associate to the device using
180  * controller-private data.
181  */
182 struct i3c_device_ibi_info {
183 	unsigned int max_payload_len;
184 	unsigned int num_slots;
185 	unsigned int enabled;
186 	void (*handler)(struct i3c_device *dev,
187 			const struct i3c_ibi_payload *payload);
188 };
189 
190 /**
191  * struct i3c_dev_boardinfo - I3C device board information
192  * @node: used to insert the boardinfo object in the I3C boardinfo list
193  * @init_dyn_addr: initial dynamic address requested by the FW. We provide no
194  *		   guarantee that the device will end up using this address,
195  *		   but try our best to assign this specific address to the
196  *		   device
197  * @static_addr: static address the I3C device listen on before it's been
198  *		 assigned a dynamic address by the master. Will be used during
199  *		 bus initialization to assign it a specific dynamic address
200  *		 before starting DAA (Dynamic Address Assignment)
201  * @pid: I3C Provisional ID exposed by the device. This is a unique identifier
202  *	 that may be used to attach boardinfo to i3c_dev_desc when the device
203  *	 does not have a static address
204  * @of_node: optional DT node in case the device has been described in the DT
205  *
206  * This structure is used to attach board-level information to an I3C device.
207  * Not all I3C devices connected on the bus will have a boardinfo. It's only
208  * needed if you want to attach extra resources to a device or assign it a
209  * specific dynamic address.
210  */
211 struct i3c_dev_boardinfo {
212 	struct list_head node;
213 	u8 init_dyn_addr;
214 	u8 static_addr;
215 	u64 pid;
216 	struct device_node *of_node;
217 };
218 
219 /**
220  * struct i3c_dev_desc - I3C device descriptor
221  * @common: common part of the I3C device descriptor
222  * @info: I3C device information. Will be automatically filled when you create
223  *	  your device with i3c_master_add_i3c_dev_locked()
224  * @ibi_lock: lock used to protect the &struct_i3c_device->ibi
225  * @ibi: IBI info attached to a device. Should be NULL until
226  *	 i3c_device_request_ibi() is called
227  * @dev: pointer to the I3C device object exposed to I3C device drivers. This
228  *	 should never be accessed from I3C master controller drivers. Only core
229  *	 code should manipulate it in when updating the dev <-> desc link or
230  *	 when propagating IBI events to the driver
231  * @boardinfo: pointer to the boardinfo attached to this I3C device
232  *
233  * Internal representation of an I3C device. This object is only used by the
234  * core and passed to I3C master controller drivers when they're requested to
235  * do some operations on the device.
236  * The core maintains the link between the internal I3C dev descriptor and the
237  * object exposed to the I3C device drivers (&struct_i3c_device).
238  */
239 struct i3c_dev_desc {
240 	struct i3c_i2c_dev_desc common;
241 	struct i3c_device_info info;
242 	struct mutex ibi_lock; /* lock used to protect the &struct_i3c_device->ibi */
243 	struct i3c_device_ibi_info *ibi;
244 	struct i3c_device *dev;
245 	const struct i3c_dev_boardinfo *boardinfo;
246 };
247 
248 /**
249  * struct i3c_device - I3C device object
250  * @dev: device object to register the I3C dev to the device model
251  * @desc: pointer to an i3c device descriptor object. This link is updated
252  *	  every time the I3C device is rediscovered with a different dynamic
253  *	  address assigned
254  * @bus: I3C bus this device is attached to
255  *
256  * I3C device object exposed to I3C device drivers. The takes care of linking
257  * this object to the relevant &struct_i3c_dev_desc one.
258  * All I3C devs on the I3C bus are represented, including I3C masters. For each
259  * of them, we have an instance of &struct i3c_device.
260  */
261 struct i3c_device {
262 	struct udevice dev;
263 	struct i3c_dev_desc *desc;
264 	struct i3c_bus *bus;
265 };
266 
267 /*
268  * The I3C specification says the maximum number of devices connected on the
269  * bus is 11, but this number depends on external parameters like trace length,
270  * capacitive load per Device, and the types of Devices present on the Bus.
271  * I3C master can also have limitations, so this number is just here as a
272  * reference and should be adjusted on a per-controller/per-board basis.
273  */
274 #define I3C_BUS_MAX_DEVS		11
275 
276 #define I3C_BUS_MAX_I3C_SCL_RATE	12900000
277 #define I3C_BUS_TYP_I3C_SCL_RATE	12500000
278 #define I3C_BUS_I2C_FM_PLUS_SCL_RATE	1000000
279 #define I3C_BUS_I2C_FM_SCL_RATE		400000
280 #define I3C_BUS_TLOW_OD_MIN_NS		200
281 #define I3C_BUS_THIGH_INIT_OD_MIN_NS	200
282 
283 /**
284  * enum i3c_bus_mode - I3C bus mode
285  * @I3C_BUS_MODE_PURE: only I3C devices are connected to the bus. No limitation
286  *		       expected
287  * @I3C_BUS_MODE_MIXED_FAST: I2C devices with 50ns spike filter are present on
288  *			     the bus. The only impact in this mode is that the
289  *			     high SCL pulse has to stay below 50ns to trick I2C
290  *			     devices when transmitting I3C frames
291  * @I3C_BUS_MODE_MIXED_LIMITED: I2C devices without 50ns spike filter are
292  *				present on the bus. However they allow
293  *				compliance up to the maximum SDR SCL clock
294  *				frequency.
295  * @I3C_BUS_MODE_MIXED_SLOW: I2C devices without 50ns spike filter are present
296  *			     on the bus
297  */
298 enum i3c_bus_mode {
299 	I3C_BUS_MODE_PURE,
300 	I3C_BUS_MODE_MIXED_FAST,
301 	I3C_BUS_MODE_MIXED_LIMITED,
302 	I3C_BUS_MODE_MIXED_SLOW,
303 };
304 
305 /**
306  * enum i3c_addr_slot_status - I3C address slot status
307  * @I3C_ADDR_SLOT_FREE: address is free
308  * @I3C_ADDR_SLOT_RSVD: address is reserved
309  * @I3C_ADDR_SLOT_I2C_DEV: address is assigned to an I2C device
310  * @I3C_ADDR_SLOT_I3C_DEV: address is assigned to an I3C device
311  * @I3C_ADDR_SLOT_STATUS_MASK: address slot mask
312  *
313  * On an I3C bus, addresses are assigned dynamically, and we need to know which
314  * addresses are free to use and which ones are already assigned.
315  *
316  * Addresses marked as reserved are those reserved by the I3C protocol
317  * (broadcast address, ...).
318  */
319 enum i3c_addr_slot_status {
320 	I3C_ADDR_SLOT_FREE,
321 	I3C_ADDR_SLOT_RSVD,
322 	I3C_ADDR_SLOT_I2C_DEV,
323 	I3C_ADDR_SLOT_I3C_DEV,
324 	I3C_ADDR_SLOT_STATUS_MASK = 3,
325 };
326 
327 /*
328  * i2c_adapter is the structure used to identify a physical i2c bus along
329  * with the access algorithms necessary to access it.
330  */
331 struct i2c_adapter {
332 	struct module *owner;
333 	unsigned int class;
334 	int timeout;			/* in jiffies */
335 	int retries;
336 	struct udevice dev;
337 	int nr;
338 	char name[48];
339 };
340 
341 /**
342  * struct i3c_bus - I3C bus object
343  * @cur_master: I3C master currently driving the bus. Since I3C is multi-master
344  *		this can change over the time. Will be used to let a master
345  *		know whether it needs to request bus ownership before sending
346  *		a frame or not
347  * @id: bus ID. Assigned by the framework when register the bus
348  * @addrslots: a bitmap with 2-bits per-slot to encode the address status and
349  *	       ease the DAA (Dynamic Address Assignment) procedure (see
350  *	       &enum i3c_addr_slot_status)
351  * @mode: bus mode (see &enum i3c_bus_mode)
352  * @scl_rate.i3c: maximum rate for the clock signal when doing I3C SDR/priv
353  *		  transfers
354  * @scl_rate.i2c: maximum rate for the clock signal when doing I2C transfers
355  * @scl_rate: SCL signal rate for I3C and I2C mode
356  * @devs.i3c: contains a list of I3C device descriptors representing I3C
357  *	      devices connected on the bus and successfully attached to the
358  *	      I3C master
359  * @devs.i2c: contains a list of I2C device descriptors representing I2C
360  *	      devices connected on the bus and successfully attached to the
361  *	      I3C master
362  * @devs: 2 lists containing all I3C/I2C devices connected to the bus
363  * @lock: read/write lock on the bus. This is needed to protect against
364  *	  operations that have an impact on the whole bus and the devices
365  *	  connected to it. For example, when asking slaves to drop their
366  *	  dynamic address (RSTDAA CCC), we need to make sure no one is trying
367  *	  to send I3C frames to these devices.
368  *	  Note that this lock does not protect against concurrency between
369  *	  devices: several drivers can send different I3C/I2C frames through
370  *	  the same master in parallel. This is the responsibility of the
371  *	  master to guarantee that frames are actually sent sequentially and
372  *	  not interlaced
373  *
374  * The I3C bus is represented with its own object and not implicitly described
375  * by the I3C master to cope with the multi-master functionality, where one bus
376  * can be shared amongst several masters, each of them requesting bus ownership
377  * when they need to.
378  */
379 struct i3c_bus {
380 	struct i3c_dev_desc *cur_master;
381 	int id;
382 	unsigned long addrslots[((I2C_MAX_ADDR + 1) * 2) / BITS_PER_LONG];
383 	enum i3c_bus_mode mode;
384 	struct {
385 		unsigned long i3c;
386 		unsigned long i2c;
387 	} scl_rate;
388 	struct {
389 		struct list_head i3c;
390 		struct list_head i2c;
391 	} devs;
392 	struct rw_semaphore lock;
393 };
394 
395 /**
396  * struct i3c_master_controller_ops - I3C master methods
397  * @bus_init: hook responsible for the I3C bus initialization. You should at
398  *	      least call master_set_info() from there and set the bus mode.
399  *	      You can also put controller specific initialization in there.
400  *	      This method is mandatory.
401  * @bus_cleanup: cleanup everything done in
402  *		 &i3c_master_controller_ops->bus_init().
403  *		 This method is optional.
404  * @attach_i3c_dev: called every time an I3C device is attached to the bus. It
405  *		    can be after a DAA or when a device is statically declared
406  *		    by the FW, in which case it will only have a static address
407  *		    and the dynamic address will be 0.
408  *		    When this function is called, device information have not
409  *		    been retrieved yet.
410  *		    This is a good place to attach master controller specific
411  *		    data to I3C devices.
412  *		    This method is optional.
413  * @reattach_i3c_dev: called every time an I3C device has its addressed
414  *		      changed. It can be because the device has been powered
415  *		      down and has lost its address, or it can happen when a
416  *		      device had a static address and has been assigned a
417  *		      dynamic address with SETDASA.
418  *		      This method is optional.
419  * @detach_i3c_dev: called when an I3C device is detached from the bus. Usually
420  *		    happens when the master device is unregistered.
421  *		    This method is optional.
422  * @do_daa: do a DAA (Dynamic Address Assignment) procedure. This is procedure
423  *	    should send an ENTDAA CCC command and then add all devices
424  *	    discovered sure the DAA using i3c_master_add_i3c_dev_locked().
425  *	    Add devices added with i3c_master_add_i3c_dev_locked() will then be
426  *	    attached or re-attached to the controller.
427  *	    This method is mandatory.
428  * @supports_ccc_cmd: should return true if the CCC command is supported, false
429  *		      otherwise.
430  *		      This method is optional, if not provided the core assumes
431  *		      all CCC commands are supported.
432  * @send_ccc_cmd: send a CCC command
433  *		  This method is mandatory.
434  * @priv_xfers: do one or several private I3C SDR transfers
435  *		This method is mandatory.
436  * @attach_i2c_dev: called every time an I2C device is attached to the bus.
437  *		    This is a good place to attach master controller specific
438  *		    data to I2C devices.
439  *		    This method is optional.
440  * @detach_i2c_dev: called when an I2C device is detached from the bus. Usually
441  *		    happens when the master device is unregistered.
442  *		    This method is optional.
443  * @i2c_xfers: do one or several I2C transfers. Note that, unlike i3c
444  *	       transfers, the core does not guarantee that buffers attached to
445  *	       the transfers are DMA-safe. If drivers want to have DMA-safe
446  *	       buffers, they should use the i2c_get_dma_safe_msg_buf()
447  *	       and i2c_put_dma_safe_msg_buf() helpers provided by the I2C
448  *	       framework.
449  *	       This method is mandatory.
450  * @request_ibi: attach an IBI handler to an I3C device. This implies defining
451  *		 an IBI handler and the constraints of the IBI (maximum payload
452  *		 length and number of pre-allocated slots).
453  *		 Some controllers support less IBI-capable devices than regular
454  *		 devices, so this method might return -%EBUSY if there's no
455  *		 more space for an extra IBI registration
456  *		 This method is optional.
457  * @free_ibi: free an IBI previously requested with ->request_ibi(). The IBI
458  *	      should have been disabled with ->disable_irq() prior to that
459  *	      This method is mandatory only if ->request_ibi is not NULL.
460  * @enable_ibi: enable the IBI. Only valid if ->request_ibi() has been called
461  *		prior to ->enable_ibi(). The controller should first enable
462  *		the IBI on the controller end (for example, unmask the hardware
463  *		IRQ) and then send the ENEC CCC command (with the IBI flag set)
464  *		to the I3C device.
465  *		This method is mandatory only if ->request_ibi is not NULL.
466  * @disable_ibi: disable an IBI. First send the DISEC CCC command with the IBI
467  *		 flag set and then deactivate the hardware IRQ on the
468  *		 controller end.
469  *		 This method is mandatory only if ->request_ibi is not NULL.
470  * @recycle_ibi_slot: recycle an IBI slot. Called every time an IBI has been
471  *		      processed by its handler. The IBI slot should be put back
472  *		      in the IBI slot pool so that the controller can re-use it
473  *		      for a future IBI
474  *		      This method is mandatory only if ->request_ibi is not
475  *		      NULL.
476  */
477 struct i3c_master_controller_ops {
478 	int (*bus_init)(struct i3c_master_controller *master);
479 	void (*bus_cleanup)(struct i3c_master_controller *master);
480 	int (*attach_i3c_dev)(struct i3c_dev_desc *dev);
481 	int (*reattach_i3c_dev)(struct i3c_dev_desc *dev, u8 old_dyn_addr);
482 	void (*detach_i3c_dev)(struct i3c_dev_desc *dev);
483 	int (*do_daa)(struct i3c_master_controller *master);
484 	bool (*supports_ccc_cmd)(struct i3c_master_controller *master,
485 				 const struct i3c_ccc_cmd *cmd);
486 	int (*send_ccc_cmd)(struct i3c_master_controller *master,
487 			    struct i3c_ccc_cmd *cmd);
488 	int (*priv_xfers)(struct i3c_dev_desc *dev,
489 			  struct i3c_priv_xfer *xfers,
490 			  u32 nxfers);
491 	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
492 	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
493 	int (*i2c_xfers)(struct i2c_dev_desc *dev,
494 			 const struct i2c_msg *xfers, int nxfers);
495 	int (*request_ibi)(struct i3c_dev_desc *dev,
496 			   const struct i3c_ibi_setup *req);
497 	void (*free_ibi)(struct i3c_dev_desc *dev);
498 	int (*enable_ibi)(struct i3c_dev_desc *dev);
499 	int (*disable_ibi)(struct i3c_dev_desc *dev);
500 	void (*recycle_ibi_slot)(struct i3c_dev_desc *dev,
501 				 struct i3c_ibi_slot *slot);
502 };
503 
504 /**
505  * struct i3c_master_controller - I3C master controller object
506  * @dev: device to be registered to the device-model
507  * @this: an I3C device object representing this master. This device will be
508  *	  added to the list of I3C devs available on the bus
509  * @i2c: I2C adapter used for backward compatibility. This adapter is
510  *	 registered to the I2C subsystem to be as transparent as possible to
511  *	 existing I2C drivers
512  * @ops: master operations. See &struct i3c_master_controller_ops
513  * @secondary: true if the master is a secondary master
514  * @init_done: true when the bus initialization is done
515  * @boardinfo.i3c: list of I3C  boardinfo objects
516  * @boardinfo.i2c: list of I2C boardinfo objects
517  * @boardinfo: board-level information attached to devices connected on the bus
518  * @bus: I3C bus exposed by this master
519  * @wq: workqueue used to execute IBI handlers. Can also be used by master
520  *	drivers if they need to postpone operations that need to take place
521  *	in a thread context. Typical examples are Hot Join processing which
522  *	requires taking the bus lock in maintenance, which in turn, can only
523  *	be done from a sleep-able context
524  *
525  * A &struct i3c_master_controller has to be registered to the I3C subsystem
526  * through i3c_master_register(). None of &struct i3c_master_controller fields
527  * should be set manually, just pass appropriate values to
528  * i3c_master_register().
529  */
530 struct i3c_master_controller {
531 	struct udevice *dev;
532 	struct i3c_dev_desc *this;
533 	struct i2c_adapter i2c;
534 	const struct i3c_master_controller_ops *ops;
535 	unsigned int secondary : 1;
536 	unsigned int init_done : 1;
537 	struct {
538 		struct list_head i3c;
539 		struct list_head i2c;
540 	} boardinfo;
541 	struct i3c_bus bus;
542 	struct workqueue_struct *wq;
543 };
544 
545 /**
546  * i3c_bus_for_each_i2cdev() - iterate over all I2C devices present on the bus
547  * @bus: the I3C bus
548  * @dev: an I2C device descriptor pointer updated to point to the current slot
549  *	 at each iteration of the loop
550  *
551  * Iterate over all I2C devs present on the bus.
552  */
553 #define i3c_bus_for_each_i2cdev(bus, dev)				\
554 	list_for_each_entry(dev, &(bus)->devs.i2c, common.node)
555 
556 /**
557  * i3c_bus_for_each_i3cdev() - iterate over all I3C devices present on the bus
558  * @bus: the I3C bus
559  * @dev: and I3C device descriptor pointer updated to point to the current slot
560  *	 at each iteration of the loop
561  *
562  * Iterate over all I3C devs present on the bus.
563  */
564 #define i3c_bus_for_each_i3cdev(bus, dev)				\
565 	list_for_each_entry(dev, &(bus)->devs.i3c, common.node)
566 
567 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
568 			    u8 evts);
569 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
570 			   u8 evts);
571 int i3c_master_entdaa_locked(struct i3c_master_controller *master);
572 int i3c_master_defslvs_locked(struct i3c_master_controller *master);
573 
574 int i3c_master_get_free_addr(struct i3c_master_controller *master,
575 			     u8 start_addr);
576 
577 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
578 				  u8 addr);
579 int i3c_master_do_daa(struct i3c_master_controller *master);
580 
581 int i3c_master_set_info(struct i3c_master_controller *master,
582 			const struct i3c_device_info *info);
583 
584 int i3c_master_register(struct i3c_master_controller *master,
585 			struct udevice *parent,
586 			const struct i3c_master_controller_ops *ops,
587 			bool secondary);
588 int i3c_master_unregister(struct i3c_master_controller *master);
589 
590 /**
591  * i3c_dev_get_master_data() - get master private data attached to an I3C
592  *			       device descriptor
593  * @dev: the I3C device descriptor to get private data from
594  *
595  * Return: the private data previously attached with i3c_dev_set_master_data()
596  *	   or NULL if no data has been attached to the device.
597  */
i3c_dev_get_master_data(const struct i3c_dev_desc * dev)598 static inline void *i3c_dev_get_master_data(const struct i3c_dev_desc *dev)
599 {
600 	return dev->common.master_priv;
601 }
602 
603 /**
604  * i3c_dev_set_master_data() - attach master private data to an I3C device
605  *			       descriptor
606  * @dev: the I3C device descriptor to attach private data to
607  * @data: private data
608  *
609  * This functions allows a master controller to attach per-device private data
610  * which can then be retrieved with i3c_dev_get_master_data().
611  */
i3c_dev_set_master_data(struct i3c_dev_desc * dev,void * data)612 static inline void i3c_dev_set_master_data(struct i3c_dev_desc *dev,
613 					   void *data)
614 {
615 	dev->common.master_priv = data;
616 }
617 
618 /**
619  * i2c_dev_get_master_data() - get master private data attached to an I2C
620  *			       device descriptor
621  * @dev: the I2C device descriptor to get private data from
622  *
623  * Return: the private data previously attached with i2c_dev_set_master_data()
624  *	   or NULL if no data has been attached to the device.
625  */
i2c_dev_get_master_data(const struct i2c_dev_desc * dev)626 static inline void *i2c_dev_get_master_data(const struct i2c_dev_desc *dev)
627 {
628 	return dev->common.master_priv;
629 }
630 
631 /**
632  * i2c_dev_set_master_data() - attach master private data to an I2C device
633  *			       descriptor
634  * @dev: the I2C device descriptor to attach private data to
635  * @data: private data
636  *
637  * This functions allows a master controller to attach per-device private data
638  * which can then be retrieved with i2c_device_get_master_data().
639  */
i2c_dev_set_master_data(struct i2c_dev_desc * dev,void * data)640 static inline void i2c_dev_set_master_data(struct i2c_dev_desc *dev,
641 					   void *data)
642 {
643 	dev->common.master_priv = data;
644 }
645 
646 /**
647  * i3c_dev_get_master() - get master used to communicate with a device
648  * @dev: I3C dev
649  *
650  * Return: the master controller driving @dev
651  */
652 static inline struct i3c_master_controller *
i3c_dev_get_master(struct i3c_dev_desc * dev)653 i3c_dev_get_master(struct i3c_dev_desc *dev)
654 {
655 	return dev->common.master;
656 }
657 
658 /**
659  * i2c_dev_get_master() - get master used to communicate with a device
660  * @dev: I2C dev
661  *
662  * Return: the master controller driving @dev
663  */
664 static inline struct i3c_master_controller *
i2c_dev_get_master(struct i2c_dev_desc * dev)665 i2c_dev_get_master(struct i2c_dev_desc *dev)
666 {
667 	return dev->common.master;
668 }
669 
670 /**
671  * i3c_master_get_bus() - get the bus attached to a master
672  * @master: master object
673  *
674  * Return: the I3C bus @master is connected to
675  */
676 static inline struct i3c_bus *
i3c_master_get_bus(struct i3c_master_controller * master)677 i3c_master_get_bus(struct i3c_master_controller *master)
678 {
679 	return &master->bus;
680 }
681 
682 struct i3c_generic_ibi_pool;
683 
684 struct i3c_generic_ibi_pool *
685 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
686 			   const struct i3c_ibi_setup *req);
687 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool);
688 
689 struct i3c_ibi_slot *
690 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool);
691 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
692 				  struct i3c_ibi_slot *slot);
693 
694 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot);
695 
696 struct i3c_ibi_slot *i3c_master_get_free_ibi_slot(struct i3c_dev_desc *dev);
697 
698 #endif /* I3C_MASTER_H */
699