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");
29 dev_err(bus
->dev
, "SoundWire Bus ops are not set");
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
);
46 dev_err(bus
->dev
, "Bus read properties failed:%d", 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
);
80 ret
= -ENOTSUPP
; /* No ACPI/DT so error out */
83 dev_err(bus
->dev
, "Finding slaves failed:%d\n", 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
93 * Default active bank will be 0 as out of reset the Slaves have
94 * to start with bank 0 (Table 40 of Spec)
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
;
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
);
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
);
139 static inline int find_response_code(enum sdw_command_response resp
)
145 case SDW_CMD_IGNORED
:
148 case SDW_CMD_TIMEOUT
:
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
;
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
)
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
;
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
)
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
;
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
)
214 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
216 * @msg: SDW message to be xfered
218 int sdw_transfer(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
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",
230 sdw_reset_page(bus
, msg
->dev_num
);
232 mutex_unlock(&bus
->msg_lock
);
238 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
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
)
250 if (!bus
->ops
->xfer_msg_defer
)
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",
259 sdw_reset_page(bus
, msg
->dev_num
);
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 */
271 msg
->dev_num
= dev_num
;
274 msg
->ssp_sync
= false;
277 if (addr
< SDW_REG_NO_PAGE
) { /* no paging area */
279 } else if (addr
>= SDW_REG_MAX
) { /* illegal addr */
280 pr_err("SDW: Invalid address %x passed\n", addr
);
284 if (addr
< SDW_REG_OPTIONAL_PAGE
) { /* 32k but no page */
285 if (slave
&& !slave
->prop
.paging_support
)
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
);
297 pr_err("SDW: No slave for paging addr\n");
299 } else if (!slave
->prop
.paging_support
) {
301 "address %x needs paging but no support", addr
);
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);
314 * sdw_nread() - Read "n" contiguous SDW Slave registers
316 * @addr: Register address
318 * @val: Buffer for values to be read
320 int sdw_nread(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
325 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
326 slave
->dev_num
, SDW_MSG_FLAG_READ
, val
);
330 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
334 ret
= sdw_transfer(slave
->bus
, &msg
);
335 pm_runtime_put(slave
->bus
->dev
);
339 EXPORT_SYMBOL(sdw_nread
);
342 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
344 * @addr: Register address
346 * @val: Buffer for values to be read
348 int sdw_nwrite(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
353 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
354 slave
->dev_num
, SDW_MSG_FLAG_WRITE
, val
);
358 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
362 ret
= sdw_transfer(slave
->bus
, &msg
);
363 pm_runtime_put(slave
->bus
->dev
);
367 EXPORT_SYMBOL(sdw_nwrite
);
370 * sdw_read() - Read a SDW Slave register
372 * @addr: Register address
374 int sdw_read(struct sdw_slave
*slave
, u32 addr
)
379 ret
= sdw_nread(slave
, addr
, 1, &buf
);
385 EXPORT_SYMBOL(sdw_read
);
388 * sdw_write() - Write a SDW Slave register
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
);
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
)
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
))
429 /* called with bus_lock held */
430 static int sdw_get_device_num(struct sdw_slave
*slave
)
434 bit
= find_first_zero_bit(slave
->bus
->assigned
, SDW_MAX_DEVICES
);
435 if (bit
== SDW_MAX_DEVICES
) {
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
);
450 static int sdw_assign_device_num(struct sdw_slave
*slave
)
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
);
460 dev_err(slave
->bus
->dev
, "Get dev_num failed: %d",
465 dev_info(slave
->bus
->dev
,
466 "Slave already registered dev_num:%d",
469 /* Clear the slave->dev_num to transfer message on device 0 */
470 dev_num
= slave
->dev_num
;
475 ret
= sdw_write(slave
, SDW_SCP_DEVNUMBER
, dev_num
);
477 dev_err(&slave
->dev
, "Program device_num failed: %d", ret
);
481 /* After xfer of msg, restore dev_num */
482 slave
->dev_num
= dev_num
;
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
);
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);
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
;
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
);
533 ret
= sdw_transfer(bus
, &msg
);
534 if (ret
== -ENODATA
) { /* end of device id reads */
539 dev_err(bus
->dev
, "DEVID read fail:%d\n", ret
);
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) |
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) {
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
564 ret
= sdw_assign_device_num(slave
);
566 dev_err(slave
->bus
->dev
,
567 "Assign dev_num failed:%d",
576 if (found
== false) {
577 /* TODO: Park this device in Group 13 */
578 dev_err(bus
->dev
, "Slave Entry not found");
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));
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
)
609 addr
= SDW_DPN_INTMASK(port
);
611 /* Set/Clear port ready interrupt mask */
614 val
|= SDW_DPN_INT_PORT_READY
;
617 val
&= ~SDW_DPN_INT_PORT_READY
;
620 ret
= sdw_update(slave
, addr
, (mask
| SDW_DPN_INT_PORT_READY
), val
);
622 dev_err(slave
->bus
->dev
,
623 "SDW_DPN_INTMASK write failed:%d", val
);
628 static int sdw_initialize_slave(struct sdw_slave
*slave
)
630 struct sdw_slave_prop
*prop
= &slave
->prop
;
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
|
643 /* Enable SCP interrupts */
644 ret
= sdw_update(slave
, SDW_SCP_INTMASK1
, val
, val
);
646 dev_err(slave
->bus
->dev
,
647 "SDW_SCP_INTMASK1 write failed:%d", ret
);
651 /* No need to continue if DP0 is not present */
652 if (!slave
->prop
.dp0_prop
)
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
);
661 dev_err(slave
->bus
->dev
,
662 "SDW_DP0_INTMASK read failed:%d", ret
);
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
);
676 dev_err(slave
->bus
->dev
,
677 "SDW_DP0_INT read failed:%d", status
);
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
);
714 dev_err(slave
->bus
->dev
,
715 "SDW_DP0_INT write failed:%d", ret
);
719 /* Read DP0 interrupt again */
720 status2
= sdw_read(slave
, SDW_DP0_INT
);
722 dev_err(slave
->bus
->dev
,
723 "SDW_DP0_INT read failed:%d", status2
);
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");
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;
747 return sdw_handle_dp0_interrupt(slave
, slave_status
);
749 addr
= SDW_DPN_INT(port
);
750 status
= sdw_read(slave
, addr
);
752 dev_err(slave
->bus
->dev
,
753 "SDW_DPN_INT read failed:%d", status
);
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
);
786 dev_err(slave
->bus
->dev
,
787 "SDW_DPN_INT write failed:%d", ret
);
791 /* Read DPN interrupt again */
792 status2
= sdw_read(slave
, addr
);
794 dev_err(slave
->bus
->dev
,
795 "SDW_DPN_INT read failed:%d", status2
);
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");
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;
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
);
825 dev_err(slave
->bus
->dev
,
826 "SDW_SCP_INT1 read failed:%d", ret
);
830 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, buf2
);
832 dev_err(slave
->bus
->dev
,
833 "SDW_SCP_INT2/3 read failed:%d", ret
);
839 * Check parity, bus clash and Slave (impl defined)
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
;
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
,
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 */
882 sdw_handle_port_interrupt(slave
,
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 */
894 sdw_handle_port_interrupt(slave
,
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
);
911 ret
= sdw_write(slave
, SDW_SCP_INT1
, clear
);
913 dev_err(slave
->bus
->dev
,
914 "SDW_SCP_INT1 write failed:%d", ret
);
919 * Read status again to ensure no new interrupts arrived
920 * while servicing interrupts.
922 _buf
= ret
= sdw_read(slave
, SDW_SCP_INT1
);
924 dev_err(slave
->bus
->dev
,
925 "SDW_SCP_INT1 read failed:%d", ret
);
929 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, _buf2
);
931 dev_err(slave
->bus
->dev
,
932 "SDW_SCP_INT2/3 read failed:%d", ret
);
936 /* Make sure no interrupts are pending */
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.
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");
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
);
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
;
978 if (status
[0] == SDW_SLAVE_ATTACHED
) {
979 ret
= sdw_program_device_num(bus
);
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
);
991 mutex_unlock(&bus
->bus_lock
);
993 slave
= sdw_get_slave(bus
, i
);
998 case SDW_SLAVE_UNATTACHED
:
999 if (slave
->status
== SDW_SLAVE_UNATTACHED
)
1002 sdw_modify_slave_status(slave
, SDW_SLAVE_UNATTACHED
);
1005 case SDW_SLAVE_ALERT
:
1006 ret
= sdw_handle_slave_alerts(slave
);
1009 "Slave %d alert handling failed: %d",
1013 case SDW_SLAVE_ATTACHED
:
1014 if (slave
->status
== SDW_SLAVE_ATTACHED
)
1017 prev_status
= slave
->status
;
1018 sdw_modify_slave_status(slave
, SDW_SLAVE_ATTACHED
);
1020 if (prev_status
== SDW_SLAVE_ALERT
)
1023 ret
= sdw_initialize_slave(slave
);
1026 "Slave %d initialization failed: %d",
1032 dev_err(bus
->dev
, "Invalid slave %d status:%d",
1037 ret
= sdw_update_slave_status(slave
, status
[i
]);
1039 dev_err(slave
->bus
->dev
,
1040 "Update Slave status failed:%d", ret
);
1046 EXPORT_SYMBOL(sdw_handle_slave_status
);