xref: /linux/sound/soc/intel/boards/sof_da7219.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2019 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine driver for Dialog headphone codec
6  */
7 
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <sound/jack.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <linux/platform_device.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/sof.h>
17 #include "../../codecs/da7219.h"
18 #include "sof_board_helpers.h"
19 #include "sof_maxim_common.h"
20 
21 /* Driver-specific board quirks: from bit 0 to 7 */
22 #define SOF_DA7219_GLK_BOARD			BIT(0)
23 #define SOF_DA7219_CML_BOARD			BIT(1)
24 #define SOF_DA7219_JSL_BOARD			BIT(2)
25 #define SOF_DA7219_MCLK_EN			BIT(3)
26 
27 #define DIALOG_CODEC_DAI	"da7219-hifi"
28 
29 static int platform_clock_control(struct snd_soc_dapm_widget *w,
30 				  struct snd_kcontrol *k, int  event)
31 {
32 	struct snd_soc_dapm_context *dapm = w->dapm;
33 	struct snd_soc_card *card = dapm->card;
34 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
35 	struct snd_soc_dai *codec_dai;
36 	int ret = 0;
37 
38 	if (ctx->da7219.pll_bypass)
39 		return ret;
40 
41 	/* PLL SRM mode */
42 	codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI);
43 	if (!codec_dai) {
44 		dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
45 		return -EIO;
46 	}
47 
48 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
49 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK,
50 					  0, 0);
51 		if (ret)
52 			dev_err(card->dev, "failed to stop PLL: %d\n", ret);
53 	} else if (SND_SOC_DAPM_EVENT_ON(event)) {
54 		dev_dbg(card->dev, "pll srm mode\n");
55 
56 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
57 					  0, DA7219_PLL_FREQ_OUT_98304);
58 		if (ret)
59 			dev_err(card->dev, "failed to start PLL: %d\n", ret);
60 	}
61 
62 	return ret;
63 }
64 
65 static const struct snd_kcontrol_new controls[] = {
66 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
67 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
68 	SOC_DAPM_PIN_SWITCH("Line Out"),
69 };
70 
71 static const struct snd_soc_dapm_widget widgets[] = {
72 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
73 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
74 	SND_SOC_DAPM_LINE("Line Out", NULL),
75 
76 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
77 			    platform_clock_control, SND_SOC_DAPM_POST_PMD |
78 			    SND_SOC_DAPM_PRE_PMU),
79 };
80 
81 static const struct snd_soc_dapm_route audio_map[] = {
82 	{ "Headphone Jack", NULL, "HPL" },
83 	{ "Headphone Jack", NULL, "HPR" },
84 
85 	{ "MIC", NULL, "Headset Mic" },
86 
87 	{ "Headphone Jack", NULL, "Platform Clock" },
88 	{ "Headset Mic", NULL, "Platform Clock" },
89 	{ "Line Out", NULL, "Platform Clock" },
90 };
91 
92 static struct snd_soc_jack_pin jack_pins[] = {
93 	{
94 		.pin    = "Headphone Jack",
95 		.mask   = SND_JACK_HEADPHONE,
96 	},
97 	{
98 		.pin    = "Headset Mic",
99 		.mask   = SND_JACK_MICROPHONE,
100 	},
101 	{
102 		.pin    = "Line Out",
103 		.mask   = SND_JACK_LINEOUT,
104 	},
105 };
106 
107 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
108 {
109 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
110 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
111 	struct snd_soc_component *component = codec_dai->component;
112 	struct snd_soc_jack *jack = &ctx->headset_jack;
113 	int mclk_rate, ret;
114 
115 	mclk_rate = sof_dai_get_mclk(rtd);
116 	if (mclk_rate <= 0) {
117 		dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_rate);
118 		return -EINVAL;
119 	}
120 
121 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, mclk_rate,
122 				     SND_SOC_CLOCK_IN);
123 	if (ret) {
124 		dev_err(rtd->dev, "fail to set sysclk, ret %d\n", ret);
125 		return ret;
126 	}
127 
128 	/*
129 	 * Use PLL bypass mode if MCLK is available, be sure to set the
130 	 * frequency of MCLK to 12.288 or 24.576MHz on topology side.
131 	 */
132 	if (ctx->da7219.mclk_en &&
133 	    (mclk_rate == 12288000 || mclk_rate == 24576000)) {
134 		/* PLL bypass mode */
135 		dev_dbg(rtd->dev, "pll bypass mode, mclk rate %d\n", mclk_rate);
136 
137 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
138 		if (ret) {
139 			dev_err(rtd->dev, "fail to set pll, ret %d\n", ret);
140 			return ret;
141 		}
142 
143 		ctx->da7219.pll_bypass = true;
144 	}
145 
146 	/*
147 	 * Headset buttons map to the google Reference headset.
148 	 * These can be configured by userspace.
149 	 */
150 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
151 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
152 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
153 					 SND_JACK_BTN_3 | SND_JACK_LINEOUT,
154 					 jack, jack_pins, ARRAY_SIZE(jack_pins));
155 	if (ret) {
156 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
157 		return ret;
158 	}
159 
160 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
161 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
162 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
163 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
164 
165 	ret = snd_soc_component_set_jack(component, jack, NULL);
166 	if (ret) {
167 		dev_err(rtd->dev, "fail to set component jack, ret %d\n", ret);
168 		return ret;
169 	}
170 
171 	return ret;
172 }
173 
174 static void da7219_codec_exit(struct snd_soc_pcm_runtime *rtd)
175 {
176 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
177 
178 	snd_soc_component_set_jack(component, NULL, NULL);
179 }
180 
181 static int max98373_hw_params(struct snd_pcm_substream *substream,
182 			      struct snd_pcm_hw_params *params)
183 {
184 	struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
185 	int ret, j;
186 
187 	for (j = 0; j < runtime->dai_link->num_codecs; j++) {
188 		struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, j);
189 
190 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) {
191 			/* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */
192 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16);
193 			if (ret < 0) {
194 				dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
195 				return ret;
196 			}
197 		}
198 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) {
199 			/* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */
200 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16);
201 			if (ret < 0) {
202 				dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
203 				return ret;
204 			}
205 		}
206 	}
207 
208 	return 0;
209 }
210 
211 static const struct snd_soc_ops max98373_ops = {
212 	.hw_params = max98373_hw_params,
213 };
214 
215 static int card_late_probe(struct snd_soc_card *card)
216 {
217 	return sof_intel_board_card_late_probe(card);
218 }
219 
220 static struct snd_soc_card card_da7219 = {
221 	.name = "da7219", /* the sof- prefix is added by the core */
222 	.owner = THIS_MODULE,
223 	.controls = controls,
224 	.num_controls = ARRAY_SIZE(controls),
225 	.dapm_widgets = widgets,
226 	.num_dapm_widgets = ARRAY_SIZE(widgets),
227 	.dapm_routes = audio_map,
228 	.num_dapm_routes = ARRAY_SIZE(audio_map),
229 	.fully_routed = true,
230 	.late_probe = card_late_probe,
231 };
232 
233 static struct snd_soc_dai_link_component da7219_component[] = {
234 	{
235 		.name = "i2c-DLGS7219:00",
236 		.dai_name = DIALOG_CODEC_DAI,
237 	}
238 };
239 
240 static int
241 sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card,
242 			  struct sof_card_private *ctx)
243 {
244 	int ret;
245 
246 	ret = sof_intel_board_set_dai_link(dev, card, ctx);
247 	if (ret)
248 		return ret;
249 
250 	if (!ctx->codec_link) {
251 		dev_err(dev, "codec link not available");
252 		return -EINVAL;
253 	}
254 
255 	/* codec-specific fields for headphone codec */
256 	ctx->codec_link->codecs = da7219_component;
257 	ctx->codec_link->num_codecs = ARRAY_SIZE(da7219_component);
258 	ctx->codec_link->init = da7219_codec_init;
259 	ctx->codec_link->exit = da7219_codec_exit;
260 
261 	if (ctx->amp_type == CODEC_NONE)
262 		return 0;
263 
264 	if (!ctx->amp_link) {
265 		dev_err(dev, "amp link not available");
266 		return -EINVAL;
267 	}
268 
269 	/* codec-specific fields for speaker amplifier */
270 	switch (ctx->amp_type) {
271 	case CODEC_MAX98357A:
272 		max_98357a_dai_link(ctx->amp_link);
273 		break;
274 	case CODEC_MAX98360A:
275 		max_98360a_dai_link(ctx->amp_link);
276 		break;
277 	case CODEC_MAX98373:
278 		max_98373_dai_link(dev, ctx->amp_link);
279 
280 		if (ctx->da7219.is_jsl_board) {
281 			ctx->amp_link->ops = &max98373_ops; /* use local ops */
282 		} else {
283 			/* TBD: implement the amp for later platform */
284 			dev_err(dev, "max98373 not support yet\n");
285 			return -EINVAL;
286 		}
287 		break;
288 	case CODEC_MAX98390:
289 		max_98390_dai_link(dev, ctx->amp_link);
290 		break;
291 	default:
292 		dev_err(dev, "invalid amp type %d\n", ctx->amp_type);
293 		return -EINVAL;
294 	}
295 
296 	return 0;
297 }
298 
299 #define GLK_LINK_ORDER	SOF_LINK_ORDER(SOF_LINK_AMP,         \
300 					SOF_LINK_CODEC,      \
301 					SOF_LINK_DMIC01,     \
302 					SOF_LINK_IDISP_HDMI, \
303 					SOF_LINK_NONE,       \
304 					SOF_LINK_NONE,       \
305 					SOF_LINK_NONE)
306 
307 #define CML_LINK_ORDER	SOF_LINK_ORDER(SOF_LINK_AMP,         \
308 					SOF_LINK_CODEC,      \
309 					SOF_LINK_DMIC01,     \
310 					SOF_LINK_IDISP_HDMI, \
311 					SOF_LINK_DMIC16K,    \
312 					SOF_LINK_NONE,       \
313 					SOF_LINK_NONE)
314 
315 #define JSL_LINK_ORDER	SOF_LINK_ORDER(SOF_LINK_AMP,         \
316 					SOF_LINK_CODEC,      \
317 					SOF_LINK_DMIC01,     \
318 					SOF_LINK_IDISP_HDMI, \
319 					SOF_LINK_DMIC16K,    \
320 					SOF_LINK_NONE,       \
321 					SOF_LINK_NONE)
322 
323 static int audio_probe(struct platform_device *pdev)
324 {
325 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
326 	struct sof_card_private *ctx;
327 	char *card_name;
328 	unsigned long board_quirk = 0;
329 	int ret;
330 
331 	if (pdev->id_entry && pdev->id_entry->driver_data)
332 		board_quirk = (unsigned long)pdev->id_entry->driver_data;
333 
334 	dev_dbg(&pdev->dev, "board_quirk = %lx\n", board_quirk);
335 
336 	/* initialize ctx with board quirk */
337 	ctx = sof_intel_board_get_ctx(&pdev->dev, board_quirk);
338 	if (!ctx)
339 		return -ENOMEM;
340 
341 	if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
342 		ctx->hdmi.idisp_codec = true;
343 
344 	if (board_quirk & SOF_DA7219_GLK_BOARD) {
345 		/* dmic16k not support */
346 		ctx->dmic_be_num = 1;
347 
348 		/* overwrite the DAI link order for GLK boards */
349 		ctx->link_order_overwrite = GLK_LINK_ORDER;
350 
351 		/* backward-compatible with existing devices */
352 		switch (ctx->amp_type) {
353 		case CODEC_MAX98357A:
354 			card_name = devm_kstrdup(&pdev->dev, "glkda7219max",
355 						 GFP_KERNEL);
356 			if (!card_name)
357 				return -ENOMEM;
358 
359 			card_da7219.name = card_name;
360 			break;
361 		default:
362 			break;
363 		}
364 	} else if (board_quirk & SOF_DA7219_CML_BOARD) {
365 		/* overwrite the DAI link order for CML boards */
366 		ctx->link_order_overwrite = CML_LINK_ORDER;
367 
368 		/* backward-compatible with existing devices */
369 		switch (ctx->amp_type) {
370 		case CODEC_MAX98357A:
371 			card_name = devm_kstrdup(&pdev->dev, "cmlda7219max",
372 						 GFP_KERNEL);
373 			if (!card_name)
374 				return -ENOMEM;
375 
376 			card_da7219.name = card_name;
377 			break;
378 		case CODEC_MAX98390:
379 			card_name = devm_kstrdup(&pdev->dev,
380 						 "cml_max98390_da7219",
381 						 GFP_KERNEL);
382 			if (!card_name)
383 				return -ENOMEM;
384 
385 			card_da7219.name = card_name;
386 			break;
387 		default:
388 			break;
389 		}
390 	} else if (board_quirk & SOF_DA7219_JSL_BOARD) {
391 		ctx->da7219.is_jsl_board = true;
392 
393 		/* overwrite the DAI link order for JSL boards */
394 		ctx->link_order_overwrite = JSL_LINK_ORDER;
395 
396 		/* backward-compatible with existing devices */
397 		switch (ctx->amp_type) {
398 		case CODEC_MAX98360A:
399 			card_name = devm_kstrdup(&pdev->dev, "da7219max98360a",
400 						 GFP_KERNEL);
401 			if (!card_name)
402 				return -ENOMEM;
403 
404 			card_da7219.name = card_name;
405 			break;
406 		case CODEC_MAX98373:
407 			card_name = devm_kstrdup(&pdev->dev, "da7219max",
408 						 GFP_KERNEL);
409 			if (!card_name)
410 				return -ENOMEM;
411 
412 			card_da7219.name = card_name;
413 			break;
414 		default:
415 			break;
416 		}
417 	}
418 
419 	if (board_quirk & SOF_DA7219_MCLK_EN)
420 		ctx->da7219.mclk_en = true;
421 
422 	/* update dai_link */
423 	ret = sof_card_dai_links_create(&pdev->dev, &card_da7219, ctx);
424 	if (ret)
425 		return ret;
426 
427 	/* update codec_conf */
428 	switch (ctx->amp_type) {
429 	case CODEC_MAX98373:
430 		max_98373_set_codec_conf(&card_da7219);
431 		break;
432 	case CODEC_MAX98390:
433 		max_98390_set_codec_conf(&pdev->dev, &card_da7219);
434 		break;
435 	case CODEC_MAX98357A:
436 	case CODEC_MAX98360A:
437 	case CODEC_NONE:
438 		/* no codec conf required */
439 		break;
440 	default:
441 		dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
442 		return -EINVAL;
443 	}
444 
445 	card_da7219.dev = &pdev->dev;
446 
447 	ret = snd_soc_fixup_dai_links_platform_name(&card_da7219,
448 						    mach->mach_params.platform);
449 	if (ret)
450 		return ret;
451 
452 	snd_soc_card_set_drvdata(&card_da7219, ctx);
453 
454 	return devm_snd_soc_register_card(&pdev->dev, &card_da7219);
455 }
456 
457 static const struct platform_device_id board_ids[] = {
458 	{
459 		.name = "glk_da7219_def",
460 		.driver_data = (kernel_ulong_t)(SOF_DA7219_GLK_BOARD |
461 					SOF_SSP_PORT_CODEC(2) |
462 					SOF_SSP_PORT_AMP(1)),
463 	},
464 	{
465 		.name = "cml_da7219_def",
466 		.driver_data = (kernel_ulong_t)(SOF_DA7219_CML_BOARD |
467 					SOF_SSP_PORT_CODEC(0) |
468 					SOF_SSP_PORT_AMP(1)),
469 	},
470 	{
471 		.name = "jsl_da7219_def",
472 		.driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD |
473 					SOF_SSP_PORT_CODEC(0) |
474 					SOF_SSP_PORT_AMP(1)),
475 	},
476 	{
477 		.name = "adl_da7219_def",
478 		.driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN |
479 					SOF_SSP_PORT_CODEC(0) |
480 					SOF_SSP_PORT_AMP(1) |
481 					SOF_NUM_IDISP_HDMI(4) |
482 					SOF_SSP_PORT_BT_OFFLOAD(2) |
483 					SOF_BT_OFFLOAD_PRESENT),
484 	},
485 	{
486 		.name = "rpl_da7219_def",
487 		.driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN |
488 					SOF_SSP_PORT_CODEC(0) |
489 					SOF_SSP_PORT_AMP(1) |
490 					SOF_NUM_IDISP_HDMI(4) |
491 					SOF_SSP_PORT_BT_OFFLOAD(2) |
492 					SOF_BT_OFFLOAD_PRESENT),
493 	},
494 	{
495 		.name = "mtl_da7219_def",
496 		.driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN |
497 					SOF_SSP_PORT_CODEC(2) |
498 					SOF_SSP_PORT_AMP(0) |
499 					SOF_SSP_PORT_BT_OFFLOAD(1) |
500 					SOF_BT_OFFLOAD_PRESENT),
501 	},
502 	{ }
503 };
504 MODULE_DEVICE_TABLE(platform, board_ids);
505 
506 static struct platform_driver audio = {
507 	.probe = audio_probe,
508 	.driver = {
509 		.name = "sof_da7219",
510 		.pm = &snd_soc_pm_ops,
511 	},
512 	.id_table = board_ids,
513 };
514 module_platform_driver(audio)
515 
516 /* Module information */
517 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver for Dialog codec");
518 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
519 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
520 MODULE_LICENSE("GPL v2");
521 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_BOARD_HELPERS);
522 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
523