Merge branch 'pm-cpufreq-fixes'
[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
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 sysclk;
52 unsigned int tx_slot_mask;
53 unsigned int rx_slot_mask;
54 int slots;
55 int slot_width;
56 struct clk *clk;
57 char dai_name[DAI_NAME_NUM];
58 };
59
60 #define IDX_CPU 0
61 #define IDX_CODEC 1
62 struct rsrc_card_priv {
63 struct snd_soc_card snd_card;
64 struct snd_soc_codec_conf codec_conf;
65 struct rsrc_card_dai *dai_props;
66 struct snd_soc_dai_link *dai_link;
67 int dai_num;
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_daifmt(struct device_node *node,
163 struct device_node *codec,
164 struct rsrc_card_priv *priv,
165 struct snd_soc_dai_link *dai_link,
166 unsigned int *retfmt)
167 {
168 struct device_node *bitclkmaster = NULL;
169 struct device_node *framemaster = NULL;
170 unsigned int daifmt;
171
172 daifmt = snd_soc_of_parse_daifmt(node, NULL,
173 &bitclkmaster, &framemaster);
174 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
175
176 if (!bitclkmaster && !framemaster)
177 return -EINVAL;
178
179 if (codec == bitclkmaster)
180 daifmt |= (codec == framemaster) ?
181 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
182 else
183 daifmt |= (codec == framemaster) ?
184 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
185
186 of_node_put(bitclkmaster);
187 of_node_put(framemaster);
188
189 *retfmt = daifmt;
190
191 return 0;
192 }
193
194 static int rsrc_card_parse_links(struct device_node *np,
195 struct rsrc_card_priv *priv,
196 int idx, bool is_fe)
197 {
198 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
199 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
200 struct of_phandle_args args;
201 int ret;
202
203 /*
204 * Get node via "sound-dai = <&phandle port>"
205 * it will be used as xxx_of_node on soc_bind_dai_link()
206 */
207 ret = of_parse_phandle_with_args(np, "sound-dai",
208 "#sound-dai-cells", 0, &args);
209 if (ret)
210 return ret;
211
212 /* Parse TDM slot */
213 ret = snd_soc_of_parse_tdm_slot(np,
214 &dai_props->tx_slot_mask,
215 &dai_props->rx_slot_mask,
216 &dai_props->slots,
217 &dai_props->slot_width);
218 if (ret)
219 return ret;
220
221 if (is_fe) {
222 /* BE is dummy */
223 dai_link->codec_of_node = NULL;
224 dai_link->codec_dai_name = "snd-soc-dummy-dai";
225 dai_link->codec_name = "snd-soc-dummy";
226
227 /* FE settings */
228 dai_link->dynamic = 1;
229 dai_link->dpcm_merged_format = 1;
230 dai_link->cpu_of_node = args.np;
231 ret = snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
232 if (ret < 0)
233 return ret;
234
235 /* set dai_name */
236 snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
237 dai_link->cpu_dai_name);
238
239 /*
240 * In soc_bind_dai_link() will check cpu name after
241 * of_node matching if dai_link has cpu_dai_name.
242 * but, it will never match if name was created by
243 * fmt_single_name() remove cpu_dai_name if cpu_args
244 * was 0. See:
245 * fmt_single_name()
246 * fmt_multiple_name()
247 */
248 if (!args.args_count)
249 dai_link->cpu_dai_name = NULL;
250 } else {
251 struct device *dev = rsrc_priv_to_dev(priv);
252 const struct rsrc_card_of_data *of_data;
253
254 of_data = of_device_get_match_data(dev);
255
256 /* FE is dummy */
257 dai_link->cpu_of_node = NULL;
258 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
259 dai_link->cpu_name = "snd-soc-dummy";
260
261 /* BE settings */
262 dai_link->no_pcm = 1;
263 dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup;
264 dai_link->codec_of_node = args.np;
265 ret = snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
266 if (ret < 0)
267 return ret;
268
269 /* additional name prefix */
270 if (of_data) {
271 priv->codec_conf.of_node = dai_link->codec_of_node;
272 priv->codec_conf.name_prefix = of_data->prefix;
273 } else {
274 snd_soc_of_parse_audio_prefix(&priv->snd_card,
275 &priv->codec_conf,
276 dai_link->codec_of_node,
277 "audio-prefix");
278 }
279
280 /* set dai_name */
281 snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
282 dai_link->codec_dai_name);
283 }
284
285 /* Simple Card assumes platform == cpu */
286 dai_link->platform_of_node = dai_link->cpu_of_node;
287 dai_link->dpcm_playback = 1;
288 dai_link->dpcm_capture = 1;
289 dai_link->name = dai_props->dai_name;
290 dai_link->stream_name = dai_props->dai_name;
291 dai_link->ops = &rsrc_card_ops;
292 dai_link->init = rsrc_card_dai_init;
293
294 return 0;
295 }
296
297 static int rsrc_card_parse_clk(struct device_node *np,
298 struct rsrc_card_priv *priv,
299 int idx, bool is_fe)
300 {
301 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
302 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
303 struct clk *clk;
304 struct device_node *of_np = is_fe ? dai_link->cpu_of_node :
305 dai_link->codec_of_node;
306 u32 val;
307
308 /*
309 * Parse dai->sysclk come from "clocks = <&xxx>"
310 * (if system has common clock)
311 * or "system-clock-frequency = <xxx>"
312 * or device's module clock.
313 */
314 if (of_property_read_bool(np, "clocks")) {
315 clk = of_clk_get(np, 0);
316 if (IS_ERR(clk))
317 return PTR_ERR(clk);
318
319 dai_props->sysclk = clk_get_rate(clk);
320 dai_props->clk = clk;
321 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
322 dai_props->sysclk = val;
323 } else {
324 clk = of_clk_get(of_np, 0);
325 if (!IS_ERR(clk))
326 dai_props->sysclk = clk_get_rate(clk);
327 }
328
329 return 0;
330 }
331
332 static int rsrc_card_dai_sub_link_of(struct device_node *node,
333 struct device_node *np,
334 struct rsrc_card_priv *priv,
335 int idx, bool is_fe)
336 {
337 struct device *dev = rsrc_priv_to_dev(priv);
338 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
339 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
340 int ret;
341
342 ret = rsrc_card_parse_links(np, priv, idx, is_fe);
343 if (ret < 0)
344 return ret;
345
346 ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
347 if (ret < 0)
348 return ret;
349
350 dev_dbg(dev, "\t%s / %04x / %d\n",
351 dai_props->dai_name,
352 dai_link->dai_fmt,
353 dai_props->sysclk);
354
355 return ret;
356 }
357
358 static int rsrc_card_dai_link_of(struct device_node *node,
359 struct rsrc_card_priv *priv)
360 {
361 struct snd_soc_dai_link *dai_link;
362 struct device_node *np;
363 unsigned int daifmt = 0;
364 int ret, i;
365 bool is_fe;
366
367 /* find 1st codec */
368 i = 0;
369 for_each_child_of_node(node, np) {
370 dai_link = rsrc_priv_to_link(priv, i);
371
372 if (strcmp(np->name, "codec") == 0) {
373 ret = rsrc_card_parse_daifmt(node, np, priv,
374 dai_link, &daifmt);
375 if (ret < 0)
376 return ret;
377 break;
378 }
379 i++;
380 }
381
382 i = 0;
383 for_each_child_of_node(node, np) {
384 dai_link = rsrc_priv_to_link(priv, i);
385 dai_link->dai_fmt = daifmt;
386
387 is_fe = false;
388 if (strcmp(np->name, "cpu") == 0)
389 is_fe = true;
390
391 ret = rsrc_card_dai_sub_link_of(node, np, priv, i, is_fe);
392 if (ret < 0)
393 return ret;
394 i++;
395 }
396
397 return 0;
398 }
399
400 static int rsrc_card_parse_of(struct device_node *node,
401 struct rsrc_card_priv *priv,
402 struct device *dev)
403 {
404 const struct rsrc_card_of_data *of_data = of_device_get_match_data(dev);
405 struct rsrc_card_dai *props;
406 struct snd_soc_dai_link *links;
407 int ret;
408 int num;
409
410 if (!node)
411 return -EINVAL;
412
413 num = of_get_child_count(node);
414 props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
415 links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
416 if (!props || !links)
417 return -ENOMEM;
418
419 priv->dai_props = props;
420 priv->dai_link = links;
421 priv->dai_num = num;
422
423 /* Init snd_soc_card */
424 priv->snd_card.owner = THIS_MODULE;
425 priv->snd_card.dev = dev;
426 priv->snd_card.dai_link = priv->dai_link;
427 priv->snd_card.num_links = num;
428 priv->snd_card.codec_conf = &priv->codec_conf;
429 priv->snd_card.num_configs = 1;
430
431 if (of_data) {
432 priv->snd_card.of_dapm_routes = of_data->routes;
433 priv->snd_card.num_of_dapm_routes = of_data->num_routes;
434 } else {
435 snd_soc_of_parse_audio_routing(&priv->snd_card,
436 "audio-routing");
437 }
438
439 /* Parse the card name from DT */
440 snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
441
442 /* sampling rate convert */
443 of_property_read_u32(node, "convert-rate", &priv->convert_rate);
444
445 /* channels transfer */
446 of_property_read_u32(node, "convert-channels", &priv->convert_channels);
447
448 dev_dbg(dev, "New rsrc-audio-card: %s\n",
449 priv->snd_card.name ? priv->snd_card.name : "");
450 dev_dbg(dev, "SRC : convert_rate %d\n", priv->convert_rate);
451 dev_dbg(dev, "CTU : convert_channels %d\n", priv->convert_channels);
452
453 ret = rsrc_card_dai_link_of(node, priv);
454 if (ret < 0)
455 return ret;
456
457 if (!priv->snd_card.name)
458 priv->snd_card.name = priv->snd_card.dai_link->name;
459
460 return 0;
461 }
462
463 /* Decrease the reference count of the device nodes */
464 static int rsrc_card_unref(struct snd_soc_card *card)
465 {
466 struct snd_soc_dai_link *dai_link;
467 int num_links;
468
469 for (num_links = 0, dai_link = card->dai_link;
470 num_links < card->num_links;
471 num_links++, dai_link++) {
472 of_node_put(dai_link->cpu_of_node);
473 of_node_put(dai_link->codec_of_node);
474 }
475 return 0;
476 }
477
478 static int rsrc_card_probe(struct platform_device *pdev)
479 {
480 struct rsrc_card_priv *priv;
481 struct device_node *np = pdev->dev.of_node;
482 struct device *dev = &pdev->dev;
483 int ret;
484
485 /* Allocate the private data */
486 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
487 if (!priv)
488 return -ENOMEM;
489
490 ret = rsrc_card_parse_of(np, priv, dev);
491 if (ret < 0) {
492 if (ret != -EPROBE_DEFER)
493 dev_err(dev, "parse error %d\n", ret);
494 goto err;
495 }
496
497 snd_soc_card_set_drvdata(&priv->snd_card, priv);
498
499 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
500 if (ret >= 0)
501 return ret;
502 err:
503 rsrc_card_unref(&priv->snd_card);
504
505 return ret;
506 }
507
508 static int rsrc_card_remove(struct platform_device *pdev)
509 {
510 struct snd_soc_card *card = platform_get_drvdata(pdev);
511
512 return rsrc_card_unref(card);
513 }
514
515 static struct platform_driver rsrc_card = {
516 .driver = {
517 .name = "renesas-src-audio-card",
518 .of_match_table = rsrc_card_of_match,
519 },
520 .probe = rsrc_card_probe,
521 .remove = rsrc_card_remove,
522 };
523
524 module_platform_driver(rsrc_card);
525
526 MODULE_ALIAS("platform:renesas-src-audio-card");
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
529 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.042556 seconds and 6 git commands to generate.