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