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