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