1 /*
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>
16 #include <linux/gpio/consumer.h>
17 #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 
27 struct 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;
32 		unsigned int mclk_fs;
33 	} *dai_props;
34 	unsigned int mclk_fs;
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(rcpu_ep, "mclk-fs", &dai_props->mclk_fs);
186 
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 
195 	ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
196 	if (ret < 0)
197 		goto dai_link_of_err;
198 
199 	ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
200 	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 
225 	asoc_simple_card_canonicalize_cpu(dai_link,
226 		of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
227 
228 dai_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 
236 static 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 
245 	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 
253 	/* Factor to mclk, used in hw_params() */
254 	of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
255 
256 	of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
257 		ret = asoc_graph_card_dai_link_of(it.node, priv, idx++);
258 		if (ret < 0) {
259 			of_node_put(it.node);
260 
261 			return ret;
262 		}
263 	}
264 
265 	return asoc_simple_card_parse_card_name(card, NULL);
266 }
267 
268 static 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 
275 	of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
276 		count++;
277 
278 	return count;
279 }
280 
281 static 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 
304 	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 
311 	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;
320 	card->dapm_widgets = asoc_graph_card_dapm_widgets;
321 	card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
322 
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);
333 	if (ret < 0)
334 		goto err;
335 
336 	return 0;
337 err:
338 	asoc_simple_card_clean_reference(card);
339 
340 	return ret;
341 }
342 
343 static 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 
350 static const struct of_device_id asoc_graph_of_match[] = {
351 	{ .compatible = "audio-graph-card", },
352 	{},
353 };
354 MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
355 
356 static struct platform_driver asoc_graph_card = {
357 	.driver = {
358 		.name = "asoc-audio-graph-card",
359 		.pm = &snd_soc_pm_ops,
360 		.of_match_table = asoc_graph_of_match,
361 	},
362 	.probe = asoc_graph_card_probe,
363 	.remove = asoc_graph_card_remove,
364 };
365 module_platform_driver(asoc_graph_card);
366 
367 MODULE_ALIAS("platform:asoc-audio-graph-card");
368 MODULE_LICENSE("GPL v2");
369 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
370 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
371