diff options
Diffstat (limited to 'sound/soc/generic/simple-card.c')
-rw-r--r-- | sound/soc/generic/simple-card.c | 211 |
1 files changed, 187 insertions, 24 deletions
diff --git a/sound/soc/generic/simple-card.c b/sound/soc/generic/simple-card.c index b2fbb7075a6c..2a1b1b5b5221 100644 --- a/sound/soc/generic/simple-card.c +++ b/sound/soc/generic/simple-card.c | |||
@@ -8,14 +8,13 @@ | |||
8 | * it under the terms of the GNU General Public License version 2 as | 8 | * it under the terms of the GNU General Public License version 2 as |
9 | * published by the Free Software Foundation. | 9 | * published by the Free Software Foundation. |
10 | */ | 10 | */ |
11 | 11 | #include <linux/clk.h> | |
12 | #include <linux/platform_device.h> | ||
13 | #include <linux/module.h> | 12 | #include <linux/module.h> |
13 | #include <linux/of.h> | ||
14 | #include <linux/platform_device.h> | ||
15 | #include <linux/string.h> | ||
14 | #include <sound/simple_card.h> | 16 | #include <sound/simple_card.h> |
15 | 17 | ||
16 | #define asoc_simple_get_card_info(p) \ | ||
17 | container_of(p->dai_link, struct asoc_simple_card_info, snd_link) | ||
18 | |||
19 | static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, | 18 | static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, |
20 | struct asoc_simple_dai *set, | 19 | struct asoc_simple_dai *set, |
21 | unsigned int daifmt) | 20 | unsigned int daifmt) |
@@ -24,7 +23,7 @@ static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, | |||
24 | 23 | ||
25 | daifmt |= set->fmt; | 24 | daifmt |= set->fmt; |
26 | 25 | ||
27 | if (!ret && daifmt) | 26 | if (daifmt) |
28 | ret = snd_soc_dai_set_fmt(dai, daifmt); | 27 | ret = snd_soc_dai_set_fmt(dai, daifmt); |
29 | 28 | ||
30 | if (ret == -ENOTSUPP) { | 29 | if (ret == -ENOTSUPP) { |
@@ -40,7 +39,8 @@ static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, | |||
40 | 39 | ||
41 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) | 40 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
42 | { | 41 | { |
43 | struct asoc_simple_card_info *info = asoc_simple_get_card_info(rtd); | 42 | struct asoc_simple_card_info *info = |
43 | snd_soc_card_get_drvdata(rtd->card); | ||
44 | struct snd_soc_dai *codec = rtd->codec_dai; | 44 | struct snd_soc_dai *codec = rtd->codec_dai; |
45 | struct snd_soc_dai *cpu = rtd->cpu_dai; | 45 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
46 | unsigned int daifmt = info->daifmt; | 46 | unsigned int daifmt = info->daifmt; |
@@ -57,22 +57,182 @@ static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) | |||
57 | return 0; | 57 | return 0; |
58 | } | 58 | } |
59 | 59 | ||
60 | static int | ||
61 | asoc_simple_card_sub_parse_of(struct device_node *np, | ||
62 | struct asoc_simple_dai *dai, | ||
63 | struct device_node **node) | ||
64 | { | ||
65 | struct clk *clk; | ||
66 | int ret; | ||
67 | |||
68 | /* | ||
69 | * get node via "sound-dai = <&phandle port>" | ||
70 | * it will be used as xxx_of_node on soc_bind_dai_link() | ||
71 | */ | ||
72 | *node = of_parse_phandle(np, "sound-dai", 0); | ||
73 | if (!*node) | ||
74 | return -ENODEV; | ||
75 | |||
76 | /* get dai->name */ | ||
77 | ret = snd_soc_of_get_dai_name(np, &dai->name); | ||
78 | if (ret < 0) | ||
79 | goto parse_error; | ||
80 | |||
81 | /* | ||
82 | * bitclock-inversion, frame-inversion | ||
83 | * bitclock-master, frame-master | ||
84 | * and specific "format" if it has | ||
85 | */ | ||
86 | dai->fmt = snd_soc_of_parse_daifmt(np, NULL); | ||
87 | |||
88 | /* | ||
89 | * dai->sysclk come from | ||
90 | * "clocks = <&xxx>" (if system has common clock) | ||
91 | * or "system-clock-frequency = <xxx>" | ||
92 | * or device's module clock. | ||
93 | */ | ||
94 | if (of_property_read_bool(np, "clocks")) { | ||
95 | clk = of_clk_get(np, 0); | ||
96 | if (IS_ERR(clk)) { | ||
97 | ret = PTR_ERR(clk); | ||
98 | goto parse_error; | ||
99 | } | ||
100 | |||
101 | dai->sysclk = clk_get_rate(clk); | ||
102 | } else if (of_property_read_bool(np, "system-clock-frequency")) { | ||
103 | of_property_read_u32(np, | ||
104 | "system-clock-frequency", | ||
105 | &dai->sysclk); | ||
106 | } else { | ||
107 | clk = of_clk_get(*node, 0); | ||
108 | if (!IS_ERR(clk)) | ||
109 | dai->sysclk = clk_get_rate(clk); | ||
110 | } | ||
111 | |||
112 | ret = 0; | ||
113 | |||
114 | parse_error: | ||
115 | of_node_put(*node); | ||
116 | |||
117 | return ret; | ||
118 | } | ||
119 | |||
120 | static int asoc_simple_card_parse_of(struct device_node *node, | ||
121 | struct asoc_simple_card_info *info, | ||
122 | struct device *dev, | ||
123 | struct device_node **of_cpu, | ||
124 | struct device_node **of_codec, | ||
125 | struct device_node **of_platform) | ||
126 | { | ||
127 | struct device_node *np; | ||
128 | char *name; | ||
129 | int ret; | ||
130 | |||
131 | /* get CPU/CODEC common format via simple-audio-card,format */ | ||
132 | info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") & | ||
133 | (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK); | ||
134 | |||
135 | /* DAPM routes */ | ||
136 | if (of_property_read_bool(node, "simple-audio-card,routing")) { | ||
137 | ret = snd_soc_of_parse_audio_routing(&info->snd_card, | ||
138 | "simple-audio-card,routing"); | ||
139 | if (ret) | ||
140 | return ret; | ||
141 | } | ||
142 | |||
143 | /* CPU sub-node */ | ||
144 | ret = -EINVAL; | ||
145 | np = of_get_child_by_name(node, "simple-audio-card,cpu"); | ||
146 | if (np) | ||
147 | ret = asoc_simple_card_sub_parse_of(np, | ||
148 | &info->cpu_dai, | ||
149 | of_cpu); | ||
150 | if (ret < 0) | ||
151 | return ret; | ||
152 | |||
153 | /* CODEC sub-node */ | ||
154 | ret = -EINVAL; | ||
155 | np = of_get_child_by_name(node, "simple-audio-card,codec"); | ||
156 | if (np) | ||
157 | ret = asoc_simple_card_sub_parse_of(np, | ||
158 | &info->codec_dai, | ||
159 | of_codec); | ||
160 | if (ret < 0) | ||
161 | return ret; | ||
162 | |||
163 | if (!info->cpu_dai.name || !info->codec_dai.name) | ||
164 | return -EINVAL; | ||
165 | |||
166 | /* card name is created from CPU/CODEC dai name */ | ||
167 | name = devm_kzalloc(dev, | ||
168 | strlen(info->cpu_dai.name) + | ||
169 | strlen(info->codec_dai.name) + 2, | ||
170 | GFP_KERNEL); | ||
171 | sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name); | ||
172 | info->name = info->card = name; | ||
173 | |||
174 | /* simple-card assumes platform == cpu */ | ||
175 | *of_platform = *of_cpu; | ||
176 | |||
177 | dev_dbg(dev, "card-name : %s\n", info->card); | ||
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 | |||
60 | static int asoc_simple_card_probe(struct platform_device *pdev) | 191 | static int asoc_simple_card_probe(struct platform_device *pdev) |
61 | { | 192 | { |
62 | struct asoc_simple_card_info *cinfo = pdev->dev.platform_data; | 193 | struct asoc_simple_card_info *cinfo; |
194 | struct device_node *np = pdev->dev.of_node; | ||
195 | struct device_node *of_cpu, *of_codec, *of_platform; | ||
63 | struct device *dev = &pdev->dev; | 196 | struct device *dev = &pdev->dev; |
197 | int ret; | ||
64 | 198 | ||
65 | if (!cinfo) { | 199 | cinfo = NULL; |
66 | dev_err(dev, "no info for asoc-simple-card\n"); | 200 | of_cpu = NULL; |
67 | return -EINVAL; | 201 | of_codec = NULL; |
202 | of_platform = NULL; | ||
203 | |||
204 | cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL); | ||
205 | if (!cinfo) | ||
206 | return -ENOMEM; | ||
207 | |||
208 | if (np && of_device_is_available(np)) { | ||
209 | cinfo->snd_card.dev = dev; | ||
210 | |||
211 | ret = asoc_simple_card_parse_of(np, cinfo, dev, | ||
212 | &of_cpu, | ||
213 | &of_codec, | ||
214 | &of_platform); | ||
215 | if (ret < 0) { | ||
216 | if (ret != -EPROBE_DEFER) | ||
217 | dev_err(dev, "parse error %d\n", ret); | ||
218 | return ret; | ||
219 | } | ||
220 | } else { | ||
221 | if (!dev->platform_data) { | ||
222 | dev_err(dev, "no info for asoc-simple-card\n"); | ||
223 | return -EINVAL; | ||
224 | } | ||
225 | |||
226 | memcpy(cinfo, dev->platform_data, sizeof(*cinfo)); | ||
227 | cinfo->snd_card.dev = dev; | ||
68 | } | 228 | } |
69 | 229 | ||
70 | if (!cinfo->name || | 230 | if (!cinfo->name || |
71 | !cinfo->card || | 231 | !cinfo->card || |
72 | !cinfo->codec || | 232 | !cinfo->codec_dai.name || |
73 | !cinfo->platform || | 233 | !(cinfo->codec || of_codec) || |
74 | !cinfo->cpu_dai.name || | 234 | !(cinfo->platform || of_platform) || |
75 | !cinfo->codec_dai.name) { | 235 | !(cinfo->cpu_dai.name || of_cpu)) { |
76 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); | 236 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
77 | return -EINVAL; | 237 | return -EINVAL; |
78 | } | 238 | } |
@@ -86,6 +246,9 @@ static int asoc_simple_card_probe(struct platform_device *pdev) | |||
86 | cinfo->snd_link.platform_name = cinfo->platform; | 246 | cinfo->snd_link.platform_name = cinfo->platform; |
87 | cinfo->snd_link.codec_name = cinfo->codec; | 247 | cinfo->snd_link.codec_name = cinfo->codec; |
88 | cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name; | 248 | cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name; |
249 | cinfo->snd_link.cpu_of_node = of_cpu; | ||
250 | cinfo->snd_link.codec_of_node = of_codec; | ||
251 | cinfo->snd_link.platform_of_node = of_platform; | ||
89 | cinfo->snd_link.init = asoc_simple_card_dai_init; | 252 | cinfo->snd_link.init = asoc_simple_card_dai_init; |
90 | 253 | ||
91 | /* | 254 | /* |
@@ -95,25 +258,25 @@ static int asoc_simple_card_probe(struct platform_device *pdev) | |||
95 | cinfo->snd_card.owner = THIS_MODULE; | 258 | cinfo->snd_card.owner = THIS_MODULE; |
96 | cinfo->snd_card.dai_link = &cinfo->snd_link; | 259 | cinfo->snd_card.dai_link = &cinfo->snd_link; |
97 | cinfo->snd_card.num_links = 1; | 260 | cinfo->snd_card.num_links = 1; |
98 | cinfo->snd_card.dev = &pdev->dev; | ||
99 | 261 | ||
100 | return snd_soc_register_card(&cinfo->snd_card); | 262 | snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo); |
101 | } | ||
102 | 263 | ||
103 | static int asoc_simple_card_remove(struct platform_device *pdev) | 264 | return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card); |
104 | { | ||
105 | struct asoc_simple_card_info *cinfo = pdev->dev.platform_data; | ||
106 | |||
107 | return snd_soc_unregister_card(&cinfo->snd_card); | ||
108 | } | 265 | } |
109 | 266 | ||
267 | static const struct of_device_id asoc_simple_of_match[] = { | ||
268 | { .compatible = "simple-audio-card", }, | ||
269 | {}, | ||
270 | }; | ||
271 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); | ||
272 | |||
110 | static struct platform_driver asoc_simple_card = { | 273 | static struct platform_driver asoc_simple_card = { |
111 | .driver = { | 274 | .driver = { |
112 | .name = "asoc-simple-card", | 275 | .name = "asoc-simple-card", |
113 | .owner = THIS_MODULE, | 276 | .owner = THIS_MODULE, |
277 | .of_match_table = asoc_simple_of_match, | ||
114 | }, | 278 | }, |
115 | .probe = asoc_simple_card_probe, | 279 | .probe = asoc_simple_card_probe, |
116 | .remove = asoc_simple_card_remove, | ||
117 | }; | 280 | }; |
118 | 281 | ||
119 | module_platform_driver(asoc_simple_card); | 282 | module_platform_driver(asoc_simple_card); |