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