2 * cht-bsw-nau8824.c - ASoc Machine driver for Intel Cherryview-based
3 * platforms Cherrytrail and Braswell, with nau8824 codec.
5 * Copyright (C) 2018 Intel Corp
6 * Copyright (C) 2018 Nuvoton Technology Corp
8 * Author: Wang, Joseph C <joequant@gmail.com>
9 * Co-author: John Hsu <KCHSU0@nuvoton.com>
10 * This file is based on cht_bsw_rt5672.c and cht-bsw-max98090.c
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/jack.h>
29 #include <linux/input.h>
30 #include "../atom/sst-atom-controls.h"
31 #include "../../codecs/nau8824.h"
33 struct cht_mc_private {
34 struct snd_soc_jack jack;
37 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
40 .mask = SND_JACK_HEADPHONE,
44 .mask = SND_JACK_MICROPHONE,
48 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
49 SND_SOC_DAPM_HP("Headphone", NULL),
50 SND_SOC_DAPM_MIC("Headset Mic", NULL),
51 SND_SOC_DAPM_MIC("Int Mic", NULL),
52 SND_SOC_DAPM_SPK("Ext Spk", NULL),
55 static const struct snd_soc_dapm_route cht_audio_map[] = {
56 {"Ext Spk", NULL, "SPKOUTL"},
57 {"Ext Spk", NULL, "SPKOUTR"},
58 {"Headphone", NULL, "HPOL"},
59 {"Headphone", NULL, "HPOR"},
60 {"MIC1", NULL, "Int Mic"},
61 {"MIC2", NULL, "Int Mic"},
62 {"HSMIC1", NULL, "Headset Mic"},
63 {"HSMIC2", NULL, "Headset Mic"},
64 {"Playback", NULL, "ssp2 Tx"},
65 {"ssp2 Tx", NULL, "codec_out0"},
66 {"ssp2 Tx", NULL, "codec_out1"},
67 {"codec_in0", NULL, "ssp2 Rx" },
68 {"codec_in1", NULL, "ssp2 Rx" },
69 {"ssp2 Rx", NULL, "Capture"},
72 static const struct snd_kcontrol_new cht_mc_controls[] = {
73 SOC_DAPM_PIN_SWITCH("Headphone"),
74 SOC_DAPM_PIN_SWITCH("Headset Mic"),
75 SOC_DAPM_PIN_SWITCH("Int Mic"),
76 SOC_DAPM_PIN_SWITCH("Ext Spk"),
79 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params *params)
82 struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 struct snd_soc_dai *codec_dai = rtd->codec_dai;
86 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8824_CLK_FLL_FS, 0,
89 dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
92 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, params_rate(params),
93 params_rate(params) * 256);
95 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
102 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
104 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
105 struct snd_soc_jack *jack = &ctx->jack;
106 struct snd_soc_dai *codec_dai = runtime->codec_dai;
107 struct snd_soc_component *component = codec_dai->component;
110 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
111 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xf, 0x1, 4, 24);
113 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
117 /* NAU88L24 supports 4 butons headset detection
123 jack_type = SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
124 SND_JACK_BTN_2 | SND_JACK_BTN_3;
125 ret = snd_soc_card_jack_new(runtime->card, "Headset", jack_type, jack,
126 cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
128 dev_err(runtime->dev,
129 "Headset Jack creation failed %d\n", ret);
132 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
133 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
134 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
135 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
137 nau8824_enable_jack_detect(component, jack);
142 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
143 struct snd_pcm_hw_params *params)
145 struct snd_interval *rate = hw_param_interval(params,
146 SNDRV_PCM_HW_PARAM_RATE);
147 struct snd_interval *channels = hw_param_interval(params,
148 SNDRV_PCM_HW_PARAM_CHANNELS);
149 struct snd_mask *fmt =
150 hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
152 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
153 rate->min = rate->max = 48000;
154 channels->min = channels->max = 2;
156 /* set SSP2 to 24-bit */
158 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
163 static int cht_aif1_startup(struct snd_pcm_substream *substream)
165 return snd_pcm_hw_constraint_single(substream->runtime,
166 SNDRV_PCM_HW_PARAM_RATE, 48000);
169 static const struct snd_soc_ops cht_aif1_ops = {
170 .startup = cht_aif1_startup,
173 static const struct snd_soc_ops cht_be_ssp2_ops = {
174 .hw_params = cht_aif1_hw_params,
177 static struct snd_soc_dai_link cht_dailink[] = {
178 /* Front End DAI links */
179 [MERR_DPCM_AUDIO] = {
180 .name = "Audio Port",
181 .stream_name = "Audio",
182 .cpu_dai_name = "media-cpu-dai",
183 .codec_dai_name = "snd-soc-dummy-dai",
184 .codec_name = "snd-soc-dummy",
185 .platform_name = "sst-mfld-platform",
190 .ops = &cht_aif1_ops,
192 [MERR_DPCM_DEEP_BUFFER] = {
193 .name = "Deep-Buffer Audio Port",
194 .stream_name = "Deep-Buffer Audio",
195 .cpu_dai_name = "deepbuffer-cpu-dai",
196 .codec_dai_name = "snd-soc-dummy-dai",
197 .codec_name = "snd-soc-dummy",
198 .platform_name = "sst-mfld-platform",
202 .ops = &cht_aif1_ops,
204 [MERR_DPCM_COMPR] = {
205 .name = "Compressed Port",
206 .stream_name = "Compress",
207 .cpu_dai_name = "compress-cpu-dai",
208 .codec_dai_name = "snd-soc-dummy-dai",
209 .codec_name = "snd-soc-dummy",
210 .platform_name = "sst-mfld-platform",
212 /* Back End DAI links */
215 .name = "SSP2-Codec",
217 .cpu_dai_name = "ssp2-port",
218 .platform_name = "sst-mfld-platform",
220 .codec_dai_name = NAU8824_CODEC_DAI,
221 .codec_name = "i2c-10508824:00",
222 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
223 | SND_SOC_DAIFMT_CBS_CFS,
224 .init = cht_codec_init,
225 .be_hw_params_fixup = cht_codec_fixup,
228 .ops = &cht_be_ssp2_ops,
233 static struct snd_soc_card snd_soc_card_cht = {
234 .name = "chtnau8824",
235 .owner = THIS_MODULE,
236 .dai_link = cht_dailink,
237 .num_links = ARRAY_SIZE(cht_dailink),
238 .dapm_widgets = cht_dapm_widgets,
239 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
240 .dapm_routes = cht_audio_map,
241 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
242 .controls = cht_mc_controls,
243 .num_controls = ARRAY_SIZE(cht_mc_controls),
246 static int snd_cht_mc_probe(struct platform_device *pdev)
248 struct cht_mc_private *drv;
251 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
254 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
256 /* register the soc card */
257 snd_soc_card_cht.dev = &pdev->dev;
258 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
261 "snd_soc_register_card failed %d\n", ret_val);
264 platform_set_drvdata(pdev, &snd_soc_card_cht);
269 static struct platform_driver snd_cht_mc_driver = {
271 .name = "cht-bsw-nau8824",
273 .probe = snd_cht_mc_probe,
276 module_platform_driver(snd_cht_mc_driver);
278 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
279 MODULE_AUTHOR("Wang, Joseph C <joequant@gmail.com>");
280 MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>");
281 MODULE_LICENSE("GPL v2");
282 MODULE_ALIAS("platform:cht-bsw-nau8824");