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