blob: da24ac171aec6783896e21233c8789b4b3c80e6a [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
Kuninori Morimotod4dbcb62017-06-05 04:27:56 +0000103 dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : "");
104
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +0000105 return 0;
106}
107EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000108
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000109int asoc_simple_card_parse_clk(struct device *dev,
110 struct device_node *node,
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000111 struct device_node *dai_of_node,
112 struct asoc_simple_dai *simple_dai)
113{
114 struct clk *clk;
115 u32 val;
116
117 /*
118 * Parse dai->sysclk come from "clocks = <&xxx>"
119 * (if system has common clock)
120 * or "system-clock-frequency = <xxx>"
121 * or device's module clock.
122 */
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000123 clk = devm_get_clk_from_child(dev, node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000124 if (!IS_ERR(clk)) {
125 simple_dai->sysclk = clk_get_rate(clk);
Lucas Stach971edb02017-03-23 15:05:26 +0100126 simple_dai->clk = clk;
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000127 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
128 simple_dai->sysclk = val;
129 } else {
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000130 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000131 if (!IS_ERR(clk))
132 simple_dai->sysclk = clk_get_rate(clk);
133 }
134
135 return 0;
136}
137EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
138
Kuninori Morimotoae30a692016-08-08 06:01:43 +0000139int asoc_simple_card_parse_dai(struct device_node *node,
140 struct device_node **dai_of_node,
141 const char **dai_name,
142 const char *list_name,
143 const char *cells_name,
144 int *is_single_link)
145{
146 struct of_phandle_args args;
147 int ret;
148
149 if (!node)
150 return 0;
151
152 /*
153 * Get node via "sound-dai = <&phandle port>"
154 * it will be used as xxx_of_node on soc_bind_dai_link()
155 */
156 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
157 if (ret)
158 return ret;
159
160 /* Get dai->name */
161 if (dai_name) {
162 ret = snd_soc_of_get_dai_name(node, dai_name);
163 if (ret < 0)
164 return ret;
165 }
166
167 *dai_of_node = args.np;
168
169 if (is_single_link)
170 *is_single_link = !args.args_count;
171
172 return 0;
173}
174EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
175
Kuninori Morimoto16893332017-04-20 01:35:18 +0000176static int asoc_simple_card_get_dai_id(struct device_node *ep)
177{
178 struct device_node *node;
179 struct device_node *endpoint;
180 int i, id;
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000181 int ret;
182
183 ret = snd_soc_get_dai_id(ep);
184 if (ret != -ENOTSUPP)
185 return ret;
Kuninori Morimoto16893332017-04-20 01:35:18 +0000186
187 node = of_graph_get_port_parent(ep);
188
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000189 /*
190 * Non HDMI sound case, counting port/endpoint on its DT
191 * is enough. Let's count it.
192 */
Kuninori Morimoto16893332017-04-20 01:35:18 +0000193 i = 0;
194 id = -1;
195 for_each_endpoint_of_node(node, endpoint) {
196 if (endpoint == ep)
197 id = i;
198 i++;
199 }
200 if (id < 0)
201 return -ENODEV;
202
203 return id;
204}
205
206int asoc_simple_card_parse_graph_dai(struct device_node *ep,
207 struct device_node **dai_of_node,
208 const char **dai_name)
209{
210 struct device_node *node;
211 struct of_phandle_args args;
212 int ret;
213
214 if (!ep)
215 return 0;
216 if (!dai_name)
217 return 0;
218
219 /*
220 * of_graph_get_port_parent() will call
221 * of_node_put(). So, call of_node_get() here
222 */
223 of_node_get(ep);
224 node = of_graph_get_port_parent(ep);
225
226 /* Get dai->name */
227 args.np = node;
228 args.args[0] = asoc_simple_card_get_dai_id(ep);
229 args.args_count = (of_graph_get_endpoint_count(node) > 1);
230
231 ret = snd_soc_get_dai_name(&args, dai_name);
232 if (ret < 0)
233 return ret;
234
235 *dai_of_node = node;
236
237 return 0;
238}
239EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
240
Kuninori Morimoto21ba62f2016-08-09 05:48:30 +0000241int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
242 struct asoc_simple_dai *simple_dai)
243{
244 int ret;
245
246 if (simple_dai->sysclk) {
247 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
248 if (ret && ret != -ENOTSUPP) {
249 dev_err(dai->dev, "simple-card: set_sysclk error\n");
250 return ret;
251 }
252 }
253
254 if (simple_dai->slots) {
255 ret = snd_soc_dai_set_tdm_slot(dai,
256 simple_dai->tx_slot_mask,
257 simple_dai->rx_slot_mask,
258 simple_dai->slots,
259 simple_dai->slot_width);
260 if (ret && ret != -ENOTSUPP) {
261 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
262 return ret;
263 }
264 }
265
266 return 0;
267}
268EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
269
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000270int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
271{
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000272 /* Assumes platform == cpu */
273 if (!dai_link->platform_of_node)
274 dai_link->platform_of_node = dai_link->cpu_of_node;
275
276 return 0;
277}
278EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
279
Kuninori Morimoto983cebd2016-08-10 02:20:19 +0000280void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
281 int is_single_links)
282{
283 /*
284 * In soc_bind_dai_link() will check cpu name after
285 * of_node matching if dai_link has cpu_dai_name.
286 * but, it will never match if name was created by
287 * fmt_single_name() remove cpu_dai_name if cpu_args
288 * was 0. See:
289 * fmt_single_name()
290 * fmt_multiple_name()
291 */
292 if (is_single_links)
293 dai_link->cpu_dai_name = NULL;
294}
295EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
296
Kuninori Morimoto0f4e0712016-08-10 02:21:25 +0000297int asoc_simple_card_clean_reference(struct snd_soc_card *card)
298{
299 struct snd_soc_dai_link *dai_link;
300 int num_links;
301
302 for (num_links = 0, dai_link = card->dai_link;
303 num_links < card->num_links;
304 num_links++, dai_link++) {
305 of_node_put(dai_link->cpu_of_node);
306 of_node_put(dai_link->codec_of_node);
307 }
308 return 0;
309}
310EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
311
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000312/* Module information */
313MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
314MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
315MODULE_LICENSE("GPL v2");