1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation
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 MAX98927_DEV0_NAME "i2c-MX98927:00"
18 #define MAX98927_DEV1_NAME "i2c-MX98927:01"
19 #define MAX98927_CODEC_NAME "max98927-aif1"
20
21 static struct snd_soc_codec_conf card_codec_conf[] = {
22 {
23 .dlc = COMP_CODEC_CONF(MAX98927_DEV0_NAME),
24 .name_prefix = "Right",
25 },
26 {
27 .dlc = COMP_CODEC_CONF(MAX98927_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_max98927_be_fixup(struct snd_soc_pcm_runtime * runrime,struct snd_pcm_hw_params * params)48 avs_max98927_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_max98927_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)67 static int avs_max98927_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 = 0;
73 int i;
74
75 for_each_rtd_codec_dais(runtime, i, codec_dai) {
76 if (!strcmp(codec_dai->component->name, MAX98927_DEV0_NAME))
77 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
78 else if (!strcmp(codec_dai->component->name, MAX98927_DEV1_NAME))
79 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
80
81 if (ret < 0) {
82 dev_err(runtime->dev, "hw_params for %s failed: %d\n",
83 codec_dai->component->name, ret);
84 return ret;
85 }
86 }
87
88 return 0;
89 }
90
91 static const struct snd_soc_ops avs_max98927_ops = {
92 .hw_params = avs_max98927_hw_params,
93 };
94
avs_create_dai_link(struct device * dev,const char * platform_name,int ssp_port,int tdm_slot,struct snd_soc_dai_link ** dai_link)95 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
96 int tdm_slot, struct snd_soc_dai_link **dai_link)
97 {
98 struct snd_soc_dai_link_component *platform;
99 struct snd_soc_dai_link *dl;
100
101 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
102 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
103 if (!dl || !platform)
104 return -ENOMEM;
105
106 platform->name = platform_name;
107
108 dl->name = devm_kasprintf(dev, GFP_KERNEL,
109 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
110 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
111 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
112 if (!dl->name || !dl->cpus || !dl->codecs)
113 return -ENOMEM;
114
115 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
116 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
117 dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV0_NAME);
118 dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
119 dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV1_NAME);
120 dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
121 if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
122 !dl->codecs[1].name || !dl->codecs[1].dai_name)
123 return -ENOMEM;
124
125 dl->num_cpus = 1;
126 dl->num_codecs = 2;
127 dl->platforms = platform;
128 dl->num_platforms = 1;
129 dl->id = 0;
130 dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
131 dl->be_hw_params_fixup = avs_max98927_be_fixup;
132 dl->nonatomic = 1;
133 dl->no_pcm = 1;
134 dl->dpcm_capture = 1;
135 dl->dpcm_playback = 1;
136 dl->ignore_pmdown_time = 1;
137 dl->ops = &avs_max98927_ops;
138
139 *dai_link = dl;
140
141 return 0;
142 }
143
avs_max98927_probe(struct platform_device * pdev)144 static int avs_max98927_probe(struct platform_device *pdev)
145 {
146 struct snd_soc_dai_link *dai_link;
147 struct snd_soc_acpi_mach *mach;
148 struct snd_soc_card *card;
149 struct device *dev = &pdev->dev;
150 const char *pname;
151 int ssp_port, tdm_slot, ret;
152
153 mach = dev_get_platdata(dev);
154 pname = mach->mach_params.platform;
155
156 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
157 if (ret)
158 return ret;
159
160 ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
161 if (ret) {
162 dev_err(dev, "Failed to create dai link: %d", ret);
163 return ret;
164 }
165
166 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
167 if (!card)
168 return -ENOMEM;
169
170 card->name = "avs_max98927";
171 card->dev = dev;
172 card->owner = THIS_MODULE;
173 card->dai_link = dai_link;
174 card->num_links = 1;
175 card->codec_conf = card_codec_conf;
176 card->num_configs = ARRAY_SIZE(card_codec_conf);
177 card->controls = card_controls;
178 card->num_controls = ARRAY_SIZE(card_controls);
179 card->dapm_widgets = card_widgets;
180 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
181 card->dapm_routes = card_base_routes;
182 card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
183 card->fully_routed = true;
184
185 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
186 if (ret)
187 return ret;
188
189 return devm_snd_soc_register_card(dev, card);
190 }
191
192 static const struct platform_device_id avs_max98927_driver_ids[] = {
193 {
194 .name = "avs_max98927",
195 },
196 {},
197 };
198 MODULE_DEVICE_TABLE(platform, avs_max98927_driver_ids);
199
200 static struct platform_driver avs_max98927_driver = {
201 .probe = avs_max98927_probe,
202 .driver = {
203 .name = "avs_max98927",
204 .pm = &snd_soc_pm_ops,
205 },
206 .id_table = avs_max98927_driver_ids,
207 };
208
209 module_platform_driver(avs_max98927_driver)
210
211 MODULE_DESCRIPTION("Intel max98927 machine driver");
212 MODULE_LICENSE("GPL");
213