wm8994-core.c revision 334e9ab8f9bb90ddf1eff0b07609961a628064b6
1/* 2 * wm8994-core.c -- Device access for Wolfson WM8994 3 * 4 * Copyright 2009 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 it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 * 13 */ 14 15#include <linux/kernel.h> 16#include <linux/module.h> 17#include <linux/slab.h> 18#include <linux/i2c.h> 19#include <linux/delay.h> 20#include <linux/mfd/core.h> 21#include <linux/pm_runtime.h> 22#include <linux/regulator/consumer.h> 23#include <linux/regulator/machine.h> 24 25#include <linux/mfd/wm8994/core.h> 26#include <linux/mfd/wm8994/pdata.h> 27#include <linux/mfd/wm8994/registers.h> 28 29static int wm8994_read(struct wm8994 *wm8994, unsigned short reg, 30 int bytes, void *dest) 31{ 32 int ret, i; 33 u16 *buf = dest; 34 35 BUG_ON(bytes % 2); 36 BUG_ON(bytes <= 0); 37 38 ret = wm8994->read_dev(wm8994, reg, bytes, dest); 39 if (ret < 0) 40 return ret; 41 42 for (i = 0; i < bytes / 2; i++) { 43 buf[i] = be16_to_cpu(buf[i]); 44 45 dev_vdbg(wm8994->dev, "Read %04x from R%d(0x%x)\n", 46 buf[i], reg + i, reg + i); 47 } 48 49 return 0; 50} 51 52/** 53 * wm8994_reg_read: Read a single WM8994 register. 54 * 55 * @wm8994: Device to read from. 56 * @reg: Register to read. 57 */ 58int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg) 59{ 60 unsigned short val; 61 int ret; 62 63 mutex_lock(&wm8994->io_lock); 64 65 ret = wm8994_read(wm8994, reg, 2, &val); 66 67 mutex_unlock(&wm8994->io_lock); 68 69 if (ret < 0) 70 return ret; 71 else 72 return val; 73} 74EXPORT_SYMBOL_GPL(wm8994_reg_read); 75 76/** 77 * wm8994_bulk_read: Read multiple WM8994 registers 78 * 79 * @wm8994: Device to read from 80 * @reg: First register 81 * @count: Number of registers 82 * @buf: Buffer to fill. 83 */ 84int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg, 85 int count, u16 *buf) 86{ 87 int ret; 88 89 mutex_lock(&wm8994->io_lock); 90 91 ret = wm8994_read(wm8994, reg, count * 2, buf); 92 93 mutex_unlock(&wm8994->io_lock); 94 95 return ret; 96} 97EXPORT_SYMBOL_GPL(wm8994_bulk_read); 98 99static int wm8994_write(struct wm8994 *wm8994, unsigned short reg, 100 int bytes, void *src) 101{ 102 u16 *buf = src; 103 int i; 104 105 BUG_ON(bytes % 2); 106 BUG_ON(bytes <= 0); 107 108 for (i = 0; i < bytes / 2; i++) { 109 dev_vdbg(wm8994->dev, "Write %04x to R%d(0x%x)\n", 110 buf[i], reg + i, reg + i); 111 112 buf[i] = cpu_to_be16(buf[i]); 113 } 114 115 return wm8994->write_dev(wm8994, reg, bytes, src); 116} 117 118/** 119 * wm8994_reg_write: Write a single WM8994 register. 120 * 121 * @wm8994: Device to write to. 122 * @reg: Register to write to. 123 * @val: Value to write. 124 */ 125int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg, 126 unsigned short val) 127{ 128 int ret; 129 130 mutex_lock(&wm8994->io_lock); 131 132 ret = wm8994_write(wm8994, reg, 2, &val); 133 134 mutex_unlock(&wm8994->io_lock); 135 136 return ret; 137} 138EXPORT_SYMBOL_GPL(wm8994_reg_write); 139 140/** 141 * wm8994_bulk_write: Write multiple WM8994 registers 142 * 143 * @wm8994: Device to write to 144 * @reg: First register 145 * @count: Number of registers 146 * @buf: Buffer to write from. 147 */ 148int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg, 149 int count, u16 *buf) 150{ 151 int ret; 152 153 mutex_lock(&wm8994->io_lock); 154 155 ret = wm8994_write(wm8994, reg, count * 2, buf); 156 157 mutex_unlock(&wm8994->io_lock); 158 159 return ret; 160} 161EXPORT_SYMBOL_GPL(wm8994_bulk_write); 162 163/** 164 * wm8994_set_bits: Set the value of a bitfield in a WM8994 register 165 * 166 * @wm8994: Device to write to. 167 * @reg: Register to write to. 168 * @mask: Mask of bits to set. 169 * @val: Value to set (unshifted) 170 */ 171int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg, 172 unsigned short mask, unsigned short val) 173{ 174 int ret; 175 u16 r; 176 177 mutex_lock(&wm8994->io_lock); 178 179 ret = wm8994_read(wm8994, reg, 2, &r); 180 if (ret < 0) 181 goto out; 182 183 r &= ~mask; 184 r |= val; 185 186 ret = wm8994_write(wm8994, reg, 2, &r); 187 188out: 189 mutex_unlock(&wm8994->io_lock); 190 191 return ret; 192} 193EXPORT_SYMBOL_GPL(wm8994_set_bits); 194 195static struct mfd_cell wm8994_regulator_devs[] = { 196 { 197 .name = "wm8994-ldo", 198 .id = 1, 199 .pm_runtime_no_callbacks = true, 200 }, 201 { 202 .name = "wm8994-ldo", 203 .id = 2, 204 .pm_runtime_no_callbacks = true, 205 }, 206}; 207 208static struct resource wm8994_codec_resources[] = { 209 { 210 .start = WM8994_IRQ_TEMP_SHUT, 211 .end = WM8994_IRQ_TEMP_WARN, 212 .flags = IORESOURCE_IRQ, 213 }, 214}; 215 216static struct resource wm8994_gpio_resources[] = { 217 { 218 .start = WM8994_IRQ_GPIO(1), 219 .end = WM8994_IRQ_GPIO(11), 220 .flags = IORESOURCE_IRQ, 221 }, 222}; 223 224static struct mfd_cell wm8994_devs[] = { 225 { 226 .name = "wm8994-codec", 227 .num_resources = ARRAY_SIZE(wm8994_codec_resources), 228 .resources = wm8994_codec_resources, 229 }, 230 231 { 232 .name = "wm8994-gpio", 233 .num_resources = ARRAY_SIZE(wm8994_gpio_resources), 234 .resources = wm8994_gpio_resources, 235 .pm_runtime_no_callbacks = true, 236 }, 237}; 238 239/* 240 * Supplies for the main bulk of CODEC; the LDO supplies are ignored 241 * and should be handled via the standard regulator API supply 242 * management. 243 */ 244static const char *wm8994_main_supplies[] = { 245 "DBVDD", 246 "DCVDD", 247 "AVDD1", 248 "AVDD2", 249 "CPVDD", 250 "SPKVDD1", 251 "SPKVDD2", 252}; 253 254static const char *wm8958_main_supplies[] = { 255 "DBVDD1", 256 "DBVDD2", 257 "DBVDD3", 258 "DCVDD", 259 "AVDD1", 260 "AVDD2", 261 "CPVDD", 262 "SPKVDD1", 263 "SPKVDD2", 264}; 265 266#ifdef CONFIG_PM 267static int wm8994_suspend(struct device *dev) 268{ 269 struct wm8994 *wm8994 = dev_get_drvdata(dev); 270 int ret; 271 272 /* Don't actually go through with the suspend if the CODEC is 273 * still active (eg, for audio passthrough from CP. */ 274 ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_1); 275 if (ret < 0) { 276 dev_err(dev, "Failed to read power status: %d\n", ret); 277 } else if (ret & WM8994_VMID_SEL_MASK) { 278 dev_dbg(dev, "CODEC still active, ignoring suspend\n"); 279 return 0; 280 } 281 282 /* GPIO configuration state is saved here since we may be configuring 283 * the GPIO alternate functions even if we're not using the gpiolib 284 * driver for them. 285 */ 286 ret = wm8994_read(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2, 287 &wm8994->gpio_regs); 288 if (ret < 0) 289 dev_err(dev, "Failed to save GPIO registers: %d\n", ret); 290 291 /* For similar reasons we also stash the regulator states */ 292 ret = wm8994_read(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2, 293 &wm8994->ldo_regs); 294 if (ret < 0) 295 dev_err(dev, "Failed to save LDO registers: %d\n", ret); 296 297 /* Explicitly put the device into reset in case regulators 298 * don't get disabled in order to ensure consistent restart. 299 */ 300 wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET, 0x8994); 301 302 wm8994->suspended = true; 303 304 ret = regulator_bulk_disable(wm8994->num_supplies, 305 wm8994->supplies); 306 if (ret != 0) { 307 dev_err(dev, "Failed to disable supplies: %d\n", ret); 308 return ret; 309 } 310 311 return 0; 312} 313 314static int wm8994_resume(struct device *dev) 315{ 316 struct wm8994 *wm8994 = dev_get_drvdata(dev); 317 int ret; 318 319 /* We may have lied to the PM core about suspending */ 320 if (!wm8994->suspended) 321 return 0; 322 323 ret = regulator_bulk_enable(wm8994->num_supplies, 324 wm8994->supplies); 325 if (ret != 0) { 326 dev_err(dev, "Failed to enable supplies: %d\n", ret); 327 return ret; 328 } 329 330 ret = wm8994_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK, 331 WM8994_NUM_IRQ_REGS * 2, &wm8994->irq_masks_cur); 332 if (ret < 0) 333 dev_err(dev, "Failed to restore interrupt masks: %d\n", ret); 334 335 ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2, 336 &wm8994->ldo_regs); 337 if (ret < 0) 338 dev_err(dev, "Failed to restore LDO registers: %d\n", ret); 339 340 ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2, 341 &wm8994->gpio_regs); 342 if (ret < 0) 343 dev_err(dev, "Failed to restore GPIO registers: %d\n", ret); 344 345 wm8994->suspended = false; 346 347 return 0; 348} 349#endif 350 351#ifdef CONFIG_REGULATOR 352static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo) 353{ 354 struct wm8994_ldo_pdata *ldo_pdata; 355 356 if (!pdata) 357 return 0; 358 359 ldo_pdata = &pdata->ldo[ldo]; 360 361 if (!ldo_pdata->init_data) 362 return 0; 363 364 return ldo_pdata->init_data->num_consumer_supplies != 0; 365} 366#else 367static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo) 368{ 369 return 0; 370} 371#endif 372 373/* 374 * Instantiate the generic non-control parts of the device. 375 */ 376static int wm8994_device_init(struct wm8994 *wm8994, int irq) 377{ 378 struct wm8994_pdata *pdata = wm8994->dev->platform_data; 379 const char *devname; 380 int ret, i; 381 382 mutex_init(&wm8994->io_lock); 383 dev_set_drvdata(wm8994->dev, wm8994); 384 385 /* Add the on-chip regulators first for bootstrapping */ 386 ret = mfd_add_devices(wm8994->dev, -1, 387 wm8994_regulator_devs, 388 ARRAY_SIZE(wm8994_regulator_devs), 389 NULL, 0); 390 if (ret != 0) { 391 dev_err(wm8994->dev, "Failed to add children: %d\n", ret); 392 goto err; 393 } 394 395 switch (wm8994->type) { 396 case WM8994: 397 wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies); 398 break; 399 case WM8958: 400 wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies); 401 break; 402 default: 403 BUG(); 404 return -EINVAL; 405 } 406 407 wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) * 408 wm8994->num_supplies, 409 GFP_KERNEL); 410 if (!wm8994->supplies) { 411 ret = -ENOMEM; 412 goto err; 413 } 414 415 switch (wm8994->type) { 416 case WM8994: 417 for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++) 418 wm8994->supplies[i].supply = wm8994_main_supplies[i]; 419 break; 420 case WM8958: 421 for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++) 422 wm8994->supplies[i].supply = wm8958_main_supplies[i]; 423 break; 424 default: 425 BUG(); 426 return -EINVAL; 427 } 428 429 ret = regulator_bulk_get(wm8994->dev, wm8994->num_supplies, 430 wm8994->supplies); 431 if (ret != 0) { 432 dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret); 433 goto err_supplies; 434 } 435 436 ret = regulator_bulk_enable(wm8994->num_supplies, 437 wm8994->supplies); 438 if (ret != 0) { 439 dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret); 440 goto err_get; 441 } 442 443 ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET); 444 if (ret < 0) { 445 dev_err(wm8994->dev, "Failed to read ID register\n"); 446 goto err_enable; 447 } 448 switch (ret) { 449 case 0x8994: 450 devname = "WM8994"; 451 if (wm8994->type != WM8994) 452 dev_warn(wm8994->dev, "Device registered as type %d\n", 453 wm8994->type); 454 wm8994->type = WM8994; 455 break; 456 case 0x8958: 457 devname = "WM8958"; 458 if (wm8994->type != WM8958) 459 dev_warn(wm8994->dev, "Device registered as type %d\n", 460 wm8994->type); 461 wm8994->type = WM8958; 462 break; 463 default: 464 dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n", 465 ret); 466 ret = -EINVAL; 467 goto err_enable; 468 } 469 470 ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION); 471 if (ret < 0) { 472 dev_err(wm8994->dev, "Failed to read revision register: %d\n", 473 ret); 474 goto err_enable; 475 } 476 477 switch (ret) { 478 case 0: 479 case 1: 480 if (wm8994->type == WM8994) 481 dev_warn(wm8994->dev, 482 "revision %c not fully supported\n", 483 'A' + ret); 484 break; 485 default: 486 break; 487 } 488 489 dev_info(wm8994->dev, "%s revision %c\n", devname, 'A' + ret); 490 491 if (pdata) { 492 wm8994->irq_base = pdata->irq_base; 493 wm8994->gpio_base = pdata->gpio_base; 494 495 /* GPIO configuration is only applied if it's non-zero */ 496 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) { 497 if (pdata->gpio_defaults[i]) { 498 wm8994_set_bits(wm8994, WM8994_GPIO_1 + i, 499 0xffff, 500 pdata->gpio_defaults[i]); 501 } 502 } 503 } 504 505 /* In some system designs where the regulators are not in use, 506 * we can achieve a small reduction in leakage currents by 507 * floating LDO outputs. This bit makes no difference if the 508 * LDOs are enabled, it only affects cases where the LDOs were 509 * in operation and are then disabled. 510 */ 511 for (i = 0; i < WM8994_NUM_LDO_REGS; i++) { 512 if (wm8994_ldo_in_use(pdata, i)) 513 wm8994_set_bits(wm8994, WM8994_LDO_1 + i, 514 WM8994_LDO1_DISCH, WM8994_LDO1_DISCH); 515 else 516 wm8994_set_bits(wm8994, WM8994_LDO_1 + i, 517 WM8994_LDO1_DISCH, 0); 518 } 519 520 wm8994_irq_init(wm8994); 521 522 ret = mfd_add_devices(wm8994->dev, -1, 523 wm8994_devs, ARRAY_SIZE(wm8994_devs), 524 NULL, 0); 525 if (ret != 0) { 526 dev_err(wm8994->dev, "Failed to add children: %d\n", ret); 527 goto err_irq; 528 } 529 530 pm_runtime_enable(wm8994->dev); 531 pm_runtime_resume(wm8994->dev); 532 533 return 0; 534 535err_irq: 536 wm8994_irq_exit(wm8994); 537err_enable: 538 regulator_bulk_disable(wm8994->num_supplies, 539 wm8994->supplies); 540err_get: 541 regulator_bulk_free(wm8994->num_supplies, wm8994->supplies); 542err_supplies: 543 kfree(wm8994->supplies); 544err: 545 mfd_remove_devices(wm8994->dev); 546 kfree(wm8994); 547 return ret; 548} 549 550static void wm8994_device_exit(struct wm8994 *wm8994) 551{ 552 pm_runtime_disable(wm8994->dev); 553 mfd_remove_devices(wm8994->dev); 554 wm8994_irq_exit(wm8994); 555 regulator_bulk_disable(wm8994->num_supplies, 556 wm8994->supplies); 557 regulator_bulk_free(wm8994->num_supplies, wm8994->supplies); 558 kfree(wm8994->supplies); 559 kfree(wm8994); 560} 561 562static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg, 563 int bytes, void *dest) 564{ 565 struct i2c_client *i2c = wm8994->control_data; 566 int ret; 567 u16 r = cpu_to_be16(reg); 568 569 ret = i2c_master_send(i2c, (unsigned char *)&r, 2); 570 if (ret < 0) 571 return ret; 572 if (ret != 2) 573 return -EIO; 574 575 ret = i2c_master_recv(i2c, dest, bytes); 576 if (ret < 0) 577 return ret; 578 if (ret != bytes) 579 return -EIO; 580 return 0; 581} 582 583static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg, 584 int bytes, void *src) 585{ 586 struct i2c_client *i2c = wm8994->control_data; 587 struct i2c_msg xfer[2]; 588 int ret; 589 590 reg = cpu_to_be16(reg); 591 592 xfer[0].addr = i2c->addr; 593 xfer[0].flags = 0; 594 xfer[0].len = 2; 595 xfer[0].buf = (char *)® 596 597 xfer[1].addr = i2c->addr; 598 xfer[1].flags = I2C_M_NOSTART; 599 xfer[1].len = bytes; 600 xfer[1].buf = (char *)src; 601 602 ret = i2c_transfer(i2c->adapter, xfer, 2); 603 if (ret < 0) 604 return ret; 605 if (ret != 2) 606 return -EIO; 607 608 return 0; 609} 610 611static int wm8994_i2c_probe(struct i2c_client *i2c, 612 const struct i2c_device_id *id) 613{ 614 struct wm8994 *wm8994; 615 616 wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL); 617 if (wm8994 == NULL) 618 return -ENOMEM; 619 620 i2c_set_clientdata(i2c, wm8994); 621 wm8994->dev = &i2c->dev; 622 wm8994->control_data = i2c; 623 wm8994->read_dev = wm8994_i2c_read_device; 624 wm8994->write_dev = wm8994_i2c_write_device; 625 wm8994->irq = i2c->irq; 626 wm8994->type = id->driver_data; 627 628 return wm8994_device_init(wm8994, i2c->irq); 629} 630 631static int wm8994_i2c_remove(struct i2c_client *i2c) 632{ 633 struct wm8994 *wm8994 = i2c_get_clientdata(i2c); 634 635 wm8994_device_exit(wm8994); 636 637 return 0; 638} 639 640static const struct i2c_device_id wm8994_i2c_id[] = { 641 { "wm8994", WM8994 }, 642 { "wm8958", WM8958 }, 643 { } 644}; 645MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id); 646 647static UNIVERSAL_DEV_PM_OPS(wm8994_pm_ops, wm8994_suspend, wm8994_resume, 648 NULL); 649 650static struct i2c_driver wm8994_i2c_driver = { 651 .driver = { 652 .name = "wm8994", 653 .owner = THIS_MODULE, 654 .pm = &wm8994_pm_ops, 655 }, 656 .probe = wm8994_i2c_probe, 657 .remove = wm8994_i2c_remove, 658 .id_table = wm8994_i2c_id, 659}; 660 661static int __init wm8994_i2c_init(void) 662{ 663 int ret; 664 665 ret = i2c_add_driver(&wm8994_i2c_driver); 666 if (ret != 0) 667 pr_err("Failed to register wm8994 I2C driver: %d\n", ret); 668 669 return ret; 670} 671module_init(wm8994_i2c_init); 672 673static void __exit wm8994_i2c_exit(void) 674{ 675 i2c_del_driver(&wm8994_i2c_driver); 676} 677module_exit(wm8994_i2c_exit); 678 679MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC"); 680MODULE_LICENSE("GPL"); 681MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 682