blob: 095ef6426d4217ec91e001aee1f8b6997a821651 [file] [log] [blame]
Kuninori Morimoto87f937b2017-05-18 01:45:37 +00001/*
2 * ASoC audio graph SCU sound card support
3 *
4 * Copyright (C) 2017 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * based on
8 * ${LINUX}/sound/soc/generic/simple-scu-card.c
9 * ${LINUX}/sound/soc/generic/audio-graph-card.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15#include <linux/clk.h>
16#include <linux/device.h>
17#include <linux/gpio.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/of_gpio.h>
22#include <linux/of_graph.h>
23#include <linux/platform_device.h>
24#include <linux/string.h>
25#include <sound/jack.h>
26#include <sound/simple_card_utils.h>
27
28struct graph_card_data {
29 struct snd_soc_card snd_card;
30 struct snd_soc_codec_conf codec_conf;
31 struct asoc_simple_dai *dai_props;
32 struct snd_soc_dai_link *dai_link;
Kuninori Morimotoc564a5b2017-06-15 00:24:43 +000033 struct asoc_simple_card_data adata;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000034};
35
36#define graph_priv_to_card(priv) (&(priv)->snd_card)
37#define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
38#define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
39#define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
40
41static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
42{
43 struct snd_soc_pcm_runtime *rtd = substream->private_data;
44 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
45 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
46
Kuninori Morimoto6654fc72017-06-09 00:45:01 +000047 return asoc_simple_card_clk_enable(dai_props);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000048}
49
50static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
51{
52 struct snd_soc_pcm_runtime *rtd = substream->private_data;
53 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
54 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
55
Kuninori Morimoto6654fc72017-06-09 00:45:01 +000056 asoc_simple_card_clk_disable(dai_props);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000057}
58
Bhumika Goyale23d8342017-08-16 22:29:26 +053059static const struct snd_soc_ops asoc_graph_card_ops = {
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000060 .startup = asoc_graph_card_startup,
61 .shutdown = asoc_graph_card_shutdown,
62};
63
64static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
65{
66 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
67 struct snd_soc_dai *dai;
68 struct snd_soc_dai_link *dai_link;
69 struct asoc_simple_dai *dai_props;
70 int num = rtd->num;
71
72 dai_link = graph_priv_to_link(priv, num);
73 dai_props = graph_priv_to_props(priv, num);
74 dai = dai_link->dynamic ?
75 rtd->cpu_dai :
76 rtd->codec_dai;
77
78 return asoc_simple_card_init_dai(dai, dai_props);
79}
80
81static int asoc_graph_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
82 struct snd_pcm_hw_params *params)
83{
84 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000085
Kuninori Morimotoc564a5b2017-06-15 00:24:43 +000086 asoc_simple_card_convert_fixup(&priv->adata, params);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +000087
88 return 0;
89}
90
91static int asoc_graph_card_dai_link_of(struct device_node *ep,
92 struct graph_card_data *priv,
93 unsigned int daifmt,
94 int idx, int is_fe)
95{
96 struct device *dev = graph_priv_to_dev(priv);
97 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
98 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, idx);
99 struct snd_soc_card *card = graph_priv_to_card(priv);
100 int ret;
101
102 if (is_fe) {
103 /* BE is dummy */
104 dai_link->codec_of_node = NULL;
105 dai_link->codec_dai_name = "snd-soc-dummy-dai";
106 dai_link->codec_name = "snd-soc-dummy";
107
108 /* FE settings */
109 dai_link->dynamic = 1;
110 dai_link->dpcm_merged_format = 1;
111
112 ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
113 if (ret)
114 return ret;
115
116 ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai_props);
117 if (ret < 0)
118 return ret;
119
120 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
121 "fe.%s",
122 dai_link->cpu_dai_name);
123 if (ret < 0)
124 return ret;
125
126 /* card->num_links includes Codec */
127 asoc_simple_card_canonicalize_cpu(dai_link,
Kuninori Morimoto32f2bcc2017-06-22 06:22:14 +0000128 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000129 } else {
130 /* FE is dummy */
131 dai_link->cpu_of_node = NULL;
132 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
133 dai_link->cpu_name = "snd-soc-dummy";
134
135 /* BE settings */
136 dai_link->no_pcm = 1;
137 dai_link->be_hw_params_fixup = asoc_graph_card_be_hw_params_fixup;
138
139 ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
140 if (ret < 0)
141 return ret;
142
143 ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai_props);
144 if (ret < 0)
145 return ret;
146
147 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
148 "be.%s",
149 dai_link->codec_dai_name);
150 if (ret < 0)
151 return ret;
152
153 snd_soc_of_parse_audio_prefix(card,
154 &priv->codec_conf,
155 dai_link->codec_of_node,
156 "prefix");
157 }
158
Kuninori Morimoto616c3b12017-06-14 00:35:47 +0000159 ret = asoc_simple_card_of_parse_tdm(ep, dai_props);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000160 if (ret)
161 return ret;
162
163 ret = asoc_simple_card_canonicalize_dailink(dai_link);
164 if (ret < 0)
165 return ret;
166
167 dai_link->dai_fmt = daifmt;
168 dai_link->dpcm_playback = 1;
169 dai_link->dpcm_capture = 1;
170 dai_link->ops = &asoc_graph_card_ops;
171 dai_link->init = asoc_graph_card_dai_init;
172
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000173 return 0;
174}
175
176static int asoc_graph_card_parse_of(struct graph_card_data *priv)
177{
178 struct of_phandle_iterator it;
179 struct device *dev = graph_priv_to_dev(priv);
180 struct snd_soc_card *card = graph_priv_to_card(priv);
181 struct device_node *node = dev->of_node;
182 struct device_node *cpu_port;
183 struct device_node *cpu_ep;
184 struct device_node *codec_ep;
185 struct device_node *rcpu_ep;
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000186 struct device_node *codec_port;
187 struct device_node *codec_port_old;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000188 unsigned int daifmt = 0;
189 int dai_idx, ret;
190 int rc, codec;
191
192 if (!node)
193 return -EINVAL;
194
195 /*
196 * we need to consider "widgets", "mclk-fs" around here
197 * see simple-card
198 */
199
Kuninori Morimoto9fb9b2f2017-06-15 00:25:51 +0000200 ret = asoc_simple_card_of_parse_routing(card, NULL, 0);
201 if (ret < 0)
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000202 return ret;
203
Kuninori Morimotoc564a5b2017-06-15 00:24:43 +0000204 asoc_simple_card_parse_convert(dev, NULL, &priv->adata);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000205
206 /*
207 * it supports multi CPU, single CODEC only here
208 * see asoc_graph_get_dais_count
209 */
210
211 /* find 1st codec */
212 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
213 cpu_port = it.node;
214 cpu_ep = of_get_next_child(cpu_port, NULL);
215 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
216 rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
217
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000218 of_node_put(cpu_ep);
219 of_node_put(codec_ep);
220 of_node_put(rcpu_ep);
221
222 if (!codec_ep)
223 continue;
224
225 if (rcpu_ep != cpu_ep) {
226 dev_err(dev, "remote-endpoint missmatch (%s/%s/%s)\n",
227 cpu_ep->name, codec_ep->name, rcpu_ep->name);
228 ret = -EINVAL;
229 goto parse_of_err;
230 }
231
232 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
233 NULL, &daifmt);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700234 if (ret < 0) {
235 of_node_put(cpu_port);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000236 goto parse_of_err;
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700237 }
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000238 }
239
240 dai_idx = 0;
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000241 codec_port_old = NULL;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000242 for (codec = 0; codec < 2; codec++) {
243 /*
244 * To listup valid sounds continuously,
245 * detect all CPU-dummy first, and
246 * detect all dummy-Codec second
247 */
248 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
249 cpu_port = it.node;
250 cpu_ep = of_get_next_child(cpu_port, NULL);
251 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000252 codec_port = of_graph_get_port_parent(codec_ep);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000253
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000254 of_node_put(cpu_ep);
255 of_node_put(codec_ep);
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000256 of_node_put(codec_port);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000257
258 if (codec) {
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000259 if (!codec_port)
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000260 continue;
261
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000262 if (codec_port_old == codec_port)
263 continue;
264
265 codec_port_old = codec_port;
266
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000267 /* Back-End (= Codec) */
268 ret = asoc_graph_card_dai_link_of(codec_ep, priv, daifmt, dai_idx++, 0);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700269 if (ret < 0) {
270 of_node_put(cpu_port);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000271 goto parse_of_err;
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700272 }
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000273 } else {
274 /* Front-End (= CPU) */
275 ret = asoc_graph_card_dai_link_of(cpu_ep, priv, daifmt, dai_idx++, 1);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700276 if (ret < 0) {
277 of_node_put(cpu_port);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000278 goto parse_of_err;
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700279 }
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000280 }
281 }
282 }
283
284 ret = asoc_simple_card_parse_card_name(card, NULL);
285 if (ret)
286 goto parse_of_err;
287
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000288 ret = 0;
289
290parse_of_err:
291 return ret;
292}
293
294static int asoc_graph_get_dais_count(struct device *dev)
295{
296 struct of_phandle_iterator it;
297 struct device_node *node = dev->of_node;
298 struct device_node *cpu_port;
299 struct device_node *cpu_ep;
300 struct device_node *codec_ep;
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000301 struct device_node *codec_port;
302 struct device_node *codec_port_old;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000303 int count = 0;
304 int rc;
305
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000306 codec_port_old = NULL;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000307 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
308 cpu_port = it.node;
309 cpu_ep = of_get_next_child(cpu_port, NULL);
310 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000311 codec_port = of_graph_get_port_parent(codec_ep);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000312
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000313 of_node_put(cpu_ep);
314 of_node_put(codec_ep);
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000315 of_node_put(codec_port);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000316
317 if (cpu_ep)
318 count++;
Kuninori Morimotof1f94042017-06-22 06:22:49 +0000319
320 if (!codec_port)
321 continue;
322
323 if (codec_port_old == codec_port)
324 continue;
325
326 count++;
327 codec_port_old = codec_port;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000328 }
329
330 return count;
331}
332
333static int asoc_graph_card_probe(struct platform_device *pdev)
334{
335 struct graph_card_data *priv;
336 struct snd_soc_dai_link *dai_link;
337 struct asoc_simple_dai *dai_props;
338 struct device *dev = &pdev->dev;
339 struct snd_soc_card *card;
340 int num, ret;
341
342 /* Allocate the private data and the DAI link array */
343 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
344 if (!priv)
345 return -ENOMEM;
346
347 num = asoc_graph_get_dais_count(dev);
348 if (num == 0)
349 return -EINVAL;
350
Kees Cooka86854d2018-06-12 14:07:58 -0700351 dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
352 dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000353 if (!dai_props || !dai_link)
354 return -ENOMEM;
355
356 priv->dai_props = dai_props;
357 priv->dai_link = dai_link;
358
359 /* Init snd_soc_card */
360 card = graph_priv_to_card(priv);
361 card->owner = THIS_MODULE;
362 card->dev = dev;
363 card->dai_link = priv->dai_link;
364 card->num_links = num;
365 card->codec_conf = &priv->codec_conf;
366 card->num_configs = 1;
367
368 ret = asoc_graph_card_parse_of(priv);
369 if (ret < 0) {
370 if (ret != -EPROBE_DEFER)
371 dev_err(dev, "parse error %d\n", ret);
372 goto err;
373 }
374
375 snd_soc_card_set_drvdata(card, priv);
376
377 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto1a2af562017-05-19 00:58:19 +0000378 if (ret < 0)
379 goto err;
380
381 return 0;
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000382err:
383 asoc_simple_card_clean_reference(card);
384
385 return ret;
386}
387
388static int asoc_graph_card_remove(struct platform_device *pdev)
389{
390 struct snd_soc_card *card = platform_get_drvdata(pdev);
391
392 return asoc_simple_card_clean_reference(card);
393}
394
395static const struct of_device_id asoc_graph_of_match[] = {
396 { .compatible = "audio-graph-scu-card", },
397 {},
398};
399MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
400
401static struct platform_driver asoc_graph_card = {
402 .driver = {
403 .name = "asoc-audio-graph-scu-card",
Kuninori Morimoto0995fb72017-08-22 04:57:12 +0000404 .pm = &snd_soc_pm_ops,
Kuninori Morimoto87f937b2017-05-18 01:45:37 +0000405 .of_match_table = asoc_graph_of_match,
406 },
407 .probe = asoc_graph_card_probe,
408 .remove = asoc_graph_card_remove,
409};
410module_platform_driver(asoc_graph_card);
411
412MODULE_ALIAS("platform:asoc-audio-graph-scu-card");
413MODULE_LICENSE("GPL v2");
414MODULE_DESCRIPTION("ASoC Audio Graph SCU Sound Card");
415MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");