Lines Matching +full:stream +full:- +full:mode +full:- +full:support
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-18 Intel Corporation.
5 * stream.c - SoundWire Bus stream operations.
70 if (bus->params.next_bank) { in _sdw_program_slave_port_params()
71 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num); in _sdw_program_slave_port_params()
72 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num); in _sdw_program_slave_port_params()
73 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num); in _sdw_program_slave_port_params()
74 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num); in _sdw_program_slave_port_params()
76 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num); in _sdw_program_slave_port_params()
77 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num); in _sdw_program_slave_port_params()
78 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num); in _sdw_program_slave_port_params()
79 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num); in _sdw_program_slave_port_params()
83 ret = sdw_write(slave, addr1, t_params->offset2); in _sdw_program_slave_port_params()
85 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n"); in _sdw_program_slave_port_params()
90 ret = sdw_write(slave, addr2, t_params->blk_pkg_mode); in _sdw_program_slave_port_params()
92 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n"); in _sdw_program_slave_port_params()
105 wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1); in _sdw_program_slave_port_params()
109 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n"); in _sdw_program_slave_port_params()
114 wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart); in _sdw_program_slave_port_params()
115 wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop); in _sdw_program_slave_port_params()
119 dev_err(bus->dev, "DPN_HCtrl register write failed\n"); in _sdw_program_slave_port_params()
128 struct sdw_transport_params *t_params = &p_rt->transport_params; in sdw_program_slave_port_params()
129 struct sdw_port_params *p_params = &p_rt->port_params; in sdw_program_slave_port_params()
130 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop; in sdw_program_slave_port_params()
136 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, in sdw_program_slave_port_params()
137 s_rt->direction, in sdw_program_slave_port_params()
138 t_params->port_num); in sdw_program_slave_port_params()
140 return -EINVAL; in sdw_program_slave_port_params()
142 addr1 = SDW_DPN_PORTCTRL(t_params->port_num); in sdw_program_slave_port_params()
143 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num); in sdw_program_slave_port_params()
145 if (bus->params.next_bank) { in sdw_program_slave_port_params()
146 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num); in sdw_program_slave_port_params()
147 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num); in sdw_program_slave_port_params()
148 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num); in sdw_program_slave_port_params()
149 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num); in sdw_program_slave_port_params()
152 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num); in sdw_program_slave_port_params()
153 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num); in sdw_program_slave_port_params()
154 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num); in sdw_program_slave_port_params()
155 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num); in sdw_program_slave_port_params()
159 wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode); in sdw_program_slave_port_params()
160 wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode); in sdw_program_slave_port_params()
162 ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf); in sdw_program_slave_port_params()
164 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
166 t_params->port_num); in sdw_program_slave_port_params()
170 if (!dpn_prop->read_only_wordlength) { in sdw_program_slave_port_params()
172 ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1)); in sdw_program_slave_port_params()
174 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
176 t_params->port_num); in sdw_program_slave_port_params()
182 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW; in sdw_program_slave_port_params()
183 ret = sdw_write(s_rt->slave, addr3, wbuf); in sdw_program_slave_port_params()
185 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
187 t_params->port_num); in sdw_program_slave_port_params()
192 ret = sdw_write(s_rt->slave, addr4, t_params->offset1); in sdw_program_slave_port_params()
194 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
196 t_params->port_num); in sdw_program_slave_port_params()
201 if (t_params->blk_grp_ctrl_valid) { in sdw_program_slave_port_params()
202 ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl); in sdw_program_slave_port_params()
204 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
206 t_params->port_num); in sdw_program_slave_port_params()
212 if (slave_prop->lane_control_support) { in sdw_program_slave_port_params()
213 ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl); in sdw_program_slave_port_params()
215 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
217 t_params->port_num); in sdw_program_slave_port_params()
222 if (dpn_prop->type != SDW_DPN_SIMPLE) { in sdw_program_slave_port_params()
223 ret = _sdw_program_slave_port_params(bus, s_rt->slave, in sdw_program_slave_port_params()
224 t_params, dpn_prop->type); in sdw_program_slave_port_params()
226 dev_err(&s_rt->slave->dev, in sdw_program_slave_port_params()
228 t_params->port_num); in sdw_program_slave_port_params()
243 * length, flow mode etc of the port in sdw_program_master_port_params()
245 ret = bus->port_ops->dpn_set_port_transport_params(bus, in sdw_program_master_port_params()
246 &p_rt->transport_params, in sdw_program_master_port_params()
247 bus->params.next_bank); in sdw_program_master_port_params()
251 return bus->port_ops->dpn_set_port_params(bus, in sdw_program_master_port_params()
252 &p_rt->port_params, in sdw_program_master_port_params()
253 bus->params.next_bank); in sdw_program_master_port_params()
257 * sdw_program_port_params() - Programs transport parameters of Master(s)
260 * @m_rt: Master stream runtime
265 struct sdw_bus *bus = m_rt->bus; in sdw_program_port_params()
270 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_program_port_params()
271 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { in sdw_program_port_params()
279 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { in sdw_program_port_params()
304 struct sdw_transport_params *t_params = &p_rt->transport_params; in sdw_enable_disable_slave_ports()
308 if (bus->params.next_bank) in sdw_enable_disable_slave_ports()
309 addr = SDW_DPN_CHANNELEN_B1(p_rt->num); in sdw_enable_disable_slave_ports()
311 addr = SDW_DPN_CHANNELEN_B0(p_rt->num); in sdw_enable_disable_slave_ports()
314 * Since bus doesn't support sharing a port across two streams, in sdw_enable_disable_slave_ports()
318 ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask); in sdw_enable_disable_slave_ports()
320 ret = sdw_write(s_rt->slave, addr, 0x0); in sdw_enable_disable_slave_ports()
323 dev_err(&s_rt->slave->dev, in sdw_enable_disable_slave_ports()
325 ret, t_params->port_num); in sdw_enable_disable_slave_ports()
334 struct sdw_transport_params *t_params = &p_rt->transport_params; in sdw_enable_disable_master_ports()
335 struct sdw_bus *bus = m_rt->bus; in sdw_enable_disable_master_ports()
339 enable_ch.port_num = p_rt->num; in sdw_enable_disable_master_ports()
340 enable_ch.ch_mask = p_rt->ch_mask; in sdw_enable_disable_master_ports()
344 if (bus->port_ops->dpn_port_enable_ch) { in sdw_enable_disable_master_ports()
345 ret = bus->port_ops->dpn_port_enable_ch(bus, in sdw_enable_disable_master_ports()
347 bus->params.next_bank); in sdw_enable_disable_master_ports()
349 dev_err(bus->dev, in sdw_enable_disable_master_ports()
351 ret, t_params->port_num); in sdw_enable_disable_master_ports()
355 dev_err(bus->dev, in sdw_enable_disable_master_ports()
358 return -EINVAL; in sdw_enable_disable_master_ports()
365 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
368 * @m_rt: Master stream runtime
369 * @en: mode (enable/disable)
378 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_enable_disable_ports()
379 list_for_each_entry(s_port, &s_rt->port_list, port_node) { in sdw_enable_disable_ports()
380 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt, in sdw_enable_disable_ports()
388 list_for_each_entry(m_port, &m_rt->port_list, port_node) { in sdw_enable_disable_ports()
401 const struct sdw_slave_ops *ops = s_rt->slave->ops; in sdw_do_port_prep()
404 if (ops->port_prep) { in sdw_do_port_prep()
405 ret = ops->port_prep(s_rt->slave, &prep_ch, cmd); in sdw_do_port_prep()
407 dev_err(&s_rt->slave->dev, in sdw_do_port_prep()
429 prep_ch.num = p_rt->num; in sdw_prep_deprep_slave_ports()
430 prep_ch.ch_mask = p_rt->ch_mask; in sdw_prep_deprep_slave_ports()
432 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, in sdw_prep_deprep_slave_ports()
433 s_rt->direction, in sdw_prep_deprep_slave_ports()
436 dev_err(bus->dev, in sdw_prep_deprep_slave_ports()
438 return -EINVAL; in sdw_prep_deprep_slave_ports()
443 prep_ch.bank = bus->params.next_bank; in sdw_prep_deprep_slave_ports()
445 if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm || in sdw_prep_deprep_slave_ports()
446 bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) in sdw_prep_deprep_slave_ports()
451 * For Port de-prepare, it is assumed that port in sdw_prep_deprep_slave_ports()
455 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, in sdw_prep_deprep_slave_ports()
456 dpn_prop->imp_def_interrupts); in sdw_prep_deprep_slave_ports()
465 if (!dpn_prop->simple_ch_prep_sm) { in sdw_prep_deprep_slave_ports()
466 addr = SDW_DPN_PREPARECTRL(p_rt->num); in sdw_prep_deprep_slave_ports()
469 ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask); in sdw_prep_deprep_slave_ports()
471 ret = sdw_write(s_rt->slave, addr, 0x0); in sdw_prep_deprep_slave_ports()
474 dev_err(&s_rt->slave->dev, in sdw_prep_deprep_slave_ports()
480 port_ready = &s_rt->slave->port_ready[prep_ch.num]; in sdw_prep_deprep_slave_ports()
482 msecs_to_jiffies(dpn_prop->ch_prep_timeout)); in sdw_prep_deprep_slave_ports()
484 val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num)); in sdw_prep_deprep_slave_ports()
485 if ((val < 0) || (val & p_rt->ch_mask)) { in sdw_prep_deprep_slave_ports()
486 ret = (val < 0) ? val : -ETIMEDOUT; in sdw_prep_deprep_slave_ports()
487 dev_err(&s_rt->slave->dev, in sdw_prep_deprep_slave_ports()
496 /* Disable interrupt after Port de-prepare */ in sdw_prep_deprep_slave_ports()
498 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, in sdw_prep_deprep_slave_ports()
499 dpn_prop->imp_def_interrupts); in sdw_prep_deprep_slave_ports()
508 struct sdw_transport_params *t_params = &p_rt->transport_params; in sdw_prep_deprep_master_ports()
509 struct sdw_bus *bus = m_rt->bus; in sdw_prep_deprep_master_ports()
510 const struct sdw_master_port_ops *ops = bus->port_ops; in sdw_prep_deprep_master_ports()
514 prep_ch.num = p_rt->num; in sdw_prep_deprep_master_ports()
515 prep_ch.ch_mask = p_rt->ch_mask; in sdw_prep_deprep_master_ports()
516 prep_ch.prepare = prep; /* Prepare/De-prepare */ in sdw_prep_deprep_master_ports()
517 prep_ch.bank = bus->params.next_bank; in sdw_prep_deprep_master_ports()
519 /* Pre-prepare/Pre-deprepare port(s) */ in sdw_prep_deprep_master_ports()
520 if (ops->dpn_port_prep) { in sdw_prep_deprep_master_ports()
521 ret = ops->dpn_port_prep(bus, &prep_ch); in sdw_prep_deprep_master_ports()
523 dev_err(bus->dev, "Port prepare failed for port:%d\n", in sdw_prep_deprep_master_ports()
524 t_params->port_num); in sdw_prep_deprep_master_ports()
533 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
537 * @prep: Prepare or De-prepare
545 /* Prepare/De-prepare Slave port(s) */ in sdw_prep_deprep_ports()
546 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_prep_deprep_ports()
547 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { in sdw_prep_deprep_ports()
548 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt, in sdw_prep_deprep_ports()
555 /* Prepare/De-prepare Master port(s) */ in sdw_prep_deprep_ports()
556 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { in sdw_prep_deprep_ports()
566 * sdw_notify_config() - Notify bus configuration
576 struct sdw_bus *bus = m_rt->bus; in sdw_notify_config()
580 if (bus->ops->set_bus_conf) { in sdw_notify_config()
581 ret = bus->ops->set_bus_conf(bus, &bus->params); in sdw_notify_config()
586 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_notify_config()
587 slave = s_rt->slave; in sdw_notify_config()
589 if (slave->ops->bus_config) { in sdw_notify_config()
590 ret = slave->ops->bus_config(slave, &bus->params); in sdw_notify_config()
592 dev_err(bus->dev, "Notify Slave: %d failed\n", in sdw_notify_config()
593 slave->dev_num); in sdw_notify_config()
603 * sdw_program_params() - Program transport and port parameters for Master(s)
614 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { in sdw_program_params()
619 * explicitly preparing a stream or handling an in sdw_program_params()
620 * already-prepared stream otherwise. in sdw_program_params()
623 m_rt->stream->state == SDW_STREAM_CONFIGURED) in sdw_program_params()
628 dev_err(bus->dev, in sdw_program_params()
635 dev_err(bus->dev, in sdw_program_params()
641 if (m_rt->stream->state != SDW_STREAM_ENABLED) in sdw_program_params()
646 dev_err(bus->dev, "Enable channel failed: %d\n", ret); in sdw_program_params()
665 return -ENOMEM; in sdw_bank_switch()
667 bus->defer_msg.msg = wr_msg; in sdw_bank_switch()
671 ret = -ENOMEM; in sdw_bank_switch()
676 col_index = sdw_find_col_index(bus->params.col); in sdw_bank_switch()
677 row_index = sdw_find_row_index(bus->params.row); in sdw_bank_switch()
680 if (bus->params.next_bank) in sdw_bank_switch()
687 wr_msg->ssp_sync = true; in sdw_bank_switch()
691 * and hardware-based sync is required in sdw_bank_switch()
693 multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links); in sdw_bank_switch()
696 ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg); in sdw_bank_switch()
701 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); in sdw_bank_switch()
708 bus->defer_msg.msg = NULL; in sdw_bank_switch()
709 bus->params.curr_bank = !bus->params.curr_bank; in sdw_bank_switch()
710 bus->params.next_bank = !bus->params.next_bank; in sdw_bank_switch()
719 bus->defer_msg.msg = NULL; in sdw_bank_switch()
734 if (!bus->multi_link) in sdw_ml_sync_bank_switch()
738 time_left = wait_for_completion_timeout(&bus->defer_msg.complete, in sdw_ml_sync_bank_switch()
739 bus->bank_switch_timeout); in sdw_ml_sync_bank_switch()
742 dev_err(bus->dev, "Controller Timed out on bank switch\n"); in sdw_ml_sync_bank_switch()
743 return -ETIMEDOUT; in sdw_ml_sync_bank_switch()
746 bus->params.curr_bank = !bus->params.curr_bank; in sdw_ml_sync_bank_switch()
747 bus->params.next_bank = !bus->params.next_bank; in sdw_ml_sync_bank_switch()
749 if (bus->defer_msg.msg) { in sdw_ml_sync_bank_switch()
750 kfree(bus->defer_msg.msg->buf); in sdw_ml_sync_bank_switch()
751 kfree(bus->defer_msg.msg); in sdw_ml_sync_bank_switch()
757 static int do_bank_switch(struct sdw_stream_runtime *stream) in do_bank_switch() argument
766 m_rt_count = stream->m_rt_count; in do_bank_switch()
768 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in do_bank_switch()
769 bus = m_rt->bus; in do_bank_switch()
770 ops = bus->ops; in do_bank_switch()
772 if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) { in do_bank_switch()
774 mutex_lock(&bus->msg_lock); in do_bank_switch()
777 /* Pre-bank switch */ in do_bank_switch()
778 if (ops->pre_bank_switch) { in do_bank_switch()
779 ret = ops->pre_bank_switch(bus); in do_bank_switch()
781 dev_err(bus->dev, in do_bank_switch()
795 dev_err(bus->dev, "Bank switch failed: %d\n", ret); in do_bank_switch()
806 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in do_bank_switch()
807 bus = m_rt->bus; in do_bank_switch()
808 ops = bus->ops; in do_bank_switch()
810 /* Post-bank switch */ in do_bank_switch()
811 if (ops->post_bank_switch) { in do_bank_switch()
812 ret = ops->post_bank_switch(bus); in do_bank_switch()
814 dev_err(bus->dev, in do_bank_switch()
820 dev_err(bus->dev, in do_bank_switch()
826 if (!bus->bank_switch_timeout) in do_bank_switch()
827 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT; in do_bank_switch()
832 dev_err(bus->dev, in do_bank_switch()
838 mutex_unlock(&bus->msg_lock); in do_bank_switch()
844 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in do_bank_switch()
845 bus = m_rt->bus; in do_bank_switch()
846 if (bus->defer_msg.msg) { in do_bank_switch()
847 kfree(bus->defer_msg.msg->buf); in do_bank_switch()
848 kfree(bus->defer_msg.msg); in do_bank_switch()
855 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in do_bank_switch()
856 bus = m_rt->bus; in do_bank_switch()
857 if (mutex_is_locked(&bus->msg_lock)) in do_bank_switch()
858 mutex_unlock(&bus->msg_lock); in do_bank_switch()
866 * sdw_release_stream() - Free the assigned stream runtime
868 * @stream: SoundWire stream runtime
870 * sdw_release_stream should be called only once per stream
872 void sdw_release_stream(struct sdw_stream_runtime *stream) in sdw_release_stream() argument
874 kfree(stream); in sdw_release_stream()
879 * sdw_alloc_stream() - Allocate and return stream runtime
881 * @stream_name: SoundWire stream name
883 * Allocates a SoundWire stream runtime instance.
884 * sdw_alloc_stream should be called only once per stream. Typically
889 struct sdw_stream_runtime *stream; in sdw_alloc_stream() local
891 stream = kzalloc(sizeof(*stream), GFP_KERNEL); in sdw_alloc_stream()
892 if (!stream) in sdw_alloc_stream()
895 stream->name = stream_name; in sdw_alloc_stream()
896 INIT_LIST_HEAD(&stream->master_list); in sdw_alloc_stream()
897 stream->state = SDW_STREAM_ALLOCATED; in sdw_alloc_stream()
898 stream->m_rt_count = 0; in sdw_alloc_stream()
900 return stream; in sdw_alloc_stream()
906 struct sdw_stream_runtime *stream) in sdw_find_master_rt() argument
911 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in sdw_find_master_rt()
912 if (m_rt->bus == bus) in sdw_find_master_rt()
920 * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
923 * @stream_config: Stream configuration
924 * @stream: Stream runtime handle.
931 struct sdw_stream_runtime *stream) in sdw_alloc_master_rt() argument
939 m_rt = sdw_find_master_rt(bus, stream); in sdw_alloc_master_rt()
948 INIT_LIST_HEAD(&m_rt->port_list); in sdw_alloc_master_rt()
949 INIT_LIST_HEAD(&m_rt->slave_rt_list); in sdw_alloc_master_rt()
950 list_add_tail(&m_rt->stream_node, &stream->master_list); in sdw_alloc_master_rt()
952 list_add_tail(&m_rt->bus_node, &bus->m_rt_list); in sdw_alloc_master_rt()
955 m_rt->ch_count = stream_config->ch_count; in sdw_alloc_master_rt()
956 m_rt->bus = bus; in sdw_alloc_master_rt()
957 m_rt->stream = stream; in sdw_alloc_master_rt()
958 m_rt->direction = stream_config->direction; in sdw_alloc_master_rt()
964 * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
967 * @stream_config: Stream configuration
968 * @stream: Stream runtime handle
975 struct sdw_stream_runtime *stream) in sdw_alloc_slave_rt() argument
983 INIT_LIST_HEAD(&s_rt->port_list); in sdw_alloc_slave_rt()
984 s_rt->ch_count = stream_config->ch_count; in sdw_alloc_slave_rt()
985 s_rt->direction = stream_config->direction; in sdw_alloc_slave_rt()
986 s_rt->slave = slave; in sdw_alloc_slave_rt()
996 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) { in sdw_master_port_release()
997 list_del(&p_rt->port_node); in sdw_master_port_release()
1004 struct sdw_stream_runtime *stream) in sdw_slave_port_release() argument
1010 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in sdw_slave_port_release()
1011 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_slave_port_release()
1012 if (s_rt->slave != slave) in sdw_slave_port_release()
1016 &s_rt->port_list, port_node) { in sdw_slave_port_release()
1017 list_del(&p_rt->port_node); in sdw_slave_port_release()
1025 * sdw_release_slave_stream() - Free Slave(s) runtime handle
1028 * @stream: Stream runtime handle.
1033 struct sdw_stream_runtime *stream) in sdw_release_slave_stream() argument
1038 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in sdw_release_slave_stream()
1041 &m_rt->slave_rt_list, m_rt_node) { in sdw_release_slave_stream()
1042 if (s_rt->slave == slave) { in sdw_release_slave_stream()
1043 list_del(&s_rt->m_rt_node); in sdw_release_slave_stream()
1052 * sdw_release_master_stream() - Free Master runtime handle
1055 * @stream: Stream runtime handle.
1063 struct sdw_stream_runtime *stream) in sdw_release_master_stream() argument
1067 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) { in sdw_release_master_stream()
1068 sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream); in sdw_release_master_stream()
1069 sdw_release_slave_stream(s_rt->slave, stream); in sdw_release_master_stream()
1072 list_del(&m_rt->stream_node); in sdw_release_master_stream()
1073 list_del(&m_rt->bus_node); in sdw_release_master_stream()
1078 * sdw_stream_remove_master() - Remove master from sdw_stream
1081 * @stream: SoundWire stream
1083 * This removes and frees port_rt and master_rt from a stream
1086 struct sdw_stream_runtime *stream) in sdw_stream_remove_master() argument
1090 mutex_lock(&bus->bus_lock); in sdw_stream_remove_master()
1093 &stream->master_list, stream_node) { in sdw_stream_remove_master()
1094 if (m_rt->bus != bus) in sdw_stream_remove_master()
1098 sdw_release_master_stream(m_rt, stream); in sdw_stream_remove_master()
1099 stream->m_rt_count--; in sdw_stream_remove_master()
1102 if (list_empty(&stream->master_list)) in sdw_stream_remove_master()
1103 stream->state = SDW_STREAM_RELEASED; in sdw_stream_remove_master()
1105 mutex_unlock(&bus->bus_lock); in sdw_stream_remove_master()
1112 * sdw_stream_remove_slave() - Remove slave from sdw_stream
1115 * @stream: SoundWire stream
1117 * This removes and frees port_rt and slave_rt from a stream
1120 struct sdw_stream_runtime *stream) in sdw_stream_remove_slave() argument
1122 mutex_lock(&slave->bus->bus_lock); in sdw_stream_remove_slave()
1124 sdw_slave_port_release(slave->bus, slave, stream); in sdw_stream_remove_slave()
1125 sdw_release_slave_stream(slave, stream); in sdw_stream_remove_slave()
1127 mutex_unlock(&slave->bus->bus_lock); in sdw_stream_remove_slave()
1134 * sdw_config_stream() - Configure the allocated stream
1137 * @stream: SoundWire stream
1138 * @stream_config: Stream configuration for audio stream
1144 struct sdw_stream_runtime *stream, in sdw_config_stream() argument
1149 * Update the stream rate, channel and bps based on data in sdw_config_stream()
1151 * match the rate, bps, stream type and increment number of channels. in sdw_config_stream()
1154 * comparison and allow the value to be set and stored in stream in sdw_config_stream()
1156 if (stream->params.rate && in sdw_config_stream()
1157 stream->params.rate != stream_config->frame_rate) { in sdw_config_stream()
1158 dev_err(dev, "rate not matching, stream:%s\n", stream->name); in sdw_config_stream()
1159 return -EINVAL; in sdw_config_stream()
1162 if (stream->params.bps && in sdw_config_stream()
1163 stream->params.bps != stream_config->bps) { in sdw_config_stream()
1164 dev_err(dev, "bps not matching, stream:%s\n", stream->name); in sdw_config_stream()
1165 return -EINVAL; in sdw_config_stream()
1168 stream->type = stream_config->type; in sdw_config_stream()
1169 stream->params.rate = stream_config->frame_rate; in sdw_config_stream()
1170 stream->params.bps = stream_config->bps; in sdw_config_stream()
1172 /* TODO: Update this check during Device-device support */ in sdw_config_stream()
1174 stream->params.ch_count += stream_config->ch_count; in sdw_config_stream()
1182 if (!SDW_VALID_PORT_RANGE(p_rt->num)) { in sdw_is_valid_port_range()
1184 "SoundWire: Invalid port number :%d\n", p_rt->num); in sdw_is_valid_port_range()
1185 return -EINVAL; in sdw_is_valid_port_range()
1202 p_rt->ch_mask = port_config[port_index].ch_mask; in sdw_port_alloc()
1203 p_rt->num = port_config[port_index].num; in sdw_port_alloc()
1218 p_rt = sdw_port_alloc(bus->dev, port_config, i); in sdw_master_port_config()
1220 return -ENOMEM; in sdw_master_port_config()
1224 * configuration (audio mode support) in sdw_master_port_config()
1227 list_add_tail(&p_rt->port_node, &m_rt->port_list); in sdw_master_port_config()
1243 p_rt = sdw_port_alloc(&slave->dev, port_config, i); in sdw_slave_port_config()
1245 return -ENOMEM; in sdw_slave_port_config()
1251 ret = sdw_is_valid_port_range(&slave->dev, p_rt); in sdw_slave_port_config()
1259 * configuration (audio mode support) in sdw_slave_port_config()
1262 list_add_tail(&p_rt->port_node, &s_rt->port_list); in sdw_slave_port_config()
1269 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1272 * @stream_config: Stream configuration for audio stream
1273 * @port_config: Port configuration for audio stream
1275 * @stream: SoundWire stream
1281 struct sdw_stream_runtime *stream) in sdw_stream_add_master() argument
1286 mutex_lock(&bus->bus_lock); in sdw_stream_add_master()
1291 * Check if bus->multi_link is set in sdw_stream_add_master()
1293 if (!bus->multi_link && stream->m_rt_count > 0) { in sdw_stream_add_master()
1294 dev_err(bus->dev, in sdw_stream_add_master()
1295 "Multilink not supported, link %d\n", bus->link_id); in sdw_stream_add_master()
1296 ret = -EINVAL; in sdw_stream_add_master()
1300 m_rt = sdw_alloc_master_rt(bus, stream_config, stream); in sdw_stream_add_master()
1302 dev_err(bus->dev, in sdw_stream_add_master()
1303 "Master runtime config failed for stream:%s\n", in sdw_stream_add_master()
1304 stream->name); in sdw_stream_add_master()
1305 ret = -ENOMEM; in sdw_stream_add_master()
1309 ret = sdw_config_stream(bus->dev, stream, stream_config, false); in sdw_stream_add_master()
1317 stream->m_rt_count++; in sdw_stream_add_master()
1322 sdw_release_master_stream(m_rt, stream); in sdw_stream_add_master()
1324 mutex_unlock(&bus->bus_lock); in sdw_stream_add_master()
1330 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1333 * @stream_config: Stream configuration for audio stream
1334 * @stream: SoundWire stream
1335 * @port_config: Port configuration for audio stream
1339 * to the Stream.
1346 struct sdw_stream_runtime *stream) in sdw_stream_add_slave() argument
1352 mutex_lock(&slave->bus->bus_lock); in sdw_stream_add_slave()
1358 m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream); in sdw_stream_add_slave()
1360 dev_err(&slave->dev, in sdw_stream_add_slave()
1361 "alloc master runtime failed for stream:%s\n", in sdw_stream_add_slave()
1362 stream->name); in sdw_stream_add_slave()
1363 ret = -ENOMEM; in sdw_stream_add_slave()
1367 s_rt = sdw_alloc_slave_rt(slave, stream_config, stream); in sdw_stream_add_slave()
1369 dev_err(&slave->dev, in sdw_stream_add_slave()
1370 "Slave runtime config failed for stream:%s\n", in sdw_stream_add_slave()
1371 stream->name); in sdw_stream_add_slave()
1372 ret = -ENOMEM; in sdw_stream_add_slave()
1376 ret = sdw_config_stream(&slave->dev, stream, stream_config, true); in sdw_stream_add_slave()
1388 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list); in sdw_stream_add_slave()
1395 * Change stream state to CONFIGURED on first Slave add. in sdw_stream_add_slave()
1396 * Bus is not aware of number of Slave(s) in a stream at this in sdw_stream_add_slave()
1398 * change stream state to CONFIGURED. in sdw_stream_add_slave()
1400 stream->state = SDW_STREAM_CONFIGURED; in sdw_stream_add_slave()
1405 * we hit error so cleanup the stream, release all Slave(s) and in sdw_stream_add_slave()
1408 sdw_release_master_stream(m_rt, stream); in sdw_stream_add_slave()
1410 mutex_unlock(&slave->bus->bus_lock); in sdw_stream_add_slave()
1416 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1431 num_ports = hweight32(slave->prop.source_ports); in sdw_get_slave_dpn_prop()
1432 dpn_prop = slave->prop.src_dpn_prop; in sdw_get_slave_dpn_prop()
1434 num_ports = hweight32(slave->prop.sink_ports); in sdw_get_slave_dpn_prop()
1435 dpn_prop = slave->prop.sink_dpn_prop; in sdw_get_slave_dpn_prop()
1449 * @stream: SoundWire stream
1452 * stream to reconfigure the bus.
1453 * NOTE: This function is called from SoundWire stream ops and is
1456 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream) in sdw_acquire_bus_lock() argument
1462 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in sdw_acquire_bus_lock()
1463 bus = m_rt->bus; in sdw_acquire_bus_lock()
1465 mutex_lock(&bus->bus_lock); in sdw_acquire_bus_lock()
1472 * @stream: SoundWire stream
1475 * NOTE: This function is called from SoundWire stream ops and is
1478 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) in sdw_release_bus_lock() argument
1484 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) { in sdw_release_bus_lock()
1485 bus = m_rt->bus; in sdw_release_bus_lock()
1486 mutex_unlock(&bus->bus_lock); in sdw_release_bus_lock()
1490 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream, in _sdw_prepare_stream() argument
1499 /* Prepare Master(s) and Slave(s) port(s) associated with stream */ in _sdw_prepare_stream()
1500 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_prepare_stream()
1501 bus = m_rt->bus; in _sdw_prepare_stream()
1502 prop = &bus->prop; in _sdw_prepare_stream()
1503 memcpy(¶ms, &bus->params, sizeof(params)); in _sdw_prepare_stream()
1505 /* TODO: Support Asynchronous mode */ in _sdw_prepare_stream()
1506 if ((prop->max_clk_freq % stream->params.rate) != 0) { in _sdw_prepare_stream()
1507 dev_err(bus->dev, "Async mode not supported\n"); in _sdw_prepare_stream()
1508 return -EINVAL; in _sdw_prepare_stream()
1515 /* TODO: Update this during Device-Device support */ in _sdw_prepare_stream()
1516 bus->params.bandwidth += m_rt->stream->params.rate * in _sdw_prepare_stream()
1517 m_rt->ch_count * m_rt->stream->params.bps; in _sdw_prepare_stream()
1520 if (bus->compute_params) { in _sdw_prepare_stream()
1521 ret = bus->compute_params(bus); in _sdw_prepare_stream()
1523 dev_err(bus->dev, "Compute params failed: %d", in _sdw_prepare_stream()
1533 dev_err(bus->dev, "Program params failed: %d\n", ret); in _sdw_prepare_stream()
1540 return -EINVAL; in _sdw_prepare_stream()
1543 ret = do_bank_switch(stream); in _sdw_prepare_stream()
1545 dev_err(bus->dev, "Bank switch failed: %d\n", ret); in _sdw_prepare_stream()
1549 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_prepare_stream()
1550 bus = m_rt->bus; in _sdw_prepare_stream()
1555 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n", in _sdw_prepare_stream()
1561 stream->state = SDW_STREAM_PREPARED; in _sdw_prepare_stream()
1566 memcpy(&bus->params, ¶ms, sizeof(params)); in _sdw_prepare_stream()
1571 * sdw_prepare_stream() - Prepare SoundWire stream
1573 * @stream: Soundwire stream
1575 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1577 int sdw_prepare_stream(struct sdw_stream_runtime *stream) in sdw_prepare_stream() argument
1582 if (!stream) { in sdw_prepare_stream()
1583 pr_err("SoundWire: Handle not found for stream\n"); in sdw_prepare_stream()
1584 return -EINVAL; in sdw_prepare_stream()
1587 sdw_acquire_bus_lock(stream); in sdw_prepare_stream()
1589 if (stream->state == SDW_STREAM_PREPARED) { in sdw_prepare_stream()
1594 if (stream->state != SDW_STREAM_CONFIGURED && in sdw_prepare_stream()
1595 stream->state != SDW_STREAM_DEPREPARED && in sdw_prepare_stream()
1596 stream->state != SDW_STREAM_DISABLED) { in sdw_prepare_stream()
1598 __func__, stream->name, stream->state); in sdw_prepare_stream()
1599 ret = -EINVAL; in sdw_prepare_stream()
1604 * when the stream is DISABLED, this means sdw_prepare_stream() in sdw_prepare_stream()
1607 * still need to be re-applied in sdw_prepare_stream()
1609 if (stream->state == SDW_STREAM_DISABLED) in sdw_prepare_stream()
1612 ret = _sdw_prepare_stream(stream, update_params); in sdw_prepare_stream()
1615 sdw_release_bus_lock(stream); in sdw_prepare_stream()
1620 static int _sdw_enable_stream(struct sdw_stream_runtime *stream) in _sdw_enable_stream() argument
1626 /* Enable Master(s) and Slave(s) port(s) associated with stream */ in _sdw_enable_stream()
1627 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_enable_stream()
1628 bus = m_rt->bus; in _sdw_enable_stream()
1633 dev_err(bus->dev, "Program params failed: %d\n", ret); in _sdw_enable_stream()
1640 dev_err(bus->dev, in _sdw_enable_stream()
1648 return -EINVAL; in _sdw_enable_stream()
1651 ret = do_bank_switch(stream); in _sdw_enable_stream()
1653 dev_err(bus->dev, "Bank switch failed: %d\n", ret); in _sdw_enable_stream()
1657 stream->state = SDW_STREAM_ENABLED; in _sdw_enable_stream()
1662 * sdw_enable_stream() - Enable SoundWire stream
1664 * @stream: Soundwire stream
1666 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1668 int sdw_enable_stream(struct sdw_stream_runtime *stream) in sdw_enable_stream() argument
1672 if (!stream) { in sdw_enable_stream()
1673 pr_err("SoundWire: Handle not found for stream\n"); in sdw_enable_stream()
1674 return -EINVAL; in sdw_enable_stream()
1677 sdw_acquire_bus_lock(stream); in sdw_enable_stream()
1679 if (stream->state != SDW_STREAM_PREPARED && in sdw_enable_stream()
1680 stream->state != SDW_STREAM_DISABLED) { in sdw_enable_stream()
1682 __func__, stream->name, stream->state); in sdw_enable_stream()
1683 ret = -EINVAL; in sdw_enable_stream()
1687 ret = _sdw_enable_stream(stream); in sdw_enable_stream()
1690 sdw_release_bus_lock(stream); in sdw_enable_stream()
1695 static int _sdw_disable_stream(struct sdw_stream_runtime *stream) in _sdw_disable_stream() argument
1700 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_disable_stream()
1701 struct sdw_bus *bus = m_rt->bus; in _sdw_disable_stream()
1706 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); in _sdw_disable_stream()
1710 stream->state = SDW_STREAM_DISABLED; in _sdw_disable_stream()
1712 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_disable_stream()
1713 struct sdw_bus *bus = m_rt->bus; in _sdw_disable_stream()
1718 dev_err(bus->dev, "Program params failed: %d\n", ret); in _sdw_disable_stream()
1723 ret = do_bank_switch(stream); in _sdw_disable_stream()
1730 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_disable_stream()
1731 struct sdw_bus *bus = m_rt->bus; in _sdw_disable_stream()
1736 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); in _sdw_disable_stream()
1745 * sdw_disable_stream() - Disable SoundWire stream
1747 * @stream: Soundwire stream
1749 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1751 int sdw_disable_stream(struct sdw_stream_runtime *stream) in sdw_disable_stream() argument
1755 if (!stream) { in sdw_disable_stream()
1756 pr_err("SoundWire: Handle not found for stream\n"); in sdw_disable_stream()
1757 return -EINVAL; in sdw_disable_stream()
1760 sdw_acquire_bus_lock(stream); in sdw_disable_stream()
1762 if (stream->state != SDW_STREAM_ENABLED) { in sdw_disable_stream()
1764 __func__, stream->name, stream->state); in sdw_disable_stream()
1765 ret = -EINVAL; in sdw_disable_stream()
1769 ret = _sdw_disable_stream(stream); in sdw_disable_stream()
1772 sdw_release_bus_lock(stream); in sdw_disable_stream()
1777 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream) in _sdw_deprepare_stream() argument
1783 list_for_each_entry(m_rt, &stream->master_list, stream_node) { in _sdw_deprepare_stream()
1784 bus = m_rt->bus; in _sdw_deprepare_stream()
1785 /* De-prepare port(s) */ in _sdw_deprepare_stream()
1788 dev_err(bus->dev, in _sdw_deprepare_stream()
1789 "De-prepare port(s) failed: %d\n", ret); in _sdw_deprepare_stream()
1793 /* TODO: Update this during Device-Device support */ in _sdw_deprepare_stream()
1794 bus->params.bandwidth -= m_rt->stream->params.rate * in _sdw_deprepare_stream()
1795 m_rt->ch_count * m_rt->stream->params.bps; in _sdw_deprepare_stream()
1798 if (bus->compute_params) { in _sdw_deprepare_stream()
1799 ret = bus->compute_params(bus); in _sdw_deprepare_stream()
1801 dev_err(bus->dev, "Compute params failed: %d", in _sdw_deprepare_stream()
1810 dev_err(bus->dev, "Program params failed: %d\n", ret); in _sdw_deprepare_stream()
1815 stream->state = SDW_STREAM_DEPREPARED; in _sdw_deprepare_stream()
1816 return do_bank_switch(stream); in _sdw_deprepare_stream()
1820 * sdw_deprepare_stream() - Deprepare SoundWire stream
1822 * @stream: Soundwire stream
1824 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1826 int sdw_deprepare_stream(struct sdw_stream_runtime *stream) in sdw_deprepare_stream() argument
1830 if (!stream) { in sdw_deprepare_stream()
1831 pr_err("SoundWire: Handle not found for stream\n"); in sdw_deprepare_stream()
1832 return -EINVAL; in sdw_deprepare_stream()
1835 sdw_acquire_bus_lock(stream); in sdw_deprepare_stream()
1837 if (stream->state != SDW_STREAM_PREPARED && in sdw_deprepare_stream()
1838 stream->state != SDW_STREAM_DISABLED) { in sdw_deprepare_stream()
1840 __func__, stream->name, stream->state); in sdw_deprepare_stream()
1841 ret = -EINVAL; in sdw_deprepare_stream()
1845 ret = _sdw_deprepare_stream(stream); in sdw_deprepare_stream()
1848 sdw_release_bus_lock(stream); in sdw_deprepare_stream()
1856 struct snd_soc_pcm_runtime *rtd = substream->private_data; in set_stream()
1861 /* Set stream pointer on all DAIs */ in set_stream()
1863 ret = snd_soc_dai_set_sdw_stream(dai, sdw_stream, substream->stream); in set_stream()
1865 dev_err(rtd->dev, "failed to set stream pointer on dai %s", dai->name); in set_stream()
1874 * sdw_startup_stream() - Startup SoundWire stream
1876 * @sdw_substream: Soundwire stream
1878 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1883 struct snd_soc_pcm_runtime *rtd = substream->private_data; in sdw_startup_stream()
1888 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) in sdw_startup_stream()
1889 name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); in sdw_startup_stream()
1891 name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name); in sdw_startup_stream()
1894 return -ENOMEM; in sdw_startup_stream()
1898 dev_err(rtd->dev, "alloc stream failed for substream DAI %s", substream->name); in sdw_startup_stream()
1899 ret = -ENOMEM; in sdw_startup_stream()
1918 * sdw_shutdown_stream() - Shutdown SoundWire stream
1920 * @sdw_substream: Soundwire stream
1922 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1927 struct snd_soc_pcm_runtime *rtd = substream->private_data; in sdw_shutdown_stream()
1931 /* Find stream from first CPU DAI */ in sdw_shutdown_stream()
1934 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream); in sdw_shutdown_stream()
1937 dev_err(rtd->dev, "no stream found for DAI %s", dai->name); in sdw_shutdown_stream()
1942 kfree(sdw_stream->name); in sdw_shutdown_stream()