185565f80SUday M Bhat // SPDX-License-Identifier: GPL-2.0-only
285565f80SUday M Bhat // Copyright (c) 2020 Intel Corporation
385565f80SUday M Bhat //
4fcb3f0fbSUday M Bhat // sof_sdw_maxim - Helpers to handle maxim codecs
585565f80SUday M Bhat // codec devices from generic machine driver
685565f80SUday M Bhat 
785565f80SUday M Bhat #include <linux/device.h>
885565f80SUday M Bhat #include <linux/errno.h>
985565f80SUday M Bhat #include <sound/control.h>
1085565f80SUday M Bhat #include <sound/soc.h>
1185565f80SUday M Bhat #include <sound/soc-acpi.h>
1285565f80SUday M Bhat #include <sound/soc-dapm.h>
1385565f80SUday M Bhat #include "sof_sdw_common.h"
1485565f80SUday M Bhat #include "sof_maxim_common.h"
1585565f80SUday M Bhat 
16fcb3f0fbSUday M Bhat static int maxim_part_id;
17dea4138dSUday M Bhat #define SOF_SDW_PART_ID_MAX98363 0x8363
18fcb3f0fbSUday M Bhat #define SOF_SDW_PART_ID_MAX98373 0x8373
19fcb3f0fbSUday M Bhat 
20fcb3f0fbSUday M Bhat static const struct snd_soc_dapm_widget maxim_widgets[] = {
2185565f80SUday M Bhat 	SND_SOC_DAPM_SPK("Left Spk", NULL),
2285565f80SUday M Bhat 	SND_SOC_DAPM_SPK("Right Spk", NULL),
2385565f80SUday M Bhat };
2485565f80SUday M Bhat 
25fcb3f0fbSUday M Bhat static const struct snd_kcontrol_new maxim_controls[] = {
2685565f80SUday M Bhat 	SOC_DAPM_PIN_SWITCH("Left Spk"),
2785565f80SUday M Bhat 	SOC_DAPM_PIN_SWITCH("Right Spk"),
2885565f80SUday M Bhat };
2985565f80SUday M Bhat 
spk_init(struct snd_soc_pcm_runtime * rtd)3085565f80SUday M Bhat static int spk_init(struct snd_soc_pcm_runtime *rtd)
3185565f80SUday M Bhat {
3285565f80SUday M Bhat 	struct snd_soc_card *card = rtd->card;
3385565f80SUday M Bhat 	int ret;
3485565f80SUday M Bhat 
3585565f80SUday M Bhat 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
36fcb3f0fbSUday M Bhat 					  "%s spk:mx%04x",
37fcb3f0fbSUday M Bhat 					  card->components, maxim_part_id);
3885565f80SUday M Bhat 	if (!card->components)
3985565f80SUday M Bhat 		return -ENOMEM;
4085565f80SUday M Bhat 
41fcb3f0fbSUday M Bhat 	dev_dbg(card->dev, "soundwire maxim card components assigned : %s\n",
42fcb3f0fbSUday M Bhat 		card->components);
43fcb3f0fbSUday M Bhat 
44fcb3f0fbSUday M Bhat 	ret = snd_soc_add_card_controls(card, maxim_controls,
45fcb3f0fbSUday M Bhat 					ARRAY_SIZE(maxim_controls));
4685565f80SUday M Bhat 	if (ret) {
47fcb3f0fbSUday M Bhat 		dev_err(card->dev, "mx%04x ctrls addition failed: %d\n", maxim_part_id, ret);
4885565f80SUday M Bhat 		return ret;
4985565f80SUday M Bhat 	}
5085565f80SUday M Bhat 
51fcb3f0fbSUday M Bhat 	ret = snd_soc_dapm_new_controls(&card->dapm, maxim_widgets,
52fcb3f0fbSUday M Bhat 					ARRAY_SIZE(maxim_widgets));
5385565f80SUday M Bhat 	if (ret) {
54fcb3f0fbSUday M Bhat 		dev_err(card->dev, "mx%04x widgets addition failed: %d\n", maxim_part_id, ret);
5585565f80SUday M Bhat 		return ret;
5685565f80SUday M Bhat 	}
5785565f80SUday M Bhat 
5885565f80SUday M Bhat 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
5985565f80SUday M Bhat 	if (ret)
6085565f80SUday M Bhat 		dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
6185565f80SUday M Bhat 
6285565f80SUday M Bhat 	return ret;
6385565f80SUday M Bhat }
6485565f80SUday M Bhat 
mx8373_enable_spk_pin(struct snd_pcm_substream * substream,bool enable)6585565f80SUday M Bhat static int mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
6685565f80SUday M Bhat {
6785565f80SUday M Bhat 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
6885565f80SUday M Bhat 	struct snd_soc_dai *codec_dai;
6985565f80SUday M Bhat 	struct snd_soc_dai *cpu_dai;
7085565f80SUday M Bhat 	int ret;
7185565f80SUday M Bhat 	int j;
7285565f80SUday M Bhat 
7385565f80SUday M Bhat 	/* set spk pin by playback only */
7485565f80SUday M Bhat 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
7585565f80SUday M Bhat 		return 0;
7685565f80SUday M Bhat 
7785565f80SUday M Bhat 	cpu_dai = asoc_rtd_to_cpu(rtd, 0);
7885565f80SUday M Bhat 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
7985565f80SUday M Bhat 		struct snd_soc_dapm_context *dapm =
8085565f80SUday M Bhat 				snd_soc_component_get_dapm(cpu_dai->component);
8185565f80SUday M Bhat 		char pin_name[16];
8285565f80SUday M Bhat 
8385565f80SUday M Bhat 		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
8485565f80SUday M Bhat 			 codec_dai->component->name_prefix);
8585565f80SUday M Bhat 
8685565f80SUday M Bhat 		if (enable)
8785565f80SUday M Bhat 			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
8885565f80SUday M Bhat 		else
8985565f80SUday M Bhat 			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
9085565f80SUday M Bhat 
9185565f80SUday M Bhat 		if (!ret)
9285565f80SUday M Bhat 			snd_soc_dapm_sync(dapm);
9385565f80SUday M Bhat 	}
9485565f80SUday M Bhat 
9585565f80SUday M Bhat 	return 0;
9685565f80SUday M Bhat }
9785565f80SUday M Bhat 
mx8373_sdw_prepare(struct snd_pcm_substream * substream)9885565f80SUday M Bhat static int mx8373_sdw_prepare(struct snd_pcm_substream *substream)
9985565f80SUday M Bhat {
10085565f80SUday M Bhat 	int ret;
10185565f80SUday M Bhat 
10285565f80SUday M Bhat 	/* according to soc_pcm_prepare dai link prepare is called first */
10385565f80SUday M Bhat 	ret = sdw_prepare(substream);
10485565f80SUday M Bhat 	if (ret < 0)
10585565f80SUday M Bhat 		return ret;
10685565f80SUday M Bhat 
10785565f80SUday M Bhat 	return mx8373_enable_spk_pin(substream, true);
10885565f80SUday M Bhat }
10985565f80SUday M Bhat 
mx8373_sdw_hw_free(struct snd_pcm_substream * substream)11085565f80SUday M Bhat static int mx8373_sdw_hw_free(struct snd_pcm_substream *substream)
11185565f80SUday M Bhat {
11285565f80SUday M Bhat 	int ret;
11385565f80SUday M Bhat 
11485565f80SUday M Bhat 	/* according to soc_pcm_hw_free dai link free is called first */
11585565f80SUday M Bhat 	ret = sdw_hw_free(substream);
11685565f80SUday M Bhat 	if (ret < 0)
11785565f80SUday M Bhat 		return ret;
11885565f80SUday M Bhat 
11985565f80SUday M Bhat 	return mx8373_enable_spk_pin(substream, false);
12085565f80SUday M Bhat }
12185565f80SUday M Bhat 
12285565f80SUday M Bhat static const struct snd_soc_ops max_98373_sdw_ops = {
12385565f80SUday M Bhat 	.startup = sdw_startup,
12485565f80SUday M Bhat 	.prepare = mx8373_sdw_prepare,
12585565f80SUday M Bhat 	.trigger = sdw_trigger,
126*0281b02eSBard Liao 	.hw_params = sdw_hw_params,
12785565f80SUday M Bhat 	.hw_free = mx8373_sdw_hw_free,
12885565f80SUday M Bhat 	.shutdown = sdw_shutdown,
12985565f80SUday M Bhat };
13085565f80SUday M Bhat 
mx8373_sdw_late_probe(struct snd_soc_card * card)13185565f80SUday M Bhat static int mx8373_sdw_late_probe(struct snd_soc_card *card)
13285565f80SUday M Bhat {
13385565f80SUday M Bhat 	struct snd_soc_dapm_context *dapm = &card->dapm;
13485565f80SUday M Bhat 
13585565f80SUday M Bhat 	/* Disable Left and Right Spk pin after boot */
13685565f80SUday M Bhat 	snd_soc_dapm_disable_pin(dapm, "Left Spk");
13785565f80SUday M Bhat 	snd_soc_dapm_disable_pin(dapm, "Right Spk");
13885565f80SUday M Bhat 	return snd_soc_dapm_sync(dapm);
13985565f80SUday M Bhat }
14085565f80SUday M Bhat 
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)141fcb3f0fbSUday M Bhat int sof_sdw_maxim_init(struct snd_soc_card *card,
14285565f80SUday M Bhat 		       const struct snd_soc_acpi_link_adr *link,
14385565f80SUday M Bhat 		       struct snd_soc_dai_link *dai_links,
14485565f80SUday M Bhat 		       struct sof_sdw_codec_info *info,
14585565f80SUday M Bhat 		       bool playback)
14685565f80SUday M Bhat {
14785565f80SUday M Bhat 	info->amp_num++;
14885565f80SUday M Bhat 	if (info->amp_num == 2)
14985565f80SUday M Bhat 		dai_links->init = spk_init;
15085565f80SUday M Bhat 
151fcb3f0fbSUday M Bhat 	maxim_part_id = info->part_id;
152fcb3f0fbSUday M Bhat 	switch (maxim_part_id) {
153dea4138dSUday M Bhat 	case SOF_SDW_PART_ID_MAX98363:
154dea4138dSUday M Bhat 		/* Default ops are set in function init_dai_link.
155dea4138dSUday M Bhat 		 * called as part of function create_sdw_dailink
156dea4138dSUday M Bhat 		 */
157dea4138dSUday M Bhat 		break;
158fcb3f0fbSUday M Bhat 	case SOF_SDW_PART_ID_MAX98373:
15985565f80SUday M Bhat 		info->codec_card_late_probe = mx8373_sdw_late_probe;
16085565f80SUday M Bhat 		dai_links->ops = &max_98373_sdw_ops;
161fcb3f0fbSUday M Bhat 		break;
162fcb3f0fbSUday M Bhat 	default:
163fcb3f0fbSUday M Bhat 		dev_err(card->dev, "Invalid maxim_part_id %#x\n", maxim_part_id);
164fcb3f0fbSUday M Bhat 		return -EINVAL;
165fcb3f0fbSUday M Bhat 	}
16685565f80SUday M Bhat 	return 0;
16785565f80SUday M Bhat }
168