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#include "../utils.h"
16
17static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
18			       int tdm_slot, struct snd_soc_dai_link **dai_link)
19{
20	struct snd_soc_dai_link_component *platform;
21	struct snd_soc_dai_link *dl;
22
23	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
24	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
25	if (!dl || !platform)
26		return -ENOMEM;
27
28	platform->name = platform_name;
29
30	dl->name = devm_kasprintf(dev, GFP_KERNEL,
31				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
32	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
33	if (!dl->name || !dl->cpus)
34		return -ENOMEM;
35
36	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
37					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
38	dl->codecs = &snd_soc_dummy_dlc;
39	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
40		return -ENOMEM;
41
42	dl->num_cpus = 1;
43	dl->num_codecs = 1;
44	dl->platforms = platform;
45	dl->num_platforms = 1;
46	dl->id = 0;
47	dl->nonatomic = 1;
48	dl->no_pcm = 1;
49	dl->dpcm_capture = 1;
50	dl->dpcm_playback = 1;
51
52	*dai_link = dl;
53
54	return 0;
55}
56
57static int avs_create_dapm_routes(struct device *dev, int ssp_port, int tdm_slot,
58				  struct snd_soc_dapm_route **routes, int *num_routes)
59{
60	struct snd_soc_dapm_route *dr;
61	const int num_dr = 2;
62
63	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
64	if (!dr)
65		return -ENOMEM;
66
67	dr[0].sink = devm_kasprintf(dev, GFP_KERNEL,
68				    AVS_STRING_FMT("ssp", "pb", ssp_port, tdm_slot));
69	dr[0].source = devm_kasprintf(dev, GFP_KERNEL,
70				      AVS_STRING_FMT("ssp", " Tx", ssp_port, tdm_slot));
71	if (!dr[0].sink || !dr[0].source)
72		return -ENOMEM;
73
74	dr[1].sink = devm_kasprintf(dev, GFP_KERNEL,
75				    AVS_STRING_FMT("ssp", " Rx", ssp_port, tdm_slot));
76	dr[1].source = devm_kasprintf(dev, GFP_KERNEL,
77				      AVS_STRING_FMT("ssp", "cp", ssp_port, tdm_slot));
78	if (!dr[1].sink || !dr[1].source)
79		return -ENOMEM;
80
81	*routes = dr;
82	*num_routes = num_dr;
83
84	return 0;
85}
86
87static int avs_create_dapm_widgets(struct device *dev, int ssp_port, int tdm_slot,
88				   struct snd_soc_dapm_widget **widgets, int *num_widgets)
89{
90	struct snd_soc_dapm_widget *dw;
91	const int num_dw = 2;
92
93	dw = devm_kcalloc(dev, num_dw, sizeof(*dw), GFP_KERNEL);
94	if (!dw)
95		return -ENOMEM;
96
97	dw[0].id = snd_soc_dapm_hp;
98	dw[0].reg = SND_SOC_NOPM;
99	dw[0].name = devm_kasprintf(dev, GFP_KERNEL,
100				    AVS_STRING_FMT("ssp", "pb", ssp_port, tdm_slot));
101	if (!dw[0].name)
102		return -ENOMEM;
103
104	dw[1].id = snd_soc_dapm_mic;
105	dw[1].reg = SND_SOC_NOPM;
106	dw[1].name = devm_kasprintf(dev, GFP_KERNEL,
107				    AVS_STRING_FMT("ssp", "cp", ssp_port, tdm_slot));
108	if (!dw[1].name)
109		return -ENOMEM;
110
111	*widgets = dw;
112	*num_widgets = num_dw;
113
114	return 0;
115}
116
117static int avs_i2s_test_probe(struct platform_device *pdev)
118{
119	struct snd_soc_dapm_widget *widgets;
120	struct snd_soc_dapm_route *routes;
121	struct snd_soc_dai_link *dai_link;
122	struct snd_soc_acpi_mach *mach;
123	struct snd_soc_card *card;
124	struct device *dev = &pdev->dev;
125	const char *pname;
126	int num_routes, num_widgets;
127	int ssp_port, tdm_slot, ret;
128
129	mach = dev_get_platdata(dev);
130	pname = mach->mach_params.platform;
131
132	if (!avs_mach_singular_ssp(mach)) {
133		dev_err(dev, "Invalid SSP configuration\n");
134		return -EINVAL;
135	}
136	ssp_port = avs_mach_ssp_port(mach);
137
138	if (!avs_mach_singular_tdm(mach, ssp_port)) {
139		dev_err(dev, "Invalid TDM configuration\n");
140		return -EINVAL;
141	}
142	tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
143
144	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
145	if (!card)
146		return -ENOMEM;
147
148	card->name = devm_kasprintf(dev, GFP_KERNEL,
149				    AVS_STRING_FMT("ssp", "-loopback", ssp_port, tdm_slot));
150	if (!card->name)
151		return -ENOMEM;
152
153	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
154	if (ret) {
155		dev_err(dev, "Failed to create dai link: %d\n", ret);
156		return ret;
157	}
158
159	ret = avs_create_dapm_routes(dev, ssp_port, tdm_slot, &routes, &num_routes);
160	if (ret) {
161		dev_err(dev, "Failed to create dapm routes: %d\n", ret);
162		return ret;
163	}
164
165	ret = avs_create_dapm_widgets(dev, ssp_port, tdm_slot, &widgets, &num_widgets);
166	if (ret) {
167		dev_err(dev, "Failed to create dapm widgets: %d\n", ret);
168		return ret;
169	}
170
171	card->dev = dev;
172	card->owner = THIS_MODULE;
173	card->dai_link = dai_link;
174	card->num_links = 1;
175	card->dapm_routes = routes;
176	card->num_dapm_routes = num_routes;
177	card->dapm_widgets = widgets;
178	card->num_dapm_widgets = num_widgets;
179	card->fully_routed = true;
180
181	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
182	if (ret)
183		return ret;
184
185	return devm_snd_soc_register_card(dev, card);
186}
187
188static const struct platform_device_id avs_i2s_test_driver_ids[] = {
189	{
190		.name = "avs_i2s_test",
191	},
192	{},
193};
194MODULE_DEVICE_TABLE(platform, avs_i2s_test_driver_ids);
195
196static struct platform_driver avs_i2s_test_driver = {
197	.probe = avs_i2s_test_probe,
198	.driver = {
199		.name = "avs_i2s_test",
200		.pm = &snd_soc_pm_ops,
201	},
202	.id_table = avs_i2s_test_driver_ids,
203};
204
205module_platform_driver(avs_i2s_test_driver);
206
207MODULE_DESCRIPTION("Intel i2s test machine driver");
208MODULE_LICENSE("GPL");
209