1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.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>
17 static void asoc_simple_fixup_sample_fmt(struct asoc_simple_data *data,
18 struct snd_pcm_hw_params *params)
21 struct snd_mask *mask = hw_param_mask(params,
22 SNDRV_PCM_HW_PARAM_FORMAT);
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},
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)) {
38 snd_mask_set(mask, of_sample_fmt_table[i].val);
44 void asoc_simple_parse_convert(struct device_node *np,
46 struct asoc_simple_data *data)
53 /* sampling rate convert */
54 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
55 of_property_read_u32(np, prop, &data->convert_rate);
57 /* channels transfer */
58 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
59 of_property_read_u32(np, prop, &data->convert_channels);
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);
65 EXPORT_SYMBOL_GPL(asoc_simple_parse_convert);
68 * asoc_simple_is_convert_required() - Query if HW param conversion was requested
71 * Returns true if any HW param conversion was requested for this DAI link with
72 * any "convert-xxx" properties.
74 bool asoc_simple_is_convert_required(const struct asoc_simple_data *data)
76 return data->convert_rate ||
77 data->convert_channels ||
78 data->convert_sample_format;
80 EXPORT_SYMBOL_GPL(asoc_simple_is_convert_required);
82 int asoc_simple_parse_daifmt(struct device *dev,
83 struct device_node *node,
84 struct device_node *codec,
88 struct device_node *bitclkmaster = NULL;
89 struct device_node *framemaster = NULL;
92 daifmt = snd_soc_daifmt_parse_format(node, prefix);
94 snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
95 if (!bitclkmaster && !framemaster) {
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.
101 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
103 daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
105 daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
106 ((codec == bitclkmaster) << 4) | (codec == framemaster));
109 of_node_put(bitclkmaster);
110 of_node_put(framemaster);
116 EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt);
118 int asoc_simple_parse_tdm_width_map(struct device *dev, struct device_node *np,
119 struct asoc_simple_dai *dai)
121 u32 *array_values, *p;
124 if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
127 n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
129 dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
133 dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
134 if (!dai->tdm_width_map)
137 array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
141 ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
143 dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
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++;
154 dai->n_tdm_widths = i;
161 EXPORT_SYMBOL_GPL(asoc_simple_parse_tdm_width_map);
163 int asoc_simple_set_dailink_name(struct device *dev,
164 struct snd_soc_dai_link *dai_link,
165 const char *fmt, ...)
172 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
178 dai_link->name = name;
179 dai_link->stream_name = name;
184 EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name);
186 int asoc_simple_parse_card_name(struct snd_soc_card *card,
194 /* Parse the card name from DT */
195 ret = snd_soc_of_parse_card_name(card, "label");
196 if (ret < 0 || !card->name) {
199 snprintf(prop, sizeof(prop), "%sname", prefix);
200 ret = snd_soc_of_parse_card_name(card, prop);
205 if (!card->name && card->dai_link)
206 card->name = card->dai_link->name;
210 EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name);
212 static int asoc_simple_clk_enable(struct asoc_simple_dai *dai)
215 return clk_prepare_enable(dai->clk);
220 static void asoc_simple_clk_disable(struct asoc_simple_dai *dai)
223 clk_disable_unprepare(dai->clk);
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)
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.
240 clk = devm_get_clk_from_child(dev, node, NULL);
241 simple_dai->clk_fixed = of_property_read_bool(
242 node, "system-clock-fixed");
244 simple_dai->sysclk = clk_get_rate(clk);
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;
251 clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
253 simple_dai->sysclk = clk_get_rate(clk);
256 if (of_property_read_bool(node, "system-clock-direction-out"))
257 simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
261 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk);
263 static int asoc_simple_check_fixed_sysclk(struct device *dev,
264 struct asoc_simple_dai *dai,
265 unsigned int *fixed_sysclk)
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);
273 *fixed_sysclk = dai->sysclk;
279 int asoc_simple_startup(struct snd_pcm_substream *substream)
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;
289 for_each_prop_dai_cpu(props, i1, dai) {
290 ret = asoc_simple_clk_enable(dai);
293 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
298 for_each_prop_dai_codec(props, i2, dai) {
299 ret = asoc_simple_clk_enable(dai);
302 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
307 if (fixed_sysclk && props->mclk_fs) {
308 unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
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);
315 ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
316 fixed_rate, fixed_rate);
324 for_each_prop_dai_codec(props, i, dai) {
327 asoc_simple_clk_disable(dai);
330 for_each_prop_dai_cpu(props, i, dai) {
333 asoc_simple_clk_disable(dai);
337 EXPORT_SYMBOL_GPL(asoc_simple_startup);
339 void asoc_simple_shutdown(struct snd_pcm_substream *substream)
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;
347 for_each_prop_dai_cpu(props, i, dai) {
348 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, i);
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);
354 asoc_simple_clk_disable(dai);
356 for_each_prop_dai_codec(props, i, dai) {
357 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, i);
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);
363 asoc_simple_clk_disable(dai);
366 EXPORT_SYMBOL_GPL(asoc_simple_shutdown);
368 static int asoc_simple_set_clk_rate(struct device *dev,
369 struct asoc_simple_dai *simple_dai,
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);
380 if (!simple_dai->clk)
383 if (clk_get_rate(simple_dai->clk) == rate)
386 return clk_set_rate(simple_dai->clk, rate);
389 static int asoc_simple_set_tdm(struct snd_soc_dai *dai,
390 struct asoc_simple_dai *simple_dai,
391 struct snd_pcm_hw_params *params)
393 int sample_bits = params_width(params);
394 int slot_width, slot_count;
397 if (!simple_dai || !simple_dai->tdm_width_map)
400 slot_width = simple_dai->slot_width;
401 slot_count = simple_dai->slots;
404 slot_width = sample_bits;
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;
414 ret = snd_soc_dai_set_tdm_slot(dai,
415 simple_dai->tx_slot_mask,
416 simple_dai->rx_slot_mask,
419 if (ret && ret != -ENOTSUPP) {
420 dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
427 int asoc_simple_hw_params(struct snd_pcm_substream *substream,
428 struct snd_pcm_hw_params *params)
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;
439 mclk_fs = props->mclk_fs;
442 struct snd_soc_component *component;
443 mclk = params_rate(params) * mclk_fs;
445 for_each_prop_dai_codec(props, i, pdai) {
446 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
451 for_each_prop_dai_cpu(props, i, pdai) {
452 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
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.
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)
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)
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)
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);
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);
497 EXPORT_SYMBOL_GPL(asoc_simple_hw_params);
499 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
500 struct snd_pcm_hw_params *params)
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);
508 if (data->convert_rate)
510 rate->max = data->convert_rate;
512 if (data->convert_channels)
514 channels->max = data->convert_channels;
516 if (data->convert_sample_format)
517 asoc_simple_fixup_sample_fmt(data, params);
521 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup);
523 static int asoc_simple_init_dai(struct snd_soc_dai *dai,
524 struct asoc_simple_dai *simple_dai)
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");
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,
545 simple_dai->slot_width);
546 if (ret && ret != -ENOTSUPP) {
547 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
555 static inline int asoc_simple_component_is_codec(struct snd_soc_component *component)
557 return component->driver->endianness;
560 static int asoc_simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
561 struct simple_dai_props *dai_props)
563 struct snd_soc_dai_link *dai_link = rtd->dai_link;
564 struct snd_soc_component *component;
565 struct snd_soc_pcm_stream *c2c_params;
566 struct snd_pcm_hardware hw;
569 /* Do nothing if it already has Codec2Codec settings */
570 if (dai_link->c2c_params)
573 /* Do nothing if it was DPCM :: BE */
574 if (dai_link->no_pcm)
578 for_each_rtd_components(rtd, i, component) {
579 if (!asoc_simple_component_is_codec(component))
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);
591 dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
595 c2c_params = devm_kzalloc(rtd->dev, sizeof(*c2c_params), GFP_KERNEL);
599 c2c_params->formats = hw.formats;
600 c2c_params->rates = hw.rates;
601 c2c_params->rate_min = hw.rate_min;
602 c2c_params->rate_max = hw.rate_max;
603 c2c_params->channels_min = hw.channels_min;
604 c2c_params->channels_max = hw.channels_max;
606 dai_link->c2c_params = c2c_params;
607 dai_link->num_c2c_params = 1;
612 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd)
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;
619 for_each_prop_dai_codec(props, i, dai) {
620 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, i), dai);
624 for_each_prop_dai_cpu(props, i, dai) {
625 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, i), dai);
630 ret = asoc_simple_init_for_codec2codec(rtd, props);
636 EXPORT_SYMBOL_GPL(asoc_simple_dai_init);
638 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
639 struct snd_soc_dai_link_component *cpus)
642 * Assumes Platform == CPU
644 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
645 * are different Component, but are sharing same component->dev.
647 * Let's assume Platform is same as CPU if it doesn't identify Platform on DT.
649 * simple-card.c :: simple_count_noml()
651 if (!platforms->of_node)
652 snd_soc_dlc_use_cpu_as_platform(platforms, cpus);
654 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform);
656 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
660 * In soc_bind_dai_link() will check cpu name after
661 * of_node matching if dai_link has cpu_dai_name.
662 * but, it will never match if name was created by
663 * fmt_single_name() remove cpu_dai_name if cpu_args
666 * fmt_multiple_name()
669 cpus->dai_name = NULL;
671 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu);
673 void asoc_simple_clean_reference(struct snd_soc_card *card)
675 struct snd_soc_dai_link *dai_link;
676 struct snd_soc_dai_link_component *cpu;
677 struct snd_soc_dai_link_component *codec;
680 for_each_card_prelinks(card, i, dai_link) {
681 for_each_link_cpus(dai_link, j, cpu)
682 of_node_put(cpu->of_node);
683 for_each_link_codecs(dai_link, j, codec)
684 of_node_put(codec->of_node);
687 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference);
689 int asoc_simple_parse_routing(struct snd_soc_card *card,
692 struct device_node *node = card->dev->of_node;
698 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
700 if (!of_property_read_bool(node, prop))
703 return snd_soc_of_parse_audio_routing(card, prop);
705 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing);
707 int asoc_simple_parse_widgets(struct snd_soc_card *card,
710 struct device_node *node = card->dev->of_node;
716 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
718 if (of_property_read_bool(node, prop))
719 return snd_soc_of_parse_audio_simple_widgets(card, prop);
721 /* no widgets is not error */
724 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets);
726 int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
734 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
736 return snd_soc_of_parse_pin_switches(card, prop);
738 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches);
740 int asoc_simple_init_jack(struct snd_soc_card *card,
741 struct asoc_simple_jack *sjack,
742 int is_hp, char *prefix,
745 struct device *dev = card->dev;
746 struct gpio_desc *desc;
756 sjack->gpio.gpio = -ENOENT;
759 snprintf(prop, sizeof(prop), "%shp-det", prefix);
760 pin_name = pin ? pin : "Headphones";
761 gpio_name = "Headphone detection";
762 mask = SND_JACK_HEADPHONE;
764 snprintf(prop, sizeof(prop), "%smic-det", prefix);
765 pin_name = pin ? pin : "Mic Jack";
766 gpio_name = "Mic detection";
767 mask = SND_JACK_MICROPHONE;
770 desc = gpiod_get_optional(dev, prop, GPIOD_IN);
771 error = PTR_ERR_OR_ZERO(desc);
776 error = gpiod_set_consumer_name(desc, gpio_name);
780 sjack->pin.pin = pin_name;
781 sjack->pin.mask = mask;
783 sjack->gpio.name = gpio_name;
784 sjack->gpio.report = mask;
785 sjack->gpio.desc = desc;
786 sjack->gpio.debounce_time = 150;
788 snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
791 snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
796 EXPORT_SYMBOL_GPL(asoc_simple_init_jack);
798 int asoc_simple_init_aux_jacks(struct asoc_simple_priv *priv, char *prefix)
800 struct snd_soc_card *card = simple_priv_to_card(priv);
801 struct snd_soc_component *component;
802 int found_jack_index = 0;
810 for_each_card_auxs(card, component) {
811 type = snd_soc_component_get_jack_type(component);
818 priv->aux_jacks = devm_kcalloc(card->dev, num,
819 sizeof(struct snd_soc_jack), GFP_KERNEL);
820 if (!priv->aux_jacks)
823 for_each_card_auxs(card, component) {
825 struct snd_soc_jack *jack;
827 if (found_jack_index >= num)
830 type = snd_soc_component_get_jack_type(component);
835 jack = &(priv->aux_jacks[found_jack_index++]);
836 snprintf(id, sizeof(id), "%s-jack", component->name);
837 ret = snd_soc_card_jack_new(card, id, type, jack);
841 (void)snd_soc_component_set_jack(component, jack, NULL);
845 EXPORT_SYMBOL_GPL(asoc_simple_init_aux_jacks);
847 int asoc_simple_init_priv(struct asoc_simple_priv *priv,
848 struct link_info *li)
850 struct snd_soc_card *card = simple_priv_to_card(priv);
851 struct device *dev = simple_priv_to_dev(priv);
852 struct snd_soc_dai_link *dai_link;
853 struct simple_dai_props *dai_props;
854 struct asoc_simple_dai *dais;
855 struct snd_soc_dai_link_component *dlcs;
856 struct snd_soc_codec_conf *cconf = NULL;
857 int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
859 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
860 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL);
861 if (!dai_props || !dai_link)
866 * dlcs (= CPU+Codec+Platform)
868 for (i = 0; i < li->link; i++) {
869 int cc = li->num[i].cpus + li->num[i].codecs;
872 dlc_num += cc + li->num[i].platforms;
874 if (!li->num[i].cpus)
875 cnf_num += li->num[i].codecs;
878 dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
879 dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
884 cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
889 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
890 li->link, dai_num, cnf_num);
892 priv->dai_props = dai_props;
893 priv->dai_link = dai_link;
896 priv->codec_conf = cconf;
898 card->dai_link = priv->dai_link;
899 card->num_links = li->link;
900 card->codec_conf = cconf;
901 card->num_configs = cnf_num;
903 for (i = 0; i < li->link; i++) {
904 if (li->num[i].cpus) {
906 dai_link[i].cpus = dlcs;
907 dai_props[i].num.cpus =
908 dai_link[i].num_cpus = li->num[i].cpus;
909 dai_props[i].cpu_dai = dais;
911 dlcs += li->num[i].cpus;
912 dais += li->num[i].cpus;
914 /* DPCM Be's CPU = dummy */
915 dai_link[i].cpus = &asoc_dummy_dlc;
916 dai_props[i].num.cpus =
917 dai_link[i].num_cpus = 1;
920 if (li->num[i].codecs) {
922 dai_link[i].codecs = dlcs;
923 dai_props[i].num.codecs =
924 dai_link[i].num_codecs = li->num[i].codecs;
925 dai_props[i].codec_dai = dais;
927 dlcs += li->num[i].codecs;
928 dais += li->num[i].codecs;
930 if (!li->num[i].cpus) {
931 /* DPCM Be's Codec */
932 dai_props[i].codec_conf = cconf;
933 cconf += li->num[i].codecs;
936 /* DPCM Fe's Codec = dummy */
937 dai_link[i].codecs = &asoc_dummy_dlc;
938 dai_props[i].num.codecs =
939 dai_link[i].num_codecs = 1;
942 if (li->num[i].platforms) {
944 dai_link[i].platforms = dlcs;
945 dai_props[i].num.platforms =
946 dai_link[i].num_platforms = li->num[i].platforms;
948 dlcs += li->num[i].platforms;
950 /* Doesn't have Platform */
951 dai_link[i].platforms = NULL;
952 dai_props[i].num.platforms =
953 dai_link[i].num_platforms = 0;
959 EXPORT_SYMBOL_GPL(asoc_simple_init_priv);
961 int asoc_simple_remove(struct platform_device *pdev)
963 struct snd_soc_card *card = platform_get_drvdata(pdev);
965 asoc_simple_clean_reference(card);
969 EXPORT_SYMBOL_GPL(asoc_simple_remove);
971 int asoc_graph_card_probe(struct snd_soc_card *card)
973 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
976 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
980 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
986 EXPORT_SYMBOL_GPL(asoc_graph_card_probe);
988 int asoc_graph_is_ports0(struct device_node *np)
990 struct device_node *port, *ports, *ports0, *top;
993 /* np is "endpoint" or "port" */
994 if (of_node_name_eq(np, "endpoint")) {
995 port = of_get_parent(np);
1001 ports = of_get_parent(port);
1002 top = of_get_parent(ports);
1003 ports0 = of_get_child_by_name(top, "ports");
1005 ret = ports0 == ports;
1009 of_node_put(ports0);
1014 EXPORT_SYMBOL_GPL(asoc_graph_is_ports0);
1016 static int graph_get_dai_id(struct device_node *ep)
1018 struct device_node *node;
1019 struct device_node *endpoint;
1020 struct of_endpoint info;
1024 /* use driver specified DAI ID if exist */
1025 ret = snd_soc_get_dai_id(ep);
1026 if (ret != -ENOTSUPP)
1029 /* use endpoint/port reg if exist */
1030 ret = of_graph_parse_endpoint(ep, &info);
1033 * Because it will count port/endpoint if it doesn't have "reg".
1034 * But, we can't judge whether it has "no reg", or "reg = <0>"
1035 * only of_graph_parse_endpoint().
1036 * We need to check "reg" property
1038 if (of_property_present(ep, "reg"))
1041 node = of_get_parent(ep);
1042 ret = of_property_present(node, "reg");
1047 node = of_graph_get_port_parent(ep);
1050 * Non HDMI sound case, counting port/endpoint on its DT
1051 * is enough. Let's count it.
1055 for_each_endpoint_of_node(node, endpoint) {
1069 int asoc_graph_parse_dai(struct device *dev, struct device_node *ep,
1070 struct snd_soc_dai_link_component *dlc, int *is_single_link)
1072 struct device_node *node;
1073 struct of_phandle_args args = {};
1074 struct snd_soc_dai *dai;
1080 node = of_graph_get_port_parent(ep);
1083 * Try to find from DAI node
1086 dai = snd_soc_get_dai_via_args(&args);
1088 dlc->dai_name = snd_soc_dai_name_get(dai);
1089 dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
1098 args.args[0] = graph_get_dai_id(ep);
1099 args.args_count = (of_graph_get_endpoint_count(node) > 1);
1104 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
1105 * If user unbinded CPU or Codec driver, but not for Sound Card,
1106 * dlc->dai_name is keeping unbinded CPU or Codec
1109 * If user re-bind CPU or Codec driver again, ALSA SoC will try
1110 * to rebind Card via snd_soc_try_rebind_card(), but because of
1111 * above reason, it might can't bind Sound Card.
1112 * Because Sound Card is pointing to released dai_name pointer.
1114 * To avoid this rebind Card issue,
1115 * 1) It needs to alloc memory to keep dai_name eventhough
1116 * CPU or Codec driver was unbinded, or
1117 * 2) user need to rebind Sound Card everytime
1118 * if he unbinded CPU or Codec.
1120 ret = snd_soc_get_dlc(&args, dlc);
1128 *is_single_link = of_graph_get_endpoint_count(node) == 1;
1132 EXPORT_SYMBOL_GPL(asoc_graph_parse_dai);
1134 /* Module information */
1135 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1136 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
1137 MODULE_LICENSE("GPL v2");