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