1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Rockchip machine ASoC driver for boards using a MAX90809 CODEC. 4 * 5 * Copyright (c) 2014, ROCKCHIP CORPORATION. All rights reserved. 6 */ 7 8 #include <linux/module.h> 9 #include <linux/of_device.h> 10 #include <linux/platform_device.h> 11 #include <linux/slab.h> 12 #include <linux/gpio.h> 13 #include <linux/of_gpio.h> 14 #include <sound/core.h> 15 #include <sound/hdmi-codec.h> 16 #include <sound/jack.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/soc.h> 20 21 #include "rockchip_i2s.h" 22 #include "../codecs/ts3a227e.h" 23 24 #define DRV_NAME "rockchip-snd-max98090" 25 26 static struct snd_soc_jack headset_jack; 27 28 /* Headset jack detection DAPM pins */ 29 static struct snd_soc_jack_pin headset_jack_pins[] = { 30 { 31 .pin = "Headphone", 32 .mask = SND_JACK_HEADPHONE, 33 }, 34 { 35 .pin = "Headset Mic", 36 .mask = SND_JACK_MICROPHONE, 37 }, 38 39 }; 40 41 #define RK_MAX98090_WIDGETS \ 42 SND_SOC_DAPM_HP("Headphone", NULL), \ 43 SND_SOC_DAPM_MIC("Headset Mic", NULL), \ 44 SND_SOC_DAPM_MIC("Int Mic", NULL), \ 45 SND_SOC_DAPM_SPK("Speaker", NULL) 46 47 #define RK_HDMI_WIDGETS \ 48 SND_SOC_DAPM_LINE("HDMI", NULL) 49 50 static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets[] = { 51 RK_MAX98090_WIDGETS, 52 }; 53 54 static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets[] = { 55 RK_HDMI_WIDGETS, 56 }; 57 58 static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets[] = { 59 RK_MAX98090_WIDGETS, 60 RK_HDMI_WIDGETS, 61 }; 62 63 #define RK_MAX98090_AUDIO_MAP \ 64 {"IN34", NULL, "Headset Mic"}, \ 65 {"Headset Mic", NULL, "MICBIAS"}, \ 66 {"DMICL", NULL, "Int Mic"}, \ 67 {"Headphone", NULL, "HPL"}, \ 68 {"Headphone", NULL, "HPR"}, \ 69 {"Speaker", NULL, "SPKL"}, \ 70 {"Speaker", NULL, "SPKR"} 71 72 #define RK_HDMI_AUDIO_MAP \ 73 {"HDMI", NULL, "TX"} 74 75 static const struct snd_soc_dapm_route rk_max98090_audio_map[] = { 76 RK_MAX98090_AUDIO_MAP, 77 }; 78 79 static const struct snd_soc_dapm_route rk_hdmi_audio_map[] = { 80 RK_HDMI_AUDIO_MAP, 81 }; 82 83 static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map[] = { 84 RK_MAX98090_AUDIO_MAP, 85 RK_HDMI_AUDIO_MAP, 86 }; 87 88 #define RK_MAX98090_CONTROLS \ 89 SOC_DAPM_PIN_SWITCH("Headphone"), \ 90 SOC_DAPM_PIN_SWITCH("Headset Mic"), \ 91 SOC_DAPM_PIN_SWITCH("Int Mic"), \ 92 SOC_DAPM_PIN_SWITCH("Speaker") 93 94 #define RK_HDMI_CONTROLS \ 95 SOC_DAPM_PIN_SWITCH("HDMI") 96 97 static const struct snd_kcontrol_new rk_max98090_controls[] = { 98 RK_MAX98090_CONTROLS, 99 }; 100 101 static const struct snd_kcontrol_new rk_hdmi_controls[] = { 102 RK_HDMI_CONTROLS, 103 }; 104 105 static const struct snd_kcontrol_new rk_max98090_hdmi_controls[] = { 106 RK_MAX98090_CONTROLS, 107 RK_HDMI_CONTROLS, 108 }; 109 110 static int rk_jack_event(struct notifier_block *nb, unsigned long event, 111 void *data) 112 { 113 struct snd_soc_jack *jack = (struct snd_soc_jack *)data; 114 struct snd_soc_dapm_context *dapm = &jack->card->dapm; 115 116 if (event & SND_JACK_MICROPHONE) { 117 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); 118 snd_soc_dapm_force_enable_pin(dapm, "SHDN"); 119 } else { 120 snd_soc_dapm_disable_pin(dapm, "MICBIAS"); 121 snd_soc_dapm_disable_pin(dapm, "SHDN"); 122 } 123 124 snd_soc_dapm_sync(dapm); 125 126 return 0; 127 } 128 129 static struct notifier_block rk_jack_nb = { 130 .notifier_call = rk_jack_event, 131 }; 132 133 static int rk_init(struct snd_soc_pcm_runtime *runtime) 134 { 135 /* 136 * The jack has already been created in the rk_98090_headset_init() 137 * function. 138 */ 139 snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb); 140 141 return 0; 142 } 143 144 static int rk_aif1_hw_params(struct snd_pcm_substream *substream, 145 struct snd_pcm_hw_params *params) 146 { 147 int ret = 0; 148 struct snd_soc_pcm_runtime *rtd = substream->private_data; 149 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 150 struct snd_soc_dai *codec_dai = rtd->codec_dai; 151 int mclk; 152 153 switch (params_rate(params)) { 154 case 8000: 155 case 16000: 156 case 24000: 157 case 32000: 158 case 48000: 159 case 64000: 160 case 96000: 161 mclk = 12288000; 162 break; 163 case 11025: 164 case 22050: 165 case 44100: 166 case 88200: 167 mclk = 11289600; 168 break; 169 default: 170 return -EINVAL; 171 } 172 173 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 174 SND_SOC_CLOCK_OUT); 175 if (ret) { 176 dev_err(cpu_dai->dev, "Can't set cpu dai clock %d\n", ret); 177 return ret; 178 } 179 180 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 181 SND_SOC_CLOCK_IN); 182 183 /* HDMI codec dai does not need to set sysclk. */ 184 if (!strcmp(rtd->dai_link->name, "HDMI")) 185 return 0; 186 187 if (ret) { 188 dev_err(codec_dai->dev, "Can't set codec dai clock %d\n", ret); 189 return ret; 190 } 191 192 return ret; 193 } 194 195 static int rk_aif1_startup(struct snd_pcm_substream *substream) 196 { 197 /* 198 * Set period size to 240 because pl330 has issue 199 * dealing with larger period in stress testing. 200 */ 201 return snd_pcm_hw_constraint_minmax(substream->runtime, 202 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 240, 240); 203 } 204 205 static const struct snd_soc_ops rk_aif1_ops = { 206 .hw_params = rk_aif1_hw_params, 207 .startup = rk_aif1_startup, 208 }; 209 210 SND_SOC_DAILINK_DEFS(analog, 211 DAILINK_COMP_ARRAY(COMP_EMPTY()), 212 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")), 213 DAILINK_COMP_ARRAY(COMP_EMPTY())); 214 215 SND_SOC_DAILINK_DEFS(hdmi, 216 DAILINK_COMP_ARRAY(COMP_EMPTY()), 217 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")), 218 DAILINK_COMP_ARRAY(COMP_EMPTY())); 219 220 enum { 221 DAILINK_MAX98090, 222 DAILINK_HDMI, 223 }; 224 225 static struct snd_soc_jack rk_hdmi_jack; 226 227 static int rk_hdmi_init(struct snd_soc_pcm_runtime *runtime) 228 { 229 struct snd_soc_card *card = runtime->card; 230 struct snd_soc_component *component = runtime->codec_dai->component; 231 int ret; 232 233 /* enable jack detection */ 234 ret = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT, 235 &rk_hdmi_jack, NULL, 0); 236 if (ret) { 237 dev_err(card->dev, "Can't new HDMI Jack %d\n", ret); 238 return ret; 239 } 240 241 return hdmi_codec_set_jack_detect(component, &rk_hdmi_jack); 242 } 243 244 /* max98090 dai_link */ 245 static struct snd_soc_dai_link rk_max98090_dailinks[] = { 246 { 247 .name = "max98090", 248 .stream_name = "Analog", 249 .init = rk_init, 250 .ops = &rk_aif1_ops, 251 /* set max98090 as slave */ 252 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 253 SND_SOC_DAIFMT_CBS_CFS, 254 SND_SOC_DAILINK_REG(analog), 255 }, 256 }; 257 258 /* HDMI codec dai_link */ 259 static struct snd_soc_dai_link rk_hdmi_dailinks[] = { 260 { 261 .name = "HDMI", 262 .stream_name = "HDMI", 263 .init = rk_hdmi_init, 264 .ops = &rk_aif1_ops, 265 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 266 SND_SOC_DAIFMT_CBS_CFS, 267 SND_SOC_DAILINK_REG(hdmi), 268 } 269 }; 270 271 /* max98090 and HDMI codec dai_link */ 272 static struct snd_soc_dai_link rk_max98090_hdmi_dailinks[] = { 273 [DAILINK_MAX98090] = { 274 .name = "max98090", 275 .stream_name = "Analog", 276 .init = rk_init, 277 .ops = &rk_aif1_ops, 278 /* set max98090 as slave */ 279 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 280 SND_SOC_DAIFMT_CBS_CFS, 281 SND_SOC_DAILINK_REG(analog), 282 }, 283 [DAILINK_HDMI] = { 284 .name = "HDMI", 285 .stream_name = "HDMI", 286 .init = rk_hdmi_init, 287 .ops = &rk_aif1_ops, 288 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 289 SND_SOC_DAIFMT_CBS_CFS, 290 SND_SOC_DAILINK_REG(hdmi), 291 } 292 }; 293 294 static int rk_98090_headset_init(struct snd_soc_component *component); 295 296 static struct snd_soc_aux_dev rk_98090_headset_dev = { 297 .dlc = COMP_EMPTY(), 298 .init = rk_98090_headset_init, 299 }; 300 301 static struct snd_soc_card rockchip_max98090_card = { 302 .name = "ROCKCHIP-I2S", 303 .owner = THIS_MODULE, 304 .dai_link = rk_max98090_dailinks, 305 .num_links = ARRAY_SIZE(rk_max98090_dailinks), 306 .aux_dev = &rk_98090_headset_dev, 307 .num_aux_devs = 1, 308 .dapm_widgets = rk_max98090_dapm_widgets, 309 .num_dapm_widgets = ARRAY_SIZE(rk_max98090_dapm_widgets), 310 .dapm_routes = rk_max98090_audio_map, 311 .num_dapm_routes = ARRAY_SIZE(rk_max98090_audio_map), 312 .controls = rk_max98090_controls, 313 .num_controls = ARRAY_SIZE(rk_max98090_controls), 314 }; 315 316 static struct snd_soc_card rockchip_hdmi_card = { 317 .name = "ROCKCHIP-HDMI", 318 .owner = THIS_MODULE, 319 .dai_link = rk_hdmi_dailinks, 320 .num_links = ARRAY_SIZE(rk_hdmi_dailinks), 321 .dapm_widgets = rk_hdmi_dapm_widgets, 322 .num_dapm_widgets = ARRAY_SIZE(rk_hdmi_dapm_widgets), 323 .dapm_routes = rk_hdmi_audio_map, 324 .num_dapm_routes = ARRAY_SIZE(rk_hdmi_audio_map), 325 .controls = rk_hdmi_controls, 326 .num_controls = ARRAY_SIZE(rk_hdmi_controls), 327 }; 328 329 static struct snd_soc_card rockchip_max98090_hdmi_card = { 330 .name = "ROCKCHIP-MAX98090-HDMI", 331 .owner = THIS_MODULE, 332 .dai_link = rk_max98090_hdmi_dailinks, 333 .num_links = ARRAY_SIZE(rk_max98090_hdmi_dailinks), 334 .aux_dev = &rk_98090_headset_dev, 335 .num_aux_devs = 1, 336 .dapm_widgets = rk_max98090_hdmi_dapm_widgets, 337 .num_dapm_widgets = ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets), 338 .dapm_routes = rk_max98090_hdmi_audio_map, 339 .num_dapm_routes = ARRAY_SIZE(rk_max98090_hdmi_audio_map), 340 .controls = rk_max98090_hdmi_controls, 341 .num_controls = ARRAY_SIZE(rk_max98090_hdmi_controls), 342 }; 343 344 static int rk_98090_headset_init(struct snd_soc_component *component) 345 { 346 int ret; 347 348 /* Enable Headset and 4 Buttons Jack detection */ 349 ret = snd_soc_card_jack_new(component->card, "Headset Jack", 350 SND_JACK_HEADSET | 351 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 352 SND_JACK_BTN_2 | SND_JACK_BTN_3, 353 &headset_jack, 354 headset_jack_pins, 355 ARRAY_SIZE(headset_jack_pins)); 356 if (ret) 357 return ret; 358 359 ret = ts3a227e_enable_jack_detect(component, &headset_jack); 360 361 return ret; 362 } 363 364 static int rk_parse_headset_from_of(struct device *dev, struct device_node *np) 365 { 366 rk_98090_headset_dev.dlc.of_node = of_parse_phandle( 367 np, "rockchip,headset-codec", 0); 368 if (!rk_98090_headset_dev.dlc.of_node) { 369 dev_err(dev, 370 "Property 'rockchip,headset-codec' missing/invalid\n"); 371 return -EINVAL; 372 } 373 return 0; 374 } 375 376 static int snd_rk_mc_probe(struct platform_device *pdev) 377 { 378 int ret = 0; 379 struct snd_soc_card *card; 380 struct device *dev = &pdev->dev; 381 struct device_node *np = pdev->dev.of_node; 382 struct device_node *np_cpu; 383 struct device_node *np_audio, *np_hdmi; 384 385 /* Parse DTS for I2S controller. */ 386 np_cpu = of_parse_phandle(np, "rockchip,i2s-controller", 0); 387 388 if (!np_cpu) { 389 dev_err(&pdev->dev, 390 "Property 'rockchip,i2s-controller missing or invalid\n"); 391 return -EINVAL; 392 } 393 394 /* 395 * Find the card to use based on the presences of audio codec 396 * and hdmi codec in device property. Set their of_node accordingly. 397 */ 398 np_audio = of_parse_phandle(np, "rockchip,audio-codec", 0); 399 np_hdmi = of_parse_phandle(np, "rockchip,hdmi-codec", 0); 400 if (np_audio && np_hdmi) { 401 card = &rockchip_max98090_hdmi_card; 402 card->dai_link[DAILINK_MAX98090].codecs->of_node = np_audio; 403 card->dai_link[DAILINK_HDMI].codecs->of_node = np_hdmi; 404 card->dai_link[DAILINK_MAX98090].cpus->of_node = np_cpu; 405 card->dai_link[DAILINK_MAX98090].platforms->of_node = np_cpu; 406 card->dai_link[DAILINK_HDMI].cpus->of_node = np_cpu; 407 card->dai_link[DAILINK_HDMI].platforms->of_node = np_cpu; 408 } else if (np_audio) { 409 card = &rockchip_max98090_card; 410 card->dai_link[0].codecs->of_node = np_audio; 411 card->dai_link[0].cpus->of_node = np_cpu; 412 card->dai_link[0].platforms->of_node = np_cpu; 413 } else if (np_hdmi) { 414 card = &rockchip_hdmi_card; 415 card->dai_link[0].codecs->of_node = np_hdmi; 416 card->dai_link[0].cpus->of_node = np_cpu; 417 card->dai_link[0].platforms->of_node = np_cpu; 418 } else { 419 dev_err(dev, "At least one of codecs should be specified\n"); 420 return -EINVAL; 421 } 422 423 card->dev = dev; 424 425 /* Parse headset detection codec. */ 426 if (np_audio) { 427 ret = rk_parse_headset_from_of(dev, np); 428 if (ret) 429 return ret; 430 } 431 432 /* Parse card name. */ 433 ret = snd_soc_of_parse_card_name(card, "rockchip,model"); 434 if (ret) { 435 dev_err(&pdev->dev, 436 "Soc parse card name failed %d\n", ret); 437 return ret; 438 } 439 440 /* register the soc card */ 441 ret = devm_snd_soc_register_card(&pdev->dev, card); 442 if (ret) { 443 dev_err(&pdev->dev, 444 "Soc register card failed %d\n", ret); 445 return ret; 446 } 447 448 return ret; 449 } 450 451 static const struct of_device_id rockchip_max98090_of_match[] = { 452 { .compatible = "rockchip,rockchip-audio-max98090", }, 453 {}, 454 }; 455 456 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match); 457 458 static struct platform_driver snd_rk_mc_driver = { 459 .probe = snd_rk_mc_probe, 460 .driver = { 461 .name = DRV_NAME, 462 .pm = &snd_soc_pm_ops, 463 .of_match_table = rockchip_max98090_of_match, 464 }, 465 }; 466 467 module_platform_driver(snd_rk_mc_driver); 468 469 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>"); 470 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver"); 471 MODULE_LICENSE("GPL v2"); 472 MODULE_ALIAS("platform:" DRV_NAME); 473