| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* bytcr_wm5102.c - ASoc Machine driver for Intel Baytrail platforms with a |
| 4 |
* Wolfson Microelectronics WM5102 codec |
| 5 |
* |
| 6 |
* Copyright (C) 2020 Hans de Goede <[email protected]> |
| 7 |
* Loosely based on bytcr_rt5640.c which is: |
| 8 |
* Copyright (C) 2014-2020 Intel Corp |
| 9 |
* Author: Subhransu S. Prusty <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/acpi.h> |
| 13 |
#include <linux/bitfield.h> |
| 14 |
#include <linux/clk.h> |
| 15 |
#include <linux/device.h> |
| 16 |
#include <linux/init.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <linux/moduleparam.h> |
| 19 |
#include <linux/platform_data/x86/soc.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/slab.h> |
| 22 |
#include <linux/spi/spi.h> |
| 23 |
#include <sound/jack.h> |
| 24 |
#include <sound/pcm.h> |
| 25 |
#include <sound/pcm_params.h> |
| 26 |
#include <sound/soc.h> |
| 27 |
#include <sound/soc-acpi.h> |
| 28 |
#include "../../codecs/wm5102.h" |
| 29 |
#include "../atom/sst-atom-controls.h" |
| 30 |
|
| 31 |
#define WM5102_MAX_SYSCLK_4K 49152000 /* max sysclk for 4K family */ |
| 32 |
#define WM5102_MAX_SYSCLK_11025 45158400 /* max sysclk for 11.025K family */ |
| 33 |
|
| 34 |
struct byt_wm5102_private { |
| 35 |
struct snd_soc_jack jack; |
| 36 |
struct clk *mclk; |
| 37 |
struct gpio_desc *spkvdd_en_gpio; |
| 38 |
int mclk_freq; |
| 39 |
}; |
| 40 |
|
| 41 |
#define BYT_WM5102_IN_MAP GENMASK(3, 0) |
| 42 |
#define BYT_WM5102_OUT_MAP GENMASK(7, 4) |
| 43 |
#define BYT_WM5102_SSP2 BIT(16) |
| 44 |
#define BYT_WM5102_MCLK_19_2MHZ BIT(17) |
| 45 |
|
| 46 |
enum { |
| 47 |
BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L, |
| 48 |
BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L, |
| 49 |
}; |
| 50 |
|
| 51 |
/* Note these values are pre-shifted for easy use of setting quirks */ |
| 52 |
enum { |
| 53 |
BYT_WM5102_SPK_SPK_MAP = FIELD_PREP_CONST(BYT_WM5102_OUT_MAP, 0), |
| 54 |
BYT_WM5102_SPK_HPOUT2_MAP = FIELD_PREP_CONST(BYT_WM5102_OUT_MAP, 1), |
| 55 |
}; |
| 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 |
switch (quirk & BYT_WM5102_IN_MAP) { |
| 66 |
case BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L: |
| 67 |
dev_info_once(dev, "quirk INTMIC_IN3L_HSMIC_IN1L enabled\n"); |
| 68 |
break; |
| 69 |
case BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L: |
| 70 |
dev_info_once(dev, "quirk INTMIC_IN1L_HSMIC_IN2L enabled\n"); |
| 71 |
break; |
| 72 |
default: |
| 73 |
dev_warn_once(dev, "quirk sets invalid input map: 0x%lx, defaulting to INTMIC_IN3L_HSMIC_IN1L\n", |
| 74 |
quirk & BYT_WM5102_IN_MAP); |
| 75 |
quirk &= ~BYT_WM5102_IN_MAP; |
| 76 |
quirk |= BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L; |
| 77 |
break; |
| 78 |
} |
| 79 |
switch (quirk & BYT_WM5102_OUT_MAP) { |
| 80 |
case BYT_WM5102_SPK_SPK_MAP: |
| 81 |
dev_info_once(dev, "quirk SPK_SPK_MAP enabled\n"); |
| 82 |
break; |
| 83 |
case BYT_WM5102_SPK_HPOUT2_MAP: |
| 84 |
dev_info_once(dev, "quirk SPK_HPOUT2_MAP enabled\n"); |
| 85 |
break; |
| 86 |
default: |
| 87 |
dev_warn_once(dev, "quirk sets invalid output map: 0x%lx, defaulting to SPK_SPK_MAP\n", |
| 88 |
quirk & BYT_WM5102_OUT_MAP); |
| 89 |
quirk &= ~BYT_WM5102_OUT_MAP; |
| 90 |
quirk |= BYT_WM5102_SPK_SPK_MAP; |
| 91 |
break; |
| 92 |
} |
| 93 |
if (quirk & BYT_WM5102_SSP2) |
| 94 |
dev_info_once(dev, "quirk SSP2 enabled"); |
| 95 |
if (quirk & BYT_WM5102_MCLK_19_2MHZ) |
| 96 |
dev_info_once(dev, "quirk MCLK 19.2MHz enabled"); |
| 97 |
} |
| 98 |
|
| 99 |
static int byt_wm5102_spkvdd_power_event(struct snd_soc_dapm_widget *w, |
| 100 |
struct snd_kcontrol *kcontrol, int event) |
| 101 |
{ |
| 102 |
struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm); |
| 103 |
struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card); |
| 104 |
|
| 105 |
gpiod_set_value_cansleep(priv->spkvdd_en_gpio, |
| 106 |
!!SND_SOC_DAPM_EVENT_ON(event)); |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
static int byt_wm5102_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, int rate) |
| 112 |
{ |
| 113 |
struct snd_soc_component *codec_component = codec_dai->component; |
| 114 |
struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(codec_component->card); |
| 115 |
int sr_mult = ((rate % 4000) == 0) ? |
| 116 |
(WM5102_MAX_SYSCLK_4K / rate) : |
| 117 |
(WM5102_MAX_SYSCLK_11025 / rate); |
| 118 |
int ret; |
| 119 |
|
| 120 |
/* Reset FLL1 */ |
| 121 |
snd_soc_dai_set_pll(codec_dai, WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0); |
| 122 |
snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0); |
| 123 |
|
| 124 |
/* Configure the FLL1 PLL before selecting it */ |
| 125 |
ret = snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_CLK_SRC_MCLK1, |
| 126 |
priv->mclk_freq, rate * sr_mult); |
| 127 |
if (ret) { |
| 128 |
dev_err(codec_component->dev, "Error setting PLL: %d\n", ret); |
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
ret = snd_soc_component_set_sysclk(codec_component, ARIZONA_CLK_SYSCLK, |
| 133 |
ARIZONA_CLK_SRC_FLL1, rate * sr_mult, |
| 134 |
SND_SOC_CLOCK_IN); |
| 135 |
if (ret) { |
| 136 |
dev_err(codec_component->dev, "Error setting SYSCLK: %d\n", ret); |
| 137 |
return ret; |
| 138 |
} |
| 139 |
|
| 140 |
ret = snd_soc_dai_set_sysclk(codec_dai, ARIZONA_CLK_SYSCLK, |
| 141 |
rate * 512, SND_SOC_CLOCK_IN); |
| 142 |
if (ret) { |
| 143 |
dev_err(codec_component->dev, "Error setting clock: %d\n", ret); |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
static int platform_clock_control(struct snd_soc_dapm_widget *w, |
| 151 |
struct snd_kcontrol *k, int event) |
| 152 |
{ |
| 153 |
struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm); |
| 154 |
struct snd_soc_dai *codec_dai; |
| 155 |
struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card); |
| 156 |
int ret; |
| 157 |
|
| 158 |
codec_dai = snd_soc_card_get_codec_dai(card, "wm5102-aif1"); |
| 159 |
if (!codec_dai) { |
| 160 |
dev_err(card->dev, "Error codec DAI not found\n"); |
| 161 |
return -EIO; |
| 162 |
} |
| 163 |
|
| 164 |
if (SND_SOC_DAPM_EVENT_ON(event)) { |
| 165 |
ret = clk_prepare_enable(priv->mclk); |
| 166 |
if (ret) { |
| 167 |
dev_err(card->dev, "Error enabling MCLK: %d\n", ret); |
| 168 |
return ret; |
| 169 |
} |
| 170 |
ret = byt_wm5102_prepare_and_enable_pll1(codec_dai, 48000); |
| 171 |
if (ret) { |
| 172 |
dev_err(card->dev, "Error setting codec sysclk: %d\n", ret); |
| 173 |
clk_disable_unprepare(priv->mclk); |
| 174 |
return ret; |
| 175 |
} |
| 176 |
} else { |
| 177 |
/* |
| 178 |
* The WM5102 has a separate 32KHz clock for jack-detect |
| 179 |
* so we can disable the PLL, followed by disabling the |
| 180 |
* platform clock which is the source-clock for the PLL. |
| 181 |
*/ |
| 182 |
snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0); |
| 183 |
clk_disable_unprepare(priv->mclk); |
| 184 |
} |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
static const struct snd_soc_dapm_widget byt_wm5102_widgets[] = { |
| 190 |
SND_SOC_DAPM_HP("Headphone", NULL), |
| 191 |
SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 192 |
SND_SOC_DAPM_MIC("Internal Mic", NULL), |
| 193 |
SND_SOC_DAPM_SPK("Speaker", NULL), |
| 194 |
SND_SOC_DAPM_LINE("Line Out", NULL), |
| 195 |
SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, |
| 196 |
platform_clock_control, SND_SOC_DAPM_PRE_PMU | |
| 197 |
SND_SOC_DAPM_POST_PMD), |
| 198 |
SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0, |
| 199 |
byt_wm5102_spkvdd_power_event, |
| 200 |
SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU), |
| 201 |
}; |
| 202 |
|
| 203 |
static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = { |
| 204 |
{"Headphone", NULL, "Platform Clock"}, |
| 205 |
{"Headset Mic", NULL, "Platform Clock"}, |
| 206 |
{"Internal Mic", NULL, "Platform Clock"}, |
| 207 |
{"Speaker", NULL, "Platform Clock"}, |
| 208 |
{"Speaker", NULL, "Speaker VDD"}, |
| 209 |
|
| 210 |
{"Headphone", NULL, "HPOUT1L"}, |
| 211 |
{"Headphone", NULL, "HPOUT1R"}, |
| 212 |
|
| 213 |
/* |
| 214 |
* The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset |
| 215 |
* is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch. |
| 216 |
*/ |
| 217 |
{"Headset Mic", NULL, "MICBIAS1"}, |
| 218 |
{"Headset Mic", NULL, "MICBIAS2"}, |
| 219 |
{"Internal Mic", NULL, "MICBIAS3"}, |
| 220 |
}; |
| 221 |
|
| 222 |
static const struct snd_soc_dapm_route bytcr_wm5102_ssp0_map[] = { |
| 223 |
{"AIF1 Playback", NULL, "ssp0 Tx"}, |
| 224 |
{"ssp0 Tx", NULL, "modem_out"}, |
| 225 |
{"modem_in", NULL, "ssp0 Rx"}, |
| 226 |
{"ssp0 Rx", NULL, "AIF1 Capture"}, |
| 227 |
}; |
| 228 |
|
| 229 |
static const struct snd_soc_dapm_route bytcr_wm5102_ssp2_map[] = { |
| 230 |
{"AIF1 Playback", NULL, "ssp2 Tx"}, |
| 231 |
{"ssp2 Tx", NULL, "codec_out0"}, |
| 232 |
{"ssp2 Tx", NULL, "codec_out1"}, |
| 233 |
{"codec_in0", NULL, "ssp2 Rx"}, |
| 234 |
{"codec_in1", NULL, "ssp2 Rx"}, |
| 235 |
{"ssp2 Rx", NULL, "AIF1 Capture"}, |
| 236 |
}; |
| 237 |
|
| 238 |
static const struct snd_soc_dapm_route byt_wm5102_spk_spk_map[] = { |
| 239 |
{"Speaker", NULL, "SPKOUTLP"}, |
| 240 |
{"Speaker", NULL, "SPKOUTLN"}, |
| 241 |
{"Speaker", NULL, "SPKOUTRP"}, |
| 242 |
{"Speaker", NULL, "SPKOUTRN"}, |
| 243 |
}; |
| 244 |
|
| 245 |
static const struct snd_soc_dapm_route byt_wm5102_spk_hpout2_map[] = { |
| 246 |
{"Speaker", NULL, "HPOUT2L"}, |
| 247 |
{"Speaker", NULL, "HPOUT2R"}, |
| 248 |
}; |
| 249 |
|
| 250 |
static const struct snd_soc_dapm_route byt_wm5102_intmic_in3l_hsmic_in1l_map[] = { |
| 251 |
{"IN3L", NULL, "Internal Mic"}, |
| 252 |
{"IN1L", NULL, "Headset Mic"}, |
| 253 |
}; |
| 254 |
|
| 255 |
static const struct snd_soc_dapm_route byt_wm5102_intmic_in1l_hsmic_in2l_map[] = { |
| 256 |
{"IN1L", NULL, "Internal Mic"}, |
| 257 |
{"IN2L", NULL, "Headset Mic"}, |
| 258 |
}; |
| 259 |
|
| 260 |
static const struct snd_kcontrol_new byt_wm5102_controls[] = { |
| 261 |
SOC_DAPM_PIN_SWITCH("Headphone"), |
| 262 |
SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 263 |
SOC_DAPM_PIN_SWITCH("Internal Mic"), |
| 264 |
SOC_DAPM_PIN_SWITCH("Speaker"), |
| 265 |
SOC_DAPM_PIN_SWITCH("Line Out"), |
| 266 |
}; |
| 267 |
|
| 268 |
static struct snd_soc_jack_pin byt_wm5102_pins[] = { |
| 269 |
{ |
| 270 |
.pin = "Headphone", |
| 271 |
.mask = SND_JACK_HEADPHONE, |
| 272 |
}, |
| 273 |
{ |
| 274 |
.pin = "Headset Mic", |
| 275 |
.mask = SND_JACK_MICROPHONE, |
| 276 |
}, |
| 277 |
{ |
| 278 |
.pin = "Line Out", |
| 279 |
.mask = SND_JACK_LINEOUT, |
| 280 |
}, |
| 281 |
}; |
| 282 |
|
| 283 |
static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime) |
| 284 |
{ |
| 285 |
struct snd_soc_card *card = runtime->card; |
| 286 |
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card); |
| 287 |
struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card); |
| 288 |
struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component; |
| 289 |
const struct snd_soc_dapm_route *custom_map = NULL; |
| 290 |
int ret, jack_type, num_routes = 0; |
| 291 |
|
| 292 |
snd_soc_dapm_set_idle_bias(dapm, false); |
| 293 |
|
| 294 |
ret = snd_soc_add_card_controls(card, byt_wm5102_controls, |
| 295 |
ARRAY_SIZE(byt_wm5102_controls)); |
| 296 |
if (ret) { |
| 297 |
dev_err(card->dev, "Error adding card controls: %d\n", ret); |
| 298 |
return ret; |
| 299 |
} |
| 300 |
|
| 301 |
switch (quirk & BYT_WM5102_IN_MAP) { |
| 302 |
case BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L: |
| 303 |
custom_map = byt_wm5102_intmic_in3l_hsmic_in1l_map; |
| 304 |
num_routes = ARRAY_SIZE(byt_wm5102_intmic_in3l_hsmic_in1l_map); |
| 305 |
break; |
| 306 |
case BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L: |
| 307 |
custom_map = byt_wm5102_intmic_in1l_hsmic_in2l_map; |
| 308 |
num_routes = ARRAY_SIZE(byt_wm5102_intmic_in1l_hsmic_in2l_map); |
| 309 |
break; |
| 310 |
} |
| 311 |
ret = snd_soc_dapm_add_routes(dapm, custom_map, num_routes); |
| 312 |
if (ret) |
| 313 |
return ret; |
| 314 |
|
| 315 |
switch (quirk & BYT_WM5102_OUT_MAP) { |
| 316 |
case BYT_WM5102_SPK_SPK_MAP: |
| 317 |
custom_map = byt_wm5102_spk_spk_map; |
| 318 |
num_routes = ARRAY_SIZE(byt_wm5102_spk_spk_map); |
| 319 |
break; |
| 320 |
case BYT_WM5102_SPK_HPOUT2_MAP: |
| 321 |
custom_map = byt_wm5102_spk_hpout2_map; |
| 322 |
num_routes = ARRAY_SIZE(byt_wm5102_spk_hpout2_map); |
| 323 |
break; |
| 324 |
} |
| 325 |
ret = snd_soc_dapm_add_routes(dapm, custom_map, num_routes); |
| 326 |
if (ret) |
| 327 |
return ret; |
| 328 |
|
| 329 |
if (quirk & BYT_WM5102_SSP2) { |
| 330 |
custom_map = bytcr_wm5102_ssp2_map; |
| 331 |
num_routes = ARRAY_SIZE(bytcr_wm5102_ssp2_map); |
| 332 |
} else { |
| 333 |
custom_map = bytcr_wm5102_ssp0_map; |
| 334 |
num_routes = ARRAY_SIZE(bytcr_wm5102_ssp0_map); |
| 335 |
} |
| 336 |
ret = snd_soc_dapm_add_routes(dapm, custom_map, num_routes); |
| 337 |
if (ret) |
| 338 |
return ret; |
| 339 |
|
| 340 |
if (quirk & BYT_WM5102_MCLK_19_2MHZ) |
| 341 |
priv->mclk_freq = 19200000; |
| 342 |
else |
| 343 |
priv->mclk_freq = 25000000; |
| 344 |
|
| 345 |
/* |
| 346 |
* The firmware might enable the clock at boot (this information |
| 347 |
* may or may not be reflected in the enable clock register). |
| 348 |
* To change the rate we must disable the clock first to cover these |
| 349 |
* cases. Due to common clock framework restrictions that do not allow |
| 350 |
* to disable a clock that has not been enabled, we need to enable |
| 351 |
* the clock first. |
| 352 |
*/ |
| 353 |
ret = clk_prepare_enable(priv->mclk); |
| 354 |
if (!ret) |
| 355 |
clk_disable_unprepare(priv->mclk); |
| 356 |
|
| 357 |
ret = clk_set_rate(priv->mclk, priv->mclk_freq); |
| 358 |
if (ret) { |
| 359 |
dev_err(card->dev, "Error setting MCLK rate: %d\n", ret); |
| 360 |
return ret; |
| 361 |
} |
| 362 |
|
| 363 |
jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 364 |
SND_JACK_BTN_2 | SND_JACK_BTN_3; |
| 365 |
ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type, |
| 366 |
&priv->jack, byt_wm5102_pins, |
| 367 |
ARRAY_SIZE(byt_wm5102_pins)); |
| 368 |
if (ret) { |
| 369 |
dev_err(card->dev, "Error creating jack: %d\n", ret); |
| 370 |
return ret; |
| 371 |
} |
| 372 |
|
| 373 |
snd_soc_component_set_jack(component, &priv->jack, NULL); |
| 374 |
|
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd, |
| 379 |
struct snd_pcm_hw_params *params) |
| 380 |
{ |
| 381 |
struct snd_interval *rate = hw_param_interval(params, |
| 382 |
SNDRV_PCM_HW_PARAM_RATE); |
| 383 |
struct snd_interval *channels = hw_param_interval(params, |
| 384 |
SNDRV_PCM_HW_PARAM_CHANNELS); |
| 385 |
int ret, bits; |
| 386 |
|
| 387 |
/* The DSP will convert the FE rate to 48k, stereo */ |
| 388 |
rate->min = 48000; |
| 389 |
rate->max = 48000; |
| 390 |
channels->min = 2; |
| 391 |
channels->max = 2; |
| 392 |
|
| 393 |
if (quirk & BYT_WM5102_SSP2) { |
| 394 |
/* set SSP2 to 24-bit */ |
| 395 |
params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); |
| 396 |
bits = 24; |
| 397 |
} else { |
| 398 |
/* set SSP0 to 16-bit */ |
| 399 |
params_set_format(params, SNDRV_PCM_FORMAT_S16_LE); |
| 400 |
bits = 16; |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* Default mode for SSP configuration is TDM 4 slot, override config |
| 405 |
* with explicit setting to I2S 2ch 16-bit. The word length is set with |
| 406 |
* dai_set_tdm_slot() since there is no other API exposed |
| 407 |
*/ |
| 408 |
ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0), |
| 409 |
SND_SOC_DAIFMT_I2S | |
| 410 |
SND_SOC_DAIFMT_NB_NF | |
| 411 |
SND_SOC_DAIFMT_BP_FP); |
| 412 |
if (ret) { |
| 413 |
dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret); |
| 414 |
return ret; |
| 415 |
} |
| 416 |
|
| 417 |
ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits); |
| 418 |
if (ret) { |
| 419 |
dev_err(rtd->dev, "Error setting I2S config: %d\n", ret); |
| 420 |
return ret; |
| 421 |
} |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream) |
| 427 |
{ |
| 428 |
return snd_pcm_hw_constraint_single(substream->runtime, |
| 429 |
SNDRV_PCM_HW_PARAM_RATE, 48000); |
| 430 |
} |
| 431 |
|
| 432 |
static const struct snd_soc_ops byt_wm5102_aif1_ops = { |
| 433 |
.startup = byt_wm5102_aif1_startup, |
| 434 |
}; |
| 435 |
|
| 436 |
SND_SOC_DAILINK_DEF(dummy, |
| 437 |
DAILINK_COMP_ARRAY(COMP_DUMMY())); |
| 438 |
|
| 439 |
SND_SOC_DAILINK_DEF(media, |
| 440 |
DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai"))); |
| 441 |
|
| 442 |
SND_SOC_DAILINK_DEF(deepbuffer, |
| 443 |
DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai"))); |
| 444 |
|
| 445 |
SND_SOC_DAILINK_DEF(ssp0_port, |
| 446 |
DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port"))); |
| 447 |
|
| 448 |
SND_SOC_DAILINK_DEF(ssp0_codec, |
| 449 |
DAILINK_COMP_ARRAY(COMP_CODEC( |
| 450 |
/* |
| 451 |
* Note there is no need to overwrite the codec-name as is done in |
| 452 |
* other bytcr machine drivers, because the codec is a MFD child-dev. |
| 453 |
*/ |
| 454 |
"wm5102-codec", |
| 455 |
"wm5102-aif1"))); |
| 456 |
|
| 457 |
SND_SOC_DAILINK_DEF(platform, |
| 458 |
DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform"))); |
| 459 |
|
| 460 |
static struct snd_soc_dai_link byt_wm5102_dais[] = { |
| 461 |
[MERR_DPCM_AUDIO] = { |
| 462 |
.name = "Baytrail Audio Port", |
| 463 |
.stream_name = "Baytrail Audio", |
| 464 |
.nonatomic = true, |
| 465 |
.dynamic = 1, |
| 466 |
.ops = &byt_wm5102_aif1_ops, |
| 467 |
SND_SOC_DAILINK_REG(media, dummy, platform), |
| 468 |
|
| 469 |
}, |
| 470 |
[MERR_DPCM_DEEP_BUFFER] = { |
| 471 |
.name = "Deep-Buffer Audio Port", |
| 472 |
.stream_name = "Deep-Buffer Audio", |
| 473 |
.nonatomic = true, |
| 474 |
.dynamic = 1, |
| 475 |
.playback_only = 1, |
| 476 |
.ops = &byt_wm5102_aif1_ops, |
| 477 |
SND_SOC_DAILINK_REG(deepbuffer, dummy, platform), |
| 478 |
}, |
| 479 |
/* back ends */ |
| 480 |
{ |
| 481 |
/* |
| 482 |
* This dailink is updated dynamically to point to SSP0 or SSP2. |
| 483 |
* Yet its name is always kept as "SSP2-Codec" because the SOF |
| 484 |
* tplg files hardcode "SSP2-Codec" even in byt-foo-ssp0.tplg. |
| 485 |
*/ |
| 486 |
.name = "SSP2-Codec", |
| 487 |
.id = 0, |
| 488 |
.no_pcm = 1, |
| 489 |
.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
| 490 |
| SND_SOC_DAIFMT_CBC_CFC, |
| 491 |
.be_hw_params_fixup = byt_wm5102_codec_fixup, |
| 492 |
.init = byt_wm5102_init, |
| 493 |
SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform), |
| 494 |
}, |
| 495 |
}; |
| 496 |
|
| 497 |
/* use space before codec name to simplify card ID, and simplify driver name */ |
| 498 |
#define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */ |
| 499 |
#define SOF_DRIVER_NAME "SOF" |
| 500 |
|
| 501 |
#define CARD_NAME "bytcr-wm5102" |
| 502 |
#define DRIVER_NAME NULL /* card name will be used for driver name */ |
| 503 |
|
| 504 |
/* SoC card */ |
| 505 |
static struct snd_soc_card byt_wm5102_card = { |
| 506 |
.owner = THIS_MODULE, |
| 507 |
.dai_link = byt_wm5102_dais, |
| 508 |
.num_links = ARRAY_SIZE(byt_wm5102_dais), |
| 509 |
.dapm_widgets = byt_wm5102_widgets, |
| 510 |
.num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets), |
| 511 |
.dapm_routes = byt_wm5102_audio_map, |
| 512 |
.num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map), |
| 513 |
.fully_routed = true, |
| 514 |
}; |
| 515 |
|
| 516 |
static char byt_wm5102_components[64]; /* = "cfg-spk:* cfg-int-mic:* cfg-hs-mic:* ..." */ |
| 517 |
|
| 518 |
static int snd_byt_wm5102_mc_probe(struct platform_device *pdev) |
| 519 |
{ |
| 520 |
static const char * const out_map_name[] = { "spk", "hpout2" }; |
| 521 |
static const char * const intmic_map_name[] = { "in3l", "in1l" }; |
| 522 |
static const char * const hsmic_map_name[] = { "in1l", "in2l" }; |
| 523 |
char codec_name[SND_ACPI_I2C_ID_LEN]; |
| 524 |
struct device *dev = &pdev->dev; |
| 525 |
struct byt_wm5102_private *priv; |
| 526 |
struct snd_soc_acpi_mach *mach; |
| 527 |
const char *platform_name; |
| 528 |
struct acpi_device *adev; |
| 529 |
struct device *codec_dev; |
| 530 |
int dai_index = 0; |
| 531 |
bool sof_parent; |
| 532 |
int i, ret; |
| 533 |
|
| 534 |
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 535 |
if (!priv) |
| 536 |
return -ENOMEM; |
| 537 |
|
| 538 |
/* Get MCLK */ |
| 539 |
priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3"); |
| 540 |
if (IS_ERR(priv->mclk)) |
| 541 |
return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n"); |
| 542 |
|
| 543 |
/* |
| 544 |
* Get speaker VDD enable GPIO: |
| 545 |
* 1. Get codec-device-name |
| 546 |
* 2. Get codec-device |
| 547 |
* 3. Get GPIO from codec-device |
| 548 |
*/ |
| 549 |
mach = dev->platform_data; |
| 550 |
adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1); |
| 551 |
if (adev) { |
| 552 |
snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev)); |
| 553 |
acpi_dev_put(adev); |
| 554 |
} else { |
| 555 |
/* Special case for when the codec is missing from the DSTD */ |
| 556 |
strscpy(codec_name, "spi-wm5102", sizeof(codec_name)); |
| 557 |
} |
| 558 |
|
| 559 |
codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name); |
| 560 |
if (!codec_dev) |
| 561 |
return -EPROBE_DEFER; |
| 562 |
|
| 563 |
/* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */ |
| 564 |
priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW); |
| 565 |
put_device(codec_dev); |
| 566 |
|
| 567 |
if (IS_ERR(priv->spkvdd_en_gpio)) { |
| 568 |
ret = PTR_ERR(priv->spkvdd_en_gpio); |
| 569 |
/* |
| 570 |
* The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c, |
| 571 |
* so -ENOENT means that arizona-spi hasn't probed yet. |
| 572 |
*/ |
| 573 |
if (ret == -ENOENT) |
| 574 |
ret = -EPROBE_DEFER; |
| 575 |
|
| 576 |
return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n"); |
| 577 |
} |
| 578 |
|
| 579 |
if (soc_intel_is_cht()) { |
| 580 |
/* |
| 581 |
* CHT always uses SSP2 and 19.2 MHz; and |
| 582 |
* the one currently supported CHT design uses HPOUT2 as |
| 583 |
* speaker output and has the intmic on IN1L + hsmic on IN2L. |
| 584 |
*/ |
| 585 |
quirk = BYT_WM5102_SSP2 | BYT_WM5102_MCLK_19_2MHZ | |
| 586 |
BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L | |
| 587 |
BYT_WM5102_SPK_HPOUT2_MAP; |
| 588 |
} |
| 589 |
if (quirk_override != -1) { |
| 590 |
dev_info_once(dev, "Overriding quirk 0x%lx => 0x%x\n", |
| 591 |
quirk, quirk_override); |
| 592 |
quirk = quirk_override; |
| 593 |
} |
| 594 |
log_quirks(dev); |
| 595 |
|
| 596 |
snprintf(byt_wm5102_components, sizeof(byt_wm5102_components), |
| 597 |
"cfg-spk:%s cfg-intmic:%s cfg-hsmic:%s", |
| 598 |
out_map_name[FIELD_GET(BYT_WM5102_OUT_MAP, quirk)], |
| 599 |
intmic_map_name[FIELD_GET(BYT_WM5102_IN_MAP, quirk)], |
| 600 |
hsmic_map_name[FIELD_GET(BYT_WM5102_IN_MAP, quirk)]); |
| 601 |
byt_wm5102_card.components = byt_wm5102_components; |
| 602 |
|
| 603 |
/* find index of codec dai */ |
| 604 |
for (i = 0; i < ARRAY_SIZE(byt_wm5102_dais); i++) { |
| 605 |
if (byt_wm5102_dais[i].num_codecs && |
| 606 |
!strcmp(byt_wm5102_dais[i].codecs->name, |
| 607 |
"wm5102-codec")) { |
| 608 |
dai_index = i; |
| 609 |
break; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/* override platform name, if required */ |
| 614 |
byt_wm5102_card.dev = dev; |
| 615 |
platform_name = mach->mach_params.platform; |
| 616 |
ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name); |
| 617 |
if (ret) |
| 618 |
goto out_put_gpio; |
| 619 |
|
| 620 |
/* override SSP port, if required */ |
| 621 |
if (quirk & BYT_WM5102_SSP2) |
| 622 |
byt_wm5102_dais[dai_index].cpus->dai_name = "ssp2-port"; |
| 623 |
|
| 624 |
/* set card and driver name and pm-ops */ |
| 625 |
sof_parent = snd_soc_acpi_sof_parent(dev); |
| 626 |
if (sof_parent) { |
| 627 |
byt_wm5102_card.name = SOF_CARD_NAME; |
| 628 |
byt_wm5102_card.driver_name = SOF_DRIVER_NAME; |
| 629 |
dev->driver->pm = &snd_soc_pm_ops; |
| 630 |
} else { |
| 631 |
byt_wm5102_card.name = CARD_NAME; |
| 632 |
byt_wm5102_card.driver_name = DRIVER_NAME; |
| 633 |
} |
| 634 |
|
| 635 |
snd_soc_card_set_drvdata(&byt_wm5102_card, priv); |
| 636 |
ret = devm_snd_soc_register_card(dev, &byt_wm5102_card); |
| 637 |
if (ret) { |
| 638 |
dev_err_probe(dev, ret, "registering card\n"); |
| 639 |
goto out_put_gpio; |
| 640 |
} |
| 641 |
|
| 642 |
platform_set_drvdata(pdev, &byt_wm5102_card); |
| 643 |
return 0; |
| 644 |
|
| 645 |
out_put_gpio: |
| 646 |
gpiod_put(priv->spkvdd_en_gpio); |
| 647 |
return ret; |
| 648 |
} |
| 649 |
|
| 650 |
static void snd_byt_wm5102_mc_remove(struct platform_device *pdev) |
| 651 |
{ |
| 652 |
struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 653 |
struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card); |
| 654 |
|
| 655 |
gpiod_put(priv->spkvdd_en_gpio); |
| 656 |
} |
| 657 |
|
| 658 |
static struct platform_driver snd_byt_wm5102_mc_driver = { |
| 659 |
.driver = { |
| 660 |
.name = "bytcr_wm5102", |
| 661 |
}, |
| 662 |
.probe = snd_byt_wm5102_mc_probe, |
| 663 |
.remove = snd_byt_wm5102_mc_remove, |
| 664 |
}; |
| 665 |
|
| 666 |
module_platform_driver(snd_byt_wm5102_mc_driver); |
| 667 |
|
| 668 |
MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver"); |
| 669 |
MODULE_AUTHOR("Hans de Goede <[email protected]>"); |
| 670 |
MODULE_LICENSE("GPL v2"); |
| 671 |
MODULE_ALIAS("platform:bytcr_wm5102"); |