3 * Copyright 2015-2016 Google Inc.
4 * Copyright 2015-2016 Linaro Ltd.
6 * Released under the GPLv2 only.
9 #include "audio_codec.h"
10 #include "greybus_protocols.h"
12 #define GBAUDIO_INVALID_ID 0xFF
15 struct gb_mixer_control {
17 unsigned int reg, rreg, shift, rshift, invert;
20 struct gbaudio_ctl_pvt {
22 unsigned int data_cport;
25 struct gb_audio_ctl_elem_info *info;
28 static struct gbaudio_module_info *find_gb_module(
29 struct gbaudio_codec_info *codec,
33 char begin[NAME_SIZE];
34 struct gbaudio_module_info *module;
39 ret = sscanf(name, "%s %d", begin, &dev_id);
40 dev_dbg(codec->dev, "%s:Find module#%d\n", __func__, dev_id);
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);
49 mutex_unlock(&codec->lock);
50 dev_warn(codec->dev, "%s: module#%d missing in codec list\n", name,
55 static const char *gbaudio_map_controlid(struct gbaudio_module_info *module,
56 __u8 control_id, __u8 index)
58 struct gbaudio_control *control;
60 if (control_id == GBAUDIO_INVALID_ID)
63 list_for_each_entry(control, &module->ctl_list, list) {
64 if (control->id == control_id) {
65 if (index == GBAUDIO_INVALID_ID)
67 if (index >= control->items)
69 return control->texts[index];
72 list_for_each_entry(control, &module->widget_ctl_list, list) {
73 if (control->id == control_id) {
74 if (index == GBAUDIO_INVALID_ID)
76 if (index >= control->items)
78 return control->texts[index];
84 static int gbaudio_map_controlname(struct gbaudio_module_info *module,
87 struct gbaudio_control *control;
89 list_for_each_entry(control, &module->ctl_list, list) {
90 if (!strncmp(control->name, name, NAME_SIZE))
94 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
99 static int gbaudio_map_wcontrolname(struct gbaudio_module_info *module,
102 struct gbaudio_control *control;
104 list_for_each_entry(control, &module->widget_ctl_list, list) {
105 if (!strncmp(control->wname, name, NAME_SIZE))
108 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
113 static int gbaudio_map_widgetname(struct gbaudio_module_info *module,
116 struct gbaudio_widget *widget;
117 list_for_each_entry(widget, &module->widget_list, list) {
118 if (!strncmp(widget->name, name, NAME_SIZE))
121 dev_warn(module->dev, "%s: missing in modules widgets list\n", name);
126 static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module,
129 struct gbaudio_widget *widget;
131 list_for_each_entry(widget, &module->widget_list, list) {
132 if (widget->id == widget_id)
138 static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
139 struct gb_audio_enumerated *gbenum)
141 const char **strings;
145 strings = devm_kzalloc(gb->dev, sizeof(char *) * gbenum->items,
147 data = gbenum->names;
149 for (i = 0; i < gbenum->items; i++) {
150 strings[i] = (const char *)data;
151 while (*data != '\0')
159 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
160 struct snd_ctl_elem_info *uinfo)
164 struct gbaudio_ctl_pvt *data;
165 struct gb_audio_ctl_elem_info *info;
166 struct gbaudio_module_info *module;
167 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
168 struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
170 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
171 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
172 info = (struct gb_audio_ctl_elem_info *)data->info;
175 dev_err(module->dev, "NULL info for %s\n", uinfo->id.name);
180 uinfo->access = data->access;
181 uinfo->count = data->vcount;
182 uinfo->type = (snd_ctl_elem_type_t)info->type;
184 switch (info->type) {
185 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
186 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
187 uinfo->value.integer.min = info->value.integer.min;
188 uinfo->value.integer.max = info->value.integer.max;
190 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
191 max = info->value.enumerated.items;
192 uinfo->value.enumerated.items = max;
193 if (uinfo->value.enumerated.item > max - 1)
194 uinfo->value.enumerated.item = max - 1;
195 module = find_gb_module(gbcodec, kcontrol->id.name);
198 name = gbaudio_map_controlid(module, data->ctl_id,
199 uinfo->value.enumerated.item);
200 strlcpy(uinfo->value.enumerated.name, name, NAME_SIZE);
203 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
204 info->type, kcontrol->id.name);
210 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
211 struct snd_ctl_elem_value *ucontrol)
214 struct gb_audio_ctl_elem_info *info;
215 struct gbaudio_ctl_pvt *data;
216 struct gb_audio_ctl_elem_value gbvalue;
217 struct gbaudio_module_info *module;
218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
219 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
220 struct gb_bundle *bundle;
222 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
223 module = find_gb_module(gb, kcontrol->id.name);
227 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
228 info = (struct gb_audio_ctl_elem_info *)data->info;
229 bundle = to_gb_bundle(module->dev);
231 ret = gb_pm_runtime_get_sync(bundle);
235 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
236 GB_AUDIO_INVALID_INDEX, &gbvalue);
238 gb_pm_runtime_put_autosuspend(bundle);
241 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
242 __func__, kcontrol->id.name);
246 /* update ucontrol */
247 switch (info->type) {
248 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
249 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
250 ucontrol->value.integer.value[0] =
251 gbvalue.value.integer_value[0];
252 if (data->vcount == 2)
253 ucontrol->value.integer.value[1] =
254 gbvalue.value.integer_value[1];
256 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
257 ucontrol->value.enumerated.item[0] =
258 gbvalue.value.enumerated_item[0];
259 if (data->vcount == 2)
260 ucontrol->value.enumerated.item[1] =
261 gbvalue.value.enumerated_item[1];
264 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
265 info->type, kcontrol->id.name);
272 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
276 struct gb_audio_ctl_elem_info *info;
277 struct gbaudio_ctl_pvt *data;
278 struct gb_audio_ctl_elem_value gbvalue;
279 struct gbaudio_module_info *module;
280 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
281 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
282 struct gb_bundle *bundle;
284 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
285 module = find_gb_module(gb, kcontrol->id.name);
289 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
290 info = (struct gb_audio_ctl_elem_info *)data->info;
291 bundle = to_gb_bundle(module->dev);
293 /* update ucontrol */
294 switch (info->type) {
295 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
296 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
297 gbvalue.value.integer_value[0] =
298 ucontrol->value.integer.value[0];
299 if (data->vcount == 2)
300 gbvalue.value.integer_value[1] =
301 ucontrol->value.integer.value[1];
303 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
304 gbvalue.value.enumerated_item[0] =
305 ucontrol->value.enumerated.item[0];
306 if (data->vcount == 2)
307 gbvalue.value.enumerated_item[1] =
308 ucontrol->value.enumerated.item[1];
311 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
312 info->type, kcontrol->id.name);
320 ret = gb_pm_runtime_get_sync(bundle);
324 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
325 GB_AUDIO_INVALID_INDEX, &gbvalue);
327 gb_pm_runtime_put_autosuspend(bundle);
330 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
331 __func__, kcontrol->id.name);
337 #define SOC_MIXER_GB(xname, kcount, data) \
338 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
339 .count = kcount, .info = gbcodec_mixer_ctl_info, \
340 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
341 .private_value = (unsigned long)data }
344 * although below callback functions seems redundant to above functions.
345 * same are kept to allow provision for different handling in case
346 * of DAPM related sequencing, etc.
348 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_info *uinfo)
351 int platform_max, platform_min;
352 struct gbaudio_ctl_pvt *data;
353 struct gb_audio_ctl_elem_info *info;
354 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
355 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
356 struct snd_soc_codec *codec = widget->codec;
358 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
359 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
360 info = (struct gb_audio_ctl_elem_info *)data->info;
363 platform_max = info->value.integer.max;
364 platform_min = info->value.integer.min;
366 if (platform_max == 1 &&
367 !strnstr(kcontrol->id.name, " Volume", NAME_SIZE))
368 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
370 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
372 uinfo->count = data->vcount;
373 uinfo->value.integer.min = 0;
374 if (info->value.integer.min < 0 &&
375 (uinfo->type == SNDRV_CTL_ELEM_TYPE_INTEGER))
376 uinfo->value.integer.max = platform_max - platform_min;
378 uinfo->value.integer.max = platform_max;
383 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
384 struct snd_ctl_elem_value *ucontrol)
387 struct gb_audio_ctl_elem_info *info;
388 struct gbaudio_ctl_pvt *data;
389 struct gb_audio_ctl_elem_value gbvalue;
390 struct gbaudio_module_info *module;
391 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
392 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
393 struct snd_soc_codec *codec = widget->codec;
394 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
395 struct gb_bundle *bundle;
397 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
398 module = find_gb_module(gb, kcontrol->id.name);
402 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
403 info = (struct gb_audio_ctl_elem_info *)data->info;
404 bundle = to_gb_bundle(module->dev);
406 if (data->vcount == 2)
407 dev_warn(widget->dapm->dev,
408 "GB: Control '%s' is stereo, which is not supported\n",
411 ret = gb_pm_runtime_get_sync(bundle);
415 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
416 GB_AUDIO_INVALID_INDEX, &gbvalue);
418 gb_pm_runtime_put_autosuspend(bundle);
421 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
422 __func__, kcontrol->id.name);
425 /* update ucontrol */
426 ucontrol->value.integer.value[0] = gbvalue.value.integer_value[0];
431 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
432 struct snd_ctl_elem_value *ucontrol)
434 int ret, wi, max, connect;
435 unsigned int mask, val;
436 struct gb_audio_ctl_elem_info *info;
437 struct gbaudio_ctl_pvt *data;
438 struct gb_audio_ctl_elem_value gbvalue;
439 struct gbaudio_module_info *module;
440 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
441 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
442 struct snd_soc_codec *codec = widget->codec;
443 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
444 struct gb_bundle *bundle;
446 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
447 module = find_gb_module(gb, kcontrol->id.name);
451 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
452 info = (struct gb_audio_ctl_elem_info *)data->info;
453 bundle = to_gb_bundle(module->dev);
455 if (data->vcount == 2)
456 dev_warn(widget->dapm->dev,
457 "GB: Control '%s' is stereo, which is not supported\n",
460 max = info->value.integer.max;
461 mask = (1 << fls(max)) - 1;
462 val = (ucontrol->value.integer.value[0] & mask);
465 /* update ucontrol */
466 if (gbvalue.value.integer_value[0] != val) {
467 for (wi = 0; wi < wlist->num_widgets; wi++) {
468 widget = wlist->widgets[wi];
471 widget->dapm->update = NULL;
472 snd_soc_dapm_mixer_update_power(widget, kcontrol,
475 gbvalue.value.integer_value[0] =
476 ucontrol->value.integer.value[0];
478 ret = gb_pm_runtime_get_sync(bundle);
482 ret = gb_audio_gb_set_control(module->mgmt_connection,
484 GB_AUDIO_INVALID_INDEX, &gbvalue);
486 gb_pm_runtime_put_autosuspend(bundle);
489 dev_err_ratelimited(codec->dev,
490 "%d:Error in %s for %s\n", ret,
491 __func__, kcontrol->id.name);
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}
504 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
505 struct snd_kcontrol *k, int event)
507 /* Ensure GB speaker is connected */
512 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
513 struct snd_kcontrol *k, int event)
515 /* Ensure GB module supports jack slot */
520 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
521 struct snd_kcontrol *k, int event)
523 /* Ensure GB module supports jack slot */
528 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
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:
541 case snd_soc_dapm_switch:
542 case snd_soc_dapm_mux:
543 if (w->ncontrols != 1)
553 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
554 struct snd_ctl_elem_value *ucontrol)
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;
564 module = find_gb_module(gb, kcontrol->id.name);
568 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
572 bundle = to_gb_bundle(module->dev);
574 ret = gb_pm_runtime_get_sync(bundle);
578 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
579 GB_AUDIO_INVALID_INDEX, &gbvalue);
581 gb_pm_runtime_put_autosuspend(bundle);
584 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
585 __func__, kcontrol->id.name);
589 ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
590 if (e->shift_l != e->shift_r)
591 ucontrol->value.enumerated.item[1] =
592 gbvalue.value.enumerated_item[1];
597 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
598 struct snd_ctl_elem_value *ucontrol)
601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
602 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
603 struct gb_audio_ctl_elem_value gbvalue;
604 struct gbaudio_module_info *module;
605 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
606 struct gb_bundle *bundle;
608 module = find_gb_module(gb, kcontrol->id.name);
612 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
616 if (ucontrol->value.enumerated.item[0] > e->max - 1)
618 gbvalue.value.enumerated_item[0] = ucontrol->value.enumerated.item[0];
620 if (e->shift_l != e->shift_r) {
621 if (ucontrol->value.enumerated.item[1] > e->max - 1)
623 gbvalue.value.enumerated_item[1] =
624 ucontrol->value.enumerated.item[1];
627 bundle = to_gb_bundle(module->dev);
629 ret = gb_pm_runtime_get_sync(bundle);
633 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
634 GB_AUDIO_INVALID_INDEX, &gbvalue);
636 gb_pm_runtime_put_autosuspend(bundle);
639 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
640 __func__, kcontrol->id.name);
646 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
647 struct snd_kcontrol_new *kctl,
648 struct gb_audio_control *ctl)
650 struct soc_enum *gbe;
651 struct gb_audio_enumerated *gb_enum;
654 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
658 gb_enum = &ctl->info.value.enumerated;
660 /* since count=1, and reg is dummy */
661 gbe->max = gb_enum->items;
662 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
664 /* debug enum info */
665 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gb_enum->items,
666 gb_enum->names_length);
667 for (i = 0; i < gb_enum->items; i++)
668 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
670 *kctl = (struct snd_kcontrol_new)
671 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
672 gbcodec_enum_ctl_put);
676 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
677 struct snd_kcontrol_new *kctl,
678 struct gb_audio_control *ctl)
681 struct gbaudio_ctl_pvt *ctldata;
683 switch (ctl->iface) {
684 case SNDRV_CTL_ELEM_IFACE_MIXER:
685 switch (ctl->info.type) {
686 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
687 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
690 ctldata = devm_kzalloc(gb->dev,
691 sizeof(struct gbaudio_ctl_pvt),
695 ctldata->ctl_id = ctl->id;
696 ctldata->data_cport = ctl->data_cport;
697 ctldata->access = ctl->access;
698 ctldata->vcount = ctl->count_values;
699 ctldata->info = &ctl->info;
700 *kctl = (struct snd_kcontrol_new)
701 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
710 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
714 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
715 struct snd_ctl_elem_value *ucontrol)
718 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
719 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
720 struct gbaudio_module_info *module;
721 struct gb_audio_ctl_elem_value gbvalue;
722 struct snd_soc_codec *codec = widget->codec;
723 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
724 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
725 struct gb_bundle *bundle;
727 module = find_gb_module(gb, kcontrol->id.name);
731 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
735 bundle = to_gb_bundle(module->dev);
737 ret = gb_pm_runtime_get_sync(bundle);
741 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
742 GB_AUDIO_INVALID_INDEX, &gbvalue);
744 gb_pm_runtime_put_autosuspend(bundle);
747 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
748 __func__, kcontrol->id.name);
752 ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
753 if (e->shift_l != e->shift_r)
754 ucontrol->value.enumerated.item[1] =
755 gbvalue.value.enumerated_item[1];
760 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_value *ucontrol)
764 unsigned int val, mux, change;
766 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
767 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
768 struct gb_audio_ctl_elem_value gbvalue;
769 struct gbaudio_module_info *module;
770 struct snd_soc_codec *codec = widget->codec;
771 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
772 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
773 struct gb_bundle *bundle;
775 if (ucontrol->value.enumerated.item[0] > e->max - 1)
778 module = find_gb_module(gb, kcontrol->id.name);
782 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
787 bundle = to_gb_bundle(module->dev);
789 ret = gb_pm_runtime_get_sync(bundle);
793 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
794 GB_AUDIO_INVALID_INDEX, &gbvalue);
796 gb_pm_runtime_put_autosuspend(bundle);
799 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
800 __func__, kcontrol->id.name);
804 mux = ucontrol->value.enumerated.item[0];
805 val = mux << e->shift_l;
806 mask = e->mask << e->shift_l;
808 if (gbvalue.value.enumerated_item[0] !=
809 ucontrol->value.enumerated.item[0]) {
811 gbvalue.value.enumerated_item[0] =
812 ucontrol->value.enumerated.item[0];
815 if (e->shift_l != e->shift_r) {
816 if (ucontrol->value.enumerated.item[1] > e->max - 1)
818 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
819 mask |= e->mask << e->shift_r;
820 if (gbvalue.value.enumerated_item[1] !=
821 ucontrol->value.enumerated.item[1]) {
823 gbvalue.value.enumerated_item[1] =
824 ucontrol->value.enumerated.item[1];
829 ret = gb_pm_runtime_get_sync(bundle);
833 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
834 GB_AUDIO_INVALID_INDEX, &gbvalue);
836 gb_pm_runtime_put_autosuspend(bundle);
839 dev_err_ratelimited(codec->dev,
840 "%d:Error in %s for %s\n", ret,
841 __func__, kcontrol->id.name);
843 for (wi = 0; wi < wlist->num_widgets; wi++) {
844 widget = wlist->widgets[wi];
847 widget->dapm->update = NULL;
848 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
855 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
856 struct snd_kcontrol_new *kctl,
857 struct gb_audio_control *ctl)
859 struct soc_enum *gbe;
860 struct gb_audio_enumerated *gb_enum;
863 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
867 gb_enum = &ctl->info.value.enumerated;
869 /* since count=1, and reg is dummy */
870 gbe->max = gb_enum->items;
871 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
873 /* debug enum info */
874 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gb_enum->items,
875 gb_enum->names_length);
876 for (i = 0; i < gb_enum->items; i++)
877 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
879 *kctl = (struct snd_kcontrol_new)
880 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
881 gbcodec_enum_dapm_ctl_put);
885 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
886 struct snd_kcontrol_new *kctl,
887 struct gb_audio_control *ctl)
889 struct gbaudio_ctl_pvt *ctldata;
891 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
895 ctldata->ctl_id = ctl->id;
896 ctldata->data_cport = ctl->data_cport;
897 ctldata->access = ctl->access;
898 ctldata->vcount = ctl->count_values;
899 ctldata->info = &ctl->info;
900 *kctl = (struct snd_kcontrol_new)
901 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
906 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
907 struct snd_kcontrol_new *kctl,
908 struct gb_audio_control *ctl)
912 switch (ctl->iface) {
913 case SNDRV_CTL_ELEM_IFACE_MIXER:
914 switch (ctl->info.type) {
915 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
916 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
919 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
928 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
933 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
934 struct snd_kcontrol *kcontrol, int event)
938 struct snd_soc_codec *codec = w->codec;
939 struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
940 struct gbaudio_module_info *module;
941 struct gb_bundle *bundle;
943 dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
945 /* Find relevant module */
946 module = find_gb_module(gbcodec, w->name);
950 /* map name to widget id */
951 wid = gbaudio_map_widgetname(module, w->name);
953 dev_err(codec->dev, "Invalid widget name:%s\n", w->name);
957 bundle = to_gb_bundle(module->dev);
959 ret = gb_pm_runtime_get_sync(bundle);
964 case SND_SOC_DAPM_PRE_PMU:
965 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
967 ret = gbaudio_module_update(gbcodec, w, module, 1);
969 case SND_SOC_DAPM_POST_PMD:
970 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
972 ret = gbaudio_module_update(gbcodec, w, module, 0);
976 dev_err_ratelimited(codec->dev,
977 "%d: widget, event:%d failed:%d\n", wid,
980 gb_pm_runtime_put_autosuspend(bundle);
985 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
986 struct snd_soc_dapm_widget *dw,
987 struct gb_audio_widget *w, int *w_size)
990 struct snd_kcontrol_new *widget_kctls;
991 struct gb_audio_control *curr;
992 struct gbaudio_control *control, *_control;
994 char temp_name[NAME_SIZE];
996 ret = gbaudio_validate_kcontrol_count(w);
998 dev_err(module->dev, "Inavlid kcontrol count=%d for %s\n",
999 w->ncontrols, w->name);
1003 /* allocate memory for kcontrol */
1005 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1006 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1011 *w_size = sizeof(struct gb_audio_widget);
1013 /* create relevant kcontrols */
1015 for (i = 0; i < w->ncontrols; i++) {
1016 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1019 dev_err(module->dev,
1020 "%s:%d type widget_ctl not supported\n",
1021 curr->name, curr->iface);
1024 control = devm_kzalloc(module->dev,
1025 sizeof(struct gbaudio_control),
1031 control->id = curr->id;
1032 control->name = curr->name;
1033 control->wname = w->name;
1035 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1036 struct gb_audio_enumerated *gbenum =
1037 &curr->info.value.enumerated;
1039 csize = offsetof(struct gb_audio_control, info);
1040 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1041 csize += offsetof(struct gb_audio_enumerated, names);
1042 csize += gbenum->names_length;
1043 control->texts = (const char * const *)
1044 gb_generate_enum_strings(module, gbenum);
1045 control->items = gbenum->items;
1047 csize = sizeof(struct gb_audio_control);
1049 curr = (void *)curr + csize;
1050 list_add(&control->list, &module->widget_ctl_list);
1051 dev_dbg(module->dev, "%s: control of type %d created\n",
1052 widget_kctls[i].name, widget_kctls[i].iface);
1055 /* Prefix dev_id to widget control_name */
1056 strlcpy(temp_name, w->name, NAME_SIZE);
1057 snprintf(w->name, NAME_SIZE, "GB %d %s", module->dev_id, temp_name);
1060 case snd_soc_dapm_spk:
1061 *dw = (struct snd_soc_dapm_widget)
1062 SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1063 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1065 case snd_soc_dapm_hp:
1066 *dw = (struct snd_soc_dapm_widget)
1067 SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1068 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1069 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1070 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1072 case snd_soc_dapm_mic:
1073 *dw = (struct snd_soc_dapm_widget)
1074 SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1075 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1077 case snd_soc_dapm_output:
1078 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1080 case snd_soc_dapm_input:
1081 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1083 case snd_soc_dapm_switch:
1084 *dw = (struct snd_soc_dapm_widget)
1085 SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1086 widget_kctls, gbaudio_widget_event,
1087 SND_SOC_DAPM_PRE_PMU |
1088 SND_SOC_DAPM_POST_PMD);
1090 case snd_soc_dapm_pga:
1091 *dw = (struct snd_soc_dapm_widget)
1092 SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1093 gbaudio_widget_event,
1094 SND_SOC_DAPM_PRE_PMU |
1095 SND_SOC_DAPM_POST_PMD);
1097 case snd_soc_dapm_mixer:
1098 *dw = (struct snd_soc_dapm_widget)
1099 SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1100 0, gbaudio_widget_event,
1101 SND_SOC_DAPM_PRE_PMU |
1102 SND_SOC_DAPM_POST_PMD);
1104 case snd_soc_dapm_mux:
1105 *dw = (struct snd_soc_dapm_widget)
1106 SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1107 widget_kctls, gbaudio_widget_event,
1108 SND_SOC_DAPM_PRE_PMU |
1109 SND_SOC_DAPM_POST_PMD);
1111 case snd_soc_dapm_aif_in:
1112 *dw = (struct snd_soc_dapm_widget)
1113 SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1115 0, 0, gbaudio_widget_event,
1116 SND_SOC_DAPM_PRE_PMU |
1117 SND_SOC_DAPM_POST_PMD);
1119 case snd_soc_dapm_aif_out:
1120 *dw = (struct snd_soc_dapm_widget)
1121 SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1123 0, 0, gbaudio_widget_event,
1124 SND_SOC_DAPM_PRE_PMU |
1125 SND_SOC_DAPM_POST_PMD);
1132 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1136 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1138 list_del(&control->list);
1139 devm_kfree(module->dev, control);
1144 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1145 struct gb_audio_control *controls)
1148 struct snd_kcontrol_new *dapm_kctls;
1149 struct gb_audio_control *curr;
1150 struct gbaudio_control *control, *_control;
1152 char temp_name[NAME_SIZE];
1154 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1155 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1160 for (i = 0; i < module->num_controls; i++) {
1161 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1164 dev_err(module->dev, "%s:%d type not supported\n",
1165 curr->name, curr->iface);
1168 control = devm_kzalloc(module->dev, sizeof(struct
1175 control->id = curr->id;
1176 /* Prefix dev_id to widget_name */
1177 strlcpy(temp_name, curr->name, NAME_SIZE);
1178 snprintf(curr->name, NAME_SIZE, "GB %d %s", module->dev_id,
1180 control->name = curr->name;
1181 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1182 struct gb_audio_enumerated *gbenum =
1183 &curr->info.value.enumerated;
1185 csize = offsetof(struct gb_audio_control, info);
1186 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1187 csize += offsetof(struct gb_audio_enumerated, names);
1188 csize += gbenum->names_length;
1189 control->texts = (const char * const *)
1190 gb_generate_enum_strings(module, gbenum);
1191 control->items = gbenum->items;
1193 csize = sizeof(struct gb_audio_control);
1195 list_add(&control->list, &module->ctl_list);
1196 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1197 curr->name, curr->info.type);
1198 curr = (void *)curr + csize;
1200 module->controls = dapm_kctls;
1204 list_for_each_entry_safe(control, _control, &module->ctl_list,
1206 list_del(&control->list);
1207 devm_kfree(module->dev, control);
1209 devm_kfree(module->dev, dapm_kctls);
1213 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1214 struct gb_audio_widget *widgets)
1217 struct snd_soc_dapm_widget *dapm_widgets;
1218 struct gb_audio_widget *curr;
1219 struct gbaudio_widget *widget, *_widget;
1222 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1223 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1228 for (i = 0; i < module->num_dapm_widgets; i++) {
1229 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1232 dev_err(module->dev, "%s:%d type not supported\n",
1233 curr->name, curr->type);
1236 widget = devm_kzalloc(module->dev, sizeof(struct
1243 widget->id = curr->id;
1244 widget->name = curr->name;
1245 list_add(&widget->list, &module->widget_list);
1246 curr = (void *)curr + w_size;
1248 module->dapm_widgets = dapm_widgets;
1253 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1255 list_del(&widget->list);
1256 devm_kfree(module->dev, widget);
1258 devm_kfree(module->dev, dapm_widgets);
1262 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1263 struct gb_audio_route *routes)
1266 struct snd_soc_dapm_route *dapm_routes;
1267 struct gb_audio_route *curr;
1270 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1271 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1275 module->dapm_routes = dapm_routes;
1278 for (i = 0; i < module->num_dapm_routes; i++) {
1280 gbaudio_map_widgetid(module, curr->destination_id);
1281 if (!dapm_routes->sink) {
1282 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1283 curr->source_id, curr->destination_id,
1284 curr->control_id, curr->index);
1288 dapm_routes->source =
1289 gbaudio_map_widgetid(module, curr->source_id);
1290 if (!dapm_routes->source) {
1291 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1292 curr->source_id, curr->destination_id,
1293 curr->control_id, curr->index);
1297 dapm_routes->control =
1298 gbaudio_map_controlid(module,
1301 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1302 !dapm_routes->control) {
1303 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1304 curr->source_id, curr->destination_id,
1305 curr->control_id, curr->index);
1309 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1310 (dapm_routes->control) ? dapm_routes->control:"NULL",
1311 dapm_routes->source);
1319 devm_kfree(module->dev, module->dapm_routes);
1323 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1324 struct gb_audio_topology *tplg_data)
1326 /* fetch no. of kcontrols, widgets & routes */
1327 module->num_controls = tplg_data->num_controls;
1328 module->num_dapm_widgets = tplg_data->num_widgets;
1329 module->num_dapm_routes = tplg_data->num_routes;
1331 /* update block offset */
1332 module->dai_offset = (unsigned long)&tplg_data->data;
1333 module->control_offset = module->dai_offset + tplg_data->size_dais;
1334 module->widget_offset = module->control_offset +
1335 tplg_data->size_controls;
1336 module->route_offset = module->widget_offset +
1337 tplg_data->size_widgets;
1339 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1340 dev_dbg(module->dev, "control offset is %lx\n",
1341 module->control_offset);
1342 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1343 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1348 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1349 struct gb_audio_topology *tplg_data)
1352 struct gb_audio_control *controls;
1353 struct gb_audio_widget *widgets;
1354 struct gb_audio_route *routes;
1359 ret = gbaudio_tplg_process_header(module, tplg_data);
1361 dev_err(module->dev, "%d: Error in parsing topology header\n",
1366 /* process control */
1367 controls = (struct gb_audio_control *)module->control_offset;
1368 ret = gbaudio_tplg_process_kcontrols(module, controls);
1370 dev_err(module->dev,
1371 "%d: Error in parsing controls data\n", ret);
1374 dev_dbg(module->dev, "Control parsing finished\n");
1376 /* process widgets */
1377 widgets = (struct gb_audio_widget *)module->widget_offset;
1378 ret = gbaudio_tplg_process_widgets(module, widgets);
1380 dev_err(module->dev,
1381 "%d: Error in parsing widgets data\n", ret);
1384 dev_dbg(module->dev, "Widget parsing finished\n");
1387 routes = (struct gb_audio_route *)module->route_offset;
1388 ret = gbaudio_tplg_process_routes(module, routes);
1390 dev_err(module->dev,
1391 "%d: Error in parsing routes data\n", ret);
1394 dev_dbg(module->dev, "Route parsing finished\n");
1396 /* parse jack capabilities */
1397 if (tplg_data->jack_type) {
1398 module->jack_mask = tplg_data->jack_type & GBCODEC_JACK_MASK;
1399 module->button_mask = tplg_data->jack_type &
1400 GBCODEC_JACK_BUTTON_MASK;
1406 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1408 struct gbaudio_control *control, *_control;
1409 struct gbaudio_widget *widget, *_widget;
1411 if (!module->topology)
1414 /* release kcontrols */
1415 list_for_each_entry_safe(control, _control, &module->ctl_list,
1417 list_del(&control->list);
1418 devm_kfree(module->dev, control);
1420 if (module->controls)
1421 devm_kfree(module->dev, module->controls);
1423 /* release widget controls */
1424 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1426 list_del(&control->list);
1427 devm_kfree(module->dev, control);
1430 /* release widgets */
1431 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1433 list_del(&widget->list);
1434 devm_kfree(module->dev, widget);
1436 if (module->dapm_widgets)
1437 devm_kfree(module->dev, module->dapm_widgets);
1439 /* release routes */
1440 if (module->dapm_routes)
1441 devm_kfree(module->dev, module->dapm_routes);