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