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;
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_kzalloc(gb->dev, sizeof(char *) * items, GFP_KERNEL);
149 data = gbenum->names;
151 for (i = 0; i < items; i++) {
152 strings[i] = (const char *)data;
153 while (*data != '\0')
161 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
162 struct snd_ctl_elem_info *uinfo)
166 struct gbaudio_ctl_pvt *data;
167 struct gb_audio_ctl_elem_info *info;
168 struct gbaudio_module_info *module;
169 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
170 struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
172 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
173 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
174 info = (struct gb_audio_ctl_elem_info *)data->info;
177 dev_err(codec->dev, "NULL info for %s\n", uinfo->id.name);
182 uinfo->access = data->access;
183 uinfo->count = data->vcount;
184 uinfo->type = (snd_ctl_elem_type_t)info->type;
186 switch (info->type) {
187 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
188 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
189 uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
190 uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
192 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
193 max = le32_to_cpu(info->value.enumerated.items);
194 uinfo->value.enumerated.items = max;
195 if (uinfo->value.enumerated.item > max - 1)
196 uinfo->value.enumerated.item = max - 1;
197 module = find_gb_module(gbcodec, kcontrol->id.name);
200 name = gbaudio_map_controlid(module, data->ctl_id,
201 uinfo->value.enumerated.item);
202 strlcpy(uinfo->value.enumerated.name, name, NAME_SIZE);
205 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
206 info->type, kcontrol->id.name);
212 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
216 struct gb_audio_ctl_elem_info *info;
217 struct gbaudio_ctl_pvt *data;
218 struct gb_audio_ctl_elem_value gbvalue;
219 struct gbaudio_module_info *module;
220 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
221 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
222 struct gb_bundle *bundle;
224 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
225 module = find_gb_module(gb, kcontrol->id.name);
229 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
230 info = (struct gb_audio_ctl_elem_info *)data->info;
231 bundle = to_gb_bundle(module->dev);
233 ret = gb_pm_runtime_get_sync(bundle);
237 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
238 GB_AUDIO_INVALID_INDEX, &gbvalue);
240 gb_pm_runtime_put_autosuspend(bundle);
243 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
244 __func__, kcontrol->id.name);
248 /* update ucontrol */
249 switch (info->type) {
250 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
251 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
252 ucontrol->value.integer.value[0] =
253 le32_to_cpu(gbvalue.value.integer_value[0]);
254 if (data->vcount == 2)
255 ucontrol->value.integer.value[1] =
256 le32_to_cpu(gbvalue.value.integer_value[1]);
258 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
259 ucontrol->value.enumerated.item[0] =
260 le32_to_cpu(gbvalue.value.enumerated_item[0]);
261 if (data->vcount == 2)
262 ucontrol->value.enumerated.item[1] =
263 le32_to_cpu(gbvalue.value.enumerated_item[1]);
266 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
267 info->type, kcontrol->id.name);
274 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
275 struct snd_ctl_elem_value *ucontrol)
278 struct gb_audio_ctl_elem_info *info;
279 struct gbaudio_ctl_pvt *data;
280 struct gb_audio_ctl_elem_value gbvalue;
281 struct gbaudio_module_info *module;
282 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
283 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
284 struct gb_bundle *bundle;
286 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
287 module = find_gb_module(gb, kcontrol->id.name);
291 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
292 info = (struct gb_audio_ctl_elem_info *)data->info;
293 bundle = to_gb_bundle(module->dev);
295 /* update ucontrol */
296 switch (info->type) {
297 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
298 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
299 gbvalue.value.integer_value[0] =
300 cpu_to_le32(ucontrol->value.integer.value[0]);
301 if (data->vcount == 2)
302 gbvalue.value.integer_value[1] =
303 cpu_to_le32(ucontrol->value.integer.value[1]);
305 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
306 gbvalue.value.enumerated_item[0] =
307 cpu_to_le32(ucontrol->value.enumerated.item[0]);
308 if (data->vcount == 2)
309 gbvalue.value.enumerated_item[1] =
310 cpu_to_le32(ucontrol->value.enumerated.item[1]);
313 dev_err(codec->dev, "Invalid type: %d for %s:kcontrol\n",
314 info->type, kcontrol->id.name);
322 ret = gb_pm_runtime_get_sync(bundle);
326 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
327 GB_AUDIO_INVALID_INDEX, &gbvalue);
329 gb_pm_runtime_put_autosuspend(bundle);
332 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
333 __func__, kcontrol->id.name);
339 #define SOC_MIXER_GB(xname, kcount, data) \
340 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
341 .count = kcount, .info = gbcodec_mixer_ctl_info, \
342 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
343 .private_value = (unsigned long)data }
346 * although below callback functions seems redundant to above functions.
347 * same are kept to allow provision for different handling in case
348 * of DAPM related sequencing, etc.
350 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_info *uinfo)
353 int platform_max, platform_min;
354 struct gbaudio_ctl_pvt *data;
355 struct gb_audio_ctl_elem_info *info;
356 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
357 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
358 struct snd_soc_codec *codec = widget->codec;
360 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
361 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
362 info = (struct gb_audio_ctl_elem_info *)data->info;
365 platform_max = le32_to_cpu(info->value.integer.max);
366 platform_min = le32_to_cpu(info->value.integer.min);
368 if (platform_max == 1 &&
369 !strnstr(kcontrol->id.name, " Volume", NAME_SIZE))
370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
372 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
374 uinfo->count = data->vcount;
375 uinfo->value.integer.min = platform_min;
376 uinfo->value.integer.max = platform_max;
381 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
382 struct snd_ctl_elem_value *ucontrol)
385 struct gb_audio_ctl_elem_info *info;
386 struct gbaudio_ctl_pvt *data;
387 struct gb_audio_ctl_elem_value gbvalue;
388 struct gbaudio_module_info *module;
389 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
390 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
391 struct snd_soc_codec *codec = widget->codec;
392 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
393 struct gb_bundle *bundle;
395 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
396 module = find_gb_module(gb, kcontrol->id.name);
400 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
401 info = (struct gb_audio_ctl_elem_info *)data->info;
402 bundle = to_gb_bundle(module->dev);
404 if (data->vcount == 2)
405 dev_warn(widget->dapm->dev,
406 "GB: Control '%s' is stereo, which is not supported\n",
409 ret = gb_pm_runtime_get_sync(bundle);
413 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
414 GB_AUDIO_INVALID_INDEX, &gbvalue);
416 gb_pm_runtime_put_autosuspend(bundle);
419 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
420 __func__, kcontrol->id.name);
423 /* update ucontrol */
424 ucontrol->value.integer.value[0] =
425 le32_to_cpu(gbvalue.value.integer_value[0]);
430 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
431 struct snd_ctl_elem_value *ucontrol)
433 int ret, wi, max, connect;
434 unsigned int mask, val;
435 struct gb_audio_ctl_elem_info *info;
436 struct gbaudio_ctl_pvt *data;
437 struct gb_audio_ctl_elem_value gbvalue;
438 struct gbaudio_module_info *module;
439 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
440 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
441 struct snd_soc_codec *codec = widget->codec;
442 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
443 struct gb_bundle *bundle;
445 dev_dbg(codec->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
446 module = find_gb_module(gb, kcontrol->id.name);
450 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
451 info = (struct gb_audio_ctl_elem_info *)data->info;
452 bundle = to_gb_bundle(module->dev);
454 if (data->vcount == 2)
455 dev_warn(widget->dapm->dev,
456 "GB: Control '%s' is stereo, which is not supported\n",
459 max = le32_to_cpu(info->value.integer.max);
460 mask = (1 << fls(max)) - 1;
461 val = ucontrol->value.integer.value[0] & mask;
464 ret = gb_pm_runtime_get_sync(bundle);
468 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
469 GB_AUDIO_INVALID_INDEX, &gbvalue);
473 /* update ucontrol */
474 if (gbvalue.value.integer_value[0] != val) {
475 for (wi = 0; wi < wlist->num_widgets; wi++) {
476 widget = wlist->widgets[wi];
479 widget->dapm->update = NULL;
480 snd_soc_dapm_mixer_update_power(widget, kcontrol,
483 gbvalue.value.integer_value[0] =
484 cpu_to_le32(ucontrol->value.integer.value[0]);
486 ret = gb_audio_gb_set_control(module->mgmt_connection,
488 GB_AUDIO_INVALID_INDEX, &gbvalue);
492 gb_pm_runtime_put_autosuspend(bundle);
494 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
495 __func__, kcontrol->id.name);
499 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
500 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
501 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
502 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
503 .private_value = (unsigned long)data}
505 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
506 struct snd_kcontrol *k, int event)
508 /* Ensure GB speaker is connected */
513 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
514 struct snd_kcontrol *k, int event)
516 /* Ensure GB module supports jack slot */
521 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
522 struct snd_kcontrol *k, int event)
524 /* Ensure GB module supports jack slot */
529 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
534 case snd_soc_dapm_spk:
535 case snd_soc_dapm_hp:
536 case snd_soc_dapm_mic:
537 case snd_soc_dapm_output:
538 case snd_soc_dapm_input:
542 case snd_soc_dapm_switch:
543 case snd_soc_dapm_mux:
544 if (w->ncontrols != 1)
554 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
555 struct snd_ctl_elem_value *ucontrol)
558 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
559 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
560 struct gb_audio_ctl_elem_value gbvalue;
561 struct gbaudio_module_info *module;
562 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
563 struct gb_bundle *bundle;
565 module = find_gb_module(gb, kcontrol->id.name);
569 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
573 bundle = to_gb_bundle(module->dev);
575 ret = gb_pm_runtime_get_sync(bundle);
579 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
580 GB_AUDIO_INVALID_INDEX, &gbvalue);
582 gb_pm_runtime_put_autosuspend(bundle);
585 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
586 __func__, kcontrol->id.name);
590 ucontrol->value.enumerated.item[0] =
591 le32_to_cpu(gbvalue.value.enumerated_item[0]);
592 if (e->shift_l != e->shift_r)
593 ucontrol->value.enumerated.item[1] =
594 le32_to_cpu(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] =
621 cpu_to_le32(ucontrol->value.enumerated.item[0]);
623 if (e->shift_l != e->shift_r) {
624 if (ucontrol->value.enumerated.item[1] > e->max - 1)
626 gbvalue.value.enumerated_item[1] =
627 cpu_to_le32(ucontrol->value.enumerated.item[1]);
630 bundle = to_gb_bundle(module->dev);
632 ret = gb_pm_runtime_get_sync(bundle);
636 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
637 GB_AUDIO_INVALID_INDEX, &gbvalue);
639 gb_pm_runtime_put_autosuspend(bundle);
642 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
643 __func__, kcontrol->id.name);
649 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
650 struct snd_kcontrol_new *kctl,
651 struct gb_audio_control *ctl)
653 struct soc_enum *gbe;
654 struct gb_audio_enumerated *gb_enum;
657 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
661 gb_enum = &ctl->info.value.enumerated;
663 /* since count=1, and reg is dummy */
664 gbe->max = le32_to_cpu(gb_enum->items);
665 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
667 /* debug enum info */
668 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->max,
669 le16_to_cpu(gb_enum->names_length));
670 for (i = 0; i < gbe->max; i++)
671 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
673 *kctl = (struct snd_kcontrol_new)
674 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
675 gbcodec_enum_ctl_put);
679 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
680 struct snd_kcontrol_new *kctl,
681 struct gb_audio_control *ctl)
684 struct gbaudio_ctl_pvt *ctldata;
686 switch (ctl->iface) {
687 case SNDRV_CTL_ELEM_IFACE_MIXER:
688 switch (ctl->info.type) {
689 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
690 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
693 ctldata = devm_kzalloc(gb->dev,
694 sizeof(struct gbaudio_ctl_pvt),
698 ctldata->ctl_id = ctl->id;
699 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
700 ctldata->access = ctl->access;
701 ctldata->vcount = ctl->count_values;
702 ctldata->info = &ctl->info;
703 *kctl = (struct snd_kcontrol_new)
704 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
713 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
717 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
721 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
722 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
723 struct gbaudio_module_info *module;
724 struct gb_audio_ctl_elem_value gbvalue;
725 struct snd_soc_codec *codec = widget->codec;
726 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
727 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
728 struct gb_bundle *bundle;
730 module = find_gb_module(gb, kcontrol->id.name);
734 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
738 bundle = to_gb_bundle(module->dev);
740 ret = gb_pm_runtime_get_sync(bundle);
744 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
745 GB_AUDIO_INVALID_INDEX, &gbvalue);
747 gb_pm_runtime_put_autosuspend(bundle);
750 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
751 __func__, kcontrol->id.name);
755 ucontrol->value.enumerated.item[0] = gbvalue.value.enumerated_item[0];
756 if (e->shift_l != e->shift_r)
757 ucontrol->value.enumerated.item[1] =
758 gbvalue.value.enumerated_item[1];
763 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
764 struct snd_ctl_elem_value *ucontrol)
767 unsigned int val, mux, change;
769 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
770 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
771 struct gb_audio_ctl_elem_value gbvalue;
772 struct gbaudio_module_info *module;
773 struct snd_soc_codec *codec = widget->codec;
774 struct gbaudio_codec_info *gb = snd_soc_codec_get_drvdata(codec);
775 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
776 struct gb_bundle *bundle;
778 if (ucontrol->value.enumerated.item[0] > e->max - 1)
781 module = find_gb_module(gb, kcontrol->id.name);
785 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
790 bundle = to_gb_bundle(module->dev);
792 ret = gb_pm_runtime_get_sync(bundle);
796 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
797 GB_AUDIO_INVALID_INDEX, &gbvalue);
799 gb_pm_runtime_put_autosuspend(bundle);
802 dev_err_ratelimited(codec->dev, "%d:Error in %s for %s\n", ret,
803 __func__, kcontrol->id.name);
807 mux = ucontrol->value.enumerated.item[0];
808 val = mux << e->shift_l;
809 mask = e->mask << e->shift_l;
811 if (gbvalue.value.enumerated_item[0] !=
812 ucontrol->value.enumerated.item[0]) {
814 gbvalue.value.enumerated_item[0] =
815 ucontrol->value.enumerated.item[0];
818 if (e->shift_l != e->shift_r) {
819 if (ucontrol->value.enumerated.item[1] > e->max - 1)
821 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
822 mask |= e->mask << e->shift_r;
823 if (gbvalue.value.enumerated_item[1] !=
824 ucontrol->value.enumerated.item[1]) {
826 gbvalue.value.enumerated_item[1] =
827 ucontrol->value.enumerated.item[1];
832 ret = gb_pm_runtime_get_sync(bundle);
836 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
837 GB_AUDIO_INVALID_INDEX, &gbvalue);
839 gb_pm_runtime_put_autosuspend(bundle);
842 dev_err_ratelimited(codec->dev,
843 "%d:Error in %s for %s\n", ret,
844 __func__, kcontrol->id.name);
846 for (wi = 0; wi < wlist->num_widgets; wi++) {
847 widget = wlist->widgets[wi];
850 widget->dapm->update = NULL;
851 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
858 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
859 struct snd_kcontrol_new *kctl,
860 struct gb_audio_control *ctl)
862 struct soc_enum *gbe;
863 struct gb_audio_enumerated *gb_enum;
866 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
870 gb_enum = &ctl->info.value.enumerated;
872 /* since count=1, and reg is dummy */
873 gbe->max = le32_to_cpu(gb_enum->items);
874 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
876 /* debug enum info */
877 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->max,
878 le16_to_cpu(gb_enum->names_length));
879 for (i = 0; i < gbe->max; i++)
880 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
882 *kctl = (struct snd_kcontrol_new)
883 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
884 gbcodec_enum_dapm_ctl_put);
888 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
889 struct snd_kcontrol_new *kctl,
890 struct gb_audio_control *ctl)
892 struct gbaudio_ctl_pvt *ctldata;
894 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
898 ctldata->ctl_id = ctl->id;
899 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
900 ctldata->access = ctl->access;
901 ctldata->vcount = ctl->count_values;
902 ctldata->info = &ctl->info;
903 *kctl = (struct snd_kcontrol_new)
904 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
909 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
910 struct snd_kcontrol_new *kctl,
911 struct gb_audio_control *ctl)
915 switch (ctl->iface) {
916 case SNDRV_CTL_ELEM_IFACE_MIXER:
917 switch (ctl->info.type) {
918 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
919 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
922 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
931 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
936 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
937 struct snd_kcontrol *kcontrol, int event)
941 struct snd_soc_codec *codec = w->codec;
942 struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
943 struct gbaudio_module_info *module;
944 struct gb_bundle *bundle;
946 dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
948 /* Find relevant module */
949 module = find_gb_module(gbcodec, w->name);
953 /* map name to widget id */
954 wid = gbaudio_map_widgetname(module, w->name);
956 dev_err(codec->dev, "Invalid widget name:%s\n", w->name);
960 bundle = to_gb_bundle(module->dev);
962 ret = gb_pm_runtime_get_sync(bundle);
967 case SND_SOC_DAPM_PRE_PMU:
968 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
970 ret = gbaudio_module_update(gbcodec, w, module, 1);
972 case SND_SOC_DAPM_POST_PMD:
973 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
975 ret = gbaudio_module_update(gbcodec, w, module, 0);
979 dev_err_ratelimited(codec->dev,
980 "%d: widget, event:%d failed:%d\n", wid,
983 gb_pm_runtime_put_autosuspend(bundle);
988 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
989 struct snd_soc_dapm_widget *dw,
990 struct gb_audio_widget *w, int *w_size)
993 struct snd_kcontrol_new *widget_kctls;
994 struct gb_audio_control *curr;
995 struct gbaudio_control *control, *_control;
997 char temp_name[NAME_SIZE];
999 ret = gbaudio_validate_kcontrol_count(w);
1001 dev_err(module->dev, "Inavlid kcontrol count=%d for %s\n",
1002 w->ncontrols, w->name);
1006 /* allocate memory for kcontrol */
1008 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
1009 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1014 *w_size = sizeof(struct gb_audio_widget);
1016 /* create relevant kcontrols */
1018 for (i = 0; i < w->ncontrols; i++) {
1019 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1022 dev_err(module->dev,
1023 "%s:%d type widget_ctl not supported\n",
1024 curr->name, curr->iface);
1027 control = devm_kzalloc(module->dev,
1028 sizeof(struct gbaudio_control),
1034 control->id = curr->id;
1035 control->name = curr->name;
1036 control->wname = w->name;
1038 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1039 struct gb_audio_enumerated *gbenum =
1040 &curr->info.value.enumerated;
1042 csize = offsetof(struct gb_audio_control, info);
1043 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1044 csize += offsetof(struct gb_audio_enumerated, names);
1045 csize += le16_to_cpu(gbenum->names_length);
1046 control->texts = (const char * const *)
1047 gb_generate_enum_strings(module, gbenum);
1048 control->items = le32_to_cpu(gbenum->items);
1050 csize = sizeof(struct gb_audio_control);
1054 curr = (void *)curr + csize;
1055 list_add(&control->list, &module->widget_ctl_list);
1056 dev_dbg(module->dev, "%s: control of type %d created\n",
1057 widget_kctls[i].name, widget_kctls[i].iface);
1060 /* Prefix dev_id to widget control_name */
1061 strlcpy(temp_name, w->name, NAME_SIZE);
1062 snprintf(w->name, NAME_SIZE, "GB %d %s", module->dev_id, temp_name);
1065 case snd_soc_dapm_spk:
1066 *dw = (struct snd_soc_dapm_widget)
1067 SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1068 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1070 case snd_soc_dapm_hp:
1071 *dw = (struct snd_soc_dapm_widget)
1072 SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1073 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1074 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1075 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1077 case snd_soc_dapm_mic:
1078 *dw = (struct snd_soc_dapm_widget)
1079 SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1080 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1082 case snd_soc_dapm_output:
1083 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1085 case snd_soc_dapm_input:
1086 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1088 case snd_soc_dapm_switch:
1089 *dw = (struct snd_soc_dapm_widget)
1090 SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1091 widget_kctls, gbaudio_widget_event,
1092 SND_SOC_DAPM_PRE_PMU |
1093 SND_SOC_DAPM_POST_PMD);
1095 case snd_soc_dapm_pga:
1096 *dw = (struct snd_soc_dapm_widget)
1097 SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1098 gbaudio_widget_event,
1099 SND_SOC_DAPM_PRE_PMU |
1100 SND_SOC_DAPM_POST_PMD);
1102 case snd_soc_dapm_mixer:
1103 *dw = (struct snd_soc_dapm_widget)
1104 SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1105 0, gbaudio_widget_event,
1106 SND_SOC_DAPM_PRE_PMU |
1107 SND_SOC_DAPM_POST_PMD);
1109 case snd_soc_dapm_mux:
1110 *dw = (struct snd_soc_dapm_widget)
1111 SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1112 widget_kctls, gbaudio_widget_event,
1113 SND_SOC_DAPM_PRE_PMU |
1114 SND_SOC_DAPM_POST_PMD);
1116 case snd_soc_dapm_aif_in:
1117 *dw = (struct snd_soc_dapm_widget)
1118 SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1120 0, 0, gbaudio_widget_event,
1121 SND_SOC_DAPM_PRE_PMU |
1122 SND_SOC_DAPM_POST_PMD);
1124 case snd_soc_dapm_aif_out:
1125 *dw = (struct snd_soc_dapm_widget)
1126 SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1128 0, 0, gbaudio_widget_event,
1129 SND_SOC_DAPM_PRE_PMU |
1130 SND_SOC_DAPM_POST_PMD);
1137 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1141 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1143 list_del(&control->list);
1144 devm_kfree(module->dev, control);
1149 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1150 struct gb_audio_control *controls)
1153 struct snd_kcontrol_new *dapm_kctls;
1154 struct gb_audio_control *curr;
1155 struct gbaudio_control *control, *_control;
1157 char temp_name[NAME_SIZE];
1159 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1160 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1165 for (i = 0; i < module->num_controls; i++) {
1166 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1169 dev_err(module->dev, "%s:%d type not supported\n",
1170 curr->name, curr->iface);
1173 control = devm_kzalloc(module->dev, sizeof(struct
1180 control->id = curr->id;
1181 /* Prefix dev_id to widget_name */
1182 strlcpy(temp_name, curr->name, NAME_SIZE);
1183 snprintf(curr->name, NAME_SIZE, "GB %d %s", module->dev_id,
1185 control->name = curr->name;
1186 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1187 struct gb_audio_enumerated *gbenum =
1188 &curr->info.value.enumerated;
1190 csize = offsetof(struct gb_audio_control, info);
1191 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1192 csize += offsetof(struct gb_audio_enumerated, names);
1193 csize += le16_to_cpu(gbenum->names_length);
1194 control->texts = (const char * const *)
1195 gb_generate_enum_strings(module, gbenum);
1196 control->items = le32_to_cpu(gbenum->items);
1198 csize = sizeof(struct gb_audio_control);
1201 list_add(&control->list, &module->ctl_list);
1202 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1203 curr->name, curr->info.type);
1204 curr = (void *)curr + csize;
1206 module->controls = dapm_kctls;
1210 list_for_each_entry_safe(control, _control, &module->ctl_list,
1212 list_del(&control->list);
1213 devm_kfree(module->dev, control);
1215 devm_kfree(module->dev, dapm_kctls);
1219 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1220 struct gb_audio_widget *widgets)
1223 struct snd_soc_dapm_widget *dapm_widgets;
1224 struct gb_audio_widget *curr;
1225 struct gbaudio_widget *widget, *_widget;
1228 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1229 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1234 for (i = 0; i < module->num_dapm_widgets; i++) {
1235 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1238 dev_err(module->dev, "%s:%d type not supported\n",
1239 curr->name, curr->type);
1242 widget = devm_kzalloc(module->dev, sizeof(struct
1249 widget->id = curr->id;
1250 widget->name = curr->name;
1251 list_add(&widget->list, &module->widget_list);
1252 curr = (void *)curr + w_size;
1254 module->dapm_widgets = dapm_widgets;
1259 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1261 list_del(&widget->list);
1262 devm_kfree(module->dev, widget);
1264 devm_kfree(module->dev, dapm_widgets);
1268 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1269 struct gb_audio_route *routes)
1272 struct snd_soc_dapm_route *dapm_routes;
1273 struct gb_audio_route *curr;
1276 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1277 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1281 module->dapm_routes = dapm_routes;
1284 for (i = 0; i < module->num_dapm_routes; i++) {
1286 gbaudio_map_widgetid(module, curr->destination_id);
1287 if (!dapm_routes->sink) {
1288 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1289 curr->source_id, curr->destination_id,
1290 curr->control_id, curr->index);
1294 dapm_routes->source =
1295 gbaudio_map_widgetid(module, curr->source_id);
1296 if (!dapm_routes->source) {
1297 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1298 curr->source_id, curr->destination_id,
1299 curr->control_id, curr->index);
1303 dapm_routes->control =
1304 gbaudio_map_controlid(module,
1307 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1308 !dapm_routes->control) {
1309 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1310 curr->source_id, curr->destination_id,
1311 curr->control_id, curr->index);
1315 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1316 (dapm_routes->control) ? dapm_routes->control : "NULL",
1317 dapm_routes->source);
1325 devm_kfree(module->dev, module->dapm_routes);
1329 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1330 struct gb_audio_topology *tplg_data)
1332 /* fetch no. of kcontrols, widgets & routes */
1333 module->num_controls = tplg_data->num_controls;
1334 module->num_dapm_widgets = tplg_data->num_widgets;
1335 module->num_dapm_routes = tplg_data->num_routes;
1337 /* update block offset */
1338 module->dai_offset = (unsigned long)&tplg_data->data;
1339 module->control_offset = module->dai_offset +
1340 le32_to_cpu(tplg_data->size_dais);
1341 module->widget_offset = module->control_offset +
1342 le32_to_cpu(tplg_data->size_controls);
1343 module->route_offset = module->widget_offset +
1344 le32_to_cpu(tplg_data->size_widgets);
1346 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1347 dev_dbg(module->dev, "control offset is %lx\n",
1348 module->control_offset);
1349 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1350 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1355 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1356 struct gb_audio_topology *tplg_data)
1359 struct gb_audio_control *controls;
1360 struct gb_audio_widget *widgets;
1361 struct gb_audio_route *routes;
1362 unsigned int jack_type;
1367 ret = gbaudio_tplg_process_header(module, tplg_data);
1369 dev_err(module->dev, "%d: Error in parsing topology header\n",
1374 /* process control */
1375 controls = (struct gb_audio_control *)module->control_offset;
1376 ret = gbaudio_tplg_process_kcontrols(module, controls);
1378 dev_err(module->dev,
1379 "%d: Error in parsing controls data\n", ret);
1382 dev_dbg(module->dev, "Control parsing finished\n");
1384 /* process widgets */
1385 widgets = (struct gb_audio_widget *)module->widget_offset;
1386 ret = gbaudio_tplg_process_widgets(module, widgets);
1388 dev_err(module->dev,
1389 "%d: Error in parsing widgets data\n", ret);
1392 dev_dbg(module->dev, "Widget parsing finished\n");
1395 routes = (struct gb_audio_route *)module->route_offset;
1396 ret = gbaudio_tplg_process_routes(module, routes);
1398 dev_err(module->dev,
1399 "%d: Error in parsing routes data\n", ret);
1402 dev_dbg(module->dev, "Route parsing finished\n");
1404 /* parse jack capabilities */
1405 jack_type = le32_to_cpu(tplg_data->jack_type);
1407 module->jack_mask = jack_type & GBCODEC_JACK_MASK;
1408 module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK;
1414 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1416 struct gbaudio_control *control, *_control;
1417 struct gbaudio_widget *widget, *_widget;
1419 if (!module->topology)
1422 /* release kcontrols */
1423 list_for_each_entry_safe(control, _control, &module->ctl_list,
1425 list_del(&control->list);
1426 devm_kfree(module->dev, control);
1428 if (module->controls)
1429 devm_kfree(module->dev, module->controls);
1431 /* release widget controls */
1432 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1434 list_del(&control->list);
1435 devm_kfree(module->dev, control);
1438 /* release widgets */
1439 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1441 list_del(&widget->list);
1442 devm_kfree(module->dev, widget);
1444 if (module->dapm_widgets)
1445 devm_kfree(module->dev, module->dapm_widgets);
1447 /* release routes */
1448 if (module->dapm_routes)
1449 devm_kfree(module->dev, module->dapm_routes);