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