Loading...
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
17static 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
44void 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}
65EXPORT_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 */
74bool 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}
80EXPORT_SYMBOL_GPL(asoc_simple_is_convert_required);
81
82int 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}
116EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt);
117
118int 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;
156out:
157 kfree(array_values);
158
159 return ret;
160}
161EXPORT_SYMBOL_GPL(asoc_simple_parse_tdm_width_map);
162
163int 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}
184EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name);
185
186int 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}
210EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name);
211
212static 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
220static void asoc_simple_clk_disable(struct asoc_simple_dai *dai)
221{
222 if (dai)
223 clk_disable_unprepare(dai->clk);
224}
225
226int 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}
261EXPORT_SYMBOL_GPL(asoc_simple_parse_clk);
262
263static 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
279int 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 return -EINVAL;
314 }
315 ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
316 fixed_rate, fixed_rate);
317 if (ret)
318 goto codec_err;
319 }
320
321 return 0;
322
323codec_err:
324 for_each_prop_dai_codec(props, i, dai) {
325 if (i >= i2)
326 break;
327 asoc_simple_clk_disable(dai);
328 }
329cpu_err:
330 for_each_prop_dai_cpu(props, i, dai) {
331 if (i >= i1)
332 break;
333 asoc_simple_clk_disable(dai);
334 }
335 return ret;
336}
337EXPORT_SYMBOL_GPL(asoc_simple_startup);
338
339void asoc_simple_shutdown(struct snd_pcm_substream *substream)
340{
341 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
342 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
343 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
344 struct asoc_simple_dai *dai;
345 int i;
346
347 for_each_prop_dai_cpu(props, i, dai) {
348 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, i);
349
350 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
351 snd_soc_dai_set_sysclk(cpu_dai,
352 0, 0, SND_SOC_CLOCK_OUT);
353
354 asoc_simple_clk_disable(dai);
355 }
356 for_each_prop_dai_codec(props, i, dai) {
357 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, i);
358
359 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
360 snd_soc_dai_set_sysclk(codec_dai,
361 0, 0, SND_SOC_CLOCK_IN);
362
363 asoc_simple_clk_disable(dai);
364 }
365}
366EXPORT_SYMBOL_GPL(asoc_simple_shutdown);
367
368static int asoc_simple_set_clk_rate(struct device *dev,
369 struct asoc_simple_dai *simple_dai,
370 unsigned long rate)
371{
372 if (!simple_dai)
373 return 0;
374
375 if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
376 dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
377 return -EINVAL;
378 }
379
380 if (!simple_dai->clk)
381 return 0;
382
383 if (clk_get_rate(simple_dai->clk) == rate)
384 return 0;
385
386 return clk_set_rate(simple_dai->clk, rate);
387}
388
389static int asoc_simple_set_tdm(struct snd_soc_dai *dai,
390 struct asoc_simple_dai *simple_dai,
391 struct snd_pcm_hw_params *params)
392{
393 int sample_bits = params_width(params);
394 int slot_width, slot_count;
395 int i, ret;
396
397 if (!simple_dai || !simple_dai->tdm_width_map)
398 return 0;
399
400 slot_width = simple_dai->slot_width;
401 slot_count = simple_dai->slots;
402
403 if (slot_width == 0)
404 slot_width = sample_bits;
405
406 for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
407 if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
408 slot_width = simple_dai->tdm_width_map[i].slot_width;
409 slot_count = simple_dai->tdm_width_map[i].slot_count;
410 break;
411 }
412 }
413
414 ret = snd_soc_dai_set_tdm_slot(dai,
415 simple_dai->tx_slot_mask,
416 simple_dai->rx_slot_mask,
417 slot_count,
418 slot_width);
419 if (ret && ret != -ENOTSUPP) {
420 dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
421 return ret;
422 }
423
424 return 0;
425}
426
427int asoc_simple_hw_params(struct snd_pcm_substream *substream,
428 struct snd_pcm_hw_params *params)
429{
430 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
431 struct asoc_simple_dai *pdai;
432 struct snd_soc_dai *sdai;
433 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
434 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
435 unsigned int mclk, mclk_fs = 0;
436 int i, ret;
437
438 if (props->mclk_fs)
439 mclk_fs = props->mclk_fs;
440
441 if (mclk_fs) {
442 struct snd_soc_component *component;
443 mclk = params_rate(params) * mclk_fs;
444
445 for_each_prop_dai_codec(props, i, pdai) {
446 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
447 if (ret < 0)
448 return ret;
449 }
450
451 for_each_prop_dai_cpu(props, i, pdai) {
452 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
453 if (ret < 0)
454 return ret;
455 }
456
457 /* Ensure sysclk is set on all components in case any
458 * (such as platform components) are missed by calls to
459 * snd_soc_dai_set_sysclk.
460 */
461 for_each_rtd_components(rtd, i, component) {
462 ret = snd_soc_component_set_sysclk(component, 0, 0,
463 mclk, SND_SOC_CLOCK_IN);
464 if (ret && ret != -ENOTSUPP)
465 return ret;
466 }
467
468 for_each_rtd_codec_dais(rtd, i, sdai) {
469 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
470 if (ret && ret != -ENOTSUPP)
471 return ret;
472 }
473
474 for_each_rtd_cpu_dais(rtd, i, sdai) {
475 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
476 if (ret && ret != -ENOTSUPP)
477 return ret;
478 }
479 }
480
481 for_each_prop_dai_codec(props, i, pdai) {
482 sdai = asoc_rtd_to_codec(rtd, i);
483 ret = asoc_simple_set_tdm(sdai, pdai, params);
484 if (ret < 0)
485 return ret;
486 }
487
488 for_each_prop_dai_cpu(props, i, pdai) {
489 sdai = asoc_rtd_to_cpu(rtd, i);
490 ret = asoc_simple_set_tdm(sdai, pdai, params);
491 if (ret < 0)
492 return ret;
493 }
494
495 return 0;
496}
497EXPORT_SYMBOL_GPL(asoc_simple_hw_params);
498
499int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
500 struct snd_pcm_hw_params *params)
501{
502 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
503 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
504 struct asoc_simple_data *data = &dai_props->adata;
505 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
506 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
507
508 if (data->convert_rate)
509 rate->min =
510 rate->max = data->convert_rate;
511
512 if (data->convert_channels)
513 channels->min =
514 channels->max = data->convert_channels;
515
516 if (data->convert_sample_format)
517 asoc_simple_fixup_sample_fmt(data, params);
518
519 return 0;
520}
521EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup);
522
523static int asoc_simple_init_dai(struct snd_soc_dai *dai,
524 struct asoc_simple_dai *simple_dai)
525{
526 int ret;
527
528 if (!simple_dai)
529 return 0;
530
531 if (simple_dai->sysclk) {
532 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
533 simple_dai->clk_direction);
534 if (ret && ret != -ENOTSUPP) {
535 dev_err(dai->dev, "simple-card: set_sysclk error\n");
536 return ret;
537 }
538 }
539
540 if (simple_dai->slots) {
541 ret = snd_soc_dai_set_tdm_slot(dai,
542 simple_dai->tx_slot_mask,
543 simple_dai->rx_slot_mask,
544 simple_dai->slots,
545 simple_dai->slot_width);
546 if (ret && ret != -ENOTSUPP) {
547 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
548 return ret;
549 }
550 }
551
552 return 0;
553}
554
555static inline int asoc_simple_component_is_codec(struct snd_soc_component *component)
556{
557 return component->driver->endianness;
558}
559
560static int asoc_simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
561 struct simple_dai_props *dai_props)
562{
563 struct snd_soc_dai_link *dai_link = rtd->dai_link;
564 struct snd_soc_component *component;
565 struct snd_soc_pcm_stream *params;
566 struct snd_pcm_hardware hw;
567 int i, ret, stream;
568
569 /* Do nothing if it already has Codec2Codec settings */
570 if (dai_link->params)
571 return 0;
572
573 /* Do nothing if it was DPCM :: BE */
574 if (dai_link->no_pcm)
575 return 0;
576
577 /* Only Codecs */
578 for_each_rtd_components(rtd, i, component) {
579 if (!asoc_simple_component_is_codec(component))
580 return 0;
581 }
582
583 /* Assumes the capabilities are the same for all supported streams */
584 for_each_pcm_streams(stream) {
585 ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
586 if (ret == 0)
587 break;
588 }
589
590 if (ret < 0) {
591 dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
592 return ret;
593 }
594
595 params = devm_kzalloc(rtd->dev, sizeof(*params), GFP_KERNEL);
596 if (!params)
597 return -ENOMEM;
598
599 params->formats = hw.formats;
600 params->rates = hw.rates;
601 params->rate_min = hw.rate_min;
602 params->rate_max = hw.rate_max;
603 params->channels_min = hw.channels_min;
604 params->channels_max = hw.channels_max;
605
606 dai_link->params = params;
607 dai_link->num_params = 1;
608
609 return 0;
610}
611
612int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd)
613{
614 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
615 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
616 struct asoc_simple_dai *dai;
617 int i, ret;
618
619 for_each_prop_dai_codec(props, i, dai) {
620 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, i), dai);
621 if (ret < 0)
622 return ret;
623 }
624 for_each_prop_dai_cpu(props, i, dai) {
625 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, i), dai);
626 if (ret < 0)
627 return ret;
628 }
629
630 ret = asoc_simple_init_for_codec2codec(rtd, props);
631 if (ret < 0)
632 return ret;
633
634 return 0;
635}
636EXPORT_SYMBOL_GPL(asoc_simple_dai_init);
637
638void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
639 struct snd_soc_dai_link_component *cpus)
640{
641 /* Assumes platform == cpu */
642 if (!platforms->of_node)
643 platforms->of_node = cpus->of_node;
644}
645EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform);
646
647void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
648 int is_single_links)
649{
650 /*
651 * In soc_bind_dai_link() will check cpu name after
652 * of_node matching if dai_link has cpu_dai_name.
653 * but, it will never match if name was created by
654 * fmt_single_name() remove cpu_dai_name if cpu_args
655 * was 0. See:
656 * fmt_single_name()
657 * fmt_multiple_name()
658 */
659 if (is_single_links)
660 cpus->dai_name = NULL;
661}
662EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu);
663
664void asoc_simple_clean_reference(struct snd_soc_card *card)
665{
666 struct snd_soc_dai_link *dai_link;
667 struct snd_soc_dai_link_component *cpu;
668 struct snd_soc_dai_link_component *codec;
669 int i, j;
670
671 for_each_card_prelinks(card, i, dai_link) {
672 for_each_link_cpus(dai_link, j, cpu)
673 of_node_put(cpu->of_node);
674 for_each_link_codecs(dai_link, j, codec)
675 of_node_put(codec->of_node);
676 }
677}
678EXPORT_SYMBOL_GPL(asoc_simple_clean_reference);
679
680int asoc_simple_parse_routing(struct snd_soc_card *card,
681 char *prefix)
682{
683 struct device_node *node = card->dev->of_node;
684 char prop[128];
685
686 if (!prefix)
687 prefix = "";
688
689 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
690
691 if (!of_property_read_bool(node, prop))
692 return 0;
693
694 return snd_soc_of_parse_audio_routing(card, prop);
695}
696EXPORT_SYMBOL_GPL(asoc_simple_parse_routing);
697
698int asoc_simple_parse_widgets(struct snd_soc_card *card,
699 char *prefix)
700{
701 struct device_node *node = card->dev->of_node;
702 char prop[128];
703
704 if (!prefix)
705 prefix = "";
706
707 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
708
709 if (of_property_read_bool(node, prop))
710 return snd_soc_of_parse_audio_simple_widgets(card, prop);
711
712 /* no widgets is not error */
713 return 0;
714}
715EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets);
716
717int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
718 char *prefix)
719{
720 char prop[128];
721
722 if (!prefix)
723 prefix = "";
724
725 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
726
727 return snd_soc_of_parse_pin_switches(card, prop);
728}
729EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches);
730
731int asoc_simple_init_jack(struct snd_soc_card *card,
732 struct asoc_simple_jack *sjack,
733 int is_hp, char *prefix,
734 char *pin)
735{
736 struct device *dev = card->dev;
737 struct gpio_desc *desc;
738 char prop[128];
739 char *pin_name;
740 char *gpio_name;
741 int mask;
742 int error;
743
744 if (!prefix)
745 prefix = "";
746
747 sjack->gpio.gpio = -ENOENT;
748
749 if (is_hp) {
750 snprintf(prop, sizeof(prop), "%shp-det", prefix);
751 pin_name = pin ? pin : "Headphones";
752 gpio_name = "Headphone detection";
753 mask = SND_JACK_HEADPHONE;
754 } else {
755 snprintf(prop, sizeof(prop), "%smic-det", prefix);
756 pin_name = pin ? pin : "Mic Jack";
757 gpio_name = "Mic detection";
758 mask = SND_JACK_MICROPHONE;
759 }
760
761 desc = gpiod_get_optional(dev, prop, GPIOD_IN);
762 error = PTR_ERR_OR_ZERO(desc);
763 if (error)
764 return error;
765
766 if (desc) {
767 error = gpiod_set_consumer_name(desc, gpio_name);
768 if (error)
769 return error;
770
771 sjack->pin.pin = pin_name;
772 sjack->pin.mask = mask;
773
774 sjack->gpio.name = gpio_name;
775 sjack->gpio.report = mask;
776 sjack->gpio.desc = desc;
777 sjack->gpio.debounce_time = 150;
778
779 snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
780 &sjack->pin, 1);
781
782 snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
783 }
784
785 return 0;
786}
787EXPORT_SYMBOL_GPL(asoc_simple_init_jack);
788
789int asoc_simple_init_priv(struct asoc_simple_priv *priv,
790 struct link_info *li)
791{
792 struct snd_soc_card *card = simple_priv_to_card(priv);
793 struct device *dev = simple_priv_to_dev(priv);
794 struct snd_soc_dai_link *dai_link;
795 struct simple_dai_props *dai_props;
796 struct asoc_simple_dai *dais;
797 struct snd_soc_dai_link_component *dlcs;
798 struct snd_soc_codec_conf *cconf = NULL;
799 int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
800
801 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
802 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL);
803 if (!dai_props || !dai_link)
804 return -ENOMEM;
805
806 /*
807 * dais (= CPU+Codec)
808 * dlcs (= CPU+Codec+Platform)
809 */
810 for (i = 0; i < li->link; i++) {
811 int cc = li->num[i].cpus + li->num[i].codecs;
812
813 dai_num += cc;
814 dlc_num += cc + li->num[i].platforms;
815
816 if (!li->num[i].cpus)
817 cnf_num += li->num[i].codecs;
818 }
819
820 dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
821 dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
822 if (!dais || !dlcs)
823 return -ENOMEM;
824
825 if (cnf_num) {
826 cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
827 if (!cconf)
828 return -ENOMEM;
829 }
830
831 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
832 li->link, dai_num, cnf_num);
833
834 /* dummy CPU/Codec */
835 priv->dummy.of_node = NULL;
836 priv->dummy.dai_name = "snd-soc-dummy-dai";
837 priv->dummy.name = "snd-soc-dummy";
838
839 priv->dai_props = dai_props;
840 priv->dai_link = dai_link;
841 priv->dais = dais;
842 priv->dlcs = dlcs;
843 priv->codec_conf = cconf;
844
845 card->dai_link = priv->dai_link;
846 card->num_links = li->link;
847 card->codec_conf = cconf;
848 card->num_configs = cnf_num;
849
850 for (i = 0; i < li->link; i++) {
851 if (li->num[i].cpus) {
852 /* Normal CPU */
853 dai_props[i].cpus =
854 dai_link[i].cpus = dlcs;
855 dai_props[i].num.cpus =
856 dai_link[i].num_cpus = li->num[i].cpus;
857 dai_props[i].cpu_dai = dais;
858
859 dlcs += li->num[i].cpus;
860 dais += li->num[i].cpus;
861 } else {
862 /* DPCM Be's CPU = dummy */
863 dai_props[i].cpus =
864 dai_link[i].cpus = &priv->dummy;
865 dai_props[i].num.cpus =
866 dai_link[i].num_cpus = 1;
867 }
868
869 if (li->num[i].codecs) {
870 /* Normal Codec */
871 dai_props[i].codecs =
872 dai_link[i].codecs = dlcs;
873 dai_props[i].num.codecs =
874 dai_link[i].num_codecs = li->num[i].codecs;
875 dai_props[i].codec_dai = dais;
876
877 dlcs += li->num[i].codecs;
878 dais += li->num[i].codecs;
879
880 if (!li->num[i].cpus) {
881 /* DPCM Be's Codec */
882 dai_props[i].codec_conf = cconf;
883 cconf += li->num[i].codecs;
884 }
885 } else {
886 /* DPCM Fe's Codec = dummy */
887 dai_props[i].codecs =
888 dai_link[i].codecs = &priv->dummy;
889 dai_props[i].num.codecs =
890 dai_link[i].num_codecs = 1;
891 }
892
893 if (li->num[i].platforms) {
894 /* Have Platform */
895 dai_props[i].platforms =
896 dai_link[i].platforms = dlcs;
897 dai_props[i].num.platforms =
898 dai_link[i].num_platforms = li->num[i].platforms;
899
900 dlcs += li->num[i].platforms;
901 } else {
902 /* Doesn't have Platform */
903 dai_props[i].platforms =
904 dai_link[i].platforms = NULL;
905 dai_props[i].num.platforms =
906 dai_link[i].num_platforms = 0;
907 }
908 }
909
910 return 0;
911}
912EXPORT_SYMBOL_GPL(asoc_simple_init_priv);
913
914int asoc_simple_remove(struct platform_device *pdev)
915{
916 struct snd_soc_card *card = platform_get_drvdata(pdev);
917
918 asoc_simple_clean_reference(card);
919
920 return 0;
921}
922EXPORT_SYMBOL_GPL(asoc_simple_remove);
923
924int asoc_graph_card_probe(struct snd_soc_card *card)
925{
926 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
927 int ret;
928
929 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
930 if (ret < 0)
931 return ret;
932
933 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
934 if (ret < 0)
935 return ret;
936
937 return 0;
938}
939EXPORT_SYMBOL_GPL(asoc_graph_card_probe);
940
941int asoc_graph_is_ports0(struct device_node *np)
942{
943 struct device_node *port, *ports, *ports0, *top;
944 int ret;
945
946 /* np is "endpoint" or "port" */
947 if (of_node_name_eq(np, "endpoint")) {
948 port = of_get_parent(np);
949 } else {
950 port = np;
951 of_node_get(port);
952 }
953
954 ports = of_get_parent(port);
955 top = of_get_parent(ports);
956 ports0 = of_get_child_by_name(top, "ports");
957
958 ret = ports0 == ports;
959
960 of_node_put(port);
961 of_node_put(ports);
962 of_node_put(ports0);
963 of_node_put(top);
964
965 return ret;
966}
967EXPORT_SYMBOL_GPL(asoc_graph_is_ports0);
968
969/* Module information */
970MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
971MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
972MODULE_LICENSE("GPL v2");
1/*
2 * simple-card-utils.c
3 *
4 * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/clk.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/of_graph.h>
14#include <sound/simple_card_utils.h>
15
16void asoc_simple_card_convert_fixup(struct asoc_simple_card_data *data,
17 struct snd_pcm_hw_params *params)
18{
19 struct snd_interval *rate = hw_param_interval(params,
20 SNDRV_PCM_HW_PARAM_RATE);
21 struct snd_interval *channels = hw_param_interval(params,
22 SNDRV_PCM_HW_PARAM_CHANNELS);
23
24 if (data->convert_rate)
25 rate->min =
26 rate->max = data->convert_rate;
27
28 if (data->convert_channels)
29 channels->min =
30 channels->max = data->convert_channels;
31}
32EXPORT_SYMBOL_GPL(asoc_simple_card_convert_fixup);
33
34void asoc_simple_card_parse_convert(struct device *dev, char *prefix,
35 struct asoc_simple_card_data *data)
36{
37 struct device_node *np = dev->of_node;
38 char prop[128];
39
40 if (!prefix)
41 prefix = "";
42
43 /* sampling rate convert */
44 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
45 of_property_read_u32(np, prop, &data->convert_rate);
46
47 /* channels transfer */
48 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
49 of_property_read_u32(np, prop, &data->convert_channels);
50
51 dev_dbg(dev, "convert_rate %d\n", data->convert_rate);
52 dev_dbg(dev, "convert_channels %d\n", data->convert_channels);
53}
54EXPORT_SYMBOL_GPL(asoc_simple_card_parse_convert);
55
56int asoc_simple_card_parse_daifmt(struct device *dev,
57 struct device_node *node,
58 struct device_node *codec,
59 char *prefix,
60 unsigned int *retfmt)
61{
62 struct device_node *bitclkmaster = NULL;
63 struct device_node *framemaster = NULL;
64 unsigned int daifmt;
65
66 daifmt = snd_soc_of_parse_daifmt(node, prefix,
67 &bitclkmaster, &framemaster);
68 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
69
70 if (!bitclkmaster && !framemaster) {
71 /*
72 * No dai-link level and master setting was not found from
73 * sound node level, revert back to legacy DT parsing and
74 * take the settings from codec node.
75 */
76 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
77
78 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
79 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
80 } else {
81 if (codec == bitclkmaster)
82 daifmt |= (codec == framemaster) ?
83 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
84 else
85 daifmt |= (codec == framemaster) ?
86 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
87 }
88
89 of_node_put(bitclkmaster);
90 of_node_put(framemaster);
91
92 *retfmt = daifmt;
93
94 dev_dbg(dev, "format : %04x\n", daifmt);
95
96 return 0;
97}
98EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
99
100int asoc_simple_card_set_dailink_name(struct device *dev,
101 struct snd_soc_dai_link *dai_link,
102 const char *fmt, ...)
103{
104 va_list ap;
105 char *name = NULL;
106 int ret = -ENOMEM;
107
108 va_start(ap, fmt);
109 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
110 va_end(ap);
111
112 if (name) {
113 ret = 0;
114
115 dai_link->name = name;
116 dai_link->stream_name = name;
117
118 dev_dbg(dev, "name : %s\n", name);
119 }
120
121 return ret;
122}
123EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
124
125int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
126 char *prefix)
127{
128 int ret;
129
130 if (!prefix)
131 prefix = "";
132
133 /* Parse the card name from DT */
134 ret = snd_soc_of_parse_card_name(card, "label");
135 if (ret < 0 || !card->name) {
136 char prop[128];
137
138 snprintf(prop, sizeof(prop), "%sname", prefix);
139 ret = snd_soc_of_parse_card_name(card, prop);
140 if (ret < 0)
141 return ret;
142 }
143
144 if (!card->name && card->dai_link)
145 card->name = card->dai_link->name;
146
147 dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : "");
148
149 return 0;
150}
151EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
152
153static void asoc_simple_card_clk_register(struct asoc_simple_dai *dai,
154 struct clk *clk)
155{
156 dai->clk = clk;
157}
158
159int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai)
160{
161 return clk_prepare_enable(dai->clk);
162}
163EXPORT_SYMBOL_GPL(asoc_simple_card_clk_enable);
164
165void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai)
166{
167 clk_disable_unprepare(dai->clk);
168}
169EXPORT_SYMBOL_GPL(asoc_simple_card_clk_disable);
170
171int asoc_simple_card_parse_clk(struct device *dev,
172 struct device_node *node,
173 struct device_node *dai_of_node,
174 struct asoc_simple_dai *simple_dai,
175 const char *name)
176{
177 struct clk *clk;
178 u32 val;
179
180 /*
181 * Parse dai->sysclk come from "clocks = <&xxx>"
182 * (if system has common clock)
183 * or "system-clock-frequency = <xxx>"
184 * or device's module clock.
185 */
186 clk = devm_get_clk_from_child(dev, node, NULL);
187 if (!IS_ERR(clk)) {
188 simple_dai->sysclk = clk_get_rate(clk);
189
190 asoc_simple_card_clk_register(simple_dai, clk);
191 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
192 simple_dai->sysclk = val;
193 } else {
194 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
195 if (!IS_ERR(clk))
196 simple_dai->sysclk = clk_get_rate(clk);
197 }
198
199 if (of_property_read_bool(node, "system-clock-direction-out"))
200 simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
201
202 dev_dbg(dev, "%s : sysclk = %d, direction %d\n", name,
203 simple_dai->sysclk, simple_dai->clk_direction);
204
205 return 0;
206}
207EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
208
209int asoc_simple_card_parse_dai(struct device_node *node,
210 struct device_node **dai_of_node,
211 const char **dai_name,
212 const char *list_name,
213 const char *cells_name,
214 int *is_single_link)
215{
216 struct of_phandle_args args;
217 int ret;
218
219 if (!node)
220 return 0;
221
222 /*
223 * Get node via "sound-dai = <&phandle port>"
224 * it will be used as xxx_of_node on soc_bind_dai_link()
225 */
226 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
227 if (ret)
228 return ret;
229
230 /* Get dai->name */
231 if (dai_name) {
232 ret = snd_soc_of_get_dai_name(node, dai_name);
233 if (ret < 0)
234 return ret;
235 }
236
237 *dai_of_node = args.np;
238
239 if (is_single_link)
240 *is_single_link = !args.args_count;
241
242 return 0;
243}
244EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
245
246static int asoc_simple_card_get_dai_id(struct device_node *ep)
247{
248 struct device_node *node;
249 struct device_node *endpoint;
250 int i, id;
251 int ret;
252
253 ret = snd_soc_get_dai_id(ep);
254 if (ret != -ENOTSUPP)
255 return ret;
256
257 node = of_graph_get_port_parent(ep);
258
259 /*
260 * Non HDMI sound case, counting port/endpoint on its DT
261 * is enough. Let's count it.
262 */
263 i = 0;
264 id = -1;
265 for_each_endpoint_of_node(node, endpoint) {
266 if (endpoint == ep)
267 id = i;
268 i++;
269 }
270
271 of_node_put(node);
272
273 if (id < 0)
274 return -ENODEV;
275
276 return id;
277}
278
279int asoc_simple_card_parse_graph_dai(struct device_node *ep,
280 struct device_node **dai_of_node,
281 const char **dai_name)
282{
283 struct device_node *node;
284 struct of_phandle_args args;
285 int ret;
286
287 if (!ep)
288 return 0;
289 if (!dai_name)
290 return 0;
291
292 node = of_graph_get_port_parent(ep);
293
294 /* Get dai->name */
295 args.np = node;
296 args.args[0] = asoc_simple_card_get_dai_id(ep);
297 args.args_count = (of_graph_get_endpoint_count(node) > 1);
298
299 ret = snd_soc_get_dai_name(&args, dai_name);
300 if (ret < 0)
301 return ret;
302
303 *dai_of_node = node;
304
305 return 0;
306}
307EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
308
309int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
310 struct asoc_simple_dai *simple_dai)
311{
312 int ret;
313
314 if (simple_dai->sysclk) {
315 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
316 simple_dai->clk_direction);
317 if (ret && ret != -ENOTSUPP) {
318 dev_err(dai->dev, "simple-card: set_sysclk error\n");
319 return ret;
320 }
321 }
322
323 if (simple_dai->slots) {
324 ret = snd_soc_dai_set_tdm_slot(dai,
325 simple_dai->tx_slot_mask,
326 simple_dai->rx_slot_mask,
327 simple_dai->slots,
328 simple_dai->slot_width);
329 if (ret && ret != -ENOTSUPP) {
330 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
331 return ret;
332 }
333 }
334
335 return 0;
336}
337EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
338
339int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
340{
341 /* Assumes platform == cpu */
342 if (!dai_link->platform_of_node)
343 dai_link->platform_of_node = dai_link->cpu_of_node;
344
345 return 0;
346}
347EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
348
349void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
350 int is_single_links)
351{
352 /*
353 * In soc_bind_dai_link() will check cpu name after
354 * of_node matching if dai_link has cpu_dai_name.
355 * but, it will never match if name was created by
356 * fmt_single_name() remove cpu_dai_name if cpu_args
357 * was 0. See:
358 * fmt_single_name()
359 * fmt_multiple_name()
360 */
361 if (is_single_links)
362 dai_link->cpu_dai_name = NULL;
363}
364EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
365
366int asoc_simple_card_clean_reference(struct snd_soc_card *card)
367{
368 struct snd_soc_dai_link *dai_link;
369 int num_links;
370
371 for (num_links = 0, dai_link = card->dai_link;
372 num_links < card->num_links;
373 num_links++, dai_link++) {
374 of_node_put(dai_link->cpu_of_node);
375 of_node_put(dai_link->codec_of_node);
376 }
377 return 0;
378}
379EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
380
381int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
382 char *prefix,
383 int optional)
384{
385 struct device_node *node = card->dev->of_node;
386 char prop[128];
387
388 if (!prefix)
389 prefix = "";
390
391 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
392
393 if (!of_property_read_bool(node, prop)) {
394 if (optional)
395 return 0;
396 return -EINVAL;
397 }
398
399 return snd_soc_of_parse_audio_routing(card, prop);
400}
401EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);
402
403int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
404 char *prefix)
405{
406 struct device_node *node = card->dev->of_node;
407 char prop[128];
408
409 if (!prefix)
410 prefix = "";
411
412 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
413
414 if (of_property_read_bool(node, prop))
415 return snd_soc_of_parse_audio_simple_widgets(card, prop);
416
417 /* no widgets is not error */
418 return 0;
419}
420EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);
421
422/* Module information */
423MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
424MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
425MODULE_LICENSE("GPL v2");