simple-card-utils.c 12.6 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
//
// simple-card-utils.c
//
// Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>

7
#include <linux/clk.h>
8 9
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
10
#include <linux/module.h>
11
#include <linux/of.h>
12
#include <linux/of_gpio.h>
13
#include <linux/of_graph.h>
14
#include <sound/jack.h>
15 16
#include <sound/simple_card_utils.h>

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
void asoc_simple_card_convert_fixup(struct asoc_simple_card_data *data,
				    struct snd_pcm_hw_params *params)
{
	struct snd_interval *rate = hw_param_interval(params,
						SNDRV_PCM_HW_PARAM_RATE);
	struct snd_interval *channels = hw_param_interval(params,
						SNDRV_PCM_HW_PARAM_CHANNELS);

	if (data->convert_rate)
		rate->min =
		rate->max = data->convert_rate;

	if (data->convert_channels)
		channels->min =
		channels->max = data->convert_channels;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_convert_fixup);

35 36 37
void asoc_simple_card_parse_convert(struct device *dev,
				    struct device_node *np,
				    char *prefix,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
				    struct asoc_simple_card_data *data)
{
	char prop[128];

	if (!prefix)
		prefix = "";

	/* sampling rate convert */
	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
	of_property_read_u32(np, prop, &data->convert_rate);

	/* channels transfer */
	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
	of_property_read_u32(np, prop, &data->convert_channels);
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_convert);

55 56 57 58 59 60 61 62 63 64 65 66 67 68
int asoc_simple_card_parse_daifmt(struct device *dev,
				  struct device_node *node,
				  struct device_node *codec,
				  char *prefix,
				  unsigned int *retfmt)
{
	struct device_node *bitclkmaster = NULL;
	struct device_node *framemaster = NULL;
	unsigned int daifmt;

	daifmt = snd_soc_of_parse_daifmt(node, prefix,
					 &bitclkmaster, &framemaster);
	daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;

69
	if (!bitclkmaster && !framemaster) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
		/*
		 * No dai-link level and master setting was not found from
		 * sound node level, revert back to legacy DT parsing and
		 * take the settings from codec node.
		 */
		dev_dbg(dev, "Revert to legacy daifmt parsing\n");

		daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
			(daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
	} else {
		if (codec == bitclkmaster)
			daifmt |= (codec == framemaster) ?
				SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
		else
			daifmt |= (codec == framemaster) ?
				SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
	}

	of_node_put(bitclkmaster);
	of_node_put(framemaster);

	*retfmt = daifmt;

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

int asoc_simple_card_set_dailink_name(struct device *dev,
				      struct snd_soc_dai_link *dai_link,
				      const char *fmt, ...)
{
	va_list ap;
	char *name = NULL;
	int ret = -ENOMEM;

	va_start(ap, fmt);
	name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
	va_end(ap);

	if (name) {
		ret = 0;

		dai_link->name		= name;
		dai_link->stream_name	= name;
	}

	return ret;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
119 120 121 122 123 124

int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
				     char *prefix)
{
	int ret;

125 126
	if (!prefix)
		prefix = "";
127 128

	/* Parse the card name from DT */
129
	ret = snd_soc_of_parse_card_name(card, "label");
130
	if (ret < 0 || !card->name) {
131 132 133 134 135 136 137
		char prop[128];

		snprintf(prop, sizeof(prop), "%sname", prefix);
		ret = snd_soc_of_parse_card_name(card, prop);
		if (ret < 0)
			return ret;
	}
138 139 140 141 142 143 144

	if (!card->name && card->dai_link)
		card->name = card->dai_link->name;

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
145

146
static int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai)
147
{
148 149 150 151
	if (dai)
		return clk_prepare_enable(dai->clk);

	return 0;
152 153 154 155
}

void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai)
{
156 157
	if (dai)
		clk_disable_unprepare(dai->clk);
158
}
159
EXPORT_SYMBOL_GPL(asoc_simple_card_clk_disable);
160

161 162
int asoc_simple_card_parse_clk(struct device *dev,
			       struct device_node *node,
163
			       struct device_node *dai_of_node,
164
			       struct asoc_simple_dai *simple_dai,
165 166
			       const char *dai_name,
			       struct snd_soc_dai_link_component *dlc)
167 168 169 170
{
	struct clk *clk;
	u32 val;

171 172 173 174 175 176 177 178 179 180 181
	/*
	 * Use snd_soc_dai_link_component instead of legacy style.
	 * It is only for codec, but cpu will be supported in the future.
	 * see
	 *	soc-core.c :: snd_soc_init_multicodec()
	 */
	if (dlc) {
		dai_of_node	= dlc->of_node;
		dai_name	= dlc->dai_name;
	}

182 183 184 185 186 187
	/*
	 * Parse dai->sysclk come from "clocks = <&xxx>"
	 * (if system has common clock)
	 *  or "system-clock-frequency = <xxx>"
	 *  or device's module clock.
	 */
188
	clk = devm_get_clk_from_child(dev, node, NULL);
189 190
	if (!IS_ERR(clk)) {
		simple_dai->sysclk = clk_get_rate(clk);
191

192
		simple_dai->clk = clk;
193 194 195
	} else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
		simple_dai->sysclk = val;
	} else {
196
		clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
197 198 199 200
		if (!IS_ERR(clk))
			simple_dai->sysclk = clk_get_rate(clk);
	}

201 202 203
	if (of_property_read_bool(node, "system-clock-direction-out"))
		simple_dai->clk_direction = SND_SOC_CLOCK_OUT;

204 205 206 207
	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
int asoc_simple_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
	struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
	int ret;

	ret = asoc_simple_card_clk_enable(dai_props->cpu_dai);
	if (ret)
		return ret;

	ret = asoc_simple_card_clk_enable(dai_props->codec_dai);
	if (ret)
		asoc_simple_card_clk_disable(dai_props->cpu_dai);

	return ret;
}
EXPORT_SYMBOL_GPL(asoc_simple_startup);

227
int asoc_simple_card_parse_dai(struct device_node *node,
228
				    struct snd_soc_dai_link_component *dlc,
229 230 231 232 233 234 235 236 237 238 239 240
				    struct device_node **dai_of_node,
				    const char **dai_name,
				    const char *list_name,
				    const char *cells_name,
				    int *is_single_link)
{
	struct of_phandle_args args;
	int ret;

	if (!node)
		return 0;

241 242 243 244 245 246 247 248 249 250 251
	/*
	 * Use snd_soc_dai_link_component instead of legacy style.
	 * It is only for codec, but cpu will be supported in the future.
	 * see
	 *	soc-core.c :: snd_soc_init_multicodec()
	 */
	if (dlc) {
		dai_name	= &dlc->dai_name;
		dai_of_node	= &dlc->of_node;
	}

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	/*
	 * Get node via "sound-dai = <&phandle port>"
	 * it will be used as xxx_of_node on soc_bind_dai_link()
	 */
	ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
	if (ret)
		return ret;

	/* Get dai->name */
	if (dai_name) {
		ret = snd_soc_of_get_dai_name(node, dai_name);
		if (ret < 0)
			return ret;
	}

	*dai_of_node = args.np;

	if (is_single_link)
		*is_single_link = !args.args_count;

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);

276 277
static int asoc_simple_card_get_dai_id(struct device_node *ep)
{
278 279
	struct device_node *node;
	struct device_node *endpoint;
280
	struct of_endpoint info;
281
	int i, id;
282 283
	int ret;

284
	/* use driver specified DAI ID if exist */
285 286 287
	ret = snd_soc_get_dai_id(ep);
	if (ret != -ENOTSUPP)
		return ret;
288

289 290 291
	/* use endpoint/port reg if exist */
	ret = of_graph_parse_endpoint(ep, &info);
	if (ret == 0) {
292 293 294 295 296 297 298
		/*
		 * Because it will count port/endpoint if it doesn't have "reg".
		 * But, we can't judge whether it has "no reg", or "reg = <0>"
		 * only of_graph_parse_endpoint().
		 * We need to check "reg" property
		 */
		if (of_get_property(ep,   "reg", NULL))
299
			return info.id;
300 301 302 303

		node = of_get_parent(ep);
		of_node_put(node);
		if (of_get_property(node, "reg", NULL))
304 305 306 307
			return info.port;
	}
	node = of_graph_get_port_parent(ep);

308 309 310 311
	/*
	 * Non HDMI sound case, counting port/endpoint on its DT
	 * is enough. Let's count it.
	 */
312 313 314 315 316 317 318 319 320 321 322 323
	i = 0;
	id = -1;
	for_each_endpoint_of_node(node, endpoint) {
		if (endpoint == ep)
			id = i;
		i++;
	}

	of_node_put(node);

	if (id < 0)
		return -ENODEV;
324

325
	return id;
326 327 328
}

int asoc_simple_card_parse_graph_dai(struct device_node *ep,
329
				     struct snd_soc_dai_link_component *dlc,
330 331 332 333 334 335 336
				     struct device_node **dai_of_node,
				     const char **dai_name)
{
	struct device_node *node;
	struct of_phandle_args args;
	int ret;

337 338 339 340 341 342 343 344 345 346 347
	/*
	 * Use snd_soc_dai_link_component instead of legacy style.
	 * It is only for codec, but cpu will be supported in the future.
	 * see
	 *	soc-core.c :: snd_soc_init_multicodec()
	 */
	if (dlc) {
		dai_name	= &dlc->dai_name;
		dai_of_node	= &dlc->of_node;
	}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	if (!ep)
		return 0;
	if (!dai_name)
		return 0;

	node = of_graph_get_port_parent(ep);

	/* Get dai->name */
	args.np		= node;
	args.args[0]	= asoc_simple_card_get_dai_id(ep);
	args.args_count	= (of_graph_get_endpoint_count(node) > 1);

	ret = snd_soc_get_dai_name(&args, dai_name);
	if (ret < 0)
		return ret;

	*dai_of_node = node;

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);

370 371 372 373 374
int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
			      struct asoc_simple_dai *simple_dai)
{
	int ret;

375 376 377
	if (!simple_dai)
		return 0;

378
	if (simple_dai->sysclk) {
379 380
		ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
					     simple_dai->clk_direction);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		if (ret && ret != -ENOTSUPP) {
			dev_err(dai->dev, "simple-card: set_sysclk error\n");
			return ret;
		}
	}

	if (simple_dai->slots) {
		ret = snd_soc_dai_set_tdm_slot(dai,
					       simple_dai->tx_slot_mask,
					       simple_dai->rx_slot_mask,
					       simple_dai->slots,
					       simple_dai->slot_width);
		if (ret && ret != -ENOTSUPP) {
			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
			return ret;
		}
	}

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);

403
void asoc_simple_card_canonicalize_platform(struct snd_soc_dai_link *dai_link)
404 405
{
	/* Assumes platform == cpu */
406 407
	if (!dai_link->platforms->of_node)
		dai_link->platforms->of_node = dai_link->cpu_of_node;
408
}
409
EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_platform);
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
				       int is_single_links)
{
	/*
	 * In soc_bind_dai_link() will check cpu name after
	 * of_node matching if dai_link has cpu_dai_name.
	 * but, it will never match if name was created by
	 * fmt_single_name() remove cpu_dai_name if cpu_args
	 * was 0. See:
	 *	fmt_single_name()
	 *	fmt_multiple_name()
	 */
	if (is_single_links)
		dai_link->cpu_dai_name = NULL;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);

428 429 430
int asoc_simple_card_clean_reference(struct snd_soc_card *card)
{
	struct snd_soc_dai_link *dai_link;
431
	int i;
432

433
	for_each_card_prelinks(card, i, dai_link) {
434
		of_node_put(dai_link->cpu_of_node);
435
		of_node_put(dai_link->codecs->of_node);
436 437 438 439 440
	}
	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);

441
int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
442
				      char *prefix)
443 444 445 446 447 448 449 450 451
{
	struct device_node *node = card->dev->of_node;
	char prop[128];

	if (!prefix)
		prefix = "";

	snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");

452 453
	if (!of_property_read_bool(node, prop))
		return 0;
454 455 456 457 458

	return snd_soc_of_parse_audio_routing(card, prop);
}
EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
				      char *prefix)
{
	struct device_node *node = card->dev->of_node;
	char prop[128];

	if (!prefix)
		prefix = "";

	snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");

	if (of_property_read_bool(node, prop))
		return snd_soc_of_parse_audio_simple_widgets(card, prop);

	/* no widgets is not error */
	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
int asoc_simple_card_init_jack(struct snd_soc_card *card,
			       struct asoc_simple_jack *sjack,
			       int is_hp, char *prefix)
{
	struct device *dev = card->dev;
	enum of_gpio_flags flags;
	char prop[128];
	char *pin_name;
	char *gpio_name;
	int mask;
	int det;

	if (!prefix)
		prefix = "";

	sjack->gpio.gpio = -ENOENT;

	if (is_hp) {
		snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
		pin_name	= "Headphones";
		gpio_name	= "Headphone detection";
		mask		= SND_JACK_HEADPHONE;
	} else {
		snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
		pin_name	= "Mic Jack";
		gpio_name	= "Mic detection";
		mask		= SND_JACK_MICROPHONE;
	}

	det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
	if (det == -EPROBE_DEFER)
		return -EPROBE_DEFER;

	if (gpio_is_valid(det)) {
		sjack->pin.pin		= pin_name;
		sjack->pin.mask		= mask;

		sjack->gpio.name	= gpio_name;
		sjack->gpio.report	= mask;
		sjack->gpio.gpio	= det;
		sjack->gpio.invert	= !!(flags & OF_GPIO_ACTIVE_LOW);
		sjack->gpio.debounce_time = 150;

		snd_soc_card_jack_new(card, pin_name, mask,
				      &sjack->jack,
				      &sjack->pin, 1);

		snd_soc_jack_add_gpios(&sjack->jack, 1,
				       &sjack->gpio);
	}

	return 0;
}
EXPORT_SYMBOL_GPL(asoc_simple_card_init_jack);

533 534 535 536
/* Module information */
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
MODULE_LICENSE("GPL v2");