linux/sound/soc/codecs/wm2000.c
<<
>>
Prefs
   1/*
   2 * wm2000.c  --  WM2000 ALSA Soc Audio driver
   3 *
   4 * Copyright 2008-2010 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 * The download image for the WM2000 will be requested as
  13 * 'wm2000_anc.bin' by default (overridable via platform data) at
  14 * runtime and is expected to be in flat binary format.  This is
  15 * generated by Wolfson configuration tools and includes
  16 * system-specific callibration information.  If supplied as a
  17 * sequence of ASCII-encoded hexidecimal bytes this can be converted
  18 * into a flat binary with a command such as this on the command line:
  19 *
  20 * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
  21 *                 < file  > wm2000_anc.bin
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/moduleparam.h>
  26#include <linux/kernel.h>
  27#include <linux/init.h>
  28#include <linux/firmware.h>
  29#include <linux/delay.h>
  30#include <linux/pm.h>
  31#include <linux/i2c.h>
  32#include <linux/regmap.h>
  33#include <linux/debugfs.h>
  34#include <linux/slab.h>
  35#include <sound/core.h>
  36#include <sound/pcm.h>
  37#include <sound/pcm_params.h>
  38#include <sound/soc.h>
  39#include <sound/initval.h>
  40#include <sound/tlv.h>
  41
  42#include <sound/wm2000.h>
  43
  44#include "wm2000.h"
  45
  46enum wm2000_anc_mode {
  47        ANC_ACTIVE = 0,
  48        ANC_BYPASS = 1,
  49        ANC_STANDBY = 2,
  50        ANC_OFF = 3,
  51};
  52
  53struct wm2000_priv {
  54        struct i2c_client *i2c;
  55        struct regmap *regmap;
  56
  57        enum wm2000_anc_mode anc_mode;
  58
  59        unsigned int anc_active:1;
  60        unsigned int anc_eng_ena:1;
  61        unsigned int spk_ena:1;
  62
  63        unsigned int mclk_div:1;
  64        unsigned int speech_clarity:1;
  65
  66        int anc_download_size;
  67        char *anc_download;
  68};
  69
  70static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
  71                        unsigned int value)
  72{
  73        struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
  74        return regmap_write(wm2000->regmap, reg, value);
  75}
  76
  77static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
  78{
  79        struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
  80        unsigned int val;
  81        int ret;
  82
  83        ret = regmap_read(wm2000->regmap, r, &val);
  84        if (ret < 0)
  85                return -1;
  86
  87        return val;
  88}
  89
  90static void wm2000_reset(struct wm2000_priv *wm2000)
  91{
  92        struct i2c_client *i2c = wm2000->i2c;
  93
  94        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
  95        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  96        wm2000_write(i2c, WM2000_REG_ID1, 0);
  97
  98        wm2000->anc_mode = ANC_OFF;
  99}
 100
 101static int wm2000_poll_bit(struct i2c_client *i2c,
 102                           unsigned int reg, u8 mask)
 103{
 104        int timeout = 4000;
 105        int val;
 106
 107        val = wm2000_read(i2c, reg);
 108
 109        while (!(val & mask) && --timeout) {
 110                msleep(1);
 111                val = wm2000_read(i2c, reg);
 112        }
 113
 114        if (timeout == 0)
 115                return 0;
 116        else
 117                return 1;
 118}
 119
 120static int wm2000_power_up(struct i2c_client *i2c, int analogue)
 121{
 122        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 123        int ret;
 124
 125        BUG_ON(wm2000->anc_mode != ANC_OFF);
 126
 127        dev_dbg(&i2c->dev, "Beginning power up\n");
 128
 129        if (!wm2000->mclk_div) {
 130                dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
 131                wm2000_write(i2c, WM2000_REG_SYS_CTL2,
 132                             WM2000_MCLK_DIV2_ENA_CLR);
 133        } else {
 134                dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
 135                wm2000_write(i2c, WM2000_REG_SYS_CTL2,
 136                             WM2000_MCLK_DIV2_ENA_SET);
 137        }
 138
 139        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
 140        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
 141
 142        /* Wait for ANC engine to become ready */
 143        if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
 144                             WM2000_ANC_ENG_IDLE)) {
 145                dev_err(&i2c->dev, "ANC engine failed to reset\n");
 146                return -ETIMEDOUT;
 147        }
 148
 149        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 150                             WM2000_STATUS_BOOT_COMPLETE)) {
 151                dev_err(&i2c->dev, "ANC engine failed to initialise\n");
 152                return -ETIMEDOUT;
 153        }
 154
 155        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
 156
 157        /* Open code download of the data since it is the only bulk
 158         * write we do. */
 159        dev_dbg(&i2c->dev, "Downloading %d bytes\n",
 160                wm2000->anc_download_size - 2);
 161
 162        ret = i2c_master_send(i2c, wm2000->anc_download,
 163                              wm2000->anc_download_size);
 164        if (ret < 0) {
 165                dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
 166                return ret;
 167        }
 168        if (ret != wm2000->anc_download_size) {
 169                dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
 170                        ret, wm2000->anc_download_size);
 171                return -EIO;
 172        }
 173
 174        dev_dbg(&i2c->dev, "Download complete\n");
 175
 176        if (analogue) {
 177                wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
 178
 179                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 180                             WM2000_MODE_ANA_SEQ_INCLUDE |
 181                             WM2000_MODE_MOUSE_ENABLE |
 182                             WM2000_MODE_THERMAL_ENABLE);
 183        } else {
 184                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 185                             WM2000_MODE_MOUSE_ENABLE |
 186                             WM2000_MODE_THERMAL_ENABLE);
 187        }
 188
 189        ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
 190        if (wm2000->speech_clarity)
 191                ret &= ~WM2000_SPEECH_CLARITY;
 192        else
 193                ret |= WM2000_SPEECH_CLARITY;
 194        wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
 195
 196        wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
 197        wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
 198
 199        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
 200
 201        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 202                             WM2000_STATUS_MOUSE_ACTIVE)) {
 203                dev_err(&i2c->dev, "Timed out waiting for device\n");
 204                return -ETIMEDOUT;
 205        }
 206
 207        dev_dbg(&i2c->dev, "ANC active\n");
 208        if (analogue)
 209                dev_dbg(&i2c->dev, "Analogue active\n");
 210        wm2000->anc_mode = ANC_ACTIVE;
 211
 212        return 0;
 213}
 214
 215static int wm2000_power_down(struct i2c_client *i2c, int analogue)
 216{
 217        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 218
 219        if (analogue) {
 220                wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
 221                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 222                             WM2000_MODE_ANA_SEQ_INCLUDE |
 223                             WM2000_MODE_POWER_DOWN);
 224        } else {
 225                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 226                             WM2000_MODE_POWER_DOWN);
 227        }
 228
 229        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 230                             WM2000_STATUS_POWER_DOWN_COMPLETE)) {
 231                dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
 232                return -ETIMEDOUT;
 233        }
 234
 235        if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
 236                             WM2000_ANC_ENG_IDLE)) {
 237                dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
 238                return -ETIMEDOUT;
 239        }
 240
 241        dev_dbg(&i2c->dev, "powered off\n");
 242        wm2000->anc_mode = ANC_OFF;
 243
 244        return 0;
 245}
 246
 247static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
 248{
 249        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 250
 251        BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
 252
 253        if (analogue) {
 254                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 255                             WM2000_MODE_ANA_SEQ_INCLUDE |
 256                             WM2000_MODE_THERMAL_ENABLE |
 257                             WM2000_MODE_BYPASS_ENTRY);
 258        } else {
 259                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 260                             WM2000_MODE_THERMAL_ENABLE |
 261                             WM2000_MODE_BYPASS_ENTRY);
 262        }
 263
 264        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 265                             WM2000_STATUS_ANC_DISABLED)) {
 266                dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
 267                return -ETIMEDOUT;
 268        }
 269
 270        if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
 271                             WM2000_ANC_ENG_IDLE)) {
 272                dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
 273                return -ETIMEDOUT;
 274        }
 275
 276        wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
 277        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
 278
 279        wm2000->anc_mode = ANC_BYPASS;
 280        dev_dbg(&i2c->dev, "bypass enabled\n");
 281
 282        return 0;
 283}
 284
 285static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
 286{
 287        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 288
 289        BUG_ON(wm2000->anc_mode != ANC_BYPASS);
 290        
 291        wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
 292
 293        if (analogue) {
 294                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 295                             WM2000_MODE_ANA_SEQ_INCLUDE |
 296                             WM2000_MODE_MOUSE_ENABLE |
 297                             WM2000_MODE_THERMAL_ENABLE);
 298        } else {
 299                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 300                             WM2000_MODE_MOUSE_ENABLE |
 301                             WM2000_MODE_THERMAL_ENABLE);
 302        }
 303
 304        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
 305        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
 306
 307        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 308                             WM2000_STATUS_MOUSE_ACTIVE)) {
 309                dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
 310                return -ETIMEDOUT;
 311        }
 312
 313        wm2000->anc_mode = ANC_ACTIVE;
 314        dev_dbg(&i2c->dev, "MOUSE active\n");
 315
 316        return 0;
 317}
 318
 319static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
 320{
 321        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 322
 323        BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
 324
 325        if (analogue) {
 326                wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
 327
 328                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 329                             WM2000_MODE_ANA_SEQ_INCLUDE |
 330                             WM2000_MODE_THERMAL_ENABLE |
 331                             WM2000_MODE_STANDBY_ENTRY);
 332        } else {
 333                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 334                             WM2000_MODE_THERMAL_ENABLE |
 335                             WM2000_MODE_STANDBY_ENTRY);
 336        }
 337
 338        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 339                             WM2000_STATUS_ANC_DISABLED)) {
 340                dev_err(&i2c->dev,
 341                        "Timed out waiting for ANC disable after 1ms\n");
 342                return -ETIMEDOUT;
 343        }
 344
 345        if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
 346                dev_err(&i2c->dev,
 347                        "Timed out waiting for standby\n");
 348                return -ETIMEDOUT;
 349        }
 350
 351        wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
 352        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
 353
 354        wm2000->anc_mode = ANC_STANDBY;
 355        dev_dbg(&i2c->dev, "standby\n");
 356        if (analogue)
 357                dev_dbg(&i2c->dev, "Analogue disabled\n");
 358
 359        return 0;
 360}
 361
 362static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
 363{
 364        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 365
 366        BUG_ON(wm2000->anc_mode != ANC_STANDBY);
 367
 368        wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
 369
 370        if (analogue) {
 371                wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
 372
 373                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 374                             WM2000_MODE_ANA_SEQ_INCLUDE |
 375                             WM2000_MODE_THERMAL_ENABLE |
 376                             WM2000_MODE_MOUSE_ENABLE);
 377        } else {
 378                wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
 379                             WM2000_MODE_THERMAL_ENABLE |
 380                             WM2000_MODE_MOUSE_ENABLE);
 381        }
 382
 383        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
 384        wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
 385
 386        if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
 387                             WM2000_STATUS_MOUSE_ACTIVE)) {
 388                dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
 389                return -ETIMEDOUT;
 390        }
 391
 392        wm2000->anc_mode = ANC_ACTIVE;
 393        dev_dbg(&i2c->dev, "MOUSE active\n");
 394        if (analogue)
 395                dev_dbg(&i2c->dev, "Analogue enabled\n");
 396
 397        return 0;
 398}
 399
 400typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
 401
 402static struct {
 403        enum wm2000_anc_mode source;
 404        enum wm2000_anc_mode dest;
 405        int analogue;
 406        wm2000_mode_fn step[2];
 407} anc_transitions[] = {
 408        {
 409                .source = ANC_OFF,
 410                .dest = ANC_ACTIVE,
 411                .analogue = 1,
 412                .step = {
 413                        wm2000_power_up,
 414                },
 415        },
 416        {
 417                .source = ANC_OFF,
 418                .dest = ANC_STANDBY,
 419                .step = {
 420                        wm2000_power_up,
 421                        wm2000_enter_standby,
 422                },
 423        },
 424        {
 425                .source = ANC_OFF,
 426                .dest = ANC_BYPASS,
 427                .analogue = 1,
 428                .step = {
 429                        wm2000_power_up,
 430                        wm2000_enter_bypass,
 431                },
 432        },
 433        {
 434                .source = ANC_ACTIVE,
 435                .dest = ANC_BYPASS,
 436                .analogue = 1,
 437                .step = {
 438                        wm2000_enter_bypass,
 439                },
 440        },
 441        {
 442                .source = ANC_ACTIVE,
 443                .dest = ANC_STANDBY,
 444                .analogue = 1,
 445                .step = {
 446                        wm2000_enter_standby,
 447                },
 448        },
 449        {
 450                .source = ANC_ACTIVE,
 451                .dest = ANC_OFF,
 452                .analogue = 1,
 453                .step = {
 454                        wm2000_power_down,
 455                },
 456        },
 457        {
 458                .source = ANC_BYPASS,
 459                .dest = ANC_ACTIVE,
 460                .analogue = 1,
 461                .step = {
 462                        wm2000_exit_bypass,
 463                },
 464        },
 465        {
 466                .source = ANC_BYPASS,
 467                .dest = ANC_STANDBY,
 468                .analogue = 1,
 469                .step = {
 470                        wm2000_exit_bypass,
 471                        wm2000_enter_standby,
 472                },
 473        },
 474        {
 475                .source = ANC_BYPASS,
 476                .dest = ANC_OFF,
 477                .step = {
 478                        wm2000_exit_bypass,
 479                        wm2000_power_down,
 480                },
 481        },
 482        {
 483                .source = ANC_STANDBY,
 484                .dest = ANC_ACTIVE,
 485                .analogue = 1,
 486                .step = {
 487                        wm2000_exit_standby,
 488                },
 489        },
 490        {
 491                .source = ANC_STANDBY,
 492                .dest = ANC_BYPASS,
 493                .analogue = 1,
 494                .step = {
 495                        wm2000_exit_standby,
 496                        wm2000_enter_bypass,
 497                },
 498        },
 499        {
 500                .source = ANC_STANDBY,
 501                .dest = ANC_OFF,
 502                .step = {
 503                        wm2000_exit_standby,
 504                        wm2000_power_down,
 505                },
 506        },
 507};
 508
 509static int wm2000_anc_transition(struct wm2000_priv *wm2000,
 510                                 enum wm2000_anc_mode mode)
 511{
 512        struct i2c_client *i2c = wm2000->i2c;
 513        int i, j;
 514        int ret;
 515
 516        if (wm2000->anc_mode == mode)
 517                return 0;
 518
 519        for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
 520                if (anc_transitions[i].source == wm2000->anc_mode &&
 521                    anc_transitions[i].dest == mode)
 522                        break;
 523        if (i == ARRAY_SIZE(anc_transitions)) {
 524                dev_err(&i2c->dev, "No transition for %d->%d\n",
 525                        wm2000->anc_mode, mode);
 526                return -EINVAL;
 527        }
 528
 529        for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
 530                if (!anc_transitions[i].step[j])
 531                        break;
 532                ret = anc_transitions[i].step[j](i2c,
 533                                                 anc_transitions[i].analogue);
 534                if (ret != 0)
 535                        return ret;
 536        }
 537
 538        return 0;
 539}
 540
 541static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
 542{
 543        struct i2c_client *i2c = wm2000->i2c;
 544        enum wm2000_anc_mode mode;
 545
 546        if (wm2000->anc_eng_ena && wm2000->spk_ena)
 547                if (wm2000->anc_active)
 548                        mode = ANC_ACTIVE;
 549                else
 550                        mode = ANC_BYPASS;
 551        else
 552                mode = ANC_STANDBY;
 553
 554        dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
 555                mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
 556                wm2000->anc_active);
 557
 558        return wm2000_anc_transition(wm2000, mode);
 559}
 560
 561static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
 562                               struct snd_ctl_elem_value *ucontrol)
 563{
 564        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 565        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 566
 567        ucontrol->value.enumerated.item[0] = wm2000->anc_active;
 568
 569        return 0;
 570}
 571
 572static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
 573                               struct snd_ctl_elem_value *ucontrol)
 574{
 575        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 576        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 577        int anc_active = ucontrol->value.enumerated.item[0];
 578
 579        if (anc_active > 1)
 580                return -EINVAL;
 581
 582        wm2000->anc_active = anc_active;
 583
 584        return wm2000_anc_set_mode(wm2000);
 585}
 586
 587static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
 588                              struct snd_ctl_elem_value *ucontrol)
 589{
 590        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 591        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 592
 593        ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
 594
 595        return 0;
 596}
 597
 598static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
 599                              struct snd_ctl_elem_value *ucontrol)
 600{
 601        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 602        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 603        int val = ucontrol->value.enumerated.item[0];
 604
 605        if (val > 1)
 606                return -EINVAL;
 607
 608        wm2000->spk_ena = val;
 609
 610        return wm2000_anc_set_mode(wm2000);
 611}
 612
 613static const struct snd_kcontrol_new wm2000_controls[] = {
 614        SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
 615                            wm2000_anc_mode_get,
 616                            wm2000_anc_mode_put),
 617        SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
 618                            wm2000_speaker_get,
 619                            wm2000_speaker_put),
 620};
 621
 622static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
 623                                  struct snd_kcontrol *kcontrol, int event)
 624{
 625        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
 626        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 627
 628        if (SND_SOC_DAPM_EVENT_ON(event))
 629                wm2000->anc_eng_ena = 1;
 630
 631        if (SND_SOC_DAPM_EVENT_OFF(event))
 632                wm2000->anc_eng_ena = 0;
 633
 634        return wm2000_anc_set_mode(wm2000);
 635}
 636
 637static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
 638/* Externally visible pins */
 639SND_SOC_DAPM_OUTPUT("SPKN"),
 640SND_SOC_DAPM_OUTPUT("SPKP"),
 641
 642SND_SOC_DAPM_INPUT("LINN"),
 643SND_SOC_DAPM_INPUT("LINP"),
 644
 645SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
 646                   wm2000_anc_power_event,
 647                   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
 648};
 649
 650/* Target, Path, Source */
 651static const struct snd_soc_dapm_route wm2000_audio_map[] = {
 652        { "SPKN", NULL, "ANC Engine" },
 653        { "SPKP", NULL, "ANC Engine" },
 654        { "ANC Engine", NULL, "LINN" },
 655        { "ANC Engine", NULL, "LINP" },
 656};
 657
 658#ifdef CONFIG_PM
 659static int wm2000_suspend(struct snd_soc_codec *codec)
 660{
 661        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 662
 663        return wm2000_anc_transition(wm2000, ANC_OFF);
 664}
 665
 666static int wm2000_resume(struct snd_soc_codec *codec)
 667{
 668        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 669
 670        return wm2000_anc_set_mode(wm2000);
 671}
 672#else
 673#define wm2000_suspend NULL
 674#define wm2000_resume NULL
 675#endif
 676
 677static const struct regmap_config wm2000_regmap = {
 678        .reg_bits = 8,
 679        .val_bits = 8,
 680};
 681
 682static int wm2000_probe(struct snd_soc_codec *codec)
 683{
 684        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 685
 686        /* This will trigger a transition to standby mode by default */
 687        wm2000_anc_set_mode(wm2000);
 688
 689        return 0;
 690}
 691
 692static int wm2000_remove(struct snd_soc_codec *codec)
 693{
 694        struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
 695
 696        return wm2000_anc_transition(wm2000, ANC_OFF);
 697}
 698
 699static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
 700        .probe = wm2000_probe,
 701        .remove = wm2000_remove,
 702        .suspend = wm2000_suspend,
 703        .resume = wm2000_resume,
 704
 705        .dapm_widgets = wm2000_dapm_widgets,
 706        .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
 707        .dapm_routes = wm2000_audio_map,
 708        .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
 709        .controls = wm2000_controls,
 710        .num_controls = ARRAY_SIZE(wm2000_controls),
 711};
 712
 713static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
 714                                      const struct i2c_device_id *i2c_id)
 715{
 716        struct wm2000_priv *wm2000;
 717        struct wm2000_platform_data *pdata;
 718        const char *filename;
 719        const struct firmware *fw = NULL;
 720        int ret;
 721        int reg;
 722        u16 id;
 723
 724        wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
 725                              GFP_KERNEL);
 726        if (wm2000 == NULL) {
 727                dev_err(&i2c->dev, "Unable to allocate private data\n");
 728                return -ENOMEM;
 729        }
 730
 731        dev_set_drvdata(&i2c->dev, wm2000);
 732
 733        wm2000->regmap = regmap_init_i2c(i2c, &wm2000_regmap);
 734        if (IS_ERR(wm2000->regmap)) {
 735                ret = PTR_ERR(wm2000->regmap);
 736                dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
 737                        ret);
 738                goto out;
 739        }
 740
 741        /* Verify that this is a WM2000 */
 742        reg = wm2000_read(i2c, WM2000_REG_ID1);
 743        id = reg << 8;
 744        reg = wm2000_read(i2c, WM2000_REG_ID2);
 745        id |= reg & 0xff;
 746
 747        if (id != 0x2000) {
 748                dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
 749                ret = -ENODEV;
 750                goto out_regmap_exit;
 751        }
 752
 753        reg = wm2000_read(i2c, WM2000_REG_REVISON);
 754        dev_info(&i2c->dev, "revision %c\n", reg + 'A');
 755
 756        filename = "wm2000_anc.bin";
 757        pdata = dev_get_platdata(&i2c->dev);
 758        if (pdata) {
 759                wm2000->mclk_div = pdata->mclkdiv2;
 760                wm2000->speech_clarity = !pdata->speech_enh_disable;
 761
 762                if (pdata->download_file)
 763                        filename = pdata->download_file;
 764        }
 765
 766        ret = request_firmware(&fw, filename, &i2c->dev);
 767        if (ret != 0) {
 768                dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
 769                goto out_regmap_exit;
 770        }
 771
 772        /* Pre-cook the concatenation of the register address onto the image */
 773        wm2000->anc_download_size = fw->size + 2;
 774        wm2000->anc_download = devm_kzalloc(&i2c->dev,
 775                                            wm2000->anc_download_size,
 776                                            GFP_KERNEL);
 777        if (wm2000->anc_download == NULL) {
 778                dev_err(&i2c->dev, "Out of memory\n");
 779                ret = -ENOMEM;
 780                goto out_regmap_exit;
 781        }
 782
 783        wm2000->anc_download[0] = 0x80;
 784        wm2000->anc_download[1] = 0x00;
 785        memcpy(wm2000->anc_download + 2, fw->data, fw->size);
 786
 787        wm2000->anc_eng_ena = 1;
 788        wm2000->anc_active = 1;
 789        wm2000->spk_ena = 1;
 790        wm2000->i2c = i2c;
 791
 792        wm2000_reset(wm2000);
 793
 794        ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
 795        if (!ret)
 796                goto out;
 797
 798out_regmap_exit:
 799        regmap_exit(wm2000->regmap);
 800out:
 801        release_firmware(fw);
 802        return ret;
 803}
 804
 805static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
 806{
 807        struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
 808
 809        snd_soc_unregister_codec(&i2c->dev);
 810        regmap_exit(wm2000->regmap);
 811
 812        return 0;
 813}
 814
 815static const struct i2c_device_id wm2000_i2c_id[] = {
 816        { "wm2000", 0 },
 817        { }
 818};
 819MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
 820
 821static struct i2c_driver wm2000_i2c_driver = {
 822        .driver = {
 823                .name = "wm2000",
 824                .owner = THIS_MODULE,
 825        },
 826        .probe = wm2000_i2c_probe,
 827        .remove = __devexit_p(wm2000_i2c_remove),
 828        .id_table = wm2000_i2c_id,
 829};
 830
 831static int __init wm2000_init(void)
 832{
 833        return i2c_add_driver(&wm2000_i2c_driver);
 834}
 835module_init(wm2000_init);
 836
 837static void __exit wm2000_exit(void)
 838{
 839        i2c_del_driver(&wm2000_i2c_driver);
 840}
 841module_exit(wm2000_exit);
 842
 843MODULE_DESCRIPTION("ASoC WM2000 driver");
 844MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
 845MODULE_LICENSE("GPL");
 846