linux/sound/soc/codecs/es7134.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2017 BayLibre, SAS.
   4 * Author: Jerome Brunet <jbrunet@baylibre.com>
   5 */
   6
   7#include <linux/of_platform.h>
   8#include <linux/module.h>
   9#include <sound/soc.h>
  10
  11/*
  12 * The everest 7134 is a very simple DA converter with no register
  13 */
  14
  15struct es7134_clock_mode {
  16        unsigned int rate_min;
  17        unsigned int rate_max;
  18        unsigned int *mclk_fs;
  19        unsigned int mclk_fs_num;
  20};
  21
  22struct es7134_chip {
  23        struct snd_soc_dai_driver *dai_drv;
  24        const struct es7134_clock_mode *modes;
  25        unsigned int mode_num;
  26        const struct snd_soc_dapm_widget *extra_widgets;
  27        unsigned int extra_widget_num;
  28        const struct snd_soc_dapm_route *extra_routes;
  29        unsigned int extra_route_num;
  30};
  31
  32struct es7134_data {
  33        unsigned int mclk;
  34        const struct es7134_chip *chip;
  35};
  36
  37static int es7134_check_mclk(struct snd_soc_dai *dai,
  38                             struct es7134_data *priv,
  39                             unsigned int rate)
  40{
  41        unsigned int mfs = priv->mclk / rate;
  42        int i, j;
  43
  44        for (i = 0; i < priv->chip->mode_num; i++) {
  45                const struct es7134_clock_mode *mode = &priv->chip->modes[i];
  46
  47                if (rate < mode->rate_min || rate > mode->rate_max)
  48                        continue;
  49
  50                for (j = 0; j < mode->mclk_fs_num; j++) {
  51                        if (mode->mclk_fs[j] == mfs)
  52                                return 0;
  53                }
  54
  55                dev_err(dai->dev, "unsupported mclk_fs %u for rate %u\n",
  56                        mfs, rate);
  57                return -EINVAL;
  58        }
  59
  60        /* should not happen */
  61        dev_err(dai->dev, "unsupported rate: %u\n", rate);
  62        return -EINVAL;
  63}
  64
  65static int es7134_hw_params(struct snd_pcm_substream *substream,
  66                            struct snd_pcm_hw_params *params,
  67                            struct snd_soc_dai *dai)
  68{
  69        struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
  70
  71        /* mclk has not been provided, assume it is OK */
  72        if (!priv->mclk)
  73                return 0;
  74
  75        return es7134_check_mclk(dai, priv, params_rate(params));
  76}
  77
  78static int es7134_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  79                             unsigned int freq, int dir)
  80{
  81        struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
  82
  83        if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
  84                priv->mclk = freq;
  85                return 0;
  86        }
  87
  88        return -ENOTSUPP;
  89}
  90
  91static int es7134_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  92{
  93        fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
  94                SND_SOC_DAIFMT_MASTER_MASK);
  95
  96        if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  97                    SND_SOC_DAIFMT_CBS_CFS)) {
  98                dev_err(codec_dai->dev, "Invalid DAI format\n");
  99                return -EINVAL;
 100        }
 101
 102        return 0;
 103}
 104
 105static int es7134_component_probe(struct snd_soc_component *c)
 106{
 107        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(c);
 108        struct es7134_data *priv = snd_soc_component_get_drvdata(c);
 109        const struct es7134_chip *chip = priv->chip;
 110        int ret;
 111
 112        if (chip->extra_widget_num) {
 113                ret = snd_soc_dapm_new_controls(dapm, chip->extra_widgets,
 114                                                chip->extra_widget_num);
 115                if (ret) {
 116                        dev_err(c->dev, "failed to add extra widgets\n");
 117                        return ret;
 118                }
 119        }
 120
 121        if (chip->extra_route_num) {
 122                ret = snd_soc_dapm_add_routes(dapm, chip->extra_routes,
 123                                              chip->extra_route_num);
 124                if (ret) {
 125                        dev_err(c->dev, "failed to add extra routes\n");
 126                        return ret;
 127                }
 128        }
 129
 130        return 0;
 131}
 132
 133static const struct snd_soc_dai_ops es7134_dai_ops = {
 134        .set_fmt        = es7134_set_fmt,
 135        .hw_params      = es7134_hw_params,
 136        .set_sysclk     = es7134_set_sysclk,
 137};
 138
 139static struct snd_soc_dai_driver es7134_dai = {
 140        .name = "es7134-hifi",
 141        .playback = {
 142                .stream_name = "Playback",
 143                .channels_min = 2,
 144                .channels_max = 2,
 145                .rates = (SNDRV_PCM_RATE_8000_48000 |
 146                          SNDRV_PCM_RATE_88200      |
 147                          SNDRV_PCM_RATE_96000      |
 148                          SNDRV_PCM_RATE_176400     |
 149                          SNDRV_PCM_RATE_192000),
 150                .formats = (SNDRV_PCM_FMTBIT_S16_LE  |
 151                            SNDRV_PCM_FMTBIT_S18_3LE |
 152                            SNDRV_PCM_FMTBIT_S20_3LE |
 153                            SNDRV_PCM_FMTBIT_S24_3LE |
 154                            SNDRV_PCM_FMTBIT_S24_LE),
 155        },
 156        .ops = &es7134_dai_ops,
 157};
 158
 159static const struct es7134_clock_mode es7134_modes[] = {
 160        {
 161                /* Single speed mode */
 162                .rate_min = 8000,
 163                .rate_max = 50000,
 164                .mclk_fs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
 165                .mclk_fs_num = 5,
 166        }, {
 167                /* Double speed mode */
 168                .rate_min = 84000,
 169                .rate_max = 100000,
 170                .mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512 },
 171                .mclk_fs_num = 5,
 172        }, {
 173                /* Quad speed mode */
 174                .rate_min = 167000,
 175                .rate_max = 192000,
 176                .mclk_fs = (unsigned int[]) { 128, 192, 256 },
 177                .mclk_fs_num = 3,
 178        },
 179};
 180
 181/* Digital I/O are also supplied by VDD on the es7134 */
 182static const struct snd_soc_dapm_route es7134_extra_routes[] = {
 183        { "Playback", NULL, "VDD", }
 184};
 185
 186static const struct es7134_chip es7134_chip = {
 187        .dai_drv = &es7134_dai,
 188        .modes = es7134_modes,
 189        .mode_num = ARRAY_SIZE(es7134_modes),
 190        .extra_routes = es7134_extra_routes,
 191        .extra_route_num = ARRAY_SIZE(es7134_extra_routes),
 192};
 193
 194static const struct snd_soc_dapm_widget es7134_dapm_widgets[] = {
 195        SND_SOC_DAPM_OUTPUT("AOUTL"),
 196        SND_SOC_DAPM_OUTPUT("AOUTR"),
 197        SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
 198        SND_SOC_DAPM_REGULATOR_SUPPLY("VDD", 0, 0),
 199};
 200
 201static const struct snd_soc_dapm_route es7134_dapm_routes[] = {
 202        { "AOUTL", NULL, "DAC" },
 203        { "AOUTR", NULL, "DAC" },
 204        { "DAC", NULL, "VDD" },
 205};
 206
 207static const struct snd_soc_component_driver es7134_component_driver = {
 208        .probe                  = es7134_component_probe,
 209        .dapm_widgets           = es7134_dapm_widgets,
 210        .num_dapm_widgets       = ARRAY_SIZE(es7134_dapm_widgets),
 211        .dapm_routes            = es7134_dapm_routes,
 212        .num_dapm_routes        = ARRAY_SIZE(es7134_dapm_routes),
 213        .idle_bias_on           = 1,
 214        .use_pmdown_time        = 1,
 215        .endianness             = 1,
 216        .non_legacy_dai_naming  = 1,
 217};
 218
 219static struct snd_soc_dai_driver es7154_dai = {
 220        .name = "es7154-hifi",
 221        .playback = {
 222                .stream_name = "Playback",
 223                .channels_min = 2,
 224                .channels_max = 2,
 225                .rates = (SNDRV_PCM_RATE_8000_48000 |
 226                          SNDRV_PCM_RATE_88200      |
 227                          SNDRV_PCM_RATE_96000),
 228                .formats = (SNDRV_PCM_FMTBIT_S16_LE  |
 229                            SNDRV_PCM_FMTBIT_S18_3LE |
 230                            SNDRV_PCM_FMTBIT_S20_3LE |
 231                            SNDRV_PCM_FMTBIT_S24_3LE |
 232                            SNDRV_PCM_FMTBIT_S24_LE),
 233        },
 234        .ops = &es7134_dai_ops,
 235};
 236
 237static const struct es7134_clock_mode es7154_modes[] = {
 238        {
 239                /* Single speed mode */
 240                .rate_min = 8000,
 241                .rate_max = 50000,
 242                .mclk_fs = (unsigned int[]) { 32, 64, 128, 192, 256,
 243                                              384, 512, 768, 1024 },
 244                .mclk_fs_num = 9,
 245        }, {
 246                /* Double speed mode */
 247                .rate_min = 84000,
 248                .rate_max = 100000,
 249                .mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512,
 250                                              768, 1024},
 251                .mclk_fs_num = 7,
 252        }
 253};
 254
 255/* Es7154 has a separate supply for digital I/O  */
 256static const struct snd_soc_dapm_widget es7154_extra_widgets[] = {
 257        SND_SOC_DAPM_REGULATOR_SUPPLY("PVDD", 0, 0),
 258};
 259
 260static const struct snd_soc_dapm_route es7154_extra_routes[] = {
 261        { "Playback", NULL, "PVDD", }
 262};
 263
 264static const struct es7134_chip es7154_chip = {
 265        .dai_drv = &es7154_dai,
 266        .modes = es7154_modes,
 267        .mode_num = ARRAY_SIZE(es7154_modes),
 268        .extra_routes = es7154_extra_routes,
 269        .extra_route_num = ARRAY_SIZE(es7154_extra_routes),
 270        .extra_widgets = es7154_extra_widgets,
 271        .extra_widget_num = ARRAY_SIZE(es7154_extra_widgets),
 272};
 273
 274static int es7134_probe(struct platform_device *pdev)
 275{
 276        struct device *dev = &pdev->dev;
 277        struct es7134_data *priv;
 278
 279        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 280        if (!priv)
 281                return -ENOMEM;
 282        platform_set_drvdata(pdev, priv);
 283
 284        priv->chip = of_device_get_match_data(dev);
 285        if (!priv->chip) {
 286                dev_err(dev, "failed to match device\n");
 287                return -ENODEV;
 288        }
 289
 290        return devm_snd_soc_register_component(&pdev->dev,
 291                                      &es7134_component_driver,
 292                                      priv->chip->dai_drv, 1);
 293}
 294
 295#ifdef CONFIG_OF
 296static const struct of_device_id es7134_ids[] = {
 297        { .compatible = "everest,es7134", .data = &es7134_chip },
 298        { .compatible = "everest,es7144", .data = &es7134_chip },
 299        { .compatible = "everest,es7154", .data = &es7154_chip },
 300        { }
 301};
 302MODULE_DEVICE_TABLE(of, es7134_ids);
 303#endif
 304
 305static struct platform_driver es7134_driver = {
 306        .driver = {
 307                .name = "es7134",
 308                .of_match_table = of_match_ptr(es7134_ids),
 309        },
 310        .probe = es7134_probe,
 311};
 312
 313module_platform_driver(es7134_driver);
 314
 315MODULE_DESCRIPTION("ASoC ES7134 audio codec driver");
 316MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
 317MODULE_LICENSE("GPL");
 318