Commit | Line | Data |
---|---|---|
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 | |
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 }, | |
b7419dd7 | 45 | { .compatible = "renesas,rsrc-card", }, |
415f1cb2 KM |
46 | {}, |
47 | }; | |
48 | MODULE_DEVICE_TABLE(of, rsrc_card_of_match); | |
49 | ||
415f1cb2 KM |
50 | #define IDX_CPU 0 |
51 | #define IDX_CODEC 1 | |
52 | struct 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 | |
65 | static 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 | ||
75 | static 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 | ||
85 | static struct snd_soc_ops rsrc_card_ops = { | |
86 | .startup = rsrc_card_startup, | |
87 | .shutdown = rsrc_card_shutdown, | |
88 | }; | |
89 | ||
04700027 | 90 | static 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 | ||
127 | err: | |
128 | return ret; | |
129 | } | |
130 | ||
af7e2be9 KM |
131 | static 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 |
151 | static 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 | |
8a99a6bd KM |
193 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
194 | "fe.%s", | |
195 | dai_link->cpu_dai_name); | |
196 | if (ret < 0) | |
197 | return ret; | |
04700027 KM |
198 | |
199 | /* | |
200 | * In soc_bind_dai_link() will check cpu name after | |
201 | * of_node matching if dai_link has cpu_dai_name. | |
202 | * but, it will never match if name was created by | |
203 | * fmt_single_name() remove cpu_dai_name if cpu_args | |
204 | * was 0. See: | |
205 | * fmt_single_name() | |
206 | * fmt_multiple_name() | |
207 | */ | |
208 | if (!args.args_count) | |
209 | dai_link->cpu_dai_name = NULL; | |
210 | } else { | |
04700027 | 211 | const struct rsrc_card_of_data *of_data; |
415f1cb2 | 212 | |
6a16c176 | 213 | of_data = of_device_get_match_data(dev); |
415f1cb2 | 214 | |
04700027 KM |
215 | /* FE is dummy */ |
216 | dai_link->cpu_of_node = NULL; | |
217 | dai_link->cpu_dai_name = "snd-soc-dummy-dai"; | |
218 | dai_link->cpu_name = "snd-soc-dummy"; | |
415f1cb2 | 219 | |
04700027 KM |
220 | /* BE settings */ |
221 | dai_link->no_pcm = 1; | |
222 | dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup; | |
223 | dai_link->codec_of_node = args.np; | |
575f1f92 KM |
224 | ret = snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name); |
225 | if (ret < 0) | |
226 | return ret; | |
04700027 | 227 | |
8a99a6bd KM |
228 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
229 | "be.%s", | |
230 | dai_link->codec_dai_name); | |
231 | if (ret < 0) | |
232 | return ret; | |
233 | ||
04700027 | 234 | /* additional name prefix */ |
b7419dd7 KM |
235 | if (of_data) { |
236 | priv->codec_conf.of_node = dai_link->codec_of_node; | |
237 | priv->codec_conf.name_prefix = of_data->prefix; | |
238 | } else { | |
239 | snd_soc_of_parse_audio_prefix(&priv->snd_card, | |
240 | &priv->codec_conf, | |
241 | dai_link->codec_of_node, | |
242 | "audio-prefix"); | |
243 | } | |
415f1cb2 KM |
244 | } |
245 | ||
04700027 KM |
246 | /* Simple Card assumes platform == cpu */ |
247 | dai_link->platform_of_node = dai_link->cpu_of_node; | |
248 | dai_link->dpcm_playback = 1; | |
249 | dai_link->dpcm_capture = 1; | |
04700027 KM |
250 | dai_link->ops = &rsrc_card_ops; |
251 | dai_link->init = rsrc_card_dai_init; | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | static int rsrc_card_parse_clk(struct device_node *np, | |
257 | struct rsrc_card_priv *priv, | |
258 | int idx, bool is_fe) | |
259 | { | |
260 | struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx); | |
303c3be4 | 261 | struct asoc_simple_dai *dai_props = rsrc_priv_to_props(priv, idx); |
04700027 KM |
262 | struct clk *clk; |
263 | struct device_node *of_np = is_fe ? dai_link->cpu_of_node : | |
264 | dai_link->codec_of_node; | |
265 | u32 val; | |
266 | ||
415f1cb2 KM |
267 | /* |
268 | * Parse dai->sysclk come from "clocks = <&xxx>" | |
269 | * (if system has common clock) | |
270 | * or "system-clock-frequency = <xxx>" | |
271 | * or device's module clock. | |
272 | */ | |
273 | if (of_property_read_bool(np, "clocks")) { | |
274 | clk = of_clk_get(np, 0); | |
061015f7 KM |
275 | if (IS_ERR(clk)) |
276 | return PTR_ERR(clk); | |
415f1cb2 | 277 | |
04700027 KM |
278 | dai_props->sysclk = clk_get_rate(clk); |
279 | dai_props->clk = clk; | |
415f1cb2 | 280 | } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) { |
04700027 | 281 | dai_props->sysclk = val; |
415f1cb2 | 282 | } else { |
04700027 | 283 | clk = of_clk_get(of_np, 0); |
415f1cb2 | 284 | if (!IS_ERR(clk)) |
04700027 | 285 | dai_props->sysclk = clk_get_rate(clk); |
415f1cb2 KM |
286 | } |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
af998f85 KM |
291 | static int rsrc_card_dai_sub_link_of(struct device_node *node, |
292 | struct device_node *np, | |
293 | struct rsrc_card_priv *priv, | |
294 | int idx, bool is_fe) | |
415f1cb2 KM |
295 | { |
296 | struct device *dev = rsrc_priv_to_dev(priv); | |
ae638b72 | 297 | struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx); |
303c3be4 | 298 | struct asoc_simple_dai *dai_props = rsrc_priv_to_props(priv, idx); |
04700027 | 299 | int ret; |
415f1cb2 | 300 | |
04700027 | 301 | ret = rsrc_card_parse_links(np, priv, idx, is_fe); |
415f1cb2 | 302 | if (ret < 0) |
04700027 | 303 | return ret; |
415f1cb2 | 304 | |
04700027 KM |
305 | ret = rsrc_card_parse_clk(np, priv, idx, is_fe); |
306 | if (ret < 0) | |
307 | return ret; | |
415f1cb2 | 308 | |
04700027 | 309 | dev_dbg(dev, "\t%s / %04x / %d\n", |
8a99a6bd | 310 | dai_link->name, |
ae638b72 | 311 | dai_link->dai_fmt, |
04700027 | 312 | dai_props->sysclk); |
415f1cb2 KM |
313 | |
314 | return ret; | |
315 | } | |
316 | ||
af998f85 KM |
317 | static int rsrc_card_dai_link_of(struct device_node *node, |
318 | struct rsrc_card_priv *priv) | |
319 | { | |
d6a4a9a4 | 320 | struct device *dev = rsrc_priv_to_dev(priv); |
af998f85 KM |
321 | struct snd_soc_dai_link *dai_link; |
322 | struct device_node *np; | |
323 | unsigned int daifmt = 0; | |
324 | int ret, i; | |
325 | bool is_fe; | |
326 | ||
327 | /* find 1st codec */ | |
328 | i = 0; | |
329 | for_each_child_of_node(node, np) { | |
330 | dai_link = rsrc_priv_to_link(priv, i); | |
331 | ||
332 | if (strcmp(np->name, "codec") == 0) { | |
d6a4a9a4 KM |
333 | ret = asoc_simple_card_parse_daifmt(dev, node, np, |
334 | NULL, &daifmt); | |
af998f85 KM |
335 | if (ret < 0) |
336 | return ret; | |
337 | break; | |
338 | } | |
339 | i++; | |
340 | } | |
341 | ||
342 | i = 0; | |
343 | for_each_child_of_node(node, np) { | |
344 | dai_link = rsrc_priv_to_link(priv, i); | |
345 | dai_link->dai_fmt = daifmt; | |
346 | ||
347 | is_fe = false; | |
348 | if (strcmp(np->name, "cpu") == 0) | |
349 | is_fe = true; | |
350 | ||
351 | ret = rsrc_card_dai_sub_link_of(node, np, priv, i, is_fe); | |
352 | if (ret < 0) | |
353 | return ret; | |
354 | i++; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
415f1cb2 | 360 | static int rsrc_card_parse_of(struct device_node *node, |
3c7e64dd KM |
361 | struct rsrc_card_priv *priv, |
362 | struct device *dev) | |
415f1cb2 | 363 | { |
6a16c176 | 364 | const struct rsrc_card_of_data *of_data = of_device_get_match_data(dev); |
303c3be4 | 365 | struct asoc_simple_dai *props; |
3433bf07 | 366 | struct snd_soc_dai_link *links; |
415f1cb2 | 367 | int ret; |
af998f85 | 368 | int num; |
415f1cb2 KM |
369 | |
370 | if (!node) | |
371 | return -EINVAL; | |
372 | ||
3433bf07 KM |
373 | num = of_get_child_count(node); |
374 | props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL); | |
375 | links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL); | |
376 | if (!props || !links) | |
377 | return -ENOMEM; | |
378 | ||
379 | priv->dai_props = props; | |
380 | priv->dai_link = links; | |
3433bf07 | 381 | |
3c7e64dd KM |
382 | /* Init snd_soc_card */ |
383 | priv->snd_card.owner = THIS_MODULE; | |
384 | priv->snd_card.dev = dev; | |
385 | priv->snd_card.dai_link = priv->dai_link; | |
3433bf07 | 386 | priv->snd_card.num_links = num; |
3c7e64dd KM |
387 | priv->snd_card.codec_conf = &priv->codec_conf; |
388 | priv->snd_card.num_configs = 1; | |
b7419dd7 KM |
389 | |
390 | if (of_data) { | |
391 | priv->snd_card.of_dapm_routes = of_data->routes; | |
392 | priv->snd_card.num_of_dapm_routes = of_data->num_routes; | |
393 | } else { | |
394 | snd_soc_of_parse_audio_routing(&priv->snd_card, | |
395 | "audio-routing"); | |
396 | } | |
415f1cb2 | 397 | |
af7e2be9 KM |
398 | /* sampling rate convert */ |
399 | of_property_read_u32(node, "convert-rate", &priv->convert_rate); | |
400 | ||
f90432fc KM |
401 | /* channels transfer */ |
402 | of_property_read_u32(node, "convert-channels", &priv->convert_channels); | |
403 | ||
404 | dev_dbg(dev, "New rsrc-audio-card: %s\n", | |
405 | priv->snd_card.name ? priv->snd_card.name : ""); | |
406 | dev_dbg(dev, "SRC : convert_rate %d\n", priv->convert_rate); | |
407 | dev_dbg(dev, "CTU : convert_channels %d\n", priv->convert_channels); | |
415f1cb2 | 408 | |
af998f85 KM |
409 | ret = rsrc_card_dai_link_of(node, priv); |
410 | if (ret < 0) | |
411 | return ret; | |
415f1cb2 | 412 | |
53ae918f KM |
413 | ret = asoc_simple_card_parse_card_name(&priv->snd_card, "card-"); |
414 | if (ret < 0) | |
415 | return ret; | |
415f1cb2 KM |
416 | |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* Decrease the reference count of the device nodes */ | |
421 | static int rsrc_card_unref(struct snd_soc_card *card) | |
422 | { | |
423 | struct snd_soc_dai_link *dai_link; | |
424 | int num_links; | |
425 | ||
426 | for (num_links = 0, dai_link = card->dai_link; | |
427 | num_links < card->num_links; | |
428 | num_links++, dai_link++) { | |
429 | of_node_put(dai_link->cpu_of_node); | |
430 | of_node_put(dai_link->codec_of_node); | |
431 | } | |
432 | return 0; | |
433 | } | |
434 | ||
435 | static int rsrc_card_probe(struct platform_device *pdev) | |
436 | { | |
437 | struct rsrc_card_priv *priv; | |
415f1cb2 KM |
438 | struct device_node *np = pdev->dev.of_node; |
439 | struct device *dev = &pdev->dev; | |
440 | int ret; | |
441 | ||
442 | /* Allocate the private data */ | |
443 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
444 | if (!priv) | |
445 | return -ENOMEM; | |
446 | ||
3c7e64dd | 447 | ret = rsrc_card_parse_of(np, priv, dev); |
415f1cb2 KM |
448 | if (ret < 0) { |
449 | if (ret != -EPROBE_DEFER) | |
450 | dev_err(dev, "parse error %d\n", ret); | |
451 | goto err; | |
452 | } | |
453 | ||
454 | snd_soc_card_set_drvdata(&priv->snd_card, priv); | |
455 | ||
456 | ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card); | |
457 | if (ret >= 0) | |
458 | return ret; | |
459 | err: | |
460 | rsrc_card_unref(&priv->snd_card); | |
461 | ||
462 | return ret; | |
463 | } | |
464 | ||
465 | static int rsrc_card_remove(struct platform_device *pdev) | |
466 | { | |
467 | struct snd_soc_card *card = platform_get_drvdata(pdev); | |
468 | ||
469 | return rsrc_card_unref(card); | |
470 | } | |
471 | ||
472 | static struct platform_driver rsrc_card = { | |
473 | .driver = { | |
474 | .name = "renesas-src-audio-card", | |
475 | .of_match_table = rsrc_card_of_match, | |
476 | }, | |
477 | .probe = rsrc_card_probe, | |
478 | .remove = rsrc_card_remove, | |
479 | }; | |
480 | ||
481 | module_platform_driver(rsrc_card); | |
482 | ||
483 | MODULE_ALIAS("platform:renesas-src-audio-card"); | |
484 | MODULE_LICENSE("GPL"); | |
485 | MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card"); | |
486 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |