Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * wm2000.c  --  WM2000 ALSA Soc Audio driver
0004  *
0005  * Copyright 2008-2011 Wolfson Microelectronics PLC.
0006  *
0007  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008  *
0009  * The download image for the WM2000 will be requested as
0010  * 'wm2000_anc.bin' by default (overridable via platform data) at
0011  * runtime and is expected to be in flat binary format.  This is
0012  * generated by Wolfson configuration tools and includes
0013  * system-specific calibration information.  If supplied as a
0014  * sequence of ASCII-encoded hexidecimal bytes this can be converted
0015  * into a flat binary with a command such as this on the command line:
0016  *
0017  * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
0018  *                 < file  > wm2000_anc.bin
0019  */
0020 
0021 #include <linux/module.h>
0022 #include <linux/moduleparam.h>
0023 #include <linux/kernel.h>
0024 #include <linux/init.h>
0025 #include <linux/firmware.h>
0026 #include <linux/clk.h>
0027 #include <linux/delay.h>
0028 #include <linux/pm.h>
0029 #include <linux/i2c.h>
0030 #include <linux/regmap.h>
0031 #include <linux/debugfs.h>
0032 #include <linux/regulator/consumer.h>
0033 #include <linux/slab.h>
0034 #include <sound/core.h>
0035 #include <sound/pcm.h>
0036 #include <sound/pcm_params.h>
0037 #include <sound/soc.h>
0038 #include <sound/initval.h>
0039 #include <sound/tlv.h>
0040 
0041 #include <sound/wm2000.h>
0042 
0043 #include "wm2000.h"
0044 
0045 #define WM2000_NUM_SUPPLIES 3
0046 
0047 static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
0048     "SPKVDD",
0049     "DBVDD",
0050     "DCVDD",
0051 };
0052 
0053 enum wm2000_anc_mode {
0054     ANC_ACTIVE = 0,
0055     ANC_BYPASS = 1,
0056     ANC_STANDBY = 2,
0057     ANC_OFF = 3,
0058 };
0059 
0060 struct wm2000_priv {
0061     struct i2c_client *i2c;
0062     struct regmap *regmap;
0063     struct clk *mclk;
0064 
0065     struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
0066 
0067     enum wm2000_anc_mode anc_mode;
0068 
0069     unsigned int anc_active:1;
0070     unsigned int anc_eng_ena:1;
0071     unsigned int spk_ena:1;
0072 
0073     unsigned int speech_clarity:1;
0074 
0075     int anc_download_size;
0076     char *anc_download;
0077 
0078     struct mutex lock;
0079 };
0080 
0081 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
0082             unsigned int value)
0083 {
0084     struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
0085     return regmap_write(wm2000->regmap, reg, value);
0086 }
0087 
0088 static void wm2000_reset(struct wm2000_priv *wm2000)
0089 {
0090     struct i2c_client *i2c = wm2000->i2c;
0091 
0092     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
0093     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
0094     wm2000_write(i2c, WM2000_REG_ID1, 0);
0095 
0096     wm2000->anc_mode = ANC_OFF;
0097 }
0098 
0099 static int wm2000_poll_bit(struct i2c_client *i2c,
0100                unsigned int reg, u8 mask)
0101 {
0102     struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
0103     int timeout = 4000;
0104     unsigned int val;
0105 
0106     regmap_read(wm2000->regmap, reg, &val);
0107 
0108     while (!(val & mask) && --timeout) {
0109         msleep(1);
0110         regmap_read(wm2000->regmap, reg, &val);
0111     }
0112 
0113     if (timeout == 0)
0114         return 0;
0115     else
0116         return 1;
0117 }
0118 
0119 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
0120 {
0121     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0122     unsigned long rate;
0123     unsigned int val;
0124     int ret;
0125 
0126     if (WARN_ON(wm2000->anc_mode != ANC_OFF))
0127         return -EINVAL;
0128 
0129     dev_dbg(&i2c->dev, "Beginning power up\n");
0130 
0131     ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0132     if (ret != 0) {
0133         dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
0134         return ret;
0135     }
0136 
0137     rate = clk_get_rate(wm2000->mclk);
0138     if (rate <= 13500000) {
0139         dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
0140         wm2000_write(i2c, WM2000_REG_SYS_CTL2,
0141                  WM2000_MCLK_DIV2_ENA_CLR);
0142     } else {
0143         dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
0144         wm2000_write(i2c, WM2000_REG_SYS_CTL2,
0145                  WM2000_MCLK_DIV2_ENA_SET);
0146     }
0147 
0148     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
0149     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
0150 
0151     /* Wait for ANC engine to become ready */
0152     if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
0153                  WM2000_ANC_ENG_IDLE)) {
0154         dev_err(&i2c->dev, "ANC engine failed to reset\n");
0155         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0156         return -ETIMEDOUT;
0157     }
0158 
0159     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0160                  WM2000_STATUS_BOOT_COMPLETE)) {
0161         dev_err(&i2c->dev, "ANC engine failed to initialise\n");
0162         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0163         return -ETIMEDOUT;
0164     }
0165 
0166     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
0167 
0168     /* Open code download of the data since it is the only bulk
0169      * write we do. */
0170     dev_dbg(&i2c->dev, "Downloading %d bytes\n",
0171         wm2000->anc_download_size - 2);
0172 
0173     ret = i2c_master_send(i2c, wm2000->anc_download,
0174                   wm2000->anc_download_size);
0175     if (ret < 0) {
0176         dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
0177         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0178         return ret;
0179     }
0180     if (ret != wm2000->anc_download_size) {
0181         dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
0182             ret, wm2000->anc_download_size);
0183         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0184         return -EIO;
0185     }
0186 
0187     dev_dbg(&i2c->dev, "Download complete\n");
0188 
0189     if (analogue) {
0190         wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
0191 
0192         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0193                  WM2000_MODE_ANA_SEQ_INCLUDE |
0194                  WM2000_MODE_MOUSE_ENABLE |
0195                  WM2000_MODE_THERMAL_ENABLE);
0196     } else {
0197         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0198                  WM2000_MODE_MOUSE_ENABLE |
0199                  WM2000_MODE_THERMAL_ENABLE);
0200     }
0201 
0202     ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val);
0203     if (ret != 0) {
0204         dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret);
0205         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0206         return ret;
0207     }
0208     if (wm2000->speech_clarity)
0209         val |= WM2000_SPEECH_CLARITY;
0210     else
0211         val &= ~WM2000_SPEECH_CLARITY;
0212     wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val);
0213 
0214     wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
0215     wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
0216 
0217     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
0218 
0219     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0220                  WM2000_STATUS_MOUSE_ACTIVE)) {
0221         dev_err(&i2c->dev, "Timed out waiting for device\n");
0222         regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0223         return -ETIMEDOUT;
0224     }
0225 
0226     dev_dbg(&i2c->dev, "ANC active\n");
0227     if (analogue)
0228         dev_dbg(&i2c->dev, "Analogue active\n");
0229     wm2000->anc_mode = ANC_ACTIVE;
0230 
0231     return 0;
0232 }
0233 
0234 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
0235 {
0236     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0237 
0238     if (analogue) {
0239         wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
0240         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0241                  WM2000_MODE_ANA_SEQ_INCLUDE |
0242                  WM2000_MODE_POWER_DOWN);
0243     } else {
0244         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0245                  WM2000_MODE_POWER_DOWN);
0246     }
0247 
0248     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0249                  WM2000_STATUS_POWER_DOWN_COMPLETE)) {
0250         dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
0251         return -ETIMEDOUT;
0252     }
0253 
0254     if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
0255                  WM2000_ANC_ENG_IDLE)) {
0256         dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
0257         return -ETIMEDOUT;
0258     }
0259 
0260     regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0261 
0262     dev_dbg(&i2c->dev, "powered off\n");
0263     wm2000->anc_mode = ANC_OFF;
0264 
0265     return 0;
0266 }
0267 
0268 static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
0269 {
0270     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0271 
0272     if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
0273         return -EINVAL;
0274 
0275     if (analogue) {
0276         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0277                  WM2000_MODE_ANA_SEQ_INCLUDE |
0278                  WM2000_MODE_THERMAL_ENABLE |
0279                  WM2000_MODE_BYPASS_ENTRY);
0280     } else {
0281         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0282                  WM2000_MODE_THERMAL_ENABLE |
0283                  WM2000_MODE_BYPASS_ENTRY);
0284     }
0285 
0286     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0287                  WM2000_STATUS_ANC_DISABLED)) {
0288         dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
0289         return -ETIMEDOUT;
0290     }
0291 
0292     if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
0293                  WM2000_ANC_ENG_IDLE)) {
0294         dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
0295         return -ETIMEDOUT;
0296     }
0297 
0298     wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
0299     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
0300 
0301     wm2000->anc_mode = ANC_BYPASS;
0302     dev_dbg(&i2c->dev, "bypass enabled\n");
0303 
0304     return 0;
0305 }
0306 
0307 static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
0308 {
0309     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0310 
0311     if (WARN_ON(wm2000->anc_mode != ANC_BYPASS))
0312         return -EINVAL;
0313     
0314     wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
0315 
0316     if (analogue) {
0317         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0318                  WM2000_MODE_ANA_SEQ_INCLUDE |
0319                  WM2000_MODE_MOUSE_ENABLE |
0320                  WM2000_MODE_THERMAL_ENABLE);
0321     } else {
0322         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0323                  WM2000_MODE_MOUSE_ENABLE |
0324                  WM2000_MODE_THERMAL_ENABLE);
0325     }
0326 
0327     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
0328     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
0329 
0330     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0331                  WM2000_STATUS_MOUSE_ACTIVE)) {
0332         dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
0333         return -ETIMEDOUT;
0334     }
0335 
0336     wm2000->anc_mode = ANC_ACTIVE;
0337     dev_dbg(&i2c->dev, "MOUSE active\n");
0338 
0339     return 0;
0340 }
0341 
0342 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
0343 {
0344     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0345 
0346     if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
0347         return -EINVAL;
0348 
0349     if (analogue) {
0350         wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
0351 
0352         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0353                  WM2000_MODE_ANA_SEQ_INCLUDE |
0354                  WM2000_MODE_THERMAL_ENABLE |
0355                  WM2000_MODE_STANDBY_ENTRY);
0356     } else {
0357         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0358                  WM2000_MODE_THERMAL_ENABLE |
0359                  WM2000_MODE_STANDBY_ENTRY);
0360     }
0361 
0362     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0363                  WM2000_STATUS_ANC_DISABLED)) {
0364         dev_err(&i2c->dev,
0365             "Timed out waiting for ANC disable after 1ms\n");
0366         return -ETIMEDOUT;
0367     }
0368 
0369     if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
0370         dev_err(&i2c->dev,
0371             "Timed out waiting for standby\n");
0372         return -ETIMEDOUT;
0373     }
0374 
0375     wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
0376     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
0377 
0378     wm2000->anc_mode = ANC_STANDBY;
0379     dev_dbg(&i2c->dev, "standby\n");
0380     if (analogue)
0381         dev_dbg(&i2c->dev, "Analogue disabled\n");
0382 
0383     return 0;
0384 }
0385 
0386 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
0387 {
0388     struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
0389 
0390     if (WARN_ON(wm2000->anc_mode != ANC_STANDBY))
0391         return -EINVAL;
0392 
0393     wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
0394 
0395     if (analogue) {
0396         wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
0397 
0398         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0399                  WM2000_MODE_ANA_SEQ_INCLUDE |
0400                  WM2000_MODE_THERMAL_ENABLE |
0401                  WM2000_MODE_MOUSE_ENABLE);
0402     } else {
0403         wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
0404                  WM2000_MODE_THERMAL_ENABLE |
0405                  WM2000_MODE_MOUSE_ENABLE);
0406     }
0407 
0408     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
0409     wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
0410 
0411     if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
0412                  WM2000_STATUS_MOUSE_ACTIVE)) {
0413         dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
0414         return -ETIMEDOUT;
0415     }
0416 
0417     wm2000->anc_mode = ANC_ACTIVE;
0418     dev_dbg(&i2c->dev, "MOUSE active\n");
0419     if (analogue)
0420         dev_dbg(&i2c->dev, "Analogue enabled\n");
0421 
0422     return 0;
0423 }
0424 
0425 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
0426 
0427 static struct {
0428     enum wm2000_anc_mode source;
0429     enum wm2000_anc_mode dest;
0430     int analogue;
0431     wm2000_mode_fn step[2];
0432 } anc_transitions[] = {
0433     {
0434         .source = ANC_OFF,
0435         .dest = ANC_ACTIVE,
0436         .analogue = 1,
0437         .step = {
0438             wm2000_power_up,
0439         },
0440     },
0441     {
0442         .source = ANC_OFF,
0443         .dest = ANC_STANDBY,
0444         .step = {
0445             wm2000_power_up,
0446             wm2000_enter_standby,
0447         },
0448     },
0449     {
0450         .source = ANC_OFF,
0451         .dest = ANC_BYPASS,
0452         .analogue = 1,
0453         .step = {
0454             wm2000_power_up,
0455             wm2000_enter_bypass,
0456         },
0457     },
0458     {
0459         .source = ANC_ACTIVE,
0460         .dest = ANC_BYPASS,
0461         .analogue = 1,
0462         .step = {
0463             wm2000_enter_bypass,
0464         },
0465     },
0466     {
0467         .source = ANC_ACTIVE,
0468         .dest = ANC_STANDBY,
0469         .analogue = 1,
0470         .step = {
0471             wm2000_enter_standby,
0472         },
0473     },
0474     {
0475         .source = ANC_ACTIVE,
0476         .dest = ANC_OFF,
0477         .analogue = 1,
0478         .step = {
0479             wm2000_power_down,
0480         },
0481     },
0482     {
0483         .source = ANC_BYPASS,
0484         .dest = ANC_ACTIVE,
0485         .analogue = 1,
0486         .step = {
0487             wm2000_exit_bypass,
0488         },
0489     },
0490     {
0491         .source = ANC_BYPASS,
0492         .dest = ANC_STANDBY,
0493         .analogue = 1,
0494         .step = {
0495             wm2000_exit_bypass,
0496             wm2000_enter_standby,
0497         },
0498     },
0499     {
0500         .source = ANC_BYPASS,
0501         .dest = ANC_OFF,
0502         .step = {
0503             wm2000_exit_bypass,
0504             wm2000_power_down,
0505         },
0506     },
0507     {
0508         .source = ANC_STANDBY,
0509         .dest = ANC_ACTIVE,
0510         .analogue = 1,
0511         .step = {
0512             wm2000_exit_standby,
0513         },
0514     },
0515     {
0516         .source = ANC_STANDBY,
0517         .dest = ANC_BYPASS,
0518         .analogue = 1,
0519         .step = {
0520             wm2000_exit_standby,
0521             wm2000_enter_bypass,
0522         },
0523     },
0524     {
0525         .source = ANC_STANDBY,
0526         .dest = ANC_OFF,
0527         .step = {
0528             wm2000_exit_standby,
0529             wm2000_power_down,
0530         },
0531     },
0532 };
0533 
0534 static int wm2000_anc_transition(struct wm2000_priv *wm2000,
0535                  enum wm2000_anc_mode mode)
0536 {
0537     struct i2c_client *i2c = wm2000->i2c;
0538     int i, j;
0539     int ret = 0;
0540 
0541     if (wm2000->anc_mode == mode)
0542         return 0;
0543 
0544     for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
0545         if (anc_transitions[i].source == wm2000->anc_mode &&
0546             anc_transitions[i].dest == mode)
0547             break;
0548     if (i == ARRAY_SIZE(anc_transitions)) {
0549         dev_err(&i2c->dev, "No transition for %d->%d\n",
0550             wm2000->anc_mode, mode);
0551         return -EINVAL;
0552     }
0553 
0554     /* Maintain clock while active */
0555     if (anc_transitions[i].source == ANC_OFF) {
0556         ret = clk_prepare_enable(wm2000->mclk);
0557         if (ret != 0) {
0558             dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret);
0559             return ret;
0560         }
0561     }
0562 
0563     for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
0564         if (!anc_transitions[i].step[j])
0565             break;
0566         ret = anc_transitions[i].step[j](i2c,
0567                          anc_transitions[i].analogue);
0568         if (ret != 0)
0569             break;
0570     }
0571 
0572     if (anc_transitions[i].dest == ANC_OFF)
0573         clk_disable_unprepare(wm2000->mclk);
0574 
0575     return ret;
0576 }
0577 
0578 static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
0579 {
0580     struct i2c_client *i2c = wm2000->i2c;
0581     enum wm2000_anc_mode mode;
0582 
0583     if (wm2000->anc_eng_ena && wm2000->spk_ena)
0584         if (wm2000->anc_active)
0585             mode = ANC_ACTIVE;
0586         else
0587             mode = ANC_BYPASS;
0588     else
0589         mode = ANC_STANDBY;
0590 
0591     dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
0592         mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
0593         wm2000->anc_active);
0594 
0595     return wm2000_anc_transition(wm2000, mode);
0596 }
0597 
0598 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
0599                    struct snd_ctl_elem_value *ucontrol)
0600 {
0601     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0602     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0603 
0604     ucontrol->value.integer.value[0] = wm2000->anc_active;
0605 
0606     return 0;
0607 }
0608 
0609 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
0610                    struct snd_ctl_elem_value *ucontrol)
0611 {
0612     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0613     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0614     unsigned int anc_active = ucontrol->value.integer.value[0];
0615     int ret;
0616 
0617     if (anc_active > 1)
0618         return -EINVAL;
0619 
0620     mutex_lock(&wm2000->lock);
0621 
0622     wm2000->anc_active = anc_active;
0623 
0624     ret = wm2000_anc_set_mode(wm2000);
0625 
0626     mutex_unlock(&wm2000->lock);
0627 
0628     return ret;
0629 }
0630 
0631 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
0632                   struct snd_ctl_elem_value *ucontrol)
0633 {
0634     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0635     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0636 
0637     ucontrol->value.integer.value[0] = wm2000->spk_ena;
0638 
0639     return 0;
0640 }
0641 
0642 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
0643                   struct snd_ctl_elem_value *ucontrol)
0644 {
0645     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0646     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0647     unsigned int val = ucontrol->value.integer.value[0];
0648     int ret;
0649 
0650     if (val > 1)
0651         return -EINVAL;
0652 
0653     mutex_lock(&wm2000->lock);
0654 
0655     wm2000->spk_ena = val;
0656 
0657     ret = wm2000_anc_set_mode(wm2000);
0658 
0659     mutex_unlock(&wm2000->lock);
0660 
0661     return ret;
0662 }
0663 
0664 static const struct snd_kcontrol_new wm2000_controls[] = {
0665     SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
0666     SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
0667                 wm2000_anc_mode_get,
0668                 wm2000_anc_mode_put),
0669     SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
0670                 wm2000_speaker_get,
0671                 wm2000_speaker_put),
0672 };
0673 
0674 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
0675                   struct snd_kcontrol *kcontrol, int event)
0676 {
0677     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0678     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0679     int ret;
0680 
0681     mutex_lock(&wm2000->lock);
0682 
0683     if (SND_SOC_DAPM_EVENT_ON(event))
0684         wm2000->anc_eng_ena = 1;
0685 
0686     if (SND_SOC_DAPM_EVENT_OFF(event))
0687         wm2000->anc_eng_ena = 0;
0688 
0689     ret = wm2000_anc_set_mode(wm2000);
0690 
0691     mutex_unlock(&wm2000->lock);
0692 
0693     return ret;
0694 }
0695 
0696 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
0697 /* Externally visible pins */
0698 SND_SOC_DAPM_OUTPUT("SPKN"),
0699 SND_SOC_DAPM_OUTPUT("SPKP"),
0700 
0701 SND_SOC_DAPM_INPUT("LINN"),
0702 SND_SOC_DAPM_INPUT("LINP"),
0703 
0704 SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
0705            wm2000_anc_power_event,
0706            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
0707 };
0708 
0709 /* Target, Path, Source */
0710 static const struct snd_soc_dapm_route wm2000_audio_map[] = {
0711     { "SPKN", NULL, "ANC Engine" },
0712     { "SPKP", NULL, "ANC Engine" },
0713     { "ANC Engine", NULL, "LINN" },
0714     { "ANC Engine", NULL, "LINP" },
0715 };
0716 
0717 #ifdef CONFIG_PM
0718 static int wm2000_suspend(struct snd_soc_component *component)
0719 {
0720     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0721 
0722     return wm2000_anc_transition(wm2000, ANC_OFF);
0723 }
0724 
0725 static int wm2000_resume(struct snd_soc_component *component)
0726 {
0727     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0728 
0729     return wm2000_anc_set_mode(wm2000);
0730 }
0731 #else
0732 #define wm2000_suspend NULL
0733 #define wm2000_resume NULL
0734 #endif
0735 
0736 static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
0737 {
0738     switch (reg) {
0739     case WM2000_REG_SYS_START:
0740     case WM2000_REG_ANC_GAIN_CTRL:
0741     case WM2000_REG_MSE_TH1:
0742     case WM2000_REG_MSE_TH2:
0743     case WM2000_REG_SPEECH_CLARITY:
0744     case WM2000_REG_SYS_WATCHDOG:
0745     case WM2000_REG_ANA_VMID_PD_TIME:
0746     case WM2000_REG_ANA_VMID_PU_TIME:
0747     case WM2000_REG_CAT_FLTR_INDX:
0748     case WM2000_REG_CAT_GAIN_0:
0749     case WM2000_REG_SYS_STATUS:
0750     case WM2000_REG_SYS_MODE_CNTRL:
0751     case WM2000_REG_SYS_START0:
0752     case WM2000_REG_SYS_START1:
0753     case WM2000_REG_ID1:
0754     case WM2000_REG_ID2:
0755     case WM2000_REG_REVISON:
0756     case WM2000_REG_SYS_CTL1:
0757     case WM2000_REG_SYS_CTL2:
0758     case WM2000_REG_ANC_STAT:
0759     case WM2000_REG_IF_CTL:
0760     case WM2000_REG_ANA_MIC_CTL:
0761     case WM2000_REG_SPK_CTL:
0762         return true;
0763     default:
0764         return false;
0765     }
0766 }
0767 
0768 static const struct regmap_config wm2000_regmap = {
0769     .reg_bits = 16,
0770     .val_bits = 8,
0771 
0772     .max_register = WM2000_REG_SPK_CTL,
0773     .readable_reg = wm2000_readable_reg,
0774 };
0775 
0776 static int wm2000_probe(struct snd_soc_component *component)
0777 {
0778     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0779 
0780     /* This will trigger a transition to standby mode by default */
0781     wm2000_anc_set_mode(wm2000);
0782 
0783     return 0;
0784 }
0785 
0786 static void wm2000_remove(struct snd_soc_component *component)
0787 {
0788     struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
0789 
0790     wm2000_anc_transition(wm2000, ANC_OFF);
0791 }
0792 
0793 static const struct snd_soc_component_driver soc_component_dev_wm2000 = {
0794     .probe          = wm2000_probe,
0795     .remove         = wm2000_remove,
0796     .suspend        = wm2000_suspend,
0797     .resume         = wm2000_resume,
0798     .controls       = wm2000_controls,
0799     .num_controls       = ARRAY_SIZE(wm2000_controls),
0800     .dapm_widgets       = wm2000_dapm_widgets,
0801     .num_dapm_widgets   = ARRAY_SIZE(wm2000_dapm_widgets),
0802     .dapm_routes        = wm2000_audio_map,
0803     .num_dapm_routes    = ARRAY_SIZE(wm2000_audio_map),
0804     .idle_bias_on       = 1,
0805     .use_pmdown_time    = 1,
0806 };
0807 
0808 static int wm2000_i2c_probe(struct i2c_client *i2c)
0809 {
0810     struct wm2000_priv *wm2000;
0811     struct wm2000_platform_data *pdata;
0812     const char *filename;
0813     const struct firmware *fw = NULL;
0814     int ret, i;
0815     unsigned int reg;
0816     u16 id;
0817 
0818     wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL);
0819     if (!wm2000)
0820         return -ENOMEM;
0821 
0822     mutex_init(&wm2000->lock);
0823 
0824     dev_set_drvdata(&i2c->dev, wm2000);
0825 
0826     wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
0827     if (IS_ERR(wm2000->regmap)) {
0828         ret = PTR_ERR(wm2000->regmap);
0829         dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
0830             ret);
0831         goto out;
0832     }
0833 
0834     for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
0835         wm2000->supplies[i].supply = wm2000_supplies[i];
0836 
0837     ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
0838                       wm2000->supplies);
0839     if (ret != 0) {
0840         dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
0841         return ret;
0842     }
0843 
0844     ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0845     if (ret != 0) {
0846         dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
0847         return ret;
0848     }
0849 
0850     /* Verify that this is a WM2000 */
0851     ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, &reg);
0852     if (ret != 0) {
0853         dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret);
0854         return ret;
0855     }
0856     id = reg << 8;
0857     ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, &reg);
0858     if (ret != 0) {
0859         dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret);
0860         return ret;
0861     }
0862     id |= reg & 0xff;
0863 
0864     if (id != 0x2000) {
0865         dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
0866         ret = -ENODEV;
0867         goto err_supplies;
0868     }
0869 
0870     ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, &reg);
0871     if (ret != 0) {
0872         dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret);
0873         return ret;
0874     }
0875     dev_info(&i2c->dev, "revision %c\n", reg + 'A');
0876 
0877     wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
0878     if (IS_ERR(wm2000->mclk)) {
0879         ret = PTR_ERR(wm2000->mclk);
0880         dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
0881         goto err_supplies;
0882     }
0883 
0884     filename = "wm2000_anc.bin";
0885     pdata = dev_get_platdata(&i2c->dev);
0886     if (pdata) {
0887         wm2000->speech_clarity = !pdata->speech_enh_disable;
0888 
0889         if (pdata->download_file)
0890             filename = pdata->download_file;
0891     }
0892 
0893     ret = request_firmware(&fw, filename, &i2c->dev);
0894     if (ret != 0) {
0895         dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
0896         goto err_supplies;
0897     }
0898 
0899     /* Pre-cook the concatenation of the register address onto the image */
0900     wm2000->anc_download_size = fw->size + 2;
0901     wm2000->anc_download = devm_kzalloc(&i2c->dev,
0902                         wm2000->anc_download_size,
0903                         GFP_KERNEL);
0904     if (wm2000->anc_download == NULL) {
0905         ret = -ENOMEM;
0906         goto err_supplies;
0907     }
0908 
0909     wm2000->anc_download[0] = 0x80;
0910     wm2000->anc_download[1] = 0x00;
0911     memcpy(wm2000->anc_download + 2, fw->data, fw->size);
0912 
0913     wm2000->anc_eng_ena = 1;
0914     wm2000->anc_active = 1;
0915     wm2000->spk_ena = 1;
0916     wm2000->i2c = i2c;
0917 
0918     wm2000_reset(wm2000);
0919 
0920     ret = devm_snd_soc_register_component(&i2c->dev,
0921                     &soc_component_dev_wm2000, NULL, 0);
0922 
0923 err_supplies:
0924     regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
0925 
0926 out:
0927     release_firmware(fw);
0928     return ret;
0929 }
0930 
0931 static const struct i2c_device_id wm2000_i2c_id[] = {
0932     { "wm2000", 0 },
0933     { }
0934 };
0935 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
0936 
0937 static struct i2c_driver wm2000_i2c_driver = {
0938     .driver = {
0939         .name = "wm2000",
0940     },
0941     .probe_new = wm2000_i2c_probe,
0942     .id_table = wm2000_i2c_id,
0943 };
0944 
0945 module_i2c_driver(wm2000_i2c_driver);
0946 
0947 MODULE_DESCRIPTION("ASoC WM2000 driver");
0948 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
0949 MODULE_LICENSE("GPL");