audio_topology.c (39828B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Greybus audio driver 4 * Copyright 2015-2016 Google Inc. 5 * Copyright 2015-2016 Linaro Ltd. 6 */ 7 8#include <linux/greybus.h> 9#include "audio_codec.h" 10 11#define GBAUDIO_INVALID_ID 0xFF 12 13/* mixer control */ 14struct gb_mixer_control { 15 int min, max; 16 unsigned int reg, rreg, shift, rshift, invert; 17}; 18 19struct gbaudio_ctl_pvt { 20 unsigned int ctl_id; 21 unsigned int data_cport; 22 unsigned int access; 23 unsigned int vcount; 24 struct gb_audio_ctl_elem_info *info; 25}; 26 27static struct gbaudio_module_info *find_gb_module( 28 struct gbaudio_codec_info *codec, 29 char const *name) 30{ 31 int dev_id; 32 char begin[NAME_SIZE]; 33 struct gbaudio_module_info *module; 34 35 if (!name) 36 return NULL; 37 38 if (sscanf(name, "%s %d", begin, &dev_id) != 2) 39 return NULL; 40 41 dev_dbg(codec->dev, "%s:Find module#%d\n", __func__, dev_id); 42 43 mutex_lock(&codec->lock); 44 list_for_each_entry(module, &codec->module_list, list) { 45 if (module->dev_id == dev_id) { 46 mutex_unlock(&codec->lock); 47 return module; 48 } 49 } 50 mutex_unlock(&codec->lock); 51 dev_warn(codec->dev, "%s: module#%d missing in codec list\n", name, 52 dev_id); 53 return NULL; 54} 55 56static const char *gbaudio_map_controlid(struct gbaudio_module_info *module, 57 __u8 control_id, __u8 index) 58{ 59 struct gbaudio_control *control; 60 61 if (control_id == GBAUDIO_INVALID_ID) 62 return NULL; 63 64 list_for_each_entry(control, &module->ctl_list, list) { 65 if (control->id == control_id) { 66 if (index == GBAUDIO_INVALID_ID) 67 return control->name; 68 if (index >= control->items) 69 return NULL; 70 return control->texts[index]; 71 } 72 } 73 list_for_each_entry(control, &module->widget_ctl_list, list) { 74 if (control->id == control_id) { 75 if (index == GBAUDIO_INVALID_ID) 76 return control->name; 77 if (index >= control->items) 78 return NULL; 79 return control->texts[index]; 80 } 81 } 82 return NULL; 83} 84 85static int gbaudio_map_controlname(struct gbaudio_module_info *module, 86 const char *name) 87{ 88 struct gbaudio_control *control; 89 90 list_for_each_entry(control, &module->ctl_list, list) { 91 if (!strncmp(control->name, name, NAME_SIZE)) 92 return control->id; 93 } 94 95 dev_warn(module->dev, "%s: missing in modules controls list\n", name); 96 97 return -EINVAL; 98} 99 100static int gbaudio_map_wcontrolname(struct gbaudio_module_info *module, 101 const char *name) 102{ 103 struct gbaudio_control *control; 104 105 list_for_each_entry(control, &module->widget_ctl_list, list) { 106 if (!strncmp(control->wname, name, NAME_SIZE)) 107 return control->id; 108 } 109 dev_warn(module->dev, "%s: missing in modules controls list\n", name); 110 111 return -EINVAL; 112} 113 114static int gbaudio_map_widgetname(struct gbaudio_module_info *module, 115 const char *name) 116{ 117 struct gbaudio_widget *widget; 118 119 list_for_each_entry(widget, &module->widget_list, list) { 120 if (!strncmp(widget->name, name, NAME_SIZE)) 121 return widget->id; 122 } 123 dev_warn(module->dev, "%s: missing in modules widgets list\n", name); 124 125 return -EINVAL; 126} 127 128static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module, 129 __u8 widget_id) 130{ 131 struct gbaudio_widget *widget; 132 133 list_for_each_entry(widget, &module->widget_list, list) { 134 if (widget->id == widget_id) 135 return widget->name; 136 } 137 return NULL; 138} 139 140static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb, 141 struct gb_audio_enumerated *gbenum) 142{ 143 const char **strings; 144 int i; 145 unsigned int items; 146 __u8 *data; 147 148 items = le32_to_cpu(gbenum->items); 149 strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL); 150 if (!strings) 151 return NULL; 152 153 data = gbenum->names; 154 155 for (i = 0; i < items; i++) { 156 strings[i] = (const char *)data; 157 while (*data != '\0') 158 data++; 159 data++; 160 } 161 162 return strings; 163} 164 165static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol, 166 struct snd_ctl_elem_info *uinfo) 167{ 168 unsigned int max; 169 const char *name; 170 struct gbaudio_ctl_pvt *data; 171 struct gb_audio_ctl_elem_info *info; 172 struct gbaudio_module_info *module; 173 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 174 struct gbaudio_codec_info *gbcodec = snd_soc_component_get_drvdata(comp); 175 176 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name); 177 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 178 info = (struct gb_audio_ctl_elem_info *)data->info; 179 180 if (!info) { 181 dev_err(comp->dev, "NULL info for %s\n", uinfo->id.name); 182 return -EINVAL; 183 } 184 185 /* update uinfo */ 186 uinfo->access = data->access; 187 uinfo->count = data->vcount; 188 uinfo->type = (__force snd_ctl_elem_type_t)info->type; 189 190 switch (info->type) { 191 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN: 192 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER: 193 uinfo->value.integer.min = le32_to_cpu(info->value.integer.min); 194 uinfo->value.integer.max = le32_to_cpu(info->value.integer.max); 195 break; 196 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED: 197 max = le32_to_cpu(info->value.enumerated.items); 198 uinfo->value.enumerated.items = max; 199 if (uinfo->value.enumerated.item > max - 1) 200 uinfo->value.enumerated.item = max - 1; 201 module = find_gb_module(gbcodec, kcontrol->id.name); 202 if (!module) 203 return -EINVAL; 204 name = gbaudio_map_controlid(module, data->ctl_id, 205 uinfo->value.enumerated.item); 206 strscpy(uinfo->value.enumerated.name, name, sizeof(uinfo->value.enumerated.name)); 207 break; 208 default: 209 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n", 210 info->type, kcontrol->id.name); 211 break; 212 } 213 return 0; 214} 215 216static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol, 217 struct snd_ctl_elem_value *ucontrol) 218{ 219 int ret; 220 struct gb_audio_ctl_elem_info *info; 221 struct gbaudio_ctl_pvt *data; 222 struct gb_audio_ctl_elem_value gbvalue; 223 struct gbaudio_module_info *module; 224 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 225 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp); 226 struct gb_bundle *bundle; 227 228 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name); 229 module = find_gb_module(gb, kcontrol->id.name); 230 if (!module) 231 return -EINVAL; 232 233 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 234 info = (struct gb_audio_ctl_elem_info *)data->info; 235 bundle = to_gb_bundle(module->dev); 236 237 ret = gb_pm_runtime_get_sync(bundle); 238 if (ret) 239 return ret; 240 241 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id, 242 GB_AUDIO_INVALID_INDEX, &gbvalue); 243 244 gb_pm_runtime_put_autosuspend(bundle); 245 246 if (ret) { 247 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret, 248 __func__, kcontrol->id.name); 249 return ret; 250 } 251 252 /* update ucontrol */ 253 switch (info->type) { 254 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN: 255 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER: 256 ucontrol->value.integer.value[0] = 257 le32_to_cpu(gbvalue.value.integer_value[0]); 258 if (data->vcount == 2) 259 ucontrol->value.integer.value[1] = 260 le32_to_cpu(gbvalue.value.integer_value[1]); 261 break; 262 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED: 263 ucontrol->value.enumerated.item[0] = 264 le32_to_cpu(gbvalue.value.enumerated_item[0]); 265 if (data->vcount == 2) 266 ucontrol->value.enumerated.item[1] = 267 le32_to_cpu(gbvalue.value.enumerated_item[1]); 268 break; 269 default: 270 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n", 271 info->type, kcontrol->id.name); 272 ret = -EINVAL; 273 break; 274 } 275 return ret; 276} 277 278static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol, 279 struct snd_ctl_elem_value *ucontrol) 280{ 281 int ret = 0; 282 struct gb_audio_ctl_elem_info *info; 283 struct gbaudio_ctl_pvt *data; 284 struct gb_audio_ctl_elem_value gbvalue; 285 struct gbaudio_module_info *module; 286 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 287 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp); 288 struct gb_bundle *bundle; 289 290 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name); 291 module = find_gb_module(gb, kcontrol->id.name); 292 if (!module) 293 return -EINVAL; 294 295 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 296 info = (struct gb_audio_ctl_elem_info *)data->info; 297 bundle = to_gb_bundle(module->dev); 298 299 /* update ucontrol */ 300 switch (info->type) { 301 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN: 302 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER: 303 gbvalue.value.integer_value[0] = 304 cpu_to_le32(ucontrol->value.integer.value[0]); 305 if (data->vcount == 2) 306 gbvalue.value.integer_value[1] = 307 cpu_to_le32(ucontrol->value.integer.value[1]); 308 break; 309 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED: 310 gbvalue.value.enumerated_item[0] = 311 cpu_to_le32(ucontrol->value.enumerated.item[0]); 312 if (data->vcount == 2) 313 gbvalue.value.enumerated_item[1] = 314 cpu_to_le32(ucontrol->value.enumerated.item[1]); 315 break; 316 default: 317 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n", 318 info->type, kcontrol->id.name); 319 ret = -EINVAL; 320 break; 321 } 322 323 if (ret) 324 return ret; 325 326 ret = gb_pm_runtime_get_sync(bundle); 327 if (ret) 328 return ret; 329 330 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id, 331 GB_AUDIO_INVALID_INDEX, &gbvalue); 332 333 gb_pm_runtime_put_autosuspend(bundle); 334 335 if (ret) { 336 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret, 337 __func__, kcontrol->id.name); 338 } 339 340 return ret; 341} 342 343#define SOC_MIXER_GB(xname, kcount, data) \ 344{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ 345 .count = kcount, .info = gbcodec_mixer_ctl_info, \ 346 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \ 347 .private_value = (unsigned long)data } 348 349/* 350 * although below callback functions seems redundant to above functions. 351 * same are kept to allow provision for different handling in case 352 * of DAPM related sequencing, etc. 353 */ 354static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol, 355 struct snd_ctl_elem_info *uinfo) 356{ 357 int platform_max, platform_min; 358 struct gbaudio_ctl_pvt *data; 359 struct gb_audio_ctl_elem_info *info; 360 361 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 362 info = (struct gb_audio_ctl_elem_info *)data->info; 363 364 /* update uinfo */ 365 platform_max = le32_to_cpu(info->value.integer.max); 366 platform_min = le32_to_cpu(info->value.integer.min); 367 368 if (platform_max == 1 && 369 !strnstr(kcontrol->id.name, " Volume", sizeof(kcontrol->id.name))) 370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 371 else 372 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 373 374 uinfo->count = data->vcount; 375 uinfo->value.integer.min = platform_min; 376 uinfo->value.integer.max = platform_max; 377 378 return 0; 379} 380 381static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol, 382 struct snd_ctl_elem_value *ucontrol) 383{ 384 int ret; 385 struct gbaudio_ctl_pvt *data; 386 struct gb_audio_ctl_elem_value gbvalue; 387 struct gbaudio_module_info *module; 388 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); 389 struct snd_soc_dapm_widget *widget = wlist->widgets[0]; 390 struct device *codec_dev = widget->dapm->dev; 391 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev); 392 struct gb_bundle *bundle; 393 394 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name); 395 module = find_gb_module(gb, kcontrol->id.name); 396 if (!module) 397 return -EINVAL; 398 399 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 400 bundle = to_gb_bundle(module->dev); 401 402 if (data->vcount == 2) 403 dev_warn(widget->dapm->dev, 404 "GB: Control '%s' is stereo, which is not supported\n", 405 kcontrol->id.name); 406 407 ret = gb_pm_runtime_get_sync(bundle); 408 if (ret) 409 return ret; 410 411 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id, 412 GB_AUDIO_INVALID_INDEX, &gbvalue); 413 414 gb_pm_runtime_put_autosuspend(bundle); 415 416 if (ret) { 417 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret, 418 __func__, kcontrol->id.name); 419 return ret; 420 } 421 /* update ucontrol */ 422 ucontrol->value.integer.value[0] = 423 le32_to_cpu(gbvalue.value.integer_value[0]); 424 425 return ret; 426} 427 428static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol, 429 struct snd_ctl_elem_value *ucontrol) 430{ 431 int ret, wi, max, connect; 432 unsigned int mask, val; 433 struct gb_audio_ctl_elem_info *info; 434 struct gbaudio_ctl_pvt *data; 435 struct gb_audio_ctl_elem_value gbvalue; 436 struct gbaudio_module_info *module; 437 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); 438 struct snd_soc_dapm_widget *widget = wlist->widgets[0]; 439 struct device *codec_dev = widget->dapm->dev; 440 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev); 441 struct gb_bundle *bundle; 442 443 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name); 444 module = find_gb_module(gb, kcontrol->id.name); 445 if (!module) 446 return -EINVAL; 447 448 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value; 449 info = (struct gb_audio_ctl_elem_info *)data->info; 450 bundle = to_gb_bundle(module->dev); 451 452 if (data->vcount == 2) 453 dev_warn(widget->dapm->dev, 454 "GB: Control '%s' is stereo, which is not supported\n", 455 kcontrol->id.name); 456 457 max = le32_to_cpu(info->value.integer.max); 458 mask = (1 << fls(max)) - 1; 459 val = ucontrol->value.integer.value[0] & mask; 460 connect = !!val; 461 462 ret = gb_pm_runtime_get_sync(bundle); 463 if (ret) 464 return ret; 465 466 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id, 467 GB_AUDIO_INVALID_INDEX, &gbvalue); 468 if (ret) 469 goto exit; 470 471 /* update ucontrol */ 472 if (le32_to_cpu(gbvalue.value.integer_value[0]) != val) { 473 for (wi = 0; wi < wlist->num_widgets; wi++) { 474 widget = wlist->widgets[wi]; 475 snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol, 476 connect, NULL); 477 } 478 gbvalue.value.integer_value[0] = 479 cpu_to_le32(ucontrol->value.integer.value[0]); 480 481 ret = gb_audio_gb_set_control(module->mgmt_connection, 482 data->ctl_id, 483 GB_AUDIO_INVALID_INDEX, &gbvalue); 484 } 485 486exit: 487 gb_pm_runtime_put_autosuspend(bundle); 488 if (ret) 489 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret, 490 __func__, kcontrol->id.name); 491 return ret; 492} 493 494#define SOC_DAPM_MIXER_GB(xname, kcount, data) \ 495{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ 496 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \ 497 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \ 498 .private_value = (unsigned long)data} 499 500static int gbcodec_event_spk(struct snd_soc_dapm_widget *w, 501 struct snd_kcontrol *k, int event) 502{ 503 /* Ensure GB speaker is connected */ 504 505 return 0; 506} 507 508static int gbcodec_event_hp(struct snd_soc_dapm_widget *w, 509 struct snd_kcontrol *k, int event) 510{ 511 /* Ensure GB module supports jack slot */ 512 513 return 0; 514} 515 516static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w, 517 struct snd_kcontrol *k, int event) 518{ 519 /* Ensure GB module supports jack slot */ 520 521 return 0; 522} 523 524static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w) 525{ 526 int ret = 0; 527 528 switch (w->type) { 529 case snd_soc_dapm_spk: 530 case snd_soc_dapm_hp: 531 case snd_soc_dapm_mic: 532 case snd_soc_dapm_output: 533 case snd_soc_dapm_input: 534 if (w->ncontrols) 535 ret = -EINVAL; 536 break; 537 case snd_soc_dapm_switch: 538 case snd_soc_dapm_mux: 539 if (w->ncontrols != 1) 540 ret = -EINVAL; 541 break; 542 default: 543 break; 544 } 545 546 return ret; 547} 548 549static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol, 550 struct snd_ctl_elem_value *ucontrol) 551{ 552 int ret, ctl_id; 553 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 554 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp); 555 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 556 struct gb_audio_ctl_elem_value gbvalue; 557 struct gbaudio_module_info *module; 558 struct gb_bundle *bundle; 559 560 module = find_gb_module(gb, kcontrol->id.name); 561 if (!module) 562 return -EINVAL; 563 564 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name); 565 if (ctl_id < 0) 566 return -EINVAL; 567 568 bundle = to_gb_bundle(module->dev); 569 570 ret = gb_pm_runtime_get_sync(bundle); 571 if (ret) 572 return ret; 573 574 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id, 575 GB_AUDIO_INVALID_INDEX, &gbvalue); 576 577 gb_pm_runtime_put_autosuspend(bundle); 578 579 if (ret) { 580 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret, 581 __func__, kcontrol->id.name); 582 return ret; 583 } 584 585 ucontrol->value.enumerated.item[0] = 586 le32_to_cpu(gbvalue.value.enumerated_item[0]); 587 if (e->shift_l != e->shift_r) 588 ucontrol->value.enumerated.item[1] = 589 le32_to_cpu(gbvalue.value.enumerated_item[1]); 590 591 return 0; 592} 593 594static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol, 595 struct snd_ctl_elem_value *ucontrol) 596{ 597 int ret, ctl_id; 598 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 599 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp); 600 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 601 struct gb_audio_ctl_elem_value gbvalue; 602 struct gbaudio_module_info *module; 603 struct gb_bundle *bundle; 604 605 module = find_gb_module(gb, kcontrol->id.name); 606 if (!module) 607 return -EINVAL; 608 609 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name); 610 if (ctl_id < 0) 611 return -EINVAL; 612 613 if (ucontrol->value.enumerated.item[0] > e->items - 1) 614 return -EINVAL; 615 gbvalue.value.enumerated_item[0] = 616 cpu_to_le32(ucontrol->value.enumerated.item[0]); 617 618 if (e->shift_l != e->shift_r) { 619 if (ucontrol->value.enumerated.item[1] > e->items - 1) 620 return -EINVAL; 621 gbvalue.value.enumerated_item[1] = 622 cpu_to_le32(ucontrol->value.enumerated.item[1]); 623 } 624 625 bundle = to_gb_bundle(module->dev); 626 627 ret = gb_pm_runtime_get_sync(bundle); 628 if (ret) 629 return ret; 630 631 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id, 632 GB_AUDIO_INVALID_INDEX, &gbvalue); 633 634 gb_pm_runtime_put_autosuspend(bundle); 635 636 if (ret) { 637 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", 638 ret, __func__, kcontrol->id.name); 639 } 640 641 return ret; 642} 643 644static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb, 645 struct snd_kcontrol_new *kctl, 646 struct gb_audio_control *ctl) 647{ 648 struct soc_enum *gbe; 649 struct gb_audio_enumerated *gb_enum; 650 int i; 651 652 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL); 653 if (!gbe) 654 return -ENOMEM; 655 656 gb_enum = &ctl->info.value.enumerated; 657 658 /* since count=1, and reg is dummy */ 659 gbe->items = le32_to_cpu(gb_enum->items); 660 gbe->texts = gb_generate_enum_strings(gb, gb_enum); 661 if (!gbe->texts) 662 return -ENOMEM; 663 664 /* debug enum info */ 665 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items, 666 le16_to_cpu(gb_enum->names_length)); 667 for (i = 0; i < gbe->items; i++) 668 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]); 669 670 *kctl = (struct snd_kcontrol_new) 671 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get, 672 gbcodec_enum_ctl_put); 673 return 0; 674} 675 676static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb, 677 struct snd_kcontrol_new *kctl, 678 struct gb_audio_control *ctl) 679{ 680 int ret = 0; 681 struct gbaudio_ctl_pvt *ctldata; 682 683 switch (ctl->iface) { 684 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER: 685 switch (ctl->info.type) { 686 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED: 687 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl); 688 break; 689 default: 690 ctldata = devm_kzalloc(gb->dev, 691 sizeof(struct gbaudio_ctl_pvt), 692 GFP_KERNEL); 693 if (!ctldata) 694 return -ENOMEM; 695 ctldata->ctl_id = ctl->id; 696 ctldata->data_cport = le16_to_cpu(ctl->data_cport); 697 ctldata->access = le32_to_cpu(ctl->access); 698 ctldata->vcount = ctl->count_values; 699 ctldata->info = &ctl->info; 700 *kctl = (struct snd_kcontrol_new) 701 SOC_MIXER_GB(ctl->name, ctl->count, ctldata); 702 ctldata = NULL; 703 break; 704 } 705 break; 706 default: 707 return -EINVAL; 708 } 709 710 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id); 711 return ret; 712} 713 714static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol, 715 struct snd_ctl_elem_value *ucontrol) 716{ 717 int ret, ctl_id; 718 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); 719 struct snd_soc_dapm_widget *widget = wlist->widgets[0]; 720 struct gbaudio_module_info *module; 721 struct gb_audio_ctl_elem_value gbvalue; 722 struct device *codec_dev = widget->dapm->dev; 723 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev); 724 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 725 struct gb_bundle *bundle; 726 727 module = find_gb_module(gb, kcontrol->id.name); 728 if (!module) 729 return -EINVAL; 730 731 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name); 732 if (ctl_id < 0) 733 return -EINVAL; 734 735 bundle = to_gb_bundle(module->dev); 736 737 ret = gb_pm_runtime_get_sync(bundle); 738 if (ret) 739 return ret; 740 741 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id, 742 GB_AUDIO_INVALID_INDEX, &gbvalue); 743 744 gb_pm_runtime_put_autosuspend(bundle); 745 746 if (ret) { 747 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret, 748 __func__, kcontrol->id.name); 749 return ret; 750 } 751 752 ucontrol->value.enumerated.item[0] = le32_to_cpu(gbvalue.value.enumerated_item[0]); 753 if (e->shift_l != e->shift_r) 754 ucontrol->value.enumerated.item[1] = 755 le32_to_cpu(gbvalue.value.enumerated_item[1]); 756 757 return 0; 758} 759 760static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol, 761 struct snd_ctl_elem_value *ucontrol) 762{ 763 int ret, wi, ctl_id; 764 unsigned int val, mux, change; 765 unsigned int mask; 766 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); 767 struct snd_soc_dapm_widget *widget = wlist->widgets[0]; 768 struct gb_audio_ctl_elem_value gbvalue; 769 struct gbaudio_module_info *module; 770 struct device *codec_dev = widget->dapm->dev; 771 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev); 772 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 773 struct gb_bundle *bundle; 774 775 if (ucontrol->value.enumerated.item[0] > e->items - 1) 776 return -EINVAL; 777 778 module = find_gb_module(gb, kcontrol->id.name); 779 if (!module) 780 return -EINVAL; 781 782 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name); 783 if (ctl_id < 0) 784 return -EINVAL; 785 786 change = 0; 787 bundle = to_gb_bundle(module->dev); 788 789 ret = gb_pm_runtime_get_sync(bundle); 790 if (ret) 791 return ret; 792 793 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id, 794 GB_AUDIO_INVALID_INDEX, &gbvalue); 795 796 gb_pm_runtime_put_autosuspend(bundle); 797 798 if (ret) { 799 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret, 800 __func__, kcontrol->id.name); 801 return ret; 802 } 803 804 mux = ucontrol->value.enumerated.item[0]; 805 val = mux << e->shift_l; 806 mask = e->mask << e->shift_l; 807 808 if (le32_to_cpu(gbvalue.value.enumerated_item[0]) != 809 ucontrol->value.enumerated.item[0]) { 810 change = 1; 811 gbvalue.value.enumerated_item[0] = 812 cpu_to_le32(ucontrol->value.enumerated.item[0]); 813 } 814 815 if (e->shift_l != e->shift_r) { 816 if (ucontrol->value.enumerated.item[1] > e->items - 1) 817 return -EINVAL; 818 val |= ucontrol->value.enumerated.item[1] << e->shift_r; 819 mask |= e->mask << e->shift_r; 820 if (le32_to_cpu(gbvalue.value.enumerated_item[1]) != 821 ucontrol->value.enumerated.item[1]) { 822 change = 1; 823 gbvalue.value.enumerated_item[1] = 824 cpu_to_le32(ucontrol->value.enumerated.item[1]); 825 } 826 } 827 828 if (change) { 829 ret = gb_pm_runtime_get_sync(bundle); 830 if (ret) 831 return ret; 832 833 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id, 834 GB_AUDIO_INVALID_INDEX, &gbvalue); 835 836 gb_pm_runtime_put_autosuspend(bundle); 837 838 if (ret) { 839 dev_err_ratelimited(codec_dev, 840 "%d:Error in %s for %s\n", ret, 841 __func__, kcontrol->id.name); 842 } 843 for (wi = 0; wi < wlist->num_widgets; wi++) { 844 widget = wlist->widgets[wi]; 845 snd_soc_dapm_mux_update_power(widget->dapm, kcontrol, 846 val, e, NULL); 847 } 848 } 849 850 return change; 851} 852 853static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb, 854 struct snd_kcontrol_new *kctl, 855 struct gb_audio_control *ctl) 856{ 857 struct soc_enum *gbe; 858 struct gb_audio_enumerated *gb_enum; 859 int i; 860 861 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL); 862 if (!gbe) 863 return -ENOMEM; 864 865 gb_enum = &ctl->info.value.enumerated; 866 867 /* since count=1, and reg is dummy */ 868 gbe->items = le32_to_cpu(gb_enum->items); 869 gbe->texts = gb_generate_enum_strings(gb, gb_enum); 870 if (!gbe->texts) 871 return -ENOMEM; 872 873 /* debug enum info */ 874 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items, 875 le16_to_cpu(gb_enum->names_length)); 876 for (i = 0; i < gbe->items; i++) 877 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]); 878 879 *kctl = (struct snd_kcontrol_new) 880 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get, 881 gbcodec_enum_dapm_ctl_put); 882 return 0; 883} 884 885static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb, 886 struct snd_kcontrol_new *kctl, 887 struct gb_audio_control *ctl) 888{ 889 struct gbaudio_ctl_pvt *ctldata; 890 891 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt), 892 GFP_KERNEL); 893 if (!ctldata) 894 return -ENOMEM; 895 ctldata->ctl_id = ctl->id; 896 ctldata->data_cport = le16_to_cpu(ctl->data_cport); 897 ctldata->access = le32_to_cpu(ctl->access); 898 ctldata->vcount = ctl->count_values; 899 ctldata->info = &ctl->info; 900 *kctl = (struct snd_kcontrol_new) 901 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata); 902 903 return 0; 904} 905 906static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb, 907 struct snd_kcontrol_new *kctl, 908 struct gb_audio_control *ctl) 909{ 910 int ret; 911 912 switch (ctl->iface) { 913 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER: 914 switch (ctl->info.type) { 915 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED: 916 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl); 917 break; 918 default: 919 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl); 920 break; 921 } 922 break; 923 default: 924 return -EINVAL; 925 } 926 927 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name, 928 ctl->id, ret); 929 return ret; 930} 931 932static int gbaudio_widget_event(struct snd_soc_dapm_widget *w, 933 struct snd_kcontrol *kcontrol, int event) 934{ 935 int wid; 936 int ret; 937 struct device *codec_dev = w->dapm->dev; 938 struct gbaudio_codec_info *gbcodec = dev_get_drvdata(codec_dev); 939 struct gbaudio_module_info *module; 940 struct gb_bundle *bundle; 941 942 dev_dbg(codec_dev, "%s %s %d\n", __func__, w->name, event); 943 944 /* Find relevant module */ 945 module = find_gb_module(gbcodec, w->name); 946 if (!module) 947 return -EINVAL; 948 949 /* map name to widget id */ 950 wid = gbaudio_map_widgetname(module, w->name); 951 if (wid < 0) { 952 dev_err(codec_dev, "Invalid widget name:%s\n", w->name); 953 return -EINVAL; 954 } 955 956 bundle = to_gb_bundle(module->dev); 957 958 ret = gb_pm_runtime_get_sync(bundle); 959 if (ret) 960 return ret; 961 962 switch (event) { 963 case SND_SOC_DAPM_PRE_PMU: 964 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid); 965 if (!ret) 966 ret = gbaudio_module_update(gbcodec, w, module, 1); 967 break; 968 case SND_SOC_DAPM_POST_PMD: 969 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid); 970 if (!ret) 971 ret = gbaudio_module_update(gbcodec, w, module, 0); 972 break; 973 } 974 if (ret) 975 dev_err_ratelimited(codec_dev, 976 "%d: widget, event:%d failed:%d\n", wid, 977 event, ret); 978 979 gb_pm_runtime_put_autosuspend(bundle); 980 981 return ret; 982} 983 984static const struct snd_soc_dapm_widget gbaudio_widgets[] = { 985 [snd_soc_dapm_spk] = SND_SOC_DAPM_SPK(NULL, gbcodec_event_spk), 986 [snd_soc_dapm_hp] = SND_SOC_DAPM_HP(NULL, gbcodec_event_hp), 987 [snd_soc_dapm_mic] = SND_SOC_DAPM_MIC(NULL, gbcodec_event_int_mic), 988 [snd_soc_dapm_output] = SND_SOC_DAPM_OUTPUT(NULL), 989 [snd_soc_dapm_input] = SND_SOC_DAPM_INPUT(NULL), 990 [snd_soc_dapm_switch] = SND_SOC_DAPM_SWITCH_E(NULL, SND_SOC_NOPM, 991 0, 0, NULL, 992 gbaudio_widget_event, 993 SND_SOC_DAPM_PRE_PMU | 994 SND_SOC_DAPM_POST_PMD), 995 [snd_soc_dapm_pga] = SND_SOC_DAPM_PGA_E(NULL, SND_SOC_NOPM, 996 0, 0, NULL, 0, 997 gbaudio_widget_event, 998 SND_SOC_DAPM_PRE_PMU | 999 SND_SOC_DAPM_POST_PMD), 1000 [snd_soc_dapm_mixer] = SND_SOC_DAPM_MIXER_E(NULL, SND_SOC_NOPM, 1001 0, 0, NULL, 0, 1002 gbaudio_widget_event, 1003 SND_SOC_DAPM_PRE_PMU | 1004 SND_SOC_DAPM_POST_PMD), 1005 [snd_soc_dapm_mux] = SND_SOC_DAPM_MUX_E(NULL, SND_SOC_NOPM, 1006 0, 0, NULL, 1007 gbaudio_widget_event, 1008 SND_SOC_DAPM_PRE_PMU | 1009 SND_SOC_DAPM_POST_PMD), 1010 [snd_soc_dapm_aif_in] = SND_SOC_DAPM_AIF_IN_E(NULL, NULL, 0, 1011 SND_SOC_NOPM, 0, 0, 1012 gbaudio_widget_event, 1013 SND_SOC_DAPM_PRE_PMU | 1014 SND_SOC_DAPM_POST_PMD), 1015 [snd_soc_dapm_aif_out] = SND_SOC_DAPM_AIF_OUT_E(NULL, NULL, 0, 1016 SND_SOC_NOPM, 0, 0, 1017 gbaudio_widget_event, 1018 SND_SOC_DAPM_PRE_PMU | 1019 SND_SOC_DAPM_POST_PMD), 1020}; 1021 1022static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module, 1023 struct snd_soc_dapm_widget *dw, 1024 struct gb_audio_widget *w, int *w_size) 1025{ 1026 int i, ret, csize; 1027 struct snd_kcontrol_new *widget_kctls; 1028 struct gb_audio_control *curr; 1029 struct gbaudio_control *control, *_control; 1030 size_t size; 1031 char temp_name[NAME_SIZE]; 1032 1033 ret = gbaudio_validate_kcontrol_count(w); 1034 if (ret) { 1035 dev_err(module->dev, "Invalid kcontrol count=%d for %s\n", 1036 w->ncontrols, w->name); 1037 return ret; 1038 } 1039 1040 /* allocate memory for kcontrol */ 1041 if (w->ncontrols) { 1042 size = sizeof(struct snd_kcontrol_new) * w->ncontrols; 1043 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL); 1044 if (!widget_kctls) 1045 return -ENOMEM; 1046 } 1047 1048 *w_size = sizeof(struct gb_audio_widget); 1049 1050 /* create relevant kcontrols */ 1051 curr = w->ctl; 1052 for (i = 0; i < w->ncontrols; i++) { 1053 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i], 1054 curr); 1055 if (ret) { 1056 dev_err(module->dev, 1057 "%s:%d type widget_ctl not supported\n", 1058 curr->name, curr->iface); 1059 goto error; 1060 } 1061 control = devm_kzalloc(module->dev, 1062 sizeof(struct gbaudio_control), 1063 GFP_KERNEL); 1064 if (!control) { 1065 ret = -ENOMEM; 1066 goto error; 1067 } 1068 control->id = curr->id; 1069 control->name = curr->name; 1070 control->wname = w->name; 1071 1072 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) { 1073 struct gb_audio_enumerated *gbenum = 1074 &curr->info.value.enumerated; 1075 1076 csize = offsetof(struct gb_audio_control, info); 1077 csize += offsetof(struct gb_audio_ctl_elem_info, value); 1078 csize += offsetof(struct gb_audio_enumerated, names); 1079 csize += le16_to_cpu(gbenum->names_length); 1080 control->texts = (const char * const *) 1081 gb_generate_enum_strings(module, gbenum); 1082 if (!control->texts) { 1083 ret = -ENOMEM; 1084 goto error; 1085 } 1086 control->items = le32_to_cpu(gbenum->items); 1087 } else { 1088 csize = sizeof(struct gb_audio_control); 1089 } 1090 1091 *w_size += csize; 1092 curr = (void *)curr + csize; 1093 list_add(&control->list, &module->widget_ctl_list); 1094 dev_dbg(module->dev, "%s: control of type %d created\n", 1095 widget_kctls[i].name, widget_kctls[i].iface); 1096 } 1097 1098 /* Prefix dev_id to widget control_name */ 1099 strscpy(temp_name, w->name, sizeof(temp_name)); 1100 snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name); 1101 1102 switch (w->type) { 1103 case snd_soc_dapm_spk: 1104 *dw = gbaudio_widgets[w->type]; 1105 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER; 1106 break; 1107 case snd_soc_dapm_hp: 1108 *dw = gbaudio_widgets[w->type]; 1109 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET 1110 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE); 1111 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET; 1112 break; 1113 case snd_soc_dapm_mic: 1114 *dw = gbaudio_widgets[w->type]; 1115 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC; 1116 break; 1117 case snd_soc_dapm_output: 1118 case snd_soc_dapm_input: 1119 case snd_soc_dapm_switch: 1120 case snd_soc_dapm_pga: 1121 case snd_soc_dapm_mixer: 1122 case snd_soc_dapm_mux: 1123 *dw = gbaudio_widgets[w->type]; 1124 break; 1125 case snd_soc_dapm_aif_in: 1126 case snd_soc_dapm_aif_out: 1127 *dw = gbaudio_widgets[w->type]; 1128 dw->sname = w->sname; 1129 break; 1130 default: 1131 ret = -EINVAL; 1132 goto error; 1133 } 1134 dw->name = w->name; 1135 1136 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name, 1137 dw->id); 1138 return 0; 1139error: 1140 list_for_each_entry_safe(control, _control, &module->widget_ctl_list, 1141 list) { 1142 list_del(&control->list); 1143 devm_kfree(module->dev, control); 1144 } 1145 return ret; 1146} 1147 1148static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module, 1149 struct gb_audio_control *controls) 1150{ 1151 int i, csize, ret; 1152 struct snd_kcontrol_new *dapm_kctls; 1153 struct gb_audio_control *curr; 1154 struct gbaudio_control *control, *_control; 1155 size_t size; 1156 char temp_name[NAME_SIZE]; 1157 1158 size = sizeof(struct snd_kcontrol_new) * module->num_controls; 1159 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL); 1160 if (!dapm_kctls) 1161 return -ENOMEM; 1162 1163 curr = controls; 1164 for (i = 0; i < module->num_controls; i++) { 1165 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i], 1166 curr); 1167 if (ret) { 1168 dev_err(module->dev, "%s:%d type not supported\n", 1169 curr->name, curr->iface); 1170 goto error; 1171 } 1172 control = devm_kzalloc(module->dev, sizeof(struct 1173 gbaudio_control), 1174 GFP_KERNEL); 1175 if (!control) { 1176 ret = -ENOMEM; 1177 goto error; 1178 } 1179 control->id = curr->id; 1180 /* Prefix dev_id to widget_name */ 1181 strscpy(temp_name, curr->name, sizeof(temp_name)); 1182 snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id, 1183 temp_name); 1184 control->name = curr->name; 1185 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) { 1186 struct gb_audio_enumerated *gbenum = 1187 &curr->info.value.enumerated; 1188 1189 csize = offsetof(struct gb_audio_control, info); 1190 csize += offsetof(struct gb_audio_ctl_elem_info, value); 1191 csize += offsetof(struct gb_audio_enumerated, names); 1192 csize += le16_to_cpu(gbenum->names_length); 1193 control->texts = (const char * const *) 1194 gb_generate_enum_strings(module, gbenum); 1195 if (!control->texts) { 1196 ret = -ENOMEM; 1197 goto error; 1198 } 1199 control->items = le32_to_cpu(gbenum->items); 1200 } else { 1201 csize = sizeof(struct gb_audio_control); 1202 } 1203 1204 list_add(&control->list, &module->ctl_list); 1205 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id, 1206 curr->name, curr->info.type); 1207 curr = (void *)curr + csize; 1208 } 1209 module->controls = dapm_kctls; 1210 1211 return 0; 1212error: 1213 list_for_each_entry_safe(control, _control, &module->ctl_list, 1214 list) { 1215 list_del(&control->list); 1216 devm_kfree(module->dev, control); 1217 } 1218 devm_kfree(module->dev, dapm_kctls); 1219 return ret; 1220} 1221 1222static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module, 1223 struct gb_audio_widget *widgets) 1224{ 1225 int i, ret, w_size; 1226 struct snd_soc_dapm_widget *dapm_widgets; 1227 struct gb_audio_widget *curr; 1228 struct gbaudio_widget *widget, *_widget; 1229 size_t size; 1230 1231 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets; 1232 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL); 1233 if (!dapm_widgets) 1234 return -ENOMEM; 1235 1236 curr = widgets; 1237 for (i = 0; i < module->num_dapm_widgets; i++) { 1238 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i], 1239 curr, &w_size); 1240 if (ret) { 1241 dev_err(module->dev, "%s:%d type not supported\n", 1242 curr->name, curr->type); 1243 goto error; 1244 } 1245 widget = devm_kzalloc(module->dev, sizeof(struct 1246 gbaudio_widget), 1247 GFP_KERNEL); 1248 if (!widget) { 1249 ret = -ENOMEM; 1250 goto error; 1251 } 1252 widget->id = curr->id; 1253 widget->name = curr->name; 1254 list_add(&widget->list, &module->widget_list); 1255 curr = (void *)curr + w_size; 1256 } 1257 module->dapm_widgets = dapm_widgets; 1258 1259 return 0; 1260 1261error: 1262 list_for_each_entry_safe(widget, _widget, &module->widget_list, 1263 list) { 1264 list_del(&widget->list); 1265 devm_kfree(module->dev, widget); 1266 } 1267 devm_kfree(module->dev, dapm_widgets); 1268 return ret; 1269} 1270 1271static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module, 1272 struct gb_audio_route *routes) 1273{ 1274 int i, ret; 1275 struct snd_soc_dapm_route *dapm_routes; 1276 struct gb_audio_route *curr; 1277 size_t size; 1278 1279 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes; 1280 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL); 1281 if (!dapm_routes) 1282 return -ENOMEM; 1283 1284 module->dapm_routes = dapm_routes; 1285 curr = routes; 1286 1287 for (i = 0; i < module->num_dapm_routes; i++) { 1288 dapm_routes->sink = 1289 gbaudio_map_widgetid(module, curr->destination_id); 1290 if (!dapm_routes->sink) { 1291 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n", 1292 curr->source_id, curr->destination_id, 1293 curr->control_id, curr->index); 1294 ret = -EINVAL; 1295 goto error; 1296 } 1297 dapm_routes->source = 1298 gbaudio_map_widgetid(module, curr->source_id); 1299 if (!dapm_routes->source) { 1300 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n", 1301 curr->source_id, curr->destination_id, 1302 curr->control_id, curr->index); 1303 ret = -EINVAL; 1304 goto error; 1305 } 1306 dapm_routes->control = 1307 gbaudio_map_controlid(module, 1308 curr->control_id, 1309 curr->index); 1310 if ((curr->control_id != GBAUDIO_INVALID_ID) && 1311 !dapm_routes->control) { 1312 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n", 1313 curr->source_id, curr->destination_id, 1314 curr->control_id, curr->index); 1315 ret = -EINVAL; 1316 goto error; 1317 } 1318 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink, 1319 (dapm_routes->control) ? dapm_routes->control : "NULL", 1320 dapm_routes->source); 1321 dapm_routes++; 1322 curr++; 1323 } 1324 1325 return 0; 1326 1327error: 1328 devm_kfree(module->dev, module->dapm_routes); 1329 return ret; 1330} 1331 1332static int gbaudio_tplg_process_header(struct gbaudio_module_info *module, 1333 struct gb_audio_topology *tplg_data) 1334{ 1335 /* fetch no. of kcontrols, widgets & routes */ 1336 module->num_controls = tplg_data->num_controls; 1337 module->num_dapm_widgets = tplg_data->num_widgets; 1338 module->num_dapm_routes = tplg_data->num_routes; 1339 1340 /* update block offset */ 1341 module->dai_offset = (unsigned long)&tplg_data->data; 1342 module->control_offset = module->dai_offset + 1343 le32_to_cpu(tplg_data->size_dais); 1344 module->widget_offset = module->control_offset + 1345 le32_to_cpu(tplg_data->size_controls); 1346 module->route_offset = module->widget_offset + 1347 le32_to_cpu(tplg_data->size_widgets); 1348 1349 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset); 1350 dev_dbg(module->dev, "control offset is %lx\n", 1351 module->control_offset); 1352 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset); 1353 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset); 1354 1355 return 0; 1356} 1357 1358int gbaudio_tplg_parse_data(struct gbaudio_module_info *module, 1359 struct gb_audio_topology *tplg_data) 1360{ 1361 int ret; 1362 struct gb_audio_control *controls; 1363 struct gb_audio_widget *widgets; 1364 struct gb_audio_route *routes; 1365 unsigned int jack_type; 1366 1367 if (!tplg_data) 1368 return -EINVAL; 1369 1370 ret = gbaudio_tplg_process_header(module, tplg_data); 1371 if (ret) { 1372 dev_err(module->dev, "%d: Error in parsing topology header\n", 1373 ret); 1374 return ret; 1375 } 1376 1377 /* process control */ 1378 controls = (struct gb_audio_control *)module->control_offset; 1379 ret = gbaudio_tplg_process_kcontrols(module, controls); 1380 if (ret) { 1381 dev_err(module->dev, 1382 "%d: Error in parsing controls data\n", ret); 1383 return ret; 1384 } 1385 dev_dbg(module->dev, "Control parsing finished\n"); 1386 1387 /* process widgets */ 1388 widgets = (struct gb_audio_widget *)module->widget_offset; 1389 ret = gbaudio_tplg_process_widgets(module, widgets); 1390 if (ret) { 1391 dev_err(module->dev, 1392 "%d: Error in parsing widgets data\n", ret); 1393 return ret; 1394 } 1395 dev_dbg(module->dev, "Widget parsing finished\n"); 1396 1397 /* process route */ 1398 routes = (struct gb_audio_route *)module->route_offset; 1399 ret = gbaudio_tplg_process_routes(module, routes); 1400 if (ret) { 1401 dev_err(module->dev, 1402 "%d: Error in parsing routes data\n", ret); 1403 return ret; 1404 } 1405 dev_dbg(module->dev, "Route parsing finished\n"); 1406 1407 /* parse jack capabilities */ 1408 jack_type = le32_to_cpu(tplg_data->jack_type); 1409 if (jack_type) { 1410 module->jack_mask = jack_type & GBCODEC_JACK_MASK; 1411 module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK; 1412 } 1413 1414 return ret; 1415} 1416 1417void gbaudio_tplg_release(struct gbaudio_module_info *module) 1418{ 1419 struct gbaudio_control *control, *_control; 1420 struct gbaudio_widget *widget, *_widget; 1421 1422 if (!module->topology) 1423 return; 1424 1425 /* release kcontrols */ 1426 list_for_each_entry_safe(control, _control, &module->ctl_list, 1427 list) { 1428 list_del(&control->list); 1429 devm_kfree(module->dev, control); 1430 } 1431 if (module->controls) 1432 devm_kfree(module->dev, module->controls); 1433 1434 /* release widget controls */ 1435 list_for_each_entry_safe(control, _control, &module->widget_ctl_list, 1436 list) { 1437 list_del(&control->list); 1438 devm_kfree(module->dev, control); 1439 } 1440 1441 /* release widgets */ 1442 list_for_each_entry_safe(widget, _widget, &module->widget_list, 1443 list) { 1444 list_del(&widget->list); 1445 devm_kfree(module->dev, widget); 1446 } 1447 if (module->dapm_widgets) 1448 devm_kfree(module->dev, module->dapm_widgets); 1449 1450 /* release routes */ 1451 if (module->dapm_routes) 1452 devm_kfree(module->dev, module->dapm_routes); 1453}