1 // SPDX-License-Identifier: GPL-2.0-only
3 * cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
4 * platforms Cherrytrail and Braswell, with max98090 & TI codec.
6 * Copyright (C) 2015 Intel Corp
7 * Author: Fang, Yang A <yang.a.fang@intel.com>
8 * This file is modified from cht_bsw_rt5645.c
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 #include <linux/dmi.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/clk.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-acpi.h>
24 #include <sound/jack.h>
25 #include "../../codecs/max98090.h"
26 #include "../atom/sst-atom-controls.h"
27 #include "../../codecs/ts3a227e.h"
29 #define CHT_PLAT_CLK_3_HZ 19200000
30 #define CHT_CODEC_DAI "HiFi"
32 #define QUIRK_PMC_PLT_CLK_0 0x01
34 struct cht_mc_private {
36 struct snd_soc_jack jack;
37 bool ts3a227e_present;
41 static int platform_clock_control(struct snd_soc_dapm_widget *w,
42 struct snd_kcontrol *k, int event)
44 struct snd_soc_dapm_context *dapm = w->dapm;
45 struct snd_soc_card *card = dapm->card;
46 struct snd_soc_dai *codec_dai;
47 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
50 /* See the comment in snd_cht_mc_probe() */
51 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
54 codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
56 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
60 if (SND_SOC_DAPM_EVENT_ON(event)) {
61 ret = clk_prepare_enable(ctx->mclk);
64 "could not configure MCLK state");
68 clk_disable_unprepare(ctx->mclk);
74 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
75 SND_SOC_DAPM_HP("Headphone", NULL),
76 SND_SOC_DAPM_MIC("Headset Mic", NULL),
77 SND_SOC_DAPM_MIC("Int Mic", NULL),
78 SND_SOC_DAPM_SPK("Ext Spk", NULL),
79 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
80 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
81 SND_SOC_DAPM_POST_PMD),
84 static const struct snd_soc_dapm_route cht_audio_map[] = {
85 {"IN34", NULL, "Headset Mic"},
86 {"Headset Mic", NULL, "MICBIAS"},
87 {"DMICL", NULL, "Int Mic"},
88 {"Headphone", NULL, "HPL"},
89 {"Headphone", NULL, "HPR"},
90 {"Ext Spk", NULL, "SPKL"},
91 {"Ext Spk", NULL, "SPKR"},
92 {"HiFi Playback", NULL, "ssp2 Tx"},
93 {"ssp2 Tx", NULL, "codec_out0"},
94 {"ssp2 Tx", NULL, "codec_out1"},
95 {"codec_in0", NULL, "ssp2 Rx" },
96 {"codec_in1", NULL, "ssp2 Rx" },
97 {"ssp2 Rx", NULL, "HiFi Capture"},
98 {"Headphone", NULL, "Platform Clock"},
99 {"Headset Mic", NULL, "Platform Clock"},
100 {"Int Mic", NULL, "Platform Clock"},
101 {"Ext Spk", NULL, "Platform Clock"},
104 static const struct snd_kcontrol_new cht_mc_controls[] = {
105 SOC_DAPM_PIN_SWITCH("Headphone"),
106 SOC_DAPM_PIN_SWITCH("Headset Mic"),
107 SOC_DAPM_PIN_SWITCH("Int Mic"),
108 SOC_DAPM_PIN_SWITCH("Ext Spk"),
111 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
112 struct snd_pcm_hw_params *params)
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct snd_soc_dai *codec_dai = rtd->codec_dai;
118 ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
119 CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
121 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
128 static int cht_ti_jack_event(struct notifier_block *nb,
129 unsigned long event, void *data)
131 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
132 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
134 if (event & SND_JACK_MICROPHONE) {
135 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
136 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
137 snd_soc_dapm_sync(dapm);
139 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
140 snd_soc_dapm_disable_pin(dapm, "SHDN");
141 snd_soc_dapm_sync(dapm);
147 static struct notifier_block cht_jack_nb = {
148 .notifier_call = cht_ti_jack_event,
151 static struct snd_soc_jack_pin hs_jack_pins[] = {
154 .mask = SND_JACK_HEADPHONE,
157 .pin = "Headset Mic",
158 .mask = SND_JACK_MICROPHONE,
162 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
165 .report = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
166 .debounce_time = 200,
171 .report = SND_JACK_MICROPHONE,
172 .debounce_time = 200,
176 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
177 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
179 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
180 { "hp-gpios", &hp_gpios, 1 },
181 { "mic-gpios", &mic_gpios, 1 },
185 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
189 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
190 struct snd_soc_jack *jack = &ctx->jack;
192 if (ctx->ts3a227e_present) {
194 * The jack has already been created in the
195 * cht_max98090_headset_init() function.
197 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
201 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
203 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
205 hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
207 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
211 ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
212 ARRAY_SIZE(hs_jack_gpios),
216 * flag error but don't bail if jack detect is broken
217 * due to platform issues or bad BIOS/configuration
219 dev_err(runtime->dev,
220 "jack detection gpios not added, error %d\n", ret);
223 /* See the comment in snd_cht_mc_probe() */
224 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
228 * The firmware might enable the clock at
229 * boot (this information may or may not
230 * be reflected in the enable clock register).
231 * To change the rate we must disable the clock
232 * first to cover these cases. Due to common
233 * clock framework restrictions that do not allow
234 * to disable a clock that has not been enabled,
235 * we need to enable the clock first.
237 ret = clk_prepare_enable(ctx->mclk);
239 clk_disable_unprepare(ctx->mclk);
241 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
244 dev_err(runtime->dev, "unable to set MCLK rate\n");
249 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
250 struct snd_pcm_hw_params *params)
252 struct snd_interval *rate = hw_param_interval(params,
253 SNDRV_PCM_HW_PARAM_RATE);
254 struct snd_interval *channels = hw_param_interval(params,
255 SNDRV_PCM_HW_PARAM_CHANNELS);
257 unsigned int fmt = 0;
259 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
261 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
265 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
266 | SND_SOC_DAIFMT_CBS_CFS;
268 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
270 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
274 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
275 rate->min = rate->max = 48000;
276 channels->min = channels->max = 2;
278 /* set SSP2 to 16-bit */
279 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
283 static int cht_aif1_startup(struct snd_pcm_substream *substream)
285 return snd_pcm_hw_constraint_single(substream->runtime,
286 SNDRV_PCM_HW_PARAM_RATE, 48000);
289 static int cht_max98090_headset_init(struct snd_soc_component *component)
291 struct snd_soc_card *card = component->card;
292 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
293 struct snd_soc_jack *jack = &ctx->jack;
298 * TI supports 4 butons headset detection
304 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
305 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
306 SND_JACK_BTN_2 | SND_JACK_BTN_3;
308 ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
311 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
315 return ts3a227e_enable_jack_detect(component, jack);
318 static const struct snd_soc_ops cht_aif1_ops = {
319 .startup = cht_aif1_startup,
322 static const struct snd_soc_ops cht_be_ssp2_ops = {
323 .hw_params = cht_aif1_hw_params,
326 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
327 .name = "Headset Chip",
328 .init = cht_max98090_headset_init,
329 .codec_name = "i2c-104C227E:00",
332 SND_SOC_DAILINK_DEF(dummy,
333 DAILINK_COMP_ARRAY(COMP_DUMMY()));
335 SND_SOC_DAILINK_DEF(media,
336 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
338 SND_SOC_DAILINK_DEF(deepbuffer,
339 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
341 SND_SOC_DAILINK_DEF(ssp2_port,
342 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
343 SND_SOC_DAILINK_DEF(ssp2_codec,
344 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-193C9890:00", "HiFi")));
346 SND_SOC_DAILINK_DEF(platform,
347 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
349 static struct snd_soc_dai_link cht_dailink[] = {
350 [MERR_DPCM_AUDIO] = {
351 .name = "Audio Port",
352 .stream_name = "Audio",
357 .ops = &cht_aif1_ops,
358 SND_SOC_DAILINK_REG(media, dummy, platform),
360 [MERR_DPCM_DEEP_BUFFER] = {
361 .name = "Deep-Buffer Audio Port",
362 .stream_name = "Deep-Buffer Audio",
366 .ops = &cht_aif1_ops,
367 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
371 .name = "SSP2-Codec",
374 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
375 | SND_SOC_DAIFMT_CBS_CFS,
376 .init = cht_codec_init,
377 .be_hw_params_fixup = cht_codec_fixup,
380 .ops = &cht_be_ssp2_ops,
381 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
386 static struct snd_soc_card snd_soc_card_cht = {
387 .name = "chtmax98090",
388 .owner = THIS_MODULE,
389 .dai_link = cht_dailink,
390 .num_links = ARRAY_SIZE(cht_dailink),
391 .aux_dev = &cht_max98090_headset_dev,
393 .dapm_widgets = cht_dapm_widgets,
394 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
395 .dapm_routes = cht_audio_map,
396 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
397 .controls = cht_mc_controls,
398 .num_controls = ARRAY_SIZE(cht_mc_controls),
401 static const struct dmi_system_id cht_max98090_quirk_table[] = {
403 /* Clapper model Chromebook */
405 DMI_MATCH(DMI_PRODUCT_NAME, "Clapper"),
407 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
410 /* Gnawty model Chromebook (Acer Chromebook CB3-111) */
412 DMI_MATCH(DMI_PRODUCT_NAME, "Gnawty"),
414 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
417 /* Swanky model Chromebook (Toshiba Chromebook 2) */
419 DMI_MATCH(DMI_PRODUCT_NAME, "Swanky"),
421 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
426 static int snd_cht_mc_probe(struct platform_device *pdev)
428 const struct dmi_system_id *dmi_id;
429 struct device *dev = &pdev->dev;
431 struct cht_mc_private *drv;
432 const char *mclk_name;
433 struct snd_soc_acpi_mach *mach;
434 const char *platform_name;
436 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
440 dmi_id = dmi_first_match(cht_max98090_quirk_table);
442 drv->quirks = (unsigned long)dmi_id->driver_data;
444 drv->ts3a227e_present = acpi_dev_found("104C227E");
445 if (!drv->ts3a227e_present) {
446 /* no need probe TI jack detection chip */
447 snd_soc_card_cht.aux_dev = NULL;
448 snd_soc_card_cht.num_aux_devs = 0;
450 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
451 acpi_max98090_gpios);
453 dev_dbg(dev, "Unable to add GPIO mapping table\n");
456 /* override plaform name, if required */
457 snd_soc_card_cht.dev = &pdev->dev;
458 mach = (&pdev->dev)->platform_data;
459 platform_name = mach->mach_params.platform;
461 ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht,
466 /* register the soc card */
467 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
469 if (drv->quirks & QUIRK_PMC_PLT_CLK_0)
470 mclk_name = "pmc_plt_clk_0";
472 mclk_name = "pmc_plt_clk_3";
474 drv->mclk = devm_clk_get(&pdev->dev, mclk_name);
475 if (IS_ERR(drv->mclk)) {
477 "Failed to get MCLK from %s: %ld\n",
478 mclk_name, PTR_ERR(drv->mclk));
479 return PTR_ERR(drv->mclk);
483 * Boards which have the MAX98090's clk connected to clk_0 do not seem
484 * to like it if we muck with the clock. If we disable the clock when
485 * it is unused we get "max98090 i2c-193C9890:00: PLL unlocked" errors
486 * and the PLL never seems to lock again.
487 * So for these boards we enable it here once and leave it at that.
489 if (drv->quirks & QUIRK_PMC_PLT_CLK_0) {
490 ret_val = clk_prepare_enable(drv->mclk);
492 dev_err(&pdev->dev, "MCLK enable error: %d\n", ret_val);
497 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
500 "snd_soc_register_card failed %d\n", ret_val);
503 platform_set_drvdata(pdev, &snd_soc_card_cht);
507 static int snd_cht_mc_remove(struct platform_device *pdev)
509 struct snd_soc_card *card = platform_get_drvdata(pdev);
510 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
512 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
513 clk_disable_unprepare(ctx->mclk);
518 static struct platform_driver snd_cht_mc_driver = {
520 .name = "cht-bsw-max98090",
522 .probe = snd_cht_mc_probe,
523 .remove = snd_cht_mc_remove,
526 module_platform_driver(snd_cht_mc_driver)
528 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
529 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
530 MODULE_LICENSE("GPL v2");
531 MODULE_ALIAS("platform:cht-bsw-max98090");