blob: fe726e83d0bd8144d5bfb8101a24d762ee65a1bf [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;
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000180 int ret;
181
182 ret = snd_soc_get_dai_id(ep);
183 if (ret != -ENOTSUPP)
184 return ret;
Kuninori Morimoto16893332017-04-20 01:35:18 +0000185
186 node = of_graph_get_port_parent(ep);
187
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000188 /*
189 * Non HDMI sound case, counting port/endpoint on its DT
190 * is enough. Let's count it.
191 */
Kuninori Morimoto16893332017-04-20 01:35:18 +0000192 i = 0;
193 id = -1;
194 for_each_endpoint_of_node(node, endpoint) {
195 if (endpoint == ep)
196 id = i;
197 i++;
198 }
199 if (id < 0)
200 return -ENODEV;
201
202 return id;
203}
204
205int asoc_simple_card_parse_graph_dai(struct device_node *ep,
206 struct device_node **dai_of_node,
207 const char **dai_name)
208{
209 struct device_node *node;
210 struct of_phandle_args args;
211 int ret;
212
213 if (!ep)
214 return 0;
215 if (!dai_name)
216 return 0;
217
218 /*
219 * of_graph_get_port_parent() will call
220 * of_node_put(). So, call of_node_get() here
221 */
222 of_node_get(ep);
223 node = of_graph_get_port_parent(ep);
224
225 /* Get dai->name */
226 args.np = node;
227 args.args[0] = asoc_simple_card_get_dai_id(ep);
228 args.args_count = (of_graph_get_endpoint_count(node) > 1);
229
230 ret = snd_soc_get_dai_name(&args, dai_name);
231 if (ret < 0)
232 return ret;
233
234 *dai_of_node = node;
235
236 return 0;
237}
238EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
239
Kuninori Morimoto21ba62f2016-08-09 05:48:30 +0000240int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
241 struct asoc_simple_dai *simple_dai)
242{
243 int ret;
244
245 if (simple_dai->sysclk) {
246 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
247 if (ret && ret != -ENOTSUPP) {
248 dev_err(dai->dev, "simple-card: set_sysclk error\n");
249 return ret;
250 }
251 }
252
253 if (simple_dai->slots) {
254 ret = snd_soc_dai_set_tdm_slot(dai,
255 simple_dai->tx_slot_mask,
256 simple_dai->rx_slot_mask,
257 simple_dai->slots,
258 simple_dai->slot_width);
259 if (ret && ret != -ENOTSUPP) {
260 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
261 return ret;
262 }
263 }
264
265 return 0;
266}
267EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
268
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000269int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
270{
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000271 /* Assumes platform == cpu */
272 if (!dai_link->platform_of_node)
273 dai_link->platform_of_node = dai_link->cpu_of_node;
274
275 return 0;
276}
277EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
278
Kuninori Morimoto983cebd2016-08-10 02:20:19 +0000279void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
280 int is_single_links)
281{
282 /*
283 * In soc_bind_dai_link() will check cpu name after
284 * of_node matching if dai_link has cpu_dai_name.
285 * but, it will never match if name was created by
286 * fmt_single_name() remove cpu_dai_name if cpu_args
287 * was 0. See:
288 * fmt_single_name()
289 * fmt_multiple_name()
290 */
291 if (is_single_links)
292 dai_link->cpu_dai_name = NULL;
293}
294EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
295
Kuninori Morimoto0f4e0712016-08-10 02:21:25 +0000296int asoc_simple_card_clean_reference(struct snd_soc_card *card)
297{
298 struct snd_soc_dai_link *dai_link;
299 int num_links;
300
301 for (num_links = 0, dai_link = card->dai_link;
302 num_links < card->num_links;
303 num_links++, dai_link++) {
304 of_node_put(dai_link->cpu_of_node);
305 of_node_put(dai_link->codec_of_node);
306 }
307 return 0;
308}
309EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
310
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000311/* Module information */
312MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
313MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
314MODULE_LICENSE("GPL v2");