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