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 .no_pcm = 1, 166 .init = codec_link_init, 167 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC, 168 .ignore_pmdown_time = 1, 169 .be_hw_params_fixup = codec_link_hw_params_fixup, 170 .ops = &codec_link_ops, 171 .dpcm_playback = 1, 172 .dpcm_capture = 1, 173 SND_SOC_DAILINK_REG(ssp0_port, codec, platform), 174 }, 175 }; 176 177 static void bdw_rt286_disable_jack(struct snd_soc_card *card) 178 { 179 struct snd_soc_component *component; 180 181 for_each_card_components(card, component) { 182 if (!strcmp(component->name, "i2c-INT343A:00")) { 183 dev_dbg(component->dev, "disabling jack detect before going to suspend.\n"); 184 snd_soc_component_set_jack(component, NULL, NULL); 185 break; 186 } 187 } 188 } 189 190 static int bdw_rt286_suspend(struct snd_soc_card *card) 191 { 192 bdw_rt286_disable_jack(card); 193 194 return 0; 195 } 196 197 static int bdw_rt286_resume(struct snd_soc_card *card) 198 { 199 struct snd_soc_component *component; 200 201 for_each_card_components(card, component) { 202 if (!strcmp(component->name, "i2c-INT343A:00")) { 203 dev_dbg(component->dev, "enabling jack detect for resume.\n"); 204 snd_soc_component_set_jack(component, &card_headset, NULL); 205 break; 206 } 207 } 208 209 return 0; 210 } 211 212 static struct snd_soc_card bdw_rt286_card = { 213 .owner = THIS_MODULE, 214 .dai_link = card_dai_links, 215 .num_links = ARRAY_SIZE(card_dai_links), 216 .controls = card_controls, 217 .num_controls = ARRAY_SIZE(card_controls), 218 .dapm_widgets = card_widgets, 219 .num_dapm_widgets = ARRAY_SIZE(card_widgets), 220 .dapm_routes = card_routes, 221 .num_dapm_routes = ARRAY_SIZE(card_routes), 222 .fully_routed = true, 223 .suspend_pre = bdw_rt286_suspend, 224 .resume_post = bdw_rt286_resume, 225 }; 226 227 /* Use space before codec name to simplify card ID, and simplify driver name. */ 228 #define SOF_CARD_NAME "bdw rt286" /* card name will be 'sof-bdw rt286' */ 229 #define SOF_DRIVER_NAME "SOF" 230 231 #define CARD_NAME "broadwell-rt286" 232 233 static int bdw_rt286_probe(struct platform_device *pdev) 234 { 235 struct snd_soc_acpi_mach *mach; 236 struct device *dev = &pdev->dev; 237 int ret; 238 239 bdw_rt286_card.dev = dev; 240 mach = dev_get_platdata(dev); 241 242 ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt286_card, mach->mach_params.platform); 243 if (ret) 244 return ret; 245 246 if (snd_soc_acpi_sof_parent(dev)) { 247 bdw_rt286_card.name = SOF_CARD_NAME; 248 bdw_rt286_card.driver_name = SOF_DRIVER_NAME; 249 } else { 250 bdw_rt286_card.name = CARD_NAME; 251 } 252 253 return devm_snd_soc_register_card(dev, &bdw_rt286_card); 254 } 255 256 static int bdw_rt286_remove(struct platform_device *pdev) 257 { 258 struct snd_soc_card *card = platform_get_drvdata(pdev); 259 260 bdw_rt286_disable_jack(card); 261 262 return 0; 263 } 264 265 static struct platform_driver bdw_rt286_driver = { 266 .probe = bdw_rt286_probe, 267 .remove = bdw_rt286_remove, 268 .driver = { 269 .name = "bdw_rt286", 270 .pm = &snd_soc_pm_ops 271 }, 272 }; 273 274 module_platform_driver(bdw_rt286_driver) 275 276 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang"); 277 MODULE_DESCRIPTION("Sound card driver for Intel Broadwell Wildcat Point with Realtek 286"); 278 MODULE_LICENSE("GPL"); 279 MODULE_ALIAS("platform:bdw_rt286"); 280