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_LINE("Line Out", NULL),
135	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
136			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
137			    SND_SOC_DAPM_POST_PMD),
138	SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0,
139			    byt_wm5102_spkvdd_power_event,
140			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
141};
142
143static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = {
144	{"Headphone", NULL, "Platform Clock"},
145	{"Headset Mic", NULL, "Platform Clock"},
146	{"Internal Mic", NULL, "Platform Clock"},
147	{"Speaker", NULL, "Platform Clock"},
148	{"Line Out", NULL, "Platform Clock"},
149
150	{"Speaker", NULL, "SPKOUTLP"},
151	{"Speaker", NULL, "SPKOUTLN"},
152	{"Speaker", NULL, "SPKOUTRP"},
153	{"Speaker", NULL, "SPKOUTRN"},
154	{"Speaker", NULL, "Speaker VDD"},
155
156	{"Headphone", NULL, "HPOUT1L"},
157	{"Headphone", NULL, "HPOUT1R"},
158
159	{"Internal Mic", NULL, "MICBIAS3"},
160	{"IN3L", NULL, "Internal Mic"},
161
162	/*
163	 * The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset
164	 * is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch.
165	 */
166	{"Headset Mic", NULL, "MICBIAS1"},
167	{"Headset Mic", NULL, "MICBIAS2"},
168	{"IN1L", NULL, "Headset Mic"},
169
170	{"AIF1 Playback", NULL, "ssp0 Tx"},
171	{"ssp0 Tx", NULL, "modem_out"},
172
173	{"modem_in", NULL, "ssp0 Rx"},
174	{"ssp0 Rx", NULL, "AIF1 Capture"},
175};
176
177static const struct snd_kcontrol_new byt_wm5102_controls[] = {
178	SOC_DAPM_PIN_SWITCH("Headphone"),
179	SOC_DAPM_PIN_SWITCH("Headset Mic"),
180	SOC_DAPM_PIN_SWITCH("Internal Mic"),
181	SOC_DAPM_PIN_SWITCH("Speaker"),
182	SOC_DAPM_PIN_SWITCH("Line Out"),
183};
184
185static struct snd_soc_jack_pin byt_wm5102_pins[] = {
186	{
187		.pin	= "Headphone",
188		.mask	= SND_JACK_HEADPHONE,
189	},
190	{
191		.pin	= "Headset Mic",
192		.mask	= SND_JACK_MICROPHONE,
193	},
194	{
195		.pin	= "Line Out",
196		.mask	= SND_JACK_LINEOUT,
197	},
198};
199
200static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime)
201{
202	struct snd_soc_card *card = runtime->card;
203	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
204	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
205	int ret, jack_type;
206
207	card->dapm.idle_bias_off = true;
208
209	ret = snd_soc_add_card_controls(card, byt_wm5102_controls,
210					ARRAY_SIZE(byt_wm5102_controls));
211	if (ret) {
212		dev_err(card->dev, "Error adding card controls: %d\n", ret);
213		return ret;
214	}
215
216	/*
217	 * The firmware might enable the clock at boot (this information
218	 * may or may not be reflected in the enable clock register).
219	 * To change the rate we must disable the clock first to cover these
220	 * cases. Due to common clock framework restrictions that do not allow
221	 * to disable a clock that has not been enabled, we need to enable
222	 * the clock first.
223	 */
224	ret = clk_prepare_enable(priv->mclk);
225	if (!ret)
226		clk_disable_unprepare(priv->mclk);
227
228	ret = clk_set_rate(priv->mclk, MCLK_FREQ);
229	if (ret) {
230		dev_err(card->dev, "Error setting MCLK rate: %d\n", ret);
231		return ret;
232	}
233
234	jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
235		    SND_JACK_BTN_2 | SND_JACK_BTN_3;
236	ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type,
237					 &priv->jack, byt_wm5102_pins,
238					 ARRAY_SIZE(byt_wm5102_pins));
239	if (ret) {
240		dev_err(card->dev, "Error creating jack: %d\n", ret);
241		return ret;
242	}
243
244	snd_soc_component_set_jack(component, &priv->jack, NULL);
245
246	return 0;
247}
248
249static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd,
250				  struct snd_pcm_hw_params *params)
251{
252	struct snd_interval *rate = hw_param_interval(params,
253						      SNDRV_PCM_HW_PARAM_RATE);
254	struct snd_interval *channels = hw_param_interval(params,
255							  SNDRV_PCM_HW_PARAM_CHANNELS);
256	int ret;
257
258	/* The DSP will convert the FE rate to 48k, stereo */
259	rate->min = 48000;
260	rate->max = 48000;
261	channels->min = 2;
262	channels->max = 2;
263
264	/* set SSP0 to 16-bit */
265	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
266
267	/*
268	 * Default mode for SSP configuration is TDM 4 slot, override config
269	 * with explicit setting to I2S 2ch 16-bit. The word length is set with
270	 * dai_set_tdm_slot() since there is no other API exposed
271	 */
272	ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
273				  SND_SOC_DAIFMT_I2S     |
274				  SND_SOC_DAIFMT_NB_NF   |
275				  SND_SOC_DAIFMT_BP_FP);
276	if (ret) {
277		dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret);
278		return ret;
279	}
280
281	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 16);
282	if (ret) {
283		dev_err(rtd->dev, "Error setting I2S config: %d\n", ret);
284		return ret;
285	}
286
287	return 0;
288}
289
290static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream)
291{
292	return snd_pcm_hw_constraint_single(substream->runtime,
293					    SNDRV_PCM_HW_PARAM_RATE, 48000);
294}
295
296static const struct snd_soc_ops byt_wm5102_aif1_ops = {
297	.startup = byt_wm5102_aif1_startup,
298};
299
300SND_SOC_DAILINK_DEF(dummy,
301	DAILINK_COMP_ARRAY(COMP_DUMMY()));
302
303SND_SOC_DAILINK_DEF(media,
304	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
305
306SND_SOC_DAILINK_DEF(deepbuffer,
307	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
308
309SND_SOC_DAILINK_DEF(ssp0_port,
310	DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
311
312SND_SOC_DAILINK_DEF(ssp0_codec,
313	DAILINK_COMP_ARRAY(COMP_CODEC(
314	/*
315	 * Note there is no need to overwrite the codec-name as is done in
316	 * other bytcr machine drivers, because the codec is a MFD child-dev.
317	 */
318	"wm5102-codec",
319	"wm5102-aif1")));
320
321SND_SOC_DAILINK_DEF(platform,
322	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
323
324static struct snd_soc_dai_link byt_wm5102_dais[] = {
325	[MERR_DPCM_AUDIO] = {
326		.name = "Baytrail Audio Port",
327		.stream_name = "Baytrail Audio",
328		.nonatomic = true,
329		.dynamic = 1,
330		.dpcm_playback = 1,
331		.dpcm_capture = 1,
332		.ops = &byt_wm5102_aif1_ops,
333		SND_SOC_DAILINK_REG(media, dummy, platform),
334
335	},
336	[MERR_DPCM_DEEP_BUFFER] = {
337		.name = "Deep-Buffer Audio Port",
338		.stream_name = "Deep-Buffer Audio",
339		.nonatomic = true,
340		.dynamic = 1,
341		.dpcm_playback = 1,
342		.ops = &byt_wm5102_aif1_ops,
343		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
344	},
345		/* back ends */
346	{
347		/*
348		 * This must be named SSP2-Codec even though this machine driver
349		 * always uses SSP0. Most machine drivers support both and dynamically
350		 * update the dailink to point to SSP0 or SSP2, while keeping the name
351		 * as "SSP2-Codec". The SOF tplg files hardcode the "SSP2-Codec" even
352		 * in the byt-foo-ssp0.tplg versions because the other machine-drivers
353		 * use "SSP2-Codec" even when SSP0 is used.
354		 */
355		.name = "SSP2-Codec",
356		.id = 0,
357		.no_pcm = 1,
358		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
359						| SND_SOC_DAIFMT_CBC_CFC,
360		.be_hw_params_fixup = byt_wm5102_codec_fixup,
361		.dpcm_playback = 1,
362		.dpcm_capture = 1,
363		.init = byt_wm5102_init,
364		SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform),
365	},
366};
367
368/* use space before codec name to simplify card ID, and simplify driver name */
369#define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */
370#define SOF_DRIVER_NAME "SOF"
371
372#define CARD_NAME "bytcr-wm5102"
373#define DRIVER_NAME NULL /* card name will be used for driver name */
374
375/* SoC card */
376static struct snd_soc_card byt_wm5102_card = {
377	.owner = THIS_MODULE,
378	.dai_link = byt_wm5102_dais,
379	.num_links = ARRAY_SIZE(byt_wm5102_dais),
380	.dapm_widgets = byt_wm5102_widgets,
381	.num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets),
382	.dapm_routes = byt_wm5102_audio_map,
383	.num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map),
384	.fully_routed = true,
385};
386
387static int snd_byt_wm5102_mc_probe(struct platform_device *pdev)
388{
389	char codec_name[SND_ACPI_I2C_ID_LEN];
390	struct device *dev = &pdev->dev;
391	struct byt_wm5102_private *priv;
392	struct snd_soc_acpi_mach *mach;
393	const char *platform_name;
394	struct acpi_device *adev;
395	struct device *codec_dev;
396	bool sof_parent;
397	int ret;
398
399	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
400	if (!priv)
401		return -ENOMEM;
402
403	/* Get MCLK */
404	priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
405	if (IS_ERR(priv->mclk))
406		return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n");
407
408	/*
409	 * Get speaker VDD enable GPIO:
410	 * 1. Get codec-device-name
411	 * 2. Get codec-device
412	 * 3. Get GPIO from codec-device
413	 */
414	mach = dev->platform_data;
415	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
416	if (!adev) {
417		dev_err(dev, "Error cannot find acpi-dev for codec\n");
418		return -ENOENT;
419	}
420	snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev));
421
422	codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name);
423	acpi_dev_put(adev);
424	if (!codec_dev)
425		return -EPROBE_DEFER;
426
427	/* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */
428	priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW);
429	put_device(codec_dev);
430
431	if (IS_ERR(priv->spkvdd_en_gpio)) {
432		ret = PTR_ERR(priv->spkvdd_en_gpio);
433		/*
434		 * The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c,
435		 * so -ENOENT means that arizona-spi hasn't probed yet.
436		 */
437		if (ret == -ENOENT)
438			ret = -EPROBE_DEFER;
439
440		return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n");
441	}
442
443	/* override platform name, if required */
444	byt_wm5102_card.dev = dev;
445	platform_name = mach->mach_params.platform;
446	ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name);
447	if (ret)
448		goto out_put_gpio;
449
450	/* set card and driver name and pm-ops */
451	sof_parent = snd_soc_acpi_sof_parent(dev);
452	if (sof_parent) {
453		byt_wm5102_card.name = SOF_CARD_NAME;
454		byt_wm5102_card.driver_name = SOF_DRIVER_NAME;
455		dev->driver->pm = &snd_soc_pm_ops;
456	} else {
457		byt_wm5102_card.name = CARD_NAME;
458		byt_wm5102_card.driver_name = DRIVER_NAME;
459	}
460
461	snd_soc_card_set_drvdata(&byt_wm5102_card, priv);
462	ret = devm_snd_soc_register_card(dev, &byt_wm5102_card);
463	if (ret) {
464		dev_err_probe(dev, ret, "registering card\n");
465		goto out_put_gpio;
466	}
467
468	platform_set_drvdata(pdev, &byt_wm5102_card);
469	return 0;
470
471out_put_gpio:
472	gpiod_put(priv->spkvdd_en_gpio);
473	return ret;
474}
475
476static void snd_byt_wm5102_mc_remove(struct platform_device *pdev)
477{
478	struct snd_soc_card *card = platform_get_drvdata(pdev);
479	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
480
481	gpiod_put(priv->spkvdd_en_gpio);
482}
483
484static struct platform_driver snd_byt_wm5102_mc_driver = {
485	.driver = {
486		.name = "bytcr_wm5102",
487	},
488	.probe = snd_byt_wm5102_mc_probe,
489	.remove_new = snd_byt_wm5102_mc_remove,
490};
491
492module_platform_driver(snd_byt_wm5102_mc_driver);
493
494MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver");
495MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
496MODULE_LICENSE("GPL v2");
497MODULE_ALIAS("platform:bytcr_wm5102");
498