blob: 1da9532ef897ae472178c4f7e25bb5bfa8beee46 [file] [log] [blame]
Kuninori Morimotodecd8962018-07-02 06:31:16 +00001// 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
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000010#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
Shawn Guof9869072017-06-29 21:26:38 +080013#include <linux/gpio/consumer.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000014#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>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000021#include <sound/simple_card_utils.h>
22
23struct graph_card_data {
24 struct snd_soc_card snd_card;
25 struct graph_dai_props {
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000026 struct asoc_simple_dai *cpu_dai;
27 struct asoc_simple_dai *codec_dai;
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +000028 struct snd_soc_dai_link_component codecs; /* single codec */
Kuninori Morimoto46c731872018-08-31 03:10:58 +000029 struct snd_soc_dai_link_component platform;
Olivier Moysan757652d2017-11-09 15:07:58 +010030 unsigned int mclk_fs;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000031 } *dai_props;
Olivier Moysan757652d2017-11-09 15:07:58 +010032 unsigned int mclk_fs;
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +090033 struct asoc_simple_jack hp_jack;
34 struct asoc_simple_jack mic_jack;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000035 struct snd_soc_dai_link *dai_link;
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000036 struct asoc_simple_dai *dais;
Shawn Guof9869072017-06-29 21:26:38 +080037 struct gpio_desc *pa_gpio;
38};
39
Kuninori Morimoto64ef0812018-11-22 00:57:40 +000040#define graph_priv_to_card(priv) (&(priv)->snd_card)
41#define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
42#define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
43#define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
44
Shawn Guof9869072017-06-29 21:26:38 +080045static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
46 struct snd_kcontrol *kcontrol,
47 int event)
48{
49 struct snd_soc_dapm_context *dapm = w->dapm;
50 struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
51
52 switch (event) {
53 case SND_SOC_DAPM_POST_PMU:
54 gpiod_set_value_cansleep(priv->pa_gpio, 1);
55 break;
56 case SND_SOC_DAPM_PRE_PMD:
57 gpiod_set_value_cansleep(priv->pa_gpio, 0);
58 break;
59 default:
60 return -EINVAL;
61 }
62
63 return 0;
64}
65
66static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
67 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
68 0, 0, NULL, 0, asoc_graph_card_outdrv_event,
69 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000070};
71
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000072static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
73{
74 struct snd_soc_pcm_runtime *rtd = substream->private_data;
75 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
76 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
77 int ret;
78
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000079 ret = asoc_simple_card_clk_enable(dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000080 if (ret)
81 return ret;
82
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000083 ret = asoc_simple_card_clk_enable(dai_props->codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000084 if (ret)
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000085 asoc_simple_card_clk_disable(dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000086
87 return ret;
88}
89
90static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
91{
92 struct snd_soc_pcm_runtime *rtd = substream->private_data;
93 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
94 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
95
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000096 asoc_simple_card_clk_disable(dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000097
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +000098 asoc_simple_card_clk_disable(dai_props->codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000099}
100
Olivier Moysan757652d2017-11-09 15:07:58 +0100101static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
102 struct snd_pcm_hw_params *params)
103{
104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
105 struct snd_soc_dai *codec_dai = rtd->codec_dai;
106 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
107 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
108 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
109 unsigned int mclk, mclk_fs = 0;
110 int ret = 0;
111
112 if (priv->mclk_fs)
113 mclk_fs = priv->mclk_fs;
114 else if (dai_props->mclk_fs)
115 mclk_fs = dai_props->mclk_fs;
116
117 if (mclk_fs) {
118 mclk = params_rate(params) * mclk_fs;
119 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
120 SND_SOC_CLOCK_IN);
121 if (ret && ret != -ENOTSUPP)
122 goto err;
123
124 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
125 SND_SOC_CLOCK_OUT);
126 if (ret && ret != -ENOTSUPP)
127 goto err;
128 }
129 return 0;
130err:
131 return ret;
132}
133
Bhumika Goyale23d8342017-08-16 22:29:26 +0530134static const struct snd_soc_ops asoc_graph_card_ops = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000135 .startup = asoc_graph_card_startup,
136 .shutdown = asoc_graph_card_shutdown,
Olivier Moysan757652d2017-11-09 15:07:58 +0100137 .hw_params = asoc_graph_card_hw_params,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000138};
139
140static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
141{
142 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000143 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
144 int ret = 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000145
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000146 ret = asoc_simple_card_init_dai(rtd->codec_dai,
147 dai_props->codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000148 if (ret < 0)
149 return ret;
150
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000151 ret = asoc_simple_card_init_dai(rtd->cpu_dai,
152 dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000153 if (ret < 0)
154 return ret;
155
156 return 0;
157}
158
159static int asoc_graph_card_dai_link_of(struct device_node *cpu_port,
160 struct graph_card_data *priv,
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000161 int *dai_idx, int link_idx)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000162{
163 struct device *dev = graph_priv_to_dev(priv);
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000164 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, link_idx);
165 struct graph_dai_props *dai_props = graph_priv_to_props(priv, link_idx);
166 struct asoc_simple_dai *cpu_dai;
167 struct asoc_simple_dai *codec_dai;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000168 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);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000170 int ret;
171
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000172 cpu_dai =
173 dai_props->cpu_dai = &priv->dais[(*dai_idx)++];
174 codec_dai =
175 dai_props->codec_dai = &priv->dais[(*dai_idx)++];
176
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000177 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
178 NULL, &dai_link->dai_fmt);
179 if (ret < 0)
180 goto dai_link_of_err;
181
Kuninori Morimoto8036dbc2018-10-10 02:22:29 +0000182 of_property_read_u32(cpu_ep, "mclk-fs", &dai_props->mclk_fs);
183 of_property_read_u32(codec_ep, "mclk-fs", &dai_props->mclk_fs);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000184
185 ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
186 if (ret < 0)
187 goto dai_link_of_err;
188
189 ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
190 if (ret < 0)
191 goto dai_link_of_err;
192
Kuninori Morimotoc98907d2017-06-14 00:35:30 +0000193 ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000194 if (ret < 0)
195 goto dai_link_of_err;
196
Kuninori Morimotoc98907d2017-06-14 00:35:30 +0000197 ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000198 if (ret < 0)
199 goto dai_link_of_err;
200
201 ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
202 if (ret < 0)
203 goto dai_link_of_err;
204
205 ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
206 if (ret < 0)
207 goto dai_link_of_err;
208
209 ret = asoc_simple_card_canonicalize_dailink(dai_link);
210 if (ret < 0)
211 goto dai_link_of_err;
212
213 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
214 "%s-%s",
215 dai_link->cpu_dai_name,
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +0000216 dai_link->codecs->dai_name);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000217 if (ret < 0)
218 goto dai_link_of_err;
219
220 dai_link->ops = &asoc_graph_card_ops;
221 dai_link->init = asoc_graph_card_dai_init;
222
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000223 asoc_simple_card_canonicalize_cpu(dai_link,
Kuninori Morimoto47ca9592017-06-22 06:21:49 +0000224 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000225
226dai_link_of_err:
227 of_node_put(cpu_ep);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000228 of_node_put(codec_ep);
229
230 return ret;
231}
232
233static int asoc_graph_card_parse_of(struct graph_card_data *priv)
234{
235 struct of_phandle_iterator it;
236 struct device *dev = graph_priv_to_dev(priv);
237 struct snd_soc_card *card = graph_priv_to_card(priv);
238 struct device_node *node = dev->of_node;
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000239 int rc, ret;
240 int link_idx, dai_idx;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000241
Shawn Guof9869072017-06-29 21:26:38 +0800242 ret = asoc_simple_card_of_parse_widgets(card, NULL);
243 if (ret < 0)
244 return ret;
245
Kuninori Morimoto33404f32018-11-21 02:11:13 +0000246 ret = asoc_simple_card_of_parse_routing(card, NULL);
Shawn Guof9869072017-06-29 21:26:38 +0800247 if (ret < 0)
248 return ret;
249
Olivier Moysan757652d2017-11-09 15:07:58 +0100250 /* Factor to mclk, used in hw_params() */
251 of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000252
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000253 link_idx = 0;
254 dai_idx = 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000255 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000256 ret = asoc_graph_card_dai_link_of(it.node, priv,
257 &dai_idx, link_idx++);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700258 if (ret < 0) {
259 of_node_put(it.node);
260
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000261 return ret;
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700262 }
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000263 }
264
265 return asoc_simple_card_parse_card_name(card, NULL);
266}
267
268static int asoc_graph_get_dais_count(struct device *dev)
269{
270 struct of_phandle_iterator it;
271 struct device_node *node = dev->of_node;
272 int count = 0;
273 int rc;
274
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700275 of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000276 count++;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000277
278 return count;
279}
280
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900281static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
282{
283 struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
284 int ret;
285
286 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
287 if (ret < 0)
288 return ret;
289
290 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
291 if (ret < 0)
292 return ret;
293
294 return 0;
295}
296
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000297static int asoc_graph_card_probe(struct platform_device *pdev)
298{
299 struct graph_card_data *priv;
300 struct snd_soc_dai_link *dai_link;
301 struct graph_dai_props *dai_props;
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000302 struct asoc_simple_dai *dais;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000303 struct device *dev = &pdev->dev;
304 struct snd_soc_card *card;
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +0000305 int num, ret, i;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000306
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
Kees Cooka86854d2018-06-12 14:07:58 -0700316 dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
317 dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000318 dais = devm_kcalloc(dev, num * 2, sizeof(*dais), GFP_KERNEL);
319 if (!dai_props || !dai_link || !dais)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000320 return -ENOMEM;
321
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +0000322 /*
323 * Use snd_soc_dai_link_component instead of legacy style
324 * It is codec only. but cpu/platform will be supported in the future.
325 * see
326 * soc-core.c :: snd_soc_init_multicodec()
327 */
328 for (i = 0; i < num; i++) {
329 dai_link[i].codecs = &dai_props[i].codecs;
330 dai_link[i].num_codecs = 1;
Kuninori Morimoto46c731872018-08-31 03:10:58 +0000331 dai_link[i].platform = &dai_props[i].platform;
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +0000332 }
333
Shawn Guof9869072017-06-29 21:26:38 +0800334 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
335 if (IS_ERR(priv->pa_gpio)) {
336 ret = PTR_ERR(priv->pa_gpio);
337 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
338 return ret;
339 }
340
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000341 priv->dai_props = dai_props;
342 priv->dai_link = dai_link;
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000343 priv->dais = dais;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000344
345 /* Init snd_soc_card */
346 card = graph_priv_to_card(priv);
347 card->owner = THIS_MODULE;
348 card->dev = dev;
349 card->dai_link = dai_link;
350 card->num_links = num;
Shawn Guof9869072017-06-29 21:26:38 +0800351 card->dapm_widgets = asoc_graph_card_dapm_widgets;
352 card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900353 card->probe = asoc_graph_soc_card_probe;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000354
355 ret = asoc_graph_card_parse_of(priv);
356 if (ret < 0) {
357 if (ret != -EPROBE_DEFER)
358 dev_err(dev, "parse error %d\n", ret);
359 goto err;
360 }
361
362 snd_soc_card_set_drvdata(card, priv);
363
364 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoecea9312017-05-19 00:58:00 +0000365 if (ret < 0)
366 goto err;
367
368 return 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000369err:
370 asoc_simple_card_clean_reference(card);
371
372 return ret;
373}
374
375static int asoc_graph_card_remove(struct platform_device *pdev)
376{
377 struct snd_soc_card *card = platform_get_drvdata(pdev);
378
379 return asoc_simple_card_clean_reference(card);
380}
381
382static const struct of_device_id asoc_graph_of_match[] = {
383 { .compatible = "audio-graph-card", },
384 {},
385};
386MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
387
388static struct platform_driver asoc_graph_card = {
389 .driver = {
390 .name = "asoc-audio-graph-card",
Kuninori Morimoto7b828a32017-08-22 04:56:44 +0000391 .pm = &snd_soc_pm_ops,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000392 .of_match_table = asoc_graph_of_match,
393 },
394 .probe = asoc_graph_card_probe,
395 .remove = asoc_graph_card_remove,
396};
397module_platform_driver(asoc_graph_card);
398
399MODULE_ALIAS("platform:asoc-audio-graph-card");
400MODULE_LICENSE("GPL v2");
401MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
402MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");