stream.c 41.3 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 16 17
#include <linux/soundwire/sdw.h>
#include "bus.h"

S
Sanyog Kale 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
 *
 * The rows are arranged as per the array index value programmed
 * in register. The index 15 has dummy value 0 in order to fill hole.
 */
int rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
			96, 100, 120, 128, 150, 160, 250, 0,
			192, 200, 240, 256, 72, 144, 90, 180};

int cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};

static int sdw_find_col_index(int col)
{
	int i;

	for (i = 0; i < SDW_FRAME_COLS; i++) {
		if (cols[i] == col)
			return i;
	}

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

static int sdw_find_row_index(int row)
{
	int i;

	for (i = 0; i < SDW_FRAME_ROWS; i++) {
		if (rows[i] == row)
			return i;
	}

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

56
static int _sdw_program_slave_port_params(struct sdw_bus *bus,
57 58 59
					  struct sdw_slave *slave,
					  struct sdw_transport_params *t_params,
					  enum sdw_dpn_type type)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
{
	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) {
80
		dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
81 82 83 84 85 86
		return ret;
	}

	/* Program DPN_BlockCtrl3 register */
	ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
	if (ret < 0) {
87
		dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
88 89 90 91 92
		return ret;
	}

	/*
	 * Data ports are FULL, SIMPLE and REDUCED. This function handles
V
Vinod Koul 已提交
93
	 * FULL and REDUCED only and beyond this point only FULL is
94 95 96 97 98 99 100 101 102 103 104 105
	 * handled, so bail out if we are not FULL data port type
	 */
	if (type != SDW_DPN_FULL)
		return ret;

	/* Program DPN_SampleCtrl2 register */
	wbuf = (t_params->sample_interval - 1);
	wbuf &= SDW_DPN_SAMPLECTRL_HIGH;
	wbuf >>= SDW_REG_SHIFT(SDW_DPN_SAMPLECTRL_HIGH);

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

	/* Program DPN_HCtrl register */
	wbuf = t_params->hstart;
	wbuf <<= SDW_REG_SHIFT(SDW_DPN_HCTRL_HSTART);
	wbuf |= t_params->hstop;

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

	return ret;
}

static int sdw_program_slave_port_params(struct sdw_bus *bus,
123 124
					 struct sdw_slave_runtime *s_rt,
					 struct sdw_port_runtime *p_rt)
125 126 127 128 129 130 131 132 133 134
{
	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;

	dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
135 136
					  s_rt->direction,
					  t_params->port_num);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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 */
	wbuf = p_params->data_mode << SDW_REG_SHIFT(SDW_DPN_PORTCTRL_DATAMODE);
	wbuf |= p_params->flow_mode;

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

	/* Program DPN_BlockCtrl1 register */
	ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
	if (ret < 0) {
		dev_err(&s_rt->slave->dev,
172
			"DPN_BlockCtrl1 register write failed for port %d\n",
173 174 175 176 177 178 179 180 181
			t_params->port_num);
		return ret;
	}

	/* 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,
182
			"DPN_SampleCtrl1 register write failed for port %d\n",
183 184 185 186 187 188 189 190
			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,
191
			"DPN_OffsetCtrl1 register write failed for port %d\n",
192 193 194 195 196 197 198 199 200
			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,
201
				"DPN_BlockCtrl2 reg write failed for port %d\n",
202 203 204 205 206 207 208 209 210 211
				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,
212
				"DPN_LaneCtrl register write failed for port %d\n",
213 214 215 216 217 218 219
				t_params->port_num);
			return ret;
		}
	}

	if (dpn_prop->type != SDW_DPN_SIMPLE) {
		ret = _sdw_program_slave_port_params(bus, s_rt->slave,
220
						     t_params, dpn_prop->type);
221 222
		if (ret < 0)
			dev_err(&s_rt->slave->dev,
223
				"Transport reg write failed for port: %d\n",
224 225 226 227 228 229 230
				t_params->port_num);
	}

	return ret;
}

static int sdw_program_master_port_params(struct sdw_bus *bus,
231
					  struct sdw_port_runtime *p_rt)
232 233 234 235 236
{
	int ret;

	/*
	 * we need to set transport and port parameters for the port.
V
Vinod Koul 已提交
237
	 * Transport parameters refers to the sample interval, offsets and
238 239 240 241 242 243 244 245 246 247
	 * 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,
248 249
						  &p_rt->port_params,
						  bus->params.next_bank);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

/**
 * 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)
{
	struct sdw_slave_runtime *s_rt = NULL;
	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;
}

284 285 286 287 288 289 290 291 292 293 294 295
/**
 * 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,
296 297 298
					  struct sdw_slave_runtime *s_rt,
					  struct sdw_port_runtime *p_rt,
					  bool en)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
{
	struct sdw_transport_params *t_params = &p_rt->transport_params;
	u32 addr;
	int ret;

	if (bus->params.next_bank)
		addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
	else
		addr = SDW_DPN_CHANNELEN_B0(p_rt->num);

	/*
	 * Since bus doesn't support sharing a port across two streams,
	 * it is safe to reset this register
	 */
	if (en)
		ret = sdw_update(s_rt->slave, addr, 0xFF, p_rt->ch_mask);
	else
		ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);

	if (ret < 0)
		dev_err(&s_rt->slave->dev,
320
			"Slave chn_en reg write failed:%d port:%d\n",
321 322 323 324 325 326
			ret, t_params->port_num);

	return ret;
}

static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
327 328
					   struct sdw_port_runtime *p_rt,
					   bool en)
329 330 331 332
{
	struct sdw_transport_params *t_params = &p_rt->transport_params;
	struct sdw_bus *bus = m_rt->bus;
	struct sdw_enable_ch enable_ch;
333
	int ret;
334 335 336 337 338 339 340 341

	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,
342 343
							&enable_ch,
							bus->params.next_bank);
344 345
		if (ret < 0) {
			dev_err(bus->dev,
346
				"Master chn_en write failed:%d port:%d\n",
347 348 349 350 351 352 353 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;
	struct sdw_slave_runtime *s_rt = NULL;
	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,
377
							     s_port, en);
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			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,
394 395
			    struct sdw_prepare_ch prep_ch,
			    enum sdw_port_prep_ops cmd)
396 397 398 399 400 401 402 403
{
	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,
V
Vinod Koul 已提交
404 405
				"Slave Port Prep cmd %d failed: %d\n",
				cmd, ret);
406 407 408 409 410 411 412 413
			return ret;
		}
	}

	return 0;
}

static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
414 415 416
				       struct sdw_slave_runtime *s_rt,
				       struct sdw_port_runtime *p_rt,
				       bool prep)
417 418 419 420 421 422 423 424 425 426 427 428 429
{
	struct completion *port_ready = NULL;
	struct sdw_dpn_prop *dpn_prop;
	struct sdw_prepare_ch prep_ch;
	unsigned int time_left;
	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,
430 431
					  s_rt->direction,
					  prep_ch.num);
432 433
	if (!dpn_prop) {
		dev_err(bus->dev,
434
			"Slave Port:%d properties not found\n", prep_ch.num);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		return -EINVAL;
	}

	prep_ch.prepare = prep;

	prep_ch.bank = bus->params.next_bank;

	if (dpn_prop->device_interrupts || !dpn_prop->simple_ch_prep_sm)
		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,
452
					     dpn_prop->device_interrupts);
453 454 455 456 457 458 459 460 461 462 463 464 465
		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)
			ret = sdw_update(s_rt->slave, addr,
466
					 0xFF, p_rt->ch_mask);
467 468 469 470 471
		else
			ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);

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

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

		val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
		val &= p_rt->ch_mask;
		if (!time_left || val) {
			dev_err(&s_rt->slave->dev,
485
				"Chn prep failed for port:%d\n", prep_ch.num);
486 487 488 489 490 491 492 493 494 495
			return -ETIMEDOUT;
		}
	}

	/* 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,
496
					     dpn_prop->device_interrupts);
497 498 499 500 501

	return ret;
}

static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
502 503
					struct sdw_port_runtime *p_rt,
					bool prep)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
{
	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) {
520
			dev_err(bus->dev, "Port prepare failed for port:%d\n",
521
				t_params->port_num);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
			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)
{
	struct sdw_slave_runtime *s_rt = NULL;
	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,
546
							  p_rt, prep);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
			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;
}

S
Sanyog Kale 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/**
 * 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);
			if (ret < 0)
589
				dev_err(bus->dev, "Notify Slave: %d failed\n",
590
					slave->dev_num);
S
Sanyog Kale 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
			return ret;
		}
	}

	return ret;
}

/**
 * sdw_program_params() - Program transport and port parameters for Master(s)
 * and Slave(s)
 *
 * @bus: SDW bus instance
 */
static int sdw_program_params(struct sdw_bus *bus)
{
	struct sdw_master_runtime *m_rt = NULL;
	int ret = 0;

	list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
		ret = sdw_program_port_params(m_rt);
		if (ret < 0) {
			dev_err(bus->dev,
613
				"Program transport params failed: %d\n", ret);
S
Sanyog Kale 已提交
614 615 616 617 618
			return ret;
		}

		ret = sdw_notify_config(m_rt);
		if (ret < 0) {
V
Vinod Koul 已提交
619 620
			dev_err(bus->dev,
				"Notify bus config failed: %d\n", ret);
S
Sanyog Kale 已提交
621 622 623 624 625 626 627 628 629
			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) {
630
			dev_err(bus->dev, "Enable channel failed: %d\n", ret);
S
Sanyog Kale 已提交
631 632 633 634 635 636 637
			return ret;
		}
	}

	return ret;
}

638
static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
S
Sanyog Kale 已提交
639 640
{
	int col_index, row_index;
641
	bool multi_link;
S
Sanyog Kale 已提交
642 643 644 645 646 647 648 649 650
	struct sdw_msg *wr_msg;
	u8 *wbuf = NULL;
	int ret = 0;
	u16 addr;

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

651 652
	bus->defer_msg.msg = wr_msg;

S
Sanyog Kale 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	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,
670
		     SDW_MSG_FLAG_WRITE, wbuf);
S
Sanyog Kale 已提交
671 672
	wr_msg->ssp_sync = true;

673 674 675 676 677 678 679 680 681 682 683
	/*
	 * Set the multi_link flag only when both the hardware supports
	 * and there is a stream handled by multiple masters
	 */
	multi_link = bus->multi_link && (m_rt_count > 1);

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

S
Sanyog Kale 已提交
684
	if (ret < 0) {
685
		dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
S
Sanyog Kale 已提交
686 687 688
		goto error;
	}

689 690 691 692 693 694 695
	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;
	}
S
Sanyog Kale 已提交
696 697 698 699 700 701 702 703 704 705

	return 0;

error:
	kfree(wbuf);
error_1:
	kfree(wr_msg);
	return ret;
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
/**
 * 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) {
725
		dev_err(bus->dev, "Controller Timed out on bank switch\n");
726 727 728 729 730 731 732 733 734 735 736 737 738 739
		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;
}

S
Sanyog Kale 已提交
740 741
static int do_bank_switch(struct sdw_stream_runtime *stream)
{
742
	struct sdw_master_runtime *m_rt = NULL;
S
Sanyog Kale 已提交
743
	const struct sdw_master_ops *ops;
744
	struct sdw_bus *bus = NULL;
745
	bool multi_link = false;
S
Sanyog Kale 已提交
746 747
	int ret = 0;

748 749 750 751
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		ops = bus->ops;

752 753 754 755 756
		if (bus->multi_link) {
			multi_link = true;
			mutex_lock(&bus->msg_lock);
		}

757 758 759 760 761
		/* Pre-bank switch */
		if (ops->pre_bank_switch) {
			ret = ops->pre_bank_switch(bus);
			if (ret < 0) {
				dev_err(bus->dev,
762
					"Pre bank switch op failed: %d\n", ret);
763
				goto msg_unlock;
764 765 766
			}
		}

767 768 769 770 771 772 773
		/*
		 * Perform Bank switch operation.
		 * For multi link cases, the actual bank switch is
		 * synchronized across all Masters and happens later as a
		 * part of post_bank_switch ops.
		 */
		ret = sdw_bank_switch(bus, stream->m_rt_count);
S
Sanyog Kale 已提交
774
		if (ret < 0) {
775
			dev_err(bus->dev, "Bank switch failed: %d\n", ret);
776
			goto error;
S
Sanyog Kale 已提交
777 778 779
		}
	}

780 781 782 783 784 785
	/*
	 * 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.
	 */
786 787 788
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		ops = bus->ops;
S
Sanyog Kale 已提交
789

790 791 792 793 794
		/* Post-bank switch */
		if (ops->post_bank_switch) {
			ret = ops->post_bank_switch(bus);
			if (ret < 0) {
				dev_err(bus->dev,
V
Vinod Koul 已提交
795 796
					"Post bank switch op failed: %d\n",
					ret);
797
				goto error;
798
			}
799 800
		} else if (bus->multi_link && stream->m_rt_count > 1) {
			dev_err(bus->dev,
801
				"Post bank switch ops not implemented\n");
802 803 804 805 806 807 808 809 810 811 812
			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,
813
				"multi link bank switch failed: %d\n", ret);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
			goto error;
		}

		mutex_unlock(&bus->msg_lock);
	}

	return ret;

error:
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;

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

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);
S
Sanyog Kale 已提交
837 838 839 840 841 842
		}
	}

	return ret;
}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
/**
 * 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_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(char *stream_name)
{
	struct sdw_stream_runtime *stream;

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

	stream->name = stream_name;
874
	INIT_LIST_HEAD(&stream->master_list);
875
	stream->state = SDW_STREAM_ALLOCATED;
876
	stream->m_rt_count = 0;
877 878 879 880 881

	return stream;
}
EXPORT_SYMBOL(sdw_alloc_stream);

882 883
static struct sdw_master_runtime
*sdw_find_master_rt(struct sdw_bus *bus,
884
		    struct sdw_stream_runtime *stream)
885 886 887 888 889 890 891 892 893 894 895 896
{
	struct sdw_master_runtime *m_rt = NULL;

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

897 898 899 900 901 902 903 904 905 906 907
/**
 * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
 *
 * @bus: SDW bus instance
 * @stream_config: Stream configuration
 * @stream: Stream runtime handle.
 *
 * This function is to be called with bus_lock held.
 */
static struct sdw_master_runtime
*sdw_alloc_master_rt(struct sdw_bus *bus,
908 909
		     struct sdw_stream_config *stream_config,
		     struct sdw_stream_runtime *stream)
910 911 912 913 914 915 916
{
	struct sdw_master_runtime *m_rt;

	/*
	 * check if Master is already allocated (as a result of Slave adding
	 * it first), if so skip allocation and go to configure
	 */
917
	m_rt = sdw_find_master_rt(bus, stream);
918 919 920 921 922 923 924 925
	if (m_rt)
		goto stream_config;

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

	/* Initialization of Master runtime handle */
926
	INIT_LIST_HEAD(&m_rt->port_list);
927
	INIT_LIST_HEAD(&m_rt->slave_rt_list);
928
	list_add_tail(&m_rt->stream_node, &stream->master_list);
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

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

stream_config:
	m_rt->ch_count = stream_config->ch_count;
	m_rt->bus = bus;
	m_rt->stream = stream;
	m_rt->direction = stream_config->direction;

	return m_rt;
}

/**
 * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
 *
 * @slave: Slave handle
 * @stream_config: Stream configuration
 * @stream: Stream runtime handle
 *
 * This function is to be called with bus_lock held.
 */
static struct sdw_slave_runtime
*sdw_alloc_slave_rt(struct sdw_slave *slave,
952 953
		    struct sdw_stream_config *stream_config,
		    struct sdw_stream_runtime *stream)
954 955 956 957 958 959 960
{
	struct sdw_slave_runtime *s_rt = NULL;

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

961
	INIT_LIST_HEAD(&s_rt->port_list);
962 963 964 965 966 967 968
	s_rt->ch_count = stream_config->ch_count;
	s_rt->direction = stream_config->direction;
	s_rt->slave = slave;

	return s_rt;
}

969
static void sdw_master_port_release(struct sdw_bus *bus,
970
				    struct sdw_master_runtime *m_rt)
971 972 973
{
	struct sdw_port_runtime *p_rt, *_p_rt;

974
	list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
975 976 977 978 979 980
		list_del(&p_rt->port_node);
		kfree(p_rt);
	}
}

static void sdw_slave_port_release(struct sdw_bus *bus,
981 982
				   struct sdw_slave *slave,
				   struct sdw_stream_runtime *stream)
983 984
{
	struct sdw_port_runtime *p_rt, *_p_rt;
985
	struct sdw_master_runtime *m_rt;
986 987
	struct sdw_slave_runtime *s_rt;

988 989 990 991 992 993
	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,
994
						 &s_rt->port_list, port_node) {
995 996 997
				list_del(&p_rt->port_node);
				kfree(p_rt);
			}
998 999 1000 1001
		}
	}
}

1002 1003 1004 1005 1006 1007 1008 1009 1010
/**
 * sdw_release_slave_stream() - 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_release_slave_stream(struct sdw_slave *slave,
1011
				     struct sdw_stream_runtime *stream)
1012 1013
{
	struct sdw_slave_runtime *s_rt, *_s_rt;
1014
	struct sdw_master_runtime *m_rt;
1015

1016 1017 1018
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		/* Retrieve Slave runtime handle */
		list_for_each_entry_safe(s_rt, _s_rt,
1019
					 &m_rt->slave_rt_list, m_rt_node) {
1020 1021 1022 1023 1024
			if (s_rt->slave == slave) {
				list_del(&s_rt->m_rt_node);
				kfree(s_rt);
				return;
			}
1025 1026 1027 1028 1029 1030 1031
		}
	}
}

/**
 * sdw_release_master_stream() - Free Master runtime handle
 *
1032
 * @m_rt: Master runtime node
1033 1034 1035 1036 1037 1038 1039
 * @stream: Stream runtime handle.
 *
 * This function is to be called with bus_lock held
 * It frees the Master runtime handle and associated Slave(s) runtime
 * handle. If this is called first then sdw_release_slave_stream() will have
 * no effect as Slave(s) runtime handle would already be freed up.
 */
1040
static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
1041
				      struct sdw_stream_runtime *stream)
1042 1043 1044
{
	struct sdw_slave_runtime *s_rt, *_s_rt;

1045 1046 1047 1048
	list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
		sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
		sdw_release_slave_stream(s_rt->slave, stream);
	}
1049

1050
	list_del(&m_rt->stream_node);
1051
	list_del(&m_rt->bus_node);
1052
	kfree(m_rt);
1053 1054 1055 1056 1057 1058 1059 1060
}

/**
 * sdw_stream_remove_master() - Remove master from sdw_stream
 *
 * @bus: SDW Bus instance
 * @stream: SoundWire stream
 *
1061
 * This removes and frees port_rt and master_rt from a stream
1062 1063
 */
int sdw_stream_remove_master(struct sdw_bus *bus,
1064
			     struct sdw_stream_runtime *stream)
1065
{
1066 1067
	struct sdw_master_runtime *m_rt, *_m_rt;

1068 1069
	mutex_lock(&bus->bus_lock);

1070
	list_for_each_entry_safe(m_rt, _m_rt,
1071
				 &stream->master_list, stream_node) {
1072 1073 1074 1075 1076
		if (m_rt->bus != bus)
			continue;

		sdw_master_port_release(bus, m_rt);
		sdw_release_master_stream(m_rt, stream);
1077
		stream->m_rt_count--;
1078 1079 1080 1081
	}

	if (list_empty(&stream->master_list))
		stream->state = SDW_STREAM_RELEASED;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094

	mutex_unlock(&bus->bus_lock);

	return 0;
}
EXPORT_SYMBOL(sdw_stream_remove_master);

/**
 * sdw_stream_remove_slave() - Remove slave from sdw_stream
 *
 * @slave: SDW Slave instance
 * @stream: SoundWire stream
 *
1095
 * This removes and frees port_rt and slave_rt from a stream
1096 1097
 */
int sdw_stream_remove_slave(struct sdw_slave *slave,
1098
			    struct sdw_stream_runtime *stream)
1099 1100 1101
{
	mutex_lock(&slave->bus->bus_lock);

1102
	sdw_slave_port_release(slave->bus, slave, stream);
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	sdw_release_slave_stream(slave, stream);

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

	return 0;
}
EXPORT_SYMBOL(sdw_stream_remove_slave);

/**
 * 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,
1122 1123 1124
			     struct sdw_stream_runtime *stream,
			     struct sdw_stream_config *stream_config,
			     bool is_slave)
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
{
	/*
	 * 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 &&
1135
	    stream->params.rate != stream_config->frame_rate) {
1136
		dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1137 1138 1139 1140
		return -EINVAL;
	}

	if (stream->params.bps &&
1141
	    stream->params.bps != stream_config->bps) {
1142
		dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
		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;
}

1157
static int sdw_is_valid_port_range(struct device *dev,
1158
				   struct sdw_port_runtime *p_rt)
1159 1160 1161
{
	if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
		dev_err(dev,
1162
			"SoundWire: Invalid port number :%d\n", p_rt->num);
1163 1164 1165 1166 1167 1168
		return -EINVAL;
	}

	return 0;
}

1169 1170 1171 1172
static struct sdw_port_runtime
*sdw_port_alloc(struct device *dev,
		struct sdw_port_config *port_config,
		int port_index)
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
{
	struct sdw_port_runtime *p_rt;

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

	p_rt->ch_mask = port_config[port_index].ch_mask;
	p_rt->num = port_config[port_index].num;

	return p_rt;
}

static int sdw_master_port_config(struct sdw_bus *bus,
1187 1188 1189
				  struct sdw_master_runtime *m_rt,
				  struct sdw_port_config *port_config,
				  unsigned int num_ports)
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
{
	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(bus->dev, port_config, i);
		if (!p_rt)
			return -ENOMEM;

		/*
		 * TODO: Check port capabilities for requested
		 * configuration (audio mode support)
		 */

		list_add_tail(&p_rt->port_node, &m_rt->port_list);
	}

	return 0;
}

static int sdw_slave_port_config(struct sdw_slave *slave,
1212 1213 1214
				 struct sdw_slave_runtime *s_rt,
				 struct sdw_port_config *port_config,
				 unsigned int num_config)
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
	struct sdw_port_runtime *p_rt;
	int i, ret;

	/* Iterate for number of ports to perform initialization */
	for (i = 0; i < num_config; i++) {
		p_rt = sdw_port_alloc(&slave->dev, port_config, i);
		if (!p_rt)
			return -ENOMEM;

		/*
		 * TODO: Check valid port range as defined by DisCo/
		 * slave
		 */
		ret = sdw_is_valid_port_range(&slave->dev, p_rt);
		if (ret < 0) {
			kfree(p_rt);
			return ret;
		}

		/*
		 * TODO: Check port capabilities for requested
		 * configuration (audio mode support)
		 */

		list_add_tail(&p_rt->port_node, &s_rt->port_list);
	}

	return 0;
}

1246 1247 1248 1249 1250
/**
 * sdw_stream_add_master() - Allocate and add master runtime to a stream
 *
 * @bus: SDW Bus instance
 * @stream_config: Stream configuration for audio stream
1251 1252
 * @port_config: Port configuration for audio stream
 * @num_ports: Number of ports
1253 1254 1255
 * @stream: SoundWire stream
 */
int sdw_stream_add_master(struct sdw_bus *bus,
1256 1257 1258 1259
			  struct sdw_stream_config *stream_config,
			  struct sdw_port_config *port_config,
			  unsigned int num_ports,
			  struct sdw_stream_runtime *stream)
1260 1261 1262 1263 1264 1265
{
	struct sdw_master_runtime *m_rt = NULL;
	int ret;

	mutex_lock(&bus->bus_lock);

1266 1267 1268 1269 1270 1271 1272
	/*
	 * 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,
1273
			"Multilink not supported, link %d\n", bus->link_id);
1274 1275 1276 1277
		ret = -EINVAL;
		goto unlock;
	}

1278 1279 1280
	m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
	if (!m_rt) {
		dev_err(bus->dev,
1281
			"Master runtime config failed for stream:%s\n",
1282
			stream->name);
1283
		ret = -ENOMEM;
1284
		goto unlock;
1285 1286 1287 1288 1289 1290
	}

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

1291 1292 1293 1294
	ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
	if (ret)
		goto stream_error;

1295 1296
	stream->m_rt_count++;

1297 1298
	goto unlock;

1299
stream_error:
1300
	sdw_release_master_stream(m_rt, stream);
1301
unlock:
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
	mutex_unlock(&bus->bus_lock);
	return ret;
}
EXPORT_SYMBOL(sdw_stream_add_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
1313 1314
 * @port_config: Port configuration for audio stream
 * @num_ports: Number of ports
1315 1316 1317 1318
 *
 * It is expected that Slave is added before adding Master
 * to the Stream.
 *
1319 1320
 */
int sdw_stream_add_slave(struct sdw_slave *slave,
1321 1322 1323 1324
			 struct sdw_stream_config *stream_config,
			 struct sdw_port_config *port_config,
			 unsigned int num_ports,
			 struct sdw_stream_runtime *stream)
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
{
	struct sdw_slave_runtime *s_rt;
	struct sdw_master_runtime *m_rt;
	int ret;

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

	/*
	 * 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_alloc_master_rt(slave->bus, stream_config, stream);
	if (!m_rt) {
		dev_err(&slave->dev,
1339
			"alloc master runtime failed for stream:%s\n",
1340
			stream->name);
1341 1342 1343 1344 1345 1346 1347
		ret = -ENOMEM;
		goto error;
	}

	s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
	if (!s_rt) {
		dev_err(&slave->dev,
1348
			"Slave runtime config failed for stream:%s\n",
1349
			stream->name);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
		ret = -ENOMEM;
		goto stream_error;
	}

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

	list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);

1360 1361 1362 1363
	ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
	if (ret)
		goto stream_error;

1364 1365 1366 1367 1368 1369
	/*
	 * 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.
	 */
1370 1371 1372 1373 1374 1375 1376 1377
	stream->state = SDW_STREAM_CONFIGURED;
	goto error;

stream_error:
	/*
	 * we hit error so cleanup the stream, release all Slave(s) and
	 * Master runtime
	 */
1378
	sdw_release_master_stream(m_rt, stream);
1379 1380 1381 1382 1383
error:
	mutex_unlock(&slave->bus->bus_lock);
	return ret;
}
EXPORT_SYMBOL(sdw_stream_add_slave);
1384 1385 1386 1387 1388 1389 1390 1391 1392

/**
 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
 *
 * @slave: Slave handle
 * @direction: Data direction.
 * @port_num: Port number
 */
struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
1393 1394
					    enum sdw_data_direction direction,
					    unsigned int port_num)
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
{
	struct sdw_dpn_prop *dpn_prop;
	u8 num_ports;
	int i;

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

	for (i = 0; i < num_ports; i++) {
		dpn_prop = &dpn_prop[i];

		if (dpn_prop->num == port_num)
			return &dpn_prop[i];
	}

	return NULL;
}
1417

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
/**
 * 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 = NULL;
	struct sdw_bus *bus = NULL;

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

		mutex_lock(&bus->bus_lock);
	}
}

/**
 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
 *
 * @stream: SoundWire stream
 *
 * Release the previously held bus_lock after reconfiguring the bus.
1447 1448
 * NOTE: This function is called from SoundWire stream ops and is
 * expected that a global lock is held before releasing bus_lock.
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
 */
static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
{
	struct sdw_master_runtime *m_rt = NULL;
	struct sdw_bus *bus = NULL;

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

1462 1463
static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
{
1464 1465
	struct sdw_master_runtime *m_rt = NULL;
	struct sdw_bus *bus = NULL;
1466 1467 1468 1469
	struct sdw_master_prop *prop = NULL;
	struct sdw_bus_params params;
	int ret;

1470 1471 1472 1473 1474
	/* 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));
1475

1476
		/* TODO: Support Asynchronous mode */
1477
		if ((prop->max_clk_freq % stream->params.rate) != 0) {
1478
			dev_err(bus->dev, "Async mode not supported\n");
1479 1480
			return -EINVAL;
		}
1481

1482 1483 1484 1485 1486 1487 1488 1489
		/* 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;

		/* Program params */
		ret = sdw_program_params(bus);
		if (ret < 0) {
1490
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1491 1492
			goto restore_params;
		}
1493 1494 1495 1496
	}

	ret = do_bank_switch(stream);
	if (ret < 0) {
1497
		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1498 1499 1500
		goto restore_params;
	}

1501 1502 1503 1504 1505 1506
	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) {
1507
			dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1508
				ret);
1509 1510
			return ret;
		}
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
	}

	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
 *
1527
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1528 1529 1530 1531 1532 1533
 */
int sdw_prepare_stream(struct sdw_stream_runtime *stream)
{
	int ret = 0;

	if (!stream) {
1534
		pr_err("SoundWire: Handle not found for stream\n");
1535 1536 1537
		return -EINVAL;
	}

1538
	sdw_acquire_bus_lock(stream);
1539 1540 1541

	ret = _sdw_prepare_stream(stream);
	if (ret < 0)
1542
		pr_err("Prepare for stream:%s failed: %d\n", stream->name, ret);
1543

1544
	sdw_release_bus_lock(stream);
1545 1546 1547 1548 1549 1550
	return ret;
}
EXPORT_SYMBOL(sdw_prepare_stream);

static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
{
1551 1552
	struct sdw_master_runtime *m_rt = NULL;
	struct sdw_bus *bus = NULL;
1553 1554
	int ret;

1555 1556 1557
	/* 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;
1558

1559 1560 1561
		/* Program params */
		ret = sdw_program_params(bus);
		if (ret < 0) {
1562
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1563 1564 1565 1566 1567 1568
			return ret;
		}

		/* Enable port(s) */
		ret = sdw_enable_disable_ports(m_rt, true);
		if (ret < 0) {
V
Vinod Koul 已提交
1569 1570
			dev_err(bus->dev,
				"Enable port(s) failed ret: %d\n", ret);
1571 1572
			return ret;
		}
1573 1574 1575 1576
	}

	ret = do_bank_switch(stream);
	if (ret < 0) {
1577
		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		return ret;
	}

	stream->state = SDW_STREAM_ENABLED;
	return 0;
}

/**
 * sdw_enable_stream() - Enable SoundWire stream
 *
 * @stream: Soundwire stream
 *
1590
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1591 1592 1593 1594 1595 1596
 */
int sdw_enable_stream(struct sdw_stream_runtime *stream)
{
	int ret = 0;

	if (!stream) {
1597
		pr_err("SoundWire: Handle not found for stream\n");
1598 1599 1600
		return -EINVAL;
	}

1601
	sdw_acquire_bus_lock(stream);
1602 1603 1604

	ret = _sdw_enable_stream(stream);
	if (ret < 0)
1605
		pr_err("Enable for stream:%s failed: %d\n", stream->name, ret);
1606

1607
	sdw_release_bus_lock(stream);
1608 1609 1610 1611 1612 1613
	return ret;
}
EXPORT_SYMBOL(sdw_enable_stream);

static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
{
1614 1615
	struct sdw_master_runtime *m_rt = NULL;
	struct sdw_bus *bus = NULL;
1616 1617
	int ret;

1618 1619 1620 1621 1622
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		/* Disable port(s) */
		ret = sdw_enable_disable_ports(m_rt, false);
		if (ret < 0) {
1623
			dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1624 1625
			return ret;
		}
1626 1627 1628
	}
	stream->state = SDW_STREAM_DISABLED;

1629 1630 1631 1632 1633
	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		/* Program params */
		ret = sdw_program_params(bus);
		if (ret < 0) {
1634
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1635 1636
			return ret;
		}
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	}

	return do_bank_switch(stream);
}

/**
 * sdw_disable_stream() - Disable SoundWire stream
 *
 * @stream: Soundwire stream
 *
1647
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1648 1649 1650 1651 1652 1653
 */
int sdw_disable_stream(struct sdw_stream_runtime *stream)
{
	int ret = 0;

	if (!stream) {
1654
		pr_err("SoundWire: Handle not found for stream\n");
1655 1656 1657
		return -EINVAL;
	}

1658
	sdw_acquire_bus_lock(stream);
1659 1660 1661

	ret = _sdw_disable_stream(stream);
	if (ret < 0)
1662
		pr_err("Disable for stream:%s failed: %d\n", stream->name, ret);
1663

1664
	sdw_release_bus_lock(stream);
1665 1666 1667 1668 1669 1670
	return ret;
}
EXPORT_SYMBOL(sdw_disable_stream);

static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
{
1671 1672
	struct sdw_master_runtime *m_rt = NULL;
	struct sdw_bus *bus = NULL;
1673 1674
	int ret = 0;

1675 1676 1677 1678 1679
	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) {
V
Vinod Koul 已提交
1680 1681
			dev_err(bus->dev,
				"De-prepare port(s) failed: %d\n", ret);
1682 1683
			return ret;
		}
1684

1685 1686 1687
		/* TODO: Update this during Device-Device support */
		bus->params.bandwidth -= m_rt->stream->params.rate *
			m_rt->ch_count * m_rt->stream->params.bps;
1688

1689 1690 1691
		/* Program params */
		ret = sdw_program_params(bus);
		if (ret < 0) {
1692
			dev_err(bus->dev, "Program params failed: %d\n", ret);
1693 1694
			return ret;
		}
1695 1696
	}

1697
	stream->state = SDW_STREAM_DEPREPARED;
1698 1699 1700 1701 1702 1703 1704 1705
	return do_bank_switch(stream);
}

/**
 * sdw_deprepare_stream() - Deprepare SoundWire stream
 *
 * @stream: Soundwire stream
 *
1706
 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1707 1708 1709 1710 1711 1712
 */
int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
{
	int ret = 0;

	if (!stream) {
1713
		pr_err("SoundWire: Handle not found for stream\n");
1714 1715 1716
		return -EINVAL;
	}

1717
	sdw_acquire_bus_lock(stream);
1718 1719
	ret = _sdw_deprepare_stream(stream);
	if (ret < 0)
1720
		pr_err("De-prepare for stream:%d failed: %d\n", ret, ret);
1721

1722
	sdw_release_bus_lock(stream);
1723 1724 1725
	return ret;
}
EXPORT_SYMBOL(sdw_deprepare_stream);