1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus audio driver
4 * Copyright 2015-2016 Google Inc.
5 * Copyright 2015-2016 Linaro Ltd.
6 */
7
8 #include <linux/greybus.h>
9 #include "audio_codec.h"
10
11 #define GBAUDIO_INVALID_ID 0xFF
12
13 /* mixer control */
14 struct gb_mixer_control {
15 int min, max;
16 unsigned int reg, rreg, shift, rshift, invert;
17 };
18
19 struct gbaudio_ctl_pvt {
20 unsigned int ctl_id;
21 unsigned int data_cport;
22 unsigned int access;
23 unsigned int vcount;
24 struct gb_audio_ctl_elem_info *info;
25 };
26
find_gb_module(struct gbaudio_codec_info * codec,char const * name)27 static struct gbaudio_module_info *find_gb_module(
28 struct gbaudio_codec_info *codec,
29 char const *name)
30 {
31 int dev_id;
32 char begin[NAME_SIZE];
33 struct gbaudio_module_info *module;
34
35 if (!name)
36 return NULL;
37
38 if (sscanf(name, "%s %d", begin, &dev_id) != 2)
39 return NULL;
40
41 dev_dbg(codec->dev, "%s:Find module#%d\n", __func__, dev_id);
42
43 mutex_lock(&codec->lock);
44 list_for_each_entry(module, &codec->module_list, list) {
45 if (module->dev_id == dev_id) {
46 mutex_unlock(&codec->lock);
47 return module;
48 }
49 }
50 mutex_unlock(&codec->lock);
51 dev_warn(codec->dev, "%s: module#%d missing in codec list\n", name,
52 dev_id);
53 return NULL;
54 }
55
gbaudio_map_controlid(struct gbaudio_module_info * module,__u8 control_id,__u8 index)56 static const char *gbaudio_map_controlid(struct gbaudio_module_info *module,
57 __u8 control_id, __u8 index)
58 {
59 struct gbaudio_control *control;
60
61 if (control_id == GBAUDIO_INVALID_ID)
62 return NULL;
63
64 list_for_each_entry(control, &module->ctl_list, list) {
65 if (control->id == control_id) {
66 if (index == GBAUDIO_INVALID_ID)
67 return control->name;
68 if (index >= control->items)
69 return NULL;
70 return control->texts[index];
71 }
72 }
73 list_for_each_entry(control, &module->widget_ctl_list, list) {
74 if (control->id == control_id) {
75 if (index == GBAUDIO_INVALID_ID)
76 return control->name;
77 if (index >= control->items)
78 return NULL;
79 return control->texts[index];
80 }
81 }
82 return NULL;
83 }
84
gbaudio_map_controlname(struct gbaudio_module_info * module,const char * name)85 static int gbaudio_map_controlname(struct gbaudio_module_info *module,
86 const char *name)
87 {
88 struct gbaudio_control *control;
89
90 list_for_each_entry(control, &module->ctl_list, list) {
91 if (!strncmp(control->name, name, NAME_SIZE))
92 return control->id;
93 }
94
95 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
96
97 return -EINVAL;
98 }
99
gbaudio_map_wcontrolname(struct gbaudio_module_info * module,const char * name)100 static int gbaudio_map_wcontrolname(struct gbaudio_module_info *module,
101 const char *name)
102 {
103 struct gbaudio_control *control;
104
105 list_for_each_entry(control, &module->widget_ctl_list, list) {
106 if (!strncmp(control->wname, name, NAME_SIZE))
107 return control->id;
108 }
109 dev_warn(module->dev, "%s: missing in modules controls list\n", name);
110
111 return -EINVAL;
112 }
113
gbaudio_map_widgetname(struct gbaudio_module_info * module,const char * name)114 static int gbaudio_map_widgetname(struct gbaudio_module_info *module,
115 const char *name)
116 {
117 struct gbaudio_widget *widget;
118
119 list_for_each_entry(widget, &module->widget_list, list) {
120 if (!strncmp(widget->name, name, NAME_SIZE))
121 return widget->id;
122 }
123 dev_warn(module->dev, "%s: missing in modules widgets list\n", name);
124
125 return -EINVAL;
126 }
127
gbaudio_map_widgetid(struct gbaudio_module_info * module,__u8 widget_id)128 static const char *gbaudio_map_widgetid(struct gbaudio_module_info *module,
129 __u8 widget_id)
130 {
131 struct gbaudio_widget *widget;
132
133 list_for_each_entry(widget, &module->widget_list, list) {
134 if (widget->id == widget_id)
135 return widget->name;
136 }
137 return NULL;
138 }
139
gb_generate_enum_strings(struct gbaudio_module_info * gb,struct gb_audio_enumerated * gbenum)140 static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
141 struct gb_audio_enumerated *gbenum)
142 {
143 const char **strings;
144 int i;
145 unsigned int items;
146 __u8 *data;
147
148 items = le32_to_cpu(gbenum->items);
149 strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL);
150 data = gbenum->names;
151
152 for (i = 0; i < items; i++) {
153 strings[i] = (const char *)data;
154 while (*data != '\0')
155 data++;
156 data++;
157 }
158
159 return strings;
160 }
161
gbcodec_mixer_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)162 static int gbcodec_mixer_ctl_info(struct snd_kcontrol *kcontrol,
163 struct snd_ctl_elem_info *uinfo)
164 {
165 unsigned int max;
166 const char *name;
167 struct gbaudio_ctl_pvt *data;
168 struct gb_audio_ctl_elem_info *info;
169 struct gbaudio_module_info *module;
170 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
171 struct gbaudio_codec_info *gbcodec = snd_soc_component_get_drvdata(comp);
172
173 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
174 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
175 info = (struct gb_audio_ctl_elem_info *)data->info;
176
177 if (!info) {
178 dev_err(comp->dev, "NULL info for %s\n", uinfo->id.name);
179 return -EINVAL;
180 }
181
182 /* update uinfo */
183 uinfo->access = data->access;
184 uinfo->count = data->vcount;
185 uinfo->type = (__force snd_ctl_elem_type_t)info->type;
186
187 switch (info->type) {
188 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
189 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
190 uinfo->value.integer.min = le32_to_cpu(info->value.integer.min);
191 uinfo->value.integer.max = le32_to_cpu(info->value.integer.max);
192 break;
193 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
194 max = le32_to_cpu(info->value.enumerated.items);
195 uinfo->value.enumerated.items = max;
196 if (uinfo->value.enumerated.item > max - 1)
197 uinfo->value.enumerated.item = max - 1;
198 module = find_gb_module(gbcodec, kcontrol->id.name);
199 if (!module)
200 return -EINVAL;
201 name = gbaudio_map_controlid(module, data->ctl_id,
202 uinfo->value.enumerated.item);
203 strlcpy(uinfo->value.enumerated.name, name, NAME_SIZE);
204 break;
205 default:
206 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
207 info->type, kcontrol->id.name);
208 break;
209 }
210 return 0;
211 }
212
gbcodec_mixer_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)213 static int gbcodec_mixer_ctl_get(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
215 {
216 int ret;
217 struct gb_audio_ctl_elem_info *info;
218 struct gbaudio_ctl_pvt *data;
219 struct gb_audio_ctl_elem_value gbvalue;
220 struct gbaudio_module_info *module;
221 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
222 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
223 struct gb_bundle *bundle;
224
225 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
226 module = find_gb_module(gb, kcontrol->id.name);
227 if (!module)
228 return -EINVAL;
229
230 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
231 info = (struct gb_audio_ctl_elem_info *)data->info;
232 bundle = to_gb_bundle(module->dev);
233
234 ret = gb_pm_runtime_get_sync(bundle);
235 if (ret)
236 return ret;
237
238 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
239 GB_AUDIO_INVALID_INDEX, &gbvalue);
240
241 gb_pm_runtime_put_autosuspend(bundle);
242
243 if (ret) {
244 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
245 __func__, kcontrol->id.name);
246 return ret;
247 }
248
249 /* update ucontrol */
250 switch (info->type) {
251 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
252 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
253 ucontrol->value.integer.value[0] =
254 le32_to_cpu(gbvalue.value.integer_value[0]);
255 if (data->vcount == 2)
256 ucontrol->value.integer.value[1] =
257 le32_to_cpu(gbvalue.value.integer_value[1]);
258 break;
259 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
260 ucontrol->value.enumerated.item[0] =
261 le32_to_cpu(gbvalue.value.enumerated_item[0]);
262 if (data->vcount == 2)
263 ucontrol->value.enumerated.item[1] =
264 le32_to_cpu(gbvalue.value.enumerated_item[1]);
265 break;
266 default:
267 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
268 info->type, kcontrol->id.name);
269 ret = -EINVAL;
270 break;
271 }
272 return ret;
273 }
274
gbcodec_mixer_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)275 static int gbcodec_mixer_ctl_put(struct snd_kcontrol *kcontrol,
276 struct snd_ctl_elem_value *ucontrol)
277 {
278 int ret = 0;
279 struct gb_audio_ctl_elem_info *info;
280 struct gbaudio_ctl_pvt *data;
281 struct gb_audio_ctl_elem_value gbvalue;
282 struct gbaudio_module_info *module;
283 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
284 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
285 struct gb_bundle *bundle;
286
287 dev_dbg(comp->dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
288 module = find_gb_module(gb, kcontrol->id.name);
289 if (!module)
290 return -EINVAL;
291
292 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
293 info = (struct gb_audio_ctl_elem_info *)data->info;
294 bundle = to_gb_bundle(module->dev);
295
296 /* update ucontrol */
297 switch (info->type) {
298 case GB_AUDIO_CTL_ELEM_TYPE_BOOLEAN:
299 case GB_AUDIO_CTL_ELEM_TYPE_INTEGER:
300 gbvalue.value.integer_value[0] =
301 cpu_to_le32(ucontrol->value.integer.value[0]);
302 if (data->vcount == 2)
303 gbvalue.value.integer_value[1] =
304 cpu_to_le32(ucontrol->value.integer.value[1]);
305 break;
306 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
307 gbvalue.value.enumerated_item[0] =
308 cpu_to_le32(ucontrol->value.enumerated.item[0]);
309 if (data->vcount == 2)
310 gbvalue.value.enumerated_item[1] =
311 cpu_to_le32(ucontrol->value.enumerated.item[1]);
312 break;
313 default:
314 dev_err(comp->dev, "Invalid type: %d for %s:kcontrol\n",
315 info->type, kcontrol->id.name);
316 ret = -EINVAL;
317 break;
318 }
319
320 if (ret)
321 return ret;
322
323 ret = gb_pm_runtime_get_sync(bundle);
324 if (ret)
325 return ret;
326
327 ret = gb_audio_gb_set_control(module->mgmt_connection, data->ctl_id,
328 GB_AUDIO_INVALID_INDEX, &gbvalue);
329
330 gb_pm_runtime_put_autosuspend(bundle);
331
332 if (ret) {
333 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
334 __func__, kcontrol->id.name);
335 }
336
337 return ret;
338 }
339
340 #define SOC_MIXER_GB(xname, kcount, data) \
341 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
342 .count = kcount, .info = gbcodec_mixer_ctl_info, \
343 .get = gbcodec_mixer_ctl_get, .put = gbcodec_mixer_ctl_put, \
344 .private_value = (unsigned long)data }
345
346 /*
347 * although below callback functions seems redundant to above functions.
348 * same are kept to allow provision for different handling in case
349 * of DAPM related sequencing, etc.
350 */
gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)351 static int gbcodec_mixer_dapm_ctl_info(struct snd_kcontrol *kcontrol,
352 struct snd_ctl_elem_info *uinfo)
353 {
354 int platform_max, platform_min;
355 struct gbaudio_ctl_pvt *data;
356 struct gb_audio_ctl_elem_info *info;
357
358 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
359 info = (struct gb_audio_ctl_elem_info *)data->info;
360
361 /* update uinfo */
362 platform_max = le32_to_cpu(info->value.integer.max);
363 platform_min = le32_to_cpu(info->value.integer.min);
364
365 if (platform_max == 1 &&
366 !strnstr(kcontrol->id.name, " Volume", NAME_SIZE))
367 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
368 else
369 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
370
371 uinfo->count = data->vcount;
372 uinfo->value.integer.min = platform_min;
373 uinfo->value.integer.max = platform_max;
374
375 return 0;
376 }
377
gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)378 static int gbcodec_mixer_dapm_ctl_get(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
380 {
381 int ret;
382 struct gbaudio_ctl_pvt *data;
383 struct gb_audio_ctl_elem_value gbvalue;
384 struct gbaudio_module_info *module;
385 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
386 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
387 struct device *codec_dev = widget->dapm->dev;
388 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
389 struct gb_bundle *bundle;
390
391 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
392 module = find_gb_module(gb, kcontrol->id.name);
393 if (!module)
394 return -EINVAL;
395
396 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
397 bundle = to_gb_bundle(module->dev);
398
399 if (data->vcount == 2)
400 dev_warn(widget->dapm->dev,
401 "GB: Control '%s' is stereo, which is not supported\n",
402 kcontrol->id.name);
403
404 ret = gb_pm_runtime_get_sync(bundle);
405 if (ret)
406 return ret;
407
408 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
409 GB_AUDIO_INVALID_INDEX, &gbvalue);
410
411 gb_pm_runtime_put_autosuspend(bundle);
412
413 if (ret) {
414 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
415 __func__, kcontrol->id.name);
416 return ret;
417 }
418 /* update ucontrol */
419 ucontrol->value.integer.value[0] =
420 le32_to_cpu(gbvalue.value.integer_value[0]);
421
422 return ret;
423 }
424
gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)425 static int gbcodec_mixer_dapm_ctl_put(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
427 {
428 int ret, wi, max, connect;
429 unsigned int mask, val;
430 struct gb_audio_ctl_elem_info *info;
431 struct gbaudio_ctl_pvt *data;
432 struct gb_audio_ctl_elem_value gbvalue;
433 struct gbaudio_module_info *module;
434 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
435 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
436 struct device *codec_dev = widget->dapm->dev;
437 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
438 struct gb_bundle *bundle;
439
440 dev_dbg(codec_dev, "Entered %s:%s\n", __func__, kcontrol->id.name);
441 module = find_gb_module(gb, kcontrol->id.name);
442 if (!module)
443 return -EINVAL;
444
445 data = (struct gbaudio_ctl_pvt *)kcontrol->private_value;
446 info = (struct gb_audio_ctl_elem_info *)data->info;
447 bundle = to_gb_bundle(module->dev);
448
449 if (data->vcount == 2)
450 dev_warn(widget->dapm->dev,
451 "GB: Control '%s' is stereo, which is not supported\n",
452 kcontrol->id.name);
453
454 max = le32_to_cpu(info->value.integer.max);
455 mask = (1 << fls(max)) - 1;
456 val = ucontrol->value.integer.value[0] & mask;
457 connect = !!val;
458
459 ret = gb_pm_runtime_get_sync(bundle);
460 if (ret)
461 return ret;
462
463 ret = gb_audio_gb_get_control(module->mgmt_connection, data->ctl_id,
464 GB_AUDIO_INVALID_INDEX, &gbvalue);
465 if (ret)
466 goto exit;
467
468 /* update ucontrol */
469 if (le32_to_cpu(gbvalue.value.integer_value[0]) != val) {
470 for (wi = 0; wi < wlist->num_widgets; wi++) {
471 widget = wlist->widgets[wi];
472 snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol,
473 connect, NULL);
474 }
475 gbvalue.value.integer_value[0] =
476 cpu_to_le32(ucontrol->value.integer.value[0]);
477
478 ret = gb_audio_gb_set_control(module->mgmt_connection,
479 data->ctl_id,
480 GB_AUDIO_INVALID_INDEX, &gbvalue);
481 }
482
483 exit:
484 gb_pm_runtime_put_autosuspend(bundle);
485 if (ret)
486 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
487 __func__, kcontrol->id.name);
488 return ret;
489 }
490
491 #define SOC_DAPM_MIXER_GB(xname, kcount, data) \
492 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
493 .count = kcount, .info = gbcodec_mixer_dapm_ctl_info, \
494 .get = gbcodec_mixer_dapm_ctl_get, .put = gbcodec_mixer_dapm_ctl_put, \
495 .private_value = (unsigned long)data}
496
gbcodec_event_spk(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)497 static int gbcodec_event_spk(struct snd_soc_dapm_widget *w,
498 struct snd_kcontrol *k, int event)
499 {
500 /* Ensure GB speaker is connected */
501
502 return 0;
503 }
504
gbcodec_event_hp(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)505 static int gbcodec_event_hp(struct snd_soc_dapm_widget *w,
506 struct snd_kcontrol *k, int event)
507 {
508 /* Ensure GB module supports jack slot */
509
510 return 0;
511 }
512
gbcodec_event_int_mic(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)513 static int gbcodec_event_int_mic(struct snd_soc_dapm_widget *w,
514 struct snd_kcontrol *k, int event)
515 {
516 /* Ensure GB module supports jack slot */
517
518 return 0;
519 }
520
gbaudio_validate_kcontrol_count(struct gb_audio_widget * w)521 static int gbaudio_validate_kcontrol_count(struct gb_audio_widget *w)
522 {
523 int ret = 0;
524
525 switch (w->type) {
526 case snd_soc_dapm_spk:
527 case snd_soc_dapm_hp:
528 case snd_soc_dapm_mic:
529 case snd_soc_dapm_output:
530 case snd_soc_dapm_input:
531 if (w->ncontrols)
532 ret = -EINVAL;
533 break;
534 case snd_soc_dapm_switch:
535 case snd_soc_dapm_mux:
536 if (w->ncontrols != 1)
537 ret = -EINVAL;
538 break;
539 default:
540 break;
541 }
542
543 return ret;
544 }
545
gbcodec_enum_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)546 static int gbcodec_enum_ctl_get(struct snd_kcontrol *kcontrol,
547 struct snd_ctl_elem_value *ucontrol)
548 {
549 int ret, ctl_id;
550 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
551 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
552 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
553 struct gb_audio_ctl_elem_value gbvalue;
554 struct gbaudio_module_info *module;
555 struct gb_bundle *bundle;
556
557 module = find_gb_module(gb, kcontrol->id.name);
558 if (!module)
559 return -EINVAL;
560
561 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
562 if (ctl_id < 0)
563 return -EINVAL;
564
565 bundle = to_gb_bundle(module->dev);
566
567 ret = gb_pm_runtime_get_sync(bundle);
568 if (ret)
569 return ret;
570
571 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
572 GB_AUDIO_INVALID_INDEX, &gbvalue);
573
574 gb_pm_runtime_put_autosuspend(bundle);
575
576 if (ret) {
577 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n", ret,
578 __func__, kcontrol->id.name);
579 return ret;
580 }
581
582 ucontrol->value.enumerated.item[0] =
583 le32_to_cpu(gbvalue.value.enumerated_item[0]);
584 if (e->shift_l != e->shift_r)
585 ucontrol->value.enumerated.item[1] =
586 le32_to_cpu(gbvalue.value.enumerated_item[1]);
587
588 return 0;
589 }
590
gbcodec_enum_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)591 static int gbcodec_enum_ctl_put(struct snd_kcontrol *kcontrol,
592 struct snd_ctl_elem_value *ucontrol)
593 {
594 int ret, ctl_id;
595 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
596 struct gbaudio_codec_info *gb = snd_soc_component_get_drvdata(comp);
597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
598 struct gb_audio_ctl_elem_value gbvalue;
599 struct gbaudio_module_info *module;
600 struct gb_bundle *bundle;
601
602 module = find_gb_module(gb, kcontrol->id.name);
603 if (!module)
604 return -EINVAL;
605
606 ctl_id = gbaudio_map_controlname(module, kcontrol->id.name);
607 if (ctl_id < 0)
608 return -EINVAL;
609
610 if (ucontrol->value.enumerated.item[0] > e->items - 1)
611 return -EINVAL;
612 gbvalue.value.enumerated_item[0] =
613 cpu_to_le32(ucontrol->value.enumerated.item[0]);
614
615 if (e->shift_l != e->shift_r) {
616 if (ucontrol->value.enumerated.item[1] > e->items - 1)
617 return -EINVAL;
618 gbvalue.value.enumerated_item[1] =
619 cpu_to_le32(ucontrol->value.enumerated.item[1]);
620 }
621
622 bundle = to_gb_bundle(module->dev);
623
624 ret = gb_pm_runtime_get_sync(bundle);
625 if (ret)
626 return ret;
627
628 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
629 GB_AUDIO_INVALID_INDEX, &gbvalue);
630
631 gb_pm_runtime_put_autosuspend(bundle);
632
633 if (ret) {
634 dev_err_ratelimited(comp->dev, "%d:Error in %s for %s\n",
635 ret, __func__, kcontrol->id.name);
636 }
637
638 return ret;
639 }
640
gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info * gb,struct snd_kcontrol_new * kctl,struct gb_audio_control * ctl)641 static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
642 struct snd_kcontrol_new *kctl,
643 struct gb_audio_control *ctl)
644 {
645 struct soc_enum *gbe;
646 struct gb_audio_enumerated *gb_enum;
647 int i;
648
649 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
650 if (!gbe)
651 return -ENOMEM;
652
653 gb_enum = &ctl->info.value.enumerated;
654
655 /* since count=1, and reg is dummy */
656 gbe->items = le32_to_cpu(gb_enum->items);
657 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
658
659 /* debug enum info */
660 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
661 le16_to_cpu(gb_enum->names_length));
662 for (i = 0; i < gbe->items; i++)
663 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
664
665 *kctl = (struct snd_kcontrol_new)
666 SOC_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_ctl_get,
667 gbcodec_enum_ctl_put);
668 return 0;
669 }
670
gbaudio_tplg_create_kcontrol(struct gbaudio_module_info * gb,struct snd_kcontrol_new * kctl,struct gb_audio_control * ctl)671 static int gbaudio_tplg_create_kcontrol(struct gbaudio_module_info *gb,
672 struct snd_kcontrol_new *kctl,
673 struct gb_audio_control *ctl)
674 {
675 int ret = 0;
676 struct gbaudio_ctl_pvt *ctldata;
677
678 switch (ctl->iface) {
679 case SNDRV_CTL_ELEM_IFACE_MIXER:
680 switch (ctl->info.type) {
681 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
682 ret = gbaudio_tplg_create_enum_kctl(gb, kctl, ctl);
683 break;
684 default:
685 ctldata = devm_kzalloc(gb->dev,
686 sizeof(struct gbaudio_ctl_pvt),
687 GFP_KERNEL);
688 if (!ctldata)
689 return -ENOMEM;
690 ctldata->ctl_id = ctl->id;
691 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
692 ctldata->access = le32_to_cpu(ctl->access);
693 ctldata->vcount = ctl->count_values;
694 ctldata->info = &ctl->info;
695 *kctl = (struct snd_kcontrol_new)
696 SOC_MIXER_GB(ctl->name, ctl->count, ctldata);
697 ctldata = NULL;
698 break;
699 }
700 break;
701 default:
702 return -EINVAL;
703 }
704
705 dev_dbg(gb->dev, "%s:%d control created\n", ctl->name, ctl->id);
706 return ret;
707 }
708
gbcodec_enum_dapm_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)709 static int gbcodec_enum_dapm_ctl_get(struct snd_kcontrol *kcontrol,
710 struct snd_ctl_elem_value *ucontrol)
711 {
712 int ret, ctl_id;
713 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
714 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
715 struct gbaudio_module_info *module;
716 struct gb_audio_ctl_elem_value gbvalue;
717 struct device *codec_dev = widget->dapm->dev;
718 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
719 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
720 struct gb_bundle *bundle;
721
722 module = find_gb_module(gb, kcontrol->id.name);
723 if (!module)
724 return -EINVAL;
725
726 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
727 if (ctl_id < 0)
728 return -EINVAL;
729
730 bundle = to_gb_bundle(module->dev);
731
732 ret = gb_pm_runtime_get_sync(bundle);
733 if (ret)
734 return ret;
735
736 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
737 GB_AUDIO_INVALID_INDEX, &gbvalue);
738
739 gb_pm_runtime_put_autosuspend(bundle);
740
741 if (ret) {
742 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
743 __func__, kcontrol->id.name);
744 return ret;
745 }
746
747 ucontrol->value.enumerated.item[0] = le32_to_cpu(gbvalue.value.enumerated_item[0]);
748 if (e->shift_l != e->shift_r)
749 ucontrol->value.enumerated.item[1] =
750 le32_to_cpu(gbvalue.value.enumerated_item[1]);
751
752 return 0;
753 }
754
gbcodec_enum_dapm_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)755 static int gbcodec_enum_dapm_ctl_put(struct snd_kcontrol *kcontrol,
756 struct snd_ctl_elem_value *ucontrol)
757 {
758 int ret, wi, ctl_id;
759 unsigned int val, mux, change;
760 unsigned int mask;
761 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
762 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
763 struct gb_audio_ctl_elem_value gbvalue;
764 struct gbaudio_module_info *module;
765 struct device *codec_dev = widget->dapm->dev;
766 struct gbaudio_codec_info *gb = dev_get_drvdata(codec_dev);
767 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
768 struct gb_bundle *bundle;
769
770 if (ucontrol->value.enumerated.item[0] > e->items - 1)
771 return -EINVAL;
772
773 module = find_gb_module(gb, kcontrol->id.name);
774 if (!module)
775 return -EINVAL;
776
777 ctl_id = gbaudio_map_wcontrolname(module, kcontrol->id.name);
778 if (ctl_id < 0)
779 return -EINVAL;
780
781 change = 0;
782 bundle = to_gb_bundle(module->dev);
783
784 ret = gb_pm_runtime_get_sync(bundle);
785 if (ret)
786 return ret;
787
788 ret = gb_audio_gb_get_control(module->mgmt_connection, ctl_id,
789 GB_AUDIO_INVALID_INDEX, &gbvalue);
790
791 gb_pm_runtime_put_autosuspend(bundle);
792
793 if (ret) {
794 dev_err_ratelimited(codec_dev, "%d:Error in %s for %s\n", ret,
795 __func__, kcontrol->id.name);
796 return ret;
797 }
798
799 mux = ucontrol->value.enumerated.item[0];
800 val = mux << e->shift_l;
801 mask = e->mask << e->shift_l;
802
803 if (le32_to_cpu(gbvalue.value.enumerated_item[0]) !=
804 ucontrol->value.enumerated.item[0]) {
805 change = 1;
806 gbvalue.value.enumerated_item[0] =
807 cpu_to_le32(ucontrol->value.enumerated.item[0]);
808 }
809
810 if (e->shift_l != e->shift_r) {
811 if (ucontrol->value.enumerated.item[1] > e->items - 1)
812 return -EINVAL;
813 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
814 mask |= e->mask << e->shift_r;
815 if (le32_to_cpu(gbvalue.value.enumerated_item[1]) !=
816 ucontrol->value.enumerated.item[1]) {
817 change = 1;
818 gbvalue.value.enumerated_item[1] =
819 cpu_to_le32(ucontrol->value.enumerated.item[1]);
820 }
821 }
822
823 if (change) {
824 ret = gb_pm_runtime_get_sync(bundle);
825 if (ret)
826 return ret;
827
828 ret = gb_audio_gb_set_control(module->mgmt_connection, ctl_id,
829 GB_AUDIO_INVALID_INDEX, &gbvalue);
830
831 gb_pm_runtime_put_autosuspend(bundle);
832
833 if (ret) {
834 dev_err_ratelimited(codec_dev,
835 "%d:Error in %s for %s\n", ret,
836 __func__, kcontrol->id.name);
837 }
838 for (wi = 0; wi < wlist->num_widgets; wi++) {
839 widget = wlist->widgets[wi];
840 snd_soc_dapm_mux_update_power(widget->dapm, kcontrol,
841 val, e, NULL);
842 }
843 }
844
845 return change;
846 }
847
gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info * gb,struct snd_kcontrol_new * kctl,struct gb_audio_control * ctl)848 static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
849 struct snd_kcontrol_new *kctl,
850 struct gb_audio_control *ctl)
851 {
852 struct soc_enum *gbe;
853 struct gb_audio_enumerated *gb_enum;
854 int i;
855
856 gbe = devm_kzalloc(gb->dev, sizeof(*gbe), GFP_KERNEL);
857 if (!gbe)
858 return -ENOMEM;
859
860 gb_enum = &ctl->info.value.enumerated;
861
862 /* since count=1, and reg is dummy */
863 gbe->items = le32_to_cpu(gb_enum->items);
864 gbe->texts = gb_generate_enum_strings(gb, gb_enum);
865
866 /* debug enum info */
867 dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
868 le16_to_cpu(gb_enum->names_length));
869 for (i = 0; i < gbe->items; i++)
870 dev_dbg(gb->dev, "src[%d]: %s\n", i, gbe->texts[i]);
871
872 *kctl = (struct snd_kcontrol_new)
873 SOC_DAPM_ENUM_EXT(ctl->name, *gbe, gbcodec_enum_dapm_ctl_get,
874 gbcodec_enum_dapm_ctl_put);
875 return 0;
876 }
877
gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info * gb,struct snd_kcontrol_new * kctl,struct gb_audio_control * ctl)878 static int gbaudio_tplg_create_mixer_ctl(struct gbaudio_module_info *gb,
879 struct snd_kcontrol_new *kctl,
880 struct gb_audio_control *ctl)
881 {
882 struct gbaudio_ctl_pvt *ctldata;
883
884 ctldata = devm_kzalloc(gb->dev, sizeof(struct gbaudio_ctl_pvt),
885 GFP_KERNEL);
886 if (!ctldata)
887 return -ENOMEM;
888 ctldata->ctl_id = ctl->id;
889 ctldata->data_cport = le16_to_cpu(ctl->data_cport);
890 ctldata->access = le32_to_cpu(ctl->access);
891 ctldata->vcount = ctl->count_values;
892 ctldata->info = &ctl->info;
893 *kctl = (struct snd_kcontrol_new)
894 SOC_DAPM_MIXER_GB(ctl->name, ctl->count, ctldata);
895
896 return 0;
897 }
898
gbaudio_tplg_create_wcontrol(struct gbaudio_module_info * gb,struct snd_kcontrol_new * kctl,struct gb_audio_control * ctl)899 static int gbaudio_tplg_create_wcontrol(struct gbaudio_module_info *gb,
900 struct snd_kcontrol_new *kctl,
901 struct gb_audio_control *ctl)
902 {
903 int ret;
904
905 switch (ctl->iface) {
906 case SNDRV_CTL_ELEM_IFACE_MIXER:
907 switch (ctl->info.type) {
908 case GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED:
909 ret = gbaudio_tplg_create_enum_ctl(gb, kctl, ctl);
910 break;
911 default:
912 ret = gbaudio_tplg_create_mixer_ctl(gb, kctl, ctl);
913 break;
914 }
915 break;
916 default:
917 return -EINVAL;
918 }
919
920 dev_dbg(gb->dev, "%s:%d DAPM control created, ret:%d\n", ctl->name,
921 ctl->id, ret);
922 return ret;
923 }
924
gbaudio_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)925 static int gbaudio_widget_event(struct snd_soc_dapm_widget *w,
926 struct snd_kcontrol *kcontrol, int event)
927 {
928 int wid;
929 int ret;
930 struct device *codec_dev = w->dapm->dev;
931 struct gbaudio_codec_info *gbcodec = dev_get_drvdata(codec_dev);
932 struct gbaudio_module_info *module;
933 struct gb_bundle *bundle;
934
935 dev_dbg(codec_dev, "%s %s %d\n", __func__, w->name, event);
936
937 /* Find relevant module */
938 module = find_gb_module(gbcodec, w->name);
939 if (!module)
940 return -EINVAL;
941
942 /* map name to widget id */
943 wid = gbaudio_map_widgetname(module, w->name);
944 if (wid < 0) {
945 dev_err(codec_dev, "Invalid widget name:%s\n", w->name);
946 return -EINVAL;
947 }
948
949 bundle = to_gb_bundle(module->dev);
950
951 ret = gb_pm_runtime_get_sync(bundle);
952 if (ret)
953 return ret;
954
955 switch (event) {
956 case SND_SOC_DAPM_PRE_PMU:
957 ret = gb_audio_gb_enable_widget(module->mgmt_connection, wid);
958 if (!ret)
959 ret = gbaudio_module_update(gbcodec, w, module, 1);
960 break;
961 case SND_SOC_DAPM_POST_PMD:
962 ret = gb_audio_gb_disable_widget(module->mgmt_connection, wid);
963 if (!ret)
964 ret = gbaudio_module_update(gbcodec, w, module, 0);
965 break;
966 }
967 if (ret)
968 dev_err_ratelimited(codec_dev,
969 "%d: widget, event:%d failed:%d\n", wid,
970 event, ret);
971
972 gb_pm_runtime_put_autosuspend(bundle);
973
974 return ret;
975 }
976
gbaudio_tplg_create_widget(struct gbaudio_module_info * module,struct snd_soc_dapm_widget * dw,struct gb_audio_widget * w,int * w_size)977 static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
978 struct snd_soc_dapm_widget *dw,
979 struct gb_audio_widget *w, int *w_size)
980 {
981 int i, ret, csize;
982 struct snd_kcontrol_new *widget_kctls;
983 struct gb_audio_control *curr;
984 struct gbaudio_control *control, *_control;
985 size_t size;
986 char temp_name[NAME_SIZE];
987
988 ret = gbaudio_validate_kcontrol_count(w);
989 if (ret) {
990 dev_err(module->dev, "Invalid kcontrol count=%d for %s\n",
991 w->ncontrols, w->name);
992 return ret;
993 }
994
995 /* allocate memory for kcontrol */
996 if (w->ncontrols) {
997 size = sizeof(struct snd_kcontrol_new) * w->ncontrols;
998 widget_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
999 if (!widget_kctls)
1000 return -ENOMEM;
1001 }
1002
1003 *w_size = sizeof(struct gb_audio_widget);
1004
1005 /* create relevant kcontrols */
1006 curr = w->ctl;
1007 for (i = 0; i < w->ncontrols; i++) {
1008 ret = gbaudio_tplg_create_wcontrol(module, &widget_kctls[i],
1009 curr);
1010 if (ret) {
1011 dev_err(module->dev,
1012 "%s:%d type widget_ctl not supported\n",
1013 curr->name, curr->iface);
1014 goto error;
1015 }
1016 control = devm_kzalloc(module->dev,
1017 sizeof(struct gbaudio_control),
1018 GFP_KERNEL);
1019 if (!control) {
1020 ret = -ENOMEM;
1021 goto error;
1022 }
1023 control->id = curr->id;
1024 control->name = curr->name;
1025 control->wname = w->name;
1026
1027 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1028 struct gb_audio_enumerated *gbenum =
1029 &curr->info.value.enumerated;
1030
1031 csize = offsetof(struct gb_audio_control, info);
1032 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1033 csize += offsetof(struct gb_audio_enumerated, names);
1034 csize += le16_to_cpu(gbenum->names_length);
1035 control->texts = (const char * const *)
1036 gb_generate_enum_strings(module, gbenum);
1037 control->items = le32_to_cpu(gbenum->items);
1038 } else {
1039 csize = sizeof(struct gb_audio_control);
1040 }
1041
1042 *w_size += csize;
1043 curr = (void *)curr + csize;
1044 list_add(&control->list, &module->widget_ctl_list);
1045 dev_dbg(module->dev, "%s: control of type %d created\n",
1046 widget_kctls[i].name, widget_kctls[i].iface);
1047 }
1048
1049 /* Prefix dev_id to widget control_name */
1050 strlcpy(temp_name, w->name, NAME_SIZE);
1051 snprintf(w->name, NAME_SIZE, "GB %d %s", module->dev_id, temp_name);
1052
1053 switch (w->type) {
1054 case snd_soc_dapm_spk:
1055 *dw = (struct snd_soc_dapm_widget)
1056 SND_SOC_DAPM_SPK(w->name, gbcodec_event_spk);
1057 module->op_devices |= GBAUDIO_DEVICE_OUT_SPEAKER;
1058 break;
1059 case snd_soc_dapm_hp:
1060 *dw = (struct snd_soc_dapm_widget)
1061 SND_SOC_DAPM_HP(w->name, gbcodec_event_hp);
1062 module->op_devices |= (GBAUDIO_DEVICE_OUT_WIRED_HEADSET
1063 | GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1064 module->ip_devices |= GBAUDIO_DEVICE_IN_WIRED_HEADSET;
1065 break;
1066 case snd_soc_dapm_mic:
1067 *dw = (struct snd_soc_dapm_widget)
1068 SND_SOC_DAPM_MIC(w->name, gbcodec_event_int_mic);
1069 module->ip_devices |= GBAUDIO_DEVICE_IN_BUILTIN_MIC;
1070 break;
1071 case snd_soc_dapm_output:
1072 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_OUTPUT(w->name);
1073 break;
1074 case snd_soc_dapm_input:
1075 *dw = (struct snd_soc_dapm_widget)SND_SOC_DAPM_INPUT(w->name);
1076 break;
1077 case snd_soc_dapm_switch:
1078 *dw = (struct snd_soc_dapm_widget)
1079 SND_SOC_DAPM_SWITCH_E(w->name, SND_SOC_NOPM, 0, 0,
1080 widget_kctls,
1081 gbaudio_widget_event,
1082 SND_SOC_DAPM_PRE_PMU |
1083 SND_SOC_DAPM_POST_PMD);
1084 break;
1085 case snd_soc_dapm_pga:
1086 *dw = (struct snd_soc_dapm_widget)
1087 SND_SOC_DAPM_PGA_E(w->name, SND_SOC_NOPM, 0, 0, NULL, 0,
1088 gbaudio_widget_event,
1089 SND_SOC_DAPM_PRE_PMU |
1090 SND_SOC_DAPM_POST_PMD);
1091 break;
1092 case snd_soc_dapm_mixer:
1093 *dw = (struct snd_soc_dapm_widget)
1094 SND_SOC_DAPM_MIXER_E(w->name, SND_SOC_NOPM, 0, 0, NULL,
1095 0, gbaudio_widget_event,
1096 SND_SOC_DAPM_PRE_PMU |
1097 SND_SOC_DAPM_POST_PMD);
1098 break;
1099 case snd_soc_dapm_mux:
1100 *dw = (struct snd_soc_dapm_widget)
1101 SND_SOC_DAPM_MUX_E(w->name, SND_SOC_NOPM, 0, 0,
1102 widget_kctls, gbaudio_widget_event,
1103 SND_SOC_DAPM_PRE_PMU |
1104 SND_SOC_DAPM_POST_PMD);
1105 break;
1106 case snd_soc_dapm_aif_in:
1107 *dw = (struct snd_soc_dapm_widget)
1108 SND_SOC_DAPM_AIF_IN_E(w->name, w->sname, 0,
1109 SND_SOC_NOPM,
1110 0, 0, gbaudio_widget_event,
1111 SND_SOC_DAPM_PRE_PMU |
1112 SND_SOC_DAPM_POST_PMD);
1113 break;
1114 case snd_soc_dapm_aif_out:
1115 *dw = (struct snd_soc_dapm_widget)
1116 SND_SOC_DAPM_AIF_OUT_E(w->name, w->sname, 0,
1117 SND_SOC_NOPM,
1118 0, 0, gbaudio_widget_event,
1119 SND_SOC_DAPM_PRE_PMU |
1120 SND_SOC_DAPM_POST_PMD);
1121 break;
1122 default:
1123 ret = -EINVAL;
1124 goto error;
1125 }
1126
1127 dev_dbg(module->dev, "%s: widget of type %d created\n", dw->name,
1128 dw->id);
1129 return 0;
1130 error:
1131 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1132 list) {
1133 list_del(&control->list);
1134 devm_kfree(module->dev, control);
1135 }
1136 return ret;
1137 }
1138
gbaudio_tplg_process_kcontrols(struct gbaudio_module_info * module,struct gb_audio_control * controls)1139 static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
1140 struct gb_audio_control *controls)
1141 {
1142 int i, csize, ret;
1143 struct snd_kcontrol_new *dapm_kctls;
1144 struct gb_audio_control *curr;
1145 struct gbaudio_control *control, *_control;
1146 size_t size;
1147 char temp_name[NAME_SIZE];
1148
1149 size = sizeof(struct snd_kcontrol_new) * module->num_controls;
1150 dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
1151 if (!dapm_kctls)
1152 return -ENOMEM;
1153
1154 curr = controls;
1155 for (i = 0; i < module->num_controls; i++) {
1156 ret = gbaudio_tplg_create_kcontrol(module, &dapm_kctls[i],
1157 curr);
1158 if (ret) {
1159 dev_err(module->dev, "%s:%d type not supported\n",
1160 curr->name, curr->iface);
1161 goto error;
1162 }
1163 control = devm_kzalloc(module->dev, sizeof(struct
1164 gbaudio_control),
1165 GFP_KERNEL);
1166 if (!control) {
1167 ret = -ENOMEM;
1168 goto error;
1169 }
1170 control->id = curr->id;
1171 /* Prefix dev_id to widget_name */
1172 strlcpy(temp_name, curr->name, NAME_SIZE);
1173 snprintf(curr->name, NAME_SIZE, "GB %d %s", module->dev_id,
1174 temp_name);
1175 control->name = curr->name;
1176 if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
1177 struct gb_audio_enumerated *gbenum =
1178 &curr->info.value.enumerated;
1179
1180 csize = offsetof(struct gb_audio_control, info);
1181 csize += offsetof(struct gb_audio_ctl_elem_info, value);
1182 csize += offsetof(struct gb_audio_enumerated, names);
1183 csize += le16_to_cpu(gbenum->names_length);
1184 control->texts = (const char * const *)
1185 gb_generate_enum_strings(module, gbenum);
1186 control->items = le32_to_cpu(gbenum->items);
1187 } else {
1188 csize = sizeof(struct gb_audio_control);
1189 }
1190
1191 list_add(&control->list, &module->ctl_list);
1192 dev_dbg(module->dev, "%d:%s created of type %d\n", curr->id,
1193 curr->name, curr->info.type);
1194 curr = (void *)curr + csize;
1195 }
1196 module->controls = dapm_kctls;
1197
1198 return 0;
1199 error:
1200 list_for_each_entry_safe(control, _control, &module->ctl_list,
1201 list) {
1202 list_del(&control->list);
1203 devm_kfree(module->dev, control);
1204 }
1205 devm_kfree(module->dev, dapm_kctls);
1206 return ret;
1207 }
1208
gbaudio_tplg_process_widgets(struct gbaudio_module_info * module,struct gb_audio_widget * widgets)1209 static int gbaudio_tplg_process_widgets(struct gbaudio_module_info *module,
1210 struct gb_audio_widget *widgets)
1211 {
1212 int i, ret, w_size;
1213 struct snd_soc_dapm_widget *dapm_widgets;
1214 struct gb_audio_widget *curr;
1215 struct gbaudio_widget *widget, *_widget;
1216 size_t size;
1217
1218 size = sizeof(struct snd_soc_dapm_widget) * module->num_dapm_widgets;
1219 dapm_widgets = devm_kzalloc(module->dev, size, GFP_KERNEL);
1220 if (!dapm_widgets)
1221 return -ENOMEM;
1222
1223 curr = widgets;
1224 for (i = 0; i < module->num_dapm_widgets; i++) {
1225 ret = gbaudio_tplg_create_widget(module, &dapm_widgets[i],
1226 curr, &w_size);
1227 if (ret) {
1228 dev_err(module->dev, "%s:%d type not supported\n",
1229 curr->name, curr->type);
1230 goto error;
1231 }
1232 widget = devm_kzalloc(module->dev, sizeof(struct
1233 gbaudio_widget),
1234 GFP_KERNEL);
1235 if (!widget) {
1236 ret = -ENOMEM;
1237 goto error;
1238 }
1239 widget->id = curr->id;
1240 widget->name = curr->name;
1241 list_add(&widget->list, &module->widget_list);
1242 curr = (void *)curr + w_size;
1243 }
1244 module->dapm_widgets = dapm_widgets;
1245
1246 return 0;
1247
1248 error:
1249 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1250 list) {
1251 list_del(&widget->list);
1252 devm_kfree(module->dev, widget);
1253 }
1254 devm_kfree(module->dev, dapm_widgets);
1255 return ret;
1256 }
1257
gbaudio_tplg_process_routes(struct gbaudio_module_info * module,struct gb_audio_route * routes)1258 static int gbaudio_tplg_process_routes(struct gbaudio_module_info *module,
1259 struct gb_audio_route *routes)
1260 {
1261 int i, ret;
1262 struct snd_soc_dapm_route *dapm_routes;
1263 struct gb_audio_route *curr;
1264 size_t size;
1265
1266 size = sizeof(struct snd_soc_dapm_route) * module->num_dapm_routes;
1267 dapm_routes = devm_kzalloc(module->dev, size, GFP_KERNEL);
1268 if (!dapm_routes)
1269 return -ENOMEM;
1270
1271 module->dapm_routes = dapm_routes;
1272 curr = routes;
1273
1274 for (i = 0; i < module->num_dapm_routes; i++) {
1275 dapm_routes->sink =
1276 gbaudio_map_widgetid(module, curr->destination_id);
1277 if (!dapm_routes->sink) {
1278 dev_err(module->dev, "%d:%d:%d:%d - Invalid sink\n",
1279 curr->source_id, curr->destination_id,
1280 curr->control_id, curr->index);
1281 ret = -EINVAL;
1282 goto error;
1283 }
1284 dapm_routes->source =
1285 gbaudio_map_widgetid(module, curr->source_id);
1286 if (!dapm_routes->source) {
1287 dev_err(module->dev, "%d:%d:%d:%d - Invalid source\n",
1288 curr->source_id, curr->destination_id,
1289 curr->control_id, curr->index);
1290 ret = -EINVAL;
1291 goto error;
1292 }
1293 dapm_routes->control =
1294 gbaudio_map_controlid(module,
1295 curr->control_id,
1296 curr->index);
1297 if ((curr->control_id != GBAUDIO_INVALID_ID) &&
1298 !dapm_routes->control) {
1299 dev_err(module->dev, "%d:%d:%d:%d - Invalid control\n",
1300 curr->source_id, curr->destination_id,
1301 curr->control_id, curr->index);
1302 ret = -EINVAL;
1303 goto error;
1304 }
1305 dev_dbg(module->dev, "Route {%s, %s, %s}\n", dapm_routes->sink,
1306 (dapm_routes->control) ? dapm_routes->control : "NULL",
1307 dapm_routes->source);
1308 dapm_routes++;
1309 curr++;
1310 }
1311
1312 return 0;
1313
1314 error:
1315 devm_kfree(module->dev, module->dapm_routes);
1316 return ret;
1317 }
1318
gbaudio_tplg_process_header(struct gbaudio_module_info * module,struct gb_audio_topology * tplg_data)1319 static int gbaudio_tplg_process_header(struct gbaudio_module_info *module,
1320 struct gb_audio_topology *tplg_data)
1321 {
1322 /* fetch no. of kcontrols, widgets & routes */
1323 module->num_controls = tplg_data->num_controls;
1324 module->num_dapm_widgets = tplg_data->num_widgets;
1325 module->num_dapm_routes = tplg_data->num_routes;
1326
1327 /* update block offset */
1328 module->dai_offset = (unsigned long)&tplg_data->data;
1329 module->control_offset = module->dai_offset +
1330 le32_to_cpu(tplg_data->size_dais);
1331 module->widget_offset = module->control_offset +
1332 le32_to_cpu(tplg_data->size_controls);
1333 module->route_offset = module->widget_offset +
1334 le32_to_cpu(tplg_data->size_widgets);
1335
1336 dev_dbg(module->dev, "DAI offset is 0x%lx\n", module->dai_offset);
1337 dev_dbg(module->dev, "control offset is %lx\n",
1338 module->control_offset);
1339 dev_dbg(module->dev, "widget offset is %lx\n", module->widget_offset);
1340 dev_dbg(module->dev, "route offset is %lx\n", module->route_offset);
1341
1342 return 0;
1343 }
1344
gbaudio_tplg_parse_data(struct gbaudio_module_info * module,struct gb_audio_topology * tplg_data)1345 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
1346 struct gb_audio_topology *tplg_data)
1347 {
1348 int ret;
1349 struct gb_audio_control *controls;
1350 struct gb_audio_widget *widgets;
1351 struct gb_audio_route *routes;
1352 unsigned int jack_type;
1353
1354 if (!tplg_data)
1355 return -EINVAL;
1356
1357 ret = gbaudio_tplg_process_header(module, tplg_data);
1358 if (ret) {
1359 dev_err(module->dev, "%d: Error in parsing topology header\n",
1360 ret);
1361 return ret;
1362 }
1363
1364 /* process control */
1365 controls = (struct gb_audio_control *)module->control_offset;
1366 ret = gbaudio_tplg_process_kcontrols(module, controls);
1367 if (ret) {
1368 dev_err(module->dev,
1369 "%d: Error in parsing controls data\n", ret);
1370 return ret;
1371 }
1372 dev_dbg(module->dev, "Control parsing finished\n");
1373
1374 /* process widgets */
1375 widgets = (struct gb_audio_widget *)module->widget_offset;
1376 ret = gbaudio_tplg_process_widgets(module, widgets);
1377 if (ret) {
1378 dev_err(module->dev,
1379 "%d: Error in parsing widgets data\n", ret);
1380 return ret;
1381 }
1382 dev_dbg(module->dev, "Widget parsing finished\n");
1383
1384 /* process route */
1385 routes = (struct gb_audio_route *)module->route_offset;
1386 ret = gbaudio_tplg_process_routes(module, routes);
1387 if (ret) {
1388 dev_err(module->dev,
1389 "%d: Error in parsing routes data\n", ret);
1390 return ret;
1391 }
1392 dev_dbg(module->dev, "Route parsing finished\n");
1393
1394 /* parse jack capabilities */
1395 jack_type = le32_to_cpu(tplg_data->jack_type);
1396 if (jack_type) {
1397 module->jack_mask = jack_type & GBCODEC_JACK_MASK;
1398 module->button_mask = jack_type & GBCODEC_JACK_BUTTON_MASK;
1399 }
1400
1401 return ret;
1402 }
1403
gbaudio_tplg_release(struct gbaudio_module_info * module)1404 void gbaudio_tplg_release(struct gbaudio_module_info *module)
1405 {
1406 struct gbaudio_control *control, *_control;
1407 struct gbaudio_widget *widget, *_widget;
1408
1409 if (!module->topology)
1410 return;
1411
1412 /* release kcontrols */
1413 list_for_each_entry_safe(control, _control, &module->ctl_list,
1414 list) {
1415 list_del(&control->list);
1416 devm_kfree(module->dev, control);
1417 }
1418 if (module->controls)
1419 devm_kfree(module->dev, module->controls);
1420
1421 /* release widget controls */
1422 list_for_each_entry_safe(control, _control, &module->widget_ctl_list,
1423 list) {
1424 list_del(&control->list);
1425 devm_kfree(module->dev, control);
1426 }
1427
1428 /* release widgets */
1429 list_for_each_entry_safe(widget, _widget, &module->widget_list,
1430 list) {
1431 list_del(&widget->list);
1432 devm_kfree(module->dev, widget);
1433 }
1434 if (module->dapm_widgets)
1435 devm_kfree(module->dev, module->dapm_widgets);
1436
1437 /* release routes */
1438 if (module->dapm_routes)
1439 devm_kfree(module->dev, module->dapm_routes);
1440 }
1441