• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
3  *                     Cherrytrail and Braswell, with RT5645 codec.
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  Author: Fang, Yang A <yang.a.fang@intel.com>
7  *	        N,Harshapriya <harshapriya.n@intel.com>
8  *  This file is modified from cht_bsw_rt5672.c
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
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.
14  *
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.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22 
23 #include <linux/module.h>
24 #include <linux/acpi.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.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/rt5645.h"
32 #include "../atom/sst-atom-controls.h"
33 
34 #define CHT_PLAT_CLK_3_HZ	19200000
35 #define CHT_CODEC_DAI	"rt5645-aif1"
36 
37 struct cht_acpi_card {
38 	char *codec_id;
39 	int codec_type;
40 	struct snd_soc_card *soc_card;
41 };
42 
43 struct cht_mc_private {
44 	struct snd_soc_jack jack;
45 	struct cht_acpi_card *acpi_card;
46 };
47 
cht_get_codec_dai(struct snd_soc_card * card)48 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
49 {
50 	int i;
51 
52 	for (i = 0; i < card->num_rtd; i++) {
53 		struct snd_soc_pcm_runtime *rtd;
54 
55 		rtd = card->rtd + i;
56 		if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
57 			     strlen(CHT_CODEC_DAI)))
58 			return rtd->codec_dai;
59 	}
60 	return NULL;
61 }
62 
platform_clock_control(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)63 static int platform_clock_control(struct snd_soc_dapm_widget *w,
64 		struct snd_kcontrol *k, int  event)
65 {
66 	struct snd_soc_dapm_context *dapm = w->dapm;
67 	struct snd_soc_card *card = dapm->card;
68 	struct snd_soc_dai *codec_dai;
69 	int ret;
70 
71 	codec_dai = cht_get_codec_dai(card);
72 	if (!codec_dai) {
73 		dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
74 		return -EIO;
75 	}
76 
77 	if (!SND_SOC_DAPM_EVENT_OFF(event))
78 		return 0;
79 
80 	/* Set codec sysclk source to its internal clock because codec PLL will
81 	 * be off when idle and MCLK will also be off by ACPI when codec is
82 	 * runtime suspended. Codec needs clock for jack detection and button
83 	 * press.
84 	 */
85 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
86 			0, SND_SOC_CLOCK_IN);
87 	if (ret < 0) {
88 		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
96 	SND_SOC_DAPM_HP("Headphone", NULL),
97 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
98 	SND_SOC_DAPM_MIC("Int Mic", NULL),
99 	SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
100 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
101 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
102 			platform_clock_control, SND_SOC_DAPM_POST_PMD),
103 };
104 
105 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
106 	{"IN1P", NULL, "Headset Mic"},
107 	{"IN1N", NULL, "Headset Mic"},
108 	{"DMIC L1", NULL, "Int Mic"},
109 	{"DMIC R1", NULL, "Int Mic"},
110 	{"IN2P", NULL, "Int Analog Mic"},
111 	{"IN2N", NULL, "Int Analog Mic"},
112 	{"Headphone", NULL, "HPOL"},
113 	{"Headphone", NULL, "HPOR"},
114 	{"Ext Spk", NULL, "SPOL"},
115 	{"Ext Spk", NULL, "SPOR"},
116 	{"AIF1 Playback", NULL, "ssp2 Tx"},
117 	{"ssp2 Tx", NULL, "codec_out0"},
118 	{"ssp2 Tx", NULL, "codec_out1"},
119 	{"codec_in0", NULL, "ssp2 Rx" },
120 	{"codec_in1", NULL, "ssp2 Rx" },
121 	{"ssp2 Rx", NULL, "AIF1 Capture"},
122 	{"Headphone", NULL, "Platform Clock"},
123 	{"Headset Mic", NULL, "Platform Clock"},
124 	{"Int Mic", NULL, "Platform Clock"},
125 	{"Int Analog Mic", NULL, "Platform Clock"},
126 	{"Int Analog Mic", NULL, "micbias1"},
127 	{"Int Analog Mic", NULL, "micbias2"},
128 	{"Ext Spk", NULL, "Platform Clock"},
129 };
130 
131 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
132 	{"IN1P", NULL, "Headset Mic"},
133 	{"IN1N", NULL, "Headset Mic"},
134 	{"DMIC L2", NULL, "Int Mic"},
135 	{"DMIC R2", NULL, "Int Mic"},
136 	{"Headphone", NULL, "HPOL"},
137 	{"Headphone", NULL, "HPOR"},
138 	{"Ext Spk", NULL, "SPOL"},
139 	{"Ext Spk", NULL, "SPOR"},
140 	{"AIF1 Playback", NULL, "ssp2 Tx"},
141 	{"ssp2 Tx", NULL, "codec_out0"},
142 	{"ssp2 Tx", NULL, "codec_out1"},
143 	{"codec_in0", NULL, "ssp2 Rx" },
144 	{"codec_in1", NULL, "ssp2 Rx" },
145 	{"ssp2 Rx", NULL, "AIF1 Capture"},
146 	{"Headphone", NULL, "Platform Clock"},
147 	{"Headset Mic", NULL, "Platform Clock"},
148 	{"Int Mic", NULL, "Platform Clock"},
149 	{"Ext Spk", NULL, "Platform Clock"},
150 };
151 
152 static const struct snd_kcontrol_new cht_mc_controls[] = {
153 	SOC_DAPM_PIN_SWITCH("Headphone"),
154 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
155 	SOC_DAPM_PIN_SWITCH("Int Mic"),
156 	SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
157 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
158 };
159 
cht_aif1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)160 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
161 			     struct snd_pcm_hw_params *params)
162 {
163 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
164 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
165 	int ret;
166 
167 	/* set codec PLL source to the 19.2MHz platform clock (MCLK) */
168 	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
169 				  CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
170 	if (ret < 0) {
171 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
172 		return ret;
173 	}
174 
175 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
176 				params_rate(params) * 512, SND_SOC_CLOCK_IN);
177 	if (ret < 0) {
178 		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
179 		return ret;
180 	}
181 
182 	return 0;
183 }
184 
cht_codec_init(struct snd_soc_pcm_runtime * runtime)185 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
186 {
187 	int ret;
188 	int jack_type;
189 	struct snd_soc_codec *codec = runtime->codec;
190 	struct snd_soc_dai *codec_dai = runtime->codec_dai;
191 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
192 
193 	/* Select clk_i2s1_asrc as ASRC clock source */
194 	rt5645_sel_asrc_clk_src(codec,
195 				RT5645_DA_STEREO_FILTER |
196 				RT5645_DA_MONO_L_FILTER |
197 				RT5645_DA_MONO_R_FILTER |
198 				RT5645_AD_STEREO_FILTER,
199 				RT5645_CLK_SEL_I2S1_ASRC);
200 
201 	/* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
202 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
203 	if (ret < 0) {
204 		dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
205 		return ret;
206 	}
207 
208 	if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
209 		jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
210 					SND_JACK_BTN_0 | SND_JACK_BTN_1 |
211 					SND_JACK_BTN_2 | SND_JACK_BTN_3;
212 	else
213 		jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
214 
215 	ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
216 				    jack_type, &ctx->jack,
217 				    NULL, 0);
218 	if (ret) {
219 		dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
220 		return ret;
221 	}
222 
223 	rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
224 
225 	return ret;
226 }
227 
cht_codec_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)228 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
229 			    struct snd_pcm_hw_params *params)
230 {
231 	struct snd_interval *rate = hw_param_interval(params,
232 			SNDRV_PCM_HW_PARAM_RATE);
233 	struct snd_interval *channels = hw_param_interval(params,
234 						SNDRV_PCM_HW_PARAM_CHANNELS);
235 
236 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
237 	rate->min = rate->max = 48000;
238 	channels->min = channels->max = 2;
239 
240 	/* set SSP2 to 24-bit */
241 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
242 	return 0;
243 }
244 
cht_aif1_startup(struct snd_pcm_substream * substream)245 static int cht_aif1_startup(struct snd_pcm_substream *substream)
246 {
247 	return snd_pcm_hw_constraint_single(substream->runtime,
248 			SNDRV_PCM_HW_PARAM_RATE, 48000);
249 }
250 
251 static struct snd_soc_ops cht_aif1_ops = {
252 	.startup = cht_aif1_startup,
253 };
254 
255 static struct snd_soc_ops cht_be_ssp2_ops = {
256 	.hw_params = cht_aif1_hw_params,
257 };
258 
259 static struct snd_soc_dai_link cht_dailink[] = {
260 	[MERR_DPCM_AUDIO] = {
261 		.name = "Audio Port",
262 		.stream_name = "Audio",
263 		.cpu_dai_name = "media-cpu-dai",
264 		.codec_dai_name = "snd-soc-dummy-dai",
265 		.codec_name = "snd-soc-dummy",
266 		.platform_name = "sst-mfld-platform",
267 		.nonatomic = true,
268 		.dynamic = 1,
269 		.dpcm_playback = 1,
270 		.dpcm_capture = 1,
271 		.ops = &cht_aif1_ops,
272 	},
273 	[MERR_DPCM_COMPR] = {
274 		.name = "Compressed Port",
275 		.stream_name = "Compress",
276 		.cpu_dai_name = "compress-cpu-dai",
277 		.codec_dai_name = "snd-soc-dummy-dai",
278 		.codec_name = "snd-soc-dummy",
279 		.platform_name = "sst-mfld-platform",
280 	},
281 	/* CODEC<->CODEC link */
282 	/* back ends */
283 	{
284 		.name = "SSP2-Codec",
285 		.be_id = 1,
286 		.cpu_dai_name = "ssp2-port",
287 		.platform_name = "sst-mfld-platform",
288 		.no_pcm = 1,
289 		.codec_dai_name = "rt5645-aif1",
290 		.codec_name = "i2c-10EC5645:00",
291 		.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
292 					| SND_SOC_DAIFMT_CBS_CFS,
293 		.init = cht_codec_init,
294 		.be_hw_params_fixup = cht_codec_fixup,
295 		.nonatomic = true,
296 		.dpcm_playback = 1,
297 		.dpcm_capture = 1,
298 		.ops = &cht_be_ssp2_ops,
299 	},
300 };
301 
302 /* SoC card */
303 static struct snd_soc_card snd_soc_card_chtrt5645 = {
304 	.name = "chtrt5645",
305 	.owner = THIS_MODULE,
306 	.dai_link = cht_dailink,
307 	.num_links = ARRAY_SIZE(cht_dailink),
308 	.dapm_widgets = cht_dapm_widgets,
309 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
310 	.dapm_routes = cht_rt5645_audio_map,
311 	.num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
312 	.controls = cht_mc_controls,
313 	.num_controls = ARRAY_SIZE(cht_mc_controls),
314 };
315 
316 static struct snd_soc_card snd_soc_card_chtrt5650 = {
317 	.name = "chtrt5650",
318 	.owner = THIS_MODULE,
319 	.dai_link = cht_dailink,
320 	.num_links = ARRAY_SIZE(cht_dailink),
321 	.dapm_widgets = cht_dapm_widgets,
322 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
323 	.dapm_routes = cht_rt5650_audio_map,
324 	.num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
325 	.controls = cht_mc_controls,
326 	.num_controls = ARRAY_SIZE(cht_mc_controls),
327 };
328 
329 static struct cht_acpi_card snd_soc_cards[] = {
330 	{"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
331 	{"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
332 };
333 
snd_acpi_codec_match(acpi_handle handle,u32 level,void * context,void ** ret)334 static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
335 				       void *context, void **ret)
336 {
337 	*(bool *)context = true;
338 	return AE_OK;
339 }
340 
snd_cht_mc_probe(struct platform_device * pdev)341 static int snd_cht_mc_probe(struct platform_device *pdev)
342 {
343 	int ret_val = 0;
344 	int i;
345 	struct cht_mc_private *drv;
346 	struct snd_soc_card *card = snd_soc_cards[0].soc_card;
347 	bool found = false;
348 	char codec_name[16];
349 
350 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
351 	if (!drv)
352 		return -ENOMEM;
353 
354 	for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
355 		if (ACPI_SUCCESS(acpi_get_devices(
356 						snd_soc_cards[i].codec_id,
357 						snd_acpi_codec_match,
358 						&found, NULL)) && found) {
359 			dev_dbg(&pdev->dev,
360 				"found codec %s\n", snd_soc_cards[i].codec_id);
361 			card = snd_soc_cards[i].soc_card;
362 			drv->acpi_card = &snd_soc_cards[i];
363 			break;
364 		}
365 	}
366 	card->dev = &pdev->dev;
367 	sprintf(codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
368 	/* set correct codec name */
369 	strcpy((char *)card->dai_link[2].codec_name, codec_name);
370 	snd_soc_card_set_drvdata(card, drv);
371 	ret_val = devm_snd_soc_register_card(&pdev->dev, card);
372 	if (ret_val) {
373 		dev_err(&pdev->dev,
374 			"snd_soc_register_card failed %d\n", ret_val);
375 		return ret_val;
376 	}
377 	platform_set_drvdata(pdev, card);
378 	return ret_val;
379 }
380 
381 static struct platform_driver snd_cht_mc_driver = {
382 	.driver = {
383 		.name = "cht-bsw-rt5645",
384 	},
385 	.probe = snd_cht_mc_probe,
386 };
387 
388 module_platform_driver(snd_cht_mc_driver)
389 
390 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
391 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
392 MODULE_LICENSE("GPL v2");
393 MODULE_ALIAS("platform:cht-bsw-rt5645");
394