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