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