xref: /linux/drivers/i3c/master.c (revision acfcdff920dcab2b71536f04684e92c7933f7f40)
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 #include <linux/atomic.h>
9 #include <linux/bug.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/of.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 
22 #include "internals.h"
23 
24 static DEFINE_IDR(i3c_bus_idr);
25 static DEFINE_MUTEX(i3c_core_lock);
26 static int __i3c_first_dynamic_bus_num;
27 static BLOCKING_NOTIFIER_HEAD(i3c_bus_notifier);
28 
29 /**
30  * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
31  * @bus: I3C bus to take the lock on
32  *
33  * This function takes the bus lock so that no other operations can occur on
34  * the bus. This is needed for all kind of bus maintenance operation, like
35  * - enabling/disabling slave events
36  * - re-triggering DAA
37  * - changing the dynamic address of a device
38  * - relinquishing mastership
39  * - ...
40  *
41  * The reason for this kind of locking is that we don't want drivers and core
42  * logic to rely on I3C device information that could be changed behind their
43  * back.
44  */
45 static void i3c_bus_maintenance_lock(struct i3c_bus *bus)
46 {
47 	down_write(&bus->lock);
48 }
49 
50 /**
51  * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
52  *			      operation
53  * @bus: I3C bus to release the lock on
54  *
55  * Should be called when the bus maintenance operation is done. See
56  * i3c_bus_maintenance_lock() for more details on what these maintenance
57  * operations are.
58  */
59 static void i3c_bus_maintenance_unlock(struct i3c_bus *bus)
60 {
61 	up_write(&bus->lock);
62 }
63 
64 /**
65  * i3c_bus_normaluse_lock - Lock the bus for a normal operation
66  * @bus: I3C bus to take the lock on
67  *
68  * This function takes the bus lock for any operation that is not a maintenance
69  * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
70  * maintenance operations). Basically all communications with I3C devices are
71  * normal operations (HDR, SDR transfers or CCC commands that do not change bus
72  * state or I3C dynamic address).
73  *
74  * Note that this lock is not guaranteeing serialization of normal operations.
75  * In other words, transfer requests passed to the I3C master can be submitted
76  * in parallel and I3C master drivers have to use their own locking to make
77  * sure two different communications are not inter-mixed, or access to the
78  * output/input queue is not done while the engine is busy.
79  */
80 void i3c_bus_normaluse_lock(struct i3c_bus *bus)
81 {
82 	down_read(&bus->lock);
83 }
84 
85 /**
86  * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
87  * @bus: I3C bus to release the lock on
88  *
89  * Should be called when a normal operation is done. See
90  * i3c_bus_normaluse_lock() for more details on what these normal operations
91  * are.
92  */
93 void i3c_bus_normaluse_unlock(struct i3c_bus *bus)
94 {
95 	up_read(&bus->lock);
96 }
97 
98 static struct i3c_master_controller *
99 i3c_bus_to_i3c_master(struct i3c_bus *i3cbus)
100 {
101 	return container_of(i3cbus, struct i3c_master_controller, bus);
102 }
103 
104 static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev)
105 {
106 	return container_of(dev, struct i3c_master_controller, dev);
107 }
108 
109 static int __must_check i3c_master_rpm_get(struct i3c_master_controller *master)
110 {
111 	int ret = master->rpm_allowed ? pm_runtime_resume_and_get(master->dev.parent) : 0;
112 
113 	if (ret < 0) {
114 		dev_err(master->dev.parent, "runtime resume failed, error %d\n", ret);
115 		return ret;
116 	}
117 	return 0;
118 }
119 
120 static void i3c_master_rpm_put(struct i3c_master_controller *master)
121 {
122 	if (master->rpm_allowed)
123 		pm_runtime_put_autosuspend(master->dev.parent);
124 }
125 
126 int i3c_bus_rpm_get(struct i3c_bus *bus)
127 {
128 	return i3c_master_rpm_get(i3c_bus_to_i3c_master(bus));
129 }
130 
131 void i3c_bus_rpm_put(struct i3c_bus *bus)
132 {
133 	i3c_master_rpm_put(i3c_bus_to_i3c_master(bus));
134 }
135 
136 bool i3c_bus_rpm_ibi_allowed(struct i3c_bus *bus)
137 {
138 	return i3c_bus_to_i3c_master(bus)->rpm_ibi_allowed;
139 }
140 
141 static const struct device_type i3c_device_type;
142 
143 static struct i3c_bus *dev_to_i3cbus(struct device *dev)
144 {
145 	struct i3c_master_controller *master;
146 
147 	if (dev->type == &i3c_device_type)
148 		return dev_to_i3cdev(dev)->bus;
149 
150 	master = dev_to_i3cmaster(dev);
151 
152 	return &master->bus;
153 }
154 
155 static struct i3c_dev_desc *dev_to_i3cdesc(struct device *dev)
156 {
157 	struct i3c_master_controller *master;
158 
159 	if (dev->type == &i3c_device_type)
160 		return dev_to_i3cdev(dev)->desc;
161 
162 	master = dev_to_i3cmaster(dev);
163 
164 	return master->this;
165 }
166 
167 static ssize_t bcr_show(struct device *dev,
168 			struct device_attribute *da,
169 			char *buf)
170 {
171 	struct i3c_bus *bus = dev_to_i3cbus(dev);
172 	struct i3c_dev_desc *desc;
173 	ssize_t ret;
174 
175 	i3c_bus_normaluse_lock(bus);
176 	desc = dev_to_i3cdesc(dev);
177 	ret = sprintf(buf, "0x%02x\n", desc->info.bcr);
178 	i3c_bus_normaluse_unlock(bus);
179 
180 	return ret;
181 }
182 static DEVICE_ATTR_RO(bcr);
183 
184 static ssize_t dcr_show(struct device *dev,
185 			struct device_attribute *da,
186 			char *buf)
187 {
188 	struct i3c_bus *bus = dev_to_i3cbus(dev);
189 	struct i3c_dev_desc *desc;
190 	ssize_t ret;
191 
192 	i3c_bus_normaluse_lock(bus);
193 	desc = dev_to_i3cdesc(dev);
194 	ret = sprintf(buf, "0x%02x\n", desc->info.dcr);
195 	i3c_bus_normaluse_unlock(bus);
196 
197 	return ret;
198 }
199 static DEVICE_ATTR_RO(dcr);
200 
201 static ssize_t pid_show(struct device *dev,
202 			struct device_attribute *da,
203 			char *buf)
204 {
205 	struct i3c_bus *bus = dev_to_i3cbus(dev);
206 	struct i3c_dev_desc *desc;
207 	ssize_t ret;
208 
209 	i3c_bus_normaluse_lock(bus);
210 	desc = dev_to_i3cdesc(dev);
211 	ret = sprintf(buf, "%llx\n", desc->info.pid);
212 	i3c_bus_normaluse_unlock(bus);
213 
214 	return ret;
215 }
216 static DEVICE_ATTR_RO(pid);
217 
218 static ssize_t dynamic_address_show(struct device *dev,
219 				    struct device_attribute *da,
220 				    char *buf)
221 {
222 	struct i3c_bus *bus = dev_to_i3cbus(dev);
223 	struct i3c_dev_desc *desc;
224 	ssize_t ret;
225 
226 	i3c_bus_normaluse_lock(bus);
227 	desc = dev_to_i3cdesc(dev);
228 	ret = sprintf(buf, "%02x\n", desc->info.dyn_addr);
229 	i3c_bus_normaluse_unlock(bus);
230 
231 	return ret;
232 }
233 static DEVICE_ATTR_RO(dynamic_address);
234 
235 static const char * const hdrcap_strings[] = {
236 	"hdr-ddr", "hdr-tsp", "hdr-tsl",
237 };
238 
239 static ssize_t hdrcap_show(struct device *dev,
240 			   struct device_attribute *da,
241 			   char *buf)
242 {
243 	struct i3c_bus *bus = dev_to_i3cbus(dev);
244 	struct i3c_dev_desc *desc;
245 	ssize_t offset = 0, ret;
246 	unsigned long caps;
247 	int mode;
248 
249 	i3c_bus_normaluse_lock(bus);
250 	desc = dev_to_i3cdesc(dev);
251 	caps = desc->info.hdr_cap;
252 	for_each_set_bit(mode, &caps, 8) {
253 		if (mode >= ARRAY_SIZE(hdrcap_strings))
254 			break;
255 
256 		if (!hdrcap_strings[mode])
257 			continue;
258 
259 		ret = sprintf(buf + offset, offset ? " %s" : "%s",
260 			      hdrcap_strings[mode]);
261 		if (ret < 0)
262 			goto out;
263 
264 		offset += ret;
265 	}
266 
267 	ret = sprintf(buf + offset, "\n");
268 	if (ret < 0)
269 		goto out;
270 
271 	ret = offset + ret;
272 
273 out:
274 	i3c_bus_normaluse_unlock(bus);
275 
276 	return ret;
277 }
278 static DEVICE_ATTR_RO(hdrcap);
279 
280 static ssize_t modalias_show(struct device *dev,
281 			     struct device_attribute *da, char *buf)
282 {
283 	struct i3c_device *i3c = dev_to_i3cdev(dev);
284 	struct i3c_device_info devinfo;
285 	u16 manuf, part, ext;
286 
287 	i3c_device_get_info(i3c, &devinfo);
288 	manuf = I3C_PID_MANUF_ID(devinfo.pid);
289 	part = I3C_PID_PART_ID(devinfo.pid);
290 	ext = I3C_PID_EXTRA_INFO(devinfo.pid);
291 
292 	if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
293 		return sprintf(buf, "i3c:dcr%02Xmanuf%04X", devinfo.dcr,
294 			       manuf);
295 
296 	return sprintf(buf, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
297 		       devinfo.dcr, manuf, part, ext);
298 }
299 static DEVICE_ATTR_RO(modalias);
300 
301 static struct attribute *i3c_device_attrs[] = {
302 	&dev_attr_bcr.attr,
303 	&dev_attr_dcr.attr,
304 	&dev_attr_pid.attr,
305 	&dev_attr_dynamic_address.attr,
306 	&dev_attr_hdrcap.attr,
307 	&dev_attr_modalias.attr,
308 	NULL,
309 };
310 ATTRIBUTE_GROUPS(i3c_device);
311 
312 static int i3c_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
313 {
314 	const struct i3c_device *i3cdev = dev_to_i3cdev(dev);
315 	struct i3c_device_info devinfo;
316 	u16 manuf, part, ext;
317 
318 	if (i3cdev->desc)
319 		devinfo = i3cdev->desc->info;
320 	manuf = I3C_PID_MANUF_ID(devinfo.pid);
321 	part = I3C_PID_PART_ID(devinfo.pid);
322 	ext = I3C_PID_EXTRA_INFO(devinfo.pid);
323 
324 	if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
325 		return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X",
326 				      devinfo.dcr, manuf);
327 
328 	return add_uevent_var(env,
329 			      "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
330 			      devinfo.dcr, manuf, part, ext);
331 }
332 
333 static const struct device_type i3c_device_type = {
334 	.groups	= i3c_device_groups,
335 	.uevent = i3c_device_uevent,
336 };
337 
338 static int i3c_device_match(struct device *dev, const struct device_driver *drv)
339 {
340 	struct i3c_device *i3cdev;
341 	const struct i3c_driver *i3cdrv;
342 
343 	if (dev->type != &i3c_device_type)
344 		return 0;
345 
346 	i3cdev = dev_to_i3cdev(dev);
347 	i3cdrv = drv_to_i3cdrv(drv);
348 	if (i3c_device_match_id(i3cdev, i3cdrv->id_table))
349 		return 1;
350 
351 	return 0;
352 }
353 
354 static int i3c_device_probe(struct device *dev)
355 {
356 	struct i3c_device *i3cdev = dev_to_i3cdev(dev);
357 	struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
358 
359 	return driver->probe(i3cdev);
360 }
361 
362 static void i3c_device_remove(struct device *dev)
363 {
364 	struct i3c_device *i3cdev = dev_to_i3cdev(dev);
365 	struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
366 
367 	if (driver->remove)
368 		driver->remove(i3cdev);
369 }
370 
371 const struct bus_type i3c_bus_type = {
372 	.name = "i3c",
373 	.match = i3c_device_match,
374 	.probe = i3c_device_probe,
375 	.remove = i3c_device_remove,
376 };
377 EXPORT_SYMBOL_GPL(i3c_bus_type);
378 
379 static enum i3c_addr_slot_status
380 i3c_bus_get_addr_slot_status_mask(struct i3c_bus *bus, u16 addr, u32 mask)
381 {
382 	unsigned long status;
383 	int bitpos = addr * I3C_ADDR_SLOT_STATUS_BITS;
384 
385 	if (addr > I2C_MAX_ADDR)
386 		return I3C_ADDR_SLOT_RSVD;
387 
388 	status = bus->addrslots[bitpos / BITS_PER_LONG];
389 	status >>= bitpos % BITS_PER_LONG;
390 
391 	return status & mask;
392 }
393 
394 static enum i3c_addr_slot_status
395 i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr)
396 {
397 	return i3c_bus_get_addr_slot_status_mask(bus, addr, I3C_ADDR_SLOT_STATUS_MASK);
398 }
399 
400 static void i3c_bus_set_addr_slot_status_mask(struct i3c_bus *bus, u16 addr,
401 					      enum i3c_addr_slot_status status, u32 mask)
402 {
403 	int bitpos = addr * I3C_ADDR_SLOT_STATUS_BITS;
404 	unsigned long *ptr;
405 
406 	if (addr > I2C_MAX_ADDR)
407 		return;
408 
409 	ptr = bus->addrslots + (bitpos / BITS_PER_LONG);
410 	*ptr &= ~((unsigned long)mask << (bitpos % BITS_PER_LONG));
411 	*ptr |= ((unsigned long)status & mask) << (bitpos % BITS_PER_LONG);
412 }
413 
414 static void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr,
415 					 enum i3c_addr_slot_status status)
416 {
417 	i3c_bus_set_addr_slot_status_mask(bus, addr, status, I3C_ADDR_SLOT_STATUS_MASK);
418 }
419 
420 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr)
421 {
422 	enum i3c_addr_slot_status status;
423 
424 	status = i3c_bus_get_addr_slot_status(bus, addr);
425 
426 	return status == I3C_ADDR_SLOT_FREE;
427 }
428 
429 /*
430  * ┌────┬─────────────┬───┬─────────┬───┐
431  * │S/Sr│ 7'h7E RnW=0 │ACK│ ENTDAA  │ T ├────┐
432  * └────┴─────────────┴───┴─────────┴───┘    │
433  * ┌─────────────────────────────────────────┘
434  * │  ┌──┬─────────────┬───┬─────────────────┬────────────────┬───┬─────────┐
435  * └─►│Sr│7'h7E RnW=1  │ACK│48bit UID BCR DCR│Assign 7bit Addr│PAR│ ACK/NACK436  *    └──┴─────────────┴───┴─────────────────┴────────────────┴───┴─────────┘
437  * Some master controllers (such as HCI) need to prepare the entire above transaction before
438  * sending it out to the I3C bus. This means that a 7-bit dynamic address needs to be allocated
439  * before knowing the target device's UID information.
440  *
441  * However, some I3C targets may request specific addresses (called as "init_dyn_addr"), which is
442  * typically specified by the DT-'s assigned-address property. Lower addresses having higher IBI
443  * priority. If it is available, i3c_bus_get_free_addr() preferably return a free address that is
444  * not in the list of desired addresses (called as "init_dyn_addr"). This allows the device with
445  * the "init_dyn_addr" to switch to its "init_dyn_addr" when it hot-joins the I3C bus. Otherwise,
446  * if the "init_dyn_addr" is already in use by another I3C device, the target device will not be
447  * able to switch to its desired address.
448  *
449  * If the previous step fails, fallback returning one of the remaining unassigned address,
450  * regardless of its state in the desired list.
451  */
452 static int i3c_bus_get_free_addr(struct i3c_bus *bus, u8 start_addr)
453 {
454 	enum i3c_addr_slot_status status;
455 	u8 addr;
456 
457 	for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) {
458 		status = i3c_bus_get_addr_slot_status_mask(bus, addr,
459 							   I3C_ADDR_SLOT_EXT_STATUS_MASK);
460 		if (status == I3C_ADDR_SLOT_FREE)
461 			return addr;
462 	}
463 
464 	for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) {
465 		status = i3c_bus_get_addr_slot_status_mask(bus, addr,
466 							   I3C_ADDR_SLOT_STATUS_MASK);
467 		if (status == I3C_ADDR_SLOT_FREE)
468 			return addr;
469 	}
470 
471 	return -ENOMEM;
472 }
473 
474 static void i3c_bus_init_addrslots(struct i3c_bus *bus)
475 {
476 	int i;
477 
478 	/* Addresses 0 to 7 are reserved. */
479 	for (i = 0; i < 8; i++)
480 		i3c_bus_set_addr_slot_status(bus, i, I3C_ADDR_SLOT_RSVD);
481 
482 	/*
483 	 * Reserve broadcast address and all addresses that might collide
484 	 * with the broadcast address when facing a single bit error.
485 	 */
486 	i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR,
487 				     I3C_ADDR_SLOT_RSVD);
488 	for (i = 0; i < 7; i++)
489 		i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR ^ BIT(i),
490 					     I3C_ADDR_SLOT_RSVD);
491 }
492 
493 static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
494 {
495 	mutex_lock(&i3c_core_lock);
496 	idr_remove(&i3c_bus_idr, i3cbus->id);
497 	mutex_unlock(&i3c_core_lock);
498 }
499 
500 static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)
501 {
502 	int ret, start, end, id = -1;
503 
504 	init_rwsem(&i3cbus->lock);
505 	INIT_LIST_HEAD(&i3cbus->devs.i2c);
506 	INIT_LIST_HEAD(&i3cbus->devs.i3c);
507 	i3c_bus_init_addrslots(i3cbus);
508 	i3cbus->mode = I3C_BUS_MODE_PURE;
509 
510 	if (np)
511 		id = of_alias_get_id(np, "i3c");
512 
513 	mutex_lock(&i3c_core_lock);
514 	if (id >= 0) {
515 		start = id;
516 		end = start + 1;
517 	} else {
518 		start = __i3c_first_dynamic_bus_num;
519 		end = 0;
520 	}
521 
522 	ret = idr_alloc(&i3c_bus_idr, i3cbus, start, end, GFP_KERNEL);
523 	mutex_unlock(&i3c_core_lock);
524 
525 	if (ret < 0)
526 		return ret;
527 
528 	i3cbus->id = ret;
529 
530 	return 0;
531 }
532 
533 void i3c_for_each_bus_locked(int (*fn)(struct i3c_bus *bus, void *data),
534 			     void *data)
535 {
536 	struct i3c_bus *bus;
537 	int id;
538 
539 	mutex_lock(&i3c_core_lock);
540 	idr_for_each_entry(&i3c_bus_idr, bus, id)
541 		fn(bus, data);
542 	mutex_unlock(&i3c_core_lock);
543 }
544 EXPORT_SYMBOL_GPL(i3c_for_each_bus_locked);
545 
546 int i3c_register_notifier(struct notifier_block *nb)
547 {
548 	return blocking_notifier_chain_register(&i3c_bus_notifier, nb);
549 }
550 EXPORT_SYMBOL_GPL(i3c_register_notifier);
551 
552 int i3c_unregister_notifier(struct notifier_block *nb)
553 {
554 	return blocking_notifier_chain_unregister(&i3c_bus_notifier, nb);
555 }
556 EXPORT_SYMBOL_GPL(i3c_unregister_notifier);
557 
558 static void i3c_bus_notify(struct i3c_bus *bus, unsigned int action)
559 {
560 	blocking_notifier_call_chain(&i3c_bus_notifier, action, bus);
561 }
562 
563 static const char * const i3c_bus_mode_strings[] = {
564 	[I3C_BUS_MODE_PURE] = "pure",
565 	[I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
566 	[I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited",
567 	[I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow",
568 };
569 
570 static ssize_t mode_show(struct device *dev,
571 			 struct device_attribute *da,
572 			 char *buf)
573 {
574 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
575 	ssize_t ret;
576 
577 	i3c_bus_normaluse_lock(i3cbus);
578 	if (i3cbus->mode < 0 ||
579 	    i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) ||
580 	    !i3c_bus_mode_strings[i3cbus->mode])
581 		ret = sprintf(buf, "unknown\n");
582 	else
583 		ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]);
584 	i3c_bus_normaluse_unlock(i3cbus);
585 
586 	return ret;
587 }
588 static DEVICE_ATTR_RO(mode);
589 
590 static ssize_t current_master_show(struct device *dev,
591 				   struct device_attribute *da,
592 				   char *buf)
593 {
594 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
595 	ssize_t ret;
596 
597 	i3c_bus_normaluse_lock(i3cbus);
598 	ret = sprintf(buf, "%d-%llx\n", i3cbus->id,
599 		      i3cbus->cur_master->info.pid);
600 	i3c_bus_normaluse_unlock(i3cbus);
601 
602 	return ret;
603 }
604 static DEVICE_ATTR_RO(current_master);
605 
606 static ssize_t i3c_scl_frequency_show(struct device *dev,
607 				      struct device_attribute *da,
608 				      char *buf)
609 {
610 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
611 	ssize_t ret;
612 
613 	i3c_bus_normaluse_lock(i3cbus);
614 	ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c);
615 	i3c_bus_normaluse_unlock(i3cbus);
616 
617 	return ret;
618 }
619 static DEVICE_ATTR_RO(i3c_scl_frequency);
620 
621 static ssize_t i2c_scl_frequency_show(struct device *dev,
622 				      struct device_attribute *da,
623 				      char *buf)
624 {
625 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
626 	ssize_t ret;
627 
628 	i3c_bus_normaluse_lock(i3cbus);
629 	ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c);
630 	i3c_bus_normaluse_unlock(i3cbus);
631 
632 	return ret;
633 }
634 static DEVICE_ATTR_RO(i2c_scl_frequency);
635 
636 static int i3c_set_hotjoin(struct i3c_master_controller *master, bool enable)
637 {
638 	int ret;
639 
640 	if (!master || !master->ops)
641 		return -EINVAL;
642 
643 	if (!master->ops->enable_hotjoin || !master->ops->disable_hotjoin)
644 		return -EINVAL;
645 
646 	if (enable || master->rpm_ibi_allowed) {
647 		ret = i3c_master_rpm_get(master);
648 		if (ret)
649 			return ret;
650 	}
651 
652 	i3c_bus_normaluse_lock(&master->bus);
653 
654 	if (enable)
655 		ret = master->ops->enable_hotjoin(master);
656 	else
657 		ret = master->ops->disable_hotjoin(master);
658 
659 	if (!ret)
660 		master->hotjoin = enable;
661 
662 	i3c_bus_normaluse_unlock(&master->bus);
663 
664 	if ((enable && ret) || (!enable && !ret) || master->rpm_ibi_allowed)
665 		i3c_master_rpm_put(master);
666 
667 	return ret;
668 }
669 
670 static ssize_t hotjoin_store(struct device *dev, struct device_attribute *attr,
671 			     const char *buf, size_t count)
672 {
673 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
674 	int ret;
675 	bool res;
676 
677 	if (!i3cbus->cur_master)
678 		return -EINVAL;
679 
680 	if (kstrtobool(buf, &res))
681 		return -EINVAL;
682 
683 	ret = i3c_set_hotjoin(i3cbus->cur_master->common.master, res);
684 	if (ret)
685 		return ret;
686 
687 	return count;
688 }
689 
690 /*
691  * i3c_master_enable_hotjoin - Enable hotjoin
692  * @master: I3C master object
693  *
694  * Return: a 0 in case of success, an negative error code otherwise.
695  */
696 int i3c_master_enable_hotjoin(struct i3c_master_controller *master)
697 {
698 	return i3c_set_hotjoin(master, true);
699 }
700 EXPORT_SYMBOL_GPL(i3c_master_enable_hotjoin);
701 
702 /*
703  * i3c_master_disable_hotjoin - Disable hotjoin
704  * @master: I3C master object
705  *
706  * Return: a 0 in case of success, an negative error code otherwise.
707  */
708 int i3c_master_disable_hotjoin(struct i3c_master_controller *master)
709 {
710 	return i3c_set_hotjoin(master, false);
711 }
712 EXPORT_SYMBOL_GPL(i3c_master_disable_hotjoin);
713 
714 static ssize_t hotjoin_show(struct device *dev, struct device_attribute *da, char *buf)
715 {
716 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
717 	ssize_t ret;
718 
719 	i3c_bus_normaluse_lock(i3cbus);
720 	ret = sysfs_emit(buf, "%d\n", i3cbus->cur_master->common.master->hotjoin);
721 	i3c_bus_normaluse_unlock(i3cbus);
722 
723 	return ret;
724 }
725 
726 static DEVICE_ATTR_RW(hotjoin);
727 
728 static ssize_t dev_nack_retry_count_show(struct device *dev,
729 					 struct device_attribute *attr, char *buf)
730 {
731 	return sysfs_emit(buf, "%u\n", dev_to_i3cmaster(dev)->dev_nack_retry_count);
732 }
733 
734 static ssize_t dev_nack_retry_count_store(struct device *dev,
735 					  struct device_attribute *attr,
736 					  const char *buf, size_t count)
737 {
738 	struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
739 	struct i3c_master_controller *master = dev_to_i3cmaster(dev);
740 	unsigned long val;
741 	int ret;
742 
743 	ret = kstrtoul(buf, 0, &val);
744 	if (ret)
745 		return ret;
746 
747 	i3c_bus_maintenance_lock(i3cbus);
748 	ret = master->ops->set_dev_nack_retry(master, val);
749 	i3c_bus_maintenance_unlock(i3cbus);
750 
751 	if (ret)
752 		return ret;
753 
754 	master->dev_nack_retry_count = val;
755 
756 	return count;
757 }
758 
759 static DEVICE_ATTR_RW(dev_nack_retry_count);
760 
761 static ssize_t do_daa_store(struct device *dev,
762 			    struct device_attribute *attr,
763 			    const char *buf, size_t count)
764 {
765 	struct i3c_master_controller *master = dev_to_i3cmaster(dev);
766 	bool val;
767 	int ret;
768 
769 	if (kstrtobool(buf, &val))
770 		return -EINVAL;
771 
772 	if (!val)
773 		return -EINVAL;
774 
775 	if (!master->init_done)
776 		return -EAGAIN;
777 
778 	ret = i3c_master_do_daa(master);
779 	if (ret)
780 		return ret;
781 
782 	return count;
783 }
784 
785 static DEVICE_ATTR_WO(do_daa);
786 
787 static struct attribute *i3c_masterdev_attrs[] = {
788 	&dev_attr_mode.attr,
789 	&dev_attr_current_master.attr,
790 	&dev_attr_i3c_scl_frequency.attr,
791 	&dev_attr_i2c_scl_frequency.attr,
792 	&dev_attr_bcr.attr,
793 	&dev_attr_dcr.attr,
794 	&dev_attr_pid.attr,
795 	&dev_attr_dynamic_address.attr,
796 	&dev_attr_hdrcap.attr,
797 	&dev_attr_hotjoin.attr,
798 	&dev_attr_do_daa.attr,
799 	NULL,
800 };
801 ATTRIBUTE_GROUPS(i3c_masterdev);
802 
803 static void i3c_masterdev_release(struct device *dev)
804 {
805 	struct i3c_master_controller *master = dev_to_i3cmaster(dev);
806 	struct i3c_bus *bus = dev_to_i3cbus(dev);
807 
808 	if (master->wq)
809 		destroy_workqueue(master->wq);
810 
811 	WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c));
812 	i3c_bus_cleanup(bus);
813 
814 	of_node_put(dev->of_node);
815 }
816 
817 static const struct device_type i3c_masterdev_type = {
818 	.groups	= i3c_masterdev_groups,
819 };
820 
821 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode,
822 			    unsigned long max_i2c_scl_rate)
823 {
824 	struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus);
825 
826 	i3cbus->mode = mode;
827 
828 	switch (i3cbus->mode) {
829 	case I3C_BUS_MODE_PURE:
830 		if (!i3cbus->scl_rate.i3c)
831 			i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE;
832 		break;
833 	case I3C_BUS_MODE_MIXED_FAST:
834 	case I3C_BUS_MODE_MIXED_LIMITED:
835 		if (!i3cbus->scl_rate.i3c)
836 			i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE;
837 		if (!i3cbus->scl_rate.i2c)
838 			i3cbus->scl_rate.i2c = max_i2c_scl_rate;
839 		break;
840 	case I3C_BUS_MODE_MIXED_SLOW:
841 		if (!i3cbus->scl_rate.i2c)
842 			i3cbus->scl_rate.i2c = max_i2c_scl_rate;
843 		if (!i3cbus->scl_rate.i3c ||
844 		    i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c)
845 			i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c;
846 		break;
847 	default:
848 		return -EINVAL;
849 	}
850 
851 	dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
852 		i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c);
853 
854 	/*
855 	 * I3C/I2C frequency may have been overridden, check that user-provided
856 	 * values are not exceeding max possible frequency.
857 	 */
858 	if (i3cbus->scl_rate.i3c > I3C_BUS_I3C_SCL_MAX_RATE ||
859 	    i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE)
860 		return -EINVAL;
861 
862 	return 0;
863 }
864 
865 static struct i3c_master_controller *
866 i2c_adapter_to_i3c_master(struct i2c_adapter *adap)
867 {
868 	return container_of(adap, struct i3c_master_controller, i2c);
869 }
870 
871 static struct i2c_adapter *
872 i3c_master_to_i2c_adapter(struct i3c_master_controller *master)
873 {
874 	return &master->i2c;
875 }
876 
877 static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev)
878 {
879 	kfree(dev);
880 }
881 
882 static struct i2c_dev_desc *
883 i3c_master_alloc_i2c_dev(struct i3c_master_controller *master,
884 			 u16 addr, u8 lvr)
885 {
886 	struct i2c_dev_desc *dev;
887 
888 	dev = kzalloc_obj(*dev);
889 	if (!dev)
890 		return ERR_PTR(-ENOMEM);
891 
892 	dev->common.master = master;
893 	dev->addr = addr;
894 	dev->lvr = lvr;
895 
896 	return dev;
897 }
898 
899 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr,
900 				   u16 payloadlen)
901 {
902 	dest->addr = addr;
903 	dest->payload.len = payloadlen;
904 	if (payloadlen)
905 		dest->payload.data = kzalloc(payloadlen, GFP_KERNEL);
906 	else
907 		dest->payload.data = NULL;
908 
909 	return dest->payload.data;
910 }
911 
912 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest)
913 {
914 	kfree(dest->payload.data);
915 }
916 
917 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id,
918 			     struct i3c_ccc_cmd_dest *dests,
919 			     unsigned int ndests)
920 {
921 	cmd->rnw = rnw ? 1 : 0;
922 	cmd->id = id;
923 	cmd->dests = dests;
924 	cmd->ndests = ndests;
925 	cmd->err = I3C_ERROR_UNKNOWN;
926 }
927 
928 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master,
929 					  struct i3c_ccc_cmd *cmd)
930 {
931 	int ret;
932 
933 	if (!cmd || !master)
934 		return -EINVAL;
935 
936 	if (WARN_ON(master->init_done &&
937 		    !rwsem_is_locked(&master->bus.lock)))
938 		return -EINVAL;
939 
940 	if (!master->ops->send_ccc_cmd)
941 		return -EOPNOTSUPP;
942 
943 	if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests))
944 		return -EINVAL;
945 
946 	if (master->ops->supports_ccc_cmd &&
947 	    !master->ops->supports_ccc_cmd(master, cmd))
948 		return -EOPNOTSUPP;
949 
950 	ret = master->ops->send_ccc_cmd(master, cmd);
951 	if (ret) {
952 		if (cmd->err != I3C_ERROR_UNKNOWN)
953 			return cmd->err;
954 
955 		return ret;
956 	}
957 
958 	return 0;
959 }
960 
961 static struct i2c_dev_desc *
962 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master,
963 				u16 addr)
964 {
965 	struct i2c_dev_desc *dev;
966 
967 	i3c_bus_for_each_i2cdev(&master->bus, dev) {
968 		if (dev->addr == addr)
969 			return dev;
970 	}
971 
972 	return NULL;
973 }
974 
975 /**
976  * i3c_master_get_free_addr() - get a free address on the bus
977  * @master: I3C master object
978  * @start_addr: where to start searching
979  *
980  * This function must be called with the bus lock held in write mode.
981  *
982  * Return: the first free address starting at @start_addr (included) or -ENOMEM
983  * if there's no more address available.
984  */
985 int i3c_master_get_free_addr(struct i3c_master_controller *master,
986 			     u8 start_addr)
987 {
988 	return i3c_bus_get_free_addr(&master->bus, start_addr);
989 }
990 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr);
991 
992 static void i3c_device_release(struct device *dev)
993 {
994 	struct i3c_device *i3cdev = dev_to_i3cdev(dev);
995 
996 	WARN_ON(i3cdev->desc);
997 
998 	of_node_put(i3cdev->dev.of_node);
999 	kfree(i3cdev);
1000 }
1001 
1002 static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev)
1003 {
1004 	kfree(dev);
1005 }
1006 
1007 static struct i3c_dev_desc *
1008 i3c_master_alloc_i3c_dev(struct i3c_master_controller *master,
1009 			 const struct i3c_device_info *info)
1010 {
1011 	struct i3c_dev_desc *dev;
1012 
1013 	dev = kzalloc_obj(*dev);
1014 	if (!dev)
1015 		return ERR_PTR(-ENOMEM);
1016 
1017 	dev->common.master = master;
1018 	dev->info = *info;
1019 	mutex_init(&dev->ibi_lock);
1020 
1021 	return dev;
1022 }
1023 
1024 static int i3c_master_rstdaa_locked(struct i3c_master_controller *master,
1025 				    u8 addr)
1026 {
1027 	enum i3c_addr_slot_status addrstat;
1028 	struct i3c_ccc_cmd_dest dest;
1029 	struct i3c_ccc_cmd cmd;
1030 	int ret;
1031 
1032 	if (!master)
1033 		return -EINVAL;
1034 
1035 	addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr);
1036 	if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV)
1037 		return -EINVAL;
1038 
1039 	i3c_ccc_cmd_dest_init(&dest, addr, 0);
1040 	i3c_ccc_cmd_init(&cmd, false,
1041 			 I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR),
1042 			 &dest, 1);
1043 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1044 	i3c_ccc_cmd_dest_cleanup(&dest);
1045 
1046 	return ret;
1047 }
1048 
1049 /**
1050  * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
1051  *				procedure
1052  * @master: master used to send frames on the bus
1053  *
1054  * Send a ENTDAA CCC command to start a DAA procedure.
1055  *
1056  * Note that this function only sends the ENTDAA CCC command, all the logic
1057  * behind dynamic address assignment has to be handled in the I3C master
1058  * driver.
1059  *
1060  * This function must be called with the bus lock held in write mode.
1061  *
1062  * Return: 0 in case of success, a positive I3C error code if the error is
1063  * one of the official Mx error codes, and a negative error code otherwise.
1064  */
1065 int i3c_master_entdaa_locked(struct i3c_master_controller *master)
1066 {
1067 	struct i3c_ccc_cmd_dest dest;
1068 	struct i3c_ccc_cmd cmd;
1069 	int ret;
1070 
1071 	i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0);
1072 	i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1);
1073 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1074 	i3c_ccc_cmd_dest_cleanup(&dest);
1075 
1076 	return ret;
1077 }
1078 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked);
1079 
1080 static int i3c_master_enec_disec_locked(struct i3c_master_controller *master,
1081 					u8 addr, bool enable, u8 evts)
1082 {
1083 	struct i3c_ccc_events *events;
1084 	struct i3c_ccc_cmd_dest dest;
1085 	struct i3c_ccc_cmd cmd;
1086 	int ret;
1087 
1088 	events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events));
1089 	if (!events)
1090 		return -ENOMEM;
1091 
1092 	events->events = evts;
1093 	i3c_ccc_cmd_init(&cmd, false,
1094 			 enable ?
1095 			 I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) :
1096 			 I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR),
1097 			 &dest, 1);
1098 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1099 	i3c_ccc_cmd_dest_cleanup(&dest);
1100 
1101 	return ret;
1102 }
1103 
1104 /**
1105  * i3c_master_disec_locked() - send a DISEC CCC command
1106  * @master: master used to send frames on the bus
1107  * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1108  * @evts: events to disable
1109  *
1110  * Send a DISEC CCC command to disable some or all events coming from a
1111  * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
1112  *
1113  * This function must be called with the bus lock held in write mode.
1114  *
1115  * Return: 0 in case of success, a positive I3C error code if the error is
1116  * one of the official Mx error codes, and a negative error code otherwise.
1117  */
1118 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
1119 			    u8 evts)
1120 {
1121 	return i3c_master_enec_disec_locked(master, addr, false, evts);
1122 }
1123 EXPORT_SYMBOL_GPL(i3c_master_disec_locked);
1124 
1125 /**
1126  * i3c_master_enec_locked() - send an ENEC CCC command
1127  * @master: master used to send frames on the bus
1128  * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1129  * @evts: events to disable
1130  *
1131  * Sends an ENEC CCC command to enable some or all events coming from a
1132  * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
1133  *
1134  * This function must be called with the bus lock held in write mode.
1135  *
1136  * Return: 0 in case of success, a positive I3C error code if the error is
1137  * one of the official Mx error codes, and a negative error code otherwise.
1138  */
1139 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
1140 			   u8 evts)
1141 {
1142 	return i3c_master_enec_disec_locked(master, addr, true, evts);
1143 }
1144 EXPORT_SYMBOL_GPL(i3c_master_enec_locked);
1145 
1146 /**
1147  * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
1148  * @master: master used to send frames on the bus
1149  *
1150  * Send a DEFSLVS CCC command containing all the devices known to the @master.
1151  * This is useful when you have secondary masters on the bus to propagate
1152  * device information.
1153  *
1154  * This should be called after all I3C devices have been discovered (in other
1155  * words, after the DAA procedure has finished) and instantiated in
1156  * &i3c_master_controller_ops->bus_init().
1157  * It should also be called if a master ACKed an Hot-Join request and assigned
1158  * a dynamic address to the device joining the bus.
1159  *
1160  * This function must be called with the bus lock held in write mode.
1161  *
1162  * Return: 0 in case of success, a positive I3C error code if the error is
1163  * one of the official Mx error codes, and a negative error code otherwise.
1164  */
1165 int i3c_master_defslvs_locked(struct i3c_master_controller *master)
1166 {
1167 	struct i3c_ccc_defslvs *defslvs;
1168 	struct i3c_ccc_dev_desc *desc;
1169 	struct i3c_ccc_cmd_dest dest;
1170 	struct i3c_dev_desc *i3cdev;
1171 	struct i2c_dev_desc *i2cdev;
1172 	struct i3c_ccc_cmd cmd;
1173 	struct i3c_bus *bus;
1174 	bool send = false;
1175 	int ndevs = 0, ret;
1176 
1177 	if (!master)
1178 		return -EINVAL;
1179 
1180 	bus = i3c_master_get_bus(master);
1181 	i3c_bus_for_each_i3cdev(bus, i3cdev) {
1182 		ndevs++;
1183 
1184 		if (i3cdev == master->this)
1185 			continue;
1186 
1187 		if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) ==
1188 		    I3C_BCR_I3C_MASTER)
1189 			send = true;
1190 	}
1191 
1192 	/* No other master on the bus, skip DEFSLVS. */
1193 	if (!send)
1194 		return 0;
1195 
1196 	i3c_bus_for_each_i2cdev(bus, i2cdev)
1197 		ndevs++;
1198 
1199 	defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR,
1200 					struct_size(defslvs, slaves,
1201 						    ndevs - 1));
1202 	if (!defslvs)
1203 		return -ENOMEM;
1204 
1205 	defslvs->count = ndevs;
1206 	defslvs->master.bcr = master->this->info.bcr;
1207 	defslvs->master.dcr = master->this->info.dcr;
1208 	defslvs->master.dyn_addr = master->this->info.dyn_addr << 1;
1209 	defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1;
1210 
1211 	desc = defslvs->slaves;
1212 	i3c_bus_for_each_i2cdev(bus, i2cdev) {
1213 		desc->lvr = i2cdev->lvr;
1214 		desc->static_addr = i2cdev->addr << 1;
1215 		desc++;
1216 	}
1217 
1218 	i3c_bus_for_each_i3cdev(bus, i3cdev) {
1219 		/* Skip the I3C dev representing this master. */
1220 		if (i3cdev == master->this)
1221 			continue;
1222 
1223 		desc->bcr = i3cdev->info.bcr;
1224 		desc->dcr = i3cdev->info.dcr;
1225 		desc->dyn_addr = i3cdev->info.dyn_addr << 1;
1226 		desc->static_addr = i3cdev->info.static_addr << 1;
1227 		desc++;
1228 	}
1229 
1230 	i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1);
1231 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1232 	i3c_ccc_cmd_dest_cleanup(&dest);
1233 
1234 	return ret;
1235 }
1236 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked);
1237 
1238 static int i3c_master_setda_locked(struct i3c_master_controller *master,
1239 				   u8 oldaddr, u8 newaddr, bool setdasa)
1240 {
1241 	struct i3c_ccc_cmd_dest dest;
1242 	struct i3c_ccc_setda *setda;
1243 	struct i3c_ccc_cmd cmd;
1244 	int ret;
1245 
1246 	if (!oldaddr || !newaddr)
1247 		return -EINVAL;
1248 
1249 	setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda));
1250 	if (!setda)
1251 		return -ENOMEM;
1252 
1253 	setda->addr = newaddr << 1;
1254 	i3c_ccc_cmd_init(&cmd, false,
1255 			 setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA,
1256 			 &dest, 1);
1257 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1258 	i3c_ccc_cmd_dest_cleanup(&dest);
1259 
1260 	return ret;
1261 }
1262 
1263 static int i3c_master_setdasa_locked(struct i3c_master_controller *master,
1264 				     u8 static_addr, u8 dyn_addr)
1265 {
1266 	return i3c_master_setda_locked(master, static_addr, dyn_addr, true);
1267 }
1268 
1269 static int i3c_master_setnewda_locked(struct i3c_master_controller *master,
1270 				      u8 oldaddr, u8 newaddr)
1271 {
1272 	return i3c_master_setda_locked(master, oldaddr, newaddr, false);
1273 }
1274 
1275 static int i3c_master_getmrl_locked(struct i3c_master_controller *master,
1276 				    struct i3c_device_info *info)
1277 {
1278 	struct i3c_ccc_cmd_dest dest;
1279 	struct i3c_ccc_mrl *mrl;
1280 	struct i3c_ccc_cmd cmd;
1281 	int ret;
1282 
1283 	mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl));
1284 	if (!mrl)
1285 		return -ENOMEM;
1286 
1287 	/*
1288 	 * When the device does not have IBI payload GETMRL only returns 2
1289 	 * bytes of data.
1290 	 */
1291 	if (!(info->bcr & I3C_BCR_IBI_PAYLOAD))
1292 		dest.payload.len -= 1;
1293 
1294 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1);
1295 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1296 	if (ret)
1297 		goto out;
1298 
1299 	switch (dest.payload.len) {
1300 	case 3:
1301 		info->max_ibi_len = mrl->ibi_len;
1302 		fallthrough;
1303 	case 2:
1304 		info->max_read_len = be16_to_cpu(mrl->read_len);
1305 		break;
1306 	default:
1307 		ret = -EIO;
1308 		goto out;
1309 	}
1310 
1311 out:
1312 	i3c_ccc_cmd_dest_cleanup(&dest);
1313 
1314 	return ret;
1315 }
1316 
1317 static int i3c_master_getmwl_locked(struct i3c_master_controller *master,
1318 				    struct i3c_device_info *info)
1319 {
1320 	struct i3c_ccc_cmd_dest dest;
1321 	struct i3c_ccc_mwl *mwl;
1322 	struct i3c_ccc_cmd cmd;
1323 	int ret;
1324 
1325 	mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl));
1326 	if (!mwl)
1327 		return -ENOMEM;
1328 
1329 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1);
1330 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1331 	if (ret)
1332 		goto out;
1333 
1334 	if (dest.payload.len != sizeof(*mwl)) {
1335 		ret = -EIO;
1336 		goto out;
1337 	}
1338 
1339 	info->max_write_len = be16_to_cpu(mwl->len);
1340 
1341 out:
1342 	i3c_ccc_cmd_dest_cleanup(&dest);
1343 
1344 	return ret;
1345 }
1346 
1347 static int i3c_master_getmxds_locked(struct i3c_master_controller *master,
1348 				     struct i3c_device_info *info)
1349 {
1350 	struct i3c_ccc_getmxds *getmaxds;
1351 	struct i3c_ccc_cmd_dest dest;
1352 	struct i3c_ccc_cmd cmd;
1353 	int ret;
1354 
1355 	getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1356 					 sizeof(*getmaxds));
1357 	if (!getmaxds)
1358 		return -ENOMEM;
1359 
1360 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1);
1361 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1362 	if (ret) {
1363 		/*
1364 		 * Retry when the device does not support max read turnaround
1365 		 * while expecting shorter length from this CCC command.
1366 		 */
1367 		dest.payload.len -= 3;
1368 		ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1369 		if (ret)
1370 			goto out;
1371 	}
1372 
1373 	if (dest.payload.len != 2 && dest.payload.len != 5) {
1374 		ret = -EIO;
1375 		goto out;
1376 	}
1377 
1378 	info->max_read_ds = getmaxds->maxrd;
1379 	info->max_write_ds = getmaxds->maxwr;
1380 	if (dest.payload.len == 5)
1381 		info->max_read_turnaround = getmaxds->maxrdturn[0] |
1382 					    ((u32)getmaxds->maxrdturn[1] << 8) |
1383 					    ((u32)getmaxds->maxrdturn[2] << 16);
1384 
1385 out:
1386 	i3c_ccc_cmd_dest_cleanup(&dest);
1387 
1388 	return ret;
1389 }
1390 
1391 static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master,
1392 				       struct i3c_device_info *info)
1393 {
1394 	struct i3c_ccc_gethdrcap *gethdrcap;
1395 	struct i3c_ccc_cmd_dest dest;
1396 	struct i3c_ccc_cmd cmd;
1397 	int ret;
1398 
1399 	gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1400 					  sizeof(*gethdrcap));
1401 	if (!gethdrcap)
1402 		return -ENOMEM;
1403 
1404 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1);
1405 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1406 	if (ret)
1407 		goto out;
1408 
1409 	if (dest.payload.len != 1) {
1410 		ret = -EIO;
1411 		goto out;
1412 	}
1413 
1414 	info->hdr_cap = gethdrcap->modes;
1415 
1416 out:
1417 	i3c_ccc_cmd_dest_cleanup(&dest);
1418 
1419 	return ret;
1420 }
1421 
1422 static int i3c_master_getpid_locked(struct i3c_master_controller *master,
1423 				    struct i3c_device_info *info)
1424 {
1425 	struct i3c_ccc_getpid *getpid;
1426 	struct i3c_ccc_cmd_dest dest;
1427 	struct i3c_ccc_cmd cmd;
1428 	int ret, i;
1429 
1430 	getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid));
1431 	if (!getpid)
1432 		return -ENOMEM;
1433 
1434 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1);
1435 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1436 	if (ret)
1437 		goto out;
1438 
1439 	info->pid = 0;
1440 	for (i = 0; i < sizeof(getpid->pid); i++) {
1441 		int sft = (sizeof(getpid->pid) - i - 1) * 8;
1442 
1443 		info->pid |= (u64)getpid->pid[i] << sft;
1444 	}
1445 
1446 out:
1447 	i3c_ccc_cmd_dest_cleanup(&dest);
1448 
1449 	return ret;
1450 }
1451 
1452 static int i3c_master_getbcr_locked(struct i3c_master_controller *master,
1453 				    struct i3c_device_info *info)
1454 {
1455 	struct i3c_ccc_getbcr *getbcr;
1456 	struct i3c_ccc_cmd_dest dest;
1457 	struct i3c_ccc_cmd cmd;
1458 	int ret;
1459 
1460 	getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr));
1461 	if (!getbcr)
1462 		return -ENOMEM;
1463 
1464 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1);
1465 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1466 	if (ret)
1467 		goto out;
1468 
1469 	info->bcr = getbcr->bcr;
1470 
1471 out:
1472 	i3c_ccc_cmd_dest_cleanup(&dest);
1473 
1474 	return ret;
1475 }
1476 
1477 static int i3c_master_getdcr_locked(struct i3c_master_controller *master,
1478 				    struct i3c_device_info *info)
1479 {
1480 	struct i3c_ccc_getdcr *getdcr;
1481 	struct i3c_ccc_cmd_dest dest;
1482 	struct i3c_ccc_cmd cmd;
1483 	int ret;
1484 
1485 	getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr));
1486 	if (!getdcr)
1487 		return -ENOMEM;
1488 
1489 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1);
1490 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1491 	if (ret)
1492 		goto out;
1493 
1494 	info->dcr = getdcr->dcr;
1495 
1496 out:
1497 	i3c_ccc_cmd_dest_cleanup(&dest);
1498 
1499 	return ret;
1500 }
1501 
1502 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev)
1503 {
1504 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1505 	enum i3c_addr_slot_status slot_status;
1506 	int ret;
1507 
1508 	if (!dev->info.dyn_addr)
1509 		return -EINVAL;
1510 
1511 	slot_status = i3c_bus_get_addr_slot_status(&master->bus,
1512 						   dev->info.dyn_addr);
1513 	if (slot_status == I3C_ADDR_SLOT_RSVD ||
1514 	    slot_status == I3C_ADDR_SLOT_I2C_DEV)
1515 		return -EINVAL;
1516 
1517 	ret = i3c_master_getpid_locked(master, &dev->info);
1518 	if (ret)
1519 		return ret;
1520 
1521 	ret = i3c_master_getbcr_locked(master, &dev->info);
1522 	if (ret)
1523 		return ret;
1524 
1525 	ret = i3c_master_getdcr_locked(master, &dev->info);
1526 	if (ret)
1527 		return ret;
1528 
1529 	if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) {
1530 		ret = i3c_master_getmxds_locked(master, &dev->info);
1531 		if (ret)
1532 			return ret;
1533 	}
1534 
1535 	if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD)
1536 		dev->info.max_ibi_len = 1;
1537 
1538 	i3c_master_getmrl_locked(master, &dev->info);
1539 	i3c_master_getmwl_locked(master, &dev->info);
1540 
1541 	if (dev->info.bcr & I3C_BCR_HDR_CAP) {
1542 		ret = i3c_master_gethdrcap_locked(master, &dev->info);
1543 		if (ret && ret != -EOPNOTSUPP)
1544 			return ret;
1545 	}
1546 
1547 	return 0;
1548 }
1549 
1550 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev)
1551 {
1552 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1553 
1554 	if (dev->info.static_addr)
1555 		i3c_bus_set_addr_slot_status(&master->bus,
1556 					     dev->info.static_addr,
1557 					     I3C_ADDR_SLOT_FREE);
1558 
1559 	if (dev->info.dyn_addr)
1560 		i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1561 					     I3C_ADDR_SLOT_FREE);
1562 
1563 	if (dev->boardinfo && dev->boardinfo->init_dyn_addr)
1564 		i3c_bus_set_addr_slot_status(&master->bus, dev->boardinfo->init_dyn_addr,
1565 					     I3C_ADDR_SLOT_FREE);
1566 }
1567 
1568 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev)
1569 {
1570 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1571 	enum i3c_addr_slot_status status;
1572 
1573 	if (!dev->info.static_addr && !dev->info.dyn_addr)
1574 		return 0;
1575 
1576 	if (dev->info.static_addr) {
1577 		status = i3c_bus_get_addr_slot_status(&master->bus,
1578 						      dev->info.static_addr);
1579 		/* Since static address and assigned dynamic address can be
1580 		 * equal, allow this case to pass.
1581 		 */
1582 		if (status != I3C_ADDR_SLOT_FREE &&
1583 		    dev->info.static_addr != dev->boardinfo->init_dyn_addr)
1584 			return -EBUSY;
1585 
1586 		i3c_bus_set_addr_slot_status(&master->bus,
1587 					     dev->info.static_addr,
1588 					     I3C_ADDR_SLOT_I3C_DEV);
1589 	}
1590 
1591 	/*
1592 	 * ->init_dyn_addr should have been reserved before that, so, if we're
1593 	 * trying to apply a pre-reserved dynamic address, we should not try
1594 	 * to reserve the address slot a second time.
1595 	 */
1596 	if (dev->info.dyn_addr &&
1597 	    (!dev->boardinfo ||
1598 	     dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) {
1599 		status = i3c_bus_get_addr_slot_status(&master->bus,
1600 						      dev->info.dyn_addr);
1601 		if (status != I3C_ADDR_SLOT_FREE)
1602 			goto err_release_static_addr;
1603 
1604 		i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1605 					     I3C_ADDR_SLOT_I3C_DEV);
1606 	}
1607 
1608 	return 0;
1609 
1610 err_release_static_addr:
1611 	if (dev->info.static_addr)
1612 		i3c_bus_set_addr_slot_status(&master->bus,
1613 					     dev->info.static_addr,
1614 					     I3C_ADDR_SLOT_FREE);
1615 
1616 	return -EBUSY;
1617 }
1618 
1619 static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master,
1620 				     struct i3c_dev_desc *dev)
1621 {
1622 	int ret;
1623 
1624 	/*
1625 	 * We don't attach devices to the controller until they are
1626 	 * addressable on the bus.
1627 	 */
1628 	if (!dev->info.static_addr && !dev->info.dyn_addr)
1629 		return 0;
1630 
1631 	ret = i3c_master_get_i3c_addrs(dev);
1632 	if (ret)
1633 		return ret;
1634 
1635 	/* Do not attach the master device itself. */
1636 	if (master->this != dev && master->ops->attach_i3c_dev) {
1637 		ret = master->ops->attach_i3c_dev(dev);
1638 		if (ret) {
1639 			i3c_master_put_i3c_addrs(dev);
1640 			return ret;
1641 		}
1642 	}
1643 
1644 	list_add_tail(&dev->common.node, &master->bus.devs.i3c);
1645 
1646 	return 0;
1647 }
1648 
1649 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
1650 				       u8 old_dyn_addr)
1651 {
1652 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1653 	int ret;
1654 
1655 	if (dev->info.dyn_addr != old_dyn_addr) {
1656 		i3c_bus_set_addr_slot_status(&master->bus,
1657 					     dev->info.dyn_addr,
1658 					     I3C_ADDR_SLOT_I3C_DEV);
1659 		if (old_dyn_addr)
1660 			i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr,
1661 						     I3C_ADDR_SLOT_FREE);
1662 	}
1663 
1664 	if (master->ops->reattach_i3c_dev) {
1665 		ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr);
1666 		if (ret) {
1667 			i3c_master_put_i3c_addrs(dev);
1668 			return ret;
1669 		}
1670 	}
1671 
1672 	return 0;
1673 }
1674 
1675 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
1676 {
1677 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1678 
1679 	/* Do not detach the master device itself. */
1680 	if (master->this != dev && master->ops->detach_i3c_dev)
1681 		master->ops->detach_i3c_dev(dev);
1682 
1683 	i3c_master_put_i3c_addrs(dev);
1684 	list_del(&dev->common.node);
1685 }
1686 
1687 static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master,
1688 				     struct i2c_dev_desc *dev)
1689 {
1690 	int ret;
1691 
1692 	if (master->ops->attach_i2c_dev) {
1693 		ret = master->ops->attach_i2c_dev(dev);
1694 		if (ret)
1695 			return ret;
1696 	}
1697 
1698 	list_add_tail(&dev->common.node, &master->bus.devs.i2c);
1699 
1700 	return 0;
1701 }
1702 
1703 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
1704 {
1705 	struct i3c_master_controller *master = i2c_dev_get_master(dev);
1706 
1707 	list_del(&dev->common.node);
1708 
1709 	if (master->ops->detach_i2c_dev)
1710 		master->ops->detach_i2c_dev(dev);
1711 }
1712 
1713 static int i3c_master_early_i3c_dev_add(struct i3c_master_controller *master,
1714 					  struct i3c_dev_boardinfo *boardinfo)
1715 {
1716 	struct i3c_device_info info = {
1717 		.static_addr = boardinfo->static_addr,
1718 		.pid = boardinfo->pid,
1719 	};
1720 	struct i3c_dev_desc *i3cdev;
1721 	int ret;
1722 
1723 	i3cdev = i3c_master_alloc_i3c_dev(master, &info);
1724 	if (IS_ERR(i3cdev))
1725 		return -ENOMEM;
1726 
1727 	i3cdev->boardinfo = boardinfo;
1728 
1729 	ret = i3c_master_attach_i3c_dev(master, i3cdev);
1730 	if (ret)
1731 		goto err_free_dev;
1732 
1733 	ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr,
1734 					i3cdev->boardinfo->init_dyn_addr);
1735 	if (ret)
1736 		goto err_detach_dev;
1737 
1738 	i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr;
1739 	ret = i3c_master_reattach_i3c_dev(i3cdev, 0);
1740 	if (ret)
1741 		goto err_rstdaa;
1742 
1743 	ret = i3c_master_retrieve_dev_info(i3cdev);
1744 	if (ret)
1745 		goto err_rstdaa;
1746 
1747 	return 0;
1748 
1749 err_rstdaa:
1750 	i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr);
1751 err_detach_dev:
1752 	i3c_master_detach_i3c_dev(i3cdev);
1753 err_free_dev:
1754 	i3c_master_free_i3c_dev(i3cdev);
1755 
1756 	return ret;
1757 }
1758 
1759 static void
1760 i3c_master_register_new_i3c_devs(struct i3c_master_controller *master)
1761 {
1762 	struct i3c_dev_desc *desc;
1763 	int ret;
1764 
1765 	if (!master->init_done)
1766 		return;
1767 
1768 	i3c_bus_for_each_i3cdev(&master->bus, desc) {
1769 		if (desc->dev || !desc->info.dyn_addr || desc == master->this)
1770 			continue;
1771 
1772 		desc->dev = kzalloc_obj(*desc->dev);
1773 		if (!desc->dev)
1774 			continue;
1775 
1776 		desc->dev->bus = &master->bus;
1777 		desc->dev->desc = desc;
1778 		desc->dev->dev.parent = &master->dev;
1779 		desc->dev->dev.type = &i3c_device_type;
1780 		desc->dev->dev.bus = &i3c_bus_type;
1781 		desc->dev->dev.release = i3c_device_release;
1782 		dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id,
1783 			     desc->info.pid);
1784 
1785 		if (desc->boardinfo)
1786 			desc->dev->dev.of_node = desc->boardinfo->of_node;
1787 
1788 		ret = device_register(&desc->dev->dev);
1789 		if (ret) {
1790 			dev_err(&master->dev,
1791 				"Failed to add I3C device (err = %d)\n", ret);
1792 			put_device(&desc->dev->dev);
1793 		}
1794 	}
1795 }
1796 
1797 /**
1798  * i3c_master_do_daa_ext() - Dynamic Address Assignment (extended version)
1799  * @master: controller
1800  * @rstdaa: whether to first perform Reset of Dynamic Addresses (RSTDAA)
1801  *
1802  * Perform Dynamic Address Assignment with optional support for System
1803  * Hibernation (@rstdaa is true).
1804  *
1805  * After System Hibernation, Dynamic Addresses can have been reassigned at boot
1806  * time to different values. A simple strategy is followed to handle that.
1807  * Perform a Reset of Dynamic Addresses (RSTDAA) followed by the normal DAA
1808  * procedure which has provision for reassigning addresses that differ from the
1809  * previously recorded addresses.
1810  *
1811  * Return: a 0 in case of success, an negative error code otherwise.
1812  */
1813 int i3c_master_do_daa_ext(struct i3c_master_controller *master, bool rstdaa)
1814 {
1815 	int rstret = 0;
1816 	int ret;
1817 
1818 	ret = i3c_master_rpm_get(master);
1819 	if (ret)
1820 		return ret;
1821 
1822 	i3c_bus_maintenance_lock(&master->bus);
1823 
1824 	if (rstdaa) {
1825 		rstret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1826 		if (rstret == I3C_ERROR_M2)
1827 			rstret = 0;
1828 	}
1829 
1830 	ret = master->ops->do_daa(master);
1831 
1832 	i3c_bus_maintenance_unlock(&master->bus);
1833 
1834 	if (ret)
1835 		goto out;
1836 
1837 	i3c_bus_normaluse_lock(&master->bus);
1838 	i3c_master_register_new_i3c_devs(master);
1839 	i3c_bus_normaluse_unlock(&master->bus);
1840 out:
1841 	i3c_master_rpm_put(master);
1842 
1843 	return rstret ?: ret;
1844 }
1845 EXPORT_SYMBOL_GPL(i3c_master_do_daa_ext);
1846 
1847 /**
1848  * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1849  * @master: master doing the DAA
1850  *
1851  * This function instantiates I3C device objects and adds them to the
1852  * I3C device list. All device information is automatically retrieved using
1853  * standard CCC commands.
1854  *
1855  * Return: a 0 in case of success, an negative error code otherwise.
1856  */
1857 int i3c_master_do_daa(struct i3c_master_controller *master)
1858 {
1859 	return i3c_master_do_daa_ext(master, false);
1860 }
1861 EXPORT_SYMBOL_GPL(i3c_master_do_daa);
1862 
1863 /**
1864  * i3c_master_dma_map_single() - Map buffer for single DMA transfer
1865  * @dev: device object of a device doing DMA
1866  * @buf: destination/source buffer for DMA
1867  * @len: length of transfer
1868  * @force_bounce: true, force to use a bounce buffer,
1869  *                false, function will auto check is a bounce buffer required
1870  * @dir: DMA direction
1871  *
1872  * Map buffer for a DMA transfer and allocate a bounce buffer if required.
1873  *
1874  * Return: I3C DMA transfer descriptor or NULL in case of error.
1875  */
1876 struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *buf,
1877 	size_t len, bool force_bounce, enum dma_data_direction dir)
1878 {
1879 	void *bounce __free(kfree) = NULL;
1880 	void *dma_buf = buf;
1881 
1882 	struct i3c_dma *dma_xfer __free(kfree) = kzalloc_obj(*dma_xfer);
1883 	if (!dma_xfer)
1884 		return NULL;
1885 
1886 	dma_xfer->dev = dev;
1887 	dma_xfer->buf = buf;
1888 	dma_xfer->dir = dir;
1889 	dma_xfer->len = len;
1890 	dma_xfer->map_len = len;
1891 
1892 	if (is_vmalloc_addr(buf))
1893 		force_bounce = true;
1894 
1895 	if (force_bounce) {
1896 		dma_xfer->map_len = ALIGN(len, cache_line_size());
1897 		if (dir == DMA_FROM_DEVICE)
1898 			bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL);
1899 		else
1900 			bounce = kmemdup(buf, dma_xfer->map_len, GFP_KERNEL);
1901 		if (!bounce)
1902 			return NULL;
1903 		dma_buf = bounce;
1904 	}
1905 
1906 	dma_xfer->addr = dma_map_single(dev, dma_buf, dma_xfer->map_len, dir);
1907 	if (dma_mapping_error(dev, dma_xfer->addr))
1908 		return NULL;
1909 
1910 	dma_xfer->bounce_buf = no_free_ptr(bounce);
1911 	return no_free_ptr(dma_xfer);
1912 }
1913 EXPORT_SYMBOL_GPL(i3c_master_dma_map_single);
1914 
1915 /**
1916  * i3c_master_dma_unmap_single() - Unmap buffer after DMA
1917  * @dma_xfer: DMA transfer and mapping descriptor
1918  *
1919  * Unmap buffer and cleanup DMA transfer descriptor.
1920  */
1921 void i3c_master_dma_unmap_single(struct i3c_dma *dma_xfer)
1922 {
1923 	dma_unmap_single(dma_xfer->dev, dma_xfer->addr,
1924 			 dma_xfer->map_len, dma_xfer->dir);
1925 	if (dma_xfer->bounce_buf) {
1926 		if (dma_xfer->dir == DMA_FROM_DEVICE)
1927 			memcpy(dma_xfer->buf, dma_xfer->bounce_buf,
1928 			       dma_xfer->len);
1929 		kfree(dma_xfer->bounce_buf);
1930 	}
1931 	kfree(dma_xfer);
1932 }
1933 EXPORT_SYMBOL_GPL(i3c_master_dma_unmap_single);
1934 
1935 /**
1936  * i3c_master_set_info() - set master device information
1937  * @master: master used to send frames on the bus
1938  * @info: I3C device information
1939  *
1940  * Set master device info. This should be called from
1941  * &i3c_master_controller_ops->bus_init().
1942  *
1943  * Not all &i3c_device_info fields are meaningful for a master device.
1944  * Here is a list of fields that should be properly filled:
1945  *
1946  * - &i3c_device_info->dyn_addr
1947  * - &i3c_device_info->bcr
1948  * - &i3c_device_info->dcr
1949  * - &i3c_device_info->pid
1950  * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1951  *   &i3c_device_info->bcr
1952  *
1953  * This function must be called with the bus lock held in maintenance mode.
1954  *
1955  * Return: 0 if @info contains valid information (not every piece of
1956  * information can be checked, but we can at least make sure @info->dyn_addr
1957  * and @info->bcr are correct), -EINVAL otherwise.
1958  */
1959 int i3c_master_set_info(struct i3c_master_controller *master,
1960 			const struct i3c_device_info *info)
1961 {
1962 	struct i3c_dev_desc *i3cdev;
1963 	int ret;
1964 
1965 	if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr))
1966 		return -EINVAL;
1967 
1968 	if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER &&
1969 	    master->secondary)
1970 		return -EINVAL;
1971 
1972 	if (master->this)
1973 		return -EINVAL;
1974 
1975 	i3cdev = i3c_master_alloc_i3c_dev(master, info);
1976 	if (IS_ERR(i3cdev))
1977 		return PTR_ERR(i3cdev);
1978 
1979 	master->this = i3cdev;
1980 	master->bus.cur_master = master->this;
1981 
1982 	ret = i3c_master_attach_i3c_dev(master, i3cdev);
1983 	if (ret)
1984 		goto err_free_dev;
1985 
1986 	return 0;
1987 
1988 err_free_dev:
1989 	i3c_master_free_i3c_dev(i3cdev);
1990 
1991 	return ret;
1992 }
1993 EXPORT_SYMBOL_GPL(i3c_master_set_info);
1994 
1995 static void i3c_master_detach_free_devs(struct i3c_master_controller *master)
1996 {
1997 	struct i3c_dev_desc *i3cdev, *i3ctmp;
1998 	struct i2c_dev_desc *i2cdev, *i2ctmp;
1999 
2000 	list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c,
2001 				 common.node) {
2002 		i3c_master_detach_i3c_dev(i3cdev);
2003 
2004 		if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr)
2005 			i3c_bus_set_addr_slot_status(&master->bus,
2006 					i3cdev->boardinfo->init_dyn_addr,
2007 					I3C_ADDR_SLOT_FREE);
2008 
2009 		i3c_master_free_i3c_dev(i3cdev);
2010 	}
2011 
2012 	list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c,
2013 				 common.node) {
2014 		i3c_master_detach_i2c_dev(i2cdev);
2015 		i3c_bus_set_addr_slot_status(&master->bus,
2016 					     i2cdev->addr,
2017 					     I3C_ADDR_SLOT_FREE);
2018 		i3c_master_free_i2c_dev(i2cdev);
2019 	}
2020 }
2021 
2022 /**
2023  * i3c_master_bus_init() - initialize an I3C bus
2024  * @master: main master initializing the bus
2025  *
2026  * This function is following all initialisation steps described in the I3C
2027  * specification:
2028  *
2029  * 1. Attach I2C devs to the master so that the master can fill its internal
2030  *    device table appropriately
2031  *
2032  * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
2033  *    the master controller. That's usually where the bus mode is selected
2034  *    (pure bus or mixed fast/slow bus)
2035  *
2036  * 3. Instruct all devices on the bus to drop their dynamic address. This is
2037  *    particularly important when the bus was previously configured by someone
2038  *    else (for example the bootloader)
2039  *
2040  * 4. Disable all slave events.
2041  *
2042  * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices
2043  *    also have static_addr, try to pre-assign dynamic addresses requested by
2044  *    the FW with SETDASA and attach corresponding statically defined I3C
2045  *    devices to the master.
2046  *
2047  * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
2048  *    remaining I3C devices
2049  *
2050  * Once this is done, all I3C and I2C devices should be usable.
2051  *
2052  * Return: a 0 in case of success, an negative error code otherwise.
2053  */
2054 static int i3c_master_bus_init(struct i3c_master_controller *master)
2055 {
2056 	enum i3c_addr_slot_status status;
2057 	struct i2c_dev_boardinfo *i2cboardinfo;
2058 	struct i3c_dev_boardinfo *i3cboardinfo;
2059 	struct i2c_dev_desc *i2cdev;
2060 	int ret;
2061 
2062 	/*
2063 	 * First attach all devices with static definitions provided by the
2064 	 * FW.
2065 	 */
2066 	list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2067 		status = i3c_bus_get_addr_slot_status(&master->bus,
2068 						      i2cboardinfo->base.addr);
2069 		if (status != I3C_ADDR_SLOT_FREE) {
2070 			ret = -EBUSY;
2071 			goto err_detach_devs;
2072 		}
2073 
2074 		i3c_bus_set_addr_slot_status(&master->bus,
2075 					     i2cboardinfo->base.addr,
2076 					     I3C_ADDR_SLOT_I2C_DEV);
2077 
2078 		i2cdev = i3c_master_alloc_i2c_dev(master,
2079 						  i2cboardinfo->base.addr,
2080 						  i2cboardinfo->lvr);
2081 		if (IS_ERR(i2cdev)) {
2082 			ret = PTR_ERR(i2cdev);
2083 			goto err_detach_devs;
2084 		}
2085 
2086 		ret = i3c_master_attach_i2c_dev(master, i2cdev);
2087 		if (ret) {
2088 			i3c_master_free_i2c_dev(i2cdev);
2089 			goto err_detach_devs;
2090 		}
2091 	}
2092 
2093 	/*
2094 	 * Now execute the controller specific ->bus_init() routine, which
2095 	 * might configure its internal logic to match the bus limitations.
2096 	 */
2097 	ret = master->ops->bus_init(master);
2098 	if (ret)
2099 		goto err_detach_devs;
2100 
2101 	/*
2102 	 * The master device should have been instantiated in ->bus_init(),
2103 	 * complain if this was not the case.
2104 	 */
2105 	if (!master->this) {
2106 		dev_err(&master->dev,
2107 			"master_set_info() was not called in ->bus_init()\n");
2108 		ret = -EINVAL;
2109 		goto err_bus_cleanup;
2110 	}
2111 
2112 	if (master->ops->set_speed) {
2113 		ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_SLOW_SPEED);
2114 		if (ret)
2115 			goto err_bus_cleanup;
2116 	}
2117 
2118 	/*
2119 	 * Reset all dynamic address that may have been assigned before
2120 	 * (assigned by the bootloader for example).
2121 	 */
2122 	ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
2123 	if (ret && ret != I3C_ERROR_M2)
2124 		goto err_bus_cleanup;
2125 
2126 	if (master->ops->set_speed) {
2127 		ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_NORMAL_SPEED);
2128 		if (ret)
2129 			goto err_bus_cleanup;
2130 	}
2131 
2132 	/* Disable all slave events before starting DAA. */
2133 	ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR,
2134 				      I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR |
2135 				      I3C_CCC_EVENT_HJ);
2136 	if (ret && ret != I3C_ERROR_M2)
2137 		goto err_bus_cleanup;
2138 
2139 	/*
2140 	 * Reserve init_dyn_addr first, and then try to pre-assign dynamic
2141 	 * address and retrieve device information if needed.
2142 	 * In case pre-assign dynamic address fails, setting dynamic address to
2143 	 * the requested init_dyn_addr is retried after DAA is done in
2144 	 * i3c_master_add_i3c_dev_locked().
2145 	 */
2146 	list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
2147 
2148 		/*
2149 		 * We don't reserve a dynamic address for devices that
2150 		 * don't explicitly request one.
2151 		 */
2152 		if (!i3cboardinfo->init_dyn_addr)
2153 			continue;
2154 
2155 		ret = i3c_bus_get_addr_slot_status(&master->bus,
2156 						   i3cboardinfo->init_dyn_addr);
2157 		if (ret != I3C_ADDR_SLOT_FREE) {
2158 			ret = -EBUSY;
2159 			goto err_rstdaa;
2160 		}
2161 
2162 		/* Do not mark as occupied until real device exist in bus */
2163 		i3c_bus_set_addr_slot_status_mask(&master->bus,
2164 						  i3cboardinfo->init_dyn_addr,
2165 						  I3C_ADDR_SLOT_EXT_DESIRED,
2166 						  I3C_ADDR_SLOT_EXT_STATUS_MASK);
2167 
2168 		/*
2169 		 * Only try to create/attach devices that have a static
2170 		 * address. Other devices will be created/attached when
2171 		 * DAA happens, and the requested dynamic address will
2172 		 * be set using SETNEWDA once those devices become
2173 		 * addressable.
2174 		 */
2175 
2176 		if (i3cboardinfo->static_addr)
2177 			i3c_master_early_i3c_dev_add(master, i3cboardinfo);
2178 	}
2179 
2180 	ret = i3c_master_do_daa(master);
2181 	if (ret)
2182 		goto err_rstdaa;
2183 
2184 	return 0;
2185 
2186 err_rstdaa:
2187 	i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
2188 
2189 err_bus_cleanup:
2190 	if (master->ops->bus_cleanup)
2191 		master->ops->bus_cleanup(master);
2192 
2193 err_detach_devs:
2194 	i3c_master_detach_free_devs(master);
2195 
2196 	return ret;
2197 }
2198 
2199 static void i3c_master_bus_cleanup(struct i3c_master_controller *master)
2200 {
2201 	if (master->ops->bus_cleanup) {
2202 		int ret = i3c_master_rpm_get(master);
2203 
2204 		if (ret) {
2205 			dev_err(&master->dev,
2206 				"runtime resume error: master bus_cleanup() not done\n");
2207 		} else {
2208 			master->ops->bus_cleanup(master);
2209 			i3c_master_rpm_put(master);
2210 		}
2211 	}
2212 
2213 	i3c_master_detach_free_devs(master);
2214 }
2215 
2216 static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev)
2217 {
2218 	struct i3c_master_controller *master = i3cdev->common.master;
2219 	struct i3c_dev_boardinfo *i3cboardinfo;
2220 
2221 	list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
2222 		if (i3cdev->info.pid != i3cboardinfo->pid)
2223 			continue;
2224 
2225 		i3cdev->boardinfo = i3cboardinfo;
2226 		i3cdev->info.static_addr = i3cboardinfo->static_addr;
2227 		return;
2228 	}
2229 }
2230 
2231 static struct i3c_dev_desc *
2232 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev)
2233 {
2234 	struct i3c_master_controller *master = i3c_dev_get_master(refdev);
2235 	struct i3c_dev_desc *i3cdev;
2236 
2237 	i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2238 		if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid)
2239 			return i3cdev;
2240 	}
2241 
2242 	return NULL;
2243 }
2244 
2245 /**
2246  * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
2247  * @master: master used to send frames on the bus
2248  * @addr: I3C slave dynamic address assigned to the device
2249  *
2250  * This function is instantiating an I3C device object and adding it to the
2251  * I3C device list. All device information are automatically retrieved using
2252  * standard CCC commands.
2253  *
2254  * The I3C device object is returned in case the master wants to attach
2255  * private data to it using i3c_dev_set_master_data().
2256  *
2257  * This function must be called with the bus lock held in write mode.
2258  *
2259  * Return: a 0 in case of success, an negative error code otherwise.
2260  */
2261 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
2262 				  u8 addr)
2263 {
2264 	struct i3c_device_info info = { .dyn_addr = addr };
2265 	struct i3c_dev_desc *newdev, *olddev;
2266 	u8 old_dyn_addr = addr, expected_dyn_addr;
2267 	struct i3c_ibi_setup ibireq = { };
2268 	bool enable_ibi = false;
2269 	int ret;
2270 
2271 	if (!master)
2272 		return -EINVAL;
2273 
2274 	newdev = i3c_master_alloc_i3c_dev(master, &info);
2275 	if (IS_ERR(newdev))
2276 		return PTR_ERR(newdev);
2277 
2278 	ret = i3c_master_attach_i3c_dev(master, newdev);
2279 	if (ret)
2280 		goto err_free_dev;
2281 
2282 	ret = i3c_master_retrieve_dev_info(newdev);
2283 	if (ret)
2284 		goto err_detach_dev;
2285 
2286 	i3c_master_attach_boardinfo(newdev);
2287 
2288 	olddev = i3c_master_search_i3c_dev_duplicate(newdev);
2289 	if (olddev) {
2290 		newdev->dev = olddev->dev;
2291 		if (newdev->dev)
2292 			newdev->dev->desc = newdev;
2293 
2294 		/*
2295 		 * We need to restore the IBI state too, so let's save the
2296 		 * IBI information and try to restore them after olddev has
2297 		 * been detached+released and its IBI has been stopped and
2298 		 * the associated resources have been freed.
2299 		 */
2300 		mutex_lock(&olddev->ibi_lock);
2301 		if (olddev->ibi) {
2302 			ibireq.handler = olddev->ibi->handler;
2303 			ibireq.max_payload_len = olddev->ibi->max_payload_len;
2304 			ibireq.num_slots = olddev->ibi->num_slots;
2305 
2306 			if (olddev->ibi->enabled)
2307 				enable_ibi = true;
2308 			/*
2309 			 * The olddev should not receive any commands on the
2310 			 * i3c bus as it does not exist and has been assigned
2311 			 * a new address. This will result in NACK or timeout.
2312 			 * So, update the olddev->ibi->enabled flag to false
2313 			 * to avoid DISEC with OldAddr.
2314 			 */
2315 			olddev->ibi->enabled = false;
2316 			i3c_dev_free_ibi_locked(olddev);
2317 		}
2318 		mutex_unlock(&olddev->ibi_lock);
2319 
2320 		old_dyn_addr = olddev->info.dyn_addr;
2321 
2322 		i3c_master_detach_i3c_dev(olddev);
2323 		i3c_master_free_i3c_dev(olddev);
2324 	}
2325 
2326 	/*
2327 	 * Depending on our previous state, the expected dynamic address might
2328 	 * differ:
2329 	 * - if the device already had a dynamic address assigned, let's try to
2330 	 *   re-apply this one
2331 	 * - if the device did not have a dynamic address and the firmware
2332 	 *   requested a specific address, pick this one
2333 	 * - in any other case, keep the address automatically assigned by the
2334 	 *   master
2335 	 */
2336 	if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr)
2337 		expected_dyn_addr = old_dyn_addr;
2338 	else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr)
2339 		expected_dyn_addr = newdev->boardinfo->init_dyn_addr;
2340 	else
2341 		expected_dyn_addr = newdev->info.dyn_addr;
2342 
2343 	if (newdev->info.dyn_addr != expected_dyn_addr &&
2344 	    i3c_bus_get_addr_slot_status(&master->bus, expected_dyn_addr) == I3C_ADDR_SLOT_FREE) {
2345 		/*
2346 		 * Try to apply the expected dynamic address. If it fails, keep
2347 		 * the address assigned by the master.
2348 		 */
2349 		ret = i3c_master_setnewda_locked(master,
2350 						 newdev->info.dyn_addr,
2351 						 expected_dyn_addr);
2352 		if (!ret) {
2353 			old_dyn_addr = newdev->info.dyn_addr;
2354 			newdev->info.dyn_addr = expected_dyn_addr;
2355 			i3c_master_reattach_i3c_dev(newdev, old_dyn_addr);
2356 		} else {
2357 			dev_err(&master->dev,
2358 				"Failed to assign reserved/old address to device %d%llx",
2359 				master->bus.id, newdev->info.pid);
2360 		}
2361 	}
2362 
2363 	/*
2364 	 * Now is time to try to restore the IBI setup. If we're lucky,
2365 	 * everything works as before, otherwise, all we can do is complain.
2366 	 * FIXME: maybe we should add callback to inform the driver that it
2367 	 * should request the IBI again instead of trying to hide that from
2368 	 * him.
2369 	 */
2370 	if (ibireq.handler) {
2371 		mutex_lock(&newdev->ibi_lock);
2372 		ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
2373 		if (ret) {
2374 			dev_err(&master->dev,
2375 				"Failed to request IBI on device %d-%llx",
2376 				master->bus.id, newdev->info.pid);
2377 		} else if (enable_ibi) {
2378 			ret = i3c_dev_enable_ibi_locked(newdev);
2379 			if (ret)
2380 				dev_err(&master->dev,
2381 					"Failed to re-enable IBI on device %d-%llx",
2382 					master->bus.id, newdev->info.pid);
2383 		}
2384 		mutex_unlock(&newdev->ibi_lock);
2385 	}
2386 
2387 	return 0;
2388 
2389 err_detach_dev:
2390 	if (newdev->dev && newdev->dev->desc)
2391 		newdev->dev->desc = NULL;
2392 
2393 	i3c_master_detach_i3c_dev(newdev);
2394 
2395 err_free_dev:
2396 	i3c_master_free_i3c_dev(newdev);
2397 
2398 	return ret;
2399 }
2400 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked);
2401 
2402 #define OF_I3C_REG1_IS_I2C_DEV			BIT(31)
2403 
2404 static int
2405 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master,
2406 				struct device_node *node, u32 *reg)
2407 {
2408 	struct i2c_dev_boardinfo *boardinfo;
2409 	struct device *dev = &master->dev;
2410 	int ret;
2411 
2412 	boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2413 	if (!boardinfo)
2414 		return -ENOMEM;
2415 
2416 	ret = of_i2c_get_board_info(dev, node, &boardinfo->base);
2417 	if (ret)
2418 		return ret;
2419 
2420 	/*
2421 	 * The I3C Specification does not clearly say I2C devices with 10-bit
2422 	 * address are supported. These devices can't be passed properly through
2423 	 * DEFSLVS command.
2424 	 */
2425 	if (boardinfo->base.flags & I2C_CLIENT_TEN) {
2426 		dev_err(dev, "I2C device with 10 bit address not supported.\n");
2427 		return -EOPNOTSUPP;
2428 	}
2429 
2430 	/* LVR is encoded in reg[2]. */
2431 	boardinfo->lvr = reg[2];
2432 
2433 	list_add_tail(&boardinfo->node, &master->boardinfo.i2c);
2434 	of_node_get(node);
2435 
2436 	return 0;
2437 }
2438 
2439 static int
2440 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
2441 				struct device_node *node, u32 *reg)
2442 {
2443 	struct i3c_dev_boardinfo *boardinfo;
2444 	struct device *dev = &master->dev;
2445 	enum i3c_addr_slot_status addrstatus;
2446 	u32 init_dyn_addr = 0;
2447 
2448 	boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2449 	if (!boardinfo)
2450 		return -ENOMEM;
2451 
2452 	if (reg[0]) {
2453 		if (reg[0] > I3C_MAX_ADDR)
2454 			return -EINVAL;
2455 
2456 		addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2457 							  reg[0]);
2458 		if (addrstatus != I3C_ADDR_SLOT_FREE)
2459 			return -EINVAL;
2460 	}
2461 
2462 	boardinfo->static_addr = reg[0];
2463 
2464 	if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) {
2465 		if (init_dyn_addr > I3C_MAX_ADDR)
2466 			return -EINVAL;
2467 
2468 		addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2469 							  init_dyn_addr);
2470 		if (addrstatus != I3C_ADDR_SLOT_FREE)
2471 			return -EINVAL;
2472 	}
2473 
2474 	boardinfo->pid = ((u64)reg[1] << 32) | reg[2];
2475 
2476 	if ((boardinfo->pid & GENMASK_ULL(63, 48)) ||
2477 	    I3C_PID_RND_LOWER_32BITS(boardinfo->pid))
2478 		return -EINVAL;
2479 
2480 	boardinfo->init_dyn_addr = init_dyn_addr;
2481 	boardinfo->of_node = of_node_get(node);
2482 	list_add_tail(&boardinfo->node, &master->boardinfo.i3c);
2483 
2484 	return 0;
2485 }
2486 
2487 static int of_i3c_master_add_dev(struct i3c_master_controller *master,
2488 				 struct device_node *node)
2489 {
2490 	u32 reg[3];
2491 	int ret;
2492 
2493 	if (!master)
2494 		return -EINVAL;
2495 
2496 	ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg));
2497 	if (ret)
2498 		return ret;
2499 
2500 	/*
2501 	 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2502 	 * dealing with an I2C device.
2503 	 */
2504 	if (!reg[1])
2505 		ret = of_i3c_master_add_i2c_boardinfo(master, node, reg);
2506 	else
2507 		ret = of_i3c_master_add_i3c_boardinfo(master, node, reg);
2508 
2509 	return ret;
2510 }
2511 
2512 static int of_populate_i3c_bus(struct i3c_master_controller *master)
2513 {
2514 	struct device *dev = &master->dev;
2515 	struct device_node *i3cbus_np = dev->of_node;
2516 	int ret;
2517 	u32 val;
2518 
2519 	if (!i3cbus_np)
2520 		return 0;
2521 
2522 	for_each_available_child_of_node_scoped(i3cbus_np, node) {
2523 		ret = of_i3c_master_add_dev(master, node);
2524 		if (ret)
2525 			return ret;
2526 	}
2527 
2528 	/*
2529 	 * The user might want to limit I2C and I3C speed in case some devices
2530 	 * on the bus are not supporting typical rates, or if the bus topology
2531 	 * prevents it from using max possible rate.
2532 	 */
2533 	if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val))
2534 		master->bus.scl_rate.i2c = val;
2535 
2536 	if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val))
2537 		master->bus.scl_rate.i3c = val;
2538 
2539 	return 0;
2540 }
2541 
2542 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap,
2543 				       struct i2c_msg *xfers, int nxfers)
2544 {
2545 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2546 	struct i2c_dev_desc *dev;
2547 	int i, ret;
2548 	u16 addr;
2549 
2550 	if (!xfers || !master || nxfers <= 0)
2551 		return -EINVAL;
2552 
2553 	if (!master->ops->i2c_xfers)
2554 		return -EOPNOTSUPP;
2555 
2556 	/* Doing transfers to different devices is not supported. */
2557 	addr = xfers[0].addr;
2558 	for (i = 1; i < nxfers; i++) {
2559 		if (addr != xfers[i].addr)
2560 			return -EOPNOTSUPP;
2561 	}
2562 
2563 	ret = i3c_master_rpm_get(master);
2564 	if (ret)
2565 		return ret;
2566 
2567 	i3c_bus_normaluse_lock(&master->bus);
2568 	dev = i3c_master_find_i2c_dev_by_addr(master, addr);
2569 	if (!dev)
2570 		ret = -ENOENT;
2571 	else
2572 		ret = master->ops->i2c_xfers(dev, xfers, nxfers);
2573 	i3c_bus_normaluse_unlock(&master->bus);
2574 
2575 	i3c_master_rpm_put(master);
2576 
2577 	return ret ? ret : nxfers;
2578 }
2579 
2580 static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter)
2581 {
2582 	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
2583 }
2584 
2585 static u8 i3c_master_i2c_get_lvr(struct i2c_client *client)
2586 {
2587 	/* Fall back to no spike filters and FM bus mode. */
2588 	u8 lvr = I3C_LVR_I2C_INDEX(2) | I3C_LVR_I2C_FM_MODE;
2589 	u32 reg[3];
2590 
2591 	if (!of_property_read_u32_array(client->dev.of_node, "reg", reg, ARRAY_SIZE(reg)))
2592 		lvr = reg[2];
2593 
2594 	return lvr;
2595 }
2596 
2597 static int i3c_master_i2c_attach(struct i2c_adapter *adap, struct i2c_client *client)
2598 {
2599 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2600 	enum i3c_addr_slot_status status;
2601 	struct i2c_dev_desc *i2cdev;
2602 	int ret;
2603 
2604 	/* Already added by board info? */
2605 	if (i3c_master_find_i2c_dev_by_addr(master, client->addr))
2606 		return 0;
2607 
2608 	status = i3c_bus_get_addr_slot_status(&master->bus, client->addr);
2609 	if (status != I3C_ADDR_SLOT_FREE)
2610 		return -EBUSY;
2611 
2612 	i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2613 				     I3C_ADDR_SLOT_I2C_DEV);
2614 
2615 	i2cdev = i3c_master_alloc_i2c_dev(master, client->addr,
2616 					  i3c_master_i2c_get_lvr(client));
2617 	if (IS_ERR(i2cdev)) {
2618 		ret = PTR_ERR(i2cdev);
2619 		goto out_clear_status;
2620 	}
2621 
2622 	ret = i3c_master_attach_i2c_dev(master, i2cdev);
2623 	if (ret)
2624 		goto out_free_dev;
2625 
2626 	return 0;
2627 
2628 out_free_dev:
2629 	i3c_master_free_i2c_dev(i2cdev);
2630 out_clear_status:
2631 	i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2632 				     I3C_ADDR_SLOT_FREE);
2633 
2634 	return ret;
2635 }
2636 
2637 static int i3c_master_i2c_detach(struct i2c_adapter *adap, struct i2c_client *client)
2638 {
2639 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2640 	struct i2c_dev_desc *dev;
2641 
2642 	dev = i3c_master_find_i2c_dev_by_addr(master, client->addr);
2643 	if (!dev)
2644 		return -ENODEV;
2645 
2646 	i3c_master_detach_i2c_dev(dev);
2647 	i3c_bus_set_addr_slot_status(&master->bus, dev->addr,
2648 				     I3C_ADDR_SLOT_FREE);
2649 	i3c_master_free_i2c_dev(dev);
2650 
2651 	return 0;
2652 }
2653 
2654 static const struct i2c_algorithm i3c_master_i2c_algo = {
2655 	.master_xfer = i3c_master_i2c_adapter_xfer,
2656 	.functionality = i3c_master_i2c_funcs,
2657 };
2658 
2659 static int i3c_i2c_notifier_call(struct notifier_block *nb, unsigned long action,
2660 				 void *data)
2661 {
2662 	struct i2c_adapter *adap;
2663 	struct i2c_client *client;
2664 	struct device *dev = data;
2665 	struct i3c_master_controller *master;
2666 	int ret;
2667 
2668 	if (dev->type != &i2c_client_type)
2669 		return 0;
2670 
2671 	client = to_i2c_client(dev);
2672 	adap = client->adapter;
2673 
2674 	if (adap->algo != &i3c_master_i2c_algo)
2675 		return 0;
2676 
2677 	master = i2c_adapter_to_i3c_master(adap);
2678 
2679 	ret = i3c_master_rpm_get(master);
2680 	if (ret)
2681 		return ret;
2682 
2683 	i3c_bus_maintenance_lock(&master->bus);
2684 	switch (action) {
2685 	case BUS_NOTIFY_ADD_DEVICE:
2686 		ret = i3c_master_i2c_attach(adap, client);
2687 		break;
2688 	case BUS_NOTIFY_DEL_DEVICE:
2689 		ret = i3c_master_i2c_detach(adap, client);
2690 		break;
2691 	default:
2692 		ret = -EINVAL;
2693 	}
2694 	i3c_bus_maintenance_unlock(&master->bus);
2695 
2696 	i3c_master_rpm_put(master);
2697 
2698 	return ret;
2699 }
2700 
2701 static struct notifier_block i2cdev_notifier = {
2702 	.notifier_call = i3c_i2c_notifier_call,
2703 };
2704 
2705 static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master)
2706 {
2707 	struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master);
2708 	struct i2c_dev_desc *i2cdev;
2709 	struct i2c_dev_boardinfo *i2cboardinfo;
2710 	int ret, id;
2711 
2712 	adap->dev.parent = master->dev.parent;
2713 	adap->owner = master->dev.parent->driver->owner;
2714 	adap->algo = &i3c_master_i2c_algo;
2715 	strscpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name));
2716 	adap->timeout = HZ;
2717 	adap->retries = 3;
2718 
2719 	id = of_alias_get_id(master->dev.of_node, "i2c");
2720 	if (id >= 0) {
2721 		adap->nr = id;
2722 		ret = i2c_add_numbered_adapter(adap);
2723 	} else {
2724 		ret = i2c_add_adapter(adap);
2725 	}
2726 	if (ret)
2727 		return ret;
2728 
2729 	/*
2730 	 * We silently ignore failures here. The bus should keep working
2731 	 * correctly even if one or more i2c devices are not registered.
2732 	 */
2733 	list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2734 		i2cdev = i3c_master_find_i2c_dev_by_addr(master,
2735 							 i2cboardinfo->base.addr);
2736 		if (WARN_ON(!i2cdev))
2737 			continue;
2738 		i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base);
2739 	}
2740 
2741 	return 0;
2742 }
2743 
2744 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master)
2745 {
2746 	struct i2c_dev_desc *i2cdev;
2747 
2748 	i2c_del_adapter(&master->i2c);
2749 
2750 	i3c_bus_for_each_i2cdev(&master->bus, i2cdev)
2751 		i2cdev->dev = NULL;
2752 }
2753 
2754 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
2755 {
2756 	struct i3c_dev_desc *i3cdev;
2757 
2758 	i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2759 		if (!i3cdev->dev)
2760 			continue;
2761 
2762 		i3cdev->dev->desc = NULL;
2763 		if (device_is_registered(&i3cdev->dev->dev))
2764 			device_unregister(&i3cdev->dev->dev);
2765 		else
2766 			put_device(&i3cdev->dev->dev);
2767 		i3cdev->dev = NULL;
2768 	}
2769 }
2770 
2771 /**
2772  * i3c_master_queue_ibi() - Queue an IBI
2773  * @dev: the device this IBI is coming from
2774  * @slot: the IBI slot used to store the payload
2775  *
2776  * Queue an IBI to the controller workqueue. The IBI handler attached to
2777  * the dev will be called from a workqueue context.
2778  */
2779 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
2780 {
2781 	if (!dev->ibi || !slot)
2782 		return;
2783 
2784 	atomic_inc(&dev->ibi->pending_ibis);
2785 	queue_work(dev->ibi->wq, &slot->work);
2786 }
2787 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
2788 
2789 static void i3c_master_handle_ibi(struct work_struct *work)
2790 {
2791 	struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
2792 						 work);
2793 	struct i3c_dev_desc *dev = slot->dev;
2794 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
2795 	struct i3c_ibi_payload payload;
2796 
2797 	payload.data = slot->data;
2798 	payload.len = slot->len;
2799 
2800 	if (dev->dev)
2801 		dev->ibi->handler(dev->dev, &payload);
2802 
2803 	master->ops->recycle_ibi_slot(dev, slot);
2804 	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
2805 		complete(&dev->ibi->all_ibis_handled);
2806 }
2807 
2808 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
2809 				     struct i3c_ibi_slot *slot)
2810 {
2811 	slot->dev = dev;
2812 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
2813 }
2814 
2815 struct i3c_generic_ibi_slot {
2816 	struct list_head node;
2817 	struct i3c_ibi_slot base;
2818 };
2819 
2820 struct i3c_generic_ibi_pool {
2821 	spinlock_t lock;
2822 	unsigned int num_slots;
2823 	void *payload_buf;
2824 	struct list_head free_slots;
2825 	struct list_head pending;
2826 	struct i3c_generic_ibi_slot slots[] __counted_by(num_slots);
2827 };
2828 
2829 /**
2830  * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2831  * @pool: the IBI pool to free
2832  *
2833  * Free all IBI slots allated by a generic IBI pool.
2834  */
2835 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
2836 {
2837 	struct i3c_generic_ibi_slot *slot;
2838 	unsigned int nslots = 0;
2839 
2840 	while (!list_empty(&pool->free_slots)) {
2841 		slot = list_first_entry(&pool->free_slots,
2842 					struct i3c_generic_ibi_slot, node);
2843 		list_del(&slot->node);
2844 		nslots++;
2845 	}
2846 
2847 	/*
2848 	 * If the number of freed slots is not equal to the number of allocated
2849 	 * slots we have a leak somewhere.
2850 	 */
2851 	WARN_ON(nslots != pool->num_slots);
2852 
2853 	kfree(pool->payload_buf);
2854 	kfree(pool);
2855 }
2856 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
2857 
2858 /**
2859  * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2860  * @dev: the device this pool will be used for
2861  * @req: IBI setup request describing what the device driver expects
2862  *
2863  * Create a generic IBI pool based on the information provided in @req.
2864  *
2865  * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2866  */
2867 struct i3c_generic_ibi_pool *
2868 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
2869 			   const struct i3c_ibi_setup *req)
2870 {
2871 	struct i3c_generic_ibi_pool *pool;
2872 	struct i3c_generic_ibi_slot *slot;
2873 	unsigned int i;
2874 	int ret;
2875 
2876 	pool = kzalloc_flex(*pool, slots, req->num_slots);
2877 	if (!pool)
2878 		return ERR_PTR(-ENOMEM);
2879 
2880 	pool->num_slots = req->num_slots;
2881 
2882 	spin_lock_init(&pool->lock);
2883 	INIT_LIST_HEAD(&pool->free_slots);
2884 	INIT_LIST_HEAD(&pool->pending);
2885 
2886 	if (req->max_payload_len) {
2887 		pool->payload_buf = kcalloc(req->num_slots,
2888 					    req->max_payload_len, GFP_KERNEL);
2889 		if (!pool->payload_buf) {
2890 			ret = -ENOMEM;
2891 			goto err_free_pool;
2892 		}
2893 	}
2894 
2895 	for (i = 0; i < req->num_slots; i++) {
2896 		slot = &pool->slots[i];
2897 		i3c_master_init_ibi_slot(dev, &slot->base);
2898 
2899 		if (req->max_payload_len)
2900 			slot->base.data = pool->payload_buf +
2901 					  (i * req->max_payload_len);
2902 
2903 		list_add_tail(&slot->node, &pool->free_slots);
2904 	}
2905 
2906 	return pool;
2907 
2908 err_free_pool:
2909 	i3c_generic_ibi_free_pool(pool);
2910 	return ERR_PTR(ret);
2911 }
2912 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
2913 
2914 /**
2915  * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2916  * @pool: the pool to query an IBI slot on
2917  *
2918  * Search for a free slot in a generic IBI pool.
2919  * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2920  * when it's no longer needed.
2921  *
2922  * Return: a pointer to a free slot, or NULL if there's no free slot available.
2923  */
2924 struct i3c_ibi_slot *
2925 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
2926 {
2927 	struct i3c_generic_ibi_slot *slot;
2928 	unsigned long flags;
2929 
2930 	spin_lock_irqsave(&pool->lock, flags);
2931 	slot = list_first_entry_or_null(&pool->free_slots,
2932 					struct i3c_generic_ibi_slot, node);
2933 	if (slot)
2934 		list_del(&slot->node);
2935 	spin_unlock_irqrestore(&pool->lock, flags);
2936 
2937 	return slot ? &slot->base : NULL;
2938 }
2939 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
2940 
2941 /**
2942  * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2943  * @pool: the pool to return the IBI slot to
2944  * @s: IBI slot to recycle
2945  *
2946  * Add an IBI slot back to its generic IBI pool. Should be called from the
2947  * master driver struct_master_controller_ops->recycle_ibi() method.
2948  */
2949 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
2950 				  struct i3c_ibi_slot *s)
2951 {
2952 	struct i3c_generic_ibi_slot *slot;
2953 	unsigned long flags;
2954 
2955 	if (!s)
2956 		return;
2957 
2958 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
2959 	spin_lock_irqsave(&pool->lock, flags);
2960 	list_add_tail(&slot->node, &pool->free_slots);
2961 	spin_unlock_irqrestore(&pool->lock, flags);
2962 }
2963 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
2964 
2965 static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops)
2966 {
2967 	if (!ops || !ops->bus_init || !ops->i3c_xfers ||
2968 	    !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers)
2969 		return -EINVAL;
2970 
2971 	if (ops->request_ibi &&
2972 	    (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi ||
2973 	     !ops->recycle_ibi_slot))
2974 		return -EINVAL;
2975 
2976 	return 0;
2977 }
2978 
2979 /**
2980  * i3c_master_register() - register an I3C master
2981  * @master: master used to send frames on the bus
2982  * @parent: the parent device (the one that provides this I3C master
2983  *	    controller)
2984  * @ops: the master controller operations
2985  * @secondary: true if you are registering a secondary master. Will return
2986  *	       -EOPNOTSUPP if set to true since secondary masters are not yet
2987  *	       supported
2988  *
2989  * This function takes care of everything for you:
2990  *
2991  * - creates and initializes the I3C bus
2992  * - populates the bus with static I2C devs if @parent->of_node is not
2993  *   NULL
2994  * - registers all I3C devices added by the controller during bus
2995  *   initialization
2996  * - registers the I2C adapter and all I2C devices
2997  *
2998  * Return: 0 in case of success, a negative error code otherwise.
2999  */
3000 int i3c_master_register(struct i3c_master_controller *master,
3001 			struct device *parent,
3002 			const struct i3c_master_controller_ops *ops,
3003 			bool secondary)
3004 {
3005 	unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE;
3006 	struct i3c_bus *i3cbus = i3c_master_get_bus(master);
3007 	enum i3c_bus_mode mode = I3C_BUS_MODE_PURE;
3008 	struct i2c_dev_boardinfo *i2cbi;
3009 	int ret;
3010 
3011 	/* We do not support secondary masters yet. */
3012 	if (secondary)
3013 		return -EOPNOTSUPP;
3014 
3015 	ret = i3c_master_check_ops(ops);
3016 	if (ret)
3017 		return ret;
3018 
3019 	master->dev.parent = parent;
3020 	master->dev.of_node = of_node_get(parent->of_node);
3021 	master->dev.bus = &i3c_bus_type;
3022 	master->dev.type = &i3c_masterdev_type;
3023 	master->dev.release = i3c_masterdev_release;
3024 	master->ops = ops;
3025 	master->secondary = secondary;
3026 	INIT_LIST_HEAD(&master->boardinfo.i2c);
3027 	INIT_LIST_HEAD(&master->boardinfo.i3c);
3028 
3029 	ret = i3c_master_rpm_get(master);
3030 	if (ret)
3031 		return ret;
3032 
3033 	device_initialize(&master->dev);
3034 
3035 	master->dev.dma_mask = parent->dma_mask;
3036 	master->dev.coherent_dma_mask = parent->coherent_dma_mask;
3037 	master->dev.dma_parms = parent->dma_parms;
3038 
3039 	ret = i3c_bus_init(i3cbus, master->dev.of_node);
3040 	if (ret)
3041 		goto err_put_dev;
3042 
3043 	dev_set_name(&master->dev, "i3c-%d", i3cbus->id);
3044 
3045 	ret = of_populate_i3c_bus(master);
3046 	if (ret)
3047 		goto err_put_dev;
3048 
3049 	list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) {
3050 		switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) {
3051 		case I3C_LVR_I2C_INDEX(0):
3052 			if (mode < I3C_BUS_MODE_MIXED_FAST)
3053 				mode = I3C_BUS_MODE_MIXED_FAST;
3054 			break;
3055 		case I3C_LVR_I2C_INDEX(1):
3056 			if (mode < I3C_BUS_MODE_MIXED_LIMITED)
3057 				mode = I3C_BUS_MODE_MIXED_LIMITED;
3058 			break;
3059 		case I3C_LVR_I2C_INDEX(2):
3060 			if (mode < I3C_BUS_MODE_MIXED_SLOW)
3061 				mode = I3C_BUS_MODE_MIXED_SLOW;
3062 			break;
3063 		default:
3064 			ret = -EINVAL;
3065 			goto err_put_dev;
3066 		}
3067 
3068 		if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE)
3069 			i2c_scl_rate = I3C_BUS_I2C_FM_SCL_MAX_RATE;
3070 	}
3071 
3072 	ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate);
3073 	if (ret)
3074 		goto err_put_dev;
3075 
3076 	master->wq = alloc_workqueue("%s", WQ_PERCPU, 0, dev_name(parent));
3077 	if (!master->wq) {
3078 		ret = -ENOMEM;
3079 		goto err_put_dev;
3080 	}
3081 
3082 	ret = i3c_master_bus_init(master);
3083 	if (ret)
3084 		goto err_put_dev;
3085 
3086 	ret = device_add(&master->dev);
3087 	if (ret)
3088 		goto err_cleanup_bus;
3089 
3090 	/*
3091 	 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
3092 	 * through the I2C subsystem.
3093 	 */
3094 	ret = i3c_master_i2c_adapter_init(master);
3095 	if (ret)
3096 		goto err_del_dev;
3097 
3098 	i3c_bus_notify(i3cbus, I3C_NOTIFY_BUS_ADD);
3099 
3100 	pm_runtime_no_callbacks(&master->dev);
3101 	pm_suspend_ignore_children(&master->dev, true);
3102 	pm_runtime_enable(&master->dev);
3103 
3104 	/*
3105 	 * We're done initializing the bus and the controller, we can now
3106 	 * register I3C devices discovered during the initial DAA.
3107 	 */
3108 	master->init_done = true;
3109 	i3c_bus_normaluse_lock(&master->bus);
3110 	i3c_master_register_new_i3c_devs(master);
3111 	i3c_bus_normaluse_unlock(&master->bus);
3112 
3113 	if (master->ops->set_dev_nack_retry)
3114 		device_create_file(&master->dev, &dev_attr_dev_nack_retry_count);
3115 
3116 	i3c_master_rpm_put(master);
3117 
3118 	return 0;
3119 
3120 err_del_dev:
3121 	device_del(&master->dev);
3122 
3123 err_cleanup_bus:
3124 	i3c_master_bus_cleanup(master);
3125 
3126 err_put_dev:
3127 	i3c_master_rpm_put(master);
3128 	put_device(&master->dev);
3129 
3130 	return ret;
3131 }
3132 EXPORT_SYMBOL_GPL(i3c_master_register);
3133 
3134 /**
3135  * i3c_master_unregister() - unregister an I3C master
3136  * @master: master used to send frames on the bus
3137  *
3138  * Basically undo everything done in i3c_master_register().
3139  */
3140 void i3c_master_unregister(struct i3c_master_controller *master)
3141 {
3142 	i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE);
3143 
3144 	if (master->ops->set_dev_nack_retry)
3145 		device_remove_file(&master->dev, &dev_attr_dev_nack_retry_count);
3146 
3147 	i3c_master_i2c_adapter_cleanup(master);
3148 	i3c_master_unregister_i3c_devs(master);
3149 	i3c_master_bus_cleanup(master);
3150 	pm_runtime_disable(&master->dev);
3151 	device_unregister(&master->dev);
3152 }
3153 EXPORT_SYMBOL_GPL(i3c_master_unregister);
3154 
3155 int i3c_dev_setdasa_locked(struct i3c_dev_desc *dev)
3156 {
3157 	struct i3c_master_controller *master;
3158 
3159 	if (!dev)
3160 		return -ENOENT;
3161 
3162 	master = i3c_dev_get_master(dev);
3163 	if (!master)
3164 		return -EINVAL;
3165 
3166 	if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr ||
3167 		!dev->boardinfo->static_addr)
3168 		return -EINVAL;
3169 
3170 	return i3c_master_setdasa_locked(master, dev->info.static_addr,
3171 						dev->boardinfo->init_dyn_addr);
3172 }
3173 
3174 int i3c_dev_do_xfers_locked(struct i3c_dev_desc *dev, struct i3c_xfer *xfers,
3175 			    int nxfers, enum i3c_xfer_mode mode)
3176 {
3177 	struct i3c_master_controller *master;
3178 
3179 	if (!dev)
3180 		return -ENOENT;
3181 
3182 	master = i3c_dev_get_master(dev);
3183 	if (!master || !xfers)
3184 		return -EINVAL;
3185 
3186 	if (mode != I3C_SDR && !(master->this->info.hdr_cap & BIT(mode)))
3187 		return -EOPNOTSUPP;
3188 
3189 	return master->ops->i3c_xfers(dev, xfers, nxfers, mode);
3190 }
3191 
3192 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev)
3193 {
3194 	struct i3c_master_controller *master;
3195 	int ret;
3196 
3197 	if (!dev->ibi)
3198 		return -EINVAL;
3199 
3200 	master = i3c_dev_get_master(dev);
3201 	ret = master->ops->disable_ibi(dev);
3202 	if (ret)
3203 		return ret;
3204 
3205 	reinit_completion(&dev->ibi->all_ibis_handled);
3206 	if (atomic_read(&dev->ibi->pending_ibis))
3207 		wait_for_completion(&dev->ibi->all_ibis_handled);
3208 
3209 	dev->ibi->enabled = false;
3210 
3211 	return 0;
3212 }
3213 
3214 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev)
3215 {
3216 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3217 	int ret;
3218 
3219 	if (!dev->ibi)
3220 		return -EINVAL;
3221 
3222 	ret = master->ops->enable_ibi(dev);
3223 	if (!ret)
3224 		dev->ibi->enabled = true;
3225 
3226 	return ret;
3227 }
3228 
3229 int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
3230 			       const struct i3c_ibi_setup *req)
3231 {
3232 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3233 	struct i3c_device_ibi_info *ibi;
3234 	int ret;
3235 
3236 	if (!master->ops->request_ibi)
3237 		return -EOPNOTSUPP;
3238 
3239 	if (dev->ibi)
3240 		return -EBUSY;
3241 
3242 	ibi = kzalloc_obj(*ibi);
3243 	if (!ibi)
3244 		return -ENOMEM;
3245 
3246 	ibi->wq = alloc_ordered_workqueue(dev_name(i3cdev_to_dev(dev->dev)), WQ_MEM_RECLAIM);
3247 	if (!ibi->wq) {
3248 		kfree(ibi);
3249 		return -ENOMEM;
3250 	}
3251 
3252 	atomic_set(&ibi->pending_ibis, 0);
3253 	init_completion(&ibi->all_ibis_handled);
3254 	ibi->handler = req->handler;
3255 	ibi->max_payload_len = req->max_payload_len;
3256 	ibi->num_slots = req->num_slots;
3257 
3258 	dev->ibi = ibi;
3259 	ret = master->ops->request_ibi(dev, req);
3260 	if (ret) {
3261 		kfree(ibi);
3262 		dev->ibi = NULL;
3263 	}
3264 
3265 	return ret;
3266 }
3267 
3268 void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
3269 {
3270 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3271 
3272 	if (!dev->ibi)
3273 		return;
3274 
3275 	if (dev->ibi->enabled) {
3276 		int ret;
3277 
3278 		dev_err(&master->dev, "Freeing IBI that is still enabled\n");
3279 		ret = i3c_master_rpm_get(master);
3280 		if (!ret) {
3281 			ret = i3c_dev_disable_ibi_locked(dev);
3282 			i3c_master_rpm_put(master);
3283 		}
3284 		if (ret)
3285 			dev_err(&master->dev, "Failed to disable IBI before freeing\n");
3286 	}
3287 
3288 	master->ops->free_ibi(dev);
3289 
3290 	if (dev->ibi->wq) {
3291 		destroy_workqueue(dev->ibi->wq);
3292 		dev->ibi->wq = NULL;
3293 	}
3294 
3295 	kfree(dev->ibi);
3296 	dev->ibi = NULL;
3297 }
3298 
3299 static int __init i3c_init(void)
3300 {
3301 	int res;
3302 
3303 	res = of_alias_get_highest_id("i3c");
3304 	if (res >= 0) {
3305 		mutex_lock(&i3c_core_lock);
3306 		__i3c_first_dynamic_bus_num = res + 1;
3307 		mutex_unlock(&i3c_core_lock);
3308 	}
3309 
3310 	res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
3311 	if (res)
3312 		return res;
3313 
3314 	res = bus_register(&i3c_bus_type);
3315 	if (res)
3316 		goto out_unreg_notifier;
3317 
3318 	return 0;
3319 
3320 out_unreg_notifier:
3321 	bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
3322 
3323 	return res;
3324 }
3325 subsys_initcall(i3c_init);
3326 
3327 static void __exit i3c_exit(void)
3328 {
3329 	bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
3330 	idr_destroy(&i3c_bus_idr);
3331 	bus_unregister(&i3c_bus_type);
3332 }
3333 module_exit(i3c_exit);
3334 
3335 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
3336 MODULE_DESCRIPTION("I3C core");
3337 MODULE_LICENSE("GPL v2");
3338