Linux 4.15.6
[linux/fpc-iii.git] / sound / soc / intel / boards / cht_bsw_max98090_ti.c
blobd3e1c7e12004dded46fd24ac9345758cea556233
1 /*
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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
26 #include <linux/clk.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/jack.h>
31 #include "../../codecs/max98090.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../../codecs/ts3a227e.h"
35 #define CHT_PLAT_CLK_3_HZ 19200000
36 #define CHT_CODEC_DAI "HiFi"
38 struct cht_mc_private {
39 struct clk *mclk;
40 struct snd_soc_jack jack;
41 bool ts3a227e_present;
44 static int platform_clock_control(struct snd_soc_dapm_widget *w,
45 struct snd_kcontrol *k, int event)
47 struct snd_soc_dapm_context *dapm = w->dapm;
48 struct snd_soc_card *card = dapm->card;
49 struct snd_soc_dai *codec_dai;
50 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
51 int ret;
53 codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
54 if (!codec_dai) {
55 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
56 return -EIO;
59 if (SND_SOC_DAPM_EVENT_ON(event)) {
60 ret = clk_prepare_enable(ctx->mclk);
61 if (ret < 0) {
62 dev_err(card->dev,
63 "could not configure MCLK state");
64 return ret;
66 } else {
67 clk_disable_unprepare(ctx->mclk);
70 return 0;
73 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
74 SND_SOC_DAPM_HP("Headphone", NULL),
75 SND_SOC_DAPM_MIC("Headset Mic", NULL),
76 SND_SOC_DAPM_MIC("Int Mic", NULL),
77 SND_SOC_DAPM_SPK("Ext Spk", NULL),
78 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
79 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
80 SND_SOC_DAPM_POST_PMD),
83 static const struct snd_soc_dapm_route cht_audio_map[] = {
84 {"IN34", NULL, "Headset Mic"},
85 {"Headset Mic", NULL, "MICBIAS"},
86 {"DMICL", NULL, "Int Mic"},
87 {"Headphone", NULL, "HPL"},
88 {"Headphone", NULL, "HPR"},
89 {"Ext Spk", NULL, "SPKL"},
90 {"Ext Spk", NULL, "SPKR"},
91 {"HiFi Playback", NULL, "ssp2 Tx"},
92 {"ssp2 Tx", NULL, "codec_out0"},
93 {"ssp2 Tx", NULL, "codec_out1"},
94 {"codec_in0", NULL, "ssp2 Rx" },
95 {"codec_in1", NULL, "ssp2 Rx" },
96 {"ssp2 Rx", NULL, "HiFi Capture"},
97 {"Headphone", NULL, "Platform Clock"},
98 {"Headset Mic", NULL, "Platform Clock"},
99 {"Int Mic", NULL, "Platform Clock"},
100 {"Ext Spk", NULL, "Platform Clock"},
103 static const struct snd_kcontrol_new cht_mc_controls[] = {
104 SOC_DAPM_PIN_SWITCH("Headphone"),
105 SOC_DAPM_PIN_SWITCH("Headset Mic"),
106 SOC_DAPM_PIN_SWITCH("Int Mic"),
107 SOC_DAPM_PIN_SWITCH("Ext Spk"),
110 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
111 struct snd_pcm_hw_params *params)
113 struct snd_soc_pcm_runtime *rtd = substream->private_data;
114 struct snd_soc_dai *codec_dai = rtd->codec_dai;
115 int ret;
117 ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
118 CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
119 if (ret < 0) {
120 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
121 return ret;
124 return 0;
127 static int cht_ti_jack_event(struct notifier_block *nb,
128 unsigned long event, void *data)
130 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
131 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
133 if (event & SND_JACK_MICROPHONE) {
134 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
135 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
136 snd_soc_dapm_sync(dapm);
137 } else {
138 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
139 snd_soc_dapm_disable_pin(dapm, "SHDN");
140 snd_soc_dapm_sync(dapm);
143 return 0;
146 static struct notifier_block cht_jack_nb = {
147 .notifier_call = cht_ti_jack_event,
150 static struct snd_soc_jack_pin hs_jack_pins[] = {
152 .pin = "Headphone",
153 .mask = SND_JACK_HEADPHONE,
156 .pin = "Headset Mic",
157 .mask = SND_JACK_MICROPHONE,
161 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
163 .name = "hp",
164 .report = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
165 .debounce_time = 200,
168 .name = "mic",
169 .invert = 1,
170 .report = SND_JACK_MICROPHONE,
171 .debounce_time = 200,
175 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
176 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
178 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
179 { "hp-gpios", &hp_gpios, 1 },
180 { "mic-gpios", &mic_gpios, 1 },
184 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
186 int ret;
187 int jack_type;
188 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
189 struct snd_soc_jack *jack = &ctx->jack;
191 if (ctx->ts3a227e_present) {
193 * The jack has already been created in the
194 * cht_max98090_headset_init() function.
196 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
197 return 0;
200 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
202 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
203 jack_type, jack,
204 hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
205 if (ret) {
206 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
207 return ret;
210 ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
211 ARRAY_SIZE(hs_jack_gpios),
212 hs_jack_gpios);
213 if (ret) {
215 * flag error but don't bail if jack detect is broken
216 * due to platform issues or bad BIOS/configuration
218 dev_err(runtime->dev,
219 "jack detection gpios not added, error %d\n", ret);
223 * The firmware might enable the clock at
224 * boot (this information may or may not
225 * be reflected in the enable clock register).
226 * To change the rate we must disable the clock
227 * first to cover these cases. Due to common
228 * clock framework restrictions that do not allow
229 * to disable a clock that has not been enabled,
230 * we need to enable the clock first.
232 ret = clk_prepare_enable(ctx->mclk);
233 if (!ret)
234 clk_disable_unprepare(ctx->mclk);
236 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
238 if (ret)
239 dev_err(runtime->dev, "unable to set MCLK rate\n");
241 return ret;
244 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
245 struct snd_pcm_hw_params *params)
247 struct snd_interval *rate = hw_param_interval(params,
248 SNDRV_PCM_HW_PARAM_RATE);
249 struct snd_interval *channels = hw_param_interval(params,
250 SNDRV_PCM_HW_PARAM_CHANNELS);
251 int ret = 0;
252 unsigned int fmt = 0;
254 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
255 if (ret < 0) {
256 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
257 return ret;
260 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
261 | SND_SOC_DAIFMT_CBS_CFS;
263 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
264 if (ret < 0) {
265 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
266 return ret;
269 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
270 rate->min = rate->max = 48000;
271 channels->min = channels->max = 2;
273 /* set SSP2 to 16-bit */
274 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
275 return 0;
278 static int cht_aif1_startup(struct snd_pcm_substream *substream)
280 return snd_pcm_hw_constraint_single(substream->runtime,
281 SNDRV_PCM_HW_PARAM_RATE, 48000);
284 static int cht_max98090_headset_init(struct snd_soc_component *component)
286 struct snd_soc_card *card = component->card;
287 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
288 struct snd_soc_jack *jack = &ctx->jack;
289 int jack_type;
290 int ret;
293 * TI supports 4 butons headset detection
294 * KEY_MEDIA
295 * KEY_VOICECOMMAND
296 * KEY_VOLUMEUP
297 * KEY_VOLUMEDOWN
299 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
300 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
301 SND_JACK_BTN_2 | SND_JACK_BTN_3;
303 ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
304 jack, NULL, 0);
305 if (ret) {
306 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
307 return ret;
310 return ts3a227e_enable_jack_detect(component, jack);
313 static const struct snd_soc_ops cht_aif1_ops = {
314 .startup = cht_aif1_startup,
317 static const struct snd_soc_ops cht_be_ssp2_ops = {
318 .hw_params = cht_aif1_hw_params,
321 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
322 .name = "Headset Chip",
323 .init = cht_max98090_headset_init,
324 .codec_name = "i2c-104C227E:00",
327 static struct snd_soc_dai_link cht_dailink[] = {
328 [MERR_DPCM_AUDIO] = {
329 .name = "Audio Port",
330 .stream_name = "Audio",
331 .cpu_dai_name = "media-cpu-dai",
332 .codec_dai_name = "snd-soc-dummy-dai",
333 .codec_name = "snd-soc-dummy",
334 .platform_name = "sst-mfld-platform",
335 .nonatomic = true,
336 .dynamic = 1,
337 .dpcm_playback = 1,
338 .dpcm_capture = 1,
339 .ops = &cht_aif1_ops,
341 [MERR_DPCM_DEEP_BUFFER] = {
342 .name = "Deep-Buffer Audio Port",
343 .stream_name = "Deep-Buffer Audio",
344 .cpu_dai_name = "deepbuffer-cpu-dai",
345 .codec_dai_name = "snd-soc-dummy-dai",
346 .codec_name = "snd-soc-dummy",
347 .platform_name = "sst-mfld-platform",
348 .nonatomic = true,
349 .dynamic = 1,
350 .dpcm_playback = 1,
351 .ops = &cht_aif1_ops,
353 /* back ends */
355 .name = "SSP2-Codec",
356 .id = 0,
357 .cpu_dai_name = "ssp2-port",
358 .platform_name = "sst-mfld-platform",
359 .no_pcm = 1,
360 .codec_dai_name = "HiFi",
361 .codec_name = "i2c-193C9890:00",
362 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
363 | SND_SOC_DAIFMT_CBS_CFS,
364 .init = cht_codec_init,
365 .be_hw_params_fixup = cht_codec_fixup,
366 .dpcm_playback = 1,
367 .dpcm_capture = 1,
368 .ops = &cht_be_ssp2_ops,
372 /* SoC card */
373 static struct snd_soc_card snd_soc_card_cht = {
374 .name = "chtmax98090",
375 .owner = THIS_MODULE,
376 .dai_link = cht_dailink,
377 .num_links = ARRAY_SIZE(cht_dailink),
378 .aux_dev = &cht_max98090_headset_dev,
379 .num_aux_devs = 1,
380 .dapm_widgets = cht_dapm_widgets,
381 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
382 .dapm_routes = cht_audio_map,
383 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
384 .controls = cht_mc_controls,
385 .num_controls = ARRAY_SIZE(cht_mc_controls),
388 static int snd_cht_mc_probe(struct platform_device *pdev)
390 struct device *dev = &pdev->dev;
391 int ret_val = 0;
392 struct cht_mc_private *drv;
394 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
395 if (!drv)
396 return -ENOMEM;
398 drv->ts3a227e_present = acpi_dev_found("104C227E");
399 if (!drv->ts3a227e_present) {
400 /* no need probe TI jack detection chip */
401 snd_soc_card_cht.aux_dev = NULL;
402 snd_soc_card_cht.num_aux_devs = 0;
404 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
405 acpi_max98090_gpios);
406 if (ret_val)
407 dev_dbg(dev, "Unable to add GPIO mapping table\n");
410 /* register the soc card */
411 snd_soc_card_cht.dev = &pdev->dev;
412 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
414 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
415 if (IS_ERR(drv->mclk)) {
416 dev_err(&pdev->dev,
417 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
418 PTR_ERR(drv->mclk));
419 return PTR_ERR(drv->mclk);
422 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
423 if (ret_val) {
424 dev_err(&pdev->dev,
425 "snd_soc_register_card failed %d\n", ret_val);
426 return ret_val;
428 platform_set_drvdata(pdev, &snd_soc_card_cht);
429 return ret_val;
432 static struct platform_driver snd_cht_mc_driver = {
433 .driver = {
434 .name = "cht-bsw-max98090",
436 .probe = snd_cht_mc_probe,
439 module_platform_driver(snd_cht_mc_driver)
441 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
442 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
443 MODULE_LICENSE("GPL v2");
444 MODULE_ALIAS("platform:cht-bsw-max98090");