blob: beb4e3817d22f9d1894288ee1392c63ab802bc22 [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
Kuninori Morimotoaaad9c12017-06-05 04:28:12 +000054 dev_dbg(dev, "format : %04x\n", daifmt);
55
Kuninori Morimotoabd31472016-05-31 09:00:14 +000056 return 0;
57}
58EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
Kuninori Morimoto1db33122016-07-11 23:57:14 +000059
60int asoc_simple_card_set_dailink_name(struct device *dev,
61 struct snd_soc_dai_link *dai_link,
62 const char *fmt, ...)
63{
64 va_list ap;
65 char *name = NULL;
66 int ret = -ENOMEM;
67
68 va_start(ap, fmt);
69 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
70 va_end(ap);
71
72 if (name) {
73 ret = 0;
74
75 dai_link->name = name;
76 dai_link->stream_name = name;
Kuninori Morimoto45797712017-06-05 04:28:29 +000077
78 dev_dbg(dev, "name : %s\n", name);
Kuninori Morimoto1db33122016-07-11 23:57:14 +000079 }
80
81 return ret;
82}
83EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000084
85int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
86 char *prefix)
87{
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000088 int ret;
89
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000090 if (!prefix)
91 prefix = "";
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +000092
93 /* Parse the card name from DT */
Kuninori Morimotodedfaa12017-04-20 01:34:49 +000094 ret = snd_soc_of_parse_card_name(card, "label");
95 if (ret < 0) {
96 char prop[128];
97
98 snprintf(prop, sizeof(prop), "%sname", prefix);
99 ret = snd_soc_of_parse_card_name(card, prop);
100 if (ret < 0)
101 return ret;
102 }
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +0000103
104 if (!card->name && card->dai_link)
105 card->name = card->dai_link->name;
106
Kuninori Morimotod4dbcb62017-06-05 04:27:56 +0000107 dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : "");
108
Kuninori Morimotofc55c9b2016-07-11 23:59:16 +0000109 return 0;
110}
111EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000112
Kuninori Morimoto891caea2017-06-09 00:43:18 +0000113static void asoc_simple_card_clk_register(struct asoc_simple_dai *dai,
114 struct clk *clk)
115{
116 dai->clk = clk;
117}
118
119int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai)
120{
121 return clk_prepare_enable(dai->clk);
122}
123
124void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai)
125{
126 clk_disable_unprepare(dai->clk);
127}
128
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000129int asoc_simple_card_parse_clk(struct device *dev,
130 struct device_node *node,
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000131 struct device_node *dai_of_node,
Kuninori Morimoto8e166382017-06-05 04:28:45 +0000132 struct asoc_simple_dai *simple_dai,
133 const char *name)
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000134{
135 struct clk *clk;
136 u32 val;
137
138 /*
139 * Parse dai->sysclk come from "clocks = <&xxx>"
140 * (if system has common clock)
141 * or "system-clock-frequency = <xxx>"
142 * or device's module clock.
143 */
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000144 clk = devm_get_clk_from_child(dev, node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000145 if (!IS_ERR(clk)) {
146 simple_dai->sysclk = clk_get_rate(clk);
Kuninori Morimoto891caea2017-06-09 00:43:18 +0000147
148 asoc_simple_card_clk_register(simple_dai, clk);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000149 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
150 simple_dai->sysclk = val;
151 } else {
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000152 clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000153 if (!IS_ERR(clk))
154 simple_dai->sysclk = clk_get_rate(clk);
155 }
156
Kuninori Morimoto8e166382017-06-05 04:28:45 +0000157 dev_dbg(dev, "%s : sysclk = %d\n", name, simple_dai->sysclk);
158
Kuninori Morimotobb6fc622016-08-08 05:59:56 +0000159 return 0;
160}
161EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
162
Kuninori Morimotoae30a692016-08-08 06:01:43 +0000163int asoc_simple_card_parse_dai(struct device_node *node,
164 struct device_node **dai_of_node,
165 const char **dai_name,
166 const char *list_name,
167 const char *cells_name,
168 int *is_single_link)
169{
170 struct of_phandle_args args;
171 int ret;
172
173 if (!node)
174 return 0;
175
176 /*
177 * Get node via "sound-dai = <&phandle port>"
178 * it will be used as xxx_of_node on soc_bind_dai_link()
179 */
180 ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
181 if (ret)
182 return ret;
183
184 /* Get dai->name */
185 if (dai_name) {
186 ret = snd_soc_of_get_dai_name(node, dai_name);
187 if (ret < 0)
188 return ret;
189 }
190
191 *dai_of_node = args.np;
192
193 if (is_single_link)
194 *is_single_link = !args.args_count;
195
196 return 0;
197}
198EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
199
Kuninori Morimoto16893332017-04-20 01:35:18 +0000200static int asoc_simple_card_get_dai_id(struct device_node *ep)
201{
202 struct device_node *node;
203 struct device_node *endpoint;
204 int i, id;
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000205 int ret;
206
207 ret = snd_soc_get_dai_id(ep);
208 if (ret != -ENOTSUPP)
209 return ret;
Kuninori Morimoto16893332017-04-20 01:35:18 +0000210
211 node = of_graph_get_port_parent(ep);
212
Kuninori Morimoto73b17f12017-05-18 01:39:44 +0000213 /*
214 * Non HDMI sound case, counting port/endpoint on its DT
215 * is enough. Let's count it.
216 */
Kuninori Morimoto16893332017-04-20 01:35:18 +0000217 i = 0;
218 id = -1;
219 for_each_endpoint_of_node(node, endpoint) {
220 if (endpoint == ep)
221 id = i;
222 i++;
223 }
224 if (id < 0)
225 return -ENODEV;
226
227 return id;
228}
229
230int asoc_simple_card_parse_graph_dai(struct device_node *ep,
231 struct device_node **dai_of_node,
232 const char **dai_name)
233{
234 struct device_node *node;
235 struct of_phandle_args args;
236 int ret;
237
238 if (!ep)
239 return 0;
240 if (!dai_name)
241 return 0;
242
243 /*
244 * of_graph_get_port_parent() will call
245 * of_node_put(). So, call of_node_get() here
246 */
247 of_node_get(ep);
248 node = of_graph_get_port_parent(ep);
249
250 /* Get dai->name */
251 args.np = node;
252 args.args[0] = asoc_simple_card_get_dai_id(ep);
253 args.args_count = (of_graph_get_endpoint_count(node) > 1);
254
255 ret = snd_soc_get_dai_name(&args, dai_name);
256 if (ret < 0)
257 return ret;
258
259 *dai_of_node = node;
260
261 return 0;
262}
263EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
264
Kuninori Morimoto21ba62f2016-08-09 05:48:30 +0000265int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
266 struct asoc_simple_dai *simple_dai)
267{
268 int ret;
269
270 if (simple_dai->sysclk) {
271 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
272 if (ret && ret != -ENOTSUPP) {
273 dev_err(dai->dev, "simple-card: set_sysclk error\n");
274 return ret;
275 }
276 }
277
278 if (simple_dai->slots) {
279 ret = snd_soc_dai_set_tdm_slot(dai,
280 simple_dai->tx_slot_mask,
281 simple_dai->rx_slot_mask,
282 simple_dai->slots,
283 simple_dai->slot_width);
284 if (ret && ret != -ENOTSUPP) {
285 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
286 return ret;
287 }
288 }
289
290 return 0;
291}
292EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
293
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000294int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
295{
Kuninori Morimotoc262c9a2016-08-09 05:49:41 +0000296 /* Assumes platform == cpu */
297 if (!dai_link->platform_of_node)
298 dai_link->platform_of_node = dai_link->cpu_of_node;
299
300 return 0;
301}
302EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
303
Kuninori Morimoto983cebd2016-08-10 02:20:19 +0000304void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
305 int is_single_links)
306{
307 /*
308 * In soc_bind_dai_link() will check cpu name after
309 * of_node matching if dai_link has cpu_dai_name.
310 * but, it will never match if name was created by
311 * fmt_single_name() remove cpu_dai_name if cpu_args
312 * was 0. See:
313 * fmt_single_name()
314 * fmt_multiple_name()
315 */
316 if (is_single_links)
317 dai_link->cpu_dai_name = NULL;
318}
319EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
320
Kuninori Morimoto0f4e0712016-08-10 02:21:25 +0000321int asoc_simple_card_clean_reference(struct snd_soc_card *card)
322{
323 struct snd_soc_dai_link *dai_link;
324 int num_links;
325
326 for (num_links = 0, dai_link = card->dai_link;
327 num_links < card->num_links;
328 num_links++, dai_link++) {
329 of_node_put(dai_link->cpu_of_node);
330 of_node_put(dai_link->codec_of_node);
331 }
332 return 0;
333}
334EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
335
Kuninori Morimoto1f85e112016-08-03 01:24:05 +0000336/* Module information */
337MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
338MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
339MODULE_LICENSE("GPL v2");