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/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15 
16 static const struct snd_kcontrol_new card_controls[] = {
17 	SOC_DAPM_PIN_SWITCH("Spk"),
18 };
19 
20 static const struct snd_soc_dapm_widget card_widgets[] = {
21 	SND_SOC_DAPM_SPK("Spk", NULL),
22 };
23 
24 static const struct snd_soc_dapm_route card_base_routes[] = {
25 	{ "Spk", NULL, "Speaker" },
26 };
27 
28 static int
29 avs_max98357a_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
30 {
31 	struct snd_interval *rate, *channels;
32 	struct snd_mask *fmt;
33 
34 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
35 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
36 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
37 
38 	/* The ADSP will convert the FE rate to 48k, stereo */
39 	rate->min = rate->max = 48000;
40 	channels->min = channels->max = 2;
41 
42 	/* set SSP0 to 16 bit */
43 	snd_mask_none(fmt);
44 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
45 	return 0;
46 }
47 
48 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
49 			       struct snd_soc_dai_link **dai_link)
50 {
51 	struct snd_soc_dai_link_component *platform;
52 	struct snd_soc_dai_link *dl;
53 
54 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
55 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
56 	if (!dl || !platform)
57 		return -ENOMEM;
58 
59 	platform->name = platform_name;
60 
61 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
62 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
63 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
64 	if (!dl->name || !dl->cpus || !dl->codecs)
65 		return -ENOMEM;
66 
67 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
68 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "MX98357A:00");
69 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "HiFi");
70 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
71 		return -ENOMEM;
72 
73 	dl->num_cpus = 1;
74 	dl->num_codecs = 1;
75 	dl->platforms = platform;
76 	dl->num_platforms = 1;
77 	dl->id = 0;
78 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
79 	dl->be_hw_params_fixup = avs_max98357a_be_fixup;
80 	dl->nonatomic = 1;
81 	dl->no_pcm = 1;
82 	dl->dpcm_playback = 1;
83 
84 	*dai_link = dl;
85 
86 	return 0;
87 }
88 
89 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
90 				  struct snd_soc_dapm_route **routes, int *num_routes)
91 {
92 	struct snd_soc_dapm_route *dr;
93 	const int num_base = ARRAY_SIZE(card_base_routes);
94 	const int num_dr = num_base + 1;
95 	int idx;
96 
97 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
98 	if (!dr)
99 		return -ENOMEM;
100 
101 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
102 
103 	idx = num_base;
104 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "HiFi Playback");
105 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
106 	if (!dr[idx].sink || !dr[idx].source)
107 		return -ENOMEM;
108 
109 	*routes = dr;
110 	*num_routes = num_dr;
111 
112 	return 0;
113 }
114 
115 static int avs_max98357a_probe(struct platform_device *pdev)
116 {
117 	struct snd_soc_dapm_route *routes;
118 	struct snd_soc_dai_link *dai_link;
119 	struct snd_soc_acpi_mach *mach;
120 	struct snd_soc_card *card;
121 	struct device *dev = &pdev->dev;
122 	const char *pname;
123 	int num_routes, ssp_port, ret;
124 
125 	mach = dev_get_platdata(dev);
126 	pname = mach->mach_params.platform;
127 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
128 
129 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
130 	if (ret) {
131 		dev_err(dev, "Failed to create dai link: %d", ret);
132 		return ret;
133 	}
134 
135 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
136 	if (ret) {
137 		dev_err(dev, "Failed to create dapm routes: %d", ret);
138 		return ret;
139 	}
140 
141 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
142 	if (!card)
143 		return -ENOMEM;
144 
145 	card->name = "avs_max98357a";
146 	card->dev = dev;
147 	card->owner = THIS_MODULE;
148 	card->dai_link = dai_link;
149 	card->num_links = 1;
150 	card->controls = card_controls;
151 	card->num_controls = ARRAY_SIZE(card_controls);
152 	card->dapm_widgets = card_widgets;
153 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
154 	card->dapm_routes = routes;
155 	card->num_dapm_routes = num_routes;
156 	card->fully_routed = true;
157 
158 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
159 	if (ret)
160 		return ret;
161 
162 	return devm_snd_soc_register_card(dev, card);
163 }
164 
165 static struct platform_driver avs_max98357a_driver = {
166 	.probe = avs_max98357a_probe,
167 	.driver = {
168 		.name = "avs_max98357a",
169 		.pm = &snd_soc_pm_ops,
170 	},
171 };
172 
173 module_platform_driver(avs_max98357a_driver)
174 
175 MODULE_LICENSE("GPL");
176 MODULE_ALIAS("platform:avs_max98357a");
177