1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // simple-card-utils.c
4 //
5 // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6
7 #include <linux/clk.h>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_graph.h>
13 #include <sound/jack.h>
14 #include <sound/pcm_params.h>
15 #include <sound/simple_card_utils.h>
16
asoc_simple_fixup_sample_fmt(struct asoc_simple_data * data,struct snd_pcm_hw_params * params)17 static void asoc_simple_fixup_sample_fmt(struct asoc_simple_data *data,
18 struct snd_pcm_hw_params *params)
19 {
20 int i;
21 struct snd_mask *mask = hw_param_mask(params,
22 SNDRV_PCM_HW_PARAM_FORMAT);
23 struct {
24 char *fmt;
25 u32 val;
26 } of_sample_fmt_table[] = {
27 { "s8", SNDRV_PCM_FORMAT_S8},
28 { "s16_le", SNDRV_PCM_FORMAT_S16_LE},
29 { "s24_le", SNDRV_PCM_FORMAT_S24_LE},
30 { "s24_3le", SNDRV_PCM_FORMAT_S24_3LE},
31 { "s32_le", SNDRV_PCM_FORMAT_S32_LE},
32 };
33
34 for (i = 0; i < ARRAY_SIZE(of_sample_fmt_table); i++) {
35 if (!strcmp(data->convert_sample_format,
36 of_sample_fmt_table[i].fmt)) {
37 snd_mask_none(mask);
38 snd_mask_set(mask, of_sample_fmt_table[i].val);
39 break;
40 }
41 }
42 }
43
asoc_simple_parse_convert(struct device_node * np,char * prefix,struct asoc_simple_data * data)44 void asoc_simple_parse_convert(struct device_node *np,
45 char *prefix,
46 struct asoc_simple_data *data)
47 {
48 char prop[128];
49
50 if (!prefix)
51 prefix = "";
52
53 /* sampling rate convert */
54 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
55 of_property_read_u32(np, prop, &data->convert_rate);
56
57 /* channels transfer */
58 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
59 of_property_read_u32(np, prop, &data->convert_channels);
60
61 /* convert sample format */
62 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-sample-format");
63 of_property_read_string(np, prop, &data->convert_sample_format);
64 }
65 EXPORT_SYMBOL_GPL(asoc_simple_parse_convert);
66
67 /**
68 * asoc_simple_is_convert_required() - Query if HW param conversion was requested
69 * @data: Link data.
70 *
71 * Returns true if any HW param conversion was requested for this DAI link with
72 * any "convert-xxx" properties.
73 */
asoc_simple_is_convert_required(const struct asoc_simple_data * data)74 bool asoc_simple_is_convert_required(const struct asoc_simple_data *data)
75 {
76 return data->convert_rate ||
77 data->convert_channels ||
78 data->convert_sample_format;
79 }
80 EXPORT_SYMBOL_GPL(asoc_simple_is_convert_required);
81
asoc_simple_parse_daifmt(struct device * dev,struct device_node * node,struct device_node * codec,char * prefix,unsigned int * retfmt)82 int asoc_simple_parse_daifmt(struct device *dev,
83 struct device_node *node,
84 struct device_node *codec,
85 char *prefix,
86 unsigned int *retfmt)
87 {
88 struct device_node *bitclkmaster = NULL;
89 struct device_node *framemaster = NULL;
90 unsigned int daifmt;
91
92 daifmt = snd_soc_daifmt_parse_format(node, prefix);
93
94 snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
95 if (!bitclkmaster && !framemaster) {
96 /*
97 * No dai-link level and master setting was not found from
98 * sound node level, revert back to legacy DT parsing and
99 * take the settings from codec node.
100 */
101 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
102
103 daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
104 } else {
105 daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
106 ((codec == bitclkmaster) << 4) | (codec == framemaster));
107 }
108
109 of_node_put(bitclkmaster);
110 of_node_put(framemaster);
111
112 *retfmt = daifmt;
113
114 return 0;
115 }
116 EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt);
117
asoc_simple_parse_tdm_width_map(struct device * dev,struct device_node * np,struct asoc_simple_dai * dai)118 int asoc_simple_parse_tdm_width_map(struct device *dev, struct device_node *np,
119 struct asoc_simple_dai *dai)
120 {
121 u32 *array_values, *p;
122 int n, i, ret;
123
124 if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
125 return 0;
126
127 n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
128 if (n % 3) {
129 dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
130 return -EINVAL;
131 }
132
133 dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
134 if (!dai->tdm_width_map)
135 return -ENOMEM;
136
137 array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
138 if (!array_values)
139 return -ENOMEM;
140
141 ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
142 if (ret < 0) {
143 dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
144 goto out;
145 }
146
147 p = array_values;
148 for (i = 0; i < n / 3; ++i) {
149 dai->tdm_width_map[i].sample_bits = *p++;
150 dai->tdm_width_map[i].slot_width = *p++;
151 dai->tdm_width_map[i].slot_count = *p++;
152 }
153
154 dai->n_tdm_widths = i;
155 ret = 0;
156 out:
157 kfree(array_values);
158
159 return ret;
160 }
161 EXPORT_SYMBOL_GPL(asoc_simple_parse_tdm_width_map);
162
asoc_simple_set_dailink_name(struct device * dev,struct snd_soc_dai_link * dai_link,const char * fmt,...)163 int asoc_simple_set_dailink_name(struct device *dev,
164 struct snd_soc_dai_link *dai_link,
165 const char *fmt, ...)
166 {
167 va_list ap;
168 char *name = NULL;
169 int ret = -ENOMEM;
170
171 va_start(ap, fmt);
172 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
173 va_end(ap);
174
175 if (name) {
176 ret = 0;
177
178 dai_link->name = name;
179 dai_link->stream_name = name;
180 }
181
182 return ret;
183 }
184 EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name);
185
asoc_simple_parse_card_name(struct snd_soc_card * card,char * prefix)186 int asoc_simple_parse_card_name(struct snd_soc_card *card,
187 char *prefix)
188 {
189 int ret;
190
191 if (!prefix)
192 prefix = "";
193
194 /* Parse the card name from DT */
195 ret = snd_soc_of_parse_card_name(card, "label");
196 if (ret < 0 || !card->name) {
197 char prop[128];
198
199 snprintf(prop, sizeof(prop), "%sname", prefix);
200 ret = snd_soc_of_parse_card_name(card, prop);
201 if (ret < 0)
202 return ret;
203 }
204
205 if (!card->name && card->dai_link)
206 card->name = card->dai_link->name;
207
208 return 0;
209 }
210 EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name);
211
asoc_simple_clk_enable(struct asoc_simple_dai * dai)212 static int asoc_simple_clk_enable(struct asoc_simple_dai *dai)
213 {
214 if (dai)
215 return clk_prepare_enable(dai->clk);
216
217 return 0;
218 }
219
asoc_simple_clk_disable(struct asoc_simple_dai * dai)220 static void asoc_simple_clk_disable(struct asoc_simple_dai *dai)
221 {
222 if (dai)
223 clk_disable_unprepare(dai->clk);
224 }
225
asoc_simple_parse_clk(struct device * dev,struct device_node * node,struct asoc_simple_dai * simple_dai,struct snd_soc_dai_link_component * dlc)226 int asoc_simple_parse_clk(struct device *dev,
227 struct device_node *node,
228 struct asoc_simple_dai *simple_dai,
229 struct snd_soc_dai_link_component *dlc)
230 {
231 struct clk *clk;
232 u32 val;
233
234 /*
235 * Parse dai->sysclk come from "clocks = <&xxx>"
236 * (if system has common clock)
237 * or "system-clock-frequency = <xxx>"
238 * or device's module clock.
239 */
240 clk = devm_get_clk_from_child(dev, node, NULL);
241 simple_dai->clk_fixed = of_property_read_bool(
242 node, "system-clock-fixed");
243 if (!IS_ERR(clk)) {
244 simple_dai->sysclk = clk_get_rate(clk);
245
246 simple_dai->clk = clk;
247 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
248 simple_dai->sysclk = val;
249 simple_dai->clk_fixed = true;
250 } else {
251 clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
252 if (!IS_ERR(clk))
253 simple_dai->sysclk = clk_get_rate(clk);
254 }
255
256 if (of_property_read_bool(node, "system-clock-direction-out"))
257 simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
258
259 return 0;
260 }
261 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk);
262
asoc_simple_check_fixed_sysclk(struct device * dev,struct asoc_simple_dai * dai,unsigned int * fixed_sysclk)263 static int asoc_simple_check_fixed_sysclk(struct device *dev,
264 struct asoc_simple_dai *dai,
265 unsigned int *fixed_sysclk)
266 {
267 if (dai->clk_fixed) {
268 if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
269 dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
270 *fixed_sysclk, dai->sysclk);
271 return -EINVAL;
272 }
273 *fixed_sysclk = dai->sysclk;
274 }
275
276 return 0;
277 }
278
asoc_simple_startup(struct snd_pcm_substream * substream)279 int asoc_simple_startup(struct snd_pcm_substream *substream)
280 {
281 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
282 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
283 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
284 struct asoc_simple_dai *dai;
285 unsigned int fixed_sysclk = 0;
286 int i1, i2, i;
287 int ret;
288
289 for_each_prop_dai_cpu(props, i1, dai) {
290 ret = asoc_simple_clk_enable(dai);
291 if (ret)
292 goto cpu_err;
293 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
294 if (ret)
295 goto cpu_err;
296 }
297
298 for_each_prop_dai_codec(props, i2, dai) {
299 ret = asoc_simple_clk_enable(dai);
300 if (ret)
301 goto codec_err;
302 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
303 if (ret)
304 goto codec_err;
305 }
306
307 if (fixed_sysclk && props->mclk_fs) {
308 unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
309
310 if (fixed_sysclk % props->mclk_fs) {
311 dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
312 fixed_sysclk, props->mclk_fs);
313 ret = -EINVAL;
314 goto codec_err;
315 }
316 ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
317 fixed_rate, fixed_rate);
318 if (ret < 0)
319 goto codec_err;
320 }
321
322 return 0;
323
324 codec_err:
325 for_each_prop_dai_codec(props, i, dai) {
326 if (i >= i2)
327 break;
328 asoc_simple_clk_disable(dai);
329 }
330 cpu_err:
331 for_each_prop_dai_cpu(props, i, dai) {
332 if (i >= i1)
333 break;
334 asoc_simple_clk_disable(dai);
335 }
336 return ret;
337 }
338 EXPORT_SYMBOL_GPL(asoc_simple_startup);
339
asoc_simple_shutdown(struct snd_pcm_substream * substream)340 void asoc_simple_shutdown(struct snd_pcm_substream *substream)
341 {
342 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
343 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
344 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
345 struct asoc_simple_dai *dai;
346 int i;
347
348 for_each_prop_dai_cpu(props, i, dai) {
349 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, i);
350
351 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
352 snd_soc_dai_set_sysclk(cpu_dai,
353 0, 0, SND_SOC_CLOCK_OUT);
354
355 asoc_simple_clk_disable(dai);
356 }
357 for_each_prop_dai_codec(props, i, dai) {
358 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, i);
359
360 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
361 snd_soc_dai_set_sysclk(codec_dai,
362 0, 0, SND_SOC_CLOCK_IN);
363
364 asoc_simple_clk_disable(dai);
365 }
366 }
367 EXPORT_SYMBOL_GPL(asoc_simple_shutdown);
368
asoc_simple_set_clk_rate(struct device * dev,struct asoc_simple_dai * simple_dai,unsigned long rate)369 static int asoc_simple_set_clk_rate(struct device *dev,
370 struct asoc_simple_dai *simple_dai,
371 unsigned long rate)
372 {
373 if (!simple_dai)
374 return 0;
375
376 if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
377 dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
378 return -EINVAL;
379 }
380
381 if (!simple_dai->clk)
382 return 0;
383
384 if (clk_get_rate(simple_dai->clk) == rate)
385 return 0;
386
387 return clk_set_rate(simple_dai->clk, rate);
388 }
389
asoc_simple_set_tdm(struct snd_soc_dai * dai,struct asoc_simple_dai * simple_dai,struct snd_pcm_hw_params * params)390 static int asoc_simple_set_tdm(struct snd_soc_dai *dai,
391 struct asoc_simple_dai *simple_dai,
392 struct snd_pcm_hw_params *params)
393 {
394 int sample_bits = params_width(params);
395 int slot_width, slot_count;
396 int i, ret;
397
398 if (!simple_dai || !simple_dai->tdm_width_map)
399 return 0;
400
401 slot_width = simple_dai->slot_width;
402 slot_count = simple_dai->slots;
403
404 if (slot_width == 0)
405 slot_width = sample_bits;
406
407 for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
408 if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
409 slot_width = simple_dai->tdm_width_map[i].slot_width;
410 slot_count = simple_dai->tdm_width_map[i].slot_count;
411 break;
412 }
413 }
414
415 ret = snd_soc_dai_set_tdm_slot(dai,
416 simple_dai->tx_slot_mask,
417 simple_dai->rx_slot_mask,
418 slot_count,
419 slot_width);
420 if (ret && ret != -ENOTSUPP) {
421 dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
422 return ret;
423 }
424
425 return 0;
426 }
427
asoc_simple_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)428 int asoc_simple_hw_params(struct snd_pcm_substream *substream,
429 struct snd_pcm_hw_params *params)
430 {
431 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
432 struct asoc_simple_dai *pdai;
433 struct snd_soc_dai *sdai;
434 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
435 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
436 unsigned int mclk, mclk_fs = 0;
437 int i, ret;
438
439 if (props->mclk_fs)
440 mclk_fs = props->mclk_fs;
441
442 if (mclk_fs) {
443 struct snd_soc_component *component;
444 mclk = params_rate(params) * mclk_fs;
445
446 for_each_prop_dai_codec(props, i, pdai) {
447 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
448 if (ret < 0)
449 return ret;
450 }
451
452 for_each_prop_dai_cpu(props, i, pdai) {
453 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
454 if (ret < 0)
455 return ret;
456 }
457
458 /* Ensure sysclk is set on all components in case any
459 * (such as platform components) are missed by calls to
460 * snd_soc_dai_set_sysclk.
461 */
462 for_each_rtd_components(rtd, i, component) {
463 ret = snd_soc_component_set_sysclk(component, 0, 0,
464 mclk, SND_SOC_CLOCK_IN);
465 if (ret && ret != -ENOTSUPP)
466 return ret;
467 }
468
469 for_each_rtd_codec_dais(rtd, i, sdai) {
470 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
471 if (ret && ret != -ENOTSUPP)
472 return ret;
473 }
474
475 for_each_rtd_cpu_dais(rtd, i, sdai) {
476 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
477 if (ret && ret != -ENOTSUPP)
478 return ret;
479 }
480 }
481
482 for_each_prop_dai_codec(props, i, pdai) {
483 sdai = asoc_rtd_to_codec(rtd, i);
484 ret = asoc_simple_set_tdm(sdai, pdai, params);
485 if (ret < 0)
486 return ret;
487 }
488
489 for_each_prop_dai_cpu(props, i, pdai) {
490 sdai = asoc_rtd_to_cpu(rtd, i);
491 ret = asoc_simple_set_tdm(sdai, pdai, params);
492 if (ret < 0)
493 return ret;
494 }
495
496 return 0;
497 }
498 EXPORT_SYMBOL_GPL(asoc_simple_hw_params);
499
asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)500 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
501 struct snd_pcm_hw_params *params)
502 {
503 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
504 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
505 struct asoc_simple_data *data = &dai_props->adata;
506 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
507 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
508
509 if (data->convert_rate)
510 rate->min =
511 rate->max = data->convert_rate;
512
513 if (data->convert_channels)
514 channels->min =
515 channels->max = data->convert_channels;
516
517 if (data->convert_sample_format)
518 asoc_simple_fixup_sample_fmt(data, params);
519
520 return 0;
521 }
522 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup);
523
asoc_simple_init_dai(struct snd_soc_dai * dai,struct asoc_simple_dai * simple_dai)524 static int asoc_simple_init_dai(struct snd_soc_dai *dai,
525 struct asoc_simple_dai *simple_dai)
526 {
527 int ret;
528
529 if (!simple_dai)
530 return 0;
531
532 if (simple_dai->sysclk) {
533 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
534 simple_dai->clk_direction);
535 if (ret && ret != -ENOTSUPP) {
536 dev_err(dai->dev, "simple-card: set_sysclk error\n");
537 return ret;
538 }
539 }
540
541 if (simple_dai->slots) {
542 ret = snd_soc_dai_set_tdm_slot(dai,
543 simple_dai->tx_slot_mask,
544 simple_dai->rx_slot_mask,
545 simple_dai->slots,
546 simple_dai->slot_width);
547 if (ret && ret != -ENOTSUPP) {
548 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
549 return ret;
550 }
551 }
552
553 return 0;
554 }
555
asoc_simple_component_is_codec(struct snd_soc_component * component)556 static inline int asoc_simple_component_is_codec(struct snd_soc_component *component)
557 {
558 return component->driver->endianness;
559 }
560
asoc_simple_init_for_codec2codec(struct snd_soc_pcm_runtime * rtd,struct simple_dai_props * dai_props)561 static int asoc_simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
562 struct simple_dai_props *dai_props)
563 {
564 struct snd_soc_dai_link *dai_link = rtd->dai_link;
565 struct snd_soc_component *component;
566 struct snd_soc_pcm_stream *c2c_params;
567 struct snd_pcm_hardware hw;
568 int i, ret, stream;
569
570 /* Do nothing if it already has Codec2Codec settings */
571 if (dai_link->c2c_params)
572 return 0;
573
574 /* Do nothing if it was DPCM :: BE */
575 if (dai_link->no_pcm)
576 return 0;
577
578 /* Only Codecs */
579 for_each_rtd_components(rtd, i, component) {
580 if (!asoc_simple_component_is_codec(component))
581 return 0;
582 }
583
584 /* Assumes the capabilities are the same for all supported streams */
585 for_each_pcm_streams(stream) {
586 ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
587 if (ret == 0)
588 break;
589 }
590
591 if (ret < 0) {
592 dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
593 return ret;
594 }
595
596 c2c_params = devm_kzalloc(rtd->dev, sizeof(*c2c_params), GFP_KERNEL);
597 if (!c2c_params)
598 return -ENOMEM;
599
600 c2c_params->formats = hw.formats;
601 c2c_params->rates = hw.rates;
602 c2c_params->rate_min = hw.rate_min;
603 c2c_params->rate_max = hw.rate_max;
604 c2c_params->channels_min = hw.channels_min;
605 c2c_params->channels_max = hw.channels_max;
606
607 dai_link->c2c_params = c2c_params;
608 dai_link->num_c2c_params = 1;
609
610 return 0;
611 }
612
asoc_simple_dai_init(struct snd_soc_pcm_runtime * rtd)613 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd)
614 {
615 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
616 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
617 struct asoc_simple_dai *dai;
618 int i, ret;
619
620 for_each_prop_dai_codec(props, i, dai) {
621 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, i), dai);
622 if (ret < 0)
623 return ret;
624 }
625 for_each_prop_dai_cpu(props, i, dai) {
626 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, i), dai);
627 if (ret < 0)
628 return ret;
629 }
630
631 ret = asoc_simple_init_for_codec2codec(rtd, props);
632 if (ret < 0)
633 return ret;
634
635 return 0;
636 }
637 EXPORT_SYMBOL_GPL(asoc_simple_dai_init);
638
asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component * platforms,struct snd_soc_dai_link_component * cpus)639 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
640 struct snd_soc_dai_link_component *cpus)
641 {
642 /*
643 * Assumes Platform == CPU
644 *
645 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
646 * are different Component, but are sharing same component->dev.
647 *
648 * Let's assume Platform is same as CPU if it doesn't identify Platform on DT.
649 * see
650 * simple-card.c :: simple_count_noml()
651 */
652 if (!platforms->of_node)
653 snd_soc_dlc_use_cpu_as_platform(platforms, cpus);
654 }
655 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform);
656
asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component * cpus,int is_single_links)657 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
658 int is_single_links)
659 {
660 /*
661 * In soc_bind_dai_link() will check cpu name after
662 * of_node matching if dai_link has cpu_dai_name.
663 * but, it will never match if name was created by
664 * fmt_single_name() remove cpu_dai_name if cpu_args
665 * was 0. See:
666 * fmt_single_name()
667 * fmt_multiple_name()
668 */
669 if (is_single_links)
670 cpus->dai_name = NULL;
671 }
672 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu);
673
asoc_simple_clean_reference(struct snd_soc_card * card)674 void asoc_simple_clean_reference(struct snd_soc_card *card)
675 {
676 struct snd_soc_dai_link *dai_link;
677 struct snd_soc_dai_link_component *cpu;
678 struct snd_soc_dai_link_component *codec;
679 int i, j;
680
681 for_each_card_prelinks(card, i, dai_link) {
682 for_each_link_cpus(dai_link, j, cpu)
683 of_node_put(cpu->of_node);
684 for_each_link_codecs(dai_link, j, codec)
685 of_node_put(codec->of_node);
686 }
687 }
688 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference);
689
asoc_simple_parse_routing(struct snd_soc_card * card,char * prefix)690 int asoc_simple_parse_routing(struct snd_soc_card *card,
691 char *prefix)
692 {
693 struct device_node *node = card->dev->of_node;
694 char prop[128];
695
696 if (!prefix)
697 prefix = "";
698
699 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
700
701 if (!of_property_read_bool(node, prop))
702 return 0;
703
704 return snd_soc_of_parse_audio_routing(card, prop);
705 }
706 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing);
707
asoc_simple_parse_widgets(struct snd_soc_card * card,char * prefix)708 int asoc_simple_parse_widgets(struct snd_soc_card *card,
709 char *prefix)
710 {
711 struct device_node *node = card->dev->of_node;
712 char prop[128];
713
714 if (!prefix)
715 prefix = "";
716
717 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
718
719 if (of_property_read_bool(node, prop))
720 return snd_soc_of_parse_audio_simple_widgets(card, prop);
721
722 /* no widgets is not error */
723 return 0;
724 }
725 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets);
726
asoc_simple_parse_pin_switches(struct snd_soc_card * card,char * prefix)727 int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
728 char *prefix)
729 {
730 char prop[128];
731
732 if (!prefix)
733 prefix = "";
734
735 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
736
737 return snd_soc_of_parse_pin_switches(card, prop);
738 }
739 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches);
740
asoc_simple_init_jack(struct snd_soc_card * card,struct asoc_simple_jack * sjack,int is_hp,char * prefix,char * pin)741 int asoc_simple_init_jack(struct snd_soc_card *card,
742 struct asoc_simple_jack *sjack,
743 int is_hp, char *prefix,
744 char *pin)
745 {
746 struct device *dev = card->dev;
747 struct gpio_desc *desc;
748 char prop[128];
749 char *pin_name;
750 char *gpio_name;
751 int mask;
752 int error;
753
754 if (!prefix)
755 prefix = "";
756
757 sjack->gpio.gpio = -ENOENT;
758
759 if (is_hp) {
760 snprintf(prop, sizeof(prop), "%shp-det", prefix);
761 pin_name = pin ? pin : "Headphones";
762 gpio_name = "Headphone detection";
763 mask = SND_JACK_HEADPHONE;
764 } else {
765 snprintf(prop, sizeof(prop), "%smic-det", prefix);
766 pin_name = pin ? pin : "Mic Jack";
767 gpio_name = "Mic detection";
768 mask = SND_JACK_MICROPHONE;
769 }
770
771 desc = gpiod_get_optional(dev, prop, GPIOD_IN);
772 error = PTR_ERR_OR_ZERO(desc);
773 if (error)
774 return error;
775
776 if (desc) {
777 error = gpiod_set_consumer_name(desc, gpio_name);
778 if (error)
779 return error;
780
781 sjack->pin.pin = pin_name;
782 sjack->pin.mask = mask;
783
784 sjack->gpio.name = gpio_name;
785 sjack->gpio.report = mask;
786 sjack->gpio.desc = desc;
787 sjack->gpio.debounce_time = 150;
788
789 snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
790 &sjack->pin, 1);
791
792 snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
793 }
794
795 return 0;
796 }
797 EXPORT_SYMBOL_GPL(asoc_simple_init_jack);
798
asoc_simple_init_aux_jacks(struct asoc_simple_priv * priv,char * prefix)799 int asoc_simple_init_aux_jacks(struct asoc_simple_priv *priv, char *prefix)
800 {
801 struct snd_soc_card *card = simple_priv_to_card(priv);
802 struct snd_soc_component *component;
803 int found_jack_index = 0;
804 int type = 0;
805 int num = 0;
806 int ret;
807
808 if (priv->aux_jacks)
809 return 0;
810
811 for_each_card_auxs(card, component) {
812 type = snd_soc_component_get_jack_type(component);
813 if (type > 0)
814 num++;
815 }
816 if (num < 1)
817 return 0;
818
819 priv->aux_jacks = devm_kcalloc(card->dev, num,
820 sizeof(struct snd_soc_jack), GFP_KERNEL);
821 if (!priv->aux_jacks)
822 return -ENOMEM;
823
824 for_each_card_auxs(card, component) {
825 char id[128];
826 struct snd_soc_jack *jack;
827
828 if (found_jack_index >= num)
829 break;
830
831 type = snd_soc_component_get_jack_type(component);
832 if (type <= 0)
833 continue;
834
835 /* create jack */
836 jack = &(priv->aux_jacks[found_jack_index++]);
837 snprintf(id, sizeof(id), "%s-jack", component->name);
838 ret = snd_soc_card_jack_new(card, id, type, jack);
839 if (ret)
840 continue;
841
842 (void)snd_soc_component_set_jack(component, jack, NULL);
843 }
844 return 0;
845 }
846 EXPORT_SYMBOL_GPL(asoc_simple_init_aux_jacks);
847
asoc_simple_init_priv(struct asoc_simple_priv * priv,struct link_info * li)848 int asoc_simple_init_priv(struct asoc_simple_priv *priv,
849 struct link_info *li)
850 {
851 struct snd_soc_card *card = simple_priv_to_card(priv);
852 struct device *dev = simple_priv_to_dev(priv);
853 struct snd_soc_dai_link *dai_link;
854 struct simple_dai_props *dai_props;
855 struct asoc_simple_dai *dais;
856 struct snd_soc_dai_link_component *dlcs;
857 struct snd_soc_codec_conf *cconf = NULL;
858 int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
859
860 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
861 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL);
862 if (!dai_props || !dai_link)
863 return -ENOMEM;
864
865 /*
866 * dais (= CPU+Codec)
867 * dlcs (= CPU+Codec+Platform)
868 */
869 for (i = 0; i < li->link; i++) {
870 int cc = li->num[i].cpus + li->num[i].codecs;
871
872 dai_num += cc;
873 dlc_num += cc + li->num[i].platforms;
874
875 if (!li->num[i].cpus)
876 cnf_num += li->num[i].codecs;
877 }
878
879 dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
880 dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
881 if (!dais || !dlcs)
882 return -ENOMEM;
883
884 if (cnf_num) {
885 cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
886 if (!cconf)
887 return -ENOMEM;
888 }
889
890 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
891 li->link, dai_num, cnf_num);
892
893 priv->dai_props = dai_props;
894 priv->dai_link = dai_link;
895 priv->dais = dais;
896 priv->dlcs = dlcs;
897 priv->codec_conf = cconf;
898
899 card->dai_link = priv->dai_link;
900 card->num_links = li->link;
901 card->codec_conf = cconf;
902 card->num_configs = cnf_num;
903
904 for (i = 0; i < li->link; i++) {
905 if (li->num[i].cpus) {
906 /* Normal CPU */
907 dai_link[i].cpus = dlcs;
908 dai_props[i].num.cpus =
909 dai_link[i].num_cpus = li->num[i].cpus;
910 dai_props[i].cpu_dai = dais;
911
912 dlcs += li->num[i].cpus;
913 dais += li->num[i].cpus;
914 } else {
915 /* DPCM Be's CPU = dummy */
916 dai_link[i].cpus = &asoc_dummy_dlc;
917 dai_props[i].num.cpus =
918 dai_link[i].num_cpus = 1;
919 }
920
921 if (li->num[i].codecs) {
922 /* Normal Codec */
923 dai_link[i].codecs = dlcs;
924 dai_props[i].num.codecs =
925 dai_link[i].num_codecs = li->num[i].codecs;
926 dai_props[i].codec_dai = dais;
927
928 dlcs += li->num[i].codecs;
929 dais += li->num[i].codecs;
930
931 if (!li->num[i].cpus) {
932 /* DPCM Be's Codec */
933 dai_props[i].codec_conf = cconf;
934 cconf += li->num[i].codecs;
935 }
936 } else {
937 /* DPCM Fe's Codec = dummy */
938 dai_link[i].codecs = &asoc_dummy_dlc;
939 dai_props[i].num.codecs =
940 dai_link[i].num_codecs = 1;
941 }
942
943 if (li->num[i].platforms) {
944 /* Have Platform */
945 dai_link[i].platforms = dlcs;
946 dai_props[i].num.platforms =
947 dai_link[i].num_platforms = li->num[i].platforms;
948
949 dlcs += li->num[i].platforms;
950 } else {
951 /* Doesn't have Platform */
952 dai_link[i].platforms = NULL;
953 dai_props[i].num.platforms =
954 dai_link[i].num_platforms = 0;
955 }
956 }
957
958 return 0;
959 }
960 EXPORT_SYMBOL_GPL(asoc_simple_init_priv);
961
asoc_simple_remove(struct platform_device * pdev)962 int asoc_simple_remove(struct platform_device *pdev)
963 {
964 struct snd_soc_card *card = platform_get_drvdata(pdev);
965
966 asoc_simple_clean_reference(card);
967
968 return 0;
969 }
970 EXPORT_SYMBOL_GPL(asoc_simple_remove);
971
asoc_graph_card_probe(struct snd_soc_card * card)972 int asoc_graph_card_probe(struct snd_soc_card *card)
973 {
974 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
975 int ret;
976
977 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
978 if (ret < 0)
979 return ret;
980
981 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
982 if (ret < 0)
983 return ret;
984
985 return 0;
986 }
987 EXPORT_SYMBOL_GPL(asoc_graph_card_probe);
988
asoc_graph_is_ports0(struct device_node * np)989 int asoc_graph_is_ports0(struct device_node *np)
990 {
991 struct device_node *port, *ports, *ports0, *top;
992 int ret;
993
994 /* np is "endpoint" or "port" */
995 if (of_node_name_eq(np, "endpoint")) {
996 port = of_get_parent(np);
997 } else {
998 port = np;
999 of_node_get(port);
1000 }
1001
1002 ports = of_get_parent(port);
1003 top = of_get_parent(ports);
1004 ports0 = of_get_child_by_name(top, "ports");
1005
1006 ret = ports0 == ports;
1007
1008 of_node_put(port);
1009 of_node_put(ports);
1010 of_node_put(ports0);
1011 of_node_put(top);
1012
1013 return ret;
1014 }
1015 EXPORT_SYMBOL_GPL(asoc_graph_is_ports0);
1016
graph_get_dai_id(struct device_node * ep)1017 static int graph_get_dai_id(struct device_node *ep)
1018 {
1019 struct device_node *node;
1020 struct device_node *endpoint;
1021 struct of_endpoint info;
1022 int i, id;
1023 int ret;
1024
1025 /* use driver specified DAI ID if exist */
1026 ret = snd_soc_get_dai_id(ep);
1027 if (ret != -ENOTSUPP)
1028 return ret;
1029
1030 /* use endpoint/port reg if exist */
1031 ret = of_graph_parse_endpoint(ep, &info);
1032 if (ret == 0) {
1033 /*
1034 * Because it will count port/endpoint if it doesn't have "reg".
1035 * But, we can't judge whether it has "no reg", or "reg = <0>"
1036 * only of_graph_parse_endpoint().
1037 * We need to check "reg" property
1038 */
1039 if (of_property_present(ep, "reg"))
1040 return info.id;
1041
1042 node = of_get_parent(ep);
1043 ret = of_property_present(node, "reg");
1044 of_node_put(node);
1045 if (ret)
1046 return info.port;
1047 }
1048 node = of_graph_get_port_parent(ep);
1049
1050 /*
1051 * Non HDMI sound case, counting port/endpoint on its DT
1052 * is enough. Let's count it.
1053 */
1054 i = 0;
1055 id = -1;
1056 for_each_endpoint_of_node(node, endpoint) {
1057 if (endpoint == ep)
1058 id = i;
1059 i++;
1060 }
1061
1062 of_node_put(node);
1063
1064 if (id < 0)
1065 return -ENODEV;
1066
1067 return id;
1068 }
1069
asoc_graph_parse_dai(struct device * dev,struct device_node * ep,struct snd_soc_dai_link_component * dlc,int * is_single_link)1070 int asoc_graph_parse_dai(struct device *dev, struct device_node *ep,
1071 struct snd_soc_dai_link_component *dlc, int *is_single_link)
1072 {
1073 struct device_node *node;
1074 struct of_phandle_args args = {};
1075 struct snd_soc_dai *dai;
1076 int ret;
1077
1078 if (!ep)
1079 return 0;
1080
1081 node = of_graph_get_port_parent(ep);
1082
1083 /*
1084 * Try to find from DAI node
1085 */
1086 args.np = ep;
1087 dai = snd_soc_get_dai_via_args(&args);
1088 if (dai) {
1089 dlc->dai_name = snd_soc_dai_name_get(dai);
1090 dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
1091 if (!dlc->dai_args)
1092 return -ENOMEM;
1093
1094 goto parse_dai_end;
1095 }
1096
1097 /* Get dai->name */
1098 args.np = node;
1099 args.args[0] = graph_get_dai_id(ep);
1100 args.args_count = (of_graph_get_endpoint_count(node) > 1);
1101
1102 /*
1103 * FIXME
1104 *
1105 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
1106 * If user unbinded CPU or Codec driver, but not for Sound Card,
1107 * dlc->dai_name is keeping unbinded CPU or Codec
1108 * driver's pointer.
1109 *
1110 * If user re-bind CPU or Codec driver again, ALSA SoC will try
1111 * to rebind Card via snd_soc_try_rebind_card(), but because of
1112 * above reason, it might can't bind Sound Card.
1113 * Because Sound Card is pointing to released dai_name pointer.
1114 *
1115 * To avoid this rebind Card issue,
1116 * 1) It needs to alloc memory to keep dai_name eventhough
1117 * CPU or Codec driver was unbinded, or
1118 * 2) user need to rebind Sound Card everytime
1119 * if he unbinded CPU or Codec.
1120 */
1121 ret = snd_soc_get_dlc(&args, dlc);
1122 if (ret < 0) {
1123 of_node_put(node);
1124 return ret;
1125 }
1126
1127 parse_dai_end:
1128 if (is_single_link)
1129 *is_single_link = of_graph_get_endpoint_count(node) == 1;
1130
1131 return 0;
1132 }
1133 EXPORT_SYMBOL_GPL(asoc_graph_parse_dai);
1134
1135 /* Module information */
1136 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1137 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
1138 MODULE_LICENSE("GPL v2");
1139