linux/sound/soc/codecs/wm8510.c
<<
>>
Prefs
   1/*
   2 * wm8510.c  --  WM8510 ALSA Soc Audio driver
   3 *
   4 * Copyright 2006 Wolfson Microelectronics PLC.
   5 *
   6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
   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
  13#include <linux/module.h>
  14#include <linux/moduleparam.h>
  15#include <linux/kernel.h>
  16#include <linux/init.h>
  17#include <linux/delay.h>
  18#include <linux/pm.h>
  19#include <linux/i2c.h>
  20#include <linux/platform_device.h>
  21#include <linux/spi/spi.h>
  22#include <linux/slab.h>
  23#include <sound/core.h>
  24#include <sound/pcm.h>
  25#include <sound/pcm_params.h>
  26#include <sound/soc.h>
  27#include <sound/initval.h>
  28
  29#include "wm8510.h"
  30
  31/*
  32 * wm8510 register cache
  33 * We can't read the WM8510 register space when we are
  34 * using 2 wire for device control, so we cache them instead.
  35 */
  36static const u16 wm8510_reg[WM8510_CACHEREGNUM] = {
  37        0x0000, 0x0000, 0x0000, 0x0000,
  38        0x0050, 0x0000, 0x0140, 0x0000,
  39        0x0000, 0x0000, 0x0000, 0x00ff,
  40        0x0000, 0x0000, 0x0100, 0x00ff,
  41        0x0000, 0x0000, 0x012c, 0x002c,
  42        0x002c, 0x002c, 0x002c, 0x0000,
  43        0x0032, 0x0000, 0x0000, 0x0000,
  44        0x0000, 0x0000, 0x0000, 0x0000,
  45        0x0038, 0x000b, 0x0032, 0x0000,
  46        0x0008, 0x000c, 0x0093, 0x00e9,
  47        0x0000, 0x0000, 0x0000, 0x0000,
  48        0x0003, 0x0010, 0x0000, 0x0000,
  49        0x0000, 0x0002, 0x0001, 0x0000,
  50        0x0000, 0x0000, 0x0039, 0x0000,
  51        0x0001,
  52};
  53
  54#define WM8510_POWER1_BIASEN  0x08
  55#define WM8510_POWER1_BUFIOEN 0x10
  56
  57#define wm8510_reset(c) snd_soc_write(c, WM8510_RESET, 0)
  58
  59/* codec private data */
  60struct wm8510_priv {
  61        enum snd_soc_control_type control_type;
  62};
  63
  64static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
  65static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
  66static const char *wm8510_alc[] = { "ALC", "Limiter" };
  67
  68static const struct soc_enum wm8510_enum[] = {
  69        SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
  70        SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
  71        SOC_ENUM_SINGLE(WM8510_DAC,  4, 4, wm8510_deemp),
  72        SOC_ENUM_SINGLE(WM8510_ALC3,  8, 2, wm8510_alc),
  73};
  74
  75static const struct snd_kcontrol_new wm8510_snd_controls[] = {
  76
  77SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
  78
  79SOC_ENUM("DAC Companding", wm8510_enum[1]),
  80SOC_ENUM("ADC Companding", wm8510_enum[0]),
  81
  82SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
  83SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
  84
  85SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
  86
  87SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
  88SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
  89SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
  90
  91SOC_SINGLE("Capture Volume", WM8510_ADCVOL,  0, 127, 0),
  92
  93SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1,  8, 1, 0),
  94SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1,  4, 15, 0),
  95SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1,  0, 15, 0),
  96
  97SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2,  4, 7, 0),
  98SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2,  0, 15, 0),
  99
 100SOC_SINGLE("ALC Enable Switch", WM8510_ALC1,  8, 1, 0),
 101SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1,  3, 7, 0),
 102SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1,  0, 7, 0),
 103
 104SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2,  8, 1, 0),
 105SOC_SINGLE("ALC Capture Hold", WM8510_ALC2,  4, 7, 0),
 106SOC_SINGLE("ALC Capture Target", WM8510_ALC2,  0, 15, 0),
 107
 108SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
 109SOC_SINGLE("ALC Capture Decay", WM8510_ALC3,  4, 15, 0),
 110SOC_SINGLE("ALC Capture Attack", WM8510_ALC3,  0, 15, 0),
 111
 112SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE,  3, 1, 0),
 113SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE,  0, 7, 0),
 114
 115SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA,  7, 1, 0),
 116SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA,  0, 63, 0),
 117
 118SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL,  7, 1, 0),
 119SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL,  6, 1, 1),
 120SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL,  0, 63, 0),
 121SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
 122
 123SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST,  8, 1, 0),
 124SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
 125};
 126
 127/* Speaker Output Mixer */
 128static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
 129SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
 130SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
 131SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
 132};
 133
 134/* Mono Output Mixer */
 135static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
 136SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
 137SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
 138SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
 139};
 140
 141static const struct snd_kcontrol_new wm8510_boost_controls[] = {
 142SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA,  6, 1, 1),
 143SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
 144SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
 145};
 146
 147static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
 148SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
 149SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
 150SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
 151};
 152
 153static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
 154SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
 155        &wm8510_speaker_mixer_controls[0],
 156        ARRAY_SIZE(wm8510_speaker_mixer_controls)),
 157SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
 158        &wm8510_mono_mixer_controls[0],
 159        ARRAY_SIZE(wm8510_mono_mixer_controls)),
 160SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
 161SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
 162SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
 163SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
 164SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
 165SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
 166
 167SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
 168                   &wm8510_micpga_controls[0],
 169                   ARRAY_SIZE(wm8510_micpga_controls)),
 170SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
 171        &wm8510_boost_controls[0],
 172        ARRAY_SIZE(wm8510_boost_controls)),
 173
 174SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
 175
 176SND_SOC_DAPM_INPUT("MICN"),
 177SND_SOC_DAPM_INPUT("MICP"),
 178SND_SOC_DAPM_INPUT("AUX"),
 179SND_SOC_DAPM_OUTPUT("MONOOUT"),
 180SND_SOC_DAPM_OUTPUT("SPKOUTP"),
 181SND_SOC_DAPM_OUTPUT("SPKOUTN"),
 182};
 183
 184static const struct snd_soc_dapm_route audio_map[] = {
 185        /* Mono output mixer */
 186        {"Mono Mixer", "PCM Playback Switch", "DAC"},
 187        {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
 188        {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
 189
 190        /* Speaker output mixer */
 191        {"Speaker Mixer", "PCM Playback Switch", "DAC"},
 192        {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
 193        {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
 194
 195        /* Outputs */
 196        {"Mono Out", NULL, "Mono Mixer"},
 197        {"MONOOUT", NULL, "Mono Out"},
 198        {"SpkN Out", NULL, "Speaker Mixer"},
 199        {"SpkP Out", NULL, "Speaker Mixer"},
 200        {"SPKOUTN", NULL, "SpkN Out"},
 201        {"SPKOUTP", NULL, "SpkP Out"},
 202
 203        /* Microphone PGA */
 204        {"Mic PGA", "MICN Switch", "MICN"},
 205        {"Mic PGA", "MICP Switch", "MICP"},
 206        { "Mic PGA", "AUX Switch", "Aux Input" },
 207
 208        /* Boost Mixer */
 209        {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
 210        {"Boost Mixer", "Mic Volume", "MICP"},
 211        {"Boost Mixer", "Aux Volume", "Aux Input"},
 212
 213        {"ADC", NULL, "Boost Mixer"},
 214};
 215
 216static int wm8510_add_widgets(struct snd_soc_codec *codec)
 217{
 218        struct snd_soc_dapm_context *dapm = &codec->dapm;
 219
 220        snd_soc_dapm_new_controls(dapm, wm8510_dapm_widgets,
 221                                  ARRAY_SIZE(wm8510_dapm_widgets));
 222        snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
 223
 224        return 0;
 225}
 226
 227struct pll_ {
 228        unsigned int pre_div:4; /* prescale - 1 */
 229        unsigned int n:4;
 230        unsigned int k;
 231};
 232
 233static struct pll_ pll_div;
 234
 235/* The size in bits of the pll divide multiplied by 10
 236 * to allow rounding later */
 237#define FIXED_PLL_SIZE ((1 << 24) * 10)
 238
 239static void pll_factors(unsigned int target, unsigned int source)
 240{
 241        unsigned long long Kpart;
 242        unsigned int K, Ndiv, Nmod;
 243
 244        Ndiv = target / source;
 245        if (Ndiv < 6) {
 246                source >>= 1;
 247                pll_div.pre_div = 1;
 248                Ndiv = target / source;
 249        } else
 250                pll_div.pre_div = 0;
 251
 252        if ((Ndiv < 6) || (Ndiv > 12))
 253                printk(KERN_WARNING
 254                        "WM8510 N value %u outwith recommended range!d\n",
 255                        Ndiv);
 256
 257        pll_div.n = Ndiv;
 258        Nmod = target % source;
 259        Kpart = FIXED_PLL_SIZE * (long long)Nmod;
 260
 261        do_div(Kpart, source);
 262
 263        K = Kpart & 0xFFFFFFFF;
 264
 265        /* Check if we need to round */
 266        if ((K % 10) >= 5)
 267                K += 5;
 268
 269        /* Move down to proper range now rounding is done */
 270        K /= 10;
 271
 272        pll_div.k = K;
 273}
 274
 275static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
 276                int source, unsigned int freq_in, unsigned int freq_out)
 277{
 278        struct snd_soc_codec *codec = codec_dai->codec;
 279        u16 reg;
 280
 281        if (freq_in == 0 || freq_out == 0) {
 282                /* Clock CODEC directly from MCLK */
 283                reg = snd_soc_read(codec, WM8510_CLOCK);
 284                snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
 285
 286                /* Turn off PLL */
 287                reg = snd_soc_read(codec, WM8510_POWER1);
 288                snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
 289                return 0;
 290        }
 291
 292        pll_factors(freq_out*4, freq_in);
 293
 294        snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
 295        snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
 296        snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
 297        snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
 298        reg = snd_soc_read(codec, WM8510_POWER1);
 299        snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
 300
 301        /* Run CODEC from PLL instead of MCLK */
 302        reg = snd_soc_read(codec, WM8510_CLOCK);
 303        snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
 304
 305        return 0;
 306}
 307
 308/*
 309 * Configure WM8510 clock dividers.
 310 */
 311static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
 312                int div_id, int div)
 313{
 314        struct snd_soc_codec *codec = codec_dai->codec;
 315        u16 reg;
 316
 317        switch (div_id) {
 318        case WM8510_OPCLKDIV:
 319                reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
 320                snd_soc_write(codec, WM8510_GPIO, reg | div);
 321                break;
 322        case WM8510_MCLKDIV:
 323                reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
 324                snd_soc_write(codec, WM8510_CLOCK, reg | div);
 325                break;
 326        case WM8510_ADCCLK:
 327                reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
 328                snd_soc_write(codec, WM8510_ADC, reg | div);
 329                break;
 330        case WM8510_DACCLK:
 331                reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
 332                snd_soc_write(codec, WM8510_DAC, reg | div);
 333                break;
 334        case WM8510_BCLKDIV:
 335                reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
 336                snd_soc_write(codec, WM8510_CLOCK, reg | div);
 337                break;
 338        default:
 339                return -EINVAL;
 340        }
 341
 342        return 0;
 343}
 344
 345static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
 346                unsigned int fmt)
 347{
 348        struct snd_soc_codec *codec = codec_dai->codec;
 349        u16 iface = 0;
 350        u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
 351
 352        /* set master/slave audio interface */
 353        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 354        case SND_SOC_DAIFMT_CBM_CFM:
 355                clk |= 0x0001;
 356                break;
 357        case SND_SOC_DAIFMT_CBS_CFS:
 358                break;
 359        default:
 360                return -EINVAL;
 361        }
 362
 363        /* interface format */
 364        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 365        case SND_SOC_DAIFMT_I2S:
 366                iface |= 0x0010;
 367                break;
 368        case SND_SOC_DAIFMT_RIGHT_J:
 369                break;
 370        case SND_SOC_DAIFMT_LEFT_J:
 371                iface |= 0x0008;
 372                break;
 373        case SND_SOC_DAIFMT_DSP_A:
 374                iface |= 0x00018;
 375                break;
 376        default:
 377                return -EINVAL;
 378        }
 379
 380        /* clock inversion */
 381        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 382        case SND_SOC_DAIFMT_NB_NF:
 383                break;
 384        case SND_SOC_DAIFMT_IB_IF:
 385                iface |= 0x0180;
 386                break;
 387        case SND_SOC_DAIFMT_IB_NF:
 388                iface |= 0x0100;
 389                break;
 390        case SND_SOC_DAIFMT_NB_IF:
 391                iface |= 0x0080;
 392                break;
 393        default:
 394                return -EINVAL;
 395        }
 396
 397        snd_soc_write(codec, WM8510_IFACE, iface);
 398        snd_soc_write(codec, WM8510_CLOCK, clk);
 399        return 0;
 400}
 401
 402static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
 403                                struct snd_pcm_hw_params *params,
 404                                struct snd_soc_dai *dai)
 405{
 406        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 407        struct snd_soc_codec *codec = rtd->codec;
 408        u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
 409        u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
 410
 411        /* bit size */
 412        switch (params_format(params)) {
 413        case SNDRV_PCM_FORMAT_S16_LE:
 414                break;
 415        case SNDRV_PCM_FORMAT_S20_3LE:
 416                iface |= 0x0020;
 417                break;
 418        case SNDRV_PCM_FORMAT_S24_LE:
 419                iface |= 0x0040;
 420                break;
 421        case SNDRV_PCM_FORMAT_S32_LE:
 422                iface |= 0x0060;
 423                break;
 424        }
 425
 426        /* filter coefficient */
 427        switch (params_rate(params)) {
 428        case 8000:
 429                adn |= 0x5 << 1;
 430                break;
 431        case 11025:
 432                adn |= 0x4 << 1;
 433                break;
 434        case 16000:
 435                adn |= 0x3 << 1;
 436                break;
 437        case 22050:
 438                adn |= 0x2 << 1;
 439                break;
 440        case 32000:
 441                adn |= 0x1 << 1;
 442                break;
 443        case 44100:
 444        case 48000:
 445                break;
 446        }
 447
 448        snd_soc_write(codec, WM8510_IFACE, iface);
 449        snd_soc_write(codec, WM8510_ADD, adn);
 450        return 0;
 451}
 452
 453static int wm8510_mute(struct snd_soc_dai *dai, int mute)
 454{
 455        struct snd_soc_codec *codec = dai->codec;
 456        u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
 457
 458        if (mute)
 459                snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
 460        else
 461                snd_soc_write(codec, WM8510_DAC, mute_reg);
 462        return 0;
 463}
 464
 465/* liam need to make this lower power with dapm */
 466static int wm8510_set_bias_level(struct snd_soc_codec *codec,
 467        enum snd_soc_bias_level level)
 468{
 469        u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
 470
 471        switch (level) {
 472        case SND_SOC_BIAS_ON:
 473        case SND_SOC_BIAS_PREPARE:
 474                power1 |= 0x1;  /* VMID 50k */
 475                snd_soc_write(codec, WM8510_POWER1, power1);
 476                break;
 477
 478        case SND_SOC_BIAS_STANDBY:
 479                power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
 480
 481                if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
 482                        /* Initial cap charge at VMID 5k */
 483                        snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
 484                        mdelay(100);
 485                }
 486
 487                power1 |= 0x2;  /* VMID 500k */
 488                snd_soc_write(codec, WM8510_POWER1, power1);
 489                break;
 490
 491        case SND_SOC_BIAS_OFF:
 492                snd_soc_write(codec, WM8510_POWER1, 0);
 493                snd_soc_write(codec, WM8510_POWER2, 0);
 494                snd_soc_write(codec, WM8510_POWER3, 0);
 495                break;
 496        }
 497
 498        codec->dapm.bias_level = level;
 499        return 0;
 500}
 501
 502#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
 503                SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
 504                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
 505
 506#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
 507        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
 508
 509static struct snd_soc_dai_ops wm8510_dai_ops = {
 510        .hw_params      = wm8510_pcm_hw_params,
 511        .digital_mute   = wm8510_mute,
 512        .set_fmt        = wm8510_set_dai_fmt,
 513        .set_clkdiv     = wm8510_set_dai_clkdiv,
 514        .set_pll        = wm8510_set_dai_pll,
 515};
 516
 517static struct snd_soc_dai_driver wm8510_dai = {
 518        .name = "wm8510-hifi",
 519        .playback = {
 520                .stream_name = "Playback",
 521                .channels_min = 2,
 522                .channels_max = 2,
 523                .rates = WM8510_RATES,
 524                .formats = WM8510_FORMATS,},
 525        .capture = {
 526                .stream_name = "Capture",
 527                .channels_min = 2,
 528                .channels_max = 2,
 529                .rates = WM8510_RATES,
 530                .formats = WM8510_FORMATS,},
 531        .ops = &wm8510_dai_ops,
 532        .symmetric_rates = 1,
 533};
 534
 535static int wm8510_suspend(struct snd_soc_codec *codec, pm_message_t state)
 536{
 537        wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
 538        return 0;
 539}
 540
 541static int wm8510_resume(struct snd_soc_codec *codec)
 542{
 543        int i;
 544        u8 data[2];
 545        u16 *cache = codec->reg_cache;
 546
 547        /* Sync reg_cache with the hardware */
 548        for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
 549                data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
 550                data[1] = cache[i] & 0x00ff;
 551                codec->hw_write(codec->control_data, data, 2);
 552        }
 553        wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 554
 555        return 0;
 556}
 557
 558static int wm8510_probe(struct snd_soc_codec *codec)
 559{
 560        struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
 561        int ret;
 562
 563        ret = snd_soc_codec_set_cache_io(codec, 7, 9,  wm8510->control_type);
 564        if (ret < 0) {
 565                printk(KERN_ERR "wm8510: failed to set cache I/O: %d\n", ret);
 566                return ret;
 567        }
 568
 569        wm8510_reset(codec);
 570
 571        /* power on device */
 572        wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 573        snd_soc_add_controls(codec, wm8510_snd_controls,
 574                                ARRAY_SIZE(wm8510_snd_controls));
 575        wm8510_add_widgets(codec);
 576
 577        return ret;
 578}
 579
 580/* power down chip */
 581static int wm8510_remove(struct snd_soc_codec *codec)
 582{
 583        struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
 584
 585        wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
 586        kfree(wm8510);
 587        return 0;
 588}
 589
 590static struct snd_soc_codec_driver soc_codec_dev_wm8510 = {
 591        .probe =        wm8510_probe,
 592        .remove =       wm8510_remove,
 593        .suspend =      wm8510_suspend,
 594        .resume =       wm8510_resume,
 595        .set_bias_level = wm8510_set_bias_level,
 596        .reg_cache_size = ARRAY_SIZE(wm8510_reg),
 597        .reg_word_size = sizeof(u16),
 598        .reg_cache_default =wm8510_reg,
 599};
 600
 601#if defined(CONFIG_SPI_MASTER)
 602static int __devinit wm8510_spi_probe(struct spi_device *spi)
 603{
 604        struct wm8510_priv *wm8510;
 605        int ret;
 606
 607        wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
 608        if (wm8510 == NULL)
 609                return -ENOMEM;
 610
 611        wm8510->control_type = SND_SOC_SPI;
 612        spi_set_drvdata(spi, wm8510);
 613
 614        ret = snd_soc_register_codec(&spi->dev,
 615                        &soc_codec_dev_wm8510, &wm8510_dai, 1);
 616        if (ret < 0)
 617                kfree(wm8510);
 618        return ret;
 619}
 620
 621static int __devexit wm8510_spi_remove(struct spi_device *spi)
 622{
 623        snd_soc_unregister_codec(&spi->dev);
 624        return 0;
 625}
 626
 627static struct spi_driver wm8510_spi_driver = {
 628        .driver = {
 629                .name   = "wm8510",
 630                .owner  = THIS_MODULE,
 631        },
 632        .probe          = wm8510_spi_probe,
 633        .remove         = __devexit_p(wm8510_spi_remove),
 634};
 635#endif /* CONFIG_SPI_MASTER */
 636
 637#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 638static __devinit int wm8510_i2c_probe(struct i2c_client *i2c,
 639                                      const struct i2c_device_id *id)
 640{
 641        struct wm8510_priv *wm8510;
 642        int ret;
 643
 644        wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
 645        if (wm8510 == NULL)
 646                return -ENOMEM;
 647
 648        i2c_set_clientdata(i2c, wm8510);
 649        wm8510->control_type = SND_SOC_I2C;
 650
 651        ret =  snd_soc_register_codec(&i2c->dev,
 652                        &soc_codec_dev_wm8510, &wm8510_dai, 1);
 653        if (ret < 0)
 654                kfree(wm8510);
 655        return ret;
 656}
 657
 658static __devexit int wm8510_i2c_remove(struct i2c_client *client)
 659{
 660        snd_soc_unregister_codec(&client->dev);
 661        return 0;
 662}
 663
 664static const struct i2c_device_id wm8510_i2c_id[] = {
 665        { "wm8510", 0 },
 666        { }
 667};
 668MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
 669
 670static struct i2c_driver wm8510_i2c_driver = {
 671        .driver = {
 672                .name = "wm8510-codec",
 673                .owner = THIS_MODULE,
 674        },
 675        .probe =    wm8510_i2c_probe,
 676        .remove =   __devexit_p(wm8510_i2c_remove),
 677        .id_table = wm8510_i2c_id,
 678};
 679#endif
 680
 681static int __init wm8510_modinit(void)
 682{
 683        int ret = 0;
 684#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 685        ret = i2c_add_driver(&wm8510_i2c_driver);
 686        if (ret != 0) {
 687                printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n",
 688                       ret);
 689        }
 690#endif
 691#if defined(CONFIG_SPI_MASTER)
 692        ret = spi_register_driver(&wm8510_spi_driver);
 693        if (ret != 0) {
 694                printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n",
 695                       ret);
 696        }
 697#endif
 698        return ret;
 699}
 700module_init(wm8510_modinit);
 701
 702static void __exit wm8510_exit(void)
 703{
 704#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
 705        i2c_del_driver(&wm8510_i2c_driver);
 706#endif
 707#if defined(CONFIG_SPI_MASTER)
 708        spi_unregister_driver(&wm8510_spi_driver);
 709#endif
 710}
 711module_exit(wm8510_exit);
 712
 713MODULE_DESCRIPTION("ASoC WM8510 driver");
 714MODULE_AUTHOR("Liam Girdwood");
 715MODULE_LICENSE("GPL");
 716