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