| 1 |
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 |
// Copyright(c) 2015-18 Intel Corporation. |
| 3 |
|
| 4 |
/* |
| 5 |
* stream.c - SoundWire Bus stream operations. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/delay.h> |
| 9 |
#include <linux/device.h> |
| 10 |
#include <linux/init.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/mod_devicetable.h> |
| 13 |
#include <linux/slab.h> |
| 14 |
#include <linux/soundwire/sdw_registers.h> |
| 15 |
#include <linux/soundwire/sdw.h> |
| 16 |
#include <linux/soundwire/sdw_type.h> |
| 17 |
#include <linux/string_choices.h> |
| 18 |
#include <sound/soc.h> |
| 19 |
#include "bus.h" |
| 20 |
|
| 21 |
/* |
| 22 |
* Array of supported rows and columns as per MIPI SoundWire Specification 1.1 |
| 23 |
* |
| 24 |
* The rows are arranged as per the array index value programmed |
| 25 |
* in register. The index 15 has dummy value 0 in order to fill hole. |
| 26 |
*/ |
| 27 |
int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147, |
| 28 |
96, 100, 120, 128, 150, 160, 250, 0, |
| 29 |
192, 200, 240, 256, 72, 144, 90, 180}; |
| 30 |
EXPORT_SYMBOL(sdw_rows); |
| 31 |
|
| 32 |
int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16}; |
| 33 |
EXPORT_SYMBOL(sdw_cols); |
| 34 |
|
| 35 |
int sdw_find_col_index(int col) |
| 36 |
{ |
| 37 |
int i; |
| 38 |
|
| 39 |
for (i = 0; i < SDW_FRAME_COLS; i++) { |
| 40 |
if (sdw_cols[i] == col) |
| 41 |
return i; |
| 42 |
} |
| 43 |
|
| 44 |
pr_warn("Requested column not found, selecting lowest column no: 2\n"); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
EXPORT_SYMBOL(sdw_find_col_index); |
| 48 |
|
| 49 |
int sdw_find_row_index(int row) |
| 50 |
{ |
| 51 |
int i; |
| 52 |
|
| 53 |
for (i = 0; i < SDW_FRAME_ROWS; i++) { |
| 54 |
if (sdw_rows[i] == row) |
| 55 |
return i; |
| 56 |
} |
| 57 |
|
| 58 |
pr_warn("Requested row not found, selecting lowest row no: 48\n"); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
EXPORT_SYMBOL(sdw_find_row_index); |
| 62 |
|
| 63 |
static int _sdw_program_slave_port_params(struct sdw_bus *bus, |
| 64 |
struct sdw_slave *slave, |
| 65 |
struct sdw_transport_params *t_params, |
| 66 |
enum sdw_dpn_type type) |
| 67 |
{ |
| 68 |
u32 addr1, addr2, addr3, addr4; |
| 69 |
int ret; |
| 70 |
u16 wbuf; |
| 71 |
|
| 72 |
if (bus->params.next_bank) { |
| 73 |
addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num); |
| 74 |
addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num); |
| 75 |
addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num); |
| 76 |
addr4 = SDW_DPN_HCTRL_B1(t_params->port_num); |
| 77 |
} else { |
| 78 |
addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num); |
| 79 |
addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num); |
| 80 |
addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num); |
| 81 |
addr4 = SDW_DPN_HCTRL_B0(t_params->port_num); |
| 82 |
} |
| 83 |
|
| 84 |
/* Program DPN_OffsetCtrl2 registers */ |
| 85 |
ret = sdw_write_no_pm(slave, addr1, t_params->offset2); |
| 86 |
if (ret < 0) { |
| 87 |
dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n"); |
| 88 |
return ret; |
| 89 |
} |
| 90 |
|
| 91 |
/* DP0 does not implement BlockCtrl3 */ |
| 92 |
if (t_params->port_num) { |
| 93 |
/* Program DPN_BlockCtrl3 register */ |
| 94 |
ret = sdw_write_no_pm(slave, addr2, t_params->blk_pkg_mode); |
| 95 |
if (ret < 0) { |
| 96 |
dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n"); |
| 97 |
return ret; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* Data ports are FULL, SIMPLE and REDUCED. This function handles |
| 103 |
* FULL and REDUCED only and beyond this point only FULL is |
| 104 |
* handled, so bail out if we are not FULL data port type |
| 105 |
*/ |
| 106 |
if (type != SDW_DPN_FULL) |
| 107 |
return ret; |
| 108 |
|
| 109 |
/* Program DPN_SampleCtrl2 register */ |
| 110 |
wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1); |
| 111 |
|
| 112 |
ret = sdw_write_no_pm(slave, addr3, wbuf); |
| 113 |
if (ret < 0) { |
| 114 |
dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n"); |
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
/* Program DPN_HCtrl register */ |
| 119 |
wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart); |
| 120 |
wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop); |
| 121 |
|
| 122 |
ret = sdw_write_no_pm(slave, addr4, wbuf); |
| 123 |
if (ret < 0) |
| 124 |
dev_err(bus->dev, "DPN_HCtrl register write failed\n"); |
| 125 |
|
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
static int sdw_program_slave_port_params(struct sdw_bus *bus, |
| 130 |
struct sdw_slave_runtime *s_rt, |
| 131 |
struct sdw_port_runtime *p_rt) |
| 132 |
{ |
| 133 |
struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 134 |
struct sdw_port_params *p_params = &p_rt->port_params; |
| 135 |
struct sdw_slave_prop *slave_prop = &s_rt->slave->prop; |
| 136 |
u32 addr1, addr2, addr3, addr4, addr5, addr6; |
| 137 |
enum sdw_dpn_type port_type; |
| 138 |
bool read_only_wordlength; |
| 139 |
int ret; |
| 140 |
u8 wbuf; |
| 141 |
|
| 142 |
if (s_rt->slave->is_mockup_device) |
| 143 |
return 0; |
| 144 |
|
| 145 |
if (t_params->port_num) { |
| 146 |
struct sdw_dpn_prop *dpn_prop; |
| 147 |
|
| 148 |
dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, s_rt->direction, |
| 149 |
t_params->port_num); |
| 150 |
if (!dpn_prop) |
| 151 |
return -EINVAL; |
| 152 |
|
| 153 |
read_only_wordlength = dpn_prop->read_only_wordlength; |
| 154 |
port_type = dpn_prop->type; |
| 155 |
} else { |
| 156 |
read_only_wordlength = false; |
| 157 |
port_type = SDW_DPN_FULL; |
| 158 |
} |
| 159 |
|
| 160 |
addr1 = SDW_DPN_PORTCTRL(t_params->port_num); |
| 161 |
addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num); |
| 162 |
|
| 163 |
if (bus->params.next_bank) { |
| 164 |
addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num); |
| 165 |
addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num); |
| 166 |
addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num); |
| 167 |
addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num); |
| 168 |
|
| 169 |
} else { |
| 170 |
addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num); |
| 171 |
addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num); |
| 172 |
addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num); |
| 173 |
addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num); |
| 174 |
} |
| 175 |
|
| 176 |
/* Program DPN_PortCtrl register */ |
| 177 |
wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode); |
| 178 |
wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode); |
| 179 |
|
| 180 |
ret = sdw_update_no_pm(s_rt->slave, addr1, 0xF, wbuf); |
| 181 |
if (ret < 0) { |
| 182 |
dev_err(&s_rt->slave->dev, |
| 183 |
"DPN_PortCtrl register write failed for port %d\n", |
| 184 |
t_params->port_num); |
| 185 |
return ret; |
| 186 |
} |
| 187 |
|
| 188 |
if (!read_only_wordlength) { |
| 189 |
/* Program DPN_BlockCtrl1 register */ |
| 190 |
ret = sdw_write_no_pm(s_rt->slave, addr2, (p_params->bps - 1)); |
| 191 |
if (ret < 0) { |
| 192 |
dev_err(&s_rt->slave->dev, |
| 193 |
"DPN_BlockCtrl1 register write failed for port %d\n", |
| 194 |
t_params->port_num); |
| 195 |
return ret; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* Program DPN_SampleCtrl1 register */ |
| 200 |
wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW; |
| 201 |
ret = sdw_write_no_pm(s_rt->slave, addr3, wbuf); |
| 202 |
if (ret < 0) { |
| 203 |
dev_err(&s_rt->slave->dev, |
| 204 |
"DPN_SampleCtrl1 register write failed for port %d\n", |
| 205 |
t_params->port_num); |
| 206 |
return ret; |
| 207 |
} |
| 208 |
|
| 209 |
/* Program DPN_OffsetCtrl1 registers */ |
| 210 |
ret = sdw_write_no_pm(s_rt->slave, addr4, t_params->offset1); |
| 211 |
if (ret < 0) { |
| 212 |
dev_err(&s_rt->slave->dev, |
| 213 |
"DPN_OffsetCtrl1 register write failed for port %d\n", |
| 214 |
t_params->port_num); |
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
/* Program DPN_BlockCtrl2 register*/ |
| 219 |
if (t_params->blk_grp_ctrl_valid) { |
| 220 |
ret = sdw_write_no_pm(s_rt->slave, addr5, t_params->blk_grp_ctrl); |
| 221 |
if (ret < 0) { |
| 222 |
dev_err(&s_rt->slave->dev, |
| 223 |
"DPN_BlockCtrl2 reg write failed for port %d\n", |
| 224 |
t_params->port_num); |
| 225 |
return ret; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* program DPN_LaneCtrl register */ |
| 230 |
if (slave_prop->lane_control_support) { |
| 231 |
ret = sdw_write_no_pm(s_rt->slave, addr6, t_params->lane_ctrl); |
| 232 |
if (ret < 0) { |
| 233 |
dev_err(&s_rt->slave->dev, |
| 234 |
"DPN_LaneCtrl register write failed for port %d\n", |
| 235 |
t_params->port_num); |
| 236 |
return ret; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (port_type != SDW_DPN_SIMPLE) { |
| 241 |
ret = _sdw_program_slave_port_params(bus, s_rt->slave, |
| 242 |
t_params, port_type); |
| 243 |
if (ret < 0) |
| 244 |
dev_err(&s_rt->slave->dev, |
| 245 |
"Transport reg write failed for port: %d\n", |
| 246 |
t_params->port_num); |
| 247 |
} |
| 248 |
|
| 249 |
return ret; |
| 250 |
} |
| 251 |
|
| 252 |
static int sdw_program_master_port_params(struct sdw_bus *bus, |
| 253 |
struct sdw_port_runtime *p_rt) |
| 254 |
{ |
| 255 |
int ret; |
| 256 |
|
| 257 |
/* |
| 258 |
* we need to set transport and port parameters for the port. |
| 259 |
* Transport parameters refers to the sample interval, offsets and |
| 260 |
* hstart/stop etc of the data. Port parameters refers to word |
| 261 |
* length, flow mode etc of the port |
| 262 |
*/ |
| 263 |
ret = bus->port_ops->dpn_set_port_transport_params(bus, |
| 264 |
&p_rt->transport_params, |
| 265 |
bus->params.next_bank); |
| 266 |
if (ret < 0) |
| 267 |
return ret; |
| 268 |
|
| 269 |
return bus->port_ops->dpn_set_port_params(bus, |
| 270 |
&p_rt->port_params, |
| 271 |
bus->params.next_bank); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* sdw_program_port_params() - Programs transport parameters of Master(s) |
| 276 |
* and Slave(s) |
| 277 |
* |
| 278 |
* @m_rt: Master stream runtime |
| 279 |
*/ |
| 280 |
static int sdw_program_port_params(struct sdw_master_runtime *m_rt) |
| 281 |
{ |
| 282 |
struct sdw_slave_runtime *s_rt; |
| 283 |
struct sdw_bus *bus = m_rt->bus; |
| 284 |
struct sdw_port_runtime *p_rt; |
| 285 |
int ret = 0; |
| 286 |
|
| 287 |
/* Program transport & port parameters for Slave(s) */ |
| 288 |
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 289 |
list_for_each_entry(p_rt, &s_rt->port_list, port_node) { |
| 290 |
ret = sdw_program_slave_port_params(bus, s_rt, p_rt); |
| 291 |
if (ret < 0) |
| 292 |
return ret; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/* Program transport & port parameters for Master(s) */ |
| 297 |
list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 298 |
ret = sdw_program_master_port_params(bus, p_rt); |
| 299 |
if (ret < 0) |
| 300 |
return ret; |
| 301 |
} |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* sdw_enable_disable_slave_ports: Enable/disable slave data port |
| 308 |
* |
| 309 |
* @bus: bus instance |
| 310 |
* @s_rt: slave runtime |
| 311 |
* @p_rt: port runtime |
| 312 |
* @en: enable or disable operation |
| 313 |
* |
| 314 |
* This function only sets the enable/disable bits in the relevant bank, the |
| 315 |
* actual enable/disable is done with a bank switch |
| 316 |
*/ |
| 317 |
static int sdw_enable_disable_slave_ports(struct sdw_bus *bus, |
| 318 |
struct sdw_slave_runtime *s_rt, |
| 319 |
struct sdw_port_runtime *p_rt, |
| 320 |
bool en) |
| 321 |
{ |
| 322 |
struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 323 |
u32 addr; |
| 324 |
int ret; |
| 325 |
|
| 326 |
if (bus->params.next_bank) |
| 327 |
addr = SDW_DPN_CHANNELEN_B1(p_rt->num); |
| 328 |
else |
| 329 |
addr = SDW_DPN_CHANNELEN_B0(p_rt->num); |
| 330 |
|
| 331 |
/* |
| 332 |
* Since bus doesn't support sharing a port across two streams, |
| 333 |
* it is safe to reset this register |
| 334 |
*/ |
| 335 |
if (en) |
| 336 |
ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); |
| 337 |
else |
| 338 |
ret = sdw_write_no_pm(s_rt->slave, addr, 0x0); |
| 339 |
|
| 340 |
if (ret < 0) |
| 341 |
dev_err(&s_rt->slave->dev, |
| 342 |
"Slave chn_en reg write failed:%d port:%d\n", |
| 343 |
ret, t_params->port_num); |
| 344 |
|
| 345 |
return ret; |
| 346 |
} |
| 347 |
|
| 348 |
static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt, |
| 349 |
struct sdw_port_runtime *p_rt, |
| 350 |
bool en) |
| 351 |
{ |
| 352 |
struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 353 |
struct sdw_bus *bus = m_rt->bus; |
| 354 |
struct sdw_enable_ch enable_ch; |
| 355 |
int ret; |
| 356 |
|
| 357 |
enable_ch.port_num = p_rt->num; |
| 358 |
enable_ch.ch_mask = p_rt->ch_mask; |
| 359 |
enable_ch.enable = en; |
| 360 |
|
| 361 |
/* Perform Master port channel(s) enable/disable */ |
| 362 |
if (bus->port_ops->dpn_port_enable_ch) { |
| 363 |
ret = bus->port_ops->dpn_port_enable_ch(bus, |
| 364 |
&enable_ch, |
| 365 |
bus->params.next_bank); |
| 366 |
if (ret < 0) { |
| 367 |
dev_err(bus->dev, |
| 368 |
"Master chn_en write failed:%d port:%d\n", |
| 369 |
ret, t_params->port_num); |
| 370 |
return ret; |
| 371 |
} |
| 372 |
} else { |
| 373 |
dev_err(bus->dev, |
| 374 |
"dpn_port_enable_ch not supported, %s failed\n", |
| 375 |
str_enable_disable(en)); |
| 376 |
return -EINVAL; |
| 377 |
} |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* sdw_enable_disable_ports() - Enable/disable port(s) for Master and |
| 384 |
* Slave(s) |
| 385 |
* |
| 386 |
* @m_rt: Master stream runtime |
| 387 |
* @en: mode (enable/disable) |
| 388 |
*/ |
| 389 |
static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en) |
| 390 |
{ |
| 391 |
struct sdw_port_runtime *s_port, *m_port; |
| 392 |
struct sdw_slave_runtime *s_rt; |
| 393 |
int ret = 0; |
| 394 |
|
| 395 |
/* Enable/Disable Slave port(s) */ |
| 396 |
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 397 |
list_for_each_entry(s_port, &s_rt->port_list, port_node) { |
| 398 |
ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt, |
| 399 |
s_port, en); |
| 400 |
if (ret < 0) |
| 401 |
return ret; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/* Enable/Disable Master port(s) */ |
| 406 |
list_for_each_entry(m_port, &m_rt->port_list, port_node) { |
| 407 |
ret = sdw_enable_disable_master_ports(m_rt, m_port, en); |
| 408 |
if (ret < 0) |
| 409 |
return ret; |
| 410 |
} |
| 411 |
|
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt, |
| 416 |
struct sdw_prepare_ch prep_ch, |
| 417 |
enum sdw_port_prep_ops cmd) |
| 418 |
{ |
| 419 |
int ret = 0; |
| 420 |
struct sdw_slave *slave = s_rt->slave; |
| 421 |
|
| 422 |
mutex_lock(&slave->sdw_dev_lock); |
| 423 |
|
| 424 |
if (slave->probed) { |
| 425 |
struct device *dev = &slave->dev; |
| 426 |
struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); |
| 427 |
|
| 428 |
if (drv->ops && drv->ops->port_prep) { |
| 429 |
ret = drv->ops->port_prep(slave, &prep_ch, cmd); |
| 430 |
if (ret < 0) |
| 431 |
dev_err(dev, "Slave Port Prep cmd %d failed: %d\n", |
| 432 |
cmd, ret); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
mutex_unlock(&slave->sdw_dev_lock); |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus, |
| 442 |
struct sdw_slave_runtime *s_rt, |
| 443 |
struct sdw_port_runtime *p_rt, |
| 444 |
bool prep) |
| 445 |
{ |
| 446 |
struct completion *port_ready; |
| 447 |
struct sdw_dpn_prop *dpn_prop; |
| 448 |
struct sdw_prepare_ch prep_ch; |
| 449 |
u32 imp_def_interrupts; |
| 450 |
bool simple_ch_prep_sm; |
| 451 |
u32 ch_prep_timeout; |
| 452 |
bool intr = false; |
| 453 |
int ret = 0, val; |
| 454 |
u32 addr; |
| 455 |
|
| 456 |
prep_ch.num = p_rt->num; |
| 457 |
prep_ch.ch_mask = p_rt->ch_mask; |
| 458 |
|
| 459 |
if (p_rt->num) { |
| 460 |
dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, s_rt->direction, prep_ch.num); |
| 461 |
if (!dpn_prop) { |
| 462 |
dev_err(bus->dev, |
| 463 |
"Slave Port:%d properties not found\n", prep_ch.num); |
| 464 |
return -EINVAL; |
| 465 |
} |
| 466 |
|
| 467 |
imp_def_interrupts = dpn_prop->imp_def_interrupts; |
| 468 |
simple_ch_prep_sm = dpn_prop->simple_ch_prep_sm; |
| 469 |
ch_prep_timeout = dpn_prop->ch_prep_timeout; |
| 470 |
} else { |
| 471 |
struct sdw_dp0_prop *dp0_prop = s_rt->slave->prop.dp0_prop; |
| 472 |
|
| 473 |
if (!dp0_prop) { |
| 474 |
dev_err(bus->dev, |
| 475 |
"Slave DP0 properties not found\n"); |
| 476 |
return -EINVAL; |
| 477 |
} |
| 478 |
imp_def_interrupts = dp0_prop->imp_def_interrupts; |
| 479 |
simple_ch_prep_sm = dp0_prop->simple_ch_prep_sm; |
| 480 |
ch_prep_timeout = dp0_prop->ch_prep_timeout; |
| 481 |
} |
| 482 |
|
| 483 |
prep_ch.prepare = prep; |
| 484 |
|
| 485 |
prep_ch.bank = bus->params.next_bank; |
| 486 |
|
| 487 |
if (imp_def_interrupts || !simple_ch_prep_sm || |
| 488 |
bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) |
| 489 |
intr = true; |
| 490 |
|
| 491 |
/* |
| 492 |
* Enable interrupt before Port prepare. |
| 493 |
* For Port de-prepare, it is assumed that port |
| 494 |
* was prepared earlier |
| 495 |
*/ |
| 496 |
if (prep && intr) { |
| 497 |
ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, |
| 498 |
imp_def_interrupts); |
| 499 |
if (ret < 0) |
| 500 |
return ret; |
| 501 |
} |
| 502 |
|
| 503 |
/* Inform slave about the impending port prepare */ |
| 504 |
sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_PRE_PREP : SDW_OPS_PORT_PRE_DEPREP); |
| 505 |
|
| 506 |
/* Prepare Slave port implementing CP_SM */ |
| 507 |
if (!simple_ch_prep_sm) { |
| 508 |
addr = SDW_DPN_PREPARECTRL(p_rt->num); |
| 509 |
|
| 510 |
if (prep) |
| 511 |
ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); |
| 512 |
else |
| 513 |
ret = sdw_write_no_pm(s_rt->slave, addr, 0x0); |
| 514 |
|
| 515 |
if (ret < 0) { |
| 516 |
dev_err(&s_rt->slave->dev, |
| 517 |
"Slave prep_ctrl reg write failed\n"); |
| 518 |
return ret; |
| 519 |
} |
| 520 |
|
| 521 |
/* Wait for completion on port ready */ |
| 522 |
port_ready = &s_rt->slave->port_ready[prep_ch.num]; |
| 523 |
wait_for_completion_timeout(port_ready, |
| 524 |
msecs_to_jiffies(ch_prep_timeout)); |
| 525 |
|
| 526 |
val = sdw_read_no_pm(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num)); |
| 527 |
if ((val < 0) || (val & p_rt->ch_mask)) { |
| 528 |
ret = (val < 0) ? val : -ETIMEDOUT; |
| 529 |
dev_err(&s_rt->slave->dev, |
| 530 |
"Chn prep failed for port %d: %d\n", prep_ch.num, ret); |
| 531 |
return ret; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* Inform slaves about ports prepared */ |
| 536 |
sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_POST_PREP : SDW_OPS_PORT_POST_DEPREP); |
| 537 |
|
| 538 |
/* Disable interrupt after Port de-prepare */ |
| 539 |
if (!prep && intr) |
| 540 |
ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, |
| 541 |
imp_def_interrupts); |
| 542 |
|
| 543 |
return ret; |
| 544 |
} |
| 545 |
|
| 546 |
static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt, |
| 547 |
struct sdw_port_runtime *p_rt, |
| 548 |
bool prep) |
| 549 |
{ |
| 550 |
struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 551 |
struct sdw_bus *bus = m_rt->bus; |
| 552 |
const struct sdw_master_port_ops *ops = bus->port_ops; |
| 553 |
struct sdw_prepare_ch prep_ch; |
| 554 |
int ret = 0; |
| 555 |
|
| 556 |
prep_ch.num = p_rt->num; |
| 557 |
prep_ch.ch_mask = p_rt->ch_mask; |
| 558 |
prep_ch.prepare = prep; /* Prepare/De-prepare */ |
| 559 |
prep_ch.bank = bus->params.next_bank; |
| 560 |
|
| 561 |
/* Pre-prepare/Pre-deprepare port(s) */ |
| 562 |
if (ops->dpn_port_prep) { |
| 563 |
ret = ops->dpn_port_prep(bus, &prep_ch); |
| 564 |
if (ret < 0) { |
| 565 |
dev_err(bus->dev, "Port prepare failed for port:%d\n", |
| 566 |
t_params->port_num); |
| 567 |
return ret; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
return ret; |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and |
| 576 |
* Slave(s) |
| 577 |
* |
| 578 |
* @m_rt: Master runtime handle |
| 579 |
* @prep: Prepare or De-prepare |
| 580 |
*/ |
| 581 |
static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep) |
| 582 |
{ |
| 583 |
struct sdw_slave_runtime *s_rt; |
| 584 |
struct sdw_port_runtime *p_rt; |
| 585 |
int ret = 0; |
| 586 |
|
| 587 |
/* Prepare/De-prepare Slave port(s) */ |
| 588 |
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 589 |
list_for_each_entry(p_rt, &s_rt->port_list, port_node) { |
| 590 |
ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt, |
| 591 |
p_rt, prep); |
| 592 |
if (ret < 0) |
| 593 |
return ret; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/* Prepare/De-prepare Master port(s) */ |
| 598 |
list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 599 |
ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep); |
| 600 |
if (ret < 0) |
| 601 |
return ret; |
| 602 |
} |
| 603 |
|
| 604 |
return ret; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* sdw_notify_config() - Notify bus configuration |
| 609 |
* |
| 610 |
* @m_rt: Master runtime handle |
| 611 |
* |
| 612 |
* This function notifies the Master(s) and Slave(s) of the |
| 613 |
* new bus configuration. |
| 614 |
*/ |
| 615 |
static int sdw_notify_config(struct sdw_master_runtime *m_rt) |
| 616 |
{ |
| 617 |
struct sdw_slave_runtime *s_rt; |
| 618 |
struct sdw_bus *bus = m_rt->bus; |
| 619 |
struct sdw_slave *slave; |
| 620 |
int ret; |
| 621 |
|
| 622 |
if (bus->ops->set_bus_conf) { |
| 623 |
ret = bus->ops->set_bus_conf(bus, &bus->params); |
| 624 |
if (ret < 0) |
| 625 |
return ret; |
| 626 |
} |
| 627 |
|
| 628 |
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 629 |
slave = s_rt->slave; |
| 630 |
|
| 631 |
mutex_lock(&slave->sdw_dev_lock); |
| 632 |
|
| 633 |
if (slave->probed) { |
| 634 |
struct device *dev = &slave->dev; |
| 635 |
struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); |
| 636 |
|
| 637 |
if (drv->ops && drv->ops->bus_config) { |
| 638 |
ret = drv->ops->bus_config(slave, &bus->params); |
| 639 |
if (ret < 0) { |
| 640 |
dev_err(dev, "Notify Slave: %d failed\n", |
| 641 |
slave->dev_num); |
| 642 |
mutex_unlock(&slave->sdw_dev_lock); |
| 643 |
return ret; |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
mutex_unlock(&slave->sdw_dev_lock); |
| 649 |
} |
| 650 |
|
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* sdw_program_params() - Program transport and port parameters for Master(s) |
| 656 |
* and Slave(s) |
| 657 |
* |
| 658 |
* @bus: SDW bus instance |
| 659 |
* @prepare: true if sdw_program_params() is called by _prepare. |
| 660 |
*/ |
| 661 |
static int sdw_program_params(struct sdw_bus *bus, bool prepare) |
| 662 |
{ |
| 663 |
struct sdw_master_runtime *m_rt; |
| 664 |
struct sdw_slave *slave; |
| 665 |
int ret = 0; |
| 666 |
u32 addr1; |
| 667 |
|
| 668 |
/* Check if all Peripherals comply with SDCA */ |
| 669 |
list_for_each_entry(slave, &bus->slaves, node) { |
| 670 |
if (!slave->dev_num_sticky) |
| 671 |
continue; |
| 672 |
if (!is_clock_scaling_supported_by_slave(slave)) { |
| 673 |
dev_dbg(&slave->dev, "The Peripheral doesn't comply with SDCA\n"); |
| 674 |
goto manager_runtime; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
if (bus->params.next_bank) |
| 679 |
addr1 = SDW_SCP_BUSCLOCK_SCALE_B1; |
| 680 |
else |
| 681 |
addr1 = SDW_SCP_BUSCLOCK_SCALE_B0; |
| 682 |
|
| 683 |
/* Program SDW_SCP_BUSCLOCK_SCALE if all Peripherals comply with SDCA */ |
| 684 |
list_for_each_entry(slave, &bus->slaves, node) { |
| 685 |
int scale_index; |
| 686 |
u8 base; |
| 687 |
|
| 688 |
if (!slave->dev_num_sticky) |
| 689 |
continue; |
| 690 |
scale_index = sdw_slave_get_scale_index(slave, &base); |
| 691 |
if (scale_index < 0) |
| 692 |
return scale_index; |
| 693 |
|
| 694 |
ret = sdw_write_no_pm(slave, addr1, scale_index); |
| 695 |
if (ret < 0) { |
| 696 |
dev_err(&slave->dev, "SDW_SCP_BUSCLOCK_SCALE register write failed\n"); |
| 697 |
return ret; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
manager_runtime: |
| 702 |
list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { |
| 703 |
|
| 704 |
/* |
| 705 |
* this loop walks through all master runtimes for a |
| 706 |
* bus, but the ports can only be configured while |
| 707 |
* explicitly preparing a stream or handling an |
| 708 |
* already-prepared stream otherwise. |
| 709 |
*/ |
| 710 |
if (!prepare && |
| 711 |
m_rt->stream->state == SDW_STREAM_CONFIGURED) |
| 712 |
continue; |
| 713 |
|
| 714 |
ret = sdw_program_port_params(m_rt); |
| 715 |
if (ret < 0) { |
| 716 |
dev_err(bus->dev, |
| 717 |
"Program transport params failed: %d\n", ret); |
| 718 |
return ret; |
| 719 |
} |
| 720 |
|
| 721 |
ret = sdw_notify_config(m_rt); |
| 722 |
if (ret < 0) { |
| 723 |
dev_err(bus->dev, |
| 724 |
"Notify bus config failed: %d\n", ret); |
| 725 |
return ret; |
| 726 |
} |
| 727 |
|
| 728 |
/* Enable port(s) on alternate bank for all active streams */ |
| 729 |
if (m_rt->stream->state != SDW_STREAM_ENABLED) |
| 730 |
continue; |
| 731 |
|
| 732 |
ret = sdw_enable_disable_ports(m_rt, true); |
| 733 |
if (ret < 0) { |
| 734 |
dev_err(bus->dev, "Enable channel failed: %d\n", ret); |
| 735 |
return ret; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
return ret; |
| 740 |
} |
| 741 |
|
| 742 |
static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) |
| 743 |
{ |
| 744 |
int col_index, row_index; |
| 745 |
bool multi_link; |
| 746 |
struct sdw_msg *wr_msg; |
| 747 |
u8 *wbuf; |
| 748 |
int ret; |
| 749 |
u16 addr; |
| 750 |
|
| 751 |
wr_msg = kzalloc_obj(*wr_msg); |
| 752 |
if (!wr_msg) |
| 753 |
return -ENOMEM; |
| 754 |
|
| 755 |
wbuf = kzalloc_obj(*wbuf); |
| 756 |
if (!wbuf) { |
| 757 |
ret = -ENOMEM; |
| 758 |
goto error_1; |
| 759 |
} |
| 760 |
|
| 761 |
/* Get row and column index to program register */ |
| 762 |
col_index = sdw_find_col_index(bus->params.col); |
| 763 |
row_index = sdw_find_row_index(bus->params.row); |
| 764 |
wbuf[0] = col_index | (row_index << 3); |
| 765 |
|
| 766 |
if (bus->params.next_bank) |
| 767 |
addr = SDW_SCP_FRAMECTRL_B1; |
| 768 |
else |
| 769 |
addr = SDW_SCP_FRAMECTRL_B0; |
| 770 |
|
| 771 |
sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM, |
| 772 |
SDW_MSG_FLAG_WRITE, wbuf); |
| 773 |
wr_msg->ssp_sync = true; |
| 774 |
|
| 775 |
/* |
| 776 |
* Set the multi_link flag only when both the hardware supports |
| 777 |
* and hardware-based sync is required |
| 778 |
*/ |
| 779 |
multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links); |
| 780 |
|
| 781 |
if (multi_link) |
| 782 |
ret = sdw_transfer_defer(bus, wr_msg); |
| 783 |
else |
| 784 |
ret = sdw_transfer(bus, wr_msg); |
| 785 |
|
| 786 |
if (ret < 0 && ret != -ENODATA) { |
| 787 |
dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); |
| 788 |
goto error; |
| 789 |
} |
| 790 |
|
| 791 |
if (!multi_link) { |
| 792 |
kfree(wbuf); |
| 793 |
kfree(wr_msg); |
| 794 |
bus->defer_msg.msg = NULL; |
| 795 |
bus->params.curr_bank = !bus->params.curr_bank; |
| 796 |
bus->params.next_bank = !bus->params.next_bank; |
| 797 |
} |
| 798 |
|
| 799 |
return 0; |
| 800 |
|
| 801 |
error: |
| 802 |
kfree(wbuf); |
| 803 |
error_1: |
| 804 |
kfree(wr_msg); |
| 805 |
bus->defer_msg.msg = NULL; |
| 806 |
return ret; |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
* sdw_ml_sync_bank_switch: Multilink register bank switch |
| 811 |
* |
| 812 |
* @bus: SDW bus instance |
| 813 |
* @multi_link: whether this is a multi-link stream with hardware-based sync |
| 814 |
* |
| 815 |
* Caller function should free the buffers on error |
| 816 |
*/ |
| 817 |
static int sdw_ml_sync_bank_switch(struct sdw_bus *bus, bool multi_link) |
| 818 |
{ |
| 819 |
unsigned long time_left; |
| 820 |
|
| 821 |
if (!multi_link) |
| 822 |
return 0; |
| 823 |
|
| 824 |
/* Wait for completion of transfer */ |
| 825 |
time_left = wait_for_completion_timeout(&bus->defer_msg.complete, |
| 826 |
bus->bank_switch_timeout); |
| 827 |
|
| 828 |
if (!time_left) { |
| 829 |
dev_err(bus->dev, "Controller Timed out on bank switch\n"); |
| 830 |
return -ETIMEDOUT; |
| 831 |
} |
| 832 |
|
| 833 |
bus->params.curr_bank = !bus->params.curr_bank; |
| 834 |
bus->params.next_bank = !bus->params.next_bank; |
| 835 |
|
| 836 |
if (bus->defer_msg.msg) { |
| 837 |
kfree(bus->defer_msg.msg->buf); |
| 838 |
kfree(bus->defer_msg.msg); |
| 839 |
bus->defer_msg.msg = NULL; |
| 840 |
} |
| 841 |
|
| 842 |
return 0; |
| 843 |
} |
| 844 |
|
| 845 |
static int do_bank_switch(struct sdw_stream_runtime *stream) |
| 846 |
{ |
| 847 |
struct sdw_master_runtime *m_rt; |
| 848 |
const struct sdw_master_ops *ops; |
| 849 |
struct sdw_bus *bus; |
| 850 |
bool multi_link = false; |
| 851 |
int m_rt_count; |
| 852 |
int ret = 0; |
| 853 |
|
| 854 |
m_rt_count = stream->m_rt_count; |
| 855 |
|
| 856 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 857 |
bus = m_rt->bus; |
| 858 |
ops = bus->ops; |
| 859 |
|
| 860 |
if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) { |
| 861 |
multi_link = true; |
| 862 |
mutex_lock(&bus->msg_lock); |
| 863 |
} |
| 864 |
|
| 865 |
/* Pre-bank switch */ |
| 866 |
if (ops->pre_bank_switch) { |
| 867 |
ret = ops->pre_bank_switch(bus); |
| 868 |
if (ret < 0) { |
| 869 |
dev_err(bus->dev, |
| 870 |
"Pre bank switch op failed: %d\n", ret); |
| 871 |
goto msg_unlock; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
/* |
| 876 |
* Perform Bank switch operation. |
| 877 |
* For multi link cases, the actual bank switch is |
| 878 |
* synchronized across all Masters and happens later as a |
| 879 |
* part of post_bank_switch ops. |
| 880 |
*/ |
| 881 |
ret = sdw_bank_switch(bus, m_rt_count); |
| 882 |
if (ret < 0) { |
| 883 |
dev_err(bus->dev, "Bank switch failed: %d\n", ret); |
| 884 |
goto error; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/* |
| 889 |
* For multi link cases, it is expected that the bank switch is |
| 890 |
* triggered by the post_bank_switch for the first Master in the list |
| 891 |
* and for the other Masters the post_bank_switch() should return doing |
| 892 |
* nothing. |
| 893 |
*/ |
| 894 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 895 |
bus = m_rt->bus; |
| 896 |
ops = bus->ops; |
| 897 |
|
| 898 |
/* Post-bank switch */ |
| 899 |
if (ops->post_bank_switch) { |
| 900 |
ret = ops->post_bank_switch(bus); |
| 901 |
if (ret < 0) { |
| 902 |
dev_err(bus->dev, |
| 903 |
"Post bank switch op failed: %d\n", |
| 904 |
ret); |
| 905 |
goto error; |
| 906 |
} |
| 907 |
} else if (multi_link) { |
| 908 |
dev_err(bus->dev, |
| 909 |
"Post bank switch ops not implemented\n"); |
| 910 |
ret = -EINVAL; |
| 911 |
goto error; |
| 912 |
} |
| 913 |
|
| 914 |
/* Set the bank switch timeout to default, if not set */ |
| 915 |
if (!bus->bank_switch_timeout) |
| 916 |
bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT; |
| 917 |
|
| 918 |
/* Check if bank switch was successful */ |
| 919 |
ret = sdw_ml_sync_bank_switch(bus, multi_link); |
| 920 |
if (ret < 0) { |
| 921 |
dev_err(bus->dev, |
| 922 |
"multi link bank switch failed: %d\n", ret); |
| 923 |
goto error; |
| 924 |
} |
| 925 |
|
| 926 |
if (multi_link) |
| 927 |
mutex_unlock(&bus->msg_lock); |
| 928 |
} |
| 929 |
|
| 930 |
return ret; |
| 931 |
|
| 932 |
error: |
| 933 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 934 |
bus = m_rt->bus; |
| 935 |
if (bus->defer_msg.msg) { |
| 936 |
kfree(bus->defer_msg.msg->buf); |
| 937 |
kfree(bus->defer_msg.msg); |
| 938 |
bus->defer_msg.msg = NULL; |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
msg_unlock: |
| 943 |
|
| 944 |
if (multi_link) { |
| 945 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 946 |
bus = m_rt->bus; |
| 947 |
if (mutex_is_locked(&bus->msg_lock)) |
| 948 |
mutex_unlock(&bus->msg_lock); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
return ret; |
| 953 |
} |
| 954 |
|
| 955 |
static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list) |
| 956 |
{ |
| 957 |
struct sdw_port_runtime *p_rt; |
| 958 |
|
| 959 |
p_rt = kzalloc_obj(*p_rt); |
| 960 |
if (!p_rt) |
| 961 |
return NULL; |
| 962 |
|
| 963 |
list_add_tail(&p_rt->port_node, port_list); |
| 964 |
|
| 965 |
return p_rt; |
| 966 |
} |
| 967 |
|
| 968 |
static int sdw_port_config(struct sdw_port_runtime *p_rt, |
| 969 |
const struct sdw_port_config *port_config, |
| 970 |
int port_index) |
| 971 |
{ |
| 972 |
p_rt->ch_mask = port_config[port_index].ch_mask; |
| 973 |
p_rt->num = port_config[port_index].num; |
| 974 |
|
| 975 |
/* |
| 976 |
* TODO: Check port capabilities for requested configuration |
| 977 |
*/ |
| 978 |
|
| 979 |
return 0; |
| 980 |
} |
| 981 |
|
| 982 |
static void sdw_port_free(struct sdw_port_runtime *p_rt) |
| 983 |
{ |
| 984 |
list_del(&p_rt->port_node); |
| 985 |
kfree(p_rt); |
| 986 |
} |
| 987 |
|
| 988 |
static bool sdw_slave_port_allocated(struct sdw_slave_runtime *s_rt) |
| 989 |
{ |
| 990 |
return !list_empty(&s_rt->port_list); |
| 991 |
} |
| 992 |
|
| 993 |
static void sdw_slave_port_free(struct sdw_slave *slave, |
| 994 |
struct sdw_stream_runtime *stream) |
| 995 |
{ |
| 996 |
struct sdw_port_runtime *p_rt, *_p_rt; |
| 997 |
struct sdw_master_runtime *m_rt; |
| 998 |
struct sdw_slave_runtime *s_rt; |
| 999 |
|
| 1000 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1001 |
list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 1002 |
if (s_rt->slave != slave) |
| 1003 |
continue; |
| 1004 |
|
| 1005 |
list_for_each_entry_safe(p_rt, _p_rt, |
| 1006 |
&s_rt->port_list, port_node) { |
| 1007 |
sdw_port_free(p_rt); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
static int sdw_slave_port_alloc(struct sdw_slave *slave, |
| 1014 |
struct sdw_slave_runtime *s_rt, |
| 1015 |
unsigned int num_config) |
| 1016 |
{ |
| 1017 |
struct sdw_port_runtime *p_rt; |
| 1018 |
int i; |
| 1019 |
|
| 1020 |
/* Iterate for number of ports to perform initialization */ |
| 1021 |
for (i = 0; i < num_config; i++) { |
| 1022 |
p_rt = sdw_port_alloc(&s_rt->port_list); |
| 1023 |
if (!p_rt) |
| 1024 |
return -ENOMEM; |
| 1025 |
} |
| 1026 |
|
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
|
| 1030 |
static int sdw_slave_port_is_valid_range(struct device *dev, int num) |
| 1031 |
{ |
| 1032 |
if (!SDW_VALID_PORT_RANGE(num)) { |
| 1033 |
dev_err(dev, "SoundWire: Invalid port number :%d\n", num); |
| 1034 |
return -EINVAL; |
| 1035 |
} |
| 1036 |
|
| 1037 |
return 0; |
| 1038 |
} |
| 1039 |
|
| 1040 |
static int sdw_slave_port_config(struct sdw_slave *slave, |
| 1041 |
struct sdw_slave_runtime *s_rt, |
| 1042 |
const struct sdw_port_config *port_config, |
| 1043 |
bool is_bpt_stream) |
| 1044 |
{ |
| 1045 |
struct sdw_port_runtime *p_rt; |
| 1046 |
int ret; |
| 1047 |
int i; |
| 1048 |
|
| 1049 |
i = 0; |
| 1050 |
list_for_each_entry(p_rt, &s_rt->port_list, port_node) { |
| 1051 |
/* |
| 1052 |
* TODO: Check valid port range as defined by DisCo/ |
| 1053 |
* slave |
| 1054 |
*/ |
| 1055 |
if (!is_bpt_stream) { |
| 1056 |
ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num); |
| 1057 |
if (ret < 0) |
| 1058 |
return ret; |
| 1059 |
} else if (port_config[i].num) { |
| 1060 |
return -EINVAL; |
| 1061 |
} |
| 1062 |
|
| 1063 |
ret = sdw_port_config(p_rt, port_config, i); |
| 1064 |
if (ret < 0) |
| 1065 |
return ret; |
| 1066 |
i++; |
| 1067 |
} |
| 1068 |
|
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static bool sdw_master_port_allocated(struct sdw_master_runtime *m_rt) |
| 1073 |
{ |
| 1074 |
return !list_empty(&m_rt->port_list); |
| 1075 |
} |
| 1076 |
|
| 1077 |
static void sdw_master_port_free(struct sdw_master_runtime *m_rt) |
| 1078 |
{ |
| 1079 |
struct sdw_port_runtime *p_rt, *_p_rt; |
| 1080 |
|
| 1081 |
list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) { |
| 1082 |
sdw_port_free(p_rt); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt, |
| 1087 |
unsigned int num_ports) |
| 1088 |
{ |
| 1089 |
struct sdw_port_runtime *p_rt; |
| 1090 |
int i; |
| 1091 |
|
| 1092 |
/* Iterate for number of ports to perform initialization */ |
| 1093 |
for (i = 0; i < num_ports; i++) { |
| 1094 |
p_rt = sdw_port_alloc(&m_rt->port_list); |
| 1095 |
if (!p_rt) |
| 1096 |
return -ENOMEM; |
| 1097 |
} |
| 1098 |
|
| 1099 |
return 0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
static int sdw_master_port_config(struct sdw_master_runtime *m_rt, |
| 1103 |
const struct sdw_port_config *port_config) |
| 1104 |
{ |
| 1105 |
struct sdw_port_runtime *p_rt; |
| 1106 |
int ret; |
| 1107 |
int i; |
| 1108 |
|
| 1109 |
i = 0; |
| 1110 |
list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 1111 |
ret = sdw_port_config(p_rt, port_config, i); |
| 1112 |
if (ret < 0) |
| 1113 |
return ret; |
| 1114 |
i++; |
| 1115 |
} |
| 1116 |
|
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
|
| 1120 |
/** |
| 1121 |
* sdw_slave_rt_alloc() - Allocate a Slave runtime handle. |
| 1122 |
* |
| 1123 |
* @slave: Slave handle |
| 1124 |
* @m_rt: Master runtime handle |
| 1125 |
* |
| 1126 |
* This function is to be called with bus_lock held. |
| 1127 |
*/ |
| 1128 |
static struct sdw_slave_runtime |
| 1129 |
*sdw_slave_rt_alloc(struct sdw_slave *slave, |
| 1130 |
struct sdw_master_runtime *m_rt) |
| 1131 |
{ |
| 1132 |
struct sdw_slave_runtime *s_rt; |
| 1133 |
|
| 1134 |
s_rt = kzalloc_obj(*s_rt); |
| 1135 |
if (!s_rt) |
| 1136 |
return NULL; |
| 1137 |
|
| 1138 |
INIT_LIST_HEAD(&s_rt->port_list); |
| 1139 |
s_rt->slave = slave; |
| 1140 |
|
| 1141 |
list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list); |
| 1142 |
|
| 1143 |
return s_rt; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/** |
| 1147 |
* sdw_slave_rt_config() - Configure a Slave runtime handle. |
| 1148 |
* |
| 1149 |
* @s_rt: Slave runtime handle |
| 1150 |
* @stream_config: Stream configuration |
| 1151 |
* |
| 1152 |
* This function is to be called with bus_lock held. |
| 1153 |
*/ |
| 1154 |
static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt, |
| 1155 |
struct sdw_stream_config *stream_config) |
| 1156 |
{ |
| 1157 |
s_rt->ch_count = stream_config->ch_count; |
| 1158 |
s_rt->direction = stream_config->direction; |
| 1159 |
|
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static struct sdw_slave_runtime *sdw_slave_rt_find(struct sdw_slave *slave, |
| 1164 |
struct sdw_stream_runtime *stream) |
| 1165 |
{ |
| 1166 |
struct sdw_slave_runtime *s_rt, *_s_rt; |
| 1167 |
struct sdw_master_runtime *m_rt; |
| 1168 |
|
| 1169 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1170 |
/* Retrieve Slave runtime handle */ |
| 1171 |
list_for_each_entry_safe(s_rt, _s_rt, |
| 1172 |
&m_rt->slave_rt_list, m_rt_node) { |
| 1173 |
if (s_rt->slave == slave) |
| 1174 |
return s_rt; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
return NULL; |
| 1178 |
} |
| 1179 |
|
| 1180 |
/** |
| 1181 |
* sdw_slave_rt_free() - Free Slave(s) runtime handle |
| 1182 |
* |
| 1183 |
* @slave: Slave handle. |
| 1184 |
* @stream: Stream runtime handle. |
| 1185 |
* |
| 1186 |
* This function is to be called with bus_lock held. |
| 1187 |
*/ |
| 1188 |
static void sdw_slave_rt_free(struct sdw_slave *slave, |
| 1189 |
struct sdw_stream_runtime *stream) |
| 1190 |
{ |
| 1191 |
struct sdw_slave_runtime *s_rt; |
| 1192 |
|
| 1193 |
s_rt = sdw_slave_rt_find(slave, stream); |
| 1194 |
if (s_rt) { |
| 1195 |
list_del(&s_rt->m_rt_node); |
| 1196 |
kfree(s_rt); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
static struct sdw_master_runtime |
| 1201 |
*sdw_master_rt_find(struct sdw_bus *bus, |
| 1202 |
struct sdw_stream_runtime *stream) |
| 1203 |
{ |
| 1204 |
struct sdw_master_runtime *m_rt; |
| 1205 |
|
| 1206 |
/* Retrieve Bus handle if already available */ |
| 1207 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1208 |
if (m_rt->bus == bus) |
| 1209 |
return m_rt; |
| 1210 |
} |
| 1211 |
|
| 1212 |
return NULL; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/** |
| 1216 |
* sdw_master_rt_alloc() - Allocates a Master runtime handle |
| 1217 |
* |
| 1218 |
* @bus: SDW bus instance |
| 1219 |
* @stream: Stream runtime handle. |
| 1220 |
* |
| 1221 |
* This function is to be called with bus_lock held. |
| 1222 |
*/ |
| 1223 |
static struct sdw_master_runtime |
| 1224 |
*sdw_master_rt_alloc(struct sdw_bus *bus, |
| 1225 |
struct sdw_stream_runtime *stream) |
| 1226 |
{ |
| 1227 |
struct sdw_master_runtime *m_rt, *walk_m_rt; |
| 1228 |
struct list_head *insert_after; |
| 1229 |
|
| 1230 |
if (stream->type == SDW_STREAM_BPT) { |
| 1231 |
if (bus->stream_refcount > 0 || bus->bpt_stream_refcount > 0) { |
| 1232 |
dev_err(bus->dev, "%s: %d/%d audio/BPT stream already allocated\n", |
| 1233 |
__func__, bus->stream_refcount, bus->bpt_stream_refcount); |
| 1234 |
return ERR_PTR(-EBUSY); |
| 1235 |
} |
| 1236 |
} else { |
| 1237 |
if (bus->bpt_stream_refcount > 0) { |
| 1238 |
dev_err(bus->dev, "%s: BPT stream already allocated\n", |
| 1239 |
__func__); |
| 1240 |
return ERR_PTR(-EAGAIN); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
m_rt = kzalloc_obj(*m_rt); |
| 1245 |
if (!m_rt) |
| 1246 |
return NULL; |
| 1247 |
|
| 1248 |
/* Initialization of Master runtime handle */ |
| 1249 |
INIT_LIST_HEAD(&m_rt->port_list); |
| 1250 |
INIT_LIST_HEAD(&m_rt->slave_rt_list); |
| 1251 |
|
| 1252 |
/* |
| 1253 |
* Add in order of bus id so that when taking the bus_lock |
| 1254 |
* of multiple buses they will always be taken in the same |
| 1255 |
* order to prevent a mutex deadlock. |
| 1256 |
*/ |
| 1257 |
insert_after = &stream->master_list; |
| 1258 |
list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) { |
| 1259 |
if (walk_m_rt->bus->id < bus->id) { |
| 1260 |
insert_after = &walk_m_rt->stream_node; |
| 1261 |
break; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
list_add(&m_rt->stream_node, insert_after); |
| 1265 |
|
| 1266 |
list_add_tail(&m_rt->bus_node, &bus->m_rt_list); |
| 1267 |
|
| 1268 |
m_rt->bus = bus; |
| 1269 |
m_rt->stream = stream; |
| 1270 |
|
| 1271 |
bus->stream_refcount++; |
| 1272 |
if (stream->type == SDW_STREAM_BPT) |
| 1273 |
bus->bpt_stream_refcount++; |
| 1274 |
|
| 1275 |
return m_rt; |
| 1276 |
} |
| 1277 |
|
| 1278 |
/** |
| 1279 |
* sdw_master_rt_config() - Configure Master runtime handle |
| 1280 |
* |
| 1281 |
* @m_rt: Master runtime handle |
| 1282 |
* @stream_config: Stream configuration |
| 1283 |
* |
| 1284 |
* This function is to be called with bus_lock held. |
| 1285 |
*/ |
| 1286 |
|
| 1287 |
static int sdw_master_rt_config(struct sdw_master_runtime *m_rt, |
| 1288 |
struct sdw_stream_config *stream_config) |
| 1289 |
{ |
| 1290 |
m_rt->ch_count = stream_config->ch_count; |
| 1291 |
m_rt->direction = stream_config->direction; |
| 1292 |
|
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
/** |
| 1297 |
* sdw_master_rt_free() - Free Master runtime handle |
| 1298 |
* |
| 1299 |
* @m_rt: Master runtime node |
| 1300 |
* @stream: Stream runtime handle. |
| 1301 |
* |
| 1302 |
* This function is to be called with bus_lock held |
| 1303 |
* It frees the Master runtime handle and associated Slave(s) runtime |
| 1304 |
* handle. If this is called first then sdw_slave_rt_free() will have |
| 1305 |
* no effect as Slave(s) runtime handle would already be freed up. |
| 1306 |
*/ |
| 1307 |
static void sdw_master_rt_free(struct sdw_master_runtime *m_rt, |
| 1308 |
struct sdw_stream_runtime *stream) |
| 1309 |
{ |
| 1310 |
struct sdw_slave_runtime *s_rt, *_s_rt; |
| 1311 |
struct sdw_bus *bus = m_rt->bus; |
| 1312 |
|
| 1313 |
list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 1314 |
sdw_slave_port_free(s_rt->slave, stream); |
| 1315 |
sdw_slave_rt_free(s_rt->slave, stream); |
| 1316 |
} |
| 1317 |
|
| 1318 |
list_del(&m_rt->stream_node); |
| 1319 |
list_del(&m_rt->bus_node); |
| 1320 |
kfree(m_rt); |
| 1321 |
|
| 1322 |
if (stream->type == SDW_STREAM_BPT) |
| 1323 |
bus->bpt_stream_refcount--; |
| 1324 |
bus->stream_refcount--; |
| 1325 |
} |
| 1326 |
|
| 1327 |
/** |
| 1328 |
* sdw_config_stream() - Configure the allocated stream |
| 1329 |
* |
| 1330 |
* @dev: SDW device |
| 1331 |
* @stream: SoundWire stream |
| 1332 |
* @stream_config: Stream configuration for audio stream |
| 1333 |
* @is_slave: is API called from Slave or Master |
| 1334 |
* |
| 1335 |
* This function is to be called with bus_lock held. |
| 1336 |
*/ |
| 1337 |
static int sdw_config_stream(struct device *dev, |
| 1338 |
struct sdw_stream_runtime *stream, |
| 1339 |
struct sdw_stream_config *stream_config, |
| 1340 |
bool is_slave) |
| 1341 |
{ |
| 1342 |
/* |
| 1343 |
* Update the stream rate, channel and bps based on data |
| 1344 |
* source. For more than one data source (multilink), |
| 1345 |
* match the rate, bps, stream type and increment number of channels. |
| 1346 |
* |
| 1347 |
* If rate/bps is zero, it means the values are not set, so skip |
| 1348 |
* comparison and allow the value to be set and stored in stream |
| 1349 |
*/ |
| 1350 |
if (stream->params.rate && |
| 1351 |
stream->params.rate != stream_config->frame_rate) { |
| 1352 |
dev_err(dev, "rate not matching, stream:%s\n", stream->name); |
| 1353 |
return -EINVAL; |
| 1354 |
} |
| 1355 |
|
| 1356 |
if (stream->params.bps && |
| 1357 |
stream->params.bps != stream_config->bps) { |
| 1358 |
dev_err(dev, "bps not matching, stream:%s\n", stream->name); |
| 1359 |
return -EINVAL; |
| 1360 |
} |
| 1361 |
|
| 1362 |
stream->type = stream_config->type; |
| 1363 |
stream->params.rate = stream_config->frame_rate; |
| 1364 |
stream->params.bps = stream_config->bps; |
| 1365 |
|
| 1366 |
/* TODO: Update this check during Device-device support */ |
| 1367 |
if (is_slave) |
| 1368 |
stream->params.ch_count += stream_config->ch_count; |
| 1369 |
|
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
|
| 1373 |
/** |
| 1374 |
* sdw_get_slave_dpn_prop() - Get Slave port capabilities |
| 1375 |
* |
| 1376 |
* @slave: Slave handle |
| 1377 |
* @direction: Data direction. |
| 1378 |
* @port_num: Port number |
| 1379 |
*/ |
| 1380 |
struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave, |
| 1381 |
enum sdw_data_direction direction, |
| 1382 |
unsigned int port_num) |
| 1383 |
{ |
| 1384 |
struct sdw_dpn_prop *dpn_prop; |
| 1385 |
u8 num_ports; |
| 1386 |
int i; |
| 1387 |
|
| 1388 |
if (!port_num) { |
| 1389 |
dev_err(&slave->dev, "%s: port_num is zero\n", __func__); |
| 1390 |
return NULL; |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (direction == SDW_DATA_DIR_TX) { |
| 1394 |
num_ports = hweight32(slave->prop.source_ports); |
| 1395 |
dpn_prop = slave->prop.src_dpn_prop; |
| 1396 |
} else { |
| 1397 |
num_ports = hweight32(slave->prop.sink_ports); |
| 1398 |
dpn_prop = slave->prop.sink_dpn_prop; |
| 1399 |
} |
| 1400 |
|
| 1401 |
for (i = 0; i < num_ports; i++) { |
| 1402 |
if (dpn_prop[i].num == port_num) |
| 1403 |
return &dpn_prop[i]; |
| 1404 |
} |
| 1405 |
|
| 1406 |
return NULL; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/** |
| 1410 |
* sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s) |
| 1411 |
* |
| 1412 |
* @stream: SoundWire stream |
| 1413 |
* |
| 1414 |
* Acquire bus_lock for each of the master runtime(m_rt) part of this |
| 1415 |
* stream to reconfigure the bus. |
| 1416 |
* NOTE: This function is called from SoundWire stream ops and is |
| 1417 |
* expected that a global lock is held before acquiring bus_lock. |
| 1418 |
*/ |
| 1419 |
static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream) |
| 1420 |
{ |
| 1421 |
struct sdw_master_runtime *m_rt; |
| 1422 |
struct sdw_bus *bus; |
| 1423 |
|
| 1424 |
/* Iterate for all Master(s) in Master list */ |
| 1425 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1426 |
bus = m_rt->bus; |
| 1427 |
|
| 1428 |
mutex_lock(&bus->bus_lock); |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
/** |
| 1433 |
* sdw_release_bus_lock: Release bus lock for all Master runtime(s) |
| 1434 |
* |
| 1435 |
* @stream: SoundWire stream |
| 1436 |
* |
| 1437 |
* Release the previously held bus_lock after reconfiguring the bus. |
| 1438 |
* NOTE: This function is called from SoundWire stream ops and is |
| 1439 |
* expected that a global lock is held before releasing bus_lock. |
| 1440 |
*/ |
| 1441 |
static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) |
| 1442 |
{ |
| 1443 |
struct sdw_master_runtime *m_rt; |
| 1444 |
struct sdw_bus *bus; |
| 1445 |
|
| 1446 |
/* Iterate for all Master(s) in Master list */ |
| 1447 |
list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) { |
| 1448 |
bus = m_rt->bus; |
| 1449 |
mutex_unlock(&bus->bus_lock); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
static int _sdw_prepare_stream(struct sdw_stream_runtime *stream, |
| 1454 |
bool update_params) |
| 1455 |
{ |
| 1456 |
struct sdw_master_runtime *m_rt; |
| 1457 |
struct sdw_bus *bus; |
| 1458 |
struct sdw_master_prop *prop; |
| 1459 |
struct sdw_bus_params params; |
| 1460 |
int ret; |
| 1461 |
|
| 1462 |
/* Prepare Master(s) and Slave(s) port(s) associated with stream */ |
| 1463 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1464 |
bus = m_rt->bus; |
| 1465 |
prop = &bus->prop; |
| 1466 |
memcpy(¶ms, &bus->params, sizeof(params)); |
| 1467 |
|
| 1468 |
/* TODO: Support Asynchronous mode */ |
| 1469 |
if ((prop->max_clk_freq % stream->params.rate) != 0) { |
| 1470 |
dev_err(bus->dev, "Async mode not supported\n"); |
| 1471 |
return -EINVAL; |
| 1472 |
} |
| 1473 |
|
| 1474 |
if (update_params) { |
| 1475 |
/* Increment cumulative bus bandwidth */ |
| 1476 |
/* TODO: Update this during Device-Device support */ |
| 1477 |
bus->params.bandwidth += m_rt->stream->params.rate * |
| 1478 |
m_rt->ch_count * m_rt->stream->params.bps; |
| 1479 |
|
| 1480 |
/* Compute params */ |
| 1481 |
if (bus->compute_params) { |
| 1482 |
ret = bus->compute_params(bus, stream); |
| 1483 |
if (ret < 0) { |
| 1484 |
dev_err(bus->dev, "Compute params failed: %d\n", |
| 1485 |
ret); |
| 1486 |
goto restore_params; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
/* Program params */ |
| 1492 |
ret = sdw_program_params(bus, true); |
| 1493 |
if (ret < 0) { |
| 1494 |
dev_err(bus->dev, "Program params failed: %d\n", ret); |
| 1495 |
goto restore_params; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
ret = do_bank_switch(stream); |
| 1500 |
if (ret < 0) { |
| 1501 |
pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); |
| 1502 |
goto restore_params; |
| 1503 |
} |
| 1504 |
|
| 1505 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1506 |
bus = m_rt->bus; |
| 1507 |
|
| 1508 |
/* Prepare port(s) on the new clock configuration */ |
| 1509 |
ret = sdw_prep_deprep_ports(m_rt, true); |
| 1510 |
if (ret < 0) { |
| 1511 |
dev_err(bus->dev, "Prepare port(s) failed ret = %d\n", |
| 1512 |
ret); |
| 1513 |
goto restore_params; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
stream->state = SDW_STREAM_PREPARED; |
| 1518 |
|
| 1519 |
return ret; |
| 1520 |
|
| 1521 |
restore_params: |
| 1522 |
memcpy(&bus->params, ¶ms, sizeof(params)); |
| 1523 |
return ret; |
| 1524 |
} |
| 1525 |
|
| 1526 |
/** |
| 1527 |
* sdw_prepare_stream() - Prepare SoundWire stream |
| 1528 |
* |
| 1529 |
* @stream: Soundwire stream |
| 1530 |
* |
| 1531 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1532 |
*/ |
| 1533 |
int sdw_prepare_stream(struct sdw_stream_runtime *stream) |
| 1534 |
{ |
| 1535 |
bool update_params = true; |
| 1536 |
int ret; |
| 1537 |
|
| 1538 |
if (!stream) { |
| 1539 |
pr_err("SoundWire: Handle not found for stream\n"); |
| 1540 |
return -EINVAL; |
| 1541 |
} |
| 1542 |
|
| 1543 |
sdw_acquire_bus_lock(stream); |
| 1544 |
|
| 1545 |
if (stream->state == SDW_STREAM_PREPARED) { |
| 1546 |
ret = 0; |
| 1547 |
goto state_err; |
| 1548 |
} |
| 1549 |
|
| 1550 |
if (stream->state != SDW_STREAM_CONFIGURED && |
| 1551 |
stream->state != SDW_STREAM_DEPREPARED && |
| 1552 |
stream->state != SDW_STREAM_DISABLED) { |
| 1553 |
pr_err("%s: %s: inconsistent state state %d\n", |
| 1554 |
__func__, stream->name, stream->state); |
| 1555 |
ret = -EINVAL; |
| 1556 |
goto state_err; |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* |
| 1560 |
* when the stream is DISABLED, this means sdw_prepare_stream() |
| 1561 |
* is called as a result of an underflow or a resume operation. |
| 1562 |
* In this case, the bus parameters shall not be recomputed, but |
| 1563 |
* still need to be re-applied |
| 1564 |
*/ |
| 1565 |
if (stream->state == SDW_STREAM_DISABLED) |
| 1566 |
update_params = false; |
| 1567 |
|
| 1568 |
ret = _sdw_prepare_stream(stream, update_params); |
| 1569 |
|
| 1570 |
state_err: |
| 1571 |
sdw_release_bus_lock(stream); |
| 1572 |
return ret; |
| 1573 |
} |
| 1574 |
EXPORT_SYMBOL(sdw_prepare_stream); |
| 1575 |
|
| 1576 |
static int _sdw_enable_stream(struct sdw_stream_runtime *stream) |
| 1577 |
{ |
| 1578 |
struct sdw_master_runtime *m_rt; |
| 1579 |
struct sdw_bus *bus; |
| 1580 |
int ret; |
| 1581 |
|
| 1582 |
/* Enable Master(s) and Slave(s) port(s) associated with stream */ |
| 1583 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1584 |
bus = m_rt->bus; |
| 1585 |
|
| 1586 |
/* Program params */ |
| 1587 |
ret = sdw_program_params(bus, false); |
| 1588 |
if (ret < 0) { |
| 1589 |
dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); |
| 1590 |
return ret; |
| 1591 |
} |
| 1592 |
|
| 1593 |
/* Enable port(s) */ |
| 1594 |
ret = sdw_enable_disable_ports(m_rt, true); |
| 1595 |
if (ret < 0) { |
| 1596 |
dev_err(bus->dev, |
| 1597 |
"Enable port(s) failed ret: %d\n", ret); |
| 1598 |
return ret; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
ret = do_bank_switch(stream); |
| 1603 |
if (ret < 0) { |
| 1604 |
pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); |
| 1605 |
return ret; |
| 1606 |
} |
| 1607 |
|
| 1608 |
stream->state = SDW_STREAM_ENABLED; |
| 1609 |
return 0; |
| 1610 |
} |
| 1611 |
|
| 1612 |
/** |
| 1613 |
* sdw_enable_stream() - Enable SoundWire stream |
| 1614 |
* |
| 1615 |
* @stream: Soundwire stream |
| 1616 |
* |
| 1617 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1618 |
*/ |
| 1619 |
int sdw_enable_stream(struct sdw_stream_runtime *stream) |
| 1620 |
{ |
| 1621 |
int ret; |
| 1622 |
|
| 1623 |
if (!stream) { |
| 1624 |
pr_err("SoundWire: Handle not found for stream\n"); |
| 1625 |
return -EINVAL; |
| 1626 |
} |
| 1627 |
|
| 1628 |
sdw_acquire_bus_lock(stream); |
| 1629 |
|
| 1630 |
if (stream->state == SDW_STREAM_ENABLED) { |
| 1631 |
ret = 0; |
| 1632 |
goto state_err; |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (stream->state != SDW_STREAM_PREPARED && |
| 1636 |
stream->state != SDW_STREAM_DISABLED) { |
| 1637 |
pr_err("%s: %s: inconsistent state state %d\n", |
| 1638 |
__func__, stream->name, stream->state); |
| 1639 |
ret = -EINVAL; |
| 1640 |
goto state_err; |
| 1641 |
} |
| 1642 |
|
| 1643 |
ret = _sdw_enable_stream(stream); |
| 1644 |
|
| 1645 |
state_err: |
| 1646 |
sdw_release_bus_lock(stream); |
| 1647 |
return ret; |
| 1648 |
} |
| 1649 |
EXPORT_SYMBOL(sdw_enable_stream); |
| 1650 |
|
| 1651 |
static int _sdw_disable_stream(struct sdw_stream_runtime *stream) |
| 1652 |
{ |
| 1653 |
struct sdw_master_runtime *m_rt; |
| 1654 |
int ret; |
| 1655 |
|
| 1656 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1657 |
struct sdw_bus *bus = m_rt->bus; |
| 1658 |
|
| 1659 |
/* Disable port(s) */ |
| 1660 |
ret = sdw_enable_disable_ports(m_rt, false); |
| 1661 |
if (ret < 0) { |
| 1662 |
dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); |
| 1663 |
return ret; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
stream->state = SDW_STREAM_DISABLED; |
| 1667 |
|
| 1668 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1669 |
struct sdw_bus *bus = m_rt->bus; |
| 1670 |
|
| 1671 |
/* Program params */ |
| 1672 |
ret = sdw_program_params(bus, false); |
| 1673 |
if (ret < 0) { |
| 1674 |
dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); |
| 1675 |
return ret; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
ret = do_bank_switch(stream); |
| 1680 |
if (ret < 0) { |
| 1681 |
pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); |
| 1682 |
return ret; |
| 1683 |
} |
| 1684 |
|
| 1685 |
/* make sure alternate bank (previous current) is also disabled */ |
| 1686 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1687 |
struct sdw_bus *bus = m_rt->bus; |
| 1688 |
|
| 1689 |
/* Disable port(s) */ |
| 1690 |
ret = sdw_enable_disable_ports(m_rt, false); |
| 1691 |
if (ret < 0) { |
| 1692 |
dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); |
| 1693 |
return ret; |
| 1694 |
} |
| 1695 |
} |
| 1696 |
|
| 1697 |
return 0; |
| 1698 |
} |
| 1699 |
|
| 1700 |
/** |
| 1701 |
* sdw_disable_stream() - Disable SoundWire stream |
| 1702 |
* |
| 1703 |
* @stream: Soundwire stream |
| 1704 |
* |
| 1705 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1706 |
*/ |
| 1707 |
int sdw_disable_stream(struct sdw_stream_runtime *stream) |
| 1708 |
{ |
| 1709 |
int ret; |
| 1710 |
|
| 1711 |
if (!stream) { |
| 1712 |
pr_err("SoundWire: Handle not found for stream\n"); |
| 1713 |
return -EINVAL; |
| 1714 |
} |
| 1715 |
|
| 1716 |
sdw_acquire_bus_lock(stream); |
| 1717 |
|
| 1718 |
if (stream->state == SDW_STREAM_DISABLED) { |
| 1719 |
ret = 0; |
| 1720 |
goto state_err; |
| 1721 |
} |
| 1722 |
|
| 1723 |
if (stream->state != SDW_STREAM_ENABLED) { |
| 1724 |
pr_err("%s: %s: inconsistent state state %d\n", |
| 1725 |
__func__, stream->name, stream->state); |
| 1726 |
ret = -EINVAL; |
| 1727 |
goto state_err; |
| 1728 |
} |
| 1729 |
|
| 1730 |
ret = _sdw_disable_stream(stream); |
| 1731 |
|
| 1732 |
state_err: |
| 1733 |
sdw_release_bus_lock(stream); |
| 1734 |
return ret; |
| 1735 |
} |
| 1736 |
EXPORT_SYMBOL(sdw_disable_stream); |
| 1737 |
|
| 1738 |
static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream) |
| 1739 |
{ |
| 1740 |
struct sdw_master_runtime *m_rt; |
| 1741 |
struct sdw_port_runtime *p_rt; |
| 1742 |
unsigned int multi_lane_bandwidth; |
| 1743 |
unsigned int bandwidth; |
| 1744 |
struct sdw_bus *bus; |
| 1745 |
int state = stream->state; |
| 1746 |
int ret = 0; |
| 1747 |
|
| 1748 |
/* |
| 1749 |
* first mark the state as DEPREPARED so that it is not taken into account |
| 1750 |
* for bit allocation |
| 1751 |
*/ |
| 1752 |
stream->state = SDW_STREAM_DEPREPARED; |
| 1753 |
|
| 1754 |
list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1755 |
bus = m_rt->bus; |
| 1756 |
/* De-prepare port(s) */ |
| 1757 |
ret = sdw_prep_deprep_ports(m_rt, false); |
| 1758 |
if (ret < 0) { |
| 1759 |
dev_err(bus->dev, |
| 1760 |
"De-prepare port(s) failed: %d\n", ret); |
| 1761 |
stream->state = state; |
| 1762 |
return ret; |
| 1763 |
} |
| 1764 |
|
| 1765 |
multi_lane_bandwidth = 0; |
| 1766 |
|
| 1767 |
list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 1768 |
if (!p_rt->lane) |
| 1769 |
continue; |
| 1770 |
|
| 1771 |
bandwidth = m_rt->stream->params.rate * hweight32(p_rt->ch_mask) * |
| 1772 |
m_rt->stream->params.bps; |
| 1773 |
multi_lane_bandwidth += bandwidth; |
| 1774 |
bus->lane_used_bandwidth[p_rt->lane] -= bandwidth; |
| 1775 |
if (!bus->lane_used_bandwidth[p_rt->lane]) |
| 1776 |
p_rt->lane = 0; |
| 1777 |
} |
| 1778 |
/* TODO: Update this during Device-Device support */ |
| 1779 |
bandwidth = m_rt->stream->params.rate * m_rt->ch_count * m_rt->stream->params.bps; |
| 1780 |
bus->params.bandwidth -= bandwidth - multi_lane_bandwidth; |
| 1781 |
|
| 1782 |
/* Compute params */ |
| 1783 |
if (bus->compute_params) { |
| 1784 |
ret = bus->compute_params(bus, stream); |
| 1785 |
if (ret < 0) { |
| 1786 |
dev_err(bus->dev, "Compute params failed: %d\n", |
| 1787 |
ret); |
| 1788 |
stream->state = state; |
| 1789 |
return ret; |
| 1790 |
} |
| 1791 |
} |
| 1792 |
|
| 1793 |
/* Program params */ |
| 1794 |
ret = sdw_program_params(bus, false); |
| 1795 |
if (ret < 0) { |
| 1796 |
dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); |
| 1797 |
stream->state = state; |
| 1798 |
return ret; |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
return do_bank_switch(stream); |
| 1803 |
} |
| 1804 |
|
| 1805 |
/** |
| 1806 |
* sdw_deprepare_stream() - Deprepare SoundWire stream |
| 1807 |
* |
| 1808 |
* @stream: Soundwire stream |
| 1809 |
* |
| 1810 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1811 |
*/ |
| 1812 |
int sdw_deprepare_stream(struct sdw_stream_runtime *stream) |
| 1813 |
{ |
| 1814 |
int ret; |
| 1815 |
|
| 1816 |
if (!stream) { |
| 1817 |
pr_err("SoundWire: Handle not found for stream\n"); |
| 1818 |
return -EINVAL; |
| 1819 |
} |
| 1820 |
|
| 1821 |
sdw_acquire_bus_lock(stream); |
| 1822 |
|
| 1823 |
if (stream->state == SDW_STREAM_DEPREPARED) { |
| 1824 |
ret = 0; |
| 1825 |
goto state_err; |
| 1826 |
} |
| 1827 |
|
| 1828 |
if (stream->state != SDW_STREAM_PREPARED && |
| 1829 |
stream->state != SDW_STREAM_DISABLED) { |
| 1830 |
pr_err("%s: %s: inconsistent state state %d\n", |
| 1831 |
__func__, stream->name, stream->state); |
| 1832 |
ret = -EINVAL; |
| 1833 |
goto state_err; |
| 1834 |
} |
| 1835 |
|
| 1836 |
ret = _sdw_deprepare_stream(stream); |
| 1837 |
|
| 1838 |
state_err: |
| 1839 |
sdw_release_bus_lock(stream); |
| 1840 |
return ret; |
| 1841 |
} |
| 1842 |
EXPORT_SYMBOL(sdw_deprepare_stream); |
| 1843 |
|
| 1844 |
static int set_stream(struct snd_pcm_substream *substream, |
| 1845 |
struct sdw_stream_runtime *sdw_stream) |
| 1846 |
{ |
| 1847 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 1848 |
struct snd_soc_dai *dai; |
| 1849 |
int ret = 0; |
| 1850 |
int i; |
| 1851 |
|
| 1852 |
/* Set stream pointer on all DAIs */ |
| 1853 |
for_each_rtd_dais(rtd, i, dai) { |
| 1854 |
ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream); |
| 1855 |
if (ret < 0) { |
| 1856 |
dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name); |
| 1857 |
break; |
| 1858 |
} |
| 1859 |
} |
| 1860 |
|
| 1861 |
return ret; |
| 1862 |
} |
| 1863 |
|
| 1864 |
/** |
| 1865 |
* sdw_alloc_stream() - Allocate and return stream runtime |
| 1866 |
* |
| 1867 |
* @stream_name: SoundWire stream name |
| 1868 |
* @type: stream type (could be PCM ,PDM or BPT) |
| 1869 |
* |
| 1870 |
* Allocates a SoundWire stream runtime instance. |
| 1871 |
* sdw_alloc_stream should be called only once per stream. Typically |
| 1872 |
* invoked from ALSA/ASoC machine/platform driver. |
| 1873 |
*/ |
| 1874 |
struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name, enum sdw_stream_type type) |
| 1875 |
{ |
| 1876 |
struct sdw_stream_runtime *stream; |
| 1877 |
|
| 1878 |
stream = kzalloc_obj(*stream); |
| 1879 |
if (!stream) |
| 1880 |
return NULL; |
| 1881 |
|
| 1882 |
stream->name = stream_name; |
| 1883 |
INIT_LIST_HEAD(&stream->master_list); |
| 1884 |
stream->state = SDW_STREAM_ALLOCATED; |
| 1885 |
stream->m_rt_count = 0; |
| 1886 |
stream->type = type; |
| 1887 |
|
| 1888 |
return stream; |
| 1889 |
} |
| 1890 |
EXPORT_SYMBOL(sdw_alloc_stream); |
| 1891 |
|
| 1892 |
/** |
| 1893 |
* sdw_startup_stream() - Startup SoundWire stream |
| 1894 |
* |
| 1895 |
* @sdw_substream: Soundwire stream |
| 1896 |
* |
| 1897 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1898 |
*/ |
| 1899 |
int sdw_startup_stream(void *sdw_substream) |
| 1900 |
{ |
| 1901 |
struct snd_pcm_substream *substream = sdw_substream; |
| 1902 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 1903 |
struct sdw_stream_runtime *sdw_stream; |
| 1904 |
char *name; |
| 1905 |
int ret; |
| 1906 |
|
| 1907 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1908 |
name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); |
| 1909 |
else |
| 1910 |
name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name); |
| 1911 |
|
| 1912 |
if (!name) |
| 1913 |
return -ENOMEM; |
| 1914 |
|
| 1915 |
sdw_stream = sdw_alloc_stream(name, SDW_STREAM_PCM); |
| 1916 |
if (!sdw_stream) { |
| 1917 |
dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name); |
| 1918 |
ret = -ENOMEM; |
| 1919 |
goto error; |
| 1920 |
} |
| 1921 |
|
| 1922 |
ret = set_stream(substream, sdw_stream); |
| 1923 |
if (ret < 0) |
| 1924 |
goto release_stream; |
| 1925 |
return 0; |
| 1926 |
|
| 1927 |
release_stream: |
| 1928 |
sdw_release_stream(sdw_stream); |
| 1929 |
set_stream(substream, NULL); |
| 1930 |
error: |
| 1931 |
kfree(name); |
| 1932 |
return ret; |
| 1933 |
} |
| 1934 |
EXPORT_SYMBOL(sdw_startup_stream); |
| 1935 |
|
| 1936 |
/** |
| 1937 |
* sdw_shutdown_stream() - Shutdown SoundWire stream |
| 1938 |
* |
| 1939 |
* @sdw_substream: Soundwire stream |
| 1940 |
* |
| 1941 |
* Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1942 |
*/ |
| 1943 |
void sdw_shutdown_stream(void *sdw_substream) |
| 1944 |
{ |
| 1945 |
struct snd_pcm_substream *substream = sdw_substream; |
| 1946 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 1947 |
struct sdw_stream_runtime *sdw_stream; |
| 1948 |
struct snd_soc_dai *dai; |
| 1949 |
|
| 1950 |
/* Find stream from first CPU DAI */ |
| 1951 |
dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 1952 |
|
| 1953 |
sdw_stream = snd_soc_dai_get_stream(dai, substream->stream); |
| 1954 |
|
| 1955 |
if (IS_ERR(sdw_stream)) { |
| 1956 |
dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name); |
| 1957 |
return; |
| 1958 |
} |
| 1959 |
|
| 1960 |
/* release memory */ |
| 1961 |
kfree(sdw_stream->name); |
| 1962 |
sdw_release_stream(sdw_stream); |
| 1963 |
|
| 1964 |
/* clear DAI data */ |
| 1965 |
set_stream(substream, NULL); |
| 1966 |
} |
| 1967 |
EXPORT_SYMBOL(sdw_shutdown_stream); |
| 1968 |
|
| 1969 |
/** |
| 1970 |
* sdw_release_stream() - Free the assigned stream runtime |
| 1971 |
* |
| 1972 |
* @stream: SoundWire stream runtime |
| 1973 |
* |
| 1974 |
* sdw_release_stream should be called only once per stream |
| 1975 |
*/ |
| 1976 |
void sdw_release_stream(struct sdw_stream_runtime *stream) |
| 1977 |
{ |
| 1978 |
kfree(stream); |
| 1979 |
} |
| 1980 |
EXPORT_SYMBOL(sdw_release_stream); |
| 1981 |
|
| 1982 |
/** |
| 1983 |
* sdw_stream_add_master() - Allocate and add master runtime to a stream |
| 1984 |
* |
| 1985 |
* @bus: SDW Bus instance |
| 1986 |
* @stream_config: Stream configuration for audio stream |
| 1987 |
* @port_config: Port configuration for audio stream |
| 1988 |
* @num_ports: Number of ports |
| 1989 |
* @stream: SoundWire stream |
| 1990 |
*/ |
| 1991 |
int sdw_stream_add_master(struct sdw_bus *bus, |
| 1992 |
struct sdw_stream_config *stream_config, |
| 1993 |
const struct sdw_port_config *port_config, |
| 1994 |
unsigned int num_ports, |
| 1995 |
struct sdw_stream_runtime *stream) |
| 1996 |
{ |
| 1997 |
struct sdw_master_runtime *m_rt; |
| 1998 |
bool alloc_master_rt = false; |
| 1999 |
int ret; |
| 2000 |
|
| 2001 |
mutex_lock(&bus->bus_lock); |
| 2002 |
|
| 2003 |
/* |
| 2004 |
* For multi link streams, add the second master only if |
| 2005 |
* the bus supports it. |
| 2006 |
* Check if bus->multi_link is set |
| 2007 |
*/ |
| 2008 |
if (!bus->multi_link && stream->m_rt_count > 0) { |
| 2009 |
dev_err(bus->dev, |
| 2010 |
"Multilink not supported, link %d\n", bus->link_id); |
| 2011 |
ret = -EINVAL; |
| 2012 |
goto unlock; |
| 2013 |
} |
| 2014 |
|
| 2015 |
/* |
| 2016 |
* check if Master is already allocated (e.g. as a result of Slave adding |
| 2017 |
* it first), if so skip allocation and go to configuration |
| 2018 |
*/ |
| 2019 |
m_rt = sdw_master_rt_find(bus, stream); |
| 2020 |
if (!m_rt) { |
| 2021 |
m_rt = sdw_master_rt_alloc(bus, stream); |
| 2022 |
if (IS_ERR(m_rt)) { |
| 2023 |
ret = PTR_ERR(m_rt); |
| 2024 |
dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s: %d\n", |
| 2025 |
__func__, stream->name, ret); |
| 2026 |
goto unlock; |
| 2027 |
} |
| 2028 |
if (!m_rt) { |
| 2029 |
dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s\n", |
| 2030 |
__func__, stream->name); |
| 2031 |
ret = -ENOMEM; |
| 2032 |
goto unlock; |
| 2033 |
} |
| 2034 |
|
| 2035 |
alloc_master_rt = true; |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (!sdw_master_port_allocated(m_rt)) { |
| 2039 |
ret = sdw_master_port_alloc(m_rt, num_ports); |
| 2040 |
if (ret) |
| 2041 |
goto alloc_error; |
| 2042 |
|
| 2043 |
stream->m_rt_count++; |
| 2044 |
} |
| 2045 |
|
| 2046 |
ret = sdw_master_rt_config(m_rt, stream_config); |
| 2047 |
if (ret < 0) |
| 2048 |
goto unlock; |
| 2049 |
|
| 2050 |
ret = sdw_config_stream(bus->dev, stream, stream_config, false); |
| 2051 |
if (ret) |
| 2052 |
goto unlock; |
| 2053 |
|
| 2054 |
ret = sdw_master_port_config(m_rt, port_config); |
| 2055 |
|
| 2056 |
goto unlock; |
| 2057 |
|
| 2058 |
alloc_error: |
| 2059 |
/* |
| 2060 |
* we only cleanup what was allocated in this routine |
| 2061 |
*/ |
| 2062 |
if (alloc_master_rt) |
| 2063 |
sdw_master_rt_free(m_rt, stream); |
| 2064 |
unlock: |
| 2065 |
mutex_unlock(&bus->bus_lock); |
| 2066 |
return ret; |
| 2067 |
} |
| 2068 |
EXPORT_SYMBOL(sdw_stream_add_master); |
| 2069 |
|
| 2070 |
/** |
| 2071 |
* sdw_stream_remove_master() - Remove master from sdw_stream |
| 2072 |
* |
| 2073 |
* @bus: SDW Bus instance |
| 2074 |
* @stream: SoundWire stream |
| 2075 |
* |
| 2076 |
* This removes and frees port_rt and master_rt from a stream |
| 2077 |
*/ |
| 2078 |
int sdw_stream_remove_master(struct sdw_bus *bus, |
| 2079 |
struct sdw_stream_runtime *stream) |
| 2080 |
{ |
| 2081 |
struct sdw_master_runtime *m_rt, *_m_rt; |
| 2082 |
|
| 2083 |
mutex_lock(&bus->bus_lock); |
| 2084 |
|
| 2085 |
list_for_each_entry_safe(m_rt, _m_rt, |
| 2086 |
&stream->master_list, stream_node) { |
| 2087 |
if (m_rt->bus != bus) |
| 2088 |
continue; |
| 2089 |
|
| 2090 |
sdw_master_port_free(m_rt); |
| 2091 |
sdw_master_rt_free(m_rt, stream); |
| 2092 |
stream->m_rt_count--; |
| 2093 |
} |
| 2094 |
|
| 2095 |
if (list_empty(&stream->master_list)) |
| 2096 |
stream->state = SDW_STREAM_RELEASED; |
| 2097 |
|
| 2098 |
mutex_unlock(&bus->bus_lock); |
| 2099 |
|
| 2100 |
return 0; |
| 2101 |
} |
| 2102 |
EXPORT_SYMBOL(sdw_stream_remove_master); |
| 2103 |
|
| 2104 |
/** |
| 2105 |
* sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream |
| 2106 |
* |
| 2107 |
* @slave: SDW Slave instance |
| 2108 |
* @stream_config: Stream configuration for audio stream |
| 2109 |
* @stream: SoundWire stream |
| 2110 |
* @port_config: Port configuration for audio stream |
| 2111 |
* @num_ports: Number of ports |
| 2112 |
* |
| 2113 |
* It is expected that Slave is added before adding Master |
| 2114 |
* to the Stream. |
| 2115 |
* |
| 2116 |
*/ |
| 2117 |
int sdw_stream_add_slave(struct sdw_slave *slave, |
| 2118 |
struct sdw_stream_config *stream_config, |
| 2119 |
const struct sdw_port_config *port_config, |
| 2120 |
unsigned int num_ports, |
| 2121 |
struct sdw_stream_runtime *stream) |
| 2122 |
{ |
| 2123 |
struct sdw_slave_runtime *s_rt; |
| 2124 |
struct sdw_master_runtime *m_rt; |
| 2125 |
bool alloc_master_rt = false; |
| 2126 |
bool alloc_slave_rt = false; |
| 2127 |
|
| 2128 |
int ret; |
| 2129 |
|
| 2130 |
mutex_lock(&slave->bus->bus_lock); |
| 2131 |
|
| 2132 |
/* |
| 2133 |
* check if Master is already allocated, if so skip allocation |
| 2134 |
* and go to configuration |
| 2135 |
*/ |
| 2136 |
m_rt = sdw_master_rt_find(slave->bus, stream); |
| 2137 |
if (!m_rt) { |
| 2138 |
/* |
| 2139 |
* If this API is invoked by Slave first then m_rt is not valid. |
| 2140 |
* So, allocate m_rt and add Slave to it. |
| 2141 |
*/ |
| 2142 |
m_rt = sdw_master_rt_alloc(slave->bus, stream); |
| 2143 |
if (IS_ERR(m_rt)) { |
| 2144 |
ret = PTR_ERR(m_rt); |
| 2145 |
dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s: %d\n", |
| 2146 |
__func__, stream->name, ret); |
| 2147 |
goto unlock; |
| 2148 |
} |
| 2149 |
if (!m_rt) { |
| 2150 |
dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s\n", |
| 2151 |
__func__, stream->name); |
| 2152 |
ret = -ENOMEM; |
| 2153 |
goto unlock; |
| 2154 |
} |
| 2155 |
|
| 2156 |
alloc_master_rt = true; |
| 2157 |
} |
| 2158 |
|
| 2159 |
s_rt = sdw_slave_rt_find(slave, stream); |
| 2160 |
if (!s_rt) { |
| 2161 |
s_rt = sdw_slave_rt_alloc(slave, m_rt); |
| 2162 |
if (!s_rt) { |
| 2163 |
dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n", |
| 2164 |
stream->name); |
| 2165 |
ret = -ENOMEM; |
| 2166 |
goto alloc_error; |
| 2167 |
} |
| 2168 |
|
| 2169 |
alloc_slave_rt = true; |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (!sdw_slave_port_allocated(s_rt)) { |
| 2173 |
ret = sdw_slave_port_alloc(slave, s_rt, num_ports); |
| 2174 |
if (ret) |
| 2175 |
goto alloc_error; |
| 2176 |
} |
| 2177 |
|
| 2178 |
ret = sdw_master_rt_config(m_rt, stream_config); |
| 2179 |
if (ret) |
| 2180 |
goto unlock; |
| 2181 |
|
| 2182 |
ret = sdw_slave_rt_config(s_rt, stream_config); |
| 2183 |
if (ret) |
| 2184 |
goto unlock; |
| 2185 |
|
| 2186 |
ret = sdw_config_stream(&slave->dev, stream, stream_config, true); |
| 2187 |
if (ret) |
| 2188 |
goto unlock; |
| 2189 |
|
| 2190 |
ret = sdw_slave_port_config(slave, s_rt, port_config, |
| 2191 |
stream->type == SDW_STREAM_BPT); |
| 2192 |
if (ret) |
| 2193 |
goto unlock; |
| 2194 |
|
| 2195 |
/* |
| 2196 |
* Change stream state to CONFIGURED on first Slave add. |
| 2197 |
* Bus is not aware of number of Slave(s) in a stream at this |
| 2198 |
* point so cannot depend on all Slave(s) to be added in order to |
| 2199 |
* change stream state to CONFIGURED. |
| 2200 |
*/ |
| 2201 |
stream->state = SDW_STREAM_CONFIGURED; |
| 2202 |
goto unlock; |
| 2203 |
|
| 2204 |
alloc_error: |
| 2205 |
/* |
| 2206 |
* we only cleanup what was allocated in this routine. The 'else if' |
| 2207 |
* is intentional, the 'master_rt_free' will call sdw_slave_rt_free() |
| 2208 |
* internally. |
| 2209 |
*/ |
| 2210 |
if (alloc_master_rt) |
| 2211 |
sdw_master_rt_free(m_rt, stream); |
| 2212 |
else if (alloc_slave_rt) |
| 2213 |
sdw_slave_rt_free(slave, stream); |
| 2214 |
unlock: |
| 2215 |
mutex_unlock(&slave->bus->bus_lock); |
| 2216 |
return ret; |
| 2217 |
} |
| 2218 |
EXPORT_SYMBOL(sdw_stream_add_slave); |
| 2219 |
|
| 2220 |
/** |
| 2221 |
* sdw_stream_remove_slave() - Remove slave from sdw_stream |
| 2222 |
* |
| 2223 |
* @slave: SDW Slave instance |
| 2224 |
* @stream: SoundWire stream |
| 2225 |
* |
| 2226 |
* This removes and frees port_rt and slave_rt from a stream |
| 2227 |
*/ |
| 2228 |
int sdw_stream_remove_slave(struct sdw_slave *slave, |
| 2229 |
struct sdw_stream_runtime *stream) |
| 2230 |
{ |
| 2231 |
mutex_lock(&slave->bus->bus_lock); |
| 2232 |
|
| 2233 |
sdw_slave_port_free(slave, stream); |
| 2234 |
sdw_slave_rt_free(slave, stream); |
| 2235 |
|
| 2236 |
mutex_unlock(&slave->bus->bus_lock); |
| 2237 |
|
| 2238 |
return 0; |
| 2239 |
} |
| 2240 |
EXPORT_SYMBOL(sdw_stream_remove_slave); |