2 * ASoC simple sound card support
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * 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>
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 #include <sound/soc-dai.h>
22 #include <sound/soc.h>
24 struct simple_card_data {
25 struct snd_soc_card snd_card;
26 struct simple_dai_props {
27 struct asoc_simple_dai cpu_dai;
28 struct asoc_simple_dai codec_dai;
33 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
36 #define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
37 #define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
38 #define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
40 static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
41 struct snd_pcm_hw_params *params)
43 struct snd_soc_pcm_runtime *rtd = substream->private_data;
44 struct snd_soc_dai *codec_dai = rtd->codec_dai;
45 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
50 mclk = params_rate(params) * priv->mclk_fs;
51 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
58 static struct snd_soc_ops asoc_simple_card_ops = {
59 .hw_params = asoc_simple_card_hw_params,
62 static struct snd_soc_jack simple_card_hp_jack;
63 static struct snd_soc_jack_pin simple_card_hp_jack_pins[] = {
66 .mask = SND_JACK_HEADPHONE,
69 static struct snd_soc_jack_gpio simple_card_hp_jack_gpio = {
70 .name = "Headphone detection",
71 .report = SND_JACK_HEADPHONE,
75 static struct snd_soc_jack simple_card_mic_jack;
76 static struct snd_soc_jack_pin simple_card_mic_jack_pins[] = {
79 .mask = SND_JACK_MICROPHONE,
82 static struct snd_soc_jack_gpio simple_card_mic_jack_gpio = {
83 .name = "Mic detection",
84 .report = SND_JACK_MICROPHONE,
88 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
89 struct asoc_simple_dai *set)
94 ret = snd_soc_dai_set_fmt(dai, set->fmt);
95 if (ret && ret != -ENOTSUPP) {
96 dev_err(dai->dev, "simple-card: set_fmt error\n");
102 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
103 if (ret && ret != -ENOTSUPP) {
104 dev_err(dai->dev, "simple-card: set_sysclk error\n");
110 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
113 if (ret && ret != -ENOTSUPP) {
114 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
125 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
127 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
128 struct snd_soc_dai *codec = rtd->codec_dai;
129 struct snd_soc_dai *cpu = rtd->cpu_dai;
130 struct simple_dai_props *dai_props;
133 num = rtd - rtd->card->rtd;
134 dai_props = &priv->dai_props[num];
135 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
139 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
143 if (gpio_is_valid(priv->gpio_hp_det)) {
144 snd_soc_jack_new(codec->codec, "Headphones", SND_JACK_HEADPHONE,
145 &simple_card_hp_jack);
146 snd_soc_jack_add_pins(&simple_card_hp_jack,
147 ARRAY_SIZE(simple_card_hp_jack_pins),
148 simple_card_hp_jack_pins);
150 simple_card_hp_jack_gpio.gpio = priv->gpio_hp_det;
151 snd_soc_jack_add_gpios(&simple_card_hp_jack, 1,
152 &simple_card_hp_jack_gpio);
155 if (gpio_is_valid(priv->gpio_mic_det)) {
156 snd_soc_jack_new(codec->codec, "Mic Jack", SND_JACK_MICROPHONE,
157 &simple_card_mic_jack);
158 snd_soc_jack_add_pins(&simple_card_mic_jack,
159 ARRAY_SIZE(simple_card_mic_jack_pins),
160 simple_card_mic_jack_pins);
161 simple_card_mic_jack_gpio.gpio = priv->gpio_mic_det;
162 snd_soc_jack_add_gpios(&simple_card_mic_jack, 1,
163 &simple_card_mic_jack_gpio);
169 asoc_simple_card_sub_parse_of(struct device_node *np,
170 struct asoc_simple_dai *dai,
171 struct device_node **p_node,
175 struct of_phandle_args args;
181 * Get node via "sound-dai = <&phandle port>"
182 * it will be used as xxx_of_node on soc_bind_dai_link()
184 ret = of_parse_phandle_with_args(np, "sound-dai",
185 "#sound-dai-cells", 0, &args);
192 *args_count = args.args_count;
195 ret = snd_soc_of_get_dai_name(np, name);
200 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
205 * Parse dai->sysclk come from "clocks = <&xxx>"
206 * (if system has common clock)
207 * or "system-clock-frequency = <xxx>"
208 * or device's module clock.
210 if (of_property_read_bool(np, "clocks")) {
211 clk = of_clk_get(np, 0);
217 dai->sysclk = clk_get_rate(clk);
218 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
221 clk = of_clk_get(args.np, 0);
223 dai->sysclk = clk_get_rate(clk);
229 static int asoc_simple_card_dai_link_of(struct device_node *node,
230 struct simple_card_data *priv,
232 bool is_top_level_node)
234 struct device *dev = simple_priv_to_dev(priv);
235 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
236 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
237 struct device_node *np = NULL;
238 struct device_node *bitclkmaster = NULL;
239 struct device_node *framemaster = NULL;
246 /* For single DAI link & old style of DT node */
247 if (is_top_level_node)
248 prefix = "simple-audio-card,";
250 daifmt = snd_soc_of_parse_daifmt(node, prefix,
251 &bitclkmaster, &framemaster);
252 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
254 snprintf(prop, sizeof(prop), "%scpu", prefix);
255 np = of_get_child_by_name(node, prop);
258 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
259 goto dai_link_of_err;
262 ret = asoc_simple_card_sub_parse_of(np, &dai_props->cpu_dai,
263 &dai_link->cpu_of_node,
264 &dai_link->cpu_dai_name,
267 goto dai_link_of_err;
269 dai_props->cpu_dai.fmt = daifmt;
270 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
272 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
275 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
278 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
281 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
286 snprintf(prop, sizeof(prop), "%scodec", prefix);
287 np = of_get_child_by_name(node, prop);
290 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
291 goto dai_link_of_err;
294 ret = asoc_simple_card_sub_parse_of(np, &dai_props->codec_dai,
295 &dai_link->codec_of_node,
296 &dai_link->codec_dai_name, NULL);
298 goto dai_link_of_err;
300 if (strlen(prefix) && !bitclkmaster && !framemaster) {
302 * No DAI link level and master setting was found
303 * from sound node level, revert back to legacy DT
304 * parsing and take the settings from codec node.
306 dev_dbg(dev, "%s: Revert to legacy daifmt parsing\n",
308 dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt =
309 snd_soc_of_parse_daifmt(np, NULL, NULL, NULL) |
310 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
312 dai_props->codec_dai.fmt = daifmt;
313 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
315 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
318 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
321 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
324 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
329 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
331 goto dai_link_of_err;
334 /* Simple Card assumes platform == cpu */
335 dai_link->platform_of_node = dai_link->cpu_of_node;
337 /* DAI link name is created from CPU/CODEC dai name */
338 name = devm_kzalloc(dev,
339 strlen(dai_link->cpu_dai_name) +
340 strlen(dai_link->codec_dai_name) + 2,
342 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
343 dai_link->codec_dai_name);
344 dai_link->name = dai_link->stream_name = name;
345 dai_link->ops = &asoc_simple_card_ops;
346 dai_link->init = asoc_simple_card_dai_init;
348 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
349 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
350 dai_link->cpu_dai_name,
351 dai_props->cpu_dai.fmt,
352 dai_props->cpu_dai.sysclk);
353 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
354 dai_link->codec_dai_name,
355 dai_props->codec_dai.fmt,
356 dai_props->codec_dai.sysclk);
359 * In soc_bind_dai_link() will check cpu name after
360 * of_node matching if dai_link has cpu_dai_name.
361 * but, it will never match if name was created by
362 * fmt_single_name() remove cpu_dai_name if cpu_args
365 * fmt_multiple_name()
368 dai_link->cpu_dai_name = NULL;
374 of_node_put(bitclkmaster);
376 of_node_put(framemaster);
380 static int asoc_simple_card_parse_of(struct device_node *node,
381 struct simple_card_data *priv)
383 struct device *dev = simple_priv_to_dev(priv);
390 /* Parse the card name from DT */
391 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
393 /* The off-codec widgets */
394 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
395 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
396 "simple-audio-card,widgets");
402 if (of_property_read_bool(node, "simple-audio-card,routing")) {
403 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
404 "simple-audio-card,routing");
409 /* Factor to mclk, used in hw_params() */
410 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
414 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
415 priv->snd_card.name : "");
417 /* Single/Muti DAI link(s) & New style of DT node */
418 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) {
419 struct device_node *np = NULL;
422 for_each_child_of_node(node, np) {
423 dev_dbg(dev, "\tlink %d:\n", i);
424 ret = asoc_simple_card_dai_link_of(np, priv,
433 /* For single DAI link & old style of DT node */
434 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
439 priv->gpio_hp_det = of_get_named_gpio(node,
440 "simple-audio-card,hp-det-gpio", 0);
441 if (priv->gpio_hp_det == -EPROBE_DEFER)
442 return -EPROBE_DEFER;
444 priv->gpio_mic_det = of_get_named_gpio(node,
445 "simple-audio-card,mic-det-gpio", 0);
446 if (priv->gpio_mic_det == -EPROBE_DEFER)
447 return -EPROBE_DEFER;
449 if (!priv->snd_card.name)
450 priv->snd_card.name = priv->snd_card.dai_link->name;
455 /* Decrease the reference count of the device nodes */
456 static int asoc_simple_card_unref(struct platform_device *pdev)
458 struct snd_soc_card *card = platform_get_drvdata(pdev);
459 struct snd_soc_dai_link *dai_link;
460 struct device_node *np;
463 for (num_links = 0, dai_link = card->dai_link;
464 num_links < card->num_links;
465 num_links++, dai_link++) {
466 np = (struct device_node *) dai_link->cpu_of_node;
469 np = (struct device_node *) dai_link->codec_of_node;
476 static int asoc_simple_card_probe(struct platform_device *pdev)
478 struct simple_card_data *priv;
479 struct snd_soc_dai_link *dai_link;
480 struct device_node *np = pdev->dev.of_node;
481 struct device *dev = &pdev->dev;
484 /* Get the number of DAI links */
485 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link"))
486 num_links = of_get_child_count(np);
490 /* Allocate the private data and the DAI link array */
491 priv = devm_kzalloc(dev,
492 sizeof(*priv) + sizeof(*dai_link) * num_links,
497 /* Init snd_soc_card */
498 priv->snd_card.owner = THIS_MODULE;
499 priv->snd_card.dev = dev;
500 dai_link = priv->dai_link;
501 priv->snd_card.dai_link = dai_link;
502 priv->snd_card.num_links = num_links;
504 priv->gpio_hp_det = -ENOENT;
505 priv->gpio_mic_det = -ENOENT;
507 /* Get room for the other properties */
508 priv->dai_props = devm_kzalloc(dev,
509 sizeof(*priv->dai_props) * num_links,
511 if (!priv->dai_props)
514 if (np && of_device_is_available(np)) {
516 ret = asoc_simple_card_parse_of(np, priv);
518 if (ret != -EPROBE_DEFER)
519 dev_err(dev, "parse error %d\n", ret);
524 struct asoc_simple_card_info *cinfo;
526 cinfo = dev->platform_data;
528 dev_err(dev, "no info for asoc-simple-card\n");
533 !cinfo->codec_dai.name ||
536 !cinfo->cpu_dai.name) {
537 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
541 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
542 dai_link->name = cinfo->name;
543 dai_link->stream_name = cinfo->name;
544 dai_link->platform_name = cinfo->platform;
545 dai_link->codec_name = cinfo->codec;
546 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
547 dai_link->codec_dai_name = cinfo->codec_dai.name;
548 dai_link->init = asoc_simple_card_dai_init;
549 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
550 sizeof(priv->dai_props->cpu_dai));
551 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
552 sizeof(priv->dai_props->codec_dai));
554 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
555 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
558 snd_soc_card_set_drvdata(&priv->snd_card, priv);
560 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
565 asoc_simple_card_unref(pdev);
569 static int asoc_simple_card_remove(struct platform_device *pdev)
571 struct snd_soc_card *card = platform_get_drvdata(pdev);
572 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
574 if (gpio_is_valid(priv->gpio_hp_det))
575 snd_soc_jack_free_gpios(&simple_card_hp_jack, 1,
576 &simple_card_hp_jack_gpio);
577 if (gpio_is_valid(priv->gpio_mic_det))
578 snd_soc_jack_free_gpios(&simple_card_mic_jack, 1,
579 &simple_card_mic_jack_gpio);
581 return asoc_simple_card_unref(pdev);
584 static const struct of_device_id asoc_simple_of_match[] = {
585 { .compatible = "simple-audio-card", },
588 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
590 static struct platform_driver asoc_simple_card = {
592 .name = "asoc-simple-card",
593 .owner = THIS_MODULE,
594 .of_match_table = asoc_simple_of_match,
596 .probe = asoc_simple_card_probe,
597 .remove = asoc_simple_card_remove,
600 module_platform_driver(asoc_simple_card);
602 MODULE_ALIAS("platform:asoc-simple-card");
603 MODULE_LICENSE("GPL");
604 MODULE_DESCRIPTION("ASoC Simple Sound Card");
605 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");