1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 // Copyright (c) 2018 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
9 #include <sound/soc-dai.h>
14 struct snd_soc_card card
;
18 struct axg_dai_link_tdm_mask
{
23 struct axg_dai_link_tdm_data
{
26 unsigned int slot_width
;
29 struct axg_dai_link_tdm_mask
*codec_masks
;
33 * Base params for the codec to codec links
34 * Those will be over-written by the CPU side of the link
36 static const struct snd_soc_pcm_stream codec_params
= {
37 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
44 #define PREFIX "amlogic,"
46 static int axg_card_reallocate_links(struct axg_card
*priv
,
47 unsigned int num_links
)
49 struct snd_soc_dai_link
*links
;
52 links
= krealloc(priv
->card
.dai_link
,
53 num_links
* sizeof(*priv
->card
.dai_link
),
54 GFP_KERNEL
| __GFP_ZERO
);
55 ldata
= krealloc(priv
->link_data
,
56 num_links
* sizeof(*priv
->link_data
),
57 GFP_KERNEL
| __GFP_ZERO
);
59 if (!links
|| !ldata
) {
60 dev_err(priv
->card
.dev
, "failed to allocate links\n");
64 priv
->card
.dai_link
= links
;
65 priv
->link_data
= ldata
;
66 priv
->card
.num_links
= num_links
;
70 static int axg_card_parse_dai(struct snd_soc_card
*card
,
71 struct device_node
*node
,
72 struct device_node
**dai_of_node
,
73 const char **dai_name
)
75 struct of_phandle_args args
;
78 if (!dai_name
|| !dai_of_node
|| !node
)
81 ret
= of_parse_phandle_with_args(node
, "sound-dai",
82 "#sound-dai-cells", 0, &args
);
84 if (ret
!= -EPROBE_DEFER
)
85 dev_err(card
->dev
, "can't parse dai %d\n", ret
);
88 *dai_of_node
= args
.np
;
90 return snd_soc_get_dai_name(&args
, dai_name
);
93 static int axg_card_set_link_name(struct snd_soc_card
*card
,
94 struct snd_soc_dai_link
*link
,
95 struct device_node
*node
,
98 char *name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s.%s",
99 prefix
, node
->full_name
);
104 link
->stream_name
= name
;
109 static void axg_card_clean_references(struct axg_card
*priv
)
111 struct snd_soc_card
*card
= &priv
->card
;
112 struct snd_soc_dai_link
*link
;
113 struct snd_soc_dai_link_component
*codec
;
114 struct snd_soc_aux_dev
*aux
;
117 if (card
->dai_link
) {
118 for_each_card_prelinks(card
, i
, link
) {
120 of_node_put(link
->cpus
->of_node
);
121 for_each_link_codecs(link
, j
, codec
)
122 of_node_put(codec
->of_node
);
127 for_each_card_pre_auxs(card
, i
, aux
)
128 of_node_put(aux
->dlc
.of_node
);
131 kfree(card
->dai_link
);
132 kfree(priv
->link_data
);
135 static int axg_card_add_aux_devices(struct snd_soc_card
*card
)
137 struct device_node
*node
= card
->dev
->of_node
;
138 struct snd_soc_aux_dev
*aux
;
141 num
= of_count_phandle_with_args(node
, "audio-aux-devs", NULL
);
142 if (num
== -ENOENT
) {
144 * It is ok to have no auxiliary devices but for this card it
145 * is a strange situtation. Let's warn the about it.
147 dev_warn(card
->dev
, "card has no auxiliary devices\n");
149 } else if (num
< 0) {
150 dev_err(card
->dev
, "error getting auxiliary devices: %d\n",
155 aux
= devm_kcalloc(card
->dev
, num
, sizeof(*aux
), GFP_KERNEL
);
159 card
->num_aux_devs
= num
;
161 for_each_card_pre_auxs(card
, i
, aux
) {
163 of_parse_phandle(node
, "audio-aux-devs", i
);
164 if (!aux
->dlc
.of_node
)
171 static int axg_card_tdm_be_hw_params(struct snd_pcm_substream
*substream
,
172 struct snd_pcm_hw_params
*params
)
174 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
175 struct axg_card
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
176 struct axg_dai_link_tdm_data
*be
=
177 (struct axg_dai_link_tdm_data
*)priv
->link_data
[rtd
->num
];
178 struct snd_soc_dai
*codec_dai
;
183 mclk
= params_rate(params
) * be
->mclk_fs
;
185 for_each_rtd_codec_dai(rtd
, i
, codec_dai
) {
186 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
188 if (ret
&& ret
!= -ENOTSUPP
)
192 ret
= snd_soc_dai_set_sysclk(rtd
->cpu_dai
, 0, mclk
,
194 if (ret
&& ret
!= -ENOTSUPP
)
201 static const struct snd_soc_ops axg_card_tdm_be_ops
= {
202 .hw_params
= axg_card_tdm_be_hw_params
,
205 static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime
*rtd
)
207 struct axg_card
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
208 struct axg_dai_link_tdm_data
*be
=
209 (struct axg_dai_link_tdm_data
*)priv
->link_data
[rtd
->num
];
210 struct snd_soc_dai
*codec_dai
;
213 for_each_rtd_codec_dai(rtd
, i
, codec_dai
) {
214 ret
= snd_soc_dai_set_tdm_slot(codec_dai
,
215 be
->codec_masks
[i
].tx
,
216 be
->codec_masks
[i
].rx
,
217 be
->slots
, be
->slot_width
);
218 if (ret
&& ret
!= -ENOTSUPP
) {
219 dev_err(codec_dai
->dev
,
220 "setting tdm link slots failed\n");
225 ret
= axg_tdm_set_tdm_slots(rtd
->cpu_dai
, be
->tx_mask
, be
->rx_mask
,
226 be
->slots
, be
->slot_width
);
228 dev_err(rtd
->cpu_dai
->dev
, "setting tdm link slots failed\n");
235 static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime
*rtd
)
237 struct axg_card
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
238 struct axg_dai_link_tdm_data
*be
=
239 (struct axg_dai_link_tdm_data
*)priv
->link_data
[rtd
->num
];
242 /* The loopback rx_mask is the pad tx_mask */
243 ret
= axg_tdm_set_tdm_slots(rtd
->cpu_dai
, NULL
, be
->tx_mask
,
244 be
->slots
, be
->slot_width
);
246 dev_err(rtd
->cpu_dai
->dev
, "setting tdm link slots failed\n");
253 static int axg_card_add_tdm_loopback(struct snd_soc_card
*card
,
256 struct axg_card
*priv
= snd_soc_card_get_drvdata(card
);
257 struct snd_soc_dai_link
*pad
= &card
->dai_link
[*index
];
258 struct snd_soc_dai_link
*lb
;
259 struct snd_soc_dai_link_component
*dlc
;
263 ret
= axg_card_reallocate_links(priv
, card
->num_links
+ 1);
267 lb
= &card
->dai_link
[*index
+ 1];
269 lb
->name
= kasprintf(GFP_KERNEL
, "%s-lb", pad
->name
);
273 dlc
= devm_kzalloc(card
->dev
, 2 * sizeof(*dlc
), GFP_KERNEL
);
278 lb
->codecs
= &dlc
[1];
282 lb
->stream_name
= lb
->name
;
283 lb
->cpus
->of_node
= pad
->cpus
->of_node
;
284 lb
->cpus
->dai_name
= "TDM Loopback";
285 lb
->codecs
->name
= "snd-soc-dummy";
286 lb
->codecs
->dai_name
= "snd-soc-dummy-dai";
287 lb
->dpcm_capture
= 1;
289 lb
->ops
= &axg_card_tdm_be_ops
;
290 lb
->init
= axg_card_tdm_dai_lb_init
;
292 /* Provide the same link data to the loopback */
293 priv
->link_data
[*index
+ 1] = priv
->link_data
[*index
];
296 * axg_card_clean_references() will iterate over this link,
297 * make sure the node count is balanced
299 of_node_get(lb
->cpus
->of_node
);
301 /* Let add_links continue where it should */
307 static unsigned int axg_card_parse_daifmt(struct device_node
*node
,
308 struct device_node
*cpu_node
)
310 struct device_node
*bitclkmaster
= NULL
;
311 struct device_node
*framemaster
= NULL
;
314 daifmt
= snd_soc_of_parse_daifmt(node
, PREFIX
,
315 &bitclkmaster
, &framemaster
);
316 daifmt
&= ~SND_SOC_DAIFMT_MASTER_MASK
;
318 /* If no master is provided, default to cpu master */
319 if (!bitclkmaster
|| bitclkmaster
== cpu_node
) {
320 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
321 SND_SOC_DAIFMT_CBS_CFS
: SND_SOC_DAIFMT_CBS_CFM
;
323 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
324 SND_SOC_DAIFMT_CBM_CFS
: SND_SOC_DAIFMT_CBM_CFM
;
327 of_node_put(bitclkmaster
);
328 of_node_put(framemaster
);
333 static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card
*card
,
334 struct snd_soc_dai_link
*link
,
335 struct device_node
*node
,
336 struct axg_dai_link_tdm_data
*be
)
342 be
->tx_mask
= devm_kcalloc(card
->dev
, AXG_TDM_NUM_LANES
,
343 sizeof(*be
->tx_mask
), GFP_KERNEL
);
344 be
->rx_mask
= devm_kcalloc(card
->dev
, AXG_TDM_NUM_LANES
,
345 sizeof(*be
->rx_mask
), GFP_KERNEL
);
346 if (!be
->tx_mask
|| !be
->rx_mask
)
349 for (i
= 0, tx
= 0; i
< AXG_TDM_NUM_LANES
; i
++) {
350 snprintf(propname
, 32, "dai-tdm-slot-tx-mask-%d", i
);
351 snd_soc_of_get_slot_mask(node
, propname
, &be
->tx_mask
[i
]);
352 tx
= max(tx
, be
->tx_mask
[i
]);
355 /* Disable playback is the interface has no tx slots */
357 link
->dpcm_playback
= 0;
359 for (i
= 0, rx
= 0; i
< AXG_TDM_NUM_LANES
; i
++) {
360 snprintf(propname
, 32, "dai-tdm-slot-rx-mask-%d", i
);
361 snd_soc_of_get_slot_mask(node
, propname
, &be
->rx_mask
[i
]);
362 rx
= max(rx
, be
->rx_mask
[i
]);
365 /* Disable capture is the interface has no rx slots */
367 link
->dpcm_capture
= 0;
369 /* ... but the interface should at least have one of them */
371 dev_err(card
->dev
, "tdm link has no cpu slots\n");
375 of_property_read_u32(node
, "dai-tdm-slot-num", &be
->slots
);
378 * If the slot number is not provided, set it such as it
379 * accommodates the largest mask
381 be
->slots
= fls(max(tx
, rx
));
382 } else if (be
->slots
< fls(max(tx
, rx
)) || be
->slots
> 32) {
384 * Error if the slots can't accommodate the largest mask or
385 * if it is just too big
387 dev_err(card
->dev
, "bad slot number\n");
391 of_property_read_u32(node
, "dai-tdm-slot-width", &be
->slot_width
);
396 static int axg_card_parse_codecs_masks(struct snd_soc_card
*card
,
397 struct snd_soc_dai_link
*link
,
398 struct device_node
*node
,
399 struct axg_dai_link_tdm_data
*be
)
401 struct axg_dai_link_tdm_mask
*codec_mask
;
402 struct device_node
*np
;
404 codec_mask
= devm_kcalloc(card
->dev
, link
->num_codecs
,
405 sizeof(*codec_mask
), GFP_KERNEL
);
409 be
->codec_masks
= codec_mask
;
411 for_each_child_of_node(node
, np
) {
412 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-rx-mask",
414 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-tx-mask",
423 static int axg_card_parse_tdm(struct snd_soc_card
*card
,
424 struct device_node
*node
,
427 struct axg_card
*priv
= snd_soc_card_get_drvdata(card
);
428 struct snd_soc_dai_link
*link
= &card
->dai_link
[*index
];
429 struct axg_dai_link_tdm_data
*be
;
432 /* Allocate tdm link parameters */
433 be
= devm_kzalloc(card
->dev
, sizeof(*be
), GFP_KERNEL
);
436 priv
->link_data
[*index
] = be
;
439 link
->ops
= &axg_card_tdm_be_ops
;
440 link
->init
= axg_card_tdm_dai_init
;
441 link
->dai_fmt
= axg_card_parse_daifmt(node
, link
->cpus
->of_node
);
443 of_property_read_u32(node
, "mclk-fs", &be
->mclk_fs
);
445 ret
= axg_card_parse_cpu_tdm_slots(card
, link
, node
, be
);
447 dev_err(card
->dev
, "error parsing tdm link slots\n");
451 ret
= axg_card_parse_codecs_masks(card
, link
, node
, be
);
455 /* Add loopback if the pad dai has playback */
456 if (link
->dpcm_playback
) {
457 ret
= axg_card_add_tdm_loopback(card
, index
);
465 static int axg_card_set_be_link(struct snd_soc_card
*card
,
466 struct snd_soc_dai_link
*link
,
467 struct device_node
*node
)
469 struct snd_soc_dai_link_component
*codec
;
470 struct device_node
*np
;
474 link
->dpcm_playback
= 1;
475 link
->dpcm_capture
= 1;
477 num_codecs
= of_get_child_count(node
);
479 dev_err(card
->dev
, "be link %s has no codec\n",
484 codec
= devm_kcalloc(card
->dev
, num_codecs
, sizeof(*codec
), GFP_KERNEL
);
488 link
->codecs
= codec
;
489 link
->num_codecs
= num_codecs
;
491 for_each_child_of_node(node
, np
) {
492 ret
= axg_card_parse_dai(card
, np
, &codec
->of_node
,
502 ret
= axg_card_set_link_name(card
, link
, node
, "be");
504 dev_err(card
->dev
, "error setting %pOFn link name\n", np
);
509 static int axg_card_set_fe_link(struct snd_soc_card
*card
,
510 struct snd_soc_dai_link
*link
,
511 struct device_node
*node
,
514 struct snd_soc_dai_link_component
*codec
;
516 codec
= devm_kzalloc(card
->dev
, sizeof(*codec
), GFP_KERNEL
);
520 link
->codecs
= codec
;
521 link
->num_codecs
= 1;
524 link
->dpcm_merged_format
= 1;
525 link
->dpcm_merged_chan
= 1;
526 link
->dpcm_merged_rate
= 1;
527 link
->codecs
->dai_name
= "snd-soc-dummy-dai";
528 link
->codecs
->name
= "snd-soc-dummy";
531 link
->dpcm_playback
= 1;
533 link
->dpcm_capture
= 1;
535 return axg_card_set_link_name(card
, link
, node
, "fe");
538 static int axg_card_cpu_is_capture_fe(struct device_node
*np
)
540 return of_device_is_compatible(np
, PREFIX
"axg-toddr");
543 static int axg_card_cpu_is_playback_fe(struct device_node
*np
)
545 return of_device_is_compatible(np
, PREFIX
"axg-frddr");
548 static int axg_card_cpu_is_tdm_iface(struct device_node
*np
)
550 return of_device_is_compatible(np
, PREFIX
"axg-tdm-iface");
553 static int axg_card_cpu_is_codec(struct device_node
*np
)
555 return of_device_is_compatible(np
, PREFIX
"g12a-tohdmitx");
558 static int axg_card_add_link(struct snd_soc_card
*card
, struct device_node
*np
,
561 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[*index
];
562 struct snd_soc_dai_link_component
*cpu
;
565 cpu
= devm_kzalloc(card
->dev
, sizeof(*cpu
), GFP_KERNEL
);
569 dai_link
->cpus
= cpu
;
570 dai_link
->num_cpus
= 1;
572 ret
= axg_card_parse_dai(card
, np
, &dai_link
->cpus
->of_node
,
573 &dai_link
->cpus
->dai_name
);
577 if (axg_card_cpu_is_playback_fe(dai_link
->cpus
->of_node
))
578 ret
= axg_card_set_fe_link(card
, dai_link
, np
, true);
579 else if (axg_card_cpu_is_capture_fe(dai_link
->cpus
->of_node
))
580 ret
= axg_card_set_fe_link(card
, dai_link
, np
, false);
582 ret
= axg_card_set_be_link(card
, dai_link
, np
);
587 if (axg_card_cpu_is_tdm_iface(dai_link
->cpus
->of_node
))
588 ret
= axg_card_parse_tdm(card
, np
, index
);
589 else if (axg_card_cpu_is_codec(dai_link
->cpus
->of_node
))
590 dai_link
->params
= &codec_params
;
595 static int axg_card_add_links(struct snd_soc_card
*card
)
597 struct axg_card
*priv
= snd_soc_card_get_drvdata(card
);
598 struct device_node
*node
= card
->dev
->of_node
;
599 struct device_node
*np
;
602 num
= of_get_child_count(node
);
604 dev_err(card
->dev
, "card has no links\n");
608 ret
= axg_card_reallocate_links(priv
, num
);
613 for_each_child_of_node(node
, np
) {
614 ret
= axg_card_add_link(card
, np
, &i
);
626 static int axg_card_parse_of_optional(struct snd_soc_card
*card
,
627 const char *propname
,
628 int (*func
)(struct snd_soc_card
*c
,
631 /* If property is not provided, don't fail ... */
632 if (!of_property_read_bool(card
->dev
->of_node
, propname
))
635 /* ... but do fail if it is provided and the parsing fails */
636 return func(card
, propname
);
639 static const struct of_device_id axg_card_of_match
[] = {
640 { .compatible
= "amlogic,axg-sound-card", },
643 MODULE_DEVICE_TABLE(of
, axg_card_of_match
);
645 static int axg_card_probe(struct platform_device
*pdev
)
647 struct device
*dev
= &pdev
->dev
;
648 struct axg_card
*priv
;
651 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
655 platform_set_drvdata(pdev
, priv
);
656 snd_soc_card_set_drvdata(&priv
->card
, priv
);
658 priv
->card
.owner
= THIS_MODULE
;
659 priv
->card
.dev
= dev
;
661 ret
= snd_soc_of_parse_card_name(&priv
->card
, "model");
665 ret
= axg_card_parse_of_optional(&priv
->card
, "audio-routing",
666 snd_soc_of_parse_audio_routing
);
668 dev_err(dev
, "error while parsing routing\n");
672 ret
= axg_card_parse_of_optional(&priv
->card
, "audio-widgets",
673 snd_soc_of_parse_audio_simple_widgets
);
675 dev_err(dev
, "error while parsing widgets\n");
679 ret
= axg_card_add_links(&priv
->card
);
683 ret
= axg_card_add_aux_devices(&priv
->card
);
687 ret
= devm_snd_soc_register_card(dev
, &priv
->card
);
694 axg_card_clean_references(priv
);
698 static int axg_card_remove(struct platform_device
*pdev
)
700 struct axg_card
*priv
= platform_get_drvdata(pdev
);
702 axg_card_clean_references(priv
);
707 static struct platform_driver axg_card_pdrv
= {
708 .probe
= axg_card_probe
,
709 .remove
= axg_card_remove
,
711 .name
= "axg-sound-card",
712 .of_match_table
= axg_card_of_match
,
715 module_platform_driver(axg_card_pdrv
);
717 MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
718 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
719 MODULE_LICENSE("GPL v2");