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 static 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 Left */
33 		.name = MAX_98373_DEV0_NAME,
34 		.dai_name = MAX_98373_CODEC_DAI,
35 	},
36 	{  /* For Right */
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 = substream->private_data;
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, 0x30, 3, 8, 16);
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, 0xC0, 3, 8, 16);
57 		}
58 	}
59 	return 0;
60 }
61 
62 static int max98373_trigger(struct snd_pcm_substream *substream, int cmd)
63 {
64 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
65 	struct snd_soc_dai *codec_dai;
66 	int j;
67 	int ret = 0;
68 
69 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
70 		struct snd_soc_component *component = codec_dai->component;
71 		struct snd_soc_dapm_context *dapm =
72 				snd_soc_component_get_dapm(component);
73 		char pin_name[MAX_98373_PIN_NAME];
74 
75 		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
76 			 codec_dai->component->name_prefix);
77 
78 		switch (cmd) {
79 		case SNDRV_PCM_TRIGGER_START:
80 		case SNDRV_PCM_TRIGGER_RESUME:
81 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
82 			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
83 			if (!ret)
84 				snd_soc_dapm_sync(dapm);
85 			break;
86 		case SNDRV_PCM_TRIGGER_STOP:
87 		case SNDRV_PCM_TRIGGER_SUSPEND:
88 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
89 			/* Make sure no streams are active before disable pin */
90 			if (snd_soc_dai_active(codec_dai) != 1)
91 				break;
92 			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
93 			if (!ret)
94 				snd_soc_dapm_sync(dapm);
95 			break;
96 		default:
97 			break;
98 		}
99 	}
100 
101 	return ret;
102 }
103 
104 struct snd_soc_ops max_98373_ops = {
105 	.hw_params = max98373_hw_params,
106 	.trigger = max98373_trigger,
107 };
108 
109 int max98373_spk_codec_init(struct snd_soc_pcm_runtime *rtd)
110 {
111 	struct snd_soc_card *card = rtd->card;
112 	int ret;
113 
114 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes,
115 				      ARRAY_SIZE(max_98373_dapm_routes));
116 	if (ret)
117 		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
118 	return ret;
119 }
120 
121 void sof_max98373_codec_conf(struct snd_soc_card *card)
122 {
123 	card->codec_conf = max_98373_codec_conf;
124 	card->num_configs = ARRAY_SIZE(max_98373_codec_conf);
125 }
126