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(codec->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 ret = gb_pm_runtime_get_sync(bundle);
469 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
470 GB_AUDIO_INVALID_INDEX, &gbvalue);
474 /* update ucontrol */
475 if (gbvalue.value.integer_value[0] != val) {
476 for (wi = 0; wi < wlist->num_widgets; wi++) {
477 widget = wlist->widgets[wi];
480 widget->dapm->update = NULL;
481 snd_soc_dapm_mixer_update_power(widget, kcontrol,
484 gbvalue.value.integer_value[0] =
485 ucontrol->value.integer.value[0];
487 ret = gb_audio_gb_set_control(module->mgmt_connection,
489 GB_AUDIO_INVALID_INDEX, &gbvalue);
493 gb_pm_runtime_put_autosuspend(bundle);
495 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
496 __func__, kcontrol->id.name);
500 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
501 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
502 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
503 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
504 .private_value = (unsigned long)data}
506 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
507 struct snd_kcontrol *k, int event)
509 /* Ensure GB speaker is connected */
514 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
515 struct snd_kcontrol *k, int event)
517 /* Ensure GB module supports jack slot */
522 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
523 struct snd_kcontrol *k, int event)
525 /* Ensure GB module supports jack slot */
530 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
535 case snd_soc_dapm_spk:
536 case snd_soc_dapm_hp:
537 case snd_soc_dapm_mic:
538 case snd_soc_dapm_output:
539 case snd_soc_dapm_input:
543 case snd_soc_dapm_switch:
544 case snd_soc_dapm_mux:
545 if (w->ncontrols != 1)
555 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
556 struct snd_ctl_elem_value *ucontrol)
559 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
560 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
561 struct gb_audio_ctl_elem_value gbvalue;
562 struct gbaudio_module_info *module;
563 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
564 struct gb_bundle *bundle;
566 module = find_gb_module(gb, kcontrol->id.name);
570 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
574 bundle = to_gb_bundle(module->dev);
576 ret = gb_pm_runtime_get_sync(bundle);
580 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
581 GB_AUDIO_INVALID_INDEX, &gbvalue);
583 gb_pm_runtime_put_autosuspend(bundle);
586 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
587 __func__, kcontrol->id.name);
591 ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
592 if (e->shift_l != e->shift_r)
593 ucontrol->value.enumerated.item[1] =
594 gbvalue.value.enumerated_item[1];
599 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
600 struct snd_ctl_elem_value *ucontrol)
603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
604 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
605 struct gb_audio_ctl_elem_value gbvalue;
606 struct gbaudio_module_info *module;
607 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
608 struct gb_bundle *bundle;
610 module = find_gb_module(gb, kcontrol->id.name);
614 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
618 if (ucontrol->value.enumerated.item[0] > e->max - 1)
620 gbvalue.value.enumerated_item[0] = ucontrol->value.enumerated.item[0];
622 if (e->shift_l != e->shift_r) {
623 if (ucontrol->value.enumerated.item[1] > e->max - 1)
625 gbvalue.value.enumerated_item[1] =
626 ucontrol->value.enumerated.item[1];
629 bundle = to_gb_bundle(module->dev);
631 ret = gb_pm_runtime_get_sync(bundle);
635 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
636 GB_AUDIO_INVALID_INDEX, &gbvalue);
638 gb_pm_runtime_put_autosuspend(bundle);
641 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
642 __func__, kcontrol->id.name);
648 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
649 struct snd_kcontrol_new *kctl,
650 struct gb_audio_control *ctl)
652 struct soc_enum *gbe;
653 struct gb_audio_enumerated *gb_enum;
656 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
660 gb_enum = &ctl->info.value.enumerated;
662 /* since count=1, and reg is dummy */
663 gbe->max = gb_enum->items;
664 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
666 /* debug enum info */
667 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gb_enum->items,
668 gb_enum->names_length);
669 for (i = 0; i < gb_enum->items; i++)
670 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
672 *kctl = (struct snd_kcontrol_new)
673 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
674 gbcodec_enum_ctl_put);
678 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
679 struct snd_kcontrol_new *kctl,
680 struct gb_audio_control *ctl)
683 struct gbaudio_ctl_pvt *ctldata;
685 switch (ctl->iface) {
686 case SNDRV_CTL_ELEM_IFACE_MIXER:
687 switch (ctl->info.type) {
688 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
689 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
692 ctldata = devm_kzalloc(gb->dev,
693 sizeof(struct gbaudio_ctl_pvt),
697 ctldata->ctl_id = ctl->id;
698 ctldata->data_cport = ctl->data_cport;
699 ctldata->access = ctl->access;
700 ctldata->vcount = ctl->count_values;
701 ctldata->info = &ctl->info;
702 *kctl = (struct snd_kcontrol_new)
703 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
712 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
716 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_value *ucontrol)
720 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
721 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
722 struct gbaudio_module_info *module;
723 struct gb_audio_ctl_elem_value gbvalue;
724 struct snd_soc_codec *codec = widget->codec;
725 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
726 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
727 struct gb_bundle *bundle;
729 module = find_gb_module(gb, kcontrol->id.name);
733 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
737 bundle = to_gb_bundle(module->dev);
739 ret = gb_pm_runtime_get_sync(bundle);
743 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
744 GB_AUDIO_INVALID_INDEX, &gbvalue);
746 gb_pm_runtime_put_autosuspend(bundle);
749 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
750 __func__, kcontrol->id.name);
754 ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
755 if (e->shift_l != e->shift_r)
756 ucontrol->value.enumerated.item[1] =
757 gbvalue.value.enumerated_item[1];
762 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
766 unsigned int val, mux, change;
768 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
769 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
770 struct gb_audio_ctl_elem_value gbvalue;
771 struct gbaudio_module_info *module;
772 struct snd_soc_codec *codec = widget->codec;
773 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
774 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
775 struct gb_bundle *bundle;
777 if (ucontrol->value.enumerated.item[0] > e->max - 1)
780 module = find_gb_module(gb, kcontrol->id.name);
784 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
789 bundle = to_gb_bundle(module->dev);
791 ret = gb_pm_runtime_get_sync(bundle);
795 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
796 GB_AUDIO_INVALID_INDEX, &gbvalue);
798 gb_pm_runtime_put_autosuspend(bundle);
801 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
802 __func__, kcontrol->id.name);
806 mux = ucontrol->value.enumerated.item[0];
807 val = mux << e->shift_l;
808 mask = e->mask << e->shift_l;
810 if (gbvalue.value.enumerated_item[0] !=
811 ucontrol->value.enumerated.item[0]) {
813 gbvalue.value.enumerated_item[0] =
814 ucontrol->value.enumerated.item[0];
817 if (e->shift_l != e->shift_r) {
818 if (ucontrol->value.enumerated.item[1] > e->max - 1)
820 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
821 mask |= e->mask << e->shift_r;
822 if (gbvalue.value.enumerated_item[1] !=
823 ucontrol->value.enumerated.item[1]) {
825 gbvalue.value.enumerated_item[1] =
826 ucontrol->value.enumerated.item[1];
831 ret = gb_pm_runtime_get_sync(bundle);
835 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
836 GB_AUDIO_INVALID_INDEX, &gbvalue);
838 gb_pm_runtime_put_autosuspend(bundle);
841 dev_err_ratelimited(codec->dev,
842 "%d:Error in %s for %s\n", ret,
843 __func__, kcontrol->id.name);
845 for (wi = 0; wi < wlist->num_widgets; wi++) {
846 widget = wlist->widgets[wi];
849 widget->dapm->update = NULL;
850 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
857 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
858 struct snd_kcontrol_new *kctl,
859 struct gb_audio_control *ctl)
861 struct soc_enum *gbe;
862 struct gb_audio_enumerated *gb_enum;
865 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
869 gb_enum = &ctl->info.value.enumerated;
871 /* since count=1, and reg is dummy */
872 gbe->max = gb_enum->items;
873 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
875 /* debug enum info */
876 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gb_enum->items,
877 gb_enum->names_length);
878 for (i = 0; i < gb_enum->items; i++)
879 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
881 *kctl = (struct snd_kcontrol_new)
882 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
883 gbcodec_enum_dapm_ctl_put);
887 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
888 struct snd_kcontrol_new *kctl,
889 struct gb_audio_control *ctl)
891 struct gbaudio_ctl_pvt *ctldata;
893 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
897 ctldata->ctl_id = ctl->id;
898 ctldata->data_cport = ctl->data_cport;
899 ctldata->access = ctl->access;
900 ctldata->vcount = ctl->count_values;
901 ctldata->info = &ctl->info;
902 *kctl = (struct snd_kcontrol_new)
903 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
908 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
909 struct snd_kcontrol_new *kctl,
910 struct gb_audio_control *ctl)
914 switch (ctl->iface) {
915 case SNDRV_CTL_ELEM_IFACE_MIXER:
916 switch (ctl->info.type) {
917 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
918 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
921 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
930 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
935 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
936 struct snd_kcontrol *kcontrol, int event)
940 struct snd_soc_codec *codec = w->codec;
941 struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
942 struct gbaudio_module_info *module;
943 struct gb_bundle *bundle;
945 dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
947 /* Find relevant module */
948 module = find_gb_module(gbcodec, w->name);
952 /* map name to widget id */
953 wid = gbaudio_map_widgetname(module, w->name);
955 dev_err(codec->dev, "Invalid widget name:%s\n", w->name);
959 bundle = to_gb_bundle(module->dev);
961 ret = gb_pm_runtime_get_sync(bundle);
966 case SND_SOC_DAPM_PRE_PMU:
967 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
969 ret = gbaudio_module_update(gbcodec, w, module, 1);
971 case SND_SOC_DAPM_POST_PMD:
972 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
974 ret = gbaudio_module_update(gbcodec, w, module, 0);
978 dev_err_ratelimited(codec->dev,
979 "%d: widget, event:%d failed:%d\n", wid,
982 gb_pm_runtime_put_autosuspend(bundle);
987 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
988 struct snd_soc_dapm_widget *dw,
989 struct gb_audio_widget *w, int *w_size)
992 struct snd_kcontrol_new *widget_kctls;
993 struct gb_audio_control *curr;
994 struct gbaudio_control *control, *_control;
996 char temp_name[NAME_SIZE];
998 ret = gbaudio_validate_kcontrol_count(w);
1000 dev_err(module->dev, "Inavlid kcontrol count=%d for %s\n",
1001 w->ncontrols, w->name);
1005 /* allocate memory for kcontrol */
1007 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1008 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1013 *w_size = sizeof(struct gb_audio_widget);
1015 /* create relevant kcontrols */
1017 for (i = 0; i < w->ncontrols; i++) {
1018 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1021 dev_err(module->dev,
1022 "%s:%d type widget_ctl not supported\n",
1023 curr->name, curr->iface);
1026 control = devm_kzalloc(module->dev,
1027 sizeof(struct gbaudio_control),
1033 control->id = curr->id;
1034 control->name = curr->name;
1035 control->wname = w->name;
1037 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1038 struct gb_audio_enumerated *gbenum =
1039 &curr->info.value.enumerated;
1041 csize = offsetof(struct gb_audio_control, info);
1042 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1043 csize += offsetof(struct gb_audio_enumerated, names);
1044 csize += gbenum->names_length;
1045 control->texts = (const char * const *)
1046 gb_generate_enum_strings(module, gbenum);
1047 control->items = gbenum->items;
1049 csize = sizeof(struct gb_audio_control);
1051 curr = (void *)curr + csize;
1052 list_add(&control->list, &module->widget_ctl_list);
1053 dev_dbg(module->dev, "%s: control of type %d created\n",
1054 widget_kctls[i].name, widget_kctls[i].iface);
1057 /* Prefix dev_id to widget control_name */
1058 strlcpy(temp_name, w->name, NAME_SIZE);
1059 snprintf(w->name, NAME_SIZE, "GB %d %s", module->dev_id, temp_name);
1062 case snd_soc_dapm_spk:
1063 *dw = (struct snd_soc_dapm_widget)
1064 SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1065 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1067 case snd_soc_dapm_hp:
1068 *dw = (struct snd_soc_dapm_widget)
1069 SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1070 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1071 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1072 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1074 case snd_soc_dapm_mic:
1075 *dw = (struct snd_soc_dapm_widget)
1076 SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1077 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1079 case snd_soc_dapm_output:
1080 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1082 case snd_soc_dapm_input:
1083 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1085 case snd_soc_dapm_switch:
1086 *dw = (struct snd_soc_dapm_widget)
1087 SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1088 widget_kctls, gbaudio_widget_event,
1089 SND_SOC_DAPM_PRE_PMU |
1090 SND_SOC_DAPM_POST_PMD);
1092 case snd_soc_dapm_pga:
1093 *dw = (struct snd_soc_dapm_widget)
1094 SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1095 gbaudio_widget_event,
1096 SND_SOC_DAPM_PRE_PMU |
1097 SND_SOC_DAPM_POST_PMD);
1099 case snd_soc_dapm_mixer:
1100 *dw = (struct snd_soc_dapm_widget)
1101 SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1102 0, gbaudio_widget_event,
1103 SND_SOC_DAPM_PRE_PMU |
1104 SND_SOC_DAPM_POST_PMD);
1106 case snd_soc_dapm_mux:
1107 *dw = (struct snd_soc_dapm_widget)
1108 SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1109 widget_kctls, gbaudio_widget_event,
1110 SND_SOC_DAPM_PRE_PMU |
1111 SND_SOC_DAPM_POST_PMD);
1113 case snd_soc_dapm_aif_in:
1114 *dw = (struct snd_soc_dapm_widget)
1115 SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1117 0, 0, gbaudio_widget_event,
1118 SND_SOC_DAPM_PRE_PMU |
1119 SND_SOC_DAPM_POST_PMD);
1121 case snd_soc_dapm_aif_out:
1122 *dw = (struct snd_soc_dapm_widget)
1123 SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1125 0, 0, gbaudio_widget_event,
1126 SND_SOC_DAPM_PRE_PMU |
1127 SND_SOC_DAPM_POST_PMD);
1134 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1138 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1140 list_del(&control->list);
1141 devm_kfree(module->dev, control);
1146 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1147 struct gb_audio_control *controls)
1150 struct snd_kcontrol_new *dapm_kctls;
1151 struct gb_audio_control *curr;
1152 struct gbaudio_control *control, *_control;
1154 char temp_name[NAME_SIZE];
1156 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1157 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1162 for (i = 0; i < module->num_controls; i++) {
1163 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1166 dev_err(module->dev, "%s:%d type not supported\n",
1167 curr->name, curr->iface);
1170 control = devm_kzalloc(module->dev, sizeof(struct
1177 control->id = curr->id;
1178 /* Prefix dev_id to widget_name */
1179 strlcpy(temp_name, curr->name, NAME_SIZE);
1180 snprintf(curr->name, NAME_SIZE, "GB %d %s", module->dev_id,
1182 control->name = curr->name;
1183 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1184 struct gb_audio_enumerated *gbenum =
1185 &curr->info.value.enumerated;
1187 csize = offsetof(struct gb_audio_control, info);
1188 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1189 csize += offsetof(struct gb_audio_enumerated, names);
1190 csize += gbenum->names_length;
1191 control->texts = (const char * const *)
1192 gb_generate_enum_strings(module, gbenum);
1193 control->items = gbenum->items;
1195 csize = sizeof(struct gb_audio_control);
1197 list_add(&control->list, &module->ctl_list);
1198 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1199 curr->name, curr->info.type);
1200 curr = (void *)curr + csize;
1202 module->controls = dapm_kctls;
1206 list_for_each_entry_safe(control, _control, &module->ctl_list,
1208 list_del(&control->list);
1209 devm_kfree(module->dev, control);
1211 devm_kfree(module->dev, dapm_kctls);
1215 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1216 struct gb_audio_widget *widgets)
1219 struct snd_soc_dapm_widget *dapm_widgets;
1220 struct gb_audio_widget *curr;
1221 struct gbaudio_widget *widget, *_widget;
1224 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1225 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1230 for (i = 0; i < module->num_dapm_widgets; i++) {
1231 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1234 dev_err(module->dev, "%s:%d type not supported\n",
1235 curr->name, curr->type);
1238 widget = devm_kzalloc(module->dev, sizeof(struct
1245 widget->id = curr->id;
1246 widget->name = curr->name;
1247 list_add(&widget->list, &module->widget_list);
1248 curr = (void *)curr + w_size;
1250 module->dapm_widgets = dapm_widgets;
1255 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1257 list_del(&widget->list);
1258 devm_kfree(module->dev, widget);
1260 devm_kfree(module->dev, dapm_widgets);
1264 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1265 struct gb_audio_route *routes)
1268 struct snd_soc_dapm_route *dapm_routes;
1269 struct gb_audio_route *curr;
1272 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1273 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1277 module->dapm_routes = dapm_routes;
1280 for (i = 0; i < module->num_dapm_routes; i++) {
1282 gbaudio_map_widgetid(module, curr->destination_id);
1283 if (!dapm_routes->sink) {
1284 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1285 curr->source_id, curr->destination_id,
1286 curr->control_id, curr->index);
1290 dapm_routes->source =
1291 gbaudio_map_widgetid(module, curr->source_id);
1292 if (!dapm_routes->source) {
1293 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1294 curr->source_id, curr->destination_id,
1295 curr->control_id, curr->index);
1299 dapm_routes->control =
1300 gbaudio_map_controlid(module,
1303 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1304 !dapm_routes->control) {
1305 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1306 curr->source_id, curr->destination_id,
1307 curr->control_id, curr->index);
1311 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1312 (dapm_routes->control) ? dapm_routes->control:"NULL",
1313 dapm_routes->source);
1321 devm_kfree(module->dev, module->dapm_routes);
1325 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1326 struct gb_audio_topology *tplg_data)
1328 /* fetch no. of kcontrols, widgets & routes */
1329 module->num_controls = tplg_data->num_controls;
1330 module->num_dapm_widgets = tplg_data->num_widgets;
1331 module->num_dapm_routes = tplg_data->num_routes;
1333 /* update block offset */
1334 module->dai_offset = (unsigned long)&tplg_data->data;
1335 module->control_offset = module->dai_offset + tplg_data->size_dais;
1336 module->widget_offset = module->control_offset +
1337 tplg_data->size_controls;
1338 module->route_offset = module->widget_offset +
1339 tplg_data->size_widgets;
1341 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1342 dev_dbg(module->dev, "control offset is %lx\n",
1343 module->control_offset);
1344 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1345 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1350 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1351 struct gb_audio_topology *tplg_data)
1354 struct gb_audio_control *controls;
1355 struct gb_audio_widget *widgets;
1356 struct gb_audio_route *routes;
1361 ret = gbaudio_tplg_process_header(module, tplg_data);
1363 dev_err(module->dev, "%d: Error in parsing topology header\n",
1368 /* process control */
1369 controls = (struct gb_audio_control *)module->control_offset;
1370 ret = gbaudio_tplg_process_kcontrols(module, controls);
1372 dev_err(module->dev,
1373 "%d: Error in parsing controls data\n", ret);
1376 dev_dbg(module->dev, "Control parsing finished\n");
1378 /* process widgets */
1379 widgets = (struct gb_audio_widget *)module->widget_offset;
1380 ret = gbaudio_tplg_process_widgets(module, widgets);
1382 dev_err(module->dev,
1383 "%d: Error in parsing widgets data\n", ret);
1386 dev_dbg(module->dev, "Widget parsing finished\n");
1389 routes = (struct gb_audio_route *)module->route_offset;
1390 ret = gbaudio_tplg_process_routes(module, routes);
1392 dev_err(module->dev,
1393 "%d: Error in parsing routes data\n", ret);
1396 dev_dbg(module->dev, "Route parsing finished\n");
1398 /* parse jack capabilities */
1399 if (tplg_data->jack_type) {
1400 module->jack_mask = tplg_data->jack_type & GBCODEC_JACK_MASK;
1401 module->button_mask = tplg_data->jack_type &
1402 GBCODEC_JACK_BUTTON_MASK;
1408 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1410 struct gbaudio_control *control, *_control;
1411 struct gbaudio_widget *widget, *_widget;
1413 if (!module->topology)
1416 /* release kcontrols */
1417 list_for_each_entry_safe(control, _control, &module->ctl_list,
1419 list_del(&control->list);
1420 devm_kfree(module->dev, control);
1422 if (module->controls)
1423 devm_kfree(module->dev, module->controls);
1425 /* release widget controls */
1426 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1428 list_del(&control->list);
1429 devm_kfree(module->dev, control);
1432 /* release widgets */
1433 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1435 list_del(&widget->list);
1436 devm_kfree(module->dev, widget);
1438 if (module->dapm_widgets)
1439 devm_kfree(module->dev, module->dapm_widgets);
1441 /* release routes */
1442 if (module->dapm_routes)
1443 devm_kfree(module->dev, module->dapm_routes);