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 <linux/platform_device.h>
11 #include <sound/soc.h>
12 #include <sound/soc-acpi.h>
13 #include <sound/soc-dapm.h>
14 
15 static const struct snd_kcontrol_new card_controls[] = {
16 	SOC_DAPM_PIN_SWITCH("Spk"),
17 };
18 
19 static const struct snd_soc_dapm_widget card_widgets[] = {
20 	SND_SOC_DAPM_SPK("Spk", NULL),
21 };
22 
23 static const struct snd_soc_dapm_route card_base_routes[] = {
24 	{ "Spk", NULL, "Speaker" },
25 };
26 
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)
29 {
30 	struct snd_soc_dai_link_component *platform;
31 	struct snd_soc_dai_link *dl;
32 
33 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
34 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
35 	if (!dl || !platform)
36 		return -ENOMEM;
37 
38 	platform->name = platform_name;
39 
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)
44 		return -ENOMEM;
45 
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)
50 		return -ENOMEM;
51 
52 	dl->num_cpus = 1;
53 	dl->num_codecs = 1;
54 	dl->platforms = platform;
55 	dl->num_platforms = 1;
56 	dl->id = 0;
57 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
58 	dl->nonatomic = 1;
59 	dl->no_pcm = 1;
60 	dl->dpcm_playback = 1;
61 
62 	*dai_link = dl;
63 
64 	return 0;
65 }
66 
67 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
68 				  struct snd_soc_dapm_route **routes, int *num_routes)
69 {
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;
73 	int idx;
74 
75 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
76 	if (!dr)
77 		return -ENOMEM;
78 
79 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
80 
81 	idx = num_base;
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)
85 		return -ENOMEM;
86 
87 	*routes = dr;
88 	*num_routes = num_dr;
89 
90 	return 0;
91 }
92 
93 static int avs_max98357a_probe(struct platform_device *pdev)
94 {
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;
100 	const char *pname;
101 	int num_routes, ssp_port, ret;
102 
103 	mach = dev_get_platdata(dev);
104 	pname = mach->mach_params.platform;
105 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
106 
107 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
108 	if (ret) {
109 		dev_err(dev, "Failed to create dai link: %d", ret);
110 		return ret;
111 	}
112 
113 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
114 	if (ret) {
115 		dev_err(dev, "Failed to create dapm routes: %d", ret);
116 		return ret;
117 	}
118 
119 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
120 	if (!card)
121 		return -ENOMEM;
122 
123 	card->name = "avs_max98357a";
124 	card->dev = dev;
125 	card->owner = THIS_MODULE;
126 	card->dai_link = dai_link;
127 	card->num_links = 1;
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;
135 
136 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
137 	if (ret)
138 		return ret;
139 
140 	return devm_snd_soc_register_card(dev, card);
141 }
142 
143 static struct platform_driver avs_max98357a_driver = {
144 	.probe = avs_max98357a_probe,
145 	.driver = {
146 		.name = "avs_max98357a",
147 		.pm = &snd_soc_pm_ops,
148 	},
149 };
150 
151 module_platform_driver(avs_max98357a_driver)
152 
153 MODULE_LICENSE("GPL");
154 MODULE_ALIAS("platform:avs_max98357a");
155