Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 2 | /* |
| 3 | * wm2000.c -- WM2000 ALSA Soc Audio driver |
| 4 | * |
Mark Brown | 656baae | 2012-05-23 12:39:07 +0100 | [diff] [blame] | 5 | * Copyright 2008-2011 Wolfson Microelectronics PLC. |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 6 | * |
| 7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 8 | * |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 9 | * The download image for the WM2000 will be requested as |
| 10 | * 'wm2000_anc.bin' by default (overridable via platform data) at |
| 11 | * runtime and is expected to be in flat binary format. This is |
| 12 | * generated by Wolfson configuration tools and includes |
Markus Elfring | cdbd9b0 | 2017-11-24 08:02:57 +0100 | [diff] [blame] | 13 | * system-specific calibration information. If supplied as a |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 14 | * sequence of ASCII-encoded hexidecimal bytes this can be converted |
| 15 | * into a flat binary with a command such as this on the command line: |
| 16 | * |
| 17 | * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }' |
| 18 | * < file > wm2000_anc.bin |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/firmware.h> |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 26 | #include <linux/clk.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 27 | #include <linux/delay.h> |
| 28 | #include <linux/pm.h> |
| 29 | #include <linux/i2c.h> |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 30 | #include <linux/regmap.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 32 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 34 | #include <sound/core.h> |
| 35 | #include <sound/pcm.h> |
| 36 | #include <sound/pcm_params.h> |
| 37 | #include <sound/soc.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 38 | #include <sound/initval.h> |
| 39 | #include <sound/tlv.h> |
| 40 | |
| 41 | #include <sound/wm2000.h> |
| 42 | |
| 43 | #include "wm2000.h" |
| 44 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 45 | #define WM2000_NUM_SUPPLIES 3 |
| 46 | |
| 47 | static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = { |
| 48 | "SPKVDD", |
| 49 | "DBVDD", |
| 50 | "DCVDD", |
| 51 | }; |
| 52 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 53 | enum wm2000_anc_mode { |
| 54 | ANC_ACTIVE = 0, |
| 55 | ANC_BYPASS = 1, |
| 56 | ANC_STANDBY = 2, |
| 57 | ANC_OFF = 3, |
| 58 | }; |
| 59 | |
| 60 | struct wm2000_priv { |
| 61 | struct i2c_client *i2c; |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 62 | struct regmap *regmap; |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 63 | struct clk *mclk; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 64 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 65 | struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES]; |
| 66 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 67 | enum wm2000_anc_mode anc_mode; |
| 68 | |
| 69 | unsigned int anc_active:1; |
| 70 | unsigned int anc_eng_ena:1; |
| 71 | unsigned int spk_ena:1; |
| 72 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 73 | unsigned int speech_clarity:1; |
| 74 | |
| 75 | int anc_download_size; |
| 76 | char *anc_download; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 77 | |
| 78 | struct mutex lock; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 81 | static int wm2000_write(struct i2c_client *i2c, unsigned int reg, |
| 82 | unsigned int value) |
| 83 | { |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 84 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
| 85 | return regmap_write(wm2000->regmap, reg, value); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 88 | static void wm2000_reset(struct wm2000_priv *wm2000) |
| 89 | { |
| 90 | struct i2c_client *i2c = wm2000->i2c; |
| 91 | |
| 92 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); |
| 93 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 94 | wm2000_write(i2c, WM2000_REG_ID1, 0); |
| 95 | |
| 96 | wm2000->anc_mode = ANC_OFF; |
| 97 | } |
| 98 | |
| 99 | static int wm2000_poll_bit(struct i2c_client *i2c, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 100 | unsigned int reg, u8 mask) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 101 | { |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 102 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 103 | int timeout = 4000; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 104 | unsigned int val; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 105 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 106 | regmap_read(wm2000->regmap, reg, &val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 107 | |
| 108 | while (!(val & mask) && --timeout) { |
| 109 | msleep(1); |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 110 | regmap_read(wm2000->regmap, reg, &val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (timeout == 0) |
| 114 | return 0; |
| 115 | else |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | static int wm2000_power_up(struct i2c_client *i2c, int analogue) |
| 120 | { |
| 121 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | d61100b | 2012-12-14 15:16:58 +0900 | [diff] [blame] | 122 | unsigned long rate; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 123 | unsigned int val; |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 124 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 125 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 126 | if (WARN_ON(wm2000->anc_mode != ANC_OFF)) |
| 127 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 128 | |
| 129 | dev_dbg(&i2c->dev, "Beginning power up\n"); |
| 130 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 131 | ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 132 | if (ret != 0) { |
| 133 | dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); |
| 134 | return ret; |
| 135 | } |
| 136 | |
Mark Brown | d61100b | 2012-12-14 15:16:58 +0900 | [diff] [blame] | 137 | rate = clk_get_rate(wm2000->mclk); |
| 138 | if (rate <= 13500000) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 139 | dev_dbg(&i2c->dev, "Disabling MCLK divider\n"); |
| 140 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, |
| 141 | WM2000_MCLK_DIV2_ENA_CLR); |
| 142 | } else { |
| 143 | dev_dbg(&i2c->dev, "Enabling MCLK divider\n"); |
| 144 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, |
| 145 | WM2000_MCLK_DIV2_ENA_SET); |
| 146 | } |
| 147 | |
| 148 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); |
| 149 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET); |
| 150 | |
| 151 | /* Wait for ANC engine to become ready */ |
| 152 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 153 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 154 | dev_err(&i2c->dev, "ANC engine failed to reset\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 155 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 156 | return -ETIMEDOUT; |
| 157 | } |
| 158 | |
| 159 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 160 | WM2000_STATUS_BOOT_COMPLETE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 161 | dev_err(&i2c->dev, "ANC engine failed to initialise\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 162 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 163 | return -ETIMEDOUT; |
| 164 | } |
| 165 | |
| 166 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 167 | |
| 168 | /* Open code download of the data since it is the only bulk |
| 169 | * write we do. */ |
| 170 | dev_dbg(&i2c->dev, "Downloading %d bytes\n", |
| 171 | wm2000->anc_download_size - 2); |
| 172 | |
| 173 | ret = i2c_master_send(i2c, wm2000->anc_download, |
| 174 | wm2000->anc_download_size); |
| 175 | if (ret < 0) { |
| 176 | dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 177 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | if (ret != wm2000->anc_download_size) { |
| 181 | dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n", |
| 182 | ret, wm2000->anc_download_size); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 183 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 184 | return -EIO; |
| 185 | } |
| 186 | |
| 187 | dev_dbg(&i2c->dev, "Download complete\n"); |
| 188 | |
| 189 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 190 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 191 | |
| 192 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 193 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 194 | WM2000_MODE_MOUSE_ENABLE | |
| 195 | WM2000_MODE_THERMAL_ENABLE); |
| 196 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 197 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 198 | WM2000_MODE_MOUSE_ENABLE | |
| 199 | WM2000_MODE_THERMAL_ENABLE); |
| 200 | } |
| 201 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 202 | ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val); |
| 203 | if (ret != 0) { |
| 204 | dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret); |
| 205 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 206 | return ret; |
| 207 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 208 | if (wm2000->speech_clarity) |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 209 | val |= WM2000_SPEECH_CLARITY; |
Mark Brown | 267f8fa | 2013-01-04 21:18:12 +0000 | [diff] [blame] | 210 | else |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 211 | val &= ~WM2000_SPEECH_CLARITY; |
| 212 | wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 213 | |
| 214 | wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33); |
| 215 | wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02); |
| 216 | |
| 217 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 218 | |
| 219 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 220 | WM2000_STATUS_MOUSE_ACTIVE)) { |
| 221 | dev_err(&i2c->dev, "Timed out waiting for device\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 222 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 223 | return -ETIMEDOUT; |
| 224 | } |
| 225 | |
| 226 | dev_dbg(&i2c->dev, "ANC active\n"); |
| 227 | if (analogue) |
| 228 | dev_dbg(&i2c->dev, "Analogue active\n"); |
| 229 | wm2000->anc_mode = ANC_ACTIVE; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int wm2000_power_down(struct i2c_client *i2c, int analogue) |
| 235 | { |
| 236 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 237 | |
| 238 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 239 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 240 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 241 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 242 | WM2000_MODE_POWER_DOWN); |
| 243 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 244 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 245 | WM2000_MODE_POWER_DOWN); |
| 246 | } |
| 247 | |
| 248 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 249 | WM2000_STATUS_POWER_DOWN_COMPLETE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 250 | dev_err(&i2c->dev, "Timeout waiting for ANC power down\n"); |
| 251 | return -ETIMEDOUT; |
| 252 | } |
| 253 | |
| 254 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 255 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 256 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
| 257 | return -ETIMEDOUT; |
| 258 | } |
| 259 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 260 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 261 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 262 | dev_dbg(&i2c->dev, "powered off\n"); |
| 263 | wm2000->anc_mode = ANC_OFF; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue) |
| 269 | { |
| 270 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
| 271 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 272 | if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE)) |
| 273 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 274 | |
| 275 | if (analogue) { |
| 276 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 277 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 278 | WM2000_MODE_THERMAL_ENABLE | |
| 279 | WM2000_MODE_BYPASS_ENTRY); |
| 280 | } else { |
| 281 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 282 | WM2000_MODE_THERMAL_ENABLE | |
| 283 | WM2000_MODE_BYPASS_ENTRY); |
| 284 | } |
| 285 | |
| 286 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 287 | WM2000_STATUS_ANC_DISABLED)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 288 | dev_err(&i2c->dev, "Timeout waiting for ANC disable\n"); |
| 289 | return -ETIMEDOUT; |
| 290 | } |
| 291 | |
| 292 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 293 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 294 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
| 295 | return -ETIMEDOUT; |
| 296 | } |
| 297 | |
| 298 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); |
| 299 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 300 | |
| 301 | wm2000->anc_mode = ANC_BYPASS; |
| 302 | dev_dbg(&i2c->dev, "bypass enabled\n"); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue) |
| 308 | { |
| 309 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
| 310 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 311 | if (WARN_ON(wm2000->anc_mode != ANC_BYPASS)) |
| 312 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 313 | |
| 314 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); |
| 315 | |
| 316 | if (analogue) { |
| 317 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 318 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 319 | WM2000_MODE_MOUSE_ENABLE | |
| 320 | WM2000_MODE_THERMAL_ENABLE); |
| 321 | } else { |
| 322 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 323 | WM2000_MODE_MOUSE_ENABLE | |
| 324 | WM2000_MODE_THERMAL_ENABLE); |
| 325 | } |
| 326 | |
| 327 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 328 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 329 | |
| 330 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 331 | WM2000_STATUS_MOUSE_ACTIVE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 332 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); |
| 333 | return -ETIMEDOUT; |
| 334 | } |
| 335 | |
| 336 | wm2000->anc_mode = ANC_ACTIVE; |
| 337 | dev_dbg(&i2c->dev, "MOUSE active\n"); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int wm2000_enter_standby(struct i2c_client *i2c, int analogue) |
| 343 | { |
| 344 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 345 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 346 | if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE)) |
| 347 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 348 | |
| 349 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 350 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 351 | |
| 352 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 353 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 354 | WM2000_MODE_THERMAL_ENABLE | |
| 355 | WM2000_MODE_STANDBY_ENTRY); |
| 356 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 357 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 358 | WM2000_MODE_THERMAL_ENABLE | |
| 359 | WM2000_MODE_STANDBY_ENTRY); |
| 360 | } |
| 361 | |
| 362 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 363 | WM2000_STATUS_ANC_DISABLED)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 364 | dev_err(&i2c->dev, |
| 365 | "Timed out waiting for ANC disable after 1ms\n"); |
| 366 | return -ETIMEDOUT; |
| 367 | } |
| 368 | |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 369 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 370 | dev_err(&i2c->dev, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 371 | "Timed out waiting for standby\n"); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 372 | return -ETIMEDOUT; |
| 373 | } |
| 374 | |
| 375 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); |
| 376 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 377 | |
| 378 | wm2000->anc_mode = ANC_STANDBY; |
| 379 | dev_dbg(&i2c->dev, "standby\n"); |
| 380 | if (analogue) |
| 381 | dev_dbg(&i2c->dev, "Analogue disabled\n"); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int wm2000_exit_standby(struct i2c_client *i2c, int analogue) |
| 387 | { |
| 388 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 389 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 390 | if (WARN_ON(wm2000->anc_mode != ANC_STANDBY)) |
| 391 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 392 | |
| 393 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); |
| 394 | |
| 395 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 396 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 397 | |
| 398 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 399 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 400 | WM2000_MODE_THERMAL_ENABLE | |
| 401 | WM2000_MODE_MOUSE_ENABLE); |
| 402 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 403 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 404 | WM2000_MODE_THERMAL_ENABLE | |
| 405 | WM2000_MODE_MOUSE_ENABLE); |
| 406 | } |
| 407 | |
| 408 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 409 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 410 | |
| 411 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 412 | WM2000_STATUS_MOUSE_ACTIVE)) { |
| 413 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 414 | return -ETIMEDOUT; |
| 415 | } |
| 416 | |
| 417 | wm2000->anc_mode = ANC_ACTIVE; |
| 418 | dev_dbg(&i2c->dev, "MOUSE active\n"); |
| 419 | if (analogue) |
| 420 | dev_dbg(&i2c->dev, "Analogue enabled\n"); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue); |
| 426 | |
| 427 | static struct { |
| 428 | enum wm2000_anc_mode source; |
| 429 | enum wm2000_anc_mode dest; |
| 430 | int analogue; |
| 431 | wm2000_mode_fn step[2]; |
| 432 | } anc_transitions[] = { |
| 433 | { |
| 434 | .source = ANC_OFF, |
| 435 | .dest = ANC_ACTIVE, |
| 436 | .analogue = 1, |
| 437 | .step = { |
| 438 | wm2000_power_up, |
| 439 | }, |
| 440 | }, |
| 441 | { |
| 442 | .source = ANC_OFF, |
| 443 | .dest = ANC_STANDBY, |
| 444 | .step = { |
| 445 | wm2000_power_up, |
| 446 | wm2000_enter_standby, |
| 447 | }, |
| 448 | }, |
| 449 | { |
| 450 | .source = ANC_OFF, |
| 451 | .dest = ANC_BYPASS, |
| 452 | .analogue = 1, |
| 453 | .step = { |
| 454 | wm2000_power_up, |
| 455 | wm2000_enter_bypass, |
| 456 | }, |
| 457 | }, |
| 458 | { |
| 459 | .source = ANC_ACTIVE, |
| 460 | .dest = ANC_BYPASS, |
| 461 | .analogue = 1, |
| 462 | .step = { |
| 463 | wm2000_enter_bypass, |
| 464 | }, |
| 465 | }, |
| 466 | { |
| 467 | .source = ANC_ACTIVE, |
| 468 | .dest = ANC_STANDBY, |
| 469 | .analogue = 1, |
| 470 | .step = { |
| 471 | wm2000_enter_standby, |
| 472 | }, |
| 473 | }, |
| 474 | { |
| 475 | .source = ANC_ACTIVE, |
| 476 | .dest = ANC_OFF, |
| 477 | .analogue = 1, |
| 478 | .step = { |
| 479 | wm2000_power_down, |
| 480 | }, |
| 481 | }, |
| 482 | { |
| 483 | .source = ANC_BYPASS, |
| 484 | .dest = ANC_ACTIVE, |
| 485 | .analogue = 1, |
| 486 | .step = { |
| 487 | wm2000_exit_bypass, |
| 488 | }, |
| 489 | }, |
| 490 | { |
| 491 | .source = ANC_BYPASS, |
| 492 | .dest = ANC_STANDBY, |
| 493 | .analogue = 1, |
| 494 | .step = { |
| 495 | wm2000_exit_bypass, |
| 496 | wm2000_enter_standby, |
| 497 | }, |
| 498 | }, |
| 499 | { |
| 500 | .source = ANC_BYPASS, |
| 501 | .dest = ANC_OFF, |
| 502 | .step = { |
| 503 | wm2000_exit_bypass, |
| 504 | wm2000_power_down, |
| 505 | }, |
| 506 | }, |
| 507 | { |
| 508 | .source = ANC_STANDBY, |
| 509 | .dest = ANC_ACTIVE, |
| 510 | .analogue = 1, |
| 511 | .step = { |
| 512 | wm2000_exit_standby, |
| 513 | }, |
| 514 | }, |
| 515 | { |
| 516 | .source = ANC_STANDBY, |
| 517 | .dest = ANC_BYPASS, |
| 518 | .analogue = 1, |
| 519 | .step = { |
| 520 | wm2000_exit_standby, |
| 521 | wm2000_enter_bypass, |
| 522 | }, |
| 523 | }, |
| 524 | { |
| 525 | .source = ANC_STANDBY, |
| 526 | .dest = ANC_OFF, |
| 527 | .step = { |
| 528 | wm2000_exit_standby, |
| 529 | wm2000_power_down, |
| 530 | }, |
| 531 | }, |
| 532 | }; |
| 533 | |
| 534 | static int wm2000_anc_transition(struct wm2000_priv *wm2000, |
| 535 | enum wm2000_anc_mode mode) |
| 536 | { |
| 537 | struct i2c_client *i2c = wm2000->i2c; |
| 538 | int i, j; |
| 539 | int ret; |
| 540 | |
| 541 | if (wm2000->anc_mode == mode) |
| 542 | return 0; |
| 543 | |
| 544 | for (i = 0; i < ARRAY_SIZE(anc_transitions); i++) |
| 545 | if (anc_transitions[i].source == wm2000->anc_mode && |
| 546 | anc_transitions[i].dest == mode) |
| 547 | break; |
| 548 | if (i == ARRAY_SIZE(anc_transitions)) { |
| 549 | dev_err(&i2c->dev, "No transition for %d->%d\n", |
| 550 | wm2000->anc_mode, mode); |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 554 | /* Maintain clock while active */ |
| 555 | if (anc_transitions[i].source == ANC_OFF) { |
| 556 | ret = clk_prepare_enable(wm2000->mclk); |
| 557 | if (ret != 0) { |
| 558 | dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret); |
| 559 | return ret; |
| 560 | } |
| 561 | } |
| 562 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 563 | for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) { |
| 564 | if (!anc_transitions[i].step[j]) |
| 565 | break; |
| 566 | ret = anc_transitions[i].step[j](i2c, |
| 567 | anc_transitions[i].analogue); |
| 568 | if (ret != 0) |
| 569 | return ret; |
| 570 | } |
| 571 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 572 | if (anc_transitions[i].dest == ANC_OFF) |
| 573 | clk_disable_unprepare(wm2000->mclk); |
| 574 | |
Charles Keepax | fa54aad | 2016-08-11 14:40:04 +0100 | [diff] [blame] | 575 | return 0; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int wm2000_anc_set_mode(struct wm2000_priv *wm2000) |
| 579 | { |
| 580 | struct i2c_client *i2c = wm2000->i2c; |
| 581 | enum wm2000_anc_mode mode; |
| 582 | |
| 583 | if (wm2000->anc_eng_ena && wm2000->spk_ena) |
| 584 | if (wm2000->anc_active) |
| 585 | mode = ANC_ACTIVE; |
| 586 | else |
| 587 | mode = ANC_BYPASS; |
| 588 | else |
| 589 | mode = ANC_STANDBY; |
| 590 | |
| 591 | dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n", |
| 592 | mode, wm2000->anc_eng_ena, !wm2000->spk_ena, |
| 593 | wm2000->anc_active); |
| 594 | |
| 595 | return wm2000_anc_transition(wm2000, mode); |
| 596 | } |
| 597 | |
| 598 | static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol, |
| 599 | struct snd_ctl_elem_value *ucontrol) |
| 600 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 601 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 602 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 603 | |
Takashi Iwai | 00a14c2 | 2015-03-10 12:39:09 +0100 | [diff] [blame] | 604 | ucontrol->value.integer.value[0] = wm2000->anc_active; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol, |
| 610 | struct snd_ctl_elem_value *ucontrol) |
| 611 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 612 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 613 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Dan Carpenter | 8444f59 | 2015-10-13 10:12:45 +0300 | [diff] [blame] | 614 | unsigned int anc_active = ucontrol->value.integer.value[0]; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 615 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 616 | |
| 617 | if (anc_active > 1) |
| 618 | return -EINVAL; |
| 619 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 620 | mutex_lock(&wm2000->lock); |
| 621 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 622 | wm2000->anc_active = anc_active; |
| 623 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 624 | ret = wm2000_anc_set_mode(wm2000); |
| 625 | |
| 626 | mutex_unlock(&wm2000->lock); |
| 627 | |
| 628 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static int wm2000_speaker_get(struct snd_kcontrol *kcontrol, |
| 632 | struct snd_ctl_elem_value *ucontrol) |
| 633 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 634 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 635 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 636 | |
Takashi Iwai | 00a14c2 | 2015-03-10 12:39:09 +0100 | [diff] [blame] | 637 | ucontrol->value.integer.value[0] = wm2000->spk_ena; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int wm2000_speaker_put(struct snd_kcontrol *kcontrol, |
| 643 | struct snd_ctl_elem_value *ucontrol) |
| 644 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 645 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 646 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Dan Carpenter | 8444f59 | 2015-10-13 10:12:45 +0300 | [diff] [blame] | 647 | unsigned int val = ucontrol->value.integer.value[0]; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 648 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 649 | |
| 650 | if (val > 1) |
| 651 | return -EINVAL; |
| 652 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 653 | mutex_lock(&wm2000->lock); |
| 654 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 655 | wm2000->spk_ena = val; |
| 656 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 657 | ret = wm2000_anc_set_mode(wm2000); |
| 658 | |
| 659 | mutex_unlock(&wm2000->lock); |
| 660 | |
| 661 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static const struct snd_kcontrol_new wm2000_controls[] = { |
Mark Brown | 3f3af6e | 2013-01-30 21:35:44 +0800 | [diff] [blame] | 665 | SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 666 | SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0, |
| 667 | wm2000_anc_mode_get, |
| 668 | wm2000_anc_mode_put), |
| 669 | SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0, |
| 670 | wm2000_speaker_get, |
| 671 | wm2000_speaker_put), |
| 672 | }; |
| 673 | |
| 674 | static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w, |
| 675 | struct snd_kcontrol *kcontrol, int event) |
| 676 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 677 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 678 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 679 | int ret; |
| 680 | |
| 681 | mutex_lock(&wm2000->lock); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 682 | |
| 683 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 684 | wm2000->anc_eng_ena = 1; |
| 685 | |
| 686 | if (SND_SOC_DAPM_EVENT_OFF(event)) |
| 687 | wm2000->anc_eng_ena = 0; |
| 688 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 689 | ret = wm2000_anc_set_mode(wm2000); |
| 690 | |
| 691 | mutex_unlock(&wm2000->lock); |
| 692 | |
| 693 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = { |
| 697 | /* Externally visible pins */ |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 698 | SND_SOC_DAPM_OUTPUT("SPKN"), |
| 699 | SND_SOC_DAPM_OUTPUT("SPKP"), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 700 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 701 | SND_SOC_DAPM_INPUT("LINN"), |
| 702 | SND_SOC_DAPM_INPUT("LINP"), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 703 | |
| 704 | SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0, |
| 705 | wm2000_anc_power_event, |
| 706 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 707 | }; |
| 708 | |
| 709 | /* Target, Path, Source */ |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 710 | static const struct snd_soc_dapm_route wm2000_audio_map[] = { |
| 711 | { "SPKN", NULL, "ANC Engine" }, |
| 712 | { "SPKP", NULL, "ANC Engine" }, |
| 713 | { "ANC Engine", NULL, "LINN" }, |
| 714 | { "ANC Engine", NULL, "LINP" }, |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 717 | #ifdef CONFIG_PM |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 718 | static int wm2000_suspend(struct snd_soc_component *component) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 719 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 720 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 721 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 722 | return wm2000_anc_transition(wm2000, ANC_OFF); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 723 | } |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 724 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 725 | static int wm2000_resume(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 726 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 727 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 728 | |
| 729 | return wm2000_anc_set_mode(wm2000); |
| 730 | } |
| 731 | #else |
| 732 | #define wm2000_suspend NULL |
| 733 | #define wm2000_resume NULL |
| 734 | #endif |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 735 | |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 736 | static bool wm2000_readable_reg(struct device *dev, unsigned int reg) |
| 737 | { |
| 738 | switch (reg) { |
| 739 | case WM2000_REG_SYS_START: |
Mark Brown | 33e7546 | 2013-01-22 15:51:08 +0900 | [diff] [blame] | 740 | case WM2000_REG_ANC_GAIN_CTRL: |
| 741 | case WM2000_REG_MSE_TH1: |
| 742 | case WM2000_REG_MSE_TH2: |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 743 | case WM2000_REG_SPEECH_CLARITY: |
| 744 | case WM2000_REG_SYS_WATCHDOG: |
| 745 | case WM2000_REG_ANA_VMID_PD_TIME: |
| 746 | case WM2000_REG_ANA_VMID_PU_TIME: |
| 747 | case WM2000_REG_CAT_FLTR_INDX: |
| 748 | case WM2000_REG_CAT_GAIN_0: |
| 749 | case WM2000_REG_SYS_STATUS: |
| 750 | case WM2000_REG_SYS_MODE_CNTRL: |
| 751 | case WM2000_REG_SYS_START0: |
| 752 | case WM2000_REG_SYS_START1: |
| 753 | case WM2000_REG_ID1: |
| 754 | case WM2000_REG_ID2: |
| 755 | case WM2000_REG_REVISON: |
| 756 | case WM2000_REG_SYS_CTL1: |
| 757 | case WM2000_REG_SYS_CTL2: |
| 758 | case WM2000_REG_ANC_STAT: |
| 759 | case WM2000_REG_IF_CTL: |
Mark Brown | 939dc51 | 2013-03-29 13:03:39 +0800 | [diff] [blame] | 760 | case WM2000_REG_ANA_MIC_CTL: |
| 761 | case WM2000_REG_SPK_CTL: |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 762 | return true; |
| 763 | default: |
| 764 | return false; |
| 765 | } |
| 766 | } |
| 767 | |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 768 | static const struct regmap_config wm2000_regmap = { |
Mark Brown | d0e12f3 | 2012-09-26 11:57:30 +0100 | [diff] [blame] | 769 | .reg_bits = 16, |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 770 | .val_bits = 8, |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 771 | |
Mark Brown | 939dc51 | 2013-03-29 13:03:39 +0800 | [diff] [blame] | 772 | .max_register = WM2000_REG_SPK_CTL, |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 773 | .readable_reg = wm2000_readable_reg, |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 774 | }; |
| 775 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 776 | static int wm2000_probe(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 777 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 778 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 779 | |
| 780 | /* This will trigger a transition to standby mode by default */ |
| 781 | wm2000_anc_set_mode(wm2000); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 786 | static void wm2000_remove(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 787 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 788 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 789 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 790 | wm2000_anc_transition(wm2000, ANC_OFF); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 793 | static const struct snd_soc_component_driver soc_component_dev_wm2000 = { |
| 794 | .probe = wm2000_probe, |
| 795 | .remove = wm2000_remove, |
| 796 | .suspend = wm2000_suspend, |
| 797 | .resume = wm2000_resume, |
| 798 | .controls = wm2000_controls, |
| 799 | .num_controls = ARRAY_SIZE(wm2000_controls), |
| 800 | .dapm_widgets = wm2000_dapm_widgets, |
| 801 | .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets), |
| 802 | .dapm_routes = wm2000_audio_map, |
| 803 | .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map), |
| 804 | .idle_bias_on = 1, |
| 805 | .use_pmdown_time = 1, |
| 806 | .endianness = 1, |
| 807 | .non_legacy_dai_naming = 1, |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 810 | static int wm2000_i2c_probe(struct i2c_client *i2c, |
| 811 | const struct i2c_device_id *i2c_id) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 812 | { |
| 813 | struct wm2000_priv *wm2000; |
| 814 | struct wm2000_platform_data *pdata; |
| 815 | const char *filename; |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 816 | const struct firmware *fw = NULL; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 817 | int ret, i; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 818 | unsigned int reg; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 819 | u16 id; |
| 820 | |
Markus Elfring | bf0842b | 2017-11-24 08:18:14 +0100 | [diff] [blame] | 821 | wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL); |
Sachin Kamat | a0f6211 | 2014-06-20 15:29:07 +0530 | [diff] [blame] | 822 | if (!wm2000) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 823 | return -ENOMEM; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 824 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 825 | mutex_init(&wm2000->lock); |
| 826 | |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 827 | dev_set_drvdata(&i2c->dev, wm2000); |
| 828 | |
Mark Brown | 8fed54a | 2012-09-22 18:32:08 -0400 | [diff] [blame] | 829 | wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap); |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 830 | if (IS_ERR(wm2000->regmap)) { |
| 831 | ret = PTR_ERR(wm2000->regmap); |
| 832 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 833 | ret); |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 834 | goto out; |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 837 | for (i = 0; i < WM2000_NUM_SUPPLIES; i++) |
| 838 | wm2000->supplies[i].supply = wm2000_supplies[i]; |
| 839 | |
| 840 | ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES, |
| 841 | wm2000->supplies); |
| 842 | if (ret != 0) { |
| 843 | dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret); |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 848 | if (ret != 0) { |
| 849 | dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); |
| 850 | return ret; |
| 851 | } |
| 852 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 853 | /* Verify that this is a WM2000 */ |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 854 | ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, ®); |
| 855 | if (ret != 0) { |
| 856 | dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret); |
| 857 | return ret; |
| 858 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 859 | id = reg << 8; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 860 | ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, ®); |
| 861 | if (ret != 0) { |
| 862 | dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret); |
| 863 | return ret; |
| 864 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 865 | id |= reg & 0xff; |
| 866 | |
| 867 | if (id != 0x2000) { |
| 868 | dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id); |
| 869 | ret = -ENODEV; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 870 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 873 | ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, ®); |
| 874 | if (ret != 0) { |
| 875 | dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret); |
| 876 | return ret; |
| 877 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 878 | dev_info(&i2c->dev, "revision %c\n", reg + 'A'); |
| 879 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 880 | wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK"); |
| 881 | if (IS_ERR(wm2000->mclk)) { |
| 882 | ret = PTR_ERR(wm2000->mclk); |
| 883 | dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret); |
| 884 | goto err_supplies; |
| 885 | } |
| 886 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 887 | filename = "wm2000_anc.bin"; |
| 888 | pdata = dev_get_platdata(&i2c->dev); |
| 889 | if (pdata) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 890 | wm2000->speech_clarity = !pdata->speech_enh_disable; |
| 891 | |
| 892 | if (pdata->download_file) |
| 893 | filename = pdata->download_file; |
| 894 | } |
| 895 | |
| 896 | ret = request_firmware(&fw, filename, &i2c->dev); |
| 897 | if (ret != 0) { |
| 898 | dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 899 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* Pre-cook the concatenation of the register address onto the image */ |
| 903 | wm2000->anc_download_size = fw->size + 2; |
Mark Brown | b03e96e | 2011-12-02 21:28:31 +0000 | [diff] [blame] | 904 | wm2000->anc_download = devm_kzalloc(&i2c->dev, |
| 905 | wm2000->anc_download_size, |
| 906 | GFP_KERNEL); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 907 | if (wm2000->anc_download == NULL) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 908 | ret = -ENOMEM; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 909 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | wm2000->anc_download[0] = 0x80; |
| 913 | wm2000->anc_download[1] = 0x00; |
| 914 | memcpy(wm2000->anc_download + 2, fw->data, fw->size); |
| 915 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 916 | wm2000->anc_eng_ena = 1; |
Mark Brown | e71fa37 | 2010-06-15 15:14:00 +0100 | [diff] [blame] | 917 | wm2000->anc_active = 1; |
| 918 | wm2000->spk_ena = 1; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 919 | wm2000->i2c = i2c; |
| 920 | |
| 921 | wm2000_reset(wm2000); |
| 922 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 923 | ret = devm_snd_soc_register_component(&i2c->dev, |
| 924 | &soc_component_dev_wm2000, NULL, 0); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 925 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 926 | err_supplies: |
| 927 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 928 | |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 929 | out: |
| 930 | release_firmware(fw); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 931 | return ret; |
| 932 | } |
| 933 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 934 | static const struct i2c_device_id wm2000_i2c_id[] = { |
| 935 | { "wm2000", 0 }, |
| 936 | { } |
| 937 | }; |
| 938 | MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id); |
| 939 | |
| 940 | static struct i2c_driver wm2000_i2c_driver = { |
| 941 | .driver = { |
| 942 | .name = "wm2000", |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 943 | }, |
| 944 | .probe = wm2000_i2c_probe, |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 945 | .id_table = wm2000_i2c_id, |
| 946 | }; |
| 947 | |
Sachin Kamat | 3a4bfd8 | 2012-08-06 17:25:54 +0530 | [diff] [blame] | 948 | module_i2c_driver(wm2000_i2c_driver); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 949 | |
| 950 | MODULE_DESCRIPTION("ASoC WM2000 driver"); |
| 951 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>"); |
| 952 | MODULE_LICENSE("GPL"); |