]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/generic/audio-graph-scu-card.c
Merge tag 'drm-misc-fixes-2017-07-27' of git://anongit.freedesktop.org/git/drm-misc...
[karo-tx-linux.git] / sound / soc / generic / audio-graph-scu-card.c
1 /*
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
28 struct 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;
33         struct asoc_simple_card_data adata;
34 };
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
41 static 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
47         return asoc_simple_card_clk_enable(dai_props);
48 }
49
50 static 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
56         asoc_simple_card_clk_disable(dai_props);
57 }
58
59 static struct snd_soc_ops asoc_graph_card_ops = {
60         .startup = asoc_graph_card_startup,
61         .shutdown = asoc_graph_card_shutdown,
62 };
63
64 static 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
81 static 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);
85
86         asoc_simple_card_convert_fixup(&priv->adata, params);
87
88         return 0;
89 }
90
91 static 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,
128                         of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
129         } 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
159         ret = asoc_simple_card_of_parse_tdm(ep, dai_props);
160         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
173         return 0;
174 }
175
176 static 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;
186         struct device_node *codec_port;
187         struct device_node *codec_port_old;
188         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
200         ret = asoc_simple_card_of_parse_routing(card, NULL, 0);
201         if (ret < 0)
202                 return ret;
203
204         asoc_simple_card_parse_convert(dev, NULL, &priv->adata);
205
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
218                 of_node_put(cpu_port);
219                 of_node_put(cpu_ep);
220                 of_node_put(codec_ep);
221                 of_node_put(rcpu_ep);
222
223                 if (!codec_ep)
224                         continue;
225
226                 if (rcpu_ep != cpu_ep) {
227                         dev_err(dev, "remote-endpoint missmatch (%s/%s/%s)\n",
228                                 cpu_ep->name, codec_ep->name, rcpu_ep->name);
229                         ret = -EINVAL;
230                         goto parse_of_err;
231                 }
232
233                 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
234                                                             NULL, &daifmt);
235                 if (ret < 0)
236                         goto parse_of_err;
237         }
238
239         dai_idx = 0;
240         codec_port_old = NULL;
241         for (codec = 0; codec < 2; codec++) {
242                 /*
243                  * To listup valid sounds continuously,
244                  * detect all CPU-dummy first, and
245                  * detect all dummy-Codec second
246                  */
247                 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
248                         cpu_port = it.node;
249                         cpu_ep   = of_get_next_child(cpu_port, NULL);
250                         codec_ep = of_graph_get_remote_endpoint(cpu_ep);
251                         codec_port = of_graph_get_port_parent(codec_ep);
252
253                         of_node_put(cpu_port);
254                         of_node_put(cpu_ep);
255                         of_node_put(codec_ep);
256                         of_node_put(codec_port);
257
258                         if (codec) {
259                                 if (!codec_port)
260                                         continue;
261
262                                 if (codec_port_old == codec_port)
263                                         continue;
264
265                                 codec_port_old = codec_port;
266
267                                 /* Back-End (= Codec) */
268                                 ret = asoc_graph_card_dai_link_of(codec_ep, priv, daifmt, dai_idx++, 0);
269                                 if (ret < 0)
270                                         goto parse_of_err;
271                         } else {
272                                 /* Front-End (= CPU) */
273                                 ret = asoc_graph_card_dai_link_of(cpu_ep, priv, daifmt, dai_idx++, 1);
274                                 if (ret < 0)
275                                         goto parse_of_err;
276                         }
277                 }
278         }
279
280         ret = asoc_simple_card_parse_card_name(card, NULL);
281         if (ret)
282                 goto parse_of_err;
283
284         ret = 0;
285
286 parse_of_err:
287         return ret;
288 }
289
290 static int asoc_graph_get_dais_count(struct device *dev)
291 {
292         struct of_phandle_iterator it;
293         struct device_node *node = dev->of_node;
294         struct device_node *cpu_port;
295         struct device_node *cpu_ep;
296         struct device_node *codec_ep;
297         struct device_node *codec_port;
298         struct device_node *codec_port_old;
299         int count = 0;
300         int rc;
301
302         codec_port_old = NULL;
303         of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
304                 cpu_port = it.node;
305                 cpu_ep   = of_get_next_child(cpu_port, NULL);
306                 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
307                 codec_port = of_graph_get_port_parent(codec_ep);
308
309                 of_node_put(cpu_port);
310                 of_node_put(cpu_ep);
311                 of_node_put(codec_ep);
312                 of_node_put(codec_port);
313
314                 if (cpu_ep)
315                         count++;
316
317                 if (!codec_port)
318                         continue;
319
320                 if (codec_port_old == codec_port)
321                         continue;
322
323                 count++;
324                 codec_port_old = codec_port;
325         }
326
327         return count;
328 }
329
330 static int asoc_graph_card_probe(struct platform_device *pdev)
331 {
332         struct graph_card_data *priv;
333         struct snd_soc_dai_link *dai_link;
334         struct asoc_simple_dai *dai_props;
335         struct device *dev = &pdev->dev;
336         struct snd_soc_card *card;
337         int num, ret;
338
339         /* Allocate the private data and the DAI link array */
340         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
341         if (!priv)
342                 return -ENOMEM;
343
344         num = asoc_graph_get_dais_count(dev);
345         if (num == 0)
346                 return -EINVAL;
347
348         dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
349         dai_link  = devm_kzalloc(dev, sizeof(*dai_link)  * num, GFP_KERNEL);
350         if (!dai_props || !dai_link)
351                 return -ENOMEM;
352
353         priv->dai_props                 = dai_props;
354         priv->dai_link                  = dai_link;
355
356         /* Init snd_soc_card */
357         card = graph_priv_to_card(priv);
358         card->owner             = THIS_MODULE;
359         card->dev               = dev;
360         card->dai_link          = priv->dai_link;
361         card->num_links         = num;
362         card->codec_conf        = &priv->codec_conf;
363         card->num_configs       = 1;
364
365         ret = asoc_graph_card_parse_of(priv);
366         if (ret < 0) {
367                 if (ret != -EPROBE_DEFER)
368                         dev_err(dev, "parse error %d\n", ret);
369                 goto err;
370         }
371
372         snd_soc_card_set_drvdata(card, priv);
373
374         ret = devm_snd_soc_register_card(dev, card);
375         if (ret < 0)
376                 goto err;
377
378         return 0;
379 err:
380         asoc_simple_card_clean_reference(card);
381
382         return ret;
383 }
384
385 static int asoc_graph_card_remove(struct platform_device *pdev)
386 {
387         struct snd_soc_card *card = platform_get_drvdata(pdev);
388
389         return asoc_simple_card_clean_reference(card);
390 }
391
392 static const struct of_device_id asoc_graph_of_match[] = {
393         { .compatible = "audio-graph-scu-card", },
394         {},
395 };
396 MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
397
398 static struct platform_driver asoc_graph_card = {
399         .driver = {
400                 .name = "asoc-audio-graph-scu-card",
401                 .of_match_table = asoc_graph_of_match,
402         },
403         .probe = asoc_graph_card_probe,
404         .remove = asoc_graph_card_remove,
405 };
406 module_platform_driver(asoc_graph_card);
407
408 MODULE_ALIAS("platform:asoc-audio-graph-scu-card");
409 MODULE_LICENSE("GPL v2");
410 MODULE_DESCRIPTION("ASoC Audio Graph SCU Sound Card");
411 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");