• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Renesas Sampling Rate Convert Sound Card for DPCM
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 
24 struct rsrc_card_of_data {
25 	const char *prefix;
26 	const struct snd_soc_dapm_route *routes;
27 	int num_routes;
28 };
29 
30 static const struct snd_soc_dapm_route routes_ssi0_ak4642[] = {
31 	{"ak4642 Playback", NULL, "DAI0 Playback"},
32 	{"DAI0 Capture", NULL, "ak4642 Capture"},
33 };
34 
35 static const struct rsrc_card_of_data routes_of_ssi0_ak4642 = {
36 	.prefix		= "ak4642",
37 	.routes		= routes_ssi0_ak4642,
38 	.num_routes	= ARRAY_SIZE(routes_ssi0_ak4642),
39 };
40 
41 static const struct of_device_id rsrc_card_of_match[] = {
42 	{ .compatible = "renesas,rsrc-card,lager",	.data = &routes_of_ssi0_ak4642 },
43 	{ .compatible = "renesas,rsrc-card,koelsch",	.data = &routes_of_ssi0_ak4642 },
44 	{ .compatible = "renesas,rsrc-card", },
45 	{},
46 };
47 MODULE_DEVICE_TABLE(of, rsrc_card_of_match);
48 
49 #define DAI_NAME_NUM	32
50 struct rsrc_card_dai {
51 	unsigned int fmt;
52 	unsigned int sysclk;
53 	struct clk *clk;
54 	char dai_name[DAI_NAME_NUM];
55 };
56 
57 #define IDX_CPU		0
58 #define IDX_CODEC	1
59 struct rsrc_card_priv {
60 	struct snd_soc_card snd_card;
61 	struct snd_soc_codec_conf codec_conf;
62 	struct rsrc_card_dai *dai_props;
63 	struct snd_soc_dai_link *dai_link;
64 	int dai_num;
65 	u32 convert_rate;
66 };
67 
68 #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
69 #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
70 #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
71 #define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data)
72 
rsrc_card_startup(struct snd_pcm_substream * substream)73 static int rsrc_card_startup(struct snd_pcm_substream *substream)
74 {
75 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 	struct rsrc_card_priv *priv =	snd_soc_card_get_drvdata(rtd->card);
77 	struct rsrc_card_dai *dai_props =
78 		rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
79 
80 	return clk_prepare_enable(dai_props->clk);
81 }
82 
rsrc_card_shutdown(struct snd_pcm_substream * substream)83 static void rsrc_card_shutdown(struct snd_pcm_substream *substream)
84 {
85 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
86 	struct rsrc_card_priv *priv =	snd_soc_card_get_drvdata(rtd->card);
87 	struct rsrc_card_dai *dai_props =
88 		rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
89 
90 	clk_disable_unprepare(dai_props->clk);
91 }
92 
93 static struct snd_soc_ops rsrc_card_ops = {
94 	.startup = rsrc_card_startup,
95 	.shutdown = rsrc_card_shutdown,
96 };
97 
rsrc_card_dai_init(struct snd_soc_pcm_runtime * rtd)98 static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd)
99 {
100 	struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
101 	struct snd_soc_dai *dai;
102 	struct snd_soc_dai_link *dai_link;
103 	struct rsrc_card_dai *dai_props;
104 	int num = rtd - rtd->card->rtd;
105 	int ret;
106 
107 	dai_link	= rsrc_priv_to_link(priv, num);
108 	dai_props	= rsrc_priv_to_props(priv, num);
109 	dai		= dai_link->dynamic ?
110 				rtd->cpu_dai :
111 				rtd->codec_dai;
112 
113 	if (dai_props->fmt) {
114 		ret = snd_soc_dai_set_fmt(dai, dai_props->fmt);
115 		if (ret && ret != -ENOTSUPP) {
116 			dev_err(dai->dev, "set_fmt error\n");
117 			goto err;
118 		}
119 	}
120 
121 	if (dai_props->sysclk) {
122 		ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0);
123 		if (ret && ret != -ENOTSUPP) {
124 			dev_err(dai->dev, "set_sysclk error\n");
125 			goto err;
126 		}
127 	}
128 
129 	ret = 0;
130 
131 err:
132 	return ret;
133 }
134 
rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)135 static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
136 					struct snd_pcm_hw_params *params)
137 {
138 	struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
139 	struct snd_interval *rate = hw_param_interval(params,
140 						      SNDRV_PCM_HW_PARAM_RATE);
141 
142 	if (!priv->convert_rate)
143 		return 0;
144 
145 	rate->min = rate->max = priv->convert_rate;
146 
147 	return 0;
148 }
149 
rsrc_card_parse_daifmt(struct device_node * node,struct device_node * np,struct rsrc_card_priv * priv,int idx,bool is_fe)150 static int rsrc_card_parse_daifmt(struct device_node *node,
151 				  struct device_node *np,
152 				  struct rsrc_card_priv *priv,
153 				  int idx, bool is_fe)
154 {
155 	struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
156 	struct device_node *bitclkmaster = NULL;
157 	struct device_node *framemaster = NULL;
158 	struct device_node *codec = is_fe ? NULL : np;
159 	unsigned int daifmt;
160 
161 	daifmt = snd_soc_of_parse_daifmt(node, NULL,
162 					 &bitclkmaster, &framemaster);
163 	daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
164 
165 	if (!bitclkmaster && !framemaster)
166 		return -EINVAL;
167 
168 	if (codec == bitclkmaster)
169 		daifmt |= (codec == framemaster) ?
170 			SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
171 	else
172 		daifmt |= (codec == framemaster) ?
173 			SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
174 
175 	dai_props->fmt	= daifmt;
176 
177 	of_node_put(bitclkmaster);
178 	of_node_put(framemaster);
179 
180 	return 0;
181 }
182 
rsrc_card_parse_links(struct device_node * np,struct rsrc_card_priv * priv,int idx,bool is_fe)183 static int rsrc_card_parse_links(struct device_node *np,
184 				 struct rsrc_card_priv *priv,
185 				 int idx, bool is_fe)
186 {
187 	struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
188 	struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
189 	struct of_phandle_args args;
190 	int ret;
191 
192 	/*
193 	 * Get node via "sound-dai = <&phandle port>"
194 	 * it will be used as xxx_of_node on soc_bind_dai_link()
195 	 */
196 	ret = of_parse_phandle_with_args(np, "sound-dai",
197 					 "#sound-dai-cells", 0, &args);
198 	if (ret)
199 		return ret;
200 
201 	if (is_fe) {
202 		/* BE is dummy */
203 		dai_link->codec_of_node		= NULL;
204 		dai_link->codec_dai_name	= "snd-soc-dummy-dai";
205 		dai_link->codec_name		= "snd-soc-dummy";
206 
207 		/* FE settings */
208 		dai_link->dynamic		= 1;
209 		dai_link->dpcm_merged_format	= 1;
210 		dai_link->cpu_of_node		= args.np;
211 		snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
212 
213 		/* set dai_name */
214 		snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
215 			 dai_link->cpu_dai_name);
216 
217 		/*
218 		 * In soc_bind_dai_link() will check cpu name after
219 		 * of_node matching if dai_link has cpu_dai_name.
220 		 * but, it will never match if name was created by
221 		 * fmt_single_name() remove cpu_dai_name if cpu_args
222 		 * was 0. See:
223 		 *	fmt_single_name()
224 		 *	fmt_multiple_name()
225 		 */
226 		if (!args.args_count)
227 			dai_link->cpu_dai_name = NULL;
228 	} else {
229 		struct device *dev = rsrc_priv_to_dev(priv);
230 		const struct rsrc_card_of_data *of_data;
231 
232 		of_data = rsrc_dev_to_of_data(dev);
233 
234 		/* FE is dummy */
235 		dai_link->cpu_of_node		= NULL;
236 		dai_link->cpu_dai_name		= "snd-soc-dummy-dai";
237 		dai_link->cpu_name		= "snd-soc-dummy";
238 
239 		/* BE settings */
240 		dai_link->no_pcm		= 1;
241 		dai_link->be_hw_params_fixup	= rsrc_card_be_hw_params_fixup;
242 		dai_link->codec_of_node		= args.np;
243 		snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
244 
245 		/* additional name prefix */
246 		if (of_data) {
247 			priv->codec_conf.of_node = dai_link->codec_of_node;
248 			priv->codec_conf.name_prefix = of_data->prefix;
249 		} else {
250 			snd_soc_of_parse_audio_prefix(&priv->snd_card,
251 						      &priv->codec_conf,
252 						      dai_link->codec_of_node,
253 						      "audio-prefix");
254 		}
255 
256 		/* set dai_name */
257 		snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
258 			 dai_link->codec_dai_name);
259 	}
260 
261 	/* Simple Card assumes platform == cpu */
262 	dai_link->platform_of_node	= dai_link->cpu_of_node;
263 	dai_link->dpcm_playback		= 1;
264 	dai_link->dpcm_capture		= 1;
265 	dai_link->name			= dai_props->dai_name;
266 	dai_link->stream_name		= dai_props->dai_name;
267 	dai_link->ops			= &rsrc_card_ops;
268 	dai_link->init			= rsrc_card_dai_init;
269 
270 	return 0;
271 }
272 
rsrc_card_parse_clk(struct device_node * np,struct rsrc_card_priv * priv,int idx,bool is_fe)273 static int rsrc_card_parse_clk(struct device_node *np,
274 			       struct rsrc_card_priv *priv,
275 			       int idx, bool is_fe)
276 {
277 	struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
278 	struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
279 	struct clk *clk;
280 	struct device_node *of_np = is_fe ?	dai_link->cpu_of_node :
281 						dai_link->codec_of_node;
282 	u32 val;
283 
284 	/*
285 	 * Parse dai->sysclk come from "clocks = <&xxx>"
286 	 * (if system has common clock)
287 	 *  or "system-clock-frequency = <xxx>"
288 	 *  or device's module clock.
289 	 */
290 	if (of_property_read_bool(np, "clocks")) {
291 		clk = of_clk_get(np, 0);
292 		if (IS_ERR(clk))
293 			return PTR_ERR(clk);
294 
295 		dai_props->sysclk = clk_get_rate(clk);
296 		dai_props->clk = clk;
297 	} else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
298 		dai_props->sysclk = val;
299 	} else {
300 		clk = of_clk_get(of_np, 0);
301 		if (!IS_ERR(clk))
302 			dai_props->sysclk = clk_get_rate(clk);
303 	}
304 
305 	return 0;
306 }
307 
rsrc_card_dai_link_of(struct device_node * node,struct device_node * np,struct rsrc_card_priv * priv,int idx)308 static int rsrc_card_dai_link_of(struct device_node *node,
309 				 struct device_node *np,
310 				 struct rsrc_card_priv *priv,
311 				 int idx)
312 {
313 	struct device *dev = rsrc_priv_to_dev(priv);
314 	struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
315 	bool is_fe = false;
316 	int ret;
317 
318 	if (0 == strcmp(np->name, "cpu"))
319 		is_fe = true;
320 
321 	ret = rsrc_card_parse_daifmt(node, np, priv, idx, is_fe);
322 	if (ret < 0)
323 		return ret;
324 
325 	ret = rsrc_card_parse_links(np, priv, idx, is_fe);
326 	if (ret < 0)
327 		return ret;
328 
329 	ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
330 	if (ret < 0)
331 		return ret;
332 
333 	dev_dbg(dev, "\t%s / %04x / %d\n",
334 		dai_props->dai_name,
335 		dai_props->fmt,
336 		dai_props->sysclk);
337 
338 	return ret;
339 }
340 
rsrc_card_parse_of(struct device_node * node,struct rsrc_card_priv * priv,struct device * dev)341 static int rsrc_card_parse_of(struct device_node *node,
342 			      struct rsrc_card_priv *priv,
343 			      struct device *dev)
344 {
345 	const struct rsrc_card_of_data *of_data = rsrc_dev_to_of_data(dev);
346 	struct rsrc_card_dai *props;
347 	struct snd_soc_dai_link *links;
348 	struct device_node *np;
349 	int ret;
350 	int i, num;
351 
352 	if (!node)
353 		return -EINVAL;
354 
355 	num = of_get_child_count(node);
356 	props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
357 	links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
358 	if (!props || !links)
359 		return -ENOMEM;
360 
361 	priv->dai_props	= props;
362 	priv->dai_link	= links;
363 	priv->dai_num	= num;
364 
365 	/* Init snd_soc_card */
366 	priv->snd_card.owner			= THIS_MODULE;
367 	priv->snd_card.dev			= dev;
368 	priv->snd_card.dai_link			= priv->dai_link;
369 	priv->snd_card.num_links		= num;
370 	priv->snd_card.codec_conf		= &priv->codec_conf;
371 	priv->snd_card.num_configs		= 1;
372 
373 	if (of_data) {
374 		priv->snd_card.of_dapm_routes		= of_data->routes;
375 		priv->snd_card.num_of_dapm_routes	= of_data->num_routes;
376 	} else {
377 		snd_soc_of_parse_audio_routing(&priv->snd_card,
378 					       "audio-routing");
379 	}
380 
381 	/* Parse the card name from DT */
382 	snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
383 
384 	/* sampling rate convert */
385 	of_property_read_u32(node, "convert-rate", &priv->convert_rate);
386 
387 	dev_dbg(dev, "New rsrc-audio-card: %s (%d)\n",
388 		priv->snd_card.name ? priv->snd_card.name : "",
389 		priv->convert_rate);
390 
391 	i = 0;
392 	for_each_child_of_node(node, np) {
393 		ret = rsrc_card_dai_link_of(node, np, priv, i);
394 		if (ret < 0)
395 			return ret;
396 		i++;
397 	}
398 
399 	if (!priv->snd_card.name)
400 		priv->snd_card.name = priv->snd_card.dai_link->name;
401 
402 	return 0;
403 }
404 
405 /* Decrease the reference count of the device nodes */
rsrc_card_unref(struct snd_soc_card * card)406 static int rsrc_card_unref(struct snd_soc_card *card)
407 {
408 	struct snd_soc_dai_link *dai_link;
409 	int num_links;
410 
411 	for (num_links = 0, dai_link = card->dai_link;
412 	     num_links < card->num_links;
413 	     num_links++, dai_link++) {
414 		of_node_put(dai_link->cpu_of_node);
415 		of_node_put(dai_link->codec_of_node);
416 	}
417 	return 0;
418 }
419 
rsrc_card_probe(struct platform_device * pdev)420 static int rsrc_card_probe(struct platform_device *pdev)
421 {
422 	struct rsrc_card_priv *priv;
423 	struct device_node *np = pdev->dev.of_node;
424 	struct device *dev = &pdev->dev;
425 	int ret;
426 
427 	/* Allocate the private data */
428 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
429 	if (!priv)
430 		return -ENOMEM;
431 
432 	ret = rsrc_card_parse_of(np, priv, dev);
433 	if (ret < 0) {
434 		if (ret != -EPROBE_DEFER)
435 			dev_err(dev, "parse error %d\n", ret);
436 		goto err;
437 	}
438 
439 	snd_soc_card_set_drvdata(&priv->snd_card, priv);
440 
441 	ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
442 	if (ret >= 0)
443 		return ret;
444 err:
445 	rsrc_card_unref(&priv->snd_card);
446 
447 	return ret;
448 }
449 
rsrc_card_remove(struct platform_device * pdev)450 static int rsrc_card_remove(struct platform_device *pdev)
451 {
452 	struct snd_soc_card *card = platform_get_drvdata(pdev);
453 
454 	return rsrc_card_unref(card);
455 }
456 
457 static struct platform_driver rsrc_card = {
458 	.driver = {
459 		.name = "renesas-src-audio-card",
460 		.of_match_table = rsrc_card_of_match,
461 	},
462 	.probe = rsrc_card_probe,
463 	.remove = rsrc_card_remove,
464 };
465 
466 module_platform_driver(rsrc_card);
467 
468 MODULE_ALIAS("platform:renesas-src-audio-card");
469 MODULE_LICENSE("GPL");
470 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
471 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
472