1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright(c) 2020 Intel Corporation. All rights reserved.
4#include <linux/string.h>
5#include <sound/pcm.h>
6#include <sound/soc.h>
7#include <sound/soc-dai.h>
8#include <sound/soc-dapm.h>
9#include <uapi/sound/asound.h>
10#include "sof_maxim_common.h"
11
12#define MAX_98373_PIN_NAME 16
13
14const struct snd_soc_dapm_route max_98373_dapm_routes[] = {
15	/* speaker */
16	{ "Left Spk", NULL, "Left BE_OUT" },
17	{ "Right Spk", NULL, "Right BE_OUT" },
18};
19
20static struct snd_soc_codec_conf max_98373_codec_conf[] = {
21	{
22		.dlc = COMP_CODEC_CONF(MAX_98373_DEV0_NAME),
23		.name_prefix = "Right",
24	},
25	{
26		.dlc = COMP_CODEC_CONF(MAX_98373_DEV1_NAME),
27		.name_prefix = "Left",
28	},
29};
30
31struct snd_soc_dai_link_component max_98373_components[] = {
32	{  /* For Right */
33		.name = MAX_98373_DEV0_NAME,
34		.dai_name = MAX_98373_CODEC_DAI,
35	},
36	{  /* For Left */
37		.name = MAX_98373_DEV1_NAME,
38		.dai_name = MAX_98373_CODEC_DAI,
39	},
40};
41
42static int max98373_hw_params(struct snd_pcm_substream *substream,
43			      struct snd_pcm_hw_params *params)
44{
45	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
46	struct snd_soc_dai *codec_dai;
47	int j;
48
49	for_each_rtd_codec_dais(rtd, j, codec_dai) {
50		if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) {
51			/* DEV0 tdm slot configuration */
52			snd_soc_dai_set_tdm_slot(codec_dai, 0x03, 3, 8, 32);
53		}
54		if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) {
55			/* DEV1 tdm slot configuration */
56			snd_soc_dai_set_tdm_slot(codec_dai, 0x0C, 3, 8, 32);
57		}
58	}
59	return 0;
60}
61
62int max98373_trigger(struct snd_pcm_substream *substream, int cmd)
63{
64	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
65	struct snd_soc_dai *codec_dai;
66	int j;
67	int ret = 0;
68
69	/* set spk pin by playback only */
70	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
71		return 0;
72
73	for_each_rtd_codec_dais(rtd, j, codec_dai) {
74		struct snd_soc_component *component = codec_dai->component;
75		struct snd_soc_dapm_context *dapm =
76				snd_soc_component_get_dapm(component);
77		char pin_name[MAX_98373_PIN_NAME];
78
79		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
80			 codec_dai->component->name_prefix);
81
82		switch (cmd) {
83		case SNDRV_PCM_TRIGGER_START:
84		case SNDRV_PCM_TRIGGER_RESUME:
85		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
86			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
87			if (!ret)
88				snd_soc_dapm_sync(dapm);
89			break;
90		case SNDRV_PCM_TRIGGER_STOP:
91		case SNDRV_PCM_TRIGGER_SUSPEND:
92		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
93			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
94			if (!ret)
95				snd_soc_dapm_sync(dapm);
96			break;
97		default:
98			break;
99		}
100	}
101
102	return ret;
103}
104
105struct snd_soc_ops max_98373_ops = {
106	.hw_params = max98373_hw_params,
107	.trigger = max98373_trigger,
108};
109
110int max98373_spk_codec_init(struct snd_soc_pcm_runtime *rtd)
111{
112	struct snd_soc_card *card = rtd->card;
113	int ret;
114
115	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes,
116				      ARRAY_SIZE(max_98373_dapm_routes));
117	if (ret)
118		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
119	return ret;
120}
121
122void sof_max98373_codec_conf(struct snd_soc_card *card)
123{
124	card->codec_conf = max_98373_codec_conf;
125	card->num_configs = ARRAY_SIZE(max_98373_codec_conf);
126}
127