1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2015-2016 Google Inc.
5 * Copyright 2015-2016 Linaro Ltd.
8 #include <linux/greybus.h>
9 #include "audio_codec.h"
11 #define GBAUDIO_INVALID_ID 0xFF
14 struct gb_mixer_control {
16 unsigned int reg, rreg, shift, rshift, invert;
19 struct gbaudio_ctl_pvt {
21 unsigned int data_cport;
24 struct gb_audio_ctl_elem_info *info;
27 static struct gbaudio_module_info *find_gb_module(
28 struct gbaudio_codec_info *codec,
32 char begin[NAME_SIZE];
33 struct gbaudio_module_info *module;
38 if (sscanf(name, "%s %d", begin, &dev_id) != 2)
41 dev_dbg(codec->dev, "%s:Find module#%d\n", __func__, dev_id);
43 mutex_lock(&codec->lock);
44 list_for_each_entry(module, &codec->module_list, list) {
45 if (module->dev_id == dev_id) {
46 mutex_unlock(&codec->lock);
50 mutex_unlock(&codec->lock);
51 dev_warn(codec->dev, "%s: module#%d missing in codec list\n", name,
56 static const char *gbaudio_map_controlid(struct gbaudio_module_info *module,
57 __u8 control_id, __u8 index)
59 struct gbaudio_control *control;
61 if (control_id == GBAUDIO_INVALID_ID)
64 list_for_each_entry(control, &module->ctl_list, list) {
65 if (control->id == control_id) {
66 if (index == GBAUDIO_INVALID_ID)
68 if (index >= control->items)
70 return control->texts[index];
73 list_for_each_entry(control, &module->widget_ctl_list, list) {
74 if (control->id == control_id) {
75 if (index == GBAUDIO_INVALID_ID)
77 if (index >= control->items)
79 return control->texts[index];
85 static int gbaudio_map_controlname(struct gbaudio_module_info *module,
88 struct gbaudio_control *control;
90 list_for_each_entry(control, &module->ctl_list, list) {
91 if (!strncmp(control->name, name, NAME_SIZE))
95 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
100 static int gbaudio_map_wcontrolname(struct gbaudio_module_info *module,
103 struct gbaudio_control *control;
105 list_for_each_entry(control, &module->widget_ctl_list, list) {
106 if (!strncmp(control->wname, name, NAME_SIZE))
109 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
114 static int gbaudio_map_widgetname(struct gbaudio_module_info *module,
117 struct gbaudio_widget *widget;
119 list_for_each_entry(widget, &module->widget_list, list) {
120 if (!strncmp(widget->name, name, NAME_SIZE))
123 dev_warn(module->dev, "%s: missing in modules widgets list\n", name);
128 static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module,
131 struct gbaudio_widget *widget;
133 list_for_each_entry(widget, &module->widget_list, list) {
134 if (widget->id == widget_id)
140 static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
141 struct gb_audio_enumerated *gbenum)
143 const char **strings;
148 items = le32_to_cpu(gbenum->items);
149 strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL);
150 data = gbenum->names;
152 for (i = 0; i < items; i++) {
153 strings[i] = (const char *)data;
154 while (*data != '\0')
162 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
163 struct snd_ctl_elem_info *uinfo)
167 struct gbaudio_ctl_pvt *data;
168 struct gb_audio_ctl_elem_info *info;
169 struct gbaudio_module_info *module;
170 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
171 struct gbaudio_codec_info *gbcodec = snd_soc_component_get_drvdata(comp);
173 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
174 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
175 info = (struct gb_audio_ctl_elem_info *)data->info;
178 dev_err(comp->dev, "NULL info for %s\n", uinfo->id.name);
183 uinfo->access = data->access;
184 uinfo->count = data->vcount;
185 uinfo->type = (__force snd_ctl_elem_type_t)info->type;
187 switch (info->type) {
188 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
189 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
190 uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
191 uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
193 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
194 max = le32_to_cpu(info->value.enumerated.items);
195 uinfo->value.enumerated.items = max;
196 if (uinfo->value.enumerated.item > max - 1)
197 uinfo->value.enumerated.item = max - 1;
198 module = find_gb_module(gbcodec, kcontrol->id.name);
201 name = gbaudio_map_controlid(module, data->ctl_id,
202 uinfo->value.enumerated.item);
203 strscpy(uinfo->value.enumerated.name, name, sizeof(uinfo->value.enumerated.name));
206 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
207 info->type, kcontrol->id.name);
213 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
217 struct gb_audio_ctl_elem_info *info;
218 struct gbaudio_ctl_pvt *data;
219 struct gb_audio_ctl_elem_value gbvalue;
220 struct gbaudio_module_info *module;
221 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
222 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
223 struct gb_bundle *bundle;
225 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
226 module = find_gb_module(gb, kcontrol->id.name);
230 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
231 info = (struct gb_audio_ctl_elem_info *)data->info;
232 bundle = to_gb_bundle(module->dev);
234 ret = gb_pm_runtime_get_sync(bundle);
238 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
239 GB_AUDIO_INVALID_INDEX, &gbvalue);
241 gb_pm_runtime_put_autosuspend(bundle);
244 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
245 __func__, kcontrol->id.name);
249 /* update ucontrol */
250 switch (info->type) {
251 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
252 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
253 ucontrol->value.integer.value[0] =
254 le32_to_cpu(gbvalue.value.integer_value[0]);
255 if (data->vcount == 2)
256 ucontrol->value.integer.value[1] =
257 le32_to_cpu(gbvalue.value.integer_value[1]);
259 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
260 ucontrol->value.enumerated.item[0] =
261 le32_to_cpu(gbvalue.value.enumerated_item[0]);
262 if (data->vcount == 2)
263 ucontrol->value.enumerated.item[1] =
264 le32_to_cpu(gbvalue.value.enumerated_item[1]);
267 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
268 info->type, kcontrol->id.name);
275 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
276 struct snd_ctl_elem_value *ucontrol)
279 struct gb_audio_ctl_elem_info *info;
280 struct gbaudio_ctl_pvt *data;
281 struct gb_audio_ctl_elem_value gbvalue;
282 struct gbaudio_module_info *module;
283 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
284 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
285 struct gb_bundle *bundle;
287 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
288 module = find_gb_module(gb, kcontrol->id.name);
292 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
293 info = (struct gb_audio_ctl_elem_info *)data->info;
294 bundle = to_gb_bundle(module->dev);
296 /* update ucontrol */
297 switch (info->type) {
298 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
299 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
300 gbvalue.value.integer_value[0] =
301 cpu_to_le32(ucontrol->value.integer.value[0]);
302 if (data->vcount == 2)
303 gbvalue.value.integer_value[1] =
304 cpu_to_le32(ucontrol->value.integer.value[1]);
306 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
307 gbvalue.value.enumerated_item[0] =
308 cpu_to_le32(ucontrol->value.enumerated.item[0]);
309 if (data->vcount == 2)
310 gbvalue.value.enumerated_item[1] =
311 cpu_to_le32(ucontrol->value.enumerated.item[1]);
314 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
315 info->type, kcontrol->id.name);
323 ret = gb_pm_runtime_get_sync(bundle);
327 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
328 GB_AUDIO_INVALID_INDEX, &gbvalue);
330 gb_pm_runtime_put_autosuspend(bundle);
333 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
334 __func__, kcontrol->id.name);
340 #define SOC_MIXER_GB(xname, kcount, data) \
341 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
342 .count = kcount, .info = gbcodec_mixer_ctl_info, \
343 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
344 .private_value = (unsigned long)data }
347 * although below callback functions seems redundant to above functions.
348 * same are kept to allow provision for different handling in case
349 * of DAPM related sequencing, etc.
351 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
352 struct snd_ctl_elem_info *uinfo)
354 int platform_max, platform_min;
355 struct gbaudio_ctl_pvt *data;
356 struct gb_audio_ctl_elem_info *info;
358 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
359 info = (struct gb_audio_ctl_elem_info *)data->info;
362 platform_max = le32_to_cpu(info->value.integer.max);
363 platform_min = le32_to_cpu(info->value.integer.min);
365 if (platform_max == 1 &&
366 !strnstr(kcontrol->id.name, " Volume", sizeof(kcontrol->id.name)))
367 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
369 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
371 uinfo->count = data->vcount;
372 uinfo->value.integer.min = platform_min;
373 uinfo->value.integer.max = platform_max;
378 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
382 struct gbaudio_ctl_pvt *data;
383 struct gb_audio_ctl_elem_value gbvalue;
384 struct gbaudio_module_info *module;
385 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
386 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
387 struct device *codec_dev = widget->dapm->dev;
388 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
389 struct gb_bundle *bundle;
391 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
392 module = find_gb_module(gb, kcontrol->id.name);
396 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
397 bundle = to_gb_bundle(module->dev);
399 if (data->vcount == 2)
400 dev_warn(widget->dapm->dev,
401 "GB: Control '%s' is stereo, which is not supported\n",
404 ret = gb_pm_runtime_get_sync(bundle);
408 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
409 GB_AUDIO_INVALID_INDEX, &gbvalue);
411 gb_pm_runtime_put_autosuspend(bundle);
414 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
415 __func__, kcontrol->id.name);
418 /* update ucontrol */
419 ucontrol->value.integer.value[0] =
420 le32_to_cpu(gbvalue.value.integer_value[0]);
425 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
428 int ret, wi, max, connect;
429 unsigned int mask, val;
430 struct gb_audio_ctl_elem_info *info;
431 struct gbaudio_ctl_pvt *data;
432 struct gb_audio_ctl_elem_value gbvalue;
433 struct gbaudio_module_info *module;
434 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
435 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
436 struct device *codec_dev = widget->dapm->dev;
437 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
438 struct gb_bundle *bundle;
440 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
441 module = find_gb_module(gb, kcontrol->id.name);
445 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
446 info = (struct gb_audio_ctl_elem_info *)data->info;
447 bundle = to_gb_bundle(module->dev);
449 if (data->vcount == 2)
450 dev_warn(widget->dapm->dev,
451 "GB: Control '%s' is stereo, which is not supported\n",
454 max = le32_to_cpu(info->value.integer.max);
455 mask = (1 << fls(max)) - 1;
456 val = ucontrol->value.integer.value[0] & mask;
459 ret = gb_pm_runtime_get_sync(bundle);
463 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
464 GB_AUDIO_INVALID_INDEX, &gbvalue);
468 /* update ucontrol */
469 if (le32_to_cpu(gbvalue.value.integer_value[0]) != val) {
470 for (wi = 0; wi < wlist->num_widgets; wi++) {
471 widget = wlist->widgets[wi];
472 snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol,
475 gbvalue.value.integer_value[0] =
476 cpu_to_le32(ucontrol->value.integer.value[0]);
478 ret = gb_audio_gb_set_control(module->mgmt_connection,
480 GB_AUDIO_INVALID_INDEX, &gbvalue);
484 gb_pm_runtime_put_autosuspend(bundle);
486 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
487 __func__, kcontrol->id.name);
491 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
492 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
493 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
494 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
495 .private_value = (unsigned long)data}
497 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
498 struct snd_kcontrol *k, int event)
500 /* Ensure GB speaker is connected */
505 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
506 struct snd_kcontrol *k, int event)
508 /* Ensure GB module supports jack slot */
513 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
514 struct snd_kcontrol *k, int event)
516 /* Ensure GB module supports jack slot */
521 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
526 case snd_soc_dapm_spk:
527 case snd_soc_dapm_hp:
528 case snd_soc_dapm_mic:
529 case snd_soc_dapm_output:
530 case snd_soc_dapm_input:
534 case snd_soc_dapm_switch:
535 case snd_soc_dapm_mux:
536 if (w->ncontrols != 1)
546 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
547 struct snd_ctl_elem_value *ucontrol)
550 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
551 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
552 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
553 struct gb_audio_ctl_elem_value gbvalue;
554 struct gbaudio_module_info *module;
555 struct gb_bundle *bundle;
557 module = find_gb_module(gb, kcontrol->id.name);
561 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
565 bundle = to_gb_bundle(module->dev);
567 ret = gb_pm_runtime_get_sync(bundle);
571 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
572 GB_AUDIO_INVALID_INDEX, &gbvalue);
574 gb_pm_runtime_put_autosuspend(bundle);
577 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
578 __func__, kcontrol->id.name);
582 ucontrol->value.enumerated.item[0] =
583 le32_to_cpu(gbvalue.value.enumerated_item[0]);
584 if (e->shift_l != e->shift_r)
585 ucontrol->value.enumerated.item[1] =
586 le32_to_cpu(gbvalue.value.enumerated_item[1]);
591 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
592 struct snd_ctl_elem_value *ucontrol)
595 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
596 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
598 struct gb_audio_ctl_elem_value gbvalue;
599 struct gbaudio_module_info *module;
600 struct gb_bundle *bundle;
602 module = find_gb_module(gb, kcontrol->id.name);
606 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
610 if (ucontrol->value.enumerated.item[0] > e->items - 1)
612 gbvalue.value.enumerated_item[0] =
613 cpu_to_le32(ucontrol->value.enumerated.item[0]);
615 if (e->shift_l != e->shift_r) {
616 if (ucontrol->value.enumerated.item[1] > e->items - 1)
618 gbvalue.value.enumerated_item[1] =
619 cpu_to_le32(ucontrol->value.enumerated.item[1]);
622 bundle = to_gb_bundle(module->dev);
624 ret = gb_pm_runtime_get_sync(bundle);
628 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
629 GB_AUDIO_INVALID_INDEX, &gbvalue);
631 gb_pm_runtime_put_autosuspend(bundle);
634 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n",
635 ret, __func__, kcontrol->id.name);
641 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
642 struct snd_kcontrol_new *kctl,
643 struct gb_audio_control *ctl)
645 struct soc_enum *gbe;
646 struct gb_audio_enumerated *gb_enum;
649 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
653 gb_enum = &ctl->info.value.enumerated;
655 /* since count=1, and reg is dummy */
656 gbe->items = le32_to_cpu(gb_enum->items);
657 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
659 /* debug enum info */
660 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
661 le16_to_cpu(gb_enum->names_length));
662 for (i = 0; i < gbe->items; i++)
663 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
665 *kctl = (struct snd_kcontrol_new)
666 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
667 gbcodec_enum_ctl_put);
671 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
672 struct snd_kcontrol_new *kctl,
673 struct gb_audio_control *ctl)
676 struct gbaudio_ctl_pvt *ctldata;
678 switch (ctl->iface) {
679 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
680 switch (ctl->info.type) {
681 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
682 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
685 ctldata = devm_kzalloc(gb->dev,
686 sizeof(struct gbaudio_ctl_pvt),
690 ctldata->ctl_id = ctl->id;
691 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
692 ctldata->access = le32_to_cpu(ctl->access);
693 ctldata->vcount = ctl->count_values;
694 ctldata->info = &ctl->info;
695 *kctl = (struct snd_kcontrol_new)
696 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
705 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
709 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
710 struct snd_ctl_elem_value *ucontrol)
713 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
714 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
715 struct gbaudio_module_info *module;
716 struct gb_audio_ctl_elem_value gbvalue;
717 struct device *codec_dev = widget->dapm->dev;
718 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
719 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
720 struct gb_bundle *bundle;
722 module = find_gb_module(gb, kcontrol->id.name);
726 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
730 bundle = to_gb_bundle(module->dev);
732 ret = gb_pm_runtime_get_sync(bundle);
736 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
737 GB_AUDIO_INVALID_INDEX, &gbvalue);
739 gb_pm_runtime_put_autosuspend(bundle);
742 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
743 __func__, kcontrol->id.name);
747 ucontrol->value.enumerated.item[0] = le32_to_cpu(gbvalue.value.enumerated_item[0]);
748 if (e->shift_l != e->shift_r)
749 ucontrol->value.enumerated.item[1] =
750 le32_to_cpu(gbvalue.value.enumerated_item[1]);
755 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
756 struct snd_ctl_elem_value *ucontrol)
759 unsigned int val, mux, change;
761 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
762 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
763 struct gb_audio_ctl_elem_value gbvalue;
764 struct gbaudio_module_info *module;
765 struct device *codec_dev = widget->dapm->dev;
766 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
767 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
768 struct gb_bundle *bundle;
770 if (ucontrol->value.enumerated.item[0] > e->items - 1)
773 module = find_gb_module(gb, kcontrol->id.name);
777 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
782 bundle = to_gb_bundle(module->dev);
784 ret = gb_pm_runtime_get_sync(bundle);
788 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
789 GB_AUDIO_INVALID_INDEX, &gbvalue);
791 gb_pm_runtime_put_autosuspend(bundle);
794 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
795 __func__, kcontrol->id.name);
799 mux = ucontrol->value.enumerated.item[0];
800 val = mux << e->shift_l;
801 mask = e->mask << e->shift_l;
803 if (le32_to_cpu(gbvalue.value.enumerated_item[0]) !=
804 ucontrol->value.enumerated.item[0]) {
806 gbvalue.value.enumerated_item[0] =
807 cpu_to_le32(ucontrol->value.enumerated.item[0]);
810 if (e->shift_l != e->shift_r) {
811 if (ucontrol->value.enumerated.item[1] > e->items - 1)
813 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
814 mask |= e->mask << e->shift_r;
815 if (le32_to_cpu(gbvalue.value.enumerated_item[1]) !=
816 ucontrol->value.enumerated.item[1]) {
818 gbvalue.value.enumerated_item[1] =
819 cpu_to_le32(ucontrol->value.enumerated.item[1]);
824 ret = gb_pm_runtime_get_sync(bundle);
828 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
829 GB_AUDIO_INVALID_INDEX, &gbvalue);
831 gb_pm_runtime_put_autosuspend(bundle);
834 dev_err_ratelimited(codec_dev,
835 "%d:Error in %s for %s\n", ret,
836 __func__, kcontrol->id.name);
838 for (wi = 0; wi < wlist->num_widgets; wi++) {
839 widget = wlist->widgets[wi];
840 snd_soc_dapm_mux_update_power(widget->dapm, kcontrol,
848 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
849 struct snd_kcontrol_new *kctl,
850 struct gb_audio_control *ctl)
852 struct soc_enum *gbe;
853 struct gb_audio_enumerated *gb_enum;
856 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
860 gb_enum = &ctl->info.value.enumerated;
862 /* since count=1, and reg is dummy */
863 gbe->items = le32_to_cpu(gb_enum->items);
864 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
866 /* debug enum info */
867 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
868 le16_to_cpu(gb_enum->names_length));
869 for (i = 0; i < gbe->items; i++)
870 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
872 *kctl = (struct snd_kcontrol_new)
873 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
874 gbcodec_enum_dapm_ctl_put);
878 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
879 struct snd_kcontrol_new *kctl,
880 struct gb_audio_control *ctl)
882 struct gbaudio_ctl_pvt *ctldata;
884 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
888 ctldata->ctl_id = ctl->id;
889 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
890 ctldata->access = le32_to_cpu(ctl->access);
891 ctldata->vcount = ctl->count_values;
892 ctldata->info = &ctl->info;
893 *kctl = (struct snd_kcontrol_new)
894 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
899 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
900 struct snd_kcontrol_new *kctl,
901 struct gb_audio_control *ctl)
905 switch (ctl->iface) {
906 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
907 switch (ctl->info.type) {
908 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
909 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
912 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
920 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
925 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
926 struct snd_kcontrol *kcontrol, int event)
930 struct device *codec_dev = w->dapm->dev;
931 struct gbaudio_codec_info *gbcodec = dev_get_drvdata(codec_dev);
932 struct gbaudio_module_info *module;
933 struct gb_bundle *bundle;
935 dev_dbg(codec_dev, "%s %s %d\n", __func__, w->name, event);
937 /* Find relevant module */
938 module = find_gb_module(gbcodec, w->name);
942 /* map name to widget id */
943 wid = gbaudio_map_widgetname(module, w->name);
945 dev_err(codec_dev, "Invalid widget name:%s\n", w->name);
949 bundle = to_gb_bundle(module->dev);
951 ret = gb_pm_runtime_get_sync(bundle);
956 case SND_SOC_DAPM_PRE_PMU:
957 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
959 ret = gbaudio_module_update(gbcodec, w, module, 1);
961 case SND_SOC_DAPM_POST_PMD:
962 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
964 ret = gbaudio_module_update(gbcodec, w, module, 0);
968 dev_err_ratelimited(codec_dev,
969 "%d: widget, event:%d failed:%d\n", wid,
972 gb_pm_runtime_put_autosuspend(bundle);
977 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
978 struct snd_soc_dapm_widget *dw,
979 struct gb_audio_widget *w, int *w_size)
982 struct snd_kcontrol_new *widget_kctls;
983 struct gb_audio_control *curr;
984 struct gbaudio_control *control, *_control;
986 char temp_name[NAME_SIZE];
988 ret = gbaudio_validate_kcontrol_count(w);
990 dev_err(module->dev, "Invalid kcontrol count=%d for %s\n",
991 w->ncontrols, w->name);
995 /* allocate memory for kcontrol */
997 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
998 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1003 *w_size = sizeof(struct gb_audio_widget);
1005 /* create relevant kcontrols */
1007 for (i = 0; i < w->ncontrols; i++) {
1008 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1011 dev_err(module->dev,
1012 "%s:%d type widget_ctl not supported\n",
1013 curr->name, curr->iface);
1016 control = devm_kzalloc(module->dev,
1017 sizeof(struct gbaudio_control),
1023 control->id = curr->id;
1024 control->name = curr->name;
1025 control->wname = w->name;
1027 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1028 struct gb_audio_enumerated *gbenum =
1029 &curr->info.value.enumerated;
1031 csize = offsetof(struct gb_audio_control, info);
1032 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1033 csize += offsetof(struct gb_audio_enumerated, names);
1034 csize += le16_to_cpu(gbenum->names_length);
1035 control->texts = (const char * const *)
1036 gb_generate_enum_strings(module, gbenum);
1037 control->items = le32_to_cpu(gbenum->items);
1039 csize = sizeof(struct gb_audio_control);
1043 curr = (void *)curr + csize;
1044 list_add(&control->list, &module->widget_ctl_list);
1045 dev_dbg(module->dev, "%s: control of type %d created\n",
1046 widget_kctls[i].name, widget_kctls[i].iface);
1049 /* Prefix dev_id to widget control_name */
1050 strscpy(temp_name, w->name, sizeof(temp_name));
1051 snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name);
1054 case snd_soc_dapm_spk:
1055 *dw = (struct snd_soc_dapm_widget)
1056 SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1057 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1059 case snd_soc_dapm_hp:
1060 *dw = (struct snd_soc_dapm_widget)
1061 SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1062 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1063 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1064 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1066 case snd_soc_dapm_mic:
1067 *dw = (struct snd_soc_dapm_widget)
1068 SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1069 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1071 case snd_soc_dapm_output:
1072 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1074 case snd_soc_dapm_input:
1075 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1077 case snd_soc_dapm_switch:
1078 *dw = (struct snd_soc_dapm_widget)
1079 SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1081 gbaudio_widget_event,
1082 SND_SOC_DAPM_PRE_PMU |
1083 SND_SOC_DAPM_POST_PMD);
1085 case snd_soc_dapm_pga:
1086 *dw = (struct snd_soc_dapm_widget)
1087 SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1088 gbaudio_widget_event,
1089 SND_SOC_DAPM_PRE_PMU |
1090 SND_SOC_DAPM_POST_PMD);
1092 case snd_soc_dapm_mixer:
1093 *dw = (struct snd_soc_dapm_widget)
1094 SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1095 0, gbaudio_widget_event,
1096 SND_SOC_DAPM_PRE_PMU |
1097 SND_SOC_DAPM_POST_PMD);
1099 case snd_soc_dapm_mux:
1100 *dw = (struct snd_soc_dapm_widget)
1101 SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1102 widget_kctls, gbaudio_widget_event,
1103 SND_SOC_DAPM_PRE_PMU |
1104 SND_SOC_DAPM_POST_PMD);
1106 case snd_soc_dapm_aif_in:
1107 *dw = (struct snd_soc_dapm_widget)
1108 SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1110 0, 0, gbaudio_widget_event,
1111 SND_SOC_DAPM_PRE_PMU |
1112 SND_SOC_DAPM_POST_PMD);
1114 case snd_soc_dapm_aif_out:
1115 *dw = (struct snd_soc_dapm_widget)
1116 SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1118 0, 0, gbaudio_widget_event,
1119 SND_SOC_DAPM_PRE_PMU |
1120 SND_SOC_DAPM_POST_PMD);
1127 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1131 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1133 list_del(&control->list);
1134 devm_kfree(module->dev, control);
1139 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1140 struct gb_audio_control *controls)
1143 struct snd_kcontrol_new *dapm_kctls;
1144 struct gb_audio_control *curr;
1145 struct gbaudio_control *control, *_control;
1147 char temp_name[NAME_SIZE];
1149 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1150 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1155 for (i = 0; i < module->num_controls; i++) {
1156 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1159 dev_err(module->dev, "%s:%d type not supported\n",
1160 curr->name, curr->iface);
1163 control = devm_kzalloc(module->dev, sizeof(struct
1170 control->id = curr->id;
1171 /* Prefix dev_id to widget_name */
1172 strscpy(temp_name, curr->name, sizeof(temp_name));
1173 snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id,
1175 control->name = curr->name;
1176 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1177 struct gb_audio_enumerated *gbenum =
1178 &curr->info.value.enumerated;
1180 csize = offsetof(struct gb_audio_control, info);
1181 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1182 csize += offsetof(struct gb_audio_enumerated, names);
1183 csize += le16_to_cpu(gbenum->names_length);
1184 control->texts = (const char * const *)
1185 gb_generate_enum_strings(module, gbenum);
1186 control->items = le32_to_cpu(gbenum->items);
1188 csize = sizeof(struct gb_audio_control);
1191 list_add(&control->list, &module->ctl_list);
1192 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1193 curr->name, curr->info.type);
1194 curr = (void *)curr + csize;
1196 module->controls = dapm_kctls;
1200 list_for_each_entry_safe(control, _control, &module->ctl_list,
1202 list_del(&control->list);
1203 devm_kfree(module->dev, control);
1205 devm_kfree(module->dev, dapm_kctls);
1209 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1210 struct gb_audio_widget *widgets)
1213 struct snd_soc_dapm_widget *dapm_widgets;
1214 struct gb_audio_widget *curr;
1215 struct gbaudio_widget *widget, *_widget;
1218 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1219 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1224 for (i = 0; i < module->num_dapm_widgets; i++) {
1225 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1228 dev_err(module->dev, "%s:%d type not supported\n",
1229 curr->name, curr->type);
1232 widget = devm_kzalloc(module->dev, sizeof(struct
1239 widget->id = curr->id;
1240 widget->name = curr->name;
1241 list_add(&widget->list, &module->widget_list);
1242 curr = (void *)curr + w_size;
1244 module->dapm_widgets = dapm_widgets;
1249 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1251 list_del(&widget->list);
1252 devm_kfree(module->dev, widget);
1254 devm_kfree(module->dev, dapm_widgets);
1258 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1259 struct gb_audio_route *routes)
1262 struct snd_soc_dapm_route *dapm_routes;
1263 struct gb_audio_route *curr;
1266 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1267 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1271 module->dapm_routes = dapm_routes;
1274 for (i = 0; i < module->num_dapm_routes; i++) {
1276 gbaudio_map_widgetid(module, curr->destination_id);
1277 if (!dapm_routes->sink) {
1278 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1279 curr->source_id, curr->destination_id,
1280 curr->control_id, curr->index);
1284 dapm_routes->source =
1285 gbaudio_map_widgetid(module, curr->source_id);
1286 if (!dapm_routes->source) {
1287 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1288 curr->source_id, curr->destination_id,
1289 curr->control_id, curr->index);
1293 dapm_routes->control =
1294 gbaudio_map_controlid(module,
1297 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1298 !dapm_routes->control) {
1299 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1300 curr->source_id, curr->destination_id,
1301 curr->control_id, curr->index);
1305 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1306 (dapm_routes->control) ? dapm_routes->control : "NULL",
1307 dapm_routes->source);
1315 devm_kfree(module->dev, module->dapm_routes);
1319 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1320 struct gb_audio_topology *tplg_data)
1322 /* fetch no. of kcontrols, widgets & routes */
1323 module->num_controls = tplg_data->num_controls;
1324 module->num_dapm_widgets = tplg_data->num_widgets;
1325 module->num_dapm_routes = tplg_data->num_routes;
1327 /* update block offset */
1328 module->dai_offset = (unsigned long)&tplg_data->data;
1329 module->control_offset = module->dai_offset +
1330 le32_to_cpu(tplg_data->size_dais);
1331 module->widget_offset = module->control_offset +
1332 le32_to_cpu(tplg_data->size_controls);
1333 module->route_offset = module->widget_offset +
1334 le32_to_cpu(tplg_data->size_widgets);
1336 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1337 dev_dbg(module->dev, "control offset is %lx\n",
1338 module->control_offset);
1339 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1340 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1345 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1346 struct gb_audio_topology *tplg_data)
1349 struct gb_audio_control *controls;
1350 struct gb_audio_widget *widgets;
1351 struct gb_audio_route *routes;
1352 unsigned int jack_type;
1357 ret = gbaudio_tplg_process_header(module, tplg_data);
1359 dev_err(module->dev, "%d: Error in parsing topology header\n",
1364 /* process control */
1365 controls = (struct gb_audio_control *)module->control_offset;
1366 ret = gbaudio_tplg_process_kcontrols(module, controls);
1368 dev_err(module->dev,
1369 "%d: Error in parsing controls data\n", ret);
1372 dev_dbg(module->dev, "Control parsing finished\n");
1374 /* process widgets */
1375 widgets = (struct gb_audio_widget *)module->widget_offset;
1376 ret = gbaudio_tplg_process_widgets(module, widgets);
1378 dev_err(module->dev,
1379 "%d: Error in parsing widgets data\n", ret);
1382 dev_dbg(module->dev, "Widget parsing finished\n");
1385 routes = (struct gb_audio_route *)module->route_offset;
1386 ret = gbaudio_tplg_process_routes(module, routes);
1388 dev_err(module->dev,
1389 "%d: Error in parsing routes data\n", ret);
1392 dev_dbg(module->dev, "Route parsing finished\n");
1394 /* parse jack capabilities */
1395 jack_type = le32_to_cpu(tplg_data->jack_type);
1397 module->jack_mask = jack_type & GBCODEC_JACK_MASK;
1398 module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK;
1404 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1406 struct gbaudio_control *control, *_control;
1407 struct gbaudio_widget *widget, *_widget;
1409 if (!module->topology)
1412 /* release kcontrols */
1413 list_for_each_entry_safe(control, _control, &module->ctl_list,
1415 list_del(&control->list);
1416 devm_kfree(module->dev, control);
1418 if (module->controls)
1419 devm_kfree(module->dev, module->controls);
1421 /* release widget controls */
1422 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1424 list_del(&control->list);
1425 devm_kfree(module->dev, control);
1428 /* release widgets */
1429 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1431 list_del(&widget->list);
1432 devm_kfree(module->dev, widget);
1434 if (module->dapm_widgets)
1435 devm_kfree(module->dev, module->dapm_widgets);
1437 /* release routes */
1438 if (module->dapm_routes)
1439 devm_kfree(module->dev, module->dapm_routes);