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