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