Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) STMicroelectronics SA 2015
0004  * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
0005  *          for STMicroelectronics.
0006  */
0007 
0008 #include <linux/io.h>
0009 #include <linux/module.h>
0010 #include <linux/regmap.h>
0011 #include <linux/reset.h>
0012 #include <linux/mfd/syscon.h>
0013 
0014 #include <sound/soc.h>
0015 #include <sound/soc-dapm.h>
0016 
0017 /* DAC definitions */
0018 
0019 /* stih407 DAC registers */
0020 /* sysconf 5041: Audio-Gue-Control */
0021 #define STIH407_AUDIO_GLUE_CTRL 0x000000A4
0022 /* sysconf 5042: Audio-DAC-Control */
0023 #define STIH407_AUDIO_DAC_CTRL 0x000000A8
0024 
0025 /* DAC definitions */
0026 #define STIH407_DAC_SOFTMUTE        0x0
0027 #define STIH407_DAC_STANDBY_ANA     0x1
0028 #define STIH407_DAC_STANDBY     0x2
0029 
0030 #define STIH407_DAC_SOFTMUTE_MASK   BIT(STIH407_DAC_SOFTMUTE)
0031 #define STIH407_DAC_STANDBY_ANA_MASK    BIT(STIH407_DAC_STANDBY_ANA)
0032 #define STIH407_DAC_STANDBY_MASK        BIT(STIH407_DAC_STANDBY)
0033 
0034 /* SPDIF definitions */
0035 #define SPDIF_BIPHASE_ENABLE        0x6
0036 #define SPDIF_BIPHASE_IDLE      0x7
0037 
0038 #define SPDIF_BIPHASE_ENABLE_MASK   BIT(SPDIF_BIPHASE_ENABLE)
0039 #define SPDIF_BIPHASE_IDLE_MASK     BIT(SPDIF_BIPHASE_IDLE)
0040 
0041 enum {
0042     STI_SAS_DAI_SPDIF_OUT,
0043     STI_SAS_DAI_ANALOG_OUT,
0044 };
0045 
0046 static const struct reg_default stih407_sas_reg_defaults[] = {
0047     { STIH407_AUDIO_DAC_CTRL, 0x000000000 },
0048     { STIH407_AUDIO_GLUE_CTRL, 0x00000040 },
0049 };
0050 
0051 struct sti_dac_audio {
0052     struct regmap *regmap;
0053     struct regmap *virt_regmap;
0054     int mclk;
0055 };
0056 
0057 struct sti_spdif_audio {
0058     struct regmap *regmap;
0059     int mclk;
0060 };
0061 
0062 /* device data structure */
0063 struct sti_sas_dev_data {
0064     const struct regmap_config *regmap;
0065     const struct snd_soc_dai_ops *dac_ops;  /* DAC function callbacks */
0066     const struct snd_soc_dapm_widget *dapm_widgets; /* dapms declaration */
0067     const int num_dapm_widgets; /* dapms declaration */
0068     const struct snd_soc_dapm_route *dapm_routes; /* route declaration */
0069     const int num_dapm_routes; /* route declaration */
0070 };
0071 
0072 /* driver data structure */
0073 struct sti_sas_data {
0074     struct device *dev;
0075     const struct sti_sas_dev_data *dev_data;
0076     struct sti_dac_audio dac;
0077     struct sti_spdif_audio spdif;
0078 };
0079 
0080 /* Read a register from the sysconf reg bank */
0081 static int sti_sas_read_reg(void *context, unsigned int reg,
0082                 unsigned int *value)
0083 {
0084     struct sti_sas_data *drvdata = context;
0085     int status;
0086     u32 val;
0087 
0088     status = regmap_read(drvdata->dac.regmap, reg, &val);
0089     *value = (unsigned int)val;
0090 
0091     return status;
0092 }
0093 
0094 /* Read a register from the sysconf reg bank */
0095 static int sti_sas_write_reg(void *context, unsigned int reg,
0096                  unsigned int value)
0097 {
0098     struct sti_sas_data *drvdata = context;
0099     int status;
0100 
0101     status = regmap_write(drvdata->dac.regmap, reg, value);
0102 
0103     return status;
0104 }
0105 
0106 static int  sti_sas_init_sas_registers(struct snd_soc_component *component,
0107                        struct sti_sas_data *data)
0108 {
0109     int ret;
0110     /*
0111      * DAC and SPDIF are activated by default
0112      * put them in IDLE to save power
0113      */
0114 
0115     /* Initialise bi-phase formatter to disabled */
0116     ret = snd_soc_component_update_bits(component, STIH407_AUDIO_GLUE_CTRL,
0117                   SPDIF_BIPHASE_ENABLE_MASK, 0);
0118 
0119     if (!ret)
0120         /* Initialise bi-phase formatter idle value to 0 */
0121         ret = snd_soc_component_update_bits(component, STIH407_AUDIO_GLUE_CTRL,
0122                       SPDIF_BIPHASE_IDLE_MASK, 0);
0123     if (ret < 0) {
0124         dev_err(component->dev, "Failed to update SPDIF registers\n");
0125         return ret;
0126     }
0127 
0128     /* Init DAC configuration */
0129     /* init configuration */
0130     ret =  snd_soc_component_update_bits(component, STIH407_AUDIO_DAC_CTRL,
0131                    STIH407_DAC_STANDBY_MASK,
0132                    STIH407_DAC_STANDBY_MASK);
0133 
0134     if (!ret)
0135         ret = snd_soc_component_update_bits(component, STIH407_AUDIO_DAC_CTRL,
0136                       STIH407_DAC_STANDBY_ANA_MASK,
0137                       STIH407_DAC_STANDBY_ANA_MASK);
0138     if (!ret)
0139         ret = snd_soc_component_update_bits(component, STIH407_AUDIO_DAC_CTRL,
0140                       STIH407_DAC_SOFTMUTE_MASK,
0141                       STIH407_DAC_SOFTMUTE_MASK);
0142 
0143     if (ret < 0) {
0144         dev_err(component->dev, "Failed to update DAC registers\n");
0145         return ret;
0146     }
0147 
0148     return ret;
0149 }
0150 
0151 /*
0152  * DAC
0153  */
0154 static int sti_sas_dac_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0155 {
0156     /* Sanity check only */
0157     if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
0158         dev_err(dai->component->dev,
0159             "%s: ERROR: Unsupported clocking 0x%x\n",
0160             __func__, fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK);
0161         return -EINVAL;
0162     }
0163 
0164     return 0;
0165 }
0166 
0167 static const struct snd_soc_dapm_widget stih407_sas_dapm_widgets[] = {
0168     SND_SOC_DAPM_OUT_DRV("DAC standby ana", STIH407_AUDIO_DAC_CTRL,
0169                  STIH407_DAC_STANDBY_ANA, 1, NULL, 0),
0170     SND_SOC_DAPM_DAC("DAC standby",  "dac_p", STIH407_AUDIO_DAC_CTRL,
0171              STIH407_DAC_STANDBY, 1),
0172     SND_SOC_DAPM_OUTPUT("DAC Output"),
0173 };
0174 
0175 static const struct snd_soc_dapm_route stih407_sas_route[] = {
0176     {"DAC Output", NULL, "DAC standby ana"},
0177     {"DAC standby ana", NULL, "DAC standby"},
0178 };
0179 
0180 
0181 static int stih407_sas_dac_mute(struct snd_soc_dai *dai, int mute, int stream)
0182 {
0183     struct snd_soc_component *component = dai->component;
0184 
0185     if (mute) {
0186         return snd_soc_component_update_bits(component, STIH407_AUDIO_DAC_CTRL,
0187                         STIH407_DAC_SOFTMUTE_MASK,
0188                         STIH407_DAC_SOFTMUTE_MASK);
0189     } else {
0190         return snd_soc_component_update_bits(component, STIH407_AUDIO_DAC_CTRL,
0191                         STIH407_DAC_SOFTMUTE_MASK,
0192                         0);
0193     }
0194 }
0195 
0196 /*
0197  * SPDIF
0198  */
0199 static int sti_sas_spdif_set_fmt(struct snd_soc_dai *dai,
0200                  unsigned int fmt)
0201 {
0202     if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
0203         dev_err(dai->component->dev,
0204             "%s: ERROR: Unsupported clocking mask 0x%x\n",
0205             __func__, fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK);
0206         return -EINVAL;
0207     }
0208 
0209     return 0;
0210 }
0211 
0212 /*
0213  * sti_sas_spdif_trigger:
0214  * Trigger function is used to ensure that BiPhase Formater is disabled
0215  * before CPU dai is stopped.
0216  * This is mandatory to avoid that BPF is stalled
0217  */
0218 static int sti_sas_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
0219                  struct snd_soc_dai *dai)
0220 {
0221     struct snd_soc_component *component = dai->component;
0222 
0223     switch (cmd) {
0224     case SNDRV_PCM_TRIGGER_START:
0225     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0226         return snd_soc_component_update_bits(component, STIH407_AUDIO_GLUE_CTRL,
0227                         SPDIF_BIPHASE_ENABLE_MASK,
0228                         SPDIF_BIPHASE_ENABLE_MASK);
0229     case SNDRV_PCM_TRIGGER_RESUME:
0230     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0231     case SNDRV_PCM_TRIGGER_STOP:
0232     case SNDRV_PCM_TRIGGER_SUSPEND:
0233         return snd_soc_component_update_bits(component, STIH407_AUDIO_GLUE_CTRL,
0234                         SPDIF_BIPHASE_ENABLE_MASK,
0235                         0);
0236     default:
0237         return -EINVAL;
0238     }
0239 }
0240 
0241 static bool sti_sas_volatile_register(struct device *dev, unsigned int reg)
0242 {
0243     if (reg == STIH407_AUDIO_GLUE_CTRL)
0244         return true;
0245 
0246     return false;
0247 }
0248 
0249 /*
0250  * CODEC DAIS
0251  */
0252 
0253 /*
0254  * sti_sas_set_sysclk:
0255  * get MCLK input frequency to check that MCLK-FS ratio is coherent
0256  */
0257 static int sti_sas_set_sysclk(struct snd_soc_dai *dai, int clk_id,
0258                   unsigned int freq, int dir)
0259 {
0260     struct snd_soc_component *component = dai->component;
0261     struct sti_sas_data *drvdata = dev_get_drvdata(component->dev);
0262 
0263     if (dir == SND_SOC_CLOCK_OUT)
0264         return 0;
0265 
0266     if (clk_id != 0)
0267         return -EINVAL;
0268 
0269     switch (dai->id) {
0270     case STI_SAS_DAI_SPDIF_OUT:
0271         drvdata->spdif.mclk = freq;
0272         break;
0273 
0274     case STI_SAS_DAI_ANALOG_OUT:
0275         drvdata->dac.mclk = freq;
0276         break;
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 static int sti_sas_prepare(struct snd_pcm_substream *substream,
0283                struct snd_soc_dai *dai)
0284 {
0285     struct snd_soc_component *component = dai->component;
0286     struct sti_sas_data *drvdata = dev_get_drvdata(component->dev);
0287     struct snd_pcm_runtime *runtime = substream->runtime;
0288 
0289     switch (dai->id) {
0290     case STI_SAS_DAI_SPDIF_OUT:
0291         if ((drvdata->spdif.mclk / runtime->rate) != 128) {
0292             dev_err(component->dev, "unexpected mclk-fs ratio\n");
0293             return -EINVAL;
0294         }
0295         break;
0296     case STI_SAS_DAI_ANALOG_OUT:
0297         if ((drvdata->dac.mclk / runtime->rate) != 256) {
0298             dev_err(component->dev, "unexpected mclk-fs ratio\n");
0299             return -EINVAL;
0300         }
0301         break;
0302     }
0303 
0304     return 0;
0305 }
0306 
0307 static const struct snd_soc_dai_ops stih407_dac_ops = {
0308     .set_fmt = sti_sas_dac_set_fmt,
0309     .mute_stream = stih407_sas_dac_mute,
0310     .prepare = sti_sas_prepare,
0311     .set_sysclk = sti_sas_set_sysclk,
0312 };
0313 
0314 static const struct regmap_config stih407_sas_regmap = {
0315     .reg_bits = 32,
0316     .val_bits = 32,
0317     .fast_io = true,
0318     .max_register = STIH407_AUDIO_DAC_CTRL,
0319     .reg_defaults = stih407_sas_reg_defaults,
0320     .num_reg_defaults = ARRAY_SIZE(stih407_sas_reg_defaults),
0321     .volatile_reg = sti_sas_volatile_register,
0322     .cache_type = REGCACHE_RBTREE,
0323     .reg_read = sti_sas_read_reg,
0324     .reg_write = sti_sas_write_reg,
0325 };
0326 
0327 static const struct sti_sas_dev_data stih407_data = {
0328     .regmap = &stih407_sas_regmap,
0329     .dac_ops = &stih407_dac_ops,
0330     .dapm_widgets = stih407_sas_dapm_widgets,
0331     .num_dapm_widgets = ARRAY_SIZE(stih407_sas_dapm_widgets),
0332     .dapm_routes =  stih407_sas_route,
0333     .num_dapm_routes = ARRAY_SIZE(stih407_sas_route),
0334 };
0335 
0336 static struct snd_soc_dai_driver sti_sas_dai[] = {
0337     {
0338         .name = "sas-dai-spdif-out",
0339         .id = STI_SAS_DAI_SPDIF_OUT,
0340         .playback = {
0341             .stream_name = "spdif_p",
0342             .channels_min = 2,
0343             .channels_max = 2,
0344             .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
0345                  SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 |
0346                  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
0347                  SNDRV_PCM_RATE_192000,
0348             .formats = SNDRV_PCM_FMTBIT_S16_LE |
0349                    SNDRV_PCM_FMTBIT_S32_LE,
0350         },
0351         .ops = (struct snd_soc_dai_ops[]) {
0352             {
0353                 .set_fmt = sti_sas_spdif_set_fmt,
0354                 .trigger = sti_sas_spdif_trigger,
0355                 .set_sysclk = sti_sas_set_sysclk,
0356                 .prepare = sti_sas_prepare,
0357             }
0358         },
0359     },
0360     {
0361         .name = "sas-dai-dac",
0362         .id = STI_SAS_DAI_ANALOG_OUT,
0363         .playback = {
0364             .stream_name = "dac_p",
0365             .channels_min = 2,
0366             .channels_max = 2,
0367             .rates = SNDRV_PCM_RATE_8000_48000,
0368             .formats = SNDRV_PCM_FMTBIT_S16_LE |
0369                    SNDRV_PCM_FMTBIT_S32_LE,
0370         },
0371     },
0372 };
0373 
0374 #ifdef CONFIG_PM_SLEEP
0375 static int sti_sas_resume(struct snd_soc_component *component)
0376 {
0377     struct sti_sas_data *drvdata = dev_get_drvdata(component->dev);
0378 
0379     return sti_sas_init_sas_registers(component, drvdata);
0380 }
0381 #else
0382 #define sti_sas_resume NULL
0383 #endif
0384 
0385 static int sti_sas_component_probe(struct snd_soc_component *component)
0386 {
0387     struct sti_sas_data *drvdata = dev_get_drvdata(component->dev);
0388     int ret;
0389 
0390     ret = sti_sas_init_sas_registers(component, drvdata);
0391 
0392     return ret;
0393 }
0394 
0395 static struct snd_soc_component_driver sti_sas_driver = {
0396     .probe          = sti_sas_component_probe,
0397     .resume         = sti_sas_resume,
0398     .idle_bias_on       = 1,
0399     .use_pmdown_time    = 1,
0400     .endianness     = 1,
0401 };
0402 
0403 static const struct of_device_id sti_sas_dev_match[] = {
0404     {
0405         .compatible = "st,stih407-sas-codec",
0406         .data = &stih407_data,
0407     },
0408     {},
0409 };
0410 MODULE_DEVICE_TABLE(of, sti_sas_dev_match);
0411 
0412 static int sti_sas_driver_probe(struct platform_device *pdev)
0413 {
0414     struct device_node *pnode = pdev->dev.of_node;
0415     struct sti_sas_data *drvdata;
0416     const struct of_device_id *of_id;
0417 
0418     /* Allocate device structure */
0419     drvdata = devm_kzalloc(&pdev->dev, sizeof(struct sti_sas_data),
0420                    GFP_KERNEL);
0421     if (!drvdata)
0422         return -ENOMEM;
0423 
0424     /* Populate data structure depending on compatibility */
0425     of_id = of_match_node(sti_sas_dev_match, pnode);
0426     if (!of_id->data) {
0427         dev_err(&pdev->dev, "data associated to device is missing\n");
0428         return -EINVAL;
0429     }
0430 
0431     drvdata->dev_data = (struct sti_sas_dev_data *)of_id->data;
0432 
0433     /* Initialise device structure */
0434     drvdata->dev = &pdev->dev;
0435 
0436     /* Request the DAC & SPDIF registers memory region */
0437     drvdata->dac.virt_regmap = devm_regmap_init(&pdev->dev, NULL, drvdata,
0438                             drvdata->dev_data->regmap);
0439     if (IS_ERR(drvdata->dac.virt_regmap)) {
0440         dev_err(&pdev->dev, "audio registers not enabled\n");
0441         return PTR_ERR(drvdata->dac.virt_regmap);
0442     }
0443 
0444     /* Request the syscon region */
0445     drvdata->dac.regmap =
0446         syscon_regmap_lookup_by_phandle(pnode, "st,syscfg");
0447     if (IS_ERR(drvdata->dac.regmap)) {
0448         dev_err(&pdev->dev, "syscon registers not available\n");
0449         return PTR_ERR(drvdata->dac.regmap);
0450     }
0451     drvdata->spdif.regmap = drvdata->dac.regmap;
0452 
0453     sti_sas_dai[STI_SAS_DAI_ANALOG_OUT].ops = drvdata->dev_data->dac_ops;
0454 
0455     /* Set dapms*/
0456     sti_sas_driver.dapm_widgets = drvdata->dev_data->dapm_widgets;
0457     sti_sas_driver.num_dapm_widgets = drvdata->dev_data->num_dapm_widgets;
0458 
0459     sti_sas_driver.dapm_routes = drvdata->dev_data->dapm_routes;
0460     sti_sas_driver.num_dapm_routes = drvdata->dev_data->num_dapm_routes;
0461 
0462     /* Store context */
0463     dev_set_drvdata(&pdev->dev, drvdata);
0464 
0465     return devm_snd_soc_register_component(&pdev->dev, &sti_sas_driver,
0466                     sti_sas_dai,
0467                     ARRAY_SIZE(sti_sas_dai));
0468 }
0469 
0470 static struct platform_driver sti_sas_platform_driver = {
0471     .driver = {
0472         .name = "sti-sas-codec",
0473         .of_match_table = sti_sas_dev_match,
0474     },
0475     .probe = sti_sas_driver_probe,
0476 };
0477 
0478 module_platform_driver(sti_sas_platform_driver);
0479 
0480 MODULE_DESCRIPTION("audio codec for STMicroelectronics sti platforms");
0481 MODULE_AUTHOR("Arnaud.pouliquen@st.com");
0482 MODULE_LICENSE("GPL v2");