984d8fed0dbd7c4a213d9a8288fd760ebfc7621e
[deliverable/linux.git] / sound / soc / sh / rcar / rsrc-card.c
1 /*
2 * Renesas Sampling Rate Convert Sound Card for DPCM
3 *
4 * Copyright (C) 2015 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * based on ${LINUX}/sound/soc/generic/simple-card.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/string.h>
20 #include <sound/jack.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dai.h>
23 #include <sound/simple_card_utils.h>
24
25 struct rsrc_card_of_data {
26 const char *prefix;
27 const struct snd_soc_dapm_route *routes;
28 int num_routes;
29 };
30
31 static const struct snd_soc_dapm_route routes_ssi0_ak4642[] = {
32 {"ak4642 Playback", NULL, "DAI0 Playback"},
33 {"DAI0 Capture", NULL, "ak4642 Capture"},
34 };
35
36 static const struct rsrc_card_of_data routes_of_ssi0_ak4642 = {
37 .prefix = "ak4642",
38 .routes = routes_ssi0_ak4642,
39 .num_routes = ARRAY_SIZE(routes_ssi0_ak4642),
40 };
41
42 static const struct of_device_id rsrc_card_of_match[] = {
43 { .compatible = "renesas,rsrc-card,lager", .data = &routes_of_ssi0_ak4642 },
44 { .compatible = "renesas,rsrc-card,koelsch", .data = &routes_of_ssi0_ak4642 },
45 { .compatible = "renesas,rsrc-card", },
46 {},
47 };
48 MODULE_DEVICE_TABLE(of, rsrc_card_of_match);
49
50 #define DAI_NAME_NUM 32
51 struct rsrc_card_dai {
52 unsigned int sysclk;
53 unsigned int tx_slot_mask;
54 unsigned int rx_slot_mask;
55 int slots;
56 int slot_width;
57 struct clk *clk;
58 char dai_name[DAI_NAME_NUM];
59 };
60
61 #define IDX_CPU 0
62 #define IDX_CODEC 1
63 struct rsrc_card_priv {
64 struct snd_soc_card snd_card;
65 struct snd_soc_codec_conf codec_conf;
66 struct rsrc_card_dai *dai_props;
67 struct snd_soc_dai_link *dai_link;
68 int dai_num;
69 u32 convert_rate;
70 u32 convert_channels;
71 };
72
73 #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
74 #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
75 #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
76
77 static int rsrc_card_startup(struct snd_pcm_substream *substream)
78 {
79 struct snd_soc_pcm_runtime *rtd = substream->private_data;
80 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
81 struct rsrc_card_dai *dai_props =
82 rsrc_priv_to_props(priv, rtd->num);
83
84 return clk_prepare_enable(dai_props->clk);
85 }
86
87 static void rsrc_card_shutdown(struct snd_pcm_substream *substream)
88 {
89 struct snd_soc_pcm_runtime *rtd = substream->private_data;
90 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
91 struct rsrc_card_dai *dai_props =
92 rsrc_priv_to_props(priv, rtd->num);
93
94 clk_disable_unprepare(dai_props->clk);
95 }
96
97 static struct snd_soc_ops rsrc_card_ops = {
98 .startup = rsrc_card_startup,
99 .shutdown = rsrc_card_shutdown,
100 };
101
102 static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd)
103 {
104 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
105 struct snd_soc_dai *dai;
106 struct snd_soc_dai_link *dai_link;
107 struct rsrc_card_dai *dai_props;
108 int num = rtd->num;
109 int ret;
110
111 dai_link = rsrc_priv_to_link(priv, num);
112 dai_props = rsrc_priv_to_props(priv, num);
113 dai = dai_link->dynamic ?
114 rtd->cpu_dai :
115 rtd->codec_dai;
116
117 if (dai_props->sysclk) {
118 ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0);
119 if (ret && ret != -ENOTSUPP) {
120 dev_err(dai->dev, "set_sysclk error\n");
121 goto err;
122 }
123 }
124
125 if (dai_props->slots) {
126 ret = snd_soc_dai_set_tdm_slot(dai,
127 dai_props->tx_slot_mask,
128 dai_props->rx_slot_mask,
129 dai_props->slots,
130 dai_props->slot_width);
131 if (ret && ret != -ENOTSUPP) {
132 dev_err(dai->dev, "set_tdm_slot error\n");
133 goto err;
134 }
135 }
136
137 ret = 0;
138
139 err:
140 return ret;
141 }
142
143 static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
144 struct snd_pcm_hw_params *params)
145 {
146 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
147 struct snd_interval *rate = hw_param_interval(params,
148 SNDRV_PCM_HW_PARAM_RATE);
149 struct snd_interval *channels = hw_param_interval(params,
150 SNDRV_PCM_HW_PARAM_CHANNELS);
151
152 if (priv->convert_rate)
153 rate->min =
154 rate->max = priv->convert_rate;
155
156 if (priv->convert_channels)
157 channels->min =
158 channels->max = priv->convert_channels;
159
160 return 0;
161 }
162
163 static int rsrc_card_parse_links(struct device_node *np,
164 struct rsrc_card_priv *priv,
165 int idx, bool is_fe)
166 {
167 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
168 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
169 struct of_phandle_args args;
170 int ret;
171
172 /*
173 * Get node via "sound-dai = <&phandle port>"
174 * it will be used as xxx_of_node on soc_bind_dai_link()
175 */
176 ret = of_parse_phandle_with_args(np, "sound-dai",
177 "#sound-dai-cells", 0, &args);
178 if (ret)
179 return ret;
180
181 /* Parse TDM slot */
182 ret = snd_soc_of_parse_tdm_slot(np,
183 &dai_props->tx_slot_mask,
184 &dai_props->rx_slot_mask,
185 &dai_props->slots,
186 &dai_props->slot_width);
187 if (ret)
188 return ret;
189
190 if (is_fe) {
191 /* BE is dummy */
192 dai_link->codec_of_node = NULL;
193 dai_link->codec_dai_name = "snd-soc-dummy-dai";
194 dai_link->codec_name = "snd-soc-dummy";
195
196 /* FE settings */
197 dai_link->dynamic = 1;
198 dai_link->dpcm_merged_format = 1;
199 dai_link->cpu_of_node = args.np;
200 ret = snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
201 if (ret < 0)
202 return ret;
203
204 /* set dai_name */
205 snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
206 dai_link->cpu_dai_name);
207
208 /*
209 * In soc_bind_dai_link() will check cpu name after
210 * of_node matching if dai_link has cpu_dai_name.
211 * but, it will never match if name was created by
212 * fmt_single_name() remove cpu_dai_name if cpu_args
213 * was 0. See:
214 * fmt_single_name()
215 * fmt_multiple_name()
216 */
217 if (!args.args_count)
218 dai_link->cpu_dai_name = NULL;
219 } else {
220 struct device *dev = rsrc_priv_to_dev(priv);
221 const struct rsrc_card_of_data *of_data;
222
223 of_data = of_device_get_match_data(dev);
224
225 /* FE is dummy */
226 dai_link->cpu_of_node = NULL;
227 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
228 dai_link->cpu_name = "snd-soc-dummy";
229
230 /* BE settings */
231 dai_link->no_pcm = 1;
232 dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup;
233 dai_link->codec_of_node = args.np;
234 ret = snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
235 if (ret < 0)
236 return ret;
237
238 /* additional name prefix */
239 if (of_data) {
240 priv->codec_conf.of_node = dai_link->codec_of_node;
241 priv->codec_conf.name_prefix = of_data->prefix;
242 } else {
243 snd_soc_of_parse_audio_prefix(&priv->snd_card,
244 &priv->codec_conf,
245 dai_link->codec_of_node,
246 "audio-prefix");
247 }
248
249 /* set dai_name */
250 snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
251 dai_link->codec_dai_name);
252 }
253
254 /* Simple Card assumes platform == cpu */
255 dai_link->platform_of_node = dai_link->cpu_of_node;
256 dai_link->dpcm_playback = 1;
257 dai_link->dpcm_capture = 1;
258 dai_link->name = dai_props->dai_name;
259 dai_link->stream_name = dai_props->dai_name;
260 dai_link->ops = &rsrc_card_ops;
261 dai_link->init = rsrc_card_dai_init;
262
263 return 0;
264 }
265
266 static int rsrc_card_parse_clk(struct device_node *np,
267 struct rsrc_card_priv *priv,
268 int idx, bool is_fe)
269 {
270 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
271 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
272 struct clk *clk;
273 struct device_node *of_np = is_fe ? dai_link->cpu_of_node :
274 dai_link->codec_of_node;
275 u32 val;
276
277 /*
278 * Parse dai->sysclk come from "clocks = <&xxx>"
279 * (if system has common clock)
280 * or "system-clock-frequency = <xxx>"
281 * or device's module clock.
282 */
283 if (of_property_read_bool(np, "clocks")) {
284 clk = of_clk_get(np, 0);
285 if (IS_ERR(clk))
286 return PTR_ERR(clk);
287
288 dai_props->sysclk = clk_get_rate(clk);
289 dai_props->clk = clk;
290 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
291 dai_props->sysclk = val;
292 } else {
293 clk = of_clk_get(of_np, 0);
294 if (!IS_ERR(clk))
295 dai_props->sysclk = clk_get_rate(clk);
296 }
297
298 return 0;
299 }
300
301 static int rsrc_card_dai_sub_link_of(struct device_node *node,
302 struct device_node *np,
303 struct rsrc_card_priv *priv,
304 int idx, bool is_fe)
305 {
306 struct device *dev = rsrc_priv_to_dev(priv);
307 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
308 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
309 int ret;
310
311 ret = rsrc_card_parse_links(np, priv, idx, is_fe);
312 if (ret < 0)
313 return ret;
314
315 ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
316 if (ret < 0)
317 return ret;
318
319 dev_dbg(dev, "\t%s / %04x / %d\n",
320 dai_props->dai_name,
321 dai_link->dai_fmt,
322 dai_props->sysclk);
323
324 return ret;
325 }
326
327 static int rsrc_card_dai_link_of(struct device_node *node,
328 struct rsrc_card_priv *priv)
329 {
330 struct device *dev = rsrc_priv_to_dev(priv);
331 struct snd_soc_dai_link *dai_link;
332 struct device_node *np;
333 unsigned int daifmt = 0;
334 int ret, i;
335 bool is_fe;
336
337 /* find 1st codec */
338 i = 0;
339 for_each_child_of_node(node, np) {
340 dai_link = rsrc_priv_to_link(priv, i);
341
342 if (strcmp(np->name, "codec") == 0) {
343 ret = asoc_simple_card_parse_daifmt(dev, node, np,
344 NULL, &daifmt);
345 if (ret < 0)
346 return ret;
347 break;
348 }
349 i++;
350 }
351
352 i = 0;
353 for_each_child_of_node(node, np) {
354 dai_link = rsrc_priv_to_link(priv, i);
355 dai_link->dai_fmt = daifmt;
356
357 is_fe = false;
358 if (strcmp(np->name, "cpu") == 0)
359 is_fe = true;
360
361 ret = rsrc_card_dai_sub_link_of(node, np, priv, i, is_fe);
362 if (ret < 0)
363 return ret;
364 i++;
365 }
366
367 return 0;
368 }
369
370 static int rsrc_card_parse_of(struct device_node *node,
371 struct rsrc_card_priv *priv,
372 struct device *dev)
373 {
374 const struct rsrc_card_of_data *of_data = of_device_get_match_data(dev);
375 struct rsrc_card_dai *props;
376 struct snd_soc_dai_link *links;
377 int ret;
378 int num;
379
380 if (!node)
381 return -EINVAL;
382
383 num = of_get_child_count(node);
384 props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
385 links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
386 if (!props || !links)
387 return -ENOMEM;
388
389 priv->dai_props = props;
390 priv->dai_link = links;
391 priv->dai_num = num;
392
393 /* Init snd_soc_card */
394 priv->snd_card.owner = THIS_MODULE;
395 priv->snd_card.dev = dev;
396 priv->snd_card.dai_link = priv->dai_link;
397 priv->snd_card.num_links = num;
398 priv->snd_card.codec_conf = &priv->codec_conf;
399 priv->snd_card.num_configs = 1;
400
401 if (of_data) {
402 priv->snd_card.of_dapm_routes = of_data->routes;
403 priv->snd_card.num_of_dapm_routes = of_data->num_routes;
404 } else {
405 snd_soc_of_parse_audio_routing(&priv->snd_card,
406 "audio-routing");
407 }
408
409 /* Parse the card name from DT */
410 snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
411
412 /* sampling rate convert */
413 of_property_read_u32(node, "convert-rate", &priv->convert_rate);
414
415 /* channels transfer */
416 of_property_read_u32(node, "convert-channels", &priv->convert_channels);
417
418 dev_dbg(dev, "New rsrc-audio-card: %s\n",
419 priv->snd_card.name ? priv->snd_card.name : "");
420 dev_dbg(dev, "SRC : convert_rate %d\n", priv->convert_rate);
421 dev_dbg(dev, "CTU : convert_channels %d\n", priv->convert_channels);
422
423 ret = rsrc_card_dai_link_of(node, priv);
424 if (ret < 0)
425 return ret;
426
427 if (!priv->snd_card.name)
428 priv->snd_card.name = priv->snd_card.dai_link->name;
429
430 return 0;
431 }
432
433 /* Decrease the reference count of the device nodes */
434 static int rsrc_card_unref(struct snd_soc_card *card)
435 {
436 struct snd_soc_dai_link *dai_link;
437 int num_links;
438
439 for (num_links = 0, dai_link = card->dai_link;
440 num_links < card->num_links;
441 num_links++, dai_link++) {
442 of_node_put(dai_link->cpu_of_node);
443 of_node_put(dai_link->codec_of_node);
444 }
445 return 0;
446 }
447
448 static int rsrc_card_probe(struct platform_device *pdev)
449 {
450 struct rsrc_card_priv *priv;
451 struct device_node *np = pdev->dev.of_node;
452 struct device *dev = &pdev->dev;
453 int ret;
454
455 /* Allocate the private data */
456 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
457 if (!priv)
458 return -ENOMEM;
459
460 ret = rsrc_card_parse_of(np, priv, dev);
461 if (ret < 0) {
462 if (ret != -EPROBE_DEFER)
463 dev_err(dev, "parse error %d\n", ret);
464 goto err;
465 }
466
467 snd_soc_card_set_drvdata(&priv->snd_card, priv);
468
469 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
470 if (ret >= 0)
471 return ret;
472 err:
473 rsrc_card_unref(&priv->snd_card);
474
475 return ret;
476 }
477
478 static int rsrc_card_remove(struct platform_device *pdev)
479 {
480 struct snd_soc_card *card = platform_get_drvdata(pdev);
481
482 return rsrc_card_unref(card);
483 }
484
485 static struct platform_driver rsrc_card = {
486 .driver = {
487 .name = "renesas-src-audio-card",
488 .of_match_table = rsrc_card_of_match,
489 },
490 .probe = rsrc_card_probe,
491 .remove = rsrc_card_remove,
492 };
493
494 module_platform_driver(rsrc_card);
495
496 MODULE_ALIAS("platform:renesas-src-audio-card");
497 MODULE_LICENSE("GPL");
498 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
499 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.048961 seconds and 5 git commands to generate.