xref: /openbmc/linux/drivers/soundwire/bus.c (revision 2359ccdd)
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/mod_devicetable.h>
6 #include <linux/pm_runtime.h>
7 #include <linux/soundwire/sdw_registers.h>
8 #include <linux/soundwire/sdw.h>
9 #include "bus.h"
10 
11 /**
12  * sdw_add_bus_master() - add a bus Master instance
13  * @bus: bus instance
14  *
15  * Initializes the bus instance, read properties and create child
16  * devices.
17  */
18 int sdw_add_bus_master(struct sdw_bus *bus)
19 {
20 	int ret;
21 
22 	if (!bus->dev) {
23 		pr_err("SoundWire bus has no device");
24 		return -ENODEV;
25 	}
26 
27 	if (!bus->ops) {
28 		dev_err(bus->dev, "SoundWire Bus ops are not set");
29 		return -EINVAL;
30 	}
31 
32 	mutex_init(&bus->msg_lock);
33 	mutex_init(&bus->bus_lock);
34 	INIT_LIST_HEAD(&bus->slaves);
35 
36 	if (bus->ops->read_prop) {
37 		ret = bus->ops->read_prop(bus);
38 		if (ret < 0) {
39 			dev_err(bus->dev, "Bus read properties failed:%d", ret);
40 			return ret;
41 		}
42 	}
43 
44 	/*
45 	 * Device numbers in SoundWire are 0 thru 15. Enumeration device
46 	 * number (0), Broadcast device number (15), Group numbers (12 and
47 	 * 13) and Master device number (14) are not used for assignment so
48 	 * mask these and other higher bits.
49 	 */
50 
51 	/* Set higher order bits */
52 	*bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
53 
54 	/* Set enumuration device number and broadcast device number */
55 	set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
56 	set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
57 
58 	/* Set group device numbers and master device number */
59 	set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
60 	set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
61 	set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
62 
63 	/*
64 	 * SDW is an enumerable bus, but devices can be powered off. So,
65 	 * they won't be able to report as present.
66 	 *
67 	 * Create Slave devices based on Slaves described in
68 	 * the respective firmware (ACPI/DT)
69 	 */
70 	if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
71 		ret = sdw_acpi_find_slaves(bus);
72 	else
73 		ret = -ENOTSUPP; /* No ACPI/DT so error out */
74 
75 	if (ret) {
76 		dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
77 		return ret;
78 	}
79 
80 	return 0;
81 }
82 EXPORT_SYMBOL(sdw_add_bus_master);
83 
84 static int sdw_delete_slave(struct device *dev, void *data)
85 {
86 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
87 	struct sdw_bus *bus = slave->bus;
88 
89 	mutex_lock(&bus->bus_lock);
90 
91 	if (slave->dev_num) /* clear dev_num if assigned */
92 		clear_bit(slave->dev_num, bus->assigned);
93 
94 	list_del_init(&slave->node);
95 	mutex_unlock(&bus->bus_lock);
96 
97 	device_unregister(dev);
98 	return 0;
99 }
100 
101 /**
102  * sdw_delete_bus_master() - delete the bus master instance
103  * @bus: bus to be deleted
104  *
105  * Remove the instance, delete the child devices.
106  */
107 void sdw_delete_bus_master(struct sdw_bus *bus)
108 {
109 	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
110 }
111 EXPORT_SYMBOL(sdw_delete_bus_master);
112 
113 /*
114  * SDW IO Calls
115  */
116 
117 static inline int find_response_code(enum sdw_command_response resp)
118 {
119 	switch (resp) {
120 	case SDW_CMD_OK:
121 		return 0;
122 
123 	case SDW_CMD_IGNORED:
124 		return -ENODATA;
125 
126 	case SDW_CMD_TIMEOUT:
127 		return -ETIMEDOUT;
128 
129 	default:
130 		return -EIO;
131 	}
132 }
133 
134 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
135 {
136 	int retry = bus->prop.err_threshold;
137 	enum sdw_command_response resp;
138 	int ret = 0, i;
139 
140 	for (i = 0; i <= retry; i++) {
141 		resp = bus->ops->xfer_msg(bus, msg);
142 		ret = find_response_code(resp);
143 
144 		/* if cmd is ok or ignored return */
145 		if (ret == 0 || ret == -ENODATA)
146 			return ret;
147 	}
148 
149 	return ret;
150 }
151 
152 static inline int do_transfer_defer(struct sdw_bus *bus,
153 			struct sdw_msg *msg, struct sdw_defer *defer)
154 {
155 	int retry = bus->prop.err_threshold;
156 	enum sdw_command_response resp;
157 	int ret = 0, i;
158 
159 	defer->msg = msg;
160 	defer->length = msg->len;
161 
162 	for (i = 0; i <= retry; i++) {
163 		resp = bus->ops->xfer_msg_defer(bus, msg, defer);
164 		ret = find_response_code(resp);
165 		/* if cmd is ok or ignored return */
166 		if (ret == 0 || ret == -ENODATA)
167 			return ret;
168 	}
169 
170 	return ret;
171 }
172 
173 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
174 {
175 	int retry = bus->prop.err_threshold;
176 	enum sdw_command_response resp;
177 	int ret = 0, i;
178 
179 	for (i = 0; i <= retry; i++) {
180 		resp = bus->ops->reset_page_addr(bus, dev_num);
181 		ret = find_response_code(resp);
182 		/* if cmd is ok or ignored return */
183 		if (ret == 0 || ret == -ENODATA)
184 			return ret;
185 	}
186 
187 	return ret;
188 }
189 
190 /**
191  * sdw_transfer() - Synchronous transfer message to a SDW Slave device
192  * @bus: SDW bus
193  * @msg: SDW message to be xfered
194  */
195 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
196 {
197 	int ret;
198 
199 	mutex_lock(&bus->msg_lock);
200 
201 	ret = do_transfer(bus, msg);
202 	if (ret != 0 && ret != -ENODATA)
203 		dev_err(bus->dev, "trf on Slave %d failed:%d\n",
204 				msg->dev_num, ret);
205 
206 	if (msg->page)
207 		sdw_reset_page(bus, msg->dev_num);
208 
209 	mutex_unlock(&bus->msg_lock);
210 
211 	return ret;
212 }
213 
214 /**
215  * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
216  * @bus: SDW bus
217  * @msg: SDW message to be xfered
218  * @defer: Defer block for signal completion
219  *
220  * Caller needs to hold the msg_lock lock while calling this
221  */
222 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
223 				struct sdw_defer *defer)
224 {
225 	int ret;
226 
227 	if (!bus->ops->xfer_msg_defer)
228 		return -ENOTSUPP;
229 
230 	ret = do_transfer_defer(bus, msg, defer);
231 	if (ret != 0 && ret != -ENODATA)
232 		dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
233 				msg->dev_num, ret);
234 
235 	if (msg->page)
236 		sdw_reset_page(bus, msg->dev_num);
237 
238 	return ret;
239 }
240 
241 
242 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
243 		u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
244 {
245 	memset(msg, 0, sizeof(*msg));
246 	msg->addr = addr; /* addr is 16 bit and truncated here */
247 	msg->len = count;
248 	msg->dev_num = dev_num;
249 	msg->flags = flags;
250 	msg->buf = buf;
251 	msg->ssp_sync = false;
252 	msg->page = false;
253 
254 	if (addr < SDW_REG_NO_PAGE) { /* no paging area */
255 		return 0;
256 	} else if (addr >= SDW_REG_MAX) { /* illegal addr */
257 		pr_err("SDW: Invalid address %x passed\n", addr);
258 		return -EINVAL;
259 	}
260 
261 	if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
262 		if (slave && !slave->prop.paging_support)
263 			return 0;
264 		/* no need for else as that will fall thru to paging */
265 	}
266 
267 	/* paging mandatory */
268 	if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
269 		pr_err("SDW: Invalid device for paging :%d\n", dev_num);
270 		return -EINVAL;
271 	}
272 
273 	if (!slave) {
274 		pr_err("SDW: No slave for paging addr\n");
275 		return -EINVAL;
276 	} else if (!slave->prop.paging_support) {
277 		dev_err(&slave->dev,
278 			"address %x needs paging but no support", addr);
279 		return -EINVAL;
280 	}
281 
282 	msg->addr_page1 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE1_MASK));
283 	msg->addr_page2 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE2_MASK));
284 	msg->addr |= BIT(15);
285 	msg->page = true;
286 
287 	return 0;
288 }
289 
290 /**
291  * sdw_nread() - Read "n" contiguous SDW Slave registers
292  * @slave: SDW Slave
293  * @addr: Register address
294  * @count: length
295  * @val: Buffer for values to be read
296  */
297 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
298 {
299 	struct sdw_msg msg;
300 	int ret;
301 
302 	ret = sdw_fill_msg(&msg, slave, addr, count,
303 			slave->dev_num, SDW_MSG_FLAG_READ, val);
304 	if (ret < 0)
305 		return ret;
306 
307 	ret = pm_runtime_get_sync(slave->bus->dev);
308 	if (ret < 0)
309 		return ret;
310 
311 	ret = sdw_transfer(slave->bus, &msg);
312 	pm_runtime_put(slave->bus->dev);
313 
314 	return ret;
315 }
316 EXPORT_SYMBOL(sdw_nread);
317 
318 /**
319  * sdw_nwrite() - Write "n" contiguous SDW Slave registers
320  * @slave: SDW Slave
321  * @addr: Register address
322  * @count: length
323  * @val: Buffer for values to be read
324  */
325 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
326 {
327 	struct sdw_msg msg;
328 	int ret;
329 
330 	ret = sdw_fill_msg(&msg, slave, addr, count,
331 			slave->dev_num, SDW_MSG_FLAG_WRITE, val);
332 	if (ret < 0)
333 		return ret;
334 
335 	ret = pm_runtime_get_sync(slave->bus->dev);
336 	if (ret < 0)
337 		return ret;
338 
339 	ret = sdw_transfer(slave->bus, &msg);
340 	pm_runtime_put(slave->bus->dev);
341 
342 	return ret;
343 }
344 EXPORT_SYMBOL(sdw_nwrite);
345 
346 /**
347  * sdw_read() - Read a SDW Slave register
348  * @slave: SDW Slave
349  * @addr: Register address
350  */
351 int sdw_read(struct sdw_slave *slave, u32 addr)
352 {
353 	u8 buf;
354 	int ret;
355 
356 	ret = sdw_nread(slave, addr, 1, &buf);
357 	if (ret < 0)
358 		return ret;
359 	else
360 		return buf;
361 }
362 EXPORT_SYMBOL(sdw_read);
363 
364 /**
365  * sdw_write() - Write a SDW Slave register
366  * @slave: SDW Slave
367  * @addr: Register address
368  * @value: Register value
369  */
370 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
371 {
372 	return sdw_nwrite(slave, addr, 1, &value);
373 
374 }
375 EXPORT_SYMBOL(sdw_write);
376 
377 /*
378  * SDW alert handling
379  */
380 
381 /* called with bus_lock held */
382 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
383 {
384 	struct sdw_slave *slave = NULL;
385 
386 	list_for_each_entry(slave, &bus->slaves, node) {
387 		if (slave->dev_num == i)
388 			return slave;
389 	}
390 
391 	return NULL;
392 }
393 
394 static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
395 {
396 
397 	if ((slave->id.unique_id != id.unique_id) ||
398 	    (slave->id.mfg_id != id.mfg_id) ||
399 	    (slave->id.part_id != id.part_id) ||
400 	    (slave->id.class_id != id.class_id))
401 		return -ENODEV;
402 
403 	return 0;
404 }
405 
406 /* called with bus_lock held */
407 static int sdw_get_device_num(struct sdw_slave *slave)
408 {
409 	int bit;
410 
411 	bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
412 	if (bit == SDW_MAX_DEVICES) {
413 		bit = -ENODEV;
414 		goto err;
415 	}
416 
417 	/*
418 	 * Do not update dev_num in Slave data structure here,
419 	 * Update once program dev_num is successful
420 	 */
421 	set_bit(bit, slave->bus->assigned);
422 
423 err:
424 	return bit;
425 }
426 
427 static int sdw_assign_device_num(struct sdw_slave *slave)
428 {
429 	int ret, dev_num;
430 
431 	/* check first if device number is assigned, if so reuse that */
432 	if (!slave->dev_num) {
433 		mutex_lock(&slave->bus->bus_lock);
434 		dev_num = sdw_get_device_num(slave);
435 		mutex_unlock(&slave->bus->bus_lock);
436 		if (dev_num < 0) {
437 			dev_err(slave->bus->dev, "Get dev_num failed: %d",
438 								dev_num);
439 			return dev_num;
440 		}
441 	} else {
442 		dev_info(slave->bus->dev,
443 				"Slave already registered dev_num:%d",
444 				slave->dev_num);
445 
446 		/* Clear the slave->dev_num to transfer message on device 0 */
447 		dev_num = slave->dev_num;
448 		slave->dev_num = 0;
449 
450 	}
451 
452 	ret = sdw_write(slave, SDW_SCP_DEVNUMBER, dev_num);
453 	if (ret < 0) {
454 		dev_err(&slave->dev, "Program device_num failed: %d", ret);
455 		return ret;
456 	}
457 
458 	/* After xfer of msg, restore dev_num */
459 	slave->dev_num = dev_num;
460 
461 	return 0;
462 }
463 
464 void sdw_extract_slave_id(struct sdw_bus *bus,
465 			u64 addr, struct sdw_slave_id *id)
466 {
467 	dev_dbg(bus->dev, "SDW Slave Addr: %llx", addr);
468 
469 	/*
470 	 * Spec definition
471 	 *   Register		Bit	Contents
472 	 *   DevId_0 [7:4]	47:44	sdw_version
473 	 *   DevId_0 [3:0]	43:40	unique_id
474 	 *   DevId_1		39:32	mfg_id [15:8]
475 	 *   DevId_2		31:24	mfg_id [7:0]
476 	 *   DevId_3		23:16	part_id [15:8]
477 	 *   DevId_4		15:08	part_id [7:0]
478 	 *   DevId_5		07:00	class_id
479 	 */
480 	id->sdw_version = (addr >> 44) & GENMASK(3, 0);
481 	id->unique_id = (addr >> 40) & GENMASK(3, 0);
482 	id->mfg_id = (addr >> 24) & GENMASK(15, 0);
483 	id->part_id = (addr >> 8) & GENMASK(15, 0);
484 	id->class_id = addr & GENMASK(7, 0);
485 
486 	dev_dbg(bus->dev,
487 		"SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x",
488 				id->class_id, id->part_id, id->mfg_id,
489 				id->unique_id, id->sdw_version);
490 
491 }
492 
493 static int sdw_program_device_num(struct sdw_bus *bus)
494 {
495 	u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
496 	struct sdw_slave *slave, *_s;
497 	struct sdw_slave_id id;
498 	struct sdw_msg msg;
499 	bool found = false;
500 	int count = 0, ret;
501 	u64 addr;
502 
503 	/* No Slave, so use raw xfer api */
504 	ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
505 			SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
506 	if (ret < 0)
507 		return ret;
508 
509 	do {
510 		ret = sdw_transfer(bus, &msg);
511 		if (ret == -ENODATA) { /* end of device id reads */
512 			ret = 0;
513 			break;
514 		}
515 		if (ret < 0) {
516 			dev_err(bus->dev, "DEVID read fail:%d\n", ret);
517 			break;
518 		}
519 
520 		/*
521 		 * Construct the addr and extract. Cast the higher shift
522 		 * bits to avoid truncation due to size limit.
523 		 */
524 		addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
525 			((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
526 			((u64)buf[0] << 40);
527 
528 		sdw_extract_slave_id(bus, addr, &id);
529 
530 		/* Now compare with entries */
531 		list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
532 			if (sdw_compare_devid(slave, id) == 0) {
533 				found = true;
534 
535 				/*
536 				 * Assign a new dev_num to this Slave and
537 				 * not mark it present. It will be marked
538 				 * present after it reports ATTACHED on new
539 				 * dev_num
540 				 */
541 				ret = sdw_assign_device_num(slave);
542 				if (ret) {
543 					dev_err(slave->bus->dev,
544 						"Assign dev_num failed:%d",
545 						ret);
546 					return ret;
547 				}
548 
549 				break;
550 			}
551 		}
552 
553 		if (found == false) {
554 			/* TODO: Park this device in Group 13 */
555 			dev_err(bus->dev, "Slave Entry not found");
556 		}
557 
558 		count++;
559 
560 		/*
561 		 * Check till error out or retry (count) exhausts.
562 		 * Device can drop off and rejoin during enumeration
563 		 * so count till twice the bound.
564 		 */
565 
566 	} while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
567 
568 	return ret;
569 }
570 
571 static void sdw_modify_slave_status(struct sdw_slave *slave,
572 				enum sdw_slave_status status)
573 {
574 	mutex_lock(&slave->bus->bus_lock);
575 	slave->status = status;
576 	mutex_unlock(&slave->bus->bus_lock);
577 }
578 
579 static int sdw_initialize_slave(struct sdw_slave *slave)
580 {
581 	struct sdw_slave_prop *prop = &slave->prop;
582 	int ret;
583 	u8 val;
584 
585 	/*
586 	 * Set bus clash, parity and SCP implementation
587 	 * defined interrupt mask
588 	 * TODO: Read implementation defined interrupt mask
589 	 * from Slave property
590 	 */
591 	val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
592 					SDW_SCP_INT1_PARITY;
593 
594 	/* Enable SCP interrupts */
595 	ret = sdw_update(slave, SDW_SCP_INTMASK1, val, val);
596 	if (ret < 0) {
597 		dev_err(slave->bus->dev,
598 				"SDW_SCP_INTMASK1 write failed:%d", ret);
599 		return ret;
600 	}
601 
602 	/* No need to continue if DP0 is not present */
603 	if (!slave->prop.dp0_prop)
604 		return 0;
605 
606 	/* Enable DP0 interrupts */
607 	val = prop->dp0_prop->device_interrupts;
608 	val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
609 
610 	ret = sdw_update(slave, SDW_DP0_INTMASK, val, val);
611 	if (ret < 0) {
612 		dev_err(slave->bus->dev,
613 				"SDW_DP0_INTMASK read failed:%d", ret);
614 		return val;
615 	}
616 
617 	return 0;
618 }
619 
620 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
621 {
622 	u8 clear = 0, impl_int_mask;
623 	int status, status2, ret, count = 0;
624 
625 	status = sdw_read(slave, SDW_DP0_INT);
626 	if (status < 0) {
627 		dev_err(slave->bus->dev,
628 				"SDW_DP0_INT read failed:%d", status);
629 		return status;
630 	}
631 
632 	do {
633 
634 		if (status & SDW_DP0_INT_TEST_FAIL) {
635 			dev_err(&slave->dev, "Test fail for port 0");
636 			clear |= SDW_DP0_INT_TEST_FAIL;
637 		}
638 
639 		/*
640 		 * Assumption: PORT_READY interrupt will be received only for
641 		 * ports implementing Channel Prepare state machine (CP_SM)
642 		 */
643 
644 		if (status & SDW_DP0_INT_PORT_READY) {
645 			complete(&slave->port_ready[0]);
646 			clear |= SDW_DP0_INT_PORT_READY;
647 		}
648 
649 		if (status & SDW_DP0_INT_BRA_FAILURE) {
650 			dev_err(&slave->dev, "BRA failed");
651 			clear |= SDW_DP0_INT_BRA_FAILURE;
652 		}
653 
654 		impl_int_mask = SDW_DP0_INT_IMPDEF1 |
655 			SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
656 
657 		if (status & impl_int_mask) {
658 			clear |= impl_int_mask;
659 			*slave_status = clear;
660 		}
661 
662 		/* clear the interrupt */
663 		ret = sdw_write(slave, SDW_DP0_INT, clear);
664 		if (ret < 0) {
665 			dev_err(slave->bus->dev,
666 				"SDW_DP0_INT write failed:%d", ret);
667 			return ret;
668 		}
669 
670 		/* Read DP0 interrupt again */
671 		status2 = sdw_read(slave, SDW_DP0_INT);
672 		if (status2 < 0) {
673 			dev_err(slave->bus->dev,
674 				"SDW_DP0_INT read failed:%d", status2);
675 			return status2;
676 		}
677 		status &= status2;
678 
679 		count++;
680 
681 		/* we can get alerts while processing so keep retrying */
682 	} while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
683 
684 	if (count == SDW_READ_INTR_CLEAR_RETRY)
685 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read");
686 
687 	return ret;
688 }
689 
690 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
691 		int port, u8 *slave_status)
692 {
693 	u8 clear = 0, impl_int_mask;
694 	int status, status2, ret, count = 0;
695 	u32 addr;
696 
697 	if (port == 0)
698 		return sdw_handle_dp0_interrupt(slave, slave_status);
699 
700 	addr = SDW_DPN_INT(port);
701 	status = sdw_read(slave, addr);
702 	if (status < 0) {
703 		dev_err(slave->bus->dev,
704 				"SDW_DPN_INT read failed:%d", status);
705 
706 		return status;
707 	}
708 
709 	do {
710 
711 		if (status & SDW_DPN_INT_TEST_FAIL) {
712 			dev_err(&slave->dev, "Test fail for port:%d", port);
713 			clear |= SDW_DPN_INT_TEST_FAIL;
714 		}
715 
716 		/*
717 		 * Assumption: PORT_READY interrupt will be received only
718 		 * for ports implementing CP_SM.
719 		 */
720 		if (status & SDW_DPN_INT_PORT_READY) {
721 			complete(&slave->port_ready[port]);
722 			clear |= SDW_DPN_INT_PORT_READY;
723 		}
724 
725 		impl_int_mask = SDW_DPN_INT_IMPDEF1 |
726 			SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
727 
728 
729 		if (status & impl_int_mask) {
730 			clear |= impl_int_mask;
731 			*slave_status = clear;
732 		}
733 
734 		/* clear the interrupt */
735 		ret = sdw_write(slave, addr, clear);
736 		if (ret < 0) {
737 			dev_err(slave->bus->dev,
738 					"SDW_DPN_INT write failed:%d", ret);
739 			return ret;
740 		}
741 
742 		/* Read DPN interrupt again */
743 		status2 = sdw_read(slave, addr);
744 		if (status2 < 0) {
745 			dev_err(slave->bus->dev,
746 					"SDW_DPN_INT read failed:%d", status2);
747 			return status2;
748 		}
749 		status &= status2;
750 
751 		count++;
752 
753 		/* we can get alerts while processing so keep retrying */
754 	} while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
755 
756 	if (count == SDW_READ_INTR_CLEAR_RETRY)
757 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
758 
759 	return ret;
760 }
761 
762 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
763 {
764 	struct sdw_slave_intr_status slave_intr;
765 	u8 clear = 0, bit, port_status[15];
766 	int port_num, stat, ret, count = 0;
767 	unsigned long port;
768 	bool slave_notify = false;
769 	u8 buf, buf2[2], _buf, _buf2[2];
770 
771 	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
772 
773 	/* Read Instat 1, Instat 2 and Instat 3 registers */
774 	buf = ret = sdw_read(slave, SDW_SCP_INT1);
775 	if (ret < 0) {
776 		dev_err(slave->bus->dev,
777 					"SDW_SCP_INT1 read failed:%d", ret);
778 		return ret;
779 	}
780 
781 	ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2);
782 	if (ret < 0) {
783 		dev_err(slave->bus->dev,
784 					"SDW_SCP_INT2/3 read failed:%d", ret);
785 		return ret;
786 	}
787 
788 	do {
789 		/*
790 		 * Check parity, bus clash and Slave (impl defined)
791 		 * interrupt
792 		 */
793 		if (buf & SDW_SCP_INT1_PARITY) {
794 			dev_err(&slave->dev, "Parity error detected");
795 			clear |= SDW_SCP_INT1_PARITY;
796 		}
797 
798 		if (buf & SDW_SCP_INT1_BUS_CLASH) {
799 			dev_err(&slave->dev, "Bus clash error detected");
800 			clear |= SDW_SCP_INT1_BUS_CLASH;
801 		}
802 
803 		/*
804 		 * When bus clash or parity errors are detected, such errors
805 		 * are unlikely to be recoverable errors.
806 		 * TODO: In such scenario, reset bus. Make this configurable
807 		 * via sysfs property with bus reset being the default.
808 		 */
809 
810 		if (buf & SDW_SCP_INT1_IMPL_DEF) {
811 			dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
812 			clear |= SDW_SCP_INT1_IMPL_DEF;
813 			slave_notify = true;
814 		}
815 
816 		/* Check port 0 - 3 interrupts */
817 		port = buf & SDW_SCP_INT1_PORT0_3;
818 
819 		/* To get port number corresponding to bits, shift it */
820 		port = port >> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3);
821 		for_each_set_bit(bit, &port, 8) {
822 			sdw_handle_port_interrupt(slave, bit,
823 						&port_status[bit]);
824 
825 		}
826 
827 		/* Check if cascade 2 interrupt is present */
828 		if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
829 			port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
830 			for_each_set_bit(bit, &port, 8) {
831 				/* scp2 ports start from 4 */
832 				port_num = bit + 3;
833 				sdw_handle_port_interrupt(slave,
834 						port_num,
835 						&port_status[port_num]);
836 			}
837 		}
838 
839 		/* now check last cascade */
840 		if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
841 			port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
842 			for_each_set_bit(bit, &port, 8) {
843 				/* scp3 ports start from 11 */
844 				port_num = bit + 10;
845 				sdw_handle_port_interrupt(slave,
846 						port_num,
847 						&port_status[port_num]);
848 			}
849 		}
850 
851 		/* Update the Slave driver */
852 		if (slave_notify && (slave->ops) &&
853 					(slave->ops->interrupt_callback)) {
854 			slave_intr.control_port = clear;
855 			memcpy(slave_intr.port, &port_status,
856 						sizeof(slave_intr.port));
857 
858 			slave->ops->interrupt_callback(slave, &slave_intr);
859 		}
860 
861 		/* Ack interrupt */
862 		ret = sdw_write(slave, SDW_SCP_INT1, clear);
863 		if (ret < 0) {
864 			dev_err(slave->bus->dev,
865 					"SDW_SCP_INT1 write failed:%d", ret);
866 			return ret;
867 		}
868 
869 		/*
870 		 * Read status again to ensure no new interrupts arrived
871 		 * while servicing interrupts.
872 		 */
873 		_buf = ret = sdw_read(slave, SDW_SCP_INT1);
874 		if (ret < 0) {
875 			dev_err(slave->bus->dev,
876 					"SDW_SCP_INT1 read failed:%d", ret);
877 			return ret;
878 		}
879 
880 		ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2);
881 		if (ret < 0) {
882 			dev_err(slave->bus->dev,
883 					"SDW_SCP_INT2/3 read failed:%d", ret);
884 			return ret;
885 		}
886 
887 		/* Make sure no interrupts are pending */
888 		buf &= _buf;
889 		buf2[0] &= _buf2[0];
890 		buf2[1] &= _buf2[1];
891 		stat = buf || buf2[0] || buf2[1];
892 
893 		/*
894 		 * Exit loop if Slave is continuously in ALERT state even
895 		 * after servicing the interrupt multiple times.
896 		 */
897 		count++;
898 
899 		/* we can get alerts while processing so keep retrying */
900 	} while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
901 
902 	if (count == SDW_READ_INTR_CLEAR_RETRY)
903 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read");
904 
905 	return ret;
906 }
907 
908 static int sdw_update_slave_status(struct sdw_slave *slave,
909 				enum sdw_slave_status status)
910 {
911 	if ((slave->ops) && (slave->ops->update_status))
912 		return slave->ops->update_status(slave, status);
913 
914 	return 0;
915 }
916 
917 /**
918  * sdw_handle_slave_status() - Handle Slave status
919  * @bus: SDW bus instance
920  * @status: Status for all Slave(s)
921  */
922 int sdw_handle_slave_status(struct sdw_bus *bus,
923 			enum sdw_slave_status status[])
924 {
925 	enum sdw_slave_status prev_status;
926 	struct sdw_slave *slave;
927 	int i, ret = 0;
928 
929 	if (status[0] == SDW_SLAVE_ATTACHED) {
930 		ret = sdw_program_device_num(bus);
931 		if (ret)
932 			dev_err(bus->dev, "Slave attach failed: %d", ret);
933 	}
934 
935 	/* Continue to check other slave statuses */
936 	for (i = 1; i <= SDW_MAX_DEVICES; i++) {
937 		mutex_lock(&bus->bus_lock);
938 		if (test_bit(i, bus->assigned) == false) {
939 			mutex_unlock(&bus->bus_lock);
940 			continue;
941 		}
942 		mutex_unlock(&bus->bus_lock);
943 
944 		slave = sdw_get_slave(bus, i);
945 		if (!slave)
946 			continue;
947 
948 		switch (status[i]) {
949 		case SDW_SLAVE_UNATTACHED:
950 			if (slave->status == SDW_SLAVE_UNATTACHED)
951 				break;
952 
953 			sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
954 			break;
955 
956 		case SDW_SLAVE_ALERT:
957 			ret = sdw_handle_slave_alerts(slave);
958 			if (ret)
959 				dev_err(bus->dev,
960 					"Slave %d alert handling failed: %d",
961 					i, ret);
962 			break;
963 
964 		case SDW_SLAVE_ATTACHED:
965 			if (slave->status == SDW_SLAVE_ATTACHED)
966 				break;
967 
968 			prev_status = slave->status;
969 			sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
970 
971 			if (prev_status == SDW_SLAVE_ALERT)
972 				break;
973 
974 			ret = sdw_initialize_slave(slave);
975 			if (ret)
976 				dev_err(bus->dev,
977 					"Slave %d initialization failed: %d",
978 					i, ret);
979 
980 			break;
981 
982 		default:
983 			dev_err(bus->dev, "Invalid slave %d status:%d",
984 							i, status[i]);
985 			break;
986 		}
987 
988 		ret = sdw_update_slave_status(slave, status[i]);
989 		if (ret)
990 			dev_err(slave->bus->dev,
991 				"Update Slave status failed:%d", ret);
992 
993 	}
994 
995 	return ret;
996 }
997 EXPORT_SYMBOL(sdw_handle_slave_status);
998