blob: 1b6164249341138ec70ae38e57d48957c69c7ba1 [file] [log] [blame]
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +00001/*
2 * ASoC audio graph sound card support
3 *
4 * Copyright (C) 2016 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/gpio.h>
Shawn Guof9869072017-06-29 21:26:38 +080016#include <linux/gpio/consumer.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000017#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/of_gpio.h>
21#include <linux/of_graph.h>
22#include <linux/platform_device.h>
23#include <linux/string.h>
24#include <sound/jack.h>
25#include <sound/simple_card_utils.h>
26
27struct graph_card_data {
28 struct snd_soc_card snd_card;
29 struct graph_dai_props {
30 struct asoc_simple_dai cpu_dai;
31 struct asoc_simple_dai codec_dai;
Olivier Moysan757652d2017-11-09 15:07:58 +010032 unsigned int mclk_fs;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000033 } *dai_props;
Olivier Moysan757652d2017-11-09 15:07:58 +010034 unsigned int mclk_fs;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000035 struct snd_soc_dai_link *dai_link;
Shawn Guof9869072017-06-29 21:26:38 +080036 struct gpio_desc *pa_gpio;
37};
38
39static 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
60static 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),
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000064};
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
71static 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
Kuninori Morimotod471d552017-06-09 00:45:23 +000078 ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000079 if (ret)
80 return ret;
81
Kuninori Morimotod471d552017-06-09 00:45:23 +000082 ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000083 if (ret)
Kuninori Morimotod471d552017-06-09 00:45:23 +000084 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000085
86 return ret;
87}
88
89static 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
Kuninori Morimotod471d552017-06-09 00:45:23 +000095 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000096
Kuninori Morimotod471d552017-06-09 00:45:23 +000097 asoc_simple_card_clk_disable(&dai_props->codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000098}
99
Olivier Moysan757652d2017-11-09 15:07:58 +0100100static 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;
129err:
130 return ret;
131}
132
Bhumika Goyale23d8342017-08-16 22:29:26 +0530133static const struct snd_soc_ops asoc_graph_card_ops = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000134 .startup = asoc_graph_card_startup,
135 .shutdown = asoc_graph_card_shutdown,
Olivier Moysan757652d2017-11-09 15:07:58 +0100136 .hw_params = asoc_graph_card_hw_params,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000137};
138
139static 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
159static 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;
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);
170 struct device_node *rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
171 int ret;
172
173 if (rcpu_ep != cpu_ep) {
Colin Ian Kinga619f042017-05-18 08:48:15 +0100174 dev_err(dev, "remote-endpoint mismatch (%s/%s/%s)\n",
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000175 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
Olivier Moysan757652d2017-11-09 15:07:58 +0100185 of_property_read_u32(rcpu_ep, "mclk-fs", &dai_props->mclk_fs);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000186
187 ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
188 if (ret < 0)
189 goto dai_link_of_err;
190
191 ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
192 if (ret < 0)
193 goto dai_link_of_err;
194
Kuninori Morimotoc98907d2017-06-14 00:35:30 +0000195 ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000196 if (ret < 0)
197 goto dai_link_of_err;
198
Kuninori Morimotoc98907d2017-06-14 00:35:30 +0000199 ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000200 if (ret < 0)
201 goto dai_link_of_err;
202
203 ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
204 if (ret < 0)
205 goto dai_link_of_err;
206
207 ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
208 if (ret < 0)
209 goto dai_link_of_err;
210
211 ret = asoc_simple_card_canonicalize_dailink(dai_link);
212 if (ret < 0)
213 goto dai_link_of_err;
214
215 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
216 "%s-%s",
217 dai_link->cpu_dai_name,
218 dai_link->codec_dai_name);
219 if (ret < 0)
220 goto dai_link_of_err;
221
222 dai_link->ops = &asoc_graph_card_ops;
223 dai_link->init = asoc_graph_card_dai_init;
224
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000225 asoc_simple_card_canonicalize_cpu(dai_link,
Kuninori Morimoto47ca9592017-06-22 06:21:49 +0000226 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000227
228dai_link_of_err:
229 of_node_put(cpu_ep);
230 of_node_put(rcpu_ep);
231 of_node_put(codec_ep);
232
233 return ret;
234}
235
236static int asoc_graph_card_parse_of(struct graph_card_data *priv)
237{
238 struct of_phandle_iterator it;
239 struct device *dev = graph_priv_to_dev(priv);
240 struct snd_soc_card *card = graph_priv_to_card(priv);
241 struct device_node *node = dev->of_node;
242 int rc, idx = 0;
243 int ret;
244
Shawn Guof9869072017-06-29 21:26:38 +0800245 ret = asoc_simple_card_of_parse_widgets(card, NULL);
246 if (ret < 0)
247 return ret;
248
249 ret = asoc_simple_card_of_parse_routing(card, NULL, 1);
250 if (ret < 0)
251 return ret;
252
Olivier Moysan757652d2017-11-09 15:07:58 +0100253 /* Factor to mclk, used in hw_params() */
254 of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000255
256 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
257 ret = asoc_graph_card_dai_link_of(it.node, priv, 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
281static int asoc_graph_card_probe(struct platform_device *pdev)
282{
283 struct graph_card_data *priv;
284 struct snd_soc_dai_link *dai_link;
285 struct graph_dai_props *dai_props;
286 struct device *dev = &pdev->dev;
287 struct snd_soc_card *card;
288 int num, ret;
289
290 /* Allocate the private data and the DAI link array */
291 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
292 if (!priv)
293 return -ENOMEM;
294
295 num = asoc_graph_get_dais_count(dev);
296 if (num == 0)
297 return -EINVAL;
298
299 dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
300 dai_link = devm_kzalloc(dev, sizeof(*dai_link) * num, GFP_KERNEL);
301 if (!dai_props || !dai_link)
302 return -ENOMEM;
303
Shawn Guof9869072017-06-29 21:26:38 +0800304 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
305 if (IS_ERR(priv->pa_gpio)) {
306 ret = PTR_ERR(priv->pa_gpio);
307 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
308 return ret;
309 }
310
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000311 priv->dai_props = dai_props;
312 priv->dai_link = dai_link;
313
314 /* Init snd_soc_card */
315 card = graph_priv_to_card(priv);
316 card->owner = THIS_MODULE;
317 card->dev = dev;
318 card->dai_link = dai_link;
319 card->num_links = num;
Shawn Guof9869072017-06-29 21:26:38 +0800320 card->dapm_widgets = asoc_graph_card_dapm_widgets;
321 card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000322
323 ret = asoc_graph_card_parse_of(priv);
324 if (ret < 0) {
325 if (ret != -EPROBE_DEFER)
326 dev_err(dev, "parse error %d\n", ret);
327 goto err;
328 }
329
330 snd_soc_card_set_drvdata(card, priv);
331
332 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoecea9312017-05-19 00:58:00 +0000333 if (ret < 0)
334 goto err;
335
336 return 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000337err:
338 asoc_simple_card_clean_reference(card);
339
340 return ret;
341}
342
343static int asoc_graph_card_remove(struct platform_device *pdev)
344{
345 struct snd_soc_card *card = platform_get_drvdata(pdev);
346
347 return asoc_simple_card_clean_reference(card);
348}
349
350static const struct of_device_id asoc_graph_of_match[] = {
351 { .compatible = "audio-graph-card", },
352 {},
353};
354MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
355
356static struct platform_driver asoc_graph_card = {
357 .driver = {
358 .name = "asoc-audio-graph-card",
Kuninori Morimoto7b828a32017-08-22 04:56:44 +0000359 .pm = &snd_soc_pm_ops,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000360 .of_match_table = asoc_graph_of_match,
361 },
362 .probe = asoc_graph_card_probe,
363 .remove = asoc_graph_card_remove,
364};
365module_platform_driver(asoc_graph_card);
366
367MODULE_ALIAS("platform:asoc-audio-graph-card");
368MODULE_LICENSE("GPL v2");
369MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
370MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");