]> Pileus Git - ~andy/linux/blob - sound/soc/generic/simple-card.c
5528dd6a4e4e031e6c3f715c29fedfca34e190d0
[~andy/linux] / 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/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <sound/simple_card.h>
16
17 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
18                                        struct asoc_simple_dai *set,
19                                        unsigned int daifmt)
20 {
21         int ret = 0;
22
23         daifmt |= set->fmt;
24
25         if (daifmt)
26                 ret = snd_soc_dai_set_fmt(dai, daifmt);
27
28         if (ret == -ENOTSUPP) {
29                 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
30                 ret = 0;
31         }
32
33         if (!ret && set->sysclk)
34                 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
35
36         return ret;
37 }
38
39 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
40 {
41         struct asoc_simple_card_info *info =
42                                 snd_soc_card_get_drvdata(rtd->card);
43         struct snd_soc_dai *codec = rtd->codec_dai;
44         struct snd_soc_dai *cpu = rtd->cpu_dai;
45         unsigned int daifmt = info->daifmt;
46         int ret;
47
48         ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
49         if (ret < 0)
50                 return ret;
51
52         ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
53         if (ret < 0)
54                 return ret;
55
56         return 0;
57 }
58
59 static int
60 asoc_simple_card_sub_parse_of(struct device_node *np,
61                               struct asoc_simple_dai *dai,
62                               struct device_node **node)
63 {
64         struct clk *clk;
65         int ret;
66
67         /*
68          * get node via "sound-dai = <&phandle port>"
69          * it will be used as xxx_of_node on soc_bind_dai_link()
70          */
71         *node = of_parse_phandle(np, "sound-dai", 0);
72         if (!*node)
73                 return -ENODEV;
74
75         /* get dai->name */
76         ret = snd_soc_of_get_dai_name(np, &dai->name);
77         if (ret < 0)
78                 goto parse_error;
79
80         /*
81          * bitclock-inversion, frame-inversion
82          * bitclock-master,    frame-master
83          * and specific "format" if it has
84          */
85         dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
86
87         /*
88          * dai->sysclk come from
89          *  "clocks = <&xxx>" (if system has common clock)
90          *  or "system-clock-frequency = <xxx>"
91          *  or device's module clock.
92          */
93         if (of_property_read_bool(np, "clocks")) {
94                 clk = of_clk_get(np, 0);
95                 if (IS_ERR(clk)) {
96                         ret = PTR_ERR(clk);
97                         goto parse_error;
98                 }
99
100                 dai->sysclk = clk_get_rate(clk);
101         } else if (of_property_read_bool(np, "system-clock-frequency")) {
102                 of_property_read_u32(np,
103                                      "system-clock-frequency",
104                                      &dai->sysclk);
105         } else {
106                 clk = of_clk_get(*node, 0);
107                 if (!IS_ERR(clk))
108                         dai->sysclk = clk_get_rate(clk);
109         }
110
111         ret = 0;
112
113 parse_error:
114         of_node_put(*node);
115
116         return ret;
117 }
118
119 static int asoc_simple_card_parse_of(struct device_node *node,
120                                      struct asoc_simple_card_info *info,
121                                      struct device *dev,
122                                      struct device_node **of_cpu,
123                                      struct device_node **of_codec,
124                                      struct device_node **of_platform)
125 {
126         struct device_node *np;
127         char *name;
128         int ret;
129
130         /* get CPU/CODEC common format via simple-audio-card,format */
131         info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
132                 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
133
134         /* DAPM routes */
135         if (of_property_read_bool(node, "simple-audio-card,routing")) {
136                 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
137                                         "simple-audio-card,routing");
138                 if (ret)
139                         return ret;
140         }
141
142         /* CPU sub-node */
143         ret = -EINVAL;
144         np = of_get_child_by_name(node, "simple-audio-card,cpu");
145         if (np)
146                 ret = asoc_simple_card_sub_parse_of(np,
147                                                   &info->cpu_dai,
148                                                   of_cpu);
149         if (ret < 0)
150                 return ret;
151
152         /* CODEC sub-node */
153         ret = -EINVAL;
154         np = of_get_child_by_name(node, "simple-audio-card,codec");
155         if (np)
156                 ret = asoc_simple_card_sub_parse_of(np,
157                                                   &info->codec_dai,
158                                                   of_codec);
159         if (ret < 0)
160                 return ret;
161
162         /* card name is created from CPU/CODEC dai name */
163         name = devm_kzalloc(dev,
164                             strlen(info->cpu_dai.name)   +
165                             strlen(info->codec_dai.name) + 2,
166                             GFP_KERNEL);
167         sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
168         info->name = info->card = name;
169
170         /* simple-card assumes platform == cpu */
171         *of_platform = *of_cpu;
172
173         dev_dbg(dev, "card-name : %s\n", info->card);
174         dev_dbg(dev, "platform : %04x\n", info->daifmt);
175         dev_dbg(dev, "cpu : %s / %04x / %d\n",
176                 info->cpu_dai.name,
177                 info->cpu_dai.fmt,
178                 info->cpu_dai.sysclk);
179         dev_dbg(dev, "codec : %s / %04x / %d\n",
180                 info->codec_dai.name,
181                 info->codec_dai.fmt,
182                 info->codec_dai.sysclk);
183
184         return 0;
185 }
186
187 static int asoc_simple_card_probe(struct platform_device *pdev)
188 {
189         struct asoc_simple_card_info *cinfo;
190         struct device_node *np = pdev->dev.of_node;
191         struct device_node *of_cpu, *of_codec, *of_platform;
192         struct device *dev = &pdev->dev;
193
194         cinfo           = NULL;
195         of_cpu          = NULL;
196         of_codec        = NULL;
197         of_platform     = NULL;
198         if (np && of_device_is_available(np)) {
199                 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
200                 if (cinfo) {
201                         int ret;
202                         cinfo->snd_card.dev = &pdev->dev;
203                         ret = asoc_simple_card_parse_of(np, cinfo, dev,
204                                                         &of_cpu,
205                                                         &of_codec,
206                                                         &of_platform);
207                         if (ret < 0) {
208                                 if (ret != -EPROBE_DEFER)
209                                         dev_err(dev, "parse error %d\n", ret);
210                                 return ret;
211                         }
212                 } else {
213                         return -ENOMEM;
214                 }
215         } else {
216                 cinfo = pdev->dev.platform_data;
217                 if (!cinfo) {
218                         dev_err(dev, "no info for asoc-simple-card\n");
219                         return -EINVAL;
220                 }
221
222                 cinfo->snd_card.dev = &pdev->dev;
223         }
224
225         if (!cinfo->name        ||
226             !cinfo->card        ||
227             !cinfo->codec_dai.name      ||
228             !(cinfo->codec              || of_codec)    ||
229             !(cinfo->platform           || of_platform) ||
230             !(cinfo->cpu_dai.name       || of_cpu)) {
231                 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
232                 return -EINVAL;
233         }
234
235         /*
236          * init snd_soc_dai_link
237          */
238         cinfo->snd_link.name            = cinfo->name;
239         cinfo->snd_link.stream_name     = cinfo->name;
240         cinfo->snd_link.cpu_dai_name    = cinfo->cpu_dai.name;
241         cinfo->snd_link.platform_name   = cinfo->platform;
242         cinfo->snd_link.codec_name      = cinfo->codec;
243         cinfo->snd_link.codec_dai_name  = cinfo->codec_dai.name;
244         cinfo->snd_link.cpu_of_node     = of_cpu;
245         cinfo->snd_link.codec_of_node   = of_codec;
246         cinfo->snd_link.platform_of_node = of_platform;
247         cinfo->snd_link.init            = asoc_simple_card_dai_init;
248
249         /*
250          * init snd_soc_card
251          */
252         cinfo->snd_card.name            = cinfo->card;
253         cinfo->snd_card.owner           = THIS_MODULE;
254         cinfo->snd_card.dai_link        = &cinfo->snd_link;
255         cinfo->snd_card.num_links       = 1;
256
257         snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo);
258
259         return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
260 }
261
262 static const struct of_device_id asoc_simple_of_match[] = {
263         { .compatible = "simple-audio-card", },
264         {},
265 };
266 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
267
268 static struct platform_driver asoc_simple_card = {
269         .driver = {
270                 .name   = "asoc-simple-card",
271                 .owner = THIS_MODULE,
272                 .of_match_table = asoc_simple_of_match,
273         },
274         .probe          = asoc_simple_card_probe,
275 };
276
277 module_platform_driver(asoc_simple_card);
278
279 MODULE_ALIAS("platform:asoc-simple-card");
280 MODULE_LICENSE("GPL");
281 MODULE_DESCRIPTION("ASoC Simple Sound Card");
282 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");