1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * rx51.c -- SoC audio for Nokia RX-51 4 * 5 * Copyright (C) 2008 - 2009 Nokia Corporation 6 * 7 * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com> 8 * Eduardo Valentin <eduardo.valentin@nokia.com> 9 * Jarkko Nikula <jarkko.nikula@bitmer.com> 10 */ 11 12 #include <linux/delay.h> 13 #include <linux/gpio.h> 14 #include <linux/platform_device.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/module.h> 17 #include <sound/core.h> 18 #include <sound/jack.h> 19 #include <sound/pcm.h> 20 #include <sound/soc.h> 21 #include <linux/platform_data/asoc-ti-mcbsp.h> 22 23 #include <asm/mach-types.h> 24 25 #include "omap-mcbsp.h" 26 27 enum { 28 RX51_JACK_DISABLED, 29 RX51_JACK_TVOUT, /* tv-out with stereo output */ 30 RX51_JACK_HP, /* headphone: stereo output, no mic */ 31 RX51_JACK_HS, /* headset: stereo output with mic */ 32 }; 33 34 struct rx51_audio_pdata { 35 struct gpio_desc *tvout_selection_gpio; 36 struct gpio_desc *jack_detection_gpio; 37 struct gpio_desc *eci_sw_gpio; 38 struct gpio_desc *speaker_amp_gpio; 39 }; 40 41 static int rx51_spk_func; 42 static int rx51_dmic_func; 43 static int rx51_jack_func; 44 45 static void rx51_ext_control(struct snd_soc_dapm_context *dapm) 46 { 47 struct snd_soc_card *card = dapm->card; 48 struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card); 49 int hp = 0, hs = 0, tvout = 0; 50 51 switch (rx51_jack_func) { 52 case RX51_JACK_TVOUT: 53 tvout = 1; 54 hp = 1; 55 break; 56 case RX51_JACK_HS: 57 hs = 1; 58 case RX51_JACK_HP: 59 hp = 1; 60 break; 61 } 62 63 snd_soc_dapm_mutex_lock(dapm); 64 65 if (rx51_spk_func) 66 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk"); 67 else 68 snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk"); 69 if (rx51_dmic_func) 70 snd_soc_dapm_enable_pin_unlocked(dapm, "DMic"); 71 else 72 snd_soc_dapm_disable_pin_unlocked(dapm, "DMic"); 73 if (hp) 74 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack"); 75 else 76 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack"); 77 if (hs) 78 snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic"); 79 else 80 snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic"); 81 82 gpiod_set_value(pdata->tvout_selection_gpio, tvout); 83 84 snd_soc_dapm_sync_unlocked(dapm); 85 86 snd_soc_dapm_mutex_unlock(dapm); 87 } 88 89 static int rx51_startup(struct snd_pcm_substream *substream) 90 { 91 struct snd_pcm_runtime *runtime = substream->runtime; 92 struct snd_soc_pcm_runtime *rtd = substream->private_data; 93 struct snd_soc_card *card = rtd->card; 94 95 snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2); 96 rx51_ext_control(&card->dapm); 97 98 return 0; 99 } 100 101 static int rx51_hw_params(struct snd_pcm_substream *substream, 102 struct snd_pcm_hw_params *params) 103 { 104 struct snd_soc_pcm_runtime *rtd = substream->private_data; 105 struct snd_soc_dai *codec_dai = rtd->codec_dai; 106 107 /* Set the codec system clock for DAC and ADC */ 108 return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000, 109 SND_SOC_CLOCK_IN); 110 } 111 112 static const struct snd_soc_ops rx51_ops = { 113 .startup = rx51_startup, 114 .hw_params = rx51_hw_params, 115 }; 116 117 static int rx51_get_spk(struct snd_kcontrol *kcontrol, 118 struct snd_ctl_elem_value *ucontrol) 119 { 120 ucontrol->value.enumerated.item[0] = rx51_spk_func; 121 122 return 0; 123 } 124 125 static int rx51_set_spk(struct snd_kcontrol *kcontrol, 126 struct snd_ctl_elem_value *ucontrol) 127 { 128 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 129 130 if (rx51_spk_func == ucontrol->value.enumerated.item[0]) 131 return 0; 132 133 rx51_spk_func = ucontrol->value.enumerated.item[0]; 134 rx51_ext_control(&card->dapm); 135 136 return 1; 137 } 138 139 static int rx51_spk_event(struct snd_soc_dapm_widget *w, 140 struct snd_kcontrol *k, int event) 141 { 142 struct snd_soc_dapm_context *dapm = w->dapm; 143 struct snd_soc_card *card = dapm->card; 144 struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card); 145 146 gpiod_set_raw_value_cansleep(pdata->speaker_amp_gpio, 147 !!SND_SOC_DAPM_EVENT_ON(event)); 148 149 return 0; 150 } 151 152 static int rx51_get_input(struct snd_kcontrol *kcontrol, 153 struct snd_ctl_elem_value *ucontrol) 154 { 155 ucontrol->value.enumerated.item[0] = rx51_dmic_func; 156 157 return 0; 158 } 159 160 static int rx51_set_input(struct snd_kcontrol *kcontrol, 161 struct snd_ctl_elem_value *ucontrol) 162 { 163 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 164 165 if (rx51_dmic_func == ucontrol->value.enumerated.item[0]) 166 return 0; 167 168 rx51_dmic_func = ucontrol->value.enumerated.item[0]; 169 rx51_ext_control(&card->dapm); 170 171 return 1; 172 } 173 174 static int rx51_get_jack(struct snd_kcontrol *kcontrol, 175 struct snd_ctl_elem_value *ucontrol) 176 { 177 ucontrol->value.enumerated.item[0] = rx51_jack_func; 178 179 return 0; 180 } 181 182 static int rx51_set_jack(struct snd_kcontrol *kcontrol, 183 struct snd_ctl_elem_value *ucontrol) 184 { 185 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 186 187 if (rx51_jack_func == ucontrol->value.enumerated.item[0]) 188 return 0; 189 190 rx51_jack_func = ucontrol->value.enumerated.item[0]; 191 rx51_ext_control(&card->dapm); 192 193 return 1; 194 } 195 196 static struct snd_soc_jack rx51_av_jack; 197 198 static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = { 199 { 200 .name = "avdet-gpio", 201 .report = SND_JACK_HEADSET, 202 .invert = 1, 203 .debounce_time = 200, 204 }, 205 }; 206 207 static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = { 208 SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event), 209 SND_SOC_DAPM_MIC("DMic", NULL), 210 SND_SOC_DAPM_HP("Headphone Jack", NULL), 211 SND_SOC_DAPM_MIC("HS Mic", NULL), 212 SND_SOC_DAPM_LINE("FM Transmitter", NULL), 213 SND_SOC_DAPM_SPK("Earphone", NULL), 214 }; 215 216 static const struct snd_soc_dapm_route audio_map[] = { 217 {"Ext Spk", NULL, "HPLOUT"}, 218 {"Ext Spk", NULL, "HPROUT"}, 219 {"Ext Spk", NULL, "HPLCOM"}, 220 {"Ext Spk", NULL, "HPRCOM"}, 221 {"FM Transmitter", NULL, "LLOUT"}, 222 {"FM Transmitter", NULL, "RLOUT"}, 223 224 {"Headphone Jack", NULL, "TPA6130A2 HPLEFT"}, 225 {"Headphone Jack", NULL, "TPA6130A2 HPRIGHT"}, 226 {"TPA6130A2 LEFTIN", NULL, "LLOUT"}, 227 {"TPA6130A2 RIGHTIN", NULL, "RLOUT"}, 228 229 {"DMic Rate 64", NULL, "DMic"}, 230 {"DMic", NULL, "Mic Bias"}, 231 232 {"b LINE2R", NULL, "MONO_LOUT"}, 233 {"Earphone", NULL, "b HPLOUT"}, 234 235 {"LINE1L", NULL, "HS Mic"}, 236 {"HS Mic", NULL, "b Mic Bias"}, 237 }; 238 239 static const char * const spk_function[] = {"Off", "On"}; 240 static const char * const input_function[] = {"ADC", "Digital Mic"}; 241 static const char * const jack_function[] = { 242 "Off", "TV-OUT", "Headphone", "Headset" 243 }; 244 245 static const struct soc_enum rx51_enum[] = { 246 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function), 247 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function), 248 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function), 249 }; 250 251 static const struct snd_kcontrol_new aic34_rx51_controls[] = { 252 SOC_ENUM_EXT("Speaker Function", rx51_enum[0], 253 rx51_get_spk, rx51_set_spk), 254 SOC_ENUM_EXT("Input Select", rx51_enum[1], 255 rx51_get_input, rx51_set_input), 256 SOC_ENUM_EXT("Jack Function", rx51_enum[2], 257 rx51_get_jack, rx51_set_jack), 258 SOC_DAPM_PIN_SWITCH("FM Transmitter"), 259 SOC_DAPM_PIN_SWITCH("Earphone"), 260 }; 261 262 static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd) 263 { 264 struct snd_soc_card *card = rtd->card; 265 struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card); 266 int err; 267 268 snd_soc_limit_volume(card, "TPA6130A2 Headphone Playback Volume", 42); 269 270 err = omap_mcbsp_st_add_controls(rtd, 2); 271 if (err < 0) { 272 dev_err(card->dev, "Failed to add MCBSP controls\n"); 273 return err; 274 } 275 276 /* AV jack detection */ 277 err = snd_soc_card_jack_new(rtd->card, "AV Jack", 278 SND_JACK_HEADSET | SND_JACK_VIDEOOUT, 279 &rx51_av_jack, NULL, 0); 280 if (err) { 281 dev_err(card->dev, "Failed to add AV Jack\n"); 282 return err; 283 } 284 285 /* prepare gpio for snd_soc_jack_add_gpios */ 286 rx51_av_jack_gpios[0].gpio = desc_to_gpio(pdata->jack_detection_gpio); 287 devm_gpiod_put(card->dev, pdata->jack_detection_gpio); 288 289 err = snd_soc_jack_add_gpios(&rx51_av_jack, 290 ARRAY_SIZE(rx51_av_jack_gpios), 291 rx51_av_jack_gpios); 292 if (err) { 293 dev_err(card->dev, "Failed to add GPIOs\n"); 294 return err; 295 } 296 297 return err; 298 } 299 300 /* Digital audio interface glue - connects codec <--> CPU */ 301 SND_SOC_DAILINK_DEFS(aic34, 302 DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.2")), 303 DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.2-0018", 304 "tlv320aic3x-hifi")), 305 DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.2"))); 306 307 static struct snd_soc_dai_link rx51_dai[] = { 308 { 309 .name = "TLV320AIC34", 310 .stream_name = "AIC34", 311 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF | 312 SND_SOC_DAIFMT_CBM_CFM, 313 .init = rx51_aic34_init, 314 .ops = &rx51_ops, 315 SND_SOC_DAILINK_REG(aic34), 316 }, 317 }; 318 319 static struct snd_soc_aux_dev rx51_aux_dev[] = { 320 { 321 .name = "TLV320AIC34b", 322 .codec_name = "tlv320aic3x-codec.2-0019", 323 }, 324 { 325 .name = "TPA61320A2", 326 .codec_name = "tpa6130a2.2-0060", 327 }, 328 }; 329 330 static struct snd_soc_codec_conf rx51_codec_conf[] = { 331 { 332 .dev_name = "tlv320aic3x-codec.2-0019", 333 .name_prefix = "b", 334 }, 335 { 336 .dev_name = "tpa6130a2.2-0060", 337 .name_prefix = "TPA6130A2", 338 }, 339 }; 340 341 /* Audio card */ 342 static struct snd_soc_card rx51_sound_card = { 343 .name = "RX-51", 344 .owner = THIS_MODULE, 345 .dai_link = rx51_dai, 346 .num_links = ARRAY_SIZE(rx51_dai), 347 .aux_dev = rx51_aux_dev, 348 .num_aux_devs = ARRAY_SIZE(rx51_aux_dev), 349 .codec_conf = rx51_codec_conf, 350 .num_configs = ARRAY_SIZE(rx51_codec_conf), 351 .fully_routed = true, 352 353 .controls = aic34_rx51_controls, 354 .num_controls = ARRAY_SIZE(aic34_rx51_controls), 355 .dapm_widgets = aic34_dapm_widgets, 356 .num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets), 357 .dapm_routes = audio_map, 358 .num_dapm_routes = ARRAY_SIZE(audio_map), 359 }; 360 361 static int rx51_soc_probe(struct platform_device *pdev) 362 { 363 struct rx51_audio_pdata *pdata; 364 struct device_node *np = pdev->dev.of_node; 365 struct snd_soc_card *card = &rx51_sound_card; 366 int err; 367 368 if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900")) 369 return -ENODEV; 370 371 card->dev = &pdev->dev; 372 373 if (np) { 374 struct device_node *dai_node; 375 376 dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0); 377 if (!dai_node) { 378 dev_err(&pdev->dev, "McBSP node is not provided\n"); 379 return -EINVAL; 380 } 381 rx51_dai[0].cpus->dai_name = NULL; 382 rx51_dai[0].platforms->name = NULL; 383 rx51_dai[0].cpus->of_node = dai_node; 384 rx51_dai[0].platforms->of_node = dai_node; 385 386 dai_node = of_parse_phandle(np, "nokia,audio-codec", 0); 387 if (!dai_node) { 388 dev_err(&pdev->dev, "Codec node is not provided\n"); 389 return -EINVAL; 390 } 391 rx51_dai[0].codecs->name = NULL; 392 rx51_dai[0].codecs->of_node = dai_node; 393 394 dai_node = of_parse_phandle(np, "nokia,audio-codec", 1); 395 if (!dai_node) { 396 dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n"); 397 return -EINVAL; 398 } 399 rx51_aux_dev[0].codec_name = NULL; 400 rx51_aux_dev[0].codec_of_node = dai_node; 401 rx51_codec_conf[0].dev_name = NULL; 402 rx51_codec_conf[0].of_node = dai_node; 403 404 dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0); 405 if (!dai_node) { 406 dev_err(&pdev->dev, "Headphone amplifier node is not provided\n"); 407 return -EINVAL; 408 } 409 rx51_aux_dev[1].codec_name = NULL; 410 rx51_aux_dev[1].codec_of_node = dai_node; 411 rx51_codec_conf[1].dev_name = NULL; 412 rx51_codec_conf[1].of_node = dai_node; 413 } 414 415 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 416 if (pdata == NULL) 417 return -ENOMEM; 418 419 snd_soc_card_set_drvdata(card, pdata); 420 421 pdata->tvout_selection_gpio = devm_gpiod_get(card->dev, 422 "tvout-selection", 423 GPIOD_OUT_LOW); 424 if (IS_ERR(pdata->tvout_selection_gpio)) { 425 dev_err(card->dev, "could not get tvout selection gpio\n"); 426 return PTR_ERR(pdata->tvout_selection_gpio); 427 } 428 429 pdata->jack_detection_gpio = devm_gpiod_get(card->dev, 430 "jack-detection", 431 GPIOD_ASIS); 432 if (IS_ERR(pdata->jack_detection_gpio)) { 433 dev_err(card->dev, "could not get jack detection gpio\n"); 434 return PTR_ERR(pdata->jack_detection_gpio); 435 } 436 437 pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch", 438 GPIOD_OUT_HIGH); 439 if (IS_ERR(pdata->eci_sw_gpio)) { 440 dev_err(card->dev, "could not get eci switch gpio\n"); 441 return PTR_ERR(pdata->eci_sw_gpio); 442 } 443 444 pdata->speaker_amp_gpio = devm_gpiod_get(card->dev, 445 "speaker-amplifier", 446 GPIOD_OUT_LOW); 447 if (IS_ERR(pdata->speaker_amp_gpio)) { 448 dev_err(card->dev, "could not get speaker enable gpio\n"); 449 return PTR_ERR(pdata->speaker_amp_gpio); 450 } 451 452 err = devm_snd_soc_register_card(card->dev, card); 453 if (err) { 454 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err); 455 return err; 456 } 457 458 return 0; 459 } 460 461 #if defined(CONFIG_OF) 462 static const struct of_device_id rx51_audio_of_match[] = { 463 { .compatible = "nokia,n900-audio", }, 464 {}, 465 }; 466 MODULE_DEVICE_TABLE(of, rx51_audio_of_match); 467 #endif 468 469 static struct platform_driver rx51_soc_driver = { 470 .driver = { 471 .name = "rx51-audio", 472 .of_match_table = of_match_ptr(rx51_audio_of_match), 473 }, 474 .probe = rx51_soc_probe, 475 }; 476 477 module_platform_driver(rx51_soc_driver); 478 479 MODULE_AUTHOR("Nokia Corporation"); 480 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51"); 481 MODULE_LICENSE("GPL"); 482 MODULE_ALIAS("platform:rx51-audio"); 483