1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-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/input.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <sound/core.h> 13 #include <sound/jack.h> 14 #include <sound/pcm.h> 15 #include <sound/pcm_params.h> 16 #include <sound/soc.h> 17 #include <sound/soc-acpi.h> 18 #include "../../../codecs/nau8825.h" 19 20 #define SKL_NUVOTON_CODEC_DAI "nau8825-hifi" 21 22 static int 23 avs_nau8825_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event) 24 { 25 struct snd_soc_dapm_context *dapm = w->dapm; 26 struct snd_soc_card *card = dapm->card; 27 struct snd_soc_dai *codec_dai; 28 int ret; 29 30 codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI); 31 if (!codec_dai) { 32 dev_err(card->dev, "Codec dai not found\n"); 33 return -EINVAL; 34 } 35 36 if (!SND_SOC_DAPM_EVENT_ON(event)) { 37 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN); 38 if (ret < 0) { 39 dev_err(card->dev, "set sysclk err = %d\n", ret); 40 return ret; 41 } 42 } 43 44 return 0; 45 } 46 47 static const struct snd_kcontrol_new card_controls[] = { 48 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 49 SOC_DAPM_PIN_SWITCH("Headset Mic"), 50 }; 51 52 static const struct snd_soc_dapm_widget card_widgets[] = { 53 SND_SOC_DAPM_HP("Headphone Jack", NULL), 54 SND_SOC_DAPM_MIC("Headset Mic", NULL), 55 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, avs_nau8825_clock_control, 56 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 57 }; 58 59 static const struct snd_soc_dapm_route card_base_routes[] = { 60 { "Headphone Jack", NULL, "HPOL" }, 61 { "Headphone Jack", NULL, "HPOR" }, 62 63 { "MIC", NULL, "Headset Mic" }, 64 65 { "Headphone Jack", NULL, "Platform Clock" }, 66 { "Headset Mic", NULL, "Platform Clock" }, 67 }; 68 69 static struct snd_soc_jack_pin card_headset_pins[] = { 70 { 71 .pin = "Headphone Jack", 72 .mask = SND_JACK_HEADPHONE, 73 }, 74 { 75 .pin = "Headset Mic", 76 .mask = SND_JACK_MICROPHONE, 77 }, 78 }; 79 80 static int avs_nau8825_codec_init(struct snd_soc_pcm_runtime *runtime) 81 { 82 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0); 83 struct snd_soc_component *component = codec_dai->component; 84 struct snd_soc_jack_pin *pins; 85 struct snd_soc_jack *jack; 86 struct snd_soc_card *card = runtime->card; 87 int num_pins, ret; 88 89 jack = snd_soc_card_get_drvdata(card); 90 num_pins = ARRAY_SIZE(card_headset_pins); 91 92 pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL); 93 if (!pins) 94 return -ENOMEM; 95 96 /* 97 * 4 buttons here map to the google Reference headset. 98 * The use of these buttons can be decided by the user space. 99 */ 100 ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0 | 101 SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3, 102 jack, pins, num_pins); 103 if (ret) 104 return ret; 105 106 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 107 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 108 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 109 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 110 111 //snd_soc_component_set_jack(component, jack, NULL); 112 // TODO: Fix nau8825 codec to use .set_jack, like everyone else 113 nau8825_enable_jack_detect(component, jack); 114 115 return 0; 116 } 117 118 static int 119 avs_nau8825_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params) 120 { 121 struct snd_interval *rate, *channels; 122 struct snd_mask *fmt; 123 124 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 125 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 126 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 127 128 /* The ADSP will convert the FE rate to 48k, stereo */ 129 rate->min = rate->max = 48000; 130 channels->min = channels->max = 2; 131 132 /* set SSP to 24 bit */ 133 snd_mask_none(fmt); 134 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 135 136 return 0; 137 } 138 139 static int avs_nau8825_trigger(struct snd_pcm_substream *substream, int cmd) 140 { 141 struct snd_pcm_runtime *runtime = substream->runtime; 142 struct snd_soc_pcm_runtime *rtm = asoc_substream_to_rtd(substream); 143 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtm, 0); 144 int ret = 0; 145 146 switch (cmd) { 147 case SNDRV_PCM_TRIGGER_START: 148 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN); 149 if (ret < 0) { 150 dev_err(codec_dai->dev, "can't set FS clock %d\n", ret); 151 break; 152 } 153 154 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256); 155 if (ret < 0) 156 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret); 157 break; 158 159 case SNDRV_PCM_TRIGGER_RESUME: 160 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256); 161 if (ret < 0) 162 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret); 163 break; 164 } 165 166 return ret; 167 } 168 169 170 static const struct snd_soc_ops avs_nau8825_ops = { 171 .trigger = avs_nau8825_trigger, 172 }; 173 174 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port, 175 struct snd_soc_dai_link **dai_link) 176 { 177 struct snd_soc_dai_link_component *platform; 178 struct snd_soc_dai_link *dl; 179 180 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); 181 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); 182 if (!dl || !platform) 183 return -ENOMEM; 184 185 platform->name = platform_name; 186 187 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port); 188 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); 189 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); 190 if (!dl->name || !dl->cpus || !dl->codecs) 191 return -ENOMEM; 192 193 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port); 194 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10508825:00"); 195 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, SKL_NUVOTON_CODEC_DAI); 196 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) 197 return -ENOMEM; 198 199 dl->num_cpus = 1; 200 dl->num_codecs = 1; 201 dl->platforms = platform; 202 dl->num_platforms = 1; 203 dl->id = 0; 204 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS; 205 dl->init = avs_nau8825_codec_init; 206 dl->be_hw_params_fixup = avs_nau8825_be_fixup; 207 dl->ops = &avs_nau8825_ops; 208 dl->nonatomic = 1; 209 dl->no_pcm = 1; 210 dl->dpcm_capture = 1; 211 dl->dpcm_playback = 1; 212 213 *dai_link = dl; 214 215 return 0; 216 } 217 218 static int avs_create_dapm_routes(struct device *dev, int ssp_port, 219 struct snd_soc_dapm_route **routes, int *num_routes) 220 { 221 struct snd_soc_dapm_route *dr; 222 const int num_base = ARRAY_SIZE(card_base_routes); 223 const int num_dr = num_base + 2; 224 int idx; 225 226 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL); 227 if (!dr) 228 return -ENOMEM; 229 230 memcpy(dr, card_base_routes, num_base * sizeof(*dr)); 231 232 idx = num_base; 233 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Playback"); 234 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port); 235 if (!dr[idx].sink || !dr[idx].source) 236 return -ENOMEM; 237 238 idx++; 239 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port); 240 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Capture"); 241 if (!dr[idx].sink || !dr[idx].source) 242 return -ENOMEM; 243 244 *routes = dr; 245 *num_routes = num_dr; 246 247 return 0; 248 } 249 250 static int avs_card_set_jack(struct snd_soc_card *card, struct snd_soc_jack *jack) 251 { 252 struct snd_soc_component *component; 253 254 for_each_card_components(card, component) 255 snd_soc_component_set_jack(component, jack, NULL); 256 return 0; 257 } 258 259 static int avs_card_remove(struct snd_soc_card *card) 260 { 261 return avs_card_set_jack(card, NULL); 262 } 263 264 static int avs_card_suspend_pre(struct snd_soc_card *card) 265 { 266 return avs_card_set_jack(card, NULL); 267 } 268 269 static int avs_card_resume_post(struct snd_soc_card *card) 270 { 271 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI); 272 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card); 273 274 if (!codec_dai) { 275 dev_err(card->dev, "Codec dai not found\n"); 276 return -EINVAL; 277 } 278 279 if (codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK] && 280 codec_dai->playback_widget->active) 281 snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN); 282 283 return avs_card_set_jack(card, jack); 284 } 285 286 static int avs_nau8825_probe(struct platform_device *pdev) 287 { 288 struct snd_soc_dapm_route *routes; 289 struct snd_soc_dai_link *dai_link; 290 struct snd_soc_acpi_mach *mach; 291 struct snd_soc_card *card; 292 struct snd_soc_jack *jack; 293 struct device *dev = &pdev->dev; 294 const char *pname; 295 int num_routes, ssp_port, ret; 296 297 mach = dev_get_platdata(dev); 298 pname = mach->mach_params.platform; 299 ssp_port = __ffs(mach->mach_params.i2s_link_mask); 300 301 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link); 302 if (ret) { 303 dev_err(dev, "Failed to create dai link: %d", ret); 304 return ret; 305 } 306 307 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes); 308 if (ret) { 309 dev_err(dev, "Failed to create dapm routes: %d", ret); 310 return ret; 311 } 312 313 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); 314 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 315 if (!jack || !card) 316 return -ENOMEM; 317 318 card->name = "avs_nau8825"; 319 card->dev = dev; 320 card->owner = THIS_MODULE; 321 card->remove = avs_card_remove; 322 card->suspend_pre = avs_card_suspend_pre; 323 card->resume_post = avs_card_resume_post; 324 card->dai_link = dai_link; 325 card->num_links = 1; 326 card->controls = card_controls; 327 card->num_controls = ARRAY_SIZE(card_controls); 328 card->dapm_widgets = card_widgets; 329 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 330 card->dapm_routes = routes; 331 card->num_dapm_routes = num_routes; 332 card->fully_routed = true; 333 snd_soc_card_set_drvdata(card, jack); 334 335 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 336 if (ret) 337 return ret; 338 339 return devm_snd_soc_register_card(dev, card); 340 } 341 342 static struct platform_driver avs_nau8825_driver = { 343 .probe = avs_nau8825_probe, 344 .driver = { 345 .name = "avs_nau8825", 346 .pm = &snd_soc_pm_ops, 347 }, 348 }; 349 350 module_platform_driver(avs_nau8825_driver) 351 352 MODULE_LICENSE("GPL"); 353 MODULE_ALIAS("platform:avs_nau8825"); 354