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
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;
47 static int platform_clock_control(struct snd_soc_dapm_widget *w,
48 struct snd_kcontrol *k, int event)
50 struct snd_soc_dapm_context *dapm = w->dapm;
51 struct snd_soc_card *card = dapm->card;
52 struct snd_soc_dai *codec_dai;
53 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
56 codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
58 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
62 if (SND_SOC_DAPM_EVENT_ON(event)) {
63 ret = clk_prepare_enable(ctx->mclk);
66 "could not configure MCLK state");
70 clk_disable_unprepare(ctx->mclk);
76 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
77 SND_SOC_DAPM_HP("Headphone", NULL),
78 SND_SOC_DAPM_MIC("Headset Mic", NULL),
79 SND_SOC_DAPM_MIC("Int Mic", NULL),
80 SND_SOC_DAPM_SPK("Ext Spk", NULL),
81 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
82 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
83 SND_SOC_DAPM_POST_PMD),
86 static const struct snd_soc_dapm_route cht_audio_map[] = {
87 {"IN34", NULL, "Headset Mic"},
88 {"Headset Mic", NULL, "MICBIAS"},
89 {"DMICL", NULL, "Int Mic"},
90 {"Headphone", NULL, "HPL"},
91 {"Headphone", NULL, "HPR"},
92 {"Ext Spk", NULL, "SPKL"},
93 {"Ext Spk", NULL, "SPKR"},
94 {"HiFi Playback", NULL, "ssp2 Tx"},
95 {"ssp2 Tx", NULL, "codec_out0"},
96 {"ssp2 Tx", NULL, "codec_out1"},
97 {"codec_in0", NULL, "ssp2 Rx" },
98 {"codec_in1", NULL, "ssp2 Rx" },
99 {"ssp2 Rx", NULL, "HiFi Capture"},
100 {"Headphone", NULL, "Platform Clock"},
101 {"Headset Mic", NULL, "Platform Clock"},
102 {"Int Mic", NULL, "Platform Clock"},
103 {"Ext Spk", NULL, "Platform Clock"},
106 static const struct snd_kcontrol_new cht_mc_controls[] = {
107 SOC_DAPM_PIN_SWITCH("Headphone"),
108 SOC_DAPM_PIN_SWITCH("Headset Mic"),
109 SOC_DAPM_PIN_SWITCH("Int Mic"),
110 SOC_DAPM_PIN_SWITCH("Ext Spk"),
113 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
116 struct snd_soc_pcm_runtime *rtd = substream->private_data;
117 struct snd_soc_dai *codec_dai = rtd->codec_dai;
120 ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
121 CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
123 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
130 static int cht_ti_jack_event(struct notifier_block *nb,
131 unsigned long event, void *data)
133 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
134 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
136 if (event & SND_JACK_MICROPHONE) {
137 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
138 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
139 snd_soc_dapm_sync(dapm);
141 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
142 snd_soc_dapm_disable_pin(dapm, "SHDN");
143 snd_soc_dapm_sync(dapm);
149 static struct notifier_block cht_jack_nb = {
150 .notifier_call = cht_ti_jack_event,
153 static struct snd_soc_jack_pin hs_jack_pins[] = {
156 .mask = SND_JACK_HEADPHONE,
159 .pin = "Headset Mic",
160 .mask = SND_JACK_MICROPHONE,
164 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
167 .report = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
168 .debounce_time = 200,
173 .report = SND_JACK_MICROPHONE,
174 .debounce_time = 200,
178 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
179 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
181 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
182 { "hp-gpios", &hp_gpios, 1 },
183 { "mic-gpios", &mic_gpios, 1 },
187 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
191 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
192 struct snd_soc_jack *jack = &ctx->jack;
194 if (ctx->ts3a227e_present) {
196 * The jack has already been created in the
197 * cht_max98090_headset_init() function.
199 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
203 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
205 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
207 hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
209 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
213 ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
214 ARRAY_SIZE(hs_jack_gpios),
218 * flag error but don't bail if jack detect is broken
219 * due to platform issues or bad BIOS/configuration
221 dev_err(runtime->dev,
222 "jack detection gpios not added, error %d\n", ret);
226 * The firmware might enable the clock at
227 * boot (this information may or may not
228 * be reflected in the enable clock register).
229 * To change the rate we must disable the clock
230 * first to cover these cases. Due to common
231 * clock framework restrictions that do not allow
232 * to disable a clock that has not been enabled,
233 * we need to enable the clock first.
235 ret = clk_prepare_enable(ctx->mclk);
237 clk_disable_unprepare(ctx->mclk);
239 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
242 dev_err(runtime->dev, "unable to set MCLK rate\n");
247 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
248 struct snd_pcm_hw_params *params)
250 struct snd_interval *rate = hw_param_interval(params,
251 SNDRV_PCM_HW_PARAM_RATE);
252 struct snd_interval *channels = hw_param_interval(params,
253 SNDRV_PCM_HW_PARAM_CHANNELS);
255 unsigned int fmt = 0;
257 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
259 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
263 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
264 | SND_SOC_DAIFMT_CBS_CFS;
266 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
268 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
272 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
273 rate->min = rate->max = 48000;
274 channels->min = channels->max = 2;
276 /* set SSP2 to 16-bit */
277 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
281 static int cht_aif1_startup(struct snd_pcm_substream *substream)
283 return snd_pcm_hw_constraint_single(substream->runtime,
284 SNDRV_PCM_HW_PARAM_RATE, 48000);
287 static int cht_max98090_headset_init(struct snd_soc_component *component)
289 struct snd_soc_card *card = component->card;
290 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
291 struct snd_soc_jack *jack = &ctx->jack;
296 * TI supports 4 butons headset detection
302 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
303 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
304 SND_JACK_BTN_2 | SND_JACK_BTN_3;
306 ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
309 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
313 return ts3a227e_enable_jack_detect(component, jack);
316 static const struct snd_soc_ops cht_aif1_ops = {
317 .startup = cht_aif1_startup,
320 static const struct snd_soc_ops cht_be_ssp2_ops = {
321 .hw_params = cht_aif1_hw_params,
324 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
325 .name = "Headset Chip",
326 .init = cht_max98090_headset_init,
327 .codec_name = "i2c-104C227E:00",
330 static struct snd_soc_dai_link cht_dailink[] = {
331 [MERR_DPCM_AUDIO] = {
332 .name = "Audio Port",
333 .stream_name = "Audio",
334 .cpu_dai_name = "media-cpu-dai",
335 .codec_dai_name = "snd-soc-dummy-dai",
336 .codec_name = "snd-soc-dummy",
337 .platform_name = "sst-mfld-platform",
342 .ops = &cht_aif1_ops,
344 [MERR_DPCM_DEEP_BUFFER] = {
345 .name = "Deep-Buffer Audio Port",
346 .stream_name = "Deep-Buffer Audio",
347 .cpu_dai_name = "deepbuffer-cpu-dai",
348 .codec_dai_name = "snd-soc-dummy-dai",
349 .codec_name = "snd-soc-dummy",
350 .platform_name = "sst-mfld-platform",
354 .ops = &cht_aif1_ops,
358 .name = "SSP2-Codec",
360 .cpu_dai_name = "ssp2-port",
361 .platform_name = "sst-mfld-platform",
363 .codec_dai_name = "HiFi",
364 .codec_name = "i2c-193C9890:00",
365 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
366 | SND_SOC_DAIFMT_CBS_CFS,
367 .init = cht_codec_init,
368 .be_hw_params_fixup = cht_codec_fixup,
371 .ops = &cht_be_ssp2_ops,
376 static struct snd_soc_card snd_soc_card_cht = {
377 .name = "chtmax98090",
378 .owner = THIS_MODULE,
379 .dai_link = cht_dailink,
380 .num_links = ARRAY_SIZE(cht_dailink),
381 .aux_dev = &cht_max98090_headset_dev,
383 .dapm_widgets = cht_dapm_widgets,
384 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
385 .dapm_routes = cht_audio_map,
386 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
387 .controls = cht_mc_controls,
388 .num_controls = ARRAY_SIZE(cht_mc_controls),
391 static const struct dmi_system_id cht_max98090_quirk_table[] = {
393 /* Swanky model Chromebook (Toshiba Chromebook 2) */
395 DMI_MATCH(DMI_PRODUCT_NAME, "Swanky"),
397 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
402 static int snd_cht_mc_probe(struct platform_device *pdev)
404 const struct dmi_system_id *dmi_id;
405 struct device *dev = &pdev->dev;
407 struct cht_mc_private *drv;
408 const char *mclk_name;
411 dmi_id = dmi_first_match(cht_max98090_quirk_table);
413 quirks = (unsigned long)dmi_id->driver_data;
415 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
419 drv->ts3a227e_present = acpi_dev_found("104C227E");
420 if (!drv->ts3a227e_present) {
421 /* no need probe TI jack detection chip */
422 snd_soc_card_cht.aux_dev = NULL;
423 snd_soc_card_cht.num_aux_devs = 0;
425 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
426 acpi_max98090_gpios);
428 dev_dbg(dev, "Unable to add GPIO mapping table\n");
431 /* register the soc card */
432 snd_soc_card_cht.dev = &pdev->dev;
433 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
435 if (quirks & QUIRK_PMC_PLT_CLK_0)
436 mclk_name = "pmc_plt_clk_0";
438 mclk_name = "pmc_plt_clk_3";
440 drv->mclk = devm_clk_get(&pdev->dev, mclk_name);
441 if (IS_ERR(drv->mclk)) {
443 "Failed to get MCLK from %s: %ld\n",
444 mclk_name, PTR_ERR(drv->mclk));
445 return PTR_ERR(drv->mclk);
448 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
451 "snd_soc_register_card failed %d\n", ret_val);
454 platform_set_drvdata(pdev, &snd_soc_card_cht);
458 static struct platform_driver snd_cht_mc_driver = {
460 .name = "cht-bsw-max98090",
462 .probe = snd_cht_mc_probe,
465 module_platform_driver(snd_cht_mc_driver)
467 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
469 MODULE_LICENSE("GPL v2");
470 MODULE_ALIAS("platform:cht-bsw-max98090");