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
)
23 pr_err("SoundWire bus has no device");
28 dev_err(bus
->dev
, "SoundWire Bus ops are not set");
32 mutex_init(&bus
->msg_lock
);
33 mutex_init(&bus
->bus_lock
);
34 INIT_LIST_HEAD(&bus
->slaves
);
36 if (bus
->ops
->read_prop
) {
37 ret
= bus
->ops
->read_prop(bus
);
39 dev_err(bus
->dev
, "Bus read properties failed:%d", ret
);
45 * Device numbers in SoundWire are 0 thru 15. Enumeration device
46 * number (0), Broadcast device number (15), Group numbers (12 and
47 * 13) and Master device number (14) are not used for assignment so
48 * mask these and other higher bits.
51 /* Set higher order bits */
52 *bus
->assigned
= ~GENMASK(SDW_BROADCAST_DEV_NUM
, SDW_ENUM_DEV_NUM
);
54 /* Set enumuration device number and broadcast device number */
55 set_bit(SDW_ENUM_DEV_NUM
, bus
->assigned
);
56 set_bit(SDW_BROADCAST_DEV_NUM
, bus
->assigned
);
58 /* Set group device numbers and master device number */
59 set_bit(SDW_GROUP12_DEV_NUM
, bus
->assigned
);
60 set_bit(SDW_GROUP13_DEV_NUM
, bus
->assigned
);
61 set_bit(SDW_MASTER_DEV_NUM
, bus
->assigned
);
64 * SDW is an enumerable bus, but devices can be powered off. So,
65 * they won't be able to report as present.
67 * Create Slave devices based on Slaves described in
68 * the respective firmware (ACPI/DT)
70 if (IS_ENABLED(CONFIG_ACPI
) && ACPI_HANDLE(bus
->dev
))
71 ret
= sdw_acpi_find_slaves(bus
);
73 ret
= -ENOTSUPP
; /* No ACPI/DT so error out */
76 dev_err(bus
->dev
, "Finding slaves failed:%d\n", ret
);
82 EXPORT_SYMBOL(sdw_add_bus_master
);
84 static int sdw_delete_slave(struct device
*dev
, void *data
)
86 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
87 struct sdw_bus
*bus
= slave
->bus
;
89 mutex_lock(&bus
->bus_lock
);
91 if (slave
->dev_num
) /* clear dev_num if assigned */
92 clear_bit(slave
->dev_num
, bus
->assigned
);
94 list_del_init(&slave
->node
);
95 mutex_unlock(&bus
->bus_lock
);
97 device_unregister(dev
);
102 * sdw_delete_bus_master() - delete the bus master instance
103 * @bus: bus to be deleted
105 * Remove the instance, delete the child devices.
107 void sdw_delete_bus_master(struct sdw_bus
*bus
)
109 device_for_each_child(bus
->dev
, NULL
, sdw_delete_slave
);
111 EXPORT_SYMBOL(sdw_delete_bus_master
);
117 static inline int find_response_code(enum sdw_command_response resp
)
123 case SDW_CMD_IGNORED
:
126 case SDW_CMD_TIMEOUT
:
134 static inline int do_transfer(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
136 int retry
= bus
->prop
.err_threshold
;
137 enum sdw_command_response resp
;
140 for (i
= 0; i
<= retry
; i
++) {
141 resp
= bus
->ops
->xfer_msg(bus
, msg
);
142 ret
= find_response_code(resp
);
144 /* if cmd is ok or ignored return */
145 if (ret
== 0 || ret
== -ENODATA
)
152 static inline int do_transfer_defer(struct sdw_bus
*bus
,
153 struct sdw_msg
*msg
, struct sdw_defer
*defer
)
155 int retry
= bus
->prop
.err_threshold
;
156 enum sdw_command_response resp
;
160 defer
->length
= msg
->len
;
162 for (i
= 0; i
<= retry
; i
++) {
163 resp
= bus
->ops
->xfer_msg_defer(bus
, msg
, defer
);
164 ret
= find_response_code(resp
);
165 /* if cmd is ok or ignored return */
166 if (ret
== 0 || ret
== -ENODATA
)
173 static int sdw_reset_page(struct sdw_bus
*bus
, u16 dev_num
)
175 int retry
= bus
->prop
.err_threshold
;
176 enum sdw_command_response resp
;
179 for (i
= 0; i
<= retry
; i
++) {
180 resp
= bus
->ops
->reset_page_addr(bus
, dev_num
);
181 ret
= find_response_code(resp
);
182 /* if cmd is ok or ignored return */
183 if (ret
== 0 || ret
== -ENODATA
)
191 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
193 * @msg: SDW message to be xfered
195 int sdw_transfer(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
199 mutex_lock(&bus
->msg_lock
);
201 ret
= do_transfer(bus
, msg
);
202 if (ret
!= 0 && ret
!= -ENODATA
)
203 dev_err(bus
->dev
, "trf on Slave %d failed:%d\n",
207 sdw_reset_page(bus
, msg
->dev_num
);
209 mutex_unlock(&bus
->msg_lock
);
215 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
217 * @msg: SDW message to be xfered
218 * @defer: Defer block for signal completion
220 * Caller needs to hold the msg_lock lock while calling this
222 int sdw_transfer_defer(struct sdw_bus
*bus
, struct sdw_msg
*msg
,
223 struct sdw_defer
*defer
)
227 if (!bus
->ops
->xfer_msg_defer
)
230 ret
= do_transfer_defer(bus
, msg
, defer
);
231 if (ret
!= 0 && ret
!= -ENODATA
)
232 dev_err(bus
->dev
, "Defer trf on Slave %d failed:%d\n",
236 sdw_reset_page(bus
, msg
->dev_num
);
242 int sdw_fill_msg(struct sdw_msg
*msg
, struct sdw_slave
*slave
,
243 u32 addr
, size_t count
, u16 dev_num
, u8 flags
, u8
*buf
)
245 memset(msg
, 0, sizeof(*msg
));
246 msg
->addr
= addr
; /* addr is 16 bit and truncated here */
248 msg
->dev_num
= dev_num
;
251 msg
->ssp_sync
= false;
254 if (addr
< SDW_REG_NO_PAGE
) { /* no paging area */
256 } else if (addr
>= SDW_REG_MAX
) { /* illegal addr */
257 pr_err("SDW: Invalid address %x passed\n", addr
);
261 if (addr
< SDW_REG_OPTIONAL_PAGE
) { /* 32k but no page */
262 if (slave
&& !slave
->prop
.paging_support
)
264 /* no need for else as that will fall thru to paging */
267 /* paging mandatory */
268 if (dev_num
== SDW_ENUM_DEV_NUM
|| dev_num
== SDW_BROADCAST_DEV_NUM
) {
269 pr_err("SDW: Invalid device for paging :%d\n", dev_num
);
274 pr_err("SDW: No slave for paging addr\n");
276 } else if (!slave
->prop
.paging_support
) {
278 "address %x needs paging but no support", addr
);
282 msg
->addr_page1
= (addr
>> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE1_MASK
));
283 msg
->addr_page2
= (addr
>> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE2_MASK
));
284 msg
->addr
|= BIT(15);
291 * sdw_nread() - Read "n" contiguous SDW Slave registers
293 * @addr: Register address
295 * @val: Buffer for values to be read
297 int sdw_nread(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
302 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
303 slave
->dev_num
, SDW_MSG_FLAG_READ
, val
);
307 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
311 ret
= sdw_transfer(slave
->bus
, &msg
);
312 pm_runtime_put(slave
->bus
->dev
);
316 EXPORT_SYMBOL(sdw_nread
);
319 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
321 * @addr: Register address
323 * @val: Buffer for values to be read
325 int sdw_nwrite(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
330 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
331 slave
->dev_num
, SDW_MSG_FLAG_WRITE
, val
);
335 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
339 ret
= sdw_transfer(slave
->bus
, &msg
);
340 pm_runtime_put(slave
->bus
->dev
);
344 EXPORT_SYMBOL(sdw_nwrite
);
347 * sdw_read() - Read a SDW Slave register
349 * @addr: Register address
351 int sdw_read(struct sdw_slave
*slave
, u32 addr
)
356 ret
= sdw_nread(slave
, addr
, 1, &buf
);
362 EXPORT_SYMBOL(sdw_read
);
365 * sdw_write() - Write a SDW Slave register
367 * @addr: Register address
368 * @value: Register value
370 int sdw_write(struct sdw_slave
*slave
, u32 addr
, u8 value
)
372 return sdw_nwrite(slave
, addr
, 1, &value
);
375 EXPORT_SYMBOL(sdw_write
);
381 /* called with bus_lock held */
382 static struct sdw_slave
*sdw_get_slave(struct sdw_bus
*bus
, int i
)
384 struct sdw_slave
*slave
= NULL
;
386 list_for_each_entry(slave
, &bus
->slaves
, node
) {
387 if (slave
->dev_num
== i
)
394 static int sdw_compare_devid(struct sdw_slave
*slave
, struct sdw_slave_id id
)
397 if ((slave
->id
.unique_id
!= id
.unique_id
) ||
398 (slave
->id
.mfg_id
!= id
.mfg_id
) ||
399 (slave
->id
.part_id
!= id
.part_id
) ||
400 (slave
->id
.class_id
!= id
.class_id
))
406 /* called with bus_lock held */
407 static int sdw_get_device_num(struct sdw_slave
*slave
)
411 bit
= find_first_zero_bit(slave
->bus
->assigned
, SDW_MAX_DEVICES
);
412 if (bit
== SDW_MAX_DEVICES
) {
418 * Do not update dev_num in Slave data structure here,
419 * Update once program dev_num is successful
421 set_bit(bit
, slave
->bus
->assigned
);
427 static int sdw_assign_device_num(struct sdw_slave
*slave
)
431 /* check first if device number is assigned, if so reuse that */
432 if (!slave
->dev_num
) {
433 mutex_lock(&slave
->bus
->bus_lock
);
434 dev_num
= sdw_get_device_num(slave
);
435 mutex_unlock(&slave
->bus
->bus_lock
);
437 dev_err(slave
->bus
->dev
, "Get dev_num failed: %d",
442 dev_info(slave
->bus
->dev
,
443 "Slave already registered dev_num:%d",
446 /* Clear the slave->dev_num to transfer message on device 0 */
447 dev_num
= slave
->dev_num
;
452 ret
= sdw_write(slave
, SDW_SCP_DEVNUMBER
, dev_num
);
454 dev_err(&slave
->dev
, "Program device_num failed: %d", ret
);
458 /* After xfer of msg, restore dev_num */
459 slave
->dev_num
= dev_num
;
464 void sdw_extract_slave_id(struct sdw_bus
*bus
,
465 u64 addr
, struct sdw_slave_id
*id
)
467 dev_dbg(bus
->dev
, "SDW Slave Addr: %llx", addr
);
471 * Register Bit Contents
472 * DevId_0 [7:4] 47:44 sdw_version
473 * DevId_0 [3:0] 43:40 unique_id
474 * DevId_1 39:32 mfg_id [15:8]
475 * DevId_2 31:24 mfg_id [7:0]
476 * DevId_3 23:16 part_id [15:8]
477 * DevId_4 15:08 part_id [7:0]
478 * DevId_5 07:00 class_id
480 id
->sdw_version
= (addr
>> 44) & GENMASK(3, 0);
481 id
->unique_id
= (addr
>> 40) & GENMASK(3, 0);
482 id
->mfg_id
= (addr
>> 24) & GENMASK(15, 0);
483 id
->part_id
= (addr
>> 8) & GENMASK(15, 0);
484 id
->class_id
= addr
& GENMASK(7, 0);
487 "SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x",
488 id
->class_id
, id
->part_id
, id
->mfg_id
,
489 id
->unique_id
, id
->sdw_version
);
493 static int sdw_program_device_num(struct sdw_bus
*bus
)
495 u8 buf
[SDW_NUM_DEV_ID_REGISTERS
] = {0};
496 struct sdw_slave
*slave
, *_s
;
497 struct sdw_slave_id id
;
503 /* No Slave, so use raw xfer api */
504 ret
= sdw_fill_msg(&msg
, NULL
, SDW_SCP_DEVID_0
,
505 SDW_NUM_DEV_ID_REGISTERS
, 0, SDW_MSG_FLAG_READ
, buf
);
510 ret
= sdw_transfer(bus
, &msg
);
511 if (ret
== -ENODATA
) { /* end of device id reads */
516 dev_err(bus
->dev
, "DEVID read fail:%d\n", ret
);
521 * Construct the addr and extract. Cast the higher shift
522 * bits to avoid truncation due to size limit.
524 addr
= buf
[5] | (buf
[4] << 8) | (buf
[3] << 16) |
525 ((u64
)buf
[2] << 24) | ((u64
)buf
[1] << 32) |
528 sdw_extract_slave_id(bus
, addr
, &id
);
530 /* Now compare with entries */
531 list_for_each_entry_safe(slave
, _s
, &bus
->slaves
, node
) {
532 if (sdw_compare_devid(slave
, id
) == 0) {
536 * Assign a new dev_num to this Slave and
537 * not mark it present. It will be marked
538 * present after it reports ATTACHED on new
541 ret
= sdw_assign_device_num(slave
);
543 dev_err(slave
->bus
->dev
,
544 "Assign dev_num failed:%d",
553 if (found
== false) {
554 /* TODO: Park this device in Group 13 */
555 dev_err(bus
->dev
, "Slave Entry not found");
561 * Check till error out or retry (count) exhausts.
562 * Device can drop off and rejoin during enumeration
563 * so count till twice the bound.
566 } while (ret
== 0 && count
< (SDW_MAX_DEVICES
* 2));
571 static void sdw_modify_slave_status(struct sdw_slave
*slave
,
572 enum sdw_slave_status status
)
574 mutex_lock(&slave
->bus
->bus_lock
);
575 slave
->status
= status
;
576 mutex_unlock(&slave
->bus
->bus_lock
);
579 static int sdw_initialize_slave(struct sdw_slave
*slave
)
581 struct sdw_slave_prop
*prop
= &slave
->prop
;
586 * Set bus clash, parity and SCP implementation
587 * defined interrupt mask
588 * TODO: Read implementation defined interrupt mask
589 * from Slave property
591 val
= SDW_SCP_INT1_IMPL_DEF
| SDW_SCP_INT1_BUS_CLASH
|
594 /* Enable SCP interrupts */
595 ret
= sdw_update(slave
, SDW_SCP_INTMASK1
, val
, val
);
597 dev_err(slave
->bus
->dev
,
598 "SDW_SCP_INTMASK1 write failed:%d", ret
);
602 /* No need to continue if DP0 is not present */
603 if (!slave
->prop
.dp0_prop
)
606 /* Enable DP0 interrupts */
607 val
= prop
->dp0_prop
->device_interrupts
;
608 val
|= SDW_DP0_INT_PORT_READY
| SDW_DP0_INT_BRA_FAILURE
;
610 ret
= sdw_update(slave
, SDW_DP0_INTMASK
, val
, val
);
612 dev_err(slave
->bus
->dev
,
613 "SDW_DP0_INTMASK read failed:%d", ret
);
620 static int sdw_handle_dp0_interrupt(struct sdw_slave
*slave
, u8
*slave_status
)
622 u8 clear
= 0, impl_int_mask
;
623 int status
, status2
, ret
, count
= 0;
625 status
= sdw_read(slave
, SDW_DP0_INT
);
627 dev_err(slave
->bus
->dev
,
628 "SDW_DP0_INT read failed:%d", status
);
634 if (status
& SDW_DP0_INT_TEST_FAIL
) {
635 dev_err(&slave
->dev
, "Test fail for port 0");
636 clear
|= SDW_DP0_INT_TEST_FAIL
;
640 * Assumption: PORT_READY interrupt will be received only for
641 * ports implementing Channel Prepare state machine (CP_SM)
644 if (status
& SDW_DP0_INT_PORT_READY
) {
645 complete(&slave
->port_ready
[0]);
646 clear
|= SDW_DP0_INT_PORT_READY
;
649 if (status
& SDW_DP0_INT_BRA_FAILURE
) {
650 dev_err(&slave
->dev
, "BRA failed");
651 clear
|= SDW_DP0_INT_BRA_FAILURE
;
654 impl_int_mask
= SDW_DP0_INT_IMPDEF1
|
655 SDW_DP0_INT_IMPDEF2
| SDW_DP0_INT_IMPDEF3
;
657 if (status
& impl_int_mask
) {
658 clear
|= impl_int_mask
;
659 *slave_status
= clear
;
662 /* clear the interrupt */
663 ret
= sdw_write(slave
, SDW_DP0_INT
, clear
);
665 dev_err(slave
->bus
->dev
,
666 "SDW_DP0_INT write failed:%d", ret
);
670 /* Read DP0 interrupt again */
671 status2
= sdw_read(slave
, SDW_DP0_INT
);
673 dev_err(slave
->bus
->dev
,
674 "SDW_DP0_INT read failed:%d", status2
);
681 /* we can get alerts while processing so keep retrying */
682 } while (status
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
684 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
685 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on DP0 read");
690 static int sdw_handle_port_interrupt(struct sdw_slave
*slave
,
691 int port
, u8
*slave_status
)
693 u8 clear
= 0, impl_int_mask
;
694 int status
, status2
, ret
, count
= 0;
698 return sdw_handle_dp0_interrupt(slave
, slave_status
);
700 addr
= SDW_DPN_INT(port
);
701 status
= sdw_read(slave
, addr
);
703 dev_err(slave
->bus
->dev
,
704 "SDW_DPN_INT read failed:%d", status
);
711 if (status
& SDW_DPN_INT_TEST_FAIL
) {
712 dev_err(&slave
->dev
, "Test fail for port:%d", port
);
713 clear
|= SDW_DPN_INT_TEST_FAIL
;
717 * Assumption: PORT_READY interrupt will be received only
718 * for ports implementing CP_SM.
720 if (status
& SDW_DPN_INT_PORT_READY
) {
721 complete(&slave
->port_ready
[port
]);
722 clear
|= SDW_DPN_INT_PORT_READY
;
725 impl_int_mask
= SDW_DPN_INT_IMPDEF1
|
726 SDW_DPN_INT_IMPDEF2
| SDW_DPN_INT_IMPDEF3
;
729 if (status
& impl_int_mask
) {
730 clear
|= impl_int_mask
;
731 *slave_status
= clear
;
734 /* clear the interrupt */
735 ret
= sdw_write(slave
, addr
, clear
);
737 dev_err(slave
->bus
->dev
,
738 "SDW_DPN_INT write failed:%d", ret
);
742 /* Read DPN interrupt again */
743 status2
= sdw_read(slave
, addr
);
745 dev_err(slave
->bus
->dev
,
746 "SDW_DPN_INT read failed:%d", status2
);
753 /* we can get alerts while processing so keep retrying */
754 } while (status
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
756 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
757 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on port read");
762 static int sdw_handle_slave_alerts(struct sdw_slave
*slave
)
764 struct sdw_slave_intr_status slave_intr
;
765 u8 clear
= 0, bit
, port_status
[15];
766 int port_num
, stat
, ret
, count
= 0;
768 bool slave_notify
= false;
769 u8 buf
, buf2
[2], _buf
, _buf2
[2];
771 sdw_modify_slave_status(slave
, SDW_SLAVE_ALERT
);
773 /* Read Instat 1, Instat 2 and Instat 3 registers */
774 buf
= ret
= sdw_read(slave
, SDW_SCP_INT1
);
776 dev_err(slave
->bus
->dev
,
777 "SDW_SCP_INT1 read failed:%d", ret
);
781 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, buf2
);
783 dev_err(slave
->bus
->dev
,
784 "SDW_SCP_INT2/3 read failed:%d", ret
);
790 * Check parity, bus clash and Slave (impl defined)
793 if (buf
& SDW_SCP_INT1_PARITY
) {
794 dev_err(&slave
->dev
, "Parity error detected");
795 clear
|= SDW_SCP_INT1_PARITY
;
798 if (buf
& SDW_SCP_INT1_BUS_CLASH
) {
799 dev_err(&slave
->dev
, "Bus clash error detected");
800 clear
|= SDW_SCP_INT1_BUS_CLASH
;
804 * When bus clash or parity errors are detected, such errors
805 * are unlikely to be recoverable errors.
806 * TODO: In such scenario, reset bus. Make this configurable
807 * via sysfs property with bus reset being the default.
810 if (buf
& SDW_SCP_INT1_IMPL_DEF
) {
811 dev_dbg(&slave
->dev
, "Slave impl defined interrupt\n");
812 clear
|= SDW_SCP_INT1_IMPL_DEF
;
816 /* Check port 0 - 3 interrupts */
817 port
= buf
& SDW_SCP_INT1_PORT0_3
;
819 /* To get port number corresponding to bits, shift it */
820 port
= port
>> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3
);
821 for_each_set_bit(bit
, &port
, 8) {
822 sdw_handle_port_interrupt(slave
, bit
,
827 /* Check if cascade 2 interrupt is present */
828 if (buf
& SDW_SCP_INT1_SCP2_CASCADE
) {
829 port
= buf2
[0] & SDW_SCP_INTSTAT2_PORT4_10
;
830 for_each_set_bit(bit
, &port
, 8) {
831 /* scp2 ports start from 4 */
833 sdw_handle_port_interrupt(slave
,
835 &port_status
[port_num
]);
839 /* now check last cascade */
840 if (buf2
[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE
) {
841 port
= buf2
[1] & SDW_SCP_INTSTAT3_PORT11_14
;
842 for_each_set_bit(bit
, &port
, 8) {
843 /* scp3 ports start from 11 */
845 sdw_handle_port_interrupt(slave
,
847 &port_status
[port_num
]);
851 /* Update the Slave driver */
852 if (slave_notify
&& (slave
->ops
) &&
853 (slave
->ops
->interrupt_callback
)) {
854 slave_intr
.control_port
= clear
;
855 memcpy(slave_intr
.port
, &port_status
,
856 sizeof(slave_intr
.port
));
858 slave
->ops
->interrupt_callback(slave
, &slave_intr
);
862 ret
= sdw_write(slave
, SDW_SCP_INT1
, clear
);
864 dev_err(slave
->bus
->dev
,
865 "SDW_SCP_INT1 write failed:%d", ret
);
870 * Read status again to ensure no new interrupts arrived
871 * while servicing interrupts.
873 _buf
= ret
= sdw_read(slave
, SDW_SCP_INT1
);
875 dev_err(slave
->bus
->dev
,
876 "SDW_SCP_INT1 read failed:%d", ret
);
880 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, _buf2
);
882 dev_err(slave
->bus
->dev
,
883 "SDW_SCP_INT2/3 read failed:%d", ret
);
887 /* Make sure no interrupts are pending */
891 stat
= buf
|| buf2
[0] || buf2
[1];
894 * Exit loop if Slave is continuously in ALERT state even
895 * after servicing the interrupt multiple times.
899 /* we can get alerts while processing so keep retrying */
900 } while (stat
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
902 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
903 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on alert read");
908 static int sdw_update_slave_status(struct sdw_slave
*slave
,
909 enum sdw_slave_status status
)
911 if ((slave
->ops
) && (slave
->ops
->update_status
))
912 return slave
->ops
->update_status(slave
, status
);
918 * sdw_handle_slave_status() - Handle Slave status
919 * @bus: SDW bus instance
920 * @status: Status for all Slave(s)
922 int sdw_handle_slave_status(struct sdw_bus
*bus
,
923 enum sdw_slave_status status
[])
925 enum sdw_slave_status prev_status
;
926 struct sdw_slave
*slave
;
929 if (status
[0] == SDW_SLAVE_ATTACHED
) {
930 ret
= sdw_program_device_num(bus
);
932 dev_err(bus
->dev
, "Slave attach failed: %d", ret
);
935 /* Continue to check other slave statuses */
936 for (i
= 1; i
<= SDW_MAX_DEVICES
; i
++) {
937 mutex_lock(&bus
->bus_lock
);
938 if (test_bit(i
, bus
->assigned
) == false) {
939 mutex_unlock(&bus
->bus_lock
);
942 mutex_unlock(&bus
->bus_lock
);
944 slave
= sdw_get_slave(bus
, i
);
949 case SDW_SLAVE_UNATTACHED
:
950 if (slave
->status
== SDW_SLAVE_UNATTACHED
)
953 sdw_modify_slave_status(slave
, SDW_SLAVE_UNATTACHED
);
956 case SDW_SLAVE_ALERT
:
957 ret
= sdw_handle_slave_alerts(slave
);
960 "Slave %d alert handling failed: %d",
964 case SDW_SLAVE_ATTACHED
:
965 if (slave
->status
== SDW_SLAVE_ATTACHED
)
968 prev_status
= slave
->status
;
969 sdw_modify_slave_status(slave
, SDW_SLAVE_ATTACHED
);
971 if (prev_status
== SDW_SLAVE_ALERT
)
974 ret
= sdw_initialize_slave(slave
);
977 "Slave %d initialization failed: %d",
983 dev_err(bus
->dev
, "Invalid slave %d status:%d",
988 ret
= sdw_update_slave_status(slave
, status
[i
]);
990 dev_err(slave
->bus
->dev
,
991 "Update Slave status failed:%d", ret
);
997 EXPORT_SYMBOL(sdw_handle_slave_status
);