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/acpi.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/delay.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/soc.h>
27 #include <sound/pcm_params.h>
28 #include <sound/jack.h>
29 #include <sound/soc-acpi.h>
30 
31 #include "../common/sst-dsp.h"
32 #include "../haswell/sst-haswell-ipc.h"
33 
34 #include "../../codecs/rt5677.h"
35 
36 struct bdw_rt5677_priv {
37 	struct gpio_desc *gpio_hp_en;
38 	struct snd_soc_component *component;
39 };
40 
41 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
42 			struct snd_kcontrol *k, int event)
43 {
44 	struct snd_soc_dapm_context *dapm = w->dapm;
45 	struct snd_soc_card *card = dapm->card;
46 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
47 
48 	if (SND_SOC_DAPM_EVENT_ON(event))
49 		msleep(70);
50 
51 	gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
52 		SND_SOC_DAPM_EVENT_ON(event));
53 
54 	return 0;
55 }
56 
57 static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
58 	SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
59 	SND_SOC_DAPM_SPK("Speaker", NULL),
60 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
61 	SND_SOC_DAPM_MIC("Local DMICs", NULL),
62 	SND_SOC_DAPM_MIC("Remote DMICs", NULL),
63 };
64 
65 static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
66 	/* Speakers */
67 	{"Speaker", NULL, "PDM1L"},
68 	{"Speaker", NULL, "PDM1R"},
69 
70 	/* Headset jack connectors */
71 	{"Headphone", NULL, "LOUT1"},
72 	{"Headphone", NULL, "LOUT2"},
73 	{"IN1P", NULL, "Headset Mic"},
74 	{"IN1N", NULL, "Headset Mic"},
75 
76 	/* Digital MICs
77 	 * Local DMICs: the two DMICs on the mainboard
78 	 * Remote DMICs: the two DMICs on the camera module
79 	 */
80 	{"DMIC L1", NULL, "Remote DMICs"},
81 	{"DMIC R1", NULL, "Remote DMICs"},
82 	{"DMIC L2", NULL, "Local DMICs"},
83 	{"DMIC R2", NULL, "Local DMICs"},
84 
85 	/* CODEC BE connections */
86 	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
87 	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
88 };
89 
90 static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
91 	SOC_DAPM_PIN_SWITCH("Speaker"),
92 	SOC_DAPM_PIN_SWITCH("Headphone"),
93 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
94 	SOC_DAPM_PIN_SWITCH("Local DMICs"),
95 	SOC_DAPM_PIN_SWITCH("Remote DMICs"),
96 };
97 
98 
99 static struct snd_soc_jack headphone_jack;
100 static struct snd_soc_jack mic_jack;
101 
102 static struct snd_soc_jack_pin headphone_jack_pin = {
103 	.pin	= "Headphone",
104 	.mask	= SND_JACK_HEADPHONE,
105 };
106 
107 static struct snd_soc_jack_pin mic_jack_pin = {
108 	.pin	= "Headset Mic",
109 	.mask	= SND_JACK_MICROPHONE,
110 };
111 
112 static struct snd_soc_jack_gpio headphone_jack_gpio = {
113 	.name			= "plug-det",
114 	.report			= SND_JACK_HEADPHONE,
115 	.debounce_time		= 200,
116 };
117 
118 static struct snd_soc_jack_gpio mic_jack_gpio = {
119 	.name			= "mic-present",
120 	.report			= SND_JACK_MICROPHONE,
121 	.debounce_time		= 200,
122 	.invert			= 1,
123 };
124 
125 /* GPIO indexes defined by ACPI */
126 enum {
127 	RT5677_GPIO_PLUG_DET		= 0,
128 	RT5677_GPIO_MIC_PRESENT_L	= 1,
129 	RT5677_GPIO_HOTWORD_DET_L	= 2,
130 	RT5677_GPIO_DSP_INT		= 3,
131 	RT5677_GPIO_HP_AMP_SHDN_L	= 4,
132 };
133 
134 static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
135 static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
136 static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
137 
138 static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
139 	{ "plug-det-gpios", &plug_det_gpio, 1 },
140 	{ "mic-present-gpios", &mic_present_gpio, 1 },
141 	{ "headphone-enable-gpios", &headphone_enable_gpio, 1 },
142 	{ NULL },
143 };
144 
145 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
146 			struct snd_pcm_hw_params *params)
147 {
148 	struct snd_interval *rate = hw_param_interval(params,
149 			SNDRV_PCM_HW_PARAM_RATE);
150 	struct snd_interval *channels = hw_param_interval(params,
151 						SNDRV_PCM_HW_PARAM_CHANNELS);
152 
153 	/* The ADSP will covert the FE rate to 48k, stereo */
154 	rate->min = rate->max = 48000;
155 	channels->min = channels->max = 2;
156 
157 	/* set SSP0 to 16 bit */
158 	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
159 	return 0;
160 }
161 
162 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
163 	struct snd_pcm_hw_params *params)
164 {
165 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
167 	int ret;
168 
169 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
170 		SND_SOC_CLOCK_IN);
171 	if (ret < 0) {
172 		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
173 		return ret;
174 	}
175 
176 	return ret;
177 }
178 
179 static const struct snd_soc_ops bdw_rt5677_ops = {
180 	.hw_params = bdw_rt5677_hw_params,
181 };
182 
183 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
184 static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
185 {
186 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
187 	struct sst_pdata *pdata = dev_get_platdata(component->dev);
188 	struct sst_hsw *broadwell = pdata->dsp;
189 	int ret;
190 
191 	/* Set ADSP SSP port settings */
192 	ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
193 		SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
194 		SST_HSW_DEVICE_CLOCK_MASTER, 9);
195 	if (ret < 0) {
196 		dev_err(rtd->dev, "error: failed to set device config\n");
197 		return ret;
198 	}
199 
200 	return 0;
201 }
202 #endif
203 
204 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
205 {
206 	struct bdw_rt5677_priv *bdw_rt5677 =
207 			snd_soc_card_get_drvdata(rtd->card);
208 	struct snd_soc_component *component = rtd->codec_dai->component;
209 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
210 	int ret;
211 
212 	ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
213 	if (ret)
214 		dev_warn(component->dev, "Failed to add driver gpios\n");
215 
216 	/* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
217 	 * The ASRC clock source is clk_i2s1_asrc.
218 	 */
219 	rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
220 			RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
221 			RT5677_CLK_SEL_I2S1_ASRC);
222 
223 	/* Request rt5677 GPIO for headphone amp control */
224 	bdw_rt5677->gpio_hp_en = devm_gpiod_get(component->dev, "headphone-enable",
225 						GPIOD_OUT_LOW);
226 	if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
227 		dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
228 		return PTR_ERR(bdw_rt5677->gpio_hp_en);
229 	}
230 
231 	/* Create and initialize headphone jack */
232 	if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
233 			SND_JACK_HEADPHONE, &headphone_jack,
234 			&headphone_jack_pin, 1)) {
235 		headphone_jack_gpio.gpiod_dev = component->dev;
236 		if (snd_soc_jack_add_gpios(&headphone_jack, 1,
237 				&headphone_jack_gpio))
238 			dev_err(component->dev, "Can't add headphone jack gpio\n");
239 	} else {
240 		dev_err(component->dev, "Can't create headphone jack\n");
241 	}
242 
243 	/* Create and initialize mic jack */
244 	if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
245 			SND_JACK_MICROPHONE, &mic_jack,
246 			&mic_jack_pin, 1)) {
247 		mic_jack_gpio.gpiod_dev = component->dev;
248 		if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
249 			dev_err(component->dev, "Can't add mic jack gpio\n");
250 	} else {
251 		dev_err(component->dev, "Can't create mic jack\n");
252 	}
253 	bdw_rt5677->component = component;
254 
255 	snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
256 	return 0;
257 }
258 
259 /* broadwell digital audio interface glue - connects codec <--> CPU */
260 static struct snd_soc_dai_link bdw_rt5677_dais[] = {
261 	/* Front End DAI links */
262 	{
263 		.name = "System PCM",
264 		.stream_name = "System Playback/Capture",
265 		.cpu_dai_name = "System Pin",
266 		.platform_name = "haswell-pcm-audio",
267 		.dynamic = 1,
268 		.codec_name = "snd-soc-dummy",
269 		.codec_dai_name = "snd-soc-dummy-dai",
270 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
271 		.init = bdw_rt5677_rtd_init,
272 #endif
273 		.trigger = {
274 			SND_SOC_DPCM_TRIGGER_POST,
275 			SND_SOC_DPCM_TRIGGER_POST
276 		},
277 		.dpcm_capture = 1,
278 		.dpcm_playback = 1,
279 	},
280 
281 	/* Back End DAI links */
282 	{
283 		/* SSP0 - Codec */
284 		.name = "Codec",
285 		.id = 0,
286 		.cpu_dai_name = "snd-soc-dummy-dai",
287 		.platform_name = "snd-soc-dummy",
288 		.no_pcm = 1,
289 		.codec_name = "i2c-RT5677CE:00",
290 		.codec_dai_name = "rt5677-aif1",
291 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
292 			SND_SOC_DAIFMT_CBS_CFS,
293 		.ignore_suspend = 1,
294 		.ignore_pmdown_time = 1,
295 		.be_hw_params_fixup = broadwell_ssp0_fixup,
296 		.ops = &bdw_rt5677_ops,
297 		.dpcm_playback = 1,
298 		.dpcm_capture = 1,
299 		.init = bdw_rt5677_init,
300 	},
301 };
302 
303 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
304 {
305 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
306 	struct snd_soc_dapm_context *dapm;
307 
308 	if (bdw_rt5677->component) {
309 		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
310 		snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
311 	}
312 	return 0;
313 }
314 
315 static int bdw_rt5677_resume_post(struct snd_soc_card *card)
316 {
317 	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
318 	struct snd_soc_dapm_context *dapm;
319 
320 	if (bdw_rt5677->component) {
321 		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
322 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
323 	}
324 	return 0;
325 }
326 
327 /* ASoC machine driver for Broadwell DSP + RT5677 */
328 static struct snd_soc_card bdw_rt5677_card = {
329 	.name = "bdw-rt5677",
330 	.owner = THIS_MODULE,
331 	.dai_link = bdw_rt5677_dais,
332 	.num_links = ARRAY_SIZE(bdw_rt5677_dais),
333 	.dapm_widgets = bdw_rt5677_widgets,
334 	.num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
335 	.dapm_routes = bdw_rt5677_map,
336 	.num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
337 	.controls = bdw_rt5677_controls,
338 	.num_controls = ARRAY_SIZE(bdw_rt5677_controls),
339 	.fully_routed = true,
340 	.suspend_pre = bdw_rt5677_suspend_pre,
341 	.resume_post = bdw_rt5677_resume_post,
342 };
343 
344 static int bdw_rt5677_probe(struct platform_device *pdev)
345 {
346 	struct bdw_rt5677_priv *bdw_rt5677;
347 	struct snd_soc_acpi_mach *mach;
348 	const char *platform_name = NULL;
349 	int ret;
350 
351 	bdw_rt5677_card.dev = &pdev->dev;
352 
353 	/* Allocate driver private struct */
354 	bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
355 		GFP_KERNEL);
356 	if (!bdw_rt5677) {
357 		dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
358 		return -ENOMEM;
359 	}
360 
361 	/* override plaform name, if required */
362 	mach = (&pdev->dev)->platform_data;
363 	if (mach) /* extra check since legacy does not pass parameters */
364 		platform_name = mach->mach_params.platform;
365 
366 	ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
367 						    platform_name);
368 	if (ret)
369 		return ret;
370 
371 	snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
372 
373 	return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
374 }
375 
376 static struct platform_driver bdw_rt5677_audio = {
377 	.probe = bdw_rt5677_probe,
378 	.driver = {
379 		.name = "bdw-rt5677",
380 	},
381 };
382 
383 module_platform_driver(bdw_rt5677_audio)
384 
385 /* Module information */
386 MODULE_AUTHOR("Ben Zhang");
387 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
388 MODULE_LICENSE("GPL v2");
389 MODULE_ALIAS("platform:bdw-rt5677");
390