Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// simple-card-utils.c
   4//
   5// Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
   6
   7#include <dt-bindings/sound/audio-graph.h>
   8#include <linux/cleanup.h>
   9#include <linux/clk.h>
  10#include <linux/gpio/consumer.h>
  11#include <linux/module.h>
  12#include <linux/of.h>
  13#include <linux/of_graph.h>
  14#include <sound/jack.h>
  15#include <sound/pcm_params.h>
  16#include <sound/simple_card_utils.h>
  17
  18int simple_util_get_sample_fmt(struct simple_util_data *data)
 
  19{
  20	int i;
  21	int val = -EINVAL;
  22
  23	struct {
  24		char *fmt;
  25		u32 val;
  26	} of_sample_fmt_table[] = {
  27		{ "s8",		SNDRV_PCM_FORMAT_S8},
  28		{ "s16_le",	SNDRV_PCM_FORMAT_S16_LE},
  29		{ "s24_le",	SNDRV_PCM_FORMAT_S24_LE},
  30		{ "s24_3le",	SNDRV_PCM_FORMAT_S24_3LE},
  31		{ "s32_le",	SNDRV_PCM_FORMAT_S32_LE},
  32	};
  33
  34	for (i = 0; i < ARRAY_SIZE(of_sample_fmt_table); i++) {
  35		if (!strcmp(data->convert_sample_format,
  36			    of_sample_fmt_table[i].fmt)) {
  37			val = of_sample_fmt_table[i].val;
 
  38			break;
  39		}
  40	}
  41	return val;
  42}
  43EXPORT_SYMBOL_GPL(simple_util_get_sample_fmt);
  44
  45static void simple_fixup_sample_fmt(struct simple_util_data *data,
  46				    struct snd_pcm_hw_params *params)
  47{
  48	int val;
  49	struct snd_mask *mask = hw_param_mask(params,
  50					      SNDRV_PCM_HW_PARAM_FORMAT);
  51
  52	val = simple_util_get_sample_fmt(data);
  53	if (val >= 0) {
  54		snd_mask_none(mask);
  55		snd_mask_set(mask, val);
  56	}
  57}
  58
  59void simple_util_parse_convert(struct device_node *np,
  60			       char *prefix,
  61			       struct simple_util_data *data)
  62{
  63	char prop[128];
  64
  65	if (!np)
  66		return;
  67
  68	if (!prefix)
  69		prefix = "";
  70
  71	/* sampling rate convert */
  72	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
  73	of_property_read_u32(np, prop, &data->convert_rate);
  74
  75	/* channels transfer */
  76	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
  77	of_property_read_u32(np, prop, &data->convert_channels);
  78
  79	/* convert sample format */
  80	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-sample-format");
  81	of_property_read_string(np, prop, &data->convert_sample_format);
  82}
  83EXPORT_SYMBOL_GPL(simple_util_parse_convert);
  84
  85/**
  86 * simple_util_is_convert_required() - Query if HW param conversion was requested
  87 * @data: Link data.
  88 *
  89 * Returns true if any HW param conversion was requested for this DAI link with
  90 * any "convert-xxx" properties.
  91 */
  92bool simple_util_is_convert_required(const struct simple_util_data *data)
  93{
  94	return data->convert_rate ||
  95	       data->convert_channels ||
  96	       data->convert_sample_format;
  97}
  98EXPORT_SYMBOL_GPL(simple_util_is_convert_required);
  99
 100int simple_util_parse_daifmt(struct device *dev,
 101			     struct device_node *node,
 102			     struct device_node *codec,
 103			     char *prefix,
 104			     unsigned int *retfmt)
 105{
 106	struct device_node *bitclkmaster = NULL;
 107	struct device_node *framemaster = NULL;
 108	unsigned int daifmt;
 109
 110	daifmt = snd_soc_daifmt_parse_format(node, prefix);
 111
 112	snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
 113	if (!bitclkmaster && !framemaster) {
 114		/*
 115		 * No dai-link level and master setting was not found from
 116		 * sound node level, revert back to legacy DT parsing and
 117		 * take the settings from codec node.
 118		 */
 119		dev_dbg(dev, "Revert to legacy daifmt parsing\n");
 120
 121		daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
 122	} else {
 123		daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
 124				((codec == bitclkmaster) << 4) | (codec == framemaster));
 125	}
 126
 127	of_node_put(bitclkmaster);
 128	of_node_put(framemaster);
 129
 130	*retfmt = daifmt;
 131
 132	return 0;
 133}
 134EXPORT_SYMBOL_GPL(simple_util_parse_daifmt);
 135
 136int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
 137				    struct simple_util_dai *dai)
 138{
 
 139	int n, i, ret;
 140	u32 *p;
 141
 142	if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
 143		return 0;
 144
 145	n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
 146	if (n % 3) {
 147		dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
 148		return -EINVAL;
 149	}
 150
 151	dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
 152	if (!dai->tdm_width_map)
 153		return -ENOMEM;
 154
 155	u32 *array_values __free(kfree) = kcalloc(n, sizeof(*array_values),
 156						  GFP_KERNEL);
 157	if (!array_values)
 158		return -ENOMEM;
 159
 160	ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
 161	if (ret < 0) {
 162		dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
 163		return ret;
 164	}
 165
 166	p = array_values;
 167	for (i = 0; i < n / 3; ++i) {
 168		dai->tdm_width_map[i].sample_bits = *p++;
 169		dai->tdm_width_map[i].slot_width = *p++;
 170		dai->tdm_width_map[i].slot_count = *p++;
 171	}
 172
 173	dai->n_tdm_widths = i;
 
 
 
 174
 175	return 0;
 176}
 177EXPORT_SYMBOL_GPL(simple_util_parse_tdm_width_map);
 178
 179int simple_util_set_dailink_name(struct device *dev,
 180				 struct snd_soc_dai_link *dai_link,
 181				 const char *fmt, ...)
 182{
 183	va_list ap;
 184	char *name = NULL;
 185	int ret = -ENOMEM;
 186
 187	va_start(ap, fmt);
 188	name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
 189	va_end(ap);
 190
 191	if (name) {
 192		ret = 0;
 193
 194		dai_link->name		= name;
 195		dai_link->stream_name	= name;
 196	}
 197
 198	return ret;
 199}
 200EXPORT_SYMBOL_GPL(simple_util_set_dailink_name);
 201
 202int simple_util_parse_card_name(struct snd_soc_card *card,
 203				char *prefix)
 204{
 205	int ret;
 206
 207	if (!prefix)
 208		prefix = "";
 209
 210	/* Parse the card name from DT */
 211	ret = snd_soc_of_parse_card_name(card, "label");
 212	if (ret < 0 || !card->name) {
 213		char prop[128];
 214
 215		snprintf(prop, sizeof(prop), "%sname", prefix);
 216		ret = snd_soc_of_parse_card_name(card, prop);
 217		if (ret < 0)
 218			return ret;
 219	}
 220
 221	if (!card->name && card->dai_link)
 222		card->name = card->dai_link->name;
 223
 224	return 0;
 225}
 226EXPORT_SYMBOL_GPL(simple_util_parse_card_name);
 227
 228static int simple_clk_enable(struct simple_util_dai *dai)
 229{
 230	if (dai)
 231		return clk_prepare_enable(dai->clk);
 232
 233	return 0;
 234}
 235
 236static void simple_clk_disable(struct simple_util_dai *dai)
 237{
 238	if (dai)
 239		clk_disable_unprepare(dai->clk);
 240}
 241
 242int simple_util_parse_clk(struct device *dev,
 243			  struct device_node *node,
 244			  struct simple_util_dai *simple_dai,
 245			  struct snd_soc_dai_link_component *dlc)
 246{
 247	struct clk *clk;
 248	u32 val;
 249
 250	/*
 251	 * Parse dai->sysclk come from "clocks = <&xxx>"
 252	 * (if system has common clock)
 253	 *  or "system-clock-frequency = <xxx>"
 254	 *  or device's module clock.
 255	 */
 256	clk = devm_get_clk_from_child(dev, node, NULL);
 257	simple_dai->clk_fixed = of_property_read_bool(
 258		node, "system-clock-fixed");
 259	if (!IS_ERR(clk)) {
 260		simple_dai->sysclk = clk_get_rate(clk);
 261
 262		simple_dai->clk = clk;
 263	} else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
 264		simple_dai->sysclk = val;
 265		simple_dai->clk_fixed = true;
 266	} else {
 267		clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
 268		if (!IS_ERR(clk))
 269			simple_dai->sysclk = clk_get_rate(clk);
 270	}
 271
 272	if (of_property_read_bool(node, "system-clock-direction-out"))
 273		simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
 274
 275	return 0;
 276}
 277EXPORT_SYMBOL_GPL(simple_util_parse_clk);
 278
 279static int simple_check_fixed_sysclk(struct device *dev,
 280					  struct simple_util_dai *dai,
 281					  unsigned int *fixed_sysclk)
 282{
 283	if (dai->clk_fixed) {
 284		if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
 285			dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
 286				*fixed_sysclk, dai->sysclk);
 287			return -EINVAL;
 288		}
 289		*fixed_sysclk = dai->sysclk;
 290	}
 291
 292	return 0;
 293}
 294
 295int simple_util_startup(struct snd_pcm_substream *substream)
 296{
 297	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 298	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 299	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->id);
 300	struct simple_util_dai *dai;
 301	unsigned int fixed_sysclk = 0;
 302	int i1, i2, i;
 303	int ret;
 304
 305	for_each_prop_dai_cpu(props, i1, dai) {
 306		ret = simple_clk_enable(dai);
 307		if (ret)
 308			goto cpu_err;
 309		ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
 310		if (ret)
 311			goto cpu_err;
 312	}
 313
 314	for_each_prop_dai_codec(props, i2, dai) {
 315		ret = simple_clk_enable(dai);
 316		if (ret)
 317			goto codec_err;
 318		ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
 319		if (ret)
 320			goto codec_err;
 321	}
 322
 323	if (fixed_sysclk && props->mclk_fs) {
 324		unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
 325
 326		if (fixed_sysclk % props->mclk_fs) {
 327			dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
 328				fixed_sysclk, props->mclk_fs);
 329			ret = -EINVAL;
 330			goto codec_err;
 331		}
 332		ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
 333			fixed_rate, fixed_rate);
 334		if (ret < 0)
 335			goto codec_err;
 336	}
 337
 338	return 0;
 339
 340codec_err:
 341	for_each_prop_dai_codec(props, i, dai) {
 342		if (i >= i2)
 343			break;
 344		simple_clk_disable(dai);
 345	}
 346cpu_err:
 347	for_each_prop_dai_cpu(props, i, dai) {
 348		if (i >= i1)
 349			break;
 350		simple_clk_disable(dai);
 351	}
 352	return ret;
 353}
 354EXPORT_SYMBOL_GPL(simple_util_startup);
 355
 356void simple_util_shutdown(struct snd_pcm_substream *substream)
 357{
 358	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 359	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 360	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->id);
 361	struct simple_util_dai *dai;
 362	int i;
 363
 364	for_each_prop_dai_cpu(props, i, dai) {
 365		struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, i);
 366
 367		if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
 368			snd_soc_dai_set_sysclk(cpu_dai,
 369					       0, 0, SND_SOC_CLOCK_OUT);
 370
 371		simple_clk_disable(dai);
 372	}
 373	for_each_prop_dai_codec(props, i, dai) {
 374		struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, i);
 375
 376		if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
 377			snd_soc_dai_set_sysclk(codec_dai,
 378					       0, 0, SND_SOC_CLOCK_IN);
 379
 380		simple_clk_disable(dai);
 381	}
 382}
 383EXPORT_SYMBOL_GPL(simple_util_shutdown);
 384
 385static int simple_set_clk_rate(struct device *dev,
 386				    struct simple_util_dai *simple_dai,
 387				    unsigned long rate)
 388{
 389	if (!simple_dai)
 390		return 0;
 391
 392	if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
 393		dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
 394		return -EINVAL;
 395	}
 396
 397	if (!simple_dai->clk)
 398		return 0;
 399
 400	if (clk_get_rate(simple_dai->clk) == rate)
 401		return 0;
 402
 403	return clk_set_rate(simple_dai->clk, rate);
 404}
 405
 406static int simple_set_tdm(struct snd_soc_dai *dai,
 407				struct simple_util_dai *simple_dai,
 408				struct snd_pcm_hw_params *params)
 409{
 410	int sample_bits = params_width(params);
 411	int slot_width, slot_count;
 412	int i, ret;
 413
 414	if (!simple_dai || !simple_dai->tdm_width_map)
 415		return 0;
 416
 417	slot_width = simple_dai->slot_width;
 418	slot_count = simple_dai->slots;
 419
 420	if (slot_width == 0)
 421		slot_width = sample_bits;
 422
 423	for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
 424		if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
 425			slot_width = simple_dai->tdm_width_map[i].slot_width;
 426			slot_count = simple_dai->tdm_width_map[i].slot_count;
 427			break;
 428		}
 429	}
 430
 431	ret = snd_soc_dai_set_tdm_slot(dai,
 432				       simple_dai->tx_slot_mask,
 433				       simple_dai->rx_slot_mask,
 434				       slot_count,
 435				       slot_width);
 436	if (ret && ret != -ENOTSUPP) {
 437		dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
 438		return ret;
 439	}
 440
 441	return 0;
 442}
 443
 444int simple_util_hw_params(struct snd_pcm_substream *substream,
 445			  struct snd_pcm_hw_params *params)
 446{
 447	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 448	struct simple_util_dai *pdai;
 449	struct snd_soc_dai *sdai;
 450	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 451	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->id);
 452	unsigned int mclk, mclk_fs = 0;
 453	int i, ret;
 454
 455	if (props->mclk_fs)
 456		mclk_fs = props->mclk_fs;
 457
 458	if (mclk_fs) {
 459		struct snd_soc_component *component;
 460		mclk = params_rate(params) * mclk_fs;
 461
 462		for_each_prop_dai_codec(props, i, pdai) {
 463			ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
 464			if (ret < 0)
 465				return ret;
 466		}
 467
 468		for_each_prop_dai_cpu(props, i, pdai) {
 469			ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
 470			if (ret < 0)
 471				return ret;
 472		}
 473
 474		/* Ensure sysclk is set on all components in case any
 475		 * (such as platform components) are missed by calls to
 476		 * snd_soc_dai_set_sysclk.
 477		 */
 478		for_each_rtd_components(rtd, i, component) {
 479			ret = snd_soc_component_set_sysclk(component, 0, 0,
 480							   mclk, SND_SOC_CLOCK_IN);
 481			if (ret && ret != -ENOTSUPP)
 482				return ret;
 483		}
 484
 485		for_each_rtd_codec_dais(rtd, i, sdai) {
 486			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
 487			if (ret && ret != -ENOTSUPP)
 488				return ret;
 489		}
 490
 491		for_each_rtd_cpu_dais(rtd, i, sdai) {
 492			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
 493			if (ret && ret != -ENOTSUPP)
 494				return ret;
 495		}
 496	}
 497
 498	for_each_prop_dai_codec(props, i, pdai) {
 499		sdai = snd_soc_rtd_to_codec(rtd, i);
 500		ret = simple_set_tdm(sdai, pdai, params);
 501		if (ret < 0)
 502			return ret;
 503	}
 504
 505	for_each_prop_dai_cpu(props, i, pdai) {
 506		sdai = snd_soc_rtd_to_cpu(rtd, i);
 507		ret = simple_set_tdm(sdai, pdai, params);
 508		if (ret < 0)
 509			return ret;
 510	}
 511
 512	return 0;
 513}
 514EXPORT_SYMBOL_GPL(simple_util_hw_params);
 515
 516int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
 517				   struct snd_pcm_hw_params *params)
 518{
 519	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 520	struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->id);
 521	struct simple_util_data *data = &dai_props->adata;
 522	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 523	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 524
 525	if (data->convert_rate)
 526		rate->min =
 527		rate->max = data->convert_rate;
 528
 529	if (data->convert_channels)
 530		channels->min =
 531		channels->max = data->convert_channels;
 532
 533	if (data->convert_sample_format)
 534		simple_fixup_sample_fmt(data, params);
 535
 536	return 0;
 537}
 538EXPORT_SYMBOL_GPL(simple_util_be_hw_params_fixup);
 539
 540static int simple_init_dai(struct snd_soc_dai *dai, struct simple_util_dai *simple_dai)
 541{
 542	int ret;
 543
 544	if (!simple_dai)
 545		return 0;
 546
 547	if (simple_dai->sysclk) {
 548		ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
 549					     simple_dai->clk_direction);
 550		if (ret && ret != -ENOTSUPP) {
 551			dev_err(dai->dev, "simple-card: set_sysclk error\n");
 552			return ret;
 553		}
 554	}
 555
 556	if (simple_dai->slots) {
 557		ret = snd_soc_dai_set_tdm_slot(dai,
 558					       simple_dai->tx_slot_mask,
 559					       simple_dai->rx_slot_mask,
 560					       simple_dai->slots,
 561					       simple_dai->slot_width);
 562		if (ret && ret != -ENOTSUPP) {
 563			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
 564			return ret;
 565		}
 566	}
 567
 568	return 0;
 569}
 570
 571static inline int simple_component_is_codec(struct snd_soc_component *component)
 572{
 573	return component->driver->endianness;
 574}
 575
 576static int simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
 577				       struct simple_dai_props *dai_props)
 578{
 579	struct snd_soc_dai_link *dai_link = rtd->dai_link;
 580	struct snd_soc_component *component;
 581	struct snd_soc_pcm_stream *c2c_params;
 582	struct snd_pcm_hardware hw;
 583	int i, ret, stream;
 584
 585	/* Do nothing if it already has Codec2Codec settings */
 586	if (dai_link->c2c_params)
 587		return 0;
 588
 589	/* Do nothing if it was DPCM :: BE */
 590	if (dai_link->no_pcm)
 591		return 0;
 592
 593	/* Only Codecs */
 594	for_each_rtd_components(rtd, i, component) {
 595		if (!simple_component_is_codec(component))
 596			return 0;
 597	}
 598
 599	/* Assumes the capabilities are the same for all supported streams */
 600	for_each_pcm_streams(stream) {
 601		ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
 602		if (ret == 0)
 603			break;
 604	}
 605
 606	if (ret < 0) {
 607		dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
 608		return ret;
 609	}
 610
 611	c2c_params = devm_kzalloc(rtd->dev, sizeof(*c2c_params), GFP_KERNEL);
 612	if (!c2c_params)
 613		return -ENOMEM;
 614
 615	c2c_params->formats		= hw.formats;
 616	c2c_params->rates		= hw.rates;
 617	c2c_params->rate_min		= hw.rate_min;
 618	c2c_params->rate_max		= hw.rate_max;
 619	c2c_params->channels_min	= hw.channels_min;
 620	c2c_params->channels_max	= hw.channels_max;
 621
 622	dai_link->c2c_params		= c2c_params;
 623	dai_link->num_c2c_params	= 1;
 624
 625	return 0;
 626}
 627
 628int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd)
 629{
 630	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 631	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->id);
 632	struct simple_util_dai *dai;
 633	int i, ret;
 634
 635	for_each_prop_dai_codec(props, i, dai) {
 636		ret = simple_init_dai(snd_soc_rtd_to_codec(rtd, i), dai);
 637		if (ret < 0)
 638			return ret;
 639	}
 640	for_each_prop_dai_cpu(props, i, dai) {
 641		ret = simple_init_dai(snd_soc_rtd_to_cpu(rtd, i), dai);
 642		if (ret < 0)
 643			return ret;
 644	}
 645
 646	ret = simple_init_for_codec2codec(rtd, props);
 647	if (ret < 0)
 648		return ret;
 649
 650	return 0;
 651}
 652EXPORT_SYMBOL_GPL(simple_util_dai_init);
 653
 654void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
 655				       struct snd_soc_dai_link_component *cpus)
 656{
 657	/*
 658	 * Assumes Platform == CPU
 659	 *
 660	 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
 661	 * are different Component, but are sharing same component->dev.
 662	 *
 663	 * Let's assume Platform is same as CPU if it doesn't identify Platform on DT.
 664	 * see
 665	 *	simple-card.c :: simple_count_noml()
 666	 */
 667	if (!platforms->of_node)
 668		snd_soc_dlc_use_cpu_as_platform(platforms, cpus);
 669}
 670EXPORT_SYMBOL_GPL(simple_util_canonicalize_platform);
 671
 672void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
 673				  int is_single_links)
 674{
 675	/*
 676	 * In soc_bind_dai_link() will check cpu name after
 677	 * of_node matching if dai_link has cpu_dai_name.
 678	 * but, it will never match if name was created by
 679	 * fmt_single_name() remove cpu_dai_name if cpu_args
 680	 * was 0. See:
 681	 *	fmt_single_name()
 682	 *	fmt_multiple_name()
 683	 */
 684	if (is_single_links)
 685		cpus->dai_name = NULL;
 686}
 687EXPORT_SYMBOL_GPL(simple_util_canonicalize_cpu);
 688
 689void simple_util_clean_reference(struct snd_soc_card *card)
 690{
 691	struct snd_soc_dai_link *dai_link;
 692	struct snd_soc_dai_link_component *cpu;
 693	struct snd_soc_dai_link_component *codec;
 694	int i, j;
 695
 696	for_each_card_prelinks(card, i, dai_link) {
 697		for_each_link_cpus(dai_link, j, cpu)
 698			of_node_put(cpu->of_node);
 699		for_each_link_codecs(dai_link, j, codec)
 700			of_node_put(codec->of_node);
 701	}
 702}
 703EXPORT_SYMBOL_GPL(simple_util_clean_reference);
 704
 705int simple_util_parse_routing(struct snd_soc_card *card,
 706			      char *prefix)
 707{
 708	struct device_node *node = card->dev->of_node;
 709	char prop[128];
 710
 711	if (!prefix)
 712		prefix = "";
 713
 714	snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
 715
 716	if (!of_property_read_bool(node, prop))
 717		return 0;
 718
 719	return snd_soc_of_parse_audio_routing(card, prop);
 720}
 721EXPORT_SYMBOL_GPL(simple_util_parse_routing);
 722
 723int simple_util_parse_widgets(struct snd_soc_card *card,
 724			      char *prefix)
 725{
 726	struct device_node *node = card->dev->of_node;
 727	char prop[128];
 728
 729	if (!prefix)
 730		prefix = "";
 731
 732	snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
 733
 734	if (of_property_read_bool(node, prop))
 735		return snd_soc_of_parse_audio_simple_widgets(card, prop);
 736
 737	/* no widgets is not error */
 738	return 0;
 739}
 740EXPORT_SYMBOL_GPL(simple_util_parse_widgets);
 741
 742int simple_util_parse_pin_switches(struct snd_soc_card *card,
 743				   char *prefix)
 744{
 745	char prop[128];
 746
 747	if (!prefix)
 748		prefix = "";
 749
 750	snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
 751
 752	return snd_soc_of_parse_pin_switches(card, prop);
 753}
 754EXPORT_SYMBOL_GPL(simple_util_parse_pin_switches);
 755
 756int simple_util_init_jack(struct snd_soc_card *card,
 757			  struct simple_util_jack *sjack,
 758			  int is_hp, char *prefix,
 759			  char *pin)
 760{
 761	struct device *dev = card->dev;
 762	struct gpio_desc *desc;
 763	char prop[128];
 764	char *pin_name;
 765	char *gpio_name;
 766	int mask;
 767	int error;
 768
 769	if (!prefix)
 770		prefix = "";
 771
 
 
 772	if (is_hp) {
 773		snprintf(prop, sizeof(prop), "%shp-det", prefix);
 774		pin_name	= pin ? pin : "Headphones";
 775		gpio_name	= "Headphone detection";
 776		mask		= SND_JACK_HEADPHONE;
 777	} else {
 778		snprintf(prop, sizeof(prop), "%smic-det", prefix);
 779		pin_name	= pin ? pin : "Mic Jack";
 780		gpio_name	= "Mic detection";
 781		mask		= SND_JACK_MICROPHONE;
 782	}
 783
 784	desc = gpiod_get_optional(dev, prop, GPIOD_IN);
 785	error = PTR_ERR_OR_ZERO(desc);
 786	if (error)
 787		return error;
 788
 789	if (desc) {
 790		error = gpiod_set_consumer_name(desc, gpio_name);
 791		if (error)
 792			return error;
 793
 794		sjack->pin.pin		= pin_name;
 795		sjack->pin.mask		= mask;
 796
 797		sjack->gpio.name	= gpio_name;
 798		sjack->gpio.report	= mask;
 799		sjack->gpio.desc	= desc;
 800		sjack->gpio.debounce_time = 150;
 801
 802		snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
 803					   &sjack->pin, 1);
 804
 805		snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
 806	}
 807
 808	return 0;
 809}
 810EXPORT_SYMBOL_GPL(simple_util_init_jack);
 811
 812int simple_util_init_aux_jacks(struct simple_util_priv *priv, char *prefix)
 813{
 814	struct snd_soc_card *card = simple_priv_to_card(priv);
 815	struct snd_soc_component *component;
 816	int found_jack_index = 0;
 817	int type = 0;
 818	int num = 0;
 819	int ret;
 820
 821	if (priv->aux_jacks)
 822		return 0;
 823
 824	for_each_card_auxs(card, component) {
 825		type = snd_soc_component_get_jack_type(component);
 826		if (type > 0)
 827			num++;
 828	}
 829	if (num < 1)
 830		return 0;
 831
 832	priv->aux_jacks = devm_kcalloc(card->dev, num,
 833				       sizeof(struct snd_soc_jack), GFP_KERNEL);
 834	if (!priv->aux_jacks)
 835		return -ENOMEM;
 836
 837	for_each_card_auxs(card, component) {
 838		char id[128];
 839		struct snd_soc_jack *jack;
 840
 841		if (found_jack_index >= num)
 842			break;
 843
 844		type = snd_soc_component_get_jack_type(component);
 845		if (type <= 0)
 846			continue;
 847
 848		/* create jack */
 849		jack = &(priv->aux_jacks[found_jack_index++]);
 850		snprintf(id, sizeof(id), "%s-jack", component->name);
 851		ret = snd_soc_card_jack_new(card, id, type, jack);
 852		if (ret)
 853			continue;
 854
 855		(void)snd_soc_component_set_jack(component, jack, NULL);
 856	}
 857	return 0;
 858}
 859EXPORT_SYMBOL_GPL(simple_util_init_aux_jacks);
 860
 861static struct simple_util_dai dummy_util_dais = {
 862	.name = "dummy_util_dais",
 863};
 864
 865int simple_util_init_priv(struct simple_util_priv *priv,
 866			  struct link_info *li)
 867{
 868	struct snd_soc_card *card = simple_priv_to_card(priv);
 869	struct device *dev = simple_priv_to_dev(priv);
 870	struct snd_soc_dai_link *dai_link;
 871	struct simple_dai_props *dai_props;
 872	struct simple_util_dai *dais;
 873	struct snd_soc_dai_link_component *dlcs;
 874	struct snd_soc_codec_conf *cconf = NULL;
 875	int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
 876
 877	dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
 878	dai_link  = devm_kcalloc(dev, li->link, sizeof(*dai_link),  GFP_KERNEL);
 879	if (!dai_props || !dai_link)
 880		return -ENOMEM;
 881
 882	/*
 883	 * dais (= CPU+Codec)
 884	 * dlcs (= CPU+Codec+Platform)
 885	 */
 886	for (i = 0; i < li->link; i++) {
 887		int cc = li->num[i].cpus + li->num[i].codecs;
 888
 889		dai_num += cc;
 890		dlc_num += cc + li->num[i].platforms;
 891
 892		if (!li->num[i].cpus)
 893			cnf_num += li->num[i].codecs;
 894	}
 895
 896	dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
 897	dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
 898	if (!dais || !dlcs)
 899		return -ENOMEM;
 900
 901	if (cnf_num) {
 902		cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
 903		if (!cconf)
 904			return -ENOMEM;
 905	}
 906
 907	dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
 908		li->link, dai_num, cnf_num);
 909
 910	priv->dai_props		= dai_props;
 911	priv->dai_link		= dai_link;
 912	priv->dais		= dais;
 913	priv->dlcs		= dlcs;
 914	priv->codec_conf	= cconf;
 915
 916	card->dai_link		= priv->dai_link;
 917	card->num_links		= li->link;
 918	card->codec_conf	= cconf;
 919	card->num_configs	= cnf_num;
 920
 921	for (i = 0; i < li->link; i++) {
 922		if (li->num[i].cpus) {
 923			/* Normal CPU */
 924			dai_link[i].cpus	= dlcs;
 925			dai_props[i].num.cpus	=
 926			dai_link[i].num_cpus	= li->num[i].cpus;
 927			dai_props[i].cpu_dai	= dais;
 928
 929			dlcs += li->num[i].cpus;
 930			dais += li->num[i].cpus;
 931		} else {
 932			/* DPCM Be's CPU = dummy */
 933			dai_link[i].cpus	= &snd_soc_dummy_dlc;
 934			dai_props[i].num.cpus	=
 935			dai_link[i].num_cpus	= 1;
 936			dai_props[i].cpu_dai	= &dummy_util_dais;
 937		}
 938
 939		if (li->num[i].codecs) {
 940			/* Normal Codec */
 941			dai_link[i].codecs	= dlcs;
 942			dai_props[i].num.codecs	=
 943			dai_link[i].num_codecs	= li->num[i].codecs;
 944			dai_props[i].codec_dai	= dais;
 945
 946			dlcs += li->num[i].codecs;
 947			dais += li->num[i].codecs;
 948
 949			if (!li->num[i].cpus) {
 950				/* DPCM Be's Codec */
 951				dai_props[i].codec_conf = cconf;
 952				cconf += li->num[i].codecs;
 953			}
 954		} else {
 955			/* DPCM Fe's Codec = dummy */
 956			dai_link[i].codecs	= &snd_soc_dummy_dlc;
 957			dai_props[i].num.codecs	=
 958			dai_link[i].num_codecs	= 1;
 959			dai_props[i].codec_dai	= &dummy_util_dais;
 960		}
 961
 962		if (li->num[i].platforms) {
 963			/* Have Platform */
 964			dai_link[i].platforms		= dlcs;
 965			dai_props[i].num.platforms	=
 966			dai_link[i].num_platforms	= li->num[i].platforms;
 967
 968			dlcs += li->num[i].platforms;
 969		} else {
 970			/* Doesn't have Platform */
 971			dai_link[i].platforms		= NULL;
 972			dai_props[i].num.platforms	=
 973			dai_link[i].num_platforms	= 0;
 974		}
 975	}
 976
 977	return 0;
 978}
 979EXPORT_SYMBOL_GPL(simple_util_init_priv);
 980
 981void simple_util_remove(struct platform_device *pdev)
 982{
 983	struct snd_soc_card *card = platform_get_drvdata(pdev);
 984
 985	simple_util_clean_reference(card);
 986}
 987EXPORT_SYMBOL_GPL(simple_util_remove);
 988
 989int graph_util_card_probe(struct snd_soc_card *card)
 990{
 991	struct simple_util_priv *priv = snd_soc_card_get_drvdata(card);
 992	int ret;
 993
 994	ret = simple_util_init_hp(card, &priv->hp_jack, NULL);
 995	if (ret < 0)
 996		return ret;
 997
 998	ret = simple_util_init_mic(card, &priv->mic_jack, NULL);
 999	if (ret < 0)
1000		return ret;
1001
1002	return 0;
1003}
1004EXPORT_SYMBOL_GPL(graph_util_card_probe);
1005
1006int graph_util_is_ports0(struct device_node *np)
1007{
1008	struct device_node *port, *ports, *ports0, *top;
1009	int ret;
1010
1011	/* np is "endpoint" or "port" */
1012	if (of_node_name_eq(np, "endpoint")) {
1013		port = of_get_parent(np);
1014	} else {
1015		port = np;
1016		of_node_get(port);
1017	}
1018
1019	ports	= of_get_parent(port);
1020	top	= of_get_parent(ports);
1021	ports0	= of_get_child_by_name(top, "ports");
1022
1023	ret = ports0 == ports;
1024
1025	of_node_put(port);
1026	of_node_put(ports);
1027	of_node_put(ports0);
1028	of_node_put(top);
1029
1030	return ret;
1031}
1032EXPORT_SYMBOL_GPL(graph_util_is_ports0);
1033
1034static int graph_get_dai_id(struct device_node *ep)
1035{
1036	struct device_node *node;
1037	struct device_node *endpoint;
1038	struct of_endpoint info;
1039	int i, id;
1040	int ret;
1041
1042	/* use driver specified DAI ID if exist */
1043	ret = snd_soc_get_dai_id(ep);
1044	if (ret != -ENOTSUPP)
1045		return ret;
1046
1047	/* use endpoint/port reg if exist */
1048	ret = of_graph_parse_endpoint(ep, &info);
1049	if (ret == 0) {
1050		/*
1051		 * Because it will count port/endpoint if it doesn't have "reg".
1052		 * But, we can't judge whether it has "no reg", or "reg = <0>"
1053		 * only of_graph_parse_endpoint().
1054		 * We need to check "reg" property
1055		 */
1056		if (of_property_present(ep,   "reg"))
1057			return info.id;
1058
1059		node = of_get_parent(ep);
1060		ret = of_property_present(node, "reg");
1061		of_node_put(node);
1062		if (ret)
1063			return info.port;
1064	}
1065	node = of_graph_get_port_parent(ep);
1066
1067	/*
1068	 * Non HDMI sound case, counting port/endpoint on its DT
1069	 * is enough. Let's count it.
1070	 */
1071	i = 0;
1072	id = -1;
1073	for_each_endpoint_of_node(node, endpoint) {
1074		if (endpoint == ep)
1075			id = i;
1076		i++;
1077	}
1078
1079	of_node_put(node);
1080
1081	if (id < 0)
1082		return -ENODEV;
1083
1084	return id;
1085}
1086
1087int graph_util_parse_dai(struct device *dev, struct device_node *ep,
1088			 struct snd_soc_dai_link_component *dlc, int *is_single_link)
1089{
1090	struct device_node *node;
1091	struct of_phandle_args args = {};
1092	struct snd_soc_dai *dai;
1093	int ret;
1094
1095	if (!ep)
1096		return 0;
1097
1098	node = of_graph_get_port_parent(ep);
1099
1100	/*
1101	 * Try to find from DAI node
1102	 */
1103	args.np = ep;
1104	dai = snd_soc_get_dai_via_args(&args);
1105	if (dai) {
1106		dlc->dai_name = snd_soc_dai_name_get(dai);
1107		dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
1108		if (!dlc->dai_args)
1109			return -ENOMEM;
1110
1111		goto parse_dai_end;
1112	}
1113
1114	/* Get dai->name */
1115	args.np		= node;
1116	args.args[0]	= graph_get_dai_id(ep);
1117	args.args_count	= (of_graph_get_endpoint_count(node) > 1);
1118
1119	/*
1120	 * FIXME
1121	 *
1122	 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
1123	 * If user unbinded CPU or Codec driver, but not for Sound Card,
1124	 * dlc->dai_name is keeping unbinded CPU or Codec
1125	 * driver's pointer.
1126	 *
1127	 * If user re-bind CPU or Codec driver again, ALSA SoC will try
1128	 * to rebind Card via snd_soc_try_rebind_card(), but because of
1129	 * above reason, it might can't bind Sound Card.
1130	 * Because Sound Card is pointing to released dai_name pointer.
1131	 *
1132	 * To avoid this rebind Card issue,
1133	 * 1) It needs to alloc memory to keep dai_name eventhough
1134	 *    CPU or Codec driver was unbinded, or
1135	 * 2) user need to rebind Sound Card everytime
1136	 *    if he unbinded CPU or Codec.
1137	 */
1138	ret = snd_soc_get_dlc(&args, dlc);
1139	if (ret < 0) {
1140		of_node_put(node);
1141		return ret;
1142	}
1143
1144parse_dai_end:
1145	if (is_single_link)
1146		*is_single_link = of_graph_get_endpoint_count(node) == 1;
1147
1148	return 0;
1149}
1150EXPORT_SYMBOL_GPL(graph_util_parse_dai);
1151
1152void graph_util_parse_link_direction(struct device_node *np,
1153				    bool *playback_only, bool *capture_only)
1154{
1155	bool is_playback_only = of_property_read_bool(np, "playback-only");
1156	bool is_capture_only  = of_property_read_bool(np, "capture-only");
1157
1158	if (is_playback_only)
1159		*playback_only = is_playback_only;
1160	if (is_capture_only)
1161		*capture_only = is_capture_only;
1162}
1163EXPORT_SYMBOL_GPL(graph_util_parse_link_direction);
1164
1165static enum snd_soc_trigger_order
1166__graph_util_parse_trigger_order(struct simple_util_priv *priv,
1167				 struct device_node *np,
1168				 const char *prop)
1169{
1170	u32 val[SND_SOC_TRIGGER_SIZE];
1171	int ret;
1172
1173	ret = of_property_read_u32_array(np, prop, val, SND_SOC_TRIGGER_SIZE);
1174	if (ret == 0) {
1175		struct device *dev = simple_priv_to_dev(priv);
1176		u32 order =	(val[0] << 8) +
1177			(val[1] << 4) +
1178			(val[2]);
1179
1180		switch (order) {
1181		case	(SND_SOC_TRIGGER_LINK		<< 8) +
1182			(SND_SOC_TRIGGER_COMPONENT	<< 4) +
1183			(SND_SOC_TRIGGER_DAI):
1184			return SND_SOC_TRIGGER_ORDER_DEFAULT;
1185
1186		case	(SND_SOC_TRIGGER_LINK		<< 8) +
1187			(SND_SOC_TRIGGER_DAI		<< 4) +
1188			(SND_SOC_TRIGGER_COMPONENT):
1189			return SND_SOC_TRIGGER_ORDER_LDC;
1190
1191		default:
1192			dev_err(dev, "unsupported trigger order [0x%x]\n", order);
1193		}
1194	}
1195
1196	/* SND_SOC_TRIGGER_ORDER_MAX means error */
1197	return SND_SOC_TRIGGER_ORDER_MAX;
1198}
1199
1200void graph_util_parse_trigger_order(struct simple_util_priv *priv,
1201				    struct device_node *np,
1202				    enum snd_soc_trigger_order *trigger_start,
1203				    enum snd_soc_trigger_order *trigger_stop)
1204{
1205	static enum snd_soc_trigger_order order;
1206
1207	/*
1208	 * We can use it like below
1209	 *
1210	 * #include <dt-bindings/sound/audio-graph.h>
1211	 *
1212	 * link-trigger-order = <SND_SOC_TRIGGER_LINK
1213	 *			 SND_SOC_TRIGGER_COMPONENT
1214	 *			 SND_SOC_TRIGGER_DAI>;
1215	 */
1216
1217	order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order");
1218	if (order < SND_SOC_TRIGGER_ORDER_MAX) {
1219		*trigger_start = order;
1220		*trigger_stop  = order;
1221	}
1222
1223	order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order-start");
1224	if (order < SND_SOC_TRIGGER_ORDER_MAX)
1225		*trigger_start = order;
1226
1227	order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order-stop");
1228	if (order < SND_SOC_TRIGGER_ORDER_MAX)
1229		*trigger_stop  = order;
1230
1231	return;
1232}
1233EXPORT_SYMBOL_GPL(graph_util_parse_trigger_order);
1234
1235/* Module information */
1236MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1237MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
1238MODULE_LICENSE("GPL v2");
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// simple-card-utils.c
   4//
   5// Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
   6
 
 
   7#include <linux/clk.h>
   8#include <linux/gpio/consumer.h>
   9#include <linux/module.h>
  10#include <linux/of.h>
  11#include <linux/of_graph.h>
  12#include <sound/jack.h>
  13#include <sound/pcm_params.h>
  14#include <sound/simple_card_utils.h>
  15
  16static void simple_fixup_sample_fmt(struct simple_util_data *data,
  17					 struct snd_pcm_hw_params *params)
  18{
  19	int i;
  20	struct snd_mask *mask = hw_param_mask(params,
  21					      SNDRV_PCM_HW_PARAM_FORMAT);
  22	struct {
  23		char *fmt;
  24		u32 val;
  25	} of_sample_fmt_table[] = {
  26		{ "s8",		SNDRV_PCM_FORMAT_S8},
  27		{ "s16_le",	SNDRV_PCM_FORMAT_S16_LE},
  28		{ "s24_le",	SNDRV_PCM_FORMAT_S24_LE},
  29		{ "s24_3le",	SNDRV_PCM_FORMAT_S24_3LE},
  30		{ "s32_le",	SNDRV_PCM_FORMAT_S32_LE},
  31	};
  32
  33	for (i = 0; i < ARRAY_SIZE(of_sample_fmt_table); i++) {
  34		if (!strcmp(data->convert_sample_format,
  35			    of_sample_fmt_table[i].fmt)) {
  36			snd_mask_none(mask);
  37			snd_mask_set(mask, of_sample_fmt_table[i].val);
  38			break;
  39		}
  40	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  41}
  42
  43void simple_util_parse_convert(struct device_node *np,
  44			       char *prefix,
  45			       struct simple_util_data *data)
  46{
  47	char prop[128];
  48
 
 
 
  49	if (!prefix)
  50		prefix = "";
  51
  52	/* sampling rate convert */
  53	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
  54	of_property_read_u32(np, prop, &data->convert_rate);
  55
  56	/* channels transfer */
  57	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
  58	of_property_read_u32(np, prop, &data->convert_channels);
  59
  60	/* convert sample format */
  61	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-sample-format");
  62	of_property_read_string(np, prop, &data->convert_sample_format);
  63}
  64EXPORT_SYMBOL_GPL(simple_util_parse_convert);
  65
  66/**
  67 * simple_util_is_convert_required() - Query if HW param conversion was requested
  68 * @data: Link data.
  69 *
  70 * Returns true if any HW param conversion was requested for this DAI link with
  71 * any "convert-xxx" properties.
  72 */
  73bool simple_util_is_convert_required(const struct simple_util_data *data)
  74{
  75	return data->convert_rate ||
  76	       data->convert_channels ||
  77	       data->convert_sample_format;
  78}
  79EXPORT_SYMBOL_GPL(simple_util_is_convert_required);
  80
  81int simple_util_parse_daifmt(struct device *dev,
  82			     struct device_node *node,
  83			     struct device_node *codec,
  84			     char *prefix,
  85			     unsigned int *retfmt)
  86{
  87	struct device_node *bitclkmaster = NULL;
  88	struct device_node *framemaster = NULL;
  89	unsigned int daifmt;
  90
  91	daifmt = snd_soc_daifmt_parse_format(node, prefix);
  92
  93	snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
  94	if (!bitclkmaster && !framemaster) {
  95		/*
  96		 * No dai-link level and master setting was not found from
  97		 * sound node level, revert back to legacy DT parsing and
  98		 * take the settings from codec node.
  99		 */
 100		dev_dbg(dev, "Revert to legacy daifmt parsing\n");
 101
 102		daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
 103	} else {
 104		daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
 105				((codec == bitclkmaster) << 4) | (codec == framemaster));
 106	}
 107
 108	of_node_put(bitclkmaster);
 109	of_node_put(framemaster);
 110
 111	*retfmt = daifmt;
 112
 113	return 0;
 114}
 115EXPORT_SYMBOL_GPL(simple_util_parse_daifmt);
 116
 117int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
 118				    struct simple_util_dai *dai)
 119{
 120	u32 *array_values, *p;
 121	int n, i, ret;
 
 122
 123	if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
 124		return 0;
 125
 126	n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
 127	if (n % 3) {
 128		dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
 129		return -EINVAL;
 130	}
 131
 132	dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
 133	if (!dai->tdm_width_map)
 134		return -ENOMEM;
 135
 136	array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
 
 137	if (!array_values)
 138		return -ENOMEM;
 139
 140	ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
 141	if (ret < 0) {
 142		dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
 143		goto out;
 144	}
 145
 146	p = array_values;
 147	for (i = 0; i < n / 3; ++i) {
 148		dai->tdm_width_map[i].sample_bits = *p++;
 149		dai->tdm_width_map[i].slot_width = *p++;
 150		dai->tdm_width_map[i].slot_count = *p++;
 151	}
 152
 153	dai->n_tdm_widths = i;
 154	ret = 0;
 155out:
 156	kfree(array_values);
 157
 158	return ret;
 159}
 160EXPORT_SYMBOL_GPL(simple_util_parse_tdm_width_map);
 161
 162int simple_util_set_dailink_name(struct device *dev,
 163				 struct snd_soc_dai_link *dai_link,
 164				 const char *fmt, ...)
 165{
 166	va_list ap;
 167	char *name = NULL;
 168	int ret = -ENOMEM;
 169
 170	va_start(ap, fmt);
 171	name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
 172	va_end(ap);
 173
 174	if (name) {
 175		ret = 0;
 176
 177		dai_link->name		= name;
 178		dai_link->stream_name	= name;
 179	}
 180
 181	return ret;
 182}
 183EXPORT_SYMBOL_GPL(simple_util_set_dailink_name);
 184
 185int simple_util_parse_card_name(struct snd_soc_card *card,
 186				char *prefix)
 187{
 188	int ret;
 189
 190	if (!prefix)
 191		prefix = "";
 192
 193	/* Parse the card name from DT */
 194	ret = snd_soc_of_parse_card_name(card, "label");
 195	if (ret < 0 || !card->name) {
 196		char prop[128];
 197
 198		snprintf(prop, sizeof(prop), "%sname", prefix);
 199		ret = snd_soc_of_parse_card_name(card, prop);
 200		if (ret < 0)
 201			return ret;
 202	}
 203
 204	if (!card->name && card->dai_link)
 205		card->name = card->dai_link->name;
 206
 207	return 0;
 208}
 209EXPORT_SYMBOL_GPL(simple_util_parse_card_name);
 210
 211static int simple_clk_enable(struct simple_util_dai *dai)
 212{
 213	if (dai)
 214		return clk_prepare_enable(dai->clk);
 215
 216	return 0;
 217}
 218
 219static void simple_clk_disable(struct simple_util_dai *dai)
 220{
 221	if (dai)
 222		clk_disable_unprepare(dai->clk);
 223}
 224
 225int simple_util_parse_clk(struct device *dev,
 226			  struct device_node *node,
 227			  struct simple_util_dai *simple_dai,
 228			  struct snd_soc_dai_link_component *dlc)
 229{
 230	struct clk *clk;
 231	u32 val;
 232
 233	/*
 234	 * Parse dai->sysclk come from "clocks = <&xxx>"
 235	 * (if system has common clock)
 236	 *  or "system-clock-frequency = <xxx>"
 237	 *  or device's module clock.
 238	 */
 239	clk = devm_get_clk_from_child(dev, node, NULL);
 240	simple_dai->clk_fixed = of_property_read_bool(
 241		node, "system-clock-fixed");
 242	if (!IS_ERR(clk)) {
 243		simple_dai->sysclk = clk_get_rate(clk);
 244
 245		simple_dai->clk = clk;
 246	} else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
 247		simple_dai->sysclk = val;
 248		simple_dai->clk_fixed = true;
 249	} else {
 250		clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
 251		if (!IS_ERR(clk))
 252			simple_dai->sysclk = clk_get_rate(clk);
 253	}
 254
 255	if (of_property_read_bool(node, "system-clock-direction-out"))
 256		simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
 257
 258	return 0;
 259}
 260EXPORT_SYMBOL_GPL(simple_util_parse_clk);
 261
 262static int simple_check_fixed_sysclk(struct device *dev,
 263					  struct simple_util_dai *dai,
 264					  unsigned int *fixed_sysclk)
 265{
 266	if (dai->clk_fixed) {
 267		if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
 268			dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
 269				*fixed_sysclk, dai->sysclk);
 270			return -EINVAL;
 271		}
 272		*fixed_sysclk = dai->sysclk;
 273	}
 274
 275	return 0;
 276}
 277
 278int simple_util_startup(struct snd_pcm_substream *substream)
 279{
 280	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 281	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 282	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
 283	struct simple_util_dai *dai;
 284	unsigned int fixed_sysclk = 0;
 285	int i1, i2, i;
 286	int ret;
 287
 288	for_each_prop_dai_cpu(props, i1, dai) {
 289		ret = simple_clk_enable(dai);
 290		if (ret)
 291			goto cpu_err;
 292		ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
 293		if (ret)
 294			goto cpu_err;
 295	}
 296
 297	for_each_prop_dai_codec(props, i2, dai) {
 298		ret = simple_clk_enable(dai);
 299		if (ret)
 300			goto codec_err;
 301		ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
 302		if (ret)
 303			goto codec_err;
 304	}
 305
 306	if (fixed_sysclk && props->mclk_fs) {
 307		unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
 308
 309		if (fixed_sysclk % props->mclk_fs) {
 310			dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
 311				fixed_sysclk, props->mclk_fs);
 312			ret = -EINVAL;
 313			goto codec_err;
 314		}
 315		ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
 316			fixed_rate, fixed_rate);
 317		if (ret < 0)
 318			goto codec_err;
 319	}
 320
 321	return 0;
 322
 323codec_err:
 324	for_each_prop_dai_codec(props, i, dai) {
 325		if (i >= i2)
 326			break;
 327		simple_clk_disable(dai);
 328	}
 329cpu_err:
 330	for_each_prop_dai_cpu(props, i, dai) {
 331		if (i >= i1)
 332			break;
 333		simple_clk_disable(dai);
 334	}
 335	return ret;
 336}
 337EXPORT_SYMBOL_GPL(simple_util_startup);
 338
 339void simple_util_shutdown(struct snd_pcm_substream *substream)
 340{
 341	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 342	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 343	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
 344	struct simple_util_dai *dai;
 345	int i;
 346
 347	for_each_prop_dai_cpu(props, i, dai) {
 348		struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, i);
 349
 350		if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
 351			snd_soc_dai_set_sysclk(cpu_dai,
 352					       0, 0, SND_SOC_CLOCK_OUT);
 353
 354		simple_clk_disable(dai);
 355	}
 356	for_each_prop_dai_codec(props, i, dai) {
 357		struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, i);
 358
 359		if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
 360			snd_soc_dai_set_sysclk(codec_dai,
 361					       0, 0, SND_SOC_CLOCK_IN);
 362
 363		simple_clk_disable(dai);
 364	}
 365}
 366EXPORT_SYMBOL_GPL(simple_util_shutdown);
 367
 368static int simple_set_clk_rate(struct device *dev,
 369				    struct simple_util_dai *simple_dai,
 370				    unsigned long rate)
 371{
 372	if (!simple_dai)
 373		return 0;
 374
 375	if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
 376		dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
 377		return -EINVAL;
 378	}
 379
 380	if (!simple_dai->clk)
 381		return 0;
 382
 383	if (clk_get_rate(simple_dai->clk) == rate)
 384		return 0;
 385
 386	return clk_set_rate(simple_dai->clk, rate);
 387}
 388
 389static int simple_set_tdm(struct snd_soc_dai *dai,
 390				struct simple_util_dai *simple_dai,
 391				struct snd_pcm_hw_params *params)
 392{
 393	int sample_bits = params_width(params);
 394	int slot_width, slot_count;
 395	int i, ret;
 396
 397	if (!simple_dai || !simple_dai->tdm_width_map)
 398		return 0;
 399
 400	slot_width = simple_dai->slot_width;
 401	slot_count = simple_dai->slots;
 402
 403	if (slot_width == 0)
 404		slot_width = sample_bits;
 405
 406	for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
 407		if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
 408			slot_width = simple_dai->tdm_width_map[i].slot_width;
 409			slot_count = simple_dai->tdm_width_map[i].slot_count;
 410			break;
 411		}
 412	}
 413
 414	ret = snd_soc_dai_set_tdm_slot(dai,
 415				       simple_dai->tx_slot_mask,
 416				       simple_dai->rx_slot_mask,
 417				       slot_count,
 418				       slot_width);
 419	if (ret && ret != -ENOTSUPP) {
 420		dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
 421		return ret;
 422	}
 423
 424	return 0;
 425}
 426
 427int simple_util_hw_params(struct snd_pcm_substream *substream,
 428			  struct snd_pcm_hw_params *params)
 429{
 430	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 431	struct simple_util_dai *pdai;
 432	struct snd_soc_dai *sdai;
 433	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 434	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
 435	unsigned int mclk, mclk_fs = 0;
 436	int i, ret;
 437
 438	if (props->mclk_fs)
 439		mclk_fs = props->mclk_fs;
 440
 441	if (mclk_fs) {
 442		struct snd_soc_component *component;
 443		mclk = params_rate(params) * mclk_fs;
 444
 445		for_each_prop_dai_codec(props, i, pdai) {
 446			ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
 447			if (ret < 0)
 448				return ret;
 449		}
 450
 451		for_each_prop_dai_cpu(props, i, pdai) {
 452			ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
 453			if (ret < 0)
 454				return ret;
 455		}
 456
 457		/* Ensure sysclk is set on all components in case any
 458		 * (such as platform components) are missed by calls to
 459		 * snd_soc_dai_set_sysclk.
 460		 */
 461		for_each_rtd_components(rtd, i, component) {
 462			ret = snd_soc_component_set_sysclk(component, 0, 0,
 463							   mclk, SND_SOC_CLOCK_IN);
 464			if (ret && ret != -ENOTSUPP)
 465				return ret;
 466		}
 467
 468		for_each_rtd_codec_dais(rtd, i, sdai) {
 469			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
 470			if (ret && ret != -ENOTSUPP)
 471				return ret;
 472		}
 473
 474		for_each_rtd_cpu_dais(rtd, i, sdai) {
 475			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
 476			if (ret && ret != -ENOTSUPP)
 477				return ret;
 478		}
 479	}
 480
 481	for_each_prop_dai_codec(props, i, pdai) {
 482		sdai = snd_soc_rtd_to_codec(rtd, i);
 483		ret = simple_set_tdm(sdai, pdai, params);
 484		if (ret < 0)
 485			return ret;
 486	}
 487
 488	for_each_prop_dai_cpu(props, i, pdai) {
 489		sdai = snd_soc_rtd_to_cpu(rtd, i);
 490		ret = simple_set_tdm(sdai, pdai, params);
 491		if (ret < 0)
 492			return ret;
 493	}
 494
 495	return 0;
 496}
 497EXPORT_SYMBOL_GPL(simple_util_hw_params);
 498
 499int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
 500				   struct snd_pcm_hw_params *params)
 501{
 502	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 503	struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
 504	struct simple_util_data *data = &dai_props->adata;
 505	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 506	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 507
 508	if (data->convert_rate)
 509		rate->min =
 510		rate->max = data->convert_rate;
 511
 512	if (data->convert_channels)
 513		channels->min =
 514		channels->max = data->convert_channels;
 515
 516	if (data->convert_sample_format)
 517		simple_fixup_sample_fmt(data, params);
 518
 519	return 0;
 520}
 521EXPORT_SYMBOL_GPL(simple_util_be_hw_params_fixup);
 522
 523static int simple_init_dai(struct snd_soc_dai *dai, struct simple_util_dai *simple_dai)
 524{
 525	int ret;
 526
 527	if (!simple_dai)
 528		return 0;
 529
 530	if (simple_dai->sysclk) {
 531		ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
 532					     simple_dai->clk_direction);
 533		if (ret && ret != -ENOTSUPP) {
 534			dev_err(dai->dev, "simple-card: set_sysclk error\n");
 535			return ret;
 536		}
 537	}
 538
 539	if (simple_dai->slots) {
 540		ret = snd_soc_dai_set_tdm_slot(dai,
 541					       simple_dai->tx_slot_mask,
 542					       simple_dai->rx_slot_mask,
 543					       simple_dai->slots,
 544					       simple_dai->slot_width);
 545		if (ret && ret != -ENOTSUPP) {
 546			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
 547			return ret;
 548		}
 549	}
 550
 551	return 0;
 552}
 553
 554static inline int simple_component_is_codec(struct snd_soc_component *component)
 555{
 556	return component->driver->endianness;
 557}
 558
 559static int simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
 560				       struct simple_dai_props *dai_props)
 561{
 562	struct snd_soc_dai_link *dai_link = rtd->dai_link;
 563	struct snd_soc_component *component;
 564	struct snd_soc_pcm_stream *c2c_params;
 565	struct snd_pcm_hardware hw;
 566	int i, ret, stream;
 567
 568	/* Do nothing if it already has Codec2Codec settings */
 569	if (dai_link->c2c_params)
 570		return 0;
 571
 572	/* Do nothing if it was DPCM :: BE */
 573	if (dai_link->no_pcm)
 574		return 0;
 575
 576	/* Only Codecs */
 577	for_each_rtd_components(rtd, i, component) {
 578		if (!simple_component_is_codec(component))
 579			return 0;
 580	}
 581
 582	/* Assumes the capabilities are the same for all supported streams */
 583	for_each_pcm_streams(stream) {
 584		ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
 585		if (ret == 0)
 586			break;
 587	}
 588
 589	if (ret < 0) {
 590		dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
 591		return ret;
 592	}
 593
 594	c2c_params = devm_kzalloc(rtd->dev, sizeof(*c2c_params), GFP_KERNEL);
 595	if (!c2c_params)
 596		return -ENOMEM;
 597
 598	c2c_params->formats		= hw.formats;
 599	c2c_params->rates		= hw.rates;
 600	c2c_params->rate_min		= hw.rate_min;
 601	c2c_params->rate_max		= hw.rate_max;
 602	c2c_params->channels_min	= hw.channels_min;
 603	c2c_params->channels_max	= hw.channels_max;
 604
 605	dai_link->c2c_params		= c2c_params;
 606	dai_link->num_c2c_params	= 1;
 607
 608	return 0;
 609}
 610
 611int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd)
 612{
 613	struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 614	struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
 615	struct simple_util_dai *dai;
 616	int i, ret;
 617
 618	for_each_prop_dai_codec(props, i, dai) {
 619		ret = simple_init_dai(snd_soc_rtd_to_codec(rtd, i), dai);
 620		if (ret < 0)
 621			return ret;
 622	}
 623	for_each_prop_dai_cpu(props, i, dai) {
 624		ret = simple_init_dai(snd_soc_rtd_to_cpu(rtd, i), dai);
 625		if (ret < 0)
 626			return ret;
 627	}
 628
 629	ret = simple_init_for_codec2codec(rtd, props);
 630	if (ret < 0)
 631		return ret;
 632
 633	return 0;
 634}
 635EXPORT_SYMBOL_GPL(simple_util_dai_init);
 636
 637void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
 638				       struct snd_soc_dai_link_component *cpus)
 639{
 640	/*
 641	 * Assumes Platform == CPU
 642	 *
 643	 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
 644	 * are different Component, but are sharing same component->dev.
 645	 *
 646	 * Let's assume Platform is same as CPU if it doesn't identify Platform on DT.
 647	 * see
 648	 *	simple-card.c :: simple_count_noml()
 649	 */
 650	if (!platforms->of_node)
 651		snd_soc_dlc_use_cpu_as_platform(platforms, cpus);
 652}
 653EXPORT_SYMBOL_GPL(simple_util_canonicalize_platform);
 654
 655void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
 656				  int is_single_links)
 657{
 658	/*
 659	 * In soc_bind_dai_link() will check cpu name after
 660	 * of_node matching if dai_link has cpu_dai_name.
 661	 * but, it will never match if name was created by
 662	 * fmt_single_name() remove cpu_dai_name if cpu_args
 663	 * was 0. See:
 664	 *	fmt_single_name()
 665	 *	fmt_multiple_name()
 666	 */
 667	if (is_single_links)
 668		cpus->dai_name = NULL;
 669}
 670EXPORT_SYMBOL_GPL(simple_util_canonicalize_cpu);
 671
 672void simple_util_clean_reference(struct snd_soc_card *card)
 673{
 674	struct snd_soc_dai_link *dai_link;
 675	struct snd_soc_dai_link_component *cpu;
 676	struct snd_soc_dai_link_component *codec;
 677	int i, j;
 678
 679	for_each_card_prelinks(card, i, dai_link) {
 680		for_each_link_cpus(dai_link, j, cpu)
 681			of_node_put(cpu->of_node);
 682		for_each_link_codecs(dai_link, j, codec)
 683			of_node_put(codec->of_node);
 684	}
 685}
 686EXPORT_SYMBOL_GPL(simple_util_clean_reference);
 687
 688int simple_util_parse_routing(struct snd_soc_card *card,
 689			      char *prefix)
 690{
 691	struct device_node *node = card->dev->of_node;
 692	char prop[128];
 693
 694	if (!prefix)
 695		prefix = "";
 696
 697	snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
 698
 699	if (!of_property_read_bool(node, prop))
 700		return 0;
 701
 702	return snd_soc_of_parse_audio_routing(card, prop);
 703}
 704EXPORT_SYMBOL_GPL(simple_util_parse_routing);
 705
 706int simple_util_parse_widgets(struct snd_soc_card *card,
 707			      char *prefix)
 708{
 709	struct device_node *node = card->dev->of_node;
 710	char prop[128];
 711
 712	if (!prefix)
 713		prefix = "";
 714
 715	snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
 716
 717	if (of_property_read_bool(node, prop))
 718		return snd_soc_of_parse_audio_simple_widgets(card, prop);
 719
 720	/* no widgets is not error */
 721	return 0;
 722}
 723EXPORT_SYMBOL_GPL(simple_util_parse_widgets);
 724
 725int simple_util_parse_pin_switches(struct snd_soc_card *card,
 726				   char *prefix)
 727{
 728	char prop[128];
 729
 730	if (!prefix)
 731		prefix = "";
 732
 733	snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
 734
 735	return snd_soc_of_parse_pin_switches(card, prop);
 736}
 737EXPORT_SYMBOL_GPL(simple_util_parse_pin_switches);
 738
 739int simple_util_init_jack(struct snd_soc_card *card,
 740			  struct simple_util_jack *sjack,
 741			  int is_hp, char *prefix,
 742			  char *pin)
 743{
 744	struct device *dev = card->dev;
 745	struct gpio_desc *desc;
 746	char prop[128];
 747	char *pin_name;
 748	char *gpio_name;
 749	int mask;
 750	int error;
 751
 752	if (!prefix)
 753		prefix = "";
 754
 755	sjack->gpio.gpio = -ENOENT;
 756
 757	if (is_hp) {
 758		snprintf(prop, sizeof(prop), "%shp-det", prefix);
 759		pin_name	= pin ? pin : "Headphones";
 760		gpio_name	= "Headphone detection";
 761		mask		= SND_JACK_HEADPHONE;
 762	} else {
 763		snprintf(prop, sizeof(prop), "%smic-det", prefix);
 764		pin_name	= pin ? pin : "Mic Jack";
 765		gpio_name	= "Mic detection";
 766		mask		= SND_JACK_MICROPHONE;
 767	}
 768
 769	desc = gpiod_get_optional(dev, prop, GPIOD_IN);
 770	error = PTR_ERR_OR_ZERO(desc);
 771	if (error)
 772		return error;
 773
 774	if (desc) {
 775		error = gpiod_set_consumer_name(desc, gpio_name);
 776		if (error)
 777			return error;
 778
 779		sjack->pin.pin		= pin_name;
 780		sjack->pin.mask		= mask;
 781
 782		sjack->gpio.name	= gpio_name;
 783		sjack->gpio.report	= mask;
 784		sjack->gpio.desc	= desc;
 785		sjack->gpio.debounce_time = 150;
 786
 787		snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
 788					   &sjack->pin, 1);
 789
 790		snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
 791	}
 792
 793	return 0;
 794}
 795EXPORT_SYMBOL_GPL(simple_util_init_jack);
 796
 797int simple_util_init_aux_jacks(struct simple_util_priv *priv, char *prefix)
 798{
 799	struct snd_soc_card *card = simple_priv_to_card(priv);
 800	struct snd_soc_component *component;
 801	int found_jack_index = 0;
 802	int type = 0;
 803	int num = 0;
 804	int ret;
 805
 806	if (priv->aux_jacks)
 807		return 0;
 808
 809	for_each_card_auxs(card, component) {
 810		type = snd_soc_component_get_jack_type(component);
 811		if (type > 0)
 812			num++;
 813	}
 814	if (num < 1)
 815		return 0;
 816
 817	priv->aux_jacks = devm_kcalloc(card->dev, num,
 818				       sizeof(struct snd_soc_jack), GFP_KERNEL);
 819	if (!priv->aux_jacks)
 820		return -ENOMEM;
 821
 822	for_each_card_auxs(card, component) {
 823		char id[128];
 824		struct snd_soc_jack *jack;
 825
 826		if (found_jack_index >= num)
 827			break;
 828
 829		type = snd_soc_component_get_jack_type(component);
 830		if (type <= 0)
 831			continue;
 832
 833		/* create jack */
 834		jack = &(priv->aux_jacks[found_jack_index++]);
 835		snprintf(id, sizeof(id), "%s-jack", component->name);
 836		ret = snd_soc_card_jack_new(card, id, type, jack);
 837		if (ret)
 838			continue;
 839
 840		(void)snd_soc_component_set_jack(component, jack, NULL);
 841	}
 842	return 0;
 843}
 844EXPORT_SYMBOL_GPL(simple_util_init_aux_jacks);
 845
 
 
 
 
 846int simple_util_init_priv(struct simple_util_priv *priv,
 847			  struct link_info *li)
 848{
 849	struct snd_soc_card *card = simple_priv_to_card(priv);
 850	struct device *dev = simple_priv_to_dev(priv);
 851	struct snd_soc_dai_link *dai_link;
 852	struct simple_dai_props *dai_props;
 853	struct simple_util_dai *dais;
 854	struct snd_soc_dai_link_component *dlcs;
 855	struct snd_soc_codec_conf *cconf = NULL;
 856	int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
 857
 858	dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
 859	dai_link  = devm_kcalloc(dev, li->link, sizeof(*dai_link),  GFP_KERNEL);
 860	if (!dai_props || !dai_link)
 861		return -ENOMEM;
 862
 863	/*
 864	 * dais (= CPU+Codec)
 865	 * dlcs (= CPU+Codec+Platform)
 866	 */
 867	for (i = 0; i < li->link; i++) {
 868		int cc = li->num[i].cpus + li->num[i].codecs;
 869
 870		dai_num += cc;
 871		dlc_num += cc + li->num[i].platforms;
 872
 873		if (!li->num[i].cpus)
 874			cnf_num += li->num[i].codecs;
 875	}
 876
 877	dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
 878	dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
 879	if (!dais || !dlcs)
 880		return -ENOMEM;
 881
 882	if (cnf_num) {
 883		cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
 884		if (!cconf)
 885			return -ENOMEM;
 886	}
 887
 888	dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
 889		li->link, dai_num, cnf_num);
 890
 891	priv->dai_props		= dai_props;
 892	priv->dai_link		= dai_link;
 893	priv->dais		= dais;
 894	priv->dlcs		= dlcs;
 895	priv->codec_conf	= cconf;
 896
 897	card->dai_link		= priv->dai_link;
 898	card->num_links		= li->link;
 899	card->codec_conf	= cconf;
 900	card->num_configs	= cnf_num;
 901
 902	for (i = 0; i < li->link; i++) {
 903		if (li->num[i].cpus) {
 904			/* Normal CPU */
 905			dai_link[i].cpus	= dlcs;
 906			dai_props[i].num.cpus	=
 907			dai_link[i].num_cpus	= li->num[i].cpus;
 908			dai_props[i].cpu_dai	= dais;
 909
 910			dlcs += li->num[i].cpus;
 911			dais += li->num[i].cpus;
 912		} else {
 913			/* DPCM Be's CPU = dummy */
 914			dai_link[i].cpus	= &snd_soc_dummy_dlc;
 915			dai_props[i].num.cpus	=
 916			dai_link[i].num_cpus	= 1;
 
 917		}
 918
 919		if (li->num[i].codecs) {
 920			/* Normal Codec */
 921			dai_link[i].codecs	= dlcs;
 922			dai_props[i].num.codecs	=
 923			dai_link[i].num_codecs	= li->num[i].codecs;
 924			dai_props[i].codec_dai	= dais;
 925
 926			dlcs += li->num[i].codecs;
 927			dais += li->num[i].codecs;
 928
 929			if (!li->num[i].cpus) {
 930				/* DPCM Be's Codec */
 931				dai_props[i].codec_conf = cconf;
 932				cconf += li->num[i].codecs;
 933			}
 934		} else {
 935			/* DPCM Fe's Codec = dummy */
 936			dai_link[i].codecs	= &snd_soc_dummy_dlc;
 937			dai_props[i].num.codecs	=
 938			dai_link[i].num_codecs	= 1;
 
 939		}
 940
 941		if (li->num[i].platforms) {
 942			/* Have Platform */
 943			dai_link[i].platforms		= dlcs;
 944			dai_props[i].num.platforms	=
 945			dai_link[i].num_platforms	= li->num[i].platforms;
 946
 947			dlcs += li->num[i].platforms;
 948		} else {
 949			/* Doesn't have Platform */
 950			dai_link[i].platforms		= NULL;
 951			dai_props[i].num.platforms	=
 952			dai_link[i].num_platforms	= 0;
 953		}
 954	}
 955
 956	return 0;
 957}
 958EXPORT_SYMBOL_GPL(simple_util_init_priv);
 959
 960void simple_util_remove(struct platform_device *pdev)
 961{
 962	struct snd_soc_card *card = platform_get_drvdata(pdev);
 963
 964	simple_util_clean_reference(card);
 965}
 966EXPORT_SYMBOL_GPL(simple_util_remove);
 967
 968int graph_util_card_probe(struct snd_soc_card *card)
 969{
 970	struct simple_util_priv *priv = snd_soc_card_get_drvdata(card);
 971	int ret;
 972
 973	ret = simple_util_init_hp(card, &priv->hp_jack, NULL);
 974	if (ret < 0)
 975		return ret;
 976
 977	ret = simple_util_init_mic(card, &priv->mic_jack, NULL);
 978	if (ret < 0)
 979		return ret;
 980
 981	return 0;
 982}
 983EXPORT_SYMBOL_GPL(graph_util_card_probe);
 984
 985int graph_util_is_ports0(struct device_node *np)
 986{
 987	struct device_node *port, *ports, *ports0, *top;
 988	int ret;
 989
 990	/* np is "endpoint" or "port" */
 991	if (of_node_name_eq(np, "endpoint")) {
 992		port = of_get_parent(np);
 993	} else {
 994		port = np;
 995		of_node_get(port);
 996	}
 997
 998	ports	= of_get_parent(port);
 999	top	= of_get_parent(ports);
1000	ports0	= of_get_child_by_name(top, "ports");
1001
1002	ret = ports0 == ports;
1003
1004	of_node_put(port);
1005	of_node_put(ports);
1006	of_node_put(ports0);
1007	of_node_put(top);
1008
1009	return ret;
1010}
1011EXPORT_SYMBOL_GPL(graph_util_is_ports0);
1012
1013static int graph_get_dai_id(struct device_node *ep)
1014{
1015	struct device_node *node;
1016	struct device_node *endpoint;
1017	struct of_endpoint info;
1018	int i, id;
1019	int ret;
1020
1021	/* use driver specified DAI ID if exist */
1022	ret = snd_soc_get_dai_id(ep);
1023	if (ret != -ENOTSUPP)
1024		return ret;
1025
1026	/* use endpoint/port reg if exist */
1027	ret = of_graph_parse_endpoint(ep, &info);
1028	if (ret == 0) {
1029		/*
1030		 * Because it will count port/endpoint if it doesn't have "reg".
1031		 * But, we can't judge whether it has "no reg", or "reg = <0>"
1032		 * only of_graph_parse_endpoint().
1033		 * We need to check "reg" property
1034		 */
1035		if (of_property_present(ep,   "reg"))
1036			return info.id;
1037
1038		node = of_get_parent(ep);
1039		ret = of_property_present(node, "reg");
1040		of_node_put(node);
1041		if (ret)
1042			return info.port;
1043	}
1044	node = of_graph_get_port_parent(ep);
1045
1046	/*
1047	 * Non HDMI sound case, counting port/endpoint on its DT
1048	 * is enough. Let's count it.
1049	 */
1050	i = 0;
1051	id = -1;
1052	for_each_endpoint_of_node(node, endpoint) {
1053		if (endpoint == ep)
1054			id = i;
1055		i++;
1056	}
1057
1058	of_node_put(node);
1059
1060	if (id < 0)
1061		return -ENODEV;
1062
1063	return id;
1064}
1065
1066int graph_util_parse_dai(struct device *dev, struct device_node *ep,
1067			 struct snd_soc_dai_link_component *dlc, int *is_single_link)
1068{
1069	struct device_node *node;
1070	struct of_phandle_args args = {};
1071	struct snd_soc_dai *dai;
1072	int ret;
1073
1074	if (!ep)
1075		return 0;
1076
1077	node = of_graph_get_port_parent(ep);
1078
1079	/*
1080	 * Try to find from DAI node
1081	 */
1082	args.np = ep;
1083	dai = snd_soc_get_dai_via_args(&args);
1084	if (dai) {
1085		dlc->dai_name = snd_soc_dai_name_get(dai);
1086		dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
1087		if (!dlc->dai_args)
1088			return -ENOMEM;
1089
1090		goto parse_dai_end;
1091	}
1092
1093	/* Get dai->name */
1094	args.np		= node;
1095	args.args[0]	= graph_get_dai_id(ep);
1096	args.args_count	= (of_graph_get_endpoint_count(node) > 1);
1097
1098	/*
1099	 * FIXME
1100	 *
1101	 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
1102	 * If user unbinded CPU or Codec driver, but not for Sound Card,
1103	 * dlc->dai_name is keeping unbinded CPU or Codec
1104	 * driver's pointer.
1105	 *
1106	 * If user re-bind CPU or Codec driver again, ALSA SoC will try
1107	 * to rebind Card via snd_soc_try_rebind_card(), but because of
1108	 * above reason, it might can't bind Sound Card.
1109	 * Because Sound Card is pointing to released dai_name pointer.
1110	 *
1111	 * To avoid this rebind Card issue,
1112	 * 1) It needs to alloc memory to keep dai_name eventhough
1113	 *    CPU or Codec driver was unbinded, or
1114	 * 2) user need to rebind Sound Card everytime
1115	 *    if he unbinded CPU or Codec.
1116	 */
1117	ret = snd_soc_get_dlc(&args, dlc);
1118	if (ret < 0) {
1119		of_node_put(node);
1120		return ret;
1121	}
1122
1123parse_dai_end:
1124	if (is_single_link)
1125		*is_single_link = of_graph_get_endpoint_count(node) == 1;
1126
1127	return 0;
1128}
1129EXPORT_SYMBOL_GPL(graph_util_parse_dai);
1130
1131int graph_util_parse_link_direction(struct device_node *np,
1132				    bool *playback_only, bool *capture_only)
1133{
1134	bool is_playback_only = false;
1135	bool is_capture_only = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1136
1137	is_playback_only = of_property_read_bool(np, "playback-only");
1138	is_capture_only = of_property_read_bool(np, "capture-only");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1139
1140	if (is_playback_only && is_capture_only)
1141		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1142
1143	*playback_only = is_playback_only;
1144	*capture_only = is_capture_only;
 
1145
1146	return 0;
1147}
1148EXPORT_SYMBOL_GPL(graph_util_parse_link_direction);
1149
1150/* Module information */
1151MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1152MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
1153MODULE_LICENSE("GPL v2");