1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3 //
4 // sof_sdw_maxim - Helpers to handle maxim codecs
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 
16 static int maxim_part_id;
17 #define SOF_SDW_PART_ID_MAX98363 0x8363
18 #define SOF_SDW_PART_ID_MAX98373 0x8373
19 
20 static const struct snd_soc_dapm_widget maxim_widgets[] = {
21 	SND_SOC_DAPM_SPK("Left Spk", NULL),
22 	SND_SOC_DAPM_SPK("Right Spk", NULL),
23 };
24 
25 static const struct snd_kcontrol_new maxim_controls[] = {
26 	SOC_DAPM_PIN_SWITCH("Left Spk"),
27 	SOC_DAPM_PIN_SWITCH("Right Spk"),
28 };
29 
spk_init(struct snd_soc_pcm_runtime * rtd)30 static int spk_init(struct snd_soc_pcm_runtime *rtd)
31 {
32 	struct snd_soc_card *card = rtd->card;
33 	int ret;
34 
35 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
36 					  "%s spk:mx%04x",
37 					  card->components, maxim_part_id);
38 	if (!card->components)
39 		return -ENOMEM;
40 
41 	dev_dbg(card->dev, "soundwire maxim card components assigned : %s\n",
42 		card->components);
43 
44 	ret = snd_soc_add_card_controls(card, maxim_controls,
45 					ARRAY_SIZE(maxim_controls));
46 	if (ret) {
47 		dev_err(card->dev, "mx%04x ctrls addition failed: %d\n", maxim_part_id, ret);
48 		return ret;
49 	}
50 
51 	ret = snd_soc_dapm_new_controls(&card->dapm, maxim_widgets,
52 					ARRAY_SIZE(maxim_widgets));
53 	if (ret) {
54 		dev_err(card->dev, "mx%04x widgets addition failed: %d\n", maxim_part_id, ret);
55 		return ret;
56 	}
57 
58 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
59 	if (ret)
60 		dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
61 
62 	return ret;
63 }
64 
mx8373_enable_spk_pin(struct snd_pcm_substream * substream,bool enable)65 static int mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
66 {
67 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
68 	struct snd_soc_dai *codec_dai;
69 	struct snd_soc_dai *cpu_dai;
70 	int ret;
71 	int j;
72 
73 	/* set spk pin by playback only */
74 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
75 		return 0;
76 
77 	cpu_dai = asoc_rtd_to_cpu(rtd, 0);
78 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
79 		struct snd_soc_dapm_context *dapm =
80 				snd_soc_component_get_dapm(cpu_dai->component);
81 		char pin_name[16];
82 
83 		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
84 			 codec_dai->component->name_prefix);
85 
86 		if (enable)
87 			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
88 		else
89 			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
90 
91 		if (!ret)
92 			snd_soc_dapm_sync(dapm);
93 	}
94 
95 	return 0;
96 }
97 
mx8373_sdw_prepare(struct snd_pcm_substream * substream)98 static int mx8373_sdw_prepare(struct snd_pcm_substream *substream)
99 {
100 	int ret;
101 
102 	/* according to soc_pcm_prepare dai link prepare is called first */
103 	ret = sdw_prepare(substream);
104 	if (ret < 0)
105 		return ret;
106 
107 	return mx8373_enable_spk_pin(substream, true);
108 }
109 
mx8373_sdw_hw_free(struct snd_pcm_substream * substream)110 static int mx8373_sdw_hw_free(struct snd_pcm_substream *substream)
111 {
112 	int ret;
113 
114 	/* according to soc_pcm_hw_free dai link free is called first */
115 	ret = sdw_hw_free(substream);
116 	if (ret < 0)
117 		return ret;
118 
119 	return mx8373_enable_spk_pin(substream, false);
120 }
121 
122 static const struct snd_soc_ops max_98373_sdw_ops = {
123 	.startup = sdw_startup,
124 	.prepare = mx8373_sdw_prepare,
125 	.trigger = sdw_trigger,
126 	.hw_params = sdw_hw_params,
127 	.hw_free = mx8373_sdw_hw_free,
128 	.shutdown = sdw_shutdown,
129 };
130 
mx8373_sdw_late_probe(struct snd_soc_card * card)131 static int mx8373_sdw_late_probe(struct snd_soc_card *card)
132 {
133 	struct snd_soc_dapm_context *dapm = &card->dapm;
134 
135 	/* Disable Left and Right Spk pin after boot */
136 	snd_soc_dapm_disable_pin(dapm, "Left Spk");
137 	snd_soc_dapm_disable_pin(dapm, "Right Spk");
138 	return snd_soc_dapm_sync(dapm);
139 }
140 
sof_sdw_maxim_init(struct snd_soc_card * card,const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,struct sof_sdw_codec_info * info,bool playback)141 int sof_sdw_maxim_init(struct snd_soc_card *card,
142 		       const struct snd_soc_acpi_link_adr *link,
143 		       struct snd_soc_dai_link *dai_links,
144 		       struct sof_sdw_codec_info *info,
145 		       bool playback)
146 {
147 	info->amp_num++;
148 	if (info->amp_num == 2)
149 		dai_links->init = spk_init;
150 
151 	maxim_part_id = info->part_id;
152 	switch (maxim_part_id) {
153 	case SOF_SDW_PART_ID_MAX98363:
154 		/* Default ops are set in function init_dai_link.
155 		 * called as part of function create_sdw_dailink
156 		 */
157 		break;
158 	case SOF_SDW_PART_ID_MAX98373:
159 		info->codec_card_late_probe = mx8373_sdw_late_probe;
160 		dai_links->ops = &max_98373_sdw_ops;
161 		break;
162 	default:
163 		dev_err(card->dev, "Invalid maxim_part_id %#x\n", maxim_part_id);
164 		return -EINVAL;
165 	}
166 	return 0;
167 }
168