simple-card.c 15.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * ASoC simple sound card support
 *
 * Copyright (C) 2012 Renesas Solutions Corp.
 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
11
#include <linux/clk.h>
12
#include <linux/device.h>
13
#include <linux/gpio.h>
14
#include <linux/module.h>
15
#include <linux/of.h>
16
#include <linux/of_gpio.h>
17
#include <linux/platform_device.h>
18
#include <linux/string.h>
19
#include <sound/jack.h>
20
#include <sound/simple_card.h>
21 22
#include <sound/soc-dai.h>
#include <sound/soc.h>
23

24 25 26 27 28 29
struct asoc_simple_jack {
	struct snd_soc_jack jack;
	struct snd_soc_jack_pin pin;
	struct snd_soc_jack_gpio gpio;
};

30 31
struct simple_card_data {
	struct snd_soc_card snd_card;
32 33 34
	struct simple_dai_props {
		struct asoc_simple_dai cpu_dai;
		struct asoc_simple_dai codec_dai;
35
		unsigned int mclk_fs;
36
	} *dai_props;
37
	unsigned int mclk_fs;
38 39
	struct asoc_simple_jack hp_jack;
	struct asoc_simple_jack mic_jack;
40
	struct snd_soc_dai_link dai_link[];	/* dynamically allocated */
41 42
};

43
#define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
44 45
#define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
#define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
46

47 48
#define PREFIX	"simple-audio-card,"

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#define asoc_simple_card_init_hp(card, sjack, prefix)\
	asoc_simple_card_init_jack(card, sjack, 1, prefix)
#define asoc_simple_card_init_mic(card, sjack, prefix)\
	asoc_simple_card_init_jack(card, sjack, 0, prefix)
static 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;

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

static void asoc_simple_card_remove_jack(struct asoc_simple_jack *sjack)
{
	if (gpio_is_valid(sjack->gpio.gpio))
		snd_soc_jack_free_gpios(&sjack->jack, 1, &sjack->gpio);
}

110 111 112 113 114
static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct simple_card_data *priv =	snd_soc_card_get_drvdata(rtd->card);
	struct simple_dai_props *dai_props =
115
		&priv->dai_props[rtd->num];
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	int ret;

	ret = clk_prepare_enable(dai_props->cpu_dai.clk);
	if (ret)
		return ret;
	
	ret = clk_prepare_enable(dai_props->codec_dai.clk);
	if (ret)
		clk_disable_unprepare(dai_props->cpu_dai.clk);

	return ret;
}

static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct simple_card_data *priv =	snd_soc_card_get_drvdata(rtd->card);
	struct simple_dai_props *dai_props =
134
		&priv->dai_props[rtd->num];
135 136 137 138 139 140

	clk_disable_unprepare(dai_props->cpu_dai.clk);

	clk_disable_unprepare(dai_props->codec_dai.clk);
}

141 142 143 144 145
static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
				      struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
146
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
147
	struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
148
	struct simple_dai_props *dai_props = &priv->dai_props[rtd->num];
149
	unsigned int mclk, mclk_fs = 0;
150 151
	int ret = 0;

152 153 154 155 156 157 158
	if (priv->mclk_fs)
		mclk_fs = priv->mclk_fs;
	else if (dai_props->mclk_fs)
		mclk_fs = dai_props->mclk_fs;

	if (mclk_fs) {
		mclk = params_rate(params) * mclk_fs;
159 160
		ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
					     SND_SOC_CLOCK_IN);
161 162 163 164 165 166 167
		if (ret && ret != -ENOTSUPP)
			goto err;

		ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
					     SND_SOC_CLOCK_OUT);
		if (ret && ret != -ENOTSUPP)
			goto err;
168
	}
169
	return 0;
170
err:
171 172 173 174
	return ret;
}

static struct snd_soc_ops asoc_simple_card_ops = {
175 176
	.startup = asoc_simple_card_startup,
	.shutdown = asoc_simple_card_shutdown,
177 178 179
	.hw_params = asoc_simple_card_hw_params,
};

180
static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
181
				       struct asoc_simple_dai *set)
182
{
183
	int ret;
184

185
	if (set->sysclk) {
186
		ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
187 188 189 190 191 192
		if (ret && ret != -ENOTSUPP) {
			dev_err(dai->dev, "simple-card: set_sysclk error\n");
			goto err;
		}
	}

193
	if (set->slots) {
194 195 196
		ret = snd_soc_dai_set_tdm_slot(dai,
					       set->tx_slot_mask,
					       set->rx_slot_mask,
197 198 199 200 201 202 203 204
						set->slots,
						set->slot_width);
		if (ret && ret != -ENOTSUPP) {
			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
			goto err;
		}
	}

205
	ret = 0;
206

207
err:
208 209 210
	return ret;
}

211 212
static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
{
213
	struct simple_card_data *priv =	snd_soc_card_get_drvdata(rtd->card);
214 215
	struct snd_soc_dai *codec = rtd->codec_dai;
	struct snd_soc_dai *cpu = rtd->cpu_dai;
216
	struct simple_dai_props *dai_props;
217
	int ret;
218

219
	dai_props = &priv->dai_props[rtd->num];
220
	ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
221 222
	if (ret < 0)
		return ret;
223

224
	ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
225 226
	if (ret < 0)
		return ret;
227

228 229 230 231 232 233 234
	ret = asoc_simple_card_init_hp(rtd->card, &priv->hp_jack, PREFIX);
	if (ret < 0)
		return ret;

	ret = asoc_simple_card_init_mic(rtd->card, &priv->hp_jack, PREFIX);
	if (ret < 0)
		return ret;
235

236 237 238
	return 0;
}

239 240 241
static int
asoc_simple_card_sub_parse_of(struct device_node *np,
			      struct asoc_simple_dai *dai,
242
			      struct device_node **p_node,
243 244
			      const char **name,
			      int *args_count)
245
{
246
	struct of_phandle_args args;
247
	struct clk *clk;
248
	u32 val;
249 250
	int ret;

251 252 253
	if (!np)
		return 0;

254
	/*
255
	 * Get node via "sound-dai = <&phandle port>"
256 257
	 * it will be used as xxx_of_node on soc_bind_dai_link()
	 */
258 259 260 261 262 263 264 265 266
	ret = of_parse_phandle_with_args(np, "sound-dai",
					 "#sound-dai-cells", 0, &args);
	if (ret)
		return ret;

	*p_node = args.np;

	if (args_count)
		*args_count = args.args_count;
267

268
	/* Get dai->name */
269 270 271 272 273 274 275 276
	if (name) {
		ret = snd_soc_of_get_dai_name(np, name);
		if (ret < 0)
			return ret;
	}

	if (!dai)
		return 0;
277

278
	/* Parse TDM slot */
279 280 281
	ret = snd_soc_of_parse_tdm_slot(np, &dai->tx_slot_mask,
					&dai->rx_slot_mask,
					&dai->slots, &dai->slot_width);
282
	if (ret)
283
		return ret;
284

285
	/*
286 287
	 * Parse dai->sysclk come from "clocks = <&xxx>"
	 * (if system has common clock)
288
	 *  or "system-clock-frequency = <xxx>"
289
	 *  or device's module clock.
290
	 */
291 292 293 294
	if (of_property_read_bool(np, "clocks")) {
		clk = of_clk_get(np, 0);
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
295
			return ret;
296 297 298
		}

		dai->sysclk = clk_get_rate(clk);
299
		dai->clk = clk;
300 301
	} else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
		dai->sysclk = val;
302
	} else {
303
		clk = of_clk_get(args.np, 0);
304 305
		if (!IS_ERR(clk))
			dai->sysclk = clk_get_rate(clk);
306
	}
307

308
	return 0;
309 310
}

311
static int asoc_simple_card_dai_link_of(struct device_node *node,
312
					struct simple_card_data *priv,
313
					int idx,
314
					bool is_top_level_node)
315
{
316
	struct device *dev = simple_priv_to_dev(priv);
317 318
	struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
	struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
319
	struct device_node *cpu = NULL;
320
	struct device_node *plat = NULL;
321
	struct device_node *codec = NULL;
322 323 324
	char *name;
	char prop[128];
	char *prefix = "";
325
	int ret, cpu_args;
326
	u32 val;
327

328
	/* For single DAI link & old style of DT node */
329
	if (is_top_level_node)
330
		prefix = PREFIX;
331 332

	snprintf(prop, sizeof(prop), "%scpu", prefix);
333 334
	cpu = of_get_child_by_name(node, prop);

335 336 337
	snprintf(prop, sizeof(prop), "%splat", prefix);
	plat = of_get_child_by_name(node, prop);

338 339 340 341
	snprintf(prop, sizeof(prop), "%scodec", prefix);
	codec = of_get_child_by_name(node, prop);

	if (!cpu || !codec) {
342
		ret = -EINVAL;
343
		dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
344
		goto dai_link_of_err;
345
	}
346

347 348
	ret = asoc_simple_card_parse_daifmt(dev, node, codec,
					    prefix, &dai_link->dai_fmt);
349 350 351
	if (ret < 0)
		goto dai_link_of_err;

352 353 354
	if (!of_property_read_u32(node, "mclk-fs", &val))
		dai_props->mclk_fs = val;

355
	ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai,
356
					    &dai_link->cpu_of_node,
357 358
					    &dai_link->cpu_dai_name,
					    &cpu_args);
359
	if (ret < 0)
360 361
		goto dai_link_of_err;

362
	ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai,
363
					    &dai_link->codec_of_node,
364
					    &dai_link->codec_dai_name, NULL);
365 366 367
	if (ret < 0)
		goto dai_link_of_err;

368 369 370 371 372 373
	ret = asoc_simple_card_sub_parse_of(plat, NULL,
					    &dai_link->platform_of_node,
					    NULL, NULL);
	if (ret < 0)
		goto dai_link_of_err;

374
	if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
375 376
		ret = -EINVAL;
		goto dai_link_of_err;
377 378
	}

379 380
	/* Assumes platform == cpu */
	if (!dai_link->platform_of_node)
381
		dai_link->platform_of_node = dai_link->cpu_of_node;
382

383
	/* DAI link name is created from CPU/CODEC dai name */
384 385 386 387
	name = devm_kzalloc(dev,
			    strlen(dai_link->cpu_dai_name)   +
			    strlen(dai_link->codec_dai_name) + 2,
			    GFP_KERNEL);
388 389 390 391 392
	if (!name) {
		ret = -ENOMEM;
		goto dai_link_of_err;
	}

393 394 395
	sprintf(name, "%s-%s", dai_link->cpu_dai_name,
				dai_link->codec_dai_name);
	dai_link->name = dai_link->stream_name = name;
396
	dai_link->ops = &asoc_simple_card_ops;
397
	dai_link->init = asoc_simple_card_dai_init;
398 399

	dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
400 401
	dev_dbg(dev, "\tformat : %04x\n", dai_link->dai_fmt);
	dev_dbg(dev, "\tcpu : %s / %d\n",
402 403
		dai_link->cpu_dai_name,
		dai_props->cpu_dai.sysclk);
404
	dev_dbg(dev, "\tcodec : %s / %d\n",
405 406 407
		dai_link->codec_dai_name,
		dai_props->codec_dai.sysclk);

408
	/*
409 410 411 412 413
	 * 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:
414 415 416
	 *	fmt_single_name()
	 *	fmt_multiple_name()
	 */
417 418
	if (!cpu_args)
		dai_link->cpu_dai_name = NULL;
419

420
dai_link_of_err:
421 422 423
	of_node_put(cpu);
	of_node_put(codec);

424 425 426
	return ret;
}

427
static int asoc_simple_card_parse_of(struct device_node *node,
428
				     struct simple_card_data *priv)
429
{
430
	struct device *dev = simple_priv_to_dev(priv);
431
	u32 val;
432
	int ret;
433

434 435 436
	if (!node)
		return -EINVAL;

437
	/* Parse the card name from DT */
438
	snd_soc_of_parse_card_name(&priv->snd_card, PREFIX "name");
439

440
	/* The off-codec widgets */
441
	if (of_property_read_bool(node, PREFIX "widgets")) {
442
		ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
443
					PREFIX "widgets");
444 445 446 447
		if (ret)
			return ret;
	}

448
	/* DAPM routes */
449
	if (of_property_read_bool(node, PREFIX "routing")) {
450
		ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
451
					PREFIX "routing");
452 453 454
		if (ret)
			return ret;
	}
455

456
	/* Factor to mclk, used in hw_params() */
457
	ret = of_property_read_u32(node, PREFIX "mclk-fs", &val);
458 459
	if (ret == 0)
		priv->mclk_fs = val;
460

461 462 463
	dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
		priv->snd_card.name : "");

464
	/* Single/Muti DAI link(s) & New style of DT node */
465
	if (of_get_child_by_name(node, PREFIX "dai-link")) {
466
		struct device_node *np = NULL;
467 468 469
		int i = 0;

		for_each_child_of_node(node, np) {
470
			dev_dbg(dev, "\tlink %d:\n", i);
471
			ret = asoc_simple_card_dai_link_of(np, priv,
472
							   i, false);
473 474 475 476
			if (ret < 0) {
				of_node_put(np);
				return ret;
			}
477
			i++;
478
		}
479
	} else {
480
		/* For single DAI link & old style of DT node */
481
		ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
482
		if (ret < 0)
483
			return ret;
484
	}
485

486
	if (!priv->snd_card.name)
487
		priv->snd_card.name = priv->snd_card.dai_link->name;
488

489 490 491
	return 0;
}

492
/* Decrease the reference count of the device nodes */
493
static int asoc_simple_card_unref(struct snd_soc_card *card)
494 495 496 497 498 499 500
{
	struct snd_soc_dai_link *dai_link;
	int num_links;

	for (num_links = 0, dai_link = card->dai_link;
	     num_links < card->num_links;
	     num_links++, dai_link++) {
501 502
		of_node_put(dai_link->cpu_of_node);
		of_node_put(dai_link->codec_of_node);
503 504 505 506
	}
	return 0;
}

507 508
static int asoc_simple_card_probe(struct platform_device *pdev)
{
509
	struct simple_card_data *priv;
510
	struct snd_soc_dai_link *dai_link;
511
	struct device_node *np = pdev->dev.of_node;
512
	struct device *dev = &pdev->dev;
513
	int num_links, ret;
514

515
	/* Get the number of DAI links */
516
	if (np && of_get_child_by_name(np, PREFIX "dai-link"))
517
		num_links = of_get_child_count(np);
518
	else
519
		num_links = 1;
520

521
	/* Allocate the private data and the DAI link array */
522
	priv = devm_kzalloc(dev,
523
			sizeof(*priv) + sizeof(*dai_link) * num_links,
524
			GFP_KERNEL);
525
	if (!priv)
526 527
		return -ENOMEM;

528
	/* Init snd_soc_card */
529 530
	priv->snd_card.owner = THIS_MODULE;
	priv->snd_card.dev = dev;
531
	dai_link = priv->dai_link;
532
	priv->snd_card.dai_link = dai_link;
533
	priv->snd_card.num_links = num_links;
534

535
	/* Get room for the other properties */
536
	priv->dai_props = devm_kzalloc(dev,
537
			sizeof(*priv->dai_props) * num_links,
538 539 540 541
			GFP_KERNEL);
	if (!priv->dai_props)
		return -ENOMEM;

542
	if (np && of_device_is_available(np)) {
543

544
		ret = asoc_simple_card_parse_of(np, priv);
545 546 547
		if (ret < 0) {
			if (ret != -EPROBE_DEFER)
				dev_err(dev, "parse error %d\n", ret);
548
			goto err;
549
		}
550

551
	} else {
552 553 554 555
		struct asoc_simple_card_info *cinfo;

		cinfo = dev->platform_data;
		if (!cinfo) {
556 557 558
			dev_err(dev, "no info for asoc-simple-card\n");
			return -EINVAL;
		}
559

560 561 562 563
		if (!cinfo->name ||
		    !cinfo->codec_dai.name ||
		    !cinfo->codec ||
		    !cinfo->platform ||
564 565 566 567
		    !cinfo->cpu_dai.name) {
			dev_err(dev, "insufficient asoc_simple_card_info settings\n");
			return -EINVAL;
		}
568

569
		priv->snd_card.name	= (cinfo->card) ? cinfo->card : cinfo->name;
570 571 572 573
		dai_link->name		= cinfo->name;
		dai_link->stream_name	= cinfo->name;
		dai_link->platform_name	= cinfo->platform;
		dai_link->codec_name	= cinfo->codec;
574 575
		dai_link->cpu_dai_name	= cinfo->cpu_dai.name;
		dai_link->codec_dai_name = cinfo->codec_dai.name;
576
		dai_link->dai_fmt	= cinfo->daifmt;
577
		dai_link->init		= asoc_simple_card_dai_init;
578 579 580 581
		memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
					sizeof(priv->dai_props->cpu_dai));
		memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
					sizeof(priv->dai_props->codec_dai));
582

583 584
	}

585
	snd_soc_card_set_drvdata(&priv->snd_card, priv);
586

587
	ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
588 589
	if (ret >= 0)
		return ret;
590 591

err:
592
	asoc_simple_card_unref(&priv->snd_card);
593
	return ret;
594 595
}

596 597
static int asoc_simple_card_remove(struct platform_device *pdev)
{
598 599 600
	struct snd_soc_card *card = platform_get_drvdata(pdev);
	struct simple_card_data *priv = snd_soc_card_get_drvdata(card);

601 602
	asoc_simple_card_remove_jack(&priv->hp_jack);
	asoc_simple_card_remove_jack(&priv->mic_jack);
603

604
	return asoc_simple_card_unref(card);
605 606
}

607 608 609 610 611 612
static const struct of_device_id asoc_simple_of_match[] = {
	{ .compatible = "simple-audio-card", },
	{},
};
MODULE_DEVICE_TABLE(of, asoc_simple_of_match);

613 614
static struct platform_driver asoc_simple_card = {
	.driver = {
615
		.name = "asoc-simple-card",
616
		.pm = &snd_soc_pm_ops,
617
		.of_match_table = asoc_simple_of_match,
618
	},
619
	.probe = asoc_simple_card_probe,
620
	.remove = asoc_simple_card_remove,
621 622 623 624
};

module_platform_driver(asoc_simple_card);

625
MODULE_ALIAS("platform:asoc-simple-card");
626 627 628
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ASoC Simple Sound Card");
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");