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>
11 #include "sysfs_local.h"
13 static DEFINE_IDA(sdw_ida);
15 static int sdw_get_id(struct sdw_bus *bus)
17 int rc = ida_alloc(&sdw_ida, GFP_KERNEL);
27 * sdw_bus_master_add() - add a bus Master instance
29 * @parent: parent device
30 * @fwnode: firmware node handle
32 * Initializes the bus instance, read properties and create child
35 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
36 struct fwnode_handle *fwnode)
38 struct sdw_master_prop *prop = NULL;
42 pr_err("SoundWire parent device is not set\n");
46 ret = sdw_get_id(bus);
48 dev_err(parent, "Failed to get bus id\n");
52 ret = sdw_master_device_add(bus, parent, fwnode);
54 dev_err(parent, "Failed to add master device at link %d\n",
60 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
64 if (!bus->compute_params) {
66 "Bandwidth allocation not configured, compute_params no set\n");
70 mutex_init(&bus->msg_lock);
71 mutex_init(&bus->bus_lock);
72 INIT_LIST_HEAD(&bus->slaves);
73 INIT_LIST_HEAD(&bus->m_rt_list);
76 * Initialize multi_link flag
77 * TODO: populate this flag by reading property from FW node
79 bus->multi_link = false;
80 if (bus->ops->read_prop) {
81 ret = bus->ops->read_prop(bus);
84 "Bus read properties failed:%d\n", ret);
89 sdw_bus_debugfs_init(bus);
92 * Device numbers in SoundWire are 0 through 15. Enumeration device
93 * number (0), Broadcast device number (15), Group numbers (12 and
94 * 13) and Master device number (14) are not used for assignment so
95 * mask these and other higher bits.
98 /* Set higher order bits */
99 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
101 /* Set enumuration device number and broadcast device number */
102 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
103 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
105 /* Set group device numbers and master device number */
106 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
107 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
108 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
111 * SDW is an enumerable bus, but devices can be powered off. So,
112 * they won't be able to report as present.
114 * Create Slave devices based on Slaves described in
115 * the respective firmware (ACPI/DT)
117 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
118 ret = sdw_acpi_find_slaves(bus);
119 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
120 ret = sdw_of_find_slaves(bus);
122 ret = -ENOTSUPP; /* No ACPI/DT so error out */
125 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
130 * Initialize clock values based on Master properties. The max
131 * frequency is read from max_clk_freq property. Current assumption
132 * is that the bus will start at highest clock frequency when
135 * Default active bank will be 0 as out of reset the Slaves have
136 * to start with bank 0 (Table 40 of Spec)
139 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
140 bus->params.curr_dr_freq = bus->params.max_dr_freq;
141 bus->params.curr_bank = SDW_BANK0;
142 bus->params.next_bank = SDW_BANK1;
146 EXPORT_SYMBOL(sdw_bus_master_add);
148 static int sdw_delete_slave(struct device *dev, void *data)
150 struct sdw_slave *slave = dev_to_sdw_dev(dev);
151 struct sdw_bus *bus = slave->bus;
153 pm_runtime_disable(dev);
155 sdw_slave_debugfs_exit(slave);
157 mutex_lock(&bus->bus_lock);
159 if (slave->dev_num) /* clear dev_num if assigned */
160 clear_bit(slave->dev_num, bus->assigned);
162 list_del_init(&slave->node);
163 mutex_unlock(&bus->bus_lock);
165 device_unregister(dev);
170 * sdw_bus_master_delete() - delete the bus master instance
171 * @bus: bus to be deleted
173 * Remove the instance, delete the child devices.
175 void sdw_bus_master_delete(struct sdw_bus *bus)
177 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
178 sdw_master_device_del(bus);
180 sdw_bus_debugfs_exit(bus);
181 ida_free(&sdw_ida, bus->id);
183 EXPORT_SYMBOL(sdw_bus_master_delete);
189 static inline int find_response_code(enum sdw_command_response resp)
195 case SDW_CMD_IGNORED:
198 case SDW_CMD_TIMEOUT:
206 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
208 int retry = bus->prop.err_threshold;
209 enum sdw_command_response resp;
212 for (i = 0; i <= retry; i++) {
213 resp = bus->ops->xfer_msg(bus, msg);
214 ret = find_response_code(resp);
216 /* if cmd is ok or ignored return */
217 if (ret == 0 || ret == -ENODATA)
224 static inline int do_transfer_defer(struct sdw_bus *bus,
226 struct sdw_defer *defer)
228 int retry = bus->prop.err_threshold;
229 enum sdw_command_response resp;
233 defer->length = msg->len;
234 init_completion(&defer->complete);
236 for (i = 0; i <= retry; i++) {
237 resp = bus->ops->xfer_msg_defer(bus, msg, defer);
238 ret = find_response_code(resp);
239 /* if cmd is ok or ignored return */
240 if (ret == 0 || ret == -ENODATA)
247 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
249 int retry = bus->prop.err_threshold;
250 enum sdw_command_response resp;
253 for (i = 0; i <= retry; i++) {
254 resp = bus->ops->reset_page_addr(bus, dev_num);
255 ret = find_response_code(resp);
256 /* if cmd is ok or ignored return */
257 if (ret == 0 || ret == -ENODATA)
264 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
268 ret = do_transfer(bus, msg);
269 if (ret != 0 && ret != -ENODATA)
270 dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
272 (msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
273 msg->addr, msg->len);
276 sdw_reset_page(bus, msg->dev_num);
282 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
284 * @msg: SDW message to be xfered
286 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
290 mutex_lock(&bus->msg_lock);
292 ret = sdw_transfer_unlocked(bus, msg);
294 mutex_unlock(&bus->msg_lock);
300 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
302 * @msg: SDW message to be xfered
303 * @defer: Defer block for signal completion
305 * Caller needs to hold the msg_lock lock while calling this
307 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
308 struct sdw_defer *defer)
312 if (!bus->ops->xfer_msg_defer)
315 ret = do_transfer_defer(bus, msg, defer);
316 if (ret != 0 && ret != -ENODATA)
317 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
321 sdw_reset_page(bus, msg->dev_num);
326 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
327 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
329 memset(msg, 0, sizeof(*msg));
330 msg->addr = addr; /* addr is 16 bit and truncated here */
332 msg->dev_num = dev_num;
336 if (addr < SDW_REG_NO_PAGE) /* no paging area */
339 if (addr >= SDW_REG_MAX) { /* illegal addr */
340 pr_err("SDW: Invalid address %x passed\n", addr);
344 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
345 if (slave && !slave->prop.paging_support)
347 /* no need for else as that will fall-through to paging */
350 /* paging mandatory */
351 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
352 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
357 pr_err("SDW: No slave for paging addr\n");
361 if (!slave->prop.paging_support) {
363 "address %x needs paging but no support\n", addr);
367 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
368 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
369 msg->addr |= BIT(15);
376 * Read/Write IO functions.
377 * no_pm versions can only be called by the bus, e.g. while enumerating or
378 * handling suspend-resume sequences.
379 * all clients need to use the pm versions
383 sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
388 ret = sdw_fill_msg(&msg, slave, addr, count,
389 slave->dev_num, SDW_MSG_FLAG_READ, val);
393 ret = sdw_transfer(slave->bus, &msg);
394 if (slave->is_mockup_device)
400 sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
405 ret = sdw_fill_msg(&msg, slave, addr, count,
406 slave->dev_num, SDW_MSG_FLAG_WRITE, (u8 *)val);
410 ret = sdw_transfer(slave->bus, &msg);
411 if (slave->is_mockup_device)
416 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
418 return sdw_nwrite_no_pm(slave, addr, 1, &value);
420 EXPORT_SYMBOL(sdw_write_no_pm);
423 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
429 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
430 SDW_MSG_FLAG_READ, &buf);
434 ret = sdw_transfer(bus, &msg);
442 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
447 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
448 SDW_MSG_FLAG_WRITE, &value);
452 return sdw_transfer(bus, &msg);
455 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
461 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
462 SDW_MSG_FLAG_READ, &buf);
466 ret = sdw_transfer_unlocked(bus, &msg);
472 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
474 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
479 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
480 SDW_MSG_FLAG_WRITE, &value);
484 return sdw_transfer_unlocked(bus, &msg);
486 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
488 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
493 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
499 EXPORT_SYMBOL(sdw_read_no_pm);
501 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
505 tmp = sdw_read_no_pm(slave, addr);
509 tmp = (tmp & ~mask) | val;
510 return sdw_write_no_pm(slave, addr, tmp);
512 EXPORT_SYMBOL(sdw_update_no_pm);
514 /* Read-Modify-Write Slave register */
515 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
519 tmp = sdw_read(slave, addr);
523 tmp = (tmp & ~mask) | val;
524 return sdw_write(slave, addr, tmp);
526 EXPORT_SYMBOL(sdw_update);
529 * sdw_nread() - Read "n" contiguous SDW Slave registers
531 * @addr: Register address
533 * @val: Buffer for values to be read
535 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
539 ret = pm_runtime_get_sync(&slave->dev);
540 if (ret < 0 && ret != -EACCES) {
541 pm_runtime_put_noidle(&slave->dev);
545 ret = sdw_nread_no_pm(slave, addr, count, val);
547 pm_runtime_mark_last_busy(&slave->dev);
548 pm_runtime_put(&slave->dev);
552 EXPORT_SYMBOL(sdw_nread);
555 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
557 * @addr: Register address
559 * @val: Buffer for values to be written
561 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
565 ret = pm_runtime_get_sync(&slave->dev);
566 if (ret < 0 && ret != -EACCES) {
567 pm_runtime_put_noidle(&slave->dev);
571 ret = sdw_nwrite_no_pm(slave, addr, count, val);
573 pm_runtime_mark_last_busy(&slave->dev);
574 pm_runtime_put(&slave->dev);
578 EXPORT_SYMBOL(sdw_nwrite);
581 * sdw_read() - Read a SDW Slave register
583 * @addr: Register address
585 int sdw_read(struct sdw_slave *slave, u32 addr)
590 ret = sdw_nread(slave, addr, 1, &buf);
596 EXPORT_SYMBOL(sdw_read);
599 * sdw_write() - Write a SDW Slave register
601 * @addr: Register address
602 * @value: Register value
604 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
606 return sdw_nwrite(slave, addr, 1, &value);
608 EXPORT_SYMBOL(sdw_write);
614 /* called with bus_lock held */
615 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
617 struct sdw_slave *slave;
619 list_for_each_entry(slave, &bus->slaves, node) {
620 if (slave->dev_num == i)
627 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
629 if (slave->id.mfg_id != id.mfg_id ||
630 slave->id.part_id != id.part_id ||
631 slave->id.class_id != id.class_id ||
632 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
633 slave->id.unique_id != id.unique_id))
638 EXPORT_SYMBOL(sdw_compare_devid);
640 /* called with bus_lock held */
641 static int sdw_get_device_num(struct sdw_slave *slave)
645 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
646 if (bit == SDW_MAX_DEVICES) {
652 * Do not update dev_num in Slave data structure here,
653 * Update once program dev_num is successful
655 set_bit(bit, slave->bus->assigned);
661 static int sdw_assign_device_num(struct sdw_slave *slave)
663 struct sdw_bus *bus = slave->bus;
665 bool new_device = false;
667 /* check first if device number is assigned, if so reuse that */
668 if (!slave->dev_num) {
669 if (!slave->dev_num_sticky) {
670 mutex_lock(&slave->bus->bus_lock);
671 dev_num = sdw_get_device_num(slave);
672 mutex_unlock(&slave->bus->bus_lock);
674 dev_err(bus->dev, "Get dev_num failed: %d\n",
678 slave->dev_num = dev_num;
679 slave->dev_num_sticky = dev_num;
682 slave->dev_num = slave->dev_num_sticky;
688 "Slave already registered, reusing dev_num:%d\n",
691 /* Clear the slave->dev_num to transfer message on device 0 */
692 dev_num = slave->dev_num;
695 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
697 dev_err(bus->dev, "Program device_num %d failed: %d\n",
702 /* After xfer of msg, restore dev_num */
703 slave->dev_num = slave->dev_num_sticky;
708 void sdw_extract_slave_id(struct sdw_bus *bus,
709 u64 addr, struct sdw_slave_id *id)
711 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
713 id->sdw_version = SDW_VERSION(addr);
714 id->unique_id = SDW_UNIQUE_ID(addr);
715 id->mfg_id = SDW_MFG_ID(addr);
716 id->part_id = SDW_PART_ID(addr);
717 id->class_id = SDW_CLASS_ID(addr);
720 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
721 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
723 EXPORT_SYMBOL(sdw_extract_slave_id);
725 static int sdw_program_device_num(struct sdw_bus *bus)
727 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
728 struct sdw_slave *slave, *_s;
729 struct sdw_slave_id id;
735 /* No Slave, so use raw xfer api */
736 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
737 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
742 ret = sdw_transfer(bus, &msg);
743 if (ret == -ENODATA) { /* end of device id reads */
744 dev_dbg(bus->dev, "No more devices to enumerate\n");
749 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
754 * Construct the addr and extract. Cast the higher shift
755 * bits to avoid truncation due to size limit.
757 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
758 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
761 sdw_extract_slave_id(bus, addr, &id);
764 /* Now compare with entries */
765 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
766 if (sdw_compare_devid(slave, id) == 0) {
770 * Assign a new dev_num to this Slave and
771 * not mark it present. It will be marked
772 * present after it reports ATTACHED on new
775 ret = sdw_assign_device_num(slave);
778 "Assign dev_num failed:%d\n",
788 /* TODO: Park this device in Group 13 */
791 * add Slave device even if there is no platform
792 * firmware description. There will be no driver probe
793 * but the user/integration will be able to see the
794 * device, enumeration status and device number in sysfs
796 sdw_slave_add(bus, &id, NULL);
798 dev_err(bus->dev, "Slave Entry not found\n");
804 * Check till error out or retry (count) exhausts.
805 * Device can drop off and rejoin during enumeration
806 * so count till twice the bound.
809 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
814 static void sdw_modify_slave_status(struct sdw_slave *slave,
815 enum sdw_slave_status status)
817 struct sdw_bus *bus = slave->bus;
819 mutex_lock(&bus->bus_lock);
822 "%s: changing status slave %d status %d new status %d\n",
823 __func__, slave->dev_num, slave->status, status);
825 if (status == SDW_SLAVE_UNATTACHED) {
827 "%s: initializing enumeration and init completion for Slave %d\n",
828 __func__, slave->dev_num);
830 init_completion(&slave->enumeration_complete);
831 init_completion(&slave->initialization_complete);
833 } else if ((status == SDW_SLAVE_ATTACHED) &&
834 (slave->status == SDW_SLAVE_UNATTACHED)) {
836 "%s: signaling enumeration completion for Slave %d\n",
837 __func__, slave->dev_num);
839 complete(&slave->enumeration_complete);
841 slave->status = status;
842 mutex_unlock(&bus->bus_lock);
845 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
846 enum sdw_clk_stop_mode mode,
847 enum sdw_clk_stop_type type)
851 if (slave->ops && slave->ops->clk_stop) {
852 ret = slave->ops->clk_stop(slave, mode, type);
860 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
861 enum sdw_clk_stop_mode mode,
868 wake_en = slave->prop.wake_capable;
871 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
873 if (mode == SDW_CLK_STOP_MODE1)
874 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
877 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
879 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
882 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
886 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
889 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
891 if (ret < 0 && ret != -ENODATA)
892 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
897 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
899 int retry = bus->clk_stop_timeout;
903 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
906 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
909 val &= SDW_SCP_STAT_CLK_STP_NF;
911 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
916 usleep_range(1000, 1500);
920 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
927 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
929 * @bus: SDW bus instance
931 * Query Slave for clock stop mode and prepare for that mode.
933 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
935 bool simple_clk_stop = true;
936 struct sdw_slave *slave;
937 bool is_slave = false;
941 * In order to save on transition time, prepare
942 * each Slave and then wait for all Slave(s) to be
943 * prepared for clock stop.
944 * If one of the Slave devices has lost sync and
945 * replies with Command Ignored/-ENODATA, we continue
948 list_for_each_entry(slave, &bus->slaves, node) {
952 if (slave->status != SDW_SLAVE_ATTACHED &&
953 slave->status != SDW_SLAVE_ALERT)
956 /* Identify if Slave(s) are available on Bus */
959 ret = sdw_slave_clk_stop_callback(slave,
961 SDW_CLK_PRE_PREPARE);
962 if (ret < 0 && ret != -ENODATA) {
963 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
967 /* Only prepare a Slave device if needed */
968 if (!slave->prop.simple_clk_stop_capable) {
969 simple_clk_stop = false;
971 ret = sdw_slave_clk_stop_prepare(slave,
974 if (ret < 0 && ret != -ENODATA) {
975 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
981 /* Skip remaining clock stop preparation if no Slave is attached */
986 * Don't wait for all Slaves to be ready if they follow the simple
989 if (!simple_clk_stop) {
990 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
991 SDW_BROADCAST_DEV_NUM);
993 * if there are no Slave devices present and the reply is
994 * Command_Ignored/-ENODATA, we don't need to continue with the
995 * flow and can just return here. The error code is not modified
996 * and its handling left as an exercise for the caller.
1002 /* Inform slaves that prep is done */
1003 list_for_each_entry(slave, &bus->slaves, node) {
1004 if (!slave->dev_num)
1007 if (slave->status != SDW_SLAVE_ATTACHED &&
1008 slave->status != SDW_SLAVE_ALERT)
1011 ret = sdw_slave_clk_stop_callback(slave,
1013 SDW_CLK_POST_PREPARE);
1015 if (ret < 0 && ret != -ENODATA) {
1016 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1023 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1026 * sdw_bus_clk_stop: stop bus clock
1028 * @bus: SDW bus instance
1030 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1031 * write to SCP_CTRL register.
1033 int sdw_bus_clk_stop(struct sdw_bus *bus)
1038 * broadcast clock stop now, attached Slaves will ACK this,
1039 * unattached will ignore
1041 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1042 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1044 if (ret != -ENODATA)
1045 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
1051 EXPORT_SYMBOL(sdw_bus_clk_stop);
1054 * sdw_bus_exit_clk_stop: Exit clock stop mode
1056 * @bus: SDW bus instance
1058 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1059 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1062 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1064 bool simple_clk_stop = true;
1065 struct sdw_slave *slave;
1066 bool is_slave = false;
1070 * In order to save on transition time, de-prepare
1071 * each Slave and then wait for all Slave(s) to be
1072 * de-prepared after clock resume.
1074 list_for_each_entry(slave, &bus->slaves, node) {
1075 if (!slave->dev_num)
1078 if (slave->status != SDW_SLAVE_ATTACHED &&
1079 slave->status != SDW_SLAVE_ALERT)
1082 /* Identify if Slave(s) are available on Bus */
1085 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1086 SDW_CLK_PRE_DEPREPARE);
1088 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
1090 /* Only de-prepare a Slave device if needed */
1091 if (!slave->prop.simple_clk_stop_capable) {
1092 simple_clk_stop = false;
1094 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1098 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
1102 /* Skip remaining clock stop de-preparation if no Slave is attached */
1107 * Don't wait for all Slaves to be ready if they follow the simple
1110 if (!simple_clk_stop) {
1111 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1113 dev_warn(&slave->dev, "clock stop deprepare wait failed:%d\n", ret);
1116 list_for_each_entry(slave, &bus->slaves, node) {
1117 if (!slave->dev_num)
1120 if (slave->status != SDW_SLAVE_ATTACHED &&
1121 slave->status != SDW_SLAVE_ALERT)
1124 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1125 SDW_CLK_POST_DEPREPARE);
1127 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
1132 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1134 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1135 int port, bool enable, int mask)
1141 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1142 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1143 enable ? "on" : "off");
1144 mask |= SDW_DPN_INT_TEST_FAIL;
1147 addr = SDW_DPN_INTMASK(port);
1149 /* Set/Clear port ready interrupt mask */
1152 val |= SDW_DPN_INT_PORT_READY;
1155 val &= ~SDW_DPN_INT_PORT_READY;
1158 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1160 dev_err(&slave->dev,
1161 "SDW_DPN_INTMASK write failed:%d\n", val);
1166 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1168 u32 mclk_freq = slave->bus->prop.mclk_freq;
1169 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1176 * frequency base and scale registers are required for SDCA
1177 * devices. They may also be used for 1.2+/non-SDCA devices,
1178 * but we will need a DisCo property to cover this case
1180 if (!slave->id.class_id)
1184 dev_err(&slave->dev,
1185 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1190 * map base frequency using Table 89 of SoundWire 1.2 spec.
1191 * The order of the tests just follows the specification, this
1192 * is not a selection between possible values or a search for
1193 * the best value but just a mapping. Only one case per platform
1195 * Some BIOS have inconsistent values for mclk_freq but a
1196 * correct root so we force the mclk_freq to avoid variations.
1198 if (!(19200000 % mclk_freq)) {
1199 mclk_freq = 19200000;
1200 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1201 } else if (!(24000000 % mclk_freq)) {
1202 mclk_freq = 24000000;
1203 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1204 } else if (!(24576000 % mclk_freq)) {
1205 mclk_freq = 24576000;
1206 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1207 } else if (!(22579200 % mclk_freq)) {
1208 mclk_freq = 22579200;
1209 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1210 } else if (!(32000000 % mclk_freq)) {
1211 mclk_freq = 32000000;
1212 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1214 dev_err(&slave->dev,
1215 "Unsupported clock base, mclk %d\n",
1220 if (mclk_freq % curr_freq) {
1221 dev_err(&slave->dev,
1222 "mclk %d is not multiple of bus curr_freq %d\n",
1223 mclk_freq, curr_freq);
1227 scale = mclk_freq / curr_freq;
1230 * map scale to Table 90 of SoundWire 1.2 spec - and check
1231 * that the scale is a power of two and maximum 64
1233 scale_index = ilog2(scale);
1235 if (BIT(scale_index) != scale || scale_index > 6) {
1236 dev_err(&slave->dev,
1237 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1238 scale, mclk_freq, curr_freq);
1243 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1245 dev_err(&slave->dev,
1246 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1250 /* initialize scale for both banks */
1251 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1253 dev_err(&slave->dev,
1254 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1257 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1259 dev_err(&slave->dev,
1260 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1262 dev_dbg(&slave->dev,
1263 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1264 base, scale_index, mclk_freq, curr_freq);
1269 static int sdw_initialize_slave(struct sdw_slave *slave)
1271 struct sdw_slave_prop *prop = &slave->prop;
1276 ret = sdw_slave_set_frequency(slave);
1280 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1281 /* Clear bus clash interrupt before enabling interrupt mask */
1282 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1284 dev_err(&slave->dev,
1285 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1288 if (status & SDW_SCP_INT1_BUS_CLASH) {
1289 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1290 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1292 dev_err(&slave->dev,
1293 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1298 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1299 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1300 /* Clear parity interrupt before enabling interrupt mask */
1301 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1303 dev_err(&slave->dev,
1304 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1307 if (status & SDW_SCP_INT1_PARITY) {
1308 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1309 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1311 dev_err(&slave->dev,
1312 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1319 * Set SCP_INT1_MASK register, typically bus clash and
1320 * implementation-defined interrupt mask. The Parity detection
1321 * may not always be correct on startup so its use is
1322 * device-dependent, it might e.g. only be enabled in
1323 * steady-state after a couple of frames.
1325 val = slave->prop.scp_int1_mask;
1327 /* Enable SCP interrupts */
1328 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1330 dev_err(&slave->dev,
1331 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
1335 /* No need to continue if DP0 is not present */
1336 if (!slave->prop.dp0_prop)
1339 /* Enable DP0 interrupts */
1340 val = prop->dp0_prop->imp_def_interrupts;
1341 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1343 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1345 dev_err(&slave->dev,
1346 "SDW_DP0_INTMASK read failed:%d\n", ret);
1350 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1352 u8 clear, impl_int_mask;
1353 int status, status2, ret, count = 0;
1355 status = sdw_read_no_pm(slave, SDW_DP0_INT);
1357 dev_err(&slave->dev,
1358 "SDW_DP0_INT read failed:%d\n", status);
1363 clear = status & ~SDW_DP0_INTERRUPTS;
1365 if (status & SDW_DP0_INT_TEST_FAIL) {
1366 dev_err(&slave->dev, "Test fail for port 0\n");
1367 clear |= SDW_DP0_INT_TEST_FAIL;
1371 * Assumption: PORT_READY interrupt will be received only for
1372 * ports implementing Channel Prepare state machine (CP_SM)
1375 if (status & SDW_DP0_INT_PORT_READY) {
1376 complete(&slave->port_ready[0]);
1377 clear |= SDW_DP0_INT_PORT_READY;
1380 if (status & SDW_DP0_INT_BRA_FAILURE) {
1381 dev_err(&slave->dev, "BRA failed\n");
1382 clear |= SDW_DP0_INT_BRA_FAILURE;
1385 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1386 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1388 if (status & impl_int_mask) {
1389 clear |= impl_int_mask;
1390 *slave_status = clear;
1393 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
1394 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
1396 dev_err(&slave->dev,
1397 "SDW_DP0_INT write failed:%d\n", ret);
1401 /* Read DP0 interrupt again */
1402 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
1404 dev_err(&slave->dev,
1405 "SDW_DP0_INT read failed:%d\n", status2);
1408 /* filter to limit loop to interrupts identified in the first status read */
1413 /* we can get alerts while processing so keep retrying */
1414 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1416 if (count == SDW_READ_INTR_CLEAR_RETRY)
1417 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
1422 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1423 int port, u8 *slave_status)
1425 u8 clear, impl_int_mask;
1426 int status, status2, ret, count = 0;
1430 return sdw_handle_dp0_interrupt(slave, slave_status);
1432 addr = SDW_DPN_INT(port);
1433 status = sdw_read_no_pm(slave, addr);
1435 dev_err(&slave->dev,
1436 "SDW_DPN_INT read failed:%d\n", status);
1442 clear = status & ~SDW_DPN_INTERRUPTS;
1444 if (status & SDW_DPN_INT_TEST_FAIL) {
1445 dev_err(&slave->dev, "Test fail for port:%d\n", port);
1446 clear |= SDW_DPN_INT_TEST_FAIL;
1450 * Assumption: PORT_READY interrupt will be received only
1451 * for ports implementing CP_SM.
1453 if (status & SDW_DPN_INT_PORT_READY) {
1454 complete(&slave->port_ready[port]);
1455 clear |= SDW_DPN_INT_PORT_READY;
1458 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1459 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1461 if (status & impl_int_mask) {
1462 clear |= impl_int_mask;
1463 *slave_status = clear;
1466 /* clear the interrupt but don't touch reserved fields */
1467 ret = sdw_write_no_pm(slave, addr, clear);
1469 dev_err(&slave->dev,
1470 "SDW_DPN_INT write failed:%d\n", ret);
1474 /* Read DPN interrupt again */
1475 status2 = sdw_read_no_pm(slave, addr);
1477 dev_err(&slave->dev,
1478 "SDW_DPN_INT read failed:%d\n", status2);
1481 /* filter to limit loop to interrupts identified in the first status read */
1486 /* we can get alerts while processing so keep retrying */
1487 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1489 if (count == SDW_READ_INTR_CLEAR_RETRY)
1490 dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
1495 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1497 struct sdw_slave_intr_status slave_intr;
1498 u8 clear = 0, bit, port_status[15] = {0};
1499 int port_num, stat, ret, count = 0;
1502 u8 sdca_cascade = 0;
1503 u8 buf, buf2[2], _buf, _buf2[2];
1507 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1509 ret = pm_runtime_get_sync(&slave->dev);
1510 if (ret < 0 && ret != -EACCES) {
1511 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1512 pm_runtime_put_noidle(&slave->dev);
1516 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1517 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1519 dev_err(&slave->dev,
1520 "SDW_SCP_INT1 read failed:%d\n", ret);
1525 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1527 dev_err(&slave->dev,
1528 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1532 if (slave->prop.is_sdca) {
1533 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1535 dev_err(&slave->dev,
1536 "SDW_DP0_INT read failed:%d\n", ret);
1539 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1543 slave_notify = false;
1546 * Check parity, bus clash and Slave (impl defined)
1549 if (buf & SDW_SCP_INT1_PARITY) {
1550 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1551 parity_quirk = !slave->first_interrupt_done &&
1552 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1554 if (parity_check && !parity_quirk)
1555 dev_err(&slave->dev, "Parity error detected\n");
1556 clear |= SDW_SCP_INT1_PARITY;
1559 if (buf & SDW_SCP_INT1_BUS_CLASH) {
1560 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1561 dev_err(&slave->dev, "Bus clash detected\n");
1562 clear |= SDW_SCP_INT1_BUS_CLASH;
1566 * When bus clash or parity errors are detected, such errors
1567 * are unlikely to be recoverable errors.
1568 * TODO: In such scenario, reset bus. Make this configurable
1569 * via sysfs property with bus reset being the default.
1572 if (buf & SDW_SCP_INT1_IMPL_DEF) {
1573 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1574 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1575 slave_notify = true;
1577 clear |= SDW_SCP_INT1_IMPL_DEF;
1580 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1582 slave_notify = true;
1584 /* Check port 0 - 3 interrupts */
1585 port = buf & SDW_SCP_INT1_PORT0_3;
1587 /* To get port number corresponding to bits, shift it */
1588 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1589 for_each_set_bit(bit, &port, 8) {
1590 sdw_handle_port_interrupt(slave, bit,
1594 /* Check if cascade 2 interrupt is present */
1595 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1596 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1597 for_each_set_bit(bit, &port, 8) {
1598 /* scp2 ports start from 4 */
1600 sdw_handle_port_interrupt(slave,
1602 &port_status[port_num]);
1606 /* now check last cascade */
1607 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1608 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1609 for_each_set_bit(bit, &port, 8) {
1610 /* scp3 ports start from 11 */
1611 port_num = bit + 10;
1612 sdw_handle_port_interrupt(slave,
1614 &port_status[port_num]);
1618 /* Update the Slave driver */
1619 if (slave_notify && slave->ops &&
1620 slave->ops->interrupt_callback) {
1621 slave_intr.sdca_cascade = sdca_cascade;
1622 slave_intr.control_port = clear;
1623 memcpy(slave_intr.port, &port_status,
1624 sizeof(slave_intr.port));
1626 slave->ops->interrupt_callback(slave, &slave_intr);
1630 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
1632 dev_err(&slave->dev,
1633 "SDW_SCP_INT1 write failed:%d\n", ret);
1637 /* at this point all initial interrupt sources were handled */
1638 slave->first_interrupt_done = true;
1641 * Read status again to ensure no new interrupts arrived
1642 * while servicing interrupts.
1644 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1646 dev_err(&slave->dev,
1647 "SDW_SCP_INT1 recheck read failed:%d\n", ret);
1652 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2);
1654 dev_err(&slave->dev,
1655 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
1659 if (slave->prop.is_sdca) {
1660 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1662 dev_err(&slave->dev,
1663 "SDW_DP0_INT recheck read failed:%d\n", ret);
1666 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1670 * Make sure no interrupts are pending, but filter to limit loop
1671 * to interrupts identified in the first status read
1674 buf2[0] &= _buf2[0];
1675 buf2[1] &= _buf2[1];
1676 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
1679 * Exit loop if Slave is continuously in ALERT state even
1680 * after servicing the interrupt multiple times.
1684 /* we can get alerts while processing so keep retrying */
1685 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1687 if (count == SDW_READ_INTR_CLEAR_RETRY)
1688 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
1691 pm_runtime_mark_last_busy(&slave->dev);
1692 pm_runtime_put_autosuspend(&slave->dev);
1697 static int sdw_update_slave_status(struct sdw_slave *slave,
1698 enum sdw_slave_status status)
1702 if (!slave->probed) {
1704 * the slave status update is typically handled in an
1705 * interrupt thread, which can race with the driver
1706 * probe, e.g. when a module needs to be loaded.
1708 * make sure the probe is complete before updating
1711 time = wait_for_completion_timeout(&slave->probe_complete,
1712 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT));
1714 dev_err(&slave->dev, "Probe not complete, timed out\n");
1719 if (!slave->ops || !slave->ops->update_status)
1722 return slave->ops->update_status(slave, status);
1726 * sdw_handle_slave_status() - Handle Slave status
1727 * @bus: SDW bus instance
1728 * @status: Status for all Slave(s)
1730 int sdw_handle_slave_status(struct sdw_bus *bus,
1731 enum sdw_slave_status status[])
1733 enum sdw_slave_status prev_status;
1734 struct sdw_slave *slave;
1735 bool attached_initializing;
1738 /* first check if any Slaves fell off the bus */
1739 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1740 mutex_lock(&bus->bus_lock);
1741 if (test_bit(i, bus->assigned) == false) {
1742 mutex_unlock(&bus->bus_lock);
1745 mutex_unlock(&bus->bus_lock);
1747 slave = sdw_get_slave(bus, i);
1751 if (status[i] == SDW_SLAVE_UNATTACHED &&
1752 slave->status != SDW_SLAVE_UNATTACHED)
1753 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1756 if (status[0] == SDW_SLAVE_ATTACHED) {
1757 dev_dbg(bus->dev, "Slave attached, programming device number\n");
1758 ret = sdw_program_device_num(bus);
1760 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
1762 * programming a device number will have side effects,
1763 * so we deal with other devices at a later time
1768 /* Continue to check other slave statuses */
1769 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1770 mutex_lock(&bus->bus_lock);
1771 if (test_bit(i, bus->assigned) == false) {
1772 mutex_unlock(&bus->bus_lock);
1775 mutex_unlock(&bus->bus_lock);
1777 slave = sdw_get_slave(bus, i);
1781 attached_initializing = false;
1783 switch (status[i]) {
1784 case SDW_SLAVE_UNATTACHED:
1785 if (slave->status == SDW_SLAVE_UNATTACHED)
1788 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1791 case SDW_SLAVE_ALERT:
1792 ret = sdw_handle_slave_alerts(slave);
1794 dev_err(&slave->dev,
1795 "Slave %d alert handling failed: %d\n",
1799 case SDW_SLAVE_ATTACHED:
1800 if (slave->status == SDW_SLAVE_ATTACHED)
1803 prev_status = slave->status;
1804 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1806 if (prev_status == SDW_SLAVE_ALERT)
1809 attached_initializing = true;
1811 ret = sdw_initialize_slave(slave);
1813 dev_err(&slave->dev,
1814 "Slave %d initialization failed: %d\n",
1820 dev_err(&slave->dev, "Invalid slave %d status:%d\n",
1825 ret = sdw_update_slave_status(slave, status[i]);
1827 dev_err(&slave->dev,
1828 "Update Slave status failed:%d\n", ret);
1829 if (attached_initializing) {
1830 dev_dbg(&slave->dev,
1831 "%s: signaling initialization completion for Slave %d\n",
1832 __func__, slave->dev_num);
1834 complete(&slave->initialization_complete);
1840 EXPORT_SYMBOL(sdw_handle_slave_status);
1842 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1844 struct sdw_slave *slave;
1847 /* Check all non-zero devices */
1848 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1849 mutex_lock(&bus->bus_lock);
1850 if (test_bit(i, bus->assigned) == false) {
1851 mutex_unlock(&bus->bus_lock);
1854 mutex_unlock(&bus->bus_lock);
1856 slave = sdw_get_slave(bus, i);
1860 if (slave->status != SDW_SLAVE_UNATTACHED) {
1861 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1862 slave->first_interrupt_done = false;
1863 sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
1866 /* keep track of request, used in pm_runtime resume */
1867 slave->unattach_request = request;
1870 EXPORT_SYMBOL(sdw_clear_slave_status);