1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2020 Intel Corporation
3//
4// sof_sdw_max98373 - Helpers to handle 2x MAX98373
5// codec devices from generic machine driver
6
7#include <linux/device.h>
8#include <linux/errno.h>
9#include <sound/control.h>
10#include <sound/soc.h>
11#include <sound/soc-acpi.h>
12#include <sound/soc-dapm.h>
13#include "sof_sdw_common.h"
14#include "sof_maxim_common.h"
15
16static const struct snd_soc_dapm_widget mx8373_widgets[] = {
17	SND_SOC_DAPM_SPK("Left Spk", NULL),
18	SND_SOC_DAPM_SPK("Right Spk", NULL),
19};
20
21static const struct snd_kcontrol_new mx8373_controls[] = {
22	SOC_DAPM_PIN_SWITCH("Left Spk"),
23	SOC_DAPM_PIN_SWITCH("Right Spk"),
24};
25
26static int spk_init(struct snd_soc_pcm_runtime *rtd)
27{
28	struct snd_soc_card *card = rtd->card;
29	int ret;
30
31	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
32					  "%s spk:mx8373",
33					  card->components);
34	if (!card->components)
35		return -ENOMEM;
36
37	ret = snd_soc_add_card_controls(card, mx8373_controls,
38					ARRAY_SIZE(mx8373_controls));
39	if (ret) {
40		dev_err(card->dev, "mx8373 ctrls addition failed: %d\n", ret);
41		return ret;
42	}
43
44	ret = snd_soc_dapm_new_controls(&card->dapm, mx8373_widgets,
45					ARRAY_SIZE(mx8373_widgets));
46	if (ret) {
47		dev_err(card->dev, "mx8373 widgets addition failed: %d\n", ret);
48		return ret;
49	}
50
51	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
52	if (ret)
53		dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
54
55	return ret;
56}
57
58static int max98373_sdw_trigger(struct snd_pcm_substream *substream, int cmd)
59{
60	int ret;
61
62	switch (cmd) {
63	case SNDRV_PCM_TRIGGER_START:
64	case SNDRV_PCM_TRIGGER_RESUME:
65	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
66		/* enable max98373 first */
67		ret = max98373_trigger(substream, cmd);
68		if (ret < 0)
69			break;
70
71		ret = sdw_trigger(substream, cmd);
72		break;
73	case SNDRV_PCM_TRIGGER_STOP:
74	case SNDRV_PCM_TRIGGER_SUSPEND:
75	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
76		ret = sdw_trigger(substream, cmd);
77		if (ret < 0)
78			break;
79
80		ret = max98373_trigger(substream, cmd);
81		break;
82	default:
83		ret = -EINVAL;
84		break;
85	}
86
87	return ret;
88}
89
90static const struct snd_soc_ops max_98373_sdw_ops = {
91	.startup = sdw_startup,
92	.prepare = sdw_prepare,
93	.trigger = max98373_sdw_trigger,
94	.hw_free = sdw_hw_free,
95	.shutdown = sdw_shutdown,
96};
97
98int sof_sdw_mx8373_init(const struct snd_soc_acpi_link_adr *link,
99			struct snd_soc_dai_link *dai_links,
100			struct sof_sdw_codec_info *info,
101			bool playback)
102{
103	info->amp_num++;
104	if (info->amp_num == 2)
105		dai_links->init = spk_init;
106
107	info->late_probe = true;
108
109	dai_links->ops = &max_98373_sdw_ops;
110
111	return 0;
112}
113
114int sof_sdw_mx8373_late_probe(struct snd_soc_card *card)
115{
116	struct snd_soc_dapm_context *dapm = &card->dapm;
117
118	/* Disable Left and Right Spk pin after boot */
119	snd_soc_dapm_disable_pin(dapm, "Left Spk");
120	snd_soc_dapm_disable_pin(dapm, "Right Spk");
121	return snd_soc_dapm_sync(dapm);
122}
123