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