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