blob: 64e8b9728580089f6d3db96c0eb39872e34ca66a [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;
33 u32 convert_rate;
34 u32 convert_channels;
35};
36
37#define graph_priv_to_card(priv) (&(priv)->snd_card)
38#define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
39#define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
40#define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
41
42static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
43{
44 struct snd_soc_pcm_runtime *rtd = substream->private_data;
45 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
46 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
47
48 return clk_prepare_enable(dai_props->clk);
49}
50
51static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
52{
53 struct snd_soc_pcm_runtime *rtd = substream->private_data;
54 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
55 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
56
57 clk_disable_unprepare(dai_props->clk);
58}
59
60static struct snd_soc_ops asoc_graph_card_ops = {
61 .startup = asoc_graph_card_startup,
62 .shutdown = asoc_graph_card_shutdown,
63};
64
65static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
66{
67 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
68 struct snd_soc_dai *dai;
69 struct snd_soc_dai_link *dai_link;
70 struct asoc_simple_dai *dai_props;
71 int num = rtd->num;
72
73 dai_link = graph_priv_to_link(priv, num);
74 dai_props = graph_priv_to_props(priv, num);
75 dai = dai_link->dynamic ?
76 rtd->cpu_dai :
77 rtd->codec_dai;
78
79 return asoc_simple_card_init_dai(dai, dai_props);
80}
81
82static int asoc_graph_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
83 struct snd_pcm_hw_params *params)
84{
85 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
86 struct snd_interval *rate = hw_param_interval(params,
87 SNDRV_PCM_HW_PARAM_RATE);
88 struct snd_interval *channels = hw_param_interval(params,
89 SNDRV_PCM_HW_PARAM_CHANNELS);
90
91 if (priv->convert_rate)
92 rate->min =
93 rate->max = priv->convert_rate;
94
95 if (priv->convert_channels)
96 channels->min =
97 channels->max = priv->convert_channels;
98
99 return 0;
100}
101
102static int asoc_graph_card_dai_link_of(struct device_node *ep,
103 struct graph_card_data *priv,
104 unsigned int daifmt,
105 int idx, int is_fe)
106{
107 struct device *dev = graph_priv_to_dev(priv);
108 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
109 struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, idx);
110 struct snd_soc_card *card = graph_priv_to_card(priv);
111 int ret;
112
113 if (is_fe) {
114 /* BE is dummy */
115 dai_link->codec_of_node = NULL;
116 dai_link->codec_dai_name = "snd-soc-dummy-dai";
117 dai_link->codec_name = "snd-soc-dummy";
118
119 /* FE settings */
120 dai_link->dynamic = 1;
121 dai_link->dpcm_merged_format = 1;
122
123 ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
124 if (ret)
125 return ret;
126
127 ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai_props);
128 if (ret < 0)
129 return ret;
130
131 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
132 "fe.%s",
133 dai_link->cpu_dai_name);
134 if (ret < 0)
135 return ret;
136
137 /* card->num_links includes Codec */
138 asoc_simple_card_canonicalize_cpu(dai_link,
139 (card->num_links - 1) == 1);
140 } else {
141 /* FE is dummy */
142 dai_link->cpu_of_node = NULL;
143 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
144 dai_link->cpu_name = "snd-soc-dummy";
145
146 /* BE settings */
147 dai_link->no_pcm = 1;
148 dai_link->be_hw_params_fixup = asoc_graph_card_be_hw_params_fixup;
149
150 ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
151 if (ret < 0)
152 return ret;
153
154 ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai_props);
155 if (ret < 0)
156 return ret;
157
158 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
159 "be.%s",
160 dai_link->codec_dai_name);
161 if (ret < 0)
162 return ret;
163
164 snd_soc_of_parse_audio_prefix(card,
165 &priv->codec_conf,
166 dai_link->codec_of_node,
167 "prefix");
168 }
169
170 ret = snd_soc_of_parse_tdm_slot(ep,
171 &dai_props->tx_slot_mask,
172 &dai_props->rx_slot_mask,
173 &dai_props->slots,
174 &dai_props->slot_width);
175 if (ret)
176 return ret;
177
178 ret = asoc_simple_card_canonicalize_dailink(dai_link);
179 if (ret < 0)
180 return ret;
181
182 dai_link->dai_fmt = daifmt;
183 dai_link->dpcm_playback = 1;
184 dai_link->dpcm_capture = 1;
185 dai_link->ops = &asoc_graph_card_ops;
186 dai_link->init = asoc_graph_card_dai_init;
187
188 dev_dbg(dev, "\t%s / %04x / %d\n",
189 dai_link->name,
190 dai_link->dai_fmt,
191 dai_props->sysclk);
192
193 return 0;
194}
195
196static int asoc_graph_card_parse_of(struct graph_card_data *priv)
197{
198 struct of_phandle_iterator it;
199 struct device *dev = graph_priv_to_dev(priv);
200 struct snd_soc_card *card = graph_priv_to_card(priv);
201 struct device_node *node = dev->of_node;
202 struct device_node *cpu_port;
203 struct device_node *cpu_ep;
204 struct device_node *codec_ep;
205 struct device_node *rcpu_ep;
206 unsigned int daifmt = 0;
207 int dai_idx, ret;
208 int rc, codec;
209
210 if (!node)
211 return -EINVAL;
212
213 /*
214 * we need to consider "widgets", "mclk-fs" around here
215 * see simple-card
216 */
217
218 ret = snd_soc_of_parse_audio_routing(card, "routing");
219 if (ret)
220 return ret;
221
222 /* sampling rate convert */
223 of_property_read_u32(node, "convert-rate", &priv->convert_rate);
224
225 /* channels transfer */
226 of_property_read_u32(node, "convert-channels", &priv->convert_channels);
227
228 /*
229 * it supports multi CPU, single CODEC only here
230 * see asoc_graph_get_dais_count
231 */
232
233 /* find 1st codec */
234 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
235 cpu_port = it.node;
236 cpu_ep = of_get_next_child(cpu_port, NULL);
237 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
238 rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
239
240 of_node_put(cpu_port);
241 of_node_put(cpu_ep);
242 of_node_put(codec_ep);
243 of_node_put(rcpu_ep);
244
245 if (!codec_ep)
246 continue;
247
248 if (rcpu_ep != cpu_ep) {
249 dev_err(dev, "remote-endpoint missmatch (%s/%s/%s)\n",
250 cpu_ep->name, codec_ep->name, rcpu_ep->name);
251 ret = -EINVAL;
252 goto parse_of_err;
253 }
254
255 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
256 NULL, &daifmt);
257 if (ret < 0)
258 goto parse_of_err;
259 }
260
261 dai_idx = 0;
262 for (codec = 0; codec < 2; codec++) {
263 /*
264 * To listup valid sounds continuously,
265 * detect all CPU-dummy first, and
266 * detect all dummy-Codec second
267 */
268 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
269 cpu_port = it.node;
270 cpu_ep = of_get_next_child(cpu_port, NULL);
271 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
272
273 of_node_put(cpu_port);
274 of_node_put(cpu_ep);
275 of_node_put(codec_ep);
276
277 if (codec) {
278 if (!codec_ep)
279 continue;
280
281 /* Back-End (= Codec) */
282 ret = asoc_graph_card_dai_link_of(codec_ep, priv, daifmt, dai_idx++, 0);
283 if (ret < 0)
284 goto parse_of_err;
285 } else {
286 /* Front-End (= CPU) */
287 ret = asoc_graph_card_dai_link_of(cpu_ep, priv, daifmt, dai_idx++, 1);
288 if (ret < 0)
289 goto parse_of_err;
290 }
291 }
292 }
293
294 ret = asoc_simple_card_parse_card_name(card, NULL);
295 if (ret)
296 goto parse_of_err;
297
298 dev_dbg(dev, "New card: %s\n",
299 card->name ? card->name : "");
300 dev_dbg(dev, "convert_rate %d\n", priv->convert_rate);
301 dev_dbg(dev, "convert_channels %d\n", priv->convert_channels);
302
303 ret = 0;
304
305parse_of_err:
306 return ret;
307}
308
309static int asoc_graph_get_dais_count(struct device *dev)
310{
311 struct of_phandle_iterator it;
312 struct device_node *node = dev->of_node;
313 struct device_node *cpu_port;
314 struct device_node *cpu_ep;
315 struct device_node *codec_ep;
316 int count = 0;
317 int rc;
318
319 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
320 cpu_port = it.node;
321 cpu_ep = of_get_next_child(cpu_port, NULL);
322 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
323
324 of_node_put(cpu_port);
325 of_node_put(cpu_ep);
326 of_node_put(codec_ep);
327
328 if (cpu_ep)
329 count++;
330 if (codec_ep)
331 count++;
332 }
333
334 return count;
335}
336
337static int asoc_graph_card_probe(struct platform_device *pdev)
338{
339 struct graph_card_data *priv;
340 struct snd_soc_dai_link *dai_link;
341 struct asoc_simple_dai *dai_props;
342 struct device *dev = &pdev->dev;
343 struct snd_soc_card *card;
344 int num, ret;
345
346 /* Allocate the private data and the DAI link array */
347 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
348 if (!priv)
349 return -ENOMEM;
350
351 num = asoc_graph_get_dais_count(dev);
352 if (num == 0)
353 return -EINVAL;
354
355 dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
356 dai_link = devm_kzalloc(dev, sizeof(*dai_link) * num, GFP_KERNEL);
357 if (!dai_props || !dai_link)
358 return -ENOMEM;
359
360 priv->dai_props = dai_props;
361 priv->dai_link = dai_link;
362
363 /* Init snd_soc_card */
364 card = graph_priv_to_card(priv);
365 card->owner = THIS_MODULE;
366 card->dev = dev;
367 card->dai_link = priv->dai_link;
368 card->num_links = num;
369 card->codec_conf = &priv->codec_conf;
370 card->num_configs = 1;
371
372 ret = asoc_graph_card_parse_of(priv);
373 if (ret < 0) {
374 if (ret != -EPROBE_DEFER)
375 dev_err(dev, "parse error %d\n", ret);
376 goto err;
377 }
378
379 snd_soc_card_set_drvdata(card, priv);
380
381 ret = devm_snd_soc_register_card(dev, card);
382 if (ret >= 0)
383 return ret;
384err:
385 asoc_simple_card_clean_reference(card);
386
387 return ret;
388}
389
390static int asoc_graph_card_remove(struct platform_device *pdev)
391{
392 struct snd_soc_card *card = platform_get_drvdata(pdev);
393
394 return asoc_simple_card_clean_reference(card);
395}
396
397static const struct of_device_id asoc_graph_of_match[] = {
398 { .compatible = "audio-graph-scu-card", },
399 {},
400};
401MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
402
403static struct platform_driver asoc_graph_card = {
404 .driver = {
405 .name = "asoc-audio-graph-scu-card",
406 .of_match_table = asoc_graph_of_match,
407 },
408 .probe = asoc_graph_card_probe,
409 .remove = asoc_graph_card_remove,
410};
411module_platform_driver(asoc_graph_card);
412
413MODULE_ALIAS("platform:asoc-audio-graph-scu-card");
414MODULE_LICENSE("GPL v2");
415MODULE_DESCRIPTION("ASoC Audio Graph SCU Sound Card");
416MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");