2 * ASoC simple sound card support
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/gpio.h>
14 #include <linux/module.h>
16 #include <linux/of_gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/string.h>
19 #include <sound/jack.h>
20 #include <sound/simple_card.h>
21 #include <sound/soc-dai.h>
22 #include <sound/soc.h>
24 struct simple_card_data
{
25 struct snd_soc_card snd_card
;
26 struct simple_dai_props
{
27 struct asoc_simple_dai cpu_dai
;
28 struct asoc_simple_dai codec_dai
;
32 int gpio_hp_det_invert
;
34 int gpio_mic_det_invert
;
35 struct snd_soc_dai_link dai_link
[]; /* dynamically allocated */
38 #define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
39 #define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
40 #define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
42 static int asoc_simple_card_hw_params(struct snd_pcm_substream
*substream
,
43 struct snd_pcm_hw_params
*params
)
45 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
46 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
47 struct simple_card_data
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
52 mclk
= params_rate(params
) * priv
->mclk_fs
;
53 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
60 static struct snd_soc_ops asoc_simple_card_ops
= {
61 .hw_params
= asoc_simple_card_hw_params
,
64 static struct snd_soc_jack simple_card_hp_jack
;
65 static struct snd_soc_jack_pin simple_card_hp_jack_pins
[] = {
68 .mask
= SND_JACK_HEADPHONE
,
71 static struct snd_soc_jack_gpio simple_card_hp_jack_gpio
= {
72 .name
= "Headphone detection",
73 .report
= SND_JACK_HEADPHONE
,
77 static struct snd_soc_jack simple_card_mic_jack
;
78 static struct snd_soc_jack_pin simple_card_mic_jack_pins
[] = {
81 .mask
= SND_JACK_MICROPHONE
,
84 static struct snd_soc_jack_gpio simple_card_mic_jack_gpio
= {
85 .name
= "Mic detection",
86 .report
= SND_JACK_MICROPHONE
,
90 static int __asoc_simple_card_dai_init(struct snd_soc_dai
*dai
,
91 struct asoc_simple_dai
*set
)
96 ret
= snd_soc_dai_set_fmt(dai
, set
->fmt
);
97 if (ret
&& ret
!= -ENOTSUPP
) {
98 dev_err(dai
->dev
, "simple-card: set_fmt error\n");
104 ret
= snd_soc_dai_set_sysclk(dai
, 0, set
->sysclk
, 0);
105 if (ret
&& ret
!= -ENOTSUPP
) {
106 dev_err(dai
->dev
, "simple-card: set_sysclk error\n");
112 ret
= snd_soc_dai_set_tdm_slot(dai
, 0, 0,
115 if (ret
&& ret
!= -ENOTSUPP
) {
116 dev_err(dai
->dev
, "simple-card: set_tdm_slot error\n");
127 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime
*rtd
)
129 struct simple_card_data
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
130 struct snd_soc_dai
*codec
= rtd
->codec_dai
;
131 struct snd_soc_dai
*cpu
= rtd
->cpu_dai
;
132 struct simple_dai_props
*dai_props
;
135 num
= rtd
- rtd
->card
->rtd
;
136 dai_props
= &priv
->dai_props
[num
];
137 ret
= __asoc_simple_card_dai_init(codec
, &dai_props
->codec_dai
);
141 ret
= __asoc_simple_card_dai_init(cpu
, &dai_props
->cpu_dai
);
145 if (gpio_is_valid(priv
->gpio_hp_det
)) {
146 snd_soc_jack_new(codec
->codec
, "Headphones", SND_JACK_HEADPHONE
,
147 &simple_card_hp_jack
);
148 snd_soc_jack_add_pins(&simple_card_hp_jack
,
149 ARRAY_SIZE(simple_card_hp_jack_pins
),
150 simple_card_hp_jack_pins
);
152 simple_card_hp_jack_gpio
.gpio
= priv
->gpio_hp_det
;
153 simple_card_hp_jack_gpio
.invert
= priv
->gpio_hp_det_invert
;
154 snd_soc_jack_add_gpios(&simple_card_hp_jack
, 1,
155 &simple_card_hp_jack_gpio
);
158 if (gpio_is_valid(priv
->gpio_mic_det
)) {
159 snd_soc_jack_new(codec
->codec
, "Mic Jack", SND_JACK_MICROPHONE
,
160 &simple_card_mic_jack
);
161 snd_soc_jack_add_pins(&simple_card_mic_jack
,
162 ARRAY_SIZE(simple_card_mic_jack_pins
),
163 simple_card_mic_jack_pins
);
164 simple_card_mic_jack_gpio
.gpio
= priv
->gpio_mic_det
;
165 simple_card_mic_jack_gpio
.invert
= priv
->gpio_mic_det_invert
;
166 snd_soc_jack_add_gpios(&simple_card_mic_jack
, 1,
167 &simple_card_mic_jack_gpio
);
173 asoc_simple_card_sub_parse_of(struct device_node
*np
,
174 struct asoc_simple_dai
*dai
,
175 struct device_node
**p_node
,
179 struct of_phandle_args args
;
185 * Get node via "sound-dai = <&phandle port>"
186 * it will be used as xxx_of_node on soc_bind_dai_link()
188 ret
= of_parse_phandle_with_args(np
, "sound-dai",
189 "#sound-dai-cells", 0, &args
);
196 *args_count
= args
.args_count
;
199 ret
= snd_soc_of_get_dai_name(np
, name
);
204 ret
= snd_soc_of_parse_tdm_slot(np
, &dai
->slots
, &dai
->slot_width
);
209 * Parse dai->sysclk come from "clocks = <&xxx>"
210 * (if system has common clock)
211 * or "system-clock-frequency = <xxx>"
212 * or device's module clock.
214 if (of_property_read_bool(np
, "clocks")) {
215 clk
= of_clk_get(np
, 0);
221 dai
->sysclk
= clk_get_rate(clk
);
222 } else if (!of_property_read_u32(np
, "system-clock-frequency", &val
)) {
225 clk
= of_clk_get(args
.np
, 0);
227 dai
->sysclk
= clk_get_rate(clk
);
233 static int asoc_simple_card_parse_daifmt(struct device_node
*node
,
234 struct simple_card_data
*priv
,
235 struct device_node
*codec
,
236 char *prefix
, int idx
)
238 struct device
*dev
= simple_priv_to_dev(priv
);
239 struct device_node
*bitclkmaster
= NULL
;
240 struct device_node
*framemaster
= NULL
;
241 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, idx
);
242 struct asoc_simple_dai
*cpu_dai
= &dai_props
->cpu_dai
;
243 struct asoc_simple_dai
*codec_dai
= &dai_props
->codec_dai
;
246 daifmt
= snd_soc_of_parse_daifmt(node
, prefix
,
247 &bitclkmaster
, &framemaster
);
248 daifmt
&= ~SND_SOC_DAIFMT_MASTER_MASK
;
250 if (strlen(prefix
) && !bitclkmaster
&& !framemaster
) {
252 * No dai-link level and master setting was not found from
253 * sound node level, revert back to legacy DT parsing and
254 * take the settings from codec node.
256 dev_dbg(dev
, "Revert to legacy daifmt parsing\n");
258 cpu_dai
->fmt
= codec_dai
->fmt
=
259 snd_soc_of_parse_daifmt(codec
, NULL
, NULL
, NULL
) |
260 (daifmt
& ~SND_SOC_DAIFMT_CLOCK_MASK
);
262 if (codec
== bitclkmaster
)
263 daifmt
|= (codec
== framemaster
) ?
264 SND_SOC_DAIFMT_CBM_CFM
: SND_SOC_DAIFMT_CBM_CFS
;
266 daifmt
|= (codec
== framemaster
) ?
267 SND_SOC_DAIFMT_CBS_CFM
: SND_SOC_DAIFMT_CBS_CFS
;
269 cpu_dai
->fmt
= daifmt
;
270 codec_dai
->fmt
= daifmt
;
273 of_node_put(bitclkmaster
);
274 of_node_put(framemaster
);
279 static int asoc_simple_card_dai_link_of(struct device_node
*node
,
280 struct simple_card_data
*priv
,
282 bool is_top_level_node
)
284 struct device
*dev
= simple_priv_to_dev(priv
);
285 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, idx
);
286 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, idx
);
287 struct device_node
*cpu
= NULL
;
288 struct device_node
*codec
= NULL
;
294 /* For single DAI link & old style of DT node */
295 if (is_top_level_node
)
296 prefix
= "simple-audio-card,";
298 snprintf(prop
, sizeof(prop
), "%scpu", prefix
);
299 cpu
= of_get_child_by_name(node
, prop
);
301 snprintf(prop
, sizeof(prop
), "%scodec", prefix
);
302 codec
= of_get_child_by_name(node
, prop
);
304 if (!cpu
|| !codec
) {
306 dev_err(dev
, "%s: Can't find %s DT node\n", __func__
, prop
);
307 goto dai_link_of_err
;
310 ret
= asoc_simple_card_parse_daifmt(node
, priv
,
313 goto dai_link_of_err
;
315 ret
= asoc_simple_card_sub_parse_of(cpu
, &dai_props
->cpu_dai
,
316 &dai_link
->cpu_of_node
,
317 &dai_link
->cpu_dai_name
,
320 goto dai_link_of_err
;
322 ret
= asoc_simple_card_sub_parse_of(codec
, &dai_props
->codec_dai
,
323 &dai_link
->codec_of_node
,
324 &dai_link
->codec_dai_name
, NULL
);
326 goto dai_link_of_err
;
328 if (!dai_link
->cpu_dai_name
|| !dai_link
->codec_dai_name
) {
330 goto dai_link_of_err
;
333 /* Simple Card assumes platform == cpu */
334 dai_link
->platform_of_node
= dai_link
->cpu_of_node
;
336 /* DAI link name is created from CPU/CODEC dai name */
337 name
= devm_kzalloc(dev
,
338 strlen(dai_link
->cpu_dai_name
) +
339 strlen(dai_link
->codec_dai_name
) + 2,
341 sprintf(name
, "%s-%s", dai_link
->cpu_dai_name
,
342 dai_link
->codec_dai_name
);
343 dai_link
->name
= dai_link
->stream_name
= name
;
344 dai_link
->ops
= &asoc_simple_card_ops
;
345 dai_link
->init
= asoc_simple_card_dai_init
;
347 dev_dbg(dev
, "\tname : %s\n", dai_link
->stream_name
);
348 dev_dbg(dev
, "\tcpu : %s / %04x / %d\n",
349 dai_link
->cpu_dai_name
,
350 dai_props
->cpu_dai
.fmt
,
351 dai_props
->cpu_dai
.sysclk
);
352 dev_dbg(dev
, "\tcodec : %s / %04x / %d\n",
353 dai_link
->codec_dai_name
,
354 dai_props
->codec_dai
.fmt
,
355 dai_props
->codec_dai
.sysclk
);
358 * In soc_bind_dai_link() will check cpu name after
359 * of_node matching if dai_link has cpu_dai_name.
360 * but, it will never match if name was created by
361 * fmt_single_name() remove cpu_dai_name if cpu_args
364 * fmt_multiple_name()
367 dai_link
->cpu_dai_name
= NULL
;
376 static int asoc_simple_card_parse_of(struct device_node
*node
,
377 struct simple_card_data
*priv
)
379 struct device
*dev
= simple_priv_to_dev(priv
);
380 enum of_gpio_flags flags
;
387 /* Parse the card name from DT */
388 snd_soc_of_parse_card_name(&priv
->snd_card
, "simple-audio-card,name");
390 /* The off-codec widgets */
391 if (of_property_read_bool(node
, "simple-audio-card,widgets")) {
392 ret
= snd_soc_of_parse_audio_simple_widgets(&priv
->snd_card
,
393 "simple-audio-card,widgets");
399 if (of_property_read_bool(node
, "simple-audio-card,routing")) {
400 ret
= snd_soc_of_parse_audio_routing(&priv
->snd_card
,
401 "simple-audio-card,routing");
406 /* Factor to mclk, used in hw_params() */
407 ret
= of_property_read_u32(node
, "simple-audio-card,mclk-fs", &val
);
411 dev_dbg(dev
, "New simple-card: %s\n", priv
->snd_card
.name
?
412 priv
->snd_card
.name
: "");
414 /* Single/Muti DAI link(s) & New style of DT node */
415 if (of_get_child_by_name(node
, "simple-audio-card,dai-link")) {
416 struct device_node
*np
= NULL
;
419 for_each_child_of_node(node
, np
) {
420 dev_dbg(dev
, "\tlink %d:\n", i
);
421 ret
= asoc_simple_card_dai_link_of(np
, priv
,
430 /* For single DAI link & old style of DT node */
431 ret
= asoc_simple_card_dai_link_of(node
, priv
, 0, true);
436 priv
->gpio_hp_det
= of_get_named_gpio_flags(node
,
437 "simple-audio-card,hp-det-gpio", 0, &flags
);
438 priv
->gpio_hp_det_invert
= !!(flags
& OF_GPIO_ACTIVE_LOW
);
439 if (priv
->gpio_hp_det
== -EPROBE_DEFER
)
440 return -EPROBE_DEFER
;
442 priv
->gpio_mic_det
= of_get_named_gpio_flags(node
,
443 "simple-audio-card,mic-det-gpio", 0, &flags
);
444 priv
->gpio_mic_det_invert
= !!(flags
& OF_GPIO_ACTIVE_LOW
);
445 if (priv
->gpio_mic_det
== -EPROBE_DEFER
)
446 return -EPROBE_DEFER
;
448 if (!priv
->snd_card
.name
)
449 priv
->snd_card
.name
= priv
->snd_card
.dai_link
->name
;
454 /* Decrease the reference count of the device nodes */
455 static int asoc_simple_card_unref(struct platform_device
*pdev
)
457 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
458 struct snd_soc_dai_link
*dai_link
;
461 for (num_links
= 0, dai_link
= card
->dai_link
;
462 num_links
< card
->num_links
;
463 num_links
++, dai_link
++) {
464 of_node_put(dai_link
->cpu_of_node
);
465 of_node_put(dai_link
->codec_of_node
);
470 static int asoc_simple_card_probe(struct platform_device
*pdev
)
472 struct simple_card_data
*priv
;
473 struct snd_soc_dai_link
*dai_link
;
474 struct device_node
*np
= pdev
->dev
.of_node
;
475 struct device
*dev
= &pdev
->dev
;
478 /* Get the number of DAI links */
479 if (np
&& of_get_child_by_name(np
, "simple-audio-card,dai-link"))
480 num_links
= of_get_child_count(np
);
484 /* Allocate the private data and the DAI link array */
485 priv
= devm_kzalloc(dev
,
486 sizeof(*priv
) + sizeof(*dai_link
) * num_links
,
491 /* Init snd_soc_card */
492 priv
->snd_card
.owner
= THIS_MODULE
;
493 priv
->snd_card
.dev
= dev
;
494 dai_link
= priv
->dai_link
;
495 priv
->snd_card
.dai_link
= dai_link
;
496 priv
->snd_card
.num_links
= num_links
;
498 priv
->gpio_hp_det
= -ENOENT
;
499 priv
->gpio_mic_det
= -ENOENT
;
501 /* Get room for the other properties */
502 priv
->dai_props
= devm_kzalloc(dev
,
503 sizeof(*priv
->dai_props
) * num_links
,
505 if (!priv
->dai_props
)
508 if (np
&& of_device_is_available(np
)) {
510 ret
= asoc_simple_card_parse_of(np
, priv
);
512 if (ret
!= -EPROBE_DEFER
)
513 dev_err(dev
, "parse error %d\n", ret
);
518 struct asoc_simple_card_info
*cinfo
;
520 cinfo
= dev
->platform_data
;
522 dev_err(dev
, "no info for asoc-simple-card\n");
527 !cinfo
->codec_dai
.name
||
530 !cinfo
->cpu_dai
.name
) {
531 dev_err(dev
, "insufficient asoc_simple_card_info settings\n");
535 priv
->snd_card
.name
= (cinfo
->card
) ? cinfo
->card
: cinfo
->name
;
536 dai_link
->name
= cinfo
->name
;
537 dai_link
->stream_name
= cinfo
->name
;
538 dai_link
->platform_name
= cinfo
->platform
;
539 dai_link
->codec_name
= cinfo
->codec
;
540 dai_link
->cpu_dai_name
= cinfo
->cpu_dai
.name
;
541 dai_link
->codec_dai_name
= cinfo
->codec_dai
.name
;
542 dai_link
->init
= asoc_simple_card_dai_init
;
543 memcpy(&priv
->dai_props
->cpu_dai
, &cinfo
->cpu_dai
,
544 sizeof(priv
->dai_props
->cpu_dai
));
545 memcpy(&priv
->dai_props
->codec_dai
, &cinfo
->codec_dai
,
546 sizeof(priv
->dai_props
->codec_dai
));
548 priv
->dai_props
->cpu_dai
.fmt
|= cinfo
->daifmt
;
549 priv
->dai_props
->codec_dai
.fmt
|= cinfo
->daifmt
;
552 snd_soc_card_set_drvdata(&priv
->snd_card
, priv
);
554 ret
= devm_snd_soc_register_card(&pdev
->dev
, &priv
->snd_card
);
559 asoc_simple_card_unref(pdev
);
563 static int asoc_simple_card_remove(struct platform_device
*pdev
)
565 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
566 struct simple_card_data
*priv
= snd_soc_card_get_drvdata(card
);
568 if (gpio_is_valid(priv
->gpio_hp_det
))
569 snd_soc_jack_free_gpios(&simple_card_hp_jack
, 1,
570 &simple_card_hp_jack_gpio
);
571 if (gpio_is_valid(priv
->gpio_mic_det
))
572 snd_soc_jack_free_gpios(&simple_card_mic_jack
, 1,
573 &simple_card_mic_jack_gpio
);
575 return asoc_simple_card_unref(pdev
);
578 static const struct of_device_id asoc_simple_of_match
[] = {
579 { .compatible
= "simple-audio-card", },
582 MODULE_DEVICE_TABLE(of
, asoc_simple_of_match
);
584 static struct platform_driver asoc_simple_card
= {
586 .name
= "asoc-simple-card",
587 .of_match_table
= asoc_simple_of_match
,
589 .probe
= asoc_simple_card_probe
,
590 .remove
= asoc_simple_card_remove
,
593 module_platform_driver(asoc_simple_card
);
595 MODULE_ALIAS("platform:asoc-simple-card");
596 MODULE_LICENSE("GPL");
597 MODULE_DESCRIPTION("ASoC Simple Sound Card");
598 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");