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