Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus audio driver
0004  * Copyright 2015-2016 Google Inc.
0005  * Copyright 2015-2016 Linaro Ltd.
0006  */
0007 
0008 #include <linux/greybus.h>
0009 #include "audio_codec.h"
0010 
0011 #define GBAUDIO_INVALID_ID  0xFF
0012 
0013 /* mixer control */
0014 struct gb_mixer_control {
0015     int min, max;
0016     unsigned int reg, rreg, shift, rshift, invert;
0017 };
0018 
0019 struct gbaudio_ctl_pvt {
0020     unsigned int ctl_id;
0021     unsigned int data_cport;
0022     unsigned int access;
0023     unsigned int vcount;
0024     struct gb_audio_ctl_elem_info *info;
0025 };
0026 
0027 static struct gbaudio_module_info *find_gb_module(
0028                     struct gbaudio_codec_info *codec,
0029                     char const *name)
0030 {
0031     int dev_id;
0032     char begin[NAME_SIZE];
0033     struct gbaudio_module_info *module;
0034 
0035     if (!name)
0036         return NULL;
0037 
0038     if (sscanf(name, "%s %d", begin, &dev_id) != 2)
0039         return NULL;
0040 
0041     dev_dbg(codec->dev, "%s:Find module#%d\n", __func__, dev_id);
0042 
0043     mutex_lock(&codec->lock);
0044     list_for_each_entry(module, &codec->module_list, list) {
0045         if (module->dev_id == dev_id) {
0046             mutex_unlock(&codec->lock);
0047             return module;
0048         }
0049     }
0050     mutex_unlock(&codec->lock);
0051     dev_warn(codec->dev, "%s: module#%d missing in codec list\n", name,
0052          dev_id);
0053     return NULL;
0054 }
0055 
0056 static const char *gbaudio_map_controlid(struct gbaudio_module_info *module,
0057                      __u8 control_id, __u8 index)
0058 {
0059     struct gbaudio_control *control;
0060 
0061     if (control_id == GBAUDIO_INVALID_ID)
0062         return NULL;
0063 
0064     list_for_each_entry(control, &module->ctl_list, list) {
0065         if (control->id == control_id) {
0066             if (index == GBAUDIO_INVALID_ID)
0067                 return control->name;
0068             if (index >= control->items)
0069                 return NULL;
0070             return control->texts[index];
0071         }
0072     }
0073     list_for_each_entry(control, &module->widget_ctl_list, list) {
0074         if (control->id == control_id) {
0075             if (index == GBAUDIO_INVALID_ID)
0076                 return control->name;
0077             if (index >= control->items)
0078                 return NULL;
0079             return control->texts[index];
0080         }
0081     }
0082     return NULL;
0083 }
0084 
0085 static int gbaudio_map_controlname(struct gbaudio_module_info *module,
0086                    const char *name)
0087 {
0088     struct gbaudio_control *control;
0089 
0090     list_for_each_entry(control, &module->ctl_list, list) {
0091         if (!strncmp(control->name, name, NAME_SIZE))
0092             return control->id;
0093     }
0094 
0095     dev_warn(module->dev, "%s: missing in modules controls list\n", name);
0096 
0097     return -EINVAL;
0098 }
0099 
0100 static int gbaudio_map_wcontrolname(struct gbaudio_module_info *module,
0101                     const char *name)
0102 {
0103     struct gbaudio_control *control;
0104 
0105     list_for_each_entry(control, &module->widget_ctl_list, list) {
0106         if (!strncmp(control->wname, name, NAME_SIZE))
0107             return control->id;
0108     }
0109     dev_warn(module->dev, "%s: missing in modules controls list\n", name);
0110 
0111     return -EINVAL;
0112 }
0113 
0114 static int gbaudio_map_widgetname(struct gbaudio_module_info *module,
0115                   const char *name)
0116 {
0117     struct gbaudio_widget *widget;
0118 
0119     list_for_each_entry(widget, &module->widget_list, list) {
0120         if (!strncmp(widget->name, name, NAME_SIZE))
0121             return widget->id;
0122     }
0123     dev_warn(module->dev, "%s: missing in modules widgets list\n", name);
0124 
0125     return -EINVAL;
0126 }
0127 
0128 static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module,
0129                     __u8 widget_id)
0130 {
0131     struct gbaudio_widget *widget;
0132 
0133     list_for_each_entry(widget, &module->widget_list, list) {
0134         if (widget->id == widget_id)
0135             return widget->name;
0136     }
0137     return NULL;
0138 }
0139 
0140 static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
0141                          struct gb_audio_enumerated *gbenum)
0142 {
0143     const char **strings;
0144     int i;
0145     unsigned int items;
0146     __u8 *data;
0147 
0148     items = le32_to_cpu(gbenum->items);
0149     strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL);
0150     if (!strings)
0151         return NULL;
0152 
0153     data = gbenum->names;
0154 
0155     for (i = 0; i < items; i++) {
0156         strings[i] = (const char *)data;
0157         while (*data != '\0')
0158             data++;
0159         data++;
0160     }
0161 
0162     return strings;
0163 }
0164 
0165 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
0166                   struct snd_ctl_elem_info *uinfo)
0167 {
0168     unsigned int max;
0169     const char *name;
0170     struct gbaudio_ctl_pvt *data;
0171     struct gb_audio_ctl_elem_info *info;
0172     struct gbaudio_module_info *module;
0173     struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
0174     struct gbaudio_codec_info *gbcodec = snd_soc_component_get_drvdata(comp);
0175 
0176     dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
0177     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0178     info = (struct gb_audio_ctl_elem_info *)data->info;
0179 
0180     if (!info) {
0181         dev_err(comp->dev, "NULL info for %s\n", uinfo->id.name);
0182         return -EINVAL;
0183     }
0184 
0185     /* update uinfo */
0186     uinfo->access = data->access;
0187     uinfo->count = data->vcount;
0188     uinfo->type = (__force snd_ctl_elem_type_t)info->type;
0189 
0190     switch (info->type) {
0191     case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
0192     case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
0193         uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
0194         uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
0195         break;
0196     case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
0197         max = le32_to_cpu(info->value.enumerated.items);
0198         uinfo->value.enumerated.items = max;
0199         if (uinfo->value.enumerated.item > max - 1)
0200             uinfo->value.enumerated.item = max - 1;
0201         module = find_gb_module(gbcodec, kcontrol->id.name);
0202         if (!module)
0203             return -EINVAL;
0204         name = gbaudio_map_controlid(module, data->ctl_id,
0205                          uinfo->value.enumerated.item);
0206         strscpy(uinfo->value.enumerated.name, name, sizeof(uinfo->value.enumerated.name));
0207         break;
0208     default:
0209         dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
0210             info->type, kcontrol->id.name);
0211         break;
0212     }
0213     return 0;
0214 }
0215 
0216 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
0217                  struct snd_ctl_elem_value *ucontrol)
0218 {
0219     int ret;
0220     struct gb_audio_ctl_elem_info *info;
0221     struct gbaudio_ctl_pvt *data;
0222     struct gb_audio_ctl_elem_value gbvalue;
0223     struct gbaudio_module_info *module;
0224     struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
0225     struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
0226     struct gb_bundle *bundle;
0227 
0228     dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
0229     module = find_gb_module(gb, kcontrol->id.name);
0230     if (!module)
0231         return -EINVAL;
0232 
0233     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0234     info = (struct gb_audio_ctl_elem_info *)data->info;
0235     bundle = to_gb_bundle(module->dev);
0236 
0237     ret = gb_pm_runtime_get_sync(bundle);
0238     if (ret)
0239         return ret;
0240 
0241     ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
0242                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0243 
0244     gb_pm_runtime_put_autosuspend(bundle);
0245 
0246     if (ret) {
0247         dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
0248                     __func__, kcontrol->id.name);
0249         return ret;
0250     }
0251 
0252     /* update ucontrol */
0253     switch (info->type) {
0254     case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
0255     case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
0256         ucontrol->value.integer.value[0] =
0257             le32_to_cpu(gbvalue.value.integer_value[0]);
0258         if (data->vcount == 2)
0259             ucontrol->value.integer.value[1] =
0260                 le32_to_cpu(gbvalue.value.integer_value[1]);
0261         break;
0262     case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
0263         ucontrol->value.enumerated.item[0] =
0264             le32_to_cpu(gbvalue.value.enumerated_item[0]);
0265         if (data->vcount == 2)
0266             ucontrol->value.enumerated.item[1] =
0267                 le32_to_cpu(gbvalue.value.enumerated_item[1]);
0268         break;
0269     default:
0270         dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
0271             info->type, kcontrol->id.name);
0272         ret = -EINVAL;
0273         break;
0274     }
0275     return ret;
0276 }
0277 
0278 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
0279                  struct snd_ctl_elem_value *ucontrol)
0280 {
0281     int ret = 0;
0282     struct gb_audio_ctl_elem_info *info;
0283     struct gbaudio_ctl_pvt *data;
0284     struct gb_audio_ctl_elem_value gbvalue;
0285     struct gbaudio_module_info *module;
0286     struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
0287     struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
0288     struct gb_bundle *bundle;
0289 
0290     dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
0291     module = find_gb_module(gb, kcontrol->id.name);
0292     if (!module)
0293         return -EINVAL;
0294 
0295     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0296     info = (struct gb_audio_ctl_elem_info *)data->info;
0297     bundle = to_gb_bundle(module->dev);
0298 
0299     /* update ucontrol */
0300     switch (info->type) {
0301     case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
0302     case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
0303         gbvalue.value.integer_value[0] =
0304             cpu_to_le32(ucontrol->value.integer.value[0]);
0305         if (data->vcount == 2)
0306             gbvalue.value.integer_value[1] =
0307                 cpu_to_le32(ucontrol->value.integer.value[1]);
0308         break;
0309     case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
0310         gbvalue.value.enumerated_item[0] =
0311             cpu_to_le32(ucontrol->value.enumerated.item[0]);
0312         if (data->vcount == 2)
0313             gbvalue.value.enumerated_item[1] =
0314                 cpu_to_le32(ucontrol->value.enumerated.item[1]);
0315         break;
0316     default:
0317         dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
0318             info->type, kcontrol->id.name);
0319         ret = -EINVAL;
0320         break;
0321     }
0322 
0323     if (ret)
0324         return ret;
0325 
0326     ret = gb_pm_runtime_get_sync(bundle);
0327     if (ret)
0328         return ret;
0329 
0330     ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
0331                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0332 
0333     gb_pm_runtime_put_autosuspend(bundle);
0334 
0335     if (ret) {
0336         dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
0337                     __func__, kcontrol->id.name);
0338     }
0339 
0340     return ret;
0341 }
0342 
0343 #define SOC_MIXER_GB(xname, kcount, data) \
0344 {   .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
0345     .count = kcount, .info = gbcodec_mixer_ctl_info, \
0346     .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
0347     .private_value = (unsigned long)data }
0348 
0349 /*
0350  * although below callback functions seems redundant to above functions.
0351  * same are kept to allow provision for different handling in case
0352  * of DAPM related sequencing, etc.
0353  */
0354 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
0355                        struct snd_ctl_elem_info *uinfo)
0356 {
0357     int platform_max, platform_min;
0358     struct gbaudio_ctl_pvt *data;
0359     struct gb_audio_ctl_elem_info *info;
0360 
0361     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0362     info = (struct gb_audio_ctl_elem_info *)data->info;
0363 
0364     /* update uinfo */
0365     platform_max = le32_to_cpu(info->value.integer.max);
0366     platform_min = le32_to_cpu(info->value.integer.min);
0367 
0368     if (platform_max == 1 &&
0369         !strnstr(kcontrol->id.name, " Volume", sizeof(kcontrol->id.name)))
0370         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
0371     else
0372         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0373 
0374     uinfo->count = data->vcount;
0375     uinfo->value.integer.min = platform_min;
0376     uinfo->value.integer.max = platform_max;
0377 
0378     return 0;
0379 }
0380 
0381 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
0382                       struct snd_ctl_elem_value *ucontrol)
0383 {
0384     int ret;
0385     struct gbaudio_ctl_pvt *data;
0386     struct gb_audio_ctl_elem_value gbvalue;
0387     struct gbaudio_module_info *module;
0388     struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
0389     struct snd_soc_dapm_widget *widget = wlist->widgets[0];
0390     struct device *codec_dev = widget->dapm->dev;
0391     struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
0392     struct gb_bundle *bundle;
0393 
0394     dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
0395     module = find_gb_module(gb, kcontrol->id.name);
0396     if (!module)
0397         return -EINVAL;
0398 
0399     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0400     bundle = to_gb_bundle(module->dev);
0401 
0402     if (data->vcount == 2)
0403         dev_warn(widget->dapm->dev,
0404              "GB: Control '%s' is stereo, which is not supported\n",
0405              kcontrol->id.name);
0406 
0407     ret = gb_pm_runtime_get_sync(bundle);
0408     if (ret)
0409         return ret;
0410 
0411     ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
0412                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0413 
0414     gb_pm_runtime_put_autosuspend(bundle);
0415 
0416     if (ret) {
0417         dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
0418                     __func__, kcontrol->id.name);
0419         return ret;
0420     }
0421     /* update ucontrol */
0422     ucontrol->value.integer.value[0] =
0423         le32_to_cpu(gbvalue.value.integer_value[0]);
0424 
0425     return ret;
0426 }
0427 
0428 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
0429                       struct snd_ctl_elem_value *ucontrol)
0430 {
0431     int ret, wi, max, connect;
0432     unsigned int mask, val;
0433     struct gb_audio_ctl_elem_info *info;
0434     struct gbaudio_ctl_pvt *data;
0435     struct gb_audio_ctl_elem_value gbvalue;
0436     struct gbaudio_module_info *module;
0437     struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
0438     struct snd_soc_dapm_widget *widget = wlist->widgets[0];
0439     struct device *codec_dev = widget->dapm->dev;
0440     struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
0441     struct gb_bundle *bundle;
0442 
0443     dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
0444     module = find_gb_module(gb, kcontrol->id.name);
0445     if (!module)
0446         return -EINVAL;
0447 
0448     data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
0449     info = (struct gb_audio_ctl_elem_info *)data->info;
0450     bundle = to_gb_bundle(module->dev);
0451 
0452     if (data->vcount == 2)
0453         dev_warn(widget->dapm->dev,
0454              "GB: Control '%s' is stereo, which is not supported\n",
0455              kcontrol->id.name);
0456 
0457     max = le32_to_cpu(info->value.integer.max);
0458     mask = (1 << fls(max)) - 1;
0459     val = ucontrol->value.integer.value[0] & mask;
0460     connect = !!val;
0461 
0462     ret = gb_pm_runtime_get_sync(bundle);
0463     if (ret)
0464         return ret;
0465 
0466     ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
0467                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0468     if (ret)
0469         goto exit;
0470 
0471     /* update ucontrol */
0472     if (le32_to_cpu(gbvalue.value.integer_value[0]) != val) {
0473         for (wi = 0; wi < wlist->num_widgets; wi++) {
0474             widget = wlist->widgets[wi];
0475             snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol,
0476                             connect, NULL);
0477         }
0478         gbvalue.value.integer_value[0] =
0479             cpu_to_le32(ucontrol->value.integer.value[0]);
0480 
0481         ret = gb_audio_gb_set_control(module->mgmt_connection,
0482                           data->ctl_id,
0483                           GB_AUDIO_INVALID_INDEX, &gbvalue);
0484     }
0485 
0486 exit:
0487     gb_pm_runtime_put_autosuspend(bundle);
0488     if (ret)
0489         dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
0490                     __func__, kcontrol->id.name);
0491     return ret;
0492 }
0493 
0494 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
0495 {   .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
0496     .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
0497     .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
0498     .private_value = (unsigned long)data}
0499 
0500 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
0501                  struct snd_kcontrol *k, int event)
0502 {
0503     /* Ensure GB speaker is connected */
0504 
0505     return 0;
0506 }
0507 
0508 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
0509                 struct snd_kcontrol *k, int event)
0510 {
0511     /* Ensure GB module supports jack slot */
0512 
0513     return 0;
0514 }
0515 
0516 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
0517                  struct snd_kcontrol *k, int event)
0518 {
0519     /* Ensure GB module supports jack slot */
0520 
0521     return 0;
0522 }
0523 
0524 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
0525 {
0526     int ret = 0;
0527 
0528     switch (w->type) {
0529     case snd_soc_dapm_spk:
0530     case snd_soc_dapm_hp:
0531     case snd_soc_dapm_mic:
0532     case snd_soc_dapm_output:
0533     case snd_soc_dapm_input:
0534         if (w->ncontrols)
0535             ret = -EINVAL;
0536         break;
0537     case snd_soc_dapm_switch:
0538     case snd_soc_dapm_mux:
0539         if (w->ncontrols != 1)
0540             ret = -EINVAL;
0541         break;
0542     default:
0543         break;
0544     }
0545 
0546     return ret;
0547 }
0548 
0549 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
0550                 struct snd_ctl_elem_value *ucontrol)
0551 {
0552     int ret, ctl_id;
0553     struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
0554     struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
0555     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0556     struct gb_audio_ctl_elem_value gbvalue;
0557     struct gbaudio_module_info *module;
0558     struct gb_bundle *bundle;
0559 
0560     module = find_gb_module(gb, kcontrol->id.name);
0561     if (!module)
0562         return -EINVAL;
0563 
0564     ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
0565     if (ctl_id < 0)
0566         return -EINVAL;
0567 
0568     bundle = to_gb_bundle(module->dev);
0569 
0570     ret = gb_pm_runtime_get_sync(bundle);
0571     if (ret)
0572         return ret;
0573 
0574     ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
0575                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0576 
0577     gb_pm_runtime_put_autosuspend(bundle);
0578 
0579     if (ret) {
0580         dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
0581                     __func__, kcontrol->id.name);
0582         return ret;
0583     }
0584 
0585     ucontrol->value.enumerated.item[0] =
0586         le32_to_cpu(gbvalue.value.enumerated_item[0]);
0587     if (e->shift_l != e->shift_r)
0588         ucontrol->value.enumerated.item[1] =
0589             le32_to_cpu(gbvalue.value.enumerated_item[1]);
0590 
0591     return 0;
0592 }
0593 
0594 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
0595                 struct snd_ctl_elem_value *ucontrol)
0596 {
0597     int ret, ctl_id;
0598     struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
0599     struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
0600     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0601     struct gb_audio_ctl_elem_value gbvalue;
0602     struct gbaudio_module_info *module;
0603     struct gb_bundle *bundle;
0604 
0605     module = find_gb_module(gb, kcontrol->id.name);
0606     if (!module)
0607         return -EINVAL;
0608 
0609     ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
0610     if (ctl_id < 0)
0611         return -EINVAL;
0612 
0613     if (ucontrol->value.enumerated.item[0] > e->items - 1)
0614         return -EINVAL;
0615     gbvalue.value.enumerated_item[0] =
0616         cpu_to_le32(ucontrol->value.enumerated.item[0]);
0617 
0618     if (e->shift_l != e->shift_r) {
0619         if (ucontrol->value.enumerated.item[1] > e->items - 1)
0620             return -EINVAL;
0621         gbvalue.value.enumerated_item[1] =
0622             cpu_to_le32(ucontrol->value.enumerated.item[1]);
0623     }
0624 
0625     bundle = to_gb_bundle(module->dev);
0626 
0627     ret = gb_pm_runtime_get_sync(bundle);
0628     if (ret)
0629         return ret;
0630 
0631     ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
0632                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0633 
0634     gb_pm_runtime_put_autosuspend(bundle);
0635 
0636     if (ret) {
0637         dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n",
0638                     ret, __func__, kcontrol->id.name);
0639     }
0640 
0641     return ret;
0642 }
0643 
0644 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
0645                      struct snd_kcontrol_new *kctl,
0646                      struct gb_audio_control *ctl)
0647 {
0648     struct soc_enum *gbe;
0649     struct gb_audio_enumerated *gb_enum;
0650     int i;
0651 
0652     gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
0653     if (!gbe)
0654         return -ENOMEM;
0655 
0656     gb_enum = &ctl->info.value.enumerated;
0657 
0658     /* since count=1, and reg is dummy */
0659     gbe->items = le32_to_cpu(gb_enum->items);
0660     gbe->texts = gb_generate_enum_strings(gb, gb_enum);
0661     if (!gbe->texts)
0662         return -ENOMEM;
0663 
0664     /* debug enum info */
0665     dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
0666         le16_to_cpu(gb_enum->names_length));
0667     for (i = 0; i < gbe->items; i++)
0668         dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
0669 
0670     *kctl = (struct snd_kcontrol_new)
0671         SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
0672                  gbcodec_enum_ctl_put);
0673     return 0;
0674 }
0675 
0676 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
0677                     struct snd_kcontrol_new *kctl,
0678                     struct gb_audio_control *ctl)
0679 {
0680     int ret = 0;
0681     struct gbaudio_ctl_pvt *ctldata;
0682 
0683     switch (ctl->iface) {
0684     case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
0685         switch (ctl->info.type) {
0686         case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
0687             ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
0688             break;
0689         default:
0690             ctldata = devm_kzalloc(gb->dev,
0691                            sizeof(struct gbaudio_ctl_pvt),
0692                            GFP_KERNEL);
0693             if (!ctldata)
0694                 return -ENOMEM;
0695             ctldata->ctl_id = ctl->id;
0696             ctldata->data_cport = le16_to_cpu(ctl->data_cport);
0697             ctldata->access = le32_to_cpu(ctl->access);
0698             ctldata->vcount = ctl->count_values;
0699             ctldata->info = &ctl->info;
0700             *kctl = (struct snd_kcontrol_new)
0701                 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
0702             ctldata = NULL;
0703             break;
0704         }
0705         break;
0706     default:
0707         return -EINVAL;
0708     }
0709 
0710     dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
0711     return ret;
0712 }
0713 
0714 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
0715                      struct snd_ctl_elem_value *ucontrol)
0716 {
0717     int ret, ctl_id;
0718     struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
0719     struct snd_soc_dapm_widget *widget = wlist->widgets[0];
0720     struct gbaudio_module_info *module;
0721     struct gb_audio_ctl_elem_value gbvalue;
0722     struct device *codec_dev = widget->dapm->dev;
0723     struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
0724     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0725     struct gb_bundle *bundle;
0726 
0727     module = find_gb_module(gb, kcontrol->id.name);
0728     if (!module)
0729         return -EINVAL;
0730 
0731     ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
0732     if (ctl_id < 0)
0733         return -EINVAL;
0734 
0735     bundle = to_gb_bundle(module->dev);
0736 
0737     ret = gb_pm_runtime_get_sync(bundle);
0738     if (ret)
0739         return ret;
0740 
0741     ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
0742                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0743 
0744     gb_pm_runtime_put_autosuspend(bundle);
0745 
0746     if (ret) {
0747         dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
0748                     __func__, kcontrol->id.name);
0749         return ret;
0750     }
0751 
0752     ucontrol->value.enumerated.item[0] = le32_to_cpu(gbvalue.value.enumerated_item[0]);
0753     if (e->shift_l != e->shift_r)
0754         ucontrol->value.enumerated.item[1] =
0755             le32_to_cpu(gbvalue.value.enumerated_item[1]);
0756 
0757     return 0;
0758 }
0759 
0760 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
0761                      struct snd_ctl_elem_value *ucontrol)
0762 {
0763     int ret, wi, ctl_id;
0764     unsigned int val, mux, change;
0765     unsigned int mask;
0766     struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
0767     struct snd_soc_dapm_widget *widget = wlist->widgets[0];
0768     struct gb_audio_ctl_elem_value gbvalue;
0769     struct gbaudio_module_info *module;
0770     struct device *codec_dev = widget->dapm->dev;
0771     struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
0772     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0773     struct gb_bundle *bundle;
0774 
0775     if (ucontrol->value.enumerated.item[0] > e->items - 1)
0776         return -EINVAL;
0777 
0778     module = find_gb_module(gb, kcontrol->id.name);
0779     if (!module)
0780         return -EINVAL;
0781 
0782     ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
0783     if (ctl_id < 0)
0784         return -EINVAL;
0785 
0786     change = 0;
0787     bundle = to_gb_bundle(module->dev);
0788 
0789     ret = gb_pm_runtime_get_sync(bundle);
0790     if (ret)
0791         return ret;
0792 
0793     ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
0794                       GB_AUDIO_INVALID_INDEX, &gbvalue);
0795 
0796     gb_pm_runtime_put_autosuspend(bundle);
0797 
0798     if (ret) {
0799         dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
0800                     __func__, kcontrol->id.name);
0801         return ret;
0802     }
0803 
0804     mux = ucontrol->value.enumerated.item[0];
0805     val = mux << e->shift_l;
0806     mask = e->mask << e->shift_l;
0807 
0808     if (le32_to_cpu(gbvalue.value.enumerated_item[0]) !=
0809         ucontrol->value.enumerated.item[0]) {
0810         change = 1;
0811         gbvalue.value.enumerated_item[0] =
0812             cpu_to_le32(ucontrol->value.enumerated.item[0]);
0813     }
0814 
0815     if (e->shift_l != e->shift_r) {
0816         if (ucontrol->value.enumerated.item[1] > e->items - 1)
0817             return -EINVAL;
0818         val |= ucontrol->value.enumerated.item[1] << e->shift_r;
0819         mask |= e->mask << e->shift_r;
0820         if (le32_to_cpu(gbvalue.value.enumerated_item[1]) !=
0821             ucontrol->value.enumerated.item[1]) {
0822             change = 1;
0823             gbvalue.value.enumerated_item[1] =
0824                 cpu_to_le32(ucontrol->value.enumerated.item[1]);
0825         }
0826     }
0827 
0828     if (change) {
0829         ret = gb_pm_runtime_get_sync(bundle);
0830         if (ret)
0831             return ret;
0832 
0833         ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
0834                           GB_AUDIO_INVALID_INDEX, &gbvalue);
0835 
0836         gb_pm_runtime_put_autosuspend(bundle);
0837 
0838         if (ret) {
0839             dev_err_ratelimited(codec_dev,
0840                         "%d:Error in %s for %s\n", ret,
0841                         __func__, kcontrol->id.name);
0842         }
0843         for (wi = 0; wi < wlist->num_widgets; wi++) {
0844             widget = wlist->widgets[wi];
0845             snd_soc_dapm_mux_update_power(widget->dapm, kcontrol,
0846                               val, e, NULL);
0847         }
0848     }
0849 
0850     return change;
0851 }
0852 
0853 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
0854                     struct snd_kcontrol_new *kctl,
0855                     struct gb_audio_control *ctl)
0856 {
0857     struct soc_enum *gbe;
0858     struct gb_audio_enumerated *gb_enum;
0859     int i;
0860 
0861     gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
0862     if (!gbe)
0863         return -ENOMEM;
0864 
0865     gb_enum = &ctl->info.value.enumerated;
0866 
0867     /* since count=1, and reg is dummy */
0868     gbe->items = le32_to_cpu(gb_enum->items);
0869     gbe->texts = gb_generate_enum_strings(gb, gb_enum);
0870     if (!gbe->texts)
0871         return -ENOMEM;
0872 
0873     /* debug enum info */
0874     dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
0875         le16_to_cpu(gb_enum->names_length));
0876     for (i = 0; i < gbe->items; i++)
0877         dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
0878 
0879     *kctl = (struct snd_kcontrol_new)
0880         SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
0881                   gbcodec_enum_dapm_ctl_put);
0882     return 0;
0883 }
0884 
0885 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
0886                      struct snd_kcontrol_new *kctl,
0887                      struct gb_audio_control *ctl)
0888 {
0889     struct gbaudio_ctl_pvt *ctldata;
0890 
0891     ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
0892                    GFP_KERNEL);
0893     if (!ctldata)
0894         return -ENOMEM;
0895     ctldata->ctl_id = ctl->id;
0896     ctldata->data_cport = le16_to_cpu(ctl->data_cport);
0897     ctldata->access = le32_to_cpu(ctl->access);
0898     ctldata->vcount = ctl->count_values;
0899     ctldata->info = &ctl->info;
0900     *kctl = (struct snd_kcontrol_new)
0901         SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
0902 
0903     return 0;
0904 }
0905 
0906 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
0907                     struct snd_kcontrol_new *kctl,
0908                     struct gb_audio_control *ctl)
0909 {
0910     int ret;
0911 
0912     switch (ctl->iface) {
0913     case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
0914         switch (ctl->info.type) {
0915         case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
0916             ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
0917             break;
0918         default:
0919             ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
0920             break;
0921         }
0922         break;
0923     default:
0924         return -EINVAL;
0925     }
0926 
0927     dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
0928         ctl->id, ret);
0929     return ret;
0930 }
0931 
0932 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
0933                 struct snd_kcontrol *kcontrol, int event)
0934 {
0935     int wid;
0936     int ret;
0937     struct device *codec_dev = w->dapm->dev;
0938     struct gbaudio_codec_info *gbcodec = dev_get_drvdata(codec_dev);
0939     struct gbaudio_module_info *module;
0940     struct gb_bundle *bundle;
0941 
0942     dev_dbg(codec_dev, "%s %s %d\n", __func__, w->name, event);
0943 
0944     /* Find relevant module */
0945     module = find_gb_module(gbcodec, w->name);
0946     if (!module)
0947         return -EINVAL;
0948 
0949     /* map name to widget id */
0950     wid = gbaudio_map_widgetname(module, w->name);
0951     if (wid < 0) {
0952         dev_err(codec_dev, "Invalid widget name:%s\n", w->name);
0953         return -EINVAL;
0954     }
0955 
0956     bundle = to_gb_bundle(module->dev);
0957 
0958     ret = gb_pm_runtime_get_sync(bundle);
0959     if (ret)
0960         return ret;
0961 
0962     switch (event) {
0963     case SND_SOC_DAPM_PRE_PMU:
0964         ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
0965         if (!ret)
0966             ret = gbaudio_module_update(gbcodec, w, module, 1);
0967         break;
0968     case SND_SOC_DAPM_POST_PMD:
0969         ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
0970         if (!ret)
0971             ret = gbaudio_module_update(gbcodec, w, module, 0);
0972         break;
0973     }
0974     if (ret)
0975         dev_err_ratelimited(codec_dev,
0976                     "%d: widget, event:%d failed:%d\n", wid,
0977                     event, ret);
0978 
0979     gb_pm_runtime_put_autosuspend(bundle);
0980 
0981     return ret;
0982 }
0983 
0984 static const struct snd_soc_dapm_widget gbaudio_widgets[] = {
0985     [snd_soc_dapm_spk]  = SND_SOC_DAPM_SPK(NULL, gbcodec_event_spk),
0986     [snd_soc_dapm_hp]   = SND_SOC_DAPM_HP(NULL, gbcodec_event_hp),
0987     [snd_soc_dapm_mic]  = SND_SOC_DAPM_MIC(NULL, gbcodec_event_int_mic),
0988     [snd_soc_dapm_output]   = SND_SOC_DAPM_OUTPUT(NULL),
0989     [snd_soc_dapm_input]    = SND_SOC_DAPM_INPUT(NULL),
0990     [snd_soc_dapm_switch]   = SND_SOC_DAPM_SWITCH_E(NULL, SND_SOC_NOPM,
0991                     0, 0, NULL,
0992                     gbaudio_widget_event,
0993                     SND_SOC_DAPM_PRE_PMU |
0994                     SND_SOC_DAPM_POST_PMD),
0995     [snd_soc_dapm_pga]  = SND_SOC_DAPM_PGA_E(NULL, SND_SOC_NOPM,
0996                     0, 0, NULL, 0,
0997                     gbaudio_widget_event,
0998                     SND_SOC_DAPM_PRE_PMU |
0999                     SND_SOC_DAPM_POST_PMD),
1000     [snd_soc_dapm_mixer]    = SND_SOC_DAPM_MIXER_E(NULL, SND_SOC_NOPM,
1001                     0, 0, NULL, 0,
1002                     gbaudio_widget_event,
1003                     SND_SOC_DAPM_PRE_PMU |
1004                     SND_SOC_DAPM_POST_PMD),
1005     [snd_soc_dapm_mux]  = SND_SOC_DAPM_MUX_E(NULL, SND_SOC_NOPM,
1006                     0, 0, NULL,
1007                     gbaudio_widget_event,
1008                     SND_SOC_DAPM_PRE_PMU |
1009                     SND_SOC_DAPM_POST_PMD),
1010     [snd_soc_dapm_aif_in]   = SND_SOC_DAPM_AIF_IN_E(NULL, NULL, 0,
1011                     SND_SOC_NOPM, 0, 0,
1012                     gbaudio_widget_event,
1013                     SND_SOC_DAPM_PRE_PMU |
1014                     SND_SOC_DAPM_POST_PMD),
1015     [snd_soc_dapm_aif_out]  = SND_SOC_DAPM_AIF_OUT_E(NULL, NULL, 0,
1016                     SND_SOC_NOPM, 0, 0,
1017                     gbaudio_widget_event,
1018                     SND_SOC_DAPM_PRE_PMU |
1019                     SND_SOC_DAPM_POST_PMD),
1020 };
1021 
1022 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
1023                       struct snd_soc_dapm_widget *dw,
1024                       struct gb_audio_widget *w, int *w_size)
1025 {
1026     int i, ret, csize;
1027     struct snd_kcontrol_new *widget_kctls;
1028     struct gb_audio_control *curr;
1029     struct gbaudio_control *control, *_control;
1030     size_t size;
1031     char temp_name[NAME_SIZE];
1032 
1033     ret = gbaudio_validate_kcontrol_count(w);
1034     if (ret) {
1035         dev_err(module->dev, "Invalid kcontrol count=%d for %s\n",
1036             w->ncontrols, w->name);
1037         return ret;
1038     }
1039 
1040     /* allocate memory for kcontrol */
1041     if (w->ncontrols) {
1042         size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1043         widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1044         if (!widget_kctls)
1045             return -ENOMEM;
1046     }
1047 
1048     *w_size = sizeof(struct gb_audio_widget);
1049 
1050     /* create relevant kcontrols */
1051     curr = w->ctl;
1052     for (i = 0; i < w->ncontrols; i++) {
1053         ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1054                            curr);
1055         if (ret) {
1056             dev_err(module->dev,
1057                 "%s:%d type widget_ctl not supported\n",
1058                 curr->name, curr->iface);
1059             goto error;
1060         }
1061         control = devm_kzalloc(module->dev,
1062                        sizeof(struct gbaudio_control),
1063                        GFP_KERNEL);
1064         if (!control) {
1065             ret = -ENOMEM;
1066             goto error;
1067         }
1068         control->id = curr->id;
1069         control->name = curr->name;
1070         control->wname = w->name;
1071 
1072         if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1073             struct gb_audio_enumerated *gbenum =
1074                 &curr->info.value.enumerated;
1075 
1076             csize = offsetof(struct gb_audio_control, info);
1077             csize += offsetof(struct gb_audio_ctl_elem_info, value);
1078             csize += offsetof(struct gb_audio_enumerated, names);
1079             csize += le16_to_cpu(gbenum->names_length);
1080             control->texts = (const char * const *)
1081                 gb_generate_enum_strings(module, gbenum);
1082             if (!control->texts) {
1083                 ret = -ENOMEM;
1084                 goto error;
1085             }
1086             control->items = le32_to_cpu(gbenum->items);
1087         } else {
1088             csize = sizeof(struct gb_audio_control);
1089         }
1090 
1091         *w_size += csize;
1092         curr = (void *)curr + csize;
1093         list_add(&control->list, &module->widget_ctl_list);
1094         dev_dbg(module->dev, "%s: control of type %d created\n",
1095             widget_kctls[i].name, widget_kctls[i].iface);
1096     }
1097 
1098     /* Prefix dev_id to widget control_name */
1099     strscpy(temp_name, w->name, sizeof(temp_name));
1100     snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name);
1101 
1102     switch (w->type) {
1103     case snd_soc_dapm_spk:
1104         *dw = gbaudio_widgets[w->type];
1105         module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1106         break;
1107     case snd_soc_dapm_hp:
1108         *dw = gbaudio_widgets[w->type];
1109         module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1110                     | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1111         module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1112         break;
1113     case snd_soc_dapm_mic:
1114         *dw = gbaudio_widgets[w->type];
1115         module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1116         break;
1117     case snd_soc_dapm_output:
1118     case snd_soc_dapm_input:
1119     case snd_soc_dapm_switch:
1120     case snd_soc_dapm_pga:
1121     case snd_soc_dapm_mixer:
1122     case snd_soc_dapm_mux:
1123         *dw = gbaudio_widgets[w->type];
1124         break;
1125     case snd_soc_dapm_aif_in:
1126     case snd_soc_dapm_aif_out:
1127         *dw = gbaudio_widgets[w->type];
1128         dw->sname = w->sname;
1129         break;
1130     default:
1131         ret = -EINVAL;
1132         goto error;
1133     }
1134     dw->name = w->name;
1135 
1136     dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1137         dw->id);
1138     return 0;
1139 error:
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 
1148 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1149                       struct gb_audio_control *controls)
1150 {
1151     int i, csize, ret;
1152     struct snd_kcontrol_new *dapm_kctls;
1153     struct gb_audio_control *curr;
1154     struct gbaudio_control *control, *_control;
1155     size_t size;
1156     char temp_name[NAME_SIZE];
1157 
1158     size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1159     dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1160     if (!dapm_kctls)
1161         return -ENOMEM;
1162 
1163     curr = controls;
1164     for (i = 0; i < module->num_controls; i++) {
1165         ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1166                            curr);
1167         if (ret) {
1168             dev_err(module->dev, "%s:%d type not supported\n",
1169                 curr->name, curr->iface);
1170             goto error;
1171         }
1172         control = devm_kzalloc(module->dev, sizeof(struct
1173                                gbaudio_control),
1174                       GFP_KERNEL);
1175         if (!control) {
1176             ret = -ENOMEM;
1177             goto error;
1178         }
1179         control->id = curr->id;
1180         /* Prefix dev_id to widget_name */
1181         strscpy(temp_name, curr->name, sizeof(temp_name));
1182         snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id,
1183              temp_name);
1184         control->name = curr->name;
1185         if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1186             struct gb_audio_enumerated *gbenum =
1187                 &curr->info.value.enumerated;
1188 
1189             csize = offsetof(struct gb_audio_control, info);
1190             csize += offsetof(struct gb_audio_ctl_elem_info, value);
1191             csize += offsetof(struct gb_audio_enumerated, names);
1192             csize += le16_to_cpu(gbenum->names_length);
1193             control->texts = (const char * const *)
1194                 gb_generate_enum_strings(module, gbenum);
1195             if (!control->texts) {
1196                 ret = -ENOMEM;
1197                 goto error;
1198             }
1199             control->items = le32_to_cpu(gbenum->items);
1200         } else {
1201             csize = sizeof(struct gb_audio_control);
1202         }
1203 
1204         list_add(&control->list, &module->ctl_list);
1205         dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1206             curr->name, curr->info.type);
1207         curr = (void *)curr + csize;
1208     }
1209     module->controls = dapm_kctls;
1210 
1211     return 0;
1212 error:
1213     list_for_each_entry_safe(control, _control, &module->ctl_list,
1214                  list) {
1215         list_del(&control->list);
1216         devm_kfree(module->dev, control);
1217     }
1218     devm_kfree(module->dev, dapm_kctls);
1219     return ret;
1220 }
1221 
1222 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1223                     struct gb_audio_widget *widgets)
1224 {
1225     int i, ret, w_size;
1226     struct snd_soc_dapm_widget *dapm_widgets;
1227     struct gb_audio_widget *curr;
1228     struct gbaudio_widget *widget, *_widget;
1229     size_t size;
1230 
1231     size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1232     dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1233     if (!dapm_widgets)
1234         return -ENOMEM;
1235 
1236     curr = widgets;
1237     for (i = 0; i < module->num_dapm_widgets; i++) {
1238         ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1239                          curr, &w_size);
1240         if (ret) {
1241             dev_err(module->dev, "%s:%d type not supported\n",
1242                 curr->name, curr->type);
1243             goto error;
1244         }
1245         widget = devm_kzalloc(module->dev, sizeof(struct
1246                                gbaudio_widget),
1247                       GFP_KERNEL);
1248         if (!widget) {
1249             ret = -ENOMEM;
1250             goto error;
1251         }
1252         widget->id = curr->id;
1253         widget->name = curr->name;
1254         list_add(&widget->list, &module->widget_list);
1255         curr = (void *)curr + w_size;
1256     }
1257     module->dapm_widgets = dapm_widgets;
1258 
1259     return 0;
1260 
1261 error:
1262     list_for_each_entry_safe(widget, _widget, &module->widget_list,
1263                  list) {
1264         list_del(&widget->list);
1265         devm_kfree(module->dev, widget);
1266     }
1267     devm_kfree(module->dev, dapm_widgets);
1268     return ret;
1269 }
1270 
1271 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1272                        struct gb_audio_route *routes)
1273 {
1274     int i, ret;
1275     struct snd_soc_dapm_route *dapm_routes;
1276     struct gb_audio_route *curr;
1277     size_t size;
1278 
1279     size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1280     dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1281     if (!dapm_routes)
1282         return -ENOMEM;
1283 
1284     module->dapm_routes = dapm_routes;
1285     curr = routes;
1286 
1287     for (i = 0; i < module->num_dapm_routes; i++) {
1288         dapm_routes->sink =
1289             gbaudio_map_widgetid(module, curr->destination_id);
1290         if (!dapm_routes->sink) {
1291             dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1292                 curr->source_id, curr->destination_id,
1293                 curr->control_id, curr->index);
1294             ret = -EINVAL;
1295             goto error;
1296         }
1297         dapm_routes->source =
1298             gbaudio_map_widgetid(module, curr->source_id);
1299         if (!dapm_routes->source) {
1300             dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1301                 curr->source_id, curr->destination_id,
1302                 curr->control_id, curr->index);
1303             ret = -EINVAL;
1304             goto error;
1305         }
1306         dapm_routes->control =
1307             gbaudio_map_controlid(module,
1308                           curr->control_id,
1309                           curr->index);
1310         if ((curr->control_id !=  GBAUDIO_INVALID_ID) &&
1311             !dapm_routes->control) {
1312             dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1313                 curr->source_id, curr->destination_id,
1314                 curr->control_id, curr->index);
1315             ret = -EINVAL;
1316             goto error;
1317         }
1318         dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1319             (dapm_routes->control) ? dapm_routes->control : "NULL",
1320             dapm_routes->source);
1321         dapm_routes++;
1322         curr++;
1323     }
1324 
1325     return 0;
1326 
1327 error:
1328     devm_kfree(module->dev, module->dapm_routes);
1329     return ret;
1330 }
1331 
1332 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1333                        struct gb_audio_topology *tplg_data)
1334 {
1335     /* fetch no. of kcontrols, widgets & routes */
1336     module->num_controls = tplg_data->num_controls;
1337     module->num_dapm_widgets = tplg_data->num_widgets;
1338     module->num_dapm_routes = tplg_data->num_routes;
1339 
1340     /* update block offset */
1341     module->dai_offset = (unsigned long)&tplg_data->data;
1342     module->control_offset = module->dai_offset +
1343                     le32_to_cpu(tplg_data->size_dais);
1344     module->widget_offset = module->control_offset +
1345                     le32_to_cpu(tplg_data->size_controls);
1346     module->route_offset = module->widget_offset +
1347                     le32_to_cpu(tplg_data->size_widgets);
1348 
1349     dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1350     dev_dbg(module->dev, "control offset is %lx\n",
1351         module->control_offset);
1352     dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1353     dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1354 
1355     return 0;
1356 }
1357 
1358 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1359                 struct gb_audio_topology *tplg_data)
1360 {
1361     int ret;
1362     struct gb_audio_control *controls;
1363     struct gb_audio_widget *widgets;
1364     struct gb_audio_route *routes;
1365     unsigned int jack_type;
1366 
1367     if (!tplg_data)
1368         return -EINVAL;
1369 
1370     ret = gbaudio_tplg_process_header(module, tplg_data);
1371     if (ret) {
1372         dev_err(module->dev, "%d: Error in parsing topology header\n",
1373             ret);
1374         return ret;
1375     }
1376 
1377     /* process control */
1378     controls = (struct gb_audio_control *)module->control_offset;
1379     ret = gbaudio_tplg_process_kcontrols(module, controls);
1380     if (ret) {
1381         dev_err(module->dev,
1382             "%d: Error in parsing controls data\n", ret);
1383         return ret;
1384     }
1385     dev_dbg(module->dev, "Control parsing finished\n");
1386 
1387     /* process widgets */
1388     widgets = (struct gb_audio_widget *)module->widget_offset;
1389     ret = gbaudio_tplg_process_widgets(module, widgets);
1390     if (ret) {
1391         dev_err(module->dev,
1392             "%d: Error in parsing widgets data\n", ret);
1393         return ret;
1394     }
1395     dev_dbg(module->dev, "Widget parsing finished\n");
1396 
1397     /* process route */
1398     routes = (struct gb_audio_route *)module->route_offset;
1399     ret = gbaudio_tplg_process_routes(module, routes);
1400     if (ret) {
1401         dev_err(module->dev,
1402             "%d: Error in parsing routes data\n", ret);
1403         return ret;
1404     }
1405     dev_dbg(module->dev, "Route parsing finished\n");
1406 
1407     /* parse jack capabilities */
1408     jack_type = le32_to_cpu(tplg_data->jack_type);
1409     if (jack_type) {
1410         module->jack_mask = jack_type & GBCODEC_JACK_MASK;
1411         module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK;
1412     }
1413 
1414     return ret;
1415 }
1416 
1417 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1418 {
1419     struct gbaudio_control *control, *_control;
1420     struct gbaudio_widget *widget, *_widget;
1421 
1422     if (!module->topology)
1423         return;
1424 
1425     /* release kcontrols */
1426     list_for_each_entry_safe(control, _control, &module->ctl_list,
1427                  list) {
1428         list_del(&control->list);
1429         devm_kfree(module->dev, control);
1430     }
1431     if (module->controls)
1432         devm_kfree(module->dev, module->controls);
1433 
1434     /* release widget controls */
1435     list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1436                  list) {
1437         list_del(&control->list);
1438         devm_kfree(module->dev, control);
1439     }
1440 
1441     /* release widgets */
1442     list_for_each_entry_safe(widget, _widget, &module->widget_list,
1443                  list) {
1444         list_del(&widget->list);
1445         devm_kfree(module->dev, widget);
1446     }
1447     if (module->dapm_widgets)
1448         devm_kfree(module->dev, module->dapm_widgets);
1449 
1450     /* release routes */
1451     if (module->dapm_routes)
1452         devm_kfree(module->dev, module->dapm_routes);
1453 }