/* * Array of supported rows and columns as per MIPI SoundWire Specification 1.1 * * The rows are arranged as per the array index value programmed * in register. The index 15 has dummy value 0 in order to fill hole.
*/ int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
96, 100, 120, 128, 150, 160, 250, 0,
192, 200, 240, 256, 72, 144, 90, 180};
EXPORT_SYMBOL(sdw_rows);
/* Program DPN_OffsetCtrl2 registers */
ret = sdw_write_no_pm(slave, addr1, t_params->offset2); if (ret < 0) {
dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n"); return ret;
}
/* DP0 does not implement BlockCtrl3 */ if (t_params->port_num) { /* Program DPN_BlockCtrl3 register */
ret = sdw_write_no_pm(slave, addr2, t_params->blk_pkg_mode); if (ret < 0) {
dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n"); return ret;
}
}
/* * Data ports are FULL, SIMPLE and REDUCED. This function handles * FULL and REDUCED only and beyond this point only FULL is * handled, so bail out if we are not FULL data port type
*/ if (type != SDW_DPN_FULL) return ret;
ret = sdw_update_no_pm(s_rt->slave, addr1, 0xF, wbuf); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_PortCtrl register write failed for port %d\n",
t_params->port_num); return ret;
}
if (!read_only_wordlength) { /* Program DPN_BlockCtrl1 register */
ret = sdw_write_no_pm(s_rt->slave, addr2, (p_params->bps - 1)); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_BlockCtrl1 register write failed for port %d\n",
t_params->port_num); return ret;
}
}
/* Program DPN_SampleCtrl1 register */
wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
ret = sdw_write_no_pm(s_rt->slave, addr3, wbuf); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_SampleCtrl1 register write failed for port %d\n",
t_params->port_num); return ret;
}
/* Program DPN_OffsetCtrl1 registers */
ret = sdw_write_no_pm(s_rt->slave, addr4, t_params->offset1); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_OffsetCtrl1 register write failed for port %d\n",
t_params->port_num); return ret;
}
/* Program DPN_BlockCtrl2 register*/ if (t_params->blk_grp_ctrl_valid) {
ret = sdw_write_no_pm(s_rt->slave, addr5, t_params->blk_grp_ctrl); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_BlockCtrl2 reg write failed for port %d\n",
t_params->port_num); return ret;
}
}
/* program DPN_LaneCtrl register */ if (slave_prop->lane_control_support) {
ret = sdw_write_no_pm(s_rt->slave, addr6, t_params->lane_ctrl); if (ret < 0) {
dev_err(&s_rt->slave->dev, "DPN_LaneCtrl register write failed for port %d\n",
t_params->port_num); return ret;
}
}
if (port_type != SDW_DPN_SIMPLE) {
ret = _sdw_program_slave_port_params(bus, s_rt->slave,
t_params, port_type); if (ret < 0)
dev_err(&s_rt->slave->dev, "Transport reg write failed for port: %d\n",
t_params->port_num);
}
return ret;
}
staticint sdw_program_master_port_params(struct sdw_bus *bus, struct sdw_port_runtime *p_rt)
{ int ret;
/* * we need to set transport and port parameters for the port. * Transport parameters refers to the sample interval, offsets and * hstart/stop etc of the data. Port parameters refers to word * length, flow mode etc of the port
*/
ret = bus->port_ops->dpn_set_port_transport_params(bus,
&p_rt->transport_params,
bus->params.next_bank); if (ret < 0) return ret;
/** * sdw_program_port_params() - Programs transport parameters of Master(s) * and Slave(s) * * @m_rt: Master stream runtime
*/ staticint sdw_program_port_params(struct sdw_master_runtime *m_rt)
{ struct sdw_slave_runtime *s_rt; struct sdw_bus *bus = m_rt->bus; struct sdw_port_runtime *p_rt; int ret = 0;
/* Program transport & port parameters for Slave(s) */
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
ret = sdw_program_slave_port_params(bus, s_rt, p_rt); if (ret < 0) return ret;
}
}
/* Program transport & port parameters for Master(s) */
list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
ret = sdw_program_master_port_params(bus, p_rt); if (ret < 0) return ret;
}
return 0;
}
/** * sdw_enable_disable_slave_ports: Enable/disable slave data port * * @bus: bus instance * @s_rt: slave runtime * @p_rt: port runtime * @en: enable or disable operation * * This function only sets the enable/disable bits in the relevant bank, the * actual enable/disable is done with a bank switch
*/ staticint sdw_enable_disable_slave_ports(struct sdw_bus *bus, struct sdw_slave_runtime *s_rt, struct sdw_port_runtime *p_rt, bool en)
{ struct sdw_transport_params *t_params = &p_rt->transport_params;
u32 addr; int ret;
if (bus->params.next_bank)
addr = SDW_DPN_CHANNELEN_B1(p_rt->num); else
addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
/* * Since bus doesn't support sharing a port across two streams, * it is safe to reset this register
*/ if (en)
ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); else
ret = sdw_write_no_pm(s_rt->slave, addr, 0x0);
/* * Enable interrupt before Port prepare. * For Port de-prepare, it is assumed that port * was prepared earlier
*/ if (prep && intr) {
ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
imp_def_interrupts); if (ret < 0) return ret;
}
/* Inform slave about the impending port prepare */
sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_PRE_PREP : SDW_OPS_PORT_PRE_DEPREP);
/* Prepare Slave port implementing CP_SM */ if (!simple_ch_prep_sm) {
addr = SDW_DPN_PREPARECTRL(p_rt->num);
if (prep)
ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); else
ret = sdw_write_no_pm(s_rt->slave, addr, 0x0);
/* Wait for completion on port ready */
port_ready = &s_rt->slave->port_ready[prep_ch.num];
wait_for_completion_timeout(port_ready,
msecs_to_jiffies(ch_prep_timeout));
val = sdw_read_no_pm(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num)); if ((val < 0) || (val & p_rt->ch_mask)) {
ret = (val < 0) ? val : -ETIMEDOUT;
dev_err(&s_rt->slave->dev, "Chn prep failed for port %d: %d\n", prep_ch.num, ret); return ret;
}
}
/** * sdw_notify_config() - Notify bus configuration * * @m_rt: Master runtime handle * * This function notifies the Master(s) and Slave(s) of the * new bus configuration.
*/ staticint sdw_notify_config(struct sdw_master_runtime *m_rt)
{ struct sdw_slave_runtime *s_rt; struct sdw_bus *bus = m_rt->bus; struct sdw_slave *slave; int ret;
if (bus->ops->set_bus_conf) {
ret = bus->ops->set_bus_conf(bus, &bus->params); if (ret < 0) return ret;
}
if (drv->ops && drv->ops->bus_config) {
ret = drv->ops->bus_config(slave, &bus->params); if (ret < 0) {
dev_err(dev, "Notify Slave: %d failed\n",
slave->dev_num);
mutex_unlock(&slave->sdw_dev_lock); return ret;
}
}
}
mutex_unlock(&slave->sdw_dev_lock);
}
return 0;
}
/** * sdw_program_params() - Program transport and port parameters for Master(s) * and Slave(s) * * @bus: SDW bus instance * @prepare: true if sdw_program_params() is called by _prepare.
*/ staticint sdw_program_params(struct sdw_bus *bus, bool prepare)
{ struct sdw_master_runtime *m_rt; struct sdw_slave *slave; int ret = 0;
u32 addr1;
/* Check if all Peripherals comply with SDCA */
list_for_each_entry(slave, &bus->slaves, node) { if (!slave->dev_num_sticky) continue; if (!is_clock_scaling_supported_by_slave(slave)) {
dev_dbg(&slave->dev, "The Peripheral doesn't comply with SDCA\n"); goto manager_runtime;
}
}
if (bus->params.next_bank)
addr1 = SDW_SCP_BUSCLOCK_SCALE_B1; else
addr1 = SDW_SCP_BUSCLOCK_SCALE_B0;
/* Program SDW_SCP_BUSCLOCK_SCALE if all Peripherals comply with SDCA */
list_for_each_entry(slave, &bus->slaves, node) { int scale_index;
u8 base;
if (!slave->dev_num_sticky) continue;
scale_index = sdw_slave_get_scale_index(slave, &base); if (scale_index < 0) return scale_index;
ret = sdw_write_no_pm(slave, addr1, scale_index); if (ret < 0) {
dev_err(&slave->dev, "SDW_SCP_BUSCLOCK_SCALE register write failed\n"); return ret;
}
}
/* * this loop walks through all master runtimes for a * bus, but the ports can only be configured while * explicitly preparing a stream or handling an * already-prepared stream otherwise.
*/ if (!prepare &&
m_rt->stream->state == SDW_STREAM_CONFIGURED) continue;
ret = sdw_program_port_params(m_rt); if (ret < 0) {
dev_err(bus->dev, "Program transport params failed: %d\n", ret); return ret;
}
ret = sdw_notify_config(m_rt); if (ret < 0) {
dev_err(bus->dev, "Notify bus config failed: %d\n", ret); return ret;
}
/* Enable port(s) on alternate bank for all active streams */ if (m_rt->stream->state != SDW_STREAM_ENABLED) continue;
ret = sdw_enable_disable_ports(m_rt, true); if (ret < 0) {
dev_err(bus->dev, "Enable channel failed: %d\n", ret); return ret;
}
}
return ret;
}
staticint sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
{ int col_index, row_index; bool multi_link; struct sdw_msg *wr_msg;
u8 *wbuf; int ret;
u16 addr;
wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL); if (!wr_msg) return -ENOMEM;
wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL); if (!wbuf) {
ret = -ENOMEM; goto error_1;
}
/* Get row and column index to program register */
col_index = sdw_find_col_index(bus->params.col);
row_index = sdw_find_row_index(bus->params.row);
wbuf[0] = col_index | (row_index << 3);
if (bus->params.next_bank)
addr = SDW_SCP_FRAMECTRL_B1; else
addr = SDW_SCP_FRAMECTRL_B0;
/* * Set the multi_link flag only when both the hardware supports * and hardware-based sync is required
*/
multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links);
if (multi_link)
ret = sdw_transfer_defer(bus, wr_msg); else
ret = sdw_transfer(bus, wr_msg);
if (ret < 0 && ret != -ENODATA) {
dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); goto error;
}
/** * sdw_ml_sync_bank_switch: Multilink register bank switch * * @bus: SDW bus instance * @multi_link: whether this is a multi-link stream with hardware-based sync * * Caller function should free the buffers on error
*/ staticint sdw_ml_sync_bank_switch(struct sdw_bus *bus, bool multi_link)
{ unsignedlong time_left;
if (!multi_link) return 0;
/* Wait for completion of transfer */
time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
bus->bank_switch_timeout);
if (!time_left) {
dev_err(bus->dev, "Controller Timed out on bank switch\n"); return -ETIMEDOUT;
}
/* Pre-bank switch */ if (ops->pre_bank_switch) {
ret = ops->pre_bank_switch(bus); if (ret < 0) {
dev_err(bus->dev, "Pre bank switch op failed: %d\n", ret); goto msg_unlock;
}
}
/* * Perform Bank switch operation. * For multi link cases, the actual bank switch is * synchronized across all Masters and happens later as a * part of post_bank_switch ops.
*/
ret = sdw_bank_switch(bus, m_rt_count); if (ret < 0) {
dev_err(bus->dev, "Bank switch failed: %d\n", ret); goto error;
}
}
/* * For multi link cases, it is expected that the bank switch is * triggered by the post_bank_switch for the first Master in the list * and for the other Masters the post_bank_switch() should return doing * nothing.
*/
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus;
ops = bus->ops;
/* Post-bank switch */ if (ops->post_bank_switch) {
ret = ops->post_bank_switch(bus); if (ret < 0) {
dev_err(bus->dev, "Post bank switch op failed: %d\n",
ret); goto error;
}
} elseif (multi_link) {
dev_err(bus->dev, "Post bank switch ops not implemented\n");
ret = -EINVAL; goto error;
}
/* Set the bank switch timeout to default, if not set */ if (!bus->bank_switch_timeout)
bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
/* Check if bank switch was successful */
ret = sdw_ml_sync_bank_switch(bus, multi_link); if (ret < 0) {
dev_err(bus->dev, "multi link bank switch failed: %d\n", ret); goto error;
}
if (multi_link)
mutex_unlock(&bus->msg_lock);
}
return ret;
error:
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus; if (bus->defer_msg.msg) {
kfree(bus->defer_msg.msg->buf);
kfree(bus->defer_msg.msg);
bus->defer_msg.msg = NULL;
}
}
msg_unlock:
if (multi_link) {
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus; if (mutex_is_locked(&bus->msg_lock))
mutex_unlock(&bus->msg_lock);
}
}
/* Iterate for number of ports to perform initialization */ for (i = 0; i < num_config; i++) {
p_rt = sdw_port_alloc(&s_rt->port_list); if (!p_rt) return -ENOMEM;
}
return 0;
}
staticint sdw_slave_port_is_valid_range(struct device *dev, int num)
{ if (!SDW_VALID_PORT_RANGE(num)) {
dev_err(dev, "SoundWire: Invalid port number :%d\n", num); return -EINVAL;
}
return 0;
}
staticint sdw_slave_port_config(struct sdw_slave *slave, struct sdw_slave_runtime *s_rt, conststruct sdw_port_config *port_config, bool is_bpt_stream)
{ struct sdw_port_runtime *p_rt; int ret; int i;
i = 0;
list_for_each_entry(p_rt, &s_rt->port_list, port_node) { /* * TODO: Check valid port range as defined by DisCo/ * slave
*/ if (!is_bpt_stream) {
ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num); if (ret < 0) return ret;
} elseif (port_config[i].num) { return -EINVAL;
}
ret = sdw_port_config(p_rt, port_config, i); if (ret < 0) return ret;
i++;
}
/* Iterate for number of ports to perform initialization */ for (i = 0; i < num_ports; i++) {
p_rt = sdw_port_alloc(&m_rt->port_list); if (!p_rt) return -ENOMEM;
}
return 0;
}
staticint sdw_master_port_config(struct sdw_master_runtime *m_rt, conststruct sdw_port_config *port_config)
{ struct sdw_port_runtime *p_rt; int ret; int i;
i = 0;
list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
ret = sdw_port_config(p_rt, port_config, i); if (ret < 0) return ret;
i++;
}
return 0;
}
/** * sdw_slave_rt_alloc() - Allocate a Slave runtime handle. * * @slave: Slave handle * @m_rt: Master runtime handle * * This function is to be called with bus_lock held.
*/ staticstruct sdw_slave_runtime
*sdw_slave_rt_alloc(struct sdw_slave *slave, struct sdw_master_runtime *m_rt)
{ struct sdw_slave_runtime *s_rt;
s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL); if (!s_rt) return NULL;
m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL); if (!m_rt) return NULL;
/* Initialization of Master runtime handle */
INIT_LIST_HEAD(&m_rt->port_list);
INIT_LIST_HEAD(&m_rt->slave_rt_list);
/* * Add in order of bus id so that when taking the bus_lock * of multiple buses they will always be taken in the same * order to prevent a mutex deadlock.
*/
insert_after = &stream->master_list;
list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) { if (walk_m_rt->bus->id < bus->id) {
insert_after = &walk_m_rt->stream_node; break;
}
}
list_add(&m_rt->stream_node, insert_after);
list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
m_rt->bus = bus;
m_rt->stream = stream;
bus->stream_refcount++; if (stream->type == SDW_STREAM_BPT)
bus->bpt_stream_refcount++;
return m_rt;
}
/** * sdw_master_rt_config() - Configure Master runtime handle * * @m_rt: Master runtime handle * @stream_config: Stream configuration * * This function is to be called with bus_lock held.
*/
/** * sdw_master_rt_free() - Free Master runtime handle * * @m_rt: Master runtime node * @stream: Stream runtime handle. * * This function is to be called with bus_lock held * It frees the Master runtime handle and associated Slave(s) runtime * handle. If this is called first then sdw_slave_rt_free() will have * no effect as Slave(s) runtime handle would already be freed up.
*/ staticvoid sdw_master_rt_free(struct sdw_master_runtime *m_rt, struct sdw_stream_runtime *stream)
{ struct sdw_slave_runtime *s_rt, *_s_rt; struct sdw_bus *bus = m_rt->bus;
if (stream->type == SDW_STREAM_BPT)
bus->bpt_stream_refcount--;
bus->stream_refcount--;
}
/** * sdw_config_stream() - Configure the allocated stream * * @dev: SDW device * @stream: SoundWire stream * @stream_config: Stream configuration for audio stream * @is_slave: is API called from Slave or Master * * This function is to be called with bus_lock held.
*/ staticint sdw_config_stream(struct device *dev, struct sdw_stream_runtime *stream, struct sdw_stream_config *stream_config, bool is_slave)
{ /* * Update the stream rate, channel and bps based on data * source. For more than one data source (multilink), * match the rate, bps, stream type and increment number of channels. * * If rate/bps is zero, it means the values are not set, so skip * comparison and allow the value to be set and stored in stream
*/ if (stream->params.rate &&
stream->params.rate != stream_config->frame_rate) {
dev_err(dev, "rate not matching, stream:%s\n", stream->name); return -EINVAL;
}
if (stream->params.bps &&
stream->params.bps != stream_config->bps) {
dev_err(dev, "bps not matching, stream:%s\n", stream->name); return -EINVAL;
}
for (i = 0; i < num_ports; i++) { if (dpn_prop[i].num == port_num) return &dpn_prop[i];
}
return NULL;
}
/** * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s) * * @stream: SoundWire stream * * Acquire bus_lock for each of the master runtime(m_rt) part of this * stream to reconfigure the bus. * NOTE: This function is called from SoundWire stream ops and is * expected that a global lock is held before acquiring bus_lock.
*/ staticvoid sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
{ struct sdw_master_runtime *m_rt; struct sdw_bus *bus;
/* Iterate for all Master(s) in Master list */
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus;
mutex_lock(&bus->bus_lock);
}
}
/** * sdw_release_bus_lock: Release bus lock for all Master runtime(s) * * @stream: SoundWire stream * * Release the previously held bus_lock after reconfiguring the bus. * NOTE: This function is called from SoundWire stream ops and is * expected that a global lock is held before releasing bus_lock.
*/ staticvoid sdw_release_bus_lock(struct sdw_stream_runtime *stream)
{ struct sdw_master_runtime *m_rt; struct sdw_bus *bus;
/* Iterate for all Master(s) in Master list */
list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus;
mutex_unlock(&bus->bus_lock);
}
}
/* Prepare Master(s) and Slave(s) port(s) associated with stream */
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus;
prop = &bus->prop;
memcpy(¶ms, &bus->params, sizeof(params));
/* TODO: Support Asynchronous mode */ if ((prop->max_clk_freq % stream->params.rate) != 0) {
dev_err(bus->dev, "Async mode not supported\n"); return -EINVAL;
}
if (update_params) { /* Increment cumulative bus bandwidth */ /* TODO: Update this during Device-Device support */
bus->params.bandwidth += m_rt->stream->params.rate *
m_rt->ch_count * m_rt->stream->params.bps;
/* Compute params */ if (bus->compute_params) {
ret = bus->compute_params(bus, stream); if (ret < 0) {
dev_err(bus->dev, "Compute params failed: %d\n",
ret); goto restore_params;
}
}
}
/* Program params */
ret = sdw_program_params(bus, true); if (ret < 0) {
dev_err(bus->dev, "Program params failed: %d\n", ret); goto restore_params;
}
}
ret = do_bank_switch(stream); if (ret < 0) {
pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); goto restore_params;
}
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
bus = m_rt->bus;
/* Prepare port(s) on the new clock configuration */
ret = sdw_prep_deprep_ports(m_rt, true); if (ret < 0) {
dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
ret); goto restore_params;
}
}
/** * sdw_prepare_stream() - Prepare SoundWire stream * * @stream: Soundwire stream * * Documentation/driver-api/soundwire/stream.rst explains this API in detail
*/ int sdw_prepare_stream(struct sdw_stream_runtime *stream)
{ bool update_params = true; int ret;
if (!stream) {
pr_err("SoundWire: Handle not found for stream\n"); return -EINVAL;
}
sdw_acquire_bus_lock(stream);
if (stream->state == SDW_STREAM_PREPARED) {
ret = 0; goto state_err;
}
if (stream->state != SDW_STREAM_CONFIGURED &&
stream->state != SDW_STREAM_DEPREPARED &&
stream->state != SDW_STREAM_DISABLED) {
pr_err("%s: %s: inconsistent state state %d\n",
__func__, stream->name, stream->state);
ret = -EINVAL; goto state_err;
}
/* * when the stream is DISABLED, this means sdw_prepare_stream() * is called as a result of an underflow or a resume operation. * In this case, the bus parameters shall not be recomputed, but * still need to be re-applied
*/ if (stream->state == SDW_STREAM_DISABLED)
update_params = false;
/* Program params */
ret = sdw_program_params(bus, false); if (ret < 0) {
dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); return ret;
}
}
ret = do_bank_switch(stream); if (ret < 0) {
pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); return ret;
}
/* make sure alternate bank (previous current) is also disabled */
list_for_each_entry(m_rt, &stream->master_list, stream_node) { struct sdw_bus *bus = m_rt->bus;
/** * sdw_disable_stream() - Disable SoundWire stream * * @stream: Soundwire stream * * Documentation/driver-api/soundwire/stream.rst explains this API in detail
*/ int sdw_disable_stream(struct sdw_stream_runtime *stream)
{ int ret;
if (!stream) {
pr_err("SoundWire: Handle not found for stream\n"); return -EINVAL;
}
sdw_acquire_bus_lock(stream);
if (stream->state == SDW_STREAM_DISABLED) {
ret = 0; goto state_err;
}
if (stream->state != SDW_STREAM_ENABLED) {
pr_err("%s: %s: inconsistent state state %d\n",
__func__, stream->name, stream->state);
ret = -EINVAL; goto state_err;
}
staticint set_stream(struct snd_pcm_substream *substream, struct sdw_stream_runtime *sdw_stream)
{ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct snd_soc_dai *dai; int ret = 0; int i;
/* Set stream pointer on all DAIs */
for_each_rtd_dais(rtd, i, dai) {
ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream); if (ret < 0) {
dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name); break;
}
}
return ret;
}
/** * sdw_alloc_stream() - Allocate and return stream runtime * * @stream_name: SoundWire stream name * @type: stream type (could be PCM ,PDM or BPT) * * Allocates a SoundWire stream runtime instance. * sdw_alloc_stream should be called only once per stream. Typically * invoked from ALSA/ASoC machine/platform driver.
*/ struct sdw_stream_runtime *sdw_alloc_stream(constchar *stream_name, enum sdw_stream_type type)
{ struct sdw_stream_runtime *stream;
stream = kzalloc(sizeof(*stream), GFP_KERNEL); if (!stream) return NULL;
/** * sdw_startup_stream() - Startup SoundWire stream * * @sdw_substream: Soundwire stream * * Documentation/driver-api/soundwire/stream.rst explains this API in detail
*/ int sdw_startup_stream(void *sdw_substream)
{ struct snd_pcm_substream *substream = sdw_substream; struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct sdw_stream_runtime *sdw_stream; char *name; int ret;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); else
name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name);
if (!name) return -ENOMEM;
sdw_stream = sdw_alloc_stream(name, SDW_STREAM_PCM); if (!sdw_stream) {
dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name);
ret = -ENOMEM; goto error;
}
ret = set_stream(substream, sdw_stream); if (ret < 0) goto release_stream; return 0;
/* clear DAI data */
set_stream(substream, NULL);
}
EXPORT_SYMBOL(sdw_shutdown_stream);
/** * sdw_release_stream() - Free the assigned stream runtime * * @stream: SoundWire stream runtime * * sdw_release_stream should be called only once per stream
*/ void sdw_release_stream(struct sdw_stream_runtime *stream)
{
kfree(stream);
}
EXPORT_SYMBOL(sdw_release_stream);
/** * sdw_stream_add_master() - Allocate and add master runtime to a stream * * @bus: SDW Bus instance * @stream_config: Stream configuration for audio stream * @port_config: Port configuration for audio stream * @num_ports: Number of ports * @stream: SoundWire stream
*/ int sdw_stream_add_master(struct sdw_bus *bus, struct sdw_stream_config *stream_config, conststruct sdw_port_config *port_config, unsignedint num_ports, struct sdw_stream_runtime *stream)
{ struct sdw_master_runtime *m_rt; bool alloc_master_rt = false; int ret;
mutex_lock(&bus->bus_lock);
/* * For multi link streams, add the second master only if * the bus supports it. * Check if bus->multi_link is set
*/ if (!bus->multi_link && stream->m_rt_count > 0) {
dev_err(bus->dev, "Multilink not supported, link %d\n", bus->link_id);
ret = -EINVAL; goto unlock;
}
/* * check if Master is already allocated (e.g. as a result of Slave adding * it first), if so skip allocation and go to configuration
*/
m_rt = sdw_master_rt_find(bus, stream); if (!m_rt) {
m_rt = sdw_master_rt_alloc(bus, stream); if (IS_ERR(m_rt)) {
ret = PTR_ERR(m_rt);
dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s: %d\n",
__func__, stream->name, ret); goto unlock;
} if (!m_rt) {
dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s\n",
__func__, stream->name);
ret = -ENOMEM; goto unlock;
}
alloc_master_rt = true;
}
if (!sdw_master_port_allocated(m_rt)) {
ret = sdw_master_port_alloc(m_rt, num_ports); if (ret) goto alloc_error;
stream->m_rt_count++;
}
ret = sdw_master_rt_config(m_rt, stream_config); if (ret < 0) goto unlock;
ret = sdw_config_stream(bus->dev, stream, stream_config, false); if (ret) goto unlock;
ret = sdw_master_port_config(m_rt, port_config);
goto unlock;
alloc_error: /* * we only cleanup what was allocated in this routine
*/ if (alloc_master_rt)
sdw_master_rt_free(m_rt, stream);
unlock:
mutex_unlock(&bus->bus_lock); return ret;
}
EXPORT_SYMBOL(sdw_stream_add_master);
/** * sdw_stream_remove_master() - Remove master from sdw_stream * * @bus: SDW Bus instance * @stream: SoundWire stream * * This removes and frees port_rt and master_rt from a stream
*/ int sdw_stream_remove_master(struct sdw_bus *bus, struct sdw_stream_runtime *stream)
{ struct sdw_master_runtime *m_rt, *_m_rt;
mutex_lock(&bus->bus_lock);
list_for_each_entry_safe(m_rt, _m_rt,
&stream->master_list, stream_node) { if (m_rt->bus != bus) continue;
/** * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream * * @slave: SDW Slave instance * @stream_config: Stream configuration for audio stream * @stream: SoundWire stream * @port_config: Port configuration for audio stream * @num_ports: Number of ports * * It is expected that Slave is added before adding Master * to the Stream. *
*/ int sdw_stream_add_slave(struct sdw_slave *slave, struct sdw_stream_config *stream_config, conststruct sdw_port_config *port_config, unsignedint num_ports, struct sdw_stream_runtime *stream)
{ struct sdw_slave_runtime *s_rt; struct sdw_master_runtime *m_rt; bool alloc_master_rt = false; bool alloc_slave_rt = false;
int ret;
mutex_lock(&slave->bus->bus_lock);
/* * check if Master is already allocated, if so skip allocation * and go to configuration
*/
m_rt = sdw_master_rt_find(slave->bus, stream); if (!m_rt) { /* * If this API is invoked by Slave first then m_rt is not valid. * So, allocate m_rt and add Slave to it.
*/
m_rt = sdw_master_rt_alloc(slave->bus, stream); if (IS_ERR(m_rt)) {
ret = PTR_ERR(m_rt);
dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s: %d\n",
__func__, stream->name, ret); goto unlock;
} if (!m_rt) {
dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s\n",
__func__, stream->name);
ret = -ENOMEM; goto unlock;
}
alloc_master_rt = true;
}
s_rt = sdw_slave_rt_find(slave, stream); if (!s_rt) {
s_rt = sdw_slave_rt_alloc(slave, m_rt); if (!s_rt) {
dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n",
stream->name);
ret = -ENOMEM; goto alloc_error;
}
alloc_slave_rt = true;
}
if (!sdw_slave_port_allocated(s_rt)) {
ret = sdw_slave_port_alloc(slave, s_rt, num_ports); if (ret) goto alloc_error;
}
ret = sdw_master_rt_config(m_rt, stream_config); if (ret) goto unlock;
ret = sdw_slave_rt_config(s_rt, stream_config); if (ret) goto unlock;
ret = sdw_config_stream(&slave->dev, stream, stream_config, true); if (ret) goto unlock;
ret = sdw_slave_port_config(slave, s_rt, port_config,
stream->type == SDW_STREAM_BPT); if (ret) goto unlock;
/* * Change stream state to CONFIGURED on first Slave add. * Bus is not aware of number of Slave(s) in a stream at this * point so cannot depend on all Slave(s) to be added in order to * change stream state to CONFIGURED.
*/
stream->state = SDW_STREAM_CONFIGURED; goto unlock;
alloc_error: /* * we only cleanup what was allocated in this routine. The 'else if' * is intentional, the 'master_rt_free' will call sdw_slave_rt_free() * internally.
*/ if (alloc_master_rt)
sdw_master_rt_free(m_rt, stream); elseif (alloc_slave_rt)
sdw_slave_rt_free(slave, stream);
unlock:
mutex_unlock(&slave->bus->bus_lock); return ret;
}
EXPORT_SYMBOL(sdw_stream_add_slave);
/** * sdw_stream_remove_slave() - Remove slave from sdw_stream * * @slave: SDW Slave instance * @stream: SoundWire stream * * This removes and frees port_rt and slave_rt from a stream
*/ int sdw_stream_remove_slave(struct sdw_slave *slave, struct sdw_stream_runtime *stream)
{
mutex_lock(&slave->bus->bus_lock);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.