xref: /openbmc/linux/sound/soc/intel/avs/boards/da7219.c (revision 3ddc8b84)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 
8 #include <linux/module.h>
9 #include <linux/platform_data/x86/soc.h>
10 #include <linux/platform_device.h>
11 #include <sound/jack.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/soc-dapm.h>
17 #include <uapi/linux/input-event-codes.h>
18 #include "../../../codecs/da7219.h"
19 
20 #define DA7219_DAI_NAME		"da7219-hifi"
21 
22 static const struct snd_kcontrol_new card_controls[] = {
23 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
24 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
25 	SOC_DAPM_PIN_SWITCH("Line Out"),
26 };
27 
28 static int platform_clock_control(struct snd_soc_dapm_widget *w,
29 				  struct snd_kcontrol *k, int  event)
30 {
31 	struct snd_soc_dapm_context *dapm = w->dapm;
32 	struct snd_soc_card *card = dapm->card;
33 	struct snd_soc_dai *codec_dai;
34 	int ret = 0;
35 
36 	codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
37 	if (!codec_dai) {
38 		dev_err(card->dev, "Codec dai not found. Unable to set/unset codec pll\n");
39 		return -EIO;
40 	}
41 
42 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
43 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
44 		if (ret)
45 			dev_err(card->dev, "failed to stop PLL: %d\n", ret);
46 	} else if (SND_SOC_DAPM_EVENT_ON(event)) {
47 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
48 					  0, DA7219_PLL_FREQ_OUT_98304);
49 		if (ret)
50 			dev_err(card->dev, "failed to start PLL: %d\n", ret);
51 	}
52 
53 	return ret;
54 }
55 
56 static const struct snd_soc_dapm_widget card_widgets[] = {
57 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
58 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
59 	SND_SOC_DAPM_LINE("Line Out", NULL),
60 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control,
61 			    SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_PRE_PMU),
62 };
63 
64 static const struct snd_soc_dapm_route card_base_routes[] = {
65 	/* HP jack connectors - unknown if we have jack detection */
66 	{"Headphone Jack", NULL, "HPL"},
67 	{"Headphone Jack", NULL, "HPR"},
68 
69 	{"MIC", NULL, "Headset Mic"},
70 
71 	{ "Headphone Jack", NULL, "Platform Clock" },
72 	{ "Headset Mic", NULL, "Platform Clock" },
73 	{ "Line Out", NULL, "Platform Clock" },
74 };
75 
76 static const struct snd_soc_jack_pin card_headset_pins[] = {
77 	{
78 		.pin = "Headphone Jack",
79 		.mask = SND_JACK_HEADPHONE,
80 	},
81 	{
82 		.pin = "Headset Mic",
83 		.mask = SND_JACK_MICROPHONE,
84 	},
85 	{
86 		.pin = "Line Out",
87 		.mask = SND_JACK_LINEOUT,
88 	},
89 };
90 
91 static int avs_da7219_codec_init(struct snd_soc_pcm_runtime *runtime)
92 {
93 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0);
94 	struct snd_soc_component *component = codec_dai->component;
95 	struct snd_soc_card *card = runtime->card;
96 	struct snd_soc_jack_pin *pins;
97 	struct snd_soc_jack *jack;
98 	int num_pins;
99 	int clk_freq;
100 	int ret;
101 
102 	jack = snd_soc_card_get_drvdata(card);
103 	if (soc_intel_is_apl())
104 		clk_freq = 19200000;
105 	else /* kbl */
106 		clk_freq = 24576000;
107 
108 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, clk_freq, SND_SOC_CLOCK_IN);
109 	if (ret) {
110 		dev_err(card->dev, "can't set codec sysclk configuration\n");
111 		return ret;
112 	}
113 
114 	num_pins = ARRAY_SIZE(card_headset_pins);
115 	pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
116 	if (!pins)
117 		return -ENOMEM;
118 
119 	/*
120 	 * Headset buttons map to the google Reference headset.
121 	 * These can be configured by userspace.
122 	 */
123 	ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
124 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
125 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
126 					 SND_JACK_BTN_3 | SND_JACK_LINEOUT,
127 					 jack, pins, num_pins);
128 	if (ret) {
129 		dev_err(card->dev, "Headset Jack creation failed: %d\n", ret);
130 		return ret;
131 	}
132 
133 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
134 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
135 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
136 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
137 
138 	return snd_soc_component_set_jack(component, jack, NULL);
139 }
140 
141 static void avs_da7219_codec_exit(struct snd_soc_pcm_runtime *rtd)
142 {
143 	snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
144 }
145 
146 static int
147 avs_da7219_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
148 {
149 	struct snd_interval *rate, *channels;
150 	struct snd_mask *fmt;
151 
152 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
153 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
154 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
155 
156 	/* The ADSP will convert the FE rate to 48k, stereo */
157 	rate->min = rate->max = 48000;
158 	channels->min = channels->max = 2;
159 
160 	/* set SSP0 to 24 bit */
161 	snd_mask_none(fmt);
162 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
163 	return 0;
164 }
165 
166 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
167 			       struct snd_soc_dai_link **dai_link)
168 {
169 	struct snd_soc_dai_link_component *platform;
170 	struct snd_soc_dai_link *dl;
171 
172 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
173 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
174 	if (!dl || !platform)
175 		return -ENOMEM;
176 
177 	platform->name = platform_name;
178 
179 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
180 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
181 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
182 	if (!dl->name || !dl->cpus || !dl->codecs)
183 		return -ENOMEM;
184 
185 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
186 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-DLGS7219:00");
187 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, DA7219_DAI_NAME);
188 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
189 		return -ENOMEM;
190 
191 	dl->num_cpus = 1;
192 	dl->num_codecs = 1;
193 	dl->platforms = platform;
194 	dl->num_platforms = 1;
195 	dl->id = 0;
196 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
197 	dl->be_hw_params_fixup = avs_da7219_be_fixup;
198 	dl->init = avs_da7219_codec_init;
199 	dl->exit = avs_da7219_codec_exit;
200 	dl->nonatomic = 1;
201 	dl->no_pcm = 1;
202 	dl->dpcm_capture = 1;
203 	dl->dpcm_playback = 1;
204 
205 	*dai_link = dl;
206 
207 	return 0;
208 }
209 
210 static int avs_card_suspend_pre(struct snd_soc_card *card)
211 {
212 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
213 
214 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
215 }
216 
217 static int avs_card_resume_post(struct snd_soc_card *card)
218 {
219 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
220 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
221 
222 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
223 }
224 
225 static int avs_da7219_probe(struct platform_device *pdev)
226 {
227 	struct snd_soc_dai_link *dai_link;
228 	struct snd_soc_acpi_mach *mach;
229 	struct snd_soc_card *card;
230 	struct snd_soc_jack *jack;
231 	struct device *dev = &pdev->dev;
232 	const char *pname;
233 	int ssp_port, ret;
234 
235 	mach = dev_get_platdata(dev);
236 	pname = mach->mach_params.platform;
237 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
238 
239 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
240 	if (ret) {
241 		dev_err(dev, "Failed to create dai link: %d", ret);
242 		return ret;
243 	}
244 
245 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
246 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
247 	if (!jack || !card)
248 		return -ENOMEM;
249 
250 	card->name = "avs_da7219";
251 	card->dev = dev;
252 	card->owner = THIS_MODULE;
253 	card->suspend_pre = avs_card_suspend_pre;
254 	card->resume_post = avs_card_resume_post;
255 	card->dai_link = dai_link;
256 	card->num_links = 1;
257 	card->controls = card_controls;
258 	card->num_controls = ARRAY_SIZE(card_controls);
259 	card->dapm_widgets = card_widgets;
260 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
261 	card->dapm_routes = card_base_routes;
262 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
263 	card->fully_routed = true;
264 	snd_soc_card_set_drvdata(card, jack);
265 
266 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
267 	if (ret)
268 		return ret;
269 
270 	return devm_snd_soc_register_card(dev, card);
271 }
272 
273 static struct platform_driver avs_da7219_driver = {
274 	.probe = avs_da7219_probe,
275 	.driver = {
276 		.name = "avs_da7219",
277 		.pm = &snd_soc_pm_ops,
278 	},
279 };
280 
281 module_platform_driver(avs_da7219_driver);
282 
283 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS("platform:avs_da7219");
286