Merge tag 'soc-drivers-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / sound / soc / intel / boards / bytcht_es8316.c
CommitLineData
8e8e69d6 1// SPDX-License-Identifier: GPL-2.0-only
a03bdaa5
DD
2/*
3 * bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
4 * platforms with Everest ES8316 SoC
5 *
6 * Copyright (C) 2017 Endless Mobile, Inc.
7 * Authors: David Yang <yangxiaohua@everest-semi.com>,
8 * Daniel Drake <drake@endlessm.com>
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
a03bdaa5
DD
12 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 */
0d3e91da 14#include <linux/acpi.h>
6ca382c4
HG
15#include <linux/clk.h>
16#include <linux/device.h>
a8d218f4 17#include <linux/dmi.h>
0d3e91da
HG
18#include <linux/gpio/consumer.h>
19#include <linux/i2c.h>
a03bdaa5 20#include <linux/init.h>
4bf538b4 21#include <linux/input.h>
a03bdaa5
DD
22#include <linux/module.h>
23#include <linux/platform_device.h>
a03bdaa5 24#include <linux/slab.h>
4bf538b4 25#include <sound/jack.h>
a03bdaa5
DD
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
7feb2f78 29#include <sound/soc-acpi.h>
b71e1d37 30#include "../../codecs/es83xx-dsm-common.h"
a03bdaa5 31#include "../atom/sst-atom-controls.h"
536cfd2f 32#include "../common/soc-intel-quirks.h"
a03bdaa5 33
ba49cf6f
PC
34/* jd-inv + terminating entry */
35#define MAX_NO_PROPS 2
36
a03bdaa5
DD
37struct byt_cht_es8316_private {
38 struct clk *mclk;
4bf538b4 39 struct snd_soc_jack jack;
0d3e91da 40 struct gpio_desc *speaker_en_gpio;
c50f126b 41 struct device *codec_dev;
0d3e91da 42 bool speaker_en;
a03bdaa5
DD
43};
44
730501a9
HG
45enum {
46 BYT_CHT_ES8316_INTMIC_IN1_MAP,
47 BYT_CHT_ES8316_INTMIC_IN2_MAP,
48};
49
50#define BYT_CHT_ES8316_MAP(quirk) ((quirk) & GENMASK(3, 0))
349e1386 51#define BYT_CHT_ES8316_SSP0 BIT(16)
249d2fc9 52#define BYT_CHT_ES8316_MONO_SPEAKER BIT(17)
ba49cf6f 53#define BYT_CHT_ES8316_JD_INVERTED BIT(18)
349e1386 54
1fb1e93a 55static unsigned long quirk;
349e1386
HG
56
57static int quirk_override = -1;
58module_param_named(quirk, quirk_override, int, 0444);
59MODULE_PARM_DESC(quirk, "Board-specific quirk override");
60
61static void log_quirks(struct device *dev)
62{
730501a9
HG
63 if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN1_MAP)
64 dev_info(dev, "quirk IN1_MAP enabled");
65 if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN2_MAP)
66 dev_info(dev, "quirk IN2_MAP enabled");
349e1386
HG
67 if (quirk & BYT_CHT_ES8316_SSP0)
68 dev_info(dev, "quirk SSP0 enabled");
249d2fc9
HG
69 if (quirk & BYT_CHT_ES8316_MONO_SPEAKER)
70 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
ba49cf6f
PC
71 if (quirk & BYT_CHT_ES8316_JD_INVERTED)
72 dev_info(dev, "quirk JD_INVERTED enabled\n");
349e1386
HG
73}
74
0d3e91da
HG
75static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
76 struct snd_kcontrol *kcontrol, int event)
77{
78 struct snd_soc_card *card = w->dapm->card;
79 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
80
81 if (SND_SOC_DAPM_EVENT_ON(event))
82 priv->speaker_en = true;
83 else
84 priv->speaker_en = false;
85
86 gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
87
88 return 0;
89}
90
a03bdaa5 91static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
0d3e91da 92 SND_SOC_DAPM_SPK("Speaker", NULL),
a03bdaa5 93 SND_SOC_DAPM_HP("Headphone", NULL),
4bf538b4 94 SND_SOC_DAPM_MIC("Headset Mic", NULL),
730501a9 95 SND_SOC_DAPM_MIC("Internal Mic", NULL),
0d3e91da
HG
96
97 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
98 byt_cht_es8316_speaker_power_event,
99 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
a03bdaa5
DD
100};
101
102static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
a03bdaa5
DD
103 {"Headphone", NULL, "HPOL"},
104 {"Headphone", NULL, "HPOR"},
0d3e91da
HG
105
106 /*
107 * There is no separate speaker output instead the speakers are muxed to
108 * the HP outputs. The mux is controlled by the "Speaker Power" supply.
109 */
110 {"Speaker", NULL, "HPOL"},
111 {"Speaker", NULL, "HPOR"},
112 {"Speaker", NULL, "Speaker Power"},
349e1386
HG
113};
114
730501a9
HG
115static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in1_map[] = {
116 {"MIC1", NULL, "Internal Mic"},
117 {"MIC2", NULL, "Headset Mic"},
118};
119
120static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in2_map[] = {
121 {"MIC2", NULL, "Internal Mic"},
122 {"MIC1", NULL, "Headset Mic"},
123};
124
349e1386
HG
125static const struct snd_soc_dapm_route byt_cht_es8316_ssp0_map[] = {
126 {"Playback", NULL, "ssp0 Tx"},
127 {"ssp0 Tx", NULL, "modem_out"},
128 {"modem_in", NULL, "ssp0 Rx"},
129 {"ssp0 Rx", NULL, "Capture"},
130};
a03bdaa5 131
349e1386 132static const struct snd_soc_dapm_route byt_cht_es8316_ssp2_map[] = {
a03bdaa5
DD
133 {"Playback", NULL, "ssp2 Tx"},
134 {"ssp2 Tx", NULL, "codec_out0"},
135 {"ssp2 Tx", NULL, "codec_out1"},
136 {"codec_in0", NULL, "ssp2 Rx" },
137 {"codec_in1", NULL, "ssp2 Rx" },
138 {"ssp2 Rx", NULL, "Capture"},
139};
140
141static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
0d3e91da 142 SOC_DAPM_PIN_SWITCH("Speaker"),
a03bdaa5 143 SOC_DAPM_PIN_SWITCH("Headphone"),
4bf538b4 144 SOC_DAPM_PIN_SWITCH("Headset Mic"),
730501a9 145 SOC_DAPM_PIN_SWITCH("Internal Mic"),
a03bdaa5
DD
146};
147
4bf538b4
HG
148static struct snd_soc_jack_pin byt_cht_es8316_jack_pins[] = {
149 {
150 .pin = "Headphone",
151 .mask = SND_JACK_HEADPHONE,
152 },
153 {
154 .pin = "Headset Mic",
155 .mask = SND_JACK_MICROPHONE,
156 },
157};
158
a03bdaa5
DD
159static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
160{
a2c1125e 161 struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
a03bdaa5
DD
162 struct snd_soc_card *card = runtime->card;
163 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
349e1386
HG
164 const struct snd_soc_dapm_route *custom_map;
165 int num_routes;
a03bdaa5
DD
166 int ret;
167
168 card->dapm.idle_bias_off = true;
169
730501a9
HG
170 switch (BYT_CHT_ES8316_MAP(quirk)) {
171 case BYT_CHT_ES8316_INTMIC_IN1_MAP:
172 default:
173 custom_map = byt_cht_es8316_intmic_in1_map;
174 num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in1_map);
175 break;
176 case BYT_CHT_ES8316_INTMIC_IN2_MAP:
177 custom_map = byt_cht_es8316_intmic_in2_map;
178 num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in2_map);
179 break;
180 }
181 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
182 if (ret)
183 return ret;
184
349e1386
HG
185 if (quirk & BYT_CHT_ES8316_SSP0) {
186 custom_map = byt_cht_es8316_ssp0_map;
187 num_routes = ARRAY_SIZE(byt_cht_es8316_ssp0_map);
188 } else {
189 custom_map = byt_cht_es8316_ssp2_map;
190 num_routes = ARRAY_SIZE(byt_cht_es8316_ssp2_map);
191 }
192 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
193 if (ret)
194 return ret;
195
a03bdaa5
DD
196 /*
197 * The firmware might enable the clock at boot (this information
198 * may or may not be reflected in the enable clock register).
199 * To change the rate we must disable the clock first to cover these
200 * cases. Due to common clock framework restrictions that do not allow
201 * to disable a clock that has not been enabled, we need to enable
202 * the clock first.
203 */
204 ret = clk_prepare_enable(priv->mclk);
205 if (!ret)
206 clk_disable_unprepare(priv->mclk);
207
208 ret = clk_set_rate(priv->mclk, 19200000);
209 if (ret)
210 dev_err(card->dev, "unable to set MCLK rate\n");
211
212 ret = clk_prepare_enable(priv->mclk);
213 if (ret)
214 dev_err(card->dev, "unable to enable MCLK\n");
215
a2c1125e 216 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(runtime, 0), 0, 19200000,
a03bdaa5
DD
217 SND_SOC_CLOCK_IN);
218 if (ret < 0) {
219 dev_err(card->dev, "can't set codec clock %d\n", ret);
220 return ret;
221 }
222
19aed2d6
AO
223 ret = snd_soc_card_jack_new_pins(card, "Headset",
224 SND_JACK_HEADSET | SND_JACK_BTN_0,
225 &priv->jack, byt_cht_es8316_jack_pins,
226 ARRAY_SIZE(byt_cht_es8316_jack_pins));
4bf538b4
HG
227 if (ret) {
228 dev_err(card->dev, "jack creation failed %d\n", ret);
229 return ret;
230 }
231
232 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
233 snd_soc_component_set_jack(codec, &priv->jack, NULL);
234
a03bdaa5
DD
235 return 0;
236}
237
a03bdaa5
DD
238static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
239 struct snd_pcm_hw_params *params)
240{
241 struct snd_interval *rate = hw_param_interval(params,
242 SNDRV_PCM_HW_PARAM_RATE);
243 struct snd_interval *channels = hw_param_interval(params,
244 SNDRV_PCM_HW_PARAM_CHANNELS);
349e1386 245 int ret, bits;
a03bdaa5 246
4088355a 247 /* The DSP will convert the FE rate to 48k, stereo */
a03bdaa5
DD
248 rate->min = rate->max = 48000;
249 channels->min = channels->max = 2;
250
349e1386
HG
251 if (quirk & BYT_CHT_ES8316_SSP0) {
252 /* set SSP0 to 16-bit */
253 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
254 bits = 16;
255 } else {
256 /* set SSP2 to 24-bit */
257 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
258 bits = 24;
259 }
a03bdaa5
DD
260
261 /*
262 * Default mode for SSP configuration is TDM 4 slot, override config
263 * with explicit setting to I2S 2ch 24-bit. The word length is set with
264 * dai_set_tdm_slot() since there is no other API exposed
265 */
a2c1125e 266 ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
a03bdaa5
DD
267 SND_SOC_DAIFMT_I2S |
268 SND_SOC_DAIFMT_NB_NF |
add9ee8c 269 SND_SOC_DAIFMT_BP_FP
a03bdaa5
DD
270 );
271 if (ret < 0) {
272 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
273 return ret;
274 }
275
a2c1125e 276 ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
a03bdaa5
DD
277 if (ret < 0) {
278 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
279 return ret;
280 }
281
282 return 0;
283}
284
285static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
286{
287 return snd_pcm_hw_constraint_single(substream->runtime,
288 SNDRV_PCM_HW_PARAM_RATE, 48000);
289}
290
291static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
292 .startup = byt_cht_es8316_aif1_startup,
293};
294
143029db
KM
295SND_SOC_DAILINK_DEF(dummy,
296 DAILINK_COMP_ARRAY(COMP_DUMMY()));
297
298SND_SOC_DAILINK_DEF(media,
299 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
300
301SND_SOC_DAILINK_DEF(deepbuffer,
302 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
303
304SND_SOC_DAILINK_DEF(ssp2_port,
305 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
306SND_SOC_DAILINK_DEF(ssp2_codec,
307 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8316:00", "ES8316 HiFi")));
308
309SND_SOC_DAILINK_DEF(platform,
310 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
311
a03bdaa5
DD
312static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
313 [MERR_DPCM_AUDIO] = {
314 .name = "Audio Port",
315 .stream_name = "Audio",
a03bdaa5
DD
316 .nonatomic = true,
317 .dynamic = 1,
318 .dpcm_playback = 1,
319 .dpcm_capture = 1,
320 .ops = &byt_cht_es8316_aif1_ops,
143029db 321 SND_SOC_DAILINK_REG(media, dummy, platform),
a03bdaa5
DD
322 },
323
324 [MERR_DPCM_DEEP_BUFFER] = {
325 .name = "Deep-Buffer Audio Port",
326 .stream_name = "Deep-Buffer Audio",
a03bdaa5
DD
327 .nonatomic = true,
328 .dynamic = 1,
329 .dpcm_playback = 1,
330 .ops = &byt_cht_es8316_aif1_ops,
143029db 331 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
a03bdaa5
DD
332 },
333
a03bdaa5
DD
334 /* back ends */
335 {
a03bdaa5 336 .name = "SSP2-Codec",
149f7757 337 .id = 0,
a03bdaa5 338 .no_pcm = 1,
a03bdaa5 339 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
5374b921 340 | SND_SOC_DAIFMT_CBC_CFC,
a03bdaa5 341 .be_hw_params_fixup = byt_cht_es8316_codec_fixup,
a03bdaa5
DD
342 .dpcm_playback = 1,
343 .dpcm_capture = 1,
344 .init = byt_cht_es8316_init,
143029db 345 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
a03bdaa5
DD
346 },
347};
348
349
350/* SoC card */
4bf538b4 351static char codec_name[SND_ACPI_I2C_ID_LEN];
b5706f8e 352#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
249d2fc9 353static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
b5706f8e 354#endif
0d5c8187 355static char components_string[32]; /* = "cfg-spk:* cfg-mic:* */
4bf538b4
HG
356
357static int byt_cht_es8316_suspend(struct snd_soc_card *card)
358{
359 struct snd_soc_component *component;
360
361 for_each_card_components(card, component) {
362 if (!strcmp(component->name, codec_name)) {
363 dev_dbg(component->dev, "disabling jack detect before suspend\n");
364 snd_soc_component_set_jack(component, NULL, NULL);
365 break;
366 }
367 }
368
369 return 0;
370}
371
372static int byt_cht_es8316_resume(struct snd_soc_card *card)
373{
374 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
375 struct snd_soc_component *component;
376
377 for_each_card_components(card, component) {
378 if (!strcmp(component->name, codec_name)) {
379 dev_dbg(component->dev, "re-enabling jack detect after resume\n");
380 snd_soc_component_set_jack(component, &priv->jack, NULL);
381 break;
382 }
383 }
384
0d3e91da
HG
385 /*
386 * Some Cherry Trail boards with an ES8316 codec have a bug in their
387 * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
388 * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
389 * that this really is a bug and the GPIO has no influence on the
390 * touchscreen at all.
391 *
392 * The silead.c touchscreen driver does not support runtime suspend, so
393 * the GPIO can only be changed underneath us during a system suspend.
394 * This resume() function runs from a pm complete() callback, and thus
395 * is guaranteed to run after the touchscreen driver/ACPI-subsys has
396 * brought the touchscreen back up again (and thus changed the GPIO).
397 *
398 * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
399 * requesting the GPIO and we set its value here to undo any changes
400 * done by the touchscreen's broken _PS0 ACPI method.
401 */
402 gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
403
4bf538b4
HG
404 return 0;
405}
406
b4ecd58b 407/* use space before codec name to simplify card ID, and simplify driver name */
41656c3d
PLB
408#define SOF_CARD_NAME "bytcht es8316" /* card name will be 'sof-bytcht es8316' */
409#define SOF_DRIVER_NAME "SOF"
410
b4ecd58b
PLB
411#define CARD_NAME "bytcht-es8316"
412#define DRIVER_NAME NULL /* card name will be used for driver name */
b4ecd58b 413
a03bdaa5 414static struct snd_soc_card byt_cht_es8316_card = {
a03bdaa5
DD
415 .owner = THIS_MODULE,
416 .dai_link = byt_cht_es8316_dais,
417 .num_links = ARRAY_SIZE(byt_cht_es8316_dais),
418 .dapm_widgets = byt_cht_es8316_widgets,
419 .num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
420 .dapm_routes = byt_cht_es8316_audio_map,
421 .num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
422 .controls = byt_cht_es8316_controls,
423 .num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
424 .fully_routed = true,
4bf538b4
HG
425 .suspend_pre = byt_cht_es8316_suspend,
426 .resume_post = byt_cht_es8316_resume,
a03bdaa5
DD
427};
428
0d3e91da
HG
429static const struct acpi_gpio_params first_gpio = { 0, 0, false };
430
431static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
432 { "speaker-enable-gpios", &first_gpio, 1 },
433 { },
434};
435
a8d218f4
PC
436/* Please keep this list alphabetically sorted */
437static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
aa2ba991
HG
438 { /* Irbis NB41 */
439 .matches = {
440 DMI_MATCH(DMI_SYS_VENDOR, "IRBIS"),
441 DMI_MATCH(DMI_PRODUCT_NAME, "NB41"),
442 },
869bced7
HG
443 .driver_data = (void *)(BYT_CHT_ES8316_SSP0
444 | BYT_CHT_ES8316_INTMIC_IN2_MAP
aa2ba991
HG
445 | BYT_CHT_ES8316_JD_INVERTED),
446 },
8bb0ac0e
HG
447 { /* Nanote UMPC-01 */
448 .matches = {
449 DMI_MATCH(DMI_SYS_VENDOR, "RWC CO.,LTD"),
450 DMI_MATCH(DMI_PRODUCT_NAME, "UMPC-01"),
451 },
452 .driver_data = (void *)BYT_CHT_ES8316_INTMIC_IN1_MAP,
453 },
a8d218f4
PC
454 { /* Teclast X98 Plus II */
455 .matches = {
456 DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
457 DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
458 },
459 .driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
460 | BYT_CHT_ES8316_JD_INVERTED),
461 },
462 {}
463};
464
7650862f
HG
465static int byt_cht_es8316_get_quirks_from_dsm(struct byt_cht_es8316_private *priv,
466 bool is_bytcr)
467{
468 int ret, val1, val2, dsm_quirk = 0;
469
470 if (is_bytcr)
471 dsm_quirk |= BYT_CHT_ES8316_SSP0;
472
473 ret = es83xx_dsm(priv->codec_dev, PLATFORM_MAINMIC_TYPE_ARG, &val1);
474 if (ret < 0)
475 return ret;
476
477 ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPMIC_TYPE_ARG, &val2);
478 if (ret < 0)
479 return ret;
480
481 if (val1 == PLATFORM_MIC_AMIC_LIN1RIN1 && val2 == PLATFORM_MIC_AMIC_LIN2RIN2) {
482 dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN1_MAP;
483 } else if (val1 == PLATFORM_MIC_AMIC_LIN2RIN2 && val2 == PLATFORM_MIC_AMIC_LIN1RIN1) {
484 dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN2_MAP;
485 } else {
486 dev_warn(priv->codec_dev, "Unknown mic settings mainmic 0x%02x hpmic 0x%02x\n",
487 val1, val2);
488 return -EINVAL;
489 }
490
491 ret = es83xx_dsm(priv->codec_dev, PLATFORM_SPK_TYPE_ARG, &val1);
492 if (ret < 0)
493 return ret;
494
495 switch (val1) {
496 case PLATFORM_SPK_MONO:
497 dsm_quirk |= BYT_CHT_ES8316_MONO_SPEAKER;
498 break;
499 case PLATFORM_SPK_STEREO:
500 break;
501 default:
502 dev_warn(priv->codec_dev, "Unknown speaker setting 0x%02x\n", val1);
503 return -EINVAL;
504 }
505
506 ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPDET_INV_ARG, &val1);
507 if (ret < 0)
508 return ret;
509
510 switch (val1) {
511 case PLATFORM_HPDET_NORMAL:
512 break;
513 case PLATFORM_HPDET_INVERTED:
514 dsm_quirk |= BYT_CHT_ES8316_JD_INVERTED;
515 break;
516 default:
517 dev_warn(priv->codec_dev, "Unknown hpdet-inv setting 0x%02x\n", val1);
518 return -EINVAL;
519 }
520
521 quirk = dsm_quirk;
522 return 0;
523}
524
a03bdaa5
DD
525static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
526{
5f6c1341 527 struct device *dev = &pdev->dev;
4ffdca62 528 static const char * const mic_name[] = { "in1", "in2" };
5f6c1341 529 struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
ba49cf6f 530 struct property_entry props[MAX_NO_PROPS] = {};
a03bdaa5 531 struct byt_cht_es8316_private *priv;
a8d218f4 532 const struct dmi_system_id *dmi_id;
0bd3c071 533 struct fwnode_handle *fwnode;
e8acf91a 534 bool sof_parent, is_bytcr;
e4bc6b11 535 const char *platform_name;
645056da 536 struct acpi_device *adev;
0d3e91da 537 struct device *codec_dev;
ba49cf6f 538 unsigned int cnt = 0;
3c22a73f
PLB
539 int dai_index = 0;
540 int i;
541 int ret = 0;
a03bdaa5 542
86909c8f 543 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
a03bdaa5
DD
544 if (!priv)
545 return -ENOMEM;
546
3c22a73f
PLB
547 /* fix index of codec dai */
548 for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
7d99a70b
HG
549 if (byt_cht_es8316_dais[i].codecs->name &&
550 !strcmp(byt_cht_es8316_dais[i].codecs->name,
3c22a73f
PLB
551 "i2c-ESSX8316:00")) {
552 dai_index = i;
553 break;
554 }
555 }
556
557 /* fixup codec name based on HID */
645056da
AS
558 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
559 if (adev) {
3c22a73f 560 snprintf(codec_name, sizeof(codec_name),
645056da 561 "i2c-%s", acpi_dev_name(adev));
143029db 562 byt_cht_es8316_dais[dai_index].codecs->name = codec_name;
69efe3b8 563 } else {
e8ccf82b 564 dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
69efe3b8 565 return -ENXIO;
3c22a73f
PLB
566 }
567
6b1c0bd6
AS
568 codec_dev = acpi_get_first_physical_node(adev);
569 acpi_dev_put(adev);
570 if (!codec_dev)
571 return -EPROBE_DEFER;
572 priv->codec_dev = get_device(codec_dev);
573
f1eebb3b 574 /* override platform name, if required */
79136a01 575 byt_cht_es8316_card.dev = dev;
e4bc6b11
PLB
576 platform_name = mach->mach_params.platform;
577
578 ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
579 platform_name);
6b1c0bd6
AS
580 if (ret) {
581 put_device(codec_dev);
e4bc6b11 582 return ret;
6b1c0bd6 583 }
e4bc6b11 584
b71e1d37
PLB
585 es83xx_dsm_dump(priv->codec_dev);
586
349e1386 587 /* Check for BYTCR or other platform and setup quirks */
e8acf91a 588 is_bytcr = soc_intel_is_byt() && mach->mach_params.acpi_ipc_irq_index == 0;
a8d218f4
PC
589 dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
590 if (dmi_id) {
1fb1e93a 591 quirk = (unsigned long)dmi_id->driver_data;
7650862f
HG
592 } else if (!byt_cht_es8316_get_quirks_from_dsm(priv, is_bytcr)) {
593 dev_info(dev, "Using ACPI DSM info for quirks\n");
e8acf91a 594 } else if (is_bytcr) {
249d2fc9
HG
595 /* On BYTCR default to SSP0, internal-mic-in2-map, mono-spk */
596 quirk = BYT_CHT_ES8316_SSP0 | BYT_CHT_ES8316_INTMIC_IN2_MAP |
597 BYT_CHT_ES8316_MONO_SPEAKER;
349e1386 598 } else {
249d2fc9
HG
599 /* Others default to internal-mic-in1-map, mono-speaker */
600 quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
601 BYT_CHT_ES8316_MONO_SPEAKER;
349e1386
HG
602 }
603 if (quirk_override != -1) {
2697f3af
PLB
604 dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
605 quirk, quirk_override);
349e1386
HG
606 quirk = quirk_override;
607 }
608 log_quirks(dev);
609
610 if (quirk & BYT_CHT_ES8316_SSP0)
143029db 611 byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
349e1386 612
86909c8f
HG
613 /* get the clock */
614 priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
6b1c0bd6
AS
615 if (IS_ERR(priv->mclk)) {
616 put_device(codec_dev);
bea03a32 617 return dev_err_probe(dev, PTR_ERR(priv->mclk), "clk_get pmc_plt_clk_3 failed\n");
6b1c0bd6 618 }
0d3e91da 619
ba49cf6f
PC
620 if (quirk & BYT_CHT_ES8316_JD_INVERTED)
621 props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
622
623 if (cnt) {
0bd3c071
HK
624 fwnode = fwnode_create_software_node(props, NULL);
625 if (IS_ERR(fwnode)) {
626 put_device(codec_dev);
627 return PTR_ERR(fwnode);
628 }
629
630 ret = device_add_software_node(codec_dev, to_software_node(fwnode));
631
632 fwnode_handle_put(fwnode);
633
b3df80ab
JX
634 if (ret) {
635 put_device(codec_dev);
ba49cf6f 636 return ret;
b3df80ab 637 }
ba49cf6f
PC
638 }
639
fe0596a0 640 /* get speaker enable GPIO */
0d3e91da
HG
641 devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
642 priv->speaker_en_gpio =
4e03b1b7
AS
643 gpiod_get_optional(codec_dev, "speaker-enable",
644 /* see comment in byt_cht_es8316_resume() */
645 GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
0d3e91da 646 if (IS_ERR(priv->speaker_en_gpio)) {
bea03a32
AS
647 ret = dev_err_probe(dev, PTR_ERR(priv->speaker_en_gpio),
648 "get speaker GPIO failed\n");
649 goto err_put_codec;
0d3e91da
HG
650 }
651
0d5c8187
JK
652 snprintf(components_string, sizeof(components_string),
653 "cfg-spk:%s cfg-mic:%s",
654 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "1" : "2",
655 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
656 byt_cht_es8316_card.components = components_string;
b5706f8e 657#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
249d2fc9
HG
658 snprintf(long_name, sizeof(long_name), "bytcht-es8316-%s-spk-%s-mic",
659 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "mono" : "stereo",
660 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
661 byt_cht_es8316_card.long_name = long_name;
b5706f8e 662#endif
0d5c8187 663
e8ccf82b 664 sof_parent = snd_soc_acpi_sof_parent(dev);
41656c3d
PLB
665
666 /* set card and driver name */
667 if (sof_parent) {
668 byt_cht_es8316_card.name = SOF_CARD_NAME;
669 byt_cht_es8316_card.driver_name = SOF_DRIVER_NAME;
670 } else {
671 byt_cht_es8316_card.name = CARD_NAME;
672 byt_cht_es8316_card.driver_name = DRIVER_NAME;
673 }
674
05ff312b
PLB
675 /* set pm ops */
676 if (sof_parent)
677 dev->driver->pm = &snd_soc_pm_ops;
678
0d5c8187 679 /* register the soc card */
86909c8f
HG
680 snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
681
682 ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
a03bdaa5 683 if (ret) {
0d3e91da 684 gpiod_put(priv->speaker_en_gpio);
86909c8f 685 dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
c50f126b 686 goto err_put_codec;
a03bdaa5
DD
687 }
688 platform_set_drvdata(pdev, &byt_cht_es8316_card);
86909c8f 689 return 0;
c50f126b
PLB
690
691err_put_codec:
0bd3c071 692 device_remove_software_node(priv->codec_dev);
c50f126b
PLB
693 put_device(priv->codec_dev);
694 return ret;
a03bdaa5
DD
695}
696
9c5b13d5 697static void snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
0d3e91da 698{
f833fe20
WY
699 struct snd_soc_card *card = platform_get_drvdata(pdev);
700 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
0d3e91da
HG
701
702 gpiod_put(priv->speaker_en_gpio);
0bd3c071 703 device_remove_software_node(priv->codec_dev);
c50f126b 704 put_device(priv->codec_dev);
0d3e91da
HG
705}
706
a03bdaa5
DD
707static struct platform_driver snd_byt_cht_es8316_mc_driver = {
708 .driver = {
709 .name = "bytcht_es8316",
710 },
711 .probe = snd_byt_cht_es8316_mc_probe,
9c5b13d5 712 .remove_new = snd_byt_cht_es8316_mc_remove,
a03bdaa5
DD
713};
714
715module_platform_driver(snd_byt_cht_es8316_mc_driver);
716MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
717MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
718MODULE_LICENSE("GPL v2");
719MODULE_ALIAS("platform:bytcht_es8316");