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