File Index
Files and directories
Sign in
FILE

sdca_asoc.c

File preview with syntax highlighting.

linux-6.19.14/sound/soc/sdca/sdca_asoc.c C
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (C) 2025 Cirrus Logic, Inc. and
   3//                    Cirrus Logic International Semiconductor Ltd.
   4
   5/*
   6 * The MIPI SDCA specification is available for public downloads at
   7 * https://www.mipi.org/mipi-sdca-v1-0-download
   8 */
   9
  10#include <linux/bits.h>
  11#include <linux/bitmap.h>
  12#include <linux/build_bug.h>
  13#include <linux/delay.h>
  14#include <linux/dev_printk.h>
  15#include <linux/device.h>
  16#include <linux/minmax.h>
  17#include <linux/module.h>
  18#include <linux/overflow.h>
  19#include <linux/pm_runtime.h>
  20#include <linux/regmap.h>
  21#include <linux/soundwire/sdw_registers.h>
  22#include <linux/string_helpers.h>
  23#include <linux/types.h>
  24#include <sound/control.h>
  25#include <sound/pcm.h>
  26#include <sound/pcm_params.h>
  27#include <sound/sdca.h>
  28#include <sound/sdca_asoc.h>
  29#include <sound/sdca_function.h>
  30#include <sound/soc.h>
  31#include <sound/soc-component.h>
  32#include <sound/soc-dai.h>
  33#include <sound/soc-dapm.h>
  34#include <sound/tlv.h>
  35
  36static bool exported_control(struct sdca_entity *entity, struct sdca_control *control)
  37{
  38	switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
  39	case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
  40		return true;
  41	default:
  42		break;
  43	}
  44
  45	return control->layers & (SDCA_ACCESS_LAYER_USER |
  46				  SDCA_ACCESS_LAYER_APPLICATION);
  47}
  48
  49static bool readonly_control(struct sdca_control *control)
  50{
  51	return control->has_fixed || control->mode == SDCA_ACCESS_MODE_RO;
  52}
  53
  54/**
  55 * sdca_asoc_count_component - count the various component parts
  56 * @dev: Pointer to the device against which allocations will be done.
  57 * @function: Pointer to the Function information.
  58 * @num_widgets: Output integer pointer, will be filled with the
  59 * required number of DAPM widgets for the Function.
  60 * @num_routes: Output integer pointer, will be filled with the
  61 * required number of DAPM routes for the Function.
  62 * @num_controls: Output integer pointer, will be filled with the
  63 * required number of ALSA controls for the Function.
  64 * @num_dais: Output integer pointer, will be filled with the
  65 * required number of ASoC DAIs for the Function.
  66 *
  67 * This function counts various things within the SDCA Function such
  68 * that the calling driver can allocate appropriate space before
  69 * calling the appropriate population functions.
  70 *
  71 * Return: Returns zero on success, and a negative error code on failure.
  72 */
  73int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *function,
  74			      int *num_widgets, int *num_routes, int *num_controls,
  75			      int *num_dais)
  76{
  77	int i, j;
  78
  79	*num_widgets = function->num_entities - 1;
  80	*num_routes = 0;
  81	*num_controls = 0;
  82	*num_dais = 0;
  83
  84	for (i = 0; i < function->num_entities - 1; i++) {
  85		struct sdca_entity *entity = &function->entities[i];
  86
  87		/* Add supply/DAI widget connections */
  88		switch (entity->type) {
  89		case SDCA_ENTITY_TYPE_IT:
  90		case SDCA_ENTITY_TYPE_OT:
  91			*num_routes += !!entity->iot.clock;
  92			*num_routes += !!entity->iot.is_dataport;
  93			*num_controls += !entity->iot.is_dataport;
  94			*num_dais += !!entity->iot.is_dataport;
  95			break;
  96		case SDCA_ENTITY_TYPE_PDE:
  97			*num_routes += entity->pde.num_managed;
  98			break;
  99		default:
 100			break;
 101		}
 102
 103		if (entity->group)
 104			(*num_routes)++;
 105
 106		/* Add primary entity connections from DisCo */
 107		*num_routes += entity->num_sources;
 108
 109		for (j = 0; j < entity->num_controls; j++) {
 110			if (exported_control(entity, &entity->controls[j]))
 111				(*num_controls)++;
 112		}
 113	}
 114
 115	return 0;
 116}
 117EXPORT_SYMBOL_NS(sdca_asoc_count_component, "SND_SOC_SDCA");
 118
 119static int entity_early_parse_ge(struct device *dev,
 120				 struct sdca_function_data *function,
 121				 struct sdca_entity *entity)
 122{
 123	struct sdca_control_range *range;
 124	struct sdca_control *control;
 125	struct snd_kcontrol_new *kctl;
 126	struct soc_enum *soc_enum;
 127	const char *control_name;
 128	unsigned int *values;
 129	const char **texts;
 130	int i;
 131
 132	control = sdca_selector_find_control(dev, entity, SDCA_CTL_GE_SELECTED_MODE);
 133	if (!control)
 134		return -EINVAL;
 135
 136	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
 137		dev_warn(dev, "%s: unexpected access layer: %x\n",
 138			 entity->label, control->layers);
 139
 140	range = sdca_control_find_range(dev, entity, control, SDCA_SELECTED_MODE_NCOLS, 0);
 141	if (!range)
 142		return -EINVAL;
 143
 144	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
 145				      entity->label, control->label);
 146	if (!control_name)
 147		return -ENOMEM;
 148
 149	kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
 150	if (!kctl)
 151		return -ENOMEM;
 152
 153	soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
 154	if (!soc_enum)
 155		return -ENOMEM;
 156
 157	texts = devm_kcalloc(dev, range->rows + 3, sizeof(*texts), GFP_KERNEL);
 158	if (!texts)
 159		return -ENOMEM;
 160
 161	values = devm_kcalloc(dev, range->rows + 3, sizeof(*values), GFP_KERNEL);
 162	if (!values)
 163		return -ENOMEM;
 164
 165	texts[0] = "Jack Unplugged";
 166	texts[1] = "Jack Unknown";
 167	texts[2] = "Detection in Progress";
 168	values[0] = SDCA_DETECTED_MODE_JACK_UNPLUGGED;
 169	values[1] = SDCA_DETECTED_MODE_JACK_UNKNOWN;
 170	values[2] = SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS;
 171	for (i = 0; i < range->rows; i++) {
 172		enum sdca_terminal_type type;
 173
 174		type = sdca_range(range, SDCA_SELECTED_MODE_TERM_TYPE, i);
 175
 176		values[i + 3] = sdca_range(range, SDCA_SELECTED_MODE_INDEX, i);
 177		texts[i + 3] = sdca_find_terminal_name(type);
 178		if (!texts[i + 3]) {
 179			dev_err(dev, "%s: unrecognised terminal type: %#x\n",
 180				entity->label, type);
 181			return -EINVAL;
 182		}
 183	}
 184
 185	soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
 186	soc_enum->items = range->rows + 3;
 187	soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
 188	soc_enum->texts = texts;
 189	soc_enum->values = values;
 190
 191	kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 192	kctl->name = control_name;
 193	kctl->info = snd_soc_info_enum_double;
 194	kctl->get = snd_soc_dapm_get_enum_double;
 195	kctl->put = snd_soc_dapm_put_enum_double;
 196	kctl->private_value = (unsigned long)soc_enum;
 197
 198	entity->ge.kctl = kctl;
 199
 200	return 0;
 201}
 202
 203static void add_route(struct snd_soc_dapm_route **route, const char *sink,
 204		      const char *control, const char *source)
 205{
 206	(*route)->sink = sink;
 207	(*route)->control = control;
 208	(*route)->source = source;
 209	(*route)++;
 210}
 211
 212static int entity_parse_simple(struct device *dev,
 213			       struct sdca_function_data *function,
 214			       struct sdca_entity *entity,
 215			       struct snd_soc_dapm_widget **widget,
 216			       struct snd_soc_dapm_route **route,
 217			       enum snd_soc_dapm_type id)
 218{
 219	int i;
 220
 221	(*widget)->id = id;
 222	(*widget)++;
 223
 224	for (i = 0; i < entity->num_sources; i++)
 225		add_route(route, entity->label, NULL, entity->sources[i]->label);
 226
 227	return 0;
 228}
 229
 230static int entity_parse_it(struct device *dev,
 231			   struct sdca_function_data *function,
 232			   struct sdca_entity *entity,
 233			   struct snd_soc_dapm_widget **widget,
 234			   struct snd_soc_dapm_route **route)
 235{
 236	int i;
 237
 238	if (entity->iot.is_dataport) {
 239		const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
 240						      entity->label, "Playback");
 241		if (!aif_name)
 242			return -ENOMEM;
 243
 244		(*widget)->id = snd_soc_dapm_aif_in;
 245
 246		add_route(route, entity->label, NULL, aif_name);
 247	} else {
 248		(*widget)->id = snd_soc_dapm_mic;
 249	}
 250
 251	if (entity->iot.clock)
 252		add_route(route, entity->label, NULL, entity->iot.clock->label);
 253
 254	for (i = 0; i < entity->num_sources; i++)
 255		add_route(route, entity->label, NULL, entity->sources[i]->label);
 256
 257	(*widget)++;
 258
 259	return 0;
 260}
 261
 262static int entity_parse_ot(struct device *dev,
 263			   struct sdca_function_data *function,
 264			   struct sdca_entity *entity,
 265			   struct snd_soc_dapm_widget **widget,
 266			   struct snd_soc_dapm_route **route)
 267{
 268	int i;
 269
 270	if (entity->iot.is_dataport) {
 271		const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
 272						      entity->label, "Capture");
 273		if (!aif_name)
 274			return -ENOMEM;
 275
 276		(*widget)->id = snd_soc_dapm_aif_out;
 277
 278		add_route(route, aif_name, NULL, entity->label);
 279	} else {
 280		(*widget)->id = snd_soc_dapm_spk;
 281	}
 282
 283	if (entity->iot.clock)
 284		add_route(route, entity->label, NULL, entity->iot.clock->label);
 285
 286	for (i = 0; i < entity->num_sources; i++)
 287		add_route(route, entity->label, NULL, entity->sources[i]->label);
 288
 289	(*widget)++;
 290
 291	return 0;
 292}
 293
 294static int entity_pde_event(struct snd_soc_dapm_widget *widget,
 295			    struct snd_kcontrol *kctl, int event)
 296{
 297	struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
 298	struct sdca_entity *entity = widget->priv;
 299	static const int polls = 100;
 300	unsigned int reg, val;
 301	int from, to, i;
 302	int poll_us;
 303	int ret;
 304
 305	if (!component)
 306		return -EIO;
 307
 308	switch (event) {
 309	case SND_SOC_DAPM_POST_PMD:
 310		from = widget->on_val;
 311		to = widget->off_val;
 312		break;
 313	case SND_SOC_DAPM_POST_PMU:
 314		from = widget->off_val;
 315		to = widget->on_val;
 316		break;
 317	default:
 318		return 0;
 319	}
 320
 321	for (i = 0; i < entity->pde.num_max_delay; i++) {
 322		struct sdca_pde_delay *delay = &entity->pde.max_delay[i];
 323
 324		if (delay->from_ps == from && delay->to_ps == to) {
 325			poll_us = delay->us / polls;
 326			break;
 327		}
 328	}
 329
 330	reg = SDW_SDCA_CTL(SDW_SDCA_CTL_FUNC(widget->reg),
 331			   SDW_SDCA_CTL_ENT(widget->reg),
 332			   SDCA_CTL_PDE_ACTUAL_PS, 0);
 333
 334	for (i = 0; i < polls; i++) {
 335		if (i)
 336			fsleep(poll_us);
 337
 338		ret = regmap_read(component->regmap, reg, &val);
 339		if (ret)
 340			return ret;
 341		else if (val == to)
 342			return 0;
 343	}
 344
 345	dev_err(component->dev, "%s: power transition failed: %x\n",
 346		entity->label, val);
 347	return -ETIMEDOUT;
 348}
 349
 350static int entity_parse_pde(struct device *dev,
 351			    struct sdca_function_data *function,
 352			    struct sdca_entity *entity,
 353			    struct snd_soc_dapm_widget **widget,
 354			    struct snd_soc_dapm_route **route)
 355{
 356	unsigned int target = (1 << SDCA_PDE_PS0) | (1 << SDCA_PDE_PS3);
 357	struct sdca_control_range *range;
 358	struct sdca_control *control;
 359	unsigned int mask = 0;
 360	int i;
 361
 362	control = sdca_selector_find_control(dev, entity, SDCA_CTL_PDE_REQUESTED_PS);
 363	if (!control)
 364		return -EINVAL;
 365
 366	/* Power should only be controlled by the driver */
 367	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
 368		dev_warn(dev, "%s: unexpected access layer: %x\n",
 369			 entity->label, control->layers);
 370
 371	range = sdca_control_find_range(dev, entity, control, SDCA_REQUESTED_PS_NCOLS, 0);
 372	if (!range)
 373		return -EINVAL;
 374
 375	for (i = 0; i < range->rows; i++)
 376		mask |= 1 << sdca_range(range, SDCA_REQUESTED_PS_STATE, i);
 377
 378	if ((mask & target) != target) {
 379		dev_err(dev, "%s: power control missing states\n", entity->label);
 380		return -EINVAL;
 381	}
 382
 383	(*widget)->id = snd_soc_dapm_supply;
 384	(*widget)->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
 385	(*widget)->mask = GENMASK(control->nbits - 1, 0);
 386	(*widget)->on_val = SDCA_PDE_PS0;
 387	(*widget)->off_val = SDCA_PDE_PS3;
 388	(*widget)->event_flags = SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD;
 389	(*widget)->event = entity_pde_event;
 390	(*widget)->priv = entity;
 391	(*widget)++;
 392
 393	for (i = 0; i < entity->pde.num_managed; i++)
 394		add_route(route, entity->pde.managed[i]->label, NULL, entity->label);
 395
 396	for (i = 0; i < entity->num_sources; i++)
 397		add_route(route, entity->label, NULL, entity->sources[i]->label);
 398
 399	return 0;
 400}
 401
 402/* Device selector units are controlled through a group entity */
 403static int entity_parse_su_device(struct device *dev,
 404				  struct sdca_function_data *function,
 405				  struct sdca_entity *entity,
 406				  struct snd_soc_dapm_widget **widget,
 407				  struct snd_soc_dapm_route **route)
 408{
 409	struct sdca_control_range *range;
 410	int num_routes = 0;
 411	int i, j;
 412
 413	if (!entity->group) {
 414		dev_err(dev, "%s: device selector unit missing group\n", entity->label);
 415		return -EINVAL;
 416	}
 417
 418	range = sdca_selector_find_range(dev, entity->group, SDCA_CTL_GE_SELECTED_MODE,
 419					 SDCA_SELECTED_MODE_NCOLS, 0);
 420	if (!range)
 421		return -EINVAL;
 422
 423	(*widget)->id = snd_soc_dapm_mux;
 424	(*widget)->kcontrol_news = entity->group->ge.kctl;
 425	(*widget)->num_kcontrols = 1;
 426	(*widget)++;
 427
 428	for (i = 0; i < entity->group->ge.num_modes; i++) {
 429		struct sdca_ge_mode *mode = &entity->group->ge.modes[i];
 430
 431		for (j = 0; j < mode->num_controls; j++) {
 432			struct sdca_ge_control *affected = &mode->controls[j];
 433			int term;
 434
 435			if (affected->id != entity->id ||
 436			    affected->sel != SDCA_CTL_SU_SELECTOR ||
 437			    !affected->val)
 438				continue;
 439
 440			if (affected->val - 1 >= entity->num_sources) {
 441				dev_err(dev, "%s: bad control value: %#x\n",
 442					entity->label, affected->val);
 443				return -EINVAL;
 444			}
 445
 446			if (++num_routes > entity->num_sources) {
 447				dev_err(dev, "%s: too many input routes\n", entity->label);
 448				return -EINVAL;
 449			}
 450
 451			term = sdca_range_search(range, SDCA_SELECTED_MODE_INDEX,
 452						 mode->val, SDCA_SELECTED_MODE_TERM_TYPE);
 453			if (!term) {
 454				dev_err(dev, "%s: mode not found: %#x\n",
 455					entity->label, mode->val);
 456				return -EINVAL;
 457			}
 458
 459			add_route(route, entity->label, sdca_find_terminal_name(term),
 460				  entity->sources[affected->val - 1]->label);
 461		}
 462	}
 463
 464	return 0;
 465}
 466
 467/* Class selector units will be exported as an ALSA control */
 468static int entity_parse_su_class(struct device *dev,
 469				 struct sdca_function_data *function,
 470				 struct sdca_entity *entity,
 471				 struct sdca_control *control,
 472				 struct snd_soc_dapm_widget **widget,
 473				 struct snd_soc_dapm_route **route)
 474{
 475	struct snd_kcontrol_new *kctl;
 476	struct soc_enum *soc_enum;
 477	const char **texts;
 478	int i;
 479
 480	kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
 481	if (!kctl)
 482		return -ENOMEM;
 483
 484	soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
 485	if (!soc_enum)
 486		return -ENOMEM;
 487
 488	texts = devm_kcalloc(dev, entity->num_sources + 1, sizeof(*texts), GFP_KERNEL);
 489	if (!texts)
 490		return -ENOMEM;
 491
 492	texts[0] = "No Signal";
 493	for (i = 0; i < entity->num_sources; i++)
 494		texts[i + 1] = entity->sources[i]->label;
 495
 496	soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
 497	soc_enum->items = entity->num_sources + 1;
 498	soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
 499	soc_enum->texts = texts;
 500
 501	kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 502	kctl->name = "Route";
 503	kctl->info = snd_soc_info_enum_double;
 504	kctl->get = snd_soc_dapm_get_enum_double;
 505	kctl->put = snd_soc_dapm_put_enum_double;
 506	kctl->private_value = (unsigned long)soc_enum;
 507
 508	(*widget)->id = snd_soc_dapm_mux;
 509	(*widget)->kcontrol_news = kctl;
 510	(*widget)->num_kcontrols = 1;
 511	(*widget)++;
 512
 513	for (i = 0; i < entity->num_sources; i++)
 514		add_route(route, entity->label, texts[i + 1], entity->sources[i]->label);
 515
 516	return 0;
 517}
 518
 519static int entity_parse_su(struct device *dev,
 520			   struct sdca_function_data *function,
 521			   struct sdca_entity *entity,
 522			   struct snd_soc_dapm_widget **widget,
 523			   struct snd_soc_dapm_route **route)
 524{
 525	struct sdca_control *control;
 526
 527	if (!entity->num_sources) {
 528		dev_err(dev, "%s: selector with no inputs\n", entity->label);
 529		return -EINVAL;
 530	}
 531
 532	control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR);
 533	if (!control)
 534		return -EINVAL;
 535
 536	if (control->layers == SDCA_ACCESS_LAYER_DEVICE)
 537		return entity_parse_su_device(dev, function, entity, widget, route);
 538
 539	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
 540		dev_warn(dev, "%s: unexpected access layer: %x\n",
 541			 entity->label, control->layers);
 542
 543	return entity_parse_su_class(dev, function, entity, control, widget, route);
 544}
 545
 546static int entity_parse_mu(struct device *dev,
 547			   struct sdca_function_data *function,
 548			   struct sdca_entity *entity,
 549			   struct snd_soc_dapm_widget **widget,
 550			   struct snd_soc_dapm_route **route)
 551{
 552	struct sdca_control *control;
 553	struct snd_kcontrol_new *kctl;
 554	int i;
 555
 556	if (!entity->num_sources) {
 557		dev_err(dev, "%s: selector 1 or more inputs\n", entity->label);
 558		return -EINVAL;
 559	}
 560
 561	control = sdca_selector_find_control(dev, entity, SDCA_CTL_MU_MIXER);
 562	if (!control)
 563		return -EINVAL;
 564
 565	/* MU control should be through DAPM */
 566	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
 567		dev_warn(dev, "%s: unexpected access layer: %x\n",
 568			 entity->label, control->layers);
 569
 570	kctl = devm_kcalloc(dev, entity->num_sources, sizeof(*kctl), GFP_KERNEL);
 571	if (!kctl)
 572		return -ENOMEM;
 573
 574	for (i = 0; i < entity->num_sources; i++) {
 575		const char *control_name;
 576		struct soc_mixer_control *mc;
 577
 578		control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %d",
 579					      control->label, i + 1);
 580		if (!control_name)
 581			return -ENOMEM;
 582
 583		mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
 584		if (!mc)
 585			return -ENOMEM;
 586
 587		mc->reg = SND_SOC_NOPM;
 588		mc->rreg = SND_SOC_NOPM;
 589		mc->invert = 1; // Ensure default is connected
 590		mc->min = 0;
 591		mc->max = 1;
 592
 593		kctl[i].name = control_name;
 594		kctl[i].private_value = (unsigned long)mc;
 595		kctl[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 596		kctl[i].info = snd_soc_info_volsw;
 597		kctl[i].get = snd_soc_dapm_get_volsw;
 598		kctl[i].put = snd_soc_dapm_put_volsw;
 599	}
 600
 601	(*widget)->id = snd_soc_dapm_mixer;
 602	(*widget)->kcontrol_news = kctl;
 603	(*widget)->num_kcontrols = entity->num_sources;
 604	(*widget)++;
 605
 606	for (i = 0; i < entity->num_sources; i++)
 607		add_route(route, entity->label, kctl[i].name, entity->sources[i]->label);
 608
 609	return 0;
 610}
 611
 612static int entity_cs_event(struct snd_soc_dapm_widget *widget,
 613			   struct snd_kcontrol *kctl, int event)
 614{
 615	struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
 616	struct sdca_entity *entity = widget->priv;
 617
 618	if (!component)
 619		return -EIO;
 620
 621	if (entity->cs.max_delay)
 622		fsleep(entity->cs.max_delay);
 623
 624	return 0;
 625}
 626
 627static int entity_parse_cs(struct device *dev,
 628			   struct sdca_function_data *function,
 629			   struct sdca_entity *entity,
 630			   struct snd_soc_dapm_widget **widget,
 631			   struct snd_soc_dapm_route **route)
 632{
 633	int i;
 634
 635	(*widget)->id = snd_soc_dapm_supply;
 636	(*widget)->subseq = 1; /* Ensure these run after PDEs */
 637	(*widget)->event_flags = SND_SOC_DAPM_POST_PMU;
 638	(*widget)->event = entity_cs_event;
 639	(*widget)->priv = entity;
 640	(*widget)++;
 641
 642	for (i = 0; i < entity->num_sources; i++)
 643		add_route(route, entity->label, NULL, entity->sources[i]->label);
 644
 645	return 0;
 646}
 647
 648/**
 649 * sdca_asoc_populate_dapm - fill in arrays of DAPM widgets and routes
 650 * @dev: Pointer to the device against which allocations will be done.
 651 * @function: Pointer to the Function information.
 652 * @widget: Array of DAPM widgets to be populated.
 653 * @route: Array of DAPM routes to be populated.
 654 *
 655 * This function populates arrays of DAPM widgets and routes from the
 656 * DisCo information for a particular SDCA Function. Typically,
 657 * snd_soc_asoc_count_component will be used to allocate appropriately
 658 * sized arrays before calling this function.
 659 *
 660 * Return: Returns zero on success, and a negative error code on failure.
 661 */
 662int sdca_asoc_populate_dapm(struct device *dev, struct sdca_function_data *function,
 663			    struct snd_soc_dapm_widget *widget,
 664			    struct snd_soc_dapm_route *route)
 665{
 666	int ret;
 667	int i;
 668
 669	for (i = 0; i < function->num_entities - 1; i++) {
 670		struct sdca_entity *entity = &function->entities[i];
 671
 672		/*
 673		 * Some entities need to add controls "early" as they are
 674		 * referenced by other entities.
 675		 */
 676		switch (entity->type) {
 677		case SDCA_ENTITY_TYPE_GE:
 678			ret = entity_early_parse_ge(dev, function, entity);
 679			if (ret)
 680				return ret;
 681			break;
 682		default:
 683			break;
 684		}
 685	}
 686
 687	for (i = 0; i < function->num_entities - 1; i++) {
 688		struct sdca_entity *entity = &function->entities[i];
 689
 690		widget->name = entity->label;
 691		widget->reg = SND_SOC_NOPM;
 692
 693		switch (entity->type) {
 694		case SDCA_ENTITY_TYPE_IT:
 695			ret = entity_parse_it(dev, function, entity, &widget, &route);
 696			break;
 697		case SDCA_ENTITY_TYPE_OT:
 698			ret = entity_parse_ot(dev, function, entity, &widget, &route);
 699			break;
 700		case SDCA_ENTITY_TYPE_PDE:
 701			ret = entity_parse_pde(dev, function, entity, &widget, &route);
 702			break;
 703		case SDCA_ENTITY_TYPE_SU:
 704			ret = entity_parse_su(dev, function, entity, &widget, &route);
 705			break;
 706		case SDCA_ENTITY_TYPE_MU:
 707			ret = entity_parse_mu(dev, function, entity, &widget, &route);
 708			break;
 709		case SDCA_ENTITY_TYPE_CS:
 710			ret = entity_parse_cs(dev, function, entity, &widget, &route);
 711			break;
 712		case SDCA_ENTITY_TYPE_CX:
 713			/*
 714			 * FIXME: For now we will just treat these as a supply,
 715			 * meaning all options are enabled.
 716			 */
 717			dev_warn(dev, "%s: clock selectors not fully supported yet\n",
 718				 entity->label);
 719			ret = entity_parse_simple(dev, function, entity, &widget,
 720						  &route, snd_soc_dapm_supply);
 721			break;
 722		case SDCA_ENTITY_TYPE_TG:
 723			ret = entity_parse_simple(dev, function, entity, &widget,
 724						  &route, snd_soc_dapm_siggen);
 725			break;
 726		case SDCA_ENTITY_TYPE_GE:
 727			ret = entity_parse_simple(dev, function, entity, &widget,
 728						  &route, snd_soc_dapm_supply);
 729			break;
 730		default:
 731			ret = entity_parse_simple(dev, function, entity, &widget,
 732						  &route, snd_soc_dapm_pga);
 733			break;
 734		}
 735		if (ret)
 736			return ret;
 737
 738		if (entity->group)
 739			add_route(&route, entity->label, NULL, entity->group->label);
 740	}
 741
 742	return 0;
 743}
 744EXPORT_SYMBOL_NS(sdca_asoc_populate_dapm, "SND_SOC_SDCA");
 745
 746static int control_limit_kctl(struct device *dev,
 747			      struct sdca_entity *entity,
 748			      struct sdca_control *control,
 749			      struct snd_kcontrol_new *kctl)
 750{
 751	struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
 752	struct sdca_control_range *range;
 753	int min, max, step;
 754	unsigned int *tlv;
 755
 756	if (control->type != SDCA_CTL_DATATYPE_Q7P8DB)
 757		return 0;
 758
 759	/*
 760	 * FIXME: For now only handle the simple case of a single linear range
 761	 */
 762	range = sdca_control_find_range(dev, entity, control, SDCA_VOLUME_LINEAR_NCOLS, 1);
 763	if (!range)
 764		return -EINVAL;
 765
 766	min = sdca_range(range, SDCA_VOLUME_LINEAR_MIN, 0);
 767	max = sdca_range(range, SDCA_VOLUME_LINEAR_MAX, 0);
 768	step = sdca_range(range, SDCA_VOLUME_LINEAR_STEP, 0);
 769
 770	min = sign_extend32(min, control->nbits - 1);
 771	max = sign_extend32(max, control->nbits - 1);
 772
 773	tlv = devm_kcalloc(dev, 4, sizeof(*tlv), GFP_KERNEL);
 774	if (!tlv)
 775		return -ENOMEM;
 776
 777	tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
 778	tlv[1] = 2 * sizeof(*tlv);
 779	tlv[2] = (min * 100) >> 8;
 780	tlv[3] = (max * 100) >> 8;
 781
 782	step = (step * 100) >> 8;
 783
 784	mc->min = ((int)tlv[2] / step);
 785	mc->max = ((int)tlv[3] / step);
 786	mc->shift = step;
 787	mc->sign_bit = 15;
 788	mc->sdca_q78 = 1;
 789
 790	kctl->tlv.p = tlv;
 791	kctl->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
 792
 793	return 0;
 794}
 795
 796static int volatile_get_volsw(struct snd_kcontrol *kcontrol,
 797			      struct snd_ctl_elem_value *ucontrol)
 798{
 799	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 800	struct device *dev = component->dev;
 801	int ret;
 802
 803	ret = pm_runtime_resume_and_get(dev);
 804	if (ret < 0) {
 805		dev_err(dev, "failed to resume reading %s: %d\n",
 806			kcontrol->id.name, ret);
 807		return ret;
 808	}
 809
 810	ret = snd_soc_get_volsw(kcontrol, ucontrol);
 811
 812	pm_runtime_put(dev);
 813
 814	return ret;
 815}
 816
 817static int volatile_put_volsw(struct snd_kcontrol *kcontrol,
 818			      struct snd_ctl_elem_value *ucontrol)
 819{
 820	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 821	struct device *dev = component->dev;
 822	int ret;
 823
 824	ret = pm_runtime_resume_and_get(dev);
 825	if (ret < 0) {
 826		dev_err(dev, "failed to resume writing %s: %d\n",
 827			kcontrol->id.name, ret);
 828		return ret;
 829	}
 830
 831	ret = snd_soc_put_volsw(kcontrol, ucontrol);
 832
 833	pm_runtime_put(dev);
 834
 835	return ret;
 836}
 837
 838static int populate_control(struct device *dev,
 839			    struct sdca_function_data *function,
 840			    struct sdca_entity *entity,
 841			    struct sdca_control *control,
 842			    struct snd_kcontrol_new **kctl)
 843{
 844	const char *control_suffix = "";
 845	const char *control_name;
 846	struct soc_mixer_control *mc;
 847	int index = 0;
 848	int ret;
 849	int cn;
 850
 851	if (!exported_control(entity, control))
 852		return 0;
 853
 854	if (control->type == SDCA_CTL_DATATYPE_ONEBIT)
 855		control_suffix = " Switch";
 856
 857	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s%s", entity->label,
 858				      control->label, control_suffix);
 859	if (!control_name)
 860		return -ENOMEM;
 861
 862	mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
 863	if (!mc)
 864		return -ENOMEM;
 865
 866	for_each_set_bit(cn, (unsigned long *)&control->cn_list,
 867			 BITS_PER_TYPE(control->cn_list)) {
 868		switch (index++) {
 869		case 0:
 870			mc->reg = SDW_SDCA_CTL(function->desc->adr, entity->id,
 871					       control->sel, cn);
 872			mc->rreg = mc->reg;
 873			break;
 874		case 1:
 875			mc->rreg = SDW_SDCA_CTL(function->desc->adr, entity->id,
 876						control->sel, cn);
 877			break;
 878		default:
 879			dev_err(dev, "%s: %s: only mono/stereo controls supported\n",
 880				entity->label, control->label);
 881			return -EINVAL;
 882		}
 883	}
 884
 885	mc->min = 0;
 886	mc->max = clamp((0x1ull << control->nbits) - 1, 0, type_max(mc->max));
 887
 888	if (SDCA_CTL_TYPE(entity->type, control->sel) == SDCA_CTL_TYPE_S(FU, MUTE))
 889		mc->invert = true;
 890
 891	(*kctl)->name = control_name;
 892	(*kctl)->private_value = (unsigned long)mc;
 893	(*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 894	(*kctl)->info = snd_soc_info_volsw;
 895	if (control->is_volatile) {
 896		(*kctl)->get = volatile_get_volsw;
 897		(*kctl)->put = volatile_put_volsw;
 898	} else {
 899		(*kctl)->get = snd_soc_get_volsw;
 900		(*kctl)->put = snd_soc_put_volsw;
 901	}
 902
 903	if (readonly_control(control))
 904		(*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READ;
 905	else
 906		(*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
 907
 908	ret = control_limit_kctl(dev, entity, control, *kctl);
 909	if (ret)
 910		return ret;
 911
 912	(*kctl)++;
 913
 914	return 0;
 915}
 916
 917static int populate_pin_switch(struct device *dev,
 918			       struct sdca_entity *entity,
 919			       struct snd_kcontrol_new **kctl)
 920{
 921	const char *control_name;
 922
 923	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s Switch", entity->label);
 924	if (!control_name)
 925		return -ENOMEM;
 926
 927	(*kctl)->name = control_name;
 928	(*kctl)->private_value = (unsigned long)entity->label;
 929	(*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 930	(*kctl)->info = snd_soc_dapm_info_pin_switch;
 931	(*kctl)->get = snd_soc_dapm_get_component_pin_switch;
 932	(*kctl)->put = snd_soc_dapm_put_component_pin_switch;
 933	(*kctl)++;
 934
 935	return 0;
 936}
 937
 938/**
 939 * sdca_asoc_populate_controls - fill in an array of ALSA controls for a Function
 940 * @dev: Pointer to the device against which allocations will be done.
 941 * @function: Pointer to the Function information.
 942 * @kctl: Array of ALSA controls to be populated.
 943 *
 944 * This function populates an array of ALSA controls from the DisCo
 945 * information for a particular SDCA Function. Typically,
 946 * snd_soc_asoc_count_component will be used to allocate an
 947 * appropriately sized array before calling this function.
 948 *
 949 * Return: Returns zero on success, and a negative error code on failure.
 950 */
 951int sdca_asoc_populate_controls(struct device *dev,
 952				struct sdca_function_data *function,
 953				struct snd_kcontrol_new *kctl)
 954{
 955	int i, j;
 956	int ret;
 957
 958	for (i = 0; i < function->num_entities; i++) {
 959		struct sdca_entity *entity = &function->entities[i];
 960
 961		switch (entity->type) {
 962		case SDCA_ENTITY_TYPE_IT:
 963		case SDCA_ENTITY_TYPE_OT:
 964			if (!entity->iot.is_dataport) {
 965				ret = populate_pin_switch(dev, entity, &kctl);
 966				if (ret)
 967					return ret;
 968			}
 969			break;
 970		default:
 971			break;
 972		}
 973
 974		for (j = 0; j < entity->num_controls; j++) {
 975			ret = populate_control(dev, function, entity,
 976					       &entity->controls[j], &kctl);
 977			if (ret)
 978				return ret;
 979		}
 980	}
 981
 982	return 0;
 983}
 984EXPORT_SYMBOL_NS(sdca_asoc_populate_controls, "SND_SOC_SDCA");
 985
 986static unsigned int rate_find_mask(unsigned int rate)
 987{
 988	switch (rate) {
 989	case 0:
 990		return SNDRV_PCM_RATE_8000_768000;
 991	case 5512:
 992		return SNDRV_PCM_RATE_5512;
 993	case 8000:
 994		return SNDRV_PCM_RATE_8000;
 995	case 11025:
 996		return SNDRV_PCM_RATE_11025;
 997	case 16000:
 998		return SNDRV_PCM_RATE_16000;
 999	case 22050:
1000		return SNDRV_PCM_RATE_22050;
1001	case 32000:
1002		return SNDRV_PCM_RATE_32000;
1003	case 44100:
1004		return SNDRV_PCM_RATE_44100;
1005	case 48000:
1006		return SNDRV_PCM_RATE_48000;
1007	case 64000:
1008		return SNDRV_PCM_RATE_64000;
1009	case 88200:
1010		return SNDRV_PCM_RATE_88200;
1011	case 96000:
1012		return SNDRV_PCM_RATE_96000;
1013	case 176400:
1014		return SNDRV_PCM_RATE_176400;
1015	case 192000:
1016		return SNDRV_PCM_RATE_192000;
1017	case 352800:
1018		return SNDRV_PCM_RATE_352800;
1019	case 384000:
1020		return SNDRV_PCM_RATE_384000;
1021	case 705600:
1022		return SNDRV_PCM_RATE_705600;
1023	case 768000:
1024		return SNDRV_PCM_RATE_768000;
1025	case 12000:
1026		return SNDRV_PCM_RATE_12000;
1027	case 24000:
1028		return SNDRV_PCM_RATE_24000;
1029	case 128000:
1030		return SNDRV_PCM_RATE_128000;
1031	default:
1032		return 0;
1033	}
1034}
1035
1036static u64 width_find_mask(unsigned int bits)
1037{
1038	switch (bits) {
1039	case 0:
1040		return SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
1041		       SNDRV_PCM_FMTBIT_S20_LE | SNDRV_PCM_FMTBIT_S24_LE |
1042		       SNDRV_PCM_FMTBIT_S32_LE;
1043	case 8:
1044		return SNDRV_PCM_FMTBIT_S8;
1045	case 16:
1046		return SNDRV_PCM_FMTBIT_S16_LE;
1047	case 20:
1048		return SNDRV_PCM_FMTBIT_S20_LE;
1049	case 24:
1050		return SNDRV_PCM_FMTBIT_S24_LE;
1051	case 32:
1052		return SNDRV_PCM_FMTBIT_S32_LE;
1053	default:
1054		return 0;
1055	}
1056}
1057
1058static int populate_rate_format(struct device *dev,
1059				struct sdca_function_data *function,
1060				struct sdca_entity *entity,
1061				struct snd_soc_pcm_stream *stream)
1062{
1063	struct sdca_control_range *range;
1064	unsigned int sample_rate, sample_width;
1065	unsigned int clock_rates = 0;
1066	unsigned int rates = 0;
1067	u64 formats = 0;
1068	int sel, i;
1069
1070	switch (entity->type) {
1071	case SDCA_ENTITY_TYPE_IT:
1072		sel = SDCA_CTL_IT_USAGE;
1073		break;
1074	case SDCA_ENTITY_TYPE_OT:
1075		sel = SDCA_CTL_OT_USAGE;
1076		break;
1077	default:
1078		dev_err(dev, "%s: entity type has no usage control\n",
1079			entity->label);
1080		return -EINVAL;
1081	}
1082
1083	if (entity->iot.clock) {
1084		range = sdca_selector_find_range(dev, entity->iot.clock,
1085						 SDCA_CTL_CS_SAMPLERATEINDEX,
1086						 SDCA_SAMPLERATEINDEX_NCOLS, 0);
1087		if (!range)
1088			return -EINVAL;
1089
1090		for (i = 0; i < range->rows; i++) {
1091			sample_rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
1092			clock_rates |= rate_find_mask(sample_rate);
1093		}
1094	} else {
1095		clock_rates = UINT_MAX;
1096	}
1097
1098	range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
1099	if (!range)
1100		return -EINVAL;
1101
1102	for (i = 0; i < range->rows; i++) {
1103		sample_rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
1104		sample_rate = rate_find_mask(sample_rate);
1105
1106		if (sample_rate & clock_rates) {
1107			rates |= sample_rate;
1108
1109			sample_width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
1110			formats |= width_find_mask(sample_width);
1111		}
1112	}
1113
1114	stream->formats = formats;
1115	stream->rates = rates;
1116
1117	return 0;
1118}
1119
1120/**
1121 * sdca_asoc_populate_dais - fill in an array of DAI drivers for a Function
1122 * @dev: Pointer to the device against which allocations will be done.
1123 * @function: Pointer to the Function information.
1124 * @dais: Array of DAI drivers to be populated.
1125 * @ops: DAI ops to be attached to each of the created DAI drivers.
1126 *
1127 * This function populates an array of ASoC DAI drivers from the DisCo
1128 * information for a particular SDCA Function. Typically,
1129 * snd_soc_asoc_count_component will be used to allocate an
1130 * appropriately sized array before calling this function.
1131 *
1132 * Return: Returns zero on success, and a negative error code on failure.
1133 */
1134int sdca_asoc_populate_dais(struct device *dev, struct sdca_function_data *function,
1135			    struct snd_soc_dai_driver *dais,
1136			    const struct snd_soc_dai_ops *ops)
1137{
1138	int i, j;
1139	int ret;
1140
1141	for (i = 0, j = 0; i < function->num_entities - 1; i++) {
1142		struct sdca_entity *entity = &function->entities[i];
1143		struct snd_soc_pcm_stream *stream;
1144		const char *stream_suffix;
1145
1146		switch (entity->type) {
1147		case SDCA_ENTITY_TYPE_IT:
1148			stream = &dais[j].playback;
1149			stream_suffix = "Playback";
1150			break;
1151		case SDCA_ENTITY_TYPE_OT:
1152			stream = &dais[j].capture;
1153			stream_suffix = "Capture";
1154			break;
1155		default:
1156			continue;
1157		}
1158
1159		/* Can't check earlier as only terminals have an iot member. */
1160		if (!entity->iot.is_dataport)
1161			continue;
1162
1163		stream->stream_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
1164						     entity->label, stream_suffix);
1165		if (!stream->stream_name)
1166			return -ENOMEM;
1167		/* Channels will be further limited by constraints */
1168		stream->channels_min = 1;
1169		stream->channels_max = SDCA_MAX_CHANNEL_COUNT;
1170
1171		ret = populate_rate_format(dev, function, entity, stream);
1172		if (ret)
1173			return ret;
1174
1175		dais[j].id = i;
1176		dais[j].name = entity->label;
1177		dais[j].ops = ops;
1178		j++;
1179	}
1180
1181	return 0;
1182}
1183EXPORT_SYMBOL_NS(sdca_asoc_populate_dais, "SND_SOC_SDCA");
1184
1185/**
1186 * sdca_asoc_populate_component - fill in a component driver for a Function
1187 * @dev: Pointer to the device against which allocations will be done.
1188 * @function: Pointer to the Function information.
1189 * @component_drv: Pointer to the component driver to be populated.
1190 * @dai_drv: Pointer to the DAI driver array to be allocated and populated.
1191 * @num_dai_drv: Pointer to integer that will be populated with the number of
1192 * DAI drivers.
1193 * @ops: DAI ops pointer that will be used for each DAI driver.
1194 *
1195 * This function populates a snd_soc_component_driver structure based
1196 * on the DisCo information for a particular SDCA Function. It does
1197 * all allocation internally.
1198 *
1199 * Return: Returns zero on success, and a negative error code on failure.
1200 */
1201int sdca_asoc_populate_component(struct device *dev,
1202				 struct sdca_function_data *function,
1203				 struct snd_soc_component_driver *component_drv,
1204				 struct snd_soc_dai_driver **dai_drv, int *num_dai_drv,
1205				 const struct snd_soc_dai_ops *ops)
1206{
1207	struct snd_soc_dapm_widget *widgets;
1208	struct snd_soc_dapm_route *routes;
1209	struct snd_kcontrol_new *controls;
1210	struct snd_soc_dai_driver *dais;
1211	int num_widgets, num_routes, num_controls, num_dais;
1212	int ret;
1213
1214	ret = sdca_asoc_count_component(dev, function, &num_widgets, &num_routes,
1215					&num_controls, &num_dais);
1216	if (ret)
1217		return ret;
1218
1219	widgets = devm_kcalloc(dev, num_widgets, sizeof(*widgets), GFP_KERNEL);
1220	if (!widgets)
1221		return -ENOMEM;
1222
1223	routes = devm_kcalloc(dev, num_routes, sizeof(*routes), GFP_KERNEL);
1224	if (!routes)
1225		return -ENOMEM;
1226
1227	controls = devm_kcalloc(dev, num_controls, sizeof(*controls), GFP_KERNEL);
1228	if (!controls)
1229		return -ENOMEM;
1230
1231	dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
1232	if (!dais)
1233		return -ENOMEM;
1234
1235	ret = sdca_asoc_populate_dapm(dev, function, widgets, routes);
1236	if (ret)
1237		return ret;
1238
1239	ret = sdca_asoc_populate_controls(dev, function, controls);
1240	if (ret)
1241		return ret;
1242
1243	ret = sdca_asoc_populate_dais(dev, function, dais, ops);
1244	if (ret)
1245		return ret;
1246
1247	component_drv->dapm_widgets = widgets;
1248	component_drv->num_dapm_widgets = num_widgets;
1249	component_drv->dapm_routes = routes;
1250	component_drv->num_dapm_routes = num_routes;
1251	component_drv->controls = controls;
1252	component_drv->num_controls = num_controls;
1253
1254	*dai_drv = dais;
1255	*num_dai_drv = num_dais;
1256
1257	return 0;
1258}
1259EXPORT_SYMBOL_NS(sdca_asoc_populate_component, "SND_SOC_SDCA");
1260
1261/**
1262 * sdca_asoc_set_constraints - constrain channels available on a DAI
1263 * @dev: Pointer to the device, used for error messages.
1264 * @regmap: Pointer to the Function register map.
1265 * @function: Pointer to the Function information.
1266 * @substream: Pointer to the PCM substream.
1267 * @dai: Pointer to the ASoC DAI.
1268 *
1269 * Typically called from startup().
1270 *
1271 * Return: Returns zero on success, and a negative error code on failure.
1272 */
1273int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
1274			      struct sdca_function_data *function,
1275			      struct snd_pcm_substream *substream,
1276			      struct snd_soc_dai *dai)
1277{
1278	static const unsigned int channel_list[] = {
1279		 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
1280		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1281	};
1282	struct sdca_entity *entity = &function->entities[dai->id];
1283	struct snd_pcm_hw_constraint_list *constraint;
1284	struct sdca_control_range *range;
1285	struct sdca_control *control;
1286	unsigned int channel_mask = 0;
1287	int i, ret;
1288
1289	static_assert(ARRAY_SIZE(channel_list) == SDCA_MAX_CHANNEL_COUNT);
1290	static_assert(sizeof(channel_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT);
1291
1292	if (entity->type != SDCA_ENTITY_TYPE_IT)
1293		return 0;
1294
1295	control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX);
1296	if (!control)
1297		return -EINVAL;
1298
1299	range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0);
1300	if (!range)
1301		return -EINVAL;
1302
1303	for (i = 0; i < range->rows; i++) {
1304		int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
1305		struct sdca_cluster *cluster;
1306
1307		cluster = sdca_id_find_cluster(dev, function, clusterid);
1308		if (!cluster)
1309			return -ENODEV;
1310
1311		channel_mask |= (1 << (cluster->num_channels - 1));
1312	}
1313
1314	dev_dbg(dev, "%s: set channel constraint mask: %#x\n",
1315		entity->label, channel_mask);
1316
1317	constraint = kzalloc(sizeof(*constraint), GFP_KERNEL);
1318	if (!constraint)
1319		return -ENOMEM;
1320
1321	constraint->count = ARRAY_SIZE(channel_list);
1322	constraint->list = channel_list;
1323	constraint->mask = channel_mask;
1324
1325	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
1326					 SNDRV_PCM_HW_PARAM_CHANNELS,
1327					 constraint);
1328	if (ret) {
1329		dev_err(dev, "%s: failed to add constraint: %d\n", entity->label, ret);
1330		kfree(constraint);
1331		return ret;
1332	}
1333
1334	dai->priv = constraint;
1335
1336	return 0;
1337}
1338EXPORT_SYMBOL_NS(sdca_asoc_set_constraints, "SND_SOC_SDCA");
1339
1340/**
1341 * sdca_asoc_free_constraints - free constraint allocations
1342 * @substream: Pointer to the PCM substream.
1343 * @dai: Pointer to the ASoC DAI.
1344 *
1345 * Typically called from shutdown().
1346 */
1347void sdca_asoc_free_constraints(struct snd_pcm_substream *substream,
1348				struct snd_soc_dai *dai)
1349{
1350	struct snd_pcm_hw_constraint_list *constraint = dai->priv;
1351
1352	kfree(constraint);
1353}
1354EXPORT_SYMBOL_NS(sdca_asoc_free_constraints, "SND_SOC_SDCA");
1355
1356/**
1357 * sdca_asoc_get_port - return SoundWire port for a DAI
1358 * @dev: Pointer to the device, used for error messages.
1359 * @regmap: Pointer to the Function register map.
1360 * @function: Pointer to the Function information.
1361 * @dai: Pointer to the ASoC DAI.
1362 *
1363 * Typically called from hw_params().
1364 *
1365 * Return: Returns a positive port number on success, and a negative error
1366 * code on failure.
1367 */
1368int sdca_asoc_get_port(struct device *dev, struct regmap *regmap,
1369		       struct sdca_function_data *function,
1370		       struct snd_soc_dai *dai)
1371{
1372	struct sdca_entity *entity = &function->entities[dai->id];
1373	struct sdca_control_range *range;
1374	unsigned int reg, val;
1375	int sel = -EINVAL;
1376	int i, ret;
1377
1378	switch (entity->type) {
1379	case SDCA_ENTITY_TYPE_IT:
1380		sel = SDCA_CTL_IT_DATAPORT_SELECTOR;
1381		break;
1382	case SDCA_ENTITY_TYPE_OT:
1383		sel = SDCA_CTL_OT_DATAPORT_SELECTOR;
1384		break;
1385	default:
1386		break;
1387	}
1388
1389	if (sel < 0 || !entity->iot.is_dataport) {
1390		dev_err(dev, "%s: port number only available for dataports\n",
1391			entity->label);
1392		return -EINVAL;
1393	}
1394
1395	range = sdca_selector_find_range(dev, entity, sel, SDCA_DATAPORT_SELECTOR_NCOLS,
1396					 SDCA_DATAPORT_SELECTOR_NROWS);
1397	if (!range)
1398		return -EINVAL;
1399
1400	reg = SDW_SDCA_CTL(function->desc->adr, entity->id, sel, 0);
1401
1402	ret = regmap_read(regmap, reg, &val);
1403	if (ret) {
1404		dev_err(dev, "%s: failed to read dataport selector: %d\n",
1405			entity->label, ret);
1406		return ret;
1407	}
1408
1409	for (i = 0; i < range->rows; i++) {
1410		static const u8 port_mask = 0xF;
1411
1412		sel = sdca_range(range, val & port_mask, i);
1413
1414		/*
1415		 * FIXME: Currently only a single dataport is supported, so
1416		 * return the first one found, technically up to 4 dataports
1417		 * could be linked, but this is not yet supported.
1418		 */
1419		if (sel != 0xFF)
1420			return sel;
1421
1422		val >>= hweight8(port_mask);
1423	}
1424
1425	dev_err(dev, "%s: no dataport found\n", entity->label);
1426	return -ENODEV;
1427}
1428EXPORT_SYMBOL_NS(sdca_asoc_get_port, "SND_SOC_SDCA");
1429
1430static int set_cluster(struct device *dev, struct regmap *regmap,
1431		       struct sdca_function_data *function,
1432		       struct sdca_entity *entity, unsigned int channels)
1433{
1434	int sel = SDCA_CTL_IT_CLUSTERINDEX;
1435	struct sdca_control_range *range;
1436	int i, ret;
1437
1438	range = sdca_selector_find_range(dev, entity, sel, SDCA_CLUSTER_NCOLS, 0);
1439	if (!range)
1440		return -EINVAL;
1441
1442	for (i = 0; i < range->rows; i++) {
1443		int cluster_id = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
1444		struct sdca_cluster *cluster;
1445
1446		cluster = sdca_id_find_cluster(dev, function, cluster_id);
1447		if (!cluster)
1448			return -ENODEV;
1449
1450		if (cluster->num_channels == channels) {
1451			int index = sdca_range(range, SDCA_CLUSTER_BYTEINDEX, i);
1452			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1453							entity->id, sel, 0);
1454
1455			ret = regmap_update_bits(regmap, reg, 0xFF, index);
1456			if (ret) {
1457				dev_err(dev, "%s: failed to write cluster index: %d\n",
1458					entity->label, ret);
1459				return ret;
1460			}
1461
1462			dev_dbg(dev, "%s: set cluster to %d (%d channels)\n",
1463				entity->label, index, channels);
1464
1465			return 0;
1466		}
1467	}
1468
1469	dev_err(dev, "%s: no cluster for %d channels\n", entity->label, channels);
1470	return -EINVAL;
1471}
1472
1473static int set_clock(struct device *dev, struct regmap *regmap,
1474		     struct sdca_function_data *function,
1475		     struct sdca_entity *entity, int target_rate)
1476{
1477	int sel = SDCA_CTL_CS_SAMPLERATEINDEX;
1478	struct sdca_control_range *range;
1479	int i, ret;
1480
1481	range = sdca_selector_find_range(dev, entity, sel, SDCA_SAMPLERATEINDEX_NCOLS, 0);
1482	if (!range)
1483		return -EINVAL;
1484
1485	for (i = 0; i < range->rows; i++) {
1486		unsigned int rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
1487
1488		if (rate == target_rate) {
1489			unsigned int index = sdca_range(range,
1490							SDCA_SAMPLERATEINDEX_INDEX,
1491							i);
1492			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1493							entity->id, sel, 0);
1494
1495			ret = regmap_update_bits(regmap, reg, 0xFF, index);
1496			if (ret) {
1497				dev_err(dev, "%s: failed to write clock rate: %d\n",
1498					entity->label, ret);
1499				return ret;
1500			}
1501
1502			dev_dbg(dev, "%s: set clock rate to %d (%dHz)\n",
1503				entity->label, index, rate);
1504
1505			return 0;
1506		}
1507	}
1508
1509	dev_err(dev, "%s: no clock rate for %dHz\n", entity->label, target_rate);
1510	return -EINVAL;
1511}
1512
1513static int set_usage(struct device *dev, struct regmap *regmap,
1514		     struct sdca_function_data *function,
1515		     struct sdca_entity *entity, int sel,
1516		     int target_rate, int target_width)
1517{
1518	struct sdca_control_range *range;
1519	int i, ret;
1520
1521	range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
1522	if (!range)
1523		return -EINVAL;
1524
1525	for (i = 0; i < range->rows; i++) {
1526		unsigned int rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
1527		unsigned int width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
1528
1529		if ((!rate || rate == target_rate) && (!width || width == target_width)) {
1530			unsigned int usage = sdca_range(range, SDCA_USAGE_NUMBER, i);
1531			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1532							entity->id, sel, 0);
1533
1534			ret = regmap_update_bits(regmap, reg, 0xFF, usage);
1535			if (ret) {
1536				dev_err(dev, "%s: failed to write usage: %d\n",
1537					entity->label, ret);
1538				return ret;
1539			}
1540
1541			dev_dbg(dev, "%s: set usage to %#x (%dHz, %d bits)\n",
1542				entity->label, usage, target_rate, target_width);
1543
1544			return 0;
1545		}
1546	}
1547
1548	dev_err(dev, "%s: no usage for %dHz, %dbits\n",
1549		entity->label, target_rate, target_width);
1550	return -EINVAL;
1551}
1552
1553/**
1554 * sdca_asoc_hw_params - set SDCA channels, sample rate and bit depth
1555 * @dev: Pointer to the device, used for error messages.
1556 * @regmap: Pointer to the Function register map.
1557 * @function: Pointer to the Function information.
1558 * @substream: Pointer to the PCM substream.
1559 * @params: Pointer to the hardware parameters.
1560 * @dai: Pointer to the ASoC DAI.
1561 *
1562 * Typically called from hw_params().
1563 *
1564 * Return: Returns zero on success, and a negative error code on failure.
1565 */
1566int sdca_asoc_hw_params(struct device *dev, struct regmap *regmap,
1567			struct sdca_function_data *function,
1568			struct snd_pcm_substream *substream,
1569			struct snd_pcm_hw_params *params,
1570			struct snd_soc_dai *dai)
1571{
1572	struct sdca_entity *entity = &function->entities[dai->id];
1573	int channels = params_channels(params);
1574	int width = params_width(params);
1575	int rate = params_rate(params);
1576	int usage_sel;
1577	int ret;
1578
1579	switch (entity->type) {
1580	case SDCA_ENTITY_TYPE_IT:
1581		ret = set_cluster(dev, regmap, function, entity, channels);
1582		if (ret)
1583			return ret;
1584
1585		usage_sel = SDCA_CTL_IT_USAGE;
1586		break;
1587	case SDCA_ENTITY_TYPE_OT:
1588		usage_sel = SDCA_CTL_OT_USAGE;
1589		break;
1590	default:
1591		dev_err(dev, "%s: hw_params on non-terminal entity\n", entity->label);
1592		return -EINVAL;
1593	}
1594
1595	if (entity->iot.clock) {
1596		ret = set_clock(dev, regmap, function, entity->iot.clock, rate);
1597		if (ret)
1598			return ret;
1599	}
1600
1601	ret = set_usage(dev, regmap, function, entity, usage_sel, rate, width);
1602	if (ret)
1603		return ret;
1604
1605	return 0;
1606}
1607EXPORT_SYMBOL_NS(sdca_asoc_hw_params, "SND_SOC_SDCA");
~/ Read-only file preview.