Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux] / sound / soc / generic / audio-graph-card.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ASoC audio graph sound card support
4 //
5 // Copyright (C) 2016 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // based on ${LINUX}/sound/soc/generic/simple-card.c
9
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/string.h>
21 #include <sound/simple_card_utils.h>
22
23 struct graph_card_data {
24         struct snd_soc_card snd_card;
25         struct graph_dai_props {
26                 struct asoc_simple_dai cpu_dai;
27                 struct asoc_simple_dai codec_dai;
28                 struct snd_soc_dai_link_component codecs; /* single codec */
29                 struct snd_soc_dai_link_component platform;
30                 unsigned int mclk_fs;
31         } *dai_props;
32         unsigned int mclk_fs;
33         struct asoc_simple_jack hp_jack;
34         struct asoc_simple_jack mic_jack;
35         struct snd_soc_dai_link *dai_link;
36         struct gpio_desc *pa_gpio;
37 };
38
39 static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
40                                         struct snd_kcontrol *kcontrol,
41                                         int event)
42 {
43         struct snd_soc_dapm_context *dapm = w->dapm;
44         struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
45
46         switch (event) {
47         case SND_SOC_DAPM_POST_PMU:
48                 gpiod_set_value_cansleep(priv->pa_gpio, 1);
49                 break;
50         case SND_SOC_DAPM_PRE_PMD:
51                 gpiod_set_value_cansleep(priv->pa_gpio, 0);
52                 break;
53         default:
54                 return -EINVAL;
55         }
56
57         return 0;
58 }
59
60 static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
61         SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
62                                0, 0, NULL, 0, asoc_graph_card_outdrv_event,
63                                SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
64 };
65
66 #define graph_priv_to_card(priv) (&(priv)->snd_card)
67 #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
68 #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
69 #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
70
71 static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
72 {
73         struct snd_soc_pcm_runtime *rtd = substream->private_data;
74         struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
75         struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
76         int ret;
77
78         ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
79         if (ret)
80                 return ret;
81
82         ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
83         if (ret)
84                 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
85
86         return ret;
87 }
88
89 static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
90 {
91         struct snd_soc_pcm_runtime *rtd = substream->private_data;
92         struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
93         struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
94
95         asoc_simple_card_clk_disable(&dai_props->cpu_dai);
96
97         asoc_simple_card_clk_disable(&dai_props->codec_dai);
98 }
99
100 static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
101                                      struct snd_pcm_hw_params *params)
102 {
103         struct snd_soc_pcm_runtime *rtd = substream->private_data;
104         struct snd_soc_dai *codec_dai = rtd->codec_dai;
105         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
106         struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
107         struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
108         unsigned int mclk, mclk_fs = 0;
109         int ret = 0;
110
111         if (priv->mclk_fs)
112                 mclk_fs = priv->mclk_fs;
113         else if (dai_props->mclk_fs)
114                 mclk_fs = dai_props->mclk_fs;
115
116         if (mclk_fs) {
117                 mclk = params_rate(params) * mclk_fs;
118                 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
119                                              SND_SOC_CLOCK_IN);
120                 if (ret && ret != -ENOTSUPP)
121                         goto err;
122
123                 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
124                                              SND_SOC_CLOCK_OUT);
125                 if (ret && ret != -ENOTSUPP)
126                         goto err;
127         }
128         return 0;
129 err:
130         return ret;
131 }
132
133 static const struct snd_soc_ops asoc_graph_card_ops = {
134         .startup = asoc_graph_card_startup,
135         .shutdown = asoc_graph_card_shutdown,
136         .hw_params = asoc_graph_card_hw_params,
137 };
138
139 static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
140 {
141         struct graph_card_data *priv =  snd_soc_card_get_drvdata(rtd->card);
142         struct snd_soc_dai *codec = rtd->codec_dai;
143         struct snd_soc_dai *cpu = rtd->cpu_dai;
144         struct graph_dai_props *dai_props =
145                 graph_priv_to_props(priv, rtd->num);
146         int ret;
147
148         ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
149         if (ret < 0)
150                 return ret;
151
152         ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
153         if (ret < 0)
154                 return ret;
155
156         return 0;
157 }
158
159 static int asoc_graph_card_dai_link_of(struct device_node *cpu_port,
160                                         struct graph_card_data *priv,
161                                         int idx)
162 {
163         struct device *dev = graph_priv_to_dev(priv);
164         struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
165         struct graph_dai_props *dai_props = graph_priv_to_props(priv, idx);
166         struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
167         struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
168         struct device_node *cpu_ep    = of_get_next_child(cpu_port, NULL);
169         struct device_node *codec_ep = of_graph_get_remote_endpoint(cpu_ep);
170         struct device_node *rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
171         int ret;
172
173         if (rcpu_ep != cpu_ep) {
174                 dev_err(dev, "remote-endpoint mismatch (%s/%s/%s)\n",
175                         cpu_ep->name, codec_ep->name, rcpu_ep->name);
176                 ret = -EINVAL;
177                 goto dai_link_of_err;
178         }
179
180         ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
181                                             NULL, &dai_link->dai_fmt);
182         if (ret < 0)
183                 goto dai_link_of_err;
184
185         of_property_read_u32(cpu_ep,   "mclk-fs", &dai_props->mclk_fs);
186         of_property_read_u32(codec_ep, "mclk-fs", &dai_props->mclk_fs);
187
188         ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
189         if (ret < 0)
190                 goto dai_link_of_err;
191
192         ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
193         if (ret < 0)
194                 goto dai_link_of_err;
195
196         ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
197         if (ret < 0)
198                 goto dai_link_of_err;
199
200         ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
201         if (ret < 0)
202                 goto dai_link_of_err;
203
204         ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
205         if (ret < 0)
206                 goto dai_link_of_err;
207
208         ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
209         if (ret < 0)
210                 goto dai_link_of_err;
211
212         ret = asoc_simple_card_canonicalize_dailink(dai_link);
213         if (ret < 0)
214                 goto dai_link_of_err;
215
216         ret = asoc_simple_card_set_dailink_name(dev, dai_link,
217                                                 "%s-%s",
218                                                 dai_link->cpu_dai_name,
219                                                 dai_link->codecs->dai_name);
220         if (ret < 0)
221                 goto dai_link_of_err;
222
223         dai_link->ops = &asoc_graph_card_ops;
224         dai_link->init = asoc_graph_card_dai_init;
225
226         asoc_simple_card_canonicalize_cpu(dai_link,
227                 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
228
229 dai_link_of_err:
230         of_node_put(cpu_ep);
231         of_node_put(rcpu_ep);
232         of_node_put(codec_ep);
233
234         return ret;
235 }
236
237 static int asoc_graph_card_parse_of(struct graph_card_data *priv)
238 {
239         struct of_phandle_iterator it;
240         struct device *dev = graph_priv_to_dev(priv);
241         struct snd_soc_card *card = graph_priv_to_card(priv);
242         struct device_node *node = dev->of_node;
243         int rc, idx = 0;
244         int ret;
245
246         ret = asoc_simple_card_of_parse_widgets(card, NULL);
247         if (ret < 0)
248                 return ret;
249
250         ret = asoc_simple_card_of_parse_routing(card, NULL, 1);
251         if (ret < 0)
252                 return ret;
253
254         /* Factor to mclk, used in hw_params() */
255         of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
256
257         of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
258                 ret = asoc_graph_card_dai_link_of(it.node, priv, idx++);
259                 if (ret < 0) {
260                         of_node_put(it.node);
261
262                         return ret;
263                 }
264         }
265
266         return asoc_simple_card_parse_card_name(card, NULL);
267 }
268
269 static int asoc_graph_get_dais_count(struct device *dev)
270 {
271         struct of_phandle_iterator it;
272         struct device_node *node = dev->of_node;
273         int count = 0;
274         int rc;
275
276         of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
277                 count++;
278
279         return count;
280 }
281
282 static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
283 {
284         struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
285         int ret;
286
287         ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
288         if (ret < 0)
289                 return ret;
290
291         ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
292         if (ret < 0)
293                 return ret;
294
295         return 0;
296 }
297
298 static int asoc_graph_card_probe(struct platform_device *pdev)
299 {
300         struct graph_card_data *priv;
301         struct snd_soc_dai_link *dai_link;
302         struct graph_dai_props *dai_props;
303         struct device *dev = &pdev->dev;
304         struct snd_soc_card *card;
305         int num, ret, i;
306
307         /* Allocate the private data and the DAI link array */
308         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
309         if (!priv)
310                 return -ENOMEM;
311
312         num = asoc_graph_get_dais_count(dev);
313         if (num == 0)
314                 return -EINVAL;
315
316         dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
317         dai_link  = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
318         if (!dai_props || !dai_link)
319                 return -ENOMEM;
320
321         /*
322          * Use snd_soc_dai_link_component instead of legacy style
323          * It is codec only. but cpu/platform will be supported in the future.
324          * see
325          *      soc-core.c :: snd_soc_init_multicodec()
326          */
327         for (i = 0; i < num; i++) {
328                 dai_link[i].codecs      = &dai_props[i].codecs;
329                 dai_link[i].num_codecs  = 1;
330                 dai_link[i].platform    = &dai_props[i].platform;
331         }
332
333         priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
334         if (IS_ERR(priv->pa_gpio)) {
335                 ret = PTR_ERR(priv->pa_gpio);
336                 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
337                 return ret;
338         }
339
340         priv->dai_props                 = dai_props;
341         priv->dai_link                  = dai_link;
342
343         /* Init snd_soc_card */
344         card = graph_priv_to_card(priv);
345         card->owner     = THIS_MODULE;
346         card->dev       = dev;
347         card->dai_link  = dai_link;
348         card->num_links = num;
349         card->dapm_widgets = asoc_graph_card_dapm_widgets;
350         card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
351         card->probe     = asoc_graph_soc_card_probe;
352
353         ret = asoc_graph_card_parse_of(priv);
354         if (ret < 0) {
355                 if (ret != -EPROBE_DEFER)
356                         dev_err(dev, "parse error %d\n", ret);
357                 goto err;
358         }
359
360         snd_soc_card_set_drvdata(card, priv);
361
362         ret = devm_snd_soc_register_card(dev, card);
363         if (ret < 0)
364                 goto err;
365
366         return 0;
367 err:
368         asoc_simple_card_clean_reference(card);
369
370         return ret;
371 }
372
373 static int asoc_graph_card_remove(struct platform_device *pdev)
374 {
375         struct snd_soc_card *card = platform_get_drvdata(pdev);
376
377         return asoc_simple_card_clean_reference(card);
378 }
379
380 static const struct of_device_id asoc_graph_of_match[] = {
381         { .compatible = "audio-graph-card", },
382         {},
383 };
384 MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
385
386 static struct platform_driver asoc_graph_card = {
387         .driver = {
388                 .name = "asoc-audio-graph-card",
389                 .pm = &snd_soc_pm_ops,
390                 .of_match_table = asoc_graph_of_match,
391         },
392         .probe = asoc_graph_card_probe,
393         .remove = asoc_graph_card_remove,
394 };
395 module_platform_driver(asoc_graph_card);
396
397 MODULE_ALIAS("platform:asoc-audio-graph-card");
398 MODULE_LICENSE("GPL v2");
399 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
400 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");