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>
12 * sdw_add_bus_master() - add a bus Master instance
15 * Initializes the bus instance, read properties and create child
18 int sdw_add_bus_master(struct sdw_bus *bus)
20 struct sdw_master_prop *prop = NULL;
24 pr_err("SoundWire bus has no device\n");
29 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
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);
47 "Bus read properties failed:%d\n", ret);
52 sdw_bus_debugfs_init(bus);
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.
61 /* Set higher order bits */
62 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
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);
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);
74 * SDW is an enumerable bus, but devices can be powered off. So,
75 * they won't be able to report as present.
77 * Create Slave devices based on Slaves described in
78 * the respective firmware (ACPI/DT)
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);
85 ret = -ENOTSUPP; /* No ACPI/DT so error out */
88 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
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
98 * Default active bank will be 0 as out of reset the Slaves have
99 * to start with bank 0 (Table 40 of Spec)
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;
109 EXPORT_SYMBOL(sdw_add_bus_master);
111 static int sdw_delete_slave(struct device *dev, void *data)
113 struct sdw_slave *slave = dev_to_sdw_dev(dev);
114 struct sdw_bus *bus = slave->bus;
116 sdw_slave_debugfs_exit(slave);
118 mutex_lock(&bus->bus_lock);
120 if (slave->dev_num) /* clear dev_num if assigned */
121 clear_bit(slave->dev_num, bus->assigned);
123 list_del_init(&slave->node);
124 mutex_unlock(&bus->bus_lock);
126 device_unregister(dev);
131 * sdw_delete_bus_master() - delete the bus master instance
132 * @bus: bus to be deleted
134 * Remove the instance, delete the child devices.
136 void sdw_delete_bus_master(struct sdw_bus *bus)
138 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
140 sdw_bus_debugfs_exit(bus);
142 EXPORT_SYMBOL(sdw_delete_bus_master);
148 static inline int find_response_code(enum sdw_command_response resp)
154 case SDW_CMD_IGNORED:
157 case SDW_CMD_TIMEOUT:
165 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
167 int retry = bus->prop.err_threshold;
168 enum sdw_command_response resp;
171 for (i = 0; i <= retry; i++) {
172 resp = bus->ops->xfer_msg(bus, msg);
173 ret = find_response_code(resp);
175 /* if cmd is ok or ignored return */
176 if (ret == 0 || ret == -ENODATA)
183 static inline int do_transfer_defer(struct sdw_bus *bus,
185 struct sdw_defer *defer)
187 int retry = bus->prop.err_threshold;
188 enum sdw_command_response resp;
192 defer->length = msg->len;
193 init_completion(&defer->complete);
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)
206 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
208 int retry = bus->prop.err_threshold;
209 enum sdw_command_response resp;
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)
224 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
226 * @msg: SDW message to be xfered
228 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
232 mutex_lock(&bus->msg_lock);
234 ret = do_transfer(bus, msg);
235 if (ret != 0 && ret != -ENODATA)
236 dev_err(bus->dev, "trf on Slave %d failed:%d\n",
240 sdw_reset_page(bus, msg->dev_num);
242 mutex_unlock(&bus->msg_lock);
248 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
250 * @msg: SDW message to be xfered
251 * @defer: Defer block for signal completion
253 * Caller needs to hold the msg_lock lock while calling this
255 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
256 struct sdw_defer *defer)
260 if (!bus->ops->xfer_msg_defer)
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",
269 sdw_reset_page(bus, msg->dev_num);
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)
277 memset(msg, 0, sizeof(*msg));
278 msg->addr = addr; /* addr is 16 bit and truncated here */
280 msg->dev_num = dev_num;
284 if (addr < SDW_REG_NO_PAGE) { /* no paging area */
286 } else if (addr >= SDW_REG_MAX) { /* illegal addr */
287 pr_err("SDW: Invalid address %x passed\n", addr);
291 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
292 if (slave && !slave->prop.paging_support)
294 /* no need for else as that will fall-through to paging */
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);
304 pr_err("SDW: No slave for paging addr\n");
306 } else if (!slave->prop.paging_support) {
308 "address %x needs paging but no support\n", addr);
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);
321 * sdw_nread() - Read "n" contiguous SDW Slave registers
323 * @addr: Register address
325 * @val: Buffer for values to be read
327 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
332 ret = sdw_fill_msg(&msg, slave, addr, count,
333 slave->dev_num, SDW_MSG_FLAG_READ, val);
337 ret = pm_runtime_get_sync(slave->bus->dev);
341 ret = sdw_transfer(slave->bus, &msg);
342 pm_runtime_put(slave->bus->dev);
346 EXPORT_SYMBOL(sdw_nread);
349 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
351 * @addr: Register address
353 * @val: Buffer for values to be read
355 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
360 ret = sdw_fill_msg(&msg, slave, addr, count,
361 slave->dev_num, SDW_MSG_FLAG_WRITE, val);
365 ret = pm_runtime_get_sync(slave->bus->dev);
369 ret = sdw_transfer(slave->bus, &msg);
370 pm_runtime_put(slave->bus->dev);
374 EXPORT_SYMBOL(sdw_nwrite);
377 * sdw_read() - Read a SDW Slave register
379 * @addr: Register address
381 int sdw_read(struct sdw_slave *slave, u32 addr)
386 ret = sdw_nread(slave, addr, 1, &buf);
392 EXPORT_SYMBOL(sdw_read);
395 * sdw_write() - Write a SDW Slave register
397 * @addr: Register address
398 * @value: Register value
400 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
402 return sdw_nwrite(slave, addr, 1, &value);
404 EXPORT_SYMBOL(sdw_write);
410 /* called with bus_lock held */
411 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
413 struct sdw_slave *slave = NULL;
415 list_for_each_entry(slave, &bus->slaves, node) {
416 if (slave->dev_num == i)
423 static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
425 if (slave->id.unique_id != id.unique_id ||
426 slave->id.mfg_id != id.mfg_id ||
427 slave->id.part_id != id.part_id ||
428 slave->id.class_id != id.class_id)
434 /* called with bus_lock held */
435 static int sdw_get_device_num(struct sdw_slave *slave)
439 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
440 if (bit == SDW_MAX_DEVICES) {
446 * Do not update dev_num in Slave data structure here,
447 * Update once program dev_num is successful
449 set_bit(bit, slave->bus->assigned);
455 static int sdw_assign_device_num(struct sdw_slave *slave)
459 /* check first if device number is assigned, if so reuse that */
460 if (!slave->dev_num) {
461 mutex_lock(&slave->bus->bus_lock);
462 dev_num = sdw_get_device_num(slave);
463 mutex_unlock(&slave->bus->bus_lock);
465 dev_err(slave->bus->dev, "Get dev_num failed: %d\n",
470 dev_info(slave->bus->dev,
471 "Slave already registered dev_num:%d\n",
474 /* Clear the slave->dev_num to transfer message on device 0 */
475 dev_num = slave->dev_num;
479 ret = sdw_write(slave, SDW_SCP_DEVNUMBER, dev_num);
481 dev_err(&slave->dev, "Program device_num %d failed: %d\n",
486 /* After xfer of msg, restore dev_num */
487 slave->dev_num = dev_num;
492 void sdw_extract_slave_id(struct sdw_bus *bus,
493 u64 addr, struct sdw_slave_id *id)
495 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
499 * Register Bit Contents
500 * DevId_0 [7:4] 47:44 sdw_version
501 * DevId_0 [3:0] 43:40 unique_id
502 * DevId_1 39:32 mfg_id [15:8]
503 * DevId_2 31:24 mfg_id [7:0]
504 * DevId_3 23:16 part_id [15:8]
505 * DevId_4 15:08 part_id [7:0]
506 * DevId_5 07:00 class_id
508 id->sdw_version = (addr >> 44) & GENMASK(3, 0);
509 id->unique_id = (addr >> 40) & GENMASK(3, 0);
510 id->mfg_id = (addr >> 24) & GENMASK(15, 0);
511 id->part_id = (addr >> 8) & GENMASK(15, 0);
512 id->class_id = addr & GENMASK(7, 0);
515 "SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x\n",
516 id->class_id, id->part_id, id->mfg_id,
517 id->unique_id, id->sdw_version);
520 static int sdw_program_device_num(struct sdw_bus *bus)
522 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
523 struct sdw_slave *slave, *_s;
524 struct sdw_slave_id id;
530 /* No Slave, so use raw xfer api */
531 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
532 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
537 ret = sdw_transfer(bus, &msg);
538 if (ret == -ENODATA) { /* end of device id reads */
539 dev_dbg(bus->dev, "No more devices to enumerate\n");
544 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
549 * Construct the addr and extract. Cast the higher shift
550 * bits to avoid truncation due to size limit.
552 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
553 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
556 sdw_extract_slave_id(bus, addr, &id);
558 /* Now compare with entries */
559 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
560 if (sdw_compare_devid(slave, id) == 0) {
564 * Assign a new dev_num to this Slave and
565 * not mark it present. It will be marked
566 * present after it reports ATTACHED on new
569 ret = sdw_assign_device_num(slave);
571 dev_err(slave->bus->dev,
572 "Assign dev_num failed:%d\n",
582 /* TODO: Park this device in Group 13 */
583 dev_err(bus->dev, "Slave Entry not found\n");
589 * Check till error out or retry (count) exhausts.
590 * Device can drop off and rejoin during enumeration
591 * so count till twice the bound.
594 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
599 static void sdw_modify_slave_status(struct sdw_slave *slave,
600 enum sdw_slave_status status)
602 mutex_lock(&slave->bus->bus_lock);
603 slave->status = status;
604 mutex_unlock(&slave->bus->bus_lock);
607 int sdw_configure_dpn_intr(struct sdw_slave *slave,
608 int port, bool enable, int mask)
614 addr = SDW_DPN_INTMASK(port);
616 /* Set/Clear port ready interrupt mask */
619 val |= SDW_DPN_INT_PORT_READY;
622 val &= ~SDW_DPN_INT_PORT_READY;
625 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
627 dev_err(slave->bus->dev,
628 "SDW_DPN_INTMASK write failed:%d\n", val);
633 static int sdw_initialize_slave(struct sdw_slave *slave)
635 struct sdw_slave_prop *prop = &slave->prop;
640 * Set bus clash, parity and SCP implementation
641 * defined interrupt mask
642 * TODO: Read implementation defined interrupt mask
643 * from Slave property
645 val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
648 /* Enable SCP interrupts */
649 ret = sdw_update(slave, SDW_SCP_INTMASK1, val, val);
651 dev_err(slave->bus->dev,
652 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
656 /* No need to continue if DP0 is not present */
657 if (!slave->prop.dp0_prop)
660 /* Enable DP0 interrupts */
661 val = prop->dp0_prop->imp_def_interrupts;
662 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
664 ret = sdw_update(slave, SDW_DP0_INTMASK, val, val);
666 dev_err(slave->bus->dev,
667 "SDW_DP0_INTMASK read failed:%d\n", ret);
674 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
676 u8 clear = 0, impl_int_mask;
677 int status, status2, ret, count = 0;
679 status = sdw_read(slave, SDW_DP0_INT);
681 dev_err(slave->bus->dev,
682 "SDW_DP0_INT read failed:%d\n", status);
687 if (status & SDW_DP0_INT_TEST_FAIL) {
688 dev_err(&slave->dev, "Test fail for port 0\n");
689 clear |= SDW_DP0_INT_TEST_FAIL;
693 * Assumption: PORT_READY interrupt will be received only for
694 * ports implementing Channel Prepare state machine (CP_SM)
697 if (status & SDW_DP0_INT_PORT_READY) {
698 complete(&slave->port_ready[0]);
699 clear |= SDW_DP0_INT_PORT_READY;
702 if (status & SDW_DP0_INT_BRA_FAILURE) {
703 dev_err(&slave->dev, "BRA failed\n");
704 clear |= SDW_DP0_INT_BRA_FAILURE;
707 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
708 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
710 if (status & impl_int_mask) {
711 clear |= impl_int_mask;
712 *slave_status = clear;
715 /* clear the interrupt */
716 ret = sdw_write(slave, SDW_DP0_INT, clear);
718 dev_err(slave->bus->dev,
719 "SDW_DP0_INT write failed:%d\n", ret);
723 /* Read DP0 interrupt again */
724 status2 = sdw_read(slave, SDW_DP0_INT);
726 dev_err(slave->bus->dev,
727 "SDW_DP0_INT read failed:%d\n", status2);
734 /* we can get alerts while processing so keep retrying */
735 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
737 if (count == SDW_READ_INTR_CLEAR_RETRY)
738 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read\n");
743 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
744 int port, u8 *slave_status)
746 u8 clear = 0, impl_int_mask;
747 int status, status2, ret, count = 0;
751 return sdw_handle_dp0_interrupt(slave, slave_status);
753 addr = SDW_DPN_INT(port);
754 status = sdw_read(slave, addr);
756 dev_err(slave->bus->dev,
757 "SDW_DPN_INT read failed:%d\n", status);
763 if (status & SDW_DPN_INT_TEST_FAIL) {
764 dev_err(&slave->dev, "Test fail for port:%d\n", port);
765 clear |= SDW_DPN_INT_TEST_FAIL;
769 * Assumption: PORT_READY interrupt will be received only
770 * for ports implementing CP_SM.
772 if (status & SDW_DPN_INT_PORT_READY) {
773 complete(&slave->port_ready[port]);
774 clear |= SDW_DPN_INT_PORT_READY;
777 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
778 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
780 if (status & impl_int_mask) {
781 clear |= impl_int_mask;
782 *slave_status = clear;
785 /* clear the interrupt */
786 ret = sdw_write(slave, addr, clear);
788 dev_err(slave->bus->dev,
789 "SDW_DPN_INT write failed:%d\n", ret);
793 /* Read DPN interrupt again */
794 status2 = sdw_read(slave, addr);
796 dev_err(slave->bus->dev,
797 "SDW_DPN_INT read failed:%d\n", status2);
804 /* we can get alerts while processing so keep retrying */
805 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
807 if (count == SDW_READ_INTR_CLEAR_RETRY)
808 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
813 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
815 struct sdw_slave_intr_status slave_intr;
816 u8 clear = 0, bit, port_status[15] = {0};
817 int port_num, stat, ret, count = 0;
819 bool slave_notify = false;
820 u8 buf, buf2[2], _buf, _buf2[2];
822 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
824 /* Read Instat 1, Instat 2 and Instat 3 registers */
825 ret = sdw_read(slave, SDW_SCP_INT1);
827 dev_err(slave->bus->dev,
828 "SDW_SCP_INT1 read failed:%d\n", ret);
833 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2);
835 dev_err(slave->bus->dev,
836 "SDW_SCP_INT2/3 read failed:%d\n", ret);
842 * Check parity, bus clash and Slave (impl defined)
845 if (buf & SDW_SCP_INT1_PARITY) {
846 dev_err(&slave->dev, "Parity error detected\n");
847 clear |= SDW_SCP_INT1_PARITY;
850 if (buf & SDW_SCP_INT1_BUS_CLASH) {
851 dev_err(&slave->dev, "Bus clash error detected\n");
852 clear |= SDW_SCP_INT1_BUS_CLASH;
856 * When bus clash or parity errors are detected, such errors
857 * are unlikely to be recoverable errors.
858 * TODO: In such scenario, reset bus. Make this configurable
859 * via sysfs property with bus reset being the default.
862 if (buf & SDW_SCP_INT1_IMPL_DEF) {
863 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
864 clear |= SDW_SCP_INT1_IMPL_DEF;
868 /* Check port 0 - 3 interrupts */
869 port = buf & SDW_SCP_INT1_PORT0_3;
871 /* To get port number corresponding to bits, shift it */
872 port = port >> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3);
873 for_each_set_bit(bit, &port, 8) {
874 sdw_handle_port_interrupt(slave, bit,
878 /* Check if cascade 2 interrupt is present */
879 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
880 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
881 for_each_set_bit(bit, &port, 8) {
882 /* scp2 ports start from 4 */
884 sdw_handle_port_interrupt(slave,
886 &port_status[port_num]);
890 /* now check last cascade */
891 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
892 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
893 for_each_set_bit(bit, &port, 8) {
894 /* scp3 ports start from 11 */
896 sdw_handle_port_interrupt(slave,
898 &port_status[port_num]);
902 /* Update the Slave driver */
903 if (slave_notify && slave->ops &&
904 slave->ops->interrupt_callback) {
905 slave_intr.control_port = clear;
906 memcpy(slave_intr.port, &port_status,
907 sizeof(slave_intr.port));
909 slave->ops->interrupt_callback(slave, &slave_intr);
913 ret = sdw_write(slave, SDW_SCP_INT1, clear);
915 dev_err(slave->bus->dev,
916 "SDW_SCP_INT1 write failed:%d\n", ret);
921 * Read status again to ensure no new interrupts arrived
922 * while servicing interrupts.
924 ret = sdw_read(slave, SDW_SCP_INT1);
926 dev_err(slave->bus->dev,
927 "SDW_SCP_INT1 read failed:%d\n", ret);
932 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2);
934 dev_err(slave->bus->dev,
935 "SDW_SCP_INT2/3 read failed:%d\n", ret);
939 /* Make sure no interrupts are pending */
943 stat = buf || buf2[0] || buf2[1];
946 * Exit loop if Slave is continuously in ALERT state even
947 * after servicing the interrupt multiple times.
951 /* we can get alerts while processing so keep retrying */
952 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
954 if (count == SDW_READ_INTR_CLEAR_RETRY)
955 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read\n");
960 static int sdw_update_slave_status(struct sdw_slave *slave,
961 enum sdw_slave_status status)
963 if (slave->ops && slave->ops->update_status)
964 return slave->ops->update_status(slave, status);
970 * sdw_handle_slave_status() - Handle Slave status
971 * @bus: SDW bus instance
972 * @status: Status for all Slave(s)
974 int sdw_handle_slave_status(struct sdw_bus *bus,
975 enum sdw_slave_status status[])
977 enum sdw_slave_status prev_status;
978 struct sdw_slave *slave;
981 if (status[0] == SDW_SLAVE_ATTACHED) {
982 dev_dbg(bus->dev, "Slave attached, programming device number\n");
983 ret = sdw_program_device_num(bus);
985 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
987 * programming a device number will have side effects,
988 * so we deal with other devices at a later time
993 /* Continue to check other slave statuses */
994 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
995 mutex_lock(&bus->bus_lock);
996 if (test_bit(i, bus->assigned) == false) {
997 mutex_unlock(&bus->bus_lock);
1000 mutex_unlock(&bus->bus_lock);
1002 slave = sdw_get_slave(bus, i);
1006 switch (status[i]) {
1007 case SDW_SLAVE_UNATTACHED:
1008 if (slave->status == SDW_SLAVE_UNATTACHED)
1011 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1014 case SDW_SLAVE_ALERT:
1015 ret = sdw_handle_slave_alerts(slave);
1018 "Slave %d alert handling failed: %d\n",
1022 case SDW_SLAVE_ATTACHED:
1023 if (slave->status == SDW_SLAVE_ATTACHED)
1026 prev_status = slave->status;
1027 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1029 if (prev_status == SDW_SLAVE_ALERT)
1032 ret = sdw_initialize_slave(slave);
1035 "Slave %d initialization failed: %d\n",
1041 dev_err(bus->dev, "Invalid slave %d status:%d\n",
1046 ret = sdw_update_slave_status(slave, status[i]);
1048 dev_err(slave->bus->dev,
1049 "Update Slave status failed:%d\n", ret);
1054 EXPORT_SYMBOL(sdw_handle_slave_status);