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