1 /* 2 * ASoC simple sound card support 3 * 4 * Copyright (C) 2012 Renesas Solutions Corp. 5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 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. 10 */ 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 #include <sound/soc-dai.h> 22 #include <sound/soc.h> 23 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; 29 unsigned int mclk_fs; 30 } *dai_props; 31 unsigned int mclk_fs; 32 int gpio_hp_det; 33 int gpio_hp_det_invert; 34 int gpio_mic_det; 35 int gpio_mic_det_invert; 36 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */ 37 }; 38 39 #define simple_priv_to_dev(priv) ((priv)->snd_card.dev) 40 #define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i) 41 #define simple_priv_to_props(priv, i) ((priv)->dai_props + i) 42 43 static int asoc_simple_card_startup(struct snd_pcm_substream *substream) 44 { 45 struct snd_soc_pcm_runtime *rtd = substream->private_data; 46 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); 47 struct simple_dai_props *dai_props = 48 &priv->dai_props[rtd - rtd->card->rtd]; 49 int ret; 50 51 ret = clk_prepare_enable(dai_props->cpu_dai.clk); 52 if (ret) 53 return ret; 54 55 ret = clk_prepare_enable(dai_props->codec_dai.clk); 56 if (ret) 57 clk_disable_unprepare(dai_props->cpu_dai.clk); 58 59 return ret; 60 } 61 62 static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream) 63 { 64 struct snd_soc_pcm_runtime *rtd = substream->private_data; 65 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); 66 struct simple_dai_props *dai_props = 67 &priv->dai_props[rtd - rtd->card->rtd]; 68 69 clk_disable_unprepare(dai_props->cpu_dai.clk); 70 71 clk_disable_unprepare(dai_props->codec_dai.clk); 72 } 73 74 static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream, 75 struct snd_pcm_hw_params *params) 76 { 77 struct snd_soc_pcm_runtime *rtd = substream->private_data; 78 struct snd_soc_dai *codec_dai = rtd->codec_dai; 79 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 80 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); 81 struct simple_dai_props *dai_props = 82 &priv->dai_props[rtd - rtd->card->rtd]; 83 unsigned int mclk, mclk_fs = 0; 84 int ret = 0; 85 86 if (priv->mclk_fs) 87 mclk_fs = priv->mclk_fs; 88 else if (dai_props->mclk_fs) 89 mclk_fs = dai_props->mclk_fs; 90 91 if (mclk_fs) { 92 mclk = params_rate(params) * mclk_fs; 93 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 94 SND_SOC_CLOCK_IN); 95 if (ret && ret != -ENOTSUPP) 96 goto err; 97 98 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 99 SND_SOC_CLOCK_OUT); 100 if (ret && ret != -ENOTSUPP) 101 goto err; 102 } 103 104 err: 105 return ret; 106 } 107 108 static struct snd_soc_ops asoc_simple_card_ops = { 109 .startup = asoc_simple_card_startup, 110 .shutdown = asoc_simple_card_shutdown, 111 .hw_params = asoc_simple_card_hw_params, 112 }; 113 114 static struct snd_soc_jack simple_card_hp_jack; 115 static struct snd_soc_jack_pin simple_card_hp_jack_pins[] = { 116 { 117 .pin = "Headphones", 118 .mask = SND_JACK_HEADPHONE, 119 }, 120 }; 121 static struct snd_soc_jack_gpio simple_card_hp_jack_gpio = { 122 .name = "Headphone detection", 123 .report = SND_JACK_HEADPHONE, 124 .debounce_time = 150, 125 }; 126 127 static struct snd_soc_jack simple_card_mic_jack; 128 static struct snd_soc_jack_pin simple_card_mic_jack_pins[] = { 129 { 130 .pin = "Mic Jack", 131 .mask = SND_JACK_MICROPHONE, 132 }, 133 }; 134 static struct snd_soc_jack_gpio simple_card_mic_jack_gpio = { 135 .name = "Mic detection", 136 .report = SND_JACK_MICROPHONE, 137 .debounce_time = 150, 138 }; 139 140 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, 141 struct asoc_simple_dai *set) 142 { 143 int ret; 144 145 if (set->sysclk) { 146 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0); 147 if (ret && ret != -ENOTSUPP) { 148 dev_err(dai->dev, "simple-card: set_sysclk error\n"); 149 goto err; 150 } 151 } 152 153 if (set->slots) { 154 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0, 155 set->slots, 156 set->slot_width); 157 if (ret && ret != -ENOTSUPP) { 158 dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); 159 goto err; 160 } 161 } 162 163 ret = 0; 164 165 err: 166 return ret; 167 } 168 169 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) 170 { 171 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); 172 struct snd_soc_dai *codec = rtd->codec_dai; 173 struct snd_soc_dai *cpu = rtd->cpu_dai; 174 struct simple_dai_props *dai_props; 175 int num, ret; 176 177 num = rtd - rtd->card->rtd; 178 dai_props = &priv->dai_props[num]; 179 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai); 180 if (ret < 0) 181 return ret; 182 183 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai); 184 if (ret < 0) 185 return ret; 186 187 if (gpio_is_valid(priv->gpio_hp_det)) { 188 snd_soc_card_jack_new(rtd->card, "Headphones", 189 SND_JACK_HEADPHONE, 190 &simple_card_hp_jack, 191 simple_card_hp_jack_pins, 192 ARRAY_SIZE(simple_card_hp_jack_pins)); 193 194 simple_card_hp_jack_gpio.gpio = priv->gpio_hp_det; 195 simple_card_hp_jack_gpio.invert = priv->gpio_hp_det_invert; 196 snd_soc_jack_add_gpios(&simple_card_hp_jack, 1, 197 &simple_card_hp_jack_gpio); 198 } 199 200 if (gpio_is_valid(priv->gpio_mic_det)) { 201 snd_soc_card_jack_new(rtd->card, "Mic Jack", 202 SND_JACK_MICROPHONE, 203 &simple_card_mic_jack, 204 simple_card_mic_jack_pins, 205 ARRAY_SIZE(simple_card_mic_jack_pins)); 206 simple_card_mic_jack_gpio.gpio = priv->gpio_mic_det; 207 simple_card_mic_jack_gpio.invert = priv->gpio_mic_det_invert; 208 snd_soc_jack_add_gpios(&simple_card_mic_jack, 1, 209 &simple_card_mic_jack_gpio); 210 } 211 return 0; 212 } 213 214 static int 215 asoc_simple_card_sub_parse_of(struct device_node *np, 216 struct asoc_simple_dai *dai, 217 struct device_node **p_node, 218 const char **name, 219 int *args_count) 220 { 221 struct of_phandle_args args; 222 struct clk *clk; 223 u32 val; 224 int ret; 225 226 /* 227 * Get node via "sound-dai = <&phandle port>" 228 * it will be used as xxx_of_node on soc_bind_dai_link() 229 */ 230 ret = of_parse_phandle_with_args(np, "sound-dai", 231 "#sound-dai-cells", 0, &args); 232 if (ret) 233 return ret; 234 235 *p_node = args.np; 236 237 if (args_count) 238 *args_count = args.args_count; 239 240 /* Get dai->name */ 241 ret = snd_soc_of_get_dai_name(np, name); 242 if (ret < 0) 243 return ret; 244 245 /* Parse TDM slot */ 246 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width); 247 if (ret) 248 return ret; 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 if (of_property_read_bool(np, "clocks")) { 257 clk = of_clk_get(np, 0); 258 if (IS_ERR(clk)) { 259 ret = PTR_ERR(clk); 260 return ret; 261 } 262 263 dai->sysclk = clk_get_rate(clk); 264 dai->clk = clk; 265 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) { 266 dai->sysclk = val; 267 } else { 268 clk = of_clk_get(args.np, 0); 269 if (!IS_ERR(clk)) 270 dai->sysclk = clk_get_rate(clk); 271 } 272 273 return 0; 274 } 275 276 static int asoc_simple_card_parse_daifmt(struct device_node *node, 277 struct simple_card_data *priv, 278 struct device_node *codec, 279 char *prefix, int idx) 280 { 281 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx); 282 struct device *dev = simple_priv_to_dev(priv); 283 struct device_node *bitclkmaster = NULL; 284 struct device_node *framemaster = NULL; 285 unsigned int daifmt; 286 287 daifmt = snd_soc_of_parse_daifmt(node, prefix, 288 &bitclkmaster, &framemaster); 289 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 290 291 if (strlen(prefix) && !bitclkmaster && !framemaster) { 292 /* 293 * No dai-link level and master setting was not found from 294 * sound node level, revert back to legacy DT parsing and 295 * take the settings from codec node. 296 */ 297 dev_dbg(dev, "Revert to legacy daifmt parsing\n"); 298 299 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) | 300 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK); 301 } else { 302 if (codec == bitclkmaster) 303 daifmt |= (codec == framemaster) ? 304 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS; 305 else 306 daifmt |= (codec == framemaster) ? 307 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS; 308 } 309 310 dai_link->dai_fmt = daifmt; 311 312 of_node_put(bitclkmaster); 313 of_node_put(framemaster); 314 315 return 0; 316 } 317 318 static int asoc_simple_card_dai_link_of(struct device_node *node, 319 struct simple_card_data *priv, 320 int idx, 321 bool is_top_level_node) 322 { 323 struct device *dev = simple_priv_to_dev(priv); 324 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx); 325 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx); 326 struct device_node *cpu = NULL; 327 struct device_node *plat = NULL; 328 struct device_node *codec = NULL; 329 char *name; 330 char prop[128]; 331 char *prefix = ""; 332 int ret, cpu_args; 333 u32 val; 334 335 /* For single DAI link & old style of DT node */ 336 if (is_top_level_node) 337 prefix = "simple-audio-card,"; 338 339 snprintf(prop, sizeof(prop), "%scpu", prefix); 340 cpu = of_get_child_by_name(node, prop); 341 342 snprintf(prop, sizeof(prop), "%splat", prefix); 343 plat = of_get_child_by_name(node, prop); 344 345 snprintf(prop, sizeof(prop), "%scodec", prefix); 346 codec = of_get_child_by_name(node, prop); 347 348 if (!cpu || !codec) { 349 ret = -EINVAL; 350 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); 351 goto dai_link_of_err; 352 } 353 354 ret = asoc_simple_card_parse_daifmt(node, priv, 355 codec, prefix, idx); 356 if (ret < 0) 357 goto dai_link_of_err; 358 359 if (!of_property_read_u32(node, "mclk-fs", &val)) 360 dai_props->mclk_fs = val; 361 362 ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai, 363 &dai_link->cpu_of_node, 364 &dai_link->cpu_dai_name, 365 &cpu_args); 366 if (ret < 0) 367 goto dai_link_of_err; 368 369 ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai, 370 &dai_link->codec_of_node, 371 &dai_link->codec_dai_name, NULL); 372 if (ret < 0) 373 goto dai_link_of_err; 374 375 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) { 376 ret = -EINVAL; 377 goto dai_link_of_err; 378 } 379 380 if (plat) { 381 struct of_phandle_args args; 382 383 ret = of_parse_phandle_with_args(plat, "sound-dai", 384 "#sound-dai-cells", 0, &args); 385 dai_link->platform_of_node = args.np; 386 } else { 387 /* Assumes platform == cpu */ 388 dai_link->platform_of_node = dai_link->cpu_of_node; 389 } 390 391 /* DAI link name is created from CPU/CODEC dai name */ 392 name = devm_kzalloc(dev, 393 strlen(dai_link->cpu_dai_name) + 394 strlen(dai_link->codec_dai_name) + 2, 395 GFP_KERNEL); 396 if (!name) { 397 ret = -ENOMEM; 398 goto dai_link_of_err; 399 } 400 401 sprintf(name, "%s-%s", dai_link->cpu_dai_name, 402 dai_link->codec_dai_name); 403 dai_link->name = dai_link->stream_name = name; 404 dai_link->ops = &asoc_simple_card_ops; 405 dai_link->init = asoc_simple_card_dai_init; 406 407 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name); 408 dev_dbg(dev, "\tformat : %04x\n", dai_link->dai_fmt); 409 dev_dbg(dev, "\tcpu : %s / %d\n", 410 dai_link->cpu_dai_name, 411 dai_props->cpu_dai.sysclk); 412 dev_dbg(dev, "\tcodec : %s / %d\n", 413 dai_link->codec_dai_name, 414 dai_props->codec_dai.sysclk); 415 416 /* 417 * In soc_bind_dai_link() will check cpu name after 418 * of_node matching if dai_link has cpu_dai_name. 419 * but, it will never match if name was created by 420 * fmt_single_name() remove cpu_dai_name if cpu_args 421 * was 0. See: 422 * fmt_single_name() 423 * fmt_multiple_name() 424 */ 425 if (!cpu_args) 426 dai_link->cpu_dai_name = NULL; 427 428 dai_link_of_err: 429 of_node_put(cpu); 430 of_node_put(codec); 431 432 return ret; 433 } 434 435 static int asoc_simple_card_parse_of(struct device_node *node, 436 struct simple_card_data *priv) 437 { 438 struct device *dev = simple_priv_to_dev(priv); 439 enum of_gpio_flags flags; 440 u32 val; 441 int ret; 442 443 if (!node) 444 return -EINVAL; 445 446 /* Parse the card name from DT */ 447 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name"); 448 449 /* The off-codec widgets */ 450 if (of_property_read_bool(node, "simple-audio-card,widgets")) { 451 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card, 452 "simple-audio-card,widgets"); 453 if (ret) 454 return ret; 455 } 456 457 /* DAPM routes */ 458 if (of_property_read_bool(node, "simple-audio-card,routing")) { 459 ret = snd_soc_of_parse_audio_routing(&priv->snd_card, 460 "simple-audio-card,routing"); 461 if (ret) 462 return ret; 463 } 464 465 /* Factor to mclk, used in hw_params() */ 466 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val); 467 if (ret == 0) 468 priv->mclk_fs = val; 469 470 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ? 471 priv->snd_card.name : ""); 472 473 /* Single/Muti DAI link(s) & New style of DT node */ 474 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) { 475 struct device_node *np = NULL; 476 int i = 0; 477 478 for_each_child_of_node(node, np) { 479 dev_dbg(dev, "\tlink %d:\n", i); 480 ret = asoc_simple_card_dai_link_of(np, priv, 481 i, false); 482 if (ret < 0) { 483 of_node_put(np); 484 return ret; 485 } 486 i++; 487 } 488 } else { 489 /* For single DAI link & old style of DT node */ 490 ret = asoc_simple_card_dai_link_of(node, priv, 0, true); 491 if (ret < 0) 492 return ret; 493 } 494 495 priv->gpio_hp_det = of_get_named_gpio_flags(node, 496 "simple-audio-card,hp-det-gpio", 0, &flags); 497 priv->gpio_hp_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW); 498 if (priv->gpio_hp_det == -EPROBE_DEFER) 499 return -EPROBE_DEFER; 500 501 priv->gpio_mic_det = of_get_named_gpio_flags(node, 502 "simple-audio-card,mic-det-gpio", 0, &flags); 503 priv->gpio_mic_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW); 504 if (priv->gpio_mic_det == -EPROBE_DEFER) 505 return -EPROBE_DEFER; 506 507 if (!priv->snd_card.name) 508 priv->snd_card.name = priv->snd_card.dai_link->name; 509 510 return 0; 511 } 512 513 /* Decrease the reference count of the device nodes */ 514 static int asoc_simple_card_unref(struct snd_soc_card *card) 515 { 516 struct snd_soc_dai_link *dai_link; 517 int num_links; 518 519 for (num_links = 0, dai_link = card->dai_link; 520 num_links < card->num_links; 521 num_links++, dai_link++) { 522 of_node_put(dai_link->cpu_of_node); 523 of_node_put(dai_link->codec_of_node); 524 } 525 return 0; 526 } 527 528 static int asoc_simple_card_probe(struct platform_device *pdev) 529 { 530 struct simple_card_data *priv; 531 struct snd_soc_dai_link *dai_link; 532 struct device_node *np = pdev->dev.of_node; 533 struct device *dev = &pdev->dev; 534 int num_links, ret; 535 536 /* Get the number of DAI links */ 537 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link")) 538 num_links = of_get_child_count(np); 539 else 540 num_links = 1; 541 542 /* Allocate the private data and the DAI link array */ 543 priv = devm_kzalloc(dev, 544 sizeof(*priv) + sizeof(*dai_link) * num_links, 545 GFP_KERNEL); 546 if (!priv) 547 return -ENOMEM; 548 549 /* Init snd_soc_card */ 550 priv->snd_card.owner = THIS_MODULE; 551 priv->snd_card.dev = dev; 552 dai_link = priv->dai_link; 553 priv->snd_card.dai_link = dai_link; 554 priv->snd_card.num_links = num_links; 555 556 priv->gpio_hp_det = -ENOENT; 557 priv->gpio_mic_det = -ENOENT; 558 559 /* Get room for the other properties */ 560 priv->dai_props = devm_kzalloc(dev, 561 sizeof(*priv->dai_props) * num_links, 562 GFP_KERNEL); 563 if (!priv->dai_props) 564 return -ENOMEM; 565 566 if (np && of_device_is_available(np)) { 567 568 ret = asoc_simple_card_parse_of(np, priv); 569 if (ret < 0) { 570 if (ret != -EPROBE_DEFER) 571 dev_err(dev, "parse error %d\n", ret); 572 goto err; 573 } 574 575 } else { 576 struct asoc_simple_card_info *cinfo; 577 578 cinfo = dev->platform_data; 579 if (!cinfo) { 580 dev_err(dev, "no info for asoc-simple-card\n"); 581 return -EINVAL; 582 } 583 584 if (!cinfo->name || 585 !cinfo->codec_dai.name || 586 !cinfo->codec || 587 !cinfo->platform || 588 !cinfo->cpu_dai.name) { 589 dev_err(dev, "insufficient asoc_simple_card_info settings\n"); 590 return -EINVAL; 591 } 592 593 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name; 594 dai_link->name = cinfo->name; 595 dai_link->stream_name = cinfo->name; 596 dai_link->platform_name = cinfo->platform; 597 dai_link->codec_name = cinfo->codec; 598 dai_link->cpu_dai_name = cinfo->cpu_dai.name; 599 dai_link->codec_dai_name = cinfo->codec_dai.name; 600 dai_link->dai_fmt = cinfo->daifmt; 601 dai_link->init = asoc_simple_card_dai_init; 602 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai, 603 sizeof(priv->dai_props->cpu_dai)); 604 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai, 605 sizeof(priv->dai_props->codec_dai)); 606 607 } 608 609 snd_soc_card_set_drvdata(&priv->snd_card, priv); 610 611 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card); 612 if (ret >= 0) 613 return ret; 614 615 err: 616 asoc_simple_card_unref(&priv->snd_card); 617 return ret; 618 } 619 620 static int asoc_simple_card_remove(struct platform_device *pdev) 621 { 622 struct snd_soc_card *card = platform_get_drvdata(pdev); 623 struct simple_card_data *priv = snd_soc_card_get_drvdata(card); 624 625 if (gpio_is_valid(priv->gpio_hp_det)) 626 snd_soc_jack_free_gpios(&simple_card_hp_jack, 1, 627 &simple_card_hp_jack_gpio); 628 if (gpio_is_valid(priv->gpio_mic_det)) 629 snd_soc_jack_free_gpios(&simple_card_mic_jack, 1, 630 &simple_card_mic_jack_gpio); 631 632 return asoc_simple_card_unref(card); 633 } 634 635 static const struct of_device_id asoc_simple_of_match[] = { 636 { .compatible = "simple-audio-card", }, 637 {}, 638 }; 639 MODULE_DEVICE_TABLE(of, asoc_simple_of_match); 640 641 static struct platform_driver asoc_simple_card = { 642 .driver = { 643 .name = "asoc-simple-card", 644 .of_match_table = asoc_simple_of_match, 645 }, 646 .probe = asoc_simple_card_probe, 647 .remove = asoc_simple_card_remove, 648 }; 649 650 module_platform_driver(asoc_simple_card); 651 652 MODULE_ALIAS("platform:asoc-simple-card"); 653 MODULE_LICENSE("GPL"); 654 MODULE_DESCRIPTION("ASoC Simple Sound Card"); 655 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 656