Debian Codegraph @

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

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2023 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <[email protected]>
6 // Amadeusz Slawinski <[email protected]>
7 //
8
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/processor.h>
15 #include <linux/slab.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-acpi.h>
21 #include <asm/cpu_device_id.h>
22 #include "../utils.h"
23
24 #define ES8336_CODEC_DAI "ES8316 HiFi"
25
26 struct avs_card_drvdata {
27 struct snd_soc_jack jack;
28 struct gpio_desc *gpiod;
29 };
30
31 static const struct acpi_gpio_params enable_gpio = { 0, 0, true };
32
33 static const struct acpi_gpio_mapping speaker_gpios[] = {
34 { "speaker-enable-gpios", &enable_gpio, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
35 { }
36 };
37
38 static int avs_es8336_speaker_power_event(struct snd_soc_dapm_widget *w,
39 struct snd_kcontrol *kcontrol, int event)
40 {
41 struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
42 struct avs_card_drvdata *data;
43 bool speaker_en;
44
45 data = snd_soc_card_get_drvdata(card);
46 /* As enable_gpio has active_low=true, logic is inverted. */
47 speaker_en = !SND_SOC_DAPM_EVENT_ON(event);
48
49 gpiod_set_value_cansleep(data->gpiod, speaker_en);
50 return 0;
51 }
52
53 static const struct snd_soc_dapm_widget card_widgets[] = {
54 SND_SOC_DAPM_SPK("Speaker", NULL),
55 SND_SOC_DAPM_HP("Headphone", NULL),
56 SND_SOC_DAPM_MIC("Headset Mic", NULL),
57 SND_SOC_DAPM_MIC("Internal Mic", NULL),
58
59 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
60 avs_es8336_speaker_power_event,
61 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
62 };
63
64 static const struct snd_soc_dapm_route card_routes[] = {
65 {"Headphone", NULL, "HPOL"},
66 {"Headphone", NULL, "HPOR"},
67
68 /*
69 * There is no separate speaker output instead the speakers are muxed to
70 * the HP outputs. The mux is controlled by the "Speaker Power" widget.
71 */
72 {"Speaker", NULL, "HPOL"},
73 {"Speaker", NULL, "HPOR"},
74 {"Speaker", NULL, "Speaker Power"},
75
76 /* Mic route map */
77 {"MIC1", NULL, "Internal Mic"},
78 {"MIC2", NULL, "Headset Mic"},
79 };
80
81 static const struct snd_kcontrol_new card_controls[] = {
82 SOC_DAPM_PIN_SWITCH("Speaker"),
83 SOC_DAPM_PIN_SWITCH("Headphone"),
84 SOC_DAPM_PIN_SWITCH("Headset Mic"),
85 SOC_DAPM_PIN_SWITCH("Internal Mic"),
86 };
87
88 static const struct snd_soc_jack_pin card_headset_pins[] = {
89 {
90 .pin = "Headphone",
91 .mask = SND_JACK_HEADPHONE,
92 },
93 {
94 .pin = "Headset Mic",
95 .mask = SND_JACK_MICROPHONE,
96 },
97 };
98
99 static int avs_es8336_codec_init(struct snd_soc_pcm_runtime *runtime)
100 {
101 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
102 struct snd_soc_component *component = codec_dai->component;
103 struct snd_soc_card *card = runtime->card;
104 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
105 struct snd_soc_jack_pin *pins;
106 struct avs_card_drvdata *data;
107 struct gpio_desc *gpiod;
108 int num_pins, ret;
109
110 data = snd_soc_card_get_drvdata(card);
111 num_pins = ARRAY_SIZE(card_headset_pins);
112
113 pins = devm_kmemdup_array(card->dev, card_headset_pins, num_pins,
114 sizeof(card_headset_pins[0]), GFP_KERNEL);
115 if (!pins)
116 return -ENOMEM;
117
118 ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0,
119 &data->jack, pins, num_pins);
120 if (ret)
121 return ret;
122
123 ret = devm_acpi_dev_add_driver_gpios(codec_dai->dev, speaker_gpios);
124 if (ret)
125 dev_warn(codec_dai->dev, "Unable to add GPIO mapping table\n");
126
127 gpiod = gpiod_get_optional(codec_dai->dev, "speaker-enable", GPIOD_OUT_LOW);
128 if (IS_ERR(gpiod))
129 return dev_err_probe(codec_dai->dev, PTR_ERR(gpiod), "Get gpiod failed: %ld\n",
130 PTR_ERR(gpiod));
131
132 data->gpiod = gpiod;
133 snd_jack_set_key(data->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
134 snd_soc_component_set_jack(component, &data->jack, NULL);
135
136 snd_soc_dapm_set_idle_bias(dapm, false);
137
138 return 0;
139 }
140
141 static void avs_es8336_codec_exit(struct snd_soc_pcm_runtime *runtime)
142 {
143 struct avs_card_drvdata *data = snd_soc_card_get_drvdata(runtime->card);
144 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
145
146 snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
147 gpiod_put(data->gpiod);
148 }
149
150 static int avs_es8336_hw_params(struct snd_pcm_substream *substream,
151 struct snd_pcm_hw_params *params)
152 {
153 struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
154 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
155 int clk_freq;
156 int ret;
157
158 switch (boot_cpu_data.x86_vfm) {
159 case INTEL_KABYLAKE_L:
160 case INTEL_KABYLAKE:
161 clk_freq = 24000000;
162 break;
163 default:
164 clk_freq = 19200000;
165 break;
166 }
167
168 ret = snd_soc_dai_set_sysclk(codec_dai, 1, clk_freq, SND_SOC_CLOCK_OUT);
169 if (ret < 0)
170 dev_err(runtime->dev, "Set codec sysclk failed: %d\n", ret);
171
172 return ret;
173 }
174
175 static const struct snd_soc_ops avs_es8336_ops = {
176 .hw_params = avs_es8336_hw_params,
177 };
178
179 static int avs_es8336_be_fixup(struct snd_soc_pcm_runtime *runtime,
180 struct snd_pcm_hw_params *params)
181 {
182 struct snd_interval *rate, *channels;
183 struct snd_mask *fmt;
184
185 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
186 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
187 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
188
189 /* The ADSP will convert the FE rate to 48k, stereo */
190 rate->min = rate->max = 48000;
191 channels->min = channels->max = 2;
192
193 /* set SSPN to 24 bit */
194 snd_mask_none(fmt);
195 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_3LE);
196
197 return 0;
198 }
199
200 static int avs_create_dai_link(struct device *dev, int ssp_port, int tdm_slot,
201 struct snd_soc_dai_link **dai_link)
202 {
203 struct snd_soc_dai_link_component *platform;
204 struct snd_soc_dai_link *dl;
205
206 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
207 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
208 if (!dl || !platform)
209 return -ENOMEM;
210
211 dl->name = devm_kasprintf(dev, GFP_KERNEL,
212 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
213 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
214 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
215 if (!dl->name || !dl->cpus || !dl->codecs)
216 return -ENOMEM;
217
218 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
219 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
220 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-ESSX8336:00");
221 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, ES8336_CODEC_DAI);
222 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
223 return -ENOMEM;
224
225 platform->name = dev_name(dev);
226 dl->num_cpus = 1;
227 dl->num_codecs = 1;
228 dl->platforms = platform;
229 dl->num_platforms = 1;
230 dl->id = 0;
231 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
232 dl->init = avs_es8336_codec_init;
233 dl->exit = avs_es8336_codec_exit;
234 dl->be_hw_params_fixup = avs_es8336_be_fixup;
235 dl->ops = &avs_es8336_ops;
236 dl->nonatomic = 1;
237 dl->no_pcm = 1;
238
239 *dai_link = dl;
240
241 return 0;
242 }
243
244 static int avs_card_suspend_pre(struct snd_soc_card *card)
245 {
246 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI);
247
248 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
249 }
250
251 static int avs_card_resume_post(struct snd_soc_card *card)
252 {
253 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI);
254 struct avs_card_drvdata *data = snd_soc_card_get_drvdata(card);
255
256 return snd_soc_component_set_jack(codec_dai->component, &data->jack, NULL);
257 }
258
259 static int avs_es8336_probe(struct platform_device *pdev)
260 {
261 struct snd_soc_dai_link *dai_link;
262 struct snd_soc_acpi_mach *mach;
263 struct avs_mach_pdata *pdata;
264 struct avs_card_drvdata *data;
265 struct snd_soc_card *card;
266 struct device *dev = &pdev->dev;
267 int ssp_port, tdm_slot, ret;
268
269 mach = dev_get_platdata(dev);
270 pdata = mach->pdata;
271
272 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
273 if (ret)
274 return ret;
275
276 ret = avs_create_dai_link(dev, ssp_port, tdm_slot, &dai_link);
277 if (ret) {
278 dev_err(dev, "Failed to create dai link: %d", ret);
279 return ret;
280 }
281
282 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
283 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
284 if (!data || !card)
285 return -ENOMEM;
286
287 if (pdata->obsolete_card_names) {
288 card->name = "avs_es8336";
289 } else {
290 card->driver_name = "avs_es8336";
291 card->long_name = card->name = "AVS I2S ES8336";
292 }
293 card->dev = dev;
294 card->owner = THIS_MODULE;
295 card->suspend_pre = avs_card_suspend_pre;
296 card->resume_post = avs_card_resume_post;
297 card->dai_link = dai_link;
298 card->num_links = 1;
299 card->controls = card_controls;
300 card->num_controls = ARRAY_SIZE(card_controls);
301 card->dapm_widgets = card_widgets;
302 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
303 card->dapm_routes = card_routes;
304 card->num_dapm_routes = ARRAY_SIZE(card_routes);
305 card->fully_routed = true;
306 snd_soc_card_set_drvdata(card, data);
307
308 return devm_snd_soc_register_deferrable_card(dev, card);
309 }
310
311 static const struct platform_device_id avs_es8336_driver_ids[] = {
312 {
313 .name = "avs_es8336",
314 },
315 {},
316 };
317 MODULE_DEVICE_TABLE(platform, avs_es8336_driver_ids);
318
319 static struct platform_driver avs_es8336_driver = {
320 .probe = avs_es8336_probe,
321 .driver = {
322 .name = "avs_es8336",
323 .pm = &snd_soc_pm_ops,
324 },
325 .id_table = avs_es8336_driver_ids,
326 };
327
328 module_platform_driver(avs_es8336_driver);
329
330 MODULE_DESCRIPTION("Intel es8336 machine driver");
331 MODULE_LICENSE("GPL");