xref: /linux/sound/soc/intel/avs/boards/max98927.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation
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 #include "../utils.h"
16 
17 #define MAX98927_DEV0_NAME	"i2c-MX98927:00"
18 #define MAX98927_DEV1_NAME	"i2c-MX98927:01"
19 #define MAX98927_CODEC_NAME	"max98927-aif1"
20 
21 static struct snd_soc_codec_conf card_codec_conf[] = {
22 	{
23 		.dlc = COMP_CODEC_CONF(MAX98927_DEV0_NAME),
24 		.name_prefix = "Right",
25 	},
26 	{
27 		.dlc = COMP_CODEC_CONF(MAX98927_DEV1_NAME),
28 		.name_prefix = "Left",
29 	},
30 };
31 
32 static const struct snd_kcontrol_new card_controls[] = {
33 	SOC_DAPM_PIN_SWITCH("Left Spk"),
34 	SOC_DAPM_PIN_SWITCH("Right Spk"),
35 };
36 
37 static const struct snd_soc_dapm_widget card_widgets[] = {
38 	SND_SOC_DAPM_SPK("Left Spk", NULL),
39 	SND_SOC_DAPM_SPK("Right Spk", NULL),
40 };
41 
42 static const struct snd_soc_dapm_route card_base_routes[] = {
43 	{ "Left Spk", NULL, "Left BE_OUT" },
44 	{ "Right Spk", NULL, "Right BE_OUT" },
45 };
46 
47 static int
48 avs_max98927_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
49 {
50 	struct snd_interval *rate, *channels;
51 	struct snd_mask *fmt;
52 
53 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
54 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
55 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
56 
57 	/* The ADSP will convert the FE rate to 48k, stereo */
58 	rate->min = rate->max = 48000;
59 	channels->min = channels->max = 2;
60 
61 	/* set SSP0 to 16 bit */
62 	snd_mask_none(fmt);
63 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
64 	return 0;
65 }
66 
67 static int avs_max98927_hw_params(struct snd_pcm_substream *substream,
68 				  struct snd_pcm_hw_params *params)
69 {
70 	struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
71 	struct snd_soc_dai *codec_dai;
72 	int ret = 0;
73 	int i;
74 
75 	for_each_rtd_codec_dais(runtime, i, codec_dai) {
76 		if (!strcmp(codec_dai->component->name, MAX98927_DEV0_NAME))
77 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
78 		else if (!strcmp(codec_dai->component->name, MAX98927_DEV1_NAME))
79 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
80 
81 		if (ret < 0) {
82 			dev_err(runtime->dev, "hw_params for %s failed: %d\n",
83 				codec_dai->component->name, ret);
84 			return ret;
85 		}
86 	}
87 
88 	return 0;
89 }
90 
91 static const struct snd_soc_ops avs_max98927_ops = {
92 	.hw_params = avs_max98927_hw_params,
93 };
94 
95 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
96 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
97 {
98 	struct snd_soc_dai_link_component *platform;
99 	struct snd_soc_dai_link *dl;
100 
101 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
102 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
103 	if (!dl || !platform)
104 		return -ENOMEM;
105 
106 	platform->name = platform_name;
107 
108 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
109 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
110 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
111 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
112 	if (!dl->name || !dl->cpus || !dl->codecs)
113 		return -ENOMEM;
114 
115 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
116 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
117 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV0_NAME);
118 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
119 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV1_NAME);
120 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_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_CBS_CFS;
131 	dl->be_hw_params_fixup = avs_max98927_be_fixup;
132 	dl->nonatomic = 1;
133 	dl->no_pcm = 1;
134 	dl->ignore_pmdown_time = 1;
135 	dl->ops = &avs_max98927_ops;
136 
137 	*dai_link = dl;
138 
139 	return 0;
140 }
141 
142 static int avs_max98927_probe(struct platform_device *pdev)
143 {
144 	struct snd_soc_dai_link *dai_link;
145 	struct snd_soc_acpi_mach *mach;
146 	struct snd_soc_card *card;
147 	struct device *dev = &pdev->dev;
148 	const char *pname;
149 	int ssp_port, tdm_slot, ret;
150 
151 	mach = dev_get_platdata(dev);
152 	pname = mach->mach_params.platform;
153 
154 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
155 	if (ret)
156 		return ret;
157 
158 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
159 	if (ret) {
160 		dev_err(dev, "Failed to create dai link: %d", ret);
161 		return ret;
162 	}
163 
164 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
165 	if (!card)
166 		return -ENOMEM;
167 
168 	card->name = "avs_max98927";
169 	card->dev = dev;
170 	card->owner = THIS_MODULE;
171 	card->dai_link = dai_link;
172 	card->num_links = 1;
173 	card->codec_conf = card_codec_conf;
174 	card->num_configs = ARRAY_SIZE(card_codec_conf);
175 	card->controls = card_controls;
176 	card->num_controls = ARRAY_SIZE(card_controls);
177 	card->dapm_widgets = card_widgets;
178 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
179 	card->dapm_routes = card_base_routes;
180 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
181 	card->fully_routed = true;
182 
183 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
184 	if (ret)
185 		return ret;
186 
187 	return devm_snd_soc_register_card(dev, card);
188 }
189 
190 static const struct platform_device_id avs_max98927_driver_ids[] = {
191 	{
192 		.name = "avs_max98927",
193 	},
194 	{},
195 };
196 MODULE_DEVICE_TABLE(platform, avs_max98927_driver_ids);
197 
198 static struct platform_driver avs_max98927_driver = {
199 	.probe = avs_max98927_probe,
200 	.driver = {
201 		.name = "avs_max98927",
202 		.pm = &snd_soc_pm_ops,
203 	},
204 	.id_table = avs_max98927_driver_ids,
205 };
206 
207 module_platform_driver(avs_max98927_driver)
208 
209 MODULE_DESCRIPTION("Intel max98927 machine driver");
210 MODULE_LICENSE("GPL");
211