stream.c 47.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
// Copyright(c) 2015-18 Intel Corporation.

/*
 *  stream.c - SoundWire Bus stream operations.
 */

#include <linux/delay.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/slab.h>
14
#include <linux/soundwire/sdw_registers.h>
15
#include <linux/soundwire/sdw.h>
16
#include <sound/soc.h>
17 18
#include "bus.h"

19 20 21 22 23 24
/*
 * 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.
 */
25
int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
26 27
			96, 100, 120, 128, 150, 160, 250, 0,
			192, 200, 240, 256, 72, 144, 90, 180};
28
EXPORT_SYMBOL(sdw_rows);
29

30
int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
31
EXPORT_SYMBOL(sdw_cols);
32

33
int sdw_find_col_index(int col)
34 35 36 37
{
	int i;

	for (i = 0; i < SDW_FRAME_COLS; i++) {
38
		if (sdw_cols[i] == col)
39 40 41 42 43 44
			return i;
	}

	pr_warn("Requested column not found, selecting lowest column no: 2\n");
	return 0;
}
45
EXPORT_SYMBOL(sdw_find_col_index);
46

47
int sdw_find_row_index(int row)
48 49 50 51
{
	int i;

	for (i = 0; i < SDW_FRAME_ROWS; i++) {
52
		if (sdw_rows[i] == row)
53 54 55 56 57 58
			return i;
	}

	pr_warn("Requested row not found, selecting lowest row no: 48\n");
	return 0;
}
59
EXPORT_SYMBOL(sdw_find_row_index);
60

61
static int _sdw_program_slave_port_params(struct sdw_bus *bus,
62 63 64
					  struct sdw_slave *slave,
					  struct sdw_transport_params *t_params,
					  enum sdw_dpn_type type)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
	u32 addr1, addr2, addr3, addr4;
	int ret;
	u16 wbuf;

	if (bus->params.next_bank) {
		addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
		addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
		addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
		addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
	} else {
		addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
		addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
		addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
		addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
	}

	/* Program DPN_OffsetCtrl2 registers */
	ret = sdw_write(slave, addr1, t_params->offset2);
	if (ret < 0) {
85
		dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
86 87 88 89 90 91
		return ret;
	}

	/* Program DPN_BlockCtrl3 register */
	ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
	if (ret < 0) {
92
		dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
93 94 95 96 97
		return ret;
	}

	/*
	 * Data ports are FULL, SIMPLE and REDUCED. This function handles
Vinod Koul's avatar
Vinod Koul committed
98
	 * FULL and REDUCED only and beyond this point only FULL is
99 100 101 102 103 104
	 * handled, so bail out if we are not FULL data port type
	 */
	if (type != SDW_DPN_FULL)
		return ret;

	/* Program DPN_SampleCtrl2 register */
105
	wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1);
106 107 108

	ret = sdw_write(slave, addr3, wbuf);
	if (ret < 0) {
109
		dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
110 111 112 113
		return ret;
	}

	/* Program DPN_HCtrl register */
114 115
	wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart);
	wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop);
116 117 118

	ret = sdw_write(slave, addr4, wbuf);
	if (ret < 0)
119
		dev_err(bus->dev, "DPN_HCtrl register write failed\n");
120 121 122 123 124

	return ret;
}

static int sdw_program_slave_port_params(struct sdw_bus *bus,
125 126
					 struct sdw_slave_runtime *s_rt,
					 struct sdw_port_runtime *p_rt)
127 128 129 130 131 132 133 134 135
{
	struct sdw_transport_params *t_params = &p_rt->transport_params;
	struct sdw_port_params *p_params = &p_rt->port_params;
	struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
	u32 addr1, addr2, addr3, addr4, addr5, addr6;
	struct sdw_dpn_prop *dpn_prop;
	int ret;
	u8 wbuf;

136 137 138
	if (s_rt->slave->is_mockup_device)
		return 0;

139
	dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
140 141
					  s_rt->direction,
					  t_params->port_num);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	if (!dpn_prop)
		return -EINVAL;

	addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
	addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);

	if (bus->params.next_bank) {
		addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
		addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
		addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
		addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);

	} else {
		addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
		addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
		addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
		addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
	}

	/* Program DPN_PortCtrl register */
162 163
	wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode);
	wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode);
164 165 166 167

	ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
	if (ret < 0) {
		dev_err(&s_rt->slave->dev,
168
			"DPN_PortCtrl register write failed for port %d\n",
169 170 171 172
			t_params->port_num);
		return ret;
	}

173 174 175 176 177 178 179 180 181
	if (!dpn_prop->read_only_wordlength) {
		/* Program DPN_BlockCtrl1 register */
		ret = sdw_write(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;
		}
182 183 184 185 186 187 188
	}

	/* Program DPN_SampleCtrl1 register */
	wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
	ret = sdw_write(s_rt->slave, addr3, wbuf);
	if (ret < 0) {
		dev_err(&s_rt->slave->dev,
189
			"DPN_SampleCtrl1 register write failed for port %d\n",
190 191 192 193 194 195 196 197
			t_params->port_num);
		return ret;
	}

	/* Program DPN_OffsetCtrl1 registers */
	ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
	if (ret < 0) {
		dev_err(&s_rt->slave->dev,
198
			"DPN_OffsetCtrl1 register write failed for port %d\n",
199 200 201 202 203 204 205 206 207
			t_params->port_num);
		return ret;
	}

	/* Program DPN_BlockCtrl2 register*/
	if (t_params->blk_grp_ctrl_valid) {
		ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
		if (ret < 0) {
			dev_err(&s_rt->slave->dev,
208
				"DPN_BlockCtrl2 reg write failed for port %d\n",
209 210 211 212 213 214 215 216 217 218
				t_params->port_num);
			return ret;
		}
	}

	/* program DPN_LaneCtrl register */
	if (slave_prop->lane_control_support) {
		ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
		if (ret < 0) {
			dev_err(&s_rt->slave->dev,
219
				"DPN_LaneCtrl register write failed for port %d\n",
220 221 222 223 224 225 226
				t_params->port_num);
			return ret;
		}
	}

	if (dpn_prop->type != SDW_DPN_SIMPLE) {
		ret = _sdw_program_slave_port_params(bus, s_rt->slave,
227
						     t_params, dpn_prop->type);
228 229
		if (ret < 0)
			dev_err(&s_rt->slave->dev,
230
				"Transport reg write failed for port: %d\n",
231 232 233 234 235 236 237
				t_params->port_num);
	}

	return ret;
}

static int sdw_program_master_port_params(struct sdw_bus *bus,
238
					  struct sdw_port_runtime *p_rt)
239 240 241 242 243
{
	int ret;

	/*
	 * we need to set transport and port parameters for the port.
Vinod Koul's avatar
Vinod Koul committed
244
	 * Transport parameters refers to the sample interval, offsets and
245 246 247 248 249 250 251 252 253 254
	 * 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;

	return bus->port_ops->dpn_set_port_params(bus,
255 256
						  &p_rt->port_params,
						  bus->params.next_bank);
257 258 259 260 261 262 263 264 265 266
}

/**
 * sdw_program_port_params() - Programs transport parameters of Master(s)
 * and Slave(s)
 *
 * @m_rt: Master stream runtime
 */
static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
{
267
	struct sdw_slave_runtime *s_rt;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	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;
}

291 292 293 294 295 296 297 298 299 300 301 302
/**
 * 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
 */
static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
303 304 305
					  struct sdw_slave_runtime *s_rt,
					  struct sdw_port_runtime *p_rt,
					  bool en)
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
{
	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)
321
		ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
322
	else
323
		ret = sdw_write(s_rt->slave, addr, 0x0);
324 325 326

	if (ret < 0)
		dev_err(&s_rt->slave->dev,
327
			"Slave chn_en reg write failed:%d port:%d\n",
328 329 330 331 332 333
			ret, t_params->port_num);

	return ret;
}

static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
334 335
					   struct sdw_port_runtime *p_rt,
					   bool en)
336 337 338 339
{
	struct sdw_transport_params *t_params = &p_rt->transport_params;
	struct sdw_bus *bus = m_rt->bus;
	struct sdw_enable_ch enable_ch;
340
	int ret;
341 342 343 344 345 346 347 348

	enable_ch.port_num = p_rt->num;
	enable_ch.ch_mask = p_rt->ch_mask;
	enable_ch.enable = en;

	/* Perform Master port channel(s) enable/disable */
	if (bus->port_ops->dpn_port_enable_ch) {
		ret = bus->port_ops->dpn_port_enable_ch(bus,
349 350
							&enable_ch,
							bus->params.next_bank);
351 352
		if (ret < 0) {
			dev_err(bus->dev,
353
				"Master chn_en write failed:%d port:%d\n",
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
				ret, t_params->port_num);
			return ret;
		}
	} else {
		dev_err(bus->dev,
			"dpn_port_enable_ch not supported, %s failed\n",
			en ? "enable" : "disable");
		return -EINVAL;
	}

	return 0;
}

/**
 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
 * Slave(s)
 *
 * @m_rt: Master stream runtime
 * @en: mode (enable/disable)
 */
static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
{
	struct sdw_port_runtime *s_port, *m_port;
377
	struct sdw_slave_runtime *s_rt;
378 379 380 381 382 383
	int ret = 0;

	/* Enable/Disable Slave port(s) */
	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
		list_for_each_entry(s_port, &s_rt->port_list, port_node) {
			ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
384
							     s_port, en);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
			if (ret < 0)
				return ret;
		}
	}

	/* Enable/Disable Master port(s) */
	list_for_each_entry(m_port, &m_rt->port_list, port_node) {
		ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
401 402
			    struct sdw_prepare_ch prep_ch,
			    enum sdw_port_prep_ops cmd)
403 404 405 406 407 408 409 410
{
	const struct sdw_slave_ops *ops = s_rt->slave->ops;
	int ret;

	if (ops->port_prep) {
		ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
		if (ret < 0) {
			dev_err(&s_rt->slave->dev,
411 412
				"Slave Port Prep cmd %d failed: %d\n",
				cmd, ret);
413 414 415 416 417 418 419 420
			return ret;
		}
	}

	return 0;
}

static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
421 422 423
				       struct sdw_slave_runtime *s_rt,
				       struct sdw_port_runtime *p_rt,
				       bool prep)
424
{
425
	struct completion *port_ready;
426 427 428 429 430 431 432 433 434 435
	struct sdw_dpn_prop *dpn_prop;
	struct sdw_prepare_ch prep_ch;
	bool intr = false;
	int ret = 0, val;
	u32 addr;

	prep_ch.num = p_rt->num;
	prep_ch.ch_mask = p_rt->ch_mask;

	dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
436 437
					  s_rt->direction,
					  prep_ch.num);
438 439
	if (!dpn_prop) {
		dev_err(bus->dev,
440
			"Slave Port:%d properties not found\n", prep_ch.num);
441 442 443 444 445 446 447
		return -EINVAL;
	}

	prep_ch.prepare = prep;

	prep_ch.bank = bus->params.next_bank;

448 449
	if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm ||
	    bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL)
450 451 452 453 454 455 456 457 458
		intr = true;

	/*
	 * 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,
459
					     dpn_prop->imp_def_interrupts);
460 461 462 463 464 465 466 467 468 469 470 471
		if (ret < 0)
			return ret;
	}

	/* Inform slave about the impending port prepare */
	sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);

	/* Prepare Slave port implementing CP_SM */
	if (!dpn_prop->simple_ch_prep_sm) {
		addr = SDW_DPN_PREPARECTRL(p_rt->num);

		if (prep)
472
			ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
473
		else
474
			ret = sdw_write(s_rt->slave, addr, 0x0);
475 476 477

		if (ret < 0) {
			dev_err(&s_rt->slave->dev,
478
				"Slave prep_ctrl reg write failed\n");
479 480 481 482 483
			return ret;
		}

		/* Wait for completion on port ready */
		port_ready = &s_rt->slave->port_ready[prep_ch.num];
484 485
		wait_for_completion_timeout(port_ready,
			msecs_to_jiffies(dpn_prop->ch_prep_timeout));
486 487

		val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
488 489
		if ((val < 0) || (val & p_rt->ch_mask)) {
			ret = (val < 0) ? val : -ETIMEDOUT;
490
			dev_err(&s_rt->slave->dev,
491 492
				"Chn prep failed for port %d: %d\n", prep_ch.num, ret);
			return ret;
493 494 495 496 497 498 499 500 501
		}
	}

	/* Inform slaves about ports prepared */
	sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);

	/* Disable interrupt after Port de-prepare */
	if (!prep && intr)
		ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
502
					     dpn_prop->imp_def_interrupts);
503 504 505 506 507

	return ret;
}

static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
508 509
					struct sdw_port_runtime *p_rt,
					bool prep)
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
	struct sdw_transport_params *t_params = &p_rt->transport_params;
	struct sdw_bus *bus = m_rt->bus;
	const struct sdw_master_port_ops *ops = bus->port_ops;
	struct sdw_prepare_ch prep_ch;
	int ret = 0;

	prep_ch.num = p_rt->num;
	prep_ch.ch_mask = p_rt->ch_mask;
	prep_ch.prepare = prep; /* Prepare/De-prepare */
	prep_ch.bank = bus->params.next_bank;

	/* Pre-prepare/Pre-deprepare port(s) */
	if (ops->dpn_port_prep) {
		ret = ops->dpn_port_prep(bus, &prep_ch);
		if (ret < 0) {
526
			dev_err(bus->dev, "Port prepare failed for port:%d\n",
527
				t_params->port_num);
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
			return ret;
		}
	}

	return ret;
}

/**
 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
 * Slave(s)
 *
 * @m_rt: Master runtime handle
 * @prep: Prepare or De-prepare
 */
static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
{
544
	struct sdw_slave_runtime *s_rt;
545 546 547 548 549 550 551
	struct sdw_port_runtime *p_rt;
	int ret = 0;

	/* Prepare/De-prepare Slave port(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_prep_deprep_slave_ports(m_rt->bus, s_rt,
552
							  p_rt, prep);
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
			if (ret < 0)
				return ret;
		}
	}

	/* Prepare/De-prepare Master port(s) */
	list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
		ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
		if (ret < 0)
			return ret;
	}

	return ret;
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
/**
 * 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.
 */
static int 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 = 0;

	if (bus->ops->set_bus_conf) {
		ret = bus->ops->set_bus_conf(bus, &bus->params);
		if (ret < 0)
			return ret;
	}

	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
		slave = s_rt->slave;

		if (slave->ops->bus_config) {
			ret = slave->ops->bus_config(slave, &bus->params);
594
			if (ret < 0) {
595
				dev_err(bus->dev, "Notify Slave: %d failed\n",
596
					slave->dev_num);
597 598
				return ret;
			}
599 600 601 602 603 604 605 606 607 608 609
		}
	}

	return ret;
}

/**
 * sdw_program_params() - Program transport and port parameters for Master(s)
 * and Slave(s)
 *
 * @bus: SDW bus instance
610
 * @prepare: true if sdw_program_params() is called by _prepare.
611
 */
612
static int sdw_program_params(struct sdw_bus *bus, bool prepare)
613
{
614
	struct sdw_master_runtime *m_rt;
615 616 617
	int ret = 0;

	list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
618 619 620 621 622 623 624 625 626 627 628

		/*
		 * 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;

629 630 631
		ret = sdw_program_port_params(m_rt);
		if (ret < 0) {
			dev_err(bus->dev,
632
				"Program transport params failed: %d\n", ret);
633 634 635 636 637
			return ret;
		}

		ret = sdw_notify_config(m_rt);
		if (ret < 0) {
638 639
			dev_err(bus->dev,
				"Notify bus config failed: %d\n", ret);
640 641 642 643 644 645 646 647 648
			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) {
649
			dev_err(bus->dev, "Enable channel failed: %d\n", ret);
650 651 652 653 654 655 656
			return ret;
		}
	}

	return ret;
}

657
static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
658 659
{
	int col_index, row_index;
660
	bool multi_link;
661
	struct sdw_msg *wr_msg;
662 663
	u8 *wbuf;
	int ret;
664 665 666 667 668 669
	u16 addr;

	wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
	if (!wr_msg)
		return -ENOMEM;

670 671
	bus->defer_msg.msg = wr_msg;

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	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;

	sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
689
		     SDW_MSG_FLAG_WRITE, wbuf);
690 691
	wr_msg->ssp_sync = true;

692 693
	/*
	 * Set the multi_link flag only when both the hardware supports
694
	 * and hardware-based sync is required
695
	 */
696
	multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links);
697 698 699 700 701 702

	if (multi_link)
		ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
	else
		ret = sdw_transfer(bus, wr_msg);

703
	if (ret < 0 && ret != -ENODATA) {
704
		dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
705 706 707
		goto error;
	}

708 709 710 711 712 713 714
	if (!multi_link) {
		kfree(wr_msg);
		kfree(wbuf);
		bus->defer_msg.msg = NULL;
		bus->params.curr_bank = !bus->params.curr_bank;
		bus->params.next_bank = !bus->params.next_bank;
	}
715 716 717 718 719 720 721

	return 0;

error:
	kfree(wbuf);
error_1:
	kfree(wr_msg);
722
	bus->defer_msg.msg = NULL;
723 724 725
	return ret;
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
/**
 * sdw_ml_sync_bank_switch: Multilink register bank switch
 *
 * @bus: SDW bus instance
 *
 * Caller function should free the buffers on error
 */
static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
{
	unsigned long time_left;

	if (!bus->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) {
745
		dev_err(bus->dev, "Controller Timed out on bank switch\n");
746 747 748 749 750 751 752 753 754 755 756 757 758 759
		return -ETIMEDOUT;
	}

	bus->params.curr_bank = !bus->params.curr_bank;
	bus->params.next_bank = !bus->params.next_bank;

	if (bus->defer_msg.msg) {
		kfree(bus->defer_msg.msg->buf);
		kfree(bus->defer_msg.msg);
	}

	return 0;
}

760 761
static int do_bank_switch(struct sdw_stream_runtime *stream)
{
762
	struct sdw_master_runtime *m_rt;
763
	const struct sdw_master_ops *ops;
764
	struct sdw_bus *bus;
765
	bool multi_link = false;
766
	int m_rt_count;
767 768
	int ret = 0;

769 770
	m_rt_count = stream->m_rt_count;

771 772 773 774
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		ops = bus->ops;

775
		if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) {
776 777 778 779
			multi_link = true;
			mutex_lock(&bus->msg_lock);
		}

780 781 782 783 784
		/* Pre-bank switch */
		if (ops->pre_bank_switch) {
			ret = ops->pre_bank_switch(bus);
			if (ret < 0) {
				dev_err(bus->dev,
785
					"Pre bank switch op failed: %d\n", ret);
786
				goto msg_unlock;
787 788 789
			}
		}

790 791 792 793 794 795
		/*
		 * 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.
		 */
796
		ret = sdw_bank_switch(bus, m_rt_count);
797
		if (ret < 0) {
798
			dev_err(bus->dev, "Bank switch failed: %d\n", ret);
799
			goto error;
800 801 802
		}
	}

803 804 805 806 807 808
	/*
	 * 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.
	 */
809 810 811
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		ops = bus->ops;
812

813 814 815 816 817
		/* Post-bank switch */
		if (ops->post_bank_switch) {
			ret = ops->post_bank_switch(bus);
			if (ret < 0) {
				dev_err(bus->dev,
818 819
					"Post bank switch op failed: %d\n",
					ret);
820
				goto error;
821
			}
822
		} else if (multi_link) {
823
			dev_err(bus->dev,
824
				"Post bank switch ops not implemented\n");
825 826 827 828 829 830 831 832 833 834 835
			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);
		if (ret < 0) {
			dev_err(bus->dev,
836
				"multi link bank switch failed: %d\n", ret);
837 838 839
			goto error;
		}

840
		if (multi_link)
841
			mutex_unlock(&bus->msg_lock);
842 843 844 845 846 847 848
	}

	return ret;

error:
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
849 850 851 852
		if (bus->defer_msg.msg) {
			kfree(bus->defer_msg.msg->buf);
			kfree(bus->defer_msg.msg);
		}
853 854 855 856 857 858 859 860 861
	}

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);
862 863 864 865 866 867
		}
	}

	return ret;
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list)
{
	struct sdw_port_runtime *p_rt;

	p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
	if (!p_rt)
		return NULL;

	list_add_tail(&p_rt->port_node, port_list);

	return p_rt;
}

static int sdw_port_config(struct sdw_port_runtime *p_rt,
			   struct sdw_port_config *port_config,
			   int port_index)
{
	p_rt->ch_mask = port_config[port_index].ch_mask;
	p_rt->num = port_config[port_index].num;

	/*
	 * TODO: Check port capabilities for requested configuration
	 */

	return 0;
}

static void sdw_port_free(struct sdw_port_runtime *p_rt)
{
	list_del(&p_rt->port_node);
	kfree(p_rt);
}

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
static void sdw_slave_port_free(struct sdw_slave *slave,
				struct sdw_stream_runtime *stream)
{
	struct sdw_port_runtime *p_rt, *_p_rt;
	struct sdw_master_runtime *m_rt;
	struct sdw_slave_runtime *s_rt;

	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
			if (s_rt->slave != slave)
				continue;

			list_for_each_entry_safe(p_rt, _p_rt,
						 &s_rt->port_list, port_node) {
				sdw_port_free(p_rt);
			}
		}
	}
}

static int sdw_slave_port_alloc(struct sdw_slave *slave,
				struct sdw_slave_runtime *s_rt,
				unsigned int num_config)
{
	struct sdw_port_runtime *p_rt;
	int i;

	/* 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;
}

static int 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;
}

static int sdw_slave_port_config(struct sdw_slave *slave,
				 struct sdw_slave_runtime *s_rt,
				 struct sdw_port_config *port_config)
{
	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
		 */
		ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num);
		if (ret < 0)
			return ret;

		ret = sdw_port_config(p_rt, port_config, i);
		if (ret < 0)
			return ret;
		i++;
	}

	return 0;
}

static void sdw_master_port_free(struct sdw_master_runtime *m_rt)
{
	struct sdw_port_runtime *p_rt, *_p_rt;

	list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
		sdw_port_free(p_rt);
	}
}

static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt,
				 unsigned int num_ports)
{
	struct sdw_port_runtime *p_rt;
	int 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;
}

static int sdw_master_port_config(struct sdw_master_runtime *m_rt,
				  struct 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;
}

1018
/**
1019
 * sdw_slave_rt_alloc() - Allocate a Slave runtime handle.
1020 1021 1022 1023 1024 1025
 *
 * @slave: Slave handle
 *
 * This function is to be called with bus_lock held.
 */
static struct sdw_slave_runtime
1026
*sdw_slave_rt_alloc(struct sdw_slave *slave)
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
{
	struct sdw_slave_runtime *s_rt;

	s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
	if (!s_rt)
		return NULL;

	INIT_LIST_HEAD(&s_rt->port_list);
	s_rt->slave = slave;

	return s_rt;
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
/**
 * sdw_slave_rt_config() - Configure a Slave runtime handle.
 *
 * @s_rt: Slave runtime handle
 * @stream_config: Stream configuration
 *
 * This function is to be called with bus_lock held.
 */
static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt,
			       struct sdw_stream_config *stream_config)
{
	s_rt->ch_count = stream_config->ch_count;
	s_rt->direction = stream_config->direction;

	return 0;
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
/**
 * sdw_slave_rt_free() - Free Slave(s) runtime handle
 *
 * @slave: Slave handle.
 * @stream: Stream runtime handle.
 *
 * This function is to be called with bus_lock held.
 */
static void sdw_slave_rt_free(struct sdw_slave *slave,
			      struct sdw_stream_runtime *stream)
{
	struct sdw_slave_runtime *s_rt, *_s_rt;
	struct sdw_master_runtime *m_rt;

	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		/* Retrieve Slave runtime handle */
		list_for_each_entry_safe(s_rt, _s_rt,
					 &m_rt->slave_rt_list, m_rt_node) {
			if (s_rt->slave == slave) {
				list_del(&s_rt->m_rt_node);
				kfree(s_rt);
				return;
			}
		}
	}
}

1084
static struct sdw_master_runtime
1085
*sdw_master_rt_find(struct sdw_bus *bus,
1086
		    struct sdw_stream_runtime *stream)
1087
{
1088
	struct sdw_master_runtime *m_rt;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

	/* Retrieve Bus handle if already available */
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		if (m_rt->bus == bus)
			return m_rt;
	}

	return NULL;
}

1099
/**
1100
 * sdw_master_rt_alloc() - Allocates a Master runtime handle
1101 1102 1103 1104 1105 1106 1107
 *
 * @bus: SDW bus instance
 * @stream: Stream runtime handle.
 *
 * This function is to be called with bus_lock held.
 */
static struct sdw_master_runtime
1108
*sdw_master_rt_alloc(struct sdw_bus *bus,
1109
		     struct sdw_stream_runtime *stream)
1110 1111 1112 1113 1114 1115 1116 1117
{
	struct sdw_master_runtime *m_rt;

	m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
	if (!m_rt)
		return NULL;

	/* Initialization of Master runtime handle */
1118
	INIT_LIST_HEAD(&m_rt->port_list);
1119
	INIT_LIST_HEAD(&m_rt->slave_rt_list);
1120
	list_add_tail(&m_rt->stream_node, &stream->master_list);
1121 1122 1123 1124 1125 1126 1127 1128 1129

	list_add_tail(&m_rt->bus_node, &bus->m_rt_list);

	m_rt->bus = bus;
	m_rt->stream = stream;

	return m_rt;
}

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
/**
 * 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.
 */

static int sdw_master_rt_config(struct sdw_master_runtime *m_rt,
				struct sdw_stream_config *stream_config)
{
	m_rt->ch_count = stream_config->ch_count;
	m_rt->direction = stream_config->direction;

	return 0;
}

1148
/**
1149
 * sdw_master_rt_free() - Free Master runtime handle
1150
 *
1151
 * @m_rt: Master runtime node
1152 1153 1154 1155
 * @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
1156
 * handle. If this is called first then sdw_slave_rt_free() will have
1157 1158
 * no effect as Slave(s) runtime handle would already be freed up.
 */
1159 1160
static void sdw_master_rt_free(struct sdw_master_runtime *m_rt,
			       struct sdw_stream_runtime *stream)
1161 1162 1163
{
	struct sdw_slave_runtime *s_rt, *_s_rt;

1164
	list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1165
		sdw_slave_port_free(s_rt->slave, stream);
1166
		sdw_slave_rt_free(s_rt->slave, stream);
1167
	}
1168

1169
	list_del(&m_rt->stream_node);
1170
	list_del(&m_rt->bus_node);
1171
	kfree(m_rt);
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
}

/**
 * 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.
 */
static int sdw_config_stream(struct device *dev,
1185 1186 1187
			     struct sdw_stream_runtime *stream,
			     struct sdw_stream_config *stream_config,
			     bool is_slave)
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
{
	/*
	 * 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 &&
1198
	    stream->params.rate != stream_config->frame_rate) {
1199
		dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1200 1201 1202 1203
		return -EINVAL;
	}

	if (stream->params.bps &&
1204
	    stream->params.bps != stream_config->bps) {
1205
		dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
		return -EINVAL;
	}

	stream->type = stream_config->type;
	stream->params.rate = stream_config->frame_rate;
	stream->params.bps = stream_config->bps;

	/* TODO: Update this check during Device-device support */
	if (is_slave)
		stream->params.ch_count += stream_config->ch_count;

	return 0;
}

/**
1221
 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1222
 *
1223 1224 1225
 * @slave: Slave handle
 * @direction: Data direction.
 * @port_num: Port number
1226
 */
1227 1228 1229
struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
					    enum sdw_data_direction direction,
					    unsigned int port_num)
1230
{
1231 1232 1233
	struct sdw_dpn_prop *dpn_prop;
	u8 num_ports;
	int i;
1234

1235 1236 1237 1238 1239 1240
	if (direction == SDW_DATA_DIR_TX) {
		num_ports = hweight32(slave->prop.source_ports);
		dpn_prop = slave->prop.src_dpn_prop;
	} else {
		num_ports = hweight32(slave->prop.sink_ports);
		dpn_prop = slave->prop.sink_dpn_prop;
1241 1242
	}

1243 1244 1245
	for (i = 0; i < num_ports; i++) {
		if (dpn_prop[i].num == port_num)
			return &dpn_prop[i];
1246 1247
	}

1248 1249
	return NULL;
}
1250

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
/**
 * 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.
 */
static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
{
	struct sdw_master_runtime *m_rt;
	struct sdw_bus *bus;
1265

1266 1267 1268
	/* Iterate for all Master(s) in Master list */
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
1269

1270 1271
		mutex_lock(&bus->bus_lock);
	}
1272 1273 1274
}

/**
1275
 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1276 1277
 *
 * @stream: SoundWire stream
1278
 *
1279 1280 1281
 * 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.
1282
 */
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
static void 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);
	}
}

static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
			       bool update_params)
1297 1298
{
	struct sdw_master_runtime *m_rt;
1299 1300 1301
	struct sdw_bus *bus = NULL;
	struct sdw_master_prop *prop;
	struct sdw_bus_params params;
1302 1303
	int ret;

1304 1305 1306 1307 1308
	/* 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(&params, &bus->params, sizeof(params));
1309

1310 1311 1312 1313 1314
		/* TODO: Support Asynchronous mode */
		if ((prop->max_clk_freq % stream->params.rate) != 0) {
			dev_err(bus->dev, "Async mode not supported\n");
			return -EINVAL;
		}
1315

1316 1317 1318
		if (!update_params)
			goto program_params;

1319 1320 1321 1322 1323
		/* 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;

1324 1325 1326 1327
		/* Compute params */
		if (bus->compute_params) {
			ret = bus->compute_params(bus);
			if (ret < 0) {
1328
				dev_err(bus->dev, "Compute params failed: %d\n",
1329 1330 1331 1332 1333
					ret);
				return ret;
			}
		}

1334
program_params:
1335
		/* Program params */
1336
		ret = sdw_program_params(bus, true);
1337
		if (ret < 0) {
1338
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1339 1340
			goto restore_params;
		}
1341 1342
	}

1343 1344 1345 1346 1347
	if (!bus) {
		pr_err("Configuration error in %s\n", __func__);
		return -EINVAL;
	}

1348 1349
	ret = do_bank_switch(stream);
	if (ret < 0) {
1350
		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1351 1352 1353
		goto restore_params;
	}

1354 1355 1356 1357 1358 1359
	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) {
1360
			dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1361
				ret);
1362 1363
			return ret;
		}
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	}

	stream->state = SDW_STREAM_PREPARED;

	return ret;

restore_params:
	memcpy(&bus->params, &params, sizeof(params));
	return ret;
}

/**
 * sdw_prepare_stream() - Prepare SoundWire stream
 *
 * @stream: Soundwire stream
 *
1380
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1381 1382 1383
 */
int sdw_prepare_stream(struct sdw_stream_runtime *stream)
{
1384
	bool update_params = true;
1385
	int ret;
1386 1387

	if (!stream) {
1388
		pr_err("SoundWire: Handle not found for stream\n");
1389 1390 1391
		return -EINVAL;
	}

1392
	sdw_acquire_bus_lock(stream);
1393

1394 1395 1396 1397 1398
	if (stream->state == SDW_STREAM_PREPARED) {
		ret = 0;
		goto state_err;
	}

1399 1400 1401 1402 1403 1404 1405 1406 1407
	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;
	}

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	/*
	 * 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;

	ret = _sdw_prepare_stream(stream, update_params);
1418

1419
state_err:
1420
	sdw_release_bus_lock(stream);
1421 1422 1423 1424 1425 1426
	return ret;
}
EXPORT_SYMBOL(sdw_prepare_stream);

static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
{
1427
	struct sdw_master_runtime *m_rt;
1428
	struct sdw_bus *bus = NULL;
1429 1430
	int ret;

1431 1432 1433
	/* Enable 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;
1434

1435
		/* Program params */
1436
		ret = sdw_program_params(bus, false);
1437
		if (ret < 0) {
1438
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1439 1440 1441 1442 1443 1444
			return ret;
		}

		/* Enable port(s) */
		ret = sdw_enable_disable_ports(m_rt, true);
		if (ret < 0) {
1445 1446
			dev_err(bus->dev,
				"Enable port(s) failed ret: %d\n", ret);
1447 1448
			return ret;
		}
1449 1450
	}

1451 1452 1453 1454 1455
	if (!bus) {
		pr_err("Configuration error in %s\n", __func__);
		return -EINVAL;
	}

1456 1457
	ret = do_bank_switch(stream);
	if (ret < 0) {
1458
		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
		return ret;
	}

	stream->state = SDW_STREAM_ENABLED;
	return 0;
}

/**
 * sdw_enable_stream() - Enable SoundWire stream
 *
 * @stream: Soundwire stream
 *
1471
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1472 1473 1474
 */
int sdw_enable_stream(struct sdw_stream_runtime *stream)
{
1475
	int ret;
1476 1477

	if (!stream) {
1478
		pr_err("SoundWire: Handle not found for stream\n");
1479 1480 1481
		return -EINVAL;
	}

1482
	sdw_acquire_bus_lock(stream);
1483

1484 1485 1486 1487 1488 1489 1490 1491
	if (stream->state != SDW_STREAM_PREPARED &&
	    stream->state != SDW_STREAM_DISABLED) {
		pr_err("%s: %s: inconsistent state state %d\n",
		       __func__, stream->name, stream->state);
		ret = -EINVAL;
		goto state_err;
	}

1492 1493
	ret = _sdw_enable_stream(stream);

1494
state_err:
1495
	sdw_release_bus_lock(stream);
1496 1497 1498 1499 1500 1501
	return ret;
}
EXPORT_SYMBOL(sdw_enable_stream);

static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
{
1502
	struct sdw_master_runtime *m_rt;
1503 1504
	int ret;

1505
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1506 1507
		struct sdw_bus *bus = m_rt->bus;

1508 1509 1510
		/* Disable port(s) */
		ret = sdw_enable_disable_ports(m_rt, false);
		if (ret < 0) {
1511
			dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1512 1513
			return ret;
		}
1514 1515 1516
	}
	stream->state = SDW_STREAM_DISABLED;

1517
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1518 1519
		struct sdw_bus *bus = m_rt->bus;

1520
		/* Program params */
1521
		ret = sdw_program_params(bus, false);
1522
		if (ret < 0) {
1523
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1524 1525
			return ret;
		}
1526 1527
	}

1528 1529
	ret = do_bank_switch(stream);
	if (ret < 0) {
1530
		pr_err("Bank switch failed: %d\n", ret);
1531 1532 1533 1534 1535
		return ret;
	}

	/* make sure alternate bank (previous current) is also disabled */
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1536 1537
		struct sdw_bus *bus = m_rt->bus;

1538 1539 1540 1541 1542 1543 1544 1545 1546
		/* Disable port(s) */
		ret = sdw_enable_disable_ports(m_rt, false);
		if (ret < 0) {
			dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
			return ret;
		}
	}

	return 0;
1547 1548 1549 1550 1551 1552 1553
}

/**
 * sdw_disable_stream() - Disable SoundWire stream
 *
 * @stream: Soundwire stream
 *
1554
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1555 1556 1557
 */
int sdw_disable_stream(struct sdw_stream_runtime *stream)
{
1558
	int ret;
1559 1560

	if (!stream) {
1561
		pr_err("SoundWire: Handle not found for stream\n");
1562 1563 1564
		return -EINVAL;
	}

1565
	sdw_acquire_bus_lock(stream);
1566

1567 1568 1569 1570 1571 1572 1573
	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;
	}

1574 1575
	ret = _sdw_disable_stream(stream);

1576
state_err:
1577
	sdw_release_bus_lock(stream);
1578 1579 1580 1581 1582 1583
	return ret;
}
EXPORT_SYMBOL(sdw_disable_stream);

static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
{
1584 1585
	struct sdw_master_runtime *m_rt;
	struct sdw_bus *bus;
1586 1587
	int ret = 0;

1588 1589 1590 1591 1592
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		/* De-prepare port(s) */
		ret = sdw_prep_deprep_ports(m_rt, false);
		if (ret < 0) {
1593 1594
			dev_err(bus->dev,
				"De-prepare port(s) failed: %d\n", ret);
1595 1596
			return ret;
		}
1597

1598 1599 1600
		/* TODO: Update this during Device-Device support */
		bus->params.bandwidth -= m_rt->stream->params.rate *
			m_rt->ch_count * m_rt->stream->params.bps;
1601

1602 1603 1604 1605
		/* Compute params */
		if (bus->compute_params) {
			ret = bus->compute_params(bus);
			if (ret < 0) {
1606
				dev_err(bus->dev, "Compute params failed: %d\n",
1607 1608 1609 1610 1611
					ret);
				return ret;
			}
		}

1612
		/* Program params */
1613
		ret = sdw_program_params(bus, false);
1614
		if (ret < 0) {
1615
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1616 1617
			return ret;
		}
1618 1619
	}

1620
	stream->state = SDW_STREAM_DEPREPARED;
1621 1622 1623 1624 1625 1626 1627 1628
	return do_bank_switch(stream);
}

/**
 * sdw_deprepare_stream() - Deprepare SoundWire stream
 *
 * @stream: Soundwire stream
 *
1629
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1630 1631 1632
 */
int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
{
1633
	int ret;
1634 1635

	if (!stream) {
1636
		pr_err("SoundWire: Handle not found for stream\n");
1637 1638 1639
		return -EINVAL;
	}

1640
	sdw_acquire_bus_lock(stream);
1641 1642 1643 1644 1645 1646 1647 1648 1649

	if (stream->state != SDW_STREAM_PREPARED &&
	    stream->state != SDW_STREAM_DISABLED) {
		pr_err("%s: %s: inconsistent state state %d\n",
		       __func__, stream->name, stream->state);
		ret = -EINVAL;
		goto state_err;
	}

1650 1651
	ret = _sdw_deprepare_stream(stream);

1652
state_err:
1653
	sdw_release_bus_lock(stream);
1654 1655 1656
	return ret;
}
EXPORT_SYMBOL(sdw_deprepare_stream);
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

static int set_stream(struct snd_pcm_substream *substream,
		      struct sdw_stream_runtime *sdw_stream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *dai;
	int ret = 0;
	int i;

	/* Set stream pointer on all DAIs */
	for_each_rtd_dais(rtd, i, dai) {
1668
		ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream);
1669
		if (ret < 0) {
1670
			dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name);
1671 1672 1673 1674 1675 1676 1677
			break;
		}
	}

	return ret;
}

1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
/**
 * sdw_alloc_stream() - Allocate and return stream runtime
 *
 * @stream_name: SoundWire stream name
 *
 * 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(const char *stream_name)
{
	struct sdw_stream_runtime *stream;

	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
	if (!stream)
		return NULL;

	stream->name = stream_name;
	INIT_LIST_HEAD(&stream->master_list);
	stream->state = SDW_STREAM_ALLOCATED;
	stream->m_rt_count = 0;

	return stream;
}
EXPORT_SYMBOL(sdw_alloc_stream);

1704 1705 1706
/**
 * sdw_startup_stream() - Startup SoundWire stream
 *
1707
 * @sdw_substream: Soundwire stream
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
 *
 * 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 = substream->private_data;
	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);
	if (!sdw_stream) {
1729
		dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name);
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
		ret = -ENOMEM;
		goto error;
	}

	ret = set_stream(substream, sdw_stream);
	if (ret < 0)
		goto release_stream;
	return 0;

release_stream:
	sdw_release_stream(sdw_stream);
	set_stream(substream, NULL);
error:
	kfree(name);
	return ret;
}
EXPORT_SYMBOL(sdw_startup_stream);

/**
 * sdw_shutdown_stream() - Shutdown SoundWire stream
 *
1751
 * @sdw_substream: Soundwire stream
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
 *
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
 */
void sdw_shutdown_stream(void *sdw_substream)
{
	struct snd_pcm_substream *substream = sdw_substream;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct sdw_stream_runtime *sdw_stream;
	struct snd_soc_dai *dai;

	/* Find stream from first CPU DAI */
	dai = asoc_rtd_to_cpu(rtd, 0);

1765
	sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
1766

1767
	if (IS_ERR(sdw_stream)) {
1768
		dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name);
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
		return;
	}

	/* release memory */
	kfree(sdw_stream->name);
	sdw_release_stream(sdw_stream);

	/* clear DAI data */
	set_stream(substream, NULL);
}
EXPORT_SYMBOL(sdw_shutdown_stream);
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862

/**
 * 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,
			  struct sdw_port_config *port_config,
			  unsigned int num_ports,
			  struct sdw_stream_runtime *stream)
{
	struct sdw_master_runtime *m_rt;
	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)
		goto skip_alloc_master_rt;

	m_rt = sdw_master_rt_alloc(bus, stream);
	if (!m_rt) {
		dev_err(bus->dev, "Master runtime alloc failed for stream:%s\n", stream->name);
		ret = -ENOMEM;
		goto unlock;
	}

	ret = sdw_master_rt_config(m_rt, stream_config);
	if (ret < 0)
		goto unlock;

skip_alloc_master_rt:
	ret = sdw_config_stream(bus->dev, stream, stream_config, false);
	if (ret)
		goto stream_error;

	ret = sdw_master_port_alloc(m_rt, num_ports);
	if (ret)
		goto stream_error;

	ret = sdw_master_port_config(m_rt, port_config);
	if (ret)
		goto stream_error;

	stream->m_rt_count++;

	goto unlock;

stream_error:
1863
	sdw_master_rt_free(m_rt, stream);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
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_master_port_free(m_rt);
1891
		sdw_master_rt_free(m_rt, stream);
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
		stream->m_rt_count--;
	}

	if (list_empty(&stream->master_list))
		stream->state = SDW_STREAM_RELEASED;

	mutex_unlock(&bus->bus_lock);

	return 0;
}
EXPORT_SYMBOL(sdw_stream_remove_master);

/**
 * 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,
			 struct sdw_port_config *port_config,
			 unsigned int num_ports,
			 struct sdw_stream_runtime *stream)
{
	struct sdw_slave_runtime *s_rt;
	struct sdw_master_runtime *m_rt;
	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)
		goto skip_alloc_master_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 (!m_rt) {
		dev_err(&slave->dev, "Master runtime alloc failed for stream:%s\n", stream->name);
		ret = -ENOMEM;
		goto error;
	}
	ret =  sdw_master_rt_config(m_rt, stream_config);
	if (ret < 0)
		goto stream_error;

skip_alloc_master_rt:
	s_rt = sdw_slave_rt_alloc(slave);
	if (!s_rt) {
		dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n", stream->name);
		ret = -ENOMEM;
		goto stream_error;
	}
	list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);

	ret = sdw_slave_rt_config(s_rt, stream_config);
	if (ret)
		goto stream_error;

	ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
	if (ret)
		goto stream_error;

	ret = sdw_slave_port_alloc(slave, s_rt, num_ports);
	if (ret)
		goto stream_error;

	ret = sdw_slave_port_config(slave, s_rt, port_config);
	if (ret)
		goto stream_error;

	/*
	 * 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 error;

stream_error:
	/*
	 * we hit error so cleanup the stream, release all Slave(s) and
	 * Master runtime
	 */
1990
	sdw_master_rt_free(m_rt, stream);
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
error:
	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);

	sdw_slave_port_free(slave, stream);
2011
	sdw_slave_rt_free(slave, stream);
2012 2013 2014 2015 2016 2017

	mutex_unlock(&slave->bus->bus_lock);

	return 0;
}
EXPORT_SYMBOL(sdw_stream_remove_slave);