perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / soundwire / bus.c
blob1cbfedfc20efd51700d29b4d2600a76d84d4d98b
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
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"
11 /**
12 * sdw_add_bus_master() - add a bus Master instance
13 * @bus: bus instance
15 * Initializes the bus instance, read properties and create child
16 * devices.
18 int sdw_add_bus_master(struct sdw_bus *bus)
20 struct sdw_master_prop *prop = NULL;
21 int ret;
23 if (!bus->dev) {
24 pr_err("SoundWire bus has no device");
25 return -ENODEV;
28 if (!bus->ops) {
29 dev_err(bus->dev, "SoundWire Bus ops are not set");
30 return -EINVAL;
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);
39 * Initialize multi_link flag
40 * TODO: populate this flag by reading property from FW node
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;
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.
58 /* Set higher order bits */
59 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
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);
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);
71 * SDW is an enumerable bus, but devices can be powered off. So,
72 * they won't be able to report as present.
74 * Create Slave devices based on Slaves described in
75 * the respective firmware (ACPI/DT)
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 */
82 if (ret) {
83 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
84 return ret;
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.
93 * Default active bank will be 0 as out of reset the Slaves have
94 * to start with bank 0 (Table 40 of Spec)
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;
102 return 0;
104 EXPORT_SYMBOL(sdw_add_bus_master);
106 static int sdw_delete_slave(struct device *dev, void *data)
108 struct sdw_slave *slave = dev_to_sdw_dev(dev);
109 struct sdw_bus *bus = slave->bus;
111 mutex_lock(&bus->bus_lock);
113 if (slave->dev_num) /* clear dev_num if assigned */
114 clear_bit(slave->dev_num, bus->assigned);
116 list_del_init(&slave->node);
117 mutex_unlock(&bus->bus_lock);
119 device_unregister(dev);
120 return 0;
124 * sdw_delete_bus_master() - delete the bus master instance
125 * @bus: bus to be deleted
127 * Remove the instance, delete the child devices.
129 void sdw_delete_bus_master(struct sdw_bus *bus)
131 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
133 EXPORT_SYMBOL(sdw_delete_bus_master);
136 * SDW IO Calls
139 static inline int find_response_code(enum sdw_command_response resp)
141 switch (resp) {
142 case SDW_CMD_OK:
143 return 0;
145 case SDW_CMD_IGNORED:
146 return -ENODATA;
148 case SDW_CMD_TIMEOUT:
149 return -ETIMEDOUT;
151 default:
152 return -EIO;
156 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
158 int retry = bus->prop.err_threshold;
159 enum sdw_command_response resp;
160 int ret = 0, i;
162 for (i = 0; i <= retry; i++) {
163 resp = bus->ops->xfer_msg(bus, msg);
164 ret = find_response_code(resp);
166 /* if cmd is ok or ignored return */
167 if (ret == 0 || ret == -ENODATA)
168 return ret;
171 return ret;
174 static inline int do_transfer_defer(struct sdw_bus *bus,
175 struct sdw_msg *msg, struct sdw_defer *defer)
177 int retry = bus->prop.err_threshold;
178 enum sdw_command_response resp;
179 int ret = 0, i;
181 defer->msg = msg;
182 defer->length = msg->len;
183 init_completion(&defer->complete);
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;
193 return ret;
196 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
198 int retry = bus->prop.err_threshold;
199 enum sdw_command_response resp;
200 int ret = 0, i;
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;
210 return ret;
214 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
215 * @bus: SDW bus
216 * @msg: SDW message to be xfered
218 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
220 int ret;
222 mutex_lock(&bus->msg_lock);
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);
229 if (msg->page)
230 sdw_reset_page(bus, msg->dev_num);
232 mutex_unlock(&bus->msg_lock);
234 return ret;
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
243 * Caller needs to hold the msg_lock lock while calling this
245 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
246 struct sdw_defer *defer)
248 int ret;
250 if (!bus->ops->xfer_msg_defer)
251 return -ENOTSUPP;
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);
258 if (msg->page)
259 sdw_reset_page(bus, msg->dev_num);
261 return ret;
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)
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;
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;
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 */
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;
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;
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;
310 return 0;
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
320 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
322 struct sdw_msg msg;
323 int ret;
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;
330 ret = pm_runtime_get_sync(slave->bus->dev);
331 if (ret < 0)
332 return ret;
334 ret = sdw_transfer(slave->bus, &msg);
335 pm_runtime_put(slave->bus->dev);
337 return ret;
339 EXPORT_SYMBOL(sdw_nread);
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
348 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
350 struct sdw_msg msg;
351 int ret;
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;
358 ret = pm_runtime_get_sync(slave->bus->dev);
359 if (ret < 0)
360 return ret;
362 ret = sdw_transfer(slave->bus, &msg);
363 pm_runtime_put(slave->bus->dev);
365 return ret;
367 EXPORT_SYMBOL(sdw_nwrite);
370 * sdw_read() - Read a SDW Slave register
371 * @slave: SDW Slave
372 * @addr: Register address
374 int sdw_read(struct sdw_slave *slave, u32 addr)
376 u8 buf;
377 int ret;
379 ret = sdw_nread(slave, addr, 1, &buf);
380 if (ret < 0)
381 return ret;
382 else
383 return buf;
385 EXPORT_SYMBOL(sdw_read);
388 * sdw_write() - Write a SDW Slave register
389 * @slave: SDW Slave
390 * @addr: Register address
391 * @value: Register value
393 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
395 return sdw_nwrite(slave, addr, 1, &value);
398 EXPORT_SYMBOL(sdw_write);
401 * SDW alert handling
404 /* called with bus_lock held */
405 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
407 struct sdw_slave *slave = NULL;
409 list_for_each_entry(slave, &bus->slaves, node) {
410 if (slave->dev_num == i)
411 return slave;
414 return NULL;
417 static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
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;
426 return 0;
429 /* called with bus_lock held */
430 static int sdw_get_device_num(struct sdw_slave *slave)
432 int bit;
434 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
435 if (bit == SDW_MAX_DEVICES) {
436 bit = -ENODEV;
437 goto err;
441 * Do not update dev_num in Slave data structure here,
442 * Update once program dev_num is successful
444 set_bit(bit, slave->bus->assigned);
446 err:
447 return bit;
450 static int sdw_assign_device_num(struct sdw_slave *slave)
452 int ret, dev_num;
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;
464 } else {
465 dev_info(slave->bus->dev,
466 "Slave already registered dev_num:%d",
467 slave->dev_num);
469 /* Clear the slave->dev_num to transfer message on device 0 */
470 dev_num = slave->dev_num;
471 slave->dev_num = 0;
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;
481 /* After xfer of msg, restore dev_num */
482 slave->dev_num = dev_num;
484 return 0;
487 void sdw_extract_slave_id(struct sdw_bus *bus,
488 u64 addr, struct sdw_slave_id *id)
490 dev_dbg(bus->dev, "SDW Slave Addr: %llx", addr);
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
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);
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);
516 static int sdw_program_device_num(struct sdw_bus *bus)
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;
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;
532 do {
533 ret = sdw_transfer(bus, &msg);
534 if (ret == -ENODATA) { /* end of device id reads */
535 ret = 0;
536 break;
538 if (ret < 0) {
539 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
540 break;
544 * Construct the addr and extract. Cast the higher shift
545 * bits to avoid truncation due to size limit.
547 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
548 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
549 ((u64)buf[0] << 40);
551 sdw_extract_slave_id(bus, addr, &id);
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;
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
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;
572 break;
576 if (found == false) {
577 /* TODO: Park this device in Group 13 */
578 dev_err(bus->dev, "Slave Entry not found");
581 count++;
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.
589 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
591 return ret;
594 static void sdw_modify_slave_status(struct sdw_slave *slave,
595 enum sdw_slave_status status)
597 mutex_lock(&slave->bus->bus_lock);
598 slave->status = status;
599 mutex_unlock(&slave->bus->bus_lock);
602 int sdw_configure_dpn_intr(struct sdw_slave *slave,
603 int port, bool enable, int mask)
605 u32 addr;
606 int ret;
607 u8 val = 0;
609 addr = SDW_DPN_INTMASK(port);
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;
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);
625 return ret;
628 static int sdw_initialize_slave(struct sdw_slave *slave)
630 struct sdw_slave_prop *prop = &slave->prop;
631 int ret;
632 u8 val;
635 * Set bus clash, parity and SCP implementation
636 * defined interrupt mask
637 * TODO: Read implementation defined interrupt mask
638 * from Slave property
640 val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
641 SDW_SCP_INT1_PARITY;
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;
651 /* No need to continue if DP0 is not present */
652 if (!slave->prop.dp0_prop)
653 return 0;
655 /* Enable DP0 interrupts */
656 val = prop->dp0_prop->device_interrupts;
657 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
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;
666 return 0;
669 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
671 u8 clear = 0, impl_int_mask;
672 int status, status2, ret, count = 0;
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;
681 do {
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;
689 * Assumption: PORT_READY interrupt will be received only for
690 * ports implementing Channel Prepare state machine (CP_SM)
693 if (status & SDW_DP0_INT_PORT_READY) {
694 complete(&slave->port_ready[0]);
695 clear |= SDW_DP0_INT_PORT_READY;
698 if (status & SDW_DP0_INT_BRA_FAILURE) {
699 dev_err(&slave->dev, "BRA failed");
700 clear |= SDW_DP0_INT_BRA_FAILURE;
703 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
704 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
706 if (status & impl_int_mask) {
707 clear |= impl_int_mask;
708 *slave_status = clear;
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;
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;
726 status &= status2;
728 count++;
730 /* we can get alerts while processing so keep retrying */
731 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
733 if (count == SDW_READ_INTR_CLEAR_RETRY)
734 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read");
736 return ret;
739 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
740 int port, u8 *slave_status)
742 u8 clear = 0, impl_int_mask;
743 int status, status2, ret, count = 0;
744 u32 addr;
746 if (port == 0)
747 return sdw_handle_dp0_interrupt(slave, slave_status);
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);
755 return status;
758 do {
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;
766 * Assumption: PORT_READY interrupt will be received only
767 * for ports implementing CP_SM.
769 if (status & SDW_DPN_INT_PORT_READY) {
770 complete(&slave->port_ready[port]);
771 clear |= SDW_DPN_INT_PORT_READY;
774 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
775 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
778 if (status & impl_int_mask) {
779 clear |= impl_int_mask;
780 *slave_status = clear;
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;
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;
798 status &= status2;
800 count++;
802 /* we can get alerts while processing so keep retrying */
803 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
805 if (count == SDW_READ_INTR_CLEAR_RETRY)
806 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
808 return ret;
811 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
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];
820 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
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;
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;
837 do {
839 * Check parity, bus clash and Slave (impl defined)
840 * interrupt
842 if (buf & SDW_SCP_INT1_PARITY) {
843 dev_err(&slave->dev, "Parity error detected");
844 clear |= SDW_SCP_INT1_PARITY;
847 if (buf & SDW_SCP_INT1_BUS_CLASH) {
848 dev_err(&slave->dev, "Bus clash error detected");
849 clear |= SDW_SCP_INT1_BUS_CLASH;
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.
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;
865 /* Check port 0 - 3 interrupts */
866 port = buf & SDW_SCP_INT1_PORT0_3;
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]);
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]);
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]);
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));
907 slave->ops->interrupt_callback(slave, &slave_intr);
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;
919 * Read status again to ensure no new interrupts arrived
920 * while servicing interrupts.
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;
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;
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];
943 * Exit loop if Slave is continuously in ALERT state even
944 * after servicing the interrupt multiple times.
946 count++;
948 /* we can get alerts while processing so keep retrying */
949 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
951 if (count == SDW_READ_INTR_CLEAR_RETRY)
952 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read");
954 return ret;
957 static int sdw_update_slave_status(struct sdw_slave *slave,
958 enum sdw_slave_status status)
960 if ((slave->ops) && (slave->ops->update_status))
961 return slave->ops->update_status(slave, status);
963 return 0;
967 * sdw_handle_slave_status() - Handle Slave status
968 * @bus: SDW bus instance
969 * @status: Status for all Slave(s)
971 int sdw_handle_slave_status(struct sdw_bus *bus,
972 enum sdw_slave_status status[])
974 enum sdw_slave_status prev_status;
975 struct sdw_slave *slave;
976 int i, ret = 0;
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);
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;
991 mutex_unlock(&bus->bus_lock);
993 slave = sdw_get_slave(bus, i);
994 if (!slave)
995 continue;
997 switch (status[i]) {
998 case SDW_SLAVE_UNATTACHED:
999 if (slave->status == SDW_SLAVE_UNATTACHED)
1000 break;
1002 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1003 break;
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;
1013 case SDW_SLAVE_ATTACHED:
1014 if (slave->status == SDW_SLAVE_ATTACHED)
1015 break;
1017 prev_status = slave->status;
1018 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1020 if (prev_status == SDW_SLAVE_ALERT)
1021 break;
1023 ret = sdw_initialize_slave(slave);
1024 if (ret)
1025 dev_err(bus->dev,
1026 "Slave %d initialization failed: %d",
1027 i, ret);
1029 break;
1031 default:
1032 dev_err(bus->dev, "Invalid slave %d status:%d",
1033 i, status[i]);
1034 break;
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);
1044 return ret;
1046 EXPORT_SYMBOL(sdw_handle_slave_status);