Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  bytcr_wm5102.c - ASoc Machine driver for Intel Baytrail platforms with a
  4 *                   Wolfson Microelectronics WM5102 codec
  5 *
  6 *  Copyright (C) 2020 Hans de Goede <hdegoede@redhat.com>
  7 *  Loosely based on bytcr_rt5640.c which is:
  8 *  Copyright (C) 2014-2020 Intel Corp
  9 *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
 10 */
 11
 12#include <linux/acpi.h>
 13#include <linux/clk.h>
 14#include <linux/device.h>
 15#include <linux/init.h>
 16#include <linux/module.h>
 17#include <linux/moduleparam.h>
 18#include <linux/platform_device.h>
 19#include <linux/slab.h>
 20#include <linux/spi/spi.h>
 21#include <sound/jack.h>
 22#include <sound/pcm.h>
 23#include <sound/pcm_params.h>
 24#include <sound/soc.h>
 25#include <sound/soc-acpi.h>
 26#include "../../codecs/wm5102.h"
 27#include "../atom/sst-atom-controls.h"
 28
 29#define MCLK_FREQ		25000000
 30
 31#define WM5102_MAX_SYSCLK_4K	49152000 /* max sysclk for 4K family */
 32#define WM5102_MAX_SYSCLK_11025	45158400 /* max sysclk for 11.025K family */
 33
 34struct byt_wm5102_private {
 35	struct snd_soc_jack jack;
 36	struct clk *mclk;
 37	struct gpio_desc *spkvdd_en_gpio;
 38};
 39
 40static int byt_wm5102_spkvdd_power_event(struct snd_soc_dapm_widget *w,
 41	struct snd_kcontrol *kcontrol, int event)
 42{
 43	struct snd_soc_card *card = w->dapm->card;
 44	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
 45
 46	gpiod_set_value_cansleep(priv->spkvdd_en_gpio,
 47				 !!SND_SOC_DAPM_EVENT_ON(event));
 48
 49	return 0;
 50}
 51
 52static int byt_wm5102_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, int rate)
 53{
 54	struct snd_soc_component *codec_component = codec_dai->component;
 55	int sr_mult = ((rate % 4000) == 0) ?
 56		(WM5102_MAX_SYSCLK_4K / rate) :
 57		(WM5102_MAX_SYSCLK_11025 / rate);
 58	int ret;
 59
 60	/* Reset FLL1 */
 61	snd_soc_dai_set_pll(codec_dai, WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
 62	snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
 63
 64	/* Configure the FLL1 PLL before selecting it */
 65	ret = snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_CLK_SRC_MCLK1,
 66				  MCLK_FREQ, rate * sr_mult);
 67	if (ret) {
 68		dev_err(codec_component->dev, "Error setting PLL: %d\n", ret);
 69		return ret;
 70	}
 71
 72	ret = snd_soc_component_set_sysclk(codec_component, ARIZONA_CLK_SYSCLK,
 73					   ARIZONA_CLK_SRC_FLL1, rate * sr_mult,
 74					   SND_SOC_CLOCK_IN);
 75	if (ret) {
 76		dev_err(codec_component->dev, "Error setting SYSCLK: %d\n", ret);
 77		return ret;
 78	}
 79
 80	ret = snd_soc_dai_set_sysclk(codec_dai, ARIZONA_CLK_SYSCLK,
 81				     rate * 512, SND_SOC_CLOCK_IN);
 82	if (ret) {
 83		dev_err(codec_component->dev, "Error setting clock: %d\n", ret);
 84		return ret;
 85	}
 86
 87	return 0;
 88}
 89
 90static int platform_clock_control(struct snd_soc_dapm_widget *w,
 91				  struct snd_kcontrol *k, int event)
 92{
 93	struct snd_soc_dapm_context *dapm = w->dapm;
 94	struct snd_soc_card *card = dapm->card;
 95	struct snd_soc_dai *codec_dai;
 96	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
 97	int ret;
 98
 99	codec_dai = snd_soc_card_get_codec_dai(card, "wm5102-aif1");
100	if (!codec_dai) {
101		dev_err(card->dev, "Error codec DAI not found\n");
102		return -EIO;
103	}
104
105	if (SND_SOC_DAPM_EVENT_ON(event)) {
106		ret = clk_prepare_enable(priv->mclk);
107		if (ret) {
108			dev_err(card->dev, "Error enabling MCLK: %d\n", ret);
109			return ret;
110		}
111		ret = byt_wm5102_prepare_and_enable_pll1(codec_dai, 48000);
112		if (ret) {
113			dev_err(card->dev, "Error setting codec sysclk: %d\n", ret);
114			return ret;
115		}
116	} else {
117		/*
118		 * The WM5102 has a separate 32KHz clock for jack-detect
119		 * so we can disable the PLL, followed by disabling the
120		 * platform clock which is the source-clock for the PLL.
121		 */
122		snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
123		clk_disable_unprepare(priv->mclk);
124	}
125
126	return 0;
127}
128
129static const struct snd_soc_dapm_widget byt_wm5102_widgets[] = {
130	SND_SOC_DAPM_HP("Headphone", NULL),
131	SND_SOC_DAPM_MIC("Headset Mic", NULL),
132	SND_SOC_DAPM_MIC("Internal Mic", NULL),
133	SND_SOC_DAPM_SPK("Speaker", NULL),
134	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
135			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
136			    SND_SOC_DAPM_POST_PMD),
137	SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0,
138			    byt_wm5102_spkvdd_power_event,
139			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
140};
141
142static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = {
143	{"Headphone", NULL, "Platform Clock"},
144	{"Headset Mic", NULL, "Platform Clock"},
145	{"Internal Mic", NULL, "Platform Clock"},
146	{"Speaker", NULL, "Platform Clock"},
147
148	{"Speaker", NULL, "SPKOUTLP"},
149	{"Speaker", NULL, "SPKOUTLN"},
150	{"Speaker", NULL, "SPKOUTRP"},
151	{"Speaker", NULL, "SPKOUTRN"},
152	{"Speaker", NULL, "Speaker VDD"},
153
154	{"Headphone", NULL, "HPOUT1L"},
155	{"Headphone", NULL, "HPOUT1R"},
156
157	{"Internal Mic", NULL, "MICBIAS3"},
158	{"IN3L", NULL, "Internal Mic"},
159
160	/*
161	 * The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset
162	 * is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch.
163	 */
164	{"Headset Mic", NULL, "MICBIAS1"},
165	{"Headset Mic", NULL, "MICBIAS2"},
166	{"IN1L", NULL, "Headset Mic"},
167
168	{"AIF1 Playback", NULL, "ssp0 Tx"},
169	{"ssp0 Tx", NULL, "modem_out"},
170
171	{"modem_in", NULL, "ssp0 Rx"},
172	{"ssp0 Rx", NULL, "AIF1 Capture"},
173};
174
175static const struct snd_kcontrol_new byt_wm5102_controls[] = {
176	SOC_DAPM_PIN_SWITCH("Headphone"),
177	SOC_DAPM_PIN_SWITCH("Headset Mic"),
178	SOC_DAPM_PIN_SWITCH("Internal Mic"),
179	SOC_DAPM_PIN_SWITCH("Speaker"),
180};
181
182static struct snd_soc_jack_pin byt_wm5102_pins[] = {
183	{
184		.pin	= "Headphone",
185		.mask	= SND_JACK_HEADPHONE,
186	},
187	{
188		.pin	= "Headset Mic",
189		.mask	= SND_JACK_MICROPHONE,
190	},
191};
192
193static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime)
194{
195	struct snd_soc_card *card = runtime->card;
196	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
197	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
198	int ret, jack_type;
199
200	card->dapm.idle_bias_off = true;
201
202	ret = snd_soc_add_card_controls(card, byt_wm5102_controls,
203					ARRAY_SIZE(byt_wm5102_controls));
204	if (ret) {
205		dev_err(card->dev, "Error adding card controls: %d\n", ret);
206		return ret;
207	}
208
209	/*
210	 * The firmware might enable the clock at boot (this information
211	 * may or may not be reflected in the enable clock register).
212	 * To change the rate we must disable the clock first to cover these
213	 * cases. Due to common clock framework restrictions that do not allow
214	 * to disable a clock that has not been enabled, we need to enable
215	 * the clock first.
216	 */
217	ret = clk_prepare_enable(priv->mclk);
218	if (!ret)
219		clk_disable_unprepare(priv->mclk);
220
221	ret = clk_set_rate(priv->mclk, MCLK_FREQ);
222	if (ret) {
223		dev_err(card->dev, "Error setting MCLK rate: %d\n", ret);
224		return ret;
225	}
226
227	jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
228		    SND_JACK_BTN_2 | SND_JACK_BTN_3;
229	ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type,
230					 &priv->jack, byt_wm5102_pins,
231					 ARRAY_SIZE(byt_wm5102_pins));
232	if (ret) {
233		dev_err(card->dev, "Error creating jack: %d\n", ret);
234		return ret;
235	}
236
237	snd_soc_component_set_jack(component, &priv->jack, NULL);
238
239	return 0;
240}
241
242static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd,
243				  struct snd_pcm_hw_params *params)
244{
245	struct snd_interval *rate = hw_param_interval(params,
246						      SNDRV_PCM_HW_PARAM_RATE);
247	struct snd_interval *channels = hw_param_interval(params,
248							  SNDRV_PCM_HW_PARAM_CHANNELS);
249	int ret;
250
251	/* The DSP will convert the FE rate to 48k, stereo */
252	rate->min = 48000;
253	rate->max = 48000;
254	channels->min = 2;
255	channels->max = 2;
256
257	/* set SSP0 to 16-bit */
258	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
259
260	/*
261	 * Default mode for SSP configuration is TDM 4 slot, override config
262	 * with explicit setting to I2S 2ch 16-bit. The word length is set with
263	 * dai_set_tdm_slot() since there is no other API exposed
264	 */
265	ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
266				  SND_SOC_DAIFMT_I2S     |
267				  SND_SOC_DAIFMT_NB_NF   |
268				  SND_SOC_DAIFMT_BP_FP);
269	if (ret) {
270		dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret);
271		return ret;
272	}
273
274	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 16);
275	if (ret) {
276		dev_err(rtd->dev, "Error setting I2S config: %d\n", ret);
277		return ret;
278	}
279
280	return 0;
281}
282
283static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream)
284{
285	return snd_pcm_hw_constraint_single(substream->runtime,
286					    SNDRV_PCM_HW_PARAM_RATE, 48000);
287}
288
289static const struct snd_soc_ops byt_wm5102_aif1_ops = {
290	.startup = byt_wm5102_aif1_startup,
291};
292
293SND_SOC_DAILINK_DEF(dummy,
294	DAILINK_COMP_ARRAY(COMP_DUMMY()));
295
296SND_SOC_DAILINK_DEF(media,
297	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
298
299SND_SOC_DAILINK_DEF(deepbuffer,
300	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
301
302SND_SOC_DAILINK_DEF(ssp0_port,
303	DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
304
305SND_SOC_DAILINK_DEF(ssp0_codec,
306	DAILINK_COMP_ARRAY(COMP_CODEC(
307	/*
308	 * Note there is no need to overwrite the codec-name as is done in
309	 * other bytcr machine drivers, because the codec is a MFD child-dev.
310	 */
311	"wm5102-codec",
312	"wm5102-aif1")));
313
314SND_SOC_DAILINK_DEF(platform,
315	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
316
317static struct snd_soc_dai_link byt_wm5102_dais[] = {
318	[MERR_DPCM_AUDIO] = {
319		.name = "Baytrail Audio Port",
320		.stream_name = "Baytrail Audio",
321		.nonatomic = true,
322		.dynamic = 1,
323		.dpcm_playback = 1,
324		.dpcm_capture = 1,
325		.ops = &byt_wm5102_aif1_ops,
326		SND_SOC_DAILINK_REG(media, dummy, platform),
327
328	},
329	[MERR_DPCM_DEEP_BUFFER] = {
330		.name = "Deep-Buffer Audio Port",
331		.stream_name = "Deep-Buffer Audio",
332		.nonatomic = true,
333		.dynamic = 1,
334		.dpcm_playback = 1,
335		.ops = &byt_wm5102_aif1_ops,
336		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
337	},
338		/* back ends */
339	{
340		/*
341		 * This must be named SSP2-Codec even though this machine driver
342		 * always uses SSP0. Most machine drivers support both and dynamically
343		 * update the dailink to point to SSP0 or SSP2, while keeping the name
344		 * as "SSP2-Codec". The SOF tplg files hardcode the "SSP2-Codec" even
345		 * in the byt-foo-ssp0.tplg versions because the other machine-drivers
346		 * use "SSP2-Codec" even when SSP0 is used.
347		 */
348		.name = "SSP2-Codec",
349		.id = 0,
350		.no_pcm = 1,
351		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
352						| SND_SOC_DAIFMT_CBC_CFC,
353		.be_hw_params_fixup = byt_wm5102_codec_fixup,
354		.dpcm_playback = 1,
355		.dpcm_capture = 1,
356		.init = byt_wm5102_init,
357		SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform),
358	},
359};
360
361/* use space before codec name to simplify card ID, and simplify driver name */
362#define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */
363#define SOF_DRIVER_NAME "SOF"
364
365#define CARD_NAME "bytcr-wm5102"
366#define DRIVER_NAME NULL /* card name will be used for driver name */
367
368/* SoC card */
369static struct snd_soc_card byt_wm5102_card = {
370	.owner = THIS_MODULE,
371	.dai_link = byt_wm5102_dais,
372	.num_links = ARRAY_SIZE(byt_wm5102_dais),
373	.dapm_widgets = byt_wm5102_widgets,
374	.num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets),
375	.dapm_routes = byt_wm5102_audio_map,
376	.num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map),
377	.fully_routed = true,
378};
379
380static int snd_byt_wm5102_mc_probe(struct platform_device *pdev)
381{
382	char codec_name[SND_ACPI_I2C_ID_LEN];
383	struct device *dev = &pdev->dev;
384	struct byt_wm5102_private *priv;
385	struct snd_soc_acpi_mach *mach;
386	const char *platform_name;
387	struct acpi_device *adev;
388	struct device *codec_dev;
389	bool sof_parent;
390	int ret;
391
392	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
393	if (!priv)
394		return -ENOMEM;
395
396	/* Get MCLK */
397	priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
398	if (IS_ERR(priv->mclk))
399		return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n");
400
401	/*
402	 * Get speaker VDD enable GPIO:
403	 * 1. Get codec-device-name
404	 * 2. Get codec-device
405	 * 3. Get GPIO from codec-device
406	 */
407	mach = dev->platform_data;
408	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
409	if (!adev) {
410		dev_err(dev, "Error cannot find acpi-dev for codec\n");
411		return -ENOENT;
412	}
413	snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev));
414
415	codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name);
416	acpi_dev_put(adev);
417	if (!codec_dev)
418		return -EPROBE_DEFER;
419
420	/* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */
421	priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW);
422	put_device(codec_dev);
423
424	if (IS_ERR(priv->spkvdd_en_gpio)) {
425		ret = PTR_ERR(priv->spkvdd_en_gpio);
426		/*
427		 * The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c,
428		 * so -ENOENT means that arizona-spi hasn't probed yet.
429		 */
430		if (ret == -ENOENT)
431			ret = -EPROBE_DEFER;
432
433		return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n");
434	}
435
436	/* override platform name, if required */
437	byt_wm5102_card.dev = dev;
438	platform_name = mach->mach_params.platform;
439	ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name);
440	if (ret)
441		goto out_put_gpio;
442
443	/* set card and driver name and pm-ops */
444	sof_parent = snd_soc_acpi_sof_parent(dev);
445	if (sof_parent) {
446		byt_wm5102_card.name = SOF_CARD_NAME;
447		byt_wm5102_card.driver_name = SOF_DRIVER_NAME;
448		dev->driver->pm = &snd_soc_pm_ops;
449	} else {
450		byt_wm5102_card.name = CARD_NAME;
451		byt_wm5102_card.driver_name = DRIVER_NAME;
452	}
453
454	snd_soc_card_set_drvdata(&byt_wm5102_card, priv);
455	ret = devm_snd_soc_register_card(dev, &byt_wm5102_card);
456	if (ret) {
457		dev_err_probe(dev, ret, "registering card\n");
458		goto out_put_gpio;
459	}
460
461	platform_set_drvdata(pdev, &byt_wm5102_card);
462	return 0;
463
464out_put_gpio:
465	gpiod_put(priv->spkvdd_en_gpio);
466	return ret;
467}
468
469static int snd_byt_wm5102_mc_remove(struct platform_device *pdev)
470{
471	struct snd_soc_card *card = platform_get_drvdata(pdev);
472	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
473
474	gpiod_put(priv->spkvdd_en_gpio);
475	return 0;
476}
477
478static struct platform_driver snd_byt_wm5102_mc_driver = {
479	.driver = {
480		.name = "bytcr_wm5102",
481	},
482	.probe = snd_byt_wm5102_mc_probe,
483	.remove = snd_byt_wm5102_mc_remove,
484};
485
486module_platform_driver(snd_byt_wm5102_mc_driver);
487
488MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver");
489MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
490MODULE_LICENSE("GPL v2");
491MODULE_ALIAS("platform:bytcr_wm5102");