xref: /linux/sound/soc/intel/avs/boards/max98373.c (revision db10cb9b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15 
16 #define MAX98373_DEV0_NAME	"i2c-MX98373:00"
17 #define MAX98373_DEV1_NAME	"i2c-MX98373:01"
18 #define MAX98373_CODEC_NAME	"max98373-aif1"
19 
20 static struct snd_soc_codec_conf card_codec_conf[] = {
21 	{
22 		.dlc = COMP_CODEC_CONF(MAX98373_DEV0_NAME),
23 		.name_prefix = "Right",
24 	},
25 	{
26 		.dlc = COMP_CODEC_CONF(MAX98373_DEV1_NAME),
27 		.name_prefix = "Left",
28 	},
29 };
30 
31 static const struct snd_kcontrol_new card_controls[] = {
32 	SOC_DAPM_PIN_SWITCH("Left Spk"),
33 	SOC_DAPM_PIN_SWITCH("Right Spk"),
34 };
35 
36 static const struct snd_soc_dapm_widget card_widgets[] = {
37 	SND_SOC_DAPM_SPK("Left Spk", NULL),
38 	SND_SOC_DAPM_SPK("Right Spk", NULL),
39 };
40 
41 static const struct snd_soc_dapm_route card_base_routes[] = {
42 	{ "Left Spk", NULL, "Left BE_OUT" },
43 	{ "Right Spk", NULL, "Right BE_OUT" },
44 };
45 
46 static int
47 avs_max98373_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
48 {
49 	struct snd_interval *rate, *channels;
50 	struct snd_mask *fmt;
51 
52 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
53 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
54 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
55 
56 	/* The ADSP will convert the FE rate to 48k, stereo */
57 	rate->min = rate->max = 48000;
58 	channels->min = channels->max = 2;
59 
60 	/* set SSP0 to 16 bit */
61 	snd_mask_none(fmt);
62 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
63 	return 0;
64 }
65 
66 static int avs_max98373_hw_params(struct snd_pcm_substream *substream,
67 				  struct snd_pcm_hw_params *params)
68 {
69 	struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream);
70 	struct snd_soc_dai *codec_dai;
71 	int ret, i;
72 
73 	for_each_rtd_codec_dais(runtime, i, codec_dai) {
74 		if (!strcmp(codec_dai->component->name, MAX98373_DEV0_NAME)) {
75 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
76 			if (ret < 0) {
77 				dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
78 				return ret;
79 			}
80 		}
81 		if (!strcmp(codec_dai->component->name, MAX98373_DEV1_NAME)) {
82 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
83 			if (ret < 0) {
84 				dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
85 				return ret;
86 			}
87 		}
88 	}
89 
90 	return 0;
91 }
92 
93 static const struct snd_soc_ops avs_max98373_ops = {
94 	.hw_params = avs_max98373_hw_params,
95 };
96 
97 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
98 			       struct snd_soc_dai_link **dai_link)
99 {
100 	struct snd_soc_dai_link_component *platform;
101 	struct snd_soc_dai_link *dl;
102 
103 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
104 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
105 	if (!dl || !platform)
106 		return -ENOMEM;
107 
108 	platform->name = platform_name;
109 
110 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
111 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
112 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
113 	if (!dl->name || !dl->cpus || !dl->codecs)
114 		return -ENOMEM;
115 
116 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
117 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV0_NAME);
118 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
119 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV1_NAME);
120 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
121 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
122 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
123 		return -ENOMEM;
124 
125 	dl->num_cpus = 1;
126 	dl->num_codecs = 2;
127 	dl->platforms = platform;
128 	dl->num_platforms = 1;
129 	dl->id = 0;
130 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
131 	dl->be_hw_params_fixup = avs_max98373_be_fixup;
132 	dl->nonatomic = 1;
133 	dl->no_pcm = 1;
134 	dl->dpcm_capture = 1;
135 	dl->dpcm_playback = 1;
136 	dl->ignore_pmdown_time = 1;
137 	dl->ops = &avs_max98373_ops;
138 
139 	*dai_link = dl;
140 
141 	return 0;
142 }
143 
144 static int avs_max98373_probe(struct platform_device *pdev)
145 {
146 	struct snd_soc_dai_link *dai_link;
147 	struct snd_soc_acpi_mach *mach;
148 	struct snd_soc_card *card;
149 	struct device *dev = &pdev->dev;
150 	const char *pname;
151 	int ssp_port, ret;
152 
153 	mach = dev_get_platdata(dev);
154 	pname = mach->mach_params.platform;
155 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
156 
157 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
158 	if (ret) {
159 		dev_err(dev, "Failed to create dai link: %d", ret);
160 		return ret;
161 	}
162 
163 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
164 	if (!card)
165 		return -ENOMEM;
166 
167 	card->name = "avs_max98373";
168 	card->dev = dev;
169 	card->owner = THIS_MODULE;
170 	card->dai_link = dai_link;
171 	card->num_links = 1;
172 	card->codec_conf = card_codec_conf;
173 	card->num_configs = ARRAY_SIZE(card_codec_conf);
174 	card->controls = card_controls;
175 	card->num_controls = ARRAY_SIZE(card_controls);
176 	card->dapm_widgets = card_widgets;
177 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
178 	card->dapm_routes = card_base_routes;
179 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
180 	card->fully_routed = true;
181 
182 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
183 	if (ret)
184 		return ret;
185 
186 	return devm_snd_soc_register_card(dev, card);
187 }
188 
189 static struct platform_driver avs_max98373_driver = {
190 	.probe = avs_max98373_probe,
191 	.driver = {
192 		.name = "avs_max98373",
193 		.pm = &snd_soc_pm_ops,
194 	},
195 };
196 
197 module_platform_driver(avs_max98373_driver)
198 
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS("platform:avs_max98373");
201