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 
14 const 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 
20 static 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 
31 struct 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 
42 static 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 
62 int 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 	struct snd_soc_dai *cpu_dai;
67 	int j;
68 	int ret = 0;
69 
70 	/* set spk pin by playback only */
71 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
72 		return 0;
73 
74 	cpu_dai = asoc_rtd_to_cpu(rtd, 0);
75 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
76 		struct snd_soc_dapm_context *dapm =
77 				snd_soc_component_get_dapm(cpu_dai->component);
78 		char pin_name[MAX_98373_PIN_NAME];
79 
80 		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
81 			 codec_dai->component->name_prefix);
82 
83 		switch (cmd) {
84 		case SNDRV_PCM_TRIGGER_START:
85 		case SNDRV_PCM_TRIGGER_RESUME:
86 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
87 			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
88 			if (!ret)
89 				snd_soc_dapm_sync(dapm);
90 			break;
91 		case SNDRV_PCM_TRIGGER_STOP:
92 		case SNDRV_PCM_TRIGGER_SUSPEND:
93 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
94 			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
95 			if (!ret)
96 				snd_soc_dapm_sync(dapm);
97 			break;
98 		default:
99 			break;
100 		}
101 	}
102 
103 	return ret;
104 }
105 
106 struct snd_soc_ops max_98373_ops = {
107 	.hw_params = max98373_hw_params,
108 	.trigger = max98373_trigger,
109 };
110 
111 int max98373_spk_codec_init(struct snd_soc_pcm_runtime *rtd)
112 {
113 	struct snd_soc_card *card = rtd->card;
114 	int ret;
115 
116 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes,
117 				      ARRAY_SIZE(max_98373_dapm_routes));
118 	if (ret)
119 		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
120 	return ret;
121 }
122 
123 void sof_max98373_codec_conf(struct snd_soc_card *card)
124 {
125 	card->codec_conf = max_98373_codec_conf;
126 	card->num_configs = ARRAY_SIZE(max_98373_codec_conf);
127 }
128