1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8
9 #include <linux/module.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15
avs_create_dai_link(struct device * dev,const char * platform_name,int ssp_port,struct snd_soc_dai_link ** dai_link)16 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
17 struct snd_soc_dai_link **dai_link)
18 {
19 struct snd_soc_dai_link_component *platform;
20 struct snd_soc_dai_link *dl;
21
22 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
23 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
24 if (!dl || !platform)
25 return -ENOMEM;
26
27 platform->name = platform_name;
28
29 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
30 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
31 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
32 if (!dl->name || !dl->cpus || !dl->codecs)
33 return -ENOMEM;
34
35 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
36 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "snd-soc-dummy");
37 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "snd-soc-dummy-dai");
38 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
39 return -ENOMEM;
40
41 dl->num_cpus = 1;
42 dl->num_codecs = 1;
43 dl->platforms = platform;
44 dl->num_platforms = 1;
45 dl->id = 0;
46 dl->nonatomic = 1;
47 dl->no_pcm = 1;
48 dl->dpcm_capture = 1;
49 dl->dpcm_playback = 1;
50
51 *dai_link = dl;
52
53 return 0;
54 }
55
avs_create_dapm_routes(struct device * dev,int ssp_port,struct snd_soc_dapm_route ** routes,int * num_routes)56 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
57 struct snd_soc_dapm_route **routes, int *num_routes)
58 {
59 struct snd_soc_dapm_route *dr;
60 const int num_dr = 2;
61
62 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
63 if (!dr)
64 return -ENOMEM;
65
66 dr[0].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%dpb", ssp_port);
67 dr[0].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
68 if (!dr[0].sink || !dr[0].source)
69 return -ENOMEM;
70
71 dr[1].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
72 dr[1].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%dcp", ssp_port);
73 if (!dr[1].sink || !dr[1].source)
74 return -ENOMEM;
75
76 *routes = dr;
77 *num_routes = num_dr;
78
79 return 0;
80 }
81
avs_create_dapm_widgets(struct device * dev,int ssp_port,struct snd_soc_dapm_widget ** widgets,int * num_widgets)82 static int avs_create_dapm_widgets(struct device *dev, int ssp_port,
83 struct snd_soc_dapm_widget **widgets, int *num_widgets)
84 {
85 struct snd_soc_dapm_widget *dw;
86 const int num_dw = 2;
87
88 dw = devm_kcalloc(dev, num_dw, sizeof(*dw), GFP_KERNEL);
89 if (!dw)
90 return -ENOMEM;
91
92 dw[0].id = snd_soc_dapm_hp;
93 dw[0].reg = SND_SOC_NOPM;
94 dw[0].name = devm_kasprintf(dev, GFP_KERNEL, "ssp%dpb", ssp_port);
95 if (!dw[0].name)
96 return -ENOMEM;
97
98 dw[1].id = snd_soc_dapm_mic;
99 dw[1].reg = SND_SOC_NOPM;
100 dw[1].name = devm_kasprintf(dev, GFP_KERNEL, "ssp%dcp", ssp_port);
101 if (!dw[1].name)
102 return -ENOMEM;
103
104 *widgets = dw;
105 *num_widgets = num_dw;
106
107 return 0;
108 }
109
avs_i2s_test_probe(struct platform_device * pdev)110 static int avs_i2s_test_probe(struct platform_device *pdev)
111 {
112 struct snd_soc_dapm_widget *widgets;
113 struct snd_soc_dapm_route *routes;
114 struct snd_soc_dai_link *dai_link;
115 struct snd_soc_acpi_mach *mach;
116 struct snd_soc_card *card;
117 struct device *dev = &pdev->dev;
118 const char *pname;
119 int num_routes, num_widgets;
120 int ssp_port, ret;
121
122 mach = dev_get_platdata(dev);
123 pname = mach->mach_params.platform;
124 ssp_port = __ffs(mach->mach_params.i2s_link_mask);
125
126 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
127 if (!card)
128 return -ENOMEM;
129
130 card->name = devm_kasprintf(dev, GFP_KERNEL, "ssp%d-loopback", ssp_port);
131 if (!card->name)
132 return -ENOMEM;
133
134 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
135 if (ret) {
136 dev_err(dev, "Failed to create dai link: %d\n", ret);
137 return ret;
138 }
139
140 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
141 if (ret) {
142 dev_err(dev, "Failed to create dapm routes: %d\n", ret);
143 return ret;
144 }
145
146 ret = avs_create_dapm_widgets(dev, ssp_port, &widgets, &num_widgets);
147 if (ret) {
148 dev_err(dev, "Failed to create dapm widgets: %d\n", ret);
149 return ret;
150 }
151
152 card->dev = dev;
153 card->owner = THIS_MODULE;
154 card->dai_link = dai_link;
155 card->num_links = 1;
156 card->dapm_routes = routes;
157 card->num_dapm_routes = num_routes;
158 card->dapm_widgets = widgets;
159 card->num_dapm_widgets = num_widgets;
160 card->fully_routed = true;
161
162 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
163 if (ret)
164 return ret;
165
166 return devm_snd_soc_register_card(dev, card);
167 }
168
169 static struct platform_driver avs_i2s_test_driver = {
170 .probe = avs_i2s_test_probe,
171 .driver = {
172 .name = "avs_i2s_test",
173 .pm = &snd_soc_pm_ops,
174 },
175 };
176
177 module_platform_driver(avs_i2s_test_driver);
178
179 MODULE_LICENSE("GPL");
180 MODULE_ALIAS("platform:avs_i2s_test");
181