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