ASoC: simple-card: tidyup cpu/codec dai_fmt settings for non-DT
[deliverable/linux.git] / sound / soc / generic / simple-card.c
1 /*
2 * ASoC simple sound card support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/string.h>
17 #include <sound/simple_card.h>
18 #include <sound/soc-dai.h>
19 #include <sound/soc.h>
20
21 struct simple_card_data {
22 struct snd_soc_card snd_card;
23 unsigned int daifmt;
24 struct asoc_simple_dai cpu_dai;
25 struct asoc_simple_dai codec_dai;
26 struct snd_soc_dai_link snd_link;
27 };
28
29 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
30 struct asoc_simple_dai *set)
31 {
32 int ret;
33
34 if (set->fmt) {
35 ret = snd_soc_dai_set_fmt(dai, set->fmt);
36 if (ret && ret != -ENOTSUPP) {
37 dev_err(dai->dev, "simple-card: set_fmt error\n");
38 goto err;
39 }
40 }
41
42 if (set->sysclk) {
43 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
44 if (ret && ret != -ENOTSUPP) {
45 dev_err(dai->dev, "simple-card: set_sysclk error\n");
46 goto err;
47 }
48 }
49
50 if (set->slots) {
51 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
52 set->slots,
53 set->slot_width);
54 if (ret && ret != -ENOTSUPP) {
55 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
56 goto err;
57 }
58 }
59
60 ret = 0;
61
62 err:
63 return ret;
64 }
65
66 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
67 {
68 struct simple_card_data *priv =
69 snd_soc_card_get_drvdata(rtd->card);
70 struct snd_soc_dai *codec = rtd->codec_dai;
71 struct snd_soc_dai *cpu = rtd->cpu_dai;
72 int ret;
73
74 ret = __asoc_simple_card_dai_init(codec, &priv->codec_dai);
75 if (ret < 0)
76 return ret;
77
78 ret = __asoc_simple_card_dai_init(cpu, &priv->cpu_dai);
79 if (ret < 0)
80 return ret;
81
82 return 0;
83 }
84
85 static int
86 asoc_simple_card_sub_parse_of(struct device_node *np,
87 unsigned int daifmt,
88 struct asoc_simple_dai *dai,
89 const struct device_node **p_node,
90 const char **name)
91 {
92 struct device_node *node;
93 struct clk *clk;
94 int ret;
95
96 /*
97 * get node via "sound-dai = <&phandle port>"
98 * it will be used as xxx_of_node on soc_bind_dai_link()
99 */
100 node = of_parse_phandle(np, "sound-dai", 0);
101 if (!node)
102 return -ENODEV;
103 *p_node = node;
104
105 /* get dai->name */
106 ret = snd_soc_of_get_dai_name(np, name);
107 if (ret < 0)
108 goto parse_error;
109
110 /* parse TDM slot */
111 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
112 if (ret)
113 goto parse_error;
114
115 /*
116 * bitclock-inversion, frame-inversion
117 * bitclock-master, frame-master
118 * and specific "format" if it has
119 */
120 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
121 dai->fmt |= daifmt;
122
123 /*
124 * dai->sysclk come from
125 * "clocks = <&xxx>" (if system has common clock)
126 * or "system-clock-frequency = <xxx>"
127 * or device's module clock.
128 */
129 if (of_property_read_bool(np, "clocks")) {
130 clk = of_clk_get(np, 0);
131 if (IS_ERR(clk)) {
132 ret = PTR_ERR(clk);
133 goto parse_error;
134 }
135
136 dai->sysclk = clk_get_rate(clk);
137 } else if (of_property_read_bool(np, "system-clock-frequency")) {
138 of_property_read_u32(np,
139 "system-clock-frequency",
140 &dai->sysclk);
141 } else {
142 clk = of_clk_get(node, 0);
143 if (!IS_ERR(clk))
144 dai->sysclk = clk_get_rate(clk);
145 }
146
147 ret = 0;
148
149 parse_error:
150 of_node_put(node);
151
152 return ret;
153 }
154
155 static int asoc_simple_card_parse_of(struct device_node *node,
156 struct simple_card_data *priv,
157 struct device *dev)
158 {
159 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
160 struct device_node *np;
161 char *name;
162 int ret;
163
164 /* parsing the card name from DT */
165 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
166
167 /* get CPU/CODEC common format via simple-audio-card,format */
168 priv->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
169 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
170
171 /* off-codec widgets */
172 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
173 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
174 "simple-audio-card,widgets");
175 if (ret)
176 return ret;
177 }
178
179 /* DAPM routes */
180 if (of_property_read_bool(node, "simple-audio-card,routing")) {
181 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
182 "simple-audio-card,routing");
183 if (ret)
184 return ret;
185 }
186
187 /* CPU sub-node */
188 ret = -EINVAL;
189 np = of_get_child_by_name(node, "simple-audio-card,cpu");
190 if (np)
191 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
192 &priv->cpu_dai,
193 &dai_link->cpu_of_node,
194 &dai_link->cpu_dai_name);
195 if (ret < 0)
196 return ret;
197
198 /* CODEC sub-node */
199 ret = -EINVAL;
200 np = of_get_child_by_name(node, "simple-audio-card,codec");
201 if (np)
202 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
203 &priv->codec_dai,
204 &dai_link->codec_of_node,
205 &dai_link->codec_dai_name);
206 if (ret < 0)
207 return ret;
208
209 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
210 return -EINVAL;
211
212 /* card name is created from CPU/CODEC dai name */
213 name = devm_kzalloc(dev,
214 strlen(dai_link->cpu_dai_name) +
215 strlen(dai_link->codec_dai_name) + 2,
216 GFP_KERNEL);
217 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
218 dai_link->codec_dai_name);
219 if (!priv->snd_card.name)
220 priv->snd_card.name = name;
221 dai_link->name = dai_link->stream_name = name;
222
223 /* simple-card assumes platform == cpu */
224 dai_link->platform_of_node = dai_link->cpu_of_node;
225
226 dev_dbg(dev, "card-name : %s\n", name);
227 dev_dbg(dev, "platform : %04x\n", priv->daifmt);
228 dev_dbg(dev, "cpu : %s / %04x / %d\n",
229 dai_link->cpu_dai_name,
230 priv->cpu_dai.fmt,
231 priv->cpu_dai.sysclk);
232 dev_dbg(dev, "codec : %s / %04x / %d\n",
233 dai_link->codec_dai_name,
234 priv->codec_dai.fmt,
235 priv->codec_dai.sysclk);
236
237 /*
238 * soc_bind_dai_link() will check cpu name
239 * after of_node matching if dai_link has cpu_dai_name.
240 * but, it will never match if name was created by fmt_single_name()
241 * remove cpu_dai_name to escape name matching.
242 * see
243 * fmt_single_name()
244 * fmt_multiple_name()
245 */
246 dai_link->cpu_dai_name = NULL;
247
248 return 0;
249 }
250
251 static int asoc_simple_card_probe(struct platform_device *pdev)
252 {
253 struct simple_card_data *priv;
254 struct snd_soc_dai_link *dai_link;
255 struct device_node *np = pdev->dev.of_node;
256 struct device *dev = &pdev->dev;
257 int ret;
258
259 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
260 if (!priv)
261 return -ENOMEM;
262
263 /*
264 * init snd_soc_card
265 */
266 priv->snd_card.owner = THIS_MODULE;
267 priv->snd_card.dev = dev;
268 dai_link = &priv->snd_link;
269 priv->snd_card.dai_link = dai_link;
270 priv->snd_card.num_links = 1;
271
272 if (np && of_device_is_available(np)) {
273
274 ret = asoc_simple_card_parse_of(np, priv, dev);
275 if (ret < 0) {
276 if (ret != -EPROBE_DEFER)
277 dev_err(dev, "parse error %d\n", ret);
278 return ret;
279 }
280 } else {
281 struct asoc_simple_card_info *cinfo;
282
283 cinfo = dev->platform_data;
284 if (!cinfo) {
285 dev_err(dev, "no info for asoc-simple-card\n");
286 return -EINVAL;
287 }
288
289 if (!cinfo->name ||
290 !cinfo->card ||
291 !cinfo->codec_dai.name ||
292 !cinfo->codec ||
293 !cinfo->platform ||
294 !cinfo->cpu_dai.name) {
295 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
296 return -EINVAL;
297 }
298
299 priv->snd_card.name = cinfo->card;
300 dai_link->name = cinfo->name;
301 dai_link->stream_name = cinfo->name;
302 dai_link->platform_name = cinfo->platform;
303 dai_link->codec_name = cinfo->codec;
304 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
305 dai_link->codec_dai_name = cinfo->codec_dai.name;
306 memcpy(&priv->cpu_dai, &cinfo->cpu_dai,
307 sizeof(priv->cpu_dai));
308 memcpy(&priv->codec_dai, &cinfo->codec_dai,
309 sizeof(priv->codec_dai));
310
311 priv->cpu_dai.fmt |= cinfo->daifmt;
312 priv->codec_dai.fmt |= cinfo->daifmt;
313 }
314
315 /*
316 * init snd_soc_dai_link
317 */
318 dai_link->init = asoc_simple_card_dai_init;
319
320 snd_soc_card_set_drvdata(&priv->snd_card, priv);
321
322 return devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
323 }
324
325 static const struct of_device_id asoc_simple_of_match[] = {
326 { .compatible = "simple-audio-card", },
327 {},
328 };
329 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
330
331 static struct platform_driver asoc_simple_card = {
332 .driver = {
333 .name = "asoc-simple-card",
334 .owner = THIS_MODULE,
335 .of_match_table = asoc_simple_of_match,
336 },
337 .probe = asoc_simple_card_probe,
338 };
339
340 module_platform_driver(asoc_simple_card);
341
342 MODULE_ALIAS("platform:asoc-simple-card");
343 MODULE_LICENSE("GPL");
344 MODULE_DESCRIPTION("ASoC Simple Sound Card");
345 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.039348 seconds and 6 git commands to generate.