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>
13 #include "sysfs_local.h"
15 static DEFINE_IDA(sdw_bus_ida);
17 static int sdw_get_id(struct sdw_bus *bus)
19 int rc = ida_alloc(&sdw_bus_ida, GFP_KERNEL);
26 if (bus->controller_id == -1)
27 bus->controller_id = rc;
33 * sdw_bus_master_add() - add a bus Master instance
35 * @parent: parent device
36 * @fwnode: firmware node handle
38 * Initializes the bus instance, read properties and create child
41 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
42 struct fwnode_handle *fwnode)
44 struct sdw_master_prop *prop = NULL;
48 pr_err("SoundWire parent device is not set\n");
52 ret = sdw_get_id(bus);
54 dev_err(parent, "Failed to get bus id\n");
58 ret = sdw_master_device_add(bus, parent, fwnode);
60 dev_err(parent, "Failed to add master device at link %d\n",
66 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
70 if (!bus->compute_params) {
72 "Bandwidth allocation not configured, compute_params no set\n");
77 * Give each bus_lock and msg_lock a unique key so that lockdep won't
78 * trigger a deadlock warning when the locks of several buses are
79 * grabbed during configuration of a multi-bus stream.
81 lockdep_register_key(&bus->msg_lock_key);
82 __mutex_init(&bus->msg_lock, "msg_lock", &bus->msg_lock_key);
84 lockdep_register_key(&bus->bus_lock_key);
85 __mutex_init(&bus->bus_lock, "bus_lock", &bus->bus_lock_key);
87 INIT_LIST_HEAD(&bus->slaves);
88 INIT_LIST_HEAD(&bus->m_rt_list);
91 * Initialize multi_link flag
93 bus->multi_link = false;
94 if (bus->ops->read_prop) {
95 ret = bus->ops->read_prop(bus);
98 "Bus read properties failed:%d\n", ret);
103 sdw_bus_debugfs_init(bus);
106 * Device numbers in SoundWire are 0 through 15. Enumeration device
107 * number (0), Broadcast device number (15), Group numbers (12 and
108 * 13) and Master device number (14) are not used for assignment so
109 * mask these and other higher bits.
112 /* Set higher order bits */
113 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
115 /* Set enumuration device number and broadcast device number */
116 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
117 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
119 /* Set group device numbers and master device number */
120 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
121 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
122 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
125 * SDW is an enumerable bus, but devices can be powered off. So,
126 * they won't be able to report as present.
128 * Create Slave devices based on Slaves described in
129 * the respective firmware (ACPI/DT)
131 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
132 ret = sdw_acpi_find_slaves(bus);
133 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
134 ret = sdw_of_find_slaves(bus);
136 ret = -ENOTSUPP; /* No ACPI/DT so error out */
139 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
144 * Initialize clock values based on Master properties. The max
145 * frequency is read from max_clk_freq property. Current assumption
146 * is that the bus will start at highest clock frequency when
149 * Default active bank will be 0 as out of reset the Slaves have
150 * to start with bank 0 (Table 40 of Spec)
153 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
154 bus->params.curr_dr_freq = bus->params.max_dr_freq;
155 bus->params.curr_bank = SDW_BANK0;
156 bus->params.next_bank = SDW_BANK1;
158 ret = sdw_irq_create(bus, fwnode);
164 EXPORT_SYMBOL(sdw_bus_master_add);
166 static int sdw_delete_slave(struct device *dev, void *data)
168 struct sdw_slave *slave = dev_to_sdw_dev(dev);
169 struct sdw_bus *bus = slave->bus;
171 pm_runtime_disable(dev);
173 sdw_slave_debugfs_exit(slave);
175 mutex_lock(&bus->bus_lock);
177 if (slave->dev_num) { /* clear dev_num if assigned */
178 clear_bit(slave->dev_num, bus->assigned);
179 if (bus->ops && bus->ops->put_device_num)
180 bus->ops->put_device_num(bus, slave);
182 list_del_init(&slave->node);
183 mutex_unlock(&bus->bus_lock);
185 device_unregister(dev);
190 * sdw_bus_master_delete() - delete the bus master instance
191 * @bus: bus to be deleted
193 * Remove the instance, delete the child devices.
195 void sdw_bus_master_delete(struct sdw_bus *bus)
197 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
201 sdw_master_device_del(bus);
203 sdw_bus_debugfs_exit(bus);
204 lockdep_unregister_key(&bus->bus_lock_key);
205 lockdep_unregister_key(&bus->msg_lock_key);
206 ida_free(&sdw_bus_ida, bus->id);
208 EXPORT_SYMBOL(sdw_bus_master_delete);
214 static inline int find_response_code(enum sdw_command_response resp)
220 case SDW_CMD_IGNORED:
223 case SDW_CMD_TIMEOUT:
231 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
233 int retry = bus->prop.err_threshold;
234 enum sdw_command_response resp;
237 for (i = 0; i <= retry; i++) {
238 resp = bus->ops->xfer_msg(bus, msg);
239 ret = find_response_code(resp);
241 /* if cmd is ok or ignored return */
242 if (ret == 0 || ret == -ENODATA)
249 static inline int do_transfer_defer(struct sdw_bus *bus,
252 struct sdw_defer *defer = &bus->defer_msg;
253 int retry = bus->prop.err_threshold;
254 enum sdw_command_response resp;
258 defer->length = msg->len;
259 init_completion(&defer->complete);
261 for (i = 0; i <= retry; i++) {
262 resp = bus->ops->xfer_msg_defer(bus);
263 ret = find_response_code(resp);
264 /* if cmd is ok or ignored return */
265 if (ret == 0 || ret == -ENODATA)
272 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
276 ret = do_transfer(bus, msg);
277 if (ret != 0 && ret != -ENODATA)
278 dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
280 (msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
281 msg->addr, msg->len);
287 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
289 * @msg: SDW message to be xfered
291 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
295 mutex_lock(&bus->msg_lock);
297 ret = sdw_transfer_unlocked(bus, msg);
299 mutex_unlock(&bus->msg_lock);
305 * sdw_show_ping_status() - Direct report of PING status, to be used by Peripheral drivers
307 * @sync_delay: Delay before reading status
309 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay)
313 if (!bus->ops->read_ping_status)
317 * wait for peripheral to sync if desired. 10-15ms should be more than
318 * enough in most cases.
321 usleep_range(10000, 15000);
323 mutex_lock(&bus->msg_lock);
325 status = bus->ops->read_ping_status(bus);
327 mutex_unlock(&bus->msg_lock);
330 dev_warn(bus->dev, "%s: no peripherals attached\n", __func__);
332 dev_dbg(bus->dev, "PING status: %#x\n", status);
334 EXPORT_SYMBOL(sdw_show_ping_status);
337 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
339 * @msg: SDW message to be xfered
341 * Caller needs to hold the msg_lock lock while calling this
343 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg)
347 if (!bus->ops->xfer_msg_defer)
350 ret = do_transfer_defer(bus, msg);
351 if (ret != 0 && ret != -ENODATA)
352 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
358 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
359 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
361 memset(msg, 0, sizeof(*msg));
362 msg->addr = addr; /* addr is 16 bit and truncated here */
364 msg->dev_num = dev_num;
368 if (addr < SDW_REG_NO_PAGE) /* no paging area */
371 if (addr >= SDW_REG_MAX) { /* illegal addr */
372 pr_err("SDW: Invalid address %x passed\n", addr);
376 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
377 if (slave && !slave->prop.paging_support)
379 /* no need for else as that will fall-through to paging */
382 /* paging mandatory */
383 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
384 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
389 pr_err("SDW: No slave for paging addr\n");
393 if (!slave->prop.paging_support) {
395 "address %x needs paging but no support\n", addr);
399 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
400 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
401 msg->addr |= BIT(15);
408 * Read/Write IO functions.
411 static int sdw_ntransfer_no_pm(struct sdw_slave *slave, u32 addr, u8 flags,
412 size_t count, u8 *val)
419 // Only handle bytes up to next page boundary
420 size = min_t(size_t, count, (SDW_REGADDR + 1) - (addr & SDW_REGADDR));
422 ret = sdw_fill_msg(&msg, slave, addr, size, slave->dev_num, flags, val);
426 ret = sdw_transfer(slave->bus, &msg);
427 if (ret < 0 && !slave->is_mockup_device)
439 * sdw_nread_no_pm() - Read "n" contiguous SDW Slave registers with no PM
441 * @addr: Register address
443 * @val: Buffer for values to be read
445 * Note that if the message crosses a page boundary each page will be
446 * transferred under a separate invocation of the msg_lock.
448 int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
450 return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_READ, count, val);
452 EXPORT_SYMBOL(sdw_nread_no_pm);
455 * sdw_nwrite_no_pm() - Write "n" contiguous SDW Slave registers with no PM
457 * @addr: Register address
459 * @val: Buffer for values to be written
461 * Note that if the message crosses a page boundary each page will be
462 * transferred under a separate invocation of the msg_lock.
464 int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
466 return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_WRITE, count, (u8 *)val);
468 EXPORT_SYMBOL(sdw_nwrite_no_pm);
471 * sdw_write_no_pm() - Write a SDW Slave register with no PM
473 * @addr: Register address
474 * @value: Register value
476 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
478 return sdw_nwrite_no_pm(slave, addr, 1, &value);
480 EXPORT_SYMBOL(sdw_write_no_pm);
483 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
489 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
490 SDW_MSG_FLAG_READ, &buf);
494 ret = sdw_transfer(bus, &msg);
502 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
507 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
508 SDW_MSG_FLAG_WRITE, &value);
512 return sdw_transfer(bus, &msg);
515 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
521 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
522 SDW_MSG_FLAG_READ, &buf);
526 ret = sdw_transfer_unlocked(bus, &msg);
532 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
534 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
539 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
540 SDW_MSG_FLAG_WRITE, &value);
544 return sdw_transfer_unlocked(bus, &msg);
546 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
549 * sdw_read_no_pm() - Read a SDW Slave register with no PM
551 * @addr: Register address
553 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
558 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
564 EXPORT_SYMBOL(sdw_read_no_pm);
566 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
570 tmp = sdw_read_no_pm(slave, addr);
574 tmp = (tmp & ~mask) | val;
575 return sdw_write_no_pm(slave, addr, tmp);
577 EXPORT_SYMBOL(sdw_update_no_pm);
579 /* Read-Modify-Write Slave register */
580 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
584 tmp = sdw_read(slave, addr);
588 tmp = (tmp & ~mask) | val;
589 return sdw_write(slave, addr, tmp);
591 EXPORT_SYMBOL(sdw_update);
594 * sdw_nread() - Read "n" contiguous SDW Slave registers
596 * @addr: Register address
598 * @val: Buffer for values to be read
600 * This version of the function will take a PM reference to the slave
602 * Note that if the message crosses a page boundary each page will be
603 * transferred under a separate invocation of the msg_lock.
605 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
609 ret = pm_runtime_get_sync(&slave->dev);
610 if (ret < 0 && ret != -EACCES) {
611 pm_runtime_put_noidle(&slave->dev);
615 ret = sdw_nread_no_pm(slave, addr, count, val);
617 pm_runtime_mark_last_busy(&slave->dev);
618 pm_runtime_put(&slave->dev);
622 EXPORT_SYMBOL(sdw_nread);
625 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
627 * @addr: Register address
629 * @val: Buffer for values to be written
631 * This version of the function will take a PM reference to the slave
633 * Note that if the message crosses a page boundary each page will be
634 * transferred under a separate invocation of the msg_lock.
636 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
640 ret = pm_runtime_get_sync(&slave->dev);
641 if (ret < 0 && ret != -EACCES) {
642 pm_runtime_put_noidle(&slave->dev);
646 ret = sdw_nwrite_no_pm(slave, addr, count, val);
648 pm_runtime_mark_last_busy(&slave->dev);
649 pm_runtime_put(&slave->dev);
653 EXPORT_SYMBOL(sdw_nwrite);
656 * sdw_read() - Read a SDW Slave register
658 * @addr: Register address
660 * This version of the function will take a PM reference to the slave
663 int sdw_read(struct sdw_slave *slave, u32 addr)
668 ret = sdw_nread(slave, addr, 1, &buf);
674 EXPORT_SYMBOL(sdw_read);
677 * sdw_write() - Write a SDW Slave register
679 * @addr: Register address
680 * @value: Register value
682 * This version of the function will take a PM reference to the slave
685 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
687 return sdw_nwrite(slave, addr, 1, &value);
689 EXPORT_SYMBOL(sdw_write);
695 /* called with bus_lock held */
696 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
698 struct sdw_slave *slave;
700 list_for_each_entry(slave, &bus->slaves, node) {
701 if (slave->dev_num == i)
708 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
710 if (slave->id.mfg_id != id.mfg_id ||
711 slave->id.part_id != id.part_id ||
712 slave->id.class_id != id.class_id ||
713 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
714 slave->id.unique_id != id.unique_id))
719 EXPORT_SYMBOL(sdw_compare_devid);
721 /* called with bus_lock held */
722 static int sdw_get_device_num(struct sdw_slave *slave)
724 struct sdw_bus *bus = slave->bus;
727 if (bus->ops && bus->ops->get_device_num) {
728 bit = bus->ops->get_device_num(bus, slave);
732 bit = find_first_zero_bit(bus->assigned, SDW_MAX_DEVICES);
733 if (bit == SDW_MAX_DEVICES) {
740 * Do not update dev_num in Slave data structure here,
741 * Update once program dev_num is successful
743 set_bit(bit, bus->assigned);
749 static int sdw_assign_device_num(struct sdw_slave *slave)
751 struct sdw_bus *bus = slave->bus;
753 bool new_device = false;
755 /* check first if device number is assigned, if so reuse that */
756 if (!slave->dev_num) {
757 if (!slave->dev_num_sticky) {
758 mutex_lock(&slave->bus->bus_lock);
759 dev_num = sdw_get_device_num(slave);
760 mutex_unlock(&slave->bus->bus_lock);
762 dev_err(bus->dev, "Get dev_num failed: %d\n",
766 slave->dev_num = dev_num;
767 slave->dev_num_sticky = dev_num;
770 slave->dev_num = slave->dev_num_sticky;
776 "Slave already registered, reusing dev_num:%d\n",
779 /* Clear the slave->dev_num to transfer message on device 0 */
780 dev_num = slave->dev_num;
783 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
785 dev_err(bus->dev, "Program device_num %d failed: %d\n",
790 /* After xfer of msg, restore dev_num */
791 slave->dev_num = slave->dev_num_sticky;
793 if (bus->ops && bus->ops->new_peripheral_assigned)
794 bus->ops->new_peripheral_assigned(bus, slave, dev_num);
799 void sdw_extract_slave_id(struct sdw_bus *bus,
800 u64 addr, struct sdw_slave_id *id)
802 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
804 id->sdw_version = SDW_VERSION(addr);
805 id->unique_id = SDW_UNIQUE_ID(addr);
806 id->mfg_id = SDW_MFG_ID(addr);
807 id->part_id = SDW_PART_ID(addr);
808 id->class_id = SDW_CLASS_ID(addr);
811 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
812 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
814 EXPORT_SYMBOL(sdw_extract_slave_id);
816 static int sdw_program_device_num(struct sdw_bus *bus, bool *programmed)
818 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
819 struct sdw_slave *slave, *_s;
820 struct sdw_slave_id id;
828 /* No Slave, so use raw xfer api */
829 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
830 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
835 ret = sdw_transfer(bus, &msg);
836 if (ret == -ENODATA) { /* end of device id reads */
837 dev_dbg(bus->dev, "No more devices to enumerate\n");
842 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
847 * Construct the addr and extract. Cast the higher shift
848 * bits to avoid truncation due to size limit.
850 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
851 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
854 sdw_extract_slave_id(bus, addr, &id);
857 /* Now compare with entries */
858 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
859 if (sdw_compare_devid(slave, id) == 0) {
863 * To prevent skipping state-machine stages don't
864 * program a device until we've seen it UNATTACH.
865 * Must return here because no other device on #0
866 * can be detected until this one has been
867 * assigned a device ID.
869 if (slave->status != SDW_SLAVE_UNATTACHED)
873 * Assign a new dev_num to this Slave and
874 * not mark it present. It will be marked
875 * present after it reports ATTACHED on new
878 ret = sdw_assign_device_num(slave);
881 "Assign dev_num failed:%d\n",
893 /* TODO: Park this device in Group 13 */
896 * add Slave device even if there is no platform
897 * firmware description. There will be no driver probe
898 * but the user/integration will be able to see the
899 * device, enumeration status and device number in sysfs
901 sdw_slave_add(bus, &id, NULL);
903 dev_err(bus->dev, "Slave Entry not found\n");
909 * Check till error out or retry (count) exhausts.
910 * Device can drop off and rejoin during enumeration
911 * so count till twice the bound.
914 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
919 static void sdw_modify_slave_status(struct sdw_slave *slave,
920 enum sdw_slave_status status)
922 struct sdw_bus *bus = slave->bus;
924 mutex_lock(&bus->bus_lock);
927 "changing status slave %d status %d new status %d\n",
928 slave->dev_num, slave->status, status);
930 if (status == SDW_SLAVE_UNATTACHED) {
932 "initializing enumeration and init completion for Slave %d\n",
935 reinit_completion(&slave->enumeration_complete);
936 reinit_completion(&slave->initialization_complete);
938 } else if ((status == SDW_SLAVE_ATTACHED) &&
939 (slave->status == SDW_SLAVE_UNATTACHED)) {
941 "signaling enumeration completion for Slave %d\n",
944 complete_all(&slave->enumeration_complete);
946 slave->status = status;
947 mutex_unlock(&bus->bus_lock);
950 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
951 enum sdw_clk_stop_mode mode,
952 enum sdw_clk_stop_type type)
956 mutex_lock(&slave->sdw_dev_lock);
959 struct device *dev = &slave->dev;
960 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
962 if (drv->ops && drv->ops->clk_stop)
963 ret = drv->ops->clk_stop(slave, mode, type);
966 mutex_unlock(&slave->sdw_dev_lock);
971 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
972 enum sdw_clk_stop_mode mode,
979 wake_en = slave->prop.wake_capable;
982 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
984 if (mode == SDW_CLK_STOP_MODE1)
985 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
988 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
990 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
993 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
997 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
1000 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
1002 if (ret < 0 && ret != -ENODATA)
1003 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
1008 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
1010 int retry = bus->clk_stop_timeout;
1014 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
1016 if (val != -ENODATA)
1017 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
1020 val &= SDW_SCP_STAT_CLK_STP_NF;
1022 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
1027 usleep_range(1000, 1500);
1031 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
1038 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
1040 * @bus: SDW bus instance
1042 * Query Slave for clock stop mode and prepare for that mode.
1044 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
1046 bool simple_clk_stop = true;
1047 struct sdw_slave *slave;
1048 bool is_slave = false;
1052 * In order to save on transition time, prepare
1053 * each Slave and then wait for all Slave(s) to be
1054 * prepared for clock stop.
1055 * If one of the Slave devices has lost sync and
1056 * replies with Command Ignored/-ENODATA, we continue
1059 list_for_each_entry(slave, &bus->slaves, node) {
1060 if (!slave->dev_num)
1063 if (slave->status != SDW_SLAVE_ATTACHED &&
1064 slave->status != SDW_SLAVE_ALERT)
1067 /* Identify if Slave(s) are available on Bus */
1070 ret = sdw_slave_clk_stop_callback(slave,
1072 SDW_CLK_PRE_PREPARE);
1073 if (ret < 0 && ret != -ENODATA) {
1074 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
1078 /* Only prepare a Slave device if needed */
1079 if (!slave->prop.simple_clk_stop_capable) {
1080 simple_clk_stop = false;
1082 ret = sdw_slave_clk_stop_prepare(slave,
1085 if (ret < 0 && ret != -ENODATA) {
1086 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
1092 /* Skip remaining clock stop preparation if no Slave is attached */
1097 * Don't wait for all Slaves to be ready if they follow the simple
1100 if (!simple_clk_stop) {
1101 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
1102 SDW_BROADCAST_DEV_NUM);
1104 * if there are no Slave devices present and the reply is
1105 * Command_Ignored/-ENODATA, we don't need to continue with the
1106 * flow and can just return here. The error code is not modified
1107 * and its handling left as an exercise for the caller.
1113 /* Inform slaves that prep is done */
1114 list_for_each_entry(slave, &bus->slaves, node) {
1115 if (!slave->dev_num)
1118 if (slave->status != SDW_SLAVE_ATTACHED &&
1119 slave->status != SDW_SLAVE_ALERT)
1122 ret = sdw_slave_clk_stop_callback(slave,
1124 SDW_CLK_POST_PREPARE);
1126 if (ret < 0 && ret != -ENODATA) {
1127 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1134 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1137 * sdw_bus_clk_stop: stop bus clock
1139 * @bus: SDW bus instance
1141 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1142 * write to SCP_CTRL register.
1144 int sdw_bus_clk_stop(struct sdw_bus *bus)
1149 * broadcast clock stop now, attached Slaves will ACK this,
1150 * unattached will ignore
1152 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1153 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1155 if (ret != -ENODATA)
1156 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
1162 EXPORT_SYMBOL(sdw_bus_clk_stop);
1165 * sdw_bus_exit_clk_stop: Exit clock stop mode
1167 * @bus: SDW bus instance
1169 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1170 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1173 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1175 bool simple_clk_stop = true;
1176 struct sdw_slave *slave;
1177 bool is_slave = false;
1181 * In order to save on transition time, de-prepare
1182 * each Slave and then wait for all Slave(s) to be
1183 * de-prepared after clock resume.
1185 list_for_each_entry(slave, &bus->slaves, node) {
1186 if (!slave->dev_num)
1189 if (slave->status != SDW_SLAVE_ATTACHED &&
1190 slave->status != SDW_SLAVE_ALERT)
1193 /* Identify if Slave(s) are available on Bus */
1196 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1197 SDW_CLK_PRE_DEPREPARE);
1199 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
1201 /* Only de-prepare a Slave device if needed */
1202 if (!slave->prop.simple_clk_stop_capable) {
1203 simple_clk_stop = false;
1205 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1209 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
1213 /* Skip remaining clock stop de-preparation if no Slave is attached */
1218 * Don't wait for all Slaves to be ready if they follow the simple
1221 if (!simple_clk_stop) {
1222 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1224 dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
1227 list_for_each_entry(slave, &bus->slaves, node) {
1228 if (!slave->dev_num)
1231 if (slave->status != SDW_SLAVE_ATTACHED &&
1232 slave->status != SDW_SLAVE_ALERT)
1235 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1236 SDW_CLK_POST_DEPREPARE);
1238 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
1243 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1245 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1246 int port, bool enable, int mask)
1252 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1253 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1254 enable ? "on" : "off");
1255 mask |= SDW_DPN_INT_TEST_FAIL;
1258 addr = SDW_DPN_INTMASK(port);
1260 /* Set/Clear port ready interrupt mask */
1263 val |= SDW_DPN_INT_PORT_READY;
1266 val &= ~SDW_DPN_INT_PORT_READY;
1269 ret = sdw_update_no_pm(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1271 dev_err(&slave->dev,
1272 "SDW_DPN_INTMASK write failed:%d\n", val);
1277 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1279 u32 mclk_freq = slave->bus->prop.mclk_freq;
1280 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1287 * frequency base and scale registers are required for SDCA
1288 * devices. They may also be used for 1.2+/non-SDCA devices.
1289 * Driver can set the property, we will need a DisCo property
1290 * to discover this case from platform firmware.
1292 if (!slave->id.class_id && !slave->prop.clock_reg_supported)
1296 dev_err(&slave->dev,
1297 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1302 * map base frequency using Table 89 of SoundWire 1.2 spec.
1303 * The order of the tests just follows the specification, this
1304 * is not a selection between possible values or a search for
1305 * the best value but just a mapping. Only one case per platform
1307 * Some BIOS have inconsistent values for mclk_freq but a
1308 * correct root so we force the mclk_freq to avoid variations.
1310 if (!(19200000 % mclk_freq)) {
1311 mclk_freq = 19200000;
1312 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1313 } else if (!(24000000 % mclk_freq)) {
1314 mclk_freq = 24000000;
1315 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1316 } else if (!(24576000 % mclk_freq)) {
1317 mclk_freq = 24576000;
1318 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1319 } else if (!(22579200 % mclk_freq)) {
1320 mclk_freq = 22579200;
1321 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1322 } else if (!(32000000 % mclk_freq)) {
1323 mclk_freq = 32000000;
1324 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1326 dev_err(&slave->dev,
1327 "Unsupported clock base, mclk %d\n",
1332 if (mclk_freq % curr_freq) {
1333 dev_err(&slave->dev,
1334 "mclk %d is not multiple of bus curr_freq %d\n",
1335 mclk_freq, curr_freq);
1339 scale = mclk_freq / curr_freq;
1342 * map scale to Table 90 of SoundWire 1.2 spec - and check
1343 * that the scale is a power of two and maximum 64
1345 scale_index = ilog2(scale);
1347 if (BIT(scale_index) != scale || scale_index > 6) {
1348 dev_err(&slave->dev,
1349 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1350 scale, mclk_freq, curr_freq);
1355 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1357 dev_err(&slave->dev,
1358 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1362 /* initialize scale for both banks */
1363 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1365 dev_err(&slave->dev,
1366 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1369 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1371 dev_err(&slave->dev,
1372 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1374 dev_dbg(&slave->dev,
1375 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1376 base, scale_index, mclk_freq, curr_freq);
1381 static int sdw_initialize_slave(struct sdw_slave *slave)
1383 struct sdw_slave_prop *prop = &slave->prop;
1388 ret = sdw_slave_set_frequency(slave);
1392 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1393 /* Clear bus clash interrupt before enabling interrupt mask */
1394 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1396 dev_err(&slave->dev,
1397 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1400 if (status & SDW_SCP_INT1_BUS_CLASH) {
1401 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1402 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1404 dev_err(&slave->dev,
1405 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1410 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1411 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1412 /* Clear parity interrupt before enabling interrupt mask */
1413 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1415 dev_err(&slave->dev,
1416 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1419 if (status & SDW_SCP_INT1_PARITY) {
1420 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1421 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1423 dev_err(&slave->dev,
1424 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1431 * Set SCP_INT1_MASK register, typically bus clash and
1432 * implementation-defined interrupt mask. The Parity detection
1433 * may not always be correct on startup so its use is
1434 * device-dependent, it might e.g. only be enabled in
1435 * steady-state after a couple of frames.
1437 val = slave->prop.scp_int1_mask;
1439 /* Enable SCP interrupts */
1440 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1442 dev_err(&slave->dev,
1443 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
1447 /* No need to continue if DP0 is not present */
1448 if (!slave->prop.dp0_prop)
1451 /* Enable DP0 interrupts */
1452 val = prop->dp0_prop->imp_def_interrupts;
1453 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1455 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1457 dev_err(&slave->dev,
1458 "SDW_DP0_INTMASK read failed:%d\n", ret);
1462 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1464 u8 clear, impl_int_mask;
1465 int status, status2, ret, count = 0;
1467 status = sdw_read_no_pm(slave, SDW_DP0_INT);
1469 dev_err(&slave->dev,
1470 "SDW_DP0_INT read failed:%d\n", status);
1475 clear = status & ~SDW_DP0_INTERRUPTS;
1477 if (status & SDW_DP0_INT_TEST_FAIL) {
1478 dev_err(&slave->dev, "Test fail for port 0\n");
1479 clear |= SDW_DP0_INT_TEST_FAIL;
1483 * Assumption: PORT_READY interrupt will be received only for
1484 * ports implementing Channel Prepare state machine (CP_SM)
1487 if (status & SDW_DP0_INT_PORT_READY) {
1488 complete(&slave->port_ready[0]);
1489 clear |= SDW_DP0_INT_PORT_READY;
1492 if (status & SDW_DP0_INT_BRA_FAILURE) {
1493 dev_err(&slave->dev, "BRA failed\n");
1494 clear |= SDW_DP0_INT_BRA_FAILURE;
1497 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1498 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1500 if (status & impl_int_mask) {
1501 clear |= impl_int_mask;
1502 *slave_status = clear;
1505 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
1506 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
1508 dev_err(&slave->dev,
1509 "SDW_DP0_INT write failed:%d\n", ret);
1513 /* Read DP0 interrupt again */
1514 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
1516 dev_err(&slave->dev,
1517 "SDW_DP0_INT read failed:%d\n", status2);
1520 /* filter to limit loop to interrupts identified in the first status read */
1525 /* we can get alerts while processing so keep retrying */
1526 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1528 if (count == SDW_READ_INTR_CLEAR_RETRY)
1529 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
1534 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1535 int port, u8 *slave_status)
1537 u8 clear, impl_int_mask;
1538 int status, status2, ret, count = 0;
1542 return sdw_handle_dp0_interrupt(slave, slave_status);
1544 addr = SDW_DPN_INT(port);
1545 status = sdw_read_no_pm(slave, addr);
1547 dev_err(&slave->dev,
1548 "SDW_DPN_INT read failed:%d\n", status);
1554 clear = status & ~SDW_DPN_INTERRUPTS;
1556 if (status & SDW_DPN_INT_TEST_FAIL) {
1557 dev_err(&slave->dev, "Test fail for port:%d\n", port);
1558 clear |= SDW_DPN_INT_TEST_FAIL;
1562 * Assumption: PORT_READY interrupt will be received only
1563 * for ports implementing CP_SM.
1565 if (status & SDW_DPN_INT_PORT_READY) {
1566 complete(&slave->port_ready[port]);
1567 clear |= SDW_DPN_INT_PORT_READY;
1570 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1571 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1573 if (status & impl_int_mask) {
1574 clear |= impl_int_mask;
1575 *slave_status = clear;
1578 /* clear the interrupt but don't touch reserved fields */
1579 ret = sdw_write_no_pm(slave, addr, clear);
1581 dev_err(&slave->dev,
1582 "SDW_DPN_INT write failed:%d\n", ret);
1586 /* Read DPN interrupt again */
1587 status2 = sdw_read_no_pm(slave, addr);
1589 dev_err(&slave->dev,
1590 "SDW_DPN_INT read failed:%d\n", status2);
1593 /* filter to limit loop to interrupts identified in the first status read */
1598 /* we can get alerts while processing so keep retrying */
1599 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1601 if (count == SDW_READ_INTR_CLEAR_RETRY)
1602 dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
1607 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1609 struct sdw_slave_intr_status slave_intr;
1610 u8 clear = 0, bit, port_status[15] = {0};
1611 int port_num, stat, ret, count = 0;
1614 u8 sdca_cascade = 0;
1619 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1621 ret = pm_runtime_get_sync(&slave->dev);
1622 if (ret < 0 && ret != -EACCES) {
1623 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1624 pm_runtime_put_noidle(&slave->dev);
1628 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1629 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1631 dev_err(&slave->dev,
1632 "SDW_SCP_INT1 read failed:%d\n", ret);
1637 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1639 dev_err(&slave->dev,
1640 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1644 if (slave->id.class_id) {
1645 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1647 dev_err(&slave->dev,
1648 "SDW_DP0_INT read failed:%d\n", ret);
1651 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1655 slave_notify = false;
1658 * Check parity, bus clash and Slave (impl defined)
1661 if (buf & SDW_SCP_INT1_PARITY) {
1662 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1663 parity_quirk = !slave->first_interrupt_done &&
1664 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1666 if (parity_check && !parity_quirk)
1667 dev_err(&slave->dev, "Parity error detected\n");
1668 clear |= SDW_SCP_INT1_PARITY;
1671 if (buf & SDW_SCP_INT1_BUS_CLASH) {
1672 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1673 dev_err(&slave->dev, "Bus clash detected\n");
1674 clear |= SDW_SCP_INT1_BUS_CLASH;
1678 * When bus clash or parity errors are detected, such errors
1679 * are unlikely to be recoverable errors.
1680 * TODO: In such scenario, reset bus. Make this configurable
1681 * via sysfs property with bus reset being the default.
1684 if (buf & SDW_SCP_INT1_IMPL_DEF) {
1685 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1686 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1687 slave_notify = true;
1689 clear |= SDW_SCP_INT1_IMPL_DEF;
1692 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1694 slave_notify = true;
1696 /* Check port 0 - 3 interrupts */
1697 port = buf & SDW_SCP_INT1_PORT0_3;
1699 /* To get port number corresponding to bits, shift it */
1700 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1701 for_each_set_bit(bit, &port, 8) {
1702 sdw_handle_port_interrupt(slave, bit,
1706 /* Check if cascade 2 interrupt is present */
1707 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1708 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1709 for_each_set_bit(bit, &port, 8) {
1710 /* scp2 ports start from 4 */
1712 sdw_handle_port_interrupt(slave,
1714 &port_status[port_num]);
1718 /* now check last cascade */
1719 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1720 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1721 for_each_set_bit(bit, &port, 8) {
1722 /* scp3 ports start from 11 */
1723 port_num = bit + 11;
1724 sdw_handle_port_interrupt(slave,
1726 &port_status[port_num]);
1730 /* Update the Slave driver */
1732 mutex_lock(&slave->sdw_dev_lock);
1734 if (slave->probed) {
1735 struct device *dev = &slave->dev;
1736 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1738 if (slave->prop.use_domain_irq && slave->irq)
1739 handle_nested_irq(slave->irq);
1741 if (drv->ops && drv->ops->interrupt_callback) {
1742 slave_intr.sdca_cascade = sdca_cascade;
1743 slave_intr.control_port = clear;
1744 memcpy(slave_intr.port, &port_status,
1745 sizeof(slave_intr.port));
1747 drv->ops->interrupt_callback(slave, &slave_intr);
1751 mutex_unlock(&slave->sdw_dev_lock);
1755 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
1757 dev_err(&slave->dev,
1758 "SDW_SCP_INT1 write failed:%d\n", ret);
1762 /* at this point all initial interrupt sources were handled */
1763 slave->first_interrupt_done = true;
1766 * Read status again to ensure no new interrupts arrived
1767 * while servicing interrupts.
1769 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1771 dev_err(&slave->dev,
1772 "SDW_SCP_INT1 recheck read failed:%d\n", ret);
1777 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1779 dev_err(&slave->dev,
1780 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
1784 if (slave->id.class_id) {
1785 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1787 dev_err(&slave->dev,
1788 "SDW_DP0_INT recheck read failed:%d\n", ret);
1791 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1795 * Make sure no interrupts are pending
1797 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
1800 * Exit loop if Slave is continuously in ALERT state even
1801 * after servicing the interrupt multiple times.
1805 /* we can get alerts while processing so keep retrying */
1806 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1808 if (count == SDW_READ_INTR_CLEAR_RETRY)
1809 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
1812 pm_runtime_mark_last_busy(&slave->dev);
1813 pm_runtime_put_autosuspend(&slave->dev);
1818 static int sdw_update_slave_status(struct sdw_slave *slave,
1819 enum sdw_slave_status status)
1823 mutex_lock(&slave->sdw_dev_lock);
1825 if (slave->probed) {
1826 struct device *dev = &slave->dev;
1827 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1829 if (drv->ops && drv->ops->update_status)
1830 ret = drv->ops->update_status(slave, status);
1833 mutex_unlock(&slave->sdw_dev_lock);
1839 * sdw_handle_slave_status() - Handle Slave status
1840 * @bus: SDW bus instance
1841 * @status: Status for all Slave(s)
1843 int sdw_handle_slave_status(struct sdw_bus *bus,
1844 enum sdw_slave_status status[])
1846 enum sdw_slave_status prev_status;
1847 struct sdw_slave *slave;
1848 bool attached_initializing, id_programmed;
1851 /* first check if any Slaves fell off the bus */
1852 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1853 mutex_lock(&bus->bus_lock);
1854 if (test_bit(i, bus->assigned) == false) {
1855 mutex_unlock(&bus->bus_lock);
1858 mutex_unlock(&bus->bus_lock);
1860 slave = sdw_get_slave(bus, i);
1864 if (status[i] == SDW_SLAVE_UNATTACHED &&
1865 slave->status != SDW_SLAVE_UNATTACHED) {
1866 dev_warn(&slave->dev, "Slave %d state check1: UNATTACHED, status was %d\n",
1868 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1870 /* Ensure driver knows that peripheral unattached */
1871 ret = sdw_update_slave_status(slave, status[i]);
1873 dev_warn(&slave->dev, "Update Slave status failed:%d\n", ret);
1877 if (status[0] == SDW_SLAVE_ATTACHED) {
1878 dev_dbg(bus->dev, "Slave attached, programming device number\n");
1881 * Programming a device number will have side effects,
1882 * so we deal with other devices at a later time.
1883 * This relies on those devices reporting ATTACHED, which will
1884 * trigger another call to this function. This will only
1885 * happen if at least one device ID was programmed.
1886 * Error returns from sdw_program_device_num() are currently
1887 * ignored because there's no useful recovery that can be done.
1888 * Returning the error here could result in the current status
1889 * of other devices not being handled, because if no device IDs
1890 * were programmed there's nothing to guarantee a status change
1891 * to trigger another call to this function.
1893 sdw_program_device_num(bus, &id_programmed);
1898 /* Continue to check other slave statuses */
1899 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1900 mutex_lock(&bus->bus_lock);
1901 if (test_bit(i, bus->assigned) == false) {
1902 mutex_unlock(&bus->bus_lock);
1905 mutex_unlock(&bus->bus_lock);
1907 slave = sdw_get_slave(bus, i);
1911 attached_initializing = false;
1913 switch (status[i]) {
1914 case SDW_SLAVE_UNATTACHED:
1915 if (slave->status == SDW_SLAVE_UNATTACHED)
1918 dev_warn(&slave->dev, "Slave %d state check2: UNATTACHED, status was %d\n",
1921 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1924 case SDW_SLAVE_ALERT:
1925 ret = sdw_handle_slave_alerts(slave);
1927 dev_err(&slave->dev,
1928 "Slave %d alert handling failed: %d\n",
1932 case SDW_SLAVE_ATTACHED:
1933 if (slave->status == SDW_SLAVE_ATTACHED)
1936 prev_status = slave->status;
1937 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1939 if (prev_status == SDW_SLAVE_ALERT)
1942 attached_initializing = true;
1944 ret = sdw_initialize_slave(slave);
1946 dev_err(&slave->dev,
1947 "Slave %d initialization failed: %d\n",
1953 dev_err(&slave->dev, "Invalid slave %d status:%d\n",
1958 ret = sdw_update_slave_status(slave, status[i]);
1960 dev_err(&slave->dev,
1961 "Update Slave status failed:%d\n", ret);
1962 if (attached_initializing) {
1963 dev_dbg(&slave->dev,
1964 "signaling initialization completion for Slave %d\n",
1967 complete_all(&slave->initialization_complete);
1970 * If the manager became pm_runtime active, the peripherals will be
1971 * restarted and attach, but their pm_runtime status may remain
1972 * suspended. If the 'update_slave_status' callback initiates
1973 * any sort of deferred processing, this processing would not be
1974 * cancelled on pm_runtime suspend.
1975 * To avoid such zombie states, we queue a request to resume.
1976 * This would be a no-op in case the peripheral was being resumed
1977 * by e.g. the ALSA/ASoC framework.
1979 pm_request_resume(&slave->dev);
1985 EXPORT_SYMBOL(sdw_handle_slave_status);
1987 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1989 struct sdw_slave *slave;
1992 /* Check all non-zero devices */
1993 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1994 mutex_lock(&bus->bus_lock);
1995 if (test_bit(i, bus->assigned) == false) {
1996 mutex_unlock(&bus->bus_lock);
1999 mutex_unlock(&bus->bus_lock);
2001 slave = sdw_get_slave(bus, i);
2005 if (slave->status != SDW_SLAVE_UNATTACHED) {
2006 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
2007 slave->first_interrupt_done = false;
2008 sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
2011 /* keep track of request, used in pm_runtime resume */
2012 slave->unattach_request = request;
2015 EXPORT_SYMBOL(sdw_clear_slave_status);