xref: /linux/drivers/i3c/master.c (revision c481ef12e713fb7c292d04f53b3532ac0804ab3d)
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 struct attribute *i3c_masterdev_attrs[] = {
762 	&dev_attr_mode.attr,
763 	&dev_attr_current_master.attr,
764 	&dev_attr_i3c_scl_frequency.attr,
765 	&dev_attr_i2c_scl_frequency.attr,
766 	&dev_attr_bcr.attr,
767 	&dev_attr_dcr.attr,
768 	&dev_attr_pid.attr,
769 	&dev_attr_dynamic_address.attr,
770 	&dev_attr_hdrcap.attr,
771 	&dev_attr_hotjoin.attr,
772 	NULL,
773 };
774 ATTRIBUTE_GROUPS(i3c_masterdev);
775 
776 static void i3c_masterdev_release(struct device *dev)
777 {
778 	struct i3c_master_controller *master = dev_to_i3cmaster(dev);
779 	struct i3c_bus *bus = dev_to_i3cbus(dev);
780 
781 	if (master->wq)
782 		destroy_workqueue(master->wq);
783 
784 	WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c));
785 	i3c_bus_cleanup(bus);
786 
787 	of_node_put(dev->of_node);
788 }
789 
790 static const struct device_type i3c_masterdev_type = {
791 	.groups	= i3c_masterdev_groups,
792 };
793 
794 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode,
795 			    unsigned long max_i2c_scl_rate)
796 {
797 	struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus);
798 
799 	i3cbus->mode = mode;
800 
801 	switch (i3cbus->mode) {
802 	case I3C_BUS_MODE_PURE:
803 		if (!i3cbus->scl_rate.i3c)
804 			i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE;
805 		break;
806 	case I3C_BUS_MODE_MIXED_FAST:
807 	case I3C_BUS_MODE_MIXED_LIMITED:
808 		if (!i3cbus->scl_rate.i3c)
809 			i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE;
810 		if (!i3cbus->scl_rate.i2c)
811 			i3cbus->scl_rate.i2c = max_i2c_scl_rate;
812 		break;
813 	case I3C_BUS_MODE_MIXED_SLOW:
814 		if (!i3cbus->scl_rate.i2c)
815 			i3cbus->scl_rate.i2c = max_i2c_scl_rate;
816 		if (!i3cbus->scl_rate.i3c ||
817 		    i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c)
818 			i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c;
819 		break;
820 	default:
821 		return -EINVAL;
822 	}
823 
824 	dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
825 		i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c);
826 
827 	/*
828 	 * I3C/I2C frequency may have been overridden, check that user-provided
829 	 * values are not exceeding max possible frequency.
830 	 */
831 	if (i3cbus->scl_rate.i3c > I3C_BUS_I3C_SCL_MAX_RATE ||
832 	    i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE)
833 		return -EINVAL;
834 
835 	return 0;
836 }
837 
838 static struct i3c_master_controller *
839 i2c_adapter_to_i3c_master(struct i2c_adapter *adap)
840 {
841 	return container_of(adap, struct i3c_master_controller, i2c);
842 }
843 
844 static struct i2c_adapter *
845 i3c_master_to_i2c_adapter(struct i3c_master_controller *master)
846 {
847 	return &master->i2c;
848 }
849 
850 static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev)
851 {
852 	kfree(dev);
853 }
854 
855 static struct i2c_dev_desc *
856 i3c_master_alloc_i2c_dev(struct i3c_master_controller *master,
857 			 u16 addr, u8 lvr)
858 {
859 	struct i2c_dev_desc *dev;
860 
861 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
862 	if (!dev)
863 		return ERR_PTR(-ENOMEM);
864 
865 	dev->common.master = master;
866 	dev->addr = addr;
867 	dev->lvr = lvr;
868 
869 	return dev;
870 }
871 
872 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr,
873 				   u16 payloadlen)
874 {
875 	dest->addr = addr;
876 	dest->payload.len = payloadlen;
877 	if (payloadlen)
878 		dest->payload.data = kzalloc(payloadlen, GFP_KERNEL);
879 	else
880 		dest->payload.data = NULL;
881 
882 	return dest->payload.data;
883 }
884 
885 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest)
886 {
887 	kfree(dest->payload.data);
888 }
889 
890 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id,
891 			     struct i3c_ccc_cmd_dest *dests,
892 			     unsigned int ndests)
893 {
894 	cmd->rnw = rnw ? 1 : 0;
895 	cmd->id = id;
896 	cmd->dests = dests;
897 	cmd->ndests = ndests;
898 	cmd->err = I3C_ERROR_UNKNOWN;
899 }
900 
901 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master,
902 					  struct i3c_ccc_cmd *cmd)
903 {
904 	int ret;
905 
906 	if (!cmd || !master)
907 		return -EINVAL;
908 
909 	if (WARN_ON(master->init_done &&
910 		    !rwsem_is_locked(&master->bus.lock)))
911 		return -EINVAL;
912 
913 	if (!master->ops->send_ccc_cmd)
914 		return -EOPNOTSUPP;
915 
916 	if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests))
917 		return -EINVAL;
918 
919 	if (master->ops->supports_ccc_cmd &&
920 	    !master->ops->supports_ccc_cmd(master, cmd))
921 		return -EOPNOTSUPP;
922 
923 	ret = master->ops->send_ccc_cmd(master, cmd);
924 	if (ret) {
925 		if (cmd->err != I3C_ERROR_UNKNOWN)
926 			return cmd->err;
927 
928 		return ret;
929 	}
930 
931 	return 0;
932 }
933 
934 static struct i2c_dev_desc *
935 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master,
936 				u16 addr)
937 {
938 	struct i2c_dev_desc *dev;
939 
940 	i3c_bus_for_each_i2cdev(&master->bus, dev) {
941 		if (dev->addr == addr)
942 			return dev;
943 	}
944 
945 	return NULL;
946 }
947 
948 /**
949  * i3c_master_get_free_addr() - get a free address on the bus
950  * @master: I3C master object
951  * @start_addr: where to start searching
952  *
953  * This function must be called with the bus lock held in write mode.
954  *
955  * Return: the first free address starting at @start_addr (included) or -ENOMEM
956  * if there's no more address available.
957  */
958 int i3c_master_get_free_addr(struct i3c_master_controller *master,
959 			     u8 start_addr)
960 {
961 	return i3c_bus_get_free_addr(&master->bus, start_addr);
962 }
963 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr);
964 
965 static void i3c_device_release(struct device *dev)
966 {
967 	struct i3c_device *i3cdev = dev_to_i3cdev(dev);
968 
969 	WARN_ON(i3cdev->desc);
970 
971 	of_node_put(i3cdev->dev.of_node);
972 	kfree(i3cdev);
973 }
974 
975 static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev)
976 {
977 	kfree(dev);
978 }
979 
980 static struct i3c_dev_desc *
981 i3c_master_alloc_i3c_dev(struct i3c_master_controller *master,
982 			 const struct i3c_device_info *info)
983 {
984 	struct i3c_dev_desc *dev;
985 
986 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
987 	if (!dev)
988 		return ERR_PTR(-ENOMEM);
989 
990 	dev->common.master = master;
991 	dev->info = *info;
992 	mutex_init(&dev->ibi_lock);
993 
994 	return dev;
995 }
996 
997 static int i3c_master_rstdaa_locked(struct i3c_master_controller *master,
998 				    u8 addr)
999 {
1000 	enum i3c_addr_slot_status addrstat;
1001 	struct i3c_ccc_cmd_dest dest;
1002 	struct i3c_ccc_cmd cmd;
1003 	int ret;
1004 
1005 	if (!master)
1006 		return -EINVAL;
1007 
1008 	addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr);
1009 	if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV)
1010 		return -EINVAL;
1011 
1012 	i3c_ccc_cmd_dest_init(&dest, addr, 0);
1013 	i3c_ccc_cmd_init(&cmd, false,
1014 			 I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR),
1015 			 &dest, 1);
1016 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1017 	i3c_ccc_cmd_dest_cleanup(&dest);
1018 
1019 	return ret;
1020 }
1021 
1022 /**
1023  * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
1024  *				procedure
1025  * @master: master used to send frames on the bus
1026  *
1027  * Send a ENTDAA CCC command to start a DAA procedure.
1028  *
1029  * Note that this function only sends the ENTDAA CCC command, all the logic
1030  * behind dynamic address assignment has to be handled in the I3C master
1031  * driver.
1032  *
1033  * This function must be called with the bus lock held in write mode.
1034  *
1035  * Return: 0 in case of success, a positive I3C error code if the error is
1036  * one of the official Mx error codes, and a negative error code otherwise.
1037  */
1038 int i3c_master_entdaa_locked(struct i3c_master_controller *master)
1039 {
1040 	struct i3c_ccc_cmd_dest dest;
1041 	struct i3c_ccc_cmd cmd;
1042 	int ret;
1043 
1044 	i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0);
1045 	i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1);
1046 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1047 	i3c_ccc_cmd_dest_cleanup(&dest);
1048 
1049 	return ret;
1050 }
1051 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked);
1052 
1053 static int i3c_master_enec_disec_locked(struct i3c_master_controller *master,
1054 					u8 addr, bool enable, u8 evts)
1055 {
1056 	struct i3c_ccc_events *events;
1057 	struct i3c_ccc_cmd_dest dest;
1058 	struct i3c_ccc_cmd cmd;
1059 	int ret;
1060 
1061 	events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events));
1062 	if (!events)
1063 		return -ENOMEM;
1064 
1065 	events->events = evts;
1066 	i3c_ccc_cmd_init(&cmd, false,
1067 			 enable ?
1068 			 I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) :
1069 			 I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR),
1070 			 &dest, 1);
1071 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1072 	i3c_ccc_cmd_dest_cleanup(&dest);
1073 
1074 	return ret;
1075 }
1076 
1077 /**
1078  * i3c_master_disec_locked() - send a DISEC CCC command
1079  * @master: master used to send frames on the bus
1080  * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1081  * @evts: events to disable
1082  *
1083  * Send a DISEC CCC command to disable some or all events coming from a
1084  * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
1085  *
1086  * This function must be called with the bus lock held in write mode.
1087  *
1088  * Return: 0 in case of success, a positive I3C error code if the error is
1089  * one of the official Mx error codes, and a negative error code otherwise.
1090  */
1091 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
1092 			    u8 evts)
1093 {
1094 	return i3c_master_enec_disec_locked(master, addr, false, evts);
1095 }
1096 EXPORT_SYMBOL_GPL(i3c_master_disec_locked);
1097 
1098 /**
1099  * i3c_master_enec_locked() - send an ENEC CCC command
1100  * @master: master used to send frames on the bus
1101  * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1102  * @evts: events to disable
1103  *
1104  * Sends an ENEC CCC command to enable some or all events coming from a
1105  * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
1106  *
1107  * This function must be called with the bus lock held in write mode.
1108  *
1109  * Return: 0 in case of success, a positive I3C error code if the error is
1110  * one of the official Mx error codes, and a negative error code otherwise.
1111  */
1112 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
1113 			   u8 evts)
1114 {
1115 	return i3c_master_enec_disec_locked(master, addr, true, evts);
1116 }
1117 EXPORT_SYMBOL_GPL(i3c_master_enec_locked);
1118 
1119 /**
1120  * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
1121  * @master: master used to send frames on the bus
1122  *
1123  * Send a DEFSLVS CCC command containing all the devices known to the @master.
1124  * This is useful when you have secondary masters on the bus to propagate
1125  * device information.
1126  *
1127  * This should be called after all I3C devices have been discovered (in other
1128  * words, after the DAA procedure has finished) and instantiated in
1129  * &i3c_master_controller_ops->bus_init().
1130  * It should also be called if a master ACKed an Hot-Join request and assigned
1131  * a dynamic address to the device joining the bus.
1132  *
1133  * This function must be called with the bus lock held in write mode.
1134  *
1135  * Return: 0 in case of success, a positive I3C error code if the error is
1136  * one of the official Mx error codes, and a negative error code otherwise.
1137  */
1138 int i3c_master_defslvs_locked(struct i3c_master_controller *master)
1139 {
1140 	struct i3c_ccc_defslvs *defslvs;
1141 	struct i3c_ccc_dev_desc *desc;
1142 	struct i3c_ccc_cmd_dest dest;
1143 	struct i3c_dev_desc *i3cdev;
1144 	struct i2c_dev_desc *i2cdev;
1145 	struct i3c_ccc_cmd cmd;
1146 	struct i3c_bus *bus;
1147 	bool send = false;
1148 	int ndevs = 0, ret;
1149 
1150 	if (!master)
1151 		return -EINVAL;
1152 
1153 	bus = i3c_master_get_bus(master);
1154 	i3c_bus_for_each_i3cdev(bus, i3cdev) {
1155 		ndevs++;
1156 
1157 		if (i3cdev == master->this)
1158 			continue;
1159 
1160 		if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) ==
1161 		    I3C_BCR_I3C_MASTER)
1162 			send = true;
1163 	}
1164 
1165 	/* No other master on the bus, skip DEFSLVS. */
1166 	if (!send)
1167 		return 0;
1168 
1169 	i3c_bus_for_each_i2cdev(bus, i2cdev)
1170 		ndevs++;
1171 
1172 	defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR,
1173 					struct_size(defslvs, slaves,
1174 						    ndevs - 1));
1175 	if (!defslvs)
1176 		return -ENOMEM;
1177 
1178 	defslvs->count = ndevs;
1179 	defslvs->master.bcr = master->this->info.bcr;
1180 	defslvs->master.dcr = master->this->info.dcr;
1181 	defslvs->master.dyn_addr = master->this->info.dyn_addr << 1;
1182 	defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1;
1183 
1184 	desc = defslvs->slaves;
1185 	i3c_bus_for_each_i2cdev(bus, i2cdev) {
1186 		desc->lvr = i2cdev->lvr;
1187 		desc->static_addr = i2cdev->addr << 1;
1188 		desc++;
1189 	}
1190 
1191 	i3c_bus_for_each_i3cdev(bus, i3cdev) {
1192 		/* Skip the I3C dev representing this master. */
1193 		if (i3cdev == master->this)
1194 			continue;
1195 
1196 		desc->bcr = i3cdev->info.bcr;
1197 		desc->dcr = i3cdev->info.dcr;
1198 		desc->dyn_addr = i3cdev->info.dyn_addr << 1;
1199 		desc->static_addr = i3cdev->info.static_addr << 1;
1200 		desc++;
1201 	}
1202 
1203 	i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1);
1204 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1205 	i3c_ccc_cmd_dest_cleanup(&dest);
1206 
1207 	return ret;
1208 }
1209 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked);
1210 
1211 static int i3c_master_setda_locked(struct i3c_master_controller *master,
1212 				   u8 oldaddr, u8 newaddr, bool setdasa)
1213 {
1214 	struct i3c_ccc_cmd_dest dest;
1215 	struct i3c_ccc_setda *setda;
1216 	struct i3c_ccc_cmd cmd;
1217 	int ret;
1218 
1219 	if (!oldaddr || !newaddr)
1220 		return -EINVAL;
1221 
1222 	setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda));
1223 	if (!setda)
1224 		return -ENOMEM;
1225 
1226 	setda->addr = newaddr << 1;
1227 	i3c_ccc_cmd_init(&cmd, false,
1228 			 setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA,
1229 			 &dest, 1);
1230 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1231 	i3c_ccc_cmd_dest_cleanup(&dest);
1232 
1233 	return ret;
1234 }
1235 
1236 static int i3c_master_setdasa_locked(struct i3c_master_controller *master,
1237 				     u8 static_addr, u8 dyn_addr)
1238 {
1239 	return i3c_master_setda_locked(master, static_addr, dyn_addr, true);
1240 }
1241 
1242 static int i3c_master_setnewda_locked(struct i3c_master_controller *master,
1243 				      u8 oldaddr, u8 newaddr)
1244 {
1245 	return i3c_master_setda_locked(master, oldaddr, newaddr, false);
1246 }
1247 
1248 static int i3c_master_getmrl_locked(struct i3c_master_controller *master,
1249 				    struct i3c_device_info *info)
1250 {
1251 	struct i3c_ccc_cmd_dest dest;
1252 	struct i3c_ccc_mrl *mrl;
1253 	struct i3c_ccc_cmd cmd;
1254 	int ret;
1255 
1256 	mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl));
1257 	if (!mrl)
1258 		return -ENOMEM;
1259 
1260 	/*
1261 	 * When the device does not have IBI payload GETMRL only returns 2
1262 	 * bytes of data.
1263 	 */
1264 	if (!(info->bcr & I3C_BCR_IBI_PAYLOAD))
1265 		dest.payload.len -= 1;
1266 
1267 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1);
1268 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1269 	if (ret)
1270 		goto out;
1271 
1272 	switch (dest.payload.len) {
1273 	case 3:
1274 		info->max_ibi_len = mrl->ibi_len;
1275 		fallthrough;
1276 	case 2:
1277 		info->max_read_len = be16_to_cpu(mrl->read_len);
1278 		break;
1279 	default:
1280 		ret = -EIO;
1281 		goto out;
1282 	}
1283 
1284 out:
1285 	i3c_ccc_cmd_dest_cleanup(&dest);
1286 
1287 	return ret;
1288 }
1289 
1290 static int i3c_master_getmwl_locked(struct i3c_master_controller *master,
1291 				    struct i3c_device_info *info)
1292 {
1293 	struct i3c_ccc_cmd_dest dest;
1294 	struct i3c_ccc_mwl *mwl;
1295 	struct i3c_ccc_cmd cmd;
1296 	int ret;
1297 
1298 	mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl));
1299 	if (!mwl)
1300 		return -ENOMEM;
1301 
1302 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1);
1303 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1304 	if (ret)
1305 		goto out;
1306 
1307 	if (dest.payload.len != sizeof(*mwl)) {
1308 		ret = -EIO;
1309 		goto out;
1310 	}
1311 
1312 	info->max_write_len = be16_to_cpu(mwl->len);
1313 
1314 out:
1315 	i3c_ccc_cmd_dest_cleanup(&dest);
1316 
1317 	return ret;
1318 }
1319 
1320 static int i3c_master_getmxds_locked(struct i3c_master_controller *master,
1321 				     struct i3c_device_info *info)
1322 {
1323 	struct i3c_ccc_getmxds *getmaxds;
1324 	struct i3c_ccc_cmd_dest dest;
1325 	struct i3c_ccc_cmd cmd;
1326 	int ret;
1327 
1328 	getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1329 					 sizeof(*getmaxds));
1330 	if (!getmaxds)
1331 		return -ENOMEM;
1332 
1333 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1);
1334 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1335 	if (ret) {
1336 		/*
1337 		 * Retry when the device does not support max read turnaround
1338 		 * while expecting shorter length from this CCC command.
1339 		 */
1340 		dest.payload.len -= 3;
1341 		ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1342 		if (ret)
1343 			goto out;
1344 	}
1345 
1346 	if (dest.payload.len != 2 && dest.payload.len != 5) {
1347 		ret = -EIO;
1348 		goto out;
1349 	}
1350 
1351 	info->max_read_ds = getmaxds->maxrd;
1352 	info->max_write_ds = getmaxds->maxwr;
1353 	if (dest.payload.len == 5)
1354 		info->max_read_turnaround = getmaxds->maxrdturn[0] |
1355 					    ((u32)getmaxds->maxrdturn[1] << 8) |
1356 					    ((u32)getmaxds->maxrdturn[2] << 16);
1357 
1358 out:
1359 	i3c_ccc_cmd_dest_cleanup(&dest);
1360 
1361 	return ret;
1362 }
1363 
1364 static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master,
1365 				       struct i3c_device_info *info)
1366 {
1367 	struct i3c_ccc_gethdrcap *gethdrcap;
1368 	struct i3c_ccc_cmd_dest dest;
1369 	struct i3c_ccc_cmd cmd;
1370 	int ret;
1371 
1372 	gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1373 					  sizeof(*gethdrcap));
1374 	if (!gethdrcap)
1375 		return -ENOMEM;
1376 
1377 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1);
1378 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1379 	if (ret)
1380 		goto out;
1381 
1382 	if (dest.payload.len != 1) {
1383 		ret = -EIO;
1384 		goto out;
1385 	}
1386 
1387 	info->hdr_cap = gethdrcap->modes;
1388 
1389 out:
1390 	i3c_ccc_cmd_dest_cleanup(&dest);
1391 
1392 	return ret;
1393 }
1394 
1395 static int i3c_master_getpid_locked(struct i3c_master_controller *master,
1396 				    struct i3c_device_info *info)
1397 {
1398 	struct i3c_ccc_getpid *getpid;
1399 	struct i3c_ccc_cmd_dest dest;
1400 	struct i3c_ccc_cmd cmd;
1401 	int ret, i;
1402 
1403 	getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid));
1404 	if (!getpid)
1405 		return -ENOMEM;
1406 
1407 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1);
1408 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1409 	if (ret)
1410 		goto out;
1411 
1412 	info->pid = 0;
1413 	for (i = 0; i < sizeof(getpid->pid); i++) {
1414 		int sft = (sizeof(getpid->pid) - i - 1) * 8;
1415 
1416 		info->pid |= (u64)getpid->pid[i] << sft;
1417 	}
1418 
1419 out:
1420 	i3c_ccc_cmd_dest_cleanup(&dest);
1421 
1422 	return ret;
1423 }
1424 
1425 static int i3c_master_getbcr_locked(struct i3c_master_controller *master,
1426 				    struct i3c_device_info *info)
1427 {
1428 	struct i3c_ccc_getbcr *getbcr;
1429 	struct i3c_ccc_cmd_dest dest;
1430 	struct i3c_ccc_cmd cmd;
1431 	int ret;
1432 
1433 	getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr));
1434 	if (!getbcr)
1435 		return -ENOMEM;
1436 
1437 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1);
1438 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1439 	if (ret)
1440 		goto out;
1441 
1442 	info->bcr = getbcr->bcr;
1443 
1444 out:
1445 	i3c_ccc_cmd_dest_cleanup(&dest);
1446 
1447 	return ret;
1448 }
1449 
1450 static int i3c_master_getdcr_locked(struct i3c_master_controller *master,
1451 				    struct i3c_device_info *info)
1452 {
1453 	struct i3c_ccc_getdcr *getdcr;
1454 	struct i3c_ccc_cmd_dest dest;
1455 	struct i3c_ccc_cmd cmd;
1456 	int ret;
1457 
1458 	getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr));
1459 	if (!getdcr)
1460 		return -ENOMEM;
1461 
1462 	i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1);
1463 	ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1464 	if (ret)
1465 		goto out;
1466 
1467 	info->dcr = getdcr->dcr;
1468 
1469 out:
1470 	i3c_ccc_cmd_dest_cleanup(&dest);
1471 
1472 	return ret;
1473 }
1474 
1475 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev)
1476 {
1477 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1478 	enum i3c_addr_slot_status slot_status;
1479 	int ret;
1480 
1481 	if (!dev->info.dyn_addr)
1482 		return -EINVAL;
1483 
1484 	slot_status = i3c_bus_get_addr_slot_status(&master->bus,
1485 						   dev->info.dyn_addr);
1486 	if (slot_status == I3C_ADDR_SLOT_RSVD ||
1487 	    slot_status == I3C_ADDR_SLOT_I2C_DEV)
1488 		return -EINVAL;
1489 
1490 	ret = i3c_master_getpid_locked(master, &dev->info);
1491 	if (ret)
1492 		return ret;
1493 
1494 	ret = i3c_master_getbcr_locked(master, &dev->info);
1495 	if (ret)
1496 		return ret;
1497 
1498 	ret = i3c_master_getdcr_locked(master, &dev->info);
1499 	if (ret)
1500 		return ret;
1501 
1502 	if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) {
1503 		ret = i3c_master_getmxds_locked(master, &dev->info);
1504 		if (ret)
1505 			return ret;
1506 	}
1507 
1508 	if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD)
1509 		dev->info.max_ibi_len = 1;
1510 
1511 	i3c_master_getmrl_locked(master, &dev->info);
1512 	i3c_master_getmwl_locked(master, &dev->info);
1513 
1514 	if (dev->info.bcr & I3C_BCR_HDR_CAP) {
1515 		ret = i3c_master_gethdrcap_locked(master, &dev->info);
1516 		if (ret && ret != -EOPNOTSUPP)
1517 			return ret;
1518 	}
1519 
1520 	return 0;
1521 }
1522 
1523 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev)
1524 {
1525 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1526 
1527 	if (dev->info.static_addr)
1528 		i3c_bus_set_addr_slot_status(&master->bus,
1529 					     dev->info.static_addr,
1530 					     I3C_ADDR_SLOT_FREE);
1531 
1532 	if (dev->info.dyn_addr)
1533 		i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1534 					     I3C_ADDR_SLOT_FREE);
1535 
1536 	if (dev->boardinfo && dev->boardinfo->init_dyn_addr)
1537 		i3c_bus_set_addr_slot_status(&master->bus, dev->boardinfo->init_dyn_addr,
1538 					     I3C_ADDR_SLOT_FREE);
1539 }
1540 
1541 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev)
1542 {
1543 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1544 	enum i3c_addr_slot_status status;
1545 
1546 	if (!dev->info.static_addr && !dev->info.dyn_addr)
1547 		return 0;
1548 
1549 	if (dev->info.static_addr) {
1550 		status = i3c_bus_get_addr_slot_status(&master->bus,
1551 						      dev->info.static_addr);
1552 		/* Since static address and assigned dynamic address can be
1553 		 * equal, allow this case to pass.
1554 		 */
1555 		if (status != I3C_ADDR_SLOT_FREE &&
1556 		    dev->info.static_addr != dev->boardinfo->init_dyn_addr)
1557 			return -EBUSY;
1558 
1559 		i3c_bus_set_addr_slot_status(&master->bus,
1560 					     dev->info.static_addr,
1561 					     I3C_ADDR_SLOT_I3C_DEV);
1562 	}
1563 
1564 	/*
1565 	 * ->init_dyn_addr should have been reserved before that, so, if we're
1566 	 * trying to apply a pre-reserved dynamic address, we should not try
1567 	 * to reserve the address slot a second time.
1568 	 */
1569 	if (dev->info.dyn_addr &&
1570 	    (!dev->boardinfo ||
1571 	     dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) {
1572 		status = i3c_bus_get_addr_slot_status(&master->bus,
1573 						      dev->info.dyn_addr);
1574 		if (status != I3C_ADDR_SLOT_FREE)
1575 			goto err_release_static_addr;
1576 
1577 		i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1578 					     I3C_ADDR_SLOT_I3C_DEV);
1579 	}
1580 
1581 	return 0;
1582 
1583 err_release_static_addr:
1584 	if (dev->info.static_addr)
1585 		i3c_bus_set_addr_slot_status(&master->bus,
1586 					     dev->info.static_addr,
1587 					     I3C_ADDR_SLOT_FREE);
1588 
1589 	return -EBUSY;
1590 }
1591 
1592 static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master,
1593 				     struct i3c_dev_desc *dev)
1594 {
1595 	int ret;
1596 
1597 	/*
1598 	 * We don't attach devices to the controller until they are
1599 	 * addressable on the bus.
1600 	 */
1601 	if (!dev->info.static_addr && !dev->info.dyn_addr)
1602 		return 0;
1603 
1604 	ret = i3c_master_get_i3c_addrs(dev);
1605 	if (ret)
1606 		return ret;
1607 
1608 	/* Do not attach the master device itself. */
1609 	if (master->this != dev && master->ops->attach_i3c_dev) {
1610 		ret = master->ops->attach_i3c_dev(dev);
1611 		if (ret) {
1612 			i3c_master_put_i3c_addrs(dev);
1613 			return ret;
1614 		}
1615 	}
1616 
1617 	list_add_tail(&dev->common.node, &master->bus.devs.i3c);
1618 
1619 	return 0;
1620 }
1621 
1622 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
1623 				       u8 old_dyn_addr)
1624 {
1625 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1626 	int ret;
1627 
1628 	if (dev->info.dyn_addr != old_dyn_addr) {
1629 		i3c_bus_set_addr_slot_status(&master->bus,
1630 					     dev->info.dyn_addr,
1631 					     I3C_ADDR_SLOT_I3C_DEV);
1632 		if (old_dyn_addr)
1633 			i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr,
1634 						     I3C_ADDR_SLOT_FREE);
1635 	}
1636 
1637 	if (master->ops->reattach_i3c_dev) {
1638 		ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr);
1639 		if (ret) {
1640 			i3c_master_put_i3c_addrs(dev);
1641 			return ret;
1642 		}
1643 	}
1644 
1645 	return 0;
1646 }
1647 
1648 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
1649 {
1650 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
1651 
1652 	/* Do not detach the master device itself. */
1653 	if (master->this != dev && master->ops->detach_i3c_dev)
1654 		master->ops->detach_i3c_dev(dev);
1655 
1656 	i3c_master_put_i3c_addrs(dev);
1657 	list_del(&dev->common.node);
1658 }
1659 
1660 static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master,
1661 				     struct i2c_dev_desc *dev)
1662 {
1663 	int ret;
1664 
1665 	if (master->ops->attach_i2c_dev) {
1666 		ret = master->ops->attach_i2c_dev(dev);
1667 		if (ret)
1668 			return ret;
1669 	}
1670 
1671 	list_add_tail(&dev->common.node, &master->bus.devs.i2c);
1672 
1673 	return 0;
1674 }
1675 
1676 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
1677 {
1678 	struct i3c_master_controller *master = i2c_dev_get_master(dev);
1679 
1680 	list_del(&dev->common.node);
1681 
1682 	if (master->ops->detach_i2c_dev)
1683 		master->ops->detach_i2c_dev(dev);
1684 }
1685 
1686 static int i3c_master_early_i3c_dev_add(struct i3c_master_controller *master,
1687 					  struct i3c_dev_boardinfo *boardinfo)
1688 {
1689 	struct i3c_device_info info = {
1690 		.static_addr = boardinfo->static_addr,
1691 		.pid = boardinfo->pid,
1692 	};
1693 	struct i3c_dev_desc *i3cdev;
1694 	int ret;
1695 
1696 	i3cdev = i3c_master_alloc_i3c_dev(master, &info);
1697 	if (IS_ERR(i3cdev))
1698 		return -ENOMEM;
1699 
1700 	i3cdev->boardinfo = boardinfo;
1701 
1702 	ret = i3c_master_attach_i3c_dev(master, i3cdev);
1703 	if (ret)
1704 		goto err_free_dev;
1705 
1706 	ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr,
1707 					i3cdev->boardinfo->init_dyn_addr);
1708 	if (ret)
1709 		goto err_detach_dev;
1710 
1711 	i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr;
1712 	ret = i3c_master_reattach_i3c_dev(i3cdev, 0);
1713 	if (ret)
1714 		goto err_rstdaa;
1715 
1716 	ret = i3c_master_retrieve_dev_info(i3cdev);
1717 	if (ret)
1718 		goto err_rstdaa;
1719 
1720 	return 0;
1721 
1722 err_rstdaa:
1723 	i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr);
1724 err_detach_dev:
1725 	i3c_master_detach_i3c_dev(i3cdev);
1726 err_free_dev:
1727 	i3c_master_free_i3c_dev(i3cdev);
1728 
1729 	return ret;
1730 }
1731 
1732 static void
1733 i3c_master_register_new_i3c_devs(struct i3c_master_controller *master)
1734 {
1735 	struct i3c_dev_desc *desc;
1736 	int ret;
1737 
1738 	if (!master->init_done)
1739 		return;
1740 
1741 	i3c_bus_for_each_i3cdev(&master->bus, desc) {
1742 		if (desc->dev || !desc->info.dyn_addr || desc == master->this)
1743 			continue;
1744 
1745 		desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL);
1746 		if (!desc->dev)
1747 			continue;
1748 
1749 		desc->dev->bus = &master->bus;
1750 		desc->dev->desc = desc;
1751 		desc->dev->dev.parent = &master->dev;
1752 		desc->dev->dev.type = &i3c_device_type;
1753 		desc->dev->dev.bus = &i3c_bus_type;
1754 		desc->dev->dev.release = i3c_device_release;
1755 		dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id,
1756 			     desc->info.pid);
1757 
1758 		if (desc->boardinfo)
1759 			desc->dev->dev.of_node = desc->boardinfo->of_node;
1760 
1761 		ret = device_register(&desc->dev->dev);
1762 		if (ret) {
1763 			dev_err(&master->dev,
1764 				"Failed to add I3C device (err = %d)\n", ret);
1765 			put_device(&desc->dev->dev);
1766 		}
1767 	}
1768 }
1769 
1770 /**
1771  * i3c_master_do_daa_ext() - Dynamic Address Assignment (extended version)
1772  * @master: controller
1773  * @rstdaa: whether to first perform Reset of Dynamic Addresses (RSTDAA)
1774  *
1775  * Perform Dynamic Address Assignment with optional support for System
1776  * Hibernation (@rstdaa is true).
1777  *
1778  * After System Hibernation, Dynamic Addresses can have been reassigned at boot
1779  * time to different values. A simple strategy is followed to handle that.
1780  * Perform a Reset of Dynamic Addresses (RSTDAA) followed by the normal DAA
1781  * procedure which has provision for reassigning addresses that differ from the
1782  * previously recorded addresses.
1783  *
1784  * Return: a 0 in case of success, an negative error code otherwise.
1785  */
1786 int i3c_master_do_daa_ext(struct i3c_master_controller *master, bool rstdaa)
1787 {
1788 	int rstret = 0;
1789 	int ret;
1790 
1791 	ret = i3c_master_rpm_get(master);
1792 	if (ret)
1793 		return ret;
1794 
1795 	i3c_bus_maintenance_lock(&master->bus);
1796 
1797 	if (rstdaa) {
1798 		rstret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1799 		if (rstret == I3C_ERROR_M2)
1800 			rstret = 0;
1801 	}
1802 
1803 	ret = master->ops->do_daa(master);
1804 
1805 	i3c_bus_maintenance_unlock(&master->bus);
1806 
1807 	if (ret)
1808 		goto out;
1809 
1810 	i3c_bus_normaluse_lock(&master->bus);
1811 	i3c_master_register_new_i3c_devs(master);
1812 	i3c_bus_normaluse_unlock(&master->bus);
1813 out:
1814 	i3c_master_rpm_put(master);
1815 
1816 	return rstret ?: ret;
1817 }
1818 EXPORT_SYMBOL_GPL(i3c_master_do_daa_ext);
1819 
1820 /**
1821  * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1822  * @master: master doing the DAA
1823  *
1824  * This function instantiates I3C device objects and adds them to the
1825  * I3C device list. All device information is automatically retrieved using
1826  * standard CCC commands.
1827  *
1828  * Return: a 0 in case of success, an negative error code otherwise.
1829  */
1830 int i3c_master_do_daa(struct i3c_master_controller *master)
1831 {
1832 	return i3c_master_do_daa_ext(master, false);
1833 }
1834 EXPORT_SYMBOL_GPL(i3c_master_do_daa);
1835 
1836 /**
1837  * i3c_master_dma_map_single() - Map buffer for single DMA transfer
1838  * @dev: device object of a device doing DMA
1839  * @buf: destination/source buffer for DMA
1840  * @len: length of transfer
1841  * @force_bounce: true, force to use a bounce buffer,
1842  *                false, function will auto check is a bounce buffer required
1843  * @dir: DMA direction
1844  *
1845  * Map buffer for a DMA transfer and allocate a bounce buffer if required.
1846  *
1847  * Return: I3C DMA transfer descriptor or NULL in case of error.
1848  */
1849 struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *buf,
1850 	size_t len, bool force_bounce, enum dma_data_direction dir)
1851 {
1852 	void *bounce __free(kfree) = NULL;
1853 	void *dma_buf = buf;
1854 
1855 	struct i3c_dma *dma_xfer __free(kfree) = kzalloc(sizeof(*dma_xfer), GFP_KERNEL);
1856 	if (!dma_xfer)
1857 		return NULL;
1858 
1859 	dma_xfer->dev = dev;
1860 	dma_xfer->buf = buf;
1861 	dma_xfer->dir = dir;
1862 	dma_xfer->len = len;
1863 	dma_xfer->map_len = len;
1864 
1865 	if (is_vmalloc_addr(buf))
1866 		force_bounce = true;
1867 
1868 	if (force_bounce) {
1869 		dma_xfer->map_len = ALIGN(len, cache_line_size());
1870 		if (dir == DMA_FROM_DEVICE)
1871 			bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL);
1872 		else
1873 			bounce = kmemdup(buf, dma_xfer->map_len, GFP_KERNEL);
1874 		if (!bounce)
1875 			return NULL;
1876 		dma_buf = bounce;
1877 	}
1878 
1879 	dma_xfer->addr = dma_map_single(dev, dma_buf, dma_xfer->map_len, dir);
1880 	if (dma_mapping_error(dev, dma_xfer->addr))
1881 		return NULL;
1882 
1883 	dma_xfer->bounce_buf = no_free_ptr(bounce);
1884 	return no_free_ptr(dma_xfer);
1885 }
1886 EXPORT_SYMBOL_GPL(i3c_master_dma_map_single);
1887 
1888 /**
1889  * i3c_master_dma_unmap_single() - Unmap buffer after DMA
1890  * @dma_xfer: DMA transfer and mapping descriptor
1891  *
1892  * Unmap buffer and cleanup DMA transfer descriptor.
1893  */
1894 void i3c_master_dma_unmap_single(struct i3c_dma *dma_xfer)
1895 {
1896 	dma_unmap_single(dma_xfer->dev, dma_xfer->addr,
1897 			 dma_xfer->map_len, dma_xfer->dir);
1898 	if (dma_xfer->bounce_buf) {
1899 		if (dma_xfer->dir == DMA_FROM_DEVICE)
1900 			memcpy(dma_xfer->buf, dma_xfer->bounce_buf,
1901 			       dma_xfer->len);
1902 		kfree(dma_xfer->bounce_buf);
1903 	}
1904 	kfree(dma_xfer);
1905 }
1906 EXPORT_SYMBOL_GPL(i3c_master_dma_unmap_single);
1907 
1908 /**
1909  * i3c_master_set_info() - set master device information
1910  * @master: master used to send frames on the bus
1911  * @info: I3C device information
1912  *
1913  * Set master device info. This should be called from
1914  * &i3c_master_controller_ops->bus_init().
1915  *
1916  * Not all &i3c_device_info fields are meaningful for a master device.
1917  * Here is a list of fields that should be properly filled:
1918  *
1919  * - &i3c_device_info->dyn_addr
1920  * - &i3c_device_info->bcr
1921  * - &i3c_device_info->dcr
1922  * - &i3c_device_info->pid
1923  * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1924  *   &i3c_device_info->bcr
1925  *
1926  * This function must be called with the bus lock held in maintenance mode.
1927  *
1928  * Return: 0 if @info contains valid information (not every piece of
1929  * information can be checked, but we can at least make sure @info->dyn_addr
1930  * and @info->bcr are correct), -EINVAL otherwise.
1931  */
1932 int i3c_master_set_info(struct i3c_master_controller *master,
1933 			const struct i3c_device_info *info)
1934 {
1935 	struct i3c_dev_desc *i3cdev;
1936 	int ret;
1937 
1938 	if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr))
1939 		return -EINVAL;
1940 
1941 	if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER &&
1942 	    master->secondary)
1943 		return -EINVAL;
1944 
1945 	if (master->this)
1946 		return -EINVAL;
1947 
1948 	i3cdev = i3c_master_alloc_i3c_dev(master, info);
1949 	if (IS_ERR(i3cdev))
1950 		return PTR_ERR(i3cdev);
1951 
1952 	master->this = i3cdev;
1953 	master->bus.cur_master = master->this;
1954 
1955 	ret = i3c_master_attach_i3c_dev(master, i3cdev);
1956 	if (ret)
1957 		goto err_free_dev;
1958 
1959 	return 0;
1960 
1961 err_free_dev:
1962 	i3c_master_free_i3c_dev(i3cdev);
1963 
1964 	return ret;
1965 }
1966 EXPORT_SYMBOL_GPL(i3c_master_set_info);
1967 
1968 static void i3c_master_detach_free_devs(struct i3c_master_controller *master)
1969 {
1970 	struct i3c_dev_desc *i3cdev, *i3ctmp;
1971 	struct i2c_dev_desc *i2cdev, *i2ctmp;
1972 
1973 	list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c,
1974 				 common.node) {
1975 		i3c_master_detach_i3c_dev(i3cdev);
1976 
1977 		if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr)
1978 			i3c_bus_set_addr_slot_status(&master->bus,
1979 					i3cdev->boardinfo->init_dyn_addr,
1980 					I3C_ADDR_SLOT_FREE);
1981 
1982 		i3c_master_free_i3c_dev(i3cdev);
1983 	}
1984 
1985 	list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c,
1986 				 common.node) {
1987 		i3c_master_detach_i2c_dev(i2cdev);
1988 		i3c_bus_set_addr_slot_status(&master->bus,
1989 					     i2cdev->addr,
1990 					     I3C_ADDR_SLOT_FREE);
1991 		i3c_master_free_i2c_dev(i2cdev);
1992 	}
1993 }
1994 
1995 /**
1996  * i3c_master_bus_init() - initialize an I3C bus
1997  * @master: main master initializing the bus
1998  *
1999  * This function is following all initialisation steps described in the I3C
2000  * specification:
2001  *
2002  * 1. Attach I2C devs to the master so that the master can fill its internal
2003  *    device table appropriately
2004  *
2005  * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
2006  *    the master controller. That's usually where the bus mode is selected
2007  *    (pure bus or mixed fast/slow bus)
2008  *
2009  * 3. Instruct all devices on the bus to drop their dynamic address. This is
2010  *    particularly important when the bus was previously configured by someone
2011  *    else (for example the bootloader)
2012  *
2013  * 4. Disable all slave events.
2014  *
2015  * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices
2016  *    also have static_addr, try to pre-assign dynamic addresses requested by
2017  *    the FW with SETDASA and attach corresponding statically defined I3C
2018  *    devices to the master.
2019  *
2020  * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
2021  *    remaining I3C devices
2022  *
2023  * Once this is done, all I3C and I2C devices should be usable.
2024  *
2025  * Return: a 0 in case of success, an negative error code otherwise.
2026  */
2027 static int i3c_master_bus_init(struct i3c_master_controller *master)
2028 {
2029 	enum i3c_addr_slot_status status;
2030 	struct i2c_dev_boardinfo *i2cboardinfo;
2031 	struct i3c_dev_boardinfo *i3cboardinfo;
2032 	struct i2c_dev_desc *i2cdev;
2033 	int ret;
2034 
2035 	/*
2036 	 * First attach all devices with static definitions provided by the
2037 	 * FW.
2038 	 */
2039 	list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2040 		status = i3c_bus_get_addr_slot_status(&master->bus,
2041 						      i2cboardinfo->base.addr);
2042 		if (status != I3C_ADDR_SLOT_FREE) {
2043 			ret = -EBUSY;
2044 			goto err_detach_devs;
2045 		}
2046 
2047 		i3c_bus_set_addr_slot_status(&master->bus,
2048 					     i2cboardinfo->base.addr,
2049 					     I3C_ADDR_SLOT_I2C_DEV);
2050 
2051 		i2cdev = i3c_master_alloc_i2c_dev(master,
2052 						  i2cboardinfo->base.addr,
2053 						  i2cboardinfo->lvr);
2054 		if (IS_ERR(i2cdev)) {
2055 			ret = PTR_ERR(i2cdev);
2056 			goto err_detach_devs;
2057 		}
2058 
2059 		ret = i3c_master_attach_i2c_dev(master, i2cdev);
2060 		if (ret) {
2061 			i3c_master_free_i2c_dev(i2cdev);
2062 			goto err_detach_devs;
2063 		}
2064 	}
2065 
2066 	/*
2067 	 * Now execute the controller specific ->bus_init() routine, which
2068 	 * might configure its internal logic to match the bus limitations.
2069 	 */
2070 	ret = master->ops->bus_init(master);
2071 	if (ret)
2072 		goto err_detach_devs;
2073 
2074 	/*
2075 	 * The master device should have been instantiated in ->bus_init(),
2076 	 * complain if this was not the case.
2077 	 */
2078 	if (!master->this) {
2079 		dev_err(&master->dev,
2080 			"master_set_info() was not called in ->bus_init()\n");
2081 		ret = -EINVAL;
2082 		goto err_bus_cleanup;
2083 	}
2084 
2085 	if (master->ops->set_speed) {
2086 		ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_SLOW_SPEED);
2087 		if (ret)
2088 			goto err_bus_cleanup;
2089 	}
2090 
2091 	/*
2092 	 * Reset all dynamic address that may have been assigned before
2093 	 * (assigned by the bootloader for example).
2094 	 */
2095 	ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
2096 	if (ret && ret != I3C_ERROR_M2)
2097 		goto err_bus_cleanup;
2098 
2099 	if (master->ops->set_speed) {
2100 		ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_NORMAL_SPEED);
2101 		if (ret)
2102 			goto err_bus_cleanup;
2103 	}
2104 
2105 	/* Disable all slave events before starting DAA. */
2106 	ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR,
2107 				      I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR |
2108 				      I3C_CCC_EVENT_HJ);
2109 	if (ret && ret != I3C_ERROR_M2)
2110 		goto err_bus_cleanup;
2111 
2112 	/*
2113 	 * Reserve init_dyn_addr first, and then try to pre-assign dynamic
2114 	 * address and retrieve device information if needed.
2115 	 * In case pre-assign dynamic address fails, setting dynamic address to
2116 	 * the requested init_dyn_addr is retried after DAA is done in
2117 	 * i3c_master_add_i3c_dev_locked().
2118 	 */
2119 	list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
2120 
2121 		/*
2122 		 * We don't reserve a dynamic address for devices that
2123 		 * don't explicitly request one.
2124 		 */
2125 		if (!i3cboardinfo->init_dyn_addr)
2126 			continue;
2127 
2128 		ret = i3c_bus_get_addr_slot_status(&master->bus,
2129 						   i3cboardinfo->init_dyn_addr);
2130 		if (ret != I3C_ADDR_SLOT_FREE) {
2131 			ret = -EBUSY;
2132 			goto err_rstdaa;
2133 		}
2134 
2135 		/* Do not mark as occupied until real device exist in bus */
2136 		i3c_bus_set_addr_slot_status_mask(&master->bus,
2137 						  i3cboardinfo->init_dyn_addr,
2138 						  I3C_ADDR_SLOT_EXT_DESIRED,
2139 						  I3C_ADDR_SLOT_EXT_STATUS_MASK);
2140 
2141 		/*
2142 		 * Only try to create/attach devices that have a static
2143 		 * address. Other devices will be created/attached when
2144 		 * DAA happens, and the requested dynamic address will
2145 		 * be set using SETNEWDA once those devices become
2146 		 * addressable.
2147 		 */
2148 
2149 		if (i3cboardinfo->static_addr)
2150 			i3c_master_early_i3c_dev_add(master, i3cboardinfo);
2151 	}
2152 
2153 	ret = i3c_master_do_daa(master);
2154 	if (ret)
2155 		goto err_rstdaa;
2156 
2157 	return 0;
2158 
2159 err_rstdaa:
2160 	i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
2161 
2162 err_bus_cleanup:
2163 	if (master->ops->bus_cleanup)
2164 		master->ops->bus_cleanup(master);
2165 
2166 err_detach_devs:
2167 	i3c_master_detach_free_devs(master);
2168 
2169 	return ret;
2170 }
2171 
2172 static void i3c_master_bus_cleanup(struct i3c_master_controller *master)
2173 {
2174 	if (master->ops->bus_cleanup) {
2175 		int ret = i3c_master_rpm_get(master);
2176 
2177 		if (ret) {
2178 			dev_err(&master->dev,
2179 				"runtime resume error: master bus_cleanup() not done\n");
2180 		} else {
2181 			master->ops->bus_cleanup(master);
2182 			i3c_master_rpm_put(master);
2183 		}
2184 	}
2185 
2186 	i3c_master_detach_free_devs(master);
2187 }
2188 
2189 static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev)
2190 {
2191 	struct i3c_master_controller *master = i3cdev->common.master;
2192 	struct i3c_dev_boardinfo *i3cboardinfo;
2193 
2194 	list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
2195 		if (i3cdev->info.pid != i3cboardinfo->pid)
2196 			continue;
2197 
2198 		i3cdev->boardinfo = i3cboardinfo;
2199 		i3cdev->info.static_addr = i3cboardinfo->static_addr;
2200 		return;
2201 	}
2202 }
2203 
2204 static struct i3c_dev_desc *
2205 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev)
2206 {
2207 	struct i3c_master_controller *master = i3c_dev_get_master(refdev);
2208 	struct i3c_dev_desc *i3cdev;
2209 
2210 	i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2211 		if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid)
2212 			return i3cdev;
2213 	}
2214 
2215 	return NULL;
2216 }
2217 
2218 /**
2219  * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
2220  * @master: master used to send frames on the bus
2221  * @addr: I3C slave dynamic address assigned to the device
2222  *
2223  * This function is instantiating an I3C device object and adding it to the
2224  * I3C device list. All device information are automatically retrieved using
2225  * standard CCC commands.
2226  *
2227  * The I3C device object is returned in case the master wants to attach
2228  * private data to it using i3c_dev_set_master_data().
2229  *
2230  * This function must be called with the bus lock held in write mode.
2231  *
2232  * Return: a 0 in case of success, an negative error code otherwise.
2233  */
2234 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
2235 				  u8 addr)
2236 {
2237 	struct i3c_device_info info = { .dyn_addr = addr };
2238 	struct i3c_dev_desc *newdev, *olddev;
2239 	u8 old_dyn_addr = addr, expected_dyn_addr;
2240 	struct i3c_ibi_setup ibireq = { };
2241 	bool enable_ibi = false;
2242 	int ret;
2243 
2244 	if (!master)
2245 		return -EINVAL;
2246 
2247 	newdev = i3c_master_alloc_i3c_dev(master, &info);
2248 	if (IS_ERR(newdev))
2249 		return PTR_ERR(newdev);
2250 
2251 	ret = i3c_master_attach_i3c_dev(master, newdev);
2252 	if (ret)
2253 		goto err_free_dev;
2254 
2255 	ret = i3c_master_retrieve_dev_info(newdev);
2256 	if (ret)
2257 		goto err_detach_dev;
2258 
2259 	i3c_master_attach_boardinfo(newdev);
2260 
2261 	olddev = i3c_master_search_i3c_dev_duplicate(newdev);
2262 	if (olddev) {
2263 		newdev->dev = olddev->dev;
2264 		if (newdev->dev)
2265 			newdev->dev->desc = newdev;
2266 
2267 		/*
2268 		 * We need to restore the IBI state too, so let's save the
2269 		 * IBI information and try to restore them after olddev has
2270 		 * been detached+released and its IBI has been stopped and
2271 		 * the associated resources have been freed.
2272 		 */
2273 		mutex_lock(&olddev->ibi_lock);
2274 		if (olddev->ibi) {
2275 			ibireq.handler = olddev->ibi->handler;
2276 			ibireq.max_payload_len = olddev->ibi->max_payload_len;
2277 			ibireq.num_slots = olddev->ibi->num_slots;
2278 
2279 			if (olddev->ibi->enabled)
2280 				enable_ibi = true;
2281 			/*
2282 			 * The olddev should not receive any commands on the
2283 			 * i3c bus as it does not exist and has been assigned
2284 			 * a new address. This will result in NACK or timeout.
2285 			 * So, update the olddev->ibi->enabled flag to false
2286 			 * to avoid DISEC with OldAddr.
2287 			 */
2288 			olddev->ibi->enabled = false;
2289 			i3c_dev_free_ibi_locked(olddev);
2290 		}
2291 		mutex_unlock(&olddev->ibi_lock);
2292 
2293 		old_dyn_addr = olddev->info.dyn_addr;
2294 
2295 		i3c_master_detach_i3c_dev(olddev);
2296 		i3c_master_free_i3c_dev(olddev);
2297 	}
2298 
2299 	/*
2300 	 * Depending on our previous state, the expected dynamic address might
2301 	 * differ:
2302 	 * - if the device already had a dynamic address assigned, let's try to
2303 	 *   re-apply this one
2304 	 * - if the device did not have a dynamic address and the firmware
2305 	 *   requested a specific address, pick this one
2306 	 * - in any other case, keep the address automatically assigned by the
2307 	 *   master
2308 	 */
2309 	if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr)
2310 		expected_dyn_addr = old_dyn_addr;
2311 	else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr)
2312 		expected_dyn_addr = newdev->boardinfo->init_dyn_addr;
2313 	else
2314 		expected_dyn_addr = newdev->info.dyn_addr;
2315 
2316 	if (newdev->info.dyn_addr != expected_dyn_addr &&
2317 	    i3c_bus_get_addr_slot_status(&master->bus, expected_dyn_addr) == I3C_ADDR_SLOT_FREE) {
2318 		/*
2319 		 * Try to apply the expected dynamic address. If it fails, keep
2320 		 * the address assigned by the master.
2321 		 */
2322 		ret = i3c_master_setnewda_locked(master,
2323 						 newdev->info.dyn_addr,
2324 						 expected_dyn_addr);
2325 		if (!ret) {
2326 			old_dyn_addr = newdev->info.dyn_addr;
2327 			newdev->info.dyn_addr = expected_dyn_addr;
2328 			i3c_master_reattach_i3c_dev(newdev, old_dyn_addr);
2329 		} else {
2330 			dev_err(&master->dev,
2331 				"Failed to assign reserved/old address to device %d%llx",
2332 				master->bus.id, newdev->info.pid);
2333 		}
2334 	}
2335 
2336 	/*
2337 	 * Now is time to try to restore the IBI setup. If we're lucky,
2338 	 * everything works as before, otherwise, all we can do is complain.
2339 	 * FIXME: maybe we should add callback to inform the driver that it
2340 	 * should request the IBI again instead of trying to hide that from
2341 	 * him.
2342 	 */
2343 	if (ibireq.handler) {
2344 		mutex_lock(&newdev->ibi_lock);
2345 		ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
2346 		if (ret) {
2347 			dev_err(&master->dev,
2348 				"Failed to request IBI on device %d-%llx",
2349 				master->bus.id, newdev->info.pid);
2350 		} else if (enable_ibi) {
2351 			ret = i3c_dev_enable_ibi_locked(newdev);
2352 			if (ret)
2353 				dev_err(&master->dev,
2354 					"Failed to re-enable IBI on device %d-%llx",
2355 					master->bus.id, newdev->info.pid);
2356 		}
2357 		mutex_unlock(&newdev->ibi_lock);
2358 	}
2359 
2360 	return 0;
2361 
2362 err_detach_dev:
2363 	if (newdev->dev && newdev->dev->desc)
2364 		newdev->dev->desc = NULL;
2365 
2366 	i3c_master_detach_i3c_dev(newdev);
2367 
2368 err_free_dev:
2369 	i3c_master_free_i3c_dev(newdev);
2370 
2371 	return ret;
2372 }
2373 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked);
2374 
2375 #define OF_I3C_REG1_IS_I2C_DEV			BIT(31)
2376 
2377 static int
2378 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master,
2379 				struct device_node *node, u32 *reg)
2380 {
2381 	struct i2c_dev_boardinfo *boardinfo;
2382 	struct device *dev = &master->dev;
2383 	int ret;
2384 
2385 	boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2386 	if (!boardinfo)
2387 		return -ENOMEM;
2388 
2389 	ret = of_i2c_get_board_info(dev, node, &boardinfo->base);
2390 	if (ret)
2391 		return ret;
2392 
2393 	/*
2394 	 * The I3C Specification does not clearly say I2C devices with 10-bit
2395 	 * address are supported. These devices can't be passed properly through
2396 	 * DEFSLVS command.
2397 	 */
2398 	if (boardinfo->base.flags & I2C_CLIENT_TEN) {
2399 		dev_err(dev, "I2C device with 10 bit address not supported.");
2400 		return -EOPNOTSUPP;
2401 	}
2402 
2403 	/* LVR is encoded in reg[2]. */
2404 	boardinfo->lvr = reg[2];
2405 
2406 	list_add_tail(&boardinfo->node, &master->boardinfo.i2c);
2407 	of_node_get(node);
2408 
2409 	return 0;
2410 }
2411 
2412 static int
2413 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
2414 				struct device_node *node, u32 *reg)
2415 {
2416 	struct i3c_dev_boardinfo *boardinfo;
2417 	struct device *dev = &master->dev;
2418 	enum i3c_addr_slot_status addrstatus;
2419 	u32 init_dyn_addr = 0;
2420 
2421 	boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2422 	if (!boardinfo)
2423 		return -ENOMEM;
2424 
2425 	if (reg[0]) {
2426 		if (reg[0] > I3C_MAX_ADDR)
2427 			return -EINVAL;
2428 
2429 		addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2430 							  reg[0]);
2431 		if (addrstatus != I3C_ADDR_SLOT_FREE)
2432 			return -EINVAL;
2433 	}
2434 
2435 	boardinfo->static_addr = reg[0];
2436 
2437 	if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) {
2438 		if (init_dyn_addr > I3C_MAX_ADDR)
2439 			return -EINVAL;
2440 
2441 		addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2442 							  init_dyn_addr);
2443 		if (addrstatus != I3C_ADDR_SLOT_FREE)
2444 			return -EINVAL;
2445 	}
2446 
2447 	boardinfo->pid = ((u64)reg[1] << 32) | reg[2];
2448 
2449 	if ((boardinfo->pid & GENMASK_ULL(63, 48)) ||
2450 	    I3C_PID_RND_LOWER_32BITS(boardinfo->pid))
2451 		return -EINVAL;
2452 
2453 	boardinfo->init_dyn_addr = init_dyn_addr;
2454 	boardinfo->of_node = of_node_get(node);
2455 	list_add_tail(&boardinfo->node, &master->boardinfo.i3c);
2456 
2457 	return 0;
2458 }
2459 
2460 static int of_i3c_master_add_dev(struct i3c_master_controller *master,
2461 				 struct device_node *node)
2462 {
2463 	u32 reg[3];
2464 	int ret;
2465 
2466 	if (!master)
2467 		return -EINVAL;
2468 
2469 	ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg));
2470 	if (ret)
2471 		return ret;
2472 
2473 	/*
2474 	 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2475 	 * dealing with an I2C device.
2476 	 */
2477 	if (!reg[1])
2478 		ret = of_i3c_master_add_i2c_boardinfo(master, node, reg);
2479 	else
2480 		ret = of_i3c_master_add_i3c_boardinfo(master, node, reg);
2481 
2482 	return ret;
2483 }
2484 
2485 static int of_populate_i3c_bus(struct i3c_master_controller *master)
2486 {
2487 	struct device *dev = &master->dev;
2488 	struct device_node *i3cbus_np = dev->of_node;
2489 	int ret;
2490 	u32 val;
2491 
2492 	if (!i3cbus_np)
2493 		return 0;
2494 
2495 	for_each_available_child_of_node_scoped(i3cbus_np, node) {
2496 		ret = of_i3c_master_add_dev(master, node);
2497 		if (ret)
2498 			return ret;
2499 	}
2500 
2501 	/*
2502 	 * The user might want to limit I2C and I3C speed in case some devices
2503 	 * on the bus are not supporting typical rates, or if the bus topology
2504 	 * prevents it from using max possible rate.
2505 	 */
2506 	if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val))
2507 		master->bus.scl_rate.i2c = val;
2508 
2509 	if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val))
2510 		master->bus.scl_rate.i3c = val;
2511 
2512 	return 0;
2513 }
2514 
2515 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap,
2516 				       struct i2c_msg *xfers, int nxfers)
2517 {
2518 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2519 	struct i2c_dev_desc *dev;
2520 	int i, ret;
2521 	u16 addr;
2522 
2523 	if (!xfers || !master || nxfers <= 0)
2524 		return -EINVAL;
2525 
2526 	if (!master->ops->i2c_xfers)
2527 		return -EOPNOTSUPP;
2528 
2529 	/* Doing transfers to different devices is not supported. */
2530 	addr = xfers[0].addr;
2531 	for (i = 1; i < nxfers; i++) {
2532 		if (addr != xfers[i].addr)
2533 			return -EOPNOTSUPP;
2534 	}
2535 
2536 	ret = i3c_master_rpm_get(master);
2537 	if (ret)
2538 		return ret;
2539 
2540 	i3c_bus_normaluse_lock(&master->bus);
2541 	dev = i3c_master_find_i2c_dev_by_addr(master, addr);
2542 	if (!dev)
2543 		ret = -ENOENT;
2544 	else
2545 		ret = master->ops->i2c_xfers(dev, xfers, nxfers);
2546 	i3c_bus_normaluse_unlock(&master->bus);
2547 
2548 	i3c_master_rpm_put(master);
2549 
2550 	return ret ? ret : nxfers;
2551 }
2552 
2553 static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter)
2554 {
2555 	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
2556 }
2557 
2558 static u8 i3c_master_i2c_get_lvr(struct i2c_client *client)
2559 {
2560 	/* Fall back to no spike filters and FM bus mode. */
2561 	u8 lvr = I3C_LVR_I2C_INDEX(2) | I3C_LVR_I2C_FM_MODE;
2562 	u32 reg[3];
2563 
2564 	if (!of_property_read_u32_array(client->dev.of_node, "reg", reg, ARRAY_SIZE(reg)))
2565 		lvr = reg[2];
2566 
2567 	return lvr;
2568 }
2569 
2570 static int i3c_master_i2c_attach(struct i2c_adapter *adap, struct i2c_client *client)
2571 {
2572 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2573 	enum i3c_addr_slot_status status;
2574 	struct i2c_dev_desc *i2cdev;
2575 	int ret;
2576 
2577 	/* Already added by board info? */
2578 	if (i3c_master_find_i2c_dev_by_addr(master, client->addr))
2579 		return 0;
2580 
2581 	status = i3c_bus_get_addr_slot_status(&master->bus, client->addr);
2582 	if (status != I3C_ADDR_SLOT_FREE)
2583 		return -EBUSY;
2584 
2585 	i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2586 				     I3C_ADDR_SLOT_I2C_DEV);
2587 
2588 	i2cdev = i3c_master_alloc_i2c_dev(master, client->addr,
2589 					  i3c_master_i2c_get_lvr(client));
2590 	if (IS_ERR(i2cdev)) {
2591 		ret = PTR_ERR(i2cdev);
2592 		goto out_clear_status;
2593 	}
2594 
2595 	ret = i3c_master_attach_i2c_dev(master, i2cdev);
2596 	if (ret)
2597 		goto out_free_dev;
2598 
2599 	return 0;
2600 
2601 out_free_dev:
2602 	i3c_master_free_i2c_dev(i2cdev);
2603 out_clear_status:
2604 	i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2605 				     I3C_ADDR_SLOT_FREE);
2606 
2607 	return ret;
2608 }
2609 
2610 static int i3c_master_i2c_detach(struct i2c_adapter *adap, struct i2c_client *client)
2611 {
2612 	struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2613 	struct i2c_dev_desc *dev;
2614 
2615 	dev = i3c_master_find_i2c_dev_by_addr(master, client->addr);
2616 	if (!dev)
2617 		return -ENODEV;
2618 
2619 	i3c_master_detach_i2c_dev(dev);
2620 	i3c_bus_set_addr_slot_status(&master->bus, dev->addr,
2621 				     I3C_ADDR_SLOT_FREE);
2622 	i3c_master_free_i2c_dev(dev);
2623 
2624 	return 0;
2625 }
2626 
2627 static const struct i2c_algorithm i3c_master_i2c_algo = {
2628 	.master_xfer = i3c_master_i2c_adapter_xfer,
2629 	.functionality = i3c_master_i2c_funcs,
2630 };
2631 
2632 static int i3c_i2c_notifier_call(struct notifier_block *nb, unsigned long action,
2633 				 void *data)
2634 {
2635 	struct i2c_adapter *adap;
2636 	struct i2c_client *client;
2637 	struct device *dev = data;
2638 	struct i3c_master_controller *master;
2639 	int ret;
2640 
2641 	if (dev->type != &i2c_client_type)
2642 		return 0;
2643 
2644 	client = to_i2c_client(dev);
2645 	adap = client->adapter;
2646 
2647 	if (adap->algo != &i3c_master_i2c_algo)
2648 		return 0;
2649 
2650 	master = i2c_adapter_to_i3c_master(adap);
2651 
2652 	ret = i3c_master_rpm_get(master);
2653 	if (ret)
2654 		return ret;
2655 
2656 	i3c_bus_maintenance_lock(&master->bus);
2657 	switch (action) {
2658 	case BUS_NOTIFY_ADD_DEVICE:
2659 		ret = i3c_master_i2c_attach(adap, client);
2660 		break;
2661 	case BUS_NOTIFY_DEL_DEVICE:
2662 		ret = i3c_master_i2c_detach(adap, client);
2663 		break;
2664 	default:
2665 		ret = -EINVAL;
2666 	}
2667 	i3c_bus_maintenance_unlock(&master->bus);
2668 
2669 	i3c_master_rpm_put(master);
2670 
2671 	return ret;
2672 }
2673 
2674 static struct notifier_block i2cdev_notifier = {
2675 	.notifier_call = i3c_i2c_notifier_call,
2676 };
2677 
2678 static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master)
2679 {
2680 	struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master);
2681 	struct i2c_dev_desc *i2cdev;
2682 	struct i2c_dev_boardinfo *i2cboardinfo;
2683 	int ret, id;
2684 
2685 	adap->dev.parent = master->dev.parent;
2686 	adap->owner = master->dev.parent->driver->owner;
2687 	adap->algo = &i3c_master_i2c_algo;
2688 	strscpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name));
2689 	adap->timeout = HZ;
2690 	adap->retries = 3;
2691 
2692 	id = of_alias_get_id(master->dev.of_node, "i2c");
2693 	if (id >= 0) {
2694 		adap->nr = id;
2695 		ret = i2c_add_numbered_adapter(adap);
2696 	} else {
2697 		ret = i2c_add_adapter(adap);
2698 	}
2699 	if (ret)
2700 		return ret;
2701 
2702 	/*
2703 	 * We silently ignore failures here. The bus should keep working
2704 	 * correctly even if one or more i2c devices are not registered.
2705 	 */
2706 	list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2707 		i2cdev = i3c_master_find_i2c_dev_by_addr(master,
2708 							 i2cboardinfo->base.addr);
2709 		if (WARN_ON(!i2cdev))
2710 			continue;
2711 		i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base);
2712 	}
2713 
2714 	return 0;
2715 }
2716 
2717 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master)
2718 {
2719 	struct i2c_dev_desc *i2cdev;
2720 
2721 	i2c_del_adapter(&master->i2c);
2722 
2723 	i3c_bus_for_each_i2cdev(&master->bus, i2cdev)
2724 		i2cdev->dev = NULL;
2725 }
2726 
2727 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
2728 {
2729 	struct i3c_dev_desc *i3cdev;
2730 
2731 	i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2732 		if (!i3cdev->dev)
2733 			continue;
2734 
2735 		i3cdev->dev->desc = NULL;
2736 		if (device_is_registered(&i3cdev->dev->dev))
2737 			device_unregister(&i3cdev->dev->dev);
2738 		else
2739 			put_device(&i3cdev->dev->dev);
2740 		i3cdev->dev = NULL;
2741 	}
2742 }
2743 
2744 /**
2745  * i3c_master_queue_ibi() - Queue an IBI
2746  * @dev: the device this IBI is coming from
2747  * @slot: the IBI slot used to store the payload
2748  *
2749  * Queue an IBI to the controller workqueue. The IBI handler attached to
2750  * the dev will be called from a workqueue context.
2751  */
2752 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
2753 {
2754 	if (!dev->ibi || !slot)
2755 		return;
2756 
2757 	atomic_inc(&dev->ibi->pending_ibis);
2758 	queue_work(dev->ibi->wq, &slot->work);
2759 }
2760 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
2761 
2762 static void i3c_master_handle_ibi(struct work_struct *work)
2763 {
2764 	struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
2765 						 work);
2766 	struct i3c_dev_desc *dev = slot->dev;
2767 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
2768 	struct i3c_ibi_payload payload;
2769 
2770 	payload.data = slot->data;
2771 	payload.len = slot->len;
2772 
2773 	if (dev->dev)
2774 		dev->ibi->handler(dev->dev, &payload);
2775 
2776 	master->ops->recycle_ibi_slot(dev, slot);
2777 	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
2778 		complete(&dev->ibi->all_ibis_handled);
2779 }
2780 
2781 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
2782 				     struct i3c_ibi_slot *slot)
2783 {
2784 	slot->dev = dev;
2785 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
2786 }
2787 
2788 struct i3c_generic_ibi_slot {
2789 	struct list_head node;
2790 	struct i3c_ibi_slot base;
2791 };
2792 
2793 struct i3c_generic_ibi_pool {
2794 	spinlock_t lock;
2795 	unsigned int num_slots;
2796 	struct i3c_generic_ibi_slot *slots;
2797 	void *payload_buf;
2798 	struct list_head free_slots;
2799 	struct list_head pending;
2800 };
2801 
2802 /**
2803  * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2804  * @pool: the IBI pool to free
2805  *
2806  * Free all IBI slots allated by a generic IBI pool.
2807  */
2808 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
2809 {
2810 	struct i3c_generic_ibi_slot *slot;
2811 	unsigned int nslots = 0;
2812 
2813 	while (!list_empty(&pool->free_slots)) {
2814 		slot = list_first_entry(&pool->free_slots,
2815 					struct i3c_generic_ibi_slot, node);
2816 		list_del(&slot->node);
2817 		nslots++;
2818 	}
2819 
2820 	/*
2821 	 * If the number of freed slots is not equal to the number of allocated
2822 	 * slots we have a leak somewhere.
2823 	 */
2824 	WARN_ON(nslots != pool->num_slots);
2825 
2826 	kfree(pool->payload_buf);
2827 	kfree(pool->slots);
2828 	kfree(pool);
2829 }
2830 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
2831 
2832 /**
2833  * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2834  * @dev: the device this pool will be used for
2835  * @req: IBI setup request describing what the device driver expects
2836  *
2837  * Create a generic IBI pool based on the information provided in @req.
2838  *
2839  * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2840  */
2841 struct i3c_generic_ibi_pool *
2842 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
2843 			   const struct i3c_ibi_setup *req)
2844 {
2845 	struct i3c_generic_ibi_pool *pool;
2846 	struct i3c_generic_ibi_slot *slot;
2847 	unsigned int i;
2848 	int ret;
2849 
2850 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2851 	if (!pool)
2852 		return ERR_PTR(-ENOMEM);
2853 
2854 	spin_lock_init(&pool->lock);
2855 	INIT_LIST_HEAD(&pool->free_slots);
2856 	INIT_LIST_HEAD(&pool->pending);
2857 
2858 	pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL);
2859 	if (!pool->slots) {
2860 		ret = -ENOMEM;
2861 		goto err_free_pool;
2862 	}
2863 
2864 	if (req->max_payload_len) {
2865 		pool->payload_buf = kcalloc(req->num_slots,
2866 					    req->max_payload_len, GFP_KERNEL);
2867 		if (!pool->payload_buf) {
2868 			ret = -ENOMEM;
2869 			goto err_free_pool;
2870 		}
2871 	}
2872 
2873 	for (i = 0; i < req->num_slots; i++) {
2874 		slot = &pool->slots[i];
2875 		i3c_master_init_ibi_slot(dev, &slot->base);
2876 
2877 		if (req->max_payload_len)
2878 			slot->base.data = pool->payload_buf +
2879 					  (i * req->max_payload_len);
2880 
2881 		list_add_tail(&slot->node, &pool->free_slots);
2882 		pool->num_slots++;
2883 	}
2884 
2885 	return pool;
2886 
2887 err_free_pool:
2888 	i3c_generic_ibi_free_pool(pool);
2889 	return ERR_PTR(ret);
2890 }
2891 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
2892 
2893 /**
2894  * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2895  * @pool: the pool to query an IBI slot on
2896  *
2897  * Search for a free slot in a generic IBI pool.
2898  * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2899  * when it's no longer needed.
2900  *
2901  * Return: a pointer to a free slot, or NULL if there's no free slot available.
2902  */
2903 struct i3c_ibi_slot *
2904 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
2905 {
2906 	struct i3c_generic_ibi_slot *slot;
2907 	unsigned long flags;
2908 
2909 	spin_lock_irqsave(&pool->lock, flags);
2910 	slot = list_first_entry_or_null(&pool->free_slots,
2911 					struct i3c_generic_ibi_slot, node);
2912 	if (slot)
2913 		list_del(&slot->node);
2914 	spin_unlock_irqrestore(&pool->lock, flags);
2915 
2916 	return slot ? &slot->base : NULL;
2917 }
2918 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
2919 
2920 /**
2921  * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2922  * @pool: the pool to return the IBI slot to
2923  * @s: IBI slot to recycle
2924  *
2925  * Add an IBI slot back to its generic IBI pool. Should be called from the
2926  * master driver struct_master_controller_ops->recycle_ibi() method.
2927  */
2928 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
2929 				  struct i3c_ibi_slot *s)
2930 {
2931 	struct i3c_generic_ibi_slot *slot;
2932 	unsigned long flags;
2933 
2934 	if (!s)
2935 		return;
2936 
2937 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
2938 	spin_lock_irqsave(&pool->lock, flags);
2939 	list_add_tail(&slot->node, &pool->free_slots);
2940 	spin_unlock_irqrestore(&pool->lock, flags);
2941 }
2942 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
2943 
2944 static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops)
2945 {
2946 	if (!ops || !ops->bus_init || !ops->i3c_xfers ||
2947 	    !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers)
2948 		return -EINVAL;
2949 
2950 	if (ops->request_ibi &&
2951 	    (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi ||
2952 	     !ops->recycle_ibi_slot))
2953 		return -EINVAL;
2954 
2955 	return 0;
2956 }
2957 
2958 /**
2959  * i3c_master_register() - register an I3C master
2960  * @master: master used to send frames on the bus
2961  * @parent: the parent device (the one that provides this I3C master
2962  *	    controller)
2963  * @ops: the master controller operations
2964  * @secondary: true if you are registering a secondary master. Will return
2965  *	       -EOPNOTSUPP if set to true since secondary masters are not yet
2966  *	       supported
2967  *
2968  * This function takes care of everything for you:
2969  *
2970  * - creates and initializes the I3C bus
2971  * - populates the bus with static I2C devs if @parent->of_node is not
2972  *   NULL
2973  * - registers all I3C devices added by the controller during bus
2974  *   initialization
2975  * - registers the I2C adapter and all I2C devices
2976  *
2977  * Return: 0 in case of success, a negative error code otherwise.
2978  */
2979 int i3c_master_register(struct i3c_master_controller *master,
2980 			struct device *parent,
2981 			const struct i3c_master_controller_ops *ops,
2982 			bool secondary)
2983 {
2984 	unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE;
2985 	struct i3c_bus *i3cbus = i3c_master_get_bus(master);
2986 	enum i3c_bus_mode mode = I3C_BUS_MODE_PURE;
2987 	struct i2c_dev_boardinfo *i2cbi;
2988 	int ret;
2989 
2990 	/* We do not support secondary masters yet. */
2991 	if (secondary)
2992 		return -EOPNOTSUPP;
2993 
2994 	ret = i3c_master_check_ops(ops);
2995 	if (ret)
2996 		return ret;
2997 
2998 	master->dev.parent = parent;
2999 	master->dev.of_node = of_node_get(parent->of_node);
3000 	master->dev.bus = &i3c_bus_type;
3001 	master->dev.type = &i3c_masterdev_type;
3002 	master->dev.release = i3c_masterdev_release;
3003 	master->ops = ops;
3004 	master->secondary = secondary;
3005 	INIT_LIST_HEAD(&master->boardinfo.i2c);
3006 	INIT_LIST_HEAD(&master->boardinfo.i3c);
3007 
3008 	ret = i3c_master_rpm_get(master);
3009 	if (ret)
3010 		return ret;
3011 
3012 	device_initialize(&master->dev);
3013 
3014 	master->dev.dma_mask = parent->dma_mask;
3015 	master->dev.coherent_dma_mask = parent->coherent_dma_mask;
3016 	master->dev.dma_parms = parent->dma_parms;
3017 
3018 	ret = i3c_bus_init(i3cbus, master->dev.of_node);
3019 	if (ret)
3020 		goto err_put_dev;
3021 
3022 	dev_set_name(&master->dev, "i3c-%d", i3cbus->id);
3023 
3024 	ret = of_populate_i3c_bus(master);
3025 	if (ret)
3026 		goto err_put_dev;
3027 
3028 	list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) {
3029 		switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) {
3030 		case I3C_LVR_I2C_INDEX(0):
3031 			if (mode < I3C_BUS_MODE_MIXED_FAST)
3032 				mode = I3C_BUS_MODE_MIXED_FAST;
3033 			break;
3034 		case I3C_LVR_I2C_INDEX(1):
3035 			if (mode < I3C_BUS_MODE_MIXED_LIMITED)
3036 				mode = I3C_BUS_MODE_MIXED_LIMITED;
3037 			break;
3038 		case I3C_LVR_I2C_INDEX(2):
3039 			if (mode < I3C_BUS_MODE_MIXED_SLOW)
3040 				mode = I3C_BUS_MODE_MIXED_SLOW;
3041 			break;
3042 		default:
3043 			ret = -EINVAL;
3044 			goto err_put_dev;
3045 		}
3046 
3047 		if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE)
3048 			i2c_scl_rate = I3C_BUS_I2C_FM_SCL_MAX_RATE;
3049 	}
3050 
3051 	ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate);
3052 	if (ret)
3053 		goto err_put_dev;
3054 
3055 	master->wq = alloc_workqueue("%s", WQ_PERCPU, 0, dev_name(parent));
3056 	if (!master->wq) {
3057 		ret = -ENOMEM;
3058 		goto err_put_dev;
3059 	}
3060 
3061 	ret = i3c_master_bus_init(master);
3062 	if (ret)
3063 		goto err_put_dev;
3064 
3065 	ret = device_add(&master->dev);
3066 	if (ret)
3067 		goto err_cleanup_bus;
3068 
3069 	/*
3070 	 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
3071 	 * through the I2C subsystem.
3072 	 */
3073 	ret = i3c_master_i2c_adapter_init(master);
3074 	if (ret)
3075 		goto err_del_dev;
3076 
3077 	i3c_bus_notify(i3cbus, I3C_NOTIFY_BUS_ADD);
3078 
3079 	pm_runtime_no_callbacks(&master->dev);
3080 	pm_suspend_ignore_children(&master->dev, true);
3081 	pm_runtime_enable(&master->dev);
3082 
3083 	/*
3084 	 * We're done initializing the bus and the controller, we can now
3085 	 * register I3C devices discovered during the initial DAA.
3086 	 */
3087 	master->init_done = true;
3088 	i3c_bus_normaluse_lock(&master->bus);
3089 	i3c_master_register_new_i3c_devs(master);
3090 	i3c_bus_normaluse_unlock(&master->bus);
3091 
3092 	if (master->ops->set_dev_nack_retry)
3093 		device_create_file(&master->dev, &dev_attr_dev_nack_retry_count);
3094 
3095 	i3c_master_rpm_put(master);
3096 
3097 	return 0;
3098 
3099 err_del_dev:
3100 	device_del(&master->dev);
3101 
3102 err_cleanup_bus:
3103 	i3c_master_bus_cleanup(master);
3104 
3105 err_put_dev:
3106 	i3c_master_rpm_put(master);
3107 	put_device(&master->dev);
3108 
3109 	return ret;
3110 }
3111 EXPORT_SYMBOL_GPL(i3c_master_register);
3112 
3113 /**
3114  * i3c_master_unregister() - unregister an I3C master
3115  * @master: master used to send frames on the bus
3116  *
3117  * Basically undo everything done in i3c_master_register().
3118  */
3119 void i3c_master_unregister(struct i3c_master_controller *master)
3120 {
3121 	i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE);
3122 
3123 	if (master->ops->set_dev_nack_retry)
3124 		device_remove_file(&master->dev, &dev_attr_dev_nack_retry_count);
3125 
3126 	i3c_master_i2c_adapter_cleanup(master);
3127 	i3c_master_unregister_i3c_devs(master);
3128 	i3c_master_bus_cleanup(master);
3129 	pm_runtime_disable(&master->dev);
3130 	device_unregister(&master->dev);
3131 }
3132 EXPORT_SYMBOL_GPL(i3c_master_unregister);
3133 
3134 int i3c_dev_setdasa_locked(struct i3c_dev_desc *dev)
3135 {
3136 	struct i3c_master_controller *master;
3137 
3138 	if (!dev)
3139 		return -ENOENT;
3140 
3141 	master = i3c_dev_get_master(dev);
3142 	if (!master)
3143 		return -EINVAL;
3144 
3145 	if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr ||
3146 		!dev->boardinfo->static_addr)
3147 		return -EINVAL;
3148 
3149 	return i3c_master_setdasa_locked(master, dev->info.static_addr,
3150 						dev->boardinfo->init_dyn_addr);
3151 }
3152 
3153 int i3c_dev_do_xfers_locked(struct i3c_dev_desc *dev, struct i3c_xfer *xfers,
3154 			    int nxfers, enum i3c_xfer_mode mode)
3155 {
3156 	struct i3c_master_controller *master;
3157 
3158 	if (!dev)
3159 		return -ENOENT;
3160 
3161 	master = i3c_dev_get_master(dev);
3162 	if (!master || !xfers)
3163 		return -EINVAL;
3164 
3165 	if (mode != I3C_SDR && !(master->this->info.hdr_cap & BIT(mode)))
3166 		return -EOPNOTSUPP;
3167 
3168 	return master->ops->i3c_xfers(dev, xfers, nxfers, mode);
3169 }
3170 
3171 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev)
3172 {
3173 	struct i3c_master_controller *master;
3174 	int ret;
3175 
3176 	if (!dev->ibi)
3177 		return -EINVAL;
3178 
3179 	master = i3c_dev_get_master(dev);
3180 	ret = master->ops->disable_ibi(dev);
3181 	if (ret)
3182 		return ret;
3183 
3184 	reinit_completion(&dev->ibi->all_ibis_handled);
3185 	if (atomic_read(&dev->ibi->pending_ibis))
3186 		wait_for_completion(&dev->ibi->all_ibis_handled);
3187 
3188 	dev->ibi->enabled = false;
3189 
3190 	return 0;
3191 }
3192 
3193 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev)
3194 {
3195 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3196 	int ret;
3197 
3198 	if (!dev->ibi)
3199 		return -EINVAL;
3200 
3201 	ret = master->ops->enable_ibi(dev);
3202 	if (!ret)
3203 		dev->ibi->enabled = true;
3204 
3205 	return ret;
3206 }
3207 
3208 int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
3209 			       const struct i3c_ibi_setup *req)
3210 {
3211 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3212 	struct i3c_device_ibi_info *ibi;
3213 	int ret;
3214 
3215 	if (!master->ops->request_ibi)
3216 		return -EOPNOTSUPP;
3217 
3218 	if (dev->ibi)
3219 		return -EBUSY;
3220 
3221 	ibi = kzalloc(sizeof(*ibi), GFP_KERNEL);
3222 	if (!ibi)
3223 		return -ENOMEM;
3224 
3225 	ibi->wq = alloc_ordered_workqueue(dev_name(i3cdev_to_dev(dev->dev)), WQ_MEM_RECLAIM);
3226 	if (!ibi->wq) {
3227 		kfree(ibi);
3228 		return -ENOMEM;
3229 	}
3230 
3231 	atomic_set(&ibi->pending_ibis, 0);
3232 	init_completion(&ibi->all_ibis_handled);
3233 	ibi->handler = req->handler;
3234 	ibi->max_payload_len = req->max_payload_len;
3235 	ibi->num_slots = req->num_slots;
3236 
3237 	dev->ibi = ibi;
3238 	ret = master->ops->request_ibi(dev, req);
3239 	if (ret) {
3240 		kfree(ibi);
3241 		dev->ibi = NULL;
3242 	}
3243 
3244 	return ret;
3245 }
3246 
3247 void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
3248 {
3249 	struct i3c_master_controller *master = i3c_dev_get_master(dev);
3250 
3251 	if (!dev->ibi)
3252 		return;
3253 
3254 	if (dev->ibi->enabled) {
3255 		int ret;
3256 
3257 		dev_err(&master->dev, "Freeing IBI that is still enabled\n");
3258 		ret = i3c_master_rpm_get(master);
3259 		if (!ret) {
3260 			ret = i3c_dev_disable_ibi_locked(dev);
3261 			i3c_master_rpm_put(master);
3262 		}
3263 		if (ret)
3264 			dev_err(&master->dev, "Failed to disable IBI before freeing\n");
3265 	}
3266 
3267 	master->ops->free_ibi(dev);
3268 
3269 	if (dev->ibi->wq) {
3270 		destroy_workqueue(dev->ibi->wq);
3271 		dev->ibi->wq = NULL;
3272 	}
3273 
3274 	kfree(dev->ibi);
3275 	dev->ibi = NULL;
3276 }
3277 
3278 static int __init i3c_init(void)
3279 {
3280 	int res;
3281 
3282 	res = of_alias_get_highest_id("i3c");
3283 	if (res >= 0) {
3284 		mutex_lock(&i3c_core_lock);
3285 		__i3c_first_dynamic_bus_num = res + 1;
3286 		mutex_unlock(&i3c_core_lock);
3287 	}
3288 
3289 	res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
3290 	if (res)
3291 		return res;
3292 
3293 	res = bus_register(&i3c_bus_type);
3294 	if (res)
3295 		goto out_unreg_notifier;
3296 
3297 	return 0;
3298 
3299 out_unreg_notifier:
3300 	bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
3301 
3302 	return res;
3303 }
3304 subsys_initcall(i3c_init);
3305 
3306 static void __exit i3c_exit(void)
3307 {
3308 	bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
3309 	idr_destroy(&i3c_bus_idr);
3310 	bus_unregister(&i3c_bus_type);
3311 }
3312 module_exit(i3c_exit);
3313 
3314 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
3315 MODULE_DESCRIPTION("I3C core");
3316 MODULE_LICENSE("GPL v2");
3317