2 * bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
3 * platforms with Everest ES8316 SoC
5 * Copyright (C) 2017 Endless Mobile, Inc.
6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
7 * Daniel Drake <drake@endlessm.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <asm/platform_sst_audio.h>
28 #include <linux/clk.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-acpi.h>
33 #include "../atom/sst-atom-controls.h"
34 #include "../common/sst-dsp.h"
36 struct byt_cht_es8316_private {
40 static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
41 SND_SOC_DAPM_HP("Headphone", NULL),
44 * The codec supports two analog microphone inputs. I have only
45 * tested MIC1. A DMIC route could also potentially be added
46 * if such functionality is found on another platform.
48 SND_SOC_DAPM_MIC("Microphone 1", NULL),
49 SND_SOC_DAPM_MIC("Microphone 2", NULL),
52 static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
53 {"MIC1", NULL, "Microphone 1"},
54 {"MIC2", NULL, "Microphone 2"},
56 {"Headphone", NULL, "HPOL"},
57 {"Headphone", NULL, "HPOR"},
59 {"Playback", NULL, "ssp2 Tx"},
60 {"ssp2 Tx", NULL, "codec_out0"},
61 {"ssp2 Tx", NULL, "codec_out1"},
62 {"codec_in0", NULL, "ssp2 Rx" },
63 {"codec_in1", NULL, "ssp2 Rx" },
64 {"ssp2 Rx", NULL, "Capture"},
67 static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
68 SOC_DAPM_PIN_SWITCH("Headphone"),
69 SOC_DAPM_PIN_SWITCH("Microphone 1"),
70 SOC_DAPM_PIN_SWITCH("Microphone 2"),
73 static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
75 struct snd_soc_card *card = runtime->card;
76 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
79 card->dapm.idle_bias_off = true;
82 * The firmware might enable the clock at boot (this information
83 * may or may not be reflected in the enable clock register).
84 * To change the rate we must disable the clock first to cover these
85 * cases. Due to common clock framework restrictions that do not allow
86 * to disable a clock that has not been enabled, we need to enable
89 ret = clk_prepare_enable(priv->mclk);
91 clk_disable_unprepare(priv->mclk);
93 ret = clk_set_rate(priv->mclk, 19200000);
95 dev_err(card->dev, "unable to set MCLK rate\n");
97 ret = clk_prepare_enable(priv->mclk);
99 dev_err(card->dev, "unable to enable MCLK\n");
101 ret = snd_soc_dai_set_sysclk(runtime->codec_dai, 0, 19200000,
104 dev_err(card->dev, "can't set codec clock %d\n", ret);
111 static const struct snd_soc_pcm_stream byt_cht_es8316_dai_params = {
112 .formats = SNDRV_PCM_FMTBIT_S24_LE,
119 static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
120 struct snd_pcm_hw_params *params)
122 struct snd_interval *rate = hw_param_interval(params,
123 SNDRV_PCM_HW_PARAM_RATE);
124 struct snd_interval *channels = hw_param_interval(params,
125 SNDRV_PCM_HW_PARAM_CHANNELS);
128 /* The DSP will covert the FE rate to 48k, stereo */
129 rate->min = rate->max = 48000;
130 channels->min = channels->max = 2;
132 /* set SSP2 to 24-bit */
133 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
136 * Default mode for SSP configuration is TDM 4 slot, override config
137 * with explicit setting to I2S 2ch 24-bit. The word length is set with
138 * dai_set_tdm_slot() since there is no other API exposed
140 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
142 SND_SOC_DAIFMT_NB_NF |
143 SND_SOC_DAIFMT_CBS_CFS
146 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
150 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
152 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
159 static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
161 return snd_pcm_hw_constraint_single(substream->runtime,
162 SNDRV_PCM_HW_PARAM_RATE, 48000);
165 static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
166 .startup = byt_cht_es8316_aif1_startup,
169 static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
170 [MERR_DPCM_AUDIO] = {
171 .name = "Audio Port",
172 .stream_name = "Audio",
173 .cpu_dai_name = "media-cpu-dai",
174 .codec_dai_name = "snd-soc-dummy-dai",
175 .codec_name = "snd-soc-dummy",
176 .platform_name = "sst-mfld-platform",
181 .ops = &byt_cht_es8316_aif1_ops,
184 [MERR_DPCM_DEEP_BUFFER] = {
185 .name = "Deep-Buffer Audio Port",
186 .stream_name = "Deep-Buffer Audio",
187 .cpu_dai_name = "deepbuffer-cpu-dai",
188 .codec_dai_name = "snd-soc-dummy-dai",
189 .codec_name = "snd-soc-dummy",
190 .platform_name = "sst-mfld-platform",
194 .ops = &byt_cht_es8316_aif1_ops,
199 /* Only SSP2 has been tested here, so BYT-CR platforms that
200 * require SSP0 will not work.
202 .name = "SSP2-Codec",
204 .cpu_dai_name = "ssp2-port",
205 .platform_name = "sst-mfld-platform",
207 .codec_dai_name = "ES8316 HiFi",
208 .codec_name = "i2c-ESSX8316:00",
209 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
210 | SND_SOC_DAIFMT_CBS_CFS,
211 .be_hw_params_fixup = byt_cht_es8316_codec_fixup,
215 .init = byt_cht_es8316_init,
221 static struct snd_soc_card byt_cht_es8316_card = {
222 .name = "bytcht-es8316",
223 .owner = THIS_MODULE,
224 .dai_link = byt_cht_es8316_dais,
225 .num_links = ARRAY_SIZE(byt_cht_es8316_dais),
226 .dapm_widgets = byt_cht_es8316_widgets,
227 .num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
228 .dapm_routes = byt_cht_es8316_audio_map,
229 .num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
230 .controls = byt_cht_es8316_controls,
231 .num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
232 .fully_routed = true,
235 static char codec_name[SND_ACPI_I2C_ID_LEN];
237 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
239 struct byt_cht_es8316_private *priv;
240 struct snd_soc_acpi_mach *mach;
241 const char *i2c_name = NULL;
246 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
250 mach = (&pdev->dev)->platform_data;
251 /* fix index of codec dai */
252 for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
253 if (!strcmp(byt_cht_es8316_dais[i].codec_name,
254 "i2c-ESSX8316:00")) {
260 /* fixup codec name based on HID */
261 i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
263 snprintf(codec_name, sizeof(codec_name),
264 "%s%s", "i2c-", i2c_name);
265 byt_cht_es8316_dais[dai_index].codec_name = codec_name;
268 /* register the soc card */
269 byt_cht_es8316_card.dev = &pdev->dev;
270 snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
272 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
273 if (IS_ERR(priv->mclk)) {
274 ret = PTR_ERR(priv->mclk);
276 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
281 ret = devm_snd_soc_register_card(&pdev->dev, &byt_cht_es8316_card);
283 dev_err(&pdev->dev, "snd_soc_register_card failed %d\n", ret);
286 platform_set_drvdata(pdev, &byt_cht_es8316_card);
290 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
292 .name = "bytcht_es8316",
294 .probe = snd_byt_cht_es8316_mc_probe,
297 module_platform_driver(snd_byt_cht_es8316_mc_driver);
298 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
299 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
300 MODULE_LICENSE("GPL v2");
301 MODULE_ALIAS("platform:bytcht_es8316");