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(struct gbaudio_codec_info *codec,
31 char begin[NAME_SIZE];
32 struct gbaudio_module_info *module;
37 if (sscanf(name, "%s %d", begin, &dev_id) != 2)
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;
118 list_for_each_entry(widget, &module->widget_list, list) {
119 if (!strncmp(widget->name, name, NAME_SIZE))
122 dev_warn(module->dev, "%s: missing in modules widgets list\n", name);
127 static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module,
130 struct gbaudio_widget *widget;
132 list_for_each_entry(widget, &module->widget_list, list) {
133 if (widget->id == widget_id)
139 static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
140 struct gb_audio_enumerated *gbenum)
142 const char **strings;
147 items = le32_to_cpu(gbenum->items);
148 strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL);
152 data = gbenum->names;
154 for (i = 0; i < items; i++) {
155 strings[i] = (const char *)data;
156 while (*data != '\0')
164 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_info *uinfo)
169 struct gbaudio_ctl_pvt *data;
170 struct gb_audio_ctl_elem_info *info;
171 struct gbaudio_module_info *module;
172 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
173 struct gbaudio_codec_info *gbcodec = snd_soc_component_get_drvdata(comp);
175 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
176 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
177 info = (struct gb_audio_ctl_elem_info *)data->info;
180 dev_err(comp->dev, "NULL info for %s\n", uinfo->id.name);
185 uinfo->access = data->access;
186 uinfo->count = data->vcount;
187 uinfo->type = (__force snd_ctl_elem_type_t)info->type;
189 switch (info->type) {
190 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
191 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
192 uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
193 uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
195 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
196 max = le32_to_cpu(info->value.enumerated.items);
197 uinfo->value.enumerated.items = max;
198 if (uinfo->value.enumerated.item > max - 1)
199 uinfo->value.enumerated.item = max - 1;
200 module = find_gb_module(gbcodec, kcontrol->id.name);
203 name = gbaudio_map_controlid(module, data->ctl_id,
204 uinfo->value.enumerated.item);
205 strscpy(uinfo->value.enumerated.name, name, sizeof(uinfo->value.enumerated.name));
208 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
209 info->type, kcontrol->id.name);
215 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_value *ucontrol)
219 struct gb_audio_ctl_elem_info *info;
220 struct gbaudio_ctl_pvt *data;
221 struct gb_audio_ctl_elem_value gbvalue;
222 struct gbaudio_module_info *module;
223 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
224 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
225 struct gb_bundle *bundle;
227 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
228 module = find_gb_module(gb, kcontrol->id.name);
232 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
233 info = (struct gb_audio_ctl_elem_info *)data->info;
234 bundle = to_gb_bundle(module->dev);
236 ret = gb_pm_runtime_get_sync(bundle);
240 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
241 GB_AUDIO_INVALID_INDEX, &gbvalue);
243 gb_pm_runtime_put_autosuspend(bundle);
246 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
247 __func__, kcontrol->id.name);
251 /* update ucontrol */
252 switch (info->type) {
253 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
254 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
255 ucontrol->value.integer.value[0] =
256 le32_to_cpu(gbvalue.value.integer_value[0]);
257 if (data->vcount == 2)
258 ucontrol->value.integer.value[1] =
259 le32_to_cpu(gbvalue.value.integer_value[1]);
261 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
262 ucontrol->value.enumerated.item[0] =
263 le32_to_cpu(gbvalue.value.enumerated_item[0]);
264 if (data->vcount == 2)
265 ucontrol->value.enumerated.item[1] =
266 le32_to_cpu(gbvalue.value.enumerated_item[1]);
269 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
270 info->type, kcontrol->id.name);
277 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
278 struct snd_ctl_elem_value *ucontrol)
281 struct gb_audio_ctl_elem_info *info;
282 struct gbaudio_ctl_pvt *data;
283 struct gb_audio_ctl_elem_value gbvalue;
284 struct gbaudio_module_info *module;
285 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
286 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
287 struct gb_bundle *bundle;
289 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
290 module = find_gb_module(gb, kcontrol->id.name);
294 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
295 info = (struct gb_audio_ctl_elem_info *)data->info;
296 bundle = to_gb_bundle(module->dev);
298 /* update ucontrol */
299 switch (info->type) {
300 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
301 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
302 gbvalue.value.integer_value[0] =
303 cpu_to_le32(ucontrol->value.integer.value[0]);
304 if (data->vcount == 2)
305 gbvalue.value.integer_value[1] =
306 cpu_to_le32(ucontrol->value.integer.value[1]);
308 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
309 gbvalue.value.enumerated_item[0] =
310 cpu_to_le32(ucontrol->value.enumerated.item[0]);
311 if (data->vcount == 2)
312 gbvalue.value.enumerated_item[1] =
313 cpu_to_le32(ucontrol->value.enumerated.item[1]);
316 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
317 info->type, kcontrol->id.name);
325 ret = gb_pm_runtime_get_sync(bundle);
329 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
330 GB_AUDIO_INVALID_INDEX, &gbvalue);
332 gb_pm_runtime_put_autosuspend(bundle);
335 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
336 __func__, kcontrol->id.name);
342 #define SOC_MIXER_GB(xname, kcount, data) \
343 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
344 .count = kcount, .info = gbcodec_mixer_ctl_info, \
345 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
346 .private_value = (unsigned long)data }
349 * although below callback functions seems redundant to above functions.
350 * same are kept to allow provision for different handling in case
351 * of DAPM related sequencing, etc.
353 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
354 struct snd_ctl_elem_info *uinfo)
356 int platform_max, platform_min;
357 struct gbaudio_ctl_pvt *data;
358 struct gb_audio_ctl_elem_info *info;
360 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
361 info = (struct gb_audio_ctl_elem_info *)data->info;
364 platform_max = le32_to_cpu(info->value.integer.max);
365 platform_min = le32_to_cpu(info->value.integer.min);
367 if (platform_max == 1 &&
368 !strnstr(kcontrol->id.name, " Volume", sizeof(kcontrol->id.name)))
369 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
373 uinfo->count = data->vcount;
374 uinfo->value.integer.min = platform_min;
375 uinfo->value.integer.max = platform_max;
380 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
381 struct snd_ctl_elem_value *ucontrol)
384 struct gbaudio_ctl_pvt *data;
385 struct gb_audio_ctl_elem_value gbvalue;
386 struct gbaudio_module_info *module;
387 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
388 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
389 struct device *codec_dev = widget->dapm->dev;
390 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
391 struct gb_bundle *bundle;
393 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
394 module = find_gb_module(gb, kcontrol->id.name);
398 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
399 bundle = to_gb_bundle(module->dev);
401 if (data->vcount == 2)
402 dev_warn(widget->dapm->dev,
403 "GB: Control '%s' is stereo, which is not supported\n",
406 ret = gb_pm_runtime_get_sync(bundle);
410 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
411 GB_AUDIO_INVALID_INDEX, &gbvalue);
413 gb_pm_runtime_put_autosuspend(bundle);
416 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
417 __func__, kcontrol->id.name);
420 /* update ucontrol */
421 ucontrol->value.integer.value[0] =
422 le32_to_cpu(gbvalue.value.integer_value[0]);
427 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
428 struct snd_ctl_elem_value *ucontrol)
430 int ret, wi, max, connect;
431 unsigned int mask, val;
432 struct gb_audio_ctl_elem_info *info;
433 struct gbaudio_ctl_pvt *data;
434 struct gb_audio_ctl_elem_value gbvalue;
435 struct gbaudio_module_info *module;
436 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
437 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
438 struct device *codec_dev = widget->dapm->dev;
439 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
440 struct gb_bundle *bundle;
442 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
443 module = find_gb_module(gb, kcontrol->id.name);
447 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
448 info = (struct gb_audio_ctl_elem_info *)data->info;
449 bundle = to_gb_bundle(module->dev);
451 if (data->vcount == 2)
452 dev_warn(widget->dapm->dev,
453 "GB: Control '%s' is stereo, which is not supported\n",
456 max = le32_to_cpu(info->value.integer.max);
457 mask = (1 << fls(max)) - 1;
458 val = ucontrol->value.integer.value[0] & mask;
461 ret = gb_pm_runtime_get_sync(bundle);
465 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
466 GB_AUDIO_INVALID_INDEX, &gbvalue);
470 /* update ucontrol */
471 if (le32_to_cpu(gbvalue.value.integer_value[0]) != val) {
472 for (wi = 0; wi < wlist->num_widgets; wi++) {
473 widget = wlist->widgets[wi];
474 snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol,
477 gbvalue.value.integer_value[0] =
478 cpu_to_le32(ucontrol->value.integer.value[0]);
480 ret = gb_audio_gb_set_control(module->mgmt_connection,
482 GB_AUDIO_INVALID_INDEX, &gbvalue);
486 gb_pm_runtime_put_autosuspend(bundle);
488 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
489 __func__, kcontrol->id.name);
493 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
494 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
495 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
496 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
497 .private_value = (unsigned long)data}
499 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
500 struct snd_kcontrol *k, int event)
502 /* Ensure GB speaker is connected */
507 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
508 struct snd_kcontrol *k, int event)
510 /* Ensure GB module supports jack slot */
515 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
516 struct snd_kcontrol *k, int event)
518 /* Ensure GB module supports jack slot */
523 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
528 case snd_soc_dapm_spk:
529 case snd_soc_dapm_hp:
530 case snd_soc_dapm_mic:
531 case snd_soc_dapm_output:
532 case snd_soc_dapm_input:
536 case snd_soc_dapm_switch:
537 case snd_soc_dapm_mux:
538 if (w->ncontrols != 1)
548 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
549 struct snd_ctl_elem_value *ucontrol)
552 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
553 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
554 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
555 struct gb_audio_ctl_elem_value gbvalue;
556 struct gbaudio_module_info *module;
557 struct gb_bundle *bundle;
559 module = find_gb_module(gb, kcontrol->id.name);
563 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
567 bundle = to_gb_bundle(module->dev);
569 ret = gb_pm_runtime_get_sync(bundle);
573 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
574 GB_AUDIO_INVALID_INDEX, &gbvalue);
576 gb_pm_runtime_put_autosuspend(bundle);
579 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
580 __func__, kcontrol->id.name);
584 ucontrol->value.enumerated.item[0] =
585 le32_to_cpu(gbvalue.value.enumerated_item[0]);
586 if (e->shift_l != e->shift_r)
587 ucontrol->value.enumerated.item[1] =
588 le32_to_cpu(gbvalue.value.enumerated_item[1]);
593 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
594 struct snd_ctl_elem_value *ucontrol)
597 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
598 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
599 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
600 struct gb_audio_ctl_elem_value gbvalue;
601 struct gbaudio_module_info *module;
602 struct gb_bundle *bundle;
604 module = find_gb_module(gb, kcontrol->id.name);
608 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
612 if (ucontrol->value.enumerated.item[0] > e->items - 1)
614 gbvalue.value.enumerated_item[0] =
615 cpu_to_le32(ucontrol->value.enumerated.item[0]);
617 if (e->shift_l != e->shift_r) {
618 if (ucontrol->value.enumerated.item[1] > e->items - 1)
620 gbvalue.value.enumerated_item[1] =
621 cpu_to_le32(ucontrol->value.enumerated.item[1]);
624 bundle = to_gb_bundle(module->dev);
626 ret = gb_pm_runtime_get_sync(bundle);
630 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
631 GB_AUDIO_INVALID_INDEX, &gbvalue);
633 gb_pm_runtime_put_autosuspend(bundle);
636 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n",
637 ret, __func__, kcontrol->id.name);
643 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
644 struct snd_kcontrol_new *kctl,
645 struct gb_audio_control *ctl)
647 struct soc_enum *gbe;
648 struct gb_audio_enumerated *gb_enum;
651 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
655 gb_enum = &ctl->info.value.enumerated;
657 /* since count=1, and reg is dummy */
658 gbe->items = le32_to_cpu(gb_enum->items);
659 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
663 /* debug enum info */
664 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
665 le16_to_cpu(gb_enum->names_length));
666 for (i = 0; i < gbe->items; i++)
667 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
669 *kctl = (struct snd_kcontrol_new)
670 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
671 gbcodec_enum_ctl_put);
675 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
676 struct snd_kcontrol_new *kctl,
677 struct gb_audio_control *ctl)
680 struct gbaudio_ctl_pvt *ctldata;
682 switch (ctl->iface) {
683 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
684 switch (ctl->info.type) {
685 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
686 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
689 ctldata = devm_kzalloc(gb->dev,
690 sizeof(struct gbaudio_ctl_pvt),
694 ctldata->ctl_id = ctl->id;
695 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
696 ctldata->access = le32_to_cpu(ctl->access);
697 ctldata->vcount = ctl->count_values;
698 ctldata->info = &ctl->info;
699 *kctl = (struct snd_kcontrol_new)
700 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
709 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
713 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
714 struct snd_ctl_elem_value *ucontrol)
717 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
718 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
719 struct gbaudio_module_info *module;
720 struct gb_audio_ctl_elem_value gbvalue;
721 struct device *codec_dev = widget->dapm->dev;
722 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
723 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
724 struct gb_bundle *bundle;
726 module = find_gb_module(gb, kcontrol->id.name);
730 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
734 bundle = to_gb_bundle(module->dev);
736 ret = gb_pm_runtime_get_sync(bundle);
740 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
741 GB_AUDIO_INVALID_INDEX, &gbvalue);
743 gb_pm_runtime_put_autosuspend(bundle);
746 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
747 __func__, kcontrol->id.name);
751 ucontrol->value.enumerated.item[0] = le32_to_cpu(gbvalue.value.enumerated_item[0]);
752 if (e->shift_l != e->shift_r)
753 ucontrol->value.enumerated.item[1] =
754 le32_to_cpu(gbvalue.value.enumerated_item[1]);
759 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
760 struct snd_ctl_elem_value *ucontrol)
763 unsigned int val, mux, change;
765 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
766 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
767 struct gb_audio_ctl_elem_value gbvalue;
768 struct gbaudio_module_info *module;
769 struct device *codec_dev = widget->dapm->dev;
770 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
771 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
772 struct gb_bundle *bundle;
774 if (ucontrol->value.enumerated.item[0] > e->items - 1)
777 module = find_gb_module(gb, kcontrol->id.name);
781 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
786 bundle = to_gb_bundle(module->dev);
788 ret = gb_pm_runtime_get_sync(bundle);
792 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
793 GB_AUDIO_INVALID_INDEX, &gbvalue);
795 gb_pm_runtime_put_autosuspend(bundle);
798 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
799 __func__, kcontrol->id.name);
803 mux = ucontrol->value.enumerated.item[0];
804 val = mux << e->shift_l;
805 mask = e->mask << e->shift_l;
807 if (le32_to_cpu(gbvalue.value.enumerated_item[0]) !=
808 ucontrol->value.enumerated.item[0]) {
810 gbvalue.value.enumerated_item[0] =
811 cpu_to_le32(ucontrol->value.enumerated.item[0]);
814 if (e->shift_l != e->shift_r) {
815 if (ucontrol->value.enumerated.item[1] > e->items - 1)
817 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
818 mask |= e->mask << e->shift_r;
819 if (le32_to_cpu(gbvalue.value.enumerated_item[1]) !=
820 ucontrol->value.enumerated.item[1]) {
822 gbvalue.value.enumerated_item[1] =
823 cpu_to_le32(ucontrol->value.enumerated.item[1]);
828 ret = gb_pm_runtime_get_sync(bundle);
832 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
833 GB_AUDIO_INVALID_INDEX, &gbvalue);
835 gb_pm_runtime_put_autosuspend(bundle);
838 dev_err_ratelimited(codec_dev,
839 "%d:Error in %s for %s\n", ret,
840 __func__, kcontrol->id.name);
842 for (wi = 0; wi < wlist->num_widgets; wi++) {
843 widget = wlist->widgets[wi];
844 snd_soc_dapm_mux_update_power(widget->dapm, kcontrol,
852 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
853 struct snd_kcontrol_new *kctl,
854 struct gb_audio_control *ctl)
856 struct soc_enum *gbe;
857 struct gb_audio_enumerated *gb_enum;
860 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
864 gb_enum = &ctl->info.value.enumerated;
866 /* since count=1, and reg is dummy */
867 gbe->items = le32_to_cpu(gb_enum->items);
868 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
872 /* debug enum info */
873 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
874 le16_to_cpu(gb_enum->names_length));
875 for (i = 0; i < gbe->items; i++)
876 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
878 *kctl = (struct snd_kcontrol_new)
879 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
880 gbcodec_enum_dapm_ctl_put);
884 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
885 struct snd_kcontrol_new *kctl,
886 struct gb_audio_control *ctl)
888 struct gbaudio_ctl_pvt *ctldata;
890 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
894 ctldata->ctl_id = ctl->id;
895 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
896 ctldata->access = le32_to_cpu(ctl->access);
897 ctldata->vcount = ctl->count_values;
898 ctldata->info = &ctl->info;
899 *kctl = (struct snd_kcontrol_new)
900 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
905 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
906 struct snd_kcontrol_new *kctl,
907 struct gb_audio_control *ctl)
911 switch (ctl->iface) {
912 case (__force int)SNDRV_CTL_ELEM_IFACE_MIXER:
913 switch (ctl->info.type) {
914 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
915 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
918 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
926 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
931 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
932 struct snd_kcontrol *kcontrol, int event)
936 struct device *codec_dev = w->dapm->dev;
937 struct gbaudio_codec_info *gbcodec = dev_get_drvdata(codec_dev);
938 struct gbaudio_module_info *module;
939 struct gb_bundle *bundle;
941 dev_dbg(codec_dev, "%s %s %d\n", __func__, w->name, event);
943 /* Find relevant module */
944 module = find_gb_module(gbcodec, w->name);
948 /* map name to widget id */
949 wid = gbaudio_map_widgetname(module, w->name);
951 dev_err(codec_dev, "Invalid widget name:%s\n", w->name);
955 bundle = to_gb_bundle(module->dev);
957 ret = gb_pm_runtime_get_sync(bundle);
962 case SND_SOC_DAPM_PRE_PMU:
963 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
965 ret = gbaudio_module_update(gbcodec, w, module, 1);
967 case SND_SOC_DAPM_POST_PMD:
968 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
970 ret = gbaudio_module_update(gbcodec, w, module, 0);
974 dev_err_ratelimited(codec_dev,
975 "%d: widget, event:%d failed:%d\n", wid,
978 gb_pm_runtime_put_autosuspend(bundle);
983 static const struct snd_soc_dapm_widget gbaudio_widgets[] = {
984 [snd_soc_dapm_spk] = SND_SOC_DAPM_SPK(NULL, gbcodec_event_spk),
985 [snd_soc_dapm_hp] = SND_SOC_DAPM_HP(NULL, gbcodec_event_hp),
986 [snd_soc_dapm_mic] = SND_SOC_DAPM_MIC(NULL, gbcodec_event_int_mic),
987 [snd_soc_dapm_output] = SND_SOC_DAPM_OUTPUT(NULL),
988 [snd_soc_dapm_input] = SND_SOC_DAPM_INPUT(NULL),
989 [snd_soc_dapm_switch] = SND_SOC_DAPM_SWITCH_E(NULL, SND_SOC_NOPM,
991 gbaudio_widget_event,
992 SND_SOC_DAPM_PRE_PMU |
993 SND_SOC_DAPM_POST_PMD),
994 [snd_soc_dapm_pga] = SND_SOC_DAPM_PGA_E(NULL, SND_SOC_NOPM,
996 gbaudio_widget_event,
997 SND_SOC_DAPM_PRE_PMU |
998 SND_SOC_DAPM_POST_PMD),
999 [snd_soc_dapm_mixer] = SND_SOC_DAPM_MIXER_E(NULL, SND_SOC_NOPM,
1001 gbaudio_widget_event,
1002 SND_SOC_DAPM_PRE_PMU |
1003 SND_SOC_DAPM_POST_PMD),
1004 [snd_soc_dapm_mux] = SND_SOC_DAPM_MUX_E(NULL, SND_SOC_NOPM,
1006 gbaudio_widget_event,
1007 SND_SOC_DAPM_PRE_PMU |
1008 SND_SOC_DAPM_POST_PMD),
1009 [snd_soc_dapm_aif_in] = SND_SOC_DAPM_AIF_IN_E(NULL, NULL, 0,
1011 gbaudio_widget_event,
1012 SND_SOC_DAPM_PRE_PMU |
1013 SND_SOC_DAPM_POST_PMD),
1014 [snd_soc_dapm_aif_out] = SND_SOC_DAPM_AIF_OUT_E(NULL, NULL, 0,
1016 gbaudio_widget_event,
1017 SND_SOC_DAPM_PRE_PMU |
1018 SND_SOC_DAPM_POST_PMD),
1021 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
1022 struct snd_soc_dapm_widget *dw,
1023 struct gb_audio_widget *w, int *w_size)
1026 struct snd_kcontrol_new *widget_kctls;
1027 struct gb_audio_control *curr;
1028 struct gbaudio_control *control, *_control;
1030 char temp_name[NAME_SIZE];
1032 ret = gbaudio_validate_kcontrol_count(w);
1034 dev_err(module->dev, "Invalid kcontrol count=%d for %s\n",
1035 w->ncontrols, w->name);
1039 /* allocate memory for kcontrol */
1041 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1042 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1047 *w_size = sizeof(struct gb_audio_widget);
1049 /* create relevant kcontrols */
1051 for (i = 0; i < w->ncontrols; i++) {
1052 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1055 dev_err(module->dev,
1056 "%s:%d type widget_ctl not supported\n",
1057 curr->name, curr->iface);
1060 control = devm_kzalloc(module->dev,
1061 sizeof(struct gbaudio_control),
1067 control->id = curr->id;
1068 control->name = curr->name;
1069 control->wname = w->name;
1071 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1072 struct gb_audio_enumerated *gbenum =
1073 &curr->info.value.enumerated;
1075 csize = offsetof(struct gb_audio_control, info);
1076 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1077 csize += offsetof(struct gb_audio_enumerated, names);
1078 csize += le16_to_cpu(gbenum->names_length);
1079 control->texts = (const char * const *)
1080 gb_generate_enum_strings(module, gbenum);
1081 if (!control->texts) {
1085 control->items = le32_to_cpu(gbenum->items);
1087 csize = sizeof(struct gb_audio_control);
1091 curr = (void *)curr + csize;
1092 list_add(&control->list, &module->widget_ctl_list);
1093 dev_dbg(module->dev, "%s: control of type %d created\n",
1094 widget_kctls[i].name, widget_kctls[i].iface);
1097 /* Prefix dev_id to widget control_name */
1098 strscpy(temp_name, w->name, sizeof(temp_name));
1099 snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name);
1102 case snd_soc_dapm_spk:
1103 *dw = gbaudio_widgets[w->type];
1104 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1106 case snd_soc_dapm_hp:
1107 *dw = gbaudio_widgets[w->type];
1108 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1109 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1110 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1112 case snd_soc_dapm_mic:
1113 *dw = gbaudio_widgets[w->type];
1114 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1116 case snd_soc_dapm_output:
1117 case snd_soc_dapm_input:
1118 case snd_soc_dapm_switch:
1119 case snd_soc_dapm_pga:
1120 case snd_soc_dapm_mixer:
1121 case snd_soc_dapm_mux:
1122 *dw = gbaudio_widgets[w->type];
1124 case snd_soc_dapm_aif_in:
1125 case snd_soc_dapm_aif_out:
1126 *dw = gbaudio_widgets[w->type];
1127 dw->sname = w->sname;
1135 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1139 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1141 list_del(&control->list);
1142 devm_kfree(module->dev, control);
1147 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1148 struct gb_audio_control *controls)
1151 struct snd_kcontrol_new *dapm_kctls;
1152 struct gb_audio_control *curr;
1153 struct gbaudio_control *control, *_control;
1155 char temp_name[NAME_SIZE];
1157 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1158 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1163 for (i = 0; i < module->num_controls; i++) {
1164 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1167 dev_err(module->dev, "%s:%d type not supported\n",
1168 curr->name, curr->iface);
1171 control = devm_kzalloc(module->dev, sizeof(struct
1178 control->id = curr->id;
1179 /* Prefix dev_id to widget_name */
1180 strscpy(temp_name, curr->name, sizeof(temp_name));
1181 snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id,
1183 control->name = curr->name;
1184 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1185 struct gb_audio_enumerated *gbenum =
1186 &curr->info.value.enumerated;
1188 csize = offsetof(struct gb_audio_control, info);
1189 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1190 csize += offsetof(struct gb_audio_enumerated, names);
1191 csize += le16_to_cpu(gbenum->names_length);
1192 control->texts = (const char * const *)
1193 gb_generate_enum_strings(module, gbenum);
1194 if (!control->texts) {
1198 control->items = le32_to_cpu(gbenum->items);
1200 csize = sizeof(struct gb_audio_control);
1203 list_add(&control->list, &module->ctl_list);
1204 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1205 curr->name, curr->info.type);
1206 curr = (void *)curr + csize;
1208 module->controls = dapm_kctls;
1212 list_for_each_entry_safe(control, _control, &module->ctl_list,
1214 list_del(&control->list);
1215 devm_kfree(module->dev, control);
1217 devm_kfree(module->dev, dapm_kctls);
1221 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1222 struct gb_audio_widget *widgets)
1225 struct snd_soc_dapm_widget *dapm_widgets;
1226 struct gb_audio_widget *curr;
1227 struct gbaudio_widget *widget, *_widget;
1230 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1231 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1236 for (i = 0; i < module->num_dapm_widgets; i++) {
1237 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1240 dev_err(module->dev, "%s:%d type not supported\n",
1241 curr->name, curr->type);
1244 widget = devm_kzalloc(module->dev, sizeof(struct
1251 widget->id = curr->id;
1252 widget->name = curr->name;
1253 list_add(&widget->list, &module->widget_list);
1254 curr = (void *)curr + w_size;
1256 module->dapm_widgets = dapm_widgets;
1261 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1263 list_del(&widget->list);
1264 devm_kfree(module->dev, widget);
1266 devm_kfree(module->dev, dapm_widgets);
1270 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1271 struct gb_audio_route *routes)
1274 struct snd_soc_dapm_route *dapm_routes;
1275 struct gb_audio_route *curr;
1278 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1279 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1283 module->dapm_routes = dapm_routes;
1286 for (i = 0; i < module->num_dapm_routes; i++) {
1288 gbaudio_map_widgetid(module, curr->destination_id);
1289 if (!dapm_routes->sink) {
1290 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1291 curr->source_id, curr->destination_id,
1292 curr->control_id, curr->index);
1296 dapm_routes->source =
1297 gbaudio_map_widgetid(module, curr->source_id);
1298 if (!dapm_routes->source) {
1299 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1300 curr->source_id, curr->destination_id,
1301 curr->control_id, curr->index);
1305 dapm_routes->control =
1306 gbaudio_map_controlid(module,
1309 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1310 !dapm_routes->control) {
1311 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1312 curr->source_id, curr->destination_id,
1313 curr->control_id, curr->index);
1317 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1318 (dapm_routes->control) ? dapm_routes->control : "NULL",
1319 dapm_routes->source);
1327 devm_kfree(module->dev, module->dapm_routes);
1331 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1332 struct gb_audio_topology *tplg_data)
1334 /* fetch no. of kcontrols, widgets & routes */
1335 module->num_controls = tplg_data->num_controls;
1336 module->num_dapm_widgets = tplg_data->num_widgets;
1337 module->num_dapm_routes = tplg_data->num_routes;
1339 /* update block offset */
1340 module->dai_offset = (unsigned long)&tplg_data->data;
1341 module->control_offset = module->dai_offset +
1342 le32_to_cpu(tplg_data->size_dais);
1343 module->widget_offset = module->control_offset +
1344 le32_to_cpu(tplg_data->size_controls);
1345 module->route_offset = module->widget_offset +
1346 le32_to_cpu(tplg_data->size_widgets);
1348 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1349 dev_dbg(module->dev, "control offset is %lx\n",
1350 module->control_offset);
1351 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1352 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1357 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1358 struct gb_audio_topology *tplg_data)
1361 struct gb_audio_control *controls;
1362 struct gb_audio_widget *widgets;
1363 struct gb_audio_route *routes;
1364 unsigned int jack_type;
1369 ret = gbaudio_tplg_process_header(module, tplg_data);
1371 dev_err(module->dev, "%d: Error in parsing topology header\n",
1376 /* process control */
1377 controls = (struct gb_audio_control *)module->control_offset;
1378 ret = gbaudio_tplg_process_kcontrols(module, controls);
1380 dev_err(module->dev,
1381 "%d: Error in parsing controls data\n", ret);
1384 dev_dbg(module->dev, "Control parsing finished\n");
1386 /* process widgets */
1387 widgets = (struct gb_audio_widget *)module->widget_offset;
1388 ret = gbaudio_tplg_process_widgets(module, widgets);
1390 dev_err(module->dev,
1391 "%d: Error in parsing widgets data\n", ret);
1394 dev_dbg(module->dev, "Widget parsing finished\n");
1397 routes = (struct gb_audio_route *)module->route_offset;
1398 ret = gbaudio_tplg_process_routes(module, routes);
1400 dev_err(module->dev,
1401 "%d: Error in parsing routes data\n", ret);
1404 dev_dbg(module->dev, "Route parsing finished\n");
1406 /* parse jack capabilities */
1407 jack_type = le32_to_cpu(tplg_data->jack_type);
1409 module->jack_mask = jack_type & GBCODEC_JACK_MASK;
1410 module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK;
1416 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1418 struct gbaudio_control *control, *_control;
1419 struct gbaudio_widget *widget, *_widget;
1421 if (!module->topology)
1424 /* release kcontrols */
1425 list_for_each_entry_safe(control, _control, &module->ctl_list,
1427 list_del(&control->list);
1428 devm_kfree(module->dev, control);
1430 if (module->controls)
1431 devm_kfree(module->dev, module->controls);
1433 /* release widget controls */
1434 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1436 list_del(&control->list);
1437 devm_kfree(module->dev, control);
1440 /* release widgets */
1441 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1443 list_del(&widget->list);
1444 devm_kfree(module->dev, widget);
1446 if (module->dapm_widgets)
1447 devm_kfree(module->dev, module->dapm_widgets);
1449 /* release routes */
1450 if (module->dapm_routes)
1451 devm_kfree(module->dev, module->dapm_routes);