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