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