xref: /openbmc/linux/drivers/soundwire/bus.c (revision e2ad626f)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3 
4 #include <linux/acpi.h>
5 #include <linux/delay.h>
6 #include <linux/irq.h>
7 #include <linux/mod_devicetable.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/soundwire/sdw_registers.h>
10 #include <linux/soundwire/sdw.h>
11 #include <linux/soundwire/sdw_type.h>
12 #include "bus.h"
13 #include "sysfs_local.h"
14 
15 static DEFINE_IDA(sdw_bus_ida);
16 
17 static int sdw_get_id(struct sdw_bus *bus)
18 {
19 	int rc = ida_alloc(&sdw_bus_ida, GFP_KERNEL);
20 
21 	if (rc < 0)
22 		return rc;
23 
24 	bus->id = rc;
25 	return 0;
26 }
27 
28 static int sdw_irq_map(struct irq_domain *h, unsigned int virq,
29 		       irq_hw_number_t hw)
30 {
31 	struct sdw_bus *bus = h->host_data;
32 
33 	irq_set_chip_data(virq, bus);
34 	irq_set_chip(virq, &bus->irq_chip);
35 	irq_set_nested_thread(virq, 1);
36 	irq_set_noprobe(virq);
37 
38 	return 0;
39 }
40 
41 static const struct irq_domain_ops sdw_domain_ops = {
42 	.map	= sdw_irq_map,
43 };
44 
45 /**
46  * sdw_bus_master_add() - add a bus Master instance
47  * @bus: bus instance
48  * @parent: parent device
49  * @fwnode: firmware node handle
50  *
51  * Initializes the bus instance, read properties and create child
52  * devices.
53  */
54 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
55 		       struct fwnode_handle *fwnode)
56 {
57 	struct sdw_master_prop *prop = NULL;
58 	int ret;
59 
60 	if (!parent) {
61 		pr_err("SoundWire parent device is not set\n");
62 		return -ENODEV;
63 	}
64 
65 	ret = sdw_get_id(bus);
66 	if (ret < 0) {
67 		dev_err(parent, "Failed to get bus id\n");
68 		return ret;
69 	}
70 
71 	ret = sdw_master_device_add(bus, parent, fwnode);
72 	if (ret < 0) {
73 		dev_err(parent, "Failed to add master device at link %d\n",
74 			bus->link_id);
75 		return ret;
76 	}
77 
78 	if (!bus->ops) {
79 		dev_err(bus->dev, "SoundWire Bus ops are not set\n");
80 		return -EINVAL;
81 	}
82 
83 	if (!bus->compute_params) {
84 		dev_err(bus->dev,
85 			"Bandwidth allocation not configured, compute_params no set\n");
86 		return -EINVAL;
87 	}
88 
89 	/*
90 	 * Give each bus_lock and msg_lock a unique key so that lockdep won't
91 	 * trigger a deadlock warning when the locks of several buses are
92 	 * grabbed during configuration of a multi-bus stream.
93 	 */
94 	lockdep_register_key(&bus->msg_lock_key);
95 	__mutex_init(&bus->msg_lock, "msg_lock", &bus->msg_lock_key);
96 
97 	lockdep_register_key(&bus->bus_lock_key);
98 	__mutex_init(&bus->bus_lock, "bus_lock", &bus->bus_lock_key);
99 
100 	INIT_LIST_HEAD(&bus->slaves);
101 	INIT_LIST_HEAD(&bus->m_rt_list);
102 
103 	/*
104 	 * Initialize multi_link flag
105 	 */
106 	bus->multi_link = false;
107 	if (bus->ops->read_prop) {
108 		ret = bus->ops->read_prop(bus);
109 		if (ret < 0) {
110 			dev_err(bus->dev,
111 				"Bus read properties failed:%d\n", ret);
112 			return ret;
113 		}
114 	}
115 
116 	sdw_bus_debugfs_init(bus);
117 
118 	/*
119 	 * Device numbers in SoundWire are 0 through 15. Enumeration device
120 	 * number (0), Broadcast device number (15), Group numbers (12 and
121 	 * 13) and Master device number (14) are not used for assignment so
122 	 * mask these and other higher bits.
123 	 */
124 
125 	/* Set higher order bits */
126 	*bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
127 
128 	/* Set enumuration device number and broadcast device number */
129 	set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
130 	set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
131 
132 	/* Set group device numbers and master device number */
133 	set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
134 	set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
135 	set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
136 
137 	/*
138 	 * SDW is an enumerable bus, but devices can be powered off. So,
139 	 * they won't be able to report as present.
140 	 *
141 	 * Create Slave devices based on Slaves described in
142 	 * the respective firmware (ACPI/DT)
143 	 */
144 	if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
145 		ret = sdw_acpi_find_slaves(bus);
146 	else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
147 		ret = sdw_of_find_slaves(bus);
148 	else
149 		ret = -ENOTSUPP; /* No ACPI/DT so error out */
150 
151 	if (ret < 0) {
152 		dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
153 		return ret;
154 	}
155 
156 	/*
157 	 * Initialize clock values based on Master properties. The max
158 	 * frequency is read from max_clk_freq property. Current assumption
159 	 * is that the bus will start at highest clock frequency when
160 	 * powered on.
161 	 *
162 	 * Default active bank will be 0 as out of reset the Slaves have
163 	 * to start with bank 0 (Table 40 of Spec)
164 	 */
165 	prop = &bus->prop;
166 	bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
167 	bus->params.curr_dr_freq = bus->params.max_dr_freq;
168 	bus->params.curr_bank = SDW_BANK0;
169 	bus->params.next_bank = SDW_BANK1;
170 
171 	bus->irq_chip.name = dev_name(bus->dev);
172 	bus->domain = irq_domain_create_linear(fwnode, SDW_MAX_DEVICES,
173 					       &sdw_domain_ops, bus);
174 	if (!bus->domain) {
175 		dev_err(bus->dev, "Failed to add IRQ domain\n");
176 		return -EINVAL;
177 	}
178 
179 	return 0;
180 }
181 EXPORT_SYMBOL(sdw_bus_master_add);
182 
183 static int sdw_delete_slave(struct device *dev, void *data)
184 {
185 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
186 	struct sdw_bus *bus = slave->bus;
187 
188 	pm_runtime_disable(dev);
189 
190 	sdw_slave_debugfs_exit(slave);
191 
192 	mutex_lock(&bus->bus_lock);
193 
194 	if (slave->dev_num) { /* clear dev_num if assigned */
195 		clear_bit(slave->dev_num, bus->assigned);
196 		if (bus->ops && bus->ops->put_device_num)
197 			bus->ops->put_device_num(bus, slave);
198 	}
199 	list_del_init(&slave->node);
200 	mutex_unlock(&bus->bus_lock);
201 
202 	device_unregister(dev);
203 	return 0;
204 }
205 
206 /**
207  * sdw_bus_master_delete() - delete the bus master instance
208  * @bus: bus to be deleted
209  *
210  * Remove the instance, delete the child devices.
211  */
212 void sdw_bus_master_delete(struct sdw_bus *bus)
213 {
214 	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
215 
216 	irq_domain_remove(bus->domain);
217 
218 	sdw_master_device_del(bus);
219 
220 	sdw_bus_debugfs_exit(bus);
221 	lockdep_unregister_key(&bus->bus_lock_key);
222 	lockdep_unregister_key(&bus->msg_lock_key);
223 	ida_free(&sdw_bus_ida, bus->id);
224 }
225 EXPORT_SYMBOL(sdw_bus_master_delete);
226 
227 /*
228  * SDW IO Calls
229  */
230 
231 static inline int find_response_code(enum sdw_command_response resp)
232 {
233 	switch (resp) {
234 	case SDW_CMD_OK:
235 		return 0;
236 
237 	case SDW_CMD_IGNORED:
238 		return -ENODATA;
239 
240 	case SDW_CMD_TIMEOUT:
241 		return -ETIMEDOUT;
242 
243 	default:
244 		return -EIO;
245 	}
246 }
247 
248 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
249 {
250 	int retry = bus->prop.err_threshold;
251 	enum sdw_command_response resp;
252 	int ret = 0, i;
253 
254 	for (i = 0; i <= retry; i++) {
255 		resp = bus->ops->xfer_msg(bus, msg);
256 		ret = find_response_code(resp);
257 
258 		/* if cmd is ok or ignored return */
259 		if (ret == 0 || ret == -ENODATA)
260 			return ret;
261 	}
262 
263 	return ret;
264 }
265 
266 static inline int do_transfer_defer(struct sdw_bus *bus,
267 				    struct sdw_msg *msg)
268 {
269 	struct sdw_defer *defer = &bus->defer_msg;
270 	int retry = bus->prop.err_threshold;
271 	enum sdw_command_response resp;
272 	int ret = 0, i;
273 
274 	defer->msg = msg;
275 	defer->length = msg->len;
276 	init_completion(&defer->complete);
277 
278 	for (i = 0; i <= retry; i++) {
279 		resp = bus->ops->xfer_msg_defer(bus);
280 		ret = find_response_code(resp);
281 		/* if cmd is ok or ignored return */
282 		if (ret == 0 || ret == -ENODATA)
283 			return ret;
284 	}
285 
286 	return ret;
287 }
288 
289 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
290 {
291 	int ret;
292 
293 	ret = do_transfer(bus, msg);
294 	if (ret != 0 && ret != -ENODATA)
295 		dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
296 			msg->dev_num, ret,
297 			(msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
298 			msg->addr, msg->len);
299 
300 	return ret;
301 }
302 
303 /**
304  * sdw_transfer() - Synchronous transfer message to a SDW Slave device
305  * @bus: SDW bus
306  * @msg: SDW message to be xfered
307  */
308 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
309 {
310 	int ret;
311 
312 	mutex_lock(&bus->msg_lock);
313 
314 	ret = sdw_transfer_unlocked(bus, msg);
315 
316 	mutex_unlock(&bus->msg_lock);
317 
318 	return ret;
319 }
320 
321 /**
322  * sdw_show_ping_status() - Direct report of PING status, to be used by Peripheral drivers
323  * @bus: SDW bus
324  * @sync_delay: Delay before reading status
325  */
326 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay)
327 {
328 	u32 status;
329 
330 	if (!bus->ops->read_ping_status)
331 		return;
332 
333 	/*
334 	 * wait for peripheral to sync if desired. 10-15ms should be more than
335 	 * enough in most cases.
336 	 */
337 	if (sync_delay)
338 		usleep_range(10000, 15000);
339 
340 	mutex_lock(&bus->msg_lock);
341 
342 	status = bus->ops->read_ping_status(bus);
343 
344 	mutex_unlock(&bus->msg_lock);
345 
346 	if (!status)
347 		dev_warn(bus->dev, "%s: no peripherals attached\n", __func__);
348 	else
349 		dev_dbg(bus->dev, "PING status: %#x\n", status);
350 }
351 EXPORT_SYMBOL(sdw_show_ping_status);
352 
353 /**
354  * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
355  * @bus: SDW bus
356  * @msg: SDW message to be xfered
357  *
358  * Caller needs to hold the msg_lock lock while calling this
359  */
360 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg)
361 {
362 	int ret;
363 
364 	if (!bus->ops->xfer_msg_defer)
365 		return -ENOTSUPP;
366 
367 	ret = do_transfer_defer(bus, msg);
368 	if (ret != 0 && ret != -ENODATA)
369 		dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
370 			msg->dev_num, ret);
371 
372 	return ret;
373 }
374 
375 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
376 		 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
377 {
378 	memset(msg, 0, sizeof(*msg));
379 	msg->addr = addr; /* addr is 16 bit and truncated here */
380 	msg->len = count;
381 	msg->dev_num = dev_num;
382 	msg->flags = flags;
383 	msg->buf = buf;
384 
385 	if (addr < SDW_REG_NO_PAGE) /* no paging area */
386 		return 0;
387 
388 	if (addr >= SDW_REG_MAX) { /* illegal addr */
389 		pr_err("SDW: Invalid address %x passed\n", addr);
390 		return -EINVAL;
391 	}
392 
393 	if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
394 		if (slave && !slave->prop.paging_support)
395 			return 0;
396 		/* no need for else as that will fall-through to paging */
397 	}
398 
399 	/* paging mandatory */
400 	if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
401 		pr_err("SDW: Invalid device for paging :%d\n", dev_num);
402 		return -EINVAL;
403 	}
404 
405 	if (!slave) {
406 		pr_err("SDW: No slave for paging addr\n");
407 		return -EINVAL;
408 	}
409 
410 	if (!slave->prop.paging_support) {
411 		dev_err(&slave->dev,
412 			"address %x needs paging but no support\n", addr);
413 		return -EINVAL;
414 	}
415 
416 	msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
417 	msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
418 	msg->addr |= BIT(15);
419 	msg->page = true;
420 
421 	return 0;
422 }
423 
424 /*
425  * Read/Write IO functions.
426  */
427 
428 static int sdw_ntransfer_no_pm(struct sdw_slave *slave, u32 addr, u8 flags,
429 			       size_t count, u8 *val)
430 {
431 	struct sdw_msg msg;
432 	size_t size;
433 	int ret;
434 
435 	while (count) {
436 		// Only handle bytes up to next page boundary
437 		size = min_t(size_t, count, (SDW_REGADDR + 1) - (addr & SDW_REGADDR));
438 
439 		ret = sdw_fill_msg(&msg, slave, addr, size, slave->dev_num, flags, val);
440 		if (ret < 0)
441 			return ret;
442 
443 		ret = sdw_transfer(slave->bus, &msg);
444 		if (ret < 0 && !slave->is_mockup_device)
445 			return ret;
446 
447 		addr += size;
448 		val += size;
449 		count -= size;
450 	}
451 
452 	return 0;
453 }
454 
455 /**
456  * sdw_nread_no_pm() - Read "n" contiguous SDW Slave registers with no PM
457  * @slave: SDW Slave
458  * @addr: Register address
459  * @count: length
460  * @val: Buffer for values to be read
461  *
462  * Note that if the message crosses a page boundary each page will be
463  * transferred under a separate invocation of the msg_lock.
464  */
465 int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
466 {
467 	return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_READ, count, val);
468 }
469 EXPORT_SYMBOL(sdw_nread_no_pm);
470 
471 /**
472  * sdw_nwrite_no_pm() - Write "n" contiguous SDW Slave registers with no PM
473  * @slave: SDW Slave
474  * @addr: Register address
475  * @count: length
476  * @val: Buffer for values to be written
477  *
478  * Note that if the message crosses a page boundary each page will be
479  * transferred under a separate invocation of the msg_lock.
480  */
481 int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
482 {
483 	return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_WRITE, count, (u8 *)val);
484 }
485 EXPORT_SYMBOL(sdw_nwrite_no_pm);
486 
487 /**
488  * sdw_write_no_pm() - Write a SDW Slave register with no PM
489  * @slave: SDW Slave
490  * @addr: Register address
491  * @value: Register value
492  */
493 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
494 {
495 	return sdw_nwrite_no_pm(slave, addr, 1, &value);
496 }
497 EXPORT_SYMBOL(sdw_write_no_pm);
498 
499 static int
500 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
501 {
502 	struct sdw_msg msg;
503 	u8 buf;
504 	int ret;
505 
506 	ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
507 			   SDW_MSG_FLAG_READ, &buf);
508 	if (ret < 0)
509 		return ret;
510 
511 	ret = sdw_transfer(bus, &msg);
512 	if (ret < 0)
513 		return ret;
514 
515 	return buf;
516 }
517 
518 static int
519 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
520 {
521 	struct sdw_msg msg;
522 	int ret;
523 
524 	ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
525 			   SDW_MSG_FLAG_WRITE, &value);
526 	if (ret < 0)
527 		return ret;
528 
529 	return sdw_transfer(bus, &msg);
530 }
531 
532 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
533 {
534 	struct sdw_msg msg;
535 	u8 buf;
536 	int ret;
537 
538 	ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
539 			   SDW_MSG_FLAG_READ, &buf);
540 	if (ret < 0)
541 		return ret;
542 
543 	ret = sdw_transfer_unlocked(bus, &msg);
544 	if (ret < 0)
545 		return ret;
546 
547 	return buf;
548 }
549 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
550 
551 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
552 {
553 	struct sdw_msg msg;
554 	int ret;
555 
556 	ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
557 			   SDW_MSG_FLAG_WRITE, &value);
558 	if (ret < 0)
559 		return ret;
560 
561 	return sdw_transfer_unlocked(bus, &msg);
562 }
563 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
564 
565 /**
566  * sdw_read_no_pm() - Read a SDW Slave register with no PM
567  * @slave: SDW Slave
568  * @addr: Register address
569  */
570 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
571 {
572 	u8 buf;
573 	int ret;
574 
575 	ret = sdw_nread_no_pm(slave, addr, 1, &buf);
576 	if (ret < 0)
577 		return ret;
578 	else
579 		return buf;
580 }
581 EXPORT_SYMBOL(sdw_read_no_pm);
582 
583 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
584 {
585 	int tmp;
586 
587 	tmp = sdw_read_no_pm(slave, addr);
588 	if (tmp < 0)
589 		return tmp;
590 
591 	tmp = (tmp & ~mask) | val;
592 	return sdw_write_no_pm(slave, addr, tmp);
593 }
594 EXPORT_SYMBOL(sdw_update_no_pm);
595 
596 /* Read-Modify-Write Slave register */
597 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
598 {
599 	int tmp;
600 
601 	tmp = sdw_read(slave, addr);
602 	if (tmp < 0)
603 		return tmp;
604 
605 	tmp = (tmp & ~mask) | val;
606 	return sdw_write(slave, addr, tmp);
607 }
608 EXPORT_SYMBOL(sdw_update);
609 
610 /**
611  * sdw_nread() - Read "n" contiguous SDW Slave registers
612  * @slave: SDW Slave
613  * @addr: Register address
614  * @count: length
615  * @val: Buffer for values to be read
616  *
617  * This version of the function will take a PM reference to the slave
618  * device.
619  * Note that if the message crosses a page boundary each page will be
620  * transferred under a separate invocation of the msg_lock.
621  */
622 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
623 {
624 	int ret;
625 
626 	ret = pm_runtime_get_sync(&slave->dev);
627 	if (ret < 0 && ret != -EACCES) {
628 		pm_runtime_put_noidle(&slave->dev);
629 		return ret;
630 	}
631 
632 	ret = sdw_nread_no_pm(slave, addr, count, val);
633 
634 	pm_runtime_mark_last_busy(&slave->dev);
635 	pm_runtime_put(&slave->dev);
636 
637 	return ret;
638 }
639 EXPORT_SYMBOL(sdw_nread);
640 
641 /**
642  * sdw_nwrite() - Write "n" contiguous SDW Slave registers
643  * @slave: SDW Slave
644  * @addr: Register address
645  * @count: length
646  * @val: Buffer for values to be written
647  *
648  * This version of the function will take a PM reference to the slave
649  * device.
650  * Note that if the message crosses a page boundary each page will be
651  * transferred under a separate invocation of the msg_lock.
652  */
653 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
654 {
655 	int ret;
656 
657 	ret = pm_runtime_get_sync(&slave->dev);
658 	if (ret < 0 && ret != -EACCES) {
659 		pm_runtime_put_noidle(&slave->dev);
660 		return ret;
661 	}
662 
663 	ret = sdw_nwrite_no_pm(slave, addr, count, val);
664 
665 	pm_runtime_mark_last_busy(&slave->dev);
666 	pm_runtime_put(&slave->dev);
667 
668 	return ret;
669 }
670 EXPORT_SYMBOL(sdw_nwrite);
671 
672 /**
673  * sdw_read() - Read a SDW Slave register
674  * @slave: SDW Slave
675  * @addr: Register address
676  *
677  * This version of the function will take a PM reference to the slave
678  * device.
679  */
680 int sdw_read(struct sdw_slave *slave, u32 addr)
681 {
682 	u8 buf;
683 	int ret;
684 
685 	ret = sdw_nread(slave, addr, 1, &buf);
686 	if (ret < 0)
687 		return ret;
688 
689 	return buf;
690 }
691 EXPORT_SYMBOL(sdw_read);
692 
693 /**
694  * sdw_write() - Write a SDW Slave register
695  * @slave: SDW Slave
696  * @addr: Register address
697  * @value: Register value
698  *
699  * This version of the function will take a PM reference to the slave
700  * device.
701  */
702 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
703 {
704 	return sdw_nwrite(slave, addr, 1, &value);
705 }
706 EXPORT_SYMBOL(sdw_write);
707 
708 /*
709  * SDW alert handling
710  */
711 
712 /* called with bus_lock held */
713 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
714 {
715 	struct sdw_slave *slave;
716 
717 	list_for_each_entry(slave, &bus->slaves, node) {
718 		if (slave->dev_num == i)
719 			return slave;
720 	}
721 
722 	return NULL;
723 }
724 
725 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
726 {
727 	if (slave->id.mfg_id != id.mfg_id ||
728 	    slave->id.part_id != id.part_id ||
729 	    slave->id.class_id != id.class_id ||
730 	    (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
731 	     slave->id.unique_id != id.unique_id))
732 		return -ENODEV;
733 
734 	return 0;
735 }
736 EXPORT_SYMBOL(sdw_compare_devid);
737 
738 /* called with bus_lock held */
739 static int sdw_get_device_num(struct sdw_slave *slave)
740 {
741 	struct sdw_bus *bus = slave->bus;
742 	int bit;
743 
744 	if (bus->ops && bus->ops->get_device_num) {
745 		bit = bus->ops->get_device_num(bus, slave);
746 		if (bit < 0)
747 			goto err;
748 	} else {
749 		bit = find_first_zero_bit(bus->assigned, SDW_MAX_DEVICES);
750 		if (bit == SDW_MAX_DEVICES) {
751 			bit = -ENODEV;
752 			goto err;
753 		}
754 	}
755 
756 	/*
757 	 * Do not update dev_num in Slave data structure here,
758 	 * Update once program dev_num is successful
759 	 */
760 	set_bit(bit, bus->assigned);
761 
762 err:
763 	return bit;
764 }
765 
766 static int sdw_assign_device_num(struct sdw_slave *slave)
767 {
768 	struct sdw_bus *bus = slave->bus;
769 	int ret, dev_num;
770 	bool new_device = false;
771 
772 	/* check first if device number is assigned, if so reuse that */
773 	if (!slave->dev_num) {
774 		if (!slave->dev_num_sticky) {
775 			mutex_lock(&slave->bus->bus_lock);
776 			dev_num = sdw_get_device_num(slave);
777 			mutex_unlock(&slave->bus->bus_lock);
778 			if (dev_num < 0) {
779 				dev_err(bus->dev, "Get dev_num failed: %d\n",
780 					dev_num);
781 				return dev_num;
782 			}
783 			slave->dev_num = dev_num;
784 			slave->dev_num_sticky = dev_num;
785 			new_device = true;
786 		} else {
787 			slave->dev_num = slave->dev_num_sticky;
788 		}
789 	}
790 
791 	if (!new_device)
792 		dev_dbg(bus->dev,
793 			"Slave already registered, reusing dev_num:%d\n",
794 			slave->dev_num);
795 
796 	/* Clear the slave->dev_num to transfer message on device 0 */
797 	dev_num = slave->dev_num;
798 	slave->dev_num = 0;
799 
800 	ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
801 	if (ret < 0) {
802 		dev_err(bus->dev, "Program device_num %d failed: %d\n",
803 			dev_num, ret);
804 		return ret;
805 	}
806 
807 	/* After xfer of msg, restore dev_num */
808 	slave->dev_num = slave->dev_num_sticky;
809 
810 	if (bus->ops && bus->ops->new_peripheral_assigned)
811 		bus->ops->new_peripheral_assigned(bus, slave, dev_num);
812 
813 	return 0;
814 }
815 
816 void sdw_extract_slave_id(struct sdw_bus *bus,
817 			  u64 addr, struct sdw_slave_id *id)
818 {
819 	dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
820 
821 	id->sdw_version = SDW_VERSION(addr);
822 	id->unique_id = SDW_UNIQUE_ID(addr);
823 	id->mfg_id = SDW_MFG_ID(addr);
824 	id->part_id = SDW_PART_ID(addr);
825 	id->class_id = SDW_CLASS_ID(addr);
826 
827 	dev_dbg(bus->dev,
828 		"SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
829 		id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
830 }
831 EXPORT_SYMBOL(sdw_extract_slave_id);
832 
833 static int sdw_program_device_num(struct sdw_bus *bus, bool *programmed)
834 {
835 	u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
836 	struct sdw_slave *slave, *_s;
837 	struct sdw_slave_id id;
838 	struct sdw_msg msg;
839 	bool found;
840 	int count = 0, ret;
841 	u64 addr;
842 
843 	*programmed = false;
844 
845 	/* No Slave, so use raw xfer api */
846 	ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
847 			   SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
848 	if (ret < 0)
849 		return ret;
850 
851 	do {
852 		ret = sdw_transfer(bus, &msg);
853 		if (ret == -ENODATA) { /* end of device id reads */
854 			dev_dbg(bus->dev, "No more devices to enumerate\n");
855 			ret = 0;
856 			break;
857 		}
858 		if (ret < 0) {
859 			dev_err(bus->dev, "DEVID read fail:%d\n", ret);
860 			break;
861 		}
862 
863 		/*
864 		 * Construct the addr and extract. Cast the higher shift
865 		 * bits to avoid truncation due to size limit.
866 		 */
867 		addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
868 			((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
869 			((u64)buf[0] << 40);
870 
871 		sdw_extract_slave_id(bus, addr, &id);
872 
873 		found = false;
874 		/* Now compare with entries */
875 		list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
876 			if (sdw_compare_devid(slave, id) == 0) {
877 				found = true;
878 
879 				/*
880 				 * To prevent skipping state-machine stages don't
881 				 * program a device until we've seen it UNATTACH.
882 				 * Must return here because no other device on #0
883 				 * can be detected until this one has been
884 				 * assigned a device ID.
885 				 */
886 				if (slave->status != SDW_SLAVE_UNATTACHED)
887 					return 0;
888 
889 				/*
890 				 * Assign a new dev_num to this Slave and
891 				 * not mark it present. It will be marked
892 				 * present after it reports ATTACHED on new
893 				 * dev_num
894 				 */
895 				ret = sdw_assign_device_num(slave);
896 				if (ret < 0) {
897 					dev_err(bus->dev,
898 						"Assign dev_num failed:%d\n",
899 						ret);
900 					return ret;
901 				}
902 
903 				*programmed = true;
904 
905 				break;
906 			}
907 		}
908 
909 		if (!found) {
910 			/* TODO: Park this device in Group 13 */
911 
912 			/*
913 			 * add Slave device even if there is no platform
914 			 * firmware description. There will be no driver probe
915 			 * but the user/integration will be able to see the
916 			 * device, enumeration status and device number in sysfs
917 			 */
918 			sdw_slave_add(bus, &id, NULL);
919 
920 			dev_err(bus->dev, "Slave Entry not found\n");
921 		}
922 
923 		count++;
924 
925 		/*
926 		 * Check till error out or retry (count) exhausts.
927 		 * Device can drop off and rejoin during enumeration
928 		 * so count till twice the bound.
929 		 */
930 
931 	} while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
932 
933 	return ret;
934 }
935 
936 static void sdw_modify_slave_status(struct sdw_slave *slave,
937 				    enum sdw_slave_status status)
938 {
939 	struct sdw_bus *bus = slave->bus;
940 
941 	mutex_lock(&bus->bus_lock);
942 
943 	dev_vdbg(bus->dev,
944 		 "changing status slave %d status %d new status %d\n",
945 		 slave->dev_num, slave->status, status);
946 
947 	if (status == SDW_SLAVE_UNATTACHED) {
948 		dev_dbg(&slave->dev,
949 			"initializing enumeration and init completion for Slave %d\n",
950 			slave->dev_num);
951 
952 		reinit_completion(&slave->enumeration_complete);
953 		reinit_completion(&slave->initialization_complete);
954 
955 	} else if ((status == SDW_SLAVE_ATTACHED) &&
956 		   (slave->status == SDW_SLAVE_UNATTACHED)) {
957 		dev_dbg(&slave->dev,
958 			"signaling enumeration completion for Slave %d\n",
959 			slave->dev_num);
960 
961 		complete_all(&slave->enumeration_complete);
962 	}
963 	slave->status = status;
964 	mutex_unlock(&bus->bus_lock);
965 }
966 
967 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
968 				       enum sdw_clk_stop_mode mode,
969 				       enum sdw_clk_stop_type type)
970 {
971 	int ret = 0;
972 
973 	mutex_lock(&slave->sdw_dev_lock);
974 
975 	if (slave->probed)  {
976 		struct device *dev = &slave->dev;
977 		struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
978 
979 		if (drv->ops && drv->ops->clk_stop)
980 			ret = drv->ops->clk_stop(slave, mode, type);
981 	}
982 
983 	mutex_unlock(&slave->sdw_dev_lock);
984 
985 	return ret;
986 }
987 
988 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
989 				      enum sdw_clk_stop_mode mode,
990 				      bool prepare)
991 {
992 	bool wake_en;
993 	u32 val = 0;
994 	int ret;
995 
996 	wake_en = slave->prop.wake_capable;
997 
998 	if (prepare) {
999 		val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
1000 
1001 		if (mode == SDW_CLK_STOP_MODE1)
1002 			val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
1003 
1004 		if (wake_en)
1005 			val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
1006 	} else {
1007 		ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
1008 		if (ret < 0) {
1009 			if (ret != -ENODATA)
1010 				dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
1011 			return ret;
1012 		}
1013 		val = ret;
1014 		val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
1015 	}
1016 
1017 	ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
1018 
1019 	if (ret < 0 && ret != -ENODATA)
1020 		dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
1021 
1022 	return ret;
1023 }
1024 
1025 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
1026 {
1027 	int retry = bus->clk_stop_timeout;
1028 	int val;
1029 
1030 	do {
1031 		val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
1032 		if (val < 0) {
1033 			if (val != -ENODATA)
1034 				dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
1035 			return val;
1036 		}
1037 		val &= SDW_SCP_STAT_CLK_STP_NF;
1038 		if (!val) {
1039 			dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
1040 				dev_num);
1041 			return 0;
1042 		}
1043 
1044 		usleep_range(1000, 1500);
1045 		retry--;
1046 	} while (retry);
1047 
1048 	dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
1049 		dev_num);
1050 
1051 	return -ETIMEDOUT;
1052 }
1053 
1054 /**
1055  * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
1056  *
1057  * @bus: SDW bus instance
1058  *
1059  * Query Slave for clock stop mode and prepare for that mode.
1060  */
1061 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
1062 {
1063 	bool simple_clk_stop = true;
1064 	struct sdw_slave *slave;
1065 	bool is_slave = false;
1066 	int ret = 0;
1067 
1068 	/*
1069 	 * In order to save on transition time, prepare
1070 	 * each Slave and then wait for all Slave(s) to be
1071 	 * prepared for clock stop.
1072 	 * If one of the Slave devices has lost sync and
1073 	 * replies with Command Ignored/-ENODATA, we continue
1074 	 * the loop
1075 	 */
1076 	list_for_each_entry(slave, &bus->slaves, node) {
1077 		if (!slave->dev_num)
1078 			continue;
1079 
1080 		if (slave->status != SDW_SLAVE_ATTACHED &&
1081 		    slave->status != SDW_SLAVE_ALERT)
1082 			continue;
1083 
1084 		/* Identify if Slave(s) are available on Bus */
1085 		is_slave = true;
1086 
1087 		ret = sdw_slave_clk_stop_callback(slave,
1088 						  SDW_CLK_STOP_MODE0,
1089 						  SDW_CLK_PRE_PREPARE);
1090 		if (ret < 0 && ret != -ENODATA) {
1091 			dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
1092 			return ret;
1093 		}
1094 
1095 		/* Only prepare a Slave device if needed */
1096 		if (!slave->prop.simple_clk_stop_capable) {
1097 			simple_clk_stop = false;
1098 
1099 			ret = sdw_slave_clk_stop_prepare(slave,
1100 							 SDW_CLK_STOP_MODE0,
1101 							 true);
1102 			if (ret < 0 && ret != -ENODATA) {
1103 				dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
1104 				return ret;
1105 			}
1106 		}
1107 	}
1108 
1109 	/* Skip remaining clock stop preparation if no Slave is attached */
1110 	if (!is_slave)
1111 		return 0;
1112 
1113 	/*
1114 	 * Don't wait for all Slaves to be ready if they follow the simple
1115 	 * state machine
1116 	 */
1117 	if (!simple_clk_stop) {
1118 		ret = sdw_bus_wait_for_clk_prep_deprep(bus,
1119 						       SDW_BROADCAST_DEV_NUM);
1120 		/*
1121 		 * if there are no Slave devices present and the reply is
1122 		 * Command_Ignored/-ENODATA, we don't need to continue with the
1123 		 * flow and can just return here. The error code is not modified
1124 		 * and its handling left as an exercise for the caller.
1125 		 */
1126 		if (ret < 0)
1127 			return ret;
1128 	}
1129 
1130 	/* Inform slaves that prep is done */
1131 	list_for_each_entry(slave, &bus->slaves, node) {
1132 		if (!slave->dev_num)
1133 			continue;
1134 
1135 		if (slave->status != SDW_SLAVE_ATTACHED &&
1136 		    slave->status != SDW_SLAVE_ALERT)
1137 			continue;
1138 
1139 		ret = sdw_slave_clk_stop_callback(slave,
1140 						  SDW_CLK_STOP_MODE0,
1141 						  SDW_CLK_POST_PREPARE);
1142 
1143 		if (ret < 0 && ret != -ENODATA) {
1144 			dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1145 			return ret;
1146 		}
1147 	}
1148 
1149 	return 0;
1150 }
1151 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1152 
1153 /**
1154  * sdw_bus_clk_stop: stop bus clock
1155  *
1156  * @bus: SDW bus instance
1157  *
1158  * After preparing the Slaves for clock stop, stop the clock by broadcasting
1159  * write to SCP_CTRL register.
1160  */
1161 int sdw_bus_clk_stop(struct sdw_bus *bus)
1162 {
1163 	int ret;
1164 
1165 	/*
1166 	 * broadcast clock stop now, attached Slaves will ACK this,
1167 	 * unattached will ignore
1168 	 */
1169 	ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1170 			       SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1171 	if (ret < 0) {
1172 		if (ret != -ENODATA)
1173 			dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
1174 		return ret;
1175 	}
1176 
1177 	return 0;
1178 }
1179 EXPORT_SYMBOL(sdw_bus_clk_stop);
1180 
1181 /**
1182  * sdw_bus_exit_clk_stop: Exit clock stop mode
1183  *
1184  * @bus: SDW bus instance
1185  *
1186  * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1187  * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1188  * back.
1189  */
1190 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1191 {
1192 	bool simple_clk_stop = true;
1193 	struct sdw_slave *slave;
1194 	bool is_slave = false;
1195 	int ret;
1196 
1197 	/*
1198 	 * In order to save on transition time, de-prepare
1199 	 * each Slave and then wait for all Slave(s) to be
1200 	 * de-prepared after clock resume.
1201 	 */
1202 	list_for_each_entry(slave, &bus->slaves, node) {
1203 		if (!slave->dev_num)
1204 			continue;
1205 
1206 		if (slave->status != SDW_SLAVE_ATTACHED &&
1207 		    slave->status != SDW_SLAVE_ALERT)
1208 			continue;
1209 
1210 		/* Identify if Slave(s) are available on Bus */
1211 		is_slave = true;
1212 
1213 		ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1214 						  SDW_CLK_PRE_DEPREPARE);
1215 		if (ret < 0)
1216 			dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
1217 
1218 		/* Only de-prepare a Slave device if needed */
1219 		if (!slave->prop.simple_clk_stop_capable) {
1220 			simple_clk_stop = false;
1221 
1222 			ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1223 							 false);
1224 
1225 			if (ret < 0)
1226 				dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
1227 		}
1228 	}
1229 
1230 	/* Skip remaining clock stop de-preparation if no Slave is attached */
1231 	if (!is_slave)
1232 		return 0;
1233 
1234 	/*
1235 	 * Don't wait for all Slaves to be ready if they follow the simple
1236 	 * state machine
1237 	 */
1238 	if (!simple_clk_stop) {
1239 		ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1240 		if (ret < 0)
1241 			dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
1242 	}
1243 
1244 	list_for_each_entry(slave, &bus->slaves, node) {
1245 		if (!slave->dev_num)
1246 			continue;
1247 
1248 		if (slave->status != SDW_SLAVE_ATTACHED &&
1249 		    slave->status != SDW_SLAVE_ALERT)
1250 			continue;
1251 
1252 		ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1253 						  SDW_CLK_POST_DEPREPARE);
1254 		if (ret < 0)
1255 			dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
1256 	}
1257 
1258 	return 0;
1259 }
1260 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1261 
1262 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1263 			   int port, bool enable, int mask)
1264 {
1265 	u32 addr;
1266 	int ret;
1267 	u8 val = 0;
1268 
1269 	if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1270 		dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1271 			enable ? "on" : "off");
1272 		mask |= SDW_DPN_INT_TEST_FAIL;
1273 	}
1274 
1275 	addr = SDW_DPN_INTMASK(port);
1276 
1277 	/* Set/Clear port ready interrupt mask */
1278 	if (enable) {
1279 		val |= mask;
1280 		val |= SDW_DPN_INT_PORT_READY;
1281 	} else {
1282 		val &= ~(mask);
1283 		val &= ~SDW_DPN_INT_PORT_READY;
1284 	}
1285 
1286 	ret = sdw_update_no_pm(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1287 	if (ret < 0)
1288 		dev_err(&slave->dev,
1289 			"SDW_DPN_INTMASK write failed:%d\n", val);
1290 
1291 	return ret;
1292 }
1293 
1294 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1295 {
1296 	u32 mclk_freq = slave->bus->prop.mclk_freq;
1297 	u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1298 	unsigned int scale;
1299 	u8 scale_index;
1300 	u8 base;
1301 	int ret;
1302 
1303 	/*
1304 	 * frequency base and scale registers are required for SDCA
1305 	 * devices. They may also be used for 1.2+/non-SDCA devices.
1306 	 * Driver can set the property, we will need a DisCo property
1307 	 * to discover this case from platform firmware.
1308 	 */
1309 	if (!slave->id.class_id && !slave->prop.clock_reg_supported)
1310 		return 0;
1311 
1312 	if (!mclk_freq) {
1313 		dev_err(&slave->dev,
1314 			"no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1315 		return -EINVAL;
1316 	}
1317 
1318 	/*
1319 	 * map base frequency using Table 89 of SoundWire 1.2 spec.
1320 	 * The order of the tests just follows the specification, this
1321 	 * is not a selection between possible values or a search for
1322 	 * the best value but just a mapping.  Only one case per platform
1323 	 * is relevant.
1324 	 * Some BIOS have inconsistent values for mclk_freq but a
1325 	 * correct root so we force the mclk_freq to avoid variations.
1326 	 */
1327 	if (!(19200000 % mclk_freq)) {
1328 		mclk_freq = 19200000;
1329 		base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1330 	} else if (!(24000000 % mclk_freq)) {
1331 		mclk_freq = 24000000;
1332 		base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1333 	} else if (!(24576000 % mclk_freq)) {
1334 		mclk_freq = 24576000;
1335 		base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1336 	} else if (!(22579200 % mclk_freq)) {
1337 		mclk_freq = 22579200;
1338 		base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1339 	} else if (!(32000000 % mclk_freq)) {
1340 		mclk_freq = 32000000;
1341 		base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1342 	} else {
1343 		dev_err(&slave->dev,
1344 			"Unsupported clock base, mclk %d\n",
1345 			mclk_freq);
1346 		return -EINVAL;
1347 	}
1348 
1349 	if (mclk_freq % curr_freq) {
1350 		dev_err(&slave->dev,
1351 			"mclk %d is not multiple of bus curr_freq %d\n",
1352 			mclk_freq, curr_freq);
1353 		return -EINVAL;
1354 	}
1355 
1356 	scale = mclk_freq / curr_freq;
1357 
1358 	/*
1359 	 * map scale to Table 90 of SoundWire 1.2 spec - and check
1360 	 * that the scale is a power of two and maximum 64
1361 	 */
1362 	scale_index = ilog2(scale);
1363 
1364 	if (BIT(scale_index) != scale || scale_index > 6) {
1365 		dev_err(&slave->dev,
1366 			"No match found for scale %d, bus mclk %d curr_freq %d\n",
1367 			scale, mclk_freq, curr_freq);
1368 		return -EINVAL;
1369 	}
1370 	scale_index++;
1371 
1372 	ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1373 	if (ret < 0) {
1374 		dev_err(&slave->dev,
1375 			"SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1376 		return ret;
1377 	}
1378 
1379 	/* initialize scale for both banks */
1380 	ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1381 	if (ret < 0) {
1382 		dev_err(&slave->dev,
1383 			"SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1384 		return ret;
1385 	}
1386 	ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1387 	if (ret < 0)
1388 		dev_err(&slave->dev,
1389 			"SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1390 
1391 	dev_dbg(&slave->dev,
1392 		"Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1393 		base, scale_index, mclk_freq, curr_freq);
1394 
1395 	return ret;
1396 }
1397 
1398 static int sdw_initialize_slave(struct sdw_slave *slave)
1399 {
1400 	struct sdw_slave_prop *prop = &slave->prop;
1401 	int status;
1402 	int ret;
1403 	u8 val;
1404 
1405 	ret = sdw_slave_set_frequency(slave);
1406 	if (ret < 0)
1407 		return ret;
1408 
1409 	if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1410 		/* Clear bus clash interrupt before enabling interrupt mask */
1411 		status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1412 		if (status < 0) {
1413 			dev_err(&slave->dev,
1414 				"SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1415 			return status;
1416 		}
1417 		if (status & SDW_SCP_INT1_BUS_CLASH) {
1418 			dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1419 			ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1420 			if (ret < 0) {
1421 				dev_err(&slave->dev,
1422 					"SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1423 				return ret;
1424 			}
1425 		}
1426 	}
1427 	if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1428 	    !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1429 		/* Clear parity interrupt before enabling interrupt mask */
1430 		status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1431 		if (status < 0) {
1432 			dev_err(&slave->dev,
1433 				"SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1434 			return status;
1435 		}
1436 		if (status & SDW_SCP_INT1_PARITY) {
1437 			dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1438 			ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1439 			if (ret < 0) {
1440 				dev_err(&slave->dev,
1441 					"SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1442 				return ret;
1443 			}
1444 		}
1445 	}
1446 
1447 	/*
1448 	 * Set SCP_INT1_MASK register, typically bus clash and
1449 	 * implementation-defined interrupt mask. The Parity detection
1450 	 * may not always be correct on startup so its use is
1451 	 * device-dependent, it might e.g. only be enabled in
1452 	 * steady-state after a couple of frames.
1453 	 */
1454 	val = slave->prop.scp_int1_mask;
1455 
1456 	/* Enable SCP interrupts */
1457 	ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1458 	if (ret < 0) {
1459 		dev_err(&slave->dev,
1460 			"SDW_SCP_INTMASK1 write failed:%d\n", ret);
1461 		return ret;
1462 	}
1463 
1464 	/* No need to continue if DP0 is not present */
1465 	if (!slave->prop.dp0_prop)
1466 		return 0;
1467 
1468 	/* Enable DP0 interrupts */
1469 	val = prop->dp0_prop->imp_def_interrupts;
1470 	val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1471 
1472 	ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1473 	if (ret < 0)
1474 		dev_err(&slave->dev,
1475 			"SDW_DP0_INTMASK read failed:%d\n", ret);
1476 	return ret;
1477 }
1478 
1479 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1480 {
1481 	u8 clear, impl_int_mask;
1482 	int status, status2, ret, count = 0;
1483 
1484 	status = sdw_read_no_pm(slave, SDW_DP0_INT);
1485 	if (status < 0) {
1486 		dev_err(&slave->dev,
1487 			"SDW_DP0_INT read failed:%d\n", status);
1488 		return status;
1489 	}
1490 
1491 	do {
1492 		clear = status & ~SDW_DP0_INTERRUPTS;
1493 
1494 		if (status & SDW_DP0_INT_TEST_FAIL) {
1495 			dev_err(&slave->dev, "Test fail for port 0\n");
1496 			clear |= SDW_DP0_INT_TEST_FAIL;
1497 		}
1498 
1499 		/*
1500 		 * Assumption: PORT_READY interrupt will be received only for
1501 		 * ports implementing Channel Prepare state machine (CP_SM)
1502 		 */
1503 
1504 		if (status & SDW_DP0_INT_PORT_READY) {
1505 			complete(&slave->port_ready[0]);
1506 			clear |= SDW_DP0_INT_PORT_READY;
1507 		}
1508 
1509 		if (status & SDW_DP0_INT_BRA_FAILURE) {
1510 			dev_err(&slave->dev, "BRA failed\n");
1511 			clear |= SDW_DP0_INT_BRA_FAILURE;
1512 		}
1513 
1514 		impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1515 			SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1516 
1517 		if (status & impl_int_mask) {
1518 			clear |= impl_int_mask;
1519 			*slave_status = clear;
1520 		}
1521 
1522 		/* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
1523 		ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
1524 		if (ret < 0) {
1525 			dev_err(&slave->dev,
1526 				"SDW_DP0_INT write failed:%d\n", ret);
1527 			return ret;
1528 		}
1529 
1530 		/* Read DP0 interrupt again */
1531 		status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
1532 		if (status2 < 0) {
1533 			dev_err(&slave->dev,
1534 				"SDW_DP0_INT read failed:%d\n", status2);
1535 			return status2;
1536 		}
1537 		/* filter to limit loop to interrupts identified in the first status read */
1538 		status &= status2;
1539 
1540 		count++;
1541 
1542 		/* we can get alerts while processing so keep retrying */
1543 	} while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1544 
1545 	if (count == SDW_READ_INTR_CLEAR_RETRY)
1546 		dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
1547 
1548 	return ret;
1549 }
1550 
1551 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1552 				     int port, u8 *slave_status)
1553 {
1554 	u8 clear, impl_int_mask;
1555 	int status, status2, ret, count = 0;
1556 	u32 addr;
1557 
1558 	if (port == 0)
1559 		return sdw_handle_dp0_interrupt(slave, slave_status);
1560 
1561 	addr = SDW_DPN_INT(port);
1562 	status = sdw_read_no_pm(slave, addr);
1563 	if (status < 0) {
1564 		dev_err(&slave->dev,
1565 			"SDW_DPN_INT read failed:%d\n", status);
1566 
1567 		return status;
1568 	}
1569 
1570 	do {
1571 		clear = status & ~SDW_DPN_INTERRUPTS;
1572 
1573 		if (status & SDW_DPN_INT_TEST_FAIL) {
1574 			dev_err(&slave->dev, "Test fail for port:%d\n", port);
1575 			clear |= SDW_DPN_INT_TEST_FAIL;
1576 		}
1577 
1578 		/*
1579 		 * Assumption: PORT_READY interrupt will be received only
1580 		 * for ports implementing CP_SM.
1581 		 */
1582 		if (status & SDW_DPN_INT_PORT_READY) {
1583 			complete(&slave->port_ready[port]);
1584 			clear |= SDW_DPN_INT_PORT_READY;
1585 		}
1586 
1587 		impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1588 			SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1589 
1590 		if (status & impl_int_mask) {
1591 			clear |= impl_int_mask;
1592 			*slave_status = clear;
1593 		}
1594 
1595 		/* clear the interrupt but don't touch reserved fields */
1596 		ret = sdw_write_no_pm(slave, addr, clear);
1597 		if (ret < 0) {
1598 			dev_err(&slave->dev,
1599 				"SDW_DPN_INT write failed:%d\n", ret);
1600 			return ret;
1601 		}
1602 
1603 		/* Read DPN interrupt again */
1604 		status2 = sdw_read_no_pm(slave, addr);
1605 		if (status2 < 0) {
1606 			dev_err(&slave->dev,
1607 				"SDW_DPN_INT read failed:%d\n", status2);
1608 			return status2;
1609 		}
1610 		/* filter to limit loop to interrupts identified in the first status read */
1611 		status &= status2;
1612 
1613 		count++;
1614 
1615 		/* we can get alerts while processing so keep retrying */
1616 	} while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1617 
1618 	if (count == SDW_READ_INTR_CLEAR_RETRY)
1619 		dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
1620 
1621 	return ret;
1622 }
1623 
1624 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1625 {
1626 	struct sdw_slave_intr_status slave_intr;
1627 	u8 clear = 0, bit, port_status[15] = {0};
1628 	int port_num, stat, ret, count = 0;
1629 	unsigned long port;
1630 	bool slave_notify;
1631 	u8 sdca_cascade = 0;
1632 	u8 buf, buf2[2];
1633 	bool parity_check;
1634 	bool parity_quirk;
1635 
1636 	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1637 
1638 	ret = pm_runtime_get_sync(&slave->dev);
1639 	if (ret < 0 && ret != -EACCES) {
1640 		dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1641 		pm_runtime_put_noidle(&slave->dev);
1642 		return ret;
1643 	}
1644 
1645 	/* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1646 	ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1647 	if (ret < 0) {
1648 		dev_err(&slave->dev,
1649 			"SDW_SCP_INT1 read failed:%d\n", ret);
1650 		goto io_err;
1651 	}
1652 	buf = ret;
1653 
1654 	ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1655 	if (ret < 0) {
1656 		dev_err(&slave->dev,
1657 			"SDW_SCP_INT2/3 read failed:%d\n", ret);
1658 		goto io_err;
1659 	}
1660 
1661 	if (slave->id.class_id) {
1662 		ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1663 		if (ret < 0) {
1664 			dev_err(&slave->dev,
1665 				"SDW_DP0_INT read failed:%d\n", ret);
1666 			goto io_err;
1667 		}
1668 		sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1669 	}
1670 
1671 	do {
1672 		slave_notify = false;
1673 
1674 		/*
1675 		 * Check parity, bus clash and Slave (impl defined)
1676 		 * interrupt
1677 		 */
1678 		if (buf & SDW_SCP_INT1_PARITY) {
1679 			parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1680 			parity_quirk = !slave->first_interrupt_done &&
1681 				(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1682 
1683 			if (parity_check && !parity_quirk)
1684 				dev_err(&slave->dev, "Parity error detected\n");
1685 			clear |= SDW_SCP_INT1_PARITY;
1686 		}
1687 
1688 		if (buf & SDW_SCP_INT1_BUS_CLASH) {
1689 			if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1690 				dev_err(&slave->dev, "Bus clash detected\n");
1691 			clear |= SDW_SCP_INT1_BUS_CLASH;
1692 		}
1693 
1694 		/*
1695 		 * When bus clash or parity errors are detected, such errors
1696 		 * are unlikely to be recoverable errors.
1697 		 * TODO: In such scenario, reset bus. Make this configurable
1698 		 * via sysfs property with bus reset being the default.
1699 		 */
1700 
1701 		if (buf & SDW_SCP_INT1_IMPL_DEF) {
1702 			if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1703 				dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1704 				slave_notify = true;
1705 			}
1706 			clear |= SDW_SCP_INT1_IMPL_DEF;
1707 		}
1708 
1709 		/* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1710 		if (sdca_cascade)
1711 			slave_notify = true;
1712 
1713 		/* Check port 0 - 3 interrupts */
1714 		port = buf & SDW_SCP_INT1_PORT0_3;
1715 
1716 		/* To get port number corresponding to bits, shift it */
1717 		port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1718 		for_each_set_bit(bit, &port, 8) {
1719 			sdw_handle_port_interrupt(slave, bit,
1720 						  &port_status[bit]);
1721 		}
1722 
1723 		/* Check if cascade 2 interrupt is present */
1724 		if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1725 			port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1726 			for_each_set_bit(bit, &port, 8) {
1727 				/* scp2 ports start from 4 */
1728 				port_num = bit + 4;
1729 				sdw_handle_port_interrupt(slave,
1730 						port_num,
1731 						&port_status[port_num]);
1732 			}
1733 		}
1734 
1735 		/* now check last cascade */
1736 		if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1737 			port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1738 			for_each_set_bit(bit, &port, 8) {
1739 				/* scp3 ports start from 11 */
1740 				port_num = bit + 11;
1741 				sdw_handle_port_interrupt(slave,
1742 						port_num,
1743 						&port_status[port_num]);
1744 			}
1745 		}
1746 
1747 		/* Update the Slave driver */
1748 		if (slave_notify) {
1749 			mutex_lock(&slave->sdw_dev_lock);
1750 
1751 			if (slave->probed) {
1752 				struct device *dev = &slave->dev;
1753 				struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1754 
1755 				if (slave->prop.use_domain_irq && slave->irq)
1756 					handle_nested_irq(slave->irq);
1757 
1758 				if (drv->ops && drv->ops->interrupt_callback) {
1759 					slave_intr.sdca_cascade = sdca_cascade;
1760 					slave_intr.control_port = clear;
1761 					memcpy(slave_intr.port, &port_status,
1762 					       sizeof(slave_intr.port));
1763 
1764 					drv->ops->interrupt_callback(slave, &slave_intr);
1765 				}
1766 			}
1767 
1768 			mutex_unlock(&slave->sdw_dev_lock);
1769 		}
1770 
1771 		/* Ack interrupt */
1772 		ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
1773 		if (ret < 0) {
1774 			dev_err(&slave->dev,
1775 				"SDW_SCP_INT1 write failed:%d\n", ret);
1776 			goto io_err;
1777 		}
1778 
1779 		/* at this point all initial interrupt sources were handled */
1780 		slave->first_interrupt_done = true;
1781 
1782 		/*
1783 		 * Read status again to ensure no new interrupts arrived
1784 		 * while servicing interrupts.
1785 		 */
1786 		ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1787 		if (ret < 0) {
1788 			dev_err(&slave->dev,
1789 				"SDW_SCP_INT1 recheck read failed:%d\n", ret);
1790 			goto io_err;
1791 		}
1792 		buf = ret;
1793 
1794 		ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1795 		if (ret < 0) {
1796 			dev_err(&slave->dev,
1797 				"SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
1798 			goto io_err;
1799 		}
1800 
1801 		if (slave->id.class_id) {
1802 			ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1803 			if (ret < 0) {
1804 				dev_err(&slave->dev,
1805 					"SDW_DP0_INT recheck read failed:%d\n", ret);
1806 				goto io_err;
1807 			}
1808 			sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1809 		}
1810 
1811 		/*
1812 		 * Make sure no interrupts are pending
1813 		 */
1814 		stat = buf || buf2[0] || buf2[1] || sdca_cascade;
1815 
1816 		/*
1817 		 * Exit loop if Slave is continuously in ALERT state even
1818 		 * after servicing the interrupt multiple times.
1819 		 */
1820 		count++;
1821 
1822 		/* we can get alerts while processing so keep retrying */
1823 	} while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1824 
1825 	if (count == SDW_READ_INTR_CLEAR_RETRY)
1826 		dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
1827 
1828 io_err:
1829 	pm_runtime_mark_last_busy(&slave->dev);
1830 	pm_runtime_put_autosuspend(&slave->dev);
1831 
1832 	return ret;
1833 }
1834 
1835 static int sdw_update_slave_status(struct sdw_slave *slave,
1836 				   enum sdw_slave_status status)
1837 {
1838 	int ret = 0;
1839 
1840 	mutex_lock(&slave->sdw_dev_lock);
1841 
1842 	if (slave->probed) {
1843 		struct device *dev = &slave->dev;
1844 		struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1845 
1846 		if (drv->ops && drv->ops->update_status)
1847 			ret = drv->ops->update_status(slave, status);
1848 	}
1849 
1850 	mutex_unlock(&slave->sdw_dev_lock);
1851 
1852 	return ret;
1853 }
1854 
1855 /**
1856  * sdw_handle_slave_status() - Handle Slave status
1857  * @bus: SDW bus instance
1858  * @status: Status for all Slave(s)
1859  */
1860 int sdw_handle_slave_status(struct sdw_bus *bus,
1861 			    enum sdw_slave_status status[])
1862 {
1863 	enum sdw_slave_status prev_status;
1864 	struct sdw_slave *slave;
1865 	bool attached_initializing, id_programmed;
1866 	int i, ret = 0;
1867 
1868 	/* first check if any Slaves fell off the bus */
1869 	for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1870 		mutex_lock(&bus->bus_lock);
1871 		if (test_bit(i, bus->assigned) == false) {
1872 			mutex_unlock(&bus->bus_lock);
1873 			continue;
1874 		}
1875 		mutex_unlock(&bus->bus_lock);
1876 
1877 		slave = sdw_get_slave(bus, i);
1878 		if (!slave)
1879 			continue;
1880 
1881 		if (status[i] == SDW_SLAVE_UNATTACHED &&
1882 		    slave->status != SDW_SLAVE_UNATTACHED) {
1883 			dev_warn(&slave->dev, "Slave %d state check1: UNATTACHED, status was %d\n",
1884 				 i, slave->status);
1885 			sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1886 
1887 			/* Ensure driver knows that peripheral unattached */
1888 			ret = sdw_update_slave_status(slave, status[i]);
1889 			if (ret < 0)
1890 				dev_warn(&slave->dev, "Update Slave status failed:%d\n", ret);
1891 		}
1892 	}
1893 
1894 	if (status[0] == SDW_SLAVE_ATTACHED) {
1895 		dev_dbg(bus->dev, "Slave attached, programming device number\n");
1896 
1897 		/*
1898 		 * Programming a device number will have side effects,
1899 		 * so we deal with other devices at a later time.
1900 		 * This relies on those devices reporting ATTACHED, which will
1901 		 * trigger another call to this function. This will only
1902 		 * happen if at least one device ID was programmed.
1903 		 * Error returns from sdw_program_device_num() are currently
1904 		 * ignored because there's no useful recovery that can be done.
1905 		 * Returning the error here could result in the current status
1906 		 * of other devices not being handled, because if no device IDs
1907 		 * were programmed there's nothing to guarantee a status change
1908 		 * to trigger another call to this function.
1909 		 */
1910 		sdw_program_device_num(bus, &id_programmed);
1911 		if (id_programmed)
1912 			return 0;
1913 	}
1914 
1915 	/* Continue to check other slave statuses */
1916 	for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1917 		mutex_lock(&bus->bus_lock);
1918 		if (test_bit(i, bus->assigned) == false) {
1919 			mutex_unlock(&bus->bus_lock);
1920 			continue;
1921 		}
1922 		mutex_unlock(&bus->bus_lock);
1923 
1924 		slave = sdw_get_slave(bus, i);
1925 		if (!slave)
1926 			continue;
1927 
1928 		attached_initializing = false;
1929 
1930 		switch (status[i]) {
1931 		case SDW_SLAVE_UNATTACHED:
1932 			if (slave->status == SDW_SLAVE_UNATTACHED)
1933 				break;
1934 
1935 			dev_warn(&slave->dev, "Slave %d state check2: UNATTACHED, status was %d\n",
1936 				 i, slave->status);
1937 
1938 			sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1939 			break;
1940 
1941 		case SDW_SLAVE_ALERT:
1942 			ret = sdw_handle_slave_alerts(slave);
1943 			if (ret < 0)
1944 				dev_err(&slave->dev,
1945 					"Slave %d alert handling failed: %d\n",
1946 					i, ret);
1947 			break;
1948 
1949 		case SDW_SLAVE_ATTACHED:
1950 			if (slave->status == SDW_SLAVE_ATTACHED)
1951 				break;
1952 
1953 			prev_status = slave->status;
1954 			sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1955 
1956 			if (prev_status == SDW_SLAVE_ALERT)
1957 				break;
1958 
1959 			attached_initializing = true;
1960 
1961 			ret = sdw_initialize_slave(slave);
1962 			if (ret < 0)
1963 				dev_err(&slave->dev,
1964 					"Slave %d initialization failed: %d\n",
1965 					i, ret);
1966 
1967 			break;
1968 
1969 		default:
1970 			dev_err(&slave->dev, "Invalid slave %d status:%d\n",
1971 				i, status[i]);
1972 			break;
1973 		}
1974 
1975 		ret = sdw_update_slave_status(slave, status[i]);
1976 		if (ret < 0)
1977 			dev_err(&slave->dev,
1978 				"Update Slave status failed:%d\n", ret);
1979 		if (attached_initializing) {
1980 			dev_dbg(&slave->dev,
1981 				"signaling initialization completion for Slave %d\n",
1982 				slave->dev_num);
1983 
1984 			complete_all(&slave->initialization_complete);
1985 
1986 			/*
1987 			 * If the manager became pm_runtime active, the peripherals will be
1988 			 * restarted and attach, but their pm_runtime status may remain
1989 			 * suspended. If the 'update_slave_status' callback initiates
1990 			 * any sort of deferred processing, this processing would not be
1991 			 * cancelled on pm_runtime suspend.
1992 			 * To avoid such zombie states, we queue a request to resume.
1993 			 * This would be a no-op in case the peripheral was being resumed
1994 			 * by e.g. the ALSA/ASoC framework.
1995 			 */
1996 			pm_request_resume(&slave->dev);
1997 		}
1998 	}
1999 
2000 	return ret;
2001 }
2002 EXPORT_SYMBOL(sdw_handle_slave_status);
2003 
2004 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
2005 {
2006 	struct sdw_slave *slave;
2007 	int i;
2008 
2009 	/* Check all non-zero devices */
2010 	for (i = 1; i <= SDW_MAX_DEVICES; i++) {
2011 		mutex_lock(&bus->bus_lock);
2012 		if (test_bit(i, bus->assigned) == false) {
2013 			mutex_unlock(&bus->bus_lock);
2014 			continue;
2015 		}
2016 		mutex_unlock(&bus->bus_lock);
2017 
2018 		slave = sdw_get_slave(bus, i);
2019 		if (!slave)
2020 			continue;
2021 
2022 		if (slave->status != SDW_SLAVE_UNATTACHED) {
2023 			sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
2024 			slave->first_interrupt_done = false;
2025 			sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
2026 		}
2027 
2028 		/* keep track of request, used in pm_runtime resume */
2029 		slave->unattach_request = request;
2030 	}
2031 }
2032 EXPORT_SYMBOL(sdw_clear_slave_status);
2033