Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
  4 *
  5 * Copyright (c) 2014, The Chromium OS Authors.  All rights reserved.
  6 */
  7
  8#include <linux/acpi.h>
  9#include <linux/module.h>
 10#include <linux/platform_device.h>
 11#include <linux/gpio/consumer.h>
 12#include <linux/delay.h>
 13#include <sound/core.h>
 14#include <sound/pcm.h>
 15#include <sound/soc.h>
 16#include <sound/pcm_params.h>
 17#include <sound/jack.h>
 18#include <sound/soc-acpi.h>
 19
 
 
 
 20#include "../../codecs/rt5677.h"
 21
 22struct bdw_rt5677_priv {
 23	struct gpio_desc *gpio_hp_en;
 24	struct snd_soc_component *component;
 25};
 26
 27static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
 28			struct snd_kcontrol *k, int event)
 29{
 30	struct snd_soc_dapm_context *dapm = w->dapm;
 31	struct snd_soc_card *card = dapm->card;
 32	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
 33
 34	if (SND_SOC_DAPM_EVENT_ON(event))
 35		msleep(70);
 36
 37	gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
 38		SND_SOC_DAPM_EVENT_ON(event));
 39
 40	return 0;
 41}
 42
 43static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
 44	SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
 45	SND_SOC_DAPM_SPK("Speaker", NULL),
 46	SND_SOC_DAPM_MIC("Headset Mic", NULL),
 47	SND_SOC_DAPM_MIC("Local DMICs", NULL),
 48	SND_SOC_DAPM_MIC("Remote DMICs", NULL),
 49};
 50
 51static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
 52	/* Speakers */
 53	{"Speaker", NULL, "PDM1L"},
 54	{"Speaker", NULL, "PDM1R"},
 55
 56	/* Headset jack connectors */
 57	{"Headphone", NULL, "LOUT1"},
 58	{"Headphone", NULL, "LOUT2"},
 59	{"IN1P", NULL, "Headset Mic"},
 60	{"IN1N", NULL, "Headset Mic"},
 61
 62	/* Digital MICs
 63	 * Local DMICs: the two DMICs on the mainboard
 64	 * Remote DMICs: the two DMICs on the camera module
 65	 */
 66	{"DMIC L1", NULL, "Remote DMICs"},
 67	{"DMIC R1", NULL, "Remote DMICs"},
 68	{"DMIC L2", NULL, "Local DMICs"},
 69	{"DMIC R2", NULL, "Local DMICs"},
 70
 71	/* CODEC BE connections */
 72	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
 73	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
 74	{"DSP Capture", NULL, "DSP Buffer"},
 75
 76	/* DSP Clock Connections */
 77	{ "DSP Buffer", NULL, "SSP0 CODEC IN" },
 78	{ "SSP0 CODEC IN", NULL, "DSPTX" },
 79};
 80
 81static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
 82	SOC_DAPM_PIN_SWITCH("Speaker"),
 83	SOC_DAPM_PIN_SWITCH("Headphone"),
 84	SOC_DAPM_PIN_SWITCH("Headset Mic"),
 85	SOC_DAPM_PIN_SWITCH("Local DMICs"),
 86	SOC_DAPM_PIN_SWITCH("Remote DMICs"),
 87};
 88
 89
 90static struct snd_soc_jack headphone_jack;
 91static struct snd_soc_jack mic_jack;
 92
 93static struct snd_soc_jack_pin headphone_jack_pin = {
 94	.pin	= "Headphone",
 95	.mask	= SND_JACK_HEADPHONE,
 96};
 97
 98static struct snd_soc_jack_pin mic_jack_pin = {
 99	.pin	= "Headset Mic",
100	.mask	= SND_JACK_MICROPHONE,
101};
102
103static struct snd_soc_jack_gpio headphone_jack_gpio = {
104	.name			= "plug-det",
105	.report			= SND_JACK_HEADPHONE,
106	.debounce_time		= 200,
107};
108
109static struct snd_soc_jack_gpio mic_jack_gpio = {
110	.name			= "mic-present",
111	.report			= SND_JACK_MICROPHONE,
112	.debounce_time		= 200,
113	.invert			= 1,
114};
115
116/* GPIO indexes defined by ACPI */
117enum {
118	RT5677_GPIO_PLUG_DET		= 0,
119	RT5677_GPIO_MIC_PRESENT_L	= 1,
120	RT5677_GPIO_HOTWORD_DET_L	= 2,
121	RT5677_GPIO_DSP_INT		= 3,
122	RT5677_GPIO_HP_AMP_SHDN_L	= 4,
123};
124
125static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
126static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
127static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
128
129static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
130	{ "plug-det-gpios", &plug_det_gpio, 1 },
131	{ "mic-present-gpios", &mic_present_gpio, 1 },
132	{ "headphone-enable-gpios", &headphone_enable_gpio, 1 },
133	{ NULL },
134};
135
136static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
137			struct snd_pcm_hw_params *params)
138{
139	struct snd_interval *rate = hw_param_interval(params,
140						      SNDRV_PCM_HW_PARAM_RATE);
141	struct snd_interval *chan = hw_param_interval(params,
142						      SNDRV_PCM_HW_PARAM_CHANNELS);
143
144	/* The ADSP will convert the FE rate to 48k, stereo */
145	rate->min = rate->max = 48000;
146	chan->min = chan->max = 2;
147
148	/* set SSP0 to 16 bit */
149	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
150	return 0;
151}
152
153static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
154	struct snd_pcm_hw_params *params)
155{
156	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
157	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
158	int ret;
159
160	ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
161		SND_SOC_CLOCK_IN);
162	if (ret < 0) {
163		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
164		return ret;
165	}
166
167	return ret;
168}
169
170static int bdw_rt5677_dsp_hw_params(struct snd_pcm_substream *substream,
171	struct snd_pcm_hw_params *params)
 
 
 
 
172{
173	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
174	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
 
175	int ret;
176
177	ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_PLL1, 24576000,
178		SND_SOC_CLOCK_IN);
179	if (ret < 0) {
180		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
181		return ret;
182	}
183	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5677_PLL1_S_MCLK,
184		24000000, 24576000);
185	if (ret < 0) {
186		dev_err(rtd->dev, "can't set codec pll configuration\n");
187		return ret;
188	}
189
190	return 0;
191}
192
193static const struct snd_soc_ops bdw_rt5677_ops = {
194	.hw_params = bdw_rt5677_hw_params,
195};
196
197static const struct snd_soc_ops bdw_rt5677_dsp_ops = {
198	.hw_params = bdw_rt5677_dsp_hw_params,
199};
200
201static const unsigned int channels[] = {
202	2,
203};
204
205static const struct snd_pcm_hw_constraint_list constraints_channels = {
206	.count = ARRAY_SIZE(channels),
207	.list = channels,
208	.mask = 0,
209};
210
211static int bdw_rt5677_fe_startup(struct snd_pcm_substream *substream)
212{
213	struct snd_pcm_runtime *runtime = substream->runtime;
214
215	/* Board supports stereo configuration only */
216	runtime->hw.channels_max = 2;
217	return snd_pcm_hw_constraint_list(runtime, 0,
218					  SNDRV_PCM_HW_PARAM_CHANNELS,
219					  &constraints_channels);
220}
221
222static const struct snd_soc_ops bdw_rt5677_fe_ops = {
223	.startup = bdw_rt5677_fe_startup,
224};
225
226static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
227{
228	struct bdw_rt5677_priv *bdw_rt5677 =
229			snd_soc_card_get_drvdata(rtd->card);
230	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
231	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
232	int ret;
233
234	ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
235	if (ret)
236		dev_warn(component->dev, "Failed to add driver gpios\n");
237
238	/* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
239	 * The ASRC clock source is clk_i2s1_asrc.
240	 */
241	rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
242			RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
243			RT5677_CLK_SEL_I2S1_ASRC);
244	/* Enable codec ASRC function for Mono ADC L.
245	 * The ASRC clock source is clk_sys2_asrc.
246	 */
247	rt5677_sel_asrc_clk_src(component, RT5677_AD_MONO_L_FILTER,
248			RT5677_CLK_SEL_SYS2);
249
250	/* Request rt5677 GPIO for headphone amp control */
251	bdw_rt5677->gpio_hp_en = gpiod_get(component->dev, "headphone-enable",
252					   GPIOD_OUT_LOW);
253	if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
254		dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
255		return PTR_ERR(bdw_rt5677->gpio_hp_en);
256	}
257
258	/* Create and initialize headphone jack */
259	if (!snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
260			SND_JACK_HEADPHONE, &headphone_jack,
261			&headphone_jack_pin, 1)) {
262		headphone_jack_gpio.gpiod_dev = component->dev;
263		if (snd_soc_jack_add_gpios(&headphone_jack, 1,
264				&headphone_jack_gpio))
265			dev_err(component->dev, "Can't add headphone jack gpio\n");
266	} else {
267		dev_err(component->dev, "Can't create headphone jack\n");
268	}
269
270	/* Create and initialize mic jack */
271	if (!snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
272			SND_JACK_MICROPHONE, &mic_jack,
273			&mic_jack_pin, 1)) {
274		mic_jack_gpio.gpiod_dev = component->dev;
275		if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
276			dev_err(component->dev, "Can't add mic jack gpio\n");
277	} else {
278		dev_err(component->dev, "Can't create mic jack\n");
279	}
280	bdw_rt5677->component = component;
281
282	snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
283	return 0;
284}
285
286static void bdw_rt5677_exit(struct snd_soc_pcm_runtime *rtd)
287{
288	struct bdw_rt5677_priv *bdw_rt5677 =
289			snd_soc_card_get_drvdata(rtd->card);
290
291	/*
292	 * The .exit() can be reached without going through the .init()
293	 * so explicitly test if the gpiod is valid
294	 */
295	if (!IS_ERR_OR_NULL(bdw_rt5677->gpio_hp_en))
296		gpiod_put(bdw_rt5677->gpio_hp_en);
297}
298
299/* broadwell digital audio interface glue - connects codec <--> CPU */
300SND_SOC_DAILINK_DEF(dummy,
301	DAILINK_COMP_ARRAY(COMP_DUMMY()));
302
303SND_SOC_DAILINK_DEF(fe,
304	DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
305
306SND_SOC_DAILINK_DEF(platform,
307	DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
308
309SND_SOC_DAILINK_DEF(be,
310	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
311
312SND_SOC_DAILINK_DEF(ssp0_port,
313	    DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
314
315/* Wake on voice interface */
316SND_SOC_DAILINK_DEFS(dsp,
317	DAILINK_COMP_ARRAY(COMP_CPU("spi-RT5677AA:00")),
318	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-dspbuffer")),
319	DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-RT5677AA:00")));
320
321static struct snd_soc_dai_link bdw_rt5677_dais[] = {
322	/* Front End DAI links */
323	{
324		.name = "System PCM",
325		.stream_name = "System Playback/Capture",
326		.nonatomic = 1,
327		.dynamic = 1,
 
 
 
328		.trigger = {
329			SND_SOC_DPCM_TRIGGER_POST,
330			SND_SOC_DPCM_TRIGGER_POST
331		},
332		.ops = &bdw_rt5677_fe_ops,
 
333		SND_SOC_DAILINK_REG(fe, dummy, platform),
334	},
335
336	/* Non-DPCM links */
337	{
338		.name = "Codec DSP",
339		.stream_name = "Wake on Voice",
340		.capture_only = 1,
341		.ops = &bdw_rt5677_dsp_ops,
342		SND_SOC_DAILINK_REG(dsp),
343	},
344
345	/* Back End DAI links */
346	{
347		/* SSP0 - Codec */
348		.name = "Codec",
349		.id = 0,
350		.nonatomic = 1,
351		.no_pcm = 1,
352		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
353			SND_SOC_DAIFMT_CBC_CFC,
 
354		.ignore_pmdown_time = 1,
355		.be_hw_params_fixup = broadwell_ssp0_fixup,
356		.ops = &bdw_rt5677_ops,
 
 
357		.init = bdw_rt5677_init,
358		.exit = bdw_rt5677_exit,
359		SND_SOC_DAILINK_REG(ssp0_port, be, platform),
360	},
361};
362
363static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
364{
365	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
366	struct snd_soc_dapm_context *dapm;
367
368	if (bdw_rt5677->component) {
369		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
370		snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
371	}
372	return 0;
373}
374
375static int bdw_rt5677_resume_post(struct snd_soc_card *card)
376{
377	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
378	struct snd_soc_dapm_context *dapm;
379
380	if (bdw_rt5677->component) {
381		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
382		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
383	}
384	return 0;
385}
386
387/* use space before codec name to simplify card ID, and simplify driver name */
388#define SOF_CARD_NAME "bdw rt5677" /* card name will be 'sof-bdw rt5677' */
389#define SOF_DRIVER_NAME "SOF"
390
391#define CARD_NAME "bdw-rt5677"
392#define DRIVER_NAME NULL /* card name will be used for driver name */
393
394/* ASoC machine driver for Broadwell DSP + RT5677 */
395static struct snd_soc_card bdw_rt5677_card = {
396	.name = CARD_NAME,
397	.driver_name = DRIVER_NAME,
398	.owner = THIS_MODULE,
399	.dai_link = bdw_rt5677_dais,
400	.num_links = ARRAY_SIZE(bdw_rt5677_dais),
401	.dapm_widgets = bdw_rt5677_widgets,
402	.num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
403	.dapm_routes = bdw_rt5677_map,
404	.num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
405	.controls = bdw_rt5677_controls,
406	.num_controls = ARRAY_SIZE(bdw_rt5677_controls),
407	.fully_routed = true,
408	.suspend_pre = bdw_rt5677_suspend_pre,
409	.resume_post = bdw_rt5677_resume_post,
410};
411
412static int bdw_rt5677_probe(struct platform_device *pdev)
413{
414	struct bdw_rt5677_priv *bdw_rt5677;
415	struct snd_soc_acpi_mach *mach;
416	int ret;
417
418	bdw_rt5677_card.dev = &pdev->dev;
419
420	/* Allocate driver private struct */
421	bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
422		GFP_KERNEL);
423	if (!bdw_rt5677)
 
424		return -ENOMEM;
 
425
426	/* override platform name, if required */
427	mach = pdev->dev.platform_data;
428	ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
429						    mach->mach_params.platform);
430	if (ret)
431		return ret;
432
433	/* set card and driver name */
434	if (snd_soc_acpi_sof_parent(&pdev->dev)) {
435		bdw_rt5677_card.name = SOF_CARD_NAME;
436		bdw_rt5677_card.driver_name = SOF_DRIVER_NAME;
437	} else {
438		bdw_rt5677_card.name = CARD_NAME;
439		bdw_rt5677_card.driver_name = DRIVER_NAME;
440	}
441
442	snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
443
444	return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
445}
446
447static struct platform_driver bdw_rt5677_audio = {
448	.probe = bdw_rt5677_probe,
449	.driver = {
450		.name = "bdw-rt5677",
451		.pm = &snd_soc_pm_ops
452	},
453};
454
455module_platform_driver(bdw_rt5677_audio)
456
457/* Module information */
458MODULE_AUTHOR("Ben Zhang");
459MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
460MODULE_LICENSE("GPL v2");
461MODULE_ALIAS("platform:bdw-rt5677");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
  4 *
  5 * Copyright (c) 2014, The Chromium OS Authors.  All rights reserved.
  6 */
  7
  8#include <linux/acpi.h>
  9#include <linux/module.h>
 10#include <linux/platform_device.h>
 11#include <linux/gpio/consumer.h>
 12#include <linux/delay.h>
 13#include <sound/core.h>
 14#include <sound/pcm.h>
 15#include <sound/soc.h>
 16#include <sound/pcm_params.h>
 17#include <sound/jack.h>
 18#include <sound/soc-acpi.h>
 19
 20#include "../common/sst-dsp.h"
 21#include "../haswell/sst-haswell-ipc.h"
 22
 23#include "../../codecs/rt5677.h"
 24
 25struct bdw_rt5677_priv {
 26	struct gpio_desc *gpio_hp_en;
 27	struct snd_soc_component *component;
 28};
 29
 30static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
 31			struct snd_kcontrol *k, int event)
 32{
 33	struct snd_soc_dapm_context *dapm = w->dapm;
 34	struct snd_soc_card *card = dapm->card;
 35	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
 36
 37	if (SND_SOC_DAPM_EVENT_ON(event))
 38		msleep(70);
 39
 40	gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
 41		SND_SOC_DAPM_EVENT_ON(event));
 42
 43	return 0;
 44}
 45
 46static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
 47	SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
 48	SND_SOC_DAPM_SPK("Speaker", NULL),
 49	SND_SOC_DAPM_MIC("Headset Mic", NULL),
 50	SND_SOC_DAPM_MIC("Local DMICs", NULL),
 51	SND_SOC_DAPM_MIC("Remote DMICs", NULL),
 52};
 53
 54static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
 55	/* Speakers */
 56	{"Speaker", NULL, "PDM1L"},
 57	{"Speaker", NULL, "PDM1R"},
 58
 59	/* Headset jack connectors */
 60	{"Headphone", NULL, "LOUT1"},
 61	{"Headphone", NULL, "LOUT2"},
 62	{"IN1P", NULL, "Headset Mic"},
 63	{"IN1N", NULL, "Headset Mic"},
 64
 65	/* Digital MICs
 66	 * Local DMICs: the two DMICs on the mainboard
 67	 * Remote DMICs: the two DMICs on the camera module
 68	 */
 69	{"DMIC L1", NULL, "Remote DMICs"},
 70	{"DMIC R1", NULL, "Remote DMICs"},
 71	{"DMIC L2", NULL, "Local DMICs"},
 72	{"DMIC R2", NULL, "Local DMICs"},
 73
 74	/* CODEC BE connections */
 75	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
 76	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
 
 
 
 
 
 77};
 78
 79static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
 80	SOC_DAPM_PIN_SWITCH("Speaker"),
 81	SOC_DAPM_PIN_SWITCH("Headphone"),
 82	SOC_DAPM_PIN_SWITCH("Headset Mic"),
 83	SOC_DAPM_PIN_SWITCH("Local DMICs"),
 84	SOC_DAPM_PIN_SWITCH("Remote DMICs"),
 85};
 86
 87
 88static struct snd_soc_jack headphone_jack;
 89static struct snd_soc_jack mic_jack;
 90
 91static struct snd_soc_jack_pin headphone_jack_pin = {
 92	.pin	= "Headphone",
 93	.mask	= SND_JACK_HEADPHONE,
 94};
 95
 96static struct snd_soc_jack_pin mic_jack_pin = {
 97	.pin	= "Headset Mic",
 98	.mask	= SND_JACK_MICROPHONE,
 99};
100
101static struct snd_soc_jack_gpio headphone_jack_gpio = {
102	.name			= "plug-det",
103	.report			= SND_JACK_HEADPHONE,
104	.debounce_time		= 200,
105};
106
107static struct snd_soc_jack_gpio mic_jack_gpio = {
108	.name			= "mic-present",
109	.report			= SND_JACK_MICROPHONE,
110	.debounce_time		= 200,
111	.invert			= 1,
112};
113
114/* GPIO indexes defined by ACPI */
115enum {
116	RT5677_GPIO_PLUG_DET		= 0,
117	RT5677_GPIO_MIC_PRESENT_L	= 1,
118	RT5677_GPIO_HOTWORD_DET_L	= 2,
119	RT5677_GPIO_DSP_INT		= 3,
120	RT5677_GPIO_HP_AMP_SHDN_L	= 4,
121};
122
123static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
124static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
125static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
126
127static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
128	{ "plug-det-gpios", &plug_det_gpio, 1 },
129	{ "mic-present-gpios", &mic_present_gpio, 1 },
130	{ "headphone-enable-gpios", &headphone_enable_gpio, 1 },
131	{ NULL },
132};
133
134static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
135			struct snd_pcm_hw_params *params)
136{
137	struct snd_interval *rate = hw_param_interval(params,
138			SNDRV_PCM_HW_PARAM_RATE);
139	struct snd_interval *channels = hw_param_interval(params,
140						SNDRV_PCM_HW_PARAM_CHANNELS);
141
142	/* The ADSP will covert the FE rate to 48k, stereo */
143	rate->min = rate->max = 48000;
144	channels->min = channels->max = 2;
145
146	/* set SSP0 to 16 bit */
147	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
148	return 0;
149}
150
151static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
152	struct snd_pcm_hw_params *params)
153{
154	struct snd_soc_pcm_runtime *rtd = substream->private_data;
155	struct snd_soc_dai *codec_dai = rtd->codec_dai;
156	int ret;
157
158	ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
159		SND_SOC_CLOCK_IN);
160	if (ret < 0) {
161		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
162		return ret;
163	}
164
165	return ret;
166}
167
168static const struct snd_soc_ops bdw_rt5677_ops = {
169	.hw_params = bdw_rt5677_hw_params,
170};
171
172#if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
173static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
174{
175	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
176	struct sst_pdata *pdata = dev_get_platdata(component->dev);
177	struct sst_hsw *broadwell = pdata->dsp;
178	int ret;
179
180	/* Set ADSP SSP port settings */
181	ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
182		SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
183		SST_HSW_DEVICE_CLOCK_MASTER, 9);
 
 
 
 
184	if (ret < 0) {
185		dev_err(rtd->dev, "error: failed to set device config\n");
186		return ret;
187	}
188
189	return 0;
190}
191#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
193static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
194{
195	struct bdw_rt5677_priv *bdw_rt5677 =
196			snd_soc_card_get_drvdata(rtd->card);
197	struct snd_soc_component *component = rtd->codec_dai->component;
198	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
199	int ret;
200
201	ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
202	if (ret)
203		dev_warn(component->dev, "Failed to add driver gpios\n");
204
205	/* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
206	 * The ASRC clock source is clk_i2s1_asrc.
207	 */
208	rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
209			RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
210			RT5677_CLK_SEL_I2S1_ASRC);
 
 
 
 
 
211
212	/* Request rt5677 GPIO for headphone amp control */
213	bdw_rt5677->gpio_hp_en = devm_gpiod_get(component->dev, "headphone-enable",
214						GPIOD_OUT_LOW);
215	if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
216		dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
217		return PTR_ERR(bdw_rt5677->gpio_hp_en);
218	}
219
220	/* Create and initialize headphone jack */
221	if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
222			SND_JACK_HEADPHONE, &headphone_jack,
223			&headphone_jack_pin, 1)) {
224		headphone_jack_gpio.gpiod_dev = component->dev;
225		if (snd_soc_jack_add_gpios(&headphone_jack, 1,
226				&headphone_jack_gpio))
227			dev_err(component->dev, "Can't add headphone jack gpio\n");
228	} else {
229		dev_err(component->dev, "Can't create headphone jack\n");
230	}
231
232	/* Create and initialize mic jack */
233	if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
234			SND_JACK_MICROPHONE, &mic_jack,
235			&mic_jack_pin, 1)) {
236		mic_jack_gpio.gpiod_dev = component->dev;
237		if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
238			dev_err(component->dev, "Can't add mic jack gpio\n");
239	} else {
240		dev_err(component->dev, "Can't create mic jack\n");
241	}
242	bdw_rt5677->component = component;
243
244	snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
245	return 0;
246}
247
 
 
 
 
 
 
 
 
 
 
 
 
 
248/* broadwell digital audio interface glue - connects codec <--> CPU */
249SND_SOC_DAILINK_DEF(dummy,
250	DAILINK_COMP_ARRAY(COMP_DUMMY()));
251
252SND_SOC_DAILINK_DEF(fe,
253	DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
254
255SND_SOC_DAILINK_DEF(platform,
256	DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
257
258SND_SOC_DAILINK_DEF(be,
259	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
260
 
 
 
 
 
 
 
 
 
261static struct snd_soc_dai_link bdw_rt5677_dais[] = {
262	/* Front End DAI links */
263	{
264		.name = "System PCM",
265		.stream_name = "System Playback/Capture",
 
266		.dynamic = 1,
267#if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
268		.init = bdw_rt5677_rtd_init,
269#endif
270		.trigger = {
271			SND_SOC_DPCM_TRIGGER_POST,
272			SND_SOC_DPCM_TRIGGER_POST
273		},
274		.dpcm_capture = 1,
275		.dpcm_playback = 1,
276		SND_SOC_DAILINK_REG(fe, dummy, platform),
277	},
278
 
 
 
 
 
 
 
 
 
279	/* Back End DAI links */
280	{
281		/* SSP0 - Codec */
282		.name = "Codec",
283		.id = 0,
 
284		.no_pcm = 1,
285		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
286			SND_SOC_DAIFMT_CBS_CFS,
287		.ignore_suspend = 1,
288		.ignore_pmdown_time = 1,
289		.be_hw_params_fixup = broadwell_ssp0_fixup,
290		.ops = &bdw_rt5677_ops,
291		.dpcm_playback = 1,
292		.dpcm_capture = 1,
293		.init = bdw_rt5677_init,
294		SND_SOC_DAILINK_REG(dummy, be, dummy),
 
295	},
296};
297
298static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
299{
300	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
301	struct snd_soc_dapm_context *dapm;
302
303	if (bdw_rt5677->component) {
304		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
305		snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
306	}
307	return 0;
308}
309
310static int bdw_rt5677_resume_post(struct snd_soc_card *card)
311{
312	struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
313	struct snd_soc_dapm_context *dapm;
314
315	if (bdw_rt5677->component) {
316		dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
317		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
318	}
319	return 0;
320}
321
 
 
 
 
 
 
 
322/* ASoC machine driver for Broadwell DSP + RT5677 */
323static struct snd_soc_card bdw_rt5677_card = {
324	.name = "bdw-rt5677",
 
325	.owner = THIS_MODULE,
326	.dai_link = bdw_rt5677_dais,
327	.num_links = ARRAY_SIZE(bdw_rt5677_dais),
328	.dapm_widgets = bdw_rt5677_widgets,
329	.num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
330	.dapm_routes = bdw_rt5677_map,
331	.num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
332	.controls = bdw_rt5677_controls,
333	.num_controls = ARRAY_SIZE(bdw_rt5677_controls),
334	.fully_routed = true,
335	.suspend_pre = bdw_rt5677_suspend_pre,
336	.resume_post = bdw_rt5677_resume_post,
337};
338
339static int bdw_rt5677_probe(struct platform_device *pdev)
340{
341	struct bdw_rt5677_priv *bdw_rt5677;
342	struct snd_soc_acpi_mach *mach;
343	int ret;
344
345	bdw_rt5677_card.dev = &pdev->dev;
346
347	/* Allocate driver private struct */
348	bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
349		GFP_KERNEL);
350	if (!bdw_rt5677) {
351		dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
352		return -ENOMEM;
353	}
354
355	/* override plaform name, if required */
356	mach = (&pdev->dev)->platform_data;
357	ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
358						    mach->mach_params.platform);
359	if (ret)
360		return ret;
361
 
 
 
 
 
 
 
 
 
362	snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
363
364	return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
365}
366
367static struct platform_driver bdw_rt5677_audio = {
368	.probe = bdw_rt5677_probe,
369	.driver = {
370		.name = "bdw-rt5677",
 
371	},
372};
373
374module_platform_driver(bdw_rt5677_audio)
375
376/* Module information */
377MODULE_AUTHOR("Ben Zhang");
378MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
379MODULE_LICENSE("GPL v2");
380MODULE_ALIAS("platform:bdw-rt5677");