1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/soc.h>
12 #include <sound/soc-acpi.h>
13 #include <sound/soc-dapm.h>
15 static const struct snd_kcontrol_new card_controls[] = {
16 SOC_DAPM_PIN_SWITCH("Spk"),
19 static const struct snd_soc_dapm_widget card_widgets[] = {
20 SND_SOC_DAPM_SPK("Spk", NULL),
23 static const struct snd_soc_dapm_route card_base_routes[] = {
24 { "Spk", NULL, "Speaker" },
27 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
28 struct snd_soc_dai_link **dai_link)
30 struct snd_soc_dai_link_component *platform;
31 struct snd_soc_dai_link *dl;
33 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
34 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
38 platform->name = platform_name;
40 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
41 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
42 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
43 if (!dl->name || !dl->cpus || !dl->codecs)
46 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
47 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "MX98357A:00");
48 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "HiFi");
49 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
54 dl->platforms = platform;
55 dl->num_platforms = 1;
57 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
60 dl->dpcm_playback = 1;
67 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
68 struct snd_soc_dapm_route **routes, int *num_routes)
70 struct snd_soc_dapm_route *dr;
71 const int num_base = ARRAY_SIZE(card_base_routes);
72 const int num_dr = num_base + 1;
75 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
79 memcpy(dr, card_base_routes, num_base * sizeof(*dr));
82 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "HiFi Playback");
83 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
84 if (!dr[idx].sink || !dr[idx].source)
93 static int avs_max98357a_probe(struct platform_device *pdev)
95 struct snd_soc_dapm_route *routes;
96 struct snd_soc_dai_link *dai_link;
97 struct snd_soc_acpi_mach *mach;
98 struct snd_soc_card *card;
99 struct device *dev = &pdev->dev;
101 int num_routes, ssp_port, ret;
103 mach = dev_get_platdata(dev);
104 pname = mach->mach_params.platform;
105 ssp_port = __ffs(mach->mach_params.i2s_link_mask);
107 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
109 dev_err(dev, "Failed to create dai link: %d", ret);
113 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
115 dev_err(dev, "Failed to create dapm routes: %d", ret);
119 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
123 card->name = "avs_max98357a";
125 card->owner = THIS_MODULE;
126 card->dai_link = dai_link;
128 card->controls = card_controls;
129 card->num_controls = ARRAY_SIZE(card_controls);
130 card->dapm_widgets = card_widgets;
131 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
132 card->dapm_routes = routes;
133 card->num_dapm_routes = num_routes;
134 card->fully_routed = true;
136 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
140 return devm_snd_soc_register_card(dev, card);
143 static struct platform_driver avs_max98357a_driver = {
144 .probe = avs_max98357a_probe,
146 .name = "avs_max98357a",
147 .pm = &snd_soc_pm_ops,
151 module_platform_driver(avs_max98357a_driver)
153 MODULE_LICENSE("GPL");
154 MODULE_ALIAS("platform:avs_max98357a");