1 /*
2  * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
3  *
4  * Copyright (c) 2014, The Chromium OS Authors.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/delay.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
26 #include <sound/pcm_params.h>
27 #include <sound/jack.h>
28 
29 #include "../common/sst-dsp.h"
30 #include "../haswell/sst-haswell-ipc.h"
31 
32 #include "../../codecs/rt5677.h"
33 
34 struct bdw_rt5677_priv {
35 	struct gpio_desc *gpio_hp_en;
36 	struct snd_soc_codec *codec;
37 };
38 
39 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
40 			struct snd_kcontrol *k, int event)
41 {
42 	struct snd_soc_dapm_context *dapm = w->dapm;
43 	struct snd_soc_card *card = dapm->card;
44 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
45 
46 	if (SND_SOC_DAPM_EVENT_ON(event))
47 		msleep(70);
48 
49 	gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
50 		SND_SOC_DAPM_EVENT_ON(event));
51 
52 	return 0;
53 }
54 
55 static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
56 	SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
57 	SND_SOC_DAPM_SPK("Speaker", NULL),
58 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
59 	SND_SOC_DAPM_MIC("Local DMICs", NULL),
60 	SND_SOC_DAPM_MIC("Remote DMICs", NULL),
61 };
62 
63 static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
64 	/* Speakers */
65 	{"Speaker", NULL, "PDM1L"},
66 	{"Speaker", NULL, "PDM1R"},
67 
68 	/* Headset jack connectors */
69 	{"Headphone", NULL, "LOUT1"},
70 	{"Headphone", NULL, "LOUT2"},
71 	{"IN1P", NULL, "Headset Mic"},
72 	{"IN1N", NULL, "Headset Mic"},
73 
74 	/* Digital MICs
75 	 * Local DMICs: the two DMICs on the mainboard
76 	 * Remote DMICs: the two DMICs on the camera module
77 	 */
78 	{"DMIC L1", NULL, "Remote DMICs"},
79 	{"DMIC R1", NULL, "Remote DMICs"},
80 	{"DMIC L2", NULL, "Local DMICs"},
81 	{"DMIC R2", NULL, "Local DMICs"},
82 
83 	/* CODEC BE connections */
84 	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
85 	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
86 };
87 
88 static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
89 	SOC_DAPM_PIN_SWITCH("Speaker"),
90 	SOC_DAPM_PIN_SWITCH("Headphone"),
91 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
92 	SOC_DAPM_PIN_SWITCH("Local DMICs"),
93 	SOC_DAPM_PIN_SWITCH("Remote DMICs"),
94 };
95 
96 
97 static struct snd_soc_jack headphone_jack;
98 static struct snd_soc_jack mic_jack;
99 
100 static struct snd_soc_jack_pin headphone_jack_pin = {
101 	.pin	= "Headphone",
102 	.mask	= SND_JACK_HEADPHONE,
103 };
104 
105 static struct snd_soc_jack_pin mic_jack_pin = {
106 	.pin	= "Headset Mic",
107 	.mask	= SND_JACK_MICROPHONE,
108 };
109 
110 static struct snd_soc_jack_gpio headphone_jack_gpio = {
111 	.name			= "plug-det",
112 	.report			= SND_JACK_HEADPHONE,
113 	.debounce_time		= 200,
114 };
115 
116 static struct snd_soc_jack_gpio mic_jack_gpio = {
117 	.name			= "mic-present",
118 	.report			= SND_JACK_MICROPHONE,
119 	.debounce_time		= 200,
120 	.invert			= 1,
121 };
122 
123 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
124 			struct snd_pcm_hw_params *params)
125 {
126 	struct snd_interval *rate = hw_param_interval(params,
127 			SNDRV_PCM_HW_PARAM_RATE);
128 	struct snd_interval *channels = hw_param_interval(params,
129 						SNDRV_PCM_HW_PARAM_CHANNELS);
130 
131 	/* The ADSP will covert the FE rate to 48k, stereo */
132 	rate->min = rate->max = 48000;
133 	channels->min = channels->max = 2;
134 
135 	/* set SSP0 to 16 bit */
136 	snd_mask_set(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT -
137 				    SNDRV_PCM_HW_PARAM_FIRST_MASK],
138 				    SNDRV_PCM_FORMAT_S16_LE);
139 	return 0;
140 }
141 
142 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
143 	struct snd_pcm_hw_params *params)
144 {
145 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
147 	int ret;
148 
149 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
150 		SND_SOC_CLOCK_IN);
151 	if (ret < 0) {
152 		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
153 		return ret;
154 	}
155 
156 	return ret;
157 }
158 
159 static const struct snd_soc_ops bdw_rt5677_ops = {
160 	.hw_params = bdw_rt5677_hw_params,
161 };
162 
163 static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
164 {
165 	struct sst_pdata *pdata = dev_get_platdata(rtd->platform->dev);
166 	struct sst_hsw *broadwell = pdata->dsp;
167 	int ret;
168 
169 	/* Set ADSP SSP port settings */
170 	ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
171 		SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
172 		SST_HSW_DEVICE_CLOCK_MASTER, 9);
173 	if (ret < 0) {
174 		dev_err(rtd->dev, "error: failed to set device config\n");
175 		return ret;
176 	}
177 
178 	return 0;
179 }
180 
181 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
182 {
183 	struct bdw_rt5677_priv *bdw_rt5677 =
184 			snd_soc_card_get_drvdata(rtd->card);
185 	struct snd_soc_codec *codec = rtd->codec;
186 	struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
187 
188 	/* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
189 	 * The ASRC clock source is clk_i2s1_asrc.
190 	 */
191 	rt5677_sel_asrc_clk_src(codec, RT5677_DA_STEREO_FILTER |
192 			RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
193 			RT5677_CLK_SEL_I2S1_ASRC);
194 
195 	/* Request rt5677 GPIO for headphone amp control */
196 	bdw_rt5677->gpio_hp_en = devm_gpiod_get(codec->dev, "headphone-enable",
197 						GPIOD_OUT_LOW);
198 	if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
199 		dev_err(codec->dev, "Can't find HP_AMP_SHDN_L gpio\n");
200 		return PTR_ERR(bdw_rt5677->gpio_hp_en);
201 	}
202 
203 	/* Create and initialize headphone jack */
204 	if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
205 			SND_JACK_HEADPHONE, &headphone_jack,
206 			&headphone_jack_pin, 1)) {
207 		headphone_jack_gpio.gpiod_dev = codec->dev;
208 		if (snd_soc_jack_add_gpios(&headphone_jack, 1,
209 				&headphone_jack_gpio))
210 			dev_err(codec->dev, "Can't add headphone jack gpio\n");
211 	} else {
212 		dev_err(codec->dev, "Can't create headphone jack\n");
213 	}
214 
215 	/* Create and initialize mic jack */
216 	if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
217 			SND_JACK_MICROPHONE, &mic_jack,
218 			&mic_jack_pin, 1)) {
219 		mic_jack_gpio.gpiod_dev = codec->dev;
220 		if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
221 			dev_err(codec->dev, "Can't add mic jack gpio\n");
222 	} else {
223 		dev_err(codec->dev, "Can't create mic jack\n");
224 	}
225 	bdw_rt5677->codec = codec;
226 
227 	snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
228 	return 0;
229 }
230 
231 /* broadwell digital audio interface glue - connects codec <--> CPU */
232 static struct snd_soc_dai_link bdw_rt5677_dais[] = {
233 	/* Front End DAI links */
234 	{
235 		.name = "System PCM",
236 		.stream_name = "System Playback/Capture",
237 		.cpu_dai_name = "System Pin",
238 		.platform_name = "haswell-pcm-audio",
239 		.dynamic = 1,
240 		.codec_name = "snd-soc-dummy",
241 		.codec_dai_name = "snd-soc-dummy-dai",
242 		.init = bdw_rt5677_rtd_init,
243 		.trigger = {
244 			SND_SOC_DPCM_TRIGGER_POST,
245 			SND_SOC_DPCM_TRIGGER_POST
246 		},
247 		.dpcm_capture = 1,
248 		.dpcm_playback = 1,
249 	},
250 
251 	/* Back End DAI links */
252 	{
253 		/* SSP0 - Codec */
254 		.name = "Codec",
255 		.id = 0,
256 		.cpu_dai_name = "snd-soc-dummy-dai",
257 		.platform_name = "snd-soc-dummy",
258 		.no_pcm = 1,
259 		.codec_name = "i2c-RT5677CE:00",
260 		.codec_dai_name = "rt5677-aif1",
261 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
262 			SND_SOC_DAIFMT_CBS_CFS,
263 		.ignore_suspend = 1,
264 		.ignore_pmdown_time = 1,
265 		.be_hw_params_fixup = broadwell_ssp0_fixup,
266 		.ops = &bdw_rt5677_ops,
267 		.dpcm_playback = 1,
268 		.dpcm_capture = 1,
269 		.init = bdw_rt5677_init,
270 	},
271 };
272 
273 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
274 {
275 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
276 	struct snd_soc_dapm_context *dapm;
277 
278 	if (bdw_rt5677->codec) {
279 		dapm = snd_soc_codec_get_dapm(bdw_rt5677->codec);
280 		snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
281 	}
282 	return 0;
283 }
284 
285 static int bdw_rt5677_resume_post(struct snd_soc_card *card)
286 {
287 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
288 	struct snd_soc_dapm_context *dapm;
289 
290 	if (bdw_rt5677->codec) {
291 		dapm = snd_soc_codec_get_dapm(bdw_rt5677->codec);
292 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
293 	}
294 	return 0;
295 }
296 
297 /* ASoC machine driver for Broadwell DSP + RT5677 */
298 static struct snd_soc_card bdw_rt5677_card = {
299 	.name = "bdw-rt5677",
300 	.owner = THIS_MODULE,
301 	.dai_link = bdw_rt5677_dais,
302 	.num_links = ARRAY_SIZE(bdw_rt5677_dais),
303 	.dapm_widgets = bdw_rt5677_widgets,
304 	.num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
305 	.dapm_routes = bdw_rt5677_map,
306 	.num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
307 	.controls = bdw_rt5677_controls,
308 	.num_controls = ARRAY_SIZE(bdw_rt5677_controls),
309 	.fully_routed = true,
310 	.suspend_pre = bdw_rt5677_suspend_pre,
311 	.resume_post = bdw_rt5677_resume_post,
312 };
313 
314 static int bdw_rt5677_probe(struct platform_device *pdev)
315 {
316 	struct bdw_rt5677_priv *bdw_rt5677;
317 
318 	bdw_rt5677_card.dev = &pdev->dev;
319 
320 	/* Allocate driver private struct */
321 	bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
322 		GFP_KERNEL);
323 	if (!bdw_rt5677) {
324 		dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
325 		return -ENOMEM;
326 	}
327 
328 	snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
329 
330 	return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
331 }
332 
333 static struct platform_driver bdw_rt5677_audio = {
334 	.probe = bdw_rt5677_probe,
335 	.driver = {
336 		.name = "bdw-rt5677",
337 	},
338 };
339 
340 module_platform_driver(bdw_rt5677_audio)
341 
342 /* Module information */
343 MODULE_AUTHOR("Ben Zhang");
344 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
345 MODULE_LICENSE("GPL v2");
346 MODULE_ALIAS("platform:bdw-rt5677");
347