]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/soc/generic/simple-card-utils.c
Merge remote-tracking branches 'asoc/topic/rockchip', 'asoc/topic/rt5514', 'asoc...
[mirror_ubuntu-bionic-kernel.git] / sound / soc / generic / simple-card-utils.c
CommitLineData
abd3147e 1/*
29a43aa9 2 * simple-card-utils.c
abd3147e
KM
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 */
bb6fc620 10#include <linux/clk.h>
1f85e118 11#include <linux/module.h>
abd3147e
KM
12#include <linux/of.h>
13#include <sound/simple_card_utils.h>
14
15int asoc_simple_card_parse_daifmt(struct device *dev,
16 struct device_node *node,
17 struct device_node *codec,
18 char *prefix,
19 unsigned int *retfmt)
20{
21 struct device_node *bitclkmaster = NULL;
22 struct device_node *framemaster = NULL;
23 int prefix_len = prefix ? strlen(prefix) : 0;
24 unsigned int daifmt;
25
26 daifmt = snd_soc_of_parse_daifmt(node, prefix,
27 &bitclkmaster, &framemaster);
28 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
29
30 if (prefix_len && !bitclkmaster && !framemaster) {
31 /*
32 * No dai-link level and master setting was not found from
33 * sound node level, revert back to legacy DT parsing and
34 * take the settings from codec node.
35 */
36 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
37
38 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
39 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
40 } else {
41 if (codec == bitclkmaster)
42 daifmt |= (codec == framemaster) ?
43 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
44 else
45 daifmt |= (codec == framemaster) ?
46 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
47 }
48
49 of_node_put(bitclkmaster);
50 of_node_put(framemaster);
51
52 *retfmt = daifmt;
53
54 return 0;
55}
56EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
1db3312e
KM
57
58int asoc_simple_card_set_dailink_name(struct device *dev,
59 struct snd_soc_dai_link *dai_link,
60 const char *fmt, ...)
61{
62 va_list ap;
63 char *name = NULL;
64 int ret = -ENOMEM;
65
66 va_start(ap, fmt);
67 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
68 va_end(ap);
69
70 if (name) {
71 ret = 0;
72
73 dai_link->name = name;
74 dai_link->stream_name = name;
75 }
76
77 return ret;
78}
79EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
fc55c9b5
KM
80
81int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
82 char *prefix)
83{
84 char prop[128];
85 int ret;
86
87 snprintf(prop, sizeof(prop), "%sname", prefix);
88
89 /* Parse the card name from DT */
90 ret = snd_soc_of_parse_card_name(card, prop);
91 if (ret < 0)
92 return ret;
93
94 if (!card->name && card->dai_link)
95 card->name = card->dai_link->name;
96
97 return 0;
98}
99EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
1f85e118 100
e984fd61
KM
101int asoc_simple_card_parse_clk(struct device *dev,
102 struct device_node *node,
bb6fc620
KM
103 struct device_node *dai_of_node,
104 struct asoc_simple_dai *simple_dai)
105{
106 struct clk *clk;
107 u32 val;
108
109 /*
110 * Parse dai->sysclk come from "clocks = <&xxx>"
111 * (if system has common clock)
112 * or "system-clock-frequency = <xxx>"
113 * or device's module clock.
114 */
e984fd61 115 clk = devm_get_clk_from_child(dev, node, NULL);
bb6fc620
KM
116 if (!IS_ERR(clk)) {
117 simple_dai->sysclk = clk_get_rate(clk);
971edb0a 118 simple_dai->clk = clk;
bb6fc620
KM
119 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
120 simple_dai->sysclk = val;
121 } else {
e984fd61 122 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
bb6fc620
KM
123 if (!IS_ERR(clk))
124 simple_dai->sysclk = clk_get_rate(clk);
125 }
126
127 return 0;
128}
129EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
130
ae30a694
KM
131int asoc_simple_card_parse_dai(struct device_node *node,
132 struct device_node **dai_of_node,
133 const char **dai_name,
134 const char *list_name,
135 const char *cells_name,
136 int *is_single_link)
137{
138 struct of_phandle_args args;
139 int ret;
140
141 if (!node)
142 return 0;
143
144 /*
145 * Get node via "sound-dai = <&phandle port>"
146 * it will be used as xxx_of_node on soc_bind_dai_link()
147 */
148 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
149 if (ret)
150 return ret;
151
152 /* Get dai->name */
153 if (dai_name) {
154 ret = snd_soc_of_get_dai_name(node, dai_name);
155 if (ret < 0)
156 return ret;
157 }
158
159 *dai_of_node = args.np;
160
161 if (is_single_link)
162 *is_single_link = !args.args_count;
163
164 return 0;
165}
166EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
167
21ba62f8
KM
168int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
169 struct asoc_simple_dai *simple_dai)
170{
171 int ret;
172
173 if (simple_dai->sysclk) {
174 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
175 if (ret && ret != -ENOTSUPP) {
176 dev_err(dai->dev, "simple-card: set_sysclk error\n");
177 return ret;
178 }
179 }
180
181 if (simple_dai->slots) {
182 ret = snd_soc_dai_set_tdm_slot(dai,
183 simple_dai->tx_slot_mask,
184 simple_dai->rx_slot_mask,
185 simple_dai->slots,
186 simple_dai->slot_width);
187 if (ret && ret != -ENOTSUPP) {
188 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
189 return ret;
190 }
191 }
192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
196
c262c9ab
KM
197int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
198{
c262c9ab
KM
199 /* Assumes platform == cpu */
200 if (!dai_link->platform_of_node)
201 dai_link->platform_of_node = dai_link->cpu_of_node;
202
203 return 0;
204}
205EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
206
983cebd6
KM
207void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
208 int is_single_links)
209{
210 /*
211 * In soc_bind_dai_link() will check cpu name after
212 * of_node matching if dai_link has cpu_dai_name.
213 * but, it will never match if name was created by
214 * fmt_single_name() remove cpu_dai_name if cpu_args
215 * was 0. See:
216 * fmt_single_name()
217 * fmt_multiple_name()
218 */
219 if (is_single_links)
220 dai_link->cpu_dai_name = NULL;
221}
222EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
223
0f4e0711
KM
224int asoc_simple_card_clean_reference(struct snd_soc_card *card)
225{
226 struct snd_soc_dai_link *dai_link;
227 int num_links;
228
229 for (num_links = 0, dai_link = card->dai_link;
230 num_links < card->num_links;
231 num_links++, dai_link++) {
232 of_node_put(dai_link->cpu_of_node);
233 of_node_put(dai_link->codec_of_node);
234 }
235 return 0;
236}
237EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
238
1f85e118
KM
239/* Module information */
240MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
241MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
242MODULE_LICENSE("GPL v2");