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