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/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/jack.h>
26#include "../../codecs/rt5670.h"
27#include "../atom/sst-atom-controls.h"
28
29/* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
30#define CHT_PLAT_CLK_3_HZ	19200000
31#define CHT_CODEC_DAI	"rt5670-aif1"
32
33static struct snd_soc_jack cht_bsw_headset;
34
35/* Headset jack detection DAPM pins */
36static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
37	{
38		.pin = "Headset Mic",
39		.mask = SND_JACK_MICROPHONE,
40	},
41	{
42		.pin = "Headphone",
43		.mask = SND_JACK_HEADPHONE,
44	},
45};
46
47static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
48{
49	int i;
50
51	for (i = 0; i < card->num_rtd; i++) {
52		struct snd_soc_pcm_runtime *rtd;
53
54		rtd = card->rtd + i;
55		if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
56			     strlen(CHT_CODEC_DAI)))
57			return rtd->codec_dai;
58	}
59	return NULL;
60}
61
62static int platform_clock_control(struct snd_soc_dapm_widget *w,
63		struct snd_kcontrol *k, int  event)
64{
65	struct snd_soc_dapm_context *dapm = w->dapm;
66	struct snd_soc_card *card = dapm->card;
67	struct snd_soc_dai *codec_dai;
68	int ret;
69
70	codec_dai = cht_get_codec_dai(card);
71	if (!codec_dai) {
72		dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
73		return -EIO;
74	}
75
76	if (SND_SOC_DAPM_EVENT_ON(event)) {
77		/* set codec PLL source to the 19.2MHz platform clock (MCLK) */
78		ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
79				CHT_PLAT_CLK_3_HZ, 48000 * 512);
80		if (ret < 0) {
81			dev_err(card->dev, "can't set codec pll: %d\n", ret);
82			return ret;
83		}
84
85		/* set codec sysclk source to PLL */
86		ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
87			48000 * 512, SND_SOC_CLOCK_IN);
88		if (ret < 0) {
89			dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
90			return ret;
91		}
92	} else {
93		/* Set codec sysclk source to its internal clock because codec
94		 * PLL will be off when idle and MCLK will also be off by ACPI
95		 * when codec is runtime suspended. Codec needs clock for jack
96		 * detection and button press.
97		 */
98		snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
99				       48000 * 512, SND_SOC_CLOCK_IN);
100	}
101	return 0;
102}
103
104static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
105	SND_SOC_DAPM_HP("Headphone", NULL),
106	SND_SOC_DAPM_MIC("Headset Mic", NULL),
107	SND_SOC_DAPM_MIC("Int Mic", NULL),
108	SND_SOC_DAPM_SPK("Ext Spk", NULL),
109	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
110			platform_clock_control, SND_SOC_DAPM_PRE_PMU |
111			SND_SOC_DAPM_POST_PMD),
112};
113
114static const struct snd_soc_dapm_route cht_audio_map[] = {
115	{"IN1P", NULL, "Headset Mic"},
116	{"IN1N", NULL, "Headset Mic"},
117	{"DMIC L1", NULL, "Int Mic"},
118	{"DMIC R1", NULL, "Int Mic"},
119	{"Headphone", NULL, "HPOL"},
120	{"Headphone", NULL, "HPOR"},
121	{"Ext Spk", NULL, "SPOLP"},
122	{"Ext Spk", NULL, "SPOLN"},
123	{"Ext Spk", NULL, "SPORP"},
124	{"Ext Spk", NULL, "SPORN"},
125	{"AIF1 Playback", NULL, "ssp2 Tx"},
126	{"ssp2 Tx", NULL, "codec_out0"},
127	{"ssp2 Tx", NULL, "codec_out1"},
128	{"codec_in0", NULL, "ssp2 Rx"},
129	{"codec_in1", NULL, "ssp2 Rx"},
130	{"ssp2 Rx", NULL, "AIF1 Capture"},
131	{"Headphone", NULL, "Platform Clock"},
132	{"Headset Mic", NULL, "Platform Clock"},
133	{"Int Mic", NULL, "Platform Clock"},
134	{"Ext Spk", NULL, "Platform Clock"},
135};
136
137static const struct snd_kcontrol_new cht_mc_controls[] = {
138	SOC_DAPM_PIN_SWITCH("Headphone"),
139	SOC_DAPM_PIN_SWITCH("Headset Mic"),
140	SOC_DAPM_PIN_SWITCH("Int Mic"),
141	SOC_DAPM_PIN_SWITCH("Ext Spk"),
142};
143
144static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
145					struct snd_pcm_hw_params *params)
146{
147	struct snd_soc_pcm_runtime *rtd = substream->private_data;
148	struct snd_soc_dai *codec_dai = rtd->codec_dai;
149	int ret;
150
151	/* set codec PLL source to the 19.2MHz platform clock (MCLK) */
152	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
153				  CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
154	if (ret < 0) {
155		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
156		return ret;
157	}
158
159	/* set codec sysclk source to PLL */
160	ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
161				     params_rate(params) * 512,
162				     SND_SOC_CLOCK_IN);
163	if (ret < 0) {
164		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
165		return ret;
166	}
167	return 0;
168}
169
170static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
171{
172	int ret;
173	struct snd_soc_dai *codec_dai = runtime->codec_dai;
174	struct snd_soc_codec *codec = codec_dai->codec;
175
176	/* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
177	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
178	if (ret < 0) {
179		dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
180		return ret;
181	}
182
183	/* Select codec ASRC clock source to track I2S1 clock, because codec
184	 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
185	 * be supported by RT5672. Otherwise, ASRC will be disabled and cause
186	 * noise.
187	 */
188	rt5670_sel_asrc_clk_src(codec,
189				RT5670_DA_STEREO_FILTER
190				| RT5670_DA_MONO_L_FILTER
191				| RT5670_DA_MONO_R_FILTER
192				| RT5670_AD_STEREO_FILTER
193				| RT5670_AD_MONO_L_FILTER
194				| RT5670_AD_MONO_R_FILTER,
195				RT5670_CLK_SEL_I2S1_ASRC);
196
197        ret = snd_soc_card_jack_new(runtime->card, "Headset",
198                SND_JACK_HEADSET | SND_JACK_BTN_0 |
199                SND_JACK_BTN_1 | SND_JACK_BTN_2, &cht_bsw_headset,
200                cht_bsw_headset_pins, ARRAY_SIZE(cht_bsw_headset_pins));
201        if (ret)
202                return ret;
203
204	rt5670_set_jack_detect(codec, &cht_bsw_headset);
205	return 0;
206}
207
208static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
209			    struct snd_pcm_hw_params *params)
210{
211	struct snd_interval *rate = hw_param_interval(params,
212			SNDRV_PCM_HW_PARAM_RATE);
213	struct snd_interval *channels = hw_param_interval(params,
214						SNDRV_PCM_HW_PARAM_CHANNELS);
215
216	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
217	rate->min = rate->max = 48000;
218	channels->min = channels->max = 2;
219
220	/* set SSP2 to 24-bit */
221	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
222	return 0;
223}
224
225static int cht_aif1_startup(struct snd_pcm_substream *substream)
226{
227	return snd_pcm_hw_constraint_single(substream->runtime,
228			SNDRV_PCM_HW_PARAM_RATE, 48000);
229}
230
231static struct snd_soc_ops cht_aif1_ops = {
232	.startup = cht_aif1_startup,
233};
234
235static struct snd_soc_ops cht_be_ssp2_ops = {
236	.hw_params = cht_aif1_hw_params,
237};
238
239static struct snd_soc_dai_link cht_dailink[] = {
240	/* Front End DAI links */
241	[MERR_DPCM_AUDIO] = {
242		.name = "Audio Port",
243		.stream_name = "Audio",
244		.cpu_dai_name = "media-cpu-dai",
245		.codec_dai_name = "snd-soc-dummy-dai",
246		.codec_name = "snd-soc-dummy",
247		.platform_name = "sst-mfld-platform",
248		.nonatomic = true,
249		.dynamic = 1,
250		.dpcm_playback = 1,
251		.dpcm_capture = 1,
252		.ops = &cht_aif1_ops,
253	},
254	[MERR_DPCM_COMPR] = {
255		.name = "Compressed Port",
256		.stream_name = "Compress",
257		.cpu_dai_name = "compress-cpu-dai",
258		.codec_dai_name = "snd-soc-dummy-dai",
259		.codec_name = "snd-soc-dummy",
260		.platform_name = "sst-mfld-platform",
261	},
262
263	/* Back End DAI links */
264	{
265		/* SSP2 - Codec */
266		.name = "SSP2-Codec",
267		.be_id = 1,
268		.cpu_dai_name = "ssp2-port",
269		.platform_name = "sst-mfld-platform",
270		.no_pcm = 1,
271		.nonatomic = true,
272		.codec_dai_name = "rt5670-aif1",
273		.codec_name = "i2c-10EC5670:00",
274		.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
275					| SND_SOC_DAIFMT_CBS_CFS,
276		.init = cht_codec_init,
277		.be_hw_params_fixup = cht_codec_fixup,
278		.dpcm_playback = 1,
279		.dpcm_capture = 1,
280		.ops = &cht_be_ssp2_ops,
281	},
282};
283
284static int cht_suspend_pre(struct snd_soc_card *card)
285{
286	struct snd_soc_codec *codec;
287
288	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
289		if (!strcmp(codec->component.name, "i2c-10EC5670:00")) {
290			dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n");
291			rt5670_jack_suspend(codec);
292			break;
293		}
294	}
295	return 0;
296}
297
298static int cht_resume_post(struct snd_soc_card *card)
299{
300	struct snd_soc_codec *codec;
301
302	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
303		if (!strcmp(codec->component.name, "i2c-10EC5670:00")) {
304			dev_dbg(codec->dev, "enabling jack detect for resume.\n");
305			rt5670_jack_resume(codec);
306			break;
307		}
308	}
309
310	return 0;
311}
312
313/* SoC card */
314static struct snd_soc_card snd_soc_card_cht = {
315	.name = "cherrytrailcraudio",
316	.owner = THIS_MODULE,
317	.dai_link = cht_dailink,
318	.num_links = ARRAY_SIZE(cht_dailink),
319	.dapm_widgets = cht_dapm_widgets,
320	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
321	.dapm_routes = cht_audio_map,
322	.num_dapm_routes = ARRAY_SIZE(cht_audio_map),
323	.controls = cht_mc_controls,
324	.num_controls = ARRAY_SIZE(cht_mc_controls),
325	.suspend_pre = cht_suspend_pre,
326	.resume_post = cht_resume_post,
327};
328
329static int snd_cht_mc_probe(struct platform_device *pdev)
330{
331	int ret_val = 0;
332
333	/* register the soc card */
334	snd_soc_card_cht.dev = &pdev->dev;
335	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
336	if (ret_val) {
337		dev_err(&pdev->dev,
338			"snd_soc_register_card failed %d\n", ret_val);
339		return ret_val;
340	}
341	platform_set_drvdata(pdev, &snd_soc_card_cht);
342	return ret_val;
343}
344
345static struct platform_driver snd_cht_mc_driver = {
346	.driver = {
347		.name = "cht-bsw-rt5672",
348	},
349	.probe = snd_cht_mc_probe,
350};
351
352module_platform_driver(snd_cht_mc_driver);
353
354MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
355MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
356MODULE_LICENSE("GPL v2");
357MODULE_ALIAS("platform:cht-bsw-rt5672");
358