linux/drivers/staging/greybus/audio_topology.c
<<
>>
Prefs
   1/*
   2 * Greybus audio driver
   3 * Copyright 2015-2016 Google Inc.
   4 * Copyright 2015-2016 Linaro Ltd.
   5 *
   6 * Released under the GPLv2 only.
   7 */
   8
   9#include "audio_codec.h"
  10#include "greybus_protocols.h"
  11
  12#define GBAUDIO_INVALID_ID      0xFF
  13
  14/* mixer control */
  15struct gb_mixer_control {
  16        int min, max;
  17        unsigned int reg, rreg, shift, rshift, invert;
  18};
  19
  20struct gbaudio_ctl_pvt {
  21        unsigned int ctl_id;
  22        unsigned int data_cport;
  23        unsigned int access;
  24        unsigned int vcount;
  25        struct gb_audio_ctl_elem_info *info;
  26};
  27
  28static struct gbaudio_module_info *find_gb_module(
  29                                        struct gbaudio_codec_info *codec,
  30                                        char const *name)
  31{
  32        int dev_id, ret;
  33        char begin[NAME_SIZE];
  34        struct gbaudio_module_info *module;
  35
  36        if (!name)
  37                return NULL;
  38
  39        ret = sscanf(name, "%s %d", begin, &dev_id);
  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_kzalloc(gb->dev, sizeof(char *) * items, GFP_KERNEL);
 149        data = gbenum->names;
 150
 151        for (i = 0; i < items; i++) {
 152                strings[i] = (const char *)data;
 153                while (*data != '\0')
 154                        data++;
 155                data++;
 156        }
 157
 158        return strings;
 159}
 160
 161static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
 162                     struct snd_ctl_elem_info *uinfo)
 163{
 164        unsigned int max;
 165        const char *name;
 166        struct gbaudio_ctl_pvt *data;
 167        struct gb_audio_ctl_elem_info *info;
 168        struct gbaudio_module_info *module;
 169        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 170        struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
 171
 172        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 173        data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
 174        info = (struct gb_audio_ctl_elem_info *)data->info;
 175
 176        if (!info) {
 177                dev_err(codec->dev, "NULL info for %s\n", uinfo->id.name);
 178                return -EINVAL;
 179        }
 180
 181        /* update uinfo */
 182        uinfo->access = data->access;
 183        uinfo->count = data->vcount;
 184        uinfo->type = (snd_ctl_elem_type_t)info->type;
 185
 186        switch (info->type) {
 187        case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
 188        case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
 189                uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
 190                uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
 191                break;
 192        case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
 193                max = le32_to_cpu(info->value.enumerated.items);
 194                uinfo->value.enumerated.items = max;
 195                if (uinfo->value.enumerated.item > max - 1)
 196                        uinfo->value.enumerated.item = max - 1;
 197                module = find_gb_module(gbcodec, kcontrol->id.name);
 198                if (!module)
 199                        return -EINVAL;
 200                name = gbaudio_map_controlid(module, data->ctl_id,
 201                                             uinfo->value.enumerated.item);
 202                strlcpy(uinfo->value.enumerated.name, name, NAME_SIZE);
 203                break;
 204        default:
 205                dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
 206                        info->type, kcontrol->id.name);
 207                break;
 208        }
 209        return 0;
 210}
 211
 212static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
 213        struct snd_ctl_elem_value *ucontrol)
 214{
 215        int ret;
 216        struct gb_audio_ctl_elem_info *info;
 217        struct gbaudio_ctl_pvt *data;
 218        struct gb_audio_ctl_elem_value gbvalue;
 219        struct gbaudio_module_info *module;
 220        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 221        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 222        struct gb_bundle *bundle;
 223
 224        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 225        module = find_gb_module(gb, kcontrol->id.name);
 226        if (!module)
 227                return -EINVAL;
 228
 229        data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
 230        info = (struct gb_audio_ctl_elem_info *)data->info;
 231        bundle = to_gb_bundle(module->dev);
 232
 233        ret = gb_pm_runtime_get_sync(bundle);
 234        if (ret)
 235                return ret;
 236
 237        ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
 238                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 239
 240        gb_pm_runtime_put_autosuspend(bundle);
 241
 242        if (ret) {
 243                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 244                                    __func__, kcontrol->id.name);
 245                return ret;
 246        }
 247
 248        /* update ucontrol */
 249        switch (info->type) {
 250        case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
 251        case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
 252                ucontrol->value.integer.value[0] =
 253                        le32_to_cpu(gbvalue.value.integer_value[0]);
 254                if (data->vcount == 2)
 255                        ucontrol->value.integer.value[1] =
 256                                le32_to_cpu(gbvalue.value.integer_value[1]);
 257                break;
 258        case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
 259                ucontrol->value.enumerated.item[0] =
 260                        le32_to_cpu(gbvalue.value.enumerated_item[0]);
 261                if (data->vcount == 2)
 262                        ucontrol->value.enumerated.item[1] =
 263                                le32_to_cpu(gbvalue.value.enumerated_item[1]);
 264                break;
 265        default:
 266                dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
 267                        info->type, kcontrol->id.name);
 268                ret = -EINVAL;
 269                break;
 270        }
 271        return ret;
 272}
 273
 274static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
 275                              struct snd_ctl_elem_value *ucontrol)
 276{
 277        int ret = 0;
 278        struct gb_audio_ctl_elem_info *info;
 279        struct gbaudio_ctl_pvt *data;
 280        struct gb_audio_ctl_elem_value gbvalue;
 281        struct gbaudio_module_info *module;
 282        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 283        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 284        struct gb_bundle *bundle;
 285
 286        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 287        module = find_gb_module(gb, kcontrol->id.name);
 288        if (!module)
 289                return -EINVAL;
 290
 291        data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
 292        info = (struct gb_audio_ctl_elem_info *)data->info;
 293        bundle = to_gb_bundle(module->dev);
 294
 295        /* update ucontrol */
 296        switch (info->type) {
 297        case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
 298        case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
 299                gbvalue.value.integer_value[0] =
 300                        cpu_to_le32(ucontrol->value.integer.value[0]);
 301                if (data->vcount == 2)
 302                        gbvalue.value.integer_value[1] =
 303                                cpu_to_le32(ucontrol->value.integer.value[1]);
 304                break;
 305        case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
 306                gbvalue.value.enumerated_item[0] =
 307                        cpu_to_le32(ucontrol->value.enumerated.item[0]);
 308                if (data->vcount == 2)
 309                        gbvalue.value.enumerated_item[1] =
 310                                cpu_to_le32(ucontrol->value.enumerated.item[1]);
 311                break;
 312        default:
 313                dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
 314                        info->type, kcontrol->id.name);
 315                ret = -EINVAL;
 316                break;
 317        }
 318
 319        if (ret)
 320                return ret;
 321
 322        ret = gb_pm_runtime_get_sync(bundle);
 323        if (ret)
 324                return ret;
 325
 326        ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
 327                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 328
 329        gb_pm_runtime_put_autosuspend(bundle);
 330
 331        if (ret) {
 332                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 333                                    __func__, kcontrol->id.name);
 334        }
 335
 336        return ret;
 337}
 338
 339#define SOC_MIXER_GB(xname, kcount, data) \
 340{       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
 341        .count = kcount, .info = gbcodec_mixer_ctl_info, \
 342        .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
 343        .private_value = (unsigned long)data }
 344
 345/*
 346 * although below callback functions seems redundant to above functions.
 347 * same are kept to allow provision for different handling in case
 348 * of DAPM related sequencing, etc.
 349 */
 350static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
 351                     struct snd_ctl_elem_info *uinfo)
 352{
 353        int platform_max, platform_min;
 354        struct gbaudio_ctl_pvt *data;
 355        struct gb_audio_ctl_elem_info *info;
 356        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
 357        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
 358        struct snd_soc_codec *codec = widget->codec;
 359
 360        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 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", NAME_SIZE))
 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 gb_audio_ctl_elem_info *info;
 386        struct gbaudio_ctl_pvt *data;
 387        struct gb_audio_ctl_elem_value gbvalue;
 388        struct gbaudio_module_info *module;
 389        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
 390        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
 391        struct snd_soc_codec *codec = widget->codec;
 392        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 393        struct gb_bundle *bundle;
 394
 395        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 396        module = find_gb_module(gb, kcontrol->id.name);
 397        if (!module)
 398                return -EINVAL;
 399
 400        data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
 401        info = (struct gb_audio_ctl_elem_info *)data->info;
 402        bundle = to_gb_bundle(module->dev);
 403
 404        if (data->vcount == 2)
 405                dev_warn(widget->dapm->dev,
 406                         "GB: Control '%s' is stereo, which is not supported\n",
 407                         kcontrol->id.name);
 408
 409        ret = gb_pm_runtime_get_sync(bundle);
 410        if (ret)
 411                return ret;
 412
 413        ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
 414                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 415
 416        gb_pm_runtime_put_autosuspend(bundle);
 417
 418        if (ret) {
 419                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 420                                    __func__, kcontrol->id.name);
 421                return ret;
 422        }
 423        /* update ucontrol */
 424        ucontrol->value.integer.value[0] =
 425                le32_to_cpu(gbvalue.value.integer_value[0]);
 426
 427        return ret;
 428}
 429
 430static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
 431                              struct snd_ctl_elem_value *ucontrol)
 432{
 433        int ret, wi, max, connect;
 434        unsigned int mask, val;
 435        struct gb_audio_ctl_elem_info *info;
 436        struct gbaudio_ctl_pvt *data;
 437        struct gb_audio_ctl_elem_value gbvalue;
 438        struct gbaudio_module_info *module;
 439        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
 440        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
 441        struct snd_soc_codec *codec = widget->codec;
 442        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 443        struct gb_bundle *bundle;
 444
 445        dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
 446        module = find_gb_module(gb, kcontrol->id.name);
 447        if (!module)
 448                return -EINVAL;
 449
 450        data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
 451        info = (struct gb_audio_ctl_elem_info *)data->info;
 452        bundle = to_gb_bundle(module->dev);
 453
 454        if (data->vcount == 2)
 455                dev_warn(widget->dapm->dev,
 456                         "GB: Control '%s' is stereo, which is not supported\n",
 457                         kcontrol->id.name);
 458
 459        max = le32_to_cpu(info->value.integer.max);
 460        mask = (1 << fls(max)) - 1;
 461        val = ucontrol->value.integer.value[0] & mask;
 462        connect = !!val;
 463
 464        /* update ucontrol */
 465        if (gbvalue.value.integer_value[0] != val) {
 466                for (wi = 0; wi < wlist->num_widgets; wi++) {
 467                        widget = wlist->widgets[wi];
 468
 469                        widget->value = val;
 470                        widget->dapm->update = NULL;
 471                        snd_soc_dapm_mixer_update_power(widget, kcontrol,
 472                                                        connect);
 473                }
 474                gbvalue.value.integer_value[0] =
 475                        cpu_to_le32(ucontrol->value.integer.value[0]);
 476
 477                ret = gb_pm_runtime_get_sync(bundle);
 478                if (ret)
 479                        return ret;
 480
 481                ret = gb_audio_gb_set_control(module->mgmt_connection,
 482                                              data->ctl_id,
 483                                              GB_AUDIO_INVALID_INDEX, &gbvalue);
 484
 485                gb_pm_runtime_put_autosuspend(bundle);
 486
 487                if (ret) {
 488                        dev_err_ratelimited(codec->dev,
 489                                            "%d:Error in %s for %s\n", ret,
 490                                            __func__, kcontrol->id.name);
 491                        return ret;
 492                }
 493        }
 494
 495        return 0;
 496}
 497
 498#define SOC_DAPM_MIXER_GB(xname, kcount, data) \
 499{       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
 500        .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
 501        .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
 502        .private_value = (unsigned long)data}
 503
 504static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
 505                                        struct snd_kcontrol *k, int event)
 506{
 507        /* Ensure GB speaker is connected */
 508
 509        return 0;
 510}
 511
 512static int gbcodec_event_hp(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
 520static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
 521                                        struct snd_kcontrol *k, int event)
 522{
 523        /* Ensure GB module supports jack slot */
 524
 525        return 0;
 526}
 527
 528static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
 529{
 530        int ret = 0;
 531
 532        switch (w->type) {
 533        case snd_soc_dapm_spk:
 534        case snd_soc_dapm_hp:
 535        case snd_soc_dapm_mic:
 536        case snd_soc_dapm_output:
 537        case snd_soc_dapm_input:
 538                if (w->ncontrols)
 539                        ret = -EINVAL;
 540                break;
 541        case snd_soc_dapm_switch:
 542        case snd_soc_dapm_mux:
 543                if (w->ncontrols != 1)
 544                        ret = -EINVAL;
 545                break;
 546        default:
 547                break;
 548        }
 549
 550        return ret;
 551}
 552
 553static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
 554                                struct snd_ctl_elem_value *ucontrol)
 555{
 556        int ret, ctl_id;
 557        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 558        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 559        struct gb_audio_ctl_elem_value gbvalue;
 560        struct gbaudio_module_info *module;
 561        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 562        struct gb_bundle *bundle;
 563
 564        module = find_gb_module(gb, kcontrol->id.name);
 565        if (!module)
 566                return -EINVAL;
 567
 568        ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
 569        if (ctl_id < 0)
 570                return -EINVAL;
 571
 572        bundle = to_gb_bundle(module->dev);
 573
 574        ret = gb_pm_runtime_get_sync(bundle);
 575        if (ret)
 576                return ret;
 577
 578        ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
 579                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 580
 581        gb_pm_runtime_put_autosuspend(bundle);
 582
 583        if (ret) {
 584                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 585                                    __func__, kcontrol->id.name);
 586                return ret;
 587        }
 588
 589        ucontrol->value.enumerated.item[0] =
 590                le32_to_cpu(gbvalue.value.enumerated_item[0]);
 591        if (e->shift_l != e->shift_r)
 592                ucontrol->value.enumerated.item[1] =
 593                        le32_to_cpu(gbvalue.value.enumerated_item[1]);
 594
 595        return 0;
 596}
 597
 598static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
 599                                struct snd_ctl_elem_value *ucontrol)
 600{
 601        int ret, ctl_id;
 602        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 603        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 604        struct gb_audio_ctl_elem_value gbvalue;
 605        struct gbaudio_module_info *module;
 606        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 607        struct gb_bundle *bundle;
 608
 609        module = find_gb_module(gb, kcontrol->id.name);
 610        if (!module)
 611                return -EINVAL;
 612
 613        ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
 614        if (ctl_id < 0)
 615                return -EINVAL;
 616
 617        if (ucontrol->value.enumerated.item[0] > e->max - 1)
 618                return -EINVAL;
 619        gbvalue.value.enumerated_item[0] =
 620                cpu_to_le32(ucontrol->value.enumerated.item[0]);
 621
 622        if (e->shift_l != e->shift_r) {
 623                if (ucontrol->value.enumerated.item[1] > e->max - 1)
 624                        return -EINVAL;
 625                gbvalue.value.enumerated_item[1] =
 626                        cpu_to_le32(ucontrol->value.enumerated.item[1]);
 627        }
 628
 629        bundle = to_gb_bundle(module->dev);
 630
 631        ret = gb_pm_runtime_get_sync(bundle);
 632        if (ret)
 633                return ret;
 634
 635        ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
 636                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 637
 638        gb_pm_runtime_put_autosuspend(bundle);
 639
 640        if (ret) {
 641                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 642                                    __func__, kcontrol->id.name);
 643        }
 644
 645        return ret;
 646}
 647
 648static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
 649                                         struct snd_kcontrol_new *kctl,
 650                                         struct gb_audio_control *ctl)
 651{
 652        struct soc_enum *gbe;
 653        struct gb_audio_enumerated *gb_enum;
 654        int i;
 655
 656        gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
 657        if (!gbe)
 658                return -ENOMEM;
 659
 660        gb_enum = &ctl->info.value.enumerated;
 661
 662        /* since count=1, and reg is dummy */
 663        gbe->max = le32_to_cpu(gb_enum->items);
 664        gbe->texts = gb_generate_enum_strings(gb, gb_enum);
 665
 666        /* debug enum info */
 667        dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->max,
 668                 le16_to_cpu(gb_enum->names_length));
 669        for (i = 0; i < gbe->max; i++)
 670                dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
 671
 672        *kctl = (struct snd_kcontrol_new)
 673                SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
 674                             gbcodec_enum_ctl_put);
 675        return 0;
 676}
 677
 678static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
 679                                        struct snd_kcontrol_new *kctl,
 680                                        struct gb_audio_control *ctl)
 681{
 682        int ret = 0;
 683        struct gbaudio_ctl_pvt *ctldata;
 684
 685        switch (ctl->iface) {
 686        case SNDRV_CTL_ELEM_IFACE_MIXER:
 687                switch (ctl->info.type) {
 688                case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
 689                        ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
 690                        break;
 691                default:
 692                        ctldata = devm_kzalloc(gb->dev,
 693                                               sizeof(struct gbaudio_ctl_pvt),
 694                                               GFP_KERNEL);
 695                        if (!ctldata)
 696                                return -ENOMEM;
 697                        ctldata->ctl_id = ctl->id;
 698                        ctldata->data_cport = le16_to_cpu(ctl->data_cport);
 699                        ctldata->access = ctl->access;
 700                        ctldata->vcount = ctl->count_values;
 701                        ctldata->info = &ctl->info;
 702                        *kctl = (struct snd_kcontrol_new)
 703                                SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
 704                        ctldata = NULL;
 705                        break;
 706                }
 707                break;
 708        default:
 709                return -EINVAL;
 710        }
 711
 712        dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
 713        return ret;
 714}
 715
 716static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
 717                                     struct snd_ctl_elem_value *ucontrol)
 718{
 719        int ret, ctl_id;
 720        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
 721        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
 722        struct gbaudio_module_info *module;
 723        struct gb_audio_ctl_elem_value gbvalue;
 724        struct snd_soc_codec *codec = widget->codec;
 725        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 726        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 727        struct gb_bundle *bundle;
 728
 729        module = find_gb_module(gb, kcontrol->id.name);
 730        if (!module)
 731                return -EINVAL;
 732
 733        ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
 734        if (ctl_id < 0)
 735                return -EINVAL;
 736
 737        bundle = to_gb_bundle(module->dev);
 738
 739        ret = gb_pm_runtime_get_sync(bundle);
 740        if (ret)
 741                return ret;
 742
 743        ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
 744                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 745
 746        gb_pm_runtime_put_autosuspend(bundle);
 747
 748        if (ret) {
 749                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 750                                    __func__, kcontrol->id.name);
 751                return ret;
 752        }
 753
 754        ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
 755        if (e->shift_l != e->shift_r)
 756                ucontrol->value.enumerated.item[1] =
 757                        gbvalue.value.enumerated_item[1];
 758
 759        return 0;
 760}
 761
 762static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
 763                                     struct snd_ctl_elem_value *ucontrol)
 764{
 765        int ret, wi, ctl_id;
 766        unsigned int val, mux, change;
 767        unsigned int mask;
 768        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
 769        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
 770        struct gb_audio_ctl_elem_value gbvalue;
 771        struct gbaudio_module_info *module;
 772        struct snd_soc_codec *codec = widget->codec;
 773        struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
 774        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 775        struct gb_bundle *bundle;
 776
 777        if (ucontrol->value.enumerated.item[0] > e->max - 1)
 778                return -EINVAL;
 779
 780        module = find_gb_module(gb, kcontrol->id.name);
 781        if (!module)
 782                return -EINVAL;
 783
 784        ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
 785        if (ctl_id < 0)
 786                return -EINVAL;
 787
 788        change = 0;
 789        bundle = to_gb_bundle(module->dev);
 790
 791        ret = gb_pm_runtime_get_sync(bundle);
 792        if (ret)
 793                return ret;
 794
 795        ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
 796                                      GB_AUDIO_INVALID_INDEX, &gbvalue);
 797
 798        gb_pm_runtime_put_autosuspend(bundle);
 799
 800        if (ret) {
 801                dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
 802                                    __func__, kcontrol->id.name);
 803                return ret;
 804        }
 805
 806        mux = ucontrol->value.enumerated.item[0];
 807        val = mux << e->shift_l;
 808        mask = e->mask << e->shift_l;
 809
 810        if (gbvalue.value.enumerated_item[0] !=
 811            ucontrol->value.enumerated.item[0]) {
 812                change = 1;
 813                gbvalue.value.enumerated_item[0] =
 814                        ucontrol->value.enumerated.item[0];
 815        }
 816
 817        if (e->shift_l != e->shift_r) {
 818                if (ucontrol->value.enumerated.item[1] > e->max - 1)
 819                        return -EINVAL;
 820                val |= ucontrol->value.enumerated.item[1] << e->shift_r;
 821                mask |= e->mask << e->shift_r;
 822                if (gbvalue.value.enumerated_item[1] !=
 823                    ucontrol->value.enumerated.item[1]) {
 824                        change = 1;
 825                        gbvalue.value.enumerated_item[1] =
 826                                ucontrol->value.enumerated.item[1];
 827                }
 828        }
 829
 830        if (change) {
 831                ret = gb_pm_runtime_get_sync(bundle);
 832                if (ret)
 833                        return ret;
 834
 835                ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
 836                                              GB_AUDIO_INVALID_INDEX, &gbvalue);
 837
 838                gb_pm_runtime_put_autosuspend(bundle);
 839
 840                if (ret) {
 841                        dev_err_ratelimited(codec->dev,
 842                                            "%d:Error in %s for %s\n", ret,
 843                                            __func__, kcontrol->id.name);
 844                }
 845                for (wi = 0; wi < wlist->num_widgets; wi++) {
 846                        widget = wlist->widgets[wi];
 847
 848                        widget->value = val;
 849                        widget->dapm->update = NULL;
 850                        snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
 851                }
 852        }
 853
 854        return change;
 855}
 856
 857static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
 858                                        struct snd_kcontrol_new *kctl,
 859                                        struct gb_audio_control *ctl)
 860{
 861        struct soc_enum *gbe;
 862        struct gb_audio_enumerated *gb_enum;
 863        int i;
 864
 865        gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
 866        if (!gbe)
 867                return -ENOMEM;
 868
 869        gb_enum = &ctl->info.value.enumerated;
 870
 871        /* since count=1, and reg is dummy */
 872        gbe->max = le32_to_cpu(gb_enum->items);
 873        gbe->texts = gb_generate_enum_strings(gb, gb_enum);
 874
 875        /* debug enum info */
 876        dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->max,
 877                 le16_to_cpu(gb_enum->names_length));
 878        for (i = 0; i < gbe->max; i++)
 879                dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
 880
 881        *kctl = (struct snd_kcontrol_new)
 882                SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
 883                                  gbcodec_enum_dapm_ctl_put);
 884        return 0;
 885}
 886
 887static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
 888                                             struct snd_kcontrol_new *kctl,
 889                                             struct gb_audio_control *ctl)
 890{
 891        struct gbaudio_ctl_pvt *ctldata;
 892
 893        ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
 894                               GFP_KERNEL);
 895        if (!ctldata)
 896                return -ENOMEM;
 897        ctldata->ctl_id = ctl->id;
 898        ctldata->data_cport = le16_to_cpu(ctl->data_cport);
 899        ctldata->access = ctl->access;
 900        ctldata->vcount = ctl->count_values;
 901        ctldata->info = &ctl->info;
 902        *kctl = (struct snd_kcontrol_new)
 903                SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
 904
 905        return 0;
 906}
 907
 908static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
 909                                             struct snd_kcontrol_new *kctl,
 910                                             struct gb_audio_control *ctl)
 911{
 912        int ret;
 913
 914        switch (ctl->iface) {
 915        case SNDRV_CTL_ELEM_IFACE_MIXER:
 916                switch (ctl->info.type) {
 917                case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
 918                        ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
 919                        break;
 920                default:
 921                        ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
 922                        break;
 923                }
 924                break;
 925        default:
 926                return -EINVAL;
 927
 928        }
 929
 930        dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
 931                ctl->id, ret);
 932        return ret;
 933}
 934
 935static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
 936                                struct snd_kcontrol *kcontrol, int event)
 937{
 938        int wid;
 939        int ret;
 940        struct snd_soc_codec *codec = w->codec;
 941        struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
 942        struct gbaudio_module_info *module;
 943        struct gb_bundle *bundle;
 944
 945        dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
 946
 947        /* Find relevant module */
 948        module = find_gb_module(gbcodec, w->name);
 949        if (!module)
 950                return -EINVAL;
 951
 952        /* map name to widget id */
 953        wid = gbaudio_map_widgetname(module, w->name);
 954        if (wid < 0) {
 955                dev_err(codec->dev, "Invalid widget name:%s\n", w->name);
 956                return -EINVAL;
 957        }
 958
 959        bundle = to_gb_bundle(module->dev);
 960
 961        ret = gb_pm_runtime_get_sync(bundle);
 962        if (ret)
 963                return ret;
 964
 965        switch (event) {
 966        case SND_SOC_DAPM_PRE_PMU:
 967                ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
 968                if (!ret)
 969                        ret = gbaudio_module_update(gbcodec, w, module, 1);
 970                break;
 971        case SND_SOC_DAPM_POST_PMD:
 972                ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
 973                if (!ret)
 974                        ret = gbaudio_module_update(gbcodec, w, module, 0);
 975                break;
 976        }
 977        if (ret)
 978                dev_err_ratelimited(codec->dev,
 979                                    "%d: widget, event:%d failed:%d\n", wid,
 980                                    event, ret);
 981
 982        gb_pm_runtime_put_autosuspend(bundle);
 983
 984        return ret;
 985}
 986
 987static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
 988                                      struct snd_soc_dapm_widget *dw,
 989                                      struct gb_audio_widget *w, int *w_size)
 990{
 991        int i, ret, csize;
 992        struct snd_kcontrol_new *widget_kctls;
 993        struct gb_audio_control *curr;
 994        struct gbaudio_control *control, *_control;
 995        size_t size;
 996        char temp_name[NAME_SIZE];
 997
 998        ret = gbaudio_validate_kcontrol_count(w);
 999        if (ret) {
1000                dev_err(module->dev, "Inavlid kcontrol count=%d for %s\n",
1001                        w->ncontrols, w->name);
1002                return ret;
1003        }
1004
1005        /* allocate memory for kcontrol */
1006        if (w->ncontrols) {
1007                size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1008                widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1009                if (!widget_kctls)
1010                        return -ENOMEM;
1011        }
1012
1013        *w_size = sizeof(struct gb_audio_widget);
1014
1015        /* create relevant kcontrols */
1016        curr = w->ctl;
1017        for (i = 0; i < w->ncontrols; i++) {
1018                ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1019                                                   curr);
1020                if (ret) {
1021                        dev_err(module->dev,
1022                                "%s:%d type widget_ctl not supported\n",
1023                                curr->name, curr->iface);
1024                        goto error;
1025                }
1026                control = devm_kzalloc(module->dev,
1027                                       sizeof(struct gbaudio_control),
1028                                       GFP_KERNEL);
1029                if (!control) {
1030                        ret = -ENOMEM;
1031                        goto error;
1032                }
1033                control->id = curr->id;
1034                control->name = curr->name;
1035                control->wname = w->name;
1036
1037                if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1038                        struct gb_audio_enumerated *gbenum =
1039                                &curr->info.value.enumerated;
1040
1041                        csize = offsetof(struct gb_audio_control, info);
1042                        csize += offsetof(struct gb_audio_ctl_elem_info, value);
1043                        csize += offsetof(struct gb_audio_enumerated, names);
1044                        csize += le16_to_cpu(gbenum->names_length);
1045                        control->texts = (const char * const *)
1046                                gb_generate_enum_strings(module, gbenum);
1047                        control->items = le32_to_cpu(gbenum->items);
1048                } else {
1049                        csize = sizeof(struct gb_audio_control);
1050                }
1051
1052                *w_size += csize;
1053                curr = (void *)curr + csize;
1054                list_add(&control->list, &module->widget_ctl_list);
1055                dev_dbg(module->dev, "%s: control of type %d created\n",
1056                        widget_kctls[i].name, widget_kctls[i].iface);
1057        }
1058
1059        /* Prefix dev_id to widget control_name */
1060        strlcpy(temp_name, w->name, NAME_SIZE);
1061        snprintf(w->name, NAME_SIZE, "GB %d %s", module->dev_id, temp_name);
1062
1063        switch (w->type) {
1064        case snd_soc_dapm_spk:
1065                *dw = (struct snd_soc_dapm_widget)
1066                        SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1067                module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1068                break;
1069        case snd_soc_dapm_hp:
1070                *dw = (struct snd_soc_dapm_widget)
1071                        SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1072                module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1073                                        | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1074                module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1075                break;
1076        case snd_soc_dapm_mic:
1077                *dw = (struct snd_soc_dapm_widget)
1078                        SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1079                module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1080                break;
1081        case snd_soc_dapm_output:
1082                *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1083                break;
1084        case snd_soc_dapm_input:
1085                *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1086                break;
1087        case snd_soc_dapm_switch:
1088                *dw = (struct snd_soc_dapm_widget)
1089                        SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1090                                            widget_kctls, gbaudio_widget_event,
1091                                            SND_SOC_DAPM_PRE_PMU |
1092                                            SND_SOC_DAPM_POST_PMD);
1093                break;
1094        case snd_soc_dapm_pga:
1095                *dw = (struct snd_soc_dapm_widget)
1096                        SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1097                                           gbaudio_widget_event,
1098                                           SND_SOC_DAPM_PRE_PMU |
1099                                           SND_SOC_DAPM_POST_PMD);
1100                break;
1101        case snd_soc_dapm_mixer:
1102                *dw = (struct snd_soc_dapm_widget)
1103                        SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1104                                           0, gbaudio_widget_event,
1105                                           SND_SOC_DAPM_PRE_PMU |
1106                                           SND_SOC_DAPM_POST_PMD);
1107                break;
1108        case snd_soc_dapm_mux:
1109                *dw = (struct snd_soc_dapm_widget)
1110                        SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1111                                         widget_kctls, gbaudio_widget_event,
1112                                         SND_SOC_DAPM_PRE_PMU |
1113                                         SND_SOC_DAPM_POST_PMD);
1114                break;
1115        case snd_soc_dapm_aif_in:
1116                *dw = (struct snd_soc_dapm_widget)
1117                        SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1118                                              SND_SOC_NOPM,
1119                                              0, 0, gbaudio_widget_event,
1120                                              SND_SOC_DAPM_PRE_PMU |
1121                                              SND_SOC_DAPM_POST_PMD);
1122                break;
1123        case snd_soc_dapm_aif_out:
1124                *dw = (struct snd_soc_dapm_widget)
1125                        SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1126                                               SND_SOC_NOPM,
1127                                               0, 0, gbaudio_widget_event,
1128                                               SND_SOC_DAPM_PRE_PMU |
1129                                               SND_SOC_DAPM_POST_PMD);
1130                break;
1131        default:
1132                ret = -EINVAL;
1133                goto error;
1134        }
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                strlcpy(temp_name, curr->name, NAME_SIZE);
1182                snprintf(curr->name, NAME_SIZE, "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                        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