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>
10 #include <linux/soundwire/sdw_type.h>
12 #include "sysfs_local.h"
14 static DEFINE_IDA(sdw_ida);
16 static int sdw_get_id(struct sdw_bus *bus)
18 int rc = ida_alloc(&sdw_ida, GFP_KERNEL);
28 * sdw_bus_master_add() - add a bus Master instance
30 * @parent: parent device
31 * @fwnode: firmware node handle
33 * Initializes the bus instance, read properties and create child
36 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
37 struct fwnode_handle *fwnode)
39 struct sdw_master_prop *prop = NULL;
43 pr_err("SoundWire parent device is not set\n");
47 ret = sdw_get_id(bus);
49 dev_err(parent, "Failed to get bus id\n");
53 ret = sdw_master_device_add(bus, parent, fwnode);
55 dev_err(parent, "Failed to add master device at link %d\n",
61 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
65 if (!bus->compute_params) {
67 "Bandwidth allocation not configured, compute_params no set\n");
71 mutex_init(&bus->msg_lock);
72 mutex_init(&bus->bus_lock);
73 INIT_LIST_HEAD(&bus->slaves);
74 INIT_LIST_HEAD(&bus->m_rt_list);
77 * Initialize multi_link flag
78 * TODO: populate this flag by reading property from FW node
80 bus->multi_link = false;
81 if (bus->ops->read_prop) {
82 ret = bus->ops->read_prop(bus);
85 "Bus read properties failed:%d\n", ret);
90 sdw_bus_debugfs_init(bus);
93 * Device numbers in SoundWire are 0 through 15. Enumeration device
94 * number (0), Broadcast device number (15), Group numbers (12 and
95 * 13) and Master device number (14) are not used for assignment so
96 * mask these and other higher bits.
99 /* Set higher order bits */
100 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
102 /* Set enumuration device number and broadcast device number */
103 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
104 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
106 /* Set group device numbers and master device number */
107 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
108 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
109 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
112 * SDW is an enumerable bus, but devices can be powered off. So,
113 * they won't be able to report as present.
115 * Create Slave devices based on Slaves described in
116 * the respective firmware (ACPI/DT)
118 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
119 ret = sdw_acpi_find_slaves(bus);
120 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
121 ret = sdw_of_find_slaves(bus);
123 ret = -ENOTSUPP; /* No ACPI/DT so error out */
126 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
131 * Initialize clock values based on Master properties. The max
132 * frequency is read from max_clk_freq property. Current assumption
133 * is that the bus will start at highest clock frequency when
136 * Default active bank will be 0 as out of reset the Slaves have
137 * to start with bank 0 (Table 40 of Spec)
140 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
141 bus->params.curr_dr_freq = bus->params.max_dr_freq;
142 bus->params.curr_bank = SDW_BANK0;
143 bus->params.next_bank = SDW_BANK1;
147 EXPORT_SYMBOL(sdw_bus_master_add);
149 static int sdw_delete_slave(struct device *dev, void *data)
151 struct sdw_slave *slave = dev_to_sdw_dev(dev);
152 struct sdw_bus *bus = slave->bus;
154 pm_runtime_disable(dev);
156 sdw_slave_debugfs_exit(slave);
158 mutex_lock(&bus->bus_lock);
160 if (slave->dev_num) /* clear dev_num if assigned */
161 clear_bit(slave->dev_num, bus->assigned);
163 list_del_init(&slave->node);
164 mutex_unlock(&bus->bus_lock);
166 device_unregister(dev);
171 * sdw_bus_master_delete() - delete the bus master instance
172 * @bus: bus to be deleted
174 * Remove the instance, delete the child devices.
176 void sdw_bus_master_delete(struct sdw_bus *bus)
178 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
179 sdw_master_device_del(bus);
181 sdw_bus_debugfs_exit(bus);
182 ida_free(&sdw_ida, bus->id);
184 EXPORT_SYMBOL(sdw_bus_master_delete);
190 static inline int find_response_code(enum sdw_command_response resp)
196 case SDW_CMD_IGNORED:
199 case SDW_CMD_TIMEOUT:
207 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
209 int retry = bus->prop.err_threshold;
210 enum sdw_command_response resp;
213 for (i = 0; i <= retry; i++) {
214 resp = bus->ops->xfer_msg(bus, msg);
215 ret = find_response_code(resp);
217 /* if cmd is ok or ignored return */
218 if (ret == 0 || ret == -ENODATA)
225 static inline int do_transfer_defer(struct sdw_bus *bus,
227 struct sdw_defer *defer)
229 int retry = bus->prop.err_threshold;
230 enum sdw_command_response resp;
234 defer->length = msg->len;
235 init_completion(&defer->complete);
237 for (i = 0; i <= retry; i++) {
238 resp = bus->ops->xfer_msg_defer(bus, msg, defer);
239 ret = find_response_code(resp);
240 /* if cmd is ok or ignored return */
241 if (ret == 0 || ret == -ENODATA)
248 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
250 int retry = bus->prop.err_threshold;
251 enum sdw_command_response resp;
254 for (i = 0; i <= retry; i++) {
255 resp = bus->ops->reset_page_addr(bus, dev_num);
256 ret = find_response_code(resp);
257 /* if cmd is ok or ignored return */
258 if (ret == 0 || ret == -ENODATA)
265 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
269 ret = do_transfer(bus, msg);
270 if (ret != 0 && ret != -ENODATA)
271 dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
273 (msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
274 msg->addr, msg->len);
277 sdw_reset_page(bus, msg->dev_num);
283 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
285 * @msg: SDW message to be xfered
287 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
291 mutex_lock(&bus->msg_lock);
293 ret = sdw_transfer_unlocked(bus, msg);
295 mutex_unlock(&bus->msg_lock);
301 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
303 * @msg: SDW message to be xfered
304 * @defer: Defer block for signal completion
306 * Caller needs to hold the msg_lock lock while calling this
308 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
309 struct sdw_defer *defer)
313 if (!bus->ops->xfer_msg_defer)
316 ret = do_transfer_defer(bus, msg, defer);
317 if (ret != 0 && ret != -ENODATA)
318 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
322 sdw_reset_page(bus, msg->dev_num);
327 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
328 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
330 memset(msg, 0, sizeof(*msg));
331 msg->addr = addr; /* addr is 16 bit and truncated here */
333 msg->dev_num = dev_num;
337 if (addr < SDW_REG_NO_PAGE) /* no paging area */
340 if (addr >= SDW_REG_MAX) { /* illegal addr */
341 pr_err("SDW: Invalid address %x passed\n", addr);
345 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
346 if (slave && !slave->prop.paging_support)
348 /* no need for else as that will fall-through to paging */
351 /* paging mandatory */
352 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
353 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
358 pr_err("SDW: No slave for paging addr\n");
362 if (!slave->prop.paging_support) {
364 "address %x needs paging but no support\n", addr);
368 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
369 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
370 msg->addr |= BIT(15);
377 * Read/Write IO functions.
378 * no_pm versions can only be called by the bus, e.g. while enumerating or
379 * handling suspend-resume sequences.
380 * all clients need to use the pm versions
384 sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
389 ret = sdw_fill_msg(&msg, slave, addr, count,
390 slave->dev_num, SDW_MSG_FLAG_READ, val);
394 ret = sdw_transfer(slave->bus, &msg);
395 if (slave->is_mockup_device)
401 sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
406 ret = sdw_fill_msg(&msg, slave, addr, count,
407 slave->dev_num, SDW_MSG_FLAG_WRITE, (u8 *)val);
411 ret = sdw_transfer(slave->bus, &msg);
412 if (slave->is_mockup_device)
417 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
419 return sdw_nwrite_no_pm(slave, addr, 1, &value);
421 EXPORT_SYMBOL(sdw_write_no_pm);
424 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
430 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
431 SDW_MSG_FLAG_READ, &buf);
435 ret = sdw_transfer(bus, &msg);
443 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
448 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
449 SDW_MSG_FLAG_WRITE, &value);
453 return sdw_transfer(bus, &msg);
456 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
462 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
463 SDW_MSG_FLAG_READ, &buf);
467 ret = sdw_transfer_unlocked(bus, &msg);
473 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
475 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
480 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
481 SDW_MSG_FLAG_WRITE, &value);
485 return sdw_transfer_unlocked(bus, &msg);
487 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
489 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
494 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
500 EXPORT_SYMBOL(sdw_read_no_pm);
502 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
506 tmp = sdw_read_no_pm(slave, addr);
510 tmp = (tmp & ~mask) | val;
511 return sdw_write_no_pm(slave, addr, tmp);
513 EXPORT_SYMBOL(sdw_update_no_pm);
515 /* Read-Modify-Write Slave register */
516 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
520 tmp = sdw_read(slave, addr);
524 tmp = (tmp & ~mask) | val;
525 return sdw_write(slave, addr, tmp);
527 EXPORT_SYMBOL(sdw_update);
530 * sdw_nread() - Read "n" contiguous SDW Slave registers
532 * @addr: Register address
534 * @val: Buffer for values to be read
536 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
540 ret = pm_runtime_get_sync(&slave->dev);
541 if (ret < 0 && ret != -EACCES) {
542 pm_runtime_put_noidle(&slave->dev);
546 ret = sdw_nread_no_pm(slave, addr, count, val);
548 pm_runtime_mark_last_busy(&slave->dev);
549 pm_runtime_put(&slave->dev);
553 EXPORT_SYMBOL(sdw_nread);
556 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
558 * @addr: Register address
560 * @val: Buffer for values to be written
562 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
566 ret = pm_runtime_get_sync(&slave->dev);
567 if (ret < 0 && ret != -EACCES) {
568 pm_runtime_put_noidle(&slave->dev);
572 ret = sdw_nwrite_no_pm(slave, addr, count, val);
574 pm_runtime_mark_last_busy(&slave->dev);
575 pm_runtime_put(&slave->dev);
579 EXPORT_SYMBOL(sdw_nwrite);
582 * sdw_read() - Read a SDW Slave register
584 * @addr: Register address
586 int sdw_read(struct sdw_slave *slave, u32 addr)
591 ret = sdw_nread(slave, addr, 1, &buf);
597 EXPORT_SYMBOL(sdw_read);
600 * sdw_write() - Write a SDW Slave register
602 * @addr: Register address
603 * @value: Register value
605 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
607 return sdw_nwrite(slave, addr, 1, &value);
609 EXPORT_SYMBOL(sdw_write);
615 /* called with bus_lock held */
616 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
618 struct sdw_slave *slave;
620 list_for_each_entry(slave, &bus->slaves, node) {
621 if (slave->dev_num == i)
628 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
630 if (slave->id.mfg_id != id.mfg_id ||
631 slave->id.part_id != id.part_id ||
632 slave->id.class_id != id.class_id ||
633 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
634 slave->id.unique_id != id.unique_id))
639 EXPORT_SYMBOL(sdw_compare_devid);
641 /* called with bus_lock held */
642 static int sdw_get_device_num(struct sdw_slave *slave)
646 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
647 if (bit == SDW_MAX_DEVICES) {
653 * Do not update dev_num in Slave data structure here,
654 * Update once program dev_num is successful
656 set_bit(bit, slave->bus->assigned);
662 static int sdw_assign_device_num(struct sdw_slave *slave)
664 struct sdw_bus *bus = slave->bus;
666 bool new_device = false;
668 /* check first if device number is assigned, if so reuse that */
669 if (!slave->dev_num) {
670 if (!slave->dev_num_sticky) {
671 mutex_lock(&slave->bus->bus_lock);
672 dev_num = sdw_get_device_num(slave);
673 mutex_unlock(&slave->bus->bus_lock);
675 dev_err(bus->dev, "Get dev_num failed: %d\n",
679 slave->dev_num = dev_num;
680 slave->dev_num_sticky = dev_num;
683 slave->dev_num = slave->dev_num_sticky;
689 "Slave already registered, reusing dev_num:%d\n",
692 /* Clear the slave->dev_num to transfer message on device 0 */
693 dev_num = slave->dev_num;
696 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
698 dev_err(bus->dev, "Program device_num %d failed: %d\n",
703 /* After xfer of msg, restore dev_num */
704 slave->dev_num = slave->dev_num_sticky;
709 void sdw_extract_slave_id(struct sdw_bus *bus,
710 u64 addr, struct sdw_slave_id *id)
712 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
714 id->sdw_version = SDW_VERSION(addr);
715 id->unique_id = SDW_UNIQUE_ID(addr);
716 id->mfg_id = SDW_MFG_ID(addr);
717 id->part_id = SDW_PART_ID(addr);
718 id->class_id = SDW_CLASS_ID(addr);
721 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
722 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
724 EXPORT_SYMBOL(sdw_extract_slave_id);
726 static int sdw_program_device_num(struct sdw_bus *bus)
728 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
729 struct sdw_slave *slave, *_s;
730 struct sdw_slave_id id;
736 /* No Slave, so use raw xfer api */
737 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
738 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
743 ret = sdw_transfer(bus, &msg);
744 if (ret == -ENODATA) { /* end of device id reads */
745 dev_dbg(bus->dev, "No more devices to enumerate\n");
750 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
755 * Construct the addr and extract. Cast the higher shift
756 * bits to avoid truncation due to size limit.
758 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
759 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
762 sdw_extract_slave_id(bus, addr, &id);
765 /* Now compare with entries */
766 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
767 if (sdw_compare_devid(slave, id) == 0) {
771 * Assign a new dev_num to this Slave and
772 * not mark it present. It will be marked
773 * present after it reports ATTACHED on new
776 ret = sdw_assign_device_num(slave);
779 "Assign dev_num failed:%d\n",
789 /* TODO: Park this device in Group 13 */
792 * add Slave device even if there is no platform
793 * firmware description. There will be no driver probe
794 * but the user/integration will be able to see the
795 * device, enumeration status and device number in sysfs
797 sdw_slave_add(bus, &id, NULL);
799 dev_err(bus->dev, "Slave Entry not found\n");
805 * Check till error out or retry (count) exhausts.
806 * Device can drop off and rejoin during enumeration
807 * so count till twice the bound.
810 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
815 static void sdw_modify_slave_status(struct sdw_slave *slave,
816 enum sdw_slave_status status)
818 struct sdw_bus *bus = slave->bus;
820 mutex_lock(&bus->bus_lock);
823 "%s: changing status slave %d status %d new status %d\n",
824 __func__, slave->dev_num, slave->status, status);
826 if (status == SDW_SLAVE_UNATTACHED) {
828 "%s: initializing enumeration and init completion for Slave %d\n",
829 __func__, slave->dev_num);
831 init_completion(&slave->enumeration_complete);
832 init_completion(&slave->initialization_complete);
834 } else if ((status == SDW_SLAVE_ATTACHED) &&
835 (slave->status == SDW_SLAVE_UNATTACHED)) {
837 "%s: signaling enumeration completion for Slave %d\n",
838 __func__, slave->dev_num);
840 complete(&slave->enumeration_complete);
842 slave->status = status;
843 mutex_unlock(&bus->bus_lock);
846 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
847 enum sdw_clk_stop_mode mode,
848 enum sdw_clk_stop_type type)
852 mutex_lock(&slave->sdw_dev_lock);
855 struct device *dev = &slave->dev;
856 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
858 if (drv->ops && drv->ops->clk_stop)
859 ret = drv->ops->clk_stop(slave, mode, type);
862 mutex_unlock(&slave->sdw_dev_lock);
867 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
868 enum sdw_clk_stop_mode mode,
875 wake_en = slave->prop.wake_capable;
878 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
880 if (mode == SDW_CLK_STOP_MODE1)
881 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
884 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
886 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
889 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
893 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
896 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
898 if (ret < 0 && ret != -ENODATA)
899 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
904 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
906 int retry = bus->clk_stop_timeout;
910 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
913 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
916 val &= SDW_SCP_STAT_CLK_STP_NF;
918 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
923 usleep_range(1000, 1500);
927 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
934 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
936 * @bus: SDW bus instance
938 * Query Slave for clock stop mode and prepare for that mode.
940 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
942 bool simple_clk_stop = true;
943 struct sdw_slave *slave;
944 bool is_slave = false;
948 * In order to save on transition time, prepare
949 * each Slave and then wait for all Slave(s) to be
950 * prepared for clock stop.
951 * If one of the Slave devices has lost sync and
952 * replies with Command Ignored/-ENODATA, we continue
955 list_for_each_entry(slave, &bus->slaves, node) {
959 if (slave->status != SDW_SLAVE_ATTACHED &&
960 slave->status != SDW_SLAVE_ALERT)
963 /* Identify if Slave(s) are available on Bus */
966 ret = sdw_slave_clk_stop_callback(slave,
968 SDW_CLK_PRE_PREPARE);
969 if (ret < 0 && ret != -ENODATA) {
970 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
974 /* Only prepare a Slave device if needed */
975 if (!slave->prop.simple_clk_stop_capable) {
976 simple_clk_stop = false;
978 ret = sdw_slave_clk_stop_prepare(slave,
981 if (ret < 0 && ret != -ENODATA) {
982 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
988 /* Skip remaining clock stop preparation if no Slave is attached */
993 * Don't wait for all Slaves to be ready if they follow the simple
996 if (!simple_clk_stop) {
997 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
998 SDW_BROADCAST_DEV_NUM);
1000 * if there are no Slave devices present and the reply is
1001 * Command_Ignored/-ENODATA, we don't need to continue with the
1002 * flow and can just return here. The error code is not modified
1003 * and its handling left as an exercise for the caller.
1009 /* Inform slaves that prep is done */
1010 list_for_each_entry(slave, &bus->slaves, node) {
1011 if (!slave->dev_num)
1014 if (slave->status != SDW_SLAVE_ATTACHED &&
1015 slave->status != SDW_SLAVE_ALERT)
1018 ret = sdw_slave_clk_stop_callback(slave,
1020 SDW_CLK_POST_PREPARE);
1022 if (ret < 0 && ret != -ENODATA) {
1023 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1030 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1033 * sdw_bus_clk_stop: stop bus clock
1035 * @bus: SDW bus instance
1037 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1038 * write to SCP_CTRL register.
1040 int sdw_bus_clk_stop(struct sdw_bus *bus)
1045 * broadcast clock stop now, attached Slaves will ACK this,
1046 * unattached will ignore
1048 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1049 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1051 if (ret != -ENODATA)
1052 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
1058 EXPORT_SYMBOL(sdw_bus_clk_stop);
1061 * sdw_bus_exit_clk_stop: Exit clock stop mode
1063 * @bus: SDW bus instance
1065 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1066 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1069 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1071 bool simple_clk_stop = true;
1072 struct sdw_slave *slave;
1073 bool is_slave = false;
1077 * In order to save on transition time, de-prepare
1078 * each Slave and then wait for all Slave(s) to be
1079 * de-prepared after clock resume.
1081 list_for_each_entry(slave, &bus->slaves, node) {
1082 if (!slave->dev_num)
1085 if (slave->status != SDW_SLAVE_ATTACHED &&
1086 slave->status != SDW_SLAVE_ALERT)
1089 /* Identify if Slave(s) are available on Bus */
1092 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1093 SDW_CLK_PRE_DEPREPARE);
1095 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
1097 /* Only de-prepare a Slave device if needed */
1098 if (!slave->prop.simple_clk_stop_capable) {
1099 simple_clk_stop = false;
1101 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1105 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
1109 /* Skip remaining clock stop de-preparation if no Slave is attached */
1114 * Don't wait for all Slaves to be ready if they follow the simple
1117 if (!simple_clk_stop) {
1118 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1120 dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
1123 list_for_each_entry(slave, &bus->slaves, node) {
1124 if (!slave->dev_num)
1127 if (slave->status != SDW_SLAVE_ATTACHED &&
1128 slave->status != SDW_SLAVE_ALERT)
1131 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1132 SDW_CLK_POST_DEPREPARE);
1134 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
1139 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1141 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1142 int port, bool enable, int mask)
1148 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1149 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1150 enable ? "on" : "off");
1151 mask |= SDW_DPN_INT_TEST_FAIL;
1154 addr = SDW_DPN_INTMASK(port);
1156 /* Set/Clear port ready interrupt mask */
1159 val |= SDW_DPN_INT_PORT_READY;
1162 val &= ~SDW_DPN_INT_PORT_READY;
1165 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1167 dev_err(&slave->dev,
1168 "SDW_DPN_INTMASK write failed:%d\n", val);
1173 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1175 u32 mclk_freq = slave->bus->prop.mclk_freq;
1176 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1183 * frequency base and scale registers are required for SDCA
1184 * devices. They may also be used for 1.2+/non-SDCA devices,
1185 * but we will need a DisCo property to cover this case
1187 if (!slave->id.class_id)
1191 dev_err(&slave->dev,
1192 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1197 * map base frequency using Table 89 of SoundWire 1.2 spec.
1198 * The order of the tests just follows the specification, this
1199 * is not a selection between possible values or a search for
1200 * the best value but just a mapping. Only one case per platform
1202 * Some BIOS have inconsistent values for mclk_freq but a
1203 * correct root so we force the mclk_freq to avoid variations.
1205 if (!(19200000 % mclk_freq)) {
1206 mclk_freq = 19200000;
1207 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1208 } else if (!(24000000 % mclk_freq)) {
1209 mclk_freq = 24000000;
1210 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1211 } else if (!(24576000 % mclk_freq)) {
1212 mclk_freq = 24576000;
1213 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1214 } else if (!(22579200 % mclk_freq)) {
1215 mclk_freq = 22579200;
1216 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1217 } else if (!(32000000 % mclk_freq)) {
1218 mclk_freq = 32000000;
1219 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1221 dev_err(&slave->dev,
1222 "Unsupported clock base, mclk %d\n",
1227 if (mclk_freq % curr_freq) {
1228 dev_err(&slave->dev,
1229 "mclk %d is not multiple of bus curr_freq %d\n",
1230 mclk_freq, curr_freq);
1234 scale = mclk_freq / curr_freq;
1237 * map scale to Table 90 of SoundWire 1.2 spec - and check
1238 * that the scale is a power of two and maximum 64
1240 scale_index = ilog2(scale);
1242 if (BIT(scale_index) != scale || scale_index > 6) {
1243 dev_err(&slave->dev,
1244 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1245 scale, mclk_freq, curr_freq);
1250 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1252 dev_err(&slave->dev,
1253 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1257 /* initialize scale for both banks */
1258 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1260 dev_err(&slave->dev,
1261 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1264 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1266 dev_err(&slave->dev,
1267 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1269 dev_dbg(&slave->dev,
1270 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1271 base, scale_index, mclk_freq, curr_freq);
1276 static int sdw_initialize_slave(struct sdw_slave *slave)
1278 struct sdw_slave_prop *prop = &slave->prop;
1283 ret = sdw_slave_set_frequency(slave);
1287 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1288 /* Clear bus clash interrupt before enabling interrupt mask */
1289 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1291 dev_err(&slave->dev,
1292 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1295 if (status & SDW_SCP_INT1_BUS_CLASH) {
1296 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1297 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1299 dev_err(&slave->dev,
1300 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1305 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1306 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1307 /* Clear parity interrupt before enabling interrupt mask */
1308 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1310 dev_err(&slave->dev,
1311 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1314 if (status & SDW_SCP_INT1_PARITY) {
1315 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1316 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1318 dev_err(&slave->dev,
1319 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1326 * Set SCP_INT1_MASK register, typically bus clash and
1327 * implementation-defined interrupt mask. The Parity detection
1328 * may not always be correct on startup so its use is
1329 * device-dependent, it might e.g. only be enabled in
1330 * steady-state after a couple of frames.
1332 val = slave->prop.scp_int1_mask;
1334 /* Enable SCP interrupts */
1335 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1337 dev_err(&slave->dev,
1338 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
1342 /* No need to continue if DP0 is not present */
1343 if (!slave->prop.dp0_prop)
1346 /* Enable DP0 interrupts */
1347 val = prop->dp0_prop->imp_def_interrupts;
1348 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1350 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1352 dev_err(&slave->dev,
1353 "SDW_DP0_INTMASK read failed:%d\n", ret);
1357 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1359 u8 clear, impl_int_mask;
1360 int status, status2, ret, count = 0;
1362 status = sdw_read_no_pm(slave, SDW_DP0_INT);
1364 dev_err(&slave->dev,
1365 "SDW_DP0_INT read failed:%d\n", status);
1370 clear = status & ~SDW_DP0_INTERRUPTS;
1372 if (status & SDW_DP0_INT_TEST_FAIL) {
1373 dev_err(&slave->dev, "Test fail for port 0\n");
1374 clear |= SDW_DP0_INT_TEST_FAIL;
1378 * Assumption: PORT_READY interrupt will be received only for
1379 * ports implementing Channel Prepare state machine (CP_SM)
1382 if (status & SDW_DP0_INT_PORT_READY) {
1383 complete(&slave->port_ready[0]);
1384 clear |= SDW_DP0_INT_PORT_READY;
1387 if (status & SDW_DP0_INT_BRA_FAILURE) {
1388 dev_err(&slave->dev, "BRA failed\n");
1389 clear |= SDW_DP0_INT_BRA_FAILURE;
1392 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1393 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1395 if (status & impl_int_mask) {
1396 clear |= impl_int_mask;
1397 *slave_status = clear;
1400 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
1401 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
1403 dev_err(&slave->dev,
1404 "SDW_DP0_INT write failed:%d\n", ret);
1408 /* Read DP0 interrupt again */
1409 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
1411 dev_err(&slave->dev,
1412 "SDW_DP0_INT read failed:%d\n", status2);
1415 /* filter to limit loop to interrupts identified in the first status read */
1420 /* we can get alerts while processing so keep retrying */
1421 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1423 if (count == SDW_READ_INTR_CLEAR_RETRY)
1424 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
1429 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1430 int port, u8 *slave_status)
1432 u8 clear, impl_int_mask;
1433 int status, status2, ret, count = 0;
1437 return sdw_handle_dp0_interrupt(slave, slave_status);
1439 addr = SDW_DPN_INT(port);
1440 status = sdw_read_no_pm(slave, addr);
1442 dev_err(&slave->dev,
1443 "SDW_DPN_INT read failed:%d\n", status);
1449 clear = status & ~SDW_DPN_INTERRUPTS;
1451 if (status & SDW_DPN_INT_TEST_FAIL) {
1452 dev_err(&slave->dev, "Test fail for port:%d\n", port);
1453 clear |= SDW_DPN_INT_TEST_FAIL;
1457 * Assumption: PORT_READY interrupt will be received only
1458 * for ports implementing CP_SM.
1460 if (status & SDW_DPN_INT_PORT_READY) {
1461 complete(&slave->port_ready[port]);
1462 clear |= SDW_DPN_INT_PORT_READY;
1465 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1466 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1468 if (status & impl_int_mask) {
1469 clear |= impl_int_mask;
1470 *slave_status = clear;
1473 /* clear the interrupt but don't touch reserved fields */
1474 ret = sdw_write_no_pm(slave, addr, clear);
1476 dev_err(&slave->dev,
1477 "SDW_DPN_INT write failed:%d\n", ret);
1481 /* Read DPN interrupt again */
1482 status2 = sdw_read_no_pm(slave, addr);
1484 dev_err(&slave->dev,
1485 "SDW_DPN_INT read failed:%d\n", status2);
1488 /* filter to limit loop to interrupts identified in the first status read */
1493 /* we can get alerts while processing so keep retrying */
1494 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1496 if (count == SDW_READ_INTR_CLEAR_RETRY)
1497 dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
1502 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1504 struct sdw_slave_intr_status slave_intr;
1505 u8 clear = 0, bit, port_status[15] = {0};
1506 int port_num, stat, ret, count = 0;
1509 u8 sdca_cascade = 0;
1510 u8 buf, buf2[2], _buf, _buf2[2];
1514 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1516 ret = pm_runtime_get_sync(&slave->dev);
1517 if (ret < 0 && ret != -EACCES) {
1518 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1519 pm_runtime_put_noidle(&slave->dev);
1523 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1524 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1526 dev_err(&slave->dev,
1527 "SDW_SCP_INT1 read failed:%d\n", ret);
1532 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1534 dev_err(&slave->dev,
1535 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1539 if (slave->prop.is_sdca) {
1540 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1542 dev_err(&slave->dev,
1543 "SDW_DP0_INT read failed:%d\n", ret);
1546 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1550 slave_notify = false;
1553 * Check parity, bus clash and Slave (impl defined)
1556 if (buf & SDW_SCP_INT1_PARITY) {
1557 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1558 parity_quirk = !slave->first_interrupt_done &&
1559 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1561 if (parity_check && !parity_quirk)
1562 dev_err(&slave->dev, "Parity error detected\n");
1563 clear |= SDW_SCP_INT1_PARITY;
1566 if (buf & SDW_SCP_INT1_BUS_CLASH) {
1567 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1568 dev_err(&slave->dev, "Bus clash detected\n");
1569 clear |= SDW_SCP_INT1_BUS_CLASH;
1573 * When bus clash or parity errors are detected, such errors
1574 * are unlikely to be recoverable errors.
1575 * TODO: In such scenario, reset bus. Make this configurable
1576 * via sysfs property with bus reset being the default.
1579 if (buf & SDW_SCP_INT1_IMPL_DEF) {
1580 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1581 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1582 slave_notify = true;
1584 clear |= SDW_SCP_INT1_IMPL_DEF;
1587 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1589 slave_notify = true;
1591 /* Check port 0 - 3 interrupts */
1592 port = buf & SDW_SCP_INT1_PORT0_3;
1594 /* To get port number corresponding to bits, shift it */
1595 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1596 for_each_set_bit(bit, &port, 8) {
1597 sdw_handle_port_interrupt(slave, bit,
1601 /* Check if cascade 2 interrupt is present */
1602 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1603 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1604 for_each_set_bit(bit, &port, 8) {
1605 /* scp2 ports start from 4 */
1607 sdw_handle_port_interrupt(slave,
1609 &port_status[port_num]);
1613 /* now check last cascade */
1614 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1615 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1616 for_each_set_bit(bit, &port, 8) {
1617 /* scp3 ports start from 11 */
1618 port_num = bit + 10;
1619 sdw_handle_port_interrupt(slave,
1621 &port_status[port_num]);
1625 /* Update the Slave driver */
1627 mutex_lock(&slave->sdw_dev_lock);
1629 if (slave->probed) {
1630 struct device *dev = &slave->dev;
1631 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1633 if (drv->ops && drv->ops->interrupt_callback) {
1634 slave_intr.sdca_cascade = sdca_cascade;
1635 slave_intr.control_port = clear;
1636 memcpy(slave_intr.port, &port_status,
1637 sizeof(slave_intr.port));
1639 drv->ops->interrupt_callback(slave, &slave_intr);
1643 mutex_unlock(&slave->sdw_dev_lock);
1647 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
1649 dev_err(&slave->dev,
1650 "SDW_SCP_INT1 write failed:%d\n", ret);
1654 /* at this point all initial interrupt sources were handled */
1655 slave->first_interrupt_done = true;
1658 * Read status again to ensure no new interrupts arrived
1659 * while servicing interrupts.
1661 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1663 dev_err(&slave->dev,
1664 "SDW_SCP_INT1 recheck read failed:%d\n", ret);
1669 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2);
1671 dev_err(&slave->dev,
1672 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
1676 if (slave->prop.is_sdca) {
1677 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1679 dev_err(&slave->dev,
1680 "SDW_DP0_INT recheck read failed:%d\n", ret);
1683 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1687 * Make sure no interrupts are pending, but filter to limit loop
1688 * to interrupts identified in the first status read
1691 buf2[0] &= _buf2[0];
1692 buf2[1] &= _buf2[1];
1693 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
1696 * Exit loop if Slave is continuously in ALERT state even
1697 * after servicing the interrupt multiple times.
1701 /* we can get alerts while processing so keep retrying */
1702 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1704 if (count == SDW_READ_INTR_CLEAR_RETRY)
1705 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
1708 pm_runtime_mark_last_busy(&slave->dev);
1709 pm_runtime_put_autosuspend(&slave->dev);
1714 static int sdw_update_slave_status(struct sdw_slave *slave,
1715 enum sdw_slave_status status)
1719 mutex_lock(&slave->sdw_dev_lock);
1721 if (slave->probed) {
1722 struct device *dev = &slave->dev;
1723 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1725 if (drv->ops && drv->ops->update_status)
1726 ret = drv->ops->update_status(slave, status);
1729 mutex_unlock(&slave->sdw_dev_lock);
1735 * sdw_handle_slave_status() - Handle Slave status
1736 * @bus: SDW bus instance
1737 * @status: Status for all Slave(s)
1739 int sdw_handle_slave_status(struct sdw_bus *bus,
1740 enum sdw_slave_status status[])
1742 enum sdw_slave_status prev_status;
1743 struct sdw_slave *slave;
1744 bool attached_initializing;
1747 /* first check if any Slaves fell off the bus */
1748 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1749 mutex_lock(&bus->bus_lock);
1750 if (test_bit(i, bus->assigned) == false) {
1751 mutex_unlock(&bus->bus_lock);
1754 mutex_unlock(&bus->bus_lock);
1756 slave = sdw_get_slave(bus, i);
1760 if (status[i] == SDW_SLAVE_UNATTACHED &&
1761 slave->status != SDW_SLAVE_UNATTACHED)
1762 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1765 if (status[0] == SDW_SLAVE_ATTACHED) {
1766 dev_dbg(bus->dev, "Slave attached, programming device number\n");
1767 ret = sdw_program_device_num(bus);
1769 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
1771 * programming a device number will have side effects,
1772 * so we deal with other devices at a later time
1777 /* Continue to check other slave statuses */
1778 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1779 mutex_lock(&bus->bus_lock);
1780 if (test_bit(i, bus->assigned) == false) {
1781 mutex_unlock(&bus->bus_lock);
1784 mutex_unlock(&bus->bus_lock);
1786 slave = sdw_get_slave(bus, i);
1790 attached_initializing = false;
1792 switch (status[i]) {
1793 case SDW_SLAVE_UNATTACHED:
1794 if (slave->status == SDW_SLAVE_UNATTACHED)
1797 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1800 case SDW_SLAVE_ALERT:
1801 ret = sdw_handle_slave_alerts(slave);
1803 dev_err(&slave->dev,
1804 "Slave %d alert handling failed: %d\n",
1808 case SDW_SLAVE_ATTACHED:
1809 if (slave->status == SDW_SLAVE_ATTACHED)
1812 prev_status = slave->status;
1813 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1815 if (prev_status == SDW_SLAVE_ALERT)
1818 attached_initializing = true;
1820 ret = sdw_initialize_slave(slave);
1822 dev_err(&slave->dev,
1823 "Slave %d initialization failed: %d\n",
1829 dev_err(&slave->dev, "Invalid slave %d status:%d\n",
1834 ret = sdw_update_slave_status(slave, status[i]);
1836 dev_err(&slave->dev,
1837 "Update Slave status failed:%d\n", ret);
1838 if (attached_initializing) {
1839 dev_dbg(&slave->dev,
1840 "%s: signaling initialization completion for Slave %d\n",
1841 __func__, slave->dev_num);
1843 complete(&slave->initialization_complete);
1849 EXPORT_SYMBOL(sdw_handle_slave_status);
1851 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1853 struct sdw_slave *slave;
1856 /* Check all non-zero devices */
1857 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1858 mutex_lock(&bus->bus_lock);
1859 if (test_bit(i, bus->assigned) == false) {
1860 mutex_unlock(&bus->bus_lock);
1863 mutex_unlock(&bus->bus_lock);
1865 slave = sdw_get_slave(bus, i);
1869 if (slave->status != SDW_SLAVE_UNATTACHED) {
1870 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1871 slave->first_interrupt_done = false;
1872 sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
1875 /* keep track of request, used in pm_runtime resume */
1876 slave->unattach_request = request;
1879 EXPORT_SYMBOL(sdw_clear_slave_status);