blob: 9c7f5b91b90acb4fa6aaa7c7104f57dae4c20824 [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;
Kuninori Morimotoabd31472016-05-31 09:00:14 +000024 unsigned int daifmt;
25
26 daifmt = snd_soc_of_parse_daifmt(node, prefix,
27 &bitclkmaster, &framemaster);
28 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
29
Kuninori Morimoto155b8f32017-05-25 01:51:31 +000030 if (!bitclkmaster && !framemaster) {
Kuninori Morimotoabd31472016-05-31 09:00:14 +000031 /*
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);
Kuninori Morimoto1db33122016-07-11 23:57:14 +000057
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);
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000080
81int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
82 char *prefix)
83{
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000084 int ret;
85
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000086 if (!prefix)
87 prefix = "";
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000088
89 /* Parse the card name from DT */
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000090 ret = snd_soc_of_parse_card_name(card, "label");
91 if (ret < 0) {
92 char prop[128];
93
94 snprintf(prop, sizeof(prop), "%sname", prefix);
95 ret = snd_soc_of_parse_card_name(card, prop);
96 if (ret < 0)
97 return ret;
98 }
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000099
100 if (!card->name && card->dai_link)
101 card->name = card->dai_link->name;
102
103 return 0;
104}
105EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000106
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000107int asoc_simple_card_parse_clk(struct device *dev,
108 struct device_node *node,
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000109 struct device_node *dai_of_node,
110 struct asoc_simple_dai *simple_dai)
111{
112 struct clk *clk;
113 u32 val;
114
115 /*
116 * Parse dai->sysclk come from "clocks = <&xxx>"
117 * (if system has common clock)
118 * or "system-clock-frequency = <xxx>"
119 * or device's module clock.
120 */
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000121 clk = devm_get_clk_from_child(dev, node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000122 if (!IS_ERR(clk)) {
123 simple_dai->sysclk = clk_get_rate(clk);
Lucas Stach971edb02017-03-23 15:05:26 +0100124 simple_dai->clk = clk;
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000125 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
126 simple_dai->sysclk = val;
127 } else {
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000128 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000129 if (!IS_ERR(clk))
130 simple_dai->sysclk = clk_get_rate(clk);
131 }
132
133 return 0;
134}
135EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
136
Kuninori Morimotoae30a692016-08-08 06:01:43 +0000137int asoc_simple_card_parse_dai(struct device_node *node,
138 struct device_node **dai_of_node,
139 const char **dai_name,
140 const char *list_name,
141 const char *cells_name,
142 int *is_single_link)
143{
144 struct of_phandle_args args;
145 int ret;
146
147 if (!node)
148 return 0;
149
150 /*
151 * Get node via "sound-dai = <&phandle port>"
152 * it will be used as xxx_of_node on soc_bind_dai_link()
153 */
154 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
155 if (ret)
156 return ret;
157
158 /* Get dai->name */
159 if (dai_name) {
160 ret = snd_soc_of_get_dai_name(node, dai_name);
161 if (ret < 0)
162 return ret;
163 }
164
165 *dai_of_node = args.np;
166
167 if (is_single_link)
168 *is_single_link = !args.args_count;
169
170 return 0;
171}
172EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
173
Kuninori Morimoto16893332017-04-20 01:35:18 +0000174static int asoc_simple_card_get_dai_id(struct device_node *ep)
175{
176 struct device_node *node;
177 struct device_node *endpoint;
178 int i, id;
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000179 int ret;
180
181 ret = snd_soc_get_dai_id(ep);
182 if (ret != -ENOTSUPP)
183 return ret;
Kuninori Morimoto16893332017-04-20 01:35:18 +0000184
185 node = of_graph_get_port_parent(ep);
186
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000187 /*
188 * Non HDMI sound case, counting port/endpoint on its DT
189 * is enough. Let's count it.
190 */
Kuninori Morimoto16893332017-04-20 01:35:18 +0000191 i = 0;
192 id = -1;
193 for_each_endpoint_of_node(node, endpoint) {
194 if (endpoint == ep)
195 id = i;
196 i++;
197 }
198 if (id < 0)
199 return -ENODEV;
200
201 return id;
202}
203
204int asoc_simple_card_parse_graph_dai(struct device_node *ep,
205 struct device_node **dai_of_node,
206 const char **dai_name)
207{
208 struct device_node *node;
209 struct of_phandle_args args;
210 int ret;
211
212 if (!ep)
213 return 0;
214 if (!dai_name)
215 return 0;
216
217 /*
218 * of_graph_get_port_parent() will call
219 * of_node_put(). So, call of_node_get() here
220 */
221 of_node_get(ep);
222 node = of_graph_get_port_parent(ep);
223
224 /* Get dai->name */
225 args.np = node;
226 args.args[0] = asoc_simple_card_get_dai_id(ep);
227 args.args_count = (of_graph_get_endpoint_count(node) > 1);
228
229 ret = snd_soc_get_dai_name(&args, dai_name);
230 if (ret < 0)
231 return ret;
232
233 *dai_of_node = node;
234
235 return 0;
236}
237EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
238
Kuninori Morimoto21ba62f2016-08-09 05:48:30 +0000239int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
240 struct asoc_simple_dai *simple_dai)
241{
242 int ret;
243
244 if (simple_dai->sysclk) {
245 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
246 if (ret && ret != -ENOTSUPP) {
247 dev_err(dai->dev, "simple-card: set_sysclk error\n");
248 return ret;
249 }
250 }
251
252 if (simple_dai->slots) {
253 ret = snd_soc_dai_set_tdm_slot(dai,
254 simple_dai->tx_slot_mask,
255 simple_dai->rx_slot_mask,
256 simple_dai->slots,
257 simple_dai->slot_width);
258 if (ret && ret != -ENOTSUPP) {
259 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
260 return ret;
261 }
262 }
263
264 return 0;
265}
266EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
267
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000268int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
269{
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000270 /* Assumes platform == cpu */
271 if (!dai_link->platform_of_node)
272 dai_link->platform_of_node = dai_link->cpu_of_node;
273
274 return 0;
275}
276EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
277
Kuninori Morimoto983cebd2016-08-10 02:20:19 +0000278void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
279 int is_single_links)
280{
281 /*
282 * In soc_bind_dai_link() will check cpu name after
283 * of_node matching if dai_link has cpu_dai_name.
284 * but, it will never match if name was created by
285 * fmt_single_name() remove cpu_dai_name if cpu_args
286 * was 0. See:
287 * fmt_single_name()
288 * fmt_multiple_name()
289 */
290 if (is_single_links)
291 dai_link->cpu_dai_name = NULL;
292}
293EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
294
Kuninori Morimoto0f4e0712016-08-10 02:21:25 +0000295int asoc_simple_card_clean_reference(struct snd_soc_card *card)
296{
297 struct snd_soc_dai_link *dai_link;
298 int num_links;
299
300 for (num_links = 0, dai_link = card->dai_link;
301 num_links < card->num_links;
302 num_links++, dai_link++) {
303 of_node_put(dai_link->cpu_of_node);
304 of_node_put(dai_link->codec_of_node);
305 }
306 return 0;
307}
308EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
309
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000310/* Module information */
311MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
312MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
313MODULE_LICENSE("GPL v2");