Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
0004  *
0005  * Copyright (c) 2014, ROCKCHIP CORPORATION.  All rights reserved.
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/of_device.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/slab.h>
0012 #include <linux/gpio.h>
0013 #include <linux/of_gpio.h>
0014 #include <sound/core.h>
0015 #include <sound/jack.h>
0016 #include <sound/pcm.h>
0017 #include <sound/pcm_params.h>
0018 #include <sound/soc.h>
0019 
0020 #include "rockchip_i2s.h"
0021 #include "../codecs/ts3a227e.h"
0022 
0023 #define DRV_NAME "rockchip-snd-max98090"
0024 
0025 static struct snd_soc_jack headset_jack;
0026 
0027 /* Headset jack detection DAPM pins */
0028 static struct snd_soc_jack_pin headset_jack_pins[] = {
0029     {
0030         .pin = "Headphone",
0031         .mask = SND_JACK_HEADPHONE,
0032     },
0033     {
0034         .pin = "Headset Mic",
0035         .mask = SND_JACK_MICROPHONE,
0036     },
0037 
0038 };
0039 
0040 #define RK_MAX98090_WIDGETS \
0041     SND_SOC_DAPM_HP("Headphone", NULL), \
0042     SND_SOC_DAPM_MIC("Headset Mic", NULL), \
0043     SND_SOC_DAPM_MIC("Int Mic", NULL), \
0044     SND_SOC_DAPM_SPK("Speaker", NULL)
0045 
0046 #define RK_HDMI_WIDGETS \
0047     SND_SOC_DAPM_LINE("HDMI", NULL)
0048 
0049 static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets[] = {
0050     RK_MAX98090_WIDGETS,
0051 };
0052 
0053 static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets[] = {
0054     RK_HDMI_WIDGETS,
0055 };
0056 
0057 static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets[] = {
0058     RK_MAX98090_WIDGETS,
0059     RK_HDMI_WIDGETS,
0060 };
0061 
0062 #define RK_MAX98090_AUDIO_MAP \
0063     {"IN34", NULL, "Headset Mic"}, \
0064     {"Headset Mic", NULL, "MICBIAS"}, \
0065     {"DMICL", NULL, "Int Mic"}, \
0066     {"Headphone", NULL, "HPL"}, \
0067     {"Headphone", NULL, "HPR"}, \
0068     {"Speaker", NULL, "SPKL"}, \
0069     {"Speaker", NULL, "SPKR"}
0070 
0071 #define RK_HDMI_AUDIO_MAP \
0072     {"HDMI", NULL, "TX"}
0073 
0074 static const struct snd_soc_dapm_route rk_max98090_audio_map[] = {
0075     RK_MAX98090_AUDIO_MAP,
0076 };
0077 
0078 static const struct snd_soc_dapm_route rk_hdmi_audio_map[] = {
0079     RK_HDMI_AUDIO_MAP,
0080 };
0081 
0082 static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map[] = {
0083     RK_MAX98090_AUDIO_MAP,
0084     RK_HDMI_AUDIO_MAP,
0085 };
0086 
0087 #define RK_MAX98090_CONTROLS \
0088     SOC_DAPM_PIN_SWITCH("Headphone"), \
0089     SOC_DAPM_PIN_SWITCH("Headset Mic"), \
0090     SOC_DAPM_PIN_SWITCH("Int Mic"), \
0091     SOC_DAPM_PIN_SWITCH("Speaker")
0092 
0093 #define RK_HDMI_CONTROLS \
0094     SOC_DAPM_PIN_SWITCH("HDMI")
0095 
0096 static const struct snd_kcontrol_new rk_max98090_controls[] = {
0097     RK_MAX98090_CONTROLS,
0098 };
0099 
0100 static const struct snd_kcontrol_new rk_hdmi_controls[] = {
0101     RK_HDMI_CONTROLS,
0102 };
0103 
0104 static const struct snd_kcontrol_new rk_max98090_hdmi_controls[] = {
0105     RK_MAX98090_CONTROLS,
0106     RK_HDMI_CONTROLS,
0107 };
0108 
0109 static int rk_jack_event(struct notifier_block *nb, unsigned long event,
0110              void *data)
0111 {
0112     struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
0113     struct snd_soc_dapm_context *dapm = &jack->card->dapm;
0114 
0115     if (event & SND_JACK_MICROPHONE) {
0116         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
0117         snd_soc_dapm_force_enable_pin(dapm, "SHDN");
0118     } else {
0119         snd_soc_dapm_disable_pin(dapm, "MICBIAS");
0120         snd_soc_dapm_disable_pin(dapm, "SHDN");
0121     }
0122 
0123     snd_soc_dapm_sync(dapm);
0124 
0125     return 0;
0126 }
0127 
0128 static struct notifier_block rk_jack_nb = {
0129     .notifier_call = rk_jack_event,
0130 };
0131 
0132 static int rk_init(struct snd_soc_pcm_runtime *runtime)
0133 {
0134     /*
0135      * The jack has already been created in the rk_98090_headset_init()
0136      * function.
0137      */
0138     snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb);
0139 
0140     return 0;
0141 }
0142 
0143 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
0144                  struct snd_pcm_hw_params *params)
0145 {
0146     int ret = 0;
0147     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0148     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0149     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0150     int mclk;
0151 
0152     switch (params_rate(params)) {
0153     case 8000:
0154     case 16000:
0155     case 24000:
0156     case 32000:
0157     case 48000:
0158     case 64000:
0159     case 96000:
0160         mclk = 12288000;
0161         break;
0162     case 11025:
0163     case 22050:
0164     case 44100:
0165     case 88200:
0166         mclk = 11289600;
0167         break;
0168     default:
0169         return -EINVAL;
0170     }
0171 
0172     ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
0173                      SND_SOC_CLOCK_OUT);
0174     if (ret) {
0175         dev_err(cpu_dai->dev, "Can't set cpu dai clock %d\n", ret);
0176         return ret;
0177     }
0178 
0179     ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
0180                      SND_SOC_CLOCK_IN);
0181 
0182     /* HDMI codec dai does not need to set sysclk. */
0183     if (!strcmp(rtd->dai_link->name, "HDMI"))
0184         return 0;
0185 
0186     if (ret) {
0187         dev_err(codec_dai->dev, "Can't set codec dai clock %d\n", ret);
0188         return ret;
0189     }
0190 
0191     return ret;
0192 }
0193 
0194 static int rk_aif1_startup(struct snd_pcm_substream *substream)
0195 {
0196     /*
0197      * Set period size to 240 because pl330 has issue
0198      * dealing with larger period in stress testing.
0199      */
0200     return snd_pcm_hw_constraint_minmax(substream->runtime,
0201             SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 240, 240);
0202 }
0203 
0204 static const struct snd_soc_ops rk_aif1_ops = {
0205     .hw_params = rk_aif1_hw_params,
0206     .startup = rk_aif1_startup,
0207 };
0208 
0209 SND_SOC_DAILINK_DEFS(analog,
0210              DAILINK_COMP_ARRAY(COMP_EMPTY()),
0211              DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
0212              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0213 
0214 SND_SOC_DAILINK_DEFS(hdmi,
0215              DAILINK_COMP_ARRAY(COMP_EMPTY()),
0216              DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
0217              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0218 
0219 enum {
0220     DAILINK_MAX98090,
0221     DAILINK_HDMI,
0222 };
0223 
0224 static struct snd_soc_jack rk_hdmi_jack;
0225 
0226 static int rk_hdmi_init(struct snd_soc_pcm_runtime *runtime)
0227 {
0228     struct snd_soc_card *card = runtime->card;
0229     struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
0230     int ret;
0231 
0232     /* enable jack detection */
0233     ret = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
0234                     &rk_hdmi_jack);
0235     if (ret) {
0236         dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
0237         return ret;
0238     }
0239 
0240     return snd_soc_component_set_jack(component, &rk_hdmi_jack, NULL);
0241 }
0242 
0243 /* max98090 dai_link */
0244 static struct snd_soc_dai_link rk_max98090_dailinks[] = {
0245     {
0246         .name = "max98090",
0247         .stream_name = "Analog",
0248         .init = rk_init,
0249         .ops = &rk_aif1_ops,
0250         /* set max98090 as slave */
0251         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0252             SND_SOC_DAIFMT_CBS_CFS,
0253         SND_SOC_DAILINK_REG(analog),
0254     },
0255 };
0256 
0257 /* HDMI codec dai_link */
0258 static struct snd_soc_dai_link rk_hdmi_dailinks[] = {
0259     {
0260         .name = "HDMI",
0261         .stream_name = "HDMI",
0262         .init = rk_hdmi_init,
0263         .ops = &rk_aif1_ops,
0264         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0265             SND_SOC_DAIFMT_CBS_CFS,
0266         SND_SOC_DAILINK_REG(hdmi),
0267     }
0268 };
0269 
0270 /* max98090 and HDMI codec dai_link */
0271 static struct snd_soc_dai_link rk_max98090_hdmi_dailinks[] = {
0272     [DAILINK_MAX98090] = {
0273         .name = "max98090",
0274         .stream_name = "Analog",
0275         .init = rk_init,
0276         .ops = &rk_aif1_ops,
0277         /* set max98090 as slave */
0278         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0279             SND_SOC_DAIFMT_CBS_CFS,
0280         SND_SOC_DAILINK_REG(analog),
0281     },
0282     [DAILINK_HDMI] = {
0283         .name = "HDMI",
0284         .stream_name = "HDMI",
0285         .init = rk_hdmi_init,
0286         .ops = &rk_aif1_ops,
0287         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0288             SND_SOC_DAIFMT_CBS_CFS,
0289         SND_SOC_DAILINK_REG(hdmi),
0290     }
0291 };
0292 
0293 static int rk_98090_headset_init(struct snd_soc_component *component);
0294 
0295 static struct snd_soc_aux_dev rk_98090_headset_dev = {
0296     .dlc = COMP_EMPTY(),
0297     .init = rk_98090_headset_init,
0298 };
0299 
0300 static struct snd_soc_card rockchip_max98090_card = {
0301     .name = "ROCKCHIP-I2S",
0302     .owner = THIS_MODULE,
0303     .dai_link = rk_max98090_dailinks,
0304     .num_links = ARRAY_SIZE(rk_max98090_dailinks),
0305     .aux_dev = &rk_98090_headset_dev,
0306     .num_aux_devs = 1,
0307     .dapm_widgets = rk_max98090_dapm_widgets,
0308     .num_dapm_widgets = ARRAY_SIZE(rk_max98090_dapm_widgets),
0309     .dapm_routes = rk_max98090_audio_map,
0310     .num_dapm_routes = ARRAY_SIZE(rk_max98090_audio_map),
0311     .controls = rk_max98090_controls,
0312     .num_controls = ARRAY_SIZE(rk_max98090_controls),
0313 };
0314 
0315 static struct snd_soc_card rockchip_hdmi_card = {
0316     .name = "ROCKCHIP-HDMI",
0317     .owner = THIS_MODULE,
0318     .dai_link = rk_hdmi_dailinks,
0319     .num_links = ARRAY_SIZE(rk_hdmi_dailinks),
0320     .dapm_widgets = rk_hdmi_dapm_widgets,
0321     .num_dapm_widgets = ARRAY_SIZE(rk_hdmi_dapm_widgets),
0322     .dapm_routes = rk_hdmi_audio_map,
0323     .num_dapm_routes = ARRAY_SIZE(rk_hdmi_audio_map),
0324     .controls = rk_hdmi_controls,
0325     .num_controls = ARRAY_SIZE(rk_hdmi_controls),
0326 };
0327 
0328 static struct snd_soc_card rockchip_max98090_hdmi_card = {
0329     .name = "ROCKCHIP-MAX98090-HDMI",
0330     .owner = THIS_MODULE,
0331     .dai_link = rk_max98090_hdmi_dailinks,
0332     .num_links = ARRAY_SIZE(rk_max98090_hdmi_dailinks),
0333     .aux_dev = &rk_98090_headset_dev,
0334     .num_aux_devs = 1,
0335     .dapm_widgets = rk_max98090_hdmi_dapm_widgets,
0336     .num_dapm_widgets = ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets),
0337     .dapm_routes = rk_max98090_hdmi_audio_map,
0338     .num_dapm_routes = ARRAY_SIZE(rk_max98090_hdmi_audio_map),
0339     .controls = rk_max98090_hdmi_controls,
0340     .num_controls = ARRAY_SIZE(rk_max98090_hdmi_controls),
0341 };
0342 
0343 static int rk_98090_headset_init(struct snd_soc_component *component)
0344 {
0345     int ret;
0346 
0347     /* Enable Headset and 4 Buttons Jack detection */
0348     ret = snd_soc_card_jack_new_pins(component->card, "Headset Jack",
0349                      SND_JACK_HEADSET |
0350                      SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0351                      SND_JACK_BTN_2 | SND_JACK_BTN_3,
0352                      &headset_jack,
0353                      headset_jack_pins,
0354                      ARRAY_SIZE(headset_jack_pins));
0355     if (ret)
0356         return ret;
0357 
0358     ret = ts3a227e_enable_jack_detect(component, &headset_jack);
0359 
0360     return ret;
0361 }
0362 
0363 static int rk_parse_headset_from_of(struct device *dev, struct device_node *np)
0364 {
0365     rk_98090_headset_dev.dlc.of_node = of_parse_phandle(
0366             np, "rockchip,headset-codec", 0);
0367     if (!rk_98090_headset_dev.dlc.of_node) {
0368         dev_err(dev,
0369             "Property 'rockchip,headset-codec' missing/invalid\n");
0370         return -EINVAL;
0371     }
0372     return 0;
0373 }
0374 
0375 static int snd_rk_mc_probe(struct platform_device *pdev)
0376 {
0377     int ret = 0;
0378     struct snd_soc_card *card;
0379     struct device *dev = &pdev->dev;
0380     struct device_node *np = pdev->dev.of_node;
0381     struct device_node *np_cpu;
0382     struct device_node *np_audio, *np_hdmi;
0383 
0384     /* Parse DTS for I2S controller. */
0385     np_cpu = of_parse_phandle(np, "rockchip,i2s-controller", 0);
0386 
0387     if (!np_cpu) {
0388         dev_err(&pdev->dev,
0389             "Property 'rockchip,i2s-controller missing or invalid\n");
0390         return -EINVAL;
0391     }
0392 
0393     /*
0394      * Find the card to use based on the presences of audio codec
0395      * and hdmi codec in device property. Set their of_node accordingly.
0396      */
0397     np_audio = of_parse_phandle(np, "rockchip,audio-codec", 0);
0398     np_hdmi = of_parse_phandle(np, "rockchip,hdmi-codec", 0);
0399     if (np_audio && np_hdmi) {
0400         card = &rockchip_max98090_hdmi_card;
0401         card->dai_link[DAILINK_MAX98090].codecs->of_node = np_audio;
0402         card->dai_link[DAILINK_HDMI].codecs->of_node = np_hdmi;
0403         card->dai_link[DAILINK_MAX98090].cpus->of_node = np_cpu;
0404         card->dai_link[DAILINK_MAX98090].platforms->of_node = np_cpu;
0405         card->dai_link[DAILINK_HDMI].cpus->of_node = np_cpu;
0406         card->dai_link[DAILINK_HDMI].platforms->of_node = np_cpu;
0407     } else if (np_audio) {
0408         card = &rockchip_max98090_card;
0409         card->dai_link[0].codecs->of_node = np_audio;
0410         card->dai_link[0].cpus->of_node = np_cpu;
0411         card->dai_link[0].platforms->of_node = np_cpu;
0412     } else if (np_hdmi) {
0413         card = &rockchip_hdmi_card;
0414         card->dai_link[0].codecs->of_node = np_hdmi;
0415         card->dai_link[0].cpus->of_node = np_cpu;
0416         card->dai_link[0].platforms->of_node = np_cpu;
0417     } else {
0418         dev_err(dev, "At least one of codecs should be specified\n");
0419         return -EINVAL;
0420     }
0421 
0422     card->dev = dev;
0423 
0424     /* Parse headset detection codec. */
0425     if (np_audio) {
0426         ret = rk_parse_headset_from_of(dev, np);
0427         if (ret)
0428             return ret;
0429     }
0430 
0431     /* Parse card name. */
0432     ret = snd_soc_of_parse_card_name(card, "rockchip,model");
0433     if (ret) {
0434         dev_err(&pdev->dev,
0435             "Soc parse card name failed %d\n", ret);
0436         return ret;
0437     }
0438 
0439     /* register the soc card */
0440     ret = devm_snd_soc_register_card(&pdev->dev, card);
0441     if (ret) {
0442         dev_err(&pdev->dev,
0443             "Soc register card failed %d\n", ret);
0444         return ret;
0445     }
0446 
0447     return ret;
0448 }
0449 
0450 static const struct of_device_id rockchip_max98090_of_match[] = {
0451     { .compatible = "rockchip,rockchip-audio-max98090", },
0452     {},
0453 };
0454 
0455 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match);
0456 
0457 static struct platform_driver snd_rk_mc_driver = {
0458     .probe = snd_rk_mc_probe,
0459     .driver = {
0460         .name = DRV_NAME,
0461         .pm = &snd_soc_pm_ops,
0462         .of_match_table = rockchip_max98090_of_match,
0463     },
0464 };
0465 
0466 module_platform_driver(snd_rk_mc_driver);
0467 
0468 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
0469 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
0470 MODULE_LICENSE("GPL v2");
0471 MODULE_ALIAS("platform:" DRV_NAME);