ASoC: simple-scu-card: add new simple_priv_to_card() macro
[muen/linux.git] / sound / soc / generic / simple-scu-card.c
1 /*
2  * ASoC simple SCU sound card support
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 simple_card_data {
26         struct snd_soc_card snd_card;
27         struct snd_soc_codec_conf codec_conf;
28         struct asoc_simple_dai *dai_props;
29         struct snd_soc_dai_link *dai_link;
30         u32 convert_rate;
31         u32 convert_channels;
32 };
33
34 #define simple_priv_to_card(priv) (&(priv)->snd_card)
35 #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
36 #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
37 #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
38
39 #define DAI     "sound-dai"
40 #define CELL    "#sound-dai-cells"
41 #define PREFIX  "simple-audio-card,"
42
43 static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
44 {
45         struct snd_soc_pcm_runtime *rtd = substream->private_data;
46         struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
47         struct asoc_simple_dai *dai_props =
48                 simple_priv_to_props(priv, rtd->num);
49
50         return clk_prepare_enable(dai_props->clk);
51 }
52
53 static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
54 {
55         struct snd_soc_pcm_runtime *rtd = substream->private_data;
56         struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
57         struct asoc_simple_dai *dai_props =
58                 simple_priv_to_props(priv, rtd->num);
59
60         clk_disable_unprepare(dai_props->clk);
61 }
62
63 static const struct snd_soc_ops asoc_simple_card_ops = {
64         .startup = asoc_simple_card_startup,
65         .shutdown = asoc_simple_card_shutdown,
66 };
67
68 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
69 {
70         struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
71         struct snd_soc_dai *dai;
72         struct snd_soc_dai_link *dai_link;
73         struct asoc_simple_dai *dai_props;
74         int num = rtd->num;
75
76         dai_link        = simple_priv_to_link(priv, num);
77         dai_props       = simple_priv_to_props(priv, num);
78         dai             = dai_link->dynamic ?
79                                 rtd->cpu_dai :
80                                 rtd->codec_dai;
81
82         return asoc_simple_card_init_dai(dai, dai_props);
83 }
84
85 static int asoc_simple_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
86                                         struct snd_pcm_hw_params *params)
87 {
88         struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
89         struct snd_interval *rate = hw_param_interval(params,
90                                                       SNDRV_PCM_HW_PARAM_RATE);
91         struct snd_interval *channels = hw_param_interval(params,
92                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
93
94         if (priv->convert_rate)
95                 rate->min =
96                 rate->max = priv->convert_rate;
97
98         if (priv->convert_channels)
99                 channels->min =
100                 channels->max = priv->convert_channels;
101
102         return 0;
103 }
104
105 static int asoc_simple_card_dai_link_of(struct device_node *np,
106                                         struct simple_card_data *priv,
107                                         unsigned int daifmt,
108                                         int idx, bool is_fe)
109 {
110         struct device *dev = simple_priv_to_dev(priv);
111         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
112         struct asoc_simple_dai *dai_props = simple_priv_to_props(priv, idx);
113         struct snd_soc_card *card = simple_priv_to_card(priv);
114         int ret;
115
116         if (is_fe) {
117                 int is_single_links = 0;
118
119                 /* BE is dummy */
120                 dai_link->codec_of_node         = NULL;
121                 dai_link->codec_dai_name        = "snd-soc-dummy-dai";
122                 dai_link->codec_name            = "snd-soc-dummy";
123
124                 /* FE settings */
125                 dai_link->dynamic               = 1;
126                 dai_link->dpcm_merged_format    = 1;
127
128                 ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL,
129                                                  &is_single_links);
130                 if (ret)
131                         return ret;
132
133                 ret = asoc_simple_card_parse_clk_cpu(np, dai_link, dai_props);
134                 if (ret < 0)
135                         return ret;
136
137                 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
138                                                         "fe.%s",
139                                                         dai_link->cpu_dai_name);
140                 if (ret < 0)
141                         return ret;
142
143                 asoc_simple_card_canonicalize_cpu(dai_link, is_single_links);
144         } else {
145                 /* FE is dummy */
146                 dai_link->cpu_of_node           = NULL;
147                 dai_link->cpu_dai_name          = "snd-soc-dummy-dai";
148                 dai_link->cpu_name              = "snd-soc-dummy";
149
150                 /* BE settings */
151                 dai_link->no_pcm                = 1;
152                 dai_link->be_hw_params_fixup    = asoc_simple_card_be_hw_params_fixup;
153
154                 ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL);
155                 if (ret < 0)
156                         return ret;
157
158                 ret = asoc_simple_card_parse_clk_codec(np, dai_link, dai_props);
159                 if (ret < 0)
160                         return ret;
161
162                 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
163                                                         "be.%s",
164                                                         dai_link->codec_dai_name);
165                 if (ret < 0)
166                         return ret;
167
168                 snd_soc_of_parse_audio_prefix(card,
169                                               &priv->codec_conf,
170                                               dai_link->codec_of_node,
171                                               PREFIX "prefix");
172         }
173
174         ret = snd_soc_of_parse_tdm_slot(np,
175                                         &dai_props->tx_slot_mask,
176                                         &dai_props->rx_slot_mask,
177                                         &dai_props->slots,
178                                         &dai_props->slot_width);
179         if (ret)
180                 return ret;
181
182         ret = asoc_simple_card_canonicalize_dailink(dai_link);
183         if (ret < 0)
184                 return ret;
185
186         dai_link->dai_fmt               = daifmt;
187         dai_link->dpcm_playback         = 1;
188         dai_link->dpcm_capture          = 1;
189         dai_link->ops                   = &asoc_simple_card_ops;
190         dai_link->init                  = asoc_simple_card_dai_init;
191
192         dev_dbg(dev, "\t%s / %04x / %d\n",
193                 dai_link->name,
194                 dai_link->dai_fmt,
195                 dai_props->sysclk);
196
197         return 0;
198 }
199
200 static int asoc_simple_card_parse_of(struct device_node *node,
201                                      struct simple_card_data *priv)
202
203 {
204         struct device *dev = simple_priv_to_dev(priv);
205         struct device_node *np;
206         struct snd_soc_card *card = simple_priv_to_card(priv);
207         unsigned int daifmt = 0;
208         bool is_fe;
209         int ret, i;
210
211         if (!node)
212                 return -EINVAL;
213
214         ret = snd_soc_of_parse_audio_routing(card, PREFIX "routing");
215         if (ret < 0)
216                 return ret;
217
218         /* sampling rate convert */
219         of_property_read_u32(node, PREFIX "convert-rate", &priv->convert_rate);
220
221         /* channels transfer */
222         of_property_read_u32(node, PREFIX "convert-channels", &priv->convert_channels);
223
224         /* find 1st codec */
225         np = of_get_child_by_name(node, PREFIX "codec");
226         if (!np)
227                 return -ENODEV;
228
229         ret = asoc_simple_card_parse_daifmt(dev, node, np, PREFIX, &daifmt);
230         if (ret < 0)
231                 return ret;
232
233         i = 0;
234         for_each_child_of_node(node, np) {
235                 is_fe = false;
236                 if (strcmp(np->name, PREFIX "cpu") == 0)
237                         is_fe = true;
238
239                 ret = asoc_simple_card_dai_link_of(np, priv, daifmt, i, is_fe);
240                 if (ret < 0)
241                         return ret;
242                 i++;
243         }
244
245         ret = asoc_simple_card_parse_card_name(card, PREFIX);
246         if (ret < 0)
247                 return ret;
248
249         dev_dbg(dev, "New card: %s\n",
250                 card->name ? card->name : "");
251         dev_dbg(dev, "convert_rate     %d\n", priv->convert_rate);
252         dev_dbg(dev, "convert_channels %d\n", priv->convert_channels);
253
254         return 0;
255 }
256
257 static int asoc_simple_card_probe(struct platform_device *pdev)
258 {
259         struct simple_card_data *priv;
260         struct snd_soc_dai_link *dai_link;
261         struct asoc_simple_dai *dai_props;
262         struct snd_soc_card *card;
263         struct device *dev = &pdev->dev;
264         struct device_node *np = dev->of_node;
265         int num, ret;
266
267         /* Allocate the private data */
268         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
269         if (!priv)
270                 return -ENOMEM;
271
272         num = of_get_child_count(np);
273
274         dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
275         dai_link  = devm_kzalloc(dev, sizeof(*dai_link)  * num, GFP_KERNEL);
276         if (!dai_props || !dai_link)
277                 return -ENOMEM;
278
279         priv->dai_props                         = dai_props;
280         priv->dai_link                          = dai_link;
281
282         /* Init snd_soc_card */
283         card = simple_priv_to_card(priv);
284         card->owner             = THIS_MODULE;
285         card->dev               = dev;
286         card->dai_link          = priv->dai_link;
287         card->num_links         = num;
288         card->codec_conf        = &priv->codec_conf;
289         card->num_configs       = 1;
290
291         ret = asoc_simple_card_parse_of(np, priv);
292         if (ret < 0) {
293                 if (ret != -EPROBE_DEFER)
294                         dev_err(dev, "parse error %d\n", ret);
295                 goto err;
296         }
297
298         snd_soc_card_set_drvdata(card, priv);
299
300         ret = devm_snd_soc_register_card(dev, card);
301         if (ret >= 0)
302                 return ret;
303 err:
304         asoc_simple_card_clean_reference(card);
305
306         return ret;
307 }
308
309 static int asoc_simple_card_remove(struct platform_device *pdev)
310 {
311         struct snd_soc_card *card = platform_get_drvdata(pdev);
312
313         return asoc_simple_card_clean_reference(card);
314 }
315
316 static const struct of_device_id asoc_simple_of_match[] = {
317         { .compatible = "renesas,rsrc-card", },
318         { .compatible = "simple-scu-audio-card", },
319         {},
320 };
321 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
322
323 static struct platform_driver asoc_simple_card = {
324         .driver = {
325                 .name = "simple-scu-audio-card",
326                 .of_match_table = asoc_simple_of_match,
327         },
328         .probe = asoc_simple_card_probe,
329         .remove = asoc_simple_card_remove,
330 };
331
332 module_platform_driver(asoc_simple_card);
333
334 MODULE_ALIAS("platform:asoc-simple-scu-card");
335 MODULE_LICENSE("GPL v2");
336 MODULE_DESCRIPTION("ASoC Simple SCU Sound Card");
337 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");