blob: 5a3d51e45938ef43fcae19961f7eb48c47ba1014 [file] [log] [blame]
Kuninori Morimotoabd31472016-05-31 09:00:14 +00001/*
Kuninori Morimoto29a43aa2016-12-02 05:27:30 +00002 * simple-card-utils.c
Kuninori Morimotoabd31472016-05-31 09:00:14 +00003 *
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 */
Kuninori Morimotobb6fc622016-08-08 05:59:56 +000010#include <linux/clk.h>
Kuninori Morimoto1f85e112016-08-03 01:24:05 +000011#include <linux/module.h>
Kuninori Morimotoabd31472016-05-31 09:00:14 +000012#include <linux/of.h>
Kuninori Morimoto16893332017-04-20 01:35:18 +000013#include <linux/of_graph.h>
Kuninori Morimotoabd31472016-05-31 09:00:14 +000014#include <sound/simple_card_utils.h>
15
16int asoc_simple_card_parse_daifmt(struct device *dev,
17 struct device_node *node,
18 struct device_node *codec,
19 char *prefix,
20 unsigned int *retfmt)
21{
22 struct device_node *bitclkmaster = NULL;
23 struct device_node *framemaster = NULL;
24 int prefix_len = prefix ? strlen(prefix) : 0;
25 unsigned int daifmt;
26
27 daifmt = snd_soc_of_parse_daifmt(node, prefix,
28 &bitclkmaster, &framemaster);
29 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
30
31 if (prefix_len && !bitclkmaster && !framemaster) {
32 /*
33 * No dai-link level and master setting was not found from
34 * sound node level, revert back to legacy DT parsing and
35 * take the settings from codec node.
36 */
37 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
38
39 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
40 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
41 } else {
42 if (codec == bitclkmaster)
43 daifmt |= (codec == framemaster) ?
44 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
45 else
46 daifmt |= (codec == framemaster) ?
47 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
48 }
49
50 of_node_put(bitclkmaster);
51 of_node_put(framemaster);
52
53 *retfmt = daifmt;
54
55 return 0;
56}
57EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
Kuninori Morimoto1db33122016-07-11 23:57:14 +000058
59int asoc_simple_card_set_dailink_name(struct device *dev,
60 struct snd_soc_dai_link *dai_link,
61 const char *fmt, ...)
62{
63 va_list ap;
64 char *name = NULL;
65 int ret = -ENOMEM;
66
67 va_start(ap, fmt);
68 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
69 va_end(ap);
70
71 if (name) {
72 ret = 0;
73
74 dai_link->name = name;
75 dai_link->stream_name = name;
76 }
77
78 return ret;
79}
80EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000081
82int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
83 char *prefix)
84{
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000085 int ret;
86
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000087 if (!prefix)
88 prefix = "";
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000089
90 /* Parse the card name from DT */
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000091 ret = snd_soc_of_parse_card_name(card, "label");
92 if (ret < 0) {
93 char prop[128];
94
95 snprintf(prop, sizeof(prop), "%sname", prefix);
96 ret = snd_soc_of_parse_card_name(card, prop);
97 if (ret < 0)
98 return ret;
99 }
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +0000100
101 if (!card->name && card->dai_link)
102 card->name = card->dai_link->name;
103
104 return 0;
105}
106EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000107
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000108int asoc_simple_card_parse_clk(struct device *dev,
109 struct device_node *node,
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000110 struct device_node *dai_of_node,
111 struct asoc_simple_dai *simple_dai)
112{
113 struct clk *clk;
114 u32 val;
115
116 /*
117 * Parse dai->sysclk come from "clocks = <&xxx>"
118 * (if system has common clock)
119 * or "system-clock-frequency = <xxx>"
120 * or device's module clock.
121 */
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000122 clk = devm_get_clk_from_child(dev, node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000123 if (!IS_ERR(clk)) {
124 simple_dai->sysclk = clk_get_rate(clk);
Lucas Stach971edb02017-03-23 15:05:26 +0100125 simple_dai->clk = clk;
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000126 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
127 simple_dai->sysclk = val;
128 } else {
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000129 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000130 if (!IS_ERR(clk))
131 simple_dai->sysclk = clk_get_rate(clk);
132 }
133
134 return 0;
135}
136EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
137
Kuninori Morimotoae30a692016-08-08 06:01:43 +0000138int asoc_simple_card_parse_dai(struct device_node *node,
139 struct device_node **dai_of_node,
140 const char **dai_name,
141 const char *list_name,
142 const char *cells_name,
143 int *is_single_link)
144{
145 struct of_phandle_args args;
146 int ret;
147
148 if (!node)
149 return 0;
150
151 /*
152 * Get node via "sound-dai = <&phandle port>"
153 * it will be used as xxx_of_node on soc_bind_dai_link()
154 */
155 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
156 if (ret)
157 return ret;
158
159 /* Get dai->name */
160 if (dai_name) {
161 ret = snd_soc_of_get_dai_name(node, dai_name);
162 if (ret < 0)
163 return ret;
164 }
165
166 *dai_of_node = args.np;
167
168 if (is_single_link)
169 *is_single_link = !args.args_count;
170
171 return 0;
172}
173EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
174
Kuninori Morimoto16893332017-04-20 01:35:18 +0000175static int asoc_simple_card_get_dai_id(struct device_node *ep)
176{
177 struct device_node *node;
178 struct device_node *endpoint;
179 int i, id;
180
181 node = of_graph_get_port_parent(ep);
182
183 i = 0;
184 id = -1;
185 for_each_endpoint_of_node(node, endpoint) {
186 if (endpoint == ep)
187 id = i;
188 i++;
189 }
190 if (id < 0)
191 return -ENODEV;
192
193 return id;
194}
195
196int asoc_simple_card_parse_graph_dai(struct device_node *ep,
197 struct device_node **dai_of_node,
198 const char **dai_name)
199{
200 struct device_node *node;
201 struct of_phandle_args args;
202 int ret;
203
204 if (!ep)
205 return 0;
206 if (!dai_name)
207 return 0;
208
209 /*
210 * of_graph_get_port_parent() will call
211 * of_node_put(). So, call of_node_get() here
212 */
213 of_node_get(ep);
214 node = of_graph_get_port_parent(ep);
215
216 /* Get dai->name */
217 args.np = node;
218 args.args[0] = asoc_simple_card_get_dai_id(ep);
219 args.args_count = (of_graph_get_endpoint_count(node) > 1);
220
221 ret = snd_soc_get_dai_name(&args, dai_name);
222 if (ret < 0)
223 return ret;
224
225 *dai_of_node = node;
226
227 return 0;
228}
229EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
230
Kuninori Morimoto21ba62f2016-08-09 05:48:30 +0000231int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
232 struct asoc_simple_dai *simple_dai)
233{
234 int ret;
235
236 if (simple_dai->sysclk) {
237 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
238 if (ret && ret != -ENOTSUPP) {
239 dev_err(dai->dev, "simple-card: set_sysclk error\n");
240 return ret;
241 }
242 }
243
244 if (simple_dai->slots) {
245 ret = snd_soc_dai_set_tdm_slot(dai,
246 simple_dai->tx_slot_mask,
247 simple_dai->rx_slot_mask,
248 simple_dai->slots,
249 simple_dai->slot_width);
250 if (ret && ret != -ENOTSUPP) {
251 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
252 return ret;
253 }
254 }
255
256 return 0;
257}
258EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
259
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000260int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
261{
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000262 /* Assumes platform == cpu */
263 if (!dai_link->platform_of_node)
264 dai_link->platform_of_node = dai_link->cpu_of_node;
265
266 return 0;
267}
268EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
269
Kuninori Morimoto983cebd2016-08-10 02:20:19 +0000270void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
271 int is_single_links)
272{
273 /*
274 * In soc_bind_dai_link() will check cpu name after
275 * of_node matching if dai_link has cpu_dai_name.
276 * but, it will never match if name was created by
277 * fmt_single_name() remove cpu_dai_name if cpu_args
278 * was 0. See:
279 * fmt_single_name()
280 * fmt_multiple_name()
281 */
282 if (is_single_links)
283 dai_link->cpu_dai_name = NULL;
284}
285EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
286
Kuninori Morimoto0f4e0712016-08-10 02:21:25 +0000287int asoc_simple_card_clean_reference(struct snd_soc_card *card)
288{
289 struct snd_soc_dai_link *dai_link;
290 int num_links;
291
292 for (num_links = 0, dai_link = card->dai_link;
293 num_links < card->num_links;
294 num_links++, dai_link++) {
295 of_node_put(dai_link->cpu_of_node);
296 of_node_put(dai_link->codec_of_node);
297 }
298 return 0;
299}
300EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
301
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000302/* Module information */
303MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
304MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
305MODULE_LICENSE("GPL v2");