1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 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 #include "../utils.h"
16
17 #define MAX98373_DEV0_NAME "i2c-MX98373:00"
18 #define MAX98373_DEV1_NAME "i2c-MX98373:01"
19 #define MAX98373_CODEC_NAME "max98373-aif1"
20
21 static struct snd_soc_codec_conf card_codec_conf[] = {
22 {
23 .dlc = COMP_CODEC_CONF(MAX98373_DEV0_NAME),
24 .name_prefix = "Right",
25 },
26 {
27 .dlc = COMP_CODEC_CONF(MAX98373_DEV1_NAME),
28 .name_prefix = "Left",
29 },
30 };
31
32 static const struct snd_kcontrol_new card_controls[] = {
33 SOC_DAPM_PIN_SWITCH("Left Spk"),
34 SOC_DAPM_PIN_SWITCH("Right Spk"),
35 };
36
37 static const struct snd_soc_dapm_widget card_widgets[] = {
38 SND_SOC_DAPM_SPK("Left Spk", NULL),
39 SND_SOC_DAPM_SPK("Right Spk", NULL),
40 };
41
42 static const struct snd_soc_dapm_route card_base_routes[] = {
43 { "Left Spk", NULL, "Left BE_OUT" },
44 { "Right Spk", NULL, "Right BE_OUT" },
45 };
46
47 static int
avs_max98373_be_fixup(struct snd_soc_pcm_runtime * runrime,struct snd_pcm_hw_params * params)48 avs_max98373_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
49 {
50 struct snd_interval *rate, *channels;
51 struct snd_mask *fmt;
52
53 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
54 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
55 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
56
57 /* The ADSP will convert the FE rate to 48k, stereo */
58 rate->min = rate->max = 48000;
59 channels->min = channels->max = 2;
60
61 /* set SSP0 to 16 bit */
62 snd_mask_none(fmt);
63 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
64 return 0;
65 }
66
avs_max98373_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)67 static int avs_max98373_hw_params(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params *params)
69 {
70 struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
71 struct snd_soc_dai *codec_dai;
72 int ret, i;
73
74 for_each_rtd_codec_dais(runtime, i, codec_dai) {
75 if (!strcmp(codec_dai->component->name, MAX98373_DEV0_NAME)) {
76 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
77 if (ret < 0) {
78 dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
79 return ret;
80 }
81 }
82 if (!strcmp(codec_dai->component->name, MAX98373_DEV1_NAME)) {
83 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
84 if (ret < 0) {
85 dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
86 return ret;
87 }
88 }
89 }
90
91 return 0;
92 }
93
94 static const struct snd_soc_ops avs_max98373_ops = {
95 .hw_params = avs_max98373_hw_params,
96 };
97
avs_create_dai_link(struct device * dev,const char * platform_name,int ssp_port,int tdm_slot,struct snd_soc_dai_link ** dai_link)98 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
99 int tdm_slot, struct snd_soc_dai_link **dai_link)
100 {
101 struct snd_soc_dai_link_component *platform;
102 struct snd_soc_dai_link *dl;
103
104 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
105 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
106 if (!dl || !platform)
107 return -ENOMEM;
108
109 platform->name = platform_name;
110
111 dl->name = devm_kasprintf(dev, GFP_KERNEL,
112 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
113 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
114 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
115 if (!dl->name || !dl->cpus || !dl->codecs)
116 return -ENOMEM;
117
118 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
119 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
120 dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV0_NAME);
121 dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
122 dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV1_NAME);
123 dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
124 if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
125 !dl->codecs[1].name || !dl->codecs[1].dai_name)
126 return -ENOMEM;
127
128 dl->num_cpus = 1;
129 dl->num_codecs = 2;
130 dl->platforms = platform;
131 dl->num_platforms = 1;
132 dl->id = 0;
133 dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
134 dl->be_hw_params_fixup = avs_max98373_be_fixup;
135 dl->nonatomic = 1;
136 dl->no_pcm = 1;
137 dl->dpcm_capture = 1;
138 dl->dpcm_playback = 1;
139 dl->ignore_pmdown_time = 1;
140 dl->ops = &avs_max98373_ops;
141
142 *dai_link = dl;
143
144 return 0;
145 }
146
avs_max98373_probe(struct platform_device * pdev)147 static int avs_max98373_probe(struct platform_device *pdev)
148 {
149 struct snd_soc_dai_link *dai_link;
150 struct snd_soc_acpi_mach *mach;
151 struct snd_soc_card *card;
152 struct device *dev = &pdev->dev;
153 const char *pname;
154 int ssp_port, tdm_slot, ret;
155
156 mach = dev_get_platdata(dev);
157 pname = mach->mach_params.platform;
158
159 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
160 if (ret)
161 return ret;
162
163 ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
164 if (ret) {
165 dev_err(dev, "Failed to create dai link: %d", ret);
166 return ret;
167 }
168
169 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
170 if (!card)
171 return -ENOMEM;
172
173 card->name = "avs_max98373";
174 card->dev = dev;
175 card->owner = THIS_MODULE;
176 card->dai_link = dai_link;
177 card->num_links = 1;
178 card->codec_conf = card_codec_conf;
179 card->num_configs = ARRAY_SIZE(card_codec_conf);
180 card->controls = card_controls;
181 card->num_controls = ARRAY_SIZE(card_controls);
182 card->dapm_widgets = card_widgets;
183 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
184 card->dapm_routes = card_base_routes;
185 card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
186 card->fully_routed = true;
187
188 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
189 if (ret)
190 return ret;
191
192 return devm_snd_soc_register_card(dev, card);
193 }
194
195 static const struct platform_device_id avs_max98373_driver_ids[] = {
196 {
197 .name = "avs_max98373",
198 },
199 {},
200 };
201 MODULE_DEVICE_TABLE(platform, avs_max98373_driver_ids);
202
203 static struct platform_driver avs_max98373_driver = {
204 .probe = avs_max98373_probe,
205 .driver = {
206 .name = "avs_max98373",
207 .pm = &snd_soc_pm_ops,
208 },
209 .id_table = avs_max98373_driver_ids,
210 };
211
212 module_platform_driver(avs_max98373_driver)
213
214 MODULE_LICENSE("GPL");
215