OSDN Git Service

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