xref: /openbmc/linux/sound/soc/intel/avs/boards/max98927.c (revision 657c45b3)
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 MAX98927_DEV0_NAME	"i2c-MX98927:00"
17 #define MAX98927_DEV1_NAME	"i2c-MX98927:01"
18 #define MAX98927_CODEC_NAME	"max98927-aif1"
19 
20 static struct snd_soc_codec_conf card_codec_conf[] = {
21 	{
22 		.dlc = COMP_CODEC_CONF(MAX98927_DEV0_NAME),
23 		.name_prefix = "Right",
24 	},
25 	{
26 		.dlc = COMP_CODEC_CONF(MAX98927_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_max98927_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_max98927_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 = 0;
72 	int i;
73 
74 	for_each_rtd_codec_dais(runtime, i, codec_dai) {
75 		if (!strcmp(codec_dai->component->name, MAX98927_DEV0_NAME))
76 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
77 		else if (!strcmp(codec_dai->component->name, MAX98927_DEV1_NAME))
78 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
79 
80 		if (ret < 0) {
81 			dev_err(runtime->dev, "hw_params for %s failed: %d\n",
82 				codec_dai->component->name, ret);
83 			return ret;
84 		}
85 	}
86 
87 	return 0;
88 }
89 
90 static const struct snd_soc_ops avs_max98927_ops = {
91 	.hw_params = avs_max98927_hw_params,
92 };
93 
94 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
95 			       struct snd_soc_dai_link **dai_link)
96 {
97 	struct snd_soc_dai_link_component *platform;
98 	struct snd_soc_dai_link *dl;
99 
100 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
101 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
102 	if (!dl || !platform)
103 		return -ENOMEM;
104 
105 	platform->name = platform_name;
106 
107 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
108 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
109 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
110 	if (!dl->name || !dl->cpus || !dl->codecs)
111 		return -ENOMEM;
112 
113 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
114 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV0_NAME);
115 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
116 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV1_NAME);
117 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
118 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
119 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
120 		return -ENOMEM;
121 
122 	dl->num_cpus = 1;
123 	dl->num_codecs = 2;
124 	dl->platforms = platform;
125 	dl->num_platforms = 1;
126 	dl->id = 0;
127 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
128 	dl->be_hw_params_fixup = avs_max98927_be_fixup;
129 	dl->nonatomic = 1;
130 	dl->no_pcm = 1;
131 	dl->dpcm_capture = 1;
132 	dl->dpcm_playback = 1;
133 	dl->ignore_pmdown_time = 1;
134 	dl->ops = &avs_max98927_ops;
135 
136 	*dai_link = dl;
137 
138 	return 0;
139 }
140 
141 static int avs_max98927_probe(struct platform_device *pdev)
142 {
143 	struct snd_soc_dai_link *dai_link;
144 	struct snd_soc_acpi_mach *mach;
145 	struct snd_soc_card *card;
146 	struct device *dev = &pdev->dev;
147 	const char *pname;
148 	int ssp_port, ret;
149 
150 	mach = dev_get_platdata(dev);
151 	pname = mach->mach_params.platform;
152 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
153 
154 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
155 	if (ret) {
156 		dev_err(dev, "Failed to create dai link: %d", ret);
157 		return ret;
158 	}
159 
160 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
161 	if (!card)
162 		return -ENOMEM;
163 
164 	card->name = "avs_max98927";
165 	card->dev = dev;
166 	card->owner = THIS_MODULE;
167 	card->dai_link = dai_link;
168 	card->num_links = 1;
169 	card->codec_conf = card_codec_conf;
170 	card->num_configs = ARRAY_SIZE(card_codec_conf);
171 	card->controls = card_controls;
172 	card->num_controls = ARRAY_SIZE(card_controls);
173 	card->dapm_widgets = card_widgets;
174 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
175 	card->dapm_routes = card_base_routes;
176 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
177 	card->fully_routed = true;
178 
179 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
180 	if (ret)
181 		return ret;
182 
183 	return devm_snd_soc_register_card(dev, card);
184 }
185 
186 static struct platform_driver avs_max98927_driver = {
187 	.probe = avs_max98927_probe,
188 	.driver = {
189 		.name = "avs_max98927",
190 		.pm = &snd_soc_pm_ops,
191 	},
192 };
193 
194 module_platform_driver(avs_max98927_driver)
195 
196 MODULE_LICENSE("GPL");
197 MODULE_ALIAS("platform:avs_max98927");
198