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