xref: /linux/sound/soc/intel/boards/bdw_rt286.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sound card driver for Intel Broadwell Wildcat Point with Realtek 286
4  *
5  * Copyright (C) 2013, Intel Corporation. All rights reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <sound/core.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 "../../codecs/rt286.h"
17 
18 static struct snd_soc_jack card_headset;
19 
20 static struct snd_soc_jack_pin card_headset_pins[] = {
21 	{
22 		.pin = "Mic Jack",
23 		.mask = SND_JACK_MICROPHONE,
24 	},
25 	{
26 		.pin = "Headphone Jack",
27 		.mask = SND_JACK_HEADPHONE,
28 	},
29 };
30 
31 static const struct snd_kcontrol_new card_controls[] = {
32 	SOC_DAPM_PIN_SWITCH("Speaker"),
33 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
34 };
35 
36 static const struct snd_soc_dapm_widget card_widgets[] = {
37 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
38 	SND_SOC_DAPM_SPK("Speaker", NULL),
39 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
40 	SND_SOC_DAPM_MIC("DMIC1", NULL),
41 	SND_SOC_DAPM_MIC("DMIC2", NULL),
42 	SND_SOC_DAPM_LINE("Line Jack", NULL),
43 };
44 
45 static const struct snd_soc_dapm_route card_routes[] = {
46 	{"Speaker", NULL, "SPOR"},
47 	{"Speaker", NULL, "SPOL"},
48 
49 	{"Headphone Jack", NULL, "HPO Pin"},
50 
51 	{"MIC1", NULL, "Mic Jack"},
52 	{"LINE1", NULL, "Line Jack"},
53 
54 	{"DMIC1 Pin", NULL, "DMIC1"},
55 	{"DMIC2 Pin", NULL, "DMIC2"},
56 
57 	/* CODEC BE connections */
58 	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
59 	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
60 };
61 
62 static int codec_link_init(struct snd_soc_pcm_runtime *rtd)
63 {
64 	struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
65 	int ret;
66 
67 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0,
68 					 &card_headset, card_headset_pins,
69 					 ARRAY_SIZE(card_headset_pins));
70 	if (ret)
71 		return ret;
72 
73 	return snd_soc_component_set_jack(codec, &card_headset, NULL);
74 }
75 
76 static int codec_link_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
77 				      struct snd_pcm_hw_params *params)
78 {
79 	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
80 	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
81 
82 	/* The ADSP will convert the FE rate to 48kHz, stereo. */
83 	rate->min = rate->max = 48000;
84 	channels->min = channels->max = 2;
85 	/* Set SSP0 to 16 bit. */
86 	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
87 
88 	return 0;
89 }
90 
91 static int codec_link_hw_params(struct snd_pcm_substream *substream,
92 				struct snd_pcm_hw_params *params)
93 {
94 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
95 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
96 	int ret;
97 
98 	ret = snd_soc_dai_set_sysclk(codec_dai, RT286_SCLK_S_PLL, 24000000, SND_SOC_CLOCK_IN);
99 	if (ret < 0) {
100 		dev_err(rtd->dev, "set codec sysclk failed: %d\n", ret);
101 		return ret;
102 	}
103 
104 	return ret;
105 }
106 
107 static const struct snd_soc_ops codec_link_ops = {
108 	.hw_params = codec_link_hw_params,
109 };
110 
111 SND_SOC_DAILINK_DEF(system, DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
112 SND_SOC_DAILINK_DEF(offload0, DAILINK_COMP_ARRAY(COMP_CPU("Offload0 Pin")));
113 SND_SOC_DAILINK_DEF(offload1, DAILINK_COMP_ARRAY(COMP_CPU("Offload1 Pin")));
114 SND_SOC_DAILINK_DEF(loopback, DAILINK_COMP_ARRAY(COMP_CPU("Loopback Pin")));
115 
116 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
117 SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
118 SND_SOC_DAILINK_DEF(codec, DAILINK_COMP_ARRAY(COMP_CODEC("i2c-INT343A:00", "rt286-aif1")));
119 SND_SOC_DAILINK_DEF(ssp0_port, DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
120 
121 static struct snd_soc_dai_link card_dai_links[] = {
122 	/* Front End DAI links */
123 	{
124 		.name = "System PCM",
125 		.stream_name = "System Playback/Capture",
126 		.nonatomic = 1,
127 		.dynamic = 1,
128 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
129 		.dpcm_playback = 1,
130 		.dpcm_capture = 1,
131 		SND_SOC_DAILINK_REG(system, dummy, platform),
132 	},
133 	{
134 		.name = "Offload0",
135 		.stream_name = "Offload0 Playback",
136 		.nonatomic = 1,
137 		.dynamic = 1,
138 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
139 		.dpcm_playback = 1,
140 		SND_SOC_DAILINK_REG(offload0, dummy, platform),
141 	},
142 	{
143 		.name = "Offload1",
144 		.stream_name = "Offload1 Playback",
145 		.nonatomic = 1,
146 		.dynamic = 1,
147 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
148 		.dpcm_playback = 1,
149 		SND_SOC_DAILINK_REG(offload1, dummy, platform),
150 	},
151 	{
152 		.name = "Loopback PCM",
153 		.stream_name = "Loopback",
154 		.nonatomic = 1,
155 		.dynamic = 1,
156 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
157 		.dpcm_capture = 1,
158 		SND_SOC_DAILINK_REG(loopback, dummy, platform),
159 	},
160 	/* Back End DAI links */
161 	{
162 		/* SSP0 - Codec */
163 		.name = "Codec",
164 		.id = 0,
165 		.nonatomic = 1,
166 		.no_pcm = 1,
167 		.init = codec_link_init,
168 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC,
169 		.ignore_pmdown_time = 1,
170 		.be_hw_params_fixup = codec_link_hw_params_fixup,
171 		.ops = &codec_link_ops,
172 		.dpcm_playback = 1,
173 		.dpcm_capture = 1,
174 		SND_SOC_DAILINK_REG(ssp0_port, codec, platform),
175 	},
176 };
177 
178 static void bdw_rt286_disable_jack(struct snd_soc_card *card)
179 {
180 	struct snd_soc_component *component;
181 
182 	for_each_card_components(card, component) {
183 		if (!strcmp(component->name, "i2c-INT343A:00")) {
184 			dev_dbg(component->dev, "disabling jack detect before going to suspend.\n");
185 			snd_soc_component_set_jack(component, NULL, NULL);
186 			break;
187 		}
188 	}
189 }
190 
191 static int bdw_rt286_suspend(struct snd_soc_card *card)
192 {
193 	bdw_rt286_disable_jack(card);
194 
195 	return 0;
196 }
197 
198 static int bdw_rt286_resume(struct snd_soc_card *card)
199 {
200 	struct snd_soc_component *component;
201 
202 	for_each_card_components(card, component) {
203 		if (!strcmp(component->name, "i2c-INT343A:00")) {
204 			dev_dbg(component->dev, "enabling jack detect for resume.\n");
205 			snd_soc_component_set_jack(component, &card_headset, NULL);
206 			break;
207 		}
208 	}
209 
210 	return 0;
211 }
212 
213 static struct snd_soc_card bdw_rt286_card = {
214 	.owner = THIS_MODULE,
215 	.dai_link = card_dai_links,
216 	.num_links = ARRAY_SIZE(card_dai_links),
217 	.controls = card_controls,
218 	.num_controls = ARRAY_SIZE(card_controls),
219 	.dapm_widgets = card_widgets,
220 	.num_dapm_widgets = ARRAY_SIZE(card_widgets),
221 	.dapm_routes = card_routes,
222 	.num_dapm_routes = ARRAY_SIZE(card_routes),
223 	.fully_routed = true,
224 	.suspend_pre = bdw_rt286_suspend,
225 	.resume_post = bdw_rt286_resume,
226 };
227 
228 /* Use space before codec name to simplify card ID, and simplify driver name. */
229 #define SOF_CARD_NAME "bdw rt286" /* card name will be 'sof-bdw rt286' */
230 #define SOF_DRIVER_NAME "SOF"
231 
232 #define CARD_NAME "broadwell-rt286"
233 
234 static int bdw_rt286_probe(struct platform_device *pdev)
235 {
236 	struct snd_soc_acpi_mach *mach;
237 	struct device *dev = &pdev->dev;
238 	int ret;
239 
240 	bdw_rt286_card.dev = dev;
241 	mach = dev_get_platdata(dev);
242 
243 	ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt286_card, mach->mach_params.platform);
244 	if (ret)
245 		return ret;
246 
247 	if (snd_soc_acpi_sof_parent(dev)) {
248 		bdw_rt286_card.name = SOF_CARD_NAME;
249 		bdw_rt286_card.driver_name = SOF_DRIVER_NAME;
250 	} else {
251 		bdw_rt286_card.name = CARD_NAME;
252 	}
253 
254 	return devm_snd_soc_register_card(dev, &bdw_rt286_card);
255 }
256 
257 static int bdw_rt286_remove(struct platform_device *pdev)
258 {
259 	struct snd_soc_card *card = platform_get_drvdata(pdev);
260 
261 	bdw_rt286_disable_jack(card);
262 
263 	return 0;
264 }
265 
266 static struct platform_driver bdw_rt286_driver = {
267 	.probe = bdw_rt286_probe,
268 	.remove = bdw_rt286_remove,
269 	.driver = {
270 		.name = "bdw_rt286",
271 		.pm = &snd_soc_pm_ops
272 	},
273 };
274 
275 module_platform_driver(bdw_rt286_driver)
276 
277 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
278 MODULE_DESCRIPTION("Sound card driver for Intel Broadwell Wildcat Point with Realtek 286");
279 MODULE_LICENSE("GPL");
280 MODULE_ALIAS("platform:bdw_rt286");
281