linux/sound/soc/codecs/wm8776.c
<<
>>
Prefs
   1/*
   2 * wm8776.c  --  WM8776 ALSA SoC Audio driver
   3 *
   4 * Copyright 2009 Wolfson Microelectronics plc
   5 *
   6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 *
  12 * TODO: Input ALC/limiter support
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/moduleparam.h>
  17#include <linux/init.h>
  18#include <linux/delay.h>
  19#include <linux/pm.h>
  20#include <linux/i2c.h>
  21#include <linux/platform_device.h>
  22#include <linux/spi/spi.h>
  23#include <linux/slab.h>
  24#include <sound/core.h>
  25#include <sound/pcm.h>
  26#include <sound/pcm_params.h>
  27#include <sound/soc.h>
  28#include <sound/initval.h>
  29#include <sound/tlv.h>
  30
  31#include "wm8776.h"
  32
  33/* codec private data */
  34struct wm8776_priv {
  35        enum snd_soc_control_type control_type;
  36        int sysclk[2];
  37};
  38
  39static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
  40        0x79, 0x79, 0x79, 0xff, 0xff,  /* 4 */
  41        0xff, 0x00, 0x90, 0x00, 0x00,  /* 9 */
  42        0x22, 0x22, 0x22, 0x08, 0xcf,  /* 14 */
  43        0xcf, 0x7b, 0x00, 0x32, 0x00,  /* 19 */
  44        0xa6, 0x01, 0x01
  45};
  46
  47static int wm8776_reset(struct snd_soc_codec *codec)
  48{
  49        return snd_soc_write(codec, WM8776_RESET, 0);
  50}
  51
  52static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
  53static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  54static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
  55
  56static const struct snd_kcontrol_new wm8776_snd_controls[] = {
  57SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
  58                 0, 127, 0, hp_tlv),
  59SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
  60                 0, 255, 0, dac_tlv),
  61SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
  62
  63SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
  64
  65SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
  66                 0, 255, 0, adc_tlv),
  67SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
  68SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
  69SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
  70};
  71
  72static const struct snd_kcontrol_new inmix_controls[] = {
  73SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
  74SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
  75SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
  76SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
  77SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
  78};
  79
  80static const struct snd_kcontrol_new outmix_controls[] = {
  81SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
  82SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
  83SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
  84};
  85
  86static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
  87SND_SOC_DAPM_INPUT("AUX"),
  88
  89SND_SOC_DAPM_INPUT("AIN1"),
  90SND_SOC_DAPM_INPUT("AIN2"),
  91SND_SOC_DAPM_INPUT("AIN3"),
  92SND_SOC_DAPM_INPUT("AIN4"),
  93SND_SOC_DAPM_INPUT("AIN5"),
  94
  95SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
  96                   inmix_controls, ARRAY_SIZE(inmix_controls)),
  97
  98SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
  99SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
 100
 101SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
 102                   outmix_controls, ARRAY_SIZE(outmix_controls)),
 103
 104SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
 105
 106SND_SOC_DAPM_OUTPUT("VOUT"),
 107
 108SND_SOC_DAPM_OUTPUT("HPOUTL"),
 109SND_SOC_DAPM_OUTPUT("HPOUTR"),
 110};
 111
 112static const struct snd_soc_dapm_route routes[] = {
 113        { "Input Mixer", "AIN1 Switch", "AIN1" },
 114        { "Input Mixer", "AIN2 Switch", "AIN2" },
 115        { "Input Mixer", "AIN3 Switch", "AIN3" },
 116        { "Input Mixer", "AIN4 Switch", "AIN4" },
 117        { "Input Mixer", "AIN5 Switch", "AIN5" },
 118
 119        { "ADC", NULL, "Input Mixer" },
 120
 121        { "Output Mixer", "DAC Switch", "DAC" },
 122        { "Output Mixer", "AUX Switch", "AUX" },
 123        { "Output Mixer", "Bypass Switch", "Input Mixer" },
 124
 125        { "VOUT", NULL, "Output Mixer" },
 126
 127        { "Headphone PGA", NULL, "Output Mixer" },
 128
 129        { "HPOUTL", NULL, "Headphone PGA" },
 130        { "HPOUTR", NULL, "Headphone PGA" },
 131};
 132
 133static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 134{
 135        struct snd_soc_codec *codec = dai->codec;
 136        int reg, iface, master;
 137
 138        switch (dai->driver->id) {
 139        case WM8776_DAI_DAC:
 140                reg = WM8776_DACIFCTRL;
 141                master = 0x80;
 142                break;
 143        case WM8776_DAI_ADC:
 144                reg = WM8776_ADCIFCTRL;
 145                master = 0x100;
 146                break;
 147        default:
 148                return -EINVAL;
 149        }
 150
 151        iface = 0;
 152
 153        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 154        case SND_SOC_DAIFMT_CBM_CFM:
 155                break;
 156        case SND_SOC_DAIFMT_CBS_CFS:
 157                master = 0;
 158                break;
 159        default:
 160                return -EINVAL;
 161        }
 162
 163        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 164        case SND_SOC_DAIFMT_I2S:
 165                iface |= 0x0002;
 166                break;
 167        case SND_SOC_DAIFMT_RIGHT_J:
 168                break;
 169        case SND_SOC_DAIFMT_LEFT_J:
 170                iface |= 0x0001;
 171                break;
 172        default:
 173                return -EINVAL;
 174        }
 175
 176        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 177        case SND_SOC_DAIFMT_NB_NF:
 178                break;
 179        case SND_SOC_DAIFMT_IB_IF:
 180                iface |= 0x00c;
 181                break;
 182        case SND_SOC_DAIFMT_IB_NF:
 183                iface |= 0x008;
 184                break;
 185        case SND_SOC_DAIFMT_NB_IF:
 186                iface |= 0x004;
 187                break;
 188        default:
 189                return -EINVAL;
 190        }
 191
 192        /* Finally, write out the values */
 193        snd_soc_update_bits(codec, reg, 0xf, iface);
 194        snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
 195
 196        return 0;
 197}
 198
 199static int mclk_ratios[] = {
 200        128,
 201        192,
 202        256,
 203        384,
 204        512,
 205        768,
 206};
 207
 208static int wm8776_hw_params(struct snd_pcm_substream *substream,
 209                            struct snd_pcm_hw_params *params,
 210                            struct snd_soc_dai *dai)
 211{
 212        struct snd_soc_codec *codec = dai->codec;
 213        struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
 214        int iface_reg, iface;
 215        int ratio_shift, master;
 216        int i;
 217
 218        iface = 0;
 219
 220        switch (dai->driver->id) {
 221        case WM8776_DAI_DAC:
 222                iface_reg = WM8776_DACIFCTRL;
 223                master = 0x80;
 224                ratio_shift = 4;
 225                break;
 226        case WM8776_DAI_ADC:
 227                iface_reg = WM8776_ADCIFCTRL;
 228                master = 0x100;
 229                ratio_shift = 0;
 230                break;
 231        default:
 232                return -EINVAL;
 233        }
 234
 235
 236        /* Set word length */
 237        switch (params_format(params)) {
 238        case SNDRV_PCM_FORMAT_S16_LE:
 239                break;
 240        case SNDRV_PCM_FORMAT_S20_3LE:
 241                iface |= 0x10;
 242                break;
 243        case SNDRV_PCM_FORMAT_S24_LE:
 244                iface |= 0x20;
 245                break;
 246        case SNDRV_PCM_FORMAT_S32_LE:
 247                iface |= 0x30;
 248                break;
 249        }
 250
 251        /* Only need to set MCLK/LRCLK ratio if we're master */
 252        if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
 253                for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
 254                        if (wm8776->sysclk[dai->driver->id] / params_rate(params)
 255                            == mclk_ratios[i])
 256                                break;
 257                }
 258
 259                if (i == ARRAY_SIZE(mclk_ratios)) {
 260                        dev_err(codec->dev,
 261                                "Unable to configure MCLK ratio %d/%d\n",
 262                                wm8776->sysclk[dai->driver->id], params_rate(params));
 263                        return -EINVAL;
 264                }
 265
 266                dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
 267
 268                snd_soc_update_bits(codec, WM8776_MSTRCTRL,
 269                                    0x7 << ratio_shift, i << ratio_shift);
 270        } else {
 271                dev_dbg(codec->dev, "DAI in slave mode\n");
 272        }
 273
 274        snd_soc_update_bits(codec, iface_reg, 0x30, iface);
 275
 276        return 0;
 277}
 278
 279static int wm8776_mute(struct snd_soc_dai *dai, int mute)
 280{
 281        struct snd_soc_codec *codec = dai->codec;
 282
 283        return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
 284}
 285
 286static int wm8776_set_sysclk(struct snd_soc_dai *dai,
 287                             int clk_id, unsigned int freq, int dir)
 288{
 289        struct snd_soc_codec *codec = dai->codec;
 290        struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
 291
 292        BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
 293
 294        wm8776->sysclk[dai->driver->id] = freq;
 295
 296        return 0;
 297}
 298
 299static int wm8776_set_bias_level(struct snd_soc_codec *codec,
 300                                 enum snd_soc_bias_level level)
 301{
 302        switch (level) {
 303        case SND_SOC_BIAS_ON:
 304                break;
 305        case SND_SOC_BIAS_PREPARE:
 306                break;
 307        case SND_SOC_BIAS_STANDBY:
 308                if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
 309                        /* Disable the global powerdown; DAPM does the rest */
 310                        snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
 311                }
 312
 313                break;
 314        case SND_SOC_BIAS_OFF:
 315                snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
 316                break;
 317        }
 318
 319        codec->dapm.bias_level = level;
 320        return 0;
 321}
 322
 323#define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
 324                      SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
 325                      SNDRV_PCM_RATE_96000)
 326
 327
 328#define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
 329                        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
 330
 331static struct snd_soc_dai_ops wm8776_dac_ops = {
 332        .digital_mute   = wm8776_mute,
 333        .hw_params      = wm8776_hw_params,
 334        .set_fmt        = wm8776_set_fmt,
 335        .set_sysclk     = wm8776_set_sysclk,
 336};
 337
 338static struct snd_soc_dai_ops wm8776_adc_ops = {
 339        .hw_params      = wm8776_hw_params,
 340        .set_fmt        = wm8776_set_fmt,
 341        .set_sysclk     = wm8776_set_sysclk,
 342};
 343
 344static struct snd_soc_dai_driver wm8776_dai[] = {
 345        {
 346                .name = "wm8776-hifi-playback",
 347                .id     = WM8776_DAI_DAC,
 348                .playback = {
 349                        .stream_name = "Playback",
 350                        .channels_min = 2,
 351                        .channels_max = 2,
 352                        .rates = WM8776_RATES,
 353                        .formats = WM8776_FORMATS,
 354                },
 355                .ops = &wm8776_dac_ops,
 356        },
 357        {
 358                .name = "wm8776-hifi-capture",
 359                .id     = WM8776_DAI_ADC,
 360                .capture = {
 361                        .stream_name = "Capture",
 362                        .channels_min = 2,
 363                        .channels_max = 2,
 364                        .rates = WM8776_RATES,
 365                        .formats = WM8776_FORMATS,
 366                },
 367                .ops = &wm8776_adc_ops,
 368        },
 369};
 370
 371#ifdef CONFIG_PM
 372static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
 373{
 374        wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
 375
 376        return 0;
 377}
 378
 379static int wm8776_resume(struct snd_soc_codec *codec)
 380{
 381        int i;
 382        u8 data[2];
 383        u16 *cache = codec->reg_cache;
 384
 385        /* Sync reg_cache with the hardware */
 386        for (i = 0; i < ARRAY_SIZE(wm8776_reg); i++) {
 387                if (cache[i] == wm8776_reg[i])
 388                        continue;
 389                data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
 390                data[1] = cache[i] & 0x00ff;
 391                codec->hw_write(codec->control_data, data, 2);
 392        }
 393
 394        wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 395
 396        return 0;
 397}
 398#else
 399#define wm8776_suspend NULL
 400#define wm8776_resume NULL
 401#endif
 402
 403static int wm8776_probe(struct snd_soc_codec *codec)
 404{
 405        struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
 406        struct snd_soc_dapm_context *dapm = &codec->dapm;
 407        int ret = 0;
 408
 409        ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
 410        if (ret < 0) {
 411                dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
 412                return ret;
 413        }
 414
 415        ret = wm8776_reset(codec);
 416        if (ret < 0) {
 417                dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
 418                return ret;
 419        }
 420
 421        wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 422
 423        /* Latch the update bits; right channel only since we always
 424         * update both. */
 425        snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
 426        snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
 427
 428        snd_soc_add_controls(codec, wm8776_snd_controls,
 429                             ARRAY_SIZE(wm8776_snd_controls));
 430        snd_soc_dapm_new_controls(dapm, wm8776_dapm_widgets,
 431                                  ARRAY_SIZE(wm8776_dapm_widgets));
 432        snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
 433
 434        return ret;
 435}
 436
 437/* power down chip */
 438static int wm8776_remove(struct snd_soc_codec *codec)
 439{
 440        wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
 441        return 0;
 442}
 443
 444static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
 445        .probe =        wm8776_probe,
 446        .remove =       wm8776_remove,
 447        .suspend =      wm8776_suspend,
 448        .resume =       wm8776_resume,
 449        .set_bias_level = wm8776_set_bias_level,
 450        .reg_cache_size = ARRAY_SIZE(wm8776_reg),
 451        .reg_word_size = sizeof(u16),
 452        .reg_cache_default = wm8776_reg,
 453};
 454
 455#if defined(CONFIG_SPI_MASTER)
 456static int __devinit wm8776_spi_probe(struct spi_device *spi)
 457{
 458        struct wm8776_priv *wm8776;
 459        int ret;
 460
 461        wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
 462        if (wm8776 == NULL)
 463                return -ENOMEM;
 464
 465        wm8776->control_type = SND_SOC_SPI;
 466        spi_set_drvdata(spi, wm8776);
 467
 468        ret = snd_soc_register_codec(&spi->dev,
 469                        &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
 470        if (ret < 0)
 471                kfree(wm8776);
 472        return ret;
 473}
 474
 475static int __devexit wm8776_spi_remove(struct spi_device *spi)
 476{
 477        snd_soc_unregister_codec(&spi->dev);
 478        kfree(spi_get_drvdata(spi));
 479        return 0;
 480}
 481
 482static struct spi_driver wm8776_spi_driver = {
 483        .driver = {
 484                .name   = "wm8776-codec",
 485                .owner  = THIS_MODULE,
 486        },
 487        .probe          = wm8776_spi_probe,
 488        .remove         = __devexit_p(wm8776_spi_remove),
 489};
 490#endif /* CONFIG_SPI_MASTER */
 491
 492#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 493static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
 494                                      const struct i2c_device_id *id)
 495{
 496        struct wm8776_priv *wm8776;
 497        int ret;
 498
 499        wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
 500        if (wm8776 == NULL)
 501                return -ENOMEM;
 502
 503        i2c_set_clientdata(i2c, wm8776);
 504        wm8776->control_type = SND_SOC_I2C;
 505
 506        ret =  snd_soc_register_codec(&i2c->dev,
 507                        &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
 508        if (ret < 0)
 509                kfree(wm8776);
 510        return ret;
 511}
 512
 513static __devexit int wm8776_i2c_remove(struct i2c_client *client)
 514{
 515        snd_soc_unregister_codec(&client->dev);
 516        kfree(i2c_get_clientdata(client));
 517        return 0;
 518}
 519
 520static const struct i2c_device_id wm8776_i2c_id[] = {
 521        { "wm8776", 0 },
 522        { }
 523};
 524MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
 525
 526static struct i2c_driver wm8776_i2c_driver = {
 527        .driver = {
 528                .name = "wm8776-codec",
 529                .owner = THIS_MODULE,
 530        },
 531        .probe =    wm8776_i2c_probe,
 532        .remove =   __devexit_p(wm8776_i2c_remove),
 533        .id_table = wm8776_i2c_id,
 534};
 535#endif
 536
 537static int __init wm8776_modinit(void)
 538{
 539        int ret = 0;
 540#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 541        ret = i2c_add_driver(&wm8776_i2c_driver);
 542        if (ret != 0) {
 543                printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
 544                       ret);
 545        }
 546#endif
 547#if defined(CONFIG_SPI_MASTER)
 548        ret = spi_register_driver(&wm8776_spi_driver);
 549        if (ret != 0) {
 550                printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
 551                       ret);
 552        }
 553#endif
 554        return ret;
 555}
 556module_init(wm8776_modinit);
 557
 558static void __exit wm8776_exit(void)
 559{
 560#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 561        i2c_del_driver(&wm8776_i2c_driver);
 562#endif
 563#if defined(CONFIG_SPI_MASTER)
 564        spi_unregister_driver(&wm8776_spi_driver);
 565#endif
 566}
 567module_exit(wm8776_exit);
 568
 569MODULE_DESCRIPTION("ASoC WM8776 driver");
 570MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
 571MODULE_LICENSE("GPL");
 572