Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * rx51.c  --  SoC audio for Nokia RX-51
0004  *
0005  * Copyright (C) 2008 - 2009 Nokia Corporation
0006  *
0007  * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
0008  *          Eduardo Valentin <eduardo.valentin@nokia.com>
0009  *          Jarkko Nikula <jarkko.nikula@bitmer.com>
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/gpio.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/module.h>
0017 #include <sound/core.h>
0018 #include <sound/jack.h>
0019 #include <sound/pcm.h>
0020 #include <sound/soc.h>
0021 #include <linux/platform_data/asoc-ti-mcbsp.h>
0022 
0023 #include <asm/mach-types.h>
0024 
0025 #include "omap-mcbsp.h"
0026 
0027 enum {
0028     RX51_JACK_DISABLED,
0029     RX51_JACK_TVOUT,        /* tv-out with stereo output */
0030     RX51_JACK_HP,           /* headphone: stereo output, no mic */
0031     RX51_JACK_HS,           /* headset: stereo output with mic */
0032 };
0033 
0034 struct rx51_audio_pdata {
0035     struct gpio_desc *tvout_selection_gpio;
0036     struct gpio_desc *jack_detection_gpio;
0037     struct gpio_desc *eci_sw_gpio;
0038     struct gpio_desc *speaker_amp_gpio;
0039 };
0040 
0041 static int rx51_spk_func;
0042 static int rx51_dmic_func;
0043 static int rx51_jack_func;
0044 
0045 static void rx51_ext_control(struct snd_soc_dapm_context *dapm)
0046 {
0047     struct snd_soc_card *card = dapm->card;
0048     struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
0049     int hp = 0, hs = 0, tvout = 0;
0050 
0051     switch (rx51_jack_func) {
0052     case RX51_JACK_TVOUT:
0053         tvout = 1;
0054         hp = 1;
0055         break;
0056     case RX51_JACK_HS:
0057         hs = 1;
0058         fallthrough;
0059     case RX51_JACK_HP:
0060         hp = 1;
0061         break;
0062     }
0063 
0064     snd_soc_dapm_mutex_lock(dapm);
0065 
0066     if (rx51_spk_func)
0067         snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
0068     else
0069         snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
0070     if (rx51_dmic_func)
0071         snd_soc_dapm_enable_pin_unlocked(dapm, "DMic");
0072     else
0073         snd_soc_dapm_disable_pin_unlocked(dapm, "DMic");
0074     if (hp)
0075         snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
0076     else
0077         snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
0078     if (hs)
0079         snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic");
0080     else
0081         snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic");
0082 
0083     gpiod_set_value(pdata->tvout_selection_gpio, tvout);
0084 
0085     snd_soc_dapm_sync_unlocked(dapm);
0086 
0087     snd_soc_dapm_mutex_unlock(dapm);
0088 }
0089 
0090 static int rx51_startup(struct snd_pcm_substream *substream)
0091 {
0092     struct snd_pcm_runtime *runtime = substream->runtime;
0093     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0094     struct snd_soc_card *card = rtd->card;
0095 
0096     snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
0097     rx51_ext_control(&card->dapm);
0098 
0099     return 0;
0100 }
0101 
0102 static int rx51_hw_params(struct snd_pcm_substream *substream,
0103     struct snd_pcm_hw_params *params)
0104 {
0105     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0106     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0107 
0108     /* Set the codec system clock for DAC and ADC */
0109     return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
0110                       SND_SOC_CLOCK_IN);
0111 }
0112 
0113 static const struct snd_soc_ops rx51_ops = {
0114     .startup = rx51_startup,
0115     .hw_params = rx51_hw_params,
0116 };
0117 
0118 static int rx51_get_spk(struct snd_kcontrol *kcontrol,
0119             struct snd_ctl_elem_value *ucontrol)
0120 {
0121     ucontrol->value.enumerated.item[0] = rx51_spk_func;
0122 
0123     return 0;
0124 }
0125 
0126 static int rx51_set_spk(struct snd_kcontrol *kcontrol,
0127             struct snd_ctl_elem_value *ucontrol)
0128 {
0129     struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
0130 
0131     if (rx51_spk_func == ucontrol->value.enumerated.item[0])
0132         return 0;
0133 
0134     rx51_spk_func = ucontrol->value.enumerated.item[0];
0135     rx51_ext_control(&card->dapm);
0136 
0137     return 1;
0138 }
0139 
0140 static int rx51_spk_event(struct snd_soc_dapm_widget *w,
0141               struct snd_kcontrol *k, int event)
0142 {
0143     struct snd_soc_dapm_context *dapm = w->dapm;
0144     struct snd_soc_card *card = dapm->card;
0145     struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
0146 
0147     gpiod_set_raw_value_cansleep(pdata->speaker_amp_gpio,
0148                      !!SND_SOC_DAPM_EVENT_ON(event));
0149 
0150     return 0;
0151 }
0152 
0153 static int rx51_get_input(struct snd_kcontrol *kcontrol,
0154               struct snd_ctl_elem_value *ucontrol)
0155 {
0156     ucontrol->value.enumerated.item[0] = rx51_dmic_func;
0157 
0158     return 0;
0159 }
0160 
0161 static int rx51_set_input(struct snd_kcontrol *kcontrol,
0162               struct snd_ctl_elem_value *ucontrol)
0163 {
0164     struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
0165 
0166     if (rx51_dmic_func == ucontrol->value.enumerated.item[0])
0167         return 0;
0168 
0169     rx51_dmic_func = ucontrol->value.enumerated.item[0];
0170     rx51_ext_control(&card->dapm);
0171 
0172     return 1;
0173 }
0174 
0175 static int rx51_get_jack(struct snd_kcontrol *kcontrol,
0176              struct snd_ctl_elem_value *ucontrol)
0177 {
0178     ucontrol->value.enumerated.item[0] = rx51_jack_func;
0179 
0180     return 0;
0181 }
0182 
0183 static int rx51_set_jack(struct snd_kcontrol *kcontrol,
0184              struct snd_ctl_elem_value *ucontrol)
0185 {
0186     struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
0187 
0188     if (rx51_jack_func == ucontrol->value.enumerated.item[0])
0189         return 0;
0190 
0191     rx51_jack_func = ucontrol->value.enumerated.item[0];
0192     rx51_ext_control(&card->dapm);
0193 
0194     return 1;
0195 }
0196 
0197 static struct snd_soc_jack rx51_av_jack;
0198 
0199 static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
0200     {
0201         .name = "avdet-gpio",
0202         .report = SND_JACK_HEADSET,
0203         .invert = 1,
0204         .debounce_time = 200,
0205     },
0206 };
0207 
0208 static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
0209     SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
0210     SND_SOC_DAPM_MIC("DMic", NULL),
0211     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0212     SND_SOC_DAPM_MIC("HS Mic", NULL),
0213     SND_SOC_DAPM_LINE("FM Transmitter", NULL),
0214     SND_SOC_DAPM_SPK("Earphone", NULL),
0215 };
0216 
0217 static const struct snd_soc_dapm_route audio_map[] = {
0218     {"Ext Spk", NULL, "HPLOUT"},
0219     {"Ext Spk", NULL, "HPROUT"},
0220     {"Ext Spk", NULL, "HPLCOM"},
0221     {"Ext Spk", NULL, "HPRCOM"},
0222     {"FM Transmitter", NULL, "LLOUT"},
0223     {"FM Transmitter", NULL, "RLOUT"},
0224 
0225     {"Headphone Jack", NULL, "TPA6130A2 HPLEFT"},
0226     {"Headphone Jack", NULL, "TPA6130A2 HPRIGHT"},
0227     {"TPA6130A2 LEFTIN", NULL, "LLOUT"},
0228     {"TPA6130A2 RIGHTIN", NULL, "RLOUT"},
0229 
0230     {"DMic Rate 64", NULL, "DMic"},
0231     {"DMic", NULL, "Mic Bias"},
0232 
0233     {"b LINE2R", NULL, "MONO_LOUT"},
0234     {"Earphone", NULL, "b HPLOUT"},
0235 
0236     {"LINE1L", NULL, "HS Mic"},
0237     {"HS Mic", NULL, "b Mic Bias"},
0238 };
0239 
0240 static const char * const spk_function[] = {"Off", "On"};
0241 static const char * const input_function[] = {"ADC", "Digital Mic"};
0242 static const char * const jack_function[] = {
0243     "Off", "TV-OUT", "Headphone", "Headset"
0244 };
0245 
0246 static const struct soc_enum rx51_enum[] = {
0247     SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
0248     SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
0249     SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
0250 };
0251 
0252 static const struct snd_kcontrol_new aic34_rx51_controls[] = {
0253     SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
0254              rx51_get_spk, rx51_set_spk),
0255     SOC_ENUM_EXT("Input Select",  rx51_enum[1],
0256              rx51_get_input, rx51_set_input),
0257     SOC_ENUM_EXT("Jack Function", rx51_enum[2],
0258              rx51_get_jack, rx51_set_jack),
0259     SOC_DAPM_PIN_SWITCH("FM Transmitter"),
0260     SOC_DAPM_PIN_SWITCH("Earphone"),
0261 };
0262 
0263 static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
0264 {
0265     struct snd_soc_card *card = rtd->card;
0266     struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
0267     int err;
0268 
0269     snd_soc_limit_volume(card, "TPA6130A2 Headphone Playback Volume", 42);
0270 
0271     err = omap_mcbsp_st_add_controls(rtd, 2);
0272     if (err < 0) {
0273         dev_err(card->dev, "Failed to add MCBSP controls\n");
0274         return err;
0275     }
0276 
0277     /* AV jack detection */
0278     err = snd_soc_card_jack_new(rtd->card, "AV Jack",
0279                     SND_JACK_HEADSET | SND_JACK_VIDEOOUT,
0280                     &rx51_av_jack);
0281     if (err) {
0282         dev_err(card->dev, "Failed to add AV Jack\n");
0283         return err;
0284     }
0285 
0286     /* prepare gpio for snd_soc_jack_add_gpios */
0287     rx51_av_jack_gpios[0].gpio = desc_to_gpio(pdata->jack_detection_gpio);
0288     devm_gpiod_put(card->dev, pdata->jack_detection_gpio);
0289 
0290     err = snd_soc_jack_add_gpios(&rx51_av_jack,
0291                      ARRAY_SIZE(rx51_av_jack_gpios),
0292                      rx51_av_jack_gpios);
0293     if (err) {
0294         dev_err(card->dev, "Failed to add GPIOs\n");
0295         return err;
0296     }
0297 
0298     return err;
0299 }
0300 
0301 /* Digital audio interface glue - connects codec <--> CPU */
0302 SND_SOC_DAILINK_DEFS(aic34,
0303     DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.2")),
0304     DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.2-0018",
0305                       "tlv320aic3x-hifi")),
0306     DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.2")));
0307 
0308 static struct snd_soc_dai_link rx51_dai[] = {
0309     {
0310         .name = "TLV320AIC34",
0311         .stream_name = "AIC34",
0312         .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
0313                SND_SOC_DAIFMT_CBM_CFM,
0314         .init = rx51_aic34_init,
0315         .ops = &rx51_ops,
0316         SND_SOC_DAILINK_REG(aic34),
0317     },
0318 };
0319 
0320 static struct snd_soc_aux_dev rx51_aux_dev[] = {
0321     {
0322         .dlc = COMP_AUX("tlv320aic3x-codec.2-0019"),
0323     },
0324     {
0325         .dlc = COMP_AUX("tpa6130a2.2-0060"),
0326     },
0327 };
0328 
0329 static struct snd_soc_codec_conf rx51_codec_conf[] = {
0330     {
0331         .dlc = COMP_CODEC_CONF("tlv320aic3x-codec.2-0019"),
0332         .name_prefix = "b",
0333     },
0334     {
0335         .dlc = COMP_CODEC_CONF("tpa6130a2.2-0060"),
0336         .name_prefix = "TPA6130A2",
0337     },
0338 };
0339 
0340 /* Audio card */
0341 static struct snd_soc_card rx51_sound_card = {
0342     .name = "RX-51",
0343     .owner = THIS_MODULE,
0344     .dai_link = rx51_dai,
0345     .num_links = ARRAY_SIZE(rx51_dai),
0346     .aux_dev = rx51_aux_dev,
0347     .num_aux_devs = ARRAY_SIZE(rx51_aux_dev),
0348     .codec_conf = rx51_codec_conf,
0349     .num_configs = ARRAY_SIZE(rx51_codec_conf),
0350     .fully_routed = true,
0351 
0352     .controls = aic34_rx51_controls,
0353     .num_controls = ARRAY_SIZE(aic34_rx51_controls),
0354     .dapm_widgets = aic34_dapm_widgets,
0355     .num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets),
0356     .dapm_routes = audio_map,
0357     .num_dapm_routes = ARRAY_SIZE(audio_map),
0358 };
0359 
0360 static int rx51_soc_probe(struct platform_device *pdev)
0361 {
0362     struct rx51_audio_pdata *pdata;
0363     struct device_node *np = pdev->dev.of_node;
0364     struct snd_soc_card *card = &rx51_sound_card;
0365     int err;
0366 
0367     if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
0368         return -ENODEV;
0369 
0370     card->dev = &pdev->dev;
0371 
0372     if (np) {
0373         struct device_node *dai_node;
0374 
0375         dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0);
0376         if (!dai_node) {
0377             dev_err(&pdev->dev, "McBSP node is not provided\n");
0378             return -EINVAL;
0379         }
0380         rx51_dai[0].cpus->dai_name = NULL;
0381         rx51_dai[0].platforms->name = NULL;
0382         rx51_dai[0].cpus->of_node = dai_node;
0383         rx51_dai[0].platforms->of_node = dai_node;
0384 
0385         dai_node = of_parse_phandle(np, "nokia,audio-codec", 0);
0386         if (!dai_node) {
0387             dev_err(&pdev->dev, "Codec node is not provided\n");
0388             return -EINVAL;
0389         }
0390         rx51_dai[0].codecs->name = NULL;
0391         rx51_dai[0].codecs->of_node = dai_node;
0392 
0393         dai_node = of_parse_phandle(np, "nokia,audio-codec", 1);
0394         if (!dai_node) {
0395             dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n");
0396             return -EINVAL;
0397         }
0398         rx51_aux_dev[0].dlc.name = NULL;
0399         rx51_aux_dev[0].dlc.of_node = dai_node;
0400         rx51_codec_conf[0].dlc.name = NULL;
0401         rx51_codec_conf[0].dlc.of_node = dai_node;
0402 
0403         dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0);
0404         if (!dai_node) {
0405             dev_err(&pdev->dev, "Headphone amplifier node is not provided\n");
0406             return -EINVAL;
0407         }
0408         rx51_aux_dev[1].dlc.name = NULL;
0409         rx51_aux_dev[1].dlc.of_node = dai_node;
0410         rx51_codec_conf[1].dlc.name = NULL;
0411         rx51_codec_conf[1].dlc.of_node = dai_node;
0412     }
0413 
0414     pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0415     if (pdata == NULL)
0416         return -ENOMEM;
0417 
0418     snd_soc_card_set_drvdata(card, pdata);
0419 
0420     pdata->tvout_selection_gpio = devm_gpiod_get(card->dev,
0421                              "tvout-selection",
0422                              GPIOD_OUT_LOW);
0423     if (IS_ERR(pdata->tvout_selection_gpio)) {
0424         dev_err(card->dev, "could not get tvout selection gpio\n");
0425         return PTR_ERR(pdata->tvout_selection_gpio);
0426     }
0427 
0428     pdata->jack_detection_gpio = devm_gpiod_get(card->dev,
0429                             "jack-detection",
0430                             GPIOD_ASIS);
0431     if (IS_ERR(pdata->jack_detection_gpio)) {
0432         dev_err(card->dev, "could not get jack detection gpio\n");
0433         return PTR_ERR(pdata->jack_detection_gpio);
0434     }
0435 
0436     pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch",
0437                         GPIOD_OUT_HIGH);
0438     if (IS_ERR(pdata->eci_sw_gpio)) {
0439         dev_err(card->dev, "could not get eci switch gpio\n");
0440         return PTR_ERR(pdata->eci_sw_gpio);
0441     }
0442 
0443     pdata->speaker_amp_gpio = devm_gpiod_get(card->dev,
0444                          "speaker-amplifier",
0445                          GPIOD_OUT_LOW);
0446     if (IS_ERR(pdata->speaker_amp_gpio)) {
0447         dev_err(card->dev, "could not get speaker enable gpio\n");
0448         return PTR_ERR(pdata->speaker_amp_gpio);
0449     }
0450 
0451     err = devm_snd_soc_register_card(card->dev, card);
0452     if (err) {
0453         dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err);
0454         return err;
0455     }
0456 
0457     return 0;
0458 }
0459 
0460 #if defined(CONFIG_OF)
0461 static const struct of_device_id rx51_audio_of_match[] = {
0462     { .compatible = "nokia,n900-audio", },
0463     {},
0464 };
0465 MODULE_DEVICE_TABLE(of, rx51_audio_of_match);
0466 #endif
0467 
0468 static struct platform_driver rx51_soc_driver = {
0469     .driver = {
0470         .name = "rx51-audio",
0471         .of_match_table = of_match_ptr(rx51_audio_of_match),
0472     },
0473     .probe = rx51_soc_probe,
0474 };
0475 
0476 module_platform_driver(rx51_soc_driver);
0477 
0478 MODULE_AUTHOR("Nokia Corporation");
0479 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
0480 MODULE_LICENSE("GPL");
0481 MODULE_ALIAS("platform:rx51-audio");