1 /*
2  *  cht-bsw-nau8824.c - ASoc Machine driver for Intel Cherryview-based
3  *          platforms Cherrytrail and Braswell, with nau8824 codec.
4  *
5  *  Copyright (C) 2018 Intel Corp
6  *  Copyright (C) 2018 Nuvoton Technology Corp
7  *
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
11  *
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.
15  *
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.
20  */
21 
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"
32 
33 struct cht_mc_private {
34 	struct snd_soc_jack jack;
35 };
36 
37 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
38 	{
39 		.pin = "Headphone",
40 		.mask = SND_JACK_HEADPHONE,
41 	},
42 	{
43 		.pin = "Headset Mic",
44 		.mask = SND_JACK_MICROPHONE,
45 	},
46 };
47 
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),
53 };
54 
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"},
70 };
71 
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"),
77 };
78 
79 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
80 	struct snd_pcm_hw_params *params)
81 {
82 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
84 	int ret;
85 
86 	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8824_CLK_FLL_FS, 0,
87 		SND_SOC_CLOCK_IN);
88 	if (ret < 0) {
89 		dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
90 		return ret;
91 	}
92 	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, params_rate(params),
93 		params_rate(params) * 256);
94 	if (ret < 0) {
95 		dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
96 		return ret;
97 	}
98 
99 	return 0;
100 }
101 
102 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
103 {
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;
108 	int ret, jack_type;
109 
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);
112 	if (ret < 0) {
113 		dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
114 		return ret;
115 	}
116 
117 	/* NAU88L24 supports 4 butons headset detection
118 	 * KEY_MEDIA
119 	 * KEY_VOICECOMMAND
120 	 * KEY_VOLUMEUP
121 	 * KEY_VOLUMEDOWN
122 	 */
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));
127 	if (ret) {
128 		dev_err(runtime->dev,
129 			"Headset Jack creation failed %d\n", ret);
130 		return ret;
131 	}
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);
136 
137 	nau8824_enable_jack_detect(component, jack);
138 
139 	return ret;
140 }
141 
142 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
143 	struct snd_pcm_hw_params *params)
144 {
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);
151 
152 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
153 	rate->min = rate->max = 48000;
154 	channels->min = channels->max = 2;
155 
156 	/* set SSP2 to 24-bit */
157 	snd_mask_none(fmt);
158 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
159 
160 	return 0;
161 }
162 
163 static int cht_aif1_startup(struct snd_pcm_substream *substream)
164 {
165 	return snd_pcm_hw_constraint_single(substream->runtime,
166 		SNDRV_PCM_HW_PARAM_RATE, 48000);
167 }
168 
169 static const struct snd_soc_ops cht_aif1_ops = {
170 	.startup = cht_aif1_startup,
171 };
172 
173 static const struct snd_soc_ops cht_be_ssp2_ops = {
174 	.hw_params = cht_aif1_hw_params,
175 };
176 
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",
186 		.nonatomic = true,
187 		.dynamic = 1,
188 		.dpcm_playback = 1,
189 		.dpcm_capture = 1,
190 		.ops = &cht_aif1_ops,
191 	},
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",
199 		.nonatomic = true,
200 		.dynamic = 1,
201 		.dpcm_playback = 1,
202 		.ops = &cht_aif1_ops,
203 	},
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",
211 	},
212 	/* Back End DAI links */
213 	{
214 		/* SSP2 - Codec */
215 		.name = "SSP2-Codec",
216 		.id = 1,
217 		.cpu_dai_name = "ssp2-port",
218 		.platform_name = "sst-mfld-platform",
219 		.no_pcm = 1,
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,
226 		.dpcm_playback = 1,
227 		.dpcm_capture = 1,
228 		.ops = &cht_be_ssp2_ops,
229 	},
230 };
231 
232 /* SoC card */
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),
244 };
245 
246 static int snd_cht_mc_probe(struct platform_device *pdev)
247 {
248 	struct cht_mc_private *drv;
249 	int ret_val;
250 
251 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
252 	if (!drv)
253 		return -ENOMEM;
254 	snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
255 
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);
259 	if (ret_val) {
260 		dev_err(&pdev->dev,
261 			"snd_soc_register_card failed %d\n", ret_val);
262 		return ret_val;
263 	}
264 	platform_set_drvdata(pdev, &snd_soc_card_cht);
265 
266 	return ret_val;
267 }
268 
269 static struct platform_driver snd_cht_mc_driver = {
270 	.driver = {
271 		.name = "cht-bsw-nau8824",
272 	},
273 	.probe = snd_cht_mc_probe,
274 };
275 
276 module_platform_driver(snd_cht_mc_driver);
277 
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");
283