Debian Codegraph @

sound/soc/intel/avs/boards/nau8825.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <[email protected]>
6 // Amadeusz Slawinski <[email protected]>
7 //
8
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <sound/core.h>
13 #include <sound/jack.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 #include <sound/soc-acpi.h>
18 #include "../../../codecs/nau8825.h"
19 #include "../utils.h"
20
21 #define SKL_NUVOTON_CODEC_DAI "nau8825-hifi"
22
23 static int
24 avs_nau8825_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event)
25 {
26 struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
27 struct snd_soc_dai *codec_dai;
28 int ret;
29
30 codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
31 if (!codec_dai) {
32 dev_err(card->dev, "Codec dai not found\n");
33 return -EINVAL;
34 }
35
36 if (SND_SOC_DAPM_EVENT_ON(event))
37 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_MCLK, 24000000,
38 SND_SOC_CLOCK_IN);
39 else
40 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
41 if (ret < 0)
42 dev_err(card->dev, "Set sysclk failed: %d\n", ret);
43
44 return ret;
45 }
46
47 static const struct snd_kcontrol_new card_controls[] = {
48 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
49 SOC_DAPM_PIN_SWITCH("Headset Mic"),
50 };
51
52 static const struct snd_soc_dapm_widget card_widgets[] = {
53 SND_SOC_DAPM_HP("Headphone Jack", NULL),
54 SND_SOC_DAPM_MIC("Headset Mic", NULL),
55 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, avs_nau8825_clock_control,
56 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
57 };
58
59 static const struct snd_soc_dapm_route card_base_routes[] = {
60 { "Headphone Jack", NULL, "HPOL" },
61 { "Headphone Jack", NULL, "HPOR" },
62
63 { "MIC", NULL, "Headset Mic" },
64
65 { "Headphone Jack", NULL, "Platform Clock" },
66 { "Headset Mic", NULL, "Platform Clock" },
67 };
68
69 static const struct snd_soc_jack_pin card_headset_pins[] = {
70 {
71 .pin = "Headphone Jack",
72 .mask = SND_JACK_HEADPHONE,
73 },
74 {
75 .pin = "Headset Mic",
76 .mask = SND_JACK_MICROPHONE,
77 },
78 };
79
80 static int avs_nau8825_codec_init(struct snd_soc_pcm_runtime *runtime)
81 {
82 struct snd_soc_card *card = runtime->card;
83 struct snd_soc_jack_pin *pins;
84 struct snd_soc_jack *jack;
85 int num_pins, ret;
86
87 jack = snd_soc_card_get_drvdata(card);
88 num_pins = ARRAY_SIZE(card_headset_pins);
89
90 pins = devm_kmemdup_array(card->dev, card_headset_pins, num_pins,
91 sizeof(card_headset_pins[0]), GFP_KERNEL);
92 if (!pins)
93 return -ENOMEM;
94
95 /*
96 * 4 buttons here map to the google Reference headset.
97 * The use of these buttons can be decided by the user space.
98 */
99 ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0 |
100 SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3,
101 jack, pins, num_pins);
102 if (ret)
103 return ret;
104
105 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
106 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
107 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
108 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
109
110 return snd_soc_component_set_jack(snd_soc_rtd_to_codec(runtime, 0)->component, jack, NULL);
111 }
112
113 static void avs_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
114 {
115 snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
116 }
117
118 static int
119 avs_nau8825_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
120 {
121 struct snd_interval *rate, *channels;
122 struct snd_mask *fmt;
123
124 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
125 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
126 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
127
128 /* The ADSP will convert the FE rate to 48k, stereo */
129 rate->min = rate->max = 48000;
130 channels->min = channels->max = 2;
131
132 /* set SSP to 24 bit */
133 snd_mask_none(fmt);
134 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
135
136 return 0;
137 }
138
139 static int avs_nau8825_trigger(struct snd_pcm_substream *substream, int cmd)
140 {
141 struct snd_pcm_runtime *runtime = substream->runtime;
142 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
143 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtm, 0);
144 int ret = 0;
145
146 switch (cmd) {
147 case SNDRV_PCM_TRIGGER_START:
148 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN);
149 if (ret < 0) {
150 dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
151 break;
152 }
153
154 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256);
155 if (ret < 0)
156 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
157 break;
158
159 case SNDRV_PCM_TRIGGER_RESUME:
160 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256);
161 if (ret < 0)
162 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
163 break;
164 }
165
166 return ret;
167 }
168
169
170 static const struct snd_soc_ops avs_nau8825_ops = {
171 .trigger = avs_nau8825_trigger,
172 };
173
174 static int avs_create_dai_link(struct device *dev, int ssp_port, int tdm_slot,
175 struct snd_soc_dai_link **dai_link)
176 {
177 struct snd_soc_dai_link_component *platform;
178 struct snd_soc_dai_link *dl;
179
180 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
181 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
182 if (!dl || !platform)
183 return -ENOMEM;
184
185 dl->name = devm_kasprintf(dev, GFP_KERNEL,
186 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
187 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
188 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
189 if (!dl->name || !dl->cpus || !dl->codecs)
190 return -ENOMEM;
191
192 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
193 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
194 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10508825:00");
195 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, SKL_NUVOTON_CODEC_DAI);
196 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
197 return -ENOMEM;
198
199 platform->name = dev_name(dev);
200 dl->num_cpus = 1;
201 dl->num_codecs = 1;
202 dl->platforms = platform;
203 dl->num_platforms = 1;
204 dl->id = 0;
205 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
206 dl->init = avs_nau8825_codec_init;
207 dl->exit = avs_nau8825_codec_exit;
208 dl->be_hw_params_fixup = avs_nau8825_be_fixup;
209 dl->ops = &avs_nau8825_ops;
210 dl->nonatomic = 1;
211 dl->no_pcm = 1;
212
213 *dai_link = dl;
214
215 return 0;
216 }
217
218 static int avs_card_suspend_pre(struct snd_soc_card *card)
219 {
220 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
221
222 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
223 }
224
225 static int avs_card_resume_post(struct snd_soc_card *card)
226 {
227 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
228 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
229 int stream = SNDRV_PCM_STREAM_PLAYBACK;
230
231 if (!codec_dai) {
232 dev_err(card->dev, "Codec dai not found\n");
233 return -EINVAL;
234 }
235
236 if (snd_soc_dai_stream_active(codec_dai, stream) &&
237 snd_soc_dai_get_widget(codec_dai, stream)->active)
238 snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN);
239
240 return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
241 }
242
243 static int avs_nau8825_probe(struct platform_device *pdev)
244 {
245 struct snd_soc_dai_link *dai_link;
246 struct snd_soc_acpi_mach *mach;
247 struct avs_mach_pdata *pdata;
248 struct snd_soc_card *card;
249 struct snd_soc_jack *jack;
250 struct device *dev = &pdev->dev;
251 int ssp_port, tdm_slot, ret;
252
253 mach = dev_get_platdata(dev);
254 pdata = mach->pdata;
255
256 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
257 if (ret)
258 return ret;
259
260 ret = avs_create_dai_link(dev, ssp_port, tdm_slot, &dai_link);
261 if (ret) {
262 dev_err(dev, "Failed to create dai link: %d", ret);
263 return ret;
264 }
265
266 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
267 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
268 if (!jack || !card)
269 return -ENOMEM;
270
271 if (pdata->obsolete_card_names) {
272 card->name = "avs_nau8825";
273 } else {
274 card->driver_name = "avs_nau8825";
275 card->long_name = card->name = "AVS I2S NAU8825";
276 }
277 card->dev = dev;
278 card->owner = THIS_MODULE;
279 card->suspend_pre = avs_card_suspend_pre;
280 card->resume_post = avs_card_resume_post;
281 card->dai_link = dai_link;
282 card->num_links = 1;
283 card->controls = card_controls;
284 card->num_controls = ARRAY_SIZE(card_controls);
285 card->dapm_widgets = card_widgets;
286 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
287 card->dapm_routes = card_base_routes;
288 card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
289 card->fully_routed = true;
290 snd_soc_card_set_drvdata(card, jack);
291
292 return devm_snd_soc_register_deferrable_card(dev, card);
293 }
294
295 static const struct platform_device_id avs_nau8825_driver_ids[] = {
296 {
297 .name = "avs_nau8825",
298 },
299 {},
300 };
301 MODULE_DEVICE_TABLE(platform, avs_nau8825_driver_ids);
302
303 static struct platform_driver avs_nau8825_driver = {
304 .probe = avs_nau8825_probe,
305 .driver = {
306 .name = "avs_nau8825",
307 .pm = &snd_soc_pm_ops,
308 },
309 .id_table = avs_nau8825_driver_ids,
310 };
311
312 module_platform_driver(avs_nau8825_driver)
313
314 MODULE_DESCRIPTION("Intel nau8825 machine driver");
315 MODULE_LICENSE("GPL");