Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // uda1334.c  --  UDA1334 ALSA SoC Audio driver
0004 //
0005 // Based on WM8523 ALSA SoC Audio driver written by Mark Brown
0006 
0007 #include <linux/module.h>
0008 #include <linux/moduleparam.h>
0009 #include <linux/init.h>
0010 #include <linux/delay.h>
0011 #include <linux/slab.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/of_device.h>
0014 #include <sound/core.h>
0015 #include <sound/pcm.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/soc.h>
0018 #include <sound/initval.h>
0019 
0020 #define UDA1334_NUM_RATES 6
0021 
0022 /* codec private data */
0023 struct uda1334_priv {
0024     struct gpio_desc *mute;
0025     struct gpio_desc *deemph;
0026     unsigned int sysclk;
0027     unsigned int rate_constraint_list[UDA1334_NUM_RATES];
0028     struct snd_pcm_hw_constraint_list rate_constraint;
0029 };
0030 
0031 static const struct snd_soc_dapm_widget uda1334_dapm_widgets[] = {
0032 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
0033 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
0034 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
0035 };
0036 
0037 static const struct snd_soc_dapm_route uda1334_dapm_routes[] = {
0038     { "LINEVOUTL", NULL, "DAC" },
0039     { "LINEVOUTR", NULL, "DAC" },
0040 };
0041 
0042 static int uda1334_put_deemph(struct snd_kcontrol *kcontrol,
0043                   struct snd_ctl_elem_value *ucontrol)
0044 {
0045     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0046     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0047     int deemph = ucontrol->value.integer.value[0];
0048 
0049     if (deemph > 1)
0050         return -EINVAL;
0051 
0052     gpiod_set_value_cansleep(uda1334->deemph, deemph);
0053 
0054     return 0;
0055 };
0056 
0057 static int uda1334_get_deemph(struct snd_kcontrol *kcontrol,
0058                   struct snd_ctl_elem_value *ucontrol)
0059 {
0060     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0061     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0062     int ret;
0063 
0064     ret = gpiod_get_value_cansleep(uda1334->deemph);
0065     if (ret < 0)
0066         return -EINVAL;
0067 
0068     ucontrol->value.integer.value[0] = ret;
0069 
0070     return 0;
0071 };
0072 
0073 static const struct snd_kcontrol_new uda1334_snd_controls[] = {
0074     SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
0075                 uda1334_get_deemph, uda1334_put_deemph),
0076 };
0077 
0078 static const struct {
0079     int value;
0080     int ratio;
0081 } lrclk_ratios[UDA1334_NUM_RATES] = {
0082     { 1, 128 },
0083     { 2, 192 },
0084     { 3, 256 },
0085     { 4, 384 },
0086     { 5, 512 },
0087     { 6, 768 },
0088 };
0089 
0090 static int uda1334_startup(struct snd_pcm_substream *substream,
0091                struct snd_soc_dai *dai)
0092 {
0093     struct snd_soc_component *component = dai->component;
0094     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0095 
0096     /*
0097      * The set of sample rates that can be supported depends on the
0098      * MCLK supplied to the CODEC - enforce this.
0099      */
0100     if (!uda1334->sysclk) {
0101         dev_err(component->dev,
0102             "No MCLK configured, call set_sysclk() on init\n");
0103         return -EINVAL;
0104     }
0105 
0106     snd_pcm_hw_constraint_list(substream->runtime, 0,
0107                    SNDRV_PCM_HW_PARAM_RATE,
0108                    &uda1334->rate_constraint);
0109 
0110     gpiod_set_value_cansleep(uda1334->mute, 1);
0111 
0112     return 0;
0113 }
0114 
0115 static void uda1334_shutdown(struct snd_pcm_substream *substream,
0116                  struct snd_soc_dai *dai)
0117 {
0118     struct snd_soc_component *component = dai->component;
0119     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0120 
0121     gpiod_set_value_cansleep(uda1334->mute, 0);
0122 }
0123 
0124 static int uda1334_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0125                   int clk_id, unsigned int freq, int dir)
0126 {
0127     struct snd_soc_component *component = codec_dai->component;
0128     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0129     unsigned int val;
0130     int i, j = 0;
0131 
0132     uda1334->sysclk = freq;
0133 
0134     uda1334->rate_constraint.count = 0;
0135     for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
0136         val = freq / lrclk_ratios[i].ratio;
0137         /*
0138          * Check that it's a standard rate since core can't
0139          * cope with others and having the odd rates confuses
0140          * constraint matching.
0141          */
0142 
0143         switch (val) {
0144         case 8000:
0145         case 32000:
0146         case 44100:
0147         case 48000:
0148         case 64000:
0149         case 88200:
0150         case 96000:
0151             dev_dbg(component->dev, "Supported sample rate: %dHz\n",
0152                 val);
0153             uda1334->rate_constraint_list[j++] = val;
0154             uda1334->rate_constraint.count++;
0155             break;
0156         default:
0157             dev_dbg(component->dev, "Skipping sample rate: %dHz\n",
0158                 val);
0159         }
0160     }
0161 
0162     /* Need at least one supported rate... */
0163     if (uda1334->rate_constraint.count == 0)
0164         return -EINVAL;
0165 
0166     return 0;
0167 }
0168 
0169 static int uda1334_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
0170 {
0171     fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
0172         SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK);
0173 
0174     if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0175             SND_SOC_DAIFMT_CBC_CFC)) {
0176         dev_err(codec_dai->dev, "Invalid DAI format\n");
0177         return -EINVAL;
0178     }
0179 
0180     return 0;
0181 }
0182 
0183 static int uda1334_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
0184 {
0185     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(dai->component);
0186 
0187     if (uda1334->mute)
0188         gpiod_set_value_cansleep(uda1334->mute, mute);
0189 
0190     return 0;
0191 }
0192 
0193 #define UDA1334_RATES SNDRV_PCM_RATE_8000_96000
0194 
0195 #define UDA1334_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
0196 
0197 static const struct snd_soc_dai_ops uda1334_dai_ops = {
0198     .startup    = uda1334_startup,
0199     .shutdown   = uda1334_shutdown,
0200     .set_sysclk = uda1334_set_dai_sysclk,
0201     .set_fmt    = uda1334_set_fmt,
0202     .mute_stream    = uda1334_mute_stream,
0203 };
0204 
0205 static struct snd_soc_dai_driver uda1334_dai = {
0206     .name = "uda1334-hifi",
0207     .playback = {
0208         .stream_name = "Playback",
0209         .channels_min = 2,
0210         .channels_max = 2,
0211         .rates = UDA1334_RATES,
0212         .formats = UDA1334_FORMATS,
0213     },
0214     .ops = &uda1334_dai_ops,
0215 };
0216 
0217 static int uda1334_probe(struct snd_soc_component *component)
0218 {
0219     struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component);
0220 
0221     uda1334->rate_constraint.list = &uda1334->rate_constraint_list[0];
0222     uda1334->rate_constraint.count =
0223         ARRAY_SIZE(uda1334->rate_constraint_list);
0224 
0225     return 0;
0226 }
0227 
0228 static const struct snd_soc_component_driver soc_component_dev_uda1334 = {
0229     .probe          = uda1334_probe,
0230     .controls       = uda1334_snd_controls,
0231     .num_controls       = ARRAY_SIZE(uda1334_snd_controls),
0232     .dapm_widgets       = uda1334_dapm_widgets,
0233     .num_dapm_widgets   = ARRAY_SIZE(uda1334_dapm_widgets),
0234     .dapm_routes        = uda1334_dapm_routes,
0235     .num_dapm_routes    = ARRAY_SIZE(uda1334_dapm_routes),
0236     .idle_bias_on       = 1,
0237     .use_pmdown_time    = 1,
0238     .endianness     = 1,
0239 };
0240 
0241 static const struct of_device_id uda1334_of_match[] = {
0242     { .compatible = "nxp,uda1334" },
0243     { /* sentinel*/ }
0244 };
0245 MODULE_DEVICE_TABLE(of, uda1334_of_match);
0246 
0247 static int uda1334_codec_probe(struct platform_device *pdev)
0248 {
0249     struct uda1334_priv *uda1334;
0250     int ret;
0251 
0252     uda1334 = devm_kzalloc(&pdev->dev, sizeof(struct uda1334_priv),
0253                    GFP_KERNEL);
0254     if (!uda1334)
0255         return -ENOMEM;
0256 
0257     platform_set_drvdata(pdev, uda1334);
0258 
0259     uda1334->mute = devm_gpiod_get(&pdev->dev, "nxp,mute", GPIOD_OUT_LOW);
0260     if (IS_ERR(uda1334->mute)) {
0261         ret = PTR_ERR(uda1334->mute);
0262         dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret);
0263         return ret;
0264     }
0265 
0266     uda1334->deemph = devm_gpiod_get(&pdev->dev, "nxp,deemph", GPIOD_OUT_LOW);
0267     if (IS_ERR(uda1334->deemph)) {
0268         ret = PTR_ERR(uda1334->deemph);
0269         dev_err(&pdev->dev, "Failed to get deemph line: %d\n", ret);
0270         return ret;
0271     }
0272 
0273     ret = devm_snd_soc_register_component(&pdev->dev,
0274                           &soc_component_dev_uda1334,
0275                           &uda1334_dai, 1);
0276     if (ret < 0)
0277         dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
0278 
0279     return ret;
0280 }
0281 
0282 static struct platform_driver uda1334_codec_driver = {
0283     .probe      = uda1334_codec_probe,
0284     .driver     = {
0285         .name   = "uda1334-codec",
0286         .of_match_table = uda1334_of_match,
0287     },
0288 };
0289 module_platform_driver(uda1334_codec_driver);
0290 
0291 MODULE_DESCRIPTION("ASoC UDA1334 driver");
0292 MODULE_AUTHOR("Andra Danciu <andradanciu1997@gmail.com>");
0293 MODULE_ALIAS("platform:uda1334-codec");
0294 MODULE_LICENSE("GPL v2");