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/delay.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/soundwire/sdw_registers.h>
9 #include <linux/soundwire/sdw.h>
13 * sdw_add_bus_master() - add a bus Master instance
16 * Initializes the bus instance, read properties and create child
19 int sdw_add_bus_master(struct sdw_bus
*bus
)
21 struct sdw_master_prop
*prop
= NULL
;
25 pr_err("SoundWire bus has no device\n");
30 dev_err(bus
->dev
, "SoundWire Bus ops are not set\n");
34 mutex_init(&bus
->msg_lock
);
35 mutex_init(&bus
->bus_lock
);
36 INIT_LIST_HEAD(&bus
->slaves
);
37 INIT_LIST_HEAD(&bus
->m_rt_list
);
40 * Initialize multi_link flag
41 * TODO: populate this flag by reading property from FW node
43 bus
->multi_link
= false;
44 if (bus
->ops
->read_prop
) {
45 ret
= bus
->ops
->read_prop(bus
);
48 "Bus read properties failed:%d\n", ret
);
53 sdw_bus_debugfs_init(bus
);
56 * Device numbers in SoundWire are 0 through 15. Enumeration device
57 * number (0), Broadcast device number (15), Group numbers (12 and
58 * 13) and Master device number (14) are not used for assignment so
59 * mask these and other higher bits.
62 /* Set higher order bits */
63 *bus
->assigned
= ~GENMASK(SDW_BROADCAST_DEV_NUM
, SDW_ENUM_DEV_NUM
);
65 /* Set enumuration device number and broadcast device number */
66 set_bit(SDW_ENUM_DEV_NUM
, bus
->assigned
);
67 set_bit(SDW_BROADCAST_DEV_NUM
, bus
->assigned
);
69 /* Set group device numbers and master device number */
70 set_bit(SDW_GROUP12_DEV_NUM
, bus
->assigned
);
71 set_bit(SDW_GROUP13_DEV_NUM
, bus
->assigned
);
72 set_bit(SDW_MASTER_DEV_NUM
, bus
->assigned
);
75 * SDW is an enumerable bus, but devices can be powered off. So,
76 * they won't be able to report as present.
78 * Create Slave devices based on Slaves described in
79 * the respective firmware (ACPI/DT)
81 if (IS_ENABLED(CONFIG_ACPI
) && ACPI_HANDLE(bus
->dev
))
82 ret
= sdw_acpi_find_slaves(bus
);
83 else if (IS_ENABLED(CONFIG_OF
) && bus
->dev
->of_node
)
84 ret
= sdw_of_find_slaves(bus
);
86 ret
= -ENOTSUPP
; /* No ACPI/DT so error out */
89 dev_err(bus
->dev
, "Finding slaves failed:%d\n", ret
);
94 * Initialize clock values based on Master properties. The max
95 * frequency is read from max_clk_freq property. Current assumption
96 * is that the bus will start at highest clock frequency when
99 * Default active bank will be 0 as out of reset the Slaves have
100 * to start with bank 0 (Table 40 of Spec)
103 bus
->params
.max_dr_freq
= prop
->max_clk_freq
* SDW_DOUBLE_RATE_FACTOR
;
104 bus
->params
.curr_dr_freq
= bus
->params
.max_dr_freq
;
105 bus
->params
.curr_bank
= SDW_BANK0
;
106 bus
->params
.next_bank
= SDW_BANK1
;
110 EXPORT_SYMBOL(sdw_add_bus_master
);
112 static int sdw_delete_slave(struct device
*dev
, void *data
)
114 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
115 struct sdw_bus
*bus
= slave
->bus
;
117 pm_runtime_disable(dev
);
119 sdw_slave_debugfs_exit(slave
);
121 mutex_lock(&bus
->bus_lock
);
123 if (slave
->dev_num
) /* clear dev_num if assigned */
124 clear_bit(slave
->dev_num
, bus
->assigned
);
126 list_del_init(&slave
->node
);
127 mutex_unlock(&bus
->bus_lock
);
129 device_unregister(dev
);
134 * sdw_delete_bus_master() - delete the bus master instance
135 * @bus: bus to be deleted
137 * Remove the instance, delete the child devices.
139 void sdw_delete_bus_master(struct sdw_bus
*bus
)
141 device_for_each_child(bus
->dev
, NULL
, sdw_delete_slave
);
143 sdw_bus_debugfs_exit(bus
);
145 EXPORT_SYMBOL(sdw_delete_bus_master
);
151 static inline int find_response_code(enum sdw_command_response resp
)
157 case SDW_CMD_IGNORED
:
160 case SDW_CMD_TIMEOUT
:
168 static inline int do_transfer(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
170 int retry
= bus
->prop
.err_threshold
;
171 enum sdw_command_response resp
;
174 for (i
= 0; i
<= retry
; i
++) {
175 resp
= bus
->ops
->xfer_msg(bus
, msg
);
176 ret
= find_response_code(resp
);
178 /* if cmd is ok or ignored return */
179 if (ret
== 0 || ret
== -ENODATA
)
186 static inline int do_transfer_defer(struct sdw_bus
*bus
,
188 struct sdw_defer
*defer
)
190 int retry
= bus
->prop
.err_threshold
;
191 enum sdw_command_response resp
;
195 defer
->length
= msg
->len
;
196 init_completion(&defer
->complete
);
198 for (i
= 0; i
<= retry
; i
++) {
199 resp
= bus
->ops
->xfer_msg_defer(bus
, msg
, defer
);
200 ret
= find_response_code(resp
);
201 /* if cmd is ok or ignored return */
202 if (ret
== 0 || ret
== -ENODATA
)
209 static int sdw_reset_page(struct sdw_bus
*bus
, u16 dev_num
)
211 int retry
= bus
->prop
.err_threshold
;
212 enum sdw_command_response resp
;
215 for (i
= 0; i
<= retry
; i
++) {
216 resp
= bus
->ops
->reset_page_addr(bus
, dev_num
);
217 ret
= find_response_code(resp
);
218 /* if cmd is ok or ignored return */
219 if (ret
== 0 || ret
== -ENODATA
)
227 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
229 * @msg: SDW message to be xfered
231 int sdw_transfer(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
235 mutex_lock(&bus
->msg_lock
);
237 ret
= do_transfer(bus
, msg
);
238 if (ret
!= 0 && ret
!= -ENODATA
)
239 dev_err(bus
->dev
, "trf on Slave %d failed:%d\n",
243 sdw_reset_page(bus
, msg
->dev_num
);
245 mutex_unlock(&bus
->msg_lock
);
251 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
253 * @msg: SDW message to be xfered
254 * @defer: Defer block for signal completion
256 * Caller needs to hold the msg_lock lock while calling this
258 int sdw_transfer_defer(struct sdw_bus
*bus
, struct sdw_msg
*msg
,
259 struct sdw_defer
*defer
)
263 if (!bus
->ops
->xfer_msg_defer
)
266 ret
= do_transfer_defer(bus
, msg
, defer
);
267 if (ret
!= 0 && ret
!= -ENODATA
)
268 dev_err(bus
->dev
, "Defer trf on Slave %d failed:%d\n",
272 sdw_reset_page(bus
, msg
->dev_num
);
277 int sdw_fill_msg(struct sdw_msg
*msg
, struct sdw_slave
*slave
,
278 u32 addr
, size_t count
, u16 dev_num
, u8 flags
, u8
*buf
)
280 memset(msg
, 0, sizeof(*msg
));
281 msg
->addr
= addr
; /* addr is 16 bit and truncated here */
283 msg
->dev_num
= dev_num
;
287 if (addr
< SDW_REG_NO_PAGE
) { /* no paging area */
289 } else if (addr
>= SDW_REG_MAX
) { /* illegal addr */
290 pr_err("SDW: Invalid address %x passed\n", addr
);
294 if (addr
< SDW_REG_OPTIONAL_PAGE
) { /* 32k but no page */
295 if (slave
&& !slave
->prop
.paging_support
)
297 /* no need for else as that will fall-through to paging */
300 /* paging mandatory */
301 if (dev_num
== SDW_ENUM_DEV_NUM
|| dev_num
== SDW_BROADCAST_DEV_NUM
) {
302 pr_err("SDW: Invalid device for paging :%d\n", dev_num
);
307 pr_err("SDW: No slave for paging addr\n");
309 } else if (!slave
->prop
.paging_support
) {
311 "address %x needs paging but no support\n", addr
);
315 msg
->addr_page1
= (addr
>> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE1_MASK
));
316 msg
->addr_page2
= (addr
>> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE2_MASK
));
317 msg
->addr
|= BIT(15);
324 * Read/Write IO functions.
325 * no_pm versions can only be called by the bus, e.g. while enumerating or
326 * handling suspend-resume sequences.
327 * all clients need to use the pm versions
331 sdw_nread_no_pm(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
336 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
337 slave
->dev_num
, SDW_MSG_FLAG_READ
, val
);
341 return sdw_transfer(slave
->bus
, &msg
);
345 sdw_nwrite_no_pm(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
350 ret
= sdw_fill_msg(&msg
, slave
, addr
, count
,
351 slave
->dev_num
, SDW_MSG_FLAG_WRITE
, val
);
355 return sdw_transfer(slave
->bus
, &msg
);
358 static int sdw_write_no_pm(struct sdw_slave
*slave
, u32 addr
, u8 value
)
360 return sdw_nwrite_no_pm(slave
, addr
, 1, &value
);
364 sdw_bread_no_pm(struct sdw_bus
*bus
, u16 dev_num
, u32 addr
)
370 ret
= sdw_fill_msg(&msg
, NULL
, addr
, 1, dev_num
,
371 SDW_MSG_FLAG_READ
, &buf
);
375 ret
= sdw_transfer(bus
, &msg
);
383 sdw_bwrite_no_pm(struct sdw_bus
*bus
, u16 dev_num
, u32 addr
, u8 value
)
388 ret
= sdw_fill_msg(&msg
, NULL
, addr
, 1, dev_num
,
389 SDW_MSG_FLAG_WRITE
, &value
);
393 return sdw_transfer(bus
, &msg
);
397 sdw_read_no_pm(struct sdw_slave
*slave
, u32 addr
)
402 ret
= sdw_nread_no_pm(slave
, addr
, 1, &buf
);
410 * sdw_nread() - Read "n" contiguous SDW Slave registers
412 * @addr: Register address
414 * @val: Buffer for values to be read
416 int sdw_nread(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
420 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
421 if (ret
< 0 && ret
!= -EACCES
) {
422 pm_runtime_put_noidle(slave
->bus
->dev
);
426 ret
= sdw_nread_no_pm(slave
, addr
, count
, val
);
428 pm_runtime_mark_last_busy(slave
->bus
->dev
);
429 pm_runtime_put(slave
->bus
->dev
);
433 EXPORT_SYMBOL(sdw_nread
);
436 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
438 * @addr: Register address
440 * @val: Buffer for values to be read
442 int sdw_nwrite(struct sdw_slave
*slave
, u32 addr
, size_t count
, u8
*val
)
446 ret
= pm_runtime_get_sync(slave
->bus
->dev
);
447 if (ret
< 0 && ret
!= -EACCES
) {
448 pm_runtime_put_noidle(slave
->bus
->dev
);
452 ret
= sdw_nwrite_no_pm(slave
, addr
, count
, val
);
454 pm_runtime_mark_last_busy(slave
->bus
->dev
);
455 pm_runtime_put(slave
->bus
->dev
);
459 EXPORT_SYMBOL(sdw_nwrite
);
462 * sdw_read() - Read a SDW Slave register
464 * @addr: Register address
466 int sdw_read(struct sdw_slave
*slave
, u32 addr
)
471 ret
= sdw_nread(slave
, addr
, 1, &buf
);
477 EXPORT_SYMBOL(sdw_read
);
480 * sdw_write() - Write a SDW Slave register
482 * @addr: Register address
483 * @value: Register value
485 int sdw_write(struct sdw_slave
*slave
, u32 addr
, u8 value
)
487 return sdw_nwrite(slave
, addr
, 1, &value
);
489 EXPORT_SYMBOL(sdw_write
);
495 /* called with bus_lock held */
496 static struct sdw_slave
*sdw_get_slave(struct sdw_bus
*bus
, int i
)
498 struct sdw_slave
*slave
= NULL
;
500 list_for_each_entry(slave
, &bus
->slaves
, node
) {
501 if (slave
->dev_num
== i
)
508 static int sdw_compare_devid(struct sdw_slave
*slave
, struct sdw_slave_id id
)
510 if (slave
->id
.mfg_id
!= id
.mfg_id
||
511 slave
->id
.part_id
!= id
.part_id
||
512 slave
->id
.class_id
!= id
.class_id
||
513 (slave
->id
.unique_id
!= SDW_IGNORED_UNIQUE_ID
&&
514 slave
->id
.unique_id
!= id
.unique_id
))
520 /* called with bus_lock held */
521 static int sdw_get_device_num(struct sdw_slave
*slave
)
525 bit
= find_first_zero_bit(slave
->bus
->assigned
, SDW_MAX_DEVICES
);
526 if (bit
== SDW_MAX_DEVICES
) {
532 * Do not update dev_num in Slave data structure here,
533 * Update once program dev_num is successful
535 set_bit(bit
, slave
->bus
->assigned
);
541 static int sdw_assign_device_num(struct sdw_slave
*slave
)
544 bool new_device
= false;
546 /* check first if device number is assigned, if so reuse that */
547 if (!slave
->dev_num
) {
548 if (!slave
->dev_num_sticky
) {
549 mutex_lock(&slave
->bus
->bus_lock
);
550 dev_num
= sdw_get_device_num(slave
);
551 mutex_unlock(&slave
->bus
->bus_lock
);
553 dev_err(slave
->bus
->dev
, "Get dev_num failed: %d\n",
557 slave
->dev_num
= dev_num
;
558 slave
->dev_num_sticky
= dev_num
;
561 slave
->dev_num
= slave
->dev_num_sticky
;
566 dev_info(slave
->bus
->dev
,
567 "Slave already registered, reusing dev_num:%d\n",
570 /* Clear the slave->dev_num to transfer message on device 0 */
571 dev_num
= slave
->dev_num
;
574 ret
= sdw_write_no_pm(slave
, SDW_SCP_DEVNUMBER
, dev_num
);
576 dev_err(&slave
->dev
, "Program device_num %d failed: %d\n",
581 /* After xfer of msg, restore dev_num */
582 slave
->dev_num
= slave
->dev_num_sticky
;
587 void sdw_extract_slave_id(struct sdw_bus
*bus
,
588 u64 addr
, struct sdw_slave_id
*id
)
590 dev_dbg(bus
->dev
, "SDW Slave Addr: %llx\n", addr
);
592 id
->sdw_version
= SDW_VERSION(addr
);
593 id
->unique_id
= SDW_UNIQUE_ID(addr
);
594 id
->mfg_id
= SDW_MFG_ID(addr
);
595 id
->part_id
= SDW_PART_ID(addr
);
596 id
->class_id
= SDW_CLASS_ID(addr
);
599 "SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x\n",
600 id
->class_id
, id
->part_id
, id
->mfg_id
,
601 id
->unique_id
, id
->sdw_version
);
604 static int sdw_program_device_num(struct sdw_bus
*bus
)
606 u8 buf
[SDW_NUM_DEV_ID_REGISTERS
] = {0};
607 struct sdw_slave
*slave
, *_s
;
608 struct sdw_slave_id id
;
614 /* No Slave, so use raw xfer api */
615 ret
= sdw_fill_msg(&msg
, NULL
, SDW_SCP_DEVID_0
,
616 SDW_NUM_DEV_ID_REGISTERS
, 0, SDW_MSG_FLAG_READ
, buf
);
621 ret
= sdw_transfer(bus
, &msg
);
622 if (ret
== -ENODATA
) { /* end of device id reads */
623 dev_dbg(bus
->dev
, "No more devices to enumerate\n");
628 dev_err(bus
->dev
, "DEVID read fail:%d\n", ret
);
633 * Construct the addr and extract. Cast the higher shift
634 * bits to avoid truncation due to size limit.
636 addr
= buf
[5] | (buf
[4] << 8) | (buf
[3] << 16) |
637 ((u64
)buf
[2] << 24) | ((u64
)buf
[1] << 32) |
640 sdw_extract_slave_id(bus
, addr
, &id
);
642 /* Now compare with entries */
643 list_for_each_entry_safe(slave
, _s
, &bus
->slaves
, node
) {
644 if (sdw_compare_devid(slave
, id
) == 0) {
648 * Assign a new dev_num to this Slave and
649 * not mark it present. It will be marked
650 * present after it reports ATTACHED on new
653 ret
= sdw_assign_device_num(slave
);
655 dev_err(slave
->bus
->dev
,
656 "Assign dev_num failed:%d\n",
666 /* TODO: Park this device in Group 13 */
667 dev_err(bus
->dev
, "Slave Entry not found\n");
673 * Check till error out or retry (count) exhausts.
674 * Device can drop off and rejoin during enumeration
675 * so count till twice the bound.
678 } while (ret
== 0 && count
< (SDW_MAX_DEVICES
* 2));
683 static void sdw_modify_slave_status(struct sdw_slave
*slave
,
684 enum sdw_slave_status status
)
686 mutex_lock(&slave
->bus
->bus_lock
);
688 dev_vdbg(&slave
->dev
,
689 "%s: changing status slave %d status %d new status %d\n",
690 __func__
, slave
->dev_num
, slave
->status
, status
);
692 if (status
== SDW_SLAVE_UNATTACHED
) {
694 "%s: initializing completion for Slave %d\n",
695 __func__
, slave
->dev_num
);
697 init_completion(&slave
->enumeration_complete
);
698 init_completion(&slave
->initialization_complete
);
700 } else if ((status
== SDW_SLAVE_ATTACHED
) &&
701 (slave
->status
== SDW_SLAVE_UNATTACHED
)) {
703 "%s: signaling completion for Slave %d\n",
704 __func__
, slave
->dev_num
);
706 complete(&slave
->enumeration_complete
);
708 slave
->status
= status
;
709 mutex_unlock(&slave
->bus
->bus_lock
);
712 static enum sdw_clk_stop_mode
sdw_get_clk_stop_mode(struct sdw_slave
*slave
)
714 enum sdw_clk_stop_mode mode
;
717 * Query for clock stop mode if Slave implements
718 * ops->get_clk_stop_mode, else read from property.
720 if (slave
->ops
&& slave
->ops
->get_clk_stop_mode
) {
721 mode
= slave
->ops
->get_clk_stop_mode(slave
);
723 if (slave
->prop
.clk_stop_mode1
)
724 mode
= SDW_CLK_STOP_MODE1
;
726 mode
= SDW_CLK_STOP_MODE0
;
732 static int sdw_slave_clk_stop_callback(struct sdw_slave
*slave
,
733 enum sdw_clk_stop_mode mode
,
734 enum sdw_clk_stop_type type
)
738 if (slave
->ops
&& slave
->ops
->clk_stop
) {
739 ret
= slave
->ops
->clk_stop(slave
, mode
, type
);
742 "Clk Stop type =%d failed: %d\n", type
, ret
);
750 static int sdw_slave_clk_stop_prepare(struct sdw_slave
*slave
,
751 enum sdw_clk_stop_mode mode
,
758 wake_en
= slave
->prop
.wake_capable
;
761 val
= SDW_SCP_SYSTEMCTRL_CLK_STP_PREP
;
763 if (mode
== SDW_CLK_STOP_MODE1
)
764 val
|= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1
;
767 val
|= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN
;
769 val
= sdw_read_no_pm(slave
, SDW_SCP_SYSTEMCTRL
);
771 val
&= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP
);
774 ret
= sdw_write_no_pm(slave
, SDW_SCP_SYSTEMCTRL
, val
);
778 "Clock Stop prepare failed for slave: %d", ret
);
783 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus
*bus
, u16 dev_num
)
785 int retry
= bus
->clk_stop_timeout
;
789 val
= sdw_bread_no_pm(bus
, dev_num
, SDW_SCP_STAT
) &
790 SDW_SCP_STAT_CLK_STP_NF
;
792 dev_info(bus
->dev
, "clock stop prep/de-prep done slave:%d",
797 usleep_range(1000, 1500);
801 dev_err(bus
->dev
, "clock stop prep/de-prep failed slave:%d",
808 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
810 * @bus: SDW bus instance
812 * Query Slave for clock stop mode and prepare for that mode.
814 int sdw_bus_prep_clk_stop(struct sdw_bus
*bus
)
816 enum sdw_clk_stop_mode slave_mode
;
817 bool simple_clk_stop
= true;
818 struct sdw_slave
*slave
;
819 bool is_slave
= false;
823 * In order to save on transition time, prepare
824 * each Slave and then wait for all Slave(s) to be
825 * prepared for clock stop.
827 list_for_each_entry(slave
, &bus
->slaves
, node
) {
831 /* Identify if Slave(s) are available on Bus */
834 if (slave
->status
!= SDW_SLAVE_ATTACHED
&&
835 slave
->status
!= SDW_SLAVE_ALERT
)
838 slave_mode
= sdw_get_clk_stop_mode(slave
);
839 slave
->curr_clk_stop_mode
= slave_mode
;
841 ret
= sdw_slave_clk_stop_callback(slave
, slave_mode
,
842 SDW_CLK_PRE_PREPARE
);
845 "pre-prepare failed:%d", ret
);
849 ret
= sdw_slave_clk_stop_prepare(slave
,
853 "pre-prepare failed:%d", ret
);
857 if (slave_mode
== SDW_CLK_STOP_MODE1
)
858 simple_clk_stop
= false;
861 if (is_slave
&& !simple_clk_stop
) {
862 ret
= sdw_bus_wait_for_clk_prep_deprep(bus
,
863 SDW_BROADCAST_DEV_NUM
);
868 /* Inform slaves that prep is done */
869 list_for_each_entry(slave
, &bus
->slaves
, node
) {
873 if (slave
->status
!= SDW_SLAVE_ATTACHED
&&
874 slave
->status
!= SDW_SLAVE_ALERT
)
877 slave_mode
= slave
->curr_clk_stop_mode
;
879 if (slave_mode
== SDW_CLK_STOP_MODE1
) {
880 ret
= sdw_slave_clk_stop_callback(slave
,
882 SDW_CLK_POST_PREPARE
);
886 "post-prepare failed:%d", ret
);
893 EXPORT_SYMBOL(sdw_bus_prep_clk_stop
);
896 * sdw_bus_clk_stop: stop bus clock
898 * @bus: SDW bus instance
900 * After preparing the Slaves for clock stop, stop the clock by broadcasting
901 * write to SCP_CTRL register.
903 int sdw_bus_clk_stop(struct sdw_bus
*bus
)
908 * broadcast clock stop now, attached Slaves will ACK this,
909 * unattached will ignore
911 ret
= sdw_bwrite_no_pm(bus
, SDW_BROADCAST_DEV_NUM
,
912 SDW_SCP_CTRL
, SDW_SCP_CTRL_CLK_STP_NOW
);
916 "ClockStopNow Broadcast msg ignored %d", ret
);
919 "ClockStopNow Broadcast msg failed %d", ret
);
925 EXPORT_SYMBOL(sdw_bus_clk_stop
);
928 * sdw_bus_exit_clk_stop: Exit clock stop mode
930 * @bus: SDW bus instance
932 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
933 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
936 int sdw_bus_exit_clk_stop(struct sdw_bus
*bus
)
938 enum sdw_clk_stop_mode mode
;
939 bool simple_clk_stop
= true;
940 struct sdw_slave
*slave
;
941 bool is_slave
= false;
945 * In order to save on transition time, de-prepare
946 * each Slave and then wait for all Slave(s) to be
947 * de-prepared after clock resume.
949 list_for_each_entry(slave
, &bus
->slaves
, node
) {
953 /* Identify if Slave(s) are available on Bus */
956 if (slave
->status
!= SDW_SLAVE_ATTACHED
&&
957 slave
->status
!= SDW_SLAVE_ALERT
)
960 mode
= slave
->curr_clk_stop_mode
;
962 if (mode
== SDW_CLK_STOP_MODE1
) {
963 simple_clk_stop
= false;
967 ret
= sdw_slave_clk_stop_callback(slave
, mode
,
968 SDW_CLK_PRE_DEPREPARE
);
970 dev_warn(&slave
->dev
,
971 "clk stop deprep failed:%d", ret
);
973 ret
= sdw_slave_clk_stop_prepare(slave
, mode
,
977 dev_warn(&slave
->dev
,
978 "clk stop deprep failed:%d", ret
);
981 if (is_slave
&& !simple_clk_stop
)
982 sdw_bus_wait_for_clk_prep_deprep(bus
, SDW_BROADCAST_DEV_NUM
);
984 list_for_each_entry(slave
, &bus
->slaves
, node
) {
988 if (slave
->status
!= SDW_SLAVE_ATTACHED
&&
989 slave
->status
!= SDW_SLAVE_ALERT
)
992 mode
= slave
->curr_clk_stop_mode
;
993 sdw_slave_clk_stop_callback(slave
, mode
,
994 SDW_CLK_POST_DEPREPARE
);
999 EXPORT_SYMBOL(sdw_bus_exit_clk_stop
);
1001 int sdw_configure_dpn_intr(struct sdw_slave
*slave
,
1002 int port
, bool enable
, int mask
)
1008 addr
= SDW_DPN_INTMASK(port
);
1010 /* Set/Clear port ready interrupt mask */
1013 val
|= SDW_DPN_INT_PORT_READY
;
1016 val
&= ~SDW_DPN_INT_PORT_READY
;
1019 ret
= sdw_update(slave
, addr
, (mask
| SDW_DPN_INT_PORT_READY
), val
);
1021 dev_err(slave
->bus
->dev
,
1022 "SDW_DPN_INTMASK write failed:%d\n", val
);
1027 static int sdw_initialize_slave(struct sdw_slave
*slave
)
1029 struct sdw_slave_prop
*prop
= &slave
->prop
;
1034 * Set bus clash, parity and SCP implementation
1035 * defined interrupt mask
1036 * TODO: Read implementation defined interrupt mask
1037 * from Slave property
1039 val
= SDW_SCP_INT1_IMPL_DEF
| SDW_SCP_INT1_BUS_CLASH
|
1040 SDW_SCP_INT1_PARITY
;
1042 /* Enable SCP interrupts */
1043 ret
= sdw_update(slave
, SDW_SCP_INTMASK1
, val
, val
);
1045 dev_err(slave
->bus
->dev
,
1046 "SDW_SCP_INTMASK1 write failed:%d\n", ret
);
1050 /* No need to continue if DP0 is not present */
1051 if (!slave
->prop
.dp0_prop
)
1054 /* Enable DP0 interrupts */
1055 val
= prop
->dp0_prop
->imp_def_interrupts
;
1056 val
|= SDW_DP0_INT_PORT_READY
| SDW_DP0_INT_BRA_FAILURE
;
1058 ret
= sdw_update(slave
, SDW_DP0_INTMASK
, val
, val
);
1060 dev_err(slave
->bus
->dev
,
1061 "SDW_DP0_INTMASK read failed:%d\n", ret
);
1065 static int sdw_handle_dp0_interrupt(struct sdw_slave
*slave
, u8
*slave_status
)
1067 u8 clear
= 0, impl_int_mask
;
1068 int status
, status2
, ret
, count
= 0;
1070 status
= sdw_read(slave
, SDW_DP0_INT
);
1072 dev_err(slave
->bus
->dev
,
1073 "SDW_DP0_INT read failed:%d\n", status
);
1078 if (status
& SDW_DP0_INT_TEST_FAIL
) {
1079 dev_err(&slave
->dev
, "Test fail for port 0\n");
1080 clear
|= SDW_DP0_INT_TEST_FAIL
;
1084 * Assumption: PORT_READY interrupt will be received only for
1085 * ports implementing Channel Prepare state machine (CP_SM)
1088 if (status
& SDW_DP0_INT_PORT_READY
) {
1089 complete(&slave
->port_ready
[0]);
1090 clear
|= SDW_DP0_INT_PORT_READY
;
1093 if (status
& SDW_DP0_INT_BRA_FAILURE
) {
1094 dev_err(&slave
->dev
, "BRA failed\n");
1095 clear
|= SDW_DP0_INT_BRA_FAILURE
;
1098 impl_int_mask
= SDW_DP0_INT_IMPDEF1
|
1099 SDW_DP0_INT_IMPDEF2
| SDW_DP0_INT_IMPDEF3
;
1101 if (status
& impl_int_mask
) {
1102 clear
|= impl_int_mask
;
1103 *slave_status
= clear
;
1106 /* clear the interrupt */
1107 ret
= sdw_write(slave
, SDW_DP0_INT
, clear
);
1109 dev_err(slave
->bus
->dev
,
1110 "SDW_DP0_INT write failed:%d\n", ret
);
1114 /* Read DP0 interrupt again */
1115 status2
= sdw_read(slave
, SDW_DP0_INT
);
1117 dev_err(slave
->bus
->dev
,
1118 "SDW_DP0_INT read failed:%d\n", status2
);
1125 /* we can get alerts while processing so keep retrying */
1126 } while (status
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
1128 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
1129 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on DP0 read\n");
1134 static int sdw_handle_port_interrupt(struct sdw_slave
*slave
,
1135 int port
, u8
*slave_status
)
1137 u8 clear
= 0, impl_int_mask
;
1138 int status
, status2
, ret
, count
= 0;
1142 return sdw_handle_dp0_interrupt(slave
, slave_status
);
1144 addr
= SDW_DPN_INT(port
);
1145 status
= sdw_read(slave
, addr
);
1147 dev_err(slave
->bus
->dev
,
1148 "SDW_DPN_INT read failed:%d\n", status
);
1154 if (status
& SDW_DPN_INT_TEST_FAIL
) {
1155 dev_err(&slave
->dev
, "Test fail for port:%d\n", port
);
1156 clear
|= SDW_DPN_INT_TEST_FAIL
;
1160 * Assumption: PORT_READY interrupt will be received only
1161 * for ports implementing CP_SM.
1163 if (status
& SDW_DPN_INT_PORT_READY
) {
1164 complete(&slave
->port_ready
[port
]);
1165 clear
|= SDW_DPN_INT_PORT_READY
;
1168 impl_int_mask
= SDW_DPN_INT_IMPDEF1
|
1169 SDW_DPN_INT_IMPDEF2
| SDW_DPN_INT_IMPDEF3
;
1171 if (status
& impl_int_mask
) {
1172 clear
|= impl_int_mask
;
1173 *slave_status
= clear
;
1176 /* clear the interrupt */
1177 ret
= sdw_write(slave
, addr
, clear
);
1179 dev_err(slave
->bus
->dev
,
1180 "SDW_DPN_INT write failed:%d\n", ret
);
1184 /* Read DPN interrupt again */
1185 status2
= sdw_read(slave
, addr
);
1187 dev_err(slave
->bus
->dev
,
1188 "SDW_DPN_INT read failed:%d\n", status2
);
1195 /* we can get alerts while processing so keep retrying */
1196 } while (status
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
1198 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
1199 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on port read");
1204 static int sdw_handle_slave_alerts(struct sdw_slave
*slave
)
1206 struct sdw_slave_intr_status slave_intr
;
1207 u8 clear
= 0, bit
, port_status
[15] = {0};
1208 int port_num
, stat
, ret
, count
= 0;
1210 bool slave_notify
= false;
1211 u8 buf
, buf2
[2], _buf
, _buf2
[2];
1213 sdw_modify_slave_status(slave
, SDW_SLAVE_ALERT
);
1215 ret
= pm_runtime_get_sync(&slave
->dev
);
1216 if (ret
< 0 && ret
!= -EACCES
) {
1217 dev_err(&slave
->dev
, "Failed to resume device: %d\n", ret
);
1218 pm_runtime_put_noidle(slave
->bus
->dev
);
1222 /* Read Instat 1, Instat 2 and Instat 3 registers */
1223 ret
= sdw_read(slave
, SDW_SCP_INT1
);
1225 dev_err(slave
->bus
->dev
,
1226 "SDW_SCP_INT1 read failed:%d\n", ret
);
1231 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, buf2
);
1233 dev_err(slave
->bus
->dev
,
1234 "SDW_SCP_INT2/3 read failed:%d\n", ret
);
1240 * Check parity, bus clash and Slave (impl defined)
1243 if (buf
& SDW_SCP_INT1_PARITY
) {
1244 dev_err(&slave
->dev
, "Parity error detected\n");
1245 clear
|= SDW_SCP_INT1_PARITY
;
1248 if (buf
& SDW_SCP_INT1_BUS_CLASH
) {
1249 dev_err(&slave
->dev
, "Bus clash error detected\n");
1250 clear
|= SDW_SCP_INT1_BUS_CLASH
;
1254 * When bus clash or parity errors are detected, such errors
1255 * are unlikely to be recoverable errors.
1256 * TODO: In such scenario, reset bus. Make this configurable
1257 * via sysfs property with bus reset being the default.
1260 if (buf
& SDW_SCP_INT1_IMPL_DEF
) {
1261 dev_dbg(&slave
->dev
, "Slave impl defined interrupt\n");
1262 clear
|= SDW_SCP_INT1_IMPL_DEF
;
1263 slave_notify
= true;
1266 /* Check port 0 - 3 interrupts */
1267 port
= buf
& SDW_SCP_INT1_PORT0_3
;
1269 /* To get port number corresponding to bits, shift it */
1270 port
= port
>> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3
);
1271 for_each_set_bit(bit
, &port
, 8) {
1272 sdw_handle_port_interrupt(slave
, bit
,
1276 /* Check if cascade 2 interrupt is present */
1277 if (buf
& SDW_SCP_INT1_SCP2_CASCADE
) {
1278 port
= buf2
[0] & SDW_SCP_INTSTAT2_PORT4_10
;
1279 for_each_set_bit(bit
, &port
, 8) {
1280 /* scp2 ports start from 4 */
1282 sdw_handle_port_interrupt(slave
,
1284 &port_status
[port_num
]);
1288 /* now check last cascade */
1289 if (buf2
[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE
) {
1290 port
= buf2
[1] & SDW_SCP_INTSTAT3_PORT11_14
;
1291 for_each_set_bit(bit
, &port
, 8) {
1292 /* scp3 ports start from 11 */
1293 port_num
= bit
+ 10;
1294 sdw_handle_port_interrupt(slave
,
1296 &port_status
[port_num
]);
1300 /* Update the Slave driver */
1301 if (slave_notify
&& slave
->ops
&&
1302 slave
->ops
->interrupt_callback
) {
1303 slave_intr
.control_port
= clear
;
1304 memcpy(slave_intr
.port
, &port_status
,
1305 sizeof(slave_intr
.port
));
1307 slave
->ops
->interrupt_callback(slave
, &slave_intr
);
1311 ret
= sdw_write(slave
, SDW_SCP_INT1
, clear
);
1313 dev_err(slave
->bus
->dev
,
1314 "SDW_SCP_INT1 write failed:%d\n", ret
);
1319 * Read status again to ensure no new interrupts arrived
1320 * while servicing interrupts.
1322 ret
= sdw_read(slave
, SDW_SCP_INT1
);
1324 dev_err(slave
->bus
->dev
,
1325 "SDW_SCP_INT1 read failed:%d\n", ret
);
1330 ret
= sdw_nread(slave
, SDW_SCP_INTSTAT2
, 2, _buf2
);
1332 dev_err(slave
->bus
->dev
,
1333 "SDW_SCP_INT2/3 read failed:%d\n", ret
);
1337 /* Make sure no interrupts are pending */
1339 buf2
[0] &= _buf2
[0];
1340 buf2
[1] &= _buf2
[1];
1341 stat
= buf
|| buf2
[0] || buf2
[1];
1344 * Exit loop if Slave is continuously in ALERT state even
1345 * after servicing the interrupt multiple times.
1349 /* we can get alerts while processing so keep retrying */
1350 } while (stat
!= 0 && count
< SDW_READ_INTR_CLEAR_RETRY
);
1352 if (count
== SDW_READ_INTR_CLEAR_RETRY
)
1353 dev_warn(slave
->bus
->dev
, "Reached MAX_RETRY on alert read\n");
1356 pm_runtime_mark_last_busy(&slave
->dev
);
1357 pm_runtime_put_autosuspend(&slave
->dev
);
1362 static int sdw_update_slave_status(struct sdw_slave
*slave
,
1363 enum sdw_slave_status status
)
1367 if (!slave
->probed
) {
1369 * the slave status update is typically handled in an
1370 * interrupt thread, which can race with the driver
1371 * probe, e.g. when a module needs to be loaded.
1373 * make sure the probe is complete before updating
1376 time
= wait_for_completion_timeout(&slave
->probe_complete
,
1377 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT
));
1379 dev_err(&slave
->dev
, "Probe not complete, timed out\n");
1384 if (!slave
->ops
|| !slave
->ops
->update_status
)
1387 return slave
->ops
->update_status(slave
, status
);
1391 * sdw_handle_slave_status() - Handle Slave status
1392 * @bus: SDW bus instance
1393 * @status: Status for all Slave(s)
1395 int sdw_handle_slave_status(struct sdw_bus
*bus
,
1396 enum sdw_slave_status status
[])
1398 enum sdw_slave_status prev_status
;
1399 struct sdw_slave
*slave
;
1400 bool attached_initializing
;
1403 /* first check if any Slaves fell off the bus */
1404 for (i
= 1; i
<= SDW_MAX_DEVICES
; i
++) {
1405 mutex_lock(&bus
->bus_lock
);
1406 if (test_bit(i
, bus
->assigned
) == false) {
1407 mutex_unlock(&bus
->bus_lock
);
1410 mutex_unlock(&bus
->bus_lock
);
1412 slave
= sdw_get_slave(bus
, i
);
1416 if (status
[i
] == SDW_SLAVE_UNATTACHED
&&
1417 slave
->status
!= SDW_SLAVE_UNATTACHED
)
1418 sdw_modify_slave_status(slave
, SDW_SLAVE_UNATTACHED
);
1421 if (status
[0] == SDW_SLAVE_ATTACHED
) {
1422 dev_dbg(bus
->dev
, "Slave attached, programming device number\n");
1423 ret
= sdw_program_device_num(bus
);
1425 dev_err(bus
->dev
, "Slave attach failed: %d\n", ret
);
1427 * programming a device number will have side effects,
1428 * so we deal with other devices at a later time
1433 /* Continue to check other slave statuses */
1434 for (i
= 1; i
<= SDW_MAX_DEVICES
; i
++) {
1435 mutex_lock(&bus
->bus_lock
);
1436 if (test_bit(i
, bus
->assigned
) == false) {
1437 mutex_unlock(&bus
->bus_lock
);
1440 mutex_unlock(&bus
->bus_lock
);
1442 slave
= sdw_get_slave(bus
, i
);
1446 attached_initializing
= false;
1448 switch (status
[i
]) {
1449 case SDW_SLAVE_UNATTACHED
:
1450 if (slave
->status
== SDW_SLAVE_UNATTACHED
)
1453 sdw_modify_slave_status(slave
, SDW_SLAVE_UNATTACHED
);
1456 case SDW_SLAVE_ALERT
:
1457 ret
= sdw_handle_slave_alerts(slave
);
1460 "Slave %d alert handling failed: %d\n",
1464 case SDW_SLAVE_ATTACHED
:
1465 if (slave
->status
== SDW_SLAVE_ATTACHED
)
1468 prev_status
= slave
->status
;
1469 sdw_modify_slave_status(slave
, SDW_SLAVE_ATTACHED
);
1471 if (prev_status
== SDW_SLAVE_ALERT
)
1474 attached_initializing
= true;
1476 ret
= sdw_initialize_slave(slave
);
1479 "Slave %d initialization failed: %d\n",
1485 dev_err(bus
->dev
, "Invalid slave %d status:%d\n",
1490 ret
= sdw_update_slave_status(slave
, status
[i
]);
1492 dev_err(slave
->bus
->dev
,
1493 "Update Slave status failed:%d\n", ret
);
1494 if (attached_initializing
)
1495 complete(&slave
->initialization_complete
);
1500 EXPORT_SYMBOL(sdw_handle_slave_status
);
1502 void sdw_clear_slave_status(struct sdw_bus
*bus
, u32 request
)
1504 struct sdw_slave
*slave
;
1507 /* Check all non-zero devices */
1508 for (i
= 1; i
<= SDW_MAX_DEVICES
; i
++) {
1509 mutex_lock(&bus
->bus_lock
);
1510 if (test_bit(i
, bus
->assigned
) == false) {
1511 mutex_unlock(&bus
->bus_lock
);
1514 mutex_unlock(&bus
->bus_lock
);
1516 slave
= sdw_get_slave(bus
, i
);
1520 if (slave
->status
!= SDW_SLAVE_UNATTACHED
)
1521 sdw_modify_slave_status(slave
, SDW_SLAVE_UNATTACHED
);
1523 /* keep track of request, used in pm_runtime resume */
1524 slave
->unattach_request
= request
;
1527 EXPORT_SYMBOL(sdw_clear_slave_status
);