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);
29 * sdw_bus_master_add() - add a bus Master instance
31 * @parent: parent device
32 * @fwnode: firmware node handle
34 * Initializes the bus instance, read properties and create child
37 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
38 struct fwnode_handle *fwnode)
40 struct sdw_master_prop *prop = NULL;
44 pr_err("SoundWire parent device is not set\n");
48 ret = sdw_get_id(bus);
50 dev_err(parent, "Failed to get bus id\n");
54 ret = sdw_master_device_add(bus, parent, fwnode);
56 dev_err(parent, "Failed to add master device at link %d\n",
62 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
66 if (!bus->compute_params) {
68 "Bandwidth allocation not configured, compute_params no set\n");
73 * Give each bus_lock and msg_lock a unique key so that lockdep won't
74 * trigger a deadlock warning when the locks of several buses are
75 * grabbed during configuration of a multi-bus stream.
77 lockdep_register_key(&bus->msg_lock_key);
78 __mutex_init(&bus->msg_lock, "msg_lock", &bus->msg_lock_key);
80 lockdep_register_key(&bus->bus_lock_key);
81 __mutex_init(&bus->bus_lock, "bus_lock", &bus->bus_lock_key);
83 INIT_LIST_HEAD(&bus->slaves);
84 INIT_LIST_HEAD(&bus->m_rt_list);
87 * Initialize multi_link flag
89 bus->multi_link = false;
90 if (bus->ops->read_prop) {
91 ret = bus->ops->read_prop(bus);
94 "Bus read properties failed:%d\n", ret);
99 sdw_bus_debugfs_init(bus);
102 * Device numbers in SoundWire are 0 through 15. Enumeration device
103 * number (0), Broadcast device number (15), Group numbers (12 and
104 * 13) and Master device number (14) are not used for assignment so
105 * mask these and other higher bits.
108 /* Set higher order bits */
109 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
111 /* Set enumuration device number and broadcast device number */
112 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
113 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
115 /* Set group device numbers and master device number */
116 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
117 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
118 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
121 * SDW is an enumerable bus, but devices can be powered off. So,
122 * they won't be able to report as present.
124 * Create Slave devices based on Slaves described in
125 * the respective firmware (ACPI/DT)
127 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
128 ret = sdw_acpi_find_slaves(bus);
129 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
130 ret = sdw_of_find_slaves(bus);
132 ret = -ENOTSUPP; /* No ACPI/DT so error out */
135 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
140 * Initialize clock values based on Master properties. The max
141 * frequency is read from max_clk_freq property. Current assumption
142 * is that the bus will start at highest clock frequency when
145 * Default active bank will be 0 as out of reset the Slaves have
146 * to start with bank 0 (Table 40 of Spec)
149 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
150 bus->params.curr_dr_freq = bus->params.max_dr_freq;
151 bus->params.curr_bank = SDW_BANK0;
152 bus->params.next_bank = SDW_BANK1;
154 ret = sdw_irq_create(bus, fwnode);
160 EXPORT_SYMBOL(sdw_bus_master_add);
162 static int sdw_delete_slave(struct device *dev, void *data)
164 struct sdw_slave *slave = dev_to_sdw_dev(dev);
165 struct sdw_bus *bus = slave->bus;
167 pm_runtime_disable(dev);
169 sdw_slave_debugfs_exit(slave);
171 mutex_lock(&bus->bus_lock);
173 if (slave->dev_num) { /* clear dev_num if assigned */
174 clear_bit(slave->dev_num, bus->assigned);
175 if (bus->ops && bus->ops->put_device_num)
176 bus->ops->put_device_num(bus, slave);
178 list_del_init(&slave->node);
179 mutex_unlock(&bus->bus_lock);
181 device_unregister(dev);
186 * sdw_bus_master_delete() - delete the bus master instance
187 * @bus: bus to be deleted
189 * Remove the instance, delete the child devices.
191 void sdw_bus_master_delete(struct sdw_bus *bus)
193 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
197 sdw_master_device_del(bus);
199 sdw_bus_debugfs_exit(bus);
200 lockdep_unregister_key(&bus->bus_lock_key);
201 lockdep_unregister_key(&bus->msg_lock_key);
202 ida_free(&sdw_bus_ida, bus->id);
204 EXPORT_SYMBOL(sdw_bus_master_delete);
210 static inline int find_response_code(enum sdw_command_response resp)
216 case SDW_CMD_IGNORED:
219 case SDW_CMD_TIMEOUT:
227 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
229 int retry = bus->prop.err_threshold;
230 enum sdw_command_response resp;
233 for (i = 0; i <= retry; i++) {
234 resp = bus->ops->xfer_msg(bus, msg);
235 ret = find_response_code(resp);
237 /* if cmd is ok or ignored return */
238 if (ret == 0 || ret == -ENODATA)
245 static inline int do_transfer_defer(struct sdw_bus *bus,
248 struct sdw_defer *defer = &bus->defer_msg;
249 int retry = bus->prop.err_threshold;
250 enum sdw_command_response resp;
254 defer->length = msg->len;
255 init_completion(&defer->complete);
257 for (i = 0; i <= retry; i++) {
258 resp = bus->ops->xfer_msg_defer(bus);
259 ret = find_response_code(resp);
260 /* if cmd is ok or ignored return */
261 if (ret == 0 || ret == -ENODATA)
268 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
272 ret = do_transfer(bus, msg);
273 if (ret != 0 && ret != -ENODATA)
274 dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
276 (msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
277 msg->addr, msg->len);
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_show_ping_status() - Direct report of PING status, to be used by Peripheral drivers
303 * @sync_delay: Delay before reading status
305 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay)
309 if (!bus->ops->read_ping_status)
313 * wait for peripheral to sync if desired. 10-15ms should be more than
314 * enough in most cases.
317 usleep_range(10000, 15000);
319 mutex_lock(&bus->msg_lock);
321 status = bus->ops->read_ping_status(bus);
323 mutex_unlock(&bus->msg_lock);
326 dev_warn(bus->dev, "%s: no peripherals attached\n", __func__);
328 dev_dbg(bus->dev, "PING status: %#x\n", status);
330 EXPORT_SYMBOL(sdw_show_ping_status);
333 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
335 * @msg: SDW message to be xfered
337 * Caller needs to hold the msg_lock lock while calling this
339 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg)
343 if (!bus->ops->xfer_msg_defer)
346 ret = do_transfer_defer(bus, msg);
347 if (ret != 0 && ret != -ENODATA)
348 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
354 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
355 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
357 memset(msg, 0, sizeof(*msg));
358 msg->addr = addr; /* addr is 16 bit and truncated here */
360 msg->dev_num = dev_num;
364 if (addr < SDW_REG_NO_PAGE) /* no paging area */
367 if (addr >= SDW_REG_MAX) { /* illegal addr */
368 pr_err("SDW: Invalid address %x passed\n", addr);
372 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
373 if (slave && !slave->prop.paging_support)
375 /* no need for else as that will fall-through to paging */
378 /* paging mandatory */
379 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
380 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
385 pr_err("SDW: No slave for paging addr\n");
389 if (!slave->prop.paging_support) {
391 "address %x needs paging but no support\n", addr);
395 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
396 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
397 msg->addr |= BIT(15);
404 * Read/Write IO functions.
407 static int sdw_ntransfer_no_pm(struct sdw_slave *slave, u32 addr, u8 flags,
408 size_t count, u8 *val)
415 // Only handle bytes up to next page boundary
416 size = min_t(size_t, count, (SDW_REGADDR + 1) - (addr & SDW_REGADDR));
418 ret = sdw_fill_msg(&msg, slave, addr, size, slave->dev_num, flags, val);
422 ret = sdw_transfer(slave->bus, &msg);
423 if (ret < 0 && !slave->is_mockup_device)
435 * sdw_nread_no_pm() - Read "n" contiguous SDW Slave registers with no PM
437 * @addr: Register address
439 * @val: Buffer for values to be read
441 * Note that if the message crosses a page boundary each page will be
442 * transferred under a separate invocation of the msg_lock.
444 int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
446 return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_READ, count, val);
448 EXPORT_SYMBOL(sdw_nread_no_pm);
451 * sdw_nwrite_no_pm() - Write "n" contiguous SDW Slave registers with no PM
453 * @addr: Register address
455 * @val: Buffer for values to be written
457 * Note that if the message crosses a page boundary each page will be
458 * transferred under a separate invocation of the msg_lock.
460 int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
462 return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_WRITE, count, (u8 *)val);
464 EXPORT_SYMBOL(sdw_nwrite_no_pm);
467 * sdw_write_no_pm() - Write a SDW Slave register with no PM
469 * @addr: Register address
470 * @value: Register value
472 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
474 return sdw_nwrite_no_pm(slave, addr, 1, &value);
476 EXPORT_SYMBOL(sdw_write_no_pm);
479 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
485 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
486 SDW_MSG_FLAG_READ, &buf);
490 ret = sdw_transfer(bus, &msg);
498 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
503 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
504 SDW_MSG_FLAG_WRITE, &value);
508 return sdw_transfer(bus, &msg);
511 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
517 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
518 SDW_MSG_FLAG_READ, &buf);
522 ret = sdw_transfer_unlocked(bus, &msg);
528 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
530 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
535 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
536 SDW_MSG_FLAG_WRITE, &value);
540 return sdw_transfer_unlocked(bus, &msg);
542 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
545 * sdw_read_no_pm() - Read a SDW Slave register with no PM
547 * @addr: Register address
549 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
554 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
560 EXPORT_SYMBOL(sdw_read_no_pm);
562 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
566 tmp = sdw_read_no_pm(slave, addr);
570 tmp = (tmp & ~mask) | val;
571 return sdw_write_no_pm(slave, addr, tmp);
573 EXPORT_SYMBOL(sdw_update_no_pm);
575 /* Read-Modify-Write Slave register */
576 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
580 tmp = sdw_read(slave, addr);
584 tmp = (tmp & ~mask) | val;
585 return sdw_write(slave, addr, tmp);
587 EXPORT_SYMBOL(sdw_update);
590 * sdw_nread() - Read "n" contiguous SDW Slave registers
592 * @addr: Register address
594 * @val: Buffer for values to be read
596 * This version of the function will take a PM reference to the slave
598 * Note that if the message crosses a page boundary each page will be
599 * transferred under a separate invocation of the msg_lock.
601 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
605 ret = pm_runtime_get_sync(&slave->dev);
606 if (ret < 0 && ret != -EACCES) {
607 pm_runtime_put_noidle(&slave->dev);
611 ret = sdw_nread_no_pm(slave, addr, count, val);
613 pm_runtime_mark_last_busy(&slave->dev);
614 pm_runtime_put(&slave->dev);
618 EXPORT_SYMBOL(sdw_nread);
621 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
623 * @addr: Register address
625 * @val: Buffer for values to be written
627 * This version of the function will take a PM reference to the slave
629 * Note that if the message crosses a page boundary each page will be
630 * transferred under a separate invocation of the msg_lock.
632 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
636 ret = pm_runtime_get_sync(&slave->dev);
637 if (ret < 0 && ret != -EACCES) {
638 pm_runtime_put_noidle(&slave->dev);
642 ret = sdw_nwrite_no_pm(slave, addr, count, val);
644 pm_runtime_mark_last_busy(&slave->dev);
645 pm_runtime_put(&slave->dev);
649 EXPORT_SYMBOL(sdw_nwrite);
652 * sdw_read() - Read a SDW Slave register
654 * @addr: Register address
656 * This version of the function will take a PM reference to the slave
659 int sdw_read(struct sdw_slave *slave, u32 addr)
664 ret = sdw_nread(slave, addr, 1, &buf);
670 EXPORT_SYMBOL(sdw_read);
673 * sdw_write() - Write a SDW Slave register
675 * @addr: Register address
676 * @value: Register value
678 * This version of the function will take a PM reference to the slave
681 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
683 return sdw_nwrite(slave, addr, 1, &value);
685 EXPORT_SYMBOL(sdw_write);
691 /* called with bus_lock held */
692 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
694 struct sdw_slave *slave;
696 list_for_each_entry(slave, &bus->slaves, node) {
697 if (slave->dev_num == i)
704 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
706 if (slave->id.mfg_id != id.mfg_id ||
707 slave->id.part_id != id.part_id ||
708 slave->id.class_id != id.class_id ||
709 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
710 slave->id.unique_id != id.unique_id))
715 EXPORT_SYMBOL(sdw_compare_devid);
717 /* called with bus_lock held */
718 static int sdw_get_device_num(struct sdw_slave *slave)
720 struct sdw_bus *bus = slave->bus;
723 if (bus->ops && bus->ops->get_device_num) {
724 bit = bus->ops->get_device_num(bus, slave);
728 bit = find_first_zero_bit(bus->assigned, SDW_MAX_DEVICES);
729 if (bit == SDW_MAX_DEVICES) {
736 * Do not update dev_num in Slave data structure here,
737 * Update once program dev_num is successful
739 set_bit(bit, bus->assigned);
745 static int sdw_assign_device_num(struct sdw_slave *slave)
747 struct sdw_bus *bus = slave->bus;
749 bool new_device = false;
751 /* check first if device number is assigned, if so reuse that */
752 if (!slave->dev_num) {
753 if (!slave->dev_num_sticky) {
754 mutex_lock(&slave->bus->bus_lock);
755 dev_num = sdw_get_device_num(slave);
756 mutex_unlock(&slave->bus->bus_lock);
758 dev_err(bus->dev, "Get dev_num failed: %d\n",
762 slave->dev_num = dev_num;
763 slave->dev_num_sticky = dev_num;
766 slave->dev_num = slave->dev_num_sticky;
772 "Slave already registered, reusing dev_num:%d\n",
775 /* Clear the slave->dev_num to transfer message on device 0 */
776 dev_num = slave->dev_num;
779 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
781 dev_err(bus->dev, "Program device_num %d failed: %d\n",
786 /* After xfer of msg, restore dev_num */
787 slave->dev_num = slave->dev_num_sticky;
789 if (bus->ops && bus->ops->new_peripheral_assigned)
790 bus->ops->new_peripheral_assigned(bus, slave, dev_num);
795 void sdw_extract_slave_id(struct sdw_bus *bus,
796 u64 addr, struct sdw_slave_id *id)
798 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
800 id->sdw_version = SDW_VERSION(addr);
801 id->unique_id = SDW_UNIQUE_ID(addr);
802 id->mfg_id = SDW_MFG_ID(addr);
803 id->part_id = SDW_PART_ID(addr);
804 id->class_id = SDW_CLASS_ID(addr);
807 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
808 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
810 EXPORT_SYMBOL(sdw_extract_slave_id);
812 static int sdw_program_device_num(struct sdw_bus *bus, bool *programmed)
814 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
815 struct sdw_slave *slave, *_s;
816 struct sdw_slave_id id;
824 /* No Slave, so use raw xfer api */
825 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
826 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
831 ret = sdw_transfer(bus, &msg);
832 if (ret == -ENODATA) { /* end of device id reads */
833 dev_dbg(bus->dev, "No more devices to enumerate\n");
838 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
843 * Construct the addr and extract. Cast the higher shift
844 * bits to avoid truncation due to size limit.
846 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
847 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
850 sdw_extract_slave_id(bus, addr, &id);
853 /* Now compare with entries */
854 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
855 if (sdw_compare_devid(slave, id) == 0) {
859 * To prevent skipping state-machine stages don't
860 * program a device until we've seen it UNATTACH.
861 * Must return here because no other device on #0
862 * can be detected until this one has been
863 * assigned a device ID.
865 if (slave->status != SDW_SLAVE_UNATTACHED)
869 * Assign a new dev_num to this Slave and
870 * not mark it present. It will be marked
871 * present after it reports ATTACHED on new
874 ret = sdw_assign_device_num(slave);
877 "Assign dev_num failed:%d\n",
889 /* TODO: Park this device in Group 13 */
892 * add Slave device even if there is no platform
893 * firmware description. There will be no driver probe
894 * but the user/integration will be able to see the
895 * device, enumeration status and device number in sysfs
897 sdw_slave_add(bus, &id, NULL);
899 dev_err(bus->dev, "Slave Entry not found\n");
905 * Check till error out or retry (count) exhausts.
906 * Device can drop off and rejoin during enumeration
907 * so count till twice the bound.
910 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
915 static void sdw_modify_slave_status(struct sdw_slave *slave,
916 enum sdw_slave_status status)
918 struct sdw_bus *bus = slave->bus;
920 mutex_lock(&bus->bus_lock);
923 "changing status slave %d status %d new status %d\n",
924 slave->dev_num, slave->status, status);
926 if (status == SDW_SLAVE_UNATTACHED) {
928 "initializing enumeration and init completion for Slave %d\n",
931 reinit_completion(&slave->enumeration_complete);
932 reinit_completion(&slave->initialization_complete);
934 } else if ((status == SDW_SLAVE_ATTACHED) &&
935 (slave->status == SDW_SLAVE_UNATTACHED)) {
937 "signaling enumeration completion for Slave %d\n",
940 complete_all(&slave->enumeration_complete);
942 slave->status = status;
943 mutex_unlock(&bus->bus_lock);
946 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
947 enum sdw_clk_stop_mode mode,
948 enum sdw_clk_stop_type type)
952 mutex_lock(&slave->sdw_dev_lock);
955 struct device *dev = &slave->dev;
956 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
958 if (drv->ops && drv->ops->clk_stop)
959 ret = drv->ops->clk_stop(slave, mode, type);
962 mutex_unlock(&slave->sdw_dev_lock);
967 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
968 enum sdw_clk_stop_mode mode,
975 wake_en = slave->prop.wake_capable;
978 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
980 if (mode == SDW_CLK_STOP_MODE1)
981 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
984 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
986 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
989 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
993 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
996 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
998 if (ret < 0 && ret != -ENODATA)
999 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
1004 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
1006 int retry = bus->clk_stop_timeout;
1010 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
1012 if (val != -ENODATA)
1013 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
1016 val &= SDW_SCP_STAT_CLK_STP_NF;
1018 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
1023 usleep_range(1000, 1500);
1027 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
1034 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
1036 * @bus: SDW bus instance
1038 * Query Slave for clock stop mode and prepare for that mode.
1040 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
1042 bool simple_clk_stop = true;
1043 struct sdw_slave *slave;
1044 bool is_slave = false;
1048 * In order to save on transition time, prepare
1049 * each Slave and then wait for all Slave(s) to be
1050 * prepared for clock stop.
1051 * If one of the Slave devices has lost sync and
1052 * replies with Command Ignored/-ENODATA, we continue
1055 list_for_each_entry(slave, &bus->slaves, node) {
1056 if (!slave->dev_num)
1059 if (slave->status != SDW_SLAVE_ATTACHED &&
1060 slave->status != SDW_SLAVE_ALERT)
1063 /* Identify if Slave(s) are available on Bus */
1066 ret = sdw_slave_clk_stop_callback(slave,
1068 SDW_CLK_PRE_PREPARE);
1069 if (ret < 0 && ret != -ENODATA) {
1070 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
1074 /* Only prepare a Slave device if needed */
1075 if (!slave->prop.simple_clk_stop_capable) {
1076 simple_clk_stop = false;
1078 ret = sdw_slave_clk_stop_prepare(slave,
1081 if (ret < 0 && ret != -ENODATA) {
1082 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
1088 /* Skip remaining clock stop preparation if no Slave is attached */
1093 * Don't wait for all Slaves to be ready if they follow the simple
1096 if (!simple_clk_stop) {
1097 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
1098 SDW_BROADCAST_DEV_NUM);
1100 * if there are no Slave devices present and the reply is
1101 * Command_Ignored/-ENODATA, we don't need to continue with the
1102 * flow and can just return here. The error code is not modified
1103 * and its handling left as an exercise for the caller.
1109 /* Inform slaves that prep is done */
1110 list_for_each_entry(slave, &bus->slaves, node) {
1111 if (!slave->dev_num)
1114 if (slave->status != SDW_SLAVE_ATTACHED &&
1115 slave->status != SDW_SLAVE_ALERT)
1118 ret = sdw_slave_clk_stop_callback(slave,
1120 SDW_CLK_POST_PREPARE);
1122 if (ret < 0 && ret != -ENODATA) {
1123 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1130 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1133 * sdw_bus_clk_stop: stop bus clock
1135 * @bus: SDW bus instance
1137 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1138 * write to SCP_CTRL register.
1140 int sdw_bus_clk_stop(struct sdw_bus *bus)
1145 * broadcast clock stop now, attached Slaves will ACK this,
1146 * unattached will ignore
1148 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1149 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1151 if (ret != -ENODATA)
1152 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
1158 EXPORT_SYMBOL(sdw_bus_clk_stop);
1161 * sdw_bus_exit_clk_stop: Exit clock stop mode
1163 * @bus: SDW bus instance
1165 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1166 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1169 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1171 bool simple_clk_stop = true;
1172 struct sdw_slave *slave;
1173 bool is_slave = false;
1177 * In order to save on transition time, de-prepare
1178 * each Slave and then wait for all Slave(s) to be
1179 * de-prepared after clock resume.
1181 list_for_each_entry(slave, &bus->slaves, node) {
1182 if (!slave->dev_num)
1185 if (slave->status != SDW_SLAVE_ATTACHED &&
1186 slave->status != SDW_SLAVE_ALERT)
1189 /* Identify if Slave(s) are available on Bus */
1192 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1193 SDW_CLK_PRE_DEPREPARE);
1195 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
1197 /* Only de-prepare a Slave device if needed */
1198 if (!slave->prop.simple_clk_stop_capable) {
1199 simple_clk_stop = false;
1201 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1205 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
1209 /* Skip remaining clock stop de-preparation if no Slave is attached */
1214 * Don't wait for all Slaves to be ready if they follow the simple
1217 if (!simple_clk_stop) {
1218 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1220 dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
1223 list_for_each_entry(slave, &bus->slaves, node) {
1224 if (!slave->dev_num)
1227 if (slave->status != SDW_SLAVE_ATTACHED &&
1228 slave->status != SDW_SLAVE_ALERT)
1231 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1232 SDW_CLK_POST_DEPREPARE);
1234 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
1239 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1241 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1242 int port, bool enable, int mask)
1248 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1249 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1250 enable ? "on" : "off");
1251 mask |= SDW_DPN_INT_TEST_FAIL;
1254 addr = SDW_DPN_INTMASK(port);
1256 /* Set/Clear port ready interrupt mask */
1259 val |= SDW_DPN_INT_PORT_READY;
1262 val &= ~SDW_DPN_INT_PORT_READY;
1265 ret = sdw_update_no_pm(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1267 dev_err(&slave->dev,
1268 "SDW_DPN_INTMASK write failed:%d\n", val);
1273 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1275 u32 mclk_freq = slave->bus->prop.mclk_freq;
1276 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1283 * frequency base and scale registers are required for SDCA
1284 * devices. They may also be used for 1.2+/non-SDCA devices.
1285 * Driver can set the property, we will need a DisCo property
1286 * to discover this case from platform firmware.
1288 if (!slave->id.class_id && !slave->prop.clock_reg_supported)
1292 dev_err(&slave->dev,
1293 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1298 * map base frequency using Table 89 of SoundWire 1.2 spec.
1299 * The order of the tests just follows the specification, this
1300 * is not a selection between possible values or a search for
1301 * the best value but just a mapping. Only one case per platform
1303 * Some BIOS have inconsistent values for mclk_freq but a
1304 * correct root so we force the mclk_freq to avoid variations.
1306 if (!(19200000 % mclk_freq)) {
1307 mclk_freq = 19200000;
1308 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1309 } else if (!(24000000 % mclk_freq)) {
1310 mclk_freq = 24000000;
1311 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1312 } else if (!(24576000 % mclk_freq)) {
1313 mclk_freq = 24576000;
1314 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1315 } else if (!(22579200 % mclk_freq)) {
1316 mclk_freq = 22579200;
1317 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1318 } else if (!(32000000 % mclk_freq)) {
1319 mclk_freq = 32000000;
1320 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1322 dev_err(&slave->dev,
1323 "Unsupported clock base, mclk %d\n",
1328 if (mclk_freq % curr_freq) {
1329 dev_err(&slave->dev,
1330 "mclk %d is not multiple of bus curr_freq %d\n",
1331 mclk_freq, curr_freq);
1335 scale = mclk_freq / curr_freq;
1338 * map scale to Table 90 of SoundWire 1.2 spec - and check
1339 * that the scale is a power of two and maximum 64
1341 scale_index = ilog2(scale);
1343 if (BIT(scale_index) != scale || scale_index > 6) {
1344 dev_err(&slave->dev,
1345 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1346 scale, mclk_freq, curr_freq);
1351 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1353 dev_err(&slave->dev,
1354 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1358 /* initialize scale for both banks */
1359 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1361 dev_err(&slave->dev,
1362 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1365 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1367 dev_err(&slave->dev,
1368 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1370 dev_dbg(&slave->dev,
1371 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1372 base, scale_index, mclk_freq, curr_freq);
1377 static int sdw_initialize_slave(struct sdw_slave *slave)
1379 struct sdw_slave_prop *prop = &slave->prop;
1384 ret = sdw_slave_set_frequency(slave);
1388 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1389 /* Clear bus clash interrupt before enabling interrupt mask */
1390 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1392 dev_err(&slave->dev,
1393 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1396 if (status & SDW_SCP_INT1_BUS_CLASH) {
1397 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1398 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1400 dev_err(&slave->dev,
1401 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1406 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1407 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1408 /* Clear parity interrupt before enabling interrupt mask */
1409 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1411 dev_err(&slave->dev,
1412 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1415 if (status & SDW_SCP_INT1_PARITY) {
1416 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1417 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1419 dev_err(&slave->dev,
1420 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1427 * Set SCP_INT1_MASK register, typically bus clash and
1428 * implementation-defined interrupt mask. The Parity detection
1429 * may not always be correct on startup so its use is
1430 * device-dependent, it might e.g. only be enabled in
1431 * steady-state after a couple of frames.
1433 val = slave->prop.scp_int1_mask;
1435 /* Enable SCP interrupts */
1436 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1438 dev_err(&slave->dev,
1439 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
1443 /* No need to continue if DP0 is not present */
1444 if (!slave->prop.dp0_prop)
1447 /* Enable DP0 interrupts */
1448 val = prop->dp0_prop->imp_def_interrupts;
1449 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1451 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1453 dev_err(&slave->dev,
1454 "SDW_DP0_INTMASK read failed:%d\n", ret);
1458 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1460 u8 clear, impl_int_mask;
1461 int status, status2, ret, count = 0;
1463 status = sdw_read_no_pm(slave, SDW_DP0_INT);
1465 dev_err(&slave->dev,
1466 "SDW_DP0_INT read failed:%d\n", status);
1471 clear = status & ~SDW_DP0_INTERRUPTS;
1473 if (status & SDW_DP0_INT_TEST_FAIL) {
1474 dev_err(&slave->dev, "Test fail for port 0\n");
1475 clear |= SDW_DP0_INT_TEST_FAIL;
1479 * Assumption: PORT_READY interrupt will be received only for
1480 * ports implementing Channel Prepare state machine (CP_SM)
1483 if (status & SDW_DP0_INT_PORT_READY) {
1484 complete(&slave->port_ready[0]);
1485 clear |= SDW_DP0_INT_PORT_READY;
1488 if (status & SDW_DP0_INT_BRA_FAILURE) {
1489 dev_err(&slave->dev, "BRA failed\n");
1490 clear |= SDW_DP0_INT_BRA_FAILURE;
1493 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1494 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1496 if (status & impl_int_mask) {
1497 clear |= impl_int_mask;
1498 *slave_status = clear;
1501 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
1502 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
1504 dev_err(&slave->dev,
1505 "SDW_DP0_INT write failed:%d\n", ret);
1509 /* Read DP0 interrupt again */
1510 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
1512 dev_err(&slave->dev,
1513 "SDW_DP0_INT read failed:%d\n", status2);
1516 /* filter to limit loop to interrupts identified in the first status read */
1521 /* we can get alerts while processing so keep retrying */
1522 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1524 if (count == SDW_READ_INTR_CLEAR_RETRY)
1525 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
1530 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1531 int port, u8 *slave_status)
1533 u8 clear, impl_int_mask;
1534 int status, status2, ret, count = 0;
1538 return sdw_handle_dp0_interrupt(slave, slave_status);
1540 addr = SDW_DPN_INT(port);
1541 status = sdw_read_no_pm(slave, addr);
1543 dev_err(&slave->dev,
1544 "SDW_DPN_INT read failed:%d\n", status);
1550 clear = status & ~SDW_DPN_INTERRUPTS;
1552 if (status & SDW_DPN_INT_TEST_FAIL) {
1553 dev_err(&slave->dev, "Test fail for port:%d\n", port);
1554 clear |= SDW_DPN_INT_TEST_FAIL;
1558 * Assumption: PORT_READY interrupt will be received only
1559 * for ports implementing CP_SM.
1561 if (status & SDW_DPN_INT_PORT_READY) {
1562 complete(&slave->port_ready[port]);
1563 clear |= SDW_DPN_INT_PORT_READY;
1566 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1567 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1569 if (status & impl_int_mask) {
1570 clear |= impl_int_mask;
1571 *slave_status = clear;
1574 /* clear the interrupt but don't touch reserved fields */
1575 ret = sdw_write_no_pm(slave, addr, clear);
1577 dev_err(&slave->dev,
1578 "SDW_DPN_INT write failed:%d\n", ret);
1582 /* Read DPN interrupt again */
1583 status2 = sdw_read_no_pm(slave, addr);
1585 dev_err(&slave->dev,
1586 "SDW_DPN_INT read failed:%d\n", status2);
1589 /* filter to limit loop to interrupts identified in the first status read */
1594 /* we can get alerts while processing so keep retrying */
1595 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
1597 if (count == SDW_READ_INTR_CLEAR_RETRY)
1598 dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
1603 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1605 struct sdw_slave_intr_status slave_intr;
1606 u8 clear = 0, bit, port_status[15] = {0};
1607 int port_num, stat, ret, count = 0;
1610 u8 sdca_cascade = 0;
1615 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1617 ret = pm_runtime_get_sync(&slave->dev);
1618 if (ret < 0 && ret != -EACCES) {
1619 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1620 pm_runtime_put_noidle(&slave->dev);
1624 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1625 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1627 dev_err(&slave->dev,
1628 "SDW_SCP_INT1 read failed:%d\n", ret);
1633 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1635 dev_err(&slave->dev,
1636 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1640 if (slave->id.class_id) {
1641 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1643 dev_err(&slave->dev,
1644 "SDW_DP0_INT read failed:%d\n", ret);
1647 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1651 slave_notify = false;
1654 * Check parity, bus clash and Slave (impl defined)
1657 if (buf & SDW_SCP_INT1_PARITY) {
1658 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1659 parity_quirk = !slave->first_interrupt_done &&
1660 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1662 if (parity_check && !parity_quirk)
1663 dev_err(&slave->dev, "Parity error detected\n");
1664 clear |= SDW_SCP_INT1_PARITY;
1667 if (buf & SDW_SCP_INT1_BUS_CLASH) {
1668 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1669 dev_err(&slave->dev, "Bus clash detected\n");
1670 clear |= SDW_SCP_INT1_BUS_CLASH;
1674 * When bus clash or parity errors are detected, such errors
1675 * are unlikely to be recoverable errors.
1676 * TODO: In such scenario, reset bus. Make this configurable
1677 * via sysfs property with bus reset being the default.
1680 if (buf & SDW_SCP_INT1_IMPL_DEF) {
1681 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1682 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1683 slave_notify = true;
1685 clear |= SDW_SCP_INT1_IMPL_DEF;
1688 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1690 slave_notify = true;
1692 /* Check port 0 - 3 interrupts */
1693 port = buf & SDW_SCP_INT1_PORT0_3;
1695 /* To get port number corresponding to bits, shift it */
1696 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1697 for_each_set_bit(bit, &port, 8) {
1698 sdw_handle_port_interrupt(slave, bit,
1702 /* Check if cascade 2 interrupt is present */
1703 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1704 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1705 for_each_set_bit(bit, &port, 8) {
1706 /* scp2 ports start from 4 */
1708 sdw_handle_port_interrupt(slave,
1710 &port_status[port_num]);
1714 /* now check last cascade */
1715 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1716 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1717 for_each_set_bit(bit, &port, 8) {
1718 /* scp3 ports start from 11 */
1719 port_num = bit + 11;
1720 sdw_handle_port_interrupt(slave,
1722 &port_status[port_num]);
1726 /* Update the Slave driver */
1728 mutex_lock(&slave->sdw_dev_lock);
1730 if (slave->probed) {
1731 struct device *dev = &slave->dev;
1732 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1734 if (slave->prop.use_domain_irq && slave->irq)
1735 handle_nested_irq(slave->irq);
1737 if (drv->ops && drv->ops->interrupt_callback) {
1738 slave_intr.sdca_cascade = sdca_cascade;
1739 slave_intr.control_port = clear;
1740 memcpy(slave_intr.port, &port_status,
1741 sizeof(slave_intr.port));
1743 drv->ops->interrupt_callback(slave, &slave_intr);
1747 mutex_unlock(&slave->sdw_dev_lock);
1751 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
1753 dev_err(&slave->dev,
1754 "SDW_SCP_INT1 write failed:%d\n", ret);
1758 /* at this point all initial interrupt sources were handled */
1759 slave->first_interrupt_done = true;
1762 * Read status again to ensure no new interrupts arrived
1763 * while servicing interrupts.
1765 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
1767 dev_err(&slave->dev,
1768 "SDW_SCP_INT1 recheck read failed:%d\n", ret);
1773 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
1775 dev_err(&slave->dev,
1776 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
1780 if (slave->id.class_id) {
1781 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
1783 dev_err(&slave->dev,
1784 "SDW_DP0_INT recheck read failed:%d\n", ret);
1787 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1791 * Make sure no interrupts are pending
1793 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
1796 * Exit loop if Slave is continuously in ALERT state even
1797 * after servicing the interrupt multiple times.
1801 /* we can get alerts while processing so keep retrying */
1802 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1804 if (count == SDW_READ_INTR_CLEAR_RETRY)
1805 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
1808 pm_runtime_mark_last_busy(&slave->dev);
1809 pm_runtime_put_autosuspend(&slave->dev);
1814 static int sdw_update_slave_status(struct sdw_slave *slave,
1815 enum sdw_slave_status status)
1819 mutex_lock(&slave->sdw_dev_lock);
1821 if (slave->probed) {
1822 struct device *dev = &slave->dev;
1823 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
1825 if (drv->ops && drv->ops->update_status)
1826 ret = drv->ops->update_status(slave, status);
1829 mutex_unlock(&slave->sdw_dev_lock);
1835 * sdw_handle_slave_status() - Handle Slave status
1836 * @bus: SDW bus instance
1837 * @status: Status for all Slave(s)
1839 int sdw_handle_slave_status(struct sdw_bus *bus,
1840 enum sdw_slave_status status[])
1842 enum sdw_slave_status prev_status;
1843 struct sdw_slave *slave;
1844 bool attached_initializing, id_programmed;
1847 /* first check if any Slaves fell off the bus */
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 (status[i] == SDW_SLAVE_UNATTACHED &&
1861 slave->status != SDW_SLAVE_UNATTACHED) {
1862 dev_warn(&slave->dev, "Slave %d state check1: UNATTACHED, status was %d\n",
1864 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1866 /* Ensure driver knows that peripheral unattached */
1867 ret = sdw_update_slave_status(slave, status[i]);
1869 dev_warn(&slave->dev, "Update Slave status failed:%d\n", ret);
1873 if (status[0] == SDW_SLAVE_ATTACHED) {
1874 dev_dbg(bus->dev, "Slave attached, programming device number\n");
1877 * Programming a device number will have side effects,
1878 * so we deal with other devices at a later time.
1879 * This relies on those devices reporting ATTACHED, which will
1880 * trigger another call to this function. This will only
1881 * happen if at least one device ID was programmed.
1882 * Error returns from sdw_program_device_num() are currently
1883 * ignored because there's no useful recovery that can be done.
1884 * Returning the error here could result in the current status
1885 * of other devices not being handled, because if no device IDs
1886 * were programmed there's nothing to guarantee a status change
1887 * to trigger another call to this function.
1889 sdw_program_device_num(bus, &id_programmed);
1894 /* Continue to check other slave statuses */
1895 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1896 mutex_lock(&bus->bus_lock);
1897 if (test_bit(i, bus->assigned) == false) {
1898 mutex_unlock(&bus->bus_lock);
1901 mutex_unlock(&bus->bus_lock);
1903 slave = sdw_get_slave(bus, i);
1907 attached_initializing = false;
1909 switch (status[i]) {
1910 case SDW_SLAVE_UNATTACHED:
1911 if (slave->status == SDW_SLAVE_UNATTACHED)
1914 dev_warn(&slave->dev, "Slave %d state check2: UNATTACHED, status was %d\n",
1917 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1920 case SDW_SLAVE_ALERT:
1921 ret = sdw_handle_slave_alerts(slave);
1923 dev_err(&slave->dev,
1924 "Slave %d alert handling failed: %d\n",
1928 case SDW_SLAVE_ATTACHED:
1929 if (slave->status == SDW_SLAVE_ATTACHED)
1932 prev_status = slave->status;
1933 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1935 if (prev_status == SDW_SLAVE_ALERT)
1938 attached_initializing = true;
1940 ret = sdw_initialize_slave(slave);
1942 dev_err(&slave->dev,
1943 "Slave %d initialization failed: %d\n",
1949 dev_err(&slave->dev, "Invalid slave %d status:%d\n",
1954 ret = sdw_update_slave_status(slave, status[i]);
1956 dev_err(&slave->dev,
1957 "Update Slave status failed:%d\n", ret);
1958 if (attached_initializing) {
1959 dev_dbg(&slave->dev,
1960 "signaling initialization completion for Slave %d\n",
1963 complete_all(&slave->initialization_complete);
1966 * If the manager became pm_runtime active, the peripherals will be
1967 * restarted and attach, but their pm_runtime status may remain
1968 * suspended. If the 'update_slave_status' callback initiates
1969 * any sort of deferred processing, this processing would not be
1970 * cancelled on pm_runtime suspend.
1971 * To avoid such zombie states, we queue a request to resume.
1972 * This would be a no-op in case the peripheral was being resumed
1973 * by e.g. the ALSA/ASoC framework.
1975 pm_request_resume(&slave->dev);
1981 EXPORT_SYMBOL(sdw_handle_slave_status);
1983 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1985 struct sdw_slave *slave;
1988 /* Check all non-zero devices */
1989 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1990 mutex_lock(&bus->bus_lock);
1991 if (test_bit(i, bus->assigned) == false) {
1992 mutex_unlock(&bus->bus_lock);
1995 mutex_unlock(&bus->bus_lock);
1997 slave = sdw_get_slave(bus, i);
2001 if (slave->status != SDW_SLAVE_UNATTACHED) {
2002 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
2003 slave->first_interrupt_done = false;
2004 sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
2007 /* keep track of request, used in pm_runtime resume */
2008 slave->unattach_request = request;
2011 EXPORT_SYMBOL(sdw_clear_slave_status);