1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23a66d387SMark Brown /*
33a66d387SMark Brown * wm2000.c -- WM2000 ALSA Soc Audio driver
43a66d387SMark Brown *
5656baaebSMark Brown * Copyright 2008-2011 Wolfson Microelectronics PLC.
63a66d387SMark Brown *
73a66d387SMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
83a66d387SMark Brown *
93a66d387SMark Brown * The download image for the WM2000 will be requested as
103a66d387SMark Brown * 'wm2000_anc.bin' by default (overridable via platform data) at
113a66d387SMark Brown * runtime and is expected to be in flat binary format. This is
123a66d387SMark Brown * generated by Wolfson configuration tools and includes
13cdbd9b0cSMarkus Elfring * system-specific calibration information. If supplied as a
143a66d387SMark Brown * sequence of ASCII-encoded hexidecimal bytes this can be converted
153a66d387SMark Brown * into a flat binary with a command such as this on the command line:
163a66d387SMark Brown *
173a66d387SMark Brown * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
183a66d387SMark Brown * < file > wm2000_anc.bin
193a66d387SMark Brown */
203a66d387SMark Brown
213a66d387SMark Brown #include <linux/module.h>
223a66d387SMark Brown #include <linux/moduleparam.h>
233a66d387SMark Brown #include <linux/kernel.h>
243a66d387SMark Brown #include <linux/init.h>
253a66d387SMark Brown #include <linux/firmware.h>
26514cfd6dSMark Brown #include <linux/clk.h>
273a66d387SMark Brown #include <linux/delay.h>
283a66d387SMark Brown #include <linux/pm.h>
293a66d387SMark Brown #include <linux/i2c.h>
308aa1fe81SMark Brown #include <linux/regmap.h>
313a66d387SMark Brown #include <linux/debugfs.h>
32a89be93cSMark Brown #include <linux/regulator/consumer.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
343a66d387SMark Brown #include <sound/core.h>
353a66d387SMark Brown #include <sound/pcm.h>
363a66d387SMark Brown #include <sound/pcm_params.h>
373a66d387SMark Brown #include <sound/soc.h>
383a66d387SMark Brown #include <sound/initval.h>
393a66d387SMark Brown #include <sound/tlv.h>
403a66d387SMark Brown
413a66d387SMark Brown #include <sound/wm2000.h>
423a66d387SMark Brown
433a66d387SMark Brown #include "wm2000.h"
443a66d387SMark Brown
45a89be93cSMark Brown #define WM2000_NUM_SUPPLIES 3
46a89be93cSMark Brown
47a89be93cSMark Brown static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
48a89be93cSMark Brown "SPKVDD",
49a89be93cSMark Brown "DBVDD",
50a89be93cSMark Brown "DCVDD",
51a89be93cSMark Brown };
52a89be93cSMark Brown
533a66d387SMark Brown enum wm2000_anc_mode {
543a66d387SMark Brown ANC_ACTIVE = 0,
553a66d387SMark Brown ANC_BYPASS = 1,
563a66d387SMark Brown ANC_STANDBY = 2,
573a66d387SMark Brown ANC_OFF = 3,
583a66d387SMark Brown };
593a66d387SMark Brown
603a66d387SMark Brown struct wm2000_priv {
613a66d387SMark Brown struct i2c_client *i2c;
628aa1fe81SMark Brown struct regmap *regmap;
63514cfd6dSMark Brown struct clk *mclk;
643a66d387SMark Brown
65a89be93cSMark Brown struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
66a89be93cSMark Brown
673a66d387SMark Brown enum wm2000_anc_mode anc_mode;
683a66d387SMark Brown
693a66d387SMark Brown unsigned int anc_active:1;
703a66d387SMark Brown unsigned int anc_eng_ena:1;
713a66d387SMark Brown unsigned int spk_ena:1;
723a66d387SMark Brown
733a66d387SMark Brown unsigned int speech_clarity:1;
743a66d387SMark Brown
753a66d387SMark Brown int anc_download_size;
763a66d387SMark Brown char *anc_download;
778e9bb423SMark Brown
788e9bb423SMark Brown struct mutex lock;
793a66d387SMark Brown };
803a66d387SMark Brown
wm2000_write(struct i2c_client * i2c,unsigned int reg,unsigned int value)813a66d387SMark Brown static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
823a66d387SMark Brown unsigned int value)
833a66d387SMark Brown {
848aa1fe81SMark Brown struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
858aa1fe81SMark Brown return regmap_write(wm2000->regmap, reg, value);
863a66d387SMark Brown }
873a66d387SMark Brown
wm2000_reset(struct wm2000_priv * wm2000)883a66d387SMark Brown static void wm2000_reset(struct wm2000_priv *wm2000)
893a66d387SMark Brown {
903a66d387SMark Brown struct i2c_client *i2c = wm2000->i2c;
913a66d387SMark Brown
923a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
933a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
943a66d387SMark Brown wm2000_write(i2c, WM2000_REG_ID1, 0);
953a66d387SMark Brown
963a66d387SMark Brown wm2000->anc_mode = ANC_OFF;
973a66d387SMark Brown }
983a66d387SMark Brown
wm2000_poll_bit(struct i2c_client * i2c,unsigned int reg,u8 mask)993a66d387SMark Brown static int wm2000_poll_bit(struct i2c_client *i2c,
1003ff1ec27SMark Brown unsigned int reg, u8 mask)
1013a66d387SMark Brown {
102c5d09485SLucas Tanure struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
1033ff1ec27SMark Brown int timeout = 4000;
104c5d09485SLucas Tanure unsigned int val;
1053a66d387SMark Brown
106c5d09485SLucas Tanure regmap_read(wm2000->regmap, reg, &val);
1073a66d387SMark Brown
1083a66d387SMark Brown while (!(val & mask) && --timeout) {
1093a66d387SMark Brown msleep(1);
110c5d09485SLucas Tanure regmap_read(wm2000->regmap, reg, &val);
1113a66d387SMark Brown }
1123a66d387SMark Brown
1133a66d387SMark Brown if (timeout == 0)
1143a66d387SMark Brown return 0;
1153a66d387SMark Brown else
1163a66d387SMark Brown return 1;
1173a66d387SMark Brown }
1183a66d387SMark Brown
wm2000_power_up(struct i2c_client * i2c,int analogue)1193a66d387SMark Brown static int wm2000_power_up(struct i2c_client *i2c, int analogue)
1203a66d387SMark Brown {
1213a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
122d61100bbSMark Brown unsigned long rate;
123c5d09485SLucas Tanure unsigned int val;
1243ff1ec27SMark Brown int ret;
1253a66d387SMark Brown
126bf90e895STakashi Iwai if (WARN_ON(wm2000->anc_mode != ANC_OFF))
127bf90e895STakashi Iwai return -EINVAL;
1283a66d387SMark Brown
1293a66d387SMark Brown dev_dbg(&i2c->dev, "Beginning power up\n");
1303a66d387SMark Brown
131a89be93cSMark Brown ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
132a89be93cSMark Brown if (ret != 0) {
133a89be93cSMark Brown dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
134a89be93cSMark Brown return ret;
135a89be93cSMark Brown }
136a89be93cSMark Brown
137d61100bbSMark Brown rate = clk_get_rate(wm2000->mclk);
138d61100bbSMark Brown if (rate <= 13500000) {
1393a66d387SMark Brown dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
1403a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2,
1413a66d387SMark Brown WM2000_MCLK_DIV2_ENA_CLR);
1423a66d387SMark Brown } else {
1433a66d387SMark Brown dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
1443a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2,
1453a66d387SMark Brown WM2000_MCLK_DIV2_ENA_SET);
1463a66d387SMark Brown }
1473a66d387SMark Brown
1483a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
1493a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
1503a66d387SMark Brown
1513a66d387SMark Brown /* Wait for ANC engine to become ready */
1523a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
1533ff1ec27SMark Brown WM2000_ANC_ENG_IDLE)) {
1543a66d387SMark Brown dev_err(&i2c->dev, "ANC engine failed to reset\n");
155a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
1563a66d387SMark Brown return -ETIMEDOUT;
1573a66d387SMark Brown }
1583a66d387SMark Brown
1593a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
1603ff1ec27SMark Brown WM2000_STATUS_BOOT_COMPLETE)) {
1613a66d387SMark Brown dev_err(&i2c->dev, "ANC engine failed to initialise\n");
162a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
1633a66d387SMark Brown return -ETIMEDOUT;
1643a66d387SMark Brown }
1653a66d387SMark Brown
1663a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
1673a66d387SMark Brown
1683a66d387SMark Brown /* Open code download of the data since it is the only bulk
1693a66d387SMark Brown * write we do. */
1703a66d387SMark Brown dev_dbg(&i2c->dev, "Downloading %d bytes\n",
1713a66d387SMark Brown wm2000->anc_download_size - 2);
1723a66d387SMark Brown
1733a66d387SMark Brown ret = i2c_master_send(i2c, wm2000->anc_download,
1743a66d387SMark Brown wm2000->anc_download_size);
1753a66d387SMark Brown if (ret < 0) {
1763a66d387SMark Brown dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
177a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
1783a66d387SMark Brown return ret;
1793a66d387SMark Brown }
1803a66d387SMark Brown if (ret != wm2000->anc_download_size) {
1813a66d387SMark Brown dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
1823a66d387SMark Brown ret, wm2000->anc_download_size);
183a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
1843a66d387SMark Brown return -EIO;
1853a66d387SMark Brown }
1863a66d387SMark Brown
1873a66d387SMark Brown dev_dbg(&i2c->dev, "Download complete\n");
1883a66d387SMark Brown
1893a66d387SMark Brown if (analogue) {
1903ff1ec27SMark Brown wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
1913a66d387SMark Brown
1923a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
1933a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
1943a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE |
1953a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE);
1963a66d387SMark Brown } else {
1973a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
1983a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE |
1993a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE);
2003a66d387SMark Brown }
2013a66d387SMark Brown
202c5d09485SLucas Tanure ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val);
203c5d09485SLucas Tanure if (ret != 0) {
204c5d09485SLucas Tanure dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret);
205c5d09485SLucas Tanure regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
206c5d09485SLucas Tanure return ret;
207c5d09485SLucas Tanure }
2083a66d387SMark Brown if (wm2000->speech_clarity)
209c5d09485SLucas Tanure val |= WM2000_SPEECH_CLARITY;
210267f8fa2SMark Brown else
211c5d09485SLucas Tanure val &= ~WM2000_SPEECH_CLARITY;
212c5d09485SLucas Tanure wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val);
2133a66d387SMark Brown
2143a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
2153a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
2163a66d387SMark Brown
2173a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
2183a66d387SMark Brown
2193a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
2203ff1ec27SMark Brown WM2000_STATUS_MOUSE_ACTIVE)) {
2213ff1ec27SMark Brown dev_err(&i2c->dev, "Timed out waiting for device\n");
222a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
2233a66d387SMark Brown return -ETIMEDOUT;
2243a66d387SMark Brown }
2253a66d387SMark Brown
2263a66d387SMark Brown dev_dbg(&i2c->dev, "ANC active\n");
2273a66d387SMark Brown if (analogue)
2283a66d387SMark Brown dev_dbg(&i2c->dev, "Analogue active\n");
2293a66d387SMark Brown wm2000->anc_mode = ANC_ACTIVE;
2303a66d387SMark Brown
2313a66d387SMark Brown return 0;
2323a66d387SMark Brown }
2333a66d387SMark Brown
wm2000_power_down(struct i2c_client * i2c,int analogue)2343a66d387SMark Brown static int wm2000_power_down(struct i2c_client *i2c, int analogue)
2353a66d387SMark Brown {
2363a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
2373a66d387SMark Brown
2383a66d387SMark Brown if (analogue) {
2393ff1ec27SMark Brown wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
2403a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
2413a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
2423a66d387SMark Brown WM2000_MODE_POWER_DOWN);
2433a66d387SMark Brown } else {
2443a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
2453a66d387SMark Brown WM2000_MODE_POWER_DOWN);
2463a66d387SMark Brown }
2473a66d387SMark Brown
2483a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
2493ff1ec27SMark Brown WM2000_STATUS_POWER_DOWN_COMPLETE)) {
2503a66d387SMark Brown dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
2513a66d387SMark Brown return -ETIMEDOUT;
2523a66d387SMark Brown }
2533a66d387SMark Brown
2543a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
2553ff1ec27SMark Brown WM2000_ANC_ENG_IDLE)) {
2563a66d387SMark Brown dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
2573a66d387SMark Brown return -ETIMEDOUT;
2583a66d387SMark Brown }
2593a66d387SMark Brown
260a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
261a89be93cSMark Brown
2623a66d387SMark Brown dev_dbg(&i2c->dev, "powered off\n");
2633a66d387SMark Brown wm2000->anc_mode = ANC_OFF;
2643a66d387SMark Brown
2653a66d387SMark Brown return 0;
2663a66d387SMark Brown }
2673a66d387SMark Brown
wm2000_enter_bypass(struct i2c_client * i2c,int analogue)2683a66d387SMark Brown static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
2693a66d387SMark Brown {
2703a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
2713a66d387SMark Brown
272bf90e895STakashi Iwai if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
273bf90e895STakashi Iwai return -EINVAL;
2743a66d387SMark Brown
2753a66d387SMark Brown if (analogue) {
2763a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
2773a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
2783a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
2793a66d387SMark Brown WM2000_MODE_BYPASS_ENTRY);
2803a66d387SMark Brown } else {
2813a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
2823a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
2833a66d387SMark Brown WM2000_MODE_BYPASS_ENTRY);
2843a66d387SMark Brown }
2853a66d387SMark Brown
2863a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
2873ff1ec27SMark Brown WM2000_STATUS_ANC_DISABLED)) {
2883a66d387SMark Brown dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
2893a66d387SMark Brown return -ETIMEDOUT;
2903a66d387SMark Brown }
2913a66d387SMark Brown
2923a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
2933ff1ec27SMark Brown WM2000_ANC_ENG_IDLE)) {
2943a66d387SMark Brown dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
2953a66d387SMark Brown return -ETIMEDOUT;
2963a66d387SMark Brown }
2973a66d387SMark Brown
2983a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
2993a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
3003a66d387SMark Brown
3013a66d387SMark Brown wm2000->anc_mode = ANC_BYPASS;
3023a66d387SMark Brown dev_dbg(&i2c->dev, "bypass enabled\n");
3033a66d387SMark Brown
3043a66d387SMark Brown return 0;
3053a66d387SMark Brown }
3063a66d387SMark Brown
wm2000_exit_bypass(struct i2c_client * i2c,int analogue)3073a66d387SMark Brown static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
3083a66d387SMark Brown {
3093a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
3103a66d387SMark Brown
311bf90e895STakashi Iwai if (WARN_ON(wm2000->anc_mode != ANC_BYPASS))
312bf90e895STakashi Iwai return -EINVAL;
3133a66d387SMark Brown
3143a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
3153a66d387SMark Brown
3163a66d387SMark Brown if (analogue) {
3173a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
3183a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
3193a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE |
3203a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE);
3213a66d387SMark Brown } else {
3223a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
3233a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE |
3243a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE);
3253a66d387SMark Brown }
3263a66d387SMark Brown
3273a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
3283a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
3293a66d387SMark Brown
3303a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
3313ff1ec27SMark Brown WM2000_STATUS_MOUSE_ACTIVE)) {
3323a66d387SMark Brown dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
3333a66d387SMark Brown return -ETIMEDOUT;
3343a66d387SMark Brown }
3353a66d387SMark Brown
3363a66d387SMark Brown wm2000->anc_mode = ANC_ACTIVE;
3373a66d387SMark Brown dev_dbg(&i2c->dev, "MOUSE active\n");
3383a66d387SMark Brown
3393a66d387SMark Brown return 0;
3403a66d387SMark Brown }
3413a66d387SMark Brown
wm2000_enter_standby(struct i2c_client * i2c,int analogue)3423a66d387SMark Brown static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
3433a66d387SMark Brown {
3443a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
3453a66d387SMark Brown
346bf90e895STakashi Iwai if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
347bf90e895STakashi Iwai return -EINVAL;
3483a66d387SMark Brown
3493a66d387SMark Brown if (analogue) {
3503ff1ec27SMark Brown wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
3513a66d387SMark Brown
3523a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
3533a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
3543a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
3553a66d387SMark Brown WM2000_MODE_STANDBY_ENTRY);
3563a66d387SMark Brown } else {
3573a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
3583a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
3593a66d387SMark Brown WM2000_MODE_STANDBY_ENTRY);
3603a66d387SMark Brown }
3613a66d387SMark Brown
3623a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
3633ff1ec27SMark Brown WM2000_STATUS_ANC_DISABLED)) {
3643a66d387SMark Brown dev_err(&i2c->dev,
3653a66d387SMark Brown "Timed out waiting for ANC disable after 1ms\n");
3663a66d387SMark Brown return -ETIMEDOUT;
3673a66d387SMark Brown }
3683a66d387SMark Brown
3693ff1ec27SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
3703a66d387SMark Brown dev_err(&i2c->dev,
3713ff1ec27SMark Brown "Timed out waiting for standby\n");
3723a66d387SMark Brown return -ETIMEDOUT;
3733a66d387SMark Brown }
3743a66d387SMark Brown
3753a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
3763a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
3773a66d387SMark Brown
3783a66d387SMark Brown wm2000->anc_mode = ANC_STANDBY;
3793a66d387SMark Brown dev_dbg(&i2c->dev, "standby\n");
3803a66d387SMark Brown if (analogue)
3813a66d387SMark Brown dev_dbg(&i2c->dev, "Analogue disabled\n");
3823a66d387SMark Brown
3833a66d387SMark Brown return 0;
3843a66d387SMark Brown }
3853a66d387SMark Brown
wm2000_exit_standby(struct i2c_client * i2c,int analogue)3863a66d387SMark Brown static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
3873a66d387SMark Brown {
3883a66d387SMark Brown struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
3893a66d387SMark Brown
390bf90e895STakashi Iwai if (WARN_ON(wm2000->anc_mode != ANC_STANDBY))
391bf90e895STakashi Iwai return -EINVAL;
3923a66d387SMark Brown
3933a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
3943a66d387SMark Brown
3953a66d387SMark Brown if (analogue) {
3963ff1ec27SMark Brown wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
3973a66d387SMark Brown
3983a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
3993a66d387SMark Brown WM2000_MODE_ANA_SEQ_INCLUDE |
4003a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
4013a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE);
4023a66d387SMark Brown } else {
4033a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
4043a66d387SMark Brown WM2000_MODE_THERMAL_ENABLE |
4053a66d387SMark Brown WM2000_MODE_MOUSE_ENABLE);
4063a66d387SMark Brown }
4073a66d387SMark Brown
4083a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
4093a66d387SMark Brown wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
4103a66d387SMark Brown
4113a66d387SMark Brown if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
4123ff1ec27SMark Brown WM2000_STATUS_MOUSE_ACTIVE)) {
4133ff1ec27SMark Brown dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
4143a66d387SMark Brown return -ETIMEDOUT;
4153a66d387SMark Brown }
4163a66d387SMark Brown
4173a66d387SMark Brown wm2000->anc_mode = ANC_ACTIVE;
4183a66d387SMark Brown dev_dbg(&i2c->dev, "MOUSE active\n");
4193a66d387SMark Brown if (analogue)
4203a66d387SMark Brown dev_dbg(&i2c->dev, "Analogue enabled\n");
4213a66d387SMark Brown
4223a66d387SMark Brown return 0;
4233a66d387SMark Brown }
4243a66d387SMark Brown
4253a66d387SMark Brown typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
4263a66d387SMark Brown
4273a66d387SMark Brown static struct {
4283a66d387SMark Brown enum wm2000_anc_mode source;
4293a66d387SMark Brown enum wm2000_anc_mode dest;
4303a66d387SMark Brown int analogue;
4313a66d387SMark Brown wm2000_mode_fn step[2];
4323a66d387SMark Brown } anc_transitions[] = {
4333a66d387SMark Brown {
4343a66d387SMark Brown .source = ANC_OFF,
4353a66d387SMark Brown .dest = ANC_ACTIVE,
4363a66d387SMark Brown .analogue = 1,
4373a66d387SMark Brown .step = {
4383a66d387SMark Brown wm2000_power_up,
4393a66d387SMark Brown },
4403a66d387SMark Brown },
4413a66d387SMark Brown {
4423a66d387SMark Brown .source = ANC_OFF,
4433a66d387SMark Brown .dest = ANC_STANDBY,
4443a66d387SMark Brown .step = {
4453a66d387SMark Brown wm2000_power_up,
4463a66d387SMark Brown wm2000_enter_standby,
4473a66d387SMark Brown },
4483a66d387SMark Brown },
4493a66d387SMark Brown {
4503a66d387SMark Brown .source = ANC_OFF,
4513a66d387SMark Brown .dest = ANC_BYPASS,
4523a66d387SMark Brown .analogue = 1,
4533a66d387SMark Brown .step = {
4543a66d387SMark Brown wm2000_power_up,
4553a66d387SMark Brown wm2000_enter_bypass,
4563a66d387SMark Brown },
4573a66d387SMark Brown },
4583a66d387SMark Brown {
4593a66d387SMark Brown .source = ANC_ACTIVE,
4603a66d387SMark Brown .dest = ANC_BYPASS,
4613a66d387SMark Brown .analogue = 1,
4623a66d387SMark Brown .step = {
4633a66d387SMark Brown wm2000_enter_bypass,
4643a66d387SMark Brown },
4653a66d387SMark Brown },
4663a66d387SMark Brown {
4673a66d387SMark Brown .source = ANC_ACTIVE,
4683a66d387SMark Brown .dest = ANC_STANDBY,
4693a66d387SMark Brown .analogue = 1,
4703a66d387SMark Brown .step = {
4713a66d387SMark Brown wm2000_enter_standby,
4723a66d387SMark Brown },
4733a66d387SMark Brown },
4743a66d387SMark Brown {
4753a66d387SMark Brown .source = ANC_ACTIVE,
4763a66d387SMark Brown .dest = ANC_OFF,
4773a66d387SMark Brown .analogue = 1,
4783a66d387SMark Brown .step = {
4793a66d387SMark Brown wm2000_power_down,
4803a66d387SMark Brown },
4813a66d387SMark Brown },
4823a66d387SMark Brown {
4833a66d387SMark Brown .source = ANC_BYPASS,
4843a66d387SMark Brown .dest = ANC_ACTIVE,
4853a66d387SMark Brown .analogue = 1,
4863a66d387SMark Brown .step = {
4873a66d387SMark Brown wm2000_exit_bypass,
4883a66d387SMark Brown },
4893a66d387SMark Brown },
4903a66d387SMark Brown {
4913a66d387SMark Brown .source = ANC_BYPASS,
4923a66d387SMark Brown .dest = ANC_STANDBY,
4933a66d387SMark Brown .analogue = 1,
4943a66d387SMark Brown .step = {
4953a66d387SMark Brown wm2000_exit_bypass,
4963a66d387SMark Brown wm2000_enter_standby,
4973a66d387SMark Brown },
4983a66d387SMark Brown },
4993a66d387SMark Brown {
5003a66d387SMark Brown .source = ANC_BYPASS,
5013a66d387SMark Brown .dest = ANC_OFF,
5023a66d387SMark Brown .step = {
5033a66d387SMark Brown wm2000_exit_bypass,
5043a66d387SMark Brown wm2000_power_down,
5053a66d387SMark Brown },
5063a66d387SMark Brown },
5073a66d387SMark Brown {
5083a66d387SMark Brown .source = ANC_STANDBY,
5093a66d387SMark Brown .dest = ANC_ACTIVE,
5103a66d387SMark Brown .analogue = 1,
5113a66d387SMark Brown .step = {
5123a66d387SMark Brown wm2000_exit_standby,
5133a66d387SMark Brown },
5143a66d387SMark Brown },
5153a66d387SMark Brown {
5163a66d387SMark Brown .source = ANC_STANDBY,
5173a66d387SMark Brown .dest = ANC_BYPASS,
5183a66d387SMark Brown .analogue = 1,
5193a66d387SMark Brown .step = {
5203a66d387SMark Brown wm2000_exit_standby,
5213a66d387SMark Brown wm2000_enter_bypass,
5223a66d387SMark Brown },
5233a66d387SMark Brown },
5243a66d387SMark Brown {
5253a66d387SMark Brown .source = ANC_STANDBY,
5263a66d387SMark Brown .dest = ANC_OFF,
5273a66d387SMark Brown .step = {
5283a66d387SMark Brown wm2000_exit_standby,
5293a66d387SMark Brown wm2000_power_down,
5303a66d387SMark Brown },
5313a66d387SMark Brown },
5323a66d387SMark Brown };
5333a66d387SMark Brown
wm2000_anc_transition(struct wm2000_priv * wm2000,enum wm2000_anc_mode mode)5343a66d387SMark Brown static int wm2000_anc_transition(struct wm2000_priv *wm2000,
5353a66d387SMark Brown enum wm2000_anc_mode mode)
5363a66d387SMark Brown {
5373a66d387SMark Brown struct i2c_client *i2c = wm2000->i2c;
5383a66d387SMark Brown int i, j;
539be2af740SYang Yingliang int ret = 0;
5403a66d387SMark Brown
5413a66d387SMark Brown if (wm2000->anc_mode == mode)
5423a66d387SMark Brown return 0;
5433a66d387SMark Brown
5443a66d387SMark Brown for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
5453a66d387SMark Brown if (anc_transitions[i].source == wm2000->anc_mode &&
5463a66d387SMark Brown anc_transitions[i].dest == mode)
5473a66d387SMark Brown break;
5483a66d387SMark Brown if (i == ARRAY_SIZE(anc_transitions)) {
5493a66d387SMark Brown dev_err(&i2c->dev, "No transition for %d->%d\n",
5503a66d387SMark Brown wm2000->anc_mode, mode);
5513a66d387SMark Brown return -EINVAL;
5523a66d387SMark Brown }
5533a66d387SMark Brown
554514cfd6dSMark Brown /* Maintain clock while active */
555514cfd6dSMark Brown if (anc_transitions[i].source == ANC_OFF) {
556514cfd6dSMark Brown ret = clk_prepare_enable(wm2000->mclk);
557514cfd6dSMark Brown if (ret != 0) {
558514cfd6dSMark Brown dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret);
559514cfd6dSMark Brown return ret;
560514cfd6dSMark Brown }
561514cfd6dSMark Brown }
562514cfd6dSMark Brown
5633a66d387SMark Brown for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
5643a66d387SMark Brown if (!anc_transitions[i].step[j])
5653a66d387SMark Brown break;
5663a66d387SMark Brown ret = anc_transitions[i].step[j](i2c,
5673a66d387SMark Brown anc_transitions[i].analogue);
5683a66d387SMark Brown if (ret != 0)
569be2af740SYang Yingliang break;
5703a66d387SMark Brown }
5713a66d387SMark Brown
572514cfd6dSMark Brown if (anc_transitions[i].dest == ANC_OFF)
573514cfd6dSMark Brown clk_disable_unprepare(wm2000->mclk);
574514cfd6dSMark Brown
575be2af740SYang Yingliang return ret;
5763a66d387SMark Brown }
5773a66d387SMark Brown
wm2000_anc_set_mode(struct wm2000_priv * wm2000)5783a66d387SMark Brown static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
5793a66d387SMark Brown {
5803a66d387SMark Brown struct i2c_client *i2c = wm2000->i2c;
5813a66d387SMark Brown enum wm2000_anc_mode mode;
5823a66d387SMark Brown
5833a66d387SMark Brown if (wm2000->anc_eng_ena && wm2000->spk_ena)
5843a66d387SMark Brown if (wm2000->anc_active)
5853a66d387SMark Brown mode = ANC_ACTIVE;
5863a66d387SMark Brown else
5873a66d387SMark Brown mode = ANC_BYPASS;
5883a66d387SMark Brown else
5893a66d387SMark Brown mode = ANC_STANDBY;
5903a66d387SMark Brown
5913a66d387SMark Brown dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
5923a66d387SMark Brown mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
5933a66d387SMark Brown wm2000->anc_active);
5943a66d387SMark Brown
5953a66d387SMark Brown return wm2000_anc_transition(wm2000, mode);
5963a66d387SMark Brown }
5973a66d387SMark Brown
wm2000_anc_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)5983a66d387SMark Brown static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
5993a66d387SMark Brown struct snd_ctl_elem_value *ucontrol)
6003a66d387SMark Brown {
601e5449affSKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
602e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
6033a66d387SMark Brown
60400a14c29STakashi Iwai ucontrol->value.integer.value[0] = wm2000->anc_active;
6053a66d387SMark Brown
6063a66d387SMark Brown return 0;
6073a66d387SMark Brown }
6083a66d387SMark Brown
wm2000_anc_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)6093a66d387SMark Brown static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
6103a66d387SMark Brown struct snd_ctl_elem_value *ucontrol)
6113a66d387SMark Brown {
612e5449affSKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
613e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
6148444f59fSDan Carpenter unsigned int anc_active = ucontrol->value.integer.value[0];
6158e9bb423SMark Brown int ret;
6163a66d387SMark Brown
6173a66d387SMark Brown if (anc_active > 1)
6183a66d387SMark Brown return -EINVAL;
6193a66d387SMark Brown
6208e9bb423SMark Brown mutex_lock(&wm2000->lock);
6218e9bb423SMark Brown
6223a66d387SMark Brown wm2000->anc_active = anc_active;
6233a66d387SMark Brown
6248e9bb423SMark Brown ret = wm2000_anc_set_mode(wm2000);
6258e9bb423SMark Brown
6268e9bb423SMark Brown mutex_unlock(&wm2000->lock);
6278e9bb423SMark Brown
6288e9bb423SMark Brown return ret;
6293a66d387SMark Brown }
6303a66d387SMark Brown
wm2000_speaker_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)6313a66d387SMark Brown static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
6323a66d387SMark Brown struct snd_ctl_elem_value *ucontrol)
6333a66d387SMark Brown {
634e5449affSKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
635e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
6363a66d387SMark Brown
63700a14c29STakashi Iwai ucontrol->value.integer.value[0] = wm2000->spk_ena;
6383a66d387SMark Brown
6393a66d387SMark Brown return 0;
6403a66d387SMark Brown }
6413a66d387SMark Brown
wm2000_speaker_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)6423a66d387SMark Brown static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
6433a66d387SMark Brown struct snd_ctl_elem_value *ucontrol)
6443a66d387SMark Brown {
645e5449affSKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
646e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
6478444f59fSDan Carpenter unsigned int val = ucontrol->value.integer.value[0];
6488e9bb423SMark Brown int ret;
6493a66d387SMark Brown
6503a66d387SMark Brown if (val > 1)
6513a66d387SMark Brown return -EINVAL;
6523a66d387SMark Brown
6538e9bb423SMark Brown mutex_lock(&wm2000->lock);
6548e9bb423SMark Brown
6553a66d387SMark Brown wm2000->spk_ena = val;
6563a66d387SMark Brown
6578e9bb423SMark Brown ret = wm2000_anc_set_mode(wm2000);
6588e9bb423SMark Brown
6598e9bb423SMark Brown mutex_unlock(&wm2000->lock);
6608e9bb423SMark Brown
6618e9bb423SMark Brown return ret;
6623a66d387SMark Brown }
6633a66d387SMark Brown
6643a66d387SMark Brown static const struct snd_kcontrol_new wm2000_controls[] = {
6653f3af6eeSMark Brown SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
6663a66d387SMark Brown SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
6673a66d387SMark Brown wm2000_anc_mode_get,
6683a66d387SMark Brown wm2000_anc_mode_put),
6693a66d387SMark Brown SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
6703a66d387SMark Brown wm2000_speaker_get,
6713a66d387SMark Brown wm2000_speaker_put),
6723a66d387SMark Brown };
6733a66d387SMark Brown
wm2000_anc_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)6743a66d387SMark Brown static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
6753a66d387SMark Brown struct snd_kcontrol *kcontrol, int event)
6763a66d387SMark Brown {
677e5449affSKuninori Morimoto struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
678e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
6798e9bb423SMark Brown int ret;
6808e9bb423SMark Brown
6818e9bb423SMark Brown mutex_lock(&wm2000->lock);
6823a66d387SMark Brown
6833a66d387SMark Brown if (SND_SOC_DAPM_EVENT_ON(event))
6843a66d387SMark Brown wm2000->anc_eng_ena = 1;
6853a66d387SMark Brown
6863a66d387SMark Brown if (SND_SOC_DAPM_EVENT_OFF(event))
6873a66d387SMark Brown wm2000->anc_eng_ena = 0;
6883a66d387SMark Brown
6898e9bb423SMark Brown ret = wm2000_anc_set_mode(wm2000);
6908e9bb423SMark Brown
6918e9bb423SMark Brown mutex_unlock(&wm2000->lock);
6928e9bb423SMark Brown
6938e9bb423SMark Brown return ret;
6943a66d387SMark Brown }
6953a66d387SMark Brown
6963a66d387SMark Brown static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
6973a66d387SMark Brown /* Externally visible pins */
6984911ccdbSMark Brown SND_SOC_DAPM_OUTPUT("SPKN"),
6994911ccdbSMark Brown SND_SOC_DAPM_OUTPUT("SPKP"),
7003a66d387SMark Brown
7014911ccdbSMark Brown SND_SOC_DAPM_INPUT("LINN"),
7024911ccdbSMark Brown SND_SOC_DAPM_INPUT("LINP"),
7033a66d387SMark Brown
7043a66d387SMark Brown SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
7053a66d387SMark Brown wm2000_anc_power_event,
7063a66d387SMark Brown SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
7073a66d387SMark Brown };
7083a66d387SMark Brown
7093a66d387SMark Brown /* Target, Path, Source */
7104911ccdbSMark Brown static const struct snd_soc_dapm_route wm2000_audio_map[] = {
7114911ccdbSMark Brown { "SPKN", NULL, "ANC Engine" },
7124911ccdbSMark Brown { "SPKP", NULL, "ANC Engine" },
7134911ccdbSMark Brown { "ANC Engine", NULL, "LINN" },
7144911ccdbSMark Brown { "ANC Engine", NULL, "LINP" },
7153a66d387SMark Brown };
7163a66d387SMark Brown
7174911ccdbSMark Brown #ifdef CONFIG_PM
wm2000_suspend(struct snd_soc_component * component)718e5449affSKuninori Morimoto static int wm2000_suspend(struct snd_soc_component *component)
7193a66d387SMark Brown {
720e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
7213a66d387SMark Brown
7224911ccdbSMark Brown return wm2000_anc_transition(wm2000, ANC_OFF);
7233a66d387SMark Brown }
7243a66d387SMark Brown
wm2000_resume(struct snd_soc_component * component)725e5449affSKuninori Morimoto static int wm2000_resume(struct snd_soc_component *component)
7264911ccdbSMark Brown {
727e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
7283a66d387SMark Brown
7294911ccdbSMark Brown return wm2000_anc_set_mode(wm2000);
7303a66d387SMark Brown }
7314911ccdbSMark Brown #else
7324911ccdbSMark Brown #define wm2000_suspend NULL
7334911ccdbSMark Brown #define wm2000_resume NULL
7344911ccdbSMark Brown #endif
7353a66d387SMark Brown
wm2000_readable_reg(struct device * dev,unsigned int reg)73651cc7ed3SMark Brown static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
73751cc7ed3SMark Brown {
73851cc7ed3SMark Brown switch (reg) {
73951cc7ed3SMark Brown case WM2000_REG_SYS_START:
74033e7546eSMark Brown case WM2000_REG_ANC_GAIN_CTRL:
74133e7546eSMark Brown case WM2000_REG_MSE_TH1:
74233e7546eSMark Brown case WM2000_REG_MSE_TH2:
74351cc7ed3SMark Brown case WM2000_REG_SPEECH_CLARITY:
74451cc7ed3SMark Brown case WM2000_REG_SYS_WATCHDOG:
74551cc7ed3SMark Brown case WM2000_REG_ANA_VMID_PD_TIME:
74651cc7ed3SMark Brown case WM2000_REG_ANA_VMID_PU_TIME:
74751cc7ed3SMark Brown case WM2000_REG_CAT_FLTR_INDX:
74851cc7ed3SMark Brown case WM2000_REG_CAT_GAIN_0:
74951cc7ed3SMark Brown case WM2000_REG_SYS_STATUS:
75051cc7ed3SMark Brown case WM2000_REG_SYS_MODE_CNTRL:
75151cc7ed3SMark Brown case WM2000_REG_SYS_START0:
75251cc7ed3SMark Brown case WM2000_REG_SYS_START1:
75351cc7ed3SMark Brown case WM2000_REG_ID1:
75451cc7ed3SMark Brown case WM2000_REG_ID2:
75551cc7ed3SMark Brown case WM2000_REG_REVISON:
75651cc7ed3SMark Brown case WM2000_REG_SYS_CTL1:
75751cc7ed3SMark Brown case WM2000_REG_SYS_CTL2:
75851cc7ed3SMark Brown case WM2000_REG_ANC_STAT:
75951cc7ed3SMark Brown case WM2000_REG_IF_CTL:
760939dc51bSMark Brown case WM2000_REG_ANA_MIC_CTL:
761939dc51bSMark Brown case WM2000_REG_SPK_CTL:
76251cc7ed3SMark Brown return true;
76351cc7ed3SMark Brown default:
76451cc7ed3SMark Brown return false;
76551cc7ed3SMark Brown }
76651cc7ed3SMark Brown }
76751cc7ed3SMark Brown
7688aa1fe81SMark Brown static const struct regmap_config wm2000_regmap = {
769d0e12f3fSMark Brown .reg_bits = 16,
7708aa1fe81SMark Brown .val_bits = 8,
77151cc7ed3SMark Brown
772939dc51bSMark Brown .max_register = WM2000_REG_SPK_CTL,
77351cc7ed3SMark Brown .readable_reg = wm2000_readable_reg,
7748aa1fe81SMark Brown };
7758aa1fe81SMark Brown
wm2000_probe(struct snd_soc_component * component)776e5449affSKuninori Morimoto static int wm2000_probe(struct snd_soc_component *component)
7774911ccdbSMark Brown {
778e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
7794911ccdbSMark Brown
7804911ccdbSMark Brown /* This will trigger a transition to standby mode by default */
7814911ccdbSMark Brown wm2000_anc_set_mode(wm2000);
7824911ccdbSMark Brown
7834911ccdbSMark Brown return 0;
7844911ccdbSMark Brown }
7854911ccdbSMark Brown
wm2000_remove(struct snd_soc_component * component)786e5449affSKuninori Morimoto static void wm2000_remove(struct snd_soc_component *component)
7874911ccdbSMark Brown {
788e5449affSKuninori Morimoto struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
7894911ccdbSMark Brown
790e5449affSKuninori Morimoto wm2000_anc_transition(wm2000, ANC_OFF);
7914911ccdbSMark Brown }
7924911ccdbSMark Brown
793e5449affSKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm2000 = {
7944911ccdbSMark Brown .probe = wm2000_probe,
7954911ccdbSMark Brown .remove = wm2000_remove,
7964911ccdbSMark Brown .suspend = wm2000_suspend,
7974911ccdbSMark Brown .resume = wm2000_resume,
798bd22e11eSKuninori Morimoto .controls = wm2000_controls,
799bd22e11eSKuninori Morimoto .num_controls = ARRAY_SIZE(wm2000_controls),
8004911ccdbSMark Brown .dapm_widgets = wm2000_dapm_widgets,
8014911ccdbSMark Brown .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
8024911ccdbSMark Brown .dapm_routes = wm2000_audio_map,
8034911ccdbSMark Brown .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
804e5449affSKuninori Morimoto .idle_bias_on = 1,
805e5449affSKuninori Morimoto .use_pmdown_time = 1,
8064911ccdbSMark Brown };
8074911ccdbSMark Brown
wm2000_i2c_probe(struct i2c_client * i2c)80897b0b6e3SStephen Kitt static int wm2000_i2c_probe(struct i2c_client *i2c)
8093a66d387SMark Brown {
8103a66d387SMark Brown struct wm2000_priv *wm2000;
8113a66d387SMark Brown struct wm2000_platform_data *pdata;
8123a66d387SMark Brown const char *filename;
813c83f1d7eSJesper Juhl const struct firmware *fw = NULL;
814a89be93cSMark Brown int ret, i;
815c5d09485SLucas Tanure unsigned int reg;
8163a66d387SMark Brown u16 id;
8173a66d387SMark Brown
818bf0842baSMarkus Elfring wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL);
819a0f62118SSachin Kamat if (!wm2000)
8203a66d387SMark Brown return -ENOMEM;
8213a66d387SMark Brown
8228e9bb423SMark Brown mutex_init(&wm2000->lock);
8238e9bb423SMark Brown
8248aa1fe81SMark Brown dev_set_drvdata(&i2c->dev, wm2000);
8258aa1fe81SMark Brown
8268fed54aeSMark Brown wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
8278aa1fe81SMark Brown if (IS_ERR(wm2000->regmap)) {
8288aa1fe81SMark Brown ret = PTR_ERR(wm2000->regmap);
8298aa1fe81SMark Brown dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
8308aa1fe81SMark Brown ret);
831c83f1d7eSJesper Juhl goto out;
8328aa1fe81SMark Brown }
8338aa1fe81SMark Brown
834a89be93cSMark Brown for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
835a89be93cSMark Brown wm2000->supplies[i].supply = wm2000_supplies[i];
836a89be93cSMark Brown
837a89be93cSMark Brown ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
838a89be93cSMark Brown wm2000->supplies);
839a89be93cSMark Brown if (ret != 0) {
840a89be93cSMark Brown dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
841a89be93cSMark Brown return ret;
842a89be93cSMark Brown }
843a89be93cSMark Brown
844a89be93cSMark Brown ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
845a89be93cSMark Brown if (ret != 0) {
846a89be93cSMark Brown dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
847a89be93cSMark Brown return ret;
848a89be93cSMark Brown }
849a89be93cSMark Brown
8503a66d387SMark Brown /* Verify that this is a WM2000 */
851c5d09485SLucas Tanure ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, ®);
852c5d09485SLucas Tanure if (ret != 0) {
853c5d09485SLucas Tanure dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret);
854c5d09485SLucas Tanure return ret;
855c5d09485SLucas Tanure }
8563a66d387SMark Brown id = reg << 8;
857c5d09485SLucas Tanure ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, ®);
858c5d09485SLucas Tanure if (ret != 0) {
859c5d09485SLucas Tanure dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret);
860c5d09485SLucas Tanure return ret;
861c5d09485SLucas Tanure }
8623a66d387SMark Brown id |= reg & 0xff;
8633a66d387SMark Brown
8643a66d387SMark Brown if (id != 0x2000) {
8653a66d387SMark Brown dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
8663a66d387SMark Brown ret = -ENODEV;
867a89be93cSMark Brown goto err_supplies;
8683a66d387SMark Brown }
8693a66d387SMark Brown
870c5d09485SLucas Tanure ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, ®);
871c5d09485SLucas Tanure if (ret != 0) {
872c5d09485SLucas Tanure dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret);
873c5d09485SLucas Tanure return ret;
874c5d09485SLucas Tanure }
8753a66d387SMark Brown dev_info(&i2c->dev, "revision %c\n", reg + 'A');
8763a66d387SMark Brown
877514cfd6dSMark Brown wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
878514cfd6dSMark Brown if (IS_ERR(wm2000->mclk)) {
879514cfd6dSMark Brown ret = PTR_ERR(wm2000->mclk);
880514cfd6dSMark Brown dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
881514cfd6dSMark Brown goto err_supplies;
882514cfd6dSMark Brown }
883514cfd6dSMark Brown
8843a66d387SMark Brown filename = "wm2000_anc.bin";
8853a66d387SMark Brown pdata = dev_get_platdata(&i2c->dev);
8863a66d387SMark Brown if (pdata) {
8873a66d387SMark Brown wm2000->speech_clarity = !pdata->speech_enh_disable;
8883a66d387SMark Brown
8893a66d387SMark Brown if (pdata->download_file)
8903a66d387SMark Brown filename = pdata->download_file;
8913a66d387SMark Brown }
8923a66d387SMark Brown
8933a66d387SMark Brown ret = request_firmware(&fw, filename, &i2c->dev);
8943a66d387SMark Brown if (ret != 0) {
8953a66d387SMark Brown dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
896a89be93cSMark Brown goto err_supplies;
8973a66d387SMark Brown }
8983a66d387SMark Brown
8993a66d387SMark Brown /* Pre-cook the concatenation of the register address onto the image */
9003a66d387SMark Brown wm2000->anc_download_size = fw->size + 2;
901b03e96e4SMark Brown wm2000->anc_download = devm_kzalloc(&i2c->dev,
902b03e96e4SMark Brown wm2000->anc_download_size,
903b03e96e4SMark Brown GFP_KERNEL);
9043a66d387SMark Brown if (wm2000->anc_download == NULL) {
9053a66d387SMark Brown ret = -ENOMEM;
906a89be93cSMark Brown goto err_supplies;
9073a66d387SMark Brown }
9083a66d387SMark Brown
9093a66d387SMark Brown wm2000->anc_download[0] = 0x80;
9103a66d387SMark Brown wm2000->anc_download[1] = 0x00;
9113a66d387SMark Brown memcpy(wm2000->anc_download + 2, fw->data, fw->size);
9123a66d387SMark Brown
9133a66d387SMark Brown wm2000->anc_eng_ena = 1;
914e71fa370SMark Brown wm2000->anc_active = 1;
915e71fa370SMark Brown wm2000->spk_ena = 1;
9163a66d387SMark Brown wm2000->i2c = i2c;
9173a66d387SMark Brown
9183a66d387SMark Brown wm2000_reset(wm2000);
9193a66d387SMark Brown
920e5449affSKuninori Morimoto ret = devm_snd_soc_register_component(&i2c->dev,
921e5449affSKuninori Morimoto &soc_component_dev_wm2000, NULL, 0);
9223a66d387SMark Brown
923a89be93cSMark Brown err_supplies:
924a89be93cSMark Brown regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
9253a66d387SMark Brown
926c83f1d7eSJesper Juhl out:
927c83f1d7eSJesper Juhl release_firmware(fw);
9283a66d387SMark Brown return ret;
9293a66d387SMark Brown }
9303a66d387SMark Brown
9313a66d387SMark Brown static const struct i2c_device_id wm2000_i2c_id[] = {
9323a66d387SMark Brown { "wm2000", 0 },
9333a66d387SMark Brown { }
9343a66d387SMark Brown };
9353a66d387SMark Brown MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
9363a66d387SMark Brown
9373a66d387SMark Brown static struct i2c_driver wm2000_i2c_driver = {
9383a66d387SMark Brown .driver = {
9393a66d387SMark Brown .name = "wm2000",
9403a66d387SMark Brown },
941*9abcd240SUwe Kleine-König .probe = wm2000_i2c_probe,
9423a66d387SMark Brown .id_table = wm2000_i2c_id,
9433a66d387SMark Brown };
9443a66d387SMark Brown
9453a4bfd88SSachin Kamat module_i2c_driver(wm2000_i2c_driver);
9463a66d387SMark Brown
9473a66d387SMark Brown MODULE_DESCRIPTION("ASoC WM2000 driver");
9483a66d387SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
9493a66d387SMark Brown MODULE_LICENSE("GPL");
950