1 /*
2  *  cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
3  *                     Cherrytrail and Braswell, with RT5672 codec.
4  *
5  *  Copyright (C) 2014 Intel Corp
6  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7  *          Mengdong Lin <mengdong.lin@intel.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  */
18 
19 #include <linux/input.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/jack.h>
28 #include <sound/soc-acpi.h>
29 #include "../../codecs/rt5670.h"
30 #include "../atom/sst-atom-controls.h"
31 
32 
33 /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
34 #define CHT_PLAT_CLK_3_HZ	19200000
35 #define CHT_CODEC_DAI	"rt5670-aif1"
36 
37 struct cht_mc_private {
38 	struct snd_soc_jack headset;
39 	char codec_name[SND_ACPI_I2C_ID_LEN];
40 	struct clk *mclk;
41 };
42 
43 /* Headset jack detection DAPM pins */
44 static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
45 	{
46 		.pin = "Headset Mic",
47 		.mask = SND_JACK_MICROPHONE,
48 	},
49 	{
50 		.pin = "Headphone",
51 		.mask = SND_JACK_HEADPHONE,
52 	},
53 };
54 
55 static int platform_clock_control(struct snd_soc_dapm_widget *w,
56 		struct snd_kcontrol *k, int  event)
57 {
58 	struct snd_soc_dapm_context *dapm = w->dapm;
59 	struct snd_soc_card *card = dapm->card;
60 	struct snd_soc_dai *codec_dai;
61 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
62 	int ret;
63 
64 	codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
65 	if (!codec_dai) {
66 		dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
67 		return -EIO;
68 	}
69 
70 	if (SND_SOC_DAPM_EVENT_ON(event)) {
71 		if (ctx->mclk) {
72 			ret = clk_prepare_enable(ctx->mclk);
73 			if (ret < 0) {
74 				dev_err(card->dev,
75 					"could not configure MCLK state");
76 				return ret;
77 			}
78 		}
79 
80 		/* set codec PLL source to the 19.2MHz platform clock (MCLK) */
81 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
82 				CHT_PLAT_CLK_3_HZ, 48000 * 512);
83 		if (ret < 0) {
84 			dev_err(card->dev, "can't set codec pll: %d\n", ret);
85 			return ret;
86 		}
87 
88 		/* set codec sysclk source to PLL */
89 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
90 			48000 * 512, SND_SOC_CLOCK_IN);
91 		if (ret < 0) {
92 			dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
93 			return ret;
94 		}
95 	} else {
96 		/* Set codec sysclk source to its internal clock because codec
97 		 * PLL will be off when idle and MCLK will also be off by ACPI
98 		 * when codec is runtime suspended. Codec needs clock for jack
99 		 * detection and button press.
100 		 */
101 		snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
102 				       48000 * 512, SND_SOC_CLOCK_IN);
103 
104 		if (ctx->mclk)
105 			clk_disable_unprepare(ctx->mclk);
106 	}
107 	return 0;
108 }
109 
110 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
111 	SND_SOC_DAPM_HP("Headphone", NULL),
112 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
113 	SND_SOC_DAPM_MIC("Int Mic", NULL),
114 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
115 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
116 			platform_clock_control, SND_SOC_DAPM_PRE_PMU |
117 			SND_SOC_DAPM_POST_PMD),
118 };
119 
120 static const struct snd_soc_dapm_route cht_audio_map[] = {
121 	{"IN1P", NULL, "Headset Mic"},
122 	{"IN1N", NULL, "Headset Mic"},
123 	{"DMIC L1", NULL, "Int Mic"},
124 	{"DMIC R1", NULL, "Int Mic"},
125 	{"Headphone", NULL, "HPOL"},
126 	{"Headphone", NULL, "HPOR"},
127 	{"Ext Spk", NULL, "SPOLP"},
128 	{"Ext Spk", NULL, "SPOLN"},
129 	{"Ext Spk", NULL, "SPORP"},
130 	{"Ext Spk", NULL, "SPORN"},
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 int cht_aif1_hw_params(struct snd_pcm_substream *substream,
151 					struct snd_pcm_hw_params *params)
152 {
153 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
154 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
155 	int ret;
156 
157 	/* set codec PLL source to the 19.2MHz platform clock (MCLK) */
158 	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
159 				  CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
160 	if (ret < 0) {
161 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
162 		return ret;
163 	}
164 
165 	/* set codec sysclk source to PLL */
166 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
167 				     params_rate(params) * 512,
168 				     SND_SOC_CLOCK_IN);
169 	if (ret < 0) {
170 		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
171 		return ret;
172 	}
173 	return 0;
174 }
175 
176 static const struct acpi_gpio_params headset_gpios = { 0, 0, false };
177 
178 static const struct acpi_gpio_mapping cht_rt5672_gpios[] = {
179 	{ "headset-gpios", &headset_gpios, 1 },
180 	{},
181 };
182 
183 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
184 {
185 	int ret;
186 	struct snd_soc_dai *codec_dai = runtime->codec_dai;
187 	struct snd_soc_component *component = codec_dai->component;
188 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
189 
190 	if (devm_acpi_dev_add_driver_gpios(component->dev, cht_rt5672_gpios))
191 		dev_warn(runtime->dev, "Unable to add GPIO mapping table\n");
192 
193 	/* Select codec ASRC clock source to track I2S1 clock, because codec
194 	 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
195 	 * be supported by RT5672. Otherwise, ASRC will be disabled and cause
196 	 * noise.
197 	 */
198 	rt5670_sel_asrc_clk_src(component,
199 				RT5670_DA_STEREO_FILTER
200 				| RT5670_DA_MONO_L_FILTER
201 				| RT5670_DA_MONO_R_FILTER
202 				| RT5670_AD_STEREO_FILTER
203 				| RT5670_AD_MONO_L_FILTER
204 				| RT5670_AD_MONO_R_FILTER,
205 				RT5670_CLK_SEL_I2S1_ASRC);
206 
207         ret = snd_soc_card_jack_new(runtime->card, "Headset",
208 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
209 				    SND_JACK_BTN_1 | SND_JACK_BTN_2,
210 				    &ctx->headset,
211 				    cht_bsw_headset_pins,
212 				    ARRAY_SIZE(cht_bsw_headset_pins));
213         if (ret)
214                 return ret;
215 
216 	snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
217 	snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
218 	snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
219 
220 	rt5670_set_jack_detect(component, &ctx->headset);
221 	if (ctx->mclk) {
222 		/*
223 		 * The firmware might enable the clock at
224 		 * boot (this information may or may not
225 		 * be reflected in the enable clock register).
226 		 * To change the rate we must disable the clock
227 		 * first to cover these cases. Due to common
228 		 * clock framework restrictions that do not allow
229 		 * to disable a clock that has not been enabled,
230 		 * we need to enable the clock first.
231 		 */
232 		ret = clk_prepare_enable(ctx->mclk);
233 		if (!ret)
234 			clk_disable_unprepare(ctx->mclk);
235 
236 		ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
237 
238 		if (ret) {
239 			dev_err(runtime->dev, "unable to set MCLK rate\n");
240 			return ret;
241 		}
242 	}
243 	return 0;
244 }
245 
246 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
247 			    struct snd_pcm_hw_params *params)
248 {
249 	struct snd_interval *rate = hw_param_interval(params,
250 			SNDRV_PCM_HW_PARAM_RATE);
251 	struct snd_interval *channels = hw_param_interval(params,
252 						SNDRV_PCM_HW_PARAM_CHANNELS);
253 	int ret;
254 
255 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
256 	rate->min = rate->max = 48000;
257 	channels->min = channels->max = 2;
258 
259 	/* set SSP2 to 24-bit */
260 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
261 
262 	/*
263 	 * Default mode for SSP configuration is TDM 4 slot
264 	 */
265 	ret = snd_soc_dai_set_fmt(rtd->codec_dai,
266 				  SND_SOC_DAIFMT_DSP_B |
267 				  SND_SOC_DAIFMT_IB_NF |
268 				  SND_SOC_DAIFMT_CBS_CFS);
269 	if (ret < 0) {
270 		dev_err(rtd->dev, "can't set format to TDM %d\n", ret);
271 		return ret;
272 	}
273 
274 	/* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
275 	ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0xF, 0xF, 4, 24);
276 	if (ret < 0) {
277 		dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
278 		return ret;
279 	}
280 
281 	return 0;
282 }
283 
284 static int cht_aif1_startup(struct snd_pcm_substream *substream)
285 {
286 	return snd_pcm_hw_constraint_single(substream->runtime,
287 			SNDRV_PCM_HW_PARAM_RATE, 48000);
288 }
289 
290 static const struct snd_soc_ops cht_aif1_ops = {
291 	.startup = cht_aif1_startup,
292 };
293 
294 static const struct snd_soc_ops cht_be_ssp2_ops = {
295 	.hw_params = cht_aif1_hw_params,
296 };
297 
298 static struct snd_soc_dai_link cht_dailink[] = {
299 	/* Front End DAI links */
300 	[MERR_DPCM_AUDIO] = {
301 		.name = "Audio Port",
302 		.stream_name = "Audio",
303 		.cpu_dai_name = "media-cpu-dai",
304 		.codec_dai_name = "snd-soc-dummy-dai",
305 		.codec_name = "snd-soc-dummy",
306 		.platform_name = "sst-mfld-platform",
307 		.nonatomic = true,
308 		.dynamic = 1,
309 		.dpcm_playback = 1,
310 		.dpcm_capture = 1,
311 		.ops = &cht_aif1_ops,
312 	},
313 	[MERR_DPCM_DEEP_BUFFER] = {
314 		.name = "Deep-Buffer Audio Port",
315 		.stream_name = "Deep-Buffer Audio",
316 		.cpu_dai_name = "deepbuffer-cpu-dai",
317 		.codec_dai_name = "snd-soc-dummy-dai",
318 		.codec_name = "snd-soc-dummy",
319 		.platform_name = "sst-mfld-platform",
320 		.nonatomic = true,
321 		.dynamic = 1,
322 		.dpcm_playback = 1,
323 		.ops = &cht_aif1_ops,
324 	},
325 
326 	/* Back End DAI links */
327 	{
328 		/* SSP2 - Codec */
329 		.name = "SSP2-Codec",
330 		.id = 0,
331 		.cpu_dai_name = "ssp2-port",
332 		.platform_name = "sst-mfld-platform",
333 		.no_pcm = 1,
334 		.nonatomic = true,
335 		.codec_dai_name = "rt5670-aif1",
336 		.codec_name = "i2c-10EC5670:00",
337 		.init = cht_codec_init,
338 		.be_hw_params_fixup = cht_codec_fixup,
339 		.dpcm_playback = 1,
340 		.dpcm_capture = 1,
341 		.ops = &cht_be_ssp2_ops,
342 	},
343 };
344 
345 static int cht_suspend_pre(struct snd_soc_card *card)
346 {
347 	struct snd_soc_component *component;
348 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
349 
350 	for_each_card_components(card, component) {
351 		if (!strncmp(component->name,
352 			     ctx->codec_name, sizeof(ctx->codec_name))) {
353 
354 			dev_dbg(component->dev, "disabling jack detect before going to suspend.\n");
355 			rt5670_jack_suspend(component);
356 			break;
357 		}
358 	}
359 	return 0;
360 }
361 
362 static int cht_resume_post(struct snd_soc_card *card)
363 {
364 	struct snd_soc_component *component;
365 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
366 
367 	for_each_card_components(card, component) {
368 		if (!strncmp(component->name,
369 			     ctx->codec_name, sizeof(ctx->codec_name))) {
370 
371 			dev_dbg(component->dev, "enabling jack detect for resume.\n");
372 			rt5670_jack_resume(component);
373 			break;
374 		}
375 	}
376 
377 	return 0;
378 }
379 
380 /* SoC card */
381 static struct snd_soc_card snd_soc_card_cht = {
382 	.name = "cht-bsw-rt5672",
383 	.owner = THIS_MODULE,
384 	.dai_link = cht_dailink,
385 	.num_links = ARRAY_SIZE(cht_dailink),
386 	.dapm_widgets = cht_dapm_widgets,
387 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
388 	.dapm_routes = cht_audio_map,
389 	.num_dapm_routes = ARRAY_SIZE(cht_audio_map),
390 	.controls = cht_mc_controls,
391 	.num_controls = ARRAY_SIZE(cht_mc_controls),
392 	.suspend_pre = cht_suspend_pre,
393 	.resume_post = cht_resume_post,
394 };
395 
396 #define RT5672_I2C_DEFAULT	"i2c-10EC5670:00"
397 
398 static int snd_cht_mc_probe(struct platform_device *pdev)
399 {
400 	int ret_val = 0;
401 	struct cht_mc_private *drv;
402 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
403 	const char *i2c_name;
404 	int i;
405 
406 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
407 	if (!drv)
408 		return -ENOMEM;
409 
410 	strcpy(drv->codec_name, RT5672_I2C_DEFAULT);
411 
412 	/* fixup codec name based on HID */
413 	if (mach) {
414 		i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
415 		if (i2c_name) {
416 			snprintf(drv->codec_name, sizeof(drv->codec_name),
417 				 "i2c-%s", i2c_name);
418 			for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
419 				if (!strcmp(cht_dailink[i].codec_name,
420 					    RT5672_I2C_DEFAULT)) {
421 					cht_dailink[i].codec_name =
422 						drv->codec_name;
423 					break;
424 				}
425 			}
426 		}
427 	}
428 
429 	drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
430 	if (IS_ERR(drv->mclk)) {
431 		dev_err(&pdev->dev,
432 			"Failed to get MCLK from pmc_plt_clk_3: %ld\n",
433 			PTR_ERR(drv->mclk));
434 		return PTR_ERR(drv->mclk);
435 	}
436 	snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
437 
438 	/* register the soc card */
439 	snd_soc_card_cht.dev = &pdev->dev;
440 	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
441 	if (ret_val) {
442 		dev_err(&pdev->dev,
443 			"snd_soc_register_card failed %d\n", ret_val);
444 		return ret_val;
445 	}
446 	platform_set_drvdata(pdev, &snd_soc_card_cht);
447 	return ret_val;
448 }
449 
450 static struct platform_driver snd_cht_mc_driver = {
451 	.driver = {
452 		.name = "cht-bsw-rt5672",
453 	},
454 	.probe = snd_cht_mc_probe,
455 };
456 
457 module_platform_driver(snd_cht_mc_driver);
458 
459 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
460 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
461 MODULE_LICENSE("GPL v2");
462 MODULE_ALIAS("platform:cht-bsw-rt5672");
463