2 * cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
3 * platforms Cherrytrail and Braswell, with max98090 & TI codec.
5 * Copyright (C) 2015 Intel Corp
6 * Author: Fang, Yang A <yang.a.fang@intel.com>
7 * This file is modified from cht_bsw_rt5645.c
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/dmi.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/acpi.h>
27 #include <linux/clk.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
32 #include "../../codecs/max98090.h"
33 #include "../atom/sst-atom-controls.h"
34 #include "../../codecs/ts3a227e.h"
36 #define CHT_PLAT_CLK_3_HZ 19200000
37 #define CHT_CODEC_DAI "HiFi"
39 #define QUIRK_PMC_PLT_CLK_0 0x01
41 struct cht_mc_private {
43 struct snd_soc_jack jack;
44 bool ts3a227e_present;
48 static int platform_clock_control(struct snd_soc_dapm_widget *w,
49 struct snd_kcontrol *k, int event)
51 struct snd_soc_dapm_context *dapm = w->dapm;
52 struct snd_soc_card *card = dapm->card;
53 struct snd_soc_dai *codec_dai;
54 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
57 /* See the comment in snd_cht_mc_probe() */
58 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
61 codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
63 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
67 if (SND_SOC_DAPM_EVENT_ON(event)) {
68 ret = clk_prepare_enable(ctx->mclk);
71 "could not configure MCLK state");
75 clk_disable_unprepare(ctx->mclk);
81 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
82 SND_SOC_DAPM_HP("Headphone", NULL),
83 SND_SOC_DAPM_MIC("Headset Mic", NULL),
84 SND_SOC_DAPM_MIC("Int Mic", NULL),
85 SND_SOC_DAPM_SPK("Ext Spk", NULL),
86 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
87 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
88 SND_SOC_DAPM_POST_PMD),
91 static const struct snd_soc_dapm_route cht_audio_map[] = {
92 {"IN34", NULL, "Headset Mic"},
93 {"Headset Mic", NULL, "MICBIAS"},
94 {"DMICL", NULL, "Int Mic"},
95 {"Headphone", NULL, "HPL"},
96 {"Headphone", NULL, "HPR"},
97 {"Ext Spk", NULL, "SPKL"},
98 {"Ext Spk", NULL, "SPKR"},
99 {"HiFi Playback", NULL, "ssp2 Tx"},
100 {"ssp2 Tx", NULL, "codec_out0"},
101 {"ssp2 Tx", NULL, "codec_out1"},
102 {"codec_in0", NULL, "ssp2 Rx" },
103 {"codec_in1", NULL, "ssp2 Rx" },
104 {"ssp2 Rx", NULL, "HiFi Capture"},
105 {"Headphone", NULL, "Platform Clock"},
106 {"Headset Mic", NULL, "Platform Clock"},
107 {"Int Mic", NULL, "Platform Clock"},
108 {"Ext Spk", NULL, "Platform Clock"},
111 static const struct snd_kcontrol_new cht_mc_controls[] = {
112 SOC_DAPM_PIN_SWITCH("Headphone"),
113 SOC_DAPM_PIN_SWITCH("Headset Mic"),
114 SOC_DAPM_PIN_SWITCH("Int Mic"),
115 SOC_DAPM_PIN_SWITCH("Ext Spk"),
118 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
119 struct snd_pcm_hw_params *params)
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
122 struct snd_soc_dai *codec_dai = rtd->codec_dai;
125 ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
126 CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
128 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
135 static int cht_ti_jack_event(struct notifier_block *nb,
136 unsigned long event, void *data)
138 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
139 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
141 if (event & SND_JACK_MICROPHONE) {
142 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
143 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
144 snd_soc_dapm_sync(dapm);
146 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
147 snd_soc_dapm_disable_pin(dapm, "SHDN");
148 snd_soc_dapm_sync(dapm);
154 static struct notifier_block cht_jack_nb = {
155 .notifier_call = cht_ti_jack_event,
158 static struct snd_soc_jack_pin hs_jack_pins[] = {
161 .mask = SND_JACK_HEADPHONE,
164 .pin = "Headset Mic",
165 .mask = SND_JACK_MICROPHONE,
169 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
172 .report = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
173 .debounce_time = 200,
178 .report = SND_JACK_MICROPHONE,
179 .debounce_time = 200,
183 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
184 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
186 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
187 { "hp-gpios", &hp_gpios, 1 },
188 { "mic-gpios", &mic_gpios, 1 },
192 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
196 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
197 struct snd_soc_jack *jack = &ctx->jack;
199 if (ctx->ts3a227e_present) {
201 * The jack has already been created in the
202 * cht_max98090_headset_init() function.
204 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
208 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
210 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
212 hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
214 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
218 ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
219 ARRAY_SIZE(hs_jack_gpios),
223 * flag error but don't bail if jack detect is broken
224 * due to platform issues or bad BIOS/configuration
226 dev_err(runtime->dev,
227 "jack detection gpios not added, error %d\n", ret);
230 /* See the comment in snd_cht_mc_probe() */
231 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
235 * The firmware might enable the clock at
236 * boot (this information may or may not
237 * be reflected in the enable clock register).
238 * To change the rate we must disable the clock
239 * first to cover these cases. Due to common
240 * clock framework restrictions that do not allow
241 * to disable a clock that has not been enabled,
242 * we need to enable the clock first.
244 ret = clk_prepare_enable(ctx->mclk);
246 clk_disable_unprepare(ctx->mclk);
248 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
251 dev_err(runtime->dev, "unable to set MCLK rate\n");
256 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
257 struct snd_pcm_hw_params *params)
259 struct snd_interval *rate = hw_param_interval(params,
260 SNDRV_PCM_HW_PARAM_RATE);
261 struct snd_interval *channels = hw_param_interval(params,
262 SNDRV_PCM_HW_PARAM_CHANNELS);
264 unsigned int fmt = 0;
266 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
268 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
272 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
273 | SND_SOC_DAIFMT_CBS_CFS;
275 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
277 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
281 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
282 rate->min = rate->max = 48000;
283 channels->min = channels->max = 2;
285 /* set SSP2 to 16-bit */
286 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
290 static int cht_aif1_startup(struct snd_pcm_substream *substream)
292 return snd_pcm_hw_constraint_single(substream->runtime,
293 SNDRV_PCM_HW_PARAM_RATE, 48000);
296 static int cht_max98090_headset_init(struct snd_soc_component *component)
298 struct snd_soc_card *card = component->card;
299 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
300 struct snd_soc_jack *jack = &ctx->jack;
305 * TI supports 4 butons headset detection
311 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
312 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
313 SND_JACK_BTN_2 | SND_JACK_BTN_3;
315 ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
318 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
322 return ts3a227e_enable_jack_detect(component, jack);
325 static const struct snd_soc_ops cht_aif1_ops = {
326 .startup = cht_aif1_startup,
329 static const struct snd_soc_ops cht_be_ssp2_ops = {
330 .hw_params = cht_aif1_hw_params,
333 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
334 .name = "Headset Chip",
335 .init = cht_max98090_headset_init,
336 .codec_name = "i2c-104C227E:00",
339 static struct snd_soc_dai_link cht_dailink[] = {
340 [MERR_DPCM_AUDIO] = {
341 .name = "Audio Port",
342 .stream_name = "Audio",
343 .cpu_dai_name = "media-cpu-dai",
344 .codec_dai_name = "snd-soc-dummy-dai",
345 .codec_name = "snd-soc-dummy",
346 .platform_name = "sst-mfld-platform",
351 .ops = &cht_aif1_ops,
353 [MERR_DPCM_DEEP_BUFFER] = {
354 .name = "Deep-Buffer Audio Port",
355 .stream_name = "Deep-Buffer Audio",
356 .cpu_dai_name = "deepbuffer-cpu-dai",
357 .codec_dai_name = "snd-soc-dummy-dai",
358 .codec_name = "snd-soc-dummy",
359 .platform_name = "sst-mfld-platform",
363 .ops = &cht_aif1_ops,
367 .name = "SSP2-Codec",
369 .cpu_dai_name = "ssp2-port",
370 .platform_name = "sst-mfld-platform",
372 .codec_dai_name = "HiFi",
373 .codec_name = "i2c-193C9890:00",
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,
385 static struct snd_soc_card snd_soc_card_cht = {
386 .name = "chtmax98090",
387 .owner = THIS_MODULE,
388 .dai_link = cht_dailink,
389 .num_links = ARRAY_SIZE(cht_dailink),
390 .aux_dev = &cht_max98090_headset_dev,
392 .dapm_widgets = cht_dapm_widgets,
393 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
394 .dapm_routes = cht_audio_map,
395 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
396 .controls = cht_mc_controls,
397 .num_controls = ARRAY_SIZE(cht_mc_controls),
400 static const struct dmi_system_id cht_max98090_quirk_table[] = {
402 /* Clapper model Chromebook */
404 DMI_MATCH(DMI_PRODUCT_NAME, "Clapper"),
406 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
409 /* Gnawty model Chromebook (Acer Chromebook CB3-111) */
411 DMI_MATCH(DMI_PRODUCT_NAME, "Gnawty"),
413 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
416 /* Swanky model Chromebook (Toshiba Chromebook 2) */
418 DMI_MATCH(DMI_PRODUCT_NAME, "Swanky"),
420 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
425 static int snd_cht_mc_probe(struct platform_device *pdev)
427 const struct dmi_system_id *dmi_id;
428 struct device *dev = &pdev->dev;
430 struct cht_mc_private *drv;
431 const char *mclk_name;
433 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
437 dmi_id = dmi_first_match(cht_max98090_quirk_table);
439 drv->quirks = (unsigned long)dmi_id->driver_data;
441 drv->ts3a227e_present = acpi_dev_found("104C227E");
442 if (!drv->ts3a227e_present) {
443 /* no need probe TI jack detection chip */
444 snd_soc_card_cht.aux_dev = NULL;
445 snd_soc_card_cht.num_aux_devs = 0;
447 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
448 acpi_max98090_gpios);
450 dev_dbg(dev, "Unable to add GPIO mapping table\n");
453 /* register the soc card */
454 snd_soc_card_cht.dev = &pdev->dev;
455 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
457 if (drv->quirks & QUIRK_PMC_PLT_CLK_0)
458 mclk_name = "pmc_plt_clk_0";
460 mclk_name = "pmc_plt_clk_3";
462 drv->mclk = devm_clk_get(&pdev->dev, mclk_name);
463 if (IS_ERR(drv->mclk)) {
465 "Failed to get MCLK from %s: %ld\n",
466 mclk_name, PTR_ERR(drv->mclk));
467 return PTR_ERR(drv->mclk);
471 * Boards which have the MAX98090's clk connected to clk_0 do not seem
472 * to like it if we muck with the clock. If we disable the clock when
473 * it is unused we get "max98090 i2c-193C9890:00: PLL unlocked" errors
474 * and the PLL never seems to lock again.
475 * So for these boards we enable it here once and leave it at that.
477 if (drv->quirks & QUIRK_PMC_PLT_CLK_0) {
478 ret_val = clk_prepare_enable(drv->mclk);
480 dev_err(&pdev->dev, "MCLK enable error: %d\n", ret_val);
485 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
488 "snd_soc_register_card failed %d\n", ret_val);
491 platform_set_drvdata(pdev, &snd_soc_card_cht);
495 static int snd_cht_mc_remove(struct platform_device *pdev)
497 struct snd_soc_card *card = platform_get_drvdata(pdev);
498 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
500 if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
501 clk_disable_unprepare(ctx->mclk);
506 static struct platform_driver snd_cht_mc_driver = {
508 .name = "cht-bsw-max98090",
510 .probe = snd_cht_mc_probe,
511 .remove = snd_cht_mc_remove,
514 module_platform_driver(snd_cht_mc_driver)
516 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
517 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
518 MODULE_LICENSE("GPL v2");
519 MODULE_ALIAS("platform:cht-bsw-max98090");